OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [imageio/] [bmp/] [EncodeRLE8.java] - Blame information for rev 769

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.