jdk
165 строк · 5.8 Кб
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_INTERNALS22#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{
44if (cinfo->global_state != CSTATE_START)45ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);46
47if (write_all_tables)48jpeg_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 */54jinit_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_scanlines58* or jpeg_write_raw_data.
59*/
60cinfo->next_scanline = 0;61cinfo->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,82JDIMENSION num_lines)83{
84JDIMENSION row_ctr, rows_left;85
86if (cinfo->global_state != CSTATE_SCANNING)87ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);88if (cinfo->next_scanline >= cinfo->image_height)89WARNMS(cinfo, JWRN_TOO_MUCH_DATA);90
91/* Call progress monitor hook if present */92if (cinfo->progress != NULL) {93cinfo->progress->pass_counter = (long) cinfo->next_scanline;94cinfo->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 to99* 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*/
103if (cinfo->master->call_pass_startup)104(*cinfo->master->pass_startup) (cinfo);105
106/* Ignore any extra scanlines at bottom of image. */107rows_left = cinfo->image_height - cinfo->next_scanline;108if (num_lines > rows_left)109num_lines = rows_left;110
111row_ctr = 0;112(*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);113cinfo->next_scanline += row_ctr;114return 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,125JDIMENSION num_lines)126{
127JDIMENSION lines_per_iMCU_row;128
129if (cinfo->global_state != CSTATE_RAW_OK)130ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);131if (cinfo->next_scanline >= cinfo->image_height) {132WARNMS(cinfo, JWRN_TOO_MUCH_DATA);133return 0;134}135
136/* Call progress monitor hook if present */137if (cinfo->progress != NULL) {138cinfo->progress->pass_counter = (long) cinfo->next_scanline;139cinfo->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 to144* 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*/
148if (cinfo->master->call_pass_startup)149(*cinfo->master->pass_startup) (cinfo);150
151/* Verify that at least one iMCU row has been passed. */152lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;153if (num_lines < lines_per_iMCU_row)154ERREXIT(cinfo, JERR_BUFFER_SIZE);155
156/* Directly compress the row. */157if (! (*cinfo->coef->compress_data) (cinfo, data)) {158/* If compressor did not consume the whole row, suspend processing. */159return 0;160}161
162/* OK, we processed one iMCU row. */163cinfo->next_scanline += lines_per_iMCU_row;164return lines_per_iMCU_row;165}
166