2 * Copyright (C)2014, 2017, 2023 D. R. Commander. All Rights Reserved.
3 * Copyright (C)2015 Viktor Szathmáry. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
30 package org.libjpegturbo.turbojpeg;
33 * This class encapsulates a planar YUV image and the metadata
34 * associated with it. The TurboJPEG API allows both the JPEG compression and
35 * decompression pipelines to be split into stages: YUV encode, compress from
36 * YUV, decompress to YUV, and YUV decode. A <code>YUVImage</code> instance
37 * serves as the destination image for YUV encode and decompress-to-YUV
38 * operations and as the source image for compress-from-YUV and YUV decode
41 * Technically, the JPEG format uses the YCbCr colorspace (which is technically
42 * not a colorspace but a color transform), but per the convention of the
43 * digital video community, the TurboJPEG API uses "YUV" to refer to an image
44 * format consisting of Y, Cb, and Cr image planes.
46 * Each plane is simply a 2D array of bytes, each byte representing the value
47 * of one of the components (Y, Cb, or Cr) at a particular location in the
48 * image. The width and height of each plane are determined by the image
49 * width, height, and level of chrominance subsampling. The luminance plane
50 * width is the image width padded to the nearest multiple of the horizontal
51 * subsampling factor (1 in the case of 4:4:4, grayscale, 4:4:0, or 4:4:1; 2 in
52 * the case of 4:2:2 or 4:2:0; 4 in the case of 4:1:1.) Similarly, the
53 * luminance plane height is the image height padded to the nearest multiple of
54 * the vertical subsampling factor (1 in the case of 4:4:4, 4:2:2, grayscale,
55 * or 4:1:1; 2 in the case of 4:2:0 or 4:4:0; 4 in the case of 4:4:1.) This is
56 * irrespective of any additional padding that may be specified as an argument
57 * to the various YUVImage methods. The chrominance plane width is equal to
58 * the luminance plane width divided by the horizontal subsampling factor, and
59 * the chrominance plane height is equal to the luminance plane height divided
60 * by the vertical subsampling factor.
62 * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
63 * used, then the luminance plane would be 36 x 35 bytes, and each of the
64 * chrominance planes would be 18 x 35 bytes. If you specify a row alignment
65 * of 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes,
66 * and each of the chrominance planes would be 20 x 35 bytes.
68 public class YUVImage {
70 private static final String NO_ASSOC_ERROR =
71 "No image data is associated with this instance";
74 * Create a new <code>YUVImage</code> instance backed by separate image
75 * planes, and allocate memory for the image planes.
77 * @param width width (in pixels) of the YUV image
79 * @param strides an array of integers, each specifying the number of bytes
80 * per row in the corresponding plane of the YUV image. Setting the stride
81 * for any plane to 0 is the same as setting it to the plane width (see
82 * {@link YUVImage above}.) If <code>strides</code> is null, then the
83 * strides for all planes will be set to their respective plane widths. When
84 * using this constructor, the stride for each plane must be equal to or
85 * greater than the plane width.
87 * @param height height (in pixels) of the YUV image
89 * @param subsamp the level of chrominance subsampling to be used in the YUV
90 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
92 public YUVImage(int width, int[] strides, int height, int subsamp) {
93 setBuf(null, null, width, strides, height, subsamp, true);
97 * Create a new <code>YUVImage</code> instance backed by a unified buffer,
98 * and allocate memory for the buffer.
100 * @param width width (in pixels) of the YUV image
102 * @param align row alignment (in bytes) of the YUV image (must be a power of
103 * 2.) Setting this parameter to n specifies that each row in each plane of
104 * the YUV image will be padded to the nearest multiple of n bytes
107 * @param height height (in pixels) of the YUV image
109 * @param subsamp the level of chrominance subsampling to be used in the YUV
110 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
112 public YUVImage(int width, int align, int height, int subsamp) {
113 setBuf(new byte[TJ.bufSizeYUV(width, align, height, subsamp)], width,
114 align, height, subsamp);
118 * Create a new <code>YUVImage</code> instance from a set of existing image
121 * @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
122 * image planes (or just the Y plane, if the image is grayscale.) These
123 * planes can be contiguous or non-contiguous in memory. Plane
124 * <code>i</code> should be at least <code>offsets[i] +
125 * {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)</code>
128 * @param offsets If this <code>YUVImage</code> instance represents a
129 * subregion of a larger image, then <code>offsets[i]</code> specifies the
130 * offset (in bytes) of the subregion within plane <code>i</code> of the
131 * larger image. Setting this to null is the same as setting the offsets for
134 * @param width width (in pixels) of the new YUV image (or subregion)
136 * @param strides an array of integers, each specifying the number of bytes
137 * per row in the corresponding plane of the YUV image. Setting the stride
138 * for any plane to 0 is the same as setting it to the plane width (see
139 * {@link YUVImage above}.) If <code>strides</code> is null, then the
140 * strides for all planes will be set to their respective plane widths. You
141 * can adjust the strides in order to add an arbitrary amount of row padding
142 * to each plane or to specify that this <code>YUVImage</code> instance is a
143 * subregion of a larger image (in which case, <code>strides[i]</code> should
144 * be set to the plane width of plane <code>i</code> in the larger image.)
146 * @param height height (in pixels) of the new YUV image (or subregion)
148 * @param subsamp the level of chrominance subsampling used in the YUV
149 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
151 public YUVImage(byte[][] planes, int[] offsets, int width, int[] strides,
152 int height, int subsamp) {
153 setBuf(planes, offsets, width, strides, height, subsamp, false);
157 * Create a new <code>YUVImage</code> instance from an existing unified
160 * @param yuvImage buffer that contains or will receive a unified planar YUV
161 * image. Use {@link TJ#bufSizeYUV TJ.bufSizeYUV()} to determine the minimum
162 * size for this buffer. The Y, U (Cb), and V (Cr) image planes are stored
163 * sequentially in the buffer. (See {@link YUVImage above} for a description
164 * of the image format.)
166 * @param width width (in pixels) of the YUV image
168 * @param align row alignment (in bytes) of the YUV image (must be a power of
169 * 2.) Setting this parameter to n specifies that each row in each plane of
170 * the YUV image will be padded to the nearest multiple of n bytes
173 * @param height height (in pixels) of the YUV image
175 * @param subsamp the level of chrominance subsampling used in the YUV
176 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
178 public YUVImage(byte[] yuvImage, int width, int align, int height,
180 setBuf(yuvImage, width, align, height, subsamp);
184 * Assign a set of image planes to this <code>YUVImage</code> instance.
186 * @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
187 * image planes (or just the Y plane, if the image is grayscale.) These
188 * planes can be contiguous or non-contiguous in memory. Plane
189 * <code>i</code> should be at least <code>offsets[i] +
190 * {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)</code>
193 * @param offsets If this <code>YUVImage</code> instance represents a
194 * subregion of a larger image, then <code>offsets[i]</code> specifies the
195 * offset (in bytes) of the subregion within plane <code>i</code> of the
196 * larger image. Setting this to null is the same as setting the offsets for
199 * @param width width (in pixels) of the YUV image (or subregion)
201 * @param strides an array of integers, each specifying the number of bytes
202 * per row in the corresponding plane of the YUV image. Setting the stride
203 * for any plane to 0 is the same as setting it to the plane width (see
204 * {@link YUVImage above}.) If <code>strides</code> is null, then the
205 * strides for all planes will be set to their respective plane widths. You
206 * can adjust the strides in order to add an arbitrary amount of row padding
207 * to each plane or to specify that this <code>YUVImage</code> instance is a
208 * subregion of a larger image (in which case, <code>strides[i]</code> should
209 * be set to the plane width of plane <code>i</code> in the larger image.)
211 * @param height height (in pixels) of the YUV image (or subregion)
213 * @param subsamp the level of chrominance subsampling used in the YUV
214 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
216 public void setBuf(byte[][] planes, int[] offsets, int width, int[] strides,
217 int height, int subsamp) {
218 setBuf(planes, offsets, width, strides, height, subsamp, false);
221 private void setBuf(byte[][] planes, int[] offsets, int width, int[] strides,
222 int height, int subsamp, boolean alloc) {
223 if ((planes == null && !alloc) || width < 1 || height < 1 || subsamp < 0 ||
224 subsamp >= TJ.NUMSAMP)
225 throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
227 int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
228 if ((planes != null && planes.length != nc) ||
229 (offsets != null && offsets.length != nc) ||
230 (strides != null && strides.length != nc))
231 throw new IllegalArgumentException("YUVImage::setBuf(): planes, offsets, or strides array is the wrong size");
234 planes = new byte[nc][];
236 offsets = new int[nc];
238 strides = new int[nc];
240 for (int i = 0; i < nc; i++) {
241 int pw = TJ.planeWidth(i, width, subsamp);
242 int ph = TJ.planeHeight(i, height, subsamp);
243 int planeSize = TJ.planeSizeYUV(i, width, strides[i], height, subsamp);
249 throw new IllegalArgumentException("Stride must be >= plane width when allocating a new YUV image");
250 planes[i] = new byte[strides[i] * ph];
252 if (planes[i] == null || offsets[i] < 0)
253 throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
254 if (strides[i] < 0 && offsets[i] - planeSize + pw < 0)
255 throw new IllegalArgumentException("Stride for plane " + i +
256 " would cause memory to be accessed below plane boundary");
257 if (planes[i].length < offsets[i] + planeSize)
258 throw new IllegalArgumentException("Image plane " + i +
259 " is not large enough");
263 yuvOffsets = offsets;
265 yuvStrides = strides;
267 yuvSubsamp = subsamp;
271 * Assign a unified buffer to this <code>YUVImage</code> instance.
273 * @param yuvImage buffer that contains or will receive a unified planar YUV
274 * image. Use {@link TJ#bufSizeYUV TJ.bufSizeYUV()} to determine the minimum
275 * size for this buffer. The Y, U (Cb), and V (Cr) image planes are stored
276 * sequentially in the buffer. (See {@link YUVImage above} for a description
277 * of the image format.)
279 * @param width width (in pixels) of the YUV image
281 * @param align row alignment (in bytes) of the YUV image (must be a power of
282 * 2.) Setting this parameter to n specifies that each row in each plane of
283 * the YUV image will be padded to the nearest multiple of n bytes
286 * @param height height (in pixels) of the YUV image
288 * @param subsamp the level of chrominance subsampling used in the YUV
289 * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
291 public void setBuf(byte[] yuvImage, int width, int align, int height,
293 if (yuvImage == null || width < 1 || align < 1 ||
294 ((align & (align - 1)) != 0) || height < 1 || subsamp < 0 ||
295 subsamp >= TJ.NUMSAMP)
296 throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
297 if (yuvImage.length < TJ.bufSizeYUV(width, align, height, subsamp))
298 throw new IllegalArgumentException("YUV buffer is not large enough");
300 int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
301 byte[][] planes = new byte[nc][];
302 int[] strides = new int[nc];
303 int[] offsets = new int[nc];
305 planes[0] = yuvImage;
306 strides[0] = pad(TJ.planeWidth(0, width, subsamp), align);
307 if (subsamp != TJ.SAMP_GRAY) {
308 strides[1] = strides[2] = pad(TJ.planeWidth(1, width, subsamp), align);
309 planes[1] = planes[2] = yuvImage;
310 offsets[1] = offsets[0] +
311 strides[0] * TJ.planeHeight(0, height, subsamp);
312 offsets[2] = offsets[1] +
313 strides[1] * TJ.planeHeight(1, height, subsamp);
317 setBuf(planes, offsets, width, strides, height, subsamp);
321 * Returns the width of the YUV image (or subregion.)
323 * @return the width of the YUV image (or subregion)
325 public int getWidth() {
327 throw new IllegalStateException(NO_ASSOC_ERROR);
332 * Returns the height of the YUV image (or subregion.)
334 * @return the height of the YUV image (or subregion)
336 public int getHeight() {
338 throw new IllegalStateException(NO_ASSOC_ERROR);
343 * Returns the row alignment (in bytes) of the YUV buffer (if this image is
344 * stored in a unified buffer rather than separate image planes.)
346 * @return the row alignment of the YUV buffer
348 public int getPad() {
349 if (yuvPlanes == null)
350 throw new IllegalStateException(NO_ASSOC_ERROR);
351 if (yuvAlign < 1 || ((yuvAlign & (yuvAlign - 1)) != 0))
352 throw new IllegalStateException("Image is not stored in a unified buffer");
357 * Returns the number of bytes per row of each plane in the YUV image.
359 * @return the number of bytes per row of each plane in the YUV image
361 public int[] getStrides() {
362 if (yuvStrides == null)
363 throw new IllegalStateException(NO_ASSOC_ERROR);
368 * Returns the offsets (in bytes) of each plane within the planes of a larger
371 * @return the offsets (in bytes) of each plane within the planes of a larger
374 public int[] getOffsets() {
375 if (yuvOffsets == null)
376 throw new IllegalStateException(NO_ASSOC_ERROR);
381 * Returns the level of chrominance subsampling used in the YUV image. See
382 * {@link TJ#SAMP_444 TJ.SAMP_*}.
384 * @return the level of chrominance subsampling used in the YUV image
386 public int getSubsamp() {
387 if (yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
388 throw new IllegalStateException(NO_ASSOC_ERROR);
393 * Returns the YUV image planes. If the image is stored in a unified buffer,
394 * then all image planes will point to that buffer.
396 * @return the YUV image planes
398 public byte[][] getPlanes() {
399 if (yuvPlanes == null)
400 throw new IllegalStateException(NO_ASSOC_ERROR);
405 * Returns the YUV buffer (if this image is stored in a unified buffer rather
406 * than separate image planes.)
408 * @return the YUV buffer
410 public byte[] getBuf() {
411 if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
412 throw new IllegalStateException(NO_ASSOC_ERROR);
413 int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
414 for (int i = 1; i < nc; i++) {
415 if (yuvPlanes[i] != yuvPlanes[0])
416 throw new IllegalStateException("Image is not stored in a unified buffer");
422 * Returns the size (in bytes) of the YUV buffer (if this image is stored in
423 * a unified buffer rather than separate image planes.)
425 * @return the size (in bytes) of the YUV buffer
427 public int getSize() {
428 if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
429 throw new IllegalStateException(NO_ASSOC_ERROR);
430 int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
432 throw new IllegalStateException("Image is not stored in a unified buffer");
433 for (int i = 1; i < nc; i++) {
434 if (yuvPlanes[i] != yuvPlanes[0])
435 throw new IllegalStateException("Image is not stored in a unified buffer");
437 return TJ.bufSizeYUV(yuvWidth, yuvAlign, yuvHeight, yuvSubsamp);
440 private static int pad(int v, int p) {
441 return (v + p - 1) & (~(p - 1));
444 private long handle = 0;
445 private byte[][] yuvPlanes = null;
446 private int[] yuvOffsets = null;
447 private int[] yuvStrides = null;
448 private int yuvAlign = 1;
449 private int yuvWidth = 0;
450 private int yuvHeight = 0;
451 private int yuvSubsamp = -1;