~tom-gall/libjpeg-turbo/upstream-1.2

« back to all changes in this revision

Viewing changes to java/org/libjpegturbo/turbojpeg/TJCompressor.java

  • Committer: Tom Gall
  • Date: 2011-08-15 23:41:58 UTC
  • Revision ID: tom.gall@linaro.org-20110815234158-yqcft1w9j8w3nnf6
merge in 1.1.90 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C)2011 D. R. Commander.  All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * - Redistributions of source code must retain the above copyright notice,
 
8
 *   this list of conditions and the following disclaimer.
 
9
 * - Redistributions in binary form must reproduce the above copyright notice,
 
10
 *   this list of conditions and the following disclaimer in the documentation
 
11
 *   and/or other materials provided with the distribution.
 
12
 * - Neither the name of the libjpeg-turbo Project nor the names of its
 
13
 *   contributors may be used to endorse or promote products derived from this
 
14
 *   software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
 
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
 * POSSIBILITY OF SUCH DAMAGE.
 
27
 */
 
28
 
 
29
package org.libjpegturbo.turbojpeg;
 
30
 
 
31
import java.awt.image.*;
 
32
import java.nio.*;
 
33
 
 
34
/**
 
35
 * TurboJPEG compressor
 
36
 */
 
37
public class TJCompressor {
 
38
 
 
39
  private final static String NO_ASSOC_ERROR =
 
40
    "No source image is associated with this instance";
 
41
 
 
42
  /**
 
43
   * Create a TurboJPEG compressor instance.
 
44
   */
 
45
  public TJCompressor() throws Exception {
 
46
    init();
 
47
  }
 
48
 
 
49
  /**
 
50
   * Create a TurboJPEG compressor instance and associate the uncompressed
 
51
   * source image stored in <code>srcImage</code> with the newly-created
 
52
   * instance.
 
53
   *
 
54
   * @param srcImage see {@link #setSourceImage} for description
 
55
   *
 
56
   * @param width see {@link #setSourceImage} for description
 
57
   *
 
58
   * @param pitch see {@link #setSourceImage} for description
 
59
   *
 
60
   * @param height see {@link #setSourceImage} for description
 
61
   *
 
62
   * @param pixelFormat see {@link #setSourceImage} for description
 
63
   */
 
64
  public TJCompressor(byte[] srcImage, int width, int pitch, int height,
 
65
    int pixelFormat) throws Exception {
 
66
    setSourceImage(srcImage, width, pitch, height, pixelFormat);
 
67
  }
 
68
 
 
69
  /**
 
70
   * Associate an uncompressed source image with this compressor instance.
 
71
   *
 
72
   * @param srcImage image buffer containing RGB or grayscale pixels to be
 
73
   * compressed
 
74
   *
 
75
   * @param width width (in pixels) of the source image
 
76
   *
 
77
   * @param pitch bytes per line of the source image.  Normally, this should be
 
78
   * <code>width * TJ.pixelSize(pixelFormat)</code> if the source image is
 
79
   * unpadded, but you can use this parameter to, for instance, specify that
 
80
   * the scanlines in the source image are padded to 4-byte boundaries, as is
 
81
   * the case for Windows bitmaps.  You can also be clever and use this
 
82
   * parameter to skip lines, etc.  Setting this parameter to 0 is the
 
83
   * equivalent of setting it to <code>width *
 
84
   * TJ.pixelSize(pixelFormat)</code>.
 
85
   *
 
86
   * @param height height (in pixels) of the source image
 
87
   *
 
88
   * @param pixelFormat pixel format of the source image (one of
 
89
   * {@link TJ TJ.PF_*})
 
90
   */
 
91
  public void setSourceImage(byte[] srcImage, int width, int pitch,
 
92
    int height, int pixelFormat) throws Exception {
 
93
    if(handle == 0) init();
 
94
    if(srcImage == null || width < 1 || height < 1 || pitch < 0
 
95
      || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
 
96
      throw new Exception("Invalid argument in setSourceImage()");
 
97
    srcBuf = srcImage;
 
98
    srcWidth = width;
 
99
    if(pitch == 0) srcPitch = width * TJ.getPixelSize(pixelFormat);
 
100
    else srcPitch = pitch;
 
101
    srcHeight = height;
 
102
    srcPixelFormat = pixelFormat;
 
103
  }
 
104
 
 
105
  /**
 
106
   * Set the level of chrominance subsampling for subsequent compress/encode
 
107
   * operations.
 
108
   *
 
109
   * @param newSubsamp the new level of chrominance subsampling (one of
 
110
   * {@link TJ TJ.SAMP_*})
 
111
   */
 
112
  public void setSubsamp(int newSubsamp) throws Exception {
 
113
    if(newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
 
114
      throw new Exception("Invalid argument in setSubsamp()");
 
115
    subsamp = newSubsamp;
 
116
  }
 
117
 
 
118
  /**
 
119
   * Set the JPEG image quality level for subsequent compress operations.
 
120
   *
 
121
   * @param quality the new JPEG image quality level (1 to 100, 1 = worst,
 
122
   * 100 = best)
 
123
   */
 
124
  public void setJPEGQuality(int quality) throws Exception {
 
125
    if(quality < 1 || quality > 100)
 
126
      throw new Exception("Invalid argument in setJPEGQuality()");
 
127
    jpegQuality = quality;
 
128
  }
 
129
 
 
130
  /**
 
131
   * Compress the uncompressed source image associated with this compressor
 
132
   * instance and output a JPEG image to the given destination buffer.
 
133
   *
 
134
   * @param dstBuf buffer which will receive the JPEG image.  Use
 
135
   * {@link TJ#bufSize} to determine the maximum size for this buffer based on
 
136
   * the image width and height.
 
137
   *
 
138
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
139
   */
 
140
  public void compress(byte[] dstBuf, int flags) throws Exception {
 
141
    if(dstBuf == null || flags < 0)
 
142
      throw new Exception("Invalid argument in compress()");
 
143
    if(srcBuf == null) throw new Exception(NO_ASSOC_ERROR);
 
144
    if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
 
145
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
146
    compressedSize = compress(srcBuf, srcWidth, srcPitch,
 
147
      srcHeight, srcPixelFormat, dstBuf, subsamp, jpegQuality, flags);
 
148
  }
 
149
 
 
150
  /**
 
151
   * Compress the uncompressed source image associated with this compressor
 
152
   * instance and return a buffer containing a JPEG image.
 
153
   *
 
154
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
155
   *
 
156
   * @return a buffer containing a JPEG image.  The length of this buffer will
 
157
   * not be equal to the size of the JPEG image.  Use {@link
 
158
   * #getCompressedSize} to obtain the size of the JPEG image.
 
159
   */
 
160
  public byte[] compress(int flags) throws Exception {
 
161
    if(srcWidth < 1 || srcHeight < 1)
 
162
      throw new Exception(NO_ASSOC_ERROR);
 
163
    byte[] buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)];
 
164
    compress(buf, flags);
 
165
    return buf;
 
166
  }
 
167
 
 
168
  /**
 
169
   * Compress the uncompressed source image stored in <code>srcImage</code>
 
170
   * and output a JPEG image to the given destination buffer.
 
171
   *
 
172
   * @param srcImage a <code>BufferedImage</code> instance containing RGB or
 
173
   * grayscale pixels to be compressed
 
174
   *
 
175
   * @param dstBuf buffer which will receive the JPEG image.  Use
 
176
   * {@link TJ#bufSize} to determine the maximum size for this buffer based on
 
177
   * the image width and height.
 
178
   *
 
179
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
180
   */
 
181
  public void compress(BufferedImage srcImage, byte[] dstBuf, int flags)
 
182
    throws Exception {
 
183
    if(srcImage == null || dstBuf == null || flags < 0)
 
184
      throw new Exception("Invalid argument in compress()");
 
185
    int width = srcImage.getWidth();
 
186
    int height = srcImage.getHeight();
 
187
    int pixelFormat;  boolean intPixels = false;
 
188
    if(byteOrder == null)
 
189
      byteOrder = ByteOrder.nativeOrder();
 
190
    switch(srcImage.getType()) {
 
191
      case BufferedImage.TYPE_3BYTE_BGR:
 
192
        pixelFormat = TJ.PF_BGR;  break;
 
193
      case BufferedImage.TYPE_BYTE_GRAY:
 
194
        pixelFormat = TJ.PF_GRAY;  break;
 
195
      case BufferedImage.TYPE_INT_BGR:
 
196
        if(byteOrder == ByteOrder.BIG_ENDIAN)
 
197
          pixelFormat = TJ.PF_XBGR;
 
198
        else
 
199
          pixelFormat = TJ.PF_RGBX;
 
200
        intPixels = true;  break;
 
201
      case BufferedImage.TYPE_INT_RGB:
 
202
        if(byteOrder == ByteOrder.BIG_ENDIAN)
 
203
          pixelFormat = TJ.PF_XRGB;
 
204
        else
 
205
          pixelFormat = TJ.PF_BGRX;
 
206
        intPixels = true;  break;
 
207
      default:
 
208
        throw new Exception("Unsupported BufferedImage format");
 
209
    }
 
210
    WritableRaster wr = srcImage.getRaster();
 
211
    if(jpegQuality < 0) throw new Exception("JPEG Quality not set");
 
212
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
213
    if(intPixels) {
 
214
      SinglePixelPackedSampleModel sm =
 
215
        (SinglePixelPackedSampleModel)srcImage.getSampleModel();
 
216
      int pitch = sm.getScanlineStride();
 
217
      DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
 
218
      int[] buf = db.getData();
 
219
      compressedSize = compress(buf, width, pitch, height, pixelFormat, dstBuf,
 
220
        subsamp, jpegQuality, flags);
 
221
    }
 
222
    else {
 
223
      ComponentSampleModel sm =
 
224
        (ComponentSampleModel)srcImage.getSampleModel();
 
225
      int pixelSize = sm.getPixelStride();
 
226
      if(pixelSize != TJ.getPixelSize(pixelFormat))
 
227
        throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
 
228
      int pitch = sm.getScanlineStride();
 
229
      DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
 
230
      byte[] buf = db.getData();
 
231
      compressedSize = compress(buf, width, pitch, height, pixelFormat, dstBuf,
 
232
        subsamp, jpegQuality, flags);
 
233
    }
 
234
  }
 
235
 
 
236
  /**
 
237
   * Compress the uncompressed source image stored in <code>srcImage</code>
 
238
   * and return a buffer containing a JPEG image.
 
239
   *
 
240
   * @param srcImage a <code>BufferedImage</code> instance containing RGB or
 
241
   * grayscale pixels to be compressed
 
242
   *
 
243
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
244
   *
 
245
   * @return a buffer containing a JPEG image.  The length of this buffer will
 
246
   * not be equal to the size of the JPEG image.  Use {@link
 
247
   * #getCompressedSize} to obtain the size of the JPEG image.
 
248
   */
 
249
  public byte[] compress(BufferedImage srcImage, int flags) throws Exception {
 
250
    int width = srcImage.getWidth();
 
251
    int height = srcImage.getHeight();
 
252
    byte[] buf = new byte[TJ.bufSize(width, height, subsamp)];
 
253
    compress(srcImage, buf, flags);
 
254
    return buf;
 
255
  }
 
256
 
 
257
  /**
 
258
   * Encode the uncompressed source image associated with this compressor
 
259
   * instance and output a YUV planar image to the given destination buffer.
 
260
   * This method uses the accelerated color conversion routines in
 
261
   * TurboJPEG's underlying codec to produce a planar YUV image that is
 
262
   * suitable for direct video display.  Specifically, if the chrominance
 
263
   * components are subsampled along the horizontal dimension, then the width
 
264
   * of the luminance plane is padded to 2 in the output image (same goes for
 
265
   * the height of the luminance plane, if the chrominance components are
 
266
   * subsampled along the vertical dimension.)  Also, each line of each plane
 
267
   * in the output image is padded to 4 bytes.  Although this will work with
 
268
   * any subsampling option, it is really only useful in combination with
 
269
   * {@link TJ#SAMP_420}, which produces an image compatible with the I420 (AKA
 
270
   * "YUV420P") format.
 
271
   *
 
272
   * @param dstBuf buffer which will receive the YUV planar image.  Use
 
273
   * {@link TJ#bufSizeYUV} to determine the appropriate size for this buffer
 
274
   * based on the image width, height, and level of chrominance subsampling.
 
275
   *
 
276
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
277
   */
 
278
  public void encodeYUV(byte[] dstBuf, int flags) throws Exception {
 
279
    if(dstBuf == null || flags < 0)
 
280
      throw new Exception("Invalid argument in compress()");
 
281
    if(srcBuf == null) throw new Exception(NO_ASSOC_ERROR);
 
282
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
283
    encodeYUV(srcBuf, srcWidth, srcPitch, srcHeight,
 
284
      srcPixelFormat, dstBuf, subsamp, flags);
 
285
    compressedSize = TJ.bufSizeYUV(srcWidth, srcHeight, subsamp);
 
286
  }
 
287
 
 
288
  /**
 
289
   * Encode the uncompressed source image associated with this compressor
 
290
   * instance and return a buffer containing a YUV planar image.  See
 
291
   * {@link #encodeYUV(byte[], int)} for more detail.
 
292
   *
 
293
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
294
   *
 
295
   * @return a buffer containing a YUV planar image
 
296
   */
 
297
  public byte[] encodeYUV(int flags) throws Exception {
 
298
    if(srcWidth < 1 || srcHeight < 1)
 
299
      throw new Exception(NO_ASSOC_ERROR);
 
300
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
301
    byte[] buf = new byte[TJ.bufSizeYUV(srcWidth, srcHeight, subsamp)];
 
302
    encodeYUV(buf, flags);
 
303
    return buf;
 
304
  }
 
305
 
 
306
  /**
 
307
   * Encode the uncompressed source image stored in <code>srcImage</code>
 
308
   * and output a YUV planar image to the given destination buffer.  See
 
309
   * {@link #encodeYUV(byte[], int)} for more detail.
 
310
   *
 
311
   * @param srcImage a <code>BufferedImage</code> instance containing RGB or
 
312
   * grayscale pixels to be encoded
 
313
   *
 
314
   * @param dstBuf buffer which will receive the YUV planar image.  Use
 
315
   * {@link TJ#bufSizeYUV} to determine the appropriate size for this buffer
 
316
   * based on the image width, height, and level of chrominance subsampling.
 
317
   *
 
318
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
319
   */
 
320
  public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags)
 
