Skip to content

Commit

Permalink
Merge branch 'pr-271'
Browse files Browse the repository at this point in the history
This closes #271
  • Loading branch information
kinow committed Mar 31, 2023
2 parents 99e34e4 + 1dc7bf3 commit 933584e
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 147 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -91,6 +91,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IMAGING-347" dev="kinow" type="update" due-to="snumlautoken">
Refactor BasicCParser::unescapeString().
</action>
<action issue="IMAGING-345" dev="kinow" type="update" due-to="Matthieu Casanova">
Make unit tests work in-memory instead of writing hundred of tmp files.
</action>
</release>
<release version="1.0-alpha3" date="2022-05-13" description="Third 1.0 alpha release">
<action issue="IMAGING-330" dev="kinow" type="fix" due-to="Gary Lucas">
Expand Down
Expand Up @@ -22,13 +22,10 @@
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.internal.Debug;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -140,9 +137,6 @@ private void writeAndReadImageData(final int[][] rawData) throws IOException, Im

final byte[] bytes = Imaging.writeImageToBytes(srcImage, ImageFormats.BMP);

final File tempFile = Files.createTempFile("temp", ".bmp").toFile();
FileUtils.writeByteArrayToFile(tempFile, bytes);

final BufferedImage dstImage = Imaging.getBufferedImage(bytes);

assertNotNull(dstImage);
Expand Down
Expand Up @@ -22,16 +22,14 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.BinaryOutputStream;
import org.apache.commons.imaging.internal.Debug;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

public class IcnsRoundTripTest extends IcnsBaseTest {
Expand Down Expand Up @@ -408,9 +406,7 @@ public void test32BPPMaskMissingIcon() throws Exception {
private void writeAndReadImageData(final String description, final byte[] rawData,
final int foreground, final int background) throws IOException,
ImageReadException {
final File exportFile = Files.createTempFile(description, ".icns").toFile();
FileUtils.writeByteArrayToFile(exportFile, rawData);
final BufferedImage dstImage = Imaging.getBufferedImage(exportFile);
final BufferedImage dstImage = Imaging.getBufferedImage(new ByteArrayInputStream(rawData), "description.icns");

assertNotNull(dstImage);
assertEquals(dstImage.getWidth(), IMAGE[0].length);
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImagingTest;
import org.apache.commons.imaging.common.bytesource.ByteSource;
import org.apache.commons.imaging.common.bytesource.ByteSourceArray;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegImageParser;

Expand All @@ -42,6 +43,15 @@ protected static boolean hasExifData(final File file) {
}
}

protected static boolean hasExifData(final String fileName, final byte[] bytes) {
try {
final ByteSource byteSource = new ByteSourceArray(fileName, bytes);
return new JpegImageParser().hasExifSegment(byteSource);
} catch (final Exception e) {
return false;
}
}

private static final ImageFilter HAS_EXIF_IMAGE_FILTER = ExifBaseTest::hasExifData;

private static final ImageFilter JPEG_IMAGE_FILTER = file -> file.getName().toLowerCase().endsWith(".jpg");
Expand Down
Expand Up @@ -23,11 +23,11 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -49,7 +49,6 @@
import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
import org.apache.commons.imaging.internal.Debug;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -84,14 +83,11 @@ public void testRemove() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ExifRewriter().removeExifMetadata(byteSource, baos);
final byte[] bytes = baos.toByteArray();
final File tempFile = Files.createTempFile("test", ".jpg").toFile();
Debug.debug("tempFile", tempFile);
FileUtils.writeByteArrayToFile(tempFile, bytes);

Debug.debug("Output Segments:");
new JpegUtils().dumpJFIF(new ByteSourceArray(bytes));

assertFalse(hasExifData(tempFile));
assertFalse(hasExifData("test.jpg", bytes));
}
}
}
Expand Down Expand Up @@ -123,15 +119,12 @@ public void testInsert() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ExifRewriter().removeExifMetadata(byteSource, baos);
final byte[] bytes = baos.toByteArray();
final File tempFile = Files.createTempFile("removed", ".jpg").toFile();
Debug.debug("tempFile", tempFile);
FileUtils.writeByteArrayToFile(tempFile, bytes);

Debug.debug("Output Segments:");
stripped = new ByteSourceArray(bytes);
new JpegUtils().dumpJFIF(stripped);

assertFalse(hasExifData(tempFile));
assertFalse(hasExifData("removed.jpg", bytes));
}

{
Expand All @@ -144,16 +137,13 @@ public void testInsert() throws Exception {
outputSet);

final byte[] bytes = baos.toByteArray();
final File tempFile = Files.createTempFile("inserted" + "_", ".jpg").toFile();
Debug.debug("tempFile", tempFile);
FileUtils.writeByteArrayToFile(tempFile, bytes);

Debug.debug("Output Segments:");
new JpegUtils().dumpJFIF(new ByteSourceArray(bytes));

// assertTrue(!hasExifData(tempFile));

final JpegImageMetadata newMetadata = (JpegImageMetadata) Imaging.getMetadata(tempFile);
final JpegImageMetadata newMetadata = (JpegImageMetadata) Imaging.getMetadata(new ByteArrayInputStream(bytes), "inserted.jpg");
assertNotNull(newMetadata);
final TiffImageMetadata newExifMetadata = newMetadata.getExif();
assertNotNull(newExifMetadata);
Expand Down Expand Up @@ -211,16 +201,13 @@ private void rewrite(final Rewriter rewriter, final String name) throws IOExcept
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
rewriter.rewrite(byteSource, baos, outputSet);
final byte[] bytes = baos.toByteArray();
final File tempFile = Files.createTempFile(name + "_", ".jpg").toFile();
Debug.debug("tempFile", tempFile);
FileUtils.writeByteArrayToFile(tempFile, bytes);

Debug.debug("Output Segments:");
new JpegUtils().dumpJFIF(new ByteSourceArray(bytes));

// assertTrue(!hasExifData(tempFile));

final JpegImageMetadata newMetadata = (JpegImageMetadata) Imaging.getMetadata(tempFile);
final JpegImageMetadata newMetadata = (JpegImageMetadata) Imaging.getMetadata(new ByteArrayInputStream(bytes), name + ".jpg");
assertNotNull(newMetadata);
final TiffImageMetadata newExifMetadata = newMetadata.getExif();
assertNotNull(newExifMetadata);
Expand Down
Expand Up @@ -20,16 +20,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.imaging.common.bytesource.ByteSource;
import org.apache.commons.imaging.common.bytesource.ByteSourceArray;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
Expand Down Expand Up @@ -76,13 +74,13 @@ public void testAddIptcData(final File imageFile) throws Exception {

final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);

final File updated = Files.createTempFile(imageFile.getName() + ".iptc.add.", ".jpg").toFile();
try (FileOutputStream fos = new FileOutputStream(updated);
OutputStream os = new BufferedOutputStream(fos)) {
byte[] bytes;
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
new JpegIptcRewriter().writeIPTC(byteSource, os, newData);
bytes = os.toByteArray();
}

final ByteSource updateByteSource = new ByteSourceFile(updated);
final ByteSource updateByteSource = new ByteSourceArray("test.jpg", bytes);
final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
updateByteSource, params);

Expand Down
Expand Up @@ -21,19 +21,17 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.common.bytesource.ByteSource;
import org.apache.commons.imaging.common.bytesource.ByteSourceArray;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
Expand Down Expand Up @@ -61,23 +59,21 @@ public void testRemove(final File imageFile) throws Exception {
byteSource, params);
assertNotNull(metadata);

final File noIptcFile = removeIptc(byteSource, imageFile);
final byte[] noIptcFile = removeIptc(byteSource, imageFile);

