Source for gnu.javax.imageio.bmp.DecodeRLE8

   1: /* DecodeRLE8.java --
   2:    Copyright (C)  2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.imageio.bmp;
  39: 
  40: import java.io.IOException;
  41: import javax.imageio.stream.ImageInputStream;
  42: import java.awt.image.BufferedImage;
  43: import java.awt.image.IndexColorModel;
  44: import java.awt.image.Raster;
  45: import java.awt.image.WritableRaster;
  46: import java.awt.image.DataBuffer;
  47: import java.awt.image.DataBufferByte;
  48: import java.awt.image.SinglePixelPackedSampleModel;
  49: import java.awt.image.SampleModel;
  50: import java.awt.Dimension;
  51: 
  52: public class DecodeRLE8 extends BMPDecoder {
  53: 
  54:     public DecodeRLE8(BMPFileHeader fh, BMPInfoHeader ih){
  55:         super(fh, ih);
  56:     }
  57: 
  58:     /**
  59:      * RLE control codes
  60:      */
  61:     private static final byte ESCAPE = (byte)0;
  62:     private static final byte EOL = (byte)0; // end of line
  63:     private static final byte EOB = (byte)1; // end of bitmap
  64:     private static final byte DELTA = (byte)2; // delta
  65: 
  66:     public BufferedImage decode(ImageInputStream in) throws IOException, BMPException {
  67:         IndexColorModel palette = readPalette(in);
  68:         skipToImage(in);
  69: 
  70:         Dimension d = infoHeader.getSize();
  71:         int h = (int)d.getHeight();
  72:         int w = (int)d.getWidth();
  73: 
  74:         byte[] data = uncompress(w, h, in);
  75:         SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  76:                                                           w, h,
  77:                                                           new int[] {0xFF});
  78:         DataBuffer db = new DataBufferByte(data, w*h, 0);
  79:         WritableRaster raster = Raster.createWritableRaster(sm, db, null);
  80: 
  81:         return new BufferedImage(palette, raster, false, null);
  82:     }
  83: 
  84:     private byte[] uncompress(int w, int h, ImageInputStream in)
  85:         throws BMPException, IOException {
  86:         byte[] cmd = new byte[2];
  87:         byte[] data = new byte[w*h];
  88:         int offIn = 0;
  89:         int x=0,y=0;
  90: 
  91:         try {
  92:             while((x + y*w) < w*h){
  93:                 if(in.read(cmd) != 2)
  94:                     throw new IOException("Error reading compressed data.");
  95: 
  96:                 if(cmd[0] == ESCAPE){
  97:                     switch(cmd[1]){
  98:                     case EOB: // end of bitmap
  99:                         return data;
 100:                     case EOL: // end of line
 101:                         x = 0;
 102:                         y++;
 103:                         break;
 104:                     case DELTA: // delta
 105:                         if(in.read(cmd) != 2)
 106:                             throw new IOException("Error reading compressed data.");
 107:                         int dx = cmd[0] & (0xFF);
 108:                         int dy = cmd[1] & (0xFF);
 109:                         x += dx;
 110:                         y += dy;
 111:                         break;
 112: 
 113:                     default:
 114:                         // decode a literal run
 115:                         int length = cmd[1] & (0xFF);
 116:                         int copylength = length;
 117: 
 118:                         // absolute mode must be word-aligned
 119:                         length += (length & 1);
 120: 
 121:                         byte[] run = new byte[length];
 122:                         if(in.read(run) != length)
 123:                             throw new IOException("Error reading compressed data.");
 124: 
 125:                         System.arraycopy(run, 0, data, (x+w*(h-y-1)),
 126:                                          copylength);
 127:                         x += copylength;
 128:                         break;
 129:                     }
 130:                 } else {
 131:                     // decode a byte run
 132:                     int length = cmd[0] & (0xFF);
 133:                     for(int i=0;i<length;i++)
 134:                         data[(h-y-1)*w + x++] = cmd[1];
 135:                 }
 136:             }
 137:             return data;
 138:         } catch(ArrayIndexOutOfBoundsException e){
 139:             throw new BMPException("Invalid RLE data.");
 140:         }
 141:     }
 142: }