| 1 | #include "mupdf/fitz.h" |
| 2 | |
| 3 | #include <string.h> |
| 4 | #include <limits.h> |
| 5 | |
| 6 | const char *fz_pclm_write_options_usage = |
| 7 | "PCLm output options:\n" |
| 8 | "\tcompression=none: No compression (default)\n" |
| 9 | "\tcompression=flate: Flate compression\n" |
| 10 | "\tstrip-height=N: Strip height (default 16)\n" |
| 11 | "\n" ; |
| 12 | |
| 13 | /* |
| 14 | Parse PCLm options. |
| 15 | |
| 16 | Currently defined options and values are as follows: |
| 17 | |
| 18 | compression=none: No compression |
| 19 | compression=flate: Flate compression |
| 20 | strip-height=n: Strip height (default 16) |
| 21 | */ |
| 22 | fz_pclm_options * |
| 23 | fz_parse_pclm_options(fz_context *ctx, fz_pclm_options *opts, const char *args) |
| 24 | { |
| 25 | const char *val; |
| 26 | |
| 27 | memset(opts, 0, sizeof *opts); |
| 28 | |
| 29 | if (fz_has_option(ctx, args, "compression" , &val)) |
| 30 | { |
| 31 | if (fz_option_eq(val, "none" )) |
| 32 | opts->compress = 0; |
| 33 | else if (fz_option_eq(val, "flate" )) |
| 34 | opts->compress = 1; |
| 35 | else |
| 36 | fz_throw(ctx, FZ_ERROR_GENERIC, "Unsupported PCLm compression %s (none, or flate only)" , val); |
| 37 | } |
| 38 | if (fz_has_option(ctx, args, "strip-height" , &val)) |
| 39 | { |
| 40 | int i = fz_atoi(val); |
| 41 | if (i <= 0) |
| 42 | fz_throw(ctx, FZ_ERROR_GENERIC, "Unsupported PCLm strip height %d (suggest 16)" , i); |
| 43 | opts->strip_height = i; |
| 44 | } |
| 45 | |
| 46 | return opts; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | fz_write_pixmap_as_pclm(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pclm_options *pclm) |
| 51 | { |
| 52 | fz_band_writer *writer; |
| 53 | |
| 54 | if (!pixmap || !out) |
| 55 | return; |
| 56 | |
| 57 | writer = fz_new_pclm_band_writer(ctx, out, pclm); |
| 58 | fz_try(ctx) |
| 59 | { |
| 60 | fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps); |
| 61 | fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples); |
| 62 | } |
| 63 | fz_always(ctx) |
| 64 | fz_drop_band_writer(ctx, writer); |
| 65 | fz_catch(ctx) |
| 66 | fz_rethrow(ctx); |
| 67 | } |
| 68 | |
| 69 | typedef struct pclm_band_writer_s |
| 70 | { |
| 71 | fz_band_writer super; |
| 72 | fz_pclm_options options; |
| 73 | |
| 74 | int obj_num; |
| 75 | int xref_max; |
| 76 | int64_t *xref; |
| 77 | int pages; |
| 78 | int page_max; |
| 79 | int *page_obj; |
| 80 | unsigned char *stripbuf; |
| 81 | unsigned char *compbuf; |
| 82 | size_t complen; |
| 83 | } pclm_band_writer; |
| 84 | |
| 85 | static int |
| 86 | new_obj(fz_context *ctx, pclm_band_writer *writer) |
| 87 | { |
| 88 | int64_t pos = fz_tell_output(ctx, writer->super.out); |
| 89 | |
| 90 | if (writer->obj_num >= writer->xref_max) |
| 91 | { |
| 92 | int new_max = writer->xref_max * 2; |
| 93 | if (new_max < writer->obj_num + 8) |
| 94 | new_max = writer->obj_num + 8; |
| 95 | writer->xref = fz_realloc_array(ctx, writer->xref, new_max, int64_t); |
| 96 | writer->xref_max = new_max; |
| 97 | } |
| 98 | |
| 99 | writer->xref[writer->obj_num] = pos; |
| 100 | |
| 101 | return writer->obj_num++; |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | (fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs) |
| 106 | { |
| 107 | pclm_band_writer *writer = (pclm_band_writer *)writer_; |
| 108 | fz_output *out = writer->super.out; |
| 109 | int w = writer->super.w; |
| 110 | int h = writer->super.h; |
| 111 | int n = writer->super.n; |
| 112 | int s = writer->super.s; |
| 113 | int a = writer->super.alpha; |
| 114 | int xres = writer->super.xres; |
| 115 | int yres = writer->super.yres; |
| 116 | int sh = writer->options.strip_height; |
| 117 | int strips = (h + sh-1)/sh; |
| 118 | int i; |
| 119 | size_t len; |
| 120 | unsigned char *data; |
| 121 | fz_buffer *buf = NULL; |
| 122 | |
| 123 | if (a != 0) |
| 124 | fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm cannot write alpha channel" ); |
| 125 | if (s != 0) |
| 126 | fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm cannot write spot colors" ); |
| 127 | if (n != 3 && n != 1) |
| 128 | fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm expected to be Grayscale or RGB" ); |
| 129 | |
| 130 | fz_free(ctx, writer->stripbuf); |
| 131 | fz_free(ctx, writer->compbuf); |
| 132 | writer->stripbuf = fz_malloc(ctx, w * sh * n); |
| 133 | writer->complen = fz_deflate_bound(ctx, w * sh * n); |
| 134 | writer->compbuf = fz_malloc(ctx, writer->complen); |
| 135 | |
| 136 | /* Send the file header on the first page */ |
| 137 | if (writer->pages == 0) |
| 138 | fz_write_string(ctx, out, "%PDF-1.4\n%PCLm-1.0\n" ); |
| 139 | |
| 140 | if (writer->page_max <= writer->pages) |
| 141 | { |
| 142 | int new_max = writer->page_max * 2; |
| 143 | if (new_max == 0) |
| 144 | new_max = writer->pages + 8; |
| 145 | writer->page_obj = fz_realloc_array(ctx, writer->page_obj, new_max, int); |
| 146 | writer->page_max = new_max; |
| 147 | } |
| 148 | writer->page_obj[writer->pages] = writer->obj_num; |
| 149 | writer->pages++; |
| 150 | |
| 151 | /* Send the Page Object */ |
| 152 | fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Resources <<\n/XObject <<\n" , new_obj(ctx, writer)); |
| 153 | for (i = 0; i < strips; i++) |
| 154 | fz_write_printf(ctx, out, "/Image%d %d 0 R\n" , i, writer->obj_num + 1 + i); |
| 155 | fz_write_printf(ctx, out, ">>\n>>\n/MediaBox[ 0 0 %g %g ]\n/Contents [ %d 0 R ]\n>>\nendobj\n" , |
| 156 | w * 72.0f / xres, h * 72.0f / yres, writer->obj_num); |
| 157 | |
| 158 | /* And the Page contents */ |
| 159 | /* We need the length to this, so write to a buffer first */ |
| 160 | fz_var(buf); |
| 161 | fz_try(ctx) |
| 162 | { |
| 163 | buf = fz_new_buffer(ctx, 0); |
| 164 | fz_append_printf(ctx, buf, "%g 0 0 %g 0 0 cm\n" , 72.0f/xres, 72.0f/yres); |
| 165 | for (i = 0; i < strips; i++) |
| 166 | { |
| 167 | int at = h - (i+1)*sh; |
| 168 | int this_sh = sh; |
| 169 | if (at < 0) |
| 170 | { |
| 171 | this_sh += at; |
| 172 | at = 0; |
| 173 | } |
| 174 | fz_append_printf(ctx, buf, "/P <</MCID 0>> BDC q\n%d 0 0 %d 0 %d cm\n/Image%d Do Q\n" , |
| 175 | w, this_sh, at, i); |
| 176 | } |
| 177 | len = fz_buffer_storage(ctx, buf, &data); |
| 178 | fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Length %zd\n>>\nstream\n" , new_obj(ctx, writer), len); |
| 179 | fz_write_data(ctx, out, data, len); |
| 180 | fz_drop_buffer(ctx, buf); |
| 181 | buf = NULL; |
| 182 | fz_write_string(ctx, out, "\nendstream\nendobj\n" ); |
| 183 | } |
| 184 | fz_catch(ctx) |
| 185 | { |
| 186 | fz_drop_buffer(ctx, buf); |
| 187 | fz_rethrow(ctx); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | flush_strip(fz_context *ctx, pclm_band_writer *writer, int fill) |
| 193 | { |
| 194 | unsigned char *data = writer->stripbuf; |
| 195 | fz_output *out = writer->super.out; |
| 196 | int w = writer->super.w; |
| 197 | int n = writer->super.n; |
| 198 | size_t len = w*n*fill; |
| 199 | |
| 200 | /* Buffer is full, compress it and write it. */ |
| 201 | if (writer->options.compress) |
| 202 | { |
| 203 | size_t destLen = writer->complen; |
| 204 | fz_deflate(ctx, writer->compbuf, &destLen, data, len, FZ_DEFLATE_DEFAULT); |
| 205 | len = destLen; |
| 206 | data = writer->compbuf; |
| 207 | } |
| 208 | fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Width %d\n/ColorSpace /Device%s\n/Height %d\n%s/Subtype /Image\n" , |
| 209 | new_obj(ctx, writer), w, n == 1 ? "Gray" : "RGB" , fill, writer->options.compress ? "/Filter /FlateDecode\n" : "" ); |
| 210 | fz_write_printf(ctx, out, "/Length %zd\n/Type /XObject\n/BitsPerComponent 8\n>>\nstream\n" , len); |
| 211 | fz_write_data(ctx, out, data, len); |
| 212 | fz_write_string(ctx, out, "\nendstream\nendobj\n" ); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | pclm_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *sp) |
| 217 | { |
| 218 | pclm_band_writer *writer = (pclm_band_writer *)writer_; |
| 219 | fz_output *out = writer->super.out; |
| 220 | int w = writer->super.w; |
| 221 | int h = writer->super.h; |
| 222 | int n = writer->super.n; |
| 223 | int sh = writer->options.strip_height; |
| 224 | int line; |
| 225 | |
| 226 | if (!out) |
| 227 | return; |
| 228 | |
| 229 | for (line = 0; line < band_height; line++) |
| 230 | { |
| 231 | int dstline = (band_start+line) % sh; |
| 232 | memcpy(writer->stripbuf + w*n*dstline, sp + line * w*n, w*n); |
| 233 | if (dstline+1 == sh) |
| 234 | flush_strip(ctx, writer, dstline+1); |
| 235 | } |
| 236 | |
| 237 | if (band_start + band_height == h && h % sh != 0) |
| 238 | flush_strip(ctx, writer, h % sh); |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | pclm_write_trailer(fz_context *ctx, fz_band_writer *writer_) |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | pclm_drop_band_writer(fz_context *ctx, fz_band_writer *writer_) |
| 248 | { |
| 249 | pclm_band_writer *writer = (pclm_band_writer *)writer_; |
| 250 | fz_output *out = writer->super.out; |
| 251 | int i; |
| 252 | |
| 253 | /* We actually do the trailer writing in the drop */ |
| 254 | if (writer->xref_max > 2) |
| 255 | { |
| 256 | int64_t t_pos; |
| 257 | |
| 258 | /* Catalog */ |
| 259 | writer->xref[1] = fz_tell_output(ctx, out); |
| 260 | fz_write_printf(ctx, out, "1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n" ); |
| 261 | |
| 262 | /* Page table */ |
| 263 | writer->xref[2] = fz_tell_output(ctx, out); |
| 264 | fz_write_printf(ctx, out, "2 0 obj\n<<\n/Count %d\n/Kids [ " , writer->pages); |
| 265 | |
| 266 | for (i = 0; i < writer->pages; i++) |
| 267 | fz_write_printf(ctx, out, "%d 0 R " , writer->page_obj[i]); |
| 268 | fz_write_string(ctx, out, "]\n/Type /Pages\n>>\nendobj\n" ); |
| 269 | |
| 270 | /* Xref */ |
| 271 | t_pos = fz_tell_output(ctx, out); |
| 272 | fz_write_printf(ctx, out, "xref\n0 %d\n0000000000 65535 f \n" , writer->obj_num); |
| 273 | for (i = 1; i < writer->obj_num; i++) |
| 274 | fz_write_printf(ctx, out, "%010zd 00000 n \n" , writer->xref[i]); |
| 275 | fz_write_printf(ctx, out, "trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\nstartxref\n%ld\n%%%%EOF\n" , writer->obj_num, t_pos); |
| 276 | } |
| 277 | |
| 278 | fz_free(ctx, writer->stripbuf); |
| 279 | fz_free(ctx, writer->compbuf); |
| 280 | fz_free(ctx, writer->page_obj); |
| 281 | fz_free(ctx, writer->xref); |
| 282 | } |
| 283 | |
| 284 | fz_band_writer *fz_new_pclm_band_writer(fz_context *ctx, fz_output *out, const fz_pclm_options *options) |
| 285 | { |
| 286 | pclm_band_writer *writer = fz_new_band_writer(ctx, pclm_band_writer, out); |
| 287 | |
| 288 | writer->super.header = pclm_write_header; |
| 289 | writer->super.band = pclm_write_band; |
| 290 | writer->super.trailer = pclm_write_trailer; |
| 291 | writer->super.drop = pclm_drop_band_writer; |
| 292 | |
| 293 | if (options) |
| 294 | writer->options = *options; |
| 295 | else |
| 296 | memset(&writer->options, 0, sizeof(writer->options)); |
| 297 | |
| 298 | if (writer->options.strip_height == 0) |
| 299 | writer->options.strip_height = 16; |
| 300 | writer->obj_num = 3; /* 1 reserved for catalog, 2 for pages tree. */ |
| 301 | |
| 302 | return &writer->super; |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | fz_save_pixmap_as_pclm(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pclm_options *pclm) |
| 307 | { |
| 308 | fz_output *out = fz_new_output_with_path(ctx, filename, append); |
| 309 | fz_try(ctx) |
| 310 | { |
| 311 | fz_write_pixmap_as_pclm(ctx, out, pixmap, pclm); |
| 312 | fz_close_output(ctx, out); |
| 313 | } |
| 314 | fz_always(ctx) |
| 315 | fz_drop_output(ctx, out); |
| 316 | fz_catch(ctx) |
| 317 | fz_rethrow(ctx); |
| 318 | } |
| 319 | |
| 320 | /* High-level document writer interface */ |
| 321 | |
| 322 | typedef struct fz_pclm_writer_s fz_pclm_writer; |
| 323 | |
| 324 | struct fz_pclm_writer_s |
| 325 | { |
| 326 | fz_document_writer super; |
| 327 | fz_draw_options draw; |
| 328 | fz_pclm_options pclm; |
| 329 | fz_pixmap *pixmap; |
| 330 | fz_band_writer *bander; |
| 331 | fz_output *out; |
| 332 | int pagenum; |
| 333 | }; |
| 334 | |
| 335 | static fz_device * |
| 336 | pclm_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox) |
| 337 | { |
| 338 | fz_pclm_writer *wri = (fz_pclm_writer*)wri_; |
| 339 | return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | pclm_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) |
| 344 | { |
| 345 | fz_pclm_writer *wri = (fz_pclm_writer*)wri_; |
| 346 | fz_pixmap *pix = wri->pixmap; |
| 347 | |
| 348 | fz_try(ctx) |
| 349 | { |
| 350 | fz_close_device(ctx, dev); |
| 351 | fz_write_header(ctx, wri->bander, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, wri->pagenum++, pix->colorspace, pix->seps); |
| 352 | fz_write_band(ctx, wri->bander, pix->stride, pix->h, pix->samples); |
| 353 | } |
| 354 | fz_always(ctx) |
| 355 | { |
| 356 | fz_drop_device(ctx, dev); |
| 357 | fz_drop_pixmap(ctx, pix); |
| 358 | wri->pixmap = NULL; |
| 359 | } |
| 360 | fz_catch(ctx) |
| 361 | fz_rethrow(ctx); |
| 362 | } |
| 363 | |
| 364 | static void |
| 365 | pclm_close_writer(fz_context *ctx, fz_document_writer *wri_) |
| 366 | { |
| 367 | fz_pclm_writer *wri = (fz_pclm_writer*)wri_; |
| 368 | |
| 369 | fz_drop_band_writer(ctx, wri->bander); |
| 370 | wri->bander = NULL; |
| 371 | |
| 372 | fz_close_output(ctx, wri->out); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | pclm_drop_writer(fz_context *ctx, fz_document_writer *wri_) |
| 377 | { |
| 378 | fz_pclm_writer *wri = (fz_pclm_writer*)wri_; |
| 379 | |
| 380 | fz_drop_pixmap(ctx, wri->pixmap); |
| 381 | fz_drop_output(ctx, wri->out); |
| 382 | fz_drop_band_writer(ctx, wri->bander); |
| 383 | } |
| 384 | |
| 385 | fz_document_writer * |
| 386 | fz_new_pclm_writer(fz_context *ctx, const char *path, const char *options) |
| 387 | { |
| 388 | fz_pclm_writer *wri = fz_new_derived_document_writer(ctx, fz_pclm_writer, pclm_begin_page, pclm_end_page, pclm_close_writer, pclm_drop_writer); |
| 389 | |
| 390 | fz_try(ctx) |
| 391 | { |
| 392 | fz_parse_draw_options(ctx, &wri->draw, options); |
| 393 | fz_parse_pclm_options(ctx, &wri->pclm, options); |
| 394 | wri->out = fz_new_output_with_path(ctx, path ? path : "out.pclm" , 0); |
| 395 | wri->bander = fz_new_pclm_band_writer(ctx, wri->out, &wri->pclm); |
| 396 | } |
| 397 | fz_catch(ctx) |
| 398 | { |
| 399 | fz_drop_output(ctx, wri->out); |
| 400 | fz_free(ctx, wri); |
| 401 | fz_rethrow(ctx); |
| 402 | } |
| 403 | |
| 404 | return (fz_document_writer*)wri; |
| 405 | } |
| 406 | |