| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 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 |
/* No page independence here... |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include <config.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "system.h" |
| 31 |
#include "mem.h" |
| 32 |
#include "error.h" |
| 33 |
|
| 34 |
#include "dpxconf.h" |
| 35 |
#include "dpxfile.h" |
| 36 |
|
| 37 |
#include "pdfdoc.h" |
| 38 |
#include "pdfdev.h" |
| 39 |
#include "pdfresource.h" |
| 40 |
|
| 41 |
#include "pdfcolor.h" |
| 42 |
|
| 43 |
int |
| 44 |
pdf_color_type (const pdf_color *color) |
| 45 |
{ |
| 46 |
ASSERT(color); |
| 47 |
|
| 48 |
return color->type; |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
pdf_color_rgbcolor (pdf_color *color, double r, double g, double b) |
| 53 |
{ |
| 54 |
ASSERT(color); |
| 55 |
|
| 56 |
if (r < 0.0 || r > 1.0) { |
| 57 |
WARN("Invalid color value specified: red=%g", r); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
if (g < 0.0 || g > 1.0) { |
| 61 |
WARN("Invalid color value specified: green=%g", g); |
| 62 |
return -1; |
| 63 |
} |
| 64 |
if (b < 0.0 || b > 1.0) { |
| 65 |
WARN("Invalid color value specified: blue=%g", b); |
| 66 |
return -1; |
| 67 |
} |
| 68 |
color->values[0] = r; |
| 69 |
color->values[1] = g; |
| 70 |
color->values[2] = b; |
| 71 |
|
| 72 |
color->res_id = -1; |
| 73 |
color->type = PDF_COLORSPACE_TYPE_RGB; |
| 74 |
color->num_components = 3; |
| 75 |
|
| 76 |
color->spot_color_name = NULL; |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
pdf_color_cmykcolor (pdf_color *color, |
| 83 |
double c, double m, double y, double k) |
| 84 |
{ |
| 85 |
ASSERT(color); |
| 86 |
|
| 87 |
if (c < 0.0 || c > 1.0) { |
| 88 |
WARN("Invalid color value specified: cyan=%g", c); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
if (m < 0.0 || m > 1.0) { |
| 92 |
WARN("Invalid color value specified: magenta=%g", m); |
| 93 |
return -1; |
| 94 |
} |
| 95 |
if (y < 0.0 || y > 1.0) { |
| 96 |
WARN("Invalid color value specified: yellow=%g", y); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
if (k < 0.0 || k > 1.0) { |
| 100 |
WARN("Invalid color value specified: black=%g", k); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
color->values[0] = c; |
| 105 |
color->values[1] = m; |
| 106 |
color->values[2] = y; |
| 107 |
color->values[3] = k; |
| 108 |
|
| 109 |
color->res_id = -1; |
| 110 |
color->type = PDF_COLORSPACE_TYPE_CMYK; |
| 111 |
color->num_components = 4; |
| 112 |
|
| 113 |
color->spot_color_name = NULL; |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
int |
| 119 |
pdf_color_graycolor (pdf_color *color, double g) |
| 120 |
{ |
| 121 |
ASSERT(color); |
| 122 |
|
| 123 |
if (g < 0.0 || g > 1.0) { |
| 124 |
WARN("Invalid color value specified: gray=%g", g); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
|
| 128 |
color->values[0] = g; |
| 129 |
|
| 130 |
color->res_id = -1; |
| 131 |
color->type = PDF_COLORSPACE_TYPE_GRAY; |
| 132 |
color->num_components = 1; |
| 133 |
|
| 134 |
color->spot_color_name = NULL; |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
int |
| 140 |
pdf_color_spotcolor (pdf_color *color, char* name, double c) |
| 141 |
{ |
| 142 |
ASSERT(color); |
| 143 |
|
| 144 |
if (c < 0.0 || c > 1.0) { |
| 145 |
WARN("Invalid color value specified: grade=%g", c); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
color->values[0] = c; |
| 150 |
color->values[1] = 0.0; /* Dummy */ |
| 151 |
|
| 152 |
color->res_id = -1; /* ??? */ |
| 153 |
color->type = PDF_COLORSPACE_TYPE_SPOT; |
| 154 |
color->num_components = 2; |
| 155 |
|
| 156 |
color->spot_color_name = name; |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
void |
| 163 |
pdf_color_copycolor (pdf_color *color1, const pdf_color *color2) |
| 164 |
{ |
| 165 |
ASSERT(color1 && color2); |
| 166 |
|
| 167 |
memcpy(color1, color2, sizeof(pdf_color)); |
| 168 |
} |
| 169 |
|
| 170 |
/* Brighten up a color. f == 0 means no change, f == 1 means white. */ |
| 171 |
void |
| 172 |
pdf_color_brighten_color (pdf_color *dst, const pdf_color *src, double f) |
| 173 |
{ |
| 174 |
ASSERT(dst && src); |
| 175 |
|
| 176 |
if (src->type != PDF_COLORSPACE_TYPE_RGB && |
| 177 |
src->type != PDF_COLORSPACE_TYPE_CMYK && |
| 178 |
src->type != PDF_COLORSPACE_TYPE_GRAY) { |
| 179 |
pdf_color_copycolor(dst, src); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
if (f == 1.0) { |
| 184 |
pdf_color_white(dst); |
| 185 |
} else { |
| 186 |
double f0, f1; |
| 187 |
int n; |
| 188 |
|
| 189 |
pdf_color_copycolor(dst, src); |
| 190 |
n = src->num_components; |
| 191 |
f1 = n == 4 ? 0.0 : f; /* n == 4 is CMYK, others are RGB and Gray */ |
| 192 |
f0 = 1.0-f; |
| 193 |
|
| 194 |
while (n--) |
| 195 |
dst->values[n] = f0 * src->values[n] + f1; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* TODO: remove "is_white"... |
| 200 |
* pdfdoc.c only use this but not necessary if we have a flag have_bgcolor |
| 201 |
* to indicate if bg color was set. |
| 202 |
*/ |
| 203 |
int |
| 204 |
pdf_color_is_white (const pdf_color *color) |
| 205 |
{ |
| 206 |
int n; |
| 207 |
double f; |
| 208 |
|
| 209 |
ASSERT(color); |
| 210 |
|
| 211 |
switch (color->type) { |
| 212 |
case PDF_COLORSPACE_TYPE_GRAY: |
| 213 |
case PDF_COLORSPACE_TYPE_RGB: |
| 214 |
f = 1.0; |
| 215 |
break; |
| 216 |
case PDF_COLORSPACE_TYPE_CMYK: |
| 217 |
f = 0.0; |
| 218 |
break; |
| 219 |
default: |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
n = color->num_components; |
| 224 |
while (n--) |
| 225 |
if (color->values[n] != f) |
| 226 |
return 0; |
| 227 |
|
| 228 |
return 1; |
| 229 |
} |
| 230 |
|
| 231 |
/* TODO: make_resource_name() in pdfresource.c with configurable prefix. */ |
| 232 |
int |
| 233 |
pdf_color_set_color (const pdf_color *color, char *buffer, size_t buffer_len, char mask) |
| 234 |
{ |
| 235 |
int len = 0; |
| 236 |
int i; |
| 237 |
|
| 238 |
{ |
| 239 |
size_t estimate = 0; |
| 240 |
if (color->num_components > 0) { |
| 241 |
estimate += 5 * (color->num_components + 1) + 4; /* Assuming color values [0, 1]... */ |
| 242 |
} |
| 243 |
estimate += strlen(" /DeiceGray CS"); |
| 244 |
if (estimate + 1 > buffer_len) { |
| 245 |
WARN("Not enough buffer space allocated for writing set_color op..."); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
switch (pdf_color_type(color)) { |
| 251 |
case PDF_COLORSPACE_TYPE_DEVICEGRAY: |
| 252 |
{ |
| 253 |
len += sprintf(buffer+len, " /DeviceGray %c%c", 'C' | mask, 'S' | mask); |
| 254 |
for (i = 0; i < color->num_components; i++) { |
| 255 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 256 |
} |
| 257 |
len += sprintf(buffer+len, " %c%c", 'S' | mask, 'C' | mask); |
| 258 |
} |
| 259 |
break; |
| 260 |
case PDF_COLORSPACE_TYPE_DEVICERGB: |
| 261 |
{ |
| 262 |
len += sprintf(buffer+len, " /DeviceRGB %c%c", 'C' | mask, 'S' | mask); |
| 263 |
for (i = 0; i < color->num_components; i++) { |
| 264 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 265 |
} |
| 266 |
len += sprintf(buffer+len, " %c%c", 'S' | mask, 'C' | mask); |
| 267 |
} |
| 268 |
break; |
| 269 |
case PDF_COLORSPACE_TYPE_DEVICECMYK: |
| 270 |
{ |
| 271 |
len += sprintf(buffer+len, " /DeviceCMYK %c%c", 'C' | mask, 'S' | mask); |
| 272 |
for (i = 0; i < color->num_components; i++) { |
| 273 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 274 |
} |
| 275 |
len += sprintf(buffer+len, " %c%c", 'S' | mask, 'C' | mask); |
| 276 |
} |
| 277 |
break; |
| 278 |
case PDF_COLORSPACE_TYPE_GRAY: |
| 279 |
{ |
| 280 |
for (i = 0; i < color->num_components; i++) { |
| 281 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 282 |
} |
| 283 |
len += sprintf(buffer+len, " %c", 'G' | mask); |
| 284 |
} |
| 285 |
break; |
| 286 |
case PDF_COLORSPACE_TYPE_RGB: |
| 287 |
{ |
| 288 |
for (i = 0; i < color->num_components; i++) { |
| 289 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 290 |
} |
| 291 |
len += sprintf(buffer+len, " %c%c", 'R' | mask, 'G' | mask); |
| 292 |
} |
| 293 |
break; |
| 294 |
case PDF_COLORSPACE_TYPE_CMYK: |
| 295 |
{ |
| 296 |
for (i = 0; i < color->num_components; i++) { |
| 297 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 298 |
} |
| 299 |
len += sprintf(buffer+len, " %c", 'K' | mask); |
| 300 |
} |
| 301 |
break; |
| 302 |
case PDF_COLORSPACE_TYPE_SPOT: |
| 303 |
{ |
| 304 |
len = sprintf(buffer+len, |
| 305 |
" /%s %c%c %g %c%c", |
| 306 |
color->spot_color_name, |
| 307 |
'C' | mask, 'S' | mask, |
| 308 |
ROUND(color->values[0], 0.001), |
| 309 |
'S' | mask, 'C' | mask); |
| 310 |
} |
| 311 |
break; |
| 312 |
case PDF_COLORSPACE_TYPE_CALGRAY: |
| 313 |
case PDF_COLORSPACE_TYPE_CALRGB: |
| 314 |
case PDF_COLORSPACE_TYPE_LAB: |
| 315 |
case PDF_COLORSPACE_TYPE_INDEXED: |
| 316 |
{ |
| 317 |
char res_name[16]; |
| 318 |
|
| 319 |
snprintf(res_name, 16, "XC%d", color->res_id & 0xffff); /* TODO: Upper 16bits for category ID. See, pdfresource.c */ |
| 320 |
res_name[15] = 0; |
| 321 |
len += sprintf(buffer+len, " /%s %c%c", res_name, 'C' | mask, 'S' | mask); |
| 322 |
for (i = 0; i < color->num_components; i++) { |
| 323 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 324 |
} |
| 325 |
len += sprintf(buffer+len, " %c%c", 'S' | mask, 'C' | mask); |
| 326 |
pdf_doc_add_page_resource("ColorSpace", res_name, pdf_get_resource_reference(color->res_id)); |
| 327 |
} |
| 328 |
break; |
| 329 |
case PDF_COLORSPACE_TYPE_PATTERN: |
| 330 |
{ |
| 331 |
char res_name[16]; |
| 332 |
|
| 333 |
if (color->res_id < 0) { |
| 334 |
len += sprintf(buffer+len, " /Pattern %c%c", 'C' | mask, 'S' | mask); |
| 335 |
/* no color value but just a name */ |
| 336 |
} else { |
| 337 |
snprintf(res_name, 16, "XC%d", color->res_id & 0xffff); /* TODO: Upper 16bits for category ID. See, pdfresource.c */ |
| 338 |
res_name[15] = 0; |
| 339 |
len += sprintf(buffer+len, " /%s %c%c", res_name, 'C' | mask, 'S' | mask); |
| 340 |
for (i = 0; i < color->num_components; i++) { |
| 341 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 342 |
} |
| 343 |
pdf_doc_add_page_resource("ColorSpace", res_name, pdf_get_resource_reference(color->res_id)); |
| 344 |
} |
| 345 |
snprintf(res_name, 16, "XP%d", color->pattern_id & 0xffff); /* TODO: see below */ |
| 346 |
res_name[15] = 0; |
| 347 |
len += sprintf(buffer+len, " /%s %c%c%c", res_name, 'S' | mask, 'C' | mask, 'N' | mask); |
| 348 |
|
| 349 |
pdf_doc_add_page_resource("Pattern", res_name, pdf_get_resource_reference(color->pattern_id)); |
| 350 |
} |
| 351 |
break; |
| 352 |
default: |
| 353 |
{ |
| 354 |
char res_name[16]; |
| 355 |
|
| 356 |
snprintf(res_name, 8, "XC%d", color->res_id & 0xffff); /* TODO: Upper 16bits for category ID. See, pdfresource.c */ |
| 357 |
res_name[8] = 0; |
| 358 |
len += sprintf(buffer+len, " /%s %c%c", res_name, 'C' | mask, 'S' | mask); |
| 359 |
for (i = 0; i < color->num_components; i++) { |
| 360 |
len += sprintf(buffer+len, " %g", ROUND(color->values[i], 0.001)); |
| 361 |
} |
| 362 |
len += sprintf(buffer+len, " %c%c%c", 'S' | mask, 'C' | mask, 'N' | mask); |
| 363 |
pdf_doc_add_page_resource("ColorSpace", res_name, pdf_get_resource_reference(color->res_id)); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
return len; |
| 368 |
} |
| 369 |
|
| 370 |
pdf_color current_fill = { |
| 371 |
-1, |
| 372 |
PDF_COLORSPACE_TYPE_GRAY, |
| 373 |
1, |
| 374 |
NULL, |
| 375 |
{0.0}, |
| 376 |
-1 |
| 377 |
}; |
| 378 |
|
| 379 |
pdf_color current_stroke = { |
| 380 |
-1, |
| 381 |
PDF_COLORSPACE_TYPE_GRAY, |
| 382 |
1, |
| 383 |
NULL, |
| 384 |
{0.0}, |
| 385 |
-1 |
| 386 |
}; |
| 387 |
|
| 388 |
/* |
| 389 |
* This routine is not a real color matching. |
| 390 |
* TODO: This can be removed too? |
| 391 |
* Maybe we are currently always forcing installing color. |
| 392 |
*/ |
| 393 |
int |
| 394 |
pdf_color_compare (const pdf_color *color1, const pdf_color *color2) |
| 395 |
{ |
| 396 |
int n; |
| 397 |
|
| 398 |
if (color1->type != PDF_COLORSPACE_TYPE_GRAY || |
| 399 |
color1->type != PDF_COLORSPACE_TYPE_RGB || |
| 400 |
color1->type != PDF_COLORSPACE_TYPE_CMYK || |
| 401 |
color1->type != PDF_COLORSPACE_TYPE_SPOT || |
| 402 |
color2->type != PDF_COLORSPACE_TYPE_GRAY || |
| 403 |
color2->type != PDF_COLORSPACE_TYPE_RGB || |
| 404 |
color2->type != PDF_COLORSPACE_TYPE_CMYK || |
| 405 |
color2->type != PDF_COLORSPACE_TYPE_SPOT || |
| 406 |
color1->type != color2->type) { |
| 407 |
return -1; |
| 408 |
} |
| 409 |
|
| 410 |
n = color1->num_components; |
| 411 |
while (--n >= 0) { |
| 412 |
if (color1->values[n] != color2->values[n]) |
| 413 |
return -1; |
| 414 |
} |
| 415 |
if (color1->spot_color_name && color2->spot_color_name) |
| 416 |
return strcmp(color1->spot_color_name, color2->spot_color_name); |
| 417 |
|
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
/* Dvipdfm special */ |
| 422 |
pdf_color default_color = { |
| 423 |
-1, |
| 424 |
PDF_COLORSPACE_TYPE_GRAY, |
| 425 |
1, |
| 426 |
NULL, |
| 427 |
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
| 428 |
-1 |
| 429 |
}; |
| 430 |
|
| 431 |
#define DEV_COLOR_STACK_MAX 128 |
| 432 |
|
| 433 |
static struct { |
| 434 |
int current; |
| 435 |
pdf_color stroke[DEV_COLOR_STACK_MAX]; |
| 436 |
pdf_color fill[DEV_COLOR_STACK_MAX]; |
| 437 |
} color_stack = { |
| 438 |
0, |
| 439 |
}; |
| 440 |
|
| 441 |
void |
| 442 |
pdf_color_clear_stack (void) |
| 443 |
{ |
| 444 |
if (color_stack.current > 0) { |
| 445 |
WARN("You've mistakenly made a global color change within nested colors."); |
| 446 |
} |
| 447 |
while (color_stack.current--) { |
| 448 |
if (color_stack.stroke[color_stack.current].spot_color_name) |
| 449 |
RELEASE(color_stack.stroke[color_stack.current].spot_color_name); |
| 450 |
if (color_stack.fill[color_stack.current].spot_color_name) |
| 451 |
RELEASE(color_stack.fill[color_stack.current].spot_color_name); |
| 452 |
} |
| 453 |
color_stack.current = 0; |
| 454 |
pdf_color_black(color_stack.stroke); |
| 455 |
pdf_color_black(color_stack.fill); |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
void |
| 460 |
pdf_color_set (pdf_color *sc, pdf_color *fc) |
| 461 |
{ |
| 462 |
pdf_color_copycolor(&color_stack.stroke[color_stack.current], sc); |
| 463 |
pdf_color_copycolor(&color_stack.fill[color_stack.current], fc); |
| 464 |
pdf_dev_reset_color(1); |
| 465 |
} |
| 466 |
|
| 467 |
void |
| 468 |
pdf_color_push (pdf_color *sc, pdf_color *fc) |
| 469 |
{ |
| 470 |
if (color_stack.current >= DEV_COLOR_STACK_MAX-1) { |
| 471 |
WARN("Color stack overflow. Just ignore."); |
| 472 |
} else { |
| 473 |
color_stack.current++; |
| 474 |
pdf_color_set(sc, fc); |
| 475 |
} |
| 476 |
return; |
| 477 |
} |
| 478 |
|
| 479 |
void |
| 480 |
pdf_color_pop (void) |
| 481 |
{ |
| 482 |
if (color_stack.current <= 0) { |
| 483 |
WARN("Color stack underflow. Just ignore."); |
| 484 |
} else { |
| 485 |
color_stack.current--; |
| 486 |
pdf_dev_reset_color(1); |
| 487 |
} |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
void |
| 492 |
pdf_color_get_current (pdf_color **sc, pdf_color **fc) |
| 493 |
{ |
| 494 |
*sc = &color_stack.stroke[color_stack.current]; |
| 495 |
*fc = &color_stack.fill[color_stack.current]; |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
#if 0 |
| 500 |
/* BUG (20060330): color change does not effect on the next page. |
| 501 |
* The problem is due to the part of grestore because it restores |
| 502 |
* the color values in the state of gsave which are not correct |
| 503 |
* if the color values are changed inside of a page. |
| 504 |
*/ |
| 505 |
void |
| 506 |
pdf_dev_preserve_color (void) |
| 507 |
{ |
| 508 |
if (color_stack.current > 0) { |
| 509 |
current_stroke = color_stack.stroke[color_stack.current]; |
| 510 |
current_fill = color_stack.fill[color_stack.current]; |
| 511 |
} |
| 512 |
} |
| 513 |
#endif |
| 514 |
|
| 515 |
/***************************** COLOR SPACE *****************************/ |
| 516 |
/* Currently only for ICCBased color loaded while reading images */ |
| 517 |
static int pdf_colorspace_defineresource (const char *ident, int subtype, void *cdata, pdf_obj *resource); |
| 518 |
static int pdf_colorspace_findresource (const char *ident, int subtype, const void *cdata); |
| 519 |
|
| 520 |
static unsigned char nullbytes16[16] = { |
| 521 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 522 |
}; |
| 523 |
|
| 524 |
static struct |
| 525 |
{ |
| 526 |
int major; |
| 527 |
int minor; |
| 528 |
} icc_versions[] = { |
| 529 |
{0, 0}, /* PDF-1.0, we don't support them */ |
| 530 |
{0, 0}, /* PDF-1.1, we don't support them */ |
| 531 |
{0, 0}, /* PDF-1.2, we don't support them */ |
| 532 |
{0x02, 0x10}, /* PDF-1.3 */ |
| 533 |
{0x02, 0x20}, /* PDF-1.4 */ |
| 534 |
{0x04, 0x00}, /* PDF-1.5 */ |
| 535 |
{0x04, 0x00}, /* PDF-1.6 */ |
| 536 |
{0x04, 0x20}, /* PDF-1.7 */ |
| 537 |
{0x04, 0x20}, /* Dummy(1.8)*/ |
| 538 |
{0x04, 0x20}, /* Dummy(1.9) */ |
| 539 |
{0x04, 0x20} /* PDF-2.0 */ |
| 540 |
}; |
| 541 |
|
| 542 |
static int |
| 543 |
iccp_version_supported (int major, int minor) |
| 544 |
{ |
| 545 |
int idx; |
| 546 |
|
| 547 |
idx = pdf_get_version() - 10; |
| 548 |
if (idx < 11) { |
| 549 |
if (icc_versions[idx].major < major) |
| 550 |
return 0; |
| 551 |
else if (icc_versions[idx].major == major && |
| 552 |
icc_versions[idx].minor < minor) |
| 553 |
return 0; |
| 554 |
else { |
| 555 |
return 1; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
typedef uint32_t iccSig; |
| 563 |
static iccSig |
| 564 |
str2iccSig (const void *s) |
| 565 |
{ |
| 566 |
const char *p; |
| 567 |
|
| 568 |
p = (const char *) s; |
| 569 |
|
| 570 |
return (iccSig) ((p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3]); |
| 571 |
} |
| 572 |
|
| 573 |
typedef struct |
| 574 |
{ |
| 575 |
int32_t X, Y, Z; /* s15Fixed16Number */ |
| 576 |
} iccXYZNumber; |
| 577 |
|
| 578 |
typedef struct |
| 579 |
{ |
| 580 |
int size; |
| 581 |
iccSig CMMType; |
| 582 |
int32_t version; |
| 583 |
iccSig devClass; |
| 584 |
iccSig colorSpace; |
| 585 |
iccSig PCS; /* Profile Connection Space */ |
| 586 |
char creationDate[12]; |
| 587 |
iccSig acsp; |
| 588 |
iccSig platform; |
| 589 |
char flags[4]; |
| 590 |
iccSig devMnfct; |
| 591 |
iccSig devModel; |
| 592 |
char devAttr[8]; |
| 593 |
int32_t intent; |
| 594 |
iccXYZNumber illuminant; |
| 595 |
iccSig creator; |
| 596 |
unsigned char ID[16]; /* MD5 checksum with Rendering intent, |
| 597 |
* Header attrs, Profile ID fields are |
| 598 |
* set to zeros. |
| 599 |
*/ |
| 600 |
/* 28 bytes reserved - must be set to zeros */ |
| 601 |
} iccHeader; |
| 602 |
|
| 603 |
#define iccNullSig 0 |
| 604 |
static void |
| 605 |
iccp_init_iccHeader (iccHeader *icch) |
| 606 |
{ |
| 607 |
ASSERT(icch); |
| 608 |
|
| 609 |
icch->size = 0; |
| 610 |
icch->CMMType = iccNullSig; |
| 611 |
icch->version = 0xFFFFFF; |
| 612 |
icch->devClass = iccNullSig; |
| 613 |
icch->colorSpace = iccNullSig; |
| 614 |
icch->PCS = iccNullSig; |
| 615 |
memset(icch->creationDate, 0, 12); |
| 616 |
icch->acsp = str2iccSig("ascp"); |
| 617 |
icch->platform = iccNullSig; |
| 618 |
memset(icch->flags, 0, 4); |
| 619 |
icch->devMnfct = iccNullSig; |
| 620 |
icch->devModel = iccNullSig; |
| 621 |
memset(icch->devAttr, 0, 8); |
| 622 |
icch->intent = 0; |
| 623 |
icch->illuminant.X = 0; |
| 624 |
icch->illuminant.Y = 0; |
| 625 |
icch->illuminant.Z = 0; |
| 626 |
icch->creator = iccNullSig; |
| 627 |
memset(icch->ID, 0, 16); |
| 628 |
} |
| 629 |
|
| 630 |
#define ICC_INTENT_TYPE(n) ((int) (((n) >> 16) & 0xff)) |
| 631 |
#define ICC_INTENT_PERCEPTUAL 0 |
| 632 |
#define ICC_INTENT_RELATIVE 1 |
| 633 |
#define ICC_INTENT_SATURATION 2 |
| 634 |
#define ICC_INTENT_ABSOLUTE 3 |
| 635 |
|
| 636 |
/* |
| 637 |
* In ICC profile stream dicrionary, there is /Range whose values must |
| 638 |
* "match the information in the profile". But where is those values in? |
| 639 |
* |
| 640 |
* How should I treat rendering intent? |
| 641 |
*/ |
| 642 |
struct iccbased_cdata |
| 643 |
{ |
| 644 |
int32_t sig; /* 'i' 'c' 'c' 'b' */ |
| 645 |
|
| 646 |
unsigned char checksum[16]; /* 16 bytes MD5 Checksum */ |
| 647 |
int colorspace; /* input colorspace: |
| 648 |
* RGB, Gray, CMYK, (Lab?) |
| 649 |
*/ |
| 650 |
int alternate; /* alternate colorspace (id), unused */ |
| 651 |
}; |
| 652 |
|
| 653 |
#define check_sig(d,p,q,r,s) ((d) && (d)->sig == ((p)<<24|(q)<<16|(r)<<8|(s))) |
| 654 |
|
| 655 |
static void |
| 656 |
init_iccbased_cdata (struct iccbased_cdata *cdata) |
| 657 |
{ |
| 658 |
ASSERT(cdata); |
| 659 |
|
| 660 |
cdata->sig = ('i' << 24|'c' << 16|'c' << 8|'b'); |
| 661 |
memset(cdata->checksum, 0, 16); |
| 662 |
cdata->colorspace = PDF_COLORSPACE_TYPE_INVALID; |
| 663 |
cdata->alternate = -1; |
| 664 |
|
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
static void |
| 669 |
release_iccbased_cdata (struct iccbased_cdata *cdata) |
| 670 |
{ |
| 671 |
ASSERT(check_sig(cdata, 'i', 'c', 'c', 'b')); |
| 672 |
|
| 673 |
RELEASE(cdata); |
| 674 |
} |
| 675 |
|
| 676 |
static int |
| 677 |
get_num_components_iccbased (const struct iccbased_cdata *cdata) |
| 678 |
{ |
| 679 |
int num_components = 0; |
| 680 |
|
| 681 |
ASSERT(check_sig(cdata, 'i', 'c', 'c', 'b')); |
| 682 |
|
| 683 |
switch (cdata->colorspace) { |
| 684 |
case PDF_COLORSPACE_TYPE_RGB: |
| 685 |
num_components = 3; |
| 686 |
break; |
| 687 |
case PDF_COLORSPACE_TYPE_CMYK: |
| 688 |
num_components = 4; |
| 689 |
break; |
| 690 |
case PDF_COLORSPACE_TYPE_GRAY: |
| 691 |
num_components = 1; |
| 692 |
break; |
| 693 |
case PDF_COLORSPACE_TYPE_LAB: |
| 694 |
num_components = 3; |
| 695 |
break; |
| 696 |
} |
| 697 |
|
| 698 |
return num_components; |
| 699 |
} |
| 700 |
|
| 701 |
static int |
| 702 |
compare_iccbased (const char *ident1, const struct iccbased_cdata *cdata1, |
| 703 |
const char *ident2, const struct iccbased_cdata *cdata2) |
| 704 |
{ |
| 705 |
if (cdata1 && cdata2) { |
| 706 |
|
| 707 |
ASSERT(check_sig(cdata1, 'i', 'c', 'c', 'b')); |
| 708 |
ASSERT(check_sig(cdata2, 'i', 'c', 'c', 'b')); |
| 709 |
|
| 710 |
if (memcmp(cdata1->checksum, nullbytes16, 16) && |
| 711 |
memcmp(cdata2->checksum, nullbytes16, 16)) { |
| 712 |
return memcmp(cdata1->checksum, cdata2->checksum, 16); |
| 713 |
} |
| 714 |
if (cdata1->colorspace != cdata2->colorspace) { |
| 715 |
return (cdata1->colorspace - cdata2->colorspace); |
| 716 |
} |
| 717 |
|
| 718 |
/* Continue if checksum unknown and colorspace is same. */ |
| 719 |
} |
| 720 |
|
| 721 |
if (ident1 && ident2) |
| 722 |
return strcmp(ident1, ident2); |
| 723 |
|
| 724 |
/* No way to compare */ |
| 725 |
return -1; |
| 726 |
} |
| 727 |
|
| 728 |
int |
| 729 |
iccp_check_colorspace (int colortype, const void *profile, int proflen) |
| 730 |
{ |
| 731 |
iccSig colorspace; |
| 732 |
const unsigned char *p; |
| 733 |
|
| 734 |
if (!profile || proflen < 128) |
| 735 |
return -1; |
| 736 |
|
| 737 |
p = (const unsigned char *) profile; |
| 738 |
|
| 739 |
colorspace = str2iccSig(p + 16); |
| 740 |
|
| 741 |
switch (colortype) { |
| 742 |
case PDF_COLORSPACE_TYPE_CALRGB: |
| 743 |
case PDF_COLORSPACE_TYPE_RGB: |
| 744 |
if (colorspace != str2iccSig("RGB ")) { |
| 745 |
return -1; |
| 746 |
} |
| 747 |
break; |
| 748 |
case PDF_COLORSPACE_TYPE_CALGRAY: |
| 749 |
case PDF_COLORSPACE_TYPE_GRAY: |
| 750 |
if (colorspace != str2iccSig("GRAY")) { |
| 751 |
return -1; |
| 752 |
} |
| 753 |
break; |
| 754 |
case PDF_COLORSPACE_TYPE_CMYK: |
| 755 |
if (colorspace != str2iccSig("CMYK")) { |
| 756 |
return -1; |
| 757 |
} |
| 758 |
break; |
| 759 |
default: |
| 760 |
return -1; |
| 761 |
} |
| 762 |
|
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
pdf_obj * |
| 767 |
iccp_get_rendering_intent (const void *profile, int proflen) |
| 768 |
{ |
| 769 |
pdf_obj *ri = NULL; |
| 770 |
const unsigned char *p; |
| 771 |
int32_t intent; |
| 772 |
|
| 773 |
if (!profile || proflen < 128) |
| 774 |
return NULL; |
| 775 |
|
| 776 |
p = (const unsigned char *) profile; |
| 777 |
|
| 778 |
intent = (p[64] << 24)|(p[65] << 16)|(p[66] << 8)|p[67]; |
| 779 |
switch (ICC_INTENT_TYPE(intent)) { |
| 780 |
case ICC_INTENT_SATURATION: |
| 781 |
ri = pdf_new_name("Saturation"); |
| 782 |
break; |
| 783 |
case ICC_INTENT_PERCEPTUAL: |
| 784 |
ri = pdf_new_name("Perceptual"); |
| 785 |
break; |
| 786 |
case ICC_INTENT_ABSOLUTE: |
| 787 |
ri = pdf_new_name("AbsoluteColorimetric"); |
| 788 |
break; |
| 789 |
case ICC_INTENT_RELATIVE: |
| 790 |
ri = pdf_new_name("RelativeColorimetric"); |
| 791 |
break; |
| 792 |
default: |
| 793 |
WARN("Invalid rendering intent type: %d", ICC_INTENT_TYPE(intent)); |
| 794 |
ri = NULL; |
| 795 |
} |
| 796 |
|
| 797 |
return ri; |
| 798 |
} |
| 799 |
|
| 800 |
#define sget_signed_long(p) ((int32_t) ((p)[0] << 24|(p)[1] << 16|(p)[2] << 8|(p)[3])) |
| 801 |
#define sget_signed_short(p) ((short) ((p)[0] << 8|(p)[1])) |
| 802 |
#define get_iccSig(p) ((iccSig) ((p)[0] << 24|(p)[1] << 16|(p)[2] << 8|(p)[3])) |
| 803 |
|
| 804 |
static int |
| 805 |
iccp_unpack_header (iccHeader *icch, |
| 806 |
const void *profile, int proflen, int check_size) |
| 807 |
{ |
| 808 |
const unsigned char *p, *endptr; |
| 809 |
|
| 810 |
if (check_size) { |
| 811 |
if (!profile || proflen < 128 || |
| 812 |
proflen % 4 != 0) { |
| 813 |
WARN("Profile size: %ld", proflen); |
| 814 |
return -1; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
p = (const unsigned char *) profile; |
| 819 |
endptr = p + 128; |
| 820 |
|
| 821 |
icch->size = sget_signed_long(p); |
| 822 |
if (check_size) { |
| 823 |
if (icch->size != proflen) { |
| 824 |
WARN("ICC Profile size: %ld(header) != %ld", icch->size, proflen); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
} |
| 828 |
p += 4; |
| 829 |
|
| 830 |
icch->CMMType = str2iccSig(p); |
| 831 |
p += 4; |
| 832 |
icch->version = sget_signed_long(p); |
| 833 |
p += 4; |
| 834 |
icch->devClass = str2iccSig(p); |
| 835 |
p += 4; |
| 836 |
icch->colorSpace = str2iccSig(p); |
| 837 |
p += 4; |
| 838 |
icch->PCS = str2iccSig(p); |
| 839 |
p += 4; |
| 840 |
memcpy(icch->creationDate, p, 12); |
| 841 |
p += 12; |
| 842 |
icch->acsp = str2iccSig(p); /* acsp */ |
| 843 |
if (icch->acsp != str2iccSig("acsp")) { |
| 844 |
WARN("Invalid ICC profile: not \"acsp\" - %c%c%c%c ", |
| 845 |
p[0], p[1], p[2], p[3]); |
| 846 |
return -1; |
| 847 |
} |
| 848 |
p += 4; |
| 849 |
icch->platform = str2iccSig(p); |
| 850 |
p += 4; |
| 851 |
memcpy(icch->flags, p, 4); |
| 852 |
p += 4; |
| 853 |
icch->devMnfct = str2iccSig(p); |
| 854 |
p += 4; |
| 855 |
icch->devModel = str2iccSig(p); |
| 856 |
p += 4; |
| 857 |
memcpy(icch->devAttr, p, 8); |
| 858 |
p += 8; |
| 859 |
icch->intent = (p[0] << 24)|(p[1] << 16)|(p[2] << 8)|p[3]; |
| 860 |
p += 4; |
| 861 |
icch->illuminant.X = sget_signed_long(p); |
| 862 |
p += 4; |
| 863 |
icch->illuminant.Y = sget_signed_long(p); |
| 864 |
p += 4; |
| 865 |
icch->illuminant.Z = sget_signed_long(p); |
| 866 |
p += 4; |
| 867 |
icch->creator = str2iccSig(p); |
| 868 |
p += 4; |
| 869 |
memcpy(icch->ID, p, 16); |
| 870 |
p += 16; |
| 871 |
|
| 872 |
/* 28 bytes reserved - must be set to zeros */ |
| 873 |
for (; p < endptr; p++) { |
| 874 |
if (*p != '\0') { |
| 875 |
WARN("Reserved pad not zero: %02x (at offset %d in ICC profile header.)", |
| 876 |
*p, 128 - ((int) (endptr - p))); |
| 877 |
return -1; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
/* MD5 checksum with Rendering intent, |
| 885 |
* Header attrs, Profile ID fields are |
| 886 |
* set to zeros. |
| 887 |
*/ |
| 888 |
#define ICC_HEAD_SECT1_START 0 |
| 889 |
#define ICC_HEAD_SECT1_LENGTH 56 |
| 890 |
/* 8 bytes devAttr, 4 bytes intent */ |
| 891 |
#define ICC_HEAD_SECT2_START 68 |
| 892 |
#define ICC_HEAD_SECT2_LENGTH 16 |
| 893 |
/* 16 bytes ID (checksum) */ |
| 894 |
#define ICC_HEAD_SECT3_START 100 |
| 895 |
#define ICC_HEAD_SECT3_LENGTH 28 |
| 896 |
|
| 897 |
#include "dpxcrypt.h" |
| 898 |
static void |
| 899 |
iccp_get_checksum (unsigned char *checksum, const void *profile, int proflen) |
| 900 |
{ |
| 901 |
const unsigned char *p; |
| 902 |
MD5_CONTEXT md5; |
| 903 |
|
| 904 |
p = (const unsigned char *) profile; |
| 905 |
|
| 906 |
MD5_init (&md5); |
| 907 |
MD5_write(&md5, p + ICC_HEAD_SECT1_START, ICC_HEAD_SECT1_LENGTH); |
| 908 |
MD5_write(&md5, nullbytes16, 12); |
| 909 |
MD5_write(&md5, p + ICC_HEAD_SECT2_START, ICC_HEAD_SECT2_LENGTH); |
| 910 |
MD5_write(&md5, nullbytes16, 16); |
| 911 |
MD5_write(&md5, p + ICC_HEAD_SECT3_START, ICC_HEAD_SECT3_LENGTH); |
| 912 |
|
| 913 |
/* body */ |
| 914 |
MD5_write(&md5, p + 128, proflen - 128); |
| 915 |
|
| 916 |
MD5_final(checksum, &md5); |
| 917 |
} |
| 918 |
|
| 919 |
static void |
| 920 |
print_iccp_header (iccHeader *icch, unsigned char *checksum) |
| 921 |
{ |
| 922 |
int i; |
| 923 |
|
| 924 |
ASSERT(icch); |
| 925 |
|
| 926 |
#define print_iccSig(s,t) if ((s) == 0) {\ |
| 927 |
MESG("pdf_color>> %s:\t(null)\n", (t)); \ |
| 928 |
} else if (!isprint(((s) >> 24) & 0xff) || \ |
| 929 |
!isprint(((s) >> 16) & 0xff) || \ |
| 930 |
!isprint(((s) >> 8) & 0xff) || \ |
| 931 |
!isprint((s) & 0xff)) { \ |
| 932 |
MESG("pdf_color>> %s:\t(invalid)\n", (t)); \ |
| 933 |
} else { \ |
| 934 |
MESG("pdf_color>> %s:\t%c%c%c%c\n", (t), \ |
| 935 |
((s) >> 24) & 0xff, ((s) >> 16) & 0xff, \ |
| 936 |
((s) >> 8) & 0xff, (s) & 0xff); \ |
| 937 |
} |
| 938 |
|
| 939 |
MESG("\n"); |
| 940 |
MESG("pdf_color>> ICC Profile Info\n"); |
| 941 |
MESG("pdf_color>> Profile Size:\t%ld bytes\n", icch->size); |
| 942 |
print_iccSig(icch->CMMType, "CMM Type"); |
| 943 |
MESG("pdf_color>> Profile Version:\t%d.%01d.%01d\n", |
| 944 |
(icch->version >> 24) & 0xff, |
| 945 |
(icch->version >> 20) & 0x0f, |
| 946 |
(icch->version >> 16) & 0x0f); |
| 947 |
print_iccSig(icch->devClass, "Device Class"); |
| 948 |
print_iccSig(icch->colorSpace, "Color Space"); |
| 949 |
print_iccSig(icch->PCS, "Connection Space"); |
| 950 |
MESG("pdf_color>> Creation Date:\t"); |
| 951 |
for (i = 0; i < 12; i += 2) { |
| 952 |
if (i == 0) |
| 953 |
MESG("%04u", |
| 954 |
sget_unsigned_pair((unsigned char *) icch->creationDate)); |
| 955 |
else { |
| 956 |
MESG(":%02u", |
| 957 |
sget_unsigned_pair((unsigned char *) (&icch->creationDate[i]))); |
| 958 |
} |
| 959 |
} |
| 960 |
MESG("\n"); |
| 961 |
print_iccSig(icch->platform, "Primary Platform"); |
| 962 |
MESG("pdf_color>> Profile Flags:\t%02x:%02x:%02x:%02x\n", |
| 963 |
icch->flags[0], icch->flags[1], icch->flags[2], icch->flags[3]); |
| 964 |
print_iccSig(icch->devMnfct, "Device Mnfct"); |
| 965 |
print_iccSig(icch->devModel, "Device Model"); |
| 966 |
MESG("pdf_color>> Device Attr:\t"); |
| 967 |
for (i = 0; i < 8; i++) { |
| 968 |
if (i == 0) |
| 969 |
MESG("%02x", icch->devAttr[i]); |
| 970 |
else |
| 971 |
MESG(":%02x", icch->devAttr[i]); |
| 972 |
} |
| 973 |
MESG("\n"); |
| 974 |
MESG("pdf_color>> Rendering Intent:\t"); |
| 975 |
switch (ICC_INTENT_TYPE(icch->intent)) { |
| 976 |
case ICC_INTENT_SATURATION: |
| 977 |
MESG("Saturation"); |
| 978 |
break; |
| 979 |
case ICC_INTENT_PERCEPTUAL: |
| 980 |
MESG("Perceptual"); |
| 981 |
break; |
| 982 |
case ICC_INTENT_ABSOLUTE: |
| 983 |
MESG("Absolute Colorimetric"); |
| 984 |
break; |
| 985 |
case ICC_INTENT_RELATIVE: |
| 986 |
MESG("Relative Colorimetric"); |
| 987 |
break; |
| 988 |
default: |
| 989 |
MESG("(invalid)"); |
| 990 |
break; |
| 991 |
} |
| 992 |
MESG("\n"); |
| 993 |
print_iccSig(icch->creator, "Creator"); |
| 994 |
MESG("pdf_color>> Illuminant (XYZ):\t"); |
| 995 |
MESG("%.3f %.3f %.3f\n", |
| 996 |
(double) icch->illuminant.X / 0x10000, |
| 997 |
(double) icch->illuminant.Y / 0x10000, |
| 998 |
(double) icch->illuminant.Z / 0x10000); |
| 999 |
MESG("pdf_color>> Checksum:\t"); |
| 1000 |
if (!memcmp(icch->ID, nullbytes16, 16)) { |
| 1001 |
MESG("(null)"); |
| 1002 |
} else { |
| 1003 |
for (i = 0; i < 16; i++) { |
| 1004 |
if (i == 0) |
| 1005 |
MESG("%02x", icch->ID[i]); |
| 1006 |
else |
| 1007 |
MESG(":%02x", icch->ID[i]); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
MESG("\n"); |
| 1011 |
if (checksum) { |
| 1012 |
MESG("pdf_color>> Calculated:\t"); |
| 1013 |
for (i = 0; i < 16; i++) { |
| 1014 |
if (i == 0) |
| 1015 |
MESG("%02x", checksum[i]); |
| 1016 |
else |
| 1017 |
MESG(":%02x", checksum[i]); |
| 1018 |
} |
| 1019 |
MESG("\n"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
return; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
static int |
| 1027 |
iccp_devClass_allowed (int dev_class) |
| 1028 |
{ |
| 1029 |
int colormode; |
| 1030 |
|
| 1031 |
colormode = pdf_dev_get_param(PDF_DEV_PARAM_COLORMODE); |
| 1032 |
|
| 1033 |
switch (colormode) { |
| 1034 |
#if 0 |
| 1035 |
case PDF_DEV_COLORMODE_PDFX1: |
| 1036 |
break; |
| 1037 |
case PDF_DEV_COLORMODE_PDFX3: |
| 1038 |
if (dev_class != str2iccSig("prtr")) { |
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
break; |
| 1042 |
#endif |
| 1043 |
default: |
| 1044 |
if (dev_class != str2iccSig("scnr") && |
| 1045 |
dev_class != str2iccSig("mntr") && |
| 1046 |
dev_class != str2iccSig("prtr") && |
| 1047 |
dev_class != str2iccSig("spac")) { |
| 1048 |
return 0; |
| 1049 |
} |
| 1050 |
break; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
return 1; |
| 1055 |
} |
| 1056 |
|
| 1057 |
int |
| 1058 |
iccp_load_profile (const char *ident, |
| 1059 |
const void *profile, int proflen) |
| 1060 |
{ |
| 1061 |
int cspc_id; |
| 1062 |
pdf_obj *resource; |
| 1063 |
pdf_obj *stream; |
| 1064 |
pdf_obj *stream_dict; |
| 1065 |
iccHeader icch; |
| 1066 |
int colorspace; |
| 1067 |
unsigned char checksum[16]; |
| 1068 |
struct iccbased_cdata *cdata; |
| 1069 |
|
| 1070 |
iccp_init_iccHeader(&icch); |
| 1071 |
if (iccp_unpack_header(&icch, profile, proflen, 1) < 0) { /* check size */ |
| 1072 |
WARN("Invalid ICC profile header in \"%s\"", ident); |
| 1073 |
print_iccp_header(&icch, NULL); |
| 1074 |
return -1; |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (!iccp_version_supported((icch.version >> 24) & 0xff, |
| 1078 |
(icch.version >> 16) & 0xff)) { |
| 1079 |
WARN("ICC profile format spec. version %d.%01d.%01d" |
| 1080 |
" not supported in current PDF version setting.", |
| 1081 |
(icch.version >> 24) & 0xff, |
| 1082 |
(icch.version >> 20) & 0x0f, |
| 1083 |
(icch.version >> 16) & 0x0f); |
| 1084 |
WARN("ICC profile not embedded."); |
| 1085 |
print_iccp_header(&icch, NULL); |
| 1086 |
return -1; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (!iccp_devClass_allowed(icch.devClass)) { |
| 1090 |
WARN("Unsupported ICC Profile Device Class:"); |
| 1091 |
print_iccp_header(&icch, NULL); |
| 1092 |
return -1; |
| 1093 |
} |
| 1094 |
|
| 1095 |
if (icch.colorSpace == str2iccSig("RGB ")) { |
| 1096 |
colorspace = PDF_COLORSPACE_TYPE_RGB; |
| 1097 |
} else if (icch.colorSpace == str2iccSig("GRAY")) { |
| 1098 |
colorspace = PDF_COLORSPACE_TYPE_GRAY; |
| 1099 |
} else if (icch.colorSpace == str2iccSig("CMYK")) { |
| 1100 |
colorspace = PDF_COLORSPACE_TYPE_CMYK; |
| 1101 |
} else { |
| 1102 |
WARN("Unsupported input color space."); |
| 1103 |
print_iccp_header(&icch, NULL); |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
|
| 1107 |
iccp_get_checksum(checksum, profile, proflen); |
| 1108 |
if (memcmp(icch.ID, nullbytes16, 16) && |
| 1109 |
memcmp(icch.ID, checksum, 16)) { |
| 1110 |
WARN("Invalid ICC profile: Inconsistent checksum."); |
| 1111 |
print_iccp_header(&icch, checksum); |
| 1112 |
return -1; |
| 1113 |
} |
| 1114 |
|
| 1115 |
cdata = NEW(1, struct iccbased_cdata); |
| 1116 |
init_iccbased_cdata(cdata); |
| 1117 |
cdata->colorspace = colorspace; |
| 1118 |
memcpy(cdata->checksum, checksum, 16); |
| 1119 |
|
| 1120 |
cspc_id = pdf_colorspace_findresource(ident, |
| 1121 |
PDF_COLORSPACE_TYPE_ICCBASED, cdata); |
| 1122 |
if (cspc_id >= 0) { |
| 1123 |
if (dpx_conf.verbose_level > 0) |
| 1124 |
MESG("(ICCP:[id=%d])", cspc_id); |
| 1125 |
release_iccbased_cdata(cdata); |
| 1126 |
return cspc_id; |
| 1127 |
} |
| 1128 |
if (dpx_conf.verbose_level > 1) { |
| 1129 |
print_iccp_header(&icch, checksum); |
| 1130 |
} |
| 1131 |
|
| 1132 |
resource = pdf_new_array(); |
| 1133 |
|
| 1134 |
stream = pdf_new_stream(STREAM_COMPRESS); |
| 1135 |
pdf_add_array(resource, pdf_new_name("ICCBased")); |
| 1136 |
pdf_add_array(resource, pdf_ref_obj (stream)); |
| 1137 |
|
| 1138 |
stream_dict = pdf_stream_dict(stream); |
| 1139 |
pdf_add_dict(stream_dict, pdf_new_name("N"), |
| 1140 |
pdf_new_number(get_num_components_iccbased(cdata))); |
| 1141 |
|
| 1142 |
pdf_add_stream (stream, profile, proflen); |
| 1143 |
pdf_release_obj(stream); |
| 1144 |
|
| 1145 |
cspc_id = pdf_colorspace_defineresource(ident, |
| 1146 |
PDF_COLORSPACE_TYPE_ICCBASED, |
| 1147 |
cdata, resource); |
| 1148 |
|
| 1149 |
return cspc_id; |
| 1150 |
} |
| 1151 |
|
| 1152 |
#if 0 |
| 1153 |
#define WBUF_SIZE 4096 |
| 1154 |
static unsigned char wbuf[WBUF_SIZE]; |
| 1155 |
|
| 1156 |
static pdf_obj * |
| 1157 |
iccp_load_file_stream (unsigned char *checksum, int length, FILE *fp) |
| 1158 |
{ |
| 1159 |
pdf_obj *stream; |
| 1160 |
MD5_CONTEXT md5; |
| 1161 |
int nb_read; |
| 1162 |
|
| 1163 |
rewind(fp); |
| 1164 |
|
| 1165 |
if (fread(wbuf, 1, 128, fp) != 128) { |
| 1166 |
return NULL; |
| 1167 |
} |
| 1168 |
length -= 128; |
| 1169 |
|
| 1170 |
stream = pdf_new_stream(STREAM_COMPRESS); |
| 1171 |
|
| 1172 |
MD5_init (&md5); |
| 1173 |
MD5_write(&md5, wbuf + ICC_HEAD_SECT1_START, ICC_HEAD_SECT1_LENGTH); |
| 1174 |
MD5_write(&md5, nullbytes16, 12); |
| 1175 |
MD5_write(&md5, wbuf + ICC_HEAD_SECT2_START, ICC_HEAD_SECT2_LENGTH); |
| 1176 |
MD5_write(&md5, nullbytes16, 16); |
| 1177 |
MD5_write(&md5, wbuf + ICC_HEAD_SECT3_START, ICC_HEAD_SECT3_LENGTH); |
| 1178 |
|
| 1179 |
pdf_add_stream(stream, wbuf, 128); |
| 1180 |
|
| 1181 |
/* body */ |
| 1182 |
while (length > 0) { |
| 1183 |
nb_read = fread(wbuf, 1, MIN(length, WBUF_SIZE), fp); |
| 1184 |
MD5_write(&md5, wbuf, nb_read); |
| 1185 |
pdf_add_stream(stream, wbuf, nb_read); |
| 1186 |
length -= nb_read; |
| 1187 |
} |
| 1188 |
|
| 1189 |
MD5_final(checksum, &md5); |
| 1190 |
|
| 1191 |
|
| 1192 |
return stream; |
| 1193 |
} |
| 1194 |
|
| 1195 |
int |
| 1196 |
pdf_colorspace_load_ICCBased (const char *ident, const char *filename) |
| 1197 |
{ |
| 1198 |
int cspc_id; |
| 1199 |
FILE *fp; |
| 1200 |
pdf_obj *resource; |
| 1201 |
pdf_obj *stream; |
| 1202 |
pdf_obj *stream_dict; |
| 1203 |
iccHeader icch; |
| 1204 |
int colorspace; |
| 1205 |
int size; |
| 1206 |
unsigned char checksum[16]; |
| 1207 |
struct iccbased_cdata *cdata; |
| 1208 |
|
| 1209 |
|
| 1210 |
fp = DPXFOPEN(filename, DPX_RES_TYPE_ICCPROFILE); |
| 1211 |
if (!fp) |
| 1212 |
return -1; |
| 1213 |
|
| 1214 |
size = file_size(fp); |
| 1215 |
if (size < 128) { |
| 1216 |
MFCLOSE(fp); |
| 1217 |
return -1; |
| 1218 |
} |
| 1219 |
if (fread(wbuf, 1, 128, fp) != 128) { |
| 1220 |
DPXFCLOSE(fp); |
| 1221 |
return -1; |
| 1222 |
} |
| 1223 |
|
| 1224 |
iccp_init_iccHeader(&icch); |
| 1225 |
if (iccp_unpack_header(&icch, wbuf, 128, 0) < 0) { |
| 1226 |
WARN("Invalid ICC profile header in \"%s\"", ident); |
| 1227 |
print_iccp_header(&icch, NULL); |
| 1228 |
DPXFCLOSE(fp); |
| 1229 |
return -1; |
| 1230 |
} |
| 1231 |
if (icch.size > size) { |
| 1232 |
WARN("File size smaller than recorded in header: %ld %ld", |
| 1233 |
icch.size, size); |
| 1234 |
DPXFCLOSE(fp); |
| 1235 |
return -1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (!iccp_version_supported((icch.version >> 24) & 0xff, |
| 1239 |
(icch.version >> 16) & 0xff)) { |
| 1240 |
WARN("ICC profile format spec. version %d.%01d.%01d" |
| 1241 |
" not supported in current PDF version setting.", |
| 1242 |
(icch.version >> 24) & 0xff, |
| 1243 |
(icch.version >> 20) & 0x0f, |
| 1244 |
(icch.version >> 16) & 0x0f); |
| 1245 |
WARN("ICC profile not embedded."); |
| 1246 |
print_iccp_header(&icch, NULL); |
| 1247 |
DPXFCLOSE(fp); |
| 1248 |
return -1; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (!iccp_devClass_allowed(icch.devClass)) { |
| 1252 |
WARN("Unsupported ICC Profile Device Class:"); |
| 1253 |
print_iccp_header(&icch, NULL); |
| 1254 |
DPXFCLOSE(fp); |
| 1255 |
return -1; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (icch.colorSpace == str2iccSig("RGB ")) { |
| 1259 |
colorspace = PDF_COLORSPACE_TYPE_RGB; |
| 1260 |
} else if (icch.colorSpace == str2iccSig("GRAY")) { |
| 1261 |
colorspace = PDF_COLORSPACE_TYPE_GRAY; |
| 1262 |
} else if (icch.colorSpace == str2iccSig("CMYK")) { |
| 1263 |
colorspace = PDF_COLORSPACE_TYPE_CMYK; |
| 1264 |
} else { |
| 1265 |
WARN("Unsupported input color space."); |
| 1266 |
print_iccp_header(&icch, NULL); |
| 1267 |
DPXFCLOSE(fp); |
| 1268 |
return -1; |
| 1269 |
} |
| 1270 |
|
| 1271 |
stream = iccp_load_file_stream(checksum, icch.size, fp); |
| 1272 |
DPXFCLOSE(fp); |
| 1273 |
|
| 1274 |
if (!stream) { |
| 1275 |
WARN("Loading ICC Profile failed...: %s", filename); |
| 1276 |
return -1; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (memcmp(icch.ID, nullbytes16, 16) && |
| 1280 |
memcmp(icch.ID, checksum, 16)) { |
| 1281 |
WARN("Invalid ICC profile: Inconsistent checksum."); |
| 1282 |
print_iccp_header(&icch, NULL); |
| 1283 |
pdf_release_obj(stream); |
| 1284 |
return -1; |
| 1285 |
} |
| 1286 |
|
| 1287 |
cdata = NEW(1, struct iccbased_cdata); |
| 1288 |
init_iccbased_cdata(cdata); |
| 1289 |
cdata->colorspace = colorspace; |
| 1290 |
memcpy(cdata->checksum, checksum, 16); |
| 1291 |
|
| 1292 |
cspc_id = pdf_colorspace_findresource(ident, |
| 1293 |
PDF_COLORSPACE_TYPE_ICCBASED, cdata); |
| 1294 |
if (cspc_id >= 0) { |
| 1295 |
if (dpx_conf.verbose_level > 0) |
| 1296 |
MESG("(ICCP:[id=%d])", cspc_id); |
| 1297 |
release_iccbased_cdata(cdata); |
| 1298 |
pdf_release_obj(stream); |
| 1299 |
return cspc_id; |
| 1300 |
} |
| 1301 |
if (dpx_conf.verbose_level > 1) { |
| 1302 |
print_iccp_header(&icch, checksum); |
| 1303 |
} |
| 1304 |
|
| 1305 |
resource = pdf_new_array(); |
| 1306 |
|
| 1307 |
pdf_add_array(resource, pdf_new_name("ICCBased")); |
| 1308 |
pdf_add_array(resource, pdf_ref_obj (stream)); |
| 1309 |
|
| 1310 |
stream_dict = pdf_stream_dict(stream); |
| 1311 |
pdf_add_dict(stream_dict, pdf_new_name("N"), |
| 1312 |
pdf_new_number(get_num_components_iccbased(cdata))); |
| 1313 |
pdf_release_obj(stream); |
| 1314 |
|
| 1315 |
cspc_id = pdf_colorspace_defineresource(ident, |
| 1316 |
PDF_COLORSPACE_TYPE_ICCBASED, |
| 1317 |
cdata, resource); |
| 1318 |
|
| 1319 |
return cspc_id; |
| 1320 |
} |
| 1321 |
#endif |
| 1322 |
|
| 1323 |
typedef struct { |
| 1324 |
char *ident; |
| 1325 |
int subtype; |
| 1326 |
|
| 1327 |
pdf_obj *resource; |
| 1328 |
pdf_obj *reference; |
| 1329 |
|
| 1330 |
void *cdata; |
| 1331 |
} pdf_colorspace; |
| 1332 |
|
| 1333 |
static struct { |
| 1334 |
int count; |
| 1335 |
int capacity; |
| 1336 |
pdf_colorspace *colorspaces; |
| 1337 |
} cspc_cache = { |
| 1338 |
0, 0, NULL |
| 1339 |
}; |
| 1340 |
|
| 1341 |
int |
| 1342 |
pdf_colorspace_findresource (const char *ident, |
| 1343 |
int type, const void *cdata) |
| 1344 |
{ |
| 1345 |
pdf_colorspace *colorspace; |
| 1346 |
int cspc_id, cmp = -1; |
| 1347 |
|
| 1348 |
for (cspc_id = 0; |
| 1349 |
cmp && cspc_id < cspc_cache.count; cspc_id++) { |
| 1350 |
colorspace = &cspc_cache.colorspaces[cspc_id]; |
| 1351 |
if (colorspace->subtype != type) |
| 1352 |
continue; |
| 1353 |
|
| 1354 |
switch (colorspace->subtype) { |
| 1355 |
case PDF_COLORSPACE_TYPE_ICCBASED: |
| 1356 |
cmp = compare_iccbased(ident, cdata, |
| 1357 |
colorspace->ident, colorspace->cdata); |
| 1358 |
break; |
| 1359 |
} |
| 1360 |
if (!cmp) |
| 1361 |
return cspc_id; |
| 1362 |
} |
| 1363 |
|
| 1364 |
return -1; /* not found */ |
| 1365 |
} |
| 1366 |
|
| 1367 |
static void |
| 1368 |
pdf_init_colorspace_struct (pdf_colorspace *colorspace) |
| 1369 |
{ |
| 1370 |
ASSERT(colorspace); |
| 1371 |
|
| 1372 |
colorspace->ident = NULL; |
| 1373 |
colorspace->subtype = PDF_COLORSPACE_TYPE_INVALID; |
| 1374 |
|
| 1375 |
colorspace->resource = NULL; |
| 1376 |
colorspace->reference = NULL; |
| 1377 |
colorspace->cdata = NULL; |
| 1378 |
|
| 1379 |
return; |
| 1380 |
} |
| 1381 |
|
| 1382 |
static void |
| 1383 |
pdf_clean_colorspace_struct (pdf_colorspace *colorspace) |
| 1384 |
{ |
| 1385 |
ASSERT(colorspace); |
| 1386 |
|
| 1387 |
if (colorspace->ident) |
| 1388 |
RELEASE(colorspace->ident); |
| 1389 |
if (colorspace->resource) |
| 1390 |
pdf_release_obj(colorspace->resource); |
| 1391 |
if (colorspace->reference) |
| 1392 |
pdf_release_obj(colorspace->reference); |
| 1393 |
colorspace->resource = NULL; |
| 1394 |
colorspace->reference = NULL; |
| 1395 |
|
| 1396 |
if (colorspace->cdata) { |
| 1397 |
switch (colorspace->subtype) { |
| 1398 |
case PDF_COLORSPACE_TYPE_ICCBASED: |
| 1399 |
release_iccbased_cdata(colorspace->cdata); |
| 1400 |
break; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
colorspace->cdata = NULL; |
| 1404 |
colorspace->subtype = PDF_COLORSPACE_TYPE_INVALID; |
| 1405 |
|
| 1406 |
return; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static void |
| 1410 |
pdf_flush_colorspace (pdf_colorspace *colorspace) |
| 1411 |
{ |
| 1412 |
ASSERT(colorspace); |
| 1413 |
|
| 1414 |
if (colorspace->resource) |
| 1415 |
pdf_release_obj(colorspace->resource); |
| 1416 |
if (colorspace->reference) |
| 1417 |
pdf_release_obj(colorspace->reference); |
| 1418 |
|
| 1419 |
colorspace->resource = NULL; |
| 1420 |
colorspace->reference = NULL; |
| 1421 |
} |
| 1422 |
|
| 1423 |
int |
| 1424 |
pdf_colorspace_defineresource (const char *ident, |
| 1425 |
int subtype, void *cdata, pdf_obj *resource) |
| 1426 |
{ |
| 1427 |
int cspc_id; |
| 1428 |
pdf_colorspace *colorspace; |
| 1429 |
|
| 1430 |
if (cspc_cache.count >= cspc_cache.capacity) { |
| 1431 |
cspc_cache.capacity += 16; |
| 1432 |
cspc_cache.colorspaces = RENEW(cspc_cache.colorspaces, |
| 1433 |
cspc_cache.capacity, pdf_colorspace); |
| 1434 |
} |
| 1435 |
cspc_id = cspc_cache.count; |
| 1436 |
colorspace = &cspc_cache.colorspaces[cspc_id]; |
| 1437 |
|
| 1438 |
pdf_init_colorspace_struct(colorspace); |
| 1439 |
if (ident) { |
| 1440 |
colorspace->ident = NEW(strlen(ident) + 1, char); |
| 1441 |
strcpy(colorspace->ident, ident); |
| 1442 |
} |
| 1443 |
colorspace->subtype = subtype; |
| 1444 |
colorspace->cdata = cdata; |
| 1445 |
colorspace->resource = resource; |
| 1446 |
|
| 1447 |
if (dpx_conf.verbose_level > 0) { |
| 1448 |
MESG("(ColorSpace:%s", ident); |
| 1449 |
if (dpx_conf.verbose_level > 1) { |
| 1450 |
switch (subtype) { |
| 1451 |
case PDF_COLORSPACE_TYPE_ICCBASED: |
| 1452 |
MESG("[ICCBased]"); |
| 1453 |
break; |
| 1454 |
case PDF_COLORSPACE_TYPE_CALRGB: |
| 1455 |
MESG("[CalRGB]"); |
| 1456 |
break; |
| 1457 |
case PDF_COLORSPACE_TYPE_CALGRAY: |
| 1458 |
MESG("[CalGray]"); |
| 1459 |
break; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
MESG(")"); |
| 1463 |
} |
| 1464 |
|
| 1465 |
cspc_cache.count++; |
| 1466 |
|
| 1467 |
return cspc_id; |
| 1468 |
} |
| 1469 |
|
| 1470 |
pdf_obj * |
| 1471 |
pdf_get_colorspace_reference (int cspc_id) |
| 1472 |
{ |
| 1473 |
pdf_colorspace *colorspace; |
| 1474 |
|
| 1475 |
colorspace = &cspc_cache.colorspaces[cspc_id]; |
| 1476 |
if (!colorspace->reference) { |
| 1477 |
colorspace->reference = pdf_ref_obj(colorspace->resource); |
| 1478 |
pdf_release_obj(colorspace->resource); /* .... */ |
| 1479 |
colorspace->resource = NULL; |
| 1480 |
} |
| 1481 |
|
| 1482 |
return pdf_link_obj(colorspace->reference); |
| 1483 |
} |
| 1484 |
|
| 1485 |
#if 0 |
| 1486 |
int |
| 1487 |
pdf_get_colorspace_num_components (int cspc_id) |
| 1488 |
{ |
| 1489 |
pdf_colorspace *colorspace; |
| 1490 |
int num_components; |
| 1491 |
|
| 1492 |
colorspace = &cspc_cache.colorspaces[cspc_id]; |
| 1493 |
|
| 1494 |
switch (colorspace->subtype) { |
| 1495 |
case PDF_COLORSPACE_TYPE_ICCBASED: |
| 1496 |
num_components = get_num_components_iccbased(colorspace->cdata); |
| 1497 |
break; |
| 1498 |
case PDF_COLORSPACE_TYPE_DEVICEGRAY: |
| 1499 |
num_components = 1; |
| 1500 |
break; |
| 1501 |
case PDF_COLORSPACE_TYPE_DEVICERGB: |
| 1502 |
num_components = 3; |
| 1503 |
break; |
| 1504 |
case PDF_COLORSPACE_TYPE_DEVICECMYK: |
| 1505 |
num_components = 4; |
| 1506 |
break; |
| 1507 |
case PDF_COLORSPACE_TYPE_CALRGB: |
| 1508 |
num_components = 3; |
| 1509 |
break; |
| 1510 |
case PDF_COLORSPACE_TYPE_CALGRAY: |
| 1511 |
num_components = 1; |
| 1512 |
break; |
| 1513 |
default: |
| 1514 |
num_components = 0; |
| 1515 |
break; |
| 1516 |
} |
| 1517 |
|
| 1518 |
return num_components; |
| 1519 |
} |
| 1520 |
|
| 1521 |
int |
| 1522 |
pdf_get_colorspace_subtype (int cspc_id) |
| 1523 |
{ |
| 1524 |
pdf_colorspace *colorspace; |
| 1525 |
|
| 1526 |
colorspace = &cspc_cache.colorspaces[cspc_id]; |
| 1527 |
|
| 1528 |
return colorspace->subtype; |
| 1529 |
} |
| 1530 |
#endif |
| 1531 |
|
| 1532 |
void |
| 1533 |
pdf_init_colors (void) |
| 1534 |
{ |
| 1535 |
cspc_cache.count = 0; |
| 1536 |
cspc_cache.capacity = 0; |
| 1537 |
cspc_cache.colorspaces = NULL; |
| 1538 |
} |
| 1539 |
|
| 1540 |
void |
| 1541 |
pdf_close_colors (void) |
| 1542 |
{ |
| 1543 |
int i; |
| 1544 |
|
| 1545 |
for (i = 0; i < cspc_cache.count; i++) { |
| 1546 |
pdf_colorspace *colorspace; |
| 1547 |
|
| 1548 |
colorspace = &cspc_cache.colorspaces[i]; |
| 1549 |
pdf_flush_colorspace(colorspace); |
| 1550 |
pdf_clean_colorspace_struct(colorspace); |
| 1551 |
} |
| 1552 |
RELEASE(cspc_cache.colorspaces); |
| 1553 |
cspc_cache.colorspaces = NULL; |
| 1554 |
cspc_cache.count = cspc_cache.capacity = 0; |
| 1555 |
|
| 1556 |
} |
| 1557 |
|
| 1558 |
#define PDF_COLORSPACE_FAMILY_DEVICE 0 |
| 1559 |
#define PDF_COLORSPACE_FAMILY_CIEBASED 1 |
| 1560 |
#define PDF_COLORSPACE_FAMILY_SPECIAL 2 |