1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "general.h"
5 #include "fileoutput.h"
6 
7 /*
8 
9 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
10 This falls under the Kohnian license.  Please read
11 it at http://ringtonetools.mikekohn.net/
12 
13 This program is NOT opensourced.  You may not use any part
14 of this program for your own software.
15 
16 */
17 
18 struct _bitmap_file
19 {
20   unsigned char bfType[2];
21   unsigned int bfSize;
22   unsigned short int reserved1;
23   unsigned short int reserved2;
24   unsigned int bfOffs;
25 };
26 
27 struct _bitmap_info
28 {
29   unsigned int biSize;
30   unsigned int biWidth;
31   unsigned int biHeight;
32   unsigned short int biPlanes;
33   unsigned short int biBitCount;
34   unsigned int biCompression;
35   unsigned int biSizeImage;
36   unsigned int biXPelsPerMetre;
37   unsigned int biYPelsPerMetre;
38   unsigned int biClrUsed;
39   unsigned int biClrImportant;
40 };
41 
42 int colors[256];
43 
read_bitmap_file(FILE * in,struct _bitmap_file * bitmap_file)44 void read_bitmap_file(FILE *in, struct _bitmap_file *bitmap_file)
45 {
46   bitmap_file->bfType[0]=getc(in);
47   bitmap_file->bfType[1]=getc(in);
48   bitmap_file->bfSize=read_long(in);
49   bitmap_file->reserved1=read_word(in);
50   bitmap_file->reserved2=read_word(in);
51   bitmap_file->bfOffs=read_long(in);
52 }
53 
read_bitmap_info(FILE * in,struct _bitmap_info * bitmap_info)54 void read_bitmap_info(FILE *in, struct _bitmap_info *bitmap_info)
55 {
56   bitmap_info->biSize=read_long(in);
57   bitmap_info->biWidth=read_long(in);
58   bitmap_info->biHeight=read_long(in);
59   bitmap_info->biPlanes=read_word(in);
60   bitmap_info->biBitCount=read_word(in);
61   bitmap_info->biCompression=read_long(in);
62   bitmap_info->biSizeImage=read_long(in);
63   bitmap_info->biXPelsPerMetre=read_long(in);
64   bitmap_info->biYPelsPerMetre=read_long(in);
65   bitmap_info->biClrUsed=read_long(in);
66   bitmap_info->biClrImportant=read_long(in);
67 }
68 
raw_uncompressed(FILE * in,struct note_t * note,int bits)69 void raw_uncompressed(FILE *in, struct note_t *note, int bits)
70 {
71 int x,y;
72 int c=0,t;
73 int byte_count;
74 int use_trans;
75 
76   use_trans=note->trans;
77 
78   for (y=note->height-1; y>=0; y--)
79   {
80     byte_count=0;
81 
82     for (x=0; x<note->width; x++)
83     {
84       if (bits==8 || bits==24 || bits==32)
85       {
86         if (bits==8)
87         {
88           c=getc(in);
89           c=colors[c];
90 /* printf("0x%06x\n",c); */
91           byte_count++;
92         }
93           else
94         if (bits==24)
95         {
96           c=getc(in)+(getc(in)<<8)+(getc(in)<<16);
97           byte_count=byte_count+3;
98         }
99           else
100         if (bits==32)
101         {
102           c=getc(in)+(getc(in)<<8)+(getc(in)<<16);
103           t=getc(in);
104           if (t==255 && note->trans==-2) note->trans=c;
105 /*
106           if (use_trans==-2)
107           {
108             rgb[0]=getc(in);
109             rgb[1]=getc(in);
110             rgb[2]=getc(in);
111 
112             t=getc(in);
113             if (t==255 && note->trans==-2) note->trans=0x000000;
114             adj=(double)(255-t)/(double)255;
115             rgb[0]=(unsigned char)((double)rgb[0]*adj);
116             rgb[1]=(unsigned char)((double)rgb[1]*adj);
117             rgb[2]=(unsigned char)((double)rgb[2]*adj);
118             c=rgb[0]+(rgb[1]<<8)+(rgb[2]<<16);
119             if (t==255 && note->trans==-2) note->trans=c;
120           }
121             else
122           {
123             c=getc(in)+(getc(in)<<8)+(getc(in)<<16);
124             t=getc(in);
125           }
126 */
127 
128           byte_count=byte_count+4;
129         }
130 
131         note->picture[x+(y*note->width)]=c;
132       }
133         else
134       if (bits==4)
135       {
136         c=getc(in);
137         byte_count++;
138 
139         note->picture[x+(y*note->width)]=colors[((c>>4)&15)];
140         x++;
141 
142         if (x<note->width)
143         {
144           note->picture[x+(y*note->width)]=colors[(c&15)];
145         }
146       }
147         else
148       if (bits==1)
149       {
150         c=getc(in);
151         byte_count++;
152 
153         for (t=7; t>=0; t--)
154         {
155           if (x<note->width)
156           {
157             if (((c>>t)&1)==0)
158             { note->picture[x+(y*note->width)]=colors[0]; }
159               else
160             { note->picture[x+(y*note->width)]=colors[1]; }
161           }
162           x++;
163         }
164         x=x-1;
165       }
166     }
167 
168     c=(byte_count%4);
169     if (c!=0)
170     {
171       for (t=c; t<4; t++)
172       { getc(in); }
173     }
174   }
175 }
176 
raw_compressed(FILE * in,struct note_t * note,int bits)177 void raw_compressed(FILE *in, struct note_t *note, int bits)
178 {
179 int x,y;
180 int c,t,r;
181 
182   y=note->height-1;
183   x=0;
184 
185   while (1)
186   {
187 #ifdef DEBUG
188 if (x==0) printf("reading line %d\n",y);
189 #endif
190 
191     c=getc(in);
192 
193     if (c==EOF) return;
194 
195     if (c!=0)
196     {
197       r=getc(in);
198 #ifdef DEBUG
199 printf("repeat same %d %d times\n",r,c);
200 #endif
201 
202       for (t=0; t<c; t++)
203       {
204         if (bits==4)
205         {
206           if ((t%2)==0)
207           { note->picture[x+(y*note->width)]=colors[(r>>4)]; }
208             else
209           { note->picture[x+(y*note->width)]=colors[(r&15)]; }
210         }
211           else
212         if (bits==8)
213         { note->picture[x+(y*note->width)]=colors[r]; }
214 
215         x++;
216       }
217     }
218       else
219     {
220       r=getc(in);
221 #ifdef DEBUG
222 printf("repeat not the same %d times\n",r);
223 #endif
224 
225       if (r==0)
226       {
227         x=0;
228         y--;
229         continue;
230       }
231         else
232       if (r==1)
233       { break; }
234         else
235       if (r==2)
236       {
237         x=x+getc(in);
238         y=y-getc(in);
239         return;
240       }
241 
242       for (t=0; t<r; t++)
243       {
244         c=getc(in);
245         if (bits==8)
246         {
247           note->picture[x+(y*note->width)]=colors[c];
248         }
249           else
250         if (bits==4)
251         {
252           note->picture[x+(y*note->width)]=colors[c>>4];
253           t++;
254           if (t<r)
255           {
256             x++;
257             note->picture[x+(y*note->width)]=colors[(c&15)];
258           }
259         }
260 
261         x++;
262       }
263 
264       if (bits==8)
265       { c=r%2; }
266         else
267       if (bits==4)
268       {
269         t=(r/2)+(r%2);
270         c=t%2;
271       }
272 
273       if (c!=0)
274       { getc(in); }
275     }
276   }
277 }
278 
parse_bmp(FILE * in,FILE * out,int out_type,struct note_t * note)279 int parse_bmp(FILE *in, FILE *out, int out_type, struct note_t *note)
280 {
281 struct _bitmap_file bitmap_file;
282 struct _bitmap_info bitmap_info;
283 int t;
284 /* int t,c; */
285 
286   read_bitmap_file(in, &bitmap_file);
287 
288   if (bitmap_file.bfType[0]!='B' || bitmap_file.bfType[1]!='M')
289   {
290     printf("Not a bitmap.\n");
291     return -1;
292   }
293 
294   read_bitmap_info(in, &bitmap_info);
295 
296 #ifdef DEBUG
297   printf("Bitmap File Header\n");
298   printf("----------------------------------------------\n");
299   printf("         bfType: %c%c\n",bitmap_file.bfType[0],bitmap_file.bfType[1]);
300   printf("         bfSize: %d\n",bitmap_file.bfSize);
301   printf("      reserved1: %d\n",bitmap_file.reserved1);
302   printf("      reserved2: %d\n",bitmap_file.reserved2);
303   printf("         bfOffs: %d\n",bitmap_file.bfOffs);
304   printf("----------------------------------------------\n");
305   printf("Bitmap Info Header\n");
306   printf("----------------------------------------------\n");
307   printf("         biSize: %d\n",bitmap_info.biSize);
308   printf("        biWidth: %d\n",bitmap_info.biWidth);
309   printf("       biHeight: %d\n",bitmap_info.biHeight);
310   printf("       biPlanes: %d\n",bitmap_info.biPlanes);
311   printf("     biBitCount: %d\n",bitmap_info.biBitCount);
312   printf("  biCompression: %d\n",bitmap_info.biCompression);
313   printf("    biSizeImage: %d\n",bitmap_info.biSizeImage);
314   printf("biXPelsPerMetre: %d\n",bitmap_info.biXPelsPerMetre);
315   printf("biYPelsPerMetre: %d\n",bitmap_info.biYPelsPerMetre);
316   printf("      biClrUsed: %d\n",bitmap_info.biClrUsed);
317   printf(" biClrImportant: %d\n",bitmap_info.biClrImportant);
318   printf("----------------------------------------------\n");
319 #endif
320 
321   colors[0]=0;
322   colors[1]=0xffffff;
323   colors[255]=0xffffff;
324 
325   if (bitmap_info.biClrImportant==0 && bitmap_info.biBitCount==8)
326   { bitmap_info.biClrImportant=256; }
327 
328   for (t=0; t<bitmap_info.biClrImportant; t++)
329   {
330     colors[t]=read_long(in);
331 /*
332     c=(getc(in)+(getc(in)<<8)+(getc(in)<<16));
333     getc(in);
334     colors[t]=c;
335 */
336   }
337 
338   note->width=bitmap_info.biWidth;
339   note->height=bitmap_info.biHeight;
340 
341   if (logo_header_route(out,note,out_type)==-1) return 0;
342 
343   fseek(in,bitmap_file.bfOffs,0);
344 
345   if (bitmap_info.biCompression==0)
346   {
347     raw_uncompressed(in,note,bitmap_info.biBitCount);
348   }
349     else
350   if (bitmap_info.biCompression==1)
351   {
352     raw_compressed(in,note,8);
353   }
354     else
355   if (bitmap_info.biCompression==2)
356   {
357     raw_compressed(in,note,4);
358   }
359     else
360   if (bitmap_info.biCompression==3)
361   {
362     raw_uncompressed(in,note,bitmap_info.biBitCount);
363   }
364     else
365   {
366     printf("This type of compression is not supported at this time.\n");
367     return 0;
368   }
369 
370   logo_footer_route(out,note,out_type);
371 
372   return 0;
373 }
374 
parse_wbmp(FILE * in,FILE * out,int out_type,struct note_t * note)375 int parse_wbmp(FILE *in, FILE *out, int out_type, struct note_t *note)
376 {
377 int x,y,ch;
378 int b,ptr;
379 
380   if (getc(in)!=0)
381   {
382     printf("WBMP: Unknown compression type\n");
383     return 0;
384   }
385 
386   getc(in);
387 
388   note->width=getc(in);
389   note->height=getc(in);
390 
391   logo_header_route(out,note,out_type);
392 
393   ptr=0;
394   for (y=0; y<note->height; y++)
395   {
396     x=0;
397     while(x<note->width)
398     {
399       ch=getc(in);
400       for (b=7; b>=0; b--)
401       {
402         if (x<note->width)
403         {
404           if ((ch&(1<<b))==0)
405           { note->picture[ptr++]=0; }
406             else
407           { note->picture[ptr++]=0xffffff; }
408         }
409         x++;
410       }
411     }
412   }
413 
414   logo_footer_route(out,note,out_type);
415 
416   return 0;
417 }
418 
parse_icon(FILE * in,FILE * out,int out_type,struct note_t * note)419 int parse_icon(FILE *in, FILE *out, int out_type, struct note_t *note)
420 {
421 struct _bitmap_info bitmap_info;
422 int x,c,t,num_pics,num_colors;
423 
424   t=read_word(in);
425   t=read_word(in);
426   if (t!=1)
427   {
428     printf("Not a Windows Icon file.\n");
429     return 0;
430   }
431 
432   num_pics=read_word(in);
433 #ifdef DEBUG
434 printf("num_pics %d\n",num_pics);
435 #endif
436 
437   /* Icon Directory */
438 
439   note->width=getc(in);
440   note->height=getc(in);
441 
442   /* printf("%dx%d\n",note->width,note->height); */
443 
444   num_colors=getc(in);
445   if (num_colors==0) num_colors=256;
446 #ifdef DEBUG
447 printf("color count %d\n",num_colors);
448 #endif
449   getc(in);
450   t=read_word(in);
451   t=read_word(in);
452 
453   t=read_long(in);
454 #ifdef DEBUG
455 printf("size of pixel array %d\n",t);
456 #endif
457   t=read_long(in);
458   x=read_long(in);
459 #ifdef DEBUG
460 printf("Offset %d  %d\n",t,(int)ftell(in));
461 #endif
462   fseek(in,t,SEEK_SET);
463 #ifdef DEBUG
464 printf("Data Offset %d\n",x);
465 #endif
466 
467   read_bitmap_info(in, &bitmap_info);
468 #ifdef DEBUG
469   printf("Bitmap Info Header\n");
470   printf("----------------------------------------------\n");
471   printf("         biSize: %d\n",bitmap_info.biSize);
472   printf("        biWidth: %d\n",bitmap_info.biWidth);
473   printf("       biHeight: %d\n",bitmap_info.biHeight);
474   printf("       biPlanes: %d\n",bitmap_info.biPlanes);
475   printf("     biBitCount: %d\n",bitmap_info.biBitCount);
476   printf("  biCompression: %d\n",bitmap_info.biCompression);
477   printf("    biSizeImage: %d\n",bitmap_info.biSizeImage);
478   printf("biXPelsPerMetre: %d\n",bitmap_info.biXPelsPerMetre);
479   printf("biYPelsPerMetre: %d\n",bitmap_info.biYPelsPerMetre);
480   printf("      biClrUsed: %d\n",bitmap_info.biClrUsed);
481   printf(" biClrImportant: %d\n",bitmap_info.biClrImportant);
482   printf("----------------------------------------------\n");
483 #endif
484 
485   /* note->width=bitmap_info.biWidth; */
486   /* note->height=bitmap_info.biHeight; */
487 
488   colors[0]=0;
489   colors[1]=0xffffff;
490   colors[255]=0xffffff;
491 
492   if (bitmap_info.biBitCount<24) num_colors=1<<bitmap_info.biBitCount;
493 
494   for (t=0; t<num_colors; t++)
495   {
496     c=(getc(in)+(getc(in)<<8)+(getc(in)<<16));
497     getc(in);
498     colors[t]=c;
499   }
500 
501   logo_header_route(out,note,out_type);
502 
503   bitmap_info.biHeight=note->height;
504 
505   raw_uncompressed(in,note,bitmap_info.biBitCount);
506 
507 /*
508   for (y=note->height-1; y>=0; y--)
509   {
510     x=0;
511     while(x<note->width)
512     {
513       c=getc(in);
514       for (t=7; t>=0; t--)
515       {
516         if (((c>>t)&1)==1) note->picture[x+(y*note->width)]=0;
517         x++;
518       }
519     }
520   }
521 */
522 
523   logo_footer_route(out,note,out_type);
524 
525   return 0;
526 }
527 
528