Пример #1
0
        public static void compress()
        {
            int num  = 0;
            int num2 = 0;

            while (!BinaryStdIn.IsEmpty)
            {
                int num3 = BinaryStdIn.readBoolean() ? 1 : 0;
                if (num3 != num2)
                {
                    BinaryStdOut.write((char)num, 8);
                    num  = 1;
                    num2 = ((num2 != 0) ? 0 : 1);
                }
                else
                {
                    if (num == 255)
                    {
                        BinaryStdOut.write((char)num, 8);
                        num = 0;
                        BinaryStdOut.write((char)num, 8);
                    }
                    num = (int)((ushort)(num + 1));
                }
            }
            BinaryStdOut.write((char)num, 8);
            BinaryStdOut.close();
        }
Пример #2
0
 private static Node readTrie() {
     boolean isLeaf = BinaryStdIn.readBoolean();
     if (isLeaf) {
         return new Node(BinaryStdIn.readChar(), -1, null, null);
     }
     else {
         return new Node('\0', -1, readTrie(), readTrie());
     }
 }
Пример #3
0
 /**
  * Reads in a sequence of bytes from standard input and draws
  * them to standard drawing output as a width-by-height picture,
  * using black for 1 and white for 0 (and red for any leftover
  * pixels).
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     int width = Integer.parseInt(args[0]);
     int height = Integer.parseInt(args[1]);
     Picture picture = new Picture(width, height);
     for (int row = 0; row < height; row++) {
         for (int col = 0; col < width; col++) {
             if (!BinaryStdIn.isEmpty()) {
                 boolean bit = BinaryStdIn.readBoolean();
                 if (bit) picture.set(col, row, Color.BLACK);
                 else     picture.set(col, row, Color.WHITE);
             }
             else {
                 picture.set(col, row, Color.RED);
             }
         }
     }
     picture.show();
 }
Пример #4
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output in binary, k bits per line,
     * where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bitsPerLine = 16;
        if (args.length == 1) {
            bitsPerLine = Integer.parseInt(args[0]);
        }

        int count;
        for (count = 0; !BinaryStdIn.isEmpty(); count++) {
            if (bitsPerLine == 0) {
                BinaryStdIn.readBoolean();
                continue;
            }
            else if (count != 0 && count % bitsPerLine == 0) StdOut.println();
            if (BinaryStdIn.readBoolean()) StdOut.print(1);
            else                           StdOut.print(0);
        }
        if (bitsPerLine != 0) StdOut.println();
        StdOut.println(count + " bits");
    }
Пример #5
0
    /**
     * Reads a sequence of bits that represents a Huffman-compressed message from
     * standard input; expands them; and writes the results to standard output.
     */
    public static void expand() {

        // read in Huffman trie from input stream
        Node root = readTrie(); 

        // number of bytes to write
        int length = BinaryStdIn.readInt();

        // decode using the Huffman trie
        for (int i = 0; i < length; i++) {
            Node x = root;
            while (!x.isLeaf()) {
                boolean bit = BinaryStdIn.readBoolean();
                if (bit) x = x.right;
                else     x = x.left;
            }
            BinaryStdOut.write(x.ch, 8);
        }
        BinaryStdOut.close();
    }
Пример #6
0
 /**
  * Reads a sequence of bits from standard input; compresses
  * them using run-length coding with 8-bit run lengths; and writes the
  * results to standard output.
  */
 public static void compress() { 
     char run = 0; 
     boolean old = false;
     while (!BinaryStdIn.isEmpty()) { 
         boolean b = BinaryStdIn.readBoolean();
         if (b != old) {
             BinaryStdOut.write(run, LG_R);
             run = 1;
             old = !old;
         }
         else { 
             if (run == R-1) { 
                 BinaryStdOut.write(run, LG_R);
                 run = 0;
                 BinaryStdOut.write(run, LG_R);
             }
             run++;
         } 
     } 
     BinaryStdOut.write(run, LG_R);
     BinaryStdOut.close();
 }