jcmainct.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * jcmainct.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1996, Thomas G. Lane.
  6. * It was modified by The libjpeg-turbo Project to include only code relevant
  7. * to libjpeg-turbo.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file contains the main buffer controller for compression.
  12. * The main buffer lies between the pre-processor and the JPEG
  13. * compressor proper; it holds downsampled data in the JPEG colorspace.
  14. */
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. /* Private buffer controller object */
  19. typedef struct {
  20. struct jpeg_c_main_controller pub; /* public fields */
  21. JDIMENSION cur_iMCU_row; /* number of current iMCU row */
  22. JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
  23. boolean suspended; /* remember if we suspended output */
  24. J_BUF_MODE pass_mode; /* current operating mode */
  25. /* If using just a strip buffer, this points to the entire set of buffers
  26. * (we allocate one for each component). In the full-image case, this
  27. * points to the currently accessible strips of the virtual arrays.
  28. */
  29. JSAMPARRAY buffer[MAX_COMPONENTS];
  30. } my_main_controller;
  31. typedef my_main_controller *my_main_ptr;
  32. /* Forward declarations */
  33. METHODDEF(void) process_data_simple_main(j_compress_ptr cinfo,
  34. JSAMPARRAY input_buf,
  35. JDIMENSION *in_row_ctr,
  36. JDIMENSION in_rows_avail);
  37. /*
  38. * Initialize for a processing pass.
  39. */
  40. METHODDEF(void)
  41. start_pass_main(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  42. {
  43. my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
  44. /* Do nothing in raw-data mode. */
  45. if (cinfo->raw_data_in)
  46. return;
  47. if (pass_mode != JBUF_PASS_THRU)
  48. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  49. main_ptr->cur_iMCU_row = 0; /* initialize counters */
  50. main_ptr->rowgroup_ctr = 0;
  51. main_ptr->suspended = FALSE;
  52. main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */
  53. main_ptr->pub.process_data = process_data_simple_main;
  54. }
  55. /*
  56. * Process some data.
  57. * This routine handles the simple pass-through mode,
  58. * where we have only a strip buffer.
  59. */
  60. METHODDEF(void)
  61. process_data_simple_main(j_compress_ptr cinfo, JSAMPARRAY input_buf,
  62. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)
  63. {
  64. my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
  65. while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
  66. /* Read input data if we haven't filled the main buffer yet */
  67. if (main_ptr->rowgroup_ctr < DCTSIZE)
  68. (*cinfo->prep->pre_process_data) (cinfo, input_buf, in_row_ctr,
  69. in_rows_avail, main_ptr->buffer,
  70. &main_ptr->rowgroup_ctr,
  71. (JDIMENSION)DCTSIZE);
  72. /* If we don't have a full iMCU row buffered, return to application for
  73. * more data. Note that preprocessor will always pad to fill the iMCU row
  74. * at the bottom of the image.
  75. */
  76. if (main_ptr->rowgroup_ctr != DCTSIZE)
  77. return;
  78. /* Send the completed row to the compressor */
  79. if (!(*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
  80. /* If compressor did not consume the whole row, then we must need to
  81. * suspend processing and return to the application. In this situation
  82. * we pretend we didn't yet consume the last input row; otherwise, if
  83. * it happened to be the last row of the image, the application would
  84. * think we were done.
  85. */
  86. if (!main_ptr->suspended) {
  87. (*in_row_ctr)--;
  88. main_ptr->suspended = TRUE;
  89. }
  90. return;
  91. }
  92. /* We did finish the row. Undo our little suspension hack if a previous
  93. * call suspended; then mark the main buffer empty.
  94. */
  95. if (main_ptr->suspended) {
  96. (*in_row_ctr)++;
  97. main_ptr->suspended = FALSE;
  98. }
  99. main_ptr->rowgroup_ctr = 0;
  100. main_ptr->cur_iMCU_row++;
  101. }
  102. }
  103. /*
  104. * Initialize main buffer controller.
  105. */
  106. GLOBAL(void)
  107. jinit_c_main_controller(j_compress_ptr cinfo, boolean need_full_buffer)
  108. {
  109. my_main_ptr main_ptr;
  110. int ci;
  111. jpeg_component_info *compptr;
  112. main_ptr = (my_main_ptr)
  113. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  114. sizeof(my_main_controller));
  115. cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
  116. main_ptr->pub.start_pass = start_pass_main;
  117. /* We don't need to create a buffer in raw-data mode. */
  118. if (cinfo->raw_data_in)
  119. return;
  120. /* Create the buffer. It holds downsampled data, so each component
  121. * may be of a different size.
  122. */
  123. if (need_full_buffer) {
  124. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  125. } else {
  126. /* Allocate a strip buffer for each component */
  127. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  128. ci++, compptr++) {
  129. main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
  130. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  131. compptr->width_in_blocks * DCTSIZE,
  132. (JDIMENSION)(compptr->v_samp_factor * DCTSIZE));
  133. }
  134. }
  135. }