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 | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 35 | import org.libjpegturbo.turbojpeg.*; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 36 | |
DRC | 2413cb8 | 2011-02-08 02:11:37 +0000 | [diff] [blame] | 37 | public class TJExample { |
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 static final String classname=new TJExample().getClass().getName(); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 40 | |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame^] | 41 | 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 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 49 | public static void main(String argv[]) { |
| 50 | |
| 51 | try { |
| 52 | |
| 53 | if(argv.length<2) { |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame^] | 54 | 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 | } |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 68 | } |
| 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 | |
DRC | 2413cb8 | 2011-02-08 02:11:37 +0000 | [diff] [blame] | 81 | TJDecompressor tjd=new TJDecompressor(); |
| 82 | TJHeaderInfo tji=tjd.decompressHeader(inputbuf, inputsize); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 83 | System.out.print("Source Image: "+tji.width+" x "+tji.height+ " pixels, "); |
| 84 | switch(tji.subsamp) { |
| 85 | case TJ.SAMP444: System.out.println("4:4:4 subsampling"); break; |
| 86 | case TJ.SAMP422: System.out.println("4:2:2 subsampling"); break; |
| 87 | case TJ.SAMP420: System.out.println("4:2:0 subsampling"); break; |
| 88 | case TJ.GRAYSCALE: System.out.println("Grayscale"); break; |
| 89 | default: System.out.println("Unknown subsampling"); break; |
| 90 | } |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame^] | 91 | |
| 92 | if(scalefactor!=1) { |
| 93 | tji.width=(tji.width+scalefactor-1)/scalefactor; |
| 94 | tji.height=(tji.height+scalefactor-1)/scalefactor; |
| 95 | System.out.println("Dest. Image: "+tji.width+" x "+tji.height |
| 96 | +" pixels"); |
| 97 | } |
| 98 | |
| 99 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 100 | byte [] tmpbuf=new byte[tji.width*tji.height*3]; |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame^] | 101 | tjd.decompress(inputbuf, inputsize, tmpbuf, tji.width*3, |
| 102 | 3, 1, scalefactor, TJ.BOTTOMUP); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 103 | tjd.close(); |
| 104 | |
DRC | 2413cb8 | 2011-02-08 02:11:37 +0000 | [diff] [blame] | 105 | TJCompressor tjc=new TJCompressor(); |
| 106 | byte [] outputbuf=new byte[(int)TJ.bufSize(tji.width, tji.height)]; |
| 107 | long outputsize=tjc.compress(tmpbuf, tji.width, tji.width*3, tji.height, |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 108 | 3, outputbuf, tji.subsamp, 95, TJ.BOTTOMUP); |
| 109 | tjc.close(); |
| 110 | |
| 111 | file=new File(argv[1]); |
| 112 | FileOutputStream fos=new FileOutputStream(file); |
| 113 | fos.write(outputbuf, 0, (int)outputsize); |
| 114 | fos.close(); |
| 115 | |
| 116 | } catch(Exception e) { |
| 117 | System.out.println(e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | }; |