1 /* image.h
2 
3    Copyright 1996-2006 Han The Thanh <thanh@pdftex.org>
4    Copyright 2006-2010 Taco Hoekwater <taco@luatex.org>
5 
6    This file is part of LuaTeX.
7 
8    LuaTeX is free software; you can redistribute it and/or modify it under
9    the terms of the GNU General Public License as published by the Free
10    Software Foundation; either version 2 of the License, or (at your
11    option) any later version.
12 
13    LuaTeX is distributed in the hope that it will be useful, but WITHOUT
14    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16    License for more details.
17 
18    You should have received a copy of the GNU General Public License along
19    with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
20 
21 
22 #ifndef IMAGE_H
23 #  define IMAGE_H
24 
25 #  include <png.h>
26 #  include "pdf/pdftypes.h"     /* for scaled_whd */
27 
28 #  define JPG_UINT16      unsigned int
29 #  define JPG_UINT32      unsigned long
30 #  define JPG_UINT8       unsigned char
31 
32 extern int do_zround(double r); /* from utils.w */
33 extern scaled one_hundred_bp;   /* from pdfgen.w */
34 #  define bp2sp(p)        do_zround(p * (one_hundred_bp / 100.0))
35 #  define sp2bp(i)        (i * 100.0 / one_hundred_bp)
36 
37 #  define TYPE_IMG        "image.meta"
38 #  define TYPE_IMG_DICT   "image.dict"
39 
40 /**********************************************************************/
41 
42 typedef struct {
43     char *stream;
44 } pdf_stream_struct;
45 
46 typedef struct {
47     png_structp png_ptr;
48     png_infop info_ptr;
49 } png_img_struct;
50 
51 typedef struct {
52     int color_space;            /* used color space. See JPG_ constants */
53     JPG_UINT32 length;          /* length of file/data */
54 } jpg_img_struct;
55 
56 typedef struct {
57     int length;                 /* length of file/data */
58 } jp2_img_struct;
59 
60 #  if 0
61 typedef struct {                /* currently unused */
62 } jb2_img_struct;
63 #  endif
64 
65 typedef enum { DICT_NEW,        /* fresh dictionary */
66     DICT_FILESCANNED,           /* image file scanned */
67     DICT_REFERED,               /* pdf_refximage_node in node list --> read-only dict */
68     DICT_OUTIMG,                /* /Im* appears in pagestream */
69     DICT_SCHEDULED,             /* image dict scheduled for writing (PONR) */
70     DICT_WRITTEN                /* image dict written to file */
71 } dict_state;
72 
73 typedef enum { IMG_TYPE_NONE, IMG_TYPE_PDF, IMG_TYPE_PNG, IMG_TYPE_JPG,
74     IMG_TYPE_JP2, IMG_TYPE_JBIG2, IMG_TYPE_PDFSTREAM, IMG_TYPE_SENTINEL
75 } imgtype_e;
76 
77 typedef enum { IMG_KEEPOPEN, IMG_CLOSEINBETWEEN } img_readtype_e;
78 
79 typedef enum { PDF_BOX_SPEC_NONE, PDF_BOX_SPEC_MEDIA, PDF_BOX_SPEC_CROP,
80     PDF_BOX_SPEC_BLEED, PDF_BOX_SPEC_TRIM, PDF_BOX_SPEC_ART,
81     PDF_BOX_SPEC_SENTINEL
82 } pdfboxspec_e;
83 
84 /**********************************************************************/
85 
86 typedef struct {
87     int objnum;
88     int index;                  /* /Im1, /Im2, ... */
89     scaled_whd dimen;           /* TeX dimensions given to \pdfximage */
90     int transform;              /* transform given to \pdfximage */
91     int x_size;                 /* dimensions in pixel counts as in JPG/PNG/JBIG2 file */
92     int y_size;
93     int x_orig;                 /* origin in sp for PDF files */
94     int y_orig;
95     int x_res;                  /* pixel resolution as in JPG/PNG/JBIG2 file */
96     int y_res;
97     int rotation;               /* rotation (multiples of 90 deg.) for PDF files */
98     int colorspace;             /* number of /ColorSpace object */
99     int group_ref;              /* if it's <=0, the page has no group */
100     int total_pages;
101     int page_num;               /* requested page (by number) */
102     char *pagename;             /* requested page (by name) */
103     char *filename;             /* requested raw file name */
104     char *visiblefilename;      /* blocks or overwrites filename as it appears in PDF output */
105     char *filepath;             /* full file path after kpathsea */
106     char *attr;                 /* additional image dict entries */
107     FILE *file;
108     imgtype_e image_type;
109     int procset;                /* /ProcSet flags */
110     int color_depth;            /* color depth */
111     pdfboxspec_e page_box_spec; /* PDF page box spec.: media/crop/bleed/trim/art */
112     int bbox[4];
113     dict_state state;
114     int flags;
115     union {
116         pdf_stream_struct *pdfstream;
117         png_img_struct *png;
118         jpg_img_struct *jpg;
119         jp2_img_struct *jp2;
120         /* jb2_img_struct *jb2; */
121     } img_struct;
122 } image_dict;
123 
124 #  define img_objnum(N)         ((N)->objnum)
125 #  define img_index(N)          ((N)->index)
126 #  define img_dimen(N)          ((N)->dimen)
127 #  define img_width(N)          ((N)->dimen.wd)
128 #  define img_height(N)         ((N)->dimen.ht)
129 #  define img_depth(N)          ((N)->dimen.dp)
130 #  define img_transform(N)      ((N)->transform)
131 #  define img_xsize(N)          ((N)->x_size)
132 #  define img_ysize(N)          ((N)->y_size)
133 #  define img_xorig(N)          ((N)->x_orig)
134 #  define img_yorig(N)          ((N)->y_orig)
135 #  define img_xres(N)           ((N)->x_res)
136 #  define img_yres(N)           ((N)->y_res)
137 #  define img_rotation(N)       ((N)->rotation)
138 #  define img_colorspace(N)     ((N)->colorspace)
139 #  define img_group_ref(N)      ((N)->group_ref)
140 #  define img_totalpages(N)     ((N)->total_pages)
141 #  define img_pagenum(N)        ((N)->page_num)
142 #  define img_pagename(N)       ((N)->pagename)
143 #  define img_filename(N)       ((N)->filename)
144 #  define img_visiblefilename(N)((N)->visiblefilename)
145 #  define img_filepath(N)       ((N)->filepath)
146 #  define img_attr(N)           ((N)->attr)
147 #  define img_file(N)           ((N)->file)
148 #  define img_type(N)           ((N)->image_type)
149 #  define img_procset(N)        ((N)->procset)
150 #  define img_colordepth(N)     ((N)->color_depth)
151 #  define img_pagebox(N)        ((N)->page_box_spec)
152 #  define img_bbox(N)           ((N)->bbox)
153 #  define img_state(N)          ((N)->state)
154 #  define img_flags(N)          ((N)->flags)
155 
156 #  define img_pdfstream_ptr(N)  ((N)->img_struct.pdfstream)
157 #  define img_pdfstream_stream(N) ((N)->img_struct.pdfstream->stream)
158 
159 #  define img_png_ptr(N)        ((N)->img_struct.png)
160 #  define img_png_png_ptr(N)    ((N)->img_struct.png->png_ptr)
161 #  define img_png_info_ptr(N)   ((N)->img_struct.png->info_ptr)
162 
163 #  define img_jpg_ptr(N)        ((N)->img_struct.jpg)
164 #  define img_jpg_color(N)      ((N)->img_struct.jpg->color_space)
165 
166 #  define img_jp2_ptr(N)        ((N)->img_struct.jp2)
167 
168 #  define img_jb2_ptr(N)        ((N)->img_struct.jb2)
169 
170 #  define F_FLAG_BBOX           (1 << 0)
171 #  define F_FLAG_GROUP          (1 << 1)
172 
173 #  define img_set_bbox(N)       (img_flags(N) |= F_FLAG_BBOX)
174 #  define img_unset_bbox(N)     (img_flags(N) &= ~F_FLAG_BBOX)
175 #  define img_is_bbox(N)        ((img_flags(N) & F_FLAG_BBOX) != 0)
176 
177 #  define img_set_group(N)      (img_flags(N) |= F_FLAG_GROUP)
178 #  define img_unset_group(N)    (img_flags(N) &= ~F_FLAG_GROUP)
179 #  define img_is_group(N)       ((img_flags(N) & F_FLAG_GROUP) != 0)
180 
181 #  define epdf_xsize(a)         img_xsize(idict_array[a])
182 #  define epdf_ysize(a)         img_ysize(idict_array[a])
183 #  define epdf_orig_x(a)        img_xorig(idict_array[a])
184 #  define epdf_orig_y(a)        img_yorig(idict_array[a])
185 
186 #  define is_pdf_image(a)       (img_type(idict_array[a]) == IMG_TYPE_PDF)
187 #  define is_png_image(a)       (img_type(idict_array[a]) == IMG_TYPE_PNG)
188 
189 #  define img_is_refered(N)     (img_index(N) != -1)
190 
191 /**********************************************************************/
192 /* image structure corresponds to pdfrefximage node */
193 
194 typedef struct {
195     scaled_whd dimen;           /* requested/actual TeX dimensions */
196     int transform;
197     image_dict *dict;
198     int dict_ref;               /* luaL_ref() reference */
199 } image;
200 
201 #  define img_dict(N)           ((N)->dict)
202 #  define img_dictref(N)        ((N)->dict_ref)
203 
204 #  define set_wd_running(N)     (img_width(N) = null_flag)
205 #  define set_ht_running(N)     (img_height(N) = null_flag)
206 #  define set_dp_running(N)     (img_depth(N) = null_flag)
207 #  define is_wd_running(N)      (img_width(N) == null_flag)
208 #  define is_ht_running(N)      (img_height(N) == null_flag)
209 #  define is_dp_running(N)      (img_depth(N) == null_flag)
210 
211 #endif                          /* IMAGE_H */
212