jpeg_memory_dest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "jpeg_memory_dest.h"
  2. /* ------------------------------------------------------------- */
  3. /* MEMORY DESTINATION INTERFACE METHODS */
  4. /* ------------------------------------------------------------- */
  5. /* This function is called by the library before any data gets written */
  6. METHODDEF(void)
  7. init_destination (j_compress_ptr cinfo)
  8. {
  9. mem_dest_ptr dest = (mem_dest_ptr)cinfo->dest;
  10. dest->pub.next_output_byte = dest->buffer; /* set destination buffer */
  11. dest->pub.free_in_buffer = dest->bufsize; /* input buffer size */
  12. dest->datasize = 0; /* reset output size */
  13. dest->errcount = 0; /* reset error count */
  14. }
  15. /* This function is called by the library if the buffer fills up
  16. I just reset destination pointer and buffer size here.
  17. Note that this behavior, while preventing seg faults
  18. will lead to invalid output streams as data is over-
  19. written.
  20. */
  21. METHODDEF(boolean)
  22. empty_output_buffer (j_compress_ptr cinfo)
  23. {
  24. mem_dest_ptr dest = (mem_dest_ptr)cinfo->dest;
  25. dest->pub.next_output_byte = dest->buffer;
  26. dest->pub.free_in_buffer = dest->bufsize;
  27. ++dest->errcount; /* need to increase error count */
  28. return TRUE;
  29. }
  30. /* Usually the library wants to flush output here.
  31. I will calculate output buffer size here.
  32. Note that results become incorrect, once
  33. empty_output_buffer was called.
  34. This situation is notified by errcount.
  35. */
  36. METHODDEF(void)
  37. term_destination (j_compress_ptr cinfo)
  38. {
  39. mem_dest_ptr dest = (mem_dest_ptr)cinfo->dest;
  40. dest->datasize = dest->bufsize - dest->pub.free_in_buffer;
  41. if (dest->outsize) *dest->outsize += (int)dest->datasize;
  42. }
  43. /* Override the default destination manager initialization
  44. provided by jpeglib. Since we want to use memory-to-memory
  45. compression, we need to use our own destination manager.
  46. */
  47. GLOBAL(void)
  48. jpeg_memory_dest (j_compress_ptr cinfo, JOCTET* buffer, int bufsize, int* outsize)
  49. {
  50. mem_dest_ptr dest;
  51. /* first call for this instance - need to setup */
  52. if (cinfo->dest == 0) {
  53. cinfo->dest = (struct jpeg_destination_mgr *)
  54. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  55. sizeof (memory_destination_mgr));
  56. }
  57. dest = (mem_dest_ptr) cinfo->dest;
  58. dest->bufsize = bufsize;
  59. dest->buffer = buffer;
  60. dest->outsize = outsize;
  61. /* set method callbacks */
  62. dest->pub.init_destination = init_destination;
  63. dest->pub.empty_output_buffer = empty_output_buffer;
  64. dest->pub.term_destination = term_destination;
  65. }
  66. /* ------------------------------------------------------------- */
  67. /* MEMORY SOURCE INTERFACE METHODS */
  68. /* ------------------------------------------------------------- */
  69. /* Called before data is read */
  70. METHODDEF(void)
  71. init_source (j_decompress_ptr dinfo)
  72. {
  73. /* nothing to do here, really. I mean. I'm not lazy or something, but...
  74. we're actually through here. */
  75. }
  76. /* Called if the decoder wants some bytes that we cannot provide... */
  77. METHODDEF(boolean)
  78. fill_input_buffer (j_decompress_ptr dinfo)
  79. {
  80. /* we can't do anything about this. This might happen if the provided
  81. buffer is either invalid with regards to its content or just a to
  82. small bufsize has been given. */
  83. /* fail. */
  84. return FALSE;
  85. }
  86. /* From IJG docs: "it's not clear that being smart is worth much trouble"
  87. So I save myself some trouble by ignoring this bit.
  88. */
  89. METHODDEF(void)
  90. skip_input_data (j_decompress_ptr dinfo, INT32 num_bytes)
  91. {
  92. /* There might be more data to skip than available in buffer.
  93. This clearly is an error, so screw this mess. */
  94. if ((size_t)num_bytes > dinfo->src->bytes_in_buffer) {
  95. dinfo->src->next_input_byte = 0; /* no buffer byte */
  96. dinfo->src->bytes_in_buffer = 0; /* no input left */
  97. } else {
  98. dinfo->src->next_input_byte += num_bytes;
  99. dinfo->src->bytes_in_buffer -= num_bytes;
  100. }
  101. }
  102. /* Finished with decompression */
  103. METHODDEF(void)
  104. term_source (j_decompress_ptr dinfo)
  105. {
  106. /* Again. Absolute laziness. Nothing to do here. Boring. */
  107. }
  108. GLOBAL(void)
  109. jpeg_memory_src (j_decompress_ptr dinfo, unsigned char* buffer, size_t size)
  110. {
  111. struct jpeg_source_mgr* src;
  112. /* first call for this instance - need to setup */
  113. if (dinfo->src == 0) {
  114. dinfo->src = (struct jpeg_source_mgr *)
  115. (*dinfo->mem->alloc_small) ((j_common_ptr) dinfo, JPOOL_PERMANENT,
  116. sizeof (struct jpeg_source_mgr));
  117. }
  118. src = dinfo->src;
  119. src->next_input_byte = buffer;
  120. src->bytes_in_buffer = size;
  121. src->init_source = init_source;
  122. src->fill_input_buffer = fill_input_buffer;
  123. src->skip_input_data = skip_input_data;
  124. src->term_source = term_source;
  125. /* IJG recommend to use their function - as I don't know ****
  126. about how to do better, I follow this recommendation */
  127. src->resync_to_restart = jpeg_resync_to_restart;
  128. }
粤ICP备19079148号