157 #include "allheaders.h" 161 static l_int32 var_NEUTRAL_BOOST_VAL = 180;
164 #ifndef NO_CONSOLE_IO 165 #define DEBUG_CONVERT_TO_COLORMAP 0 166 #define DEBUG_UNROLLING 0 212 PROCNAME(
"pixThreshold8");
215 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
216 if (pixGetDepth(pixs) != 8)
217 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
219 return (
PIX *)ERROR_PTR(
"nlevels must be at least 2", procName, NULL);
239 return (
PIX *)ERROR_PTR(
"d must be in {1,2,4,8}", procName, NULL);
243 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
244 pixCopyInputFormat(pixd, pixs);
276 PROCNAME(
"pixRemoveColormapGeneral");
279 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
281 return (
PIX *)ERROR_PTR(
"invalid value for ifnocmap", procName, NULL);
283 if (pixGetColormap(pixs))
325 l_int32 sval, rval, gval, bval, val0, val1;
326 l_int32 i, j, k, w, h, d, wpls, wpld, ncolors, count;
327 l_int32 opaque, colorfound, blackwhite;
328 l_int32 *rmap, *gmap, *bmap, *amap;
329 l_uint32 *datas, *lines, *datad, *lined, *lut, *graymap;
330 l_uint32 sword, dword;
334 PROCNAME(
"pixRemoveColormap");
337 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
338 if ((cmap = pixGetColormap(pixs)) == NULL)
346 L_WARNING(
"Invalid type; converting based on src\n", procName);
351 if (d != 1 && d != 2 && d != 4 && d != 8)
352 return (
PIX *)ERROR_PTR(
"pixs must be {1,2,4,8} bpp", procName, NULL);
355 return (
PIX *)ERROR_PTR(
"colormap arrays not made", procName, NULL);
358 L_WARNING(
"not 1 bpp; can't remove cmap to binary\n", procName);
369 }
else if (colorfound) {
372 if (d == 1 && blackwhite)
381 wpls = pixGetWpl(pixs);
383 if ((pixd =
pixCopy(NULL, pixs)) == NULL) {
384 L_ERROR(
"pixd not made\n", procName);
388 val0 = rval + gval + bval;
390 val1 = rval + gval + bval;
395 if ((pixd =
pixCreate(w, h, 8)) == NULL) {
396 L_ERROR(
"pixd not made\n", procName);
399 pixCopyResolution(pixd, pixs);
400 pixCopyInputFormat(pixd, pixs);
402 wpld = pixGetWpl(pixd);
403 graymap = (l_uint32 *)LEPT_CALLOC(ncolors,
sizeof(l_int32));
409 for (i = 0; i < h; i++) {
410 lines = datas + i * wpls;
411 lined = datad + i * wpld;
416 for (j = 0, count = 0; j + 3 < w; j += 4, count++) {
417 sword = lines[count];
418 dword = (graymap[(sword >> 24) & 0xff] << 24) |
419 (graymap[(sword >> 16) & 0xff] << 16) |
420 (graymap[(sword >> 8) & 0xff] << 8) |
421 graymap[sword & 0xff];
422 lined[count] = dword;
427 gval = graymap[sval];
431 #define CHECK_VALUE(a, b, c) if (GET_DATA_BYTE(a, b) != c) { \ 432 fprintf(stderr, "Error: mismatch at %d, %d vs %d\n", \ 433 j, GET_DATA_BYTE(a, b), c); } 434 for (j = 0; j < w; j++) {
436 gval = graymap[sval];
437 CHECK_VALUE(lined, j, gval);
443 for (j = 0, count = 0; j + 7 < w; j += 8, count++) {
444 sword = lines[count];
445 dword = (graymap[(sword >> 28) & 0xf] << 24) |
446 (graymap[(sword >> 24) & 0xf] << 16) |
447 (graymap[(sword >> 20) & 0xf] << 8) |
448 graymap[(sword >> 16) & 0xf];
449 lined[2 * count] = dword;
450 dword = (graymap[(sword >> 12) & 0xf] << 24) |
451 (graymap[(sword >> 8) & 0xf] << 16) |
452 (graymap[(sword >> 4) & 0xf] << 8) |
453 graymap[sword & 0xf];
454 lined[2 * count + 1] = dword;
459 gval = graymap[sval];
463 for (j = 0; j < w; j++) {
465 gval = graymap[sval];
466 CHECK_VALUE(lined, j, gval);
472 for (j = 0, count = 0; j + 15 < w; j += 16, count++) {
473 sword = lines[count];
474 dword = (graymap[(sword >> 30) & 0x3] << 24) |
475 (graymap[(sword >> 28) & 0x3] << 16) |
476 (graymap[(sword >> 26) & 0x3] << 8) |
477 graymap[(sword >> 24) & 0x3];
478 lined[4 * count] = dword;
479 dword = (graymap[(sword >> 22) & 0x3] << 24) |
480 (graymap[(sword >> 20) & 0x3] << 16) |
481 (graymap[(sword >> 18) & 0x3] << 8) |
482 graymap[(sword >> 16) & 0x3];
483 lined[4 * count + 1] = dword;
484 dword = (graymap[(sword >> 14) & 0x3] << 24) |
485 (graymap[(sword >> 12) & 0x3] << 16) |
486 (graymap[(sword >> 10) & 0x3] << 8) |
487 graymap[(sword >> 8) & 0x3];
488 lined[4 * count + 2] = dword;
489 dword = (graymap[(sword >> 6) & 0x3] << 24) |
490 (graymap[(sword >> 4) & 0x3] << 16) |
491 (graymap[(sword >> 2) & 0x3] << 8) |
492 graymap[sword & 0x3];
493 lined[4 * count + 3] = dword;
498 gval = graymap[sval];
502 for (j = 0; j < w; j++) {
504 gval = graymap[sval];
505 CHECK_VALUE(lined, j, gval);
511 for (j = 0, count = 0; j + 31 < w; j += 32, count++) {
512 sword = lines[count];
513 for (k = 0; k < 4; k++) {
515 dword = (graymap[(sword >> 31) & 0x1] << 24) |
516 (graymap[(sword >> 30) & 0x1] << 16) |
517 (graymap[(sword >> 29) & 0x1] << 8) |
518 graymap[(sword >> 28) & 0x1];
519 lined[8 * count + 2 * k] = dword;
520 dword = (graymap[(sword >> 27) & 0x1] << 24) |
521 (graymap[(sword >> 26) & 0x1] << 16) |
522 (graymap[(sword >> 25) & 0x1] << 8) |
523 graymap[(sword >> 24) & 0x1];
524 lined[8 * count + 2 * k + 1] = dword;
531 gval = graymap[sval];
535 for (j = 0; j < w; j++) {
537 gval = graymap[sval];
538 CHECK_VALUE(lined, j, gval);
550 if ((pixd =
pixCreate(w, h, 32)) == NULL) {
551 L_ERROR(
"pixd not made\n", procName);
554 pixCopyInputFormat(pixd, pixs);
555 pixCopyResolution(pixd, pixs);
559 wpld = pixGetWpl(pixd);
560 lut = (l_uint32 *)LEPT_CALLOC(ncolors,
sizeof(l_uint32));
561 for (i = 0; i < ncolors; i++) {
568 for (i = 0; i < h; i++) {
569 lines = datas + i * wpls;
570 lined = datad + i * wpld;
571 for (j = 0; j < w; j++) {
581 L_WARNING(
"pixel value out of bounds\n", procName);
583 lined[j] = lut[sval];
617 PROCNAME(
"pixAddGrayColormap8");
619 if (!pixs || pixGetDepth(pixs) != 8)
620 return ERROR_INT(
"pixs not defined or not 8 bpp", procName, 1);
621 if (pixGetColormap(pixs))
646 l_int32 ncolors, w, h, i, j, wpl1, wpld, index, val;
647 l_int32 *inta, *revmap;
648 l_uint32 *data1, *datad, *line1, *lined;
652 PROCNAME(
"pixAddMinimalGrayColormap8");
654 if (!pixs || pixGetDepth(pixs) != 8)
655 return (
PIX *)ERROR_PTR(
"pixs undefined or not 8 bpp", procName, NULL);
659 cmap = pixGetColormap(pixs);
666 if (ncolors == 256) {
677 wpl1 = pixGetWpl(pix1);
678 inta = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
679 for (i = 0; i < h; i++) {
680 line1 = data1 + i * wpl1;
681 for (j = 0; j < w; j++) {
687 revmap = (l_int32 *)LEPT_CALLOC(256,
sizeof(l_int32));
688 for (i = 0, index = 0; i < 256; i++) {
698 pixCopyInputFormat(pixd, pixs);
699 pixCopyResolution(pixd, pixs);
701 wpld = pixGetWpl(pixd);
702 for (i = 0; i < h; i++) {
703 line1 = data1 + i * wpl1;
704 lined = datad + i * wpld;
705 for (j = 0; j < w; j++) {
758 l_int32 i, j, w, h, wpls, wpld, val;
760 l_uint32 *datas, *lines, *datad, *lined;
764 PROCNAME(
"pixConvertRGBToGray");
767 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
768 if (pixGetDepth(pixs) != 32)
769 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
770 if (rwt < 0.0 || gwt < 0.0 || bwt < 0.0)
771 return (
PIX *)ERROR_PTR(
"weights not all >= 0.0", procName, NULL);
775 if (rwt == 0.0 && gwt == 0.0 && bwt == 0.0) {
780 sum = rwt + gwt + bwt;
781 if (L_ABS(sum - 1.0) > 0.0001) {
782 L_WARNING(
"weights don't sum to 1; maintaining ratios\n", procName);
790 wpls = pixGetWpl(pixs);
792 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
793 pixCopyResolution(pixd, pixs);
794 pixCopyInputFormat(pixd, pixs);
796 wpld = pixGetWpl(pixd);
798 for (i = 0; i < h; i++) {
799 lines = datas + i * wpls;
800 lined = datad + i * wpld;
801 for (j = 0; j < w; j++) {
803 val = (l_int32)(rwt * ((word >> L_RED_SHIFT) & 0xff) +
804 gwt * ((word >> L_GREEN_SHIFT) & 0xff) +
805 bwt * ((word >> L_BLUE_SHIFT) & 0xff) + 0.5);
833 l_int32 i, j, w, h, wpls, wpld, val;
834 l_uint32 *datas, *lines, *datad, *lined;
837 PROCNAME(
"pixConvertRGBToGrayFast");
840 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
841 if (pixGetDepth(pixs) != 32)
842 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
846 wpls = pixGetWpl(pixs);
848 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
849 pixCopyResolution(pixd, pixs);
850 pixCopyInputFormat(pixd, pixs);
852 wpld = pixGetWpl(pixd);
854 for (i = 0; i < h; i++) {
855 lines = datas + i * wpls;
856 lined = datad + i * wpld;
857 for (j = 0; j < w; j++, lines++) {
858 val = ((*lines) >> L_GREEN_SHIFT) & 0xff;
890 l_int32 i, j, w, h, wpls, wpld, rval, gval, bval, val, minval, maxval;
891 l_uint32 *datas, *lines, *datad, *lined;
894 PROCNAME(
"pixConvertRGBToGrayMinMax");
897 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
898 if (pixGetDepth(pixs) != 32)
899 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
900 if (type != L_CHOOSE_MIN && type != L_CHOOSE_MAX &&
901 type != L_CHOOSE_MAXDIFF && type != L_CHOOSE_MIN_BOOST &&
902 type != L_CHOOSE_MAX_BOOST)
903 return (
PIX *)ERROR_PTR(
"invalid type", procName, NULL);
907 wpls = pixGetWpl(pixs);
909 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
910 pixCopyResolution(pixd, pixs);
911 pixCopyInputFormat(pixd, pixs);
913 wpld = pixGetWpl(pixd);
915 for (i = 0; i < h; i++) {
916 lines = datas + i * wpls;
917 lined = datad + i * wpld;
918 for (j = 0; j < w; j++) {
920 if (type == L_CHOOSE_MIN || type == L_CHOOSE_MIN_BOOST) {
921 val = L_MIN(rval, gval);
922 val = L_MIN(val, bval);
923 if (type == L_CHOOSE_MIN_BOOST)
924 val = L_MIN(255, (val * val) / var_NEUTRAL_BOOST_VAL);
925 }
else if (type == L_CHOOSE_MAX || type == L_CHOOSE_MAX_BOOST) {
926 val = L_MAX(rval, gval);
927 val = L_MAX(val, bval);
928 if (type == L_CHOOSE_MAX_BOOST)
929 val = L_MIN(255, (val * val) / var_NEUTRAL_BOOST_VAL);
931 minval = L_MIN(rval, gval);
932 minval = L_MIN(minval, bval);
933 maxval = L_MAX(rval, gval);
934 maxval = L_MAX(maxval, bval);
935 val = maxval - minval;
977 l_int32 w, h, d, i, j, wplt, wpld;
978 l_int32 rval, gval, bval, sval, minrg, maxrg, min, max, delta;
979 l_int32 fullsat, newval;
980 l_float32 *invmax, *ratio;
981 l_uint32 *linet, *lined, *datat, *datad;
984 PROCNAME(
"pixConvertRGBToGraySatBoost");
987 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
989 if (d != 32 && !pixGetColormap(pixs))
990 return (
PIX *)ERROR_PTR(
"pixs not cmapped or rgb", procName, NULL);
991 if (refval < 1 || refval > 255)
992 return (
PIX *)ERROR_PTR(
"refval not in [1 ... 255]", procName, NULL);
996 pixCopyResolution(pixd, pixs);
997 pixCopyInputFormat(pixd, pixs);
998 wplt = pixGetWpl(pixt);
1000 wpld = pixGetWpl(pixd);
1002 invmax = (l_float32 *)LEPT_CALLOC(256,
sizeof(l_float32));
1003 ratio = (l_float32 *)LEPT_CALLOC(256,
sizeof(l_float32));
1004 for (i = 1; i < 256; i++) {
1005 invmax[i] = 1.0 / (l_float32)i;
1006 ratio[i] = (l_float32)i / (l_float32)refval;
1008 for (i = 0; i < h; i++) {
1009 linet = datat + i * wplt;
1010 lined = datad + i * wpld;
1011 for (j = 0; j < w; j++) {
1013 minrg = L_MIN(rval, gval);
1014 min = L_MIN(minrg, bval);
1015 maxrg = L_MAX(rval, gval);
1016 max = L_MAX(maxrg, bval);
1021 sval = (l_int32)(255. * (l_float32)delta * invmax[max] + 0.5);
1023 fullsat = L_MIN(255, 255 * ratio[max]);
1024 newval = (sval * fullsat + (255 - sval) * max) / 255;
1057 l_int32 i, j, w, h, wpls, wpld, rval, gval, bval, val;
1058 l_uint32 *datas, *lines, *datad, *lined;
1061 PROCNAME(
"pixConvertRGBToGrayArb");
1064 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1065 if (pixGetDepth(pixs) != 32)
1066 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
1067 if (rc <= 0 && gc <= 0 && bc <= 0)
1068 return (
PIX *)ERROR_PTR(
"all coefficients <= 0", procName, NULL);
1072 wpls = pixGetWpl(pixs);
1073 if ((pixd =
pixCreate(w, h, 8)) == NULL)
1074 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1075 pixCopyResolution(pixd, pixs);
1076 pixCopyInputFormat(pixd, pixs);
1078 wpld = pixGetWpl(pixd);
1080 for (i = 0; i < h; i++) {
1081 lines = datas + i * wpls;
1082 lined = datad + i * wpld;
1083 for (j = 0; j < w; j++) {
1085 val = (l_int32)(rc * rval + gc * gval + bc * bval);
1086 val = L_MIN(255, L_MAX(0, val));
1124 PROCNAME(
"pixConvertRGBToBinaryArb");
1126 if (!pixs || pixGetDepth(pixs) != 32)
1127 return (
PIX *)ERROR_PTR(
"pixs undefined or not 32 bpp", procName, NULL);
1128 if (rc <= 0 && gc <= 0 && bc <= 0)
1129 return (
PIX *)ERROR_PTR(
"all coefficients <= 0", procName, NULL);
1132 return (
PIX *)ERROR_PTR(
"invalid relation", procName, NULL);
1136 thresh : thresh + 1;
1177 PROCNAME(
"pixConvertGrayToColormap");
1180 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1181 d = pixGetDepth(pixs);
1182 if (d != 2 && d != 4 && d != 8)
1183 return (
PIX *)ERROR_PTR(
"pixs not 2, 4 or 8 bpp", procName, NULL);
1185 if (pixGetColormap(pixs)) {
1186 L_INFO(
"pixs already has a colormap\n", procName);
1198 pixCopyInputFormat(pixd, pixs);
1229 l_int32 ncolors, w, h, depth, i, j, wpls, wpld;
1230 l_int32 index, num, val, newval;
1232 l_uint32 *lines, *lined, *datas, *datad;
1237 PROCNAME(
"pixConvertGrayToColormap8");
1240 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1241 if (pixGetDepth(pixs) != 8)
1242 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
1243 if (mindepth != 2 && mindepth != 4 && mindepth != 8) {
1244 L_WARNING(
"invalid value of mindepth; setting to 8\n", procName);
1248 if (pixGetColormap(pixs)) {
1249 L_INFO(
"pixs already has a colormap\n", procName);
1255 if (mindepth == 8 || ncolors > 16)
1257 else if (mindepth == 4 || ncolors > 4)
1266 pixCopyInputFormat(pixd, pixs);
1267 pixCopyResolution(pixd, pixs);
1270 for (i = 0; i < 256; i++) {
1281 wpls = pixGetWpl(pixs);
1283 wpld = pixGetWpl(pixd);
1284 for (i = 0; i < h; i++) {
1285 lines = datas + i * wpls;
1286 lined = datad + i * wpld;
1287 for (j = 0; j < w; j++) {
1289 newval = array[val];
1292 else if (depth == 4)
1327 l_int32 i, j, w, h, wplt, wpld, val8;
1328 l_uint32 *datad, *datat, *lined, *linet, *tab;
1332 PROCNAME(
"pixColorizeGray");
1335 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1336 if (pixGetDepth(pixs) != 8 && !pixGetColormap(pixs))
1337 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp or cmapped", procName, NULL);
1339 if (pixGetColormap(pixs))
1356 pixCopyResolution(pixd, pixs);
1357 pixCopyInputFormat(pixd, pixs);
1359 wpld = pixGetWpl(pixd);
1361 wplt = pixGetWpl(pixt);
1362 for (i = 0; i < h; i++) {
1363 lined = datad + i * wpld;
1364 linet = datat + i * wplt;
1365 for (j = 0; j < w; j++) {
1367 lined[j] = tab[val8];
1419 PROCNAME(
"pixConvertRGBToColormap");
1422 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1423 if (pixGetDepth(pixs) != 32)
1424 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
1425 if (pixGetSpp(pixs) == 4)
1426 L_WARNING(
"pixs has alpha; removing\n", procName);
1438 if (ncolors > 256) {
1441 L_INFO(
"More than 256 colors; using octree quant with dithering\n",
1444 L_INFO(
"More than 256 colors; using octree quant; no dithering\n",
1452 pixCopyInputFormat(pixd, pixs);
1479 l_int32 i, j, nc, w, h, imin, imax, factor, wpl1, wpld;
1480 l_int32 index, rmin, gmin, bmin, rmax, gmax, bmax, dmin, dmax;
1481 l_float32 minfract, ifract;
1483 l_uint32 *line1, *lined, *data1, *datad;
1488 PROCNAME(
"pixConvertCmapTo1");
1491 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1492 if ((cmap = pixGetColormap(pixs)) == NULL)
1493 return (
PIX *)ERROR_PTR(
"no colormap", procName, NULL);
1506 if ((lut = (l_int32 *)LEPT_CALLOC(nc,
sizeof(l_int32))) == NULL)
1507 return (
PIX *)ERROR_PTR(
"calloc fail for lut", procName, NULL);
1509 factor = L_MAX(1, (l_int32)sqrt((l_float64)(w * h) / 50000. + 0.5));
1513 for (i = 0; i < nc; i++) {
1530 wpl1 = pixGetWpl(pix1);
1531 wpld = pixGetWpl(pixd);
1532 for (i = 0; i < h; i++) {
1533 line1 = data1 + i * wpl1;
1534 lined = datad + i * wpld;
1535 for (j = 0; j < w; j++) {
1545 if (minfract > 0.5) {
1546 L_INFO(
"minfract = %5.3f; inverting\n", procName, minfract);
1590 l_int32 mingraycolors,
1594 l_int32 d, ncolors, iscolor, graycolors;
1597 PROCNAME(
"pixQuantizeIfFewColors");
1600 return ERROR_INT(
"&pixd not defined", procName, 1);
1603 return ERROR_INT(
"pixs not defined", procName, 1);
1604 d = pixGetDepth(pixs);
1605 if (d != 8 && d != 32)
1606 return ERROR_INT(
"pixs not defined", procName, 1);
1607 if (pixGetColormap(pixs) != NULL) {
1614 L_WARNING(
"maxcolors > 50; very large!\n", procName);
1615 if (mingraycolors <= 0)
1617 if (mingraycolors > 30)
1618 L_WARNING(
"mingraycolors > 30; very large!\n", procName);
1619 if (octlevel != 3 && octlevel != 4) {
1620 L_WARNING(
"invalid octlevel; setting to 3\n", procName);
1627 if (ncolors > maxcolors)
1628 return ERROR_INT(
"too many colors", procName, 1);
1645 L_WARNING(
"quantized at level 2; low quality\n", procName);
1652 graycolors = L_MAX(mingraycolors, (l_int32)(1.5 * ncolors));
1653 graycolors = L_MIN(graycolors, 256);
1654 if (graycolors < 16)
1663 return ERROR_INT(
"pixd not made", procName, 1);
1664 pixCopyInputFormat(pixd, pixs);
1693 l_int32 w, h, wpls, wpld, i, j, val, use_lsb;
1694 l_uint32 sword, first, second;
1695 l_uint32 *datas, *datad, *lines, *lined;
1698 PROCNAME(
"pixConvert16To8");
1701 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1702 if (pixGetDepth(pixs) != 16)
1703 return (
PIX *)ERROR_PTR(
"pixs not 16 bpp", procName, NULL);
1706 return (
PIX *)ERROR_PTR(
"invalid type", procName, NULL);
1709 if ((pixd =
pixCreate(w, h, 8)) == NULL)
1710 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1711 pixCopyInputFormat(pixd, pixs);
1712 pixCopyResolution(pixd, pixs);
1713 wpls = pixGetWpl(pixs);
1715 wpld = pixGetWpl(pixd);
1720 for (i = 0; i < h; i++) {
1721 lines = datas + i * wpls;
1722 for (j = 0; j < wpls; j++) {
1729 if (!use_lsb)
break;
1735 for (i = 0; i < h; i++) {
1736 lines = datas + i * wpls;
1737 lined = datad + i * wpld;
1739 for (j = 0; j < wpls; j++) {
1740 sword = *(lines + j);
1741 dword = ((sword >> 8) & 0xff00) | (sword & 0xff);
1745 for (j = 0; j < wpls; j++) {
1746 sword = *(lines + j);
1747 dword = ((sword >> 16) & 0xff00) | ((sword >> 8) & 0xff);
1751 for (j = 0; j < wpls; j++) {
1752 sword = *(lines + j);
1753 first = (sword >> 24) ? 255 : ((sword >> 16) & 0xff);
1754 second = ((sword >> 8) & 0xff) ? 255 : (sword & 0xff);
1755 dword = (first << 8) | second;
1789 l_int32 d, i, rval, bval, gval;
1791 l_float32 invgamma, x;
1795 PROCNAME(
"pixConvertGrayToFalseColor");
1798 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1799 d = pixGetDepth(pixs);
1800 if (d != 8 && d != 16)
1801 return (
PIX *)ERROR_PTR(
"pixs not 8 or 16 bpp", procName, NULL);
1806 if (pixGetColormap(pixs))
1812 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1815 pixCopyResolution(pixd, pixs);
1816 pixCopyInputFormat(pixd, pixs);
1819 curve = (l_int32 *)LEPT_CALLOC(64,
sizeof(l_int32));
1820 if (gamma == 0.0) gamma = 1.0;
1821 invgamma = 1. / gamma;
1822 for (i = 0; i < 64; i++) {
1823 x = (l_float32)i / 64.;
1824 curve[i] = (l_int32)(255. * powf(x, invgamma) + 0.5);
1827 for (i = 0; i < 256; i++) {
1831 bval = curve[i + 32];
1832 }
else if (i < 96) {
1834 gval = curve[i - 32];
1836 }
else if (i < 160) {
1837 rval = curve[i - 96];
1839 bval = curve[159 - i];
1840 }
else if (i < 224) {
1842 gval = curve[223 - i];
1845 rval = curve[287 - i];
1884 PROCNAME(
"pixUnpackBinary");
1887 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1888 if (pixGetDepth(pixs) != 1)
1889 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
1890 if (depth != 2 && depth != 4 && depth != 8 && depth != 16 && depth != 32)
1891 return (
PIX *)ERROR_PTR(
"depth not 2, 4, 8, 16 or 32 bpp",
1899 }
else if (depth == 4) {
1904 }
else if (depth == 8) {
1909 }
else if (depth == 16) {
1921 pixCopyInputFormat(pixd, pixs);
1948 l_int32 w, h, i, j, dibit, ndibits, wpls, wpld;
1951 l_uint32 *tab, *datas, *datad, *lines, *lined;
1953 PROCNAME(
"pixConvert1To16");
1956 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
1957 if (pixGetDepth(pixs) != 1)
1958 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
1962 if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd))
1963 return (
PIX *)ERROR_PTR(
"pix sizes unequal", procName, pixd);
1964 if (pixGetDepth(pixd) != 16)
1965 return (
PIX *)ERROR_PTR(
"pixd not 16 bpp", procName, pixd);
1967 if ((pixd =
pixCreate(w, h, 16)) == NULL)
1968 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
1970 pixCopyResolution(pixd, pixs);
1971 pixCopyInputFormat(pixd, pixs);
1974 tab = (l_uint32 *)LEPT_CALLOC(4,
sizeof(l_uint32));
1977 for (index = 0; index < 4; index++) {
1978 tab[index] = (val[(index >> 1) & 1] << 16) | val[index & 1];
1982 wpls = pixGetWpl(pixs);
1984 wpld = pixGetWpl(pixd);
1985 ndibits = (w + 1) / 2;
1986 for (i = 0; i < h; i++) {
1987 lines = datas + i * wpls;
1988 lined = datad + i * wpld;
1989 for (j = 0; j < ndibits; j++) {
1991 lined[j] = tab[dibit];
2022 l_int32 w, h, i, j, wpls, wpld, bit;
2024 l_uint32 *datas, *datad, *lines, *lined;
2026 PROCNAME(
"pixConvert1To32");
2029 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2030 if (pixGetDepth(pixs) != 1)
2031 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
2035 if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd))
2036 return (
PIX *)ERROR_PTR(
"pix sizes unequal", procName, pixd);
2037 if (pixGetDepth(pixd) != 32)
2038 return (
PIX *)ERROR_PTR(
"pixd not 32 bpp", procName, pixd);
2040 if ((pixd =
pixCreate(w, h, 32)) == NULL)
2041 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2043 pixCopyResolution(pixd, pixs);
2044 pixCopyInputFormat(pixd, pixs);
2049 wpls = pixGetWpl(pixs);
2051 wpld = pixGetWpl(pixd);
2052 for (i = 0; i < h; i++) {
2053 lines = datas + i * wpls;
2054 lined = datad + i * wpld;
2055 for (j = 0; j <w; j++) {
2057 lined[j] = val[bit];
2085 PROCNAME(
"pixConvert1To2Cmap");
2088 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2089 if (pixGetDepth(pixs) != 1)
2090 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
2093 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2098 pixCopyInputFormat(pixd, pixs);
2128 l_int32 w, h, i, j, byteval,
nbytes, wpls, wpld;
2132 l_uint32 *datas, *datad, *lines, *lined;
2134 PROCNAME(
"pixConvert1To2");
2137 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, pixd);
2138 if (pixGetDepth(pixs) != 1)
2139 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, pixd);
2143 if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd))
2144 return (
PIX *)ERROR_PTR(
"pix sizes unequal", procName, pixd);
2145 if (pixGetDepth(pixd) != 2)
2146 return (
PIX *)ERROR_PTR(
"pixd not 2 bpp", procName, pixd);
2148 if ((pixd =
pixCreate(w, h, 2)) == NULL)
2149 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2151 pixCopyResolution(pixd, pixs);
2152 pixCopyInputFormat(pixd, pixs);
2155 tab = (l_uint16 *)LEPT_CALLOC(256,
sizeof(l_uint16));
2158 for (index = 0; index < 256; index++) {
2159 tab[index] = (val[(index >> 7) & 1] << 14) |
2160 (val[(index >> 6) & 1] << 12) |
2161 (val[(index >> 5) & 1] << 10) |
2162 (val[(index >> 4) & 1] << 8) |
2163 (val[(index >> 3) & 1] << 6) |
2164 (val[(index >> 2) & 1] << 4) |
2165 (val[(index >> 1) & 1] << 2) | val[index & 1];
2169 wpls = pixGetWpl(pixs);
2171 wpld = pixGetWpl(pixd);
2173 for (i = 0; i < h; i++) {
2174 lines = datas + i * wpls;
2175 lined = datad + i * wpld;
2176 for (j = 0; j <
nbytes; j++) {
2207 PROCNAME(
"pixConvert1To4Cmap");
2210 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2211 if (pixGetDepth(pixs) != 1)
2212 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
2215 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2220 pixCopyInputFormat(pixd, pixs);
2250 l_int32 w, h, i, j, byteval,
nbytes, wpls, wpld;
2253 l_uint32 *tab, *datas, *datad, *lines, *lined;
2255 PROCNAME(
"pixConvert1To4");
2258 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, pixd);
2259 if (pixGetDepth(pixs) != 1)
2260 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, pixd);
2264 if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd))
2265 return (
PIX *)ERROR_PTR(
"pix sizes unequal", procName, pixd);
2266 if (pixGetDepth(pixd) != 4)
2267 return (
PIX *)ERROR_PTR(
"pixd not 4 bpp", procName, pixd);
2269 if ((pixd =
pixCreate(w, h, 4)) == NULL)
2270 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2272 pixCopyResolution(pixd, pixs);
2273 pixCopyInputFormat(pixd, pixs);
2276 tab = (l_uint32 *)LEPT_CALLOC(256,
sizeof(l_uint32));
2279 for (index = 0; index < 256; index++) {
2280 tab[index] = (val[(index >> 7) & 1] << 28) |
2281 (val[(index >> 6) & 1] << 24) |
2282 (val[(index >> 5) & 1] << 20) |
2283 (val[(index >> 4) & 1] << 16) |
2284 (val[(index >> 3) & 1] << 12) |
2285 (val[(index >> 2) & 1] << 8) |
2286 (val[(index >> 1) & 1] << 4) | val[index & 1];
2290 wpls = pixGetWpl(pixs);
2292 wpld = pixGetWpl(pixd);
2294 for (i = 0; i < h; i++) {
2295 lines = datas + i * wpls;
2296 lined = datad + i * wpld;
2297 for (j = 0; j <
nbytes; j++) {
2299 lined[j] = tab[byteval];
2328 PROCNAME(
"pixConvert1To8Cmap");
2331 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2332 if (pixGetDepth(pixs) != 1)
2333 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, NULL);
2336 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2341 pixCopyInputFormat(pixd, pixs);
2371 l_int32 w, h, i, j, qbit, nqbits, wpls, wpld;
2374 l_uint32 *tab, *datas, *datad, *lines, *lined;
2376 PROCNAME(
"pixConvert1To8");
2379 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, pixd);
2380 if (pixGetDepth(pixs) != 1)
2381 return (
PIX *)ERROR_PTR(
"pixs not 1 bpp", procName, pixd);
2385 if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd))
2386 return (
PIX *)ERROR_PTR(
"pix sizes unequal", procName, pixd);
2387 if (pixGetDepth(pixd) != 8)
2388 return (
PIX *)ERROR_PTR(
"pixd not 8 bpp", procName, pixd);
2390 if ((pixd =
pixCreate(w, h, 8)) == NULL)
2391 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2393 pixCopyResolution(pixd, pixs);
2394 pixCopyInputFormat(pixd, pixs);
2397 tab = (l_uint32 *)LEPT_CALLOC(16,
sizeof(l_uint32));
2400 for (index = 0; index < 16; index++) {
2401 tab[index] = ((l_uint32)val[(index >> 3) & 1] << 24) |
2402 (val[(index >> 2) & 1] << 16) |
2403 (val[(index >> 1) & 1] << 8) | val[index & 1];
2407 wpls = pixGetWpl(pixs);
2409 wpld = pixGetWpl(pixd);
2410 nqbits = (w + 3) / 4;
2411 for (i = 0; i < h; i++) {
2412 lines = datas + i * wpls;
2413 lined = datad + i * wpld;
2414 for (j = 0; j < nqbits; j++) {
2416 lined[j] = tab[qbit];
2463 l_int32 w, h, i, j,
nbytes, wpls, wpld, dibit, byte;
2466 l_uint32 *tab, *datas, *datad, *lines, *lined;
2470 PROCNAME(
"pixConvert2To8");
2473 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2474 if (pixGetDepth(pixs) != 2)
2475 return (
PIX *)ERROR_PTR(
"pixs not 2 bpp", procName, NULL);
2477 cmaps = pixGetColormap(pixs);
2478 if (cmaps && cmapflag == FALSE)
2482 if ((pixd =
pixCreate(w, h, 8)) == NULL)
2483 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2484 pixCopyResolution(pixd, pixs);
2485 pixCopyInputFormat(pixd, pixs);
2487 wpls = pixGetWpl(pixs);
2489 wpld = pixGetWpl(pixd);
2491 if (cmapflag == TRUE) {
2502 for (i = 0; i < h; i++) {
2503 lines = datas + i * wpls;
2504 lined = datad + i * wpld;
2505 for (j = 0; j < w; j++) {
2516 tab = (l_uint32 *)LEPT_CALLOC(256,
sizeof(l_uint32));
2521 for (index = 0; index < 256; index++) {
2522 tab[index] = (val[(index >> 6) & 3] << 24) |
2523 (val[(index >> 4) & 3] << 16) |
2524 (val[(index >> 2) & 3] << 8) | val[index & 3];
2528 for (i = 0; i < h; i++) {
2529 lines = datas + i * wpls;
2530 lined = datad + i * wpld;
2531 for (j = 0; j <
nbytes; j++) {
2533 lined[j] = tab[byte];
2570 l_int32 w, h, i, j, wpls, wpld, byte, qbit;
2571 l_uint32 *datas, *datad, *lines, *lined;
2575 PROCNAME(
"pixConvert4To8");
2578 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2579 if (pixGetDepth(pixs) != 4)
2580 return (
PIX *)ERROR_PTR(
"pixs not 4 bpp", procName, NULL);
2582 cmaps = pixGetColormap(pixs);
2583 if (cmaps && cmapflag == FALSE)
2587 if ((pixd =
pixCreate(w, h, 8)) == NULL)
2588 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
2589 pixCopyResolution(pixd, pixs);
2590 pixCopyInputFormat(pixd, pixs);
2592 wpls = pixGetWpl(pixs);
2594 wpld = pixGetWpl(pixd);
2596 if (cmapflag == TRUE) {
2601 for (i = 0; i < 16; i++)
2605 for (i = 0; i < h; i++) {
2606 lines = datas + i * wpls;
2607 lined = datad + i * wpld;
2608 for (j = 0; j < w; j++) {
2618 for (i = 0; i < h; i++) {
2619 lines = datas + i * wpls;
2620 lined = datad + i * wpld;
2621 for (j = 0; j < w; j++) {
2623 byte = (qbit << 4) | qbit;
2655 l_int32 i, j, w, h, d, wplt, wpld, val;
2656 l_uint32 *datat, *datad, *linet, *lined;
2659 PROCNAME(
"pixConvert8To16");
2662 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2665 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
2666 if (leftshift < 0 || leftshift > 8)
2667 return (
PIX *)ERROR_PTR(
"leftshift not in [0 ... 8]", procName, NULL);
2669 if (pixGetColormap(pixs) != NULL)
2675 pixCopyResolution(pixd, pixs);
2676 pixCopyInputFormat(pixd, pixs);
2679 wplt = pixGetWpl(pixt);
2680 wpld = pixGetWpl(pixd);
2681 for (i = 0; i < h; i++) {
2682 linet = datat + i * wplt;
2683 lined = datad + i * wpld;
2684 for (j = 0; j < w; j++) {
2687 val = val | (val << leftshift);
2721 PIX *pix1, *pix2, *pix3, *pixd;
2723 PROCNAME(
"pixConvertTo2");
2726 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2727 d = pixGetDepth(pixs);
2728 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 32)
2729 return (
PIX *)ERROR_PTR(
"depth not {1,2,4,8,32}", procName, NULL);
2731 if (pixGetColormap(pixs) != NULL) {
2733 d = pixGetDepth(pix1);
2744 }
else if (d == 2) {
2746 }
else if (d == 4) {
2772 l_int32 i, j, w, h, wpls, wpld;
2774 l_uint32 *datas, *lines, *datad, *lined;
2777 PROCNAME(
"pixConvert8To2");
2779 if (!pix || pixGetDepth(pix) != 8)
2780 return (
PIX *)ERROR_PTR(
"pix undefined or not 8 bpp", procName, NULL);
2782 if (pixGetColormap(pix) != NULL)
2788 wpls = pixGetWpl(pixs);
2791 wpld = pixGetWpl(pixd);
2792 for (i = 0; i < h; i++) {
2793 lines = datas + i * wpls;
2794 lined = datad + i * wpld;
2795 for (j = 0; j < wpls; j++) {
2796 word = lines[j] & 0xc0c0c0c0;
2797 word = (word >> 24) | ((word & 0xff0000) >> 18) |
2798 ((word & 0xff00) >> 12) | ((word & 0xff) >> 6);
2829 PIX *pix1, *pix2, *pix3, *pixd;
2831 PROCNAME(
"pixConvertTo4");
2834 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2835 d = pixGetDepth(pixs);
2836 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 32)
2837 return (
PIX *)ERROR_PTR(
"depth not {1,2,4,8,32}", procName, NULL);
2839 if (pixGetColormap(pixs) != NULL) {
2841 d = pixGetDepth(pix1);
2852 }
else if (d == 2) {
2856 }
else if (d == 4) {
2880 l_int32 i, j, w, h, wpls, wpld, val;
2881 l_uint32 *datas, *lines, *datad, *lined;
2884 PROCNAME(
"pixConvert8To4");
2886 if (!pix || pixGetDepth(pix) != 8)
2887 return (
PIX *)ERROR_PTR(
"pix undefined or not 8 bpp", procName, NULL);
2889 if (pixGetColormap(pix) != NULL)
2895 wpls = pixGetWpl(pixs);
2898 wpld = pixGetWpl(pixd);
2899 for (i = 0; i < h; i++) {
2900 lines = datas + i * wpls;
2901 lined = datad + i * wpld;
2902 for (j = 0; j < w; j++) {
2936 l_int32 d, color0, color1, rval, gval, bval;
2940 PROCNAME(
"pixConvertTo1");
2943 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
2944 d = pixGetDepth(pixs);
2945 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 16 && d != 32)
2946 return (
PIX *)ERROR_PTR(
"depth not {1,2,4,8,16,32}", procName, NULL);
2948 cmap = pixGetColormap(pixs);
2955 color0 = rval + gval + bval;
2957 color1 = rval + gval + bval;
2960 if (color1 > color0)
2993 l_float32 scalefactor;
2996 PROCNAME(
"pixConvertTo1BySampling");
2999 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3001 return (
PIX *)ERROR_PTR(
"factor must be >= 1", procName, NULL);
3003 scalefactor = 1. / (l_float32)factor;
3048 PROCNAME(
"pixConvertTo8");
3051 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3052 d = pixGetDepth(pixs);
3053 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 16 && d != 32)
3054 return (
PIX *)ERROR_PTR(
"depth not {1,2,4,8,16,32}", procName, NULL);
3061 }
else if (d == 2) {
3063 }
else if (d == 4) {
3065 }
else if (d == 8) {
3066 cmap = pixGetColormap(pixs);
3067 if ((cmap && cmapflag) || (!cmap && !cmapflag)) {
3076 }
else if (d == 16) {
3109 l_float32 scalefactor;
3112 PROCNAME(
"pixConvertTo8BySampling");
3115 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3117 return (
PIX *)ERROR_PTR(
"factor must be >= 1", procName, NULL);
3119 scalefactor = 1. / (l_float32)factor;
3154 PROCNAME(
"pixConvertTo8Colormap");
3157 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3158 d = pixGetDepth(pixs);
3159 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 16 && d != 32)
3160 return (
PIX *)ERROR_PTR(
"depth not {1,2,4,8,16,32}", procName, NULL);
3188 PROCNAME(
"pixConvertTo16");
3191 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3193 d = pixGetDepth(pixs);
3199 return (
PIX *)ERROR_PTR(
"src depth not 1 or 8 bpp", procName, NULL);
3238 PROCNAME(
"pixConvertTo32");
3241 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3243 d = pixGetDepth(pixs);
3246 }
else if (d == 2) {
3251 }
else if (d == 4) {
3256 }
else if (d == 8) {
3258 }
else if (d == 16) {
3263 }
else if (d == 24) {
3265 }
else if (d == 32) {
3268 return (
PIX *)ERROR_PTR(
"depth not 1, 2, 4, 8, 16, 32 bpp",
3291 l_float32 scalefactor;
3294 PROCNAME(
"pixConvertTo32BySampling");
3297 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3299 return (
PIX *)ERROR_PTR(
"factor must be >= 1", procName, NULL);
3301 scalefactor = 1. / (l_float32)factor;
3325 l_int32 i, j, w, h, wpls, wpld, val;
3326 l_uint32 *datas, *datad, *lines, *lined;
3330 PROCNAME(
"pixConvert8To32");
3333 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3334 if (pixGetDepth(pixs) != 8)
3335 return (
PIX *)ERROR_PTR(
"pixs not 8 bpp", procName, NULL);
3337 if (pixGetColormap(pixs))
3342 wpls = pixGetWpl(pixs);
3343 if ((pixd =
pixCreate(w, h, 32)) == NULL)
3344 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
3345 pixCopyResolution(pixd, pixs);
3346 pixCopyInputFormat(pixd, pixs);
3348 wpld = pixGetWpl(pixd);
3351 tab = (l_uint32 *)LEPT_CALLOC(256,
sizeof(l_uint32));
3352 for (i = 0; i < 256; i++)
3353 tab[i] = (i << 24) | (i << 16) | (i << 8);
3356 for (i = 0; i < h; i++) {
3357 lines = datas + i * wpls;
3358 lined = datad + i * wpld;
3359 for (j = 0; j < w; j++) {
3361 lined[j] = tab[val];
3400 PROCNAME(
"pixConvertTo8Or32");
3403 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3405 return (
PIX *)ERROR_PTR(
"invalid copyflag", procName, NULL);
3407 d = pixGetDepth(pixs);
3408 if (pixGetColormap(pixs)) {
3409 if (warnflag) L_WARNING(
"pix has colormap; removing\n", procName);
3411 }
else if (d == 8 || d == 32) {
3421 d = pixGetDepth(pixd);
3422 if (d != 8 && d != 32) {
3424 return (
PIX *)ERROR_PTR(
"depth not 8 or 32 bpp", procName, NULL);
3459 l_int32 w, h, d, i, j, wpls, wpld, rval, gval, bval;
3461 l_uint32 *datas, *datad, *lined;
3464 PROCNAME(
"pixConvert24to32");
3467 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3470 return (
PIX *)ERROR_PTR(
"pixs not 24 bpp", procName, NULL);
3475 wpls = pixGetWpl(pixs);
3476 wpld = pixGetWpl(pixd);
3477 for (i = 0; i < h; i++) {
3478 lines = (l_uint8 *)(datas + i * wpls);
3479 lined = datad + i * wpld;
3480 for (j = 0; j < w; j++) {
3488 pixCopyResolution(pixd, pixs);
3489 pixCopyInputFormat(pixd, pixs);
3509 l_int32 w, h, d, i, j, wpls, wpld, rval, gval, bval;
3510 l_uint32 *datas, *lines, *rgbdata;
3513 PROCNAME(
"pixConvert32to24");
3516 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3519 return (
PIX *)ERROR_PTR(
"pixs not 32 bpp", procName, NULL);
3522 wpls = pixGetWpl(pixs);
3525 wpld = pixGetWpl(pixd);
3526 for (i = 0; i < h; i++) {
3527 lines = datas + i * wpls;
3528 rgbdata8 = (l_uint8 *)(rgbdata + i * wpld);
3529 for (j = 0; j < w; j++) {
3536 pixCopyResolution(pixd, pixs);
3537 pixCopyInputFormat(pixd, pixs);
3563 l_int32 w, h, i, j, wpls, wpld;
3565 l_uint32 *datas, *lines, *datad, *lined;
3568 PROCNAME(
"pixConvert32to16");
3570 if (!pixs || pixGetDepth(pixs) != 32)
3571 return (
PIX *)ERROR_PTR(
"pixs undefined or not 32 bpp", procName, NULL);
3574 return (
PIX *)ERROR_PTR(
"invalid type", procName, NULL);
3577 if ((pixd =
pixCreate(w, h, 16)) == NULL)
3578 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
3579 pixCopyResolution(pixd, pixs);
3580 pixCopyInputFormat(pixd, pixs);
3581 wpls = pixGetWpl(pixs);
3583 wpld = pixGetWpl(pixd);
3586 for (i = 0; i < h; i++) {
3587 lines = datas + i * wpls;
3588 lined = datad + i * wpld;
3590 for (j = 0; j < wpls; j++) {
3591 sword = *(lines + j);
3592 dword = sword & 0xffff;
3596 for (j = 0; j < wpls; j++) {
3597 sword = *(lines + j);
3598 dword = sword >> 16;
3602 for (j = 0; j < wpls; j++) {
3603 sword = *(lines + j);
3604 dword = (sword >> 16) ? 0xffff : (sword & 0xffff);
3629 PROCNAME(
"pixConvert32to8");
3631 if (!pixs || pixGetDepth(pixs) != 32)
3632 return (
PIX *)ERROR_PTR(
"pixs undefined or not 32 bpp", procName, NULL);
3635 return (
PIX *)ERROR_PTR(
"invalid type16", procName, NULL);
3637 return (
PIX *)ERROR_PTR(
"invalid type8", procName, NULL);
3664 PROCNAME(
"pixRemoveAlpha");
3667 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3669 if (pixGetDepth(pixs) == 32 && pixGetSpp(pixs) == 4)
3704 PROCNAME(
"pixAddAlphaTo1bpp");
3706 if (!pixs || (pixGetDepth(pixs) != 1))
3707 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
3708 if (pixd && (pixd != pixs))
3709 return (
PIX *)ERROR_PTR(
"pixd defined but != pixs", procName, NULL);
3744 l_int32 w, h, ds, wpls, wpld, i, j, val;
3745 l_uint32 *datas, *datad, *lines, *lined;
3748 PROCNAME(
"pixConvertLossless");
3751 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3752 if (pixGetColormap(pixs))
3753 return (
PIX *)ERROR_PTR(
"pixs has colormap", procName, NULL);
3754 if (d != 2 && d != 4 && d != 8)
3755 return (
PIX *)ERROR_PTR(
"invalid dest depth", procName, NULL);
3759 return (
PIX *)ERROR_PTR(
"depth > d", procName, NULL);
3763 if ((pixd =
pixCreate(w, h, d)) == NULL)
3764 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
3765 pixCopyResolution(pixd, pixs);
3766 pixCopyInputFormat(pixd, pixs);
3770 wpls = pixGetWpl(pixs);
3772 wpld = pixGetWpl(pixd);
3773 for (i = 0; i < h; i++) {
3774 lines = datas + i * wpls;
3775 lined = datad + i * wpld;
3779 for (j = 0; j < w; j++) {
3790 for (j = 0; j < w; j++) {
3799 for (j = 0; j < w; j++) {
3838 PROCNAME(
"pixConvertForPSWrap");
3841 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3843 cmap = pixGetColormap(pixs);
3844 d = pixGetDepth(pixs);
3870 fprintf(stderr,
"depth not in {1, 2, 4, 8, 16, 32}");
3920 PROCNAME(
"pixConvertToSubpixelRGB");
3923 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3924 d = pixGetDepth(pixs);
3925 cmap = pixGetColormap(pixs);
3926 if (d != 8 && d != 32 && !cmap)
3927 return (
PIX *)ERROR_PTR(
"pix not 8 or 32 bpp and not cmapped",
3929 if (scalex <= 0.0 || scaley <= 0.0)
3930 return (
PIX *)ERROR_PTR(
"scale factors must be > 0", procName, NULL);
3933 return (
PIX *)ERROR_PTR(
"invalid subpixel order", procName, NULL);
3935 return (
PIX *)ERROR_PTR(
"pix1 not made", procName, NULL);
3937 d = pixGetDepth(pix1);
3944 L_ERROR(
"invalid depth %d\n", procName, d);
3986 l_int32 w, h, d, wd, hd, wplt, wpld, i, j, rval, gval, bval, direction;
3987 l_uint32 *datat, *datad, *linet, *lined;
3988 PIX *pix1, *pix2, *pixd;
3991 PROCNAME(
"pixConvertGrayToSubpixelRGB");
3994 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
3995 d = pixGetDepth(pixs);
3996 cmap = pixGetColormap(pixs);
3997 if (d != 8 && !cmap)
3998 return (
PIX *)ERROR_PTR(
"pix not 8 bpp & not cmapped", procName, NULL);
3999 if (scalex <= 0.0 || scaley <= 0.0)
4000 return (
PIX *)ERROR_PTR(
"scale factors must be > 0", procName, NULL);
4003 return (
PIX *)ERROR_PTR(
"invalid subpixel order", procName, NULL);
4009 if (direction == L_HORIZ)
4010 pix2 =
pixScale(pix1, 3.0 * scalex, scaley);
4012 pix2 =
pixScale(pix1, scalex, 3.0 * scaley);
4015 wd = (direction == L_HORIZ) ? w / 3 : w;
4016 hd = (direction == L_VERT) ? h / 3 : h;
4019 wpld = pixGetWpl(pixd);
4021 wplt = pixGetWpl(pix2);
4022 if (direction == L_HORIZ) {
4023 for (i = 0; i < hd; i++) {
4024 linet = datat + i * wplt;
4025 lined = datad + i * wpld;
4026 for (j = 0; j < wd; j++) {
4037 for (i = 0; i < hd; i++) {
4038 linet = datat + 3 * i * wplt;
4039 lined = datad + i * wpld;
4040 for (j = 0; j < wd; j++) {
4091 l_int32 w, h, d, wd, hd, wplt, wpld, i, j, rval, gval, bval, direction;
4092 l_uint32 *datat, *datad, *linet, *lined;
4093 PIX *pix1, *pix2, *pixd;
4096 PROCNAME(
"pixConvertColorToSubpixelRGB");
4099 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
4100 d = pixGetDepth(pixs);
4101 cmap = pixGetColormap(pixs);
4102 if (d != 32 && !cmap)
4103 return (
PIX *)ERROR_PTR(
"pix not 32 bpp & not cmapped", procName, NULL);
4104 if (scalex <= 0.0 || scaley <= 0.0)
4105 return (
PIX *)ERROR_PTR(
"scale factors must be > 0", procName, NULL);
4108 return (
PIX *)ERROR_PTR(
"invalid subpixel order", procName, NULL);
4114 if (direction == L_HORIZ)
4115 pix2 =
pixScale(pix1, 3.0 * scalex, scaley);
4117 pix2 =
pixScale(pix1, scalex, 3.0 * scaley);
4120 wd = (direction == L_HORIZ) ? w / 3 : w;
4121 hd = (direction == L_VERT) ? h / 3 : h;
4123 pixCopyInputFormat(pixd, pixs);
4125 wpld = pixGetWpl(pixd);
4127 wplt = pixGetWpl(pix2);
4128 if (direction == L_HORIZ) {
4129 for (i = 0; i < hd; i++) {
4130 linet = datat + i * wplt;
4131 lined = datad + i * wpld;
4132 for (j = 0; j < wd; j++) {
4146 for (i = 0; i < hd; i++) {
4147 linet = datat + 3 * i * wplt;
4148 lined = datad + i * wpld;
4149 for (j = 0; j < wd; j++) {
4164 if (pixGetSpp(pixs) == 4)
4191 PROCNAME(
"l_setNeutralBoostVal");
4194 L_ERROR(
"invalid reference value for neutral boost\n", procName);
4197 var_NEUTRAL_BOOST_VAL = val;
PIX * pixUnpackBinary(PIX *pixs, l_int32 depth, l_int32 invert)
pixUnpackBinary()
PIX * pixConvertRGBToLuminance(PIX *pixs)
pixConvertRGBToLuminance()
PIX * pixOctreeColorQuant(PIX *pixs, l_int32 colors, l_int32 ditherflag)
pixOctreeColorQuant()
NUMA * pixGetGrayHistogram(PIX *pixs, l_int32 factor)
pixGetGrayHistogram()
PIX * pixConvertTo1(PIX *pixs, l_int32 threshold)
pixConvertTo1()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixConvertRGBToBinaryArb(PIX *pixs, l_float32 rc, l_float32 gc, l_float32 bc, l_int32 thresh, l_int32 relation)
pixConvertRGBToBinaryArb()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
PIX * pixConvertLossless(PIX *pixs, l_int32 d)
pixConvertLossless()
NUMA * pixOctcubeHistogram(PIX *pixs, l_int32 level, l_int32 *pncolors)
pixOctcubeHistogram()
NUMA * pixGetCmapHistogram(PIX *pixs, l_int32 factor)
pixGetCmapHistogram()
PIXCMAP * pixcmapConvertTo8(PIXCMAP *cmaps)
pixcmapConvertTo8()
static const l_float32 L_RED_WEIGHT
PIX * pixThresholdOn8bpp(PIX *pixs, l_int32 nlevels, l_int32 cmapflag)
pixThresholdOn8bpp()
PIX * pixRemoveAlpha(PIX *pixs)
pixRemoveAlpha()
PIX * pixConvert1To4(PIX *pixd, PIX *pixs, l_int32 val0, l_int32 val1)
pixConvert1To4()
PIXCMAP * pixcmapGrayToColor(l_uint32 color)
pixcmapGrayToColor()
PIX * pixConvert8To32(PIX *pixs)
pixConvert8To32()
l_ok numaGetCountRelativeToZero(NUMA *na, l_int32 type, l_int32 *pcount)
numaGetCountRelativeToZero()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
PIX * pixAddMinimalGrayColormap8(PIX *pixs)
pixAddMinimalGrayColormap8()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixcmapIsBlackAndWhite(PIXCMAP *cmap, l_int32 *pblackwhite)
pixcmapIsBlackAndWhite()
void l_setNeutralBoostVal(l_int32 val)
l_setNeutralBoostVal()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
#define SET_DATA_QBIT(pdata, n, val)
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
PIX * pixConvertGrayToColormap8(PIX *pixs, l_int32 mindepth)
pixConvertGrayToColormap8()
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
PIX * pixScaleBySampling(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScaleBySampling()
PIX * pixThreshold8(PIX *pixs, l_int32 d, l_int32 nlevels, l_int32 cmapflag)
pixThreshold8()
PIX * pixRemoveColormapGeneral(PIX *pixs, l_int32 type, l_int32 ifnocmap)
pixRemoveColormapGeneral()
l_ok pixNumColors(PIX *pixs, l_int32 factor, l_int32 *pncolors)
pixNumColors()
PIX * pixFewColorsOctcubeQuant2(PIX *pixs, l_int32 level, NUMA *na, l_int32 ncolors, l_int32 *pnerrors)
pixFewColorsOctcubeQuant2()
l_ok pixcmapGetRangeValues(PIXCMAP *cmap, l_int32 select, l_int32 *pminval, l_int32 *pmaxval, l_int32 *pminindex, l_int32 *pmaxindex)
pixcmapGetRangeValues()
#define GET_DATA_BIT(pdata, n)
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
PIX * pixColorizeGray(PIX *pixs, l_uint32 color, l_int32 cmapflag)
pixColorizeGray()
PIXCMAP * pixcmapCreateLinear(l_int32 d, l_int32 nlevels)
pixcmapCreateLinear()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
#define SET_DATA_DIBIT(pdata, n, val)
PIX * pixConvert4To8(PIX *pixs, l_int32 cmapflag)
pixConvert4To8()
l_ok pixColorsForQuantization(PIX *pixs, l_int32 thresh, l_int32 *pncolors, l_int32 *piscolor, l_int32 debug)
pixColorsForQuantization()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
PIX * pixConvertRGBToGrayFast(PIX *pixs)
pixConvertRGBToGrayFast()
PIX * pixConvert1To8Cmap(PIX *pixs)
pixConvert1To8Cmap()
PIX * pixConvert24To32(PIX *pixs)
pixConvert24To32()
l_ok pixDestroyColormap(PIX *pix)
pixDestroyColormap()
PIX * pixAlphaBlendUniform(PIX *pixs, l_uint32 color)
pixAlphaBlendUniform()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
PIX * pixConvertGrayToSubpixelRGB(PIX *pixs, l_float32 scalex, l_float32 scaley, l_int32 order)
pixConvertGrayToSubpixelRGB()
PIX * pixConvertRGBToGraySatBoost(PIX *pixs, l_int32 refval)
pixConvertRGBToGraySatBoost()
static const l_float32 L_GREEN_WEIGHT
static const l_float32 L_BLUE_WEIGHT
l_ok pixcmapToArrays(PIXCMAP *cmap, l_int32 **prmap, l_int32 **pgmap, l_int32 **pbmap, l_int32 **pamap)
pixcmapToArrays()
PIX * pixConvert1To2Cmap(PIX *pixs)
pixConvert1To2Cmap()
PIX * pixConvert32To16(PIX *pixs, l_int32 type)
pixConvert32To16()
l_ok pixcmapGetColor(PIXCMAP *cmap, l_int32 index, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
pixcmapGetColor()
PIX * pixConvert1To32(PIX *pixd, PIX *pixs, l_uint32 val0, l_uint32 val1)
pixConvert1To32()
PIX * pixConvert1To2(PIX *pixd, PIX *pixs, l_int32 val0, l_int32 val1)
pixConvert1To2()
l_ok composeRGBAPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_int32 aval, l_uint32 *ppixel)
composeRGBAPixel()
PIX * pixConvertTo8Colormap(PIX *pixs, l_int32 dither)
pixConvertTo8Colormap()
#define SET_DATA_BYTE(pdata, n, val)
l_ok pixAddGrayColormap8(PIX *pixs)
pixAddGrayColormap8()
#define GET_DATA_QBIT(pdata, n)
PIX * pixConvert32To24(PIX *pixs)
pixConvert32To24()
PIX * pixConvertRGBToGrayMinMax(PIX *pixs, l_int32 type)
pixConvertRGBToGrayMinMax()
#define GET_DATA_BYTE(pdata, n)
PIX * pixConvertGrayToColormap(PIX *pixs)
pixConvertGrayToColormap()
PIX * pixConvertTo8BySampling(PIX *pixs, l_int32 factor, l_int32 cmapflag)
pixConvertTo8BySampling()
PIX * pixCreateNoInit(l_int32 width, l_int32 height, l_int32 depth)
pixCreateNoInit()
PIX * pixClone(PIX *pixs)
pixClone()
l_ok pixcmapIsOpaque(PIXCMAP *cmap, l_int32 *popaque)
pixcmapIsOpaque()
void pixDestroy(PIX **ppix)
pixDestroy()
PIX * pixConvert8To2(PIX *pix)
pixConvert8To2()
PIX * pixFewColorsOctcubeQuant1(PIX *pixs, l_int32 level)
pixFewColorsOctcubeQuant1()
PIX * pixConvertCmapTo1(PIX *pixs)
pixConvertCmapTo1()
l_ok pixcmapHasColor(PIXCMAP *cmap, l_int32 *pcolor)
pixcmapHasColor()
void numaDestroy(NUMA **pna)
numaDestroy()
PIX * pixConvertTo16(PIX *pixs)
pixConvertTo16()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixConvertRGBToGray(PIX *pixs, l_float32 rwt, l_float32 gwt, l_float32 bwt)
pixConvertRGBToGray()
PIX * pixConvertGrayToFalseColor(PIX *pixs, l_float32 gamma)
pixConvertGrayToFalseColor()
PIX * pixConvertForPSWrap(PIX *pixs)
pixConvertForPSWrap()
PIX * pixConvert2To8(PIX *pixs, l_uint8 val0, l_uint8 val1, l_uint8 val2, l_uint8 val3, l_int32 cmapflag)
pixConvert2To8()
PIX * pixConvertRGBToGrayArb(PIX *pixs, l_float32 rc, l_float32 gc, l_float32 bc)
pixConvertRGBToGrayArb()
PIX * pixConvertColorToSubpixelRGB(PIX *pixs, l_float32 scalex, l_float32 scaley, l_int32 order)
pixConvertColorToSubpixelRGB()
PIX * pixConvert8To4(PIX *pix)
pixConvert8To4()
#define GET_DATA_TWO_BYTES(pdata, n)
l_ok pixcmapToRGBTable(PIXCMAP *cmap, l_uint32 **ptab, l_int32 *pncolors)
pixcmapToRGBTable()
#define GET_DATA_DIBIT(pdata, n)
PIX * pixThresholdTo4bpp(PIX *pixs, l_int32 nlevels, l_int32 cmapflag)
pixThresholdTo4bpp()
PIX * pixConvertRGBToColormap(PIX *pixs, l_int32 ditherflag)
pixConvertRGBToColormap()
PIX * pixConvertTo4(PIX *pixs)
pixConvertTo4()
PIX * pixConvertTo2(PIX *pixs)
pixConvertTo2()
l_int32 pixcmapGetCount(PIXCMAP *cmap)
pixcmapGetCount()
PIX * pixConvert1To4Cmap(PIX *pixs)
pixConvert1To4Cmap()
PIX * pixThresholdTo2bpp(PIX *pixs, l_int32 nlevels, l_int32 cmapflag)
pixThresholdTo2bpp()
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
PIX * pixConvert1To8(PIX *pixd, PIX *pixs, l_uint8 val0, l_uint8 val1)
pixConvert1To8()
PIX * pixConvert16To8(PIX *pixs, l_int32 type)
pixConvert16To8()
NUMA * numaNormalizeHistogram(NUMA *nas, l_float32 tsum)
numaNormalizeHistogram()
PIX * pixConvert8To16(PIX *pixs, l_int32 leftshift)
pixConvert8To16()
PIX * pixConvert1To16(PIX *pixd, PIX *pixs, l_uint16 val0, l_uint16 val1)
pixConvert1To16()
PIX * pixConvertTo1BySampling(PIX *pixs, l_int32 factor, l_int32 threshold)
pixConvertTo1BySampling()
l_ok composeRGBPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *ppixel)
composeRGBPixel()
l_ok pixcmapGetDistanceToColor(PIXCMAP *cmap, l_int32 index, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pdist)
pixcmapGetDistanceToColor()
l_ok pixcmapAddColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapAddColor()
l_ok pixScaleAndTransferAlpha(PIX *pixd, PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScaleAndTransferAlpha()
PIX * pixConvertTo32BySampling(PIX *pixs, l_int32 factor)
pixConvertTo32BySampling()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
#define SET_DATA_TWO_BYTES(pdata, n, val)
l_ok pixQuantizeIfFewColors(PIX *pixs, l_int32 maxcolors, l_int32 mingraycolors, l_int32 octlevel, PIX **ppixd)
pixQuantizeIfFewColors()
void extractRGBValues(l_uint32 pixel, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
extractRGBValues()
PIX * pixConvertTo8Or32(PIX *pixs, l_int32 copyflag, l_int32 warnflag)
pixConvertTo8Or32()
#define SET_DATA_BIT(pdata, n)
l_ok pixcmapAddRGBA(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 aval)
pixcmapAddRGBA()
PIX * pixConvertToSubpixelRGB(PIX *pixs, l_float32 scalex, l_float32 scaley, l_int32 order)
pixConvertToSubpixelRGB()
PIX * pixConvert32To8(PIX *pixs, l_int32 type16, l_int32 type8)
pixConvert32To8()
PIX * pixAddAlphaTo1bpp(PIX *pixd, PIX *pixs)
pixAddAlphaTo1bpp()