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