home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / ImageSave.bproj / bmpsave.c next >
Encoding:
C/C++ Source or Header  |  1996-09-29  |  2.6 KB  |  114 lines

  1. #include <stdio.h>
  2. #include <libc.h>
  3. #include "../bmp.h"
  4. #include "../common.h"
  5. #include "save.h"
  6.  
  7. static void saveBmpHeader(FILE *fp,
  8.         commonInfo *cinf, int bits, int cnum, paltype *pal)
  9. {
  10.     int i;
  11.     unsigned char *p;
  12.     int colors = 1 << bits;
  13.     long iSize = ((((cinf->width * bits) + 31) >> 3) & ~3) * cinf->height;
  14.     long hSize = (bits == 24)? 54 : (54 + colors * 4);
  15.  
  16.     putc('B', fp);
  17.     putc('M', fp);
  18.     put_long(iSize + hSize + 2, fp);    /* File Size */
  19.     put_long(0, fp);    /* Reserved */
  20.     put_long(hSize, fp);
  21.     put_long(WIN3, fp);
  22.     put_long((long)cinf->width, fp);
  23.     put_long((long)cinf->height, fp);
  24.     put_short(1, fp);    /* plane */
  25.     put_short(bits, fp);
  26.     put_long(0, fp);    /* compression */
  27.     put_long(iSize, fp);    /* image size */
  28.     put_long(2834, fp);    /* Pixels/Meter */
  29.     put_long(2834, fp);    /* 2834 = 72bpi */
  30.     put_long(colors, fp);    /* colors */
  31.     put_long(colors, fp);
  32.  
  33.     if (bits != 24 && pal) {
  34.         for (i = 0; i < cnum; i ++) {
  35.             p = pal[i];
  36.             putc(p[BLUE], fp);
  37.             putc(p[GREEN], fp);
  38.             putc(p[RED], fp);
  39.             putc(0, fp);
  40.         }
  41.         for ( ; i < colors; i ++) {
  42.             putc(255, fp);
  43.             putc(255, fp);
  44.             putc(255, fp);    /* white */
  45.             putc(0, fp);
  46.         }
  47.     }
  48. }
  49.  
  50. int saveBmpbmap(FILE *fp, commonInfo *cinf,
  51.         int cnum, paltype *pal, unsigned char **planes)
  52. {
  53.     int    x, y, r, g, b, a;
  54.     int    cnt, bits;
  55.  
  56.     bits = (cnum <= 2) ? 1 :((cnum <= 16) ? 4 : 8);
  57.     if (pal) {
  58.         saveBmpHeader(fp, cinf, bits, cnum, pal);
  59.         for (y = cinf->height - 1; y >= 0; y--) {
  60.             cnt = 0;
  61.             resetPixel(planes, y);
  62.             if (cnum <= 2) {
  63.             int cc, mask;
  64.             for (x = 0; x < cinf->width; x += 8, cnt++) {
  65.                 cc = 0;
  66.                 for (mask = 0x80; mask; mask >>= 1) {
  67.                     (void) getPixel(&r, &g, &b, &a);
  68.                     if (mapping(r, g, b))
  69.                         cc |= mask;
  70.                 }
  71.                 putc(cc, fp);
  72.             }
  73.             }else if (cnum <= 16) {
  74.             int cc, dd;
  75.             for (x = 0; x < cinf->width; x++, cnt++) {
  76.                 (void) getPixel(&r, &g, &b, &a);
  77.                 cc = (mapping(r, g, b) << 4) & 0xf0;
  78.                 if (++x >= cinf->width) dd = 0;
  79.                 else {
  80.                     (void) getPixel(&r, &g, &b, &a);
  81.                     dd = mapping(r, g, b) & 0x0f;
  82.                 }
  83.                 putc((cc|dd), fp);
  84.             }
  85.             }else { /* 8 bits */
  86.             for (x = 0; x < cinf->width; x++, cnt++) {
  87.                 (void) getPixel(&r, &g, &b, &a);
  88.                 putc(mapping(r, g, b), fp);
  89.             }
  90.             }
  91.             while (cnt & 0x03) {
  92.             putc(0, fp);
  93.             cnt++;
  94.             }
  95.         }
  96.     }else { /* full color */
  97.         saveBmpHeader(fp, cinf, 24, 0, NULL);
  98.         for (y = cinf->height - 1; y >= 0; y--) {
  99.             resetPixel(planes, y);
  100.             for (x = 0; x < cinf->width; x++) {
  101.                 (void) getPixel(&r, &g, &b, &a);
  102.                 putc(b, fp);
  103.                 putc(g, fp);
  104.                 putc(r, fp);
  105.             }
  106.             for (x *= 3 ; x & 0x03; x++)
  107.                 putc(0, fp);
  108.         }
  109.     }
  110.     putc(0, fp);
  111.     putc(0, fp);
  112.     return 0;
  113. }
  114.