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)
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
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
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
.nio
.ByteBuffer
;
43 import java
.nio
.ByteOrder
;
44 import java
.awt
.image
.IndexColorModel
;
45 import java
.awt
.image
.BufferedImage
;
47 public abstract class BMPDecoder
{
49 protected BMPInfoHeader infoHeader
;
50 protected BMPFileHeader fileHeader
;
51 protected long offset
;
53 public BMPDecoder(BMPFileHeader fh
, BMPInfoHeader ih
){
56 offset
= BMPFileHeader
.SIZE
+ BMPInfoHeader
.SIZE
;
60 * Determines the coding type of the bitmap and returns the corresponding
63 public static BMPDecoder
getDecoder(BMPFileHeader fh
, BMPInfoHeader ih
){
64 switch(ih
.getCompression()){
65 case BMPInfoHeader
.BI_RGB
: // uncompressed RGB
66 switch(ih
.getBitCount()){
68 return new DecodeBF32(fh
, ih
, true);
71 return new DecodeRGB24(fh
, ih
);
74 return new DecodeBF16(fh
, ih
, true);
77 return new DecodeRGB8(fh
, ih
);
80 return new DecodeRGB4(fh
, ih
);
83 return new DecodeRGB1(fh
, ih
);
89 case BMPInfoHeader
.BI_RLE8
:
90 return new DecodeRLE8(fh
, ih
);
92 case BMPInfoHeader
.BI_RLE4
:
93 return new DecodeRLE4(fh
, ih
);
95 case BMPInfoHeader
.BI_BITFIELDS
:
96 switch(ih
.getBitCount()){
98 return new DecodeBF16(fh
, ih
, false);
101 return new DecodeBF32(fh
, ih
, false);
115 public abstract BufferedImage
decode(ImageInputStream in
)
116 throws IOException
, BMPException
;
119 * Reads r,g,b bit masks from an inputstream
121 protected int[] readBitMasks(ImageInputStream in
) throws IOException
{
122 int[] bitmasks
= new int[3];
123 byte[] temp
= new byte[12];
124 if(in
.read(temp
) != 12)
125 throw new IOException("Couldn't read bit masks.");
128 ByteBuffer buf
= ByteBuffer
.wrap(temp
);
129 buf
.order(ByteOrder
.LITTLE_ENDIAN
);
130 bitmasks
[0] = buf
.getInt();
131 bitmasks
[1] = buf
.getInt();
132 bitmasks
[2] = buf
.getInt();
137 * Reads an N-color palette from an inputstream in RGBQUAD format and
138 * returns an equivalent ColorModel object
140 protected IndexColorModel
readPalette(ImageInputStream in
) throws IOException
{
141 int N
= infoHeader
.getNumberOfPaletteEntries();
142 byte[] r
= new byte[N
];
143 byte[] g
= new byte[N
];
144 byte[] b
= new byte[N
];
145 for(int i
=0;i
<N
;i
++){
146 byte[] RGBquad
= new byte[4];
147 if(in
.read(RGBquad
) != 4)
148 throw new IOException("Error reading palette information.");
149 // RGBQUAD structure is b,g,r,0
156 return new IndexColorModel(8, N
, r
, g
, b
);
160 * Read bytes to the start of the image data
162 protected void skipToImage(ImageInputStream in
) throws IOException
{
163 byte[] d
= new byte[1];
164 long n
= fileHeader
.getOffset() - offset
;