2
* Copyright (C)2011 D. R. Commander. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
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.
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.
29
package org.libjpegturbo.turbojpeg;
31
import java.awt.image.*;
35
* TurboJPEG compressor
37
public class TJCompressor {
39
private final static String NO_ASSOC_ERROR =
40
"No source image is associated with this instance";
43
* Create a TurboJPEG compressor instance.
45
public TJCompressor() throws Exception {
50
* Create a TurboJPEG compressor instance and associate the uncompressed
51
* source image stored in <code>srcImage</code> with the newly-created
54
* @param srcImage see {@link #setSourceImage} for description
56
* @param width see {@link #setSourceImage} for description
58
* @param pitch see {@link #setSourceImage} for description
60
* @param height see {@link #setSourceImage} for description
62
* @param pixelFormat see {@link #setSourceImage} for description
64
public TJCompressor(byte[] srcImage, int width, int pitch, int height,
65
int pixelFormat) throws Exception {
66
setSourceImage(srcImage, width, pitch, height, pixelFormat);
70
* Associate an uncompressed source image with this compressor instance.
72
* @param srcImage image buffer containing RGB or grayscale pixels to be
75
* @param width width (in pixels) of the source image
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>.
86
* @param height height (in pixels) of the source image
88
* @param pixelFormat pixel format of the source image (one of
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()");
99
if(pitch == 0) srcPitch = width * TJ.getPixelSize(pixelFormat);
100
else srcPitch = pitch;
102
srcPixelFormat = pixelFormat;
106
* Set the level of chrominance subsampling for subsequent compress/encode
109
* @param newSubsamp the new level of chrominance subsampling (one of
110
* {@link TJ TJ.SAMP_*})
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;
119
* Set the JPEG image quality level for subsequent compress operations.
121
* @param quality the new JPEG image quality level (1 to 100, 1 = worst,
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;
131
* Compress the uncompressed source image associated with this compressor
132
* instance and output a JPEG image to the given destination buffer.
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.
138
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
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);
151
* Compress the uncompressed source image associated with this compressor
152
* instance and return a buffer containing a JPEG image.
154
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
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.
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);
169
* Compress the uncompressed source image stored in <code>srcImage</code>
170
* and output a JPEG image to the given destination buffer.
172
* @param srcImage a <code>BufferedImage</code> instance containing RGB or
173
* grayscale pixels to be compressed
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.
179
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
181
public void compress(BufferedImage srcImage, byte[] dstBuf, int flags)
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;
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;
205
pixelFormat = TJ.PF_BGRX;
206
intPixels = true; break;
208
throw new Exception("Unsupported BufferedImage format");
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");
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);
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);
237
* Compress the uncompressed source image stored in <code>srcImage</code>
238
* and return a buffer containing a JPEG image.
240
* @param srcImage a <code>BufferedImage</code> instance containing RGB or
241
* grayscale pixels to be compressed
243
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
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.
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);
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
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.
276
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
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);
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.
293
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
295
* @return a buffer containing a YUV planar image
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);
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.
311
* @param srcImage a <code>BufferedImage</code> instance containing RGB or
312
* grayscale pixels to be encoded
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.
318
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
320
public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags)
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;
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;
344
pixelFormat = TJ.PF_BGRX;
345
intPixels = true; break;
347
throw new Exception("Unsupported BufferedImage format");
349
WritableRaster wr = srcImage.getRaster();
350
if(subsamp < 0) throw new Exception("Subsampling level not set");
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,
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,
372
compressedSize = TJ.bufSizeYUV(width, height, subsamp);
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.
380
* @param srcImage a <code>BufferedImage</code> instance containing RGB or
381
* grayscale pixels to be encoded
383
* @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*}
385
* @return a buffer containing a YUV planar image
387
public byte[] encodeYUV(BufferedImage srcImage, int flags)
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);
398
* Returns the size of the image (in bytes) generated by the most recent
399
* compress/encode operation.
401
* @return the size of the image (in bytes) generated by the most recent
402
* compress/encode operation
404
public int getCompressedSize() {
405
return compressedSize;
409
* Free the native structures associated with this compressor instance.
411
public void close() throws Exception {
415
protected void finalize() throws Throwable {
419
catch(Exception e) {}
425
private native void init() throws Exception;
427
private native void destroy() throws Exception;
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;
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;
438
private native void encodeYUV(byte[] srcBuf, int width, int pitch,
439
int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)
442
private native void encodeYUV(int[] srcBuf, int width, int pitch,
443
int height, int pixelFormat, byte[] dstbuf, int subsamp, int flags)
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;