jdk/src/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java
author omajid
Fri, 23 May 2014 11:04:37 -0400
changeset 25103 4dcb7cd7652e
parent 23312 4711f66e7d5c
permissions -rw-r--r--
8043805: Allow using a system-installed libjpeg Reviewed-by: andrew, anthony, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20798
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3009
diff changeset
    23
 * questions.
2
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.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.imageio.ImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.imageio.IIOImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.imageio.metadata.IIOMetadataFormatImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.imageio.metadata.IIOInvalidTreeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.imageio.plugins.jpeg.JPEGQTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.image.ColorConvertOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.image.RenderedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.awt.color.ICC_ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.awt.color.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.java2d.Disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.java2d.DisposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class JPEGImageWriter extends ImageWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    ///////// Private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The following variable contains a pointer to the IJG library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * structure for this reader.  It is assigned in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * and then is passed in to every native call.  It is set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * by dispose to avoid disposing twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private long structPointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** The output stream we write to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private ImageOutputStream ios = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** The Raster we will write from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private Raster srcRas = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** An intermediate Raster holding compressor-friendly data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private WritableRaster raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Set to true if we are writing an image with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * indexed ColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private boolean indexed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private IndexColorModel indexCM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private boolean convertTosRGB = false;  // Used by PhotoYCC only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private WritableRaster converted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private boolean isAlphaPremultiplied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private ColorModel srcCM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If there are thumbnails to be written, this is the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
   110
    private List<? extends BufferedImage> thumbnails = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If metadata should include an icc profile, store it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private ICC_Profile iccProfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int sourceXOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int sourceYOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private int sourceWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private int [] srcBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private int sourceHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /** Used when calling listeners */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private int currentImage = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private ColorConvertOp convertOp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private JPEGQTable [] streamQTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private JPEGHuffmanTable[] streamDCHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private JPEGHuffmanTable[] streamACHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Parameters for writing metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private boolean ignoreJFIF = false;  // If it's there, use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private boolean forceJFIF = false;  // Add one for the thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private boolean ignoreAdobe = false;  // If it's there, use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private int newAdobeTransform = JPEG.ADOBE_IMPOSSIBLE;  // Change if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean writeDefaultJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean writeAdobe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private JPEGMetadata metadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private boolean sequencePrepared = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private int numScans = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /** The referent to be registered with the Disposer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private Object disposerReferent = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /** The DisposerRecord that handles the actual disposal of this writer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private DisposerRecord disposerRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    ///////// End of Private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    ///////// Protected variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected static final int WARNING_DEST_IGNORED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected static final int WARNING_STREAM_METADATA_IGNORED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected static final int WARNING_DEST_METADATA_COMP_MISMATCH = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected static final int WARNING_DEST_METADATA_JFIF_MISMATCH = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected static final int WARNING_DEST_METADATA_ADOBE_MISMATCH = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected static final int WARNING_IMAGE_METADATA_JFIF_MISMATCH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected static final int WARNING_IMAGE_METADATA_ADOBE_MISMATCH = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected static final int WARNING_METADATA_NOT_JPEG_FOR_RASTER = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected static final int WARNING_NO_BANDS_ON_INDEXED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected static final int WARNING_ILLEGAL_THUMBNAIL = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected static final int WARNING_IGNORING_THUMBS = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected static final int WARNING_FORCING_JFIF = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected static final int WARNING_THUMB_CLIPPED = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected static final int WARNING_METADATA_ADJUSTED_FOR_THUMB = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected static final int WARNING_NO_RGB_THUMB_AS_INDEXED = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected static final int WARNING_NO_GRAY_THUMB_AS_INDEXED = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final int MAX_WARNING = WARNING_NO_GRAY_THUMB_AS_INDEXED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ///////// End of Protected variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    ///////// static initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
   180
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
   181
                public Void run() {
25103
4dcb7cd7652e 8043805: Allow using a system-installed libjpeg
omajid
parents: 23312
diff changeset
   182
                    System.loadLibrary("javajpeg");
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
   183
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
   184
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
   185
            });
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   186
        initWriterIDs(JPEGQTable.class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                      JPEGHuffmanTable.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    //////// Public API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public JPEGImageWriter(ImageWriterSpi originator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        super(originator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        structPointer = initJPEGImageWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        disposerRecord = new JPEGWriterDisposerRecord(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Disposer.addRecord(disposerReferent, disposerRecord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void setOutput(Object output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   202
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            super.setOutput(output); // validates output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            resetInternalState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            ios = (ImageOutputStream) output; // so this will always work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // Set the native destination
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   208
            setDest(structPointer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public ImageWriteParam getDefaultWriteParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new JPEGImageWriteParam(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return new JPEGMetadata(param, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public IIOMetadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        getDefaultImageMetadata(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return new JPEGMetadata(imageType, param, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public IIOMetadata convertStreamMetadata(IIOMetadata inData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                             ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // There isn't much we can do.  If it's one of ours, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // return it.  Otherwise just return null.  We use it only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // for tables, so we can't get a default and modify it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // as this will usually not be what is intended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (inData instanceof JPEGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            JPEGMetadata jpegData = (JPEGMetadata) inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (jpegData.isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public IIOMetadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        convertImageMetadata(IIOMetadata inData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                             ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                             ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return convertImageMetadataOnThread(inData, imageType, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private IIOMetadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        convertImageMetadataOnThread(IIOMetadata inData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                     ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                     ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // If it's one of ours, just return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (inData instanceof JPEGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            JPEGMetadata jpegData = (JPEGMetadata) inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (!jpegData.isStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // Can't convert stream metadata to image metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // XXX Maybe this should put out a warning?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // If it's not one of ours, create a default and set it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // the standard tree from the input, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (inData.isStandardMetadataFormatSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            String formatName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                IIOMetadataFormatImpl.standardMetadataFormatName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Node tree = inData.getAsTree(formatName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                JPEGMetadata jpegData = new JPEGMetadata(imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                         param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                                         this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    jpegData.setFromTree(formatName, tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } catch (IIOInvalidTreeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // Other plug-in generates bogus standard tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // XXX Maybe this should put out a warning?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return jpegData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public int getNumThumbnailsSupported(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                         ImageWriteParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                         IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                         IIOMetadata imageMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (jfifOK(imageType, param, streamMetadata, imageMetadata)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    static final Dimension [] preferredThumbSizes = {new Dimension(1, 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                     new Dimension(255, 255)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public Dimension[] getPreferredThumbnailSizes(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                  ImageWriteParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                  IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                  IIOMetadata imageMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (jfifOK(imageType, param, streamMetadata, imageMetadata)) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20798
diff changeset
   322
            return preferredThumbSizes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private boolean jfifOK(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                           ImageWriteParam param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                           IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                           IIOMetadata imageMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // If the image type and metadata are JFIF compatible, return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if ((imageType != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            (!JPEG.isJFIFcompliant(imageType, true))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (imageMetadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            JPEGMetadata metadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (imageMetadata instanceof JPEGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                metadata = (JPEGMetadata) imageMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                metadata = (JPEGMetadata)convertImageMetadata(imageMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                                              imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                              param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // metadata must have a jfif node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                (JFIFMarkerSegment.class, true) == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean canWriteRasters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void write(IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                      IIOImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                      ImageWriteParam param) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   363
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
   364
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            writeOnThread(streamMetadata, image, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private void writeOnThread(IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                      IIOImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                      ImageWriteParam param) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (ios == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new IllegalStateException("Output has not been set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("image is null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // if streamMetadata is not null, issue a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (streamMetadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            warningOccurred(WARNING_STREAM_METADATA_IGNORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // Obtain the raster and image, if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        boolean rasterOnly = image.hasRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        RenderedImage rimage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (rasterOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            srcRas = image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            rimage = image.getRenderedImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (rimage instanceof BufferedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // Use the Raster directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                srcRas = ((BufferedImage)rimage).getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            } else if (rimage.getNumXTiles() == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                       rimage.getNumYTiles() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                // Get the unique tile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                srcRas = rimage.getTile(rimage.getMinTileX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                        rimage.getMinTileY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // Ensure the Raster has dimensions of the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // as the tile dimensions might differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (srcRas.getWidth() != rimage.getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    srcRas.getHeight() != rimage.getHeight())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    srcRas = srcRas.createChild(srcRas.getMinX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                                srcRas.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                                rimage.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                                rimage.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                                srcRas.getMinX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                                srcRas.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                // Image is tiled so get a contiguous raster by copying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                srcRas = rimage.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Now determine if we are using a band subset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // By default, we are using all source bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int numSrcBands = srcRas.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        indexed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        indexCM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        ColorModel cm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        ColorSpace cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        isAlphaPremultiplied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        srcCM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (!rasterOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            cm = rimage.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (cm instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    indexed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    indexCM = (IndexColorModel) cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    numSrcBands = cm.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (cm.isAlphaPremultiplied()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    isAlphaPremultiplied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    srcCM = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        srcBands = JPEG.bandOffsets[numSrcBands-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        int numBandsUsed = numSrcBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Consult the param to determine if we're writing a subset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            int[] sBands = param.getSourceBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (sBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (indexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    warningOccurred(WARNING_NO_BANDS_ON_INDEXED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    srcBands = sBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    numBandsUsed = srcBands.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    if (numBandsUsed > numSrcBands) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        ("ImageWriteParam specifies too many source bands");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        boolean usingBandSubset = (numBandsUsed != numSrcBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        boolean fullImage = ((!rasterOnly) && (!usingBandSubset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        int [] bandSizes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (!indexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            bandSizes = srcRas.getSampleModel().getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // If this is a subset, we must adjust bandSizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (usingBandSubset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                int [] temp = new int [numBandsUsed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                for (int i = 0; i < numBandsUsed; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    temp[i] = bandSizes[srcBands[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                bandSizes = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            int [] tempSize = srcRas.getSampleModel().getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            bandSizes = new int [numSrcBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            for (int i = 0; i < numSrcBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                bandSizes[i] = tempSize[0];  // All the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        for (int i = 0; i < bandSizes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            // 4450894 part 1: The IJG libraries are compiled so they only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            // handle <= 8-bit samples.  We now check the band sizes and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            // an exception for images, such as USHORT_GRAY, with > 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // per sample.
20798
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
   498
            if (bandSizes[i] <= 0 || bandSizes[i] > 8) {
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
   499
                throw new IIOException("Illegal band size: should be 0 < size <= 8");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            // 4450894 part 2: We expand IndexColorModel images to full 24-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            // or 32-bit in grabPixels() for each scanline.  For indexed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            // images such as BYTE_BINARY, we need to ensure that we update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // bandSizes to account for the scaling from 1-bit band sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            // to 8-bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (indexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                bandSizes[i] = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            System.out.println("numSrcBands is " + numSrcBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            System.out.println("numBandsUsed is " + numBandsUsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            System.out.println("usingBandSubset is " + usingBandSubset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            System.out.println("fullImage is " + fullImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            System.out.print("Band sizes:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            for (int i = 0; i< bandSizes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                System.out.print(" " + bandSizes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        // Destination type, if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        ImageTypeSpecifier destType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            destType = param.getDestinationType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            // Ignore dest type if we are writing a complete image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if ((fullImage) && (destType != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                warningOccurred(WARNING_DEST_IGNORED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                destType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // Examine the param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        sourceXOffset = srcRas.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        sourceYOffset = srcRas.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int imageWidth = srcRas.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int imageHeight = srcRas.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        sourceWidth = imageWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        sourceHeight = imageHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int periodX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        int periodY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        int gridX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int gridY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        JPEGQTable [] qTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        JPEGHuffmanTable[] DCHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        JPEGHuffmanTable[] ACHuffmanTables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        boolean optimizeHuffman = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        JPEGImageWriteParam jparam = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int progressiveMode = ImageWriteParam.MODE_DISABLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            Rectangle sourceRegion = param.getSourceRegion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (sourceRegion != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                Rectangle imageBounds = new Rectangle(sourceXOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                                      sourceYOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                                                      sourceWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                                      sourceHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                sourceRegion = sourceRegion.intersection(imageBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                sourceXOffset = sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                sourceYOffset = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                sourceWidth = sourceRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                sourceHeight = sourceRegion.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (sourceWidth + sourceXOffset > imageWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                sourceWidth = imageWidth - sourceXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (sourceHeight + sourceYOffset > imageHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                sourceHeight = imageHeight - sourceYOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            periodX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            periodY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            gridX = param.getSubsamplingXOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            gridY = param.getSubsamplingYOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            switch(param.getCompressionMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            case ImageWriteParam.MODE_DISABLED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                throw new IIOException("JPEG compression cannot be disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            case ImageWriteParam.MODE_EXPLICIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                float quality = param.getCompressionQuality();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                quality = JPEG.convertToLinearQuality(quality);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                qTables = new JPEGQTable[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                qTables[0] = JPEGQTable.K1Luminance.getScaledInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    (quality, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                qTables[1] = JPEGQTable.K2Chrominance.getScaledInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    (quality, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            case ImageWriteParam.MODE_DEFAULT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                qTables = new JPEGQTable[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                qTables[0] = JPEGQTable.K1Div2Luminance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                qTables[1] = JPEGQTable.K2Div2Chrominance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            // We'll handle the metadata case later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            progressiveMode = param.getProgressiveMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (param instanceof JPEGImageWriteParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                jparam = (JPEGImageWriteParam)param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                optimizeHuffman = jparam.getOptimizeHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // Now examine the metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        IIOMetadata mdata = image.getMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (mdata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            if (mdata instanceof JPEGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                metadata = (JPEGMetadata) mdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        ("We have metadata, and it's JPEG metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (!rasterOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    ImageTypeSpecifier type = destType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        type = new ImageTypeSpecifier(rimage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    metadata = (JPEGMetadata) convertImageMetadata(mdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                                                   type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                                                   param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    warningOccurred(WARNING_METADATA_NOT_JPEG_FOR_RASTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // First set a default state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        ignoreJFIF = false;  // If it's there, use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        ignoreAdobe = false;  // If it's there, use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        newAdobeTransform = JPEG.ADOBE_IMPOSSIBLE;  // Change if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        writeDefaultJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        writeAdobe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        // By default we'll do no conversion:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int inCsType = JPEG.JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int outCsType = JPEG.JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        JFIFMarkerSegment jfif = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        AdobeMarkerSegment adobe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        SOFMarkerSegment sof = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (metadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            jfif = (JFIFMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            adobe = (AdobeMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                (AdobeMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            sof = (SOFMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                (SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        iccProfile = null;  // By default don't write one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        convertTosRGB = false;  // PhotoYCC does this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        converted = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (destType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (numBandsUsed != destType.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    ("Number of source bands != number of destination bands");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            cs = destType.getColorModel().getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            // Check the metadata against the destination type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (metadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                checkSOFBands(sof, numBandsUsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                checkJFIF(jfif, destType, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                // Do we want to write an ICC profile?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                if ((jfif != null) && (ignoreJFIF == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    if (JPEG.isNonStandardICC(cs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        iccProfile = ((ICC_ColorSpace) cs).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                checkAdobe(adobe, destType, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            } else { // no metadata, but there is a dest type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                // If we can add a JFIF or an Adobe marker segment, do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                if (JPEG.isJFIFcompliant(destType, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    writeDefaultJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    // Do we want to write an ICC profile?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    if (JPEG.isNonStandardICC(cs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        iccProfile = ((ICC_ColorSpace) cs).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    int transform = JPEG.transformForType(destType, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    if (transform != JPEG.ADOBE_IMPOSSIBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        writeAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        newAdobeTransform = transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                // re-create the metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                metadata = new JPEGMetadata(destType, null, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            inCsType = getSrcCSType(destType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            outCsType = getDefaultDestCSType(destType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        } else { // no destination type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if (metadata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (fullImage) {  // no dest, no metadata, full image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    // Use default metadata matching the image and param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    metadata = new JPEGMetadata(new ImageTypeSpecifier(rimage),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                                param, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    if (metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        (JFIFMarkerSegment.class, true) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        cs = rimage.getColorModel().getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        if (JPEG.isNonStandardICC(cs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            iccProfile = ((ICC_ColorSpace) cs).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    inCsType = getSrcCSType(rimage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    outCsType = getDefaultDestCSType(rimage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                // else no dest, no metadata, not an image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                // so no special headers, no color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            } else { // no dest type, but there is metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                checkSOFBands(sof, numBandsUsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (fullImage) {  // no dest, metadata, image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    // Check that the metadata and the image match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    ImageTypeSpecifier inputType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        new ImageTypeSpecifier(rimage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    inCsType = getSrcCSType(rimage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        boolean alpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        switch (cs.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                            if (!alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                outCsType = JPEG.JCS_GRAYSCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                    ignoreJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                    warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                    (WARNING_IMAGE_METADATA_JFIF_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                // out colorspace remains unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                            if ((adobe != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                && (adobe.transform != JPEG.ADOBE_UNKNOWN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                                newAdobeTransform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                                warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                (WARNING_IMAGE_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        case ColorSpace.TYPE_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            if (!alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                    outCsType = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                    if (JPEG.isNonStandardICC(cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                        || ((cs instanceof ICC_ColorSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                            && (jfif.iccSegment != null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                        iccProfile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                            ((ICC_ColorSpace) cs).getProfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                } else if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                    switch (adobe.transform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                                    case JPEG.ADOBE_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                                        outCsType = JPEG.JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                    case JPEG.ADOBE_YCC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                        outCsType = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                        warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                        (WARNING_IMAGE_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                        newAdobeTransform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                        outCsType = JPEG.JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                    // consult the ids
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                    int outCS = sof.getIDencodedCSType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                    // if they don't resolve it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                    // consult the sampling factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                    if (outCS != JPEG.JCS_UNKNOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                                        outCsType = outCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                        boolean subsampled =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                        isSubsampled(sof.componentSpecs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                        if (subsampled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                                            outCsType = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                            outCsType = JPEG.JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                            } else { // RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                    ignoreJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                    warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                    (WARNING_IMAGE_METADATA_JFIF_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                    if (adobe.transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                        != JPEG.ADOBE_UNKNOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                        newAdobeTransform = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                        warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                        (WARNING_IMAGE_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                    outCsType = JPEG.JCS_RGBA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                    // consult the ids
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                    int outCS = sof.getIDencodedCSType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                    // if they don't resolve it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                    // consult the sampling factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                                    if (outCS != JPEG.JCS_UNKNOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                        outCsType = outCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                                        boolean subsampled =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                        isSubsampled(sof.componentSpecs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                        outCsType = subsampled ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                                            JPEG.JCS_YCbCrA : JPEG.JCS_RGBA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        case ColorSpace.TYPE_3CLR:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
   823
                            if (cs == JPEG.JCS.getYCC()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                if (!alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                    if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                        convertTosRGB = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                        convertOp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                        new ColorConvertOp(cs,
2381
0afc5d868c46 6631559: Registration of ImageIO plugins should not cause loading of jpeg.dlli and cmm.dll
bae
parents: 2
diff changeset
   829
                                                           JPEG.JCS.sRGB,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                                                           null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                                        outCsType = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                                    } else if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                                        if (adobe.transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                            != JPEG.ADOBE_YCC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                            newAdobeTransform = JPEG.ADOBE_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                            warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                            (WARNING_IMAGE_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                        outCsType = JPEG.JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                        outCsType = JPEG.JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                } else { // PhotoYCCA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                    if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                        ignoreJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                        warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                        (WARNING_IMAGE_METADATA_JFIF_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                    } else if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                        if (adobe.transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                            != JPEG.ADOBE_UNKNOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                            newAdobeTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                                            = JPEG.ADOBE_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                                            warningOccurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                                            (WARNING_IMAGE_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                    outCsType = JPEG.JCS_YCCA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                } // else no dest, metadata, not an image.  Defaults ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        boolean metadataProgressive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        int [] scans = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (metadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            if (sof == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                sof = (SOFMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    (SOFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if ((sof != null) && (sof.tag == JPEG.SOF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                metadataProgressive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                if (progressiveMode == ImageWriteParam.MODE_COPY_FROM_METADATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    scans = collectScans(metadata, sof);  // Might still be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    numScans = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            if (jfif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                jfif = (JFIFMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    (JFIFMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        thumbnails = image.getThumbnails();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        int numThumbs = image.getNumThumbnails();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        forceJFIF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        // determine if thumbnails can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        // If we are going to add a default JFIF marker segment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        // then thumbnails can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (!writeDefaultJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            // If there is no metadata, then we can't write thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            if (metadata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                thumbnails = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (numThumbs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    warningOccurred(WARNING_IGNORING_THUMBS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                // There is metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                // If we are writing a raster or subbands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                // then the user must specify JFIF on the metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                if (fullImage == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    if (jfif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                        thumbnails = null;  // Or we can't include thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        if (numThumbs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                            warningOccurred(WARNING_IGNORING_THUMBS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                } else {  // It is a full image, and there is metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    if (jfif == null) {  // Not JFIF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        // Can it have JFIF?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        if ((outCsType == JPEG.JCS_GRAYSCALE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                            || (outCsType == JPEG.JCS_YCbCr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                            if (numThumbs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                                forceJFIF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                warningOccurred(WARNING_FORCING_JFIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                        } else {  // Nope, not JFIF-compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                            thumbnails = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                            if (numThumbs != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                warningOccurred(WARNING_IGNORING_THUMBS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // Set up a boolean to indicate whether we need to call back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        // write metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        boolean haveMetadata =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            ((metadata != null) || writeDefaultJFIF || writeAdobe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        // Now that we have dealt with metadata, finalize our tables set up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        // Are we going to write tables?  By default, yes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        boolean writeDQT = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        boolean writeDHT = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        // But if the metadata has no tables, no.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        DQTMarkerSegment dqt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        DHTMarkerSegment dht = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        int restartInterval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (metadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            dqt = (DQTMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                (DQTMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            dht = (DHTMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                (DHTMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            DRIMarkerSegment dri =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                (DRIMarkerSegment) metadata.findMarkerSegment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                (DRIMarkerSegment.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (dri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                restartInterval = dri.restartInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (dqt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                writeDQT = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (dht == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                writeDHT = false;  // Ignored if optimizeHuffman is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        // Whether we write tables or not, we need to figure out which ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        // to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (qTables == null) { // Get them from metadata, or use defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (dqt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                qTables = collectQTablesFromMetadata(metadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            } else if (streamQTables != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                qTables = streamQTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            } else if ((jparam != null) && (jparam.areTablesSet())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                qTables = jparam.getQTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                qTables = JPEG.getDefaultQTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        // If we are optimizing, we don't want any tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (optimizeHuffman == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            // If they were for progressive scans, we can't use them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if ((dht != null) && (metadataProgressive == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                DCHuffmanTables = collectHTablesFromMetadata(metadata, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                ACHuffmanTables = collectHTablesFromMetadata(metadata, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            } else if (streamDCHuffmanTables != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                DCHuffmanTables = streamDCHuffmanTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                ACHuffmanTables = streamACHuffmanTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            } else if ((jparam != null) && (jparam.areTablesSet())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                DCHuffmanTables = jparam.getDCHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                ACHuffmanTables = jparam.getACHuffmanTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                DCHuffmanTables = JPEG.getDefaultHuffmanTables(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                ACHuffmanTables = JPEG.getDefaultHuffmanTables(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        // By default, ids are 1 - N, no subsampling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        int [] componentIds = new int[numBandsUsed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        int [] HsamplingFactors = new int[numBandsUsed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        int [] VsamplingFactors = new int[numBandsUsed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        int [] QtableSelectors = new int[numBandsUsed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        for (int i = 0; i < numBandsUsed; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            componentIds[i] = i+1; // JFIF compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            HsamplingFactors[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            VsamplingFactors[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            QtableSelectors[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        // Now override them with the contents of sof, if there is one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            for (int i = 0; i < numBandsUsed; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                if (forceJFIF == false) {  // else use JFIF-compatible default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    componentIds[i] = sof.componentSpecs[i].componentId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                HsamplingFactors[i] = sof.componentSpecs[i].HsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                VsamplingFactors[i] = sof.componentSpecs[i].VsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                QtableSelectors[i] = sof.componentSpecs[i].QtableSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        sourceXOffset += gridX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        sourceWidth -= gridX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        sourceYOffset += gridY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        sourceHeight -= gridY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        int destWidth = (sourceWidth + periodX - 1)/periodX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        int destHeight = (sourceHeight + periodY - 1)/periodY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        // Create an appropriate 1-line databuffer for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        int lineSize = sourceWidth*numBandsUsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        DataBufferByte buffer = new DataBufferByte(lineSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        // Create a raster from that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        int [] bandOffs = JPEG.bandOffsets[numBandsUsed-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        raster = Raster.createInterleavedRaster(buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                                sourceWidth, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                                lineSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                                                numBandsUsed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                                                bandOffs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // Call the writer, who will call back for every scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        processImageStarted(currentImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        boolean aborted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            System.out.println("inCsType: " + inCsType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            System.out.println("outCsType: " + outCsType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // Note that getData disables acceleration on buffer, but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // just a 1-line intermediate data transfer buffer that does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        // affect the acceleration of the source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        aborted = writeImage(structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                             buffer.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                             inCsType, outCsType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                             numBandsUsed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                             bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                             sourceWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                             destWidth, destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                             periodX, periodY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                             qTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                             writeDQT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                             DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                             ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                             writeDHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                             optimizeHuffman,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                             (progressiveMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                              != ImageWriteParam.MODE_DISABLED),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                             numScans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                             scans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                             componentIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                             HsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                             VsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                             QtableSelectors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                             haveMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                             restartInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1088
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1089
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1090
            if (aborted) {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1091
                processWriteAborted();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1092
            } else {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1093
                processImageComplete();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1094
            }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1095
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1096
            ios.flush();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1097
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1098
            cbLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        currentImage++;  // After a successful write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public void prepareWriteSequence(IIOMetadata streamMetadata)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1107
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1108
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            prepareWriteSequenceOnThread(streamMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    private void prepareWriteSequenceOnThread(IIOMetadata streamMetadata)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (ios == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            throw new IllegalStateException("Output has not been set!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
         * from jpeg_metadata.html:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
         * If no stream metadata is supplied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * <code>ImageWriter.prepareWriteSequence</code>, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         * tables-only image is written.  If stream metadata containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         * no tables is supplied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         * <code>ImageWriter.prepareWriteSequence</code>, then a tables-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         * image containing default visually lossless tables is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (streamMetadata != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (streamMetadata instanceof JPEGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                // write a complete tables-only image at the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                // the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                JPEGMetadata jmeta = (JPEGMetadata) streamMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                if (jmeta.isStream == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                        ("Invalid stream metadata object.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                // Check that we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                // at the beginning of the stream, or can go there, and haven't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                // written out the metadata already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                if (currentImage != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                        ("JPEG Stream metadata must precede all images");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                if (sequencePrepared == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    throw new IIOException("Stream metadata already written!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                // Set the tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                // If the metadata has no tables, use default tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                streamQTables = collectQTablesFromMetadata(jmeta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    System.out.println("after collecting from stream metadata, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                       + "streamQTables.length is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                       + streamQTables.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                if (streamQTables == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    streamQTables = JPEG.getDefaultQTables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                streamDCHuffmanTables =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    collectHTablesFromMetadata(jmeta, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                if (streamDCHuffmanTables == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    streamDCHuffmanTables = JPEG.getDefaultHuffmanTables(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                streamACHuffmanTables =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    collectHTablesFromMetadata(jmeta, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                if (streamACHuffmanTables == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    streamACHuffmanTables = JPEG.getDefaultHuffmanTables(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                // Now write them out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                writeTables(structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                            streamQTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                            streamDCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                            streamACHuffmanTables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                throw new IIOException("Stream metadata must be JPEG metadata");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        sequencePrepared = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    public void writeToSequence(IIOImage image, ImageWriteParam param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1188
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1189
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            if (sequencePrepared == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                throw new IllegalStateException("sequencePrepared not called!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            // In the case of JPEG this does nothing different from write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            write(null, image, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    public void endWriteSequence() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1203
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1204
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            if (sequencePrepared == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                throw new IllegalStateException("sequencePrepared not called!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            sequencePrepared = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    public synchronized void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1217
            /**
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1218
             * NB: we do not check the call back lock here, we allow to abort
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1219
             * the reader any time.
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1220
             */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            super.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            abortWrite(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    private void resetInternalState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // reset C structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        resetWriter(structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        // reset local Java structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        srcRas = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        raster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        convertTosRGB = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        currentImage = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        numScans = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        metadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1244
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1245
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        setThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        try {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1255
            cbLock.check();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            if (structPointer != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                disposerRecord.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                structPointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            clearThreadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    ////////// End of public API
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    ///////// Package-access API
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Called by the native code or other classes to signal a warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * The code is used to lookup a localized message to be used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * sending warnings to listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    void warningOccurred(int code) {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1276
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1277
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1278
            if ((code < 0) || (code > MAX_WARNING)){
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1279
                throw new InternalError("Invalid warning index");
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1280
            }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1281
            processWarningOccurred
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1282
                (currentImage,
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1283
                 "com.sun.imageio.plugins.jpeg.JPEGImageWriterResources",
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1284
                Integer.toString(code));
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1285
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1286
            cbLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * The library has it's own error facility that emits warning messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * This routine is called by the native code when it has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * formatted a string for output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * XXX  For truly complete localization of all warning messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * the sun_jpeg_output_message routine in the native code should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * send only the codes and parameters to a method here in Java,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * which will then format and send the warnings, using localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * strings.  This method will have to deal with all the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * and formats (%u with possibly large numbers, %02d, %02x, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * that actually occur in the JPEG library.  For now, this prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * library warnings from being printed to stderr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    void warningWithMessage(String msg) {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1304
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1305
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1306
            processWarningOccurred(currentImage, msg);
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1307
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1308
            cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1309
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    void thumbnailStarted(int thumbnailIndex) {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1313
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1314
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1315
            processThumbnailStarted(currentImage, thumbnailIndex);
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1316
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1317
            cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1318
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    void thumbnailProgress(float percentageDone) {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1323
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1324
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1325
            processThumbnailProgress(percentageDone);
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1326
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1327
            cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1328
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // Provide access to protected superclass method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    void thumbnailComplete() {
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1333
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1334
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1335
            processThumbnailComplete();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1336
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1337
            cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1338
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    ///////// End of Package-access API
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    ///////// Private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    ///////// Metadata handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    private void checkSOFBands(SOFMarkerSegment sof, int numBandsUsed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        // Does the metadata frame header, if any, match numBandsUsed?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        if (sof != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            if (sof.componentSpecs.length != numBandsUsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    ("Metadata components != number of destination bands");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    private void checkJFIF(JFIFMarkerSegment jfif,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                           ImageTypeSpecifier type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                           boolean input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        if (jfif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            if (!JPEG.isJFIFcompliant(type, input)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                ignoreJFIF = true;  // type overrides metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                warningOccurred(input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                                ? WARNING_IMAGE_METADATA_JFIF_MISMATCH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                                : WARNING_DEST_METADATA_JFIF_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    private void checkAdobe(AdobeMarkerSegment adobe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                           ImageTypeSpecifier type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                           boolean input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        if (adobe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            int rightTransform = JPEG.transformForType(type, input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (adobe.transform != rightTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                warningOccurred(input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                ? WARNING_IMAGE_METADATA_ADOBE_MISMATCH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                : WARNING_DEST_METADATA_ADOBE_MISMATCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                if (rightTransform == JPEG.ADOBE_IMPOSSIBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    ignoreAdobe = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    newAdobeTransform = rightTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * Collect all the scan info from the given metadata, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * organize it into the scan info array required by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * IJG libray.  It is much simpler to parse out this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * data in Java and then just copy the data in C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    private int [] collectScans(JPEGMetadata metadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                SOFMarkerSegment sof) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1397
        List<SOSMarkerSegment> segments = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        int SCAN_SIZE = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        int MAX_COMPS_PER_SCAN = 4;
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1400
        for (Iterator<MarkerSegment> iter = metadata.markerSequence.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
             iter.hasNext();) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1402
            MarkerSegment seg = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            if (seg instanceof SOSMarkerSegment) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1404
                segments.add((SOSMarkerSegment) seg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        int [] retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        numScans = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        if (!segments.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            numScans = segments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            retval = new int [numScans*SCAN_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            for (int i = 0; i < numScans; i++) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1414
                SOSMarkerSegment sos = segments.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                retval[index++] = sos.componentSpecs.length; // num comps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                for (int j = 0; j < MAX_COMPS_PER_SCAN; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                    if (j < sos.componentSpecs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                        int compSel = sos.componentSpecs[j].componentSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                        for (int k = 0; k < sof.componentSpecs.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                            if (compSel == sof.componentSpecs[k].componentId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                                retval[index++] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                                break; // out of for over sof comps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                        retval[index++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                retval[index++] = sos.startSpectralSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                retval[index++] = sos.endSpectralSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                retval[index++] = sos.approxHigh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                retval[index++] = sos.approxLow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * Finds all DQT marker segments and returns all the q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * tables as a single array of JPEGQTables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    private JPEGQTable [] collectQTablesFromMetadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        (JPEGMetadata metadata) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1444
        ArrayList<DQTMarkerSegment.Qtable> tables = new ArrayList<>();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1445
        Iterator<MarkerSegment> iter = metadata.markerSequence.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        while (iter.hasNext()) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1447
            MarkerSegment seg = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            if (seg instanceof DQTMarkerSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                DQTMarkerSegment dqt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                    (DQTMarkerSegment) seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                tables.addAll(dqt.tables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        JPEGQTable [] retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (tables.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            retval = new JPEGQTable[tables.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            for (int i = 0; i < retval.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                retval[i] =
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1459
                    new JPEGQTable(tables.get(i).data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * Finds all DHT marker segments and returns all the q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * tables as a single array of JPEGQTables.  The metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * must not be for a progressive image, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * will be thrown when two Huffman tables with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * table id are encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    private JPEGHuffmanTable[] collectHTablesFromMetadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        (JPEGMetadata metadata, boolean wantDC) throws IIOException {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1474
        ArrayList<DHTMarkerSegment.Htable> tables = new ArrayList<>();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1475
        Iterator<MarkerSegment> iter = metadata.markerSequence.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        while (iter.hasNext()) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1477
            MarkerSegment seg = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            if (seg instanceof DHTMarkerSegment) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1479
                DHTMarkerSegment dht = (DHTMarkerSegment) seg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                for (int i = 0; i < dht.tables.size(); i++) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1481
                    DHTMarkerSegment.Htable htable = dht.tables.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    if (htable.tableClass == (wantDC ? 0 : 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        tables.add(htable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        JPEGHuffmanTable [] retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        if (tables.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            DHTMarkerSegment.Htable [] htables =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                new DHTMarkerSegment.Htable[tables.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            tables.toArray(htables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            retval = new JPEGHuffmanTable[tables.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            for (int i = 0; i < retval.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                retval[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                for (int j = 0; j < tables.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                    if (htables[j].tableID == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                        if (retval[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                            throw new IIOException("Metadata has duplicate Htables!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                        retval[i] = new JPEGHuffmanTable(htables[j].numCodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                                         htables[j].values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    /////////// End of metadata handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    ////////////// ColorSpace conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    private int getSrcCSType(ImageTypeSpecifier type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
         return getSrcCSType(type.getColorModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    private int getSrcCSType(RenderedImage rimage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        return getSrcCSType(rimage.getColorModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    private int getSrcCSType(ColorModel cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        int retval = JPEG.JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            boolean alpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            ColorSpace cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            switch (cs.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                retval = JPEG.JCS_GRAYSCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            case ColorSpace.TYPE_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    retval = JPEG.JCS_RGBA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    retval = JPEG.JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    retval = JPEG.JCS_YCbCrA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    retval = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            case ColorSpace.TYPE_3CLR:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1547
                if (cs == JPEG.JCS.getYCC()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                        retval = JPEG.JCS_YCCA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                        retval = JPEG.JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                }
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1554
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            case ColorSpace.TYPE_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                retval = JPEG.JCS_CMYK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    private int getDestCSType(ImageTypeSpecifier destType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        ColorModel cm = destType.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        boolean alpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        ColorSpace cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        int retval = JPEG.JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        switch (cs.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                retval = JPEG.JCS_GRAYSCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            case ColorSpace.TYPE_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    retval = JPEG.JCS_RGBA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    retval = JPEG.JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                    retval = JPEG.JCS_YCbCrA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                    retval = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            case ColorSpace.TYPE_3CLR:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1587
                if (cs == JPEG.JCS.getYCC()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                    if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                        retval = JPEG.JCS_YCCA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                        retval = JPEG.JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                }
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1594
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            case ColorSpace.TYPE_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                retval = JPEG.JCS_CMYK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    private int getDefaultDestCSType(ImageTypeSpecifier type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        return getDefaultDestCSType(type.getColorModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    private int getDefaultDestCSType(RenderedImage rimage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        return getDefaultDestCSType(rimage.getColorModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    private int getDefaultDestCSType(ColorModel cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        int retval = JPEG.JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            boolean alpha = cm.hasAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            ColorSpace cs = cm.getColorSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            switch (cs.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            case ColorSpace.TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                retval = JPEG.JCS_GRAYSCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            case ColorSpace.TYPE_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    retval = JPEG.JCS_YCbCrA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    retval = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            case ColorSpace.TYPE_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                    retval = JPEG.JCS_YCbCrA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    retval = JPEG.JCS_YCbCr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            case ColorSpace.TYPE_3CLR:
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2381
diff changeset
  1634
                if (cs == JPEG.JCS.getYCC()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    if (alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                        retval = JPEG.JCS_YCCA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                        retval = JPEG.JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                }
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1641
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            case ColorSpace.TYPE_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                retval = JPEG.JCS_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    private boolean isSubsampled(SOFMarkerSegment.ComponentSpec [] specs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        int hsamp0 = specs[0].HsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        int vsamp0 = specs[0].VsamplingFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        for (int i = 1; i < specs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            if ((specs[i].HsamplingFactor != hsamp0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                (specs[i].HsamplingFactor != hsamp0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    ////////////// End of ColorSpace conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    ////////////// Native methods and callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    /** Sets up static native structures. */
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1666
    private static native void initWriterIDs(Class<?> qTableClass,
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 22584
diff changeset
  1667
                                             Class<?> huffClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    /** Sets up per-writer native structure and returns a pointer to it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    private native long initJPEGImageWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /** Sets up native structures for output stream */
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1673
    private native void setDest(long structPointer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * Returns <code>true</code> if the write was aborted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    private native boolean writeImage(long structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                                      byte [] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                                      int inCsType, int outCsType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                                      int numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                                      int [] bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                                      int srcWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                                      int destWidth, int destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                                      int stepX, int stepY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                                      JPEGQTable [] qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                                      boolean writeDQT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                                      JPEGHuffmanTable[] DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                                      JPEGHuffmanTable[] ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                                      boolean writeDHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                                      boolean optimizeHuffman,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                                      boolean progressive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                                      int numScans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                                      int [] scans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                                      int [] componentIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                                      int [] HsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                                      int [] VsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                      int [] QtableSelectors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                                      boolean haveMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                                      int restartInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Writes the metadata out when called by the native code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * which will have already written the header to the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * and established the library state.  This is simpler than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * breaking the write call in two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    private void writeMetadata() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        if (metadata == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            if (writeDefaultJFIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                JFIFMarkerSegment.writeDefaultJFIF(ios,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                                                   thumbnails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                                                   iccProfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                                                   this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            if (writeAdobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                AdobeMarkerSegment.writeAdobeSegment(ios, newAdobeTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            metadata.writeToStream(ios,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                                   ignoreJFIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                                   forceJFIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                                   thumbnails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                                   iccProfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                                   ignoreAdobe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                                   newAdobeTransform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                                   this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * Write out a tables-only image to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    private native void writeTables(long structPointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                                    JPEGQTable [] qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                                    JPEGHuffmanTable[] DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                                    JPEGHuffmanTable[] ACHuffmanTables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * Put the scanline y of the source ROI view Raster into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * 1-line Raster for writing.  This handles ROI and band
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * rearrangements, and expands indexed images.  Subsampling is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * done in the native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * This is called by the native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    private void grabPixels(int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        Raster sourceLine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        if (indexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            sourceLine = srcRas.createChild(sourceXOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                                            sourceYOffset+y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                                            sourceWidth, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                                            0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                                            new int [] {0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            // If the image has BITMASK transparency, we need to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            // it gets converted to 32-bit ARGB, because the JPEG encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            // relies upon the full 8-bit alpha channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            boolean forceARGB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                (indexCM.getTransparency() != Transparency.OPAQUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                                                              forceARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            sourceLine = temp.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            sourceLine = srcRas.createChild(sourceXOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                                            sourceYOffset+y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                                            sourceWidth, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                                            0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                                            srcBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        if (convertTosRGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                System.out.println("Converting to sRGB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            // The first time through, converted is null, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            // a new raster is allocated.  It is then reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            // on subsequent lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            converted = convertOp.filter(sourceLine, converted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            sourceLine = converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        if (isAlphaPremultiplied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            WritableRaster wr = sourceLine.createCompatibleWritableRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            int[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                                        sourceLine.getWidth(), sourceLine.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                                        data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                         sourceLine.getWidth(), sourceLine.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                         data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            srcCM.coerceData(wr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                                        wr.getWidth(), wr.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                                        0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                                        srcBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        raster.setRect(sourceLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1798
            cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1799
            try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1800
                processImageProgress((float) y / (float) sourceHeight * 100.0F);
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1801
            } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1802
                cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1803
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /** Aborts the current write in the native code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    private native void abortWrite(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /** Resets native structures */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    private native void resetWriter(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /** Releases native structures */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    private static native void disposeWriter(long structPointer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    private static class JPEGWriterDisposerRecord implements DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        private long pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        public JPEGWriterDisposerRecord(long pData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            this.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        public synchronized void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            if (pData != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                disposeWriter(pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                pData = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1831
    /**
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1832
     * This method is called from native code in order to write encoder
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1833
     * output to the destination.
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1834
     *
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1835
     * We block any attempt to change the writer state during this
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1836
     * method, in order to prevent a corruption of the native encoder
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1837
     * state.
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1838
     */
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1839
    private void writeOutputData(byte[] data, int offset, int len)
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1840
            throws IOException
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1841
    {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1842
        cbLock.lock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1843
        try {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1844
            ios.write(data, offset, len);
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1845
        } finally {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1846
            cbLock.unlock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1847
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1848
    }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1849
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    private Thread theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    private int theLockCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    private synchronized void setThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        if (theThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                // it looks like that this reader instance is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                // by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                throw new IllegalStateException("Attempt to use instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                                                this + " locked on thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                                                theThread + " from thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                                                currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                theLockCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            theThread = currThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            theLockCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    private synchronized void clearThreadLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        Thread currThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        if (theThread == null || theThread != currThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            throw new IllegalStateException("Attempt to clear thread lock form wrong thread. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                                            "Locked thread: " + theThread +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                                            "; current thread: " + currThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        theLockCount --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        if (theLockCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            theThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    }
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1884
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1885
    private CallBackLock cbLock = new CallBackLock();
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1886
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1887
    private static class CallBackLock {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1888
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1889
        private State lockState;
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1890
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1891
        CallBackLock() {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1892
            lockState = State.Unlocked;
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1893
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1894
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1895
        void check() {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1896
            if (lockState != State.Unlocked) {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1897
                throw new IllegalStateException("Access to the writer is not allowed");
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1898
            }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1899
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1900
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1901
        private void lock() {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1902
            lockState = State.Locked;
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1903
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1904
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1905
        private void unlock() {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1906
            lockState = State.Unlocked;
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1907
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1908
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1909
        private static enum State {
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1910
            Unlocked,
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1911
            Locked
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1912
        }
c9b0f63bb215 8007918: Better image writing
bae
parents: 14342
diff changeset
  1913
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
}