1afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford/* 2afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * Copyright (C) 2013 The Android Open Source Project 3afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * 4afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * Licensed under the Apache License, Version 2.0 (the "License"); 5afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * you may not use this file except in compliance with the License. 6afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * You may obtain a copy of the License at 7afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * 8afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * http://www.apache.org/licenses/LICENSE-2.0 9afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * 10afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * Unless required by applicable law or agreed to in writing, software 11afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * distributed under the License is distributed on an "AS IS" BASIS, 12afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * See the License for the specific language governing permissions and 14afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford * limitations under the License. 15afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford */ 16afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 17afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordpackage com.android.gallery3d.filtershow.tools; 18afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 19afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport android.content.Context; 20afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport android.net.Uri; 21afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport android.util.Log; 22afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 23afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.adobe.xmp.XMPException; 24afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.adobe.xmp.XMPMeta; 25afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.adobe.xmp.XMPMetaFactory; 26afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.android.gallery3d.R; 27afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.android.gallery3d.common.Utils; 28afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage; 29ce9ceff5776a9b0479c30cbeb2a9388b44df1865nicolasroardimport com.android.gallery3d.filtershow.pipeline.ImagePreset; 30afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport com.android.gallery3d.util.XmpUtilHelper; 31afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 32afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport java.io.File; 33afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport java.io.FileNotFoundException; 34afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordimport java.io.InputStream; 35afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 36afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hofordpublic class XmpPresets { 37afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static final String 38afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMP_GOOGLE_FILTER_NAMESPACE = "http://ns.google.com/photos/1.0/filter/"; 39afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static final String XMP_GOOGLE_FILTER_PREFIX = "AFltr"; 40afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static final String XMP_SRC_FILE_URI = "SourceFileUri"; 41afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static final String XMP_FILTERSTACK = "filterstack"; 428a0072e519157b976d7d1fd34ca2aa1c849a983bJohn Hoford 43afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford private static final String LOGTAG = "XmpPresets"; 44afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 45afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static class XMresults { 46afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public String presetString; 47afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public ImagePreset preset; 48afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public Uri originalimage; 49afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 50afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 51afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford static { 52afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford try { 53afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMPMetaFactory.getSchemaRegistry().registerNamespace( 54afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMP_GOOGLE_FILTER_NAMESPACE, XMP_GOOGLE_FILTER_PREFIX); 55afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } catch (XMPException e) { 56afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Log.e(LOGTAG, "Register XMP name space failed", e); 57afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 58afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 59afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 60afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static void writeFilterXMP( 61afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Context context, Uri srcUri, File dstFile, ImagePreset preset) { 62afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford InputStream is = null; 63afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMPMeta xmpMeta = null; 64afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford try { 65afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford is = context.getContentResolver().openInputStream(srcUri); 66afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford xmpMeta = XmpUtilHelper.extractXMPMeta(is); 67afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } catch (FileNotFoundException e) { 68afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 69afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } finally { 70afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Utils.closeSilently(is); 71afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 72afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 73afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford if (xmpMeta == null) { 74afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford xmpMeta = XMPMetaFactory.create(); 75afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 76afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford try { 77afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford xmpMeta.setProperty(XMP_GOOGLE_FILTER_NAMESPACE, 78afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMP_SRC_FILE_URI, srcUri.toString()); 79afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford xmpMeta.setProperty(XMP_GOOGLE_FILTER_NAMESPACE, 80b17765a3e9ead269493d7a851a21af3622931c40John Hoford XMP_FILTERSTACK, preset.getJsonString(ImagePreset.JASON_SAVED)); 81afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } catch (XMPException e) { 82afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Log.v(LOGTAG, "Write XMP meta to file failed:" + dstFile.getAbsolutePath()); 83afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford return; 84afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 85afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 86afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford if (!XmpUtilHelper.writeXMPMeta(dstFile.getAbsolutePath(), xmpMeta)) { 87afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Log.v(LOGTAG, "Write XMP meta to file failed:" + dstFile.getAbsolutePath()); 88afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 89afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 90afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 91afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford public static XMresults extractXMPData( 92afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Context context, MasterImage mMasterImage, Uri uriToEdit) { 93afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMresults ret = new XMresults(); 94afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 95afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford InputStream is = null; 96afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMPMeta xmpMeta = null; 97afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford try { 98afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford is = context.getContentResolver().openInputStream(uriToEdit); 99afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford xmpMeta = XmpUtilHelper.extractXMPMeta(is); 100afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } catch (FileNotFoundException e) { 101afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } finally { 102afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Utils.closeSilently(is); 103afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 104afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 105afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford if (xmpMeta == null) { 106afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford return null; 107afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 108afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 109afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford try { 110afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford String strSrcUri = xmpMeta.getPropertyString(XMP_GOOGLE_FILTER_NAMESPACE, 111afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMP_SRC_FILE_URI); 112afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 113afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford if (strSrcUri != null) { 114afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford String filterString = xmpMeta.getPropertyString(XMP_GOOGLE_FILTER_NAMESPACE, 115afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford XMP_FILTERSTACK); 116afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 117afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford Uri srcUri = Uri.parse(strSrcUri); 118afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford ret.originalimage = srcUri; 119afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 12032cc4dd751569721aa19218b4d947145577060d0nicolasroard ret.preset = new ImagePreset(); 121afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford ret.presetString = filterString; 122afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford boolean ok = ret.preset.readJsonFromString(filterString); 123afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford if (!ok) { 124afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford return null; 125afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 126afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford return ret; 127afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 128afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } catch (XMPException e) { 129afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford e.printStackTrace(); 130afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 131afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford 132afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford return null; 133afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford } 134afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford} 135