Mon, 26 Aug 2002 05:43:49 +0000
fixed 'middle-endian' output from TIFFReadScanline
eric@18 | 1 | #include <stdlib.h> |
eric@18 | 2 | #include <string.h> |
eric@16 | 3 | #include <stdio.h> |
eric@16 | 4 | |
eric@16 | 5 | #include "type.h" |
eric@18 | 6 | #include "semantics.h" |
eric@16 | 7 | #include "parser.tab.h" |
eric@44 | 8 | #include "t2p.h" |
eric@16 | 9 | |
eric@16 | 10 | |
eric@22 | 11 | typedef struct |
eric@22 | 12 | { |
eric@32 | 13 | boolean has_page_size; |
eric@32 | 14 | page_size_t page_size; |
eric@22 | 15 | |
eric@22 | 16 | boolean has_rotation; |
eric@22 | 17 | int rotation; |
eric@22 | 18 | |
eric@22 | 19 | boolean has_crop; |
eric@22 | 20 | crop_t crop; |
eric@22 | 21 | } input_modifiers_t; |
eric@22 | 22 | |
eric@22 | 23 | |
eric@22 | 24 | typedef struct input_context_t |
eric@22 | 25 | { |
eric@22 | 26 | struct input_context_t *parent; |
eric@22 | 27 | struct input_context_t *next; |
eric@22 | 28 | |
eric@22 | 29 | int image_count; /* how many pages reference this context, |
eric@22 | 30 | including those from subcontexts */ |
eric@22 | 31 | |
eric@22 | 32 | char *input_file; |
eric@22 | 33 | |
eric@22 | 34 | input_modifiers_t modifiers [INPUT_MODIFIER_TYPE_COUNT]; |
eric@22 | 35 | } input_context_t; |
eric@22 | 36 | |
eric@22 | 37 | |
eric@22 | 38 | typedef struct input_image_t |
eric@22 | 39 | { |
eric@22 | 40 | struct input_image_t *next; |
eric@22 | 41 | input_context_t *input_context; |
eric@22 | 42 | range_t range; |
eric@22 | 43 | } input_image_t; |
eric@22 | 44 | |
eric@22 | 45 | |
eric@22 | 46 | typedef struct output_context_t |
eric@22 | 47 | { |
eric@22 | 48 | struct output_context_t *parent; |
eric@22 | 49 | struct output_context_t *next; |
eric@22 | 50 | |
eric@22 | 51 | int page_count; /* how many pages reference this context, |
eric@22 | 52 | including those from subcontexts */ |
eric@22 | 53 | |
eric@22 | 54 | char *output_file; |
eric@30 | 55 | pdf_file_attributes_t file_attributes; |
eric@30 | 56 | |
eric@22 | 57 | bookmark_t *first_bookmark; |
eric@22 | 58 | bookmark_t *last_bookmark; |
eric@25 | 59 | |
eric@25 | 60 | boolean has_page_label; |
eric@25 | 61 | page_label_t page_label; |
eric@22 | 62 | } output_context_t; |
eric@22 | 63 | |
eric@22 | 64 | |
eric@22 | 65 | typedef struct output_page_t |
eric@22 | 66 | { |
eric@22 | 67 | struct output_page_t *next; |
eric@22 | 68 | output_context_t *output_context; |
eric@22 | 69 | range_t range; |
eric@22 | 70 | bookmark_t *bookmark_list; |
eric@22 | 71 | } output_page_t; |
eric@22 | 72 | |
eric@22 | 73 | |
eric@36 | 74 | #undef SEMANTIC_DEBUG |
eric@36 | 75 | |
eric@19 | 76 | #ifdef SEMANTIC_DEBUG |
eric@19 | 77 | #define SDBG(x) printf x |
eric@19 | 78 | #else |
eric@19 | 79 | #define SDBG(x) |
eric@19 | 80 | #endif |
eric@19 | 81 | |
eric@19 | 82 | |
eric@16 | 83 | FILE *yyin; |
eric@16 | 84 | int line; /* line number in spec file */ |
eric@16 | 85 | |
eric@24 | 86 | int bookmark_level; |
eric@16 | 87 | |
eric@20 | 88 | input_context_t *first_input_context; |
eric@20 | 89 | input_context_t *last_input_context; |
eric@20 | 90 | |
eric@20 | 91 | input_modifier_type_t current_modifier_context; |
eric@18 | 92 | |
eric@20 | 93 | input_image_t *first_input_image; |
eric@20 | 94 | input_image_t *last_input_image; |
eric@18 | 95 | |
eric@20 | 96 | output_context_t *first_output_context; |
eric@20 | 97 | output_context_t *last_output_context; |
eric@20 | 98 | |
eric@20 | 99 | output_page_t *first_output_page; |
eric@20 | 100 | output_page_t *last_output_page; |
eric@16 | 101 | |
eric@16 | 102 | |
eric@19 | 103 | void input_push_context (void) |
eric@16 | 104 | { |
eric@18 | 105 | input_context_t *new_input_context; |
eric@18 | 106 | |
eric@18 | 107 | new_input_context = malloc (sizeof (input_context_t)); |
eric@18 | 108 | if (! new_input_context) |
eric@18 | 109 | { |
eric@20 | 110 | fprintf (stderr, "failed to malloc an input context\n"); |
eric@18 | 111 | return; |
eric@18 | 112 | } |
eric@18 | 113 | |
eric@20 | 114 | if (last_input_context) |
eric@18 | 115 | { |
eric@20 | 116 | memcpy (new_input_context, last_input_context, sizeof (input_context_t)); |
eric@20 | 117 | new_input_context->image_count = 0; |
eric@18 | 118 | } |
eric@18 | 119 | else |
eric@20 | 120 | { |
eric@20 | 121 | memset (new_input_context, 0, sizeof (input_context_t)); |
eric@20 | 122 | first_input_context = new_input_context; |
eric@20 | 123 | } |
eric@18 | 124 | |
eric@20 | 125 | new_input_context->parent = last_input_context; |
eric@20 | 126 | last_input_context = new_input_context; |
eric@16 | 127 | }; |
eric@16 | 128 | |
eric@16 | 129 | void input_pop_context (void) |
eric@16 | 130 | { |
eric@20 | 131 | if (! last_input_context) |
eric@18 | 132 | { |
eric@18 | 133 | fprintf (stderr, "failed to pop an input context\n"); |
eric@18 | 134 | return; |
eric@18 | 135 | } |
eric@18 | 136 | |
eric@20 | 137 | last_input_context = last_input_context->parent; |
eric@16 | 138 | }; |
eric@16 | 139 | |
eric@19 | 140 | void input_set_modifier_context (input_modifier_type_t type) |
eric@19 | 141 | { |
eric@19 | 142 | current_modifier_context = type; |
eric@19 | 143 | #ifdef SEMANTIC_DEBUG |
eric@19 | 144 | SDBG(("modifier type ")); |
eric@19 | 145 | switch (type) |
eric@19 | 146 | { |
eric@19 | 147 | case INPUT_MODIFIER_ALL: SDBG(("all")); break; |
eric@19 | 148 | case INPUT_MODIFIER_ODD: SDBG(("odd")); break; |
eric@19 | 149 | case INPUT_MODIFIER_EVEN: SDBG(("even")); break; |
eric@19 | 150 | default: SDBG(("unknown %d", type)); |
eric@19 | 151 | } |
eric@19 | 152 | SDBG(("\n")); |
eric@19 | 153 | #endif /* SEMANTIC_DEBUG */ |
eric@19 | 154 | } |
eric@19 | 155 | |
eric@22 | 156 | static void input_clone (void) |
eric@20 | 157 | { |
eric@20 | 158 | input_context_t *new_input_context; |
eric@20 | 159 | |
eric@20 | 160 | if (! last_input_context->image_count) |
eric@20 | 161 | return; |
eric@20 | 162 | |
eric@20 | 163 | new_input_context = malloc (sizeof (input_context_t)); |
eric@20 | 164 | if (! new_input_context) |
eric@20 | 165 | { |
eric@20 | 166 | fprintf (stderr, "failed to malloc an input context\n"); |
eric@20 | 167 | return; |
eric@20 | 168 | } |
eric@20 | 169 | |
eric@20 | 170 | memcpy (new_input_context, last_input_context, sizeof (input_context_t)); |
eric@20 | 171 | new_input_context->image_count = 0; |
eric@20 | 172 | last_input_context->next = new_input_context; |
eric@45 | 173 | last_input_context = new_input_context; |
eric@20 | 174 | } |
eric@20 | 175 | |
eric@16 | 176 | void input_set_file (char *name) |
eric@16 | 177 | { |
eric@20 | 178 | input_clone (); |
eric@20 | 179 | last_input_context->input_file = name; |
eric@16 | 180 | }; |
eric@16 | 181 | |
eric@19 | 182 | void input_set_rotation (int rotation) |
eric@19 | 183 | { |
eric@20 | 184 | last_input_context->modifiers [current_modifier_context].has_rotation = 1; |
eric@20 | 185 | last_input_context->modifiers [current_modifier_context].rotation = rotation; |
eric@19 | 186 | SDBG(("rotation %d\n", rotation)); |
eric@19 | 187 | } |
eric@19 | 188 | |
eric@32 | 189 | void input_set_page_size (page_size_t size) |
eric@32 | 190 | { |
eric@32 | 191 | last_input_context->modifiers [current_modifier_context].has_page_size = 1; |
eric@32 | 192 | last_input_context->modifiers [current_modifier_context].page_size = size; |
eric@32 | 193 | SDBG(("page size %f, %f\n", size.width, size.height)); |
eric@32 | 194 | } |
eric@32 | 195 | |
eric@22 | 196 | static void increment_input_image_count (int count) |
eric@16 | 197 | { |
eric@20 | 198 | input_context_t *context; |
eric@20 | 199 | |
eric@20 | 200 | for (context = last_input_context; context; context = context->parent) |
eric@20 | 201 | context->image_count += count; |
eric@20 | 202 | } |
eric@20 | 203 | |
eric@20 | 204 | void input_images (range_t range) |
eric@20 | 205 | { |
eric@20 | 206 | input_image_t *new_image; |
eric@20 | 207 | int count = ((range.last - range.first) + 1); |
eric@20 | 208 | |
eric@19 | 209 | #ifdef SEMANTIC_DEBUG |
eric@20 | 210 | if (range.first == range.last) |
eric@20 | 211 | SDBG(("image %d\n", range.first)); |
eric@16 | 212 | else |
eric@20 | 213 | SDBG(("images %d..%d\n", range.first, range.last)); |
eric@19 | 214 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 215 | |
eric@20 | 216 | new_image = calloc (1, sizeof (input_image_t)); |
eric@20 | 217 | if (! new_image) |
eric@20 | 218 | { |
eric@20 | 219 | fprintf (stderr, "failed to malloc an input image struct\n"); |
eric@20 | 220 | return; |
eric@20 | 221 | } |
eric@20 | 222 | if (first_input_image) |
eric@20 | 223 | { |
eric@20 | 224 | last_input_image->next = new_image; |
eric@20 | 225 | last_input_image = new_image; |
eric@20 | 226 | } |
eric@20 | 227 | else |
eric@20 | 228 | { |
eric@20 | 229 | first_input_image = last_input_image = new_image; |
eric@20 | 230 | } |
eric@20 | 231 | new_image->range = range; |
eric@20 | 232 | new_image->input_context = last_input_context; |
eric@20 | 233 | increment_input_image_count (count); |
eric@16 | 234 | } |
eric@16 | 235 | |
eric@18 | 236 | |
eric@18 | 237 | void output_push_context (void) |
eric@18 | 238 | { |
eric@20 | 239 | output_context_t *new_output_context; |
eric@20 | 240 | |
eric@20 | 241 | new_output_context = malloc (sizeof (output_context_t)); |
eric@20 | 242 | if (! new_output_context) |
eric@20 | 243 | { |
eric@20 | 244 | fprintf (stderr, "failed to malloc an output context\n"); |
eric@20 | 245 | return; |
eric@20 | 246 | } |
eric@20 | 247 | |
eric@20 | 248 | if (last_output_context) |
eric@20 | 249 | { |
eric@20 | 250 | memcpy (new_output_context, last_output_context, sizeof (output_context_t)); |
eric@20 | 251 | new_output_context->page_count = 0; |
eric@21 | 252 | new_output_context->first_bookmark = NULL; |
eric@21 | 253 | new_output_context->last_bookmark = NULL; |
eric@20 | 254 | } |
eric@20 | 255 | else |
eric@20 | 256 | { |
eric@20 | 257 | memset (new_output_context, 0, sizeof (output_context_t)); |
eric@20 | 258 | first_output_context = new_output_context; |
eric@20 | 259 | } |
eric@20 | 260 | |
eric@20 | 261 | new_output_context->parent = last_output_context; |
eric@20 | 262 | last_output_context = new_output_context; |
eric@18 | 263 | }; |
eric@18 | 264 | |
eric@20 | 265 | void output_pop_context (void) |
eric@20 | 266 | { |
eric@20 | 267 | if (! last_output_context) |
eric@20 | 268 | { |
eric@20 | 269 | fprintf (stderr, "failed to pop an output context\n"); |
eric@20 | 270 | return; |
eric@20 | 271 | } |
eric@20 | 272 | |
eric@20 | 273 | last_output_context = last_output_context->parent; |
eric@20 | 274 | }; |
eric@20 | 275 | |
eric@22 | 276 | static void output_clone (void) |
eric@20 | 277 | { |
eric@20 | 278 | output_context_t *new_output_context; |
eric@20 | 279 | |
eric@20 | 280 | if (! last_output_context->page_count) |
eric@20 | 281 | return; |
eric@20 | 282 | |
eric@20 | 283 | new_output_context = malloc (sizeof (output_context_t)); |
eric@20 | 284 | if (! new_output_context) |
eric@20 | 285 | { |
eric@20 | 286 | fprintf (stderr, "failed to malloc an output context\n"); |
eric@20 | 287 | return; |
eric@20 | 288 | } |
eric@20 | 289 | |
eric@20 | 290 | memcpy (new_output_context, last_output_context, sizeof (output_context_t)); |
eric@20 | 291 | new_output_context->page_count = 0; |
eric@20 | 292 | last_output_context->next = new_output_context; |
eric@20 | 293 | } |
eric@20 | 294 | |
eric@16 | 295 | void output_set_file (char *name) |
eric@16 | 296 | { |
eric@20 | 297 | output_clone (); |
eric@20 | 298 | last_output_context->output_file = name; |
eric@30 | 299 | last_output_context->file_attributes.author = NULL; |
eric@30 | 300 | last_output_context->file_attributes.creator = NULL; |
eric@30 | 301 | last_output_context->file_attributes.title = NULL; |
eric@30 | 302 | last_output_context->file_attributes.subject = NULL; |
eric@30 | 303 | last_output_context->file_attributes.keywords = NULL; |
eric@16 | 304 | }; |
eric@16 | 305 | |
eric@30 | 306 | void output_set_author (char *author) |
eric@30 | 307 | { |
eric@30 | 308 | last_output_context->file_attributes.author = author; |
eric@30 | 309 | } |
eric@30 | 310 | |
eric@30 | 311 | void output_set_creator (char *creator) |
eric@30 | 312 | { |
eric@30 | 313 | last_output_context->file_attributes.creator = creator; |
eric@30 | 314 | } |
eric@30 | 315 | |
eric@30 | 316 | void output_set_title (char *title) |
eric@30 | 317 | { |
eric@30 | 318 | last_output_context->file_attributes.title = title; |
eric@30 | 319 | } |
eric@30 | 320 | |
eric@30 | 321 | void output_set_subject (char *subject) |
eric@30 | 322 | { |
eric@30 | 323 | last_output_context->file_attributes.subject = subject; |
eric@30 | 324 | } |
eric@30 | 325 | |
eric@30 | 326 | void output_set_keywords (char *keywords) |
eric@30 | 327 | { |
eric@30 | 328 | last_output_context->file_attributes.keywords = keywords; |
eric@30 | 329 | } |
eric@30 | 330 | |
eric@20 | 331 | void output_set_bookmark (char *name) |
eric@16 | 332 | { |
eric@20 | 333 | bookmark_t *new_bookmark; |
eric@20 | 334 | |
eric@20 | 335 | /* As the language is defined (parser.y), a bookmark can only appear |
eric@20 | 336 | at the beginning of a context! */ |
eric@20 | 337 | if (last_output_context->page_count) |
eric@20 | 338 | { |
eric@20 | 339 | fprintf (stderr, "internal error, bookmark not at beginning of context\n"); |
eric@20 | 340 | exit (2); |
eric@20 | 341 | } |
eric@20 | 342 | |
eric@20 | 343 | new_bookmark = calloc (1, sizeof (bookmark_t)); |
eric@20 | 344 | if (! new_bookmark) |
eric@20 | 345 | { |
eric@20 | 346 | fprintf (stderr, "failed to calloc a bookmark\n"); |
eric@20 | 347 | return; |
eric@20 | 348 | } |
eric@20 | 349 | |
eric@24 | 350 | new_bookmark->level = bookmark_level; |
eric@20 | 351 | new_bookmark->name = name; |
eric@20 | 352 | if (last_output_context->first_bookmark) |
eric@20 | 353 | last_output_context->last_bookmark->next = new_bookmark; |
eric@20 | 354 | else |
eric@20 | 355 | last_output_context->first_bookmark = new_bookmark; |
eric@20 | 356 | last_output_context->last_bookmark = new_bookmark; |
eric@20 | 357 | } |
eric@20 | 358 | |
eric@25 | 359 | void output_set_page_label (page_label_t label) |
eric@20 | 360 | { |
eric@20 | 361 | output_clone (); |
eric@25 | 362 | last_output_context->has_page_label = 1; |
eric@25 | 363 | last_output_context->page_label = label; |
eric@20 | 364 | } |
eric@20 | 365 | |
eric@22 | 366 | static void increment_output_page_count (int count) |
eric@20 | 367 | { |
eric@20 | 368 | output_context_t *context; |
eric@20 | 369 | |
eric@20 | 370 | for (context = last_output_context; context; context = context->parent) |
eric@20 | 371 | context->page_count += count; |
eric@20 | 372 | } |
eric@20 | 373 | |
eric@21 | 374 | |
eric@20 | 375 | void output_pages (range_t range) |
eric@20 | 376 | { |
eric@20 | 377 | output_page_t *new_page; |
eric@21 | 378 | output_context_t *context; |
eric@20 | 379 | int count = ((range.last - range.first) + 1); |
eric@20 | 380 | |
eric@19 | 381 | #ifdef SEMANTIC_DEBUG |
eric@20 | 382 | if (range.first == range.last) |
eric@20 | 383 | SDBG(("page %d\n", range.first)); |
eric@16 | 384 | else |
eric@20 | 385 | SDBG(("pages %d..%d\n", range.first, range.last)); |
eric@19 | 386 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 387 | |
eric@20 | 388 | new_page = calloc (1, sizeof (output_page_t)); |
eric@20 | 389 | if (! new_page) |
eric@20 | 390 | { |
eric@20 | 391 | fprintf (stderr, "failed to malloc an output page struct\n"); |
eric@20 | 392 | return; |
eric@20 | 393 | } |
eric@20 | 394 | if (first_output_page) |
eric@20 | 395 | { |
eric@20 | 396 | last_output_page->next = new_page; |
eric@20 | 397 | last_output_page = new_page; |
eric@20 | 398 | } |
eric@20 | 399 | else |
eric@20 | 400 | { |
eric@20 | 401 | first_output_page = last_output_page = new_page; |
eric@20 | 402 | } |
eric@20 | 403 | new_page->range = range; |
eric@20 | 404 | new_page->output_context = last_output_context; |
eric@20 | 405 | |
eric@21 | 406 | /* transfer bookmarks from context(s) to page */ |
eric@21 | 407 | for (context = last_output_context; context; context = context->parent) |
eric@21 | 408 | if (context->first_bookmark) |
eric@21 | 409 | { |
eric@21 | 410 | context->last_bookmark->next = new_page->bookmark_list; |
eric@21 | 411 | new_page->bookmark_list = context->first_bookmark; |
eric@21 | 412 | context->first_bookmark = NULL; |
eric@21 | 413 | context->last_bookmark = NULL; |
eric@21 | 414 | } |
eric@20 | 415 | |
eric@20 | 416 | increment_output_page_count (count); |
eric@16 | 417 | } |
eric@16 | 418 | |
eric@16 | 419 | |
eric@16 | 420 | void yyerror (char *s) |
eric@16 | 421 | { |
eric@16 | 422 | fprintf (stderr, "%d: %s\n", line, s); |
eric@16 | 423 | } |
eric@16 | 424 | |
eric@16 | 425 | |
eric@22 | 426 | static char *get_input_file (input_context_t *context) |
eric@20 | 427 | { |
eric@20 | 428 | for (; context; context = context->parent) |
eric@20 | 429 | if (context->input_file) |
eric@20 | 430 | return (context->input_file); |
eric@20 | 431 | fprintf (stderr, "no input file name found\n"); |
eric@20 | 432 | exit (2); |
eric@20 | 433 | } |
eric@20 | 434 | |
eric@32 | 435 | static boolean get_input_rotation (input_context_t *context, |
eric@32 | 436 | input_modifier_type_t type, |
eric@32 | 437 | int *rotation) |
eric@20 | 438 | { |
eric@20 | 439 | for (; context; context = context->parent) |
eric@20 | 440 | { |
eric@20 | 441 | if (context->modifiers [type].has_rotation) |
eric@32 | 442 | { |
eric@32 | 443 | * rotation = context->modifiers [type].rotation; |
eric@32 | 444 | return (1); |
eric@32 | 445 | } |
eric@20 | 446 | if (context->modifiers [INPUT_MODIFIER_ALL].has_rotation) |
eric@32 | 447 | { |
eric@32 | 448 | * rotation = context->modifiers [INPUT_MODIFIER_ALL].rotation; |
eric@32 | 449 | return (1); |
eric@32 | 450 | } |
eric@32 | 451 | } |
eric@32 | 452 | return (0); /* default */ |
eric@32 | 453 | } |
eric@32 | 454 | |
eric@32 | 455 | static boolean get_input_page_size (input_context_t *context, |
eric@32 | 456 | input_modifier_type_t type, |
eric@32 | 457 | page_size_t *page_size) |
eric@32 | 458 | { |
eric@32 | 459 | for (; context; context = context->parent) |
eric@32 | 460 | { |
eric@32 | 461 | if (context->modifiers [type].has_page_size) |
eric@32 | 462 | { |
eric@32 | 463 | * page_size = context->modifiers [type].page_size; |
eric@32 | 464 | return (1); |
eric@32 | 465 | } |
eric@32 | 466 | if (context->modifiers [INPUT_MODIFIER_ALL].has_page_size) |
eric@32 | 467 | { |
eric@32 | 468 | * page_size = context->modifiers [INPUT_MODIFIER_ALL].page_size; |
eric@32 | 469 | return (1); |
eric@32 | 470 | } |
eric@20 | 471 | } |
eric@20 | 472 | return (0); /* default */ |
eric@20 | 473 | } |
eric@20 | 474 | |
eric@22 | 475 | static char *get_output_file (output_context_t *context) |
eric@20 | 476 | { |
eric@20 | 477 | for (; context; context = context->parent) |
eric@20 | 478 | if (context->output_file) |
eric@20 | 479 | return (context->output_file); |
eric@30 | 480 | fprintf (stderr, "no output file found\n"); |
eric@30 | 481 | exit (2); |
eric@30 | 482 | } |
eric@30 | 483 | |
eric@30 | 484 | static pdf_file_attributes_t *get_output_file_attributes (output_context_t *context) |
eric@30 | 485 | { |
eric@30 | 486 | for (; context; context = context->parent) |
eric@30 | 487 | if (context->output_file) |
eric@30 | 488 | return (& context->file_attributes); |
eric@30 | 489 | fprintf (stderr, "no output file found\n"); |
eric@20 | 490 | exit (2); |
eric@20 | 491 | } |
eric@20 | 492 | |
eric@25 | 493 | static page_label_t *get_output_page_label (output_context_t *context) |
eric@20 | 494 | { |
eric@20 | 495 | for (; context; context = context->parent) |
eric@25 | 496 | if (context->has_page_label) |
eric@25 | 497 | return (& context->page_label); |
eric@20 | 498 | return (NULL); /* default */ |
eric@20 | 499 | } |
eric@20 | 500 | |
eric@20 | 501 | |
eric@20 | 502 | #ifdef SEMANTIC_DEBUG |
eric@20 | 503 | void dump_input_tree (void) |
eric@20 | 504 | { |
eric@20 | 505 | input_image_t *image; |
eric@20 | 506 | int i; |
eric@20 | 507 | |
eric@20 | 508 | printf ("input images:\n"); |
eric@20 | 509 | for (image = first_input_image; image; image = image->next) |
eric@20 | 510 | for (i = image->range.first; i <= image->range.last; i++) |
eric@20 | 511 | { |
eric@20 | 512 | input_modifier_type_t parity = (i % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN; |
eric@32 | 513 | boolean has_rotation, has_page_size; |
eric@32 | 514 | int rotation; |
eric@32 | 515 | page_size_t page_size; |
eric@32 | 516 | |
eric@32 | 517 | has_rotation = get_input_rotation (image->input_context, |
eric@32 | 518 | parity, |
eric@32 | 519 | & rotation); |
eric@32 | 520 | has_page_size = get_input_page_size (image->input_context, |
eric@32 | 521 | parity, |
eric@32 | 522 | & page_size); |
eric@32 | 523 | printf ("file '%s' image %d", |
eric@20 | 524 | get_input_file (image->input_context), |
eric@32 | 525 | i); |
eric@32 | 526 | if (has_rotation) |
eric@32 | 527 | printf (" rotation %d", rotation); |
eric@32 | 528 | if (has_page_size) |
eric@32 | 529 | printf (" size %f, %f", page_size.width, page_size.height); |
eric@32 | 530 | printf ("\n"); |
eric@45 | 531 | printf ("context: %08x\n", image->input_context); |
eric@20 | 532 | } |
eric@20 | 533 | } |
eric@20 | 534 | |
eric@20 | 535 | void dump_output_tree (void) |
eric@20 | 536 | { |
eric@20 | 537 | int i; |
eric@20 | 538 | output_page_t *page; |
eric@20 | 539 | bookmark_t *bookmark; |
eric@20 | 540 | |
eric@20 | 541 | printf ("output pages:\n"); |
eric@20 | 542 | for (page = first_output_page; page; page = page->next) |
eric@20 | 543 | { |
eric@20 | 544 | if (page->bookmark_list) |
eric@20 | 545 | { |
eric@20 | 546 | for (bookmark = page->bookmark_list; bookmark; bookmark = bookmark->next) |
eric@25 | 547 | printf ("bookmark %d \"%s\"\n", bookmark->level, bookmark->name); |
eric@20 | 548 | } |
eric@20 | 549 | for (i = page->range.first; i <= page->range.last; i++) |
eric@20 | 550 | { |
eric@25 | 551 | page_label_t *label = get_output_page_label (page->output_context); |
eric@25 | 552 | printf ("file \"%s\" ", get_output_file (page->output_context)); |
eric@25 | 553 | if (label) |
eric@25 | 554 | { |
eric@25 | 555 | printf ("label "); |
eric@25 | 556 | if (label->prefix) |
eric@25 | 557 | printf ("\"%s\" ", label->prefix); |
eric@25 | 558 | if (label->style) |
eric@25 | 559 | printf ("'%c' ", label->style); |
eric@25 | 560 | } |
eric@20 | 561 | printf ("page %d\n", i); |
eric@20 | 562 | } |
eric@20 | 563 | } |
eric@20 | 564 | } |
eric@20 | 565 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 566 | |
eric@25 | 567 | |
eric@25 | 568 | static inline int range_count (range_t range) |
eric@25 | 569 | { |
eric@25 | 570 | return ((range.last - range.first) + 1); |
eric@25 | 571 | } |
eric@25 | 572 | |
eric@25 | 573 | |
eric@16 | 574 | boolean parse_spec_file (char *fn) |
eric@16 | 575 | { |
eric@16 | 576 | boolean result = 0; |
eric@16 | 577 | |
eric@16 | 578 | yyin = fopen (fn, "r"); |
eric@16 | 579 | if (! yyin) |
eric@16 | 580 | { |
eric@16 | 581 | fprintf (stderr, "can't open spec file '%s'\n", fn); |
eric@16 | 582 | goto fail; |
eric@16 | 583 | } |
eric@16 | 584 | |
eric@16 | 585 | line = 1; |
eric@16 | 586 | |
eric@20 | 587 | input_push_context (); /* create root input context */ |
eric@20 | 588 | input_push_context (); /* create inital input context */ |
eric@20 | 589 | |
eric@20 | 590 | output_push_context (); /* create root output context */ |
eric@18 | 591 | output_push_context (); /* create initial output context */ |
eric@18 | 592 | |
eric@16 | 593 | yyparse (); |
eric@16 | 594 | |
eric@20 | 595 | if (first_input_context->image_count != first_output_context->page_count) |
eric@18 | 596 | { |
eric@20 | 597 | fprintf (stderr, "input image count %d != output page count %d\n", |
eric@20 | 598 | first_input_context->image_count, |
eric@20 | 599 | first_output_context->page_count); |
eric@18 | 600 | goto fail; |
eric@18 | 601 | } |
eric@18 | 602 | |
eric@20 | 603 | fprintf (stderr, "%d pages specified\n", first_input_context->image_count); |
eric@18 | 604 | |
eric@16 | 605 | result = 1; |
eric@16 | 606 | |
eric@20 | 607 | #ifdef SEMANTIC_DEBUG |
eric@20 | 608 | dump_input_tree (); |
eric@20 | 609 | dump_output_tree (); |
eric@20 | 610 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 611 | |
eric@16 | 612 | fail: |
eric@16 | 613 | if (yyin) |
eric@16 | 614 | fclose (yyin); |
eric@16 | 615 | |
eric@16 | 616 | return (result); |
eric@16 | 617 | } |
eric@26 | 618 | |
eric@26 | 619 | |
eric@26 | 620 | boolean process_specs (void) |
eric@26 | 621 | { |
eric@26 | 622 | input_image_t *image = NULL; |
eric@26 | 623 | output_page_t *page = NULL; |
eric@26 | 624 | int i = 0; |
eric@26 | 625 | int p = 0; |
eric@26 | 626 | int page_index = 0; |
eric@26 | 627 | input_attributes_t input_attributes; |
eric@26 | 628 | input_modifier_type_t parity; |
eric@26 | 629 | page_label_t *page_label; |
eric@26 | 630 | |
eric@26 | 631 | for (;;) |
eric@26 | 632 | { |
eric@26 | 633 | if ((! image) || (i >= range_count (image->range))) |
eric@26 | 634 | { |
eric@26 | 635 | if (image) |
eric@26 | 636 | image = image->next; |
eric@26 | 637 | else |
eric@26 | 638 | image = first_input_image; |
eric@26 | 639 | if (! image) |
eric@26 | 640 | return (0); |
eric@26 | 641 | i = 0; |
eric@26 | 642 | if (! open_tiff_input_file (get_input_file (image->input_context))) |
eric@26 | 643 | return (0); |
eric@26 | 644 | } |
eric@26 | 645 | |
eric@26 | 646 | if ((! page) || (p >= range_count (page->range))) |
eric@26 | 647 | { |
eric@26 | 648 | if (page) |
eric@26 | 649 | page = page->next; |
eric@26 | 650 | else |
eric@26 | 651 | page = first_output_page; |
eric@26 | 652 | p = 0; |
eric@30 | 653 | if (! open_pdf_output_file (get_output_file (page->output_context), |
eric@30 | 654 | get_output_file_attributes (page->output_context))) |
eric@26 | 655 | return (0); |
eric@26 | 656 | page_label = get_output_page_label (page->output_context); |
eric@26 | 657 | process_page_numbers (page_index, |
eric@26 | 658 | range_count (page->range), |
eric@26 | 659 | page->range.first, |
eric@26 | 660 | page_label); |
eric@26 | 661 | } |
eric@26 | 662 | |
eric@26 | 663 | parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN; |
eric@26 | 664 | |
eric@26 | 665 | memset (& input_attributes, 0, sizeof (input_attributes)); |
eric@32 | 666 | |
eric@32 | 667 | input_attributes.rotation = 0; |
eric@32 | 668 | input_attributes.has_rotation = get_input_rotation (image->input_context, |
eric@32 | 669 | parity, |
eric@32 | 670 | & input_attributes.rotation); |
eric@32 | 671 | |
eric@32 | 672 | input_attributes.has_page_size = get_input_page_size (image->input_context, |
eric@32 | 673 | parity, |
eric@32 | 674 | & input_attributes.page_size); |
eric@26 | 675 | |
eric@26 | 676 | process_page (image->range.first + i, |
eric@26 | 677 | input_attributes, |
eric@26 | 678 | page->bookmark_list); |
eric@26 | 679 | i++; |
eric@26 | 680 | p++; |
eric@26 | 681 | page_index++; |
eric@26 | 682 | } |
eric@26 | 683 | |
eric@26 | 684 | return (1); |
eric@26 | 685 | } |