001: // structs and bitfield, the GIF image format example
002: #include<stdio.h>
003: #include<stdlib.h>
004: #include<math.h>
005:
006: // this struct correspond to the exact data organization of the initial
007: // header of a GIF file (13 bytes)
008: struct gif_header
009: {
010: // beginning of the file:
011: unsigned long signature :24; // it shouled contain 'G', 'I', 'F'
012: unsigned long version :16; // usually '8' '9' but also '8' '7' exists
013: unsigned long subversion :8; // typically 'a'
014:
015: // logical Screen Width:
016: unsigned long width :16; // canvas width
017: unsigned long height :16; // canvas height
018:
019: // packed fields:
020: unsigned long global_color :1; // true when the file features a global palette
021: unsigned long color_res :3; // used to determine how many colors we have in the image
022: unsigned long sort_flag :1; // true when colors in the palette are sorted according the frequency
023: unsigned long color_table_size :3; // used to compute the size of the palette
024:
025: unsigned long bg_color_index :8; // backround color (if any)
026: unsigned long pixel_aspect_ratio:8; // when != 0 can be used to compute pixel aspect ratio
027: };
028: // 13 bytes (actually we should use __attribute__((packed)) to obtain this)
029:
030:
031:
032: void print_gif_data(const char *);
033:
034: int main(int argc, char **argv)
035: {
036:
037: printf("struct gif_header needs %ld bytes to be stored\n", sizeof(struct gif_header));
038:
039: print_gif_data("files/logical.gif");
040: print_gif_data("files/test.gif");
041: print_gif_data("files/test2.gif");
042: print_gif_data("files/test3.gif");
043: print_gif_data("files/animated.gif");
044: print_gif_data("files/gif87a.gif");
045: print_gif_data("files/test.jpg");
046:
047: return 0;
048: }
049:
050: void print_gif_data(const char *s)
051: {
052:
053: FILE *bf;
054:
055: bf=fopen(s, "rb");
056: if(!bf)
057: {
058: perror("\nError in opening the file");
059: return;
060: }
061:
062: struct gif_header a;
063:
064: if( fread(&a, sizeof(a), 1, bf) != 1 )
065: {
066: printf("\nUnable to read %ld bytes from the file\n", sizeof(a));
067: fclose(bf);
068: return;
069: }
070: fclose(bf);
071:
072: if(a.signature != 0x464947)
073: {
074: printf("\n%s is an invalid GIF file\n", s);
075: return;
076: }
077:
078:
079: printf("\nHeader of the GIF image file '%s':\n", s);
080: printf(" %d x %d width x height\n", a.width, a.height);
081: if(a.global_color)
082: {
083: printf(" The file has a global color table\n");
084: printf(" Background color index is %d\n", a.bg_color_index);
085: printf(" Color table size is %d\n", a.color_table_size);
086: }
087: else
088: {
089: printf( " The file has not a global color table\n");
090: }
091:
092: printf(" Color Resolution is %d\n", a.color_res);
093: if(a.sort_flag)
094: printf(" color values are sorted according to their frequency\n");
095: else
096: printf(" color values are unsorted\n");
097:
098: if(a.pixel_aspect_ratio)
099: printf(" the aspect ratio of each pixel is %d\n", a.pixel_aspect_ratio);
100:
101:
102: return;
103: }
104:
105:
Se avete commenti o osservazioni su questa pagina
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it