Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c60752b2 authored by Sascha Haeberling's avatar Sascha Haeberling
Browse files

Use XMP meta-data for correct tiny planets.

 Bug: 7403766

Change-Id: Ie03d40d0396d352d6fe48c91b995a2f872c98a06
parent eb75699b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -384,7 +384,7 @@ public class ImageLoader {
        return mAdapter;
    }

    public Object getXmpObject() {
    public XMPMeta getXmpObject() {
        try {
            InputStream is = mContext.getContentResolver().openInputStream(getUri());
            return XmpUtilHelper.extractXMPMeta(is);
+59 −11
Original line number Diff line number Diff line
@@ -17,7 +17,11 @@
package com.android.gallery3d.filtershow.filters;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMeta;
import com.android.gallery3d.filtershow.presets.ImagePreset;

/**
@@ -25,6 +29,20 @@ import com.android.gallery3d.filtershow.presets.ImagePreset;
 */
public class ImageFilterTinyPlanet extends ImageFilter {
    private static final String TAG = ImageFilterTinyPlanet.class.getSimpleName();
    public static final String GOOGLE_PANO_NAMESPACE = "http://ns.google.com/photos/1.0/panorama/";

    public static final String CROPPED_AREA_IMAGE_WIDTH_PIXELS =
            "CroppedAreaImageWidthPixels";
    public static final String CROPPED_AREA_IMAGE_HEIGHT_PIXELS =
            "CroppedAreaImageHeightPixels";
    public static final String CROPPED_AREA_FULL_PANO_WIDTH_PIXELS =
            "FullPanoWidthPixels";
    public static final String CROPPED_AREA_FULL_PANO_HEIGHT_PIXELS =
            "FullPanoHeightPixels";
    public static final String CROPPED_AREA_LEFT =
            "CroppedAreaLeftPixels";
    public static final String CROPPED_AREA_TOP =
            "CroppedAreaTopPixels";

    public ImageFilterTinyPlanet() {
        setFilterType(TYPE_TINYPLANET);
@@ -43,26 +61,56 @@ public class ImageFilterTinyPlanet extends ImageFilter {

    @Override
    public Bitmap apply(Bitmap bitmapIn, float scaleFactor, boolean highQuality) {
        int w = bitmapIn.getWidth();
        int h = bitmapIn.getHeight();
        int outputSize = Math.min(w, h);

        ImagePreset preset = getImagePreset();
        if (preset != null) {
            if (preset.isPanoramaSafe()) {
                // TODO(haeberling): Get XMPMeta object.
                Object xmp = preset.getImageLoader().getXmpObject();
                try {
                    XMPMeta xmp = preset.getImageLoader().getXmpObject();
                    int croppedAreaWidth =
                            getInt(xmp, CROPPED_AREA_IMAGE_WIDTH_PIXELS);
                    int croppedAreaHeight =
                            getInt(xmp, CROPPED_AREA_IMAGE_HEIGHT_PIXELS);
                    int fullPanoWidth =
                            getInt(xmp, CROPPED_AREA_FULL_PANO_WIDTH_PIXELS);
                    int fullPanoHeight =
                            getInt(xmp, CROPPED_AREA_FULL_PANO_HEIGHT_PIXELS);
                    int left = getInt(xmp, CROPPED_AREA_LEFT);
                    int top = getInt(xmp, CROPPED_AREA_TOP);

                    Bitmap paddedBitmap = Bitmap.createBitmap(
                            fullPanoWidth, fullPanoHeight, Bitmap.Config.ARGB_8888);
                    Canvas paddedCanvas = new Canvas(paddedBitmap);

                    int right = left + croppedAreaWidth;
                    int bottom = top + croppedAreaHeight;
                    Rect destRect = new Rect(left, top, right, bottom);
                    paddedCanvas.drawBitmap(bitmapIn, null, destRect, null);
                    bitmapIn = paddedBitmap;
                } catch (XMPException ex) {
                    // Do nothing, just use bitmapIn as is.
                }
            } else {
                // TODO(haeberling): What should we do for:
                // !preset.isPanoramaSafe()?
                // Do nothing, just use bitmapIn as is, there is nothing else we
                // can do.
            }
        }

        int w = bitmapIn.getWidth();
        int h = bitmapIn.getHeight();
        int outputSize = Math.min(w, h);

        Bitmap mBitmapOut = Bitmap.createBitmap(
                outputSize, outputSize, Bitmap.Config.ARGB_8888);

        // TODO(haeberling): Add the padding back in based on the meta-data.
        nativeApplyFilter(bitmapIn, w, h, mBitmapOut, outputSize, mParameter / 100f, 0f);
        nativeApplyFilter(bitmapIn, bitmapIn.getWidth(), bitmapIn.getHeight(), mBitmapOut,
                outputSize, mParameter / 100f, 0f);
        return mBitmapOut;
    }

    private static int getInt(XMPMeta xmp, String key) throws XMPException {
        if (xmp.doesPropertyExist(GOOGLE_PANO_NAMESPACE, key)) {
            return xmp.getPropertyInteger(GOOGLE_PANO_NAMESPACE, key);
        } else {
            return 0;
        }
    }
}