Debian Codegraph @

java/org/libjpegturbo/turbojpeg/TJTransformer.java

raw
1 /*
2 * Copyright (C)2011, 2013-2015, 2023 D. R. Commander. All Rights Reserved.
3 * Copyright (C)2015 Viktor Szathmáry. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of the libjpeg-turbo Project nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package org.libjpegturbo.turbojpeg;
31
32 /**
33 * TurboJPEG lossless transformer
34 */
35 public class TJTransformer extends TJDecompressor {
36
37 /**
38 * Create a TurboJPEG lossless transformer instance.
39 */
40 public TJTransformer() throws TJException {
41 init();
42 }
43
44 /**
45 * Create a TurboJPEG lossless transformer instance and associate the JPEG
46 * source image stored in <code>jpegImage</code> with the newly created
47 * instance.
48 *
49 * @param jpegImage buffer containing the JPEG source image to transform.
50 * (The size of the JPEG image is assumed to be the length of the array.)
51 * This buffer is not modified.
52 */
53 public TJTransformer(byte[] jpegImage) throws TJException {
54 init();
55 setSourceImage(jpegImage, jpegImage.length);
56 }
57
58 /**
59 * Create a TurboJPEG lossless transformer instance and associate the JPEG
60 * source image of length <code>imageSize</code> bytes stored in
61 * <code>jpegImage</code> with the newly created instance.
62 *
63 * @param jpegImage buffer containing the JPEG source image to transform.
64 * This buffer is not modified.
65 *
66 * @param imageSize size of the JPEG source image (in bytes)
67 */
68 public TJTransformer(byte[] jpegImage, int imageSize) throws TJException {
69 init();
70 setSourceImage(jpegImage, imageSize);
71 }
72
73 /**
74 * Embed an ICC (International Color Consortium) color management profile in
75 * JPEG images generated by subsequent transform operations.
76 *
77 * @param iccBuf buffer containing an ICC profile. (The size of the ICC
78 * profile is assumed to be the length of the array.) A copy is made of the
79 * ICC profile, so this buffer can be reused as soon as this method returns.
80 * Setting this parameter to null removes any ICC profile that was previously
81 * associated with this transformer instance.
82 */
83 public native void setICCProfile(byte[] iccBuf) throws TJException;
84
85 /**
86 * Returns the maximum size of the buffer (in bytes) required to hold a JPEG
87 * image transformed, with the given transform parameters and/or cropping
88 * region, from the JPEG source image associated with this transformer
89 * instance. This function is a wrapper for {@link TJ#bufSize TJ.bufSize()}
90 * that takes into account cropping, transposition of the width and height
91 * (which affects the destination image dimensions and level of chrominance
92 * subsampling), grayscale conversion, and the ICC profile (if any) that was
93 * previously associated with this transformer instance (see
94 * {@link #setICCProfile setICCProfile()}) or extracted from the source image
95 * (see {@link #getICCProfile} and {@link TJ#PARAM_SAVEMARKERS}.)
96 *
97 * @param transform a {@link TJTransform} instance that specifies the
98 * transform parameters and/or cropping region for the transformed JPEG
99 * image.
100 *
101 * @return the maximum size of the buffer (in bytes) required to hold a JPEG
102 * image transformed, with the given transform parameters and/or cropping
103 * region, from the JPEG source image associated with this transformer
104 * instance
105 */
106 public native int bufSize(TJTransform transform) throws TJException;
107
108 /**
109 * Losslessly transform the JPEG source image associated with this
110 * transformer instance into one or more JPEG images stored in the given
111 * destination buffers. Lossless transforms work by moving the raw
112 * coefficients from one JPEG image structure to another without altering the
113 * values of the coefficients. While this is typically faster than
114 * decompressing the image, transforming it, and re-compressing it, lossless
115 * transforms are not free. Each lossless transform requires reading and
116 * performing Huffman decoding on all of the coefficients in the source
117 * image, regardless of the size of the destination image. Thus, this method
118 * provides a means of generating multiple transformed images from the same
119 * source or of applying multiple transformations simultaneously, in order to
120 * eliminate the need to read the source coefficients multiple times.
121 *
122 * @param dstBufs an array of JPEG destination buffers.
123 * <code>dstbufs[i]</code> will receive a JPEG image that has been
124 * transformed using the parameters in <code>transforms[i]</code>. Use
125 * {@link #bufSize bufSize()} to determine the maximum size for each buffer.
126 *
127 * @param transforms an array of {@link TJTransform} instances, each of
128 * which specifies the transform parameters and/or cropping region for the
129 * corresponding transformed JPEG image
130 */
131 public void transform(byte[][] dstBufs, TJTransform[] transforms)
132 throws TJException {
133 transformedSizes = transform(getJPEGBuf(), getJPEGSize(), dstBufs,
134 transforms);
135 }
136
137 /**
138 * Losslessly transform the JPEG source image associated with this
139 * transformer instance and return an array of {@link TJDecompressor}
140 * instances, each of which has a transformed JPEG image associated with it.
141 *
142 * @param transforms an array of {@link TJTransform} instances, each of
143 * which specifies the transform parameters and/or cropping region for the
144 * corresponding transformed JPEG image
145 *
146 * @return an array of {@link TJDecompressor} instances, each of
147 * which has a transformed JPEG image associated with it.
148 */
149 public TJDecompressor[] transform(TJTransform[] transforms)
150 throws TJException {
151 byte[][] dstBufs = new byte[transforms.length][];
152 for (int i = 0; i < transforms.length; i++)
153 dstBufs[i] = new byte[bufSize(transforms[i])];
154 TJDecompressor[] tjd = new TJDecompressor[transforms.length];
155 transform(dstBufs, transforms);
156 for (int i = 0; i < transforms.length; i++)
157 tjd[i] = new TJDecompressor(dstBufs[i], transformedSizes[i]);
158 return tjd;
159 }
160
161 /**
162 * Returns an array containing the sizes of the transformed JPEG images
163 * (in bytes) generated by the most recent transform operation.
164 *
165 * @return an array containing the sizes of the transformed JPEG images
166 * (in bytes) generated by the most recent transform operation.
167 */
168 public int[] getTransformedSizes() {
169 if (transformedSizes == null)
170 throw new IllegalStateException("No image has been transformed yet");
171 return transformedSizes;
172 }
173
174 private native void init() throws TJException;
175
176 private native int[] transform(byte[] srcBuf, int srcSize, byte[][] dstBufs,
177 TJTransform[] transforms) throws TJException;
178
179 static {
180 TJLoader.load();
181 }
182
183 private int[] transformedSizes = null;
184 private int iccSize = 0;
185 }