C语言JPEG转BMP源码
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
void write_bmp_header(FILE *bmp_file, int width, int height) {
unsigned char bmpfileheader[14] = {
0x42, 0x4D, // BM
0, 0, 0, 0, // file size
0, 0, // reserved
0, 0, // reserved
54, 0, 0, 0 // start of pixel data
};
unsigned char bmpinfoheader[40] = {
40, 0, 0, 0, // size of this header
0, 0, 0, 0, // width of bitmap
0, 0, 0, 0, // height of bitmap
1, 0, // number of color planes
24, 0, // number of bits per pixel
0, 0, 0, 0, // compression method (no compression)
0, 0, 0, 0, // size of pixel data (can be 0 for BI_RGB bitmaps)
0, 0, 0, 0, // horizontal resolution
0, 0, 0, 0, // vertical resolution
0, 0, 0, 0, // number of colors in palette
0, 0, 0, 0 // important colors (0 = all colors are important)
};
int filesize = 54 + 3 * width * height;
bmpfileheader[2] = (unsigned char)(filesize);
bmpfileheader[3] = (unsigned char)(filesize >> 8);
bmpfileheader[4] = (unsigned char)(filesize >> 16);
bmpfileheader[5] = (unsigned char)(filesize >> 24);
bmpinfoheader[4] = (unsigned char)(width);
bmpinfoheader[5] = (unsigned char)(width >> 8);
bmpinfoheader[6] = (unsigned char)(width >> 16);
bmpinfoheader[7] = (unsigned char)(width >> 24);
bmpinfoheader[8] = (unsigned char)(height);
bmpinfoheader[9] = (unsigned char)(height >> 8);
bmpinfoheader[10] = (unsigned char)(height >> 16);
bmpinfoheader[11] = (unsigned char)(height >> 24);
fwrite(bmpfileheader, 1, 14, bmp_file);
fwrite(bmpinfoheader, 1, 40, bmp_file);
}
void jpeg_to_bmp(const char *jpeg_file, const char *bmp_file) {
FILE *infile = fopen(jpeg_file, "rb");
if (infile == NULL) {
fprintf(stderr, "Error opening JPEG file %s\n", jpeg_file);
return;
}
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
int width = cinfo.output_width;
int height = cinfo.output_height;
int pixel_size = cinfo.output_components;
unsigned char *buffer = (unsigned char *)malloc(width * pixel_size);
if (buffer == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return;
}
FILE *outfile = fopen(bmp_file, "wb");
if (outfile == NULL) {
fprintf(stderr, "Error opening BMP file %s\n", bmp_file);
return;
}
write_bmp_header(outfile, width, height);
unsigned char *row = (unsigned char *)malloc(3 * width);
if (row == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return;
}
while (cinfo.output_scanline < height) {
jpeg_read_scanlines(&cinfo, &buffer, 1);
for (int i = 0; i < width; i++) {
row[3 * i] = buffer[3 * i + 2]; // B
row[3 * i + 1] = buffer[3 * i + 1]; // G
row[3 * i + 2] = buffer[3 * i]; // R
}
fwrite(row, 3, width, outfile);
}
free(row);
fclose(outfile);
free(buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <input.jpg> <output.bmp>\n", argv[0]);
return 1;
}
jpeg_to_bmp(argv[1], argv[2]);
return 0;
}
|