pdf_util.c

Mon, 14 Dec 2009 15:51:53 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 15:51:53 +0000
changeset 166
301f6f17c364
parent 125
e2ef1c2f9eca
permissions
-rw-r--r--

Patched to add PNG and JP2 support.

Created-By: Daniel Glöckner <daniel-gl at gmx.net>


The attached patch adds PNG and JP2 support to tumble.

PNG:
As the deflated data is directly copied into the PDF, there are some
limitations to the list of supported images:
- bit depth <= 8
- no alpha channel
- no interlace

JP2:
The PDF Reference says JP2 is just a subset of the allowed JPX
format. I don't have a copy of the official standard, so I don't know
what to change to cover JPXes as well.
You'll need the Adobe Acrobat Reader 6 to display those images.
Xpdf and Ghostscript are missing the ColorSpace key in the image
dictionary, which is optional for JPXDecode and IMHO not just a matter
of a few lines of code.
One thing left to do is to change the PDF version to 1.5 if a JP2 file
has been given to tumble - maybe using the Version key in the Catalog
if seeking is not possible.
Using the resolution info in a JP2 (resc/resd boxes) is implemented but
untested. Jasper doesn't write those boxes.

I had to change the string handling to allow black in PNG palettes.
And there was a double free in tumble_input.c which happens when not
using control files.

Daniel

eric@62 1 /*
eric@125 2 * tumble: build a PDF file from image files
eric@62 3 *
eric@62 4 * PDF routines
eric@125 5 * $Id: pdf_util.c,v 1.6 2003/03/13 00:57:05 eric Exp $
eric@62 6 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
eric@62 7 *
eric@62 8 * This program is free software; you can redistribute it and/or modify
eric@62 9 * it under the terms of the GNU General Public License version 2 as
eric@62 10 * published by the Free Software Foundation. Note that permission is
eric@62 11 * not granted to redistribute this program under the terms of any
eric@62 12 * other version of the General Public License.
eric@62 13 *
eric@62 14 * This program is distributed in the hope that it will be useful,
eric@62 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eric@62 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eric@62 17 * GNU General Public License for more details.
eric@62 18 *
eric@62 19 * You should have received a copy of the GNU General Public License
eric@62 20 * along with this program; if not, write to the Free Software
eric@62 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
eric@62 22 */
eric@62 23
eric@62 24
eric@59 25 #include <stdarg.h>
eric@62 26 #include <stdbool.h>
eric@62 27 #include <stdint.h>
eric@59 28 #include <stdio.h>
eric@59 29 #include <stdlib.h>
eric@59 30 #include <string.h>
eric@59 31
eric@62 32 #include "bitblt.h"
eric@60 33 #include "pdf_util.h"
eric@59 34
eric@59 35
eric@59 36 void pdf_fatal (char *fmt, ...)
eric@59 37 {
eric@59 38 va_list ap;
eric@59 39
eric@59 40 va_start (ap, fmt);
eric@59 41 vfprintf (stderr, fmt, ap);
eric@59 42 va_end (ap);
eric@59 43
eric@59 44 exit (2);
eric@59 45 }
eric@59 46
eric@59 47
eric@67 48 void *pdf_calloc (size_t nmemb, size_t size)
eric@59 49 {
eric@67 50 void *m = calloc (nmemb, size);
eric@59 51 if (! m)
eric@59 52 pdf_fatal ("failed to allocate memory\n");
eric@59 53 return (m);
eric@59 54 }
eric@59 55
eric@59 56
eric@59 57 char *pdf_strdup (char *s)
eric@59 58 {
eric@59 59 unsigned long len = strlen (s);
eric@67 60 char *s2 = pdf_calloc (1, len + 1);
eric@59 61 strcpy (s2, s);
eric@59 62 return (s2);
eric@59 63 }