# include "data.h"

int resize (int argc, char **argv) {

  char buffer[128], buffer2[128], *end;
  double NX, NY;
  int N, Source, Nsource, IsImage;
  
  /* display source */
  Nsource = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Nsource = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (N = get_argument (argc, argv, "-g")) {
    remove_argument (N, &argc, argv);
    SetImageDevice (FALSE);
  }  
  if (N = get_argument (argc, argv, "-i")) {
    remove_argument (N, &argc, argv);
    SetImageDevice (TRUE);
  }  
  IsImage = GetCurrentDevice ();
  if (IsImage) {
    if (!GetImage (&Source, &Nsource)) return (FALSE);
  } else {
    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: resize NX NY [-n] [-g | -i]\n");
    return (FALSE);
  }

  /* NX & NY are pixels for the screen & points for PS 
     convert units to points (1in = 72pt, 1cm = 28pt) */

  /* have kapa convert physical units to screen units 
     for now, fixed at 96 pix / in == 38 pix / cm
  */

  NX = strtod (argv[1], &end);
  if (!strcmp (end, "in")) { NX *= 96; }
  if (!strcmp (end, "cm")) { NX *= 38; }

  NY = strtod (argv[2], &end);
  if (!strcmp (end, "in")) { NY *= 96; }
  if (!strcmp (end, "cm")) { NY *= 38; }

  write (Source, "RSIZ", 4); /* tell Source to resize the image */

  sprintf (buffer, "%d %d", (int) NX, (int) NY);
  sprintf (buffer2, "NBYTES: %6d ", strlen (buffer));
  write (Source, buffer2, 16);
  write (Source, buffer, strlen (buffer));

  return (TRUE);

}

