jcmainct.c 9.43 KB
Newer Older
1 2 3 4
/*
 * jcmainct.c
 *
 * Copyright (C) 1994-1996, Thomas G. Lane.
5
 * Modified 2003-2012 by Guido Vollbeding.
6 7 8
 * This file is part of the Independent JPEG Group's software.
 * For conditions of distribution and use, see the accompanying README file.
 *
9 10
 * This file contains the main buffer controller for compression.
 * The main buffer lies between the pre-processor and the JPEG
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 * compressor proper; it holds downsampled data in the JPEG colorspace.
 */

#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"


/* Note: currently, there is no operating mode in which a full-image buffer
 * is needed at this step.  If there were, that mode could not be used with
 * "raw data" input, since this module is bypassed in that case.  However,
 * we've left the code here for possible use in special applications.
 */
#undef FULL_MAIN_BUFFER_SUPPORTED


/* Private buffer controller object */

typedef struct {
  struct jpeg_c_main_controller pub; /* public fields */

  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
  boolean suspended;		/* remember if we suspended output */
  J_BUF_MODE pass_mode;		/* current operating mode */

  /* If using just a strip buffer, this points to the entire set of buffers
   * (we allocate one for each component).  In the full-image case, this
   * points to the currently accessible strips of the virtual arrays.
   */
  JSAMPARRAY buffer[MAX_COMPONENTS];

#ifdef FULL_MAIN_BUFFER_SUPPORTED
  /* If using full-image storage, this array holds pointers to virtual-array
   * control blocks for each component.  Unused if not full-image storage.
   */
  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
#endif
} my_main_controller;

typedef my_main_controller * my_main_ptr;


/* Forward declarations */
METHODDEF(void) process_data_simple_main
56 57
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
58 59
#ifdef FULL_MAIN_BUFFER_SUPPORTED
METHODDEF(void) process_data_buffer_main
60 61
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
62 63 64 65 66 67 68 69 70 71
#endif


/*
 * Initialize for a processing pass.
 */

METHODDEF(void)
start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
72
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
73 74 75 76 77

  /* Do nothing in raw-data mode. */
  if (cinfo->raw_data_in)
    return;

78 79 80 81
  mainp->cur_iMCU_row = 0;	/* initialize counters */
  mainp->rowgroup_ctr = 0;
  mainp->suspended = FALSE;
  mainp->pass_mode = pass_mode;	/* save mode for use by process_data */
82 83 84 85

  switch (pass_mode) {
  case JBUF_PASS_THRU:
#ifdef FULL_MAIN_BUFFER_SUPPORTED
86
    if (mainp->whole_image[0] != NULL)
87 88
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
89
    mainp->pub.process_data = process_data_simple_main;
90 91 92 93 94
    break;
#ifdef FULL_MAIN_BUFFER_SUPPORTED
  case JBUF_SAVE_SOURCE:
  case JBUF_CRANK_DEST:
  case JBUF_SAVE_AND_PASS:
95
    if (mainp->whole_image[0] == NULL)
96
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
97
    mainp->pub.process_data = process_data_buffer_main;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    break;
#endif
  default:
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
    break;
  }
}


/*
 * Process some data.
 * This routine handles the simple pass-through mode,
 * where we have only a strip buffer.
 */

METHODDEF(void)
process_data_simple_main (j_compress_ptr cinfo,
115 116
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
			  JDIMENSION in_rows_avail)
117
{
118
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
119

120 121 122
  while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
    /* Read input data if we haven't filled the main buffer yet */
    if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
123
      (*cinfo->prep->pre_process_data) (cinfo,
124 125 126
					input_buf, in_row_ctr, in_rows_avail,
					mainp->buffer, &mainp->rowgroup_ctr,
					(JDIMENSION) cinfo->min_DCT_v_scaled_size);
127 128 129 130 131

    /* If we don't have a full iMCU row buffered, return to application for
     * more data.  Note that preprocessor will always pad to fill the iMCU row
     * at the bottom of the image.
     */
132
    if (mainp->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
133 134 135
      return;

    /* Send the completed row to the compressor */
136
    if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
137 138 139 140 141 142
      /* If compressor did not consume the whole row, then we must need to
       * suspend processing and return to the application.  In this situation
       * we pretend we didn't yet consume the last input row; otherwise, if
       * it happened to be the last row of the image, the application would
       * think we were done.
       */
143
      if (! mainp->suspended) {
144 145
	(*in_row_ctr)--;
	mainp->suspended = TRUE;
146 147 148 149
      }
      return;
    }
    /* We did finish the row.  Undo our little suspension hack if a previous
150
     * call suspended; then mark the main buffer empty.
151
     */
152
    if (mainp->suspended) {
153
      (*in_row_ctr)++;
154
      mainp->suspended = FALSE;
155
    }
156 157
    mainp->rowgroup_ctr = 0;
    mainp->cur_iMCU_row++;
158 159 160 161 162 163 164 165 166 167 168 169 170
  }
}


