{"record":{"id":"c72269cfc21161c5","repo":"Yalantis/uCrop","slug":"load-png-invalid-bit-depth-u-in-file-s","errorCode":null,"errorMessage":"load_png(): Invalid bit depth %u in file '%s'.","messagePattern":"load_png\\(\\): Invalid bit depth %u in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57470,"sourceCode":"        bit_depth = 8;\n      }\n      if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) {\n        png_set_tRNS_to_alpha(png_ptr);\n        color_type |= PNG_COLOR_MASK_ALPHA;\n      }\n      if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) {\n        png_set_gray_to_rgb(png_ptr);\n        color_type |= PNG_COLOR_MASK_COLOR;\n        is_gray = true;\n      }\n      if (color_type==PNG_COLOR_TYPE_RGB)\n        png_set_filler(png_ptr,0xffffU,PNG_FILLER_AFTER);\n\n      png_read_update_info(png_ptr,info_ptr);\n      if (bit_depth!=8 && bit_depth!=16) {\n        if (!file) cimg::fclose(nfile);\n        png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Invalid bit depth %u in file '%s'.\",\n                              cimg_instance,\n                              bit_depth,nfilename?nfilename:\"(FILE*)\");\n      }\n      const int byte_depth = bit_depth>>3;\n\n      // Allocate memory for image reading.\n      png_bytep *const imgData = new png_bytep[H];\n      for (unsigned int row = 0; row<H; ++row) imgData[row] = new png_byte[(size_t)byte_depth*4*W];\n      png_read_image(png_ptr,imgData);\n      png_read_end(png_ptr,end_info);\n\n      // Read pixel data.\n      if (color_type!=PNG_COLOR_TYPE_RGB && color_type!=PNG_COLOR_TYPE_RGB_ALPHA) {\n        if (!file) cimg::fclose(nfile);\n        png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Invalid color coding type %u in file '%s'.\",","sourceCodeStart":57452,"sourceCodeEnd":57488,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57452-L57488","documentation":"After reading the PNG header, CImg only supports 8-bit and 16-bit per-channel depths (bit_depth != 8 && != 16). Any other depth (1, 2, or 4 bits per pixel) causes CImgIOException, since CImg's pixel buffer cannot represent them directly.","triggerScenarios":"Loading a paletted or grayscale PNG stored with 1/2/4-bit depth (common for icons, small sprites, optimized PNGs) via load_png().","commonSituations":"Icons exported from design tools with 'PNG-8'/indexed or bit-depth-reduced settings; optimized PNGs from compressors like pngquant/oxipng; low-color screenshots.","solutions":["Re-export the image as 8-bit or 16-bit truecolor/grayscale PNG (e.g. magick input.png -depth 8 out.png).","If using pngquant/oxipng, raise the minimum bit depth or skip depth reduction for files destined for CImg.","Check the file with pngcheck -v or magick identify to confirm its bit depth."],"exampleFix":"// shell: before (1/2/4-bit png) -> after\n// magick icon.png -define png:color-type=6 -depth 8 icon8.png\nimg.load_png(\"icon8.png\"); // now 8-bit, loads fine","handlingStrategy":"fallback","validationCode":"// inspect IHDR bit depth (byte 24 of the file) before loading\nbool supportedDepth(const char *p) {\n  unsigned char h[26] = {}; std::FILE *f = std::fopen(p,\"rb\");\n  if (!f) return false;\n  size_t n = std::fread(h,1,26,f); std::fclose(f);\n  return n==26 && (h[24]==8 || h[24]==16);\n}","typeGuard":null,"tryCatchPattern":"try { img.load_png(path); }\ncatch (CImgIOException &e) {\n  img.load(path); // fallback loader / pre-converted 8-bit copy\n}","preventionTips":["Standardize image assets on 8-bit PNGs for processing pipelines","Check bit depth with pngcheck/identify during asset ingestion","Avoid depth-reducing optimizers (pngquant) on inputs fed to CImg"],"tags":["cimg","png","bit-depth","unsupported-format"],"backgroundTag":"unsupported-dtype","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"}