jcinit.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * jcinit.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2003-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 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. * Master selection of compression modules.
  23. * This is done once at the start of processing an image. We determine
  24. * which modules will be used and give them appropriate initialization calls.
  25. */
  26. GLOBAL(void)
  27. jinit_compress_master (j_compress_ptr cinfo)
  28. {
  29. long samplesperrow;
  30. JDIMENSION jd_samplesperrow;
  31. /* For now, precision must match compiled-in value... */
  32. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  33. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  34. /* Sanity check on image dimensions */
  35. if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
  36. cinfo->input_components <= 0)
  37. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  38. /* Width of an input scanline must be representable as JDIMENSION. */
  39. samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  40. jd_samplesperrow = (JDIMENSION) samplesperrow;
  41. if ((long) jd_samplesperrow != samplesperrow)
  42. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  43. /* Initialize master control (includes parameter checking/processing) */
  44. jinit_c_master_control(cinfo, FALSE /* full compression */);
  45. /* Preprocessing */
  46. if (! cinfo->raw_data_in) {
  47. jinit_color_converter(cinfo);
  48. jinit_downsampler(cinfo);
  49. jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
  50. }
  51. /* Forward DCT */
  52. jinit_forward_dct(cinfo);
  53. /* Entropy encoding: either Huffman or arithmetic coding. */
  54. if (cinfo->arith_code)
  55. jinit_arith_encoder(cinfo);
  56. else {
  57. jinit_huff_encoder(cinfo);
  58. }
  59. /* Need a full-image coefficient buffer in any multi-pass mode. */
  60. jinit_c_coef_controller(cinfo,
  61. (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
  62. jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
  63. jinit_marker_writer(cinfo);
  64. /* We can now tell the memory manager to allocate virtual arrays. */
  65. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  66. /* Write the datastream header (SOI) immediately.
  67. * Frame and scan headers are postponed till later.
  68. * This lets application insert special markers after the SOI.
  69. */
  70. (*cinfo->marker->write_file_header) (cinfo);
  71. }