/*
	name:	tga.c

	Targa 24-bit color picture support
	----------------------------------

	These routines enable image input/output in 24-bit targa format.


    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"


	unsigned char	tgadata[parraysize*3];

	short	ReadTgaInverted;


/* ---------------------------------------------------------------------
   WriteTgaHeader()
   --------------------------------------------------------------------- */

void WriteTgaHeader(FILE *f,long width, long height)
{
	long	i;
	char	tga_header[18];

	for(i=0;i<18;i++) tga_header[i]=(char)0;	/* Init header (all entries = 0) */
	tga_header[2]=(char) 2;
	tga_header[12]=(char) (width&0xff);
	tga_header[13]=(char) (width>>8);
	tga_header[14]=(char) (height&0xff);
	tga_header[15]=(char) (height>>8);
	tga_header[16]=(char) 24;
	tga_header[17]=(char) 0x20;	/* Make picture appear "right" */
					/* (upper left corner = first byte in body) */

	for(i=0;i<18;i++) { fputc(tga_header[i],f); }	/* Write header to file */
}


/* ---------------------------------------------------------------------
   CleanupTga()
   --------------------------------------------------------------------- */

void CleanupTga(FILE *f)
{
	(void) fseek(f,14L,SEEK_SET);
	fputc((int)(RenderedLines&0xff),f);
	fputc((int)((RenderedLines>>8)&0xff),f);
}


/* ---------------------------------------------------------------------
   WriteTgaLine()
   --------------------------------------------------------------------- */

void WriteTgaLine(FILE *f, long width)
{
	long	i;
	unsigned char *pixels;

	pixels=tgadata;
	for(i=0;i<width;i++) {
	    *pixels++=pixelarray[i].b;	/* Use order: b,g,r (Intel-style) */
	    *pixels++=pixelarray[i].g;
	    *pixels++=pixelarray[i].r;
	}
	(void) fwrite((void *)tgadata,sizeof(unsigned char),(size_t)(width*3),f);
}


/* ---------------------------------------------------------------------
   ReadTgaHeader()
   --------------------------------------------------------------------- */

void ReadTgaHeader(FILE *f, long *width, long *height)
{
	long	temp;

	temp=0L;
	(void) fseek(f,12L,SEEK_SET);
	temp=fgetc(f); temp+=(fgetc(f)<<8); *width=temp&0xffff;
	temp=fgetc(f); temp+=(fgetc(f)<<8); *height=temp&0xffff;
	temp=fgetc(f);
	if(temp!=24) {
	    fprintf(textoutput,"\nError: Not a 24-bit Targa image\n");
	    *width=*height=-1;
	}
	if((fgetc(f)&0x20)==0) ReadTgaInverted=TRUE;
	else ReadTgaInverted=FALSE;
}


/* ---------------------------------------------------------------------
   ReadTgaImage()
   --------------------------------------------------------------------- */

short ReadTgaImage(int *ImageNum, FILE *f, long width, long height)
{
	short	ImageType;
	int	i,j,index,done;
	unsigned char *Tgaptr;
	IMAGE24	*Image24;
	unsigned char *Body;


	ImageType=IMG_NONE;

	if(Num24Images<maximages) {
	    Image24=(void *)malloc(sizeof(IMAGE24));
	    if(Image24!=NULL) {
		Image24->XSize=width; Image24->YSize=height;
		Image24->Interpolate=INTPOL_NONE;
		Image24->Maptype=MAP_PLANAR;
		Image24->Tile=TILE_TRUE;
		Image24->Body=(unsigned char *)malloc((size_t)(width*height*3));
		if(Image24->Body!=NULL) {
		    done=FALSE;
		    if(ReadTgaInverted==TRUE) i=height-1;
		    else i=0;
		    while(done==FALSE) {
			(void) fread(tgadata,(size_t)1,(size_t)(3*width),f);
			Body=Image24->Body; Tgaptr=tgadata;
			for(j=0;j<width;j++) {
			    index=3*(i*width+j);
			    Body[index+2]=*Tgaptr++;
			    Body[index+1]=*Tgaptr++;
			    Body[index]=*Tgaptr++;
			}
			if(ReadTgaInverted==TRUE) {
			    i--;
			    if(i<0) done=TRUE;
			}
			else {
			    i++;
			    if(i>=height) done=TRUE;
			}
		    }
		    *ImageNum=Num24Images;
		    Img24Array[Num24Images]=Image24;
		    Num24Images++;
		    ImageType=IMG_24BIT;
		}
		else {
		    free(Image24);
		    Image24=NULL;
		}
	    }
	}
	else {
	    fprintf(textoutput,"\nError: Maximum amount of 24-bit images exceeded\n");
	}

	return(ImageType);
}
