Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVImageHandler.java
Go to the documentation of this file.
1 // Copyright 2007 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); You may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 // applicable law or agreed to in writing, software distributed under the
7 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8 // OF ANY KIND, either express or implied. See the License for the specific
9 // language governing permissions and limitations under the License.
10 
11 package com.google.scrollview.ui;
12 
13 import edu.umd.cs.piccolo.nodes.PImage;
14 
15 import java.awt.image.BufferedImage;
16 import java.util.HashMap;
17 
26 public class SVImageHandler {
31  static HashMap<String, PImage> images = new HashMap<String, PImage>();
32 
34  static boolean readImageData = false;
35 
36  // TODO(wanke) Consider moving all this into an SVImage class.
38  static String imageName = null; // Image name
39  static int bytesRead = 0; // Nr. of bytes already read
40  static int bpp = 0; // Bit depth
41  static int pictureArray[]; // The array holding the actual image
42 
43  static int bytePerPixel = 0; // # of used bytes to transmit a pixel (32 bpp
44  // -> 7 BPP)
45  static int width = 0;
46  static int height = 0;
47 
48  /* All methods are static, so we forbid to construct SVImageHandler objects */
49  private SVImageHandler() {
50  }
51 
56  private static int[] processBinaryImage(String inputLine) {
57  int BLACK = 0;
58  int WHITE = Integer.MAX_VALUE;
59 
60  int[] imgData = new int[inputLine.length()];
61 
62  for (int i = 0; i < inputLine.length(); i++) {
63  if (inputLine.charAt(i) == '0') {
64  imgData[i] = WHITE;
65  } else if (inputLine.charAt(i) == '1') {
66  imgData[i] = BLACK;
67  } // BLACK is default anyway
68  else { // Something is wrong: We did get unexpected data
69  System.out.println("Error: unexpected non-image-data: ("
70  + SVImageHandler.bytesRead + "," + inputLine.length() + ","
71  + (SVImageHandler.height * SVImageHandler.width) + ")");
72  System.exit(1);
73  }
74  }
75  return imgData;
76  }
77 
83  private static int[] processGrayImage(String inputLine) {
84  int[] imgData = new int[inputLine.length() / 2];
85  // Note: This is really inefficient, splitting it 2-byte-arrays in one pass
86  // would be wa faster than substring everytime.
87  for (int i = 0; i < inputLine.length(); i +=2) {
88  String s = inputLine.substring(i, i+1);
89  imgData[i] = Integer.parseInt(s, 16);
90  }
91 
92  return imgData;
93  }
94 
100  private static int[] process32bppImage(String inputLine) {
101 
102  String[] strData = inputLine.split("#");
103  int[] imgData = new int[strData.length - 1];
104 
105  for (int i = 1; i < strData.length; i++) {
106  imgData[i - 1] = Integer.parseInt(strData[i], 16);
107  }
108 
109  return imgData;
110  }
111 
116  private static void closeImage() {
117 
118  BufferedImage bi = null;
119  if (bpp == 1) {
120  bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
121  } else if (bpp == 8) {
122  bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
123  } else if (bpp == 32) {
124  bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
125  } else {
126  System.out.println("Unsupported Image Type: " + bpp + " bpp");
127  System.exit(1);
128  }
129 
130  bi.setRGB(0, 0, width, height, pictureArray, 0, width);
131 
132  PImage img = new PImage(bi);
133 
134  images.put(imageName, img);
135 
136  imageName = null;
137  readImageData = false;
138 
139  System.out.println("(server, #Bytes:" + bytesRead + ") Image Completed");
140 
141  bytesRead = 0;
142  bpp = 0;
143  }
144 
146  public static void createImage(String name, int width, int height,
147  int bitsPerPixel) {
148  // Create buffered image that does not support transparency
149  bpp = bitsPerPixel;
150  if (bpp == 1) {
151  bytePerPixel = 1;
152  } else if (bpp == 8) {
153  bytePerPixel = 2;
154  } else if (bpp == 32) {
155  bytePerPixel = 7;
156  } else {
157  throw new IllegalArgumentException(
158  "bpp should be 1 (binary), 8 (gray) or 32 (argb), is " + bpp);
159  }
160  if (imageName != null) {
161  throw new IllegalArgumentException("Image " + imageName + " already opened!");
162  }
163  else {
164  imageName = name;
165  bytesRead = 0;
166  readImageData = true;
167  SVImageHandler.height = height;
168  SVImageHandler.width = width;
169  pictureArray = new int[width * height];
170  }
171 
172  System.out.println("Processing Image with " + bpp + " bpp, size " + width + "x" + height);
173  }
174 
183  public static void openImage(String location) {
184  PImage img = new PImage(location);
185  images.put(location, img);
186  }
187 
189  public static PImage getImage(String name) {
190  return images.get(name);
191  }
192 
197  public static void parseData(String inputLine) {
198  int[] data = null;
199 
200  if (bpp == 1) {
201  data = processBinaryImage(inputLine);
202  } else if (bpp == 8) {
203  data = processGrayImage(inputLine);
204  } else if (bpp == 32) {
205  data = process32bppImage(inputLine);
206  } else {
207  System.out.println("Unsupported Bit Type: " + bpp);
208  }
209 
210  System.arraycopy(data, 0, pictureArray, bytesRead, data.length);
211  bytesRead += data.length;
212 
213  // We have read all image data - close the image
214  if (bytesRead == (height * width)) {
215  closeImage();
216  }
217  }
218 
220  public static boolean getReadImageData() {
221  return readImageData;
222  }
223 
225  public static int getMissingRemainingBytes() {
226  return (height * width * bytePerPixel) - bytesRead;
227  }
228 }