src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java
author jdv
Wed, 18 Apr 2018 13:22:53 +0530
changeset 49996 39dc39093c5e
parent 47216 71c04702a3d5
permissions -rw-r--r--
6574555: PNGImageWriter incorrectly sets bKGD chunk Reviewed-by: prr, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49996
39dc39093c5e 6574555: PNGImageWriter incorrectly sets bKGD chunk
jdv
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 2376
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: 2376
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: 2376
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2376
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.png;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.RenderedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.zip.Deflater;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.zip.DeflaterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.imageio.IIOImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.imageio.ImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.imageio.stream.ImageOutputStreamImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    50
final class CRC {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    52
    private static final int[] crcTable = new int[256];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int crc = 0xffffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // Initialize CRC table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int n = 0; n < 256; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            int c = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            for (int k = 0; k < 8; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                if ((c & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    c = 0xedb88320 ^ (c >>> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    c >>>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                crcTable[n] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    71
    CRC() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    73
    void reset() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        crc = 0xffffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    77
    void update(byte[] data, int off, int len) {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    78
        int c = crc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int n = 0; n < len; n++) {
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    80
            c = crcTable[(c ^ data[off + n]) & 0xff] ^ (c >>> 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    82
        crc = c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    85
    void update(int data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        crc = crcTable[(crc ^ data) & 0xff] ^ (crc >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    89
    int getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return crc ^ 0xffffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
final class ChunkStream extends ImageOutputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    97
    private final ImageOutputStream stream;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    98
    private final long startPos;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
    99
    private final CRC crc = new CRC();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   101
    ChunkStream(int type, ImageOutputStream stream) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.startPos = stream.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        stream.writeInt(-1); // length, will backpatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        writeInt(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   109
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throw new RuntimeException("Method not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   114
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throw new RuntimeException("Method not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   119
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        stream.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   125
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        crc.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        stream.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   131
    void finish() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Write CRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        stream.writeInt(crc.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Write length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        long pos = stream.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        stream.seek(startPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        stream.writeInt((int)(pos - startPos) - 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Return to end of chunk and flush to minimize buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        stream.seek(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        stream.flushBefore(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   145
    @Override
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 41403
diff changeset
   146
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Empty finalizer (for improved performance; no need to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // super.finalize() in this case)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
// Compress output and write as a series of 'IDAT' chunks of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
// fixed length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
final class IDATOutputStream extends ImageOutputStreamImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   157
    private static final byte[] chunkType = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        (byte)'I', (byte)'D', (byte)'A', (byte)'T'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   161
    private final ImageOutputStream stream;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   162
    private final int chunkLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private long startPos;
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   164
    private final CRC crc = new CRC();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   166
    private final Deflater def;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   167
    private final byte[] buf = new byte[512];
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   168
    // reused 1 byte[] array:
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   169
    private final byte[] wbuf1 = new byte[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private int bytesRemaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   173
    IDATOutputStream(ImageOutputStream stream, int chunkLength,
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   174
                            int deflaterLevel) throws IOException
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   175
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.chunkLength = chunkLength;
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   178
        this.def = new Deflater(deflaterLevel);
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   179
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        startChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private void startChunk() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.startPos = stream.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        stream.writeInt(-1); // length, will backpatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        crc.update(chunkType, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        stream.write(chunkType, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        this.bytesRemaining = chunkLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private void finishChunk() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Write CRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        stream.writeInt(crc.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Write length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        long pos = stream.getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        stream.seek(startPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        stream.writeInt((int)(pos - startPos) - 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Return to end of chunk and flush to minimize buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        stream.seek(pos);
34792
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   205
        try {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   206
            stream.flushBefore(pos);
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   207
        } catch (IOException e) {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   208
            /*
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   209
             * If flushBefore() fails we try to access startPos in finally
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   210
             * block of write_IDAT(). We should update startPos to avoid
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   211
             * IndexOutOfBoundException while seek() is happening.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   212
             */
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   213
            this.startPos = stream.getStreamPosition();
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   214
            throw e;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents: 25859
diff changeset
   215
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   218
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throw new RuntimeException("Method not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   223
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        throw new RuntimeException("Method not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   228
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            def.setInput(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            while (!def.needsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                deflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   242
    void deflate() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int len = def.deflate(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (bytesRemaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                finishChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                startChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int nbytes = Math.min(len, bytesRemaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            crc.update(buf, off, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            stream.write(buf, off, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            bytesRemaining -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   262
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void write(int b) throws IOException {
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   264
        wbuf1[0] = (byte)b;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   265
        write(wbuf1, 0, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   268
    void finish() throws IOException {
1715
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   269
        try {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   270
            if (!def.finished()) {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   271
                def.finish();
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   272
                while (!def.finished()) {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   273
                    deflate();
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   274
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
1715
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   276
            finishChunk();
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   277
        } finally {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
   278
            def.end();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   282
    @Override
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 41403
diff changeset
   283
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // Empty finalizer (for improved performance; no need to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // super.finalize() in this case)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   291
final class PNGImageWriteParam extends ImageWriteParam {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   292
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   293
    /** Default quality level = 0.5 ie medium compression */
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   294
    private static final float DEFAULT_QUALITY = 0.5f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   296
    private static final String[] compressionNames = {"Deflate"};
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   297
    private static final float[] qualityVals = { 0.00F, 0.30F, 0.75F, 1.00F };
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   298
    private static final String[] qualityDescs = {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   299
        "High compression",   // 0.00 -> 0.30
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   300
        "Medium compression", // 0.30 -> 0.75
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   301
        "Low compression"     // 0.75 -> 1.00
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   302
    };
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   303
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   304
    PNGImageWriteParam(Locale locale) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this.canWriteProgressive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        this.locale = locale;
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   308
        this.canWriteCompressed = true;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   309
        this.compressionTypes = compressionNames;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   310
        this.compressionType = compressionTypes[0];
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   311
        this.compressionMode = MODE_DEFAULT;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   312
        this.compressionQuality = DEFAULT_QUALITY;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   313
    }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   314
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   315
    /**
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   316
     * Removes any previous compression quality setting.
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   317
     *
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   318
     * <p> The default implementation resets the compression quality
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   319
     * to <code>0.5F</code>.
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   320
     *
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   321
     * @exception IllegalStateException if the compression mode is not
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   322
     * <code>MODE_EXPLICIT</code>.
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   323
     */
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   324
    @Override
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   325
    public void unsetCompression() {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   326
        super.unsetCompression();
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   327
        this.compressionType = compressionTypes[0];
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   328
        this.compressionQuality = DEFAULT_QUALITY;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   329
    }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   330
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   331
    /**
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   332
     * Returns <code>true</code> since the PNG plug-in only supports
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   333
     * lossless compression.
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   334
     *
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   335
     * @return <code>true</code>.
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   336
     */
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   337
    @Override
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   338
    public boolean isCompressionLossless() {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   339
        return true;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   340
    }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   341
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   342
    @Override
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   343
    public String[] getCompressionQualityDescriptions() {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   344
        super.getCompressionQualityDescriptions();
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   345
        return qualityDescs.clone();
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   346
    }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   347
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   348
    @Override
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   349
    public float[] getCompressionQualityValues() {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   350
        super.getCompressionQualityValues();
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   351
        return qualityVals.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 */
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   357
public final class PNGImageWriter extends ImageWriter {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   358
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   359
    /** Default compression level = 4 ie medium compression */
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   360
    private static final int DEFAULT_COMPRESSION_LEVEL = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    ImageOutputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    PNGMetadata metadata = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // Factors from the ImageWriteParam
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    int sourceXOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    int sourceYOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    int sourceWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    int sourceHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    int[] sourceBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    int periodX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    int periodY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    int numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    int bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    RowFilter rowFilter = new RowFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    byte[] prevRow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    byte[] currRow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    byte[][] filteredRows = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    // Per-band scaling tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    // After the first call to initializeScaleTables, either scale and scale0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    // will be valid, or scaleh and scalel will be valid, but not both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    // The tables will be designed for use with a set of input but depths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // given by sampleSize, and an output bit depth given by scalingBitDepth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    int[] sampleSize = null; // Sample size per band, in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    int scalingBitDepth = -1; // Output bit depth of the scaling tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    // Tables for 1, 2, 4, or 8 bit output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    byte[][] scale = null; // 8 bit table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    byte[] scale0 = null; // equivalent to scale[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    // Tables for 16 bit output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    byte[][] scaleh = null; // High bytes of output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    byte[][] scalel = null; // Low bytes of output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    int totalPixels; // Total number of pixels to be written by write_IDAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    int pixelsDone; // Running count of pixels written by write_IDAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public PNGImageWriter(ImageWriterSpi originatingProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        super(originatingProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   409
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public void setOutput(Object output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        super.setOutput(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (output != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (!(output instanceof ImageOutputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                throw new IllegalArgumentException("output not an ImageOutputStream!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            this.stream = (ImageOutputStream)output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            this.stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   422
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public ImageWriteParam getDefaultWriteParam() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return new PNGImageWriteParam(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   427
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   432
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                               ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        PNGMetadata m = new PNGMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        m.initialize(imageType, imageType.getSampleModel().getNumBands());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public IIOMetadata convertStreamMetadata(IIOMetadata inData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                             ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
   446
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public IIOMetadata convertImageMetadata(IIOMetadata inData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                            ImageTypeSpecifier imageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                            ImageWriteParam param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // TODO - deal with imageType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (inData instanceof PNGMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return (PNGMetadata)((PNGMetadata)inData).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return new PNGMetadata(inData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private void write_magic() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // Write signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        byte[] magic = { (byte)137, 80, 78, 71, 13, 10, 26, 10 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        stream.write(magic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private void write_IHDR() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // Write IHDR chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        ChunkStream cs = new ChunkStream(PNGImageReader.IHDR_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        cs.writeInt(metadata.IHDR_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        cs.writeInt(metadata.IHDR_height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        cs.writeByte(metadata.IHDR_bitDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        cs.writeByte(metadata.IHDR_colorType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (metadata.IHDR_compressionMethod != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throw new IIOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
"Only compression method 0 is defined in PNG 1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        cs.writeByte(metadata.IHDR_compressionMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (metadata.IHDR_filterMethod != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new IIOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
"Only filter method 0 is defined in PNG 1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        cs.writeByte(metadata.IHDR_filterMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (metadata.IHDR_interlaceMethod < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            metadata.IHDR_interlaceMethod > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new IIOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
"Only interlace methods 0 (node) and 1 (adam7) are defined in PNG 1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        cs.writeByte(metadata.IHDR_interlaceMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private void write_cHRM() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (metadata.cHRM_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            ChunkStream cs = new ChunkStream(PNGImageReader.cHRM_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            cs.writeInt(metadata.cHRM_whitePointX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            cs.writeInt(metadata.cHRM_whitePointY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            cs.writeInt(metadata.cHRM_redX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            cs.writeInt(metadata.cHRM_redY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            cs.writeInt(metadata.cHRM_greenX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            cs.writeInt(metadata.cHRM_greenY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            cs.writeInt(metadata.cHRM_blueX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            cs.writeInt(metadata.cHRM_blueY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private void write_gAMA() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (metadata.gAMA_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            ChunkStream cs = new ChunkStream(PNGImageReader.gAMA_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            cs.writeInt(metadata.gAMA_gamma);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private void write_iCCP() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (metadata.iCCP_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            ChunkStream cs = new ChunkStream(PNGImageReader.iCCP_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            cs.writeBytes(metadata.iCCP_profileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            cs.writeByte(0); // null terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            cs.writeByte(metadata.iCCP_compressionMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            cs.write(metadata.iCCP_compressedProfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private void write_sBIT() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (metadata.sBIT_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            ChunkStream cs = new ChunkStream(PNGImageReader.sBIT_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            int colorType = metadata.IHDR_colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (metadata.sBIT_colorType != colorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                processWarningOccurred(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
"sBIT metadata has wrong color type.\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
"The chunk will not be written.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (colorType == PNGImageReader.PNG_COLOR_GRAY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                cs.writeByte(metadata.sBIT_grayBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } else if (colorType == PNGImageReader.PNG_COLOR_RGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                       colorType == PNGImageReader.PNG_COLOR_PALETTE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                       colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                cs.writeByte(metadata.sBIT_redBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                cs.writeByte(metadata.sBIT_greenBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                cs.writeByte(metadata.sBIT_blueBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                cs.writeByte(metadata.sBIT_alphaBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private void write_sRGB() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (metadata.sRGB_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            ChunkStream cs = new ChunkStream(PNGImageReader.sRGB_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            cs.writeByte(metadata.sRGB_renderingIntent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    private void write_PLTE() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (metadata.PLTE_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (metadata.IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
              metadata.IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                // PLTE cannot occur in a gray image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                processWarningOccurred(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
"A PLTE chunk may not appear in a gray or gray alpha image.\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
"The chunk will not be written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            ChunkStream cs = new ChunkStream(PNGImageReader.PLTE_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            int numEntries = metadata.PLTE_red.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            byte[] palette = new byte[numEntries*3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                palette[index++] = metadata.PLTE_red[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                palette[index++] = metadata.PLTE_green[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                palette[index++] = metadata.PLTE_blue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            cs.write(palette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    private void write_hIST() throws IOException, IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (metadata.hIST_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            ChunkStream cs = new ChunkStream(PNGImageReader.hIST_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (!metadata.PLTE_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                throw new IIOException("hIST chunk without PLTE chunk!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            cs.writeChars(metadata.hIST_histogram,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                          0, metadata.hIST_histogram.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    private void write_tRNS() throws IOException, IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (metadata.tRNS_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            ChunkStream cs = new ChunkStream(PNGImageReader.tRNS_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            int colorType = metadata.IHDR_colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            int chunkType = metadata.tRNS_colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // Special case: image is RGB and chunk is Gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // Promote chunk contents to RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            int chunkRed = metadata.tRNS_red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            int chunkGreen = metadata.tRNS_green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            int chunkBlue = metadata.tRNS_blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (colorType == PNGImageReader.PNG_COLOR_RGB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                chunkType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                chunkType = colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                chunkRed = chunkGreen = chunkBlue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    metadata.tRNS_gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (chunkType != colorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                processWarningOccurred(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
"tRNS metadata has incompatible color type.\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
"The chunk will not be written.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (!metadata.PLTE_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    throw new IIOException("tRNS chunk without PLTE chunk!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                cs.write(metadata.tRNS_alpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            } else if (colorType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                cs.writeShort(metadata.tRNS_gray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            } else if (colorType == PNGImageReader.PNG_COLOR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                cs.writeShort(chunkRed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                cs.writeShort(chunkGreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                cs.writeShort(chunkBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                throw new IIOException("tRNS chunk for color type 4 or 6!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    private void write_bKGD() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (metadata.bKGD_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            ChunkStream cs = new ChunkStream(PNGImageReader.bKGD_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            int colorType = metadata.IHDR_colorType & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            int chunkType = metadata.bKGD_colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
49996
39dc39093c5e 6574555: PNGImageWriter incorrectly sets bKGD chunk
jdv
parents: 47216
diff changeset
   654
            int chunkRed = metadata.bKGD_red;
39dc39093c5e 6574555: PNGImageWriter incorrectly sets bKGD chunk
jdv
parents: 47216
diff changeset
   655
            int chunkGreen = metadata.bKGD_green;
39dc39093c5e 6574555: PNGImageWriter incorrectly sets bKGD chunk
jdv
parents: 47216
diff changeset
   656
            int chunkBlue = metadata.bKGD_blue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // Special case: image is RGB(A) and chunk is Gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // Promote chunk contents to RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (colorType == PNGImageReader.PNG_COLOR_RGB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                chunkType == PNGImageReader.PNG_COLOR_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                // Make a gray bKGD chunk look like RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                chunkType = colorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                chunkRed = chunkGreen = chunkBlue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    metadata.bKGD_gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            // Ignore status of alpha in colorType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (chunkType != colorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                processWarningOccurred(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
"bKGD metadata has incompatible color type.\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
"The chunk will not be written.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (colorType == PNGImageReader.PNG_COLOR_PALETTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                cs.writeByte(metadata.bKGD_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            } else if (colorType == PNGImageReader.PNG_COLOR_GRAY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                       colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                cs.writeShort(metadata.bKGD_gray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            } else { // colorType == PNGImageReader.PNG_COLOR_RGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                     // colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                cs.writeShort(chunkRed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                cs.writeShort(chunkGreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                cs.writeShort(chunkBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private void write_pHYs() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (metadata.pHYs_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            ChunkStream cs = new ChunkStream(PNGImageReader.pHYs_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            cs.writeInt(metadata.pHYs_pixelsPerUnitXAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            cs.writeInt(metadata.pHYs_pixelsPerUnitYAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            cs.writeByte(metadata.pHYs_unitSpecifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    private void write_sPLT() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (metadata.sPLT_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            ChunkStream cs = new ChunkStream(PNGImageReader.sPLT_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            cs.writeBytes(metadata.sPLT_paletteName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            cs.writeByte(0); // null terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            cs.writeByte(metadata.sPLT_sampleDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            int numEntries = metadata.sPLT_red.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (metadata.sPLT_sampleDepth == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    cs.writeByte(metadata.sPLT_red[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    cs.writeByte(metadata.sPLT_green[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    cs.writeByte(metadata.sPLT_blue[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    cs.writeByte(metadata.sPLT_alpha[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    cs.writeShort(metadata.sPLT_frequency[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            } else { // sampleDepth == 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    cs.writeShort(metadata.sPLT_red[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    cs.writeShort(metadata.sPLT_green[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    cs.writeShort(metadata.sPLT_blue[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    cs.writeShort(metadata.sPLT_alpha[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    cs.writeShort(metadata.sPLT_frequency[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    private void write_tIME() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (metadata.tIME_present) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            ChunkStream cs = new ChunkStream(PNGImageReader.tIME_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            cs.writeShort(metadata.tIME_year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            cs.writeByte(metadata.tIME_month);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            cs.writeByte(metadata.tIME_day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            cs.writeByte(metadata.tIME_hour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            cs.writeByte(metadata.tIME_minute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            cs.writeByte(metadata.tIME_second);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    private void write_tEXt() throws IOException {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   745
        Iterator<String> keywordIter = metadata.tEXt_keyword.iterator();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   746
        Iterator<String> textIter = metadata.tEXt_text.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        while (keywordIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            ChunkStream cs = new ChunkStream(PNGImageReader.tEXt_TYPE, stream);
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   750
            String keyword = keywordIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            cs.writeBytes(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            cs.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   754
            String text = textIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            cs.writeBytes(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   760
    private byte[] deflate(byte[] b) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 1734
diff changeset
   763
        dos.write(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    private void write_iTXt() throws IOException {
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   769
        Iterator<String> keywordIter = metadata.iTXt_keyword.iterator();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   770
        Iterator<Boolean> flagIter = metadata.iTXt_compressionFlag.iterator();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   771
        Iterator<Integer> methodIter = metadata.iTXt_compressionMethod.iterator();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   772
        Iterator<String> languageIter = metadata.iTXt_languageTag.iterator();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   773
        Iterator<String> translatedKeywordIter =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            metadata.iTXt_translatedKeyword.iterator();
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   775
        Iterator<String> textIter = metadata.iTXt_text.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        while (keywordIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            ChunkStream cs = new ChunkStream(PNGImageReader.iTXt_TYPE, stream);
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   779
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   780
            cs.writeBytes(keywordIter.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            cs.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   783
            Boolean compressed = flagIter.next();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   784
            cs.writeByte(compressed ? 1 : 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   786
            cs.writeByte(methodIter.next().intValue());
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   787
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   788
            cs.writeBytes(languageIter.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            cs.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   791
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   792
            cs.write(translatedKeywordIter.next().getBytes("UTF8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            cs.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   795
            String text = textIter.next();
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   796
            if (compressed) {
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   797
                cs.write(deflate(text.getBytes("UTF8")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            } else {
1734
861400729115 6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents: 1715
diff changeset
   799
                cs.write(text.getBytes("UTF8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    private void write_zTXt() throws IOException {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   806
        Iterator<String> keywordIter = metadata.zTXt_keyword.iterator();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   807
        Iterator<Integer> methodIter = metadata.zTXt_compressionMethod.iterator();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   808
        Iterator<String> textIter = metadata.zTXt_text.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        while (keywordIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            ChunkStream cs = new ChunkStream(PNGImageReader.zTXt_TYPE, stream);
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   812
            String keyword = keywordIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            cs.writeBytes(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            cs.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   816
            int compressionMethod = (methodIter.next()).intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            cs.writeByte(compressionMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   819
            String text = textIter.next();
2376
63e13f6d2319 6782079: PNG: reading metadata may cause OOM on truncated images.
bae
parents: 1734
diff changeset
   820
            cs.write(deflate(text.getBytes("ISO-8859-1")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    private void writeUnknownChunks() throws IOException {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   826
        Iterator<String> typeIter = metadata.unknownChunkType.iterator();
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   827
        Iterator<byte[]> dataIter = metadata.unknownChunkData.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        while (typeIter.hasNext() && dataIter.hasNext()) {
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   830
            String type = typeIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            ChunkStream cs = new ChunkStream(chunkType(type), stream);
23312
4711f66e7d5c 8033716: Fix raw and unchecked lint warnings in com.sun.imageio
henryjen
parents: 5506
diff changeset
   832
            byte[] data = dataIter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            cs.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    private static int chunkType(String typeString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        char c0 = typeString.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        char c1 = typeString.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        char c2 = typeString.charAt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        char c3 = typeString.charAt(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        int type = (c0 << 24) | (c1 << 16) | (c2 << 8) | c3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    private void encodePass(ImageOutputStream os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                            RenderedImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                            int xOffset, int yOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                            int xSkip, int ySkip) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int minX = sourceXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        int minY = sourceYOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        int width = sourceWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        int height = sourceHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Adjust offsets and skips based on source subsampling factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        xOffset *= periodX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        xSkip *= periodX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        yOffset *= periodY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        ySkip *= periodY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // Early exit if no data for this pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int hpixels = (width - xOffset + xSkip - 1)/xSkip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        int vpixels = (height - yOffset + ySkip - 1)/ySkip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        if (hpixels == 0 || vpixels == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        // Convert X offset and skip from pixels to samples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        xOffset *= numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        xSkip *= numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        // Create row buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        int samplesPerByte = 8/metadata.IHDR_bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        int numSamples = width*numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        int[] samples = new int[numSamples];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        int bytesPerRow = hpixels*numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (metadata.IHDR_bitDepth < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            bytesPerRow = (bytesPerRow + samplesPerByte - 1)/samplesPerByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } else if (metadata.IHDR_bitDepth == 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            bytesPerRow *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        IndexColorModel icm_gray_alpha = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        if (metadata.IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            image.getColorModel() instanceof IndexColorModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // reserve space for alpha samples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            bytesPerRow *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            // will be used to calculate alpha value for the pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            icm_gray_alpha = (IndexColorModel)image.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        currRow = new byte[bytesPerRow + bpp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        prevRow = new byte[bytesPerRow + bpp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        filteredRows = new byte[5][bytesPerRow + bpp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        int bitDepth = metadata.IHDR_bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        for (int row = minY + yOffset; row < minY + height; row += ySkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            Rectangle rect = new Rectangle(minX, row, width, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            Raster ras = image.getData(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if (sourceBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                ras = ras.createChild(minX, row, width, 1, minX, row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                      sourceBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            ras.getPixels(minX, row, width, 1, samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (image.getColorModel().isAlphaPremultiplied()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                WritableRaster wr = ras.createCompatibleWritableRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                wr.setPixels(wr.getMinX(), wr.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                             wr.getWidth(), wr.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                             samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                image.getColorModel().coerceData(wr, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                wr.getPixels(wr.getMinX(), wr.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                             wr.getWidth(), wr.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                             samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // Reorder palette data if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            int[] paletteOrder = metadata.PLTE_order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (paletteOrder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                for (int i = 0; i < numSamples; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    samples[i] = paletteOrder[samples[i]];
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
            int count = bpp; // leave first 'bpp' bytes zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            int tmp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            switch (bitDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            case 1: case 2: case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                // Image can only have a single band
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                int mask = samplesPerByte - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                for (int s = xOffset; s < numSamples; s += xSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    byte val = scale0[samples[s]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    tmp = (tmp << bitDepth) | val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    if ((pos++ & mask) == mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        currRow[count++] = (byte)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                        tmp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                // Left shift the last byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                if ((pos & mask) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    tmp <<= ((8/bitDepth) - pos)*bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    currRow[count++] = (byte)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            case 8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                if (numBands == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    for (int s = xOffset; s < numSamples; s += xSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        currRow[count++] = scale0[samples[s]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        if (icm_gray_alpha != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                            currRow[count++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                                scale0[icm_gray_alpha.getAlpha(0xff & samples[s])];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    for (int s = xOffset; s < numSamples; s += xSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        for (int b = 0; b < numBands; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                            currRow[count++] = scale[b][samples[s + b]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            case 16:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                for (int s = xOffset; s < numSamples; s += xSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    for (int b = 0; b < numBands; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        currRow[count++] = scaleh[b][samples[s + b]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                        currRow[count++] = scalel[b][samples[s + b]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            // Perform filtering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            int filterType = rowFilter.filterRow(metadata.IHDR_colorType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                                                 currRow, prevRow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                                 filteredRows,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                                                 bytesPerRow, bpp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            os.write(filterType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            os.write(filteredRows[filterType], bpp, bytesPerRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            // Swap current and previous rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            byte[] swap = currRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            currRow = prevRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            prevRow = swap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            pixelsDone += hpixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            processImageProgress(100.0F*pixelsDone/totalPixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // If write has been aborted, just return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            // processWriteAborted will be called later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (abortRequested()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    // Use sourceXOffset, etc.
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1013
    private void write_IDAT(RenderedImage image, int deflaterLevel)
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1014
        throws IOException
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1015
    {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1016
        IDATOutputStream ios = new IDATOutputStream(stream, 32768,
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1017
                                                    deflaterLevel);
1715
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1018
        try {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1019
            if (metadata.IHDR_interlaceMethod == 1) {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1020
                for (int i = 0; i < 7; i++) {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1021
                    encodePass(ios, image,
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1022
                               PNGImageReader.adam7XOffset[i],
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1023
                               PNGImageReader.adam7YOffset[i],
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1024
                               PNGImageReader.adam7XSubsampling[i],
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1025
                               PNGImageReader.adam7YSubsampling[i]);
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1026
                    if (abortRequested()) {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1027
                        break;
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1028
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
1715
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1030
            } else {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1031
                encodePass(ios, image, 0, 0, 1, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
1715
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1033
        } finally {
349c53487e78 6687968: PNGImageReader leaks native memory through an Inflater.
bae
parents: 2
diff changeset
  1034
            ios.finish();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    private void writeIEND() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        ChunkStream cs = new ChunkStream(PNGImageReader.IEND_TYPE, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        cs.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    // Check two int arrays for value equality, always returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    // if either array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    private boolean equals(int[] s0, int[] s1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (s0 == null || s1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (s0.length != s1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        for (int i = 0; i < s0.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (s0[i] != s1[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    // Initialize the scale/scale0 or scaleh/scalel arrays to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    // hold the results of scaling an input value to the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    // output bit depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private void initializeScaleTables(int[] sampleSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        int bitDepth = metadata.IHDR_bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        // If the existing tables are still valid, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        if (bitDepth == scalingBitDepth &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            equals(sampleSize, this.sampleSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        // Compute new tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        this.sampleSize = sampleSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        this.scalingBitDepth = bitDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        int maxOutSample = (1 << bitDepth) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        if (bitDepth <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            scale = new byte[numBands][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            for (int b = 0; b < numBands; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                int maxInSample = (1 << sampleSize[b]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                int halfMaxInSample = maxInSample/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                scale[b] = new byte[maxInSample + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                for (int s = 0; s <= maxInSample; s++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    scale[b][s] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                        (byte)((s*maxOutSample + halfMaxInSample)/maxInSample);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            scale0 = scale[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            scaleh = scalel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        } else { // bitDepth == 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // Divide scaling table into high and low bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            scaleh = new byte[numBands][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            scalel = new byte[numBands][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            for (int b = 0; b < numBands; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                int maxInSample = (1 << sampleSize[b]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                int halfMaxInSample = maxInSample/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                scaleh[b] = new byte[maxInSample + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                scalel[b] = new byte[maxInSample + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                for (int s = 0; s <= maxInSample; s++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    int val = (s*maxOutSample + halfMaxInSample)/maxInSample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    scaleh[b][s] = (byte)(val >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    scalel[b][s] = (byte)(val & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            scale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            scale0 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1110
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    public void write(IIOMetadata streamMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                      IIOImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                      ImageWriteParam param) throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            throw new IllegalStateException("output == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            throw new IllegalArgumentException("image == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (image.hasRaster()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            throw new UnsupportedOperationException("image has a Raster!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        RenderedImage im = image.getRenderedImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        SampleModel sampleModel = im.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        this.numBands = sampleModel.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        // Set source region and subsampling to default values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        this.sourceXOffset = im.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        this.sourceYOffset = im.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        this.sourceWidth = im.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        this.sourceHeight = im.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        this.sourceBands = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        this.periodX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        this.periodY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            // Get source region and subsampling factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            Rectangle sourceRegion = param.getSourceRegion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            if (sourceRegion != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                Rectangle imageBounds = new Rectangle(im.getMinX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                                                      im.getMinY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                                                      im.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                                                      im.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                // Clip to actual image bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                sourceRegion = sourceRegion.intersection(imageBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                sourceXOffset = sourceRegion.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                sourceYOffset = sourceRegion.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                sourceWidth = sourceRegion.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                sourceHeight = sourceRegion.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            // Adjust for subsampling offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            int gridX = param.getSubsamplingXOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            int gridY = param.getSubsamplingYOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            sourceXOffset += gridX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            sourceYOffset += gridY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            sourceWidth -= gridX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            sourceHeight -= gridY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            // Get subsampling factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            periodX = param.getSourceXSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            periodY = param.getSourceYSubsampling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            int[] sBands = param.getSourceBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            if (sBands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                sourceBands = sBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                numBands = sourceBands.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Compute output dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        int destWidth = (sourceWidth + periodX - 1)/periodX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int destHeight = (sourceHeight + periodY - 1)/periodY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        if (destWidth <= 0 || destHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            throw new IllegalArgumentException("Empty source region!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // Compute total number of pixels for progress notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        this.totalPixels = destWidth*destHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        this.pixelsDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        // Create metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        IIOMetadata imd = image.getMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        if (imd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            metadata = (PNGMetadata)convertImageMetadata(imd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                               ImageTypeSpecifier.createFromRenderedImage(im),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            metadata = new PNGMetadata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1193
        // reset compression level to default:
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1194
        int deflaterLevel = DEFAULT_COMPRESSION_LEVEL;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1195
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (param != null) {
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1197
            switch(param.getCompressionMode()) {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1198
            case ImageWriteParam.MODE_DISABLED:
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1199
                deflaterLevel = Deflater.NO_COMPRESSION;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1200
                break;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1201
            case ImageWriteParam.MODE_EXPLICIT:
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1202
                float quality = param.getCompressionQuality();
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1203
                if (quality >= 0f && quality <= 1f) {
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1204
                    deflaterLevel = 9 - Math.round(9f * quality);
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1205
                }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1206
                break;
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1207
            default:
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1208
            }
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            // Use Adam7 interlacing if set in write param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            switch (param.getProgressiveMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            case ImageWriteParam.MODE_DEFAULT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                metadata.IHDR_interlaceMethod = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            case ImageWriteParam.MODE_DISABLED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                metadata.IHDR_interlaceMethod = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                break;
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1218
                // MODE_COPY_FROM_METADATA should already be taken care of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                // MODE_EXPLICIT is not allowed
35648
091db1c018f5 8145608: PNG writer should permit setting compression level and iDAT chunk maximum size
lbourges
parents: 35647
diff changeset
  1220
            default:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        // Initialize bitDepth and colorType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        metadata.initialize(new ImageTypeSpecifier(im), numBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // Overwrite IHDR width and height values with values from image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        metadata.IHDR_width = destWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        metadata.IHDR_height = destHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        this.bpp = numBands*((metadata.IHDR_bitDepth == 16) ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        // Initialize scaling tables for this image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        initializeScaleTables(sampleModel.getSampleSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        clearAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        processImageStarted(0);
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1239
        if (abortRequested()) {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1240
            processWriteAborted();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1241
        } else {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1242
            try {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1243
                write_magic();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1244
                write_IHDR();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1246
                write_cHRM();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1247
                write_gAMA();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1248
                write_iCCP();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1249
                write_sBIT();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1250
                write_sRGB();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1252
                write_PLTE();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1254
                write_hIST();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1255
                write_tRNS();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1256
                write_bKGD();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1258
                write_pHYs();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1259
                write_sPLT();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1260
                write_tIME();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1261
                write_tEXt();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1262
                write_iTXt();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1263
                write_zTXt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1265
                writeUnknownChunks();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1266
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1267
                write_IDAT(im, deflaterLevel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
41403
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1269
                if (abortRequested()) {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1270
                    processWriteAborted();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1271
                } else {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1272
                    // Finish up and inform the listeners we are done
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1273
                    writeIEND();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1274
                    processImageComplete();
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1275
                }
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1276
            } catch (IOException e) {
be56daafbeaa 8164931: Verify if writer.abort() works properly for all writers in IIOWriteProgressListener.
jdv
parents: 35648
diff changeset
  1277
                throw new IIOException("I/O error writing PNG file!", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
}