Skip to content

Commit

Permalink
codec: add a null check in ImageIOJPEGImageWriter, other small impr…
Browse files Browse the repository at this point in the history
…ovements
  • Loading branch information
carlosame committed Jul 19, 2024
1 parent bface85 commit 94c63db
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,11 @@ public ImageIOImageWriter(String mime) {
this.targetMIME = mime;
}

/**
* @see ImageWriter#writeImage(java.awt.image.RenderedImage,
* java.io.OutputStream)
*/
@Override
public void writeImage(RenderedImage image, OutputStream out) throws IOException {
writeImage(image, out, null);
}

/**
* @see ImageWriter#writeImage(java.awt.image.RenderedImage,
* java.io.OutputStream, ImageWriterParams)
*/
@Override
public void writeImage(RenderedImage image, OutputStream out, ImageWriterParams params) throws IOException {
Iterator<javax.imageio.ImageWriter> iter = ImageIO.getImageWritersByMIMEType(getMIMEType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ private static IIOMetadata addAdobeTransform(IIOMetadata meta) {
protected ImageWriteParam getDefaultWriteParam(ImageWriter iiowriter, RenderedImage image,
ImageWriterParams params) {
JPEGImageWriteParam param = new JPEGImageWriteParam(iiowriter.getLocale());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(params.getJPEGQuality());
if (params.getCompressionMethod() != null && !"JPEG".equals(params.getCompressionMethod())) {
throw new IllegalArgumentException("No compression method other than JPEG is supported for JPEG output!");
}
if (params.getJPEGForceBaseline()) {
param.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
if (params != null) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(params.getJPEGQuality());
if (params.getCompressionMethod() != null && !"JPEG".equals(params.getCompressionMethod())) {
throw new IllegalArgumentException(
"No compression method other than JPEG is supported for JPEG output!");
}
if (params.getJPEGForceBaseline()) {
param.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
}
}
return param;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;

import io.sf.carte.echosvg.ext.awt.image.codec.impl.ColorUtil;

/**
* ImageWriter that encodes PNG images using Image I/O.
*
Expand All @@ -47,7 +49,7 @@ public ImageIOPNGImageWriter() {

@Override
protected void updateColorMetadata(IIOMetadata meta, ColorSpace colorSpace) {
if (!colorSpace.isCS_sRGB() && colorSpace instanceof ICC_ColorSpace
if (!ColorUtil.isBuiltInColorSpace(colorSpace) && colorSpace instanceof ICC_ColorSpace
&& meta.isStandardMetadataFormatSupported()) {
final String metaName = "javax_imageio_png_1.0";
IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(metaName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
See the NOTICE file distributed with this work for additional
information regarding copyright ownership.
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 io.sf.carte.echosvg.ext.awt.image.codec.impl;

import java.awt.color.ColorSpace;

/**
* Color utilities.
*
* @version $Id$
*/
public class ColorUtil {

/**
* Avoid instantiation.
*/
ColorUtil() {
super();
}

/**
*
* @param colorSpace the color space.
* @return {@code true} if it is a built-in color space.
*/
public static boolean isBuiltInColorSpace(ColorSpace colorSpace) {
return colorSpace.isCS_sRGB() || colorSpace == ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)
|| colorSpace == ColorSpace.getInstance(ColorSpace.CS_GRAY)
|| colorSpace == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)
|| colorSpace == ColorSpace.getInstance(ColorSpace.CS_PYCC);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
See the NOTICE file distributed with this work for additional
information regarding copyright ownership.
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.
*/
/**
* Implementation classes, do not use outside of the codec module.
*/
package io.sf.carte.echosvg.ext.awt.image.codec.impl;
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

import io.sf.carte.echosvg.ext.awt.image.codec.impl.ColorUtil;
import io.sf.carte.echosvg.ext.awt.image.codec.util.ImageEncoderImpl;
import io.sf.carte.echosvg.ext.awt.image.codec.util.PropertyUtil;

Expand Down Expand Up @@ -1093,7 +1094,7 @@ public void encode(RenderedImage im) throws IOException {

private void setICCProfileInfo(ColorModel colorModel) {
ColorSpace cs = colorModel.getColorSpace();
if (!cs.isCS_sRGB() && cs instanceof ICC_ColorSpace) {
if (!ColorUtil.isBuiltInColorSpace(cs) && cs instanceof ICC_ColorSpace) {
ICC_Profile profile = ((ICC_ColorSpace) cs).getProfile();
byte[] bdesc = profile.getData(ICC_Profile.icSigProfileDescriptionTag);
/*
Expand All @@ -1113,13 +1114,13 @@ private void setICCProfileInfo(ColorModel colorModel) {
String desc = new String(bdesc, offset, len, StandardCharsets.UTF_16BE).trim();
iccProfileName = desc;
iccProfileData = profile.getData();
return;
}
}
}
} else {
iccProfileName = null;
iccProfileData = null;
}
iccProfileName = null;
iccProfileData = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static String getProjectBuildURL(Class<?> cl, String projectDirname) {
String classUrl;
if (url == null) {
url = cwdURL();
File f = new File(url.getFile(), projectDirname);
File f = new File(url.getFile(), projectDirname);
if (f.exists()) {
// CWD is root directory
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* comparisons with an arbitrary number of variants to match. See
* {@link ImageVariants} for a description of the variants mechanism.
* </p>
* <p>
* This software was written for image comparisons in the context of the EchoSVG
* project. Usage outside of that context is not supported.
* </p>
*/
public class ImageComparator {

Expand Down Expand Up @@ -619,7 +623,7 @@ public static String getResultDescription(short code) {
* @return an image comparing the two given images, one on each side.
*/
public static BufferedImage createCompareImage(BufferedImage ref, BufferedImage gen) {
BufferedImage cmp = new BufferedImage(ref.getWidth() * 2, ref.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage cmp = createImageOfType(ref.getWidth() * 2, ref.getHeight(), ref);

Graphics2D g = cmp.createGraphics();
g.setPaint(Color.white);
Expand Down Expand Up @@ -650,7 +654,8 @@ public static BufferedImage createDiffImage(BufferedImage ref, BufferedImage gen
final int w = ref.getWidth();
final int h = ref.getHeight();

BufferedImage diff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
BufferedImage diff = createImageOfType(w, h, ref);

WritableRaster refWR = ref.getRaster();
WritableRaster genWR = gen.getRaster();
WritableRaster dstWR = diff.getRaster();
Expand Down Expand Up @@ -701,6 +706,21 @@ public static BufferedImage createDiffImage(BufferedImage ref, BufferedImage gen
return diff;
}

private static BufferedImage createImageOfType(int width, int height, BufferedImage ref) {
BufferedImage image;

int type = ref.getType();
if (type != BufferedImage.TYPE_CUSTOM) {
image = new BufferedImage(width, height, type);
} else {
ColorModel cm = ref.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
image = new BufferedImage(cm, raster, false, null);
}

return image;
}

/**
* Creates a new image that is the exact difference between the two input
* images.
Expand All @@ -722,7 +742,8 @@ public static BufferedImage createExactDiffImage(BufferedImage ref, BufferedImag
"Ref. image has height " + h + " but generated image is " + gen.getHeight());
}

BufferedImage diff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
BufferedImage diff = createImageOfType(w, h, ref);

WritableRaster refWR = ref.getRaster();
WritableRaster genWR = gen.getRaster();
WritableRaster dstWR = diff.getRaster();
Expand Down Expand Up @@ -782,7 +803,8 @@ public static BufferedImage createMergedDiffImage(BufferedImage ref, BufferedIma
final int w = ref.getWidth();
final int h = ref.getHeight();

BufferedImage diff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
BufferedImage diff = createImageOfType(w, h, ref);

WritableRaster refWR = ref.getRaster();
WritableRaster genWR = gen.getRaster();
WritableRaster rangeWR = rangeDiff.getRaster();
Expand Down
Loading

0 comments on commit 94c63db

Please sign in to comment.