void PixCompareDwa(L_REGPARAMS *rp, l_int32 size, const char *type, PIX *pix1, PIX *pix2, PIX *pix3, PIX *pix4, PIX *pix5, PIX *pix6) { l_int32 same; pixEqual(pix1, pix2, &same); regTestCompareValues(rp, TRUE, same, 0); if (!same) fprintf(stderr, "%s (%d, 1) not same\n", type, size); pixEqual(pix3, pix4, &same); regTestCompareValues(rp, TRUE, same, 0); if (!same) fprintf(stderr, "%s (1, %d) not same\n", type, size); pixEqual(pix5, pix6, &same); regTestCompareValues(rp, TRUE, same, 0); if (!same) fprintf(stderr, "%s (%d, %d) not same\n", type, size, size); }
l_int32 PixCompareDwa(l_int32 size, const char *type, PIX *pixt1, PIX *pixt2, PIX *pixt3, PIX *pixt4, PIX *pixt5, PIX *pixt6) { l_int32 same, fail; fail = FALSE; pixEqual(pixt1, pixt2, &same); if (!same) { fail = TRUE; fprintf(stderr, "%s (%d, 1) not same\n", type, size); } pixEqual(pixt3, pixt4, &same); if (!same) { fail = TRUE; fprintf(stderr, "%s (1, %d) not same\n", type, size); } pixEqual(pixt5, pixt6, &same); if (!same) { fail = TRUE; fprintf(stderr, "%s (%d, %d) not same\n", type, size, size); } return fail; }
static l_int32 compareResults(PIX *pixs, PIX *pixt1, PIX *pixt2, l_int32 count1, l_int32 count2, const char *descr) { l_int32 ret, same; ret = 0; pixEqual(pixs, pixt1, &same); if (!same) { fprintf(stderr, "pixt1 != pixs in %s\n", descr); ret = 1; } pixEqual(pixs, pixt2, &same); if (!same) { fprintf(stderr, "pixt2 != pixs in %s\n", descr); ret = 1; } if (count1 != count2) { fprintf(stderr, "Counts not same in %s\n", descr); ret = 1; } else fprintf(stderr, "Counts equal in %s: %d\n", descr, count1); pixClearAll(pixt1); pixClearAll(pixt2); if (!ret) fprintf(stderr, "All OK for %s\n", descr); return ret; }
int main(int argc, char **argv) { l_int32 i, nsels, same1, same2, ok; char *filein, *selname; PIX *pixs, *pixref, *pixt1, *pixt2, *pixt3, *pixt4; SEL *sel; SELA *sela; static char mainName[] = "fhmtauto_reg"; if (argc != 2) return ERROR_INT(" Syntax: fhmtauto_reg filein", mainName, 1); filein = argv[1]; if ((pixs = pixRead(filein)) == NULL) return ERROR_INT("pixs not made", mainName, 1); sela = selaAddHitMiss(NULL); nsels = selaGetCount(sela); ok = TRUE; for (i = 0; i < nsels; i++) { sel = selaGetSel(sela, i); selname = selGetName(sel); pixref = pixHMT(NULL, pixs, sel); pixt1 = pixAddBorder(pixs, 32, 0); pixt2 = pixFHMTGen_1(NULL, pixt1, selname); pixt3 = pixRemoveBorder(pixt2, 32); pixt4 = pixHMTDwa_1(NULL, pixs, selname); pixEqual(pixref, pixt3, &same1); pixEqual(pixref, pixt4, &same2); if (same1 && same2) fprintf(stderr, "hmt are identical for sel %d (%s)\n", i, selname); else { fprintf(stderr, "hmt differ for sel %d (%s)\n", i, selname); ok = FALSE; } pixDestroy(&pixref); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); } if (ok) fprintf(stderr, "\n ******** All hmt are correct *******\n"); else fprintf(stderr, "\n ******** ERROR in at least one hmt *******\n"); pixDestroy(&pixs); selaDestroy(&sela); return 0; }
/* Returns 1 on error */ static l_int32 test_mem_png(const char *fname) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixs; PIX *pixd = NULL; if ((pixs = pixRead(fname)) == NULL) { fprintf(stderr, "Failure to read %s\n", fname); return 1; } if (pixWriteMem(&data, &size, pixs, IFF_PNG)) { fprintf(stderr, "Mem write fail for png\n"); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem read fail for png\n"); lept_free(data); return 1; } pixEqual(pixs, pixd, &same); if (!same) fprintf(stderr, "Mem write/read fail for file %s\n", fname); pixDestroy(&pixs); pixDestroy(&pixd); lept_free(data); return (!same); }
static l_int32 test_1bpp_color(L_REGPARAMS *rp) { l_int32 same, transp; FILE *fp; PIX *pix1, *pix2; PIXCMAP *cmap; pix1 = pixRead("feyn-fract2.tif"); cmap = pixcmapCreate(1); pixSetColormap(pix1, cmap); pixcmapAddRGBA(cmap, 180, 130, 220, 255); /* color, opaque */ pixcmapAddRGBA(cmap, 20, 120, 0, 255); /* color, opaque */ pixWrite("/tmp/regout/1bpp-color.png", pix1, IFF_PNG); pix2 = pixRead("/tmp/regout/1bpp-color.png"); pixEqual(pix1, pix2, &same); if (same) fprintf(stderr, "1bpp_color: success\n"); else fprintf(stderr, "1bpp_color: bad output\n"); pixDisplayWithTitle(pix2, 700, 100, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); fp = fopenReadStream("/tmp/regout/1bpp-color.png"); fgetPngColormapInfo(fp, &cmap, &transp); fclose(fp); if (transp) fprintf(stderr, "1bpp_color: error -- transparency found!\n"); else fprintf(stderr, "1bpp_color: correct -- no transparency found\n"); if (rp->display) pixcmapWriteStream(stderr, cmap); pixcmapDestroy(&cmap); return same; }
/*! * regTestComparePix() * * Input: rp (regtest parameters) * pix1, pix2 (to be tested for equality) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This function compares two pix for equality. On failure, * this writes to stderr. */ l_int32 regTestComparePix(L_REGPARAMS *rp, PIX *pix1, PIX *pix2) { l_int32 same; PROCNAME("regTestComparePix"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (!pix1 || !pix2) { rp->success = FALSE; return ERROR_INT("pix1 and pix2 not both defined", procName, 1); } rp->index++; pixEqual(pix1, pix2, &same); /* Record on failure */ if (!same) { if (rp->fp) { fprintf(rp->fp, "Failure in %s_reg: pix comparison for index %d\n", rp->testname, rp->index); } fprintf(stderr, "Failure in %s_reg: pix comparison for index %d\n", rp->testname, rp->index); rp->success = FALSE; } return 0; }
static void test_gif(const char *fname, PIXA *pixa, L_REGPARAMS *rp) { char buf[256]; l_int32 same; PIX *pixs, *pix1, *pix2; pixs = pixRead(fname); snprintf(buf, sizeof(buf), "/tmp/lept/gif/gifio-a.%d.gif", rp->index + 1); pixWrite(buf, pixs, IFF_GIF); pix1 = pixRead(buf); snprintf(buf, sizeof(buf), "/tmp/lept/gif/gifio-b.%d.gif", rp->index + 1); pixWrite(buf, pix1, IFF_GIF); pix2 = pixRead(buf); regTestWritePixAndCheck(rp, pix2, IFF_GIF); pixEqual(pixs, pix2, &same); if (!same && rp->index < 6) { fprintf(stderr, "Error for %s\n", fname); rp->success = FALSE; } if (rp->display) { fprintf(stderr, " depth: pixs = %d, pix1 = %d\n", pixGetDepth(pixs), pixGetDepth(pix1)); pixaAddPix(pixa, pix2, L_CLONE); } pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); return; }
/* Returns 1 on error */ static l_int32 testcomp(const char *filename, PIX *pix, l_int32 comptype) { l_int32 format, sameformat, sameimage; FILE *fp; PIX *pixt; fp = lept_fopen(filename, "rb"); findFileFormatStream(fp, &format); sameformat = TRUE; if (format != comptype) { fprintf(stderr, "File %s has format %d, not comptype %d\n", filename, format, comptype); sameformat = FALSE; } lept_fclose(fp); pixt = pixRead(filename); pixEqual(pix, pixt, &sameimage); pixDestroy(&pixt); if (!sameimage) fprintf(stderr, "Write/read fail for file %s with format %d\n", filename, format); return (!sameformat || !sameimage); }
/* Returns 1 on error */ static l_int32 test_mem_gif(const char *fname, l_int32 index) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixs; PIX *pixd = NULL; if ((pixs = pixRead(fname)) == NULL) { fprintf(stderr, "Failure to read gif file: %s\n", fname); return 1; } if (pixWriteMem(&data, &size, pixs, IFF_GIF)) { fprintf(stderr, "Mem gif write fail on image %d\n", index); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem gif read fail on image %d\n", index); lept_free(data); return 1; } pixEqual(pixs, pixd, &same); pixDestroy(&pixs); pixDestroy(&pixd); lept_free(data); if (!same && index < 6) { fprintf(stderr, "Mem gif write/read fail for file %s\n", fname); return 1; } else return 0; }
static l_int32 test_8bpp_trans(L_REGPARAMS *rp) { l_int32 same, transp; FILE *fp; PIX *pix1, *pix2, *pix3; PIXCMAP *cmap; pix1 = pixRead("wyom.jpg"); pix2 = pixColorSegment(pix1, 75, 10, 8, 7); cmap = pixGetColormap(pix2); pixcmapSetAlpha(cmap, 0, 0); /* set blueish sky color to transparent */ pixWrite("/tmp/regout/8bpp-trans.png", pix2, IFF_PNG); pix3 = pixRead("/tmp/regout/8bpp-trans.png"); pixEqual(pix2, pix3, &same); if (same) fprintf(stderr, "8bpp_trans: success\n"); else fprintf(stderr, "8bpp_trans: bad output\n"); pixDisplayWithTitle(pix3, 700, 0, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); fp = fopenReadStream("/tmp/regout/8bpp-trans.png"); fgetPngColormapInfo(fp, &cmap, &transp); fclose(fp); if (transp) fprintf(stderr, "8bpp_trans: correct -- transparency found\n"); else fprintf(stderr, "8bpp_trans: error -- no transparency found!\n"); if (rp->display) pixcmapWriteStream(stderr, cmap); pixcmapDestroy(&cmap); return same; }
main(int argc, char **argv) { l_int32 i, j, w, h, same, width, height, cx, cy; l_uint32 val; PIX *pixs, *pixse, *pixd1, *pixd2; SEL *sel; static char mainName[] = "rasterop_reg"; if (argc != 1) return ERROR_INT(" Syntax: rasterop_reg", mainName, 1); pixs = pixRead("feyn.tif"); for (width = 1; width <= 25; width += 3) { for (height = 1; height <= 25; height += 4) { cx = width / 2; cy = height / 2; /* Dilate using an actual sel */ sel = selCreateBrick(height, width, cy, cx, SEL_HIT); pixd1 = pixDilate(NULL, pixs, sel); /* Dilate using a pix as a sel */ pixse = pixCreate(width, height, 1); pixSetAll(pixse); pixd2 = pixCopy(NULL, pixs); w = pixGetWidth(pixs); h = pixGetHeight(pixs); for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { pixGetPixel(pixs, j, i, &val); if (val) pixRasterop(pixd2, j - cx, i - cy, width, height, PIX_SRC | PIX_DST, pixse, 0, 0); } } pixEqual(pixd1, pixd2, &same); if (same == 1) fprintf(stderr, "Correct for (%d,%d)\n", width, height); else { fprintf(stderr, "Error: results are different!\n"); fprintf(stderr, "SE: width = %d, height = %d\n", width, height); pixWrite("/tmp/junkout1", pixd1, IFF_PNG); pixWrite("/tmp/junkout2", pixd2, IFF_PNG); return 1; } pixDestroy(&pixse); pixDestroy(&pixd1); pixDestroy(&pixd2); selDestroy(&sel); } } pixDestroy(&pixs); return 0; }
void PixTestEqual(PIX *pixs1, PIX *pixs2, PIX *pixm, l_int32 set, l_int32 connectivity) { l_int32 same; PIX *pixc11, *pixc12, *pixc21, *pixc22, *pixmi; pixmi = pixInvert(NULL, pixm); pixc11 = pixCopy(NULL, pixs1); pixc12 = pixCopy(NULL, pixs1); pixc21 = pixCopy(NULL, pixs2); pixc22 = pixCopy(NULL, pixs2); /* Test inverse seed filling */ pixSeedfillGrayInv(pixc11, pixm, connectivity); pixSeedfillGrayInvSimple(pixc12, pixm, connectivity); pixEqual(pixc11, pixc12, &same); if (same) fprintf(stderr, "\nSuccess for inv set %d\n", set); else fprintf(stderr, "\nFailure for inv set %d\n", set); /* Test seed filling */ pixSeedfillGray(pixc21, pixm, connectivity); pixSeedfillGraySimple(pixc22, pixm, connectivity); pixEqual(pixc21, pixc22, &same); if (same) fprintf(stderr, "Success for set %d\n", set); else fprintf(stderr, "Failure for set %d\n", set); /* Display the filling results */ /* pixDisplay(pixc11, 220 * (set - 1), 100); pixDisplay(pixc21, 220 * (set - 1), 320); */ pixDestroy(&pixmi); pixDestroy(&pixc11); pixDestroy(&pixc12); pixDestroy(&pixc21); pixDestroy(&pixc22); return; }
int main(int argc, char **argv) { char *filein, *fileout; char bigbuf[512]; l_int32 iplot, same; l_float32 gam; l_float64 gamma[] = {.5, 1.0, 1.5, 2.0, 2.5, -1.0}; GPLOT *gplot; NUMA *na, *nax; PIX *pixs, *pixd; static char mainName[] = "gammatest"; if (argc != 4) return ERROR_INT(" Syntax: gammatest filein gam fileout", mainName, 1); lept_mkdir("lept/gamma"); filein = argv[1]; gam = atof(argv[2]); fileout = argv[3]; if ((pixs = pixRead(filein)) == NULL) return ERROR_INT("pixs not made", mainName, 1); startTimer(); pixd = pixGammaTRC(NULL, pixs, gam, MINVAL, MAXVAL); fprintf(stderr, "Time for gamma: %7.3f sec\n", stopTimer()); pixGammaTRC(pixs, pixs, gam, MINVAL, MAXVAL); pixEqual(pixs, pixd, &same); if (!same) fprintf(stderr, "Error in pixGammaTRC!\n"); pixWrite(fileout, pixs, IFF_JFIF_JPEG); pixDestroy(&pixs); na = numaGammaTRC(gam, MINVAL, MAXVAL); gplotSimple1(na, GPLOT_PNG, "/tmp/lept/gamma/trc", "gamma trc"); l_fileDisplay("/tmp/lept/gamma/trc.png", 100, 100, 1.0); numaDestroy(&na); /* Plot gamma TRC maps */ gplot = gplotCreate("/tmp/lept/gamma/corr", GPLOT_PNG, "Mapping function for gamma correction", "value in", "value out"); nax = numaMakeSequence(0.0, 1.0, 256); for (iplot = 0; gamma[iplot] >= 0.0; iplot++) { na = numaGammaTRC(gamma[iplot], 30, 215); sprintf(bigbuf, "gamma = %3.1f", gamma[iplot]); gplotAddPlot(gplot, nax, na, GPLOT_LINES, bigbuf); numaDestroy(&na); } gplotMakeOutput(gplot); gplotDestroy(&gplot); l_fileDisplay("/tmp/lept/gamma/corr.png", 100, 100, 1.0); numaDestroy(&nax); return 0; }
/* Returns 1 on error */ static l_int32 test_writemem(PIX *pixs, l_int32 format, char *psfile) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixd = NULL; if (format == IFF_PS) { pixWriteMemPS(&data, &size, pixs, NULL, 0, 1.0); l_binaryWrite(psfile, "w", data, size); lept_free(data); return 0; } /* Fail silently if library is not available */ #if !HAVE_LIBJPEG if (format == IFF_JFIF_JPEG) return 0; #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBPNG if (format == IFF_PNG) return 0; #endif /* !HAVE_LIBPNG */ #if !HAVE_LIBTIFF if (format == IFF_TIFF) return 0; #endif /* !HAVE_LIBTIFF */ if (pixWriteMem(&data, &size, pixs, format)) { fprintf(stderr, "Mem write fail for format %d\n", format); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem read fail for format %d\n", format); lept_free(data); return 1; } if (format == IFF_JFIF_JPEG) { fprintf(stderr, "jpeg size = %ld\n", size); pixDisplayWrite(pixd, 1); same = TRUE; } else { pixEqual(pixs, pixd, &same); if (!same) fprintf(stderr, "Mem write/read fail for format %d\n", format); } pixDestroy(&pixd); lept_free(data); return (!same); }
main(int argc, char **argv) { l_int32 i, j, equal; PIX *pixs, *pixt, *pixd; static char mainName[] = "rasteropip_reg"; pixs = pixRead("test8.jpg"); pixt = pixCopy(NULL, pixs); /* Copy, in-place and one COLUMN at a time, from the right side to the left side. */ for (j = 0; j < 200; j++) pixRasterop(pixs, 20 + j, 20, 1, 250, PIX_SRC, pixs, 250 + j, 20); pixDisplay(pixs, 50, 50); /* Copy, in-place and one ROW at a time, from the right side to the left side. */ for (i = 0; i < 250; i++) pixRasterop(pixt, 20, 20 + i, 200, 1, PIX_SRC, pixt, 250, 20 + i); pixDisplay(pixt, 620, 50); /* Test */ pixEqual(pixs, pixt, &equal); if (equal) fprintf(stderr, "OK: images are the same\n"); else fprintf(stderr, "Error: images are different\n"); pixWrite("/tmp/junkpix.png", pixs, IFF_PNG); pixDestroy(&pixs); pixDestroy(&pixt); /* Show the mirrored border, which uses the general pixRasterop() on an image in-place. */ pixs = pixRead("test8.jpg"); pixt = pixRemoveBorder(pixs, 40); pixd = pixAddMirroredBorder(pixt, 25, 25, 25, 25); pixDisplay(pixd, 50, 550); pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixd); return 0; }
/* simple comparison function */ static void pixCompare(PIX *pix1, PIX *pix2, const char *msg1, const char *msg2) { l_int32 same; pixEqual(pix1, pix2, &same); if (same) { fprintf(stderr, "%s\n", msg1); pixDisplayWrite(pix1, 1); } else { fprintf(stderr, "%s\n", msg2); pixDisplayWrite(pix1, 1); pixDisplayWrite(pix2, 1); } return; }
/* Returns 1 on error */ static l_int32 testcomp_mem(PIX *pixs, PIX **ppixt, l_int32 index, l_int32 format) { l_int32 sameimage; PIX *pixt; pixt = *ppixt; pixEqual(pixs, pixt, &sameimage); if (!sameimage) fprintf(stderr, "Mem Write/read fail for file %d with format %d\n", index, format); pixDestroy(&pixt); *ppixt = NULL; return (!sameimage); }
void Compare(PIX *pix1, PIX *pix2, l_int32 *perror) { l_int32 same; if (!pix1 || !pix2) { fprintf(stderr, "pix not defined\n"); *perror = 1; return; } pixEqual(pix1, pix2, &same); if (same) fprintf(stderr, "OK\n"); else { fprintf(stderr, "Fail: not equal\n"); *perror = 1; } return; }
/*! * regTestComparePix() * * Input: stream (for output; use NULL to generate golden files) * argv ([0] == name of reg test) * pix1, pix2 (to be tested for equality) * index (of the pix pair test; 0-based for the reg test) * &success (<return> 0 on failure; input value on success) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This function compares two pix for equality. When called * with @fp == NULL, it records success or failure to stderr. * Otherwise, it writes on failure to @fp. * (2) This function can be called repeatedly in a single reg test. * (3) The value for @success is initialized to TRUE in the reg test * setup before this function is called for the first time. * A failure in any pix comparison is registered as a failure * of the regression test. */ l_int32 regTestComparePix(FILE *fp, char **argv, PIX *pix1, PIX *pix2, l_int32 index, l_int32 *psuccess) { l_int32 same; PROCNAME("regTestComparePix"); if (!psuccess) return ERROR_INT("&success not defined", procName, 1); if (index < 0) return ERROR_INT("index is negative", procName, 1); if (!pix1 || !pix2) return ERROR_INT("pix1 and pix2 not both defined", procName, 1); pixEqual(pix1, pix2, &same); if (!fp) { /* just output result to stderr */ if (same) fprintf(stderr, "%s: Success in comparison %d\n", argv[0], index); else fprintf(stderr, "Failure in comparison %d\n", index); return 0; } /* Record on failure */ if (!same) { fprintf(fp, "Failure in %s: pix comparison %d\n", argv[0], index); fprintf(stderr, "Failure in %s: pix comparison %d\n", argv[0], index); *psuccess = 0; } return 0; }
static l_int32 test_1bpp_bw2(L_REGPARAMS *rp) { l_int32 same; PIX *pix1, *pix2; PIXCMAP *cmap; pix1 = pixRead("feyn-fract2.tif"); cmap = pixcmapCreate(1); pixSetColormap(pix1, cmap); pixcmapAddRGBA(cmap, 255, 255, 255, 255); /* white, opaque */ pixcmapAddRGBA(cmap, 0, 0, 0, 255); /* black, opaque */ pixWrite("/tmp/regout/1bpp-bw2.png", pix1, IFF_PNG); pix2 = pixRead("/tmp/regout/1bpp-bw2.png"); pixEqual(pix1, pix2, &same); if (same) fprintf(stderr, "1bpp_bw2: success\n"); else fprintf(stderr, "1bpp_bw2: bad output\n"); pixDisplayWithTitle(pix2, 700, 400, NULL, rp->display); pixDestroy(&pix1); pixDestroy(&pix2); return same; }
main(int argc, char **argv) { char *str; char buffer1[256]; char buffer2[256]; l_int32 i, same, same2, factor1, factor2, diff, success; PIX *pixs, *pixsd, *pixt1, *pixt2, *pixt3; SEL *sel1, *sel2; static char mainName[] = "binmorph2_reg"; #if 1 pixs = pixRead("rabi.png"); pixsd = pixMorphCompSequence(pixs, "d5.5", 0); success = TRUE; for (i = 1; i < MAX_SEL_SIZE; i++) { /* Check if the size is exactly decomposable */ selectComposableSizes(i, &factor1, &factor2); diff = factor1 * factor2 - i; fprintf(stderr, "%d: (%d, %d): %d\n", i, factor1, factor2, diff); /* Carry out operations on identical sized Sels: dilation */ sprintf(buffer1, "d%d.%d", i + diff, i + diff); sprintf(buffer2, "d%d.%d", i, i); pixt1 = pixMorphSequence(pixsd, buffer1, 0); pixt2 = pixMorphCompSequence(pixsd, buffer2, 0); pixEqual(pixt1, pixt2, &same); if (i < 64) { pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0); pixEqual(pixt1, pixt3, &same2); } else { pixt3 = NULL; same2 = TRUE; } if (same && same2) writeResult(buffer1, 1); else { writeResult(buffer1, 0); success = FALSE; } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); /* ... erosion */ sprintf(buffer1, "e%d.%d", i + diff, i + diff); sprintf(buffer2, "e%d.%d", i, i); pixt1 = pixMorphSequence(pixsd, buffer1, 0); pixt2 = pixMorphCompSequence(pixsd, buffer2, 0); pixEqual(pixt1, pixt2, &same); if (i < 64) { pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0); pixEqual(pixt1, pixt3, &same2); } else { pixt3 = NULL; same2 = TRUE; } if (same && same2) writeResult(buffer1, 1); else { writeResult(buffer1, 0); success = FALSE; } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); /* ... opening */ sprintf(buffer1, "o%d.%d", i + diff, i + diff); sprintf(buffer2, "o%d.%d", i, i); pixt1 = pixMorphSequence(pixsd, buffer1, 0); pixt2 = pixMorphCompSequence(pixsd, buffer2, 0); pixEqual(pixt1, pixt2, &same); if (i < 64) { pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0); pixEqual(pixt1, pixt3, &same2); } else { pixt3 = NULL; same2 = TRUE; } if (same && same2) writeResult(buffer1, 1); else { writeResult(buffer1, 0); success = FALSE; } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); /* ... closing */ sprintf(buffer1, "c%d.%d", i + diff, i + diff); sprintf(buffer2, "c%d.%d", i, i); pixt1 = pixMorphSequence(pixsd, buffer1, 0); pixt2 = pixMorphCompSequence(pixsd, buffer2, 0); pixEqual(pixt1, pixt2, &same); if (i < 64) { pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0); pixEqual(pixt1, pixt3, &same2); } else { pixt3 = NULL; same2 = TRUE; } if (same && same2) writeResult(buffer1, 1); else { writeResult(buffer1, 0); success = FALSE; } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); } pixDestroy(&pixs); pixDestroy(&pixsd); if (success) fprintf(stderr, "\n---------- Success: no errors ----------\n"); else fprintf(stderr, "\n---------- Failure: error(s) found -----------\n"); #endif #if 0 for (i = 1; i < 400; i++) { selectComposableSizes(i, &factor1, &factor2); diff = factor1 * factor2 - i; fprintf(stderr, "%d: (%d, %d): %d\n", i, factor1, factor2, diff); selectComposableSels(i, L_HORIZ, &sel1, &sel2); selDestroy(&sel1); selDestroy(&sel2); } #endif #if 0 selectComposableSels(68, L_HORIZ, &sel1, &sel2); /* 17, 4 */ str = selPrintToString(sel2); fprintf(stderr, str); selDestroy(&sel1); selDestroy(&sel2); FREE(str); selectComposableSels(70, L_HORIZ, &sel1, &sel2); /* 10, 7 */ str = selPrintToString(sel2); selDestroy(&sel1); selDestroy(&sel2); fprintf(stderr, str); FREE(str); selectComposableSels(85, L_HORIZ, &sel1, &sel2); /* 17, 5 */ str = selPrintToString(sel2); selDestroy(&sel1); selDestroy(&sel2); fprintf(stderr, str); FREE(str); selectComposableSels(96, L_HORIZ, &sel1, &sel2); /* 12, 8 */ str = selPrintToString(sel2); selDestroy(&sel1); selDestroy(&sel2); fprintf(stderr, str); FREE(str); { SELA *sela; sela = selaAddBasic(NULL); selaWrite("/tmp/junksela.sela", sela); selaDestroy(&sela); } #endif return 0; }
main(int argc, char **argv) { #if HAVE_FMEMOPEN char psname[256]; #endif /* HAVE_FMEMOPEN */ char *tempname; l_uint8 *data; l_int32 i, d, n, success, failure, same; l_int32 w, h, bps, spp; size_t size, nbytes; PIX *pix1, *pix2, *pix4, *pix8, *pix16, *pix32; PIX *pix, *pixt, *pixd; PIXA *pixa; L_REGPARAMS *rp; #if !HAVE_LIBJPEG fprintf(stderr, "Omitting libjpeg tests in ioformats_reg\n"); #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBTIFF fprintf(stderr, "Omitting libtiff tests in ioformats_reg\n"); #endif /* !HAVE_LIBTIFF */ #if !HAVE_LIBPNG || !HAVE_LIBZ fprintf(stderr, "Omitting libpng tests in ioformats_reg\n"); #endif /* !HAVE_LIBPNG || !HAVE_LIBZ */ if (regTestSetup(argc, argv, &rp)) return 1; /* --------- Part 1: Test all lossless formats for r/w to file ---------*/ failure = FALSE; success = TRUE; fprintf(stderr, "Test bmp 1 bpp file:\n"); if (ioFormatTest(BMP_FILE)) success = FALSE; #if HAVE_LIBTIFF fprintf(stderr, "\nTest other 1 bpp file:\n"); if (ioFormatTest(FILE_1BPP)) success = FALSE; #endif /* HAVE_LIBTIFF */ #if HAVE_LIBPNG fprintf(stderr, "\nTest 2 bpp file:\n"); if (ioFormatTest(FILE_2BPP)) success = FALSE; fprintf(stderr, "\nTest 2 bpp file with cmap:\n"); if (ioFormatTest(FILE_2BPP_C)) success = FALSE; fprintf(stderr, "\nTest 4 bpp file:\n"); if (ioFormatTest(FILE_4BPP)) success = FALSE; fprintf(stderr, "\nTest 4 bpp file with cmap:\n"); if (ioFormatTest(FILE_4BPP_C)) success = FALSE; fprintf(stderr, "\nTest 8 bpp grayscale file with cmap:\n"); if (ioFormatTest(FILE_8BPP_1)) success = FALSE; fprintf(stderr, "\nTest 8 bpp color file with cmap:\n"); if (ioFormatTest(FILE_8BPP_2)) success = FALSE; #endif /* HAVE_LIBPNG */ #if HAVE_LIBJPEG fprintf(stderr, "\nTest 8 bpp file without cmap:\n"); if (ioFormatTest(FILE_8BPP_3)) success = FALSE; #endif /* HAVE_LIBJPEG */ #if HAVE_LIBTIFF fprintf(stderr, "\nTest 16 bpp file:\n"); if (ioFormatTest(FILE_16BPP)) success = FALSE; #endif /* HAVE_LIBTIFF */ #if HAVE_LIBJPEG fprintf(stderr, "\nTest 32 bpp file:\n"); if (ioFormatTest(FILE_32BPP)) success = FALSE; #endif /* HAVE_LIBJPEG */ if (success) fprintf(stderr, "\n ********** Success on all i/o format tests *********\n"); else fprintf(stderr, "\n ******* Failure on at least one i/o format test ******\n"); if (!success) failure = TRUE; /* ------------------ Part 2: Test tiff r/w to file ------------------- */ #if !HAVE_LIBTIFF goto part6; #endif /* !HAVE_LIBTIFF */ fprintf(stderr, "\nTest tiff r/w and format extraction\n"); pixa = pixaCreate(6); pix1 = pixRead(BMP_FILE); pix2 = pixConvert1To2(NULL, pix1, 3, 0); pix4 = pixConvert1To4(NULL, pix1, 15, 0); pix16 = pixRead(FILE_16BPP); fprintf(stderr, "Input format: %d\n", pixGetInputFormat(pix16)); pix8 = pixConvert16To8(pix16, 1); pix32 = pixRead(FILE_32BPP); pixaAddPix(pixa, pix1, L_INSERT); pixaAddPix(pixa, pix2, L_INSERT); pixaAddPix(pixa, pix4, L_INSERT); pixaAddPix(pixa, pix8, L_INSERT); pixaAddPix(pixa, pix16, L_INSERT); pixaAddPix(pixa, pix32, L_INSERT); n = pixaGetCount(pixa); success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); fprintf(stderr, "%d bpp\n", d); if (i == 0) { /* 1 bpp */ pixWrite("/tmp/junkg3.tif", pix, IFF_TIFF_G3); pixWrite("/tmp/junkg4.tif", pix, IFF_TIFF_G4); pixWrite("/tmp/junkrle.tif", pix, IFF_TIFF_RLE); pixWrite("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS); if (testcomp("/tmp/junkg3.tif", pix, IFF_TIFF_G3)) success = FALSE; if (testcomp("/tmp/junkg4.tif", pix, IFF_TIFF_G4)) success = FALSE; if (testcomp("/tmp/junkrle.tif", pix, IFF_TIFF_RLE)) success = FALSE; if (testcomp("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS)) success = FALSE; } pixWrite("/tmp/junklzw.tif", pix, IFF_TIFF_LZW); pixWrite("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP); pixWrite("/tmp/junknon.tif", pix, IFF_TIFF); if (testcomp("/tmp/junklzw.tif", pix, IFF_TIFF_LZW)) success = FALSE; if (testcomp("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP)) success = FALSE; if (testcomp("/tmp/junknon.tif", pix, IFF_TIFF)) success = FALSE; pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on tiff r/w to file *********\n\n"); else fprintf(stderr, "\n ******* Failure on at least one tiff r/w to file ******\n\n"); if (!success) failure = TRUE; /* ------------------ Part 3: Test tiff r/w to memory ----------------- */ success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); fprintf(stderr, "%d bpp\n", d); if (i == 0) { /* 1 bpp */ pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G3); nbytes = nbytesInFile("/tmp/junkg3.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G3)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G4); nbytes = nbytesInFile("/tmp/junkg4.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G4)) success = FALSE; readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp, NULL, NULL, NULL); fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp); lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_RLE); nbytes = nbytesInFile("/tmp/junkrle.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_RLE)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_PACKBITS); nbytes = nbytesInFile("/tmp/junkpb.tif"); fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_PACKBITS)) success = FALSE; lept_free(data); } pixWriteMemTiff(&data, &size, pix, IFF_TIFF_LZW); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_LZW)) success = FALSE; lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF_ZIP); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF_ZIP)) success = FALSE; readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp, NULL, NULL, NULL); fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp); lept_free(data); pixWriteMemTiff(&data, &size, pix, IFF_TIFF); pixt = pixReadMemTiff(data, size, 0); if (testcomp_mem(pix, &pixt, i, IFF_TIFF)) success = FALSE; lept_free(data); pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on tiff r/w to memory *********\n\n"); else fprintf(stderr, "\n ******* Failure on at least one tiff r/w to memory ******\n\n"); if (!success) failure = TRUE; /* ---------------- Part 4: Test non-tiff r/w to memory ---------------- */ #if HAVE_FMEMOPEN pixDisplayWrite(NULL, -1); success = TRUE; for (i = 0; i < n; i++) { pix = pixaGetPix(pixa, i, L_CLONE); d = pixGetDepth(pix); sprintf(psname, "/tmp/junkps.%d", d); fprintf(stderr, "%d bpp\n", d); if (d != 16) { if (test_writemem(pix, IFF_PNG, NULL)) success = FALSE; if (test_writemem(pix, IFF_BMP, NULL)) success = FALSE; } if (test_writemem(pix, IFF_PNM, NULL)) success = FALSE; if (test_writemem(pix, IFF_PS, psname)) success = FALSE; if (d == 8 || d == 32) if (test_writemem(pix, IFF_JFIF_JPEG, NULL)) success = FALSE; pixDestroy(&pix); } if (success) fprintf(stderr, "\n ********** Success on non-tiff r/w to memory *********\n\n"); else fprintf(stderr, "\n **** Failure on at least one non-tiff r/w to memory *****\n\n"); if (!success) failure = TRUE; #else fprintf(stderr, "\n ***** Non-tiff r/w to memory not enabled *****\n\n"); #endif /* HAVE_FMEMOPEN */ pixaDestroy(&pixa); /* ------------ Part 5: Test multipage tiff r/w to memory ------------ */ /* Make a multipage tiff file, and read it back into memory */ success = TRUE; pix = pixRead("feyn.tif"); pixa = pixaSplitPix(pix, 3, 3, 0, 0); for (i = 0; i < 9; i++) { pixt = pixaGetPix(pixa, i, L_CLONE); if (i == 0) pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "w"); else pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "a"); pixDestroy(&pixt); } data = l_binaryRead("/tmp/junktiffmpage.tif", &nbytes); pixaDestroy(&pixa); /* Read the individual pages from memory to a pix */ pixa = pixaCreate(9); for (i = 0; i < 9; i++) { pixt = pixReadMemTiff(data, nbytes, i); pixaAddPix(pixa, pixt, L_INSERT); } lept_free(data); /* Un-tile the pix in the pixa back to the original image */ pixt = pixaDisplayUnsplit(pixa, 3, 3, 0, 0); pixaDestroy(&pixa); /* Clip to foreground to remove any extra rows or columns */ pixClipToForeground(pix, &pix1, NULL); pixClipToForeground(pixt, &pix2, NULL); pixEqual(pix1, pix2, &same); if (same) fprintf(stderr, "\n ******* Success on tiff multipage read from memory ******\n\n"); else fprintf(stderr, "\n ******* Failure on tiff multipage read from memory ******\n\n"); if (!same) failure = TRUE; pixDestroy(&pix); pixDestroy(&pixt); pixDestroy(&pix1); pixDestroy(&pix2); /* ------------ Part 6: Test 24 bpp writing ------------ */ #if !HAVE_LIBTIFF part6: #endif /* !HAVE_LIBTIFF */ #if !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF goto finish; #endif /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */ /* Generate a 24 bpp (not 32 bpp !!) rgb pix and write it out */ success = TRUE; pix = pixRead("marge.jpg"); pixt = make_24_bpp_pix(pix); pixWrite("/tmp/junk24.png", pixt, IFF_PNG); pixWrite("/tmp/junk24.jpg", pixt, IFF_JFIF_JPEG); pixWrite("/tmp/junk24.tif", pixt, IFF_TIFF); pixd = pixRead("/tmp/junk24.png"); pixEqual(pix, pixd, &same); if (!same) success = FALSE; pixDestroy(&pixd); pixd = pixRead("/tmp/junk24.jpg"); regTestCompareSimilarPix(rp, pix, pixd, 10, 0.0002, 0); pixDestroy(&pixd); pixd = pixRead("/tmp/junk24.tif"); pixEqual(pix, pixd, &same); if (!same) success = FALSE; pixDestroy(&pixd); if (success) fprintf(stderr, "\n ******* Success on 24 bpp rgb writing *******\n\n"); else fprintf(stderr, "\n ******* Failure on 24 bpp rgb writing *******\n\n"); if (!success) failure = TRUE; pixDestroy(&pix); pixDestroy(&pixt); /* -------------- Part 7: Read header information -------------- */ success = TRUE; if (get_header_data(FILE_1BPP, IFF_TIFF_G4)) success = FALSE; if (get_header_data(FILE_2BPP, IFF_PNG)) success = FALSE; if (get_header_data(FILE_2BPP_C, IFF_PNG)) success = FALSE; if (get_header_data(FILE_4BPP, IFF_PNG)) success = FALSE; if (get_header_data(FILE_4BPP_C, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_1, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_2, IFF_PNG)) success = FALSE; if (get_header_data(FILE_8BPP_3, IFF_JFIF_JPEG)) success = FALSE; if (get_header_data(FILE_16BPP, IFF_TIFF_ZIP)) success = FALSE; if (get_header_data(FILE_32BPP, IFF_JFIF_JPEG)) success = FALSE; #if HAVE_FMEMOPEN pix = pixRead(FILE_8BPP_1); tempname = genTempFilename((const char *)"/tmp", (const char *)".pnm", 1, 1); pixWrite(tempname, pix, IFF_PNM); if (get_header_data(tempname, IFF_PNM)) success = FALSE; pixDestroy(&pix); lept_free(tempname); #endif /* HAVE_FMEMOPEN */ pix = pixRead(FILE_1BPP); tempname = genTempFilename((const char *)"/tmp", (const char *)".tif", 1, 1); pixWrite(tempname, pix, IFF_TIFF_G3); if (get_header_data(tempname, IFF_TIFF_G3)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_G4); if (get_header_data(tempname, IFF_TIFF_G4)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_PACKBITS); if (get_header_data(tempname, IFF_TIFF_PACKBITS)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_RLE); if (get_header_data(tempname, IFF_TIFF_RLE)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_LZW); if (get_header_data(tempname, IFF_TIFF_LZW)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF_ZIP); if (get_header_data(tempname, IFF_TIFF_ZIP)) success = FALSE; pixWrite(tempname, pix, IFF_TIFF); if (get_header_data(tempname, IFF_TIFF)) success = FALSE; pixDestroy(&pix); lept_free(tempname); if (success) fprintf(stderr, "\n ******* Success on reading headers *******\n\n"); else fprintf(stderr, "\n ******* Failure on reading headers *******\n\n"); if (!success) failure = TRUE; #if !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF finish: #endif /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */ if (!failure) fprintf(stderr, " ******* Success on all tests *******\n\n"); else fprintf(stderr, " ******* Failure on at least one test *******\n\n"); return regTestCleanup(rp); }
main(int argc, char **argv) { l_int32 i, j, w1, h1, w2, h2, w, h, same; BOX *box1, *box2; PIX *pixs, *pixs1, *pixs2, *pix1, *pix2; PIX *pixg, *pixg1, *pixg2, *pixc2, *pixbl, *pixd; PIXA *pixa; static char mainName[] = "blend2_reg"; /* --- Set up the 8 bpp blending image --- */ pixg = pixCreate(660, 500, 8); for (i = 0; i < 500; i++) for (j = 0; j < 660; j++) pixSetPixel(pixg, j, i, (l_int32)(0.775 * j) % 256); /* --- Set up the initial color images to be blended together --- */ pixs1 = pixRead("wyom.jpg"); pixs2 = pixRead("fish24.jpg"); pixGetDimensions(pixs1, &w1, &h1, NULL); pixGetDimensions(pixs2, &w2, &h2, NULL); h = L_MIN(h1, h2); w = L_MIN(w1, w2); box1 = boxCreate(0, 0, w1, h1); box2 = boxCreate(0, 300, 660, 500); pix1 = pixClipRectangle(pixs1, box1, NULL); pix2 = pixClipRectangle(pixs2, box2, NULL); pixDestroy(&pixs1); pixDestroy(&pixs2); boxDestroy(&box1); boxDestroy(&box2); /* --- Blend 2 rgb images --- */ pixa = pixaCreate(0); pixSaveTiled(pixg, pixa, 1, 1, 40, 32); pixd = pixBlendWithGrayMask(pix1, pix2, pixg, 50, 50); pixSaveTiled(pix1, pixa, 1, 1, 40, 32); pixSaveTiled(pix2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* --- Blend 2 grayscale images --- */ pixg1 = pixConvertRGBToLuminance(pix1); pixg2 = pixConvertRGBToLuminance(pix2); pixd = pixBlendWithGrayMask(pixg1, pixg2, pixg, 50, 50); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixg2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixg1); pixDestroy(&pixg2); pixDestroy(&pixd); /* --- Blend a colormap image and an rgb image --- */ pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); pixd = pixBlendWithGrayMask(pix1, pixc2, pixg, 50, 50); pixSaveTiled(pix1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixc2); pixDestroy(&pixd); /* --- Blend a colormap image and a grayscale image --- */ pixg1 = pixConvertRGBToLuminance(pix1); pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, 50, 50); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, -100, -100); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); pixDestroy(&pixg1); pixDestroy(&pixc2); /* --- Test png read/write with alpha channel --- */ /* First make pixs1, using pixg as the alpha channel */ pixs = pixRead("fish24.jpg"); box1 = boxCreate(0, 300, 660, 500); pixs1 = pixClipRectangle(pixs, box1, NULL); pixSaveTiled(pixs1, pixa, 1, 1, 40, 32); pixSetRGBComponent(pixs1, pixg, L_ALPHA_CHANNEL); /* To see the alpha channel, blend with a black image */ pixbl = pixCreate(660, 500, 32); pixd = pixBlendWithGrayMask(pixbl, pixs1, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* Write out the RGBA image and read it back */ l_pngSetWriteAlpha(1); pixWrite("/tmp/junkpixs1.png", pixs1, IFF_PNG); l_pngSetStripAlpha(0); pixs2 = pixRead("/tmp/junkpixs1.png"); /* Make sure that the alpha channel image hasn't changed */ pixg2 = pixGetRGBComponent(pixs2, L_ALPHA_CHANNEL); pixEqual(pixg, pixg2, &same); if (same) fprintf(stderr, "PNG with alpha read/write OK\n"); else fprintf(stderr, "PNG with alpha read/write failed\n"); /* Blend again with a black image */ pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* Blend with a white image */ pixSetAll(pixbl); pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); l_pngSetWriteAlpha(0); /* reset to default */ l_pngSetStripAlpha(1); /* reset to default */ pixDestroy(&pixbl); pixDestroy(&pixs); pixDestroy(&pixs1); pixDestroy(&pixs2); pixDestroy(&pixg2); boxDestroy(&box1); /* --- Display results --- */ pixd = pixaDisplay(pixa, 0, 0); pixDisplay(pixd, 100, 100); pixWrite("/tmp/junkblend2.jpg", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixg); pixDestroy(&pix1); pixDestroy(&pix2); return 0; }
int main(int argc, char **argv) { l_int32 i, j, w, h, same; l_float32 t, t1, t2; GPLOT *gplot; NUMA *nax, *nay1, *nay2; PIX *pixs, *pixd, *pixt1, *pixt2, *pixt3, *pixt4; PIXA *pixa; static char mainName[] = "rank_reg"; if (argc != 1) return ERROR_INT(" Syntax: rank_reg", mainName, 1); if ((pixs = pixRead("lucasta.150.jpg")) == NULL) return ERROR_INT("pixs not made", mainName, 1); pixGetDimensions(pixs, &w, &h, NULL); startTimer(); pixd = pixRankFilterGray(pixs, 15, 15, 0.4); t = stopTimer(); fprintf(stderr, "Time = %7.3f sec\n", t); fprintf(stderr, "MPix/sec: %7.3f\n", 0.000001 * w * h / t); pixDisplay(pixs, 0, 200); pixDisplay(pixd, 600, 200); pixWrite("/tmp/filter.png", pixd, IFF_PNG); pixDestroy(&pixd); /* Get results for dilation */ startTimer(); pixt1 = pixDilateGray(pixs, 15, 15); t = stopTimer(); fprintf(stderr, "Dilation time = %7.3f sec\n", t); /* Get results for erosion */ pixt2 = pixErodeGray(pixs, 15, 15); /* Get results using the rank filter for rank = 0.0 and 1.0. * Don't use 0.0 or 1.0, because those are dispatched * automatically to erosion and dilation! */ pixt3 = pixRankFilterGray(pixs, 15, 15, 0.0001); pixt4 = pixRankFilterGray(pixs, 15, 15, 0.9999); /* Compare */ pixEqual(pixt1, pixt4, &same); if (same) fprintf(stderr, "Correct: dilation results same as rank 1.0\n"); else fprintf(stderr, "Error: dilation results differ from rank 1.0\n"); pixEqual(pixt2, pixt3, &same); if (same) fprintf(stderr, "Correct: erosion results same as rank 0.0\n"); else fprintf(stderr, "Error: erosion results differ from rank 0.0\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); fprintf(stderr, "\n----------------------------------------\n"); fprintf(stderr, "The next part takes about 30 seconds\n"); fprintf(stderr, "----------------------------------------\n\n"); nax = numaMakeSequence(1, 1, SIZE); nay1 = numaCreate(SIZE); nay2 = numaCreate(SIZE); gplot = gplotCreate("/tmp/rankroot", GPLOT_X11, "sec/MPix vs filter size", "size", "time"); for (i = 1; i <= SIZE; i++) { t1 = t2 = 0.0; for (j = 0; j < 5; j++) { startTimer(); pixt1 = pixRankFilterGray(pixs, i, SIZE + 1, 0.5); t1 += stopTimer(); pixDestroy(&pixt1); startTimer(); pixt1 = pixRankFilterGray(pixs, SIZE + 1, i, 0.5); t2 += stopTimer(); if (j == 0) pixDisplayWrite(pixt1, 1); pixDestroy(&pixt1); } numaAddNumber(nay1, 1000000. * t1 / (5. * w * h)); numaAddNumber(nay2, 1000000. * t2 / (5. * w * h)); } gplotAddPlot(gplot, nax, nay1, GPLOT_LINES, "vertical"); gplotAddPlot(gplot, nax, nay2, GPLOT_LINES, "horizontal"); gplotMakeOutput(gplot); gplotDestroy(&gplot); /* Display tiled */ pixa = pixaReadFiles("/tmp/display", "file"); pixd = pixaDisplayTiledAndScaled(pixa, 8, 250, 5, 0, 25, 2); pixWrite("/tmp/tiles.jpg", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixs); pixDisplayWrite(NULL, -1); /* clear out */ pixs = pixRead("test8.jpg"); for (i = 1; i <= 4; i++) { pixt1 = pixScaleGrayRank2(pixs, i); pixDisplay(pixt1, 300 * (i - 1), 100); pixDestroy(&pixt1); } pixDestroy(&pixs); pixs = pixRead("test24.jpg"); pixt1 = pixConvertRGBToLuminance(pixs); pixt2 = pixScale(pixt1, 1.5, 1.5); for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { pixt3 = pixScaleGrayRankCascade(pixt2, i, j, 0, 0); pixDisplayWrite(pixt3, 1); pixDestroy(&pixt3); } } pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixs); pixDisplayMultiple("/tmp/display/file*"); return 0; }
/*! * regTestCheckFile() * * Input: rp (regtest parameters) * localname (name of output file from reg test) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This function does one of three things, depending on the mode: * * "generate": makes a "golden" file as a copy @localname. * * "compare": compares @localname contents with the golden file * * "display": makes the @localname file but does no comparison * (2) The canonical format of the golden filenames is: * /tmp/golden/<root of main name>_golden.<index>.<ext of localname> * e.g., * /tmp/golden/maze_golden.0.png * It is important to add an extension to the local name, because * the extension is added to the name of the golden file. */ l_int32 regTestCheckFile(L_REGPARAMS *rp, const char *localname) { char *ext; char namebuf[256]; l_int32 ret, same, format; PIX *pix1, *pix2; PROCNAME("regTestCheckFile"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (!localname) { rp->success = FALSE; return ERROR_INT("local name not defined", procName, 1); } if (rp->mode != L_REG_GENERATE && rp->mode != L_REG_COMPARE && rp->mode != L_REG_DISPLAY) { rp->success = FALSE; return ERROR_INT("invalid mode", procName, 1); } rp->index++; /* If display mode, no generation and no testing */ if (rp->mode == L_REG_DISPLAY) return 0; /* Generate the golden file name; used in 'generate' and 'compare' */ splitPathAtExtension(localname, NULL, &ext); snprintf(namebuf, sizeof(namebuf), "/tmp/golden/%s_golden.%02d%s", rp->testname, rp->index, ext); FREE(ext); /* Generate mode. No testing. */ if (rp->mode == L_REG_GENERATE) { /* Save the file as a golden file */ ret = fileCopy(localname, namebuf); #if 0 /* Enable for details on writing of golden files */ if (!ret) { char *local = genPathname(localname, NULL); char *golden = genPathname(namebuf, NULL); L_INFO("Copy: %s to %s\n", procName, local, golden); FREE(local); FREE(golden); } #endif return ret; } /* Compare mode: test and record on failure. GIF compression * is lossless for images with up to 8 bpp (but not for RGB * because it must generate a 256 color palette). Although * the read/write cycle for GIF is idempotent in the image * pixels for bpp <= 8, it is not idempotent in the actual * file bytes. Tests comparing file bytes before and after * a GIF read/write cycle will fail. So for GIF we uncompress * the two images and compare the actual pixels. From my tests, * PNG, in addition to being lossless, is idempotent in file * bytes on read/write, so comparing the pixels is not necessary. * (It also increases the regression test time by an an average * of about 8%.) JPEG is lossy and not idempotent in the image * pixels, so no tests are constructed that would require it. */ findFileFormat(localname, &format); if (format == IFF_GIF) { same = 0; pix1 = pixRead(localname); pix2 = pixRead(namebuf); pixEqual(pix1, pix2, &same); pixDestroy(&pix1); pixDestroy(&pix2); } else { filesAreIdentical(localname, namebuf, &same); } if (!same) { fprintf(rp->fp, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, localname, namebuf); fprintf(stderr, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, localname, namebuf); rp->success = FALSE; } return 0; }
/*! * pixThinGeneral() * * Input: pixs (1 bpp) * type (L_THIN_FG, L_THIN_BG) * sela (of Sels for parallel composite HMTs) * maxiters (max number of iters allowed; use 0 to iterate * until completion) * Return: pixd, or null on error * * Notes: * (1) See notes in pixThin(). That function chooses among * the best of the Sels for thinning. * (2) This is a general function that takes a Sela of HMTs * that are used in parallel for thinning from each * of four directions. One iteration consists of four * such parallel thins. */ PIX * pixThinGeneral(PIX *pixs, l_int32 type, SELA *sela, l_int32 maxiters) { l_int32 i, j, r, nsels, same; PIXA *pixahmt; PIX **pixhmt; /* array owned by pixahmt; do not destroy! */ PIX *pixd, *pixt; SEL *sel, *selr; PROCNAME("pixThinGeneral"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 1) return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, NULL); if (type != L_THIN_FG && type != L_THIN_BG) return (PIX *)ERROR_PTR("invalid fg/bg type", procName, NULL); if (!sela) return (PIX *)ERROR_PTR("sela not defined", procName, NULL); if (maxiters == 0) maxiters = 10000; /* Set up array of temp pix to hold hmts */ nsels = selaGetCount(sela); pixahmt = pixaCreate(nsels); for (i = 0; i < nsels; i++) { pixt = pixCreateTemplate(pixs); pixaAddPix(pixahmt, pixt, L_INSERT); } pixhmt = pixaGetPixArray(pixahmt); if (!pixhmt) return (PIX *)ERROR_PTR("pixhmt array not made", procName, NULL); #if DEBUG_SELS pixt = selaDisplayInPix(sela, 35, 3, 15, 4); pixDisplayWithTitle(pixt, 100, 100, "allsels", 1); pixDestroy(&pixt); #endif /* DEBUG_SELS */ /* Set up initial image for fg thinning */ if (type == L_THIN_FG) pixd = pixCopy(NULL, pixs); else /* bg thinning */ pixd = pixInvert(NULL, pixs); /* Thin the fg, with up to maxiters iterations */ for (i = 0; i < maxiters; i++) { pixt = pixCopy(NULL, pixd); /* test for completion */ for (r = 0; r < 4; r++) { /* over 90 degree rotations of Sels */ for (j = 0; j < nsels; j++) { /* over individual sels in sela */ sel = selaGetSel(sela, j); /* not a copy */ selr = selRotateOrth(sel, r); pixHMT(pixhmt[j], pixd, selr); selDestroy(&selr); if (j > 0) pixOr(pixhmt[0], pixhmt[0], pixhmt[j]); /* accum result */ } pixSubtract(pixd, pixd, pixhmt[0]); /* remove result */ } pixEqual(pixd, pixt, &same); pixDestroy(&pixt); if (same) { L_INFO("%d iterations to completion\n", procName, i); break; } } if (type == L_THIN_BG) pixInvert(pixd, pixd); pixaDestroy(&pixahmt); return pixd; }
int main(int argc, char **argv) { char *filein, *fileout; l_int32 i, w, h, liney, linex, same; l_float32 angle, deg2rad; PIX *pixt1, *pixt2, *pixs, *pixd; static char mainName[] = "sheartest"; if (argc != 4) return ERROR_INT(" Syntax: sheartest filein angle fileout", mainName, 1); /* Compare in-place H shear with H shear to a new pix */ pixt1 = pixRead("marge.jpg"); pixGetDimensions(pixt1, &w, &h, NULL); pixt2 = pixHShear(NULL, pixt1, (l_int32)(0.3 * h), 0.17, L_BRING_IN_WHITE); pixHShearIP(pixt1, (l_int32)(0.3 * h), 0.17, L_BRING_IN_WHITE); pixEqual(pixt1, pixt2, &same); if (same) fprintf(stderr, "Correct for H shear\n"); else fprintf(stderr, "Error for H shear\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Compare in-place V shear with V shear to a new pix */ pixt1 = pixRead("marge.jpg"); pixGetDimensions(pixt1, &w, &h, NULL); pixt2 = pixVShear(NULL, pixt1, (l_int32)(0.3 * w), 0.17, L_BRING_IN_WHITE); pixVShearIP(pixt1, (l_int32)(0.3 * w), 0.17, L_BRING_IN_WHITE); pixEqual(pixt1, pixt2, &same); if (same) fprintf(stderr, "Correct for V shear\n"); else fprintf(stderr, "Error for V shear\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); filein = argv[1]; angle = atof(argv[2]); fileout = argv[3]; deg2rad = 3.1415926535 / 180.; if ((pixs = pixRead(filein)) == NULL) return ERROR_INT("pix not made", mainName, 1); pixGetDimensions(pixs, &w, &h, NULL); #if 0 /* Select an operation from this list ... * ------------------------------------------ pixd = pixHShear(NULL, pixs, liney, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixVShear(NULL, pixs, linex, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixHShearCorner(NULL, pixs, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixVShearCorner(NULL, pixs, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixHShearCenter(NULL, pixs, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixVShearCenter(NULL, pixs, deg2rad * angle, L_BRING_IN_WHITE); pixHShearIP(pixs, liney, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixs; pixVShearIP(pixs, linex, deg2rad * angle, L_BRING_IN_WHITE); pixd = pixs; pixRasteropHip(pixs, 0, h/3, -50, L_BRING_IN_WHITE); pixd = pixs; pixRasteropVip(pixs, 0, w/3, -50, L_BRING_IN_WHITE); pixd = pixs; * ------------------------------------------ * ... and use it in the following: */ pixd = pixHShear(NULL, pixs, liney, deg2rad * angle, L_BRING_IN_WHITE); pixWrite(fileout, pixd, IFF_PNG); pixDisplay(pixd, 50, 50); pixDestroy(&pixd); #endif #if 0 /* Do a horizontal shear about a line */ for (i = 0; i < NTIMES; i++) { liney = i * h / (NTIMES - 1); if (liney >= h) liney = h - 1; pixd = pixHShear(NULL, pixs, liney, deg2rad * angle, L_BRING_IN_WHITE); pixDisplay(pixd, 50 + 10 * i, 50 + 10 * i); pixDestroy(&pixd); } #endif #if 0 /* Do a vertical shear about a line */ for (i = 0; i < NTIMES; i++) { linex = i * w / (NTIMES - 1); if (linex >= w) linex = w - 1; pixd = pixVShear(NULL, pixs, linex, deg2rad * angle, L_BRING_IN_WHITE); pixDisplay(pixd, 50 + 10 * i, 50 + 10 * i); pixDestroy(&pixd); } #endif #if 0 /* Do a horizontal in-place shear about a line */ pixSetPadBits(pixs, 0); for (i = 0; i < NTIMES; i++) { pixd = pixCopy(NULL, pixs); liney = i * h / (NTIMES - 1); if (liney >= h) liney = h - 1; pixHShearIP(pixd, liney, deg2rad * angle, L_BRING_IN_WHITE); pixDisplay(pixd, 50 + 10 * i, 50 + 10 * i); pixDestroy(&pixd); } #endif #if 0 /* Do a vertical in-place shear about a line */ for (i = 0; i < NTIMES; i++) { pixd = pixCopy(NULL, pixs); linex = i * w / (NTIMES - 1); if (linex >= w) linex = w - 1; pixVShearIP(pixd, linex, deg2rad * angle, L_BRING_IN_WHITE); pixDisplay(pixd, 50 + 10 * i, 50 + 10 * i); pixDestroy(&pixd); } #endif pixDestroy(&pixs); return 0; }
main(int argc, char **argv) { char *errorstr; l_int32 same, error; PIX *pixs1, *pixs2, *pixs4, *pixs8, *pixs16, *pixs32, *pixd; PIX *pixc2, *pixc4, *pixc8; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; PIXCMAP *cmap; SARRAY *sa; static char mainName[] = "convert_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: convert_rt", mainName, 1)); if ((pixs1 = pixRead("test1.png")) == NULL) exit(ERROR_INT("pixs1 not made", mainName, 1)); if ((pixs2 = pixRead("dreyfus2.png")) == NULL) exit(ERROR_INT("pixs2 not made", mainName, 1)); if ((pixc2 = pixRead("weasel2.4c.png")) == NULL) exit(ERROR_INT("pixc2 not made", mainName, 1)); if ((pixs4 = pixRead("weasel4.16g.png")) == NULL) exit(ERROR_INT("pixs4 not made", mainName, 1)); if ((pixc4 = pixRead("weasel4.11c.png")) == NULL) exit(ERROR_INT("pixc4 not made", mainName, 1)); if ((pixs8 = pixRead("karen8.jpg")) == NULL) exit(ERROR_INT("pixs8 not made", mainName, 1)); if ((pixc8 = pixRead("weasel8.240c.png")) == NULL) exit(ERROR_INT("pixc8 not made", mainName, 1)); if ((pixs16 = pixRead("test16.tif")) == NULL) exit(ERROR_INT("pixs16 not made", mainName, 1)); if ((pixs32 = pixRead("marge.jpg")) == NULL) exit(ERROR_INT("pixs32 not made", mainName, 1)); error = FALSE; sa = sarrayCreate(0); /* Conversion: 1 bpp --> 8 bpp --> 1 bpp */ pixt1 = pixConvertTo8(pixs1, FALSE); pixt2 = pixThreshold8(pixt1, 1, 0, 0); pixEqual(pixs1, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs1, 100, 100, "1 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "1 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 1 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 1 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 2 bpp --> 8 bpp --> 2 bpp */ /* Conversion: 2 bpp cmap --> 8 bpp cmap --> 2 bpp cmap */ pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixThreshold8(pixt1, 2, 4, 0); pixt3 = pixConvertTo8(pixt2, FALSE); pixt4 = pixThreshold8(pixt3, 2, 4, 0); pixEqual(pixt2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "2 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp\n"); pixt5 = pixConvertTo8(pixs2, TRUE); pixt6 = pixThreshold8(pixt5, 2, 4, 1); pixEqual(pixs2, pixt6, &same); if (!same) { pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt6, 500, 100, "2 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 8 bpp; cmap", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 8 bpp; cmap\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); /* Conversion: 4 bpp --> 8 bpp --> 4 bpp */ /* Conversion: 4 bpp cmap --> 8 bpp cmap --> 4 bpp cmap */ pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixThreshold8(pixt1, 4, 16, 0); pixt3 = pixConvertTo8(pixt2, FALSE); pixt4 = pixThreshold8(pixt3, 4, 16, 0); pixEqual(pixt2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "4 bpp, no cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp\n"); pixt5 = pixConvertTo8(pixs4, TRUE); pixt6 = pixThreshold8(pixt5, 4, 16, 1); pixEqual(pixs4, pixt6, &same); if (!same) { pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt6, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 8 bpp, cmap", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 8 bpp; cmap\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); /* Conversion: 2 bpp cmap --> 2 bpp --> 2 bpp cmap --> 2 bpp */ pixt1 = pixRemoveColormap(pixs2, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixConvertGrayToColormap(pixt1); pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE); pixt4 = pixThresholdTo2bpp(pixt3, 4, 1); pixEqual(pixt1, pixt4, &same); if (!same) { pixDisplayWithTitle(pixs2, 100, 100, "2 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "2 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 2 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 2 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 4 bpp cmap --> 4 bpp --> 4 bpp cmap --> 4 bpp */ pixt1 = pixRemoveColormap(pixs4, REMOVE_CMAP_TO_GRAYSCALE); pixt2 = pixConvertGrayToColormap(pixt1); pixt3 = pixRemoveColormap(pixt2, REMOVE_CMAP_TO_GRAYSCALE); pixt4 = pixThresholdTo4bpp(pixt3, 16, 1); pixEqual(pixt1, pixt4, &same); if (!same) { pixDisplayWithTitle(pixs4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 4 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 4 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 8 bpp --> 8 bpp cmap --> 8 bpp */ pixt1 = pixConvertTo8(pixs8, TRUE); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixt1, 100, 100, "8 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp, no cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 2 bpp cmap --> 32 bpp --> 2 bpp cmap */ pixt1 = pixConvertTo8(pixc2, TRUE); pixt2 = pixConvertTo32(pixt1); pixt3 = pixConvertTo32(pixc2); pixEqual(pixt2, pixt3, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG); pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp ==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n"); cmap = pixGetColormap(pixc2); pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE); pixEqual(pixc2, pixt4, &same); if (!same) { pixDisplayWithTitle(pixc2, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 2 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 2 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 4 bpp cmap --> 32 bpp --> 4 bpp cmap */ pixt1 = pixConvertTo8(pixc4, TRUE); pixt2 = pixConvertTo32(pixt1); pixt3 = pixConvertTo32(pixc4); pixEqual(pixt2, pixt3, &same); if (!same) { pixDisplayWithTitle(pixt2, 100, 100, "32 bpp", DFLAG); pixDisplayWithTitle(pixt3, 500, 100, "32 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp ==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n"); cmap = pixGetColormap(pixc4); pixt4 = pixOctcubeQuantFromCmap(pixt3, cmap, 2, 4, L_EUCLIDEAN_DISTANCE); pixEqual(pixc4, pixt4, &same); if (!same) { pixDisplayWithTitle(pixc4, 100, 100, "4 bpp, cmap", DFLAG); pixDisplayWithTitle(pixt4, 500, 100, "4 bpp, cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 4 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 4 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Conversion: 8 bpp --> 32 bpp --> 8 bpp */ pixt1 = pixConvertTo32(pixs8); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 32 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 8 bpp --> 16 bpp --> 8 bpp */ pixt1 = pixConvert8To16(pixs8, 8); pixt2 = pixConvertTo8(pixt1, FALSE); pixEqual(pixs8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs8, 100, 100, "8 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp <==> 16 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 16 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 16 bpp --> 8 bpp --> 16 bpp */ pixt1 = pixConvert16To8(pixs16, 1); pixt2 = pixConvertTo16(pixt1); pixWrite("/tmp/junkpix.png", pixt2, IFF_PNG); pixEqual(pixs16, pixt2, &same); if (!same) { pixDisplayWithTitle(pixs16, 100, 100, "16 bpp", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "16 bpp", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 16 bpp <==> 8 bpp", L_COPY); } else fprintf(stderr, "OK: conversion 16 bpp <==> 8 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Conversion: 8 bpp cmap --> 32 bpp --> 8 bpp cmap */ /* Required to go to level 6 of octcube to get identical result */ pixt1 = pixConvertTo32(pixc8); cmap = pixGetColormap(pixc8); pixt2 = pixOctcubeQuantFromCmap(pixt1, cmap, 2, 6, L_EUCLIDEAN_DISTANCE); pixEqual(pixc8, pixt2, &same); if (!same) { pixDisplayWithTitle(pixc8, 100, 100, "8 bpp cmap", DFLAG); pixDisplayWithTitle(pixt2, 500, 100, "8 bpp cmap", DFLAG); error = TRUE; sarrayAddString(sa, (char *)"conversion 8 bpp cmap <==> 32 bpp cmap", L_COPY); } else fprintf(stderr, "OK: conversion 8 bpp <==> 32 bpp\n"); pixDestroy(&pixt1); pixDestroy(&pixt2); /* Summarize results */ if (error == FALSE) fprintf(stderr, "No errors found\n"); else { errorstr = sarrayToString(sa, 1); fprintf(stderr, "Errors in the following:\n %s", errorstr); lept_free(errorstr); } sarrayDestroy(&sa); pixDestroy(&pixs1); pixDestroy(&pixs2); pixDestroy(&pixs4); pixDestroy(&pixc2); pixDestroy(&pixc4); pixDestroy(&pixs8); pixDestroy(&pixc8); pixDestroy(&pixs16); pixDestroy(&pixs32); return 0; }
main(int argc, char **argv) { l_int32 i, nsels, same, xorcount; char *selname; PIX *pixs, *pixs1, *pixt1, *pixt2, *pixt3; SEL *sel; SELA *sela; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; if ((pixs = pixRead("feyn-fract.tif")) == NULL) { rp->success = FALSE; regTestCleanup(rp); return 1; } sela = selaAddDwaLinear(NULL); nsels = selaGetCount(sela); for (i = 0; i < nsels; i++) { sel = selaGetSel(sela, i); selname = selGetName(sel); /* --------- dilation ----------*/ pixt1 = pixDilate(NULL, pixs, sel); pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_DILATE, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "dilations are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "dilations differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- erosion with asymmetric b.c ----------*/ resetMorphBoundaryCondition(ASYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixErode(NULL, pixs, sel); pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_ERODE, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "erosions are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "erosions differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- erosion with symmetric b.c ----------*/ resetMorphBoundaryCondition(SYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixErode(NULL, pixs, sel); pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_ERODE, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "erosions are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "erosions differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- opening with asymmetric b.c ----------*/ resetMorphBoundaryCondition(ASYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixOpen(NULL, pixs, sel); pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_OPEN, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "openings are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "openings differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- opening with symmetric b.c ----------*/ resetMorphBoundaryCondition(SYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixOpen(NULL, pixs, sel); pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_OPEN, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "openings are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "openings differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- safe closing with asymmetric b.c ----------*/ resetMorphBoundaryCondition(ASYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixCloseSafe(NULL, pixs, sel); /* must use safe version */ pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_CLOSE, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "closings are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "closings differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); /* --------- safe closing with symmetric b.c ----------*/ resetMorphBoundaryCondition(SYMMETRIC_MORPH_BC); fprintf(stderr, "MORPH_BC = %d ... ", MORPH_BC); pixt1 = pixClose(NULL, pixs, sel); /* safe version not required */ pixt2 = pixMorphDwa_3(NULL, pixs, L_MORPH_CLOSE, selname); pixEqual(pixt1, pixt2, &same); if (same == 1) { fprintf(stderr, "closings are identical for sel %d (%s)\n", i, selname); } else { rp->success = FALSE; fprintf(rp->fp, "closings differ for sel %d (%s)\n", i, selname); pixt3 = pixXor(NULL, pixt1, pixt2); pixCountPixels(pixt3, &xorcount, NULL); fprintf(rp->fp, "Number of pixels in XOR: %d\n", xorcount); pixDestroy(&pixt3); } pixDestroy(&pixt1); pixDestroy(&pixt2); } selaDestroy(&sela); pixDestroy(&pixs); return regTestCleanup(rp); }