具有 300x300 分辨率的 jpeg 到 bmp 输出失真的灰度图像

jpeg to bmp with 300x300 resolution outputs distorted grayscale image

我在 Ubuntu 14.04 环境中使用 libjpeg 来解压缩我的 JPEG 图像并将其写入 BMP 文件。但是,如果我有一个分辨率为 300x300(2550x4206 像素)的彩色 JPEG 图像,输出的 BMP 文件是灰度的,图像看起来很暗淡。其他分辨率为 200x200、400x400 和 600x600 的 JPEG 图像将输出正确的 BMP 图像。需要你的建议。请帮忙。谢谢。

link 到 JPEG 图片:https://drive.google.com/file/d/0B3ob0t07z5xEdmtVVWRicUQ5SGs/view?usp=sharing

BMP 预览不可用。在 Ubuntu 或 Linux 环境系统中下载并查看图像。我不知道为什么它不在 Google 甚至 Windows.

中显示图像

link输出BMP图片:https://drive.google.com/file/d/0B3ob0t07z5xEZTMycVRVX3Vscnc/view?usp=sharing

将解压缩的 JPEG 图像写入 BMP 文件的代码片段:

struct jpeg_decompress_struct cinfo;
unsigned int bytesPerRow = cinfo.output_width * cinfo.num_components;
unsigned int colColor;
FILE *bmpFile = NULL;

while (cinfo.output_scanline < cinfo.image_height) {

    JSAMPROW row_pointer[1];
    row_pointer[0] = raw_image
            + cinfo.output_scanline * bytesPerRow;
    jpeg_read_scanlines(&cinfo, row_pointer, 1);
    for (colColor = 0; colColor < cinfo.image_width; colColor++) {

        /* BMP scanlines should be aligned at 4-byte boundary */

    }

    /* write each row to bmp file */
    fwrite(row_pointer[0], 1, bytesPerRow, bmpFile);
}

BMP 文件:(位字段逐字节设置)

typedef struct {
    unsigned int img_bits_per_pixel;
    unsigned int img_scansize;
    unsigned int img_width;
    unsigned int img_height;
} Image_Information;

Image_Information *image_info;
image_info = (Image_Information *) malloc(sizeof(Image_Information));

image_info->img_height = cinfo.image_height;
image_info->img_width = cinfo.image_width;
image_info->img_scansize = ((image_info->img_width * 24 + 31) & ~31) / 8;

BITMAPFILEHEADER:
bfType = "BM";
bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + image_info->img_scansize * image_info->img_height;
bfReserved1 = 0;    
bfReserved2 = 0;
bfOffBits = 54;

BITMAPINFOHEADER:
biSize  = sizeof(BITMAPINFOHEADER); 
biWidth = image_info->img_width;
biHeight = image_info->img_heigh;
biPlanes = 1;
biBitCount = 24;
biCompression = 0;
biSizeImage = 0;
biXPelsPerMeter = 0;
biYPelsPerMeter = 0;
biClrUsed = 0;
biClrImportant = 0;

我实际上从@user3629249 那里得到了答案。谢谢你。 仅当分辨率为 300x300

时,我才在每行末尾添加 2 个字节的填充
padding = 0;
fwrite(&padding, 2, 1, bmpFile);