Permalink
Browse files

The user directory functionality was added with support for PNG image…

… types.

A PIXEL object was introduced.
  • Loading branch information...
1 parent 9e6518d commit e98c4d1d7ee05b295303dddc546535f9992777f1 @onebeartoe committed Mar 22, 2013
@@ -0,0 +1,180 @@
+
+package com.ledpixelart.hardware;
+
+import com.ledpixelart.pc.PixelApp;
+import ioio.lib.api.RgbLedMatrix;
+import ioio.lib.api.exception.ConnectionLostException;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+
+/**
+ *
+ * @author rmarquez
+ */
+public class Pixel
+{
+
+ public RgbLedMatrix matrix;
+
+ public RgbLedMatrix.Matrix KIND;
+
+ protected byte[] BitmapBytes;
+
+ protected InputStream BitmapInputStream;
+
+ protected short[] frame_;
+
+ public Pixel(RgbLedMatrix.Matrix KIND)
+ {
+ this.KIND = KIND;
+
+ BitmapBytes = new byte[KIND.width * KIND.height * 2]; //512 * 2 = 1024 or 1024 * 2 = 2048
+
+ frame_ = new short[KIND.width * KIND.height];
+ }
+
+ public void loadRGB565(String raw565ImagePath) throws ConnectionLostException
+ {
+
+ BitmapInputStream = PixelApp.class.getClassLoader().getResourceAsStream(raw565ImagePath);
+
+ try
+ {
+ int n = BitmapInputStream.read(BitmapBytes, 0, BitmapBytes.length); // reads
+ // the
+ // input
+ // stream
+ // into
+ // a
+ // byte
+ // array
+ Arrays.fill(BitmapBytes, n, BitmapBytes.length, (byte) 0);
+ }
+ catch (IOException e)
+ {
+ System.err.println("An error occured while trying to load " + raw565ImagePath + ".");
+ System.err.println("Make sure " + raw565ImagePath + "is included in the executable JAR.");
+ e.printStackTrace();
+ }
+
+ int y = 0;
+ for (int f = 0; f < frame_.length; f++)
+ {
+ frame_[f] = (short) (((short) BitmapBytes[y] & 0xFF) | (((short) BitmapBytes[y + 1] & 0xFF) << 8));
+ y = y + 2;
+ }
+ matrix = PixelApp.getMatrix();
+ matrix.frame(frame_);
+ }
+
+ private void loadRGB565PNG() throws ConnectionLostException
+ {
+ int y = 0;
+ for (int f = 0; f < frame_.length; f++)
+ {
+ frame_[f] = (short) (((short) BitmapBytes[y] & 0xFF) | (((short) BitmapBytes[y + 1] & 0xFF) << 8));
+ y = y + 2;
+ }
+
+ matrix = PixelApp.getMatrix();
+ matrix.frame(frame_);
+ }
+
+ public void writeImagetoMatrix(BufferedImage originalImage) throws ConnectionLostException
+// public void writeImagetoMatrix(String imagePath) throws ConnectionLostException
+ {
+ //here we'll take a PNG, BMP, or whatever and convert it to RGB565 via a canvas, also we'll re-size the image if necessary
+
+
+// try
+ {
+// URL url = PixelApp.class.getClassLoader().getResource(imagePath);
+// try
+// {
+// BufferedImage originalImage = ImageIO.read(url);
+ int width_original = originalImage.getWidth();
+ int height_original = originalImage.getHeight();
+
+ if (width_original != KIND.width || height_original != KIND.height)
+ {
+ //the image is not the right dimensions, ie, 32px by 32px
+ BufferedImage ResizedImage = new BufferedImage(KIND.width, KIND.height, originalImage.getType());
+ Graphics2D g = ResizedImage.createGraphics();
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ g.drawImage(originalImage, 0, 0, KIND.width, KIND.height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
+ g.dispose();
+ originalImage = ResizedImage;
+ }
+
+ int numByte = 0;
+ int i = 0;
+ int j = 0;
+
+ for (i = 0; i < KIND.height; i++)
+ {
+ for (j = 0; j < KIND.width; j++)
+ {
+ Color c = new Color(originalImage.getRGB(j, i)); //i and j were reversed which was rotationg the image by 90 degrees
+ int aRGBpix = originalImage.getRGB(j, i); //i and j were reversed which was rotationg the image by 90 degrees
+ int alpha;
+ int red = c.getRed();
+ int green = c.getGreen();
+ int blue = c.getBlue();
+
+ //RGB565
+ red = red >> 3;
+ green = green >> 2;
+ blue = blue >> 3;
+ //A pixel is represented by a 4-byte (32 bit) integer, like so:
+ //00000000 00000000 00000000 11111111
+ //^ Alpha ^Red ^Green ^Blue
+ //Converting to RGB565
+
+ short pixel_to_send = 0;
+ int pixel_to_send_int = 0;
+ pixel_to_send_int = (red << 11) | (green << 5) | (blue);
+ pixel_to_send = (short) pixel_to_send_int;
+
+ //dividing into bytes
+ byte byteH = (byte) ((pixel_to_send >> 8) & 0x0FF);
+ byte byteL = (byte) (pixel_to_send & 0x0FF);
+
+ //Writing it to array - High-byte is the first
+
+ BitmapBytes[numByte + 1] = byteH;
+ BitmapBytes[numByte] = byteL;
+ numByte += 2;
+ }
+ }
+ }
+// catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+// e.printStackTrace();
+ }
+
+ loadRGB565PNG();
+ }
+
+ /**
+ *
+ * //************ this part of code writes to the LED matrix in code without any external file *********
+ // writeTest(); //this just writes a test pattern to the LEDs in code without using any external file, uncomment out this line if you want to see that and then comment out the next two lines
+ //***************************************************************************************************
+ */
+ private void writeTest()
+ {
+ for (int i = 0; i < frame_.length; i++)
+ {
+ // frame_[i] = (short) (((short) 0x00000000 & 0xFF) | (((short) (short) 0x00000000 & 0xFF) << 8)); //all black
+ frame_[i] = (short) (((short) 0xFFF5FFB0 & 0xFF) | (((short) (short) 0xFFF5FFB0 & 0xFF) << 8)); //pink
+ //frame_[i] = (short) (((short) 0xFFFFFFFF & 0xFF) | (((short) (short) 0xFFFFFFFF & 0xFF) << 8)); //all white
+ }
+ }
+
+}
@@ -32,106 +32,90 @@ public AnimationsPanel(RgbLedMatrix.Matrix KIND)
super(KIND);
imageListPath = "/animations.text";
- AnimateTimer = new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- if (!pixelFound) { //only go here if PIXEL wa found, other leave the timer
- return;
- }
- // System.out.println("animate");
-
- i++;
-
- if (i >= numFrames - 1) {
- i = 0;
- }
-
- // framestring = "animations/decoded/boat/boat" + i + ".rgb565";
- String framestring = "animations/decoded/" + animation_name + "/" + animation_name + i + ".rgb565";
- try {
- loadRGB565(framestring);
- } catch (ConnectionLostException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- // if (i == numFrames - 1) {
- // Animate.restart();
- // }
-
- }
-
- };
+ AnimateTimer = new ActionListener()
+ {
+ public void actionPerformed(ActionEvent evt)
+ {
+ if (!pixelFound)
+ {
+ //only go here if PIXEL wa found, other leave the timer
+ return;
+ }
+
+ i++;
+
+ if (i >= numFrames - 1)
+ {
+ i = 0;
+ }
+
+ String framestring = "animations/decoded/" + animation_name + "/" + animation_name + i + ".rgb565";
+ try
+ {
+ PixelApp.pixel.loadRGB565(framestring);
+ }
+ catch (ConnectionLostException e1)
+ {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ }
+ };
}
@Override
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
System.out.println("animation comamand: " + command);
-
- String selectedFileName = event.getActionCommand();
- String decodedDirPath = "animations/decoded";
-
- //System.out.println("selected file name: " + selectedFileName);
-
- InputStream decodedFile = PixelAnimationsPC.class.getClassLoader().getResourceAsStream(decodedDirPath + "/" + selectedFileName + "/" + selectedFileName + ".txt"); //decoded/rain/rain.text
- //note can't use file operator here as you can't reference files from a jar file
-
- if (decodedFile != null)
- {
- // ok good, now let's read it, we need to get the total numbers of frames and the frame speed
-
- String line = "";
-
- try
- {
- BufferedReader
- br = new BufferedReader(
- new InputStreamReader(decodedFile));
-
- line = br.readLine();
-
- // while ((line = br.readLine()) != null) {
- // text.append(line);
- // text.append('\n');
- // }
- }
- catch (IOException e)
- {
- //You'll need to add proper error handling here
- }
-
- String fileAttribs = line.toString(); //now convert to a string
- //System.out.println(fileAttribs);
- String fdelim = "[,]"; //now parse this string considering the comma split ie, 32,60
- String [] fileAttribs2 = fileAttribs.split(fdelim);
- int selectedFileTotalFrames = Integer.parseInt(fileAttribs2[0].trim());
-
- //System.out.println("total frames: " + selectedFileTotalFrames);
- //System.out.println(fileAttribs2[0] + " " + fileAttribs2[1] + fileAttribs2[2]);
-
- int selectedFileDelay = Integer.parseInt(fileAttribs2[1].trim());
- //selectedFileResolution = 32;
- //selectedFileResolution = Integer.parseInt(fileAttribs2[2].trim());
-
-
- //****** Now let's setup the animation ******
- i = 0;
- animation_name = event.getActionCommand();
- numFrames = selectedFileTotalFrames;
- // System.out.println("file delay: " + selectedFileDelay);
-
- timer = new Timer(selectedFileDelay , AnimateTimer);
-
- if (timer.isRunning() == true) {
- timer.stop();
- }
- timer.start();
- }
-
- // System.out.println(fileAttribs);
-
-
+
+ String selectedFileName = event.getActionCommand();
+ String decodedDirPath = "animations/decoded";
+
+ //System.out.println("selected file name: " + selectedFileName);
+
+ InputStream decodedFile = PixelAnimationsPC.class.getClassLoader().getResourceAsStream(decodedDirPath + "/" + selectedFileName + "/" + selectedFileName + ".txt"); //decoded/rain/rain.text
+ //note can't use file operator here as you can't reference files from a jar file
+
+ if (decodedFile != null)
+ {
+ // ok good, now let's read it, we need to get the total numbers of frames and the frame speed
+
+ String line = "";
+
+ try
+ {
+ InputStreamReader streamReader = new InputStreamReader(decodedFile);
+ BufferedReader br = new BufferedReader(streamReader);
+ line = br.readLine();
+ }
+ catch (IOException e)
+ {
+ //You'll need to add proper error handling here
+ }
+
+ String fileAttribs = line.toString(); //now convert to a string
+
+ String fdelim = "[,]"; //now parse this string considering the comma split ie, 32,60
+ String[] fileAttribs2 = fileAttribs.split(fdelim);
+ int selectedFileTotalFrames = Integer.parseInt(fileAttribs2[0].trim());
+
+ int selectedFileDelay = Integer.parseInt(fileAttribs2[1].trim());
+
+ //****** Now let's setup the animation ******
+ i = 0;
+ animation_name = event.getActionCommand();
+ numFrames = selectedFileTotalFrames;
+ // System.out.println("file delay: " + selectedFileDelay);
+
+ timer = new Timer(selectedFileDelay, AnimateTimer);
+
+ if (timer.isRunning() == true)
+ {
+ timer.stop();
+ }
+ timer.start();
+ }
}
@Override
@@ -141,3 +125,4 @@ protected String imagePath()
}
}
+
Oops, something went wrong.

0 comments on commit e98c4d1

Please sign in to comment.