/*
 * panorama.c - distort textures to create a panorama effect.
 *
 * Version 1.0
 * Copyright (C) 1998 Gustav Taxen.
 * 
 * This is free software with ABSOLUTELY NO WARRANTY.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <malloc.h>
#include <assert.h>
#include <stdio.h>

#include <image.h>
#include <distort.h>


void
readInputFiles (char *argv[], image *src)
{
  char  str[1024];

  sprintf (str, "%s_ft.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[0], str));
  sprintf (str, "%s_lf.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[1], str));
  sprintf (str, "%s_bk.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[2], str));
  sprintf (str, "%s_rt.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[3], str));
  sprintf (str, "%s_up.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[4], str));
  sprintf (str, "%s_dn.ppm", argv[1]);
  printf ("%s: %d bytes read.\n", str, readImage (&src[5], str));
}


void
writeOutputFiles (char *argv[], image *src)
{
  char  str[1024];

  sprintf (str, "%s_ft.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[0]));
  sprintf (str, "%s_lf.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[1]));
  sprintf (str, "%s_bk.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[2]));
  sprintf (str, "%s_rt.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[3]));
  sprintf (str, "%s_up.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[4]));
  sprintf (str, "%s_dn.ppm", argv[2]);
  printf ("%s: %d bytes written.\n", str, writeImage (str, &src[5]));
}


void
initDestinationImages (image    *dst,
		       image    *src)
{
  int    i;
  
  for (i = 0; i < 6; i++)
    {
      dst[i].width = src[i].width;
      dst[i].height = src[i].height;
      dst[i].data = (char *) malloc (sizeof (char) *
				     dst[i].width *
				     dst[i].height *
				     3);
      assert (dst[i].data);
    }
}


int
main (int     argc,
      char   *argv[])
{
  image src[6];            /* source images */
  image dst[6];

  if (argc < 3)
    {
      printf ("Usage:\n\npanorama infilespec outfilespec\n\n");
      printf ("The input images are assumed to be named\n\n");
      printf ("infilespec_ft.ppm\n");
      printf ("infilespec_lf.ppm\n");
      printf ("infilespec_bk.ppm\n");
      printf ("infilespec_rt.ppm\n");
      printf ("infilespec_up.ppm\n");
      printf ("infilespec_dn.ppm\n\n");
      printf ("(analogous for the output files)\n");
      return 0;
    }

  printf ("Reading input files...\n");
  readInputFiles (argv, src);

  printf ("Initializing destination images...\n");
  initDestinationImages (dst, src);

  printf ("Distorting...\n");
  distort (dst, src);

  printf ("Writing output images...\n");
  writeOutputFiles (argv, dst);

  return 0;
}

