-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathurftopdf.cpp
635 lines (518 loc) · 18.6 KB
/
urftopdf.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @brief Decode URF to a PDF file
* @file urf_decode.cpp
* @author Neil 'Superna' Armstrong <superna9999@gmail.com> (C) 2010-2014
* @author Tobias Hoffmann <smilingthax@gmail.com> (c) 2012
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h> // ntohl
#include <vector>
#ifdef QPDF_BACKEND
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_Buffer.hh>
#endif
#ifdef HPDF_BACKEND
#include <cups/cups.h>
#include <hpdf.h>
#endif
#include "unirast.h"
#define DEFAULT_PDF_UNIT 72 // 1/72 inch
#define PROGRAM "urftopdf"
#ifdef URF_DEBUG
#define dprintf(format, ...) fprintf(stderr, "DEBUG: (" PROGRAM ") " format, __VA_ARGS__)
#else
#define dprintf(format, ...)
#endif
#define iprintf(format, ...) fprintf(stderr, "INFO: (" PROGRAM ") " format, __VA_ARGS__)
void die(const char * str)
{
fprintf(stderr, "CRIT: (" PROGRAM ") die(%s) [%m]\n", str);
exit(1);
}
//------------- PDF ---------------
struct pdf_info
{
#ifdef HPDF_BACKEND
HPDF_Doc pdf;
HPDF_Page page;
HPDF_Image image;
HPDF_ColorSpace color_space;
unsigned stride_bytes;
uint8_t * page_data;
char * filename;
#endif
#ifdef QPDF_BACKEND
pdf_info()
: pagecount(0),
width(0),height(0),
pixel_bytes(0),line_bytes(0),
bpp(0),
page_width(0),page_height(0)
{
}
QPDF pdf;
QPDFObjectHandle page;
PointerHolder<Buffer> page_data;
double page_width,page_height;
unsigned page_colourspace;
#endif
unsigned pagecount;
unsigned width;
unsigned height;
unsigned pixel_bytes;
unsigned line_bytes;
unsigned bpp;
};
#ifdef HPDF_BACKEND
void pdf_error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
{
fprintf(stderr, "CRIT: (" PROGRAM ") pdf_error_handler error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
exit(1);
}
int create_pdf_file(struct pdf_info * info, char * filename, unsigned pagecount)
{
if((info->pdf = HPDF_New (pdf_error_handler, NULL)) == NULL) die("cannot create PdfDoc object");
info->filename = filename;
HPDF_SetCompressionMode(info->pdf, HPDF_COMP_ALL);
info->pagecount = pagecount;
return 0;
}
void draw_pdf_page(struct pdf_info * info)
{
if(info->page_data)
{
//Finish previous Page
info->image = HPDF_LoadRawImageFromMem(info->pdf, info->page_data, info->width, info->height, info->color_space, 8);
if(info->image == NULL) die("Unable to load image data");
HPDF_Page_DrawImage(info->page, info->image, 0, 0, HPDF_Page_GetWidth(info->page), HPDF_Page_GetHeight(info->page));
free(info->page_data);
info->page_data = NULL;
}
}
int add_pdf_page(struct pdf_info * info, int pagen, unsigned width, unsigned height, int bpp, unsigned dpi,
unsigned color_space)
{
draw_pdf_page(info);
info->width = width;
info->height = height;
info->pixel_bytes = bpp/8;
info->line_bytes = (width*info->pixel_bytes);
info->bpp = bpp;
info->page_data = (uint8_t*)malloc(info->line_bytes*info->height);
if(info->page_data == NULL) die("Unable to allocate page data");
info->page = HPDF_AddPage(info->pdf);
info->color_space = HPDF_CS_DEVICE_RGB;
if(color_space == UNIRAST_COLOR_SPACE_GRAYSCALE_8BIT)
info->color_space = HPDF_CS_DEVICE_GRAY;
// Convert to 72DPI sizes
HPDF_Page_SetWidth(info->page, ((float)info->width/(float)dpi)*DEFAULT_PDF_UNIT);
HPDF_Page_SetHeight(info->page, ((float)info->height/(float)dpi)*DEFAULT_PDF_UNIT);
return 0;
}
int close_pdf_file(struct pdf_info * info)
{
draw_pdf_page(info);
HPDF_SaveToFile(info->pdf, info->filename);
return 0;
}
void pdf_set_line(struct pdf_info * info, unsigned line_n, uint8_t line[])
{
dprintf("pdf_set_line(%d)\n", line_n);
if(line_n > info->height)
{
dprintf("Bad line %d\n", line_n);
return;
}
memcpy((info->page_data+(line_n*info->line_bytes)), line, info->line_bytes);
}
#endif
#ifdef QPDF_BACKEND
int create_pdf_file(struct pdf_info * info, unsigned pagecount)
{
try {
info->pdf.emptyPDF();
} catch (...) {
return 1;
}
info->pagecount = pagecount;
return 0;
}
QPDFObjectHandle makeBox(double x1, double y1, double x2, double y2)
{
QPDFObjectHandle ret=QPDFObjectHandle::newArray();
ret.appendItem(QPDFObjectHandle::newReal(x1));
ret.appendItem(QPDFObjectHandle::newReal(y1));
ret.appendItem(QPDFObjectHandle::newReal(x2));
ret.appendItem(QPDFObjectHandle::newReal(y2));
return ret;
}
enum ColorSpace {
DEVICE_GRAY,
DEVICE_RGB,
DEVICE_CMYK
};
#define PRE_COMPRESS
/* or temporarily store images?
if(cupsTempFile2(tempfile_name, 255) == NULL) die("Unable to create a temporary pdf file");
iprintf("Created temporary file '%s'\n", tempfile_name);
*/
QPDFObjectHandle makeImage(QPDF &pdf, PointerHolder<Buffer> page_data, unsigned width, unsigned height, ColorSpace cs, unsigned bpc)
{
QPDFObjectHandle ret = QPDFObjectHandle::newStream(&pdf);
std::map<std::string,QPDFObjectHandle> dict;
dict["/Type"]=QPDFObjectHandle::newName("/XObject");
dict["/Subtype"]=QPDFObjectHandle::newName("/Image");
dict["/Width"]=QPDFObjectHandle::newInteger(width);
dict["/Height"]=QPDFObjectHandle::newInteger(height);
dict["/BitsPerComponent"]=QPDFObjectHandle::newInteger(bpc);
if (cs==DEVICE_GRAY) {
dict["/ColorSpace"]=QPDFObjectHandle::newName("/DeviceGray");
} else if (cs==DEVICE_RGB) {
dict["/ColorSpace"]=QPDFObjectHandle::newName("/DeviceRGB");
} else if (cs==DEVICE_CMYK) {
dict["/ColorSpace"]=QPDFObjectHandle::newName("/DeviceCMYK");
} else {
return QPDFObjectHandle();
}
ret.replaceDict(QPDFObjectHandle::newDictionary(dict));
#ifdef PRE_COMPRESS
// we deliver already compressed content (instead of letting QPDFWriter do it), to avoid using excessive memory
Pl_Buffer psink("psink");
Pl_Flate pflate("pflate",&psink,Pl_Flate::a_deflate);
pflate.write(page_data->getBuffer(),page_data->getSize());
pflate.finish();
// /Filter /FlateDecode
// /DecodeParms [<</Predictor 1 /Colors 1[3] /BitsPerComponent $bits /Columns $x>>] ??
ret.replaceStreamData(PointerHolder<Buffer>(psink.getBuffer()),
QPDFObjectHandle::newName("/FlateDecode"),QPDFObjectHandle::newNull());
#else
ret.replaceStreamData(page_data,QPDFObjectHandle::newNull(),QPDFObjectHandle::newNull());
#endif
return ret;
}
void finish_page(struct pdf_info * info)
{
//Finish previous Page
if(!info->page_data.getPointer())
return;
ColorSpace qpdf_cs = DEVICE_RGB;
if(info->page_colourspace == UNIRAST_COLOR_SPACE_GRAYSCALE_8BIT)
qpdf_cs = DEVICE_GRAY;
QPDFObjectHandle image = makeImage(info->pdf, info->page_data, info->width, info->height, qpdf_cs, 8);
if(!image.isInitialized()) die("Unable to load image data");
// add it
info->page.getKey("/Resources").getKey("/XObject").replaceKey("/I",image);
// draw it
std::string content;
content.append(QUtil::double_to_string(info->page_width) + " 0 0 " +
QUtil::double_to_string(info->page_height) + " 0 0 cm\n");
content.append("/I Do\n");
info->page.getKey("/Contents").replaceStreamData(content,QPDFObjectHandle::newNull(),QPDFObjectHandle::newNull());
// bookkeeping
info->page_data = PointerHolder<Buffer>();
}
int add_pdf_page(struct pdf_info * info, int pagen, unsigned width, unsigned height, int bpp, unsigned dpi,
unsigned colourspace)
{
try {
finish_page(info); // any active
info->width = width;
info->height = height;
info->pixel_bytes = bpp/8;
info->line_bytes = (width*info->pixel_bytes);
info->bpp = bpp;
info->page_colourspace = colourspace;
info->page_data = PointerHolder<Buffer>(new Buffer(info->line_bytes*info->height));
QPDFObjectHandle page = QPDFObjectHandle::parse(
"<<"
" /Type /Page"
" /Resources <<"
" /XObject << >> "
" >>"
" /MediaBox null "
" /Contents null "
">>");
page.replaceKey("/Contents",QPDFObjectHandle::newStream(&info->pdf)); // data will be provided later
// Convert to pdf units
info->page_width=((double)info->width/dpi)*DEFAULT_PDF_UNIT;
info->page_height=((double)info->height/dpi)*DEFAULT_PDF_UNIT;
page.replaceKey("/MediaBox",makeBox(0,0,info->page_width,info->page_height));
info->page = info->pdf.makeIndirectObject(page); // we want to keep a reference
info->pdf.addPage(info->page, false);
} catch (std::bad_alloc &ex) {
die("Unable to allocate page data");
} catch (...) {
return 1;
}
return 0;
}
int close_pdf_file(struct pdf_info * info)
{
try {
finish_page(info); // any active
QPDFWriter output(info->pdf,NULL);
output.write();
} catch (...) {
return 1;
}
return 0;
}
void pdf_set_line(struct pdf_info * info, unsigned line_n, uint8_t line[])
{
dprintf("pdf_set_line(%d)\n", line_n);
if(line_n > info->height)
{
dprintf("Bad line %d\n", line_n);
return;
}
memcpy((info->page_data->getBuffer()+(line_n*info->line_bytes)), line, info->line_bytes);
}
#endif
// Data are in network endianness
struct urf_file_header {
char unirast[8];
uint32_t page_count;
} __attribute__((__packed__));
struct urf_page_header {
uint8_t bpp;
uint8_t colorspace;
uint8_t duplex;
uint8_t quality;
uint32_t unknown0;
uint32_t unknown1;
uint32_t width;
uint32_t height;
uint32_t dot_per_inch;
uint32_t unknown2;
uint32_t unknown3;
} __attribute__((__packed__));
int decode_raster(int fd, unsigned width, unsigned height, int bpp, struct pdf_info * info)
{
// We should be at raster start
int i, j;
unsigned cur_line = 0;
unsigned pos = 0;
uint8_t line_repeat_byte = 0;
unsigned line_repeat = 0;
int8_t packbit_code = 0;
int pixel_size = (bpp/8);
std::vector<uint8_t> pixel_container;
std::vector<uint8_t> line_container;
try {
pixel_container.resize(pixel_size);
line_container.resize(pixel_size*width);
} catch (...) {
die("Unable to allocate temporary storage");
}
do
{
if(read(fd, &line_repeat_byte, 1) < 1)
{
dprintf("l%06d : line_repeat EOF at %lu\n", cur_line, lseek(fd, 0, SEEK_CUR));
return 1;
}
line_repeat = (unsigned)line_repeat_byte + 1;
dprintf("l%06d : next actions for %d lines\n", cur_line, line_repeat);
// Start of line
pos = 0;
do
{
if(read(fd, &packbit_code, 1) < 1)
{
dprintf("p%06dl%06d : packbit_code EOF at %lu\n", pos, cur_line, lseek(fd, 0, SEEK_CUR));
return 1;
}
dprintf("p%06dl%06d: Raster code %02X='%d'.\n", pos, cur_line, (uint8_t)packbit_code, packbit_code);
if(packbit_code == -128)
{
dprintf("\tp%06dl%06d : blank rest of line.\n", pos, cur_line);
memset((&line_container[pos*pixel_size]), 0xFF, (pixel_size*(width-pos)));
pos = width;
break;
}
else if(packbit_code >= 0 && packbit_code <= 127)
{
int n = (packbit_code+1);
//Read pixel
if(read(fd, &pixel_container[0], pixel_size) < pixel_size)
{
dprintf("p%06dl%06d : pixel repeat EOF at %lu\n", pos, cur_line, lseek(fd, 0, SEEK_CUR));
return 1;
}
dprintf("\tp%06dl%06d : Repeat pixel '", pos, cur_line);
for(j = 0 ; j < pixel_size ; ++j)
dprintf("%02X ", pixel_container[j]);
dprintf("' for %d times.\n", n);
for(i = 0 ; i < n ; ++i)
{
//for(j = pixel_size-1 ; j >= 0 ; --j)
for(j = 0 ; j < pixel_size ; ++j)
line_container[pixel_size*pos + j] = pixel_container[j];
++pos;
if(pos >= width)
break;
}
if(i < n && pos >= width)
{
dprintf("\tp%06dl%06d : Forced end of line for pixel repeat.\n", pos, cur_line);
}
if(pos >= width)
break;
}
else if(packbit_code > -128 && packbit_code < 0)
{
int n = (-(int)packbit_code)+1;
dprintf("\tp%06dl%06d : Copy %d verbatim pixels.\n", pos, cur_line, n);
for(i = 0 ; i < n ; ++i)
{
if(read(fd, &pixel_container[0], pixel_size) < pixel_size)
{
dprintf("p%06dl%06d : literal_pixel EOF at %lu\n", pos, cur_line, lseek(fd, 0, SEEK_CUR));
return 1;
}
//Invert pixels, should be programmable
for(j = 0 ; j < pixel_size ; ++j)
line_container[pixel_size*pos + j] = pixel_container[j];
++pos;
if(pos >= width)
break;
}
if(i < n && pos >= width)
{
dprintf("\tp%06dl%06d : Forced end of line for pixel copy.\n", pos, cur_line);
}
if(pos >= width)
break;
}
}
while(pos < width);
dprintf("\tl%06d : End Of line, drawing %d times.\n", cur_line, line_repeat);
// write lines
for(i = 0 ; i < (int)line_repeat ; ++i)
{
pdf_set_line(info, cur_line, &line_container[0]);
++cur_line;
}
}
while(cur_line < height);
return 0;
}
int main(int argc, char **argv)
{
int fd, page;
struct urf_file_header head, head_orig;
struct urf_page_header page_header, page_header_orig;
struct pdf_info pdf;
#ifdef HPDF_BACKEND
char tempfile_name[255];
memset(&pdf, 0, sizeof(pdf));
#endif
FILE * input = NULL;
if(argc < 6)
{
fprintf(stderr, "Usage: %s <job> <user> <job name> <copies> <option> [file]\n", argv[0]);
return 1;
}
if(argc > 6)
{
input = fopen(argv[6], "rb");
if(input == NULL) die("Unable to open unirast file");
}
else
input = stdin;
#ifdef HPDF_BACKEND
if(cupsTempFile2(tempfile_name, 255) == NULL) die("Unable to create a temporary pdf file");
iprintf("Created temporary file '%s'\n", tempfile_name);
#endif
// Get fd from file
fd = fileno(input);
if(read(fd, &head_orig, sizeof(head)) == -1) die("Unable to read file header");
//Transform
memcpy(head.unirast, head_orig.unirast, sizeof(head.unirast));
head.page_count = ntohl(head_orig.page_count);
if(head.unirast[7])
head.unirast[7] = 0;
if(strncmp(head.unirast, "UNIRAST", 7) != 0) die("Bad File Header");
iprintf("%s file, with %d page(s).\n", head.unirast, head.page_count);
#ifdef HPDF_BACKEND
if(create_pdf_file(&pdf, tempfile_name, head.page_count) != 0) die("Unable to create PDF file");
#else
if(create_pdf_file(&pdf, head.page_count) != 0) die("Unable to create PDF file");
#endif
for(page = 0 ; page < (int)head.page_count ; ++page)
{
if(read(fd, &page_header_orig, sizeof(page_header_orig)) == -1) die("Unable to read page header");
//Transform
page_header.bpp = page_header_orig.bpp;
page_header.colorspace = page_header_orig.colorspace;
page_header.duplex = page_header_orig.duplex;
page_header.quality = page_header_orig.quality;
page_header.unknown0 = 0;
page_header.unknown1 = 0;
page_header.width = ntohl(page_header_orig.width);
page_header.height = ntohl(page_header_orig.height);
page_header.dot_per_inch = ntohl(page_header_orig.dot_per_inch);
page_header.unknown2 = 0;
page_header.unknown3 = 0;
iprintf("Page %d :\n", page);
iprintf("Bits Per Pixel : %d\n", page_header.bpp);
iprintf("Colorspace : %d\n", page_header.colorspace);
iprintf("Duplex Mode : %d\n", page_header.duplex);
iprintf("Quality : %d\n", page_header.quality);
iprintf("Size : %dx%d pixels\n", page_header.width, page_header.height);
iprintf("Dots per Inches : %d\n", page_header.dot_per_inch);
if(page_header.colorspace != UNIRAST_COLOR_SPACE_SRGB_24BIT_1 && page_header.colorspace != UNIRAST_COLOR_SPACE_GRAYSCALE_8BIT)
{
die("Invalid ColorSpace, currently only RGB 24BIT type 1 and Grayscale 8BIT are supported");
}
if(page_header.bpp != UNIRAST_BPP_24BIT && page_header.bpp != UNIRAST_BPP_8BIT)
{
die("Invalid Bit Per Pixel value, only 24bit and 8bit are supported");
}
if(add_pdf_page(&pdf, page, page_header.width, page_header.height, page_header.bpp, page_header.dot_per_inch, page_header.colorspace) != 0)
die("Unable to create PDF file");
if(decode_raster(fd, page_header.width, page_header.height, page_header.bpp, &pdf) != 0)
die("Failed to decode Page");
}
close_pdf_file(&pdf);
#ifdef HPDF_BACKEND
// output generated file
{
uint8_t * buffer[4096];
size_t sread = 4096;
int file = open(tempfile_name, O_RDONLY);
if(file == -1) die("Unable to read back temporary file");
while(sread > 0)
{
sread = read(file, buffer, 4096);
fwrite(buffer, sread, 1, stdout);
}
close(file);
}
unlink(tempfile_name);
#endif
return 0;
}