| 1 |
/* |
| 2 |
* Copyright (C)2011-2015, 2018, 2020, 2022-2025 D. R. Commander. |
| 3 |
* All Rights Reserved. |
| 4 |
* Copyright (C)2015 Viktor Szathmáry. All Rights Reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* - Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
* this list of conditions and the following disclaimer in the documentation |
| 13 |
* and/or other materials provided with the distribution. |
| 14 |
* - Neither the name of the libjpeg-turbo Project nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from this |
| 16 |
* software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
* POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
package org.libjpegturbo.turbojpeg; |
| 32 |
|
| 33 |
import java.awt.image.*; |
| 34 |
import java.nio.*; |
| 35 |
import java.io.*; |
| 36 |
|
| 37 |
/** |
| 38 |
* TurboJPEG compressor |
| 39 |
*/ |
| 40 |
public class TJCompressor implements Closeable { |
| 41 |
|
| 42 |
private static final String NO_ASSOC_ERROR = |
| 43 |
"No source image is associated with this instance"; |
| 44 |
|
| 45 |
/** |
| 46 |
* Create a TurboJPEG compressor instance. |
| 47 |
*/ |
| 48 |
public TJCompressor() throws TJException { |
| 49 |
init(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Create a TurboJPEG compressor instance and associate the packed-pixel |
| 54 |
* source image, stored in <code>srcImage</code> with 2 to 8 bits of data |
| 55 |
* precision per sample, with the newly created instance. |
| 56 |
* |
| 57 |
* @param srcImage see {@link #setSourceImage} for description |
| 58 |
* |
| 59 |
* @param x see {@link #setSourceImage} for description |
| 60 |
* |
| 61 |
* @param y see {@link #setSourceImage} for description |
| 62 |
* |
| 63 |
* @param width see {@link #setSourceImage} for description |
| 64 |
* |
| 65 |
* @param pitch see {@link #setSourceImage} for description |
| 66 |
* |
| 67 |
* @param height see {@link #setSourceImage} for description |
| 68 |
* |
| 69 |
* @param pixelFormat pixel format of the source image (one of |
| 70 |
* {@link TJ#PF_RGB TJ.PF_*}) |
| 71 |
*/ |
| 72 |
public TJCompressor(byte[] srcImage, int x, int y, int width, int pitch, |
| 73 |
int height, int pixelFormat) throws TJException { |
| 74 |
setSourceImage(srcImage, x, y, width, pitch, height, pixelFormat); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Create a TurboJPEG compressor instance and associate the 8-bit-per-sample |
| 79 |
* packed-pixel source image stored in <code>srcImage</code> with the newly |
| 80 |
* created instance. |
| 81 |
* |
| 82 |
* @param srcImage see |
| 83 |
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description |
| 84 |
* |
| 85 |
* @param x see |
| 86 |
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description |
| 87 |
* |
| 88 |
* @param y see |
| 89 |
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description |
| 90 |
* |
| 91 |
* @param width see |
| 92 |
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description |
| 93 |
* |
| 94 |
* @param height see |
| 95 |
* {@link #setSourceImage(BufferedImage, int, int, int, int)} for description |
| 96 |
*/ |
| 97 |
public TJCompressor(BufferedImage srcImage, int x, int y, int width, |
| 98 |
int height) throws TJException { |
| 99 |
setSourceImage(srcImage, x, y, width, height); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Associate a packed-pixel RGB, grayscale, or CMYK source image with 2 to 8 |
| 104 |
* bits of data precision per sample with this compressor instance. Note |
| 105 |
* that packed-pixel source images with 2 to 7 bits of data precision per |
| 106 |
* sample can only be compressed into lossless JPEG images. |
| 107 |
* |
| 108 |
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK |
| 109 |
* source image to be compressed or encoded. The data precision of the |
| 110 |
* source image (from 2 to 8 bits per sample) can be specified using |
| 111 |
* {@link TJ#PARAM_PRECISION} and defaults to 8 if {@link TJ#PARAM_PRECISION} |
| 112 |
* is unset or out of range. This buffer is not modified. |
| 113 |
* |
| 114 |
* @param x x offset (in pixels) of the region in the source image from which |
| 115 |
* the JPEG or YUV image should be compressed/encoded |
| 116 |
* |
| 117 |
* @param y y offset (in pixels) of the region in the source image from which |
| 118 |
* the JPEG or YUV image should be compressed/encoded |
| 119 |
* |
| 120 |
* @param width width (in pixels) of the region in the source image from |
| 121 |
* which the JPEG or YUV image should be compressed/encoded |
| 122 |
* |
| 123 |
* @param pitch bytes per row in the source image. Normally this should be |
| 124 |
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>, |
| 125 |
* if the source image is unpadded. (Setting this parameter to 0 is the |
| 126 |
* equivalent of setting it to <code>width * |
| 127 |
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However, |
| 128 |
* you can also use this parameter to specify the row alignment/padding of |
| 129 |
* the source image, to skip rows, or to compress/encode a JPEG or YUV image |
| 130 |
* from a specific region of a larger source image. |
| 131 |
* |
| 132 |
* @param height height (in pixels) of the region in the source image from |
| 133 |
* which the JPEG or YUV image should be compressed/encoded |
| 134 |
* |
| 135 |
* @param pixelFormat pixel format of the source image (one of |
| 136 |
* {@link TJ#PF_RGB TJ.PF_*}) |
| 137 |
*/ |
| 138 |
public void setSourceImage(byte[] srcImage, int x, int y, int width, |
| 139 |
int pitch, int height, int pixelFormat) |
| 140 |
throws TJException { |
| 141 |
if (handle == 0) init(); |
| 142 |
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 || |
| 143 |
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF) |
| 144 |
throw new IllegalArgumentException("Invalid argument in setSourceImage()"); |
| 145 |
srcBuf8 = srcImage; |
| 146 |
srcWidth = width; |
| 147 |
if (pitch == 0) |
| 148 |
srcPitch = width * TJ.getPixelSize(pixelFormat); |
| 149 |
else |
| 150 |
srcPitch = pitch; |
| 151 |
srcHeight = height; |
| 152 |
srcPixelFormat = pixelFormat; |
| 153 |
srcX = x; |
| 154 |
srcY = y; |
| 155 |
srcBuf12 = null; |
| 156 |
srcBuf16 = null; |
| 157 |
srcBufInt = null; |
| 158 |
srcYUVImage = null; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Associate a packed-pixel RGB, grayscale, or CMYK source image with 9 to 12 |
| 163 |
* bits of data precision per sample with this compressor instance. Note |
| 164 |
* that packed-pixel source images with 9 to 11 bits of data precision per |
| 165 |
* sample can only be compressed into lossless JPEG images. |
| 166 |
* |
| 167 |
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK |
| 168 |
* source image to be compressed. The data precision of the source image |
| 169 |
* (from 9 to 12 bits per sample) can be specified using |
| 170 |
* {@link TJ#PARAM_PRECISION} and defaults to 12 if |
| 171 |
* {@link TJ#PARAM_PRECISION} is unset or out of range. This buffer is not |
| 172 |
* modified. |
| 173 |
* |
| 174 |
* @param x x offset (in pixels) of the region in the source image from which |
| 175 |
* the JPEG image should be compressed |
| 176 |
* |
| 177 |
* @param y y offset (in pixels) of the region in the source image from which |
| 178 |
* the JPEG image should be compressed |
| 179 |
* |
| 180 |
* @param width width (in pixels) of the region in the source image from |
| 181 |
* which the JPEG image should be compressed |
| 182 |
* |
| 183 |
* @param pitch samples per row in the source image. Normally this should be |
| 184 |
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>, |
| 185 |
* if the source image is unpadded. (Setting this parameter to 0 is the |
| 186 |
* equivalent of setting it to <code>width * |
| 187 |
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However, |
| 188 |
* you can also use this parameter to specify the row alignment/padding of |
| 189 |
* the source image, to skip rows, or to compress a JPEG image from a |
| 190 |
* specific region of a larger source image. |
| 191 |
* |
| 192 |
* @param height height (in pixels) of the region in the source image from |
| 193 |
* which the JPEG image should be compressed |
| 194 |
* |
| 195 |
* @param pixelFormat pixel format of the source image (one of |
| 196 |
* {@link TJ#PF_RGB TJ.PF_*}) |
| 197 |
*/ |
| 198 |
public void setSourceImage12(short[] srcImage, int x, int y, int width, |
| 199 |
int pitch, int height, int pixelFormat) |
| 200 |
throws TJException { |
| 201 |
if (handle == 0) init(); |
| 202 |
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 || |
| 203 |
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF) |
| 204 |
throw new IllegalArgumentException("Invalid argument in setSourceImage()"); |
| 205 |
srcBuf12 = srcImage; |
| 206 |
srcWidth = width; |
| 207 |
if (pitch == 0) |
| 208 |
srcPitch = width * TJ.getPixelSize(pixelFormat); |
| 209 |
else |
| 210 |
srcPitch = pitch; |
| 211 |
srcHeight = height; |
| 212 |
srcPixelFormat = pixelFormat; |
| 213 |
srcX = x; |
| 214 |
srcY = y; |
| 215 |
srcBuf8 = null; |
| 216 |
srcBuf16 = null; |
| 217 |
srcBufInt = null; |
| 218 |
srcYUVImage = null; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Associate a packed-pixel RGB, grayscale, or CMYK source image with 13 to |
| 223 |
* 16 bits of data precision per sample with this compressor instance. Note |
| 224 |
* that packed-pixel source images with 13 to 16 bits of data precision per |
| 225 |
* sample can only be compressed into lossless JPEG images. |
| 226 |
* |
| 227 |
* @param srcImage buffer containing a packed-pixel RGB, grayscale, or CMYK |
| 228 |
* source image to be compressed. The data precision of the source image |
| 229 |
* (from 13 to 16 bits per sample) can be specified using |
| 230 |
* {@link TJ#PARAM_PRECISION} and defaults to 16 if |
| 231 |
* {@link TJ#PARAM_PRECISION} is unset or out of range. This buffer is not |
| 232 |
* modified. |
| 233 |
* |
| 234 |
* @param x x offset (in pixels) of the region in the source image from which |
| 235 |
* the JPEG image should be compressed |
| 236 |
* |
| 237 |
* @param y y offset (in pixels) of the region in the source image from which |
| 238 |
* the JPEG image should be compressed |
| 239 |
* |
| 240 |
* @param width width (in pixels) of the region in the source image from |
| 241 |
* which the JPEG image should be compressed |
| 242 |
* |
| 243 |
* @param pitch samples per row in the source image. Normally this should be |
| 244 |
* <code>width * {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>, |
| 245 |
* if the source image is unpadded. (Setting this parameter to 0 is the |
| 246 |
* equivalent of setting it to <code>width * |
| 247 |
* {@link TJ#getPixelSize TJ.getPixelSize}(pixelFormat)</code>.) However, |
| 248 |
* you can also use this parameter to specify the row alignment/padding of |
| 249 |
* the source image, to skip rows, or to compress a JPEG image from a |
| 250 |
* specific region of a larger source image. |
| 251 |
* |
| 252 |
* @param height height (in pixels) of the region in the source image from |
| 253 |
* which the JPEG image should be compressed |
| 254 |
* |
| 255 |
* @param pixelFormat pixel format of the source image (one of |
| 256 |
* {@link TJ#PF_RGB TJ.PF_*}) |
| 257 |
*/ |
| 258 |
public void setSourceImage16(short[] srcImage, int x, int y, int width, |
| 259 |
int pitch, int height, int pixelFormat) |
| 260 |
throws TJException { |
| 261 |
if (handle == 0) init(); |
| 262 |
if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 || |
| 263 |
pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF) |
| 264 |
throw new IllegalArgumentException("Invalid argument in setSourceImage()"); |
| 265 |
srcBuf16 = srcImage; |
| 266 |
srcWidth = width; |
| 267 |
if (pitch == 0) |
| 268 |
srcPitch = width * TJ.getPixelSize(pixelFormat); |
| 269 |
else |
| 270 |
srcPitch = pitch; |
| 271 |
srcHeight = height; |
| 272 |
srcPixelFormat = pixelFormat; |
| 273 |
srcX = x; |
| 274 |
srcY = y; |
| 275 |
srcBuf8 = null; |
| 276 |
srcBuf12 = null; |
| 277 |
srcBufInt = null; |
| 278 |
srcYUVImage = null; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Associate an 8-bit-per-sample packed-pixel RGB or grayscale source image |
| 283 |
* with this compressor instance. |
| 284 |
* |
| 285 |
* @param srcImage a <code>BufferedImage</code> instance containing a |
| 286 |
* packed-pixel RGB or grayscale source image to be compressed or encoded. |
| 287 |
* This image is not modified. |
| 288 |
* |
| 289 |
* @param x x offset (in pixels) of the region in the source image from which |
| 290 |
* the JPEG or YUV image should be compressed/encoded |
| 291 |
* |
| 292 |
* @param y y offset (in pixels) of the region in the source image from which |
| 293 |
* the JPEG or YUV image should be compressed/encoded |
| 294 |
* |
| 295 |
* @param width width (in pixels) of the region in the source image from |
| 296 |
* which the JPEG or YUV image should be compressed/encoded (0 = use the |
| 297 |
* width of the source image) |
| 298 |
* |
| 299 |
* @param height height (in pixels) of the region in the source image from |
| 300 |
* which the JPEG or YUV image should be compressed/encoded (0 = use the |
| 301 |
* height of the source image) |
| 302 |
*/ |
| 303 |
public void setSourceImage(BufferedImage srcImage, int x, int y, int width, |
| 304 |
int height) throws TJException { |
| 305 |
if (handle == 0) init(); |
| 306 |
if (srcImage == null || x < 0 || y < 0 || width < 0 || height < 0) |
| 307 |
throw new IllegalArgumentException("Invalid argument in setSourceImage()"); |
| 308 |
srcX = x; |
| 309 |
srcY = y; |
| 310 |
srcWidth = (width == 0) ? srcImage.getWidth() : width; |
| 311 |
srcHeight = (height == 0) ? srcImage.getHeight() : height; |
| 312 |
if (x + width > srcImage.getWidth() || y + height > srcImage.getHeight()) |
| 313 |
throw new IllegalArgumentException("Compression region exceeds the bounds of the source image"); |
| 314 |
|
| 315 |
int pixelFormat; |
| 316 |
boolean intPixels = false; |
| 317 |
if (byteOrder == null) |
| 318 |
byteOrder = ByteOrder.nativeOrder(); |
| 319 |
switch (srcImage.getType()) { |
| 320 |
case BufferedImage.TYPE_3BYTE_BGR: |
| 321 |
pixelFormat = TJ.PF_BGR; break; |
| 322 |
case BufferedImage.TYPE_4BYTE_ABGR: |
| 323 |
case BufferedImage.TYPE_4BYTE_ABGR_PRE: |
| 324 |
pixelFormat = TJ.PF_XBGR; break; |
| 325 |
case BufferedImage.TYPE_BYTE_GRAY: |
| 326 |
pixelFormat = TJ.PF_GRAY; break; |
| 327 |
case BufferedImage.TYPE_INT_BGR: |
| 328 |
if (byteOrder == ByteOrder.BIG_ENDIAN) |
| 329 |
pixelFormat = TJ.PF_XBGR; |
| 330 |
else |
| 331 |
pixelFormat = TJ.PF_RGBX; |
| 332 |
intPixels = true; break; |
| 333 |
case BufferedImage.TYPE_INT_RGB: |
| 334 |
case BufferedImage.TYPE_INT_ARGB: |
| 335 |
case BufferedImage.TYPE_INT_ARGB_PRE: |
| 336 |
if (byteOrder == ByteOrder.BIG_ENDIAN) |
| 337 |
pixelFormat = TJ.PF_XRGB; |
| 338 |
else |
| 339 |
pixelFormat = TJ.PF_BGRX; |
| 340 |
intPixels = true; break; |
| 341 |
default: |
| 342 |
throw new IllegalArgumentException("Unsupported BufferedImage format"); |
| 343 |
} |
| 344 |
srcPixelFormat = pixelFormat; |
| 345 |
|
| 346 |
WritableRaster wr = srcImage.getRaster(); |
| 347 |
if (intPixels) { |
| 348 |
SinglePixelPackedSampleModel sm = |
| 349 |
(SinglePixelPackedSampleModel)srcImage.getSampleModel(); |
| 350 |
srcPitch = sm.getScanlineStride(); |
| 351 |
DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); |
| 352 |
srcBufInt = db.getData(); |
| 353 |
srcBuf8 = null; |
| 354 |
srcBuf12 = null; |
| 355 |
srcBuf16 = null; |
| 356 |
} else { |
| 357 |
ComponentSampleModel sm = |
| 358 |
(ComponentSampleModel)srcImage.getSampleModel(); |
| 359 |
int pixelSize = sm.getPixelStride(); |
| 360 |
if (pixelSize != TJ.getPixelSize(pixelFormat)) |
| 361 |
throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage"); |
| 362 |
srcPitch = sm.getScanlineStride(); |
| 363 |
DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); |
| 364 |
srcBuf8 = db.getData(); |
| 365 |
srcBuf12 = null; |
| 366 |
srcBuf16 = null; |
| 367 |
srcBufInt = null; |
| 368 |
} |
| 369 |
srcYUVImage = null; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Associate an 8-bit-per-sample planar YUV source image with this compressor |
| 374 |
* instance. This method sets {@link TJ#PARAM_SUBSAMP} to the chrominance |
| 375 |
* subsampling level of the source image. |
| 376 |
* |
| 377 |
* @param srcImage planar YUV source image to be compressed. This image is |
| 378 |
* not modified. |
| 379 |
*/ |
| 380 |
public void setSourceImage(YUVImage srcImage) throws TJException { |
| 381 |
if (handle == 0) init(); |
| 382 |
if (srcImage == null) |
| 383 |
throw new IllegalArgumentException("Invalid argument in setSourceImage()"); |
| 384 |
srcYUVImage = srcImage; |
| 385 |
set(TJ.PARAM_SUBSAMP, srcImage.getSubsamp()); |
| 386 |
srcBuf8 = null; |
| 387 |
srcBuf12 = null; |
| 388 |
srcBuf16 = null; |
| 389 |
srcBufInt = null; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Load a packed-pixel RGB or grayscale source image with 2 to 16 bits of |
| 394 |
* data precision per sample from disk into memory and associate it with this |
| 395 |
* compressor instance. Note that packed-pixel source images with 2 to 7, 9 |
| 396 |
* to 11, or 13 to 16 bits of data precision per sample can only be |
| 397 |
* compressed into lossless JPEG images. |
| 398 |
* |
| 399 |
* <p>{@link #getWidth()}, {@link #getPitch()}, {@link #getHeight()}, |
| 400 |
* {@link #getPixelFormat()}, and {@link #getSourceBuf()} can be used to |
| 401 |
* obtain the source image's dimensions, pixel format, and buffer after it is |
| 402 |
* loaded. |
| 403 |
* |
| 404 |
* @param fileName name of a file containing a packed-pixel image in Windows |
| 405 |
* BMP or PBMPLUS (PPM/PGM) format. Windows BMP files require |
| 406 |
* 8-bit-per-sample data precision. When loading a PBMPLUS file, the target |
| 407 |
* data precision (from 2 to 16 bits per sample) can be specified using |
| 408 |
* {@link TJ#PARAM_PRECISION} and defaults to 8 if {@link TJ#PARAM_PRECISION} |
| 409 |
* is unset. If the data precision of the PBMPLUS file does not match the |
| 410 |
* target data precision, then upconverting or downconverting will be |
| 411 |
* performed. |
| 412 |
* |
| 413 |
* @param align row alignment (in samples) of the packed-pixel buffer into |
| 414 |
* which the source image will be loaded (must be a power of 2.) Setting |
| 415 |
* this parameter to n will cause all rows in the buffer to be padded to the |
| 416 |
* nearest multiple of n samples (1 = unpadded.) |
| 417 |
* |
| 418 |
* @param pixelFormat pixel format of the packed-pixel buffer into which the |
| 419 |
* source image will be loaded. The behavior of this method varies depending |
| 420 |
* on the value of <code>pixelFormat</code>: |
| 421 |
* <ul> |
| 422 |
* <li> {@link TJ#PF_UNKNOWN} : The packed-pixel buffer created by this |
| 423 |
* method will use the most optimal pixel format for the file type. Use |
| 424 |
* {@link #getPixelFormat()} to obtain the ID of that pixel format. |
| 425 |
* <li> {@link TJ#PF_GRAY} : Only PGM files and 8-bit-per-pixel BMP files |
| 426 |
* with a grayscale colormap can be loaded. |
| 427 |
* <li> {@link TJ#PF_CMYK} : The RGB or grayscale pixels stored in the file |
| 428 |
* will be converted using a quick & dirty algorithm that is suitable |
| 429 |
* only for testing purposes. (Proper conversion between CMYK and other |
| 430 |
* formats requires a color management system.) |
| 431 |
* <li> Other {@link TJ#PF_RGB pixel formats} : The packed-pixel buffer |
| 432 |
* will use the specified pixel format, and pixel format conversion will be |
| 433 |
* performed if necessary. |
| 434 |
* </ul> |
| 435 |
*/ |
| 436 |
public void loadSourceImage(String fileName, int align, int pixelFormat) |
| 437 |
throws TJException { |
| 438 |
int precision = get(TJ.PARAM_PRECISION); |
| 439 |
if (precision < 2 || precision > 16) |
| 440 |
precision = 8; |
| 441 |
Object srcBuf = loadSourceImage(precision, fileName, align, pixelFormat); |
| 442 |
if (precision <= 8) { |
| 443 |
srcBuf8 = (byte[])srcBuf; |
| 444 |
srcBuf12 = null; |
| 445 |
srcBuf16 = null; |
| 446 |
} else if (precision <= 12) { |
| 447 |
srcBuf12 = (short[])srcBuf; |
| 448 |
srcBuf8 = null; |
| 449 |
srcBuf16 = null; |
| 450 |
} else { |
| 451 |
srcBuf16 = (short[])srcBuf; |
| 452 |
srcBuf8 = null; |
| 453 |
srcBuf12 = null; |
| 454 |
} |
| 455 |
srcBufInt = null; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Returns the width of the source image (packed-pixel or YUV) associated |
| 460 |
* with this compressor instance. |
| 461 |
* |
| 462 |
* @return the width of the source image (packed-pixel or YUV) associated |
| 463 |
* with this compressor instance. |
| 464 |
*/ |
| 465 |
public int getWidth() { |
| 466 |
if (srcYUVImage != null) |
| 467 |
return srcYUVImage.getWidth(); |
| 468 |
if (srcWidth < 1) |
| 469 |
throw new IllegalStateException(NO_ASSOC_ERROR); |
| 470 |
return srcWidth; |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Returns the pitch (samples per row) of the packed-pixel source image |
| 475 |
* associated with this compressor instance. If the source image is a |
| 476 |
* <code>BufferedImage</code> instance with integer pixels, then the stride |
| 477 |
* (pixels per row) is returned instead. |
| 478 |
* |
| 479 |
* @return the pitch (samples per row) of the packed-pixel source image |
| 480 |
* associated with this compressor instance. |
| 481 |
*/ |
| 482 |
public int getPitch() { |
| 483 |
if (srcPitch < 1) |
| 484 |
throw new IllegalStateException(NO_ASSOC_ERROR); |
| 485 |
return srcPitch; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Returns the height of the source image (packed-pixel or YUV) associated |
| 490 |
* with this compressor instance. |
| 491 |
* |
| 492 |
* @return the height of the source image (packed-pixel or YUV) associated |
| 493 |
* with this compressor instance. |
| 494 |
*/ |
| 495 |
public int getHeight() { |
| 496 |
if (srcYUVImage != null) |
| 497 |
return srcYUVImage.getHeight(); |
| 498 |
if (srcHeight < 1) |
| 499 |
throw new IllegalStateException(NO_ASSOC_ERROR); |
| 500 |
return srcHeight; |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Returns the pixel format of the packed-pixel source image associated with |
| 505 |
* this compressor instance. The pixel format is one of |
| 506 |
* {@link TJ#PF_RGB TJ.PF_*}. |
| 507 |
* |
| 508 |
* @return the pixel format of the packed-pixel source image associated with |
| 509 |
* this compressor instance. |
| 510 |
*/ |
| 511 |
public int getPixelFormat() { |
| 512 |
if (srcPixelFormat == TJ.PF_UNKNOWN) |
| 513 |
throw new IllegalStateException(NO_ASSOC_ERROR); |
| 514 |
return srcPixelFormat; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Returns the buffer containing the packed-pixel image associated with this |
| 519 |
* compressor instance. The buffer is a <code>byte</code> array if the |
| 520 |
* source image has 2 to 8 bits of data precision per sample, a |
| 521 |
* <code>short</code> array if the source image has 9 to 16 bits of data |
| 522 |
* precision per sample, and either a <code>byte</code> or an |
| 523 |
* <code>int</code> array (depending on the image type) if the source image |
| 524 |
* is a <code>BufferedImage</code> instance. |
| 525 |
* |
| 526 |
* @return the buffer containing the packed-pixel image associated with this |
| 527 |
* compressor instance. |
| 528 |
*/ |
| 529 |
public Object getSourceBuf() { |
| 530 |
if (srcBuf8 != null) |
| 531 |
return srcBuf8; |
| 532 |
else if (srcBuf12 != null) |
| 533 |
return srcBuf12; |
| 534 |
else if (srcBuf16 != null) |
| 535 |
return srcBuf16; |
| 536 |
else if (srcBufInt != null) |
| 537 |
return srcBufInt; |
| 538 |
else |
| 539 |
throw new IllegalStateException(NO_ASSOC_ERROR); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Set the value of a compression parameter. |
| 544 |
* |
| 545 |
* @param param one of {@link TJ#PARAM_STOPONWARNING TJ.PARAM_*} |
| 546 |
* |
| 547 |
* @param value value of the compression parameter (refer to |
| 548 |
* {@link TJ#PARAM_STOPONWARNING parameter documentation}) |
| 549 |
*/ |
| 550 |
public native void set(int param, int value); |
| 551 |
|
| 552 |
/** |
| 553 |
* Get the value of a compression parameter. |
| 554 |
* |
| 555 |
* @param param one of {@link TJ#PARAM_STOPONWARNING TJ.PARAM_*} |
| 556 |
* |
| 557 |
* @return the value of the specified compression parameter, or -1 if the |
| 558 |
* value is unknown. |
| 559 |
*/ |
| 560 |
public native int get(int param); |
| 561 |
|
| 562 |
/** |
| 563 |
* Embed an ICC (International Color Consortium) color management profile in |
| 564 |
* JPEG images generated by subsequent compression operations. |
| 565 |
* |
| 566 |
* @param iccBuf buffer containing an ICC profile. (The size of the ICC |
| 567 |
* profile is assumed to be the length of the array.) A copy is made of the |
| 568 |
* ICC profile, so this buffer can be reused as soon as this method returns. |
| 569 |
* Setting this parameter to null removes any ICC profile that was previously |
| 570 |
* associated with this compressor instance. |
| 571 |
*/ |
| 572 |
public native void setICCProfile(byte[] iccBuf) throws TJException; |
| 573 |
|
| 574 |
/** |
| 575 |
* Compress the packed-pixel or planar YUV source image associated with this |
| 576 |
* compressor instance and output a JPEG image to the given destination |
| 577 |
* buffer. |
| 578 |
* |
| 579 |
* @param dstBuf buffer that will receive the JPEG image. Use |
| 580 |
* {@link TJ#bufSize TJ.bufSize()} to determine the maximum size for this |
| 581 |
* buffer based on the source image's width and height and the desired level |
| 582 |
* of chrominance subsampling (see {@link TJ#PARAM_SUBSAMP}), then add the |
| 583 |
* size of the ICC profile (if any) that was previously associated with this |
| 584 |
* compressor instance (see {@link #setICCProfile setICCProfile()}.) |
| 585 |
*/ |
| 586 |
public void compress(byte[] dstBuf) throws TJException { |
| 587 |
if (dstBuf == null) |
| 588 |
throw new IllegalArgumentException("Invalid argument in compress()"); |
| 589 |
|
| 590 |
if (srcYUVImage != null) { |
| 591 |
checkSubsampling(); |
| 592 |
if (get(TJ.PARAM_SUBSAMP) != srcYUVImage.getSubsamp()) |
| 593 |
throw new IllegalStateException("TJ.PARAM_SUBSAMP must match subsampling level of YUV image"); |
| 594 |
compressedSize = compressFromYUV8(srcYUVImage.getPlanes(), |
| 595 |
srcYUVImage.getOffsets(), |
| 596 |
srcYUVImage.getWidth(), |
| 597 |
srcYUVImage.getStrides(), |
| 598 |
srcYUVImage.getHeight(), dstBuf); |
| 599 |
} else if (srcBuf8 != null) |
| 600 |
compressedSize = compress8(srcBuf8, srcX, srcY, srcWidth, srcPitch, |
| 601 |
srcHeight, srcPixelFormat, dstBuf); |
| 602 |
else if (srcBuf12 != null) |
| 603 |
compressedSize = compress12(srcBuf12, srcX, srcY, srcWidth, srcPitch, |
| 604 |
srcHeight, srcPixelFormat, dstBuf); |
| 605 |
else if (srcBuf16 != null) |
| 606 |
compressedSize = compress16(srcBuf16, srcX, srcY, srcWidth, srcPitch, |
| 607 |
srcHeight, srcPixelFormat, dstBuf); |
| 608 |
else if (srcBufInt != null) |
| 609 |
compressedSize = compress8(srcBufInt, srcX, srcY, srcWidth, srcPitch, |
| 610 |
srcHeight, srcPixelFormat, dstBuf); |
| 611 |
else |
| 612 |
throw new IllegalStateException("No source image is associated with this instance"); |
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* Compress the packed-pixel or planar YUV source image associated with this |
| 617 |
* compressor instance and return a buffer containing a JPEG image. |
| 618 |
* |
| 619 |
* @return a buffer containing a JPEG image. The length of this buffer will |
| 620 |
* not be equal to the size of the JPEG image. Use |
| 621 |
* {@link #getCompressedSize} to obtain the size of the JPEG image. |
| 622 |
*/ |
| 623 |
public byte[] compress() throws TJException { |
| 624 |
byte[] buf; |
| 625 |
if (srcYUVImage != null) { |
| 626 |
buf = new byte[TJ.bufSize(srcYUVImage.getWidth(), |
| 627 |
srcYUVImage.getHeight(), |
| 628 |
srcYUVImage.getSubsamp())]; |
| 629 |
} else { |
| 630 |
checkSubsampling(); |
| 631 |
int subsamp = get(TJ.PARAM_SUBSAMP); |
| 632 |
if (get(TJ.PARAM_LOSSLESS) == 1 && subsamp != TJ.SAMP_GRAY) |
| 633 |
subsamp = TJ.SAMP_444; |
| 634 |
buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp) + iccSize]; |
| 635 |
} |
| 636 |
compress(buf); |
| 637 |
return buf; |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Encode the 8-bit-per-sample packed-pixel source image associated with this |
| 642 |
* compressor instance into an 8-bit-per-sample planar YUV image and store it |
| 643 |
* in the given {@link YUVImage} instance. This method performs color |
| 644 |
* conversion and downsampling (which are accelerated in the libjpeg-turbo |
| 645 |
* implementation) but does not execute any of the other steps in the JPEG |
| 646 |
* compression process. Encoding CMYK source images into YUV images is not |
| 647 |
* supported. This method sets {@link TJ#PARAM_SUBSAMP} to the chrominance |
| 648 |
* subsampling level of the destination image. |
| 649 |
* |
| 650 |
* @param dstImage {@link YUVImage} instance that will receive the planar YUV |
| 651 |
* image |
| 652 |
*/ |
| 653 |
public void encodeYUV(YUVImage dstImage) throws TJException { |
| 654 |
if (dstImage == null) |
| 655 |
throw new IllegalArgumentException("Invalid argument in encodeYUV()"); |
| 656 |
if (srcBuf8 == null && srcBufInt == null) |
| 657 |
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance"); |
| 658 |
if (srcYUVImage != null) |
| 659 |
throw new IllegalStateException("Source image is not correct type"); |
| 660 |
if (srcWidth != dstImage.getWidth() || srcHeight != dstImage.getHeight()) |
| 661 |
throw new IllegalStateException("Destination image is the wrong size"); |
| 662 |
set(TJ.PARAM_SUBSAMP, dstImage.getSubsamp()); |
| 663 |
|
| 664 |
if (srcBufInt != null) { |
| 665 |
encodeYUV8(srcBufInt, srcX, srcY, srcWidth, srcPitch, srcHeight, |
| 666 |
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(), |
| 667 |
dstImage.getStrides()); |
| 668 |
} else { |
| 669 |
encodeYUV8(srcBuf8, srcX, srcY, srcWidth, srcPitch, srcHeight, |
| 670 |
srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(), |
| 671 |
dstImage.getStrides()); |
| 672 |
} |
| 673 |
compressedSize = 0; |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Encode the 8-bit-per-sample packed-pixel source image associated with this |
| 678 |
* compressor instance into an 8-bit-per-sample unified planar YUV image and |
| 679 |
* return a {@link YUVImage} instance containing the encoded image. This |
| 680 |
* method performs color conversion and downsampling (which are accelerated |
| 681 |
* in the libjpeg-turbo implementation) but does not execute any of the other |
| 682 |
* steps in the JPEG compression process. Encoding CMYK source images into |
| 683 |
* YUV images is not supported. |
| 684 |
* |
| 685 |
* @param align row alignment (in bytes) of the YUV image (must be a power of |
| 686 |
* 2.) Setting this parameter to n will cause each row in each plane of the |
| 687 |
* YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.) |
| 688 |
* |
| 689 |
* @return a {@link YUVImage} instance containing the unified planar YUV |
| 690 |
* encoded image |
| 691 |
*/ |
| 692 |
public YUVImage encodeYUV(int align) throws TJException { |
| 693 |
if (srcBuf8 == null && srcBufInt == null) |
| 694 |
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance"); |
| 695 |
checkSubsampling(); |
| 696 |
if (align < 1 || ((align & (align - 1)) != 0)) |
| 697 |
throw new IllegalStateException("Invalid argument in encodeYUV()"); |
| 698 |
YUVImage dstYUVImage = new YUVImage(srcWidth, align, srcHeight, |
| 699 |
get(TJ.PARAM_SUBSAMP)); |
| 700 |
encodeYUV(dstYUVImage); |
| 701 |
return dstYUVImage; |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Encode the 8-bit-per-sample packed-pixel source image associated with this |
| 706 |
* compressor instance into separate 8-bit-per-sample Y, U (Cb), and V (Cr) |
| 707 |
* image planes and return a {@link YUVImage} instance containing the encoded |
| 708 |
* image planes. This method performs color conversion and downsampling |
| 709 |
* (which are accelerated in the libjpeg-turbo implementation) but does not |
| 710 |
* execute any of the other steps in the JPEG compression process. Encoding |
| 711 |
* CMYK source images into YUV images is not supported. |
| 712 |
* |
| 713 |
* @param strides an array of integers, each specifying the number of bytes |
| 714 |
* per row in the corresponding plane of the YUV source image. Setting the |
| 715 |
* stride for any plane to 0 is the same as setting it to the plane width |
| 716 |
* (see {@link YUVImage}.) If <code>strides</code> is null, then the strides |
| 717 |
* for all planes will be set to their respective plane widths. You can |
| 718 |
* adjust the strides in order to add an arbitrary amount of row padding to |
| 719 |
* each plane. |
| 720 |
* |
| 721 |
* @return a {@link YUVImage} instance containing the encoded image planes |
| 722 |
*/ |
| 723 |
public YUVImage encodeYUV(int[] strides) throws TJException { |
| 724 |
if (srcBuf8 == null && srcBufInt == null) |
| 725 |
throw new IllegalStateException("No 8-bit-per-sample source image is associated with this instance"); |
| 726 |
checkSubsampling(); |
| 727 |
YUVImage dstYUVImage = new YUVImage(srcWidth, strides, srcHeight, |
| 728 |
get(TJ.PARAM_SUBSAMP)); |
| 729 |
encodeYUV(dstYUVImage); |
| 730 |
return dstYUVImage; |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* Returns the size of the image (in bytes) generated by the most recent |
| 735 |
* compression operation. |
| 736 |
* |
| 737 |
* @return the size of the image (in bytes) generated by the most recent |
| 738 |
* compression operation. |
| 739 |
*/ |
| 740 |
public int getCompressedSize() { |
| 741 |
return compressedSize; |
| 742 |
} |
| 743 |
|
| 744 |
/** |
| 745 |
* Free the native structures associated with this compressor instance. |
| 746 |
*/ |
| 747 |
@Override |
| 748 |
public void close() throws TJException { |
| 749 |
if (handle != 0) |
| 750 |
destroy(); |
| 751 |
} |
| 752 |
|
| 753 |
@SuppressWarnings("checkstyle:DesignForExtension") |
| 754 |
@Override |
| 755 |
protected void finalize() throws Throwable { |
| 756 |
try { |
| 757 |
close(); |
| 758 |
} catch (TJException e) { |
| 759 |
} finally { |
| 760 |
super.finalize(); |
| 761 |
} |
| 762 |
}; |
| 763 |
|
| 764 |
private void checkSubsampling() { |
| 765 |
if (get(TJ.PARAM_SUBSAMP) == TJ.SAMP_UNKNOWN) |
| 766 |
throw new IllegalStateException("TJ.PARAM_SUBSAMP must be specified"); |
| 767 |
} |
| 768 |
|
| 769 |
private native void init() throws TJException; |
| 770 |
|
| 771 |
private native void destroy() throws TJException; |
| 772 |
|
| 773 |
// JPEG size in bytes is returned |
| 774 |
@SuppressWarnings("checkstyle:HiddenField") |
| 775 |
private native int compress8(byte[] srcBuf, int x, int y, int width, |
| 776 |
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException; |
| 777 |
|
| 778 |
@SuppressWarnings("checkstyle:HiddenField") |
| 779 |
private native int compress12(short[] srcBuf, int x, int y, int width, |
| 780 |
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException; |
| 781 |
|
| 782 |
@SuppressWarnings("checkstyle:HiddenField") |
| 783 |
private native int compress16(short[] srcBuf, int x, int y, int width, |
| 784 |
int pitch, int height, int pixelFormat, byte[] jpegBuf) throws TJException; |
| 785 |
|
| 786 |
@SuppressWarnings("checkstyle:HiddenField") |
| 787 |
private native int compress8(int[] srcBuf, int x, int y, int width, |
| 788 |
int stride, int height, int pixelFormat, byte[] jpegBuf) |
| 789 |
throws TJException; |
| 790 |
|
| 791 |
@SuppressWarnings("checkstyle:HiddenField") |
| 792 |
private native int compressFromYUV8(byte[][] srcPlanes, int[] srcOffsets, |
| 793 |
int width, int[] srcStrides, int height, byte[] jpegBuf) |
| 794 |
throws TJException; |
| 795 |
|
| 796 |
@SuppressWarnings("checkstyle:HiddenField") |
| 797 |
private native void encodeYUV8(byte[] srcBuf, int x, int y, int width, |
| 798 |
int pitch, int height, int pixelFormat, byte[][] dstPlanes, |
| 799 |
int[] dstOffsets, int[] dstStrides) throws TJException; |
| 800 |
|
| 801 |
@SuppressWarnings("checkstyle:HiddenField") |
| 802 |
private native void encodeYUV8(int[] srcBuf, int x, int y, int width, |
| 803 |
int srcStride, int height, int pixelFormat, byte[][] dstPlanes, |
| 804 |
int[] dstOffsets, int[] dstStrides) throws TJException; |
| 805 |
|
| 806 |
private native Object loadSourceImage(int precision, String fileName, |
| 807 |
int align, int pixelFormat) throws TJException; |
| 808 |
|
| 809 |
static { |
| 810 |
TJLoader.load(); |
| 811 |
} |
| 812 |
|
| 813 |
private long handle = 0; |
| 814 |
private byte[] srcBuf8 = null; |
| 815 |
private short[] srcBuf12 = null; |
| 816 |
private short[] srcBuf16 = null; |
| 817 |
private int[] srcBufInt = null; |
| 818 |
private int srcWidth = 0; |
| 819 |
private int srcHeight = 0; |
| 820 |
private int srcX = -1; |
| 821 |
private int srcY = -1; |
| 822 |
private int srcPitch = -1; |
| 823 |
private int srcPixelFormat = TJ.PF_UNKNOWN; |
| 824 |
private YUVImage srcYUVImage = null; |
| 825 |
private int compressedSize = 0; |
| 826 |
private ByteOrder byteOrder = null; |
| 827 |
private int iccSize = 0; |
| 828 |
} |