TJCompressor.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (C)2011-2015, 2018, 2020 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. @SuppressWarnings("checkstyle:JavadocMethod")
  74. @Deprecated
  75. public TJCompressor(byte[] srcImage, int width, int pitch, int height,
  76. int pixelFormat) throws TJException {
  77. setSourceImage(srcImage, width, pitch, height, pixelFormat);
  78. }
  79. /**
  80. * Create a TurboJPEG compressor instance and associate the uncompressed
  81. * source image stored in <code>srcImage</code> with the newly created
  82. * instance.
  83. *
  84. * @param srcImage see
  85. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  86. *
  87. * @param x see
  88. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  89. *
  90. * @param y see
  91. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  92. *
  93. * @param width see
  94. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  95. *
  96. * @param height see
  97. * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description
  98. */
  99. public TJCompressor(BufferedImage srcImage, int x, int y, int width,
  100. int height) throws TJException {
  101. setSourceImage(srcImage, x, y, width, height);
  102. }
  103. /**
  104. * Associate an uncompressed RGB, grayscale, or CMYK source image with this
  105. * compressor instance.
  106. *
  107. * @param srcImage image buffer containing RGB, grayscale, or CMYK pixels to
  108. * be compressed or encoded. This buffer is not modified.
  109. *
  110. * @param x x offset (in pixels) of the region in the source image from which
  111. * the JPEG or YUV image should be compressed/encoded
  112. *
  113. * @param y y offset (in pixels) of the region in the source image from which
  114. * the JPEG or YUV image should be compressed/encoded
  115. *
  116. * @param width width (in pixels) of the region in the source image from
  117. * which the JPEG or YUV image should be compressed/encoded
  118. *
  119. * @param pitch bytes per line of the source image. Normally, this should be
  120. * <code>width * TJ.pixelSize(pixelFormat)</code> if the source image is
  121. * unpadded, but you can use this parameter to, for instance, specify that
  122. * the scanlines in the source image are padded to a 4-byte boundary or to
  123. * compress/encode a JPEG or YUV image from a region of a larger source
  124. * image. You can also be clever and use this parameter to skip lines, etc.
  125. * Setting this parameter to 0 is the equivalent of setting it to
  126. * <code>width * TJ.pixelSize(pixelFormat)</code>.
  127. *
  128. * @param height height (in pixels) of the region in the source image from
  129. * which the JPEG or YUV image should be compressed/encoded
  130. *
  131. * @param pixelFormat pixel format of the source image (one of
  132. * {@link TJ#PF_RGB TJ.PF_*})
  133. */
  134. public void setSourceImage(byte[] srcImage, int x, int y, int width,
  135. int pitch, int height, int pixelFormat)
  136. throws TJException {
  137. if (handle == 0) init();
  138. if (srcImage == null || x < 0 || y < 0 || width < 1 || height < 1 ||
  139. pitch < 0 || pixelFormat < 0 || pixelFormat >= TJ.NUMPF)
  140. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  141. srcBuf = srcImage;
  142. srcWidth = width;
  143. if (pitch == 0)
  144. srcPitch = width * TJ.getPixelSize(pixelFormat);
  145. else
  146. srcPitch = pitch;
  147. srcHeight = height;
  148. srcPixelFormat = pixelFormat;
  149. srcX = x;
  150. srcY = y;
  151. srcBufInt = null;
  152. srcYUVImage = null;
  153. }
  154. /**
  155. * @deprecated Use
  156. * {@link #setSourceImage(byte[], int, int, int, int, int, int)} instead.
  157. */
  158. @SuppressWarnings("checkstyle:JavadocMethod")
  159. @Deprecated
  160. public void setSourceImage(byte[] srcImage, int width, int pitch,
  161. int height, int pixelFormat) throws TJException {
  162. setSourceImage(srcImage, 0, 0, width, pitch, height, pixelFormat);
  163. srcX = srcY = -1;
  164. }
  165. /**
  166. * Associate an uncompressed RGB or grayscale source image with this
  167. * compressor instance.
  168. *
  169. * @param srcImage a <code>BufferedImage</code> instance containing RGB or
  170. * grayscale pixels to be compressed or encoded. This image is not modified.
  171. *
  172. * @param x x offset (in pixels) of the region in the source image from which
  173. * the JPEG or YUV image should be compressed/encoded
  174. *
  175. * @param y y offset (in pixels) of the region in the source image from which
  176. * the JPEG or YUV image should be compressed/encoded
  177. *
  178. * @param width width (in pixels) of the region in the source image from
  179. * which the JPEG or YUV image should be compressed/encoded (0 = use the
  180. * width of the source image)
  181. *
  182. * @param height height (in pixels) of the region in the source image from
  183. * which the JPEG or YUV image should be compressed/encoded (0 = use the
  184. * height of the source image)
  185. */
  186. public void setSourceImage(BufferedImage srcImage, int x, int y, int width,
  187. int height) throws TJException {
  188. if (handle == 0) init();
  189. if (srcImage == null || x < 0 || y < 0 || width < 0 || height < 0)
  190. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  191. srcX = x;
  192. srcY = y;
  193. srcWidth = (width == 0) ? srcImage.getWidth() : width;
  194. srcHeight = (height == 0) ? srcImage.getHeight() : height;
  195. if (x + width > srcImage.getWidth() || y + height > srcImage.getHeight())
  196. throw new IllegalArgumentException("Compression region exceeds the bounds of the source image");
  197. int pixelFormat;
  198. boolean intPixels = false;
  199. if (byteOrder == null)
  200. byteOrder = ByteOrder.nativeOrder();
  201. switch (srcImage.getType()) {
  202. case BufferedImage.TYPE_3BYTE_BGR:
  203. pixelFormat = TJ.PF_BGR; break;
  204. case BufferedImage.TYPE_4BYTE_ABGR:
  205. case BufferedImage.TYPE_4BYTE_ABGR_PRE:
  206. pixelFormat = TJ.PF_XBGR; break;
  207. case BufferedImage.TYPE_BYTE_GRAY:
  208. pixelFormat = TJ.PF_GRAY; break;
  209. case BufferedImage.TYPE_INT_BGR:
  210. if (byteOrder == ByteOrder.BIG_ENDIAN)
  211. pixelFormat = TJ.PF_XBGR;
  212. else
  213. pixelFormat = TJ.PF_RGBX;
  214. intPixels = true; break;
  215. case BufferedImage.TYPE_INT_RGB:
  216. case BufferedImage.TYPE_INT_ARGB:
  217. case BufferedImage.TYPE_INT_ARGB_PRE:
  218. if (byteOrder == ByteOrder.BIG_ENDIAN)
  219. pixelFormat = TJ.PF_XRGB;
  220. else
  221. pixelFormat = TJ.PF_BGRX;
  222. intPixels = true; break;
  223. default:
  224. throw new IllegalArgumentException("Unsupported BufferedImage format");
  225. }
  226. srcPixelFormat = pixelFormat;
  227. WritableRaster wr = srcImage.getRaster();
  228. if (intPixels) {
  229. SinglePixelPackedSampleModel sm =
  230. (SinglePixelPackedSampleModel)srcImage.getSampleModel();
  231. srcStride = sm.getScanlineStride();
  232. DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
  233. srcBufInt = db.getData();
  234. srcBuf = null;
  235. } else {
  236. ComponentSampleModel sm =
  237. (ComponentSampleModel)srcImage.getSampleModel();
  238. int pixelSize = sm.getPixelStride();
  239. if (pixelSize != TJ.getPixelSize(pixelFormat))
  240. throw new IllegalArgumentException("Inconsistency between pixel format and pixel size in BufferedImage");
  241. srcPitch = sm.getScanlineStride();
  242. DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
  243. srcBuf = db.getData();
  244. srcBufInt = null;
  245. }
  246. srcYUVImage = null;
  247. }
  248. /**
  249. * Associate an uncompressed YUV planar source image with this compressor
  250. * instance.
  251. *
  252. * @param srcImage YUV planar image to be compressed. This image is not
  253. * modified.
  254. */
  255. public void setSourceImage(YUVImage srcImage) throws TJException {
  256. if (handle == 0) init();
  257. if (srcImage == null)
  258. throw new IllegalArgumentException("Invalid argument in setSourceImage()");
  259. srcYUVImage = srcImage;
  260. srcBuf = null;
  261. srcBufInt = null;
  262. }
  263. /**
  264. * Set the level of chrominance subsampling for subsequent compress/encode
  265. * operations. When pixels are converted from RGB to YCbCr (see
  266. * {@link TJ#CS_YCbCr}) or from CMYK to YCCK (see {@link TJ#CS_YCCK}) as part
  267. * of the JPEG compression process, some of the Cb and Cr (chrominance)
  268. * components can be discarded or averaged together to produce a smaller
  269. * image with little perceptible loss of image clarity (the human eye is more
  270. * sensitive to small changes in brightness than to small changes in color.)
  271. * This is called "chrominance subsampling".
  272. * <p>
  273. * NOTE: This method has no effect when compressing a JPEG image from a YUV
  274. * planar source. In that case, the level of chrominance subsampling in
  275. * the JPEG image is determined by the source. Furthermore, this method has
  276. * no effect when encoding to a pre-allocated {@link YUVImage} instance. In
  277. * that case, the level of chrominance subsampling is determined by the
  278. * destination.
  279. *
  280. * @param newSubsamp the level of chrominance subsampling to use in
  281. * subsequent compress/encode oeprations (one of
  282. * {@link TJ#SAMP_444 TJ.SAMP_*})
  283. */
  284. public void setSubsamp(int newSubsamp) {
  285. if (newSubsamp < 0 || newSubsamp >= TJ.NUMSAMP)
  286. throw new IllegalArgumentException("Invalid argument in setSubsamp()");
  287. subsamp = newSubsamp;
  288. }
  289. /**
  290. * Set the JPEG image quality level for subsequent compress operations.
  291. *
  292. * @param quality the new JPEG image quality level (1 to 100, 1 = worst,
  293. * 100 = best)
  294. */
  295. public void setJPEGQuality(int quality) {
  296. if (quality < 1 || quality > 100)
  297. throw new IllegalArgumentException("Invalid argument in setJPEGQuality()");
  298. jpegQuality = quality;
  299. }
  300. /**
  301. * Compress the uncompressed source image associated with this compressor
  302. * instance and output a JPEG image to the given destination buffer.
  303. *
  304. * @param dstBuf buffer that will receive the JPEG image. Use
  305. * {@link TJ#bufSize} to determine the maximum size for this buffer based on
  306. * the source image's width and height and the desired level of chrominance
  307. * subsampling.
  308. *
  309. * @param flags the bitwise OR of one or more of
  310. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  311. */
  312. public void compress(byte[] dstBuf, int flags) throws TJException {
  313. if (dstBuf == null || flags < 0)
  314. throw new IllegalArgumentException("Invalid argument in compress()");
  315. if (srcBuf == null && srcBufInt == null && srcYUVImage == null)
  316. throw new IllegalStateException(NO_ASSOC_ERROR);
  317. if (jpegQuality < 0)
  318. throw new IllegalStateException("JPEG Quality not set");
  319. if (subsamp < 0 && srcYUVImage == null)
  320. throw new IllegalStateException("Subsampling level not set");
  321. if (srcYUVImage != null)
  322. compressedSize = compressFromYUV(srcYUVImage.getPlanes(),
  323. srcYUVImage.getOffsets(),
  324. srcYUVImage.getWidth(),
  325. srcYUVImage.getStrides(),
  326. srcYUVImage.getHeight(),
  327. srcYUVImage.getSubsamp(),
  328. dstBuf, jpegQuality, flags);
  329. else if (srcBuf != null) {
  330. if (srcX >= 0 && srcY >= 0)
  331. compressedSize = compress(srcBuf, srcX, srcY, srcWidth, srcPitch,
  332. srcHeight, srcPixelFormat, dstBuf, subsamp,
  333. jpegQuality, flags);
  334. else
  335. compressedSize = compress(srcBuf, srcWidth, srcPitch, srcHeight,
  336. srcPixelFormat, dstBuf, subsamp, jpegQuality,
  337. flags);
  338. } else if (srcBufInt != null) {
  339. if (srcX >= 0 && srcY >= 0)
  340. compressedSize = compress(srcBufInt, srcX, srcY, srcWidth, srcStride,
  341. srcHeight, srcPixelFormat, dstBuf, subsamp,
  342. jpegQuality, flags);
  343. else
  344. compressedSize = compress(srcBufInt, srcWidth, srcStride, srcHeight,
  345. srcPixelFormat, dstBuf, subsamp, jpegQuality,
  346. flags);
  347. }
  348. }
  349. /**
  350. * Compress the uncompressed source image associated with this compressor
  351. * instance and return a buffer containing a JPEG image.
  352. *
  353. * @param flags the bitwise OR of one or more of
  354. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  355. *
  356. * @return a buffer containing a JPEG image. The length of this buffer will
  357. * not be equal to the size of the JPEG image. Use {@link
  358. * #getCompressedSize} to obtain the size of the JPEG image.
  359. */
  360. public byte[] compress(int flags) throws TJException {
  361. byte[] buf;
  362. if (srcYUVImage != null) {
  363. buf = new byte[TJ.bufSize(srcYUVImage.getWidth(),
  364. srcYUVImage.getHeight(),
  365. srcYUVImage.getSubsamp())];
  366. } else {
  367. checkSourceImage();
  368. buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)];
  369. }
  370. compress(buf, flags);
  371. return buf;
  372. }
  373. /**
  374. * @deprecated Use
  375. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  376. * {@link #compress(byte[], int)} instead.
  377. */
  378. @SuppressWarnings("checkstyle:JavadocMethod")
  379. @Deprecated
  380. public void compress(BufferedImage srcImage, byte[] dstBuf, int flags)
  381. throws TJException {
  382. setSourceImage(srcImage, 0, 0, 0, 0);
  383. compress(dstBuf, flags);
  384. }
  385. /**
  386. * @deprecated Use
  387. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  388. * {@link #compress(int)} instead.
  389. */
  390. @SuppressWarnings("checkstyle:JavadocMethod")
  391. @Deprecated
  392. public byte[] compress(BufferedImage srcImage, int flags)
  393. throws TJException {
  394. setSourceImage(srcImage, 0, 0, 0, 0);
  395. return compress(flags);
  396. }
  397. /**
  398. * Encode the uncompressed source image associated with this compressor
  399. * instance into a YUV planar image and store it in the given
  400. * <code>YUVImage</code> instance. This method uses the accelerated color
  401. * conversion routines in TurboJPEG's underlying codec but does not execute
  402. * any of the other steps in the JPEG compression process. Encoding
  403. * CMYK source images to YUV is not supported.
  404. *
  405. * @param dstImage {@link YUVImage} instance that will receive the YUV planar
  406. * image
  407. *
  408. * @param flags the bitwise OR of one or more of
  409. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  410. */
  411. public void encodeYUV(YUVImage dstImage, int flags) throws TJException {
  412. if (dstImage == null || flags < 0)
  413. throw new IllegalArgumentException("Invalid argument in encodeYUV()");
  414. if (srcBuf == null && srcBufInt == null)
  415. throw new IllegalStateException(NO_ASSOC_ERROR);
  416. if (srcYUVImage != null)
  417. throw new IllegalStateException("Source image is not correct type");
  418. checkSubsampling();
  419. if (srcWidth != dstImage.getWidth() || srcHeight != dstImage.getHeight())
  420. throw new IllegalStateException("Destination image is the wrong size");
  421. if (srcBufInt != null) {
  422. encodeYUV(srcBufInt, srcX, srcY, srcWidth, srcStride, srcHeight,
  423. srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
  424. dstImage.getStrides(), dstImage.getSubsamp(), flags);
  425. } else {
  426. encodeYUV(srcBuf, srcX, srcY, srcWidth, srcPitch, srcHeight,
  427. srcPixelFormat, dstImage.getPlanes(), dstImage.getOffsets(),
  428. dstImage.getStrides(), dstImage.getSubsamp(), flags);
  429. }
  430. compressedSize = 0;
  431. }
  432. /**
  433. * @deprecated Use {@link #encodeYUV(YUVImage, int)} instead.
  434. */
  435. @SuppressWarnings("checkstyle:JavadocMethod")
  436. @Deprecated
  437. public void encodeYUV(byte[] dstBuf, int flags) throws TJException {
  438. if (dstBuf == null)
  439. throw new IllegalArgumentException("Invalid argument in encodeYUV()");
  440. checkSourceImage();
  441. checkSubsampling();
  442. YUVImage dstYUVImage = new YUVImage(dstBuf, srcWidth, 4, srcHeight,
  443. subsamp);
  444. encodeYUV(dstYUVImage, flags);
  445. }
  446. /**
  447. * Encode the uncompressed source image associated with this compressor
  448. * instance into a unified YUV planar image buffer and return a
  449. * <code>YUVImage</code> instance containing the encoded image. This method
  450. * uses the accelerated color conversion routines in TurboJPEG's underlying
  451. * codec but does not execute any of the other steps in the JPEG compression
  452. * process. Encoding CMYK source images to YUV is not supported.
  453. *
  454. * @param pad the width of each line in each plane of the YUV image will be
  455. * padded to the nearest multiple of this number of bytes (must be a power of
  456. * 2.)
  457. *
  458. * @param flags the bitwise OR of one or more of
  459. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  460. *
  461. * @return a YUV planar image.
  462. */
  463. public YUVImage encodeYUV(int pad, int flags) throws TJException {
  464. checkSourceImage();
  465. checkSubsampling();
  466. if (pad < 1 || ((pad & (pad - 1)) != 0))
  467. throw new IllegalStateException("Invalid argument in encodeYUV()");
  468. YUVImage dstYUVImage = new YUVImage(srcWidth, pad, srcHeight, subsamp);
  469. encodeYUV(dstYUVImage, flags);
  470. return dstYUVImage;
  471. }
  472. /**
  473. * Encode the uncompressed source image associated with this compressor
  474. * instance into separate Y, U (Cb), and V (Cr) image planes and return a
  475. * <code>YUVImage</code> instance containing the encoded image planes. This
  476. * method uses the accelerated color conversion routines in TurboJPEG's
  477. * underlying codec but does not execute any of the other steps in the JPEG
  478. * compression process. Encoding CMYK source images to YUV is not supported.
  479. *
  480. * @param strides an array of integers, each specifying the number of bytes
  481. * per line in the corresponding plane of the output image. Setting the
  482. * stride for any plane to 0 is the same as setting it to the component width
  483. * of the plane. If <code>strides</code> is null, then the strides for all
  484. * planes will be set to their respective component widths. You can adjust
  485. * the strides in order to add an arbitrary amount of line padding to each
  486. * plane.
  487. *
  488. * @param flags the bitwise OR of one or more of
  489. * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
  490. *
  491. * @return a YUV planar image.
  492. */
  493. public YUVImage encodeYUV(int[] strides, int flags) throws TJException {
  494. checkSourceImage();
  495. checkSubsampling();
  496. YUVImage dstYUVImage = new YUVImage(srcWidth, strides, srcHeight, subsamp);
  497. encodeYUV(dstYUVImage, flags);
  498. return dstYUVImage;
  499. }
  500. /**
  501. * @deprecated Use {@link #encodeYUV(int, int)} instead.
  502. */
  503. @SuppressWarnings("checkstyle:JavadocMethod")
  504. @Deprecated
  505. public byte[] encodeYUV(int flags) throws TJException {
  506. checkSourceImage();
  507. checkSubsampling();
  508. YUVImage dstYUVImage = new YUVImage(srcWidth, 4, srcHeight, subsamp);
  509. encodeYUV(dstYUVImage, flags);
  510. return dstYUVImage.getBuf();
  511. }
  512. /**
  513. * @deprecated Use
  514. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  515. * {@link #encodeYUV(byte[], int)} instead.
  516. */
  517. @SuppressWarnings("checkstyle:JavadocMethod")
  518. @Deprecated
  519. public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags)
  520. throws TJException {
  521. setSourceImage(srcImage, 0, 0, 0, 0);
  522. encodeYUV(dstBuf, flags);
  523. }
  524. /**
  525. * @deprecated Use
  526. * {@link #setSourceImage(BufferedImage, int, int, int, int)} and
  527. * {@link #encodeYUV(int, int)} instead.
  528. */
  529. @SuppressWarnings("checkstyle:JavadocMethod")
  530. @Deprecated
  531. public byte[] encodeYUV(BufferedImage srcImage, int flags)
  532. throws TJException {
  533. setSourceImage(srcImage, 0, 0, 0, 0);
  534. return encodeYUV(flags);
  535. }
  536. /**
  537. * Returns the size of the image (in bytes) generated by the most recent
  538. * compress operation.
  539. *
  540. * @return the size of the image (in bytes) generated by the most recent
  541. * compress operation.
  542. */
  543. public int getCompressedSize() {
  544. return compressedSize;
  545. }
  546. /**
  547. * Free the native structures associated with this compressor instance.
  548. */
  549. @Override
  550. public void close() throws TJException {
  551. if (handle != 0)
  552. destroy();
  553. }
  554. @SuppressWarnings("checkstyle:DesignForExtension")
  555. @Override
  556. protected void finalize() throws Throwable {
  557. try {
  558. close();
  559. } catch (TJException e) {
  560. } finally {
  561. super.finalize();
  562. }
  563. };
  564. private native void init() throws TJException;
  565. private native void destroy() throws TJException;
  566. // JPEG size in bytes is returned
  567. @SuppressWarnings("checkstyle:HiddenField")
  568. @Deprecated
  569. private native int compress(byte[] srcBuf, int width, int pitch,
  570. int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp, int jpegQual,
  571. int flags) throws TJException;
  572. @SuppressWarnings("checkstyle:HiddenField")
  573. private native int compress(byte[] srcBuf, int x, int y, int width,
  574. int pitch, int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp,
  575. int jpegQual, int flags) throws TJException;
  576. @SuppressWarnings("checkstyle:HiddenField")
  577. @Deprecated
  578. private native int compress(int[] srcBuf, int width, int stride,
  579. int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp, int jpegQual,
  580. int flags) throws TJException;
  581. @SuppressWarnings("checkstyle:HiddenField")
  582. private native int compress(int[] srcBuf, int x, int y, int width,
  583. int stride, int height, int pixelFormat, byte[] jpegBuf, int jpegSubsamp,
  584. int jpegQual, int flags) throws TJException;
  585. @SuppressWarnings("checkstyle:HiddenField")
  586. private native int compressFromYUV(byte[][] srcPlanes, int[] srcOffsets,
  587. int width, int[] srcStrides, int height, int subsamp, byte[] jpegBuf,
  588. int jpegQual, int flags)
  589. throws TJException;
  590. @SuppressWarnings("checkstyle:HiddenField")
  591. @Deprecated
  592. private native void encodeYUV(byte[] srcBuf, int width, int pitch,
  593. int height, int pixelFormat, byte[] dstBuf, int subsamp, int flags)
  594. throws TJException;
  595. @SuppressWarnings("checkstyle:HiddenField")
  596. private native void encodeYUV(byte[] srcBuf, int x, int y, int width,
  597. int pitch, int height, int pixelFormat, byte[][] dstPlanes,
  598. int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
  599. throws TJException;
  600. @SuppressWarnings("checkstyle:HiddenField")
  601. @Deprecated
  602. private native void encodeYUV(int[] srcBuf, int width, int stride,
  603. int height, int pixelFormat, byte[] dstBuf, int subsamp, int flags)
  604. throws TJException;
  605. @SuppressWarnings("checkstyle:HiddenField")
  606. private native void encodeYUV(int[] srcBuf, int x, int y, int width,
  607. int srcStride, int height, int pixelFormat, byte[][] dstPlanes,
  608. int[] dstOffsets, int[] dstStrides, int subsamp, int flags)
  609. throws TJException;
  610. static {
  611. TJLoader.load();
  612. }
  613. private void checkSourceImage() {
  614. if (srcWidth < 1 || srcHeight < 1)
  615. throw new IllegalStateException(NO_ASSOC_ERROR);
  616. }
  617. private void checkSubsampling() {
  618. if (subsamp < 0)
  619. throw new IllegalStateException("Subsampling level not set");
  620. }
  621. private long handle = 0;
  622. private byte[] srcBuf = null;
  623. private int[] srcBufInt = null;
  624. private int srcWidth = 0;
  625. private int srcHeight = 0;
  626. private int srcX = -1;
  627. private int srcY = -1;
  628. private int srcPitch = 0;
  629. private int srcStride = 0;
  630. private int srcPixelFormat = -1;
  631. private YUVImage srcYUVImage = null;
  632. private int subsamp = -1;
  633. private int jpegQuality = -1;
  634. private int compressedSize = 0;
  635. private int yuvPad = 4;
  636. private ByteOrder byteOrder = null;
  637. }