jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEG.java
author bae
Sat, 23 May 2009 08:35:37 +0400
changeset 3009 de653b2cab31
parent 2381 0afc5d868c46
child 5506 202f599c92aa
permissions -rw-r--r--
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY Reviewed-by: igor, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2002 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.imageio.plugins.jpeg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.imageio.metadata.IIOMetadataFormatImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.imageio.plugins.jpeg.JPEGQTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.color.ICC_ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A class containing JPEG-related constants, definitions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * static methods.  This class and its constants must be public so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>JPEGImageWriteParam</code> can see it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class JPEG {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // List of all the JPEG markers (pre-JPEG2000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** For temporary use in arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final int TEM = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Codes 0x02 - 0xBF are reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // SOF markers for Nondifferential Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** Baseline DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int SOF0 = 0xC0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Extended Sequential DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int SOF1 = 0xC1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** Progressive DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int SOF2 = 0xC2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** Lossless Sequential */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int SOF3 = 0xC3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** Define Huffman Tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final int DHT = 0xC4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // SOF markers for Differential Huffman coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** Differential Sequential DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final int SOF5 = 0xC5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** Differential Progressive DCT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int SOF6 = 0xC6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Differential Lossless */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int SOF7 = 0xC7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** Reserved for JPEG extensions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int JPG = 0xC8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // SOF markers for Nondifferential arithmetic coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Extended Sequential DCT, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int SOF9 = 0xC9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** Progressive DCT, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int SOF10 = 0xCA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** Lossless Sequential, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static final int SOF11 = 0xCB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** Define Arithmetic conditioning tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int DAC = 0xCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // SOF markers for Differential arithmetic coding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Differential Sequential DCT, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int SOF13 = 0xCD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** Differential Progressive DCT, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static final int SOF14 = 0xCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Differential Lossless, Arithmetic coding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final int SOF15 = 0xCF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Restart Markers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final int RST0 = 0xD0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static final int RST1 = 0xD1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final int RST2 = 0xD2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final int RST3 = 0xD3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final int RST4 = 0xD4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final int RST5 = 0xD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final int RST6 = 0xD6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final int RST7 = 0xD7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** Number of restart markers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final int RESTART_RANGE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /** Start of Image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int SOI = 0xD8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /** End of Image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static final int EOI = 0xD9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** Start of Scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static final int SOS = 0xDA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** Define Quantisation Tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static final int DQT = 0xDB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /** Define Number of lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final int DNL = 0xDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** Define Restart Interval */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static final int DRI = 0xDD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** Define Heirarchical progression */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final int DHP = 0xDE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** Expand reference image(s) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static final int EXP = 0xDF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // Application markers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /** APP0 used by JFIF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static final int APP0 = 0xE0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static final int APP1 = 0xE1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final int APP2 = 0xE2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static final int APP3 = 0xE3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static final int APP4 = 0xE4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public static final int APP5 = 0xE5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static final int APP6 = 0xE6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static final int APP7 = 0xE7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static final int APP8 = 0xE8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static final int APP9 = 0xE9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static final int APP10 = 0xEA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static final int APP11 = 0xEB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static final int APP12 = 0xEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static final int APP13 = 0xED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /** APP14 used by Adobe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static final int APP14 = 0xEE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static final int APP15 = 0xEF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // codes 0xF0 to 0xFD are reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /** Comment marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static final int COM = 0xFE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // JFIF Resolution units
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** The X and Y units simply indicate the aspect ratio of the pixels. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static final int DENSITY_UNIT_ASPECT_RATIO = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /** Pixel density is in pixels per inch. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static final int DENSITY_UNIT_DOTS_INCH    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /** Pixel density is in pixels per centemeter. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static final int DENSITY_UNIT_DOTS_CM      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /** The max known value for DENSITY_UNIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static final int NUM_DENSITY_UNIT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // Adobe transform values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public static final int ADOBE_IMPOSSIBLE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static final int ADOBE_UNKNOWN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public static final int ADOBE_YCC = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static final int ADOBE_YCCK = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // Spi initialization stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public static final String vendor = "Sun Microsystems, Inc.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static final String version = "0.5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Names of the formats we can read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static final String [] names = {"JPEG", "jpeg", "JPG", "jpg"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public static final String [] suffixes = {"jpg", "jpeg"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static final String [] MIMETypes = {"image/jpeg"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static final String nativeImageMetadataFormatName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        "javax_imageio_jpeg_image_1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public static final String nativeImageMetadataFormatClassName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        "com.sun.imageio.plugins.jpeg.JPEGImageMetadataFormat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static final String nativeStreamMetadataFormatName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        "javax_imageio_jpeg_stream_1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static final String nativeStreamMetadataFormatClassName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        "com.sun.imageio.plugins.jpeg.JPEGStreamMetadataFormat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // IJG Color codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public static final int JCS_UNKNOWN = 0;       // error/unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static final int JCS_GRAYSCALE = 1;     // monochrome
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static final int JCS_RGB = 2;           // red/green/blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static final int JCS_YCbCr = 3;         // Y/Cb/Cr (also known as YUV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static final int JCS_CMYK = 4;          // C/M/Y/K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static final int JCS_YCC = 5;           // PhotoYCC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static final int JCS_RGBA = 6;          // RGB-Alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static final int JCS_YCbCrA = 7;        // Y/Cb/Cr/Alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // 8 and 9 were old "Legacy" codes which the old code never identified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // on reading anyway.  Support for writing them is being dropped, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static final int JCS_YCCA = 10;         // PhotoYCC-Alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static final int JCS_YCCK = 11;         // Y/Cb/Cr/K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public static final int NUM_JCS_CODES = JCS_YCCK+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /** IJG can handle up to 4-channel JPEGs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final int [] [] bandOffsets = {{0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                          {0, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                          {0, 1, 2},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                          {0, 1, 2, 3}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public static final int [] bOffsRGB = { 2, 1, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
2381
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   211
    /* These are kept in the inner class to avoid static initialization
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   212
     * of the CMM class until someone actually needs it.
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   213
     * (e.g. do not init CMM on the request for jpeg mime types)
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   214
     */
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   215
    public static class JCS {
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   216
        public static final ColorSpace sRGB =
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   217
            ColorSpace.getInstance(ColorSpace.CS_sRGB);
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   218
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   219
        private static ColorSpace YCC = null;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   220
        private static boolean yccInited = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   222
        public static ColorSpace getYCC() {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   223
            if (!yccInited) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   224
                try {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   225
                    YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   226
                } catch (IllegalArgumentException e) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   227
                    // PYCC.pf may not always be installed
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   228
                } finally {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   229
                    yccInited = true;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   230
                }
2381
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   231
            }
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   232
            return YCC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // Default value for ImageWriteParam
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static final float DEFAULT_QUALITY = 0.75F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns <code>true</code> if the given <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * object is an instance of ICC_ColorSpace but is not one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * standard <code>ColorSpaces</code> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <code>ColorSpace.getInstance()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static boolean isNonStandardICC(ColorSpace cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean retval = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if ((cs instanceof ICC_ColorSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            && (!cs.isCS_sRGB())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            retval = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns <code>true</code> if the given imageType can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * in a JFIF file.  If <code>input</code> is true, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * image type is considered before colorspace conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static boolean isJFIFcompliant(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                   boolean input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ColorModel cm = imageType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Can't have alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (cm.hasAlpha()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // Gray is OK, always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int numComponents = imageType.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (numComponents == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // If it isn't gray, it must have 3 channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (numComponents != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Must be RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Must be YCbCr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Given an image type, return the Adobe transform corresponding to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * that type, or ADOBE_IMPOSSIBLE if the image type is incompatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * with an Adobe marker segment.  If <code>input</code> is true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * the image type is considered before colorspace conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    static int transformForType(ImageTypeSpecifier imageType, boolean input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int retval = ADOBE_IMPOSSIBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ColorModel cm = imageType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        switch (cm.getColorSpace().getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            retval = ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        case ColorSpace.TYPE_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            retval = input ? ADOBE_YCC : ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            retval = ADOBE_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        case ColorSpace.TYPE_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            retval = input ? ADOBE_YCCK : ADOBE_IMPOSSIBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Converts an ImageWriteParam (i.e. IJG) non-linear quality value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * to a float suitable for passing to JPEGQTable.getScaledInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static float convertToLinearQuality(float quality) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // The following is converted from the IJG code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (quality <= 0.0F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            quality = 0.01F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (quality > 1.00F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            quality = 1.00F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (quality < 0.5F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            quality = 0.5F / quality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            quality = 2.0F - (quality * 2.0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return quality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Return an array of default, visually lossless quantization tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static JPEGQTable [] getDefaultQTables() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        JPEGQTable [] qTables = new JPEGQTable[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        qTables[0] = JPEGQTable.K1Div2Luminance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        qTables[1] = JPEGQTable.K2Div2Chrominance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return qTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Return an array of default Huffman tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static JPEGHuffmanTable [] getDefaultHuffmanTables(boolean wantDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        JPEGHuffmanTable [] tables = new JPEGHuffmanTable[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (wantDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            tables[0] = JPEGHuffmanTable.StdDCLuminance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            tables[1] = JPEGHuffmanTable.StdDCChrominance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            tables[0] = JPEGHuffmanTable.StdACLuminance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            tables[1] = JPEGHuffmanTable.StdACChrominance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
}