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 public byte[] data = null;
40 public int height = 0;
41 public int channel = 0;
43 public Image createROIImage( int rx, int ry, int rw, int rh)
45 int []pix = new int[ rw*rh];
47 for( int i=0; i<rh; i++)
48 for( int j=0; j<rw; j++){
49 pix[i*rw+j] = 0xFF << 24; // transparency
51 Byte lum = data[(ry+i)*width+rx+j];
55 slum = (short)(2*128+lum);
59 for( int c=0; c<3; c++){
60 pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
64 for( int c=0; c<3; c++){
65 Byte lum = data[ ((ry+i)*width+rx+j)*channel+(2-c)];
69 slum = (short)(2*128+lum);
73 pix[i*rw+j] = pix[i*rw+j] | slum << (8*c);
76 return createImage(new MemoryImageSource( rw, rh, pix, 0, rw));
79 public Image createScaleImage( double scale)
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);
85 return createImage(prod);
88 public void openimage( String filename)
97 FileInputStream fis = new FileInputStream( new File(filename));
98 DataInputStream is = new DataInputStream( fis);
100 pat = Pattern.compile("^P([56])$");
101 mat = pat.matcher(str = is.readLine());
103 System.out.println("PNM header format error");
107 if( (mat.group(1)).compareTo("5") == 0)
112 pat = Pattern.compile("^(\\d+) (\\d+)$");
113 mat = pat.matcher(str = is.readLine());
115 System.out.println("PNM header format error");
118 width = Integer.parseInt( mat.group(1));
119 height = Integer.parseInt( mat.group(2));
121 str = is.readLine(); // 255
123 bytes = width*height*channel;
124 data = new byte[bytes];
128 r = is.read(data, offset, bytes);
130 System.err.println(" failed to read()");
136 catch (IOException e) { e.printStackTrace(); }
139 } catch (IOException e) { e.printStackTrace(); }