/*
	name:	pic.c

	Multiple picture formats support
	--------------------------------


    This source-code is part of the RayLab 1.1 package, and it is provided
    for your compiling pleasure.  You may use it, change it, re-compile it
    etc., as long as nobody else but you receive the changes/compilations
    that you have made!

    You may not use any part(s) of this source-code for your own productions
    without the permission from the author of RayLab. Please read the legal
    information found in the users documentation for RayLab for more details.

*/

#include <stdlib.h>

#include  "defs.h"
#include  "extern.h"


/* ---------------------------------------------------------------------
   WritePicHeader()
   --------------------------------------------------------------------- */

void WritePicHeader(FILE *f, long width, long height)
{
	if(OutputFormat==FORMAT_TGA)
	    WriteTgaHeader(f,width,height);
	else if(OutputFormat==FORMAT_IFF)
	    WriteIffHeader(f,width,height);
	else if(OutputFormat==FORMAT_PPM)
	    WritePpmHeader(f,width,height);
}


/* ---------------------------------------------------------------------
   WritePicLine()
   --------------------------------------------------------------------- */

void WritePicLine(FILE *f, long width)
{
	if(OutputFormat==FORMAT_TGA)
	    WriteTgaLine(f,width);
	else if(OutputFormat==FORMAT_IFF)
	    WriteIffLine(f,width);
	else if(OutputFormat==FORMAT_PPM)
	    WritePpmLine(f,width);
	RenderedLines++;		/* Count the amount of written lines */
}


/* ---------------------------------------------------------------------
   CleanupPic()
   --------------------------------------------------------------------- */

void CleanupPic(FILE *f)
{
	if(OutputFormat==FORMAT_TGA)
	    CleanupTga(f);
	else if(OutputFormat==FORMAT_IFF)
	    CleanupIff(f);
	else if(OutputFormat==FORMAT_PPM)
	    CleanupPpm(f);
}



/* ---------------------------------------------------------------------
   ReadPicHeader()
   --------------------------------------------------------------------- */

void ReadPicHeader(char *Name, int Format, FILE **f, long *width, long *height)
{
	if((*f=fopen(Name,"rb"))==NULL) {
	    fprintf(textoutput,"\nError: Could not open image file \"%s\"\n",Name);
	    *width=*height=-1;
	}
	else {
	    if(Format==FORMAT_TGA)
		ReadTgaHeader(*f,width,height);
	    else if(Format==FORMAT_IFF)
		ReadIffHeader(*f,width,height);
	    else if(Format==FORMAT_PPM)
		ReadPpmHeader(*f,width,height);
	}
}


/* ---------------------------------------------------------------------
   ReadImage()
   --------------------------------------------------------------------- */

short ReadImage(int *ImageNum,int Format, FILE *f, long width, long height)
{
	short	ImageType;

	if(Format==FORMAT_TGA)
	    ImageType=ReadTgaImage(ImageNum,f,width,height);
	else if(Format==FORMAT_IFF)
	    ImageType=ReadIffImage(ImageNum,f,width,height);
	else if(Format==FORMAT_PPM)
	    ImageType=ReadPpmImage(ImageNum,f,width,height);
	else {
	    ImageType=IMG_NONE;
	    *ImageNum=0;
	}

	switch(ImageType) {
	    case IMG_24BIT:
		ClearTransform(&Img24Array[*ImageNum]->Transform);
		break;
	    case IMG_8BIT:
		ClearTransform(&Img8Array[*ImageNum]->Transform);
		break;
	    case IMG_NONE:
	    default:
		break;
	}

	return(ImageType);
}
