FreeCalypso > hg > fc-magnetite
comparison src/aci2/bmi/Icons/BMP/bmpToHex.c @ 3:93999a60b835
src/aci2, src/condat2: import of g23m/condat source pieces from TCS211
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 26 Sep 2016 00:29:36 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:c41a534f33c6 | 3:93999a60b835 |
---|---|
1 #include <stdlib.h> | |
2 #include <stdio.h> | |
3 | |
4 typedef unsigned char byte; | |
5 typedef unsigned char u8; | |
6 typedef signed char s8; | |
7 typedef signed short s16; | |
8 typedef unsigned short u16; | |
9 typedef signed long s32; | |
10 typedef unsigned long u32; | |
11 | |
12 typedef FILE* FS_file_handle; | |
13 | |
14 byte FS_fgetbyte(FS_file_handle file) | |
15 { | |
16 return((byte)fgetc(file)); | |
17 } | |
18 | |
19 u16 FS_fget16(FS_file_handle file) | |
20 { | |
21 u16 c; | |
22 byte l=FS_fgetbyte(file); | |
23 byte h=FS_fgetbyte(file); | |
24 c=l|(h<<8); | |
25 return(c); | |
26 } | |
27 | |
28 u32 FS_fget32(FS_file_handle file) | |
29 { | |
30 u32 c; | |
31 u16 l=FS_fget16(file); | |
32 u16 h=FS_fget16(file); | |
33 c=l|(h<<16); | |
34 return(c); | |
35 } | |
36 | |
37 s32 FS_fseek(FS_file_handle file, s32 offset, s32 origin) | |
38 { | |
39 return(fseek(file,offset,origin)); | |
40 } | |
41 | |
42 typedef struct _bitmap_file_header | |
43 { | |
44 u16 file_type; | |
45 u32 file_size; | |
46 u16 reserved1; | |
47 u16 reserved2; | |
48 u32 bitmap_offset; | |
49 | |
50 } bitmap_file_header; | |
51 | |
52 typedef struct _bitmap_info_header | |
53 { | |
54 u32 header_size; | |
55 u32 width; | |
56 u32 height; | |
57 u16 number_of_planes; | |
58 u16 bits_per_pixel; | |
59 u32 compression; | |
60 u32 bitmap_size; | |
61 u32 device_width; | |
62 u32 device_height; | |
63 u32 number_of_colors; | |
64 u32 number_of_important_colors; | |
65 | |
66 } bitmap_info_header; | |
67 | |
68 byte BMP_palette[256*3]; | |
69 | |
70 void BMP_write_line(s32 x,s32 y,s32 width,FS_file_handle file,byte *data,s32 pad_bytes,s32 in_color_depth,s32 out_color_depth) | |
71 { s32 i,j,xx,offset=0; | |
72 s32 color_index; | |
73 s32 w,dd,ii; | |
74 byte color; | |
75 u16 *data16; | |
76 u16 color16; | |
77 byte R,G,B; | |
78 | |
79 switch(out_color_depth) | |
80 { case 8: switch(in_color_depth) | |
81 { case 8: for(i=0;i<width;i++) | |
82 { | |
83 #if(0) | |
84 data[offset]=FS_fgetbyte(file); | |
85 offset++; | |
86 #else | |
87 color_index=FS_fgetbyte(file); | |
88 R=BMP_palette[color_index*3+0]; | |
89 G=BMP_palette[color_index*3+1]; | |
90 B=BMP_palette[color_index*3+2]; | |
91 color=R&0xe0; | |
92 color|=(G>>3)&0x1c; | |
93 color|=(B>>6)&0x03; | |
94 data[offset]=color; | |
95 offset++; | |
96 #endif | |
97 } | |
98 break; | |
99 } | |
100 break; | |
101 #if(0) | |
102 case 16: data16=(u16*)data; | |
103 switch(in_color_depth) | |
104 { case 1: w=(width>>3); | |
105 if(width&7) w++; | |
106 for(i=0;i<w;i++) | |
107 { dd=FS_fgetbyte(file); | |
108 for(j=0;j<8;j++) | |
109 { ii=(i<<3)+j; | |
110 if(ii>=width) break; | |
111 xx=x+ii; | |
112 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
113 { if(dd & (1<<(7-j))) color16=0xffff; | |
114 else color16=0; | |
115 data16[offset]=color16; | |
116 } | |
117 offset++; | |
118 } | |
119 } | |
120 break; | |
121 | |
122 case 4: w=(width>>1); | |
123 if(width&1) w++; | |
124 for(i=0;i<w;i++) | |
125 { dd=FS_fgetbyte(file); | |
126 for(j=0;j<2;j++) | |
127 { ii=(i<<1)+j; | |
128 if(ii>=width) break; | |
129 xx=x+ii; | |
130 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
131 { color_index=((dd>>((1-j)<<2))&0xf)*3; | |
132 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
133 PC_RGB_TO_D16(color16,R,G,B); | |
134 data16[offset]=color16; | |
135 } | |
136 offset++; | |
137 } | |
138 } | |
139 break; | |
140 | |
141 case 8: for(i=0;i<width;i++) | |
142 { xx=x+i; | |
143 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
144 { color_index=FS_fgetbyte(file)*3; | |
145 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
146 PC_RGB_TO_D16(color16,R,G,B); | |
147 data16[offset]=color16; | |
148 } | |
149 else | |
150 { FS_fgetbyte(file); | |
151 } | |
152 offset++; | |
153 } | |
154 break; | |
155 | |
156 case 24: for(i=0;i<width;i++) | |
157 { xx=x+i; | |
158 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
159 { B=FS_fgetbyte(file); | |
160 G=FS_fgetbyte(file); | |
161 R=FS_fgetbyte(file); | |
162 PC_RGB_TO_D16(color16,R,G,B); | |
163 data16[offset]=color16; | |
164 } | |
165 else | |
166 { FS_fgetbyte(file); | |
167 FS_fgetbyte(file); | |
168 FS_fgetbyte(file); | |
169 } | |
170 offset++; | |
171 } | |
172 break; | |
173 } | |
174 break; | |
175 | |
176 case 24: switch(in_color_depth) | |
177 { case 1: w=(width>>3); | |
178 if(width&7) w++; | |
179 for(i=0;i<w;i++) | |
180 { dd=FS_fgetbyte(file); | |
181 for(j=0;j<8;j++) | |
182 { ii=(i<<3)+j; | |
183 if(ii>=width) break; | |
184 xx=x+ii; | |
185 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
186 { if(dd & (1<<(7-j))) color=255; | |
187 else color=0; | |
188 data[offset+2]=color; | |
189 data[offset+1]=color; | |
190 data[offset+0]=color; | |
191 } | |
192 offset+=3; | |
193 } | |
194 } | |
195 break; | |
196 | |
197 case 4: w=(width>>1); | |
198 if(width&1) w++; | |
199 for(i=0;i<w;i++) | |
200 { dd=FS_fgetbyte(file); | |
201 for(j=0;j<2;j++) | |
202 { ii=(i<<1)+j; | |
203 if(ii>=width) break; | |
204 xx=x+ii; | |
205 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
206 { color_index=((dd>>((1-j)<<2))&0xf)*3; | |
207 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
208 PC_WRITE_RGB(data,offset,R,G,B); | |
209 } | |
210 offset+=3; | |
211 } | |
212 } | |
213 break; | |
214 | |
215 case 8: for(i=0;i<width;i++) | |
216 { xx=x+i; | |
217 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
218 { color_index=FS_fgetbyte(file)*3; | |
219 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
220 PC_WRITE_RGB(data,offset,R,G,B); | |
221 } | |
222 else | |
223 { | |
224 FS_fgetbyte(file); | |
225 } | |
226 offset+=3; | |
227 } | |
228 break; | |
229 | |
230 case 24: for(i=0;i<width;i++) | |
231 { xx=x+i; | |
232 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
233 { B=FS_fgetbyte(file); | |
234 G=FS_fgetbyte(file); | |
235 R=FS_fgetbyte(file); | |
236 PC_WRITE_RGB(data,offset,R,G,B); | |
237 } | |
238 else | |
239 { FS_fgetbyte(file); | |
240 FS_fgetbyte(file); | |
241 FS_fgetbyte(file); | |
242 } | |
243 offset+=3; | |
244 } | |
245 break; | |
246 } | |
247 break; | |
248 | |
249 case 32: switch(in_color_depth) | |
250 { case 1: w=(width>>3); | |
251 if(width&7) w++; | |
252 for(i=0;i<w;i++) | |
253 { dd=FS_fgetbyte(file); | |
254 for(j=0;j<8;j++) | |
255 { ii=(i<<3)+j; | |
256 if(ii>=width) break; | |
257 xx=x+ii; | |
258 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
259 { if(dd & (1<<(7-j))) color=255; | |
260 else color=0; | |
261 data[offset+2]=color; | |
262 data[offset+1]=color; | |
263 data[offset+0]=color; | |
264 } | |
265 offset+=4; | |
266 } | |
267 } | |
268 break; | |
269 | |
270 case 4: w=(width>>1); | |
271 if(width&1) w++; | |
272 for(i=0;i<w;i++) | |
273 { dd=FS_fgetbyte(file); | |
274 for(j=0;j<2;j++) | |
275 { ii=(i<<1)+j; | |
276 if(ii>=width) break; | |
277 xx=x+ii; | |
278 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
279 { color_index=((dd>>((1-j)<<2))&0xf)*3; | |
280 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
281 PC_WRITE_RGB(data,offset,R,G,B); | |
282 } | |
283 offset+=4; | |
284 } | |
285 } | |
286 break; | |
287 | |
288 case 8: for(i=0;i<width;i++) | |
289 { xx=x+i; | |
290 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
291 { color_index=FS_fgetbyte(file)*3; | |
292 PC_READ_PALETTE(BMP_palette,color_index,R,G,B); | |
293 PC_WRITE_RGB(data,offset,R,G,B); | |
294 } | |
295 else | |
296 { FS_fgetbyte(file); | |
297 } | |
298 offset+=4; | |
299 } | |
300 break; | |
301 | |
302 case 24: for(i=0;i<width;i++) | |
303 { xx=x+i; | |
304 if((xx>=GFX_clip_x1) && (xx<=GFX_clip_x2)) | |
305 { B=FS_fgetbyte(file); | |
306 G=FS_fgetbyte(file); | |
307 R=FS_fgetbyte(file); | |
308 PC_WRITE_RGB(data,offset,R,G,B); | |
309 } | |
310 else | |
311 { FS_fgetbyte(file); | |
312 FS_fgetbyte(file); | |
313 FS_fgetbyte(file); | |
314 } | |
315 offset+=4; | |
316 } | |
317 break; | |
318 } | |
319 break; | |
320 #endif | |
321 } | |
322 for(i=0;i<pad_bytes;i++) | |
323 { FS_fgetbyte(file); | |
324 } | |
325 } | |
326 | |
327 void BMP_load_file_header(bitmap_file_header *h,FS_file_handle file) | |
328 { | |
329 h->file_type=FS_fget16(file); | |
330 h->file_size=FS_fget32(file); | |
331 h->reserved1=FS_fget16(file); | |
332 h->reserved2=FS_fget16(file); | |
333 h->bitmap_offset=FS_fget32(file); | |
334 } | |
335 | |
336 void BMP_load_info_header(bitmap_info_header *h,FS_file_handle file) | |
337 { | |
338 h->header_size=FS_fget32(file); | |
339 h->width=FS_fget32(file); | |
340 h->height=FS_fget32(file); | |
341 h->number_of_planes=FS_fget16(file); | |
342 h->bits_per_pixel=FS_fget16(file); | |
343 h->compression=FS_fget32(file); | |
344 h->bitmap_size=FS_fget32(file); | |
345 h->device_width=FS_fget32(file); | |
346 h->device_height=FS_fget32(file); | |
347 h->number_of_colors=FS_fget32(file); | |
348 h->number_of_important_colors=FS_fget32(file); | |
349 } | |
350 | |
351 byte *b_data; | |
352 byte *b_palette; | |
353 s32 b_color_depth=8; | |
354 s32 b_row_bytes; | |
355 s32 b_width; | |
356 s32 b_height; | |
357 s32 b_size; | |
358 | |
359 byte BMP_load(s32 x,s32 y,FS_file_handle file) | |
360 { bitmap_file_header file_header; | |
361 bitmap_info_header info_header; | |
362 s32 ncolors,i,j,k,width,height,offset; | |
363 s32 bitmap_size,row_bytes,used_row_bytes,total_bits; | |
364 s32 dest_row_bytes,pad_bytes; | |
365 | |
366 FS_fseek(file,0,SEEK_SET); | |
367 BMP_load_file_header(&file_header,file); | |
368 | |
369 if((file_header.file_type&0xff)!='B') return(0); | |
370 if((file_header.file_type>>8)!='M') return(0); | |
371 | |
372 BMP_load_info_header(&info_header,file); | |
373 width=info_header.width; | |
374 height=info_header.height; | |
375 ncolors=1<<info_header.bits_per_pixel; | |
376 | |
377 if(info_header.bits_per_pixel==24) | |
378 { used_row_bytes=width*3; | |
379 } | |
380 else | |
381 { total_bits=width*info_header.bits_per_pixel; | |
382 used_row_bytes=total_bits>>3; /*total_bits/8 */ | |
383 if((total_bits%8)>0) used_row_bytes+=1; /* total_bits%8 */ | |
384 } | |
385 | |
386 if((used_row_bytes%4)>0) row_bytes=used_row_bytes+(4-(used_row_bytes%4)); | |
387 else row_bytes=used_row_bytes; | |
388 if(b_color_depth<=8) | |
389 { dest_row_bytes=width/(8/b_color_depth); | |
390 if((width%(8/b_color_depth))>0) dest_row_bytes++; | |
391 } | |
392 else | |
393 { dest_row_bytes=width*(b_color_depth/8); | |
394 } | |
395 bitmap_size=dest_row_bytes*height; | |
396 b_width=width; | |
397 b_height=height; | |
398 b_size=bitmap_size; | |
399 b_data=(byte*)malloc(b_size); | |
400 b_palette=(byte*)malloc(256*3); | |
401 b_row_bytes=dest_row_bytes; | |
402 memset(b_data,0,b_size); | |
403 if(b_color_depth<=8) | |
404 { if(b_palette==NULL) return(0); | |
405 } | |
406 if(b_data==NULL) | |
407 { return(0); | |
408 } | |
409 if(info_header.bits_per_pixel<=8) | |
410 { k=0; | |
411 if(b_color_depth<=8) | |
412 { | |
413 for(i=0;i<ncolors;i++) | |
414 { BMP_palette[k+2]=b_palette[k+2]=FS_fgetbyte(file); | |
415 BMP_palette[k+1]=b_palette[k+1]=FS_fgetbyte(file); | |
416 BMP_palette[k+0]=b_palette[k+0]=FS_fgetbyte(file); | |
417 k+=3; | |
418 FS_fgetbyte(file); | |
419 } | |
420 } | |
421 else | |
422 { for(i=0;i<ncolors;i++) | |
423 { BMP_palette[k+2]=FS_fgetbyte(file); | |
424 BMP_palette[k+1]=FS_fgetbyte(file); | |
425 BMP_palette[k+0]=FS_fgetbyte(file); | |
426 k+=3; | |
427 FS_fgetbyte(file); | |
428 } | |
429 } | |
430 } | |
431 FS_fseek(file,file_header.bitmap_offset,SEEK_SET); | |
432 offset=(y+height-1)*b_row_bytes+x; | |
433 pad_bytes=row_bytes-used_row_bytes; | |
434 | |
435 for(j=height-1;j>=0;j--) | |
436 { | |
437 BMP_write_line(x,(y+j),width,file,&b_data[offset],pad_bytes,info_header.bits_per_pixel,b_color_depth); | |
438 | |
439 offset-=b_row_bytes; | |
440 } | |
441 | |
442 return(1); | |
443 } | |
444 | |
445 void write_buffer(FILE *file_handle) | |
446 { | |
447 #if(1) | |
448 s32 i; | |
449 fprintf(file_handle,"\n{"); | |
450 for(i=0;i<b_size;i++) | |
451 { | |
452 if((i%32)==0) fprintf(file_handle,"\n"); | |
453 fprintf(file_handle,"0x%02X,",b_data[i]); | |
454 | |
455 } | |
456 fprintf(file_handle,"\n};"); | |
457 #else | |
458 s32 i,j,offset; | |
459 char outchars[]={'.','*','#','.','*','#','+','-'}; | |
460 | |
461 for(j=0;j<b_height;j++) | |
462 { | |
463 fprintf(file_handle,"\n"); | |
464 for(i=0;i<b_width;i++) | |
465 { | |
466 offset=j*b_width+i; | |
467 fprintf(file_handle,"%c",outchars[b_data[offset]%8]); | |
468 } | |
469 } | |
470 #endif | |
471 } | |
472 | |
473 int main(int argc,char *argv[]) | |
474 { | |
475 FILE *input_file_handle; | |
476 FILE *output_file_handle; | |
477 | |
478 if(argc<3) | |
479 { | |
480 printf("\nUsage: converter <input-file> <output-file>"); | |
481 exit(0); | |
482 } | |
483 input_file_handle=fopen(argv[1],"rb"); | |
484 if(input_file_handle==NULL) | |
485 { | |
486 printf("\nInput file %s not found",argv[1]); | |
487 exit(0); | |
488 } | |
489 BMP_load(0,0,input_file_handle); | |
490 fclose(input_file_handle); | |
491 output_file_handle=fopen(argv[2],"wb"); | |
492 if(output_file_handle==NULL) | |
493 { | |
494 printf("\nUnable to create output file %s",argv[2]); | |
495 exit(0); | |
496 } | |
497 | |
498 write_buffer(output_file_handle); | |
499 fclose(output_file_handle); | |
500 free(b_data); | |
501 free(b_palette); | |
502 printf("\nWrote bitmap width=%d, height=%d, total=%d bytes",b_width,b_height,b_size); | |
503 } |