#include "stdio.h"
#include "stdlib.h"

#define TRUE  1
#define FALSE 0
#define SWAP TRUE

/*
	Crap to view various things in SoftImage files
*/

int ReadInt(FILE *,int *,int);
int ReadShort(FILE *,short int *,int);

int main(int argc,char **argv)
{
	int n,aspect;
	short int width,height,itype,padding;
	FILE *fptr;

	if (argc < 2) {
		fprintf(stderr,"Usage: %s picfilename\n",argv[0]);
		exit(-1);
	}

	if ((fptr = fopen(argv[1],"r")) == NULL) {
		fprintf(stderr,"Unable to open file\n");
		exit(-1);
	}

	/* Read the header (magic number) */
	ReadInt(fptr,&n,SWAP);
	printf("1st 4 bytes : 0x%0x (Should be 5380f634)\n",n);

	/* Read the file identifier */
	fseek(fptr,88,SEEK_SET);
	ReadInt(fptr,&n,SWAP);
	printf("File identifier : %0x (Should be 50494354 == 'PICT')\n",n);

	/* Read the image size */
	ReadShort(fptr,&width,SWAP);
	ReadShort(fptr,&height,SWAP);
	printf("Image width : %ld\n",width);
   printf("Image height : %ld\n",height);

	/* Read some other header stuff */
	ReadInt(fptr,&aspect,SWAP);
   ReadShort(fptr,&itype,SWAP);
   ReadShort(fptr,&padding,SWAP);
	printf("Aspect : %d\n",aspect);
	printf("Interlace type : %ld\n",itype);
	printf("Padding : %d\n",padding);

	fclose(fptr);
}

/*
   Read a possibly byte swapped integer
*/
int ReadInt(FILE *fptr,int *n,int swap)
{
   unsigned char *cptr,tmp;

   if (fread(n,4,1,fptr) != 1)
      return(FALSE);
   if (swap) {
      cptr = (unsigned char *)n;
      tmp = cptr[0];
      cptr[0] = cptr[3];
      cptr[3] = tmp;
      tmp = cptr[1];
      cptr[1] = cptr[2];
      cptr[2] = tmp;
   }
   return(TRUE);
}

/*
   Read a possibly byte swapped short integer
*/
int ReadShort(FILE *fptr,short int *n,int swap)
{
   unsigned char *cptr,tmp;

   if (fread(n,2,1,fptr) != 1)
      return(FALSE);
   if (swap) {
      cptr = (unsigned char *)n;
      tmp = cptr[0];
      cptr[0] = cptr[1];
      cptr[1] = tmp;
   }
   return(TRUE);
}


