merge with original d2 file
[btb/d2x.git] / iff / archive / iff8bpp.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <malloc.h>
4
5 #include "iff.h"
6
7 extern int parse_iff(FILE *ifile,struct bitmap_header *bitmap_header);
8
9 int x,y,pl,bc;
10 int bytes_per_row,color;
11 int mask,first_bit_value;
12 FILE *ifile;
13 struct bitmap_header iff_bitmap_header;
14
15 // Parse ilbm style data at my_bh->raw_data.
16 BITMAP8 * IFF_To_8BPP(char * ifilename)
17 {
18         struct bitmap_header * my_bh;
19         int Process_width,Process_height;
20         unsigned char  *p;
21         struct pal_entry *palptr;
22         int newptr = 0;
23     int i;
24         BITMAP8 * new;
25
26         my_bh = &iff_bitmap_header;
27         palptr=my_bh->palette;
28         p=my_bh->raw_data;
29
30         Process_width = 32767;  // say to process full width of bitmap
31         Process_height = 32767; // say to process full height of bitmap
32
33         if ((ifile = fopen(ifilename,"rb")) == NULL) {
34                 printf("Unable to open bitmap file %s.\n", ifilename);
35                 exit(1);
36         }
37
38         parse_iff(ifile,&iff_bitmap_header);
39         if (Process_width > iff_bitmap_header.w)
40                 Process_width = iff_bitmap_header.w;
41
42         if (Process_height > iff_bitmap_header.h)
43                 Process_height = iff_bitmap_header.h;
44
45     printf( "%d, %d\n", Process_width, Process_height );
46
47         new = (BITMAP8 *)malloc( sizeof(BITMAP8)+ (Process_width * Process_height ) );
48     if (new==NULL) exit(1);
49
50         new->Width = Process_width;
51         new->Height = Process_height;
52     for (i=0;i<256;i++) {
53         new->Palette[3*i] = my_bh->palette[i].r;
54         new->Palette[3*i+1] = my_bh->palette[i].g;
55         new->Palette[3*i+2] = my_bh->palette[i].b;
56     }
57     printf("Process_width = %i, Process_height = %i\n",Process_width,Process_height);
58         first_bit_value = 1 << (my_bh->nplanes-1);
59         bytes_per_row = 2*((my_bh->w+15)/16);
60         for (y=0; y<Process_height; y++) {
61                 bc = Process_width;
62                 p = &my_bh->raw_data[y*bytes_per_row*my_bh->nplanes];
63
64                 switch (my_bh->type) {
65                         case PBM_TYPE:
66                                 for (x=0; x<my_bh->w; x++) {
67                                         new->Data[newptr++] = my_bh->raw_data[y*my_bh->w+x];
68                                 }
69                                 break;
70                         case ILBM_TYPE:
71                                 for (x=0; x<bytes_per_row; x++) {
72                                         for (mask=128; mask; mask /=2) {
73                                                 color = 0;
74                                                 for (pl=0; pl<my_bh->nplanes; pl++) {
75                                                         color /= 2;
76                                                         if ( p[pl*bytes_per_row+x] & mask)
77                                                                 color += first_bit_value;
78                                                 }
79                                                 new->Data[newptr++] = color;
80                                                 bc--;
81                                                 if (!bc)
82                                                         goto line_done;
83                                         }
84                                 }
85 line_done: ;
86                                 break;
87                 }
88         }
89         free( my_bh->raw_data );
90     fclose(ifile);
91     return new;
92 }
93
94 ΓΏ