+ if ((image.getWidth() < 1)
+ || (image.getWidth() > 10000)
+ || (image.getHeight() < 1)
+ || (image.getHeight() > 10000)
+ ) {
+ return;
+ }
+
+ imageToCells(image, true);
+ }
+
+ /**
+ * Parse a "Jexer" RGB image string into a bitmap image, and overlay that
+ * image onto the text cells.
+ *
+ * @param pw width token
+ * @param ph height token
+ * @param ps scroll token
+ * @param data pixel data
+ */
+ private void parseJexerImageRGB(final String pw, final String ph,
+ final String ps, final String data) {
+
+ int imageWidth = 0;
+ int imageHeight = 0;
+ boolean scroll = false;
+ try {
+ imageWidth = Integer.parseInt(pw);
+ imageHeight = Integer.parseInt(ph);
+ } catch (NumberFormatException e) {
+ // SQUASH
+ return;
+ }
+ if ((imageWidth < 1)
+ || (imageWidth > 10000)
+ || (imageHeight < 1)
+ || (imageHeight > 10000)
+ ) {
+ return;
+ }
+ if (ps.equals("1")) {
+ scroll = true;
+ } else if (ps.equals("0")) {
+ scroll = false;
+ } else {
+ return;
+ }
+
+ byte [] bytes = StringUtils.fromBase64(data.getBytes());
+ if (bytes.length != (imageWidth * imageHeight * 3)) {
+ return;
+ }
+
+ BufferedImage image = new BufferedImage(imageWidth, imageHeight,
+ BufferedImage.TYPE_INT_ARGB);
+
+ for (int x = 0; x < imageWidth; x++) {
+ for (int y = 0; y < imageHeight; y++) {
+ int red = bytes[(y * imageWidth * 3) + (x * 3) ];
+ if (red < 0) {
+ red += 256;
+ }
+ int green = bytes[(y * imageWidth * 3) + (x * 3) + 1];
+ if (green < 0) {
+ green += 256;
+ }
+ int blue = bytes[(y * imageWidth * 3) + (x * 3) + 2];
+ if (blue < 0) {
+ blue += 256;
+ }
+ int rgb = 0xFF000000 | (red << 16) | (green << 8) | blue;
+ image.setRGB(x, y, rgb);
+ }
+ }
+
+ imageToCells(image, scroll);
+ }
+
+ /**
+ * Parse a "Jexer" PNG or JPG image string into a bitmap image, and
+ * overlay that image onto the text cells.
+ *
+ * @param type 1 for PNG, 2 for JPG
+ * @param ps scroll token
+ * @param data pixel data
+ */
+ private void parseJexerImageFile(final int type, final String ps,
+ final String data) {
+
+ int imageWidth = 0;
+ int imageHeight = 0;
+ boolean scroll = false;
+ BufferedImage image = null;
+ try {
+ byte [] bytes = StringUtils.fromBase64(data.getBytes());
+
+ switch (type) {
+ case 1:
+ if ((bytes[0] != (byte) 0x89)
+ || (bytes[1] != 'P')
+ || (bytes[2] != 'N')
+ || (bytes[3] != 'G')
+ || (bytes[4] != (byte) 0x0D)
+ || (bytes[5] != (byte) 0x0A)
+ || (bytes[6] != (byte) 0x1A)
+ || (bytes[7] != (byte) 0x0A)
+ ) {
+ // File does not have PNG header, bail out.
+ return;
+ }
+ break;
+
+ case 2:
+ if ((bytes[0] != (byte) 0XFF)
+ || (bytes[1] != (byte) 0xD8)
+ || (bytes[2] != (byte) 0xFF)
+ ) {
+ // File does not have JPG header, bail out.
+ return;
+ }
+ break;
+
+ default:
+ // Unsupported type, bail out.
+ return;
+ }
+
+ image = ImageIO.read(new ByteArrayInputStream(bytes));
+ } catch (IOException e) {
+ // SQUASH
+ return;
+ }
+ assert (image != null);
+ imageWidth = image.getWidth();
+ imageHeight = image.getHeight();
+ if ((imageWidth < 1)
+ || (imageWidth > 10000)
+ || (imageHeight < 1)
+ || (imageHeight > 10000)
+ ) {
+ return;
+ }
+ if (ps.equals("1")) {
+ scroll = true;
+ } else if (ps.equals("0")) {
+ scroll = false;
+ } else {
+ return;
+ }
+
+ imageToCells(image, scroll);
+ }
+
+ /**
+ * Break up an image into the cells at the current cursor.
+ *
+ * @param image the image to display
+ * @param scroll if true, scroll the image and move the cursor
+ */
+ private void imageToCells(final BufferedImage image, final boolean scroll) {
+ assert (image != null);