1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
|
/* Copyright (C) 2001-2012 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* Common code for ImageType 1 and 4 initialization */
#include "gx.h"
#include "math_.h"
#include "memory_.h"
#include "gpcheck.h"
#include "gscdefs.h" /* for image class table */
#include "gserrors.h"
#include "gsstruct.h"
#include "gsutil.h"
#include "gxfixed.h"
#include "gxfrac.h"
#include "gxarith.h"
#include "gxmatrix.h"
#include "gsccolor.h"
#include "gspaint.h"
#include "gzstate.h"
#include "gxdevice.h"
#include "gzpath.h"
#include "gzcpath.h"
#include "gxdevmem.h"
#include "gximage.h"
#include "gxiparam.h"
#include "gdevmrop.h"
#include "gscspace.h"
#include "gscindex.h"
#include "gsicc_cache.h"
#include "gsicc_cms.h"
/* Structure descriptors */
private_st_gx_image_enum();
/* Image class procedures */
extern_gx_image_class_table();
/* Enumerator procedures */
static const gx_image_enum_procs_t image1_enum_procs = {
gx_image1_plane_data, gx_image1_end_image, gx_image1_flush
};
/* GC procedures */
gs_private_st_ptrs2(st_color_cache, gx_image_color_cache_t, "gx_image_color_cache",
color_cache_enum_ptrs, color_cache_reloc_ptrs,
is_transparent, device_contone);
static
ENUM_PTRS_WITH(image_enum_enum_ptrs, gx_image_enum *eptr)
{
int bps;
gs_ptr_type_t ret;
/* Enumerate the used members of clues.dev_color. */
index -= gx_image_enum_num_ptrs;
bps = eptr->unpack_bps;
if (eptr->spp != 1)
bps = 8;
else if (bps > 8 || eptr->unpack == sample_unpack_copy)
bps = 1;
if (index >= (1 << bps) * st_device_color_max_ptrs) /* done */
return 0;
/* the clues may have been cleared by gx_image_free_enum, but not freed in that */
/* function due to being at a different save level. Only trace if dev_color.type != 0. */
if (eptr->spp == 1) {
if (eptr->clues != NULL) {
if (eptr->clues[(index/st_device_color_max_ptrs) *
(255 / ((1 << bps) - 1))].dev_color.type != 0) {
ret = ENUM_USING(st_device_color,
&eptr->clues[(index / st_device_color_max_ptrs) *
(255 / ((1 << bps) - 1))].dev_color,
sizeof(eptr->clues[0].dev_color),
index % st_device_color_max_ptrs);
} else {
ret = 0;
}
} else {
ret = 0;
}
} else {
ret = 0;
}
if (ret == 0) /* don't stop early */
ENUM_RETURN(0);
return ret;
}
#define e1(i,elt) ENUM_PTR(i,gx_image_enum,elt);
gx_image_enum_do_ptrs(e1)
#undef e1
ENUM_PTRS_END
static RELOC_PTRS_WITH(image_enum_reloc_ptrs, gx_image_enum *eptr)
{
int i;
#define r1(i,elt) RELOC_PTR(gx_image_enum,elt);
gx_image_enum_do_ptrs(r1)
#undef r1
{
int bps = eptr->unpack_bps;
if (eptr->spp != 1)
bps = 8;
else if (bps > 8 || eptr->unpack == sample_unpack_copy)
bps = 1;
if (eptr->spp == 1) {
for (i = 0; i <= 255; i += 255 / ((1 << bps) - 1))
RELOC_USING(st_device_color,
&eptr->clues[i].dev_color, sizeof(gx_device_color));
}
}
}
RELOC_PTRS_END
/* Forward declarations */
static int color_draws_b_w(gx_device * dev,
const gx_drawing_color * pdcolor);
static void image_init_map(byte * map, int map_size, const float *decode);
static void image_init_colors(gx_image_enum * penum, int bps, int spp,
gs_image_format_t format,
const float *decode,
const gs_imager_state * pis, gx_device * dev,
const gs_color_space * pcs, bool * pdcb);
/* Procedures for unpacking the input data into bytes or fracs. */
/*extern SAMPLE_UNPACK_PROC(sample_unpack_copy); *//* declared above */
/*
* Do common initialization for processing an ImageType 1 or 4 image.
* Allocate the enumerator and fill in the following members:
* rect
*/
int
gx_image_enum_alloc(const gs_image_common_t * pic,
const gs_int_rect * prect, gs_memory_t * mem,
gx_image_enum **ppenum)
{
const gs_pixel_image_t *pim = (const gs_pixel_image_t *)pic;
int width = pim->Width, height = pim->Height;
int bpc = pim->BitsPerComponent;
gx_image_enum *penum;
if (width < 0 || height < 0)
return_error(gs_error_rangecheck);
switch (pim->format) {
case gs_image_format_chunky:
case gs_image_format_component_planar:
switch (bpc) {
case 1: case 2: case 4: case 8: case 12: case 16: break;
default: return_error(gs_error_rangecheck);
}
break;
case gs_image_format_bit_planar:
if (bpc < 1 || bpc > 8)
return_error(gs_error_rangecheck);
}
if (prect) {
if (prect->p.x < 0 || prect->p.y < 0 ||
prect->q.x < prect->p.x || prect->q.y < prect->p.y ||
prect->q.x > width || prect->q.y > height
)
return_error(gs_error_rangecheck);
}
penum = gs_alloc_struct(mem, gx_image_enum, &st_gx_image_enum,
"gx_default_begin_image");
if (penum == 0)
return_error(gs_error_VMerror);
if (prect) {
penum->rect.x = prect->p.x;
penum->rect.y = prect->p.y;
penum->rect.w = prect->q.x - prect->p.x;
penum->rect.h = prect->q.y - prect->p.y;
} else {
penum->rect.x = 0, penum->rect.y = 0;
penum->rect.w = width, penum->rect.h = height;
}
penum->rrect.x = penum->rect.x;
penum->rrect.y = penum->rect.y;
penum->rrect.w = penum->rect.w;
penum->rrect.h = penum->rect.h;
#ifdef DEBUG
if (gs_debug_c('b')) {
dlprintf2("[b]Image: w=%d h=%d", width, height);
if (prect)
dprintf4(" ((%d,%d),(%d,%d))",
prect->p.x, prect->p.y, prect->q.x, prect->q.y);
}
#endif
*ppenum = penum;
return 0;
}
/*
* Finish initialization for processing an ImageType 1 or 4 image.
* Assumes the following members of *penum are set in addition to those
* set by gx_image_enum_alloc:
* alpha, use_mask_color, mask_color (if use_mask_color is true),
* masked, adjust
*/
int
gx_image_enum_begin(gx_device * dev, const gs_imager_state * pis,
const gs_matrix *pmat, const gs_image_common_t * pic,
const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
gs_memory_t * mem, gx_image_enum *penum)
{
const gs_pixel_image_t *pim = (const gs_pixel_image_t *)pic;
gs_image_format_t format = pim->format;
const int width = pim->Width;
const int height = pim->Height;
const int bps = pim->BitsPerComponent;
bool masked = penum->masked;
const float *decode = pim->Decode;
gs_matrix_double mat;
int index_bps;
const gs_color_space *pcs = pim->ColorSpace;
gs_logical_operation_t lop = (pis ? pis->log_op : lop_default);
int code;
int log2_xbytes = (bps <= 8 ? 0 : arch_log2_sizeof_frac);
int spp, nplanes, spread;
uint bsize;
byte *buffer;
fixed mtx, mty;
gs_fixed_point row_extent, col_extent, x_extent, y_extent;
bool device_color = true;
gs_fixed_rect obox, cbox;
penum->clues = NULL;
penum->icc_setup.has_transfer = false;
penum->icc_setup.is_lab = false;
penum->icc_setup.must_halftone = false;
penum->icc_setup.need_decode = false;
penum->Width = width;
penum->Height = height;
if (pmat == 0)
pmat = &ctm_only(pis);
if (pim->ImageMatrix.xx == pmat->xx && pim->ImageMatrix.xy == pmat->xy &&
pim->ImageMatrix.yx == pmat->yx && pim->ImageMatrix.yy == pmat->yy) {
/* Process common special case separately to accept singular matrix. */
mat.xx = mat.yy = 1.;
mat.xy = mat.yx = 0.;
mat.tx = pmat->tx - pim->ImageMatrix.tx;
mat.ty = pmat->ty - pim->ImageMatrix.ty;
} else {
if ((code = gs_matrix_invert_to_double(&pim->ImageMatrix, &mat)) < 0 ||
(code = gs_matrix_multiply_double(&mat, pmat, &mat)) < 0
) {
gs_free_object(mem, penum, "gx_default_begin_image");
return code;
}
}
/* Grid fit: A common construction in postscript/PDF files is for images
* to be constructed as a series of 'stacked' 1 pixel high images.
* Furthermore, many of these are implemented as an imagemask plotted on
* top of thin rectangles. The different fill rules for images and line
* art produces problems; line art fills a pixel if any part of it is
* touched - images only fill a pixel if the centre of the pixel is
* covered. Bug 692666 is such a problem.
*
* As a workaround for this problem, the code below was introduced. The
* concept is that orthogonal images can be 'grid fitted' (or 'stretched')
* to entirely cover pixels that they touch. Initially I had this working
* for all images regardless of type, but as testing has proceeded, this
* showed more and more regressions, so I've cut the cases back in which
* this code is used until it now only triggers on imagemasks that are
* either 1 pixel high, or wide, and then not if we are rendering a
* glyph (such as from a type3 font).
*/
if (pis != NULL && pis->is_gstate && ((gs_state *)pis)->show_gstate != NULL) {
/* If we're a graphics state, and we're in a text object, then we
* must be in a type3 font. Don't fiddle with it. */
} else if (!penum->masked || penum->image_parent_type != 0) {
/* We only grid fit for ImageMasks, currently */
} else if (pis != NULL && pis->fill_adjust.x == 0 && pis->fill_adjust.y == 0) {
/* If fill adjust is disabled, so is grid fitting */
} else if (mat.xy == 0 && mat.yx == 0) {
if (width == 1) {
if (mat.xx > 0) {
fixed ix0 = int2fixed(fixed2int(float2fixed(mat.tx)));
double x1 = mat.tx + mat.xx * width;
fixed ix1 = int2fixed(fixed2int_ceiling(float2fixed(x1)));
mat.tx = (double)fixed2float(ix0);
mat.xx = (double)(fixed2float(ix1 - ix0)/width);
} else if (mat.xx < 0) {
fixed ix0 = int2fixed(fixed2int_ceiling(float2fixed(mat.tx)));
double x1 = mat.tx + mat.xx * width;
fixed ix1 = int2fixed(fixed2int(float2fixed(x1)));
mat.tx = (double)fixed2float(ix0);
mat.xx = (double)(fixed2float(ix1 - ix0)/width);
}
}
if (height == 1) {
if (mat.yy > 0) {
fixed iy0 = int2fixed(fixed2int(float2fixed(mat.ty)));
double y1 = mat.ty + mat.yy * height;
fixed iy1 = int2fixed(fixed2int_ceiling(float2fixed(y1)));
mat.ty = (double)fixed2float(iy0);
mat.yy = (double)(fixed2float(iy1 - iy0)/height);
} else if (mat.yy < 0) {
fixed iy0 = int2fixed(fixed2int_ceiling(float2fixed(mat.ty)));
double y1 = mat.ty + mat.yy * height;
fixed iy1 = int2fixed(fixed2int(float2fixed(y1)));
mat.ty = (double)fixed2float(iy0);
mat.yy = ((double)fixed2float(iy1 - iy0)/height);
}
}
} else if (mat.xx == 0 && mat.yy == 0) {
if (height == 1) {
if (mat.yx > 0) {
fixed ix0 = int2fixed(fixed2int(float2fixed(mat.tx)));
double x1 = mat.tx + mat.yx * height;
fixed ix1 = int2fixed(fixed2int_ceiling(float2fixed(x1)));
mat.tx = (double)fixed2float(ix0);
mat.yx = (double)(fixed2float(ix1 - ix0)/height);
} else if (mat.yx < 0) {
fixed ix0 = int2fixed(fixed2int_ceiling(float2fixed(mat.tx)));
double x1 = mat.tx + mat.yx * height;
fixed ix1 = int2fixed(fixed2int(float2fixed(x1)));
mat.tx = (double)fixed2float(ix0);
mat.yx = (double)(fixed2float(ix1 - ix0)/height);
}
}
if (width == 1) {
if (mat.xy > 0) {
fixed iy0 = int2fixed(fixed2int(float2fixed(mat.ty)));
double y1 = mat.ty + mat.xy * width;
fixed iy1 = int2fixed(fixed2int_ceiling(float2fixed(y1)));
mat.ty = (double)fixed2float(iy0);
mat.xy = (double)(fixed2float(iy1 - iy0)/width);
} else if (mat.xy < 0) {
fixed iy0 = int2fixed(fixed2int_ceiling(float2fixed(mat.ty)));
double y1 = mat.ty + mat.xy * width;
fixed iy1 = int2fixed(fixed2int(float2fixed(y1)));
mat.ty = (double)fixed2float(iy0);
mat.xy = ((double)fixed2float(iy1 - iy0)/width);
}
}
}
/* Can we restrict the amount of image we need? */
while (pcpath) /* So we can break out of it */
{
gs_rect rect, rect_out;
gs_matrix mi;
gs_fixed_rect obox;
gs_int_rect irect;
if ((code = gs_matrix_invert(&ctm_only(pis), &mi)) < 0 ||
(code = gs_matrix_multiply(&mi, &pic->ImageMatrix, &mi)) < 0) {
/* Give up trying to shrink the render box, but continue processing */
break;
}
gx_cpath_outer_box(pcpath, &obox);
rect.p.x = fixed2float(obox.p.x);
rect.p.y = fixed2float(obox.p.y);
rect.q.x = fixed2float(obox.q.x);
rect.q.y = fixed2float(obox.q.y);
code = gs_bbox_transform(&rect, &mi, &rect_out);
if (code < 0) {
/* Give up trying to shrink the render box, but continue processing */
break;
}
irect.p.x = (int)(rect_out.p.x-1.0);
irect.p.y = (int)(rect_out.p.y-1.0);
irect.q.x = (int)(rect_out.q.x+1.0);
irect.q.y = (int)(rect_out.q.y+1.0);
/* Need to expand the region to allow for the fact that the mitchell
* scaler reads multiple pixels in. This calculation can probably be
* improved. */
/* If mi.{xx,yy} > 1 then we are downscaling. During downscaling,
* the support increases to ensure that we don't lose pixels contributions
* entirely. */
/* I do not understand the need for the +/- 1 fudge factors in the Y,
* but they seem to be required. Increasing the render rectangle can
* never be bad at least... RJW */
irect.p.x -= MAX_ISCALE_SUPPORT * (int)any_abs(mi.xx) + 1;
irect.p.y -= MAX_ISCALE_SUPPORT * (int)any_abs(mi.yy) + 1;
irect.q.x += MAX_ISCALE_SUPPORT * (int)max(1,any_abs(mi.xx)+1.0) + 1;
irect.q.y += MAX_ISCALE_SUPPORT * (int)max(1,any_abs(mi.yy)+1.0) + 1;
if (penum->rrect.x < irect.p.x) {
penum->rrect.w -= irect.p.x - penum->rrect.x;
if (penum->rrect.w < 0)
penum->rrect.w = 0;
penum->rrect.x = irect.p.x;
}
if (penum->rrect.x + penum->rrect.w > irect.q.x) {
penum->rrect.w = irect.q.x - penum->rrect.x;
if (penum->rrect.w < 0)
penum->rrect.w = 0;
}
if (penum->rrect.y < irect.p.y) {
penum->rrect.h -= irect.p.y - penum->rrect.y;
if (penum->rrect.h < 0)
penum->rrect.h = 0;
penum->rrect.y = irect.p.y;
}
if (penum->rrect.y + penum->rrect.h > irect.q.y) {
penum->rrect.h = irect.q.y - penum->rrect.y;
if (penum->rrect.h < 0)
penum->rrect.h = 0;
}
break; /* Out of the while */
}
/*penum->matrix = mat;*/
penum->matrix.xx = mat.xx;
penum->matrix.xy = mat.xy;
penum->matrix.yx = mat.yx;
penum->matrix.yy = mat.yy;
penum->matrix.tx = mat.tx;
penum->matrix.ty = mat.ty;
if_debug6('b', " [%g %g %g %g %g %g]\n",
mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
/* following works for 1, 2, 4, 8, 12, 16 */
index_bps = (bps < 8 ? bps >> 1 : (bps >> 2) + 1);
/*
* Compute extents with distance transformation.
*/
if (mat.tx > 0)
mtx = float2fixed(mat.tx);
else { /* Use positive values to ensure round down. */
int f = (int)-mat.tx + 1;
mtx = float2fixed(mat.tx + f) - int2fixed(f);
}
if (mat.ty > 0)
mty = float2fixed(mat.ty);
else { /* Use positive values to ensure round down. */
int f = (int)-mat.ty + 1;
mty = float2fixed(mat.ty + f) - int2fixed(f);
}
row_extent.x = float2fixed_rounded(width * mat.xx);
row_extent.y =
(is_fzero(mat.xy) ? fixed_0 :
float2fixed_rounded(width * mat.xy));
col_extent.x =
(is_fzero(mat.yx) ? fixed_0 :
float2fixed_rounded(height * mat.yx));
col_extent.y = float2fixed_rounded(height * mat.yy);
gx_image_enum_common_init((gx_image_enum_common_t *)penum,
(const gs_data_image_t *)pim,
&image1_enum_procs, dev,
(masked ? 1 : (penum->alpha ? cs_num_components(pcs)+1 : cs_num_components(pcs))),
format);
if (penum->rect.w == width && penum->rect.h == height) {
x_extent = row_extent;
y_extent = col_extent;
} else {
int rw = penum->rect.w, rh = penum->rect.h;
x_extent.x = float2fixed_rounded(rw * mat.xx);
x_extent.y =
(is_fzero(mat.xy) ? fixed_0 :
float2fixed_rounded(rw * mat.xy));
y_extent.x =
(is_fzero(mat.yx) ? fixed_0 :
float2fixed_rounded(rh * mat.yx));
y_extent.y = float2fixed_rounded(rh * mat.yy);
}
/* Set icolor0 and icolor1 to point to image clues locations if we have
1spp or an imagemask, otherwise image clues is not used and
we have these values point to other member variables */
if (masked || cs_num_components(pcs) == 1) {
/* Go ahead and allocate now if not already done. For a mask
we really should only do 2 values. For now, the goal is to
eliminate the 256 bytes for the >8bpp image enumerator */
penum->clues = (gx_image_clue*) gs_alloc_bytes(mem, sizeof(gx_image_clue)*256,
"gx_image_enum_begin");
penum->icolor0 = &(penum->clues[0].dev_color);
penum->icolor1 = &(penum->clues[255].dev_color);
} else {
penum->icolor0 = &(penum->icolor0_val);
penum->icolor1 = &(penum->icolor1_val);
}
if (masked) { /* This is imagemask. */
if (bps != 1 || pcs != NULL || penum->alpha || decode[0] == decode[1]) {
gs_free_object(mem, penum, "gx_default_begin_image");
return_error(gs_error_rangecheck);
}
/* Initialize color entries 0 and 255. */
set_nonclient_dev_color(penum->icolor0, gx_no_color_index);
set_nonclient_dev_color(penum->icolor1, gx_no_color_index);
*(penum->icolor1) = *pdcolor;
memcpy(&penum->map[0].table.lookup4x1to32[0],
(decode[0] < decode[1] ? lookup4x1to32_inverted :
lookup4x1to32_identity),
16 * 4);
penum->map[0].decoding = sd_none;
spp = 1;
lop = rop3_know_S_0(lop);
} else { /* This is image, not imagemask. */
const gs_color_space_type *pcst = pcs->type;
int b_w_color;
spp = cs_num_components(pcs);
if (spp < 0) { /* Pattern not allowed */
gs_free_object(mem, penum, "gx_default_begin_image");
return_error(gs_error_rangecheck);
}
if (penum->alpha)
++spp;
/* Use a less expensive format if possible. */
switch (format) {
case gs_image_format_bit_planar:
if (bps > 1)
break;
format = gs_image_format_component_planar;
case gs_image_format_component_planar:
if (spp == 1)
format = gs_image_format_chunky;
default: /* chunky */
break;
}
if (pcs->cmm_icc_profile_data != NULL) {
device_color = false;
} else {
device_color = (*pcst->concrete_space) (pcs, pis) == pcs;
}
image_init_colors(penum, bps, spp, format, decode, pis, dev,
pcs, &device_color);
/* If we have a CIE based color space and the icc equivalent profile
is not yet set, go ahead and handle that now. It may already
be done due to the above init_colors which may go through remap. */
if (gs_color_space_is_PSCIE(pcs) && pcs->icc_equivalent == NULL) {
gs_colorspace_set_icc_equivalent(pcs, &(penum->icc_setup.is_lab),
pis->memory);
if (penum->icc_setup.is_lab) {
/* Free what ever profile was created and use the icc manager's
cielab profile */
gs_color_space *curr_pcs = pcs;
rc_decrement(curr_pcs->icc_equivalent,"gx_image_enum_begin");
rc_decrement(curr_pcs->cmm_icc_profile_data,"gx_image_enum_begin");
curr_pcs->cmm_icc_profile_data = pis->icc_manager->lab_profile;
rc_increment(curr_pcs->cmm_icc_profile_data);
}
}
/* Try to transform non-default RasterOps to something */
/* that we implement less expensively. */
if (!pim->CombineWithColor)
lop = rop3_know_T_0(lop) & ~lop_T_transparent;
else if ((rop3_uses_T(lop) && color_draws_b_w(dev, pdcolor) == 0))
lop = rop3_know_T_0(lop);
if (lop != rop3_S && /* if best case, no more work needed */
!rop3_uses_T(lop) && bps == 1 && spp == 1 &&
(b_w_color =
color_draws_b_w(dev, penum->icolor0)) >= 0 &&
color_draws_b_w(dev, penum->icolor1) == (b_w_color ^ 1)
) {
if (b_w_color) { /* Swap the colors and invert the RasterOp source. */
gx_device_color dcolor;
dcolor = *(penum->icolor0);
*(penum->icolor0) = *(penum->icolor1);
*(penum->icolor1) = dcolor;
lop = rop3_invert_S(lop);
}
/*
* At this point, we know that the source pixels
* correspond directly to the S input for the raster op,
* i.e., icolor0 is black and icolor1 is white.
*/
switch (lop) {
case rop3_D & rop3_S:
/* Implement this as an inverted mask writing 0s. */
*(penum->icolor1) = *(penum->icolor0);
/* (falls through) */
case rop3_D | rop3_not(rop3_S):
/* Implement this as an inverted mask writing 1s. */
memcpy(&penum->map[0].table.lookup4x1to32[0],
lookup4x1to32_inverted, 16 * 4);
rmask: /* Fill in the remaining parameters for a mask. */
penum->masked = masked = true;
set_nonclient_dev_color(penum->icolor0, gx_no_color_index);
penum->map[0].decoding = sd_none;
lop = rop3_T;
break;
case rop3_D & rop3_not(rop3_S):
/* Implement this as a mask writing 0s. */
*(penum->icolor1) = *(penum->icolor0);
/* (falls through) */
case rop3_D | rop3_S:
/* Implement this as a mask writing 1s. */
memcpy(&penum->map[0].table.lookup4x1to32[0],
lookup4x1to32_identity, 16 * 4);
goto rmask;
default:
;
}
}
}
penum->device_color = device_color;
/*
* Adjust width upward for unpacking up to 7 trailing bits in
* the row, plus 1 byte for end-of-run, plus up to 7 leading
* bits for data_x offset within a packed byte.
*/
bsize = ((bps > 8 ? width * 2 : width) + 15) * spp;
buffer = gs_alloc_bytes(mem, bsize, "image buffer");
if (buffer == 0) {
gs_free_object(mem, penum, "gx_default_begin_image");
return_error(gs_error_VMerror);
}
penum->bps = bps;
penum->unpack_bps = bps;
penum->log2_xbytes = log2_xbytes;
penum->spp = spp;
switch (format) {
case gs_image_format_chunky:
nplanes = 1;
spread = 1 << log2_xbytes;
break;
case gs_image_format_component_planar:
nplanes = spp;
spread = spp << log2_xbytes;
break;
case gs_image_format_bit_planar:
nplanes = spp * bps;
spread = spp << log2_xbytes;
break;
default:
/* No other cases are possible (checked by gx_image_enum_alloc). */
return_error(gs_error_Fatal);
}
penum->num_planes = nplanes;
penum->spread = spread;
/*
* If we're asked to interpolate in a partial image, we have to
* assume that the client either really only is interested in
* the given sub-image, or else is constructing output out of
* overlapping pieces.
*/
penum->interpolate = pim->Interpolate;
penum->x_extent = x_extent;
penum->y_extent = y_extent;
penum->posture =
((x_extent.y | y_extent.x) == 0 ? image_portrait :
(x_extent.x | y_extent.y) == 0 ? image_landscape :
image_skewed);
penum->pis = pis;
penum->pcs = pcs;
penum->memory = mem;
penum->buffer = buffer;
penum->buffer_size = bsize;
penum->line = NULL;
penum->icc_link = NULL;
penum->color_cache = NULL;
penum->ht_buffer = NULL;
penum->thresh_buffer = NULL;
penum->cie_range = NULL;
penum->line_size = 0;
penum->use_rop = lop != (masked ? rop3_T : rop3_S);
#ifdef DEBUG
if (gs_debug_c('*')) {
if (penum->use_rop)
dprintf1("[%03x]", lop);
dprintf5("%c%d%c%dx%d ",
(masked ? (color_is_pure(pdcolor) ? 'm' : 'h') : 'i'),
bps,
(penum->posture == image_portrait ? ' ' :
penum->posture == image_landscape ? 'L' : 'T'),
width, height);
}
#endif
penum->slow_loop = 0;
if (pcpath == 0) {
(*dev_proc(dev, get_clipping_box)) (dev, &obox);
cbox = obox;
penum->clip_image = 0;
} else
penum->clip_image =
(gx_cpath_outer_box(pcpath, &obox) | /* not || */
gx_cpath_inner_box(pcpath, &cbox) ?
0 : image_clip_region);
penum->clip_outer = obox;
penum->clip_inner = cbox;
penum->log_op = rop3_T; /* rop device takes care of this */
penum->clip_dev = 0; /* in case we bail out */
penum->rop_dev = 0; /* ditto */
penum->scaler = 0; /* ditto */
/*
* If all four extrema of the image fall within the clipping
* rectangle, clipping is never required. When making this check,
* we must carefully take into account the fact that we only care
* about pixel centers.
*/
{
fixed
epx = min(row_extent.x, 0) + min(col_extent.x, 0),
eqx = max(row_extent.x, 0) + max(col_extent.x, 0),
epy = min(row_extent.y, 0) + min(col_extent.y, 0),
eqy = max(row_extent.y, 0) + max(col_extent.y, 0);
{
int hwx, hwy;
switch (penum->posture) {
case image_portrait:
hwx = width, hwy = height;
break;
case image_landscape:
hwx = height, hwy = width;
break;
default:
hwx = hwy = 0;
}
/*
* If the image is only 1 sample wide or high,
* and is less than 1 device pixel wide or high,
* move it slightly so that it covers pixel centers.
* This is a hack to work around a bug in some old
* versions of TeX/dvips, which use 1-bit-high images
* to draw horizontal and vertical lines without
* positioning them properly.
*/
if (hwx == 1 && eqx - epx < fixed_1) {
fixed diff =
arith_rshift_1(row_extent.x + col_extent.x);
mtx = (((mtx + diff) | fixed_half) & -fixed_half) - diff;
}
if (hwy == 1 && eqy - epy < fixed_1) {
fixed diff =
arith_rshift_1(row_extent.y + col_extent.y);
mty = (((mty + diff) | fixed_half) & -fixed_half) - diff;
}
}
if_debug5('b', "[b]Image: %sspp=%d, bps=%d, mt=(%g,%g)\n",
(masked? "masked, " : ""), spp, bps,
fixed2float(mtx), fixed2float(mty));
if_debug9('b',
"[b] cbox=(%g,%g),(%g,%g), obox=(%g,%g),(%g,%g), clip_image=0x%x\n",
fixed2float(cbox.p.x), fixed2float(cbox.p.y),
fixed2float(cbox.q.x), fixed2float(cbox.q.y),
fixed2float(obox.p.x), fixed2float(obox.p.y),
fixed2float(obox.q.x), fixed2float(obox.q.y),
penum->clip_image);
dda_init(penum->dda.row.x, mtx, col_extent.x, height);
dda_init(penum->dda.row.y, mty, col_extent.y, height);
penum->dst_width = row_extent.x;
penum->dst_height = col_extent.y;
penum->yi0 = fixed2int_pixround_perfect(dda_current(penum->dda.row.y)); /* For gs_image_class_0_interpolate. */
if (penum->rect.y) {
int y = penum->rect.y;
while (y--) {
dda_next(penum->dda.row.x);
dda_next(penum->dda.row.y);
}
}
penum->cur.x = penum->prev.x = dda_current(penum->dda.row.x);
penum->cur.y = penum->prev.y = dda_current(penum->dda.row.y);
dda_init(penum->dda.strip.x, penum->cur.x, row_extent.x, width);
dda_init(penum->dda.strip.y, penum->cur.y, row_extent.y, width);
if (penum->rect.x) {
dda_advance(penum->dda.strip.x, penum->rect.x);
dda_advance(penum->dda.strip.y, penum->rect.x);
} {
fixed ox = dda_current(penum->dda.strip.x);
fixed oy = dda_current(penum->dda.strip.y);
if (!penum->clip_image) /* i.e., not clip region */
penum->clip_image =
(fixed_pixround(ox + epx) < fixed_pixround(cbox.p.x) ?
image_clip_xmin : 0) +
(fixed_pixround(ox + eqx) >= fixed_pixround(cbox.q.x) ?
image_clip_xmax : 0) +
(fixed_pixround(oy + epy) < fixed_pixround(cbox.p.y) ?
image_clip_ymin : 0) +
(fixed_pixround(oy + eqy) >= fixed_pixround(cbox.q.y) ?
image_clip_ymax : 0);
}
}
penum->y = 0;
penum->used.x = 0;
penum->used.y = 0;
{
static sample_unpack_proc_t procs[2][6] = {
{ sample_unpack_1, sample_unpack_2,
sample_unpack_4, sample_unpack_8,
0, 0
},
{ sample_unpack_1_interleaved, sample_unpack_2_interleaved,
sample_unpack_4_interleaved, sample_unpack_8_interleaved,
0, 0
}};
int num_planes = penum->num_planes;
bool interleaved = (num_planes == 1 && penum->plane_depths[0] != penum->bps);
int i;
procs[0][4] = procs[1][4] = sample_unpack_12_proc;
procs[0][5] = procs[1][5] = sample_unpack_16_proc;
if (interleaved) {
int num_components = penum->plane_depths[0] / penum->bps;
for (i = 1; i < num_components; i++) {
if (decode[0] != decode[i * 2 + 0] ||
decode[1] != decode[i * 2 + 1])
break;
}
if (i == num_components)
interleaved = false; /* Use single table. */
}
if (index_bps >= 4) {
if ((penum->unpack = procs[interleaved][index_bps]) == 0) { /* bps case not supported. */
gx_default_end_image(dev,
(gx_image_enum_common_t *) penum,
false);
return_error(gs_error_rangecheck);
}
} else {
penum->unpack = procs[interleaved][index_bps];
}
if_debug1('b', "[b]unpack=%d\n", bps);
/* Set up pixel0 for image class procedures. */
penum->dda.pixel0 = penum->dda.strip;
for (i = 0; i < gx_image_class_table_count; ++i)
if ((penum->render = gx_image_class_table[i](penum)) != 0)
break;
if (i == gx_image_class_table_count) {
/* No available class can handle this image. */
gx_default_end_image(dev, (gx_image_enum_common_t *) penum,
false);
return_error(gs_error_rangecheck);
}
}
if (penum->clip_image && pcpath) { /* Set up the clipping device. */
gx_device_clip *cdev =
gs_alloc_struct(mem, gx_device_clip,
&st_device_clip, "image clipper");
if (cdev == 0) {
gx_default_end_image(dev,
(gx_image_enum_common_t *) penum,
false);
return_error(gs_error_VMerror);
}
gx_make_clip_device_in_heap(cdev, pcpath, dev, mem);
penum->clip_dev = cdev;
}
if (penum->use_rop) { /* Set up the RasterOp source device. */
gx_device_rop_texture *rtdev;
code = gx_alloc_rop_texture_device(&rtdev, mem,
"image RasterOp");
if (code < 0) {
gx_default_end_image(dev, (gx_image_enum_common_t *) penum,
false);
return code;
}
gx_make_rop_texture_device(rtdev,
(penum->clip_dev != 0 ?
(gx_device *) penum->clip_dev :
dev), lop, pdcolor);
gx_device_retain((gx_device *)rtdev, true);
penum->rop_dev = rtdev;
}
return 0;
}
/* If a drawing color is black or white, return 0 or 1 respectively, */
/* otherwise return -1. */
static int
color_draws_b_w(gx_device * dev, const gx_drawing_color * pdcolor)
{
if (color_is_pure(pdcolor)) {
gx_color_value rgb[3];
(*dev_proc(dev, map_color_rgb)) (dev, gx_dc_pure_color(pdcolor),
rgb);
if (!(rgb[0] | rgb[1] | rgb[2]))
return 0;
if ((rgb[0] & rgb[1] & rgb[2]) == gx_max_color_value)
return 1;
}
return -1;
}
static void
image_cache_decode(gx_image_enum *penum, byte input, byte *output, bool scale)
{
float temp;
switch ( penum->map[0].decoding ) {
case sd_none:
*output = input;
break;
case sd_lookup:
temp = penum->map[0].decode_lookup[input >> 4]*255.0f;
if (temp > 255) temp = 255;
if (temp < 0 ) temp = 0;
*output = (unsigned char) temp;
break;
case sd_compute:
temp = penum->map[0].decode_base +
(float) input * penum->map[0].decode_factor;
if (scale) {
temp = temp * 255.0;
}
if (temp > 255) temp = 255;
if (temp < 0 ) temp = 0;
*output = (unsigned char) temp;
break;
default:
*output = 0;
break;
}
}
static bool
decode_range_needed(gx_image_enum *penum)
{
bool scale = true;
if (penum->map[0].decoding == sd_compute) {
if (!(gs_color_space_is_ICC(penum->pcs) ||
gs_color_space_is_PSCIE(penum->pcs))) {
scale = false;
}
}
return scale;
}
/* A special case where we go ahead and initialize the whole index cache with
contone. Device colors. If we are halftoning we will then go ahead and
apply the thresholds to the device contone values. Only used for gray,
rgb or cmyk source colors (No DeviceN for now) */
/* TO DO Add in PSCIE decoder */
int
image_init_color_cache(gx_image_enum * penum, int bps, int spp)
{
int num_des_comp = penum->dev->color_info.num_components;
int num_src_comp;
int num_entries = 1 << bps;
bool need_decode = penum->icc_setup.need_decode;
bool has_transfer = penum->icc_setup.has_transfer;
byte value;
bool decode_scale = true;
int k, kk;
byte psrc[4];
byte *temp_buffer;
byte *byte_ptr;
bool is_indexed = (gs_color_space_get_index(penum->pcs) ==
gs_color_space_index_Indexed);
bool free_temp_buffer = true;
gsicc_bufferdesc_t input_buff_desc;
gsicc_bufferdesc_t output_buff_desc;
gx_color_value conc[GX_DEVICE_COLOR_MAX_COMPONENTS];
if (penum->icc_link == NULL) {
return gs_rethrow(-1, "ICC Link not created during image render color");
}
if (is_indexed) {
num_src_comp = gs_color_space_num_components(penum->pcs->base_space);
} else {
/* Detect case where cache is not needed. Colors are already in the
device space. Need to fast track this one and halftone row directly.
Detected in gximono.c by looking if penum->color_cache is NULL */
if (penum->icc_link->is_identity && !need_decode && !has_transfer) {
return 0;
}
num_src_comp = 1;
}
/* Allocate cache of device contone values */
penum->color_cache = gs_alloc_struct(penum->memory, gx_image_color_cache_t,
&st_color_cache,
"image_init_color_cache");
penum->color_cache->device_contone = (byte*) gs_alloc_bytes(penum->memory,
num_des_comp * num_entries * sizeof(byte), "image_init_color_cache");
penum->color_cache->is_transparent = (bool*) gs_alloc_bytes(penum->memory,
num_entries * sizeof(bool), "image_init_color_cache");
penum->color_cache->free_contone = true;
/* Initialize */
memset(penum->color_cache->is_transparent,0,num_entries * sizeof(bool));
/* Depending upon if we need decode and ICC CM, fill the cache a couple
different ways. If the link is the identity, then we don't need to do any
color conversions except for potentially a decode. This is written in
the manner shown below so that the common case of no decode and indexed
image with a look-up-table uses the table data directly or does as many
operations with memcpy as we can */
/* Need to check the decode output range so we know how we need to scale.
We want 8 bit output */
if (need_decode) {
decode_scale = decode_range_needed(penum);
}
if (penum->icc_link->is_identity) {
/* No CM needed. */
if (need_decode || has_transfer) {
/* Slower case. This could be sped up later to avoid the tests
within the loop by use of specialized loops. */
for (k = 0; k < num_entries; k++) {
/* Data is in k */
if (need_decode) {
image_cache_decode(penum, k, &value, decode_scale);
} else {
value = k;
}
/* Data is in value */
if (is_indexed) {
gs_cspace_indexed_lookup_bytes(penum->pcs, value, psrc);
} else {
psrc[0] = value;
}
/* Data is in psrc */
/* These silly transforms need to go away. ToDo. */
if (has_transfer) {
for (kk = 0; kk < num_des_comp; kk++) {
conc[kk] = gx_color_value_from_byte(psrc[kk]);
}
cmap_transfer(&(conc[0]), penum->pis, penum->dev);
for (kk = 0; kk < num_des_comp; kk++) {
psrc[kk] = gx_color_value_to_byte(conc[kk]);
}
}
memcpy(&(penum->color_cache->device_contone[k * num_des_comp]),
psrc, num_des_comp);
}
} else {
/* Indexing only. No CM, decode or transfer functions. */
if (penum->pcs->params.indexed.use_proc) {
/* Have to do the slow way */
for (k = 0; k < num_entries; k++) {
gs_cspace_indexed_lookup_bytes(penum->pcs, (float)k, psrc);
memcpy(&(penum->color_cache->device_contone[k * num_des_comp]),
psrc, num_des_comp);
}
} else {
/* Possible GC issue? In this case we just use the pointer
of the index table data directly for our cache */
gs_free_object(penum->memory, penum->color_cache->device_contone,
"image_init_color_cache(device_contone)");
penum->color_cache->device_contone =
(byte*) penum->pcs->params.indexed.lookup.table.data;
penum->color_cache->free_contone = false;
}
}
} else {
/* Need CM */
/* We need to worry about if the source is indexed and if we need
to decode first. Then we can apply CM. Create a temp buffer in
the source space and then transform it with one call */
temp_buffer = (byte*) gs_alloc_bytes(penum->memory,
num_entries * num_src_comp,
"image_init_color_cache");
if (need_decode) {
if (is_indexed) {
/* Decode and un-indexed */
for (k = 0; k < num_entries; k++) {
image_cache_decode(penum, k, &value, decode_scale);
gs_cspace_indexed_lookup_bytes(penum->pcs, value, psrc);
memcpy(&(temp_buffer[k * num_src_comp]), psrc, num_src_comp);
}
} else {
/* Decode only */
for (k = 0; k < num_entries; k++) {
image_cache_decode(penum, k, &(temp_buffer[k]), decode_scale);
}
}
} else {
/* No Decode */
if (is_indexed) {
/* If index uses a table then just use its pointer */
if (penum->pcs->params.indexed.use_proc) {
/* Have to do the slow way */
for (k = 0; k < num_entries; k++) {
gs_cspace_indexed_lookup_bytes(penum->pcs, (float)k, psrc);
memcpy(&(temp_buffer[k * num_src_comp]), psrc, num_src_comp);
}
} else {
/* Use the index table directly. */
gs_free_object(penum->memory, temp_buffer, "image_init_color_cache");
free_temp_buffer = false;
temp_buffer = penum->pcs->params.indexed.lookup.table.data;
}
} else {
/* CM only */
for (k = 0; k < num_entries; k++) {
temp_buffer[k] = k;
}
}
}
/* Set up the buffer descriptors. */
gsicc_init_buffer(&input_buff_desc, num_src_comp, 1, false, false, false,
0, num_entries * num_src_comp, 1, num_entries);
gsicc_init_buffer(&output_buff_desc, num_des_comp, 1, false, false, false,
0, num_entries * num_des_comp,
1, num_entries);
(penum->icc_link->procs.map_buffer)(penum->dev, penum->icc_link,
&input_buff_desc, &output_buff_desc,
(void*) temp_buffer,
(void*) penum->color_cache->device_contone);
/* Check if we need to apply any transfer functions. If so then do it now */
if (has_transfer) {
for (k = 0; k < num_entries; k++) {
byte_ptr =
&(penum->color_cache->device_contone[k * num_des_comp]);
for (kk = 0; kk < num_des_comp; kk++) {
conc[kk] = gx_color_value_from_byte(byte_ptr[kk]);
}
cmap_transfer(&(conc[0]), penum->pis, penum->dev);
for (kk = 0; kk < num_des_comp; kk++) {
byte_ptr[kk] = gx_color_value_to_byte(conc[kk]);
}
}
}
if (free_temp_buffer)
gs_free_object(penum->memory, temp_buffer, "image_init_color_cache");
}
return 0;
}
/* Export this for use by image_render_ functions */
void
image_init_clues(gx_image_enum * penum, int bps, int spp)
{
/* Initialize the color table */
#define ictype(i)\
penum->clues[i].dev_color.type
switch ((spp == 1 ? bps : 8)) {
case 8: /* includes all color images */
{
register gx_image_clue *pcht = &penum->clues[0];
register int n = 64; /* 8 bits means 256 clues, do */
/* 4 at a time for efficiency */
do {
pcht[0].dev_color.type =
pcht[1].dev_color.type =
pcht[2].dev_color.type =
pcht[3].dev_color.type =
gx_dc_type_none;
pcht[0].key = pcht[1].key =
pcht[2].key = pcht[3].key = 0;
pcht += 4;
}
while (--n > 0);
penum->clues[0].key = 1; /* guarantee no hit */
break;
}
case 4:
ictype(17) = ictype(2 * 17) = ictype(3 * 17) =
ictype(4 * 17) = ictype(6 * 17) = ictype(7 * 17) =
ictype(8 * 17) = ictype(9 * 17) = ictype(11 * 17) =
ictype(12 * 17) = ictype(13 * 17) = ictype(14 * 17) =
gx_dc_type_none;
/* falls through */
case 2:
ictype(5 * 17) = ictype(10 * 17) = gx_dc_type_none;
#undef ictype
}
}
/* Initialize the color mapping tables for a non-mask image. */
static void
image_init_colors(gx_image_enum * penum, int bps, int spp,
gs_image_format_t format, const float *decode /*[spp*2] */ ,
const gs_imager_state * pis, gx_device * dev,
const gs_color_space * pcs, bool * pdcb)
{
int ci, decode_type;
static const float default_decode[] = {
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0
};
/* Clues are only used with image_mono_render */
if (spp == 1) {
image_init_clues(penum, bps, spp);
}
decode_type = 3; /* 0=custom, 1=identity, 2=inverted, 3=impossible */
for (ci = 0; ci < spp; ci +=2 ) {
decode_type &= (decode[ci] == 0. && decode[ci + 1] == 1.) |
(decode[ci] == 1. && decode[ci + 1] == 0.) << 1;
}
/* Initialize the maps from samples to intensities. */
for (ci = 0; ci < spp; ci++) {
sample_map *pmap = &penum->map[ci];
/* If the decoding is [0 1] or [1 0], we can fold it */
/* into the expansion of the sample values; */
/* otherwise, we have to use the floating point method. */
const float *this_decode = &decode[ci * 2];
const float *map_decode; /* decoding used to */
/* construct the expansion map */
const float *real_decode; /* decoding for expanded samples */
map_decode = real_decode = this_decode;
if (!(decode_type & 1)) {
if ((decode_type & 2) && bps <= 8) {
real_decode = default_decode;
} else {
*pdcb = false;
map_decode = default_decode;
}
}
if (bps > 2 || format != gs_image_format_chunky) {
if (bps <= 8)
image_init_map(&pmap->table.lookup8[0], 1 << bps,
map_decode);
} else { /* The map index encompasses more than one pixel. */
byte map[4];
register int i;
image_init_map(&map[0], 1 << bps, map_decode);
switch (bps) {
case 1:
{
register bits32 *p = &pmap->table.lookup4x1to32[0];
if (map[0] == 0 && map[1] == 0xff)
memcpy((byte *) p, lookup4x1to32_identity, 16 * 4);
else if (map[0] == 0xff && map[1] == 0)
memcpy((byte *) p, lookup4x1to32_inverted, 16 * 4);
else
for (i = 0; i < 16; i++, p++)
((byte *) p)[0] = map[i >> 3],
((byte *) p)[1] = map[(i >> 2) & 1],
((byte *) p)[2] = map[(i >> 1) & 1],
((byte *) p)[3] = map[i & 1];
}
break;
case 2:
{
register bits16 *p = &pmap->table.lookup2x2to16[0];
for (i = 0; i < 16; i++, p++)
((byte *) p)[0] = map[i >> 2],
((byte *) p)[1] = map[i & 3];
}
break;
}
}
pmap->decode_base /* = decode_lookup[0] */ = real_decode[0];
pmap->decode_factor =
(real_decode[1] - real_decode[0]) /
(bps <= 8 ? 255.0 : (float)frac_1);
pmap->decode_max /* = decode_lookup[15] */ = real_decode[1];
if (decode_type) {
pmap->decoding = sd_none;
pmap->inverted = map_decode[0] != 0;
} else if (bps <= 4) {
int step = 15 / ((1 << bps) - 1);
int i;
pmap->decoding = sd_lookup;
for (i = 15 - step; i > 0; i -= step)
pmap->decode_lookup[i] = pmap->decode_base +
i * (255.0 / 15) * pmap->decode_factor;
} else
pmap->decoding = sd_compute;
if (spp == 1) { /* and ci == 0 *//* Pre-map entries 0 and 255. */
gs_client_color cc;
/* Image clues are used in this case */
cc.paint.values[0] = real_decode[0];
(*pcs->type->remap_color) (&cc, pcs, penum->icolor0,
pis, dev, gs_color_select_source);
cc.paint.values[0] = real_decode[1];
(*pcs->type->remap_color) (&cc, pcs, penum->icolor1,
pis, dev, gs_color_select_source);
}
}
}
/* Construct a mapping table for sample values. */
/* map_size is 2, 4, 16, or 256. Note that 255 % (map_size - 1) == 0, */
/* so the division 0xffffL / (map_size - 1) is always exact. */
static void
image_init_map(byte * map, int map_size, const float *decode)
{
float min_v = decode[0];
float diff_v = decode[1] - min_v;
if (diff_v == 1 || diff_v == -1) { /* We can do the stepping with integers, without overflow. */
byte *limit = map + map_size;
uint value = (uint)(min_v * 0xffffL);
int diff = (int)(diff_v * (0xffffL / (map_size - 1)));
for (; map != limit; map++, value += diff)
*map = value >> 8;
} else { /* Step in floating point, with clamping. */
int i;
for (i = 0; i < map_size; ++i) {
int value = (int)((min_v + diff_v * i / (map_size - 1)) * 255);
map[i] = (value < 0 ? 0 : value > 255 ? 255 : value);
}
}
}
/*
* Scale a pair of mask_color values to match the scaling of each sample to
* a full byte, and complement and swap them if the map incorporates
* a Decode = [1 0] inversion.
*/
void
gx_image_scale_mask_colors(gx_image_enum *penum, int component_index)
{
uint scale = 255 / ((1 << penum->bps) - 1);
uint *values = &penum->mask_color.values[component_index * 2];
uint v0 = values[0] *= scale;
uint v1 = values[1] *= scale;
if (penum->map[component_index].decoding == sd_none &&
penum->map[component_index].inverted
) {
values[0] = 255 - v1;
values[1] = 255 - v0;
}
}
/* Used to indicate for ICC procesing if we have decoding to do */
bool
gx_has_transfer(const gs_imager_state *pis, int num_comps)
{
int k;
for (k = 0; k < num_comps; k++) {
if (pis->effective_transfer[k]->proc != gs_identity_transfer) {
return(true);
}
}
return(false);
}
|