package org.netevolved.png; import java.io.File; import ar.com.hjg.pngj.ImageInfo; import ar.com.hjg.pngj.ImageLineHelper; import ar.com.hjg.pngj.ImageLineInt; import ar.com.hjg.pngj.PngWriter; import ar.com.hjg.pngj.chunks.PngChunkTextVar; /** * grayscale image - distorted diagonal stripes */ public class CreatePNG { public void makeTestImage(PngWriter png) { int cols = png.imgInfo.cols; int rows = png.imgInfo.rows; png.getMetadata().setDpi(123.0); png.getMetadata().setTimeNow(0); png.getMetadata().setText(PngChunkTextVar.KEY_Software, "pngj test"); //double t1 = (cols + rows) / 16.0; // typical period ImageLineInt iline = new ImageLineInt(png.imgInfo); png.setIdatMaxSize(2000); for (int i = 0; i < rows; i++) { //double fase = Math.sin(1.3 * i / t1); for (int j = 0; j < cols; j++) { //double sin = Math.sin((i + j) * Math.PI / t1 + fase); float height = imageinfo[j][i]*100; if(height >60){ iline.getScanline()[j] = ImageLineHelper.clampTo_0_255((int) 50); } else{ iline.getScanline()[j] = ImageLineHelper.clampTo_0_255((int) 0); } } png.writeRow(iline, i); } png.end(); } public void createTest(String name, int cols, int rows) { PngWriter i2 = new PngWriter(new File(name), new ImageInfo(cols, rows, 8, false, true, false), true); makeTestImage(i2); System.out.println("Done: " + name); } public static void main(String[] args) throws Exception { // if (args.length < 3) { // System.err.println("Arguments: [pngdest] [cols] [rows]"); // System.exit(1); // } int cols =8193; int rows = 8193; CreatePNG png = new CreatePNG(); png.drawPlasma(8193,8193); png.createTest("testpng.png", cols, rows); } float[][] imageinfo = new float[8193][8193]; float Displace(float num) { float max = num / (float)(8193 + 8193) * 3; return ((float)Math.random() - 0.5f) * max; } //This is something of a "helper function" to create an initial grid //before the recursive function is called. void drawPlasma( int width, int height) { float c1, c2, c3, c4; //Assign the four corners of the intial grid random color values //These will end up being the colors of the four corners of the applet. c1 = (float)Math.random(); c2 = (float)Math.random(); c3 = (float)Math.random(); c4 = (float)Math.random(); DivideGrid( 0, 0, width , height , c1, c2, c3, c4); } //This is the recursive function that implements the random midpoint //displacement algorithm. It will call itself until the grid pieces //become smaller than one pixel. void DivideGrid( float x, float y, float width, float height, float c1, float c2, float c3, float c4) { float Edge1, Edge2, Edge3, Edge4, Middle; float newWidth = width / 2; float newHeight = height / 2; if (width > 2 || height > 2) { Middle = (c1 + c2 + c3 + c4) / 4 + Displace(newWidth + newHeight); //Randomly displace the midpoint! Edge1 = (c1 + c2) / 2; //Calculate the edges by averaging the two corners of each edge. Edge2 = (c2 + c3) / 2; Edge3 = (c3 + c4) / 2; Edge4 = (c4 + c1) / 2; //Make sure that the midpoint doesn't accidentally "randomly displaced" past the boundaries! if (Middle < 0) { Middle = 0; } else if (Middle > 1.0f) { Middle = 1.0f; } //Do the operation over again for each of the four new grids. DivideGrid( x, y, newWidth, newHeight, c1, Edge1, Middle, Edge4); DivideGrid( x + newWidth, y, newWidth, newHeight, Edge1, c2, Edge2, Middle); DivideGrid( x + newWidth, y + newHeight, newWidth, newHeight, Middle, Edge2, c3, Edge3); DivideGrid( x, y + newHeight, newWidth, newHeight, Edge4, Middle, Edge3, c4); } else //This is the "base case," where each grid piece is less than the size of a pixel. { //The four corners of the grid piece will be averaged and drawn as a single pixel. float c = (c1 + c2 + c3 + c4) / 4; imageinfo[(int) x][(int) y]= c; //x and y and c.. this is the final answer. } } }