Re-build the jar files from the source code.
[openjpeg.git] / applications / jpip / util / opj_viewer / src / PnmImage.java
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 import java.awt.*;
32 import java.awt.image.*;
33 import java.io.*;
34 import java.util.regex.*;
35
36 public class PnmImage extends Component
37 {
38     private byte[] data = null;
39     private int width = 0;
40     private int height = 0;
41     private int channel = 0;
42     
43     public PnmImage( int c, int w, int h)
44     {
45         channel = c;
46         width   = w;
47         height  = h;
48         data = new byte [ w*h*c];
49     }
50        
51     public PnmImage( String filename)
52     {
53         String  str;
54         Pattern pat;
55         Matcher mat;
56         int bytes;
57         int r, offset = 0;
58                 
59         try {
60             FileInputStream fis = new FileInputStream( new File(filename));    
61             DataInputStream is = new DataInputStream( fis);
62             
63             pat = Pattern.compile("^P([56])$");
64             mat = pat.matcher(str = is.readLine());
65             if( !mat.matches()){
66                 System.out.println("PNM header format error");
67                 return;
68             }
69
70             if( (mat.group(1)).compareTo("5") == 0)
71                 channel = 1;
72             else
73                 channel = 3;
74             
75             pat = Pattern.compile("^(\\d+) (\\d+)$");
76             mat = pat.matcher(str = is.readLine());
77             if( !mat.matches()){
78                 System.out.println("PNM header format error");
79                 return;
80             }
81             width  = Integer.parseInt( mat.group(1));
82             height = Integer.parseInt( mat.group(2));
83
84             str = is.readLine(); // 255
85             
86             bytes = width*height*channel;
87             data = new byte[bytes];
88                     
89             while( bytes > 0){
90                 try {
91                     r = is.read(data, offset, bytes);
92                     if( r == -1){
93                         System.err.println("    failed to read()");
94                         break;
95                     }
96                     offset += r; 
97                     bytes -= r; 
98                 }
99                 catch (IOException e) { e.printStackTrace(); }
100             }    
101             fis.close();
102         } catch (IOException e) { e.printStackTrace(); }
103     }
104
105     public byte [] get_data(){  return data;}
106     public int get_width() { return width;}
107     public int get_height(){ return height;}
108     
109     public Image createROIImage( int rx, int ry, int rw, int rh)
110     {
111         int []pix = new int[ rw*rh];
112         
113         for( int i=0; i<rh; i++)
114             for( int j=0; j<rw; j++){
115                 pix[i*rw+j] = 0xFF << 24; // transparency
116                 if( channel == 1){
117                     Byte lum = data[(ry+i)*width+rx+j];
118                     short slum;
119           
120                     if( lum < 0)
121                         slum = (short)(2*128+lum);
122                     else
123                         slum = (short)lum;
124           
125                     for( int c=0; c<3; c++){
126                         pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
127                     }
128                 }
129                 else
130                     for( int c=0; c<3; c++){
131                         Byte lum = data[ ((ry+i)*width+rx+j)*channel+(2-c)];
132                         short slum;
133             
134                         if( lum < 0)
135                             slum = (short)(2*128+lum);
136                         else
137                             slum = (short)lum;
138             
139                         pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
140                     }
141             }
142
143         return createImage(new MemoryImageSource( rw, rh, pix, 0, rw));
144     }
145
146     public Image createScaleImage( double scale)
147     {
148         Image src = createROIImage( 0, 0, width, height);       
149         ImageFilter replicate = new ReplicateScaleFilter( (int)(width*scale), (int)(height*scale));
150         ImageProducer prod = new FilteredImageSource( src.getSource(), replicate);
151         
152         return createImage(prod);
153     }
154 }