15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * jcmainct.c
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1994-1996, Thomas G. Lane.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This file is part of the Independent JPEG Group's software.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * For conditions of distribution and use, see the accompanying README file.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * This file contains the main buffer controller for compression.
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * The main buffer lies between the pre-processor and the JPEG
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * compressor proper; it holds downsampled data in the JPEG colorspace.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define JPEG_INTERNALS
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jinclude.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jpeglib.h"
167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Note: currently, there is no operating mode in which a full-image buffer
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * is needed at this step.  If there were, that mode could not be used with
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "raw data" input, since this module is bypassed in that case.  However,
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * we've left the code here for possible use in special applications.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FULL_MAIN_BUFFER_SUPPORTED
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Private buffer controller object */
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef struct {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct jpeg_c_main_controller pub; /* public fields */
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  boolean suspended;		/* remember if we suspended output */
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  J_BUF_MODE pass_mode;		/* current operating mode */
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  /* If using just a strip buffer, this points to the entire set of buffers
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   * (we allocate one for each component).  In the full-image case, this
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   * points to the currently accessible strips of the virtual arrays.
39ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch   */
40ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  JSAMPARRAY buffer[MAX_COMPONENTS];
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef FULL_MAIN_BUFFER_SUPPORTED
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /* If using full-image storage, this array holds pointers to virtual-array
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   * control blocks for each component.  Unused if not full-image storage.
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   */
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} my_main_controller;
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef my_main_controller * my_main_ptr;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Forward declarations */
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)METHODDEF(void) process_data_simple_main
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef FULL_MAIN_BUFFER_SUPPORTED
58ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochMETHODDEF(void) process_data_buffer_main
59ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Initialize for a processing pass.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)METHODDEF(void)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  my_main_ptr main = (my_main_ptr) cinfo->main;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /* Do nothing in raw-data mode. */
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (cinfo->raw_data_in)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main->cur_iMCU_row = 0;	/* initialize counters */
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main->rowgroup_ctr = 0;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main->suspended = FALSE;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main->pass_mode = pass_mode;	/* save mode for use by process_data */
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (pass_mode) {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  case JBUF_PASS_THRU:
847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#ifdef FULL_MAIN_BUFFER_SUPPORTED
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (main->whole_image[0] != NULL)
867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    main->pub.process_data = process_data_simple_main;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    break;
907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#ifdef FULL_MAIN_BUFFER_SUPPORTED
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  case JBUF_SAVE_SOURCE:
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  case JBUF_CRANK_DEST:
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  case JBUF_SAVE_AND_PASS:
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (main->whole_image[0] == NULL)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    main->pub.process_data = process_data_buffer_main;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    break;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  default:
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    break;
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Process some data.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * This routine handles the simple pass-through mode,
109f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * where we have only a strip buffer.
110f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) */
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)METHODDEF(void)
113ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochprocess_data_simple_main (j_compress_ptr cinfo,
114ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)			  JDIMENSION in_rows_avail)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  my_main_ptr main = (my_main_ptr) cinfo->main;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /* Read input data if we haven't filled the main buffer yet */
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (main->rowgroup_ctr < DCTSIZE)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (*cinfo->prep->pre_process_data) (cinfo,
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)					input_buf, in_row_ctr, in_rows_avail,
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)					main->buffer, &main->rowgroup_ctr,
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					(JDIMENSION) DCTSIZE);
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* If we don't have a full iMCU row buffered, return to application for
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     * more data.  Note that preprocessor will always pad to fill the iMCU row
1297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch     * at the bottom of the image.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     */
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (main->rowgroup_ctr != DCTSIZE)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* Send the completed row to the compressor */
135    if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
136      /* If compressor did not consume the whole row, then we must need to
137       * suspend processing and return to the application.  In this situation
138       * we pretend we didn't yet consume the last input row; otherwise, if
139       * it happened to be the last row of the image, the application would
140       * think we were done.
141       */
142      if (! main->suspended) {
143	(*in_row_ctr)--;
144	main->suspended = TRUE;
145      }
146      return;
147    }
148    /* We did finish the row.  Undo our little suspension hack if a previous
149     * call suspended; then mark the main buffer empty.
150     */
151    if (main->suspended) {
152      (*in_row_ctr)++;
153      main->suspended = FALSE;
154    }
155    main->rowgroup_ctr = 0;
156    main->cur_iMCU_row++;
157  }
158}
159
160
161#ifdef FULL_MAIN_BUFFER_SUPPORTED
162
163/*
164 * Process some data.
165 * This routine handles all of the modes that use a full-size buffer.
166 */
167
168METHODDEF(void)
169process_data_buffer_main (j_compress_ptr cinfo,
170			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
171			  JDIMENSION in_rows_avail)
172{
173  my_main_ptr main = (my_main_ptr) cinfo->main;
174  int ci;
175  jpeg_component_info *compptr;
176  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
177
178  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
179    /* Realign the virtual buffers if at the start of an iMCU row. */
180    if (main->rowgroup_ctr == 0) {
181      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
182	   ci++, compptr++) {
183	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
184	  ((j_common_ptr) cinfo, main->whole_image[ci],
185	   main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
186	   (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
187      }
188      /* In a read pass, pretend we just read some source data. */
189      if (! writing) {
190	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
191	main->rowgroup_ctr = DCTSIZE;
192      }
193    }
194
195    /* If a write pass, read input data until the current iMCU row is full. */
196    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
197    if (writing) {
198      (*cinfo->prep->pre_process_data) (cinfo,
199					input_buf, in_row_ctr, in_rows_avail,
200					main->buffer, &main->rowgroup_ctr,
201					(JDIMENSION) DCTSIZE);
202      /* Return to application if we need more data to fill the iMCU row. */
203      if (main->rowgroup_ctr < DCTSIZE)
204	return;
205    }
206
207    /* Emit data, unless this is a sink-only pass. */
208    if (main->pass_mode != JBUF_SAVE_SOURCE) {
209      if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
210	/* If compressor did not consume the whole row, then we must need to
211	 * suspend processing and return to the application.  In this situation
212	 * we pretend we didn't yet consume the last input row; otherwise, if
213	 * it happened to be the last row of the image, the application would
214	 * think we were done.
215	 */
216	if (! main->suspended) {
217	  (*in_row_ctr)--;
218	  main->suspended = TRUE;
219	}
220	return;
221      }
222      /* We did finish the row.  Undo our little suspension hack if a previous
223       * call suspended; then mark the main buffer empty.
224       */
225      if (main->suspended) {
226	(*in_row_ctr)++;
227	main->suspended = FALSE;
228      }
229    }
230
231    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
232    main->rowgroup_ctr = 0;
233    main->cur_iMCU_row++;
234  }
235}
236
237#endif /* FULL_MAIN_BUFFER_SUPPORTED */
238
239
240/*
241 * Initialize main buffer controller.
242 */
243
244GLOBAL(void)
245jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
246{
247  my_main_ptr main;
248  int ci;
249  jpeg_component_info *compptr;
250
251  main = (my_main_ptr)
252    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
253				SIZEOF(my_main_controller));
254  cinfo->main = (struct jpeg_c_main_controller *) main;
255  main->pub.start_pass = start_pass_main;
256
257  /* We don't need to create a buffer in raw-data mode. */
258  if (cinfo->raw_data_in)
259    return;
260
261  /* Create the buffer.  It holds downsampled data, so each component
262   * may be of a different size.
263   */
264  if (need_full_buffer) {
265#ifdef FULL_MAIN_BUFFER_SUPPORTED
266    /* Allocate a full-image virtual array for each component */
267    /* Note we pad the bottom to a multiple of the iMCU height */
268    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
269	 ci++, compptr++) {
270      main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
271	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
272	 compptr->width_in_blocks * DCTSIZE,
273	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
274				(long) compptr->v_samp_factor) * DCTSIZE,
275	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
276    }
277#else
278    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
279#endif
280  } else {
281#ifdef FULL_MAIN_BUFFER_SUPPORTED
282    main->whole_image[0] = NULL; /* flag for no virtual arrays */
283#endif
284    /* Allocate a strip buffer for each component */
285    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
286	 ci++, compptr++) {
287      main->buffer[ci] = (*cinfo->mem->alloc_sarray)
288	((j_common_ptr) cinfo, JPOOL_IMAGE,
289	 compptr->width_in_blocks * DCTSIZE,
290	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
291    }
292  }
293}
294