321
    throws Exception {
 
322
    if(srcImage == null || dstBuf == null || flags < 0)
 
323
      throw new Exception("Invalid argument in encodeYUV()");
 
324
    int width = srcImage.getWidth();
 
325
    int height = srcImage.getHeight();
 
326
    int pixelFormat;  boolean intPixels = false;
 
327
    if(byteOrder == null)
 
328
      byteOrder = ByteOrder.nativeOrder();
 
329
    switch(srcImage.getType()) {
 
330
      case BufferedImage.TYPE_3BYTE_BGR:
 
331
        pixelFormat = TJ.PF_BGR;  break;
 
332
      case BufferedImage.TYPE_BYTE_GRAY:
 
333
        pixelFormat = TJ.PF_GRAY;  break;
 
334
      case BufferedImage.TYPE_INT_BGR:
 
335
        if(byteOrder == ByteOrder.BIG_ENDIAN)
 
336
          pixelFormat = TJ.PF_XBGR;
 
337
        else
 
338
          pixelFormat = TJ.PF_RGBX;
 
339
        intPixels = true;  break;
 
340
      case BufferedImage.TYPE_INT_RGB:
 
341
        if(byteOrder == ByteOrder.BIG_ENDIAN)
 
342
          pixelFormat = TJ.PF_XRGB;
 
343
        else
 
344
          pixelFormat = TJ.PF_BGRX;
 
345
        intPixels = true;  break;
 
346
      default:
 
347
        throw new Exception("Unsupported BufferedImage format");
 
348
    }
 
349
    WritableRaster wr = srcImage.getRaster();
 
350
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
351
    if(intPixels) {
 
352
      SinglePixelPackedSampleModel sm =
 
353
        (SinglePixelPackedSampleModel)srcImage.getSampleModel();
 
354
      int pitch = sm.getScanlineStride();
 
355
      DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
 
356
      int[] buf = db.getData();
 
357
      encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
 
358
        flags);
 
359
    }
 
