在C中将24bpp bmp彩色图像转换为8bpp灰度bmp图像

时间:2019-07-25 16:20:51

标签: c bmp 8-bit

我正在尝试将24bpp BMP彩色图像转换为8bpp灰度BMP图像。我做了一些事情,但我没有得到100%正确的输出。

我已经将24bpp转换为8bpp。但结果图像颜色表也被视为像素数据。我尝试在标头中设置偏移量字节,但问题仍然存在。

#include <stdio.h>

int main()
{

    FILE* fIn = fopen("lena_colored_256.bmp","rb");         
    FILE* fOut = fopen("lena_gray.bmp", "w+");              
    int i, j, y;
    unsigned char byte[54];
    unsigned char colorTable[1024];
    if (fIn == NULL)// check if the input file has not been opened
    {
        printf("File does not exist.\n");
    }

    for (i = 0; i < 54; i++)//read the 54 byte header from fIn
    {
        byte[i] = getc(fIn);

    }
    byte[28] = (unsigned char)8; // write code to modify bitDepth to 8

    byte[11] = (unsigned char)04;
    //write code to add colorTable

    for (i = 0; i < 256; i++)
    {
            colorTable[i * 4 + 0] = i;
            colorTable[i * 4 + 1] = i;
            colorTable[i * 4 + 2] = i;
            colorTable[i * 4 + 3] = i;
    }


    for (i = 0; i < 54; i++)
    {
        putc(byte[i], fOut);
    }
    for (i = 0; i < 1024; i++)
    {
        putc(colorTable[i], fOut);

    }
        // extract image height, width and bitDepth from imageHeader 
    int *height = (int)& byte[18];
    int *width = (int)& byte[22];
    int *bitDepth = (int)& byte[28];

    printf("bitDepth : %d\n", *bitDepth);
    printf("width: %d\n", *width);
    printf("height: %d\n", *height);
    int size = (*height) * (*width)*3; //calculate image size

    unsigned char* buffer;
    buffer = (unsigned char*)malloc(sizeof(int) * size);

    for (i = 0; i < size; i=i+3)    //RGB to gray
    {

        buffer[i+2] = getc(fIn);//blue
        buffer[i+1] = getc(fIn);//green
        buffer[i+0] = getc(fIn);//red

        y = (buffer[i+0] * 0.33) + (buffer[i+1] * 0.33) + (buffer[i+2] * 0.33);

        putc(y, fOut);
    }


    fclose(fOut);
    fclose(fIn);

    return 0;
}

颜色表数据也被图像视为像素数据。 我已经检查了输入BMP文件的颜色表数据。我已经打印出文件指针的位置,在第94个字节输入后,它增加了2个字节而不是1个字节,这共发生4次,而其他1020次文件指针增加了1个字节。对此有任何解释吗?

2 个答案:

答案 0 :(得分:1)

更改24位到8位的位图并不像更改头文件中的位计数那样简单。 24位位图没有颜色表。您必须为8位建立一个颜色表。幸运的是,这对于灰度图像来说相对容易。

位图头文件中的几个值需要修改。

然后将24位更改为灰度,然后更改为8位位图。有关其他信息,请参见位图文件格式。另请阅读“填充”,其中位图的宽度(以字节为单位)应始终为4的倍数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#pragma pack(push, 2)
typedef struct {
    int16_t bfType;
    int32_t bfSize;
    int16_t bfReserved1;
    int16_t bfReserved2;
    int32_t bfOffBits;
} BITMAPFILEHEADER;

typedef struct {
    int32_t biSize;
    int32_t biWidth;
    int32_t biHeight;
    int16_t biPlanes;
    int16_t biBitCount;
    int32_t biCompression;
    int32_t biSizeImage;
    int32_t biXPelsPerMeter;
    int32_t biYPelsPerMeter;
    int32_t biClrUsed;
    int32_t biClrImportant;
} BITMAPINFOHEADER;
#pragma pack(pop)

int main()
{
    FILE* fin = fopen("fin.bmp", "rb");
    FILE* fout = fopen("fout.bmp", "wb");
    if(!fin) { printf("fin error\n"); goto error; }
    if(!fout) { printf("fout error\n"); goto error; }

    BITMAPFILEHEADER bf;
    BITMAPINFOHEADER bi;
    fread(&bf, sizeof bf, 1, fin);
    fread(&bi, sizeof bi, 1, fin);

    if(sizeof bf != 14) { printf("Wrong pragma pack\n"); goto error; }
    if(sizeof bi != 40) { printf("Wrong pragma pack\n"); goto error; }
    if(bf.bfType != 0x4D42) { printf("not bmp, or not LE system\n"); goto error; }
    if(bi.biSize != 40) { printf("Can't handle this bitmap format\n"); goto error; }
    if(bi.biBitCount != 24) { printf("not 24-bit\n"); goto error; }

    int height = bi.biHeight;
    if(height < 0) height = -height; 

    //width in bytes:
    int src_wb = ((bi.biWidth * 24 + 31) / 32) * 4;
    int dst_wb = ((bi.biWidth * 8 + 31) / 32) * 4;
    int src_size = src_wb * height;
    int dst_size = dst_wb * height;

    //allocate for source and destination
    uint8_t *src = malloc(src_size);
    uint8_t *dst = malloc(dst_size);

    //read pixels
    fread(src, 1, src_size, fin);

    //make gray scale color-table
    uint8_t clr[1024] = { 0 };
    for(int i = 0; i < 256; i++)
        clr[i * 4 + 0] = clr[i * 4 + 1] = clr[i * 4 + 2] = (uint8_t)i;

    for(int y = height - 1; y >= 0; y--)
    {
        for(int x = 0; x < bi.biWidth; x++)
        {
            uint8_t blu = src[y * src_wb + x * 3 + 0];
            uint8_t grn = src[y * src_wb + x * 3 + 1];
            uint8_t red = src[y * src_wb + x * 3 + 2];
            uint8_t gry = (uint8_t)(.33 * red + .34 * grn + .33 * blu);
            dst[y * dst_wb + x] = gry; //this will match the index in color-table
        }
    }

    //modify bitmap headers
    bf.bfSize = 54 + 1024 + dst_size;
    bi.biBitCount = 8;
    bi.biSizeImage = dst_size;

    fwrite(&bf, sizeof bf, 1, fout);
    fwrite(&bi, sizeof bi, 1, fout);
    fwrite(clr, 1, 1024, fout);
    fwrite(dst, 1, dst_size, fout);
    free(src);
    free(dst);
error:
    fclose(fout);
    fclose(fin);
    return 0;
}

答案 1 :(得分:1)

FILE* fOut = fopen("lena_gray.bmp", "w+");//ERROR
FILE* fOut = fopen("lena_gray.bmp", "wb");//TRUE