DecodeRLE4.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* DecodeRLE4.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.io.IOException;
  33. import javax.imageio.stream.ImageInputStream;
  34. import java.awt.image.BufferedImage;
  35. import java.awt.image.IndexColorModel;
  36. import java.awt.image.Raster;
  37. import java.awt.image.WritableRaster;
  38. import java.awt.image.DataBuffer;
  39. import java.awt.image.DataBufferByte;
  40. import java.awt.image.MultiPixelPackedSampleModel;
  41. import java.awt.image.SampleModel;
  42. import java.awt.Dimension;
  43. public class DecodeRLE4 extends BMPDecoder {
  44. public DecodeRLE4(BMPFileHeader fh, BMPInfoHeader ih){
  45. super(fh, ih);
  46. }
  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. public BufferedImage decode(ImageInputStream in) throws IOException, BMPException {
  55. IndexColorModel palette = readPalette(in);
  56. skipToImage(in);
  57. Dimension d = infoHeader.getSize();
  58. int h = (int)d.getHeight();
  59. int w = (int)d.getWidth();
  60. byte[] data = uncompress(w, h, in);
  61. SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  62. w, h, 4);
  63. DataBuffer db = new DataBufferByte(data, w*h, 0);
  64. WritableRaster raster = Raster.createWritableRaster(sm, db, null);
  65. return new BufferedImage(palette, raster, false, null);
  66. }
  67. private byte[] uncompress(int w, int h, ImageInputStream in)
  68. throws BMPException, IOException {
  69. byte[] cmd = new byte[2];
  70. byte[] data = new byte[w*h>>1];
  71. int offIn = 0;
  72. int x=0,y=0;
  73. // width in bytes
  74. w += (w&1);
  75. w = w >> 1;
  76. try {
  77. while(((x>>1) + y*w) < w*h){
  78. if(in.read(cmd) != 2)
  79. throw new IOException("Error reading compressed data.");
  80. if(cmd[0] == ESCAPE){
  81. switch(cmd[1]){
  82. case EOB: // end of bitmap
  83. return data;
  84. case EOL: // end of line
  85. x = 0;
  86. y++;
  87. break;
  88. case DELTA: // delta
  89. if(in.read(cmd) != 2)
  90. throw new IOException("Error reading compressed data.");
  91. int dx = cmd[0] & (0xFF);
  92. int dy = cmd[1] & (0xFF);
  93. x += dx;
  94. y += dy;
  95. break;
  96. default:
  97. // decode a literal run
  98. int length = cmd[1] & (0xFF);
  99. // size of run, which is word aligned.
  100. int bytesize = length;
  101. bytesize += (bytesize & 1);
  102. bytesize >>= 1;
  103. bytesize += (bytesize & 1);
  104. byte[] run = new byte[bytesize];
  105. if(in.read(run) != bytesize)
  106. throw new IOException("Error reading compressed data.");
  107. if((x&1) == 0){
  108. length += (length&1);
  109. length >>= 1;
  110. System.arraycopy(run, 0, data, ((x>>1) + w*(h-y-1)),
  111. length);
  112. } else {
  113. for(int i=0;i<length;i++){
  114. if((i&1) == 0) // copy high to low
  115. data[((x+i)>>1) + w*(h-y-1)]
  116. |= ((run[i>>1]&0xF0) >> 4);
  117. else // copy low to high
  118. data[((x+i)>>1) + w*(h-y-1)]
  119. |= ((run[i>>1]&0x0F) << 4);
  120. }
  121. }
  122. x += cmd[1] & (0xFF);
  123. break;
  124. }
  125. } else {
  126. // decode a byte run
  127. int length = cmd[0] & (0xFF);
  128. if((x&1) == 0){
  129. length += (length&1);
  130. length >>= 1;
  131. for(int i=0;i<length;i++)
  132. data[(h-y-1)*w + i + (x >> 1)] = cmd[1];
  133. } else {
  134. for(int i=0;i<length;i++){
  135. if((i&1) == 0) // copy high to low
  136. data[((x+i)>>1) + w*(h-y-1)]
  137. |= ((cmd[1]&0xF0) >> 4);
  138. else // copy low to high
  139. data[((x+i)>>1) + w*(h-y-1)]
  140. |= ((cmd[1]&0x0F) << 4);
  141. }
  142. }
  143. x += cmd[0] & (0xFF);
  144. }
  145. }
  146. return data;
  147. } catch(ArrayIndexOutOfBoundsException e){
  148. throw new BMPException("Invalid RLE data.");
  149. }
  150. }
  151. }