1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * jcapistd.c
7 *
8 * Copyright (C) 1994-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This file contains application interface code for the compression half
13 * of the JPEG library. These are the "standard" API routines that are
14 * used in the normal full-compression case. They are not used by a
15 * transcoding-only application. Note that if an application links in
16 * jpeg_start_compress, it will end up linking in the entire compressor.
17 * We thus must separate this file from jcapimin.c to avoid linking the
18 * whole compression library into a transcoder.
19 */
20
21#define JPEG_INTERNALS
22#include "jinclude.h"
23#include "jpeglib.h"
24
25
26/*
27 * Compression initialization.
28 * Before calling this, all parameters and a data destination must be set up.
29 *
30 * We require a write_all_tables parameter as a failsafe check when writing
31 * multiple datastreams from the same compression object. Since prior runs
32 * will have left all the tables marked sent_table=TRUE, a subsequent run
33 * would emit an abbreviated stream (no tables) by default. This may be what
34 * is wanted, but for safety's sake it should not be the default behavior:
35 * programmers should have to make a deliberate choice to emit abbreviated
36 * images. Therefore the documentation and examples should encourage people
37 * to pass write_all_tables=TRUE; then it will take active thought to do the
38 * wrong thing.
39 */
40
41GLOBAL(void)
42jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
43{
44 if (cinfo->global_state != CSTATE_START)
45 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
46
47 if (write_all_tables)
48 jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
49
50 /* (Re)initialize error mgr and destination modules */
51 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
52 (*cinfo->dest->init_destination) (cinfo);
53 /* Perform master selection of active modules */
54 jinit_compress_master(cinfo);
55 /* Set up for the first pass */
56 (*cinfo->master->prepare_for_pass) (cinfo);
57 /* Ready for application to drive first pass through jpeg_write_scanlines
58 * or jpeg_write_raw_data.
59 */
60 cinfo->next_scanline = 0;
61 cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
62}
63
64
65/*
66 * Write some scanlines of data to the JPEG compressor.
67 *
68 * The return value will be the number of lines actually written.
69 * This should be less than the supplied num_lines only in case that
70 * the data destination module has requested suspension of the compressor,
71 * or if more than image_height scanlines are passed in.
72 *
73 * Note: we warn about excess calls to jpeg_write_scanlines() since
74 * this likely signals an application programmer error. However,
75 * excess scanlines passed in the last valid call are *silently* ignored,
76 * so that the application need not adjust num_lines for end-of-image
77 * when using a multiple-scanline buffer.
78 */
79
80GLOBAL(JDIMENSION)
81jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
82 JDIMENSION num_lines)
83{
84 JDIMENSION row_ctr, rows_left;
85
86 if (cinfo->global_state != CSTATE_SCANNING)
87 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
88 if (cinfo->next_scanline >= cinfo->image_height)
89 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
90
91 /* Call progress monitor hook if present */
92 if (cinfo->progress != NULL) {
93 cinfo->progress->pass_counter = (long) cinfo->next_scanline;
94 cinfo->progress->pass_limit = (long) cinfo->image_height;
95 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
96 }
97
98 /* Give master control module another chance if this is first call to
99 * jpeg_write_scanlines. This lets output of the frame/scan headers be
100 * delayed so that application can write COM, etc, markers between
101 * jpeg_start_compress and jpeg_write_scanlines.
102 */
103 if (cinfo->master->call_pass_startup)
104 (*cinfo->master->pass_startup) (cinfo);
105
106 /* Ignore any extra scanlines at bottom of image. */
107 rows_left = cinfo->image_height - cinfo->next_scanline;
108 if (num_lines > rows_left)
109 num_lines = rows_left;
110
111 row_ctr = 0;
112 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
113 cinfo->next_scanline += row_ctr;
114 return row_ctr;
115}
116
117
118/*
119 * Alternate entry point to write raw data.
120 * Processes exactly one iMCU row per call, unless suspended.
121 */
122
123GLOBAL(JDIMENSION)
124jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
125 JDIMENSION num_lines)
126{
127 JDIMENSION lines_per_iMCU_row;
128
129 if (cinfo->global_state != CSTATE_RAW_OK)
130 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
131 if (cinfo->next_scanline >= cinfo->image_height) {
132 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
133 return 0;
134 }
135
136 /* Call progress monitor hook if present */
137 if (cinfo->progress != NULL) {
138 cinfo->progress->pass_counter = (long) cinfo->next_scanline;
139 cinfo->progress->pass_limit = (long) cinfo->image_height;
140 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
141 }
142
143 /* Give master control module another chance if this is first call to
144 * jpeg_write_raw_data. This lets output of the frame/scan headers be
145 * delayed so that application can write COM, etc, markers between
146 * jpeg_start_compress and jpeg_write_raw_data.
147 */
148 if (cinfo->master->call_pass_startup)
149 (*cinfo->master->pass_startup) (cinfo);
150
151 /* Verify that at least one iMCU row has been passed. */
152 lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
153 if (num_lines < lines_per_iMCU_row)
154 ERREXIT(cinfo, JERR_BUFFER_SIZE);
155
156 /* Directly compress the row. */
157 if (! (*cinfo->coef->compress_data) (cinfo, data)) {
158 /* If compressor did not consume the whole row, suspend processing. */
159 return 0;
160 }
161
162 /* OK, we processed one iMCU row. */
163 cinfo->next_scanline += lines_per_iMCU_row;
164 return lines_per_iMCU_row;
165}
166