jdk/src/share/classes/sun/awt/image/ImageDecoder.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public abstract class ImageDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    InputStreamImageSource source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    Thread feeder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected boolean aborted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected boolean finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    ImageConsumerQueue queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    ImageDecoder next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public ImageDecoder(InputStreamImageSource src, InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        source = src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        feeder = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public boolean isConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return ImageConsumerQueue.isConsumer(queue, ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public void removeConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        queue = ImageConsumerQueue.removeConsumer(queue, ic, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (!finished && queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected ImageConsumerQueue nextConsumer(ImageConsumerQueue cq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        synchronized (source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            cq = ((cq == null) ? queue : cq.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            while (cq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                if (cq.interested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    return cq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                cq = cq.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected int setDimensions(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            cq.consumer.setDimensions(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected int setProperties(Hashtable props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            cq.consumer.setProperties(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected int setColorModel(ColorModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            cq.consumer.setColorModel(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected int setHints(int hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            cq.consumer.setHints(hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void headerComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        feeder.setPriority(ImageFetcher.LOW_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected int setPixels(int x, int y, int w, int h, ColorModel model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            byte pix[], int off, int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        source.latchConsumers(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected int setPixels(int x, int y, int w, int h, ColorModel model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            int pix[], int off, int scansize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        source.latchConsumers(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected int imageComplete(int status, boolean done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        source.latchConsumers(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            source.doneDecoding(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ImageConsumerQueue cq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        while ((cq = nextConsumer(cq)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            cq.consumer.imageComplete(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public abstract void produceImage() throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                               ImageFormatException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        source.doneDecoding(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                feeder.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public synchronized void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (input != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                input.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}