import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.sql.Array; import javax.imageio.ImageIO; public class patronus { public static void gen(String origen, String destino) throws IOException { // Variables para el tratamiento de los ficheros de entrada / salida File inFile, outFile; FileInputStream inReader; FileWriter outWriter; // Cargamos el fichero inFile = new File(origen); outFile = new File(destino); if (!inFile.exists()) { System.out.println("Input file doesn't exist"); System.exit(-1); } inReader = new FileInputStream(inFile); // Calculamos tamaño del fichero y de la imagen resultante double fileSize = inFile.length(); String fileSizeStr = String.valueOf((long)fileSize); int imgSize = (int) (Math.sqrt(fileSize + fileSizeStr.length() + 1)); int rest = (int) (fileSize - imgSize*imgSize); int width = imgSize; if (rest > 0) width += rest / imgSize; while ((imgSize * width) < (fileSize + fileSizeStr.length() + 1)) width++; BufferedImage bufferedImage = new BufferedImage(width, imgSize, BufferedImage.TYPE_INT_RGB); // Comenzamos la lectura del fichero y la creación de la imagen int c; int x,y; x = y = 0; // Primero codificamos el tamaño real del fichero, para su posterior recuperación for (int i = 0; i= width) { x = 0; y++; } } // Insertamos caracter finalizador bufferedImage.setRGB(x,y, 255); x++; if (x >= width) { x = 0; y++; } while ((c = inReader.read())!= -1) { bufferedImage.setRGB(x,y,c); x++; if (x >= width) { x = 0; y++; } } ImageIO.write(bufferedImage, "png", outFile); inReader.close(); } public static void rev(String origen, String destino) throws IOException { // Variables para el tratamiento de los ficheros de entrada / salida File inFile, outFile; FileReader inReader; FileOutputStream outWriter; // Cargamos el fichero inFile = new File(origen); outFile = new File(destino); if (!inFile.exists()) { System.out.println("Input file doesn't exist"); System.exit(-1); } outWriter = new FileOutputStream(outFile); BufferedImage bufferedImage = ImageIO.read(inFile); // Comenzamos la lectura de la imagen y la recreación del fichero original int c; int x,y, maxX, maxY; x = y = 0; maxX = bufferedImage.getWidth(); maxY = bufferedImage.getHeight(); long size = 0, count = 1; StringBuffer sizeStr = new StringBuffer(); for (y=0; y size) break; } } if (count > size) break; } outWriter.close(); } public static void usage() { System.out.println("Usage: patronus "); System.out.println("\ngen: generate a PNG file from the source file"); System.out.println("rev: from a PNG file previosly generated, recover the original file"); System.out.println("\nThis software is distributed under GPL Licence"); System.out.println("This software is distributed without any guarantees"); System.out.println("Written by Víctor Henríquez (www.sendaoscura.com)"); System.exit(-1); } /** * Basado en la idea de Ruben Santamarta (Blog 48bits) * @throws IOException */ public static void main(String[] args) throws IOException { if (args.length != 3) usage(); if ("gen".equalsIgnoreCase(args[0])) gen(args[1], args[2]); else if ("rev".equalsIgnoreCase(args[0])) rev(args[1], args[2]); else usage(); } }