写一个用C语言将jpg格式的图片转换成bmp格式图片的代码
时间: 2023-03-07 13:19:49 浏览: 496
C语言jpg转换成bmp格式代码
4星 · 用户满意度95%
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <bmp_lib.h>
int jpg2bmp(char *jpgname, char *bmpname)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *fp;
int row_stride;
int i, j;
unsigned char * line_buffer;
unsigned char *buffer;
unsigned char *data;
if ((fp = fopen(jpgname, "rb")) == NULL)
{
printf("open %s failed\n", jpgname);
return -1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
(void) jpeg_read_header(&cinfo, TRUE);
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
line_buffer = (unsigned char *)malloc(row_stride);
buffer = (unsigned char *)malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
data = buffer;
while (cinfo.output_scanline < cinfo.output_height)
{
(void) jpeg_read_scanlines(&cinfo, &line_buffer, 1);
for (i = 0; i < cinfo.output_width*cinfo.output_components; i++)
*(buffer++) = *(line_buffer + i);
}
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
free(line_buffer);
bmp_write(bmpname, cinfo.output_width, cinfo.output_height, 24, data);
free(data);
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("useage: jpg2bmp jpgname bmpname\n");
return -1;
}
jpg2bmp(argv[1], argv[2]);
return 0;
}答案:我提供了一段C语言的代码,可以将jpg格式的图片转换成bmp格式图片:#include <stdio.h>#include <stdlib.h>#include <jpeglib.h>#include <bmp_lib.h>int jpg2bmp(char *jpgname, char *bmpname){ struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp; int row_stride; int i, j; unsigned char * line_buffer; unsigned char *buffer; unsigned char *data; if ((fp = fopen(jpgname, "rb")) == NULL){ printf("open %s failed\n", jpgname); return -1;} cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); (void) jpeg_read_header(&cinfo, TRUE); (void) jpeg_start_decompress(&cinfo); row_stride = cinfo.output_width * cinfo.output_components; line_buffer = (unsigned char *)malloc(row_stride); buffer = (unsigned char *)malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components); data = buffer; while (cinfo.output_scanline < cinfo.output_height){ (void) jpeg_read_scanlines(&cinfo, &line_buffer, 1); for (i = 0; i < cinfo.output_width*cinfo.output_components; i++) *(buffer++) = *(line_buffer + i);} (void) jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(fp); free(line_buffer); bmp_write(bmpname, cinfo.output_width, cinfo.output_height, 24, data); free(data); return 0;} int main(int argc, char *argv[]){ if (argc != 3){ printf("useage: jpg2bmp jpgname bmpname\n"); return -1;} jpg2bmp(argv[1], argv[2]); return 0;}
阅读全文