提问人:Ethan Leyden 提问时间:5/31/2023 更新时间:5/31/2023 访问量:88
如何使用 libjpeg 生成原始数据并创建 jpeg?
How to generate raw data and create a jpeg using libjpeg?
问:
我正在练习将 libjpeg 库用于一个单独的项目,我只想使用 libjpeg 生成一个蓝色的 100x100 jpeg 图像文件。我遵循了他们文档中的每个步骤,并在 .我已经利用 gdb 来更深入地挖掘正在发生的事情,但我在看到最初调用函数和下一步(在 中)之间发生的情况时遇到了问题。这是我的代码:jpeg_finish_compress()
jpeg_finish_compress()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "jpeglib.h"
int height = 100;
int width = 100;
int i, j;
int main() {
//init the image
unsigned char image[height][width][3];
for(i = 0; i < height; i++) {
for(j = 0; j < width; j++) {
image[i][j][0] = 0x0;
image[i][j][1] = 0x0;
image[i][j][2] = 0xFF;
}
}
//Create compression objects
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
//Choose a destination and add error handling
FILE *outfile = (FILE*) malloc(sizeof(FILE));
char* filename = "test.jpeg";
if(outfile == fopen(filename, "wb")) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
//Compression parameters
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3; //RGB. 1 if grayscale
cinfo.in_color_space = JCS_RGB; //Could be JCS_GRAYSCALE also
//Note: There are other compression parameters, but for most purposes, you can use:
jpeg_set_defaults(&cinfo);
//DO the compression
jpeg_start_compress(&cinfo, TRUE);
JSAMPROW row_pointer[1]; //pointer to a row with 12-bit precision
while(cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = *image[cinfo.next_scanline];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile); //close the file, responsibly
jpeg_destroy_compress(&cinfo);
}
我认为这与我构建变量的方式有关,但也可能是我错误地使用了库。究竟是做什么的,如何从一组RGB数据中获取jpeg?image
jpeg_finish_compress
答: 暂无答案
评论
example.c
jpeg_set_defaults()