| 1 |
/* This is extractbb, a bounding box extraction program. |
| 2 |
Copyright (C) 2008-2026 by Jin-Hwan Cho and Matthias Franz |
| 3 |
and the dvipdfmx project team. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
#include <config.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <time.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "dpxconf.h" |
| 28 |
#include "dpxutil.h" |
| 29 |
|
| 30 |
#include "numbers.h" |
| 31 |
#include "system.h" |
| 32 |
#include "mem.h" |
| 33 |
#include "error.h" |
| 34 |
#include "mfileio.h" |
| 35 |
#include "pdfobj.h" |
| 36 |
#include "pdfdoc.h" |
| 37 |
#include "pdfparse.h" |
| 38 |
#include "pdfdraw.h" |
| 39 |
|
| 40 |
#include "bmpimage.h" |
| 41 |
#include "jpegimage.h" |
| 42 |
#include "jp2image.h" |
| 43 |
#include "pngimage.h" |
| 44 |
|
| 45 |
#include "dvipdfmx.h" |
| 46 |
#include "pdflimits.h" |
| 47 |
|
| 48 |
static enum pdf_page_boundary PageBox = pdf_page_boundary__auto; |
| 49 |
|
| 50 |
static int Include_Page = 1; |
| 51 |
|
| 52 |
static void show_version(void) |
| 53 |
{ |
| 54 |
fprintf (stdout, "\nThis is %s Version " VERSION "\n", my_name); |
| 55 |
fprintf (stdout, "\nCopyright (C) 2008-2026 by Jin-Hwan Cho and Matthias Franz\n"); |
| 56 |
fprintf (stdout, "\nThis is free software; you can redistribute it and/or modify\n"); |
| 57 |
fprintf (stdout, "it under the terms of the GNU General Public License as published by\n"); |
| 58 |
fprintf (stdout, "the Free Software Foundation; either version 2 of the License, or\n"); |
| 59 |
fprintf (stdout, "(at your option) any later version.\n"); |
| 60 |
} |
| 61 |
|
| 62 |
static void show_usage(void) |
| 63 |
{ |
| 64 |
printf ("\nUsage: %s [-B pagebox] [-p page] [-q|-v] [-O] [-m|-x] FILE...\n", my_name); |
| 65 |
printf (" %s --help|--version\n", my_name); |
| 66 |
printf ("Extract bounding box from PDF, PNG, JPEG, JP2, or BMP file; default output below.\n"); |
| 67 |
printf ("\nOptions:\n"); |
| 68 |
printf (" -B pagebox\tSpecify a PDF pagebox for bounding box\n"); |
| 69 |
printf (" \tpagebox=cropbox, mediabox, artbox, trimbox, bleedbox\n"); |
| 70 |
printf (" -h | --help\tShow this help message and exit\n"); |
| 71 |
printf (" --version\tOutput version information and exit\n"); |
| 72 |
printf (" -p page\tSpecify a PDF page to extract bounding box\n"); |
| 73 |
printf (" -q\t\tBe quiet\n"); |
| 74 |
printf (" -v\t\tBe verbose\n"); |
| 75 |
printf (" -O\t\tWrite output to stdout\n"); |
| 76 |
printf (" -m\t\tOutput .bb file used in DVIPDFM%s\n", my_name[1] == 'b' ? " (default)" : ""); |
| 77 |
printf (" -x\t\tOutput .xbb file used in DVIPDFMx%s\n", my_name[1] == 'b' ? "" : " (default)"); |
| 78 |
} |
| 79 |
|
| 80 |
static void usage(void) |
| 81 |
{ |
| 82 |
fprintf(stdout, "\nTry \"%s --help\" for more information.\n", my_name); |
| 83 |
exit(1); |
| 84 |
} |
| 85 |
|
| 86 |
static void do_time(FILE *file) |
| 87 |
{ |
| 88 |
time_t current_time; |
| 89 |
struct tm *bd_time; |
| 90 |
|
| 91 |
current_time = dpx_util_get_unique_time_if_given(); |
| 92 |
if (current_time == INVALID_EPOCH_VALUE) { |
| 93 |
time(¤t_time); |
| 94 |
bd_time = localtime(¤t_time); |
| 95 |
} else { |
| 96 |
bd_time = gmtime(¤t_time); |
| 97 |
} |
| 98 |
fprintf(file, "%%%%CreationDate: %s\n", asctime(bd_time)); |
| 99 |
} |
| 100 |
|
| 101 |
const char *extensions[] = { |
| 102 |
".ai", ".AI", ".bmp", ".BMP", ".jpeg", ".JPEG", ".jpg", ".JPG", |
| 103 |
".jp2", ".JP2", ".jpf", ".JPF", ".pdf", ".PDF", ".png", ".PNG" |
| 104 |
}; |
| 105 |
|
| 106 |
static int xbb_to_file = 1; |
| 107 |
|
| 108 |
static char *make_xbb_filename(const char *name) |
| 109 |
{ |
| 110 |
char *result; |
| 111 |
int i; |
| 112 |
|
| 113 |
for (i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++) { |
| 114 |
if (strlen(extensions[i]) < strlen(name) && |
| 115 |
strncmp(name+strlen(name)-strlen(extensions[i]), extensions[i], strlen(extensions[i])) == 0) |
| 116 |
break; |
| 117 |
} |
| 118 |
if (i == sizeof(extensions) / sizeof(extensions[0])) { |
| 119 |
WARN("%s: Filename does not end in a recognizable extension.\n", name); |
| 120 |
result = NEW(strlen(name)+strlen(".xbb")+1, char); |
| 121 |
strcpy(result, name); |
| 122 |
} else { /* Remove extension */ |
| 123 |
size_t len; |
| 124 |
len = strlen(name) - strlen(extensions[i]); |
| 125 |
result = NEW(len+strlen(".xbb")+1, char); |
| 126 |
strncpy(result, name, len); |
| 127 |
result[len] = 0; |
| 128 |
} |
| 129 |
strcat(result, ((dpx_conf.compat_mode == dpx_mode_compat_mode) ? ".bb" : ".xbb")); |
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
static void write_xbb(char *fname, |
| 134 |
double bbllx_f, double bblly_f, |
| 135 |
double bburx_f, double bbury_f, |
| 136 |
int pdf_version, int pagecount) |
| 137 |
{ |
| 138 |
char *outname = NULL; |
| 139 |
FILE *fp = NULL; |
| 140 |
|
| 141 |
int bbllx = ROUND(bbllx_f, 1.0), bblly = ROUND(bblly_f, 1.0); |
| 142 |
int bburx = ROUND(bburx_f, 1.0), bbury = ROUND(bbury_f, 1.0); |
| 143 |
|
| 144 |
if (xbb_to_file) { |
| 145 |
outname = make_xbb_filename(fname); |
| 146 |
if (!kpse_out_name_ok(outname) || !(fp = MFOPEN(outname, FOPEN_W_MODE))) { |
| 147 |
ERROR("Unable to open output file: %s\n", outname); |
| 148 |
} |
| 149 |
} else { |
| 150 |
fp = stdout; |
| 151 |
#ifdef WIN32 |
| 152 |
setmode(fileno(fp), _O_BINARY); |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
if (dpx_conf.verbose_level > 0) { |
| 157 |
MESG("Writing to %s: ", xbb_to_file ? outname : "stdout"); |
| 158 |
MESG("Bounding box: %d %d %d %d\n", bbllx, bblly, bburx, bbury); |
| 159 |
} |
| 160 |
|
| 161 |
fprintf(fp, "%%%%Title: %s\n", fname); |
| 162 |
fprintf(fp, "%%%%Creator: extractbb %s\n", VERSION); |
| 163 |
fprintf(fp, "%%%%BoundingBox: %d %d %d %d\n", bbllx, bblly, bburx, bbury); |
| 164 |
|
| 165 |
if (dpx_conf.compat_mode != dpx_mode_compat_mode) { |
| 166 |
/* Note: |
| 167 |
* According to Adobe Technical Note #5644, the arguments to |
| 168 |
* "%%HiResBoundingBox:" must be of type real. And according |
| 169 |
* to the PostScript Language Reference, a real number must |
| 170 |
* be written with a decimal point (or an exponent). Hence |
| 171 |
* it seems illegal to replace "0.0" by "0". |
| 172 |
*/ |
| 173 |
fprintf(fp, "%%%%HiResBoundingBox: %f %f %f %f\n", |
| 174 |
bbllx_f, bblly_f, bburx_f, bbury_f); |
| 175 |
if (pdf_version >= 0) { |
| 176 |
fprintf(fp, "%%%%PDFVersion: %d.%d\n", pdf_version/10, pdf_version%10); |
| 177 |
fprintf(fp, "%%%%Pages: %d\n", pagecount); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
do_time(fp); |
| 182 |
|
| 183 |
if (xbb_to_file) { |
| 184 |
RELEASE(outname); |
| 185 |
MFCLOSE(fp); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static void do_bmp (FILE *fp, char *filename) |
| 190 |
{ |
| 191 |
int width, height; |
| 192 |
double xdensity, ydensity; |
| 193 |
|
| 194 |
if (bmp_get_bbox(fp, &width, &height, &xdensity, &ydensity) < 0) { |
| 195 |
WARN("%s does not look like a BMP file...\n", filename); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
write_xbb(filename, 0, 0, xdensity*width, ydensity*height, -1, -1); |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
static void do_jpeg (FILE *fp, char *filename) |
| 204 |
{ |
| 205 |
int width, height; |
| 206 |
double xdensity, ydensity; |
| 207 |
|
| 208 |
if (jpeg_get_bbox(fp, &width, &height, &xdensity, &ydensity) < 0) { |
| 209 |
WARN("%s does not look like a JPEG file...\n", filename); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
write_xbb(filename, 0, 0, xdensity*width, ydensity*height, -1, -1); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
static void do_jp2 (FILE *fp, char *filename) |
| 218 |
{ |
| 219 |
int width, height; |
| 220 |
double xdensity, ydensity; |
| 221 |
|
| 222 |
if (jp2_get_bbox(fp, &width, &height, &xdensity, &ydensity) < 0) { |
| 223 |
WARN("%s does not look like a JP2/JPX file...\n", filename); |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
write_xbb(filename, 0, 0, xdensity*width, ydensity*height, -1, -1); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
#ifdef HAVE_LIBPNG |
| 232 |
static void do_png (FILE *fp, char *filename) |
| 233 |
{ |
| 234 |
uint32_t width, height; |
| 235 |
double xdensity, ydensity; |
| 236 |
|
| 237 |
if (png_get_bbox(fp, &width, &height, &xdensity, &ydensity) < 0) { |
| 238 |
WARN("%s does not look like a PNG file...\n", filename); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
write_xbb(filename, 0, 0, xdensity*width, ydensity*height, -1, -1); |
| 243 |
return; |
| 244 |
} |
| 245 |
#endif /* HAVE_LIBPNG */ |
| 246 |
|
| 247 |
static void do_pdf (FILE *fp, char *filename) |
| 248 |
{ |
| 249 |
pdf_obj *page; |
| 250 |
pdf_file *pf; |
| 251 |
int page_no = Include_Page; |
| 252 |
int count; |
| 253 |
pdf_rect bbox; |
| 254 |
pdf_tmatrix matrix; |
| 255 |
pdf_coord p1, p2, p3, p4; |
| 256 |
|
| 257 |
pf = pdf_open(filename, fp); |
| 258 |
if (!pf) { |
| 259 |
WARN("%s does not look like a PDF file...\n", filename); |
| 260 |
return; |
| 261 |
} |
| 262 |
count = pdf_doc_get_page_count(pf); |
| 263 |
page = pdf_doc_get_page(pf, page_no, NULL, PageBox, &bbox, &matrix, NULL); |
| 264 |
|
| 265 |
pdf_close(pf); |
| 266 |
|
| 267 |
if (!page) |
| 268 |
return; |
| 269 |
|
| 270 |
pdf_release_obj(page); |
| 271 |
|
| 272 |
/* Image's attribute "bbox" here is affected by /Rotate entry of included |
| 273 |
* PDF page. |
| 274 |
*/ |
| 275 |
p1.x = bbox.llx; p1.y = bbox.lly; |
| 276 |
pdf_dev_transform(&p1, &matrix); |
| 277 |
p2.x = bbox.urx; p2.y = bbox.lly; |
| 278 |
pdf_dev_transform(&p2, &matrix); |
| 279 |
p3.x = bbox.urx; p3.y = bbox.ury; |
| 280 |
pdf_dev_transform(&p3, &matrix); |
| 281 |
p4.x = bbox.llx; p4.y = bbox.ury; |
| 282 |
pdf_dev_transform(&p4, &matrix); |
| 283 |
|
| 284 |
bbox.llx = min4(p1.x, p2.x, p3.x, p4.x); |
| 285 |
bbox.lly = min4(p1.y, p2.y, p3.y, p4.y); |
| 286 |
bbox.urx = max4(p1.x, p2.x, p3.x, p4.x); |
| 287 |
bbox.ury = max4(p1.y, p2.y, p3.y, p4.y); |
| 288 |
|
| 289 |
write_xbb(filename, bbox.llx, bbox.lly, bbox.urx, bbox.ury, |
| 290 |
pdf_file_get_version(pf), count); |
| 291 |
} |
| 292 |
|
| 293 |
static const char *optstrig = ":hB:p:qvObmx"; |
| 294 |
|
| 295 |
static struct option long_options[] = { |
| 296 |
{"help", 0, 0, 'h'}, |
| 297 |
{"version", 0, 0, 130}, |
| 298 |
{0, 0, 0, 0} |
| 299 |
}; |
| 300 |
|
| 301 |
int extractbb (int argc, char *argv[]) |
| 302 |
{ |
| 303 |
int c; |
| 304 |
|
| 305 |
pdf_files_init(); |
| 306 |
|
| 307 |
dpx_conf.is_xbb = 1; |
| 308 |
|
| 309 |
opterr = 0; |
| 310 |
|
| 311 |
while ((c = getopt_long(argc, argv, optstrig, long_options, NULL)) != -1) { |
| 312 |
switch(c) { |
| 313 |
case 'h': |
| 314 |
show_usage(); |
| 315 |
exit(0); |
| 316 |
|
| 317 |
case 130: |
| 318 |
show_version(); |
| 319 |
exit(0); |
| 320 |
|
| 321 |
case 'B': |
| 322 |
if (strcasecmp (optarg, "cropbox") == 0) PageBox = pdf_page_boundary_cropbox; |
| 323 |
else if (strcasecmp (optarg, "mediabox") == 0) PageBox = pdf_page_boundary_mediabox; |
| 324 |
else if (strcasecmp (optarg, "artbox") == 0) PageBox = pdf_page_boundary_artbox; |
| 325 |
else if (strcasecmp (optarg, "trimbox") == 0) PageBox = pdf_page_boundary_trimbox; |
| 326 |
else if (strcasecmp (optarg, "bleedbox") == 0) PageBox = pdf_page_boundary_bleedbox; |
| 327 |
else { |
| 328 |
fprintf(stderr, "%s: Invalid argument \"-B %s\"", my_name, optarg); |
| 329 |
usage(); |
| 330 |
} |
| 331 |
break; |
| 332 |
|
| 333 |
case 'p': |
| 334 |
Include_Page = atol(optarg); |
| 335 |
if (Include_Page == 0) |
| 336 |
Include_Page = 1; |
| 337 |
break; |
| 338 |
|
| 339 |
case 'q': |
| 340 |
dpx_conf.verbose_level = 0; |
| 341 |
break; |
| 342 |
case 'v': |
| 343 |
dpx_conf.verbose_level++; |
| 344 |
break; |
| 345 |
|
| 346 |
case 'O': |
| 347 |
xbb_to_file = 0; |
| 348 |
case 'b': /* Ignored for backward compatibility */ |
| 349 |
break; |
| 350 |
|
| 351 |
case 'm': |
| 352 |
dpx_conf.compat_mode = dpx_mode_compat_mode; |
| 353 |
break; |
| 354 |
case 'x': |
| 355 |
dpx_conf.compat_mode = dpx_mode_normal_mode; |
| 356 |
break; |
| 357 |
|
| 358 |
default: |
| 359 |
fprintf(stderr, "%s: %s \"-%c\"", my_name, |
| 360 |
c == ':' ? "Missing argument for" : "Unknown option", |
| 361 |
optopt); |
| 362 |
usage(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if (optind >= argc) { |
| 367 |
fprintf (stderr, "%s: Missing filename argument", my_name); |
| 368 |
usage(); |
| 369 |
} |
| 370 |
|
| 371 |
for (; optind < argc; optind++) { |
| 372 |
FILE *infile = NULL; |
| 373 |
char *kpse_file_name = NULL; |
| 374 |
|
| 375 |
if (kpse_in_name_ok(argv[optind])) { |
| 376 |
infile = MFOPEN(argv[optind], FOPEN_RBIN_MODE); |
| 377 |
if (infile) { |
| 378 |
kpse_file_name = xstrdup(argv[optind]); |
| 379 |
} else { |
| 380 |
kpse_file_name = kpse_find_pict(argv[optind]); |
| 381 |
if (kpse_file_name && kpse_in_name_ok(kpse_file_name)) |
| 382 |
infile = MFOPEN(kpse_file_name, FOPEN_RBIN_MODE); |
| 383 |
} |
| 384 |
} |
| 385 |
if (infile == NULL) { |
| 386 |
WARN("Can't find file (%s), or it is forbidden to read ...skipping\n", argv[optind]); |
| 387 |
goto cont; |
| 388 |
} |
| 389 |
if (check_for_bmp(infile)) { |
| 390 |
do_bmp(infile, kpse_file_name); |
| 391 |
goto cont; |
| 392 |
} |
| 393 |
if (check_for_jpeg(infile)) { |
| 394 |
do_jpeg(infile, kpse_file_name); |
| 395 |
goto cont; |
| 396 |
} |
| 397 |
if (check_for_jp2(infile)) { |
| 398 |
do_jp2(infile, kpse_file_name); |
| 399 |
goto cont; |
| 400 |
} |
| 401 |
if (check_for_pdf(infile)) { |
| 402 |
do_pdf(infile, kpse_file_name); |
| 403 |
goto cont; |
| 404 |
} |
| 405 |
#ifdef HAVE_LIBPNG |
| 406 |
if (check_for_png(infile)) { |
| 407 |
do_png(infile, kpse_file_name); |
| 408 |
goto cont; |
| 409 |
} |
| 410 |
#endif /* HAVE_LIBPNG */ |
| 411 |
WARN("Can't handle file type for file named %s\n", argv[optind]); |
| 412 |
cont: |
| 413 |
if (kpse_file_name) |
| 414 |
RELEASE(kpse_file_name); |
| 415 |
if (infile) |
| 416 |
MFCLOSE(infile); |
| 417 |
} |
| 418 |
|
| 419 |
pdf_files_close(); |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |