11 package com.google.scrollview.ui;
13 import edu.umd.cs.piccolo.nodes.PImage;
15 import java.awt.image.BufferedImage;
16 import java.util.HashMap;
31 static HashMap<String, PImage> images =
new HashMap<String, PImage>();
34 static boolean readImageData =
false;
38 static String imageName = null;
39 static int bytesRead = 0;
41 static int pictureArray[];
43 static int bytePerPixel = 0;
46 static int height = 0;
56 private static int[] processBinaryImage(String inputLine) {
58 int WHITE = Integer.MAX_VALUE;
60 int[] imgData =
new int[inputLine.length()];
62 for (
int i = 0; i < inputLine.length(); i++) {
63 if (inputLine.charAt(i) ==
'0') {
65 }
else if (inputLine.charAt(i) ==
'1') {
69 System.out.println(
"Error: unexpected non-image-data: ("
83 private static int[] processGrayImage(String inputLine) {
84 int[] imgData =
new int[inputLine.length() / 2];
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);
100 private static int[] process32bppImage(String inputLine) {
102 String[] strData = inputLine.split(
"#");
103 int[] imgData =
new int[strData.length - 1];
105 for (
int i = 1; i < strData.length; i++) {
106 imgData[i - 1] = Integer.parseInt(strData[i], 16);
116 private static void closeImage() {
118 BufferedImage bi = null;
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);
126 System.out.println(
"Unsupported Image Type: " + bpp +
" bpp");
130 bi.setRGB(0, 0, width, height, pictureArray, 0, width);
132 PImage img =
new PImage(bi);
134 images.put(imageName, img);
137 readImageData =
false;
139 System.out.println(
"(server, #Bytes:" + bytesRead +
") Image Completed");
146 public static void createImage(String name,
int width,
int height,
152 }
else if (bpp == 8) {
154 }
else if (bpp == 32) {
157 throw new IllegalArgumentException(
158 "bpp should be 1 (binary), 8 (gray) or 32 (argb), is " + bpp);
160 if (imageName != null) {
161 throw new IllegalArgumentException(
"Image " + imageName +
" already opened!");
166 readImageData =
true;
169 pictureArray =
new int[width * height];
172 System.out.println(
"Processing Image with " + bpp +
" bpp, size " + width +
"x" + height);
184 PImage img =
new PImage(location);
185 images.put(location, img);
190 return images.get(name);
201 data = processBinaryImage(inputLine);
202 }
else if (bpp == 8) {
203 data = processGrayImage(inputLine);
204 }
else if (bpp == 32) {
205 data = process32bppImage(inputLine);
207 System.out.println(
"Unsupported Bit Type: " + bpp);
210 System.arraycopy(data, 0, pictureArray, bytesRead, data.length);
211 bytesRead += data.length;
214 if (bytesRead == (height * width)) {
221 return readImageData;
226 return (height * width * bytePerPixel) - bytesRead;