/imageio/imageio-ico/src/main/java/com/twelvemonkeys/imageio/plugins/ico/DIBHeader.java

https://github.com/conceptboard/TwelveMonkeys · Java · 197 lines · 105 code · 36 blank · 56 comment · 5 complexity · 5ec9d7cc4d85bebb17edbbc79a7de023 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009, Harald Kuhr
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name "TwelveMonkeys" nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package com.twelvemonkeys.imageio.plugins.ico;
  29. import javax.imageio.IIOException;
  30. import java.io.DataInput;
  31. import java.io.IOException;
  32. /**
  33. * Represents the DIB (Device Independent Bitmap) Information header structure.
  34. *
  35. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  36. * @author last modified by $Author: haraldk$
  37. * @version $Id: DIBHeader.java,v 1.0 May 5, 2009 10:45:31 AM haraldk Exp$
  38. * @see <a href="http://en.wikipedia.org/wiki/BMP_file_format">BMP file format (Wikipedia)</a>
  39. */
  40. abstract class DIBHeader {
  41. protected int size;
  42. protected int width;
  43. // NOTE: If a bitmask is present, this value includes the height of the mask
  44. // (so often header.height = entry.height * 2)
  45. protected int height;
  46. protected int planes;
  47. protected int bitCount;
  48. /**
  49. * 0 = BI_RGB: No compression
  50. * 1 = BI_RLE8: 8 bit RLE Compression (8 bit only)
  51. * 2 = BI_RLE4: 4 bit RLE Compression (4 bit only)
  52. * 3 = BI_BITFIELDS: No compression (16 & 32 bit only)
  53. */
  54. protected int compression;
  55. // May be 0 if not known
  56. protected int imageSize;
  57. protected int xPixelsPerMeter;
  58. protected int yPixelsPerMeter;
  59. protected int colorsUsed;
  60. // 0 means all colors are important
  61. protected int colorsImportant;
  62. protected DIBHeader() {
  63. }
  64. public static DIBHeader read(final DataInput pStream) throws IOException {
  65. int size = pStream.readInt();
  66. // ICO always uses the Microsoft Windows V3 DIB header, which is 40 bytes
  67. DIBHeader header = createHeader(size);
  68. header.read(size, pStream);
  69. return header;
  70. }
  71. private static DIBHeader createHeader(final int pSize) throws IOException {
  72. switch (pSize) {
  73. case DIB.OS2_V1_HEADER_SIZE:
  74. case DIB.OS2_V2_HEADER_SIZE:
  75. throw new IIOException(String.format("OS/2 Bitmap Information Header (size: %s) not supported", pSize));
  76. case DIB.WINDOWS_V3_HEADER_SIZE:
  77. return new WindowsV3DIBHeader();
  78. case DIB.WINDOWS_V4_HEADER_SIZE:
  79. case DIB.WINDOWS_V5_HEADER_SIZE:
  80. throw new IIOException(String.format("Windows Bitmap Information Header (size: %s) not supported", pSize));
  81. default:
  82. throw new IIOException(String.format("Unknown Bitmap Information Header (size: %s)", pSize));
  83. }
  84. }
  85. protected abstract void read(int pSize, DataInput pStream) throws IOException;
  86. public final int getSize() {
  87. return size;
  88. }
  89. public final int getWidth() {
  90. return width;
  91. }
  92. public final int getHeight() {
  93. return height;
  94. }
  95. public final int getPlanes() {
  96. return planes;
  97. }
  98. public final int getBitCount() {
  99. return bitCount;
  100. }
  101. public int getCompression() {
  102. return compression;
  103. }
  104. public int getImageSize() {
  105. return imageSize;
  106. }
  107. public int getXPixelsPerMeter() {
  108. return xPixelsPerMeter;
  109. }
  110. public int getYPixelsPerMeter() {
  111. return yPixelsPerMeter;
  112. }
  113. public int getColorsUsed() {
  114. return colorsUsed;
  115. }
  116. public int getColorsImportant() {
  117. return colorsImportant;
  118. }
  119. public String toString() {
  120. return String.format(
  121. "%s: size: %d bytes, " +
  122. "width: %d, height: %d, planes: %d, bit count: %d, compression: %d, " +
  123. "image size: %d%s, " +
  124. "X pixels per m: %d, Y pixels per m: %d, " +
  125. "colors used: %d, colors important: %d%s",
  126. getClass().getSimpleName(),
  127. getSize(), getWidth(), getHeight(), getPlanes(), getBitCount(), getCompression(),
  128. getImageSize(), (getImageSize() == 0 ? " (unknown)" : ""),
  129. getXPixelsPerMeter(), getYPixelsPerMeter(),
  130. getColorsUsed(), getColorsImportant(), (getColorsImportant() == 0 ? " (all)" : "")
  131. );
  132. }
  133. /**
  134. * Represents the DIB (Device Independent Bitmap) Windows V3 Bitmap Information header structure.
  135. * This is the common format for persistent DIB structures, even if Windows
  136. * may use the later versions at run-time.
  137. * <p/>
  138. *
  139. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  140. * @version $Id: DIBHeader.java,v 1.0 25.feb.2006 00:29:44 haku Exp$
  141. * @see <a href="http://en.wikipedia.org/wiki/BMP_file_format">BMP file format (Wikipedia)</a>
  142. */
  143. static final class WindowsV3DIBHeader extends DIBHeader {
  144. protected void read(final int pSize, final DataInput pStream) throws IOException {
  145. if (pSize != DIB.WINDOWS_V3_HEADER_SIZE) {
  146. throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.WINDOWS_V3_HEADER_SIZE));
  147. }
  148. size = pSize;
  149. width = pStream.readInt();
  150. height = pStream.readInt();
  151. planes = pStream.readUnsignedShort();
  152. bitCount = pStream.readUnsignedShort();
  153. compression = pStream.readInt();
  154. imageSize = pStream.readInt();
  155. xPixelsPerMeter = pStream.readInt();
  156. yPixelsPerMeter = pStream.readInt();
  157. colorsUsed = pStream.readInt();
  158. colorsImportant = pStream.readInt();
  159. }
  160. }
  161. }