compressed-buffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. /* This code needs to be kept out of stm_buffer.c to avoid it being
  24. * pulled into cmapdump.c */
  25. fz_compressed_buffer *
  26. fz_keep_compressed_buffer(fz_context *ctx, fz_compressed_buffer *cbuf)
  27. {
  28. return (fz_compressed_buffer *)fz_keep_imp(ctx, cbuf, &cbuf->refs);
  29. }
  30. void
  31. fz_drop_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf)
  32. {
  33. if (fz_drop_imp(ctx, buf, &buf->refs))
  34. {
  35. if (buf->params.type == FZ_IMAGE_JBIG2)
  36. fz_drop_jbig2_globals(ctx, buf->params.u.jbig2.globals);
  37. fz_drop_buffer(ctx, buf->buffer);
  38. fz_free(ctx, buf);
  39. }
  40. }
  41. fz_compressed_buffer *
  42. fz_new_compressed_buffer(fz_context *ctx)
  43. {
  44. fz_compressed_buffer *cbuf = fz_malloc_struct(ctx, fz_compressed_buffer);
  45. cbuf->refs = 1;
  46. return cbuf;
  47. }
  48. fz_stream *
  49. fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *buffer, int *l2factor)
  50. {
  51. fz_stream *head, *tail;
  52. tail = fz_open_buffer(ctx, buffer->buffer);
  53. fz_try(ctx)
  54. head = fz_open_image_decomp_stream(ctx, tail, &buffer->params, l2factor);
  55. fz_always(ctx)
  56. fz_drop_stream(ctx, tail);
  57. fz_catch(ctx)
  58. fz_rethrow(ctx);
  59. return head;
  60. }
  61. fz_stream *
  62. fz_open_image_decomp_stream(fz_context *ctx, fz_stream *tail, fz_compression_params *params, int *l2factor)
  63. {
  64. fz_stream *head = NULL, *body = NULL;
  65. int our_l2factor = 0;
  66. fz_var(body);
  67. fz_try(ctx)
  68. {
  69. switch (params->type)
  70. {
  71. default:
  72. head = fz_keep_stream(ctx, tail);
  73. break;
  74. case FZ_IMAGE_FAX:
  75. head = fz_open_faxd(ctx, tail,
  76. params->u.fax.k,
  77. params->u.fax.end_of_line,
  78. params->u.fax.encoded_byte_align,
  79. params->u.fax.columns,
  80. params->u.fax.rows,
  81. params->u.fax.end_of_block,
  82. params->u.fax.black_is_1);
  83. break;
  84. case FZ_IMAGE_JPEG:
  85. if (l2factor)
  86. {
  87. our_l2factor = *l2factor;
  88. if (our_l2factor > 3)
  89. our_l2factor = 3;
  90. *l2factor -= our_l2factor;
  91. }
  92. head = fz_open_dctd(ctx, tail, params->u.jpeg.color_transform, params->u.jpeg.invert_cmyk, our_l2factor, NULL);
  93. break;
  94. case FZ_IMAGE_JBIG2:
  95. head = fz_open_jbig2d(ctx, tail, params->u.jbig2.globals, params->u.jbig2.embedded);
  96. break;
  97. case FZ_IMAGE_RLD:
  98. head = fz_open_rld(ctx, tail);
  99. break;
  100. case FZ_IMAGE_FLATE:
  101. head = fz_open_flated(ctx, tail, 15);
  102. if (params->u.flate.predictor > 1)
  103. {
  104. body = head;
  105. head = fz_open_predict(ctx, body,
  106. params->u.flate.predictor,
  107. params->u.flate.columns,
  108. params->u.flate.colors,
  109. params->u.flate.bpc);
  110. }
  111. break;
  112. case FZ_IMAGE_BROTLI:
  113. head = fz_open_brotlid(ctx, tail);
  114. if (params->u.brotli.predictor > 1)
  115. {
  116. body = head;
  117. head = fz_open_predict(ctx, body,
  118. params->u.brotli.predictor,
  119. params->u.brotli.columns,
  120. params->u.brotli.colors,
  121. params->u.brotli.bpc);
  122. }
  123. break;
  124. case FZ_IMAGE_LZW:
  125. head = fz_open_lzwd(ctx, tail, params->u.lzw.early_change, 9, 0, 0);
  126. if (params->u.flate.predictor > 1)
  127. {
  128. body = head;
  129. head = fz_open_predict(ctx, body,
  130. params->u.lzw.predictor,
  131. params->u.lzw.columns,
  132. params->u.lzw.colors,
  133. params->u.lzw.bpc);
  134. }
  135. break;
  136. }
  137. }
  138. fz_always(ctx)
  139. fz_drop_stream(ctx, body);
  140. fz_catch(ctx)
  141. fz_rethrow(ctx);
  142. return head;
  143. }
  144. fz_stream *
  145. fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buffer)
  146. {
  147. return fz_open_image_decomp_stream_from_buffer(ctx, buffer, NULL);
  148. }
  149. size_t
  150. fz_compressed_buffer_size(fz_compressed_buffer *buffer)
  151. {
  152. if (!buffer)
  153. return 0;
  154. if (buffer->buffer)
  155. return (size_t)buffer->buffer->cap + sizeof(*buffer);
  156. return sizeof(*buffer);
  157. }