diff --git a/Tools/Authoring/PixelTextureGenerator.java b/Tools/Authoring/PixelTextureGenerator.java index 1cd4aac73a0774a3bb6d150fc77f67361cb68a84..7fa5640a9a7c33d625a5198aeeafae86eeae2c83 100644 --- a/Tools/Authoring/PixelTextureGenerator.java +++ b/Tools/Authoring/PixelTextureGenerator.java @@ -1,6 +1,5 @@ /* * PixelTextureGenerator.java - * http://web.nps.navy.mil/~brutzman/Savage/Tools/Authoring/PixelTextureGenerator.java * https://savage.nps.edu/Savage/Tools/Authoring/PixelTextureGenerator.java * Created on June 10, 2004, 12:24 AM * Modified on June 17, 2004, 1:46 AM @@ -9,15 +8,15 @@ * Revised 4 January 2006, Don Brutzman * Revised 3 January 2007, Don Brutzman * Revised 10 November 2018, Don Brutzman + * Revised 14 June 2020, Don Brutzman */ import java.awt.*; -import java.awt.event.*; import java.awt.image.*; import java.io.*; /** - * + * Generate X3D PixelTexture values. * @author Louis Gutierrez * @author Don Brutzman * References: @@ -27,90 +26,98 @@ import java.io.*; public class PixelTextureGenerator extends Frame { - private Image image; - private File outputFile; - private FileWriter out; - private int height, width; - private String outputFilename; + private Image image; + private File outputFile; + private FileWriter out; + private int height, width; + private String outputFilename; - static String UsageMessage = "usage: java PixelTextureGenerator imageName.ext [outputSceneName.x3d]"; + static String UsageMessage = "usage: java PixelTextureGenerator imageName.ext [outputSceneName.x3d]"; public PixelTextureGenerator(String imageFilename, String pOutputFilename) throws IOException { - try - { - String name = ""; - Toolkit toolkit = Toolkit.getDefaultToolkit(); - image = toolkit.getImage(imageFilename); - MediaTracker mediaTracker = new MediaTracker(this); - mediaTracker.addImage(image, 0); - try - { - mediaTracker.waitForID(0); //makes sure image loads - } - catch (InterruptedException ie) - { - System.err.println(ie); - System.exit(1); - } - - //string manipulation for output file and header - name = imageFilename.substring(imageFilename.lastIndexOf("\\")+1,imageFilename.lastIndexOf(".")); - if (pOutputFilename.length() == 0) - { - outputFilename = name + "PixelTexture.x3d"; - } - else outputFilename = pOutputFilename; + try + { + String name = ""; + Toolkit toolkit = Toolkit.getDefaultToolkit(); + image = toolkit.getImage(imageFilename); + MediaTracker mediaTracker = new MediaTracker(this); + mediaTracker.addImage(image, 0); + try + { + mediaTracker.waitForID(0); //makes sure image loads + } + catch (InterruptedException ie) + { + System.err.println(ie); + System.exit(1); + } + // string manipulation for output file and header + name = imageFilename.substring(imageFilename.lastIndexOf("\\")+1,imageFilename.lastIndexOf(".")); + if (pOutputFilename.length() == 0) + { + outputFilename = name + "PixelTexture.x3d"; + } + else outputFilename = pOutputFilename; - // System.out.println ("[outputFilename=" + outputFilename + "]"); + // System.out.println ("[outputFilename=" + outputFilename + "]"); - System.out.print (".."); - outputFile = new File( outputFilename ); - out = new FileWriter( outputFile ); - System.out.print ("."); + System.out.print (".."); + outputFile = new File( outputFilename ); + out = new FileWriter( outputFile ); + System.out.print ("."); - width = image.getWidth (null); - height = image.getHeight(null); - header(outputFilename, imageFilename, width, height); - handlePixels(image, 0, 0, width, height); - footer(width, height); - System.out.println ("created " + outputFile); - } - catch (Exception e) - { - System.err.println(e); - System.exit(1); - } + width = image.getWidth (null); + height = image.getHeight(null); + header(outputFilename, imageFilename, width, height); + handlePixels(image, 0, 0, width, height); + footer(width, height); + System.out.println ("created " + outputFile); + } + catch (IOException ioe) + { + System.err.println(ioe); + System.exit(1); + } } -/** - * This method uses pixelGrabber to grab pixels from an image and then each pixel is - * manipulated into a hex string and written to file. - */ - public void handlePixels(Image img, int x, int y, int imageWidth, int imageHeight) throws IOException + /** + * This method uses pixelGrabber to grab pixels from an image and then each + * pixel is manipulated into a hex string and written to file. + * + * @param inputImage image of interest + * @param x initial x pixel coordinate of interest + * @param y initial y pixel coordinate of interest + * @param imageWidth image width in pixels + * @param imageHeight image height in pixels + */ + public void handlePixels(Image inputImage, int x, int y, int imageWidth, int imageHeight) throws IOException { try { out.write(imageWidth + " " + imageHeight + " " + "3 "); //default is 256*256*256 colors out.write("\n"); int[] pixels = new int[imageWidth * imageHeight]; //array of pixels to grab - PixelGrabber pg = new PixelGrabber(img, x, y, imageWidth, imageHeight, pixels, 0, imageWidth); + PixelGrabber pixelGrabber = new PixelGrabber(inputImage, x, y, imageWidth, imageHeight, pixels, 0, imageWidth); try { - pg.grabPixels(); + pixelGrabber.grabPixels(); } catch (InterruptedException e) { System.err.println("[Error] interrupted waiting for pixels!"); return; } - if ((pg.getStatus() & ImageObserver.ABORT) != 0) { + if ((pixelGrabber.getStatus() & ImageObserver.ABORT) != 0) + { System.err.println("[Error] image fetch abort or error"); return; } //cycles through the array of pixels // SFImage: Pixels are specified from left to right, bottom to top // for (int j = 0; j < imageHeight; j++) { - for (int j = imageHeight - 1; j >= 0; j--) { - for (int i = 0; i < imageWidth; i++) { + for (int j = imageHeight - 1; j >= 0; j--) + { + for (int i = 0; i < imageWidth; i++) + { handleSinglePixel(x+i, y+j, pixels[j * imageWidth + i]); } // break long lines of pixels for countability and to help some parsers/editors @@ -118,18 +125,24 @@ public class PixelTextureGenerator extends Frame } out.flush(); } - catch (Exception e) + catch (IOException e) { System.err.println (e); } } -/** - * This method translates the integer value passed by pixeGrabber into - * alpha, red, green, and blue. It then converts these integers to a hex - * string. - */ - public void handleSinglePixel(int x, int y, int pixel) throws IOException{ + /** + * This method translates the integer value passed by pixeGrabber into + * alpha, red, green, and blue.It then converts these integers to a hex + * string. + * + * @param x x pixel coordinate of interest + * @param y y pixel coordinate of interest + * @param pixel pixel value + * @throws java.io.IOException file exception + */ + public void handleSinglePixel(int x, int y, int pixel) throws IOException + { //translates pixel integer int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; @@ -140,11 +153,17 @@ public class PixelTextureGenerator extends Frame hexConverter(red,green, blue); } -/** - * This method produces a string that represents a 6 digit hex value - * and writes it to file. - */ - public void hexConverter(int r,int g,int b) throws IOException{ + /** + * This method produces a string that represents a 6 digit hex value and + * writes it to file. + * + * @param r red value + * @param g green value + * @param b blue value + * @throws java.io.IOException file exception + */ + public void hexConverter(int r, int g, int b) throws IOException + { String temp = "0x"; temp += hexCase(r/16); @@ -159,41 +178,48 @@ public class PixelTextureGenerator extends Frame } /** - * pThis method simply changes standard form into hex + * This method simply changes standard form into hex */ - public char hexCase(int c) { + public char hexCase (int c) + { char out='Z'; - switch (c) { - case 0: out = '0'; break; - case 1: out = '1'; break; - case 2: out = '2'; break; - case 3: out = '3'; break; - case 4: out = '4'; break; - case 5: out = '5'; break; - case 6: out = '6'; break; - case 7: out = '7'; break; - case 8: out = '8'; break; - case 9: out = '9'; break; - case 10: out = 'A'; break; - case 11: out = 'B'; break; - case 12: out = 'C'; break; - case 13: out = 'D'; break; - case 14: out = 'E'; break; - case 15: out = 'F'; break; - default: System.out.println("incorrect color value"); - } + switch (c) { + case 0: out = '0'; break; + case 1: out = '1'; break; + case 2: out = '2'; break; + case 3: out = '3'; break; + case 4: out = '4'; break; + case 5: out = '5'; break; + case 6: out = '6'; break; + case 7: out = '7'; break; + case 8: out = '8'; break; + case 9: out = '9'; break; + case 10: out = 'A'; break; + case 11: out = 'B'; break; + case 12: out = 'C'; break; + case 13: out = 'D'; break; + case 14: out = 'E'; break; + case 15: out = 'F'; break; + default: System.out.println("incorrect color value"); + } return out; } -/** - * This method writes the header information to file - */ + /** + * This method writes the header information to file + * + * @param outputFileName + * @param imageFilename + * @param x initial x pixel coordinate of interest + * @param y initial y pixel coordinate of interest + * @throws java.io.IOException file exception + */ public void header(String outputFileName, String imageFilename, int x, int y) throws IOException{ out.write("<?xml version='1.0' encoding='UTF-8'?>\n"); // final DOCTYPE - out.write("<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.1//EN\" \"http://www.web3d.org/specifications/x3d-3.1.dtd\">\n"); - out.write("<X3D version='3.1' profile='Interactive'\n"); - out.write(" xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-3.0.xsd'>\n"); + out.write("<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.3//EN\" \"http://www.web3d.org/specifications/x3d-3.3.dtd\">\n"); + out.write("<X3D version='3.3' profile='Interactive'\n"); + out.write(" xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-3.3.xsd'>\n"); out.write(" <head>\n"); out.write(" <meta content='" + outputFileName + "' name='title'/>\n"); out.write(" <meta content='" + imageFilename + "' name='image'/>\n"); @@ -280,6 +306,5 @@ public class PixelTextureGenerator extends Frame System.out.println (UsageMessage); System.exit (-1); } - } }