Merge from mainline.
[official-gcc.git] / libjava / classpath / gnu / javax / imageio / bmp / BMPFileHeader.java
blob4ba32d3c30a9e8700caaad9054fd505855b7421b
1 /* BMPFileHeader.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.awt.image.RenderedImage;
41 import java.io.IOException;
42 import java.nio.ByteBuffer;
43 import java.nio.ByteOrder;
45 import javax.imageio.IIOImage;
46 import javax.imageio.stream.ImageInputStream;
47 import javax.imageio.stream.ImageOutputStream;
49 public class BMPFileHeader {
50 /** Header signature, always 'BM' */
51 private final static short bfType = 0x424d;
53 /** Bitmap file size, in bytes. */
54 protected long bfSize;
56 /** Offset from the beginning of the file to the bitmap data */
57 protected long bfOffBits;
59 /** BITMAPFILEHEADER is 14 bytes */
60 public static final int SIZE = 14;
61 private static final int BITMAPINFOHEADER_SIZE = 40;
63 /**
64 * Creates the header from an input stream, which is not closed.
66 * @throws IOException if an I/O error occured.
67 * @throws BMPException if the header was invalid
69 public BMPFileHeader(ImageInputStream in) throws IOException, BMPException {
70 byte[] data = new byte[SIZE];
72 if (in.read(data) != SIZE)
73 throw new IOException("Couldn't read header.");
74 ByteBuffer buf = ByteBuffer.wrap(data);
76 if(buf.getShort(0) != bfType)
77 throw new BMPException("Not a BMP file.");
79 buf.order(ByteOrder.LITTLE_ENDIAN);
81 // get size (keep unsigned)
82 bfSize = ((long)buf.getInt(2) & (0xFFFFFFFF));
84 // Two reserved shorts are here, and should be zero,
85 // perhaps they should be tested to be zero, but I don't
86 // feel this strictness is necessary.
88 bfOffBits = ((long)buf.getInt(10) & (0xFFFFFFFF));
91 /**
92 * Creates the header from an output stream, which is not closed.
94 * @param out - the image output stream
95 * @param im - the image
96 * @throws IOException if an I/O error occured.
98 public BMPFileHeader(ImageOutputStream out, IIOImage im) throws IOException
100 RenderedImage img = im.getRenderedImage();
101 int w = img.getWidth();
102 int h = img.getHeight();
104 bfOffBits = SIZE + BITMAPINFOHEADER_SIZE;
105 bfSize = ((w * h) * 3) + ((4 - ((w * 3) % 4)) * h) + bfOffBits;
107 write(out);
111 * Writes the header to an output stream, which is not closed or flushed.
113 * @throws IOException if an I/O error occured.
115 public void write(ImageOutputStream out) throws IOException {
116 ByteBuffer buf = ByteBuffer.allocate(SIZE);
117 buf.putShort(0, bfType); // ID
118 buf.putInt(2, (int)(bfSize & (0xFFFFFFFF))); // size
119 buf.putInt(6, 0); // 4 reserved bytes set to zero
120 buf.putInt(7, (int)(bfOffBits & (0xFFFFFFFF))); // size
121 out.write(buf.array());
125 * Sets the file size
127 public void setSize(long size){
128 bfSize = size;
132 * Sets the bitmap offset within the file
134 public void setOffset(long offset){
135 bfOffBits = offset;
139 * Gets the file size
141 public long getSize(){
142 return bfSize;
146 * Gets the bitmap offset within the file
148 public long getOffset(){
149 return bfOffBits;