1 /* 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.awt.image.RenderedImage; 41 import java.io.IOException; 42 import java.nio.ByteBuffer; 43 import java.nio.ByteOrder; 44 45 import javax.imageio.IIOImage; 46 import javax.imageio.stream.ImageInputStream; 47 import javax.imageio.stream.ImageOutputStream; 48 49 public class BMPFileHeader { 50 /** Header signature, always 'BM' */ 51 private final static short bfType = 0x424d; 52 53 /** Bitmap file size, in bytes. */ 54 protected long bfSize; 55 56 /** Offset from the beginning of the file to the bitmap data */ 57 protected long bfOffBits; 58 59 /** BITMAPFILEHEADER is 14 bytes */ 60 public static final int SIZE = 14; 61 private static final int BITMAPINFOHEADER_SIZE = 40; 62 63 /** 64 * Creates the header from an input stream, which is not closed. 65 * 66 * @throws IOException if an I/O error occured. 67 * @throws BMPException if the header was invalid 68 */ BMPFileHeader(ImageInputStream in)69 public BMPFileHeader(ImageInputStream in) throws IOException, BMPException { 70 byte[] data = new byte[SIZE]; 71 72 if (in.read(data) != SIZE) 73 throw new IOException("Couldn't read header."); 74 ByteBuffer buf = ByteBuffer.wrap(data); 75 76 if(buf.getShort(0) != bfType) 77 throw new BMPException("Not a BMP file."); 78 79 buf.order(ByteOrder.LITTLE_ENDIAN); 80 81 // get size (keep unsigned) 82 bfSize = ((long)buf.getInt(2) & (0xFFFFFFFF)); 83 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. 87 88 bfOffBits = ((long)buf.getInt(10) & (0xFFFFFFFF)); 89 } 90 91 /** 92 * Creates the header from an output stream, which is not closed. 93 * 94 * @param out - the image output stream 95 * @param im - the image 96 * @throws IOException if an I/O error occured. 97 */ BMPFileHeader(ImageOutputStream out, IIOImage im)98 public BMPFileHeader(ImageOutputStream out, IIOImage im) throws IOException 99 { 100 RenderedImage img = im.getRenderedImage(); 101 int w = img.getWidth(); 102 int h = img.getHeight(); 103 104 bfOffBits = SIZE + BITMAPINFOHEADER_SIZE; 105 bfSize = ((w * h) * 3) + ((4 - ((w * 3) % 4)) * h) + bfOffBits; 106 107 write(out); 108 } 109 110 /** 111 * Writes the header to an output stream, which is not closed or flushed. 112 * 113 * @throws IOException if an I/O error occured. 114 */ write(ImageOutputStream out)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()); 122 } 123 124 /** 125 * Sets the file size 126 */ setSize(long size)127 public void setSize(long size){ 128 bfSize = size; 129 } 130 131 /** 132 * Sets the bitmap offset within the file 133 */ setOffset(long offset)134 public void setOffset(long offset){ 135 bfOffBits = offset; 136 } 137 138 /** 139 * Gets the file size 140 */ getSize()141 public long getSize(){ 142 return bfSize; 143 } 144 145 /** 146 * Gets the bitmap offset within the file 147 */ getOffset()148 public long getOffset(){ 149 return bfOffBits; 150 } 151 } 152