00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "jinclude.h"
00020 #include "jpeglib.h"
00021 #include "jerror.h"
00022
00023 #ifndef HAVE_STDLIB_H
00024 extern void * malloc JPP((size_t size));
00025 extern void free JPP((void *ptr));
00026 #endif
00027
00028
00029
00030
00031 typedef struct {
00032 struct jpeg_destination_mgr pub;
00033
00034 FILE * outfile;
00035 JOCTET * buffer;
00036 } my_destination_mgr;
00037
00038 typedef my_destination_mgr * my_dest_ptr;
00039
00040 #define OUTPUT_BUF_SIZE 4096
00041
00042
00043
00044
00045 typedef struct {
00046 struct jpeg_destination_mgr pub;
00047
00048 unsigned char ** outbuffer;
00049 unsigned long * outsize;
00050 unsigned char * newbuffer;
00051 JOCTET * buffer;
00052 size_t bufsize;
00053 } my_mem_destination_mgr;
00054
00055 typedef my_mem_destination_mgr * my_mem_dest_ptr;
00056
00057
00058
00059
00060
00061
00062
00063 METHODDEF(void)
00064 init_destination (j_compress_ptr cinfo)
00065 {
00066 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00067
00068
00069 dest->buffer = (JOCTET *)
00070 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00071 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
00072
00073 dest->pub.next_output_byte = dest->buffer;
00074 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
00075 }
00076
00077 METHODDEF(void)
00078 init_mem_destination (j_compress_ptr cinfo)
00079 {
00080
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 METHODDEF(boolean)
00108 empty_output_buffer (j_compress_ptr cinfo)
00109 {
00110 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00111
00112 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
00113 (size_t) OUTPUT_BUF_SIZE)
00114 ERREXIT(cinfo, JERR_FILE_WRITE);
00115
00116 dest->pub.next_output_byte = dest->buffer;
00117 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
00118
00119 return TRUE;
00120 }
00121
00122 METHODDEF(boolean)
00123 empty_mem_output_buffer (j_compress_ptr cinfo)
00124 {
00125 size_t nextsize;
00126 JOCTET * nextbuffer;
00127 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
00128
00129
00130 nextsize = dest->bufsize * 2;
00131 nextbuffer = malloc(nextsize);
00132
00133 if (nextbuffer == NULL)
00134 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
00135
00136 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
00137
00138 if (dest->newbuffer != NULL)
00139 free(dest->newbuffer);
00140
00141 dest->newbuffer = nextbuffer;
00142
00143 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
00144 dest->pub.free_in_buffer = dest->bufsize;
00145
00146 dest->buffer = nextbuffer;
00147 dest->bufsize = nextsize;
00148
00149 return TRUE;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 METHODDEF(void)
00163 term_destination (j_compress_ptr cinfo)
00164 {
00165 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
00166 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
00167
00168
00169 if (datacount > 0) {
00170 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
00171 ERREXIT(cinfo, JERR_FILE_WRITE);
00172 }
00173 fflush(dest->outfile);
00174
00175 if (ferror(dest->outfile))
00176 ERREXIT(cinfo, JERR_FILE_WRITE);
00177 }
00178
00179 METHODDEF(void)
00180 term_mem_destination (j_compress_ptr cinfo)
00181 {
00182 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
00183
00184 *dest->outbuffer = dest->buffer;
00185 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 GLOBAL(void)
00196 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
00197 {
00198 my_dest_ptr dest;
00199
00200
00201
00202
00203
00204
00205
00206 if (cinfo->dest == NULL) {
00207 cinfo->dest = (struct jpeg_destination_mgr *)
00208 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00209 SIZEOF(my_destination_mgr));
00210 }
00211
00212 dest = (my_dest_ptr) cinfo->dest;
00213 dest->pub.init_destination = init_destination;
00214 dest->pub.empty_output_buffer = empty_output_buffer;
00215 dest->pub.term_destination = term_destination;
00216 dest->outfile = outfile;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 GLOBAL(void)
00232 jpeg_mem_dest (j_compress_ptr cinfo,
00233 unsigned char ** outbuffer, unsigned long * outsize)
00234 {
00235 my_mem_dest_ptr dest;
00236
00237 if (outbuffer == NULL || outsize == NULL)
00238 ERREXIT(cinfo, JERR_BUFFER_SIZE);
00239
00240
00241
00242
00243 if (cinfo->dest == NULL) {
00244 cinfo->dest = (struct jpeg_destination_mgr *)
00245 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00246 SIZEOF(my_mem_destination_mgr));
00247 }
00248
00249 dest = (my_mem_dest_ptr) cinfo->dest;
00250 dest->pub.init_destination = init_mem_destination;
00251 dest->pub.empty_output_buffer = empty_mem_output_buffer;
00252 dest->pub.term_destination = term_mem_destination;
00253 dest->outbuffer = outbuffer;
00254 dest->outsize = outsize;
00255 dest->newbuffer = NULL;
00256
00257 if (*outbuffer == NULL || *outsize == 0) {
00258
00259 dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE);
00260 if (dest->newbuffer == NULL)
00261 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
00262 *outsize = OUTPUT_BUF_SIZE;
00263 }
00264
00265 dest->pub.next_output_byte = dest->buffer = *outbuffer;
00266 dest->pub.free_in_buffer = dest->bufsize = *outsize;
00267 }