OSDN Git Service

・#26997 DTXViewer023 のソースコード一式を追加。変更点は以下の通り。
[dtxmania/dtxmania.git] / @jpeglibソリューション / jpeg-8c / jctrans.c
1 /*\r
2  * jctrans.c\r
3  *\r
4  * Copyright (C) 1995-1998, Thomas G. Lane.\r
5  * Modified 2000-2009 by Guido Vollbeding.\r
6  * This file is part of the Independent JPEG Group's software.\r
7  * For conditions of distribution and use, see the accompanying README file.\r
8  *\r
9  * This file contains library routines for transcoding compression,\r
10  * that is, writing raw DCT coefficient arrays to an output JPEG file.\r
11  * The routines in jcapimin.c will also be needed by a transcoder.\r
12  */\r
13 \r
14 #define JPEG_INTERNALS\r
15 #include "jinclude.h"\r
16 #include "jpeglib.h"\r
17 \r
18 \r
19 /* Forward declarations */\r
20 LOCAL(void) transencode_master_selection\r
21         JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));\r
22 LOCAL(void) transencode_coef_controller\r
23         JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));\r
24 \r
25 \r
26 /*\r
27  * Compression initialization for writing raw-coefficient data.\r
28  * Before calling this, all parameters and a data destination must be set up.\r
29  * Call jpeg_finish_compress() to actually write the data.\r
30  *\r
31  * The number of passed virtual arrays must match cinfo->num_components.\r
32  * Note that the virtual arrays need not be filled or even realized at\r
33  * the time write_coefficients is called; indeed, if the virtual arrays\r
34  * were requested from this compression object's memory manager, they\r
35  * typically will be realized during this routine and filled afterwards.\r
36  */\r
37 \r
38 GLOBAL(void)\r
39 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)\r
40 {\r
41   if (cinfo->global_state != CSTATE_START)\r
42     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
43   /* Mark all tables to be written */\r
44   jpeg_suppress_tables(cinfo, FALSE);\r
45   /* (Re)initialize error mgr and destination modules */\r
46   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);\r
47   (*cinfo->dest->init_destination) (cinfo);\r
48   /* Perform master selection of active modules */\r
49   transencode_master_selection(cinfo, coef_arrays);\r
50   /* Wait for jpeg_finish_compress() call */\r
51   cinfo->next_scanline = 0;     /* so jpeg_write_marker works */\r
52   cinfo->global_state = CSTATE_WRCOEFS;\r
53 }\r
54 \r
55 \r
56 /*\r
57  * Initialize the compression object with default parameters,\r
58  * then copy from the source object all parameters needed for lossless\r
59  * transcoding.  Parameters that can be varied without loss (such as\r
60  * scan script and Huffman optimization) are left in their default states.\r
61  */\r
62 \r
63 GLOBAL(void)\r
64 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,\r
65                                j_compress_ptr dstinfo)\r
66 {\r
67   JQUANT_TBL ** qtblptr;\r
68   jpeg_component_info *incomp, *outcomp;\r
69   JQUANT_TBL *c_quant, *slot_quant;\r
70   int tblno, ci, coefi;\r
71 \r
72   /* Safety check to ensure start_compress not called yet. */\r
73   if (dstinfo->global_state != CSTATE_START)\r
74     ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);\r
75   /* Copy fundamental image dimensions */\r
76   dstinfo->image_width = srcinfo->image_width;\r
77   dstinfo->image_height = srcinfo->image_height;\r
78   dstinfo->input_components = srcinfo->num_components;\r
79   dstinfo->in_color_space = srcinfo->jpeg_color_space;\r
80   dstinfo->jpeg_width = srcinfo->output_width;\r
81   dstinfo->jpeg_height = srcinfo->output_height;\r
82   dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size;\r
83   dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size;\r
84   /* Initialize all parameters to default values */\r
85   jpeg_set_defaults(dstinfo);\r
86   /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.\r
87    * Fix it to get the right header markers for the image colorspace.\r
88    */\r
89   jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);\r
90   dstinfo->data_precision = srcinfo->data_precision;\r
91   dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;\r
92   /* Copy the source's quantization tables. */\r
93   for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {\r
94     if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {\r
95       qtblptr = & dstinfo->quant_tbl_ptrs[tblno];\r
96       if (*qtblptr == NULL)\r
97         *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);\r
98       MEMCOPY((*qtblptr)->quantval,\r
99               srcinfo->quant_tbl_ptrs[tblno]->quantval,\r
100               SIZEOF((*qtblptr)->quantval));\r
101       (*qtblptr)->sent_table = FALSE;\r
102     }\r
103   }\r
104   /* Copy the source's per-component info.\r
105    * Note we assume jpeg_set_defaults has allocated the dest comp_info array.\r
106    */\r
107   dstinfo->num_components = srcinfo->num_components;\r
108   if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)\r
109     ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,\r
110              MAX_COMPONENTS);\r
111   for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;\r
112        ci < dstinfo->num_components; ci++, incomp++, outcomp++) {\r
113     outcomp->component_id = incomp->component_id;\r
114     outcomp->h_samp_factor = incomp->h_samp_factor;\r
115     outcomp->v_samp_factor = incomp->v_samp_factor;\r
116     outcomp->quant_tbl_no = incomp->quant_tbl_no;\r
117     /* Make sure saved quantization table for component matches the qtable\r
118      * slot.  If not, the input file re-used this qtable slot.\r
119      * IJG encoder currently cannot duplicate this.\r
120      */\r
121     tblno = outcomp->quant_tbl_no;\r
122     if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||\r
123         srcinfo->quant_tbl_ptrs[tblno] == NULL)\r
124       ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);\r
125     slot_quant = srcinfo->quant_tbl_ptrs[tblno];\r
126     c_quant = incomp->quant_table;\r
127     if (c_quant != NULL) {\r
128       for (coefi = 0; coefi < DCTSIZE2; coefi++) {\r
129         if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])\r
130           ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);\r
131       }\r
132     }\r
133     /* Note: we do not copy the source's Huffman table assignments;\r
134      * instead we rely on jpeg_set_colorspace to have made a suitable choice.\r
135      */\r
136   }\r
137   /* Also copy JFIF version and resolution information, if available.\r
138    * Strictly speaking this isn't "critical" info, but it's nearly\r
139    * always appropriate to copy it if available.  In particular,\r
140    * if the application chooses to copy JFIF 1.02 extension markers from\r
141    * the source file, we need to copy the version to make sure we don't\r
142    * emit a file that has 1.02 extensions but a claimed version of 1.01.\r
143    * We will *not*, however, copy version info from mislabeled "2.01" files.\r
144    */\r
145   if (srcinfo->saw_JFIF_marker) {\r
146     if (srcinfo->JFIF_major_version == 1) {\r
147       dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;\r
148       dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;\r
149     }\r
150     dstinfo->density_unit = srcinfo->density_unit;\r
151     dstinfo->X_density = srcinfo->X_density;\r
152     dstinfo->Y_density = srcinfo->Y_density;\r
153   }\r
154 }\r
155 \r
156 \r
157 /*\r
158  * Master selection of compression modules for transcoding.\r
159  * This substitutes for jcinit.c's initialization of the full compressor.\r
160  */\r
161 \r
162 LOCAL(void)\r
163 transencode_master_selection (j_compress_ptr cinfo,\r
164                               jvirt_barray_ptr * coef_arrays)\r
165 {\r
166   /* Initialize master control (includes parameter checking/processing) */\r
167   jinit_c_master_control(cinfo, TRUE /* transcode only */);\r
168 \r
169   /* Entropy encoding: either Huffman or arithmetic coding. */\r
170   if (cinfo->arith_code)\r
171     jinit_arith_encoder(cinfo);\r
172   else {\r
173     jinit_huff_encoder(cinfo);\r
174   }\r
175 \r
176   /* We need a special coefficient buffer controller. */\r
177   transencode_coef_controller(cinfo, coef_arrays);\r
178 \r
179   jinit_marker_writer(cinfo);\r
180 \r
181   /* We can now tell the memory manager to allocate virtual arrays. */\r
182   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);\r
183 \r
184   /* Write the datastream header (SOI, JFIF) immediately.\r
185    * Frame and scan headers are postponed till later.\r
186    * This lets application insert special markers after the SOI.\r
187    */\r
188   (*cinfo->marker->write_file_header) (cinfo);\r
189 }\r
190 \r
191 \r
192 /*\r
193  * The rest of this file is a special implementation of the coefficient\r
194  * buffer controller.  This is similar to jccoefct.c, but it handles only\r
195  * output from presupplied virtual arrays.  Furthermore, we generate any\r
196  * dummy padding blocks on-the-fly rather than expecting them to be present\r
197  * in the arrays.\r
198  */\r
199 \r
200 /* Private buffer controller object */\r
201 \r
202 typedef struct {\r
203   struct jpeg_c_coef_controller pub; /* public fields */\r
204 \r
205   JDIMENSION iMCU_row_num;      /* iMCU row # within image */\r
206   JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */\r
207   int MCU_vert_offset;          /* counts MCU rows within iMCU row */\r
208   int MCU_rows_per_iMCU_row;    /* number of such rows needed */\r
209 \r
210   /* Virtual block array for each component. */\r
211   jvirt_barray_ptr * whole_image;\r
212 \r
213   /* Workspace for constructing dummy blocks at right/bottom edges. */\r
214   JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];\r
215 } my_coef_controller;\r
216 \r
217 typedef my_coef_controller * my_coef_ptr;\r
218 \r
219 \r
220 LOCAL(void)\r
221 start_iMCU_row (j_compress_ptr cinfo)\r
222 /* Reset within-iMCU-row counters for a new row */\r
223 {\r
224   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
225 \r
226   /* In an interleaved scan, an MCU row is the same as an iMCU row.\r
227    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.\r
228    * But at the bottom of the image, process only what's left.\r
229    */\r
230   if (cinfo->comps_in_scan > 1) {\r
231     coef->MCU_rows_per_iMCU_row = 1;\r
232   } else {\r
233     if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))\r
234       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;\r
235     else\r
236       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;\r
237   }\r
238 \r
239   coef->mcu_ctr = 0;\r
240   coef->MCU_vert_offset = 0;\r
241 }\r
242 \r
243 \r
244 /*\r
245  * Initialize for a processing pass.\r
246  */\r
247 \r
248 METHODDEF(void)\r
249 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)\r
250 {\r
251   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
252 \r
253   if (pass_mode != JBUF_CRANK_DEST)\r
254     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);\r
255 \r
256   coef->iMCU_row_num = 0;\r
257   start_iMCU_row(cinfo);\r
258 }\r
259 \r
260 \r
261 /*\r
262  * Process some data.\r
263  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)\r
264  * per call, ie, v_samp_factor block rows for each component in the scan.\r
265  * The data is obtained from the virtual arrays and fed to the entropy coder.\r
266  * Returns TRUE if the iMCU row is completed, FALSE if suspended.\r
267  *\r
268  * NB: input_buf is ignored; it is likely to be a NULL pointer.\r
269  */\r
270 \r
271 METHODDEF(boolean)\r
272 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)\r
273 {\r
274   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
275   JDIMENSION MCU_col_num;       /* index of current MCU within row */\r
276   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;\r
277   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;\r
278   int blkn, ci, xindex, yindex, yoffset, blockcnt;\r
279   JDIMENSION start_col;\r
280   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];\r
281   JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];\r
282   JBLOCKROW buffer_ptr;\r
283   jpeg_component_info *compptr;\r
284 \r
285   /* Align the virtual buffers for the components used in this scan. */\r
286   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {\r
287     compptr = cinfo->cur_comp_info[ci];\r
288     buffer[ci] = (*cinfo->mem->access_virt_barray)\r
289       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],\r
290        coef->iMCU_row_num * compptr->v_samp_factor,\r
291        (JDIMENSION) compptr->v_samp_factor, FALSE);\r
292   }\r
293 \r
294   /* Loop to process one whole iMCU row */\r
295   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;\r
296        yoffset++) {\r
297     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;\r
298          MCU_col_num++) {\r
299       /* Construct list of pointers to DCT blocks belonging to this MCU */\r
300       blkn = 0;                 /* index of current DCT block within MCU */\r
301       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {\r
302         compptr = cinfo->cur_comp_info[ci];\r
303         start_col = MCU_col_num * compptr->MCU_width;\r
304         blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width\r
305                                                 : compptr->last_col_width;\r
306         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {\r
307           if (coef->iMCU_row_num < last_iMCU_row ||\r
308               yindex+yoffset < compptr->last_row_height) {\r
309             /* Fill in pointers to real blocks in this row */\r
310             buffer_ptr = buffer[ci][yindex+yoffset] + start_col;\r
311             for (xindex = 0; xindex < blockcnt; xindex++)\r
312               MCU_buffer[blkn++] = buffer_ptr++;\r
313           } else {\r
314             /* At bottom of image, need a whole row of dummy blocks */\r
315             xindex = 0;\r
316           }\r
317           /* Fill in any dummy blocks needed in this row.\r
318            * Dummy blocks are filled in the same way as in jccoefct.c:\r
319            * all zeroes in the AC entries, DC entries equal to previous\r
320            * block's DC value.  The init routine has already zeroed the\r
321            * AC entries, so we need only set the DC entries correctly.\r
322            */\r
323           for (; xindex < compptr->MCU_width; xindex++) {\r
324             MCU_buffer[blkn] = coef->dummy_buffer[blkn];\r
325             MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];\r
326             blkn++;\r
327           }\r
328         }\r
329       }\r
330       /* Try to write the MCU. */\r
331       if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {\r
332         /* Suspension forced; update state counters and exit */\r
333         coef->MCU_vert_offset = yoffset;\r
334         coef->mcu_ctr = MCU_col_num;\r
335         return FALSE;\r
336       }\r
337     }\r
338     /* Completed an MCU row, but perhaps not an iMCU row */\r
339     coef->mcu_ctr = 0;\r
340   }\r
341   /* Completed the iMCU row, advance counters for next one */\r
342   coef->iMCU_row_num++;\r
343   start_iMCU_row(cinfo);\r
344   return TRUE;\r
345 }\r
346 \r
347 \r
348 /*\r
349  * Initialize coefficient buffer controller.\r
350  *\r
351  * Each passed coefficient array must be the right size for that\r
352  * coefficient: width_in_blocks wide and height_in_blocks high,\r
353  * with unitheight at least v_samp_factor.\r
354  */\r
355 \r
356 LOCAL(void)\r
357 transencode_coef_controller (j_compress_ptr cinfo,\r
358                              jvirt_barray_ptr * coef_arrays)\r
359 {\r
360   my_coef_ptr coef;\r
361   JBLOCKROW buffer;\r
362   int i;\r
363 \r
364   coef = (my_coef_ptr)\r
365     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
366                                 SIZEOF(my_coef_controller));\r
367   cinfo->coef = (struct jpeg_c_coef_controller *) coef;\r
368   coef->pub.start_pass = start_pass_coef;\r
369   coef->pub.compress_data = compress_output;\r
370 \r
371   /* Save pointer to virtual arrays */\r
372   coef->whole_image = coef_arrays;\r
373 \r
374   /* Allocate and pre-zero space for dummy DCT blocks. */\r
375   buffer = (JBLOCKROW)\r
376     (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
377                                 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));\r
378   jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));\r
379   for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {\r
380     coef->dummy_buffer[i] = buffer + i;\r
381   }\r
382 }\r