~ubuntu-branches/ubuntu/karmic/luatex/karmic

« back to all changes in this revision

Viewing changes to src/texk/web2c/luatexdir/image/writeimg.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2008-04-15 07:50:17 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080415075017-syz15wlp1f2ep6lo
Tags: 0.25.2-1
* several new upstreams
* fix poppler patch for 0.4 (etch) to work with split out libpng patch
* fix poppler patch for 0.6
* fix README.Debian-source
* add copyright statement for src/libs/md5 (needed now)
* link the luatex reference manual into /usr/share/doc/texmf to make it
  available to texdoc (Closes: #473049)
* fix doc-base section of luatex-ref manual

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Copyright (c) 1996-2002, 2005 Han The Thanh, <thanh@pdftex.org>
3
3
 
4
4
This file is part of pdfTeX.
5
 
 
6
 
pdfTeX is free software; you can redistribute it and/or modify
 
5
pdfTeX is free software;
 
6
you can redistribute it and / or modify
7
7
it under the terms of the GNU General Public License as published by
8
8
the Free Software Foundation; either version 2 of the License, or
9
9
(at your option) any later version.
17
17
along with pdfTeX; if not, write to the Free Software
18
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
 
$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/writeimg.c#17 $
 
20
$Id: writeimg.c 1125 2008-03-25 19:36:52Z hhenkel $
21
21
*/
22
22
 
 
23
#include <assert.h>
23
24
#include "ptexlib.h"
24
 
#include "image.h"
25
25
#include <kpathsea/c-auto.h>
26
26
#include <kpathsea/c-memstr.h>
27
27
 
28
 
#define bp2int(p)    round(p*(one_hundred_bp/100.0))
29
 
 
30
 
/* define image_ptr, image_array & image_limit */
31
 
define_array (image);
32
 
 
33
 
float epdf_width;
34
 
float epdf_height;
35
 
float epdf_orig_x_i;
36
 
float epdf_orig_y_i;
37
 
integer epdf_selected_page;
38
 
integer epdf_num_pages;
39
 
integer epdf_page_box;
40
 
void *epdf_doc;
41
 
 
42
 
static integer new_image_entry (void)
43
 
{
44
 
    alloc_array (image, 1, SMALL_BUF_SIZE);
45
 
    image_ptr->image_type = IMAGE_TYPE_NONE;
46
 
    image_ptr->color_type = 0;
47
 
    image_ptr->num_pages = 0;
48
 
    image_ptr->x_res = 0;
49
 
    image_ptr->y_res = 0;
50
 
    image_ptr->width = 0;
51
 
    image_ptr->height = 0;
52
 
    image_ptr->colorspace_ref = 0;
53
 
    return image_ptr++ - image_array;
54
 
}
55
 
 
56
 
integer imagecolor (integer img)
57
 
{
58
 
    return img_color (img);
59
 
}
60
 
 
61
 
integer image_width (integer img)
62
 
{
63
 
    return img_width (img);
64
 
}
65
 
 
66
 
integer image_height (integer img)
67
 
{
68
 
    return img_height (img);
69
 
}
70
 
 
71
 
integer image_x_res (integer img)
72
 
{
73
 
    return img_xres (img);
74
 
}
75
 
 
76
 
integer image_y_res (integer img)
77
 
{
78
 
    return img_yres (img);
79
 
}
80
 
 
81
 
boolean is_pdf_image (integer img)
82
 
{
83
 
    return img_type (img) == IMAGE_TYPE_PDF;
84
 
}
85
 
 
86
 
boolean check_image_b (integer procset)
87
 
{
88
 
    return procset & IMAGE_COLOR_B;
89
 
}
90
 
 
91
 
boolean check_image_c (integer procset)
92
 
{
93
 
    return procset & IMAGE_COLOR_C;
94
 
}
95
 
 
96
 
boolean check_image_i (integer procset)
97
 
{
98
 
    return procset & IMAGE_COLOR_I;
99
 
}
100
 
 
101
 
void update_image_procset (integer img)
102
 
{
103
 
    pdf_image_procset |= img_color (img);
104
 
}
105
 
 
106
 
integer epdf_orig_x (integer img)
107
 
{
108
 
    return pdf_ptr (img)->orig_x;
109
 
}
110
 
 
111
 
integer epdf_orig_y (integer img)
112
 
{
113
 
    return pdf_ptr (img)->orig_y;
114
 
}
115
 
 
116
 
integer image_pages (integer img)
117
 
{
118
 
    return img_pages (img);
119
 
}
120
 
 
121
 
integer image_colordepth (integer img)
122
 
{
123
 
    switch (img_type (img)) {
124
 
    case IMAGE_TYPE_PNG:
125
 
        return png_info (img)->bit_depth;
126
 
    case IMAGE_TYPE_JPG:
127
 
        return jpg_ptr (img)->bits_per_component;
128
 
    case IMAGE_TYPE_JBIG2:
129
 
        return 0;
130
 
    case IMAGE_TYPE_PDF:
131
 
        return 0;
132
 
    default:
133
 
        pdftex_fail ("unknown type of image");
134
 
        return -1;              /* to make the compiler happy */
135
 
    }
136
 
}
137
 
 
 
28
#include "image.h"
 
29
 
 
30
#include <../lua51/lua.h>
 
31
#include <../lua51/lauxlib.h>
 
32
 
 
33
extern void pdf_print_real(integer m, integer d);
 
34
 
 
35
#define obj_aux(A) obj_tab[(A)].int4
 
36
 
 
37
/**********************************************************************/
138
38
/*
139
39
  Patch ImageTypeDetection 2003/02/08 by Heiko Oberdiek.
140
40
 
147
47
 
148
48
  The patch now looks at the file header first regardless of
149
49
  the file extension. This is implemented in function
150
 
  "checktypebyheader". If this check fails, the traditional
 
50
  "check_type_by_header". If this check fails, the traditional
151
51
  test of standard file extension is tried, done in function
152
 
  "checktypebyextension".
 
52
  "check_type_by_extension".
153
53
 
154
54
  Magic headers:
155
55
 
171
71
  |   sequence: X'FF', SOI X'FF', APP0, <2 bytes to be skipped>,
172
72
  |   "JFIF", X'00'.
173
73
 
174
 
  Function "checktypebyheader" only looks at the first two bytes:
 
74
  Function "check_type_by_header" only looks at the first two bytes:
175
75
    "\xFF\xD8"
176
76
 
177
77
  * ISO/IEC JTC 1/SC 29/WG 1
198
98
    |   13. Acrobat viewers will also accept a header of the form
199
99
    |           %!PS-Adobe-N.n PDF-M.m
200
100
 
201
 
    The check in function "checktypebyheader" only implements
 
101
    The check in function "check_type_by_header" only implements
202
102
    the first issue. The implementation notes are not considered.
203
103
    Therefore files with garbage at start of file must have the
204
104
    standard extension.
205
105
 
206
 
    Functions "checktypebyheader" and "checktypebyextension":
207
 
    img_type(img) is set to IMAGE_TYPE_NONE by new_image_entry().
208
 
    Both functions tries to detect a type and set img_type(img).
 
106
    Functions "check_type_by_header" and "check_type_by_extension":
 
107
    img_type(img) is set to IMAGE_TYPE_NONE by new_image_dict().
 
108
    Both functions try to detect a type and set img_type(img).
209
109
    Thus a value other than IMAGE_TYPE_NONE indicates that a
210
 
    type is found.
 
110
    type has been found.
211
111
*/
212
112
 
213
113
#define HEADER_JPG "\xFF\xD8"
215
115
#define HEADER_JBIG2 "\x97\x4A\x42\x32\x0D\x0A\x1A\x0A"
216
116
#define HEADER_PDF "%PDF-1."
217
117
#define MAX_HEADER (sizeof(HEADER_PNG)-1)
218
 
static void checktypebyheader (integer img)
 
118
 
 
119
static void check_type_by_header(image_dict * idict)
219
120
{
220
121
    int i;
221
122
    FILE *file = NULL;
222
123
    char header[MAX_HEADER];
223
124
 
224
 
    if (img_type (img) != IMAGE_TYPE_NONE)      /* nothing to do */
 
125
    assert(idict != NULL);
 
126
    if (img_type(idict) != IMAGE_TYPE_NONE)     /* nothing to do */
225
127
        return;
226
 
 
227
128
    /* read the header */
228
 
    file = xfopen (img_name (img), FOPEN_RBIN_MODE);
 
129
    file = xfopen(img_filepath(idict), FOPEN_RBIN_MODE);
229
130
    for (i = 0; (unsigned) i < MAX_HEADER; i++) {
230
 
        header[i] = xgetc (file);
231
 
        if (feof (file))
232
 
            pdftex_fail ("reading image file failed");
 
131
        header[i] = xgetc(file);
 
132
        if (feof(file))
 
133
            pdftex_fail("reading image file failed");
233
134
    }
234
 
    xfclose (file, img_name (img));
235
 
 
 
135
    xfclose(file, img_filepath(idict));
236
136
    /* tests */
237
 
    if (strncmp (header, HEADER_JPG, sizeof (HEADER_JPG) - 1) == 0)
238
 
        img_type (img) = IMAGE_TYPE_JPG;
239
 
    else if (strncmp (header, HEADER_PNG, sizeof (HEADER_PNG) - 1) == 0)
240
 
        img_type (img) = IMAGE_TYPE_PNG;
241
 
    else if (strncmp (header, HEADER_JBIG2, sizeof (HEADER_JBIG2) - 1) == 0)
242
 
        img_type (img) = IMAGE_TYPE_JBIG2;
243
 
    else if (strncmp (header, HEADER_PDF, sizeof (HEADER_PDF) - 1) == 0)
244
 
        img_type (img) = IMAGE_TYPE_PDF;
 
137
    if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0)
 
138
        img_type(idict) = IMAGE_TYPE_JPG;
 
139
    else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0)
 
