/*! * pixReadMemJp2k() * * Input: data (const; jpeg-encoded) * size (of data) * reduction (scaling factor: 1, 2, 4, 8) * box (<optional> for extracting a subregion), can be null * hint (a bitwise OR of L_JP2K_* values; 0 for default) * debug (output callback messages, etc) * Return: pix, or null on error * * Notes: * (1) This crashes when reading through the fmemopen cookie. * Until we can fix this, we use the file-based work-around. * And fixing this may take some time, because the basic * stream interface is no longer supported in openjpeg. * (2) See pixReadJp2k() for usage. */ PIX * pixReadMemJp2k(const l_uint8 *data, size_t size, l_uint32 reduction, BOX *box, l_int32 hint, l_int32 debug) { FILE *fp; PIX *pix; PROCNAME("pixReadMemJp2k"); if (!data) return (PIX *)ERROR_PTR("data not defined", procName, NULL); #if 0 /* Avoid the crash for now */ if ((fp = fmemopen((void *)data, size, "r")) == NULL) return (PIX *)ERROR_PTR("stream not opened", procName, NULL); #else L_WARNING("work-around: writing to a temp file\n", procName); if ((fp = tmpfile()) == NULL) return (PIX *)ERROR_PTR("tmpfile stream not opened", procName, NULL); fwrite(data, 1, size, fp); rewind(fp); #endif /* HAVE_FMEMOPEN */ pix = pixReadStreamJp2k(fp, reduction, box, hint, debug); fclose(fp); if (!pix) L_ERROR("pix not read\n", procName); return pix; }
/*! * pixReadJp2k() * * Input: filename * reduction (scaling factor: 1, 2, 4, 8, 16) * box (<optional> for extracting a subregion), can be null * hint (a bitwise OR of L_JP2K_* values; 0 for default) * debug (output callback messages, etc) * Return: pix (8 or 32 bpp), or null on error * * Notes: * (1) This is a special function for reading jp2k files. * The high-level pixReadStream() uses default values: * @reduction = 1 * @box = NULL * (2) This decodes at either full resolution or at a reduction by * a power of 2. The default value @reduction == 1 gives a full * resolution image. Use @reduction > 1 to get a reduced image. * The actual values of @reduction that can be used on an image * depend on the number of resolution levels chosen when the * image was compressed. Typical values might be 1, 2, 4, 8 and 16. * Using a value representing a reduction level that was not * stored when the file was written will fail with the message: * "failed to read the header". * (3) Use @box to decode only a part of the image. The box is defined * at full resolution. It is reduced internally by @reduction, * and clipping to the right and bottom of the image is automatic. * (4) We presently only handle images with 8 bits/sample (bps). * If the image has 16 bps, the read will fail. * (5) There are 4 possible values of samples/pixel (spp). * The values in brackets give the pixel values in the Pix: * spp = 1 ==> grayscale [8 bpp grayscale] * spp = 2 ==> grayscale + alpha [32 bpp rgba] * spp = 3 ==> rgb [32 bpp rgb] * spp = 4 ==> rgba [32 bpp rgba] * (6) The @hint parameter is reserved for future use. */ PIX * pixReadJp2k(const char *filename, l_uint32 reduction, BOX *box, l_int32 hint, l_int32 debug) { FILE *fp; PIX *pix; PROCNAME("pixReadJp2k"); if (!filename) return (PIX *)ERROR_PTR("filename not defined", procName, NULL); if ((fp = fopenReadStream(filename)) == NULL) return (PIX *)ERROR_PTR("image file not found", procName, NULL); pix = pixReadStreamJp2k(fp, reduction, box, hint, debug); fclose(fp); if (!pix) return (PIX *)ERROR_PTR("image not returned", procName, NULL); 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; }