blob: 4db7a63905217522045bb3f0431e71d2b581ee6f [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.*;
DRCc5a41992011-02-08 06:54:36 +000035import org.libjpegturbo.turbojpeg.*;
DRCf8e00552011-02-04 11:06:36 +000036
DRC2413cb82011-02-08 02:11:37 +000037public class TJExample {
DRCf8e00552011-02-04 11:06:36 +000038
DRC2413cb82011-02-08 02:11:37 +000039 public static final String classname=new TJExample().getClass().getName();
DRCf8e00552011-02-04 11:06:36 +000040
DRCe1303ef2011-02-16 03:26:48 +000041 private static void usage() {
42 System.out.println("\nUSAGE: java "+classname+" <Input file> <Output file> [options]\n");
43 System.out.println("Options:\n");
44 System.out.println("-scale 1/N = scale the width/height of the output image by a factor of 1/N");
45 System.out.println(" (N = 1, 2, 4, or 8}\n");
46 System.exit(1);
47 }
48
DRCf8e00552011-02-04 11:06:36 +000049 public static void main(String argv[]) {
50
51 try {
52
53 if(argv.length<2) {
DRCe1303ef2011-02-16 03:26:48 +000054 usage();
55 }
56
57 int scalefactor=1;
58 if(argv.length>2) {
59 for(int i=2; i<argv.length; i++) {
60 if(argv[i].equalsIgnoreCase("-scale") && i<argv.length-1) {
61 String [] scalearg=argv[++i].split("/");
62 if(scalearg.length!=2 || Integer.parseInt(scalearg[0])!=1
63 || (scalefactor=Integer.parseInt(scalearg[1]))<1
64 || scalefactor>8 || (scalefactor&(scalefactor-1))!=0)
65 usage();
66 }
67 }
DRCf8e00552011-02-04 11:06:36 +000068 }
69
70 File file=new File(argv[0]);
71 FileInputStream fis=new FileInputStream(file);
72 int inputsize=fis.available();
73 if(inputsize<1) {
74 System.out.println("Input file contains no data");
75 System.exit(1);
76 }
77 byte [] inputbuf=new byte[inputsize];
78 fis.read(inputbuf);
79 fis.close();
80
DRC36336fc2011-02-22 10:27:31 +000081 TJDecompressor tjd=new TJDecompressor(inputbuf);
82 int width=tjd.getWidth();
83 int height=tjd.getHeight();
84 int subsamp=tjd.getSubsamp();
85 System.out.print("Source Image: "+width+" x "+height+" pixels, ");
86 switch(subsamp) {
DRC3bad53f2011-02-23 02:20:49 +000087 case TJ.SAMP_444: System.out.println("4:4:4 subsampling"); break;
88 case TJ.SAMP_422: System.out.println("4:2:2 subsampling"); break;
89 case TJ.SAMP_420: System.out.println("4:2:0 subsampling"); break;
90 case TJ.SAMP_GRAY: System.out.println("Grayscale"); break;
DRCf8e00552011-02-04 11:06:36 +000091 default: System.out.println("Unknown subsampling"); break;
92 }
DRCe1303ef2011-02-16 03:26:48 +000093
94 if(scalefactor!=1) {
DRC36336fc2011-02-22 10:27:31 +000095 width=(width+scalefactor-1)/scalefactor;
96 height=(height+scalefactor-1)/scalefactor;
97 System.out.println("Dest. Image: "+width+" x "+height
DRCe1303ef2011-02-16 03:26:48 +000098 +" pixels");
99 }
100
DRC3bad53f2011-02-23 02:20:49 +0000101 byte [] tmpbuf=tjd.decompress(width, 0, height, TJ.PF_BGR, TJ.BOTTOMUP);
DRCf8e00552011-02-04 11:06:36 +0000102 tjd.close();
103
DRC3bad53f2011-02-23 02:20:49 +0000104 TJCompressor tjc=new TJCompressor(tmpbuf, width, 0, height, TJ.PF_BGR);
105 byte [] outputbuf=new byte[TJ.bufSize(width, height)];
106 int outputsize=tjc.compress(outputbuf, subsamp, 95, TJ.BOTTOMUP);
DRCf8e00552011-02-04 11:06:36 +0000107 tjc.close();
108
109 file=new File(argv[1]);
110 FileOutputStream fos=new FileOutputStream(file);
DRC3bad53f2011-02-23 02:20:49 +0000111 fos.write(outputbuf, 0, outputsize);
DRCf8e00552011-02-04 11:06:36 +0000112 fos.close();
113
114 } catch(Exception e) {
115 System.out.println(e);
116 }
117 }
118
119};