Modification of opj_dec_server to be portable to windows
[openjpeg.git] / applications / jpip / opj_client / 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     public byte[] data = null;
39     public int width = 0;
40     public int height = 0;
41     public int channel = 0;
42     
43     public Image createROIImage( int rx, int ry, int rw, int rh)
44     {
45         int []pix = new int[ rw*rh];
46         
47         for( int i=0; i<rh; i++)
48             for( int j=0; j<rw; j++){
49                 pix[i*rw+j] = 0xFF << 24; // transparency
50                 if( channel == 1){
51                     Byte lum = data[(ry+i)*width+rx+j];
52                     short slum;
53           
54                     if( lum < 0)
55                         slum = (short)(2*128+lum);
56                     else
57                         slum = (short)lum;
58           
59                     for( int c=0; c<3; c++){
60                         pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
61                     }
62                 }
63                 else
64                     for( int c=0; c<3; c++){
65                         Byte lum = data[ ((ry+i)*width+rx+j)*channel+(2-c)];
66                         short slum;
67             
68                         if( lum < 0)
69                             slum = (short)(2*128+lum);
70                         else
71                             slum = (short)lum;
72             
73                         pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
74                     }
75             }
76         return createImage(new MemoryImageSource( rw, rh, pix, 0, rw));
77     }
78
79     public Image createScaleImage( double scale)
80     {
81         Image src = createROIImage( 0, 0, width, height);       
82         ImageFilter replicate = new ReplicateScaleFilter( (int)(width*scale), (int)(height*scale));
83         ImageProducer prod = new FilteredImageSource( src.getSource(), replicate);
84         
85         return createImage(prod);
86     }
87     
88     public void openimage( String filename)
89     {
90         String  str;
91         Pattern pat;
92         Matcher mat;
93         int bytes;
94         int r, offset = 0;
95                 
96         try {
97             FileInputStream fis = new FileInputStream( new File(filename));    
98             DataInputStream is = new DataInputStream( fis);
99             
100             pat = Pattern.compile("^P([56])$");
101             mat = pat.matcher(str = is.readLine());
102             if( !mat.matches()){
103                 System.out.println("PNM header format error");
104                 return;
105             }
106
107             if( (mat.group(1)).compareTo("5") == 0)
108                 channel = 1;
109             else
110                 channel = 3;
111             
112             pat = Pattern.compile("^(\\d+) (\\d+)$");
113             mat = pat.matcher(str = is.readLine());
114             if( !mat.matches()){
115                 System.out.println("PNM header format error");
116                 return;
117             }
118             width  = Integer.parseInt( mat.group(1));
119             height = Integer.parseInt( mat.group(2));
120
121             str = is.readLine(); // 255
122             
123             bytes = width*height*channel;
124             data = new byte[bytes];
125                     
126             while( bytes > 0){
127                 try {
128                     r = is.read(data, offset, bytes);
129                     if( r == -1){
130                         System.err.println("    failed to read()");
131                         break;
132                     }
133                     offset += r; 
134                     bytes -= r; 
135                 }
136                 catch (IOException e) { e.printStackTrace(); }
137             }    
138             fis.close();
139         } catch (IOException e) { e.printStackTrace(); }
140     }   
141 }