00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define JPEG_INTERNALS
00020 #include "jinclude.h"
00021 #include "jpeglib.h"
00022
00023
00024
00025
00026 typedef struct {
00027 struct jpeg_d_post_controller pub;
00028
00029
00030
00031
00032
00033
00034 jvirt_sarray_ptr whole_image;
00035 JSAMPARRAY buffer;
00036 JDIMENSION strip_height;
00037
00038 JDIMENSION starting_row;
00039 JDIMENSION next_row;
00040 } my_post_controller;
00041
00042 typedef my_post_controller * my_post_ptr;
00043
00044
00045
00046 METHODDEF(void) post_process_1pass
00047 JPP((j_decompress_ptr cinfo,
00048 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00049 JDIMENSION in_row_groups_avail,
00050 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00051 JDIMENSION out_rows_avail));
00052 #ifdef QUANT_2PASS_SUPPORTED
00053 METHODDEF(void) post_process_prepass
00054 JPP((j_decompress_ptr cinfo,
00055 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00056 JDIMENSION in_row_groups_avail,
00057 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00058 JDIMENSION out_rows_avail));
00059 METHODDEF(void) post_process_2pass
00060 JPP((j_decompress_ptr cinfo,
00061 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00062 JDIMENSION in_row_groups_avail,
00063 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00064 JDIMENSION out_rows_avail));
00065 #endif
00066
00067
00068
00069
00070
00071
00072 METHODDEF(void)
00073 start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
00074 {
00075 my_post_ptr post = (my_post_ptr) cinfo->post;
00076
00077 switch (pass_mode) {
00078 case JBUF_PASS_THRU:
00079 if (cinfo->quantize_colors) {
00080
00081 post->pub.post_process_data = post_process_1pass;
00082
00083
00084
00085
00086 if (post->buffer == NULL) {
00087 post->buffer = (*cinfo->mem->access_virt_sarray)
00088 ((j_common_ptr) cinfo, post->whole_image,
00089 (JDIMENSION) 0, post->strip_height, TRUE);
00090 }
00091 } else {
00092
00093
00094
00095 post->pub.post_process_data = cinfo->upsample->upsample;
00096 }
00097 break;
00098 #ifdef QUANT_2PASS_SUPPORTED
00099 case JBUF_SAVE_AND_PASS:
00100
00101 if (post->whole_image == NULL)
00102 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00103 post->pub.post_process_data = post_process_prepass;
00104 break;
00105 case JBUF_CRANK_DEST:
00106
00107 if (post->whole_image == NULL)
00108 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00109 post->pub.post_process_data = post_process_2pass;
00110 break;
00111 #endif
00112 default:
00113 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00114 break;
00115 }
00116 post->starting_row = post->next_row = 0;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 METHODDEF(void)
00126 post_process_1pass (j_decompress_ptr cinfo,
00127 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00128 JDIMENSION in_row_groups_avail,
00129 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00130 JDIMENSION out_rows_avail)
00131 {
00132 my_post_ptr post = (my_post_ptr) cinfo->post;
00133 JDIMENSION num_rows, max_rows;
00134
00135
00136
00137 max_rows = out_rows_avail - *out_row_ctr;
00138 if (max_rows > post->strip_height)
00139 max_rows = post->strip_height;
00140 num_rows = 0;
00141 (*cinfo->upsample->upsample) (cinfo,
00142 input_buf, in_row_group_ctr, in_row_groups_avail,
00143 post->buffer, &num_rows, max_rows);
00144
00145 (*cinfo->cquantize->color_quantize) (cinfo,
00146 post->buffer, output_buf + *out_row_ctr, (int) num_rows);
00147 *out_row_ctr += num_rows;
00148 }
00149
00150
00151 #ifdef QUANT_2PASS_SUPPORTED
00152
00153
00154
00155
00156
00157 METHODDEF(void)
00158 post_process_prepass (j_decompress_ptr cinfo,
00159 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00160 JDIMENSION in_row_groups_avail,
00161 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00162 JDIMENSION out_rows_avail)
00163 {
00164 my_post_ptr post = (my_post_ptr) cinfo->post;
00165 JDIMENSION old_next_row, num_rows;
00166
00167
00168 if (post->next_row == 0) {
00169 post->buffer = (*cinfo->mem->access_virt_sarray)
00170 ((j_common_ptr) cinfo, post->whole_image,
00171 post->starting_row, post->strip_height, TRUE);
00172 }
00173
00174
00175 old_next_row = post->next_row;
00176 (*cinfo->upsample->upsample) (cinfo,
00177 input_buf, in_row_group_ctr, in_row_groups_avail,
00178 post->buffer, &post->next_row, post->strip_height);
00179
00180
00181
00182 if (post->next_row > old_next_row) {
00183 num_rows = post->next_row - old_next_row;
00184 (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
00185 (JSAMPARRAY) NULL, (int) num_rows);
00186 *out_row_ctr += num_rows;
00187 }
00188
00189
00190 if (post->next_row >= post->strip_height) {
00191 post->starting_row += post->strip_height;
00192 post->next_row = 0;
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201 METHODDEF(void)
00202 post_process_2pass (j_decompress_ptr cinfo,
00203 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
00204 JDIMENSION in_row_groups_avail,
00205 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
00206 JDIMENSION out_rows_avail)
00207 {
00208 my_post_ptr post = (my_post_ptr) cinfo->post;
00209 JDIMENSION num_rows, max_rows;
00210
00211
00212 if (post->next_row == 0) {
00213 post->buffer = (*cinfo->mem->access_virt_sarray)
00214 ((j_common_ptr) cinfo, post->whole_image,
00215 post->starting_row, post->strip_height, FALSE);
00216 }
00217
00218
00219 num_rows = post->strip_height - post->next_row;
00220 max_rows = out_rows_avail - *out_row_ctr;
00221 if (num_rows > max_rows)
00222 num_rows = max_rows;
00223
00224 max_rows = cinfo->output_height - post->starting_row;
00225 if (num_rows > max_rows)
00226 num_rows = max_rows;
00227
00228
00229 (*cinfo->cquantize->color_quantize) (cinfo,
00230 post->buffer + post->next_row, output_buf + *out_row_ctr,
00231 (int) num_rows);
00232 *out_row_ctr += num_rows;
00233
00234
00235 post->next_row += num_rows;
00236 if (post->next_row >= post->strip_height) {
00237 post->starting_row += post->strip_height;
00238 post->next_row = 0;
00239 }
00240 }
00241
00242 #endif
00243
00244
00245
00246
00247
00248
00249 GLOBAL(void)
00250 jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
00251 {
00252 my_post_ptr post;
00253
00254 post = (my_post_ptr)
00255 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00256 SIZEOF(my_post_controller));
00257 cinfo->post = (struct jpeg_d_post_controller *) post;
00258 post->pub.start_pass = start_pass_dpost;
00259 post->whole_image = NULL;
00260 post->buffer = NULL;
00261
00262
00263 if (cinfo->quantize_colors) {
00264
00265
00266
00267
00268 post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
00269 if (need_full_buffer) {
00270
00271
00272 #ifdef QUANT_2PASS_SUPPORTED
00273 post->whole_image = (*cinfo->mem->request_virt_sarray)
00274 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
00275 cinfo->output_width * cinfo->out_color_components,
00276 (JDIMENSION) jround_up((long) cinfo->output_height,
00277 (long) post->strip_height),
00278 post->strip_height);
00279 #else
00280 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00281 #endif
00282 } else {
00283
00284 post->buffer = (*cinfo->mem->alloc_sarray)
00285 ((j_common_ptr) cinfo, JPOOL_IMAGE,
00286 cinfo->output_width * cinfo->out_color_components,
00287 post->strip_height);
00288 }
00289 }
00290 }