{"record":{"id":"512e03f8660f492d","repo":"Yalantis/uCrop","slug":"load-png-invalid-color-coding-type-u-in-file","errorCode":null,"errorMessage":"load_png(): Invalid color coding type %u in file '%s'.","messagePattern":"load_png\\(\\): Invalid color coding type %u in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57487,"sourceCode":"        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'.\",\n                              cimg_instance,\n                              color_type,nfilename?nfilename:\"(FILE*)\");\n      }\n      const bool is_alpha = (color_type==PNG_COLOR_TYPE_RGBA);\n      try { assign(W,H,1,(is_gray?1:3) + (is_alpha?1:0)); }\n      catch (...) { if (!file) cimg::fclose(nfile); throw; }\n      T\n        *ptr_r = data(0,0,0,0),\n        *ptr_g = is_gray?0:data(0,0,0,1),\n        *ptr_b = is_gray?0:data(0,0,0,2),\n        *ptr_a = !is_alpha?0:data(0,0,0,is_gray?1:3);\n      switch (bit_depth) {\n      case 8 : {\n        cimg_forY(*this,y) {\n          const unsigned char *ptrs = (unsigned char*)imgData[y];\n          cimg_forX(*this,x) {\n            *(ptr_r++) = (T)*(ptrs++);","sourceCodeStart":57469,"sourceCodeEnd":57505,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57469-L57505","documentation":"CImg's load_png only handles PNG_COLOR_TYPE_RGB and PNG_COLOR_TYPE_RGB_ALPHA. Grayscale, palette/indexed, and grayscale+alpha images throw CImgIOException even though libpng read them fine — the loader's post-read check rejects those color types.","triggerScenarios":"Loading a grayscale (PNG_COLOR_TYPE_GRAY) PNG, a palette/indexed PNG-8 (icons, optimized assets), or a gray+alpha PNG via load_png().","commonSituations":"Black-and-white scans or screenshots saved as grayscale PNG; icons optimized to indexed color by pngquant; UI assets exported as 'PNG-8' from design tools.","solutions":["Re-export as RGB or RGBA PNG: magick input.png PNG24:output.png (or PNG32 for alpha).","Patch the loader to add png_set_palette_to_rgb/png_set_gray_to_rgb conversions (this custom CImg.h copy rejects them after conversion).","Use img.load(path) (format auto-detect) if it routes to a more permissive loader in this build.","Confirm the color type with pngcheck -v before distributing assets to the pipeline."],"exampleFix":"// shell: convert grayscale/indexed PNG to RGBA\n// magick icon.png PNG32:icon_rgba.png\nimg.load_png(\"icon_rgba.png\"); // color type 6 (RGBA), accepted","handlingStrategy":"fallback","validationCode":"// read IHDR color type (byte 25): 2=RGB, 6=RGBA are supported\nbool supportedColorType(const char *p) {\n  unsigned char h[27] = {}; std::FILE *f = std::fopen(p,\"rb\");\n  if (!f) return false;\n  size_t n = std::fread(h,1,27,f); std::fclose(f);\n  return n==27 && (h[25]==2 || h[25]==6);\n}","typeGuard":null,"tryCatchPattern":"try { img.load_png(path); }\ncatch (CImgIOException &e) {\n  img.load(path); // fallback loader may handle gray/palette\n}","preventionTips":["Convert all pipeline inputs to RGB/RGBA PNGs at ingest","Avoid pngquant/indexed-color exports for images destined for this loader","Validate color type with pngcheck during asset build"],"tags":["cimg","png","color-type","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"}