- if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
- || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
- fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
- goto fin;
- }
-
- if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL)) == NULL) {
- goto fin;
- }
- if ((info = png_create_info_struct(png)) == NULL) {
- goto fin;
- }
+ {
+ opj_image_t *image = NULL;
+ opj_image_cmptparm_t cmptparm[4];
+ OPJ_UINT32 nr_comp;
+ convert_XXx32s_C1R cvtXXTo32s = NULL;
+ convert_32s_CXPX cvtCxToPx = NULL;
+ OPJ_INT32* planes[4];
+ double gamma;
+ int bit_depth, interlace_type, compression_type, filter_type;
+ OPJ_UINT32 i;
+ png_uint_32 width, height = 0U;
+ int color_type;
+ OPJ_BYTE** rows = NULL;
+ OPJ_INT32* row32s = NULL;
+
+ png_init_io(png, reader);
+ png_set_sig_bytes(png, MAGIC_SIZE);
+
+ png_read_info(png, info);
+
+ if (png_get_IHDR(png, info, &width, &height,
+ &bit_depth, &color_type, &interlace_type,
+ &compression_type, &filter_type) == 0) {
+ return image;
+ }
+ *pheight = height;
+
+ /* png_set_expand():
+ * expand paletted images to RGB, expand grayscale images of
+ * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
+ * to alpha channels.
+ */
+ if (color_type == PNG_COLOR_TYPE_PALETTE) {
+ png_set_expand(png);
+ }