File: BitmapReader.java

package info (click to toggle)
libpixie-java 1%3A1.1.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,076 kB
  • sloc: java: 10,175; xml: 1,579; makefile: 13
file content (361 lines) | stat: -rw-r--r-- 10,972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2000 - 2009 Pentaho Corporation, Object Refinery Limited and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.pixie.wmf.bitmap;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.pentaho.reporting.libraries.base.util.IOUtils;


public class BitmapReader
{
  private BitmapReader()
  {
  }

  // build an int from a byte array - convert little to big endian
  public static int constructInt(final byte[] in, final int offset)
  {
    int ret = ((int) in[offset + 3] & 0xff);
    ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
    ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
    ret = (ret << 8) | ((int) in[offset] & 0xff);
    return (ret);
  }

  // build an int from a byte array - convert little to big endian
  // set high order bytes to 0xfff
  public static int constructInt3(final byte[] in, final int offset)
  {
    int ret = 0xff;
    ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
    ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
    ret = (ret << 8) | ((int) in[offset] & 0xff);
    return (ret);
  }

  // build an int from a byte array - convert little to big endian
  public static long constructLong(final byte[] in, final int offset)
  {
    long ret = ((long) in[offset + 7] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);
    ret |= (ret << 8) | ((long) in[offset] & 0xff);
    return (ret);
  }

  // build an double from a byte array - convert little to big endian
  public static double constructDouble(final byte[] in, final int offset)
  {
    final long ret = constructLong(in, offset);
    return (Double.longBitsToDouble(ret));
  }

  // build an short from a byte array - convert little to big endian
  public static short constructShort(final byte[] in, final int offset)
  {
    short ret = (short) ((short) in[offset + 1] & 0xff);
    ret = (short) ((ret << 8) | (short) ((short) in[offset] & 0xff));
    return (ret);
  }

  //
  // internal class representing a bitmap header structure
  // with code to read it from a file
  protected static class BitmapHeader
  {
    public int nsize;
    public int nbisize;
    public int nwidth;
    public int nheight;
    public int nplanes;
    public int nbitcount;
    public int ncompression;
    public int nsizeimage;
    public int nxpm;
    public int nypm;
    public int nclrused;
    public int nclrimp;

    protected BitmapHeader()
    {
    }

    // read in the bitmap header
    public void read(final InputStream fs)
        throws IOException
    {
      final int bflen = 14;  // 14 byte BITMAPFILEHEADER
      final byte[] bf = new byte[bflen];
      IOUtils.getInstance().readFully(fs, bf, 0, bflen);

      final int bilen = 40; // 40-byte BITMAPINFOHEADER
      final byte[] bi = new byte[bilen];
      IOUtils.getInstance().readFully(fs, bi, 0, bilen);

      // Interperet data.
      nsize = constructInt(bf, 2);
      //  System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
      //  System.out.println("Size of file is :"+nsize);

      nbisize = constructInt(bi, 2);
      //   System.out.println("Size of bitmapinfoheader is :"+nbisize);

      nwidth = constructInt(bi, 4);
      //   System.out.println("Width is :"+nwidth);

      nheight = constructInt(bi, 8);
      //  System.out.println("Height is :"+nheight);

      nplanes = constructShort(bi, 12); //(((int)bi[13]&0xff)<<8) | (int)bi[12]&0xff;
      //  System.out.println("Planes is :"+nplanes);

      nbitcount = constructShort(bi, 14); //(((int)bi[15]&0xff)<<8) | (int)bi[14]&0xff;
      //  System.out.println("BitCount is :"+nbitcount);

      // Look for non-zero values to indicate compression
      ncompression = constructInt(bi, 16);
      //  System.out.println("Compression is :"+ncompression);

      nsizeimage = constructInt(bi, 20);
      //  System.out.println("SizeImage is :"+nsizeimage);

      nxpm = constructInt(bi, 24);
      // System.out.println("X-Pixels per meter is :"+nxpm);

      nypm = constructInt(bi, 28);
      // System.out.println("Y-Pixels per meter is :"+nypm);

      nclrused = constructInt(bi, 32);
      //  System.out.println("Colors used are :"+nclrused);

      nclrimp = constructInt(bi, 36);
      //  System.out.println("Colors important are :"+nclrimp);
    }
  }

  public static Image read(final FileInputStream fs)
  {
    try
    {
      final BitmapHeader bh = new BitmapHeader();
      bh.read(fs);

      if (bh.nbitcount == 24)
      {
        return (readMap24(fs, bh));
      }

      if (bh.nbitcount == 32)
      {
        return (readMap32(fs, bh));
      }

      if (bh.nbitcount == 8)
      {
        return (readMap8(fs, bh));
      }

      fs.close();
    }
    catch (IOException e)
    {
      // System.out.println("Caught exception in loadbitmap!");
    }
    return (null);
  }

