summaryrefslogtreecommitdiff
path: root/src/modules/loaders/loader_bmp.c
blob: d319b3d47a5b69d7bbbb84d2bb620bd2129da24a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Based off of Peter Alm's BMP loader from xmms, with additions from
 * imlib's old BMP loader
 */
/*
 * 21.3.2006 - Changes made by Petr Kobalicek
 * - Simplify and make secure RLE encoding
 * - Fix 16 and 32 bit depth (old code was incorrect and it's commented) 
 */
#include "loader_common.h"
#include <sys/stat.h>

#define DEBUG 0
#if DEBUG
#define D(fmt...) fprintf(stdout, "BMP loader: " fmt)
#else
#define D(fmt...)
#endif
#define Dx(fmt...)

/* The BITMAPFILEHEADER (size 14) */
typedef struct {
   DATA8               header[2];
   DATA8               size[4];
   DATA8               rsvd1[2];
   DATA8               rsvd2[2];
   DATA8               offs[4];
} bfh_t;

/* The BITMAPINFOHEADER */
typedef union {
   DATA32              header_size;
   struct {
      /* BITMAPCOREHEADER (size 12) */
      DATA32              header_size;
      DATA16              width;
      DATA16              height;
      DATA16              planes;
      DATA16              bpp;
   } bch;
   struct {
      /* BITMAPINFOHEADER (size 40) */
      DATA32              header_size;
      DATA32              width;
      DATA32              height;
      DATA16              planes;
      DATA16              bpp;
      DATA32              compression;
      DATA32              size;
      DATA32              res_hor;
      DATA32              res_ver;
      DATA32              colors;
      DATA32              colors_important;
      /* BITMAPV3INFOHEADER (size 56) */
      DATA32              mask_r;
      DATA32              mask_g;
      DATA32              mask_b;
      DATA32              mask_a;
   } bih;
   char                bytes[124];
} bih_t;

typedef struct {
   unsigned char       rgbBlue;
   unsigned char       rgbGreen;
   unsigned char       rgbRed;
   unsigned char       rgbReserved;
} RGBQUAD;

/* Compression methods */
#define BI_RGB                  0
#define BI_RLE8                 1
#define BI_RLE4                 2
#define BI_BITFIELDS            3
#define BI_JPEG                 4       /* Unsupported */
#define BI_PNG                  5       /* Unsupported */
#define BI_ALPHABITFIELDS       6
#define BI_CMYK                11       /* Unsupported */
#define BI_CMYKRLE8            12       /* Unsupported */
#define BI_CMYKRLE4            13       /* Unsupported */

enum {
   RLE_NEXT = 0,                /* Next line */
   RLE_END = 1,                 /* End of RLE encoding */
   RLE_MOVE = 2                 /* Move by X and Y (Offset is stored in two next bytes) */
};

static int
WriteleByte(FILE * file, unsigned char val)
{
   int                 rc;

   rc = fputc((int)val & 0xff, file);
   if (rc == EOF)
      return 0;

   return 1;
}

static int
WriteleShort(FILE * file, unsigned short val)
{
   int                 rc;

   rc = fputc((int)(val & 0xff), file);
   if (rc == EOF)
      return 0;
   rc = fputc((int)((val >> 8) & 0xff), file);
   if (rc == EOF)
      return 0;

   return 1;
}

static int
WriteleLong(FILE * file, unsigned long val)
{
   int                 rc;

   rc = fputc((int)(val & 0xff), file);
   if (rc == EOF)
      return 0;
   rc = fputc((int)((val >> 8) & 0xff), file);
   if (rc == EOF)
      return 0;
   rc = fputc((int)((val >> 16) & 0xff), file);
   if (rc == EOF)
      return 0;
   rc = fputc((int)((val >> 24) & 0xff), file);
   if (rc == EOF)
      return 0;

   return 1;
}

