/*! * pixWriteJp2k() * * Input: filename * pix (any depth, cmap is OK) * quality (SNR > 0; default ~34; 0 for lossless encoding) * nlevels (resolution levels; <= 10; default = 5) * 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) The @quality parameter is the SNR. The useful range is narrow: * SNR < 27 (terrible quality) * SNR = 34 (default; approximately equivalent to jpeg quality 75) * SNR = 40 (very high quality) * SNR = 45 (nearly lossless) * Use 0 for default. * (2) The @nlevels parameter is the number of resolution levels * to be written. For example, with nlevels == 5, images with * reduction factors of 1, 2, 4, 8 and 16 are encoded, and retrieval * is done at the level requested when reading. For default, * use either 5 or 0. * (3) The @hint parameter is not yet in use. * (4) For now, we only support 1 "layer" for quality. */ l_int32 pixWriteJp2k(const char *filename, PIX *pix, l_int32 quality, l_int32 nlevels, l_int32 hint, l_int32 debug) { FILE *fp; PROCNAME("pixWriteJp2k"); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (!filename) return ERROR_INT("filename not defined", procName, 1); if ((fp = fopenWriteStream(filename, "wb+")) == NULL) return ERROR_INT("stream not opened", procName, 1); if (pixWriteStreamJp2k(fp, pix, quality, nlevels, hint, debug)) { fclose(fp); return ERROR_INT("pix not written to stream", procName, 1); } fclose(fp); return 0; }
/*! * 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 pixWriteStream() * * \param[in] fp file stream * \param[in] pix * \param[in] format * \return 0 if OK; 1 on error. */ l_int32 pixWriteStream(FILE *fp, PIX *pix, l_int32 format) { PROCNAME("pixWriteStream"); if (!fp) return ERROR_INT("stream not defined", procName, 1); if (!pix) return ERROR_INT("pix not defined", procName, 1); if (format == IFF_DEFAULT) format = pixChooseOutputFormat(pix); switch(format) { case IFF_BMP: pixWriteStreamBmp(fp, pix); break; case IFF_JFIF_JPEG: /* default quality; baseline sequential */ return pixWriteStreamJpeg(fp, pix, 75, 0); break; case IFF_PNG: /* no gamma value stored */ return pixWriteStreamPng(fp, pix, 0.0); break; case IFF_TIFF: /* uncompressed */ case IFF_TIFF_PACKBITS: /* compressed, binary only */ case IFF_TIFF_RLE: /* compressed, binary only */ case IFF_TIFF_G3: /* compressed, binary only */ case IFF_TIFF_G4: /* compressed, binary only */ case IFF_TIFF_LZW: /* compressed, all depths */ case IFF_TIFF_ZIP: /* compressed, all depths */ return pixWriteStreamTiff(fp, pix, format); break; case IFF_PNM: return pixWriteStreamPnm(fp, pix); break; case IFF_PS: return pixWriteStreamPS(fp, pix, NULL, 0, DEFAULT_SCALING); break; case IFF_GIF: return pixWriteStreamGif(fp, pix); break; case IFF_JP2: return pixWriteStreamJp2k(fp, pix, 34, 4, 0, 0); break; case IFF_WEBP: return pixWriteStreamWebP(fp, pix, 80, 0); break; case IFF_LPDF: return pixWriteStreamPdf(fp, pix, 0, NULL); break; case IFF_SPIX: return pixWriteStreamSpix(fp, pix); break; default: return ERROR_INT("unknown format", procName, 1); break; } return 0; }