final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
new ByteSourceFile(noIptcFile), params);
new ByteSourceArray("test.jpg", noIptcFile), params);

// FIXME should either be null or empty
assertTrue(outMetadata == null
|| outMetadata.getItems().isEmpty());
}

public File removeIptc(final ByteSource byteSource, final File imageFile) throws Exception {
final File noIptcFile = Files.createTempFile(imageFile.getName() + ".iptc.remove.", ".jpg").toFile();

try (OutputStream os = new BufferedOutputStream(new FileOutputStream(noIptcFile))) {
public byte[] removeIptc(final ByteSource byteSource, final File imageFile) throws Exception {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
new JpegIptcRewriter().removeIPTC(byteSource, os);
return os.toByteArray();
}
return noIptcFile;
}

@ParameterizedTest
Expand All @@ -91,7 +87,7 @@ public void testInsert(final File imageFile) throws Exception {
byteSource, params);
assertNotNull(metadata);

final File noIptcFile = removeIptc(byteSource, imageFile);
final byte[] noIptcFile = removeIptc(byteSource, imageFile);

final List<IptcBlock> newBlocks = new ArrayList<>();
final List<IptcRecord> newRecords = new ArrayList<>();
Expand All @@ -103,15 +99,14 @@ public void testInsert(final File imageFile) throws Exception {
final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords,
newBlocks);

final File updated = Files.createTempFile(imageFile.getName()
+ ".iptc.insert.", ".jpg").toFile();
try (FileOutputStream fos = new FileOutputStream(updated);
OutputStream os = new BufferedOutputStream(fos)) {
new JpegIptcRewriter().writeIPTC(new ByteSourceFile(
byte[] updated;
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
new JpegIptcRewriter().writeIPTC(new ByteSourceArray("test.jpg",
noIptcFile), os, newData);
updated = os.toByteArray();
}

final ByteSource updateByteSource = new ByteSourceFile(updated);
final ByteSource updateByteSource = new ByteSourceArray("test.jpg", updated);
final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
updateByteSource, params);

Expand Down Expand Up @@ -139,24 +134,21 @@ public void testUpdate(final File imageFile) throws Exception {
final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords,
newBlocks);

final File updated = writeIptc(byteSource, newData, imageFile);
final byte[] updated = writeIptc(byteSource, newData, imageFile);

final ByteSource updateByteSource = new ByteSourceFile(updated);
final ByteSource updateByteSource = new ByteSourceArray("test.jpg", updated);
final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
updateByteSource, params);

assertNotNull(outMetadata);
assertEquals(2, outMetadata.getItems().size());
}

public File writeIptc(final ByteSource byteSource, final PhotoshopApp13Data newData, final File imageFile) throws IOException, ImageReadException, ImageWriteException {
final File updated = Files.createTempFile(imageFile.getName()
+ ".iptc.update.", ".jpg").toFile();
try (FileOutputStream fos = new FileOutputStream(updated);
OutputStream os = new BufferedOutputStream(fos)) {
public byte[] writeIptc(final ByteSource byteSource, final PhotoshopApp13Data newData, final File imageFile) throws IOException, ImageReadException, ImageWriteException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
new JpegIptcRewriter().writeIPTC(byteSource, os, newData);
return os.toByteArray();
}
return updated;
}

@ParameterizedTest
Expand Down Expand Up @@ -184,9 +176,9 @@ public void testNoChangeUpdate(final File imageFile) throws Exception {

final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);

final File updated = writeIptc(byteSource, newData, imageFile);
final byte[] updated = writeIptc(byteSource, newData, imageFile);

final ByteSource updateByteSource = new ByteSourceFile(updated);
final ByteSource updateByteSource = new ByteSourceArray("test.jpg", updated);
final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(updateByteSource, params);

assertNotNull(outMetadata);
Expand Down

0 comments on commit 933584e

Please sign in to comment.