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