void print_image () {
  int i;
  printf("I");
  for (i=0; i < ImageP; i++) {
    printf(" %i", ImageData[i]);
  }
  printf(" +\n");
}


void bin_print (int x) {
  int high, tmp, bit;
  tmp = x;
  for (high = 0; tmp > 0; high++) {
    tmp = tmp >> 1;
  }
  printf("0b");
  for (tmp = high; tmp != 0; tmp--) {
    bit = (x >> (tmp - 1)) & 1;
    printf("%i", bit);
  }
  printf("\n");
}

void bin_lenprint (int x, int len) {
  int tmp, bit;
  printf("0b");
  for (tmp = len; tmp != 0; tmp--) {
    bit = (x >> (tmp - 1)) & 1;
    printf("%i", bit);
  }
  printf("\n");
}

  
int trans_rgb(FILE *fp_in, FILE *fp_out, int tRed, int tGreen, int tBlue) {

  
  fpos_t restore_position;
  imageinfo iinfo;
  what_to_do directive;

  if (fgetpos(fp_in, &restore_position) != 0) {
    fprintf(stderr, "Error reading input file.\n");
    return(-2);
  }
  
  rewind(fp_in);
  
  if (read_gif_header(fp_in, &iinfo, fp_out) == -1) {
    fprintf(stderr, "Error: input file not in the GIF format.\n");
    fsetpos(fp_in, &restore_position);
  }

  init_trans_directive(&directive, tRed, tGreen, tBlue);
  
  read_gif_stream(fp_in, &iinfo, fp_out, directive);

  fsetpos(fp_in, &restore_position);
  return(0);
}

int PixelColor(FILE *fp_in, color *TheColor, int x, int y) {

  /* Return values:
   *   0 is success
   *  -1 is failure during read; file pointer was restored
   *  -2 is failure during fgetpos(); file pointer NOT restored.
   *  -3 is failure during write; file pointer was restored
   *  -4 is failure during gif decryption for now; probably malloc failure
   */
  
  fpos_t restore_position;
  imageinfo iinfo;
  what_to_do directive;

  if (fgetpos(fp_in, &restore_position) != 0) {
    fprintf(stderr, "Error reading input file.\n");
    return(-2);
  }

  if (read_gif_header(fp_in, &iinfo, NULL) == -1) {
    fprintf(stderr, "Error: input file not in the GIF format.\n");
    fsetpos(fp_in, &restore_position);
  }

  init_pixel_directive(&directive, x, y, TheColor);
  
  read_gif_stream(fp_in, &iinfo, NULL, directive);
  
  fsetpos(fp_in, &restore_position);
  return(0);
}

