/* vim:expandtab:ts=2 sw=2:
*/
/*  Grafx2 - The Ultimate 256-color bitmap paint program
 *  Gif Analyzer tool
    Copyright 2010 Adrien Destugues
    Grafx2 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; version 2
    of the License.
    Grafx2 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 Grafx2; if not, see 
*/
#include 
#include 
#include 
#include 
int main(int argc, char* argv[])
{
	FILE* theFile;
	if(argc != 2)
	{
		printf("%s file.gif\n",argv[0]);
		exit(-1);
	}
	theFile = fopen(argv[1], "r");
	uint8_t buffer[256];
	fread(buffer, 1, 6, theFile);
  buffer[6] = 0;
	printf("Signature: %6s\n", buffer);
	uint16_t w,h;
	fread(&w,2,1,theFile);
	fread(&h,2,1,theFile);
	printf("Resolution: %dx%d\n",w,h);
  fread(buffer,1,3,theFile);
  int colors = 1 << (((buffer[0]&0b01110000)>>4)+1);
  int color_table_size = 1 << ((buffer[0]&0x7)+1);
  printf("Color palette: %#02.2x\n",buffer[0]&0xFF);
  if (buffer[0] & 0b10000000) {
    printf("\tGlobal palette");
    if(buffer[0] & 0b00001000)
      printf(" SORTED");
    printf("\n");
  }
  printf("\tColor count: %d\n", colors);
  printf("\tSize of color table : %d\t", color_table_size);
  printf("Index of background color: %d\n",buffer[1]);
  printf("pixel aspect ratio = %d\n",buffer[2]);
  if(buffer[2] != 0) printf("\t aspect ratio = %d/64\n", 15 + buffer[2]);
  printf("Color palette:\n");
  for (int i = 0; i < color_table_size; i++)
  {
    fread(buffer,1,3,theFile);
    /*printf("\t%d: %u %u %u\t",i,buffer[0], buffer[1], buffer[2]);*/
    printf("   %3d: #%02x%02x%02x",i,buffer[0], buffer[1], buffer[2]);
    if ((i+1)%8 ==0)puts("");
  }
  int i = 0;
  do {
    fread(buffer,1,1,theFile);
    i++;
  } while (buffer[0] != ',');
  if (i > 1)
    printf("Skipped %d meaningless bytes before image descriptor\n",i);
  uint16_t x,y;
	fread(&x,2,1,theFile);
	fread(&y,2,1,theFile);
	fread(&w,2,1,theFile);
	fread(&h,2,1,theFile);
  printf("Image descriptor\n");
  printf("\tx=%d y=%d w=%d h=%d\n",x,y,w,h);
	fclose(theFile);
}