19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.imageio.plugins.jpeg;
27
28 import javax.imageio.ImageTypeSpecifier;
29 import javax.imageio.plugins.jpeg.JPEGQTable;
30 import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
31
32 import java.awt.image.ColorModel;
33 import java.awt.color.ColorSpace;
34 import java.awt.color.ICC_ColorSpace;
35
36 /**
37 * A class containing JPEG-related constants, definitions, and
38 * static methods. This class and its constants must be public so that
39 * <code>JPEGImageWriteParam</code> can see it.
40 */
41 public class JPEG {
42
43 // List of all the JPEG markers (pre-JPEG2000)
44
45 /** For temporary use in arithmetic coding */
46 public static final int TEM = 0x01;
47
48 // Codes 0x02 - 0xBF are reserved
49
50 // SOF markers for Nondifferential Huffman coding
51 /** Baseline DCT */
52 public static final int SOF0 = 0xC0;
53 /** Extended Sequential DCT */
54 public static final int SOF1 = 0xC1;
55 /** Progressive DCT */
56 public static final int SOF2 = 0xC2;
57 /** Lossless Sequential */
58 public static final int SOF3 = 0xC3;
59
217 private static boolean yccInited = false;
218
219 public static ColorSpace getYCC() {
220 if (!yccInited) {
221 try {
222 YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
223 } catch (IllegalArgumentException e) {
224 // PYCC.pf may not always be installed
225 } finally {
226 yccInited = true;
227 }
228 }
229 return YCC;
230 }
231 }
232
233 // Default value for ImageWriteParam
234 public static final float DEFAULT_QUALITY = 0.75F;
235
236 /**
237 * Returns <code>true</code> if the given <code>ColorSpace</code>
238 * object is an instance of ICC_ColorSpace but is not one of the
239 * standard <code>ColorSpaces</code> returned by
240 * <code>ColorSpace.getInstance()</code>.
241 */
242 static boolean isNonStandardICC(ColorSpace cs) {
243 boolean retval = false;
244 if ((cs instanceof ICC_ColorSpace)
245 && (!cs.isCS_sRGB())
246 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
247 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
248 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
249 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
250 ) {
251 retval = true;
252 }
253 return retval;
254 }
255
256
257 /**
258 * Returns <code>true</code> if the given imageType can be used
259 * in a JFIF file. If <code>input</code> is true, then the
260 * image type is considered before colorspace conversion.
261 */
262 static boolean isJFIFcompliant(ImageTypeSpecifier imageType,
263 boolean input) {
264 ColorModel cm = imageType.getColorModel();
265 // Can't have alpha
266 if (cm.hasAlpha()) {
267 return false;
268 }
269 // Gray is OK, always
270 int numComponents = imageType.getNumComponents();
271 if (numComponents == 1) {
272 return true;
273 }
274
275 // If it isn't gray, it must have 3 channels
276 if (numComponents != 3) {
277 return false;
278 }
279
280 if (input) {
281 // Must be RGB
282 if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
283 return true;
284 }
285 } else {
286 // Must be YCbCr
287 if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) {
288 return true;
289 }
290 }
291
292 return false;
293 }
294
295 /**
296 * Given an image type, return the Adobe transform corresponding to
297 * that type, or ADOBE_IMPOSSIBLE if the image type is incompatible
298 * with an Adobe marker segment. If <code>input</code> is true, then
299 * the image type is considered before colorspace conversion.
300 */
301 static int transformForType(ImageTypeSpecifier imageType, boolean input) {
302 int retval = ADOBE_IMPOSSIBLE;
303 ColorModel cm = imageType.getColorModel();
304 switch (cm.getColorSpace().getType()) {
305 case ColorSpace.TYPE_GRAY:
306 retval = ADOBE_UNKNOWN;
307 break;
308 case ColorSpace.TYPE_RGB:
309 retval = input ? ADOBE_YCC : ADOBE_UNKNOWN;
310 break;
311 case ColorSpace.TYPE_YCbCr:
312 retval = ADOBE_YCC;
313 break;
314 case ColorSpace.TYPE_CMYK:
315 retval = input ? ADOBE_YCCK : ADOBE_IMPOSSIBLE;
316 }
317 return retval;
318 }
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.imageio.plugins.jpeg;
27
28 import javax.imageio.ImageTypeSpecifier;
29 import javax.imageio.plugins.jpeg.JPEGQTable;
30 import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
31
32 import java.awt.image.ColorModel;
33 import java.awt.color.ColorSpace;
34 import java.awt.color.ICC_ColorSpace;
35
36 /**
37 * A class containing JPEG-related constants, definitions, and
38 * static methods. This class and its constants must be public so that
39 * {@code JPEGImageWriteParam} can see it.
40 */
41 public class JPEG {
42
43 // List of all the JPEG markers (pre-JPEG2000)
44
45 /** For temporary use in arithmetic coding */
46 public static final int TEM = 0x01;
47
48 // Codes 0x02 - 0xBF are reserved
49
50 // SOF markers for Nondifferential Huffman coding
51 /** Baseline DCT */
52 public static final int SOF0 = 0xC0;
53 /** Extended Sequential DCT */
54 public static final int SOF1 = 0xC1;
55 /** Progressive DCT */
56 public static final int SOF2 = 0xC2;
57 /** Lossless Sequential */
58 public static final int SOF3 = 0xC3;
59
217 private static boolean yccInited = false;
218
219 public static ColorSpace getYCC() {
220 if (!yccInited) {
221 try {
222 YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
223 } catch (IllegalArgumentException e) {
224 // PYCC.pf may not always be installed
225 } finally {
226 yccInited = true;
227 }
228 }
229 return YCC;
230 }
231 }
232
233 // Default value for ImageWriteParam
234 public static final float DEFAULT_QUALITY = 0.75F;
235
236 /**
237 * Returns {@code true} if the given {@code ColorSpace}
238 * object is an instance of ICC_ColorSpace but is not one of the
239 * standard {@code ColorSpaces} returned by
240 * {@code ColorSpace.getInstance()}.
241 */
242 static boolean isNonStandardICC(ColorSpace cs) {
243 boolean retval = false;
244 if ((cs instanceof ICC_ColorSpace)
245 && (!cs.isCS_sRGB())
246 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
247 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
248 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
249 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
250 ) {
251 retval = true;
252 }
253 return retval;
254 }
255
256
257 /**
258 * Returns {@code true} if the given imageType can be used
259 * in a JFIF file. If {@code input} is true, then the
260 * image type is considered before colorspace conversion.
261 */
262 static boolean isJFIFcompliant(ImageTypeSpecifier imageType,
263 boolean input) {
264 ColorModel cm = imageType.getColorModel();
265 // Can't have alpha
266 if (cm.hasAlpha()) {
267 return false;
268 }
269 // Gray is OK, always
270 int numComponents = imageType.getNumComponents();
271 if (numComponents == 1) {
272 return true;
273 }
274
275 // If it isn't gray, it must have 3 channels
276 if (numComponents != 3) {
277 return false;
278 }
279
280 if (input) {
281 // Must be RGB
282 if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
283 return true;
284 }
285 } else {
286 // Must be YCbCr
287 if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) {
288 return true;
289 }
290 }
291
292 return false;
293 }
294
295 /**
296 * Given an image type, return the Adobe transform corresponding to
297 * that type, or ADOBE_IMPOSSIBLE if the image type is incompatible
298 * with an Adobe marker segment. If {@code input} is true, then
299 * the image type is considered before colorspace conversion.
300 */
301 static int transformForType(ImageTypeSpecifier imageType, boolean input) {
302 int retval = ADOBE_IMPOSSIBLE;
303 ColorModel cm = imageType.getColorModel();
304 switch (cm.getColorSpace().getType()) {
305 case ColorSpace.TYPE_GRAY:
306 retval = ADOBE_UNKNOWN;
307 break;
308 case ColorSpace.TYPE_RGB:
309 retval = input ? ADOBE_YCC : ADOBE_UNKNOWN;
310 break;
311 case ColorSpace.TYPE_YCbCr:
312 retval = ADOBE_YCC;
313 break;
314 case ColorSpace.TYPE_CMYK:
315 retval = input ? ADOBE_YCCK : ADOBE_IMPOSSIBLE;
316 }
317 return retval;
318 }
|