blob: 5f923335ee0794960db15c78c11b2b8b29b0ad1f [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.*;
35
36public class tjexample {
37
38 public static final String classname=new tjexample().getClass().getName();
39
40 public static void main(String argv[]) {
41
42 try {
43
44 if(argv.length<2) {
45 System.out.println("USAGE: java "+classname+" <Input file> <Output file>");
46 System.exit(1);
47 }
48
49 File file=new File(argv[0]);
50 FileInputStream fis=new FileInputStream(file);
51 int inputsize=fis.available();
52 if(inputsize<1) {
53 System.out.println("Input file contains no data");
54 System.exit(1);
55 }
56 byte [] inputbuf=new byte[inputsize];
57 fis.read(inputbuf);
58 fis.close();
59
60 tjDecompressor tjd=new tjDecompressor();
61 tjHeaderInfo tji=tjd.DecompressHeader(inputbuf, inputsize);
62 System.out.print("Source Image: "+tji.width+" x "+tji.height+ " pixels, ");
63 switch(tji.subsamp) {
64 case TJ.SAMP444: System.out.println("4:4:4 subsampling"); break;
65 case TJ.SAMP422: System.out.println("4:2:2 subsampling"); break;
66 case TJ.SAMP420: System.out.println("4:2:0 subsampling"); break;
67 case TJ.GRAYSCALE: System.out.println("Grayscale"); break;
68 default: System.out.println("Unknown subsampling"); break;
69 }
70 byte [] tmpbuf=new byte[tji.width*tji.height*3];
71 tjd.Decompress(inputbuf, inputsize, tmpbuf, tji.width, tji.width*3,
72 tji.height, 3, TJ.BOTTOMUP);
73 tjd.close();
74
75 tjCompressor tjc=new tjCompressor();
76 byte [] outputbuf=new byte[(int)TJ.BUFSIZE(tji.width, tji.height)];
77 long outputsize=tjc.Compress(tmpbuf, tji.width, tji.width*3, tji.height,
78 3, outputbuf, tji.subsamp, 95, TJ.BOTTOMUP);
79 tjc.close();
80
81 file=new File(argv[1]);
82 FileOutputStream fos=new FileOutputStream(file);
83 fos.write(outputbuf, 0, (int)outputsize);
84 fos.close();
85
86 } catch(Exception e) {
87 System.out.println(e);
88 }
89 }
90
91};