diff options
author | Karl Berry <karl@freefriends.org> | 2008-05-21 01:08:58 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2008-05-21 01:08:58 +0000 |
commit | 0ec78c5842c97cb4790129f345d904837dd1fd9c (patch) | |
tree | 20936290ae9060c90dffab6e58548b990228dd61 /Build/source/texk/dvipdfmx/src/xbb.c | |
parent | 29da3025cc0e364dc9a60bafce96e7fa110d433a (diff) |
dvipdfmx-20080520 snapshot, after remaking autofiles
git-svn-id: svn://tug.org/texlive/trunk@8263 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/dvipdfmx/src/xbb.c')
-rw-r--r-- | Build/source/texk/dvipdfmx/src/xbb.c | 359 |
1 files changed, 152 insertions, 207 deletions
diff --git a/Build/source/texk/dvipdfmx/src/xbb.c b/Build/source/texk/dvipdfmx/src/xbb.c index 4f1b17e825d..cac27b3867b 100644 --- a/Build/source/texk/dvipdfmx/src/xbb.c +++ b/Build/source/texk/dvipdfmx/src/xbb.c @@ -1,10 +1,8 @@ -/* $Header: /home/cvsroot/dvipdfmx/src/xbb.c,v 1.3 2007/11/14 03:36:01 chofchof Exp $ +/* $Header: /home/cvsroot/dvipdfmx/src/xbb.c,v 1.12 2008/05/18 14:31:06 matthias Exp $ - This is xbb, a bounding box extraction program extending ebb. + This is extractbb, a bounding box extraction program. - Copyright (C) 2007 by Jin-Hwan Cho <chofchof@ktug.or.kr> - - Copyright (C) 1998, 1999 by Mark A. Wicks + Copyright (C) 2008 by Jin-Hwan Cho <chofchof@ktug.or.kr> 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 @@ -36,19 +34,25 @@ #include <png.h> #endif -#define XBB_PROGRAM "xbb" -#define XBB_VERSION "Version 0.1" +#include "xbb.h" + +#define XBB_PROGRAM "extractbb" +#define XBB_VERSION "Version 0.2" -static void usage (void) +static void usage(void) { - fprintf (stderr, "%s, version %s, Copyright (C) 2007 by Jin-Hwan Cho\n", + fprintf (stderr, "%s, version %s, Copyright (C) 2008 by Jin-Hwan Cho\n", XBB_PROGRAM, XBB_VERSION); - fprintf (stderr, "xbb is an extension of ebb developed by Mark A. Wicks.\n"); - fprintf (stderr, "This is free software, and you are welcome to redistribute it\n"); - fprintf (stderr, "under certain conditions. Details are distributed with the software.\n"); - fprintf (stderr, "\nUsage: xbb [-v] [-b] [files]\n"); - fprintf (stderr, "\t-b\t\tWrite .xbb file in binary mode\n"); - fprintf (stderr, "\t-v\t\tVerbose\n"); + fprintf (stderr, "A bounding box extraction utility from PDF, PNG, and JPG.\n"); + fprintf (stdout, "\nThis is free software; you can redistribute it and/or modify\n"); + fprintf (stdout, "it under the terms of the GNU General Public License as published by\n"); + fprintf (stdout, "the Free Software Foundation; either version 2 of the License, or\n"); + fprintf (stdout, "(at your option) any later version.\n"); + fprintf (stderr, "\nUsage: %s [-v] [-b] [-m|-x] [files]\n", XBB_PROGRAM); + fprintf (stderr, "\t-b\tWrite output file in binary mode\n"); + fprintf (stderr, "\t-m\tOutput .bb file used in DVIPDFM\n"); + fprintf (stderr, "\t-x\tOutput .xbb file used in DVIPDFMx (default)\n"); + fprintf (stderr, "\t-v\tVerbose\n"); exit(1); } @@ -58,111 +62,79 @@ static void do_time(FILE *file) { time_t current_time; struct tm *bd_time; + time(¤t_time); bd_time = localtime(¤t_time); - fprintf (file, "%%%%CreationDate: %s\n", asctime (bd_time)); + fprintf(file, "%%%%CreationDate: %s\n", asctime(bd_time)); } char *extensions[] = { ".jpeg", ".JPEG", ".jpg", ".JPG", ".pdf", ".PDF", ".png", ".PNG" }; -static char *make_xbb_filename (const char *name) +static int xbb_output_mode = XBB_OUTPUT; + +static char *make_xbb_filename(const char *name) { int i; char *result; - name = xbasename (name); - for (i=0; i<sizeof(extensions)/sizeof(extensions[0]); i++) { - if (strlen (extensions[i]) < strlen(name) && - !strncmp (name+strlen(name)-strlen(extensions[i]), - extensions[i], strlen(extensions[i]))) + + for (i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) { + if (strlen(extensions[i]) < strlen(name) && + strncmp(name+strlen(name)-strlen(extensions[i]), extensions[i], strlen(extensions[i])) == 0) break; } - if (i == sizeof(extensions)/sizeof(extensions[0])) { - fprintf (stderr, - "xbb: Warning: %s: Filename does not end in a recognizeable extension.\n", - name); - result = NEW (strlen(name)+3, char); - strcpy (result, name); - } - else { /* Remove extension */ - result = NEW (strlen(name)+3-strlen(extensions[i])+1, char); - strncpy (result, name, strlen(name)-strlen(extensions[i])); + if (i == sizeof(extensions) / sizeof(extensions[0])) { + fprintf(stderr, "Warning: %s: Filename does not end in a recognizeable extension.\n", name); + result = NEW(strlen(name)+3, char); + strcpy(result, name); + } else { /* Remove extension */ + result = NEW(strlen(name)+3-strlen(extensions[i])+1, char); + strncpy(result, name, strlen(name)-strlen(extensions[i])); result[strlen(name)-strlen(extensions[i])] = 0; } - strcat (result, ".xbb"); + strcat(result, (xbb_output_mode == XBB_OUTPUT ? ".xbb" : ".bb")); return result; } static char *xbb_file_mode = FOPEN_W_MODE; -static void write_xbb (char *filename, int bbllx, int bblly, int bburx, - int bbury) +static void write_xbb(char *fname, int bbllx, int bblly, int bburx, int bbury) { - char *xbbfilename; - FILE *xbbfile; - if (verbose) - fprintf (stderr, "okay\n"); - xbbfilename = make_xbb_filename (filename); - if ((xbbfile = MFOPEN (xbbfilename, xbb_file_mode)) == NULL) { - fprintf (stderr, "Unable to open output file: %s\n", xbbfilename); + char *outname; + FILE *fp; + + outname = make_xbb_filename(fname); + if ((fp = MFOPEN(outname, xbb_file_mode)) == NULL) { + fprintf(stderr, "Unable to open output file: %s\n", outname); + RELEASE(outname); return; } if (verbose) { - fprintf (stderr, "Writing to %s: ", xbbfilename); - fprintf (stderr, "Bounding box: %d %d %d %d\n", bbllx, bblly, - bburx, bbury); + fprintf(stderr, "Writing to %s: ", outname); + fprintf(stderr, "Bounding box: %d %d %d %d\n", bbllx, bblly, bburx, bbury); } - fprintf (xbbfile, "%%%%Title: %s\n", filename); - fprintf (xbbfile, "%%%%Creator: %s %s\n", XBB_PROGRAM, XBB_VERSION); - fprintf (xbbfile, "%%%%BoundingBox: %d %d %d %d\n", - bbllx, bblly, bburx, bbury); - do_time(xbbfile); - RELEASE (xbbfilename); - MFCLOSE (xbbfile); - return; + fprintf(fp,"%%%%Title: %s\n", fname); + fprintf(fp,"%%%%Creator: %s %s\n", XBB_PROGRAM, XBB_VERSION); + fprintf(fp,"%%%%BoundingBox: %d %d %d %d\n", bbllx, bblly, bburx, bbury); + do_time(fp); + RELEASE(outname); + MFCLOSE(fp); } -/* JPEG Markers */ typedef enum { - JM_SOF0 = 0xc0, - JM_SOF1 = 0xc1, - JM_SOF2 = 0xc2, - JM_SOF3 = 0xc3, - JM_SOF5 = 0xc5, - JM_DHT = 0xc4, - JM_SOF6 = 0xc6, - JM_SOF7 = 0xc7, - JM_SOF9 = 0xc9, - JM_SOF10 = 0xca, - JM_SOF11 = 0xcb, - JM_DAC = 0xcc, - JM_SOF13 = 0xcd, - JM_SOF14 = 0xce, - JM_SOF15 = 0xcf, - - JM_RST0 = 0xd0, - JM_RST1 = 0xd1, - JM_RST2 = 0xd2, - JM_RST3 = 0xd3, - JM_RST4 = 0xd4, - JM_RST5 = 0xd5, - JM_RST6 = 0xd6, - JM_RST7 = 0xd7, - - JM_SOI = 0xd8, - JM_EOI = 0xd9, - JM_SOS = 0xda, - JM_DQT = 0xdb, - JM_DNL = 0xdc, - JM_DRI = 0xdd, - JM_DHP = 0xde, - JM_EXP = 0xdf, - - JM_APP0 = 0xe0, - JM_APP2 = 0xe2, - JM_APP14 = 0xee, - JM_APP15 = 0xef, + JM_SOF0 = 0xc0, JM_SOF1 = 0xc1, JM_SOF2 = 0xc2, JM_SOF3 = 0xc3, + JM_SOF5 = 0xc5, JM_DHT = 0xc4, JM_SOF6 = 0xc6, JM_SOF7 = 0xc7, + JM_SOF9 = 0xc9, JM_SOF10 = 0xca, JM_SOF11 = 0xcb, JM_DAC = 0xcc, + JM_SOF13 = 0xcd, JM_SOF14 = 0xce, JM_SOF15 = 0xcf, + + JM_RST0 = 0xd0, JM_RST1 = 0xd1, JM_RST2 = 0xd2, JM_RST3 = 0xd3, + JM_RST4 = 0xd4, JM_RST5 = 0xd5, JM_RST6 = 0xd6, JM_RST7 = 0xd7, + + JM_SOI = 0xd8, JM_EOI = 0xd9, JM_SOS = 0xda, JM_DQT = 0xdb, + JM_DNL = 0xdc, JM_DRI = 0xdd, JM_DHP = 0xde, JM_EXP = 0xdf, + + JM_APP0 = 0xe0, JM_APP2 = 0xe2, JM_APP14 = 0xee, JM_APP15 = 0xef, JM_COM = 0xfe } JPEG_marker; @@ -213,6 +185,9 @@ static int jpeg_get_info (FILE *fp, int *width, int *height) case JM_SOF10: case JM_SOF11: case JM_SOF13: case JM_SOF14: case JM_SOF15: get_unsigned_byte(fp); + if (xbb_output_mode != XBB_OUTPUT) { /* EBB_OUTPUT */ + xdensity = ydensity = 72.0 / 100.0; + } *height = (int)(get_unsigned_pair(fp) * ydensity + 0.5); *width = (int)(get_unsigned_pair(fp) * xdensity + 0.5); return 0; @@ -226,7 +201,6 @@ static int jpeg_get_info (FILE *fp, int *width, int *height) units = (int)get_unsigned_byte(fp); xden = (int)get_unsigned_pair(fp); yden = (int)get_unsigned_pair(fp); - fprintf(stderr, "%d:%d:%d\n", units, xden, yden); switch (units) { case 1: /* pixels per inch */ xdensity = 72.0 / xden; @@ -275,28 +249,36 @@ static int check_for_png (FILE *png_file) else return 1; } -static int png_get_info (FILE *png_file, int *width, int *height) +static int png_get_info(FILE *png_file, int *width, int *height) { png_structp png_ptr; png_infop png_info_ptr; png_uint_32 xppm, yppm; - rewind (png_file); + rewind(png_file); + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL || (png_info_ptr = png_create_info_struct (png_ptr)) == NULL) { + if (png_ptr == NULL || (png_info_ptr = png_create_info_struct(png_ptr)) == NULL) { if (png_ptr) png_destroy_read_struct(&png_ptr, NULL, NULL); return -1; } - png_init_io (png_ptr, png_file); + png_init_io(png_ptr, png_file); png_read_info(png_ptr, png_info_ptr); - *width = (int)png_get_image_width (png_ptr, png_info_ptr); + + *width = (int)png_get_image_width(png_ptr, png_info_ptr); *height = (int)png_get_image_height(png_ptr, png_info_ptr); - xppm = png_get_x_pixels_per_meter(png_ptr, png_info_ptr); - yppm = png_get_y_pixels_per_meter(png_ptr, png_info_ptr); - if (xppm > 0) - *width = (int)(*width * 72.0 / 0.0254 / xppm + 0.5); - if (yppm > 0) - *height = (int)(*height * 72.0 / 0.0254 / yppm + 0.5); + + if (xbb_output_mode == XBB_OUTPUT) { + xppm = png_get_x_pixels_per_meter(png_ptr, png_info_ptr); + yppm = png_get_y_pixels_per_meter(png_ptr, png_info_ptr); + if (xppm > 0) + *width = (int)(*width * 72.0 / 0.0254 / xppm + 0.5); + if (yppm > 0) + *height = (int)(*height * 72.0 / 0.0254 / yppm + 0.5); + } else { /* EBB_OUTPUT */ + *width = (int)(*width * 72.0 / 100.0 + 0.5); + *height = (int)(*height * 72.0 / 100.0 + 0.5); + } return 0; } @@ -323,31 +305,32 @@ static int rect_equal (pdf_obj *rect1, pdf_obj *rect2) return 1; } -static int pdf_get_info (FILE *image_file, int *llx, int *lly, int *urx, int *ury) +static int pdf_get_info (FILE *image_file, char *filename, int *llx, int *lly, int *urx, int *ury) { pdf_obj *page_tree; pdf_obj *bbox; + pdf_file *pf; page_tree = NULL; { pdf_obj *trailer, *catalog, *tmp; - trailer = pdf_open(image_file); - if (!trailer) { - WARN("Trailer not found! Corrupt PDF file?"); - pdf_close(); + pf = pdf_open(filename, image_file); + if (!pf) return -1; - } + + trailer = pdf_file_get_trailer(pf); + if (pdf_lookup_dict(trailer, "Encrypt")) { WARN("This PDF document is encrypted."); pdf_release_obj(trailer); - pdf_close(); + pdf_close(pf); return -1; } catalog = pdf_deref_obj(pdf_lookup_dict(trailer, "Root")); if (!catalog) { WARN("Catalog isn't where I expect it."); - pdf_close(); + pdf_close(pf); return -1; } pdf_release_obj(trailer); @@ -356,7 +339,7 @@ static int pdf_get_info (FILE *image_file, int *llx, int *lly, int *urx, int *ur } if (!page_tree) { WARN("Page tree not found."); - pdf_close(); + pdf_close(pf); return -1; } { @@ -385,21 +368,24 @@ static int pdf_get_info (FILE *image_file, int *llx, int *lly, int *urx, int *ur if (bbox) pdf_release_obj(bbox); bbox = tmp; - } + } else + pdf_release_obj(tmp); } if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "TrimBox")))) { if (!rect_equal(tmp, bbox)) { if (bbox) pdf_release_obj(bbox); bbox = tmp; - } + } else + pdf_release_obj(tmp); } if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "ArtBox")))) { if (!rect_equal(tmp, bbox)) { if (bbox) pdf_release_obj(bbox); bbox = tmp; - } + } else + pdf_release_obj(tmp); } if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "CropBox")))) { if (crop_box) @@ -412,11 +398,11 @@ static int pdf_get_info (FILE *image_file, int *llx, int *lly, int *urx, int *ur bbox = crop_box; } } + pdf_release_obj(page_tree); if (!bbox) { WARN("No BoundingBox information available."); - pdf_release_obj(page_tree); - pdf_close(); + pdf_close(pf); return -1; } @@ -425,7 +411,9 @@ static int pdf_get_info (FILE *image_file, int *llx, int *lly, int *urx, int *ur *urx = (int)pdf_number_value(pdf_get_array(bbox, 2)); *ury = (int)pdf_number_value(pdf_get_array(bbox, 3)); - pdf_close(); + pdf_release_obj(bbox); + + pdf_close(pf); return 0; } @@ -433,7 +421,7 @@ static void do_pdf (FILE *fp, char *filename) { int llx, lly, urx, ury; - if (pdf_get_info(fp, &llx, &lly, &urx, &ury) < 0) { + if (pdf_get_info(fp, filename, &llx, &lly, &urx, &ury) < 0) { fprintf (stderr, "%s does not look like a PDF file...\n", filename); return; } @@ -441,124 +429,81 @@ static void do_pdf (FILE *fp, char *filename) return; } -FILE *inputfile; - -int main (int argc, char *argv[]) +int extractbb (int argc, char *argv[], int mode) { mem_debug_init(); + xbb_output_mode = mode; + + pdf_files_init(); + pdf_set_version(5); - kpse_set_program_name (argv[0], NULL); - argc -= 1; - argv += 1; + + kpse_set_program_name(argv[0], NULL); + + argc -= 1; argv += 1; if (argc == 0) usage(); + while (argc > 0 && *argv[0] == '-') { switch (*(argv[0]+1)) { case 'b': xbb_file_mode = FOPEN_WBIN_MODE; + argc -= 1; argv += 1; + break; + case 'm': + xbb_output_mode = EBB_OUTPUT; + argc -= 1; argv += 1; + break; + case 'x': + xbb_output_mode = XBB_OUTPUT; + argc -= 1; argv += 1; + break; case 'v': verbose = 1; - argc -= 1; - argv += 1; + argc -= 1; argv += 1; break; case 'h': usage(); - argc -= 1; - argv += 1; + argc -= 1; argv += 1; break; default: usage(); } } for (; argc > 0; argc--, argv++) { + FILE *infile; char *kpse_file_name; if (!(kpse_file_name = kpse_find_pict(argv[0])) || - (inputfile = MFOPEN (kpse_file_name, FOPEN_RBIN_MODE)) == NULL) { - fprintf (stderr, "Can't find file (%s)...skipping\n", argv[0]); - continue; + (infile = MFOPEN(kpse_file_name, FOPEN_RBIN_MODE)) == NULL) { + fprintf(stderr, "Can't find file (%s)...skipping\n", argv[0]); + goto cont; } - if (check_for_jpeg(inputfile)) { - do_jpeg(inputfile, kpse_file_name); - MFCLOSE(inputfile); - continue; + if (check_for_jpeg(infile)) { + do_jpeg(infile, kpse_file_name); + goto cont; } - if (check_for_pdf(inputfile)) { - do_pdf(inputfile, kpse_file_name); - MFCLOSE(inputfile); - continue; + if (check_for_pdf(infile)) { + do_pdf(infile, kpse_file_name); + goto cont; } #ifdef HAVE_LIBPNG - if (check_for_png(inputfile)) { - do_png(inputfile, kpse_file_name); - MFCLOSE(inputfile); - continue; + if (check_for_png(infile)) { + do_png(infile, kpse_file_name); + goto cont; } #endif /* HAVE_LIBPNG */ - fprintf (stderr, "Can't handle file type for file named %s\n", - argv[0]); - } - mem_debug_check(); - - return 0; -} - -/* The following is here to prevent a link error. Other routines - that get linked with xbb call this, but it isn't actually needed - for xbb */ - -pdf_obj *get_reference (char **start, char *end) -{ - fprintf (stderr, "get_reference(): This should never have been called\n"); - return NULL; -} - -void error_cleanup(void) -{ - /* For now, this is a do nothing routine, but the stub is required - by error.h */ - return; -} - -static int p_dtoa (double value, int prec, char *buf) -{ - int n; - char *p, *q; - n = sprintf(buf, "%.*f", prec, value); - for (p = buf + n - 1; p > buf && *p != '.'; p--); - if (p > buf) { - for (q = buf + n - 1; q > p && *q == '0'; q--) { *q = '\0'; n--; } - if (q == p) { *q = '\0'; n--; } + fprintf(stderr, "Can't handle file type for file named %s\n", argv[0]); + cont: + if (kpse_file_name) + RELEASE(kpse_file_name); + if (infile) + MFCLOSE(infile); } - if (n == 2 && buf[0] == '-' && buf[1] == '0') { - buf[0] = '0'; buf[1] = '\0'; n = 1; - } - return n; -} -int pdf_sprint_number (char *buf, double value) -{ - int len = p_dtoa(value, 8, buf); - buf[len] = '\0'; - return len; -} - -pdf_obj *spc_lookup_reference (const char *key) -{ - return NULL; -} - -void pdf_enc_set_generation (unsigned generation) -{ - return; -} + pdf_files_close(); -void pdf_enc_set_label (unsigned long label) -{ - return; -} + mem_debug_check(); -void pdf_encrypt_data (unsigned char *data, unsigned long len) -{ - return; + return 0; } |