EncodeRLE8.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* EncodeRGB32.java --
  2. Copyright (C) 2006 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.BufferedImage;
  33. import java.awt.image.PixelGrabber;
  34. import java.io.IOException;
  35. import java.nio.BufferUnderflowException;
  36. import java.nio.ByteBuffer;
  37. import javax.imageio.IIOImage;
  38. import javax.imageio.ImageWriteParam;
  39. import javax.imageio.metadata.IIOMetadata;
  40. import javax.imageio.stream.ImageOutputStream;
  41. public class EncodeRLE8
  42. extends BMPEncoder
  43. {
  44. protected BMPInfoHeader infoHeader;
  45. protected BMPFileHeader fileHeader;
  46. protected long offset;
  47. /**
  48. * RLE control codes
  49. */
  50. private static final byte ESCAPE = (byte)0;
  51. private static final byte EOL = (byte)0; // end of line
  52. private static final byte EOB = (byte)1; // end of bitmap
  53. private static final byte DELTA = (byte)2; // delta
  54. /**
  55. * Constructs an instance of this class.
  56. *
  57. * @param fh - the file header to use.
  58. * @param ih - the info header to use.
  59. */
  60. public EncodeRLE8(BMPFileHeader fh, BMPInfoHeader ih)
  61. {
  62. super();
  63. fileHeader = fh;
  64. infoHeader = ih;
  65. offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE;
  66. }
  67. /**
  68. * The image encoder.
  69. *
  70. * @param o - the image output stream
  71. * @param streamMetadata - metadata associated with this stream, or
  72. * null
  73. * @param image - an IIOImage containing image data.
  74. * @param param - image writing parameters, or null
  75. * @exception IOException if a write error occurs
  76. */
  77. public void encode(ImageOutputStream o, IIOMetadata streamMetadata,
  78. IIOImage image, ImageWriteParam param) throws IOException
  79. {
  80. int size;
  81. int value;
  82. int j;
  83. int rowCount;
  84. int rowIndex;
  85. int lastRowIndex;
  86. int[] bitmap;
  87. size = (infoHeader.biWidth * infoHeader.biHeight) - 1;
  88. rowCount = 1;
  89. rowIndex = size - infoHeader.biWidth;
  90. lastRowIndex = rowIndex;
  91. ByteBuffer buf = ByteBuffer.allocate(size);
  92. try
  93. {
  94. bitmap = new int[infoHeader.biWidth * infoHeader.biHeight];
  95. PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(),
  96. 0, 0, infoHeader.biWidth,
  97. infoHeader.biHeight, bitmap, 0,
  98. infoHeader.biWidth);
  99. pg.grabPixels();
  100. for (j = 0; j < size; j++)
  101. {
  102. value = bitmap[rowIndex];
  103. buf.put((byte) (value & 0xFF));
  104. if (rowCount == infoHeader.biWidth)
  105. {
  106. rowCount = 1;
  107. rowIndex = lastRowIndex - infoHeader.biWidth;
  108. lastRowIndex = rowIndex;
  109. }
  110. else
  111. rowCount++;
  112. rowIndex++;
  113. }
  114. buf.flip();
  115. o.write(uncompress(infoHeader.biWidth, infoHeader.biHeight, buf));
  116. }
  117. catch (Exception wb)
  118. {
  119. wb.printStackTrace();
  120. }
  121. }
  122. /**
  123. * Uncompresses the image stored in the buffer.
  124. *
  125. * @param w - the width of the image
  126. * @param h - the height of the image
  127. * @param buf - the ByteBuffer containing the pixel values.
  128. * @return byte array containing the uncompressed image
  129. * @throws IOException if an error is encountered while reading
  130. * buffer.
  131. */
  132. private byte[] uncompress(int w, int h, ByteBuffer buf) throws IOException
  133. {
  134. byte[] cmd = new byte[2];
  135. byte[] data = new byte[w * h];
  136. int offIn = 0;
  137. int x = 0, y = 0;
  138. try
  139. {
  140. while ((x + y * w) < w * h)
  141. {
  142. try
  143. {
  144. buf.get(cmd);
  145. }
  146. catch (BufferUnderflowException e)
  147. {
  148. throw new IOException("Error reading compressed data.");
  149. }
  150. if (cmd[0] == ESCAPE)
  151. {
  152. switch (cmd[1])
  153. {
  154. case EOB:
  155. return data;
  156. case EOL:
  157. x = 0;
  158. y++;
  159. break;
  160. case DELTA:
  161. try
  162. {
  163. buf.get(cmd);
  164. }
  165. catch (BufferUnderflowException e)
  166. {
  167. throw new IOException("Error reading compressed data.");
  168. }
  169. int dx = cmd[0] & (0xFF);
  170. int dy = cmd[1] & (0xFF);
  171. x += dx;
  172. y += dy;
  173. break;
  174. default:
  175. int length = cmd[1] & (0xFF);
  176. int copylength = length;
  177. length += (length & 1);
  178. byte[] run = new byte[length];
  179. try
  180. {
  181. buf.get(run);
  182. }
  183. catch (BufferUnderflowException e)
  184. {
  185. throw new IOException("Error reading compressed data.");
  186. }
  187. System.arraycopy(run, 0, data, (x + w * (h - y - 1)),
  188. copylength);
  189. x += copylength;
  190. break;
  191. }
  192. }
  193. else
  194. {
  195. int length = cmd[0] & (0xFF);
  196. for (int i = 0; i < length; i++)
  197. data[(h - y - 1) * w + x++] = cmd[1];
  198. }
  199. }
  200. return data;
  201. }
  202. catch (ArrayIndexOutOfBoundsException e)
  203. {
  204. throw new BMPException("Invalid RLE data.");
  205. }
  206. }
  207. }