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