00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef INPUT_SMOOTHING_SUPPORTED
00028 #define CONTEXT_ROWS_SUPPORTED
00029 #endif
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 typedef struct {
00054 struct jpeg_c_prep_controller pub;
00055
00056
00057
00058
00059 JSAMPARRAY color_buf[MAX_COMPONENTS];
00060
00061 JDIMENSION rows_to_go;
00062 int next_buf_row;
00063
00064 #ifdef CONTEXT_ROWS_SUPPORTED
00065 int this_row_group;
00066 int next_buf_stop;
00067 #endif
00068 } my_prep_controller;
00069
00070 typedef my_prep_controller * my_prep_ptr;
00071
00072
00073
00074
00075
00076
00077 METHODDEF(void)
00078 start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00079 {
00080 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00081
00082 if (pass_mode != JBUF_PASS_THRU)
00083 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00084
00085
00086 prep->rows_to_go = cinfo->image_height;
00087
00088 prep->next_buf_row = 0;
00089 #ifdef CONTEXT_ROWS_SUPPORTED
00090
00091
00092
00093 prep->this_row_group = 0;
00094
00095 prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
00096 #endif
00097 }
00098
00099
00100
00101
00102
00103
00104
00105 LOCAL(void)
00106 expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
00107 int input_rows, int output_rows)
00108 {
00109 register int row;
00110
00111 for (row = input_rows; row < output_rows; row++) {
00112 jcopy_sample_rows(image_data, input_rows-1, image_data, row,
00113 1, num_cols);
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 METHODDEF(void)
00128 pre_process_data (j_compress_ptr cinfo,
00129 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00130 JDIMENSION in_rows_avail,
00131 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
00132 JDIMENSION out_row_groups_avail)
00133 {
00134 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00135 int numrows, ci;
00136 JDIMENSION inrows;
00137 jpeg_component_info * compptr;
00138
00139 while (*in_row_ctr < in_rows_avail &&
00140 *out_row_group_ctr < out_row_groups_avail) {
00141
00142 inrows = in_rows_avail - *in_row_ctr;
00143 numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
00144 numrows = (int) MIN((JDIMENSION) numrows, inrows);
00145 (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
00146 prep->color_buf,
00147 (JDIMENSION) prep->next_buf_row,
00148 numrows);
00149 *in_row_ctr += numrows;
00150 prep->next_buf_row += numrows;
00151 prep->rows_to_go -= numrows;
00152
00153 if (prep->rows_to_go == 0 &&
00154 prep->next_buf_row < cinfo->max_v_samp_factor) {
00155 for (ci = 0; ci < cinfo->num_components; ci++) {
00156 expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
00157 prep->next_buf_row, cinfo->max_v_samp_factor);
00158 }
00159 prep->next_buf_row = cinfo->max_v_samp_factor;
00160 }
00161
00162 if (prep->next_buf_row == cinfo->max_v_samp_factor) {
00163 (*cinfo->downsample->downsample) (cinfo,
00164 prep->color_buf, (JDIMENSION) 0,
00165 output_buf, *out_row_group_ctr);
00166 prep->next_buf_row = 0;
00167 (*out_row_group_ctr)++;
00168 }
00169
00170
00171
00172 if (prep->rows_to_go == 0 &&
00173 *out_row_group_ctr < out_row_groups_avail) {
00174 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00175 ci++, compptr++) {
00176 expand_bottom_edge(output_buf[ci],
00177 compptr->width_in_blocks * DCTSIZE,
00178 (int) (*out_row_group_ctr * compptr->v_samp_factor),
00179 (int) (out_row_groups_avail * compptr->v_samp_factor));
00180 }
00181 *out_row_group_ctr = out_row_groups_avail;
00182 break;
00183 }
00184 }
00185 }
00186
00187
00188 #ifdef CONTEXT_ROWS_SUPPORTED
00189
00190
00191
00192
00193
00194 METHODDEF(void)
00195 pre_process_context (j_compress_ptr cinfo,
00196 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00197 JDIMENSION in_rows_avail,
00198 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
00199 JDIMENSION out_row_groups_avail)
00200 {
00201 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00202 int numrows, ci;
00203 int buf_height = cinfo->max_v_samp_factor * 3;
00204 JDIMENSION inrows;
00205
00206 while (*out_row_group_ctr < out_row_groups_avail) {
00207 if (*in_row_ctr < in_rows_avail) {
00208
00209 inrows = in_rows_avail - *in_row_ctr;
00210 numrows = prep->next_buf_stop - prep->next_buf_row;
00211 numrows = (int) MIN((JDIMENSION) numrows, inrows);
00212 (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
00213 prep->color_buf,
00214 (JDIMENSION) prep->next_buf_row,
00215 numrows);
00216
00217 if (prep->rows_to_go == cinfo->image_height) {
00218 for (ci = 0; ci < cinfo->num_components; ci++) {
00219 int row;
00220 for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
00221 jcopy_sample_rows(prep->color_buf[ci], 0,
00222 prep->color_buf[ci], -row,
00223 1, cinfo->image_width);
00224 }
00225 }
00226 }
00227 *in_row_ctr += numrows;
00228 prep->next_buf_row += numrows;
00229 prep->rows_to_go -= numrows;
00230 } else {
00231
00232 if (prep->rows_to_go != 0)
00233 break;
00234
00235 if (prep->next_buf_row < prep->next_buf_stop) {
00236 for (ci = 0; ci < cinfo->num_components; ci++) {
00237 expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
00238 prep->next_buf_row, prep->next_buf_stop);
00239 }
00240 prep->next_buf_row = prep->next_buf_stop;
00241 }
00242 }
00243
00244 if (prep->next_buf_row == prep->next_buf_stop) {
00245 (*cinfo->downsample->downsample) (cinfo,
00246 prep->color_buf,
00247 (JDIMENSION) prep->this_row_group,
00248 output_buf, *out_row_group_ctr);
00249 (*out_row_group_ctr)++;
00250
00251 prep->this_row_group += cinfo->max_v_samp_factor;
00252 if (prep->this_row_group >= buf_height)
00253 prep->this_row_group = 0;
00254 if (prep->next_buf_row >= buf_height)
00255 prep->next_buf_row = 0;
00256 prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
00257 }
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266 LOCAL(void)
00267 create_context_buffer (j_compress_ptr cinfo)
00268 {
00269 my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
00270 int rgroup_height = cinfo->max_v_samp_factor;
00271 int ci, i;
00272 jpeg_component_info * compptr;
00273 JSAMPARRAY true_buffer, fake_buffer;
00274
00275
00276
00277
00278 fake_buffer = (JSAMPARRAY)
00279 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00280 (cinfo->num_components * 5 * rgroup_height) *
00281 SIZEOF(JSAMPROW));
00282
00283 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00284 ci++, compptr++) {
00285
00286
00287
00288
00289 true_buffer = (*cinfo->mem->alloc_sarray)
00290 ((j_common_ptr) cinfo, JPOOL_IMAGE,
00291 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
00292 cinfo->max_h_samp_factor) / compptr->h_samp_factor),
00293 (JDIMENSION) (3 * rgroup_height));
00294
00295 MEMCOPY(fake_buffer + rgroup_height, true_buffer,
00296 3 * rgroup_height * SIZEOF(JSAMPROW));
00297
00298 for (i = 0; i < rgroup_height; i++) {
00299 fake_buffer[i] = true_buffer[2 * rgroup_height + i];
00300 fake_buffer[4 * rgroup_height + i] = true_buffer[i];
00301 }
00302 prep->color_buf[ci] = fake_buffer + rgroup_height;
00303 fake_buffer += 5 * rgroup_height;
00304 }
00305 }
00306
00307 #endif
00308
00309
00310
00311
00312
00313
00314 GLOBAL(void)
00315 jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
00316 {
00317 my_prep_ptr prep;
00318 int ci;
00319 jpeg_component_info * compptr;
00320
00321 if (need_full_buffer)
00322 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00323
00324 prep = (my_prep_ptr)
00325 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00326 SIZEOF(my_prep_controller));
00327 cinfo->prep = (struct jpeg_c_prep_controller *) prep;
00328 prep->pub.start_pass = start_pass_prep;
00329
00330
00331
00332
00333
00334 if (cinfo->downsample->need_context_rows) {
00335
00336 #ifdef CONTEXT_ROWS_SUPPORTED
00337 prep->pub.pre_process_data = pre_process_context;
00338 create_context_buffer(cinfo);
00339 #else
00340 ERREXIT(cinfo, JERR_NOT_COMPILED);
00341 #endif
00342 } else {
00343
00344 prep->pub.pre_process_data = pre_process_data;
00345 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00346 ci++, compptr++) {
00347 prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
00348 ((j_common_ptr) cinfo, JPOOL_IMAGE,
00349 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
00350 cinfo->max_h_samp_factor) / compptr->h_samp_factor),
00351 (JDIMENSION) cinfo->max_v_samp_factor);
00352 }
00353 }
00354 }