
/************************************************************************/
#define OP_NAME      "ppm2pgm_UConv"
#define VERSION      "V1.02"
#define DATE         "31.01.98"
#define AUTHOR       "Stefan Diener"
/************************************************************************/

#define NO_PSEUDO_PPM_MODE
#define NO_PSEUDO_PGM_MODE

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>

#include <STIMP/pgm.c>
#include <STIMP/ppm.c>

struct PPM_Info source;
struct PGM_Info desti;

void Do_It(void)
{
  int i, punkte;
  unsigned char *srcR,*srcG, *srcB, *dst;

  srcR=source.redData;
  srcG=source.greenData;
  srcB=source.blueData;
  dst=desti.Data;
  desti.maxval=source.maxval;

  punkte=source.width*source.height;

  for (i=0; i<punkte; i++) *dst++=((*srcR++)+(*srcG++)+(*srcB++)+1)/3;
}

int main(int argc,char **argv)
/* Hauptprogramm */
{
  /* moegliche Bildschirmausgaben unterdruecken */
  beVerbose=FALSE;

  /* Mindestzahl der Argumente pruefen */
  if (argc<3) exit(-1);

  /* Anzahl der Dateinamen überprüfen */
  if (FilenameCount(argc, argv)!=2) exit(-1);

  if (ReadPPMFile(GetFilename(1,argc,argv),&source)==0)
  {
    if (CreatePGMArray(source.height,source.width,&desti)==0)
    {
      Do_It();

      WritePGMFile(GetFilename(2,argc,argv),&desti);
      FreePGMArray(&desti);
    }
    FreePPMArray(&source);
  }

  PrintClosing();
  exit(0);
}

