
static char rcsid[] = "some thing wjr's assert makes me put here\n";


#include <stdio.h>
#include <string.h>

#include "clarg.h"
#include "image.h"


static char *USAGE =
  "\n%s:  Print the comment of a PBM++ image file.\n"
  "\n"
  "   -verbose:     Print the maximum and minimum values too.\n"
  "\n"
  "Any number of filenames are accepted on the command line,\n"
  "and their comments, types, and dimensions are printed.  If\n"
  "no files are specified, input is taken from stdin.\n\n";

#define VERBOSE "v.erbose"

static CLINIT clinit[] = {{VERBOSE, 0,     0, NULL, 0},
			  {NULL,    0, 10000, NULL, 0}};
static char *depend[] = {NULL};


#define MXCOL 70

void output_doc(AnyImage im, boolean verbose);
void output_max_and_min(AnyImage im);

int main(int argc, char **argv, char **envp)
{
  AnyImage im;
  int i, n, j;
  boolean verbose;
  

  if (cl_init(argc, argv, envp, clinit, depend, USAGE, CL_NORESTRICT))
    return 0;

  verbose = cl_opt(VERBOSE);

  if ((n = cl_numarg(NULL)) == 0) {
    im = (AnyImage) imLoad(IMAGE_ANY, NULL);

    if (im == (AnyImage) NULL) {
      fprintf(stderr, "IMDOC: Error reading an image from stdin\n");
      return 0;
    }

    output_doc(im, verbose);
    imFree(im);
  }
  else {
    for (i = 1; i <= n; i++) {
      char *name = cl_arg(NULL, i);
	
      im = (AnyImage) imLoad(IMAGE_ANY, name);

      if (im == (AnyImage) NULL) {
	fprintf(stderr, "IMDOC: Error reading an image from \"%s\"\n", name);
	continue;
      }

      if (n > 1) {
	int len = strlen(name) + 2;

	if (i == 1)
	  putchar('\n');
	
	for (j = 0; j < MXCOL/2 - len/2; j++)
	  putchar('=');

	printf(" %s ", name);

	for (j += len; j < MXCOL; j++)
	  putchar('=');	
      }
      
      output_doc(im, verbose);
      imFree(im);
    }

    if (n > 1) {
      for (j = 0; j < MXCOL; j++)
	putchar('=');
    }
    printf("\n\n");
  }

  return 0;
}


void output_doc(AnyImage im, boolean verbose)
{
  char *type;


  switch (imGetType(im)) {
  case IMAGE_GRAY:    type = "gray image (PGM)";     break;
  case IMAGE_FLOAT:   type = "float image (PFM)";    break;
  case IMAGE_BINARY:  type = "binary image (PBM)";   break;
  case IMAGE_LONG:    type = "long image (PLM)";     break;
  case IMAGE_DOUBLE:  type = "double image (PDM)";   break;
  case IMAGE_RGB:     type = "RGB image (PPM)";      break;
  case IMAGE_SHORT:   type = "short image (PSM)";    break;
  case IMAGE_PTR:     type = "pointer image";        break;
  case IMAGE_RGBF:    type = "RGB-Float image";      break;
  case IMAGE_HSV:     type = "HSV image";            break;
  case IMAGE_STRUCT:  type = "Struct image";         break;
  default:            type = "unrecognized image";   break;
  }

  printf("\n\n A %d by %d ", imGetWidth(im), imGetHeight(im));
  printf(type);

  if (verbose) {
    output_max_and_min(im);
  }
    
  printf("\n%s\n\n", strGet(imDocString(im)));
}


void output_max_and_min(AnyImage im)
{
  switch (imGetType(im)) {
  case IMAGE_GRAY:  { uchar max, min;
		      imMaxAndMin((GrayImage) im, max, min);
		      printf("     Values: [%u .. %u]", min, max);
		      break;
		    }
  case IMAGE_FLOAT: { float max, min;
		      imMaxAndMin((FloatImage) im, max, min);
		      printf("     Values: [%f .. %f]", min, max);
		      break;
		    }
  case IMAGE_BINARY:{ char max, min;
		      imMaxAndMin((BinaryImage) im, max, min);
		      printf("     Values: [%d .. %d]", min, max);
		      break;
		    }
  case IMAGE_LONG:  { long max, min;
		      imMaxAndMin((LongImage) im, max, min);
		      printf("     Values: [%ld .. %ld]", min, max);
		      break;
		    }
  case IMAGE_DOUBLE:{ double max, min;
		      imMaxAndMin((DoubleImage) im, max, min);
		      printf("     Values: [%f .. %f]", min, max);
		      break;
		    }
  case IMAGE_SHORT: { short max, min;
		      imMaxAndMin((ShortImage) im, max, min);
		      printf("     Values: [%hd .. %hd]", min, max);
		      break;
		    }
  case IMAGE_RGB:
  case IMAGE_PTR:
  default:
    break;
  }
}

