#include #include #include #include // 自作したメッセージ表示用関数 void my_output_message( j_common_ptr cinfo ) { char buffer[JMSG_LENGTH_MAX]; // メッセージの構築 (*cinfo->err->format_message) ( cinfo, buffer ); // 表示 printf( "my_output_message : %s\n", buffer ); } // 自作したエラー処理用の関数 void my_error_exit( j_common_ptr cinfo ) { printf( "my_err_exit\n" ); // 渡されたオブジェクトが、圧縮なのか展開なのかを判断する if ( cinfo->is_decompressor ) { // 圧縮用のオブジェクトだった printf( "decompress\n" ); } else { // 展開用のオブジェクトだった printf( "compress\n" ); } // client_dataの値を表示しておく printf( "%s\n", (char*)( cinfo->client_data ) ); // エラーメッセージを表示する my_output_message( cinfo ); // プロセスを終了する exit( 0 ); // 呼び出し側に制御を返してはいけない } void main() { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *outfile; JSAMPROW line; int i, j; // 一行分のデータを保持するためのメモリ領域を確保する line = (JSAMPROW)malloc( sizeof( JSAMPLE ) * 3 * 256 ); // JPEGオブジェクトの初期化 cinfo.err = jpeg_std_error( &jerr ); // 独自のエラーハンドラを設定 cinfo.client_data = "My Client Data"; jerr.error_exit = my_error_exit; jerr.output_message = my_output_message; // JPEG圧縮オブジェクトの構築 jpeg_create_compress( &cinfo ); // ファイルを開く outfile = fopen( "a.jpg", "wb" ); jpeg_stdio_dest( &cinfo, outfile ); // パラメータの設定 cinfo.image_width = 256; cinfo.image_height = 256; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; // デフォルト値の設定 jpeg_set_defaults( &cinfo ); // 圧縮の開始 jpeg_start_compress( &cinfo, TRUE ); // 一行ずつ出力する for ( i = 0; i < 256; i++ ) { // テスト用に途中で処理を打ち切ってしまう if ( i == 128 ) break; // 一行分のイメージデータを生成する for ( j = 0; j < 256; j++ ) { line[ j * 3 + 0 ] = i; line[ j * 3 + 1 ] = 0; line[ j * 3 + 2 ] = j; } // 一行出力 jpeg_write_scanlines( &cinfo, &line, 1 ); } // 圧縮を終了する jpeg_finish_compress( &cinfo ); // JPEGオブジェクトを破棄する jpeg_destroy_compress( &cinfo ); // ファイルを閉じる fclose( outfile ); // 一行分のイメージデータを保持するメモリ領域を開放する free( line ); }