|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C)2014, 2017 D. R. Commander. All Rights Reserved.
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C)2015 Viktor Szathmรกry. All Rights Reserved.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Redistribution and use in source and binary forms, with or without
|
|
shun-iwasawa |
82a8f5 |
* modification, are permitted provided that the following conditions are met:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* - Redistributions of source code must retain the above copyright notice,
|
|
shun-iwasawa |
82a8f5 |
* this list of conditions and the following disclaimer.
|
|
shun-iwasawa |
82a8f5 |
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
shun-iwasawa |
82a8f5 |
* this list of conditions and the following disclaimer in the documentation
|
|
shun-iwasawa |
82a8f5 |
* and/or other materials provided with the distribution.
|
|
shun-iwasawa |
82a8f5 |
* - Neither the name of the libjpeg-turbo Project nor the names of its
|
|
shun-iwasawa |
82a8f5 |
* contributors may be used to endorse or promote products derived from this
|
|
shun-iwasawa |
82a8f5 |
* software without specific prior written permission.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
|
|
shun-iwasawa |
82a8f5 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
shun-iwasawa |
82a8f5 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
shun-iwasawa |
82a8f5 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
|
shun-iwasawa |
82a8f5 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
shun-iwasawa |
82a8f5 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
shun-iwasawa |
82a8f5 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
shun-iwasawa |
82a8f5 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
shun-iwasawa |
82a8f5 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
shun-iwasawa |
82a8f5 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
shun-iwasawa |
82a8f5 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
package org.libjpegturbo.turbojpeg;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* This class encapsulates a YUV planar image and the metadata
|
|
shun-iwasawa |
82a8f5 |
* associated with it. The TurboJPEG API allows both the JPEG compression and
|
|
shun-iwasawa |
82a8f5 |
* decompression pipelines to be split into stages: YUV encode, compress from
|
|
shun-iwasawa |
82a8f5 |
* YUV, decompress to YUV, and YUV decode. A YUVImage instance
|
|
shun-iwasawa |
82a8f5 |
* serves as the destination image for YUV encode and decompress-to-YUV
|
|
shun-iwasawa |
82a8f5 |
* operations and as the source image for compress-from-YUV and YUV decode
|
|
shun-iwasawa |
82a8f5 |
* operations.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Technically, the JPEG format uses the YCbCr colorspace (which technically is
|
|
shun-iwasawa |
82a8f5 |
* not a "colorspace" but rather a "color transform"), but per the convention
|
|
shun-iwasawa |
82a8f5 |
* of the digital video community, the TurboJPEG API uses "YUV" to refer to an
|
|
shun-iwasawa |
82a8f5 |
* image format consisting of Y, Cb, and Cr image planes.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Each plane is simply a 2D array of bytes, each byte representing the value
|
|
shun-iwasawa |
82a8f5 |
* of one of the components (Y, Cb, or Cr) at a particular location in the
|
|
shun-iwasawa |
82a8f5 |
* image. The width and height of each plane are determined by the image
|
|
shun-iwasawa |
82a8f5 |
* width, height, and level of chrominance subsampling. The luminance plane
|
|
shun-iwasawa |
82a8f5 |
* width is the image width padded to the nearest multiple of the horizontal
|
|
shun-iwasawa |
82a8f5 |
* subsampling factor (2 in the case of 4:2:0 and 4:2:2, 4 in the case of
|
|
shun-iwasawa |
82a8f5 |
* 4:1:1, 1 in the case of 4:4:4 or grayscale.) Similarly, the luminance plane
|
|
shun-iwasawa |
82a8f5 |
* height is the image height padded to the nearest multiple of the vertical
|
|
shun-iwasawa |
82a8f5 |
* subsampling factor (2 in the case of 4:2:0 or 4:4:0, 1 in the case of 4:4:4
|
|
shun-iwasawa |
82a8f5 |
* or grayscale.) The chrominance plane width is equal to the luminance plane
|
|
shun-iwasawa |
82a8f5 |
* width divided by the horizontal subsampling factor, and the chrominance
|
|
shun-iwasawa |
82a8f5 |
* plane height is equal to the luminance plane height divided by the vertical
|
|
shun-iwasawa |
82a8f5 |
* subsampling factor.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
|
|
shun-iwasawa |
82a8f5 |
* used, then the luminance plane would be 36 x 35 bytes, and each of the
|
|
shun-iwasawa |
82a8f5 |
* chrominance planes would be 18 x 35 bytes. If you specify a line padding of
|
|
shun-iwasawa |
82a8f5 |
* 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes, and
|
|
shun-iwasawa |
82a8f5 |
* each of the chrominance planes would be 20 x 35 bytes.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public class YUVImage {
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
private static final String NO_ASSOC_ERROR =
|
|
shun-iwasawa |
82a8f5 |
"No image data is associated with this instance";
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Create a new YUVImage instance backed by separate image
|
|
shun-iwasawa |
82a8f5 |
* planes, and allocate memory for the image planes.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param strides an array of integers, each specifying the number of bytes
|
|
shun-iwasawa |
82a8f5 |
* per line in the corresponding plane of the YUV image. Setting the stride
|
|
shun-iwasawa |
82a8f5 |
* for any plane to 0 is the same as setting it to the plane width (see
|
|
shun-iwasawa |
82a8f5 |
* {@link YUVImage above}.) If strides is null, then the
|
|
shun-iwasawa |
82a8f5 |
* strides for all planes will be set to their respective plane widths. When
|
|
shun-iwasawa |
82a8f5 |
* using this constructor, the stride for each plane must be equal to or
|
|
shun-iwasawa |
82a8f5 |
* greater than the plane width.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling to be used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public YUVImage(int width, int[] strides, int height, int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
setBuf(null, null, width, strides, height, subsamp, true);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Create a new YUVImage instance backed by a unified image
|
|
shun-iwasawa |
82a8f5 |
* buffer, and allocate memory for the image buffer.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param pad Each line of each plane in the YUV image buffer will be padded
|
|
shun-iwasawa |
82a8f5 |
* to this number of bytes (must be a power of 2.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling to be used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public YUVImage(int width, int pad, int height, int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
setBuf(new byte[TJ.bufSizeYUV(width, pad, height, subsamp)], width, pad,
|
|
shun-iwasawa |
82a8f5 |
height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Create a new YUVImage instance from a set of existing image
|
|
shun-iwasawa |
82a8f5 |
* planes.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
|
|
shun-iwasawa |
82a8f5 |
* image planes (or just the Y plane, if the image is grayscale.) These
|
|
shun-iwasawa |
82a8f5 |
* planes can be contiguous or non-contiguous in memory. Plane
|
|
shun-iwasawa |
82a8f5 |
* i should be at least offsets[i] +
|
|
shun-iwasawa |
82a8f5 |
* {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)
|
|
shun-iwasawa |
82a8f5 |
* bytes in size.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param offsets If this YUVImage instance represents a
|
|
shun-iwasawa |
82a8f5 |
* subregion of a larger image, then offsets[i] specifies the
|
|
shun-iwasawa |
82a8f5 |
* offset (in bytes) of the subregion within plane i of the
|
|
shun-iwasawa |
82a8f5 |
* larger image. Setting this to null is the same as setting the offsets for
|
|
shun-iwasawa |
82a8f5 |
* all planes to 0.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the new YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param strides an array of integers, each specifying the number of bytes
|
|
shun-iwasawa |
82a8f5 |
* per line in the corresponding plane of the YUV image. Setting the stride
|
|
shun-iwasawa |
82a8f5 |
* for any plane to 0 is the same as setting it to the plane width (see
|
|
shun-iwasawa |
82a8f5 |
* {@link YUVImage above}.) If strides is null, then the
|
|
shun-iwasawa |
82a8f5 |
* strides for all planes will be set to their respective plane widths. You
|
|
shun-iwasawa |
82a8f5 |
* can adjust the strides in order to add an arbitrary amount of line padding
|
|
shun-iwasawa |
82a8f5 |
* to each plane or to specify that this YUVImage instance is a
|
|
shun-iwasawa |
82a8f5 |
* subregion of a larger image (in which case, strides[i] should
|
|
shun-iwasawa |
82a8f5 |
* be set to the plane width of plane i in the larger image.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the new YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public YUVImage(byte[][] planes, int[] offsets, int width, int[] strides,
|
|
shun-iwasawa |
82a8f5 |
int height, int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
setBuf(planes, offsets, width, strides, height, subsamp, false);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Create a new YUVImage instance from an existing unified image
|
|
shun-iwasawa |
82a8f5 |
* buffer.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param yuvImage image buffer that contains or will contain YUV planar
|
|
shun-iwasawa |
82a8f5 |
* image data. Use {@link TJ#bufSizeYUV} to determine the minimum size for
|
|
shun-iwasawa |
82a8f5 |
* this buffer. The Y, U (Cb), and V (Cr) image planes are stored
|
|
shun-iwasawa |
82a8f5 |
* sequentially in the buffer (see {@link YUVImage above} for a description
|
|
shun-iwasawa |
82a8f5 |
* of the image format.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param pad the line padding used in the YUV image buffer. For
|
|
shun-iwasawa |
82a8f5 |
* instance, if each line in each plane of the buffer is padded to the
|
|
shun-iwasawa |
82a8f5 |
* nearest multiple of 4 bytes, then pad should be set to 4.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public YUVImage(byte[] yuvImage, int width, int pad, int height,
|
|
shun-iwasawa |
82a8f5 |
int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
setBuf(yuvImage, width, pad, height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Assign a set of image planes to this YUVImage instance.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param planes an array of buffers representing the Y, U (Cb), and V (Cr)
|
|
shun-iwasawa |
82a8f5 |
* image planes (or just the Y plane, if the image is grayscale.) These
|
|
shun-iwasawa |
82a8f5 |
* planes can be contiguous or non-contiguous in memory. Plane
|
|
shun-iwasawa |
82a8f5 |
* i should be at least offsets[i] +
|
|
shun-iwasawa |
82a8f5 |
* {@link TJ#planeSizeYUV TJ.planeSizeYUV}(i, width, strides[i], height, subsamp)
|
|
shun-iwasawa |
82a8f5 |
* bytes in size.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param offsets If this YUVImage instance represents a
|
|
shun-iwasawa |
82a8f5 |
* subregion of a larger image, then offsets[i] specifies the
|
|
shun-iwasawa |
82a8f5 |
* offset (in bytes) of the subregion within plane i of the
|
|
shun-iwasawa |
82a8f5 |
* larger image. Setting this to null is the same as setting the offsets for
|
|
shun-iwasawa |
82a8f5 |
* all planes to 0.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param strides an array of integers, each specifying the number of bytes
|
|
shun-iwasawa |
82a8f5 |
* per line in the corresponding plane of the YUV image. Setting the stride
|
|
shun-iwasawa |
82a8f5 |
* for any plane to 0 is the same as setting it to the plane width (see
|
|
shun-iwasawa |
82a8f5 |
* {@link YUVImage above}.) If strides is null, then the
|
|
shun-iwasawa |
82a8f5 |
* strides for all planes will be set to their respective plane widths. You
|
|
shun-iwasawa |
82a8f5 |
* can adjust the strides in order to add an arbitrary amount of line padding
|
|
shun-iwasawa |
82a8f5 |
* to each plane or to specify that this YUVImage image is a
|
|
shun-iwasawa |
82a8f5 |
* subregion of a larger image (in which case, strides[i] should
|
|
shun-iwasawa |
82a8f5 |
* be set to the plane width of plane i in the larger image.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public void setBuf(byte[][] planes, int[] offsets, int width, int[] strides,
|
|
shun-iwasawa |
82a8f5 |
int height, int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
setBuf(planes, offsets, width, strides, height, subsamp, false);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
private void setBuf(byte[][] planes, int[] offsets, int width, int[] strides,
|
|
shun-iwasawa |
82a8f5 |
int height, int subsamp, boolean alloc) {
|
|
shun-iwasawa |
82a8f5 |
if ((planes == null && !alloc) || width < 1 || height < 1 || subsamp < 0 ||
|
|
shun-iwasawa |
82a8f5 |
subsamp >= TJ.NUMSAMP)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
|
|
shun-iwasawa |
82a8f5 |
if ((planes != null && planes.length != nc) ||
|
|
shun-iwasawa |
82a8f5 |
(offsets != null && offsets.length != nc) ||
|
|
shun-iwasawa |
82a8f5 |
(strides != null && strides.length != nc))
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("YUVImage::setBuf(): planes, offsets, or strides array is the wrong size");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (planes == null)
|
|
shun-iwasawa |
82a8f5 |
planes = new byte[nc][];
|
|
shun-iwasawa |
82a8f5 |
if (offsets == null)
|
|
shun-iwasawa |
82a8f5 |
offsets = new int[nc];
|
|
shun-iwasawa |
82a8f5 |
if (strides == null)
|
|
shun-iwasawa |
82a8f5 |
strides = new int[nc];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (int i = 0; i < nc; i++) {
|
|
shun-iwasawa |
82a8f5 |
int pw = TJ.planeWidth(i, width, subsamp);
|
|
shun-iwasawa |
82a8f5 |
int ph = TJ.planeHeight(i, height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
int planeSize = TJ.planeSizeYUV(i, width, strides[i], height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (strides[i] == 0)
|
|
shun-iwasawa |
82a8f5 |
strides[i] = pw;
|
|
shun-iwasawa |
82a8f5 |
if (alloc) {
|
|
shun-iwasawa |
82a8f5 |
if (strides[i] < pw)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Stride must be >= plane width when allocating a new YUV image");
|
|
shun-iwasawa |
82a8f5 |
planes[i] = new byte[strides[i] * ph];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (planes[i] == null || offsets[i] < 0)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
|
|
shun-iwasawa |
82a8f5 |
if (strides[i] < 0 && offsets[i] - planeSize + pw < 0)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Stride for plane " + i +
|
|
shun-iwasawa |
82a8f5 |
" would cause memory to be accessed below plane boundary");
|
|
shun-iwasawa |
82a8f5 |
if (planes[i].length < offsets[i] + planeSize)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Image plane " + i +
|
|
shun-iwasawa |
82a8f5 |
" is not large enough");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
yuvPlanes = planes;
|
|
shun-iwasawa |
82a8f5 |
yuvOffsets = offsets;
|
|
shun-iwasawa |
82a8f5 |
yuvWidth = width;
|
|
shun-iwasawa |
82a8f5 |
yuvStrides = strides;
|
|
shun-iwasawa |
82a8f5 |
yuvHeight = height;
|
|
shun-iwasawa |
82a8f5 |
yuvSubsamp = subsamp;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Assign a unified image buffer to this YUVImage instance.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param yuvImage image buffer that contains or will contain YUV planar
|
|
shun-iwasawa |
82a8f5 |
* image data. Use {@link TJ#bufSizeYUV} to determine the minimum size for
|
|
shun-iwasawa |
82a8f5 |
* this buffer. The Y, U (Cb), and V (Cr) image planes are stored
|
|
shun-iwasawa |
82a8f5 |
* sequentially in the buffer (see {@link YUVImage above} for a description
|
|
shun-iwasawa |
82a8f5 |
* of the image format.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param width width (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param pad the line padding used in the YUV image buffer. For
|
|
shun-iwasawa |
82a8f5 |
* instance, if each line in each plane of the buffer is padded to the
|
|
shun-iwasawa |
82a8f5 |
* nearest multiple of 4 bytes, then pad should be set to 4.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param height height (in pixels) of the YUV image
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @param subsamp the level of chrominance subsampling used in the YUV
|
|
shun-iwasawa |
82a8f5 |
* image (one of {@link TJ#SAMP_444 TJ.SAMP_*})
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public void setBuf(byte[] yuvImage, int width, int pad, int height,
|
|
shun-iwasawa |
82a8f5 |
int subsamp) {
|
|
shun-iwasawa |
82a8f5 |
if (yuvImage == null || width < 1 || pad < 1 || ((pad & (pad - 1)) != 0) ||
|
|
shun-iwasawa |
82a8f5 |
height < 1 || subsamp < 0 || subsamp >= TJ.NUMSAMP)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("Invalid argument in YUVImage::setBuf()");
|
|
shun-iwasawa |
82a8f5 |
if (yuvImage.length < TJ.bufSizeYUV(width, pad, height, subsamp))
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalArgumentException("YUV image buffer is not large enough");
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
int nc = (subsamp == TJ.SAMP_GRAY ? 1 : 3);
|
|
shun-iwasawa |
82a8f5 |
byte[][] planes = new byte[nc][];
|
|
shun-iwasawa |
82a8f5 |
int[] strides = new int[nc];
|
|
shun-iwasawa |
82a8f5 |
int[] offsets = new int[nc];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
planes[0] = yuvImage;
|
|
shun-iwasawa |
82a8f5 |
strides[0] = pad(TJ.planeWidth(0, width, subsamp), pad);
|
|
shun-iwasawa |
82a8f5 |
if (subsamp != TJ.SAMP_GRAY) {
|
|
shun-iwasawa |
82a8f5 |
strides[1] = strides[2] = pad(TJ.planeWidth(1, width, subsamp), pad);
|
|
shun-iwasawa |
82a8f5 |
planes[1] = planes[2] = yuvImage;
|
|
shun-iwasawa |
82a8f5 |
offsets[1] = offsets[0] +
|
|
shun-iwasawa |
82a8f5 |
strides[0] * TJ.planeHeight(0, height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
offsets[2] = offsets[1] +
|
|
shun-iwasawa |
82a8f5 |
strides[1] * TJ.planeHeight(1, height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
yuvPad = pad;
|
|
shun-iwasawa |
82a8f5 |
setBuf(planes, offsets, width, strides, height, subsamp);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the width of the YUV image (or subregion.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the width of the YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int getWidth() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvWidth < 1)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvWidth;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the height of the YUV image (or subregion.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the height of the YUV image (or subregion)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int getHeight() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvHeight < 1)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvHeight;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the line padding used in the YUV image buffer (if this image is
|
|
shun-iwasawa |
82a8f5 |
* stored in a unified buffer rather than separate image planes.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the line padding used in the YUV image buffer
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int getPad() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes == null)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
if (yuvPad < 1 || ((yuvPad & (yuvPad - 1)) != 0))
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException("Image is not stored in a unified buffer");
|
|
shun-iwasawa |
82a8f5 |
return yuvPad;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the number of bytes per line of each plane in the YUV image.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the number of bytes per line of each plane in the YUV image
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int[] getStrides() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvStrides == null)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvStrides;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the offsets (in bytes) of each plane within the planes of a larger
|
|
shun-iwasawa |
82a8f5 |
* YUV image.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the offsets (in bytes) of each plane within the planes of a larger
|
|
shun-iwasawa |
82a8f5 |
* YUV image
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int[] getOffsets() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvOffsets == null)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvOffsets;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the level of chrominance subsampling used in the YUV image. See
|
|
shun-iwasawa |
82a8f5 |
* {@link TJ#SAMP_444 TJ.SAMP_*}.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the level of chrominance subsampling used in the YUV image
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int getSubsamp() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvSubsamp;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the YUV image planes. If the image is stored in a unified buffer,
|
|
shun-iwasawa |
82a8f5 |
* then all image planes will point to that buffer.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the YUV image planes
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public byte[][] getPlanes() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes == null)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
return yuvPlanes;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the YUV image buffer (if this image is stored in a unified
|
|
shun-iwasawa |
82a8f5 |
* buffer rather than separate image planes.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the YUV image buffer
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public byte[] getBuf() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
|
|
shun-iwasawa |
82a8f5 |
for (int i = 1; i < nc; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes[i] != yuvPlanes[0])
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException("Image is not stored in a unified buffer");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
return yuvPlanes[0];
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/**
|
|
shun-iwasawa |
82a8f5 |
* Returns the size (in bytes) of the YUV image buffer (if this image is
|
|
shun-iwasawa |
82a8f5 |
* stored in a unified buffer rather than separate image planes.)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* @return the size (in bytes) of the YUV image buffer
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
public int getSize() {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes == null || yuvSubsamp < 0 || yuvSubsamp >= TJ.NUMSAMP)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException(NO_ASSOC_ERROR);
|
|
shun-iwasawa |
82a8f5 |
int nc = (yuvSubsamp == TJ.SAMP_GRAY ? 1 : 3);
|
|
shun-iwasawa |
82a8f5 |
if (yuvPad < 1)
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException("Image is not stored in a unified buffer");
|
|
shun-iwasawa |
82a8f5 |
for (int i = 1; i < nc; i++) {
|
|
shun-iwasawa |
82a8f5 |
if (yuvPlanes[i] != yuvPlanes[0])
|
|
shun-iwasawa |
82a8f5 |
throw new IllegalStateException("Image is not stored in a unified buffer");
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
return TJ.bufSizeYUV(yuvWidth, yuvPad, yuvHeight, yuvSubsamp);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
private static int pad(int v, int p) {
|
|
shun-iwasawa |
82a8f5 |
return (v + p - 1) & (~(p - 1));
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
protected long handle = 0;
|
|
shun-iwasawa |
82a8f5 |
protected byte[][] yuvPlanes = null;
|
|
shun-iwasawa |
82a8f5 |
protected int[] yuvOffsets = null;
|
|
shun-iwasawa |
82a8f5 |
protected int[] yuvStrides = null;
|
|
shun-iwasawa |
82a8f5 |
protected int yuvPad = 0;
|
|
shun-iwasawa |
82a8f5 |
protected int yuvWidth = 0;
|
|
shun-iwasawa |
82a8f5 |
protected int yuvHeight = 0;
|
|
shun-iwasawa |
82a8f5 |
protected int yuvSubsamp = -1;
|
|
shun-iwasawa |
82a8f5 |
}
|