1 |
14 |
jlechner |
/* BMPFileHeader.java --
|
2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
package gnu.javax.imageio.bmp;
|
39 |
|
|
|
40 |
|
|
import java.io.IOException;
|
41 |
|
|
import javax.imageio.stream.ImageInputStream;
|
42 |
|
|
import java.io.OutputStream;
|
43 |
|
|
import java.nio.ByteBuffer;
|
44 |
|
|
import java.nio.ByteOrder;
|
45 |
|
|
|
46 |
|
|
public class BMPFileHeader {
|
47 |
|
|
/** Header signature, always 'BM' */
|
48 |
|
|
private final static short bfType = 0x424d;
|
49 |
|
|
|
50 |
|
|
/** Bitmap file size, in bytes. */
|
51 |
|
|
private long bfSize;
|
52 |
|
|
|
53 |
|
|
/** Offset from the beginning of the file to the bitmap data */
|
54 |
|
|
private long bfOffBits;
|
55 |
|
|
|
56 |
|
|
/** BITMAPFILEHEADER is 14 bytes */
|
57 |
|
|
public static final int SIZE = 14;
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* Creates the header from an input stream, which is not closed.
|
61 |
|
|
* @throws IOException if an I/O error occured.
|
62 |
|
|
* @throws BMPException if the header was invalid
|
63 |
|
|
*/
|
64 |
|
|
public BMPFileHeader(ImageInputStream in) throws IOException, BMPException {
|
65 |
|
|
byte[] data = new byte[SIZE];
|
66 |
|
|
|
67 |
|
|
if (in.read(data) != SIZE)
|
68 |
|
|
throw new IOException("Couldn't read header.");
|
69 |
|
|
ByteBuffer buf = ByteBuffer.wrap(data);
|
70 |
|
|
|
71 |
|
|
if(buf.getShort(0) != bfType)
|
72 |
|
|
throw new BMPException("Not a BMP file.");
|
73 |
|
|
|
74 |
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
75 |
|
|
|
76 |
|
|
// get size (keep unsigned)
|
77 |
|
|
bfSize = ((long)buf.getInt(2) & (0xFFFFFFFF));
|
78 |
|
|
|
79 |
|
|
// Two reserved shorts are here, and should be zero,
|
80 |
|
|
// perhaps they should be tested to be zero, but I don't
|
81 |
|
|
// feel this strictness is necessary.
|
82 |
|
|
|
83 |
|
|
bfOffBits = ((long)buf.getInt(10) & (0xFFFFFFFF));
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* Writes the header to an output stream, which is not closed or flushed.
|
88 |
|
|
* @throws IOException if an I/O error occured.
|
89 |
|
|
*/
|
90 |
|
|
public void write(OutputStream out) throws IOException {
|
91 |
|
|
ByteBuffer buf = ByteBuffer.allocate(SIZE);
|
92 |
|
|
buf.putShort(0, bfType); // ID
|
93 |
|
|
buf.putInt(2, (int)(bfSize & (0xFFFFFFFF))); // size
|
94 |
|
|
buf.putInt(6, 0); // 4 reserved bytes set to zero
|
95 |
|
|
buf.putInt(2, (int)(bfOffBits & (0xFFFFFFFF))); // size
|
96 |
|
|
out.write(buf.array());
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
/**
|
100 |
|
|
* Sets the file size
|
101 |
|
|
*/
|
102 |
|
|
public void setSize(long size){
|
103 |
|
|
bfSize = size;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Sets the bitmap offset within the file
|
108 |
|
|
*/
|
109 |
|
|
public void setOffset(long offset){
|
110 |
|
|
bfOffBits = offset;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Gets the file size
|
115 |
|
|
*/
|
116 |
|
|
public long getSize(){
|
117 |
|
|
return bfSize;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* Gets the bitmap offset within the file
|
122 |
|
|
*/
|
123 |
|
|
public long getOffset(){
|
124 |
|
|
return bfOffBits;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
|