This is bmpsave.c in view mode; [Download] [Up]
#include <stdio.h> #include <libc.h> #include "../bmp.h" #include "../common.h" #include "save.h" static void saveBmpHeader(FILE *fp, commonInfo *cinf, int bits, int cnum, paltype *pal) { int i; unsigned char *p; int colors = 1 << bits; long iSize = ((((cinf->width * bits) + 31) >> 3) & ~3) * cinf->height; long hSize = (bits == 24)? 54 : (54 + colors * 4); putc('B', fp); putc('M', fp); put_long(iSize + hSize + 2, fp); /* File Size */ put_long(0, fp); /* Reserved */ put_long(hSize, fp); put_long(WIN3, fp); put_long((long)cinf->width, fp); put_long((long)cinf->height, fp); put_short(1, fp); /* plane */ put_short(bits, fp); put_long(0, fp); /* compression */ put_long(iSize, fp); /* image size */ put_long(2834, fp); /* Pixels/Meter */ put_long(2834, fp); /* 2834 = 72bpi */ put_long(colors, fp); /* colors */ put_long(colors, fp); if (bits != 24 && pal) { for (i = 0; i < cnum; i ++) { p = pal[i]; putc(p[BLUE], fp); putc(p[GREEN], fp); putc(p[RED], fp); putc(0, fp); } for ( ; i < colors; i ++) { putc(255, fp); putc(255, fp); putc(255, fp); /* white */ putc(0, fp); } } } int saveBmpbmap(FILE *fp, commonInfo *cinf, int cnum, paltype *pal, unsigned char **planes) { int x, y, r, g, b, a; int cnt, bits; bits = (cnum <= 2) ? 1 :((cnum <= 16) ? 4 : 8); if (pal) { saveBmpHeader(fp, cinf, bits, cnum, pal); for (y = cinf->height - 1; y >= 0; y--) { cnt = 0; resetPixel(planes, y); if (cnum <= 2) { int cc, mask; for (x = 0; x < cinf->width; x += 8, cnt++) { cc = 0; for (mask = 0x80; mask; mask >>= 1) { (void) getPixel(&r, &g, &b, &a); if (mapping(r, g, b)) cc |= mask; } putc(cc, fp); } }else if (cnum <= 16) { int cc, dd; for (x = 0; x < cinf->width; x++, cnt++) { (void) getPixel(&r, &g, &b, &a); cc = (mapping(r, g, b) << 4) & 0xf0; if (++x >= cinf->width) dd = 0; else { (void) getPixel(&r, &g, &b, &a); dd = mapping(r, g, b) & 0x0f; } putc((cc|dd), fp); } }else { /* 8 bits */ for (x = 0; x < cinf->width; x++, cnt++) { (void) getPixel(&r, &g, &b, &a); putc(mapping(r, g, b), fp); } } while (cnt & 0x03) { putc(0, fp); cnt++; } } }else { /* full color */ saveBmpHeader(fp, cinf, 24, 0, NULL); for (y = cinf->height - 1; y >= 0; y--) { resetPixel(planes, y); for (x = 0; x < cinf->width; x++) { (void) getPixel(&r, &g, &b, &a); putc(b, fp); putc(g, fp); putc(r, fp); } for (x *= 3 ; x & 0x03; x++) putc(0, fp); } } putc(0, fp); putc(0, fp); return 0; }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.