TJ.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C)2011-2013 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. /**
  31. * TurboJPEG utility class (cannot be instantiated)
  32. */
  33. public final class TJ {
  34. /**
  35. * The number of chrominance subsampling options
  36. */
  37. public static final int NUMSAMP = 6;
  38. /**
  39. * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG
  40. * or YUV image will contain one chrominance component for every pixel in the
  41. * source image.
  42. */
  43. public static final int SAMP_444 = 0;
  44. /**
  45. * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
  46. * chrominance component for every 2x1 block of pixels in the source image.
  47. */
  48. public static final int SAMP_422 = 1;
  49. /**
  50. * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
  51. * chrominance component for every 2x2 block of pixels in the source image.
  52. */
  53. public static final int SAMP_420 = 2;
  54. /**
  55. * Grayscale. The JPEG or YUV image will contain no chrominance components.
  56. */
  57. public static final int SAMP_GRAY = 3;
  58. /**
  59. * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
  60. * chrominance component for every 1x2 block of pixels in the source image.
  61. * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
  62. */
  63. public static final int SAMP_440 = 4;
  64. /**
  65. * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
  66. * chrominance component for every 4x1 block of pixels in the source image.
  67. * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
  68. * same size as those compressed with 4:2:0 subsampling, and in the
  69. * aggregate, both subsampling methods produce approximately the same
  70. * perceptual quality. However, 4:1:1 is better able to reproduce sharp
  71. * horizontal features. Note that 4:1:1 subsampling is not fully accelerated
  72. * in libjpeg-turbo.
  73. */
  74. public static final int SAMP_411 = 5;
  75. /**
  76. * Returns the MCU block width for the given level of chrominance
  77. * subsampling.
  78. *
  79. * @param subsamp the level of chrominance subsampling (one of
  80. * <code>SAMP_*</code>)
  81. *
  82. * @return the MCU block width for the given level of chrominance
  83. * subsampling.
  84. */
  85. public static int getMCUWidth(int subsamp) {
  86. checkSubsampling(subsamp);
  87. return mcuWidth[subsamp];
  88. }
  89. private static final int[] mcuWidth = {
  90. 8, 16, 16, 8, 8, 32
  91. };
  92. /**
  93. * Returns the MCU block height for the given level of chrominance
  94. * subsampling.
  95. *
  96. * @param subsamp the level of chrominance subsampling (one of
  97. * <code>SAMP_*</code>)
  98. *
  99. * @return the MCU block height for the given level of chrominance
  100. * subsampling.
  101. */
  102. public static int getMCUHeight(int subsamp) {
  103. checkSubsampling(subsamp);
  104. return mcuHeight[subsamp];
  105. }
  106. private static final int[] mcuHeight = {
  107. 8, 8, 16, 8, 16, 8
  108. };
  109. /**
  110. * The number of pixel formats
  111. */
  112. public static final int NUMPF = 12;
  113. /**
  114. * RGB pixel format. The red, green, and blue components in the image are
  115. * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
  116. * address within each pixel.
  117. */
  118. public static final int PF_RGB = 0;
  119. /**
  120. * BGR pixel format. The red, green, and blue components in the image are
  121. * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
  122. * address within each pixel.
  123. */
  124. public static final int PF_BGR = 1;
  125. /**
  126. * RGBX pixel format. The red, green, and blue components in the image are
  127. * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
  128. * address within each pixel. The X component is ignored when compressing
  129. * and undefined when decompressing.
  130. */
  131. public static final int PF_RGBX = 2;
  132. /**
  133. * BGRX pixel format. The red, green, and blue components in the image are
  134. * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
  135. * address within each pixel. The X component is ignored when compressing
  136. * and undefined when decompressing.
  137. */
  138. public static final int PF_BGRX = 3;
  139. /**
  140. * XBGR pixel format. The red, green, and blue components in the image are
  141. * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
  142. * address within each pixel. The X component is ignored when compressing
  143. * and undefined when decompressing.
  144. */
  145. public static final int PF_XBGR = 4;
  146. /**
  147. * XRGB pixel format. The red, green, and blue components in the image are
  148. * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
  149. * address within each pixel. The X component is ignored when compressing
  150. * and undefined when decompressing.
  151. */
  152. public static final int PF_XRGB = 5;
  153. /**
  154. * Grayscale pixel format. Each 1-byte pixel represents a luminance
  155. * (brightness) level from 0 to 255.
  156. */
  157. public static final int PF_GRAY = 6;
  158. /**
  159. * RGBA pixel format. This is the same as {@link #PF_RGBX}, except that when
  160. * decompressing, the X byte is guaranteed to be 0xFF, which can be
  161. * interpreted as an opaque alpha channel.
  162. */
  163. public static final int PF_RGBA = 7;
  164. /**
  165. * BGRA pixel format. This is the same as {@link #PF_BGRX}, except that when
  166. * decompressing, the X byte is guaranteed to be 0xFF, which can be
  167. * interpreted as an opaque alpha channel.
  168. */
  169. public static final int PF_BGRA = 8;
  170. /**
  171. * ABGR pixel format. This is the same as {@link #PF_XBGR}, except that when
  172. * decompressing, the X byte is guaranteed to be 0xFF, which can be
  173. * interpreted as an opaque alpha channel.
  174. */
  175. public static final int PF_ABGR = 9;
  176. /**
  177. * ARGB pixel format. This is the same as {@link #PF_XRGB}, except that when
  178. * decompressing, the X byte is guaranteed to be 0xFF, which can be
  179. * interpreted as an opaque alpha channel.
  180. */
  181. public static final int PF_ARGB = 10;
  182. /**
  183. * CMYK pixel format. Unlike RGB, which is an additive color model used
  184. * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
  185. * color model used primarily for printing. In the CMYK color model, the
  186. * value of each color component typically corresponds to an amount of cyan,
  187. * magenta, yellow, or black ink that is applied to a white background. In
  188. * order to convert between CMYK and RGB, it is necessary to use a color
  189. * management system (CMS.) A CMS will attempt to map colors within the
  190. * printer's gamut to perceptually similar colors in the display's gamut and
  191. * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
  192. * be defined with a simple formula. Thus, such a conversion is out of scope
  193. * for a codec library. However, the TurboJPEG API allows for compressing
  194. * CMYK pixels into a YCCK JPEG image (see {@link #CS_YCCK}) and
  195. * decompressing YCCK JPEG images into CMYK pixels.
  196. */
  197. public static final int PF_CMYK = 11;
  198. /**
  199. * Returns the pixel size (in bytes) for the given pixel format.
  200. *
  201. * @param pixelFormat the pixel format (one of <code>PF_*</code>)
  202. *
  203. * @return the pixel size (in bytes) for the given pixel format.
  204. */
  205. public static int getPixelSize(int pixelFormat) {
  206. checkPixelFormat(pixelFormat);
  207. return pixelSize[pixelFormat];
  208. }
  209. private static final int[] pixelSize = {
  210. 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
  211. };
  212. /**
  213. * For the given pixel format, returns the number of bytes that the red
  214. * component is offset from the start of the pixel. For instance, if a pixel
  215. * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
  216. * then the red component will be
  217. * <code>pixel[TJ.getRedOffset(TJ.PF_BGRX)]</code>.
  218. *
  219. * @param pixelFormat the pixel format (one of <code>PF_*</code>)
  220. *
  221. * @return the red offset for the given pixel format.
  222. */
  223. public static int getRedOffset(int pixelFormat) {
  224. checkPixelFormat(pixelFormat);
  225. return redOffset[pixelFormat];
  226. }
  227. private static final int[] redOffset = {
  228. 0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1, -1
  229. };
  230. /**
  231. * For the given pixel format, returns the number of bytes that the green
  232. * component is offset from the start of the pixel. For instance, if a pixel
  233. * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
  234. * then the green component will be
  235. * <code>pixel[TJ.getGreenOffset(TJ.PF_BGRX)]</code>.
  236. *
  237. * @param pixelFormat the pixel format (one of <code>PF_*</code>)
  238. *
  239. * @return the green offset for the given pixel format.
  240. */
  241. public static int getGreenOffset(int pixelFormat) {
  242. checkPixelFormat(pixelFormat);
  243. return greenOffset[pixelFormat];
  244. }
  245. private static final int[] greenOffset = {
  246. 1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2, -1
  247. };
  248. /**
  249. * For the given pixel format, returns the number of bytes that the blue
  250. * component is offset from the start of the pixel. For instance, if a pixel
  251. * of format <code>TJ.PF_BGRX</code> is stored in <code>char pixel[]</code>,
  252. * then the blue component will be
  253. * <code>pixel[TJ.getBlueOffset(TJ.PF_BGRX)]</code>.
  254. *
  255. * @param pixelFormat the pixel format (one of <code>PF_*</code>)
  256. *
  257. * @return the blue offset for the given pixel format.
  258. */
  259. public static int getBlueOffset(int pixelFormat) {
  260. checkPixelFormat(pixelFormat);
  261. return blueOffset[pixelFormat];
  262. }
  263. private static final int[] blueOffset = {
  264. 2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3, -1
  265. };
  266. /**
  267. * The number of JPEG colorspaces
  268. */
  269. public static final int NUMCS = 5;
  270. /**
  271. * RGB colorspace. When compressing the JPEG image, the R, G, and B
  272. * components in the source image are reordered into image planes, but no
  273. * colorspace conversion or subsampling is performed. RGB JPEG images can be
  274. * decompressed to any of the extended RGB pixel formats or grayscale, but
  275. * they cannot be decompressed to YUV images.
  276. */
  277. public static final int CS_RGB = 0;
  278. /**
  279. * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
  280. * mathematical transformation of RGB designed solely for storage and
  281. * transmission. YCbCr images must be converted to RGB before they can
  282. * actually be displayed. In the YCbCr colorspace, the Y (luminance)
  283. * component represents the black & white portion of the original image, and
  284. * the Cb and Cr (chrominance) components represent the color portion of the
  285. * original image. Originally, the analog equivalent of this transformation
  286. * allowed the same signal to drive both black & white and color televisions,
  287. * but JPEG images use YCbCr primarily because it allows the color data to be
  288. * optionally subsampled for the purposes of reducing bandwidth or disk
  289. * space. YCbCr is the most common JPEG colorspace, and YCbCr JPEG images
  290. * can be compressed from and decompressed to any of the extended RGB pixel
  291. * formats or grayscale, or they can be decompressed to YUV planar images.
  292. */
  293. public static final int CS_YCbCr = 1;
  294. /**
  295. * Grayscale colorspace. The JPEG image retains only the luminance data (Y
  296. * component), and any color data from the source image is discarded.
  297. * Grayscale JPEG images can be compressed from and decompressed to any of
  298. * the extended RGB pixel formats or grayscale, or they can be decompressed
  299. * to YUV planar images.
  300. */
  301. public static final int CS_GRAY = 2;
  302. /**
  303. * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
  304. * components in the source image are reordered into image planes, but no
  305. * colorspace conversion or subsampling is performed. CMYK JPEG images can
  306. * only be decompressed to CMYK pixels.
  307. */
  308. public static final int CS_CMYK = 3;
  309. /**
  310. * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
  311. * rather a mathematical transformation of CMYK designed solely for storage
  312. * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
  313. * reversibly transformed into YCCK, and as with YCbCr, the chrominance
  314. * components in the YCCK pixels can be subsampled without incurring major
  315. * perceptual loss. YCCK JPEG images can only be compressed from and
  316. * decompressed to CMYK pixels.
  317. */
  318. public static final int CS_YCCK = 4;
  319. /**
  320. * The uncompressed source/destination image is stored in bottom-up (Windows,
  321. * OpenGL) order, not top-down (X11) order.
  322. */
  323. public static final int FLAG_BOTTOMUP = 2;
  324. @Deprecated
  325. public static final int FLAG_FORCEMMX = 8;
  326. @Deprecated
  327. public static final int FLAG_FORCESSE = 16;
  328. @Deprecated
  329. public static final int FLAG_FORCESSE2 = 32;
  330. @Deprecated
  331. public static final int FLAG_FORCESSE3 = 128;
  332. /**
  333. * When decompressing an image that was compressed using chrominance
  334. * subsampling, use the fastest chrominance upsampling algorithm available in
  335. * the underlying codec. The default is to use smooth upsampling, which
  336. * creates a smooth transition between neighboring chrominance components in
  337. * order to reduce upsampling artifacts in the decompressed image.
  338. */
  339. public static final int FLAG_FASTUPSAMPLE = 256;
  340. /**
  341. * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
  342. * default if this flag is not specified is implementation-specific. For
  343. * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
  344. * algorithm by default when compressing, because this has been shown to have
  345. * only a very slight effect on accuracy, but it uses the accurate algorithm
  346. * when decompressing, because this has been shown to have a larger effect.
  347. */
  348. public static final int FLAG_FASTDCT = 2048;
  349. /**
  350. * Use the most accurate DCT/IDCT algorithm available in the underlying
  351. * codec. The default if this flag is not specified is
  352. * implementation-specific. For example, the implementation of TurboJPEG for
  353. * libjpeg[-turbo] uses the fast algorithm by default when compressing,
  354. * because this has been shown to have only a very slight effect on accuracy,
  355. * but it uses the accurate algorithm when decompressing, because this has
  356. * been shown to have a larger effect.
  357. */
  358. public static final int FLAG_ACCURATEDCT = 4096;
  359. /**
  360. * Returns the maximum size of the buffer (in bytes) required to hold a JPEG
  361. * image with the given width, height, and level of chrominance subsampling.
  362. *
  363. * @param width the width (in pixels) of the JPEG image
  364. *
  365. * @param height the height (in pixels) of the JPEG image
  366. *
  367. * @param jpegSubsamp the level of chrominance subsampling to be used when
  368. * generating the JPEG image (one of {@link TJ TJ.SAMP_*})
  369. *
  370. * @return the maximum size of the buffer (in bytes) required to hold a JPEG
  371. * image with the given width, height, and level of chrominance subsampling.
  372. */
  373. public static native int bufSize(int width, int height, int jpegSubsamp);
  374. /**
  375. * Returns the size of the buffer (in bytes) required to hold a YUV planar
  376. * image with the given width, height, and level of chrominance subsampling.
  377. *
  378. * @param width the width (in pixels) of the YUV image
  379. *
  380. * @param pad the width of each line in each plane of the image is padded to
  381. * the nearest multiple of this number of bytes (must be a power of 2.)
  382. *
  383. * @param height the height (in pixels) of the YUV image
  384. *
  385. * @param subsamp the level of chrominance subsampling used in the YUV
  386. * image (one of {@link TJ TJ.SAMP_*})
  387. *
  388. * @return the size of the buffer (in bytes) required to hold a YUV planar
  389. * image with the given width, height, and level of chrominance subsampling.
  390. */
  391. public static native int bufSizeYUV(int width, int pad, int height,
  392. int subsamp);
  393. /**
  394. * @deprecated Use {@link #bufSizeYUV(int, int, int, int)} instead.
  395. */
  396. @Deprecated
  397. public static native int bufSizeYUV(int width, int height, int subsamp);
  398. /**
  399. * Returns the size of the buffer (in bytes) required to hold a YUV image
  400. * plane with the given parameters.
  401. *
  402. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
  403. * 2 = V/Cr)
  404. *
  405. * @param width width (in pixels) of the YUV image. NOTE: this is the width
  406. * of the whole image, not the plane width.
  407. *
  408. * @param stride bytes per line in the image plane.
  409. *
  410. * @param height height (in pixels) of the YUV image. NOTE: this is the
  411. * height of the whole image, not the plane height.
  412. *
  413. * @param subsamp the level of chrominance subsampling used in the YUV
  414. * image (one of {@link TJ TJ.SAMP_*})
  415. *
  416. * @return the size of the buffer (in bytes) required to hold a YUV planar
  417. * image with the given parameters.
  418. */
  419. public static native int planeSizeYUV(int componentID, int width, int stride,
  420. int height, int subsamp);
  421. /**
  422. * Returns the plane width of a YUV image plane with the given parameters.
  423. * Refer to {@link YUVImage YUVImage} for a description of plane width.
  424. *
  425. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
  426. * 2 = V/Cr)
  427. *
  428. * @param width width (in pixels) of the YUV image
  429. *
  430. * @param subsamp the level of chrominance subsampling used in the YUV image
  431. * (one of {@link TJ TJ.SAMP_*})
  432. *
  433. * @return the plane width of a YUV image plane with the given parameters.
  434. */
  435. public static native int planeWidth(int componentID, int width, int subsamp);
  436. /**
  437. * Returns the plane height of a YUV image plane with the given parameters.
  438. * Refer to {@link YUVImage YUVImage} for a description of plane height.
  439. *
  440. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb,
  441. * 2 = V/Cr)
  442. *
  443. * @param height height (in pixels) of the YUV image
  444. *
  445. * @param subsamp the level of chrominance subsampling used in the YUV image
  446. * (one of {@link TJ TJ.SAMP_*})
  447. *
  448. * @return the plane height of a YUV image plane with the given parameters.
  449. */
  450. public static native int planeHeight(int componentID, int height,
  451. int subsamp);
  452. /**
  453. * Returns a list of fractional scaling factors that the JPEG decompressor in
  454. * this implementation of TurboJPEG supports.
  455. *
  456. * @return a list of fractional scaling factors that the JPEG decompressor in
  457. * this implementation of TurboJPEG supports.
  458. */
  459. public static native TJScalingFactor[] getScalingFactors();
  460. static {
  461. TJLoader.load();
  462. }
  463. private static void checkPixelFormat(int pixelFormat) {
  464. if (pixelFormat < 0 || pixelFormat >= NUMPF)
  465. throw new IllegalArgumentException("Invalid pixel format");
  466. }
  467. private static void checkSubsampling(int subsamp) {
  468. if (subsamp < 0 || subsamp >= NUMSAMP)
  469. throw new IllegalArgumentException("Invalid subsampling type");
  470. }
  471. }