| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Matthias Franz, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
/* |
| 28 |
* JP2 & JPX baseline support |
| 29 |
* |
| 30 |
* 20150412 Added opacity (transparency) support. |
| 31 |
* TODO: XMP Metadata |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <math.h> |
| 35 |
#include "system.h" |
| 36 |
#include "error.h" |
| 37 |
#include "mem.h" |
| 38 |
|
| 39 |
#include "dpxconf.h" |
| 40 |
|
| 41 |
#include "pdfobj.h" |
| 42 |
|
| 43 |
#include "jp2image.h" |
| 44 |
|
| 45 |
/* Box types. */ |
| 46 |
#define JP2_BOX_JP__ 0x6a502020 /* Signature */ |
| 47 |
#define JP2_BOX_FTYP 0x66747970 /* File Type */ |
| 48 |
|
| 49 |
#define JP2_BOX_JP2H 0x6a703268 /* JP2 Header */ |
| 50 |
#define JP2_BOX_IHDR 0x69686472 /* Image Header */ |
| 51 |
#define JP2_BOX_BPCC 0x62706363 /* Bits Per Component */ |
| 52 |
#define JP2_BOX_COLR 0x636f6c72 /* Color Specification */ |
| 53 |
#define JP2_BOX_PCLR 0x70636c72 /* Palette */ |
| 54 |
#define JP2_BOX_CMAP 0x636d6170 /* Component Mapping */ |
| 55 |
#define JP2_BOX_CDEF 0x63646566 /* Channel Definition */ |
| 56 |
#define JP2_BOX_RES_ 0x72657320 /* Resolution */ |
| 57 |
#define JP2_BOX_RESC 0x72657363 /* Capture Resolution */ |
| 58 |
#define JP2_BOX_RESD 0x72657364 /* Default Display Resolution */ |
| 59 |
|
| 60 |
#define JP2_BOX_JP2C 0x6a703263 /* Contiguous Code Stream */ |
| 61 |
|
| 62 |
#define JPX_BOX_LBL_ 0x6c626c20 /* Label */ |
| 63 |
|
| 64 |
|
| 65 |
static unsigned int |
| 66 |
read_box_hdr (FILE *fp, unsigned int *lbox, unsigned int *tbox) |
| 67 |
{ |
| 68 |
unsigned int bytesread = 0; |
| 69 |
|
| 70 |
*lbox = get_unsigned_quad(fp); |
| 71 |
*tbox = get_unsigned_quad(fp); |
| 72 |
bytesread += 8; |
| 73 |
if (*lbox == 1) { |
| 74 |
if (get_unsigned_quad(fp) != 0) |
| 75 |
ERROR("JPEG2000: LBox value in JP2 file >32 bits.\nI can't handle this!"); |
| 76 |
*lbox = get_unsigned_quad(fp); |
| 77 |
bytesread += 8; |
| 78 |
} else if (*lbox > 1 && *lbox < 8) { |
| 79 |
WARN("JPEG2000: Unknown LBox value %lu in JP2 file!", lbox); |
| 80 |
} |
| 81 |
|
| 82 |
return bytesread; |
| 83 |
} |
| 84 |
|
| 85 |
#define FTYP_BR_JP2_ 0x6a703220 |
| 86 |
#define FTYP_BR_JPX_ 0x6a707820 |
| 87 |
#define FTYP_CL_JPXB 0x6a707862 |
| 88 |
|
| 89 |
static int |
| 90 |
check_jp___box (FILE *fp) |
| 91 |
{ |
| 92 |
if (get_unsigned_quad(fp) != 0x0c) |
| 93 |
return 0; |
| 94 |
if (get_unsigned_quad(fp) != JP2_BOX_JP__) |
| 95 |
return 0; |
| 96 |
/* Next 4 bytes shall be 0D 0A 87 0A */ |
| 97 |
if (get_unsigned_quad(fp) != 0x0D0A870A) |
| 98 |
return 0; |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
static int |
| 103 |
check_ftyp_data (FILE *fp, unsigned int size) |
| 104 |
{ |
| 105 |
int supported = 0; |
| 106 |
unsigned int BR, CLi; |
| 107 |
|
| 108 |
BR = get_unsigned_quad(fp); |
| 109 |
size -= 4; |
| 110 |
/* MinV = */ get_unsigned_quad(fp); |
| 111 |
size -= 4; |
| 112 |
switch (BR) { |
| 113 |
case FTYP_BR_JP2_: /* "jp2 " ... supported */ |
| 114 |
seek_relative(fp, size); |
| 115 |
size = 0; |
| 116 |
supported = 1; |
| 117 |
break; |
| 118 |
case FTYP_BR_JPX_: /* "jpx " ... baseline subset supported */ |
| 119 |
while (size > 0) { |
| 120 |
CLi = get_unsigned_quad(fp); |
| 121 |
if (CLi == FTYP_CL_JPXB) |
| 122 |
supported = 1; |
| 123 |
size -= 4; |
| 124 |
} |
| 125 |
break; |
| 126 |
default: |
| 127 |
WARN("JPEG2000: Unknown JPEG 2000 File Type box Brand field value."); |
| 128 |
seek_relative(fp, size); |
| 129 |
size = 0; |
| 130 |
supported = 0; |
| 131 |
} |
| 132 |
|
| 133 |
return supported; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
static void |
| 138 |
read_res__data (ximage_info *info, FILE *fp, unsigned int size) |
| 139 |
{ |
| 140 |
unsigned int VR_N, VR_D, HR_N, HR_D; |
| 141 |
unsigned char VR_E, HR_E; |
| 142 |
|
| 143 |
VR_N = get_unsigned_pair(fp); |
| 144 |
VR_D = get_unsigned_pair(fp); |
| 145 |
HR_N = get_unsigned_pair(fp); |
| 146 |
HR_D = get_unsigned_pair(fp); |
| 147 |
VR_E = get_unsigned_byte(fp); |
| 148 |
HR_E = get_unsigned_byte(fp); |
| 149 |
if (dpx_conf.compat_mode == dpx_mode_compat_mode) |
| 150 |
info->xdensity = info->ydensity = 72.0 / 100.0; |
| 151 |
else { |
| 152 |
info->xdensity = 72.0/(((double) HR_N / HR_D) * pow(10.0, HR_E) * 0.0254); |
| 153 |
info->ydensity = 72.0/(((double) VR_N / VR_D) * pow(10.0, VR_E) * 0.0254); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static int |
| 158 |
scan_res_ (ximage_info *info, FILE *fp, unsigned int size) |
| 159 |
{ |
| 160 |
unsigned int len, lbox, tbox; |
| 161 |
int have_resd = 0; |
| 162 |
|
| 163 |
while (size > 0) { |
| 164 |
len = read_box_hdr(fp, &lbox, &tbox); |
| 165 |
if (lbox == 0) { |
| 166 |
WARN("JPEG2000: Unexpected lbox value 0 in JP2 Resolution box."); |
| 167 |
break; |
| 168 |
} |
| 169 |
switch (tbox) { |
| 170 |
case JP2_BOX_RESC: |
| 171 |
if (!have_resd) { |
| 172 |
read_res__data(info, fp, lbox - len); |
| 173 |
} else { |
| 174 |
seek_relative(fp, lbox - len); |
| 175 |
} |
| 176 |
break; |
| 177 |
case JP2_BOX_RESD: |
| 178 |
read_res__data(info, fp, lbox - len); |
| 179 |
have_resd = 1; |
| 180 |
break; |
| 181 |
default: |
| 182 |
WARN("JPEG2000: Unknown JPEG 2000 box type in Resolution box."); |
| 183 |
seek_relative(fp, lbox - len); |
| 184 |
} |
| 185 |
size -= lbox; |
| 186 |
} |
| 187 |
|
| 188 |
return size == 0 ? 0 : -1; |
| 189 |
} |
| 190 |
|
| 191 |
/* Acrobat seems require Channel Definition box to be defined when image data |
| 192 |
* contains opacity channel. However, OpenJPEG (and maybe most of JPEG 2000 coders?) |
| 193 |
* does not write Channel Definition box so transparency will be ignored. |
| 194 |
*/ |
| 195 |
static int |
| 196 |
scan_cdef (ximage_info *info, int *smask, FILE *fp, unsigned int size) |
| 197 |
{ |
| 198 |
int opacity_channels = 0; |
| 199 |
int have_type0 = 0; |
| 200 |
unsigned int i, Cn, N, Typ, Asoc; |
| 201 |
|
| 202 |
*smask = 0; |
| 203 |
|
| 204 |
N = get_unsigned_pair(fp); |
| 205 |
if (size < N * 6 + 2) { |
| 206 |
WARN("JPEG2000: Inconsistent N value in Channel Definition box."); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
for (i = 0; i < N; i++) { |
| 210 |
Cn = get_unsigned_pair(fp); /* Cn */ |
| 211 |
Typ = get_unsigned_pair(fp); |
| 212 |
Asoc = get_unsigned_pair(fp); /* must be 0 for SMask */ |
| 213 |
if (Cn > N) |
| 214 |
WARN("JPEG2000: Invalid Cn value in Channel Definition box."); |
| 215 |
if (Typ == 1) { |
| 216 |
if (Asoc == 0) |
| 217 |
have_type0 = 1; |
| 218 |
opacity_channels++; |
| 219 |
} else if (Typ == 2) { |
| 220 |
opacity_channels++; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (opacity_channels == 1) |
| 225 |
*smask = have_type0 ? 1 : 0; |
| 226 |
else if (opacity_channels > 1) { |
| 227 |
WARN("JPEG2000: Unsupported transparency type. (ignored)"); |
| 228 |
} |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
static int |
| 234 |
scan_jp2h (ximage_info *info, int *smask, FILE *fp, unsigned int size) |
| 235 |
{ |
| 236 |
int error = 0, have_ihdr = 0; |
| 237 |
unsigned int len, lbox, tbox; |
| 238 |
|
| 239 |
while (size > 0 && !error) { |
| 240 |
len = read_box_hdr(fp, &lbox, &tbox); |
| 241 |
if (lbox == 0) { |
| 242 |
WARN("JPEG2000: Unexpected lbox value 0 in JP2 Header box..."); |
| 243 |
error = -1; |
| 244 |
break; |
| 245 |
} |
| 246 |
switch (tbox) { |
| 247 |
case JP2_BOX_IHDR: |
| 248 |
info->height = get_unsigned_quad(fp); |
| 249 |
info->width = get_unsigned_quad(fp); |
| 250 |
info->num_components = get_unsigned_pair(fp); |
| 251 |
/* c = */ get_unsigned_byte(fp); /* BPC - 1 */ |
| 252 |
/* c = */ get_unsigned_byte(fp); /* C: Compression type */ |
| 253 |
/* c = */ get_unsigned_byte(fp); /* UnkC */ |
| 254 |
/* c = */ get_unsigned_byte(fp); /* IPR */ |
| 255 |
have_ihdr = 1; |
| 256 |
break; |
| 257 |
case JP2_BOX_RES_: |
| 258 |
error = scan_res_(info, fp, lbox - len); |
| 259 |
break; |
| 260 |
case JP2_BOX_CDEF: |
| 261 |
error = scan_cdef(info, smask, fp, lbox - len); |
| 262 |
break; |
| 263 |
case JP2_BOX_BPCC: case JP2_BOX_COLR: case JP2_BOX_PCLR: |
| 264 |
case JP2_BOX_CMAP: |
| 265 |
case JPX_BOX_LBL_: |
| 266 |
seek_relative(fp, lbox - len); |
| 267 |
break; |
| 268 |
default: |
| 269 |
WARN("JPEG2000: Unknown JPEG 2000 box in JP2 Header box."); |
| 270 |
seek_relative(fp, lbox - len); |
| 271 |
error = -1; |
| 272 |
} |
| 273 |
size -= lbox; |
| 274 |
} |
| 275 |
|
| 276 |
if (!have_ihdr) |
| 277 |
WARN("JPEG2000: Expecting JPEG 2000 Image Header box but could not find."); |
| 278 |
return (!error && have_ihdr && size == 0) ? 0 : -1; |
| 279 |
} |
| 280 |
|
| 281 |
static int |
| 282 |
scan_file (ximage_info *info, int *smask, FILE *fp) |
| 283 |
{ |
| 284 |
int error = 0, have_jp2h = 0; |
| 285 |
int size; |
| 286 |
unsigned int len, lbox, tbox; |
| 287 |
|
| 288 |
|
| 289 |
size = file_size(fp); |
| 290 |
|
| 291 |
/* Should have already been checked before. */ |
| 292 |
/* JPEG 2000 Singature box */ |
| 293 |
if (!check_jp___box(fp)) |
| 294 |
return -1; |
| 295 |
size -= 12; |
| 296 |
/* File Type box shall immediately follow */ |
| 297 |
len = read_box_hdr(fp, &lbox, &tbox); |
| 298 |
if (tbox != JP2_BOX_FTYP) |
| 299 |
return -1; |
| 300 |
if (!check_ftyp_data(fp, lbox - len)) |
| 301 |
return -1; |
| 302 |
size -= lbox; |
| 303 |
|
| 304 |
/* Search for JP2 Header box */ |
| 305 |
while (size > 0 && !error) { |
| 306 |
len = read_box_hdr(fp, &lbox, &tbox); |
| 307 |
if (lbox == 0) |
| 308 |
lbox = size; |
| 309 |
switch (tbox) { |
| 310 |
case JP2_BOX_JP2H: |
| 311 |
error = scan_jp2h(info, smask, fp, lbox - len); |
| 312 |
have_jp2h = 1; |
| 313 |
break; |
| 314 |
case JP2_BOX_JP2C: |
| 315 |
/* JP2 requires JP2H appears before JP2C. */ |
| 316 |
if (!have_jp2h) |
| 317 |
WARN("JPEG2000: JPEG 2000 Codestream box found before JP2 Header box."); |
| 318 |
seek_relative(fp, lbox - len); |
| 319 |
break; |
| 320 |
default: |
| 321 |
seek_relative(fp, lbox - len); |
| 322 |
} |
| 323 |
size -= lbox; |
| 324 |
} |
| 325 |
|
| 326 |
/* From ISO/IEC 15444-2 M.9.2.7 |
| 327 |
* The JP2 Header box shall be found in the file before the first |
| 328 |
* Contiguous Codestream box, Fragment Table box, Media Data box, |
| 329 |
* Codestream Header box, and Compositing Layer Header box. ... |
| 330 |
*/ |
| 331 |
if (!have_jp2h && !error) { |
| 332 |
WARN("JPEG2000: No JP2 Header box found. Not a JP2/JPX baseline file?"); |
| 333 |
error = -1; |
| 334 |
} |
| 335 |
return error; |
| 336 |
} |
| 337 |
|
| 338 |
int |
| 339 |
check_for_jp2 (FILE *fp) |
| 340 |
{ |
| 341 |
unsigned int len, lbox, tbox; |
| 342 |
|
| 343 |
if (!fp) |
| 344 |
return 0; |
| 345 |
|
| 346 |
rewind(fp); |
| 347 |
|
| 348 |
/* JPEG 2000 Singature box */ |
| 349 |
if (!check_jp___box(fp)) |
| 350 |
return 0; |
| 351 |
|
| 352 |
/* File Type box shall immediately follow */ |
| 353 |
len = read_box_hdr(fp, &lbox, &tbox); |
| 354 |
if (tbox != JP2_BOX_FTYP) |
| 355 |
return 0; |
| 356 |
if (!check_ftyp_data(fp, lbox - len)) |
| 357 |
return 0; |
| 358 |
|
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
int |
| 363 |
jp2_include_image (pdf_ximage *ximage, FILE *fp) |
| 364 |
{ |
| 365 |
int smask = 0; |
| 366 |
pdf_obj *stream, *stream_dict; |
| 367 |
ximage_info info; |
| 368 |
|
| 369 |
if (pdf_check_version(1, 5) < 0) { |
| 370 |
WARN("JPEG 2000 support requires PDF version >= 1.5 (Current setting %d.%d)\n", pdf_get_version_major(), pdf_get_version_minor()); |
| 371 |
return -1; |
| 372 |
} |
| 373 |
|
| 374 |
pdf_ximage_init_image_info(&info); |
| 375 |
stream = stream_dict = NULL; |
| 376 |
|
| 377 |
rewind(fp); |
| 378 |
if (scan_file(&info, &smask, fp) < 0) { |
| 379 |
WARN("JPEG2000: Reading JPEG 2000 file failed."); |
| 380 |
return -1; |
| 381 |
} |
| 382 |
|
| 383 |
stream = pdf_new_stream(0); |
| 384 |
stream_dict = pdf_stream_dict(stream); |
| 385 |
pdf_add_dict(stream_dict, |
| 386 |
pdf_new_name("Filter"), pdf_new_name("JPXDecode")); |
| 387 |
if (smask) |
| 388 |
pdf_add_dict(stream_dict, |
| 389 |
pdf_new_name("SMaskInData"), pdf_new_number(1)); |
| 390 |
/* Read whole file */ |
| 391 |
{ |
| 392 |
int nb_read; |
| 393 |
rewind(fp); |
| 394 |
while ((nb_read = |
| 395 |
fread(work_buffer, sizeof(char), WORK_BUFFER_SIZE, fp)) > 0) |
| 396 |
pdf_add_stream(stream, work_buffer, nb_read); |
| 397 |
} |
| 398 |
|
| 399 |
pdf_ximage_set_image(ximage, &info, stream); |
| 400 |
|
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
int |
| 405 |
jp2_get_bbox (FILE *fp, int *width, int *height, |
| 406 |
double *xdensity, double *ydensity) |
| 407 |
{ |
| 408 |
int r, smask = 0; |
| 409 |
ximage_info info; |
| 410 |
|
| 411 |
pdf_ximage_init_image_info(&info); |
| 412 |
|
| 413 |
rewind(fp); |
| 414 |
r = scan_file(&info, &smask, fp); |
| 415 |
|
| 416 |
*width = info.width; |
| 417 |
*height = info.height; |
| 418 |
*xdensity = info.xdensity; |
| 419 |
*ydensity = info.ydensity; |
| 420 |
|
| 421 |
return r; |
| 422 |
} |