140
        img_type(idict) = IMAGE_TYPE_PNG;
 
141
    else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0)
 
142
        img_type(idict) = IMAGE_TYPE_JBIG2;
 
143
    else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0)
 
144
        img_type(idict) = IMAGE_TYPE_PDF;
245
145
}
246
146
 
247
 
static void checktypebyextension (integer img)
 
147
static void check_type_by_extension(image_dict * idict)
248
148
{
249
149
    char *image_suffix;
250
150
 
251
 
    if (img_type (img) != IMAGE_TYPE_NONE)      /* nothing to do */
 
151
    assert(idict != NULL);
 
152
    if (img_type(idict) != IMAGE_TYPE_NONE)     /* nothing to do */
252
153
        return;
253
154
    /* tests */
254
 
    if ((image_suffix = strrchr (cur_file_name, '.')) == 0)
255
 
        img_type (img) = IMAGE_TYPE_NONE;
256
 
    else if (strcasecmp (image_suffix, ".png") == 0)
257
 
        img_type (img) = IMAGE_TYPE_PNG;
258
 
    else if (strcasecmp (image_suffix, ".jpg") == 0 ||
259
 
             strcasecmp (image_suffix, ".jpeg") == 0)
260
 
        img_type (img) = IMAGE_TYPE_JPG;
261
 
    else if (strcasecmp (image_suffix, ".jbig2") == 0 ||
262
 
             strcasecmp (image_suffix, ".jb2") == 0)
263
 
        img_type (img) = IMAGE_TYPE_JBIG2;
264
 
    else if (strcasecmp (image_suffix, ".pdf") == 0)
265
 
        img_type (img) = IMAGE_TYPE_PDF;
266
 
}
267
 
 
268
 
integer read_image (strnumber s, integer page_num, strnumber page_name,
269
 
                   integer colorspace, integer pagebox,
270
 
                   integer pdfversion, integer pdfinclusionerrorlevel)
271
 
{
272
 
    char *dest = NULL;
273
 
    integer img = new_image_entry ();
274
 
    char *imagename;
 
155
    if ((image_suffix = strrchr(img_filename(idict), '.')) == 0)
 
156
        img_type(idict) = IMAGE_TYPE_NONE;
 
157
    else if (strcasecmp(image_suffix, ".png") == 0)
 
158
        img_type(idict) = IMAGE_TYPE_PNG;
 
159
    else if (strcasecmp(image_suffix, ".jpg") == 0 ||
 
160
             strcasecmp(image_suffix, ".jpeg") == 0)
 
161
        img_type(idict) = IMAGE_TYPE_JPG;
 
162
    else if (strcasecmp(image_suffix, ".jbig2") == 0 ||
 
163
             strcasecmp(image_suffix, ".jb2") == 0)
 
164
        img_type(idict) = IMAGE_TYPE_JBIG2;
 
165
    else if (strcasecmp(image_suffix, ".pdf") == 0)
 
166
        img_type(idict) = IMAGE_TYPE_PDF;
 
167
}
 
168
 
 
169
/**********************************************************************/
 
170
 
 
171
void init_image(image * p)
 
172
{
 
173
    assert(p != NULL);
 
174
    set_wd_running(p);
 
175
    set_ht_running(p);
 
176
    set_dp_running(p);
 
177
    img_transform(p) = 0;
 
178
    img_flags(p) = 0;
 
179
    img_unset_refered(p);       /* wd/ht/dp may be modified */
 
180
    img_unset_scaled(p);
 
181
    img_dict(p) = NULL;
 
182
    img_dictref(p) = LUA_NOREF;
 
183
}
 
184
 
 
185
image *new_image()
 
186
{
 
187
    image *p = xtalloc(1, image);
 
188
    init_image(p);
 
189
    return p;
 
190
}
 
191
 
 
192
void init_image_dict(image_dict * p)
 
193
{
 
194
    assert(p != NULL);
 
195
    img_objnum(p) = 0;
 
196
    img_index(p) = 0;
 
197
    img_xsize(p) = 0;
 
198
    img_ysize(p) = 0;
 
199
    img_xorig(p) = 0;
 
200
    img_yorig(p) = 0;
 
201
    img_xres(p) = 0;
 
202
    img_yres(p) = 0;
 
203
    img_colorspace(p) = 0;
 
204
    img_totalpages(p) = 0;
 
205
    img_pagenum(p) = 1;
 
206
    img_pagename(p) = NULL;
 
207
    img_filename(p) = NULL;
 
208
    img_filepath(p) = NULL;
 
209
    img_attr(p) = NULL;
 
210
    img_file(p) = NULL;
 
211
    img_type(p) = IMAGE_TYPE_NONE;
 
212
    img_color(p) = 0;
 
213
    img_colordepth(p) = 0;
 
214
    img_pagebox(p) = PDF_BOX_SPEC_MEDIA;
 
215
    img_state(p) = DICT_NEW;
 
216
    img_png_ptr(p) = NULL;      /* union */
 
217
}
 
