{"record":{"id":"e66e563d46338bf8","repo":"Yalantis/uCrop","slug":"save-png-instance-has-pixel-values-in-g-g","errorCode":null,"errorMessage":"save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.","messagePattern":"save_png\\(\\): Instance has pixel values in \\[%g,%g\\], probable type overflow in file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62097,"sourceCode":"      const char *nfilename = filename;\n      std::FILE *nfile = file?file:cimg::fopen(nfilename,\"wb\");\n      double stmin, stmax = (double)max_min(stmin);\n#endif\n\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_png(): Instance is volumetric, only the first slice will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename);\n\n      if (_spectrum>4)\n        cimg::warn(_cimg_instance\n                   \"save_png(): Instance is multispectral, only the three first channels will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename);\n\n      if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)\n        cimg::warn(_cimg_instance\n                   \"save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.\",\n                   cimg_instance,\n                   stmin,stmax,filename);\n\n      // Setup PNG structures for write.\n      png_voidp user_error_ptr = 0;\n      png_error_ptr user_error_fn = 0, user_warning_fn = 0;\n      png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,user_error_ptr, user_error_fn,\n                                                    user_warning_fn);\n      if (!png_ptr){\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"save_png(): Failed to initialize 'png_ptr' structure when saving file '%s'.\",\n                              cimg_instance,\n                              nfilename?nfilename:\"(FILE*)\");\n      }\n      png_infop info_ptr = png_create_info_struct(png_ptr);\n      if (!info_ptr) {","sourceCodeStart":62079,"sourceCodeEnd":62115,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62079-L62115","documentation":"Warning from CImg<T>::save_png() when the pixel value range does not fit the target PNG bit depth: negative values present, max >= 256 for 8-bit output, or max >= 65536 for 16-bit. During conversion to the png_byte/png_uint_16 buffers values are truncated, producing clipped or wrapped output. The save itself succeeds.","triggerScenarios":"Calling save_png() with bytes_per_pixel==1 and stmax>=256, or bytes_per_pixel==2 and stmax>=65536, or any negative stmin (from max_min() computed just before the check).","commonSituations":"Saving float [0,1] images without scaling (all-black output), filtered images with negative values, or >16-bit scientific data to PNG.","solutions":["Normalize: img.normalize(0,255) before save_png (or 0,65535 for 16-bit)","Clamp with img.cut(0,255) to keep scale and trim outliers","Shift negatives into range if the data allows","Use a floating-point-capable format (PFM, TIFF, .cimg) instead"],"exampleFix":"// before\nimg.save_png(\"out.png\"); // float [0,1]\n// after\nimg.get_normalize(0,255).save_png(\"out.png\");","handlingStrategy":"validation","validationCode":"double mn, mx = img.max_min(mn);\nif (mn < 0 || mx >= 256) img.normalize(0,255); // then save_png","typeGuard":null,"tryCatchPattern":"// cimg::warn does not throw; check the value range before writing","preventionTips":["Normalize or cut values to 0-255 before save_png","Check img.min()/img.max() before encode","Use 16-bit PNG or float formats for wider ranges"],"tags":["image-encoding","value-range","png"],"backgroundTag":"value-out-of-range","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"}