Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / javax / imageio / bmp / DecodeRLE4.java
blob55d314f05f1e2df51890be9caa8ba0b51b88898e
1 /* DecodeRLE4.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.javax.imageio.bmp;
40 import java.io.IOException;
41 import javax.imageio.stream.ImageInputStream;
42 import java.awt.image.BufferedImage;
43 import java.awt.image.IndexColorModel;
44 import java.awt.image.Raster;
45 import java.awt.image.WritableRaster;
46 import java.awt.image.DataBuffer;
47 import java.awt.image.DataBufferByte;
48 import java.awt.image.MultiPixelPackedSampleModel;
49 import java.awt.image.SampleModel;
50 import java.awt.Dimension;
52 public class DecodeRLE4 extends BMPDecoder {
54 public DecodeRLE4(BMPFileHeader fh, BMPInfoHeader ih){
55 super(fh, ih);
58 /**
59 * RLE control codes
61 private static final byte ESCAPE = (byte)0;
62 private static final byte EOL = (byte)0; // end of line
63 private static final byte EOB = (byte)1; // end of bitmap
64 private static final byte DELTA = (byte)2; // delta
66 public BufferedImage decode(ImageInputStream in) throws IOException, BMPException {
67 IndexColorModel palette = readPalette(in);
68 skipToImage(in);
70 Dimension d = infoHeader.getSize();
71 int h = (int)d.getHeight();
72 int w = (int)d.getWidth();
74 byte[] data = uncompress(w, h, in);
75 SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
76 w, h, 4);
78 DataBuffer db = new DataBufferByte(data, w*h, 0);
79 WritableRaster raster = Raster.createWritableRaster(sm, db, null);
81 return new BufferedImage(palette, raster, false, null);
84 private byte[] uncompress(int w, int h, ImageInputStream in)
85 throws BMPException, IOException {
86 byte[] cmd = new byte[2];
87 byte[] data = new byte[w*h>>1];
88 int offIn = 0;
89 int x=0,y=0;
91 // width in bytes
92 w += (w&1);
93 w = w >> 1;
95 try {
96 while(((x>>1) + y*w) < w*h){
97 if(in.read(cmd) != 2)
98 throw new IOException("Error reading compressed data.");
100 if(cmd[0] == ESCAPE){
101 switch(cmd[1]){
102 case EOB: // end of bitmap
103 return data;
104 case EOL: // end of line
105 x = 0;
106 y++;
107 break;
108 case DELTA: // delta
109 if(in.read(cmd) != 2)
110 throw new IOException("Error reading compressed data.");
111 int dx = cmd[0] & (0xFF);
112 int dy = cmd[1] & (0xFF);
113 x += dx;
114 y += dy;
115 break;
117 default:
118 // decode a literal run
119 int length = cmd[1] & (0xFF);
121 // size of run, which is word aligned.
122 int bytesize = length;
123 bytesize += (bytesize & 1);
124 bytesize >>= 1;
125 bytesize += (bytesize & 1);
127 byte[] run = new byte[bytesize];
128 if(in.read(run) != bytesize)
129 throw new IOException("Error reading compressed data.");
131 if((x&1) == 0){
132 length += (length&1);
133 length >>= 1;
134 System.arraycopy(run, 0, data, ((x>>1) + w*(h-y-1)),
135 length);
136 } else {
137 for(int i=0;i<length;i++){
138 if((i&1) == 0) // copy high to low
139 data[((x+i)>>1) + w*(h-y-1)]
140 |= ((run[i>>1]&0xF0) >> 4);
141 else // copy low to high
142 data[((x+i)>>1) + w*(h-y-1)]
143 |= ((run[i>>1]&0x0F) << 4);
146 x += cmd[1] & (0xFF);
147 break;
149 } else {
150 // decode a byte run
151 int length = cmd[0] & (0xFF);
152 if((x&1) == 0){
153 length += (length&1);
154 length >>= 1;
155 for(int i=0;i<length;i++)
156 data[(h-y-1)*w + i + (x >> 1)] = cmd[1];
157 } else {
158 for(int i=0;i<length;i++){
159 if((i&1) == 0) // copy high to low
160 data[((x+i)>>1) + w*(h-y-1)]
161 |= ((cmd[1]&0xF0) >> 4);
162 else // copy low to high
163 data[((x+i)>>1) + w*(h-y-1)]
164 |= ((cmd[1]&0x0F) << 4);
167 x += cmd[0] & (0xFF);
170 return data;
171 } catch(ArrayIndexOutOfBoundsException e){
172 throw new BMPException("Invalid RLE data.");