BMPInfoHeader.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* BMPInfoHeader.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.imageio.bmp;
  32. import java.awt.Dimension;
  33. import java.awt.image.ColorModel;
  34. import java.awt.image.RenderedImage;
  35. import java.io.IOException;
  36. import java.nio.ByteBuffer;
  37. import java.nio.ByteOrder;
  38. import javax.imageio.IIOImage;
  39. import javax.imageio.ImageWriteParam;
  40. import javax.imageio.stream.ImageInputStream;
  41. import javax.imageio.stream.ImageOutputStream;
  42. public class BMPInfoHeader
  43. {
  44. /** Size of the bitmap info header */
  45. protected int biSize;
  46. /** Pixel width of the bitmap */
  47. protected int biWidth;
  48. /** Pixel height of the bitmap */
  49. protected int biHeight;
  50. /** Number of bitplanes = 1 */
  51. protected short biPlanes;
  52. /** Number of bpp = 1,4,8,24 */
  53. protected short biBitCount;
  54. /** Compression type, RGB8, RLE8, RLE4, BITFIELDS */
  55. protected int biCompression;
  56. /** Byte size of the uncompressed bitmap, can be 0. */
  57. protected int biSizeImage;
  58. /** X resolution, dots per meter */
  59. protected int biXPelsPerMeter;
  60. /** Y resolution, dots per meter */
  61. protected int biYPelsPerMeter;
  62. /** Number of colors used (palette only, can be 0 for all) */
  63. protected int biClrUsed;
  64. /** Number of 'important' colors, 0 for all */
  65. protected int biClrImportant;
  66. /** BITMAPINFOHEADER is 40 bytes */
  67. public static final int SIZE = 40;
  68. /**
  69. * Compression types
  70. */
  71. public static final int BI_RGB = 0;
  72. public static final int BI_RLE8 = 1;
  73. public static final int BI_RLE4 = 2;
  74. public static final int BI_BITFIELDS = 3;
  75. /**
  76. * Creates the header from an input stream, which is not closed.
  77. *
  78. * @param in - the image input stream
  79. * @throws IOException if an I/O error occured.
  80. * @throws BMPException if the header was invalid
  81. */
  82. public BMPInfoHeader(ImageInputStream in) throws IOException, BMPException
  83. {
  84. byte[] data = new byte[SIZE];
  85. if (in.read(data) != SIZE)
  86. throw new IOException("Couldn't read header.");
  87. ByteBuffer buf = ByteBuffer.wrap(data);
  88. buf.order(ByteOrder.LITTLE_ENDIAN);
  89. int n;
  90. if ((n = buf.getInt()) != SIZE)
  91. throw new BMPException("Invalid BITMAPINFOHEADER size: " + n);
  92. biWidth = buf.getInt();
  93. biHeight = buf.getInt();
  94. biPlanes = buf.getShort();
  95. setBitCount(buf.getShort());
  96. setCompression(buf.getInt());
  97. biSizeImage = buf.getInt();
  98. biXPelsPerMeter = buf.getInt();
  99. biYPelsPerMeter = buf.getInt();
  100. biClrUsed = buf.getInt();
  101. biClrImportant = buf.getInt();
  102. }
  103. /**
  104. * Creates the info header from an output stream, which is not closed.
  105. *
  106. * @param out - the image output stream
  107. * @param im - the image
  108. * @param param - the image write param.
  109. * @throws IOException if an I/O error occured.
  110. */
  111. public BMPInfoHeader(ImageOutputStream out, IIOImage im, ImageWriteParam param) throws IOException
  112. {
  113. RenderedImage img = im.getRenderedImage();
  114. ColorModel cMod = img.getColorModel();
  115. biSize = SIZE;
  116. biWidth = img.getWidth();
  117. biHeight = img.getHeight();
  118. biPlanes = 1;
  119. if (param != null && param.canWriteCompressed())
  120. {
  121. String compType = param.getCompressionType();
  122. if (compType.equals("BI_RLE8"))
  123. {
  124. biCompression = BI_RLE8;
  125. biBitCount = 8;
  126. }
  127. else if (compType.equals("BI_RLE4"))
  128. {
  129. biCompression = BI_RLE4;
  130. biBitCount = 4;
  131. }
  132. else
  133. {
  134. biCompression = BI_RGB;
  135. biBitCount = (short) cMod.getPixelSize();
  136. }
  137. }
  138. else
  139. {
  140. biBitCount = (short) cMod.getPixelSize();
  141. biCompression = BI_RGB;
  142. }
  143. biXPelsPerMeter = 0x0;
  144. biYPelsPerMeter = 0x0;
  145. biClrUsed = 0;
  146. biClrImportant = 0;
  147. biSizeImage = ((biWidth * biHeight) * 3)
  148. + ((4 - ((biWidth * 3) % 4)) * biHeight);
  149. out.write(intToDWord(biSize));
  150. out.write(intToDWord(biWidth));
  151. out.write(intToDWord(biHeight));
  152. out.write(intToWord(biPlanes));
  153. out.write(intToWord(biBitCount));
  154. out.write(intToDWord(biCompression));
  155. out.write(intToDWord(biSizeImage));
  156. out.write(intToDWord(biXPelsPerMeter));
  157. out.write(intToDWord(biYPelsPerMeter));
  158. out.write(intToDWord(biClrUsed));
  159. out.write(intToDWord(biClrImportant));
  160. }
  161. /**
  162. * Converts an int to a word, where the return value is stored in a
  163. * 2-byte array.
  164. *
  165. * @param val - the value to convert
  166. * @return the array
  167. */
  168. private byte[] intToWord(int val)
  169. {
  170. byte b[] = new byte[2];
  171. b[0] = (byte) (val & 0x00FF);
  172. b[1] = (byte) ((val >> 8) & 0x00FF);
  173. return b;
  174. }
  175. /**
  176. * Converts an int to a double word, where the return value is
  177. * stored in a 4-byte array.
  178. *
  179. * @param val - the value to convert
  180. * @return the array
  181. */
  182. private byte[] intToDWord(int val)
  183. {
  184. byte b[] = new byte[4];
  185. b[0] = (byte) (val & 0x00FF);
  186. b[1] = (byte) ((val >> 8) & 0x000000FF);
  187. b[2] = (byte) ((val >> 16) & 0x000000FF);
  188. b[3] = (byte) ((val >> 24) & 0x000000FF);
  189. return b;
  190. }
  191. public void setBitCount(short bitcount) throws BMPException
  192. {
  193. switch (bitcount)
  194. {
  195. case 1:
  196. case 4:
  197. case 8:
  198. case 16:
  199. case 24:
  200. case 32:
  201. biBitCount = bitcount;
  202. break;
  203. default:
  204. throw new BMPException("Invalid number of bits per pixel: " + bitcount);
  205. }
  206. }
  207. public short getBitCount()
  208. {
  209. return biBitCount;
  210. }
  211. public void setCompression(int compression) throws BMPException
  212. {
  213. switch (compression)
  214. {
  215. case BI_RLE8:
  216. if (getBitCount() != 8)
  217. throw new BMPException("Invalid number of bits per pixel.");
  218. biCompression = compression;
  219. break;
  220. case BI_RLE4:
  221. if (getBitCount() != 4)
  222. throw new BMPException("Invalid number of bits per pixel.");
  223. biCompression = compression;
  224. break;
  225. case BI_RGB:
  226. case BI_BITFIELDS:
  227. biCompression = compression;
  228. break;
  229. default:
  230. throw new BMPException("Unknown bitmap compression type.");
  231. }
  232. }
  233. public int getNumberOfPaletteEntries()
  234. {
  235. if (biClrUsed == 0)
  236. switch (biBitCount)
  237. {
  238. case 1:
  239. return 2;
  240. case 4:
  241. return 16;
  242. case 8:
  243. return 256;
  244. default: // should not happen
  245. return 0;
  246. }
  247. return biClrUsed;
  248. }
  249. public int getCompression()
  250. {
  251. return biCompression;
  252. }
  253. public Dimension getSize()
  254. {
  255. return new Dimension(biWidth, biHeight);
  256. }
  257. public int getWidth()
  258. {
  259. return biWidth;
  260. }
  261. public int getHeight()
  262. {
  263. return biHeight;
  264. }
  265. public void setSize(Dimension d)
  266. {
  267. biWidth = (int) d.getWidth();
  268. biHeight = (int) d.getHeight();
  269. }
  270. }