jcinit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * jcinit.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2003-2017 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 initialization logic for the JPEG compressor.
  10. * This routine is in charge of selecting the modules to be executed and
  11. * making an initialization call to each one.
  12. *
  13. * Logically, this code belongs in jcmaster.c. It's split out because
  14. * linking this routine implies linking the entire compression library.
  15. * For a transcoding-only application, we want to be able to use jcmaster.c
  16. * without linking in the whole library.
  17. */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. /*
  22. * Compute JPEG image dimensions and related values.
  23. * NOTE: this is exported for possible use by application.
  24. * Hence it mustn't do anything that can't be done twice.
  25. */
  26. GLOBAL(void)
  27. jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
  28. /* Do computations that are needed before master selection phase */
  29. {
  30. /* Sanity check on input image dimensions to prevent overflow in
  31. * following calculations.
  32. * We do check jpeg_width and jpeg_height in initial_setup in jcmaster.c,
  33. * but image_width and image_height can come from arbitrary data,
  34. * and we need some space for multiplication by block_size.
  35. */
  36. if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
  37. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  38. #ifdef DCT_SCALING_SUPPORTED
  39. /* Compute actual JPEG image dimensions and DCT scaling choices. */
  40. if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
  41. /* Provide block_size/1 scaling */
  42. cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
  43. cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
  44. cinfo->min_DCT_h_scaled_size = 1;
  45. cinfo->min_DCT_v_scaled_size = 1;
  46. } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
  47. /* Provide block_size/2 scaling */
  48. cinfo->jpeg_width = (JDIMENSION)
  49. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
  50. cinfo->jpeg_height = (JDIMENSION)
  51. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
  52. cinfo->min_DCT_h_scaled_size = 2;
  53. cinfo->min_DCT_v_scaled_size = 2;
  54. } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
  55. /* Provide block_size/3 scaling */
  56. cinfo->jpeg_width = (JDIMENSION)
  57. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
  58. cinfo->jpeg_height = (JDIMENSION)
  59. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
  60. cinfo->min_DCT_h_scaled_size = 3;
  61. cinfo->min_DCT_v_scaled_size = 3;
  62. } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
  63. /* Provide block_size/4 scaling */
  64. cinfo->jpeg_width = (JDIMENSION)
  65. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
  66. cinfo->jpeg_height = (JDIMENSION)
  67. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
  68. cinfo->min_DCT_h_scaled_size = 4;
  69. cinfo->min_DCT_v_scaled_size = 4;
  70. } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
  71. /* Provide block_size/5 scaling */
  72. cinfo->jpeg_width = (JDIMENSION)
  73. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
  74. cinfo->jpeg_height = (JDIMENSION)
  75. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
  76. cinfo->min_DCT_h_scaled_size = 5;
  77. cinfo->min_DCT_v_scaled_size = 5;
  78. } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
  79. /* Provide block_size/6 scaling */
  80. cinfo->jpeg_width = (JDIMENSION)
  81. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
  82. cinfo->jpeg_height = (JDIMENSION)
  83. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
  84. cinfo->min_DCT_h_scaled_size = 6;
  85. cinfo->min_DCT_v_scaled_size = 6;
  86. } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
  87. /* Provide block_size/7 scaling */
  88. cinfo->jpeg_width = (JDIMENSION)
  89. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
  90. cinfo->jpeg_height = (JDIMENSION)
  91. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
  92. cinfo->min_DCT_h_scaled_size = 7;
  93. cinfo->min_DCT_v_scaled_size = 7;
  94. } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
  95. /* Provide block_size/8 scaling */
  96. cinfo->jpeg_width = (JDIMENSION)
  97. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
  98. cinfo->jpeg_height = (JDIMENSION)
  99. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
  100. cinfo->min_DCT_h_scaled_size = 8;
  101. cinfo->min_DCT_v_scaled_size = 8;
  102. } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
  103. /* Provide block_size/9 scaling */
  104. cinfo->jpeg_width = (JDIMENSION)
  105. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
  106. cinfo->jpeg_height = (JDIMENSION)
  107. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
  108. cinfo->min_DCT_h_scaled_size = 9;
  109. cinfo->min_DCT_v_scaled_size = 9;
  110. } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
  111. /* Provide block_size/10 scaling */
  112. cinfo->jpeg_width = (JDIMENSION)
  113. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
  114. cinfo->jpeg_height = (JDIMENSION)
  115. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
  116. cinfo->min_DCT_h_scaled_size = 10;
  117. cinfo->min_DCT_v_scaled_size = 10;
  118. } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
  119. /* Provide block_size/11 scaling */
  120. cinfo->jpeg_width = (JDIMENSION)
  121. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
  122. cinfo->jpeg_height = (JDIMENSION)
  123. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
  124. cinfo->min_DCT_h_scaled_size = 11;
  125. cinfo->min_DCT_v_scaled_size = 11;
  126. } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
  127. /* Provide block_size/12 scaling */
  128. cinfo->jpeg_width = (JDIMENSION)
  129. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
  130. cinfo->jpeg_height = (JDIMENSION)
  131. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
  132. cinfo->min_DCT_h_scaled_size = 12;
  133. cinfo->min_DCT_v_scaled_size = 12;
  134. } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
  135. /* Provide block_size/13 scaling */
  136. cinfo->jpeg_width = (JDIMENSION)
  137. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
  138. cinfo->jpeg_height = (JDIMENSION)
  139. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
  140. cinfo->min_DCT_h_scaled_size = 13;
  141. cinfo->min_DCT_v_scaled_size = 13;
  142. } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
  143. /* Provide block_size/14 scaling */
  144. cinfo->jpeg_width = (JDIMENSION)
  145. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
  146. cinfo->jpeg_height = (JDIMENSION)
  147. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
  148. cinfo->min_DCT_h_scaled_size = 14;
  149. cinfo->min_DCT_v_scaled_size = 14;
  150. } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
  151. /* Provide block_size/15 scaling */
  152. cinfo->jpeg_width = (JDIMENSION)
  153. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
  154. cinfo->jpeg_height = (JDIMENSION)
  155. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
  156. cinfo->min_DCT_h_scaled_size = 15;
  157. cinfo->min_DCT_v_scaled_size = 15;
  158. } else {
  159. /* Provide block_size/16 scaling */
  160. cinfo->jpeg_width = (JDIMENSION)
  161. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
  162. cinfo->jpeg_height = (JDIMENSION)
  163. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
  164. cinfo->min_DCT_h_scaled_size = 16;
  165. cinfo->min_DCT_v_scaled_size = 16;
  166. }
  167. #else /* !DCT_SCALING_SUPPORTED */
  168. /* Hardwire it to "no scaling" */
  169. cinfo->jpeg_width = cinfo->image_width;
  170. cinfo->jpeg_height = cinfo->image_height;
  171. cinfo->min_DCT_h_scaled_size = DCTSIZE;
  172. cinfo->min_DCT_v_scaled_size = DCTSIZE;
  173. #endif /* DCT_SCALING_SUPPORTED */
  174. }
  175. /*
  176. * Master selection of compression modules.
  177. * This is done once at the start of processing an image. We determine
  178. * which modules will be used and give them appropriate initialization calls.
  179. */
  180. GLOBAL(void)
  181. jinit_compress_master (j_compress_ptr cinfo)
  182. {
  183. long samplesperrow;
  184. JDIMENSION jd_samplesperrow;
  185. /* For now, precision must match compiled-in value... */
  186. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  187. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  188. /* Sanity check on input image dimensions */
  189. if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
  190. cinfo->input_components <= 0)
  191. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  192. /* Width of an input scanline must be representable as JDIMENSION. */
  193. samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  194. jd_samplesperrow = (JDIMENSION) samplesperrow;
  195. if ((long) jd_samplesperrow != samplesperrow)
  196. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  197. /* Compute JPEG image dimensions and related values. */
  198. jpeg_calc_jpeg_dimensions(cinfo);
  199. /* Initialize master control (includes parameter checking/processing) */
  200. jinit_c_master_control(cinfo, FALSE /* full compression */);
  201. /* Preprocessing */
  202. if (! cinfo->raw_data_in) {
  203. jinit_color_converter(cinfo);
  204. jinit_downsampler(cinfo);
  205. jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
  206. }
  207. /* Forward DCT */
  208. jinit_forward_dct(cinfo);
  209. /* Entropy encoding: either Huffman or arithmetic coding. */
  210. if (cinfo->arith_code)
  211. jinit_arith_encoder(cinfo);
  212. else {
  213. jinit_huff_encoder(cinfo);
  214. }
  215. /* Need a full-image coefficient buffer in any multi-pass mode. */
  216. jinit_c_coef_controller(cinfo,
  217. (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
  218. jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
  219. jinit_marker_writer(cinfo);
  220. /* We can now tell the memory manager to allocate virtual arrays. */
  221. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  222. /* Write the datastream header (SOI) immediately.
  223. * Frame and scan headers are postponed till later.
  224. * This lets application insert special markers after the SOI.
  225. */
  226. (*cinfo->marker->write_file_header) (cinfo);
  227. }