私はUbuntuIntrepidを使用しており、jpeglib626b-14を使用しています。私はいくつかのコードに取り組んでいましたが、それを実行しようとすると、上部に文字化けした出力がある黒い画面しか表示されませんでした。数時間のデバッグの後、ほぼJPEGベースに到達したので、サンプルコードを取得し、その周りに小さなコードを記述しましたが、出力はまったく同じでした。
jpeglibはこのシステムのもっと多くの場所で使用されていると確信しており、これは単にリポジトリからのバージョンであるため、これはjpeglibまたはUbuntuパッケージのバグであるとは言いがたいです。
以下にサンプルコードを配置します(ほとんどのコメントは削除されています)。入力JPEGファイルは3チャンネルの非圧縮640x480ファイルであるため、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);
}