/*! * pixBilinearPta() * * Input: pixs (all depths; colormap ok) * ptad (4 pts of final coordinate space) * ptas (4 pts of initial coordinate space) * incolor (L_BRING_IN_WHITE, L_BRING_IN_BLACK) * Return: pixd, or null on error * * Notes: * (1) Brings in either black or white pixels from the boundary * (2) Removes any existing colormap, if necessary, before transforming */ PIX * pixBilinearPta(PIX *pixs, PTA *ptad, PTA *ptas, l_int32 incolor) { l_int32 d; l_uint32 colorval; PIX *pixt1, *pixt2, *pixd; PROCNAME("pixBilinearPta"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (!ptas) return (PIX *)ERROR_PTR("ptas not defined", procName, NULL); if (!ptad) return (PIX *)ERROR_PTR("ptad not defined", procName, NULL); if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK) return (PIX *)ERROR_PTR("invalid incolor", procName, NULL); if (ptaGetCount(ptas) != 4) return (PIX *)ERROR_PTR("ptas count not 4", procName, NULL); if (ptaGetCount(ptad) != 4) return (PIX *)ERROR_PTR("ptad count not 4", procName, NULL); if (pixGetDepth(pixs) == 1) return pixBilinearSampledPta(pixs, ptad, ptas, incolor); /* Remove cmap if it exists, and unpack to 8 bpp if necessary */ pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); d = pixGetDepth(pixt1); if (d < 8) pixt2 = pixConvertTo8(pixt1, FALSE); else pixt2 = pixClone(pixt1); d = pixGetDepth(pixt2); /* Compute actual color to bring in from edges */ colorval = 0; if (incolor == L_BRING_IN_WHITE) { if (d == 8) colorval = 255; else /* d == 32 */ colorval = 0xffffff00; } if (d == 8) pixd = pixBilinearPtaGray(pixt2, ptad, ptas, colorval); else /* d == 32 */ pixd = pixBilinearPtaColor(pixt2, ptad, ptas, colorval); pixDestroy(&pixt1); pixDestroy(&pixt2); return pixd; }
main(int argc, char **argv) { l_int32 i, d, h; l_float32 rat; PIX *pixs, *pixgb, *pixt1, *pixt2, *pixt3, *pixt4, *pixg, *pixd; PIXA *pixa; PTA *ptas, *ptad; static char mainName[] = "bilinear_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: bilinear_reg", mainName, 1)); pixs = pixRead("feyn.tif"); pixg = pixScaleToGray3(pixs); #if ALL /* Test non-invertability of sampling */ pixa = pixaCreate(0); for (i = 1; i < 3; i++) { pixgb = pixAddBorder(pixg, ADDED_BORDER_PIXELS, 255); MakePtas(i, &ptas, &ptad); pixt1 = pixBilinearSampledPta(pixgb, ptad, ptas, L_BRING_IN_WHITE); pixSaveTiled(pixt1, pixa, 2, 1, 20, 8); pixt2 = pixBilinearSampledPta(pixt1, ptas, ptad, L_BRING_IN_WHITE); pixSaveTiled(pixt2, pixa, 2, 0, 20, 0); pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS); pixInvert(pixd, pixd); pixXor(pixd, pixd, pixg); pixSaveTiled(pixd, pixa, 2, 0, 20, 0); if (i == 0) pixWrite("/tmp/junksamp.png", pixt1, IFF_PNG); pixDestroy(&pixgb); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixd); ptaDestroy(&ptas); ptaDestroy(&ptad); } pixt1 = pixaDisplay(pixa, 0, 0); pixWrite("/tmp/junkbilin1.png", pixt1, IFF_PNG); pixDisplay(pixt1, 100, 300); pixDestroy(&pixt1); pixaDestroy(&pixa); #endif #if ALL /* Test non-invertability of interpolation */ pixa = pixaCreate(0); for (i = 1; i < 3; i++) { pixgb = pixAddBorder(pixg, ADDED_BORDER_PIXELS, 255); MakePtas(i, &ptas, &ptad); pixt1 = pixBilinearPta(pixgb, ptad, ptas, L_BRING_IN_WHITE); pixSaveTiled(pixt1, pixa, 2, 1, 20, 8); pixt2 = pixBilinearPta(pixt1, ptas, ptad, L_BRING_IN_WHITE); pixSaveTiled(pixt2, pixa, 2, 0, 20, 0); pixd = pixRemoveBorder(pixt2, ADDED_BORDER_PIXELS); pixInvert(pixd, pixd); pixXor(pixd, pixd, pixg); pixSaveTiled(pixd, pixa, 2, 0, 20, 0); if (i == 0) pixWrite("/tmp/junkinterp.png", pixt1, IFF_PNG); pixDestroy(&pixgb); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixd); ptaDestroy(&ptas); ptaDestroy(&ptad); } pixt1 = pixaDisplay(pixa, 0, 0); pixWrite("/tmp/junkbilin2.png", pixt1, IFF_PNG); pixDisplay(pixt1, 100, 300); pixDestroy(&pixt1); pixaDestroy(&pixa); #endif #if ALL /* test with large distortion and inversion */ MakePtas(0, &ptas, &ptad); pixa = pixaCreate(0); startTimer(); pixt1 = pixBilinearSampledPta(pixg, ptas, ptad, L_BRING_IN_WHITE); fprintf(stderr, " Time for pixBilinearSampled(): %6.2f sec\n", stopTimer()); pixSaveTiled(pixt1, pixa, 2, 1, 20, 8); startTimer(); pixt2 = pixBilinearPta(pixg, ptas, ptad, L_BRING_IN_WHITE); fprintf(stderr, " Time for pixBilinearInterpolated(): %6.2f sec\n", stopTimer()); pixSaveTiled(pixt2, pixa, 2, 0, 20, 8); pixt3 = pixBilinearSampledPta(pixt1, ptad, ptas, L_BRING_IN_WHITE); pixSaveTiled(pixt3, pixa, 2, 0, 20, 8); pixt4 = pixBilinearPta(pixt2, ptad, ptas, L_BRING_IN_WHITE); pixSaveTiled(pixt4, pixa, 2, 0, 20, 8); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixt1 = pixaDisplay(pixa, 0, 0); pixWrite("/tmp/junkbilin3.png", pixt1, IFF_PNG); pixDisplay(pixt1, 100, 300); pixDestroy(&pixt1); pixaDestroy(&pixa); pixDestroy(&pixs); pixDestroy(&pixg); ptaDestroy(&ptas); ptaDestroy(&ptad); #endif return 0; }