218
 
 
219
image_dict *new_image_dict()
 
220
{
 
221
    image_dict *p = xtalloc(1, image_dict);
 
222
    init_image_dict(p);
 
223
    return p;
 
224
}
 
225
 
 
226
void free_dict_strings(image_dict * p)
 
227
{
 
228
    if (img_filename(p) != NULL)
 
229
        xfree(img_filename(p));
 
230
    img_filename(p) = NULL;
 
231
    if (img_filepath(p) != NULL)
 
232
        xfree(img_filepath(p));
 
233
    img_filepath(p) = NULL;
 
234
    if (img_attr(p) != NULL)
 
235
        xfree(img_attr(p));
 
236
    img_attr(p) = NULL;
 
237
    if (img_pagename(p) != NULL)
 
238
        xfree(img_pagename(p));
 
239
    img_pagename(p) = NULL;
 
240
}
 
241
 
 
242
void free_image_dict(image_dict * p)
 
243
{                               /* called from limglib.c */
 
244
    assert(img_state(p) < DICT_REFERED);
 
245
    switch (img_type(p)) {
 
246
    case IMAGE_TYPE_PDF:
 
247
        unrefPdfDocument(img_filepath(p));
 
248
        break;
 
249
    case IMAGE_TYPE_PNG:       /* assuming IMG_CLOSEINBETWEEN */
 
250
        assert(img_png_ptr(p) == NULL);
 
251
        break;
 
252
    case IMAGE_TYPE_JPG:       /* assuming IMG_CLOSEINBETWEEN */
 
253
        assert(img_jpg_ptr(p) == NULL);
 
254
        break;
 
255
    case IMAGE_TYPE_JBIG2:     /* todo: writejbig2.c cleanup */
 
256
        break;
 
257
    case IMAGE_TYPE_NONE:
 
258
        break;
 
259
    default:
 
260
        assert(0);
 
261
    }
 
262
    free_dict_strings(p);
 
263
    assert(img_file(p) == NULL);
 
264
    xfree(p);
 
265
}
 
266
 
 
267
/**********************************************************************/
 
268
 
 
269
void pdf_print_resname_prefix()
 
270
{
 
271
    if (pdf_resname_prefix != 0)
 
272
        pdf_printf(makecstring(pdf_resname_prefix));
 
273
}
 
274
 
 
275
void read_img(image_dict * idict, integer pdf_minor_version,
 
276
              integer pdf_inclusion_errorlevel)
 
