1 #ifndef MUPDF_FITZ_IMAGE_H
2 #define MUPDF_FITZ_IMAGE_H
3 
4 #include "mupdf/fitz/system.h"
5 #include "mupdf/fitz/context.h"
6 #include "mupdf/fitz/store.h"
7 #include "mupdf/fitz/pixmap.h"
8 
9 #include "mupdf/fitz/buffer.h"
10 #include "mupdf/fitz/stream.h"
11 #include "mupdf/fitz/compressed-buffer.h"
12 
13 /**
14 	Images are storable objects from which we can obtain fz_pixmaps.
15 	These may be implemented as simple wrappers around a pixmap, or
16 	as more complex things that decode at different subsample
17 	settings on demand.
18 */
19 typedef struct fz_image fz_image;
20 typedef struct fz_compressed_image fz_compressed_image;
21 typedef struct fz_pixmap_image fz_pixmap_image;
22 
23 /**
24 	Called to get a handle to a pixmap from an image.
25 
26 	image: The image to retrieve a pixmap from.
27 
28 	color_params: The color parameters (or NULL for defaults).
29 
30 	subarea: The subarea of the image that we actually care about
31 	(or NULL to indicate the whole image).
32 
33 	trans: Optional, unless subarea is given. If given, then on
34 	entry this is the transform that will be applied to the complete
35 	image. It should be updated on exit to the transform to apply to
36 	the given subarea of the image. This is used to calculate the
37 	desired width/height for subsampling.
38 
39 	w: If non-NULL, a pointer to an int to be updated on exit to the
40 	width (in pixels) that the scaled output will cover.
41 
42 	h: If non-NULL, a pointer to an int to be updated on exit to the
43 	height (in pixels) that the scaled output will cover.
44 
45 	Returns a non NULL pixmap pointer. May throw exceptions.
46 */
47 fz_pixmap *fz_get_pixmap_from_image(fz_context *ctx, fz_image *image, const fz_irect *subarea, fz_matrix *ctm, int *w, int *h);
48 
49 /**
50 	Increment the (normal) reference count for an image. Returns the
51 	same pointer.
52 
53 	Never throws exceptions.
54 */
55 fz_image *fz_keep_image(fz_context *ctx, fz_image *image);
56 
57 /**
58 	Decrement the (normal) reference count for an image. When the
59 	total (normal + key) reference count reaches zero, the image and
60 	its resources are freed.
61 
62 	Never throws exceptions.
63 */
64 void fz_drop_image(fz_context *ctx, fz_image *image);
65 
66 /**
67 	Increment the store key reference for an image. Returns the same
68 	pointer. (This is the count of references for an image held by
69 	keys in the image store).
70 
71 	Never throws exceptions.
72 */
73 fz_image *fz_keep_image_store_key(fz_context *ctx, fz_image *image);
74 
75 /**
76 	Decrement the store key reference count for an image. When the
77 	total (normal + key) reference count reaches zero, the image and
78 	its resources are freed.
79 
80 	Never throws exceptions.
81 */
82 void fz_drop_image_store_key(fz_context *ctx, fz_image *image);
83 
84 /**
85 	Function type to destroy an images data
86 	when it's reference count reaches zero.
87 */
88 typedef void (fz_drop_image_fn)(fz_context *ctx, fz_image *image);
89 
90 /**
91 	Function type to get a decoded pixmap for an image.
92 
93 	im: The image to decode.
94 
95 	subarea: NULL, or the subarea of the image required. Expressed
96 	in terms of a rectangle in the original width/height of the
97 	image. If non NULL, this should be updated by the function to
98 	the actual subarea decoded - which must include the requested
99 	area!
100 
101 	w, h: The actual width and height that the whole image would
102 	need to be decoded to.
103 
104 	l2factor: On entry, the log 2 subsample factor required. If
105 	possible the decode process can take care of (all or some) of
106 	this subsampling, and must then update the value so the caller
107 	knows what remains to be done.
108 
109 	Returns a reference to a decoded pixmap that satisfies the
110 	requirements of the request. The caller owns the returned
111 	reference.
112 */
113 typedef fz_pixmap *(fz_image_get_pixmap_fn)(fz_context *ctx, fz_image *im, fz_irect *subarea, int w, int h, int *l2factor);
114 
115 /**
116 	Function type to get the given storage
117 	size for an image.
118 
119 	Returns the size in bytes used for a given image.
120 */
121 typedef size_t (fz_image_get_size_fn)(fz_context *, fz_image *);
122 
123 /**
124 	Internal function to make a new fz_image structure
125 	for a derived class.
126 
127 	w,h: Width and height of the created image.
128 
129 	bpc: Bits per component.
130 
131 	colorspace: The colorspace (determines the number of components,
132 	and any color conversions required while decoding).
133 
134 	xres, yres: The X and Y resolutions respectively.
135 
136 	interpolate: 1 if interpolation should be used when decoding
137 	this image, 0 otherwise.
138 
139 	imagemask: 1 if this is an imagemask (i.e. transparent), 0
140 	otherwise.
141 
142 	decode: NULL, or a pointer to to a decode array. The default
143 	decode array is [0 1] (repeated n times, for n color components).
144 
145 	colorkey: NULL, or a pointer to a colorkey array. The default
146 	colorkey array is [0 255] (repeated n times, for n color
147 	components).
148 
149 	mask: NULL, or another image to use as a mask for this one.
150 	A new reference is taken to this image. Supplying a masked
151 	image as a mask to another image is illegal!
152 
153 	size: The size of the required allocated structure (the size of
154 	the derived structure).
155 
156 	get: The function to be called to obtain a decoded pixmap.
157 
158 	get_size: The function to be called to return the storage size
159 	used by this image.
160 
161 	drop: The function to be called to dispose of this image once
162 	the last reference is dropped.
163 
164 	Returns a pointer to an allocated structure of the required size,
165 	with the first sizeof(fz_image) bytes initialised as appropriate
166 	given the supplied parameters, and the other bytes set to zero.
167 */
168 fz_image *fz_new_image_of_size(fz_context *ctx,
169 		int w,
170 		int h,
171 		int bpc,
172 		fz_colorspace *colorspace,
173 		int xres,
174 		int yres,
175 		int interpolate,
176 		int imagemask,
177 		float *decode,
178 		int *colorkey,
179 		fz_image *mask,
180 		size_t size,
181 		fz_image_get_pixmap_fn *get_pixmap,
182 		fz_image_get_size_fn *get_size,
183 		fz_drop_image_fn *drop);
184 
185 #define fz_new_derived_image(CTX,W,H,B,CS,X,Y,I,IM,D,C,M,T,G,S,Z) \
186 	((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))
187 
188 /**
189 	Create an image based on
190 	the data in the supplied compressed buffer.
191 
192 	w,h: Width and height of the created image.
193 
194 	bpc: Bits per component.
195 
196 	colorspace: The colorspace (determines the number of components,
197 	and any color conversions required while decoding).
198 
199 	xres, yres: The X and Y resolutions respectively.
200 
201 	interpolate: 1 if interpolation should be used when decoding
202 	this image, 0 otherwise.
203 
204 	imagemask: 1 if this is an imagemask (i.e. transparency bitmap
205 	mask), 0 otherwise.
206 
207 	decode: NULL, or a pointer to to a decode array. The default
208 	decode array is [0 1] (repeated n times, for n color components).
209 
210 	colorkey: NULL, or a pointer to a colorkey array. The default
211 	colorkey array is [0 255] (repeated n times, for n color
212 	components).
213 
214 	buffer: Buffer of compressed data and compression parameters.
215 	Ownership of this reference is passed in.
216 
217 	mask: NULL, or another image to use as a mask for this one.
218 	A new reference is taken to this image. Supplying a masked
219 	image as a mask to another image is illegal!
220 */
221 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);
222 
223 /**
224 	Create an image from the given
225 	pixmap.
226 
227 	pixmap: The pixmap to base the image upon. A new reference
228 	to this is taken.
229 
230 	mask: NULL, or another image to use as a mask for this one.
231 	A new reference is taken to this image. Supplying a masked
232 	image as a mask to another image is illegal!
233 */
234 fz_image *fz_new_image_from_pixmap(fz_context *ctx, fz_pixmap *pixmap, fz_image *mask);
235 
236 /**
237 	Create a new image from a
238 	buffer of data, inferring its type from the format
239 	of the data.
240 */
241 fz_image *fz_new_image_from_buffer(fz_context *ctx, fz_buffer *buffer);
242 
243 /**
244 	Create a new image from the contents
245 	of a file, inferring its type from the format of the
246 	data.
247 */
248 fz_image *fz_new_image_from_file(fz_context *ctx, const char *path);
249 
250 /**
251 	Internal destructor exposed for fz_store integration.
252 */
253 void fz_drop_image_imp(fz_context *ctx, fz_storable *image);
254 
255 /**
256 	Internal destructor for the base image class members.
257 
258 	Exposed to allow derived image classes to be written.
259 */
260 void fz_drop_image_base(fz_context *ctx, fz_image *image);
261 
262 /**
263 	Decode a subarea of a compressed image at a given l2factor
264 	from the given stream.
265 */
266 fz_pixmap *fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image *image, fz_irect *subarea, int indexed, int l2factor);
267 
268 /**
269 	Convert pixmap from indexed to base colorspace.
270 
271 	This creates a new bitmap containing the converted pixmap data.
272  */
273 fz_pixmap *fz_convert_indexed_pixmap_to_base(fz_context *ctx, const fz_pixmap *src);
274 
275 /**
276 	Convert pixmap from DeviceN/Separation to base colorspace.
277 
278 	This creates a new bitmap containing the converted pixmap data.
279 */
280 fz_pixmap *fz_convert_separation_pixmap_to_base(fz_context *ctx, const fz_pixmap *src);
281 
282 /**
283 	Return the size of the storage used by an image.
284 */
285 size_t fz_image_size(fz_context *ctx, fz_image *im);
286 
287 /**
288 	Structure is public to allow other structures to
289 	be derived from it. Do not access members directly.
290 */
291 struct fz_image
292 {
293 	fz_key_storable key_storable;
294 	int w, h;
295 	uint8_t n;
296 	uint8_t bpc;
297 	unsigned int imagemask:1;
298 	unsigned int interpolate:1;
299 	unsigned int use_colorkey:1;
300 	unsigned int use_decode:1;
301 	unsigned int invert_cmyk_jpeg:1;
302 	unsigned int decoded:1;
303 	unsigned int scalable:1;
304 	fz_image *mask;
305 	int xres; /* As given in the image, not necessarily as rendered */
306 	int yres; /* As given in the image, not necessarily as rendered */
307 	fz_colorspace *colorspace;
308 	fz_drop_image_fn *drop_image;
309 	fz_image_get_pixmap_fn *get_pixmap;
310 	fz_image_get_size_fn *get_size;
311 	int colorkey[FZ_MAX_COLORS * 2];
312 	float decode[FZ_MAX_COLORS * 2];
313 };
314 
315 /**
316 	Request the natural resolution
317 	of an image.
318 
319 	xres, yres: Pointers to ints to be updated with the
320 	natural resolution of an image (or a sensible default
321 	if not encoded).
322 */
323 void fz_image_resolution(fz_image *image, int *xres, int *yres);
324 
325 /**
326 	Retrieve the underlying compressed data for an image.
327 
328 	Returns a pointer to the underlying data buffer for an image,
329 	or NULL if this image is not based upon a compressed data
330 	buffer.
331 
332 	This is not a reference counted structure, so no reference is
333 	returned. Lifespan is limited to that of the image itself.
334 */
335 fz_compressed_buffer *fz_compressed_image_buffer(fz_context *ctx, fz_image *image);
336 void fz_set_compressed_image_buffer(fz_context *ctx, fz_compressed_image *cimg, fz_compressed_buffer *buf);
337 
338 /**
339 	Retrieve the underlying fz_pixmap for an image.
340 
341 	Returns a pointer to the underlying fz_pixmap for an image,
342 	or NULL if this image is not based upon an fz_pixmap.
343 
344 	No reference is returned. Lifespan is limited to that of
345 	the image itself. If required, use fz_keep_pixmap to take
346 	a reference to keep it longer.
347 */
348 fz_pixmap *fz_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *cimg);
349 void fz_set_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *cimg, fz_pixmap *pix);
350 
351 /* Implementation details: subject to change. */
352 
353 /**
354 	Exposed for PDF.
355 */
356 fz_pixmap *fz_load_jpx(fz_context *ctx, const unsigned char *data, size_t size, fz_colorspace *cs);
357 
358 /**
359 	Exposed for CBZ.
360 */
361 int fz_load_tiff_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len);
362 fz_pixmap *fz_load_tiff_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage);
363 int fz_load_pnm_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len);
364 fz_pixmap *fz_load_pnm_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage);
365 int fz_load_jbig2_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len);
366 fz_pixmap *fz_load_jbig2_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage);
367 int fz_load_bmp_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len);
368 fz_pixmap *fz_load_bmp_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage);
369 
370 #endif
371