summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/image/image.h
blob: 687aa2bdf85d400a6cf81c60d4d884ebf9c90059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* image.h

   Copyright 1996-2006 Han The Thanh <thanh@pdftex.org>
   Copyright 2006-2010 Taco Hoekwater <taco@luatex.org>

   This file is part of LuaTeX.

   LuaTeX is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   LuaTeX is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */

#ifndef IMAGE_H
#  define IMAGE_H

#  include <png.h>
#  include "pdf/pdftypes.h"     /* for scaled_whd */

#  define JPG_UINT16      unsigned int
#  define JPG_UINT32      unsigned long
#  define JPG_UINT8       unsigned char

extern int do_zround(double r); /* from utils.w */
extern scaled one_hundred_bp;   /* from pdfgen.w */

#  define bp2sp(p)        do_zround(p * (one_hundred_bp / 100.0))
#  define sp2bp(i)        (i * 100.0 / one_hundred_bp)

#  define TYPE_IMG        "image.meta"
#  define TYPE_IMG_DICT   "image.dict"

typedef struct {
    char *stream;
    size_t size;
} pdf_stream_struct;

typedef struct {
    png_structp png_ptr;
    png_infop info_ptr;
} png_img_struct;

typedef struct {
    int color_space;   /* used color space. See JPG_ constants */
    JPG_UINT32 length; /* length of file/data */
} jpg_img_struct;

typedef struct {
    int length;        /* length of file/data */
} jp2_img_struct;

#  if 0
typedef struct {       /* currently unused */
} jb2_img_struct;
#  endif

typedef enum {
    DICT_NEW,          /* fresh dictionary */
    DICT_FILESCANNED,  /* image file scanned */
    DICT_REFERED,      /* pdf_refximage_node in node list --> read-only dict */
    DICT_OUTIMG,       /* /Im* appears in pagestream */
    DICT_SCHEDULED,    /* image dict scheduled for writing (PONR) */
    DICT_WRITTEN       /* image dict written to file */
} dict_state;

typedef enum {
    IMG_TYPE_NONE,
    IMG_TYPE_PDF,
    IMG_TYPE_PNG,
    IMG_TYPE_JPG,
    IMG_TYPE_JP2,
    IMG_TYPE_JBIG2,
    IMG_TYPE_PDFSTREAM,
    IMG_TYPE_PDFMEMSTREAM,
    IMG_TYPE_SENTINEL
} imgtype_e;

typedef enum { IMG_KEEPOPEN, IMG_CLOSEINBETWEEN } img_readtype_e;

typedef enum {
    PDF_BOX_SPEC_NONE,
    PDF_BOX_SPEC_MEDIA,
    PDF_BOX_SPEC_CROP,
    PDF_BOX_SPEC_BLEED,
    PDF_BOX_SPEC_TRIM,
    PDF_BOX_SPEC_ART,
    PDF_BOX_SPEC_SENTINEL
} pdfboxspec_e;

typedef struct {
    int objnum;
    int index;                  /* /Im1, /Im2, ... */
    scaled_whd dimen;           /* TeX dimensions given to \pdfximage */
    int transform;              /* transform given to \pdfximage */
    int x_size;                 /* dimensions in pixel counts as in JPG/PNG/JBIG2 file */
    int y_size;
    int x_orig;                 /* origin in sp for PDF files */
    int y_orig;
    int x_res;                  /* pixel resolution as in JPG/PNG/JBIG2 file */
    int y_res;
    int rotation;               /* rotation (multiples of 90 deg.) for PDF files */
    int orientation;            /* jpeg orientation, 0=unset, 1=ul, 2=ur, 3=lr, 4=ll, 5-8 flipped */
    int colorspace;             /* number of /ColorSpace object */
    int group_ref;              /* if it's <=0, the page has no group */
    int total_pages;
    int page_num;               /* requested page (by number) */
    char *pagename;             /* requested page (by name) */
    char *filename;             /* requested raw file name */
    char *visiblefilename;      /* blocks or overwrites filename as it appears in PDF output */
    char *filepath;             /* full file path after kpathsea */
    char *attr;                 /* additional image dict entries */
    FILE *file;
    char *userpassword;
    char *ownerpassword;
    imgtype_e image_type;
    int procset;                /* /ProcSet flags */
    int color_depth;            /* color depth */
    pdfboxspec_e page_box_spec; /* PDF page box spec.: media/crop/bleed/trim/art */
    int bbox[4];
    dict_state state;
    int flags;
    int luaref ;
    boolean keepopen;
    boolean nolength;
    boolean notype;
    boolean nobbox;
    int errorlevel;
    int pdfmajorversion;
    int pdfminorversion;
    union {
        pdf_stream_struct *pdfstream;
        png_img_struct *png;
        jpg_img_struct *jpg;
        jp2_img_struct *jp2;
     /* jb2_img_struct *jb2; */
    } img_struct;
} image_dict;

