1 /*---------------------------------------------------------------------------*
2  |              PDFlib - A library for generating PDF on the fly             |
3  +---------------------------------------------------------------------------+
4  | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
5  +---------------------------------------------------------------------------+
6  |                                                                           |
7  |    This software is subject to the PDFlib license. It is NOT in the       |
8  |    public domain. Extended versions and commercial licenses are           |
9  |    available, please check http://www.pdflib.com.                         |
10  |                                                                           |
11  *---------------------------------------------------------------------------*/
12 
13 /* $Id: p_image.h,v 1.97.2.13 2009/08/07 15:31:37 tm Exp $
14  *
15  * Header file for the PDFlib image subsystem
16  *
17  */
18 
19 #ifndef P_IMAGE_H
20 #define P_IMAGE_H
21 
22 #ifdef HAVE_LIBTIFF
23 #include "tiffio.h"
24 #endif
25 
26 #ifdef HAVE_LIBPNG
27 #include "png.h"
28 #endif
29 
30 /* image type */
31 typedef enum
32 {
33     pdf_img_auto,
34     pdf_img_bmp,
35     pdf_img_ccitt,
36     pdf_img_gif,
37     pdf_img_jpeg,
38     pdf_img_jpeg2000,
39     pdf_img_png,
40     pdf_img_raw,
41     pdf_img_tiff
42 }
43 pdf_image_type;
44 
45 /* compression type */
46 typedef enum
47 {
48     pdf_comp_none,
49     pdf_comp_lzw,
50     pdf_comp_runlength,
51     pdf_comp_ccitt,
52     pdf_comp_dct,
53     pdf_comp_flate,
54     pdf_comp_jbig2,
55     pdf_comp_jpx
56 }
57 pdf_compression;
58 
59 /* image reference */
60 typedef enum
61 {
62     pdf_ref_direct,
63     pdf_ref_file,
64     pdf_ref_url
65 }
66 pdf_ref_type;
67 
68 typedef enum
69 {
70     pred_default = 1,
71     pred_tiff = 2,
72     pred_png = 15
73 }
74 pdf_predictor;
75 
76 #ifdef P_IMAGE_C
77 
78 const pdc_keyconn pdf_image_keylist[] =
79 {
80     {"auto",      pdf_img_auto},
81     {"bmp",       pdf_img_bmp},
82     {"ccitt",     pdf_img_ccitt},
83     {"gif",       pdf_img_gif},
84     {"jpeg",      pdf_img_jpeg},
85     {"jpeg2000",  pdf_img_jpeg2000},
86     {"png",       pdf_img_png},
87     {"raw",       pdf_img_raw},
88     {"tiff",      pdf_img_tiff},
89     {NULL, 0}
90 };
91 
92 const pdc_keyconn pdf_filter_pdfkeylist[] =
93 {
94     {"",                 pdf_comp_none},
95     {"LZWDecode",        pdf_comp_lzw},
96     {"RunLengthDecode",  pdf_comp_runlength},
97     {"CCITTFaxDecode",   pdf_comp_ccitt},
98     {"DCTDecode",        pdf_comp_dct},
99     {"FlateDecode",      pdf_comp_flate},
100     {"JBIG2Decode",      pdf_comp_jbig2},
101     {"JPXDecode",        pdf_comp_jpx},
102     {NULL, 0}
103 };
104 
105 const pdc_keyconn pdf_shortfilter_pdfkeylist[] =
106 {
107     {"",      pdf_comp_none},
108     {"LZW",   pdf_comp_lzw},
109     {"RL",    pdf_comp_runlength},
110     {"CCF",   pdf_comp_ccitt},
111     {"DCT",   pdf_comp_dct},
112     {"Fl",    pdf_comp_flate},
113     {NULL, 0}
114 };
115 
116 static const pdc_keyconn pdf_reftype_keys[] =
117 {
118     {"direct",  pdf_ref_direct},
119     {"fileref", pdf_ref_file},
120     {"url",     pdf_ref_url},
121     {NULL, 0}
122 };
123 
124 #endif /* P_IMAGE_C */
125 
126 /* BMP specific image information */
127 typedef struct pdf_bmp_info_t {
128     pdc_uint32          compression;    /* BMP compression */
129     pdc_uint32          redmask;        /* red mask */
130     pdc_ushort          redmax;         /* red maximal value */
131     pdc_ushort          redmove;        /* red mask's movement */
132     pdc_uint32          greenmask;      /* green mask */
133     pdc_ushort          greenmax;       /* green maximal value */
134     pdc_ushort          greenmove;      /* green mask's movement */
135     pdc_uint32          bluemask;       /* blue mask */
136     pdc_ushort          bluemax;        /* blue maximal value */
137     pdc_ushort          bluemove;       /* blue mask's movement */
138     pdc_ushort          bpp;            /* bits per pixel */
139     size_t              rowbytes;       /* length of row data */
140     size_t              rowbytes_pad;   /* padded length of row data */
141     size_t              rowbytes_buf;   /* buffer for row data */
142     size_t              rowbytes_pdf;   /* length of row data for PDF */
143     size_t              skiprows;       /* number of rows to be skipped */
144     pdc_byte           *bitmap;         /* bitmap buffer */
145     pdc_byte           *end;            /* first byte above bitmap buffer */
146     pdc_byte           *pos;            /* current position in bitmap buffer */
147 } pdf_bmp_info;
148 
149 typedef struct pdf_jpeg_segment_s pdf_jpeg_segment;
150 
151 /* JPEG specific image information */
152 #define JPEG_MAX_COMPS 4		/* max number components */
153 typedef struct pdf_jpeg_info_t {
154     const char         *virtfile;       /* temporary virtual file name */
155     pdf_jpeg_segment   *seglist;        /* list of segments to be copy */
156     int                 capacity;       /* currently allocated size */
157     int                 number;         /* next available segment number */
158     pdc_uint32		jpegifoffset;	/* offset to JPEG data for TIFF OJPEG */
159     pdc_byte		id[JPEG_MAX_COMPS]; /* component ids */
160     pdc_byte		hsamp[JPEG_MAX_COMPS]; /* horizontal sampling factor */
161     pdc_byte		vsamp[JPEG_MAX_COMPS]; /* vertical sampling factor */
162     pdc_byte		table[JPEG_MAX_COMPS]; /* quant table  */
163 } pdf_jpeg_info;
164 
165 /* GIF specific image information */
166 typedef struct pdf_gif_info_t {
167     int			useGlobalColormap;
168     int			interlace;
169 
170     /* LZW decompression state */
171     int			ZeroDataBlock;
172     int			curbit;
173     int			lastbit;
174     int			get_done;
175     int			last_byte;
176     int			return_clear;
177     int			*sp;
178     int			code_size, set_code_size;
179     int			max_code, max_code_size;
180     int			clear_code, end_code;
181     pdc_byte		buf[280];
182     int			firstcode;
183     int			oldcode;
184 
185     /* These are dynamically malloc'ed to avoid wasting 64KB for each image */
186 #define MAX_LWZ_BITS            12
187 #define GIF_TABLE_ELEMENTS	(1<< MAX_LWZ_BITS)
188     int			(*table)[GIF_TABLE_ELEMENTS];
189     int			*stack;
190 } pdf_gif_info;
191 
192 
193 /* PNG specific image information */
194 typedef struct pdf_png_info_t {
195     size_t		nbytes;		/* number of bytes left		*/
196     					/* in current IDAT chunk	*/
197 #ifdef HAVE_LIBPNG
198     png_structp		png_ptr;
199     png_infop		info_ptr;
200     png_uint_32		rowbytes;
201     pdc_byte		*raster;
202     int			cur_line;
203 #endif	/* HAVE_LIBPNG */
204 } pdf_png_info;
205 
206 
207 /* TIFF specific image information */
208 typedef struct pdf_tiff_info_t {
209 #ifdef HAVE_LIBTIFF
210     TIFF		*tif;		/* pointer to TIFF data structure */
211     uint32		*raster;	/* frame buffer */
212 #endif	/* HAVE_LIBTIFF */
213 
214     int			cur_line;	/* current image row or strip */
215 } pdf_tiff_info;
216 
217 /* CCITT specific image information */
218 typedef struct pdf_ccitt_info_t {
219     int			BitReverse;	/* reverse all bits prior to use */
220 } pdf_ccitt_info;
221 
222 /* The image descriptor */
223 struct pdf_image_s {
224     pdc_file 		*fp;		/* image file pointer */
225     char		*filename;	/* image file name or url */
226     /* width and height in pixels, or in points for PDF pages and templates */
227     pdc_scalar          width;          /* image width */
228     pdc_scalar          height;         /* image height */
229     int                 orientation;    /* image orientation according TIFF */
230     pdf_compression	compression;	/* image compression type */
231     int	                colorspace;	/* image color space */
232 
233     /*************************** option variables *****************************/
234     pdc_bool            verbose;        /* put out warning/error messages */
235     pdc_bool            bitreverse;     /* bitwise reversal of all bytes */
236     int                 bpc;            /* bits per color component */
237     int                 components;     /* number of color components */
238     int                 height_pixel;   /* image height in pixel */
239     int                 width_pixel;    /* image width in pixel */
240     pdc_bool            ignoremask;     /* ignore any transparency information*/
241     pdc_bool            ignoreorient;   /* ignore orientation TIFF tag */
242     pdc_bool            doinline;       /* inline image */
243     pdc_bool            interpolate;    /* interpolate image   */
244     pdc_bool            invert;         /* reverse black and white */
245     pdc_bool            jpegoptimize;   /* skip application segments of JPEG */
246     pdc_bool            passthrough;    /* pass through mode for TIFF, JPEG */
247     int                 K;              /* encoding type of CCITT */
248     pdc_bool            imagemask;     	/* create a mask from a 1-bit image */
249     int                 mask;           /* image number of image mask */
250     pdf_renderingintent ri;             /* rendering intent of image */
251     int                 page;           /* page number of TIFF image */
252     pdf_ref_type        reference;      /* kind of image data reference */
253     pdc_bool            topdown_save;   /* saved topdown flag */
254     char               *iconname;       /* icon name for template images */
255     pdf_transgroup      tgroup;         /* transparency group definition */
256     /**************************************************************************/
257 
258     pdc_bool		transparent;	/* image is transparent */
259     pdc_ushort		transval[4];	/* transparent color values */
260     pdf_predictor	predictor;	/* predictor for lzw and flate */
261 
262     pdc_scalar          dpi_x;          /* horiz. resolution in dots per inch */
263     pdc_scalar          dpi_y;          /* vert. resolution in dots per inch */
264     					/* dpi is 0 if unknown */
265 
266     pdc_bool            in_use;         /* image slot currently in use */
267     pdc_bool            corrupt;        /* image is corrupt */
268 
269     char		*params;	/* for TIFF */
270     int			strips;		/* number of strips in image */
271     int			rowsperstrip;	/* number of rows per strip */
272     int                 pagehandle;     /* PDI page handle */
273     int			dochandle;	/* PDI document handle */
274     pdc_pagebox		usebox;
275     pdc_bool		use_raw;	/* use raw (compressed) image data */
276     /* Only relevant for use_raw = false */
277     pdc_bool		pixelmode;	/* Use TIFFReadRGBAImageOriented() ? */
278 
279     pdf_image_type	type;		/* image type, used for cleanup */
280     /* image format specific information */
281     union {
282         pdf_bmp_info    bmp;
283 	pdf_jpeg_info	jpeg;
284 	pdf_gif_info	gif;
285 	pdf_png_info	png;
286 	pdf_tiff_info	tiff;
287 	pdf_ccitt_info	ccitt;
288     } info;
289 
290     int			no;		/* PDF image number */
291     PDF_data_source	src;
292 };
293 
294 /* xobject types */
295 typedef enum {
296     image_xobject = 1 << 0,
297     form_xobject = 1 << 1,
298     pdi_xobject = 1 << 2
299 } pdf_xobj_type;
300 
301 typedef enum {
302     xobj_flag_used = 1 << 0,		/* in use */
303     xobj_flag_write = 1 << 1		/* write at end of page */
304 } pdf_xobj_flags;
305 
306 /* A PDF xobject */
307 struct pdf_xobject_s {
308     pdc_id		obj_id;		/* object id of this xobject */
309     int			flags;		/* bit mask of pdf_xobj_flags */
310     pdf_xobj_type	type;		/* type of this xobject */
311 };
312 
313 /* p_bmp.c */
314 int      pdf_process_BMP_data(PDF *p, int imageslot);
315 pdc_bool pdf_is_BMP_file(PDF *p, pdc_file *fp);
316 
317 /* p_ccitt.c */
318 int      pdf_process_CCITT_data(PDF *p, int imageslot);
319 int      pdf_process_RAW_data(PDF *p, int imageslot);
320 
321 /* p_gif.c */
322 int      pdf_process_GIF_data(PDF *p, int imageslot);
323 pdc_bool pdf_is_GIF_file(PDF *p, pdc_file *fp);
324 void	 pdf_cleanup_gif(PDF *p, pdf_image *image);
325 
326 /* p_jpeg.c */
327 int      pdf_process_JPEG_data(PDF *p, int imageslot);
328 pdc_bool pdf_is_JPEG_file(PDF *p, pdc_file *fp);
329 void     pdf_cleanup_jpeg(PDF *p, pdf_image *image);
330 
331 /* p_jpx.c */
332 int      pdf_process_JPX_data(PDF *p, int imageslot);
333 pdc_bool pdf_is_JPX_file(PDF *p, pdc_file *fp);
334 void     pdf_cleanup_jpx(PDF *p, pdf_image *image);
335 
336 /* p_png.c */
337 int	 pdf_process_PNG_data(PDF *p, int imageslot);
338 pdc_bool pdf_is_PNG_file(PDF *p, pdc_file *fp);
339 
340 /* p_tiff.c */
341 int      pdf_process_TIFF_data(PDF *p, int imageslot);
342 pdc_bool pdf_is_TIFF_file(PDF *p, pdc_file *fp, pdf_tiff_info *tiff,
343                           pdc_bool check);
344 
345 void     pdf_proc_TIFF_alpha(PDF *p, int imageslot, pdc_byte *smaskData,
346                              unsigned int smaskLen);
347 
348 /* p_image.c */
349 pdc_id pdf_get_xobject(PDF *p, int im);
350 void pdf_init_xobjects(PDF *p);
351 void pdf_write_xobjects(PDF *p);
352 void pdf_place_xobject(PDF *p, int im, pdc_scalar x, pdc_scalar y,
353         const char *optlist);
354 int pdf_new_xobject(PDF *p, pdf_xobj_type type, pdc_id obj_id);
355 void pdf_get_page_xobjects(PDF *p, pdf_reslist *rl);
356 void pdf_mark_page_xobject(PDF *p, int n);
357 void pdf_cleanup_xobjects(PDF *p);
358 const char *pdf_get_image_filename(PDF *p, pdf_image *image);
359 
360 
361 #endif /* P_IMAGE_H */
362 
363