我在Ubuntu 14.04环境中使用libjpeg解压缩我的JPEG图像并将其写入BMP文件。但是,如果我有300x300分辨率(2550x4206像素)的彩色JPEG图像,则输出BMP文件为灰度,图像看起来很暗淡。分辨率为200x200,400x400和600x600的其他JPEG图像将输出正确的BMP图像。需要你的建议。请帮忙。谢谢。
链接到JPEG图片:https://drive.google.com/file/d/0B3ob0t07z5xEdmtVVWRicUQ5SGs/view?usp=sharing
预览BMP不可用。在Ubuntu或Linux环境系统中下载并查看映像。我不知道它为什么不在谷歌甚至在Windows中显示图像。
链接到输出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 FILE :(位字段逐字节设置)
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;
答案 0 :(得分:0)
我实际上从@ user3629249得到了答案。谢谢。 我只在分辨率为300x300
时在每行的末尾添加了2个字节的填充padding = 0;
fwrite(&padding, 2, 1, bmpFile);