{"record":{"id":"633f8226e225ce91","repo":"Yalantis/uCrop","slug":"save-jpeg-unable-to-save-data-in-file-unle","errorCode":null,"errorMessage":"save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.","messagePattern":"save_jpeg\\(\\): Unable to save data in '\\(\\*FILE\\)' unless libjpeg is enabled\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61667,"sourceCode":"    const CImg<T>& save_jpeg(std::FILE *const file, const unsigned int quality=100) const {\n      return _save_jpeg(file,0,quality);\n    }\n\n    const CImg<T>& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_jpeg(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n\n#ifndef cimg_use_jpeg\n      if (!file) return save_other(filename,quality);\n      else throw CImgIOException(_cimg_instance\n                                 \"save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.\",\n                                 cimg_instance);\n#else\n      unsigned int dimbuf = 0;\n      J_COLOR_SPACE colortype = JCS_RGB;\n\n      switch (_spectrum) {\n      case 1 : dimbuf = 1; colortype = JCS_GRAYSCALE; break;\n      case 2 : dimbuf = 3; colortype = JCS_RGB; break;\n      case 3 : dimbuf = 3; colortype = JCS_RGB; break;\n      default : dimbuf = 4; colortype = JCS_CMYK; break;\n      }\n\n      // Call libjpeg functions.\n      struct jpeg_compress_struct cinfo;\n      struct jpeg_error_mgr jerr;\n      cinfo.err = jpeg_std_error(&jerr);\n      jpeg_create_compress(&cinfo);","sourceCodeStart":61649,"sourceCodeEnd":61685,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61649-L61685","documentation":"When CImg is compiled without cimg_use_jpeg, JPEG encoding via libjpeg is unavailable. Saving to an already-open FILE* cannot fall back to format detection by extension (that path needs a filename), so CImg throws CImgIOException stating the data cannot be saved to a FILE* unless libjpeg support is enabled.","triggerScenarios":"Calling img.save_jpeg(std::FILE*) (or _save_jpeg with a non-null file) in a build where cimg_use_jpeg is NOT defined. Saving via a filename instead falls back to save_other().","commonSituations":"Android NDK / uCrop JNI builds where libjpeg was not linked, plain builds with no cimg_use_jpeg define, or third-party prebuilt CImg.h without JPEG support compiled in.","solutions":["Define cimg_use_jpeg and link against libjpeg (and its headers) before including CImg.h.","Pass a filename instead of a FILE* so CImg can fall back to save_other() (slower, but works without libjpeg).","Save to a different format that is enabled (e.g. PNG via libpng) or serialize with save_raw/save_other by extension."],"exampleFix":"// before (no cimg_use_jpeg)\nstd::FILE *f = fopen(\"out.jpg\", \"wb\");\nimg.save_jpeg(f);\n// after\n#define cimg_use_jpeg 1\n#include <jpeglib.h> // link -ljpeg\nimg.save_jpeg(\"out.jpg\");","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  img.save_jpeg(file, quality);\n} catch (const cimg_library::CImgIOException &e) {\n  // libjpeg not enabled in this build\n  img.save_png(\"out.png\"); // fallback to an enabled codec\n}","preventionTips":["Define cimg_use_jpeg and link libjpeg in build configs (CMake/NDK).","Detect codec support at startup rather than at first save.","Prefer filename overloads in builds with optional codecs so save_other() can kick in."],"tags":["cimg","jpeg","build-config","missing-dependency"],"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-17T15:17:12.973Z"}