{"record":{"id":"7578586eb4747cca","repo":"Yalantis/uCrop","slug":"save-analyze-unsupported-pixel-type-s-for-fi","errorCode":null,"errorMessage":"save_analyze(): Unsupported pixel type '%s' for file '%s'.","messagePattern":"save_analyze\\(\\): Unsupported pixel type '(.+?)' for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62949,"sourceCode":"      ((char*)&(header[38]))[0] = 114;\n      ((short*)&(header[40]))[0] = 4;\n      ((short*)&(header[40]))[1] = (short)_width;\n      ((short*)&(header[40]))[2] = (short)_height;\n      ((short*)&(header[40]))[3] = (short)_depth;\n      ((short*)&(header[40]))[4] = (short)_spectrum;\n      if (!cimg::strcasecmp(pixel_type(),\"bool\") ||\n          !cimg::strcasecmp(pixel_type(),\"uint8\") ||\n          !cimg::strcasecmp(pixel_type(),\"int8\")) datatype = 2;\n      if (!cimg::strcasecmp(pixel_type(),\"uint16\") ||\n          !cimg::strcasecmp(pixel_type(),\"int16\")) datatype = 4;\n      if (!cimg::strcasecmp(pixel_type(),\"uint32\") ||\n          !cimg::strcasecmp(pixel_type(),\"int32\")) datatype = 8;\n      if (!cimg::strcasecmp(pixel_type(),\"uint64\") ||\n          !cimg::strcasecmp(pixel_type(),\"int64\")) datatype = 8;\n      if (!cimg::strcasecmp(pixel_type(),\"float32\")) datatype = 16;\n      if (!cimg::strcasecmp(pixel_type(),\"float64\")) datatype = 64;\n      if (datatype<0)\n        throw CImgIOException(_cimg_instance\n                              \"save_analyze(): Unsupported pixel type '%s' for file '%s'.\",\n                              cimg_instance,\n                              pixel_type(),filename);\n\n      ((short*)&(header[70]))[0] = datatype;\n      ((short*)&(header[72]))[0] = sizeof(T);\n      ((float*)&(header[108]))[0] = (float)(*iname?0:header.width());\n      ((float*)&(header[112]))[0] = 1;\n      ((float*)&(header[76]))[0] = 0;\n      if (voxel_size) {\n        ((float*)&(header[76]))[1] = voxel_size[0];\n        ((float*)&(header[76]))[2] = voxel_size[1];\n        ((float*)&(header[76]))[3] = voxel_size[2];\n      } else ((float*)&(header[76]))[1] = ((float*)&(header[76]))[2] = ((float*)&(header[76]))[3] = 1;\n      file = cimg::fopen(hname,\"wb\");\n      cimg::fwrite(header._data,header.width(),file);\n      if (*iname) { cimg::fclose(file); file = cimg::fopen(iname,\"wb\"); }\n      cimg::fwrite(_data,size(),file);","sourceCodeStart":62931,"sourceCodeEnd":62967,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62931-L62967","documentation":"The Analyze 7.5 header format only stores a limited set of datatypes; save_analyze() maps pixel_type() strings to header datatype codes (8 for 32-bit ints/64-bit-as-int32, 16 for float32, 64 for float64). If after all comparisons datatype is still negative, meaning the image's element type has no Analyze representation, it throws CImgIOException. Note the writer also aliases some types (e.g. uint64/int64 both written as int32) so errors only occur for widths like unsigned char/short.","triggerScenarios":"Calling save_analyze() on a CImg<unsigned char>, CImg<unsigned short>, or other type with no datatype mapping; calling it via generic save(\"file.hdr\") with an 8/16-bit image instance.","commonSituations":"Saving segmentation masks or label maps stored as uint8; code refactored from float volumes to byte volumes; library version where fewer pixel types were registered for Analyze output.","solutions":["Cast the image to a supported type first: CImg<float>(img).save_analyze(filename) or img.get_float32().save_analyze(...).","If exact 8-bit storage is required, use save_analyze on a type-mapped copy or switch to a format that supports unsigned char natively (PNG, NIfTI).","Check pixel_type() at runtime and refuse unsupported saves early with a clear app-level message.","Update CImg if a newer version extends the Analyze datatype table."],"exampleFix":"// before\nCImg<unsigned char> labels(256,256,1,1,0);\nlabels.save_analyze(\"seg.hdr\"); // throws\n\n// after\nCImg<float> labelf(labels);\nlabelf.save_analyze(\"seg.hdr\");","handlingStrategy":"validation","validationCode":"static const char* kAnalyzeTypes[] = {\"uint32\",\"int32\",\"uint64\",\"int64\",\"float32\",\"float64\"};\nbool ok = false;\nfor (const char* t : kAnalyzeTypes)\n  if (!cimg::strcasecmp(img.pixel_type(), t)) { ok = true; break; }\nif (!ok) throw std::runtime_error(std::string(\"Analyze unsupported for \") + img.pixel_type());","typeGuard":"template <typename T>\nconstexpr bool analyze_supported() {\n  return sizeof(T) >= 4; // Analyze writer only maps >=32-bit types\n}","tryCatchPattern":"try {\n  img.save_analyze(\"seg.hdr\");\n} catch (const CImgIOException& e) {\n  std::fprintf(stderr, \"converting to float32 for Analyze: %s\\n\", e.what());\n  CImg<float>(img).save_analyze(\"seg.hdr\");\n}","preventionTips":["Cast 8/16-bit images to float32 before Analyze export","Remember some types are silently downcast (int64/uint64 -> int32)","Verify datatype mapping against the Analyze 7.5 header spec"],"tags":["cimg","analyze","pixel-type","datatype"],"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"}