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