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