blob: 238bc2fc352a12e844d49a977a9c33762f569040 [file] [log] [blame]
DRCf8e00552011-02-04 11:06:36 +00001/*
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
34import java.io.*;
DRC026f7ce2011-02-23 20:51:54 +000035import java.awt.image.*;
36import javax.imageio.*;
DRCc5a41992011-02-08 06:54:36 +000037import org.libjpegturbo.turbojpeg.*;
DRCf8e00552011-02-04 11:06:36 +000038
DRC2413cb82011-02-08 02:11:37 +000039public class TJExample {
DRCf8e00552011-02-04 11:06:36 +000040
DRC026f7ce2011-02-23 20:51:54 +000041 public static final String classname = new TJExample().getClass().getName();
DRCf8e00552011-02-04 11:06:36 +000042
DRCe1303ef2011-02-16 03:26:48 +000043 private static void usage() {
DRC026f7ce2011-02-23 20:51:54 +000044 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");
DRCe1303ef2011-02-16 03:26:48 +000048 System.out.println("Options:\n");
DRC026f7ce2011-02-23 20:51:54 +000049 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");
DRCe1303ef2011-02-16 03:26:48 +000058 System.exit(1);
59 }
60
DRC026f7ce2011-02-23 20:51:54 +000061 private final static String sampName[] = {
62 "4:4:4", "4:2:2", "4:2:0", "Grayscale"
63 };
64
DRCf8e00552011-02-04 11:06:36 +000065 public static void main(String argv[]) {
66
DRC026f7ce2011-02-23 20:51:54 +000067 BufferedImage img = null; byte [] bmpBuf = null;
68
DRCf8e00552011-02-04 11:06:36 +000069 try {
70
DRC026f7ce2011-02-23 20:51:54 +000071 if(argv.length < 2) {
DRCe1303ef2011-02-16 03:26:48 +000072 usage();
73 }
74
DRC026f7ce2011-02-23 20:51:54 +000075 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();
DRCe1303ef2011-02-16 03:26:48 +0000116 }
117 }
DRCf8e00552011-02-04 11:06:36 +0000118 }
DRC026f7ce2011-02-23 20:51:54 +0000119 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];
DRCf8e00552011-02-04 11:06:36 +0000125
DRC026f7ce2011-02-23 20:51:54 +0000126 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
153 if(!outFormat.equalsIgnoreCase("jpg")) {
154 img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
155 tjd.decompress(img, 0);
156 }
157 else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, 0);
158 tjd.close();
DRCf8e00552011-02-04 11:06:36 +0000159 }
DRC026f7ce2011-02-23 20:51:54 +0000160 else {
161 img = ImageIO.read(file);
162 width = img.getWidth();
163 height = img.getHeight();
164 if(outSubsamp < 0) {
165 if(img.getType() == BufferedImage.TYPE_BYTE_GRAY)
166 outSubsamp = TJ.SAMP_GRAY;
167 else outSubsamp = TJ.SAMP_444;
168 }
DRCf8e00552011-02-04 11:06:36 +0000169 }
DRC026f7ce2011-02-23 20:51:54 +0000170 System.out.print("Dest. Image (" + outFormat + "): " + width + " x "
171 + height + " pixels");
DRCe1303ef2011-02-16 03:26:48 +0000172
DRC026f7ce2011-02-23 20:51:54 +0000173 if(outFormat.equalsIgnoreCase("jpg")) {
174 System.out.println(", " + sampName[outSubsamp]
175 + " subsampling, quality = " + outQual);
176 TJCompressor tjc = new TJCompressor();
177 int jpegSize;
178 byte [] jpegBuf = new byte[TJ.bufSize(width, height)];
179
180 if(img != null)
181 jpegSize = tjc.compress(img, jpegBuf, outSubsamp, outQual, 0);
182 else {
183 tjc.setBitmapBuffer(bmpBuf, width, 0, height, TJ.PF_BGRX);
184 jpegSize = tjc.compress(jpegBuf, outSubsamp, outQual, 0);
185 }
186 tjc.close();
187
188 file = new File(argv[1]);
189 FileOutputStream fos = new FileOutputStream(file);
190 fos.write(jpegBuf, 0, jpegSize);
191 fos.close();
DRCe1303ef2011-02-16 03:26:48 +0000192 }
DRC026f7ce2011-02-23 20:51:54 +0000193 else {
DRC0ad78a62011-02-23 20:57:17 +0000194 System.out.print("\n");
DRC026f7ce2011-02-23 20:51:54 +0000195 file = new File(argv[1]);
196 ImageIO.write(img, outFormat, file);
197 }
DRCf8e00552011-02-04 11:06:36 +0000198
199 } catch(Exception e) {
200 System.out.println(e);
201 }
202 }
203
204};