bmp2int32.c 1.19 KB

/* 
   convert 8bits bmp to biniary file
   File format:
   color index (#of color * 4)
   Then pixel value 
 */
 
#include <stdio.h>
#include <stdlib.h>

#define PIXEL_START  (54 + 1024)

int main(int argc, char **argv)
{
    FILE *src, *dst;
    int i, r, g, b, a, rgba, w, h, color_num, bpp;
    unsigned char header[54];

    if (argc != 3 ) {
        printf("Usage: bmp2int 8/4bits_bmp fake_intensity_file\n");
        return;
    }
    
    src = fopen(argv[1], "rb");
    dst = fopen(argv[2], "wb");

    fread(header, 1, sizeof(header), src);
    w = (header[21] << 24) | (header[20] << 16) | (header[19] << 8) | header[18];
    h = (header[25] << 24) | (header[24] << 16) | (header[23] << 8) | header[22];
    color_num = 1 << header[0x1c]; 
    bpp = w * header[0x1c] / 8;

    for (i=0; i<color_num; i++) {
        b = fgetc(src);
        g = fgetc(src);
        r = fgetc(src);
        a = fgetc(src);

        fputc( r, dst);
        fputc( g, dst);
        fputc( b, dst);
        fputc( 0xff, dst);
    }

    for (i=0; i<h; i++) {
        fseek(src, 54+color_num*4+(h-i-1)*bpp, SEEK_SET);
        for (r=0; r<bpp; r++) fputc(fgetc(src), dst);
    }

    fclose(src);
    fclose(dst);

    return 0;
}