  /**
   * readMap24 internal routine to read the bytes in a 24 bit bitmap
   *
   * @param fs file stream
   * @param bh header struct
   * @return Image Object, be sure to check for (Image)null !!!!
   */
  protected static Image readMap32(final FileInputStream fs, final BitmapHeader bh)
      throws IOException
  {
    // No Palatte data for 24-bit format but scan lines are
    // padded out to even 4-byte boundaries.
//    final int xwidth = bh.nsizeimage / bh.nheight;
    final int[] ndata = new int[bh.nheight * bh.nwidth];
    final byte[] brgb = new byte[bh.nwidth * 4 * bh.nheight];
    IOUtils.getInstance().readFully(fs, brgb, 0, bh.nwidth * 4 * bh.nheight);
    int nindex = 0;

    for (int j = 0; j < bh.nheight; j++)
    {
      for (int i = 0; i < bh.nwidth; i++)
      {
        ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(brgb, nindex);
        nindex += 4;
      }
    }

    final Image image = Toolkit.getDefaultToolkit().createImage
        (new MemoryImageSource(bh.nwidth, bh.nheight,
            ndata, 0, bh.nwidth));
    fs.close();
    return (image);
  }


  /**
   * readMap24 internal routine to read the bytes in a 24 bit bitmap
   *
   * @param fs file stream
   * @param bh header struct
   * @return Image Object, be sure to check for (Image)null !!!!
   */
  protected static Image readMap24(final FileInputStream fs, final BitmapHeader bh)
      throws IOException
  {

    // No Palatte data for 24-bit format but scan lines are
    // padded out to even 4-byte boundaries.
    final int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;
    final int[] ndata = new int[bh.nheight * bh.nwidth];
    final byte[] brgb = new byte[(bh.nwidth + npad) * 3 * bh.nheight];
    IOUtils.getInstance().readFully(fs, brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);

    int nindex = 0;
    for (int j = 0; j < bh.nheight; j++)
    {
      for (int i = 0; i < bh.nwidth; i++)
      {
        ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(brgb, nindex);
        nindex += 3;
      }

      nindex += npad;
    }

    final Image image = Toolkit.getDefaultToolkit().createImage
        (new MemoryImageSource(bh.nwidth, bh.nheight,
            ndata, 0, bh.nwidth));

    fs.close();
    return (image);
  }

  /**
   * readMap8 internal routine to read the bytes in a 8 bit bitmap
   *
   * @param fs file stream
   * @param bh header struct
   * @return Image Object, be sure to check for (Image)null !!!!
   */
  protected static Image readMap8(final FileInputStream fs, final BitmapHeader bh)
      throws IOException
  {

    // Have to determine the number of colors, the clrsused
    // parameter is dominant if it is greater than zero.  If
    // zero, calculate colors based on bitsperpixel.
    final int nNumColors;
    if (bh.nclrused > 0)
    {
      nNumColors = bh.nclrused;
    }
    else
    {
      nNumColors = (1 & 0xff) << bh.nbitcount;
    }

    //     System.out.println("The number of Colors is"+nNumColors);
    // Some bitmaps do not have the sizeimage field calculated
    // Ferret out these cases and fix 'em.
    if (bh.nsizeimage == 0)
    {
      bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);
      bh.nsizeimage *= bh.nheight;
//          System.out.println("nsizeimage (backup) is"+nsizeimage);
    }

    // Read the palatte colors.
    final int[] npalette = new int[nNumColors];
    final byte[] bpalette = new byte[nNumColors * 4];
    IOUtils.getInstance().readFully(fs, bpalette, 0, nNumColors * 4);
    int nindex8 = 0;

    for (int n = 0; n < nNumColors; n++)
    {
      npalette[n] = constructInt3(bpalette, nindex8);
      nindex8 += 4;
    }

    // Read the image data (actually indices into the palette)
    // Scan lines are still padded out to even 4-byte
    // boundaries.

    final int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;
    //    System.out.println("nPad is:"+npad8);

    final int[] ndata8 = new int[bh.nwidth * bh.nheight];
    final byte[] bdata = new byte[(bh.nwidth + npad8) * bh.nheight];
    IOUtils.getInstance().readFully(fs, bdata, 0, (bh.nwidth + npad8) * bh.nheight);
    nindex8 = 0;

    for (int j8 = 0; j8 < bh.nheight; j8++)
    {
      for (int i8 = 0; i8 < bh.nwidth; i8++)
      {
        ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =
            npalette[((int) bdata[nindex8] & 0xff)];
        nindex8++;
      }

      nindex8 += npad8;
    }

    final Image image = Toolkit.getDefaultToolkit().createImage
        (new MemoryImageSource(bh.nwidth, bh.nheight,
            ndata8, 0, bh.nwidth));
    return (image);
  }


  /**
   * load method - see read for details
   *
   * @param sdir full path name
   * @return Image Object, be sure to check for (Image)null !!!!
   */
  public static Image load(final String sdir)
  {
    try
    {
      final FileInputStream fs = new FileInputStream(sdir);
      return (read(fs));
    }
    catch (IOException ex)
    {
      return (null);
    }
  }
}