001package algs55; // section 5.5
002import stdlib.*;
003import java.awt.Color;
004/* ***********************************************************************
005 *  Compilation:  javac PictureDump.java
006 *  Execution:    java PictureDump width height < file
007 *  Dependencies: BinaryIn.java Picture.java
008 *  Data file:    http://introcs.cs.princeton.edu/stdlib/abra.txt
009 *
010 *  Reads in a binary file and writes out the bits as w-by-h picture,
011 *  with the 1 bits in black and the 0 bits in white.
012 *
013 *  % more abra.txt
014 *  ABRACADABRA!
015 *
016 *  % java PictureDump 16 6 < abra.txt
017 *
018 *************************************************************************/
019
020public class PictureDump {
021        private static BinaryIn binaryIn;
022
023        public static void main(String[] args) {
024                binaryIn = new BinaryIn("data/mobydick.txt");
025                //binaryIn = new BinaryIn("lib/Jama.jar");
026                args = new String[] { "500", "500" };
027
028                int width = Integer.parseInt(args[0]);
029                int height = Integer.parseInt(args[1]);
030                Picture pic = new Picture(width, height);
031                int count = 0;
032                for (int i = 0; i < height; i++) {
033                        for (int j = 0; j < width; j++) {
034                                pic.set(j, i, Color.RED);
035                                if (!binaryIn.isEmpty()) {
036                                        count++;
037                                        boolean bit = binaryIn.readBoolean();
038                                        if (bit) pic.set(j, i, Color.BLACK);
039                                        else     pic.set(j, i, Color.WHITE);
040                                }
041                        }
042                }
043                pic.show();
044                StdOut.println(count + " bits");
045        }
046}