#  define img_objnum(N)           ((N)->objnum)
#  define img_index(N)            ((N)->index)
#  define img_dimen(N)            ((N)->dimen)
#  define img_width(N)            ((N)->dimen.wd)
#  define img_height(N)           ((N)->dimen.ht)
#  define img_depth(N)            ((N)->dimen.dp)
#  define img_transform(N)        ((N)->transform)
#  define img_xsize(N)            ((N)->x_size)
#  define img_ysize(N)            ((N)->y_size)
#  define img_xorig(N)            ((N)->x_orig)
#  define img_yorig(N)            ((N)->y_orig)
#  define img_xres(N)             ((N)->x_res)
#  define img_yres(N)             ((N)->y_res)
#  define img_rotation(N)         ((N)->rotation)
#  define img_orientation(N)      ((N)->orientation)
#  define img_colorspace(N)       ((N)->colorspace)
#  define img_group_ref(N)        ((N)->group_ref)
#  define img_totalpages(N)       ((N)->total_pages)
#  define img_pagenum(N)          ((N)->page_num)
#  define img_pagename(N)         ((N)->pagename)
#  define img_filename(N)         ((N)->filename)
#  define img_visiblefilename(N)  ((N)->visiblefilename)
#  define img_userpassword(N)     ((N)->userpassword)
#  define img_ownerpassword(N)    ((N)->ownerpassword)
#  define img_filepath(N)         ((N)->filepath)
#  define img_attr(N)             ((N)->attr)
#  define img_file(N)             ((N)->file)
#  define img_type(N)             ((N)->image_type)
#  define img_procset(N)          ((N)->procset)
#  define img_colordepth(N)       ((N)->color_depth)
#  define img_pagebox(N)          ((N)->page_box_spec)
#  define img_bbox(N)             ((N)->bbox)
#  define img_state(N)            ((N)->state)
#  define img_flags(N)            ((N)->flags)
#  define img_luaref(N)           ((N)->luaref)
#  define img_keepopen(N)         ((N)->keepopen)
#  define img_nobbox(N)           ((N)->nobbox)
#  define img_nolength(N)         ((N)->nolength)
#  define img_notype(N)           ((N)->notype)
#  define img_errorlevel(N)       ((N)->errorlevel)
#  define img_pdfmajorversion(N)  ((N)->pdfmajorversion)
#  define img_pdfminorversion(N)  ((N)->pdfminorversion)

#  define img_pdfstream_ptr(N)    ((N)->img_struct.pdfstream)
#  define img_pdfstream_stream(N) ((N)->img_struct.pdfstream->stream)
#  define img_pdfstream_size(N)   ((N)->img_struct.pdfstream->size)

#  define img_png_ptr(N)          ((N)->img_struct.png)
#  define img_png_png_ptr(N)      ((N)->img_struct.png->png_ptr)
#  define img_png_info_ptr(N)     ((N)->img_struct.png->info_ptr)

#  define img_jpg_ptr(N)          ((N)->img_struct.jpg)
#  define img_jpg_color(N)        ((N)->img_struct.jpg->color_space)

#  define img_jp2_ptr(N)          ((N)->img_struct.jp2)

#  define img_jb2_ptr(N)          ((N)->img_struct.jb2)

#  define F_FLAG_BBOX             (1 << 0)
#  define F_FLAG_GROUP            (1 << 1)

#  define img_set_bbox(N)         (img_flags(N) |= F_FLAG_BBOX)
#  define img_unset_bbox(N)       (img_flags(N) &= ~F_FLAG_BBOX)
#  define img_is_bbox(N)          ((img_flags(N) & F_FLAG_BBOX) != 0)

#  define img_set_group(N)        (img_flags(N) |= F_FLAG_GROUP)
#  define img_unset_group(N)      (img_flags(N) &= ~F_FLAG_GROUP)
#  define img_is_group(N)         ((img_flags(N) & F_FLAG_GROUP) != 0)

#  define epdf_xsize(a)           img_xsize(idict_array[a])
#  define epdf_ysize(a)           img_ysize(idict_array[a])
#  define epdf_orig_x(a)          img_xorig(idict_array[a])
#  define epdf_orig_y(a)          img_yorig(idict_array[a])

#  define is_pdf_image(a)         ((img_type(idict_array[a]) == IMG_TYPE_PDF) || (img_type(idict_array[a]) == IMG_TYPE_PDFMEMSTREAM))
#  define is_png_image(a)         (img_type(idict_array[a]) == IMG_TYPE_PNG)

#  define img_is_refered(N)       (img_index(N) != -1)

typedef struct {
    scaled_whd dimen; /* requested/actual TeX dimensions */
    int transform;
    image_dict *dict;
    int dict_ref;     /* luaL_ref() reference */
} image;

#  define img_dict(N)             ((N)->dict)
#  define img_dictref(N)          ((N)->dict_ref)

#  define set_wd_running(N)       (img_width(N) = null_flag)
#  define set_ht_running(N)       (img_height(N) = null_flag)
#  define set_dp_running(N)       (img_depth(N) = null_flag)
#  define is_wd_running(N)        (img_width(N) == null_flag)
#  define is_ht_running(N)        (img_height(N) == null_flag)
#  define is_dp_running(N)        (img_depth(N) == null_flag)

#endif