Program reads first argument (name) and then converts name.txt to name.png.
Here is smile.txt:
10 140 140 140 140 140 140 140 140 140 140 140 10 120 0 0 0 0 0 0 0 0 0 0 0 200 120 0 0 0 0 0 0 0 0 0 0 0 200 120 0 0 255 255 0 0 0 255 255 0 0 200 120 0 0 255 255 0 0 0 255 255 0 0 200 120 0 0 0 0 0 0 0 0 0 0 0 200 120 0 0 0 0 0 255 0 0 0 0 0 200 120 0 0 0 0 0 0 0 0 0 0 0 200 120 0 255 0 0 0 0 0 0 0 255 0 200 120 0 0 255 255 255 255 255 255 255 0 0 200 120 0 0 0 0 0 0 0 0 0 0 0 200 120 0 0 0 0 0 0 0 0 0 0 0 200 80 160 160 160 160 160 160 160 160 160 160 160 80
Here is smile.png
Then after running the following code:
c:\ece538\2019\impro2>java -jar showSmImage.jar smile.png resized smile.png by 16
TextImage.java
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.File; import java.awt.image.*; import javax.imageio.*; public class TextImage { public static void main(String[] args) { String name, inf, outf; if (args.length<1) name = "smile"; else name = args[0]; int type = BufferedImage.TYPE_INT_RGB; BufferedImage out = null; int value[] = null; int v,r,g,b; int npix=0; int j=0; inf = name + ".txt"; try ( BufferedReader br = new BufferedReader(new FileReader(inf)); ) { String s; while ((s = br.readLine()) != null) { String [] tokens = s.trim().split("[\\s]+"); if (out==null) { npix = tokens.length; System.out.format("image size %d x %d\n",npix,npix); out = new BufferedImage(npix,npix,type); value = new int[npix]; } for (int i=0; i<npix; i++) { v = Integer.parseInt(tokens[i])&0xFF; value[i] = 0xFF000000 | (v << 16) | (v << 8) | v; } out.setRGB(0, j++, npix, 1, value, 0, npix); } br.close(); } catch (IOException e) { System.out.println("file not found: " + inf); //e.printStackTrace(); } outf = name + ".png"; try { ImageIO.write(out, "png", new File(outf)); } catch (IOException e) { System.out.println("image write failed"); System.out.println(e); System.exit(-1); } } }
Maintained by John Loomis, updated Mon Mar 04 13:09:26 2019