Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8264002: Delete outdated assumptions about ColorSpace initialization #3140

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@

package com.sun.imageio.plugins.common;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
@@ -47,10 +46,9 @@
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import java.awt.image.WritableRaster;
import java.util.Arrays;
import java.util.Iterator;

import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
@@ -1162,30 +1160,17 @@ public static final boolean imageIsContiguous(RenderedImage image) {
}

/**
* Returns <code>true</code> if the given <code>ColorSpace</code> object is
* an instance of <code>ICC_ColorSpace</code> but is not one of the standard
* <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>.
* Returns {@code true} if the given {@code ColorSpace} object is an
* instance of {@code ICC_ColorSpace} but is not one of the standard
* {@code ColorSpace}s returned by {@code ColorSpace.getInstance()}.
*
* @param cs The <code>ColorSpace</code> to test.
* @param cs the {@code ColorSpace} to test
*/
public static boolean isNonStandardICCColorSpace(ColorSpace cs) {
boolean retval = false;

try {
// Check the standard ColorSpaces in decreasing order of
// likelihood except check CS_PYCC last as in some JREs
// PYCC.pf used not to be installed.
retval =
(cs instanceof ICC_ColorSpace) &&
!(cs.isCS_sRGB() ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) ||
cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)));
} catch(IllegalArgumentException e) {
// PYCC.pf not installed: ignore it - 'retval' is still 'false'.
}

return retval;
return cs instanceof ICC_ColorSpace && !cs.isCS_sRGB()
&& !cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB))
&& !cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY))
&& !cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ))
&& !cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC));
}
Copy link
Member Author

@mrserb mrserb Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method above was added by the tiff implementation in jdk9 and it is mostly a copy of the older code in the jpeg plugin. I merged two methods and leave it here in the "common" code.

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1001,7 +1001,7 @@ BufferedImage getThumbnail(ImageInputStream iis,
3,
new int [] {0, 1, 2},
null);
ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
ColorModel cm = new ComponentColorModel(JPEG.sRGB,
false,
false,
ColorModel.OPAQUE,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,13 +25,12 @@

package com.sun.imageio.plugins.jpeg;

import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;

import javax.imageio.ImageTypeSpecifier;
import javax.imageio.plugins.jpeg.JPEGQTable;
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;

import java.awt.image.ColorModel;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import javax.imageio.plugins.jpeg.JPEGQTable;

/**
* A class containing JPEG-related constants, definitions, and
@@ -199,39 +198,11 @@ public class JPEG {

static final int [] bOffsRGB = { 2, 1, 0 };

/* These are kept in the inner class to avoid static initialization
* of the CMM class until someone actually needs it.
* (e.g. do not init CMM on the request for jpeg mime types)
*/
Copy link
Member Author

@mrserb mrserb Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to delay the usage of ColorSpace, it itself defers the usage of cmm.

public static class JCS {
public static final ColorSpace sRGB =
ColorSpace.getInstance(ColorSpace.CS_sRGB);
}
static final ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);

// Default value for ImageWriteParam
public static final float DEFAULT_QUALITY = 0.75F;

/**
* Returns {@code true} if the given {@code ColorSpace}
* object is an instance of ICC_ColorSpace but is not one of the
* standard {@code ColorSpaces} returned by
* {@code ColorSpace.getInstance()}.
*/
static boolean isNonStandardICC(ColorSpace cs) {
boolean retval = false;
if ((cs instanceof ICC_ColorSpace)
&& (!cs.isCS_sRGB())
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
&& (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
) {
retval = true;
}
return retval;
}


/**
* Returns {@code true} if the given imageType can be used
* in a JFIF file. If {@code input} is true, then the
@@ -342,5 +313,4 @@ static float convertToLinearQuality(float quality) {
}
return tables;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1047,7 +1047,7 @@ private void checkColorConversion(BufferedImage image,
(!cs.isCS_sRGB()) &&
(cm.getNumComponents() == numComponents)) {
// Target isn't sRGB, so convert from sRGB to the target
convert = new ColorConvertOp(JPEG.JCS.sRGB, cs, null);
convert = new ColorConvertOp(JPEG.sRGB, cs, null);
} else if (csType != ColorSpace.TYPE_RGB) {
throw new IIOException("Incompatible color conversion");
}
@@ -1898,7 +1898,7 @@ protected ImageTypeSpecifier produce() {
case JPEG.JCS_YCbCr:
//there is no YCbCr raw type so by default we assume it as RGB
case JPEG.JCS_RGB:
return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
return ImageTypeSpecifier.createInterleaved(JPEG.sRGB,
JPEG.bOffsRGB,
DataBuffer.TYPE_BYTE,
false,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,43 +25,41 @@

package com.sun.imageio.plugins.jpeg;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.IIOException;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import javax.imageio.IIOImage;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.plugins.jpeg.JPEGQTable;
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageOutputStream;

import com.sun.imageio.plugins.common.ImageUtil;
import org.w3c.dom.Node;

import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.ColorConvertOp;
import java.awt.image.RenderedImage;
import java.awt.image.BufferedImage;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Transparency;

import java.io.IOException;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

@@ -687,7 +685,7 @@ private void writeOnThread(IIOMetadata streamMetadata,
checkJFIF(jfif, destType, false);
// Do we want to write an ICC profile?
if ((jfif != null) && (ignoreJFIF == false)) {
if (JPEG.isNonStandardICC(cs)) {
if (ImageUtil.isNonStandardICCColorSpace(cs)) {
iccProfile = ((ICC_ColorSpace) cs).getProfile();
}
}
@@ -698,7 +696,7 @@ private void writeOnThread(IIOMetadata streamMetadata,
if (JPEG.isJFIFcompliant(destType, false)) {
writeDefaultJFIF = true;
// Do we want to write an ICC profile?
if (JPEG.isNonStandardICC(cs)) {
if (ImageUtil.isNonStandardICCColorSpace(cs)) {
iccProfile = ((ICC_ColorSpace) cs).getProfile();
}
} else {
@@ -722,7 +720,7 @@ private void writeOnThread(IIOMetadata streamMetadata,
if (metadata.findMarkerSegment
(JFIFMarkerSegment.class, true) != null) {
cs = rimage.getColorModel().getColorSpace();
if (JPEG.isNonStandardICC(cs)) {
if (ImageUtil.isNonStandardICCColorSpace(cs)) {
iccProfile = ((ICC_ColorSpace) cs).getProfile();
}
}
@@ -766,7 +764,7 @@ private void writeOnThread(IIOMetadata streamMetadata,
case ColorSpace.TYPE_RGB:
if (jfif != null) {
outCsType = JPEG.JCS_YCbCr;
if (JPEG.isNonStandardICC(cs)
if (ImageUtil.isNonStandardICCColorSpace(cs)
|| ((cs instanceof ICC_ColorSpace)
&& (jfif.iccSegment != null))) {
iccProfile =
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,36 +25,34 @@

package com.sun.imageio.plugins.jpeg;

import java.awt.Point;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.imageio.IIOException;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.IIOException;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.metadata.IIOMetadataFormat;
import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.plugins.jpeg.JPEGQTable;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

import com.sun.imageio.plugins.common.ImageUtil;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
import java.io.IOException;
import java.awt.color.ICC_Profile;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.Point;

/**
* Metadata for the JPEG plug-in.
@@ -576,7 +574,7 @@ public class JPEGMetadata extends IIOMetadata implements Cloneable {
}

// do we want an ICC profile?
if (wantJFIF && JPEG.isNonStandardICC(cs)) {
if (wantJFIF && ImageUtil.isNonStandardICCColorSpace(cs)) {
wantICC = true;
}