/*! * regTestWritePixAndCheck() * * Input: rp (regtest parameters) * pix (to be written) * format (of output pix) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This function makes it easy to write the pix in a numbered * sequence of files, and either to: * (a) write the golden file ("generate" arg to regression test) * (b) make a local file and "compare" with the golden file * (c) make a local file and "display" the results * (3) The canonical format of the local filename is: * /tmp/regout/<root of main name>.<count>.<format extension string> * e.g., for scale_reg, * /tmp/regout/scale.0.png */ l_int32 regTestWritePixAndCheck(L_REGPARAMS *rp, PIX *pix, l_int32 format) { char namebuf[256]; PROCNAME("regTestWritePixAndCheck"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (!pix) { rp->success = FALSE; return ERROR_INT("pix not defined", procName, 1); } if (format < 0 || format >= NumImageFileFormatExtensions) { rp->success = FALSE; return ERROR_INT("invalid format", procName, 1); } /* Generate the local file name */ snprintf(namebuf, sizeof(namebuf), "/tmp/regout/%s.%02d.%s", rp->testname, rp->index + 1, ImageFileFormatExtensions[format]); /* Write the local file */ if (pixGetDepth(pix) < 8) pixSetPadBits(pix, 0); pixWrite(namebuf, pix, format); /* Either write the golden file ("generate") or check the local file against an existing golden file ("compare") */ regTestCheckFile(rp, namebuf); return 0; }
/*! * \brief pixWriteStreamGif() * * \param[in] fp file stream opened for writing * \param[in] pix 1, 2, 4, 8, 16 or 32 bpp * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) All output gif have colormaps. If the pix is 32 bpp rgb, * this quantizes the colors and writes out 8 bpp. * If the pix is 16 bpp grayscale, it converts to 8 bpp first. * </pre> */ l_int32 pixWriteStreamGif(FILE *fp, PIX *pix) { l_uint8 *filedata; size_t filebytes, nbytes; PROCNAME("pixWriteStreamGif"); if (!fp) return ERROR_INT("stream not open", procName, 1); if (!pix) return ERROR_INT("pix not defined", procName, 1); pixSetPadBits(pix, 0); if (pixWriteMemGif(&filedata, &filebytes, pix) != 0) { LEPT_FREE(filedata); return ERROR_INT("failure to gif encode pix", procName, 1); } rewind(fp); nbytes = fwrite(filedata, 1, filebytes, fp); LEPT_FREE(filedata); if (nbytes != filebytes) return ERROR_INT("write error", procName, 1); return 0; }
/*! * \brief pixWriteStreampWebP() * * \param[in] fp file stream * \param[in] pixs all depths * \param[in] quality 0 - 100; default ~80 * \param[in] lossless use 1 for lossless; 0 for lossy * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) See pixWriteMemWebP() for details. * (2) Use 'free', and not leptonica's 'LEPT_FREE', for all heap data * that is returned from the WebP library. * </pre> */ l_ok pixWriteStreamWebP(FILE *fp, PIX *pixs, l_int32 quality, l_int32 lossless) { l_uint8 *filedata; size_t filebytes, nbytes; PROCNAME("pixWriteStreamWebP"); if (!fp) return ERROR_INT("stream not open", procName, 1); if (!pixs) return ERROR_INT("pixs not defined", procName, 1); pixSetPadBits(pixs, 0); pixWriteMemWebP(&filedata, &filebytes, pixs, quality, lossless); rewind(fp); nbytes = fwrite(filedata, 1, filebytes, fp); free(filedata); if (nbytes != filebytes) return ERROR_INT("Write error", procName, 1); return 0; }
/*! * pixCreateNoInit() * * Input: width, height, depth * Return: pixd (with data allocated but not initialized), * or null on error * * Notes: * (1) Must set pad bits to avoid reading unitialized data, because * some optimized routines (e.g., pixConnComp()) read from pad bits. */ PIX * pixCreateNoInit(l_int32 width, l_int32 height, l_int32 depth) { l_int32 wpl; PIX *pixd; l_uint32 *data; PROCNAME("pixCreateNoInit"); pixd = pixCreateHeader(width, height, depth); if (!pixd) return NULL; wpl = pixGetWpl(pixd); if ((data = (l_uint32 *)pix_malloc(4 * wpl * height)) == NULL) return (PIX *)ERROR_PTR("pix_malloc fail for data", procName, NULL); pixSetData(pixd, data); pixSetPadBits(pixd, 0); 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; }