#ifdef FULL_MAIN_BUFFER_SUPPORTED

/*
 * Process some data.
 * This routine handles all of the modes that use a full-size buffer.
 */

METHODDEF(void)
process_data_buffer_main (j_compress_ptr cinfo,
171 172
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
			  JDIMENSION in_rows_avail)
173
{
174
  my_main_ptr mainp = (my_main_ptr) cinfo->main;
175 176
  int ci;
  jpeg_component_info *compptr;
177
  boolean writing = (mainp->pass_mode != JBUF_CRANK_DEST);
178

179
  while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
180
    /* Realign the virtual buffers if at the start of an iMCU row. */
181
    if (mainp->rowgroup_ctr == 0) {
182
      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
183 184 185 186 187 188
	   ci++, compptr++) {
	mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
	  ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
	   ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
	   (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
	   writing);
189 190 191
      }
      /* In a read pass, pretend we just read some source data. */
      if (! writing) {
192 193 194
	*in_row_ctr += (JDIMENSION)
	  (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
	mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
195 196 197 198 199 200 201
      }
    }

    /* If a write pass, read input data until the current iMCU row is full. */
    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
    if (writing) {
      (*cinfo->prep->pre_process_data) (cinfo,
202 203 204
					input_buf, in_row_ctr, in_rows_avail,
					mainp->buffer, &mainp->rowgroup_ctr,
					(JDIMENSION) cinfo->min_DCT_v_scaled_size);
205
      /* Return to application if we need more data to fill the iMCU row. */
206
      if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
207
	return;
208 209 210
    }

    /* Emit data, unless this is a sink-only pass. */
211 212
    if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
      if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
213 214 215 216 217 218 219 220 221 222 223
	/* If compressor did not consume the whole row, then we must need to
	 * suspend processing and return to the application.  In this situation
	 * we pretend we didn't yet consume the last input row; otherwise, if
	 * it happened to be the last row of the image, the application would
	 * think we were done.
	 */
	if (! mainp->suspended) {
	  (*in_row_ctr)--;
	  mainp->suspended = TRUE;
	}
	return;
224 225
      }
      /* We did finish the row.  Undo our little suspension hack if a previous
226
       * call suspended; then mark the main buffer empty.
227
       */
228
      if (mainp->suspended) {
229 230
	(*in_row_ctr)++;
	mainp->suspended = FALSE;
231 232 233 234
      }
    }

    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
235 236
    mainp->rowgroup_ctr = 0;
    mainp->cur_iMCU_row++;
237 238 239 240 241 242 243
  }
}

#endif /* FULL_MAIN_BUFFER_SUPPORTED */


/*
244
 * Initialize main buffer controller.
245 246 247 248 249
 */

GLOBAL(void)
jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
{
250
  my_main_ptr mainp;
251 252 253
  int ci;
  jpeg_component_info *compptr;

254
  mainp = (my_main_ptr)
255
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
256
				SIZEOF(my_main_controller));
257 258
  cinfo->main = &mainp->pub;
  mainp->pub.start_pass = start_pass_main;
259 260 261 262 263 264 265 266 267 268 269 270 271

  /* We don't need to create a buffer in raw-data mode. */
  if (cinfo->raw_data_in)
    return;

  /* Create the buffer.  It holds downsampled data, so each component
   * may be of a different size.
   */
  if (need_full_buffer) {
#ifdef FULL_MAIN_BUFFER_SUPPORTED
    /* Allocate a full-image virtual array for each component */
    /* Note we pad the bottom to a multiple of the iMCU height */
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
272
	 ci++, compptr++) {
273
      mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
274 275 276 277 278 279
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
	 compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
	 ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
				 (long) compptr->v_samp_factor)) *
	 ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
	 (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
280 281 282 283 284 285
    }
#else
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
  } else {
#ifdef FULL_MAIN_BUFFER_SUPPORTED
286
    mainp->whole_image[0] = NULL; /* flag for no virtual arrays */
287 288 289
#endif
    /* Allocate a strip buffer for each component */
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
290
	 ci++, compptr++) {
291
      mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
292 293 294
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
	 (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
295 296 297
    }
  }
}