
/* Copyright (c) 1995 Greg Klanderman and MIT.  All Rights Reserved.
 *
 * Use, reproduction and preparation of derivative works of this software is
 * permitted.  Any copy of this software or of any derivative work must
 * include both the above copyright notice and this
 * paragraph.  This software is made available AS IS, and MIT
 * disclaims all warranties, express or implied.
 */

static char rcsid[] = "bin-gregk/imgs2strm.c";

/*   Greg Klanderman    Spring 1995
     
     this program makes a movie out of PNM+ files
*/

#include <stdio.h>

#include "clarg.h"
#include "image.h"
#include "string-support.h"


static char *USAGE =
  "\n%s:  Make an image stream from numbered image files.\n"
  "\n"
  "Arguments are:\n"
  "   -base file:           Specify the input file name, eg. `foo%%03d.pgm'\n"
  "   -downsample dx [dy]:  Specify the downsampling factor (default 1)\n"
  "   -from:                Specify the first image index (default 0)\n"
  "   -to:                  Specify the last image index (default all\n"
  "   -by:                  Specify the image index increment (default 1)\n"
  "   -custom <file>        Specify a custom ordering file\n"
  "\n\n";


#define  BASE 	"b.ase"
#define  DOWN 	"d.ownsample"
#define  FROM   "from" 
#define  TO     "to" 
#define  BY     "by" 
#define  CUSTOM "c.ustom"

static CLINIT clinit[] = {{BASE,   1,  1, NULL,    0},
			  {DOWN,   1,  2, cl_int,  0},
			  {FROM,   0,  1, cl_int,  0},
			  {TO,     0,  1, cl_int,  0},
			  {BY,     0,  1, cl_int,  0},
			  {CUSTOM, 0,  1, NULL,    0},
			  {NULL,   0,  0, NULL,    0}};


static char *depend[] = {BASE,
                         "if (#"FROM"   < 1) { $"FROM" }",
                         "if (#"TO"     < 1) { $"TO" }",
                         "if (#"BY"     < 1) { $"BY" }",
                         "if (#"CUSTOM" < 1) { $"CUSTOM" }",
                         "if (#"DOWN"  == 1) { @"DOWN"[ "DOWN"[1] : 2 ] }",
                         "!("CUSTOM" && ("FROM" || "TO" || "BY"))",
                         NULL};


int main(int argc, char **argv, char **envp)
{
  AnyImage img, tmp;
  int downx, downy, lo, hi, inc, i;
  char *base, *fn;
  String newdoc;
  char *custom;
  FILE *customfp=NULL;

  if (cl_init(argc, argv, envp, clinit, depend, USAGE, CL_NORESTRICT))  /* parse args */
    return 0;

  base  = cl_arg1(BASE);
  downx = cl_opt(DOWN) ? atoi(cl_arg1(DOWN)) : 1;
  downy = cl_opt(DOWN) ? atoi(cl_arg2(DOWN)) : 1;
  lo    = cl_opt(FROM) ? atoi(cl_arg1(FROM)) : 0;
  hi    = cl_opt(TO)   ? atoi(cl_arg1(TO))   : -1;
  inc   = cl_opt(BY)   ? atoi(cl_arg1(BY))   : 1;
  custom = cl_opt(CUSTOM) ? cl_arg1(CUSTOM) : NULL;
  
  fn = (char *) malloc(strlen(base) + 100);

  if (fn == NULL) {
    fprintf(stderr, "IMGS2STRM: Out of memory\n");
    exit(0);
  }

  if (custom) {
    customfp = fopen(custom, "r");
    if (customfp == NULL) {
      fprintf(stderr, "IMGS2STRM: Can't open custom file \"%s\"\n", custom);
      exit(0);
    }
  }

  for (i = lo; custom ? (fscanf(customfp, "%d", &i) == 1) : (hi == -1 || i <= hi); i += inc) {
    sprintf(fn, base, i);

    if ((img = (AnyImage) imLoad(IMAGE_ANY, fn)) == NULL) {
      if (custom ||  hi != -1  ||  i == lo)
        fprintf(stderr, "IMGS2STRM: Error reading input image \"%s\"\n", fn);

      return 0;
    }

    newdoc = strNewComment(argc, argv, envp, imDocString(img), NULLSTRING);
  
    if (downx > 1  ||  downy > 1) {
      tmp = img;
      img = imDown(tmp, downx, downy);
      imFree(tmp);
    }
    else {
      strFree(imDocString(img));
    }
    
    imDocString(img) = newdoc;

    if (imSaveF(img, stdout)) {
      fprintf(stderr, "IMGS2STRM: Error saving to stdout\n");
      return -1;
    }
  
    imFree(img);
  }
  
  if (custom) {
    fclose(customfp);
  }
  
  return 0;
}



