DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 1 | /* |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame^] | 2 | * Copyright (C)2011-2012, 2014-2015, 2017 D. R. Commander. |
| 3 | * All Rights Reserved. |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 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 | /* |
| 31 | * This program demonstrates how to compress and decompress JPEG files using |
| 32 | * the TurboJPEG JNI wrapper |
| 33 | */ |
| 34 | |
| 35 | import java.io.*; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 36 | import java.awt.*; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 37 | import java.awt.image.*; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 38 | import java.nio.*; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 39 | import javax.imageio.*; |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 40 | import javax.swing.*; |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 41 | import org.libjpegturbo.turbojpeg.*; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 42 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 43 | public class TJExample implements TJCustomFilter { |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 44 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 45 | public static final String classname = new TJExample().getClass().getName(); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 46 | |
DRC | 5528b55 | 2011-03-01 20:43:47 +0000 | [diff] [blame] | 47 | private static void usage() throws Exception { |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 48 | System.out.println("\nUSAGE: java " + classname + " <Input file> <Output file> [options]\n"); |
| 49 | System.out.println("Input and output files can be any image format that the Java Image I/O"); |
| 50 | System.out.println("extensions understand. If either filename ends in a .jpg extension, then"); |
| 51 | System.out.println("TurboJPEG will be used to compress or decompress the file.\n"); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 52 | System.out.println("Options:\n"); |
DRC | 5528b55 | 2011-03-01 20:43:47 +0000 | [diff] [blame] | 53 | System.out.println("-scale M/N = if the input image is a JPEG file, scale the width/height of the"); |
| 54 | System.out.print(" output image by a factor of M/N (M/N = "); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 55 | for (int i = 0; i < sf.length; i++) { |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 56 | System.out.print(sf[i].getNum() + "/" + sf[i].getDenom()); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 57 | if (sf.length == 2 && i != sf.length - 1) |
| 58 | System.out.print(" or "); |
| 59 | else if (sf.length > 2) { |
| 60 | if (i != sf.length - 1) |
| 61 | System.out.print(", "); |
| 62 | if (i == sf.length - 2) |
| 63 | System.out.print("or "); |
DRC | 5528b55 | 2011-03-01 20:43:47 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | System.out.println(")\n"); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 67 | System.out.println("-samp <444|422|420|gray> = If the output image is a JPEG file, this specifies"); |
| 68 | System.out.println(" the level of chrominance subsampling to use when"); |
| 69 | System.out.println(" recompressing it. Default is to use the same level"); |
| 70 | System.out.println(" of subsampling as the input, if the input is a JPEG"); |
| 71 | System.out.println(" file, or 4:4:4 otherwise.\n"); |
| 72 | System.out.println("-q <1-100> = If the output image is a JPEG file, this specifies the JPEG"); |
| 73 | System.out.println(" quality to use when recompressing it (default = 95).\n"); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 74 | System.out.println("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 ="); |
| 75 | System.out.println(" If the input image is a JPEG file, perform the corresponding lossless"); |
| 76 | System.out.println(" transform prior to decompression (these options are mutually exclusive)\n"); |
| 77 | System.out.println("-grayscale = If the input image is a JPEG file, perform lossless grayscale"); |
| 78 | System.out.println(" conversion prior to decompression (can be combined with the other"); |
| 79 | System.out.println(" transforms above)\n"); |
| 80 | System.out.println("-crop X,Y,WxH = If the input image is a JPEG file, perform lossless cropping"); |
| 81 | System.out.println(" prior to decompression. X,Y specifies the upper left corner of the"); |
| 82 | System.out.println(" cropping region, and WxH specifies its width and height. X,Y must be"); |
| 83 | System.out.println(" evenly divible by the MCU block size (8x8 if the source image was"); |
| 84 | System.out.println(" compressed using no subsampling or grayscale, or 16x8 for 4:2:2 or 16x16"); |
| 85 | System.out.println(" for 4:2:0.)\n"); |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 86 | System.out.println("-display = Display output image (Output file need not be specified in this"); |
| 87 | System.out.println(" case.)\n"); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 88 | System.out.println("-fastupsample = Use the fastest chrominance upsampling algorithm available in"); |
| 89 | System.out.println(" the underlying codec\n"); |
| 90 | System.out.println("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying"); |
| 91 | System.out.println(" codec\n"); |
| 92 | System.out.println("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the"); |
| 93 | System.out.println(" underlying codec\n"); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 94 | System.exit(1); |
| 95 | } |
| 96 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 97 | private static final String[] sampName = { |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame^] | 98 | "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1" |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 101 | public static void main(String[] argv) { |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 102 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 103 | BufferedImage img = null; |
| 104 | byte[] bmpBuf = null; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 105 | TJTransform xform = new TJTransform(); |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 106 | int flags = 0; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 107 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 108 | try { |
| 109 | |
DRC | 5528b55 | 2011-03-01 20:43:47 +0000 | [diff] [blame] | 110 | sf = TJ.getScalingFactors(); |
| 111 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 112 | if (argv.length < 2) { |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 113 | usage(); |
| 114 | } |
| 115 | |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 116 | TJScalingFactor scaleFactor = new TJScalingFactor(1, 1); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 117 | String inFormat = "jpg", outFormat = "jpg"; |
| 118 | int outSubsamp = -1, outQual = 95; |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 119 | boolean display = false; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 120 | |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame^] | 121 | if (argv[1].substring(0, 2).equalsIgnoreCase("-d")) |
| 122 | display = true; |
| 123 | |
| 124 | for (int i = 2; i < argv.length; i++) { |
| 125 | if (argv[i].length() < 2) |
| 126 | continue; |
| 127 | else if (argv[i].length() > 2 && |
| 128 | argv[i].substring(0, 3).equalsIgnoreCase("-sc")) { |
| 129 | int match = 0; |
| 130 | if (i < argv.length - 1) { |
| 131 | String[] scaleArg = argv[++i].split("/"); |
| 132 | if (scaleArg.length == 2) { |
| 133 | TJScalingFactor tempsf = |
| 134 | new TJScalingFactor(Integer.parseInt(scaleArg[0]), |
| 135 | Integer.parseInt(scaleArg[1])); |
| 136 | for (int j = 0; j < sf.length; j++) { |
| 137 | if (tempsf.equals(sf[j])) { |
| 138 | scaleFactor = sf[j]; |
| 139 | match = 1; |
| 140 | break; |
DRC | 5528b55 | 2011-03-01 20:43:47 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 143 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 144 | } |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame^] | 145 | if (match != 1) usage(); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 146 | } |
Leon Scroggins III | bd7903e | 2018-02-28 14:05:04 -0500 | [diff] [blame^] | 147 | else if (argv[i].length() > 2 && |
| 148 | argv[i].substring(0, 3).equalsIgnoreCase("-sa")) { |
| 149 | if (i < argv.length - 1) { |
| 150 | i++; |
| 151 | if (argv[i].substring(0, 1).equalsIgnoreCase("g")) |
| 152 | outSubsamp = TJ.SAMP_GRAY; |
| 153 | else if (argv[i].equals("444")) |
| 154 | outSubsamp = TJ.SAMP_444; |
| 155 | else if (argv[i].equals("422")) |
| 156 | outSubsamp = TJ.SAMP_422; |
| 157 | else if (argv[i].equals("420")) |
| 158 | outSubsamp = TJ.SAMP_420; |
| 159 | else |
| 160 | usage(); |
| 161 | } else |
| 162 | usage(); |
| 163 | } |
| 164 | else if (argv[i].substring(0, 2).equalsIgnoreCase("-q")) { |
| 165 | if (i < argv.length - 1) { |
| 166 | int qual = Integer.parseInt(argv[++i]); |
| 167 | if (qual >= 1 && qual <= 100) |
| 168 | outQual = qual; |
| 169 | else |
| 170 | usage(); |
| 171 | } else |
| 172 | usage(); |
| 173 | } |
| 174 | else if (argv[i].substring(0, 2).equalsIgnoreCase("-g")) |
| 175 | xform.options |= TJTransform.OPT_GRAY; |
| 176 | else if (argv[i].equalsIgnoreCase("-hflip")) |
| 177 | xform.op = TJTransform.OP_HFLIP; |
| 178 | else if (argv[i].equalsIgnoreCase("-vflip")) |
| 179 | xform.op = TJTransform.OP_VFLIP; |
| 180 | else if (argv[i].equalsIgnoreCase("-transpose")) |
| 181 | xform.op = TJTransform.OP_TRANSPOSE; |
| 182 | else if (argv[i].equalsIgnoreCase("-transverse")) |
| 183 | xform.op = TJTransform.OP_TRANSVERSE; |
| 184 | else if (argv[i].equalsIgnoreCase("-rot90")) |
| 185 | xform.op = TJTransform.OP_ROT90; |
| 186 | else if (argv[i].equalsIgnoreCase("-rot180")) |
| 187 | xform.op = TJTransform.OP_ROT180; |
| 188 | else if (argv[i].equalsIgnoreCase("-rot270")) |
| 189 | xform.op = TJTransform.OP_ROT270; |
| 190 | else if (argv[i].equalsIgnoreCase("-custom")) |
| 191 | xform.cf = new TJExample(); |
| 192 | else if (argv[i].length() > 2 && |
| 193 | argv[i].substring(0, 2).equalsIgnoreCase("-c")) { |
| 194 | if (i >= argv.length - 1) |
| 195 | usage(); |
| 196 | String[] cropArg = argv[++i].split(","); |
| 197 | if (cropArg.length != 3) |
| 198 | usage(); |
| 199 | String[] dimArg = cropArg[2].split("[xX]"); |
| 200 | if (dimArg.length != 2) |
| 201 | usage(); |
| 202 | int tempx = Integer.parseInt(cropArg[0]); |
| 203 | int tempy = Integer.parseInt(cropArg[1]); |
| 204 | int tempw = Integer.parseInt(dimArg[0]); |
| 205 | int temph = Integer.parseInt(dimArg[1]); |
| 206 | if (tempx < 0 || tempy < 0 || tempw < 0 || temph < 0) |
| 207 | usage(); |
| 208 | xform.x = tempx; |
| 209 | xform.y = tempy; |
| 210 | xform.width = tempw; |
| 211 | xform.height = temph; |
| 212 | xform.options |= TJTransform.OPT_CROP; |
| 213 | } |
| 214 | else if (argv[i].substring(0, 2).equalsIgnoreCase("-d")) |
| 215 | display = true; |
| 216 | else if (argv[i].equalsIgnoreCase("-fastupsample")) { |
| 217 | System.out.println("Using fast upsampling code"); |
| 218 | flags |= TJ.FLAG_FASTUPSAMPLE; |
| 219 | } |
| 220 | else if (argv[i].equalsIgnoreCase("-fastdct")) { |
| 221 | System.out.println("Using fastest DCT/IDCT algorithm"); |
| 222 | flags |= TJ.FLAG_FASTDCT; |
| 223 | } |
| 224 | else if (argv[i].equalsIgnoreCase("-accuratedct")) { |
| 225 | System.out.println("Using most accurate DCT/IDCT algorithm"); |
| 226 | flags |= TJ.FLAG_ACCURATEDCT; |
| 227 | } |
| 228 | else usage(); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 229 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 230 | String[] inFileTokens = argv[0].split("\\."); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 231 | if (inFileTokens.length > 1) |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 232 | inFormat = inFileTokens[inFileTokens.length - 1]; |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 233 | String[] outFileTokens; |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 234 | if (display) |
| 235 | outFormat = "bmp"; |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 236 | else { |
| 237 | outFileTokens = argv[1].split("\\."); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 238 | if (outFileTokens.length > 1) |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 239 | outFormat = outFileTokens[outFileTokens.length - 1]; |
| 240 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 241 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 242 | File file = new File(argv[0]); |
DRC | 4f8c295 | 2011-03-31 10:06:17 +0000 | [diff] [blame] | 243 | int width, height; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 244 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 245 | if (inFormat.equalsIgnoreCase("jpg")) { |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 246 | FileInputStream fis = new FileInputStream(file); |
| 247 | int inputSize = fis.available(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 248 | if (inputSize < 1) { |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 249 | System.out.println("Input file contains no data"); |
| 250 | System.exit(1); |
| 251 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 252 | byte[] inputBuf = new byte[inputSize]; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 253 | fis.read(inputBuf); |
| 254 | fis.close(); |
| 255 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 256 | TJDecompressor tjd; |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 257 | if (xform.op != TJTransform.OP_NONE || xform.options != 0 || |
| 258 | xform.cf != null) { |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 259 | TJTransformer tjt = new TJTransformer(inputBuf); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 260 | TJTransform[] t = new TJTransform[1]; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 261 | t[0] = xform; |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 262 | t[0].options |= TJTransform.OPT_TRIM; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 263 | TJDecompressor[] tjdx = tjt.transform(t, 0); |
| 264 | tjd = tjdx[0]; |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 265 | } else |
| 266 | tjd = new TJDecompressor(inputBuf); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 267 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 268 | width = tjd.getWidth(); |
| 269 | height = tjd.getHeight(); |
| 270 | int inSubsamp = tjd.getSubsamp(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 271 | System.out.println("Source Image: " + width + " x " + height + |
| 272 | " pixels, " + sampName[inSubsamp] + " subsampling"); |
| 273 | if (outSubsamp < 0) |
| 274 | outSubsamp = inSubsamp; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 275 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 276 | if (outFormat.equalsIgnoreCase("jpg") && |
| 277 | (xform.op != TJTransform.OP_NONE || xform.options != 0) && |
| 278 | scaleFactor.isOne()) { |
DRC | 7d4b001 | 2011-03-04 13:40:42 +0000 | [diff] [blame] | 279 | file = new File(argv[1]); |
| 280 | FileOutputStream fos = new FileOutputStream(file); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame] | 281 | fos.write(tjd.getJPEGBuf(), 0, tjd.getJPEGSize()); |
DRC | 7d4b001 | 2011-03-04 13:40:42 +0000 | [diff] [blame] | 282 | fos.close(); |
| 283 | System.exit(0); |
| 284 | } |
| 285 | |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 286 | width = scaleFactor.getScaled(width); |
| 287 | height = scaleFactor.getScaled(height); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 288 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 289 | if (!outFormat.equalsIgnoreCase("jpg")) |
DRC | 73d74c1 | 2012-06-29 23:46:38 +0000 | [diff] [blame] | 290 | img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB, |
| 291 | flags); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 292 | else |
| 293 | bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 294 | tjd.close(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 295 | } else { |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 296 | img = ImageIO.read(file); |
DRC | 98ca1c3 | 2013-01-19 06:43:27 +0000 | [diff] [blame] | 297 | if (img == null) |
| 298 | throw new Exception("Input image type not supported."); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 299 | width = img.getWidth(); |
| 300 | height = img.getHeight(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 301 | if (outSubsamp < 0) { |
| 302 | if (img.getType() == BufferedImage.TYPE_BYTE_GRAY) |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 303 | outSubsamp = TJ.SAMP_GRAY; |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 304 | else |
| 305 | outSubsamp = TJ.SAMP_444; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 306 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 307 | } |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 308 | System.gc(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 309 | if (!display) |
| 310 | System.out.print("Dest. Image (" + outFormat + "): " + width + " x " + |
| 311 | height + " pixels"); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 312 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 313 | if (display) { |
DRC | 16c7077 | 2011-03-07 09:59:08 +0000 | [diff] [blame] | 314 | ImageIcon icon = new ImageIcon(img); |
| 315 | JLabel label = new JLabel(icon, JLabel.CENTER); |
| 316 | JOptionPane.showMessageDialog(null, label, "Output Image", |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 317 | JOptionPane.PLAIN_MESSAGE); |
| 318 | } else if (outFormat.equalsIgnoreCase("jpg")) { |
| 319 | System.out.println(", " + sampName[outSubsamp] + |
| 320 | " subsampling, quality = " + outQual); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 321 | TJCompressor tjc = new TJCompressor(); |
| 322 | int jpegSize; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 323 | byte[] jpegBuf; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 324 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 325 | tjc.setSubsamp(outSubsamp); |
| 326 | tjc.setJPEGQuality(outQual); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 327 | if (img != null) |
DRC | b148139 | 2014-03-14 08:53:33 +0000 | [diff] [blame] | 328 | tjc.setSourceImage(img, 0, 0, 0, 0); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 329 | else { |
DRC | b148139 | 2014-03-14 08:53:33 +0000 | [diff] [blame] | 330 | tjc.setSourceImage(bmpBuf, 0, 0, width, 0, height, TJ.PF_BGRX); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 331 | } |
DRC | b148139 | 2014-03-14 08:53:33 +0000 | [diff] [blame] | 332 | jpegBuf = tjc.compress(flags); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 333 | jpegSize = tjc.getCompressedSize(); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 334 | tjc.close(); |
| 335 | |
| 336 | file = new File(argv[1]); |
| 337 | FileOutputStream fos = new FileOutputStream(file); |
| 338 | fos.write(jpegBuf, 0, jpegSize); |
| 339 | fos.close(); |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 340 | } else { |
DRC | 0ad78a6 | 2011-02-23 20:57:17 +0000 | [diff] [blame] | 341 | System.out.print("\n"); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 342 | file = new File(argv[1]); |
| 343 | ImageIO.write(img, outFormat, file); |
| 344 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 345 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 346 | } catch(Exception e) { |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 347 | e.printStackTrace(); |
| 348 | System.exit(-1); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 352 | public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion, |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 353 | Rectangle planeRegion, int componentIndex, |
| 354 | int transformIndex, TJTransform transform) |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 355 | throws TJException { |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 356 | for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) { |
| 357 | coeffBuffer.put(i, (short)(-coeffBuffer.get(i))); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
DRC | 67bee86 | 2013-04-27 12:36:07 +0000 | [diff] [blame] | 361 | static TJScalingFactor[] sf = null; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 362 | }; |