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