/*! * freadHeaderPng() * * Input: stream * &width (<return>) * &height (<return>) * &bps (<return>, bits/sample) * &spp (<return>, samples/pixel) * &iscmap (<optional return>; input NULL to ignore) * Return: 0 if OK, 1 on error * * Notes: * (1) If there is a colormap, iscmap is returned as 1; else 0. */ l_int32 freadHeaderPng(FILE *fp, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap) { l_int32 nbytes, ret; l_uint8 *data; PROCNAME("freadHeaderPng"); if (!fp) return ERROR_INT("stream not defined", procName, 1); if (!pwidth || !pheight || !pbps || !pspp) return ERROR_INT("input ptr(s) not defined", procName, 1); nbytes = fnbytesInFile(fp); if (nbytes < 40) return ERROR_INT("file too small to be png", procName, 1); if ((data = (l_uint8 *)CALLOC(40, sizeof(l_uint8))) == NULL) return ERROR_INT("CALLOC fail for data", procName, 1); if (fread(data, 1, 40, fp) != 40) return ERROR_INT("error reading data", procName, 1); ret = sreadHeaderPng(data, pwidth, pheight, pbps, pspp, piscmap); FREE(data); return ret; }
/*! * findFileFormatStream() * * Input: fp (file stream) * &format (<return>) * Return: 0 if OK, 1 on error or if format is not recognized * * Notes: * (1) Important: Side effect -- this resets fp to BOF. */ l_int32 findFileFormatStream(FILE *fp, l_int32 *pformat) { l_uint8 firstbytes[12]; l_int32 format; PROCNAME("findFileFormatStream"); if (!pformat) return ERROR_INT("&format not defined", procName, 1); *pformat = IFF_UNKNOWN; if (!fp) return ERROR_INT("stream not defined", procName, 1); rewind(fp); if (fnbytesInFile(fp) < 12) return ERROR_INT("truncated file", procName, 1); if (fread((char *)&firstbytes, 1, 12, fp) != 12) return ERROR_INT("failed to read first 12 bytes of file", procName, 1); rewind(fp); findFileFormatBuffer(firstbytes, &format); if (format == IFF_TIFF) { findTiffCompression(fp, &format); rewind(fp); } *pformat = format; if (format == IFF_UNKNOWN) return 1; else return 0; }