• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C)2014 D. R. Commander.  All Rights Reserved.
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 
31 /**
32  * This class encapsulates a YUV planar image and the metadata
33  * associated with it.  The TurboJPEG API allows both the JPEG compression and
34  * decompression pipelines to be split into stages:  YUV encode, compress from
35  * YUV, decompress to YUV, and YUV decode.  A <code>YUVImage</code> instance
36  * serves as the destination image for YUV encode and decompress-to-YUV
37  * operations and as the source image for compress-from-YUV and YUV decode
38  * operations.
39  * <p>
40  * Technically, the JPEG format uses the YCbCr colorspace (which technically is
41  * not a "colorspace" but rather a "color transform"), but per the convention
42  * of the digital video community, the TurboJPEG API uses "YUV" to refer to an
43  * image format consisting of Y, Cb, and Cr image planes.
44  * <p>
45  * Each plane is simply a 2D array of bytes, each byte representing the value
46  * of one of the components (Y, Cb, or Cr) at a particular location in the
47  * image.  The width and height of each plane are determined by the image
48  * width, height, and level of chrominance subsampling.  The luminance plane
49  * width is the image width padded to the nearest multiple of the horizontal
50  * subsampling factor (2 in the case of 4:2:0 and 4:2:2, 4 in the case of
51  * 4:1:1, 1 in the case of 4:4:4 or grayscale.)  Similarly, the luminance plane
52  * height is the image height padded to the nearest multiple of the vertical
53  * subsampling factor (2 in the case of 4:2:0 or 4:4:0, 1 in the case of 4:4:4
54  * or grayscale.)  The chrominance plane width is equal to the luminance plane
55  * width divided by the horizontal subsampling factor, and the chrominance
56  * plane height is equal to the luminance plane height divided by the vertical
57  * subsampling factor.
58  * <p>
59  * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
60  * used, then the luminance plane would be 36 x 35 bytes, and each of the
61  * chrominance planes would be 18 x 35 bytes.  If you specify a line padding of
62  * 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes, and
63  * each of the chrominance planes would be 20 x 35 bytes.
64  */
65 public class YUVImage {
66 
67   private static final String NO_ASSOC_ERROR =
68     "No image data is associated with this instance";
69 
70   /**
71    * Create a new <code>YUVImage</code> instance backed by separate image
72    * planes, and allocate memory for the image planes.
73    *
74    * @param width width (in pixels) of the YUV image
75    *
76    * @param strides an array of integers, each specifying the number of bytes
77    * per line in the corresponding plane of the YUV image.  Setting the stride
78    * for any plane to 0 is the same as setting it to the plane width (see
79    * {@link YUVImage above}.)  If <code>strides</code> is null, then the
80    * strides for all planes will be set to their respective plane widths.  When
81    * using this constructor, the stride for each plane must be equal to or
82    * greater than the plane width.
83    *
84    * @param height height (in pixels) of the YUV image
85    *
86    * @param subsamp the level of chrominance subsampling to be used in the YUV
87    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
88    */
YUVImage(int width, int[] strides, int height, int subsamp)89   public YUVImage(int width, int[] strides, int height, int subsamp)
90     throws Exception {
91     setBuf(null, null, width, strides, height, subsamp, true);
92   }
93 
94   /**
95    * Create a new <code>YUVImage</code> instance backed by a unified image
96    * buffer, and allocate memory for the image buffer.
97    *
98    * @param width width (in pixels) of the YUV image
99    *
100    * @param pad Each line of each plane in the YUV image buffer will be padded
101    * to this number of bytes (must be a power of 2.)
102    *
103    * @param height height (in pixels) of the YUV image
104    *
105    * @param subsamp the level of chrominance subsampling to be used in the YUV
106    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
107    */
YUVImage(int width, int pad, int height, int subsamp)108   public YUVImage(int width, int pad, int height, int subsamp)
109     throws Exception {
110     setBuf(new byte[TJ.bufSizeYUV(width, pad, height, subsamp)], width, pad,
111            height, subsamp);
112   }
113 
114   /**
115    * Create a new <code>YUVImage</code> instance from a set of existing image
116    * planes.
117    *
118    * @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
119    * image planes (or just the Y plane, if the image is grayscale.)   These
120    * planes can be contiguous or non-contiguous in memory.  Plane
121    * <code>i</code> should be at least <code>offsets[i] +
122    * {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)</code>
123    * bytes in size.
124    *
125    * @param offsets If this <code>YUVImage</code> instance represents a
126    * subregion of a larger image, then <code>offsets[i]</code> specifies the
127    * offset (in bytes) of the subregion within plane <code>i</code> of the
128    * larger image.  Setting this to null is the same as setting the offsets for
129    * all planes to 0.
130    *
131    * @param width width (in pixels) of the new YUV image (or subregion)
132    *
133    * @param strides an array of integers, each specifying the number of bytes
134    * per line in the corresponding plane of the YUV image.  Setting the stride
135    * for any plane to 0 is the same as setting it to the plane width (see
136    * {@link YUVImage above}.)  If <code>strides</code> is null, then the
137    * strides for all planes will be set to their respective plane widths.  You
138    * can adjust the strides in order to add an arbitrary amount of line padding
139    * to each plane or to specify that this <code>YUVImage</code> instance is a
140    * subregion of a larger image (in which case, <code>strides[i]</code> should
141    * be set to the plane width of plane <code>i</code> in the larger image.)
142    *
143    * @param height height (in pixels) of the new YUV image (or subregion)
144    *
145    * @param subsamp the level of chrominance subsampling used in the YUV
146    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
147    */
YUVImage(byte[][] planes, int[] offsets, int width, int[] strides, int height, int subsamp)148   public YUVImage(byte[][] planes, int[] offsets, int width, int[] strides,
149                   int height, int subsamp) throws Exception {
150     setBuf(planes, offsets, width, strides, height, subsamp, false);
151   }
152 
153   /**
154    * Create a new <code>YUVImage</code> instance from an existing unified image
155    * buffer.
156    *
157    * @param yuvImage image buffer that contains or will contain YUV planar
158    * image data.  Use {@link TJ#bufSizeYUV} to determine the minimum size for
159    * this buffer.  The Y, U (Cb), and V (Cr) image planes are stored
160    * sequentially in the buffer (see {@link YUVImage above} for a description
161    * of the image format.)
162    *
163    * @param width width (in pixels) of the YUV image
164    *
165    * @param pad the line padding used in the YUV image buffer.  For
166    * instance, if each line in each plane of the buffer is padded to the
167    * nearest multiple of 4 bytes, then <code>pad</code> should be set to 4.
168    *
169    * @param height height (in pixels) of the YUV image
170    *
171    * @param subsamp the level of chrominance subsampling used in the YUV
172    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
173    */
YUVImage(byte[] yuvImage, int width, int pad, int height, int subsamp)174   public YUVImage(byte[] yuvImage, int width, int pad, int height,
175                   int subsamp) throws Exception {
176     setBuf(yuvImage, width, pad, height, subsamp);
177   }
178 
179   /**
180    * Assign a set of image planes to this <code>YUVImage</code> instance.
181    *
182    * @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
183    * image planes (or just the Y plane, if the image is grayscale.)  These
184    * planes can be contiguous or non-contiguous in memory.  Plane
185    * <code>i</code> should be at least <code>offsets[i] +
186    * {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)</code>
187    * bytes in size.
188    *
189    * @param offsets If this <code>YUVImage</code> instance represents a
190    * subregion of a larger image, then <code>offsets[i]</code> specifies the
191    * offset (in bytes) of the subregion within plane <code>i</code> of the
192    * larger image.  Setting this to null is the same as setting the offsets for
193    * all planes to 0.
194    *
195    * @param width width (in pixels) of the YUV image (or subregion)
196    *
197    * @param strides an array of integers, each specifying the number of bytes
198    * per line in the corresponding plane of the YUV image.  Setting the stride
199    * for any plane to 0 is the same as setting it to the plane width (see
200    * {@link YUVImage above}.)  If <code>strides</code> is null, then the
201    * strides for all planes will be set to their respective plane widths.  You
202    * can adjust the strides in order to add an arbitrary amount of line padding
203    * to each plane or to specify that this <code>YUVImage</code> image is a
204    * subregion of a larger image (in which case, <code>strides[i]</code> should
205    * be set to the plane width of plane <code>i</code> in the larger image.)
206    *
207    * @param height height (in pixels) of the YUV image (or subregion)
208    *
209    * @param subsamp the level of chrominance subsampling used in the YUV
210    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
211    */
setBuf(byte[][] planes, int[] offsets, int width, int strides[], int height, int subsamp)212   public void setBuf(byte[][] planes, int[] offsets, int width, int strides[],
213                      int height, int subsamp) throws Exception {
214     setBuf(planes, offsets, width, strides, height, subsamp, false);
215   }
216 
setBuf(byte[][] planes, int[] offsets, int width, int strides[], int height, int subsamp, boolean alloc)217   private void setBuf(byte[][] planes, int[] offsets, int width, int strides[],
218                      int height, int subsamp, boolean alloc) throws Exception {
219     if ((planes == null && !alloc) || width < 1 || height < 1 || subsamp < 0 ||
220         subsamp >= TJ.NUMSAMP)
221       throw new Exception("Invalid argument in YUVImage::setBuf()");
222 
223     int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
224     if (planes.length != nc || (offsets != null && offsets.length != nc) ||
225         (strides != null && strides.length != nc))
226       throw new Exception("YUVImage::setBuf(): planes, offsets, or strides array is the wrong size");
227 
228     if (offsets == null)
229       offsets = new int[nc];
230     if (strides == null)
231       strides = new int[nc];
232 
233     for (int i = 0; i < nc; i++) {
234       int pw = TJ.planeWidth(i, width, subsamp);
235       int ph = TJ.planeHeight(i, height, subsamp);
236       int planeSize = TJ.planeSizeYUV(i, width, strides[i], height, subsamp);
237 
238       if (strides[i] == 0)
239         strides[i] = pw;
240       if (alloc) {
241         if (strides[i] < pw)
242           throw new Exception("Stride must be >= plane width when allocating a new YUV image");
243         planes[i] = new byte[strides[i] * ph];
244       }
245       if (planes[i] == null || offsets[i] < 0)
246         throw new Exception("Invalid argument in YUVImage::setBuf()");
247       if (strides[i] < 0 && offsets[i] - planeSize + pw < 0)
248         throw new Exception("Stride for plane " + i + " would cause memory to be accessed below plane boundary");
249       if (planes[i].length < offsets[i] + planeSize)
250         throw new Exception("Image plane " + i + " is not large enough");
251     }
252 
253     yuvPlanes = planes;
254     yuvOffsets = offsets;
255     yuvWidth = width;
256     yuvStrides = strides;
257     yuvHeight = height;
258     yuvSubsamp = subsamp;
259   }
260 
261   /**
262    * Assign a unified image buffer to this <code>YUVImage</code> instance.
263    *
264    * @param yuvImage image buffer that contains or will contain YUV planar
265    * image data.  Use {@link TJ#bufSizeYUV} to determine the minimum size for
266    * this buffer.  The Y, U (Cb), and V (Cr) image planes are stored
267    * sequentially in the buffer (see {@link YUVImage above} for a description
268    * of the image format.)
269    *
270    * @param width width (in pixels) of the YUV image
271    *
272    * @param pad the line padding used in the YUV image buffer.  For
273    * instance, if each line in each plane of the buffer is padded to the
274    * nearest multiple of 4 bytes, then <code>pad</code> should be set to 4.
275    *
276    * @param height height (in pixels) of the YUV image
277    *
278    * @param subsamp the level of chrominance subsampling used in the YUV
279    * image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
280    */
setBuf(byte[] yuvImage, int width, int pad, int height, int subsamp)281   public void setBuf(byte[] yuvImage, int width, int pad, int height,
282                      int subsamp) throws Exception {
283     if (yuvImage == null || width < 1 || pad < 1 || ((pad & (pad - 1)) != 0) ||
284         height < 1 || subsamp < 0 || subsamp >= TJ.NUMSAMP)
285       throw new Exception("Invalid argument in YUVImage::setBuf()");
286     if (yuvImage.length < TJ.bufSizeYUV(width, pad, height, subsamp))
287       throw new Exception("YUV image buffer is not large enough");
288 
289     int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
290     byte[][] planes = new byte[nc][];
291     int[] strides = new int[nc];
292     int[] offsets = new int[nc];
293 
294     planes[0] = yuvImage;
295     strides[0] = PAD(TJ.planeWidth(0, width, subsamp), pad);
296     if (subsamp != TJ.SAMP_GRAY) {
297       strides[1] = strides[2] = PAD(TJ.planeWidth(1, width, subsamp), pad);
298       planes[1] = planes[2] = yuvImage;
299       offsets[1] = offsets[0] +
300         strides[0] * TJ.planeHeight(0, height, subsamp);
301       offsets[2] = offsets[1] +
302         strides[1] * TJ.planeHeight(1, height, subsamp);
303     }
304 
305     yuvPad = pad;
306     setBuf(planes, offsets, width, strides, height, subsamp);
307   }
308 
309   /**
310    * Returns the width of the YUV image (or subregion.)
311    *
312    * @return the width of the YUV image (or subregion)
313    */
getWidth()314   public int getWidth() throws Exception {
315     if (yuvWidth < 1)
316       throw new Exception(NO_ASSOC_ERROR);
317     return yuvWidth;
318   }
319 
320   /**
321    * Returns the height of the YUV image (or subregion.)
322    *
323    * @return the height of the YUV image (or subregion)
324    */
getHeight()325   public int getHeight() throws Exception {
326     if (yuvHeight < 1)
327       throw new Exception(NO_ASSOC_ERROR);
328     return yuvHeight;
329   }
330 
331   /**
332    * Returns the line padding used in the YUV image buffer (if this image is
333    * stored in a unified buffer rather than separate image planes.)
334    *
335    * @return the line padding used in the YUV image buffer
336    */
getPad()337   public int getPad() throws Exception {
338     if (yuvPlanes == null)
339       throw new Exception(NO_ASSOC_ERROR);
340     if (yuvPad < 1 || ((yuvPad & (yuvPad - 1)) != 0))
341       throw new Exception("Image is not stored in a unified buffer");
342     return yuvPad;
343   }
344 
345   /**
346    * Returns the number of bytes per line of each plane in the YUV image.
347    *
348    * @return the number of bytes per line of each plane in the YUV image
349    */
getStrides()350   public int[] getStrides() throws Exception {
351     if (yuvStrides == null)
352       throw new Exception(NO_ASSOC_ERROR);
353     return yuvStrides;
354   }
355 
356   /**
357    * Returns the offsets (in bytes) of each plane within the planes of a larger
358    * YUV image.
359    *
360    * @return the offsets (in bytes) of each plane within the planes of a larger
361    * YUV image
362    */
getOffsets()363   public int[] getOffsets() throws Exception {
364     if (yuvOffsets == null)
365       throw new Exception(NO_ASSOC_ERROR);
366     return yuvOffsets;
367   }
368 
369   /**
370    * Returns the level of chrominance subsampling used in the YUV image.  See
371    * {@link TJ#SAMP_444 TJ.SAMP_*}.
372    *
373    * @return the level of chrominance subsampling used in the YUV image
374    */
getSubsamp()375   public int getSubsamp() throws Exception {
376     if (yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
377       throw new Exception(NO_ASSOC_ERROR);
378     return yuvSubsamp;
379   }
380 
381   /**
382    * Returns the YUV image planes.  If the image is stored in a unified buffer,
383    * then all image planes will point to that buffer.
384    *
385    * @return the YUV image planes
386    */
getPlanes()387   public byte[][] getPlanes() throws Exception {
388     if (yuvPlanes == null)
389       throw new Exception(NO_ASSOC_ERROR);
390     return yuvPlanes;
391   }
392 
393   /**
394    * Returns the YUV image buffer (if this image is stored in a unified
395    * buffer rather than separate image planes.)
396    *
397    * @return the YUV image buffer
398    */
getBuf()399   public byte[] getBuf() throws Exception {
400     if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
401       throw new Exception(NO_ASSOC_ERROR);
402     int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
403     for (int i = 1; i < nc; i++) {
404       if (yuvPlanes[i] != yuvPlanes[0])
405         throw new Exception("Image is not stored in a unified buffer");
406     }
407     return yuvPlanes[0];
408   }
409 
410   /**
411    * Returns the size (in bytes) of the YUV image buffer (if this image is
412    * stored in a unified buffer rather than separate image planes.)
413    *
414    * @return the size (in bytes) of the YUV image buffer
415    */
getSize()416   public int getSize() throws Exception {
417     if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
418       throw new Exception(NO_ASSOC_ERROR);
419     int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
420     if (yuvPad < 1)
421       throw new Exception("Image is not stored in a unified buffer");
422     for (int i = 1; i < nc; i++) {
423       if (yuvPlanes[i] != yuvPlanes[0])
424         throw new Exception("Image is not stored in a unified buffer");
425     }
426     return TJ.bufSizeYUV(yuvWidth, yuvPad, yuvHeight, yuvSubsamp);
427   }
428 
PAD(int v, int p)429   private static final int PAD(int v, int p) {
430     return (v + p - 1) & (~(p - 1));
431   }
432 
433   protected long handle = 0;
434   protected byte[][] yuvPlanes = null;
435   protected int[] yuvOffsets = null;
436   protected int[] yuvStrides = null;
437   protected int yuvPad = 0;
438   protected int yuvWidth = 0;
439   protected int yuvHeight = 0;
440   protected int yuvSubsamp = -1;
441 };
442