1 /* |
1 /* |
2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
27 |
27 |
28 import java.awt.Point; |
28 import java.awt.Point; |
29 import java.awt.Rectangle; |
29 import java.awt.Rectangle; |
30 import java.awt.Transparency; |
30 import java.awt.Transparency; |
31 import java.awt.color.ColorSpace; |
31 import java.awt.color.ColorSpace; |
|
32 import java.awt.color.ICC_ColorSpace; |
32 import java.awt.image.BufferedImage; |
33 import java.awt.image.BufferedImage; |
33 import java.awt.image.ColorModel; |
34 import java.awt.image.ColorModel; |
34 import java.awt.image.ComponentColorModel; |
35 import java.awt.image.ComponentColorModel; |
35 import java.awt.image.ComponentSampleModel; |
36 import java.awt.image.ComponentSampleModel; |
36 import java.awt.image.DataBuffer; |
37 import java.awt.image.DataBuffer; |
45 import java.awt.image.RenderedImage; |
46 import java.awt.image.RenderedImage; |
46 import java.awt.image.SampleModel; |
47 import java.awt.image.SampleModel; |
47 import java.awt.image.SinglePixelPackedSampleModel; |
48 import java.awt.image.SinglePixelPackedSampleModel; |
48 import java.awt.image.WritableRaster; |
49 import java.awt.image.WritableRaster; |
49 import java.util.Arrays; |
50 import java.util.Arrays; |
50 |
51 import java.util.Iterator; |
51 //import javax.imageio.ImageTypeSpecifier; |
|
52 |
|
53 import javax.imageio.IIOException; |
52 import javax.imageio.IIOException; |
54 import javax.imageio.IIOImage; |
53 import javax.imageio.IIOImage; |
|
54 import javax.imageio.ImageReadParam; |
55 import javax.imageio.ImageTypeSpecifier; |
55 import javax.imageio.ImageTypeSpecifier; |
56 import javax.imageio.ImageWriter; |
56 import javax.imageio.ImageWriter; |
57 import javax.imageio.spi.ImageWriterSpi; |
57 import javax.imageio.spi.ImageWriterSpi; |
58 |
58 |
59 public class ImageUtil { |
59 public class ImageUtil { |
60 /* XXX testing only |
|
61 public static void main(String[] args) { |
|
62 ImageTypeSpecifier bilevel = |
|
63 ImageTypeSpecifier.createIndexed(new byte[] {(byte)0, (byte)255}, |
|
64 new byte[] {(byte)0, (byte)255}, |
|
65 new byte[] {(byte)0, (byte)255}, |
|
66 null, 1, |
|
67 DataBuffer.TYPE_BYTE); |
|
68 ImageTypeSpecifier gray = |
|
69 ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false); |
|
70 ImageTypeSpecifier grayAlpha = |
|
71 ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false, |
|
72 false); |
|
73 ImageTypeSpecifier rgb = |
|
74 ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB), |
|
75 new int[] {0, 1, 2}, |
|
76 DataBuffer.TYPE_BYTE, |
|
77 false, |
|
78 false); |
|
79 ImageTypeSpecifier rgba = |
|
80 ImageTypeSpecifier.createInterleaved(ColorSpace.getInstance(ColorSpace.CS_sRGB), |
|
81 new int[] {0, 1, 2, 3}, |
|
82 DataBuffer.TYPE_BYTE, |
|
83 true, |
|
84 false); |
|
85 ImageTypeSpecifier packed = |
|
86 ImageTypeSpecifier.createPacked(ColorSpace.getInstance(ColorSpace.CS_sRGB), |
|
87 0xff000000, |
|
88 0x00ff0000, |
|
89 0x0000ff00, |
|
90 0x000000ff, |
|
91 DataBuffer.TYPE_BYTE, |
|
92 false); |
|
93 |
|
94 SampleModel bandedSM = |
|
95 new java.awt.image.BandedSampleModel(DataBuffer.TYPE_BYTE, |
|
96 1, 1, 15); |
|
97 |
|
98 System.out.println(createColorModel(bilevel.getSampleModel())); |
|
99 System.out.println(createColorModel(gray.getSampleModel())); |
|
100 System.out.println(createColorModel(grayAlpha.getSampleModel())); |
|
101 System.out.println(createColorModel(rgb.getSampleModel())); |
|
102 System.out.println(createColorModel(rgba.getSampleModel())); |
|
103 System.out.println(createColorModel(packed.getSampleModel())); |
|
104 System.out.println(createColorModel(bandedSM)); |
|
105 } |
|
106 */ |
|
107 |
|
108 /** |
60 /** |
109 * Creates a <code>ColorModel</code> that may be used with the |
61 * Creates a <code>ColorModel</code> that may be used with the |
110 * specified <code>SampleModel</code>. If a suitable |
62 * specified <code>SampleModel</code>. If a suitable |
111 * <code>ColorModel</code> cannot be found, this method returns |
63 * <code>ColorModel</code> cannot be found, this method returns |
112 * <code>null</code>. |
64 * <code>null</code>. |
1160 // Otherwise true if and only if it's a bilevel image with |
1112 // Otherwise true if and only if it's a bilevel image with |
1161 // a MultiPixelPackedSampleModel, 1 bit per pixel, and 1 bit |
1113 // a MultiPixelPackedSampleModel, 1 bit per pixel, and 1 bit |
1162 // pixel stride. |
1114 // pixel stride. |
1163 return ImageUtil.isBinary(sm); |
1115 return ImageUtil.isBinary(sm); |
1164 } |
1116 } |
|
1117 |
|
1118 /** |
|
1119 * Gets the destination image type. |
|
1120 */ |
|
1121 public static final ImageTypeSpecifier |
|
1122 getDestinationType(ImageReadParam param, |
|
1123 Iterator<ImageTypeSpecifier> imageTypes) throws IIOException { |
|
1124 |
|
1125 if (imageTypes == null || !imageTypes.hasNext()) { |
|
1126 throw new IllegalArgumentException("imageTypes null or empty!"); |
|
1127 } |
|
1128 |
|
1129 ImageTypeSpecifier imageType = null; |
|
1130 |
|
1131 // If param is non-null, use it |
|
1132 if (param != null) { |
|
1133 imageType = param.getDestinationType(); |
|
1134 } |
|
1135 |
|
1136 // No info from param, use fallback image type |
|
1137 if (imageType == null) { |
|
1138 Object o = imageTypes.next(); |
|
1139 if (!(o instanceof ImageTypeSpecifier)) { |
|
1140 throw new IllegalArgumentException |
|
1141 ("Non-ImageTypeSpecifier retrieved from imageTypes!"); |
|
1142 } |
|
1143 imageType = (ImageTypeSpecifier)o; |
|
1144 } else { |
|
1145 boolean foundIt = false; |
|
1146 while (imageTypes.hasNext()) { |
|
1147 ImageTypeSpecifier type = |
|
1148 imageTypes.next(); |
|
1149 if (type.equals(imageType)) { |
|
1150 foundIt = true; |
|
1151 break; |
|
1152 } |
|
1153 } |
|
1154 |
|
1155 if (!foundIt) { |
|
1156 throw new IIOException |
|
1157 ("Destination type from ImageReadParam does not match!"); |
|
1158 } |
|
1159 } |
|
1160 |
|
1161 return imageType; |
|
1162 } |
|
1163 |
|
1164 /** |
|
1165 * Returns <code>true</code> if the given <code>ColorSpace</code> object is |
|
1166 * an instance of <code>ICC_ColorSpace</code> but is not one of the standard |
|
1167 * <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>. |
|
1168 * |
|
1169 * @param cs The <code>ColorSpace</code> to test. |
|
1170 */ |
|
1171 public static boolean isNonStandardICCColorSpace(ColorSpace cs) { |
|
1172 boolean retval = false; |
|
1173 |
|
1174 try { |
|
1175 // Check the standard ColorSpaces in decreasing order of |
|
1176 // likelihood except check CS_PYCC last as in some JREs |
|
1177 // PYCC.pf used not to be installed. |
|
1178 retval = |
|
1179 (cs instanceof ICC_ColorSpace) && |
|
1180 !(cs.isCS_sRGB() || |
|
1181 cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) || |
|
1182 cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) || |
|
1183 cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) || |
|
1184 cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC))); |
|
1185 } catch(IllegalArgumentException e) { |
|
1186 // PYCC.pf not installed: ignore it - 'retval' is still 'false'. |
|
1187 } |
|
1188 |
|
1189 return retval; |
|
1190 } |
1165 } |
1191 } |