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 | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 81 | 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) { |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 87 | 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; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 91 | default: System.out.println("Unknown subsampling"); break; |
| 92 | } |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 93 | |
| 94 | if(scalefactor!=1) { |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 95 | width=(width+scalefactor-1)/scalefactor; |
| 96 | height=(height+scalefactor-1)/scalefactor; |
| 97 | System.out.println("Dest. Image: "+width+" x "+height |
DRC | e1303ef | 2011-02-16 03:26:48 +0000 | [diff] [blame] | 98 | +" pixels"); |
| 99 | } |
| 100 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 101 | byte [] tmpbuf=tjd.decompress(width, 0, height, TJ.PF_BGR, TJ.BOTTOMUP); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 102 | tjd.close(); |
| 103 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 104 | 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); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 107 | tjc.close(); |
| 108 | |
| 109 | file=new File(argv[1]); |
| 110 | FileOutputStream fos=new FileOutputStream(file); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 111 | fos.write(outputbuf, 0, outputsize); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 112 | fos.close(); |
| 113 | |
| 114 | } catch(Exception e) { |
| 115 | System.out.println(e); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | }; |