diff --git a/Android.mk b/Android.mk index 84a2b48c58b169c1b5dd2bb3aa11c988a26efc71..c17ee04214ded88aed0033511cfd07db9610cf5e 100644 --- a/Android.mk +++ b/Android.mk @@ -1,5 +1,3 @@ -ifeq ($(BOARD_USES_LEGACY_CAMERA), true) - LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) @@ -8,69 +6,38 @@ LOCAL_MODULE_TAGS := optional LOCAL_STATIC_JAVA_LIBRARIES := android-support-v13 LOCAL_STATIC_JAVA_LIBRARIES += com.android.gallery3d.common2 +LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit +LOCAL_STATIC_JAVA_LIBRARIES += mp4parser +LOCAL_STATIC_JAVA_LIBRARIES += android-support-v8-renderscript -LOCAL_SRC_FILES := $(call all-java-files-under, src) -LOCAL_SRC_FILES += $(call all-java-files-under, src_pd) -LOCAL_SRC_FILES += $(call all-java-files-under, ../LegacyCamera/src) - -LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/res packages/apps/LegacyCamera/res -LOCAL_AAPT_FLAGS := --auto-add-overlay --extra-packages com.android.camera - -LOCAL_PACKAGE_NAME := Gallery2 - -LOCAL_OVERRIDES_PACKAGES := Gallery Gallery3D GalleryNew3D - -LOCAL_JNI_SHARED_LIBRARIES := libjni_legacymosaic libjni_eglfence - -LOCAL_REQUIRED_MODULES := libjni_legacymosaic libjni_eglfence - -LOCAL_PROGUARD_FLAG_FILES := proguard.flags - -include $(BUILD_PACKAGE) - -include $(call all-makefiles-under, jni) - -ifeq ($(strip $(LOCAL_PACKAGE_OVERRIDES)),) -# Use the following include to make gallery test apk. -include $(call all-makefiles-under, $(LOCAL_PATH)) - -# Use the following include to make camera test apk. -include $(call all-makefiles-under, ../LegacyCamera) -endif #gallery and camera test apk +LOCAL_RENDERSCRIPT_TARGET_API := 18 +LOCAL_RENDERSCRIPT_COMPATIBILITY := 18 +LOCAL_RENDERSCRIPT_FLAGS := -rs-package-name=android.support.v8.renderscript -else #BOARD_USES_LEGACY_CAMERA +# Keep track of previously compiled RS files too (from bundled GalleryGoogle). +prev_compiled_rs_files := $(call all-renderscript-files-under, src) -LOCAL_PATH:= $(call my-dir) +# We already have these files from GalleryGoogle, so don't install them. +LOCAL_RENDERSCRIPT_SKIP_INSTALL := $(prev_compiled_rs_files) -include $(CLEAR_VARS) - -LOCAL_MODULE_TAGS := optional - -LOCAL_STATIC_JAVA_LIBRARIES := android-support-v13 -LOCAL_STATIC_JAVA_LIBRARIES += com.android.gallery3d.common2 -LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit -LOCAL_STATIC_JAVA_LIBRARIES += mp4parser - -LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src) +LOCAL_SRC_FILES := $(call all-java-files-under, src) $(prev_compiled_rs_files) LOCAL_SRC_FILES += $(call all-java-files-under, src_pd) -LOCAL_SRC_FILES += $(call all-java-files-under, ../Camera/src) -LOCAL_SRC_FILES += $(call all-renderscript-files-under, ../Camera/src) -LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/res -LOCAL_RESOURCE_DIR += packages/apps/Camera/res +LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/res -LOCAL_AAPT_FLAGS := --auto-add-overlay \ - --extra-packages com.android.camera +LOCAL_AAPT_FLAGS := --auto-add-overlay LOCAL_PACKAGE_NAME := Gallery2 LOCAL_OVERRIDES_PACKAGES := Gallery Gallery3D GalleryNew3D +LOCAL_SDK_VERSION := current + # If this is an unbundled build (to install seprately) then include # the libraries in the APK, otherwise just put them in /system/lib and # leave them out of the APK ifneq (,$(TARGET_BUILD_APPS)) - LOCAL_JNI_SHARED_LIBRARIES := libjni_mosaic libjni_eglfence libjni_filtershow_filters + LOCAL_JNI_SHARED_LIBRARIES := libjni_mosaic libjni_eglfence libjni_filtershow_filters librsjni else LOCAL_REQUIRED_MODULES := libjni_mosaic libjni_eglfence libjni_filtershow_filters endif @@ -82,11 +49,8 @@ include $(BUILD_PACKAGE) include $(call all-makefiles-under, jni) ifeq ($(strip $(LOCAL_PACKAGE_OVERRIDES)),) -# Use the following include to make gallery test apk. -include $(call all-makefiles-under, $(LOCAL_PATH)) -# Use the following include to make camera test apk. -include $(call all-makefiles-under, ../Camera) -endif #gallery and camera test apk +# Use the following include to make gallery test apk and the mosaic library +include $(call all-makefiles-under, $(LOCAL_PATH)) -endif #BOARD_USES_LEGACY_CAMERA +endif diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a95134d1b3449d94fdb857c662b9facc5be9661c..7c68d8ec18f7e59299c01cb5732f7795e2fa87eb 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,13 +1,13 @@ - - + @@ -20,7 +20,6 @@ - @@ -32,6 +31,7 @@ + + android:largeHeap="true" + android:backupAgent="com.android.camera.CameraBackupAgent" + android:restoreAnyVersion="true"> + @@ -104,6 +108,20 @@ + + + + + + + + + + + + + @@ -124,6 +142,7 @@ + @@ -143,20 +162,6 @@ - - - - - - - - - - - - - @@ -170,16 +175,19 @@ - - + + + - - - - - - - - - - - - - - @@ -244,8 +235,15 @@ - - + + + + + @@ -271,6 +269,11 @@ android:exported="true" android:permission="com.android.gallery3d.permission.GALLERY_PROVIDER" android:authorities="com.android.gallery3d.provider" /> + + + diff --git a/CleanSpec.mk b/CleanSpec.mk index cc930a144423cd722fdaa616698ed2646c27582d..20db309190f1c90bd54e0913d175f7e472428a82 100644 --- a/CleanSpec.mk +++ b/CleanSpec.mk @@ -47,6 +47,9 @@ $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/APPS/Camera*) $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/Camera*) $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/APPS/Gallery*) $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/Gallery*) +$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/Gallery*) +$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/Gallery*) +$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/Gallery*) # ************************************************ # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST diff --git a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java index 837777e518b2163e64ce702a3389b38b91235686..dde4c567aeb98ab8ec48eeecadfd958673818acb 100644 --- a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java +++ b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java @@ -22,6 +22,7 @@ import android.hardware.Camera; import android.os.Build; import android.provider.MediaStore.MediaColumns; import android.view.View; +import android.view.WindowManager; import java.lang.reflect.Field; @@ -36,8 +37,11 @@ public class ApiHelper { public static final int ICE_CREAM_SANDWICH_MR1 = 15; public static final int JELLY_BEAN = 16; public static final int JELLY_BEAN_MR1 = 17; + public static final int JELLY_BEAN_MR2 = 18; } + public static final boolean AT_LEAST_16 = Build.VERSION.SDK_INT >= 16; + public static final boolean USE_888_PIXEL_FORMAT = Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; @@ -170,6 +174,30 @@ public class ApiHelper { public static final boolean HAS_POST_ON_ANIMATION = Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; + public static final boolean HAS_ANNOUNCE_FOR_ACCESSIBILITY = + Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; + + public static final boolean HAS_OBJECT_ANIMATION = + Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; + + public static final boolean HAS_GLES20_REQUIRED = + Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; + + public static final boolean HAS_ROTATION_ANIMATION = + hasField(WindowManager.LayoutParams.class, "rotationAnimation"); + + public static final boolean HAS_ORIENTATION_LOCK = + Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2; + + public static final boolean HAS_CANCELLATION_SIGNAL = + Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; + + public static final boolean HAS_MEDIA_MUXER = + Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2; + + public static final boolean HAS_DISPLAY_LISTENER = + Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1; + public static int getIntFieldIfExists(Class klass, String fieldName, Class obj, int defaultVal) { try { diff --git a/gallerycommon/src/com/android/gallery3d/common/ExifTags.java b/gallerycommon/src/com/android/gallery3d/common/ExifTags.java deleted file mode 100644 index 9b11fe4169a68c7b0fd6bfedaa8be888a85644b2..0000000000000000000000000000000000000000 --- a/gallerycommon/src/com/android/gallery3d/common/ExifTags.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.gallery3d.common; - -/** - * The class holds the EXIF tag names that are not available in - * {@link android.media.ExifInterface} prior to API level 11. - */ -public interface ExifTags { - static final String TAG_ISO = "ISOSpeedRatings"; - static final String TAG_EXPOSURE_TIME = "ExposureTime"; - static final String TAG_APERTURE = "FNumber"; -} diff --git a/gallerycommon/src/com/android/gallery3d/common/LongSparseArray.java b/gallerycommon/src/com/android/gallery3d/common/LongSparseArray.java deleted file mode 100644 index b3298e6726a6c321b8452d0a790f55a1ba0f9cbc..0000000000000000000000000000000000000000 --- a/gallerycommon/src/com/android/gallery3d/common/LongSparseArray.java +++ /dev/null @@ -1,368 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.gallery3d.common; - - -// Copied from android.util.LongSparseArray for unbundling - -/** - * SparseArray mapping longs to Objects. Unlike a normal array of Objects, - * there can be gaps in the indices. It is intended to be more efficient - * than using a HashMap to map Longs to Objects. - */ -public class LongSparseArray implements Cloneable { - private static final Object DELETED = new Object(); - private boolean mGarbage = false; - - private long[] mKeys; - private Object[] mValues; - private int mSize; - - /** - * Creates a new LongSparseArray containing no mappings. - */ - public LongSparseArray() { - this(10); - } - - /** - * Creates a new LongSparseArray containing no mappings that will not - * require any additional memory allocation to store the specified - * number of mappings. - */ - public LongSparseArray(int initialCapacity) { - initialCapacity = idealLongArraySize(initialCapacity); - - mKeys = new long[initialCapacity]; - mValues = new Object[initialCapacity]; - mSize = 0; - } - - @Override - @SuppressWarnings("unchecked") - public LongSparseArray clone() { - LongSparseArray clone = null; - try { - clone = (LongSparseArray) super.clone(); - clone.mKeys = mKeys.clone(); - clone.mValues = mValues.clone(); - } catch (CloneNotSupportedException cnse) { - /* ignore */ - } - return clone; - } - - /** - * Gets the Object mapped from the specified key, or null - * if no such mapping has been made. - */ - public E get(long key) { - return get(key, null); - } - - /** - * Gets the Object mapped from the specified key, or the specified Object - * if no such mapping has been made. - */ - @SuppressWarnings("unchecked") - public E get(long key, E valueIfKeyNotFound) { - int i = binarySearch(mKeys, 0, mSize, key); - - if (i < 0 || mValues[i] == DELETED) { - return valueIfKeyNotFound; - } else { - return (E) mValues[i]; - } - } - - /** - * Removes the mapping from the specified key, if there was any. - */ - public void delete(long key) { - int i = binarySearch(mKeys, 0, mSize, key); - - if (i >= 0) { - if (mValues[i] != DELETED) { - mValues[i] = DELETED; - mGarbage = true; - } - } - } - - /** - * Alias for {@link #delete(long)}. - */ - public void remove(long key) { - delete(key); - } - - /** - * Removes the mapping at the specified index. - */ - public void removeAt(int index) { - if (mValues[index] != DELETED) { - mValues[index] = DELETED; - mGarbage = true; - } - } - - private void gc() { - // Log.e("SparseArray", "gc start with " + mSize); - - int n = mSize; - int o = 0; - long[] keys = mKeys; - Object[] values = mValues; - - for (int i = 0; i < n; i++) { - Object val = values[i]; - - if (val != DELETED) { - if (i != o) { - keys[o] = keys[i]; - values[o] = val; - values[i] = null; - } - - o++; - } - } - - mGarbage = false; - mSize = o; - - // Log.e("SparseArray", "gc end with " + mSize); - } - - /** - * Adds a mapping from the specified key to the specified value, - * replacing the previous mapping from the specified key if there - * was one. - */ - public void put(long key, E value) { - int i = binarySearch(mKeys, 0, mSize, key); - - if (i >= 0) { - mValues[i] = value; - } else { - i = ~i; - - if (i < mSize && mValues[i] == DELETED) { - mKeys[i] = key; - mValues[i] = value; - return; - } - - if (mGarbage && mSize >= mKeys.length) { - gc(); - - // Search again because indices may have changed. - i = ~binarySearch(mKeys, 0, mSize, key); - } - - if (mSize >= mKeys.length) { - int n = idealLongArraySize(mSize + 1); - - long[] nkeys = new long[n]; - Object[] nvalues = new Object[n]; - - // Log.e("SparseArray", "grow " + mKeys.length + " to " + n); - System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); - System.arraycopy(mValues, 0, nvalues, 0, mValues.length); - - mKeys = nkeys; - mValues = nvalues; - } - - if (mSize - i != 0) { - // Log.e("SparseArray", "move " + (mSize - i)); - System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); - System.arraycopy(mValues, i, mValues, i + 1, mSize - i); - } - - mKeys[i] = key; - mValues[i] = value; - mSize++; - } - } - - /** - * Returns the number of key-value mappings that this LongSparseArray - * currently stores. - */ - public int size() { - if (mGarbage) { - gc(); - } - - return mSize; - } - - /** - * Given an index in the range 0...size()-1, returns - * the key from the indexth key-value mapping that this - * LongSparseArray stores. - */ - public long keyAt(int index) { - if (mGarbage) { - gc(); - } - - return mKeys[index]; - } - - /** - * Given an index in the range 0...size()-1, returns - * the value from the indexth key-value mapping that this - * LongSparseArray stores. - */ - @SuppressWarnings("unchecked") - public E valueAt(int index) { - if (mGarbage) { - gc(); - } - - return (E) mValues[index]; - } - - /** - * Given an index in the range 0...size()-1, sets a new - * value for the indexth key-value mapping that this - * LongSparseArray stores. - */ - public void setValueAt(int index, E value) { - if (mGarbage) { - gc(); - } - - mValues[index] = value; - } - - /** - * Returns the index for which {@link #keyAt} would return the - * specified key, or a negative number if the specified - * key is not mapped. - */ - public int indexOfKey(long key) { - if (mGarbage) { - gc(); - } - - return binarySearch(mKeys, 0, mSize, key); - } - - /** - * Returns an index for which {@link #valueAt} would return the - * specified key, or a negative number if no keys map to the - * specified value. - * Beware that this is a linear search, unlike lookups by key, - * and that multiple keys can map to the same value and this will - * find only one of them. - */ - public int indexOfValue(E value) { - if (mGarbage) { - gc(); - } - - for (int i = 0; i < mSize; i++) - if (mValues[i] == value) - return i; - - return -1; - } - - /** - * Removes all key-value mappings from this LongSparseArray. - */ - public void clear() { - int n = mSize; - Object[] values = mValues; - - for (int i = 0; i < n; i++) { - values[i] = null; - } - - mSize = 0; - mGarbage = false; - } - - /** - * Puts a key/value pair into the array, optimizing for the case where - * the key is greater than all existing keys in the array. - */ - public void append(long key, E value) { - if (mSize != 0 && key <= mKeys[mSize - 1]) { - put(key, value); - return; - } - - if (mGarbage && mSize >= mKeys.length) { - gc(); - } - - int pos = mSize; - if (pos >= mKeys.length) { - int n = idealLongArraySize(pos + 1); - - long[] nkeys = new long[n]; - Object[] nvalues = new Object[n]; - - // Log.e("SparseArray", "grow " + mKeys.length + " to " + n); - System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); - System.arraycopy(mValues, 0, nvalues, 0, mValues.length); - - mKeys = nkeys; - mValues = nvalues; - } - - mKeys[pos] = key; - mValues[pos] = value; - mSize = pos + 1; - } - - private static int binarySearch(long[] a, int start, int len, long key) { - int high = start + len, low = start - 1, guess; - - while (high - low > 1) { - guess = (high + low) / 2; - - if (a[guess] < key) - low = guess; - else - high = guess; - } - - if (high == start + len) - return ~(start + len); - else if (a[high] == key) - return high; - else - return ~high; - } - - private static int idealByteArraySize(int need) { - for (int i = 4; i < 32; i++) - if (need <= (1 << i) - 12) - return (1 << i) - 12; - - return need; - } - - public static int idealLongArraySize(int need) { - return idealByteArraySize(need * 8) / 8; - } -} diff --git a/gallerycommon/src/com/android/gallery3d/common/Utils.java b/gallerycommon/src/com/android/gallery3d/common/Utils.java index 3a68745c4ff991a7069283fc1c11c2cdf7061766..614a081c8ffc44095c55702eb3ba6765d385c392 100644 --- a/gallerycommon/src/com/android/gallery3d/common/Utils.java +++ b/gallerycommon/src/com/android/gallery3d/common/Utils.java @@ -26,6 +26,7 @@ import android.text.TextUtils; import android.util.Log; import java.io.Closeable; +import java.io.IOException; import java.io.InterruptedIOException; public class Utils { @@ -173,8 +174,8 @@ public class Utils { if (c == null) return; try { c.close(); - } catch (Throwable t) { - Log.w(TAG, "close fail", t); + } catch (IOException t) { + Log.w(TAG, "close fail ", t); } } diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java b/gallerycommon/src/com/android/gallery3d/exif/ByteBufferInputStream.java similarity index 54% rename from src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java rename to gallerycommon/src/com/android/gallery3d/exif/ByteBufferInputStream.java index 95edc5d153b8d7651b7956cf0884fc508386b1e0..7fb9f22cc0d9aaa0584c36df9f46e88a24e3b2e9 100644 --- a/src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ByteBufferInputStream.java @@ -14,32 +14,35 @@ * limitations under the License. */ -package com.android.gallery3d.filtershow.presets; +package com.android.gallery3d.exif; -import android.graphics.Bitmap; +import java.io.InputStream; +import java.nio.ByteBuffer; -import com.android.gallery3d.filtershow.filters.ImageFilterFx; +class ByteBufferInputStream extends InputStream { -public class ImagePresetFX extends ImagePreset { - String name; - Bitmap fxBitmap; + private ByteBuffer mBuf; - @Override - public String name() { - return name; + public ByteBufferInputStream(ByteBuffer buf) { + mBuf = buf; } - public ImagePresetFX(Bitmap bitmap, String name) { - fxBitmap = bitmap; - this.name = name; - setup(); + @Override + public int read() { + if (!mBuf.hasRemaining()) { + return -1; + } + return mBuf.get() & 0xFF; } @Override - public void setup() { - if (fxBitmap != null) { - mFilters.add(new ImageFilterFx(fxBitmap,name)); + public int read(byte[] bytes, int off, int len) { + if (!mBuf.hasRemaining()) { + return -1; } - } + len = Math.min(len, mBuf.remaining()); + mBuf.get(bytes, off, len); + return len; + } } diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifData.java b/gallerycommon/src/com/android/gallery3d/exif/ExifData.java index 39eb57455c00ff136802f8dcdaf5e99c893496e5..8422382bbdba13620576eeb18f1d6c8f4e544a8b 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/ExifData.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifData.java @@ -16,65 +16,70 @@ package com.android.gallery3d.exif; +import android.util.Log; + +import java.io.UnsupportedEncodingException; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** - * This class stores the EXIF header in IFDs according to the JPEG specification. - * It is the result produced by {@link ExifReader}. - * @see ExifReader - * @see IfdData + * This class stores the EXIF header in IFDs according to the JPEG + * specification. It is the result produced by {@link ExifReader}. + * + * @see ExifReader + * @see IfdData */ -public class ExifData { +class ExifData { + private static final String TAG = "ExifData"; + private static final byte[] USER_COMMENT_ASCII = { + 0x41, 0x53, 0x43, 0x49, 0x49, 0x00, 0x00, 0x00 + }; + private static final byte[] USER_COMMENT_JIS = { + 0x4A, 0x49, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + private static final byte[] USER_COMMENT_UNICODE = { + 0x55, 0x4E, 0x49, 0x43, 0x4F, 0x44, 0x45, 0x00 + }; + private final IfdData[] mIfdDatas = new IfdData[IfdId.TYPE_IFD_COUNT]; private byte[] mThumbnail; private ArrayList mStripBytes = new ArrayList(); private final ByteOrder mByteOrder; - public ExifData(ByteOrder order) { + ExifData(ByteOrder order) { mByteOrder = order; } - IfdData getIfdData(int ifdId) { - return mIfdDatas[ifdId]; - } - /** - * Adds IFD data. If IFD data of the same type already exists, - * it will be replaced by the new data. - */ - void addIfdData(IfdData data) { - mIfdDatas[data.getId()] = data; - } - - /** - * Gets the compressed thumbnail. Returns null if there is no compressed thumbnail. + * Gets the compressed thumbnail. Returns null if there is no compressed + * thumbnail. * * @see #hasCompressedThumbnail() */ - public byte[] getCompressedThumbnail() { + protected byte[] getCompressedThumbnail() { return mThumbnail; } /** * Sets the compressed thumbnail. */ - public void setCompressedThumbnail(byte[] thumbnail) { + protected void setCompressedThumbnail(byte[] thumbnail) { mThumbnail = thumbnail; } /** * Returns true it this header contains a compressed thumbnail. */ - public boolean hasCompressedThumbnail() { + protected boolean hasCompressedThumbnail() { return mThumbnail != null; } /** * Adds an uncompressed strip. */ - public void setStripBytes(int index, byte[] strip) { + protected void setStripBytes(int index, byte[] strip) { if (index < mStripBytes.size()) { mStripBytes.set(index, strip); } else { @@ -88,98 +93,57 @@ public class ExifData { /** * Gets the strip count. */ - public int getStripCount() { + protected int getStripCount() { return mStripBytes.size(); } /** * Gets the strip at the specified index. + * * @exceptions #IndexOutOfBoundException */ - public byte[] getStrip(int index) { + protected byte[] getStrip(int index) { return mStripBytes.get(index); } /** - * Gets the byte order. + * Returns true if this header contains uncompressed strip. */ - public ByteOrder getByteOrder() { - return mByteOrder; + protected boolean hasUncompressedStrip() { + return mStripBytes.size() != 0; } /** - * Returns true if this header contains uncompressed strip of thumbnail. + * Gets the byte order. */ - public boolean hasUncompressedStrip() { - return mStripBytes.size() != 0; - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof ExifData) { - ExifData data = (ExifData) obj; - if (data.mByteOrder != mByteOrder - || !Arrays.equals(data.mThumbnail, mThumbnail) - || data.mStripBytes.size() != mStripBytes.size()) return false; - - for (int i = 0; i < mStripBytes.size(); i++) { - if (!Arrays.equals(data.mStripBytes.get(i), mStripBytes.get(i))) return false; - } - - for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) { - if (!Util.equals(data.getIfdData(i), getIfdData(i))) return false; - } - return true; - } - return false; + protected ByteOrder getByteOrder() { + return mByteOrder; } /** - * Adds {@link ExifTag#TAG_GPS_LATITUDE}, {@link ExifTag#TAG_GPS_LONGITUDE}, - * {@link ExifTag#TAG_GPS_LATITUDE_REF} and {@link ExifTag#TAG_GPS_LONGITUDE_REF} with the - * given latitude and longitude. + * Returns the {@link IfdData} object corresponding to a given IFD if it + * exists or null. */ - public void addGpsTags(double latitude, double longitude) { - IfdData gpsIfd = getIfdData(IfdId.TYPE_IFD_GPS); - if (gpsIfd == null) { - gpsIfd = new IfdData(IfdId.TYPE_IFD_GPS); - addIfdData(gpsIfd); + protected IfdData getIfdData(int ifdId) { + if (ExifTag.isValidIfd(ifdId)) { + return mIfdDatas[ifdId]; } - ExifTag latTag = new ExifTag(ExifTag.TAG_GPS_LATITUDE, ExifTag.TYPE_RATIONAL, - 3, IfdId.TYPE_IFD_GPS); - ExifTag longTag = new ExifTag(ExifTag.TAG_GPS_LONGITUDE, ExifTag.TYPE_RATIONAL, - 3, IfdId.TYPE_IFD_GPS); - ExifTag latRefTag = new ExifTag(ExifTag.TAG_GPS_LATITUDE_REF, - ExifTag.TYPE_ASCII, 2, IfdId.TYPE_IFD_GPS); - ExifTag longRefTag = new ExifTag(ExifTag.TAG_GPS_LONGITUDE_REF, - ExifTag.TYPE_ASCII, 2, IfdId.TYPE_IFD_GPS); - latTag.setValue(toExifLatLong(latitude)); - longTag.setValue(toExifLatLong(longitude)); - latRefTag.setValue(latitude >= 0 - ? ExifTag.GpsLatitudeRef.NORTH - : ExifTag.GpsLatitudeRef.SOUTH); - longRefTag.setValue(longitude >= 0 - ? ExifTag.GpsLongitudeRef.EAST - : ExifTag.GpsLongitudeRef.WEST); - gpsIfd.setTag(latTag); - gpsIfd.setTag(longTag); - gpsIfd.setTag(latRefTag); - gpsIfd.setTag(longRefTag); + return null; } - private static Rational[] toExifLatLong(double value) { - // convert to the format dd/1 mm/1 ssss/100 - value = Math.abs(value); - int degrees = (int) value; - value = (value - degrees) * 60; - int minutes = (int) value; - value = (value - minutes) * 6000; - int seconds = (int) value; - return new Rational[] { - new Rational(degrees, 1), new Rational(minutes, 1), new Rational(seconds, 100)}; + /** + * Adds IFD data. If IFD data of the same type already exists, it will be + * replaced by the new data. + */ + protected void addIfdData(IfdData data) { + mIfdDatas[data.getId()] = data; } - private IfdData getOrCreateIfdData(int ifdId) { + /** + * Returns the {@link IfdData} object corresponding to a given IFD or + * generates one if none exist. + */ + protected IfdData getOrCreateIfdData(int ifdId) { IfdData ifdData = mIfdDatas[ifdId]; if (ifdData == null) { ifdData = new IfdData(ifdId); @@ -189,73 +153,196 @@ public class ExifData { } /** - * Gets the tag with the given tag ID. Returns null if the tag does not exist. For tags - * related to interoperability or thumbnail, call {@link #getInteroperabilityTag(short)} and - * {@link #getThumbnailTag(short)} respectively. + * Returns the tag with a given TID in the given IFD if the tag exists. + * Otherwise returns null. */ - public ExifTag getTag(short tagId) { - int ifdId = ExifTag.getIfdIdFromTagId(tagId); - IfdData ifdData = mIfdDatas[ifdId]; - return (ifdData == null) ? null : ifdData.getTag(tagId); + protected ExifTag getTag(short tag, int ifd) { + IfdData ifdData = mIfdDatas[ifd]; + return (ifdData == null) ? null : ifdData.getTag(tag); + } + + /** + * Adds the given ExifTag to its default IFD and returns an existing ExifTag + * with the same TID or null if none exist. + */ + protected ExifTag addTag(ExifTag tag) { + if (tag != null) { + int ifd = tag.getIfd(); + return addTag(tag, ifd); + } + return null; + } + + /** + * Adds the given ExifTag to the given IFD and returns an existing ExifTag + * with the same TID or null if none exist. + */ + protected ExifTag addTag(ExifTag tag, int ifdId) { + if (tag != null && ExifTag.isValidIfd(ifdId)) { + IfdData ifdData = getOrCreateIfdData(ifdId); + return ifdData.setTag(tag); + } + return null; + } + + protected void clearThumbnailAndStrips() { + mThumbnail = null; + mStripBytes.clear(); } /** - * Gets the thumbnail-related tag with the given tag ID. + * Removes the thumbnail and its related tags. IFD1 will be removed. */ - public ExifTag getThumbnailTag(short tagId) { - IfdData ifdData = mIfdDatas[IfdId.TYPE_IFD_1]; - return (ifdData == null) ? null : ifdData.getTag(tagId); + protected void removeThumbnailData() { + clearThumbnailAndStrips(); + mIfdDatas[IfdId.TYPE_IFD_1] = null; } /** - * Gets the interoperability-related tag with the given tag ID. + * Removes the tag with a given TID and IFD. */ - public ExifTag getInteroperabilityTag(short tagId) { - IfdData ifdData = mIfdDatas[IfdId.TYPE_IFD_INTEROPERABILITY]; - return (ifdData == null) ? null : ifdData.getTag(tagId); + protected void removeTag(short tagId, int ifdId) { + IfdData ifdData = mIfdDatas[ifdId]; + if (ifdData == null) { + return; + } + ifdData.removeTag(tagId); } /** - * Adds a tag with the given tag ID. The original tag will be replaced by the new tag. For tags - * related to interoperability or thumbnail, call {@link #addInteroperabilityTag(short)} or - * {@link #addThumbnailTag(short)} respectively. - * @exception IllegalArgumentException if the tag ID is invalid. + * Decodes the user comment tag into string as specified in the EXIF + * standard. Returns null if decoding failed. */ - public ExifTag addTag(short tagId) { - int ifdId = ExifTag.getIfdIdFromTagId(tagId); - IfdData ifdData = getOrCreateIfdData(ifdId); - ExifTag tag = ExifTag.buildTag(tagId); - ifdData.setTag(tag); - return tag; + protected String getUserComment() { + IfdData ifdData = mIfdDatas[IfdId.TYPE_IFD_0]; + if (ifdData == null) { + return null; + } + ExifTag tag = ifdData.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_USER_COMMENT)); + if (tag == null) { + return null; + } + if (tag.getComponentCount() < 8) { + return null; + } + + byte[] buf = new byte[tag.getComponentCount()]; + tag.getBytes(buf); + + byte[] code = new byte[8]; + System.arraycopy(buf, 0, code, 0, 8); + + try { + if (Arrays.equals(code, USER_COMMENT_ASCII)) { + return new String(buf, 8, buf.length - 8, "US-ASCII"); + } else if (Arrays.equals(code, USER_COMMENT_JIS)) { + return new String(buf, 8, buf.length - 8, "EUC-JP"); + } else if (Arrays.equals(code, USER_COMMENT_UNICODE)) { + return new String(buf, 8, buf.length - 8, "UTF-16"); + } else { + return null; + } + } catch (UnsupportedEncodingException e) { + Log.w(TAG, "Failed to decode the user comment"); + return null; + } } /** - * Adds a thumbnail-related tag with the given tag ID. The original tag will be replaced - * by the new tag. - * @exception IllegalArgumentException if the tag ID is invalid. + * Returns a list of all {@link ExifTag}s in the ExifData or null if there + * are none. */ - public ExifTag addThumbnailTag(short tagId) { - IfdData ifdData = getOrCreateIfdData(IfdId.TYPE_IFD_1); - ExifTag tag = ExifTag.buildThumbnailTag(tagId); - ifdData.setTag(tag); - return tag; + protected List getAllTags() { + ArrayList ret = new ArrayList(); + for (IfdData d : mIfdDatas) { + if (d != null) { + ExifTag[] tags = d.getAllTags(); + if (tags != null) { + for (ExifTag t : tags) { + ret.add(t); + } + } + } + } + if (ret.size() == 0) { + return null; + } + return ret; } /** - * Adds an interoperability-related tag with the given tag ID. The original tag will be - * replaced by the new tag. - * @exception IllegalArgumentException if the tag ID is invalid. + * Returns a list of all {@link ExifTag}s in a given IFD or null if there + * are none. */ - public ExifTag addInteroperabilityTag(short tagId) { - IfdData ifdData = getOrCreateIfdData(IfdId.TYPE_IFD_INTEROPERABILITY); - ExifTag tag = ExifTag.buildInteroperabilityTag(tagId); - ifdData.setTag(tag); - return tag; + protected List getAllTagsForIfd(int ifd) { + IfdData d = mIfdDatas[ifd]; + if (d == null) { + return null; + } + ExifTag[] tags = d.getAllTags(); + if (tags == null) { + return null; + } + ArrayList ret = new ArrayList(tags.length); + for (ExifTag t : tags) { + ret.add(t); + } + if (ret.size() == 0) { + return null; + } + return ret; } - public void removeThumbnailData() { - mThumbnail = null; - mStripBytes.clear(); - mIfdDatas[IfdId.TYPE_IFD_1] = null; + /** + * Returns a list of all {@link ExifTag}s with a given TID or null if there + * are none. + */ + protected List getAllTagsForTagId(short tag) { + ArrayList ret = new ArrayList(); + for (IfdData d : mIfdDatas) { + if (d != null) { + ExifTag t = d.getTag(tag); + if (t != null) { + ret.add(t); + } + } + } + if (ret.size() == 0) { + return null; + } + return ret; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (obj instanceof ExifData) { + ExifData data = (ExifData) obj; + if (data.mByteOrder != mByteOrder || + data.mStripBytes.size() != mStripBytes.size() || + !Arrays.equals(data.mThumbnail, mThumbnail)) { + return false; + } + for (int i = 0; i < mStripBytes.size(); i++) { + if (!Arrays.equals(data.mStripBytes.get(i), mStripBytes.get(i))) { + return false; + } + } + for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) { + IfdData ifd1 = data.getIfdData(i); + IfdData ifd2 = getIfdData(i); + if (ifd1 != ifd2 && ifd1 != null && !ifd1.equals(ifd2)) { + return false; + } + } + return true; + } + return false; } -} \ No newline at end of file + +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifInterface.java b/gallerycommon/src/com/android/gallery3d/exif/ExifInterface.java new file mode 100644 index 0000000000000000000000000000000000000000..a1cf0fc85299cbfb0d32a6c8f448450c0a9114aa --- /dev/null +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifInterface.java @@ -0,0 +1,2407 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.exif; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.SparseIntArray; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel.MapMode; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.TimeZone; + +/** + * This class provides methods and constants for reading and writing jpeg file + * metadata. It contains a collection of ExifTags, and a collection of + * definitions for creating valid ExifTags. The collection of ExifTags can be + * updated by: reading new ones from a file, deleting or adding existing ones, + * or building new ExifTags from a tag definition. These ExifTags can be written + * to a valid jpeg image as exif metadata. + *

+ * Each ExifTag has a tag ID (TID) and is stored in a specific image file + * directory (IFD) as specified by the exif standard. A tag definition can be + * looked up with a constant that is a combination of TID and IFD. This + * definition has information about the type, number of components, and valid + * IFDs for a tag. + * + * @see ExifTag + */ +public class ExifInterface { + public static final int TAG_NULL = -1; + public static final int IFD_NULL = -1; + public static final int DEFINITION_NULL = 0; + + /** + * Tag constants for Jeita EXIF 2.2 + */ + + // IFD 0 + public static final int TAG_IMAGE_WIDTH = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0100); + public static final int TAG_IMAGE_LENGTH = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0101); // Image height + public static final int TAG_BITS_PER_SAMPLE = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0102); + public static final int TAG_COMPRESSION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0103); + public static final int TAG_PHOTOMETRIC_INTERPRETATION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0106); + public static final int TAG_IMAGE_DESCRIPTION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x010E); + public static final int TAG_MAKE = + defineTag(IfdId.TYPE_IFD_0, (short) 0x010F); + public static final int TAG_MODEL = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0110); + public static final int TAG_STRIP_OFFSETS = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0111); + public static final int TAG_ORIENTATION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0112); + public static final int TAG_SAMPLES_PER_PIXEL = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0115); + public static final int TAG_ROWS_PER_STRIP = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0116); + public static final int TAG_STRIP_BYTE_COUNTS = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0117); + public static final int TAG_X_RESOLUTION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x011A); + public static final int TAG_Y_RESOLUTION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x011B); + public static final int TAG_PLANAR_CONFIGURATION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x011C); + public static final int TAG_RESOLUTION_UNIT = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0128); + public static final int TAG_TRANSFER_FUNCTION = + defineTag(IfdId.TYPE_IFD_0, (short) 0x012D); + public static final int TAG_SOFTWARE = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0131); + public static final int TAG_DATE_TIME = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0132); + public static final int TAG_ARTIST = + defineTag(IfdId.TYPE_IFD_0, (short) 0x013B); + public static final int TAG_WHITE_POINT = + defineTag(IfdId.TYPE_IFD_0, (short) 0x013E); + public static final int TAG_PRIMARY_CHROMATICITIES = + defineTag(IfdId.TYPE_IFD_0, (short) 0x013F); + public static final int TAG_Y_CB_CR_COEFFICIENTS = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0211); + public static final int TAG_Y_CB_CR_SUB_SAMPLING = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0212); + public static final int TAG_Y_CB_CR_POSITIONING = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0213); + public static final int TAG_REFERENCE_BLACK_WHITE = + defineTag(IfdId.TYPE_IFD_0, (short) 0x0214); + public static final int TAG_COPYRIGHT = + defineTag(IfdId.TYPE_IFD_0, (short) 0x8298); + public static final int TAG_EXIF_IFD = + defineTag(IfdId.TYPE_IFD_0, (short) 0x8769); + public static final int TAG_GPS_IFD = + defineTag(IfdId.TYPE_IFD_0, (short) 0x8825); + // IFD 1 + public static final int TAG_JPEG_INTERCHANGE_FORMAT = + defineTag(IfdId.TYPE_IFD_1, (short) 0x0201); + public static final int TAG_JPEG_INTERCHANGE_FORMAT_LENGTH = + defineTag(IfdId.TYPE_IFD_1, (short) 0x0202); + // IFD Exif Tags + public static final int TAG_EXPOSURE_TIME = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x829A); + public static final int TAG_F_NUMBER = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x829D); + public static final int TAG_EXPOSURE_PROGRAM = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x8822); + public static final int TAG_SPECTRAL_SENSITIVITY = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x8824); + public static final int TAG_ISO_SPEED_RATINGS = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x8827); + public static final int TAG_OECF = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x8828); + public static final int TAG_EXIF_VERSION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9000); + public static final int TAG_DATE_TIME_ORIGINAL = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9003); + public static final int TAG_DATE_TIME_DIGITIZED = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9004); + public static final int TAG_COMPONENTS_CONFIGURATION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9101); + public static final int TAG_COMPRESSED_BITS_PER_PIXEL = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9102); + public static final int TAG_SHUTTER_SPEED_VALUE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9201); + public static final int TAG_APERTURE_VALUE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9202); + public static final int TAG_BRIGHTNESS_VALUE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9203); + public static final int TAG_EXPOSURE_BIAS_VALUE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9204); + public static final int TAG_MAX_APERTURE_VALUE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9205); + public static final int TAG_SUBJECT_DISTANCE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9206); + public static final int TAG_METERING_MODE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9207); + public static final int TAG_LIGHT_SOURCE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9208); + public static final int TAG_FLASH = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9209); + public static final int TAG_FOCAL_LENGTH = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x920A); + public static final int TAG_SUBJECT_AREA = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9214); + public static final int TAG_MAKER_NOTE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x927C); + public static final int TAG_USER_COMMENT = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9286); + public static final int TAG_SUB_SEC_TIME = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9290); + public static final int TAG_SUB_SEC_TIME_ORIGINAL = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9291); + public static final int TAG_SUB_SEC_TIME_DIGITIZED = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0x9292); + public static final int TAG_FLASHPIX_VERSION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA000); + public static final int TAG_COLOR_SPACE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA001); + public static final int TAG_PIXEL_X_DIMENSION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA002); + public static final int TAG_PIXEL_Y_DIMENSION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA003); + public static final int TAG_RELATED_SOUND_FILE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA004); + public static final int TAG_INTEROPERABILITY_IFD = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA005); + public static final int TAG_FLASH_ENERGY = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA20B); + public static final int TAG_SPATIAL_FREQUENCY_RESPONSE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA20C); + public static final int TAG_FOCAL_PLANE_X_RESOLUTION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA20E); + public static final int TAG_FOCAL_PLANE_Y_RESOLUTION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA20F); + public static final int TAG_FOCAL_PLANE_RESOLUTION_UNIT = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA210); + public static final int TAG_SUBJECT_LOCATION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA214); + public static final int TAG_EXPOSURE_INDEX = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA215); + public static final int TAG_SENSING_METHOD = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA217); + public static final int TAG_FILE_SOURCE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA300); + public static final int TAG_SCENE_TYPE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA301); + public static final int TAG_CFA_PATTERN = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA302); + public static final int TAG_CUSTOM_RENDERED = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA401); + public static final int TAG_EXPOSURE_MODE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA402); + public static final int TAG_WHITE_BALANCE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA403); + public static final int TAG_DIGITAL_ZOOM_RATIO = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA404); + public static final int TAG_FOCAL_LENGTH_IN_35_MM_FILE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA405); + public static final int TAG_SCENE_CAPTURE_TYPE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA406); + public static final int TAG_GAIN_CONTROL = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA407); + public static final int TAG_CONTRAST = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA408); + public static final int TAG_SATURATION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA409); + public static final int TAG_SHARPNESS = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA40A); + public static final int TAG_DEVICE_SETTING_DESCRIPTION = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA40B); + public static final int TAG_SUBJECT_DISTANCE_RANGE = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA40C); + public static final int TAG_IMAGE_UNIQUE_ID = + defineTag(IfdId.TYPE_IFD_EXIF, (short) 0xA420); + // IFD GPS tags + public static final int TAG_GPS_VERSION_ID = + defineTag(IfdId.TYPE_IFD_GPS, (short) 0); + public static final int TAG_GPS_LATITUDE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 1); + public static final int TAG_GPS_LATITUDE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 2); + public static final int TAG_GPS_LONGITUDE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 3); + public static final int TAG_GPS_LONGITUDE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 4); + public static final int TAG_GPS_ALTITUDE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 5); + public static final int TAG_GPS_ALTITUDE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 6); + public static final int TAG_GPS_TIME_STAMP = + defineTag(IfdId.TYPE_IFD_GPS, (short) 7); + public static final int TAG_GPS_SATTELLITES = + defineTag(IfdId.TYPE_IFD_GPS, (short) 8); + public static final int TAG_GPS_STATUS = + defineTag(IfdId.TYPE_IFD_GPS, (short) 9); + public static final int TAG_GPS_MEASURE_MODE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 10); + public static final int TAG_GPS_DOP = + defineTag(IfdId.TYPE_IFD_GPS, (short) 11); + public static final int TAG_GPS_SPEED_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 12); + public static final int TAG_GPS_SPEED = + defineTag(IfdId.TYPE_IFD_GPS, (short) 13); + public static final int TAG_GPS_TRACK_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 14); + public static final int TAG_GPS_TRACK = + defineTag(IfdId.TYPE_IFD_GPS, (short) 15); + public static final int TAG_GPS_IMG_DIRECTION_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 16); + public static final int TAG_GPS_IMG_DIRECTION = + defineTag(IfdId.TYPE_IFD_GPS, (short) 17); + public static final int TAG_GPS_MAP_DATUM = + defineTag(IfdId.TYPE_IFD_GPS, (short) 18); + public static final int TAG_GPS_DEST_LATITUDE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 19); + public static final int TAG_GPS_DEST_LATITUDE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 20); + public static final int TAG_GPS_DEST_LONGITUDE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 21); + public static final int TAG_GPS_DEST_LONGITUDE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 22); + public static final int TAG_GPS_DEST_BEARING_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 23); + public static final int TAG_GPS_DEST_BEARING = + defineTag(IfdId.TYPE_IFD_GPS, (short) 24); + public static final int TAG_GPS_DEST_DISTANCE_REF = + defineTag(IfdId.TYPE_IFD_GPS, (short) 25); + public static final int TAG_GPS_DEST_DISTANCE = + defineTag(IfdId.TYPE_IFD_GPS, (short) 26); + public static final int TAG_GPS_PROCESSING_METHOD = + defineTag(IfdId.TYPE_IFD_GPS, (short) 27); + public static final int TAG_GPS_AREA_INFORMATION = + defineTag(IfdId.TYPE_IFD_GPS, (short) 28); + public static final int TAG_GPS_DATE_STAMP = + defineTag(IfdId.TYPE_IFD_GPS, (short) 29); + public static final int TAG_GPS_DIFFERENTIAL = + defineTag(IfdId.TYPE_IFD_GPS, (short) 30); + // IFD Interoperability tags + public static final int TAG_INTEROPERABILITY_INDEX = + defineTag(IfdId.TYPE_IFD_INTEROPERABILITY, (short) 1); + + /** + * Tags that contain offset markers. These are included in the banned + * defines. + */ + private static HashSet sOffsetTags = new HashSet(); + static { + sOffsetTags.add(getTrueTagKey(TAG_GPS_IFD)); + sOffsetTags.add(getTrueTagKey(TAG_EXIF_IFD)); + sOffsetTags.add(getTrueTagKey(TAG_JPEG_INTERCHANGE_FORMAT)); + sOffsetTags.add(getTrueTagKey(TAG_INTEROPERABILITY_IFD)); + sOffsetTags.add(getTrueTagKey(TAG_STRIP_OFFSETS)); + } + + /** + * Tags with definitions that cannot be overridden (banned defines). + */ + protected static HashSet sBannedDefines = new HashSet(sOffsetTags); + static { + sBannedDefines.add(getTrueTagKey(TAG_NULL)); + sBannedDefines.add(getTrueTagKey(TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)); + sBannedDefines.add(getTrueTagKey(TAG_STRIP_BYTE_COUNTS)); + } + + /** + * Returns the constant representing a tag with a given TID and default IFD. + */ + public static int defineTag(int ifdId, short tagId) { + return (tagId & 0x0000ffff) | (ifdId << 16); + } + + /** + * Returns the TID for a tag constant. + */ + public static short getTrueTagKey(int tag) { + // Truncate + return (short) tag; + } + + /** + * Returns the default IFD for a tag constant. + */ + public static int getTrueIfd(int tag) { + return tag >>> 16; + } + + /** + * Constants for {@link TAG_ORIENTATION}. They can be interpreted as + * follows: + *

    + *
  • TOP_LEFT is the normal orientation.
  • + *
  • TOP_RIGHT is a left-right mirror.
  • + *
  • BOTTOM_LEFT is a 180 degree rotation.
  • + *
  • BOTTOM_RIGHT is a top-bottom mirror.
  • + *
  • LEFT_TOP is mirrored about the top-left<->bottom-right axis.
  • + *
  • RIGHT_TOP is a 90 degree clockwise rotation.
  • + *
  • LEFT_BOTTOM is mirrored about the top-right<->bottom-left axis.
  • + *
  • RIGHT_BOTTOM is a 270 degree clockwise rotation.
  • + *
+ */ + public static interface Orientation { + public static final short TOP_LEFT = 1; + public static final short TOP_RIGHT = 2; + public static final short BOTTOM_LEFT = 3; + public static final short BOTTOM_RIGHT = 4; + public static final short LEFT_TOP = 5; + public static final short RIGHT_TOP = 6; + public static final short LEFT_BOTTOM = 7; + public static final short RIGHT_BOTTOM = 8; + } + + /** + * Constants for {@link TAG_Y_CB_CR_POSITIONING} + */ + public static interface YCbCrPositioning { + public static final short CENTERED = 1; + public static final short CO_SITED = 2; + } + + /** + * Constants for {@link TAG_COMPRESSION} + */ + public static interface Compression { + public static final short UNCOMPRESSION = 1; + public static final short JPEG = 6; + } + + /** + * Constants for {@link TAG_RESOLUTION_UNIT} + */ + public static interface ResolutionUnit { + public static final short INCHES = 2; + public static final short CENTIMETERS = 3; + } + + /** + * Constants for {@link TAG_PHOTOMETRIC_INTERPRETATION} + */ + public static interface PhotometricInterpretation { + public static final short RGB = 2; + public static final short YCBCR = 6; + } + + /** + * Constants for {@link TAG_PLANAR_CONFIGURATION} + */ + public static interface PlanarConfiguration { + public static final short CHUNKY = 1; + public static final short PLANAR = 2; + } + + /** + * Constants for {@link TAG_EXPOSURE_PROGRAM} + */ + public static interface ExposureProgram { + public static final short NOT_DEFINED = 0; + public static final short MANUAL = 1; + public static final short NORMAL_PROGRAM = 2; + public static final short APERTURE_PRIORITY = 3; + public static final short SHUTTER_PRIORITY = 4; + public static final short CREATIVE_PROGRAM = 5; + public static final short ACTION_PROGRAM = 6; + public static final short PROTRAIT_MODE = 7; + public static final short LANDSCAPE_MODE = 8; + } + + /** + * Constants for {@link TAG_METERING_MODE} + */ + public static interface MeteringMode { + public static final short UNKNOWN = 0; + public static final short AVERAGE = 1; + public static final short CENTER_WEIGHTED_AVERAGE = 2; + public static final short SPOT = 3; + public static final short MULTISPOT = 4; + public static final short PATTERN = 5; + public static final short PARTAIL = 6; + public static final short OTHER = 255; + } + + /** + * Constants for {@link TAG_FLASH} As the definition in Jeita EXIF 2.2 + * standard, we can treat this constant as bitwise flag. + *

+ * e.g. + *

+ * short flash = FIRED | RETURN_STROBE_RETURN_LIGHT_DETECTED | + * MODE_AUTO_MODE + */ + public static interface Flash { + // LSB + public static final short DID_NOT_FIRED = 0; + public static final short FIRED = 1; + // 1st~2nd bits + public static final short RETURN_NO_STROBE_RETURN_DETECTION_FUNCTION = 0 << 1; + public static final short RETURN_STROBE_RETURN_LIGHT_NOT_DETECTED = 2 << 1; + public static final short RETURN_STROBE_RETURN_LIGHT_DETECTED = 3 << 1; + // 3rd~4th bits + public static final short MODE_UNKNOWN = 0 << 3; + public static final short MODE_COMPULSORY_FLASH_FIRING = 1 << 3; + public static final short MODE_COMPULSORY_FLASH_SUPPRESSION = 2 << 3; + public static final short MODE_AUTO_MODE = 3 << 3; + // 5th bit + public static final short FUNCTION_PRESENT = 0 << 5; + public static final short FUNCTION_NO_FUNCTION = 1 << 5; + // 6th bit + public static final short RED_EYE_REDUCTION_NO_OR_UNKNOWN = 0 << 6; + public static final short RED_EYE_REDUCTION_SUPPORT = 1 << 6; + } + + /** + * Constants for {@link TAG_COLOR_SPACE} + */ + public static interface ColorSpace { + public static final short SRGB = 1; + public static final short UNCALIBRATED = (short) 0xFFFF; + } + + /** + * Constants for {@link TAG_EXPOSURE_MODE} + */ + public static interface ExposureMode { + public static final short AUTO_EXPOSURE = 0; + public static final short MANUAL_EXPOSURE = 1; + public static final short AUTO_BRACKET = 2; + } + + /** + * Constants for {@link TAG_WHITE_BALANCE} + */ + public static interface WhiteBalance { + public static final short AUTO = 0; + public static final short MANUAL = 1; + } + + /** + * Constants for {@link TAG_SCENE_CAPTURE_TYPE} + */ + public static interface SceneCapture { + public static final short STANDARD = 0; + public static final short LANDSCAPE = 1; + public static final short PROTRAIT = 2; + public static final short NIGHT_SCENE = 3; + } + + /** + * Constants for {@link TAG_COMPONENTS_CONFIGURATION} + */ + public static interface ComponentsConfiguration { + public static final short NOT_EXIST = 0; + public static final short Y = 1; + public static final short CB = 2; + public static final short CR = 3; + public static final short R = 4; + public static final short G = 5; + public static final short B = 6; + } + + /** + * Constants for {@link TAG_LIGHT_SOURCE} + */ + public static interface LightSource { + public static final short UNKNOWN = 0; + public static final short DAYLIGHT = 1; + public static final short FLUORESCENT = 2; + public static final short TUNGSTEN = 3; + public static final short FLASH = 4; + public static final short FINE_WEATHER = 9; + public static final short CLOUDY_WEATHER = 10; + public static final short SHADE = 11; + public static final short DAYLIGHT_FLUORESCENT = 12; + public static final short DAY_WHITE_FLUORESCENT = 13; + public static final short COOL_WHITE_FLUORESCENT = 14; + public static final short WHITE_FLUORESCENT = 15; + public static final short STANDARD_LIGHT_A = 17; + public static final short STANDARD_LIGHT_B = 18; + public static final short STANDARD_LIGHT_C = 19; + public static final short D55 = 20; + public static final short D65 = 21; + public static final short D75 = 22; + public static final short D50 = 23; + public static final short ISO_STUDIO_TUNGSTEN = 24; + public static final short OTHER = 255; + } + + /** + * Constants for {@link TAG_SENSING_METHOD} + */ + public static interface SensingMethod { + public static final short NOT_DEFINED = 1; + public static final short ONE_CHIP_COLOR = 2; + public static final short TWO_CHIP_COLOR = 3; + public static final short THREE_CHIP_COLOR = 4; + public static final short COLOR_SEQUENTIAL_AREA = 5; + public static final short TRILINEAR = 7; + public static final short COLOR_SEQUENTIAL_LINEAR = 8; + } + + /** + * Constants for {@link TAG_FILE_SOURCE} + */ + public static interface FileSource { + public static final short DSC = 3; + } + + /** + * Constants for {@link TAG_SCENE_TYPE} + */ + public static interface SceneType { + public static final short DIRECT_PHOTOGRAPHED = 1; + } + + /** + * Constants for {@link TAG_GAIN_CONTROL} + */ + public static interface GainControl { + public static final short NONE = 0; + public static final short LOW_UP = 1; + public static final short HIGH_UP = 2; + public static final short LOW_DOWN = 3; + public static final short HIGH_DOWN = 4; + } + + /** + * Constants for {@link TAG_CONTRAST} + */ + public static interface Contrast { + public static final short NORMAL = 0; + public static final short SOFT = 1; + public static final short HARD = 2; + } + + /** + * Constants for {@link TAG_SATURATION} + */ + public static interface Saturation { + public static final short NORMAL = 0; + public static final short LOW = 1; + public static final short HIGH = 2; + } + + /** + * Constants for {@link TAG_SHARPNESS} + */ + public static interface Sharpness { + public static final short NORMAL = 0; + public static final short SOFT = 1; + public static final short HARD = 2; + } + + /** + * Constants for {@link TAG_SUBJECT_DISTANCE} + */ + public static interface SubjectDistance { + public static final short UNKNOWN = 0; + public static final short MACRO = 1; + public static final short CLOSE_VIEW = 2; + public static final short DISTANT_VIEW = 3; + } + + /** + * Constants for {@link TAG_GPS_LATITUDE_REF}, + * {@link TAG_GPS_DEST_LATITUDE_REF} + */ + public static interface GpsLatitudeRef { + public static final String NORTH = "N"; + public static final String SOUTH = "S"; + } + + /** + * Constants for {@link TAG_GPS_LONGITUDE_REF}, + * {@link TAG_GPS_DEST_LONGITUDE_REF} + */ + public static interface GpsLongitudeRef { + public static final String EAST = "E"; + public static final String WEST = "W"; + } + + /** + * Constants for {@link TAG_GPS_ALTITUDE_REF} + */ + public static interface GpsAltitudeRef { + public static final short SEA_LEVEL = 0; + public static final short SEA_LEVEL_NEGATIVE = 1; + } + + /** + * Constants for {@link TAG_GPS_STATUS} + */ + public static interface GpsStatus { + public static final String IN_PROGRESS = "A"; + public static final String INTEROPERABILITY = "V"; + } + + /** + * Constants for {@link TAG_GPS_MEASURE_MODE} + */ + public static interface GpsMeasureMode { + public static final String MODE_2_DIMENSIONAL = "2"; + public static final String MODE_3_DIMENSIONAL = "3"; + } + + /** + * Constants for {@link TAG_GPS_SPEED_REF}, + * {@link TAG_GPS_DEST_DISTANCE_REF} + */ + public static interface GpsSpeedRef { + public static final String KILOMETERS = "K"; + public static final String MILES = "M"; + public static final String KNOTS = "N"; + } + + /** + * Constants for {@link TAG_GPS_TRACK_REF}, + * {@link TAG_GPS_IMG_DIRECTION_REF}, {@link TAG_GPS_DEST_BEARING_REF} + */ + public static interface GpsTrackRef { + public static final String TRUE_DIRECTION = "T"; + public static final String MAGNETIC_DIRECTION = "M"; + } + + /** + * Constants for {@link TAG_GPS_DIFFERENTIAL} + */ + public static interface GpsDifferential { + public static final short WITHOUT_DIFFERENTIAL_CORRECTION = 0; + public static final short DIFFERENTIAL_CORRECTION_APPLIED = 1; + } + + private static final String NULL_ARGUMENT_STRING = "Argument is null"; + private ExifData mData = new ExifData(DEFAULT_BYTE_ORDER); + public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN; + + public ExifInterface() { + mGPSDateStampFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + /** + * Reads the exif tags from a byte array, clearing this ExifInterface + * object's existing exif tags. + * + * @param jpeg a byte array containing a jpeg compressed image. + * @throws IOException + */ + public void readExif(byte[] jpeg) throws IOException { + readExif(new ByteArrayInputStream(jpeg)); + } + + /** + * Reads the exif tags from an InputStream, clearing this ExifInterface + * object's existing exif tags. + * + * @param inStream an InputStream containing a jpeg compressed image. + * @throws IOException + */ + public void readExif(InputStream inStream) throws IOException { + if (inStream == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + ExifData d = null; + try { + d = new ExifReader(this).read(inStream); + } catch (ExifInvalidFormatException e) { + throw new IOException("Invalid exif format : " + e); + } + mData = d; + } + + /** + * Reads the exif tags from a file, clearing this ExifInterface object's + * existing exif tags. + * + * @param inFileName a string representing the filepath to jpeg file. + * @throws FileNotFoundException + * @throws IOException + */ + public void readExif(String inFileName) throws FileNotFoundException, IOException { + if (inFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + InputStream is = null; + try { + is = (InputStream) new BufferedInputStream(new FileInputStream(inFileName)); + readExif(is); + } catch (IOException e) { + closeSilently(is); + throw e; + } + is.close(); + } + + /** + * Sets the exif tags, clearing this ExifInterface object's existing exif + * tags. + * + * @param tags a collection of exif tags to set. + */ + public void setExif(Collection tags) { + clearExif(); + setTags(tags); + } + + /** + * Clears this ExifInterface object's existing exif tags. + */ + public void clearExif() { + mData = new ExifData(DEFAULT_BYTE_ORDER); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg image, + * removing prior exif tags. + * + * @param jpeg a byte array containing a jpeg compressed image. + * @param exifOutStream an OutputStream to which the jpeg image with added + * exif tags will be written. + * @throws IOException + */ + public void writeExif(byte[] jpeg, OutputStream exifOutStream) throws IOException { + if (jpeg == null || exifOutStream == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = getExifWriterStream(exifOutStream); + s.write(jpeg, 0, jpeg.length); + s.flush(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg compressed + * bitmap, removing prior exif tags. + * + * @param bmap a bitmap to compress and write exif into. + * @param exifOutStream the OutputStream to which the jpeg image with added + * exif tags will be written. + * @throws IOException + */ + public void writeExif(Bitmap bmap, OutputStream exifOutStream) throws IOException { + if (bmap == null || exifOutStream == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = getExifWriterStream(exifOutStream); + bmap.compress(Bitmap.CompressFormat.JPEG, 90, s); + s.flush(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg stream, + * removing prior exif tags. + * + * @param jpegStream an InputStream containing a jpeg compressed image. + * @param exifOutStream an OutputStream to which the jpeg image with added + * exif tags will be written. + * @throws IOException + */ + public void writeExif(InputStream jpegStream, OutputStream exifOutStream) throws IOException { + if (jpegStream == null || exifOutStream == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = getExifWriterStream(exifOutStream); + doExifStreamIO(jpegStream, s); + s.flush(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg image, + * removing prior exif tags. + * + * @param jpeg a byte array containing a jpeg compressed image. + * @param exifOutFileName a String containing the filepath to which the jpeg + * image with added exif tags will be written. + * @throws FileNotFoundException + * @throws IOException + */ + public void writeExif(byte[] jpeg, String exifOutFileName) throws FileNotFoundException, + IOException { + if (jpeg == null || exifOutFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = null; + try { + s = getExifWriterStream(exifOutFileName); + s.write(jpeg, 0, jpeg.length); + s.flush(); + } catch (IOException e) { + closeSilently(s); + throw e; + } + s.close(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg compressed + * bitmap, removing prior exif tags. + * + * @param bmap a bitmap to compress and write exif into. + * @param exifOutFileName a String containing the filepath to which the jpeg + * image with added exif tags will be written. + * @throws FileNotFoundException + * @throws IOException + */ + public void writeExif(Bitmap bmap, String exifOutFileName) throws FileNotFoundException, + IOException { + if (bmap == null || exifOutFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = null; + try { + s = getExifWriterStream(exifOutFileName); + bmap.compress(Bitmap.CompressFormat.JPEG, 90, s); + s.flush(); + } catch (IOException e) { + closeSilently(s); + throw e; + } + s.close(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg stream, + * removing prior exif tags. + * + * @param jpegStream an InputStream containing a jpeg compressed image. + * @param exifOutFileName a String containing the filepath to which the jpeg + * image with added exif tags will be written. + * @throws FileNotFoundException + * @throws IOException + */ + public void writeExif(InputStream jpegStream, String exifOutFileName) + throws FileNotFoundException, IOException { + if (jpegStream == null || exifOutFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream s = null; + try { + s = getExifWriterStream(exifOutFileName); + doExifStreamIO(jpegStream, s); + s.flush(); + } catch (IOException e) { + closeSilently(s); + throw e; + } + s.close(); + } + + /** + * Writes the tags from this ExifInterface object into a jpeg file, removing + * prior exif tags. + * + * @param jpegFileName a String containing the filepath for a jpeg file. + * @param exifOutFileName a String containing the filepath to which the jpeg + * image with added exif tags will be written. + * @throws FileNotFoundException + * @throws IOException + */ + public void writeExif(String jpegFileName, String exifOutFileName) + throws FileNotFoundException, IOException { + if (jpegFileName == null || exifOutFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + InputStream is = null; + try { + is = new FileInputStream(jpegFileName); + writeExif(is, exifOutFileName); + } catch (IOException e) { + closeSilently(is); + throw e; + } + is.close(); + } + + /** + * Wraps an OutputStream object with an ExifOutputStream. Exif tags in this + * ExifInterface object will be added to a jpeg image written to this + * stream, removing prior exif tags. Other methods of this ExifInterface + * object should not be called until the returned OutputStream has been + * closed. + * + * @param outStream an OutputStream to wrap. + * @return an OutputStream that wraps the outStream parameter, and adds exif + * metadata. A jpeg image should be written to this stream. + */ + public OutputStream getExifWriterStream(OutputStream outStream) { + if (outStream == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + ExifOutputStream eos = new ExifOutputStream(outStream, this); + eos.setExifData(mData); + return eos; + } + + /** + * Returns an OutputStream object that writes to a file. Exif tags in this + * ExifInterface object will be added to a jpeg image written to this + * stream, removing prior exif tags. Other methods of this ExifInterface + * object should not be called until the returned OutputStream has been + * closed. + * + * @param exifOutFileName an String containing a filepath for a jpeg file. + * @return an OutputStream that writes to the exifOutFileName file, and adds + * exif metadata. A jpeg image should be written to this stream. + * @throws FileNotFoundException + */ + public OutputStream getExifWriterStream(String exifOutFileName) throws FileNotFoundException { + if (exifOutFileName == null) { + throw new IllegalArgumentException(NULL_ARGUMENT_STRING); + } + OutputStream out = null; + try { + out = (OutputStream) new FileOutputStream(exifOutFileName); + } catch (FileNotFoundException e) { + closeSilently(out); + throw e; + } + return getExifWriterStream(out); + } + + /** + * Attempts to do an in-place rewrite the exif metadata in a file for the + * given tags. If tags do not exist or do not have the same size as the + * existing exif tags, this method will fail. + * + * @param filename a String containing a filepath for a jpeg file with exif + * tags to rewrite. + * @param tags tags that will be written into the jpeg file over existing + * tags if possible. + * @return true if success, false if could not overwrite. If false, no + * changes are made to the file. + * @throws FileNotFoundException + * @throws IOException + */ + public boolean rewriteExif(String filename, Collection tags) + throws FileNotFoundException, IOException { + RandomAccessFile file = null; + InputStream is = null; + boolean ret; + try { + File temp = new File(filename); + is = new BufferedInputStream(new FileInputStream(temp)); + + // Parse beginning of APP1 in exif to find size of exif header. + ExifParser parser = null; + try { + parser = ExifParser.parse(is, this); + } catch (ExifInvalidFormatException e) { + throw new IOException("Invalid exif format : ", e); + } + long exifSize = parser.getOffsetToExifEndFromSOF(); + + // Free up resources + is.close(); + is = null; + + // Open file for memory mapping. + file = new RandomAccessFile(temp, "rw"); + long fileLength = file.length(); + if (fileLength < exifSize) { + throw new IOException("Filesize changed during operation"); + } + + // Map only exif header into memory. + ByteBuffer buf = file.getChannel().map(MapMode.READ_WRITE, 0, exifSize); + + // Attempt to overwrite tag values without changing lengths (avoids + // file copy). + ret = rewriteExif(buf, tags); + } catch (IOException e) { + closeSilently(file); + throw e; + } finally { + closeSilently(is); + } + file.close(); + return ret; + } + + /** + * Attempts to do an in-place rewrite the exif metadata in a ByteBuffer for + * the given tags. If tags do not exist or do not have the same size as the + * existing exif tags, this method will fail. + * + * @param buf a ByteBuffer containing a jpeg file with existing exif tags to + * rewrite. + * @param tags tags that will be written into the jpeg ByteBuffer over + * existing tags if possible. + * @return true if success, false if could not overwrite. If false, no + * changes are made to the ByteBuffer. + * @throws IOException + */ + public boolean rewriteExif(ByteBuffer buf, Collection tags) throws IOException { + ExifModifier mod = null; + try { + mod = new ExifModifier(buf, this); + for (ExifTag t : tags) { + mod.modifyTag(t); + } + return mod.commit(); + } catch (ExifInvalidFormatException e) { + throw new IOException("Invalid exif format : " + e); + } + } + + /** + * Attempts to do an in-place rewrite of the exif metadata. If this fails, + * fall back to overwriting file. This preserves tags that are not being + * rewritten. + * + * @param filename a String containing a filepath for a jpeg file. + * @param tags tags that will be written into the jpeg file over existing + * tags if possible. + * @throws FileNotFoundException + * @throws IOException + * @see #rewriteExif + */ + public void forceRewriteExif(String filename, Collection tags) + throws FileNotFoundException, + IOException { + // Attempt in-place write + if (!rewriteExif(filename, tags)) { + // Fall back to doing a copy + ExifData tempData = mData; + mData = new ExifData(DEFAULT_BYTE_ORDER); + FileInputStream is = null; + ByteArrayOutputStream bytes = null; + try { + is = new FileInputStream(filename); + bytes = new ByteArrayOutputStream(); + doExifStreamIO(is, bytes); + byte[] imageBytes = bytes.toByteArray(); + readExif(imageBytes); + setTags(tags); + writeExif(imageBytes, filename); + } catch (IOException e) { + closeSilently(is); + throw e; + } finally { + is.close(); + // Prevent clobbering of mData + mData = tempData; + } + } + } + + /** + * Attempts to do an in-place rewrite of the exif metadata using the tags in + * this ExifInterface object. If this fails, fall back to overwriting file. + * This preserves tags that are not being rewritten. + * + * @param filename a String containing a filepath for a jpeg file. + * @throws FileNotFoundException + * @throws IOException + * @see #rewriteExif + */ + public void forceRewriteExif(String filename) throws FileNotFoundException, IOException { + forceRewriteExif(filename, getAllTags()); + } + + /** + * Get the exif tags in this ExifInterface object or null if none exist. + * + * @return a List of {@link ExifTag}s. + */ + public List getAllTags() { + return mData.getAllTags(); + } + + /** + * Returns a list of ExifTags that share a TID (which can be obtained by + * calling {@link #getTrueTagKey} on a defined tag constant) or null if none + * exist. + * + * @param tagId a TID as defined in the exif standard (or with + * {@link #defineTag}). + * @return a List of {@link ExifTag}s. + */ + public List getTagsForTagId(short tagId) { + return mData.getAllTagsForTagId(tagId); + } + + /** + * Returns a list of ExifTags that share an IFD (which can be obtained by + * calling {@link #getTrueIFD} on a defined tag constant) or null if none + * exist. + * + * @param ifdId an IFD as defined in the exif standard (or with + * {@link #defineTag}). + * @return a List of {@link ExifTag}s. + */ + public List getTagsForIfdId(int ifdId) { + return mData.getAllTagsForIfd(ifdId); + } + + /** + * Gets an ExifTag for an IFD other than the tag's default. + * + * @see #getTag + */ + public ExifTag getTag(int tagId, int ifdId) { + if (!ExifTag.isValidIfd(ifdId)) { + return null; + } + return mData.getTag(getTrueTagKey(tagId), ifdId); + } + + /** + * Returns the ExifTag in that tag's default IFD for a defined tag constant + * or null if none exists. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return an {@link ExifTag} or null if none exists. + */ + public ExifTag getTag(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTag(tagId, ifdId); + } + + /** + * Gets a tag value for an IFD other than the tag's default. + * + * @see #getTagValue + */ + public Object getTagValue(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + return (t == null) ? null : t.getValue(); + } + + /** + * Returns the value of the ExifTag in that tag's default IFD for a defined + * tag constant or null if none exists or the value could not be cast into + * the return type. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return the value of the ExifTag or null if none exists. + */ + public Object getTagValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagValue(tagId, ifdId); + } + + /* + * Getter methods that are similar to getTagValue. Null is returned if the + * tag value cannot be cast into the return type. + */ + + /** + * @see #getTagValue + */ + public String getTagStringValue(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return null; + } + return t.getValueAsString(); + } + + /** + * @see #getTagValue + */ + public String getTagStringValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagStringValue(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public Long getTagLongValue(int tagId, int ifdId) { + long[] l = getTagLongValues(tagId, ifdId); + if (l == null || l.length <= 0) { + return null; + } + return new Long(l[0]); + } + + /** + * @see #getTagValue + */ + public Long getTagLongValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagLongValue(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public Integer getTagIntValue(int tagId, int ifdId) { + int[] l = getTagIntValues(tagId, ifdId); + if (l == null || l.length <= 0) { + return null; + } + return new Integer(l[0]); + } + + /** + * @see #getTagValue + */ + public Integer getTagIntValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagIntValue(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public Byte getTagByteValue(int tagId, int ifdId) { + byte[] l = getTagByteValues(tagId, ifdId); + if (l == null || l.length <= 0) { + return null; + } + return new Byte(l[0]); + } + + /** + * @see #getTagValue + */ + public Byte getTagByteValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagByteValue(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public Rational getTagRationalValue(int tagId, int ifdId) { + Rational[] l = getTagRationalValues(tagId, ifdId); + if (l == null || l.length == 0) { + return null; + } + return new Rational(l[0]); + } + + /** + * @see #getTagValue + */ + public Rational getTagRationalValue(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagRationalValue(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public long[] getTagLongValues(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return null; + } + return t.getValueAsLongs(); + } + + /** + * @see #getTagValue + */ + public long[] getTagLongValues(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagLongValues(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public int[] getTagIntValues(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return null; + } + return t.getValueAsInts(); + } + + /** + * @see #getTagValue + */ + public int[] getTagIntValues(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagIntValues(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public byte[] getTagByteValues(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return null; + } + return t.getValueAsBytes(); + } + + /** + * @see #getTagValue + */ + public byte[] getTagByteValues(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagByteValues(tagId, ifdId); + } + + /** + * @see #getTagValue + */ + public Rational[] getTagRationalValues(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return null; + } + return t.getValueAsRationals(); + } + + /** + * @see #getTagValue + */ + public Rational[] getTagRationalValues(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return getTagRationalValues(tagId, ifdId); + } + + /** + * Checks whether a tag has a defined number of elements. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return true if the tag has a defined number of elements. + */ + public boolean isTagCountDefined(int tagId) { + int info = getTagInfo().get(tagId); + // No value in info can be zero, as all tags have a non-zero type + if (info == 0) { + return false; + } + return getComponentCountFromInfo(info) != ExifTag.SIZE_UNDEFINED; + } + + /** + * Gets the defined number of elements for a tag. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return the number of elements or {@link ExifTag#SIZE_UNDEFINED} if the + * tag or the number of elements is not defined. + */ + public int getDefinedTagCount(int tagId) { + int info = getTagInfo().get(tagId); + if (info == 0) { + return ExifTag.SIZE_UNDEFINED; + } + return getComponentCountFromInfo(info); + } + + /** + * Gets the number of elements for an ExifTag in a given IFD. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param ifdId the IFD containing the ExifTag to check. + * @return the number of elements in the ExifTag, if the tag's size is + * undefined this will return the actual number of elements that is + * in the ExifTag's value. + */ + public int getActualTagCount(int tagId, int ifdId) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return 0; + } + return t.getComponentCount(); + } + + /** + * Gets the default IFD for a tag. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return the default IFD for a tag definition or {@link #IFD_NULL} if no + * definition exists. + */ + public int getDefinedTagDefaultIfd(int tagId) { + int info = getTagInfo().get(tagId); + if (info == DEFINITION_NULL) { + return IFD_NULL; + } + return getTrueIfd(tagId); + } + + /** + * Gets the defined type for a tag. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @return the type. + * @see ExifTag#getDataType() + */ + public short getDefinedTagType(int tagId) { + int info = getTagInfo().get(tagId); + if (info == 0) { + return -1; + } + return getTypeFromInfo(info); + } + + /** + * Returns true if tag TID is one of the following: {@link TAG_EXIF_IFD}, + * {@link TAG_GPS_IFD}, {@link TAG_JPEG_INTERCHANGE_FORMAT}, + * {@link TAG_STRIP_OFFSETS}, {@link TAG_INTEROPERABILITY_IFD} + *

+ * Note: defining tags with these TID's is disallowed. + * + * @param tag a tag's TID (can be obtained from a defined tag constant with + * {@link #getTrueTagKey}). + * @return true if the TID is that of an offset tag. + */ + protected static boolean isOffsetTag(short tag) { + return sOffsetTags.contains(tag); + } + + /** + * Creates a tag for a defined tag constant in a given IFD if that IFD is + * allowed for the tag. This method will fail anytime the appropriate + * {@link ExifTag#setValue} for this tag's datatype would fail. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param ifdId the IFD that the tag should be in. + * @param val the value of the tag to set. + * @return an ExifTag object or null if one could not be constructed. + * @see #buildTag + */ + public ExifTag buildTag(int tagId, int ifdId, Object val) { + int info = getTagInfo().get(tagId); + if (info == 0 || val == null) { + return null; + } + short type = getTypeFromInfo(info); + int definedCount = getComponentCountFromInfo(info); + boolean hasDefinedCount = (definedCount != ExifTag.SIZE_UNDEFINED); + if (!ExifInterface.isIfdAllowed(info, ifdId)) { + return null; + } + ExifTag t = new ExifTag(getTrueTagKey(tagId), type, definedCount, ifdId, hasDefinedCount); + if (!t.setValue(val)) { + return null; + } + return t; + } + + /** + * Creates a tag for a defined tag constant in the tag's default IFD. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param val the tag's value. + * @return an ExifTag object. + */ + public ExifTag buildTag(int tagId, Object val) { + int ifdId = getTrueIfd(tagId); + return buildTag(tagId, ifdId, val); + } + + protected ExifTag buildUninitializedTag(int tagId) { + int info = getTagInfo().get(tagId); + if (info == 0) { + return null; + } + short type = getTypeFromInfo(info); + int definedCount = getComponentCountFromInfo(info); + boolean hasDefinedCount = (definedCount != ExifTag.SIZE_UNDEFINED); + int ifdId = getTrueIfd(tagId); + ExifTag t = new ExifTag(getTrueTagKey(tagId), type, definedCount, ifdId, hasDefinedCount); + return t; + } + + /** + * Sets the value of an ExifTag if it exists in the given IFD. The value + * must be the correct type and length for that ExifTag. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param ifdId the IFD that the ExifTag is in. + * @param val the value to set. + * @return true if success, false if the ExifTag doesn't exist or the value + * is the wrong type/length. + * @see #setTagValue + */ + public boolean setTagValue(int tagId, int ifdId, Object val) { + ExifTag t = getTag(tagId, ifdId); + if (t == null) { + return false; + } + return t.setValue(val); + } + + /** + * Sets the value of an ExifTag if it exists it's default IFD. The value + * must be the correct type and length for that ExifTag. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param val the value to set. + * @return true if success, false if the ExifTag doesn't exist or the value + * is the wrong type/length. + */ + public boolean setTagValue(int tagId, Object val) { + int ifdId = getDefinedTagDefaultIfd(tagId); + return setTagValue(tagId, ifdId, val); + } + + /** + * Puts an ExifTag into this ExifInterface object's tags, removing a + * previous ExifTag with the same TID and IFD. The IFD it is put into will + * be the one the tag was created with in {@link #buildTag}. + * + * @param tag an ExifTag to put into this ExifInterface's tags. + * @return the previous ExifTag with the same TID and IFD or null if none + * exists. + */ + public ExifTag setTag(ExifTag tag) { + return mData.addTag(tag); + } + + /** + * Puts a collection of ExifTags into this ExifInterface objects's tags. Any + * previous ExifTags with the same TID and IFDs will be removed. + * + * @param tags a Collection of ExifTags. + * @see #setTag + */ + public void setTags(Collection tags) { + for (ExifTag t : tags) { + setTag(t); + } + } + + /** + * Removes the ExifTag for a tag constant from the given IFD. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + * @param ifdId the IFD of the ExifTag to remove. + */ + public void deleteTag(int tagId, int ifdId) { + mData.removeTag(getTrueTagKey(tagId), ifdId); + } + + /** + * Removes the ExifTag for a tag constant from that tag's default IFD. + * + * @param tagId a tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + */ + public void deleteTag(int tagId) { + int ifdId = getDefinedTagDefaultIfd(tagId); + deleteTag(tagId, ifdId); + } + + /** + * Creates a new tag definition in this ExifInterface object for a given TID + * and default IFD. Creating a definition with the same TID and default IFD + * as a previous definition will override it. + * + * @param tagId the TID for the tag. + * @param defaultIfd the default IFD for the tag. + * @param tagType the type of the tag (see {@link ExifTag#getDataType()}). + * @param defaultComponentCount the number of elements of this tag's type in + * the tags value. + * @param allowedIfds the IFD's this tag is allowed to be put in. + * @return the defined tag constant (e.g. {@link #TAG_IMAGE_WIDTH}) or + * {@link #TAG_NULL} if the definition could not be made. + */ + public int setTagDefinition(short tagId, int defaultIfd, short tagType, + short defaultComponentCount, int[] allowedIfds) { + if (sBannedDefines.contains(tagId)) { + return TAG_NULL; + } + if (ExifTag.isValidType(tagType) && ExifTag.isValidIfd(defaultIfd)) { + int tagDef = defineTag(defaultIfd, tagId); + if (tagDef == TAG_NULL) { + return TAG_NULL; + } + int[] otherDefs = getTagDefinitionsForTagId(tagId); + SparseIntArray infos = getTagInfo(); + // Make sure defaultIfd is in allowedIfds + boolean defaultCheck = false; + for (int i : allowedIfds) { + if (defaultIfd == i) { + defaultCheck = true; + } + if (!ExifTag.isValidIfd(i)) { + return TAG_NULL; + } + } + if (!defaultCheck) { + return TAG_NULL; + } + + int ifdFlags = getFlagsFromAllowedIfds(allowedIfds); + // Make sure no identical tags can exist in allowedIfds + if (otherDefs != null) { + for (int def : otherDefs) { + int tagInfo = infos.get(def); + int allowedFlags = getAllowedIfdFlagsFromInfo(tagInfo); + if ((ifdFlags & allowedFlags) != 0) { + return TAG_NULL; + } + } + } + getTagInfo().put(tagDef, ifdFlags << 24 | (tagType << 16) | defaultComponentCount); + return tagDef; + } + return TAG_NULL; + } + + protected int getTagDefinition(short tagId, int defaultIfd) { + return getTagInfo().get(defineTag(defaultIfd, tagId)); + } + + protected int[] getTagDefinitionsForTagId(short tagId) { + int[] ifds = IfdData.getIfds(); + int[] defs = new int[ifds.length]; + int counter = 0; + SparseIntArray infos = getTagInfo(); + for (int i : ifds) { + int def = defineTag(i, tagId); + if (infos.get(def) != DEFINITION_NULL) { + defs[counter++] = def; + } + } + if (counter == 0) { + return null; + } + + return Arrays.copyOfRange(defs, 0, counter); + } + + protected int getTagDefinitionForTag(ExifTag tag) { + short type = tag.getDataType(); + int count = tag.getComponentCount(); + int ifd = tag.getIfd(); + return getTagDefinitionForTag(tag.getTagId(), type, count, ifd); + } + + protected int getTagDefinitionForTag(short tagId, short type, int count, int ifd) { + int[] defs = getTagDefinitionsForTagId(tagId); + if (defs == null) { + return TAG_NULL; + } + SparseIntArray infos = getTagInfo(); + int ret = TAG_NULL; + for (int i : defs) { + int info = infos.get(i); + short def_type = getTypeFromInfo(info); + int def_count = getComponentCountFromInfo(info); + int[] def_ifds = getAllowedIfdsFromInfo(info); + boolean valid_ifd = false; + for (int j : def_ifds) { + if (j == ifd) { + valid_ifd = true; + break; + } + } + if (valid_ifd && type == def_type + && (count == def_count || def_count == ExifTag.SIZE_UNDEFINED)) { + ret = i; + break; + } + } + return ret; + } + + /** + * Removes a tag definition for given defined tag constant. + * + * @param tagId a defined tag constant, e.g. {@link #TAG_IMAGE_WIDTH}. + */ + public void removeTagDefinition(int tagId) { + getTagInfo().delete(tagId); + } + + /** + * Resets tag definitions to the default ones. + */ + public void resetTagDefinitions() { + mTagInfo = null; + } + + /** + * Returns the thumbnail from IFD1 as a bitmap, or null if none exists. + * + * @return the thumbnail as a bitmap. + */ + public Bitmap getThumbnailBitmap() { + if (mData.hasCompressedThumbnail()) { + byte[] thumb = mData.getCompressedThumbnail(); + return BitmapFactory.decodeByteArray(thumb, 0, thumb.length); + } else if (mData.hasUncompressedStrip()) { + // TODO: implement uncompressed + } + return null; + } + + /** + * Returns the thumbnail from IFD1 as a byte array, or null if none exists. + * The bytes may either be an uncompressed strip as specified in the exif + * standard or a jpeg compressed image. + * + * @return the thumbnail as a byte array. + */ + public byte[] getThumbnailBytes() { + if (mData.hasCompressedThumbnail()) { + return mData.getCompressedThumbnail(); + } else if (mData.hasUncompressedStrip()) { + // TODO: implement this + } + return null; + } + + /** + * Returns the thumbnail if it is jpeg compressed, or null if none exists. + * + * @return the thumbnail as a byte array. + */ + public byte[] getThumbnail() { + return mData.getCompressedThumbnail(); + } + + /** + * Check if thumbnail is compressed. + * + * @return true if the thumbnail is compressed. + */ + public boolean isThumbnailCompressed() { + return mData.hasCompressedThumbnail(); + } + + /** + * Check if thumbnail exists. + * + * @return true if a compressed thumbnail exists. + */ + public boolean hasThumbnail() { + // TODO: add back in uncompressed strip + return mData.hasCompressedThumbnail(); + } + + // TODO: uncompressed thumbnail setters + + /** + * Sets the thumbnail to be a jpeg compressed image. Clears any prior + * thumbnail. + * + * @param thumb a byte array containing a jpeg compressed image. + * @return true if the thumbnail was set. + */ + public boolean setCompressedThumbnail(byte[] thumb) { + mData.clearThumbnailAndStrips(); + mData.setCompressedThumbnail(thumb); + return true; + } + + /** + * Sets the thumbnail to be a jpeg compressed bitmap. Clears any prior + * thumbnail. + * + * @param thumb a bitmap to compress to a jpeg thumbnail. + * @return true if the thumbnail was set. + */ + public boolean setCompressedThumbnail(Bitmap thumb) { + ByteArrayOutputStream thumbnail = new ByteArrayOutputStream(); + if (!thumb.compress(Bitmap.CompressFormat.JPEG, 90, thumbnail)) { + return false; + } + return setCompressedThumbnail(thumbnail.toByteArray()); + } + + /** + * Clears the compressed thumbnail if it exists. + */ + public void removeCompressedThumbnail() { + mData.setCompressedThumbnail(null); + } + + // Convenience methods: + + /** + * Decodes the user comment tag into string as specified in the EXIF + * standard. Returns null if decoding failed. + */ + public String getUserComment() { + return mData.getUserComment(); + } + + /** + * Returns the Orientation ExifTag value for a given number of degrees. + * + * @param degrees the amount an image is rotated in degrees. + */ + public static short getOrientationValueForRotation(int degrees) { + degrees %= 360; + if (degrees < 0) { + degrees += 360; + } + if (degrees < 90) { + return Orientation.TOP_LEFT; // 0 degrees + } else if (degrees < 180) { + return Orientation.RIGHT_TOP; // 90 degrees cw + } else if (degrees < 270) { + return Orientation.BOTTOM_LEFT; // 180 degrees + } else { + return Orientation.RIGHT_BOTTOM; // 270 degrees cw + } + } + + /** + * Returns the rotation degrees corresponding to an ExifTag Orientation + * value. + * + * @param orientation the ExifTag Orientation value. + */ + public static int getRotationForOrientationValue(short orientation) { + switch (orientation) { + case Orientation.TOP_LEFT: + return 0; + case Orientation.RIGHT_TOP: + return 90; + case Orientation.BOTTOM_LEFT: + return 180; + case Orientation.RIGHT_BOTTOM: + return 270; + default: + return 0; + } + } + + /** + * Gets the double representation of the GPS latitude or longitude + * coordinate. + * + * @param coordinate an array of 3 Rationals representing the degrees, + * minutes, and seconds of the GPS location as defined in the + * exif specification. + * @param reference a GPS reference reperesented by a String containing "N", + * "S", "E", or "W". + * @return the GPS coordinate represented as degrees + minutes/60 + + * seconds/3600 + */ + public static double convertLatOrLongToDouble(Rational[] coordinate, String reference) { + try { + double degrees = coordinate[0].toDouble(); + double minutes = coordinate[1].toDouble(); + double seconds = coordinate[2].toDouble(); + double result = degrees + minutes / 60.0 + seconds / 3600.0; + if ((reference.equals("S") || reference.equals("W"))) { + return -result; + } + return result; + } catch (ArrayIndexOutOfBoundsException e) { + throw new IllegalArgumentException(); + } + } + + /** + * Gets the GPS latitude and longitude as a pair of doubles from this + * ExifInterface object's tags, or null if the necessary tags do not exist. + * + * @return an array of 2 doubles containing the latitude, and longitude + * respectively. + * @see #convertLatOrLongToDouble + */ + public double[] getLatLongAsDoubles() { + Rational[] latitude = getTagRationalValues(TAG_GPS_LATITUDE); + String latitudeRef = getTagStringValue(TAG_GPS_LATITUDE_REF); + Rational[] longitude = getTagRationalValues(TAG_GPS_LONGITUDE); + String longitudeRef = getTagStringValue(TAG_GPS_LONGITUDE_REF); + if (latitude == null || longitude == null || latitudeRef == null || longitudeRef == null + || latitude.length < 3 || longitude.length < 3) { + return null; + } + double[] latLon = new double[2]; + latLon[0] = convertLatOrLongToDouble(latitude, latitudeRef); + latLon[1] = convertLatOrLongToDouble(longitude, longitudeRef); + return latLon; + } + + private static final String GPS_DATE_FORMAT_STR = "yyyy:MM:dd"; + private static final String DATETIME_FORMAT_STR = "yyyy:MM:dd kk:mm:ss"; + private final DateFormat mDateTimeStampFormat = new SimpleDateFormat(DATETIME_FORMAT_STR); + private final DateFormat mGPSDateStampFormat = new SimpleDateFormat(GPS_DATE_FORMAT_STR); + private final Calendar mGPSTimeStampCalendar = Calendar + .getInstance(TimeZone.getTimeZone("UTC")); + + /** + * Creates, formats, and sets the DateTimeStamp tag for one of: + * {@link #TAG_DATE_TIME}, {@link #TAG_DATE_TIME_DIGITIZED}, + * {@link #TAG_DATE_TIME_ORIGINAL}. + * + * @param tagId one of the DateTimeStamp tags. + * @param timestamp a timestamp to format. + * @param timezone a TimeZone object. + * @return true if success, false if the tag could not be set. + */ + public boolean addDateTimeStampTag(int tagId, long timestamp, TimeZone timezone) { + if (tagId == TAG_DATE_TIME || tagId == TAG_DATE_TIME_DIGITIZED + || tagId == TAG_DATE_TIME_ORIGINAL) { + mDateTimeStampFormat.setTimeZone(timezone); + ExifTag t = buildTag(tagId, mDateTimeStampFormat.format(timestamp)); + if (t == null) { + return false; + } + setTag(t); + } else { + return false; + } + return true; + } + + /** + * Creates and sets all to the GPS tags for a give latitude and longitude. + * + * @param latitude a GPS latitude coordinate. + * @param longitude a GPS longitude coordinate. + * @return true if success, false if they could not be created or set. + */ + public boolean addGpsTags(double latitude, double longitude) { + ExifTag latTag = buildTag(TAG_GPS_LATITUDE, toExifLatLong(latitude)); + ExifTag longTag = buildTag(TAG_GPS_LONGITUDE, toExifLatLong(longitude)); + ExifTag latRefTag = buildTag(TAG_GPS_LATITUDE_REF, + latitude >= 0 ? ExifInterface.GpsLatitudeRef.NORTH + : ExifInterface.GpsLatitudeRef.SOUTH); + ExifTag longRefTag = buildTag(TAG_GPS_LONGITUDE_REF, + longitude >= 0 ? ExifInterface.GpsLongitudeRef.EAST + : ExifInterface.GpsLongitudeRef.WEST); + if (latTag == null || longTag == null || latRefTag == null || longRefTag == null) { + return false; + } + setTag(latTag); + setTag(longTag); + setTag(latRefTag); + setTag(longRefTag); + return true; + } + + /** + * Creates and sets the GPS timestamp tag. + * + * @param timestamp a GPS timestamp. + * @return true if success, false if could not be created or set. + */ + public boolean addGpsDateTimeStampTag(long timestamp) { + ExifTag t = buildTag(TAG_GPS_DATE_STAMP, mGPSDateStampFormat.format(timestamp)); + if (t == null) { + return false; + } + setTag(t); + mGPSTimeStampCalendar.setTimeInMillis(timestamp); + t = buildTag(TAG_GPS_TIME_STAMP, new Rational[] { + new Rational(mGPSTimeStampCalendar.get(Calendar.HOUR_OF_DAY), 1), + new Rational(mGPSTimeStampCalendar.get(Calendar.MINUTE), 1), + new Rational(mGPSTimeStampCalendar.get(Calendar.SECOND), 1) + }); + if (t == null) { + return false; + } + setTag(t); + return true; + } + + private static Rational[] toExifLatLong(double value) { + // convert to the format dd/1 mm/1 ssss/100 + value = Math.abs(value); + int degrees = (int) value; + value = (value - degrees) * 60; + int minutes = (int) value; + value = (value - minutes) * 6000; + int seconds = (int) value; + return new Rational[] { + new Rational(degrees, 1), new Rational(minutes, 1), new Rational(seconds, 100) + }; + } + + private void doExifStreamIO(InputStream is, OutputStream os) throws IOException { + byte[] buf = new byte[1024]; + int ret = is.read(buf, 0, 1024); + while (ret != -1) { + os.write(buf, 0, ret); + ret = is.read(buf, 0, 1024); + } + } + + protected static void closeSilently(Closeable c) { + if (c != null) { + try { + c.close(); + } catch (Throwable e) { + // ignored + } + } + } + + private SparseIntArray mTagInfo = null; + + protected SparseIntArray getTagInfo() { + if (mTagInfo == null) { + mTagInfo = new SparseIntArray(); + initTagInfo(); + } + return mTagInfo; + } + + private void initTagInfo() { + /** + * We put tag information in a 4-bytes integer. The first byte a bitmask + * representing the allowed IFDs of the tag, the second byte is the data + * type, and the last two byte are a short value indicating the default + * component count of this tag. + */ + // IFD0 tags + int[] ifdAllowedIfds = { + IfdId.TYPE_IFD_0, IfdId.TYPE_IFD_1 + }; + int ifdFlags = getFlagsFromAllowedIfds(ifdAllowedIfds) << 24; + mTagInfo.put(ExifInterface.TAG_MAKE, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_IMAGE_WIDTH, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_IMAGE_LENGTH, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_BITS_PER_SAMPLE, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 3); + mTagInfo.put(ExifInterface.TAG_COMPRESSION, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_PHOTOMETRIC_INTERPRETATION, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_ORIENTATION, ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 + | 1); + mTagInfo.put(ExifInterface.TAG_SAMPLES_PER_PIXEL, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_PLANAR_CONFIGURATION, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_Y_CB_CR_SUB_SAMPLING, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 2); + mTagInfo.put(ExifInterface.TAG_Y_CB_CR_POSITIONING, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_X_RESOLUTION, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_Y_RESOLUTION, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_RESOLUTION_UNIT, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_STRIP_OFFSETS, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_ROWS_PER_STRIP, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_STRIP_BYTE_COUNTS, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_TRANSFER_FUNCTION, + ifdFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 3 * 256); + mTagInfo.put(ExifInterface.TAG_WHITE_POINT, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 2); + mTagInfo.put(ExifInterface.TAG_PRIMARY_CHROMATICITIES, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 6); + mTagInfo.put(ExifInterface.TAG_Y_CB_CR_COEFFICIENTS, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 3); + mTagInfo.put(ExifInterface.TAG_REFERENCE_BLACK_WHITE, + ifdFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 6); + mTagInfo.put(ExifInterface.TAG_DATE_TIME, + ifdFlags | ExifTag.TYPE_ASCII << 16 | 20); + mTagInfo.put(ExifInterface.TAG_IMAGE_DESCRIPTION, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_MAKE, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_MODEL, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_SOFTWARE, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_ARTIST, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_COPYRIGHT, + ifdFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_EXIF_IFD, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_IFD, + ifdFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + // IFD1 tags + int[] ifd1AllowedIfds = { + IfdId.TYPE_IFD_1 + }; + int ifdFlags1 = getFlagsFromAllowedIfds(ifd1AllowedIfds) << 24; + mTagInfo.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT, + ifdFlags1 | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, + ifdFlags1 | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + // Exif tags + int[] exifAllowedIfds = { + IfdId.TYPE_IFD_EXIF + }; + int exifFlags = getFlagsFromAllowedIfds(exifAllowedIfds) << 24; + mTagInfo.put(ExifInterface.TAG_EXIF_VERSION, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | 4); + mTagInfo.put(ExifInterface.TAG_FLASHPIX_VERSION, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | 4); + mTagInfo.put(ExifInterface.TAG_COLOR_SPACE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_COMPONENTS_CONFIGURATION, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | 4); + mTagInfo.put(ExifInterface.TAG_COMPRESSED_BITS_PER_PIXEL, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_PIXEL_X_DIMENSION, + exifFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_PIXEL_Y_DIMENSION, + exifFlags | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + mTagInfo.put(ExifInterface.TAG_MAKER_NOTE, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_USER_COMMENT, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_RELATED_SOUND_FILE, + exifFlags | ExifTag.TYPE_ASCII << 16 | 13); + mTagInfo.put(ExifInterface.TAG_DATE_TIME_ORIGINAL, + exifFlags | ExifTag.TYPE_ASCII << 16 | 20); + mTagInfo.put(ExifInterface.TAG_DATE_TIME_DIGITIZED, + exifFlags | ExifTag.TYPE_ASCII << 16 | 20); + mTagInfo.put(ExifInterface.TAG_SUB_SEC_TIME, + exifFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_SUB_SEC_TIME_ORIGINAL, + exifFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_SUB_SEC_TIME_DIGITIZED, + exifFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_IMAGE_UNIQUE_ID, + exifFlags | ExifTag.TYPE_ASCII << 16 | 33); + mTagInfo.put(ExifInterface.TAG_EXPOSURE_TIME, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_F_NUMBER, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_EXPOSURE_PROGRAM, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SPECTRAL_SENSITIVITY, + exifFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_ISO_SPEED_RATINGS, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_OECF, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_SHUTTER_SPEED_VALUE, + exifFlags | ExifTag.TYPE_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_APERTURE_VALUE, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_BRIGHTNESS_VALUE, + exifFlags | ExifTag.TYPE_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_EXPOSURE_BIAS_VALUE, + exifFlags | ExifTag.TYPE_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_MAX_APERTURE_VALUE, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SUBJECT_DISTANCE, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_METERING_MODE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_LIGHT_SOURCE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FLASH, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FOCAL_LENGTH, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SUBJECT_AREA, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_FLASH_ENERGY, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SPATIAL_FREQUENCY_RESPONSE, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_FOCAL_PLANE_X_RESOLUTION, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FOCAL_PLANE_Y_RESOLUTION, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FOCAL_PLANE_RESOLUTION_UNIT, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SUBJECT_LOCATION, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 2); + mTagInfo.put(ExifInterface.TAG_EXPOSURE_INDEX, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SENSING_METHOD, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FILE_SOURCE, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SCENE_TYPE, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | 1); + mTagInfo.put(ExifInterface.TAG_CFA_PATTERN, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_CUSTOM_RENDERED, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_EXPOSURE_MODE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_WHITE_BALANCE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_DIGITAL_ZOOM_RATIO, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_FOCAL_LENGTH_IN_35_MM_FILE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SCENE_CAPTURE_TYPE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GAIN_CONTROL, + exifFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_CONTRAST, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SATURATION, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_SHARPNESS, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_DEVICE_SETTING_DESCRIPTION, + exifFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_SUBJECT_DISTANCE_RANGE, + exifFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 1); + mTagInfo.put(ExifInterface.TAG_INTEROPERABILITY_IFD, exifFlags + | ExifTag.TYPE_UNSIGNED_LONG << 16 | 1); + // GPS tag + int[] gpsAllowedIfds = { + IfdId.TYPE_IFD_GPS + }; + int gpsFlags = getFlagsFromAllowedIfds(gpsAllowedIfds) << 24; + mTagInfo.put(ExifInterface.TAG_GPS_VERSION_ID, + gpsFlags | ExifTag.TYPE_UNSIGNED_BYTE << 16 | 4); + mTagInfo.put(ExifInterface.TAG_GPS_LATITUDE_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_LONGITUDE_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_LATITUDE, + gpsFlags | ExifTag.TYPE_RATIONAL << 16 | 3); + mTagInfo.put(ExifInterface.TAG_GPS_LONGITUDE, + gpsFlags | ExifTag.TYPE_RATIONAL << 16 | 3); + mTagInfo.put(ExifInterface.TAG_GPS_ALTITUDE_REF, + gpsFlags | ExifTag.TYPE_UNSIGNED_BYTE << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_ALTITUDE, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_TIME_STAMP, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 3); + mTagInfo.put(ExifInterface.TAG_GPS_SATTELLITES, + gpsFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_GPS_STATUS, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_MEASURE_MODE, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_DOP, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_SPEED_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_SPEED, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_TRACK_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_TRACK, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_IMG_DIRECTION_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_IMG_DIRECTION, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_MAP_DATUM, + gpsFlags | ExifTag.TYPE_ASCII << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_LATITUDE_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_LATITUDE, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_BEARING_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_BEARING, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_DISTANCE_REF, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 2); + mTagInfo.put(ExifInterface.TAG_GPS_DEST_DISTANCE, + gpsFlags | ExifTag.TYPE_UNSIGNED_RATIONAL << 16 | 1); + mTagInfo.put(ExifInterface.TAG_GPS_PROCESSING_METHOD, + gpsFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_GPS_AREA_INFORMATION, + gpsFlags | ExifTag.TYPE_UNDEFINED << 16 | ExifTag.SIZE_UNDEFINED); + mTagInfo.put(ExifInterface.TAG_GPS_DATE_STAMP, + gpsFlags | ExifTag.TYPE_ASCII << 16 | 11); + mTagInfo.put(ExifInterface.TAG_GPS_DIFFERENTIAL, + gpsFlags | ExifTag.TYPE_UNSIGNED_SHORT << 16 | 11); + // Interoperability tag + int[] interopAllowedIfds = { + IfdId.TYPE_IFD_INTEROPERABILITY + }; + int interopFlags = getFlagsFromAllowedIfds(interopAllowedIfds) << 24; + mTagInfo.put(TAG_INTEROPERABILITY_INDEX, interopFlags | ExifTag.TYPE_ASCII << 16 + | ExifTag.SIZE_UNDEFINED); + } + + protected static int getAllowedIfdFlagsFromInfo(int info) { + return info >>> 24; + } + + protected static int[] getAllowedIfdsFromInfo(int info) { + int ifdFlags = getAllowedIfdFlagsFromInfo(info); + int[] ifds = IfdData.getIfds(); + ArrayList l = new ArrayList(); + for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) { + int flag = (ifdFlags >> i) & 1; + if (flag == 1) { + l.add(ifds[i]); + } + } + if (l.size() <= 0) { + return null; + } + int[] ret = new int[l.size()]; + int j = 0; + for (int i : l) { + ret[j++] = i; + } + return ret; + } + + protected static boolean isIfdAllowed(int info, int ifd) { + int[] ifds = IfdData.getIfds(); + int ifdFlags = getAllowedIfdFlagsFromInfo(info); + for (int i = 0; i < ifds.length; i++) { + if (ifd == ifds[i] && ((ifdFlags >> i) & 1) == 1) { + return true; + } + } + return false; + } + + protected static int getFlagsFromAllowedIfds(int[] allowedIfds) { + if (allowedIfds == null || allowedIfds.length == 0) { + return 0; + } + int flags = 0; + int[] ifds = IfdData.getIfds(); + for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) { + for (int j : allowedIfds) { + if (ifds[i] == j) { + flags |= 1 << i; + break; + } + } + } + return flags; + } + + protected static short getTypeFromInfo(int info) { + return (short) ((info >> 16) & 0x0ff); + } + + protected static int getComponentCountFromInfo(int info) { + return info & 0x0ffff; + } + +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifModifier.java b/gallerycommon/src/com/android/gallery3d/exif/ExifModifier.java new file mode 100644 index 0000000000000000000000000000000000000000..f00362b6b0e09ffbc78e7417a74806436339baf6 --- /dev/null +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifModifier.java @@ -0,0 +1,196 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.exif; + +import android.util.Log; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.List; + +class ExifModifier { + public static final String TAG = "ExifModifier"; + public static final boolean DEBUG = false; + private final ByteBuffer mByteBuffer; + private final ExifData mTagToModified; + private final List mTagOffsets = new ArrayList(); + private final ExifInterface mInterface; + private int mOffsetBase; + + private static class TagOffset { + final int mOffset; + final ExifTag mTag; + + TagOffset(ExifTag tag, int offset) { + mTag = tag; + mOffset = offset; + } + } + + protected ExifModifier(ByteBuffer byteBuffer, ExifInterface iRef) throws IOException, + ExifInvalidFormatException { + mByteBuffer = byteBuffer; + mOffsetBase = byteBuffer.position(); + mInterface = iRef; + InputStream is = null; + try { + is = new ByteBufferInputStream(byteBuffer); + // Do not require any IFD; + ExifParser parser = ExifParser.parse(is, mInterface); + mTagToModified = new ExifData(parser.getByteOrder()); + mOffsetBase += parser.getTiffStartPosition(); + mByteBuffer.position(0); + } finally { + ExifInterface.closeSilently(is); + } + } + + protected ByteOrder getByteOrder() { + return mTagToModified.getByteOrder(); + } + + protected boolean commit() throws IOException, ExifInvalidFormatException { + InputStream is = null; + try { + is = new ByteBufferInputStream(mByteBuffer); + int flag = 0; + IfdData[] ifdDatas = new IfdData[] { + mTagToModified.getIfdData(IfdId.TYPE_IFD_0), + mTagToModified.getIfdData(IfdId.TYPE_IFD_1), + mTagToModified.getIfdData(IfdId.TYPE_IFD_EXIF), + mTagToModified.getIfdData(IfdId.TYPE_IFD_INTEROPERABILITY), + mTagToModified.getIfdData(IfdId.TYPE_IFD_GPS) + }; + + if (ifdDatas[IfdId.TYPE_IFD_0] != null) { + flag |= ExifParser.OPTION_IFD_0; + } + if (ifdDatas[IfdId.TYPE_IFD_1] != null) { + flag |= ExifParser.OPTION_IFD_1; + } + if (ifdDatas[IfdId.TYPE_IFD_EXIF] != null) { + flag |= ExifParser.OPTION_IFD_EXIF; + } + if (ifdDatas[IfdId.TYPE_IFD_GPS] != null) { + flag |= ExifParser.OPTION_IFD_GPS; + } + if (ifdDatas[IfdId.TYPE_IFD_INTEROPERABILITY] != null) { + flag |= ExifParser.OPTION_IFD_INTEROPERABILITY; + } + + ExifParser parser = ExifParser.parse(is, flag, mInterface); + int event = parser.next(); + IfdData currIfd = null; + while (event != ExifParser.EVENT_END) { + switch (event) { + case ExifParser.EVENT_START_OF_IFD: + currIfd = ifdDatas[parser.getCurrentIfd()]; + if (currIfd == null) { + parser.skipRemainingTagsInCurrentIfd(); + } + break; + case ExifParser.EVENT_NEW_TAG: + ExifTag oldTag = parser.getTag(); + ExifTag newTag = currIfd.getTag(oldTag.getTagId()); + if (newTag != null) { + if (newTag.getComponentCount() != oldTag.getComponentCount() + || newTag.getDataType() != oldTag.getDataType()) { + return false; + } else { + mTagOffsets.add(new TagOffset(newTag, oldTag.getOffset())); + currIfd.removeTag(oldTag.getTagId()); + if (currIfd.getTagCount() == 0) { + parser.skipRemainingTagsInCurrentIfd(); + } + } + } + break; + } + event = parser.next(); + } + for (IfdData ifd : ifdDatas) { + if (ifd != null && ifd.getTagCount() > 0) { + return false; + } + } + modify(); + } finally { + ExifInterface.closeSilently(is); + } + return true; + } + + private void modify() { + mByteBuffer.order(getByteOrder()); + for (TagOffset tagOffset : mTagOffsets) { + writeTagValue(tagOffset.mTag, tagOffset.mOffset); + } + } + + private void writeTagValue(ExifTag tag, int offset) { + if (DEBUG) { + Log.v(TAG, "modifying tag to: \n" + tag.toString()); + Log.v(TAG, "at offset: " + offset); + } + mByteBuffer.position(offset + mOffsetBase); + switch (tag.getDataType()) { + case ExifTag.TYPE_ASCII: + byte buf[] = tag.getStringByte(); + if (buf.length == tag.getComponentCount()) { + buf[buf.length - 1] = 0; + mByteBuffer.put(buf); + } else { + mByteBuffer.put(buf); + mByteBuffer.put((byte) 0); + } + break; + case ExifTag.TYPE_LONG: + case ExifTag.TYPE_UNSIGNED_LONG: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + mByteBuffer.putInt((int) tag.getValueAt(i)); + } + break; + case ExifTag.TYPE_RATIONAL: + case ExifTag.TYPE_UNSIGNED_RATIONAL: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + Rational v = tag.getRational(i); + mByteBuffer.putInt((int) v.getNumerator()); + mByteBuffer.putInt((int) v.getDenominator()); + } + break; + case ExifTag.TYPE_UNDEFINED: + case ExifTag.TYPE_UNSIGNED_BYTE: + buf = new byte[tag.getComponentCount()]; + tag.getBytes(buf); + mByteBuffer.put(buf); + break; + case ExifTag.TYPE_UNSIGNED_SHORT: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + mByteBuffer.putShort((short) tag.getValueAt(i)); + } + break; + } + } + + public void modifyTag(ExifTag tag) { + mTagToModified.addTag(tag); + } +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifOutputStream.java b/gallerycommon/src/com/android/gallery3d/exif/ExifOutputStream.java index b8db8e34cab848020b5504d10687c49b9488f46d..7ca05f2e0bfaa2e74526c07610700ca71ebb2839 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/ExifOutputStream.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifOutputStream.java @@ -16,14 +16,51 @@ package com.android.gallery3d.exif; +import android.util.Log; + +import java.io.BufferedOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.ArrayList; -public class ExifOutputStream extends FilterOutputStream { +/** + * This class provides a way to replace the Exif header of a JPEG image. + *

+ * Below is an example of writing EXIF data into a file + * + *

+ * public static void writeExif(byte[] jpeg, ExifData exif, String path) {
+ *     OutputStream os = null;
+ *     try {
+ *         os = new FileOutputStream(path);
+ *         ExifOutputStream eos = new ExifOutputStream(os);
+ *         // Set the exif header
+ *         eos.setExifData(exif);
+ *         // Write the original jpeg out, the header will be add into the file.
+ *         eos.write(jpeg);
+ *     } catch (FileNotFoundException e) {
+ *         e.printStackTrace();
+ *     } catch (IOException e) {
+ *         e.printStackTrace();
+ *     } finally {
+ *         if (os != null) {
+ *             try {
+ *                 os.close();
+ *             } catch (IOException e) {
+ *                 e.printStackTrace();
+ *             }
+ *         }
+ *     }
+ * }
+ * 
+ */ +class ExifOutputStream extends FilterOutputStream { private static final String TAG = "ExifOutputStream"; + private static final boolean DEBUG = false; + private static final int STREAMBUFFER_SIZE = 0x00010000; // 64Kb private static final int STATE_SOI = 0; private static final int STATE_FRAME_HEADER = 1; @@ -35,22 +72,33 @@ public class ExifOutputStream extends FilterOutputStream { private static final short TIFF_LITTLE_ENDIAN = 0x4949; private static final short TAG_SIZE = 12; private static final short TIFF_HEADER_SIZE = 8; + private static final int MAX_EXIF_SIZE = 65535; private ExifData mExifData; private int mState = STATE_SOI; private int mByteToSkip; private int mByteToCopy; + private byte[] mSingleByteArray = new byte[1]; private ByteBuffer mBuffer = ByteBuffer.allocate(4); + private final ExifInterface mInterface; - public ExifOutputStream(OutputStream ou) { - super(ou); + protected ExifOutputStream(OutputStream ou, ExifInterface iRef) { + super(new BufferedOutputStream(ou, STREAMBUFFER_SIZE)); + mInterface = iRef; } - public void setExifData(ExifData exifData) { + /** + * Sets the ExifData to be written into the JPEG file. Should be called + * before writing image data. + */ + protected void setExifData(ExifData exifData) { mExifData = exifData; } - public ExifData getExifData() { + /** + * Gets the Exif header to be written into the JPEF file. + */ + protected ExifData getExifData() { return mExifData; } @@ -62,9 +110,13 @@ public class ExifOutputStream extends FilterOutputStream { return byteToRead; } + /** + * Writes the image out. The input data should be a valid JPEG format. After + * writing, it's Exif header will be replaced by the given header. + */ @Override public void write(byte[] buffer, int offset, int length) throws IOException { - while((mByteToSkip > 0 || mByteToCopy > 0 || mState != STATE_JPEG_DATA) + while ((mByteToSkip > 0 || mByteToCopy > 0 || mState != STATE_JPEG_DATA) && length > 0) { if (mByteToSkip > 0) { int byteToProcess = length > mByteToSkip ? mByteToSkip : length; @@ -79,22 +131,29 @@ public class ExifOutputStream extends FilterOutputStream { mByteToCopy -= byteToProcess; offset += byteToProcess; } - if (length == 0) return; + if (length == 0) { + return; + } switch (mState) { case STATE_SOI: int byteRead = requestByteToBuffer(2, buffer, offset, length); offset += byteRead; length -= byteRead; - if (mBuffer.position() < 2) return; + if (mBuffer.position() < 2) { + return; + } mBuffer.rewind(); - assert(mBuffer.getShort() == JpegHeader.SOI); - out.write(mBuffer.array(), 0 ,2); + if (mBuffer.getShort() != JpegHeader.SOI) { + throw new IOException("Not a valid jpeg image, cannot write exif"); + } + out.write(mBuffer.array(), 0, 2); mState = STATE_FRAME_HEADER; mBuffer.rewind(); writeExifData(); break; case STATE_FRAME_HEADER: - // We ignore the APP1 segment and copy all other segments until SOF tag. + // We ignore the APP1 segment and copy all other segments + // until SOF tag. byteRead = requestByteToBuffer(4, buffer, offset, length); offset += byteRead; length -= byteRead; @@ -106,15 +165,17 @@ public class ExifOutputStream extends FilterOutputStream { mBuffer.rewind(); } } - if (mBuffer.position() < 4) return; + if (mBuffer.position() < 4) { + return; + } mBuffer.rewind(); short marker = mBuffer.getShort(); if (marker == JpegHeader.APP1) { - mByteToSkip = (mBuffer.getShort() & 0xff) - 2; + mByteToSkip = (mBuffer.getShort() & 0x0000ffff) - 2; mState = STATE_JPEG_DATA; } else if (!JpegHeader.isSofMarker(marker)) { out.write(mBuffer.array(), 0, 4); - mByteToCopy = (mBuffer.getShort() & 0xff) - 2; + mByteToCopy = (mBuffer.getShort() & 0x0000ffff) - 2; } else { out.write(mBuffer.array(), 0, 4); mState = STATE_JPEG_DATA; @@ -127,20 +188,37 @@ public class ExifOutputStream extends FilterOutputStream { } } + /** + * Writes the one bytes out. The input data should be a valid JPEG format. + * After writing, it's Exif header will be replaced by the given header. + */ @Override public void write(int oneByte) throws IOException { - byte[] buf = new byte[] {(byte) (0xff & oneByte)}; - write(buf); + mSingleByteArray[0] = (byte) (0xff & oneByte); + write(mSingleByteArray); } + /** + * Equivalent to calling write(buffer, 0, buffer.length). + */ @Override public void write(byte[] buffer) throws IOException { write(buffer, 0, buffer.length); } private void writeExifData() throws IOException { + if (mExifData == null) { + return; + } + if (DEBUG) { + Log.v(TAG, "Writing exif data..."); + } + ArrayList nullTags = stripNullValueTags(mExifData); createRequiredIfdAndTag(); int exifSize = calculateAllOffset(); + if (exifSize + 8 > MAX_EXIF_SIZE) { + throw new IOException("Exif header is too large (>64Kb)"); + } OrderedDataOutputStream dataOutputStream = new OrderedDataOutputStream(out); dataOutputStream.setByteOrder(ByteOrder.BIG_ENDIAN); dataOutputStream.writeShort(JpegHeader.APP1); @@ -157,6 +235,20 @@ public class ExifOutputStream extends FilterOutputStream { dataOutputStream.writeInt(8); writeAllTags(dataOutputStream); writeThumbnail(dataOutputStream); + for (ExifTag t : nullTags) { + mExifData.addTag(t); + } + } + + private ArrayList stripNullValueTags(ExifData data) { + ArrayList nullTags = new ArrayList(); + for(ExifTag t : data.getAllTags()) { + if (t.getValue() == null && !ExifInterface.isOffsetTag(t.getTagId())) { + data.removeTag(t.getTagId(), t.getIfd()); + nullTags.add(t); + } + } + return nullTags; } private void writeThumbnail(OrderedDataOutputStream dataOutputStream) throws IOException { @@ -190,71 +282,34 @@ public class ExifOutputStream extends FilterOutputStream { throws IOException { ExifTag[] tags = ifd.getAllTags(); dataOutputStream.writeShort((short) tags.length); - for (ExifTag tag: tags) { + for (ExifTag tag : tags) { dataOutputStream.writeShort(tag.getTagId()); dataOutputStream.writeShort(tag.getDataType()); dataOutputStream.writeInt(tag.getComponentCount()); + if (DEBUG) { + Log.v(TAG, "\n" + tag.toString()); + } if (tag.getDataSize() > 4) { dataOutputStream.writeInt(tag.getOffset()); } else { - writeTagValue(tag, dataOutputStream); + ExifOutputStream.writeTagValue(tag, dataOutputStream); for (int i = 0, n = 4 - tag.getDataSize(); i < n; i++) { dataOutputStream.write(0); } } } dataOutputStream.writeInt(ifd.getOffsetToNextIfd()); - for (ExifTag tag: tags) { + for (ExifTag tag : tags) { if (tag.getDataSize() > 4) { - writeTagValue(tag, dataOutputStream); + ExifOutputStream.writeTagValue(tag, dataOutputStream); } } } - private void writeTagValue(ExifTag tag, OrderedDataOutputStream dataOutputStream) - throws IOException { - switch (tag.getDataType()) { - case ExifTag.TYPE_ASCII: - dataOutputStream.write(tag.getString().getBytes()); - int remain = tag.getComponentCount() - tag.getString().length(); - for (int i = 0; i < remain; i++) { - dataOutputStream.write(0); - } - break; - case ExifTag.TYPE_LONG: - for (int i = 0, n = tag.getComponentCount(); i < n; i++) { - dataOutputStream.writeInt(tag.getLong(i)); - } - break; - case ExifTag.TYPE_RATIONAL: - case ExifTag.TYPE_UNSIGNED_RATIONAL: - for (int i = 0, n = tag.getComponentCount(); i < n; i++) { - dataOutputStream.writeRational(tag.getRational(i)); - } - break; - case ExifTag.TYPE_UNDEFINED: - case ExifTag.TYPE_UNSIGNED_BYTE: - byte[] buf = new byte[tag.getComponentCount()]; - tag.getBytes(buf); - dataOutputStream.write(buf); - break; - case ExifTag.TYPE_UNSIGNED_LONG: - for (int i = 0, n = tag.getComponentCount(); i < n; i++) { - dataOutputStream.writeInt((int) tag.getUnsignedLong(i)); - } - break; - case ExifTag.TYPE_UNSIGNED_SHORT: - for (int i = 0, n = tag.getComponentCount(); i < n; i++) { - dataOutputStream.writeShort((short) tag.getUnsignedShort(i)); - } - break; - } - } - private int calculateOffsetOfIfd(IfdData ifd, int offset) { offset += 2 + ifd.getTagCount() * TAG_SIZE + 4; ExifTag[] tags = ifd.getAllTags(); - for(ExifTag tag: tags) { + for (ExifTag tag : tags) { if (tag.getDataSize() > 4) { tag.setOffset(offset); offset += tag.getDataSize(); @@ -263,18 +318,21 @@ public class ExifOutputStream extends FilterOutputStream { return offset; } - private void createRequiredIfdAndTag() { + private void createRequiredIfdAndTag() throws IOException { // IFD0 is required for all file IfdData ifd0 = mExifData.getIfdData(IfdId.TYPE_IFD_0); if (ifd0 == null) { ifd0 = new IfdData(IfdId.TYPE_IFD_0); mExifData.addIfdData(ifd0); } - ExifTag exifOffsetTag = new ExifTag(ExifTag.TAG_EXIF_IFD, - ExifTag.TYPE_UNSIGNED_LONG, 1, IfdId.TYPE_IFD_0); + ExifTag exifOffsetTag = mInterface.buildUninitializedTag(ExifInterface.TAG_EXIF_IFD); + if (exifOffsetTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_EXIF_IFD); + } ifd0.setTag(exifOffsetTag); - // Exif IFD is required for all file. + // Exif IFD is required for all files. IfdData exifIfd = mExifData.getIfdData(IfdId.TYPE_IFD_EXIF); if (exifIfd == null) { exifIfd = new IfdData(IfdId.TYPE_IFD_EXIF); @@ -284,16 +342,23 @@ public class ExifOutputStream extends FilterOutputStream { // GPS IFD IfdData gpsIfd = mExifData.getIfdData(IfdId.TYPE_IFD_GPS); if (gpsIfd != null) { - ExifTag gpsOffsetTag = new ExifTag(ExifTag.TAG_GPS_IFD, - ExifTag.TYPE_UNSIGNED_LONG, 1, IfdId.TYPE_IFD_0); + ExifTag gpsOffsetTag = mInterface.buildUninitializedTag(ExifInterface.TAG_GPS_IFD); + if (gpsOffsetTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_GPS_IFD); + } ifd0.setTag(gpsOffsetTag); } // Interoperability IFD IfdData interIfd = mExifData.getIfdData(IfdId.TYPE_IFD_INTEROPERABILITY); if (interIfd != null) { - ExifTag interOffsetTag = new ExifTag(ExifTag.TAG_INTEROPERABILITY_IFD, - ExifTag.TYPE_UNSIGNED_LONG, 1, IfdId.TYPE_IFD_EXIF); + ExifTag interOffsetTag = mInterface + .buildUninitializedTag(ExifInterface.TAG_INTEROPERABILITY_IFD); + if (interOffsetTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_INTEROPERABILITY_IFD); + } exifIfd.setTag(interOffsetTag); } @@ -301,27 +366,50 @@ public class ExifOutputStream extends FilterOutputStream { // thumbnail if (mExifData.hasCompressedThumbnail()) { + if (ifd1 == null) { ifd1 = new IfdData(IfdId.TYPE_IFD_1); mExifData.addIfdData(ifd1); } - ExifTag offsetTag = new ExifTag(ExifTag.TAG_JPEG_INTERCHANGE_FORMAT, - ExifTag.TYPE_UNSIGNED_LONG, 1, IfdId.TYPE_IFD_1); + + ExifTag offsetTag = mInterface + .buildUninitializedTag(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT); + if (offsetTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT); + } + ifd1.setTag(offsetTag); - ExifTag lengthTag = new ExifTag(ExifTag.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, - ExifTag.TYPE_UNSIGNED_LONG, 1, IfdId.TYPE_IFD_1); + ExifTag lengthTag = mInterface + .buildUninitializedTag(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH); + if (lengthTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH); + } + lengthTag.setValue(mExifData.getCompressedThumbnail().length); ifd1.setTag(lengthTag); - } else if (mExifData.hasUncompressedStrip()){ + + // Get rid of tags for uncompressed if they exist. + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_STRIP_OFFSETS)); + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_STRIP_BYTE_COUNTS)); + } else if (mExifData.hasUncompressedStrip()) { if (ifd1 == null) { ifd1 = new IfdData(IfdId.TYPE_IFD_1); mExifData.addIfdData(ifd1); } int stripCount = mExifData.getStripCount(); - ExifTag offsetTag = new ExifTag(ExifTag.TAG_STRIP_OFFSETS, - ExifTag.TYPE_UNSIGNED_LONG, stripCount, IfdId.TYPE_IFD_1); - ExifTag lengthTag = new ExifTag(ExifTag.TAG_STRIP_BYTE_COUNTS, - ExifTag.TYPE_UNSIGNED_LONG, stripCount, IfdId.TYPE_IFD_1); + ExifTag offsetTag = mInterface.buildUninitializedTag(ExifInterface.TAG_STRIP_OFFSETS); + if (offsetTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_STRIP_OFFSETS); + } + ExifTag lengthTag = mInterface + .buildUninitializedTag(ExifInterface.TAG_STRIP_BYTE_COUNTS); + if (lengthTag == null) { + throw new IOException("No definition for crucial exif tag: " + + ExifInterface.TAG_STRIP_BYTE_COUNTS); + } long[] lengths = new long[stripCount]; for (int i = 0; i < mExifData.getStripCount(); i++) { lengths[i] = mExifData.getStrip(i).length; @@ -329,6 +417,17 @@ public class ExifOutputStream extends FilterOutputStream { lengthTag.setValue(lengths); ifd1.setTag(offsetTag); ifd1.setTag(lengthTag); + // Get rid of tags for compressed if they exist. + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT)); + ifd1.removeTag(ExifInterface + .getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)); + } else if (ifd1 != null) { + // Get rid of offset and length tags if there is no thumbnail. + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_STRIP_OFFSETS)); + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_STRIP_BYTE_COUNTS)); + ifd1.removeTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT)); + ifd1.removeTag(ExifInterface + .getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)); } } @@ -336,20 +435,21 @@ public class ExifOutputStream extends FilterOutputStream { int offset = TIFF_HEADER_SIZE; IfdData ifd0 = mExifData.getIfdData(IfdId.TYPE_IFD_0); offset = calculateOffsetOfIfd(ifd0, offset); - ifd0.getTag(ExifTag.TAG_EXIF_IFD).setValue(offset); + ifd0.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_EXIF_IFD)).setValue(offset); IfdData exifIfd = mExifData.getIfdData(IfdId.TYPE_IFD_EXIF); offset = calculateOffsetOfIfd(exifIfd, offset); IfdData interIfd = mExifData.getIfdData(IfdId.TYPE_IFD_INTEROPERABILITY); if (interIfd != null) { - exifIfd.getTag(ExifTag.TAG_INTEROPERABILITY_IFD).setValue(offset); + exifIfd.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_INTEROPERABILITY_IFD)) + .setValue(offset); offset = calculateOffsetOfIfd(interIfd, offset); } IfdData gpsIfd = mExifData.getIfdData(IfdId.TYPE_IFD_GPS); if (gpsIfd != null) { - ifd0.getTag(ExifTag.TAG_GPS_IFD).setValue(offset); + ifd0.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_GPS_IFD)).setValue(offset); offset = calculateOffsetOfIfd(gpsIfd, offset); } @@ -361,17 +461,58 @@ public class ExifOutputStream extends FilterOutputStream { // thumbnail if (mExifData.hasCompressedThumbnail()) { - ifd1.getTag(ExifTag.TAG_JPEG_INTERCHANGE_FORMAT).setValue(offset); + ifd1.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT)) + .setValue(offset); offset += mExifData.getCompressedThumbnail().length; - } else if (mExifData.hasUncompressedStrip()){ + } else if (mExifData.hasUncompressedStrip()) { int stripCount = mExifData.getStripCount(); long[] offsets = new long[stripCount]; for (int i = 0; i < mExifData.getStripCount(); i++) { offsets[i] = offset; offset += mExifData.getStrip(i).length; } - ifd1.getTag(ExifTag.TAG_STRIP_OFFSETS).setValue(offsets); + ifd1.getTag(ExifInterface.getTrueTagKey(ExifInterface.TAG_STRIP_OFFSETS)).setValue( + offsets); } return offset; } -} \ No newline at end of file + + static void writeTagValue(ExifTag tag, OrderedDataOutputStream dataOutputStream) + throws IOException { + switch (tag.getDataType()) { + case ExifTag.TYPE_ASCII: + byte buf[] = tag.getStringByte(); + if (buf.length == tag.getComponentCount()) { + buf[buf.length - 1] = 0; + dataOutputStream.write(buf); + } else { + dataOutputStream.write(buf); + dataOutputStream.write(0); + } + break; + case ExifTag.TYPE_LONG: + case ExifTag.TYPE_UNSIGNED_LONG: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + dataOutputStream.writeInt((int) tag.getValueAt(i)); + } + break; + case ExifTag.TYPE_RATIONAL: + case ExifTag.TYPE_UNSIGNED_RATIONAL: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + dataOutputStream.writeRational(tag.getRational(i)); + } + break; + case ExifTag.TYPE_UNDEFINED: + case ExifTag.TYPE_UNSIGNED_BYTE: + buf = new byte[tag.getComponentCount()]; + tag.getBytes(buf); + dataOutputStream.write(buf); + break; + case ExifTag.TYPE_UNSIGNED_SHORT: + for (int i = 0, n = tag.getComponentCount(); i < n; i++) { + dataOutputStream.writeShort((short) tag.getValueAt(i)); + } + break; + } + } +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifParser.java b/gallerycommon/src/com/android/gallery3d/exif/ExifParser.java index f1e52c5b35ab76050031b733e6be71de325cf646..5467d423d754fa1d687e69c9389e0fe1bf784c7e 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/ExifParser.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifParser.java @@ -16,8 +16,8 @@ package com.android.gallery3d.exif; -import java.io.DataInputStream; -import java.io.EOFException; +import android.util.Log; + import java.io.IOException; import java.io.InputStream; import java.nio.ByteOrder; @@ -26,10 +26,13 @@ import java.util.Map.Entry; import java.util.TreeMap; /** - * This class provides a low-level EXIF parsing API. Given a JPEG format InputStream, the caller - * can request which IFD's to read via {@link #parse(InputStream, int)} with given options. + * This class provides a low-level EXIF parsing API. Given a JPEG format + * InputStream, the caller can request which IFD's to read via + * {@link #parse(InputStream, int)} with given options. *

- * Below is an example of getting EXIF data from IFD 0 and EXIF IFD using the parser. + * Below is an example of getting EXIF data from IFD 0 and EXIF IFD using the + * parser. + * *

  * void parse() {
  *     ExifParser parser = ExifParser.parse(mImageInputStream,
@@ -63,10 +66,12 @@ import java.util.TreeMap;
  * }
  * 
*/ -public class ExifParser { +class ExifParser { + private static final boolean LOGV = false; + private static final String TAG = "ExifParser"; /** - * When the parser reaches a new IFD area. Call - * {@link #getCurrentIfd()} to know which IFD we are in. + * When the parser reaches a new IFD area. Call {@link #getCurrentIfd()} to + * know which IFD we are in. */ public static final int EVENT_START_OF_IFD = 0; /** @@ -76,8 +81,8 @@ public class ExifParser { public static final int EVENT_NEW_TAG = 1; /** * When the parser reaches the value area of tag that is registered by - * {@link #registerForTagValue(ExifTag)} previously. Call - * {@link #getTag()} to get the corresponding tag. + * {@link #registerForTagValue(ExifTag)} previously. Call {@link #getTag()} + * to get the corresponding tag. */ public static final int EVENT_VALUE_OF_REGISTERED_TAG = 2; @@ -86,8 +91,9 @@ public class ExifParser { */ public static final int EVENT_COMPRESSED_IMAGE = 3; /** - * When the parser reaches the uncompressed image strip. - * Call {@link #getStripIndex()} to get the index of the strip. + * When the parser reaches the uncompressed image strip. Call + * {@link #getStripIndex()} to get the index of the strip. + * * @see #getStripIndex() * @see #getStripCount() */ @@ -122,16 +128,20 @@ public class ExifParser { */ public static final int OPTION_THUMBNAIL = 1 << 5; - private static final int EXIF_HEADER = 0x45786966; // EXIF header "Exif" - private static final short EXIF_HEADER_TAIL = (short) 0x0000; // EXIF header in APP1 + protected static final int EXIF_HEADER = 0x45786966; // EXIF header "Exif" + protected static final short EXIF_HEADER_TAIL = (short) 0x0000; // EXIF header in APP1 // TIFF header - private static final short LITTLE_ENDIAN_TAG = (short) 0x4949; // "II" - private static final short BIG_ENDIAN_TAG = (short) 0x4d4d; // "MM" - private static final short TIFF_HEADER_TAIL = 0x002A; + protected static final short LITTLE_ENDIAN_TAG = (short) 0x4949; // "II" + protected static final short BIG_ENDIAN_TAG = (short) 0x4d4d; // "MM" + protected static final short TIFF_HEADER_TAIL = 0x002A; + + protected static final int TAG_SIZE = 12; + protected static final int OFFSET_SIZE = 2; - private static final int TAG_SIZE = 12; - private static final int OFFSET_SIZE = 2; + private static final Charset US_ASCII = Charset.forName("US-ASCII"); + + protected static final int DEFAULT_IFD0_OFFSET = 8; private final CountedDataInputStream mTiffStream; private final int mOptions; @@ -145,6 +155,26 @@ public class ExifParser { private ExifTag mJpegSizeTag; private boolean mNeedToParseOffsetsInCurrentIfd; private boolean mContainExifData = false; + private int mApp1End; + private int mOffsetToApp1EndFromSOF = 0; + private byte[] mDataAboveIfd0; + private int mIfd0Position; + private int mTiffStartPosition; + private final ExifInterface mInterface; + + private static final short TAG_EXIF_IFD = ExifInterface + .getTrueTagKey(ExifInterface.TAG_EXIF_IFD); + private static final short TAG_GPS_IFD = ExifInterface.getTrueTagKey(ExifInterface.TAG_GPS_IFD); + private static final short TAG_INTEROPERABILITY_IFD = ExifInterface + .getTrueTagKey(ExifInterface.TAG_INTEROPERABILITY_IFD); + private static final short TAG_JPEG_INTERCHANGE_FORMAT = ExifInterface + .getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT); + private static final short TAG_JPEG_INTERCHANGE_FORMAT_LENGTH = ExifInterface + .getTrueTagKey(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH); + private static final short TAG_STRIP_OFFSETS = ExifInterface + .getTrueTagKey(ExifInterface.TAG_STRIP_OFFSETS); + private static final short TAG_STRIP_BYTE_COUNTS = ExifInterface + .getTrueTagKey(ExifInterface.TAG_STRIP_BYTE_COUNTS); private final TreeMap mCorrespondingEvent = new TreeMap(); @@ -168,41 +198,62 @@ public class ExifParser { return (mOptions & OPTION_THUMBNAIL) != 0; } - private ExifParser(InputStream inputStream, int options) + private ExifParser(InputStream inputStream, int options, ExifInterface iRef) throws IOException, ExifInvalidFormatException { + if (inputStream == null) { + throw new IOException("Null argument inputStream to ExifParser"); + } + if (LOGV) { + Log.v(TAG, "Reading exif..."); + } + mInterface = iRef; mContainExifData = seekTiffData(inputStream); mTiffStream = new CountedDataInputStream(inputStream); mOptions = options; - if (!mContainExifData) return; - if (mTiffStream.getReadByteCount() == 0) { - parseTiffHeader(); - long offset = mTiffStream.readUnsignedInt(); + if (!mContainExifData) { + return; + } + + parseTiffHeader(); + long offset = mTiffStream.readUnsignedInt(); + if (offset > Integer.MAX_VALUE) { + throw new ExifInvalidFormatException("Invalid offset " + offset); + } + mIfd0Position = (int) offset; + mIfdType = IfdId.TYPE_IFD_0; + if (isIfdRequested(IfdId.TYPE_IFD_0) || needToParseOffsetsInCurrentIfd()) { registerIfd(IfdId.TYPE_IFD_0, offset); + if (offset != DEFAULT_IFD0_OFFSET) { + mDataAboveIfd0 = new byte[(int) offset - DEFAULT_IFD0_OFFSET]; + read(mDataAboveIfd0); + } } } /** * Parses the the given InputStream with the given options + * * @exception IOException * @exception ExifInvalidFormatException */ - public static ExifParser parse(InputStream inputStream, int options) - throws IOException, ExifInvalidFormatException { - return new ExifParser(inputStream, options); + protected static ExifParser parse(InputStream inputStream, int options, ExifInterface iRef) + throws IOException, ExifInvalidFormatException { + return new ExifParser(inputStream, options, iRef); } /** - * Parses the the given InputStream with default options; that is, every IFD and thumbnaill - * will be parsed. + * Parses the the given InputStream with default options; that is, every IFD + * and thumbnaill will be parsed. + * * @exception IOException * @exception ExifInvalidFormatException * @see #parse(InputStream, int) */ - public static ExifParser parse(InputStream inputStream) + protected static ExifParser parse(InputStream inputStream, ExifInterface iRef) throws IOException, ExifInvalidFormatException { return new ExifParser(inputStream, OPTION_IFD_0 | OPTION_IFD_1 | OPTION_IFD_EXIF | OPTION_IFD_GPS | OPTION_IFD_INTEROPERABILITY - | OPTION_THUMBNAIL); + | OPTION_THUMBNAIL, iRef); } /** @@ -217,7 +268,7 @@ public class ExifParser { * @see #EVENT_UNCOMPRESSED_STRIP * @see #EVENT_END */ - public int next() throws IOException, ExifInvalidFormatException { + protected int next() throws IOException, ExifInvalidFormatException { if (!mContainExifData) { return EVENT_END; } @@ -225,33 +276,59 @@ public class ExifParser { int endOfTags = mIfdStartOffset + OFFSET_SIZE + TAG_SIZE * mNumOfTagInIfd; if (offset < endOfTags) { mTag = readTag(); + if (mTag == null) { + return next(); + } if (mNeedToParseOffsetsInCurrentIfd) { checkOffsetOrImageTag(mTag); } return EVENT_NEW_TAG; } else if (offset == endOfTags) { - long ifdOffset = readUnsignedLong(); // There is a link to ifd1 at the end of ifd0 if (mIfdType == IfdId.TYPE_IFD_0) { + long ifdOffset = readUnsignedLong(); if (isIfdRequested(IfdId.TYPE_IFD_1) || isThumbnailRequested()) { if (ifdOffset != 0) { registerIfd(IfdId.TYPE_IFD_1, ifdOffset); } } } else { - if (ifdOffset != 0) { - throw new ExifInvalidFormatException("Invalid link to next IFD"); + int offsetSize = 4; + // Some camera models use invalid length of the offset + if (mCorrespondingEvent.size() > 0) { + offsetSize = mCorrespondingEvent.firstEntry().getKey() - + mTiffStream.getReadByteCount(); + } + if (offsetSize < 4) { + Log.w(TAG, "Invalid size of link to next IFD: " + offsetSize); + } else { + long ifdOffset = readUnsignedLong(); + if (ifdOffset != 0) { + Log.w(TAG, "Invalid link to next IFD: " + ifdOffset); + } } } } - while(mCorrespondingEvent.size() != 0) { + while (mCorrespondingEvent.size() != 0) { Entry entry = mCorrespondingEvent.pollFirstEntry(); Object event = entry.getValue(); - skipTo(entry.getKey()); + try { + skipTo(entry.getKey()); + } catch (IOException e) { + Log.w(TAG, "Failed to skip to data at: " + entry.getKey() + + " for " + event.getClass().getName() + ", the file may be broken."); + continue; + } if (event instanceof IfdEvent) { mIfdType = ((IfdEvent) event).ifd; mNumOfTagInIfd = mTiffStream.readUnsignedShort(); mIfdStartOffset = entry.getKey(); + + if (mNumOfTagInIfd * TAG_SIZE + mIfdStartOffset + OFFSET_SIZE > mApp1End) { + Log.w(TAG, "Invalid size of IFD " + mIfdType); + return EVENT_END; + } + mNeedToParseOffsetsInCurrentIfd = needToParseOffsetsInCurrentIfd(); if (((IfdEvent) event).isRequested) { return EVENT_START_OF_IFD; @@ -277,21 +354,26 @@ public class ExifParser { } /** - * Skips the tags area of current IFD, if the parser is not in the tag area, nothing will - * happen. + * Skips the tags area of current IFD, if the parser is not in the tag area, + * nothing will happen. * * @throws IOException * @throws ExifInvalidFormatException */ - public void skipRemainingTagsInCurrentIfd() throws IOException, ExifInvalidFormatException { + protected void skipRemainingTagsInCurrentIfd() throws IOException, ExifInvalidFormatException { int endOfTags = mIfdStartOffset + OFFSET_SIZE + TAG_SIZE * mNumOfTagInIfd; int offset = mTiffStream.getReadByteCount(); - if (offset > endOfTags) return; + if (offset > endOfTags) { + return; + } if (mNeedToParseOffsetsInCurrentIfd) { while (offset < endOfTags) { mTag = readTag(); - checkOffsetOrImageTag(mTag); offset += TAG_SIZE; + if (mTag == null) { + continue; + } + checkOffsetOrImageTag(mTag); } } else { skipTo(endOfTags); @@ -310,7 +392,8 @@ public class ExifParser { switch (mIfdType) { case IfdId.TYPE_IFD_0: return isIfdRequested(IfdId.TYPE_IFD_EXIF) || isIfdRequested(IfdId.TYPE_IFD_GPS) - || isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY); + || isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY) + || isIfdRequested(IfdId.TYPE_IFD_1); case IfdId.TYPE_IFD_1: return isThumbnailRequested(); case IfdId.TYPE_IFD_EXIF: @@ -322,38 +405,37 @@ public class ExifParser { } /** - * If {@link #next()} return {@link #EVENT_NEW_TAG} or {@link #EVENT_VALUE_OF_REGISTERED_TAG}, - * call this function to get the corresponding tag. + * If {@link #next()} return {@link #EVENT_NEW_TAG} or + * {@link #EVENT_VALUE_OF_REGISTERED_TAG}, call this function to get the + * corresponding tag. *

- * - * For {@link #EVENT_NEW_TAG}, the tag may not contain the value if the size of the value is - * greater than 4 bytes. One should call {@link ExifTag#hasValue()} to check if the tag - * contains value. - * If there is no value,call {@link #registerForTagValue(ExifTag)} to have the parser emit - * {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area pointed by the offset. - * + * For {@link #EVENT_NEW_TAG}, the tag may not contain the value if the size + * of the value is greater than 4 bytes. One should call + * {@link ExifTag#hasValue()} to check if the tag contains value. If there + * is no value,call {@link #registerForTagValue(ExifTag)} to have the parser + * emit {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area + * pointed by the offset. *

- * When {@link #EVENT_VALUE_OF_REGISTERED_TAG} is emitted, the value of the tag will have - * already been read except for tags of undefined type. For tags of undefined type, call - * one of the read methods to get the value. + * When {@link #EVENT_VALUE_OF_REGISTERED_TAG} is emitted, the value of the + * tag will have already been read except for tags of undefined type. For + * tags of undefined type, call one of the read methods to get the value. * * @see #registerForTagValue(ExifTag) * @see #read(byte[]) * @see #read(byte[], int, int) * @see #readLong() * @see #readRational() - * @see #readShort() * @see #readString(int) * @see #readString(int, Charset) */ - public ExifTag getTag() { + protected ExifTag getTag() { return mTag; } /** * Gets number of tags in the current IFD area. */ - public int getTagCountInCurrentIfd() { + protected int getTagCountInCurrentIfd() { return mNumOfTagInIfd; } @@ -366,51 +448,49 @@ public class ExifParser { * @see IfdId#TYPE_IFD_INTEROPERABILITY * @see IfdId#TYPE_IFD_EXIF */ - public int getCurrentIfd() { + protected int getCurrentIfd() { return mIfdType; } /** - * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, - * call this function to get the index of this strip. + * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to + * get the index of this strip. + * * @see #getStripCount() */ - public int getStripIndex() { + protected int getStripIndex() { return mImageEvent.stripIndex; } /** - * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to get the number - * of strip data. + * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to + * get the number of strip data. + * * @see #getStripIndex() */ - public int getStripCount() { + protected int getStripCount() { return mStripCount; } /** - * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to get the strip size. + * When receiving {@link #EVENT_UNCOMPRESSED_STRIP}, call this function to + * get the strip size. */ - public int getStripSize() { - if (mStripSizeTag == null) return 0; - if (mStripSizeTag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) { - return mStripSizeTag.getUnsignedShort(mImageEvent.stripIndex); - } else { - // Cast unsigned int to int since the strip size is always smaller - // than the size of APP1 (65536) - return (int) mStripSizeTag.getUnsignedLong(mImageEvent.stripIndex); - } + protected int getStripSize() { + if (mStripSizeTag == null) + return 0; + return (int) mStripSizeTag.getValueAt(0); } /** - * When receiving {@link #EVENT_COMPRESSED_IMAGE}, call this function to get the image data - * size. + * When receiving {@link #EVENT_COMPRESSED_IMAGE}, call this function to get + * the image data size. */ - public int getCompressedImageSize() { - if (mJpegSizeTag == null) return 0; - // Cast unsigned int to int since the thumbnail is always smaller - // than the size of APP1 (65536) - return (int) mJpegSizeTag.getUnsignedLong(0); + protected int getCompressedImageSize() { + if (mJpegSizeTag == null) { + return 0; + } + return (int) mJpegSizeTag.getValueAt(0); } private void skipTo(int offset) throws IOException { @@ -421,15 +501,18 @@ public class ExifParser { } /** - * When getting {@link #EVENT_NEW_TAG} in the tag area of IFD, - * the tag may not contain the value if the size of the value is greater than 4 bytes. - * When the value is not available here, call this method so that the parser will emit - * {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area where the value is located. - + * When getting {@link #EVENT_NEW_TAG} in the tag area of IFD, the tag may + * not contain the value if the size of the value is greater than 4 bytes. + * When the value is not available here, call this method so that the parser + * will emit {@link #EVENT_VALUE_OF_REGISTERED_TAG} when it reaches the area + * where the value is located. + * * @see #EVENT_VALUE_OF_REGISTERED_TAG */ - public void registerForTagValue(ExifTag tag) { - mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, true)); + protected void registerForTagValue(ExifTag tag) { + if (tag.getOffset() >= mTiffStream.getReadByteCount()) { + mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, true)); + } } private void registerIfd(int ifdType, long offset) { @@ -455,7 +538,15 @@ public class ExifParser { throw new ExifInvalidFormatException( "Number of component is larger then Integer.MAX_VALUE"); } - ExifTag tag = new ExifTag(tagId, dataFormat, (int) numOfComp, mIfdType); + // Some invalid image file contains invalid data type. Ignore those tags + if (!ExifTag.isValidType(dataFormat)) { + Log.w(TAG, String.format("Tag %04x: Invalid data type %d", tagId, dataFormat)); + mTiffStream.skip(4); + return null; + } + // TODO: handle numOfComp overflow + ExifTag tag = new ExifTag(tagId, dataFormat, (int) numOfComp, mIfdType, + ((int) numOfComp) != ExifTag.SIZE_UNDEFINED); int dataSize = tag.getDataSize(); if (dataSize > 4) { long offset = mTiffStream.readUnsignedInt(); @@ -463,136 +554,188 @@ public class ExifParser { throw new ExifInvalidFormatException( "offset is larger then Integer.MAX_VALUE"); } - tag.setOffset((int) offset); + // Some invalid images put some undefined data before IFD0. + // Read the data here. + if ((offset < mIfd0Position) && (dataFormat == ExifTag.TYPE_UNDEFINED)) { + byte[] buf = new byte[(int) numOfComp]; + System.arraycopy(mDataAboveIfd0, (int) offset - DEFAULT_IFD0_OFFSET, + buf, 0, (int) numOfComp); + tag.setValue(buf); + } else { + tag.setOffset((int) offset); + } } else { + boolean defCount = tag.hasDefinedCount(); + // Set defined count to 0 so we can add \0 to non-terminated strings + tag.setHasDefinedCount(false); + // Read value readFullTagValue(tag); + tag.setHasDefinedCount(defCount); mTiffStream.skip(4 - dataSize); + // Set the offset to the position of value. + tag.setOffset(mTiffStream.getReadByteCount() - 4); } return tag; } /** - * Check the tag, if the tag is one of the offset tag that points to the IFD or image the - * caller is interested in, register the IFD or image. + * Check the tag, if the tag is one of the offset tag that points to the IFD + * or image the caller is interested in, register the IFD or image. */ private void checkOffsetOrImageTag(ExifTag tag) { - switch (tag.getTagId()) { - case ExifTag.TAG_EXIF_IFD: - if (isIfdRequested(IfdId.TYPE_IFD_EXIF) - || isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) { - registerIfd(IfdId.TYPE_IFD_EXIF, tag.getUnsignedLong(0)); - } - break; - case ExifTag.TAG_GPS_IFD: - if (isIfdRequested(IfdId.TYPE_IFD_GPS)) { - registerIfd(IfdId.TYPE_IFD_GPS, tag.getUnsignedLong(0)); + // Some invalid formattd image contains tag with 0 size. + if (tag.getComponentCount() == 0) { + return; + } + short tid = tag.getTagId(); + int ifd = tag.getIfd(); + if (tid == TAG_EXIF_IFD && checkAllowed(ifd, ExifInterface.TAG_EXIF_IFD)) { + if (isIfdRequested(IfdId.TYPE_IFD_EXIF) + || isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) { + registerIfd(IfdId.TYPE_IFD_EXIF, tag.getValueAt(0)); + } + } else if (tid == TAG_GPS_IFD && checkAllowed(ifd, ExifInterface.TAG_GPS_IFD)) { + if (isIfdRequested(IfdId.TYPE_IFD_GPS)) { + registerIfd(IfdId.TYPE_IFD_GPS, tag.getValueAt(0)); + } + } else if (tid == TAG_INTEROPERABILITY_IFD + && checkAllowed(ifd, ExifInterface.TAG_INTEROPERABILITY_IFD)) { + if (isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) { + registerIfd(IfdId.TYPE_IFD_INTEROPERABILITY, tag.getValueAt(0)); + } + } else if (tid == TAG_JPEG_INTERCHANGE_FORMAT + && checkAllowed(ifd, ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT)) { + if (isThumbnailRequested()) { + registerCompressedImage(tag.getValueAt(0)); + } + } else if (tid == TAG_JPEG_INTERCHANGE_FORMAT_LENGTH + && checkAllowed(ifd, ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)) { + if (isThumbnailRequested()) { + mJpegSizeTag = tag; + } + } else if (tid == TAG_STRIP_OFFSETS && checkAllowed(ifd, ExifInterface.TAG_STRIP_OFFSETS)) { + if (isThumbnailRequested()) { + if (tag.hasValue()) { + for (int i = 0; i < tag.getComponentCount(); i++) { + if (tag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) { + registerUncompressedStrip(i, tag.getValueAt(i)); + } else { + registerUncompressedStrip(i, tag.getValueAt(i)); + } + } + } else { + mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, false)); } - break; - case ExifTag.TAG_INTEROPERABILITY_IFD: - if (isIfdRequested(IfdId.TYPE_IFD_INTEROPERABILITY)) { - registerIfd(IfdId.TYPE_IFD_INTEROPERABILITY, tag.getUnsignedLong(0)); + } + } else if (tid == TAG_STRIP_BYTE_COUNTS + && checkAllowed(ifd, ExifInterface.TAG_STRIP_BYTE_COUNTS) + &&isThumbnailRequested() && tag.hasValue()) { + mStripSizeTag = tag; + } + } + + private boolean checkAllowed(int ifd, int tagId) { + int info = mInterface.getTagInfo().get(tagId); + if (info == ExifInterface.DEFINITION_NULL) { + return false; + } + return ExifInterface.isIfdAllowed(info, ifd); + } + + protected void readFullTagValue(ExifTag tag) throws IOException { + // Some invalid images contains tags with wrong size, check it here + short type = tag.getDataType(); + if (type == ExifTag.TYPE_ASCII || type == ExifTag.TYPE_UNDEFINED || + type == ExifTag.TYPE_UNSIGNED_BYTE) { + int size = tag.getComponentCount(); + if (mCorrespondingEvent.size() > 0) { + if (mCorrespondingEvent.firstEntry().getKey() < mTiffStream.getReadByteCount() + + size) { + Object event = mCorrespondingEvent.firstEntry().getValue(); + if (event instanceof ImageEvent) { + // Tag value overlaps thumbnail, ignore thumbnail. + Log.w(TAG, "Thumbnail overlaps value for tag: \n" + tag.toString()); + Entry entry = mCorrespondingEvent.pollFirstEntry(); + Log.w(TAG, "Invalid thumbnail offset: " + entry.getKey()); + } else { + // Tag value overlaps another tag, shorten count + if (event instanceof IfdEvent) { + Log.w(TAG, "Ifd " + ((IfdEvent) event).ifd + + " overlaps value for tag: \n" + tag.toString()); + } else if (event instanceof ExifTagEvent) { + Log.w(TAG, "Tag value for tag: \n" + + ((ExifTagEvent) event).tag.toString() + + " overlaps value for tag: \n" + tag.toString()); + } + size = mCorrespondingEvent.firstEntry().getKey() + - mTiffStream.getReadByteCount(); + Log.w(TAG, "Invalid size of tag: \n" + tag.toString() + + " setting count to: " + size); + tag.forceSetComponentCount(size); + } } + } + } + switch (tag.getDataType()) { + case ExifTag.TYPE_UNSIGNED_BYTE: + case ExifTag.TYPE_UNDEFINED: { + byte buf[] = new byte[tag.getComponentCount()]; + read(buf); + tag.setValue(buf); + } break; - case ExifTag.TAG_JPEG_INTERCHANGE_FORMAT: - if (isThumbnailRequested()) { - registerCompressedImage(tag.getUnsignedLong(0)); - } + case ExifTag.TYPE_ASCII: + tag.setValue(readString(tag.getComponentCount())); break; - case ExifTag.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH: - if (isThumbnailRequested()) { - mJpegSizeTag = tag; + case ExifTag.TYPE_UNSIGNED_LONG: { + long value[] = new long[tag.getComponentCount()]; + for (int i = 0, n = value.length; i < n; i++) { + value[i] = readUnsignedLong(); } + tag.setValue(value); + } break; - case ExifTag.TAG_STRIP_OFFSETS: - if (isThumbnailRequested()) { - if (tag.hasValue()) { - for (int i = 0; i < tag.getComponentCount(); i++) { - if (tag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) { - registerUncompressedStrip(i, tag.getUnsignedShort(i)); - } else { - registerUncompressedStrip(i, tag.getUnsignedLong(i)); - } - } - } else { - mCorrespondingEvent.put(tag.getOffset(), new ExifTagEvent(tag, false)); - } + case ExifTag.TYPE_UNSIGNED_RATIONAL: { + Rational value[] = new Rational[tag.getComponentCount()]; + for (int i = 0, n = value.length; i < n; i++) { + value[i] = readUnsignedRational(); } + tag.setValue(value); + } break; - case ExifTag.TAG_STRIP_BYTE_COUNTS: - if (isThumbnailRequested()) { - if (tag.hasValue()) { - mStripSizeTag = tag; - } + case ExifTag.TYPE_UNSIGNED_SHORT: { + int value[] = new int[tag.getComponentCount()]; + for (int i = 0, n = value.length; i < n; i++) { + value[i] = readUnsignedShort(); } + tag.setValue(value); + } break; - } - } - - private void readFullTagValue(ExifTag tag) throws IOException { - switch(tag.getDataType()) { - case ExifTag.TYPE_UNSIGNED_BYTE: - case ExifTag.TYPE_UNDEFINED: - { - byte buf[] = new byte[tag.getComponentCount()]; - read(buf); - tag.setValue(buf); + case ExifTag.TYPE_LONG: { + int value[] = new int[tag.getComponentCount()]; + for (int i = 0, n = value.length; i < n; i++) { + value[i] = readLong(); } + tag.setValue(value); + } break; - case ExifTag.TYPE_ASCII: - tag.setValue(readString(tag.getComponentCount())); - break; - case ExifTag.TYPE_UNSIGNED_LONG: - { - long value[] = new long[tag.getComponentCount()]; - for (int i = 0, n = value.length; i < n; i++) { - value[i] = readUnsignedLong(); - } - tag.setValue(value); + case ExifTag.TYPE_RATIONAL: { + Rational value[] = new Rational[tag.getComponentCount()]; + for (int i = 0, n = value.length; i < n; i++) { + value[i] = readRational(); } + tag.setValue(value); + } break; - case ExifTag.TYPE_UNSIGNED_RATIONAL: - { - Rational value[] = new Rational[tag.getComponentCount()]; - for (int i = 0, n = value.length; i < n; i++) { - value[i] = readUnsignedRational(); - } - tag.setValue(value); - } - break; - case ExifTag.TYPE_UNSIGNED_SHORT: - { - int value[] = new int[tag.getComponentCount()]; - for (int i = 0, n = value.length; i < n; i++) { - value[i] = readUnsignedShort(); - } - tag.setValue(value); - } - break; - case ExifTag.TYPE_LONG: - { - int value[] = new int[tag.getComponentCount()]; - for (int i = 0, n = value.length; i < n; i++) { - value[i] = readLong(); - } - tag.setValue(value); - } - break; - case ExifTag.TYPE_RATIONAL: - { - Rational value[] = new Rational[tag.getComponentCount()]; - for (int i = 0, n = value.length; i < n; i++) { - value[i] = readRational(); - } - tag.setValue(value); - } - break; + } + if (LOGV) { + Log.v(TAG, "\n" + tag.toString()); } } private void parseTiffHeader() throws IOException, ExifInvalidFormatException { short byteOrder = mTiffStream.readShort(); - ByteOrder order; if (LITTLE_ENDIAN_TAG == byteOrder) { mTiffStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); } else if (BIG_ENDIAN_TAG == byteOrder) { @@ -608,89 +751,106 @@ public class ExifParser { private boolean seekTiffData(InputStream inputStream) throws IOException, ExifInvalidFormatException { - DataInputStream dataStream = new DataInputStream(inputStream); - - // SOI and APP1 + CountedDataInputStream dataStream = new CountedDataInputStream(inputStream); if (dataStream.readShort() != JpegHeader.SOI) { throw new ExifInvalidFormatException("Invalid JPEG format"); } short marker = dataStream.readShort(); - while(marker != JpegHeader.APP1 && marker != JpegHeader.EOI + while (marker != JpegHeader.EOI && !JpegHeader.isSofMarker(marker)) { int length = dataStream.readUnsignedShort(); - if ((length - 2) != dataStream.skip(length - 2)) { - throw new EOFException(); + // Some invalid formatted image contains multiple APP1, + // try to find the one with Exif data. + if (marker == JpegHeader.APP1) { + int header = 0; + short headerTail = 0; + if (length >= 8) { + header = dataStream.readInt(); + headerTail = dataStream.readShort(); + length -= 6; + if (header == EXIF_HEADER && headerTail == EXIF_HEADER_TAIL) { + mTiffStartPosition = dataStream.getReadByteCount(); + mApp1End = length; + mOffsetToApp1EndFromSOF = mTiffStartPosition + mApp1End; + return true; + } + } + } + if (length < 2 || (length - 2) != dataStream.skip(length - 2)) { + Log.w(TAG, "Invalid JPEG format."); + return false; } marker = dataStream.readShort(); } + return false; + } - if (marker != JpegHeader.APP1) return false; // No APP1 segment - - // APP1 length, it's not used for us - dataStream.readShort(); + protected int getOffsetToExifEndFromSOF() { + return mOffsetToApp1EndFromSOF; + } - // Exif header - return (dataStream.readInt() == EXIF_HEADER - && dataStream.readShort() == EXIF_HEADER_TAIL); + protected int getTiffStartPosition() { + return mTiffStartPosition; } /** * Reads bytes from the InputStream. */ - public int read(byte[] buffer, int offset, int length) throws IOException { + protected int read(byte[] buffer, int offset, int length) throws IOException { return mTiffStream.read(buffer, offset, length); } /** * Equivalent to read(buffer, 0, buffer.length). */ - public int read(byte[] buffer) throws IOException { + protected int read(byte[] buffer) throws IOException { return mTiffStream.read(buffer); } /** - * Reads a String from the InputStream with UTF8 charset. - * This is used for reading values of type {@link ExifTag#TYPE_ASCII}. + * Reads a String from the InputStream with US-ASCII charset. The parser + * will read n bytes and convert it to ascii string. This is used for + * reading values of type {@link ExifTag#TYPE_ASCII}. */ - public String readString(int n) throws IOException { - if (n > 0) { - byte[] buf = new byte[n]; - mTiffStream.readOrThrow(buf); - return new String(buf, 0, n - 1, "UTF8"); - } else { - return ""; - } + protected String readString(int n) throws IOException { + return readString(n, US_ASCII); } /** - * Reads a String from the InputStream with the given charset. - * This is used for reading values of type {@link ExifTag#TYPE_ASCII}. + * Reads a String from the InputStream with the given charset. The parser + * will read n bytes and convert it to string. This is used for reading + * values of type {@link ExifTag#TYPE_ASCII}. */ - public String readString(int n, Charset charset) throws IOException { - byte[] buf = new byte[n]; - mTiffStream.readOrThrow(buf); - return new String(buf, 0, n - 1, charset); + protected String readString(int n, Charset charset) throws IOException { + if (n > 0) { + return mTiffStream.readString(n, charset); + } else { + return ""; + } } /** - * Reads value of type {@link ExifTag#TYPE_UNSIGNED_SHORT} from the InputStream. + * Reads value of type {@link ExifTag#TYPE_UNSIGNED_SHORT} from the + * InputStream. */ - public int readUnsignedShort() throws IOException { + protected int readUnsignedShort() throws IOException { return mTiffStream.readShort() & 0xffff; } /** - * Reads value of type {@link ExifTag#TYPE_UNSIGNED_LONG} from the InputStream. + * Reads value of type {@link ExifTag#TYPE_UNSIGNED_LONG} from the + * InputStream. */ - public long readUnsignedLong() throws IOException { + protected long readUnsignedLong() throws IOException { return readLong() & 0xffffffffL; } /** - * Reads value of type {@link ExifTag#TYPE_UNSIGNED_RATIONAL} from the InputStream. + * Reads value of type {@link ExifTag#TYPE_UNSIGNED_RATIONAL} from the + * InputStream. */ - public Rational readUnsignedRational() throws IOException { + protected Rational readUnsignedRational() throws IOException { long nomi = readUnsignedLong(); long denomi = readUnsignedLong(); return new Rational(nomi, denomi); @@ -699,14 +859,14 @@ public class ExifParser { /** * Reads value of type {@link ExifTag#TYPE_LONG} from the InputStream. */ - public int readLong() throws IOException { + protected int readLong() throws IOException { return mTiffStream.readInt(); } /** * Reads value of type {@link ExifTag#TYPE_RATIONAL} from the InputStream. */ - public Rational readRational() throws IOException { + protected Rational readRational() throws IOException { int nomi = readLong(); int denomi = readLong(); return new Rational(nomi, denomi); @@ -715,10 +875,12 @@ public class ExifParser { private static class ImageEvent { int stripIndex; int type; + ImageEvent(int type) { this.stripIndex = 0; this.type = type; } + ImageEvent(int type, int stripIndex) { this.type = type; this.stripIndex = stripIndex; @@ -728,6 +890,7 @@ public class ExifParser { private static class IfdEvent { int ifd; boolean isRequested; + IfdEvent(int ifd, boolean isInterestedIfd) { this.ifd = ifd; this.isRequested = isInterestedIfd; @@ -737,6 +900,7 @@ public class ExifParser { private static class ExifTagEvent { ExifTag tag; boolean isRequested; + ExifTagEvent(ExifTag tag, boolean isRequireByUser) { this.tag = tag; this.isRequested = isRequireByUser; @@ -746,7 +910,7 @@ public class ExifParser { /** * Gets the byte order of the current InputStream. */ - public ByteOrder getByteOrder() { + protected ByteOrder getByteOrder() { return mTiffStream.getByteOrder(); } -} \ No newline at end of file +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifReader.java b/gallerycommon/src/com/android/gallery3d/exif/ExifReader.java index d8083b2dd3343d6a088b208e8745d624d549879f..68e972fb7d14a0a755443ffb1d2a8454c4b6002e 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/ExifReader.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifReader.java @@ -16,22 +16,36 @@ package com.android.gallery3d.exif; +import android.util.Log; + import java.io.IOException; import java.io.InputStream; /** - * This class reads the EXIF header of a JPEG file and stores it in {@link ExifData}. + * This class reads the EXIF header of a JPEG file and stores it in + * {@link ExifData}. */ -public class ExifReader { +class ExifReader { + private static final String TAG = "ExifReader"; + + private final ExifInterface mInterface; + + ExifReader(ExifInterface iRef) { + mInterface = iRef; + } + /** - * Parses the inputStream and and returns the EXIF data in an {@link ExifData}. + * Parses the inputStream and and returns the EXIF data in an + * {@link ExifData}. + * * @throws ExifInvalidFormatException * @throws IOException */ - public ExifData read(InputStream inputStream) throws ExifInvalidFormatException, + protected ExifData read(InputStream inputStream) throws ExifInvalidFormatException, IOException { - ExifParser parser = ExifParser.parse(inputStream); + ExifParser parser = ExifParser.parse(inputStream, mInterface); ExifData exifData = new ExifData(parser.getByteOrder()); + ExifTag tag = null; int event = parser.next(); while (event != ExifParser.EVENT_END) { @@ -40,7 +54,7 @@ public class ExifReader { exifData.addIfdData(new IfdData(parser.getCurrentIfd())); break; case ExifParser.EVENT_NEW_TAG: - ExifTag tag = parser.getTag(); + tag = parser.getTag(); if (!tag.hasValue()) { parser.registerForTagValue(tag); } else { @@ -50,25 +64,29 @@ public class ExifReader { case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG: tag = parser.getTag(); if (tag.getDataType() == ExifTag.TYPE_UNDEFINED) { - byte[] buf = new byte[tag.getComponentCount()]; - parser.read(buf); - tag.setValue(buf); + parser.readFullTagValue(tag); } exifData.getIfdData(tag.getIfd()).setTag(tag); break; case ExifParser.EVENT_COMPRESSED_IMAGE: byte buf[] = new byte[parser.getCompressedImageSize()]; - parser.read(buf); - exifData.setCompressedThumbnail(buf); + if (buf.length == parser.read(buf)) { + exifData.setCompressedThumbnail(buf); + } else { + Log.w(TAG, "Failed to read the compressed thumbnail"); + } break; case ExifParser.EVENT_UNCOMPRESSED_STRIP: buf = new byte[parser.getStripSize()]; - parser.read(buf); - exifData.setStripBytes(parser.getStripIndex(), buf); + if (buf.length == parser.read(buf)) { + exifData.setStripBytes(parser.getStripIndex(), buf); + } else { + Log.w(TAG, "Failed to read the strip bytes"); + } break; } event = parser.next(); } return exifData; } -} \ No newline at end of file +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/ExifTag.java b/gallerycommon/src/com/android/gallery3d/exif/ExifTag.java index 49cb6edbc7c559aa364de3038af2039440a810df..b8b387201ffef53e1b31b0e4f49a5792c8119ca3 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/ExifTag.java +++ b/gallerycommon/src/com/android/gallery3d/exif/ExifTag.java @@ -16,487 +16,26 @@ package com.android.gallery3d.exif; -import android.util.SparseArray; - +import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; /** - * This class stores information of an EXIF tag. - * @see ExifParser - * @see ExifReader - * @see IfdData - * @see ExifData + * This class stores information of an EXIF tag. For more information about + * defined EXIF tags, please read the Jeita EXIF 2.2 standard. Tags should be + * instantiated using {@link ExifInterface#buildTag}. + * + * @see ExifInterface */ public class ExifTag { - // Tiff Tags - public static final short TAG_IMAGE_WIDTH = 0x100; - /* - * The height of the image. - */ - public static final short TAG_IMAGE_LENGTH = 0x101; - public static final short TAG_BITS_PER_SAMPLE = 0x102; - public static final short TAG_COMPRESSION = 0x103; - public static final short TAG_PHOTOMETRIC_INTERPRETATION = 0x106; - public static final short TAG_IMAGE_DESCRIPTION = 0x10E; - public static final short TAG_MAKE = 0x10F; - public static final short TAG_MODEL = 0x110; - public static final short TAG_STRIP_OFFSETS = 0x111; - public static final short TAG_ORIENTATION = 0x112; - public static final short TAG_SAMPLES_PER_PIXEL = 0x115; - public static final short TAG_ROWS_PER_STRIP = 0x116; - public static final short TAG_STRIP_BYTE_COUNTS = 0x117; - public static final short TAG_X_RESOLUTION = 0x11A; - public static final short TAG_Y_RESOLUTION = 0x11B; - public static final short TAG_PLANAR_CONFIGURATION = 0x11C; - public static final short TAG_RESOLUTION_UNIT = 0x128; - public static final short TAG_TRANSFER_FUNCTION = 0x12D; - public static final short TAG_SOFTWARE = 0x131; - public static final short TAG_DATE_TIME = 0x132; - public static final short TAG_ARTIST = 0x13B; - public static final short TAG_WHITE_POINT = 0x13E; - public static final short TAG_PRIMARY_CHROMATICITIES = 0x13F; - public static final short TAG_JPEG_INTERCHANGE_FORMAT = 0x201; - public static final short TAG_JPEG_INTERCHANGE_FORMAT_LENGTH = 0x202; - public static final short TAG_Y_CB_CR_COEFFICIENTS = 0x211; - public static final short TAG_Y_CB_CR_SUB_SAMPLING = 0x212; - public static final short TAG_Y_CB_CR_POSITIONING = 0x213; - public static final short TAG_REFERENCE_BLACK_WHITE = 0x214; - public static final short TAG_COPYRIGHT = (short) 0x8298; - public static final short TAG_EXIF_IFD = (short) 0x8769; - public static final short TAG_GPS_IFD = (short) 0x8825; - - // Exif Tags - public static final short TAG_EXPOSURE_TIME = (short) 0x829A; - public static final short TAG_F_NUMBER = (short) 0x829D; - public static final short TAG_EXPOSURE_PROGRAM = (short) 0x8822; - public static final short TAG_SPECTRAL_SENSITIVITY = (short) 0x8824; - public static final short TAG_ISO_SPEED_RATINGS = (short) 0x8827; - public static final short TAG_OECF = (short) 0x8828; - public static final short TAG_EXIF_VERSION = (short) 0x9000; - public static final short TAG_DATE_TIME_ORIGINAL = (short) 0x9003; - public static final short TAG_DATE_TIME_DIGITIZED = (short) 0x9004; - public static final short TAG_COMPONENTS_CONFIGURATION = (short) 0x9101; - public static final short TAG_COMPRESSED_BITS_PER_PIXEL = (short) 0x9102; - public static final short TAG_SHUTTER_SPEED_VALUE = (short) 0x9201; - public static final short TAG_APERTURE_VALUE = (short) 0x9202; - public static final short TAG_BRIGHTNESS_VALUE = (short) 0x9203; - public static final short TAG_EXPOSURE_BIAS_VALUE = (short) 0x9204; - public static final short TAG_MAX_APERTURE_VALUE = (short) 0x9205; - public static final short TAG_SUBJECT_DISTANCE = (short) 0x9206; - public static final short TAG_METERING_MODE = (short) 0x9207; - public static final short TAG_LIGHT_SOURCE = (short) 0x9208; - public static final short TAG_FLASH = (short) 0x9209; - public static final short TAG_FOCAL_LENGTH = (short) 0x920A; - public static final short TAG_SUBJECT_AREA = (short) 0x9214; - public static final short TAG_MAKER_NOTE = (short) 0x927C; - public static final short TAG_USER_COMMENT = (short) 0x9286; - public static final short TAG_SUB_SEC_TIME = (short) 0x9290; - public static final short TAG_SUB_SEC_TIME_ORIGINAL = (short) 0x9291; - public static final short TAG_SUB_SEC_TIME_DIGITIZED = (short) 0x9292; - public static final short TAG_FLASHPIX_VERSION = (short) 0xA000; - public static final short TAG_COLOR_SPACE = (short) 0xA001; - public static final short TAG_PIXEL_X_DIMENSION = (short) 0xA002; - public static final short TAG_PIXEL_Y_DIMENSION = (short) 0xA003; - public static final short TAG_RELATED_SOUND_FILE = (short) 0xA004; - public static final short TAG_INTEROPERABILITY_IFD = (short) 0xA005; - public static final short TAG_FLASH_ENERGY = (short) 0xA20B; - public static final short TAG_SPATIAL_FREQUENCY_RESPONSE = (short) 0xA20C; - public static final short TAG_FOCAL_PLANE_X_RESOLUTION = (short) 0xA20E; - public static final short TAG_FOCAL_PLANE_Y_RESOLUTION = (short) 0xA20F; - public static final short TAG_FOCAL_PLANE_RESOLUTION_UNIT = (short) 0xA210; - public static final short TAG_SUBJECT_LOCATION = (short) 0xA214; - public static final short TAG_EXPOSURE_INDEX = (short) 0xA215; - public static final short TAG_SENSING_METHOD = (short) 0xA217; - public static final short TAG_FILE_SOURCE = (short) 0xA300; - public static final short TAG_SCENE_TYPE = (short) 0xA301; - public static final short TAG_CFA_PATTERN = (short) 0xA302; - public static final short TAG_CUSTOM_RENDERED = (short) 0xA401; - public static final short TAG_EXPOSURE_MODE = (short) 0xA402; - public static final short TAG_WHITE_BALANCE = (short) 0xA403; - public static final short TAG_DIGITAL_ZOOM_RATIO = (short) 0xA404; - public static final short TAG_FOCAL_LENGTH_IN_35_MM_FILE = (short) 0xA405; - public static final short TAG_SCENE_CAPTURE_TYPE = (short) 0xA406; - public static final short TAG_GAIN_CONTROL = (short) 0xA407; - public static final short TAG_CONTRAST = (short) 0xA408; - public static final short TAG_SATURATION = (short) 0xA409; - public static final short TAG_SHARPNESS = (short) 0xA40A; - public static final short TAG_DEVICE_SETTING_DESCRIPTION = (short) 0xA40B; - public static final short TAG_SUBJECT_DISTANCE_RANGE = (short) 0xA40C; - public static final short TAG_IMAGE_UNIQUE_ID = (short) 0xA420; - - // GPS tags - public static final short TAG_GPS_VERSION_ID = 0; - public static final short TAG_GPS_LATITUDE_REF = 1; - public static final short TAG_GPS_LATITUDE = 2; - public static final short TAG_GPS_LONGITUDE_REF = 3; - public static final short TAG_GPS_LONGITUDE = 4; - public static final short TAG_GPS_ALTITUDE_REF = 5; - public static final short TAG_GPS_ALTITUDE = 6; - public static final short TAG_GPS_TIME_STAMP = 7; - public static final short TAG_GPS_SATTELLITES = 8; - public static final short TAG_GPS_STATUS = 9; - public static final short TAG_GPS_MEASURE_MODE = 10; - public static final short TAG_GPS_DOP = 11; - public static final short TAG_GPS_SPEED_REF = 12; - public static final short TAG_GPS_SPEED = 13; - public static final short TAG_GPS_TRACK_REF = 14; - public static final short TAG_GPS_TRACK = 15; - public static final short TAG_GPS_IMG_DIRECTION_REF = 16; - public static final short TAG_GPS_IMG_DIRECTION = 17; - public static final short TAG_GPS_MAP_DATUM = 18; - public static final short TAG_GPS_DEST_LATITUDE_REF = 19; - public static final short TAG_GPS_DEST_LATITUDE = 20; - public static final short TAG_GPS_DEST_LONGITUDE_REF = 21; - public static final short TAG_GPS_DEST_LONGITUDE = 22; - public static final short TAG_GPS_DEST_BEARING_REF = 23; - public static final short TAG_GPS_DEST_BEARING = 24; - public static final short TAG_GPS_DEST_DISTANCE_REF = 25; - public static final short TAG_GPS_DEST_DISTANCE = 26; - public static final short TAG_GPS_PROCESSING_METHOD = 27; - public static final short TAG_GPS_AREA_INFORMATION = 28; - public static final short TAG_GPS_DATA_STAMP = 29; - public static final short TAG_GPS_DIFFERENTIAL = 30; - - // Interoperability tag - public static final short TAG_INTEROPERABILITY_INDEX = 1; - - /** - * Constants for {@link #TAG_ORIENTATION} - */ - public static interface Orientation { - public static final short TOP_LEFT = 1; - public static final short TOP_RIGHT = 2; - public static final short BOTTOM_LEFT = 3; - public static final short BOTTOM_RIGHT = 4; - public static final short LEFT_TOP = 5; - public static final short RIGHT_TOP = 6; - public static final short LEFT_BOTTOM = 7; - public static final short RIGHT_BOTTOM = 8; - } - - /** - * Constants for {@link #TAG_Y_CB_CR_POSITIONING} - */ - public static interface YCbCrPositioning { - public static final short CENTERED = 1; - public static final short CO_SITED = 2; - } - - /** - * Constants for {@link #TAG_COMPRESSION} - */ - public static interface Compression { - public static final short UNCOMPRESSION = 1; - public static final short JPEG = 6; - } - - /** - * Constants for {@link #TAG_RESOLUTION_UNIT} - */ - public static interface ResolutionUnit { - public static final short INCHES = 2; - public static final short CENTIMETERS = 3; - } - - /** - * Constants for {@link #TAG_PHOTOMETRIC_INTERPRETATION} - */ - public static interface PhotometricInterpretation { - public static final short RGB = 2; - public static final short YCBCR = 6; - } - - /** - * Constants for {@link #TAG_PLANAR_CONFIGURATION} - */ - public static interface PlanarConfiguration { - public static final short CHUNKY = 1; - public static final short PLANAR = 2; - } - - /** - * Constants for {@link #TAG_EXPOSURE_PROGRAM} - */ - public static interface ExposureProgram { - public static final short NOT_DEFINED = 0; - public static final short MANUAL = 1; - public static final short NORMAL_PROGRAM = 2; - public static final short APERTURE_PRIORITY = 3; - public static final short SHUTTER_PRIORITY = 4; - public static final short CREATIVE_PROGRAM = 5; - public static final short ACTION_PROGRAM = 6; - public static final short PROTRAIT_MODE = 7; - public static final short LANDSCAPE_MODE = 8; - } - - /** - * Constants for {@link #TAG_METERING_MODE} - */ - public static interface MeteringMode { - public static final short UNKNOWN = 0; - public static final short AVERAGE = 1; - public static final short CENTER_WEIGHTED_AVERAGE = 2; - public static final short SPOT = 3; - public static final short MULTISPOT = 4; - public static final short PATTERN = 5; - public static final short PARTAIL = 6; - public static final short OTHER = 255; - } - - /** - * Constants for {@link #TAG_FLASH} As the definition in Jeita EXIF 2.2 standard, we can - * treat this constant as bitwise flag. - *

- * e.g. - *

- * short flash = FIRED | RETURN_STROBE_RETURN_LIGHT_DETECTED | MODE_AUTO_MODE - */ - public static interface Flash { - // LSB - public static final short DID_NOT_FIRED = 0; - public static final short FIRED = 1; - // 1st~2nd bits - public static final short RETURN_NO_STROBE_RETURN_DETECTION_FUNCTION = 0 << 1; - public static final short RETURN_STROBE_RETURN_LIGHT_NOT_DETECTED = 2 << 1; - public static final short RETURN_STROBE_RETURN_LIGHT_DETECTED = 3 << 1; - // 3rd~4th bits - public static final short MODE_UNKNOWN = 0 << 3; - public static final short MODE_COMPULSORY_FLASH_FIRING = 1 << 3; - public static final short MODE_COMPULSORY_FLASH_SUPPRESSION = 2 << 3; - public static final short MODE_AUTO_MODE = 3 << 3; - // 5th bit - public static final short FUNCTION_PRESENT = 0 << 5; - public static final short FUNCTION_NO_FUNCTION = 1 << 5; - // 6th bit - public static final short RED_EYE_REDUCTION_NO_OR_UNKNOWN = 0 << 6; - public static final short RED_EYE_REDUCTION_SUPPORT = 1 << 6; - } - - /** - * Constants for {@link #TAG_COLOR_SPACE} - */ - public static interface ColorSpace { - public static final short SRGB = 1; - public static final short UNCALIBRATED = (short) 0xFFFF; - } - - /** - * Constants for {@link #TAG_EXPOSURE_MODE} - */ - public static interface ExposureMode { - public static final short AUTO_EXPOSURE = 0; - public static final short MANUAL_EXPOSURE = 1; - public static final short AUTO_BRACKET = 2; - } - - /** - * Constants for {@link #TAG_WHITE_BALANCE} - */ - public static interface WhiteBalance { - public static final short AUTO = 0; - public static final short MANUAL = 1; - } - - /** - * Constants for {@link #TAG_SCENE_CAPTURE_TYPE} - */ - public static interface SceneCapture { - public static final short STANDARD = 0; - public static final short LANDSCAPE = 1; - public static final short PROTRAIT = 2; - public static final short NIGHT_SCENE = 3; - } - - /** - * Constants for {@link #TAG_COMPONENTS_CONFIGURATION} - */ - public static interface ComponentsConfiguration { - public static final short NOT_EXIST = 0; - public static final short Y = 1; - public static final short CB = 2; - public static final short CR = 3; - public static final short R = 4; - public static final short G = 5; - public static final short B = 6; - } - - /** - * Constants for {@link #TAG_LIGHT_SOURCE} - */ - public static interface LightSource { - public static final short UNKNOWN = 0; - public static final short DAYLIGHT = 1; - public static final short FLUORESCENT = 2; - public static final short TUNGSTEN = 3; - public static final short FLASH = 4; - public static final short FINE_WEATHER = 9; - public static final short CLOUDY_WEATHER = 10; - public static final short SHADE = 11; - public static final short DAYLIGHT_FLUORESCENT = 12; - public static final short DAY_WHITE_FLUORESCENT = 13; - public static final short COOL_WHITE_FLUORESCENT = 14; - public static final short WHITE_FLUORESCENT = 15; - public static final short STANDARD_LIGHT_A = 17; - public static final short STANDARD_LIGHT_B = 18; - public static final short STANDARD_LIGHT_C = 19; - public static final short D55 = 20; - public static final short D65 = 21; - public static final short D75 = 22; - public static final short D50 = 23; - public static final short ISO_STUDIO_TUNGSTEN = 24; - public static final short OTHER = 255; - } - - /** - * Constants for {@link #TAG_SENSING_METHOD} - */ - public static interface SensingMethod { - public static final short NOT_DEFINED = 1; - public static final short ONE_CHIP_COLOR = 2; - public static final short TWO_CHIP_COLOR = 3; - public static final short THREE_CHIP_COLOR = 4; - public static final short COLOR_SEQUENTIAL_AREA = 5; - public static final short TRILINEAR = 7; - public static final short COLOR_SEQUENTIAL_LINEAR = 8; - } - - /** - * Constants for {@link #TAG_FILE_SOURCE} - */ - public static interface FileSource { - public static final short DSC = 3; - } - - /** - * Constants for {@link #TAG_SCENE_TYPE} - */ - public static interface SceneType { - public static final short DIRECT_PHOTOGRAPHED = 1; - } - - /** - * Constants for {@link #TAG_GAIN_CONTROL} - */ - public static interface GainControl { - public static final short NONE = 0; - public static final short LOW_UP = 1; - public static final short HIGH_UP = 2; - public static final short LOW_DOWN = 3; - public static final short HIGH_DOWN = 4; - } - - /** - * Constants for {@link #TAG_CONTRAST} - */ - public static interface Contrast { - public static final short NORMAL = 0; - public static final short SOFT = 1; - public static final short HARD = 2; - } - - /** - * Constants for {@link #TAG_SATURATION} - */ - public static interface Saturation { - public static final short NORMAL = 0; - public static final short LOW = 1; - public static final short HIGH = 2; - } - - /** - * Constants for {@link #TAG_SHARPNESS} - */ - public static interface Sharpness { - public static final short NORMAL = 0; - public static final short SOFT = 1; - public static final short HARD = 2; - } - - /** - * Constants for {@link #TAG_SUBJECT_DISTANCE} - */ - public static interface SubjectDistance { - public static final short UNKNOWN = 0; - public static final short MACRO = 1; - public static final short CLOSE_VIEW = 2; - public static final short DISTANT_VIEW = 3; - } - - /** - * Constants for {@link #TAG_GPS_LATITUDE_REF}, {@link #TAG_GPS_DEST_LATITUDE_REF} - */ - public static interface GpsLatitudeRef { - public static final String NORTH = "N"; - public static final String SOUTH = "S"; - } - - /** - * Constants for {@link #TAG_GPS_LONGITUDE_REF}, {@link #TAG_GPS_DEST_LONGITUDE_REF} - */ - public static interface GpsLongitudeRef { - public static final String EAST = "E"; - public static final String WEST = "W"; - } - - /** - * Constants for {@link #TAG_GPS_ALTITUDE_REF} - */ - public static interface GpsAltitudeRef { - public static final short SEA_LEVEL = 0; - public static final short SEA_LEVEL_NEGATIVE = 1; - } - - /** - * Constants for {@link #TAG_GPS_STATUS} - */ - public static interface GpsStatus { - public static final String IN_PROGRESS = "A"; - public static final String INTEROPERABILITY = "V"; - } - - /** - * Constants for {@link #TAG_GPS_MEASURE_MODE} - */ - public static interface GpsMeasureMode { - public static final String MODE_2_DIMENSIONAL = "2"; - public static final String MODE_3_DIMENSIONAL = "3"; - } - - /** - * Constants for {@link #TAG_GPS_SPEED_REF}, {@link #TAG_GPS_DEST_DISTANCE_REF} - */ - public static interface GpsSpeedRef { - public static final String KILOMETERS = "K"; - public static final String MILES = "M"; - public static final String KNOTS = "N"; - } - - /** - * Constants for {@link #TAG_GPS_TRACK_REF}, {@link #TAG_GPS_IMG_DIRECTION_REF}, - * {@link #TAG_GPS_DEST_BEARING_REF} - */ - public static interface GpsTrackRef { - public static final String TRUE_DIRECTION = "T"; - public static final String MAGNETIC_DIRECTION = "M"; - } - - /** - * Constants for {@link #TAG_GPS_DIFFERENTIAL} - */ - public static interface GpsDifferential { - public static final short WITHOUT_DIFFERENTIAL_CORRECTION = 0; - public static final short DIFFERENTIAL_CORRECTION_APPLIED = 1; - } - /** * The BYTE type in the EXIF standard. An 8-bit unsigned integer. */ public static final short TYPE_UNSIGNED_BYTE = 1; /** - * The ASCII type in the EXIF standard. An 8-bit byte containing one 7-bit ASCII code. - * The final byte is terminated with NULL. + * The ASCII type in the EXIF standard. An 8-bit byte containing one 7-bit + * ASCII code. The final byte is terminated with NULL. */ public static final short TYPE_ASCII = 2; /** @@ -508,13 +47,13 @@ public class ExifTag { */ public static final short TYPE_UNSIGNED_LONG = 4; /** - * The RATIONAL type of EXIF standard. It consists of two LONGs. The first one is the numerator - * and the second one expresses the denominator. + * The RATIONAL type of EXIF standard. It consists of two LONGs. The first + * one is the numerator and the second one expresses the denominator. */ public static final short TYPE_UNSIGNED_RATIONAL = 5; /** - * The UNDEFINED type in the EXIF standard. An 8-bit byte that can take any value - * depending on the field definition. + * The UNDEFINED type in the EXIF standard. An 8-bit byte that can take any + * value depending on the field definition. */ public static final short TYPE_UNDEFINED = 7; /** @@ -523,12 +62,18 @@ public class ExifTag { */ public static final short TYPE_LONG = 9; /** - * The SRATIONAL type of EXIF standard. It consists of two SLONGs. The first one is the - * numerator and the second one is the denominator. + * The SRATIONAL type of EXIF standard. It consists of two SLONGs. The first + * one is the numerator and the second one is the denominator. */ public static final short TYPE_RATIONAL = 10; + private static Charset US_ASCII = Charset.forName("US-ASCII"); private static final int TYPE_TO_SIZE_MAP[] = new int[11]; + private static final int UNSIGNED_SHORT_MAX = 65535; + private static final long UNSIGNED_LONG_MAX = 4294967295L; + private static final long LONG_MAX = Integer.MAX_VALUE; + private static final long LONG_MIN = Integer.MIN_VALUE; + static { TYPE_TO_SIZE_MAP[TYPE_UNSIGNED_BYTE] = 1; TYPE_TO_SIZE_MAP[TYPE_ASCII] = 1; @@ -540,383 +85,69 @@ public class ExifTag { TYPE_TO_SIZE_MAP[TYPE_RATIONAL] = 8; } - /** - * Gets the element size of the given data type. - * - * @see #TYPE_ASCII - * @see #TYPE_LONG - * @see #TYPE_RATIONAL - * @see #TYPE_UNDEFINED - * @see #TYPE_UNSIGNED_BYTE - * @see #TYPE_UNSIGNED_LONG - * @see #TYPE_UNSIGNED_RATIONAL - * @see #TYPE_UNSIGNED_SHORT - */ - public static int getElementSize(short type) { - return TYPE_TO_SIZE_MAP[type]; - } - - private static volatile SparseArray sTagInfo = null; - private static volatile SparseArray sInteroperTagInfo = null; - private static final int SIZE_UNDEFINED = 0; - - private static SparseArray getTagInfo() { - if (sTagInfo == null) { - synchronized(ExifTag.class) { - if (sTagInfo == null) { - sTagInfo = new SparseArray(); - initTagInfo(); - } - } - } - return sTagInfo; - } - - private static SparseArray getInteroperTagInfo() { - if (sInteroperTagInfo == null) { - synchronized(ExifTag.class) { - if (sInteroperTagInfo == null) { - sInteroperTagInfo = new SparseArray(); - sInteroperTagInfo.put(TAG_INTEROPERABILITY_INDEX, - (IfdId.TYPE_IFD_INTEROPERABILITY << 24) - | TYPE_ASCII << 16 | SIZE_UNDEFINED); - } - } - } - return sInteroperTagInfo; - } - - private static void initTagInfo() { - /** - * We put tag information in a 4-bytes integer. The first byte is the - * IFD of the tag, and the second byte is the default data type. The - * last two byte are a short value indicating the component count of this - * tag. - */ - sTagInfo.put(TAG_MAKE, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_IMAGE_WIDTH, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_IMAGE_LENGTH, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_BITS_PER_SAMPLE, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 3); - sTagInfo.put(TAG_COMPRESSION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_PHOTOMETRIC_INTERPRETATION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_ORIENTATION, (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SAMPLES_PER_PIXEL, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_PLANAR_CONFIGURATION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_Y_CB_CR_SUB_SAMPLING, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 2); - sTagInfo.put(TAG_Y_CB_CR_POSITIONING, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_X_RESOLUTION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_Y_RESOLUTION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_RESOLUTION_UNIT, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_STRIP_OFFSETS, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_ROWS_PER_STRIP, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_STRIP_BYTE_COUNTS, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_JPEG_INTERCHANGE_FORMAT, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_TRANSFER_FUNCTION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_SHORT << 16 | 3 * 256); - sTagInfo.put(TAG_WHITE_POINT, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 2); - sTagInfo.put(TAG_PRIMARY_CHROMATICITIES, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 6); - sTagInfo.put(TAG_Y_CB_CR_COEFFICIENTS, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 3); - sTagInfo.put(TAG_REFERENCE_BLACK_WHITE, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 6); - sTagInfo.put(TAG_DATE_TIME, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | 20); - sTagInfo.put(TAG_IMAGE_DESCRIPTION, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_MAKE, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_MODEL, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_SOFTWARE, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_ARTIST, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_COPYRIGHT, - (IfdId.TYPE_IFD_0 << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_EXIF_IFD, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_GPS_IFD, - (IfdId.TYPE_IFD_0 << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - - // EXIF TAG - sTagInfo.put(TAG_EXIF_VERSION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | 4); - sTagInfo.put(TAG_FLASHPIX_VERSION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | 4); - sTagInfo.put(TAG_COLOR_SPACE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_COMPONENTS_CONFIGURATION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | 4); - sTagInfo.put(TAG_COMPRESSED_BITS_PER_PIXEL, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_PIXEL_X_DIMENSION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_PIXEL_Y_DIMENSION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_LONG << 16 | 1); - sTagInfo.put(TAG_MAKER_NOTE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_USER_COMMENT, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_RELATED_SOUND_FILE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | 13); - sTagInfo.put(TAG_DATE_TIME_ORIGINAL, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | 20); - sTagInfo.put(TAG_DATE_TIME_DIGITIZED, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | 20); - sTagInfo.put(TAG_SUB_SEC_TIME, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_SUB_SEC_TIME_ORIGINAL, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_SUB_SEC_TIME_DIGITIZED, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_IMAGE_UNIQUE_ID, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | 33); - sTagInfo.put(TAG_EXPOSURE_TIME, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_F_NUMBER, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_EXPOSURE_PROGRAM, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SPECTRAL_SENSITIVITY, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_ISO_SPEED_RATINGS, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_OECF, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_SHUTTER_SPEED_VALUE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_RATIONAL << 16 | 1); - sTagInfo.put(TAG_APERTURE_VALUE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_BRIGHTNESS_VALUE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_RATIONAL << 16 | 1); - sTagInfo.put(TAG_EXPOSURE_BIAS_VALUE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_RATIONAL << 16 | 1); - sTagInfo.put(TAG_MAX_APERTURE_VALUE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_SUBJECT_DISTANCE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_METERING_MODE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_LIGHT_SOURCE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_FLASH, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_FOCAL_LENGTH, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_SUBJECT_AREA, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_FLASH_ENERGY, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_SPATIAL_FREQUENCY_RESPONSE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_FOCAL_PLANE_X_RESOLUTION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_FOCAL_PLANE_Y_RESOLUTION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_FOCAL_PLANE_RESOLUTION_UNIT, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SUBJECT_LOCATION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 2); - sTagInfo.put(TAG_EXPOSURE_INDEX, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_SENSING_METHOD, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_FILE_SOURCE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | 1); - sTagInfo.put(TAG_SCENE_TYPE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | 1); - sTagInfo.put(TAG_CFA_PATTERN, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_CUSTOM_RENDERED, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_EXPOSURE_MODE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_WHITE_BALANCE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_DIGITAL_ZOOM_RATIO, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_FOCAL_LENGTH_IN_35_MM_FILE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SCENE_CAPTURE_TYPE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_GAIN_CONTROL, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_CONTRAST, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SATURATION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_SHARPNESS, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - sTagInfo.put(TAG_DEVICE_SETTING_DESCRIPTION, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_SUBJECT_DISTANCE_RANGE, - (IfdId.TYPE_IFD_EXIF << 24) | TYPE_UNSIGNED_SHORT << 16 | 1); - // GPS tag - sTagInfo.put(TAG_GPS_VERSION_ID, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_BYTE << 16 | 4); - sTagInfo.put(TAG_GPS_LATITUDE_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_LONGITUDE_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_LATITUDE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_RATIONAL << 16 | 3); - sTagInfo.put(TAG_GPS_LONGITUDE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_RATIONAL << 16 | 3); - sTagInfo.put(TAG_GPS_ALTITUDE_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_BYTE << 16 | 1); - sTagInfo.put(TAG_GPS_ALTITUDE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_TIME_STAMP, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 3); - sTagInfo.put(TAG_GPS_SATTELLITES, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_GPS_STATUS, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_MEASURE_MODE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_DOP, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_SPEED_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_SPEED, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_TRACK_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_TRACK, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_IMG_DIRECTION_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_IMG_DIRECTION, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_MAP_DATUM, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_GPS_DEST_LATITUDE_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_DEST_LATITUDE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_DEST_BEARING_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_DEST_BEARING, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_DEST_DISTANCE_REF, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 2); - sTagInfo.put(TAG_GPS_DEST_DISTANCE, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_RATIONAL << 16 | 1); - sTagInfo.put(TAG_GPS_PROCESSING_METHOD, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_GPS_AREA_INFORMATION, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNDEFINED << 16 | SIZE_UNDEFINED); - sTagInfo.put(TAG_GPS_DATA_STAMP, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_ASCII << 16 | 11); - sTagInfo.put(TAG_GPS_DIFFERENTIAL, - (IfdId.TYPE_IFD_GPS << 24) | TYPE_UNSIGNED_SHORT << 16 | 11); - } + static final int SIZE_UNDEFINED = 0; + // Exif TagId private final short mTagId; + // Exif Tag Type private final short mDataType; - private final int mIfd; - private final boolean mComponentCountDefined; - private int mComponentCount; + // If tag has defined count + private boolean mHasDefinedDefaultComponentCount; + // Actual data count in tag (should be number of elements in value array) + private int mComponentCountActual; + // The ifd that this tag should be put in + private int mIfd; + // The value (array of elements of type Tag Type) private Object mValue; + // Value offset in exif header. private int mOffset; - static private short getTypeFromInfo(int info) { - return (short) ((info >> 16) & 0xff); - } - - static private int getComponentCountFromInfo(int info) { - return info & 0xffff; - } - - static private int getIfdIdFromInfo(int info) { - return (info >> 24) & 0xff; - } - - static private boolean getComponentCountDefined(short tagId, int ifd) { - Integer info = (ifd == IfdId.TYPE_IFD_INTEROPERABILITY) ? - getInteroperTagInfo().get(tagId) : getTagInfo().get(tagId); - if (info == null) return false; - return getComponentCountFromInfo(info) != SIZE_UNDEFINED; - } - - static int getIfdIdFromTagId(short tagId) { - Integer info = getTagInfo().get(tagId); - if (info == null) { - throw new IllegalArgumentException("Unknown Tag ID: " + tagId); - } - return getIfdIdFromInfo(info); - } - - /** - * Create a tag with given ID. For tags related to interoperability and thumbnail, call - * {@link #buildInteroperabilityTag(short)} and {@link #buildThumbnailTag(short)} respectively. - * @exception IllegalArgumentException If the ID is invalid. - */ - static public ExifTag buildTag(short tagId) { - Integer info = getTagInfo().get(tagId); - if (info == null) { - throw new IllegalArgumentException("Unknown Tag ID: " + tagId); - } - return new ExifTag(tagId, getTypeFromInfo(info), - getComponentCountFromInfo(info), - getIfdIdFromInfo(info)); - } + private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy:MM:dd kk:mm:ss"); /** - * Create a tag related to thumbnail with given ID. - * @exception IllegalArgumentException If the ID is invalid. + * Returns true if the given IFD is a valid IFD. */ - static public ExifTag buildThumbnailTag(short tagId) { - Integer info = getTagInfo().get(tagId); - if (info == null || getIfdIdFromInfo(info) != IfdId.TYPE_IFD_0) { - throw new IllegalArgumentException("Unknown Thumnail Tag ID: " + tagId); - } - return new ExifTag(tagId, getTypeFromInfo(info), - getComponentCountFromInfo(info), - IfdId.TYPE_IFD_1); + public static boolean isValidIfd(int ifdId) { + return ifdId == IfdId.TYPE_IFD_0 || ifdId == IfdId.TYPE_IFD_1 + || ifdId == IfdId.TYPE_IFD_EXIF || ifdId == IfdId.TYPE_IFD_INTEROPERABILITY + || ifdId == IfdId.TYPE_IFD_GPS; } /** - * Create a tag related to interoperability with given ID. - * @exception IllegalArgumentException If the ID is invalid. + * Returns true if a given type is a valid tag type. */ - static public ExifTag buildInteroperabilityTag(short tagId) { - Integer info = getInteroperTagInfo().get(tagId); - if (info == null || getIfdIdFromInfo(info) != IfdId.TYPE_IFD_INTEROPERABILITY) { - throw new RuntimeException("Unknown Interoperability Tag ID: " + tagId); - } - return new ExifTag(tagId, getTypeFromInfo(info), - getComponentCountFromInfo(info), - IfdId.TYPE_IFD_INTEROPERABILITY); + public static boolean isValidType(short type) { + return type == TYPE_UNSIGNED_BYTE || type == TYPE_ASCII || + type == TYPE_UNSIGNED_SHORT || type == TYPE_UNSIGNED_LONG || + type == TYPE_UNSIGNED_RATIONAL || type == TYPE_UNDEFINED || + type == TYPE_LONG || type == TYPE_RATIONAL; } - ExifTag(short tagId, short type, int componentCount, int ifd) { + // Use builtTag in ExifInterface instead of constructor. + ExifTag(short tagId, short type, int componentCount, int ifd, + boolean hasDefinedComponentCount) { mTagId = tagId; mDataType = type; - mComponentCount = componentCount; - mComponentCountDefined = getComponentCountDefined(tagId, ifd); + mComponentCountActual = componentCount; + mHasDefinedDefaultComponentCount = hasDefinedComponentCount; mIfd = ifd; + mValue = null; + } + + /** + * Gets the element size of the given data type in bytes. + * + * @see #TYPE_ASCII + * @see #TYPE_LONG + * @see #TYPE_RATIONAL + * @see #TYPE_UNDEFINED + * @see #TYPE_UNSIGNED_BYTE + * @see #TYPE_UNSIGNED_LONG + * @see #TYPE_UNSIGNED_RATIONAL + * @see #TYPE_UNSIGNED_SHORT + */ + public static int getElementSize(short type) { + return TYPE_TO_SIZE_MAP[type]; } /** @@ -932,8 +163,12 @@ public class ExifTag { return mIfd; } + protected void setIfd(int ifdId) { + mIfd = ifdId; + } + /** - * Gets the ID of this tag. + * Gets the TID of this tag. */ public short getTagId() { return mTagId; @@ -965,153 +200,51 @@ public class ExifTag { /** * Gets the component count of this tag. */ - public int getComponentCount() { - return mComponentCount; - } - /** - * Returns true if this ExifTag contains value; otherwise, this tag will contain an offset value - * that links to the area where the actual value is located. - * - * @see #getOffset() - */ - public boolean hasValue() { - return mValue != null; + // TODO: fix integer overflows with this + public int getComponentCount() { + return mComponentCountActual; } /** - * Gets the offset of this tag. This is only valid if this data size > 4 and contains an offset - * to the location of the actual value. + * Sets the component count of this tag. Call this function before + * setValue() if the length of value does not match the component count. */ - public int getOffset() { - return mOffset; + protected void forceSetComponentCount(int count) { + mComponentCountActual = count; } /** - * Sets the offset of this tag. + * Returns true if this ExifTag contains value; otherwise, this tag will + * contain an offset value that is determined when the tag is written. */ - void setOffset(int offset) { - mOffset = offset; - } - - private void checkComponentCountOrThrow(int count) - throws IllegalArgumentException { - if (mComponentCountDefined && (mComponentCount != count)) { - throw new IllegalArgumentException("Tag " + mTagId + ": Required " - + mComponentCount + " components but was given " + count - + " component(s)"); - } - } - - private void throwTypeNotMatchedException(String className) - throws IllegalArgumentException { - throw new IllegalArgumentException("Tag " + mTagId + ": expect type " + - convertTypeToString(mDataType) + " but got " + className); - } - - private static String convertTypeToString(short type) { - switch (type) { - case TYPE_UNSIGNED_BYTE: - return "UNSIGNED_BYTE"; - case TYPE_ASCII: - return "ASCII"; - case TYPE_UNSIGNED_SHORT: - return "UNSIGNED_SHORT"; - case TYPE_UNSIGNED_LONG: - return "UNSIGNED_LONG"; - case TYPE_UNSIGNED_RATIONAL: - return "UNSIGNED_RATIONAL"; - case TYPE_UNDEFINED: - return "UNDEFINED"; - case TYPE_LONG: - return "LONG"; - case TYPE_RATIONAL: - return "RATIONAL"; - default: - return ""; - } - } - - private static final int UNSIGNED_SHORT_MAX = 65535; - private static final long UNSIGNED_LONG_MAX = 4294967295L; - private static final long LONG_MAX = Integer.MAX_VALUE; - private static final long LONG_MIN = Integer.MIN_VALUE; - - private void checkOverflowForUnsignedShort(int[] value) { - for (int v : value) { - if (v > UNSIGNED_SHORT_MAX || v < 0) { - throw new IllegalArgumentException( - "Tag " + mTagId+ ": Value" + v + - " is illegal for type UNSIGNED_SHORT"); - } - } - } - - private void checkOverflowForUnsignedLong(long[] value) { - for (long v: value) { - if (v < 0 || v > UNSIGNED_LONG_MAX) { - throw new IllegalArgumentException( - "Tag " + mTagId+ ": Value" + v + - " is illegal for type UNSIGNED_LONG"); - } - } - } - - private void checkOverflowForUnsignedLong(int[] value) { - for (int v: value) { - if (v < 0) { - throw new IllegalArgumentException( - "Tag " + mTagId+ ": Value" + v + - " is illegal for type UNSIGNED_LONG"); - } - } - } - - private void checkOverflowForUnsignedRational(Rational[] value) { - for (Rational v: value) { - if (v.getNominator() < 0 || v.getDenominator() < 0 - || v.getNominator() > UNSIGNED_LONG_MAX - || v.getDenominator() > UNSIGNED_LONG_MAX) { - throw new IllegalArgumentException( - "Tag " + mTagId+ ": Value" + v + - " is illegal for type UNSIGNED_RATIONAL"); - } - } - } - - private void checkOverflowForRational(Rational[] value) { - for (Rational v: value) { - if (v.getNominator() < LONG_MIN || v.getDenominator() < LONG_MIN - || v.getNominator() > LONG_MAX - || v.getDenominator() > LONG_MAX) { - throw new IllegalArgumentException( - "Tag " + mTagId+ ": Value" + v + - " is illegal for type RATIONAL"); - } - } + public boolean hasValue() { + return mValue != null; } /** - * Sets integer values into this tag. - * @exception IllegalArgumentException for the following situation: + * Sets integer values into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_SHORT}. This method will fail if: *

    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT}, - * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.
  • - *
  • The value overflows.
  • - *
  • The value.length does NOT match the definition of component count in - * EXIF standard.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT}, + * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.
  • + *
  • The value overflows.
  • + *
  • The value.length does NOT match the component count in the definition + * for this tag.
  • *
*/ - public void setValue(int[] value) { - checkComponentCountOrThrow(value.length); + public boolean setValue(int[] value) { + if (checkBadComponentCount(value.length)) { + return false; + } if (mDataType != TYPE_UNSIGNED_SHORT && mDataType != TYPE_LONG && mDataType != TYPE_UNSIGNED_LONG) { - throwTypeNotMatchedException("int"); + return false; } - if (mDataType == TYPE_UNSIGNED_SHORT) { - checkOverflowForUnsignedShort(value); - } else if (mDataType == TYPE_UNSIGNED_LONG) { - checkOverflowForUnsignedLong(value); + if (mDataType == TYPE_UNSIGNED_SHORT && checkOverflowForUnsignedShort(value)) { + return false; + } else if (mDataType == TYPE_UNSIGNED_LONG && checkOverflowForUnsignedLong(value)) { + return false; } long[] data = new long[value.length]; @@ -1119,218 +252,588 @@ public class ExifTag { data[i] = value[i]; } mValue = data; - mComponentCount = value.length; + mComponentCountActual = value.length; + return true; } /** - * Sets integer values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets integer value into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_SHORT}, or {@link #TYPE_LONG}. This method + * will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT}, - * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.
  • - *
  • The value overflows.
  • - *
  • The component count in the definition of EXIF standard is not 1.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT}, + * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.
  • + *
  • The value overflows.
  • + *
  • The component count in the definition of this tag is not 1.
  • *
*/ - public void setValue(int value) { - checkComponentCountOrThrow(1); - setValue(new int[] {value}); + public boolean setValue(int value) { + return setValue(new int[] { + value + }); } /** - * Sets long values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets long values into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_LONG}. This method will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.
  • - *
  • The value overflows.
  • - *
  • The value.length does NOT match the definition of component count in - * EXIF standard.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.
  • + *
  • The value overflows.
  • + *
  • The value.length does NOT match the component count in the definition + * for this tag.
  • *
*/ - public void setValue(long[] value) { - checkComponentCountOrThrow(value.length); - if (mDataType != TYPE_UNSIGNED_LONG) { - throwTypeNotMatchedException("long"); + public boolean setValue(long[] value) { + if (checkBadComponentCount(value.length) || mDataType != TYPE_UNSIGNED_LONG) { + return false; + } + if (checkOverflowForUnsignedLong(value)) { + return false; } - checkOverflowForUnsignedLong(value); mValue = value; - mComponentCount = value.length; + mComponentCountActual = value.length; + return true; } /** - * Sets long values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets long values into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_LONG}. This method will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.
  • - *
  • The value overflows.
  • - *
  • The component count in the definition of EXIF standard is not 1.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.
  • + *
  • The value overflows.
  • + *
  • The component count in the definition for this tag is not 1.
  • *
*/ - public void setValue(long value) { - setValue(new long[] {value}); + public boolean setValue(long value) { + return setValue(new long[] { + value + }); } /** - * Sets string values into this tag. - * @exception IllegalArgumentException If the data type is not {@link #TYPE_ASCII} - * or value.length() + 1 does NOT fit the definition of the component count in the - * EXIF standard. + * Sets a string value into this tag. This method should be used for tags of + * type {@link #TYPE_ASCII}. The string is converted to an ASCII string. + * Characters that cannot be converted are replaced with '?'. The length of + * the string must be equal to either (component count -1) or (component + * count). The final byte will be set to the string null terminator '\0', + * overwriting the last character in the string if the value.length is equal + * to the component count. This method will fail if: + *
    + *
  • The data type is not {@link #TYPE_ASCII} or {@link #TYPE_UNDEFINED}.
  • + *
  • The length of the string is not equal to (component count -1) or + * (component count) in the definition for this tag.
  • + *
*/ - public void setValue(String value) { - checkComponentCountOrThrow(value.length() + 1); - if (mDataType != TYPE_ASCII) { - throwTypeNotMatchedException("String"); + public boolean setValue(String value) { + if (mDataType != TYPE_ASCII && mDataType != TYPE_UNDEFINED) { + return false; } - mComponentCount = value.length() + 1; - mValue = value; + + byte[] buf = value.getBytes(US_ASCII); + byte[] finalBuf = buf; + if (buf.length > 0) { + finalBuf = (buf[buf.length - 1] == 0 || mDataType == TYPE_UNDEFINED) ? buf : Arrays + .copyOf(buf, buf.length + 1); + } else if (mDataType == TYPE_ASCII && mComponentCountActual == 1) { + finalBuf = new byte[] { 0 }; + } + int count = finalBuf.length; + if (checkBadComponentCount(count)) { + return false; + } + mComponentCountActual = count; + mValue = finalBuf; + return true; } /** - * Sets Rational values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets Rational values into this tag. This method should be used for tags + * of type {@link #TYPE_UNSIGNED_RATIONAL}, or {@link #TYPE_RATIONAL}. This + * method will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL} or - * {@link #TYPE_RATIONAL} .
  • - *
  • The value overflows.
  • - *
  • The value.length does NOT match the definition of component count in - * EXIF standard.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL} + * or {@link #TYPE_RATIONAL}.
  • + *
  • The value overflows.
  • + *
  • The value.length does NOT match the component count in the definition + * for this tag.
  • *
+ * + * @see Rational */ - public void setValue(Rational[] value) { - if (mDataType == TYPE_UNSIGNED_RATIONAL) { - checkOverflowForUnsignedRational(value); - } else if (mDataType == TYPE_RATIONAL) { - checkOverflowForRational(value); - } else { - throwTypeNotMatchedException("Rational"); + public boolean setValue(Rational[] value) { + if (checkBadComponentCount(value.length)) { + return false; + } + if (mDataType != TYPE_UNSIGNED_RATIONAL && mDataType != TYPE_RATIONAL) { + return false; } - checkComponentCountOrThrow(value.length); + if (mDataType == TYPE_UNSIGNED_RATIONAL && checkOverflowForUnsignedRational(value)) { + return false; + } else if (mDataType == TYPE_RATIONAL && checkOverflowForRational(value)) { + return false; + } + mValue = value; - mComponentCount = value.length; + mComponentCountActual = value.length; + return true; } /** - * Sets Rational values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets a Rational value into this tag. This method should be used for tags + * of type {@link #TYPE_UNSIGNED_RATIONAL}, or {@link #TYPE_RATIONAL}. This + * method will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL} or - * {@link #TYPE_RATIONAL} .
  • - *
  • The value overflows.
  • - *
  • The component count in the definition of EXIF standard is not 1.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL} + * or {@link #TYPE_RATIONAL}.
  • + *
  • The value overflows.
  • + *
  • The component count in the definition for this tag is not 1.
  • *
- * */ - public void setValue(Rational value) { - setValue(new Rational[] {value}); + * + * @see Rational + */ + public boolean setValue(Rational value) { + return setValue(new Rational[] { + value + }); } /** - * Sets byte values into this tag. - * @exception IllegalArgumentException For the following situation: + * Sets byte values into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_BYTE} or {@link #TYPE_UNDEFINED}. This method + * will fail if: *
    - *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_BYTE} or - * {@link #TYPE_UNDEFINED} .
  • - *
  • The length does NOT match the definition of component count in EXIF standard.
  • + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_BYTE} or + * {@link #TYPE_UNDEFINED} .
  • + *
  • The length does NOT match the component count in the definition for + * this tag.
  • *
- * */ - public void setValue(byte[] value, int offset, int length) { - checkComponentCountOrThrow(length); + */ + public boolean setValue(byte[] value, int offset, int length) { + if (checkBadComponentCount(length)) { + return false; + } if (mDataType != TYPE_UNSIGNED_BYTE && mDataType != TYPE_UNDEFINED) { - throwTypeNotMatchedException("byte"); + return false; } mValue = new byte[length]; System.arraycopy(value, offset, mValue, 0, length); - mComponentCount = length; + mComponentCountActual = length; + return true; } /** * Equivalent to setValue(value, 0, value.length). */ - public void setValue(byte[] value) { - setValue(value, 0, value.length); + public boolean setValue(byte[] value) { + return setValue(value, 0, value.length); } - private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy:MM:dd kk:mm:ss"); + /** + * Sets byte value into this tag. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_BYTE} or {@link #TYPE_UNDEFINED}. This method + * will fail if: + *
    + *
  • The component type of this tag is not {@link #TYPE_UNSIGNED_BYTE} or + * {@link #TYPE_UNDEFINED} .
  • + *
  • The component count in the definition for this tag is not 1.
  • + *
+ */ + public boolean setValue(byte value) { + return setValue(new byte[] { + value + }); + } /** - * Sets a timestamp to this tag. The method converts the timestamp with the format of - * "yyyy:MM:dd kk:mm:ss" and calls {@link #setValue(String)}. + * Sets the value for this tag using an appropriate setValue method for the + * given object. This method will fail if: + *
    + *
  • The corresponding setValue method for the class of the object passed + * in would fail.
  • + *
  • There is no obvious way to cast the object passed in into an EXIF tag + * type.
  • + *
+ */ + public boolean setValue(Object obj) { + if (obj == null) { + return false; + } else if (obj instanceof Short) { + return setValue(((Short) obj).shortValue() & 0x0ffff); + } else if (obj instanceof String) { + return setValue((String) obj); + } else if (obj instanceof int[]) { + return setValue((int[]) obj); + } else if (obj instanceof long[]) { + return setValue((long[]) obj); + } else if (obj instanceof Rational) { + return setValue((Rational) obj); + } else if (obj instanceof Rational[]) { + return setValue((Rational[]) obj); + } else if (obj instanceof byte[]) { + return setValue((byte[]) obj); + } else if (obj instanceof Integer) { + return setValue(((Integer) obj).intValue()); + } else if (obj instanceof Long) { + return setValue(((Long) obj).longValue()); + } else if (obj instanceof Byte) { + return setValue(((Byte) obj).byteValue()); + } else if (obj instanceof Short[]) { + // Nulls in this array are treated as zeroes. + Short[] arr = (Short[]) obj; + int[] fin = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + fin[i] = (arr[i] == null) ? 0 : arr[i].shortValue() & 0x0ffff; + } + return setValue(fin); + } else if (obj instanceof Integer[]) { + // Nulls in this array are treated as zeroes. + Integer[] arr = (Integer[]) obj; + int[] fin = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + fin[i] = (arr[i] == null) ? 0 : arr[i].intValue(); + } + return setValue(fin); + } else if (obj instanceof Long[]) { + // Nulls in this array are treated as zeroes. + Long[] arr = (Long[]) obj; + long[] fin = new long[arr.length]; + for (int i = 0; i < arr.length; i++) { + fin[i] = (arr[i] == null) ? 0 : arr[i].longValue(); + } + return setValue(fin); + } else if (obj instanceof Byte[]) { + // Nulls in this array are treated as zeroes. + Byte[] arr = (Byte[]) obj; + byte[] fin = new byte[arr.length]; + for (int i = 0; i < arr.length; i++) { + fin[i] = (arr[i] == null) ? 0 : arr[i].byteValue(); + } + return setValue(fin); + } else { + return false; + } + } + + /** + * Sets a timestamp to this tag. The method converts the timestamp with the + * format of "yyyy:MM:dd kk:mm:ss" and calls {@link #setValue(String)}. This + * method will fail if the data type is not {@link #TYPE_ASCII} or the + * component count of this tag is not 20 or undefined. * * @param time the number of milliseconds since Jan. 1, 1970 GMT - * @exception IllegalArgumentException If the data type is not {@link #TYPE_ASCII} - * or the component count of this tag is not 20 or undefined + * @return true on success */ - public void setTimeValue(long time) { + public boolean setTimeValue(long time) { // synchronized on TIME_FORMAT as SimpleDateFormat is not thread safe synchronized (TIME_FORMAT) { - setValue(TIME_FORMAT.format(new Date(time))); + return setValue(TIME_FORMAT.format(new Date(time))); } } /** - * Gets the {@link #TYPE_UNSIGNED_SHORT} data. - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_UNSIGNED_SHORT}. + * Gets the value as a String. This method should be used for tags of type + * {@link #TYPE_ASCII}. + * + * @return the value as a String, or null if the tag's value does not exist + * or cannot be converted to a String. + */ + public String getValueAsString() { + if (mValue == null) { + return null; + } else if (mValue instanceof String) { + return (String) mValue; + } else if (mValue instanceof byte[]) { + return new String((byte[]) mValue, US_ASCII); + } + return null; + } + + /** + * Gets the value as a String. This method should be used for tags of type + * {@link #TYPE_ASCII}. + * + * @param defaultValue the String to return if the tag's value does not + * exist or cannot be converted to a String. + * @return the tag's value as a String, or the defaultValue. + */ + public String getValueAsString(String defaultValue) { + String s = getValueAsString(); + if (s == null) { + return defaultValue; + } + return s; + } + + /** + * Gets the value as a byte array. This method should be used for tags of + * type {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}. + * + * @return the value as a byte array, or null if the tag's value does not + * exist or cannot be converted to a byte array. */ - public int getUnsignedShort(int index) { - if (mDataType != TYPE_UNSIGNED_SHORT) { - throw new IllegalArgumentException("Cannot get UNSIGNED_SHORT value from " - + convertTypeToString(mDataType)); + public byte[] getValueAsBytes() { + if (mValue instanceof byte[]) { + return (byte[]) mValue; + } + return null; + } + + /** + * Gets the value as a byte. If there are more than 1 bytes in this value, + * gets the first byte. This method should be used for tags of type + * {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}. + * + * @param defaultValue the byte to return if tag's value does not exist or + * cannot be converted to a byte. + * @return the tag's value as a byte, or the defaultValue. + */ + public byte getValueAsByte(byte defaultValue) { + byte[] b = getValueAsBytes(); + if (b == null || b.length < 1) { + return defaultValue; } - return (int) (((long[]) mValue) [index]); + return b[0]; } /** - * Gets the {@link #TYPE_LONG} data. - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_LONG}. + * Gets the value as an array of Rationals. This method should be used for + * tags of type {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}. + * + * @return the value as as an array of Rationals, or null if the tag's value + * does not exist or cannot be converted to an array of Rationals. */ - public int getLong(int index) { - if (mDataType != TYPE_LONG) { - throw new IllegalArgumentException("Cannot get LONG value from " - + convertTypeToString(mDataType)); + public Rational[] getValueAsRationals() { + if (mValue instanceof Rational[]) { + return (Rational[]) mValue; + } + return null; + } + + /** + * Gets the value as a Rational. If there are more than 1 Rationals in this + * value, gets the first one. This method should be used for tags of type + * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}. + * + * @param defaultValue the Rational to return if tag's value does not exist + * or cannot be converted to a Rational. + * @return the tag's value as a Rational, or the defaultValue. + */ + public Rational getValueAsRational(Rational defaultValue) { + Rational[] r = getValueAsRationals(); + if (r == null || r.length < 1) { + return defaultValue; } - return (int) (((long[]) mValue) [index]); + return r[0]; } /** - * Gets the {@link #TYPE_UNSIGNED_LONG} data. - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_UNSIGNED_LONG}. + * Gets the value as a Rational. If there are more than 1 Rationals in this + * value, gets the first one. This method should be used for tags of type + * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}. + * + * @param defaultValue the numerator of the Rational to return if tag's + * value does not exist or cannot be converted to a Rational (the + * denominator will be 1). + * @return the tag's value as a Rational, or the defaultValue. */ - public long getUnsignedLong(int index) { - if (mDataType != TYPE_UNSIGNED_LONG) { - throw new IllegalArgumentException("Cannot get UNSIGNED LONG value from " - + convertTypeToString(mDataType)); + public Rational getValueAsRational(long defaultValue) { + Rational defaultVal = new Rational(defaultValue, 1); + return getValueAsRational(defaultVal); + } + + /** + * Gets the value as an array of ints. This method should be used for tags + * of type {@link #TYPE_UNSIGNED_SHORT}, {@link #TYPE_UNSIGNED_LONG}. + * + * @return the value as as an array of ints, or null if the tag's value does + * not exist or cannot be converted to an array of ints. + */ + public int[] getValueAsInts() { + if (mValue == null) { + return null; + } else if (mValue instanceof long[]) { + long[] val = (long[]) mValue; + int[] arr = new int[val.length]; + for (int i = 0; i < val.length; i++) { + arr[i] = (int) val[i]; // Truncates + } + return arr; } - return ((long[]) mValue) [index]; + return null; + } + + /** + * Gets the value as an int. If there are more than 1 ints in this value, + * gets the first one. This method should be used for tags of type + * {@link #TYPE_UNSIGNED_SHORT}, {@link #TYPE_UNSIGNED_LONG}. + * + * @param defaultValue the int to return if tag's value does not exist or + * cannot be converted to an int. + * @return the tag's value as a int, or the defaultValue. + */ + public int getValueAsInt(int defaultValue) { + int[] i = getValueAsInts(); + if (i == null || i.length < 1) { + return defaultValue; + } + return i[0]; + } + + /** + * Gets the value as an array of longs. This method should be used for tags + * of type {@link #TYPE_UNSIGNED_LONG}. + * + * @return the value as as an array of longs, or null if the tag's value + * does not exist or cannot be converted to an array of longs. + */ + public long[] getValueAsLongs() { + if (mValue instanceof long[]) { + return (long[]) mValue; + } + return null; + } + + /** + * Gets the value or null if none exists. If there are more than 1 longs in + * this value, gets the first one. This method should be used for tags of + * type {@link #TYPE_UNSIGNED_LONG}. + * + * @param defaultValue the long to return if tag's value does not exist or + * cannot be converted to a long. + * @return the tag's value as a long, or the defaultValue. + */ + public long getValueAsLong(long defaultValue) { + long[] l = getValueAsLongs(); + if (l == null || l.length < 1) { + return defaultValue; + } + return l[0]; + } + + /** + * Gets the tag's value or null if none exists. + */ + public Object getValue() { + return mValue; + } + + /** + * Gets a long representation of the value. + * + * @param defaultValue value to return if there is no value or value is a + * rational with a denominator of 0. + * @return the tag's value as a long, or defaultValue if no representation + * exists. + */ + public long forceGetValueAsLong(long defaultValue) { + long[] l = getValueAsLongs(); + if (l != null && l.length >= 1) { + return l[0]; + } + byte[] b = getValueAsBytes(); + if (b != null && b.length >= 1) { + return b[0]; + } + Rational[] r = getValueAsRationals(); + if (r != null && r.length >= 1 && r[0].getDenominator() != 0) { + return (long) r[0].toDouble(); + } + return defaultValue; + } + + /** + * Gets a string representation of the value. + */ + public String forceGetValueAsString() { + if (mValue == null) { + return ""; + } else if (mValue instanceof byte[]) { + if (mDataType == TYPE_ASCII) { + return new String((byte[]) mValue, US_ASCII); + } else { + return Arrays.toString((byte[]) mValue); + } + } else if (mValue instanceof long[]) { + if (((long[]) mValue).length == 1) { + return String.valueOf(((long[]) mValue)[0]); + } else { + return Arrays.toString((long[]) mValue); + } + } else if (mValue instanceof Object[]) { + if (((Object[]) mValue).length == 1) { + Object val = ((Object[]) mValue)[0]; + if (val == null) { + return ""; + } else { + return val.toString(); + } + } else { + return Arrays.toString((Object[]) mValue); + } + } else { + return mValue.toString(); + } + } + + /** + * Gets the value for type {@link #TYPE_ASCII}, {@link #TYPE_LONG}, + * {@link #TYPE_UNDEFINED}, {@link #TYPE_UNSIGNED_BYTE}, + * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_UNSIGNED_SHORT}. For + * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}, call + * {@link #getRational(int)} instead. + * + * @exception IllegalArgumentException if the data type is + * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}. + */ + protected long getValueAt(int index) { + if (mValue instanceof long[]) { + return ((long[]) mValue)[index]; + } else if (mValue instanceof byte[]) { + return ((byte[]) mValue)[index]; + } + throw new IllegalArgumentException("Cannot get integer value from " + + convertTypeToString(mDataType)); } /** * Gets the {@link #TYPE_ASCII} data. - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_ASCII}. + * + * @exception IllegalArgumentException If the type is NOT + * {@link #TYPE_ASCII}. */ - public String getString() { + protected String getString() { if (mDataType != TYPE_ASCII) { throw new IllegalArgumentException("Cannot get ASCII value from " + convertTypeToString(mDataType)); } - return (String) mValue; + return new String((byte[]) mValue, US_ASCII); + } + + /* + * Get the converted ascii byte. Used by ExifOutputStream. + */ + protected byte[] getStringByte() { + return (byte[]) mValue; } /** * Gets the {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL} data. - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_RATIONAL} or - * {@link #TYPE_UNSIGNED_RATIONAL}. + * + * @exception IllegalArgumentException If the type is NOT + * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}. */ - public Rational getRational(int index) { + protected Rational getRational(int index) { if ((mDataType != TYPE_RATIONAL) && (mDataType != TYPE_UNSIGNED_RATIONAL)) { throw new IllegalArgumentException("Cannot get RATIONAL value from " + convertTypeToString(mDataType)); } - return ((Rational[]) mValue) [index]; + return ((Rational[]) mValue)[index]; } /** * Equivalent to getBytes(buffer, 0, buffer.length). */ - public void getBytes(byte[] buf) { + protected void getBytes(byte[] buf) { getBytes(buf, 0, buf.length); } @@ -1339,95 +842,151 @@ public class ExifTag { * * @param buf the byte array in which to store the bytes read. * @param offset the initial position in buffer to store the bytes. - * @param length the maximum number of bytes to store in buffer. If length > component count, - * only the valid bytes will be stored. - * - * @exception IllegalArgumentException If the type is NOT {@link #TYPE_UNDEFINED} or - * {@link #TYPE_UNSIGNED_BYTE}. + * @param length the maximum number of bytes to store in buffer. If length > + * component count, only the valid bytes will be stored. + * @exception IllegalArgumentException If the type is NOT + * {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}. */ - public void getBytes(byte[] buf, int offset, int length) { + protected void getBytes(byte[] buf, int offset, int length) { if ((mDataType != TYPE_UNDEFINED) && (mDataType != TYPE_UNSIGNED_BYTE)) { throw new IllegalArgumentException("Cannot get BYTE value from " + convertTypeToString(mDataType)); } System.arraycopy(mValue, 0, buf, offset, - (length > mComponentCount) ? mComponentCount : length); + (length > mComponentCountActual) ? mComponentCountActual : length); } /** - * Returns a string representation of the value of this tag. + * Gets the offset of this tag. This is only valid if this data size > 4 and + * contains an offset to the location of the actual value. */ - public String valueToString() { - StringBuilder sbuilder = new StringBuilder(); - switch (getDataType()) { - case ExifTag.TYPE_UNDEFINED: - case ExifTag.TYPE_UNSIGNED_BYTE: - byte buf[] = new byte[getComponentCount()]; - getBytes(buf); - for(int i = 0, n = getComponentCount(); i < n; i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(String.format("%02x", buf[i])); - } - break; - case ExifTag.TYPE_ASCII: - sbuilder.append(getString()); - break; - case ExifTag.TYPE_UNSIGNED_LONG: - for(int i = 0, n = getComponentCount(); i < n; i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(getUnsignedLong(i)); - } - break; - case ExifTag.TYPE_RATIONAL: - case ExifTag.TYPE_UNSIGNED_RATIONAL: - for(int i = 0, n = getComponentCount(); i < n; i++) { - Rational r = getRational(i); - if(i != 0) sbuilder.append(" "); - sbuilder.append(r.getNominator()).append("/").append(r.getDenominator()); - } - break; - case ExifTag.TYPE_UNSIGNED_SHORT: - for(int i = 0, n = getComponentCount(); i < n; i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(getUnsignedShort(i)); - } - break; - case ExifTag.TYPE_LONG: - for(int i = 0, n = getComponentCount(); i < n; i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(getLong(i)); - } - break; - } - return sbuilder.toString(); + protected int getOffset() { + return mOffset; } /** - * Returns true if the ID is one of the following: {@link #TAG_EXIF_IFD}, - * {@link #TAG_GPS_IFD}, {@link #TAG_JPEG_INTERCHANGE_FORMAT}, - * {@link #TAG_STRIP_OFFSETS}, {@link #TAG_INTEROPERABILITY_IFD} + * Sets the offset of this tag. */ - static boolean isOffsetTag(short tagId) { - return tagId == TAG_EXIF_IFD - || tagId == TAG_GPS_IFD - || tagId == TAG_JPEG_INTERCHANGE_FORMAT - || tagId == TAG_STRIP_OFFSETS - || tagId == TAG_INTEROPERABILITY_IFD; + protected void setOffset(int offset) { + mOffset = offset; + } + + protected void setHasDefinedCount(boolean d) { + mHasDefinedDefaultComponentCount = d; + } + + protected boolean hasDefinedCount() { + return mHasDefinedDefaultComponentCount; + } + + private boolean checkBadComponentCount(int count) { + if (mHasDefinedDefaultComponentCount && (mComponentCountActual != count)) { + return true; + } + return false; + } + + private static String convertTypeToString(short type) { + switch (type) { + case TYPE_UNSIGNED_BYTE: + return "UNSIGNED_BYTE"; + case TYPE_ASCII: + return "ASCII"; + case TYPE_UNSIGNED_SHORT: + return "UNSIGNED_SHORT"; + case TYPE_UNSIGNED_LONG: + return "UNSIGNED_LONG"; + case TYPE_UNSIGNED_RATIONAL: + return "UNSIGNED_RATIONAL"; + case TYPE_UNDEFINED: + return "UNDEFINED"; + case TYPE_LONG: + return "LONG"; + case TYPE_RATIONAL: + return "RATIONAL"; + default: + return ""; + } + } + + private boolean checkOverflowForUnsignedShort(int[] value) { + for (int v : value) { + if (v > UNSIGNED_SHORT_MAX || v < 0) { + return true; + } + } + return false; + } + + private boolean checkOverflowForUnsignedLong(long[] value) { + for (long v : value) { + if (v < 0 || v > UNSIGNED_LONG_MAX) { + return true; + } + } + return false; + } + + private boolean checkOverflowForUnsignedLong(int[] value) { + for (int v : value) { + if (v < 0) { + return true; + } + } + return false; + } + + private boolean checkOverflowForUnsignedRational(Rational[] value) { + for (Rational v : value) { + if (v.getNumerator() < 0 || v.getDenominator() < 0 + || v.getNumerator() > UNSIGNED_LONG_MAX + || v.getDenominator() > UNSIGNED_LONG_MAX) { + return true; + } + } + return false; + } + + private boolean checkOverflowForRational(Rational[] value) { + for (Rational v : value) { + if (v.getNumerator() < LONG_MIN || v.getDenominator() < LONG_MIN + || v.getNumerator() > LONG_MAX + || v.getDenominator() > LONG_MAX) { + return true; + } + } + return false; } @Override public boolean equals(Object obj) { + if (obj == null) { + return false; + } if (obj instanceof ExifTag) { ExifTag tag = (ExifTag) obj; + if (tag.mTagId != this.mTagId + || tag.mComponentCountActual != this.mComponentCountActual + || tag.mDataType != this.mDataType) { + return false; + } if (mValue != null) { - if (mValue instanceof long[]) { - if (!(tag.mValue instanceof long[])) return false; + if (tag.mValue == null) { + return false; + } else if (mValue instanceof long[]) { + if (!(tag.mValue instanceof long[])) { + return false; + } return Arrays.equals((long[]) mValue, (long[]) tag.mValue); } else if (mValue instanceof Rational[]) { - if (!(tag.mValue instanceof Rational[])) return false; + if (!(tag.mValue instanceof Rational[])) { + return false; + } return Arrays.equals((Rational[]) mValue, (Rational[]) tag.mValue); } else if (mValue instanceof byte[]) { - if (!(tag.mValue instanceof byte[])) return false; + if (!(tag.mValue instanceof byte[])) { + return false; + } return Arrays.equals((byte[]) mValue, (byte[]) tag.mValue); } else { return mValue.equals(tag.mValue); @@ -1438,4 +997,12 @@ public class ExifTag { } return false; } + + @Override + public String toString() { + return String.format("tag id: %04X\n", mTagId) + "ifd id: " + mIfd + "\ntype: " + + convertTypeToString(mDataType) + "\ncount: " + mComponentCountActual + + "\noffset: " + mOffset + "\nvalue: " + forceGetValueAsString() + "\n"; + } + } diff --git a/gallerycommon/src/com/android/gallery3d/exif/IfdData.java b/gallerycommon/src/com/android/gallery3d/exif/IfdData.java index 78f9173ccb63c660cb6bc60f318c721e5e00d466..093944aec9524521e162abf4eba05bab4caac92c 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/IfdData.java +++ b/gallerycommon/src/com/android/gallery3d/exif/IfdData.java @@ -30,7 +30,10 @@ class IfdData { private final int mIfdId; private final Map mExifTags = new HashMap(); private int mOffsetToNextIfd = 0; - + private static final int[] sIfds = { + IfdId.TYPE_IFD_0, IfdId.TYPE_IFD_1, IfdId.TYPE_IFD_EXIF, + IfdId.TYPE_IFD_INTEROPERABILITY, IfdId.TYPE_IFD_GPS + }; /** * Creates an IfdData with given IFD ID. * @@ -40,14 +43,18 @@ class IfdData { * @see IfdId#TYPE_IFD_GPS * @see IfdId#TYPE_IFD_INTEROPERABILITY */ - public IfdData(int ifdId) { + IfdData(int ifdId) { mIfdId = ifdId; } + static protected int[] getIfds() { + return sIfds; + } + /** * Get a array the contains all {@link ExifTag} in this IFD. */ - public ExifTag[] getAllTags() { + protected ExifTag[] getAllTags() { return mExifTags.values().toArray(new ExifTag[mExifTags.size()]); } @@ -60,63 +67,86 @@ class IfdData { * @see IfdId#TYPE_IFD_GPS * @see IfdId#TYPE_IFD_INTEROPERABILITY */ - public int getId() { + protected int getId() { return mIfdId; } /** - * Gets the {@link ExifTag} with given tag id. Return null if there is no such tag. + * Gets the {@link ExifTag} with given tag id. Return null if there is no + * such tag. */ - public ExifTag getTag(short tagId) { + protected ExifTag getTag(short tagId) { return mExifTags.get(tagId); } /** * Adds or replaces a {@link ExifTag}. */ - public void setTag(ExifTag tag) { - mExifTags.put(tag.getTagId(), tag); + protected ExifTag setTag(ExifTag tag) { + tag.setIfd(mIfdId); + return mExifTags.put(tag.getTagId(), tag); + } + + protected boolean checkCollision(short tagId) { + return mExifTags.get(tagId) != null; + } + + /** + * Removes the tag of the given ID + */ + protected void removeTag(short tagId) { + mExifTags.remove(tagId); } /** * Gets the tags count in the IFD. */ - public int getTagCount() { + protected int getTagCount() { return mExifTags.size(); } /** * Sets the offset of next IFD. */ - void setOffsetToNextIfd(int offset) { + protected void setOffsetToNextIfd(int offset) { mOffsetToNextIfd = offset; } /** * Gets the offset of next IFD. */ - int getOffsetToNextIfd() { + protected int getOffsetToNextIfd() { return mOffsetToNextIfd; } /** - * Returns true if all tags in this two IFDs are equal. Note that tags of IFDs offset or - * thumbnail offset will be ignored. + * Returns true if all tags in this two IFDs are equal. Note that tags of + * IFDs offset or thumbnail offset will be ignored. */ @Override public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } if (obj instanceof IfdData) { IfdData data = (IfdData) obj; if (data.getId() == mIfdId && data.getTagCount() == getTagCount()) { ExifTag[] tags = data.getAllTags(); - for (ExifTag tag: tags) { - if (ExifTag.isOffsetTag(tag.getTagId())) continue; + for (ExifTag tag : tags) { + if (ExifInterface.isOffsetTag(tag.getTagId())) { + continue; + } ExifTag tag2 = mExifTags.get(tag.getTagId()); - if (!tag.equals(tag2)) return false; + if (!tag.equals(tag2)) { + return false; + } } return true; } } return false; } -} \ No newline at end of file +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/IfdId.java b/gallerycommon/src/com/android/gallery3d/exif/IfdId.java index 1b96343694625dab8ea0188989ac22e166318fd5..7842edbd46697db5612d40c7352a0618e9486135 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/IfdId.java +++ b/gallerycommon/src/com/android/gallery3d/exif/IfdId.java @@ -13,14 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.android.gallery3d.exif; +/** + * The constants of the IFD ID defined in EXIF spec. + */ public interface IfdId { public static final int TYPE_IFD_0 = 0; public static final int TYPE_IFD_1 = 1; public static final int TYPE_IFD_EXIF = 2; public static final int TYPE_IFD_INTEROPERABILITY = 3; public static final int TYPE_IFD_GPS = 4; - /* This is use in ExifData to allocate enough IfdData */ + /* This is used in ExifData to allocate enough IfdData */ static final int TYPE_IFD_COUNT = 5; + } diff --git a/gallerycommon/src/com/android/gallery3d/exif/OrderedDataOutputStream.java b/gallerycommon/src/com/android/gallery3d/exif/OrderedDataOutputStream.java index 4f785a889c9b5f5c4438d68bb0eb6db7d3307783..428e6b9fc481300dc788c794a5ffab8c0e02eef8 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/OrderedDataOutputStream.java +++ b/gallerycommon/src/com/android/gallery3d/exif/OrderedDataOutputStream.java @@ -29,24 +29,28 @@ class OrderedDataOutputStream extends FilterOutputStream { super(out); } - public void setByteOrder(ByteOrder order) { + public OrderedDataOutputStream setByteOrder(ByteOrder order) { mByteBuffer.order(order); + return this; } - public void writeShort(short value) throws IOException { + public OrderedDataOutputStream writeShort(short value) throws IOException { mByteBuffer.rewind(); mByteBuffer.putShort(value); out.write(mByteBuffer.array(), 0, 2); - } + return this; + } - public void writeInt(int value) throws IOException { + public OrderedDataOutputStream writeInt(int value) throws IOException { mByteBuffer.rewind(); mByteBuffer.putInt(value); out.write(mByteBuffer.array()); + return this; } - public void writeRational(Rational rational) throws IOException { - writeInt((int) rational.getNominator()); + public OrderedDataOutputStream writeRational(Rational rational) throws IOException { + writeInt((int) rational.getNumerator()); writeInt((int) rational.getDenominator()); + return this; } } diff --git a/gallerycommon/src/com/android/gallery3d/exif/Rational.java b/gallerycommon/src/com/android/gallery3d/exif/Rational.java index 7d902626117b467db750b83eb0b58bb34b004318..591d63faf74e2dd3f8b3503f2323b39d48395cc3 100644 --- a/gallerycommon/src/com/android/gallery3d/exif/Rational.java +++ b/gallerycommon/src/com/android/gallery3d/exif/Rational.java @@ -16,30 +16,73 @@ package com.android.gallery3d.exif; +/** + * The rational data type of EXIF tag. Contains a pair of longs representing the + * numerator and denominator of a Rational number. + */ public class Rational { - private final long mNominator; + private final long mNumerator; private final long mDenominator; + /** + * Create a Rational with a given numerator and denominator. + * + * @param nominator + * @param denominator + */ public Rational(long nominator, long denominator) { - mNominator = nominator; + mNumerator = nominator; mDenominator = denominator; } - public long getNominator() { - return mNominator; + /** + * Create a copy of a Rational. + */ + public Rational(Rational r) { + mNumerator = r.mNumerator; + mDenominator = r.mDenominator; + } + + /** + * Gets the numerator of the rational. + */ + public long getNumerator() { + return mNumerator; } + /** + * Gets the denominator of the rational + */ public long getDenominator() { return mDenominator; } + /** + * Gets the rational value as type double. Will cause a divide-by-zero error + * if the denominator is 0. + */ + public double toDouble() { + return mNumerator / (double) mDenominator; + } + @Override public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (this == obj) { + return true; + } if (obj instanceof Rational) { Rational data = (Rational) obj; - return mNominator == data.mNominator && mDenominator == data.mDenominator; + return mNumerator == data.mNumerator && mDenominator == data.mDenominator; } return false; } -} \ No newline at end of file + + @Override + public String toString() { + return mNumerator + "/" + mDenominator; + } +} diff --git a/gallerycommon/src/com/android/gallery3d/exif/Util.java b/gallerycommon/src/com/android/gallery3d/exif/Util.java deleted file mode 100644 index 594d6fc7f843ad73f7d19d7b0881151e0f2c4eda..0000000000000000000000000000000000000000 --- a/gallerycommon/src/com/android/gallery3d/exif/Util.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.gallery3d.exif; - -import java.io.Closeable; - -class Util { - public static boolean equals(Object a, Object b) { - return (a == b) || (a == null ? false : a.equals(b)); - } - - public static void closeSilently(Closeable c) { - if (c == null) return; - try { - c.close(); - } catch (Throwable t) { - // do nothing - } - } -} diff --git a/jni/Android.mk b/jni/Android.mk index db31bfdf6e10dbb623df487e4d152fa57f1e02fa..e612486e1bc580a076151d01cda3e54fcc9bfc91 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -25,23 +25,26 @@ LOCAL_CPP_EXTENSION := .cc LOCAL_LDFLAGS := -llog -ljnigraphics LOCAL_SDK_VERSION := 9 LOCAL_MODULE := libjni_filtershow_filters -LOCAL_SRC_FILES := filters/bw.c \ - filters/gradient.c \ +LOCAL_SRC_FILES := filters/gradient.c \ filters/saturated.c \ filters/exposure.c \ + filters/edge.c \ filters/contrast.c \ filters/hue.c \ filters/shadows.c \ + filters/highlight.c \ filters/hsv.c \ filters/vibrance.c \ filters/geometry.c \ + filters/negative.c \ filters/vignette.c \ filters/redEyeMath.c \ filters/fx.c \ filters/wbalance.c \ filters/redeye.c \ filters/bwfilter.c \ - filters/tinyplanet.cc + filters/tinyplanet.cc \ + filters/kmeans.cc LOCAL_CFLAGS += -ffast-math -O3 -funroll-loops LOCAL_ARM_MODE := arm diff --git a/jni/filters/bw.c b/jni/filters/bw.c deleted file mode 100644 index f075d307e28d6b47c35d59071565e498c8f633d8..0000000000000000000000000000000000000000 --- a/jni/filters/bw.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "filters.h" - -void JNIFUNCF(ImageFilterBW, nativeApplyFilter, jobject bitmap, jint width, jint height) -{ - char* destination = 0; - AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - int i; - int len = width * height * 4; - float Rf = 0.2999f; - float Gf = 0.587f; - float Bf = 0.114f; - - for (i = 0; i < len; i+=4) - { - int r = destination[RED]; - int g = destination[GREEN]; - int b = destination[BLUE]; - int t = CLAMP(Rf * r + Gf * g + Bf *b); - - destination[RED] = t; - destination[GREEN] = t; - destination[BLUE] = t; - } - AndroidBitmap_unlockPixels(env, bitmap); -} - -void JNIFUNCF(ImageFilterBWRed, nativeApplyFilter, jobject bitmap, jint width, jint height) -{ - char* destination = 0; - AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - int i; - int len = width * height * 4; - for (i = 0; i < len; i+=4) - { - int r = destination[RED]; - int g = destination[GREEN]; - int b = destination[BLUE]; - int t = (g + b) / 2; - r = t; - g = t; - b = t; - destination[RED] = r; - destination[GREEN] = g; - destination[BLUE] = b; - } - AndroidBitmap_unlockPixels(env, bitmap); -} - -void JNIFUNCF(ImageFilterBWGreen, nativeApplyFilter, jobject bitmap, jint width, jint height) -{ - char* destination = 0; - AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - int i; - int len = width * height * 4; - for (i = 0; i < len; i+=4) - { - int r = destination[RED]; - int g = destination[GREEN]; - int b = destination[BLUE]; - int t = (r + b) / 2; - r = t; - g = t; - b = t; - destination[RED] = r; - destination[GREEN] = g; - destination[BLUE] = b; - } - AndroidBitmap_unlockPixels(env, bitmap); -} - -void JNIFUNCF(ImageFilterBWBlue, nativeApplyFilter, jobject bitmap, jint width, jint height) -{ - char* destination = 0; - AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - int i; - int len = width * height * 4; - for (i = 0; i < len; i+=4) - { - int r = destination[RED]; - int g = destination[GREEN]; - int b = destination[BLUE]; - int t = (r + g) / 2; - r = t; - g = t; - b = t; - destination[RED] = r; - destination[GREEN] = g; - destination[BLUE] = b; - } - AndroidBitmap_unlockPixels(env, bitmap); -} diff --git a/jni/filters/contrast.c b/jni/filters/contrast.c index 6c1b976cf24c2436ab0a39a0c408945e7722f44b..b04e9364efc33c9bbdb019e19b2a6f443bb14440 100644 --- a/jni/filters/contrast.c +++ b/jni/filters/contrast.c @@ -27,6 +27,15 @@ unsigned char clamp(int c) return (unsigned char) c; } +int clampMax(int c,int max) +{ + c &= ~(c >> 31); + c -= max; + c &= (c >> 31); + c += max; + return c; +} + void JNIFUNCF(ImageFilterContrast, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat bright) { char* destination = 0; diff --git a/jni/filters/edge.c b/jni/filters/edge.c new file mode 100644 index 0000000000000000000000000000000000000000..9f5d88f77e60be189cc82aafbb6a4cc9e902d7aa --- /dev/null +++ b/jni/filters/edge.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "filters.h" + +void JNIFUNCF(ImageFilterEdge, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat p) +{ + char* destination = 0; + AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); + + // using contrast function: + // f(v) = exp(-alpha * v^beta) + // use beta ~ 1 + + float const alpha = 5.0f; + float const beta = p; + float const c_min = 100.0f; + float const c_max = 500.0f; + + // pixels must be 4 bytes + char * dst = destination; + + int j, k; + char * ptr = destination; + int row_stride = 4 * width; + + // set 2 row buffer (avoids bitmap copy) + int buf_len = 2 * row_stride; + char buf[buf_len]; + int buf_row_ring = 0; + + // set initial buffer to black + memset(buf, 0, buf_len * sizeof(char)); + for (j = 3; j < buf_len; j+=4) { + *(buf + j) = 255; // set initial alphas + } + + // apply sobel filter + for (j = 1; j < height - 1; j++) { + + for (k = 1; k < width - 1; k++){ + int loc = j * row_stride + k * 4; + + float bestx = 0.0f; + int l; + for (l = 0; l < 3; l++) { + float tmp = 0.0f; + tmp += *(ptr + (loc - row_stride + 4 + l)); + tmp += *(ptr + (loc + 4 + l)) * 2.0f; + tmp += *(ptr + (loc + row_stride + 4 + l)); + tmp -= *(ptr + (loc - row_stride - 4 + l)); + tmp -= *(ptr + (loc - 4 + l)) * 2.0f; + tmp -= *(ptr + (loc + row_stride - 4 + l)); + if (fabs(tmp) > fabs(bestx)) { + bestx = tmp; + } + } + + float besty = 0.0f; + for (l = 0; l < 3; l++) { + float tmp = 0.0f; + tmp -= *(ptr + (loc - row_stride - 4 + l)); + tmp -= *(ptr + (loc - row_stride + l)) * 2.0f; + tmp -= *(ptr + (loc - row_stride + 4 + l)); + tmp += *(ptr + (loc + row_stride - 4 + l)); + tmp += *(ptr + (loc + row_stride + l)) * 2.0f; + tmp += *(ptr + (loc + row_stride + 4 + l)); + if (fabs(tmp) > fabs(besty)) { + besty = tmp; + } + } + + // compute gradient magnitude + float mag = sqrt(bestx * bestx + besty * besty); + + // clamp + mag = MIN(MAX(c_min, mag), c_max); + + // scale to [0, 1] + mag = (mag - c_min) / (c_max - c_min); + + float ret = 1.0f - exp (- alpha * pow(mag, beta)); + ret = 255 * ret; + + int off = k * 4; + *(buf + buf_row_ring + off) = ret; + *(buf + buf_row_ring + off + 1) = ret; + *(buf + buf_row_ring + off + 2) = ret; + *(buf + buf_row_ring + off + 3) = *(ptr + loc + 3); + } + + buf_row_ring += row_stride; + buf_row_ring %= buf_len; + + if (j - 1 >= 0) { + memcpy((dst + row_stride * (j - 1)), (buf + buf_row_ring), row_stride * sizeof(char)); + } + + } + buf_row_ring += row_stride; + buf_row_ring %= buf_len; + int second_last_row = row_stride * (height - 2); + memcpy((dst + second_last_row), (buf + buf_row_ring), row_stride * sizeof(char)); + + // set last row to black + int last_row = row_stride * (height - 1); + memset((dst + last_row), 0, row_stride * sizeof(char)); + for (j = 3; j < row_stride; j+=4) { + *(dst + last_row + j) = 255; // set alphas + } + AndroidBitmap_unlockPixels(env, bitmap); +} diff --git a/jni/filters/filters.h b/jni/filters/filters.h index d518b6398e8c3667a1f32849f5f079884a3784f1..14b69cdd477169c9ec47a2cc84c79f7e5a739e35 100644 --- a/jni/filters/filters.h +++ b/jni/filters/filters.h @@ -44,6 +44,7 @@ typedef unsigned int Color; #define CLAMP(c) (MAX(0, MIN(255, c))) __inline__ unsigned char clamp(int c); +__inline__ int clampMax(int c,int max); extern void rgb2hsv( unsigned char *rgb,int rgbOff,unsigned short *hsv,int hsvOff); extern void hsv2rgb(unsigned short *hsv,int hsvOff,unsigned char *rgb,int rgbOff); diff --git a/jni/filters/highlight.c b/jni/filters/highlight.c new file mode 100644 index 0000000000000000000000000000000000000000..fe9b88f9495713bdbd56ecbf4703422e9059677a --- /dev/null +++ b/jni/filters/highlight.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "filters.h" + +void JNIFUNCF(ImageFilterHighlights, nativeApplyFilter, jobject bitmap, + jint width, jint height, jfloatArray luminanceMap){ + char* destination = 0; + AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); + unsigned char * rgb = (unsigned char * )destination; + int i; + int len = width * height * 4; + jfloat* lum = (*env)->GetFloatArrayElements(env, luminanceMap,0); + unsigned short * hsv = (unsigned short *)malloc(3*sizeof(short)); + + for (i = 0; i < len; i+=4) + { + rgb2hsv(rgb,i,hsv,0); + int v = clampMax(hsv[0],4080); + hsv[0] = (unsigned short) clampMax(lum[((255*v)/4080)]*4080,4080); + hsv2rgb(hsv,0, rgb,i); + } + + free(hsv); + AndroidBitmap_unlockPixels(env, bitmap); +} diff --git a/jni/filters/kmeans.cc b/jni/filters/kmeans.cc new file mode 100644 index 0000000000000000000000000000000000000000..97cead7bce6ca41939f57be2679d3ec68bf6df2d --- /dev/null +++ b/jni/filters/kmeans.cc @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "filters.h" +#include "kmeans.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * For reasonable speeds: + * k < 30 + * small_ds_bitmap width/height < 64 pixels. + * large_ds_bitmap width/height < 512 pixels + * + * bad for high-frequency image noise + */ + +void JNIFUNCF(ImageFilterKMeans, nativeApplyFilter, jobject bitmap, jint width, jint height, + jobject large_ds_bitmap, jint lwidth, jint lheight, jobject small_ds_bitmap, + jint swidth, jint sheight, jint p, jint seed) +{ + char* destination = 0; + char* larger_ds_dst = 0; + char* smaller_ds_dst = 0; + AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); + AndroidBitmap_lockPixels(env, large_ds_bitmap, (void**) &larger_ds_dst); + AndroidBitmap_lockPixels(env, small_ds_bitmap, (void**) &smaller_ds_dst); + unsigned char * dst = (unsigned char *) destination; + + unsigned char * small_ds = (unsigned char *) smaller_ds_dst; + unsigned char * large_ds = (unsigned char *) larger_ds_dst; + + // setting for small bitmap + int len = swidth * sheight * 4; + int dimension = 3; + int stride = 4; + int iterations = 20; + int k = p; + unsigned int s = seed; + unsigned char finalCentroids[k * stride]; + + // get initial picks from small downsampled image + runKMeans(k, finalCentroids, small_ds, len, dimension, + stride, iterations, s); + + + len = lwidth * lheight * 4; + iterations = 8; + unsigned char nextCentroids[k * stride]; + + // run kmeans on large downsampled image + runKMeansWithPicks(k, nextCentroids, large_ds, len, + dimension, stride, iterations, finalCentroids); + + len = width * height * 4; + + // apply to final image + applyCentroids(k, nextCentroids, dst, len, dimension, stride); + + AndroidBitmap_unlockPixels(env, small_ds_bitmap); + AndroidBitmap_unlockPixels(env, large_ds_bitmap); + AndroidBitmap_unlockPixels(env, bitmap); +} +#ifdef __cplusplus +} +#endif diff --git a/jni/filters/kmeans.h b/jni/filters/kmeans.h new file mode 100644 index 0000000000000000000000000000000000000000..24506058a0ff1520df66d6442e84bcefb7f66250 --- /dev/null +++ b/jni/filters/kmeans.h @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KMEANS_H +#define KMEANS_H + +#include +#include + +// Helper functions + +template +inline void sum(T values[], int len, int dimension, int stride, N dst[]) { + int x, y; + // zero out dst vector + for (x = 0; x < dimension; x++) { + dst[x] = 0; + } + for (x = 0; x < len; x+= stride) { + for (y = 0; y < dimension; y++) { + dst[y] += values[x + y]; + } + } +} + +template +inline void set(T val1[], N val2[], int dimension) { + int x; + for (x = 0; x < dimension; x++) { + val1[x] = val2[x]; + } +} + +template +inline void add(T val[], N dst[], int dimension) { + int x; + for (x = 0; x < dimension; x++) { + dst[x] += val[x]; + } +} + +template +inline void divide(T dst[], N divisor, int dimension) { + int x; + if (divisor == 0) { + return; + } + for (x = 0; x < dimension; x++) { + dst[x] /= divisor; + } +} + +/** + * Calculates euclidean distance. + */ + +template +inline N euclideanDist(T val1[], T val2[], int dimension) { + int x; + N sum = 0; + for (x = 0; x < dimension; x++) { + N diff = (N) val1[x] - (N) val2[x]; + sum += diff * diff; + } + return sqrt(sum); +} + +// K-Means + + +/** + * Picks k random starting points from the data set. + */ +template +void initialPickHeuristicRandom(int k, T values[], int len, int dimension, int stride, T dst[], + unsigned int seed) { + int x, z, num_vals, cntr; + num_vals = len / stride; + cntr = 0; + srand(seed); + unsigned int r_vals[k]; + unsigned int r; + + for (x = 0; x < k; x++) { + + // ensure randomly chosen value is unique + int r_check = 0; + while (r_check == 0) { + r = (unsigned int) rand() % num_vals; + r_check = 1; + for (z = 0; z < x; z++) { + if (r == r_vals[z]) { + r_check = 0; + } + } + } + r_vals[x] = r; + r *= stride; + + // set dst to be randomly chosen value + set(dst + cntr, values + r, dimension); + cntr += stride; + } +} + +/** + * Finds index of closet centroid to a value + */ +template +inline int findClosest(T values[], T oldCenters[], int dimension, int stride, int pop_size) { + int best_ind = 0; + N best_len = euclideanDist (values, oldCenters, dimension); + int y; + for (y = stride; y < pop_size; y+=stride) { + N l = euclideanDist (values, oldCenters + y, dimension); + if (l < best_len) { + best_len = l; + best_ind = y; + } + } + return best_ind; +} + +/** + * Calculates new centroids by averaging value clusters for old centroids. + */ +template +int calculateNewCentroids(int k, T values[], int len, int dimension, int stride, T oldCenters[], + T dst[]) { + int x, pop_size; + pop_size = k * stride; + int popularities[k]; + N tmp[pop_size]; + + //zero popularities + memset(popularities, 0, sizeof(int) * k); + // zero dst, and tmp + for (x = 0; x < pop_size; x++) { + tmp[x] = 0; + } + + // put summation for each k in tmp + for (x = 0; x < len; x+=stride) { + int best = findClosest(values + x, oldCenters, dimension, stride, pop_size); + add(values + x, tmp + best, dimension); + popularities[best / stride]++; + + } + + int ret = 0; + int y; + // divide to get centroid and set dst to result + for (x = 0; x < pop_size; x+=stride) { + divide(tmp + x, popularities[x / stride], dimension); + for (y = 0; y < dimension; y++) { + if ((dst + x)[y] != (T) ((tmp + x)[y])) { + ret = 1; + } + } + set(dst + x, tmp + x, dimension); + } + return ret; +} + +template +void runKMeansWithPicks(int k, T finalCentroids[], T values[], int len, int dimension, int stride, + int iterations, T initialPicks[]){ + int k_len = k * stride; + int x; + + // zero newCenters + for (x = 0; x < k_len; x++) { + finalCentroids[x] = 0; + } + + T * c1 = initialPicks; + T * c2 = finalCentroids; + T * temp; + int ret = 1; + for (x = 0; x < iterations; x++) { + ret = calculateNewCentroids(k, values, len, dimension, stride, c1, c2); + temp = c1; + c1 = c2; + c2 = temp; + if (ret == 0) { + x = iterations; + } + } + set(finalCentroids, c1, dimension); +} + +/** + * Runs the k-means algorithm on dataset values with some initial centroids. + */ +template +void runKMeans(int k, T finalCentroids[], T values[], int len, int dimension, int stride, + int iterations, unsigned int seed){ + int k_len = k * stride; + T initialPicks [k_len]; + initialPickHeuristicRandom(k, values, len, dimension, stride, initialPicks, seed); + + runKMeansWithPicks(k, finalCentroids, values, len, dimension, stride, + iterations, initialPicks); +} + +/** + * Sets each value in values to the closest centroid. + */ +template +void applyCentroids(int k, T centroids[], T values[], int len, int dimension, int stride) { + int x, pop_size; + pop_size = k * stride; + for (x = 0; x < len; x+= stride) { + int best = findClosest(values + x, centroids, dimension, stride, pop_size); + set(values + x, centroids + best, dimension); + } +} + +#endif // KMEANS_H diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePresetSaturated.java b/jni/filters/negative.c similarity index 56% rename from src/com/android/gallery3d/filtershow/presets/ImagePresetSaturated.java rename to jni/filters/negative.c index ddfca75085c721dc739f3726727ab38e341551c0..735e583c9828c5c04af0b1fe6131bf8f8ef9ab22 100644 --- a/src/com/android/gallery3d/filtershow/presets/ImagePresetSaturated.java +++ b/jni/filters/negative.c @@ -14,22 +14,20 @@ * limitations under the License. */ -package com.android.gallery3d.filtershow.presets; +#include "filters.h" -import com.android.gallery3d.filtershow.filters.ImageFilterSaturated; +void JNIFUNCF(ImageFilterNegative, nativeApplyFilter, jobject bitmap, jint width, jint height) +{ + char* destination = 0; + AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); -public class ImagePresetSaturated extends ImagePreset { - - @Override - public String name() { - return "Saturated"; - } - - @Override - public void setup() { - ImageFilterSaturated filter = new ImageFilterSaturated(); - filter.setParameter(50); - mFilters.add(filter); + int tot_len = height * width * 4; + int i; + char * dst = destination; + for (i = 0; i < tot_len; i+=4) { + dst[RED] = 255 - dst[RED]; + dst[GREEN] = 255 - dst[GREEN]; + dst[BLUE] = 255 - dst[BLUE]; } - + AndroidBitmap_unlockPixels(env, bitmap); } diff --git a/jni/filters/tinyplanet.cc b/jni/filters/tinyplanet.cc index a40470d3410a39eff4779147cf289d82e7db3018..beac0861aedec490859ece7e1288e00abd385c1e 100644 --- a/jni/filters/tinyplanet.cc +++ b/jni/filters/tinyplanet.cc @@ -80,6 +80,7 @@ inline void InterpolatePixel(const ImageRGBA &image, float x, float y, ax * ay * p2[4] + axn * ay * p2[0] + 0.5f); p++; p2++; + dest[3] = 0xFF; } // Wrap circular coordinates around the globe diff --git a/jni/filters/vignette.c b/jni/filters/vignette.c index 2799ff001810ba84c7ea02994e1f5c9dc579cf53..b9ee3ff01adc85955849dd6fb028745c74bf51a1 100644 --- a/jni/filters/vignette.c +++ b/jni/filters/vignette.c @@ -15,52 +15,32 @@ */ #include "filters.h" +#include static int* gVignetteMap = 0; static int gVignetteWidth = 0; static int gVignetteHeight = 0; -__inline__ void createVignetteMap(int w, int h) -{ - if (gVignetteMap && (gVignetteWidth != w || gVignetteHeight != h)) - { - free(gVignetteMap); - gVignetteMap = 0; - } - if (gVignetteMap == 0) - { - gVignetteWidth = w; - gVignetteHeight = h; - - int cx = w / 2; - int cy = h / 2; - int i, j; - - gVignetteMap = malloc(w * h * sizeof(int)); - float maxDistance = cx * cx * 2.0f; - for (i = 0; i < w; i++) - { - for (j = 0; j < h; j++) - { - float distance = (cx - i) * (cx - i) + (cy - j) * (cy - j); - gVignetteMap[j * w + i] = (int) (distance / maxDistance * 255); - } - } - } -} - -void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat strength) +void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jint centerx, jint centery, jfloat radiusx, jfloat radiusy, jfloat strength) { char* destination = 0; AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - createVignetteMap(width, height); int i; int len = width * height * 4; int vignette = 0; + float d = centerx; + if (radiusx == 0) radiusx = 10; + if (radiusy == 0) radiusy = 10; + float scalex = 1/radiusx; + float scaley = 1/radiusy; for (i = 0; i < len; i += 4) { - vignette = (int) (strength * gVignetteMap[i / 4]); + int p = i/4; + float x = ((p%width)-centerx)*scalex; + float y = ((p/width)-centery)*scaley; + float dist = sqrt(x*x+y*y)-1; + vignette = (int) (strength*256*MAX(dist,0)); destination[RED] = CLAMP(destination[RED] - vignette); destination[GREEN] = CLAMP(destination[GREEN] - vignette); destination[BLUE] = CLAMP(destination[BLUE] - vignette); diff --git a/jni_mosaic/Android.mk b/jni_mosaic/Android.mk new file mode 100755 index 0000000000000000000000000000000000000000..9f6f7392505ccc337f7c61b6571169280b2eadda --- /dev/null +++ b/jni_mosaic/Android.mk @@ -0,0 +1,60 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/feature_stab/db_vlvm \ + $(LOCAL_PATH)/feature_stab/src \ + $(LOCAL_PATH)/feature_stab/src/dbreg \ + $(LOCAL_PATH)/feature_mos/src \ + $(LOCAL_PATH)/feature_mos/src/mosaic + +LOCAL_CFLAGS := -O3 -DNDEBUG -fstrict-aliasing + +LOCAL_SRC_FILES := \ + feature_mos_jni.cpp \ + mosaic_renderer_jni.cpp \ + feature_mos/src/mosaic/trsMatrix.cpp \ + feature_mos/src/mosaic/AlignFeatures.cpp \ + feature_mos/src/mosaic/Blend.cpp \ + feature_mos/src/mosaic/Delaunay.cpp \ + feature_mos/src/mosaic/ImageUtils.cpp \ + feature_mos/src/mosaic/Mosaic.cpp \ + feature_mos/src/mosaic/Pyramid.cpp \ + feature_mos/src/mosaic_renderer/Renderer.cpp \ + feature_mos/src/mosaic_renderer/WarpRenderer.cpp \ + feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.cpp \ + feature_mos/src/mosaic_renderer/YVURenderer.cpp \ + feature_mos/src/mosaic_renderer/FrameBuffer.cpp \ + feature_stab/db_vlvm/db_feature_detection.cpp \ + feature_stab/db_vlvm/db_feature_matching.cpp \ + feature_stab/db_vlvm/db_framestitching.cpp \ + feature_stab/db_vlvm/db_image_homography.cpp \ + feature_stab/db_vlvm/db_rob_image_homography.cpp \ + feature_stab/db_vlvm/db_utilities.cpp \ + feature_stab/db_vlvm/db_utilities_camera.cpp \ + feature_stab/db_vlvm/db_utilities_indexing.cpp \ + feature_stab/db_vlvm/db_utilities_linalg.cpp \ + feature_stab/db_vlvm/db_utilities_poly.cpp \ + feature_stab/src/dbreg/dbreg.cpp \ + feature_stab/src/dbreg/dbstabsmooth.cpp \ + feature_stab/src/dbreg/vp_motionmodel.c + +ifeq ($(TARGET_ARCH), arm) + LOCAL_SDK_VERSION := 9 +endif + +ifeq ($(TARGET_ARCH), x86) + LOCAL_SDK_VERSION := 9 +endif + +ifeq ($(TARGET_ARCH), mips) + LOCAL_SDK_VERSION := 9 +endif + +LOCAL_LDFLAGS := -llog -lGLESv2 + +LOCAL_MODULE_TAGS := optional + +LOCAL_MODULE := libjni_mosaic +include $(BUILD_SHARED_LIBRARY) diff --git a/jni_mosaic/NOTICE b/jni_mosaic/NOTICE new file mode 100644 index 0000000000000000000000000000000000000000..7317ae2a74b7227b5389f6a4bcd51a691255b973 --- /dev/null +++ b/jni_mosaic/NOTICE @@ -0,0 +1,190 @@ + + Copyright (c) 2005-2011, The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/jni_mosaic/feature_mos/doc/Readme.txt b/jni_mosaic/feature_mos/doc/Readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..83ce165e243a380e433175801e8946747981e69a --- /dev/null +++ b/jni_mosaic/feature_mos/doc/Readme.txt @@ -0,0 +1,3 @@ +To generate the html docs, execute +doxygen feature_mos_API_doxyfile + diff --git a/jni_mosaic/feature_mos/doc/feature_mos_API_doxyfile b/jni_mosaic/feature_mos/doc/feature_mos_API_doxyfile new file mode 100755 index 0000000000000000000000000000000000000000..dca8c8c07304f371554a347bc0f93583a18f1d54 --- /dev/null +++ b/jni_mosaic/feature_mos/doc/feature_mos_API_doxyfile @@ -0,0 +1,1557 @@ +# Doxyfile 1.6.1 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all +# text before the first occurrence of this tag. Doxygen uses libiconv (or the +# iconv built into libc) for the transcoding. See +# http://www.gnu.org/software/libiconv for the list of possible encodings. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = . + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional, +# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German, +# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English +# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian, +# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak, +# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = /Users/dimitri/doxygen/mail/1.5.7/doxywizard/ + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like regular Qt-style comments +# (thus requiring an explicit @brief command for a brief description.) + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then Doxygen will +# interpret the first line (until the first dot) of a Qt-style +# comment as the brief description. If set to NO, the comments +# will behave just like regular Qt-style comments (thus requiring +# an explicit \brief command for a brief description.) + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for +# Java. For instance, namespaces will be presented as packages, qualified +# scopes will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources only. Doxygen will then generate output that is more tailored for +# Fortran. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for +# VHDL. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it parses. +# With this tag you can assign which parser to use for a given extension. +# Doxygen has a built-in mapping, but you can override or extend it using this tag. +# The format is ext=language, where ext is a file extension, and language is one of +# the parsers supported by doxygen: IDL, Java, Javascript, C#, C, C++, D, PHP, +# Objective-C, Python, Fortran, VHDL, C, C++. For instance to make doxygen treat +# .inc files as Fortran files (default is PHP), and .f files as C (default is Fortran), +# use: inc=Fortran f=C. Note that for custom extensions you also need to set +# FILE_PATTERNS otherwise the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only. +# Doxygen will parse them like normal C++ but will assume all classes use public +# instead of private inheritance when no explicit protection keyword is present. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate getter +# and setter methods for a property. Setting this option to YES (the default) +# will make doxygen to replace the get and set methods by a property in the +# documentation. This will only work if the methods are indeed getting or +# setting a simple type. If this is not the case, or you want to show the +# methods anyway, you should set this option to NO. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum +# is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically +# be useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. + +TYPEDEF_HIDES_STRUCT = NO + +# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to +# determine which symbols to keep in memory and which to flush to disk. +# When the cache is full, less often used symbols will be written to disk. +# For small to medium size projects (<1000 input files) the default value is +# probably good enough. For larger projects a too small cache size can cause +# doxygen to be busy swapping symbols to and from disk most of the time +# causing a significant performance penality. +# If the system has enough physical memory increasing the cache will improve the +# performance by keeping more symbols in memory. Note that the value works on +# a logarithmic scale so increasing the size by one will rougly double the +# memory usage. The cache size is given by this formula: +# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, +# corresponding to a cache size of 2^16 = 65536 symbols + +SYMBOL_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base +# name of the file that contains the anonymous namespace. By default +# anonymous namespace are hidden. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = NO + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = YES + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen +# will sort the (brief and detailed) documentation of class members so that +# constructors and destructors are listed first. If set to NO (the default) +# the constructors will appear in the respective orders defined by +# SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. +# This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO +# and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the +# hierarchy of group names into alphabetical order. If set to NO (the default) +# the group names will appear in their defined order. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = NO + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. +# This will remove the Files entry from the Quick Index and from the +# Folder Tree View (if specified). The default is YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the +# Namespaces page. This will remove the Namespaces entry from the Quick Index +# and from the Folder Tree View (if specified). The default is YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed by +# doxygen. The layout file controls the global structure of the generated output files +# in an output format independent way. The create the layout file that represents +# doxygen's defaults, run doxygen with the -l option. You can optionally specify a +# file name after the option, if omitted DoxygenLayout.xml will be used as the name +# of the layout file. + +LAYOUT_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = ../src/mosaic/Mosaic.h + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is +# also the default input encoding. Doxygen uses libiconv (or the iconv built +# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for +# the list of possible encodings. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py *.f90 + +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py \ + *.f90 \ + *.f \ + *.vhd \ + *.vhdl + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = * + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = NO + +# If the REFERENCES_RELATION tag is set to YES +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = NO + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. Otherwise they will link to the documentation. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. For this to work a browser that supports +# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox +# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari). + +HTML_DYNAMIC_SECTIONS = NO + +# If the GENERATE_DOCSET tag is set to YES, additional index files +# will be generated that can be used as input for Apple's Xcode 3 +# integrated development environment, introduced with OSX 10.5 (Leopard). +# To create a documentation set, doxygen will generate a Makefile in the +# HTML output directory. Running make will produce the docset in that +# directory and running "make install" will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find +# it at startup. +# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html for more information. + +GENERATE_DOCSET = NO + +# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the +# feed. A documentation feed provides an umbrella under which multiple +# documentation sets from a single provider (such as a company or product suite) +# can be grouped. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that +# should uniquely identify the documentation set bundle. This should be a +# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen +# will append .docset to the name. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compiled HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING +# is used to encode HtmlHelp index (hhk), content (hhc) and project file +# content. + +CHM_INDEX_ENCODING = + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and QHP_VIRTUAL_FOLDER +# are set, an additional index file will be generated that can be used as input for +# Qt's qhelpgenerator to generate a Qt Compressed Help (.qch) of the generated +# HTML documentation. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can +# be used to specify the file name of the resulting .qch file. +# The path specified is relative to the HTML output folder. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#namespace + +QHP_NAMESPACE = + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#virtual-folders + +QHP_VIRTUAL_FOLDER = doc + +# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to add. +# For more information please see +# http://doc.trolltech.com/qthelpproject.html#custom-filters + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the custom filter to add.For more information please see +# Qt Help Project / Custom Filters. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this project's +# filter section matches. +# Qt Help Project / Filter Attributes. + +QHP_SECT_FILTER_ATTRS = + +# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can +# be used to specify the location of Qt's qhelpgenerator. +# If non-empty doxygen will try to run qhelpgenerator on the generated +# .qhp file. + +QHG_LOCATION = + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. +# If the tag value is set to YES, a side panel will be generated +# containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser). +# Windows users are probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories, +# and Class Hierarchy pages using a tree view instead of an ordered list. + +USE_INLINE_TREES = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +# Use this tag to change the font size of Latex formulas included +# as images in the HTML documentation. The default is 10. Note that +# when you change the font size after a successful doxygen run you need +# to manually remove any form_*.png images from the HTML output directory +# to force them to be regenerated. + +FORMULA_FONTSIZE = 10 + +# When the SEARCHENGINE tag is enable doxygen will generate a search box +# for the HTML output. The underlying search engine uses javascript +# and DHTML and should work on any modern browser. Note that when using +# HTML help (GENERATE_HTMLHELP) or Qt help (GENERATE_QHP) +# there is already a search function so this one should typically +# be disabled. + +SEARCHENGINE = YES + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = NO + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = a4wide + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = YES + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = YES + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +# If LATEX_SOURCE_CODE is set to YES then doxygen will include +# source code with syntax highlighting in the LaTeX output. +# Note that which sources are shown also depends on other settings +# such as SOURCE_BROWSER. + +LATEX_SOURCE_CODE = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = NO + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = NO + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = NO + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# You can define message sequence charts within doxygen comments using the \msc +# command. Doxygen will then run the mscgen tool (see +# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the +# documentation. The MSCGEN_PATH tag allows you to specify the directory where +# the mscgen tool resides. If left empty the tool is assumed to be found in the +# default search path. + +MSCGEN_PATH = + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = NO + +# By default doxygen will write a font called FreeSans.ttf to the output +# directory and reference it in all dot files that doxygen generates. This +# font does not include all possible unicode characters however, so when you need +# these (or just want a differently looking font) you can specify the font name +# using DOT_FONTNAME. You need need to make sure dot is able to find the font, +# which can be done by putting it in a standard location or by setting the +# DOTFONTPATH environment variable or by setting DOT_FONTPATH to the directory +# containing the font. + +DOT_FONTNAME = FreeSans + +# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs. +# The default size is 10pt. + +DOT_FONTSIZE = 10 + +# By default doxygen will tell dot to use the output directory to look for the +# FreeSans.ttf font (which doxygen will put there itself). If you specify a +# different font using DOT_FONTNAME you can set the path where dot +# can find it using this tag. + +DOT_FONTPATH = + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT options are set to YES then +# doxygen will generate a call dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable call graphs +# for selected functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then +# doxygen will generate a caller dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable caller +# graphs for selected functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of +# nodes that will be shown in the graph. If the number of nodes in a graph +# becomes larger than this value, doxygen will truncate the graph, which is +# visualized by representing a node as a red box. Note that doxygen if the +# number of direct children of the root node in a graph is already larger than +# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note +# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. + +DOT_GRAPH_MAX_NODES = 50 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that the size of a graph can be further restricted by +# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, because dot on Windows does not +# seem to support this out of the box. Warning: Depending on the platform used, +# enabling this option may lead to badly anti-aliased labels on the edges of +# a graph (i.e. they become hard to read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES diff --git a/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp b/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aeabf8f97e0a2618fc4ab9ac4ed7cdcc61fa4042 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// AlignFeatures.cpp +// S.O. # : +// Author(s): zkira, mbansal, bsouthall, narodits +// $Id: AlignFeatures.cpp,v 1.20 2011/06/17 13:35:47 mbansal Exp $ + +#include +#include + +#include "trsMatrix.h" +#include "MatrixUtils.h" +#include "AlignFeatures.h" +#include "Log.h" + +#define LOG_TAG "AlignFeatures" + +Align::Align() +{ + width = height = 0; + frame_number = 0; + num_frames_captured = 0; + reference_frame_index = 0; + db_Identity3x3(Hcurr); + db_Identity3x3(Hprev); +} + +Align::~Align() +{ + // Free gray-scale image + if (imageGray != ImageUtils::IMAGE_TYPE_NOIMAGE) + ImageUtils::freeImage(imageGray); +} + +char* Align::getRegProfileString() +{ + return reg.profile_string; +} + +int Align::initialize(int width, int height, bool _quarter_res, float _thresh_still) +{ + int nr_corners = DEFAULT_NR_CORNERS; + double max_disparity = DEFAULT_MAX_DISPARITY; + int motion_model_type = DEFAULT_MOTION_MODEL; + int nrsamples = DB_DEFAULT_NR_SAMPLES; + double scale = DB_POINT_STANDARDDEV; + int chunk_size = DB_DEFAULT_CHUNK_SIZE; + int nrhorz = width/48; // Empirically determined number of horizontal + int nrvert = height/60; // and vertical buckets for harris corner detection. + bool linear_polish = false; + unsigned int reference_update_period = DEFAULT_REFERENCE_UPDATE_PERIOD; + + const bool DEFAULT_USE_SMALLER_MATCHING_WINDOW = false; + bool use_smaller_matching_window = DEFAULT_USE_SMALLER_MATCHING_WINDOW; + + quarter_res = _quarter_res; + thresh_still = _thresh_still; + + frame_number = 0; + num_frames_captured = 0; + reference_frame_index = 0; + db_Identity3x3(Hcurr); + db_Identity3x3(Hprev); + + if (!reg.Initialized()) + { + reg.Init(width, height, motion_model_type, 20, linear_polish, quarter_res, + scale, reference_update_period, false, 0, nrsamples, chunk_size, + nr_corners, max_disparity, use_smaller_matching_window, + nrhorz, nrvert); + } + this->width = width; + this->height = height; + + imageGray = ImageUtils::allocateImage(width, height, 1); + + if (reg.Initialized()) + return ALIGN_RET_OK; + else + return ALIGN_RET_ERROR; +} + +int Align::addFrameRGB(ImageType imageRGB) +{ + ImageUtils::rgb2gray(imageGray, imageRGB, width, height); + return addFrame(imageGray); +} + +int Align::addFrame(ImageType imageGray_) +{ + int ret_code = ALIGN_RET_OK; + + // Obtain a vector of pointers to rows in image and pass in to dbreg + ImageType *m_rows = ImageUtils::imageTypeToRowPointers(imageGray_, width, height); + + if (frame_number == 0) + { + reg.AddFrame(m_rows, Hcurr, true); // Force this to be a reference frame + int num_corner_ref = reg.GetNrRefCorners(); + + if (num_corner_ref < MIN_NR_REF_CORNERS) + { + return ALIGN_RET_LOW_TEXTURE; + } + } + else + { + reg.AddFrame(m_rows, Hcurr, false); + } + + // Average translation per frame = + // [Translation from Frame0 to Frame(n-1)] / [(n-1)] + average_tx_per_frame = (num_frames_captured < 2) ? 0.0 : + Hprev[2] / (num_frames_captured - 1); + + // Increment the captured frame counter if we already have a reference frame + num_frames_captured++; + + if (frame_number != 0) + { + int num_inliers = reg.GetNrInliers(); + + if(num_inliers < MIN_NR_INLIERS) + { + ret_code = ALIGN_RET_FEW_INLIERS; + + Hcurr[0] = 1.0; + Hcurr[1] = 0.0; + // Set this as the average per frame translation taking into acccount + // the separation of the current frame from the reference frame... + Hcurr[2] = -average_tx_per_frame * + (num_frames_captured - reference_frame_index); + Hcurr[3] = 0.0; + Hcurr[4] = 1.0; + Hcurr[5] = 0.0; + Hcurr[6] = 0.0; + Hcurr[7] = 0.0; + Hcurr[8] = 1.0; + } + + if(fabs(Hcurr[2]) + +#include "ImageUtils.h" +#include "MatrixUtils.h" + +class Align { + +public: + // Types of alignment possible + static const int ALIGN_TYPE_PAN = 1; + + // Return codes + static const int ALIGN_RET_LOW_TEXTURE = -2; + static const int ALIGN_RET_ERROR = -1; + static const int ALIGN_RET_OK = 0; + static const int ALIGN_RET_FEW_INLIERS = 1; + + ///// Settings for feature-based alignment + // Number of features to use from corner detection + static const int DEFAULT_NR_CORNERS=750; + static const double DEFAULT_MAX_DISPARITY=0.1;//0.4; + // Type of homography to model + static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_R_T; +// static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_PROJECTIVE; +// static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_AFFINE; + static const unsigned int DEFAULT_REFERENCE_UPDATE_PERIOD=1500; // Manual reference frame update so set this to a large number + + static const int MIN_NR_REF_CORNERS = 25; + static const int MIN_NR_INLIERS = 10; + + Align(); + ~Align(); + + // Initialization of structures, etc. + int initialize(int width, int height, bool quarter_res, float thresh_still); + + // Add a frame. Note: The alignment computation is performed + // in this function + int addFrameRGB(ImageType image); + int addFrame(ImageType image); + + // Obtain the TRS matrix from the last two frames + int getLastTRS(double trs[3][3]); + char* getRegProfileString(); + +protected: + + db_FrameToReferenceRegistration reg; + + int frame_number; + + double Hcurr[9]; // Homography from the alignment reference to the frame-t + double Hprev[9]; // Homography from frame-0 to the frame-(t-1) + + int reference_frame_index; // Index of the reference frame from all captured frames + int num_frames_captured; // Total number of frames captured (different from frame_number) + double average_tx_per_frame; // Average pixel translation per captured frame + + int width,height; + + bool quarter_res; // Whether to process at quarter resolution + float thresh_still; // Translation threshold in pixels to detect still camera + ImageType imageGray; +}; + + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Blend.cpp b/jni_mosaic/feature_mos/src/mosaic/Blend.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef983ff671ab87156e819705389d8b96472cbf98 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Blend.cpp @@ -0,0 +1,1410 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// Blend.cpp +// $Id: Blend.cpp,v 1.22 2011/06/24 04:22:14 mbansal Exp $ + +#include + +#include "Interp.h" +#include "Blend.h" + +#include "Geometry.h" +#include "trsMatrix.h" + +#include "Log.h" +#define LOG_TAG "BLEND" + +Blend::Blend() +{ + m_wb.blendingType = BLEND_TYPE_NONE; +} + +Blend::~Blend() +{ + if (m_pFrameVPyr) free(m_pFrameVPyr); + if (m_pFrameUPyr) free(m_pFrameUPyr); + if (m_pFrameYPyr) free(m_pFrameYPyr); +} + +int Blend::initialize(int blendingType, int stripType, int frame_width, int frame_height) +{ + this->width = frame_width; + this->height = frame_height; + this->m_wb.blendingType = blendingType; + this->m_wb.stripType = stripType; + + m_wb.blendRange = m_wb.blendRangeUV = BLEND_RANGE_DEFAULT; + m_wb.nlevs = m_wb.blendRange; + m_wb.nlevsC = m_wb.blendRangeUV; + + if (m_wb.nlevs <= 0) m_wb.nlevs = 1; // Need levels for YUV processing + if (m_wb.nlevsC > m_wb.nlevs) m_wb.nlevsC = m_wb.nlevs; + + m_wb.roundoffOverlap = 1.5; + + m_pFrameYPyr = NULL; + m_pFrameUPyr = NULL; + m_pFrameVPyr = NULL; + + m_pFrameYPyr = PyramidShort::allocatePyramidPacked(m_wb.nlevs, (unsigned short) width, (unsigned short) height, BORDER); + m_pFrameUPyr = PyramidShort::allocatePyramidPacked(m_wb.nlevsC, (unsigned short) (width), (unsigned short) (height), BORDER); + m_pFrameVPyr = PyramidShort::allocatePyramidPacked(m_wb.nlevsC, (unsigned short) (width), (unsigned short) (height), BORDER); + + if (!m_pFrameYPyr || !m_pFrameUPyr || !m_pFrameVPyr) + { + LOGE("Error: Could not allocate pyramids for blending"); + return BLEND_RET_ERROR_MEMORY; + } + + return BLEND_RET_OK; +} + +inline double max(double a, double b) { return a > b ? a : b; } +inline double min(double a, double b) { return a < b ? a : b; } + +void Blend::AlignToMiddleFrame(MosaicFrame **frames, int frames_size) +{ + // Unwarp this frame and Warp the others to match + MosaicFrame *mb = NULL; + MosaicFrame *ref = frames[int(frames_size/2)]; // Middle frame + + double invtrs[3][3]; + inv33d(ref->trs, invtrs); + + for(int mfit = 0; mfit < frames_size; mfit++) + { + mb = frames[mfit]; + double temp[3][3]; + mult33d(temp, invtrs, mb->trs); + memcpy(mb->trs, temp, sizeof(temp)); + normProjMat33d(mb->trs); + } +} + +int Blend::runBlend(MosaicFrame **oframes, MosaicFrame **rframes, + int frames_size, + ImageType &imageMosaicYVU, int &mosaicWidth, int &mosaicHeight, + float &progress, bool &cancelComputation) +{ + int ret; + int numCenters; + + MosaicFrame **frames; + + // For THIN strip mode, accept all frames for blending + if (m_wb.stripType == STRIP_TYPE_THIN) + { + frames = oframes; + } + else // For WIDE strip mode, first select the relevant frames to blend. + { + SelectRelevantFrames(oframes, frames_size, rframes, frames_size); + frames = rframes; + } + + ComputeBlendParameters(frames, frames_size, true); + numCenters = frames_size; + + if (numCenters == 0) + { + LOGE("Error: No frames to blend"); + return BLEND_RET_ERROR; + } + + if (!(m_AllSites = m_Triangulator.allocMemory(numCenters))) + { + return BLEND_RET_ERROR_MEMORY; + } + + // Bounding rectangle (real numbers) of the final mosaic computed by projecting + // each input frame into the mosaic coordinate system. + BlendRect global_rect; + + global_rect.lft = global_rect.bot = 2e30; // min values + global_rect.rgt = global_rect.top = -2e30; // max values + MosaicFrame *mb = NULL; + double halfwidth = width / 2.0; + double halfheight = height / 2.0; + + double z, x0, y0, x1, y1, x2, y2, x3, y3; + + // Corners of the left-most and right-most frames respectively in the + // mosaic coordinate system. + double xLeftCorners[2] = {2e30, 2e30}; + double xRightCorners[2] = {-2e30, -2e30}; + + // Corners of the top-most and bottom-most frames respectively in the + // mosaic coordinate system. + double yTopCorners[2] = {2e30, 2e30}; + double yBottomCorners[2] = {-2e30, -2e30}; + + + // Determine the extents of the final mosaic + CSite *csite = m_AllSites ; + for(int mfit = 0; mfit < frames_size; mfit++) + { + mb = frames[mfit]; + + // Compute clipping for this frame's rect + FrameToMosaicRect(mb->width, mb->height, mb->trs, mb->brect); + // Clip global rect using this frame's rect + ClipRect(mb->brect, global_rect); + + // Calculate the corner points + FrameToMosaic(mb->trs, 0.0, 0.0, x0, y0); + FrameToMosaic(mb->trs, 0.0, mb->height-1.0, x1, y1); + FrameToMosaic(mb->trs, mb->width-1.0, mb->height-1.0, x2, y2); + FrameToMosaic(mb->trs, mb->width-1.0, 0.0, x3, y3); + + if(x0 < xLeftCorners[0] || x1 < xLeftCorners[1]) // If either of the left corners is lower + { + xLeftCorners[0] = x0; + xLeftCorners[1] = x1; + } + + if(x3 > xRightCorners[0] || x2 > xRightCorners[1]) // If either of the right corners is higher + { + xRightCorners[0] = x3; + xRightCorners[1] = x2; + } + + if(y0 < yTopCorners[0] || y3 < yTopCorners[1]) // If either of the top corners is lower + { + yTopCorners[0] = y0; + yTopCorners[1] = y3; + } + + if(y1 > yBottomCorners[0] || y2 > yBottomCorners[1]) // If either of the bottom corners is higher + { + yBottomCorners[0] = y1; + yBottomCorners[1] = y2; + } + + + // Compute the centroid of the warped region + FindQuadCentroid(x0, y0, x1, y1, x2, y2, x3, y3, csite->getVCenter().x, csite->getVCenter().y); + + csite->setMb(mb); + csite++; + } + + // Get origin and sizes + + // Bounding rectangle (int numbers) of the final mosaic computed by projecting + // each input frame into the mosaic coordinate system. + MosaicRect fullRect; + + fullRect.left = (int) floor(global_rect.lft); // min-x + fullRect.top = (int) floor(global_rect.bot); // min-y + fullRect.right = (int) ceil(global_rect.rgt); // max-x + fullRect.bottom = (int) ceil(global_rect.top);// max-y + Mwidth = (unsigned short) (fullRect.right - fullRect.left + 1); + Mheight = (unsigned short) (fullRect.bottom - fullRect.top + 1); + + int xLeftMost, xRightMost; + int yTopMost, yBottomMost; + + // Rounding up, so that we don't include the gray border. + xLeftMost = max(0, max(xLeftCorners[0], xLeftCorners[1]) - fullRect.left + 1); + xRightMost = min(Mwidth - 1, min(xRightCorners[0], xRightCorners[1]) - fullRect.left - 1); + + yTopMost = max(0, max(yTopCorners[0], yTopCorners[1]) - fullRect.top + 1); + yBottomMost = min(Mheight - 1, min(yBottomCorners[0], yBottomCorners[1]) - fullRect.top - 1); + + if (xRightMost <= xLeftMost || yBottomMost <= yTopMost) + { + LOGE("RunBlend: aborting -consistency check failed," + "(xLeftMost, xRightMost, yTopMost, yBottomMost): (%d, %d, %d, %d)", + xLeftMost, xRightMost, yTopMost, yBottomMost); + return BLEND_RET_ERROR; + } + + // Make sure image width is multiple of 4 + Mwidth = (unsigned short) ((Mwidth + 3) & ~3); + Mheight = (unsigned short) ((Mheight + 3) & ~3); // Round up. + + ret = MosaicSizeCheck(LIMIT_SIZE_MULTIPLIER, LIMIT_HEIGHT_MULTIPLIER); + if (ret != BLEND_RET_OK) + { + LOGE("RunBlend: aborting - mosaic size check failed, " + "(frame_width, frame_height) vs (mosaic_width, mosaic_height): " + "(%d, %d) vs (%d, %d)", width, height, Mwidth, Mheight); + return ret; + } + + LOGI("Allocate mosaic image for blending - size: %d x %d", Mwidth, Mheight); + YUVinfo *imgMos = YUVinfo::allocateImage(Mwidth, Mheight); + if (imgMos == NULL) + { + LOGE("RunBlend: aborting - couldn't alloc %d x %d mosaic image", Mwidth, Mheight); + return BLEND_RET_ERROR_MEMORY; + } + + // Set the Y image to 255 so we can distinguish when frame idx are written to it + memset(imgMos->Y.ptr[0], 255, (imgMos->Y.width * imgMos->Y.height)); + // Set the v and u images to black + memset(imgMos->V.ptr[0], 128, (imgMos->V.width * imgMos->V.height) << 1); + + // Do the triangulation. It returns a sorted list of edges + SEdgeVector *edge; + int n = m_Triangulator.triangulate(&edge, numCenters, width, height); + m_Triangulator.linkNeighbors(edge, n, numCenters); + + // Bounding rectangle that determines the positioning of the rectangle that is + // cropped out of the computed mosaic to get rid of the gray borders. + MosaicRect cropping_rect; + + if (m_wb.horizontal) + { + cropping_rect.left = xLeftMost; + cropping_rect.right = xRightMost; + } + else + { + cropping_rect.top = yTopMost; + cropping_rect.bottom = yBottomMost; + } + + // Do merging and blending : + ret = DoMergeAndBlend(frames, numCenters, width, height, *imgMos, fullRect, + cropping_rect, progress, cancelComputation); + + if (m_wb.blendingType == BLEND_TYPE_HORZ) + CropFinalMosaic(*imgMos, cropping_rect); + + + m_Triangulator.freeMemory(); // note: can be called even if delaunay_alloc() wasn't successful + + imageMosaicYVU = imgMos->Y.ptr[0]; + + + if (m_wb.blendingType == BLEND_TYPE_HORZ) + { + mosaicWidth = cropping_rect.right - cropping_rect.left + 1; + mosaicHeight = cropping_rect.bottom - cropping_rect.top + 1; + } + else + { + mosaicWidth = Mwidth; + mosaicHeight = Mheight; + } + + return ret; +} + +int Blend::MosaicSizeCheck(float sizeMultiplier, float heightMultiplier) { + if (Mwidth < width || Mheight < height) { + return BLEND_RET_ERROR; + } + + if ((Mwidth * Mheight) > (width * height * sizeMultiplier)) { + return BLEND_RET_ERROR; + } + + // We won't do blending for the cases where users swing the device too much + // in the secondary direction. We use a short side to determine the + // secondary direction because users may hold the device in landsape + // or portrait. + int shortSide = min(Mwidth, Mheight); + if (shortSide > height * heightMultiplier) { + return BLEND_RET_ERROR; + } + + return BLEND_RET_OK; +} + +int Blend::FillFramePyramid(MosaicFrame *mb) +{ + ImageType mbY, mbU, mbV; + // Lay this image, centered into the temporary buffer + mbY = mb->image; + mbU = mb->getU(); + mbV = mb->getV(); + + int h, w; + + for(h=0; hptr[h]; + ImageTypeShort uptr = m_pFrameUPyr->ptr[h]; + ImageTypeShort vptr = m_pFrameVPyr->ptr[h]; + + for(w=0; wgetMb(); + + mb->vcrect = mb->brect; + ClipBlendRect(csite, mb->vcrect); + + ComputeMask(csite, mb->vcrect, mb->brect, rect, imgMos, site_idx); + + site_idx++; + } + + ////////// imgMos.Y, imgMos.V, imgMos.U are used as follows ////////////// + ////////////////////// THIN STRIP MODE /////////////////////////////////// + + // imgMos.Y is used to store the index of the image from which each pixel + // in the output mosaic can be read out for the thin-strip mode. Thus, + // there is no special handling for pixels around the seam. Also, imgMos.Y + // is set to 255 wherever we can't get its value from any input image e.g. + // in the gray border areas. imgMos.V and imgMos.U are set to 128 for the + // thin-strip mode. + + ////////////////////// WIDE STRIP MODE /////////////////////////////////// + + // imgMos.Y is used the same way as the thin-strip mode. + // imgMos.V is used to store the index of the neighboring image which + // should contribute to the color of an output pixel in a band around + // the seam. Thus, in this band, we will crossfade between the color values + // from the image index imgMos.Y and image index imgMos.V. imgMos.U is + // used to store the weight (multiplied by 100) that each image will + // contribute to the blending process. Thus, we start at 99% contribution + // from the first image, then go to 50% contribution from each image at + // the seam. Then, the contribution from the second image goes up to 99%. + + // For WIDE mode, set the pixel masks to guide the blender to cross-fade + // between the images on either side of each seam: + if (m_wb.stripType == STRIP_TYPE_WIDE) + { + if(m_wb.horizontal) + { + // Set the number of pixels around the seam to cross-fade between + // the two component images, + int tw = STRIP_CROSS_FADE_WIDTH_PXLS; + + // Proceed with the image index calculation for cross-fading + // only if the cross-fading width is larger than 0 + if (tw > 0) + { + for(int y = 0; y < imgMos.Y.height; y++) + { + // Since we compare two adjecant pixels to determine + // whether there is a seam, the termination condition of x + // is set to imgMos.Y.width - tw, so that x+1 below + // won't exceed the imgMos' boundary. + for(int x = tw; x < imgMos.Y.width - tw; ) + { + // Determine where the seam is... + if (imgMos.Y.ptr[y][x] != imgMos.Y.ptr[y][x+1] && + imgMos.Y.ptr[y][x] != 255 && + imgMos.Y.ptr[y][x+1] != 255) + { + // Find the image indices on both sides of the seam + unsigned char idx1 = imgMos.Y.ptr[y][x]; + unsigned char idx2 = imgMos.Y.ptr[y][x+1]; + + for (int o = tw; o >= 0; o--) + { + // Set the image index to use for cross-fading + imgMos.V.ptr[y][x - o] = idx2; + // Set the intensity weights to use for cross-fading + imgMos.U.ptr[y][x - o] = 50 + (99 - 50) * o / tw; + } + + for (int o = 1; o <= tw; o++) + { + // Set the image index to use for cross-fading + imgMos.V.ptr[y][x + o] = idx1; + // Set the intensity weights to use for cross-fading + imgMos.U.ptr[y][x + o] = imgMos.U.ptr[y][x - o]; + } + + x += (tw + 1); + } + else + { + x++; + } + } + } + } + } + else + { + // Set the number of pixels around the seam to cross-fade between + // the two component images, + int tw = STRIP_CROSS_FADE_WIDTH_PXLS; + + // Proceed with the image index calculation for cross-fading + // only if the cross-fading width is larger than 0 + if (tw > 0) + { + for(int x = 0; x < imgMos.Y.width; x++) + { + // Since we compare two adjecant pixels to determine + // whether there is a seam, the termination condition of y + // is set to imgMos.Y.height - tw, so that y+1 below + // won't exceed the imgMos' boundary. + for(int y = tw; y < imgMos.Y.height - tw; ) + { + // Determine where the seam is... + if (imgMos.Y.ptr[y][x] != imgMos.Y.ptr[y+1][x] && + imgMos.Y.ptr[y][x] != 255 && + imgMos.Y.ptr[y+1][x] != 255) + { + // Find the image indices on both sides of the seam + unsigned char idx1 = imgMos.Y.ptr[y][x]; + unsigned char idx2 = imgMos.Y.ptr[y+1][x]; + + for (int o = tw; o >= 0; o--) + { + // Set the image index to use for cross-fading + imgMos.V.ptr[y - o][x] = idx2; + // Set the intensity weights to use for cross-fading + imgMos.U.ptr[y - o][x] = 50 + (99 - 50) * o / tw; + } + + for (int o = 1; o <= tw; o++) + { + // Set the image index to use for cross-fading + imgMos.V.ptr[y + o][x] = idx1; + // Set the intensity weights to use for cross-fading + imgMos.U.ptr[y + o][x] = imgMos.U.ptr[y - o][x]; + } + + y += (tw + 1); + } + else + { + y++; + } + } + } + } + } + + } + + // Now perform the actual blending using the frame assignment determined above + site_idx = 0; + for(CSite *csite = m_AllSites; csite < esite; csite++) + { + if(cancelComputation) + { + if (m_pMosaicVPyr) free(m_pMosaicVPyr); + if (m_pMosaicUPyr) free(m_pMosaicUPyr); + if (m_pMosaicYPyr) free(m_pMosaicYPyr); + return BLEND_RET_CANCELLED; + } + + mb = csite->getMb(); + + + if(FillFramePyramid(mb)!=BLEND_RET_OK) + return BLEND_RET_ERROR; + + ProcessPyramidForThisFrame(csite, mb->vcrect, mb->brect, rect, imgMos, mb->trs, site_idx); + + progress += TIME_PERCENT_BLEND/nsite; + + site_idx++; + } + + + // Blend + PerformFinalBlending(imgMos, cropping_rect); + + if (cropping_rect.Width() <= 0 || cropping_rect.Height() <= 0) + { + LOGE("Size of the cropping_rect is invalid - (width, height): (%d, %d)", + cropping_rect.Width(), cropping_rect.Height()); + return BLEND_RET_ERROR; + } + + if (m_pMosaicVPyr) free(m_pMosaicVPyr); + if (m_pMosaicUPyr) free(m_pMosaicUPyr); + if (m_pMosaicYPyr) free(m_pMosaicYPyr); + + progress += TIME_PERCENT_FINAL; + + return BLEND_RET_OK; +} + +void Blend::CropFinalMosaic(YUVinfo &imgMos, MosaicRect &cropping_rect) +{ + int i, j, k; + ImageType yimg; + ImageType uimg; + ImageType vimg; + + + yimg = imgMos.Y.ptr[0]; + uimg = imgMos.U.ptr[0]; + vimg = imgMos.V.ptr[0]; + + k = 0; + for (j = cropping_rect.top; j <= cropping_rect.bottom; j++) + { + for (i = cropping_rect.left; i <= cropping_rect.right; i++) + { + yimg[k] = yimg[j*imgMos.Y.width+i]; + k++; + } + } + for (j = cropping_rect.top; j <= cropping_rect.bottom; j++) + { + for (i = cropping_rect.left; i <= cropping_rect.right; i++) + { + yimg[k] = vimg[j*imgMos.Y.width+i]; + k++; + } + } + for (j = cropping_rect.top; j <= cropping_rect.bottom; j++) + { + for (i = cropping_rect.left; i <= cropping_rect.right; i++) + { + yimg[k] = uimg[j*imgMos.Y.width+i]; + k++; + } + } +} + +int Blend::PerformFinalBlending(YUVinfo &imgMos, MosaicRect &cropping_rect) +{ + if (!PyramidShort::BorderExpand(m_pMosaicYPyr, m_wb.nlevs, 1) || !PyramidShort::BorderExpand(m_pMosaicUPyr, m_wb.nlevsC, 1) || + !PyramidShort::BorderExpand(m_pMosaicVPyr, m_wb.nlevsC, 1)) + { + LOGE("Error: Could not BorderExpand!"); + return BLEND_RET_ERROR; + } + + ImageTypeShort myimg; + ImageTypeShort muimg; + ImageTypeShort mvimg; + ImageType yimg; + ImageType uimg; + ImageType vimg; + + int cx = (int)imgMos.Y.width/2; + int cy = (int)imgMos.Y.height/2; + + // 2D boolean array that contains true wherever the mosaic image data is + // invalid (i.e. in the gray border). + bool **b = new bool*[imgMos.Y.height]; + + for(int j=0; jptr[j]; + muimg = m_pMosaicUPyr->ptr[j]; + mvimg = m_pMosaicVPyr->ptr[j]; + + for (i = 0; i> 3); + if (value < 0) value = 0; + else if (value > 255) value = 255; + *yimg = (unsigned char) value; + + value = (short) ((*muimg) >> 3); + if (value < 0) value = 0; + else if (value > 255) value = 255; + *uimg = (unsigned char) value; + + value = (short) ((*mvimg) >> 3); + if (value < 0) value = 0; + else if (value > 255) value = 255; + *vimg = (unsigned char) value; + + b[j][i] = false; + + } + else + { // set border color in here + *yimg = (unsigned char) 96; + *uimg = (unsigned char) 128; + *vimg = (unsigned char) 128; + + b[j][i] = true; + } + + yimg++; + uimg++; + vimg++; + myimg++; + muimg++; + mvimg++; + } + } + + if(m_wb.horizontal) + { + //Scan through each row and increment top if the row contains any gray + for (j = 0; j < imgMos.Y.height; j++) + { + for (i = cropping_rect.left; i < cropping_rect.right; i++) + { + if (b[j][i]) + { + break; // to next row + } + } + + if (i == cropping_rect.right) //no gray pixel in this row! + { + cropping_rect.top = j; + break; + } + } + + //Scan through each row and decrement bottom if the row contains any gray + for (j = imgMos.Y.height-1; j >= 0; j--) + { + for (i = cropping_rect.left; i < cropping_rect.right; i++) + { + if (b[j][i]) + { + break; // to next row + } + } + + if (i == cropping_rect.right) //no gray pixel in this row! + { + cropping_rect.bottom = j; + break; + } + } + } + else // Vertical Mosaic + { + //Scan through each column and increment left if the column contains any gray + for (i = 0; i < imgMos.Y.width; i++) + { + for (j = cropping_rect.top; j < cropping_rect.bottom; j++) + { + if (b[j][i]) + { + break; // to next column + } + } + + if (j == cropping_rect.bottom) //no gray pixel in this column! + { + cropping_rect.left = i; + break; + } + } + + //Scan through each column and decrement right if the column contains any gray + for (i = imgMos.Y.width-1; i >= 0; i--) + { + for (j = cropping_rect.top; j < cropping_rect.bottom; j++) + { + if (b[j][i]) + { + break; // to next column + } + } + + if (j == cropping_rect.bottom) //no gray pixel in this column! + { + cropping_rect.right = i; + break; + } + } + + } + + RoundingCroppingSizeToMultipleOf8(cropping_rect); + + for(int j=0; j= dptr->width) ? dptr->width + BORDER - 1 : r + BORDER; + else if (r >= dptr->width + BORDER) + r = dptr->width + BORDER - 1; + + if (vcrect.top == brect.top) + t = (t >= dptr->height) ? dptr->height + BORDER - 1 : t + BORDER; + else if (t >= dptr->height + BORDER) + t = dptr->height + BORDER - 1; + + // Walk the Region of interest and populate the pyramid + for (int j = b; j <= t; j++) + { + int jj = j; + double sj = jj + rect.top; + + for (int i = l; i <= r; i++) + { + int ii = i; + // project point and then triangulate to neighbors + double si = ii + rect.left; + + double dself = hypotSq(csite->getVCenter().x - si, csite->getVCenter().y - sj); + int inMask = ((unsigned) ii < imgMos.Y.width && + (unsigned) jj < imgMos.Y.height) ? 1 : 0; + + if(!inMask) + continue; + + // scan the neighbors to see if this is a valid position + unsigned char mask = (unsigned char) 255; + SEdgeVector *ce; + int ecnt; + for (ce = csite->getNeighbor(), ecnt = csite->getNumNeighbors(); ecnt--; ce++) + { + double d1 = hypotSq(m_AllSites[ce->second].getVCenter().x - si, + m_AllSites[ce->second].getVCenter().y - sj); + if (d1 < dself) + { + break; + } + } + + if (ecnt >= 0) continue; + + imgMos.Y.ptr[jj][ii] = (unsigned char)site_idx; + } + } +} + +void Blend::ProcessPyramidForThisFrame(CSite *csite, BlendRect &vcrect, BlendRect &brect, MosaicRect &rect, YUVinfo &imgMos, double trs[3][3], int site_idx) +{ + // Put the Region of interest (for all levels) into m_pMosaicYPyr + double inv_trs[3][3]; + inv33d(trs, inv_trs); + + // Process each pyramid level + PyramidShort *sptr = m_pFrameYPyr; + PyramidShort *suptr = m_pFrameUPyr; + PyramidShort *svptr = m_pFrameVPyr; + + PyramidShort *dptr = m_pMosaicYPyr; + PyramidShort *duptr = m_pMosaicUPyr; + PyramidShort *dvptr = m_pMosaicVPyr; + + int dscale = 0; // distance scale for the current level + int nC = m_wb.nlevsC; + for (int n = m_wb.nlevs; n--; dscale++, dptr++, sptr++, dvptr++, duptr++, svptr++, suptr++, nC--) + { + int l = (int) ((vcrect.lft - rect.left) / (1 << dscale)); + int b = (int) ((vcrect.bot - rect.top) / (1 << dscale)); + int r = (int) ((vcrect.rgt - rect.left) / (1 << dscale) + .5); + int t = (int) ((vcrect.top - rect.top) / (1 << dscale) + .5); + + if (vcrect.lft == brect.lft) + l = (l <= 0) ? -BORDER : l - BORDER; + else if (l < -BORDER) + l = -BORDER; + + if (vcrect.bot == brect.bot) + b = (b <= 0) ? -BORDER : b - BORDER; + else if (b < -BORDER) + b = -BORDER; + + if (vcrect.rgt == brect.rgt) + r = (r >= dptr->width) ? dptr->width + BORDER - 1 : r + BORDER; + else if (r >= dptr->width + BORDER) + r = dptr->width + BORDER - 1; + + if (vcrect.top == brect.top) + t = (t >= dptr->height) ? dptr->height + BORDER - 1 : t + BORDER; + else if (t >= dptr->height + BORDER) + t = dptr->height + BORDER - 1; + + // Walk the Region of interest and populate the pyramid + for (int j = b; j <= t; j++) + { + int jj = (j << dscale); + double sj = jj + rect.top; + + for (int i = l; i <= r; i++) + { + int ii = (i << dscale); + // project point and then triangulate to neighbors + double si = ii + rect.left; + + int inMask = ((unsigned) ii < imgMos.Y.width && + (unsigned) jj < imgMos.Y.height) ? 1 : 0; + + if(inMask && imgMos.Y.ptr[jj][ii] != site_idx && + imgMos.V.ptr[jj][ii] != site_idx && + imgMos.Y.ptr[jj][ii] != 255) + continue; + + // Setup weights for cross-fading + // Weight of the intensity already in the output pixel + double wt0 = 0.0; + // Weight of the intensity from the input pixel (current frame) + double wt1 = 1.0; + + if (m_wb.stripType == STRIP_TYPE_WIDE) + { + if(inMask && imgMos.Y.ptr[jj][ii] != 255) + { + // If not on a seam OR pyramid level exceeds + // maximum level for cross-fading. + if((imgMos.V.ptr[jj][ii] == 128) || + (dscale > STRIP_CROSS_FADE_MAX_PYR_LEVEL)) + { + wt0 = 0.0; + wt1 = 1.0; + } + else + { + wt0 = 1.0; + wt1 = ((imgMos.Y.ptr[jj][ii] == site_idx) ? + (double)imgMos.U.ptr[jj][ii] / 100.0 : + 1.0 - (double)imgMos.U.ptr[jj][ii] / 100.0); + } + } + } + + // Project this mosaic point into the original frame coordinate space + double xx, yy; + + MosaicToFrame(inv_trs, si, sj, xx, yy); + + if (xx < 0.0 || yy < 0.0 || xx > width - 1.0 || yy > height - 1.0) + { + if(inMask) + { + imgMos.Y.ptr[jj][ii] = 255; + wt0 = 0.0f; + wt1 = 1.0f; + } + } + + xx /= (1 << dscale); + yy /= (1 << dscale); + + + int x1 = (xx >= 0.0) ? (int) xx : (int) floor(xx); + int y1 = (yy >= 0.0) ? (int) yy : (int) floor(yy); + + // Final destination in extended pyramid +#ifndef LINEAR_INTERP + if(inSegment(x1, sptr->width, BORDER-1) && + inSegment(y1, sptr->height, BORDER-1)) + { + double xfrac = xx - x1; + double yfrac = yy - y1; + dptr->ptr[j][i] = (short) (wt0 * dptr->ptr[j][i] + .5 + + wt1 * ciCalc(sptr, x1, y1, xfrac, yfrac)); + if (dvptr >= m_pMosaicVPyr && nC > 0) + { + duptr->ptr[j][i] = (short) (wt0 * duptr->ptr[j][i] + .5 + + wt1 * ciCalc(suptr, x1, y1, xfrac, yfrac)); + dvptr->ptr[j][i] = (short) (wt0 * dvptr->ptr[j][i] + .5 + + wt1 * ciCalc(svptr, x1, y1, xfrac, yfrac)); + } + } +#else + if(inSegment(x1, sptr->width, BORDER) && inSegment(y1, sptr->height, BORDER)) + { + int x2 = x1 + 1; + int y2 = y1 + 1; + double xfrac = xx - x1; + double yfrac = yy - y1; + double y1val = sptr->ptr[y1][x1] + + (sptr->ptr[y1][x2] - sptr->ptr[y1][x1]) * xfrac; + double y2val = sptr->ptr[y2][x1] + + (sptr->ptr[y2][x2] - sptr->ptr[y2][x1]) * xfrac; + dptr->ptr[j][i] = (short) (y1val + yfrac * (y2val - y1val)); + + if (dvptr >= m_pMosaicVPyr && nC > 0) + { + y1val = suptr->ptr[y1][x1] + + (suptr->ptr[y1][x2] - suptr->ptr[y1][x1]) * xfrac; + y2val = suptr->ptr[y2][x1] + + (suptr->ptr[y2][x2] - suptr->ptr[y2][x1]) * xfrac; + + duptr->ptr[j][i] = (short) (y1val + yfrac * (y2val - y1val)); + + y1val = svptr->ptr[y1][x1] + + (svptr->ptr[y1][x2] - svptr->ptr[y1][x1]) * xfrac; + y2val = svptr->ptr[y2][x1] + + (svptr->ptr[y2][x2] - svptr->ptr[y2][x1]) * xfrac; + + dvptr->ptr[j][i] = (short) (y1val + yfrac * (y2val - y1val)); + } + } +#endif + else + { + clipToSegment(x1, sptr->width, BORDER); + clipToSegment(y1, sptr->height, BORDER); + + dptr->ptr[j][i] = (short) (wt0 * dptr->ptr[j][i] + 0.5 + + wt1 * sptr->ptr[y1][x1] ); + if (dvptr >= m_pMosaicVPyr && nC > 0) + { + dvptr->ptr[j][i] = (short) (wt0 * dvptr->ptr[j][i] + + 0.5 + wt1 * svptr->ptr[y1][x1] ); + duptr->ptr[j][i] = (short) (wt0 * duptr->ptr[j][i] + + 0.5 + wt1 * suptr->ptr[y1][x1] ); + } + } + } + } + } +} + +void Blend::MosaicToFrame(double trs[3][3], double x, double y, double &wx, double &wy) +{ + double X, Y, z; + if (m_wb.theta == 0.0) + { + X = x; + Y = y; + } + else if (m_wb.horizontal) + { + double alpha = x * m_wb.direction / m_wb.width; + double length = (y - alpha * m_wb.correction) * m_wb.direction + m_wb.radius; + double deltaTheta = m_wb.theta * alpha; + double sinTheta = sin(deltaTheta); + double cosTheta = sqrt(1.0 - sinTheta * sinTheta) * m_wb.direction; + X = length * sinTheta + m_wb.x; + Y = length * cosTheta + m_wb.y; + } + else + { + double alpha = y * m_wb.direction / m_wb.width; + double length = (x - alpha * m_wb.correction) * m_wb.direction + m_wb.radius; + double deltaTheta = m_wb.theta * alpha; + double sinTheta = sin(deltaTheta); + double cosTheta = sqrt(1.0 - sinTheta * sinTheta) * m_wb.direction; + Y = length * sinTheta + m_wb.y; + X = length * cosTheta + m_wb.x; + } + z = ProjZ(trs, X, Y, 1.0); + wx = ProjX(trs, X, Y, z, 1.0); + wy = ProjY(trs, X, Y, z, 1.0); +} + +void Blend::FrameToMosaic(double trs[3][3], double x, double y, double &wx, double &wy) +{ + // Project into the intermediate Mosaic coordinate system + double z = ProjZ(trs, x, y, 1.0); + double X = ProjX(trs, x, y, z, 1.0); + double Y = ProjY(trs, x, y, z, 1.0); + + if (m_wb.theta == 0.0) + { + // No rotation, then this is all we need to do. + wx = X; + wy = Y; + } + else if (m_wb.horizontal) + { + double deltaX = X - m_wb.x; + double deltaY = Y - m_wb.y; + double length = sqrt(deltaX * deltaX + deltaY * deltaY); + double deltaTheta = asin(deltaX / length); + double alpha = deltaTheta / m_wb.theta; + wx = alpha * m_wb.width * m_wb.direction; + wy = (length - m_wb.radius) * m_wb.direction + alpha * m_wb.correction; + } + else + { + double deltaX = X - m_wb.x; + double deltaY = Y - m_wb.y; + double length = sqrt(deltaX * deltaX + deltaY * deltaY); + double deltaTheta = asin(deltaY / length); + double alpha = deltaTheta / m_wb.theta; + wy = alpha * m_wb.width * m_wb.direction; + wx = (length - m_wb.radius) * m_wb.direction + alpha * m_wb.correction; + } +} + + + +// Clip the region of interest as small as possible by using the Voronoi edges of +// the neighbors +void Blend::ClipBlendRect(CSite *csite, BlendRect &brect) +{ + SEdgeVector *ce; + int ecnt; + for (ce = csite->getNeighbor(), ecnt = csite->getNumNeighbors(); ecnt--; ce++) + { + // calculate the Voronoi bisector intersection + const double epsilon = 1e-5; + double dx = (m_AllSites[ce->second].getVCenter().x - m_AllSites[ce->first].getVCenter().x); + double dy = (m_AllSites[ce->second].getVCenter().y - m_AllSites[ce->first].getVCenter().y); + double xmid = m_AllSites[ce->first].getVCenter().x + dx/2.0; + double ymid = m_AllSites[ce->first].getVCenter().y + dy/2.0; + double inter; + + if (dx > epsilon) + { + // neighbor is on right + if ((inter = m_wb.roundoffOverlap + xmid - dy * (((dy >= 0.0) ? brect.bot : brect.top) - ymid) / dx) < brect.rgt) + brect.rgt = inter; + } + else if (dx < -epsilon) + { + // neighbor is on left + if ((inter = -m_wb.roundoffOverlap + xmid - dy * (((dy >= 0.0) ? brect.bot : brect.top) - ymid) / dx) > brect.lft) + brect.lft = inter; + } + if (dy > epsilon) + { + // neighbor is above + if ((inter = m_wb.roundoffOverlap + ymid - dx * (((dx >= 0.0) ? brect.lft : brect.rgt) - xmid) / dy) < brect.top) + brect.top = inter; + } + else if (dy < -epsilon) + { + // neighbor is below + if ((inter = -m_wb.roundoffOverlap + ymid - dx * (((dx >= 0.0) ? brect.lft : brect.rgt) - xmid) / dy) > brect.bot) + brect.bot = inter; + } + } +} + +void Blend::FrameToMosaicRect(int width, int height, double trs[3][3], BlendRect &brect) +{ + // We need to walk the perimeter since the borders can be bent. + brect.lft = brect.bot = 2e30; + brect.rgt = brect.top = -2e30; + double xpos, ypos; + double lasty = height - 1.0; + double lastx = width - 1.0; + int i; + + for (i = width; i--;) + { + + FrameToMosaic(trs, (double) i, 0.0, xpos, ypos); + ClipRect(xpos, ypos, brect); + FrameToMosaic(trs, (double) i, lasty, xpos, ypos); + ClipRect(xpos, ypos, brect); + } + for (i = height; i--;) + { + FrameToMosaic(trs, 0.0, (double) i, xpos, ypos); + ClipRect(xpos, ypos, brect); + FrameToMosaic(trs, lastx, (double) i, xpos, ypos); + ClipRect(xpos, ypos, brect); + } +} + +void Blend::SelectRelevantFrames(MosaicFrame **frames, int frames_size, + MosaicFrame **relevant_frames, int &relevant_frames_size) +{ + MosaicFrame *first = frames[0]; + MosaicFrame *last = frames[frames_size-1]; + MosaicFrame *mb; + + double fxpos = first->trs[0][2], fypos = first->trs[1][2]; + + double midX = last->width / 2.0; + double midY = last->height / 2.0; + double z = ProjZ(first->trs, midX, midY, 1.0); + double firstX, firstY; + double prevX = firstX = ProjX(first->trs, midX, midY, z, 1.0); + double prevY = firstY = ProjY(first->trs, midX, midY, z, 1.0); + + relevant_frames[0] = first; // Add first frame by default + relevant_frames_size = 1; + + for (int i = 0; i < frames_size - 1; i++) + { + mb = frames[i]; + double currX, currY; + z = ProjZ(mb->trs, midX, midY, 1.0); + currX = ProjX(mb->trs, midX, midY, z, 1.0); + currY = ProjY(mb->trs, midX, midY, z, 1.0); + double deltaX = currX - prevX; + double deltaY = currY - prevY; + double center2centerDist = sqrt(deltaY * deltaY + deltaX * deltaX); + + if (fabs(deltaX) > STRIP_SEPARATION_THRESHOLD_PXLS || + fabs(deltaY) > STRIP_SEPARATION_THRESHOLD_PXLS) + { + relevant_frames[relevant_frames_size] = mb; + relevant_frames_size++; + + prevX = currX; + prevY = currY; + } + } + + // Add last frame by default + relevant_frames[relevant_frames_size] = last; + relevant_frames_size++; +} + +void Blend::ComputeBlendParameters(MosaicFrame **frames, int frames_size, int is360) +{ + // For FULL and PAN modes, we do not unwarp the mosaic into a rectangular coordinate system + // and so we set the theta to 0 and return. + if (m_wb.blendingType != BLEND_TYPE_CYLPAN && m_wb.blendingType != BLEND_TYPE_HORZ) + { + m_wb.theta = 0.0; + return; + } + + MosaicFrame *first = frames[0]; + MosaicFrame *last = frames[frames_size-1]; + MosaicFrame *mb; + + double lxpos = last->trs[0][2], lypos = last->trs[1][2]; + double fxpos = first->trs[0][2], fypos = first->trs[1][2]; + + // Calculate warp to produce proper stitching. + // get x, y displacement + double midX = last->width / 2.0; + double midY = last->height / 2.0; + double z = ProjZ(first->trs, midX, midY, 1.0); + double firstX, firstY; + double prevX = firstX = ProjX(first->trs, midX, midY, z, 1.0); + double prevY = firstY = ProjY(first->trs, midX, midY, z, 1.0); + + double arcLength, lastTheta; + m_wb.theta = lastTheta = arcLength = 0.0; + + // Step through all the frames to compute the total arc-length of the cone + // swept while capturing the mosaic (in the original conical coordinate system). + for (int i = 0; i < frames_size; i++) + { + mb = frames[i]; + double currX, currY; + z = ProjZ(mb->trs, midX, midY, 1.0); + currX = ProjX(mb->trs, midX, midY, z, 1.0); + currY = ProjY(mb->trs, midX, midY, z, 1.0); + double deltaX = currX - prevX; + double deltaY = currY - prevY; + + // The arcLength is computed by summing the lengths of the chords + // connecting the pairwise projected image centers of the input image frames. + arcLength += sqrt(deltaY * deltaY + deltaX * deltaX); + + if (!is360) + { + double thisTheta = asin(mb->trs[1][0]); + m_wb.theta += thisTheta - lastTheta; + lastTheta = thisTheta; + } + + prevX = currX; + prevY = currY; + } + + // Stretch this to end at the proper alignment i.e. the width of the + // rectangle is determined by the arcLength computed above and the cone + // sector angle is determined using the rotation of the last frame. + m_wb.width = arcLength; + if (is360) m_wb.theta = asin(last->trs[1][0]); + + // If there is no rotation, we're done. + if (m_wb.theta != 0.0) + { + double dx = prevX - firstX; + double dy = prevY - firstY; + + // If the mosaic was captured by sweeping horizontally + if (abs(lxpos - fxpos) > abs(lypos - fypos)) + { + m_wb.horizontal = 1; + // Calculate radius position to make ends exactly the same Y offset + double radiusTheta = dx / cos(3.14159 / 2.0 - m_wb.theta); + m_wb.radius = dy + radiusTheta * cos(m_wb.theta); + if (m_wb.radius < 0.0) m_wb.radius = -m_wb.radius; + } + else + { + m_wb.horizontal = 0; + // Calculate radius position to make ends exactly the same Y offset + double radiusTheta = dy / cos(3.14159 / 2.0 - m_wb.theta); + m_wb.radius = dx + radiusTheta * cos(m_wb.theta); + if (m_wb.radius < 0.0) m_wb.radius = -m_wb.radius; + } + + // Determine major direction + if (m_wb.horizontal) + { + // Horizontal strip + // m_wb.x,y record the origin of the rectangle coordinate system. + if (is360) m_wb.x = firstX; + else + { + if (lxpos - fxpos < 0) + { + m_wb.x = firstX + midX; + z = ProjZ(last->trs, 0.0, midY, 1.0); + prevX = ProjX(last->trs, 0.0, midY, z, 1.0); + prevY = ProjY(last->trs, 0.0, midY, z, 1.0); + } + else + { + m_wb.x = firstX - midX; + z = ProjZ(last->trs, last->width - 1.0, midY, 1.0); + prevX = ProjX(last->trs, last->width - 1.0, midY, z, 1.0); + prevY = ProjY(last->trs, last->width - 1.0, midY, z, 1.0); + } + } + dy = prevY - firstY; + if (dy < 0.0) m_wb.direction = 1.0; + else m_wb.direction = -1.0; + m_wb.y = firstY - m_wb.radius * m_wb.direction; + if (dy * m_wb.theta > 0.0) m_wb.width = -m_wb.width; + } + else + { + // Vertical strip + if (is360) m_wb.y = firstY; + else + { + if (lypos - fypos < 0) + { + m_wb.x = firstY + midY; + z = ProjZ(last->trs, midX, 0.0, 1.0); + prevX = ProjX(last->trs, midX, 0.0, z, 1.0); + prevY = ProjY(last->trs, midX, 0.0, z, 1.0); + } + else + { + m_wb.x = firstX - midX; + z = ProjZ(last->trs, midX, last->height - 1.0, 1.0); + prevX = ProjX(last->trs, midX, last->height - 1.0, z, 1.0); + prevY = ProjY(last->trs, midX, last->height - 1.0, z, 1.0); + } + } + dx = prevX - firstX; + if (dx < 0.0) m_wb.direction = 1.0; + else m_wb.direction = -1.0; + m_wb.x = firstX - m_wb.radius * m_wb.direction; + if (dx * m_wb.theta > 0.0) m_wb.width = -m_wb.width; + } + + // Calculate the correct correction factor + double deltaX = prevX - m_wb.x; + double deltaY = prevY - m_wb.y; + double length = sqrt(deltaX * deltaX + deltaY * deltaY); + double deltaTheta = (m_wb.horizontal) ? deltaX : deltaY; + deltaTheta = asin(deltaTheta / length); + m_wb.correction = ((m_wb.radius - length) * m_wb.direction) / + (deltaTheta / m_wb.theta); + } +} diff --git a/jni_mosaic/feature_mos/src/mosaic/Blend.h b/jni_mosaic/feature_mos/src/mosaic/Blend.h new file mode 100644 index 0000000000000000000000000000000000000000..2c7ee5c5f1ca750665fb00a8f7952cf22a8dbdbd --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Blend.h @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// Blend.h +// $Id: Blend.h,v 1.23 2011/06/24 04:22:14 mbansal Exp $ + +#ifndef BLEND_H +#define BLEND_H + +#include "MosaicTypes.h" +#include "Pyramid.h" +#include "Delaunay.h" + +#define BLEND_RANGE_DEFAULT 6 +#define BORDER 8 + +// Percent of total mosaicing time spent on each of the following operations +const float TIME_PERCENT_ALIGN = 20.0; +const float TIME_PERCENT_BLEND = 75.0; +const float TIME_PERCENT_FINAL = 5.0; + +// This threshold determines the minimum separation between the image centers +// of the input image frames for them to be accepted for blending in the +// STRIP_TYPE_WIDE mode. +const float STRIP_SEPARATION_THRESHOLD_PXLS = 10; + +// This threshold determines the number of pixels on either side of the strip +// to cross-fade using the images contributing to each seam. +const float STRIP_CROSS_FADE_WIDTH_PXLS = 2; +// This specifies the maximum pyramid level to which cross-fading is applied. +// The original image resolution is Level-0, half of that size is Level-1 and +// so on. BLEND_RANGE_DEFAULT specifies the number of pyramid levels used by +// the blending algorithm. +const int STRIP_CROSS_FADE_MAX_PYR_LEVEL = 2; + +/** + * Class for pyramid blending a mosaic. + */ +class Blend { + +public: + + static const int BLEND_TYPE_NONE = -1; + static const int BLEND_TYPE_FULL = 0; + static const int BLEND_TYPE_PAN = 1; + static const int BLEND_TYPE_CYLPAN = 2; + static const int BLEND_TYPE_HORZ = 3; + + static const int STRIP_TYPE_THIN = 0; + static const int STRIP_TYPE_WIDE = 1; + + static const int BLEND_RET_ERROR = -1; + static const int BLEND_RET_OK = 0; + static const int BLEND_RET_ERROR_MEMORY = 1; + static const int BLEND_RET_CANCELLED = -2; + + Blend(); + ~Blend(); + + int initialize(int blendingType, int stripType, int frame_width, int frame_height); + + int runBlend(MosaicFrame **frames, MosaicFrame **rframes, int frames_size, ImageType &imageMosaicYVU, + int &mosaicWidth, int &mosaicHeight, float &progress, bool &cancelComputation); + +protected: + + PyramidShort *m_pFrameYPyr; + PyramidShort *m_pFrameUPyr; + PyramidShort *m_pFrameVPyr; + + PyramidShort *m_pMosaicYPyr; + PyramidShort *m_pMosaicUPyr; + PyramidShort *m_pMosaicVPyr; + + CDelaunay m_Triangulator; + CSite *m_AllSites; + + BlendParams m_wb; + + // Height and width of individual frames + int width, height; + + // Height and width of mosaic + unsigned short Mwidth, Mheight; + + // Helper functions + void FrameToMosaic(double trs[3][3], double x, double y, double &wx, double &wy); + void MosaicToFrame(double trs[3][3], double x, double y, double &wx, double &wy); + void FrameToMosaicRect(int width, int height, double trs[3][3], BlendRect &brect); + void ClipBlendRect(CSite *csite, BlendRect &brect); + void AlignToMiddleFrame(MosaicFrame **frames, int frames_size); + + int DoMergeAndBlend(MosaicFrame **frames, int nsite, int width, int height, YUVinfo &imgMos, MosaicRect &rect, MosaicRect &cropping_rect, float &progress, bool &cancelComputation); + void ComputeMask(CSite *csite, BlendRect &vcrect, BlendRect &brect, MosaicRect &rect, YUVinfo &imgMos, int site_idx); + void ProcessPyramidForThisFrame(CSite *csite, BlendRect &vcrect, BlendRect &brect, MosaicRect &rect, YUVinfo &imgMos, double trs[3][3], int site_idx); + + int FillFramePyramid(MosaicFrame *mb); + + // TODO: need to add documentation about the parameters + void ComputeBlendParameters(MosaicFrame **frames, int frames_size, int is360); + void SelectRelevantFrames(MosaicFrame **frames, int frames_size, + MosaicFrame **relevant_frames, int &relevant_frames_size); + + int PerformFinalBlending(YUVinfo &imgMos, MosaicRect &cropping_rect); + void CropFinalMosaic(YUVinfo &imgMos, MosaicRect &cropping_rect); + +private: + static const float LIMIT_SIZE_MULTIPLIER = 5.0f * 2.0f; + static const float LIMIT_HEIGHT_MULTIPLIER = 2.5f; + int MosaicSizeCheck(float sizeMultiplier, float heightMultiplier); + void RoundingCroppingSizeToMultipleOf8(MosaicRect& rect); +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/CSite.h b/jni_mosaic/feature_mos/src/mosaic/CSite.h new file mode 100644 index 0000000000000000000000000000000000000000..928c1734bee2d63e205d4daf413de4bfd1f855c9 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/CSite.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// CSite.h +// $Id: CSite.h,v 1.3 2011/06/17 13:35:47 mbansal Exp $ + +#ifndef TRIDEL_H +#define TRIDEL_H + +#include "MosaicTypes.h" + +typedef struct +{ + short first; + short second; +} SEdgeVector; + +typedef struct +{ + double x; + double y; +} SVec2d; + +class CSite +{ +private: + MosaicFrame *mosaicFrame; + SEdgeVector *neighbor; + int numNeighbors; + SVec2d voronoiCenter; + +public: + CSite(); + ~CSite(); + + inline MosaicFrame* getMb() { return mosaicFrame; } + inline SEdgeVector* getNeighbor() { return neighbor; } + inline int getNumNeighbors() { return numNeighbors; } + inline SVec2d& getVCenter() { return voronoiCenter; } + inline double X() { return voronoiCenter.x; } + inline double Y() { return voronoiCenter.y; } + + inline void incrNumNeighbors() { numNeighbors++; } + inline void setNumNeighbors(int num) { numNeighbors = num; } + inline void setNeighbor(SEdgeVector *nb) { neighbor = nb; } + inline void setMb(MosaicFrame *mb) { mosaicFrame = mb; } +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Delaunay.cpp b/jni_mosaic/feature_mos/src/mosaic/Delaunay.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ce09fc5104cb58bec452fececd3a5319b52949a --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Delaunay.cpp @@ -0,0 +1,633 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Delaunay.cpp +// $Id: Delaunay.cpp,v 1.10 2011/06/17 13:35:48 mbansal Exp $ + +#include +#include +#include +#include "Delaunay.h" + +#define QQ 9 // Optimal value as determined by testing +#define DM 38 // 2^(1+DM/2) element sort capability. DM=38 for >10^6 elements +#define NYL -1 +#define valid(l) ccw(orig(basel), dest(l), dest(basel)) + + +CDelaunay::CDelaunay() +{ +} + +CDelaunay::~CDelaunay() +{ +} + +// Allocate storage, construct triangulation, compute voronoi corners +int CDelaunay::triangulate(SEdgeVector **edges, int n_sites, int width, int height) +{ + EdgePointer cep; + + deleteAllEdges(); + buildTriangulation(n_sites); + cep = consolidateEdges(); + *edges = ev; + + // Note: construction_list will change ev + return constructList(cep, width, height); +} + +// builds delaunay triangulation +void CDelaunay::buildTriangulation(int size) +{ + int i, rows; + EdgePointer lefte, righte; + + rows = (int)( 0.5 + sqrt( (double) size / log( (double) size ))); + + // Sort the pointers by x-coordinate of site + for ( i=0 ; i < size ; i++ ) { + sp[i] = (SitePointer) i; + } + + spsortx( sp, 0, size-1 ); + build( 0, size-1, &lefte, &righte, rows ); + oneBndryEdge = lefte; +} + +// Recursive Delaunay Triangulation Procedure +// Contains modifications for axis-switching division. +void CDelaunay::build(int lo, int hi, EdgePointer *le, EdgePointer *re, int rows) +{ + EdgePointer a, b, c, ldo, rdi, ldi, rdo, maxx, minx; + int split, lowrows; + int low, high; + SitePointer s1, s2, s3; + low = lo; + high = hi; + + if ( low < (high-2) ) { + // more than three elements; do recursion + minx = sp[low]; + maxx = sp[high]; + if (rows == 1) { // time to switch axis of division + spsorty( sp, low, high); + rows = 65536; + } + lowrows = rows/2; + split = low - 1 + (int) + (0.5 + ((double)(high-low+1) * ((double)lowrows / (double)rows))); + build( low, split, &ldo, &ldi, lowrows ); + build( split+1, high, &rdi, &rdo, (rows-lowrows) ); + doMerge(&ldo, ldi, rdi, &rdo); + while (orig(ldo) != minx) { + ldo = rprev(ldo); + } + while (orig(rdo) != maxx) { + rdo = (SitePointer) lprev(rdo); + } + *le = ldo; + *re = rdo; + } + else if (low >= (high - 1)) { // two or one points + a = makeEdge(sp[low], sp[high]); + *le = a; + *re = (EdgePointer) sym(a); + } else { // three points + // 3 cases: triangles of 2 orientations, and 3 points on a line + a = makeEdge((s1 = sp[low]), (s2 = sp[low+1])); + b = makeEdge(s2, (s3 = sp[high])); + splice((EdgePointer) sym(a), b); + if (ccw(s1, s3, s2)) { + c = connectLeft(b, a); + *le = (EdgePointer) sym(c); + *re = c; + } else { + *le = a; + *re = (EdgePointer) sym(b); + if (ccw(s1, s2, s3)) { + // not colinear + c = connectLeft(b, a); + } + } + } +} + +// Quad-edge manipulation primitives +EdgePointer CDelaunay::makeEdge(SitePointer origin, SitePointer destination) +{ + EdgePointer temp, ans; + temp = allocEdge(); + ans = temp; + + onext(temp) = ans; + orig(temp) = origin; + onext(++temp) = (EdgePointer) (ans + 3); + onext(++temp) = (EdgePointer) (ans + 2); + orig(temp) = destination; + onext(++temp) = (EdgePointer) (ans + 1); + + return(ans); +} + +void CDelaunay::splice(EdgePointer a, EdgePointer b) +{ + EdgePointer alpha, beta, temp; + alpha = (EdgePointer) rot(onext(a)); + beta = (EdgePointer) rot(onext(b)); + temp = onext(alpha); + onext(alpha) = onext(beta); + onext(beta) = temp; + temp = onext(a); + onext(a) = onext(b); + onext(b) = temp; +} + +EdgePointer CDelaunay::connectLeft(EdgePointer a, EdgePointer b) +{ + EdgePointer ans; + ans = makeEdge(dest(a), orig(b)); + splice(ans, (EdgePointer) lnext(a)); + splice((EdgePointer) sym(ans), b); + return(ans); +} + +EdgePointer CDelaunay::connectRight(EdgePointer a, EdgePointer b) +{ + EdgePointer ans; + ans = makeEdge(dest(a), orig(b)); + splice(ans, (EdgePointer) sym(a)); + splice((EdgePointer) sym(ans), (EdgePointer) oprev(b)); + return(ans); +} + +// disconnects e from the rest of the structure and destroys it +void CDelaunay::deleteEdge(EdgePointer e) +{ + splice(e, (EdgePointer) oprev(e)); + splice((EdgePointer) sym(e), (EdgePointer) oprev(sym(e))); + freeEdge(e); +} + +// +// Overall storage allocation +// + +// Quad-edge storage allocation +CSite *CDelaunay::allocMemory(int n) +{ + unsigned int size; + + size = ((sizeof(CSite) + sizeof(SitePointer)) * n + + (sizeof(SitePointer) + sizeof(EdgePointer)) * 12 + ) * n; + if (!(sa = (CSite*) malloc(size))) { + return NULL; + } + sp = (SitePointer *) (sa + n); + ev = (SEdgeVector *) (org = sp + n); + next = (EdgePointer *) (org + 12 * n); + ei = (struct EDGE_INFO *) (next + 12 * n); + return sa; +} + +void CDelaunay::freeMemory() +{ + if (sa) { + free(sa); + sa = (CSite*)NULL; + } +} + +// +// Edge storage management +// + +void CDelaunay::deleteAllEdges() +{ + nextEdge = 0; + availEdge = NYL; +} + +EdgePointer CDelaunay::allocEdge() +{ + EdgePointer ans; + + if (availEdge == NYL) { + ans = nextEdge, nextEdge += 4; + } else { + ans = availEdge, availEdge = onext(availEdge); + } + return(ans); +} + +void CDelaunay::freeEdge(EdgePointer e) +{ + e ^= e & 3; + onext(e) = availEdge; + availEdge = e; +} + +EdgePointer CDelaunay::consolidateEdges() +{ + EdgePointer e; + int i,j; + + while (availEdge != NYL) { + nextEdge -= 4; e = availEdge; availEdge = onext(availEdge); + + if (e==nextEdge) { + continue; // the one deleted was the last one anyway + } + if ((oneBndryEdge&~3) == nextEdge) { + oneBndryEdge = (EdgePointer) (e | (oneBndryEdge&3)); + } + for (i=0,j=3; i<4; i++,j=rot(j)) { + onext(e+i) = onext(nextEdge+i); + onext(rot(onext(e+i))) = (EdgePointer) (e+j); + } + } + return nextEdge; +} + +// +// Sorting Routines +// + +int CDelaunay::xcmpsp(int i, int j) +{ + double d = sa[(i>=0)?sp[i]:sp1].X() - sa[(j>=0)?sp[j]:sp1].X(); + if ( d > 0. ) { + return 1; + } + if ( d < 0. ) { + return -1; + } + d = sa[(i>=0)?sp[i]:sp1].Y() - sa[(j>=0)?sp[j]:sp1].Y(); + if ( d > 0. ) { + return 1; + } + if ( d < 0. ) { + return -1; + } + return 0; +} + +int CDelaunay::ycmpsp(int i, int j) +{ + double d = sa[(i>=0)?sp[i]:sp1].Y() - sa[(j>=0)?sp[j]:sp1].Y(); + if ( d > 0. ) { + return 1; + } + if ( d < 0. ) { + return -1; + } + d = sa[(i>=0)?sp[i]:sp1].X() - sa[(j>=0)?sp[j]:sp1].X(); + if ( d > 0. ) { + return 1; + } + if ( d < 0. ) { + return -1; + } + return 0; +} + +int CDelaunay::cmpev(int i, int j) +{ + return (ev[i].first - ev[j].first); +} + +void CDelaunay::swapsp(int i, int j) +{ + int t; + t = (i>=0) ? sp[i] : sp1; + + if (i>=0) { + sp[i] = (j>=0)?sp[j]:sp1; + } else { + sp1 = (j>=0)?sp[j]:sp1; + } + + if (j>=0) { + sp[j] = (SitePointer) t; + } else { + sp1 = (SitePointer) t; + } +} + +void CDelaunay::swapev(int i, int j) +{ + SEdgeVector temp; + + temp = ev[i]; + ev[i] = ev[j]; + ev[j] = temp; +} + +void CDelaunay::copysp(int i, int j) +{ + if (j>=0) { + sp[j] = (i>=0)?sp[i]:sp1; + } else { + sp1 = (i>=0)?sp[i]:sp1; + } +} + +void CDelaunay::copyev(int i, int j) +{ + ev[j] = ev[i]; +} + +void CDelaunay::spsortx(SitePointer *sp_in, int low, int high) +{ + sp = sp_in; + rcssort(low,high,-1,&CDelaunay::xcmpsp,&CDelaunay::swapsp,&CDelaunay::copysp); +} + +void CDelaunay::spsorty(SitePointer *sp_in, int low, int high ) +{ + sp = sp_in; + rcssort(low,high,-1,&CDelaunay::ycmpsp,&CDelaunay::swapsp,&CDelaunay::copysp); +} + +void CDelaunay::rcssort(int lowelt, int highelt, int temp, + int (CDelaunay::*comparison)(int,int), + void (CDelaunay::*swap)(int,int), + void (CDelaunay::*copy)(int,int)) +{ + int m,sij,si,sj,sL,sk; + int stack[DM]; + + if (highelt-lowelt<=1) { + return; + } + if (highelt-lowelt>QQ) { + m = 0; + si = lowelt; sj = highelt; + for (;;) { // partition [si,sj] about median-of-3. + sij = (sj+si) >> 1; + + // Now to sort elements si,sij,sj into order & set temp=their median + if ( (this->*comparison)( si,sij ) > 0 ) { + (this->*swap)( si,sij ); + } + if ( (this->*comparison)( sij,sj ) > 0 ) { + (this->*swap)( sj,sij ); + if ( (this->*comparison)( si,sij ) > 0 ) { + (this->*swap)( si,sij ); + } + } + (this->*copy)( sij,temp ); + + // Now to partition into elements <=temp, >=temp, and ==temp. + sk = si; sL = sj; + do { + do { + sL--; + } while( (this->*comparison)( sL,temp ) > 0 ); + do { + sk++; + } while( (this->*comparison)( temp,sk ) > 0 ); + if ( sk < sL ) { + (this->*swap)( sL,sk ); + } + } while(sk <= sL); + + // Now to recurse on shorter partition, store longer partition on stack + if ( sL-si > sj-sk ) { + if ( sL-si < QQ ) { + if( m==0 ) { + break; // empty stack && both partitions < QQ so break + } else { + sj = stack[--m]; + si = stack[--m]; + } + } + else { + if ( sj-sk < QQ ) { + sj = sL; + } else { + stack[m++] = si; + stack[m++] = sL; + si = sk; + } + } + } + else { + if ( sj-sk < QQ ) { + if ( m==0 ) { + break; // empty stack && both partitions < QQ so break + } else { + sj = stack[--m]; + si = stack[--m]; + } + } + else { + if ( sL-si < QQ ) { + si = sk; + } else { + stack[m++] = sk; + stack[m++] = sj; + sj = sL; + } + } + } + } + } + + // Now for 0 or Data bounded "straight insertion" sort of [0,nels-1]; if it is + // known that el[-1] = -INF, then can omit the "sk>=0" test and save time. + for (si=lowelt; si*comparison)( si,si+1 ) > 0 ) { + (this->*copy)( si+1,temp ); + sj = sk = si; + sj++; + do { + (this->*copy)( sk,sj ); + sj = sk; + sk--; + } while ( (this->*comparison)( sk,temp ) > 0 && sk>=lowelt ); + (this->*copy)( temp,sj ); + } + } +} + +// +// Geometric primitives +// + +// incircle, as in the Guibas-Stolfi paper. +int CDelaunay::incircle(SitePointer a, SitePointer b, SitePointer c, SitePointer d) +{ + double adx, ady, bdx, bdy, cdx, cdy, dx, dy, nad, nbd, ncd; + dx = sa[d].X(); + dy = sa[d].Y(); + adx = sa[a].X() - dx; + ady = sa[a].Y() - dy; + bdx = sa[b].X() - dx; + bdy = sa[b].Y() - dy; + cdx = sa[c].X() - dx; + cdy = sa[c].Y() - dy; + nad = adx*adx+ady*ady; + nbd = bdx*bdx+bdy*bdy; + ncd = cdx*cdx+cdy*cdy; + return( (0.0 < (nad * (bdx * cdy - bdy * cdx) + + nbd * (cdx * ady - cdy * adx) + + ncd * (adx * bdy - ady * bdx))) ? TRUE : FALSE ); +} + +// TRUE iff A, B, C form a counterclockwise oriented triangle +int CDelaunay::ccw(SitePointer a, SitePointer b, SitePointer c) +{ + int result; + + double ax = sa[a].X(); + double bx = sa[b].X(); + double cx = sa[c].X(); + double ay = sa[a].Y(); + double by = sa[b].Y(); + double cy = sa[c].Y(); + + double val = (ax - cx)*(by - cy) - (bx - cx)*(ay - cy); + if ( val > 0.0) { + return true; + } + + return false; +} + +// +// The Merge Procedure. +// + +void CDelaunay::doMerge(EdgePointer *ldo, EdgePointer ldi, EdgePointer rdi, EdgePointer *rdo) +{ + int rvalid, lvalid; + EdgePointer basel,lcand,rcand,t; + + for (;;) { + while (ccw(orig(ldi), dest(ldi), orig(rdi))) { + ldi = (EdgePointer) lnext(ldi); + } + if (ccw(dest(rdi), orig(rdi), orig(ldi))) { + rdi = (EdgePointer)rprev(rdi); + } else { + break; + } + } + + basel = connectLeft((EdgePointer) sym(rdi), ldi); + lcand = rprev(basel); + rcand = (EdgePointer) oprev(basel); + if (orig(basel) == orig(*rdo)) { + *rdo = basel; + } + if (dest(basel) == orig(*ldo)) { + *ldo = (EdgePointer) sym(basel); + } + + for (;;) { +#if 1 + if (valid(t=onext(lcand))) { +#else + t = (EdgePointer)onext(lcand); + if (valid(basel, t)) { +#endif + while (incircle(dest(lcand), dest(t), orig(lcand), orig(basel))) { + deleteEdge(lcand); + lcand = t; + t = onext(lcand); + } + } +#if 1 + if (valid(t=(EdgePointer)oprev(rcand))) { +#else + t = (EdgePointer)oprev(rcand); + if (valid(basel, t)) { +#endif + while (incircle(dest(t), dest(rcand), orig(rcand), dest(basel))) { + deleteEdge(rcand); + rcand = t; + t = (EdgePointer)oprev(rcand); + } + } + +#if 1 + lvalid = valid(lcand); + rvalid = valid(rcand); +#else + lvalid = valid(basel, lcand); + rvalid = valid(basel, rcand); +#endif + if ((! lvalid) && (! rvalid)) { + return; + } + + if (!lvalid || + (rvalid && incircle(dest(lcand), orig(lcand), orig(rcand), dest(rcand)))) { + basel = connectLeft(rcand, (EdgePointer) sym(basel)); + rcand = (EdgePointer) lnext(sym(basel)); + } else { + basel = (EdgePointer) sym(connectRight(lcand, basel)); + lcand = rprev(basel); + } + } +} + +int CDelaunay::constructList(EdgePointer last, int width, int height) +{ + int c, i; + EdgePointer curr, src, nex; + SEdgeVector *currv, *prevv; + + c = (int) ((curr = (EdgePointer) ((last & ~3))) >> 1); + + for (last -= 4; last >= 0; last -= 4) { + src = orig(last); + nex = dest(last); + orig(--curr) = src; + orig(--curr) = nex; + orig(--curr) = nex; + orig(--curr) = src; + } + rcssort(0, c - 1, -1, &CDelaunay::cmpev, &CDelaunay::swapev, &CDelaunay::copyev); + + // Throw out any edges that are too far apart + currv = prevv = ev; + for (i = c; i--; currv++) { + if ((int) fabs(sa[currv->first].getVCenter().x - sa[currv->second].getVCenter().x) <= width && + (int) fabs(sa[currv->first].getVCenter().y - sa[currv->second].getVCenter().y) <= height) { + *(prevv++) = *currv; + } else { + c--; + } + } + return c; +} + +// Fill in site neighbor information +void CDelaunay::linkNeighbors(SEdgeVector *edge, int nedge, int nsite) +{ + int i; + + for (i = 0; i < nsite; i++) { + sa[i].setNeighbor(edge); + sa[i].setNumNeighbors(0); + for (; edge->first == i && nedge; edge++, nedge--) { + sa[i].incrNumNeighbors(); + } + } +} diff --git a/jni_mosaic/feature_mos/src/mosaic/Delaunay.h b/jni_mosaic/feature_mos/src/mosaic/Delaunay.h new file mode 100644 index 0000000000000000000000000000000000000000..7a450b5e4fc689481d6240e6b5755c9df349bb0c --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Delaunay.h @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Delaunay.h +// $Id: Delaunay.h,v 1.9 2011/06/17 13:35:48 mbansal Exp $ + +#ifndef DELAUNAY_H +#define DELAUNAY_H +#include +#include +#include "CSite.h" +#include "EdgePointerUtil.h" + +#ifndef TRUE +#define TRUE 1==1 +#define FALSE 0==1 +#endif + +//****************************************************************************** +// Reference for Quad-edge data structure: +// +// Leonidas Guibas and Jorge Stolfi, "Primitives for the manipulation of general +// subdivisions and the computations of Voronoi diagrams", +// ACM Transactions on Graphics 4, 74-123 (1985). +// +//****************************************************************************** + +// +// Common data structures +// + +typedef short SitePointer; +typedef short TrianglePointer; + +class CDelaunay +{ +private: + CSite *sa; + EdgePointer oneBndryEdge; + EdgePointer *next; + SitePointer *org; + struct EDGE_INFO *ei; + SitePointer *sp; + SEdgeVector *ev; + + SitePointer sp1; + EdgePointer nextEdge; + EdgePointer availEdge; + +private: + void build(int lo, int hi, EdgePointer *le, EdgePointer *re, int rows); + void buildTriangulation(int size); + + EdgePointer allocEdge(); + void freeEdge(EdgePointer e); + + EdgePointer makeEdge(SitePointer origin, SitePointer destination); + void deleteEdge(EdgePointer e); + + void splice(EdgePointer, EdgePointer); + EdgePointer consolidateEdges(); + void deleteAllEdges(); + + void spsortx(SitePointer *, int, int); + void spsorty(SitePointer *, int, int); + + int cmpev(int i, int j); + int xcmpsp(int i, int j); + int ycmpsp(int i, int j); + + void swapsp(int i, int j); + void swapev(int i, int j); + + void copysp(int i, int j); + void copyev(int i, int j); + + void rcssort(int lowelt, int highelt, int temp, + int (CDelaunay::*comparison)(int,int), + void (CDelaunay::*swap)(int,int), + void (CDelaunay::*copy)(int,int)); + + void doMerge(EdgePointer *ldo, EdgePointer ldi, EdgePointer rdi, EdgePointer *rdo); + EdgePointer connectLeft(EdgePointer a, EdgePointer b); + EdgePointer connectRight(EdgePointer a, EdgePointer b); + int ccw(SitePointer a, SitePointer b, SitePointer c); + int incircle(SitePointer a, SitePointer b, SitePointer c, SitePointer d); + int constructList(EdgePointer e, int width, int height); + +public: + CDelaunay(); + ~CDelaunay(); + + CSite *allocMemory(int nsite); + void freeMemory(); + int triangulate(SEdgeVector **edge, int nsite, int width, int height); + void linkNeighbors(SEdgeVector *edge, int nedge, int nsite); +}; + +#define onext(a) next[a] +#define oprev(a) rot(onext(rot(a))) +#define lnext(a) rot(onext(rotinv(a))) +#define lprev(a) sym(onext(a)) +#define rnext(a) rotinv(onext(rot(a))) +#define rprev(a) onext(sym(a)) +#define dnext(a) sym(onext(sym(a))) +#define dprev(a) rotinv(onext(rotinv(a))) + +#define orig(a) org[a] +#define dest(a) orig(sym(a)) +#define left(a) orig(rotinv(a)) +#define right(a) orig(rot(a)) + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/EdgePointerUtil.h b/jni_mosaic/feature_mos/src/mosaic/EdgePointerUtil.h new file mode 100644 index 0000000000000000000000000000000000000000..fad05d7ecc37ffcdfaebe52d210040ca19a27dd1 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/EdgePointerUtil.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _EDGEPOINTERUTIL_H_ +#define _EDGEPOINTERUTIL_H_ + +typedef short EdgePointer; + +inline EdgePointer sym(EdgePointer a) +{ + return a ^ 2; +} + +inline EdgePointer rot(EdgePointer a) +{ + return (((a) + 1) & 3) | ((a) & ~3); +} + +inline EdgePointer rotinv(EdgePointer a) +{ + return (((a) + 3) & 3) | ((a) & ~3); +} + +#endif //_EDGEPOINTERUTIL_H_ diff --git a/jni_mosaic/feature_mos/src/mosaic/Geometry.h b/jni_mosaic/feature_mos/src/mosaic/Geometry.h new file mode 100644 index 0000000000000000000000000000000000000000..0efa0f4a54971fce043772fc30d66dfe661e60e6 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Geometry.h @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +///////////////////////////// +// Geometry.h +// $Id: Geometry.h,v 1.2 2011/06/17 13:35:48 mbansal Exp $ + +#pragma once +#include "MosaicTypes.h" + +/////////////////////////////////////////////////////////////// +///////////////// BEG GLOBAL ROUTINES ///////////////////////// +/////////////////////////////////////////////////////////////// + + +inline double hypotSq(double a, double b) +{ + return ((a)*(a)+(b)*(b)); +} + +inline void ClipRect(double x, double y, BlendRect &brect) +{ + if (y < brect.bot) brect.bot = y; + if (y > brect.top) brect.top = y; + if (x < brect.lft) brect.lft = x; + if (x > brect.rgt) brect.rgt = x; +} + +inline void ClipRect(BlendRect rrect, BlendRect &brect) +{ + if (rrect.bot < brect.bot) brect.bot = rrect.bot; + if (rrect.top > brect.top) brect.top = rrect.top; + if (rrect.lft < brect.lft) brect.lft = rrect.lft; + if (rrect.rgt > brect.rgt) brect.rgt = rrect.rgt; +} + +// Clip x to be within [-border,width+border-1] +inline void clipToSegment(int &x, int width, int border) +{ + if(x < -border) + x = -border; + else if(x >= width+border) + x = width + border - 1; +} + +// Return true if x within [-border,width+border-1] +inline bool inSegment(int x, int width, int border) +{ + return (x >= -border && x < width + border - 1); +} + +inline void FindTriangleCentroid(double x0, double y0, double x1, double y1, + double x2, double y2, + double &mass, double ¢X, double ¢Y) +{ + // Calculate the centroid of the triangle + centX = (x0 + x1 + x2) / 3.0; + centY = (y0 + y1 + y2) / 3.0; + + // Calculate 2*Area for the triangle + if (y0 == y2) + { + if (x0 == x1) + { + mass = fabs((y1 - y0) * (x2 - x0)); // Special case 1a + } + else + { + mass = fabs((y1 - y0) * (x1 - x0)); // Special case 1b + } + } + else if (x0 == x2) + { + if (x0 == x1) + { + mass = fabs((x2 - x0) * (y2 - y0)); // Special case 2a + } + else + { + mass = fabs((x1 - x0) * (y2 - y0)); // Special case 2a + } + } + else if (x1 == x2) + { + mass = fabs((x1 - x0) * (y2 - y0)); // Special case 3 + } + else + { + // Calculate line equation from x0,y0 to x2,y2 + double dx = x2 - x0; + double dy = y2 - y0; + // Calculate the length of the side + double len1 = sqrt(dx * dx + dy * dy); + double m1 = dy / dx; + double b1 = y0 - m1 * x0; + // Calculate the line that goes through x1,y1 and is perpendicular to + // the other line + double m2 = 1.0 / m1; + double b2 = y1 - m2 * x1; + // Calculate the intersection of the two lines + if (fabs( m1 - m2 ) > 1.e-6) + { + double x = (b2 - b1) / (m1 - m2); + // the mass is the base * height + dx = x1 - x; + dy = y1 - m1 * x + b1; + mass = len1 * sqrt(dx * dx + dy * dy); + } + else + { + mass = fabs( (y1 - y0) * (x2 - x0) ); + } + } +} + +inline void FindQuadCentroid(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, + double ¢X, double ¢Y) + +{ + // To find the centroid: + // 1) Divide the quadrilateral into two triangles by scribing a diagonal + // 2) Calculate the centroid of each triangle (the intersection of the angle bisections). + // 3) Find the centroid of the quad by weighting each triangle centroids by their area. + + // Calculate the corner points + double z; + + // The quad is split from x0,y0 to x2,y2 + double mass1, mass2, cent1x, cent2x, cent1y, cent2y; + FindTriangleCentroid(x0, y0, x1, y1, x2, y2, mass1, cent1x, cent1y); + FindTriangleCentroid(x0, y0, x3, y3, x2, y2, mass2, cent2x, cent2y); + + // determine position of quad centroid + z = mass2 / (mass1 + mass2); + centX = cent1x + (cent2x - cent1x) * z; + centY = cent1y + (cent2y - cent1y) * z; +} + +/////////////////////////////////////////////////////////////// +////////////////// END GLOBAL ROUTINES //////////////////////// +/////////////////////////////////////////////////////////////// + + diff --git a/jni_mosaic/feature_mos/src/mosaic/ImageUtils.cpp b/jni_mosaic/feature_mos/src/mosaic/ImageUtils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d0aac0c19dc48f27bf505ff0a1166097fb3f768 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/ImageUtils.cpp @@ -0,0 +1,408 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// ImageUtils.cpp +// $Id: ImageUtils.cpp,v 1.12 2011/06/17 13:35:48 mbansal Exp $ + + +#include +#include +#include + +#include "ImageUtils.h" + +void ImageUtils::rgba2yvu(ImageType out, ImageType in, int width, int height) +{ + int r,g,b, a; + ImageType yimg = out; + ImageType vimg = yimg + width*height; + ImageType uimg = vimg + width*height; + ImageType image = in; + + for (int ii = 0; ii < height; ii++) { + for (int ij = 0; ij < width; ij++) { + r = (*image++); + g = (*image++); + b = (*image++); + a = (*image++); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + int val = (int) (REDY * r + GREENY * g + BLUEY * b) / 1000 + 16; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(yimg) = val; + + val = (int) (REDV * r - GREENV * g - BLUEV * b) / 1000 + 128; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(vimg) = val; + + val = (int) (-REDU * r - GREENU * g + BLUEU * b) / 1000 + 128; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(uimg) = val; + + yimg++; + uimg++; + vimg++; + } + } +} + + +void ImageUtils::rgb2yvu(ImageType out, ImageType in, int width, int height) +{ + int r,g,b; + ImageType yimg = out; + ImageType vimg = yimg + width*height; + ImageType uimg = vimg + width*height; + ImageType image = in; + + for (int ii = 0; ii < height; ii++) { + for (int ij = 0; ij < width; ij++) { + r = (*image++); + g = (*image++); + b = (*image++); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + int val = (int) (REDY * r + GREENY * g + BLUEY * b) / 1000 + 16; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(yimg) = val; + + val = (int) (REDV * r - GREENV * g - BLUEV * b) / 1000 + 128; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(vimg) = val; + + val = (int) (-REDU * r - GREENU * g + BLUEU * b) / 1000 + 128; + if (val < 0) val = 0; + if (val > 255) val = 255; + *(uimg) = val; + + yimg++; + uimg++; + vimg++; + } + } +} + +ImageType ImageUtils::rgb2gray(ImageType in, int width, int height) +{ + int r,g,b, nr, ng, nb, val; + ImageType gray = NULL; + ImageType image = in; + ImageType out = ImageUtils::allocateImage(width, height, 1); + ImageType outCopy = out; + + for (int ii = 0; ii < height; ii++) { + for (int ij = 0; ij < width; ij++) { + r = (*image++); + g = (*image++); + b = (*image++); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + (*outCopy) = ( 0.3*r + 0.59*g + 0.11*b); + + outCopy++; + } + } + + return out; +} + +ImageType ImageUtils::rgb2gray(ImageType out, ImageType in, int width, int height) +{ + int r,g,b, nr, ng, nb, val; + ImageType gray = out; + ImageType image = in; + ImageType outCopy = out; + + for (int ii = 0; ii < height; ii++) { + for (int ij = 0; ij < width; ij++) { + r = (*image++); + g = (*image++); + b = (*image++); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + (*outCopy) = ( 0.3*r + 0.59*g + 0.11*b); + + outCopy++; + } + } + + return out; + +} + +ImageType *ImageUtils::imageTypeToRowPointers(ImageType in, int width, int height) +{ + int i; + int m_h = height; + int m_w = width; + + ImageType *m_rows = new ImageType[m_h]; + + for (i=0;i 255) y = 255; + if (u < 0) u = 0; + if (u > 255) u = 255; + if (v < 0) v = 0; + if (v > 255) v = 255; + + b = (int) ( 1.164*(y - 16) + 2.018*(u-128)); + g = (int) ( 1.164*(y - 16) - 0.813*(v-128) - 0.391*(u-128)); + r = (int) ( 1.164*(y - 16) + 1.596*(v-128)); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + *(image++) = r; + *(image++) = g; + *(image++) = b; + + yimg++; + uimg++; + vimg++; + + } + } +} + +void ImageUtils::yvu2bgr(ImageType out, ImageType in, int width, int height) +{ + int y,v,u, r, g, b; + unsigned char *yimg = in; + unsigned char *vimg = yimg + width*height; + unsigned char *uimg = vimg + width*height; + unsigned char *image = out; + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + + y = (*yimg); + v = (*vimg); + u = (*uimg); + + if (y < 0) y = 0; + if (y > 255) y = 255; + if (u < 0) u = 0; + if (u > 255) u = 255; + if (v < 0) v = 0; + if (v > 255) v = 255; + + b = (int) ( 1.164*(y - 16) + 2.018*(u-128)); + g = (int) ( 1.164*(y - 16) - 0.813*(v-128) - 0.391*(u-128)); + r = (int) ( 1.164*(y - 16) + 1.596*(v-128)); + + if (r < 0) r = 0; + if (r > 255) r = 255; + if (g < 0) g = 0; + if (g > 255) g = 255; + if (b < 0) b = 0; + if (b > 255) b = 255; + + *(image++) = b; + *(image++) = g; + *(image++) = r; + + yimg++; + uimg++; + vimg++; + + } + } +} + + +ImageType ImageUtils::readBinaryPPM(const char *filename, int &width, int &height) +{ + + FILE *imgin = NULL; + int mval=0, format=0, eret; + ImageType ret = IMAGE_TYPE_NOIMAGE; + + imgin = fopen(filename, "r"); + if (imgin == NULL) { + fprintf(stderr, "Error: Filename %s not found\n", filename); + return ret; + } + + eret = fscanf(imgin, "P%d\n", &format); + if (format != 6) { + fprintf(stderr, "Error: readBinaryPPM only supports PPM format (P6)\n"); + return ret; + } + + eret = fscanf(imgin, "%d %d\n", &width, &height); + eret = fscanf(imgin, "%d\n", &mval); + ret = allocateImage(width, height, IMAGE_TYPE_NUM_CHANNELS); + eret = fread(ret, sizeof(ImageTypeBase), IMAGE_TYPE_NUM_CHANNELS*width*height, imgin); + + fclose(imgin); + + return ret; + +} + +void ImageUtils::writeBinaryPPM(ImageType image, const char *filename, int width, int height, int numChannels) +{ + FILE *imgout = fopen(filename, "w"); + + if (imgout == NULL) { + fprintf(stderr, "Error: Filename %s could not be opened for writing\n", filename); + return; + } + + if (numChannels == 3) { + fprintf(imgout, "P6\n%d %d\n255\n", width, height); + } else if (numChannels == 1) { + fprintf(imgout, "P5\n%d %d\n255\n", width, height); + } else { + fprintf(stderr, "Error: writeBinaryPPM: Unsupported number of channels\n"); + } + fwrite(image, sizeof(ImageTypeBase), numChannels*width*height, imgout); + + fclose(imgout); + +} + +ImageType ImageUtils::allocateImage(int width, int height, int numChannels, short int border) +{ + int overallocation = 256; + return (ImageType) calloc(width*height*numChannels+overallocation, sizeof(ImageTypeBase)); +} + + +void ImageUtils::freeImage(ImageType image) +{ + free(image); +} + + +// allocation of one color image used for tmp buffers, etc. +// format of contiguous memory block: +// YUVInfo struct (type + BimageInfo for Y,U, and V), +// Y row pointers +// U row pointers +// V row pointers +// Y image pixels +// U image pixels +// V image pixels +YUVinfo *YUVinfo::allocateImage(unsigned short width, unsigned short height) +{ + unsigned short heightUV, widthUV; + + widthUV = width; + heightUV = height; + + // figure out how much space to hold all pixels... + int size = ((width * height * 3) + 8); + unsigned char *position = 0; + + // VC 8 does not like calling free on yuv->Y.ptr since it is in + // the middle of a block. So rearrange the memory layout so after + // calling mapYUVInforToImage yuv->Y.ptr points to the begginning + // of the calloc'ed block. + YUVinfo *yuv = (YUVinfo *) calloc(sizeof(YUVinfo), 1); + if (yuv) { + yuv->Y.width = yuv->Y.pitch = width; + yuv->Y.height = height; + yuv->Y.border = yuv->U.border = yuv->V.border = (unsigned short) 0; + yuv->U.width = yuv->U.pitch = yuv->V.width = yuv->V.pitch = widthUV; + yuv->U.height = yuv->V.height = heightUV; + + unsigned char* block = (unsigned char*) calloc( + sizeof(unsigned char *) * (height + heightUV + heightUV) + + sizeof(unsigned char) * size, 1); + + position = block; + unsigned char **y = (unsigned char **) (block + size); + + /* Initialize and assign row pointers */ + yuv->Y.ptr = y; + yuv->V.ptr = &y[height]; + yuv->U.ptr = &y[height + heightUV]; + } + if (size) + mapYUVInfoToImage(yuv, position); + return yuv; +} + +// wrap YUVInfo row pointers around 3 contiguous image (color component) planes. +// position = starting pixel in image. +void YUVinfo::mapYUVInfoToImage(YUVinfo *img, unsigned char *position) +{ + int i; + for (i = 0; i < img->Y.height; i++, position += img->Y.width) + img->Y.ptr[i] = position; + for (i = 0; i < img->V.height; i++, position += img->V.width) + img->V.ptr[i] = position; + for (i = 0; i < img->U.height; i++, position += img->U.width) + img->U.ptr[i] = position; +} + + diff --git a/jni_mosaic/feature_mos/src/mosaic/ImageUtils.h b/jni_mosaic/feature_mos/src/mosaic/ImageUtils.h new file mode 100644 index 0000000000000000000000000000000000000000..92965ca817f63e31cacf84438bb3ab7ac47198c7 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/ImageUtils.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// ImageUtils.h +// $Id: ImageUtils.h,v 1.9 2011/05/16 15:33:06 mbansal Exp $ + +#ifndef IMAGE_UTILS_H +#define IMAGE_UTILS_H + +#include + +/** + * Definition of basic image types + */ +typedef unsigned char ImageTypeBase; +typedef ImageTypeBase *ImageType; + +typedef short ImageTypeShortBase; +typedef ImageTypeShortBase *ImageTypeShort; + +typedef float ImageTypeFloatBase; +typedef ImageTypeFloatBase *ImageTypeFloat; + + +class ImageUtils { +public: + + /** + * Default number of channels in image. + */ + static const int IMAGE_TYPE_NUM_CHANNELS = 3; + + /** + * Definition of an empty image. + */ + static const int IMAGE_TYPE_NOIMAGE = 0; + + /** + * Convert image from BGR (interlaced) to YVU (non-interlaced) + * + * Arguments: + * out: Resulting image (note must be preallocated before + * call) + * in: Input image + * width: Width of input image + * height: Height of input image + */ + static void rgb2yvu(ImageType out, ImageType in, int width, int height); + + static void rgba2yvu(ImageType out, ImageType in, int width, int height); + + /** + * Convert image from YVU (non-interlaced) to BGR (interlaced) + * + * Arguments: + * out: Resulting image (note must be preallocated before + * call) + * in: Input image + * width: Width of input image + * height: Height of input image + */ + static void yvu2rgb(ImageType out, ImageType in, int width, int height); + static void yvu2bgr(ImageType out, ImageType in, int width, int height); + + /** + * Convert image from BGR to grayscale + * + * Arguments: + * in: Input image + * width: Width of input image + * height: Height of input image + * + * Return: + * Pointer to resulting image (allocation is done here, free + * must be done by caller) + */ + static ImageType rgb2gray(ImageType in, int width, int height); + static ImageType rgb2gray(ImageType out, ImageType in, int width, int height); + + /** + * Read a binary PPM image + */ + static ImageType readBinaryPPM(const char *filename, int &width, int &height); + + /** + * Write a binary PPM image + */ + static void writeBinaryPPM(ImageType image, const char *filename, int width, int height, int numChannels = IMAGE_TYPE_NUM_CHANNELS); + + /** + * Allocate space for a standard image. + */ + static ImageType allocateImage(int width, int height, int numChannels, short int border = 0); + + /** + * Free memory of image + */ + static void freeImage(ImageType image); + + static ImageType *imageTypeToRowPointers(ImageType out, int width, int height); + /** + * Get time. + */ + static double getTime(); + +protected: + + /** + * Constants for YVU/RGB conversion + */ + static const int REDY = 257; + static const int REDV = 439; + static const int REDU = 148; + static const int GREENY = 504; + static const int GREENV = 368; + static const int GREENU = 291; + static const int BLUEY = 98; + static const int BLUEV = 71; + static const int BLUEU = 439; + +}; + +/** + * Structure containing an image and other bookkeeping items. + * Used in YUVinfo to store separate YVU image planes. + */ +typedef struct { + ImageType *ptr; + unsigned short width; + unsigned short height; + unsigned short border; + unsigned short pitch; +} BimageInfo; + +/** + * A YUV image container, + */ +class YUVinfo { +public: + static YUVinfo *allocateImage(unsigned short width, unsigned short height); + static void mapYUVInfoToImage(YUVinfo *img, unsigned char *position); + + /** + * Y Plane + */ + BimageInfo Y; + + /** + * V (1st color) plane + */ + BimageInfo V; + + /** + * U (1st color) plane + */ + BimageInfo U; +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Interp.h b/jni_mosaic/feature_mos/src/mosaic/Interp.h new file mode 100644 index 0000000000000000000000000000000000000000..19c4a40cbf3798067da84f787fee98c6570e4904 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Interp.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////////////// +// Interp.h +// $Id: Interp.h,v 1.2 2011/06/17 13:35:48 mbansal Exp $ + +#ifndef INTERP_H +#define INTERP_H + +#include "Pyramid.h" + +#define CTAPS 40 +static double ciTable[81] = { + 1, 0.998461, 0.993938, 0.98657, 0.9765, + 0.963867, 0.948813, 0.931477, 0.912, 0.890523, + 0.867188, 0.842133, 0.8155, 0.78743, 0.758062, + 0.727539, 0.696, 0.663586, 0.630437, 0.596695, + 0.5625, 0.527992, 0.493312, 0.458602, 0.424, + 0.389648, 0.355687, 0.322258, 0.2895, 0.257555, + 0.226562, 0.196664, 0.168, 0.140711, 0.114937, + 0.0908203, 0.0685, 0.0481172, 0.0298125, 0.0137266, + 0, -0.0118828, -0.0225625, -0.0320859, -0.0405, + -0.0478516, -0.0541875, -0.0595547, -0.064, -0.0675703, + -0.0703125, -0.0722734, -0.0735, -0.0740391, -0.0739375, + -0.0732422, -0.072, -0.0702578, -0.0680625, -0.0654609, + -0.0625, -0.0592266, -0.0556875, -0.0519297, -0.048, + -0.0439453, -0.0398125, -0.0356484, -0.0315, -0.0274141, + -0.0234375, -0.0196172, -0.016, -0.0126328, -0.0095625, + -0.00683594, -0.0045, -0.00260156, -0.0011875, -0.000304687, 0.0 +}; + +inline double ciCalc(PyramidShort *img, int xi, int yi, double xfrac, double yfrac) +{ + double tmpf[4]; + + // Interpolate using 16 points + ImageTypeShortBase *in = img->ptr[yi-1] + xi - 1; + int off = (int)(xfrac * CTAPS); + + tmpf[0] = in[0] * ciTable[off + 40]; + tmpf[0] += in[1] * ciTable[off]; + tmpf[0] += in[2] * ciTable[40 - off]; + tmpf[0] += in[3] * ciTable[80 - off]; + in += img->pitch; + tmpf[1] = in[0] * ciTable[off + 40]; + tmpf[1] += in[1] * ciTable[off]; + tmpf[1] += in[2] * ciTable[40 - off]; + tmpf[1] += in[3] * ciTable[80 - off]; + in += img->pitch; + tmpf[2] = in[0] * ciTable[off + 40]; + tmpf[2] += in[1] * ciTable[off]; + tmpf[2] += in[2] * ciTable[40 - off]; + tmpf[2] += in[3] * ciTable[80 - off]; + in += img->pitch; + tmpf[3] = in[0] * ciTable[off + 40]; + tmpf[3] += in[1] * ciTable[off]; + tmpf[3] += in[2] * ciTable[40 - off]; + tmpf[3] += in[3] * ciTable[80 - off]; + + // this is the final interpolation + off = (int)(yfrac * CTAPS); + return (ciTable[off + 40] * tmpf[0] + ciTable[off] * tmpf[1] + + ciTable[40 - off] * tmpf[2] + ciTable[80 - off] * tmpf[3]); +} + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Log.h b/jni_mosaic/feature_mos/src/mosaic/Log.h new file mode 100644 index 0000000000000000000000000000000000000000..cf6f14b18ae4b836e8a8f1d6292ddf7f74b42dd4 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Log.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LOG_H_ +#define LOG_H + +#include +#define LOGV(...) __android_log_print(ANDROID_LOG_SILENT, LOG_TAG, __VA_ARGS__) +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/MatrixUtils.h b/jni_mosaic/feature_mos/src/mosaic/MatrixUtils.h new file mode 100644 index 0000000000000000000000000000000000000000..a0b84d8133c9cf2c5d8c61721c152e8c0b8ebd04 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/MatrixUtils.h @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// Matrixutils.h +// $Id: MatrixUtils.h,v 1.5 2011/05/16 15:33:06 mbansal Exp $ + + +#ifndef MATRIX_UTILS_H +#define MATRIX_UTILS_H + +/* Simple class for 3x3 matrix, mainly used to convert from 9x1 + * to 3x3 + */ +class Matrix33 { +public: + + /** + * Empty constructor + */ + Matrix33() { + initialize(); + } + + /** + * Constructor with identity initialization + * Arguments: + * identity: Specifies wether to initialize matrix to + * identity or zeros + */ + Matrix33(bool identity) { + initialize(identity); + } + + /** + * Initialize to identity matrix + */ + void initialize(bool identity = false) { + mat[0][1] = mat[0][2] = mat[1][0] = mat[1][2] = mat[2][0] = mat[2][1] = 0.0; + if (identity) { + mat[0][0] = mat[1][1] = mat[2][2] = 1.0; + } else { + mat[0][0] = mat[1][1] = mat[2][2] = 0.0; + } + } + + /** + * Conver ta 9x1 matrix to a 3x3 matrix + */ + static void convert9to33(double out[3][3], double in[9]) { + out[0][0] = in[0]; + out[0][1] = in[1]; + out[0][2] = in[2]; + + out[1][0] = in[3]; + out[1][1] = in[4]; + out[1][2] = in[5]; + + out[2][0] = in[6]; + out[2][1] = in[7]; + out[2][2] = in[8]; + + } + + /* Matrix data */ + double mat[3][3]; + +}; + +/* Simple class for 9x1 matrix, mainly used to convert from 3x3 + * to 9x1 + */ +class Matrix9 { +public: + + /** + * Empty constructor + */ + Matrix9() { + initialize(); + } + + /** + * Constructor with identity initialization + * Arguments: + * identity: Specifies wether to initialize matrix to + * identity or zeros + */ + Matrix9(bool identity) { + initialize(identity); + } + + /** + * Initialize to identity matrix + */ + void initialize(bool identity = false) { + mat[1] = mat[2] = mat[3] = mat[5] = mat[6] = mat[7] = 0.0; + if (identity) { + mat[0] = mat[4] = mat[8] = 1.0; + } else { + mat[0] = mat[4] = mat[8] = 0.0; + } + } + + /** + * Conver ta 3x3 matrix to a 9x1 matrix + */ + static void convert33to9(double out[9], double in[3][3]) { + out[0] = in[0][0]; + out[1] = in[0][1]; + out[2] = in[0][2]; + + out[3] = in[1][0]; + out[4] = in[1][1]; + out[5] = in[1][2]; + + out[6] = in[2][0]; + out[7] = in[2][1]; + out[8] = in[2][2]; + + } + + /* Matrix data */ + double mat[9]; + +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Mosaic.cpp b/jni_mosaic/feature_mos/src/mosaic/Mosaic.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b96fa5c5b324050c87b577ecc5db47acf4c3e7b --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Mosaic.cpp @@ -0,0 +1,265 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// Mosaic.pp +// S.O. # : +// Author(s): zkira +// $Id: Mosaic.cpp,v 1.20 2011/06/24 04:22:14 mbansal Exp $ + +#include +#include + +#include "Mosaic.h" +#include "trsMatrix.h" + +#include "Log.h" +#define LOG_TAG "MOSAIC" + +Mosaic::Mosaic() +{ + initialized = false; + imageMosaicYVU = NULL; + frames_size = 0; + max_frames = 200; +} + +Mosaic::~Mosaic() +{ + for (int i = 0; i < frames_size; i++) + { + if (frames[i]) + delete frames[i]; + } + delete frames; + delete rframes; + + for (int j = 0; j < owned_size; j++) + delete owned_frames[j]; + delete owned_frames; + + if (aligner != NULL) + delete aligner; + if (blender != NULL) + delete blender; +} + +int Mosaic::initialize(int blendingType, int stripType, int width, int height, int nframes, bool quarter_res, float thresh_still) +{ + this->blendingType = blendingType; + + // TODO: Review this logic if enabling FULL or PAN mode + if (blendingType == Blend::BLEND_TYPE_FULL || + blendingType == Blend::BLEND_TYPE_PAN) + { + stripType = Blend::STRIP_TYPE_THIN; + } + + this->stripType = stripType; + this->width = width; + this->height = height; + + + mosaicWidth = mosaicHeight = 0; + imageMosaicYVU = NULL; + + frames = new MosaicFrame *[max_frames]; + rframes = new MosaicFrame *[max_frames]; + + if(nframes>-1) + { + for(int i=0; iwidth,this->height,false); // Do no allocate memory for YUV data + } + } + else + { + for(int i=0; iinitialize(width, height,quarter_res,thresh_still); + + if (blendingType == Blend::BLEND_TYPE_FULL || + blendingType == Blend::BLEND_TYPE_PAN || + blendingType == Blend::BLEND_TYPE_CYLPAN || + blendingType == Blend::BLEND_TYPE_HORZ) { + blender = new Blend(); + blender->initialize(blendingType, stripType, width, height); + } else { + blender = NULL; + LOGE("Error: Unknown blending type %d",blendingType); + return MOSAIC_RET_ERROR; + } + + initialized = true; + + return MOSAIC_RET_OK; +} + +int Mosaic::addFrameRGB(ImageType imageRGB) +{ + ImageType imageYVU; + // Convert to YVU24 which is used by blending + imageYVU = ImageUtils::allocateImage(this->width, this->height, ImageUtils::IMAGE_TYPE_NUM_CHANNELS); + ImageUtils::rgb2yvu(imageYVU, imageRGB, width, height); + + int existing_frames_size = frames_size; + int ret = addFrame(imageYVU); + + if (frames_size > existing_frames_size) + owned_frames[owned_size++] = imageYVU; + else + ImageUtils::freeImage(imageYVU); + + return ret; +} + +int Mosaic::addFrame(ImageType imageYVU) +{ + if(frames[frames_size]==NULL) + frames[frames_size] = new MosaicFrame(this->width,this->height,false); + + MosaicFrame *frame = frames[frames_size]; + + frame->image = imageYVU; + + // Add frame to aligner + int ret = MOSAIC_RET_ERROR; + if (aligner != NULL) + { + // Note aligner takes in RGB images + int align_flag = Align::ALIGN_RET_OK; + align_flag = aligner->addFrame(frame->image); + aligner->getLastTRS(frame->trs); + + if (frames_size >= max_frames) + { + LOGV("WARNING: More frames than preallocated, ignoring." + "Increase maximum number of frames (-f ) to avoid this"); + return MOSAIC_RET_ERROR; + } + + switch (align_flag) + { + case Align::ALIGN_RET_OK: + frames_size++; + ret = MOSAIC_RET_OK; + break; + case Align::ALIGN_RET_FEW_INLIERS: + frames_size++; + ret = MOSAIC_RET_FEW_INLIERS; + break; + case Align::ALIGN_RET_LOW_TEXTURE: + ret = MOSAIC_RET_LOW_TEXTURE; + break; + case Align::ALIGN_RET_ERROR: + ret = MOSAIC_RET_ERROR; + break; + default: + break; + } + } + + return ret; +} + + +int Mosaic::createMosaic(float &progress, bool &cancelComputation) +{ + if (frames_size <= 0) + { + // Haven't accepted any frame in aligner. No need to do blending. + progress = TIME_PERCENT_ALIGN + TIME_PERCENT_BLEND + + TIME_PERCENT_FINAL; + return MOSAIC_RET_OK; + } + + if (blendingType == Blend::BLEND_TYPE_PAN) + { + + balanceRotations(); + + } + + int ret = Blend::BLEND_RET_ERROR; + + // Blend the mosaic (alignment has already been done) + if (blender != NULL) + { + ret = blender->runBlend((MosaicFrame **) frames, (MosaicFrame **) rframes, + frames_size, imageMosaicYVU, + mosaicWidth, mosaicHeight, progress, cancelComputation); + } + + switch(ret) + { + case Blend::BLEND_RET_ERROR: + case Blend::BLEND_RET_ERROR_MEMORY: + ret = MOSAIC_RET_ERROR; + break; + case Blend::BLEND_RET_CANCELLED: + ret = MOSAIC_RET_CANCELLED; + break; + case Blend::BLEND_RET_OK: + ret = MOSAIC_RET_OK; + } + return ret; +} + +ImageType Mosaic::getMosaic(int &width, int &height) +{ + width = mosaicWidth; + height = mosaicHeight; + + return imageMosaicYVU; +} + + + +int Mosaic::balanceRotations() +{ + // Normalize to the mean angle of rotation (Smiley face) + double sineAngle = 0.0; + + for (int i = 0; i < frames_size; i++) sineAngle += frames[i]->trs[0][1]; + sineAngle /= frames_size; + // Calculate the cosineAngle (1 - sineAngle*sineAngle) = cosineAngle*cosineAngle + double cosineAngle = sqrt(1.0 - sineAngle*sineAngle); + double m[3][3] = { + { cosineAngle, -sineAngle, 0 }, + { sineAngle, cosineAngle, 0}, + { 0, 0, 1}}; + double tmp[3][3]; + + for (int i = 0; i < frames_size; i++) { + memcpy(tmp, frames[i]->trs, sizeof(tmp)); + mult33d(frames[i]->trs, m, tmp); + } + + return MOSAIC_RET_OK; +} diff --git a/jni_mosaic/feature_mos/src/mosaic/Mosaic.h b/jni_mosaic/feature_mos/src/mosaic/Mosaic.h new file mode 100644 index 0000000000000000000000000000000000000000..9dea66422e8d4654831fe4a35e87081b92d23392 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Mosaic.h @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// Mosaic.h +// S.O. # : +// Author(s): zkira +// $Id: Mosaic.h,v 1.16 2011/06/24 04:22:14 mbansal Exp $ + +#ifndef MOSAIC_H +#define MOSAIC_H + +#include "ImageUtils.h" +#include "AlignFeatures.h" +#include "Blend.h" +#include "MosaicTypes.h" + +/*! \mainpage Mosaic + + \section intro Introduction + The class Mosaic provides a simple interface to the panoramic mosaicing algorithm. The class allows passing in individual image frames to be stitched together, computes the alignment transformation between them, and then stitches and blends them together into a single panoramic output which can then be accessed as a single image. \ + + \section usage Usage + The class methods need to be called as outlined in the sample application which is created from the mosaic_main.cpp file in the directory src/mosaic/. A brief snapshot of the flow is given below: + + \code + Mosaic mosaic; + // Define blending types to use, and the frame dimensions + int blendingType = Blend::BLEND_TYPE_CYLPAN; + int stripType = Blend::STRIP_TYPE_THIN; + int width = 640; + int height = 480; + + while () + { + // Check for initialization and if not, initialize + if (!mosaic.isInitialized()) + { + // Initialize mosaic processing + mosaic.initialize(blendingType, stripType, width, height, -1, false, 5.0f); + } + + // Add to list of frames + mosaic.addFrameRGB(imageRGB); + + // Free image + ImageUtils::freeImage(imageRGB); + } + + // Create the mosaic + ret = mosaic.createMosaic(); + + // Get back the result + resultYVU = mosaic.getMosaic(mosaicWidth, mosaicHeight); + + printf("Got mosaic of size %d,%d\n", mosaicWidth, mosaicHeight); + + \endcode +*/ + +/*! + * Main class that creates a mosaic by creating an aligner and blender. + */ +class Mosaic +{ + +public: + + Mosaic(); + ~Mosaic(); + + /*! + * Creates the aligner and blender and initializes state. + * \param blendingType Type of blending to perform + * \param stripType Type of strip to use. 0: thin, 1: wide. stripType + * is effective only when blendingType is CylPan or + * Horz. Otherwise, it is set to thin irrespective of the input. + * \param width Width of input images (note: all images must be same size) + * \param height Height of input images (note: all images must be same size) + * \param nframes Number of frames to pre-allocate; default value -1 will allocate each frame as it comes + * \param quarter_res Whether to compute alignment at quarter the input resolution (default = false) + * \param thresh_still Minimum number of pixels of translation detected between the new frame and the last frame before this frame is added to be mosaiced. For the low-res processing at 320x180 resolution input, we set this to 5 pixels. To reject no frames, set this to 0.0 (default value). + * \return Return code signifying success or failure. + */ + int initialize(int blendingType, int stripType, int width, int height, int nframes = -1, bool quarter_res = false, float thresh_still = 0.0); + + /*! + * Adds a YVU frame to the mosaic. + * \param imageYVU Pointer to a YVU image. + * \return Return code signifying success or failure. + */ + int addFrame(ImageType imageYVU); + + /*! + * Adds a RGB frame to the mosaic. + * \param imageRGB Pointer to a RGB image. + * \return Return code signifying success or failure. + */ + int addFrameRGB(ImageType imageRGB); + + /*! + * After adding all frames, call this function to perform the final blending. + * \param progress Variable to set the current progress in. + * \return Return code signifying success or failure. + */ + int createMosaic(float &progress, bool &cancelComputation); + + /*! + * Obtains the resulting mosaic and its dimensions. + * \param width Width of the resulting mosaic (returned) + * \param height Height of the resulting mosaic (returned) + * \return Pointer to image. + */ + ImageType getMosaic(int &width, int &height); + + /*! + * Provides access to the internal alignment object pointer. + * \return Pointer to the aligner object. + */ + Align* getAligner() { return aligner; } + + /*! + * Obtain initialization state. + * + * return Returns true if initialized, false otherwise. + */ + bool isInitialized() { return initialized; } + + + /*! + * Return codes for mosaic. + */ + static const int MOSAIC_RET_OK = 1; + static const int MOSAIC_RET_ERROR = -1; + static const int MOSAIC_RET_CANCELLED = -2; + static const int MOSAIC_RET_LOW_TEXTURE = -3; + static const int MOSAIC_RET_FEW_INLIERS = 2; + +protected: + + /** + * Size of image frames making up mosaic + */ + int width, height; + + /** + * Size of actual mosaic + */ + int mosaicWidth, mosaicHeight; + + /** + * Bounding box to crop the mosaic when the gray border is not desired. + */ + MosaicRect mosaicCroppingRect; + + ImageType imageMosaicYVU; + + /** + * Collection of frames that will make up mosaic. + */ + MosaicFrame **frames; + + /** + * Subset of frames that are considered as relevant. + */ + MosaicFrame **rframes; + + int frames_size; + int max_frames; + + /** + * Implicitly created frames, should be freed by Mosaic. + */ + ImageType *owned_frames; + int owned_size; + + /** + * Initialization state. + */ + bool initialized; + + /** + * Type of blending to perform. + */ + int blendingType; + + /** + * Type of strip to use. 0: thin (default), 1: wide + */ + int stripType; + + /** + * Pointer to aligner. + */ + Align *aligner; + + /** + * Pointer to blender. + */ + Blend *blender; + + /** + * Modifies TRS matrices so that rotations are balanced + * about center of mosaic + * + * Side effect: TRS matrices of all mosaic frames + * are modified + */ + int balanceRotations(); + +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/MosaicTypes.h b/jni_mosaic/feature_mos/src/mosaic/MosaicTypes.h new file mode 100644 index 0000000000000000000000000000000000000000..395ec458655f66cfbc982315e6afd4415e29630d --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/MosaicTypes.h @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// MosaicTypes.h +// S.O. # : +// Author(s): zkira +// $Id: MosaicTypes.h,v 1.15 2011/06/17 13:35:48 mbansal Exp $ + + +#ifndef MOSAIC_TYPES_H +#define MOSAIC_TYPES_H + +#include "ImageUtils.h" + +/** + * Definition of rectangle in a mosaic. + */ +class MosaicRect +{ + public: + MosaicRect() + { + left = right = top = bottom = 0.0; + } + + inline int Width() + { + return right - left; + } + + inline int Height() + { + return bottom - top; + } + + /** + * Bounds of the rectangle + */ + int left, right, top, bottom; +}; + +class BlendRect +{ + public: + double lft, rgt, top, bot; +}; + +/** + * A frame making up the mosaic. + * Note: Currently assumes a YVU image + * containing separate Y,V, and U planes + * in contiguous memory (in that order). + */ +class MosaicFrame { +public: + ImageType image; + double trs[3][3]; + int width, height; + BlendRect brect; // This frame warped to the Mosaic coordinate system + BlendRect vcrect; // brect clipped using the voronoi neighbors + bool internal_allocation; + + MosaicFrame() { }; + MosaicFrame(int _width, int _height, bool allocate=true) + { + width = _width; + height = _height; + internal_allocation = allocate; + if(internal_allocation) + image = ImageUtils::allocateImage(width, height, ImageUtils::IMAGE_TYPE_NUM_CHANNELS); + } + + + ~MosaicFrame() + { + if(internal_allocation) + if (image) + free(image); + } + + /** + * Get the V plane of the image. + */ + inline ImageType getV() + { + return (image + (width*height)); + } + + /** + * Get the U plane of the image. + */ + inline ImageType getU() + { + return (image + (width*height*2)); + } + + /** + * Get a pixel from the V plane of the image. + */ + inline int getV(int y, int x) + { + ImageType U = image + (width*height); + return U[y*width+x]; + } + + /** + * Get a pixel from the U plane of the image. + */ + inline int getU(int y, int x) + { + ImageType U = image + (width*height*2); + return U[y*width+x]; + } + +}; + +/** + * Structure for describing a warp. + */ +typedef struct { + int horizontal; + double theta; + double x; + double y; + double width; + double radius; + double direction; + double correction; + int blendRange; + int blendRangeUV; + int nlevs; + int nlevsC; + int blendingType; + int stripType; + // Add an overlap to prevent a gap between pictures due to roundoffs + double roundoffOverlap;// 1.5 + +} BlendParams; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/Pyramid.cpp b/jni_mosaic/feature_mos/src/mosaic/Pyramid.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b022d73dbed97b851664d013d7e860b646c5c266 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Pyramid.cpp @@ -0,0 +1,270 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// pyramid.cpp + +#include +#include + +#include "Pyramid.h" + +// We allocate the entire pyramid into one contiguous storage. This makes +// cleanup easier than fragmented stuff. In addition, we added a "pitch" +// field, so pointer manipulation is much simpler when it would be faster. +PyramidShort *PyramidShort::allocatePyramidPacked(real levels, + real width, real height, real border) +{ + real border2 = (real) (border << 1); + int lines, size = calcStorage(width, height, border2, levels, &lines); + + PyramidShort *img = (PyramidShort *) calloc(sizeof(PyramidShort) * levels + + sizeof(short *) * lines + + + sizeof(short) * size, 1); + + if (img) { + PyramidShort *curr, *last; + ImageTypeShort *y = (ImageTypeShort *) &img[levels]; + ImageTypeShort position = (ImageTypeShort) &y[lines]; + for (last = (curr = img) + levels; curr < last; curr++) { + curr->width = width; + curr->height = height; + curr->border = border; + curr->pitch = (real) (width + border2); + curr->ptr = y + border; + + // Assign row pointers + for (int j = height + border2; j--; y++, position += curr->pitch) { + *y = position + border; + } + + width >>= 1; + height >>= 1; + } + } + + return img; +} + +// Allocate an image of type short +PyramidShort *PyramidShort::allocateImage(real width, real height, real border) +{ + real border2 = (real) (border << 1); + PyramidShort *img = (PyramidShort *) + calloc(sizeof(PyramidShort) + sizeof(short *) * (height + border2) + + sizeof(short) * (width + border2) * (height + border2), 1); + + if (img) { + short **y = (short **) &img[1]; + short *position = (short *) &y[height + border2]; + img->width = width; + img->height = height; + img->border = border; + img->pitch = (real) (width + border2); + img->ptr = y + border; + position += border; // Move position down to origin of real image + + // Assign row pointers + for (int j = height + border2; j--; y++, position += img->pitch) { + *y = position; + } + } + + return img; +} + +// Free the images +void PyramidShort::freeImage(PyramidShort *image) +{ + if (image != NULL) + free(image); +} + +// Calculate amount of storage needed taking into account the borders, etc. +unsigned int PyramidShort::calcStorage(real width, real height, real border2, int levels, int *lines) +{ + int size; + + *lines = size = 0; + + while(levels--) { + size += (width + border2) * (height + border2); + *lines += height + border2; + width >>= 1; + height >>= 1; + } + + return size; +} + +void PyramidShort::BorderSpread(PyramidShort *pyr, int left, int right, + int top, int bot) +{ + int off, off2, height, h, w; + ImageTypeShort base; + + if (left || right) { + off = pyr->border - left; + off2 = pyr->width + off + pyr->border - right - 1; + h = pyr->border - top; + height = pyr->height + (h << 1); + base = pyr->ptr[-h] - off; + + // spread in X + for (h = height; h--; base += pyr->pitch) { + for (w = left; w--;) + base[-1 - w] = base[0]; + for (w = right; w--;) + base[off2 + w + 1] = base[off2]; + } + } + + if (top || bot) { + // spread in Y + base = pyr->ptr[top - pyr->border] - pyr->border; + for (h = top; h--; base -= pyr->pitch) { + memcpy(base - pyr->pitch, base, pyr->pitch * sizeof(short)); + } + + base = pyr->ptr[pyr->height + pyr->border - bot] - pyr->border; + for (h = bot; h--; base += pyr->pitch) { + memcpy(base, base - pyr->pitch, pyr->pitch * sizeof(short)); + } + } +} + +void PyramidShort::BorderExpandOdd(PyramidShort *in, PyramidShort *out, PyramidShort *scr, + int mode) +{ + int i,j; + int off = in->border / 2; + + // Vertical Filter + for (j = -off; j < in->height + off; j++) { + int j2 = j * 2; + int limit = scr->width + scr->border; + for (i = -scr->border; i < limit; i++) { + int t1 = in->ptr[j][i]; + int t2 = in->ptr[j+1][i]; + scr->ptr[j2][i] = (short) + ((6 * t1 + (in->ptr[j-1][i] + t2) + 4) >> 3); + scr->ptr[j2+1][i] = (short)((t1 + t2 + 1) >> 1); + } + } + + BorderSpread(scr, 0, 0, 3, 3); + + // Horizontal Filter + int limit = out->height + out->border; + for (j = -out->border; j < limit; j++) { + for (i = -off; i < scr->width + off; i++) { + int i2 = i * 2; + int t1 = scr->ptr[j][i]; + int t2 = scr->ptr[j][i+1]; + out->ptr[j][i2] = (short) (out->ptr[j][i2] + + (mode * ((6 * t1 + + scr->ptr[j][i-1] + t2 + 4) >> 3))); + out->ptr[j][i2+1] = (short) (out->ptr[j][i2+1] + + (mode * ((t1 + t2 + 1) >> 1))); + } + } + +} + +int PyramidShort::BorderExpand(PyramidShort *pyr, int nlev, int mode) +{ + PyramidShort *tpyr = pyr + nlev - 1; + PyramidShort *scr = allocateImage(pyr[1].width, pyr[0].height, pyr->border); + if (scr == NULL) return 0; + + if (mode > 0) { + // Expand and add (reconstruct from Laplacian) + for (; tpyr > pyr; tpyr--) { + scr->width = tpyr[0].width; + scr->height = tpyr[-1].height; + BorderExpandOdd(tpyr, tpyr - 1, scr, 1); + } + } + else if (mode < 0) { + // Expand and subtract (build Laplacian) + while ((pyr++) < tpyr) { + scr->width = pyr[0].width; + scr->height = pyr[-1].height; + BorderExpandOdd(pyr, pyr - 1, scr, -1); + } + } + + freeImage(scr); + return 1; +} + +void PyramidShort::BorderReduceOdd(PyramidShort *in, PyramidShort *out, PyramidShort *scr) +{ + ImageTypeShortBase *s, *ns, *ls, *p, *np; + + int off = scr->border - 2; + s = scr->ptr[-scr->border] - (off >> 1); + ns = s + scr->pitch; + ls = scr->ptr[scr->height + scr->border - 1] + scr->pitch - (off >> 1); + int width = scr->width + scr->border; + p = in->ptr[-scr->border] - off; + np = p + in->pitch; + + // treat it as if the whole thing were the image + for (; s < ls; s = ns, ns += scr->pitch, p = np, np += in->pitch) { + for (int w = width; w--; s++, p += 2) { + *s = (short)((((int) p[-2]) + ((int) p[2]) + 8 + // 1 + ((((int) p[-1]) + ((int) p[1])) << 2) + // 4 + ((int) *p) * 6) >> 4); // 6 + } + } + + BorderSpread(scr, 5, 4 + ((in->width ^ 1) & 1), 0, 0); // + + s = out->ptr[-(off >> 1)] - out->border; + ns = s + out->pitch; + ls = s + out->pitch * (out->height + off); + p = scr->ptr[-off] - out->border; + int pitch = scr->pitch; + int pitch2 = pitch << 1; + np = p + pitch2; + for (; s < ls; s = ns, ns += out->pitch, p = np, np += pitch2) { + for (int w = out->pitch; w--; s++, p++) { + *s = (short)((((int) p[-pitch2]) + ((int) p[pitch2]) + 8 + // 1 + ((((int) p[-pitch]) + ((int) p[pitch])) << 2) + // 4 + ((int) *p) * 6) >> 4); // 6 + } + } + BorderSpread(out, 0, 0, 5, 5); + +} + +int PyramidShort::BorderReduce(PyramidShort *pyr, int nlev) +{ + PyramidShort *scr = allocateImage(pyr[1].width, pyr[0].height, pyr->border); + if (scr == NULL) + return 0; + + BorderSpread(pyr, pyr->border, pyr->border, pyr->border, pyr->border); + while (--nlev) { + BorderReduceOdd(pyr, pyr + 1, scr); + pyr++; + scr->width = pyr[1].width; + scr->height = pyr[0].height; + } + + freeImage(scr); + return 1; +} diff --git a/jni_mosaic/feature_mos/src/mosaic/Pyramid.h b/jni_mosaic/feature_mos/src/mosaic/Pyramid.h new file mode 100644 index 0000000000000000000000000000000000000000..c5fe907147371b35133d3e5f292c8a667d446ed4 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/Pyramid.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Pyramid.h + +#ifndef PYRAMID_H +#define PYRAMID_H + +#include "ImageUtils.h" + +typedef unsigned short int real; + +// Structure containing a packed pyramid of type ImageTypeShort. Used for pyramid +// blending, among other things. + +class PyramidShort +{ + +public: + + ImageTypeShort *ptr; // Pointer containing the image + real width, height; // Width and height of input images + real numChannels; // Number of channels in input images + real border; // border size + real pitch; // Pitch. Used for moving through image efficiently. + + static PyramidShort *allocatePyramidPacked(real width, real height, real levels, real border = 0); + static PyramidShort *allocateImage(real width, real height, real border); + static void createPyramid(ImageType image, PyramidShort *pyramid, int last = 3 ); + static void freeImage(PyramidShort *image); + + static unsigned int calcStorage(real width, real height, real border2, int levels, int *lines); + + static void BorderSpread(PyramidShort *pyr, int left, int right, int top, int bot); + static void BorderExpandOdd(PyramidShort *in, PyramidShort *out, PyramidShort *scr, int mode); + static int BorderExpand(PyramidShort *pyr, int nlev, int mode); + static int BorderReduce(PyramidShort *pyr, int nlev); + static void BorderReduceOdd(PyramidShort *in, PyramidShort *out, PyramidShort *scr); +}; + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic/trsMatrix.cpp b/jni_mosaic/feature_mos/src/mosaic/trsMatrix.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fc6a86b3c47bcf5b47e699f867370dd67f185ad --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/trsMatrix.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// trsMatrix.cpp +// $Id: trsMatrix.cpp,v 1.9 2011/06/17 13:35:48 mbansal Exp $ + +#include "stdio.h" +#include +#include "trsMatrix.h" + +void mult33d(double a[3][3], double b[3][3], double c[3][3]) +{ + a[0][0] = b[0][0]*c[0][0] + b[0][1]*c[1][0] + b[0][2]*c[2][0]; + a[0][1] = b[0][0]*c[0][1] + b[0][1]*c[1][1] + b[0][2]*c[2][1]; + a[0][2] = b[0][0]*c[0][2] + b[0][1]*c[1][2] + b[0][2]*c[2][2]; + a[1][0] = b[1][0]*c[0][0] + b[1][1]*c[1][0] + b[1][2]*c[2][0]; + a[1][1] = b[1][0]*c[0][1] + b[1][1]*c[1][1] + b[1][2]*c[2][1]; + a[1][2] = b[1][0]*c[0][2] + b[1][1]*c[1][2] + b[1][2]*c[2][2]; + a[2][0] = b[2][0]*c[0][0] + b[2][1]*c[1][0] + b[2][2]*c[2][0]; + a[2][1] = b[2][0]*c[0][1] + b[2][1]*c[1][1] + b[2][2]*c[2][1]; + a[2][2] = b[2][0]*c[0][2] + b[2][1]*c[1][2] + b[2][2]*c[2][2]; +} + + +// normProjMat33d +// m = input matrix +// return: result if successful +int normProjMat33d(double m[3][3]) +{ + double m22; + + if(m[2][2] == 0.0) + { + return 0; +} + + m[0][0] /= m[2][2]; + m[0][1] /= m[2][2]; + m[0][2] /= m[2][2]; + m[1][0] /= m[2][2]; + m[1][1] /= m[2][2]; + m[1][2] /= m[2][2]; + m[2][0] /= m[2][2]; + m[2][1] /= m[2][2]; + m[2][2] = 1.0; + + return 1; +} + +// det33d +// m = input matrix +// returns: determinant +double det33d(const double m[3][3]) +{ + double result; + + result = m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]); + result += m[0][1] * (m[1][2] * m[2][0] - m[1][0] * m[2][2]); + result += m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); + + return result; +} + +// inv33d +// +void inv33d(const double m[3][3], double out[3][3]) +{ + double det = det33d(m); + + out[0][0] = (m[1][1]*m[2][2] - m[1][2]*m[2][1]) / det; + out[1][0] = (m[1][2]*m[2][0] - m[1][0]*m[2][2]) / det; + out[2][0] = (m[1][0]*m[2][1] - m[1][1]*m[2][0]) / det; + + out[0][1] = (m[0][2]*m[2][1] - m[0][1]*m[2][2]) / det; + out[1][1] = (m[0][0]*m[2][2] - m[0][2]*m[2][0]) / det; + out[2][1] = (m[0][1]*m[2][0] - m[0][0]*m[2][1]) / det; + + out[0][2] = (m[0][1]*m[1][2] - m[0][2]*m[1][1]) / det; + out[1][2] = (m[0][2]*m[1][0] - m[0][0]*m[1][2]) / det; + out[2][2] = (m[0][0]*m[1][1] - m[0][1]*m[1][0]) / det; +} diff --git a/jni_mosaic/feature_mos/src/mosaic/trsMatrix.h b/jni_mosaic/feature_mos/src/mosaic/trsMatrix.h new file mode 100644 index 0000000000000000000000000000000000000000..054cc3335c030f7fbf5621dbe2d11d667fbe30c4 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/trsMatrix.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// trsMatrix.h +// $Id: trsMatrix.h,v 1.8 2011/06/17 13:35:48 mbansal Exp $ + +#ifndef TRSMATRIX_H_ +#define TRSMATRIX_H_ + + +// Calculate the determinant of a matrix +double det33d(const double m[3][3]); + +// Invert a matrix +void inv33d(const double m[3][3], double out[3][3]); + +// Multiply a = b * c +void mult33d(double a[3][3], double b[3][3], double c[3][3]); + +// Normalize matrix so matrix[2][2] is '1' +int normProjMat33d(double m[3][3]); + +inline double ProjZ(double trs[3][3], double x, double y, double f) +{ + return ((trs)[2][0]*(x) + (trs)[2][1]*(y) + (trs)[2][2]*(f)); +} + +inline double ProjX(double trs[3][3], double x, double y, double z, double f) +{ + return (((trs)[0][0]*(x) + (trs)[0][1]*(y) + (trs)[0][2]*(f)) / (z)); +} + +inline double ProjY(double trs[3][3], double x, double y, double z, double f) +{ + return (((trs)[1][0]*(x) + (trs)[1][1]*(y) + (trs)[1][2]*(f)) / (z)); +} + + +#endif diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.cpp b/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a956f23b77da5dd1ef129a6656b2d66260415152 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.cpp @@ -0,0 +1,98 @@ +#include "FrameBuffer.h" + +FrameBuffer::FrameBuffer() +{ + Reset(); +} + +FrameBuffer::~FrameBuffer() { +} + +void FrameBuffer::Reset() { + mFrameBufferName = -1; + mTextureName = -1; + mWidth = 0; + mHeight = 0; + mFormat = -1; +} + +bool FrameBuffer::InitializeGLContext() { + Reset(); + return CreateBuffers(); +} + +bool FrameBuffer::Init(int width, int height, GLenum format) { + if (mFrameBufferName == (GLuint)-1) { + if (!CreateBuffers()) { + return false; + } + } + glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName); + glBindTexture(GL_TEXTURE_2D, mTextureName); + + glTexImage2D(GL_TEXTURE_2D, + 0, + format, + width, + height, + 0, + format, + GL_UNSIGNED_BYTE, + NULL); + if (!checkGlError("bind/teximage")) { + return false; + } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // This is necessary to work with user-generated frame buffers with + // dimensions that are NOT powers of 2. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + // Attach texture to frame buffer. + glFramebufferTexture2D(GL_FRAMEBUFFER, + GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, + mTextureName, + 0); + checkFramebufferStatus("FrameBuffer.cpp"); + checkGlError("framebuffertexture2d"); + + if (!checkGlError("texture setup")) { + return false; + } + mWidth = width; + mHeight = height; + mFormat = format; + glBindFramebuffer(GL_FRAMEBUFFER, 0); + return true; +} + +bool FrameBuffer::CreateBuffers() { + glGenFramebuffers(1, &mFrameBufferName); + glGenTextures(1, &mTextureName); + if (!checkGlError("texture generation")) { + return false; + } + return true; +} + +GLuint FrameBuffer::GetTextureName() const { + return mTextureName; +} + +GLuint FrameBuffer::GetFrameBufferName() const { + return mFrameBufferName; +} + +GLenum FrameBuffer::GetFormat() const { + return mFormat; +} + +int FrameBuffer::GetWidth() const { + return mWidth; +} + +int FrameBuffer::GetHeight() const { + return mHeight; +} diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.h b/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.h new file mode 100755 index 0000000000000000000000000000000000000000..314b1262285c5064d801bcc1fecb8c617e67149c --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/FrameBuffer.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include + +#define checkGlError(op) checkGLErrorDetail(__FILE__, __LINE__, (op)) + +extern bool checkGLErrorDetail(const char* file, int line, const char* op); +extern void checkFramebufferStatus(const char* name); + +class FrameBuffer { + public: + FrameBuffer(); + virtual ~FrameBuffer(); + + bool InitializeGLContext(); + bool Init(int width, int height, GLenum format); + GLuint GetTextureName() const; + GLuint GetFrameBufferName() const; + GLenum GetFormat() const; + + int GetWidth() const; + int GetHeight() const; + + private: + void Reset(); + bool CreateBuffers(); + GLuint mFrameBufferName; + GLuint mTextureName; + int mWidth; + int mHeight; + GLenum mFormat; +}; diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.cpp b/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b9938eb6bfa6fd6e4db47b5982cf9fb0459a9c7d --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.cpp @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Renderer.h" + +#include "mosaic/Log.h" +#define LOG_TAG "Renderer" + +#include + +Renderer::Renderer() + : mGlProgram(0), + mInputTextureName(-1), + mInputTextureWidth(0), + mInputTextureHeight(0), + mSurfaceWidth(0), + mSurfaceHeight(0) +{ + InitializeGLContext(); +} + +Renderer::~Renderer() { +} + +GLuint Renderer::loadShader(GLenum shaderType, const char* pSource) { + GLuint shader = glCreateShader(shaderType); + if (shader) { + glShaderSource(shader, 1, &pSource, NULL); + glCompileShader(shader); + GLint compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen) { + char* buf = (char*) malloc(infoLen); + if (buf) { + glGetShaderInfoLog(shader, infoLen, NULL, buf); + LOGE("Could not compile shader %d:\n%s\n", + shaderType, buf); + free(buf); + } + glDeleteShader(shader); + shader = 0; + } + } + } + return shader; +} + +GLuint Renderer::createProgram(const char* pVertexSource, const char* pFragmentSource) +{ + GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); + if (!vertexShader) + { + return 0; + } + + GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); + if (!pixelShader) + { + return 0; + } + + GLuint program = glCreateProgram(); + if (program) + { + glAttachShader(program, vertexShader); + checkGlError("glAttachShader"); + glAttachShader(program, pixelShader); + checkGlError("glAttachShader"); + + glLinkProgram(program); + GLint linkStatus = GL_FALSE; + glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); + + LOGI("Program Linked (%d)!", program); + + if (linkStatus != GL_TRUE) + { + GLint bufLength = 0; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); + if (bufLength) + { + char* buf = (char*) malloc(bufLength); + if (buf) + { + glGetProgramInfoLog(program, bufLength, NULL, buf); + LOGE("Could not link program:\n%s\n", buf); + free(buf); + } + } + glDeleteProgram(program); + program = 0; + } + } + return program; +} + +// Set this renderer to use the default frame-buffer (screen) and +// set the viewport size to be the given width and height (pixels). +bool Renderer::SetupGraphics(int width, int height) +{ + bool succeeded = false; + do { + if (mGlProgram == 0) + { + if (!InitializeGLProgram()) + { + break; + } + } + glUseProgram(mGlProgram); + if (!checkGlError("glUseProgram")) break; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + mFrameBuffer = NULL; + mSurfaceWidth = width; + mSurfaceHeight = height; + + glViewport(0, 0, mSurfaceWidth, mSurfaceHeight); + if (!checkGlError("glViewport")) break; + succeeded = true; + } while (false); + + return succeeded; +} + + +// Set this renderer to use the specified FBO and +// set the viewport size to be the width and height of this FBO. +bool Renderer::SetupGraphics(FrameBuffer* buffer) +{ + bool succeeded = false; + do { + if (mGlProgram == 0) + { + if (!InitializeGLProgram()) + { + break; + } + } + glUseProgram(mGlProgram); + if (!checkGlError("glUseProgram")) break; + + glBindFramebuffer(GL_FRAMEBUFFER, buffer->GetFrameBufferName()); + + mFrameBuffer = buffer; + mSurfaceWidth = mFrameBuffer->GetWidth(); + mSurfaceHeight = mFrameBuffer->GetHeight(); + + glViewport(0, 0, mSurfaceWidth, mSurfaceHeight); + if (!checkGlError("glViewport")) break; + succeeded = true; + } while (false); + + return succeeded; +} + +bool Renderer::Clear(float r, float g, float b, float a) +{ + bool succeeded = false; + do { + bool rt = (mFrameBuffer == NULL)? + SetupGraphics(mSurfaceWidth, mSurfaceHeight) : + SetupGraphics(mFrameBuffer); + + if(!rt) + break; + + glClearColor(r, g, b, a); + glClear(GL_COLOR_BUFFER_BIT); + + succeeded = true; + } while (false); + return succeeded; + +} + +void Renderer::InitializeGLContext() +{ + if(mFrameBuffer != NULL) + { + delete mFrameBuffer; + mFrameBuffer = NULL; + } + + mInputTextureName = -1; + mInputTextureType = GL_TEXTURE_2D; + mGlProgram = 0; +} + +int Renderer::GetTextureName() +{ + return mInputTextureName; +} + +void Renderer::SetInputTextureName(GLuint textureName) +{ + mInputTextureName = textureName; +} + +void Renderer::SetInputTextureType(GLenum textureType) +{ + mInputTextureType = textureType; +} + +void Renderer::SetInputTextureDimensions(int width, int height) +{ + mInputTextureWidth = width; + mInputTextureHeight = height; +} diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.h b/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.h new file mode 100755 index 0000000000000000000000000000000000000000..a43e8028e49f1bdaf420457a2a5045908322f271 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/Renderer.h @@ -0,0 +1,65 @@ +#pragma once + +#include "FrameBuffer.h" + +#include + +#include +#include +#include + +class Renderer { + public: + Renderer(); + virtual ~Renderer(); + + // Initialize OpenGL resources + // @return true if successful + virtual bool InitializeGLProgram() = 0; + + bool SetupGraphics(FrameBuffer* buffer); + bool SetupGraphics(int width, int height); + + bool Clear(float r, float g, float b, float a); + + int GetTextureName(); + void SetInputTextureName(GLuint textureName); + void SetInputTextureDimensions(int width, int height); + void SetInputTextureType(GLenum textureType); + + void InitializeGLContext(); + + protected: + + GLuint loadShader(GLenum shaderType, const char* pSource); + GLuint createProgram(const char*, const char* ); + + int SurfaceWidth() const { return mSurfaceWidth; } + int SurfaceHeight() const { return mSurfaceHeight; } + + // Source code for shaders. + virtual const char* VertexShaderSource() const = 0; + virtual const char* FragmentShaderSource() const = 0; + + // Redefine this to use special texture types such as + // GL_TEXTURE_EXTERNAL_OES. + GLenum InputTextureType() const { return mInputTextureType; } + + GLuint mGlProgram; + GLuint mInputTextureName; + GLenum mInputTextureType; + int mInputTextureWidth; + int mInputTextureHeight; + + // Attribute locations + GLint mScalingtransLoc; + GLint maPositionHandle; + GLint maTextureHandle; + + + int mSurfaceWidth; // Width of target surface. + int mSurfaceHeight; // Height of target surface. + + FrameBuffer *mFrameBuffer; +}; + diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.cpp b/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..88aac362667926781adfaceff3c0ce093825d7d8 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SurfaceTextureRenderer.h" + +#include +const GLfloat g_vVertices[] = { + -1.f, -1.f, 0.0f, 1.0f, // Position 0 + 0.0f, 0.0f, // TexCoord 0 + 1.f, -1.f, 0.0f, 1.0f, // Position 1 + 1.0f, 0.0f, // TexCoord 1 + -1.f, 1.f, 0.0f, 1.0f, // Position 2 + 0.0f, 1.0f, // TexCoord 2 + 1.f, 1.f, 0.0f, 1.0f, // Position 3 + 1.0f, 1.0f // TexCoord 3 +}; +GLushort g_iIndices2[] = { 0, 1, 2, 3 }; + +const int GL_TEXTURE_EXTERNAL_OES_ENUM = 0x8D65; + +const int VERTEX_STRIDE = 6 * sizeof(GLfloat); + +SurfaceTextureRenderer::SurfaceTextureRenderer() : Renderer() { + memset(mSTMatrix, 0.0, 16*sizeof(float)); + mSTMatrix[0] = 1.0f; + mSTMatrix[5] = 1.0f; + mSTMatrix[10] = 1.0f; + mSTMatrix[15] = 1.0f; +} + +SurfaceTextureRenderer::~SurfaceTextureRenderer() { +} + +void SurfaceTextureRenderer::SetViewportMatrix(int w, int h, int W, int H) +{ + for(int i=0; i<16; i++) + { + mViewportMatrix[i] = 0.0f; + } + + mViewportMatrix[0] = float(w)/float(W); + mViewportMatrix[5] = float(h)/float(H); + mViewportMatrix[10] = 1.0f; + mViewportMatrix[12] = -1.0f + float(w)/float(W); + mViewportMatrix[13] = -1.0f + float(h)/float(H); + mViewportMatrix[15] = 1.0f; +} + +void SurfaceTextureRenderer::SetScalingMatrix(float xscale, float yscale) +{ + for(int i=0; i<16; i++) + { + mScalingMatrix[i] = 0.0f; + } + + mScalingMatrix[0] = xscale; + mScalingMatrix[5] = yscale; + mScalingMatrix[10] = 1.0f; + mScalingMatrix[15] = 1.0f; +} + +void SurfaceTextureRenderer::SetSTMatrix(float *stmat) +{ + memcpy(mSTMatrix, stmat, 16*sizeof(float)); +} + + +bool SurfaceTextureRenderer::InitializeGLProgram() +{ + bool succeeded = false; + do { + GLuint glProgram; + glProgram = createProgram(VertexShaderSource(), + FragmentShaderSource()); + if (!glProgram) { + break; + } + + glUseProgram(glProgram); + if (!checkGlError("glUseProgram")) break; + + maPositionHandle = glGetAttribLocation(glProgram, "aPosition"); + checkGlError("glGetAttribLocation aPosition"); + maTextureHandle = glGetAttribLocation(glProgram, "aTextureCoord"); + checkGlError("glGetAttribLocation aTextureCoord"); + muSTMatrixHandle = glGetUniformLocation(glProgram, "uSTMatrix"); + checkGlError("glGetUniformLocation uSTMatrix"); + mScalingtransLoc = glGetUniformLocation(glProgram, "u_scalingtrans"); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + mGlProgram = glProgram; + succeeded = true; + } while (false); + + if (!succeeded && (mGlProgram != 0)) + { + glDeleteProgram(mGlProgram); + checkGlError("glDeleteProgram"); + mGlProgram = 0; + } + return succeeded; +} + +bool SurfaceTextureRenderer::DrawTexture(GLfloat *affine) +{ + bool succeeded = false; + do { + bool rt = (mFrameBuffer == NULL)? + SetupGraphics(mSurfaceWidth, mSurfaceHeight) : + SetupGraphics(mFrameBuffer); + + if(!rt) + break; + + glDisable(GL_BLEND); + + glActiveTexture(GL_TEXTURE0); + if (!checkGlError("glActiveTexture")) break; + + const GLenum texture_type = InputTextureType(); + glBindTexture(texture_type, mInputTextureName); + if (!checkGlError("glBindTexture")) break; + + glUniformMatrix4fv(mScalingtransLoc, 1, GL_FALSE, mScalingMatrix); + glUniformMatrix4fv(muSTMatrixHandle, 1, GL_FALSE, mSTMatrix); + + // Load the vertex position + glVertexAttribPointer(maPositionHandle, 4, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, g_vVertices); + glEnableVertexAttribArray(maPositionHandle); + // Load the texture coordinate + glVertexAttribPointer(maTextureHandle, 2, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, &g_vVertices[4]); + glEnableVertexAttribArray(maTextureHandle); + + // And, finally, execute the GL draw command. + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, g_iIndices2); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + succeeded = true; + } while (false); + return succeeded; +} + +const char* SurfaceTextureRenderer::VertexShaderSource() const +{ + static const char gVertexShader[] = + "uniform mat4 uSTMatrix;\n" + "uniform mat4 u_scalingtrans; \n" + "attribute vec4 aPosition;\n" + "attribute vec4 aTextureCoord;\n" + "varying vec2 vTextureNormCoord;\n" + "void main() {\n" + " gl_Position = u_scalingtrans * aPosition;\n" + " vTextureNormCoord = (uSTMatrix * aTextureCoord).xy;\n" + "}\n"; + + return gVertexShader; +} + +const char* SurfaceTextureRenderer::FragmentShaderSource() const +{ + static const char gFragmentShader[] = + "#extension GL_OES_EGL_image_external : require\n" + "precision mediump float;\n" + "varying vec2 vTextureNormCoord;\n" + "uniform samplerExternalOES sTexture;\n" + "void main() {\n" + " gl_FragColor = texture2D(sTexture, vTextureNormCoord);\n" + "}\n"; + + return gFragmentShader; +} diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.h b/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.h new file mode 100755 index 0000000000000000000000000000000000000000..ea2b81adea3f5e7c80873f775e15f6a32d07ab90 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/SurfaceTextureRenderer.h @@ -0,0 +1,44 @@ +#pragma once + +#include "FrameBuffer.h" +#include "Renderer.h" + +#include + +#include +#include +#include + +class SurfaceTextureRenderer: public Renderer { + public: + SurfaceTextureRenderer(); + virtual ~SurfaceTextureRenderer(); + + // Initialize OpenGL resources + // @return true if successful + bool InitializeGLProgram(); + + bool DrawTexture(GLfloat *affine); + + void SetViewportMatrix(int w, int h, int W, int H); + void SetScalingMatrix(float xscale, float yscale); + void SetSTMatrix(float *stmat); + + private: + // Source code for shaders. + const char* VertexShaderSource() const; + const char* FragmentShaderSource() const; + + // Attribute locations + GLint mScalingtransLoc; + GLint muSTMatrixHandle; + GLint maPositionHandle; + GLint maTextureHandle; + + GLfloat mViewportMatrix[16]; + GLfloat mScalingMatrix[16]; + + GLfloat mSTMatrix[16]; + +}; + diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.cpp b/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..af6779a3fd44378de5ab19b5ba417e3cbb2a6003 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.cpp @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "WarpRenderer.h" + +#include + +const GLfloat g_vVertices[] = { + -1.f, 1.f, 0.0f, 1.0f, // Position 0 + 0.0f, 1.0f, // TexCoord 0 + 1.f, 1.f, 0.0f, 1.0f, // Position 1 + 1.0f, 1.0f, // TexCoord 1 + -1.f, -1.f, 0.0f, 1.0f, // Position 2 + 0.0f, 0.0f, // TexCoord 2 + 1.f, -1.f, 0.0f, 1.0f, // Position 3 + 1.0f, 0.0f // TexCoord 3 +}; + +const int VERTEX_STRIDE = 6 * sizeof(GLfloat); + +GLushort g_iIndices[] = { 0, 1, 2, 3 }; + +WarpRenderer::WarpRenderer() : Renderer() +{ +} + +WarpRenderer::~WarpRenderer() { +} + +void WarpRenderer::SetViewportMatrix(int w, int h, int W, int H) +{ + for(int i=0; i<16; i++) + { + mViewportMatrix[i] = 0.0f; + } + + mViewportMatrix[0] = float(w)/float(W); + mViewportMatrix[5] = float(h)/float(H); + mViewportMatrix[10] = 1.0f; + mViewportMatrix[12] = -1.0f + float(w)/float(W); + mViewportMatrix[13] = -1.0f + float(h)/float(H); + mViewportMatrix[15] = 1.0f; +} + +void WarpRenderer::SetScalingMatrix(float xscale, float yscale) +{ + for(int i=0; i<16; i++) + { + mScalingMatrix[i] = 0.0f; + } + + mScalingMatrix[0] = xscale; + mScalingMatrix[5] = yscale; + mScalingMatrix[10] = 1.0f; + mScalingMatrix[15] = 1.0f; +} + +bool WarpRenderer::InitializeGLProgram() +{ + bool succeeded = false; + do { + GLuint glProgram; + glProgram = createProgram(VertexShaderSource(), + FragmentShaderSource()); + if (!glProgram) { + break; + } + + glUseProgram(glProgram); + if (!checkGlError("glUseProgram")) break; + + // Get attribute locations + mPositionLoc = glGetAttribLocation(glProgram, "a_position"); + mAffinetransLoc = glGetUniformLocation(glProgram, "u_affinetrans"); + mViewporttransLoc = glGetUniformLocation(glProgram, "u_viewporttrans"); + mScalingtransLoc = glGetUniformLocation(glProgram, "u_scalingtrans"); + mTexCoordLoc = glGetAttribLocation(glProgram, "a_texCoord"); + + // Get sampler location + mSamplerLoc = glGetUniformLocation(glProgram, "s_texture"); + + mGlProgram = glProgram; + succeeded = true; + } while (false); + + if (!succeeded && (mGlProgram != 0)) + { + glDeleteProgram(mGlProgram); + checkGlError("glDeleteProgram"); + mGlProgram = 0; + } + return succeeded; +} + +bool WarpRenderer::DrawTexture(GLfloat *affine) +{ + bool succeeded = false; + do { + bool rt = (mFrameBuffer == NULL)? + SetupGraphics(mSurfaceWidth, mSurfaceHeight) : + SetupGraphics(mFrameBuffer); + + if(!rt) + break; + + glDisable(GL_BLEND); + + glActiveTexture(GL_TEXTURE0); + if (!checkGlError("glActiveTexture")) break; + + const GLenum texture_type = InputTextureType(); + glBindTexture(texture_type, mInputTextureName); + if (!checkGlError("glBindTexture")) break; + + // Set the sampler texture unit to 0 + glUniform1i(mSamplerLoc, 0); + + // Load the vertex position + glVertexAttribPointer(mPositionLoc, 4, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, g_vVertices); + + // Load the texture coordinate + glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, &g_vVertices[4]); + + glEnableVertexAttribArray(mPositionLoc); + glEnableVertexAttribArray(mTexCoordLoc); + + // pass matrix information to the vertex shader + glUniformMatrix4fv(mAffinetransLoc, 1, GL_FALSE, affine); + glUniformMatrix4fv(mViewporttransLoc, 1, GL_FALSE, mViewportMatrix); + glUniformMatrix4fv(mScalingtransLoc, 1, GL_FALSE, mScalingMatrix); + + // And, finally, execute the GL draw command. + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, g_iIndices); + + checkGlError("glDrawElements"); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + succeeded = true; + } while (false); + return succeeded; +} + +const char* WarpRenderer::VertexShaderSource() const +{ + static const char gVertexShader[] = + "uniform mat4 u_affinetrans; \n" + "uniform mat4 u_viewporttrans; \n" + "uniform mat4 u_scalingtrans; \n" + "attribute vec4 a_position; \n" + "attribute vec2 a_texCoord; \n" + "varying vec2 v_texCoord; \n" + "void main() \n" + "{ \n" + " gl_Position = u_scalingtrans * u_viewporttrans * u_affinetrans * a_position; \n" + " v_texCoord = a_texCoord; \n" + "} \n"; + + return gVertexShader; +} + +const char* WarpRenderer::FragmentShaderSource() const +{ + static const char gFragmentShader[] = + "precision mediump float; \n" + "varying vec2 v_texCoord; \n" + "uniform sampler2D s_texture; \n" + "void main() \n" + "{ \n" + " vec4 color; \n" + " color = texture2D(s_texture, v_texCoord); \n" + " gl_FragColor = color; \n" + "} \n"; + + return gFragmentShader; +} diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.h b/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.h new file mode 100755 index 0000000000000000000000000000000000000000..8e9a694ece8c56915539638232357598a18728ea --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/WarpRenderer.h @@ -0,0 +1,48 @@ +#pragma once + +#include "FrameBuffer.h" +#include "Renderer.h" + +#include + +#include +#include +#include + +class WarpRenderer: public Renderer { + public: + WarpRenderer(); + virtual ~WarpRenderer(); + + // Initialize OpenGL resources + // @return true if successful + bool InitializeGLProgram(); + + void SetViewportMatrix(int w, int h, int W, int H); + void SetScalingMatrix(float xscale, float yscale); + + bool DrawTexture(GLfloat *affine); + + private: + // Source code for shaders. + const char* VertexShaderSource() const; + const char* FragmentShaderSource() const; + + GLuint mTexHandle; // Handle to s_texture. + GLuint mTexCoordHandle; // Handle to a_texCoord. + GLuint mTriangleVerticesHandle; // Handle to vPosition. + + // Attribute locations + GLint mPositionLoc; + GLint mAffinetransLoc; + GLint mViewporttransLoc; + GLint mScalingtransLoc; + GLint mTexCoordLoc; + + GLfloat mViewportMatrix[16]; + GLfloat mScalingMatrix[16]; + + // Sampler location + GLint mSamplerLoc; +}; + diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.cpp b/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.cpp new file mode 100755 index 0000000000000000000000000000000000000000..f7dcf6f6131c8661a2a383d1ce5556fb532ef0fb --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "YVURenderer.h" + +#include + +const GLfloat g_vVertices[] = { + -1.f, 1.f, 0.0f, 1.0f, // Position 0 + 0.0f, 1.0f, // TexCoord 0 + 1.f, 1.f, 0.0f, 1.0f, // Position 1 + 1.0f, 1.0f, // TexCoord 1 + -1.f, -1.f, 0.0f, 1.0f, // Position 2 + 0.0f, 0.0f, // TexCoord 2 + 1.f, -1.f, 0.0f, 1.0f, // Position 3 + 1.0f, 0.0f // TexCoord 3 +}; + +const int VERTEX_STRIDE = 6 * sizeof(GLfloat); + +GLushort g_iIndices3[] = { 0, 1, 2, 3 }; + +YVURenderer::YVURenderer() : Renderer() + { +} + +YVURenderer::~YVURenderer() { +} + +bool YVURenderer::InitializeGLProgram() +{ + bool succeeded = false; + do { + GLuint glProgram; + glProgram = createProgram(VertexShaderSource(), + FragmentShaderSource()); + if (!glProgram) { + break; + } + + glUseProgram(glProgram); + if (!checkGlError("glUseProgram")) break; + + // Get attribute locations + mPositionLoc = glGetAttribLocation(glProgram, "a_Position"); + mTexCoordLoc = glGetAttribLocation(glProgram, "a_texCoord"); + + // Get sampler location + mSamplerLoc = glGetUniformLocation(glProgram, "s_texture"); + + mGlProgram = glProgram; + succeeded = true; + } while (false); + + if (!succeeded && (mGlProgram != 0)) + { + glDeleteProgram(mGlProgram); + checkGlError("glDeleteProgram"); + mGlProgram = 0; + } + return succeeded; +} + +bool YVURenderer::DrawTexture() +{ + bool succeeded = false; + do { + bool rt = (mFrameBuffer == NULL)? + SetupGraphics(mSurfaceWidth, mSurfaceHeight) : + SetupGraphics(mFrameBuffer); + + if(!rt) + break; + + glDisable(GL_BLEND); + + glActiveTexture(GL_TEXTURE0); + if (!checkGlError("glActiveTexture")) break; + + const GLenum texture_type = InputTextureType(); + glBindTexture(texture_type, mInputTextureName); + if (!checkGlError("glBindTexture")) break; + + // Set the sampler texture unit to 0 + glUniform1i(mSamplerLoc, 0); + + // Load the vertex position + glVertexAttribPointer(mPositionLoc, 4, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, g_vVertices); + + // Load the texture coordinate + glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, + GL_FALSE, VERTEX_STRIDE, &g_vVertices[4]); + + glEnableVertexAttribArray(mPositionLoc); + glEnableVertexAttribArray(mTexCoordLoc); + + // And, finally, execute the GL draw command. + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, g_iIndices3); + + checkGlError("glDrawElements"); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + succeeded = true; + } while (false); + return succeeded; +} + +const char* YVURenderer::VertexShaderSource() const +{ + // All this really does is copy the coordinates into + // variables for the fragment shader to pick up. + static const char gVertexShader[] = + "attribute vec4 a_Position;\n" + "attribute vec2 a_texCoord;\n" + "varying vec2 v_texCoord;\n" + "void main() {\n" + " gl_Position = a_Position;\n" + " v_texCoord = a_texCoord;\n" + "}\n"; + + return gVertexShader; +} + +const char* YVURenderer::FragmentShaderSource() const +{ + static const char gFragmentShader[] = + "precision mediump float;\n" + "uniform sampler2D s_texture;\n" + "const vec4 coeff_y = vec4(0.257, 0.594, 0.098, 0.063);\n" + "const vec4 coeff_v = vec4(0.439, -0.368, -0.071, 0.500);\n" + "const vec4 coeff_u = vec4(-0.148, -0.291, 0.439, 0.500);\n" + "varying vec2 v_texCoord;\n" + "void main() {\n" + " vec4 p;\n" + " p = texture2D(s_texture, v_texCoord);\n" + " gl_FragColor[0] = dot(p, coeff_y);\n" + " p = texture2D(s_texture, v_texCoord);\n" + " gl_FragColor[1] = dot(p, coeff_v);\n" + " p = texture2D(s_texture, v_texCoord);\n" + " gl_FragColor[2] = dot(p, coeff_u);\n" + " p = texture2D(s_texture, v_texCoord);\n" + " gl_FragColor[3] = dot(p, coeff_y);\n" + "}\n"; + + return gFragmentShader; +} diff --git a/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.h b/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.h new file mode 100755 index 0000000000000000000000000000000000000000..d14a4b9906335b8d7d959623348149d571839183 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic_renderer/YVURenderer.h @@ -0,0 +1,35 @@ +#pragma once + +#include "FrameBuffer.h" +#include "Renderer.h" + +#include + +#include +#include +#include + +class YVURenderer: public Renderer { + public: + YVURenderer(); + virtual ~YVURenderer(); + + // Initialize OpenGL resources + // @return true if successful + bool InitializeGLProgram(); + + bool DrawTexture(); + + private: + // Source code for shaders. + const char* VertexShaderSource() const; + const char* FragmentShaderSource() const; + + // Attribute locations + GLint mPositionLoc; + GLint mTexCoordLoc; + + // Sampler location + GLint mSamplerLoc; +}; + diff --git a/jni_mosaic/feature_mos_jni.cpp b/jni_mosaic/feature_mos_jni.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fa792a6b390a09dcfdb3bd591470e42f1684436 --- /dev/null +++ b/jni_mosaic/feature_mos_jni.cpp @@ -0,0 +1,669 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +* + */ +#include +#include +#include +#include +#include +#include + +#include "mosaic/AlignFeatures.h" +#include "mosaic/Blend.h" +#include "mosaic/Mosaic.h" +#include "mosaic/Log.h" +#define LOG_TAG "FEATURE_MOS_JNI" + +#ifdef __cplusplus +extern "C" { +#endif + +#include "mosaic_renderer_jni.h" + +char buffer[1024]; + +const int MAX_FRAMES = 100; + +static double mTx; + +int tWidth[NR]; +int tHeight[NR]; + +ImageType tImage[NR][MAX_FRAMES];// = {{ImageUtils::IMAGE_TYPE_NOIMAGE}}; // YVU24 format image +Mosaic *mosaic[NR] = {NULL,NULL}; +ImageType resultYVU = ImageUtils::IMAGE_TYPE_NOIMAGE; +ImageType resultBGR = ImageUtils::IMAGE_TYPE_NOIMAGE; +float gTRS[11]; // 9 elements of the transformation, 1 for frame-number, 1 for alignment error code. +// Variables to keep track of the mosaic computation progress for both LR & HR. +float gProgress[NR]; +// Variables to be able to cancel the mosaic computation when the GUI says so. +bool gCancelComputation[NR]; + +int c; +int width=0, height=0; +int mosaicWidth=0, mosaicHeight=0; + +//int blendingType = Blend::BLEND_TYPE_FULL; +//int blendingType = Blend::BLEND_TYPE_CYLPAN; +int blendingType = Blend::BLEND_TYPE_HORZ; +int stripType = Blend::STRIP_TYPE_THIN; +bool high_res = false; +bool quarter_res[NR] = {false,false}; +float thresh_still[NR] = {5.0f,0.0f}; + +/* return current time in milliseconds*/ + +#ifndef now_ms +static double +now_ms(void) +{ + //struct timespec res; + struct timeval res; + //clock_gettime(CLOCK_REALTIME, &res); + gettimeofday(&res, NULL); + return 1000.0*res.tv_sec + (double)res.tv_usec/1e3; +} +#endif + + +static int frame_number_HR = 0; +static int frame_number_LR = 0; + +int Init(int mID, int nmax) +{ + double t0, t1, time_c; + + if(mosaic[mID]!=NULL) + { + delete mosaic[mID]; + mosaic[mID] = NULL; + } + + mosaic[mID] = new Mosaic(); + + t0 = now_ms(); + + // When processing higher than 720x480 video, process low-res at + // quarter resolution + if(tWidth[LR]>180) + quarter_res[LR] = true; + + + // Check for initialization and if not, initialize + if (!mosaic[mID]->isInitialized()) + { + mosaic[mID]->initialize(blendingType, stripType, tWidth[mID], tHeight[mID], + nmax, quarter_res[mID], thresh_still[mID]); + } + + t1 = now_ms(); + time_c = t1 - t0; + LOGV("Init[%d]: %g ms [%d frames]",mID,time_c,nmax); + return 1; +} + +void GenerateQuarterResImagePlanar(ImageType im, int input_w, int input_h, + ImageType &out) +{ + ImageType imp; + ImageType outp; + + int count = 0; + + for (int j = 0; j < input_h; j += H2L_FACTOR) + { + imp = im + j * input_w; + outp = out + (j / H2L_FACTOR) * (input_w / H2L_FACTOR); + + for (int i = 0; i < input_w; i += H2L_FACTOR) + { + *outp++ = *(imp + i); + count++; + } + } + + for (int j = input_h; j < 2 * input_h; j += H2L_FACTOR) + { + imp = im + j * input_w; + outp = out + (j / H2L_FACTOR) * (input_w / H2L_FACTOR); + + for (int i = 0; i < input_w; i += H2L_FACTOR) + { + *outp++ = *(imp + i); + count++; + } + } + + for (int j = 2 * input_h; j < 3 * input_h; j += H2L_FACTOR) + { + imp = im + j * input_w; + outp = out + (j / H2L_FACTOR) * (input_w / H2L_FACTOR); + + for (int i = 0; i < input_w; i += H2L_FACTOR) + { + *outp++ = *(imp + i); + count++; + } + } +} + +int AddFrame(int mID, int k, float* trs1d) +{ + double t0, t1, time_c; + double trs[3][3]; + + int ret_code = mosaic[mID]->addFrame(tImage[mID][k]); + + mosaic[mID]->getAligner()->getLastTRS(trs); + + if(trs1d!=NULL) + { + + trs1d[0] = trs[0][0]; + trs1d[1] = trs[0][1]; + trs1d[2] = trs[0][2]; + trs1d[3] = trs[1][0]; + trs1d[4] = trs[1][1]; + trs1d[5] = trs[1][2]; + trs1d[6] = trs[2][0]; + trs1d[7] = trs[2][1]; + trs1d[8] = trs[2][2]; + } + + return ret_code; +} + +int Finalize(int mID) +{ + double t0, t1, time_c; + + t0 = now_ms(); + // Create the mosaic + int ret = mosaic[mID]->createMosaic(gProgress[mID], gCancelComputation[mID]); + t1 = now_ms(); + time_c = t1 - t0; + LOGV("CreateMosaic: %g ms",time_c); + + // Get back the result + resultYVU = mosaic[mID]->getMosaic(mosaicWidth, mosaicHeight); + + return ret; +} + +void YUV420toYVU24(ImageType yvu24, ImageType yuv420sp, int width, int height) +{ + int frameSize = width * height; + + ImageType oyp = yvu24; + ImageType ovp = yvu24+frameSize; + ImageType oup = yvu24+frameSize+frameSize; + + for (int j = 0, yp = 0; j < height; j++) + { + unsigned char u = 0, v = 0; + int uvp = frameSize + (j >> 1) * width; + for (int i = 0; i < width; i++, yp++) + { + *oyp++ = yuv420sp[yp]; + //int y = (0xff & (int)yuv420sp[yp]) -16; + //yvu24p[yp] = (y<0)?0:y; + + if ((i & 1) == 0) + { + v = yuv420sp[uvp++]; + u = yuv420sp[uvp++]; + } + + *ovp++ = v; + *oup++ = u; + } + } +} + +void YUV420toYVU24_NEW(ImageType yvu24, ImageType yuv420sp, int width, + int height) +{ + int frameSize = width * height; + + ImageType oyp = yvu24; + ImageType ovp = yvu24 + frameSize; + ImageType oup = yvu24 + frameSize + frameSize; + + memcpy(yvu24, yuv420sp, frameSize * sizeof(unsigned char)); + + for (int j = 0; j < height; j += 2) + { + unsigned char u = 0, v = 0; + int uvp = frameSize + (j >> 1) * width; + ovp = yvu24 + frameSize + j * width; + oup = ovp + frameSize; + + ImageType iuvp = yuv420sp + uvp; + + for (int i = 0; i < width; i += 2) + { + v = *iuvp++; + u = *iuvp++; + + *ovp++ = v; + *oup++ = u; + + *ovp++ = v; + *oup++ = u; + + } + memcpy(ovp, ovp - width, width * sizeof(unsigned char)); + memcpy(oup, oup - width, width * sizeof(unsigned char)); + } +} + + +JNIEXPORT void JNICALL Java_com_android_camera_Mosaic_allocateMosaicMemory( + JNIEnv* env, jobject thiz, jint width, jint height) +{ + tWidth[HR] = width; + tHeight[HR] = height; + tWidth[LR] = int(width / H2L_FACTOR); + tHeight[LR] = int(height / H2L_FACTOR); + + for(int i=0; i 262143) r = 262143; + if (g < 0) g = 0; else if (g > 262143) g = 262143; + if (b < 0) b = 0; else if (b > 262143) b = 262143; + + //rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); + int p = j*width*3+i*3; + rgb[p+0] = (r<<6 & 0xFF0000)>>16; + rgb[p+1] = (g>>2 & 0xFF00)>>8; + rgb[p+2] = b>>10 & 0xFF; + } + } +} + +static int count = 0; + +void ConvertYVUAiToPlanarYVU(unsigned char *planar, unsigned char *in, int width, + int height) +{ + int planeSize = width * height; + unsigned char* Yptr = planar; + unsigned char* Vptr = planar + planeSize; + unsigned char* Uptr = Vptr + planeSize; + + for (int i = 0; i < planeSize; i++) + { + *Yptr++ = *in++; + *Vptr++ = *in++; + *Uptr++ = *in++; + in++; // Alpha + } +} + +JNIEXPORT jfloatArray JNICALL Java_com_android_camera_Mosaic_setSourceImageFromGPU( + JNIEnv* env, jobject thiz) +{ + double t0, t1, time_c; + t0 = now_ms(); + int ret_code = Mosaic::MOSAIC_RET_OK; + + if(frame_number_HRNewFloatArray(11); + if(bytes != 0) + { + env->SetFloatArrayRegion(bytes, 0, 11, (jfloat*) gTRS); + } + return bytes; +} + + + +JNIEXPORT jfloatArray JNICALL Java_com_android_camera_Mosaic_setSourceImage( + JNIEnv* env, jobject thiz, jbyteArray photo_data) +{ + double t0, t1, time_c; + t0 = now_ms(); + + int ret_code = Mosaic::MOSAIC_RET_OK; + + if(frame_number_HRGetByteArrayElements(photo_data, 0); + + YUV420toYVU24_NEW(tImage[HR][frame_number_HR], (ImageType)pixels, + tWidth[HR], tHeight[HR]); + + env->ReleaseByteArrayElements(photo_data, pixels, 0); + + double last_tx = mTx; + + t0 = now_ms(); + GenerateQuarterResImagePlanar(tImage[HR][frame_number_HR], tWidth[HR], + tHeight[HR], tImage[LR][frame_number_LR]); + + + sem_wait(&gPreviewImage_semaphore); + decodeYUV444SP(gPreviewImage[LR], tImage[LR][frame_number_LR], + gPreviewImageWidth[LR], gPreviewImageHeight[LR]); + sem_post(&gPreviewImage_semaphore); + + ret_code = AddFrame(LR, frame_number_LR, gTRS); + + if(ret_code == Mosaic::MOSAIC_RET_OK || ret_code == Mosaic::MOSAIC_RET_FEW_INLIERS) + { + frame_number_LR++; + frame_number_HR++; + } + + } + else + { + gTRS[1] = gTRS[2] = gTRS[3] = gTRS[5] = gTRS[6] = gTRS[7] = 0.0f; + gTRS[0] = gTRS[4] = gTRS[8] = 1.0f; + } + + UpdateWarpTransformation(gTRS); + + gTRS[9] = frame_number_HR; + gTRS[10] = ret_code; + + jfloatArray bytes = env->NewFloatArray(11); + if(bytes != 0) + { + env->SetFloatArrayRegion(bytes, 0, 11, (jfloat*) gTRS); + } + return bytes; +} + +JNIEXPORT void JNICALL Java_com_android_camera_Mosaic_setBlendingType( + JNIEnv* env, jobject thiz, jint type) +{ + blendingType = int(type); +} + +JNIEXPORT void JNICALL Java_com_android_camera_Mosaic_setStripType( + JNIEnv* env, jobject thiz, jint type) +{ + stripType = int(type); +} + +JNIEXPORT void JNICALL Java_com_android_camera_Mosaic_reset( + JNIEnv* env, jobject thiz) +{ + frame_number_HR = 0; + frame_number_LR = 0; + + gProgress[LR] = 0.0; + gProgress[HR] = 0.0; + + gCancelComputation[LR] = false; + gCancelComputation[HR] = false; + + Init(LR,MAX_FRAMES); +} + +JNIEXPORT jint JNICALL Java_com_android_camera_Mosaic_reportProgress( + JNIEnv* env, jobject thiz, jboolean hires, jboolean cancel_computation) +{ + if(bool(hires)) + gCancelComputation[HR] = cancel_computation; + else + gCancelComputation[LR] = cancel_computation; + + if(bool(hires)) + return (jint) gProgress[HR]; + else + return (jint) gProgress[LR]; +} + +JNIEXPORT jint JNICALL Java_com_android_camera_Mosaic_createMosaic( + JNIEnv* env, jobject thiz, jboolean value) +{ + high_res = bool(value); + + int ret; + + if(high_res) + { + LOGV("createMosaic() - High-Res Mode"); + double t0, t1, time_c; + + gProgress[HR] = 0.0; + t0 = now_ms(); + + Init(HR, frame_number_HR); + + for(int k = 0; k < frame_number_HR; k++) + { + if (gCancelComputation[HR]) + break; + AddFrame(HR, k, NULL); + gProgress[HR] += TIME_PERCENT_ALIGN/frame_number_HR; + } + + if (gCancelComputation[HR]) + { + ret = Mosaic::MOSAIC_RET_CANCELLED; + } + else + { + gProgress[HR] = TIME_PERCENT_ALIGN; + + t1 = now_ms(); + time_c = t1 - t0; + LOGV("AlignAll - %d frames [HR]: %g ms", frame_number_HR, time_c); + + ret = Finalize(HR); + + gProgress[HR] = 100.0; + } + + high_res = false; + } + else + { + LOGV("createMosaic() - Low-Res Mode"); + gProgress[LR] = TIME_PERCENT_ALIGN; + + ret = Finalize(LR); + + gProgress[LR] = 100.0; + } + + return (jint) ret; +} + +JNIEXPORT jintArray JNICALL Java_com_android_camera_Mosaic_getFinalMosaic( + JNIEnv* env, jobject thiz) +{ + int y,x; + int width = mosaicWidth; + int height = mosaicHeight; + int imageSize = width * height; + + // Convert back to RGB24 + resultBGR = ImageUtils::allocateImage(mosaicWidth, mosaicHeight, + ImageUtils::IMAGE_TYPE_NUM_CHANNELS); + ImageUtils::yvu2bgr(resultBGR, resultYVU, mosaicWidth, mosaicHeight); + + LOGV("MosBytes: %d, W = %d, H = %d", imageSize, width, height); + + int* image = new int[imageSize]; + int* dims = new int[2]; + + for(y=0; yNewIntArray(imageSize+2); + if (bytes == 0) { + LOGE("Error in creating the image."); + delete[] image; + return 0; + } + env->SetIntArrayRegion(bytes, 0, imageSize, (jint*) image); + env->SetIntArrayRegion(bytes, imageSize, 2, (jint*) dims); + delete[] image; + delete[] dims; + return bytes; +} + +JNIEXPORT jbyteArray JNICALL Java_com_android_camera_Mosaic_getFinalMosaicNV21( + JNIEnv* env, jobject thiz) +{ + int y,x; + int width; + int height; + + width = mosaicWidth; + height = mosaicHeight; + + int imageSize = 1.5*width * height; + + // Convert YVU to NV21 format in-place + ImageType V = resultYVU+mosaicWidth*mosaicHeight; + ImageType U = V+mosaicWidth*mosaicHeight; + for(int j=0; j> 24); + dims[1] = (unsigned char)(width >> 16); + dims[2] = (unsigned char)(width >> 8); + dims[3] = (unsigned char)width; + + dims[4] = (unsigned char)(height >> 24); + dims[5] = (unsigned char)(height >> 16); + dims[6] = (unsigned char)(height >> 8); + dims[7] = (unsigned char)height; + + jbyteArray bytes = env->NewByteArray(imageSize+8); + if (bytes == 0) { + LOGE("Error in creating the image."); + ImageUtils::freeImage(resultYVU); + return 0; + } + env->SetByteArrayRegion(bytes, 0, imageSize, (jbyte*) resultYVU); + env->SetByteArrayRegion(bytes, imageSize, 8, (jbyte*) dims); + delete[] dims; + ImageUtils::freeImage(resultYVU); + return bytes; +} + +#ifdef __cplusplus +} +#endif diff --git a/jni_mosaic/feature_stab/db_vlvm/db_bundle.h b/jni_mosaic/feature_stab/db_vlvm/db_bundle.h new file mode 100644 index 0000000000000000000000000000000000000000..e4fb8db2c7746d7ef2056c53a14da661462cf99e --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_bundle.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_bundle.h,v 1.2 2011/06/17 14:03:30 mbansal Exp $ */ + +#ifndef DB_BUNDLE_H +#define DB_BUNDLE_H + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMBundle (LM) Bundle adjustment utilities (a.k.a. Levenberg-Marquardt algorithm) + */ +/*\{*/ + +#include "db_utilities.h" + +/*! +Solve for update dx such that diagmult(1+lambda,transpose(J)%J)%dx= -Jtf +using only upper half of JtJ, destroying lower half below diagonal in the process +dimension is n and d should point to n allocated doubles of scratch memory +*/ +inline void db_Compute_dx(double *dx,double **JtJ,double *min_Jtf,double lambda,double *d,int n) +{ + int i; + double opl; + + opl=1.0+lambda; + for(i=0;i +#endif +#include + +#define DB_SUB_PIXEL + +#define BORDER 10 // 5 + +float** db_AllocStrengthImage_f(float **im,int w,int h) +{ + int i,n,aw; + long c,size; + float **img,*aim,*p; + + /*Determine number of 124 element chunks needed*/ + n=(db_maxi(1,w-6)+123)/124; + /*Determine the total allocation width aw*/ + aw=n*124+8; + /*Allocate*/ + size=aw*h+16; + *im=new float [size]; + /*Clean up*/ + p=(*im); + for(c=0;c>1; + Iy=(img[i-1][j+c]-img[i+1][j+c])>>1; + dxx[c]=Ix*Ix; + dxx[c+128]=Ix*Iy; + dxx[c+256]=Iy*Iy; + } +#endif /*DB_USE_MMX*/ +} + +/*Filter vertically five rows of derivatives of length chunk_width into gxx,gxy,gyy*/ +inline void db_gxx_gxy_gyy_row_f(float *gxx,float *gxy,float *gyy,int chunk_width, + float *Ix0,float *Ix1,float *Ix2,float *Ix3,float *Ix4, + float *Iy0,float *Iy1,float *Iy2,float *Iy3,float *Iy4) +{ + int c; + float dx,dy; + float Ixx0,Ixy0,Iyy0,Ixx1,Ixy1,Iyy1,Ixx2,Ixy2,Iyy2,Ixx3,Ixy3,Iyy3,Ixx4,Ixy4,Iyy4; + + for(c=0;clast) right=last; + /*Compute the Harris strength of a chunk*/ + db_HarrisStrengthChunk_f(s,img,x,3,right,h-4,temp); + } +} + +/*Compute Harris corner strength of img. Strength is returned for the region +with (3,3) as upper left and (w-4,h-4) as lower right, positioned in the +same place in s. In other words,image should be at least 7 pixels wide and 7 pixels high +for a meaningful result.Moreover, the image should be overallocated by 256 bytes. +s[i][3] should by 16 byte aligned for any i*/ +void db_HarrisStrength_u(float **s, const unsigned char * const *img,int w,int h, + /*temp should point to at least + 18*128 of allocated memory*/ + int *temp) +{ + int x,next_x,last; + int nc; + + last=w-4; + for(x=3;x<=last;x=next_x) + { + next_x=x+124; + + // mayban: to revert to the original full chunks state, change the line below to: nc = 128; + nc = db_mini(128,last-x+1); + //nc = 128; + + /*Compute the Harris strength of a chunk*/ + db_HarrisStrengthChunk_u(s,img,x,3,h-4,temp,nc); + } +} + +inline float db_Max_128Aligned16_f(float *v) +{ +#ifdef DB_USE_SIMD + float back; + + _asm + { + mov eax,v + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + movaps xmm2,[eax+32] + movaps xmm3,[eax+48] + movaps xmm4,[eax+64] + movaps xmm5,[eax+80] + movaps xmm6,[eax+96] + movaps xmm7,[eax+112] + + /*Chunk2*/ + maxps xmm0,[eax+128] + maxps xmm1,[eax+144] + maxps xmm2,[eax+160] + maxps xmm3,[eax+176] + maxps xmm4,[eax+192] + maxps xmm5,[eax+208] + maxps xmm6,[eax+224] + maxps xmm7,[eax+240] + + /*Chunk3*/ + maxps xmm0,[eax+256] + maxps xmm1,[eax+272] + maxps xmm2,[eax+288] + maxps xmm3,[eax+304] + maxps xmm4,[eax+320] + maxps xmm5,[eax+336] + maxps xmm6,[eax+352] + maxps xmm7,[eax+368] + + /*Chunk4*/ + maxps xmm0,[eax+384] + maxps xmm1,[eax+400] + maxps xmm2,[eax+416] + maxps xmm3,[eax+432] + maxps xmm4,[eax+448] + maxps xmm5,[eax+464] + maxps xmm6,[eax+480] + maxps xmm7,[eax+496] + + /*Collect*/ + maxps xmm0,xmm1 + maxps xmm2,xmm3 + maxps xmm4,xmm5 + maxps xmm6,xmm7 + maxps xmm0,xmm2 + maxps xmm4,xmm6 + maxps xmm0,xmm4 + movhlps xmm1,xmm0 + maxps xmm0,xmm1 + shufps xmm1,xmm0,1 + maxps xmm0,xmm1 + movss back,xmm0 + } + + return(back); +#else + float val,max_val; + float *p,*stop_p; + max_val=v[0]; + for(p=v+1,stop_p=v+128;p!=stop_p;) + { + val= *p++; + if(val>max_val) max_val=val; + } + return(max_val); +#endif /*DB_USE_SIMD*/ +} + +inline float db_Max_64Aligned16_f(float *v) +{ +#ifdef DB_USE_SIMD + float back; + + _asm + { + mov eax,v + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + movaps xmm2,[eax+32] + movaps xmm3,[eax+48] + movaps xmm4,[eax+64] + movaps xmm5,[eax+80] + movaps xmm6,[eax+96] + movaps xmm7,[eax+112] + + /*Chunk2*/ + maxps xmm0,[eax+128] + maxps xmm1,[eax+144] + maxps xmm2,[eax+160] + maxps xmm3,[eax+176] + maxps xmm4,[eax+192] + maxps xmm5,[eax+208] + maxps xmm6,[eax+224] + maxps xmm7,[eax+240] + + /*Collect*/ + maxps xmm0,xmm1 + maxps xmm2,xmm3 + maxps xmm4,xmm5 + maxps xmm6,xmm7 + maxps xmm0,xmm2 + maxps xmm4,xmm6 + maxps xmm0,xmm4 + movhlps xmm1,xmm0 + maxps xmm0,xmm1 + shufps xmm1,xmm0,1 + maxps xmm0,xmm1 + movss back,xmm0 + } + + return(back); +#else + float val,max_val; + float *p,*stop_p; + max_val=v[0]; + for(p=v+1,stop_p=v+64;p!=stop_p;) + { + val= *p++; + if(val>max_val) max_val=val; + } + return(max_val); +#endif /*DB_USE_SIMD*/ +} + +inline float db_Max_32Aligned16_f(float *v) +{ +#ifdef DB_USE_SIMD + float back; + + _asm + { + mov eax,v + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + movaps xmm2,[eax+32] + movaps xmm3,[eax+48] + movaps xmm4,[eax+64] + movaps xmm5,[eax+80] + movaps xmm6,[eax+96] + movaps xmm7,[eax+112] + + /*Collect*/ + maxps xmm0,xmm1 + maxps xmm2,xmm3 + maxps xmm4,xmm5 + maxps xmm6,xmm7 + maxps xmm0,xmm2 + maxps xmm4,xmm6 + maxps xmm0,xmm4 + movhlps xmm1,xmm0 + maxps xmm0,xmm1 + shufps xmm1,xmm0,1 + maxps xmm0,xmm1 + movss back,xmm0 + } + + return(back); +#else + float val,max_val; + float *p,*stop_p; + max_val=v[0]; + for(p=v+1,stop_p=v+32;p!=stop_p;) + { + val= *p++; + if(val>max_val) max_val=val; + } + return(max_val); +#endif /*DB_USE_SIMD*/ +} + +inline float db_Max_16Aligned16_f(float *v) +{ +#ifdef DB_USE_SIMD + float back; + + _asm + { + mov eax,v + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + movaps xmm2,[eax+32] + movaps xmm3,[eax+48] + + /*Collect*/ + maxps xmm0,xmm1 + maxps xmm2,xmm3 + maxps xmm0,xmm2 + movhlps xmm1,xmm0 + maxps xmm0,xmm1 + shufps xmm1,xmm0,1 + maxps xmm0,xmm1 + movss back,xmm0 + } + + return(back); +#else + float val,max_val; + float *p,*stop_p; + max_val=v[0]; + for(p=v+1,stop_p=v+16;p!=stop_p;) + { + val= *p++; + if(val>max_val) max_val=val; + } + return(max_val); +#endif /*DB_USE_SIMD*/ +} + +inline float db_Max_8Aligned16_f(float *v) +{ +#ifdef DB_USE_SIMD + float back; + + _asm + { + mov eax,v + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + + /*Collect*/ + maxps xmm0,xmm1 + movhlps xmm1,xmm0 + maxps xmm0,xmm1 + shufps xmm1,xmm0,1 + maxps xmm0,xmm1 + movss back,xmm0 + } + + return(back); +#else + float val,max_val; + float *p,*stop_p; + max_val=v[0]; + for(p=v+1,stop_p=v+8;p!=stop_p;) + { + val= *p++; + if(val>max_val) max_val=val; + } + return(max_val); +#endif /*DB_USE_SIMD*/ +} + +inline float db_Max_Aligned16_f(float *v,int size) +{ + float val,max_val; + float *stop_v; + + max_val=v[0]; + for(;size>=128;size-=128) + { + val=db_Max_128Aligned16_f(v); + v+=128; + if(val>max_val) max_val=val; + } + if(size&64) + { + val=db_Max_64Aligned16_f(v); + v+=64; + if(val>max_val) max_val=val; + } + if(size&32) + { + val=db_Max_32Aligned16_f(v); + v+=32; + if(val>max_val) max_val=val; + } + if(size&16) + { + val=db_Max_16Aligned16_f(v); + v+=16; + if(val>max_val) max_val=val; + } + if(size&8) + { + val=db_Max_8Aligned16_f(v); + v+=8; + if(val>max_val) max_val=val; + } + if(size&7) + { + for(stop_v=v+(size&7);v!=stop_v;) + { + val= *v++; + if(val>max_val) max_val=val; + } + } + + return(max_val); +} + +/*Find maximum value of img in the region starting at (left,top) +and with width w and height h. img[left] should be 16 byte aligned*/ +float db_MaxImage_Aligned16_f(float **img,int left,int top,int w,int h) +{ + float val,max_val; + int i,stop_i; + + if(w && h) + { + stop_i=top+h; + max_val=img[top][left]; + + for(i=top;imax_val) max_val=val; + } + return(max_val); + } + return(0.0); +} + +inline void db_MaxVector_128_Aligned16_f(float *m,float *v1,float *v2) +{ +#ifdef DB_USE_SIMD + _asm + { + mov eax,v1 + mov ebx,v2 + mov ecx,m + + /*Chunk1*/ + movaps xmm0,[eax] + movaps xmm1,[eax+16] + movaps xmm2,[eax+32] + movaps xmm3,[eax+48] + movaps xmm4,[eax+64] + movaps xmm5,[eax+80] + movaps xmm6,[eax+96] + movaps xmm7,[eax+112] + maxps xmm0,[ebx] + maxps xmm1,[ebx+16] + maxps xmm2,[ebx+32] + maxps xmm3,[ebx+48] + maxps xmm4,[ebx+64] + maxps xmm5,[ebx+80] + maxps xmm6,[ebx+96] + maxps xmm7,[ebx+112] + movaps [ecx],xmm0 + movaps [ecx+16],xmm1 + movaps [ecx+32],xmm2 + movaps [ecx+48],xmm3 + movaps [ecx+64],xmm4 + movaps [ecx+80],xmm5 + movaps [ecx+96],xmm6 + movaps [ecx+112],xmm7 + + /*Chunk2*/ + movaps xmm0,[eax+128] + movaps xmm1,[eax+144] + movaps xmm2,[eax+160] + movaps xmm3,[eax+176] + movaps xmm4,[eax+192] + movaps xmm5,[eax+208] + movaps xmm6,[eax+224] + movaps xmm7,[eax+240] + maxps xmm0,[ebx+128] + maxps xmm1,[ebx+144] + maxps xmm2,[ebx+160] + maxps xmm3,[ebx+176] + maxps xmm4,[ebx+192] + maxps xmm5,[ebx+208] + maxps xmm6,[ebx+224] + maxps xmm7,[ebx+240] + movaps [ecx+128],xmm0 + movaps [ecx+144],xmm1 + movaps [ecx+160],xmm2 + movaps [ecx+176],xmm3 + movaps [ecx+192],xmm4 + movaps [ecx+208],xmm5 + movaps [ecx+224],xmm6 + movaps [ecx+240],xmm7 + + /*Chunk3*/ + movaps xmm0,[eax+256] + movaps xmm1,[eax+272] + movaps xmm2,[eax+288] + movaps xmm3,[eax+304] + movaps xmm4,[eax+320] + movaps xmm5,[eax+336] + movaps xmm6,[eax+352] + movaps xmm7,[eax+368] + maxps xmm0,[ebx+256] + maxps xmm1,[ebx+272] + maxps xmm2,[ebx+288] + maxps xmm3,[ebx+304] + maxps xmm4,[ebx+320] + maxps xmm5,[ebx+336] + maxps xmm6,[ebx+352] + maxps xmm7,[ebx+368] + movaps [ecx+256],xmm0 + movaps [ecx+272],xmm1 + movaps [ecx+288],xmm2 + movaps [ecx+304],xmm3 + movaps [ecx+320],xmm4 + movaps [ecx+336],xmm5 + movaps [ecx+352],xmm6 + movaps [ecx+368],xmm7 + + /*Chunk4*/ + movaps xmm0,[eax+384] + movaps xmm1,[eax+400] + movaps xmm2,[eax+416] + movaps xmm3,[eax+432] + movaps xmm4,[eax+448] + movaps xmm5,[eax+464] + movaps xmm6,[eax+480] + movaps xmm7,[eax+496] + maxps xmm0,[ebx+384] + maxps xmm1,[ebx+400] + maxps xmm2,[ebx+416] + maxps xmm3,[ebx+432] + maxps xmm4,[ebx+448] + maxps xmm5,[ebx+464] + maxps xmm6,[ebx+480] + maxps xmm7,[ebx+496] + movaps [ecx+384],xmm0 + movaps [ecx+400],xmm1 + movaps [ecx+416],xmm2 + movaps [ecx+432],xmm3 + movaps [ecx+448],xmm4 + movaps [ecx+464],xmm5 + movaps [ecx+480],xmm6 + movaps [ecx+496],xmm7 + } +#else + int i; + float a,b; + for(i=0;i<128;i++) + { + a=v1[i]; + b=v2[i]; + if(a>=b) m[i]=a; + else m[i]=b; + } +#endif /*DB_USE_SIMD*/ +} + +inline void db_MaxVector_128_SecondSourceDestAligned16_f(float *m,float *v1,float *v2) +{ +#ifdef DB_USE_SIMD + _asm + { + mov eax,v1 + mov ebx,v2 + mov ecx,m + + /*Chunk1*/ + movups xmm0,[eax] + movups xmm1,[eax+16] + movups xmm2,[eax+32] + movups xmm3,[eax+48] + movups xmm4,[eax+64] + movups xmm5,[eax+80] + movups xmm6,[eax+96] + movups xmm7,[eax+112] + maxps xmm0,[ebx] + maxps xmm1,[ebx+16] + maxps xmm2,[ebx+32] + maxps xmm3,[ebx+48] + maxps xmm4,[ebx+64] + maxps xmm5,[ebx+80] + maxps xmm6,[ebx+96] + maxps xmm7,[ebx+112] + movaps [ecx],xmm0 + movaps [ecx+16],xmm1 + movaps [ecx+32],xmm2 + movaps [ecx+48],xmm3 + movaps [ecx+64],xmm4 + movaps [ecx+80],xmm5 + movaps [ecx+96],xmm6 + movaps [ecx+112],xmm7 + + /*Chunk2*/ + movups xmm0,[eax+128] + movups xmm1,[eax+144] + movups xmm2,[eax+160] + movups xmm3,[eax+176] + movups xmm4,[eax+192] + movups xmm5,[eax+208] + movups xmm6,[eax+224] + movups xmm7,[eax+240] + maxps xmm0,[ebx+128] + maxps xmm1,[ebx+144] + maxps xmm2,[ebx+160] + maxps xmm3,[ebx+176] + maxps xmm4,[ebx+192] + maxps xmm5,[ebx+208] + maxps xmm6,[ebx+224] + maxps xmm7,[ebx+240] + movaps [ecx+128],xmm0 + movaps [ecx+144],xmm1 + movaps [ecx+160],xmm2 + movaps [ecx+176],xmm3 + movaps [ecx+192],xmm4 + movaps [ecx+208],xmm5 + movaps [ecx+224],xmm6 + movaps [ecx+240],xmm7 + + /*Chunk3*/ + movups xmm0,[eax+256] + movups xmm1,[eax+272] + movups xmm2,[eax+288] + movups xmm3,[eax+304] + movups xmm4,[eax+320] + movups xmm5,[eax+336] + movups xmm6,[eax+352] + movups xmm7,[eax+368] + maxps xmm0,[ebx+256] + maxps xmm1,[ebx+272] + maxps xmm2,[ebx+288] + maxps xmm3,[ebx+304] + maxps xmm4,[ebx+320] + maxps xmm5,[ebx+336] + maxps xmm6,[ebx+352] + maxps xmm7,[ebx+368] + movaps [ecx+256],xmm0 + movaps [ecx+272],xmm1 + movaps [ecx+288],xmm2 + movaps [ecx+304],xmm3 + movaps [ecx+320],xmm4 + movaps [ecx+336],xmm5 + movaps [ecx+352],xmm6 + movaps [ecx+368],xmm7 + + /*Chunk4*/ + movups xmm0,[eax+384] + movups xmm1,[eax+400] + movups xmm2,[eax+416] + movups xmm3,[eax+432] + movups xmm4,[eax+448] + movups xmm5,[eax+464] + movups xmm6,[eax+480] + movups xmm7,[eax+496] + maxps xmm0,[ebx+384] + maxps xmm1,[ebx+400] + maxps xmm2,[ebx+416] + maxps xmm3,[ebx+432] + maxps xmm4,[ebx+448] + maxps xmm5,[ebx+464] + maxps xmm6,[ebx+480] + maxps xmm7,[ebx+496] + movaps [ecx+384],xmm0 + movaps [ecx+400],xmm1 + movaps [ecx+416],xmm2 + movaps [ecx+432],xmm3 + movaps [ecx+448],xmm4 + movaps [ecx+464],xmm5 + movaps [ecx+480],xmm6 + movaps [ecx+496],xmm7 + } +#else + int i; + float a,b; + for(i=0;i<128;i++) + { + a=v1[i]; + b=v2[i]; + if(a>=b) m[i]=a; + else m[i]=b; + } +#endif /*DB_USE_SIMD*/ +} + +/*Compute Max-suppression-filtered image for a chunk of sf starting at (left,top), of width 124 and +stopping at bottom. The output is shifted two steps left and overwrites 128 elements for each row. +The input s should be of width at least 128, and exist for 2 pixels outside the specified region. +s[i][left-2] and sf[i][left-2] should be 16 byte aligned. Top must be at least 3*/ +inline void db_MaxSuppressFilterChunk_5x5_Aligned16_f(float **sf,float **s,int left,int top,int bottom, + /*temp should point to at least + 6*132 floats of 16-byte-aligned allocated memory*/ + float *temp) +{ +#ifdef DB_USE_SIMD + int i,lm2; + float *two[4]; + float *four,*five; + + lm2=left-2; + + /*Set pointers to pre-allocated memory*/ + four=temp; + five=four+132; + for(i=0;i<4;i++) + { + two[i]=five+(i+1)*132; + } + + /*Set rests of four and five to zero to avoid + floating point exceptions*/ + for(i=129;i<132;i++) + { + four[i]=0.0; + five[i]=0.0; + } + + /*Fill three rows of the wrap-around max buffers*/ + for(i=top-3;is[i-2][j-2] && sv>s[i-2][j-1] && sv>s[i-2][j] && sv>s[i-2][j+1] && sv>s[i-2][j+2] && + sv>s[i-1][j-2] && sv>s[i-1][j-1] && sv>s[i-1][j] && sv>s[i-1][j+1] && sv>s[i-1][j+2] && + sv>s[ i][j-2] && sv>s[ i][j-1] && sv>s[ i][j+1] && sv>s[ i][j+2] && + sv>s[i+1][j-2] && sv>s[i+1][j-1] && sv>s[i+1][j] && sv>s[i+1][j+1] && sv>s[i+1][j+2] && + sv>s[i+2][j-2] && sv>s[i+2][j-1] && sv>s[i+2][j] && sv>s[i+2][j+1] && sv>s[i+2][j+2]) + { + sf[i][j-2]=0.0; + } + else sf[i][j-2]=sv; + } +#endif /*DB_USE_SIMD*/ +} + +/*Compute Max-suppression-filtered image for a chunk of sf starting at (left,top) and +stopping at bottom. The output is shifted two steps left. The input s should exist for 2 pixels +outside the specified region. s[i][left-2] and sf[i][left-2] should be 16 byte aligned. +Top must be at least 3. Reading and writing from and to the input and output images is done +as if the region had a width equal to a multiple of 124. If this is not the case, the images +should be over-allocated and the input cleared for a sufficient region*/ +void db_MaxSuppressFilter_5x5_Aligned16_f(float **sf,float **s,int left,int top,int right,int bottom, + /*temp should point to at least + 6*132 floats of 16-byte-aligned allocated memory*/ + float *temp) +{ + int x,next_x; + + for(x=left;x<=right;x=next_x) + { + next_x=x+124; + db_MaxSuppressFilterChunk_5x5_Aligned16_f(sf,s,x,top,bottom,temp); + } +} + +/*Extract corners from the chunk (left,top) to (right,bottom). Store in x_temp,y_temp and s_temp +which should point to space of at least as many positions as there are pixels in the chunk*/ +inline int db_CornersFromChunk(float **strength,int left,int top,int right,int bottom,float threshold,double *x_temp,double *y_temp,double *s_temp) +{ + int i,j,nr; + float s; + + nr=0; + for(i=top;i<=bottom;i++) for(j=left;j<=right;j++) + { + s=strength[i][j]; + + if(s>=threshold && + s>strength[i-2][j-2] && s>strength[i-2][j-1] && s>strength[i-2][j] && s>strength[i-2][j+1] && s>strength[i-2][j+2] && + s>strength[i-1][j-2] && s>strength[i-1][j-1] && s>strength[i-1][j] && s>strength[i-1][j+1] && s>strength[i-1][j+2] && + s>strength[ i][j-2] && s>strength[ i][j-1] && s>strength[ i][j+1] && s>strength[ i][j+2] && + s>strength[i+1][j-2] && s>strength[i+1][j-1] && s>strength[i+1][j] && s>strength[i+1][j+1] && s>strength[i+1][j+2] && + s>strength[i+2][j-2] && s>strength[i+2][j-1] && s>strength[i+2][j] && s>strength[i+2][j+1] && s>strength[i+2][j+2]) + { + x_temp[nr]=(double) j; + y_temp[nr]=(double) i; + s_temp[nr]=(double) s; + nr++; + } + } + return(nr); +} + + +//Sub-pixel accuracy using 2D quadratic interpolation.(YCJ) +inline void db_SubPixel(float **strength, const double xd, const double yd, double &xs, double &ys) +{ + int x = (int) xd; + int y = (int) yd; + + float fxx = strength[y][x-1] - strength[y][x] - strength[y][x] + strength[y][x+1]; + float fyy = strength[y-1][x] - strength[y][x] - strength[y][x] + strength[y+1][x]; + float fxy = (strength[y-1][x-1] - strength[y-1][x+1] - strength[y+1][x-1] + strength[y+1][x+1])/(float)4.0; + + float denom = (fxx * fyy - fxy * fxy) * (float) 2.0; + + xs = xd; + ys = yd; + + if ( db_absf(denom) <= FLT_EPSILON ) + { + return; + } + else + { + float fx = strength[y][x+1] - strength[y][x-1]; + float fy = strength[y+1][x] - strength[y-1][x]; + + float dx = (fyy * fx - fxy * fy) / denom; + float dy = (fxx * fy - fxy * fx) / denom; + + if ( db_absf(dx) > 1.0 || db_absf(dy) > 1.0 ) + { + return; + } + else + { + xs -= dx; + ys -= dy; + } + } + + return; +} + +/*Extract corners from the image part from (left,top) to (right,bottom). +Store in x and y, extracting at most satnr corners in each block of size (bw,bh). +The pointer temp_d should point to at least 5*bw*bh positions. +area_factor holds how many corners max to extract per 10000 pixels*/ +void db_ExtractCornersSaturated(float **strength,int left,int top,int right,int bottom, + int bw,int bh,unsigned long area_factor, + float threshold,double *temp_d, + double *x_coord,double *y_coord,int *nr_corners) +{ + double *x_temp,*y_temp,*s_temp,*select_temp; + double loc_thresh; + unsigned long bwbh,area,saturation; + int x,next_x,last_x; + int y,next_y,last_y; + int nr,nr_points,i,stop; + + bwbh=bw*bh; + x_temp=temp_d; + y_temp=x_temp+bwbh; + s_temp=y_temp+bwbh; + select_temp=s_temp+bwbh; + +#ifdef DB_SUB_PIXEL + // subpixel processing may sometimes push the corner ourside the real border + // increasing border size: + left++; + top++; + bottom--; + right--; +#endif /*DB_SUB_PIXEL*/ + + nr_points=0; + for(y=top;y<=bottom;y=next_y) + { + next_y=y+bh; + last_y=next_y-1; + if(last_y>bottom) last_y=bottom; + for(x=left;x<=right;x=next_x) + { + next_x=x+bw; + last_x=next_x-1; + if(last_x>right) last_x=right; + + area=(last_x-x+1)*(last_y-y+1); + saturation=(area*area_factor)/10000; + nr=db_CornersFromChunk(strength,x,y,last_x,last_y,threshold,x_temp,y_temp,s_temp); + if(nr) + { + if(((unsigned long)nr)>saturation) loc_thresh=db_LeanQuickSelect(s_temp,nr,nr-saturation,select_temp); + else loc_thresh=threshold; + + stop=nr_points+saturation; + for(i=0;(i=loc_thresh) + { + #ifdef DB_SUB_PIXEL + db_SubPixel(strength, x_temp[i], y_temp[i], x_coord[nr_points], y_coord[nr_points]); + #else + x_coord[nr_points]=x_temp[i]; + y_coord[nr_points]=y_temp[i]; + #endif + + nr_points++; + } + } + } + } + } + *nr_corners=nr_points; +} + +db_CornerDetector_f::db_CornerDetector_f() +{ + m_w=0; m_h=0; +} + +db_CornerDetector_f::~db_CornerDetector_f() +{ + Clean(); +} + +void db_CornerDetector_f::Clean() +{ + if(m_w!=0) + { + delete [] m_temp_f; + delete [] m_temp_d; + db_FreeStrengthImage_f(m_strength_mem,m_strength,m_h); + } + m_w=0; m_h=0; +} + +unsigned long db_CornerDetector_f::Init(int im_width,int im_height,int target_nr_corners, + int nr_horizontal_blocks,int nr_vertical_blocks, + double absolute_threshold,double relative_threshold) +{ + int chunkwidth=208; + int block_width,block_height; + unsigned long area_factor; + int active_width,active_height; + + active_width=db_maxi(1,im_width-10); + active_height=db_maxi(1,im_height-10); + block_width=db_maxi(1,active_width/nr_horizontal_blocks); + block_height=db_maxi(1,active_height/nr_vertical_blocks); + + area_factor=db_minl(1000,db_maxl(1,(long)(10000.0*((double)target_nr_corners)/ + (((double)active_width)*((double)active_height))))); + + return(Start(im_width,im_height,block_width,block_height,area_factor, + absolute_threshold,relative_threshold,chunkwidth)); +} + +unsigned long db_CornerDetector_f::Start(int im_width,int im_height, + int block_width,int block_height,unsigned long area_factor, + double absolute_threshold,double relative_threshold,int chunkwidth) +{ + Clean(); + + m_w=im_width; + m_h=im_height; + m_cw=chunkwidth; + m_bw=block_width; + m_bh=block_height; + m_area_factor=area_factor; + m_r_thresh=relative_threshold; + m_a_thresh=absolute_threshold; + m_max_nr=db_maxl(1,1+(m_w*m_h*m_area_factor)/10000); + + m_temp_f=new float[13*(m_cw+4)]; + m_temp_d=new double[5*m_bw*m_bh]; + m_strength=db_AllocStrengthImage_f(&m_strength_mem,m_w,m_h); + + return(m_max_nr); +} + +void db_CornerDetector_f::DetectCorners(const float * const *img,double *x_coord,double *y_coord,int *nr_corners) const +{ + float max_val,threshold; + + db_HarrisStrength_f(m_strength,img,m_w,m_h,m_temp_f,m_cw); + + if(m_r_thresh) + { + max_val=db_MaxImage_Aligned16_f(m_strength,3,3,m_w-6,m_h-6); + threshold= (float) db_maxd(m_a_thresh,max_val*m_r_thresh); + } + else threshold= (float) m_a_thresh; + + db_ExtractCornersSaturated(m_strength,BORDER,BORDER,m_w-BORDER-1,m_h-BORDER-1,m_bw,m_bh,m_area_factor,threshold, + m_temp_d,x_coord,y_coord,nr_corners); +} + +db_CornerDetector_u::db_CornerDetector_u() +{ + m_w=0; m_h=0; +} + +db_CornerDetector_u::~db_CornerDetector_u() +{ + Clean(); +} + +db_CornerDetector_u::db_CornerDetector_u(const db_CornerDetector_u& cd) +{ + Start(cd.m_w, cd.m_h, cd.m_bw, cd.m_bh, cd.m_area_factor, + cd.m_a_thresh, cd.m_r_thresh); +} + +db_CornerDetector_u& db_CornerDetector_u::operator=(const db_CornerDetector_u& cd) +{ + if ( this == &cd ) return *this; + + Clean(); + + Start(cd.m_w, cd.m_h, cd.m_bw, cd.m_bh, cd.m_area_factor, + cd.m_a_thresh, cd.m_r_thresh); + + return *this; +} + +void db_CornerDetector_u::Clean() +{ + if(m_w!=0) + { + delete [] m_temp_i; + delete [] m_temp_d; + db_FreeStrengthImage_f(m_strength_mem,m_strength,m_h); + } + m_w=0; m_h=0; +} + +unsigned long db_CornerDetector_u::Init(int im_width,int im_height,int target_nr_corners, + int nr_horizontal_blocks,int nr_vertical_blocks, + double absolute_threshold,double relative_threshold) +{ + int block_width,block_height; + unsigned long area_factor; + int active_width,active_height; + + active_width=db_maxi(1,im_width-10); + active_height=db_maxi(1,im_height-10); + block_width=db_maxi(1,active_width/nr_horizontal_blocks); + block_height=db_maxi(1,active_height/nr_vertical_blocks); + + area_factor=db_minl(1000,db_maxl(1,(long)(10000.0*((double)target_nr_corners)/ + (((double)active_width)*((double)active_height))))); + + return(Start(im_width,im_height,block_width,block_height,area_factor, + 16.0*absolute_threshold,relative_threshold)); +} + +unsigned long db_CornerDetector_u::Start(int im_width,int im_height, + int block_width,int block_height,unsigned long area_factor, + double absolute_threshold,double relative_threshold) +{ + Clean(); + + m_w=im_width; + m_h=im_height; + m_bw=block_width; + m_bh=block_height; + m_area_factor=area_factor; + m_r_thresh=relative_threshold; + m_a_thresh=absolute_threshold; + m_max_nr=db_maxl(1,1+(m_w*m_h*m_area_factor)/10000); + + m_temp_i=new int[18*128]; + m_temp_d=new double[5*m_bw*m_bh]; + m_strength=db_AllocStrengthImage_f(&m_strength_mem,m_w,m_h); + + return(m_max_nr); +} + +void db_CornerDetector_u::DetectCorners(const unsigned char * const *img,double *x_coord,double *y_coord,int *nr_corners, + const unsigned char * const *msk, unsigned char fgnd) const +{ + float max_val,threshold; + + db_HarrisStrength_u(m_strength,img,m_w,m_h,m_temp_i); + + + if(m_r_thresh) + { + max_val=db_MaxImage_Aligned16_f(m_strength,3,3,m_w-6,m_h-6); + threshold= (float) db_maxd(m_a_thresh,max_val*m_r_thresh); + } + else threshold= (float) m_a_thresh; + + db_ExtractCornersSaturated(m_strength,BORDER,BORDER,m_w-BORDER-1,m_h-BORDER-1,m_bw,m_bh,m_area_factor,threshold, + m_temp_d,x_coord,y_coord,nr_corners); + + + if ( msk ) + { + int nr_corners_mask=0; + + for ( int i = 0; i < *nr_corners; ++i) + { + int cor_x = db_roundi(*(x_coord+i)); + int cor_y = db_roundi(*(y_coord+i)); + if ( msk[cor_y][cor_x] == fgnd ) + { + x_coord[nr_corners_mask] = x_coord[i]; + y_coord[nr_corners_mask] = y_coord[i]; + nr_corners_mask++; + } + } + *nr_corners = nr_corners_mask; + } +} + +void db_CornerDetector_u::ExtractCorners(float ** strength, double *x_coord, double *y_coord, int *nr_corners) { + if ( m_w!=0 ) + db_ExtractCornersSaturated(strength,BORDER,BORDER,m_w-BORDER-1,m_h-BORDER-1,m_bw,m_bh,m_area_factor,float(m_a_thresh), + m_temp_d,x_coord,y_coord,nr_corners); +} + diff --git a/jni_mosaic/feature_stab/db_vlvm/db_feature_detection.h b/jni_mosaic/feature_stab/db_vlvm/db_feature_detection.h new file mode 100644 index 0000000000000000000000000000000000000000..68ffcc9adaffbd01aa178161cfb497030bdbaa93 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_feature_detection.h @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*$Id: db_feature_detection.h,v 1.3 2011/06/17 14:03:30 mbansal Exp $*/ + +#ifndef DB_FEATURE_DETECTION_H +#define DB_FEATURE_DETECTION_H + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup FeatureDetection Feature Detection + */ +#include "db_utilities.h" +#include "db_utilities_constants.h" +#include //for NULL + +/*! + * \class db_CornerDetector_f + * \ingroup FeatureDetection + * \brief Harris corner detector for float images. + * + * This class performs Harris corner extraction on *float* images managed + * with functions in \ref LMImageBasicUtilities. + */ +class DB_API db_CornerDetector_f +{ +public: + db_CornerDetector_f(); + ~db_CornerDetector_f(); + + /*! + * Set parameters and pre-allocate memory. Return an upper bound + * on the number of corners detected in one frame. + * \param im_width width + * \param im_height height + * \param target_nr_corners + * \param nr_horizontal_blocks + * \param nr_vertical_blocks + * \param absolute_threshold + * \param relative_threshold + */ + unsigned long Init(int im_width,int im_height, + int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS, + int nr_horizontal_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS, + int nr_vertical_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS, + double absolute_threshold=DB_DEFAULT_ABS_CORNER_THRESHOLD, + double relative_threshold=DB_DEFAULT_REL_CORNER_THRESHOLD); + + /*! + * Detect the corners. + * x_coord and y_coord should be pre-allocated arrays of length returned by Init(). + * \param img row array pointer + * \param x_coord corner locations + * \param y_coord corner locations + * \param nr_corners actual number of corners computed + */ + void DetectCorners(const float * const *img,double *x_coord,double *y_coord,int *nr_corners) const; + void SetAbsoluteThreshold(double a_thresh) { m_a_thresh = a_thresh; }; + void SetRelativeThreshold(double r_thresh) { m_r_thresh = r_thresh; }; +protected: + void Clean(); + unsigned long Start(int im_width,int im_height, + int block_width,int block_height,unsigned long area_factor, + double absolute_threshold,double relative_threshold,int chunkwidth); + + int m_w,m_h,m_cw,m_bw,m_bh; + /*Area factor holds the maximum number of corners to detect + per 10000 pixels*/ + unsigned long m_area_factor,m_max_nr; + double m_a_thresh,m_r_thresh; + float *m_temp_f; + double *m_temp_d; + float **m_strength,*m_strength_mem; +}; +/*! + * \class db_CornerDetector_u + * \ingroup FeatureDetection + * \brief Harris corner detector for byte images. + * + * This class performs Harris corner extraction on *byte* images managed + * with functions in \ref LMImageBasicUtilities. + */ +class DB_API db_CornerDetector_u +{ +public: + db_CornerDetector_u(); + virtual ~db_CornerDetector_u(); + + /*! + Copy ctor duplicates settings. + Memory is not copied. + */ + db_CornerDetector_u(const db_CornerDetector_u& cd); + /*! + Assignment optor duplicates settings. + Memory not copied. + */ + db_CornerDetector_u& operator=(const db_CornerDetector_u& cd); + + /*! + * Set parameters and pre-allocate memory. Return an upper bound + * on the number of corners detected in one frame + */ + virtual unsigned long Init(int im_width,int im_height, + int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS, + int nr_horizontal_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS, + int nr_vertical_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS, + double absolute_threshold=DB_DEFAULT_ABS_CORNER_THRESHOLD, + double relative_threshold=DB_DEFAULT_REL_CORNER_THRESHOLD); + + /*! + * Detect the corners. + * Observe that the image should be overallocated by at least 256 bytes + * at the end. + * x_coord and y_coord should be pre-allocated arrays of length returned by Init(). + * Specifying image mask will restrict corner output to foreground regions. + * Foreground value can be specified using fgnd. By default any >0 mask value + * is considered to be foreground + * \param img row array pointer + * \param x_coord corner locations + * \param y_coord corner locations + * \param nr_corners actual number of corners computed + * \param msk row array pointer to mask image + * \param fgnd foreground value in the mask + */ + virtual void DetectCorners(const unsigned char * const *img,double *x_coord,double *y_coord,int *nr_corners, + const unsigned char * const * msk=NULL, unsigned char fgnd=255) const; + + /*! + Set absolute feature threshold + */ + virtual void SetAbsoluteThreshold(double a_thresh) { m_a_thresh = a_thresh; }; + /*! + Set relative feature threshold + */ + virtual void SetRelativeThreshold(double r_thresh) { m_r_thresh = r_thresh; }; + + /*! + Extract corners from a pre-computed strength image. + \param strength Harris strength image + \param x_coord corner locations + \param y_coord corner locations + \param nr_corners actual number of corners computed + */ + virtual void ExtractCorners(float ** strength, double *x_coord, double *y_coord, int *nr_corners); +protected: + virtual void Clean(); + /*The absolute threshold to this function should be 16.0 times + normal*/ + unsigned long Start(int im_width,int im_height, + int block_width,int block_height,unsigned long area_factor, + double absolute_threshold,double relative_threshold); + + int m_w,m_h,m_bw,m_bh; + /*Area factor holds the maximum number of corners to detect + per 10000 pixels*/ + unsigned long m_area_factor,m_max_nr; + double m_a_thresh,m_r_thresh; + int *m_temp_i; + double *m_temp_d; + float **m_strength,*m_strength_mem; +}; + +#endif /*DB_FEATURE_DETECTION_H*/ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.cpp b/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d278d0cf64f493b5f7860940179baad39c944344 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.cpp @@ -0,0 +1,3410 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*$Id: db_feature_matching.cpp,v 1.4 2011/06/17 14:03:30 mbansal Exp $*/ + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +#include "db_utilities.h" +#include "db_feature_matching.h" +#ifdef _VERBOSE_ +#include +#endif + + +int AffineWarpPoint_NN_LUT_x[11][11]; +int AffineWarpPoint_NN_LUT_y[11][11]; + +float AffineWarpPoint_BL_LUT_x[11][11]; +float AffineWarpPoint_BL_LUT_y[11][11]; + + +inline float db_SignedSquareNormCorr7x7_u(unsigned char **f_img,unsigned char **g_img,int x_f,int y_f,int x_g,int y_g) +{ + unsigned char *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-3; + xm_g=x_g-3; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=49.0f*fgsum-fsum*gsum; + den=(49.0f*f2sum-fsum*fsum)*(49.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline float db_SignedSquareNormCorr9x9_u(unsigned char **f_img,unsigned char **g_img,int x_f,int y_f,int x_g,int y_g) +{ + unsigned char *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-4; + xm_g=x_g-4; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=81.0f*fgsum-fsum*gsum; + den=(81.0f*f2sum-fsum*fsum)*(81.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline float db_SignedSquareNormCorr11x11_u(unsigned char **f_img,unsigned char **g_img,int x_f,int y_f,int x_g,int y_g) +{ + unsigned char *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-5; + xm_g=x_g-5; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=121.0f*fgsum-fsum*gsum; + den=(121.0f*f2sum-fsum*fsum)*(121.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline void db_SignedSquareNormCorr11x11_Pre_u(unsigned char **f_img,int x_f,int y_f,float *sum,float *recip) +{ + unsigned char *pf; + float den; + int f,f2sum,fsum; + int xm_f; + + xm_f=x_f-5; + + pf=f_img[y_f-5]+xm_f; + f= *pf++; f2sum=f*f; fsum=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+5]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + *sum= (float) fsum; + den=(121.0f*f2sum-fsum*fsum); + *recip=(float)(((den!=0.0)?1.0/den:0.0)); +} + +inline void db_SignedSquareNormCorr5x5_PreAlign_u(short *patch,const unsigned char * const *f_img,int x_f,int y_f,float *sum,float *recip) +{ + float den; + int f2sum,fsum; + int xm_f=x_f-2; + +#ifndef DB_USE_SSE2 + const unsigned char *pf; + short f; + + pf=f_img[y_f-2]+xm_f; + f= *pf++; f2sum=f*f; fsum=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + //int xwi; + //int ywi; + //f2sum=0; + //fsum=0; + //for (int r=-5;r<=5;r++){ + // ywi=y_f+r; + // for (int c=-5;c<=5;c++){ + // xwi=x_f+c; + // f=f_img[ywi][xwi]; + // f2sum+=f*f; + // fsum+=f; + // (*patch++)=f; + // } + //} + (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; + (*patch++)=0; (*patch++)=0; +#endif /* DB_USE_SSE2 */ + + *sum= (float) fsum; + den=(25.0f*f2sum-fsum*fsum); + *recip= (float)((den!=0.0)?1.0/den:0.0); +} + +inline void db_SignedSquareNormCorr21x21_PreAlign_u(short *patch,const unsigned char * const *f_img,int x_f,int y_f,float *sum,float *recip) +{ + float den; + int f2sum,fsum; + int xm_f=x_f-10; + short f; + + int xwi; + int ywi; + f2sum=0; + fsum=0; + for (int r=-10;r<=10;r++){ + ywi=y_f+r; + for (int c=-10;c<=10;c++){ + xwi=x_f+c; + f=f_img[ywi][xwi]; + f2sum+=f*f; + fsum+=f; + (*patch++)=f; + } + } + + for(int i=442; i<512; i++) + (*patch++)=0; + + *sum= (float) fsum; + den=(441.0f*f2sum-fsum*fsum); + *recip= (float)((den!=0.0)?1.0/den:0.0); + + +} + +/* Lay out the image in the patch, computing norm and +*/ +inline void db_SignedSquareNormCorr11x11_PreAlign_u(short *patch,const unsigned char * const *f_img,int x_f,int y_f,float *sum,float *recip) +{ + float den; + int f2sum,fsum; + int xm_f=x_f-5; + +#ifndef DB_USE_SSE2 + const unsigned char *pf; + short f; + + pf=f_img[y_f-5]+xm_f; + f= *pf++; f2sum=f*f; fsum=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+5]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + //int xwi; + //int ywi; + //f2sum=0; + //fsum=0; + //for (int r=-5;r<=5;r++){ + // ywi=y_f+r; + // for (int c=-5;c<=5;c++){ + // xwi=x_f+c; + // f=f_img[ywi][xwi]; + // f2sum+=f*f; + // fsum+=f; + // (*patch++)=f; + // } + //} + + (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; + (*patch++)=0; (*patch++)=0; +#else + const unsigned char *pf0 =f_img[y_f-5]+xm_f; + const unsigned char *pf1 =f_img[y_f-4]+xm_f; + const unsigned char *pf2 =f_img[y_f-3]+xm_f; + const unsigned char *pf3 =f_img[y_f-2]+xm_f; + const unsigned char *pf4 =f_img[y_f-1]+xm_f; + const unsigned char *pf5 =f_img[y_f ]+xm_f; + const unsigned char *pf6 =f_img[y_f+1]+xm_f; + const unsigned char *pf7 =f_img[y_f+2]+xm_f; + const unsigned char *pf8 =f_img[y_f+3]+xm_f; + const unsigned char *pf9 =f_img[y_f+4]+xm_f; + const unsigned char *pf10=f_img[y_f+5]+xm_f; + + /* pixel mask */ + const unsigned char pm[16] = { + 0xFF,0xFF, + 0xFF,0xFF, + 0xFF,0xFF, + 0,0,0,0,0, + 0,0,0,0,0}; + const unsigned char * pm_p = pm; + + _asm + { + mov ecx,patch /* load patch pointer */ + mov ebx, pm_p /* load pixel mask pointer */ + movdqu xmm1,[ebx] /* load pixel mask */ + + pxor xmm5,xmm5 /* set xmm5 to 0 accumulator for sum squares */ + pxor xmm4,xmm4 /* set xmm4 to 0 accumulator for sum */ + pxor xmm0,xmm0 /* set xmm0 to 0 */ + + /* row 0 */ + mov eax,pf0 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqa [ecx+0*22],xmm7 /* move short values to patch */ + movdqa [ecx+0*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 1 */ + mov eax,pf1 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+1*22],xmm7 /* move short values to patch */ + movdqu [ecx+1*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 2 */ + mov eax,pf2 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+2*22],xmm7 /* move short values to patch */ + movdqu [ecx+2*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 3 */ + mov eax,pf3 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+3*22],xmm7 /* move short values to patch */ + movdqu [ecx+3*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 4 */ + mov eax,pf4 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+4*22],xmm7 /* move short values to patch */ + movdqu [ecx+4*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 5 */ + mov eax,pf5 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+5*22],xmm7 /* move short values to patch */ + movdqu [ecx+5*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 6 */ + mov eax,pf6 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+6*22],xmm7 /* move short values to patch */ + movdqu [ecx+6*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 7 */ + mov eax,pf7 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+7*22],xmm7 /* move short values to patch */ + movdqu [ecx+7*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 8 */ + mov eax,pf8 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqa [ecx+8*22],xmm7 /* move short values to patch */ + movdqa [ecx+8*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 9 */ + mov eax,pf9 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+9*22],xmm7 /* move short values to patch */ + movdqu [ecx+9*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit uints into 16 bit uints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* row 10 */ + mov eax,pf10 /* load image pointer */ + movdqu xmm7,[eax] /* load 16 pixels */ + movdqa xmm6,xmm7 + + punpcklbw xmm7,xmm0 /* unpack low pixels (first 8)*/ + punpckhbw xmm6,xmm0 /* unpack high pixels (last 8)*/ + + pand xmm6,xmm1 /* mask out pixels 12-16 */ + + movdqu [ecx+10*22],xmm7 /* move short values to patch */ + movdqu [ecx+10*22+16],xmm6 /* move short values to patch */ + + paddusw xmm4,xmm7 /* accumulate sums */ + pmaddwd xmm7,xmm7 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm7 /* accumulate sum squares */ + + paddw xmm4,xmm6 /* accumulate sums */ + pmaddwd xmm6,xmm6 /* multiply 16 bit ints and add into 32 bit ints */ + paddd xmm5,xmm6 /* accumulate sum squares */ + + /* add up the sum squares */ + movhlps xmm0,xmm5 /* high half to low half */ + paddd xmm5,xmm0 /* add high to low */ + pshuflw xmm0,xmm5, 0xE /* reshuffle */ + paddd xmm5,xmm0 /* add remaining */ + movd f2sum,xmm5 + + /* add up the sum */ + movhlps xmm0,xmm4 + paddw xmm4,xmm0 /* halves added */ + pshuflw xmm0,xmm4,0xE + paddw xmm4,xmm0 /* quarters added */ + pshuflw xmm0,xmm4,0x1 + paddw xmm4,xmm0 /* eighth added */ + movd fsum, xmm4 + + emms + } + + fsum = fsum & 0xFFFF; + + patch[126] = 0; + patch[127] = 0; +#endif /* DB_USE_SSE2 */ + + *sum= (float) fsum; + den=(121.0f*f2sum-fsum*fsum); + *recip= (float)((den!=0.0)?1.0/den:0.0); +} + +void AffineWarpPointOffset(float &r_w,float &c_w,double Hinv[9],int r,int c) +{ + r_w=(float)(Hinv[3]*c+Hinv[4]*r); + c_w=(float)(Hinv[0]*c+Hinv[1]*r); +} + + + +/*! +Prewarp the patches with given affine transform. For a given homogeneous point "x", "H*x" is +the warped point and for any displacement "d" in the warped image resulting in point "y", the +corresponding point in the original image is given by "Hinv*y", which can be simplified for affine H. +If "affine" is 1, then nearest neighbor method is used, else if it is 2, then +bilinear method is used. + */ +inline void db_SignedSquareNormCorr11x11_PreAlign_AffinePatchWarp_u(short *patch,const unsigned char * const *f_img, + int xi,int yi,float *sum,float *recip, + const double Hinv[9],int affine) +{ + float den; + short f; + int f2sum,fsum; + + f2sum=0; + fsum=0; + + if (affine==1) + { + for (int r=0;r<11;r++){ + for (int c=0;c<11;c++){ + f=f_img[yi+AffineWarpPoint_NN_LUT_y[r][c]][xi+AffineWarpPoint_NN_LUT_x[r][c]]; + f2sum+=f*f; + fsum+=f; + (*patch++)=f; + } + } + } + else if (affine==2) + { + for (int r=0;r<11;r++){ + for (int c=0;c<11;c++){ + f=db_BilinearInterpolation(yi+AffineWarpPoint_BL_LUT_y[r][c] + ,xi+AffineWarpPoint_BL_LUT_x[r][c],f_img); + f2sum+=f*f; + fsum+=f; + (*patch++)=f; + } + } + } + + + + (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; (*patch++)=0; + (*patch++)=0; (*patch++)=0; + + *sum= (float) fsum; + den=(121.0f*f2sum-fsum*fsum); + *recip= (float)((den!=0.0)?1.0/den:0.0); +} + + +inline float db_SignedSquareNormCorr11x11_Post_u(unsigned char **f_img,unsigned char **g_img,int x_f,int y_f,int x_g,int y_g, + float fsum_gsum,float f_recip_g_recip) +{ + unsigned char *pf,*pg; + int fgsum; + float fg_corr; + int xm_f,xm_g; + + xm_f=x_f-5; + xm_g=x_g-5; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + fgsum=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + fg_corr=121.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + +float db_SignedSquareNormCorr21x21Aligned_Post_s(const short *f_patch,const short *g_patch,float fsum_gsum,float f_recip_g_recip) +{ + float fgsum,fg_corr; + + fgsum= (float) db_ScalarProduct512_s(f_patch,g_patch); + + fg_corr=441.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + + +float db_SignedSquareNormCorr11x11Aligned_Post_s(const short *f_patch,const short *g_patch,float fsum_gsum,float f_recip_g_recip) +{ + float fgsum,fg_corr; + + fgsum= (float) db_ScalarProduct128_s(f_patch,g_patch); + + fg_corr=121.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + +float db_SignedSquareNormCorr5x5Aligned_Post_s(const short *f_patch,const short *g_patch,float fsum_gsum,float f_recip_g_recip) +{ + float fgsum,fg_corr; + + fgsum= (float) db_ScalarProduct32_s(f_patch,g_patch); + + fg_corr=25.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + + +inline float db_SignedSquareNormCorr15x15_u(unsigned char **f_img,unsigned char **g_img,int x_f,int y_f,int x_g,int y_g) +{ + unsigned char *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-7; + xm_g=x_g-7; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-7]+xm_f; pg=g_img[y_g-7]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-6]+xm_f; pg=g_img[y_g-6]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+6]+xm_f; pg=g_img[y_g+6]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+7]+xm_f; pg=g_img[y_g+7]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=225.0f*fgsum-fsum*gsum; + den=(225.0f*f2sum-fsum*fsum)*(225.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline float db_SignedSquareNormCorr7x7_f(float **f_img,float **g_img,int x_f,int y_f,int x_g,int y_g) +{ + float f,g,*pf,*pg,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-3; + xm_g=x_g-3; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=49.0f*fgsum-fsum*gsum; + den=(49.0f*f2sum-fsum*fsum)*(49.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline float db_SignedSquareNormCorr9x9_f(float **f_img,float **g_img,int x_f,int y_f,int x_g,int y_g) +{ + float f,g,*pf,*pg,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-4; + xm_g=x_g-4; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=81.0f*fgsum-fsum*gsum; + den=(81.0f*f2sum-fsum*fsum)*(81.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline float db_SignedSquareNormCorr11x11_f(float **f_img,float **g_img,int x_f,int y_f,int x_g,int y_g) +{ + float *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-5; + xm_g=x_g-5; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=121.0f*fgsum-fsum*gsum; + den=(121.0f*f2sum-fsum*fsum)*(121.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +inline void db_SignedSquareNormCorr11x11_Pre_f(float **f_img,int x_f,int y_f,float *sum,float *recip) +{ + float *pf,den; + float f,f2sum,fsum; + int xm_f; + + xm_f=x_f-5; + + pf=f_img[y_f-5]+xm_f; + f= *pf++; f2sum=f*f; fsum=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f-1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + pf=f_img[y_f+5]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf++; f2sum+=f*f; fsum+=f; + f= *pf; f2sum+=f*f; fsum+=f; + + *sum=fsum; + den=(121.0f*f2sum-fsum*fsum); + *recip= (float) ((den!=0.0)?1.0/den:0.0); +} + +inline void db_SignedSquareNormCorr11x11_PreAlign_f(float *patch,const float * const *f_img,int x_f,int y_f,float *sum,float *recip) +{ + const float *pf; + float den,f,f2sum,fsum; + int xm_f; + + xm_f=x_f-5; + + pf=f_img[y_f-5]+xm_f; + f= *pf++; f2sum=f*f; fsum=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f-1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+1]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+2]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+3]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+4]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + pf=f_img[y_f+5]+xm_f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf++; f2sum+=f*f; fsum+=f; (*patch++)=f; + f= *pf; f2sum+=f*f; fsum+=f; (*patch++)=f; + + (*patch++)=0.0; (*patch++)=0.0; (*patch++)=0.0; (*patch++)=0.0; (*patch++)=0.0; + (*patch++)=0.0; (*patch++)=0.0; + + *sum=fsum; + den=(121.0f*f2sum-fsum*fsum); + *recip= (float) ((den!=0.0)?1.0/den:0.0); +} + +inline float db_SignedSquareNormCorr11x11_Post_f(float **f_img,float **g_img,int x_f,int y_f,int x_g,int y_g, + float fsum_gsum,float f_recip_g_recip) +{ + float *pf,*pg; + float fgsum,fg_corr; + int xm_f,xm_g; + + xm_f=x_f-5; + xm_g=x_g-5; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + fgsum=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); fgsum+=(*pf++)*(*pg++); + fgsum+=(*pf++)*(*pg++); fgsum+=(*pf)*(*pg); + + fg_corr=121.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + +inline float db_SignedSquareNormCorr11x11Aligned_Post_f(const float *f_patch,const float *g_patch,float fsum_gsum,float f_recip_g_recip) +{ + float fgsum,fg_corr; + + fgsum=db_ScalarProduct128Aligned16_f(f_patch,g_patch); + + fg_corr=121.0f*fgsum-fsum_gsum; + if(fg_corr>=0.0) return(fg_corr*fg_corr*f_recip_g_recip); + return(-fg_corr*fg_corr*f_recip_g_recip); +} + +inline float db_SignedSquareNormCorr15x15_f(float **f_img,float **g_img,int x_f,int y_f,int x_g,int y_g) +{ + float *pf,*pg; + float f,g,fgsum,f2sum,g2sum,fsum,gsum,fg_corr,den; + int xm_f,xm_g; + + xm_f=x_f-7; + xm_g=x_g-7; + fgsum=0.0; f2sum=0.0; g2sum=0.0; fsum=0.0; gsum=0.0; + + pf=f_img[y_f-7]+xm_f; pg=g_img[y_g-7]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-6]+xm_f; pg=g_img[y_g-6]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-5]+xm_f; pg=g_img[y_g-5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-4]+xm_f; pg=g_img[y_g-4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-3]+xm_f; pg=g_img[y_g-3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-2]+xm_f; pg=g_img[y_g-2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f-1]+xm_f; pg=g_img[y_g-1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f]+xm_f; pg=g_img[y_g]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+1]+xm_f; pg=g_img[y_g+1]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+2]+xm_f; pg=g_img[y_g+2]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+3]+xm_f; pg=g_img[y_g+3]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+4]+xm_f; pg=g_img[y_g+4]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+5]+xm_f; pg=g_img[y_g+5]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+6]+xm_f; pg=g_img[y_g+6]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + pf=f_img[y_f+7]+xm_f; pg=g_img[y_g+7]+xm_g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf++; g= *pg++; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + f= *pf; g= *pg; fgsum+=f*g; f2sum+=f*f; g2sum+=g*g; fsum+=f; gsum+=g; + + fg_corr=225.0f*fgsum-fsum*gsum; + den=(225.0f*f2sum-fsum*fsum)*(225.0f*g2sum-gsum*gsum); + if(den!=0.0) + { + if(fg_corr>=0.0) return(fg_corr*fg_corr/den); + return(-fg_corr*fg_corr/den); + } + return(0.0); +} + +db_Bucket_f** db_AllocBuckets_f(int nr_h,int nr_v,int bd) +{ + int i,j; + db_Bucket_f **bp,*b; + + b=new db_Bucket_f [(nr_h+2)*(nr_v+2)]; + bp=new db_Bucket_f* [(nr_v+2)]; + bp=bp+1; + for(i= -1;i<=nr_v;i++) + { + bp[i]=b+1+(nr_h+2)*(i+1); + for(j= -1;j<=nr_h;j++) + { + bp[i][j].ptr=new db_PointInfo_f [bd]; + } + } + + return(bp); +} + +db_Bucket_u** db_AllocBuckets_u(int nr_h,int nr_v,int bd) +{ + int i,j; + db_Bucket_u **bp,*b; + + b=new db_Bucket_u [(nr_h+2)*(nr_v+2)]; + bp=new db_Bucket_u* [(nr_v+2)]; + bp=bp+1; + for(i= -1;i<=nr_v;i++) + { + bp[i]=b+1+(nr_h+2)*(i+1); + for(j= -1;j<=nr_h;j++) + { + bp[i][j].ptr=new db_PointInfo_u [bd]; + } + } + + return(bp); +} + +void db_FreeBuckets_f(db_Bucket_f **bp,int nr_h,int nr_v) +{ + int i,j; + + for(i= -1;i<=nr_v;i++) for(j= -1;j<=nr_h;j++) + { + delete [] bp[i][j].ptr; + } + delete [] (bp[-1]-1); + delete [] (bp-1); +} + +void db_FreeBuckets_u(db_Bucket_u **bp,int nr_h,int nr_v) +{ + int i,j; + + for(i= -1;i<=nr_v;i++) for(j= -1;j<=nr_h;j++) + { + delete [] bp[i][j].ptr; + } + delete [] (bp[-1]-1); + delete [] (bp-1); +} + +void db_EmptyBuckets_f(db_Bucket_f **bp,int nr_h,int nr_v) +{ + int i,j; + for(i= -1;i<=nr_v;i++) for(j= -1;j<=nr_h;j++) bp[i][j].nr=0; +} + +void db_EmptyBuckets_u(db_Bucket_u **bp,int nr_h,int nr_v) +{ + int i,j; + for(i= -1;i<=nr_v;i++) for(j= -1;j<=nr_h;j++) bp[i][j].nr=0; +} + +float* db_FillBuckets_f(float *patch_space,const float * const *f_img,db_Bucket_f **bp,int bw,int bh,int nr_h,int nr_v,int bd,const double *x,const double *y,int nr_corners) +{ + int i,xi,yi,xpos,ypos,nr; + db_Bucket_f *br; + db_PointInfo_f *pir; + + db_EmptyBuckets_f(bp,nr_h,nr_v); + for(i=0;i=0 && xpos=0 && yposnr; + if(nrptr[nr]); + pir->x=xi; + pir->y=yi; + pir->id=i; + pir->pir=0; + pir->patch=patch_space; + br->nr=nr+1; + + db_SignedSquareNormCorr11x11_PreAlign_f(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=128; + } + } + } + return(patch_space); +} + +short* db_FillBuckets_u(short *patch_space,const unsigned char * const *f_img,db_Bucket_u **bp,int bw,int bh,int nr_h,int nr_v,int bd,const double *x,const double *y,int nr_corners,int use_smaller_matching_window, int use_21) +{ + int i,xi,yi,xpos,ypos,nr; + db_Bucket_u *br; + db_PointInfo_u *pir; + + db_EmptyBuckets_u(bp,nr_h,nr_v); + for(i=0;i=0 && xpos=0 && yposnr; + if(nrptr[nr]); + pir->x=xi; + pir->y=yi; + pir->id=i; + pir->pir=0; + pir->patch=patch_space; + br->nr=nr+1; + + if(use_21) + { + db_SignedSquareNormCorr21x21_PreAlign_u(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=512; + } + else + { + if(!use_smaller_matching_window) + { + db_SignedSquareNormCorr11x11_PreAlign_u(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=128; + } + else + { + db_SignedSquareNormCorr5x5_PreAlign_u(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=32; + } + } + } + } + } + return(patch_space); +} + + + +float* db_FillBucketsPrewarped_f(float *patch_space,const float *const *f_img,db_Bucket_f **bp,int bw,int bh,int nr_h,int nr_v,int bd,const double *x,const double *y,int nr_corners,const double H[9]) +{ + int i,xi,yi,xpos,ypos,nr,wxi,wyi; + db_Bucket_f *br; + db_PointInfo_f *pir; + double xd[2],wx[2]; + + db_EmptyBuckets_f(bp,nr_h,nr_v); + for(i=0;i= -1 && xpos<=nr_h && ypos>= -1 && ypos<=nr_v) + { + br=&bp[ypos][xpos]; + nr=br->nr; + if(nrptr[nr]); + pir->x=wxi; + pir->y=wyi; + pir->id=i; + pir->pir=0; + pir->patch=patch_space; + br->nr=nr+1; + + db_SignedSquareNormCorr11x11_PreAlign_f(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=128; + } + } + } + return(patch_space); +} + +short* db_FillBucketsPrewarped_u(short *patch_space,const unsigned char * const *f_img,db_Bucket_u **bp, + int bw,int bh,int nr_h,int nr_v,int bd,const double *x,const double *y, + int nr_corners,const double H[9]) +{ + int i,xi,yi,xpos,ypos,nr,wxi,wyi; + db_Bucket_u *br; + db_PointInfo_u *pir; + double xd[2],wx[2]; + + db_EmptyBuckets_u(bp,nr_h,nr_v); + for(i=0;i= -1 && xpos<=nr_h && ypos>= -1 && ypos<=nr_v) + { + br=&bp[ypos][xpos]; + nr=br->nr; + if(nrptr[nr]); + pir->x=wxi; + pir->y=wyi; + pir->id=i; + pir->pir=0; + pir->patch=patch_space; + br->nr=nr+1; + + db_SignedSquareNormCorr11x11_PreAlign_u(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip)); + patch_space+=128; + } + } + } + return(patch_space); +} + + + +short* db_FillBucketsPrewarpedAffine_u(short *patch_space,const unsigned char * const *f_img,db_Bucket_u **bp, + int bw,int bh,int nr_h,int nr_v,int bd,const double *x,const double *y, + int nr_corners,const double H[9],const double Hinv[9],const int warpboundsp[4], + int affine) +{ + int i,xi,yi,xpos,ypos,nr,wxi,wyi; + db_Bucket_u *br; + db_PointInfo_u *pir; + double xd[2],wx[2]; + + db_EmptyBuckets_u(bp,nr_h,nr_v); + for(i=0;i= -1 && xpos<=nr_h && ypos>= -1 && ypos<=nr_v) + { + if( xi>warpboundsp[0] && xiwarpboundsp[2] && yinr; + if(nrptr[nr]); + pir->x=wxi; + pir->y=wyi; + pir->id=i; + pir->pir=0; + pir->patch=patch_space; + br->nr=nr+1; + + db_SignedSquareNormCorr11x11_PreAlign_AffinePatchWarp_u(patch_space,f_img,xi,yi,&(pir->sum),&(pir->recip),Hinv,affine); + patch_space+=128; + } + } + } + } + return(patch_space); +} + + + +inline void db_MatchPointPair_f(db_PointInfo_f *pir_l,db_PointInfo_f *pir_r, + unsigned long kA,unsigned long kB) +{ + int x_l,y_l,x_r,y_r,xm,ym; + double score; + + x_l=pir_l->x; + y_l=pir_l->y; + x_r=pir_r->x; + y_r=pir_r->y; + xm=x_l-x_r; + ym=y_l-y_r; + /*Check if disparity is within the maximum disparity + with the formula xm^2*256+ym^2*kApatch,pir_r->patch, + (pir_l->sum)*(pir_r->sum), + (pir_l->recip)*(pir_r->recip)); + + if((!(pir_l->pir)) || (score>pir_l->s)) + { + /*Update left corner*/ + pir_l->s=score; + pir_l->pir=pir_r; + } + if((!(pir_r->pir)) || (score>pir_r->s)) + { + /*Update right corner*/ + pir_r->s=score; + pir_r->pir=pir_l; + } + } +} + +inline void db_MatchPointPair_u(db_PointInfo_u *pir_l,db_PointInfo_u *pir_r, + unsigned long kA,unsigned long kB, unsigned int rect_window,bool use_smaller_matching_window, int use_21) +{ + int xm,ym; + double score; + bool compute_score; + + + if( rect_window ) + compute_score = ((unsigned)db_absi(pir_l->x - pir_r->x)y - pir_r->y)x - pir_r->x; + ym= pir_l->y - pir_r->y; + compute_score = ((xm*xm)<<8)+ym*ym*kA < kB; + } + + if ( compute_score ) + { + if(use_21) + { + score=db_SignedSquareNormCorr21x21Aligned_Post_s(pir_l->patch,pir_r->patch, + (pir_l->sum)*(pir_r->sum), + (pir_l->recip)*(pir_r->recip)); + } + else + { + /*Correlate*/ + if(!use_smaller_matching_window) + { + score=db_SignedSquareNormCorr11x11Aligned_Post_s(pir_l->patch,pir_r->patch, + (pir_l->sum)*(pir_r->sum), + (pir_l->recip)*(pir_r->recip)); + } + else + { + score=db_SignedSquareNormCorr5x5Aligned_Post_s(pir_l->patch,pir_r->patch, + (pir_l->sum)*(pir_r->sum), + (pir_l->recip)*(pir_r->recip)); + } + } + + if((!(pir_l->pir)) || (score>pir_l->s)) + { + /*Update left corner*/ + pir_l->s=score; + pir_l->pir=pir_r; + } + if((!(pir_r->pir)) || (score>pir_r->s)) + { + /*Update right corner*/ + pir_r->s=score; + pir_r->pir=pir_l; + } + } +} + +inline void db_MatchPointAgainstBucket_f(db_PointInfo_f *pir_l,db_Bucket_f *b_r, + unsigned long kA,unsigned long kB) +{ + int p_r,nr; + db_PointInfo_f *pir_r; + + nr=b_r->nr; + pir_r=b_r->ptr; + for(p_r=0;p_rnr; + pir_r=b_r->ptr; + + for(p_r=0;p_rnr; + /*For all points in bucket*/ + for(k=0;kptr+k; + for(a=i-1;a<=i+1;a++) + { + for(b=j-1;b<=j+1;b++) + { + db_MatchPointAgainstBucket_f(pir_l,&bp_r[a][b],kA,kB); + } + } + } + } +} + +void db_MatchBuckets_u(db_Bucket_u **bp_l,db_Bucket_u **bp_r,int nr_h,int nr_v, + unsigned long kA,unsigned long kB,int rect_window,bool use_smaller_matching_window, int use_21) +{ + int i,j,k,a,b,br_nr; + db_Bucket_u *br; + db_PointInfo_u *pir_l; + + /*For all buckets*/ + for(i=0;inr; + /*For all points in bucket*/ + for(k=0;kptr+k; + for(a=i-1;a<=i+1;a++) + { + for(b=j-1;b<=j+1;b++) + { + db_MatchPointAgainstBucket_u(pir_l,&bp_r[a][b],kA,kB,rect_window,use_smaller_matching_window, use_21); + } + } + } + } +} + +void db_CollectMatches_f(db_Bucket_f **bp_l,int nr_h,int nr_v,unsigned long target,int *id_l,int *id_r,int *nr_matches) +{ + int i,j,k,br_nr; + unsigned long count; + db_Bucket_f *br; + db_PointInfo_f *pir,*pir2; + + count=0; + /*For all buckets*/ + for(i=0;inr; + /*For all points in bucket*/ + for(k=0;kptr+k; + pir2=pir->pir; + if(pir2) + { + /*This point has a best match*/ + if((pir2->pir)==pir) + { + /*We have a mutually consistent match*/ + if(countid; + id_r[count]=pir2->id; + count++; + } + } + } + } + } + *nr_matches=count; +} + +void db_CollectMatches_u(db_Bucket_u **bp_l,int nr_h,int nr_v,unsigned long target,int *id_l,int *id_r,int *nr_matches) +{ + int i,j,k,br_nr; + unsigned long count; + db_Bucket_u *br; + db_PointInfo_u *pir,*pir2; + + count=0; + /*For all buckets*/ + for(i=0;inr; + /*For all points in bucket*/ + for(k=0;kptr+k; + pir2=pir->pir; + if(pir2) + { + /*This point has a best match*/ + if((pir2->pir)==pir) + { + /*We have a mutually consistent match*/ + if(countid; + id_r[count]=pir2->id; + count++; + } + } + } + } + } + *nr_matches=count; +} + +db_Matcher_f::db_Matcher_f() +{ + m_w=0; m_h=0; +} + +db_Matcher_f::~db_Matcher_f() +{ + Clean(); +} + +void db_Matcher_f::Clean() +{ + if(m_w) + { + /*Free buckets*/ + db_FreeBuckets_f(m_bp_l,m_nr_h,m_nr_v); + db_FreeBuckets_f(m_bp_r,m_nr_h,m_nr_v); + /*Free space for patch layouts*/ + delete [] m_patch_space; + } + m_w=0; m_h=0; +} + +unsigned long db_Matcher_f::Init(int im_width,int im_height,double max_disparity,int target_nr_corners) +{ + Clean(); + m_w=im_width; + m_h=im_height; + m_bw=db_maxi(1,(int) (max_disparity*((double)im_width))); + m_bh=db_maxi(1,(int) (max_disparity*((double)im_height))); + m_nr_h=1+(im_width-1)/m_bw; + m_nr_v=1+(im_height-1)/m_bh; + m_bd=db_maxi(1,(int)(((double)target_nr_corners)* + max_disparity*max_disparity)); + m_target=target_nr_corners; + m_kA=(long)(256.0*((double)(m_w*m_w))/((double)(m_h*m_h))); + m_kB=(long)(256.0*max_disparity*max_disparity*((double)(m_w*m_w))); + + /*Alloc bucket structure*/ + m_bp_l=db_AllocBuckets_f(m_nr_h,m_nr_v,m_bd); + m_bp_r=db_AllocBuckets_f(m_nr_h,m_nr_v,m_bd); + + /*Alloc 16byte-aligned space for patch layouts*/ + m_patch_space=new float [2*(m_nr_h+2)*(m_nr_v+2)*m_bd*128+16]; + m_aligned_patch_space=db_AlignPointer_f(m_patch_space,16); + + return(m_target); +} + +void db_Matcher_f::Match(const float * const *l_img,const float * const *r_img, + const double *x_l,const double *y_l,int nr_l,const double *x_r,const double *y_r,int nr_r, + int *id_l,int *id_r,int *nr_matches,const double H[9]) +{ + float *ps; + + /*Insert the corners into bucket structure*/ + ps=db_FillBuckets_f(m_aligned_patch_space,l_img,m_bp_l,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_l,y_l,nr_l); + if(H==0) db_FillBuckets_f(ps,r_img,m_bp_r,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_r,y_r,nr_r); + else db_FillBucketsPrewarped_f(ps,r_img,m_bp_r,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_r,y_r,nr_r,H); + + /*Compute all the necessary match scores*/ + db_MatchBuckets_f(m_bp_l,m_bp_r,m_nr_h,m_nr_v,m_kA,m_kB); + + /*Collect the correspondences*/ + db_CollectMatches_f(m_bp_l,m_nr_h,m_nr_v,m_target,id_l,id_r,nr_matches); +} + +db_Matcher_u::db_Matcher_u() +{ + m_w=0; m_h=0; + m_rect_window = 0; + m_bw=m_bh=m_nr_h=m_nr_v=m_bd=m_target=0; + m_bp_l=m_bp_r=0; + m_patch_space=m_aligned_patch_space=0; +} + +db_Matcher_u::db_Matcher_u(const db_Matcher_u& cm) +{ + Init(cm.m_w, cm.m_h, cm.m_max_disparity, cm.m_target, cm.m_max_disparity_v); +} + +db_Matcher_u& db_Matcher_u::operator= (const db_Matcher_u& cm) +{ + if ( this == &cm ) return *this; + Init(cm.m_w, cm.m_h, cm.m_max_disparity, cm.m_target, cm.m_max_disparity_v); + return *this; +} + + +db_Matcher_u::~db_Matcher_u() +{ + Clean(); +} + +void db_Matcher_u::Clean() +{ + if(m_w) + { + /*Free buckets*/ + db_FreeBuckets_u(m_bp_l,m_nr_h,m_nr_v); + db_FreeBuckets_u(m_bp_r,m_nr_h,m_nr_v); + /*Free space for patch layouts*/ + delete [] m_patch_space; + } + m_w=0; m_h=0; +} + + +unsigned long db_Matcher_u::Init(int im_width,int im_height,double max_disparity,int target_nr_corners, + double max_disparity_v, bool use_smaller_matching_window, int use_21) +{ + Clean(); + m_w=im_width; + m_h=im_height; + m_max_disparity=max_disparity; + m_max_disparity_v=max_disparity_v; + + if ( max_disparity_v != DB_DEFAULT_NO_DISPARITY ) + { + m_rect_window = 1; + + m_bw=db_maxi(1,(int)(max_disparity*((double)im_width))); + m_bh=db_maxi(1,(int)(max_disparity_v*((double)im_height))); + + m_bd=db_maxi(1,(int)(((double)target_nr_corners)*max_disparity*max_disparity_v)); + + m_kA=(int)(max_disparity*m_w); + m_kB=(int)(max_disparity_v*m_h); + + } else + { + m_bw=(int)db_maxi(1,(int)(max_disparity*((double)im_width))); + m_bh=(int)db_maxi(1,(int)(max_disparity*((double)im_height))); + + m_bd=db_maxi(1,(int)(((double)target_nr_corners)*max_disparity*max_disparity)); + + m_kA=(long)(256.0*((double)(m_w*m_w))/((double)(m_h*m_h))); + m_kB=(long)(256.0*max_disparity*max_disparity*((double)(m_w*m_w))); + } + + m_nr_h=1+(im_width-1)/m_bw; + m_nr_v=1+(im_height-1)/m_bh; + + m_target=target_nr_corners; + + /*Alloc bucket structure*/ + m_bp_l=db_AllocBuckets_u(m_nr_h,m_nr_v,m_bd); + m_bp_r=db_AllocBuckets_u(m_nr_h,m_nr_v,m_bd); + + m_use_smaller_matching_window = use_smaller_matching_window; + m_use_21 = use_21; + + if(m_use_21) + { + /*Alloc 64byte-aligned space for patch layouts*/ + m_patch_space=new short [2*(m_nr_h+2)*(m_nr_v+2)*m_bd*512+64]; + m_aligned_patch_space=db_AlignPointer_s(m_patch_space,64); + } + else + { + if(!m_use_smaller_matching_window) + { + /*Alloc 16byte-aligned space for patch layouts*/ + m_patch_space=new short [2*(m_nr_h+2)*(m_nr_v+2)*m_bd*128+16]; + m_aligned_patch_space=db_AlignPointer_s(m_patch_space,16); + } + else + { + /*Alloc 4byte-aligned space for patch layouts*/ + m_patch_space=new short [2*(m_nr_h+2)*(m_nr_v+2)*m_bd*32+4]; + m_aligned_patch_space=db_AlignPointer_s(m_patch_space,4); + } + } + + return(m_target); +} + +void db_Matcher_u::Match(const unsigned char * const *l_img,const unsigned char * const *r_img, + const double *x_l,const double *y_l,int nr_l,const double *x_r,const double *y_r,int nr_r, + int *id_l,int *id_r,int *nr_matches,const double H[9],int affine) +{ + short *ps; + + /*Insert the corners into bucket structure*/ + ps=db_FillBuckets_u(m_aligned_patch_space,l_img,m_bp_l,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_l,y_l,nr_l,m_use_smaller_matching_window,m_use_21); + if(H==0) + db_FillBuckets_u(ps,r_img,m_bp_r,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_r,y_r,nr_r,m_use_smaller_matching_window,m_use_21); + else + { + if (affine) + { + double Hinv[9]; + db_InvertAffineTransform(Hinv,H); + float r_w, c_w; + float stretch_x[2]; + float stretch_y[2]; + AffineWarpPointOffset(r_w,c_w,Hinv, 5,5); + stretch_x[0]=db_absf(c_w);stretch_y[0]=db_absf(r_w); + AffineWarpPointOffset(r_w,c_w,Hinv, 5,-5); + stretch_x[1]=db_absf(c_w);stretch_y[1]=db_absf(r_w); + int max_stretxh_x=(int) (db_maxd(stretch_x[0],stretch_x[1])); + int max_stretxh_y=(int) (db_maxd(stretch_y[0],stretch_y[1])); + int warpbounds[4]={max_stretxh_x,m_w-1-max_stretxh_x,max_stretxh_y,m_h-1-max_stretxh_y}; + + for (int r=-5;r<=5;r++){ + for (int c=-5;c<=5;c++){ + AffineWarpPointOffset(r_w,c_w,Hinv,r,c); + AffineWarpPoint_BL_LUT_y[r+5][c+5]=r_w; + AffineWarpPoint_BL_LUT_x[r+5][c+5]=c_w; + + AffineWarpPoint_NN_LUT_y[r+5][c+5]=db_roundi(r_w); + AffineWarpPoint_NN_LUT_x[r+5][c+5]=db_roundi(c_w); + + } + } + + db_FillBucketsPrewarpedAffine_u(ps,r_img,m_bp_r,m_bw,m_bh,m_nr_h,m_nr_v,m_bd, + x_r,y_r,nr_r,H,Hinv,warpbounds,affine); + } + else + db_FillBucketsPrewarped_u(ps,r_img,m_bp_r,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,x_r,y_r,nr_r,H); + } + + + /*Compute all the necessary match scores*/ + db_MatchBuckets_u(m_bp_l,m_bp_r,m_nr_h,m_nr_v,m_kA,m_kB, m_rect_window,m_use_smaller_matching_window,m_use_21); + + /*Collect the correspondences*/ + db_CollectMatches_u(m_bp_l,m_nr_h,m_nr_v,m_target,id_l,id_r,nr_matches); +} + +int db_Matcher_u::IsAllocated() +{ + return (int)(m_w != 0); +} diff --git a/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.h b/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.h new file mode 100644 index 0000000000000000000000000000000000000000..6c056b9a38702c607b35449bc6aab6b7800f3dab --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_feature_matching.h @@ -0,0 +1,260 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*$Id: db_feature_matching.h,v 1.3 2011/06/17 14:03:30 mbansal Exp $*/ + +#ifndef DB_FEATURE_MATCHING_H +#define DB_FEATURE_MATCHING_H + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup FeatureMatching Feature Matching + */ +#include "db_utilities.h" +#include "db_utilities_constants.h" + +DB_API void db_SignedSquareNormCorr21x21_PreAlign_u(short *patch,const unsigned char * const *f_img,int x_f,int y_f,float *sum,float *recip); +DB_API void db_SignedSquareNormCorr11x11_PreAlign_u(short *patch,const unsigned char * const *f_img,int x_f,int y_f,float *sum,float *recip); +float db_SignedSquareNormCorr21x21Aligned_Post_s(const short *f_patch,const short *g_patch,float fsum_gsum,float f_recip_g_recip); +float db_SignedSquareNormCorr11x11Aligned_Post_s(const short *f_patch,const short *g_patch,float fsum_gsum,float f_recip_g_recip); + +class db_PointInfo_f +{ +public: + /*Coordinates of point*/ + int x; + int y; + /*Id nr of point*/ + int id; + /*Best match score*/ + double s; + /*Best match candidate*/ + db_PointInfo_f *pir; + /*Precomputed coefficients + of image patch*/ + float sum; + float recip; + /*Pointer to patch layout*/ + const float *patch; +}; + +class db_Bucket_f +{ +public: + db_PointInfo_f *ptr; + int nr; +}; + +class db_PointInfo_u +{ +public: + /*Coordinates of point*/ + int x; + int y; + /*Id nr of point*/ + int id; + /*Best match score*/ + double s; + /*Best match candidate*/ + db_PointInfo_u *pir; + /*Precomputed coefficients + of image patch*/ + float sum; + float recip; + /*Pointer to patch layout*/ + const short *patch; +}; + +class db_Bucket_u +{ +public: + db_PointInfo_u *ptr; + int nr; +}; +/*! + * \class db_Matcher_f + * \ingroup FeatureMatching + * \brief Feature matcher for float images. + * + * Normalized correlation feature matcher for float images. + * Correlation window size is constant and set to 11x11. + * See \ref FeatureDetection to detect Harris corners. + * Images are managed with functions in \ref LMImageBasicUtilities. + */ +class DB_API db_Matcher_f +{ +public: + db_Matcher_f(); + ~db_Matcher_f(); + + /*! + * Set parameters and pre-allocate memory. Return an upper bound + * on the number of matches. + * \param im_width width + * \param im_height height + * \param max_disparity maximum distance (as fraction of image size) between matches + * \param target_nr_corners maximum number of matches + * \return maximum number of matches + */ + unsigned long Init(int im_width,int im_height, + double max_disparity=DB_DEFAULT_MAX_DISPARITY, + int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS); + + /*! + * Match two sets of features. + * If the prewarp H is not NULL it will be applied to the features + * in the right image before matching. + * Parameters id_l and id_r must point to arrays of size target_nr_corners + * (returned by Init()). + * The results of matching are in id_l and id_r. + * Interpretaqtion of results: if id_l[i] = m and id_r[i] = n, + * feature at (x_l[m],y_l[m]) matched to (x_r[n],y_r[n]). + * \param l_img left image + * \param r_img right image + * \param x_l left x coordinates of features + * \param y_l left y coordinates of features + * \param nr_l number of features in left image + * \param x_r right x coordinates of features + * \param y_r right y coordinates of features + * \param nr_r number of features in right image + * \param id_l indices of left features that matched + * \param id_r indices of right features that matched + * \param nr_matches number of features actually matched + * \param H image homography (prewarp) to be applied to right image features + */ + void Match(const float * const *l_img,const float * const *r_img, + const double *x_l,const double *y_l,int nr_l,const double *x_r,const double *y_r,int nr_r, + int *id_l,int *id_r,int *nr_matches,const double H[9]=0); + +protected: + void Clean(); + + int m_w,m_h,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,m_target; + unsigned long m_kA,m_kB; + db_Bucket_f **m_bp_l; + db_Bucket_f **m_bp_r; + float *m_patch_space,*m_aligned_patch_space; +}; +/*! + * \class db_Matcher_u + * \ingroup FeatureMatching + * \brief Feature matcher for byte images. + * + * Normalized correlation feature matcher for byte images. + * Correlation window size is constant and set to 11x11. + * See \ref FeatureDetection to detect Harris corners. + * Images are managed with functions in \ref LMImageBasicUtilities. + * + * If the prewarp matrix H is supplied, the feature coordinates are warped by H before being placed in + * appropriate buckets. If H is an affine transform and the "affine" parameter is set to 1 or 2, + * then the correlation patches themselves are warped before being placed in the patch space. + */ +class DB_API db_Matcher_u +{ +public: + db_Matcher_u(); + + int GetPatchSize(){return 11;}; + + virtual ~db_Matcher_u(); + + /*! + Copy ctor duplicates settings. + Memory not copied. + */ + db_Matcher_u(const db_Matcher_u& cm); + + /*! + Assignment optor duplicates settings + Memory not copied. + */ + db_Matcher_u& operator= (const db_Matcher_u& cm); + + /*! + * Set parameters and pre-allocate memory. Return an upper bound + * on the number of matches. + * If max_disparity_v is DB_DEFAULT_NO_DISPARITY, look for matches + * in a ellipse around a feature of radius max_disparity*im_width by max_disparity*im_height. + * If max_disparity_v is specified, use a rectangle max_disparity*im_width by max_disparity_v*im_height. + * \param im_width width + * \param im_height height + * \param max_disparity maximum distance (as fraction of image size) between matches + * \param target_nr_corners maximum number of matches + * \param max_disparity_v maximum vertical disparity (distance between matches) + * \param use_smaller_matching_window if set to true, uses a correlation window of 5x5 instead of the default 11x11 + * \return maximum number of matches + */ + virtual unsigned long Init(int im_width,int im_height, + double max_disparity=DB_DEFAULT_MAX_DISPARITY, + int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS, + double max_disparity_v=DB_DEFAULT_NO_DISPARITY, + bool use_smaller_matching_window=false, int use_21=0); + + /*! + * Match two sets of features. + * If the prewarp H is not NULL it will be applied to the features + * in the right image before matching. + * Parameters id_l and id_r must point to arrays of size target_nr_corners + * (returned by Init()). + * The results of matching are in id_l and id_r. + * Interpretaqtion of results: if id_l[i] = m and id_r[i] = n, + * feature at (x_l[m],y_l[m]) matched to (x_r[n],y_r[n]). + * \param l_img left image + * \param r_img right image + * \param x_l left x coordinates of features + * \param y_l left y coordinates of features + * \param nr_l number of features in left image + * \param x_r right x coordinates of features + * \param y_r right y coordinates of features + * \param nr_r number of features in right image + * \param id_l indices of left features that matched + * \param id_r indices of right features that matched + * \param nr_matches number of features actually matched + * \param H image homography (prewarp) to be applied to right image features + * \param affine prewarp the 11x11 patches by given affine transform. 0 means no warping, + 1 means nearest neighbor, 2 means bilinear warping. + */ + virtual void Match(const unsigned char * const *l_img,const unsigned char * const *r_img, + const double *x_l,const double *y_l,int nr_l,const double *x_r,const double *y_r,int nr_r, + int *id_l,int *id_r,int *nr_matches,const double H[9]=0,int affine=0); + + /*! + * Checks if Init() was called. + * \return 1 if Init() was called, 0 otherwise. + */ + int IsAllocated(); + +protected: + virtual void Clean(); + + + int m_w,m_h,m_bw,m_bh,m_nr_h,m_nr_v,m_bd,m_target; + unsigned long m_kA,m_kB; + db_Bucket_u **m_bp_l; + db_Bucket_u **m_bp_r; + short *m_patch_space,*m_aligned_patch_space; + + double m_max_disparity, m_max_disparity_v; + int m_rect_window; + bool m_use_smaller_matching_window; + int m_use_21; +}; + + + +#endif /*DB_FEATURE_MATCHING_H*/ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_framestitching.cpp b/jni_mosaic/feature_stab/db_vlvm/db_framestitching.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b574f7a04739a190768aedbcd2de0908e05764ec --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_framestitching.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_framestitching.cpp,v 1.2 2011/06/17 14:03:30 mbansal Exp $ */ + +#include "db_utilities.h" +#include "db_framestitching.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +inline void db_RotationFromMOuterProductSum(double R[9],double *score,double M[9]) +{ + double N[16],q[4],lambda[4],lambda_max; + double y[4]; + int nr_roots; + + N[0]= M[0]+M[4]+M[8]; + N[5]= M[0]-M[4]-M[8]; + N[10]= -M[0]+M[4]-M[8]; + N[15]= -M[0]-M[4]+M[8]; + N[1] =N[4] =M[5]-M[7]; + N[2] =N[8] =M[6]-M[2]; + N[3] =N[12]=M[1]-M[3]; + N[6] =N[9] =M[1]+M[3]; + N[7] =N[13]=M[6]+M[2]; + N[11]=N[14]=M[5]+M[7]; + + /*get the quaternion representing the rotation + by finding the eigenvector corresponding to the most + positive eigenvalue. Force eigenvalue solutions, since the matrix + is symmetric and solutions might otherwise be lost + when the data is planar*/ + db_RealEigenvalues4x4(lambda,&nr_roots,N,1); + if(nr_roots) + { + lambda_max=lambda[0]; + if(nr_roots>=2) + { + if(lambda[1]>lambda_max) lambda_max=lambda[1]; + if(nr_roots>=3) + { + if(lambda[2]>lambda_max) lambda_max=lambda[2]; + { + if(nr_roots>=4) if(lambda[3]>lambda_max) lambda_max=lambda[3]; + } + } + } + } + else lambda_max=1.0; + db_EigenVector4x4(q,lambda_max,N); + + /*Compute the rotation matrix*/ + db_QuaternionToRotation(R,q); + + if(score) + { + /*Compute score=transpose(q)*N*q */ + db_Multiply4x4_4x1(y,N,q); + *score=db_ScalarProduct4(q,y); + } +} + +void db_StitchSimilarity3DRaw(double *scale,double R[9],double t[3], + double **Xp,double **X,int nr_points,int orientation_preserving, + int allow_scaling,int allow_rotation,int allow_translation) +{ + int i; + double c[3],cp[3],r[3],rp[3],M[9],s,sp,sc; + double Rr[9],score_p,score_r; + double *temp,*temp_p; + + if(allow_translation) + { + db_PointCentroid3D(c,X,nr_points); + db_PointCentroid3D(cp,Xp,nr_points); + } + else + { + db_Zero3(c); + db_Zero3(cp); + } + + db_Zero9(M); + s=sp=0; + for(i=0;iscore_p) + { + /*Reverse is better*/ + R[0]=Rr[0]; R[1]=Rr[1]; R[2]= -Rr[2]; + R[3]=Rr[3]; R[4]=Rr[4]; R[5]= -Rr[5]; + R[6]=Rr[6]; R[7]=Rr[7]; R[8]= -Rr[8]; + } + } + } + else db_Identity3x3(R); + + /*Compute translation*/ + if(allow_translation) + { + t[0]=cp[0]-sc*(R[0]*c[0]+R[1]*c[1]+R[2]*c[2]); + t[1]=cp[1]-sc*(R[3]*c[0]+R[4]*c[1]+R[5]*c[2]); + t[2]=cp[2]-sc*(R[6]*c[0]+R[7]*c[1]+R[8]*c[2]); + } + else db_Zero3(t); +} + + diff --git a/jni_mosaic/feature_stab/db_vlvm/db_framestitching.h b/jni_mosaic/feature_stab/db_vlvm/db_framestitching.h new file mode 100644 index 0000000000000000000000000000000000000000..5fef5f37ef9e7b5e15e4f1d0029096bbb7493ca0 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_framestitching.h @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_framestitching.h,v 1.2 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_FRAMESTITCHING_H +#define DB_FRAMESTITCHING_H +/*! + * \defgroup FrameStitching Frame Stitching (2D and 3D homography estimation) + */ +/*\{*/ + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMFrameStitching (LM) Frame Stitching (2D and 3D homography estimation) + */ +/*\{*/ + +/*! +Find scale, rotation and translation of the similarity that +takes the nr_points inhomogenous 3D points X to Xp +(left to right according to Horn), i.e. for the homogenous equivalents +Xp and X we would have +\code + Xp~ + [sR t]*X + [0 1] +\endcode +If orientation_preserving is true, R is restricted such that det(R)>0. +allow_scaling, allow_rotation and allow_translation allow s,R and t +to differ from 1,Identity and 0 + +Full similarity takes the following on 550MHz: +\code +4.5 microseconds with 3 points +4.7 microseconds with 4 points +5.0 microseconds with 5 points +5.2 microseconds with 6 points +5.8 microseconds with 10 points +20 microseconds with 100 points +205 microseconds with 1000 points +2.9 milliseconds with 10000 points +50 milliseconds with 100000 points +0.5 seconds with 1000000 points +\endcode +Without orientation_preserving: +\code +4 points is minimal for (s,R,t) (R,t) +3 points is minimal for (s,R) (R) +2 points is minimal for (s,t) +1 point is minimal for (s) (t) +\endcode +With orientation_preserving: +\code +3 points is minimal for (s,R,t) (R,t) +2 points is minimal for (s,R) (s,t) (R) +1 point is minimal for (s) (t) +\endcode + +\param scale scale +\param R rotation +\param t translation +\param Xp inhomogenouse 3D points in first coordinate system +\param X inhomogenouse 3D points in second coordinate system +\param nr_points number of points +\param orientation_preserving if true, R is restricted such that det(R)>0. +\param allow_scaling estimate scale +\param allow_rotation estimate rotation +\param allow_translation estimate translation +*/ +DB_API void db_StitchSimilarity3DRaw(double *scale,double R[9],double t[3], + double **Xp,double **X,int nr_points,int orientation_preserving=1, + int allow_scaling=1,int allow_rotation=1,int allow_translation=1); + + +/*\}*/ + +#endif /* DB_FRAMESTITCHING_H */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_image_homography.cpp b/jni_mosaic/feature_stab/db_vlvm/db_image_homography.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaad7f85e76ab56d9417e767ab0cc74fa293415a --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_image_homography.cpp @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_image_homography.cpp,v 1.2 2011/06/17 14:03:31 mbansal Exp $ */ + +#include "db_utilities.h" +#include "db_image_homography.h" +#include "db_framestitching.h" +#include "db_metrics.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +/*Compute the linear constraint on H obtained by requiring that the +ratio between coordinate i_num and i_den of xp is equal to the ratio +between coordinate i_num and i_den of Hx. i_zero should be set to +the coordinate not equal to i_num or i_den. No normalization is used*/ +inline void db_SProjImagePointPointConstraint(double c[9],int i_num,int i_den,int i_zero, + double xp[3],double x[3]) +{ + db_MultiplyScalarCopy3(c+3*i_den,x, xp[i_num]); + db_MultiplyScalarCopy3(c+3*i_num,x, -xp[i_den]); + db_Zero3(c+3*i_zero); +} + +/*Compute two constraints on H generated by the correspondence (Xp,X), +assuming that Xp ~= H*X. No normalization is used*/ +inline void db_SProjImagePointPointConstraints(double c1[9],double c2[9],double xp[3],double x[3]) +{ + int ma_ind; + + /*Find index of coordinate of Xp with largest absolute value*/ + ma_ind=db_MaxAbsIndex3(xp); + + /*Generate 2 constraints, + each constraint is generated by considering the ratio between a + coordinate and the largest absolute value coordinate*/ + switch(ma_ind) + { + case 0: + db_SProjImagePointPointConstraint(c1,1,0,2,xp,x); + db_SProjImagePointPointConstraint(c2,2,0,1,xp,x); + break; + case 1: + db_SProjImagePointPointConstraint(c1,0,1,2,xp,x); + db_SProjImagePointPointConstraint(c2,2,1,0,xp,x); + break; + default: + db_SProjImagePointPointConstraint(c1,0,2,1,xp,x); + db_SProjImagePointPointConstraint(c2,1,2,0,xp,x); + } +} + +inline void db_SAffineImagePointPointConstraints(double c1[7],double c2[7],double xp[3],double x[3]) +{ + double ct1[9],ct2[9]; + + db_SProjImagePointPointConstraints(ct1,ct2,xp,x); + db_Copy6(c1,ct1); c1[6]=ct1[8]; + db_Copy6(c2,ct2); c2[6]=ct2[8]; +} + +void db_StitchProjective2D_4Points(double H[9], + double x1[3],double x2[3],double x3[3],double x4[3], + double xp1[3],double xp2[3],double xp3[3],double xp4[3]) +{ + double c[72]; + + /*Collect the constraints*/ + db_SProjImagePointPointConstraints(c ,c+9 ,xp1,x1); + db_SProjImagePointPointConstraints(c+18,c+27,xp2,x2); + db_SProjImagePointPointConstraints(c+36,c+45,xp3,x3); + db_SProjImagePointPointConstraints(c+54,c+63,xp4,x4); + /*Solve for the nullvector*/ + db_NullVector8x9Destructive(H,c); +} + +void db_StitchAffine2D_3Points(double H[9], + double x1[3],double x2[3],double x3[3], + double xp1[3],double xp2[3],double xp3[3]) +{ + double c[42]; + + /*Collect the constraints*/ + db_SAffineImagePointPointConstraints(c ,c+7 ,xp1,x1); + db_SAffineImagePointPointConstraints(c+14,c+21,xp2,x2); + db_SAffineImagePointPointConstraints(c+28,c+35,xp3,x3); + /*Solve for the nullvector*/ + db_NullVector6x7Destructive(H,c); + db_MultiplyScalar6(H,db_SafeReciprocal(H[6])); + H[6]=H[7]=0; H[8]=1.0; +} + +/*Compute up to three solutions for the focal length given two point correspondences +generated by a rotation with a common unknown focal length. No specific normalization +of the input points is required. If signed_disambiguation is true, the points are +required to be in front of the camera*/ +inline void db_CommonFocalLengthFromRotation_2Point(double fsol[3],int *nr_sols,double x1[3],double x2[3],double xp1[3],double xp2[3],int signed_disambiguation=1) +{ + double m,ax,ay,apx,apy,bx,by,bpx,bpy; + double p1[2],p2[2],p3[2],p4[2],p5[2],p6[2]; + double p7[3],p8[4],p9[5],p10[3],p11[4]; + double roots[3]; + int nr_roots,i,j; + + /*Solve for focal length using the equation + ^2*=^2* + where a and ap are the homogenous vectors in the first image + after focal length scaling and b,bp are the vectors in the + second image*/ + + /*Normalize homogenous coordinates so that last coordinate is one*/ + m=db_SafeReciprocal(x1[2]); + ax=x1[0]*m; + ay=x1[1]*m; + m=db_SafeReciprocal(xp1[2]); + apx=xp1[0]*m; + apy=xp1[1]*m; + m=db_SafeReciprocal(x2[2]); + bx=x2[0]*m; + by=x2[1]*m; + m=db_SafeReciprocal(xp2[2]); + bpx=xp2[0]*m; + bpy=xp2[1]*m; + + /*Compute cubic in l=1/(f^2) + by dividing out the root l=0 from the equation + (l(ax*bx+ay*by)+1)^2*(l(apx^2+apy^2)+1)*(l(bpx^2+bpy^2)+1)= + (l(apx*bpx+apy*bpy)+1)^2*(l(ax^2+ay^2)+1)*(l(bx^2+by^2)+1)*/ + p1[1]=ax*bx+ay*by; + p2[1]=db_sqr(apx)+db_sqr(apy); + p3[1]=db_sqr(bpx)+db_sqr(bpy); + p4[1]=apx*bpx+apy*bpy; + p5[1]=db_sqr(ax)+db_sqr(ay); + p6[1]=db_sqr(bx)+db_sqr(by); + p1[0]=p2[0]=p3[0]=p4[0]=p5[0]=p6[0]=1; + + db_MultiplyPoly1_1(p7,p1,p1); + db_MultiplyPoly1_2(p8,p2,p7); + db_MultiplyPoly1_3(p9,p3,p8); + + db_MultiplyPoly1_1(p10,p4,p4); + db_MultiplyPoly1_2(p11,p5,p10); + db_SubtractPolyProduct1_3(p9,p6,p11); + /*Cubic starts at p9[1]*/ + db_SolveCubic(roots,&nr_roots,p9[4],p9[3],p9[2],p9[1]); + + for(j=0,i=0;i0) + { + if((!signed_disambiguation) || (db_PolyEval1(p1,roots[i])*db_PolyEval1(p4,roots[i])>0)) + { + fsol[j++]=db_SafeSqrtReciprocal(roots[i]); + } + } + } + *nr_sols=j; +} + +int db_StitchRotationCommonFocalLength_3Points(double H[9],double x1[3],double x2[3],double x3[3],double xp1[3],double xp2[3],double xp3[3],double *f,int signed_disambiguation) +{ + double fsol[3]; + int nr_sols,i,best_sol,done; + double cost,best_cost; + double m,hyp[27],x1_temp[3],x2_temp[3],xp1_temp[3],xp2_temp[3]; + double *hyp_point,ft; + double y[2]; + + db_CommonFocalLengthFromRotation_2Point(fsol,&nr_sols,x1,x2,xp1,xp2,signed_disambiguation); + if(nr_sols) + { + db_DeHomogenizeImagePoint(y,xp3); + done=0; + for(i=0;idivisor) + { + m=db_SafeReciprocal(divisor2); + Am=Aacc2*m; + Bm=Bacc2*m; + R[0]= Am; + R[1]= Bm; + R[2]= Bm; + R[3]= -Am; + } + } + } + else db_Identity2x2(R); + + /*Compute translation*/ + if(allow_translation) + { + t[0]=cp[0]-sc*(R[0]*c[0]+R[1]*c[1]); + t[1]=cp[1]-sc*(R[2]*c[0]+R[3]*c[1]); + } + else db_Zero2(t); +} + + diff --git a/jni_mosaic/feature_stab/db_vlvm/db_image_homography.h b/jni_mosaic/feature_stab/db_vlvm/db_image_homography.h new file mode 100644 index 0000000000000000000000000000000000000000..165447dd78fbec8c570dd1268f7643e3277bd92c --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_image_homography.h @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_image_homography.h,v 1.2 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_IMAGE_HOMOGRAPHY +#define DB_IMAGE_HOMOGRAPHY + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +#include "db_framestitching.h" +/*! + * \defgroup LMImageHomography (LM) Image Homography Estimation (feature based) + */ +/*\{*/ +/*! +Solve for projective H such that xp~Hx. Prior normalization is not necessary, +although desirable for numerical conditioning +\param H image projective (out) +\param x1 image 1 point 1 +\param x2 image 1 point 2 +\param x3 image 1 point 3 +\param x4 image 1 point 4 +\param xp1 image 2 point 1 +\param xp2 image 2 point 2 +\param xp3 image 2 point 3 +\param xp4 image 2 point 4 +*/ +DB_API void db_StitchProjective2D_4Points(double H[9], + double x1[3],double x2[3],double x3[3],double x4[3], + double xp1[3],double xp2[3],double xp3[3],double xp4[3]); + +/*! +Solve for affine H such that xp~Hx. Prior normalization is not necessary, +although desirable for numerical conditioning +\param H image projective (out) +\param x1 image 1 point 1 +\param x2 image 1 point 2 +\param x3 image 1 point 3 +\param xp1 image 2 point 1 +\param xp2 image 2 point 2 +\param xp3 image 2 point 3 +*/ +DB_API void db_StitchAffine2D_3Points(double H[9], + double x1[3],double x2[3],double x3[3], + double xp1[3],double xp2[3],double xp3[3]); + +/*! +Solve for rotation R such that xp~Rx. +Image points have to be of unit norm for the least squares to be meaningful. +\param R image rotation (out) +\param x1 image 1 point 1 +\param x2 image 1 point 2 +\param xp1 image 2 point 1 +\param xp2 image 2 point 2 +*/ +inline void db_StitchCameraRotation_2Points(double R[9], + /*Image points have to be of unit norm + for the least squares to be meaningful*/ + double x1[3],double x2[3], + double xp1[3],double xp2[3]) +{ + double* x[2]; + double* xp[2]; + double scale,t[3]; + + x[0]=x1; + x[1]=x2; + xp[0]=xp1; + xp[1]=xp2; + db_StitchSimilarity3DRaw(&scale,R,t,xp,x,2,1,0,1,0); +} + +/*! +Solve for a homography H generated by a rotation R with a common unknown focal length f, i.e. +H=diag(f,f,1)*R*diag(1/f,1/f,1) such that xp~Hx. +If signed_disambiguation is true, the points are +required to be in front of the camera. No specific normalization of the homogenous points +is required, although it could be desirable to keep x1,x2,xp1 and xp2 of reasonable magnitude. +If a solution is obtained the function returns 1, otherwise 0. If the focal length is desired +a valid pointer should be passed in f +*/ +DB_API int db_StitchRotationCommonFocalLength_3Points(double H[9],double x1[3],double x2[3],double x3[3], + double xp1[3],double xp2[3],double xp3[3],double *f=0,int signed_disambiguation=1); + +/*! +Find scale, rotation and translation of the similarity that +takes the nr_points inhomogenous 2D points X to Xp, +i.e. for the homogenous equivalents +Xp and X we would have +\code +Xp~ +[sR t]*X +[0 1] +\endcode +If orientation_preserving is true, R is restricted such that det(R)>0. +allow_scaling, allow_rotation and allow_translation allow s,R and t +to differ from 1,Identity and 0 + +Full similarity takes the following on 550MHz: +\code +0.9 microseconds with 2 points +1.0 microseconds with 3 points +1.1 microseconds with 4 points +1.3 microseconds with 5 points +1.4 microseconds with 6 points +1.7 microseconds with 10 points +9 microseconds with 100 points +130 microseconds with 1000 points +1.3 milliseconds with 10000 points +35 milliseconds with 100000 points +350 milliseconds with 1000000 points +\endcode + +Without orientation_preserving: +\code +3 points is minimal for (s,R,t) (R,t) +2 points is minimal for (s,t) (s,R) (R) +1 point is minimal for (s) (t) +\endcode + +With orientation_preserving: +\code +2 points is minimal for (s,R,t) (R,t) (s,t) +1 point is minimal for (s,R) (R) (s) (t) +\endcode +\param scale (out) +\param R 2D rotation (out) +\param t 2D translation (out) +\param Xp (nr_points x 2) pointer to array of image points +\param X (nr_points x 2 ) pointer to array of image points +\param nr_points number of points +\param orientation_preserving +\param allow_scaling compute scale (if 0, scale=1) +\param allow_rotation compute rotation (if 0, R=[I]) +\param allow_translation compute translation (if 0 t = [0,0]') +*/ +DB_API void db_StitchSimilarity2DRaw(double *scale,double R[4],double t[2], + double **Xp,double **X,int nr_points,int orientation_preserving=1, + int allow_scaling=1,int allow_rotation=1,int allow_translation=1); +/*! +See db_StitchRotationCommonFocalLength_3Points(). +\param H Image similarity transformation (out) +\param Xp (nr_points x 2) pointer to array of image points +\param X (nr_points x 2) pointer to array of image points +\param nr_points number of points +\param orientation_preserving +\param allow_scaling compute scale (if 0, scale=1) +\param allow_rotation compute rotation (if 0, R=[I]) +\param allow_translation compute translation (if 0 t = [0,0]') +*/ +inline void db_StitchSimilarity2D(double H[9],double **Xp,double **X,int nr_points,int orientation_preserving=1, + int allow_scaling=1,int allow_rotation=1,int allow_translation=1) +{ + double s,R[4],t[2]; + + db_StitchSimilarity2DRaw(&s,R,t,Xp,X,nr_points,orientation_preserving, + allow_scaling,allow_rotation,allow_translation); + + H[0]=s*R[0]; H[1]=s*R[1]; H[2]=t[0]; + H[3]=s*R[2]; H[4]=s*R[3]; H[5]=t[1]; + db_Zero2(H+6); + H[8]=1.0; +} +/*\}*/ +#endif /* DB_IMAGE_HOMOGRAPHY */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_metrics.h b/jni_mosaic/feature_stab/db_vlvm/db_metrics.h new file mode 100644 index 0000000000000000000000000000000000000000..6b95458f1965a873af785c0945c4bc5703787bfd --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_metrics.h @@ -0,0 +1,408 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_metrics.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_METRICS +#define DB_METRICS + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +#include "db_utilities.h" +/*! + * \defgroup LMMetrics (LM) Metrics + */ +/*\{*/ + + + + +/*! +Compute function value fp and Jacobian J of robustifier given input value f*/ +inline void db_CauchyDerivative(double J[4],double fp[2],const double f[2],double one_over_scale2) +{ + double x2,y2,r,r2,r2s,one_over_r2,fu,r_fu,one_over_r_fu; + double one_plus_r2s,half_dfu_dx,half_dfu_dy,coeff,coeff2,coeff3; + int at_zero; + + /*The robustifier takes the input (x,y) and makes a new + vector (xp,yp) where + xp=sqrt(log(1+(x^2+y^2)*one_over_scale2))*x/sqrt(x^2+y^2) + yp=sqrt(log(1+(x^2+y^2)*one_over_scale2))*y/sqrt(x^2+y^2) + The new vector has the property + xp^2+yp^2=log(1+(x^2+y^2)*one_over_scale2) + i.e. when it is square-summed it gives the robust + reprojection error + Define + r2=(x^2+y^2) and + r2s=r2*one_over_scale2 + fu=log(1+r2s)/r2 + then + xp=sqrt(fu)*x + yp=sqrt(fu)*y + and + d(r2)/dx=2x + d(r2)/dy=2y + and + dfu/dx=d(r2)/dx*(r2s/(1+r2s)-log(1+r2s))/(r2*r2) + dfu/dy=d(r2)/dy*(r2s/(1+r2s)-log(1+r2s))/(r2*r2) + and + d(xp)/dx=1/(2sqrt(fu))*(dfu/dx)*x+sqrt(fu) + d(xp)/dy=1/(2sqrt(fu))*(dfu/dy)*x + d(yp)/dx=1/(2sqrt(fu))*(dfu/dx)*y + d(yp)/dy=1/(2sqrt(fu))*(dfu/dy)*y+sqrt(fu) + */ + + x2=db_sqr(f[0]); + y2=db_sqr(f[1]); + r2=x2+y2; + r=sqrt(r2); + + if(r2<=0.0) at_zero=1; + else + { + one_over_r2=1.0/r2; + r2s=r2*one_over_scale2; + one_plus_r2s=1.0+r2s; + fu=log(one_plus_r2s)*one_over_r2; + r_fu=sqrt(fu); + if(r_fu<=0.0) at_zero=1; + else + { + one_over_r_fu=1.0/r_fu; + fp[0]=r_fu*f[0]; + fp[1]=r_fu*f[1]; + /*r2s is always >= 0*/ + coeff=(r2s/one_plus_r2s*one_over_r2-fu)*one_over_r2; + half_dfu_dx=f[0]*coeff; + half_dfu_dy=f[1]*coeff; + coeff2=one_over_r_fu*half_dfu_dx; + coeff3=one_over_r_fu*half_dfu_dy; + + J[0]=coeff2*f[0]+r_fu; + J[1]=coeff3*f[0]; + J[2]=coeff2*f[1]; + J[3]=coeff3*f[1]+r_fu; + at_zero=0; + } + } + if(at_zero) + { + /*Close to zero the robustifying mapping + becomes identity*sqrt(one_over_scale2)*/ + fp[0]=0.0; + fp[1]=0.0; + J[0]=sqrt(one_over_scale2); + J[1]=0.0; + J[2]=0.0; + J[3]=J[0]; + } +} + +inline double db_SquaredReprojectionErrorHomography(const double y[2],const double H[9],const double x[3]) +{ + double x0,x1,x2,mult; + double sd; + + x0=H[0]*x[0]+H[1]*x[1]+H[2]*x[2]; + x1=H[3]*x[0]+H[4]*x[1]+H[5]*x[2]; + x2=H[6]*x[0]+H[7]*x[1]+H[8]*x[2]; + mult=1.0/((x2!=0.0)?x2:1.0); + sd=db_sqr((y[0]-x0*mult))+db_sqr((y[1]-x1*mult)); + + return(sd); +} + +inline double db_SquaredInhomogenousHomographyError(const double y[2],const double H[9],const double x[2]) +{ + double x0,x1,x2,mult; + double sd; + + x0=H[0]*x[0]+H[1]*x[1]+H[2]; + x1=H[3]*x[0]+H[4]*x[1]+H[5]; + x2=H[6]*x[0]+H[7]*x[1]+H[8]; + mult=1.0/((x2!=0.0)?x2:1.0); + sd=db_sqr((y[0]-x0*mult))+db_sqr((y[1]-x1*mult)); + + return(sd); +} + +/*! +Return a constant divided by likelihood of a Cauchy distributed +reprojection error given the image point y, homography H, image point +point x and the squared scale coefficient one_over_scale2=1.0/(scale*scale) +where scale is the half width at half maximum (hWahM) of the +Cauchy distribution*/ +inline double db_ExpCauchyInhomogenousHomographyError(const double y[2],const double H[9],const double x[2], + double one_over_scale2) +{ + double sd; + sd=db_SquaredInhomogenousHomographyError(y,H,x); + return(1.0+sd*one_over_scale2); +} + +/*! +Compute residual vector f between image point y and homography Hx of +image point x. Also compute Jacobian of f with respect +to an update dx of H*/ +inline void db_DerivativeInhomHomographyError(double Jf_dx[18],double f[2],const double y[2],const double H[9], + const double x[2]) +{ + double xh,yh,zh,mult,mult2,xh_mult2,yh_mult2; + /*The Jacobian of the inhomogenous coordinates with respect to + the homogenous is + [1/zh 0 -xh/(zh*zh)] + [ 0 1/zh -yh/(zh*zh)] + The Jacobian of the homogenous coordinates with respect to dH is + [x0 x1 1 0 0 0 0 0 0] + [ 0 0 0 x0 x1 1 0 0 0] + [ 0 0 0 0 0 0 x0 x1 1] + The output Jacobian is minus their product, i.e. + [-x0/zh -x1/zh -1/zh 0 0 0 x0*xh/(zh*zh) x1*xh/(zh*zh) xh/(zh*zh)] + [ 0 0 0 -x0/zh -x1/zh -1/zh x0*yh/(zh*zh) x1*yh/(zh*zh) yh/(zh*zh)]*/ + + /*Compute warped point, which is the same as + homogenous coordinates of reprojection*/ + xh=H[0]*x[0]+H[1]*x[1]+H[2]; + yh=H[3]*x[0]+H[4]*x[1]+H[5]; + zh=H[6]*x[0]+H[7]*x[1]+H[8]; + mult=1.0/((zh!=0.0)?zh:1.0); + /*Compute inhomogenous residual*/ + f[0]=y[0]-xh*mult; + f[1]=y[1]-yh*mult; + /*Compute Jacobian*/ + mult2=mult*mult; + xh_mult2=xh*mult2; + yh_mult2=yh*mult2; + Jf_dx[0]= -x[0]*mult; + Jf_dx[1]= -x[1]*mult; + Jf_dx[2]= -mult; + Jf_dx[3]=0; + Jf_dx[4]=0; + Jf_dx[5]=0; + Jf_dx[6]=x[0]*xh_mult2; + Jf_dx[7]=x[1]*xh_mult2; + Jf_dx[8]=xh_mult2; + Jf_dx[9]=0; + Jf_dx[10]=0; + Jf_dx[11]=0; + Jf_dx[12]=Jf_dx[0]; + Jf_dx[13]=Jf_dx[1]; + Jf_dx[14]=Jf_dx[2]; + Jf_dx[15]=x[0]*yh_mult2; + Jf_dx[16]=x[1]*yh_mult2; + Jf_dx[17]=yh_mult2; +} + +/*! +Compute robust residual vector f between image point y and homography Hx of +image point x. Also compute Jacobian of f with respect +to an update dH of H*/ +inline void db_DerivativeCauchyInhomHomographyReprojection(double Jf_dx[18],double f[2],const double y[2],const double H[9], + const double x[2],double one_over_scale2) +{ + double Jf_dx_loc[18],f_loc[2]; + double J[4],J0,J1,J2,J3; + + /*Compute reprojection Jacobian*/ + db_DerivativeInhomHomographyError(Jf_dx_loc,f_loc,y,H,x); + /*Compute robustifier Jacobian*/ + db_CauchyDerivative(J,f,f_loc,one_over_scale2); + + /*Multiply the robustifier Jacobian with + the reprojection Jacobian*/ + J0=J[0];J1=J[1];J2=J[2];J3=J[3]; + Jf_dx[0]=J0*Jf_dx_loc[0]; + Jf_dx[1]=J0*Jf_dx_loc[1]; + Jf_dx[2]=J0*Jf_dx_loc[2]; + Jf_dx[3]= J1*Jf_dx_loc[12]; + Jf_dx[4]= J1*Jf_dx_loc[13]; + Jf_dx[5]= J1*Jf_dx_loc[14]; + Jf_dx[6]=J0*Jf_dx_loc[6]+J1*Jf_dx_loc[15]; + Jf_dx[7]=J0*Jf_dx_loc[7]+J1*Jf_dx_loc[16]; + Jf_dx[8]=J0*Jf_dx_loc[8]+J1*Jf_dx_loc[17]; + Jf_dx[9]= J2*Jf_dx_loc[0]; + Jf_dx[10]=J2*Jf_dx_loc[1]; + Jf_dx[11]=J2*Jf_dx_loc[2]; + Jf_dx[12]= J3*Jf_dx_loc[12]; + Jf_dx[13]= J3*Jf_dx_loc[13]; + Jf_dx[14]= J3*Jf_dx_loc[14]; + Jf_dx[15]=J2*Jf_dx_loc[6]+J3*Jf_dx_loc[15]; + Jf_dx[16]=J2*Jf_dx_loc[7]+J3*Jf_dx_loc[16]; + Jf_dx[17]=J2*Jf_dx_loc[8]+J3*Jf_dx_loc[17]; +} +/*! +Compute residual vector f between image point y and rotation of +image point x by R. Also compute Jacobian of f with respect +to an update dx of R*/ +inline void db_DerivativeInhomRotationReprojection(double Jf_dx[6],double f[2],const double y[2],const double R[9], + const double x[2]) +{ + double xh,yh,zh,mult,mult2,xh_mult2,yh_mult2; + /*The Jacobian of the inhomogenous coordinates with respect to + the homogenous is + [1/zh 0 -xh/(zh*zh)] + [ 0 1/zh -yh/(zh*zh)] + The Jacobian at zero of the homogenous coordinates with respect to + [sin(phi) sin(ohm) sin(kap)] is + [-rx2 0 rx1 ] + [ 0 rx2 -rx0 ] + [ rx0 -rx1 0 ] + The output Jacobian is minus their product, i.e. + [1+xh*xh/(zh*zh) -xh*yh/(zh*zh) -yh/zh] + [xh*yh/(zh*zh) -1-yh*yh/(zh*zh) xh/zh]*/ + + /*Compute rotated point, which is the same as + homogenous coordinates of reprojection*/ + xh=R[0]*x[0]+R[1]*x[1]+R[2]; + yh=R[3]*x[0]+R[4]*x[1]+R[5]; + zh=R[6]*x[0]+R[7]*x[1]+R[8]; + mult=1.0/((zh!=0.0)?zh:1.0); + /*Compute inhomogenous residual*/ + f[0]=y[0]-xh*mult; + f[1]=y[1]-yh*mult; + /*Compute Jacobian*/ + mult2=mult*mult; + xh_mult2=xh*mult2; + yh_mult2=yh*mult2; + Jf_dx[0]= 1.0+xh*xh_mult2; + Jf_dx[1]= -yh*xh_mult2; + Jf_dx[2]= -yh*mult; + Jf_dx[3]= -Jf_dx[1]; + Jf_dx[4]= -1-yh*yh_mult2; + Jf_dx[5]= xh*mult; +} + +/*! +Compute robust residual vector f between image point y and rotation of +image point x by R. Also compute Jacobian of f with respect +to an update dx of R*/ +inline void db_DerivativeCauchyInhomRotationReprojection(double Jf_dx[6],double f[2],const double y[2],const double R[9], + const double x[2],double one_over_scale2) +{ + double Jf_dx_loc[6],f_loc[2]; + double J[4],J0,J1,J2,J3; + + /*Compute reprojection Jacobian*/ + db_DerivativeInhomRotationReprojection(Jf_dx_loc,f_loc,y,R,x); + /*Compute robustifier Jacobian*/ + db_CauchyDerivative(J,f,f_loc,one_over_scale2); + + /*Multiply the robustifier Jacobian with + the reprojection Jacobian*/ + J0=J[0];J1=J[1];J2=J[2];J3=J[3]; + Jf_dx[0]=J0*Jf_dx_loc[0]+J1*Jf_dx_loc[3]; + Jf_dx[1]=J0*Jf_dx_loc[1]+J1*Jf_dx_loc[4]; + Jf_dx[2]=J0*Jf_dx_loc[2]+J1*Jf_dx_loc[5]; + Jf_dx[3]=J2*Jf_dx_loc[0]+J3*Jf_dx_loc[3]; + Jf_dx[4]=J2*Jf_dx_loc[1]+J3*Jf_dx_loc[4]; + Jf_dx[5]=J2*Jf_dx_loc[2]+J3*Jf_dx_loc[5]; +} + + + +/*! +// remove the outliers whose projection error is larger than pre-defined +*/ +inline int db_RemoveOutliers_Homography(const double H[9], double *x_i,double *xp_i, double *wp,double *im, double *im_p, double *im_r, double *im_raw,double *im_raw_p,int point_count,double scale, double thresh=DB_OUTLIER_THRESHOLD) +{ + double temp_valueE, t2; + int c; + int k1=0; + int k2=0; + int k3=0; + int numinliers=0; + int ind1; + int ind2; + int ind3; + int isinlier; + + // experimentally determined + t2=1.0/(thresh*thresh*thresh*thresh); + + // count the inliers + for(c=0;c +using namespace std; +#endif /*VERBOSE*/ + +inline double db_RobImageHomography_Cost(double H[9],int point_count,double *x_i,double *xp_i,double one_over_scale2) +{ + int c; + double back,acc,*x_i_temp,*xp_i_temp; + + for(back=0.0,c=0;cnr_points=point_count; + stat->one_over_scale2=one_over_scale2; + stat->nr_inliers=i; + stat->inlier_fraction=frac; + + stat->cost=db_RobImageHomography_Cost(H,point_count,x_i,xp_i,one_over_scale2); + stat->model_dimension=0; + /*stat->nr_parameters=;*/ + + stat->lambda1=log(4.0); + stat->lambda2=log(4.0*((double)db_maxi(1,stat->nr_points))); + stat->lambda3=10.0; + stat->gric=stat->cost+stat->lambda1*stat->model_dimension*((double)stat->nr_points)+stat->lambda2*((double)stat->nr_parameters); + stat->inlier_evidence=((double)stat->nr_inliers)-stat->lambda3*((double)stat->nr_parameters); + } + + return(frac); +} + +/*Compute min_Jtf and upper right of JtJ. Return cost.*/ +inline double db_RobImageHomography_Jacobians(double JtJ[81],double min_Jtf[9],double H[9],int point_count,double *x_i,double *xp_i,double one_over_scale2) +{ + double back,Jf_dx[18],f[2],temp,temp2; + int i; + + db_Zero(JtJ,81); + db_Zero(min_Jtf,9); + for(back=0.0,i=0;i=sample_size) for(i=0;i=2) for(i=0;i=3) for(i=0;i=3) for(i=0;i=3) for(i=0;i=4) for(i=0;i0) && (inr_parameters=8; + break; + case DB_HOMOGRAPHY_TYPE_AFFINE: + if(stat) stat->nr_parameters=6; + break; + case DB_HOMOGRAPHY_TYPE_SIMILARITY: + case DB_HOMOGRAPHY_TYPE_SIMILARITY_U: + case DB_HOMOGRAPHY_TYPE_CAMROTATION_F: + case DB_HOMOGRAPHY_TYPE_CAMROTATION_F_UD: + if(stat) stat->nr_parameters=4; + break; + case DB_HOMOGRAPHY_TYPE_CAMROTATION: + if(stat) stat->nr_parameters=3; + break; + case DB_HOMOGRAPHY_TYPE_TRANSLATION: + case DB_HOMOGRAPHY_TYPE_S_T: + case DB_HOMOGRAPHY_TYPE_R_T: + case DB_HOMOGRAPHY_TYPE_R_S: + if(stat) stat->nr_parameters=2; + break; + case DB_HOMOGRAPHY_TYPE_ROTATION: + case DB_HOMOGRAPHY_TYPE_ROTATION_U: + case DB_HOMOGRAPHY_TYPE_SCALING: + if(stat) stat->nr_parameters=1; + break; + } + + db_RobImageHomography_Statistics(H_temp,db_mini(point_count,max_points),x_i,xp_i,one_over_scale2,stat); + + /*Put on the calibration matrices*/ + db_Multiply3x3_3x3(H_temp2,H_temp,K_inv); + db_Multiply3x3_3x3(H,Kp,H_temp2); + + if (finalNumE) + *finalNumE = point_count_new; + +} diff --git a/jni_mosaic/feature_stab/db_vlvm/db_rob_image_homography.h b/jni_mosaic/feature_stab/db_vlvm/db_rob_image_homography.h new file mode 100644 index 0000000000000000000000000000000000000000..59cde7daa80e70972f00f950f8a6432c962720bb --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_rob_image_homography.h @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_rob_image_homography.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_ROB_IMAGE_HOMOGRAPHY +#define DB_ROB_IMAGE_HOMOGRAPHY + +#include "db_utilities.h" +#include "db_robust.h" +#include "db_metrics.h" + +#include // for NULL + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMRobImageHomography (LM) Robust Image Homography + */ +/*\{*/ + +#define DB_HOMOGRAPHY_TYPE_DEFAULT 0 +#define DB_HOMOGRAPHY_TYPE_PROJECTIVE 0 +#define DB_HOMOGRAPHY_TYPE_AFFINE 1 +#define DB_HOMOGRAPHY_TYPE_SIMILARITY 2 +#define DB_HOMOGRAPHY_TYPE_SIMILARITY_U 3 +#define DB_HOMOGRAPHY_TYPE_TRANSLATION 4 +#define DB_HOMOGRAPHY_TYPE_ROTATION 5 +#define DB_HOMOGRAPHY_TYPE_ROTATION_U 6 +#define DB_HOMOGRAPHY_TYPE_SCALING 7 +#define DB_HOMOGRAPHY_TYPE_S_T 8 +#define DB_HOMOGRAPHY_TYPE_R_T 9 +#define DB_HOMOGRAPHY_TYPE_R_S 10 +#define DB_HOMOGRAPHY_TYPE_CAMROTATION 11 +#define DB_HOMOGRAPHY_TYPE_CAMROTATION_F 12 +#define DB_HOMOGRAPHY_TYPE_CAMROTATION_F_UD 13 + +/*! +Solve for homography H such that xp~Hx +\param H best homography + +2D point to 2D point constraints: + +\param im first image points +\param im_p second image points +\param nr_points number of points + +Calibration matrices: + +\param K first camera +\param Kp second camera + + Temporary space: + + \param temp_d pre-allocated space of size 12*nr_samples+10*nr_points doubles + \param temp_i pre-allocated space of size max(nr_samples,nr_points) ints + + Statistics for this estimation + + \param stat NULL - do not compute + + \param homography_type see DB_HOMOGRAPHY_TYPE_* definitions above + + Estimation parameters: + + \param max_iterations max number of polishing steps + \param max_points only use this many points + \param scale Cauchy scale coefficient (see db_ExpCauchyReprojectionError() ) + \param nr_samples number of times to compute a hypothesis + \param chunk_size size of cost chunks +*/ +DB_API void db_RobImageHomography( + /*Best homography*/ + double H[9], + /*2DPoint to 2DPoint constraints + Points are assumed to be given in + homogenous coordinates*/ + double *im,double *im_p, + /*Nr of points in total*/ + int nr_points, + /*Calibration matrices + used to normalize the points*/ + double K[9], + double Kp[9], + /*Pre-allocated space temp_d + should point to at least + 12*nr_samples+10*nr_points + allocated positions*/ + double *temp_d, + /*Pre-allocated space temp_i + should point to at least + max(nr_samples,nr_points) + allocated positions*/ + int *temp_i, + int homography_type=DB_HOMOGRAPHY_TYPE_DEFAULT, + db_Statistics *stat=NULL, + int max_iterations=DB_DEFAULT_MAX_ITERATIONS, + int max_points=DB_DEFAULT_MAX_POINTS, + double scale=DB_POINT_STANDARDDEV, + int nr_samples=DB_DEFAULT_NR_SAMPLES, + int chunk_size=DB_DEFAULT_CHUNK_SIZE, + /////////////////////////////////////////////////// + // flag for the outlier removal + int outlierremoveflagE = 0, + // if flag is 1, then the following variables + // need to input + /////////////////////////////////////////////////// + // 3D coordinates + double *wp=NULL, + // its corresponding stereo pair's points + double *im_r=NULL, + // raw image coordinates + double *im_raw=NULL, double *im_raw_p=NULL, + // final matches + int *final_NumE=0); + +DB_API double db_RobImageHomography_Cost(double H[9],int point_count,double *x_i, + double *xp_i,double one_over_scale2); + + +DB_API void db_RobCamRotation_Polish(double H[9],int point_count,double *x_i, + double *xp_i, double one_over_scale2, + int max_iterations=DB_DEFAULT_MAX_ITERATIONS, + double improvement_requirement=DB_DEFAULT_IMP_REQ); + + +DB_API void db_RobCamRotation_Polish_Generic(double H[9],int point_count,int homography_type, + double *x_i,double *xp_i,double one_over_scale2, + int max_iterations=DB_DEFAULT_MAX_ITERATIONS, + double improvement_requirement=DB_DEFAULT_IMP_REQ); + + +#endif /* DB_ROB_IMAGE_HOMOGRAPHY */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_robust.h b/jni_mosaic/feature_stab/db_vlvm/db_robust.h new file mode 100644 index 0000000000000000000000000000000000000000..be0794c6ee13557605ccbecea03bb9a794f30331 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_robust.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_robust.h,v 1.4 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_ROBUST +#define DB_ROBUST + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMRobust (LM) Robust Estimation + */ + +/*! + \struct db_Statistics + \ingroup LMRobust + \brief (LnM) Sampling problem statistics + \date Mon Sep 10 10:28:08 EDT 2007 + \par Copyright: 2007 Sarnoff Corporation. All Rights Reserved + */ + struct db_stat_struct + { + int nr_points; + int nr_inliers; + double inlier_fraction; + double cost; + double one_over_scale2; + double lambda1; + double lambda2; + double lambda3; + int nr_parameters; + int model_dimension; + double gric; + double inlier_evidence; + double posestd[6]; + double rotationvecCov[9]; + double translationvecCov[9]; + int posecov_inliercount; + int posecovready; + double median_reprojection_error; + }; + typedef db_stat_struct db_Statistics; + +#endif /* DB_ROBUST */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities.cpp b/jni_mosaic/feature_stab/db_vlvm/db_utilities.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce2093b0165839660efcf1a6b27de7b684caea36 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities.cpp,v 1.4 2011/06/17 14:03:31 mbansal Exp $ */ + +#include "db_utilities.h" +#include +#include + +float** db_SetupImageReferences_f(float *im,int w,int h) +{ + int i; + float **img; + assert(im); + img=new float* [h]; + for(i=0;i must be a 2D image array with the same image size +// ---> the size of the input and output images must be same +// +// ------------------------------------------------------------------------------------------------------------ ; +void db_CopyImage_u(unsigned char **d,const unsigned char * const *s, int w, int h, int over_allocation) +{ + int i; + + for (i=0;i(lut_x[j][i]); + //yd = static_cast(lut_y[j][i]); + xd = (unsigned int)(lut_x[j][i]); + yd = (unsigned int)(lut_y[j][i]); + if ( xd >= w || yd >= h || + xd < 0 || yd < 0) + dst[j][i] = 0; + else + dst[j][i] = src[yd][xd]; + } +} + +inline void db_WarpImageLutBilinear_u(const unsigned char * const * src, unsigned char ** dst, int w, int h, + const float * const * lut_x,const float * const* lut_y) +{ + assert(src && dst); + double xd=0.0, yd=0.0; + + for ( int i = 0; i < w; ++i ) + for ( int j = 0; j < h; ++j ) + { + xd = static_cast(lut_x[j][i]); + yd = static_cast(lut_y[j][i]); + if ( xd > w || yd > h || + xd < 0.0 || yd < 0.0) + dst[j][i] = 0; + else + dst[j][i] = db_BilinearInterpolation(yd, xd, src); + } +} + + +void db_WarpImageLut_u(const unsigned char * const * src, unsigned char ** dst, int w, int h, + const float * const * lut_x,const float * const * lut_y, int type) +{ + switch (type) + { + case DB_WARP_FAST: + db_WarpImageLutFast_u(src,dst,w,h,lut_x,lut_y); + break; + case DB_WARP_BILINEAR: + db_WarpImageLutBilinear_u(src,dst,w,h,lut_x,lut_y); + break; + default: + break; + } +} + + +void db_PrintDoubleVector(double *a,long size) +{ + printf("[ "); + for(long i=0;i +#endif + +#include + +#include +#include "db_utilities_constants.h" +/*! + * \defgroup LMBasicUtilities (LM) Utility Functions (basic math, linear algebra and array manipulations) + */ +/*\{*/ + +/*! + * Round double into int using fld and fistp instructions. + */ +inline int db_roundi (double x) { +#ifdef WIN32_ASM + int n; + __asm + { + fld x; + fistp n; + } + return n; +#else + return static_cast(floor(x+0.5)); +#endif +} + +/*! + * Square a double. + */ +inline double db_sqr(double a) +{ + return(a*a); +} + +/*! + * Square a long. + */ +inline long db_sqr(long a) +{ + return(a*a); +} + +/*! + * Square an int. + */ +inline long db_sqr(int a) +{ + return(a*a); +} + +/*! + * Maximum of two doubles. + */ +inline double db_maxd(double a,double b) +{ + if(b>a) return(b); + else return(a); +} +/*! + * Minumum of two doubles. + */ +inline double db_mind(double a,double b) +{ + if(ba) return(b); + else return(a); +} + +/*! + * Minimum of two numbers. + */ +inline int db_mini(int a,int b) +{ + if(ba) return(b); + else return(a); +} + +/*! + * Minimum of two numbers. + */ +inline long db_minl(long a,long b) +{ + if(b=0.0) return(1.0); + else return(-1.0); +} +/*! + * Absolute value. + */ +inline int db_absi(int a) +{ + if(a<0) return(-a); + else return(a); +} +/*! + * Absolute value. + */ +inline float db_absf(float a) +{ + if(a<0) return(-a); + else return(a); +} + +/*! + * Absolute value. + */ +inline double db_absd(double a) +{ + if(a<0) return(-a); + else return(a); +} + +/*! + * Reciprocal (1/a). Prevents divide by 0. + * \return 1/a if a != 0. 1.0 otherwise. + */ +inline double db_SafeReciprocal(double a) +{ + return((a!=0.0)?(1.0/a):1.0); +} + +/*! + * Division. Prevents divide by 0. + * \return a/b if b!=0. a otherwise. + */ +inline double db_SafeDivision(double a,double b) +{ + return((b!=0.0)?(a/b):a); +} + +/*! + * Square root. Prevents imaginary output. + * \return sqrt(a) if a > 0.0. 0.0 otherewise. + */ +inline double db_SafeSqrt(double a) +{ + return((a>=0.0)?(sqrt(a)):0.0); +} + +/*! + * Square root of a reciprocal. Prevents divide by 0 and imaginary output. + * \return sqrt(1/a) if a > 0.0. 1.0 otherewise. + */ +inline double db_SafeSqrtReciprocal(double a) +{ + return((a>0.0)?(sqrt(1.0/a)):1.0); +} +/*! + * Cube root. + */ +inline double db_CubRoot(double x) +{ + if(x>=0.0) return(pow(x,1.0/3.0)); + else return(-pow(-x,1.0/3.0)); +} +/*! + * Sum of squares of elements of x. + */ +inline double db_SquareSum3(const double x[3]) +{ + return(db_sqr(x[0])+db_sqr(x[1])+db_sqr(x[2])); +} +/*! + * Sum of squares of elements of x. + */ +inline double db_SquareSum7(double x[7]) +{ + return(db_sqr(x[0])+db_sqr(x[1])+db_sqr(x[2])+ + db_sqr(x[3])+db_sqr(x[4])+db_sqr(x[5])+ + db_sqr(x[6])); +} +/*! + * Sum of squares of elements of x. + */ +inline double db_SquareSum9(double x[9]) +{ + return(db_sqr(x[0])+db_sqr(x[1])+db_sqr(x[2])+ + db_sqr(x[3])+db_sqr(x[4])+db_sqr(x[5])+ + db_sqr(x[6])+db_sqr(x[7])+db_sqr(x[8])); +} +/*! + * Copy a vector. + * \param xd destination + * \param xs source + */ +void inline db_Copy3(double xd[3],const double xs[3]) +{ + xd[0]=xs[0];xd[1]=xs[1];xd[2]=xs[2]; +} +/*! + * Copy a vector. + * \param xd destination + * \param xs source + */ +void inline db_Copy6(double xd[6],const double xs[6]) +{ + xd[0]=xs[0];xd[1]=xs[1];xd[2]=xs[2]; + xd[3]=xs[3];xd[4]=xs[4];xd[5]=xs[5]; +} +/*! + * Copy a vector. + * \param xd destination + * \param xs source + */ +void inline db_Copy9(double xd[9],const double xs[9]) +{ + xd[0]=xs[0];xd[1]=xs[1];xd[2]=xs[2]; + xd[3]=xs[3];xd[4]=xs[4];xd[5]=xs[5]; + xd[6]=xs[6];xd[7]=xs[7];xd[8]=xs[8]; +} + +/*! + * Scalar product: Transpose(A)*B. + */ +inline double db_ScalarProduct4(const double A[4],const double B[4]) +{ + return(A[0]*B[0]+A[1]*B[1]+A[2]*B[2]+A[3]*B[3]); +} +/*! + * Scalar product: Transpose(A)*B. + */ +inline double db_ScalarProduct7(const double A[7],const double B[7]) +{ + return(A[0]*B[0]+A[1]*B[1]+A[2]*B[2]+ + A[3]*B[3]+A[4]*B[4]+A[5]*B[5]+ + A[6]*B[6]); +} +/*! + * Scalar product: Transpose(A)*B. + */ +inline double db_ScalarProduct9(const double A[9],const double B[9]) +{ + return(A[0]*B[0]+A[1]*B[1]+A[2]*B[2]+ + A[3]*B[3]+A[4]*B[4]+A[5]*B[5]+ + A[6]*B[6]+A[7]*B[7]+A[8]*B[8]); +} +/*! + * Vector addition: S=A+B. + */ +inline void db_AddVectors6(double S[6],const double A[6],const double B[6]) +{ + S[0]=A[0]+B[0]; S[1]=A[1]+B[1]; S[2]=A[2]+B[2]; S[3]=A[3]+B[3]; S[4]=A[4]+B[4]; + S[5]=A[5]+B[5]; +} +/*! + * Multiplication: C(3x1)=A(3x3)*B(3x1). + */ +inline void db_Multiply3x3_3x1(double y[3],const double A[9],const double x[3]) +{ + y[0]=A[0]*x[0]+A[1]*x[1]+A[2]*x[2]; + y[1]=A[3]*x[0]+A[4]*x[1]+A[5]*x[2]; + y[2]=A[6]*x[0]+A[7]*x[1]+A[8]*x[2]; +} +inline void db_Multiply3x3_3x3(double C[9], const double A[9],const double B[9]) +{ + C[0]=A[0]*B[0]+A[1]*B[3]+A[2]*B[6]; + C[1]=A[0]*B[1]+A[1]*B[4]+A[2]*B[7]; + C[2]=A[0]*B[2]+A[1]*B[5]+A[2]*B[8]; + + C[3]=A[3]*B[0]+A[4]*B[3]+A[5]*B[6]; + C[4]=A[3]*B[1]+A[4]*B[4]+A[5]*B[7]; + C[5]=A[3]*B[2]+A[4]*B[5]+A[5]*B[8]; + + C[6]=A[6]*B[0]+A[7]*B[3]+A[8]*B[6]; + C[7]=A[6]*B[1]+A[7]*B[4]+A[8]*B[7]; + C[8]=A[6]*B[2]+A[7]*B[5]+A[8]*B[8]; +} +/*! + * Multiplication: C(4x1)=A(4x4)*B(4x1). + */ +inline void db_Multiply4x4_4x1(double y[4],const double A[16],const double x[4]) +{ + y[0]=A[0]*x[0]+A[1]*x[1]+A[2]*x[2]+A[3]*x[3]; + y[1]=A[4]*x[0]+A[5]*x[1]+A[6]*x[2]+A[7]*x[3]; + y[2]=A[8]*x[0]+A[9]*x[1]+A[10]*x[2]+A[11]*x[3]; + y[3]=A[12]*x[0]+A[13]*x[1]+A[14]*x[2]+A[15]*x[3]; +} +/*! + * Scalar multiplication in place: A(3)=mult*A(3). + */ +inline void db_MultiplyScalar3(double *A,double mult) +{ + (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; +} + +/*! + * Scalar multiplication: A(3)=mult*B(3). + */ +inline void db_MultiplyScalarCopy3(double *A,const double *B,double mult) +{ + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; +} + +/*! + * Scalar multiplication: A(4)=mult*B(4). + */ +inline void db_MultiplyScalarCopy4(double *A,const double *B,double mult) +{ + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; +} +/*! + * Scalar multiplication: A(7)=mult*B(7). + */ +inline void db_MultiplyScalarCopy7(double *A,const double *B,double mult) +{ + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; +} +/*! + * Scalar multiplication: A(9)=mult*B(9). + */ +inline void db_MultiplyScalarCopy9(double *A,const double *B,double mult) +{ + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; + (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; (*A++)=(*B++)*mult; +} + +/*! + * \defgroup LMImageBasicUtilities (LM) Basic Image Utility Functions + + Images in db are simply 2D arrays of unsigned char or float types. + Only the very basic operations are supported: allocation/deallocation, +copying, simple pyramid construction and LUT warping. These images are used +by db_CornerDetector_u and db_Matcher_u. The db_Image class is an attempt +to wrap these images. It has not been tested well. + + */ +/*\{*/ +/*! + * Given a float image array, allocates and returns the set of row poiners. + * \param im image pointer + * \param w image width + * \param h image height + */ +DB_API float** db_SetupImageReferences_f(float *im,int w,int h); +/*! + * Allocate a float image. + * Note: for feature detection images must be overallocated by 256 bytes. + * \param w width + * \param h height + * \param over_allocation allocate this many extra bytes at the end + * \return row array pointer + */ +DB_API float** db_AllocImage_f(int w,int h,int over_allocation=256); +/*! + * Free a float image + * \param img row array pointer + * \param h image height (number of rows) + */ +DB_API void db_FreeImage_f(float **img,int h); +/*! + * Given an unsigned char image array, allocates and returns the set of row poiners. + * \param im image pointer + * \param w image width + * \param h image height + */ +DB_API unsigned char** db_SetupImageReferences_u(unsigned char *im,int w,int h); +/*! + * Allocate an unsigned char image. + * Note: for feature detection images must be overallocated by 256 bytes. + * \param w width + * \param h height + * \param over_allocation allocate this many extra bytes at the end + * \return row array pointer + */ +DB_API unsigned char** db_AllocImage_u(int w,int h,int over_allocation=256); +/*! + * Free an unsigned char image + * \param img row array pointer + * \param h image height (number of rows) + */ +DB_API void db_FreeImage_u(unsigned char **img,int h); + +/*! + Copy an image from s to d. Both s and d must be pre-allocated at of the same size. + Copy is done row by row. + \param s source + \param d destination + \param w width + \param h height + \param over_allocation copy this many bytes after the end of the last line + */ +DB_API void db_CopyImage_u(unsigned char **d,const unsigned char * const *s,int w,int h,int over_allocation=0); + +DB_API inline unsigned char db_BilinearInterpolation(double y, double x, const unsigned char * const * v) +{ + int floor_x=(int) x; + int floor_y=(int) y; + + int ceil_x=floor_x+1; + int ceil_y=floor_y+1; + + unsigned char f00 = v[floor_y][floor_x]; + unsigned char f01 = v[floor_y][ceil_x]; + unsigned char f10 = v[ceil_y][floor_x]; + unsigned char f11 = v[ceil_y][ceil_x]; + + double xl = x-floor_x; + double yl = y-floor_y; + + return (unsigned char)(f00*(1-yl)*(1-xl) + f10*yl*(1-xl) + f01*(1-yl)*xl + f11*yl*xl); +} +/*\}*/ +/*! + * \ingroup LMRotation + * Compute an incremental rotation matrix using the update dx=[sin(phi) sin(ohm) sin(kap)] + */ +inline void db_IncrementalRotationMatrix(double R[9],const double dx[3]) +{ + double sp,so,sk,om_sp2,om_so2,om_sk2,cp,co,ck,sp_so,cp_so; + + /*Store sines*/ + sp=dx[0]; so=dx[1]; sk=dx[2]; + om_sp2=1.0-sp*sp; + om_so2=1.0-so*so; + om_sk2=1.0-sk*sk; + /*Compute cosines*/ + cp=(om_sp2>=0.0)?sqrt(om_sp2):1.0; + co=(om_so2>=0.0)?sqrt(om_so2):1.0; + ck=(om_sk2>=0.0)?sqrt(om_sk2):1.0; + /*Compute matrix*/ + sp_so=sp*so; + cp_so=cp*so; + R[0]=sp_so*sk+cp*ck; R[1]=co*sk; R[2]=cp_so*sk-sp*ck; + R[3]=sp_so*ck-cp*sk; R[4]=co*ck; R[5]=cp_so*ck+sp*sk; + R[6]=sp*co; R[7]= -so; R[8]=cp*co; +} +/*! + * Zero out 2 vector in place. + */ +void inline db_Zero2(double x[2]) +{ + x[0]=x[1]=0; +} +/*! + * Zero out 3 vector in place. + */ +void inline db_Zero3(double x[3]) +{ + x[0]=x[1]=x[2]=0; +} +/*! + * Zero out 4 vector in place. + */ +void inline db_Zero4(double x[4]) +{ + x[0]=x[1]=x[2]=x[3]=0; +} +/*! + * Zero out 9 vector in place. + */ +void inline db_Zero9(double x[9]) +{ + x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=x[8]=0; +} + +#define DB_WARP_FAST 0 +#define DB_WARP_BILINEAR 1 + +/*! + * Perform a look-up table warp. + * The LUTs must be float images of the same size as source image. + * The source value x_s is determined from destination (x_d,y_d) through lut_x + * and y_s is determined from lut_y: + \code + x_s = lut_x[y_d][x_d]; + y_s = lut_y[y_d][x_d]; + \endcode + + * \param src source image + * \param dst destination image + * \param w width + * \param h height + * \param lut_x LUT for x + * \param lut_y LUT for y + * \param type warp type (DB_WARP_FAST or DB_WARP_BILINEAR) + */ +DB_API void db_WarpImageLut_u(const unsigned char * const * src,unsigned char ** dst, int w, int h, + const float * const * lut_x, const float * const * lut_y, int type=DB_WARP_BILINEAR); + +DB_API void db_PrintDoubleVector(double *a,long size); +DB_API void db_PrintDoubleMatrix(double *a,long rows,long cols); + +#include "db_utilities_constants.h" +#include "db_utilities_algebra.h" +#include "db_utilities_indexing.h" +#include "db_utilities_linalg.h" +#include "db_utilities_poly.h" +#include "db_utilities_geometry.h" +#include "db_utilities_random.h" +#include "db_utilities_rotation.h" +#include "db_utilities_camera.h" + +#define DB_INVALID (-1) + + +#endif /* DB_UTILITIES_H */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_algebra.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_algebra.h new file mode 100644 index 0000000000000000000000000000000000000000..2aedd74d54fa27658113d4b6bb78795cf92e595b --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_algebra.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_algebra.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_UTILITIES_ALGEBRA +#define DB_UTILITIES_ALGEBRA + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMAlgebra (LM) Algebra utilities + */ +/*\{*/ + +inline void db_HomogenousNormalize3(double *x) +{ + db_MultiplyScalar3(x,db_SafeSqrtReciprocal(db_SquareSum3(x))); +} + +/*\}*/ + +#endif /* DB_UTILITIES_ALGEBRA */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.cpp b/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dceba9b62ce6a06389400640ba9161b0fe419b8e --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_camera.cpp,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#include "db_utilities_camera.h" +#include "db_utilities.h" +#include + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +void db_Approx3DCalMat(double K[9],double Kinv[9],int im_width,int im_height,double f_correction,int field) +{ + double iw,ih,av_size,field_fact; + + if(field) field_fact=2.0; + else field_fact=1.0; + + iw=(double)im_width; + ih=(double)(im_height*field_fact); + av_size=(iw+ih)/2.0; + K[0]=f_correction*av_size; + K[1]=0; + K[2]=iw/2.0; + K[3]=0; + K[4]=f_correction*av_size/field_fact; + K[5]=ih/2.0/field_fact; + K[6]=0; + K[7]=0; + K[8]=1; + + db_InvertCalibrationMatrix(Kinv,K); +} diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.h new file mode 100644 index 0000000000000000000000000000000000000000..26ba4420a94d37398eadc14684f17aaef252db80 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_camera.h @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_camera.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_UTILITIES_CAMERA +#define DB_UTILITIES_CAMERA + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMCamera (LM) Camera Utilities + */ +/*\{*/ + +#include "db_utilities.h" + +#define DB_RADDISTMODE_BOUGEUT 4 +#define DB_RADDISTMODE_2NDORDER 5 +#define DB_RADDISTMODE_IDENTITY 6 + +/*! +Give reasonable guess of the calibration matrix for normalization purposes. +Use real K matrix when doing real geometry. +focal length = (w+h)/2.0*f_correction. +\param K calibration matrix (out) +\param Kinv inverse of K (out) +\param im_width image width +\param im_height image height +\param f_correction focal length correction factor +\param field set to 1 if this is a field image (fy = fx/2) +\return K(3x3) intrinsic calibration matrix +*/ +DB_API void db_Approx3DCalMat(double K[9],double Kinv[9],int im_width,int im_height,double f_correction=1.0,int field=0); + +/*! + Make a 2x2 identity matrix + */ +void inline db_Identity2x2(double A[4]) +{ + A[0]=1;A[1]=0; + A[2]=0;A[3]=1; +} +/*! + Make a 3x3 identity matrix + */ +void inline db_Identity3x3(double A[9]) +{ + A[0]=1;A[1]=0;A[2]=0; + A[3]=0;A[4]=1;A[5]=0; + A[6]=0;A[7]=0;A[8]=1; +} +/*! + Invert intrinsic calibration matrix K(3x3) + If fx or fy is 0, I is returned. + */ +void inline db_InvertCalibrationMatrix(double Kinv[9],const double K[9]) +{ + double a,b,c,d,e,f,ainv,dinv,adinv; + + a=K[0];b=K[1];c=K[2];d=K[4];e=K[5];f=K[8]; + if((a==0.0)||(d==0.0)) db_Identity3x3(Kinv); + else + { + Kinv[3]=0.0; + Kinv[6]=0.0; + Kinv[7]=0.0; + Kinv[8]=1.0; + + ainv=1.0/a; + dinv=1.0/d; + adinv=ainv*dinv; + Kinv[0]=f*ainv; + Kinv[1]= -b*f*adinv; + Kinv[2]=(b*e-c*d)*adinv; + Kinv[4]=f*dinv; + Kinv[5]= -e*dinv; + } +} +/*! + De-homogenize image point: xd(1:2) = xs(1:2)/xs(3). + If xs(3) is 0, xd will become 0 + \param xd destination point + \param xs source point + */ +void inline db_DeHomogenizeImagePoint(double xd[2],const double xs[3]) +{ + double temp,div; + + temp=xs[2]; + if(temp!=0) + { + div=1.0/temp; + xd[0]=xs[0]*div;xd[1]=xs[1]*div; + } + else + { + xd[0]=0.0;xd[1]=0.0; + } +} + + +/*! + Orthonormalize 3D rotation R + */ +inline void db_OrthonormalizeRotation(double R[9]) +{ + double s,mult; + /*Normalize first vector*/ + s=db_sqr(R[0])+db_sqr(R[1])+db_sqr(R[2]); + mult=sqrt(1.0/(s?s:1)); + R[0]*=mult; R[1]*=mult; R[2]*=mult; + /*Subtract scalar product from second vector*/ + s=R[0]*R[3]+R[1]*R[4]+R[2]*R[5]; + R[3]-=s*R[0]; R[4]-=s*R[1]; R[5]-=s*R[2]; + /*Normalize second vector*/ + s=db_sqr(R[3])+db_sqr(R[4])+db_sqr(R[5]); + mult=sqrt(1.0/(s?s:1)); + R[3]*=mult; R[4]*=mult; R[5]*=mult; + /*Get third vector by vector product*/ + R[6]=R[1]*R[5]-R[4]*R[2]; + R[7]=R[2]*R[3]-R[5]*R[0]; + R[8]=R[0]*R[4]-R[3]*R[1]; +} +/*! +Update a rotation with the update dx=[sin(phi) sin(ohm) sin(kap)] +*/ +inline void db_UpdateRotation(double R_p_dx[9],double R[9],const double dx[3]) +{ + double R_temp[9]; + /*Update rotation*/ + db_IncrementalRotationMatrix(R_temp,dx); + db_Multiply3x3_3x3(R_p_dx,R_temp,R); +} +/*! + Compute xp = Hx for inhomogenous image points. + */ +inline void db_ImageHomographyInhomogenous(double xp[2],const double H[9],const double x[2]) +{ + double x3,m; + + x3=H[6]*x[0]+H[7]*x[1]+H[8]; + if(x3!=0.0) + { + m=1.0/x3; + xp[0]=m*(H[0]*x[0]+H[1]*x[1]+H[2]); + xp[1]=m*(H[3]*x[0]+H[4]*x[1]+H[5]); + } + else + { + xp[0]=xp[1]=0.0; + } +} +inline double db_FocalFromCamRotFocalHomography(const double H[9]) +{ + double k1,k2; + + k1=db_sqr(H[2])+db_sqr(H[5]); + k2=db_sqr(H[6])+db_sqr(H[7]); + if(k1>=k2) + { + return(db_SafeSqrt(db_SafeDivision(k1,1.0-db_sqr(H[8])))); + } + else + { + return(db_SafeSqrt(db_SafeDivision(1.0-db_sqr(H[8]),k2))); + } +} + +inline double db_FocalAndRotFromCamRotFocalHomography(double R[9],const double H[9]) +{ + double back,fi; + + back=db_FocalFromCamRotFocalHomography(H); + fi=db_SafeReciprocal(back); + R[0]=H[0]; R[1]=H[1]; R[2]=fi*H[2]; + R[3]=H[3]; R[4]=H[4]; R[5]=fi*H[5]; + R[6]=back*H[6]; R[7]=back*H[7]; R[8]=H[8]; + return(back); +} +/*! +Compute Jacobian at zero of three coordinates dR*x with +respect to the update dR([sin(phi) sin(ohm) sin(kap)]) given x. + +The Jacobian at zero of the homogenous coordinates with respect to + [sin(phi) sin(ohm) sin(kap)] is +\code + [-rx2 0 rx1 ] + [ 0 rx2 -rx0 ] + [ rx0 -rx1 0 ]. +\endcode + +*/ +inline void db_JacobianOfRotatedPointStride(double J[9],const double x[3],int stride) +{ + /*The Jacobian at zero of the homogenous coordinates with respect to + [sin(phi) sin(ohm) sin(kap)] is + [-rx2 0 rx1 ] + [ 0 rx2 -rx0 ] + [ rx0 -rx1 0 ]*/ + + J[0]= -x[stride<<1]; + J[1]=0; + J[2]= x[stride]; + J[3]=0; + J[4]= x[stride<<1]; + J[5]= -x[0]; + J[6]= x[0]; + J[7]= -x[stride]; + J[8]=0; +} +/*! + Invert an affine (if possible) + \param Hinv inverted matrix + \param H input matrix + \return true if success and false if matrix is ill-conditioned (det < 1e-7) + */ +inline bool db_InvertAffineTransform(double Hinv[9],const double H[9]) +{ + double det=H[0]*H[4]-H[3]*H[1]; + if (det<1e-7) + { + db_Copy9(Hinv,H); + return false; + } + else + { + Hinv[0]=H[4]/det; + Hinv[1]=-H[1]/det; + Hinv[3]=-H[3]/det; + Hinv[4]=H[0]/det; + Hinv[2]= -Hinv[0]*H[2]-Hinv[1]*H[5]; + Hinv[5]= -Hinv[3]*H[2]-Hinv[4]*H[5]; + } + return true; +} + +/*! +Update of upper 2x2 is multiplication by +\code +[s 0][ cos(theta) sin(theta)] +[0 s][-sin(theta) cos(theta)] +\endcode +*/ +inline void db_MultiplyScaleOntoImageHomography(double H[9],double s) +{ + + H[0]*=s; + H[1]*=s; + H[3]*=s; + H[4]*=s; +} +/*! +Update of upper 2x2 is multiplication by +\code +[s 0][ cos(theta) sin(theta)] +[0 s][-sin(theta) cos(theta)] +\endcode +*/ +inline void db_MultiplyRotationOntoImageHomography(double H[9],double theta) +{ + double c,s,H0,H1; + + + c=cos(theta); + s=db_SafeSqrt(1.0-db_sqr(c)); + H0= c*H[0]+s*H[3]; + H[3]= -s*H[0]+c*H[3]; + H[0]=H0; + H1=c*H[1]+s*H[4]; + H[4]= -s*H[1]+c*H[4]; + H[1]=H1; +} + +inline void db_UpdateImageHomographyAffine(double H_p_dx[9],const double H[9],const double dx[6]) +{ + db_AddVectors6(H_p_dx,H,dx); + db_Copy3(H_p_dx+6,H+6); +} + +inline void db_UpdateImageHomographyProjective(double H_p_dx[9],const double H[9],const double dx[8],int frozen_coord) +{ + int i,j; + + for(j=0,i=0;i<9;i++) + { + if(i!=frozen_coord) + { + H_p_dx[i]=H[i]+dx[j]; + j++; + } + else H_p_dx[i]=H[i]; + } +} + +inline void db_UpdateRotFocalHomography(double H_p_dx[9],const double H[9],const double dx[4]) +{ + double f,fp,fpi; + double R[9],dR[9]; + + /*Updated matrix is diag(f+df,f+df)*dR*R*diag(1/(f+df),1/(f+df),1)*/ + f=db_FocalAndRotFromCamRotFocalHomography(R,H); + db_IncrementalRotationMatrix(dR,dx); + db_Multiply3x3_3x3(H_p_dx,dR,R); + fp=f+dx[3]; + fpi=db_SafeReciprocal(fp); + H_p_dx[2]*=fp; + H_p_dx[5]*=fp; + H_p_dx[6]*=fpi; + H_p_dx[7]*=fpi; +} + +/*\}*/ +#endif /* DB_UTILITIES_CAMERA */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_constants.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..07565efd212f203ae77c84c6d7651c14ceb82a73 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_constants.h @@ -0,0 +1,208 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_constants.h,v 1.2 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_UTILITIES_CONSTANTS +#define DB_UTILITIES_CONSTANTS + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +/****************Constants********************/ +#define DB_E 2.7182818284590452354 +#define DB_LOG2E 1.4426950408889634074 +#define DB_LOG10E 0.43429448190325182765 +#define DB_LN2 0.69314718055994530942 +#define DB_LN10 2.30258509299404568402 +#define DB_PI 3.1415926535897932384626433832795 +#define DB_PI_2 1.57079632679489661923 +#define DB_PI_4 0.78539816339744830962 +#define DB_1_PI 0.31830988618379067154 +#define DB_2_PI 0.63661977236758134308 +#define DB_SQRTPI 1.7724538509055160272981674833411 +#define DB_SQRT_2PI 2.506628274631000502415765284811 +#define DB_2_SQRTPI 1.12837916709551257390 +#define DB_SQRT2 1.41421356237309504880 +#define DB_SQRT3 1.7320508075688772935274463415059 +#define DB_SQRT1_2 0.70710678118654752440 +#define DB_EPS 2.220446049250313e-016 /* for 32 bit double */ + +/****************Default Parameters********************/ +/*Preemptive ransac parameters*/ +#define DB_DEFAULT_NR_SAMPLES 500 +#define DB_DEFAULT_CHUNK_SIZE 100 +#define DB_DEFAULT_GROUP_SIZE 10 + +/*Optimisation parameters*/ +#define DB_DEFAULT_MAX_POINTS 1000 +#define DB_DEFAULT_MAX_ITERATIONS 25 +#define DB_DEFAULT_IMP_REQ 0.001 + +/*Feature standard deviation parameters*/ +#define DB_POINT_STANDARDDEV (1.0/(826.0)) /*1 pixel for CIF (fraction of (image width+image height)/2)*/ +#define DB_OUTLIER_THRESHOLD 3.0 /*In number of DB_POINT_STANDARDDEV's*/ +#define DB_WORST_CASE 50.0 /*In number of DB_POINT_STANDARDDEV's*/ + +/*Front-end parameters*/ +#define DB_DEFAULT_TARGET_NR_CORNERS 5000 +#define DB_DEFAULT_NR_FEATURE_BLOCKS 10 +#define DB_DEFAULT_ABS_CORNER_THRESHOLD 50000000.0 +#define DB_DEFAULT_REL_CORNER_THRESHOLD 0.00005 +#define DB_DEFAULT_MAX_DISPARITY 0.1 +#define DB_DEFAULT_NO_DISPARITY -1.0 +#define DB_DEFAULT_MAX_TRACK_LENGTH 300 + +#define DB_DEFAULT_MAX_NR_CAMERAS 1000 + +#define DB_DEFAULT_TRIPLE_STEP 2 +#define DB_DEFAULT_DOUBLE_STEP 2 +#define DB_DEFAULT_SINGLE_STEP 1 +#define DB_DEFAULT_NR_SINGLES 10 +#define DB_DEFAULT_NR_DOUBLES 1 +#define DB_DEFAULT_NR_TRIPLES 1 + +#define DB_DEFAULT_TRIFOCAL_FOUR_STEPS 40 + +#define DB_DEFAULT_EPIPOLAR_ERROR 1 /*in pixels*/ + +////////////////////////// DOXYGEN ///////////////////// + +/*! + * \def DB_DEFAULT_GROUP_SIZE + * \ingroup LMRobust + * \brief Default group size for db_PreemptiveRansac class. + * Group size is the number of observation costs multiplied together + * before a log of the product is added to the total cost. +*/ + +/*! + * \def DB_DEFAULT_TARGET_NR_CORNERS + * \ingroup FeatureDetection + * \brief Default target number of corners +*/ +/*! + * \def DB_DEFAULT_NR_FEATURE_BLOCKS + * \ingroup FeatureDetection + * \brief Default number of regions (horizontal or vertical) that are considered separately + * for feature detection. The greater the number, the more uniform the distribution of + * detected features. +*/ +/*! + * \def DB_DEFAULT_ABS_CORNER_THRESHOLD + * \ingroup FeatureDetection + * \brief Absolute feature strength threshold. +*/ +/*! + * \def DB_DEFAULT_REL_CORNER_THRESHOLD + * \ingroup FeatureDetection + * \brief Relative feature strength threshold. +*/ +/*! + * \def DB_DEFAULT_MAX_DISPARITY + * \ingroup FeatureMatching + * \brief Maximum disparity (as fraction of image size) allowed in feature matching +*/ + /*! + * \def DB_DEFAULT_NO_DISPARITY + * \ingroup FeatureMatching + * \brief Indicates that vertical disparity is the same as horizontal disparity. +*/ +/////////////////////////////////////////////////////////////////////////////////// + /*! + * \def DB_E + * \ingroup LMBasicUtilities + * \brief e +*/ + /*! + * \def DB_LOG2E + * \ingroup LMBasicUtilities + * \brief log2(e) +*/ + /*! + * \def DB_LOG10E + * \ingroup LMBasicUtilities + * \brief log10(e) +*/ + /*! + * \def DB_LOG10E + * \ingroup LMBasicUtilities + * \brief log10(e) +*/ +/*! + * \def DB_LN2 + * \ingroup LMBasicUtilities + * \brief ln(2) +*/ +/*! + * \def DB_LN10 + * \ingroup LMBasicUtilities + * \brief ln(10) +*/ +/*! + * \def DB_PI + * \ingroup LMBasicUtilities + * \brief Pi +*/ +/*! + * \def DB_PI_2 + * \ingroup LMBasicUtilities + * \brief Pi/2 +*/ +/*! + * \def DB_PI_4 + * \ingroup LMBasicUtilities + * \brief Pi/4 +*/ +/*! + * \def DB_1_PI + * \ingroup LMBasicUtilities + * \brief 1/Pi +*/ +/*! + * \def DB_2_PI + * \ingroup LMBasicUtilities + * \brief 2/Pi +*/ +/*! + * \def DB_SQRTPI + * \ingroup LMBasicUtilities + * \brief sqrt(Pi) +*/ +/*! + * \def DB_SQRT_2PI + * \ingroup LMBasicUtilities + * \brief sqrt(2*Pi) +*/ +/*! + * \def DB_SQRT2 + * \ingroup LMBasicUtilities + * \brief sqrt(2) +*/ +/*! + * \def DB_SQRT3 + * \ingroup LMBasicUtilities + * \brief sqrt(3) +*/ +/*! + * \def DB_SQRT1_2 + * \ingroup LMBasicUtilities + * \brief sqrt(1/2) +*/ +#endif /* DB_UTILITIES_CONSTANTS */ + + diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_geometry.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_geometry.h new file mode 100644 index 0000000000000000000000000000000000000000..f215584673817e63c13f23d63e06b8c5ea256be7 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_geometry.h @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_geometry.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_UTILITIES_GEOMETRY_H +#define DB_UTILITIES_GEOMETRY_H + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! Get the inhomogenous 2D-point centroid of nr_point inhomogenous +points in X*/ +inline void db_PointCentroid2D(double c[2],const double *X,int nr_points) +{ + int i; + double cx,cy,m; + + cx=0;cy=0; + for(i=0;ipivot) *(d_top--)=temp; + } + *first_equal=d_bottom-dest; + *last_equal=d_top-dest; +} + +double db_LeanQuickSelect(const double *s,long nr_elements,long pos,double *temp) +{ + long first=0; + long last=nr_elements-1; + double pivot; + long first_equal,last_equal; + double *tempA; + double *tempB; + double *tempC; + const double *source; + double *dest; + + tempA=temp; + tempB=temp+nr_elements; + source=s; + dest=tempA; + + for(;last-first>2;) + { + pivot=db_TripleMedian(source[first],source[last],source[(first+last)/2]); + db_LeanPartitionOnPivot(pivot,dest,source,first,last,&first_equal,&last_equal); + + if(first_equal>pos) last=first_equal-1; + else if(last_equal=s[1]) return(0); + return(1); +} + +inline int db_MaxIndex3(const double s[3]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]>best){best=s[1];pos=1;} + if(s[2]>best){best=s[2];pos=2;} + return(pos); +} + +inline int db_MaxIndex4(const double s[4]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]>best){best=s[1];pos=1;} + if(s[2]>best){best=s[2];pos=2;} + if(s[3]>best){best=s[3];pos=3;} + return(pos); +} + +inline int db_MaxIndex5(const double s[5]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]>best){best=s[1];pos=1;} + if(s[2]>best){best=s[2];pos=2;} + if(s[3]>best){best=s[3];pos=3;} + if(s[4]>best){best=s[4];pos=4;} + return(pos); +} + +inline int db_MaxIndex6(const double s[6]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]>best){best=s[1];pos=1;} + if(s[2]>best){best=s[2];pos=2;} + if(s[3]>best){best=s[3];pos=3;} + if(s[4]>best){best=s[4];pos=4;} + if(s[5]>best){best=s[5];pos=5;} + return(pos); +} + +inline int db_MaxIndex7(const double s[7]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]>best){best=s[1];pos=1;} + if(s[2]>best){best=s[2];pos=2;} + if(s[3]>best){best=s[3];pos=3;} + if(s[4]>best){best=s[4];pos=4;} + if(s[5]>best){best=s[5];pos=5;} + if(s[6]>best){best=s[6];pos=6;} + return(pos); +} + +inline int db_MinIndex7(const double s[7]) +{ + double best; + int pos; + + best=s[0];pos=0; + if(s[1]best){best=t;pos=1;} + t=fabs(s[2]);if(t>best){pos=2;} + return(pos); +} + +inline int db_MaxAbsIndex9(const double *s) +{ + double t,best; + int pos; + + best=fabs(s[0]);pos=0; + t=fabs(s[1]);if(t>best){best=t;pos=1;} + t=fabs(s[2]);if(t>best){best=t;pos=2;} + t=fabs(s[3]);if(t>best){best=t;pos=3;} + t=fabs(s[4]);if(t>best){best=t;pos=4;} + t=fabs(s[5]);if(t>best){best=t;pos=5;} + t=fabs(s[6]);if(t>best){best=t;pos=6;} + t=fabs(s[7]);if(t>best){best=t;pos=7;} + t=fabs(s[8]);if(t>best){best=t;pos=8;} + return(pos); +} + + +/*! +Select ordinal pos (zero based) out of nr_elements in s. +temp should point to alloced memory of at least nr_elements*2 +Optimized runtimes on 450MHz: +\code + 30 with 3 microsecs + 100 with 11 microsecs + 300 with 30 microsecs + 500 with 40 microsecs +1000 with 100 microsecs +5000 with 540 microsecs +\endcode +so the expected runtime is around +(nr_elements/10) microseconds +The total quickselect cost of splitting 500 hypotheses recursively +is thus around 100 microseconds + +Does the same operation as std::nth_element(). +*/ +DB_API double db_LeanQuickSelect(const double *s,long nr_elements,long pos,double *temp); + +/*! + Median of 3 doubles + */ +inline double db_TripleMedian(double a,double b,double c) +{ + if(a>b) + { + if(c>a) return(a); + else if(c>b) return(c); + else return(b); + } + else + { + if(c>b) return(b); + else if(c>a) return(c); + else return(a); + } +} + +/*! +Align float pointer to nr_bytes by moving forward +*/ +DB_API float* db_AlignPointer_f(float *p,unsigned long nr_bytes); + +/*! +Align short pointer to nr_bytes by moving forward +*/ +DB_API short* db_AlignPointer_s(short *p,unsigned long nr_bytes); + +#endif /* DB_UTILITIES_INDEXING */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.cpp b/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f68b303aa79426d239e308661a0a5469ef3dfb8 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.cpp @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_linalg.cpp,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */ + +#include "db_utilities_linalg.h" +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +/*Cholesky-factorize symmetric positive definite 6 x 6 matrix A. Upper +part of A is used from the input. The Cholesky factor is output as +subdiagonal part in A and diagonal in d, which is 6-dimensional*/ +void db_CholeskyDecomp6x6(double A[36],double d[6]) +{ + double s,temp; + + /*[50 mult 35 add 6sqrt=85flops 6func]*/ + /*i=0*/ + s=A[0]; + d[0]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[0]); + A[6]=A[1]*temp; + A[12]=A[2]*temp; + A[18]=A[3]*temp; + A[24]=A[4]*temp; + A[30]=A[5]*temp; + /*i=1*/ + s=A[7]-A[6]*A[6]; + d[1]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[1]); + A[13]=(A[8]-A[6]*A[12])*temp; + A[19]=(A[9]-A[6]*A[18])*temp; + A[25]=(A[10]-A[6]*A[24])*temp; + A[31]=(A[11]-A[6]*A[30])*temp; + /*i=2*/ + s=A[14]-A[12]*A[12]-A[13]*A[13]; + d[2]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[2]); + A[20]=(A[15]-A[12]*A[18]-A[13]*A[19])*temp; + A[26]=(A[16]-A[12]*A[24]-A[13]*A[25])*temp; + A[32]=(A[17]-A[12]*A[30]-A[13]*A[31])*temp; + /*i=3*/ + s=A[21]-A[18]*A[18]-A[19]*A[19]-A[20]*A[20]; + d[3]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[3]); + A[27]=(A[22]-A[18]*A[24]-A[19]*A[25]-A[20]*A[26])*temp; + A[33]=(A[23]-A[18]*A[30]-A[19]*A[31]-A[20]*A[32])*temp; + /*i=4*/ + s=A[28]-A[24]*A[24]-A[25]*A[25]-A[26]*A[26]-A[27]*A[27]; + d[4]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[4]); + A[34]=(A[29]-A[24]*A[30]-A[25]*A[31]-A[26]*A[32]-A[27]*A[33])*temp; + /*i=5*/ + s=A[35]-A[30]*A[30]-A[31]*A[31]-A[32]*A[32]-A[33]*A[33]-A[34]*A[34]; + d[5]=((s>0.0)?sqrt(s):1.0); +} + +/*Cholesky-factorize symmetric positive definite n x n matrix A.Part +above diagonal of A is used from the input, diagonal of A is assumed to +be stored in d. The Cholesky factor is output as +subdiagonal part in A and diagonal in d, which is n-dimensional*/ +void db_CholeskyDecompSeparateDiagonal(double **A,double *d,int n) +{ + int i,j,k; + double s; + double temp = 0.0; + + for(i=0;i=0;k--) s-=A[i][k]*A[j][k]; + if(i==j) + { + d[i]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[i]); + } + else A[j][i]=s*temp; + } +} + +/*Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of an n x n matrix and the right hand side b. The vector b is unchanged*/ +void db_CholeskyBacksub(double *x,const double * const *A,const double *d,int n,const double *b) +{ + int i,k; + double s; + + for(i=0;i=0;k--) s-=A[i][k]*x[k]; + x[i]=db_SafeDivision(s,d[i]); + } + for(i=n-1;i>=0;i--) + { + for(s=x[i],k=i+1;k0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[0]); + A[3]=A[1]*temp; + A[6]=A[2]*temp; + /*i=1*/ + s=d[1]-A[3]*A[3]; + d[1]=((s>0.0)?sqrt(s):1.0); + temp=db_SafeReciprocal(d[1]); + A[7]=(A[5]-A[3]*A[6])*temp; + /*i=2*/ + s=d[2]-A[6]*A[6]-A[7]*A[7]; + d[2]=((s>0.0)?sqrt(s):1.0); +} + +/*Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of a 3 x 3 matrix and the right hand side b. The vector b is unchanged*/ +void db_CholeskyBacksub3x3(double x[3],const double A[9],const double d[3],const double b[3]) +{ + /*[42 mult 30 add=72flops]*/ + x[0]=db_SafeDivision(b[0],d[0]); + x[1]=db_SafeDivision((b[1]-A[3]*x[0]),d[1]); + x[2]=db_SafeDivision((b[2]-A[6]*x[0]-A[7]*x[1]),d[2]); + x[2]=db_SafeDivision(x[2],d[2]); + x[1]=db_SafeDivision((x[1]-A[7]*x[2]),d[1]); + x[0]=db_SafeDivision((x[0]-A[6]*x[2]-A[3]*x[1]),d[0]); +} + +/*Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of a 6 x 6 matrix and the right hand side b. The vector b is unchanged*/ +void db_CholeskyBacksub6x6(double x[6],const double A[36],const double d[6],const double b[6]) +{ + /*[42 mult 30 add=72flops]*/ + x[0]=db_SafeDivision(b[0],d[0]); + x[1]=db_SafeDivision((b[1]-A[6]*x[0]),d[1]); + x[2]=db_SafeDivision((b[2]-A[12]*x[0]-A[13]*x[1]),d[2]); + x[3]=db_SafeDivision((b[3]-A[18]*x[0]-A[19]*x[1]-A[20]*x[2]),d[3]); + x[4]=db_SafeDivision((b[4]-A[24]*x[0]-A[25]*x[1]-A[26]*x[2]-A[27]*x[3]),d[4]); + x[5]=db_SafeDivision((b[5]-A[30]*x[0]-A[31]*x[1]-A[32]*x[2]-A[33]*x[3]-A[34]*x[4]),d[5]); + x[5]=db_SafeDivision(x[5],d[5]); + x[4]=db_SafeDivision((x[4]-A[34]*x[5]),d[4]); + x[3]=db_SafeDivision((x[3]-A[33]*x[5]-A[27]*x[4]),d[3]); + x[2]=db_SafeDivision((x[2]-A[32]*x[5]-A[26]*x[4]-A[20]*x[3]),d[2]); + x[1]=db_SafeDivision((x[1]-A[31]*x[5]-A[25]*x[4]-A[19]*x[3]-A[13]*x[2]),d[1]); + x[0]=db_SafeDivision((x[0]-A[30]*x[5]-A[24]*x[4]-A[18]*x[3]-A[12]*x[2]-A[6]*x[1]),d[0]); +} + + +void db_Orthogonalize6x7(double A[42],int orthonormalize) +{ + int i; + double ss[6]; + + /*Compute square sums of rows*/ + ss[0]=db_SquareSum7(A); + ss[1]=db_SquareSum7(A+7); + ss[2]=db_SquareSum7(A+14); + ss[3]=db_SquareSum7(A+21); + ss[4]=db_SquareSum7(A+28); + ss[5]=db_SquareSum7(A+35); + + ss[1]-=db_OrthogonalizePair7(A+7 ,A,ss[0]); + ss[2]-=db_OrthogonalizePair7(A+14,A,ss[0]); + ss[3]-=db_OrthogonalizePair7(A+21,A,ss[0]); + ss[4]-=db_OrthogonalizePair7(A+28,A,ss[0]); + ss[5]-=db_OrthogonalizePair7(A+35,A,ss[0]); + + /*Pivot on largest ss (could also be done on ss/(original_ss))*/ + i=db_MaxIndex5(ss+1); + db_OrthogonalizationSwap7(A+7,i,ss+1); + + ss[2]-=db_OrthogonalizePair7(A+14,A+7,ss[1]); + ss[3]-=db_OrthogonalizePair7(A+21,A+7,ss[1]); + ss[4]-=db_OrthogonalizePair7(A+28,A+7,ss[1]); + ss[5]-=db_OrthogonalizePair7(A+35,A+7,ss[1]); + + i=db_MaxIndex4(ss+2); + db_OrthogonalizationSwap7(A+14,i,ss+2); + + ss[3]-=db_OrthogonalizePair7(A+21,A+14,ss[2]); + ss[4]-=db_OrthogonalizePair7(A+28,A+14,ss[2]); + ss[5]-=db_OrthogonalizePair7(A+35,A+14,ss[2]); + + i=db_MaxIndex3(ss+3); + db_OrthogonalizationSwap7(A+21,i,ss+3); + + ss[4]-=db_OrthogonalizePair7(A+28,A+21,ss[3]); + ss[5]-=db_OrthogonalizePair7(A+35,A+21,ss[3]); + + i=db_MaxIndex2(ss+4); + db_OrthogonalizationSwap7(A+28,i,ss+4); + + ss[5]-=db_OrthogonalizePair7(A+35,A+28,ss[4]); + + if(orthonormalize) + { + db_MultiplyScalar7(A ,db_SafeSqrtReciprocal(ss[0])); + db_MultiplyScalar7(A+7 ,db_SafeSqrtReciprocal(ss[1])); + db_MultiplyScalar7(A+14,db_SafeSqrtReciprocal(ss[2])); + db_MultiplyScalar7(A+21,db_SafeSqrtReciprocal(ss[3])); + db_MultiplyScalar7(A+28,db_SafeSqrtReciprocal(ss[4])); + db_MultiplyScalar7(A+35,db_SafeSqrtReciprocal(ss[5])); + } +} + +void db_Orthogonalize8x9(double A[72],int orthonormalize) +{ + int i; + double ss[8]; + + /*Compute square sums of rows*/ + ss[0]=db_SquareSum9(A); + ss[1]=db_SquareSum9(A+9); + ss[2]=db_SquareSum9(A+18); + ss[3]=db_SquareSum9(A+27); + ss[4]=db_SquareSum9(A+36); + ss[5]=db_SquareSum9(A+45); + ss[6]=db_SquareSum9(A+54); + ss[7]=db_SquareSum9(A+63); + + ss[1]-=db_OrthogonalizePair9(A+9 ,A,ss[0]); + ss[2]-=db_OrthogonalizePair9(A+18,A,ss[0]); + ss[3]-=db_OrthogonalizePair9(A+27,A,ss[0]); + ss[4]-=db_OrthogonalizePair9(A+36,A,ss[0]); + ss[5]-=db_OrthogonalizePair9(A+45,A,ss[0]); + ss[6]-=db_OrthogonalizePair9(A+54,A,ss[0]); + ss[7]-=db_OrthogonalizePair9(A+63,A,ss[0]); + + /*Pivot on largest ss (could also be done on ss/(original_ss))*/ + i=db_MaxIndex7(ss+1); + db_OrthogonalizationSwap9(A+9,i,ss+1); + + ss[2]-=db_OrthogonalizePair9(A+18,A+9,ss[1]); + ss[3]-=db_OrthogonalizePair9(A+27,A+9,ss[1]); + ss[4]-=db_OrthogonalizePair9(A+36,A+9,ss[1]); + ss[5]-=db_OrthogonalizePair9(A+45,A+9,ss[1]); + ss[6]-=db_OrthogonalizePair9(A+54,A+9,ss[1]); + ss[7]-=db_OrthogonalizePair9(A+63,A+9,ss[1]); + + i=db_MaxIndex6(ss+2); + db_OrthogonalizationSwap9(A+18,i,ss+2); + + ss[3]-=db_OrthogonalizePair9(A+27,A+18,ss[2]); + ss[4]-=db_OrthogonalizePair9(A+36,A+18,ss[2]); + ss[5]-=db_OrthogonalizePair9(A+45,A+18,ss[2]); + ss[6]-=db_OrthogonalizePair9(A+54,A+18,ss[2]); + ss[7]-=db_OrthogonalizePair9(A+63,A+18,ss[2]); + + i=db_MaxIndex5(ss+3); + db_OrthogonalizationSwap9(A+27,i,ss+3); + + ss[4]-=db_OrthogonalizePair9(A+36,A+27,ss[3]); + ss[5]-=db_OrthogonalizePair9(A+45,A+27,ss[3]); + ss[6]-=db_OrthogonalizePair9(A+54,A+27,ss[3]); + ss[7]-=db_OrthogonalizePair9(A+63,A+27,ss[3]); + + i=db_MaxIndex4(ss+4); + db_OrthogonalizationSwap9(A+36,i,ss+4); + + ss[5]-=db_OrthogonalizePair9(A+45,A+36,ss[4]); + ss[6]-=db_OrthogonalizePair9(A+54,A+36,ss[4]); + ss[7]-=db_OrthogonalizePair9(A+63,A+36,ss[4]); + + i=db_MaxIndex3(ss+5); + db_OrthogonalizationSwap9(A+45,i,ss+5); + + ss[6]-=db_OrthogonalizePair9(A+54,A+45,ss[5]); + ss[7]-=db_OrthogonalizePair9(A+63,A+45,ss[5]); + + i=db_MaxIndex2(ss+6); + db_OrthogonalizationSwap9(A+54,i,ss+6); + + ss[7]-=db_OrthogonalizePair9(A+63,A+54,ss[6]); + + if(orthonormalize) + { + db_MultiplyScalar9(A ,db_SafeSqrtReciprocal(ss[0])); + db_MultiplyScalar9(A+9 ,db_SafeSqrtReciprocal(ss[1])); + db_MultiplyScalar9(A+18,db_SafeSqrtReciprocal(ss[2])); + db_MultiplyScalar9(A+27,db_SafeSqrtReciprocal(ss[3])); + db_MultiplyScalar9(A+36,db_SafeSqrtReciprocal(ss[4])); + db_MultiplyScalar9(A+45,db_SafeSqrtReciprocal(ss[5])); + db_MultiplyScalar9(A+54,db_SafeSqrtReciprocal(ss[6])); + db_MultiplyScalar9(A+63,db_SafeSqrtReciprocal(ss[7])); + } +} + +void db_NullVectorOrthonormal6x7(double x[7],const double A[42]) +{ + int i; + double omss[7]; + const double *B; + + /*Pivot by choosing row of the identity matrix + (the one corresponding to column of A with smallest square sum)*/ + omss[0]=db_SquareSum6Stride7(A); + omss[1]=db_SquareSum6Stride7(A+1); + omss[2]=db_SquareSum6Stride7(A+2); + omss[3]=db_SquareSum6Stride7(A+3); + omss[4]=db_SquareSum6Stride7(A+4); + omss[5]=db_SquareSum6Stride7(A+5); + omss[6]=db_SquareSum6Stride7(A+6); + i=db_MinIndex7(omss); + /*orthogonalize that row against all previous rows + and normalize it*/ + B=A+i; + db_MultiplyScalarCopy7(x,A,-B[0]); + db_RowOperation7(x,A+7 ,B[7]); + db_RowOperation7(x,A+14,B[14]); + db_RowOperation7(x,A+21,B[21]); + db_RowOperation7(x,A+28,B[28]); + db_RowOperation7(x,A+35,B[35]); + x[i]+=1.0; + db_MultiplyScalar7(x,db_SafeSqrtReciprocal(1.0-omss[i])); +} + +void db_NullVectorOrthonormal8x9(double x[9],const double A[72]) +{ + int i; + double omss[9]; + const double *B; + + /*Pivot by choosing row of the identity matrix + (the one corresponding to column of A with smallest square sum)*/ + omss[0]=db_SquareSum8Stride9(A); + omss[1]=db_SquareSum8Stride9(A+1); + omss[2]=db_SquareSum8Stride9(A+2); + omss[3]=db_SquareSum8Stride9(A+3); + omss[4]=db_SquareSum8Stride9(A+4); + omss[5]=db_SquareSum8Stride9(A+5); + omss[6]=db_SquareSum8Stride9(A+6); + omss[7]=db_SquareSum8Stride9(A+7); + omss[8]=db_SquareSum8Stride9(A+8); + i=db_MinIndex9(omss); + /*orthogonalize that row against all previous rows + and normalize it*/ + B=A+i; + db_MultiplyScalarCopy9(x,A,-B[0]); + db_RowOperation9(x,A+9 ,B[9]); + db_RowOperation9(x,A+18,B[18]); + db_RowOperation9(x,A+27,B[27]); + db_RowOperation9(x,A+36,B[36]); + db_RowOperation9(x,A+45,B[45]); + db_RowOperation9(x,A+54,B[54]); + db_RowOperation9(x,A+63,B[63]); + x[i]+=1.0; + db_MultiplyScalar9(x,db_SafeSqrtReciprocal(1.0-omss[i])); +} + diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.h new file mode 100644 index 0000000000000000000000000000000000000000..1f63d4e57a0454ce90e102ac0d5ea6e342a02b76 --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_linalg.h @@ -0,0 +1,802 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_linalg.h,v 1.5 2011/06/17 14:03:31 mbansal Exp $ */ + +#ifndef DB_UTILITIES_LINALG +#define DB_UTILITIES_LINALG + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMLinAlg (LM) Linear Algebra Utilities (QR factorization, orthogonal basis, etc.) + */ + +/*! + \ingroup LMBasicUtilities + */ +inline void db_MultiplyScalar6(double A[6],double mult) +{ + (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; + (*A++) *= mult; +} +/*! + \ingroup LMBasicUtilities + */ +inline void db_MultiplyScalar7(double A[7],double mult) +{ + (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; + (*A++) *= mult; (*A++) *= mult; +} +/*! + \ingroup LMBasicUtilities + */ +inline void db_MultiplyScalar9(double A[9],double mult) +{ + (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; + (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; (*A++) *= mult; +} + +/*! + \ingroup LMBasicUtilities + */ +inline double db_SquareSum6Stride7(const double *x) +{ + return(db_sqr(x[0])+db_sqr(x[7])+db_sqr(x[14])+ + db_sqr(x[21])+db_sqr(x[28])+db_sqr(x[35])); +} + +/*! + \ingroup LMBasicUtilities + */ +inline double db_SquareSum8Stride9(const double *x) +{ + return(db_sqr(x[0])+db_sqr(x[9])+db_sqr(x[18])+ + db_sqr(x[27])+db_sqr(x[36])+db_sqr(x[45])+ + db_sqr(x[54])+db_sqr(x[63])); +} + +/*! + \ingroup LMLinAlg + Cholesky-factorize symmetric positive definite 6 x 6 matrix A. Upper +part of A is used from the input. The Cholesky factor is output as +subdiagonal part in A and diagonal in d, which is 6-dimensional +1.9 microseconds on 450MHz*/ +DB_API void db_CholeskyDecomp6x6(double A[36],double d[6]); + +/*! + \ingroup LMLinAlg + Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of a 6 x 6 matrix and the right hand side b. The vector b is unchanged +1.3 microseconds on 450MHz*/ +DB_API void db_CholeskyBacksub6x6(double x[6],const double A[36],const double d[6],const double b[6]); + +/*! + \ingroup LMLinAlg + Cholesky-factorize symmetric positive definite n x n matrix A.Part +above diagonal of A is used from the input, diagonal of A is assumed to +be stored in d. The Cholesky factor is output as +subdiagonal part in A and diagonal in d, which is n-dimensional*/ +DB_API void db_CholeskyDecompSeparateDiagonal(double **A,double *d,int n); + +/*! + \ingroup LMLinAlg + Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of an n x n matrix and the right hand side b. The vector b is unchanged*/ +DB_API void db_CholeskyBacksub(double *x,const double * const *A,const double *d,int n,const double *b); + +/*! + \ingroup LMLinAlg + Cholesky-factorize symmetric positive definite 3 x 3 matrix A. Part +above diagonal of A is used from the input, diagonal of A is assumed to +be stored in d. The Cholesky factor is output as subdiagonal part in A +and diagonal in d, which is 3-dimensional*/ +DB_API void db_CholeskyDecomp3x3SeparateDiagonal(double A[9],double d[3]); + +/*! + \ingroup LMLinAlg + Backsubstitute L%transpose(L)*x=b for x given the Cholesky decomposition +of a 3 x 3 matrix and the right hand side b. The vector b is unchanged*/ +DB_API void db_CholeskyBacksub3x3(double x[3],const double A[9],const double d[3],const double b[3]); + +/*! + \ingroup LMLinAlg + perform A-=B*mult*/ +inline void db_RowOperation3(double A[3],const double B[3],double mult) +{ + *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); +} + +/*! + \ingroup LMLinAlg + */ +inline void db_RowOperation7(double A[7],const double B[7],double mult) +{ + *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); + *A++ -= mult*(*B++); *A++ -= mult*(*B++); +} + +/*! + \ingroup LMLinAlg + */ +inline void db_RowOperation9(double A[9],const double B[9],double mult) +{ + *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); + *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); *A++ -= mult*(*B++); +} + +/*! + \ingroup LMBasicUtilities + Swap values of A[7] and B[7] + */ +inline void db_Swap7(double A[7],double B[7]) +{ + double temp; + temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; + temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; + temp= *A; *A++ = *B; *B++ =temp; +} + +/*! + \ingroup LMBasicUtilities + Swap values of A[9] and B[9] + */ +inline void db_Swap9(double A[9],double B[9]) +{ + double temp; + temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; + temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; + temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; temp= *A; *A++ = *B; *B++ =temp; +} + + +/*! + \ingroup LMLinAlg + */ +DB_API void db_Orthogonalize6x7(double A[42],int orthonormalize=0); + +/*! + \ingroup LMLinAlg + */ +DB_API void db_Orthogonalize8x9(double A[72],int orthonormalize=0); + +/*! + \ingroup LMLinAlg + */ +inline double db_OrthogonalizePair7(double *x,const double *v,double ssv) +{ + double m,sp,sp_m; + + m=db_SafeReciprocal(ssv); + sp=db_ScalarProduct7(x,v); + sp_m=sp*m; + db_RowOperation7(x,v,sp_m); + return(sp*sp_m); +} + +/*! + \ingroup LMLinAlg + */ +inline double db_OrthogonalizePair9(double *x,const double *v,double ssv) +{ + double m,sp,sp_m; + + m=db_SafeReciprocal(ssv); + sp=db_ScalarProduct9(x,v); + sp_m=sp*m; + db_RowOperation9(x,v,sp_m); + return(sp*sp_m); +} + +/*! + \ingroup LMLinAlg + */ +inline void db_OrthogonalizationSwap7(double *A,int i,double *ss) +{ + double temp; + + db_Swap7(A,A+7*i); + temp=ss[0]; ss[0]=ss[i]; ss[i]=temp; +} +/*! + \ingroup LMLinAlg + */ +inline void db_OrthogonalizationSwap9(double *A,int i,double *ss) +{ + double temp; + + db_Swap9(A,A+9*i); + temp=ss[0]; ss[0]=ss[i]; ss[i]=temp; +} + +/*! + \ingroup LMLinAlg + */ +DB_API void db_NullVectorOrthonormal6x7(double x[7],const double A[42]); +/*! + \ingroup LMLinAlg + */ +DB_API void db_NullVectorOrthonormal8x9(double x[9],const double A[72]); + +/*! + \ingroup LMLinAlg + */ +inline void db_NullVector6x7Destructive(double x[7],double A[42]) +{ + db_Orthogonalize6x7(A,1); + db_NullVectorOrthonormal6x7(x,A); +} + +/*! + \ingroup LMLinAlg + */ +inline void db_NullVector8x9Destructive(double x[9],double A[72]) +{ + db_Orthogonalize8x9(A,1); + db_NullVectorOrthonormal8x9(x,A); +} + +inline int db_ScalarProduct512_s(const short *f,const short *g) +{ +#ifndef DB_USE_MMX + int back; + back=0; + for(int i=1; i<=512; i++) + back+=(*f++)*(*g++); + + return(back); +#endif +} + + +inline int db_ScalarProduct32_s(const short *f,const short *g) +{ +#ifndef DB_USE_MMX + int back; + back=0; + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + return(back); +#endif +} + +/*! + \ingroup LMLinAlg + Scalar product of 128-vectors (short) + Compile-time control: MMX, SSE2 or standard C + */ +inline int db_ScalarProduct128_s(const short *f,const short *g) +{ +#ifndef DB_USE_MMX + int back; + back=0; + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + return(back); +#else +#ifdef DB_USE_SSE2 + int back; + + _asm + { + mov eax,f + mov ecx,g + /*First iteration************************************/ + movdqa xmm0,[eax] + pxor xmm7,xmm7 /*Set xmm7 to zero*/ + pmaddwd xmm0,[ecx] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm2,[eax+16] + paddd xmm7,xmm0 + pmaddwd xmm2,[ecx+16] + /*Stall*/ + movdqa xmm1,[eax+32] + paddd xmm7,xmm2 + pmaddwd xmm1,[ecx+32] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm0,[eax+48] + paddd xmm7,xmm1 + pmaddwd xmm0,[ecx+48] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm2,[eax+64] + paddd xmm7,xmm0 + pmaddwd xmm2,[ecx+64] + /*Stall*/ + movdqa xmm1,[eax+80] + paddd xmm7,xmm2 + pmaddwd xmm1,[ecx+80] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm0,[eax+96] + paddd xmm7,xmm1 + pmaddwd xmm0,[ecx+96] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm2,[eax+112] + paddd xmm7,xmm0 + pmaddwd xmm2,[ecx+112] + /*Stall*/ + movdqa xmm1,[eax+128] + paddd xmm7,xmm2 + pmaddwd xmm1,[ecx+128] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm0,[eax+144] + paddd xmm7,xmm1 + pmaddwd xmm0,[ecx+144] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm2,[eax+160] + paddd xmm7,xmm0 + pmaddwd xmm2,[ecx+160] + /*Stall*/ + movdqa xmm1,[eax+176] + paddd xmm7,xmm2 + pmaddwd xmm1,[ecx+176] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm0,[eax+192] + paddd xmm7,xmm1 + pmaddwd xmm0,[ecx+192] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm2,[eax+208] + paddd xmm7,xmm0 + pmaddwd xmm2,[ecx+208] + /*Stall*/ + movdqa xmm1,[eax+224] + paddd xmm7,xmm2 + pmaddwd xmm1,[ecx+224] + /*Stall*/ + /*Standard iteration************************************/ + movdqa xmm0,[eax+240] + paddd xmm7,xmm1 + pmaddwd xmm0,[ecx+240] + /*Stall*/ + /*Rest iteration************************************/ + paddd xmm7,xmm0 + + /* add up the sum squares */ + movhlps xmm0,xmm7 /* high half to low half */ + paddd xmm7,xmm0 /* add high to low */ + pshuflw xmm0,xmm7, 0xE /* reshuffle */ + paddd xmm7,xmm0 /* add remaining */ + movd back,xmm7 + + emms + } + + return(back); +#else + int back; + + _asm + { + mov eax,f + mov ecx,g + /*First iteration************************************/ + movq mm0,[eax] + pxor mm7,mm7 /*Set mm7 to zero*/ + pmaddwd mm0,[ecx] + /*Stall*/ + movq mm1,[eax+8] + /*Stall*/ + pmaddwd mm1,[ecx+8] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+16] + paddd mm7,mm0 + pmaddwd mm2,[ecx+16] + /*Stall*/ + movq mm0,[eax+24] + paddd mm7,mm1 + pmaddwd mm0,[ecx+24] + /*Stall*/ + movq mm1,[eax+32] + paddd mm7,mm2 + pmaddwd mm1,[ecx+32] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+40] + paddd mm7,mm0 + pmaddwd mm2,[ecx+40] + /*Stall*/ + movq mm0,[eax+48] + paddd mm7,mm1 + pmaddwd mm0,[ecx+48] + /*Stall*/ + movq mm1,[eax+56] + paddd mm7,mm2 + pmaddwd mm1,[ecx+56] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+64] + paddd mm7,mm0 + pmaddwd mm2,[ecx+64] + /*Stall*/ + movq mm0,[eax+72] + paddd mm7,mm1 + pmaddwd mm0,[ecx+72] + /*Stall*/ + movq mm1,[eax+80] + paddd mm7,mm2 + pmaddwd mm1,[ecx+80] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+88] + paddd mm7,mm0 + pmaddwd mm2,[ecx+88] + /*Stall*/ + movq mm0,[eax+96] + paddd mm7,mm1 + pmaddwd mm0,[ecx+96] + /*Stall*/ + movq mm1,[eax+104] + paddd mm7,mm2 + pmaddwd mm1,[ecx+104] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+112] + paddd mm7,mm0 + pmaddwd mm2,[ecx+112] + /*Stall*/ + movq mm0,[eax+120] + paddd mm7,mm1 + pmaddwd mm0,[ecx+120] + /*Stall*/ + movq mm1,[eax+128] + paddd mm7,mm2 + pmaddwd mm1,[ecx+128] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+136] + paddd mm7,mm0 + pmaddwd mm2,[ecx+136] + /*Stall*/ + movq mm0,[eax+144] + paddd mm7,mm1 + pmaddwd mm0,[ecx+144] + /*Stall*/ + movq mm1,[eax+152] + paddd mm7,mm2 + pmaddwd mm1,[ecx+152] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+160] + paddd mm7,mm0 + pmaddwd mm2,[ecx+160] + /*Stall*/ + movq mm0,[eax+168] + paddd mm7,mm1 + pmaddwd mm0,[ecx+168] + /*Stall*/ + movq mm1,[eax+176] + paddd mm7,mm2 + pmaddwd mm1,[ecx+176] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+184] + paddd mm7,mm0 + pmaddwd mm2,[ecx+184] + /*Stall*/ + movq mm0,[eax+192] + paddd mm7,mm1 + pmaddwd mm0,[ecx+192] + /*Stall*/ + movq mm1,[eax+200] + paddd mm7,mm2 + pmaddwd mm1,[ecx+200] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+208] + paddd mm7,mm0 + pmaddwd mm2,[ecx+208] + /*Stall*/ + movq mm0,[eax+216] + paddd mm7,mm1 + pmaddwd mm0,[ecx+216] + /*Stall*/ + movq mm1,[eax+224] + paddd mm7,mm2 + pmaddwd mm1,[ecx+224] + /*Stall*/ + /*Standard iteration************************************/ + movq mm2,[eax+232] + paddd mm7,mm0 + pmaddwd mm2,[ecx+232] + /*Stall*/ + movq mm0,[eax+240] + paddd mm7,mm1 + pmaddwd mm0,[ecx+240] + /*Stall*/ + movq mm1,[eax+248] + paddd mm7,mm2 + pmaddwd mm1,[ecx+248] + /*Stall*/ + /*Rest iteration************************************/ + paddd mm7,mm0 + /*Stall*/ + /*Stall*/ + /*Stall*/ + paddd mm7,mm1 + /*Stall*/ + movq mm0,mm7 + psrlq mm7,32 + paddd mm0,mm7 + /*Stall*/ + /*Stall*/ + /*Stall*/ + movd back,mm0 + emms + } + + return(back); +#endif +#endif /*DB_USE_MMX*/ +} + +/*! + \ingroup LMLinAlg + Scalar product of 16 byte aligned 128-vectors (float). + Compile-time control: SIMD (SSE) or standard C. + */ +inline float db_ScalarProduct128Aligned16_f(const float *f,const float *g) +{ +#ifndef DB_USE_SIMD + float back; + back=0.0; + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + back+=(*f++)*(*g++); back+=(*f++)*(*g++); back+=(*f++)*(*g++); + + return(back); +#else + float back; + + _asm + { + mov eax,f + mov ecx,g + /*First iteration************************************/ + movaps xmm0,[eax] + xorps xmm7,xmm7 /*Set mm7 to zero*/ + mulps xmm0,[ecx] + /*Stall*/ + movaps xmm1,[eax+16] + /*Stall*/ + mulps xmm1,[ecx+16] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+32] + addps xmm7,xmm0 + mulps xmm2,[ecx+32] + /*Stall*/ + movaps xmm0,[eax+48] + addps xmm7,xmm1 + mulps xmm0,[ecx+48] + /*Stall*/ + movaps xmm1,[eax+64] + addps xmm7,xmm2 + mulps xmm1,[ecx+64] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+80] + addps xmm7,xmm0 + mulps xmm2,[ecx+80] + /*Stall*/ + movaps xmm0,[eax+96] + addps xmm7,xmm1 + mulps xmm0,[ecx+96] + /*Stall*/ + movaps xmm1,[eax+112] + addps xmm7,xmm2 + mulps xmm1,[ecx+112] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+128] + addps xmm7,xmm0 + mulps xmm2,[ecx+128] + /*Stall*/ + movaps xmm0,[eax+144] + addps xmm7,xmm1 + mulps xmm0,[ecx+144] + /*Stall*/ + movaps xmm1,[eax+160] + addps xmm7,xmm2 + mulps xmm1,[ecx+160] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+176] + addps xmm7,xmm0 + mulps xmm2,[ecx+176] + /*Stall*/ + movaps xmm0,[eax+192] + addps xmm7,xmm1 + mulps xmm0,[ecx+192] + /*Stall*/ + movaps xmm1,[eax+208] + addps xmm7,xmm2 + mulps xmm1,[ecx+208] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+224] + addps xmm7,xmm0 + mulps xmm2,[ecx+224] + /*Stall*/ + movaps xmm0,[eax+240] + addps xmm7,xmm1 + mulps xmm0,[ecx+240] + /*Stall*/ + movaps xmm1,[eax+256] + addps xmm7,xmm2 + mulps xmm1,[ecx+256] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+272] + addps xmm7,xmm0 + mulps xmm2,[ecx+272] + /*Stall*/ + movaps xmm0,[eax+288] + addps xmm7,xmm1 + mulps xmm0,[ecx+288] + /*Stall*/ + movaps xmm1,[eax+304] + addps xmm7,xmm2 + mulps xmm1,[ecx+304] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+320] + addps xmm7,xmm0 + mulps xmm2,[ecx+320] + /*Stall*/ + movaps xmm0,[eax+336] + addps xmm7,xmm1 + mulps xmm0,[ecx+336] + /*Stall*/ + movaps xmm1,[eax+352] + addps xmm7,xmm2 + mulps xmm1,[ecx+352] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+368] + addps xmm7,xmm0 + mulps xmm2,[ecx+368] + /*Stall*/ + movaps xmm0,[eax+384] + addps xmm7,xmm1 + mulps xmm0,[ecx+384] + /*Stall*/ + movaps xmm1,[eax+400] + addps xmm7,xmm2 + mulps xmm1,[ecx+400] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+416] + addps xmm7,xmm0 + mulps xmm2,[ecx+416] + /*Stall*/ + movaps xmm0,[eax+432] + addps xmm7,xmm1 + mulps xmm0,[ecx+432] + /*Stall*/ + movaps xmm1,[eax+448] + addps xmm7,xmm2 + mulps xmm1,[ecx+448] + /*Stall*/ + /*Standard iteration************************************/ + movaps xmm2,[eax+464] + addps xmm7,xmm0 + mulps xmm2,[ecx+464] + /*Stall*/ + movaps xmm0,[eax+480] + addps xmm7,xmm1 + mulps xmm0,[ecx+480] + /*Stall*/ + movaps xmm1,[eax+496] + addps xmm7,xmm2 + mulps xmm1,[ecx+496] + /*Stall*/ + /*Rest iteration************************************/ + addps xmm7,xmm0 + /*Stall*/ + addps xmm7,xmm1 + /*Stall*/ + movaps xmm6,xmm7 + /*Stall*/ + shufps xmm6,xmm6,4Eh + /*Stall*/ + addps xmm7,xmm6 + /*Stall*/ + movaps xmm6,xmm7 + /*Stall*/ + shufps xmm6,xmm6,11h + /*Stall*/ + addps xmm7,xmm6 + /*Stall*/ + movss back,xmm7 + } + + return(back); +#endif /*DB_USE_SIMD*/ +} + +#endif /* DB_UTILITIES_LINALG */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.cpp b/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.cpp new file mode 100644 index 0000000000000000000000000000000000000000..013ac726e661e72df501afa3a21828ec9bd4f32a --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.cpp @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_poly.cpp,v 1.2 2010/09/03 12:00:10 bsouthall Exp $ */ + +#include "db_utilities_poly.h" +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ + +void db_SolveCubic(double *roots,int *nr_roots,double a,double b,double c,double d) +{ + double bp,bp2,cp,dp,q,r,srq; + double r2_min_q3,theta,bp_through3,theta_through3; + double cos_theta_through3,sin_theta_through3,min2_cos_theta_plu,min2_cos_theta_min; + double si_r_srq,A; + + /*For nondegenerate cubics with three roots + [24 mult 9 add 2sqrt 1acos 1cos=33flops 4func] + For nondegenerate cubics with one root + [16 mult 6 add 1sqrt 1qbrt=24flops 3func]*/ + + if(a==0.0) db_SolveQuadratic(roots,nr_roots,b,c,d); + else + { + bp=b/a; + bp2=bp*bp; + cp=c/a; + dp=d/a; + + q=(bp2-3.0*cp)/9.0; + r=(2.0*bp2*bp-9.0*bp*cp+27.0*dp)/54.0; + r2_min_q3=r*r-q*q*q; + if(r2_min_q3<0.0) + { + *nr_roots=3; + /*q has to be > 0*/ + srq=sqrt(q); + theta=acos(db_maxd(-1.0,db_mind(1.0,r/(q*srq)))); + bp_through3=bp/3.0; + theta_through3=theta/3.0; + cos_theta_through3=cos(theta_through3); + sin_theta_through3=sqrt(db_maxd(0.0,1.0-cos_theta_through3*cos_theta_through3)); + + /*cos(theta_through3+2*pi/3)=cos_theta_through3*cos(2*pi/3)-sin_theta_through3*sin(2*pi/3) + = -0.5*cos_theta_through3-sqrt(3)/2.0*sin_theta_through3 + = -0.5*(cos_theta_through3+sqrt(3)*sin_theta_through3)*/ + min2_cos_theta_plu=cos_theta_through3+DB_SQRT3*sin_theta_through3; + min2_cos_theta_min=cos_theta_through3-DB_SQRT3*sin_theta_through3; + + roots[0]= -2.0*srq*cos_theta_through3-bp_through3; + roots[1]=srq*min2_cos_theta_plu-bp_through3; + roots[2]=srq*min2_cos_theta_min-bp_through3; + } + else if(r2_min_q3>0.0) + { + *nr_roots=1; + A= -db_sign(r)*db_CubRoot(db_absd(r)+sqrt(r2_min_q3)); + bp_through3=bp/3.0; + if(A!=0.0) roots[0]=A+q/A-bp_through3; + else roots[0]= -bp_through3; + } + else + { + *nr_roots=2; + bp_through3=bp/3.0; + /*q has to be >= 0*/ + si_r_srq=db_sign(r)*sqrt(q); + /*Single root*/ + roots[0]= -2.0*si_r_srq-bp_through3; + /*Double root*/ + roots[1]=si_r_srq-bp_through3; + } + } +} + +void db_SolveQuartic(double *roots,int *nr_roots,double a,double b,double c,double d,double e) +{ + /*Normalized coefficients*/ + double c0,c1,c2,c3; + /*Temporary coefficients*/ + double c3through2,c3through4,c3c3through4_min_c2,min4_c0; + double lz,ms,ns,mn,m,n,lz_through2; + /*Cubic polynomial roots, nr of roots and coefficients*/ + double c_roots[3]; + int nr_c_roots; + double k0,k1; + /*nr additional roots from second quadratic*/ + int addroots; + + /*For nondegenerate quartics + [16mult 11add 2sqrt 1cubic 2quadratic=74flops 8funcs]*/ + + if(a==0.0) db_SolveCubic(roots,nr_roots,b,c,d,e); + else if(e==0.0) + { + db_SolveCubic(roots,nr_roots,a,b,c,d); + roots[*nr_roots]=0.0; + *nr_roots+=1; + } + else + { + /*Compute normalized coefficients*/ + c3=b/a; + c2=c/a; + c1=d/a; + c0=e/a; + /*Compute temporary coefficients*/ + c3through2=c3/2.0; + c3through4=c3/4.0; + c3c3through4_min_c2=c3*c3through4-c2; + min4_c0= -4.0*c0; + /*Compute coefficients of cubic*/ + k0=min4_c0*c3c3through4_min_c2-c1*c1; + k1=c1*c3+min4_c0; + /*k2= -c2*/ + /*k3=1.0*/ + + /*Solve it for roots*/ + db_SolveCubic(c_roots,&nr_c_roots,1.0,-c2,k1,k0); + + if(nr_c_roots>0) + { + lz=c_roots[0]; + lz_through2=lz/2.0; + ms=lz+c3c3through4_min_c2; + ns=lz_through2*lz_through2-c0; + mn=lz*c3through4-c1/2.0; + + if((ms>=0.0)&&(ns>=0.0)) + { + m=sqrt(ms); + n=sqrt(ns)*db_sign(mn); + + db_SolveQuadratic(roots,nr_roots, + 1.0,c3through2+m,lz_through2+n); + + db_SolveQuadratic(&roots[*nr_roots],&addroots, + 1.0,c3through2-m,lz_through2-n); + + *nr_roots+=addroots; + } + else *nr_roots=0; + } + else *nr_roots=0; + } +} + +void db_SolveQuarticForced(double *roots,int *nr_roots,double a,double b,double c,double d,double e) +{ + /*Normalized coefficients*/ + double c0,c1,c2,c3; + /*Temporary coefficients*/ + double c3through2,c3through4,c3c3through4_min_c2,min4_c0; + double lz,ms,ns,mn,m,n,lz_through2; + /*Cubic polynomial roots, nr of roots and coefficients*/ + double c_roots[3]; + int nr_c_roots; + double k0,k1; + /*nr additional roots from second quadratic*/ + int addroots; + + /*For nondegenerate quartics + [16mult 11add 2sqrt 1cubic 2quadratic=74flops 8funcs]*/ + + if(a==0.0) db_SolveCubic(roots,nr_roots,b,c,d,e); + else if(e==0.0) + { + db_SolveCubic(roots,nr_roots,a,b,c,d); + roots[*nr_roots]=0.0; + *nr_roots+=1; + } + else + { + /*Compute normalized coefficients*/ + c3=b/a; + c2=c/a; + c1=d/a; + c0=e/a; + /*Compute temporary coefficients*/ + c3through2=c3/2.0; + c3through4=c3/4.0; + c3c3through4_min_c2=c3*c3through4-c2; + min4_c0= -4.0*c0; + /*Compute coefficients of cubic*/ + k0=min4_c0*c3c3through4_min_c2-c1*c1; + k1=c1*c3+min4_c0; + /*k2= -c2*/ + /*k3=1.0*/ + + /*Solve it for roots*/ + db_SolveCubic(c_roots,&nr_c_roots,1.0,-c2,k1,k0); + + if(nr_c_roots>0) + { + lz=c_roots[0]; + lz_through2=lz/2.0; + ms=lz+c3c3through4_min_c2; + ns=lz_through2*lz_through2-c0; + mn=lz*c3through4-c1/2.0; + + if(ms<0.0) ms=0.0; + if(ns<0.0) ns=0.0; + + m=sqrt(ms); + n=sqrt(ns)*db_sign(mn); + + db_SolveQuadratic(roots,nr_roots, + 1.0,c3through2+m,lz_through2+n); + + db_SolveQuadratic(&roots[*nr_roots],&addroots, + 1.0,c3through2-m,lz_through2-n); + + *nr_roots+=addroots; + } + else *nr_roots=0; + } +} diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.h new file mode 100644 index 0000000000000000000000000000000000000000..1f87890778f95bdb1d82a96286893590a345841e --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_poly.h @@ -0,0 +1,383 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_poly.h,v 1.2 2010/09/03 12:00:11 bsouthall Exp $ */ + +#ifndef DB_UTILITIES_POLY +#define DB_UTILITIES_POLY + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMPolynomial (LM) Polynomial utilities (solvers, arithmetic, evaluation, etc.) + */ +/*\{*/ + +/*! +In debug mode closed form quadratic solving takes on the order of 15 microseconds +while eig of the companion matrix takes about 1.1 milliseconds +Speed-optimized code in release mode solves a quadratic in 0.3 microseconds on 450MHz +*/ +inline void db_SolveQuadratic(double *roots,int *nr_roots,double a,double b,double c) +{ + double rs,srs,q; + + /*For non-degenerate quadratics + [5 mult 2 add 1 sqrt=7flops 1func]*/ + if(a==0.0) + { + if(b==0.0) *nr_roots=0; + else + { + roots[0]= -c/b; + *nr_roots=1; + } + } + else + { + rs=b*b-4.0*a*c; + if(rs>=0.0) + { + *nr_roots=2; + srs=sqrt(rs); + q= -0.5*(b+db_sign(b)*srs); + roots[0]=q/a; + /*If b is zero db_sign(b) returns 1, + so q is only zero when b=0 and c=0*/ + if(q==0.0) *nr_roots=1; + else roots[1]=c/q; + } + else *nr_roots=0; + } +} + +/*! +In debug mode closed form cubic solving takes on the order of 45 microseconds +while eig of the companion matrix takes about 1.3 milliseconds +Speed-optimized code in release mode solves a cubic in 1.5 microseconds on 450MHz +For a non-degenerate cubic with two roots, the first root is the single root and +the second root is the double root +*/ +DB_API void db_SolveCubic(double *roots,int *nr_roots,double a,double b,double c,double d); +/*! +In debug mode closed form quartic solving takes on the order of 0.1 milliseconds +while eig of the companion matrix takes about 1.5 milliseconds +Speed-optimized code in release mode solves a quartic in 2.6 microseconds on 450MHz*/ +DB_API void db_SolveQuartic(double *roots,int *nr_roots,double a,double b,double c,double d,double e); +/*! +Quartic solving where a solution is forced when splitting into quadratics, which +can be good if the quartic is sometimes in fact a quadratic, such as in absolute orientation +when the data is planar*/ +DB_API void db_SolveQuarticForced(double *roots,int *nr_roots,double a,double b,double c,double d,double e); + +inline double db_PolyEval1(const double p[2],double x) +{ + return(p[0]+x*p[1]); +} + +inline void db_MultiplyPoly1_1(double *d,const double *a,const double *b) +{ + double a0,a1; + double b0,b1; + a0=a[0];a1=a[1]; + b0=b[0];b1=b[1]; + + d[0]=a0*b0; + d[1]=a0*b1+a1*b0; + d[2]= a1*b1; +} + +inline void db_MultiplyPoly0_2(double *d,const double *a,const double *b) +{ + double a0; + double b0,b1,b2; + a0=a[0]; + b0=b[0];b1=b[1];b2=b[2]; + + d[0]=a0*b0; + d[1]=a0*b1; + d[2]=a0*b2; +} + +inline void db_MultiplyPoly1_2(double *d,const double *a,const double *b) +{ + double a0,a1; + double b0,b1,b2; + a0=a[0];a1=a[1]; + b0=b[0];b1=b[1];b2=b[2]; + + d[0]=a0*b0; + d[1]=a0*b1+a1*b0; + d[2]=a0*b2+a1*b1; + d[3]= a1*b2; +} + + +inline void db_MultiplyPoly1_3(double *d,const double *a,const double *b) +{ + double a0,a1; + double b0,b1,b2,b3; + a0=a[0];a1=a[1]; + b0=b[0];b1=b[1];b2=b[2];b3=b[3]; + + d[0]=a0*b0; + d[1]=a0*b1+a1*b0; + d[2]=a0*b2+a1*b1; + d[3]=a0*b3+a1*b2; + d[4]= a1*b3; +} +/*! +Multiply d=a*b where a is one degree and b is two degree*/ +inline void db_AddPolyProduct0_1(double *d,const double *a,const double *b) +{ + double a0; + double b0,b1; + a0=a[0]; + b0=b[0];b1=b[1]; + + d[0]+=a0*b0; + d[1]+=a0*b1; +} +inline void db_AddPolyProduct0_2(double *d,const double *a,const double *b) +{ + double a0; + double b0,b1,b2; + a0=a[0]; + b0=b[0];b1=b[1];b2=b[2]; + + d[0]+=a0*b0; + d[1]+=a0*b1; + d[2]+=a0*b2; +} +/*! +Multiply d=a*b where a is one degree and b is two degree*/ +inline void db_SubtractPolyProduct0_0(double *d,const double *a,const double *b) +{ + double a0; + double b0; + a0=a[0]; + b0=b[0]; + + d[0]-=a0*b0; +} + +inline void db_SubtractPolyProduct0_1(double *d,const double *a,const double *b) +{ + double a0; + double b0,b1; + a0=a[0]; + b0=b[0];b1=b[1]; + + d[0]-=a0*b0; + d[1]-=a0*b1; +} + +inline void db_SubtractPolyProduct0_2(double *d,const double *a,const double *b) +{ + double a0; + double b0,b1,b2; + a0=a[0]; + b0=b[0];b1=b[1];b2=b[2]; + + d[0]-=a0*b0; + d[1]-=a0*b1; + d[2]-=a0*b2; +} + +inline void db_SubtractPolyProduct1_3(double *d,const double *a,const double *b) +{ + double a0,a1; + double b0,b1,b2,b3; + a0=a[0];a1=a[1]; + b0=b[0];b1=b[1];b2=b[2];b3=b[3]; + + d[0]-=a0*b0; + d[1]-=a0*b1+a1*b0; + d[2]-=a0*b2+a1*b1; + d[3]-=a0*b3+a1*b2; + d[4]-= a1*b3; +} + +inline void db_CharacteristicPolynomial4x4(double p[5],const double A[16]) +{ + /*All two by two determinants of the first two rows*/ + double two01[3],two02[3],two03[3],two12[3],two13[3],two23[3]; + /*Polynomials representing third and fourth row of A*/ + double P0[2],P1[2],P2[2],P3[2]; + double P4[2],P5[2],P6[2],P7[2]; + /*All three by three determinants of the first three rows*/ + double neg_three0[4],neg_three1[4],three2[4],three3[4]; + + /*Compute 2x2 determinants*/ + two01[0]=A[0]*A[5]-A[1]*A[4]; + two01[1]= -(A[0]+A[5]); + two01[2]=1.0; + + two02[0]=A[0]*A[6]-A[2]*A[4]; + two02[1]= -A[6]; + + two03[0]=A[0]*A[7]-A[3]*A[4]; + two03[1]= -A[7]; + + two12[0]=A[1]*A[6]-A[2]*A[5]; + two12[1]=A[2]; + + two13[0]=A[1]*A[7]-A[3]*A[5]; + two13[1]=A[3]; + + two23[0]=A[2]*A[7]-A[3]*A[6]; + + P0[0]=A[8]; + P1[0]=A[9]; + P2[0]=A[10];P2[1]= -1.0; + P3[0]=A[11]; + + P4[0]=A[12]; + P5[0]=A[13]; + P6[0]=A[14]; + P7[0]=A[15];P7[1]= -1.0; + + /*Compute 3x3 determinants.Note that the highest + degree polynomial goes first and the smaller ones + are added or subtracted from it*/ + db_MultiplyPoly1_1( neg_three0,P2,two13); + db_SubtractPolyProduct0_0(neg_three0,P1,two23); + db_SubtractPolyProduct0_1(neg_three0,P3,two12); + + db_MultiplyPoly1_1( neg_three1,P2,two03); + db_SubtractPolyProduct0_1(neg_three1,P3,two02); + db_SubtractPolyProduct0_0(neg_three1,P0,two23); + + db_MultiplyPoly0_2( three2,P3,two01); + db_AddPolyProduct0_1( three2,P0,two13); + db_SubtractPolyProduct0_1(three2,P1,two03); + + db_MultiplyPoly1_2( three3,P2,two01); + db_AddPolyProduct0_1( three3,P0,two12); + db_SubtractPolyProduct0_1(three3,P1,two02); + + /*Compute 4x4 determinants*/ + db_MultiplyPoly1_3( p,P7,three3); + db_AddPolyProduct0_2( p,P4,neg_three0); + db_SubtractPolyProduct0_2(p,P5,neg_three1); + db_SubtractPolyProduct0_2(p,P6,three2); +} + +inline void db_RealEigenvalues4x4(double lambda[4],int *nr_roots,const double A[16],int forced=0) +{ + double p[5]; + + db_CharacteristicPolynomial4x4(p,A); + if(forced) db_SolveQuarticForced(lambda,nr_roots,p[4],p[3],p[2],p[1],p[0]); + else db_SolveQuartic(lambda,nr_roots,p[4],p[3],p[2],p[1],p[0]); +} + +/*! +Compute the unit norm eigenvector v of the matrix A corresponding +to the eigenvalue lambda +[96mult 60add 1sqrt=156flops 1sqrt]*/ +inline void db_EigenVector4x4(double v[4],double lambda,const double A[16]) +{ + double a0,a5,a10,a15; + double d01,d02,d03,d12,d13,d23; + double e01,e02,e03,e12,e13,e23; + double C[16],n0,n1,n2,n3,m; + + /*Compute diagonal + [4add=4flops]*/ + a0=A[0]-lambda; + a5=A[5]-lambda; + a10=A[10]-lambda; + a15=A[15]-lambda; + + /*Compute 2x2 determinants of rows 1,2 and 3,4 + [24mult 12add=36flops]*/ + d01=a0*a5 -A[1]*A[4]; + d02=a0*A[6] -A[2]*A[4]; + d03=a0*A[7] -A[3]*A[4]; + d12=A[1]*A[6]-A[2]*a5; + d13=A[1]*A[7]-A[3]*a5; + d23=A[2]*A[7]-A[3]*A[6]; + + e01=A[8]*A[13]-A[9] *A[12]; + e02=A[8]*A[14]-a10 *A[12]; + e03=A[8]*a15 -A[11]*A[12]; + e12=A[9]*A[14]-a10 *A[13]; + e13=A[9]*a15 -A[11]*A[13]; + e23=a10 *a15 -A[11]*A[14]; + + /*Compute matrix of cofactors + [48mult 32 add=80flops*/ + C[0]= (a5 *e23-A[6]*e13+A[7]*e12); + C[1]= -(A[4]*e23-A[6]*e03+A[7]*e02); + C[2]= (A[4]*e13-a5 *e03+A[7]*e01); + C[3]= -(A[4]*e12-a5 *e02+A[6]*e01); + + C[4]= -(A[1]*e23-A[2]*e13+A[3]*e12); + C[5]= (a0 *e23-A[2]*e03+A[3]*e02); + C[6]= -(a0 *e13-A[1]*e03+A[3]*e01); + C[7]= (a0 *e12-A[1]*e02+A[2]*e01); + + C[8]= (A[13]*d23-A[14]*d13+a15 *d12); + C[9]= -(A[12]*d23-A[14]*d03+a15 *d02); + C[10]= (A[12]*d13-A[13]*d03+a15 *d01); + C[11]= -(A[12]*d12-A[13]*d02+A[14]*d01); + + C[12]= -(A[9]*d23-a10 *d13+A[11]*d12); + C[13]= (A[8]*d23-a10 *d03+A[11]*d02); + C[14]= -(A[8]*d13-A[9]*d03+A[11]*d01); + C[15]= (A[8]*d12-A[9]*d02+a10 *d01); + + /*Compute square sums of rows + [16mult 12add=28flops*/ + n0=db_sqr(C[0]) +db_sqr(C[1]) +db_sqr(C[2]) +db_sqr(C[3]); + n1=db_sqr(C[4]) +db_sqr(C[5]) +db_sqr(C[6]) +db_sqr(C[7]); + n2=db_sqr(C[8]) +db_sqr(C[9]) +db_sqr(C[10])+db_sqr(C[11]); + n3=db_sqr(C[12])+db_sqr(C[13])+db_sqr(C[14])+db_sqr(C[15]); + + /*Take the largest norm row and normalize + [4mult 1 sqrt=4flops 1sqrt]*/ + if(n0>=n1 && n0>=n2 && n0>=n3) + { + m=db_SafeReciprocal(sqrt(n0)); + db_MultiplyScalarCopy4(v,C,m); + } + else if(n1>=n2 && n1>=n3) + { + m=db_SafeReciprocal(sqrt(n1)); + db_MultiplyScalarCopy4(v,&(C[4]),m); + } + else if(n2>=n3) + { + m=db_SafeReciprocal(sqrt(n2)); + db_MultiplyScalarCopy4(v,&(C[8]),m); + } + else + { + m=db_SafeReciprocal(sqrt(n3)); + db_MultiplyScalarCopy4(v,&(C[12]),m); + } +} + + + +/*\}*/ +#endif /* DB_UTILITIES_POLY */ diff --git a/jni_mosaic/feature_stab/db_vlvm/db_utilities_random.h b/jni_mosaic/feature_stab/db_vlvm/db_utilities_random.h new file mode 100644 index 0000000000000000000000000000000000000000..ef24039c1ea72091bbd7a8ae9966b79ec0fc93fc --- /dev/null +++ b/jni_mosaic/feature_stab/db_vlvm/db_utilities_random.h @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: db_utilities_random.h,v 1.1 2010/08/19 18:09:20 bsouthall Exp $ */ + +#ifndef DB_UTILITIES_RANDOM +#define DB_UTILITIES_RANDOM + +#include "db_utilities.h" + + + +/***************************************************************** +* Lean and mean begins here * +*****************************************************************/ +/*! + * \defgroup LMRandom (LM) Random numbers, random sampling + */ +/*\{*/ +/*! + Random Number generator. Initialize with non-zero +integer value r. A double between zero and one is +returned. +\param r seed +\return random double +*/ +inline double db_QuickRandomDouble(int &r) +{ + int c; + c=r/127773; + r=16807*(r-c*127773)-2836*c; + if(r<0) r+=2147483647; + return((1.0/((double)2147483647))*r); + //return (((double)rand())/(double)RAND_MAX); +} + +/*! +Random Number generator. Initialize with non-zero +integer value r. An int between and including 0 and max + \param r seed + \param max upped limit + \return random int +*/ +inline int db_RandomInt(int &r,int max) +{ + double dtemp; + int itemp; + dtemp=db_QuickRandomDouble(r)*(max+1); + itemp=(int) dtemp; + if(itemp<=0) return(0); + if(itemp>=max) return(max); + return(itemp); +} + +/*! + Generate a random sample indexing into [0..pool_size-1]. + \param s sample (out) pre-allocated array of size sample_size + \param sample_size size of sample + \param pool_size upper limit on item index + \param r_seed random number generator seed + */ +inline void db_RandomSample(int *s,int sample_size,int pool_size,int &r_seed) +{ + int temp,temp2,i,j; + + for(i=0;i