| 1 |
/* |
| 2 |
* Copyright (C)2011, 2013, 2018, 2022-2024 D. R. Commander. |
| 3 |
* 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 |
import java.awt.*; |
| 33 |
|
| 34 |
/** |
| 35 |
* Lossless transform parameters |
| 36 |
*/ |
| 37 |
public class TJTransform extends Rectangle { |
| 38 |
|
| 39 |
private static final long serialVersionUID = -127367705761430371L; |
| 40 |
|
| 41 |
/** |
| 42 |
* The number of lossless transform operations |
| 43 |
*/ |
| 44 |
public static final int NUMOP = 8; |
| 45 |
/** |
| 46 |
* Do not transform the position of the image pixels. |
| 47 |
*/ |
| 48 |
public static final int OP_NONE = 0; |
| 49 |
/** |
| 50 |
* Flip (mirror) image horizontally. This transform is imperfect if there |
| 51 |
* are any partial iMCUs on the right edge. |
| 52 |
* @see #OPT_PERFECT |
| 53 |
*/ |
| 54 |
public static final int OP_HFLIP = 1; |
| 55 |
/** |
| 56 |
* Flip (mirror) image vertically. This transform is imperfect if there are |
| 57 |
* any partial iMCUs on the bottom edge. |
| 58 |
* @see #OPT_PERFECT |
| 59 |
*/ |
| 60 |
public static final int OP_VFLIP = 2; |
| 61 |
/** |
| 62 |
* Transpose image (flip/mirror along upper left to lower right axis). This |
| 63 |
* transform is always perfect. |
| 64 |
* @see #OPT_PERFECT |
| 65 |
*/ |
| 66 |
public static final int OP_TRANSPOSE = 3; |
| 67 |
/** |
| 68 |
* Transverse transpose image (flip/mirror along upper right to lower left |
| 69 |
* axis). This transform is imperfect if there are any partial iMCUs in the |
| 70 |
* image. |
| 71 |
* @see #OPT_PERFECT |
| 72 |
*/ |
| 73 |
public static final int OP_TRANSVERSE = 4; |
| 74 |
/** |
| 75 |
* Rotate image clockwise by 90 degrees. This transform is imperfect if |
| 76 |
* there are any partial iMCUs on the bottom edge. |
| 77 |
* @see #OPT_PERFECT |
| 78 |
*/ |
| 79 |
public static final int OP_ROT90 = 5; |
| 80 |
/** |
| 81 |
* Rotate image 180 degrees. This transform is imperfect if there are any |
| 82 |
* partial iMCUs in the image. |
| 83 |
* @see #OPT_PERFECT |
| 84 |
*/ |
| 85 |
public static final int OP_ROT180 = 6; |
| 86 |
/** |
| 87 |
* Rotate image counter-clockwise by 90 degrees. This transform is imperfect |
| 88 |
* if there are any partial iMCUs on the right edge. |
| 89 |
* @see #OPT_PERFECT |
| 90 |
*/ |
| 91 |
public static final int OP_ROT270 = 7; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* This option causes {@link TJTransformer#transform |
| 96 |
* TJTransformer.transform()} to throw an exception if the transform is not |
| 97 |
* perfect. Lossless transforms operate on iMCUs, the size of which depends |
| 98 |
* on the level of chrominance subsampling used. If the image's width or |
| 99 |
* height is not evenly divisible by the iMCU size (see {@link TJ#getMCUWidth |
| 100 |
* TJ.getMCUWidth()} and {@link TJ#getMCUHeight TJ.getMCUHeight()}), then |
| 101 |
* there will be partial iMCUs on the right and/or bottom edges. It is not |
| 102 |
* possible to move these partial iMCUs to the top or left of the image, so |
| 103 |
* any transform that would require that is "imperfect." If this option is |
| 104 |
* not specified, then any partial iMCUs that cannot be transformed will be |
| 105 |
* left in place, which will create odd-looking strips on the right or bottom |
| 106 |
* edge of the image. |
| 107 |
*/ |
| 108 |
public static final int OPT_PERFECT = (1 << 0); |
| 109 |
/** |
| 110 |
* Discard any partial iMCUs that cannot be transformed. |
| 111 |
*/ |
| 112 |
public static final int OPT_TRIM = (1 << 1); |
| 113 |
/** |
| 114 |
* Enable lossless cropping. |
| 115 |
*/ |
| 116 |
public static final int OPT_CROP = (1 << 2); |
| 117 |
/** |
| 118 |
* Discard the color data in the source image, and generate a grayscale |
| 119 |
* destination image. |
| 120 |
*/ |
| 121 |
public static final int OPT_GRAY = (1 << 3); |
| 122 |
/** |
| 123 |
* Do not generate a destination image. This can be used in conjunction with |
| 124 |
* a custom filter to capture the transformed DCT coefficients without |
| 125 |
* transcoding them. |
| 126 |
*/ |
| 127 |
public static final int OPT_NOOUTPUT = (1 << 4); |
| 128 |
/** |
| 129 |
* Generate a progressive destination image instead of a single-scan |
| 130 |
* destination image. Progressive JPEG images generally have better |
| 131 |
* compression ratios than single-scan JPEG images (much better if the image |
| 132 |
* has large areas of solid color), but progressive JPEG decompression is |
| 133 |
* considerably slower than single-scan JPEG decompression. Can be combined |
| 134 |
* with {@link #OPT_ARITHMETIC}. Implies {@link #OPT_OPTIMIZE} unless |
| 135 |
* {@link #OPT_ARITHMETIC} is also specified. |
| 136 |
*/ |
| 137 |
public static final int OPT_PROGRESSIVE = (1 << 5); |
| 138 |
/** |
| 139 |
* Do not copy any extra markers (including Exif and ICC profile data) from |
| 140 |
* the source image to the destination image. |
| 141 |
*/ |
| 142 |
public static final int OPT_COPYNONE = (1 << 6); |
| 143 |
/** |
| 144 |
* Enable arithmetic entropy coding in the destination image. Arithmetic |
| 145 |
* entropy coding generally improves compression relative to Huffman entropy |
| 146 |
* coding (the default), but it reduces decompression performance |
| 147 |
* considerably. Can be combined with {@link #OPT_PROGRESSIVE}. |
| 148 |
*/ |
| 149 |
public static final int OPT_ARITHMETIC = (1 << 7); |
| 150 |
/** |
| 151 |
* Enable Huffman table optimization for the destination image. Huffman |
| 152 |
* table optimization improves compression slightly (generally 5% or less.) |
| 153 |
*/ |
| 154 |
public static final int OPT_OPTIMIZE = (1 << 8); |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Create a new lossless transform instance. |
| 159 |
*/ |
| 160 |
public TJTransform() { |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Create a new lossless transform instance with the given parameters. |
| 165 |
* |
| 166 |
* @param x the left boundary of the cropping region. This must be evenly |
| 167 |
* divisible by the iMCU width (see {@link TJ#getMCUWidth TJ.getMCUWidth()}) |
| 168 |
* of the destination image. |
| 169 |
* |
| 170 |
* @param y the upper boundary of the cropping region. This must be evenly |
| 171 |
* divisible by the iMCU height (see {@link TJ#getMCUHeight |
| 172 |
* TJ.getMCUHeight()}) of the destination image. |
| 173 |
* |
| 174 |
* @param w the width of the cropping region. Setting this to 0 is the |
| 175 |
* equivalent of setting it to (width of the source JPEG image - |
| 176 |
* <code>x</code>). |
| 177 |
* |
| 178 |
* @param h the height of the cropping region. Setting this to 0 is the |
| 179 |
* equivalent of setting it to (height of the source JPEG image - |
| 180 |
* <code>y</code>). |
| 181 |
* |
| 182 |
* @param op one of the transform operations ({@link #OP_NONE OP_*}) |
| 183 |
* |
| 184 |
* @param options the bitwise OR of one or more of the transform options |
| 185 |
* ({@link #OPT_PERFECT OPT_*}) |
| 186 |
* |
| 187 |
* @param cf an instance of an object that implements the |
| 188 |
* {@link TJCustomFilter} interface, or null if no custom filter is needed |
| 189 |
*/ |
| 190 |
@SuppressWarnings("checkstyle:HiddenField") |
| 191 |
public TJTransform(int x, int y, int w, int h, int op, int options, |
| 192 |
TJCustomFilter cf) { |
| 193 |
super(x, y, w, h); |
| 194 |
this.op = op; |
| 195 |
this.options = options; |
| 196 |
this.cf = cf; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Create a new lossless transform instance with the given parameters. |
| 201 |
* |
| 202 |
* @param r a <code>java.awt.Rectangle</code> instance that specifies the |
| 203 |
* cropping region. See |
| 204 |
* {@link #TJTransform(int, int, int, int, int, int, TJCustomFilter)} for |
| 205 |
* more details. |
| 206 |
* |
| 207 |
* @param op one of the transform operations ({@link #OP_NONE OP_*}) |
| 208 |
* |
| 209 |
* @param options the bitwise OR of one or more of the transform options |
| 210 |
* ({@link #OPT_PERFECT OPT_*}) |
| 211 |
* |
| 212 |
* @param cf an instance of an object that implements the |
| 213 |
* {@link TJCustomFilter} interface, or null if no custom filter is needed |
| 214 |
*/ |
| 215 |
@SuppressWarnings("checkstyle:HiddenField") |
| 216 |
public TJTransform(Rectangle r, int op, int options, |
| 217 |
TJCustomFilter cf) { |
| 218 |
super(r); |
| 219 |
this.op = op; |
| 220 |
this.options = options; |
| 221 |
this.cf = cf; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Transform operation (one of {@link #OP_NONE OP_*}) |
| 226 |
*/ |
| 227 |
@SuppressWarnings("checkstyle:VisibilityModifier") |
| 228 |
public int op = 0; |
| 229 |
|
| 230 |
/** |
| 231 |
* Transform options (bitwise OR of one or more of |
| 232 |
* {@link #OPT_PERFECT OPT_*}) |
| 233 |
*/ |
| 234 |
@SuppressWarnings("checkstyle:VisibilityModifier") |
| 235 |
public int options = 0; |
| 236 |
|
| 237 |
/** |
| 238 |
* Custom filter instance |
| 239 |
*/ |
| 240 |
@SuppressWarnings("checkstyle:VisibilityModifier") |
| 241 |
public TJCustomFilter cf = null; |
| 242 |
} |