334l_int32 i, j, w, h, d, x, y, wpls, wpld, color, cmapindex;
336l_uint32 *datas, *datad, *lines, *lined;
341 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
343 return (
PIX *)ERROR_PTR(
"vc not defined", __func__, NULL);
345 return (
PIX *)ERROR_PTR(
"invalid incolor", __func__, NULL);
346 pixGetDimensions(pixs, &w, &h, &d);
347 if (d != 1 && d != 2 && d != 4 && d != 8 && d != 32)
348 return (
PIX *)ERROR_PTR(
"depth not 1, 2, 4, 8 or 16", __func__, NULL);
351 pixd = pixCreateTemplate(pixs);
352 if ((cmap = pixGetColormap(pixs)) != NULL) {
357 pixcmapAddBlackOrWhite(cmap, color, &cmapindex);
358 pixSetAllArbitrary(pixd, cmapindex);
369 datas = pixGetData(pixs);
370 wpls = pixGetWpl(pixs);
371 datad = pixGetData(pixd);
372 wpld = pixGetWpl(pixd);
373 for (i = 0; i < h; i++) {
374 lined = datad + i * wpld;
375 for (j = 0; j < w; j++) {
377 if (x < 0 || y < 0 || x >=w || y >= h)
379 lines = datas + y * wpls;
386 }
else if (d == 32) {
586l_int32 i, j, w, h, d, wpls, wpld;
588l_uint32 *datas, *datad, *lined;
590PIX *pix1, *pix2, *pixd;
593 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
594 pixGetDimensions(pixs, &w, &h, &d);
596 return (
PIX *)ERROR_PTR(
"pixs must be 32 bpp", __func__, NULL);
598 return (
PIX *)ERROR_PTR(
"vc not defined", __func__, NULL);
600 datas = pixGetData(pixs);
601 wpls = pixGetWpl(pixs);
602 pixd = pixCreateTemplate(pixs);
603 pixSetAllArbitrary(pixd, colorval);
604 datad = pixGetData(pixd);
605 wpld = pixGetWpl(pixd);
608 for (i = 0; i < h; i++) {
609 lined = datad + i * wpld;
610 for (j = 0; j < w; j++) {
620 if (pixGetSpp(pixs) == 4) {
686l_int32 i, j, w, h, wpls, wpld, val;
687l_uint32 *datas, *datad, *lined;
692 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
693 pixGetDimensions(pixs, &w, &h, NULL);
694 if (pixGetDepth(pixs) != 8)
695 return (
PIX *)ERROR_PTR(
"pixs must be 8 bpp", __func__, NULL);
697 return (
PIX *)ERROR_PTR(
"vc not defined", __func__, NULL);
699 datas = pixGetData(pixs);
700 wpls = pixGetWpl(pixs);
701 pixd = pixCreateTemplate(pixs);
702 pixSetAllArbitrary(pixd, grayval);
703 datad = pixGetData(pixd);
704 wpld = pixGetWpl(pixd);
707 for (i = 0; i < h; i++) {
708 lined = datad + i * wpld;
709 for (j = 0; j < w; j++) {
776PIX *pixd, *pixb1, *pixb2, *pixg2, *pixga;
780 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
781 pixGetDimensions(pixs, &ws, &hs, &d);
782 if (d != 32 && pixGetColormap(pixs) == NULL)
783 return (
PIX *)ERROR_PTR(
"pixs not cmapped or 32 bpp", __func__, NULL);
784 if (pixg && pixGetDepth(pixg) != 8) {
785 L_WARNING(
"pixg not 8 bpp; using 'fract' transparent alpha\n",
789 if (!pixg && (fract < 0.0 || fract > 1.0)) {
790 L_WARNING(
"invalid fract; using 1.0 (fully transparent)\n", __func__);
793 if (!pixg && fract == 0.0)
794 L_WARNING(
"fully opaque alpha; image will not be blended\n", __func__);
796 return (
PIX *)ERROR_PTR(
"ptad not defined", __func__, NULL);
798 return (
PIX *)ERROR_PTR(
"ptas not defined", __func__, NULL);
801 pixb1 = pixAddBorder(pixs, border, 0);
804 ptad2 = ptaTransform(ptad, border, border, 1.0, 1.0);
805 ptas2 = ptaTransform(ptas, border, border, 1.0, 1.0);
810 pixg2 = pixCreate(ws, hs, 8);
814 pixSetAllArbitrary(pixg2, (l_int32)(255.0 * fract));
816 pixg2 = pixResizeToMatch(pixg, NULL, ws, hs);
818 if (ws > 10 && hs > 10) {
819 pixSetBorderRingVal(pixg2, 1,
820 (l_int32)(255.0 * fract * AlphaMaskBorderVals[0]));
821 pixSetBorderRingVal(pixg2, 2,
822 (l_int32)(255.0 * fract * AlphaMaskBorderVals[1]));
825 pixb2 = pixAddBorder(pixg2, border, 0);
920l_float32 x1, y1, x2, y2, x3, y3;
925 return ERROR_INT(
"ptas not defined", __func__, 1);
927 return ERROR_INT(
"ptad not defined", __func__, 1);
929 return ERROR_INT(
"&vc not defined", __func__, 1);
931 b = (l_float32 *)LEPT_CALLOC(6,
sizeof(l_float32));
934 ptaGetPt(ptas, 0, &x1, &y1);
935 ptaGetPt(ptas, 1, &x2, &y2);
936 ptaGetPt(ptas, 2, &x3, &y3);
937 ptaGetPt(ptad, 0, &b[0], &b[1]);
938 ptaGetPt(ptad, 1, &b[2], &b[3]);
939 ptaGetPt(ptad, 2, &b[4], &b[5]);
941 for (i = 0; i < 6; i++)
942 a[i] = (l_float32 *)LEPT_CALLOC(6,
sizeof(l_float32));
964 for (i = 0; i < 6; i++)
1162l_int32 valid, xpm, ypm, xp, xp2, yp, xf, yf;
1163l_int32 rval, gval, bval;
1164l_uint32 word00, word01, word10, word11;
1168 return ERROR_INT(
"&val not defined", __func__, 1);
1171 return ERROR_INT(
"datas not defined", __func__, 1);
1177 valid = (x >= 0.0 && y >= 0.0 && x < w && y < h);
1178 if (!valid)
return 0;
1180 xpm = (l_int32)(16.0 * x);
1181 ypm = (l_int32)(16.0 * y);
1183 xp2 = xp + 1 < w ? xp + 1 : xp;
1185 if (yp + 1 >= h) wpls = 0;
1190 if (xf < 0 || yf < 0)
1191 lept_stderr(
"xp = %d, yp = %d, xf = %d, yf = %d\n", xp, yp, xf, yf);
1195 lines = datas + yp * wpls;
1196 word00 = *(lines + xp);
1197 word10 = *(lines + xp2);
1198 word01 = *(lines + wpls + xp);
1199 word11 = *(lines + wpls + xp2);
1200 rval = ((16 - xf) * (16 - yf) * ((word00 >> L_RED_SHIFT) & 0xff) +
1201 xf * (16 - yf) * ((word10 >> L_RED_SHIFT) & 0xff) +
1202 (16 - xf) * yf * ((word01 >> L_RED_SHIFT) & 0xff) +
1203 xf * yf * ((word11 >> L_RED_SHIFT) & 0xff)) / 256;
1204 gval = ((16 - xf) * (16 - yf) * ((word00 >> L_GREEN_SHIFT) & 0xff) +
1205 xf * (16 - yf) * ((word10 >> L_GREEN_SHIFT) & 0xff) +
1206 (16 - xf) * yf * ((word01 >> L_GREEN_SHIFT) & 0xff) +
1207 xf * yf * ((word11 >> L_GREEN_SHIFT) & 0xff)) / 256;
1208 bval = ((16 - xf) * (16 - yf) * ((word00 >> L_BLUE_SHIFT) & 0xff) +
1209 xf * (16 - yf) * ((word10 >> L_BLUE_SHIFT) & 0xff) +
1210 (16 - xf) * yf * ((word01 >> L_BLUE_SHIFT) & 0xff) +
1211 xf * yf * ((word11 >> L_BLUE_SHIFT) & 0xff)) / 256;
1212 composeRGBPixel(rval, gval, bval, pval);
1246l_int32 valid, xpm, ypm, xp, xp2, yp, xf, yf, v00, v10, v01, v11;
1250 return ERROR_INT(
"&val not defined", __func__, 1);
1253 return ERROR_INT(
"datas not defined", __func__, 1);
1259 valid = (x >= 0.0 && y >= 0.0 && x < w && y < h);
1260 if (!valid)
return 0;
1262 xpm = (l_int32)(16.0 * x);
1263 ypm = (l_int32)(16.0 * y);
1265 xp2 = xp + 1 < w ? xp + 1 : xp;
1267 if (yp + 1 >= h) wpls = 0;
1272 if (xf < 0 || yf < 0)
1273 lept_stderr(
"xp = %d, yp = %d, xf = %d, yf = %d\n", xp, yp, xf, yf);
1277 lines = datas + yp * wpls;
1282 *pval = (v00 + v01 + v10 + v11) / 256;
1318l_int32 i, icol, irow, j, k, col, row, success;
1319l_int32 *indexc, *indexr, *ipiv;
1320l_float32 maxval, val, pivinv, temp;
1323 return ERROR_INT(
"a not defined", __func__, 1);
1325 return ERROR_INT(
"b not defined", __func__, 1);
1328 indexc = (l_int32 *)LEPT_CALLOC(n,
sizeof(l_int32));
1329 indexr = (l_int32 *)LEPT_CALLOC(n,
sizeof(l_int32));
1330 ipiv = (l_int32 *)LEPT_CALLOC(n,
sizeof(l_int32));
1331 if (!indexc || !indexr || !ipiv) {
1332 L_ERROR(
"array not made\n", __func__);
1334 goto cleanup_arrays;
1338 for (i = 0; i < n; i++) {
1340 for (j = 0; j < n; j++) {
1342 for (k = 0; k < n; k++) {
1344 if (fabs(a[j][k]) >= maxval) {
1345 maxval = fabs(a[j][k]);
1349 }
else if (ipiv[k] > 1) {
1350 L_ERROR(
"singular matrix\n", __func__);
1352 goto cleanup_arrays;
1360 for (col = 0; col < n; col++)
1361 SWAP(a[irow][col], a[icol][col]);
1362 SWAP(b[irow], b[icol]);
1367 if (a[icol][icol] == 0.0) {
1368 L_ERROR(
"singular matrix\n", __func__);
1370 goto cleanup_arrays;
1372 pivinv = 1.0 / a[icol][icol];
1373 a[icol][icol] = 1.0;
1374 for (col = 0; col < n; col++)
1375 a[icol][col] *= pivinv;
1378 for (row = 0; row < n; row++) {
1382 for (col = 0; col < n; col++)
1383 a[row][col] -= a[icol][col] * val;
1384 b[row] -= b[icol] * val;
1389 for (col = n - 1; col >= 0; col--) {
1390 if (indexr[col] != indexc[col]) {
1391 for (k = 0; k < n; k++)
1392 SWAP(a[k][indexr[col]], a[k][indexc[col]]);
1400 return (success) ? 0 : 1;
1442l_int32 x1, y1, x2, y2, x3, y3;
1443l_int32 x1p, y1p, x2p, y2p, x3p, y3p;
1445l_float32 x2s, x2sp, scalex, scaley;
1446l_float32 th3, th3p, ph2, ph2p;
1450PIX *pix1, *pix2, *pixd;
1453 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1455 return (
PIX *)ERROR_PTR(
"ptas not defined", __func__, NULL);
1457 return (
PIX *)ERROR_PTR(
"ptad not defined", __func__, NULL);
1459 if (ptaGetCount(ptas) != 3)
1460 return (
PIX *)ERROR_PTR(
"ptas count not 3", __func__, NULL);
1461 if (ptaGetCount(ptad) != 3)
1462 return (
PIX *)ERROR_PTR(
"ptad count not 3", __func__, NULL);
1463 ptaGetIPt(ptas, 0, &x1, &y1);
1464 ptaGetIPt(ptas, 1, &x2, &y2);
1465 ptaGetIPt(ptas, 2, &x3, &y3);
1466 ptaGetIPt(ptad, 0, &x1p, &y1p);
1467 ptaGetIPt(ptad, 1, &x2p, &y2p);
1468 ptaGetIPt(ptad, 2, &x3p, &y3p);
1470 pix1 = pix2 = pixd = NULL;
1473 return (
PIX *)ERROR_PTR(
"y1 == y3!", __func__, NULL);
1475 return (
PIX *)ERROR_PTR(
"y1p == y3p!", __func__, NULL);
1477 if (bw != 0 || bh != 0) {
1492 if ((pix1 = pixAddBorderGeneral(pixs, bw, bw, bh, bh, 0)) == NULL)
1493 return (
PIX *)ERROR_PTR(
"pix1 not made", __func__, NULL);
1495 pix1 = pixCopy(NULL, pixs);
1510 th3 = atan2((l_float64)(x1 - x3), (l_float64)(y1 - y3));
1511 x2s = (l_float32)(x2 - ((l_float32)(y1 - y2) * (x3 - x1)) / (y1 - y3));
1512 if (x2s == (l_float32)x1) {
1513 L_ERROR(
"x2s == x1!\n", __func__);
1516 ph2 = atan2((l_float64)(y1 - y2), (l_float64)(x2s - x1));
1522 th3p = atan2((l_float64)(x1p - x3p), (l_float64)(y1p - y3p));
1523 x2sp = (l_float32)(x2p -
1524 ((l_float32)(y1p - y2p) * (x3p - x1p)) / (y1p - y3p));
1525 if (x2sp == (l_float32)x1p) {
1526 L_ERROR(
"x2sp == x1p!\n", __func__);
1529 ph2p = atan2((l_float64)(y1p - y2p), (l_float64)(x2sp - x1p));
1540 scalex = (l_float32)(x2sp - x1p) / (x2s - x1);
1541 scaley = (l_float32)(y3p - y1p) / (y3 - y1);
1542 if ((pix2 = pixScale(pix1, scalex, scaley)) == NULL) {
1543 L_ERROR(
"pix2 not made\n", __func__);
1548 rad2deg = 180. / 3.1415926535;
1549 lept_stderr(
"th3 = %5.1f deg, ph2 = %5.1f deg\n",
1550 rad2deg * th3, rad2deg * ph2);
1551 lept_stderr(
"th3' = %5.1f deg, ph2' = %5.1f deg\n",
1552 rad2deg * th3p, rad2deg * ph2p);
1553 lept_stderr(
"scalex = %6.3f, scaley = %6.3f\n", scalex, scaley);
1567 x1sc = (l_int32)(scalex * x1 + 0.5);
1568 y1sc = (l_int32)(scaley * y1 + 0.5);
1576 if (bw != 0 || bh != 0) {
1577 if ((pixd = pixRemoveBorderGeneral(pix2, bw, bw, bh, bh)) == NULL)
1578 L_ERROR(
"pixd not made\n", __func__);
1580 pixd = pixClone(pix2);