/* * fgetJp2kResolution() * * Input: stream (opened for read) * &xres, &yres (<return> resolution in ppi) * Return: 0 if OK; 1 on error * * Notes: * (1) If the capture resolution field is not set, this is not an error; * the returned resolution values are 0 (designating 'unknown'). * (2) Side-effect: this rewinds the stream. * (3) The capture resolution box is optional in the jp2 spec, and * it is usually not written. * (4) The big-endian data fields that follow the 4 bytes of 'resc' are: * ynum: 2 bytes * ydenom: 2 bytes * xnum: 2 bytes * xdenom: 2 bytes * yexp: 1 byte * xexp: 1 byte */ l_int32 fgetJp2kResolution(FILE *fp, l_int32 *pxres, l_int32 *pyres) { l_uint8 xexp, yexp; l_uint8 *data; l_uint16 xnum, ynum, xdenom, ydenom; /* these jp2k fields are 2-byte */ l_int32 loc, found; l_uint8 resc[4] = {0x72, 0x65, 0x73, 0x63}; /* 'resc' */ size_t nbytes; l_float64 xres, yres; PROCNAME("fgetJp2kResolution"); if (pxres) *pxres = 0; if (pyres) *pyres = 0; if (!pxres || !pyres) return ERROR_INT("&xres and &yres not both defined", procName, 1); if (!fp) return ERROR_INT("stream not opened", procName, 1); rewind(fp); data = l_binaryReadStream(fp, &nbytes); rewind(fp); /* Search for the start of the first capture resolution box: 'resc' */ arrayFindSequence(data, nbytes, resc, 4, &loc, &found); if (!found) { L_WARNING("image resolution not found\n", procName); FREE(data); return 0; } /* Extract the fields and calculate the resolution in pixels/meter. * See section 1.5.3.7.1 of JPEG 2000 ISO/IEC 15444-1 spec. */ ynum = data[loc + 5] << 8 | data[loc + 4]; ynum = convertOnLittleEnd16(ynum); ydenom = data[loc + 7] << 8 | data[loc + 6]; ydenom = convertOnLittleEnd16(ydenom); xnum = data[loc + 9] << 8 | data[loc + 8]; xnum = convertOnLittleEnd16(xnum); xdenom = data[loc + 11] << 8 | data[loc + 10]; xdenom = convertOnLittleEnd16(xdenom); yexp = data[loc + 12]; xexp = data[loc + 13]; yres = ((l_float64)ynum / (l_float64)ydenom) * pow(10.0, (l_float64)yexp); xres = ((l_float64)xnum / (l_float64)xdenom) * pow(10.0, (l_float64)xexp); /* Convert from pixels/meter to ppi */ yres *= (300.0 / 11811.0); xres *= (300.0 / 11811.0); *pyres = (l_int32)(yres + 0.5); *pxres = (l_int32)(xres + 0.5); FREE(data); return 0; }
/*! * \brief pixWriteMemJpeg() * * \param[out] pdata data of jpeg compressed image * \param[out] psize size of returned data * \param[in] pix any depth; cmap is OK * \param[in] quality 1 - 100; 75 is default value; 0 is also default * \param[in] progressive 0 for baseline sequential; 1 for progressive * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) See pixWriteStreamJpeg() for usage. This version writes to * memory instead of to a file stream. * </pre> */ l_int32 pixWriteMemJpeg(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive) { l_int32 ret; FILE *fp; PROCNAME("pixWriteMemJpeg"); if (pdata) *pdata = NULL; if (psize) *psize = 0; if (!pdata) return ERROR_INT("&data not defined", procName, 1 ); if (!psize) return ERROR_INT("&size not defined", procName, 1 ); if (!pix) return ERROR_INT("&pix not defined", procName, 1 ); #if HAVE_FMEMOPEN if ((fp = open_memstream((char **)pdata, psize)) == NULL) return ERROR_INT("stream not opened", procName, 1); ret = pixWriteStreamJpeg(fp, pix, quality, progressive); #else L_INFO("work-around: writing to a temp file\n", procName); #ifdef _WIN32 if ((fp = fopenWriteWinTempfile()) == NULL) return ERROR_INT("tmpfile stream not opened", procName, 1); #else if ((fp = tmpfile()) == NULL) return ERROR_INT("tmpfile stream not opened", procName, 1); #endif /* _WIN32 */ ret = pixWriteStreamJpeg(fp, pix, quality, progressive); rewind(fp); *pdata = l_binaryReadStream(fp, psize); #endif /* HAVE_FMEMOPEN */ fclose(fp); return ret; }
/*! * pixReadStreamWebP() * * Input: stream corresponding to WebP image * Return: pix (32 bpp), or null on error */ PIX * pixReadStreamWebP(FILE *fp) { l_uint8 *filedata; size_t filesize; PIX *pix; PROCNAME("pixReadStreamWebP"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); /* Read data from file and decode into Y,U,V arrays */ rewind(fp); if ((filedata = l_binaryReadStream(fp, &filesize)) == NULL) return (PIX *)ERROR_PTR("filedata not read", procName, NULL); pix = pixReadMemWebP(filedata, filesize); FREE(filedata); return pix; }
/*! * \brief pixReadStreamBmp() * * \param[in] fp file stream opened for read * \return pix, or NULL on error * * <pre> * Notes: * (1) Here are references on the bmp file format: * http://en.wikipedia.org/wiki/BMP_file_format * http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html * </pre> */ PIX * pixReadStreamBmp(FILE *fp) { l_uint8 *data; size_t size; PIX *pix; PROCNAME("pixReadStreamBmp"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); /* Read data from file and decode into Y,U,V arrays */ rewind(fp); if ((data = l_binaryReadStream(fp, &size)) == NULL) return (PIX *)ERROR_PTR("data not read", procName, NULL); pix = pixReadMemBmp(data, size); LEPT_FREE(data); return pix; }
/*! * l_byteaInitFromStream() * * Input: stream * Return: l_bytea, or null on error */ L_BYTEA * l_byteaInitFromStream(FILE *fp) { l_uint8 *data; size_t nbytes; L_BYTEA *ba; PROCNAME("l_byteaInitFromStream"); if (!fp) return (L_BYTEA *)ERROR_PTR("stream not defined", procName, NULL); if ((data = l_binaryReadStream(fp, &nbytes)) == NULL) return (L_BYTEA *)ERROR_PTR("data not read", procName, NULL); if ((ba = l_byteaCreate(nbytes)) == NULL) return (L_BYTEA *)ERROR_PTR("ba not made", procName, NULL); memcpy(ba->data, data, nbytes); ba->size = nbytes; FREE(data); return ba; }
/*! * \brief recogWriteMem() * * \param[out] pdata data of serialized recog (not ascii) * \param[out] psize size of returned data * \param[in] recog * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) Serializes a recog in memory and puts the result in a buffer. * </pre> */ l_int32 recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog) { l_int32 ret; FILE *fp; PROCNAME("recogWriteMem"); if (pdata) *pdata = NULL; if (psize) *psize = 0; if (!pdata) return ERROR_INT("&data not defined", procName, 1); if (!psize) return ERROR_INT("&size not defined", procName, 1); if (!recog) return ERROR_INT("recog not defined", procName, 1); #if HAVE_FMEMOPEN if ((fp = open_memstream((char **)pdata, psize)) == NULL) return ERROR_INT("stream not opened", procName, 1); ret = recogWriteStream(fp, recog); #else L_INFO("work-around: writing to a temp file\n", procName); #ifdef _WIN32 if ((fp = fopenWriteWinTempfile()) == NULL) return ERROR_INT("tmpfile stream not opened", procName, 1); #else if ((fp = tmpfile()) == NULL) return ERROR_INT("tmpfile stream not opened", procName, 1); #endif /* _WIN32 */ ret = recogWriteStream(fp, recog); rewind(fp); *pdata = l_binaryReadStream(fp, psize); #endif /* HAVE_FMEMOPEN */ fclose(fp); return ret; }
/*! * pixReadStreamSpix() * * Input: stream * Return: pix, or null on error. * * Notes: * (1) If called from pixReadStream(), the stream is positioned * at the beginning of the file. */ PIX * pixReadStreamSpix(FILE *fp) { size_t nbytes; l_uint8 *data; PIX *pix; PROCNAME("pixReadStreamSpix"); if (!fp) return (PIX *)ERROR_PTR("stream not defined", procName, NULL); if ((data = l_binaryReadStream(fp, &nbytes)) == NULL) return (PIX *)ERROR_PTR("data not read", procName, NULL); if ((pix = pixReadMemSpix(data, nbytes)) == NULL) { FREE(data); return (PIX *)ERROR_PTR("pix not made", procName, NULL); } FREE(data); return pix; }
/*! * pixWriteMemJp2k() * * Input: &data (<return> data of jpeg compressed image) * &size (<return> size of returned data) * pix (8 or 32 bpp) * quality (SNR > 0; default ~34; 0 for lossless encoding) * nlevels (0 for default) * hint (a bitwise OR of L_JP2K_* values; 0 for default) * debug (output callback messages, etc) * Return: 0 if OK, 1 on error * * Notes: * (1) See pixWriteJp2k() for usage. This version writes to * memory instead of to a file stream. */ l_int32 pixWriteMemJp2k(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 nlevels, l_int32 hint, l_int32 debug) { l_int32 ret; FILE *fp; PROCNAME("pixWriteMemJp2k"); if (pdata) *pdata = NULL; if (psize) *psize = 0; if (!pdata) return ERROR_INT("&data not defined", procName, 1 ); if (!psize) return ERROR_INT("&size not defined", procName, 1 ); if (!pix) return ERROR_INT("&pix not defined", procName, 1 ); #if HAVE_FMEMOPEN if ((fp = open_memstream((char **)pdata, psize)) == NULL) return ERROR_INT("stream not opened", procName, 1); ret = pixWriteStreamJp2k(fp, pix, quality, nlevels, hint, debug); #else L_WARNING("work-around: writing to a temp file\n", procName); if ((fp = tmpfile()) == NULL) return ERROR_INT("tmpfile stream not opened", procName, 1); ret = pixWriteStreamJp2k(fp, pix, quality, nlevels, hint, debug); rewind(fp); *pdata = l_binaryReadStream(fp, psize); #endif /* HAVE_FMEMOPEN */ fclose(fp); return ret; }
/*! * \brief pixReadStreamGif() * * \param[in] fp file stream opened for reading * \return pix, or NULL on error */ PIX * pixReadStreamGif(FILE *fp) { l_uint8 *filedata; size_t filesize; PIX *pix; PROCNAME("pixReadStreamGif"); if (!fp) return (PIX *)ERROR_PTR("fp not defined", procName, NULL); /* Read data into memory from file */ rewind(fp); if ((filedata = l_binaryReadStream(fp, &filesize)) == NULL) return (PIX *)ERROR_PTR("filedata not read", procName, NULL); /* Uncompress from memory */ pix = pixReadMemGif(filedata, filesize); LEPT_FREE(filedata); if (!pix) L_ERROR("failed to read gif from file data\n", procName); return pix; }