/* IFFhdr V1.0 - a IFF ILBM file header reader and information reporter  (= */
/*   written by Chris Hillery  --  Copyright (C) 1990 CeejaTec Enterprises  */

/* Oh boy, another new software company...                                  */

/* This program is about as simple as they come-just type "IFFhdr <files>"  */
/* and it will report the picture and screen dimensions of any IFF ILBM     */
/* files it comes across. Easy; just a little reference tool.               */


/* This program is freely redistributable, but I maintain the copyright. In */
/* other words, give it to whomever you want, but don't change anything.    */
/* Specifically, please include this doc/source with the binary program.    */
/* If you receive this program without this doc/source file, please let me  */
/* know.  :)                                                                */

/* This program is simply V1.0 of what will eventually be a more extensive  */
/* program to deal with referencing many (all?) IFF files, graphics and     */
/* otherwise. Later versions of this program will hopefully include some    */
/* more options and cover more ground (read: SHAM). If you can think of any */
/* little features to add, or if you come across any bugs or perceived      */
/* flaws (not necessarily the same thing), or if (!) you like this program  */
/* SO much you want to encourage me with a few dollars (who am I kidding?), */
/* by all means write to me at one of the below addresses.                  */

/* Now comes to real plea: I know this program ain't much, really, but hey, */
/* I'm just starting out. This is mainly a programming exercise so far.     */
/* SO, if you have ANY suggestions to improve the size, speed, cleanliness, */
/* functionality, appearance, usage, style or literary significance of this */
/* program, PLEASE also write to me at an address below. I'm looking for    */
/* things like: "Hey, you know, your code would be half this size if you.." */
/* I'm already planning on replacing all these commands with their Amiga    */
/* library equivalents (Open, etc) to see how that affects things (THIS     */
/* version of IFFhdr is also designed to run on my friend's IBM clone, so I */
/* wrote it with (I believe) straight ANSI C). Anyway, any suggestions to   */
/* a new programmer would be appreciated. Thank you, Amiga Community!  (=   */

/* And look for more new slightly useful little utilities coming SOON from  */

/*                       CeejaTec Enterprises!                              */
/*                     "The New Kid on the Block"   (no relation)           */
/*                                                                          */
/*   (Hey, I might write something good SOME day...)                        */

/*                                          ---Chris Hillery (aka Ceej)     */
/*                                          Founder, President, C.E.O.,     */
/*                                          Resident Deity and Sole Member, */
/*                                          CeejaTec Enterprises            */

/* Mail me at:         (my name here)
                       1004 MacDonald
                       Commons Mail Room, RPI
                       Troy, NY 12180-3590                                  */

/* After May 1991:     (here my name)
                       8207 Lakespring Court
                       West Chester, Ohio 45069                             */

/* Alternately (and preferably unless you're (gasp!) sending donations),    */
/* email me on the Internet (I don't know other net.routes, sorry..) at:    */
/*                          ceej@pawl.rpi.edu                               */


/*  -----cut here-----CUT HERE-----Cut Here-----cUt hErE-----CUt hERe-----  */




#include <stdio.h>
#include <string.h>
#define copyright "Written by \33[1mChris Hillery\33[0m -- Copyright \
\251 1990 \33[3mCeejaTec Enterprises\33[0m\n\n"

FILE *fp;
char work[5]="\0\0\0\0";

void shut()
{
 fclose(fp);
}

get4 ()
{
 return(fread(work,1,4,fp));
}

void goBMHDhunk()
{
 unsigned int size;

 get4();
 fread(&size,sizeof(unsigned int),1,fp);
 if (strcmp(work,"BMHD"))
  {
   fseek(fp,size,1);
   goBMHDhunk();
  }
}

main (int argc,char *argv[])
{
 unsigned char stuff[20];
 register unsigned char bits;
 register unsigned short int loop;

 printf ("\n\33[33mIFFhdr\33[0m V1.0 - Amiga IFF ILBM file header info reader\n");
 printf (copyright);
 if (argc == 1)
  {
   printf("Usage: IFFhdr <filename> [<filename> ...]\n\n");
   return(1);
  }
 printf ("Reading header info:\n--------------------\n");
 for (loop=1;loop!=argc;loop++)
  {
   printf("File: %s\n",argv[loop]);
   if (!(fp=fopen(argv[loop],"rb")))
    {
     printf ("  Can't open file %s for input!\n\n",argv[loop]);
     continue;
    }
   fseek(fp,8,0);
   if (get4() != 4)
    {
     printf("  Error reading file %s!\n\n",argv[loop]);
     shut();
     continue;
    }
   if (strcmp(work,"ILBM"))
    {
     printf("  %s is not an IFF ILBM file!\n\n",argv[loop]);
     shut();
     continue;
    }
   goBMHDhunk();
   fread(stuff,20,1,fp);
   printf("  Bitmap: %dx%d",stuff[0]*256+stuff[1],stuff[2]*256+stuff[3]);
   printf("  Screen: %dx%d",stuff[16]*256+stuff[17],stuff[18]*256+stuff[19]);
   bits=stuff[8];
   printf("  Bitplanes: %d ",bits);
   if (bits==6)
     printf("(HAM mode)\n\n");

/* I know this is a bit of a cop-out; is it a valid one? (ie, can there be  */
/* a 6-bitplane NON-HAM IFF ILBM file?                                      */

    else
     printf("(%d colors)\n\n",1<<bits);
   shut();
  }
}