char
load(ImlibImage * im, ImlibProgressFunction progress,
     char progress_granularity, char immediate_load)
{
   FILE               *f;
   char                pper = 0;
   int                 pl = 0;
   unsigned int        offset;
   unsigned int        size, comp, imgsize;
   unsigned int        bitcount, ncols, skip;
   unsigned char       a, r, g, b;
   unsigned char       byte = 0, byte1, byte2;
   unsigned int        i, k;
   int                 w, h, x, y, j, l;
   DATA32             *ptr, pixel;
   unsigned char      *buffer_ptr, *buffer, *buffer_end, *buffer_end_safe;
   RGBQUAD             rgbQuads[256];
   DATA32              argbCmap[256];
   unsigned int        amask, rmask, gmask, bmask;
   int                 ashift1, rshift1, gshift1, bshift1;
   int                 ashift2, rshift2, gshift2, bshift2;
   bih_t               bih;

   f = fopen(im->real_file, "rb");
   if (!f)
      return 0;

   buffer = NULL;

   /* Load header */
   {
      struct stat         statbuf;
      bfh_t               bfh;

      if (fstat(fileno(f), &statbuf) < 0)
         goto quit_err;

      size = statbuf.st_size;
      if (size != statbuf.st_size)
         goto quit_err;

      if (fread(&bfh, sizeof(bfh), 1, f) != 1)
         goto quit_err;

      if (bfh.header[0] != 'B' || bfh.header[1] != 'M')
         goto quit_err;

#define WORD_LE_32(p8) ((p8[3] << 24) | (p8[2] << 16) | (p8[1] << 8) | p8[0])
      offset = WORD_LE_32(bfh.offs);

      if (offset >= size)
         goto quit_err;

      memset(&bih, 0, sizeof(bih));
      if (fread(&bih, 4, 1, f) != 1)
         goto quit_err;

      SWAP_LE_32_INPLACE(bih.header_size);

      D("fsize=%u, hsize=%u, header: fsize=%u offs=%u\n",
        size, bih.header_size, WORD_LE_32(bfh.size), offset);

      if (bih.header_size < 12 || bih.header_size > sizeof(bih))
         goto quit_err;

      if (fread(&bih.header_size + 1, bih.header_size - 4, 1, f) != 1)
         goto quit_err;

      w = h = 0;
      bitcount = 0;
      comp = BI_RGB;
      amask = rmask = gmask = bmask = 0;
      ashift1 = rshift1 = gshift1 = bshift1 = 0;
      ashift2 = rshift2 = gshift2 = bshift2 = 1;

      UNSET_FLAG(im->flags, F_HAS_ALPHA);

      if (bih.header_size == 12)
        {
           w = SWAP_LE_16(bih.bch.width);
           h = SWAP_LE_16(bih.bch.height);
//         planes = SWAP_LE_16(bih.bch.planes);
           bitcount = SWAP_LE_16(bih.bch.bpp);
        }
      else if (bih.header_size >= 16)
        {
           w = SWAP_LE_32(bih.bih.width);
           h = SWAP_LE_32(bih.bih.height);
//         planes = SWAP_LE_16(bih.bih.planes);
           bitcount = SWAP_LE_16(bih.bih.bpp);
           comp = SWAP_LE_32(bih.bih.compression);
           imgsize = SWAP_LE_32(bih.bih.size);  /* We don't use this */

           if (bih.header_size >= 40 &&
               (comp == BI_BITFIELDS || comp == BI_ALPHABITFIELDS))
             {
                if (bih.header_size == 40)
                  {
                     ncols = (comp == BI_ALPHABITFIELDS) ? 4 : 3;
                     if (fread(&bih.bih.mask_r, 4, ncols, f) != ncols)
                        goto quit_err;
                  }
                rmask = SWAP_LE_32(bih.bih.mask_r);
                gmask = SWAP_LE_32(bih.bih.mask_g);
                bmask = SWAP_LE_32(bih.bih.mask_b);
                amask = SWAP_LE_32(bih.bih.mask_a);
                if (amask)
                   SET_FLAG(im->flags, F_HAS_ALPHA);
             }
        }
      else
        {
           goto quit_err;
        }

      imgsize = size - offset;
      D("w=%3d h=%3d bitcount=%d comp=%d imgsize=%d\n",
        w, h, bitcount, comp, imgsize);

      /* "Bottom-up" images are loaded but not properly flipped */
      h = abs(h);

      if (!IMAGE_DIMENSIONS_OK(w, h))
         goto quit_err;

      switch (bitcount)
        {
        default:
           goto quit_err;

        case 1:
        case 4:
        case 8:
           ncols = (offset - bih.header_size - 14);
           if (bih.header_size == 12)
             {
                ncols /= 3;
                if (ncols > 256)
                   ncols = 256;
                for (i = 0; i < ncols; i++)
                   if (fread(&rgbQuads[i], 3, 1, f) != 1)
                      goto quit_err;
             }
           else
             {
                ncols /= 4;
                if (ncols > 256)
                   ncols = 256;
                if (fread(rgbQuads, 4, ncols, f) != ncols)
                   goto quit_err;
             }
           for (i = 0; i < ncols; i++)
              argbCmap[i] =
                 PIXEL_ARGB(0xff, rgbQuads[i].rgbRed, rgbQuads[i].rgbGreen,
                            rgbQuads[i].rgbBlue);
           D("ncols=%d\n", ncols);
           break;

        case 24:
           break;

        case 16:
        case 32:
           if (comp == BI_BITFIELDS || comp == BI_ALPHABITFIELDS)
             {
                unsigned int        bit, bithi;
                unsigned int        mask;

                D("mask   ARGB: %08x %08x %08x %08x\n",
                  amask, rmask, gmask, bmask);
                if (bitcount == 16)
                  {
                     amask &= 0xffffU;
                     rmask &= 0xffffU;
                     gmask &= 0xffffU;
                     bmask &= 0xffffU;
                  }
                if (rmask == 0 && gmask == 0 && bmask == 0)
                   goto quit_err;
                for (bit = 0; bit < bitcount; bit++)
                  {
                     /* Find LSB bit positions */
                     bithi = bitcount - bit - 1;
                     mask = 1 << bithi;
                     if (amask & mask)
                        ashift1 = bithi;
                     if (bmask & mask)
                        bshift1 = bithi;
                     if (gmask & mask)
                        gshift1 = bithi;
                     if (rmask & mask)
                        rshift1 = bithi;

                     /* Find MSB bit positions */
                     mask = 1 << bit;
                     if (amask & mask)
                        ashift2 = bit;
                     if (rmask & mask)
                        rshift2 = bit;
                     if (gmask & mask)
                        gshift2 = bit;
                     if (bmask & mask)
                        bshift2 = bit;
                  }

                /* Calculate shift2s as bits in mask */
                ashift2 -= ashift1 - 1;
                rshift2 -= rshift1 - 1;
                gshift2 -= gshift1 - 1;
                bshift2 -= bshift1 - 1;
             }
           else if (bitcount == 16)
             {
                rmask = 0x7C00;
                gmask = 0x03E0;
                bmask = 0x001F;
                rshift1 = 10;
                gshift1 = 5;
                bshift1 = 0;
                rshift2 = gshift2 = bshift2 = 5;
             }
           else if (bitcount == 32)
             {
                amask = 0xFF000000;
                rmask = 0x00FF0000;
                gmask = 0x0000FF00;
                bmask = 0x000000FF;
                ashift1 = 24;
                rshift1 = 16;
                gshift1 = 8;
                bshift1 = 0;
                ashift2 = rshift2 = gshift2 = bshift2 = 8;
             }

           /* Calculate shift2s as scale factor */
           ashift2 = ashift2 > 0 ? (1 << ashift2) - 1 : 1;
           rshift2 = rshift2 > 0 ? (1 << rshift2) - 1 : 1;
           gshift2 = gshift2 > 0 ? (1 << gshift2) - 1 : 1;
           bshift2 = bshift2 > 0 ? (1 << bshift2) - 1 : 1;

#define SCALE(c, x) ((((x & c##mask)>> (c##shift1 - 0)) * 255) / c##shift2)

           D("mask   ARGB: %08x %08x %08x %08x\n", amask, rmask, gmask, bmask);
           D("shift1 ARGB: %8d %8d %8d %8d\n",
             ashift1, rshift1, gshift1, bshift1);
           D("shift2 ARGB: %8d %8d %8d %8d\n",
             ashift2, rshift2, gshift2, bshift2);
           D("check  ARGB: %08x %08x %08x %08x\n",
             SCALE(a, amask), SCALE(r, rmask),
             SCALE(g, gmask), SCALE(b, bmask));
           break;
        }

      im->w = w;
      im->h = h;
   }

   if (!(im->loader || immediate_load || progress))
     {
        fclose(f);
        return 1;
     }

   /* Load data */

   fseek(f, offset, SEEK_SET);

   buffer = malloc(imgsize);
   if (!buffer)
      goto quit_err;

   if (!__imlib_AllocateData(im))
      goto quit_err;

   if (fread(buffer, imgsize, 1, f) != 1)
     {
        __imlib_FreeData(im);
        goto quit_err;
     }
   fclose(f);

   buffer_ptr = buffer;
   buffer_end = buffer + imgsize;

   ptr = im->data + ((h - 1) * w);

   switch (bitcount)
     {
     default:                  /* It should not be possible to go here */
        goto quit_err2;

     case 1:
        switch (comp)
          {
          default:
             goto quit_err2;

          case BI_RGB:
             skip = ((((w + 31) / 32) * 32) - w) / 8;
             for (y = 0; y < h; y++)
               {
                  for (x = 0; x < w && buffer_ptr < buffer_end; x++)
                    {
                       if ((x & 7) == 0)
                          byte = *buffer_ptr++;
                       k = (byte >> 7) & 1;
                       *ptr++ = argbCmap[k];
                       byte <<= 1;
                    }
                  buffer_ptr += skip;
                  ptr -= w * 2;
                  if (progress)
                    {
                       char                per;
                       int                 ll;

                       per = (char)((100 * y) / im->h);
                       if (((per - pper) >= progress_granularity) ||
                           (y == (im->h - 1)))
                         {
                            ll = y - pl;
                            if (!progress
                                (im, per, 0, im->h - y - 1, im->w,
                                 im->h - y + ll))
                              {
                                 free(buffer);
                                 return 2;
                              }
                            pper = per;
                            pl = y;
                         }
                    }
               }
             break;
          }
        break;

     case 4:
        switch (comp)
          {
          default:
             goto quit_err2;

          case BI_RLE4:
             buffer_end_safe = buffer_end - 1;

             x = 0;
             y = 0;

             for (; buffer_ptr < buffer_end_safe;)
               {
                  byte1 = buffer_ptr[0];
                  byte2 = buffer_ptr[1];
                  buffer_ptr += 2;
                  Dx("%3d %3d: %02x %02x (%d %d)\n",
                     x, y, byte1, byte2, byte2 >> 4, byte2 & 0xf);
                  if (byte1)
                    {
                       DATA32              t1, t2;

                       l = byte1;
                       /* Check for invalid length */
                       if (l + x > w)
                          goto bail_bc4;

                       t1 = argbCmap[byte2 >> 4];
                       t2 = argbCmap[byte2 & 0xF];
                       for (j = 0; j < l; j++)
                         {
                            *ptr++ = t1;
                            if (++j < l)
                               *ptr++ = t2;
                         }
                       x += l;
                    }
                  else
                    {
                       switch (byte2)
                         {
                         case RLE_NEXT:
                            x = 0;
                            if (++y >= h)
                               goto bail_bc4;
                            ptr = im->data + (h - y - 1) * w;
                            break;
                         case RLE_END:
                            goto bail_bc4;
                         case RLE_MOVE:
                            /* Need to read two bytes */
                            if (buffer_ptr >= buffer_end_safe)
                               goto bail_bc4;
                            x += buffer_ptr[0];
                            y += buffer_ptr[1];
                            buffer_ptr += 2;
                            /* Check for correct coordinates */
                            if (x >= w)
                               goto bail_bc4;
                            if (y >= h)
                               goto bail_bc4;
                            ptr = im->data + (h - y - 1) * w + x;
                            break;
                         default:
                            l = byte2;
                            /* Check for invalid length and valid buffer size */
                            if (l + x > w)
                               goto bail_bc4;
                            if (buffer_ptr + (l >> 1) + (l & 1) > buffer_end)
                               goto bail_bc4;

                            for (j = 0; j < l; j++)
                              {
                                 byte = *buffer_ptr++;
                                 Dx("%3d %3d:   %d/%d: %2d %2d\n",
                                    x, y, j, l, byte >> 4, byte & 0xf);
                                 *ptr++ = argbCmap[byte >> 4];
                                 if (++j < l)
                                    *ptr++ = argbCmap[byte & 0xF];
                              }
                            x += l;

                            /* Pad to even number of palette bytes */
                            buffer_ptr += ((l + 1) / 2) & 1;
                            break;
                         }
                    }
                bail_bc4:
                  if (progress)
                    {
                       char                per;
                       int                 ll;

                       per = (char)((100 * y) / im->h);
                       if (((per - pper) >= progress_granularity) ||
                           (y == (im->h - 1)))
                         {
                            ll = y - pl;
                            if (!progress
                                (im, per, 0, im->h - y - 1, im->w,
                                 im->h - y + ll))
                              {
                                 free(buffer);
                                 return 2;
                              }
                            pper = per;
                            pl = y;
                         }
                    }
               }
             break;

          case BI_RGB:
             skip = ((((w + 7) / 8) * 8) - w) / 2;
             for (y = 0; y < h; y++)
               {
                  for (x = 0; x < w && buffer_ptr < buffer_end; x++)
                    {
                       if ((x & 1) == 0)
                          byte = *buffer_ptr++;
                       k = (byte & 0xF0) >> 4;
                       *ptr++ = argbCmap[k];
                       byte <<= 4;
                    }
                  buffer_ptr += skip;
                  ptr -= w * 2;
                  if (progress)
                    {
                       char                per;
                       int                 ll;

                       per = (char)((100 * y) / im->h);
                       if (((per - pper) >= progress_granularity) ||
                           (y == (im->h - 1)))
                         {
                            ll = y - pl;
                            if (!progress
                                (im, per, 0, im->h - y - 1, im->w,
                                 im->h - y + ll))
                              {
                                 free(buffer);
                                 return 2;
                              }
                            pper = per;
                            pl = y;
                         }
                    }
               }
             break;
          }
        break;

     case 8:
        switch (comp)
          {
          default:
             goto quit_err2;

          case BI_RLE8:
             buffer_end_safe = buffer_end - 1;

             x = 0;
             y = 0;
             for (; buffer_ptr < buffer_end_safe;)
               {
                  byte1 = buffer_ptr[0];
                  byte2 = buffer_ptr[1];
                  buffer_ptr += 2;
                  Dx("%3d %3d: %02x %02x\n", x, y, byte1, byte2);
                  if (byte1)
                    {
                       pixel = argbCmap[byte2];
                       l = byte1;
                       if (x + l > w)
                          goto bail_bc8;
                       for (j = l; j; j--)
                          *ptr++ = pixel;
                       x += l;
                    }
                  else
                    {
                       switch (byte2)
                         {
                         case RLE_NEXT:
                            x = 0;
                            if (++y >= h)
                               goto bail_bc8;
                            ptr = im->data + ((h - y - 1) * w) + x;
                            break;
                         case RLE_END:
                            goto bail_bc8;
                         case RLE_MOVE:
                            /* Need to read two bytes */
                            if (buffer_ptr >= buffer_end_safe)
                               goto bail_bc8;
                            x += buffer_ptr[0];
                            y += buffer_ptr[1];
                            buffer_ptr += 2;
                            /* Check for correct coordinates */
                            if (x >= w)
                               goto bail_bc8;
                            if (y >= h)
                               goto bail_bc8;
                            ptr = im->data + ((h - y - 1) * w) + x;
                            break;
                         default:
                            l = byte2;
                            if (x + l > w)
                               goto bail_bc8;
                            if (buffer_ptr + l > buffer_end)
                               goto bail_bc8;
                            for (j = 0; j < l; j++)
                              {
                                 byte = *buffer_ptr++;
                                 Dx("%3d %3d:   %d/%d: %2d\n",
                                    x, y, j, l, byte);
                                 *ptr++ = argbCmap[byte];
                              }
                            x += l;
                            if (l & 1)
                               buffer_ptr++;
                            break;
                         }
                    }
               }
           bail_bc8:
             if (progress)
               {
                  char                per;
                  int                 ll;

                  per = (char)((100 * y) / im->h);
#if 0
                  /* Always call progress() at least once */
                  if (((per - pper) >= progress_granularity) ||
                      (y == (im->h - 1)))
#endif
                    {
                       ll = y - pl;
                       if (!progress
                           (im, per, 0, im->h - y - 1, im->w, im->h - y + ll))
                         {
                            free(buffer);
                            return 2;
                         }
                       pper = per;
                       pl = y;
                    }
               }
             break;

          case BI_RGB:
             skip = (((w + 3) / 4) * 4) - w;
             for (y = 0; y < h; y++)
               {
                  for (x = 0; x < w && buffer_ptr < buffer_end; x++)
                    {
                       byte = *buffer_ptr++;
                       *ptr++ = argbCmap[byte];
                    }
                  ptr -= w * 2;
                  buffer_ptr += skip;

                  if (progress)
                    {
                       char                per;
                       int                 ll;

                       per = (char)((100 * y) / im->h);
                       if (((per - pper) >= progress_granularity) ||
                           (y == (im->h - 1)))
                         {
                            ll = y - pl;
                            if (!progress
                                (im, per, 0, im->h - y - 1, im->w,
                                 im->h - y + ll))
                              {
                                 free(buffer);
                                 return 2;
                              }
                            pper = per;
                            pl = y;
                         }
                    }
               }
             break;
          }
        break;

     case 16:
        buffer_end_safe = buffer_end - 1;

        skip = (((w * 16 + 31) / 32) * 4) - (w * 2);
        for (y = 0; y < h; y++)
          {
             for (x = 0; x < w && buffer_ptr < buffer_end_safe; x++)
               {
                  pixel = *(unsigned short *)buffer_ptr;

                  if (im->flags & F_HAS_ALPHA)
                     a = SCALE(a, pixel);
                  else
                     a = 0xff;
                  r = SCALE(r, pixel);
                  g = SCALE(g, pixel);
                  b = SCALE(b, pixel);
                  *ptr++ = PIXEL_ARGB(a, r, g, b);
                  buffer_ptr += 2;
               }
             ptr -= w * 2;
             buffer_ptr += skip;

             if (progress)
               {
                  char                per;
                  int                 ll;

                  per = (char)((100 * y) / im->h);
                  if (((per - pper) >= progress_granularity) ||
                      (y == (im->h - 1)))
                    {
                       ll = y - pl;
                       if (!progress
                           (im, per, 0, im->h - y - 1, im->w, im->h - y + ll))
                         {
                            free(buffer);
                            return 2;
                         }
                       pper = per;
                       pl = y;
                    }
               }
          }
        break;

     case 24:
        buffer_end_safe = buffer_end - 2;

        skip = (4 - ((w * 3) % 4)) & 3;
        for (y = 0; y < h; y++)
          {
             for (x = 0; x < w && buffer_ptr < buffer_end_safe; x++)
               {
                  b = *buffer_ptr++;
                  g = *buffer_ptr++;
                  r = *buffer_ptr++;
                  *ptr++ = PIXEL_ARGB(0xff, r, g, b);
               }
             ptr -= w * 2;
             buffer_ptr += skip;

             if (progress)
               {
                  char                per;
                  int                 ll;

                  per = (char)((100 * y) / im->h);
                  if (((per - pper) >= progress_granularity) ||
                      (y == (im->h - 1)))
                    {
                       ll = y - pl;
                       if (!progress
                           (im, per, 0, im->h - y - 1, im->w, im->h - y + ll))
                         {
                            free(buffer);
                            return 2;
                         }
                       pper = per;
                       pl = y;
                    }
               }
          }
        break;

     case 32:
        buffer_end_safe = buffer_end - 3;

        skip = (((w * 32 + 31) / 32) * 4) - (w * 4);
        for (y = 0; y < h; y++)
          {
             for (x = 0; x < w && buffer_ptr < buffer_end_safe; x++)
               {
                  pixel = *(unsigned int *)buffer_ptr;

                  if (im->flags & F_HAS_ALPHA)
                     a = SCALE(a, pixel);
                  else
                     a = 0xff;
                  r = SCALE(r, pixel);
                  g = SCALE(g, pixel);
                  b = SCALE(b, pixel);
                  *ptr++ = PIXEL_ARGB(a, r, g, b);
                  buffer_ptr += 4;
               }
             ptr -= w * 2;
             buffer_ptr += skip;

             if (progress)
               {
                  char                per;
                  int                 ll;

                  per = (char)((100 * y) / im->h);
                  if (((per - pper) >= progress_granularity) ||
                      (y == (im->h - 1)))
                    {
                       ll = y - pl;
                       if (!progress
                           (im, per, 0, im->h - y - 1, im->w, im->h - y + ll))
                         {
                            free(buffer);
                            return 2;
                         }
                       pper = per;
                       pl = y;
                    }
               }
          }
        break;
     }

   free(buffer);
   return 1;

 quit_err:
   fclose(f);
 quit_err2:
   free(buffer);
   return 0;
}

