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