// (c) 2000 Benjamin Fry, MIT Media Laboratory, fry@media.mit.edu
// Aesthetics + Computation Group, Massachussetts Institute of Technology


import java.io.*;


public class Utilities {
  static public String[] split(String what, char splitChar) {
    char chars[] = what.toCharArray();
    int splitCount = 1;
    for (int i = 0; i < chars.length; i++) {
      if (chars[i] == splitChar) splitCount++;
    }
    if (chars[chars.length-1] == splitChar) splitCount--;
    if (splitCount == 0) {
      String splits[] = new String[1];
      splits[0] = new String(what);
      return splits;
    }

    String splits[] = new String[splitCount];
    int splitIndex = 0;
    int startIndex = 0;
    for (int i = 0; i < chars.length; i++) {
      if (chars[i] == splitChar) {
	splits[splitIndex++] = 
	  new String(chars, startIndex, i-startIndex);
	startIndex = i + 1;
      }
    }
    if (startIndex != chars.length) {
      splits[splitIndex] =
	new String(chars, startIndex, chars.length-startIndex);
    }
    return splits;
  }


  static byte tiffHeader[] = {
    77, 77, 0, 42, 0, 0, 0, 8, 0, 9, 0, -2, 0, 4, 0, 0, 0, 1, 0, 0,
    0, 0, 1, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 3, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 2, 0, 3, 0, 0, 0, 3, 0, 0, 0, 122, 1, 6, 0, 3, 0, 
    0, 0, 1, 0, 2, 0, 0, 1, 17, 0, 4, 0, 0, 0, 1, 0, 0, 3, 0, 1, 21, 
    0, 3, 0, 0, 0, 1, 0, 3, 0, 0, 1, 22, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 23, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 8, 0, 8
  };

  static void writeTiffHeader(OutputStream os, int width, int height) 
  throws IOException {
    byte header[] = new byte[768];
    System.arraycopy(tiffHeader, 0, header, 0, tiffHeader.length);
    header[30] = (byte) ((width >> 8) & 0xff);
    header[31] = (byte) ((width) & 0xff);
    header[42] = header[102] = (byte) ((height >> 8) & 0xff);
    header[43] = header[103] = (byte) ((height) & 0xff);
    int count = width*height*3;
    header[114] = (byte) ((count >> 24) & 0xff);
    header[115] = (byte) ((count >> 16) & 0xff);
    header[116] = (byte) ((count >> 8) & 0xff);
    header[117] = (byte) ((count) & 0xff);
    os.write(header);
  }


  static public int reportPercent(int a, int b, int prev) {
    int percent = (int) ((100 * (float)a) / (float)b);
    if (percent != prev) System.out.println(percent + "%");
    return percent;
  }
}

