blob: f6c2e43bbe8fc71cc40b947a59c6a773811e710d [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
41 public static void main(String argv[]) {
42
43 try {
44
45 if(argv.length<2) {
46 System.out.println("USAGE: java "+classname+" <Input file> <Output file>");
47 System.exit(1);
48 }
49
50 File file=new File(argv[0]);
51 FileInputStream fis=new FileInputStream(file);
52 int inputsize=fis.available();
53 if(inputsize<1) {
54 System.out.println("Input file contains no data");
55 System.exit(1);
56 }
57 byte [] inputbuf=new byte[inputsize];
58 fis.read(inputbuf);
59 fis.close();
60
DRC2413cb82011-02-08 02:11:37 +000061 TJDecompressor tjd=new TJDecompressor();
62 TJHeaderInfo tji=tjd.decompressHeader(inputbuf, inputsize);
DRCf8e00552011-02-04 11:06:36 +000063 System.out.print("Source Image: "+tji.width+" x "+tji.height+ " pixels, ");
64 switch(tji.subsamp) {
65 case TJ.SAMP444: System.out.println("4:4:4 subsampling"); break;
66 case TJ.SAMP422: System.out.println("4:2:2 subsampling"); break;
67 case TJ.SAMP420: System.out.println("4:2:0 subsampling"); break;
68 case TJ.GRAYSCALE: System.out.println("Grayscale"); break;
69 default: System.out.println("Unknown subsampling"); break;
70 }
71 byte [] tmpbuf=new byte[tji.width*tji.height*3];
DRC2413cb82011-02-08 02:11:37 +000072 tjd.decompress(inputbuf, inputsize, tmpbuf, tji.width, tji.width*3,
DRCf8e00552011-02-04 11:06:36 +000073 tji.height, 3, TJ.BOTTOMUP);
74 tjd.close();
75
DRC2413cb82011-02-08 02:11:37 +000076 TJCompressor tjc=new TJCompressor();
77 byte [] outputbuf=new byte[(int)TJ.bufSize(tji.width, tji.height)];
78 long outputsize=tjc.compress(tmpbuf, tji.width, tji.width*3, tji.height,
DRCf8e00552011-02-04 11:06:36 +000079 3, outputbuf, tji.subsamp, 95, TJ.BOTTOMUP);
80 tjc.close();
81
82 file=new File(argv[1]);
83 FileOutputStream fos=new FileOutputStream(file);
84 fos.write(outputbuf, 0, (int)outputsize);
85 fos.close();
86
87 } catch(Exception e) {
88 System.out.println(e);
89 }
90 }
91
92};