File: SVImageHandler.java

package info (click to toggle)
tesseract 2.04-2%2Bsqueeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 7,336 kB
  • ctags: 6,860
  • sloc: cpp: 81,388; sh: 3,446; java: 1,220; makefile: 376
file content (228 lines) | stat: -rw-r--r-- 7,589 bytes parent folder | download | duplicates (3)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2007 Google Inc. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); You may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
// applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

package com.google.scrollview.ui;

import edu.umd.cs.piccolo.nodes.PImage;

import java.awt.image.BufferedImage;
import java.util.HashMap;

/**
 * The ScrollViewImageHandler is a helper class which takes care of image
 * processing. It is used to construct an Image from the message-stream and
 * basically consists of a number of utility functions to process the input
 * stream.
 * 
 * @author wanke@google.com
 */
public class SVImageHandler {
  /**
   * Stores a mapping from the name of the string to its actual image. It
   * enables us to re-use images without having to load or transmit them again
   */
  static HashMap<String, PImage> images = new HashMap<String, PImage>();

  /** A global flag stating whether we are currently expecting Image data */
  static boolean readImageData = false;

  // TODO(wanke) Consider moving all this into an SVImage class.
  /** These are all values belonging to the image which is currently being read */
  static String imageName = null; // Image name
  static int bytesRead = 0; // Nr. of bytes already read
  static int bpp = 0; // Bit depth
  static int pictureArray[]; // The array holding the actual image

  static int bytePerPixel = 0; // # of used bytes to transmit a pixel (32 bpp
                                // -> 7 BPP)
  static int width = 0;
  static int height = 0;

  /* All methods are static, so we forbid to construct SVImageHandler objects */
  private SVImageHandler() {
  }

  /**
   * Takes a binary input string (consisting of '0' and '1' characters) and
   * converts it to an integer representation usable as image data.
   */
  private static int[] processBinaryImage(String inputLine) {
    int BLACK = 0;
    int WHITE = Integer.MAX_VALUE;

    int[] imgData = new int[inputLine.length()];

    for (int i = 0; i < inputLine.length(); i++) {
      if (inputLine.charAt(i) == '0') {
        imgData[i] = WHITE;
      } else if (inputLine.charAt(i) == '1') {
        imgData[i] = BLACK;
      } // BLACK is default anyway
      else { // Something is wrong: We did get unexpected data
        System.out.println("Error: unexpected non-image-data: ("
            + SVImageHandler.bytesRead + "," + inputLine.length() + ","
            + (SVImageHandler.height * SVImageHandler.width) + ")");
        System.exit(1);
      }
    }
    return imgData;
  }

  /**
   * Takes an input string with pixel depth of 8 (represented by 2 bytes in
   * hexadecimal format, e.g. FF for white) and converts it to an
   * integer representation usable as image data
   */
  private static int[] processGrayImage(String inputLine) {
    int[] imgData = new int[inputLine.length() / 2];
    // Note: This is really inefficient, splitting it 2-byte-arrays in one pass
    // would be wa faster than substring everytime.
    for (int i = 0; i < inputLine.length(); i +=2) {
      String s = inputLine.substring(i, i+1);
      imgData[i] = Integer.parseInt(s, 16);
    }
    
    return imgData;
  }
  
  /**
   * Takes an input string with pixel depth of 32 (represented by HTML-like
   * colors in hexadecimal format, e.g. #00FF00 for green) and converts it to an
   * integer representation usable as image data
   */
  private static int[] process32bppImage(String inputLine) {

    String[] strData = inputLine.split("#");
    int[] imgData = new int[strData.length - 1];

    for (int i = 1; i < strData.length; i++) {
      imgData[i - 1] = Integer.parseInt(strData[i], 16);
    }

    return imgData;
  }

  /**
   * Called when all image data is transmitted. Generates the actual image used
   * by java and puts it into the images-hashmap.
   */
  private static void closeImage() {

    BufferedImage bi = null;
    if (bpp == 1) {
      bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
    } else if (bpp == 8) {
      bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    } else if (bpp == 32) {
      bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    } else {
      System.out.println("Unsupported Image Type: " + bpp + " bpp");
      System.exit(1);
    }

    bi.setRGB(0, 0, width, height, pictureArray, 0, width);

    PImage img = new PImage(bi);

    images.put(imageName, img);

    imageName = null;
    readImageData = false;

    System.out.println("(server, #Bytes:" + bytesRead + ") Image Completed");

    bytesRead = 0;
    bpp = 0;
  }

  /** Starts creation of a new image. */
  public static void createImage(String name, int width, int height,
      int bitsPerPixel) {
    // Create buffered image that does not support transparency
    bpp = bitsPerPixel;
    if (bpp == 1) {
      bytePerPixel = 1;
    } else if (bpp == 8) {
      bytePerPixel = 2;
    } else if (bpp == 32) {
      bytePerPixel = 7;
    } else {
      throw new IllegalArgumentException(
          "bpp should be 1 (binary), 8 (gray) or 32 (argb), is " + bpp);
    }
    if (imageName != null) {
      throw new IllegalArgumentException("Image " + imageName + " already opened!");
    }
    else {
      imageName = name;
      bytesRead = 0;
      readImageData = true;
      SVImageHandler.height = height;
      SVImageHandler.width = width;
      pictureArray = new int[width * height];
    }   

    System.out.println("Processing Image with " + bpp + " bpp, size " + width + "x" + height);
  }

  /**
   * Opens an Image from location. This means the image does not have to be
   * actually transfered over the network. Thus, it is a lot faster than using
   * the createImage method.
   * 
   * @param location The (local) location from where to open the file. This is
   * also the internal name associated with the image (if you want to draw it).
   */
  public static void openImage(String location) {
    PImage img = new PImage(location);
    images.put(location, img);
  }

  /** Find the image corresponding to a given name */
  public static PImage getImage(String name) {
    return images.get(name);
  }

  /**
   * Gets called while currently reading image data. Decides, how to process it
   * (which image type, whether all data is there).
   */
  public static void parseData(String inputLine) {
    int[] data = null;

    if (bpp == 1) {
      data = processBinaryImage(inputLine);
    } else if (bpp == 8) {
      data = processGrayImage(inputLine);
    } else if (bpp == 32) {
      data = process32bppImage(inputLine);
    } else {
      System.out.println("Unsupported Bit Type: " + bpp);
    }

    System.arraycopy(data, 0, pictureArray, bytesRead, data.length);
    bytesRead += data.length;

    // We have read all image data - close the image
    if (bytesRead == (height * width)) {
      closeImage();
    }
  }

  /** Returns whether we a currently reading image data or not */
  public static boolean getReadImageData() {
    return readImageData;
  }

  /** Computes how many bytes of the image data are still missing */
  public static int getMissingRemainingBytes() {
    return (height * width * bytePerPixel) - bytesRead;
  }
}