char
save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
{
   FILE               *f;
   int                 i, j, pad;
   DATA32              pixel;

   if (!im->data)
      return 0;

   f = fopen(im->real_file, "wb");
   if (!f)
      return 0;

   /* calculate number of bytes to pad on end of each row */
   pad = (4 - ((im->w * 3) % 4)) & 0x03;

   /* write BMP file header */
   WriteleShort(f, 0x4d42);     /* prefix */
   WriteleLong(f, 54 + 3 * im->w * im->h);      /* filesize */
   WriteleShort(f, 0x0000);     /* reserved #1 */
   WriteleShort(f, 0x0000);     /* reserved #2 */
   WriteleLong(f, 54);          /* offset to image data */

   /* write BMP bitmap header */
   WriteleLong(f, 40);          /* 40-byte header */
   WriteleLong(f, im->w);
   WriteleLong(f, im->h);
   WriteleShort(f, 1);          /* one plane      */
   WriteleShort(f, 24);         /* bits per pixel */
   WriteleLong(f, 0);           /* no compression */
   WriteleLong(f, 3 * im->w * im->h);
   for (i = 0; i < 4; i++)
      WriteleLong(f, 0x0000);   /* pad to end of header */

   /* write actual BMP data */
   for (i = 0; i < im->h; i++)
     {
        for (j = 0; j < im->w; j++)
          {
             pixel = im->data[im->w * (im->h - i - 1) + j];
             WriteleByte(f, PIXEL_B(pixel));
             WriteleByte(f, PIXEL_G(pixel));
             WriteleByte(f, PIXEL_R(pixel));
          }
        for (j = 0; j < pad; j++)
           WriteleByte(f, 0);
     }

   fclose(f);
   return 1;
}

void
formats(ImlibLoader * l)
{
   static const char  *const list_formats[] = { "bmp" };
   int                 i;

   l->num_formats = sizeof(list_formats) / sizeof(char *);
   l->formats = malloc(sizeof(char *) * l->num_formats);

   for (i = 0; i < l->num_formats; i++)
      l->formats[i] = strdup(list_formats[i]);
}