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