{"record":{"id":"730320ac3465076e","repo":"Yalantis/uCrop","slug":"load-png-unable-to-load-data-from-file-unl","errorCode":null,"errorMessage":"load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.","messagePattern":"load_png\\(\\): Unable to load data from '\\(FILE\\*\\)' unless libpng is enabled\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57368,"sourceCode":"      return _load_png(file,0,bits_per_value);\n    }\n\n    //! Load image from a PNG file \\newinstance.\n    static CImg<T> get_load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {\n      return CImg<T>().load_png(file,bits_per_value);\n    }\n\n    // (Note: Most of this function has been written by Eric Fausett).\n    CImg<T>& _load_png(std::FILE *const file, const char *const filename, unsigned int *const bits_per_value) {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_png(): Specified filename is (null).\",\n                                    cimg_instance);\n\n#ifndef cimg_use_png\n      cimg::unused(bits_per_value);\n      if (file)\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.\",\n                              cimg_instance);\n\n      else return load_other(filename);\n#else\n      // Open file and check for PNG validity.\n#if defined __GNUC__\n      const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning\n      std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,\"rb\");\n#else\n      const char *nfilename = filename;\n      std::FILE *nfile = file?file:cimg::fopen(nfilename,\"rb\");\n#endif\n      unsigned char pngCheck[8] = {};\n      cimg::fread(pngCheck,8,(std::FILE*)nfile);\n      if (png_sig_cmp(pngCheck,0,8)) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance","sourceCodeStart":57350,"sourceCodeEnd":57386,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57350-L57386","documentation":"CImg is compiled without cimg_use_png, so libpng support is disabled and the loader can only read from a named file (via the generic load_other path). Reading from a FILE* stream is impossible, so a CImgIOException is thrown.","triggerScenarios":"Building CImg without -Dcimg_use_png (libpng not linked) and calling load_png(std::FILE*) or load_png(istream) with a file handle but no filename.","commonSituations":"Android/JNI builds (like this ucrop NDK build) that omitted the libpng define in Android.mk/CMakeLists; switching code from filename-based loading to stream-based loading on a build that never had libpng enabled.","solutions":["Rebuild with cimg_use_png defined and link against libpng (e.g. add -Dcimg_use_png -lpng).","Until rebuilt, call load_png(filename) instead of load_png(FILE*) so the generic fallback path is used.","Verify the preprocessor define reaches the TU that includes CImg.h (check build flags / jni config)."],"exampleFix":"// build: LOCAL_CFLAGS += -Dcimg_use_png ; LOCAL_LDLIBS += -lpng\n// before\nimg.load_png(file); // throws without libpng\n// after\nimg.load_png(\"/path/to/image.png\");","handlingStrategy":"fallback","validationCode":"#ifdef cimg_use_png\n  img.load_png(file);\n#else\n  img.load_png(filename); // stream loading unsupported\n#endif","typeGuard":null,"tryCatchPattern":"try { img.load_png(file); }\ncatch (CImgIOException &e) { img.load_png(filename.c_str()); }","preventionTips":["Define cimg_use_png in a shared build-config header so all TUs agree","Add a CI build with a PNG stream-loading test","Document required preprocessor defines for stream I/O"],"tags":["cimg","libpng","build-configuration"],"backgroundTag":"missing-optional-dependency","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}