BMPFileHeader.java 4.8 KB

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