360
    else {
 
361
      ComponentSampleModel sm =
 
362
        (ComponentSampleModel)srcImage.getSampleModel();
 
363
      int pixelSize = sm.getPixelStride();
 
364
      if(pixelSize != TJ.getPixelSize(pixelFormat))
 
365
        throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage");
 
366
      int pitch = sm.getScanlineStride();
 
367
      DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
 
368
      byte[] buf = db.getData();
 
369
      encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, subsamp,
 
370
        flags);
 
371
    }
 
372
    compressedSize = TJ.bufSizeYUV(width, height, subsamp);
 
373
  }
 
374
 
 
375
  /**
 
376
   * Encode the uncompressed source image stored in <code>srcImage</code>
 
377
   * and return a buffer containing a YUV planar image.  See
 
378
   * {@link #encodeYUV(byte[], int)} for more detail.
 
379
   *
 
380
   * @param srcImage a <code>BufferedImage</code> instance containing RGB or
 
381
   * grayscale pixels to be encoded
 
382
   *
 
383
   * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
 
384
   *
 
385
   * @return a buffer containing a YUV planar image
 
386
   */
 
387
  public byte[] encodeYUV(BufferedImage srcImage, int flags)
 
388
    throws Exception {
 
389
    if(subsamp < 0) throw new Exception("Subsampling level not set");
 
390
    int width = srcImage.getWidth();
 
391
    int height = srcImage.getHeight();
 
392
    byte[] buf = new byte[TJ.bufSizeYUV(width, height, subsamp)];
 
393
    encodeYUV(srcImage, buf, flags);
 
394
    return buf;
 
395
  }
 
