rdjpeg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * rdjpeg.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * mozjpeg Modifications:
  6. * Copyright (C) 2014, Mozilla Corporation.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. */
  11. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  12. #if JPEG_RAW_READER
  13. #define NUM_ROWS 32
  14. #endif
  15. /* Private version of data source object */
  16. typedef struct _jpeg_source_struct * jpeg_source_ptr;
  17. typedef struct _jpeg_source_struct {
  18. struct cjpeg_source_struct pub; /* public fields */
  19. j_compress_ptr cinfo; /* back link saves passing separate parm */
  20. struct jpeg_decompress_struct dinfo;
  21. struct jpeg_error_mgr jerr;
  22. } jpeg_source_struct;
  23. METHODDEF(JDIMENSION)
  24. get_rows (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  25. {
  26. jpeg_source_ptr source = (jpeg_source_ptr) sinfo;
  27. #if !JPEG_RAW_READER
  28. return jpeg_read_scanlines(&source->dinfo, source->pub.buffer, source->pub.buffer_height);
  29. #else
  30. jpeg_read_raw_data(&source->dinfo, source->pub.plane_pointer, 8*cinfo->max_v_samp_factor);
  31. return 8*cinfo->max_v_samp_factor;
  32. #endif
  33. }
  34. /*
  35. * Read the file header; return image size and component count.
  36. */
  37. METHODDEF(void)
  38. start_input_jpeg (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  39. {
  40. #if JPEG_RAW_READER
  41. int i;
  42. #endif
  43. int m;
  44. jpeg_source_ptr source = (jpeg_source_ptr) sinfo;
  45. source->dinfo.err = jpeg_std_error(&source->jerr);
  46. jpeg_create_decompress(&source->dinfo);
  47. jpeg_stdio_src(&source->dinfo, source->pub.input_file);
  48. jpeg_save_markers(&source->dinfo, JPEG_COM, 0xFFFF);
  49. for (m = 0; m < 16; m++)
  50. jpeg_save_markers(&source->dinfo, JPEG_APP0 + m, 0xFFFF);
  51. jpeg_read_header(&source->dinfo, TRUE);
  52. source->pub.marker_list = source->dinfo.marker_list;
  53. #if !JPEG_RAW_READER
  54. source->dinfo.raw_data_out = FALSE;
  55. jpeg_start_decompress(&source->dinfo);
  56. cinfo->in_color_space = source->dinfo.out_color_space;
  57. cinfo->input_components = source->dinfo.output_components;
  58. cinfo->data_precision = source->dinfo.data_precision;
  59. cinfo->image_width = source->dinfo.image_width;
  60. cinfo->image_height = source->dinfo.image_height;
  61. cinfo->raw_data_in = FALSE;
  62. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  63. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  64. (JDIMENSION) (cinfo->image_width * cinfo->input_components), (JDIMENSION) 1);
  65. source->pub.buffer_height = 1;
  66. #else
  67. source->dinfo.raw_data_out = TRUE;
  68. source->dinfo.do_fancy_upsampling = FALSE;
  69. jpeg_start_decompress(&source->dinfo);
  70. cinfo->in_color_space = source->dinfo.out_color_space;
  71. cinfo->input_components = source->dinfo.output_components;
  72. cinfo->data_precision = source->dinfo.data_precision;
  73. cinfo->image_width = source->dinfo.image_width;
  74. cinfo->image_height = source->dinfo.image_height;
  75. jpeg_set_colorspace(cinfo, source->dinfo.jpeg_color_space);
  76. cinfo->max_v_samp_factor = source->dinfo.max_v_samp_factor;
  77. cinfo->max_h_samp_factor = source->dinfo.max_h_samp_factor;
  78. cinfo->raw_data_in = TRUE;
  79. #if JPEG_LIB_VERSION >= 70
  80. cinfo->do_fancy_upsampling = FALSE;
  81. #endif
  82. for (i = 0; i < cinfo->input_components; i++) {
  83. cinfo->comp_info[i].h_samp_factor = source->dinfo.comp_info[i].h_samp_factor;
  84. cinfo->comp_info[i].v_samp_factor = source->dinfo.comp_info[i].v_samp_factor;
  85. source->pub.plane_pointer[i] = (*cinfo->mem->alloc_sarray)
  86. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  87. (JDIMENSION) cinfo->image_width, (JDIMENSION) NUM_ROWS);
  88. }
  89. #endif
  90. source->pub.get_pixel_rows = get_rows;
  91. }
  92. /*
  93. * Finish up at the end of the file.
  94. */
  95. METHODDEF(void)
  96. finish_input_jpeg (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  97. {
  98. jpeg_source_ptr source = (jpeg_source_ptr) sinfo;
  99. jpeg_finish_decompress(&source->dinfo);
  100. jpeg_destroy_decompress(&source->dinfo);
  101. }
  102. /*
  103. * The module selection routine for JPEG format input.
  104. */
  105. GLOBAL(cjpeg_source_ptr)
  106. jinit_read_jpeg (j_compress_ptr cinfo)
  107. {
  108. jpeg_source_ptr source;
  109. /* Create module interface object */
  110. source = (jpeg_source_ptr)
  111. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  112. sizeof(jpeg_source_struct));
  113. source->cinfo = cinfo; /* make back link for subroutines */
  114. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  115. source->pub.start_input = start_input_jpeg;
  116. source->pub.finish_input = finish_input_jpeg;
  117. return (cjpeg_source_ptr) source;
  118. }