image.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #ifndef MUPDF_FITZ_IMAGE_H
  2. #define MUPDF_FITZ_IMAGE_H
  3. #include "mupdf/fitz/system.h"
  4. #include "mupdf/fitz/context.h"
  5. #include "mupdf/fitz/store.h"
  6. #include "mupdf/fitz/colorspace.h"
  7. #include "mupdf/fitz/pixmap.h"
  8. #include "mupdf/fitz/buffer.h"
  9. #include "mupdf/fitz/stream.h"
  10. #include "mupdf/fitz/compressed-buffer.h"
  11. /*
  12. Images are storable objects from which we can obtain fz_pixmaps.
  13. These may be implemented as simple wrappers around a pixmap, or as
  14. more complex things that decode at different subsample settings on
  15. demand.
  16. */
  17. typedef struct fz_image_s fz_image;
  18. typedef struct fz_compressed_image_s fz_compressed_image;
  19. typedef struct fz_pixmap_image_s fz_pixmap_image;
  20. /*
  21. fz_get_pixmap_from_image: Called to get a handle to a pixmap from an image.
  22. image: The image to retrieve a pixmap from.
  23. subarea: The subarea of the image that we actually care about (or NULL
  24. to indicate the whole image).
  25. trans: Optional, unless subarea is given. If given, then on entry this is
  26. the transform that will be applied to the complete image. It should be
  27. updated on exit to the transform to apply to the given subarea of the
  28. image. This is used to calculate the desired width/height for subsampling.
  29. w: If non-NULL, a pointer to an int to be updated on exit to the
  30. width (in pixels) that the scaled output will cover.
  31. h: If non-NULL, a pointer to an int to be updated on exit to the
  32. height (in pixels) that the scaled output will cover.
  33. Returns a non NULL pixmap pointer. May throw exceptions.
  34. */
  35. fz_pixmap *fz_get_pixmap_from_image(fz_context *ctx, fz_image *image, const fz_irect *subarea, fz_matrix *trans, int *w, int *h);
  36. /*
  37. fz_drop_image: Drop a reference to an image.
  38. image: The image to drop a reference to.
  39. */
  40. void fz_drop_image(fz_context *ctx, fz_image *image);
  41. /*
  42. fz_keep_image: Increment the reference count of an image.
  43. image: The image to take a reference to.
  44. Returns a pointer to the image.
  45. */
  46. fz_image *fz_keep_image(fz_context *ctx, fz_image *image);
  47. fz_image *fz_keep_image_store_key(fz_context *ctx, fz_image *image);
  48. void fz_drop_image_store_key(fz_context *ctx, fz_image *image);
  49. /*
  50. fz_drop_image_fn: Function type to destroy an images data
  51. when it's reference count reaches zero.
  52. */
  53. typedef void (fz_drop_image_fn)(fz_context *ctx, fz_image *image);
  54. /*
  55. fz_get_pixmap_fn: Function type to get a decoded pixmap
  56. for an image.
  57. im: The image to decode.
  58. subarea: NULL, or the subarea of the image required. Expressed
  59. in terms of a rectangle in the original width/height of the
  60. image. If non NULL, this should be updated by the function to
  61. the actual subarea decoded - which must include the requested
  62. area!
  63. w, h: The actual width and height that the whole image would
  64. need to be decoded to.
  65. l2factor: On entry, the log 2 subsample factor required. If
  66. possible the decode process can take care of (all or some) of
  67. this subsampling, and must then update the value so the caller
  68. knows what remains to be done.
  69. Returns a reference to a decoded pixmap that satisfies the
  70. requirements of the request.
  71. */
  72. typedef fz_pixmap *(fz_image_get_pixmap_fn)(fz_context *ctx, fz_image *im, fz_irect *subarea, int w, int h, int *l2factor);
  73. /*
  74. fz_image_get_size_fn: Function type to get the given storage
  75. size for an image.
  76. Returns the size in bytes used for a given image.
  77. */
  78. typedef size_t (fz_image_get_size_fn)(fz_context *, fz_image *);
  79. /*
  80. fz_new_image_of_size: Internal function to make a new fz_image structure
  81. for a derived class.
  82. w,h: Width and height of the created image.
  83. bpc: Bits per component.
  84. colorspace: The colorspace (determines the number of components,
  85. and any color conversions required while decoding).
  86. xres, yres: The X and Y resolutions respectively.
  87. interpolate: 1 if interpolation should be used when decoding
  88. this image, 0 otherwise.
  89. imagemask: 1 if this is an imagemask (i.e. transparent), 0
  90. otherwise.
  91. decode: NULL, or a pointer to to a decode array. The default
  92. decode array is [0 1] (repeated n times, for n color components).
  93. colorkey: NULL, or a pointer to a colorkey array. The default
  94. colorkey array is [0 255] (repeatd n times, for n color
  95. components).
  96. mask: NULL, or another image to use as a mask for this one.
  97. A new reference is taken to this image. Supplying a masked
  98. image as a mask to another image is illegal!
  99. size: The size of the required allocated structure (the size of
  100. the derived structure).
  101. get: The function to be called to obtain a decoded pixmap.
  102. get_size: The function to be called to return the storage size
  103. used by this image.
  104. drop: The function to be called to dispose of this image once
  105. the last reference is dropped.
  106. Returns a pointer to an allocated structure of the required size,
  107. with the first sizeof(fz_image) bytes initialised as appropriate
  108. given the supplied parameters, and the other bytes set to zero.
  109. */
  110. fz_image *fz_new_image_of_size(fz_context *ctx, int w, int h, int bpc, fz_colorspace *colorspace, int xres, int yres, int interpolate, int imagemask, float *decode, int *colorkey, fz_image *mask, int size, fz_image_get_pixmap_fn *get, fz_image_get_size_fn *get_size, fz_drop_image_fn *drop);
  111. #define fz_new_derived_image(CTX,W,H,B,CS,X,Y,I,IM,D,C,M,T,G,S,Z) \
  112. ((T*)Memento_label(fz_new_image_of_size(CTX,W,H,B,CS,X,Y,I,IM,D,C,M,sizeof(T),G,S,Z),#T))
  113. /*
  114. fz_new_image_from_compressed_buffer: Create an image based on
  115. the data in the supplied compressed buffer.
  116. w,h: Width and height of the created image.
  117. bpc: Bits per component.
  118. colorspace: The colorspace (determines the number of components,
  119. and any color conversions required while decoding).
  120. xres, yres: The X and Y resolutions respectively.
  121. interpolate: 1 if interpolation should be used when decoding
  122. this image, 0 otherwise.
  123. imagemask: 1 if this is an imagemask (i.e. transparent), 0
  124. otherwise.
  125. decode: NULL, or a pointer to to a decode array. The default
  126. decode array is [0 1] (repeated n times, for n color components).
  127. colorkey: NULL, or a pointer to a colorkey array. The default
  128. colorkey array is [0 255] (repeatd n times, for n color
  129. components).
  130. buffer: Buffer of compressed data and compression parameters.
  131. Ownership of this reference is passed in.
  132. mask: NULL, or another image to use as a mask for this one.
  133. A new reference is taken to this image. Supplying a masked
  134. image as a mask to another image is illegal!
  135. */
  136. fz_image *fz_new_image_from_compressed_buffer(fz_context *ctx, int w, int h, int bpc, fz_colorspace *colorspace, int xres, int yres, int interpolate, int imagemask, float *decode, int *colorkey, fz_compressed_buffer *buffer, fz_image *mask);
  137. /*
  138. fz_new_image_from_pixmap: Create an image from the given
  139. pixmap.
  140. pixmap: The pixmap to base the image upon. A new reference
  141. to this is taken.
  142. mask: NULL, or another image to use as a mask for this one.
  143. A new reference is taken to this image. Supplying a masked
  144. image as a mask to another image is illegal!
  145. */
  146. fz_image *fz_new_image_from_pixmap(fz_context *ctx, fz_pixmap *pixmap, fz_image *mask);
  147. /*
  148. fz_new_image_from_buffer: Create a new image from a
  149. buffer of data, inferring its type from the format
  150. of the data.
  151. */
  152. fz_image *fz_new_image_from_buffer(fz_context *ctx, fz_buffer *buffer);
  153. /*
  154. fz_image_from_file: Create a new image from the contents
  155. of a file, inferring its type from the format of the
  156. data.
  157. */
  158. fz_image *fz_new_image_from_file(fz_context *ctx, const char *path);
  159. void fz_drop_image_imp(fz_context *ctx, fz_storable *image);
  160. fz_pixmap *fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image *image, fz_irect *subarea, int indexed, int l2factor);
  161. fz_pixmap *fz_expand_indexed_pixmap(fz_context *ctx, const fz_pixmap *src, int alpha);
  162. size_t fz_image_size(fz_context *ctx, fz_image *im);
  163. /*
  164. Structure is public to allow other structures to
  165. be derived from it. Do not access members directly.
  166. */
  167. struct fz_image_s
  168. {
  169. fz_key_storable key_storable;
  170. int w, h;
  171. uint8_t n;
  172. uint8_t bpc;
  173. unsigned int imagemask:1;
  174. unsigned int interpolate:1;
  175. unsigned int use_colorkey:1;
  176. unsigned int use_decode:1;
  177. unsigned int invert_cmyk_jpeg:1;
  178. unsigned int decoded:1;
  179. unsigned int scalable:1;
  180. fz_image *mask;
  181. int xres; /* As given in the image, not necessarily as rendered */
  182. int yres; /* As given in the image, not necessarily as rendered */
  183. fz_colorspace *colorspace;
  184. fz_drop_image_fn *drop_image;
  185. fz_image_get_pixmap_fn *get_pixmap;
  186. fz_image_get_size_fn *get_size;
  187. int colorkey[FZ_MAX_COLORS * 2];
  188. float decode[FZ_MAX_COLORS * 2];
  189. };
  190. fz_pixmap *fz_load_jpeg(fz_context *ctx, unsigned char *data, size_t size);
  191. fz_pixmap *fz_load_jpx(fz_context *ctx, unsigned char *data, size_t size, fz_colorspace *cs);
  192. fz_pixmap *fz_load_png(fz_context *ctx, unsigned char *data, size_t size);
  193. fz_pixmap *fz_load_tiff(fz_context *ctx, unsigned char *data, size_t size);
  194. fz_pixmap *fz_load_jxr(fz_context *ctx, unsigned char *data, size_t size);
  195. fz_pixmap *fz_load_gif(fz_context *ctx, unsigned char *data, size_t size);
  196. fz_pixmap *fz_load_bmp(fz_context *ctx, unsigned char *data, size_t size);
  197. fz_pixmap *fz_load_pnm(fz_context *ctx, unsigned char *data, size_t size);
  198. void fz_load_jpeg_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  199. void fz_load_jpx_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  200. void fz_load_png_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  201. void fz_load_tiff_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  202. void fz_load_jxr_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  203. void fz_load_gif_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  204. void fz_load_bmp_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  205. void fz_load_pnm_info(fz_context *ctx, unsigned char *data, size_t size, int *w, int *h, int *xres, int *yres, fz_colorspace **cspace);
  206. int fz_load_tiff_subimage_count(fz_context *ctx, unsigned char *buf, size_t len);
  207. fz_pixmap *fz_load_tiff_subimage(fz_context *ctx, unsigned char *buf, size_t len, int subimage);
  208. /*
  209. fz_image_resolution: Request the natural resolution
  210. of an image.
  211. xres, yres: Pointers to ints to be updated with the
  212. natural resolution of an image (or a sensible default
  213. if not encoded).
  214. */
  215. void fz_image_resolution(fz_image *image, int *xres, int *yres);
  216. fz_pixmap *fz_compressed_image_tile(fz_context *ctx, fz_compressed_image *cimg);
  217. void fz_set_compressed_image_tile(fz_context *ctx, fz_compressed_image *cimg, fz_pixmap *pix);
  218. /*
  219. fz_compressed_image_buffer: Retrieve the underlying compressed
  220. data for an image.
  221. Returns a pointer to the underlying data buffer for an image,
  222. or NULL if this image is not based upon a compressed data
  223. buffer.
  224. This is not a reference counted structure, so no reference is
  225. returned. Lifespan is limited to that of the image itself.
  226. */
  227. fz_compressed_buffer *fz_compressed_image_buffer(fz_context *ctx, fz_image *image);
  228. void fz_set_compressed_image_buffer(fz_context *ctx, fz_compressed_image *cimg, fz_compressed_buffer *buf);
  229. /*
  230. fz_pixmap_image_tile: Retried the underlying fz_pixmap
  231. for an image.
  232. Returns a pointer to the underlying fz_pixmap for an image,
  233. or NULL if this image is not based upon an fz_pixmap.
  234. No reference is returned. Lifespan is limited to that of
  235. the image itself. If required, use fz_keep_pixmap to take
  236. a reference to keep it longer.
  237. */
  238. fz_pixmap *fz_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *cimg);
  239. void fz_set_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *cimg, fz_pixmap *pix);
  240. #endif