396
 
 
397
  /**
 
398
   * Returns the size of the image (in bytes) generated by the most recent
 
399
   * compress/encode operation.
 
400
   *
 
401
   * @return the size of the image (in bytes) generated by the most recent
 
402
   * compress/encode operation
 
403
   */
 
404
  public int getCompressedSize() {
 
405
    return compressedSize;
 
406
  }
 
407
 
 
408
  /**
 
409
   * Free the native structures associated with this compressor instance.
 
410
   */
 
411
  public void close() throws Exception {
 
412
    destroy();
 
413
  }
 
414
 
 
415
  protected void finalize() throws Throwable {
 
416
    try {
 
417
      close();
 
418
    }
 
419
    catch(Exception e) {}
 
420
    finally {
 
421
      super.finalize();
 
422
    }
 
423
  };
 
424
 
 
425
  private native void init() throws Exception;
 
426
 
 
427
  private native void destroy() throws Exception;
 
428
 
 
429
  // JPEG size in bytes is returned
 
430
  private native int compress(byte[] srcBuf, int width, int pitch,
 
431
    int height, int pixelFormat, byte[] dstbuf, int jpegSubsamp, int jpegQual,
 
432
    int flags) throws Exception;
 
433
 
 
434
  private native int compress(int[] srcBuf, int width, int pitch,
 
435
    int height, int pixelFormat, byte[] dstbuf, int jpegSubsamp, int jpegQual,
 
436
    int flags) throws Exception;
 
437
 
 
438
  private native void encodeYUV(byte[] srcBuf, int width, int pitch,
 
439
    int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)
 
440
    throws Exception;
 
441
 
 
442
  private native void encodeYUV(int[] srcBuf, int width, int pitch,
 
443
    int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)
 
444
    throws Exception;
 
445
 
 
446
  static {
 
447
    TJLoader.load();
 
448
  }
 
449
 
 
450
  private long handle = 0;
 
451
  private byte[] srcBuf = null;
 
452
  private int srcWidth = 0;
 
453
  private int srcHeight = 0;
 
454
  private int srcPitch = 0;
 
455
  private int srcPixelFormat = -1;
 
456
  private int subsamp = -1;
 
457
  private int jpegQuality = -1;
 
458
  private int compressedSize = 0;
 
459
  private ByteOrder byteOrder = null;
 
460
};