commit 19b764849713c4b05a13efab12e7044149d86956 from: leo date: Wed Nov 12 12:06:44 2008 UTC some minor code cleanup and documentation commit - dd5b15d42d1c234d425ffe08bbc7b49091c2ff0a commit + 19b764849713c4b05a13efab12e7044149d86956 blob - 088d129fec920b36fcfe192b372c6a9aec879478 blob + 281fe8abb4acae7c0d33f288ea22ef865e2909db --- MePixel.iml +++ MePixel.iml @@ -1,6 +1,5 @@ - - + @@ -9,180 +8,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - blob - ba22d4400f61b1fa1eb1b649c1fe3df77ac5c42c blob + 9fde4773701a4eced1618f1d8a8e257c530c9ff2 --- MePixel.ipr +++ MePixel.ipr @@ -1,5 +1,5 @@ - + @@ -7,6 +7,7 @@ + @@ -40,11 +41,12 @@ - - + + + - + - + + + - + - + + + - - + + + + + + - - - blob - 5351f0b50c42a6e0ad2537eeb811bf12ff7bd10d blob + aff9383990aa4b7bd06b59bf851662cf82933aa2 --- MePixel.iws +++ MePixel.iws @@ -1,6 +1,19 @@ - - + + + - - - + - + - - + + + - - - + - - blob - deedc8f08cbf751168a30a155647f2a2e3e23680 blob + 162ae7c12df3899b5264d3c27e81f90989327151 --- src/com/thinkberg/mepixel/BufferedDisplay.java +++ src/com/thinkberg/mepixel/BufferedDisplay.java @@ -14,35 +14,35 @@ import java.awt.*; * * @author Matthias L. Jugel */ -public class BufferedDisplay extends JFrame { - private Image backingStore; - private int xOffset; - private int yOffset; +class BufferedDisplay extends JFrame { + private Image backingStore; + private int xOffset; + private int yOffset; - public BufferedDisplay() { - super(); + public BufferedDisplay() { + super(); - setUndecorated(true); - setExtendedState(Frame.MAXIMIZED_BOTH); - setVisible(true); - setBackground(Color.BLACK); + setUndecorated(true); + setExtendedState(Frame.MAXIMIZED_BOTH); + setVisible(true); + setBackground(Color.BLACK); - getGraphicsConfiguration().getDevice().setFullScreenWindow(this); + getGraphicsConfiguration().getDevice().setFullScreenWindow(this); - } + } - public void setBackingStore(Image image) { - backingStore = image; - xOffset = (getWidth() - backingStore.getWidth(this)) / 2; - yOffset = (getHeight() - backingStore.getHeight(this)) / 2; - } + public void setBackingStore(Image image) { + backingStore = image; + xOffset = (getWidth() - backingStore.getWidth(this)) / 2; + yOffset = (getHeight() - backingStore.getHeight(this)) / 2; + } - /** - * Paint the current screen using the backing store image. - */ - public void paint(Graphics g) { - if (backingStore != null) { - g.drawImage(backingStore, xOffset, yOffset, this); + /** + * Paint the current screen using the backing store image. + */ + public void paint(Graphics g) { + if (backingStore != null) { + g.drawImage(backingStore, xOffset, yOffset, this); + } } - } } blob - 0f538f2bde4cdab2d264d69ded00d6ec51ba9a98 blob + 5ecbfce5bc4c49917ceae8b19c4aa07f4633d4a7 --- src/com/thinkberg/mepixel/CameraGrabberThread.java +++ src/com/thinkberg/mepixel/CameraGrabberThread.java @@ -24,98 +24,98 @@ import quicktime.util.RawEncodedImage; * * @author Matthias L. Jugel */ -public class CameraGrabberThread extends Thread { - private boolean running = false; +class CameraGrabberThread extends Thread { + private boolean running = false; - private SequenceGrabber grabber; - private SGVideoChannel channel; - private RawEncodedImage rowEncodedImage; - private int[] pixelData; - private int width, height; + private SequenceGrabber grabber; + private SGVideoChannel channel; + private RawEncodedImage rowEncodedImage; + private int[] pixelData; + private int width, height; - public CameraGrabberThread() { - super(); - } + public CameraGrabberThread() { + super(); + } - public void init() throws QTException { - QTSession.open(); + public void init() throws QTException { + QTSession.open(); - grabber = new SequenceGrabber(); - channel = new SGVideoChannel(grabber); + grabber = new SequenceGrabber(); + channel = new SGVideoChannel(grabber); - width = channel.getSrcVideoBounds().getWidth(); - height = channel.getSrcVideoBounds().getHeight(); - QDRect bounds = new QDRect(width, height); - QDGraphics graphics; - if (quicktime.util.EndianOrder.isNativeLittleEndian()) { - graphics = new QDGraphics(QDConstants.k32BGRAPixelFormat, bounds); - } else { - graphics = new QDGraphics(QDGraphics.kDefaultPixelFormat, bounds); - } - grabber.setGWorld(graphics, null); - channel.setBounds(bounds); - channel.setUsage(StdQTConstants.seqGrabPreview); - grabber.prepare(true, false); + width = channel.getSrcVideoBounds().getWidth(); + height = channel.getSrcVideoBounds().getHeight(); + QDRect bounds = new QDRect(width, height); + QDGraphics graphics; + if (quicktime.util.EndianOrder.isNativeLittleEndian()) { + graphics = new QDGraphics(QDConstants.k32BGRAPixelFormat, bounds); + } else { + graphics = new QDGraphics(QDGraphics.kDefaultPixelFormat, bounds); + } + grabber.setGWorld(graphics, null); + channel.setBounds(bounds); + channel.setUsage(StdQTConstants.seqGrabPreview); + grabber.prepare(true, false); - grabber.setDataOutput(null, StdQTConstants.seqGrabDontMakeMovie); - grabber.prepare(true, true); - grabber.startRecord(); + grabber.setDataOutput(null, StdQTConstants.seqGrabDontMakeMovie); + grabber.prepare(true, true); + grabber.startRecord(); - PixMap pixMap = graphics.getPixMap(); - rowEncodedImage = pixMap.getPixelData(); - pixelData = new int[width * height]; + PixMap pixMap = graphics.getPixMap(); + rowEncodedImage = pixMap.getPixelData(); + pixelData = new int[width * height]; - QTRuntimeException.registerHandler(new QTRuntimeHandler() { - public void exceptionOccurred( - QTRuntimeException e, Object eGenerator, - String methodNameIfKnown, boolean unrecoverableFlag) { - System.err.println("A Quicktime error has occurred: " + e.getMessage()); - e.printStackTrace(); - } - }); - } + QTRuntimeException.registerHandler(new QTRuntimeHandler() { + public void exceptionOccurred( + QTRuntimeException e, Object eGenerator, + String methodNameIfKnown, boolean unrecoverableFlag) { + System.err.println("A Quicktime error has occurred: " + e.getMessage()); + e.printStackTrace(); + } + }); + } - public int getCameraWidth() { - return width; - } + public int getCameraWidth() { + return width; + } - public int getCameraHeight() { - return height; - } + public int getCameraHeight() { + return height; + } - public void enable() { - running = true; - start(); - } + public void enable() { + running = true; + start(); + } - public void disable() { - running = true; - } + public void disable() { + running = true; + } - public void run() { - try { - while (running) { - grabber.idle(); - rowEncodedImage.copyToArray(0, pixelData, 0, pixelData.length); - } - } catch (QTException e) { - e.printStackTrace(); + public void run() { + try { + while (running) { + grabber.idle(); + rowEncodedImage.copyToArray(0, pixelData, 0, pixelData.length); + } + } catch (QTException e) { + e.printStackTrace(); + } } - } - public int[] getPixelData() { - return pixelData; - } + public int[] getPixelData() { + return pixelData; + } - public void dispose() { - try { - grabber.stop(); - grabber.release(); - grabber.disposeChannel(channel); - } catch (Exception e) { - e.printStackTrace(); - } finally { - QTSession.close(); + public void dispose() { + try { + grabber.stop(); + grabber.release(); + grabber.disposeChannel(channel); + } catch (Exception e) { + e.printStackTrace(); + } finally { + QTSession.close(); + } } - } } blob - d7a62eb0dbcd64db9e6c636b7eaaa01933bcb285 blob + 855528c8984ca566127f7092e8976953054a62ee --- src/com/thinkberg/mepixel/MePixel.java +++ src/com/thinkberg/mepixel/MePixel.java @@ -22,169 +22,169 @@ import java.io.InputStreamReader; * * @author Matthias L. Jugel */ -public class MePixel { - private static int delay = 20; - private static boolean fillSwap = true; - private static boolean charSwap = false; - private static double brightnessThreshold = 0.50; +class MePixel { + private static int delay = 20; + private static boolean fillSwap = true; + private static boolean charSwap = false; + private static double brightnessThreshold = 0.50; - private static void showUsage() { - System.out.println("MePixel (c) 2007 Matthias L. Jugel. All Rights Reserved."); - System.out.println(); - System.out.println("** Use cursor 'UP' and 'DOWN' to increase/decrease speed"); - System.out.println("** Use cursor 'LEFT' and 'RIGHT' to change the brightness threshold"); - System.out.println("** Press 'C' to change between number and rectangle fill mode"); - System.out.println("** Press 'F' to toggle fill modes (toggle 1 and 0)"); - System.out.println("** To quit, press 'Cmd-Q' or 'Alt-F4'"); - System.out.println(); - System.out.println("Press return to continue ..."); - try { - new BufferedReader(new InputStreamReader(System.in)).readLine(); - } catch (IOException e) { - // ignore + private static void showUsage() { + System.out.println("MePixel (c) 2007 Matthias L. Jugel. All Rights Reserved."); + System.out.println(); + System.out.println("** Use cursor 'UP' and 'DOWN' to increase/decrease speed"); + System.out.println("** Use cursor 'LEFT' and 'RIGHT' to change the brightness threshold"); + System.out.println("** Press 'C' to change between number and rectangle fill mode"); + System.out.println("** Press 'F' to toggle fill modes (toggle 1 and 0)"); + System.out.println("** To quit, press 'Cmd-Q' or 'Alt-F4'"); + System.out.println(); + System.out.println("Press return to continue ..."); + try { + new BufferedReader(new InputStreamReader(System.in)).readLine(); + } catch (IOException e) { + // ignore + } } - } - public MePixel(String[] args) { - showUsage(); + public MePixel(String[] args) { + showUsage(); - int fontSize = 8; - try { - if (args.length > 0) { - fontSize = Integer.parseInt(args[0]); - } - } catch (NumberFormatException e) { - // ignore if the value is not an integer - } + int fontSize = 8; + try { + if (args.length > 0) { + fontSize = Integer.parseInt(args[0]); + } + } catch (NumberFormatException e) { + // ignore if the value is not an integer + } - CameraGrabberThread cameraGrabber = new CameraGrabberThread(); - try { - cameraGrabber.init(); - } catch (QTException e) { - System.err.println("Camera grabber failed: " + e.getMessage() + " (no camera found?)"); - System.err.println("Exiting ..."); - System.exit(-1); - } - cameraGrabber.enable(); + CameraGrabberThread cameraGrabber = new CameraGrabberThread(); + try { + cameraGrabber.init(); + } catch (QTException e) { + System.err.println("Camera grabber failed: " + e.getMessage() + " (no camera found?)"); + System.err.println("Exiting ..."); + System.exit(-1); + } + cameraGrabber.enable(); - int cameraWidth = cameraGrabber.getCameraWidth(); - int cameraHeight = cameraGrabber.getCameraHeight(); + int cameraWidth = cameraGrabber.getCameraWidth(); + int cameraHeight = cameraGrabber.getCameraHeight(); - int[] pixelData = cameraGrabber.getPixelData(); + int[] pixelData = cameraGrabber.getPixelData(); - BufferedDisplay bufferedDisplay = new BufferedDisplay(); - bufferedDisplay.addKeyListener(new KeyAdapter() { - public void keyPressed(KeyEvent keyEvent) { - switch (keyEvent.getKeyCode()) { - case KeyEvent.VK_UP: - delay += 20; - break; - case KeyEvent.VK_DOWN: - if (delay >= 20) delay -= 20; - break; - case KeyEvent.VK_LEFT: - if (brightnessThreshold > 0) brightnessThreshold -= 0.05; - break; - case KeyEvent.VK_RIGHT: - if (brightnessThreshold < 1) brightnessThreshold += 0.05; - break; - case KeyEvent.VK_F: - fillSwap = !fillSwap; - break; - case KeyEvent.VK_C: - charSwap = !charSwap; - } - } - }); + BufferedDisplay bufferedDisplay = new BufferedDisplay(); + bufferedDisplay.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent keyEvent) { + switch (keyEvent.getKeyCode()) { + case KeyEvent.VK_UP: + delay += 20; + break; + case KeyEvent.VK_DOWN: + if (delay >= 20) delay -= 20; + break; + case KeyEvent.VK_LEFT: + if (brightnessThreshold > 0) brightnessThreshold -= 0.05; + break; + case KeyEvent.VK_RIGHT: + if (brightnessThreshold < 1) brightnessThreshold += 0.05; + break; + case KeyEvent.VK_F: + fillSwap = !fillSwap; + break; + case KeyEvent.VK_C: + charSwap = !charSwap; + } + } + }); - Font font = new Font("Bitstream Vera Sans Mono", Font.PLAIN, fontSize); - bufferedDisplay.setFont(font); - int charWidth = bufferedDisplay.getFontMetrics(font).charWidth('@'); - int charHeight = bufferedDisplay.getFontMetrics(font).getHeight(); - int adaptedScreenHeight = bufferedDisplay.getHeight() / charHeight; - int adaptedScreenWidth = bufferedDisplay.getWidth() / charWidth; - System.out.println("Using a character size of [" + charWidth + "x" + charHeight + "] pixels"); - System.out.println("The screen is divided into [" + adaptedScreenWidth + "x" + adaptedScreenHeight + "] squares"); + Font font = new Font("Bitstream Vera Sans Mono", Font.PLAIN, fontSize); + bufferedDisplay.setFont(font); + int charWidth = bufferedDisplay.getFontMetrics(font).charWidth('@'); + int charHeight = bufferedDisplay.getFontMetrics(font).getHeight(); + int adaptedScreenHeight = bufferedDisplay.getHeight() / charHeight; + int adaptedScreenWidth = bufferedDisplay.getWidth() / charWidth; + System.out.println("Using a character size of [" + charWidth + "x" + charHeight + "] pixels"); + System.out.println("The screen is divided into [" + adaptedScreenWidth + "x" + adaptedScreenHeight + "] squares"); - BufferedImage cachedImage = new BufferedImage(adaptedScreenWidth * charWidth, - adaptedScreenHeight * charHeight, - BufferedImage.TYPE_INT_RGB); - Graphics2D cachedImageG2D = cachedImage.createGraphics(); - cachedImageG2D.setFont(font); - bufferedDisplay.setBackingStore(cachedImage); - float[] hsvValues = new float[3]; + BufferedImage cachedImage = new BufferedImage(adaptedScreenWidth * charWidth, + adaptedScreenHeight * charHeight, + BufferedImage.TYPE_INT_RGB); + Graphics2D cachedImageG2D = cachedImage.createGraphics(); + cachedImageG2D.setFont(font); + bufferedDisplay.setBackingStore(cachedImage); + float[] hsvValues = new float[3]; - int rectWidth = cameraWidth / adaptedScreenWidth; - int rectHeight = cameraHeight / adaptedScreenHeight; + int rectWidth = cameraWidth / adaptedScreenWidth; + int rectHeight = cameraHeight / adaptedScreenHeight; - while (true) { - for (int row = 0; row < cameraHeight; row += rectHeight) { - int y = (row / rectHeight) * charHeight; + while (true) { + for (int row = 0; row < cameraHeight; row += rectHeight) { + int y = (row / rectHeight) * charHeight; - cachedImageG2D.setColor(bufferedDisplay.getBackground()); - cachedImageG2D.fillRect(0, y, cameraWidth * charWidth, charHeight); + cachedImageG2D.setColor(bufferedDisplay.getBackground()); + cachedImageG2D.fillRect(0, y, cameraWidth * charWidth, charHeight); - for (int column = 0; column < cameraWidth; column += rectWidth) { - int offset = (row * cameraWidth) + column; - int avgColor = getAveragedRectPixelColor(pixelData, offset, cameraWidth, rectWidth, rectHeight); + for (int column = 0; column < cameraWidth; column += rectWidth) { + int offset = (row * cameraWidth) + column; + int avgColor = getAveragedRectPixelColor(pixelData, offset, cameraWidth, rectWidth, rectHeight); - int x = ((cameraWidth - column) / rectWidth) * charWidth; + int x = ((cameraWidth - column) / rectWidth) * charWidth; - Color fg = new Color(avgColor); - Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsvValues); - cachedImageG2D.setColor(fg); - if ((fillSwap && (hsvValues[2] > brightnessThreshold)) || - (!fillSwap && hsvValues[2] < brightnessThreshold)) { - if (charSwap) { - cachedImageG2D.drawRect(x, y, charWidth, charHeight); - } else { - cachedImageG2D.drawString("1", x, y - charHeight); + Color fg = new Color(avgColor); + Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsvValues); + cachedImageG2D.setColor(fg); + if ((fillSwap && (hsvValues[2] > brightnessThreshold)) || + (!fillSwap && hsvValues[2] < brightnessThreshold)) { + if (charSwap) { + cachedImageG2D.drawRect(x, y, charWidth, charHeight); + } else { + cachedImageG2D.drawString("1", x, y - charHeight); + } + } else { + if (charSwap) { + cachedImageG2D.fillRect(x, y, charWidth, charHeight); + } else { + cachedImageG2D.drawString("0", x, y - charHeight); + } + } + } + bufferedDisplay.repaint(); + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + // ignore thread errors + } } - } else { - if (charSwap) { - cachedImageG2D.fillRect(x, y, charWidth, charHeight); - } else { - cachedImageG2D.drawString("0", x, y - charHeight); - } - } } - bufferedDisplay.repaint(); - try { - Thread.sleep(delay); - } catch (InterruptedException e) { - // ignore thread errors - } - } } - } - /** - * Calculate the average color for a certain pixel rectangle - * - * @param image the image to get the data from - * @param offset current offset (left, top) in the data array - * @param rowSize the current row length in bytes - * @param width the width of the rectangle in pixels - * @param height the height of the rectange in pixels - * @return a 24 bit color int - */ - private int getAveragedRectPixelColor(int[] image, int offset, int rowSize, int width, int height) { - int avgColor = 0; - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int pixColor; - try { - pixColor = image[offset + (y * rowSize) + x]; - } catch (Exception e) { - return avgColor; + /** + * Calculate the average color for a certain pixel rectangle + * + * @param image the image to get the data from + * @param offset current offset (left, top) in the data array + * @param rowSize the current row length in bytes + * @param width the width of the rectangle in pixels + * @param height the height of the rectange in pixels + * @return a 24 bit color int + */ + private int getAveragedRectPixelColor(int[] image, int offset, int rowSize, int width, int height) { + int avgColor = 0; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int pixColor; + try { + pixColor = image[offset + (y * rowSize) + x]; + } catch (Exception e) { + return avgColor; + } + // color averaging + avgColor = (((avgColor ^ pixColor) & 0xfffefefe) >> 1) + (avgColor & pixColor); + } } - // color averaging - avgColor = (((avgColor ^ pixColor) & 0xfffefefe) >> 1) + (avgColor & pixColor); - } + return avgColor; } - return avgColor; - } } blob - 502c12a8854edfb641a1b016dcd3d57943d25ad2 blob + e7d4feddd8701ddaa234ba15f3cb4947c31ba8b1 --- src/com/thinkberg/mepixel/MePixelLauncher.java +++ src/com/thinkberg/mepixel/MePixelLauncher.java @@ -23,54 +23,54 @@ import java.util.List; * @author Matthias L. Jugel */ public class MePixelLauncher { - private static List qtClassPath = Arrays.asList( - "/System/Library/Java/Extensions/QTJava.zip", - "C:\\Program Files\\Quicktime\\QTSystem\\QTJava.zip", - "C:\\Programme\\Quicktime\\QTSystem\\QTJava.zip" - ); + private static final List qtClassPath = Arrays.asList( + "/System/Library/Java/Extensions/QTJava.zip", + "C:\\Program Files\\Quicktime\\QTSystem\\QTJava.zip", + "C:\\Programme\\Quicktime\\QTSystem\\QTJava.zip" + ); - public static void main(String args[]) { + public static void main(String args[]) { - System.err.println("MePixelLauncher (c) 2007 Matthias L. Jugel. All Rights Reserved."); - System.err.println("++ Checking Quicktime installation ..."); + System.err.println("MePixelLauncher (c) 2007 Matthias L. Jugel. All Rights Reserved."); + System.err.println("++ Checking Quicktime installation ..."); - List classPath = new ArrayList(); - classPath.add(MePixel.class.getProtectionDomain().getCodeSource().getLocation()); - for (String fileName : qtClassPath) { - File file = new File(fileName); - if (file.exists()) { - try { - classPath.add(file.toURL()); - System.err.println("++ Added " + file + " to class path."); - } catch (MalformedURLException e) { - // ignore + List classPath = new ArrayList(); + classPath.add(MePixel.class.getProtectionDomain().getCodeSource().getLocation()); + for (String fileName : qtClassPath) { + File file = new File(fileName); + if (file.exists()) { + try { + classPath.add(file.toURL()); + System.err.println("++ Added " + file + " to class path."); + } catch (MalformedURLException e) { + // ignore + } + } } - } - } - URLClassLoader classLoader = new URLClassLoader(classPath.toArray(new URL[0]), ClassLoader.getSystemClassLoader()); - Thread.currentThread().setContextClassLoader(classLoader); + URLClassLoader classLoader = new URLClassLoader(classPath.toArray(new URL[classPath.size()]), ClassLoader.getSystemClassLoader()); + Thread.currentThread().setContextClassLoader(classLoader); - // for the sake of Java Web Start it is necessary to uninstall the security manager - if (System.getSecurityManager() != null) { - System.err.println("Launcher: uninstalling security manager ..."); - System.setSecurityManager(null); - } + // for the sake of Java Web Start it is necessary to uninstall the security manager + if (System.getSecurityManager() != null) { + System.err.println("Launcher: uninstalling security manager ..."); + System.setSecurityManager(null); + } - try { - Policy.getPolicy().refresh(); - } catch (Exception e) { - e.printStackTrace(); - } + try { + Policy.getPolicy().refresh(); + } catch (Exception e) { + e.printStackTrace(); + } - System.err.println(); - try { - // load and start main class - Class mainClass = classLoader.loadClass("com.thinkberg.mepixel.MePixel"); - final Constructor mainContructor = mainClass.getConstructor(String[].class); - mainContructor.newInstance(new Object[]{args}); - } catch (Exception e) { - e.printStackTrace(); + System.err.println(); + try { + // load and start main class + Class mainClass = classLoader.loadClass("com.thinkberg.mepixel.MePixel"); + final Constructor mainContructor = mainClass.getConstructor(String[].class); + mainContructor.newInstance(new Object[]{args}); + } catch (Exception e) { + e.printStackTrace(); + } } - } }