我在 Ubuntu Intrepid 上,我正在使用 jpeglib62 6b-14。我正在编写一些代码,当我尝试运行它时,它只会在顶部出现黑屏和一些乱码输出。经过几个小时的调试,我把它归结为 JPEG 基础,所以我拿了示例代码,围绕它写了一小段代码,输出完全相同。
我确信 jpeglib 在这个系统上的更多地方使用,它只是存储库中的版本,所以我很犹豫说这是 jpeglib 或 Ubuntu 打包中的错误。
我把示例代码放在下面(大部分评论被删除)。输入的 JPEG 文件是一个未压缩的 640x480 文件,有 3 个通道,所以它应该是 921600 字节(它是)。输出图像是 JFIF,大约 9000 字节。
如果您能给我一点提示,我将不胜感激。
谢谢!
#include <stdio.h>
#include <stdlib.h>
#include "jpeglib.h"
#include <setjmp.h>
int main ()
{
// read data
FILE *input = fopen("input.jpg", "rb");
JSAMPLE *image_buffer = (JSAMPLE*) malloc(sizeof(JSAMPLE) * 640 * 480 * 3);
if(input == NULL or image_buffer == NULL)
exit(1);
fread(image_buffer, 640 * 3, 480, input);
// initialise jpeg library
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
// write to foo.jpg
FILE *outfile = fopen("foo.jpg", "wb");
if (outfile == NULL)
exit(1);
jpeg_stdio_dest(&cinfo, outfile);
// setup library
cinfo.image_width = 640;
cinfo.image_height = 480;
cinfo.input_components = 3; // 3 components (R, G, B)
cinfo.in_color_space = JCS_RGB; // RGB
jpeg_set_defaults(&cinfo); // set defaults
// start compressing
int row_stride = 640 * 3; // number of characters in a row
JSAMPROW row_pointer[1]; // pointer to the current row data
jpeg_start_compress(&cinfo, TRUE); // start compressing to jpeg
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
// clean up
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}