jdk/src/share/classes/sun/awt/image/XbmImageDecoder.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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: 2
diff changeset
     2
 * Copyright (c) 1995, 2003, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      Reads xbitmap format images into a DIBitmap structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Parse files of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * #define foo_width w
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * #define foo_height h
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * static char foo_bits[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * 0xnn,0xnn,0xnn,0xnn};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class XbmImageDecoder extends ImageDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static byte XbmColormap[] = {(byte) 255, (byte) 255, (byte) 255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                         0, 0, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int XbmHints = (ImageConsumer.TOPDOWNLEFTRIGHT |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                   ImageConsumer.COMPLETESCANLINES |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                   ImageConsumer.SINGLEPASS |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                   ImageConsumer.SINGLEFRAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public XbmImageDecoder(InputStreamImageSource src, InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(src, is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (!(input instanceof BufferedInputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // If the topmost stream is a metered stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // we take forever to decode the image...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            input = new BufferedInputStream(input, 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * An error has occurred. Throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static void error(String s1) throws ImageFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        throw new ImageFormatException(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * produce an image from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void produceImage() throws IOException, ImageFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        char nm[] = new char[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int H = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int W = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        boolean start = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte raster[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        IndexColorModel model = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        while (!aborted && (c = input.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if ('a' <= c && c <= 'z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    'A' <= c && c <= 'Z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    '0' <= c && c <= '9' || c == '#' || c == '_') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (i < 78)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    nm[i++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                int nc = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    if (nc != 7 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        nm[0] != '#' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        nm[1] != 'd' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        nm[2] != 'e' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        nm[3] != 'f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        nm[4] != 'i' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        nm[5] != 'n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        nm[6] != 'e')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        error("Not an XBM file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    start = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (nm[nc - 1] == 'h')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    state = 1;  /* expecting width */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                else if (nm[nc - 1] == 't' && nc > 1 && nm[nc - 2] == 'h')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    state = 2;  /* expecting height */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                else if (nc > 2 && state < 0 && nm[0] == '0' && nm[1] == 'x') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    for (int p = 2; p < nc; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        c = nm[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        if ('0' <= c && c <= '9')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            c = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        else if ('A' <= c && c <= 'Z')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            c = c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        else if ('a' <= c && c <= 'z')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            c = c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        n = n * 16 + c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    for (int mask = 1; mask <= 0x80; mask <<= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        if (x < W) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            if ((n & mask) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                raster[x] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                raster[x] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (x >= W) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        if (setPixels(0, y, W, 1, model, raster, 0, W) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        if (y++ >= H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    for (int p = 0; p < nc; p++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        if ('0' <= (c = nm[p]) && c <= '9')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                            n = n * 10 + c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    if (n > 0 && state > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        if (state == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            W = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            H = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        if (W == 0 || H == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            model = new IndexColorModel(8, 2, XbmColormap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                        0, false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            setDimensions(W, H);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            setColorModel(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            setHints(XbmHints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            headerComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            raster = new byte[W];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            state = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        }
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
        input.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        imageComplete(ImageConsumer.STATICIMAGEDONE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}