00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 extern "C" {
00023 typedef boolean(*boolean_jpeg_compress_struct)(jpeg_compress_struct*);
00024 typedef void(*void_jpeg_compress_struct)(jpeg_compress_struct*);
00025 }
00026
00027 typedef struct {
00028 struct jpeg_destination_mgr pub;
00029
00030 std::ofstream * outfile;
00031 JOCTET * buffer;
00032 } my_destination_mgr;
00033
00034 typedef my_destination_mgr * my_dest_ptr;
00035
00036 #define OUTPUT_BUF_SIZE 4096
00037
00038
00039
00040
00041
00042
00043
00044 METHODDEF(void)
00045 init_destination (j_compress_ptr cinfo)
00046 {
00047 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00048
00049
00050 dest->buffer = (JOCTET *)
00051 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00052 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
00053
00054 dest->pub.next_output_byte = dest->buffer;
00055 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 METHODDEF(boolean)
00083 empty_output_buffer (j_compress_ptr cinfo)
00084 {
00085 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00086
00087 #if 0
00088 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
00089 (size_t) OUTPUT_BUF_SIZE)
00090 ERREXIT(cinfo, JERR_FILE_WRITE);
00091 #else
00092 dest->outfile->write((char*)dest->buffer, OUTPUT_BUF_SIZE);
00093 if( dest->outfile->fail() )
00094 ERREXIT(cinfo, JERR_FILE_WRITE);
00095 #endif
00096
00097 dest->pub.next_output_byte = dest->buffer;
00098 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
00099
00100 return TRUE;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 METHODDEF(void)
00114 term_destination (j_compress_ptr cinfo)
00115 {
00116 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00117 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
00118
00119
00120 #if 0
00121 if (datacount > 0) {
00122 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
00123 ERREXIT(cinfo, JERR_FILE_WRITE);
00124 }
00125 fflush(dest->outfile);
00126
00127 if (ferror(dest->outfile))
00128 ERREXIT(cinfo, JERR_FILE_WRITE);
00129 #else
00130 if (datacount > 0) {
00131 dest->outfile->write((char*)dest->buffer, datacount);
00132 if (dest->outfile->fail())
00133 ERREXIT(cinfo, JERR_FILE_WRITE);
00134 dest->outfile->flush();
00135
00136 if (dest->outfile->fail())
00137 ERREXIT(cinfo, JERR_FILE_WRITE);
00138 }
00139 #endif
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 GLOBAL(void)
00150 jpeg_stdio_dest (j_compress_ptr cinfo, std::ofstream * outfile)
00151 {
00152 my_dest_ptr dest;
00153
00154
00155
00156
00157
00158
00159
00160 if (cinfo->dest == NULL) {
00161 cinfo->dest = (struct jpeg_destination_mgr *)
00162 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00163 SIZEOF(my_destination_mgr));
00164 }
00165
00166 dest = (my_dest_ptr) cinfo->dest;
00167 dest->pub.init_destination = reinterpret_cast<void_jpeg_compress_struct>(init_destination);
00168 dest->pub.empty_output_buffer = reinterpret_cast<boolean_jpeg_compress_struct>(empty_output_buffer);
00169 dest->pub.term_destination = reinterpret_cast<void_jpeg_compress_struct>(term_destination);
00170 dest->outfile = outfile;
00171 }