/* JPG2DBN @author Warren Sack @version February 2003 **/ // Given a JPG (or GIF) image roughly square in shape, return a // version of the image encoded in DBN. It is encoded as a set // of assignments to array elements. Each element of the array // contains a color value. Six hundred and twenty-five array // elements are used to code the image. Thus, if the image is not // extremely small to start with, the encoded will be a degraded // version of the image. Use the utility LOAD_PHOTO.DBN to load // the encoded image into DBN and display it to the DBNB paper. import java.lang.*; import java.awt.*; import java.awt.Image; import java.awt.image.*; import java.awt.event.*; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.io.IOException; import java.io.OutputStream; import java.io.FileOutputStream; import javax.swing.ImageIcon; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class JPG2DBN { public static void main(String[] args) { String inFile= args[0]; String outFile = args[1]; JPG2DBN converter = new JPG2DBN(inFile,outFile); } public JPG2DBN(String inputFilename, String outputFilename) { writeDBNCode(inputFilename,outputFilename); System.exit(0); } public void writeDBNCode(String inputFilename, String outputFilename) { try { // Get the image from a file using the Swing ImageIcon class. Image inputImage = new ImageIcon(inputFilename).getImage(); // Find the dimensions of the input image double inImageWidth = (double)inputImage.getWidth(null); double inImageHeight = (double)inputImage.getHeight(null); // Recast the input image from an Image into a BufferedImage. BufferedImage inImage = new BufferedImage((int)inImageWidth,(int)inImageHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2din = inImage.createGraphics(); g2din.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2din.drawImage(inputImage, 0, 0, null); g2din.dispose(); // Find the width and height scales: remember that DBN paper is 25 pixels by 25 pixels double widthScale = ((double)25.0) / inImageWidth; double heightScale = ((double)25.0) / inImageHeight; // Create an output image buffer. BufferedImage outImage = new BufferedImage(25,25,BufferedImage.TYPE_INT_RGB); // Create the transform AffineTransform at = new AffineTransform(); at.scale(widthScale,heightScale); // Paint the extracted and scaled image onto the output image. Graphics2D g2d = outImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(inImage, at, null); g2d.dispose(); // Open the output file to write the DBN code. OutputStream os = new FileOutputStream(outputFilename); BufferedWriter printout = new BufferedWriter (new OutputStreamWriter(os)); // Loop through all of the pixels in the image and record each pixel with a "set [X Y] R G B statement in DBN int numberOfRows = 25; int numberOfColumns = 25; for (int i = 0; i <= (numberOfColumns - 1); i++) { for (int j = 0; j <= (numberOfRows - 1); j++) { int rgb = outImage.getRGB(i,j); int red = ((rgb & 0x00ff0000) >> 16); int redPortion = (int)((double)red/(double)2.55); if ( redPortion == 100 ) { redPortion = 99; } int green = ((rgb & 0x0000ff00) >> 8); int greenPortion = (int)((double)green/(double)2.55); if ( greenPortion == 100 ) { greenPortion = 99; } int blue = (rgb & 0x000000ff); int bluePortion = (int)((double)blue/(double)2.55); if ( bluePortion == 100 ) { bluePortion = 99; } int square = ( (numberOfRows - (j + 1)) * numberOfColumns ) + i + 1; String dbnStatement = "set " + redPortion + greenPortion + bluePortion + "\n"; printout.write(dbnStatement,0,dbnStatement.length()); } } // Close the output file printout.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } }