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 | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 37 | import org.libjpegturbo.turbojpeg.*; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 38 | |
DRC | 2413cb8 | 2011-02-08 02:11:37 +0000 | [diff] [blame] | 39 | public class TJExample { |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 40 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 41 | public static final String classname = new TJExample().getClass().getName(); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 42 | |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 43 | private static void usage() { |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 44 | System.out.println("\nUSAGE: java " + classname + " <Input file> <Output file> [options]\n"); |
| 45 | System.out.println("Input and output files can be any image format that the Java Image I/O"); |
| 46 | System.out.println("extensions understand. If either filename ends in a .jpg extension, then"); |
| 47 | 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] | 48 | System.out.println("Options:\n"); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 49 | System.out.println("-scale 1/N = if the input image is a JPEG file, scale the width/height of the"); |
| 50 | System.out.println(" output image by a factor of 1/N (N = 1, 2, 4, or 8}\n"); |
| 51 | System.out.println("-samp <444|422|420|gray> = If the output image is a JPEG file, this specifies"); |
| 52 | System.out.println(" the level of chrominance subsampling to use when"); |
| 53 | System.out.println(" recompressing it. Default is to use the same level"); |
| 54 | System.out.println(" of subsampling as the input, if the input is a JPEG"); |
| 55 | System.out.println(" file, or 4:4:4 otherwise.\n"); |
| 56 | System.out.println("-q <1-100> = If the output image is a JPEG file, this specifies the JPEG"); |
| 57 | System.out.println(" quality to use when recompressing it (default = 95).\n"); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 58 | System.exit(1); |
| 59 | } |
| 60 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 61 | private final static String sampName[] = { |
| 62 | "4:4:4", "4:2:2", "4:2:0", "Grayscale" |
| 63 | }; |
| 64 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 65 | public static void main(String argv[]) { |
| 66 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 67 | BufferedImage img = null; byte [] bmpBuf = null; |
| 68 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 69 | try { |
| 70 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 71 | if(argv.length < 2) { |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 72 | usage(); |
| 73 | } |
| 74 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 75 | int scaleFactor = 1; |
| 76 | String inFormat = "jpg", outFormat = "jpg"; |
| 77 | int outSubsamp = -1, outQual = 95; |
| 78 | |
| 79 | if(argv.length > 2) { |
| 80 | for(int i = 2; i < argv.length; i++) { |
| 81 | if(argv[i].length() < 2) continue; |
| 82 | if(argv[i].length() > 2 |
| 83 | && argv[i].substring(0, 3).equalsIgnoreCase("-sc")) { |
| 84 | if(i < argv.length - 1) { |
| 85 | String [] scaleArg = argv[++i].split("/"); |
| 86 | if(scaleArg.length != 2 || Integer.parseInt(scaleArg[0]) != 1 |
| 87 | || (scaleFactor = Integer.parseInt(scaleArg[1])) < 1 |
| 88 | || scaleFactor > 8 || (scaleFactor & (scaleFactor - 1)) != 0) |
| 89 | usage(); |
| 90 | } |
| 91 | else usage(); |
| 92 | } |
| 93 | if(argv[i].substring(0, 2).equalsIgnoreCase("-h") |
| 94 | || argv[i].equalsIgnoreCase("-?")) |
| 95 | usage(); |
| 96 | if(argv[i].length() > 2 |
| 97 | && argv[i].substring(0, 3).equalsIgnoreCase("-sa")) { |
| 98 | if(i < argv.length - 1) { |
| 99 | i++; |
| 100 | if(argv[i].substring(0, 1).equalsIgnoreCase("g")) |
| 101 | outSubsamp = TJ.SAMP_GRAY; |
| 102 | else if(argv[i].equals("444")) outSubsamp = TJ.SAMP_444; |
| 103 | else if(argv[i].equals("422")) outSubsamp = TJ.SAMP_422; |
| 104 | else if(argv[i].equals("420")) outSubsamp = TJ.SAMP_420; |
| 105 | else usage(); |
| 106 | } |
| 107 | else usage(); |
| 108 | } |
| 109 | if(argv[i].substring(0, 2).equalsIgnoreCase("-q")) { |
| 110 | if(i < argv.length - 1) { |
| 111 | int qual = Integer.parseInt(argv[++i]); |
| 112 | if(qual >= 1 && qual <= 100) outQual = qual; |
| 113 | else usage(); |
| 114 | } |
| 115 | else usage(); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 118 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 119 | String [] inFileTokens = argv[0].split("\\."); |
| 120 | if(inFileTokens.length > 1) |
| 121 | inFormat = inFileTokens[inFileTokens.length - 1]; |
| 122 | String [] outFileTokens = argv[1].split("\\."); |
| 123 | if(outFileTokens.length > 1) |
| 124 | outFormat = outFileTokens[outFileTokens.length - 1]; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 125 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 126 | File file = new File(argv[0]); |
| 127 | int width, height, subsamp = TJ.SAMP_444; |
| 128 | |
| 129 | if(inFormat.equalsIgnoreCase("jpg")) { |
| 130 | FileInputStream fis = new FileInputStream(file); |
| 131 | int inputSize = fis.available(); |
| 132 | if(inputSize < 1) { |
| 133 | System.out.println("Input file contains no data"); |
| 134 | System.exit(1); |
| 135 | } |
| 136 | byte [] inputBuf = new byte[inputSize]; |
| 137 | fis.read(inputBuf); |
| 138 | fis.close(); |
| 139 | |
| 140 | TJDecompressor tjd = new TJDecompressor(inputBuf); |
| 141 | width = tjd.getWidth(); |
| 142 | height = tjd.getHeight(); |
| 143 | int inSubsamp = tjd.getSubsamp(); |
| 144 | System.out.println("Source Image: " + width + " x " + height |
| 145 | + " pixels, " + sampName[inSubsamp] + " subsampling"); |
| 146 | if(outSubsamp < 0) outSubsamp = inSubsamp; |
| 147 | |
| 148 | if(scaleFactor != 1) { |
| 149 | width = (width + scaleFactor - 1)/scaleFactor; |
| 150 | height = (height + scaleFactor - 1)/scaleFactor; |
| 151 | } |
| 152 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 153 | if(!outFormat.equalsIgnoreCase("jpg")) |
| 154 | img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB, 0); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 155 | else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, 0); |
| 156 | tjd.close(); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 157 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 158 | else { |
| 159 | img = ImageIO.read(file); |
| 160 | width = img.getWidth(); |
| 161 | height = img.getHeight(); |
| 162 | if(outSubsamp < 0) { |
| 163 | if(img.getType() == BufferedImage.TYPE_BYTE_GRAY) |
| 164 | outSubsamp = TJ.SAMP_GRAY; |
| 165 | else outSubsamp = TJ.SAMP_444; |
| 166 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 167 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 168 | System.out.print("Dest. Image (" + outFormat + "): " + width + " x " |
| 169 | + height + " pixels"); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 170 | |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 171 | if(outFormat.equalsIgnoreCase("jpg")) { |
| 172 | System.out.println(", " + sampName[outSubsamp] |
| 173 | + " subsampling, quality = " + outQual); |
| 174 | TJCompressor tjc = new TJCompressor(); |
| 175 | int jpegSize; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 176 | byte [] jpegBuf; |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 177 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 178 | tjc.setSubsamp(outSubsamp); |
| 179 | tjc.setJPEGQuality(outQual); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 180 | if(img != null) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 181 | jpegBuf = tjc.compress(img, 0); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 182 | else { |
| 183 | tjc.setBitmapBuffer(bmpBuf, width, 0, height, TJ.PF_BGRX); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 184 | jpegBuf = tjc.compress(0); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 185 | } |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame^] | 186 | jpegSize = tjc.getCompressedSize(); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 187 | tjc.close(); |
| 188 | |
| 189 | file = new File(argv[1]); |
| 190 | FileOutputStream fos = new FileOutputStream(file); |
| 191 | fos.write(jpegBuf, 0, jpegSize); |
| 192 | fos.close(); |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 193 | } |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 194 | else { |
DRC | 0ad78a6 | 2011-02-23 20:57:17 +0000 | [diff] [blame] | 195 | System.out.print("\n"); |
DRC | 026f7ce | 2011-02-23 20:51:54 +0000 | [diff] [blame] | 196 | file = new File(argv[1]); |
| 197 | ImageIO.write(img, outFormat, file); |
| 198 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 199 | |
| 200 | } catch(Exception e) { |
| 201 | System.out.println(e); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | }; |