TJCompressor.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (C)2011-2015 D. R. Commander. All Rights Reserved.
  3. * Copyright (C)2015 Viktor Szathmáry. All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * - Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * - Neither the name of the libjpeg-turbo Project nor the names of its
  14. * contributors may be used to endorse or promote products derived from this
  15. * software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. package org.libjpegturbo.turbojpeg;
  30. import java.awt.image.*;
  31. import java.nio.*;
  32. import java.io.*;
  33. /**
  34. * TurboJPEG compressor
  35. */
  36. public class TJCompressor implements Closeable {
  37. private static final String NO_ASSOC_ERROR =
  38. "No source image is associated with this instance";
  39. /**
  40. * Create a TurboJPEG compressor instance.
  41. */
  42. public TJCompressor() throws TJException {
  43. init();
  44. }
  45. /**
  46. * Create a TurboJPEG compressor instance and associate the uncompressed
  47. * source image stored in <code>srcImage</code> with the newly created
  48. * instance.
  49. *
  50. * @param srcImage see {@link #setSourceImage} for description
  51. *
  52. * @param x see {@link #setSourceImage} for description
  53. *
  54. * @param y 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 pixel format of the source image (one of
  63. * {@link TJ#PF_RGB TJ.PF_*})
  64. */
  65. public TJCompressor(byte[] srcImage, int x, int y, int width, int pitch,
  66. int height, int pixelFormat) throws TJException {
  67. setSourceImage(srcImage, x, y, width, pitch, height, pixelFormat);
  68. }
  69. /**
  70. * @deprecated Use
  71. * {@link #TJCompressor(byte[], int, int, int, int, int, int)} instead.
  72. */
  73. @Deprecated
  74. public TJCompressor(byte[] srcImage, int width, int pitch, int height,
  75. int pixelFormat) throws TJException {
  76. setSourceImage(srcImage, width, pitch, height, pixelFormat);
  77. }
  78. /**
  79. * Create a TurboJPEG compressor instance and associate the uncompressed
  80. * source image stored in <code>srcImage</code> with the newly created
  81. * instance.
  82. *
  83. * @param srcImage see
  84. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  85. *
  86. * @param x see
  87. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  88. *
  89. * @param y see
  90. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  91. *
  92. * @param width see
  93. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  94. *
  95. * @param height see
  96. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  97. */
  98. public TJCompressor(BufferedImage srcImage, int x, int y, int width,
  99. int height) throws TJException {
  100. setSourceImage(srcImage, x, y, width, height);
  101. }
  102. /**
  103. * Associate an uncompressed RGB, grayscale, or CMYK source image with this
  104. * compressor instance.
  105. *
  106. * @param srcImage image buffer containing RGB, grayscale, or CMYK pixels to
  107. * be compressed or encoded. This buffer is not modified.
  108. *
  109. * @param x x offset (in pixels) of the region in the source image from which
  110. * the JPEG or YUV image should be compressed/encoded
  111. *
  112. * @param y y offset (in pixels) of the region in the source image from which
  113. * the JPEG or YUV image should be compressed/encoded
  114. *
  115. * @param width width (in pixels) of the region in the source image from
  116. * which the JPEG or YUV image should be compressed/encoded
  117. *
  118. * @param pitch bytes per line of the source image. Normally, this should be
  119. * <code>width * TJ.pixelSize(pixelFormat)</code> if the source image is
  120. * unpadded, but you can use this parameter to, for instance, specify that
  121. * the scanlines in the source image are padded to a 4-byte boundary or to
  122. * compress/encode a JPEG or YUV image from a region of a larger source
  123. * image. You can also be clever and use this parameter to skip lines, etc.
  124. * Setting this parameter to 0 is the equivalent of setting it to
  125. * <code>width * TJ.pixelSize(pixelFormat)</code>.
  126. *
  127. * @param height height (in pixels) of the region in the source image from
  128. * which the JPEG or YUV image should be compressed/encoded
  129. *
  130. * @param pixelFormat pixel format of the source image (one of
  131. * {@link TJ#PF_RGB TJ.PF_*})
  132. */
  133. public void setSourceImage(byte[] srcImage, int x, int y, int width,
  134. int pitch, int height, int pixelFormat)
  135. throws TJException {
  136. if (handle == 0) init();
  137. if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
  138. pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
  139. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  140. srcBuf = srcImage;
  141. srcWidth = width;
  142. if (pitch == 0)
  143. srcPitch = width * TJ.getPixelSize(pixelFormat);
  144. else
  145. srcPitch = pitch;
  146. srcHeight = height;
  147. srcPixelFormat = pixelFormat;
  148. srcX = x;
  149. srcY = y;
  150. srcBufInt = null;
  151. srcYUVImage = null;
  152. }
  153. /**
  154. * @deprecated Use
  155. * {@link #setSourceImage(byte[], int, int, int, int, int, int)} instead.
  156. */
  157. @Deprecated
  158. public void setSourceImage(byte[] srcImage, int width, int pitch,
  159. int height, int pixelFormat) throws TJException {
  160. setSourceImage(srcImage, 0, 0, width, pitch, height, pixelFormat);
  161. srcX = srcY = -1;
  162. }
  163. /**
  164. * Associate an uncompressed RGB or grayscale source image with this
  165. * compressor instance.
  166. *
  167. * @param srcImage a <code>BufferedImage</code> instance containing RGB or
  168. * grayscale pixels to be compressed or encoded. This image is not modified.
  169. *
  170. * @param x x offset (in pixels) of the region in the source image from which
  171. * the JPEG or YUV image should be compressed/encoded
  172. *
  173. * @param y y offset (in pixels) of the region in the source image from which
  174. * the JPEG or YUV image should be compressed/encoded
  175. *
  176. * @param width width (in pixels) of the region in the source image from
  177. * which the JPEG or YUV image should be compressed/encoded (0 = use the
  178. * width of the source image)
  179. *
  180. * @param height height (in pixels) of the region in the source image from
  181. * which the JPEG or YUV image should be compressed/encoded (0 = use the
  182. * height of the source image)
  183. */
  184. public void setSourceImage(BufferedImage srcImage, int x, int y, int width,
  185. int height) throws TJException {
  186. if (handle == 0) init();
  187. if (srcImage == null || x < 0 || y < 0 || width < 0 || height < 0)
  188. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  189. srcX = x;
  190. srcY = y;
  191. srcWidth = (width == 0) ? srcImage.getWidth(): width;
  192. srcHeight = (height == 0) ? srcImage.getHeight() : height;
  193. if (x + width > srcImage.getWidth() || y + height > srcImage.getHeight())
  194. throw new IllegalArgumentException("Compression region exceeds the bounds of the source image");
  195. int pixelFormat;
  196. boolean intPixels = false;
  197. if (byteOrder == null)
  198. byteOrder = ByteOrder.nativeOrder();
  199. switch(srcImage.getType()) {
  200. case BufferedImage.TYPE_3BYTE_BGR:
  201. pixelFormat = TJ.PF_BGR; break;
  202. case BufferedImage.TYPE_4BYTE_ABGR:
  203. case BufferedImage.TYPE_4BYTE_ABGR_PRE:
  204. pixelFormat = TJ.PF_XBGR; break;
  205. case BufferedImage.TYPE_BYTE_GRAY:
  206. pixelFormat = TJ.PF_GRAY; break;
  207. case BufferedImage.TYPE_INT_BGR:
  208. if (byteOrder == ByteOrder.BIG_ENDIAN)
  209. pixelFormat = TJ.PF_XBGR;
  210. else
  211. pixelFormat = TJ.PF_RGBX;
  212. intPixels = true; break;
  213. case BufferedImage.TYPE_INT_RGB:
  214. case BufferedImage.TYPE_INT_ARGB:
  215. case BufferedImage.TYPE_INT_ARGB_PRE:
  216. if (byteOrder == ByteOrder.BIG_ENDIAN)
  217. pixelFormat = TJ.PF_XRGB;
  218. else
  219. pixelFormat = TJ.PF_BGRX;
  220. intPixels = true; break;
  221. default:
  222. throw new IllegalArgumentException("Unsupported BufferedImage format");
  223. }
  224. srcPixelFormat = pixelFormat;
  225. WritableRaster wr = srcImage.getRaster();
  226. if (intPixels) {
  227. SinglePixelPackedSampleModel sm =
  228. (SinglePixelPackedSampleModel)srcImage.getSampleModel();
  229. srcStride = sm.getScanlineStride();
  230. DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
  231. srcBufInt = db.getData();
  232. srcBuf = null;
  233. } else {
  234. ComponentSampleModel sm =
  235. (ComponentSampleModel)srcImage.getSampleModel();
  236. int pixelSize = sm.getPixelStride();
  237. if (pixelSize != TJ.getPixelSize(pixelFormat))
  238. throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage");
  239. srcPitch = sm.getScanlineStride();
  240. DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
  241. srcBuf = db.getData();
  242. srcBufInt = null;
  243. }
  244. srcYUVImage = null;
  245. }
  246. /**
  247. * Associate an uncompressed YUV planar source image with this compressor
  248. * instance.
  249. *
  250. * @param srcImage YUV planar image to be compressed. This image is not
  251. * modified.
  252. */
  253. public void setSourceImage(YUVImage srcImage) throws TJException {
  254. if (handle == 0) init();
  255. if (srcImage == null)
  256. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  257. srcYUVImage = srcImage;
  258. srcBuf = null;
  259. srcBufInt = null;
  260. }
  261. /**
  262. * Set the level of chrominance subsampling for subsequent compress/encode
  263. * operations. When pixels are converted from RGB to YCbCr (see
  264. * {@link TJ#CS_YCbCr}) or from CMYK to YCCK (see {@link TJ#CS_YCCK}) as part
  265. * of the JPEG compression process, some of the Cb and Cr (chrominance)
  266. * components can be discarded or averaged together to produce a smaller
  267. * image with little perceptible loss of image clarity (the human eye is more
  268. * sensitive to small changes in brightness than to small changes in color.)
  269. * This is called "chrominance subsampling".
  270. * <p>
  271. * NOTE: This method has no effect when compressing a JPEG image from a YUV
  272. * planar source. In that case, the level of chrominance subsampling in
  273. * the JPEG image is determined by the source. Further, this method has no
  274. * effect when encoding to a pre-allocated {@link YUVImage} instance. In
  275. * that case, the level of chrominance subsampling is determined by the
  276. * destination.
  277. *
  278. * @param newSubsamp the level of chrominance subsampling to use in
  279. * subsequent compress/encode oeprations (one of
  280. * {@link TJ#SAMP_444 TJ.SAMP_*})
  281. */
  282. public void setSubsamp(int newSubsamp) {
  283. if (newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
  284. throw new IllegalArgumentException("Invalid argument in setSubsamp()");
  285. subsamp = newSubsamp;
  286. }
  287. /**
  288. * Set the JPEG image quality level for subsequent compress operations.
  289. *
  290. * @param quality the new JPEG image quality level (1 to 100, 1 = worst,
  291. * 100 = best)
  292. */
  293. public void setJPEGQuality(int quality) {
  294. if (quality < 1 || quality > 100)
  295. throw new IllegalArgumentException("Invalid argument in setJPEGQuality()");
  296. jpegQuality = quality;
  297. }
  298. /**
  299. * Compress the uncompressed source image associated with this compressor
  300. * instance and output a JPEG image to the given destination buffer.
  301. *
  302. * @param dstBuf buffer that will receive the JPEG image. Use
  303. * {@link TJ#bufSize} to determine the maximum size for this buffer based on
  304. * the source image's width and height and the desired level of chrominance
  305. * subsampling.
  306. *
  307. * @param flags the bitwise OR of one or more of
  308. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  309. */
  310. public void compress(byte[] dstBuf, int flags) throws TJException {
  311. if (dstBuf == null || flags < 0)
  312. throw new IllegalArgumentException("Invalid argument in compress()");
  313. if (srcBuf == null && srcBufInt == null && srcYUVImage == null)
  314. throw new IllegalStateException(NO_ASSOC_ERROR);
  315. if (jpegQuality < 0)
  316. throw new IllegalStateException("JPEG Quality not set");
  317. if (subsamp < 0 && srcYUVImage == null)
  318. throw new IllegalStateException("Subsampling level not set");
  319. if (srcYUVImage != null)
  320. compressedSize = compressFromYUV(srcYUVImage.getPlanes(),
  321. srcYUVImage.getOffsets(),
  322. srcYUVImage.getWidth(),
  323. srcYUVImage.getStrides(),
  324. srcYUVImage.getHeight(),
  325. srcYUVImage.getSubsamp(),
  326. dstBuf, jpegQuality, flags);
  327. else if (srcBuf != null) {
  328. if (srcX >= 0 && srcY >= 0)
  329. compressedSize = compress(srcBuf, srcX, srcY, srcWidth, srcPitch,
  330. srcHeight, srcPixelFormat, dstBuf, subsamp,
  331. jpegQuality, flags);
  332. else
  333. compressedSize = compress(srcBuf, srcWidth, srcPitch, srcHeight,
  334. srcPixelFormat, dstBuf, subsamp, jpegQuality,
  335. flags);
  336. } else if (srcBufInt != null) {
  337. if (srcX >= 0 && srcY >= 0)
  338. compressedSize = compress(srcBufInt, srcX, srcY, srcWidth, srcStride,
  339. srcHeight, srcPixelFormat, dstBuf, subsamp,
  340. jpegQuality, flags);
  341. else
  342. compressedSize = compress(srcBufInt, srcWidth, srcStride, srcHeight,
  343. srcPixelFormat, dstBuf, subsamp, jpegQuality,
  344. flags);
  345. }
  346. }
  347. /**
  348. * Compress the uncompressed source image associated with this compressor
  349. * instance and return a buffer containing a JPEG image.
  350. *
  351. * @param flags the bitwise OR of one or more of
  352. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  353. *
  354. * @return a buffer containing a JPEG image. The length of this buffer will
  355. * not be equal to the size of the JPEG image. Use {@link
  356. * #getCompressedSize} to obtain the size of the JPEG image.
  357. */
  358. public byte[] compress(int flags) throws TJException {
  359. checkSourceImage();
  360. byte[] buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)];
  361. compress(buf, flags);
  362. return buf;
  363. }
  364. /**
  365. * @deprecated Use
  366. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  367. * {@link #compress(byte[], int)} instead.
  368. */
  369. @Deprecated
  370. public void compress(BufferedImage srcImage, byte[] dstBuf, int flags)
  371. throws TJException {
  372. setSourceImage(srcImage, 0, 0, 0, 0);
  373. compress(dstBuf, flags);
  374. }
  375. /**
  376. * @deprecated Use
  377. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  378. * {@link #compress(int)} instead.
  379. */
  380. @Deprecated
  381. public byte[] compress(BufferedImage srcImage, int flags)
  382. throws TJException {
  383. setSourceImage(srcImage, 0, 0, 0, 0);
  384. return compress(flags);
  385. }
  386. /**
  387. * Encode the uncompressed source image associated with this compressor
  388. * instance into a YUV planar image and store it in the given
  389. * <code>YUVImage</code> instance. This method uses the accelerated color
  390. * conversion routines in TurboJPEG's underlying codec but does not execute
  391. * any of the other steps in the JPEG compression process. Encoding
  392. * CMYK source images to YUV is not supported.
  393. *
  394. * @param dstImage {@link YUVImage} instance that will receive the YUV planar
  395. * image
  396. *
  397. * @param flags the bitwise OR of one or more of
  398. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  399. */
  400. public void encodeYUV(YUVImage dstImage, int flags) throws TJException {
  401. if (dstImage == null || flags < 0)
  402. throw new IllegalArgumentException("Invalid argument in encodeYUV()");
  403. if (srcBuf == null && srcBufInt == null)
  404. throw new IllegalStateException(NO_ASSOC_ERROR);
  405. if (srcYUVImage != null)
  406. throw new IllegalStateException("Source image is not correct type");
  407. checkSubsampling();
  408. if (srcWidth != dstImage.getWidth() || srcHeight != dstImage.getHeight())
  409. throw new IllegalStateException("Destination image is the wrong size");
  410. if (srcBufInt != null) {
  411. encodeYUV(srcBufInt, srcX, srcY, srcWidth, srcStride, srcHeight,
  412. srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
  413. dstImage.getStrides(), dstImage.getSubsamp(), flags);
  414. } else {
  415. encodeYUV(srcBuf, srcX, srcY, srcWidth, srcPitch, srcHeight,
  416. srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
  417. dstImage.getStrides(), dstImage.getSubsamp(), flags);
  418. }
  419. compressedSize = 0;
  420. }
  421. /**
  422. * @deprecated Use {@link #encodeYUV(YUVImage, int)} instead.
  423. */
  424. @Deprecated
  425. public void encodeYUV(byte[] dstBuf, int flags) throws TJException {
  426. if(dstBuf == null)
  427. throw new IllegalArgumentException("Invalid argument in encodeYUV()");
  428. checkSourceImage();
  429. checkSubsampling();
  430. YUVImage yuvImage = new YUVImage(dstBuf, srcWidth, 4, srcHeight, subsamp);
  431. encodeYUV(yuvImage, flags);
  432. }
  433. /**
  434. * Encode the uncompressed source image associated with this compressor
  435. * instance into a unified YUV planar image buffer and return a
  436. * <code>YUVImage</code> instance containing the encoded image. This method
  437. * uses the accelerated color conversion routines in TurboJPEG's underlying
  438. * codec but does not execute any of the other steps in the JPEG compression
  439. * process. Encoding CMYK source images to YUV is not supported.
  440. *
  441. * @param pad the width of each line in each plane of the YUV image will be
  442. * padded to the nearest multiple of this number of bytes (must be a power of
  443. * 2.)
  444. *
  445. * @param flags the bitwise OR of one or more of
  446. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  447. *
  448. * @return a YUV planar image.
  449. */
  450. public YUVImage encodeYUV(int pad, int flags) throws TJException {
  451. checkSourceImage();
  452. checkSubsampling();
  453. if(pad < 1 || ((pad & (pad - 1)) != 0))
  454. throw new IllegalStateException("Invalid argument in encodeYUV()");
  455. YUVImage yuvImage = new YUVImage(srcWidth, pad, srcHeight, subsamp);
  456. encodeYUV(yuvImage, flags);
  457. return yuvImage;
  458. }
  459. /**
  460. * Encode the uncompressed source image associated with this compressor
  461. * instance into separate Y, U (Cb), and V (Cr) image planes and return a
  462. * <code>YUVImage</code> instance containing the encoded image planes. This
  463. * method uses the accelerated color conversion routines in TurboJPEG's
  464. * underlying codec but does not execute any of the other steps in the JPEG
  465. * compression process. Encoding CMYK source images to YUV is not supported.
  466. *
  467. * @param strides an array of integers, each specifying the number of bytes
  468. * per line in the corresponding plane of the output image. Setting the
  469. * stride for any plane to 0 is the same as setting it to the component width
  470. * of the plane. If <code>strides</code> is null, then the strides for all
  471. * planes will be set to their respective component widths. You can adjust
  472. * the strides in order to add an arbitrary amount of line padding to each
  473. * plane.
  474. *
  475. * @param flags the bitwise OR of one or more of
  476. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  477. *
  478. * @return a YUV planar image.
  479. */
  480. public YUVImage encodeYUV(int[] strides, int flags) throws TJException {
  481. checkSourceImage();
  482. checkSubsampling();
  483. YUVImage yuvImage = new YUVImage(srcWidth, strides, srcHeight, subsamp);
  484. encodeYUV(yuvImage, flags);
  485. return yuvImage;
  486. }
  487. /**
  488. * @deprecated Use {@link #encodeYUV(int, int)} instead.
  489. */
  490. @Deprecated
  491. public byte[] encodeYUV(int flags) throws TJException {
  492. checkSourceImage();
  493. checkSubsampling();
  494. YUVImage yuvImage = new YUVImage(srcWidth, 4, srcHeight, subsamp);
  495. encodeYUV(yuvImage, flags);
  496. return yuvImage.getBuf();
  497. }
  498. /**
  499. * @deprecated Use
  500. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  501. * {@link #encodeYUV(byte[], int)} instead.
  502. */
  503. @Deprecated
  504. public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags)
  505. throws TJException {
  506. setSourceImage(srcImage, 0, 0, 0, 0);
  507. encodeYUV(dstBuf, flags);
  508. }
  509. /**
  510. * @deprecated Use
  511. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  512. * {@link #encodeYUV(int, int)} instead.
  513. */
  514. @Deprecated
  515. public byte[] encodeYUV(BufferedImage srcImage, int flags)
  516. throws TJException {
  517. setSourceImage(srcImage, 0, 0, 0, 0);
  518. return encodeYUV(flags);
  519. }
  520. /**
  521. * Returns the size of the image (in bytes) generated by the most recent
  522. * compress operation.
  523. *
  524. * @return the size of the image (in bytes) generated by the most recent
  525. * compress operation.
  526. */
  527. public int getCompressedSize() {
  528. return compressedSize;
  529. }
  530. /**
  531. * Free the native structures associated with this compressor instance.
  532. */
  533. @Override
  534. public void close() throws TJException {
  535. if (handle != 0)
  536. destroy();
  537. }
  538. @Override
  539. protected void finalize() throws Throwable {
  540. try {
  541. close();
  542. } catch(TJException e) {
  543. } finally {
  544. super.finalize();
  545. }
  546. };
  547. private native void init() throws TJException;
  548. private native void destroy() throws TJException;
  549. // JPEG size in bytes is returned
  550. @Deprecated
  551. private native int compress(byte[] srcBuf, int width, int pitch,
  552. int height, int pixelFormat, byte[] dstBuf, int jpegSubsamp, int jpegQual,
  553. int flags) throws TJException;
  554. private native int compress(byte[] srcBuf, int x, int y, int width,
  555. int pitch, int height, int pixelFormat, byte[] dstBuf, int jpegSubsamp,
  556. int jpegQual, int flags) throws TJException;
  557. @Deprecated
  558. private native int compress(int[] srcBuf, int width, int stride,
  559. int height, int pixelFormat, byte[] dstBuf, int jpegSubsamp, int jpegQual,
  560. int flags) throws TJException;
  561. private native int compress(int[] srcBuf, int x, int y, int width,
  562. int stride, int height, int pixelFormat, byte[] dstBuf, int jpegSubsamp,
  563. int jpegQual, int flags) throws TJException;
  564. private native int compressFromYUV(byte[][] srcPlanes, int[] srcOffsets,
  565. int width, int[] srcStrides, int height, int subsamp, byte[] dstBuf,
  566. int jpegQual, int flags)
  567. throws TJException;
  568. @Deprecated
  569. private native void encodeYUV(byte[] srcBuf, int width, int pitch,
  570. int height, int pixelFormat, byte[] dstBuf, int subsamp, int flags)
  571. throws TJException;
  572. private native void encodeYUV(byte[] srcBuf, int x, int y, int width,
  573. int pitch, int height, int pixelFormat, byte[][] dstPlanes,
  574. int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
  575. throws TJException;
  576. @Deprecated
  577. private native void encodeYUV(int[] srcBuf, int width, int stride,
  578. int height, int pixelFormat, byte[] dstBuf, int subsamp, int flags)
  579. throws TJException;
  580. private native void encodeYUV(int[] srcBuf, int x, int y, int width,
  581. int srcStride, int height, int pixelFormat, byte[][] dstPlanes,
  582. int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
  583. throws TJException;
  584. static {
  585. TJLoader.load();
  586. }
  587. private void checkSourceImage() {
  588. if (srcWidth < 1 || srcHeight < 1)
  589. throw new IllegalStateException(NO_ASSOC_ERROR);
  590. }
  591. private void checkSubsampling() {
  592. if (subsamp < 0)
  593. throw new IllegalStateException("Subsampling level not set");
  594. }
  595. private long handle = 0;
  596. private byte[] srcBuf = null;
  597. private int[] srcBufInt = null;
  598. private int srcWidth = 0;
  599. private int srcHeight = 0;
  600. private int srcX = -1;
  601. private int srcY = -1;
  602. private int srcPitch = 0;
  603. private int srcStride = 0;
  604. private int srcPixelFormat = -1;
  605. private YUVImage srcYUVImage = null;
  606. private int subsamp = -1;
  607. private int jpegQuality = -1;
  608. private int compressedSize = 0;
  609. private int yuvPad = 4;
  610. private ByteOrder byteOrder = null;
  611. }