/*! * pixReadRGBAPng() * * Input: filename (of png file) * Return: pix, or null on error * * Notes: * (1) Wrapper to keep the alpha channel of a png, if it exists. * (2) The default behavior of pix read functions is to ignore * the alpha channel. * (3) This always leaves alpha stripping in the same mode as * when this function begins. So if alpha stripping is in * default mode, this disables it, reads the file (including * the alpha channel), and resets back to stripping. Otherwise, * it leaves stripping disabled. */ PIX * pixReadRGBAPng(const char *filename) { l_int32 format; PIX *pix; PROCNAME("pixReadRGBAPng"); if (!filename) return (PIX *)ERROR_PTR("filename not defined", procName, NULL); /* Make sure it's a png file */ findFileFormat(filename, &format); if (format != IFF_PNG) { L_ERROR_STRING("file format is %s, not png", procName, ImageFileFormatExtensions[format]); return NULL; } /* If alpha channel reading is enabled, just read it */ if (var_PNG_STRIP_ALPHA == FALSE) return pixRead(filename); l_pngSetStripAlpha(0); pix = pixRead(filename); l_pngSetStripAlpha(1); /* reset to default */ if (!pix) L_ERROR("pix not read", procName); return pix; }
/* * writeImageCompressedToPSFile() * * Input: filein (input image file) * fileout (output ps file) * res (output printer resolution) * &firstfile (<input and return> 1 if the first image; * 0 otherwise) * &index (<input and return> index of image in output ps file) * Return: 0 if OK, 1 on error * * Notes: * (1) This wraps a single page image in PS. * (2) The input file can be in any format. It is compressed as follows: * * if in tiffg4 --> use ccittg4 * * if in jpeg --> use dct * * all others --> use flate * (3) @index is incremented if the page is successfully written. */ l_int32 writeImageCompressedToPSFile(const char *filein, const char *fileout, l_int32 res, l_int32 *pfirstfile, l_int32 *pindex) { const char *op; l_int32 format, retval; FILE *fp; PROCNAME("writeImageCompressedToPSFile"); if (!pfirstfile || !pindex) return ERROR_INT("&firstfile and &index not defined", procName, 1); if ((fp = fopenReadStream(filein)) == NULL) return ERROR_INT("filein not found", procName, 1); findFileFormat(fp, &format); fclose(fp); if (format == IFF_UNKNOWN) { L_ERROR_STRING("Format of %s not known", procName, filein); return 1; } op = (*pfirstfile == TRUE) ? "w" : "a"; if (format == IFF_JFIF_JPEG) { retval = convertJpegToPS(filein, fileout, op, 0, 0, res, 1.0, *pindex + 1, TRUE); if (retval == 0) { *pfirstfile = FALSE; (*pindex)++; } } else if (format == IFF_TIFF_G4) { retval = convertTiffG4ToPS(filein, fileout, op, 0, 0, res, 1.0, *pindex + 1, FALSE, TRUE); if (retval == 0) { *pfirstfile = FALSE; (*pindex)++; } } else { /* all other image formats */ retval = convertFlateToPS(filein, fileout, op, 0, 0, res, 1.0, *pindex + 1, TRUE); if (retval == 0) { *pfirstfile = FALSE; (*pindex)++; } } return retval; }
/*! * regTestCompareFiles() * * Input: rp (regtest parameters) * index1 (of one output file from reg test) * index2 (of another output file from reg test) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This only does something in "compare" mode. * (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 */ l_int32 regTestCompareFiles(L_REGPARAMS *rp, l_int32 index1, l_int32 index2) { char *name1, *name2; char namebuf[256]; l_int32 same; SARRAY *sa; PROCNAME("regTestCompareFiles"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (index1 < 0 || index2 < 0) { rp->success = FALSE; return ERROR_INT("index1 and/or index2 is negative", procName, 1); } if (index1 == index2) { rp->success = FALSE; return ERROR_INT("index1 must differ from index2", procName, 1); } rp->index++; if (rp->mode != L_REG_COMPARE) return 0; /* Generate the golden file names */ snprintf(namebuf, sizeof(namebuf), "%s_golden.%d.", rp->testname, index1); sa = getSortedPathnamesInDirectory("/tmp/golden", namebuf, 0, 0); if (sarrayGetCount(sa) != 1) { sarrayDestroy(&sa); rp->success = FALSE; L_ERROR_STRING("golden file %s not found", procName, namebuf); return 1; } name1 = sarrayGetString(sa, 0, L_COPY); sarrayDestroy(&sa); snprintf(namebuf, sizeof(namebuf), "%s_golden.%d.", rp->testname, index2); sa = getSortedPathnamesInDirectory("/tmp/golden", namebuf, 0, 0); if (sarrayGetCount(sa) != 1) { sarrayDestroy(&sa); rp->success = FALSE; FREE(name1); L_ERROR_STRING("golden file %s not found", procName, namebuf); return 1; } name2 = sarrayGetString(sa, 0, L_COPY); sarrayDestroy(&sa); /* Test and record on failure */ filesAreIdentical(name1, name2, &same); if (!same) { fprintf(rp->fp, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, name1, name2); fprintf(stderr, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, name1, name2); rp->success = FALSE; } FREE(name1); FREE(name2); return 0; }