/*! * 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; FILE *fp; PIX *pix; PROCNAME("pixReadRGBAPng"); if (!filename) return (PIX *)ERROR_PTR("filename not defined", procName, NULL); /* If alpha channel reading is enabled, just read it */ if (var_PNG_STRIP_ALPHA == FALSE) return pixRead(filename); /* Make sure it's a png file */ if ((fp = fopenReadStream(filename)) == NULL) return (PIX *)ERROR_PTR("image file not found", procName, NULL); findFileFormat(fp, &format); if (format != IFF_PNG) { fclose(fp); L_ERROR_STRING("file format is %s, not png", procName, ImageFileFormatExtensions[format]); return NULL; } l_pngSetStripAlpha(0); pix = pixReadStreamPng(fp); l_pngSetStripAlpha(1); /* reset to default */ fclose(fp); if (!pix) L_ERROR("pix not read", procName); return pix; }
/*! * pixaReadStream() * * Input: stream * Return: pixa, or null on error */ PIXA * pixaReadStream(FILE *fp) { l_int32 n, i, xres, yres, version; l_int32 ignore; BOXA *boxa; PIX *pix; PIXA *pixa; PROCNAME("pixaReadStream"); #if !HAVE_LIBPNG /* defined in environ.h */ return (PIXA *)ERROR_PTR("no libpng: can't read data", procName, NULL); #else if (!fp) return (PIXA *)ERROR_PTR("stream not defined", procName, NULL); if (fscanf(fp, "\nPixa Version %d\n", &version) != 1) return (PIXA *)ERROR_PTR("not a pixa file", procName, NULL); if (version != PIXA_VERSION_NUMBER) return (PIXA *)ERROR_PTR("invalid pixa version", procName, NULL); if (fscanf(fp, "Number of pix = %d\n", &n) != 1) return (PIXA *)ERROR_PTR("not a pixa file", procName, NULL); if ((pixa = pixaCreate(n)) == NULL) return (PIXA *)ERROR_PTR("pixa not made", procName, NULL); if ((boxa = boxaReadStream(fp)) == NULL) return (PIXA *)ERROR_PTR("boxa not made", procName, NULL); boxaDestroy(&pixa->boxa); pixa->boxa = boxa; for (i = 0; i < n; i++) { if ((fscanf(fp, " pix[%d]: xres = %d, yres = %d\n", &ignore, &xres, &yres)) != 3) return (PIXA *)ERROR_PTR("res reading", procName, NULL); if ((pix = pixReadStreamPng(fp)) == NULL) return (PIXA *)ERROR_PTR("pix not read", procName, NULL); pixSetXRes(pix, xres); pixSetYRes(pix, yres); pixaAddPix(pixa, pix, L_INSERT); } return pixa; #endif /* !HAVE_LIBPNG */ }
/*! * pixReadMemPng() * * Input: cdata (const; png-encoded) * size (of data) * Return: pix, or null on error * * Notes: * (1) The @size byte of @data must be a null character. */ PIX * pixReadMemPng(const l_uint8 *cdata, size_t size) { l_uint8 *data; FILE *fp; PIX *pix; PROCNAME("pixReadMemPng"); if (!cdata) return (PIX *)ERROR_PTR("cdata not defined", procName, NULL); data = (l_uint8 *)cdata; /* we're really not going to change this */ if ((fp = fmemopen(data, size, "r")) == NULL) return (PIX *)ERROR_PTR("stream not opened", procName, NULL); pix = pixReadStreamPng(fp); fclose(fp); return pix; }
/*! * pixReadStream() * * Input: fp (file stream) * hint (bitwise OR of L_HINT_* values for jpeg; use 0 for no hint) * Return: pix if OK; null on error * * Notes: * (1) The hint only applies to jpeg. */ PIX * pixReadStream(FILE *fp, l_int32 hint) { l_int32 format, ret; l_uint8 *comment; PIX *pix; PROCNAME("pixReadStream"); if (!fp) return (PIX *)ERROR_PTR("stream not defined", procName, NULL); pix = NULL; findFileFormatStream(fp, &format); switch (format) { case IFF_BMP: if ((pix = pixReadStreamBmp(fp)) == NULL ) return (PIX *)ERROR_PTR( "bmp: no pix returned", procName, NULL); break; // case IFF_JFIF_JPEG: // if ((pix = pixReadStreamJpeg(fp, READ_24_BIT_COLOR, 1, NULL, hint)) // == NULL) // return (PIX *)ERROR_PTR( "jpeg: no pix returned", procName, NULL); // ret = fgetJpegComment(fp, &comment); // if (!ret && comment) // pixSetText(pix, (char *)comment); // FREE(comment); // break; case IFF_PNG: if ((pix = pixReadStreamPng(fp)) == NULL) return (PIX *)ERROR_PTR("png: no pix returned", procName, NULL); break; case IFF_TIFF: case IFF_TIFF_PACKBITS: case IFF_TIFF_RLE: case IFF_TIFF_G3: case IFF_TIFF_G4: case IFF_TIFF_LZW: case IFF_TIFF_ZIP: if ((pix = pixReadStreamTiff(fp, 0)) == NULL) /* page 0 by default */ return (PIX *)ERROR_PTR("tiff: no pix returned", procName, NULL); break; case IFF_PNM: if ((pix = pixReadStreamPnm(fp)) == NULL) return (PIX *)ERROR_PTR("pnm: no pix returned", procName, NULL); break; case IFF_GIF: if ((pix = pixReadStreamGif(fp)) == NULL) return (PIX *)ERROR_PTR("gif: no pix returned", procName, NULL); break; case IFF_JP2: if ((pix = pixReadStreamJp2k(fp, 1, NULL, 0)) == NULL) return (PIX *)ERROR_PTR("jp2: no pix returned", procName, NULL); break; case IFF_WEBP: if ((pix = pixReadStreamWebP(fp)) == NULL) return (PIX *)ERROR_PTR("webp: no pix returned", procName, NULL); break; case IFF_SPIX: if ((pix = pixReadStreamSpix(fp)) == NULL) return (PIX *)ERROR_PTR("spix: no pix returned", procName, NULL); break; case IFF_UNKNOWN: return (PIX *)ERROR_PTR( "Unknown format: no pix returned", procName, NULL); break; } if (pix) pixSetInputFormat(pix, format); return pix; }