SkJpegUtility.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/codec/SkJpegUtility.h"
  8. #include "src/codec/SkCodecPriv.h"
  9. /*
  10. * Call longjmp to continue execution on an error
  11. */
  12. void skjpeg_err_exit(j_common_ptr dinfo) {
  13. // Simply return to Skia client code
  14. // JpegDecoderMgr will take care of freeing memory
  15. skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err;
  16. (*error->output_message) (dinfo);
  17. if (error->fJmpBufStack.empty()) {
  18. SK_ABORT("JPEG error with no jmp_buf set.");
  19. }
  20. longjmp(*error->fJmpBufStack.back(), 1);
  21. }
  22. // Functions for buffered sources //
  23. /*
  24. * Initialize the buffered source manager
  25. */
  26. static void sk_init_buffered_source(j_decompress_ptr dinfo) {
  27. skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
  28. src->next_input_byte = (const JOCTET*) src->fBuffer;
  29. src->bytes_in_buffer = 0;
  30. }
  31. /*
  32. * Fill the input buffer from the stream
  33. */
  34. static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) {
  35. skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
  36. size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
  37. // libjpeg is still happy with a less than full read, as long as the result is non-zero
  38. if (bytes == 0) {
  39. // Let libjpeg know that the buffer needs to be refilled
  40. src->next_input_byte = nullptr;
  41. src->bytes_in_buffer = 0;
  42. return false;
  43. }
  44. src->next_input_byte = (const JOCTET*) src->fBuffer;
  45. src->bytes_in_buffer = bytes;
  46. return true;
  47. }
  48. /*
  49. * Skip a certain number of bytes in the stream
  50. */
  51. static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
  52. skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
  53. size_t bytes = (size_t) numBytes;
  54. if (bytes > src->bytes_in_buffer) {
  55. size_t bytesToSkip = bytes - src->bytes_in_buffer;
  56. if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
  57. SkCodecPrintf("Failure to skip.\n");
  58. dinfo->err->error_exit((j_common_ptr) dinfo);
  59. return;
  60. }
  61. src->next_input_byte = (const JOCTET*) src->fBuffer;
  62. src->bytes_in_buffer = 0;
  63. } else {
  64. src->next_input_byte += numBytes;
  65. src->bytes_in_buffer -= numBytes;
  66. }
  67. }
  68. /*
  69. * We do not need to do anything to terminate our stream
  70. */
  71. static void sk_term_source(j_decompress_ptr dinfo)
  72. {
  73. // The current implementation of SkJpegCodec does not call
  74. // jpeg_finish_decompress(), so this function is never called.
  75. // If we want to modify this function to do something, we also
  76. // need to modify SkJpegCodec to call jpeg_finish_decompress().
  77. }
  78. // Functions for memory backed sources //
  79. /*
  80. * Initialize the mem backed source manager
  81. */
  82. static void sk_init_mem_source(j_decompress_ptr dinfo) {
  83. /* no work necessary here, all things are done in constructor */
  84. }
  85. static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
  86. jpeg_source_mgr* src = cinfo->src;
  87. size_t bytes = static_cast<size_t>(num_bytes);
  88. if(bytes > src->bytes_in_buffer) {
  89. src->next_input_byte = nullptr;
  90. src->bytes_in_buffer = 0;
  91. } else {
  92. src->next_input_byte += bytes;
  93. src->bytes_in_buffer -= bytes;
  94. }
  95. }
  96. static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
  97. /* The whole JPEG data is expected to reside in the supplied memory,
  98. * buffer, so any request for more data beyond the given buffer size
  99. * is treated as an error.
  100. */
  101. return false;
  102. }
  103. /*
  104. * Constructor for the source manager that we provide to libjpeg
  105. * We provide skia implementations of all of the stream processing functions required by libjpeg
  106. */
  107. skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
  108. : fStream(stream)
  109. {
  110. if (stream->hasLength() && stream->getMemoryBase()) {
  111. init_source = sk_init_mem_source;
  112. fill_input_buffer = sk_fill_mem_input_buffer;
  113. skip_input_data = sk_skip_mem_input_data;
  114. resync_to_restart = jpeg_resync_to_restart;
  115. term_source = sk_term_source;
  116. bytes_in_buffer = static_cast<size_t>(stream->getLength());
  117. next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
  118. } else {
  119. init_source = sk_init_buffered_source;
  120. fill_input_buffer = sk_fill_buffered_input_buffer;
  121. skip_input_data = sk_skip_buffered_input_data;
  122. resync_to_restart = jpeg_resync_to_restart;
  123. term_source = sk_term_source;
  124. }
  125. }