-
-
-
-static void BMPEncodeLine(FILE *f, unsigned char *data, int npxls, int pixbytes)
-{
- int nbytes, i, j, k;
-
- switch (pixbytes)
- {
- case 1 :
- nbytes = (npxls + 3) / 4;
- nbytes *= 4;
-
- fwrite(data, npxls, 1, f);
- nbytes -= npxls;
-
- while (nbytes-- > 0) fputc(0, f);
- return;
-
- case 3 :
- // reorder rgb to bgr
- for (i = 0, j = 0; i < npxls; i++, j+= 3)
- {
- k = data[j];
- data[j] = data[j + 2];
- data[j + 2] = k;
- }
-
- nbytes = ((npxls * 3) + 3) / 4;
- nbytes *= 4;
-
- fwrite(data, npxls, 3, f);
- nbytes -= npxls * 3;
-
- while (nbytes-- > 0) fputc(0, f);
- return;
- }
-
- Error("BMPEncodeLine Failed.");
-}
-
-
-
-void WriteBMP(char *filename, bitmap_t *bit)
-{
- FILE *f;
- bmphd_t header;
- binfo_t info;
- drgb_t q; // palette that gets written
- long bmofs;
- int w, h, i;
- int pixbytes;
-
- if (bit->bpp == 8) pixbytes = 1;
- else if (bit->bpp == 24) pixbytes = 3;
-
- else
- {
- Error("Only 8BPP and 24BPP supported");
- //Error("BPP %d not supported.", bit->bpp);
- }
-
-
- if ((f = fopen(filename, "wb")) == NULL)
- {
- Error("Unable to open file");//%s.", filename);
- }
-
- // write out an empty header as a place holder
- if (fwrite(&header, sizeof(header), 1, f) != 1)
- {
- Error("Unable to fwrite.");
- }
-
- // init and write info header
- info.biSize = sizeof(binfo_t);
- info.biWidth = bit->width;
- info.biHeight = bit->height;
- info.biPlanes = 1;
- info.biBitCount = bit->bpp;
- info.biCompression = xBI_NONE;
- info.biSizeImage = bit->width * bit->height;
- info.biXPelsPerMeter = 0;
- info.biYPelsPerMeter = 0;
- info.biClrUsed = 256;
- info.biClrImportant = 256;
-
- if (fwrite(&info, sizeof(binfo_t), 1, f) != 1)
- {
- Error("fwrite failed.");
- }
-
- // write out palette if we need to
- if (bit->bpp == 8)
- {
- for (i = 0; i < 256; i++)
- {
- q.red = bit->palette[i].r;
- q.green = bit->palette[i].g;
- q.blue = bit->palette[i].b;
-
- fwrite(&q, sizeof(q), 1, f);
- }
- }
-
- // save offset to start of bitmap
- bmofs = ftell(f);
-
- // output bitmap
- w = bit->width;
- h = bit->height;
-
- for (i = h - 1; i >= 0; i--)
- {
- BMPEncodeLine(f, &bit->data[w * pixbytes * i], w, pixbytes);
- }
-
- // update and rewrite file header
- header.bfType = BMP_SIGNATURE_WORD;
- header.bfSize = ftell(f);
- header.bfOffBits = bmofs;
-
- fseek(f, 0L, SEEK_SET);
- fwrite(&header, sizeof(header), 1, f);
-
- fclose(f);
-}
-
-
-void NewBMP(int width, int height, int bpp, bitmap_t *bit)
-{
- int pixbytes;
-
- if (bpp == 8) pixbytes = 1;
- else if (bpp == 24) pixbytes = 3;
-
- else
- {
- Error("NewBMP: 8 or 24 bit only.");
- }
-
- bit->bpp = bpp;
- bit->width = width;
- bit->height = height;
-
- bit->data = reinterpret_cast<unsigned char*>(g_malloc(width * height * pixbytes));
-
- if (bit->data == NULL)
- {
- Error("NewBMP: g_malloc failed.");
- }
-
- // see if we need to create a palette
- if (pixbytes == 1)
- {
- bit->palette = (rgb_t *) g_malloc(768);
-
- if (bit->palette == NULL)
- {
- Error("NewBMP: unable to g_malloc palette.");
- }
- }
- else
- {
- bit->palette = NULL;
- }
-}
-
-
-
-void FreeBMP(bitmap_t *bitmap)
-{
- if (bitmap->palette)
- {
- g_free(bitmap->palette);
- bitmap->palette = NULL;
- }
-
- if (bitmap->data)
- {
- g_free(bitmap->data);
- bitmap->data = NULL;
- }
-}
-
-