1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: public class BMPInfoHeader {
48:
49: private int biSize;
50:
51:
52: private int biWidth;
53:
54:
55: private int biHeight;
56:
57:
58: private short biPlanes;
59:
60:
61: private short biBitCount;
62:
63:
64: private int biCompression;
65:
66:
67: private int biSizeImage;
68:
69:
70: private int biXPelsPerMeter;
71:
72:
73: private int biYPelsPerMeter;
74:
75:
76: private int biClrUsed;
77:
78:
79: private int biClrImportant;
80:
81:
82: public static final int SIZE = 40;
83:
84:
87: public static final int BI_RGB = 0;
88: public static final int BI_RLE8 = 1;
89: public static final int BI_RLE4 = 2;
90: public static final int BI_BITFIELDS = 3;
91:
92:
97: public BMPInfoHeader(ImageInputStream in) throws IOException, BMPException {
98: byte[] data = new byte[SIZE];
99:
100: if (in.read(data) != SIZE)
101: throw new IOException("Couldn't read header.");
102: ByteBuffer buf = ByteBuffer.wrap(data);
103: buf.order(ByteOrder.LITTLE_ENDIAN);
104:
105: int n;
106: if((n=buf.getInt()) != SIZE)
107: throw new BMPException("Invalid BITMAPINFOHEADER size: "+n);
108:
109: biWidth = buf.getInt();
110: biHeight = buf.getInt();
111: biPlanes = buf.getShort();
112: setBitCount(buf.getShort());
113: setCompression(buf.getInt());
114: biSizeImage = buf.getInt();
115: biXPelsPerMeter = buf.getInt();
116: biYPelsPerMeter = buf.getInt();
117: biClrUsed = buf.getInt();
118: biClrImportant = buf.getInt();
119: }
120:
121: public void setBitCount(short bitcount) throws BMPException {
122: switch(bitcount){
123: case 1:
124: case 4:
125: case 8:
126: case 16:
127: case 24:
128: case 32:
129: biBitCount = bitcount;
130: break;
131:
132: default:
133: throw new BMPException("Invalid number of bits per pixel: "+
134: bitcount);
135: }
136: }
137:
138: public short getBitCount() { return biBitCount; }
139:
140: public void setCompression(int compression) throws BMPException {
141: switch(compression){
142: case BI_RLE8:
143: if(getBitCount() != 8)
144: throw new BMPException("Invalid number of bits per pixel.");
145: biCompression = compression;
146: break;
147: case BI_RLE4:
148: if(getBitCount() != 4)
149: throw new BMPException("Invalid number of bits per pixel.");
150: biCompression = compression;
151: break;
152:
153: case BI_RGB:
154: case BI_BITFIELDS:
155: biCompression = compression;
156: break;
157:
158: default:
159: throw new BMPException("Unknown bitmap compression type.");
160: }
161: }
162:
163: public int getNumberOfPaletteEntries(){
164: if(biClrUsed == 0)
165: switch(biBitCount){
166: case 1:
167: return 2;
168: case 4:
169: return 16;
170: case 8:
171: return 256;
172:
173: default:
174: return 0;
175: }
176:
177: return biClrUsed;
178: }
179:
180: public int getCompression(){
181: return biCompression;
182: }
183:
184: public Dimension getSize(){
185: return new Dimension(biWidth, biHeight);
186: }
187:
188: public int getWidth(){
189: return biWidth;
190: }
191:
192: public int getHeight(){
193: return biHeight;
194: }
195:
196: public void setSize(Dimension d){
197: biWidth = (int)d.getWidth();
198: biHeight = (int)d.getHeight();
199: }
200:
201: }