compressed-buffer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef MUPDF_FITZ_COMPRESSED_BUFFER_H
  23. #define MUPDF_FITZ_COMPRESSED_BUFFER_H
  24. #include "mupdf/fitz/system.h"
  25. #include "mupdf/fitz/context.h"
  26. #include "mupdf/fitz/buffer.h"
  27. #include "mupdf/fitz/stream.h"
  28. #include "mupdf/fitz/filter.h"
  29. /**
  30. Compression parameters used for buffers of compressed data;
  31. typically for the source data for images.
  32. */
  33. typedef struct
  34. {
  35. int type;
  36. union {
  37. struct {
  38. int color_transform; /* Use -1 for unset */
  39. int invert_cmyk; /* Use 1 for standalone JPEG files */
  40. } jpeg;
  41. struct {
  42. int smask_in_data;
  43. } jpx;
  44. struct {
  45. fz_jbig2_globals *globals;
  46. int embedded;
  47. } jbig2;
  48. struct {
  49. int columns;
  50. int rows;
  51. int k;
  52. int end_of_line;
  53. int encoded_byte_align;
  54. int end_of_block;
  55. int black_is_1;
  56. int damaged_rows_before_error;
  57. } fax;
  58. struct
  59. {
  60. int columns;
  61. int colors;
  62. int predictor;
  63. int bpc;
  64. }
  65. flate;
  66. struct
  67. {
  68. int columns;
  69. int colors;
  70. int predictor;
  71. int bpc;
  72. }
  73. brotli;
  74. struct
  75. {
  76. int columns;
  77. int colors;
  78. int predictor;
  79. int bpc;
  80. int early_change;
  81. } lzw;
  82. } u;
  83. } fz_compression_params;
  84. /**
  85. Buffers of compressed data; typically for the source data
  86. for images.
  87. */
  88. typedef struct
  89. {
  90. int refs;
  91. fz_compression_params params;
  92. fz_buffer *buffer;
  93. } fz_compressed_buffer;
  94. /**
  95. Take a reference to an fz_compressed_buffer.
  96. */
  97. fz_compressed_buffer *fz_keep_compressed_buffer(fz_context *ctx, fz_compressed_buffer *cbuf);
  98. /**
  99. Return the storage size used for a buffer and its data.
  100. Used in implementing store handling.
  101. Never throws exceptions.
  102. */
  103. size_t fz_compressed_buffer_size(fz_compressed_buffer *buffer);
  104. /**
  105. Open a stream to read the decompressed version of a buffer.
  106. */
  107. fz_stream *fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *);
  108. /**
  109. Open a stream to read the decompressed version of a buffer,
  110. with optional log2 subsampling.
  111. l2factor = NULL for no subsampling, or a pointer to an integer
  112. containing the maximum log2 subsample factor acceptable (0 =
  113. none, 1 = halve dimensions, 2 = quarter dimensions etc). If
  114. non-NULL, then *l2factor will be updated on exit with the actual
  115. log2 subsample factor achieved.
  116. */
  117. fz_stream *fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *, int *l2factor);
  118. /**
  119. Open a stream to read the decompressed version of another stream
  120. with optional log2 subsampling.
  121. */
  122. fz_stream *fz_open_image_decomp_stream(fz_context *ctx, fz_stream *, fz_compression_params *, int *l2factor);
  123. /**
  124. Recognise image format strings in the first 8 bytes from image
  125. data.
  126. */
  127. int fz_recognize_image_format(fz_context *ctx, unsigned char p[8]);
  128. /**
  129. Map from FZ_IMAGE_* value to string.
  130. The returned string is static and therefore must not be freed.
  131. */
  132. const char *fz_image_type_name(int type);
  133. /**
  134. Map from (case sensitive) image type string to FZ_IMAGE_*
  135. type value.
  136. */
  137. int fz_lookup_image_type(const char *type);
  138. enum
  139. {
  140. FZ_IMAGE_UNKNOWN = 0,
  141. /* Uncompressed samples */
  142. FZ_IMAGE_RAW,
  143. /* Compressed samples */
  144. FZ_IMAGE_FAX,
  145. FZ_IMAGE_FLATE,
  146. FZ_IMAGE_LZW,
  147. FZ_IMAGE_RLD,
  148. FZ_IMAGE_BROTLI,
  149. /* Full image formats */
  150. FZ_IMAGE_BMP,
  151. FZ_IMAGE_GIF,
  152. FZ_IMAGE_JBIG2,
  153. FZ_IMAGE_JPEG,
  154. FZ_IMAGE_JPX,
  155. FZ_IMAGE_JXR,
  156. FZ_IMAGE_PNG,
  157. FZ_IMAGE_PNM,
  158. FZ_IMAGE_TIFF,
  159. FZ_IMAGE_PSD,
  160. };
  161. /**
  162. Drop a reference to a compressed buffer. Destroys the buffer
  163. and frees any storage/other references held by it.
  164. Never throws exceptions.
  165. */
  166. void fz_drop_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf);
  167. /**
  168. Create a new, UNKNOWN format, compressed_buffer.
  169. */
  170. fz_compressed_buffer *fz_new_compressed_buffer(fz_context *ctx);
  171. #endif