# include "data.h"

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

  char filename[1024];
  int N, Source, Nsource, IsImage, IsPNG;
  
  if ((N = get_argument (argc, argv, "--help"))) {
    gprint (GP_ERR, "USAGE: jpeg [-name file] [-g | -i] [-n device] [-ppm]\n");
    return (FALSE);
  }

  /* image type */
  IsPNG = TRUE;
  if ((N = get_argument (argc, argv, "-ppm"))) {
    remove_argument (N, &argc, argv);
    IsPNG = FALSE;
  }

  /* file name */
  filename[0] = 0;
  if ((N = get_argument (argc, argv, "-name"))) {
    remove_argument (N, &argc, argv);
    strcpy (filename, argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* 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);
    if (!filename[0]) strcpy (filename, "Ximage.jpg");
    KiiJPEG (Source, filename);
  } else {
    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
    if (!filename[0]) strcpy (filename, "Xgraph.png");
    if (IsPNG) {
      KapaPNG (Source, filename);
    } else {
      KapaPPM (Source, filename);
    }
  }
  return (TRUE);
}

/* jpeg converts graph to png or ppm
   jpeg converts image to jpeg */
