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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
32 import java.awt.image.*;
34 import java.util.regex.*;
36 public class PnmImage extends Component
38 private byte[] data = null;
39 private int width = 0;
40 private int height = 0;
41 private int channel = 0;
43 public PnmImage( int c, int w, int h)
48 data = new byte [ w*h*c];
51 public PnmImage( String filename)
60 FileInputStream fis = new FileInputStream( new File(filename));
61 DataInputStream is = new DataInputStream( fis);
63 pat = Pattern.compile("^P([56])$");
64 mat = pat.matcher(str = is.readLine());
66 System.out.println("PNM header format error");
70 if( (mat.group(1)).compareTo("5") == 0)
75 pat = Pattern.compile("^(\\d+) (\\d+)$");
76 mat = pat.matcher(str = is.readLine());
78 System.out.println("PNM header format error");
81 width = Integer.parseInt( mat.group(1));
82 height = Integer.parseInt( mat.group(2));
84 str = is.readLine(); // 255
86 bytes = width*height*channel;
87 data = new byte[bytes];
91 r = is.read(data, offset, bytes);
93 System.err.println(" failed to read()");
99 catch (IOException e) { e.printStackTrace(); }
102 } catch (IOException e) { e.printStackTrace(); }
105 public byte [] get_data(){ return data;}
106 public int get_width() { return width;}
107 public int get_height(){ return height;}
109 public Image createROIImage( int rx, int ry, int rw, int rh)
111 int []pix = new int[ rw*rh];
113 for( int i=0; i<rh; i++)
114 for( int j=0; j<rw; j++){
115 pix[i*rw+j] = 0xFF << 24; // transparency
117 Byte lum = data[(ry+i)*width+rx+j];
121 slum = (short)(2*128+lum);
125 for( int c=0; c<3; c++){
126 pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
130 for( int c=0; c<3; c++){
131 Byte lum = data[ ((ry+i)*width+rx+j)*channel+(2-c)];
135 slum = (short)(2*128+lum);
139 pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
143 return createImage(new MemoryImageSource( rw, rh, pix, 0, rw));
146 public Image createScaleImage( double scale)
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);
152 return createImage(prod);