diff options
Diffstat (limited to 'tests')
6 files changed, 273 insertions, 349 deletions
diff --git a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java new file mode 100644 index 000000000..fb85b2c91 --- /dev/null +++ b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java @@ -0,0 +1,77 @@ +/* + * 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.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.test.InstrumentationTestCase; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class ExifOutputStreamTest extends InstrumentationTestCase { + + private final int mImageResourceId; + + public ExifOutputStreamTest(int imageResourceId, int xmlReourceId) { + mImageResourceId = imageResourceId; + } + + public void testExifOutputStream() throws IOException, ExifInvalidFormatException { + File file = File.createTempFile("exif_test", ".jpg"); + InputStream imageInputStream = null; + InputStream exifInputStream = null; + FileInputStream reDecodeInputStream = null; + FileInputStream reParseInputStream = null; + try { + // Read the image + imageInputStream = getInstrumentation() + .getContext().getResources().openRawResource(mImageResourceId); + Bitmap bmp = BitmapFactory.decodeStream(imageInputStream); + + // Read exif data + exifInputStream = getInstrumentation() + .getContext().getResources().openRawResource(mImageResourceId); + ExifData exifData = new ExifReader().read(exifInputStream); + + // Encode the image with the exif data + FileOutputStream outputStream = new FileOutputStream(file); + ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream); + exifOutputStream.setExifData(exifData); + bmp.compress(Bitmap.CompressFormat.JPEG, 100, exifOutputStream); + exifOutputStream.close(); + + // Re-decode the temp file and check the data. + reDecodeInputStream = new FileInputStream(file); + Bitmap decodedBmp = BitmapFactory.decodeStream(reDecodeInputStream); + assertNotNull(decodedBmp); + + // Re-parse the temp file the check EXIF tag + reParseInputStream = new FileInputStream(file); + ExifData reExifData = new ExifReader().read(reParseInputStream); + assertEquals(exifData, reExifData); + } finally { + Util.closeSilently(imageInputStream); + Util.closeSilently(exifInputStream); + Util.closeSilently(reDecodeInputStream); + Util.closeSilently(reParseInputStream); + } + } +}
\ No newline at end of file diff --git a/tests/src/com/android/gallery3d/exif/ExifParserTest.java b/tests/src/com/android/gallery3d/exif/ExifParserTest.java index 86ef12fa7..e967e3d28 100644 --- a/tests/src/com/android/gallery3d/exif/ExifParserTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifParserTest.java @@ -20,7 +20,6 @@ import android.content.res.XmlResourceParser; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.test.InstrumentationTestCase; -import android.util.Log; import java.io.IOException; import java.io.InputStream; @@ -58,338 +57,176 @@ public class ExifParserTest extends InstrumentationTestCase { } public void testParse() throws IOException, ExifInvalidFormatException { - ExifParser parser = new ExifParser(); - parseIfd0(parser.parse(mImageInputStream)); - } - - private void parseIfd0(IfdParser ifdParser) throws IOException, - ExifInvalidFormatException { - int type = ifdParser.next(); - int tagNumber=0; - boolean isEnterNextIfd = false; - boolean isEnterExifIfd = false; - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifdParser.readTag(); - if (tag.getDataSize() > 4 || tag.getTagId() == ExifTag.TIFF_TAG.TAG_EXIF_IFD) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); + ExifParser parser = ExifParser.parse(mImageInputStream); + int event = parser.next(); + while (event != ExifParser.EVENT_END) { + switch (event) { + case ExifParser.EVENT_START_OF_IFD: + break; + case ExifParser.EVENT_NEW_TAG: + ExifTag tag = parser.getTag(); + if (!tag.hasValue()) { + parser.registerForTagValue(tag); } else { - checkTag(tag, ifdParser, mIfd0Value); + checkTag(tag); } - tagNumber++; break; - case IfdParser.TYPE_NEXT_IFD: - parseIfd1(ifdParser.parseIfdBlock()); - isEnterNextIfd = true; - break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - tag = ifdParser.getCorrespodingExifTag(); - if(tag.getTagId() == ExifTag.TIFF_TAG.TAG_EXIF_IFD) { - parseExifIfd(ifdParser.parseIfdBlock()); - isEnterExifIfd = true; - } else { - checkTag(ifdParser.getCorrespodingExifTag(), ifdParser, mIfd0Value); + 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); } + checkTag(tag); break; } - type = ifdParser.next(); + event = parser.next(); } - assertEquals(mIfd0Value.size(), tagNumber); - assertTrue(isEnterNextIfd); - assertTrue(isEnterExifIfd); } - private void parseIfd1(IfdParser ifdParser) throws IOException, - ExifInvalidFormatException { - int type = ifdParser.next(); - int tagNumber = 0; - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifdParser.readTag(); - if (tag.getDataSize() > 4) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); - } else { - checkTag(tag, ifdParser, mIfd1Value); - } - tagNumber++; - break; - case IfdParser.TYPE_NEXT_IFD: - fail("Find a ifd after ifd1"); - break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - checkTag(ifdParser.getCorrespodingExifTag(), ifdParser, mIfd1Value); - break; - } - type = ifdParser.next(); + private void checkTag(ExifTag tag) { + HashMap<Short, String> truth = null; + switch (tag.getIfd()) { + case IfdId.TYPE_IFD_0: + truth = mIfd0Value; + break; + case IfdId.TYPE_IFD_1: + truth = mIfd1Value; + break; + case IfdId.TYPE_IFD_EXIF: + truth = mExifIfdValue; + break; + case IfdId.TYPE_IFD_INTEROPERABILITY: + truth = mInteroperabilityIfdValue; + break; } - assertEquals(mIfd1Value.size(), tagNumber); - } - private void parseExifIfd(IfdParser ifdParser) throws IOException, - ExifInvalidFormatException { - int type = ifdParser.next(); - int tagNumber = 0; - boolean isHasInterIfd = false; - boolean isEnterInterIfd = false; - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifdParser.readTag(); - if (tag.getDataSize() > 4) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); - } else if (tag.getTagId() == ExifTag.EXIF_TAG.TAG_INTEROPERABILITY_IFD) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); - isHasInterIfd = true; - } else { - checkTag(tag, ifdParser, mExifIfdValue); - } - tagNumber++; - break; - case IfdParser.TYPE_NEXT_IFD: - fail("Find a ifd after exif ifd"); - break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - tag = ifdParser.getCorrespodingExifTag(); - if (tag.getTagId() == ExifTag.EXIF_TAG.TAG_INTEROPERABILITY_IFD) { - parseInteroperabilityIfd(ifdParser.parseIfdBlock()); - isEnterInterIfd = true; - } else { - checkTag(ifdParser.getCorrespodingExifTag(), ifdParser, mExifIfdValue); - } - break; - } - type = ifdParser.next(); - } - assertEquals(mExifIfdValue.size(), tagNumber); - if (isHasInterIfd) { - assertTrue(isEnterInterIfd); + String truthString = truth.get(tag.getTagId()); + String dataString = tag.valueToString(); + if (truthString == null) { + fail(String.format("Unknown Tag %02x", tag.getTagId())); } + assertEquals(String.format("Tag %02x", tag.getTagId()), truthString, dataString); } - private void parseInteroperabilityIfd(IfdParser ifdParser) throws IOException, - ExifInvalidFormatException { - int type = ifdParser.next(); - int tagNumber = 0; - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifdParser.readTag(); - if (tag.getDataSize() > 4) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); + + private void parseOneIfd(int ifd, int options, HashMap<Short, String> expectedResult) + throws IOException, ExifInvalidFormatException { + int numOfTag = 0; + ExifParser parser = ExifParser.parse(mImageInputStream, options); + int event = parser.next(); + while(event != ExifParser.EVENT_END) { + switch (event) { + case ExifParser.EVENT_START_OF_IFD: + assertEquals(ifd, parser.getCurrentIfd()); + break; + case ExifParser.EVENT_NEW_TAG: + numOfTag++; + ExifTag tag = parser.getTag(); + if (tag.hasValue()) { + checkTag(tag); } else { - checkTag(tag, ifdParser, mInteroperabilityIfdValue); + parser.registerForTagValue(tag); } - tagNumber++; break; - case IfdParser.TYPE_NEXT_IFD: - fail("Find a ifd after exif ifd"); + 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); + } + checkTag(tag); break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - checkTag(ifdParser.getCorrespodingExifTag(), ifdParser - , mInteroperabilityIfdValue); + case ExifParser.EVENT_COMPRESSED_IMAGE: + case ExifParser.EVENT_UNCOMPRESSED_STRIP: + fail("Invalid Event type: " + event); break; } - type = ifdParser.next(); + event = parser.next(); } - assertEquals(mInteroperabilityIfdValue.size(), tagNumber); + assertEquals(expectedResult.size(), numOfTag); } - private void checkTag(ExifTag tag, IfdParser ifdParser, HashMap<Short, String> truth) - throws IOException { - String truthString = truth.get(tag.getTagId()); - if (truthString == null) { - fail(String.format("Unknown Tag %02x", tag.getTagId())); - } - String dataString = readValueToString(tag, ifdParser); - if (!truthString.equals(dataString)) { - fail(String.format("Tag %02x: expect %s but %s", - tag.getTagId(), truthString, dataString)); - } + public void testOnlyExifIfd() throws IOException, ExifInvalidFormatException { + parseOneIfd(IfdId.TYPE_IFD_EXIF, ExifParser.OPTION_IFD_EXIF, mExifIfdValue); } - private String readValueToString(ExifTag tag, IfdParser parser) throws IOException { - StringBuilder sbuilder = new StringBuilder(); - switch(tag.getDataType()) { - case ExifTag.TYPE_BYTE: - byte buf[] = new byte[tag.getComponentCount()]; - parser.read(buf); - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(String.format("%02x", buf[i])); - } - break; - case ExifTag.TYPE_ASCII: - // trim the string for comparison between xml - sbuilder.append(parser.readString(tag.getComponentCount()).trim()); - break; - case ExifTag.TYPE_INT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(parser.readUnsignedInt()); - } - break; - case ExifTag.TYPE_RATIONAL: - for(int i = 0; i < tag.getComponentCount(); i++) { - Rational r = parser.readUnsignedRational(); - if(i != 0) sbuilder.append(" "); - sbuilder.append(r.getNominator()).append("/").append(r.getDenominator()); - } - break; - case ExifTag.TYPE_SHORT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(parser.readUnsignedShort()); - } - break; - case ExifTag.TYPE_SINT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(parser.readInt()); - } - break; - case ExifTag.TYPE_SRATIONAL: - for(int i = 0; i < tag.getComponentCount(); i++) { - Rational r = parser.readRational(); - if(i != 0) sbuilder.append(" "); - sbuilder.append(r.getNominator()).append("/").append(r.getDenominator()); - } - break; - case ExifTag.TYPE_UNDEFINED: - byte buffer[] = new byte[tag.getComponentCount()]; - parser.read(buffer); - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(String.format("%02x", buffer[i])); - } - break; - } - return sbuilder.toString(); + public void testOnlyIfd0() throws IOException, ExifInvalidFormatException { + parseOneIfd(IfdId.TYPE_IFD_0, ExifParser.OPTION_IFD_0, mIfd0Value); } - public void testSkipToNextIfd() throws ExifInvalidFormatException, IOException { - ExifParser exifParser = new ExifParser(); - IfdParser ifdParser = exifParser.parse(mImageInputStream); - int type = ifdParser.next(); - boolean isEnterNextIfd = false; - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - // Do nothing, we don't care - break; - case IfdParser.TYPE_NEXT_IFD: - parseIfd1(ifdParser.parseIfdBlock()); - isEnterNextIfd = true; - break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - // We won't get this since to skip everything - fail("Get value of previous tag but we've skip everything"); - break; - } - type = ifdParser.next(); - } - assertTrue(isEnterNextIfd); + public void testOnlyIfd1() throws IOException, ExifInvalidFormatException { + parseOneIfd(IfdId.TYPE_IFD_1, ExifParser.OPTION_IFD_1, mIfd1Value); + } + + public void testOnlyInteroperabilityIfd() throws IOException, ExifInvalidFormatException { + parseOneIfd(IfdId.TYPE_IFD_INTEROPERABILITY, ExifParser.OPTION_IFD_INTEROPERABILITY + , mInteroperabilityIfdValue); } - public void testOnlySaveSomeValue() throws ExifInvalidFormatException, IOException { - ExifParser exifParser = new ExifParser(); - IfdParser ifdParser = exifParser.parse(mImageInputStream); - boolean isEnterNextIfd = false; - boolean isEnterExifIfd = false; - int type = ifdParser.next(); - while (type != IfdParser.TYPE_END) { - switch (type) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifdParser.readTag(); - // only interested in these two tags - if (tag.getDataSize() > 4 || tag.getTagId() == ExifTag.TIFF_TAG.TAG_EXIF_IFD) { - if(tag.getTagId() == ExifTag.TIFF_TAG.TAG_MODEL - || tag.getTagId() == ExifTag.TIFF_TAG.TAG_EXIF_IFD) { - long offset = ifdParser.readUnsignedInt(); - assertTrue(offset <= Integer.MAX_VALUE); - ifdParser.waitValueOfTag(tag, offset); + public void testOnlyReadSomeTag() throws IOException, ExifInvalidFormatException { + ExifParser parser = ExifParser.parse(mImageInputStream, ExifParser.OPTION_IFD_0); + int event = parser.next(); + boolean isTagFound = false; + while (event != ExifParser.EVENT_END) { + switch (event) { + case ExifParser.EVENT_START_OF_IFD: + assertEquals(IfdId.TYPE_IFD_0, parser.getCurrentIfd()); + break; + case ExifParser.EVENT_NEW_TAG: + ExifTag tag = parser.getTag(); + if (tag.getTagId() == ExifTag.TIFF_TAG.TAG_MODEL) { + if (tag.hasValue()) { + isTagFound = true; + checkTag(tag); + } else { + parser.registerForTagValue(tag); } + parser.skipRemainingTagsInCurrentIfd(); } break; - case IfdParser.TYPE_NEXT_IFD: - parseIfd1(ifdParser.parseIfdBlock()); - isEnterNextIfd = true; - break; - case IfdParser.TYPE_VALUE_OF_PREV_TAG: - tag = ifdParser.getCorrespodingExifTag(); - if(tag.getTagId() == ExifTag.TIFF_TAG.TAG_EXIF_IFD) { - parseExifIfd(ifdParser.parseIfdBlock()); - isEnterExifIfd = true; - } else { - checkTag(ifdParser.getCorrespodingExifTag(), ifdParser, mIfd0Value); - } + case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG: + tag = parser.getTag(); + assertEquals(ExifTag.TIFF_TAG.TAG_MODEL, tag.getTagId()); + checkTag(tag); + isTagFound = true; break; } - type = ifdParser.next(); + event = parser.next(); } - assertTrue(isEnterNextIfd); - assertTrue(isEnterExifIfd); + assertTrue(isTagFound); } public void testReadThumbnail() throws ExifInvalidFormatException, IOException { - ExifParser exifParser = new ExifParser(); - IfdParser ifdParser = exifParser.parse(mImageInputStream); - int type = ifdParser.next(); - while (type != IfdParser.TYPE_END && type != IfdParser.TYPE_NEXT_IFD) { - type = ifdParser.next(); - } - if (type == IfdParser.TYPE_END) { - Log.i(TAG, "No Thumbnail"); - return; - } - IfdParser ifd1Parser = ifdParser.parseIfdBlock(); - int thumbOffset = 0; - int thumbSize = 0; - boolean isFinishRead = false; - while (!isFinishRead) { - switch (ifd1Parser.next()) { - case IfdParser.TYPE_NEW_TAG: - ExifTag tag = ifd1Parser.readTag(); - if (tag.getTagId() == ExifTag.TIFF_TAG.TAG_JPEG_INTERCHANGE_FORMAT) { - long unsignedInt = ifdParser.readUnsignedInt(); - assertTrue(unsignedInt <= Integer.MAX_VALUE); - thumbOffset = (int) unsignedInt; - } else if (tag.getTagId() == - ExifTag.TIFF_TAG.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH) { - long unsignedInt = ifdParser.readUnsignedInt(); - assertTrue(unsignedInt <= Integer.MAX_VALUE); - thumbSize = (int) unsignedInt; - } else if (tag.getTagId() == ExifTag.TIFF_TAG.TAG_COMPRESSION) { - if (ifdParser.readUnsignedShort() == - ExifTag.TIFF_TAG.COMPRESSION_UNCOMPRESSION) { - // This test doesn't apply to uncompression thumbnail. - return; + ExifParser parser = ExifParser.parse(mImageInputStream, + ExifParser.OPTION_IFD_1 | ExifParser.OPTION_THUMBNAIL); + + int event = parser.next(); + Bitmap bmp = null; + boolean mIsContainCompressedImage = false; + while (event != ExifParser.EVENT_END) { + switch (event) { + case ExifParser.EVENT_NEW_TAG: + ExifTag tag = parser.getTag(); + if (tag.getTagId() == ExifTag.TIFF_TAG.TAG_COMPRESSION) { + if (tag.getUnsignedShort(0) == ExifTag.TIFF_TAG.COMPRESSION_JPEG) { + mIsContainCompressedImage = true; } } - isFinishRead = thumbOffset != 0 && thumbSize != 0; break; - case IfdParser.TYPE_END: - fail("No thumbnail information found"); + case ExifParser.EVENT_COMPRESSED_IMAGE: + int imageSize = parser.getCompressedImageSize(); + byte buf[] = new byte[imageSize]; + parser.read(buf); + bmp = BitmapFactory.decodeByteArray(buf, 0, imageSize); break; } + event = parser.next(); + } + if (mIsContainCompressedImage) { + assertNotNull(bmp); } - - byte buf[] = new byte[thumbSize]; - ifd1Parser.skipTo(thumbOffset); - ifd1Parser.read(buf); - Bitmap bmp = BitmapFactory.decodeByteArray(buf, 0, thumbSize); - // Check correctly decoded - assertTrue(bmp != null); } @Override @@ -399,4 +236,4 @@ public class ExifParserTest extends InstrumentationTestCase { mIfd1Value.clear(); mExifIfdValue.clear(); } -} +}
\ No newline at end of file diff --git a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java index 9964997f2..e4ed8ebf9 100644 --- a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java @@ -17,6 +17,7 @@ package com.android.gallery3d.exif; import android.content.res.XmlResourceParser; +import android.graphics.BitmapFactory; import android.test.InstrumentationTestCase; import java.io.IOException; @@ -56,73 +57,81 @@ public class ExifReaderTest extends InstrumentationTestCase { public void testRead() throws ExifInvalidFormatException, IOException { ExifReader reader = new ExifReader(); - ExifData exifData = reader.getExifData(mImageInputStream); - checkIfd(exifData, ExifData.TYPE_IFD_0, mIfd0Value); - checkIfd(exifData, ExifData.TYPE_IFD_1, mIfd1Value); - checkIfd(exifData, ExifData.TYPE_IFD_EXIF, mExifIfdValue); - checkIfd(exifData, ExifData.TYPE_IFD_INTEROPERABILITY, mInteroperabilityIfdValue); + ExifData exifData = reader.read(mImageInputStream); + checkIfd(exifData.getIfdData(IfdId.TYPE_IFD_0), mIfd0Value); + checkIfd(exifData.getIfdData(IfdId.TYPE_IFD_1), mIfd1Value); + checkIfd(exifData.getIfdData(IfdId.TYPE_IFD_EXIF), mExifIfdValue); + checkIfd(exifData.getIfdData(IfdId.TYPE_IFD_INTEROPERABILITY), + mInteroperabilityIfdValue); + checkThumbnail(exifData); } - private void checkIfd(ExifData exifData, int ifdType, HashMap<Short, String> ifdValue) - throws IOException { - IfdData ifd = exifData.getIfdData(ifdType); + private void checkThumbnail(ExifData exifData) { + IfdData ifd1 = exifData.getIfdData(IfdId.TYPE_IFD_1); + if (ifd1 != null) { + if (ifd1.getTag(ExifTag.TIFF_TAG.TAG_COMPRESSION).getUnsignedShort(0) == + ExifTag.TIFF_TAG.COMPRESSION_JPEG) { + assertTrue(exifData.hasCompressedThumbnail()); + byte[] thumbnail = exifData.getCompressedThumbnail(); + assertTrue(BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length) != null); + } else { + // Try to check the strip count with the formula provided by EXIF spec. + int planarType = ExifTag.TIFF_TAG.PLANAR_CONFIGURATION_CHUNKY; + ExifTag planarTag = ifd1.getTag(ExifTag.TIFF_TAG.TAG_PLANAR_CONFIGURATION); + if (planarTag != null) { + planarType = planarTag.getUnsignedShort(0); + } + + ExifTag heightTag = ifd1.getTag(ExifTag.TIFF_TAG.TAG_IMAGE_HEIGHT); + ExifTag rowPerStripTag = ifd1.getTag(ExifTag.TIFF_TAG.TAG_ROWS_PER_STRIP); + + int imageLength = getUnsignedIntOrShort(heightTag); + int rowsPerStrip = getUnsignedIntOrShort(rowPerStripTag); + int stripCount = ifd1.getTag( + ExifTag.TIFF_TAG.TAG_STRIP_OFFSETS).getComponentCount(); + + if (planarType == ExifTag.TIFF_TAG.PLANAR_CONFIGURATION_CHUNKY) { + assertTrue(stripCount == (imageLength + rowsPerStrip - 1) / rowsPerStrip); + } else { + ExifTag samplePerPixelTag = ifd1.getTag(ExifTag.TIFF_TAG.TAG_SAMPLES_PER_PIXEL); + int samplePerPixel = samplePerPixelTag.getUnsignedShort(0); + assertTrue(stripCount == + (imageLength + rowsPerStrip - 1) / rowsPerStrip * samplePerPixel); + } + + for (int i = 0; i < stripCount; i++) { + ExifTag byteCountTag = ifd1.getTag(ExifTag.TIFF_TAG.TAG_STRIP_BYTE_COUNTS); + if (byteCountTag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) { + assertEquals(byteCountTag.getUnsignedShort(i), exifData.getStrip(i).length); + } else { + assertEquals( + byteCountTag.getUnsignedInt(i), exifData.getStrip(i).length); + } + } + } + } + } + + private int getUnsignedIntOrShort(ExifTag tag) { + if (tag.getDataType() == ExifTag.TYPE_UNSIGNED_SHORT) { + return tag.getUnsignedShort(0); + } else { + return (int) tag.getUnsignedInt(0); + } + } + + private void checkIfd(IfdData ifd, HashMap<Short, String> ifdValue) { if (ifd == null) { assertEquals(0 ,ifdValue.size()); return; } - ExifTag[] tags = ifd.getAllTags(new ExifTag[0]); + ExifTag[] tags = ifd.getAllTags(); for (ExifTag tag : tags) { - assertEquals(ifdValue.get(tag.getTagId()), readValueToString(tag, ifd)); + assertEquals(ifdValue.get(tag.getTagId()), tag.valueToString()); } assertEquals(ifdValue.size(), tags.length); } - private String readValueToString(ExifTag tag, IfdData ifdData) throws IOException { - StringBuilder sbuilder = new StringBuilder(); - switch(tag.getDataType()) { - case ExifTag.TYPE_UNDEFINED: - case ExifTag.TYPE_BYTE: - byte buf[] = new byte[tag.getComponentCount()]; - ifdData.getBytes(tag.getTagId(), buf); - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(String.format("%02x", buf[i])); - } - break; - case ExifTag.TYPE_ASCII: - // trim the string for comparison between xml - sbuilder.append(ifdData.getString(tag.getTagId()).trim()); - break; - case ExifTag.TYPE_INT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(ifdData.getUnsignedInt(tag.getTagId(), i)); - } - break; - case ExifTag.TYPE_SRATIONAL: - case ExifTag.TYPE_RATIONAL: - for(int i = 0; i < tag.getComponentCount(); i++) { - Rational r = ifdData.getRational(tag.getTagId(), i); - if(i != 0) sbuilder.append(" "); - sbuilder.append(r.getNominator()).append("/").append(r.getDenominator()); - } - break; - case ExifTag.TYPE_SHORT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(ifdData.getUnsignedShort(tag.getTagId(), i)); - } - break; - case ExifTag.TYPE_SINT: - for(int i = 0; i < tag.getComponentCount(); i++) { - if(i != 0) sbuilder.append(" "); - sbuilder.append(ifdData.getInt(tag.getTagId(), i)); - } - break; - } - return sbuilder.toString(); - } - @Override public void tearDown() throws Exception { mImageInputStream.close(); diff --git a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java index 022597d73..bcbc9f57b 100644 --- a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java +++ b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java @@ -44,6 +44,7 @@ public class ExifTestRunner extends InstrumentationTestRunner { TestSuite suite = new InstrumentationTestSuite(this); getAllTestFromTestCase(ExifParserTest.class, suite); getAllTestFromTestCase(ExifReaderTest.class, suite); + getAllTestFromTestCase(ExifOutputStreamTest.class, suite); return suite; } diff --git a/tests/src/com/android/gallery3d/exif/ExifXmlReader.java b/tests/src/com/android/gallery3d/exif/ExifXmlReader.java index ea748cc18..72dd31373 100644 --- a/tests/src/com/android/gallery3d/exif/ExifXmlReader.java +++ b/tests/src/com/android/gallery3d/exif/ExifXmlReader.java @@ -94,4 +94,4 @@ public class ExifXmlReader { data.put(id, value); parser.require(XmlPullParser.END_TAG, null, XML_TAG); } -} +}
\ No newline at end of file diff --git a/tests/src/com/android/gallery3d/ui/GLCanvasStub.java b/tests/src/com/android/gallery3d/ui/GLCanvasStub.java index f30b57964..2f2d753e0 100644 --- a/tests/src/com/android/gallery3d/ui/GLCanvasStub.java +++ b/tests/src/com/android/gallery3d/ui/GLCanvasStub.java @@ -23,7 +23,7 @@ import javax.microedition.khronos.opengles.GL11; public class GLCanvasStub implements GLCanvas { public void setSize(int width, int height) {} public void clearBuffer() {} - public void clearBuffer(float r, float g, float b, float a) {} + public void clearBuffer(float[] argb) {} public void setCurrentAnimationTimeMillis(long time) {} public long currentAnimationTimeMillis() { throw new UnsupportedOperationException(); |