69#include <config_auto.h>
73#include "allheaders.h"
75extern l_int32 NumImageFileFormatExtensions;
76extern const char *ImageFileFormatExtensions[];
127char *testname, *vers;
131 if (argc != 1 && argc != 2) {
132 snprintf(errormsg,
sizeof(errormsg),
133 "Syntax: %s [ [compare] | generate | display ]", argv[0]);
134 return ERROR_INT(errormsg, __func__, 1);
138 return ERROR_INT(
"invalid root", __func__, 1);
152 lept_mkdir(
"lept/regout");
155 if (argc == 1 || !strcmp(argv[1],
"compare")) {
156 rp->
mode = L_REG_COMPARE;
157 rp->
tempfile = stringNew(
"/tmp/lept/regout/regtest_output.txt");
158 rp->
fp = fopenWriteStream(rp->
tempfile,
"wb");
159 if (rp->
fp == NULL) {
161 return ERROR_INT_1(
"stream not opened for tempfile",
164 }
else if (!strcmp(argv[1],
"generate")) {
165 rp->
mode = L_REG_GENERATE;
166 lept_mkdir(
"lept/golden");
167 }
else if (!strcmp(argv[1],
"display")) {
168 rp->
mode = L_REG_DISPLAY;
172 snprintf(errormsg,
sizeof(errormsg),
173 "Syntax: %s [ [generate] | compare | display ]", argv[0]);
174 return ERROR_INT(errormsg, __func__, 1);
179 lept_stderr(
"\n////////////////////////////////////////////////\n"
180 "//////////////// %s_reg ///////////////\n"
181 "////////////////////////////////////////////////\n",
183 vers = getLeptonicaVersion();
184 lept_stderr(
"%s : ", vers);
186 vers = getImagelibVersions();
187 lept_stderr(
"%s\n", vers);
190 rp->
tstart = startTimerNested();
217 return ERROR_INT(
"rp not defined", __func__, 1);
219 lept_stderr(
"Time: %7.3f sec\n", stopTimerNested(rp->
tstart));
231 text = (
char *)l_binaryRead(rp->
tempfile, &nbytes);
237 return ERROR_INT(
"text not returned", __func__, 1);
242 snprintf(result,
sizeof(result),
"SUCCESS: %s_reg\n", rp->
testname);
244 snprintf(result,
sizeof(result),
"FAILURE: %s_reg\n", rp->
testname);
245 message = stringJoin(text, result);
247 results_file = stringNew(
"/tmp/lept/reg_results.txt");
248 fileAppendString(results_file, message);
249 retval = (rp->
success) ? 0 : 1;
250 LEPT_FREE(results_file);
278 return ERROR_INT(
"rp not defined", __func__, 1);
281 diff = L_ABS(val2 - val1);
287 "Failure in %s_reg: value comparison for index %d\n"
288 "difference = %f but allowed delta = %f\n",
291 lept_stderr(
"Failure in %s_reg: value comparison for index %d\n"
292 "difference = %f but allowed delta = %f\n",
322 return ERROR_INT(
"rp not defined", __func__, 1);
325 l_binaryCompare(string1, bytes1, string2, bytes2, &same);
330 snprintf(buf,
sizeof(buf),
"/tmp/lept/regout/string1_%d_%zu",
332 l_binaryWrite(buf,
"w", string1, bytes1);
333 snprintf(buf,
sizeof(buf),
"/tmp/lept/regout/string2_%d_%zu",
335 l_binaryWrite(buf,
"w", string2, bytes2);
338 snprintf(buf,
sizeof(buf),
"/tmp/lept/regout/string*_%d_*", rp->
index);
341 "Failure in %s_reg: string comp for index %d; "
344 lept_stderr(
"Failure in %s_reg: string comp for index %d; "
374 return ERROR_INT(
"rp not defined", __func__, 1);
375 if (!pix1 || !pix2) {
377 return ERROR_INT(
"pix1 and pix2 not both defined", __func__, 1);
381 pixEqual(pix1, pix2, &same);
386 fprintf(rp->
fp,
"Failure in %s_reg: pix comparison for index %d\n",
389 lept_stderr(
"Failure in %s_reg: pix comparison for index %d\n",
432l_int32 w, h, factor, similar;
435 return ERROR_INT(
"rp not defined", __func__, 1);
436 if (!pix1 || !pix2) {
438 return ERROR_INT(
"pix1 and pix2 not both defined", __func__, 1);
442 pixGetDimensions(pix1, &w, &h, NULL);
443 factor = L_MAX(w, h) / 400;
444 factor = L_MAX(1, L_MIN(factor, 4));
445 pixTestForSimilarity(pix1, pix2, factor, mindiff, maxfract, 0.0,
446 &similar, printstats);
452 "Failure in %s_reg: pix similarity comp for index %d\n",
455 lept_stderr(
"Failure in %s_reg: pix similarity comp for index %d\n",
491 const char *localname)
495l_int32 ret, same, format;
499 return ERROR_INT(
"rp not defined", __func__, 1);
502 return ERROR_INT(
"local name not defined", __func__, 1);
504 if (rp->
mode != L_REG_GENERATE && rp->
mode != L_REG_COMPARE &&
505 rp->
mode != L_REG_DISPLAY) {
507 return ERROR_INT(
"invalid mode", __func__, 1);
512 if (rp->
mode == L_REG_DISPLAY)
return 0;
515 splitPathAtExtension(localname, NULL, &ext);
516 snprintf(namebuf,
sizeof(namebuf),
"/tmp/lept/golden/%s_golden.%02d%s",
521 if (rp->
mode == L_REG_GENERATE) {
523 ret = fileCopy(localname, namebuf);
526 char *local = genPathname(localname, NULL);
527 char *golden = genPathname(namebuf, NULL);
528 L_INFO(
"Copy: %s to %s\n", __func__, local, golden);
553 findFileFormat(localname, &format);
554 if (format == IFF_GIF) {
556 pix1 = pixRead(localname);
557 pix2 = pixRead(namebuf);
558 pixEqual(pix1, pix2, &same);
562 filesAreIdentical(localname, namebuf, &same);
565 fprintf(rp->
fp,
"Failure in %s_reg, index %d: comparing %s with %s\n",
567 lept_stderr(
"Failure in %s_reg, index %d: comparing %s with %s\n",
606 return ERROR_INT(
"rp not defined", __func__, 1);
607 if (index1 < 0 || index2 < 0) {
609 return ERROR_INT(
"index1 and/or index2 is negative", __func__, 1);
611 if (index1 == index2) {
613 return ERROR_INT(
"index1 must differ from index2", __func__, 1);
617 if (rp->
mode != L_REG_COMPARE)
return 0;
620 snprintf(namebuf,
sizeof(namebuf),
"%s_golden.%02d", rp->
testname, index1);
621 sa = getSortedPathnamesInDirectory(
"/tmp/lept/golden", namebuf, 0, 0);
622 if (sarrayGetCount(sa) != 1) {
625 L_ERROR(
"golden file %s not found\n", __func__, namebuf);
628 name1 = sarrayGetString(sa, 0,
L_COPY);
631 snprintf(namebuf,
sizeof(namebuf),
"%s_golden.%02d", rp->
testname, index2);
632 sa = getSortedPathnamesInDirectory(
"/tmp/lept/golden", namebuf, 0, 0);
633 if (sarrayGetCount(sa) != 1) {
637 L_ERROR(
"golden file %s not found\n", __func__, namebuf);
640 name2 = sarrayGetString(sa, 0,
L_COPY);
644 filesAreIdentical(name1, name2, &same);
647 "Failure in %s_reg, index %d: comparing %s with %s\n",
649 lept_stderr(
"Failure in %s_reg, index %d: comparing %s with %s\n",
694 return ERROR_INT(
"rp not defined", __func__, 1);
697 return ERROR_INT(
"pix not defined", __func__, 1);
699 if (format < 0 || format >= NumImageFileFormatExtensions) {
701 return ERROR_INT(
"invalid format", __func__, 1);
706 changeFormatForMissingLib(&format);
709 snprintf(namebuf,
sizeof(namebuf),
"/tmp/lept/regout/%s.%02d.%s",
710 rp->
testname, rp->
index + 1, ImageFileFormatExtensions[format]);
713 if (pixGetDepth(pix) < 8)
714 pixSetPadBits(pix, 0);
715 pixWrite(namebuf, pix, format);
764 return ERROR_INT(
"rp not defined", __func__, 1);
765 if (!data || nbytes == 0) {
767 return ERROR_INT(
"data not defined or size == 0", __func__, 1);
771 snprintf(namebuf,
sizeof(namebuf),
"/tmp/lept/regout/%s.%02d.%s",
775 l_binaryWrite(namebuf,
"w", data, nbytes);
813 return (
char *)ERROR_PTR(
"rp not defined", __func__, NULL);
815 ind = (index >= 0) ? index : rp->
index;
816 snprintf(buf,
sizeof(buf),
"/tmp/lept/regout/%s.%02d.%s",
817 rp->
testname, ind, ImageFileFormatExtensions[format]);
818 return stringNew(buf);
844 splitPathAtDirectory(argv0, NULL, &root);
845 if ((len = strlen(root)) <= 4) {
847 return (
char *)ERROR_PTR(
"invalid argv0; too small", __func__, NULL);
854 if (stringFindSubstr(root,
"-", &loc)) {
855 newroot = stringNew(root + loc + 1);
863 if (strstr(root,
".exe") != NULL)
865 if (strstr(root,
"_reg") == root + len - 4)
l_ok regTestSetup(l_int32 argc, char **argv, L_REGPARAMS **prp)
regTestSetup()
l_ok regTestCompareSimilarPix(L_REGPARAMS *rp, PIX *pix1, PIX *pix2, l_int32 mindiff, l_float32 maxfract, l_int32 printstats)
regTestCompareSimilarPix()
l_ok regTestCompareFiles(L_REGPARAMS *rp, l_int32 index1, l_int32 index2)
regTestCompareFiles()
l_ok regTestCompareStrings(L_REGPARAMS *rp, l_uint8 *string1, size_t bytes1, l_uint8 *string2, size_t bytes2)
regTestCompareStrings()
l_ok regTestWritePixAndCheck(L_REGPARAMS *rp, PIX *pix, l_int32 format)
regTestWritePixAndCheck()
static char * getRootNameFromArgv0(const char *argv0)
getRootNameFromArgv0()
l_ok regTestCheckFile(L_REGPARAMS *rp, const char *localname)
regTestCheckFile()
l_ok regTestComparePix(L_REGPARAMS *rp, PIX *pix1, PIX *pix2)
regTestComparePix()
l_ok regTestCompareValues(L_REGPARAMS *rp, l_float32 val1, l_float32 val2, l_float32 delta)
regTestCompareValues()
l_ok regTestCleanup(L_REGPARAMS *rp)
regTestCleanup()
l_ok regTestWriteDataAndCheck(L_REGPARAMS *rp, void *data, size_t nbytes, const char *ext)
regTestWriteDataAndCheck()
char * regTestGenLocalFilename(L_REGPARAMS *rp, l_int32 index, l_int32 format)
regTestGenLocalFilename()