277
{
 
278
    char *filepath;
275
279
    int callback_id;
276
 
    img_colorspace_ref (img) = colorspace;
277
 
 
278
 
    /* need to allocate new string as makecstring's buffer is 
279
 
       already used by cur_file_name */
280
 
    if (page_name != 0)
281
 
        dest = xstrdup (makecstring (page_name));
282
 
    cur_file_name = makecfilename (s);
283
 
    
284
 
    callback_id=callback_defined(find_image_file_callback);
285
 
    if (callback_id>0 && run_callback(callback_id,"S->S",cur_file_name,&imagename)) {
286
 
          if (imagename && !strlen(imagename))
287
 
                imagename = NULL;
288
 
      img_name (img) = imagename;
289
 
    } else {
290
 
      img_name (img) = kpse_find_file (cur_file_name, kpse_tex_format, true);
 
280
    assert(idict != NULL);
 
281
    if (img_filename(idict) == NULL)
 
282
        pdftex_fail("image file name missing");
 
283
    callback_id = callback_defined(find_image_file_callback);
 
284
    if (img_filepath(idict) == NULL) {
 
285
        if (callback_id > 0
 
286
            && run_callback(callback_id, "S->S", img_filename(idict),
 
287
                            &filepath)) {
 
288
            if (filepath && (strlen(filepath) > 0))
 
289
                img_filepath(idict) = strdup(filepath);
 
290
        } else
 
291
            img_filepath(idict) =
 
292
                kpse_find_file(img_filename(idict), kpse_tex_format, true);
291
293
    }
292
 
    if (img_name (img) == NULL)
293
 
      pdftex_fail ("cannot find image file");
294
 
    /* kpse_find_file perhaps changed the file name */
295
 
    cur_file_name = img_name (img);
296
 
 
 
294
    if (img_filepath(idict) == NULL)
 
295
        pdftex_fail("cannot find image file");
297
296
    /* type checks */
298
 
    checktypebyheader (img);
299
 
    checktypebyextension (img);
 
297
    check_type_by_header(idict);
 
298
    check_type_by_extension(idict);
300
299
    /* read image */
301
 
    switch (img_type (img)) {
302
 
    case IMAGE_TYPE_PDF:
303
 
        pdf_ptr (img) = xtalloc (1, pdf_image_struct);
304
 
        pdf_ptr (img)->page_box = pagebox;
305
 
        page_num = read_pdf_info (img_name (img), dest, page_num, pagebox,
306
 
                                  pdfversion, pdfinclusionerrorlevel);
307
 
        img_width (img) = bp2int (epdf_width);
308
 
        img_height (img) = bp2int (epdf_height);
309
 
        img_pages (img) = epdf_num_pages;
310
 
        pdf_ptr (img)->orig_x = bp2int (epdf_orig_x_i);
311
 
        pdf_ptr (img)->orig_y = bp2int (epdf_orig_y_i);
312
 
        pdf_ptr (img)->selected_page = page_num;
313
 
        pdf_ptr (img)->doc = epdf_doc;
314
 
        break;
315
 
    case IMAGE_TYPE_PNG:
316
 
        img_pages (img) = 1;
317
 
        read_png_info (img);
318
 
        break;
319
 
    case IMAGE_TYPE_JPG:
320
 
        jpg_ptr (img) = xtalloc (1, JPG_IMAGE_INFO);
321
 
        img_pages (img) = 1;
322
 
        read_jpg_info (img);
323
 
        break;
324
 
    case IMAGE_TYPE_JBIG2:
325
 
            if (pdfversion < 4) {
326
 
                  pdftex_fail
327
 
                    ("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
328
 
                     (int) pdfversion);
329
 
            }
330
 
        jbig2_ptr (img) = xtalloc (1, JBIG2_IMAGE_INFO);
331
 
        img_type (img) = IMAGE_TYPE_JBIG2;
332
 
        jbig2_ptr (img)->selected_page = page_num;
333
 
        read_jbig2_info (img);
334
 
        break;
335
 
    default:
336
 
        pdftex_fail ("unknown type of image");
337
 
    }
338
 
    xfree (dest);
339
 
    cur_file_name = NULL;
340
 
    return img;
341
 
}
342
 
 
343
 
void write_image (integer img)
344
 
{
345
 
    cur_file_name = img_name (img);
346
 
        if (tracefilenames)
347
 
          tex_printf (" <%s", img_name (img));
348
 
    switch (img_type (img)) {
349
 
    case IMAGE_TYPE_PNG:
350
 
        write_png (img);
351
 
        break;
352
 
    case IMAGE_TYPE_JPG:
353
 
        write_jpg (img);
354
 
        break;
355
 
    case IMAGE_TYPE_JBIG2:
356
 
        write_jbig2 (img);
357
 
        break;
358
 
    case IMAGE_TYPE_PDF:
359
 
        epdf_doc = pdf_ptr (img)->doc;
360
 
        epdf_selected_page = pdf_ptr (img)->selected_page;
361
 
        epdf_page_box = pdf_ptr (img)->page_box;
362
 
        write_epdf ();
363
 
        break;
364
 
    default:
365
 
        pdftex_fail ("unknown type of image");
366
 
    }
367
 
        if (tracefilenames)
368
 
          tex_printf (">");
369
 
    cur_file_name = NULL;
370
 
}
371
 
 
372
 
void delete_image (integer img)
373
 
{
374
 
    switch (img_type (img)) {
375
 
    case IMAGE_TYPE_PDF:
376
 
        epdf_doc = pdf_ptr (img)->doc;
377
 
        epdf_delete ();
378
 
        break;
379
 
    case IMAGE_TYPE_PNG:
380
 
        xfclose ((FILE *) png_ptr (img)->io_ptr, cur_file_name);
381
 
        png_destroy_read_struct (&(png_ptr (img)), &(png_info (img)), NULL);
382
 
        break;
383
 
    case IMAGE_TYPE_JPG:
384
 
        xfclose (jpg_ptr (img)->file, cur_file_name);
385
 
        break;
386
 
    case IMAGE_TYPE_JBIG2:
387
 
        break;
388
 
    default:
389
 
        pdftex_fail ("unknown type of image");
390
 
    }
391
 
    xfree (img_name (img));
392
 
    return;
393
 
}
394
 
 
395
 
void img_free ()
396
 
{
397
 
    xfree (image_array);
 
300
    switch (img_type(idict)) {
 
301
    case IMAGE_TYPE_PDF:
 
302
        read_pdf_info(idict, pdf_minor_version, pdf_inclusion_errorlevel);
 
303
        break;
 
304
    case IMAGE_TYPE_PNG:
 
305
        read_png_info(idict, IMG_CLOSEINBETWEEN);
 
306
        break;
 
307
    case IMAGE_TYPE_JPG:
 
308
        read_jpg_info(idict, IMG_CLOSEINBETWEEN);
 
309
        break;
 
310
    case IMAGE_TYPE_JBIG2:
 
311
        if (pdf_minor_version < 4) {
 
312
            pdftex_fail
 
313
                ("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
 
314
                 (int) pdf_minor_version);
 
315
        }
 
316
        read_jbig2_info(idict);
 
317
        break;
 
318
    default:
 
319
        pdftex_fail("internal error: unknown image type");
 
320
    }
 
321
    cur_file_name = NULL;
 
322
    if (img_state(idict) < DICT_FILESCANNED)
 
323
        img_state(idict) = DICT_FILESCANNED;
 
324
}
 
325
 
 
326
void scale_img(image * img)
 
327
{
 
328
    integer x, y, xr, yr;       /* size and resolution of image */
 
329
    scaled w, h;                /* indeed size corresponds to image resolution */
 
330
    integer default_res;
 
331
    assert(img != NULL);
 
332
    image_dict *idict = img_dict(img);
 
333
    assert(idict != NULL);
 
334
    x = img_xsize(idict);       /* dimensions, resolutions from image file */
 
335
    y = img_ysize(idict);
 
336
    xr = img_xres(idict);
 
337
    yr = img_yres(idict);
 
338
    if ((img_transform(img) & 1) == 1) {
 
339
        int tmp = x;
 
340
        x = y;
 
341
        y = tmp;
 
342
        tmp = xr;
 
343
        xr = yr;
 
344
        yr = tmp;
 
345
    }
 
346
    if (xr > 65535 || yr > 65535) {
 
347
        xr = 0;
 
348
        yr = 0;
 
349
        pdftex_warn("ext1: too large image resolution ignored");
 
350
    }
 
351
    if (x <= 0 || y <= 0 || xr < 0 || yr < 0)
 
352
        pdftex_fail("ext1: invalid image dimensions");
 
353
    if (img_type(idict) == IMAGE_TYPE_PDF) {
 
354
        w = x;
 
355
        h = y;
 
356
    } else {
 
357
        default_res = fix_int(get_pdf_image_resolution(), 0, 65535);
 
358
        if (default_res > 0 && (xr == 0 || yr == 0)) {
 
359
            xr = default_res;
 
360
            yr = default_res;
 
361
        }
 
362
        if (is_wd_running(img) && is_ht_running(img)) {
 
363
            if (xr > 0 && yr > 0) {
 
364
                w = ext_xn_over_d(one_hundred_inch, x, 100 * xr);
 
365
                h = ext_xn_over_d(one_hundred_inch, y, 100 * yr);
 
366
            } else {
 
367
                w = ext_xn_over_d(one_hundred_inch, x, 7200);
 
368
                h = ext_xn_over_d(one_hundred_inch, y, 7200);
 
369
            }
 
370
        }
 
371
    }
 
372
    if (is_wd_running(img) && is_ht_running(img) && is_dp_running(img)) {
 
373
        img_width(img) = w;
 
374
        img_height(img) = h;
 
375
        img_depth(img) = 0;
 
376
    } else if (is_wd_running(img)) {
 
377
        /* image depth or height is explicitly specified */
 
378
        if (is_ht_running(img)) {
 
379
            /* image depth is explicitly specified */
 
380
            img_width(img) = ext_xn_over_d(h, x, y);
 
381
            img_height(img) = h - img_depth(img);
 
382
        } else if (is_dp_running(img)) {
 
383
            /* image height is explicitly specified */
 
384
            img_width(img) = ext_xn_over_d(img_height(img), x, y);
 
385
            img_depth(img) = 0;
 
386
        } else {
 
387
            /* both image depth and height are explicitly specified */
 
388
            img_width(img) =
 
389
                ext_xn_over_d(img_height(img) + img_depth(img), x, y);
 
390
        }
 
391
    } else {
 
392
        /* image width is explicitly specified */
 
393
        if (is_ht_running(img) && is_dp_running(img)) {
 
394
            /* both image depth and height are not specified */
 
395
            img_height(img) = ext_xn_over_d(img_width(img), y, x);
 
396
            img_depth(img) = 0;
 
397
        }
 
398
        /* image depth is explicitly specified */
 
399
        else if (is_ht_running(img)) {
 
400
            img_height(img) =
 
401
                ext_xn_over_d(img_width(img), y, x) - img_depth(img);
 
402
        }
 
403
        /* image height is explicitly specified */
 
404
        else if (is_dp_running(img)) {
 
405
            img_depth(img) = 0;
 
406
        }
 
407
        /* else both image depth and height are explicitly specified */
 
408
    }
 
409
    img_set_scaled(img);
 
410
}
 
411
 
 
412
void out_img(image * img, scaled hpos, scaled vpos)
 
413
{
 
414
    float a[6];                 /* transformation matrix */
 
415
    int r;                      /* number of digits after the decimal point */
 
416
    assert(img != 0);
 
417
    image_dict *idict = img_dict(img);
 
418
    assert(idict != 0);
 
419
    scaled wd = img_width(img);
 
420
    scaled ht = img_height(img);
 
421
    scaled dp = img_depth(img);
 
422
    if (img_type(idict) == IMAGE_TYPE_PDF) {
 
423
        a[0] = wd * 1.0e6 / img_xsize(idict);
 
424
        a[1] = 0;
 
425
        a[2] = 0;
 
426
        a[3] = (ht + dp) * 1.0e6 / img_ysize(idict);
 
427
        a[4] = hpos - (float) wd *img_xorig(idict) / img_xsize(idict);
 
428
        a[5] = vpos - (float) ht *img_yorig(idict) / img_ysize(idict);
 
429
        r = 6;
 
430
    } else {
 
431
        a[0] = wd * 1.0e6 / one_hundred_bp;
 
432
        a[1] = 0;
 
433
        a[2] = 0;
 
434
        a[3] = (ht + dp) * 1.0e6 / one_hundred_bp;
 
435
        a[4] = hpos;
 
436
        a[5] = vpos;
 
437
        r = 4;
 
438
    }
 
439
    if ((img_transform(img) & 1) == 1) {
 
440
        if (ht == -dp)
 
441
            pdftex_fail("image transform: division by zero (height == -depth)");
 
442
        if (wd == 0)
 
443
            pdftex_fail("image transform: division by zero (width == 0)");
 
444
    }
 
445
    switch (img_transform(img) & 7) {
 
446
    case 0:                    /* no transform */
 
447
        break;
 
448
    case 1:                    /* rot. 90 deg. (counterclockwise) */
 
449
        a[1] = a[0] * (ht + dp) / wd;
 
450
        a[0] = 0;
 
451
        a[2] = -a[3] * wd / (ht + dp);
 
452
        a[3] = 0;
 
453
        a[4] += wd;
 
454
        break;
 
455
    case 2:                    /* rot. 180 deg. */
 
456
        a[0] = -a[0];
 
457
        a[3] = -a[3];
 
458
        a[4] += wd;
 
459
        a[5] += ht + dp;
 
460
        break;
 
461
    case 3:                    /* rot. 270 deg. */
 
462
        a[1] = -a[0] * (ht + dp) / wd;
 
463
        a[0] = 0;
 
464
        a[2] = a[3] * wd / (ht + dp);
 
465
        a[3] = 0;
 
466
        a[5] += ht + dp;
 
467
        break;
 
468
    case 4:                    /* mirrored, unrotated */
 
469
        a[0] = -a[0];
 
470
        a[4] += wd;
 
471
        break;
 
472
    case 5:                    /* mirrored, then rot. 90 deg. */
 
473
        a[1] = -a[0] * (ht + dp) / wd;
 
474
        a[0] = 0;
 
475
        a[2] = -a[3] * wd / (ht + dp);
 
476
        a[3] = 0;
 
477
        a[4] += wd;
 
478
        a[5] += ht + dp;
 
479
        break;
 
480
    case 6:                    /* mirrored, then rot. 180 deg. */
 
481
        a[3] = -a[3];
 
482
        a[5] += ht + dp;
 
483
        break;
 
484
    case 7:                    /* mirrored, then rot. 270 deg. */
 
485
        a[1] = a[0] * (ht + dp) / wd;
 
486
        a[0] = 0;
 
487
        a[2] = a[3] * wd / (ht + dp);
 
488
        a[3] = 0;
 
489
        break;
 
490
    default:
 
491
        assert(0);
 
492
    }
 
493
    pdf_end_text();
 
494
    pdf_printf("q\n");
 
495
    pdf_print_real((integer) a[0], r);
 
496
    pdfout(' ');
 
497
    pdf_print_real((integer) a[1], r);
 
498
    pdfout(' ');
 
499
    pdf_print_real((integer) a[2], r);
 
500
    pdfout(' ');
 
501
    pdf_print_real((integer) a[3], r);
 
502
    pdfout(' ');
 
503
    pdf_print_bp((integer) a[4]);
 
504
    pdfout(' ');
 
505
    pdf_print_bp((integer) a[5]);
 
506
    pdf_printf(" cm\n/Im");
 
507
    pdf_print_int(img_index(idict));
 
508
    pdf_print_resname_prefix();
 
509
    pdf_printf(" Do\nQ\n");
 
510
    if (img_state(idict) < DICT_OUTIMG)
 
511
        img_state(idict) = DICT_OUTIMG;
 
512
}
 
513
 
 
514
void write_img(image_dict * idict)
 
515
{
 
516
    assert(idict != NULL);
 
517
    if (img_state(idict) < DICT_WRITTEN) {
 
518
        if (tracefilenames)
 
519
            tex_printf(" <%s", img_filepath(idict));
 
520
        switch (img_type(idict)) {
 
521
        case IMAGE_TYPE_PNG:
 
522
            write_png(idict);
 
523
            break;
 
524
        case IMAGE_TYPE_JPG:
 
525
            write_jpg(idict);
 
526
            break;
 
527
        case IMAGE_TYPE_JBIG2:
 
528
            write_jbig2(idict);
 
529
            break;
 
530
        case IMAGE_TYPE_PDF:
 
531
            write_epdf(idict);
 
532
            break;
 
533
        default:
 
534
            pdftex_fail("internal error: unknown image type");
 
535
        }
 
536
        if (tracefilenames)
 
537
            tex_printf(">");
 
538
    }
 
539
    if (img_state(idict) < DICT_WRITTEN)
 
540
        img_state(idict) = DICT_WRITTEN;
 
541
}
 
542
 
 
543
/**********************************************************************/
 
544
 
 
545
typedef image *img_entry;
 
546
/* define img_ptr, img_array, & img_limit */
 
547
define_array(img);              /* array of pointers to image structures */
 
548
 
 
549
integer img_to_array(image * img)
 
550
{
 
551
    assert(img != NULL);
 
552
    alloc_array(img, 1, SMALL_BUF_SIZE);
 
553
    *img_ptr = img;
 
554
    return img_ptr++ - img_array;       /* now img is read-only */
 
555
}
 
556
 
 
557
/**********************************************************************/
 
558
/* stuff to be accessible from TeX */
 
559
 
 
560
integer read_image(integer objnum, integer index, strnumber filename,
 
561
                   integer page_num,
 
562
                   strnumber attr,
 
563
                   strnumber page_name,
 
564
                   integer colorspace, integer page_box,
 
565
                   integer pdf_minor_version, integer pdf_inclusion_errorlevel)
 
566
{
 
567
    integer ref;
 
568
    image *a = new_image();
 
569
    ref = img_to_array(a);
 
570
    image_dict *idict = img_dict(a) = new_image_dict();
 
571
    assert(idict != NULL);
 
572
    img_objnum(idict) = objnum;
 
573
    img_index(idict) = index;
 
574
    /* img_xsize, img_ysize, img_xres, img_yres set by read_img() */
 
575
    img_colorspace(idict) = colorspace;
 
576
    img_pagenum(idict) = page_num;
 
577
    /* img_totalpages set by read_img() */
 
578
    if (page_name != 0)
 
579
        img_pagename(idict) = xstrdup(makecstring(page_name));
 
580
    cur_file_name = makecfilename(filename);
 
581
    assert(cur_file_name != NULL);
 
582
    img_filename(idict) = xstrdup(cur_file_name);
 
583
    if (attr != 0)
 
584
        img_attr(idict) = xstrdup(makecstring(attr));
 
585
    img_pagebox(idict) = page_box;
 
586
    read_img(idict, pdf_minor_version, pdf_inclusion_errorlevel);
 
587
    img_unset_scaled(a);
 
588
    img_set_refered(a);
 
589
    return ref;
 
590
}
 
591
 
 
592
void set_image_dimensions(integer ref, integer wd, integer ht, integer dp)
 
593
{
 
594
    image *a = img_array[ref];
 
595
    img_width(a) = wd;
 
596
    img_height(a) = ht;
 
597
    img_depth(a) = dp;
 
598
}
 
599
 
 
600
void scale_image(integer ref)
 
601
{
 
602
    scale_img(img_array[ref]);
 
603
}
 
604
 
 
605
void out_image(integer ref, scaled hpos, scaled vpos)
 
606
{
 
607
    image *a = img_array[ref];
 
608
    out_img(a, hpos, vpos);
 
609
}
 
610
 
 
611
void write_image(integer ref)
 
612
{
 
613
    write_img(img_dict(img_array[ref]));
 
614
}
 
615
 
 
616
integer image_pages(integer ref)
 
617
{
 
618
    return img_totalpages(img_dict(img_array[ref]));
 
619
}
 
620
 
 
621
integer image_colordepth(integer ref)
 
622
{
 
623
    return img_colordepth(img_dict(img_array[ref]));
 
624
}
 
625
 
 
626
integer epdf_orig_x(integer ref)
 
627
{
 
628
    return img_xorig(img_dict(img_array[ref]));
 
629
}
 
630
 
 
631
integer epdf_orig_y(integer ref)
 
632
{
 
633
    return img_yorig(img_dict(img_array[ref]));
 
634
}
 
635
 
 
636
integer image_objnum(integer ref)
 
637
{
 
638
    return img_objnum(img_dict(img_array[ref]));
 
639
}
 
640
 
 
641
integer image_index(integer ref)
 
642
{
 
643
    return img_index(img_dict(img_array[ref]));
 
644
}
 
645
 
 
646
integer image_width(integer ref)
 
647
{
 
648
    return img_width(img_array[ref]);
 
649
}
 
650
 
 
651
integer image_height(integer ref)
 
652
{
 
653
    return img_height(img_array[ref]);
 
654
}
 
655
 
 
656
integer image_depth(integer ref)
 
657
{
 
658
    return img_depth(img_array[ref]);
 
659
}
 
660
 
 
661
void update_image_procset(integer ref)
 
662
{
 
663
    pdf_image_procset |= img_color(img_dict(img_array[ref]));
 
664
}
 
665
 
 
666
boolean check_image_b(integer procset)
 
667
{
 
668
    return procset & IMAGE_COLOR_B;
 
669
}
 
670
 
 
671
boolean check_image_c(integer procset)
 
672
{
 
673
    return procset & IMAGE_COLOR_C;
 
674
}
 
675
 
 
676
boolean check_image_i(integer procset)
 
677
{
 
678
    return procset & IMAGE_COLOR_I;
398
679
}