Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/org/apache/commons/imaging/AbstractImageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,24 @@ public final ImageMetadata getMetadata(final File file, final T params) throws I
public void writeImage(final BufferedImage src, final OutputStream os, final T params) throws ImagingException, IOException {
throw new ImagingException("This image format (" + getName() + ") cannot be written.");
}

/**
* Writes the content of a BufferedImage to the specified output stream in the given format.
*
* <p>
* A parser whose {@link #getAcceptedTypes()} lists more than one format overrides this method so that the format selects the output; the rest write their
* only format and ignore the argument.
* </p>
*
* @param src An image giving the source content for output.
* @param os A valid output stream for storing the formatted image.
* @param format The format the caller asked for; one of {@link #getAcceptedTypes()}.
* @throws ImagingException In the event that the output format cannot handle the input image.
* @throws IOException In the event of a write error from the output stream.
*
* @since 1.0.0-alpha7
*/
protected void writeImageForFormat(final BufferedImage src, final OutputStream os, final ImageFormat format) throws ImagingException, IOException {
writeImage(src, os, null);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/imaging/Imaging.java
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ public static void writeImage(final BufferedImage src, final OutputStream output
Objects.requireNonNull(format, "format");

final AbstractImageParser<?> imageParser = ImageParserFactory.getImageParser(format);
imageParser.writeImage(src, outputStream, null);
imageParser.writeImageForFormat(src, outputStream, format);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,13 @@ public void writeImage(final BufferedImage src, final OutputStream os, final Pnm

writer.writeImage(src, os, params);
}

@Override
protected void writeImageForFormat(final BufferedImage src, final OutputStream os, final ImageFormat format) throws ImagingException, IOException {
final PnmImagingParameters params = new PnmImagingParameters();
if (format instanceof ImageFormats) {
params.setSubtype((ImageFormats) format);
}
writeImage(src, os, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -36,6 +37,18 @@ class PnmImageParserTest {

private static final Charset US_ASCII = StandardCharsets.US_ASCII;

@Test
void testWriteImageHonorsRequestedFormat() throws ImagingException, IOException {
final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
final ImageFormats[] formats = { ImageFormats.PBM, ImageFormats.PGM, ImageFormats.PPM, ImageFormats.PAM };
final String[] magic = { "P4", "P5", "P6", "P7" };
for (int i = 0; i < formats.length; i++) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Imaging.writeImage(image, out, formats[i]);
assertEquals(magic[i], new String(out.toByteArray(), 0, 2, US_ASCII), formats[i].name());
}
}

@Test
void testGetImageInfo_happyCase() throws ImagingException, IOException {
final byte[] bytes = "P1\n3 2\n0 1 0\n1 0 1\n".getBytes(US_ASCII);
Expand Down