blob: 3f23028c467e5da2f7c59dc5f9b4c1077a4e5960 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmainct.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
11 * This file contains the main buffer controller for compression.
12 * The main buffer lies between the pre-processor and the JPEG
13 * compressor proper; it holds downsampled data in the JPEG colorspace.
14 */
15
16#define JPEG_INTERNALS
17#include "jinclude.h"
18#include "jpeglib.h"
19
20
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000021/* Private buffer controller object */
22
23typedef struct {
24 struct jpeg_c_main_controller pub; /* public fields */
25
DRCe5eaf372014-05-09 18:00:32 +000026 JDIMENSION cur_iMCU_row; /* number of current iMCU row */
27 JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
28 boolean suspended; /* remember if we suspended output */
29 J_BUF_MODE pass_mode; /* current operating mode */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030
31 /* If using just a strip buffer, this points to the entire set of buffers
32 * (we allocate one for each component). In the full-image case, this
33 * points to the currently accessible strips of the virtual arrays.
34 */
35 JSAMPARRAY buffer[MAX_COMPONENTS];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036} my_main_controller;
37
Alex Naidis6eb7d372016-10-16 23:10:08 +020038typedef my_main_controller *my_main_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039
40
41/* Forward declarations */
Leon Scroggins III3993b372018-07-16 10:43:45 -040042METHODDEF(void) process_data_simple_main(j_compress_ptr cinfo,
43 JSAMPARRAY input_buf,
44 JDIMENSION *in_row_ctr,
45 JDIMENSION in_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046
47
48/*
49 * Initialize for a processing pass.
50 */
51
Thomas G. Lane489583f1996-02-07 00:00:00 +000052METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040053start_pass_main(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054{
Leon Scroggins III3993b372018-07-16 10:43:45 -040055 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056
57 /* Do nothing in raw-data mode. */
58 if (cinfo->raw_data_in)
59 return;
60
DRCa29d6b42014-12-01 20:13:12 +000061 if (pass_mode != JBUF_PASS_THRU)
62 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
63
DRCe5eaf372014-05-09 18:00:32 +000064 main_ptr->cur_iMCU_row = 0; /* initialize counters */
DRC7ed7b572012-02-07 22:51:49 +000065 main_ptr->rowgroup_ctr = 0;
66 main_ptr->suspended = FALSE;
DRCe5eaf372014-05-09 18:00:32 +000067 main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */
DRCa29d6b42014-12-01 20:13:12 +000068 main_ptr->pub.process_data = process_data_simple_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069}
70
71
72/*
73 * Process some data.
74 * This routine handles the simple pass-through mode,
75 * where we have only a strip buffer.
76 */
77
Thomas G. Lane489583f1996-02-07 00:00:00 +000078METHODDEF(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040079process_data_simple_main(j_compress_ptr cinfo, JSAMPARRAY input_buf,
80 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081{
Leon Scroggins III3993b372018-07-16 10:43:45 -040082 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083
DRC7ed7b572012-02-07 22:51:49 +000084 while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 /* Read input data if we haven't filled the main buffer yet */
DRC7ed7b572012-02-07 22:51:49 +000086 if (main_ptr->rowgroup_ctr < DCTSIZE)
Leon Scroggins III3993b372018-07-16 10:43:45 -040087 (*cinfo->prep->pre_process_data) (cinfo, input_buf, in_row_ctr,
88 in_rows_avail, main_ptr->buffer,
89 &main_ptr->rowgroup_ctr,
90 (JDIMENSION)DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091
92 /* If we don't have a full iMCU row buffered, return to application for
93 * more data. Note that preprocessor will always pad to fill the iMCU row
94 * at the bottom of the image.
95 */
DRC7ed7b572012-02-07 22:51:49 +000096 if (main_ptr->rowgroup_ctr != DCTSIZE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 return;
98
99 /* Send the completed row to the compressor */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400100 if (!(*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000101 /* If compressor did not consume the whole row, then we must need to
102 * suspend processing and return to the application. In this situation
103 * we pretend we didn't yet consume the last input row; otherwise, if
104 * it happened to be the last row of the image, the application would
105 * think we were done.
106 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400107 if (!main_ptr->suspended) {
DRCe5eaf372014-05-09 18:00:32 +0000108 (*in_row_ctr)--;
109 main_ptr->suspended = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 }
111 return;
112 }
113 /* We did finish the row. Undo our little suspension hack if a previous
114 * call suspended; then mark the main buffer empty.
115 */
DRC7ed7b572012-02-07 22:51:49 +0000116 if (main_ptr->suspended) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117 (*in_row_ctr)++;
DRC7ed7b572012-02-07 22:51:49 +0000118 main_ptr->suspended = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000119 }
DRC7ed7b572012-02-07 22:51:49 +0000120 main_ptr->rowgroup_ctr = 0;
121 main_ptr->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000122 }
123}
124
125
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000126/*
127 * Initialize main buffer controller.
128 */
129
Thomas G. Lane489583f1996-02-07 00:00:00 +0000130GLOBAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400131jinit_c_main_controller(j_compress_ptr cinfo, boolean need_full_buffer)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132{
DRC7ed7b572012-02-07 22:51:49 +0000133 my_main_ptr main_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134 int ci;
135 jpeg_component_info *compptr;
136
DRC7ed7b572012-02-07 22:51:49 +0000137 main_ptr = (my_main_ptr)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400138 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000139 sizeof(my_main_controller));
Leon Scroggins III3993b372018-07-16 10:43:45 -0400140 cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
DRC7ed7b572012-02-07 22:51:49 +0000141 main_ptr->pub.start_pass = start_pass_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142
143 /* We don't need to create a buffer in raw-data mode. */
144 if (cinfo->raw_data_in)
145 return;
146
147 /* Create the buffer. It holds downsampled data, so each component
148 * may be of a different size.
149 */
150 if (need_full_buffer) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153 /* Allocate a strip buffer for each component */
154 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000155 ci++, compptr++) {
DRC7ed7b572012-02-07 22:51:49 +0000156 main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400157 ((j_common_ptr)cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000158 compptr->width_in_blocks * DCTSIZE,
Leon Scroggins III3993b372018-07-16 10:43:45 -0400159 (JDIMENSION)(compptr->v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 }
161 }
162}