summaryrefslogtreecommitdiff
path: root/j2kviewer/src/PgmImage.java
blob: 324fbfab366c0043073aba3de196cb4c9f608c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import java.util.regex.*;

class PgmImage extends Component
{
  private Socket s;
  private BufferedReader in;
  private int x, y;
  
  PgmImage()
  {
  }
  
  private String read()
  {
    try { return in.readLine(); }
    catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
  
  public Image open(String filename)
  {
    String  str;
    Pattern pat;
    Matcher mat;
    int bytes, width, height, depth;
    FileInputStream fis;
    
    try {
      in  = new BufferedReader(
              new InputStreamReader(
	        fis = new FileInputStream(
		  new File(filename))));

      pat = Pattern.compile("^P5$");
      mat = pat.matcher(str = read());
      mat.matches();
      pat = Pattern.compile("^(\\d+) (\\d+)$");
      mat = pat.matcher(str = read());
      mat.matches();
      x = new Integer(mat.group(1)).intValue();
      y = new Integer(mat.group(2)).intValue();
      width  = x;
      height = y;
      depth  = 1;
      pat = Pattern.compile("^255$");
      mat = pat.matcher(str = read());
      mat.matches();
      bytes = x*y;
      char[] buf = new char[bytes];
      int r, offset = 0;
      while (bytes > 0) {
	try { r = in.read(buf, offset, bytes); offset += r; bytes -= r; }
	catch (IOException e) { e.printStackTrace(); }
      }
      int[] buf2 = new int[buf.length];
      if (depth == 3) {
	for (int i = 0; i < buf.length/3; ++i)
	  buf2[i] = 0xFF << 24 | buf[3*i] << 16 | buf[3*i+1] << 8 | buf[3*i+2];
      } else {
	for (int i = 0; i < buf.length; ++i)
	  buf2[i] = 0xFF << 24 | buf[i] << 16 | buf[i] << 8 | buf[i];
      }
      fis.close();
      return createImage(new MemoryImageSource(width, height, buf2, 0, width));
    } catch (IOException e) { e.printStackTrace(); }
    return null;
  }

  public void close()
  {
  }
  
  public boolean bye()
  {
    return true;
  }
  
  public int getXOffset()
  {
    return x;
  }
  
  public int getYOffset()
  {
    return y;
  }
}