Yalantis/uCrop · warning
load_jpeg(): Incomplete data in file '%s'.
Error message
load_jpeg(): Incomplete data in file '%s'.
What it means
CImg<T>::load_jpeg() decodes a JPEG via libjpeg; if jpeg_read_scanlines() fails to return a scanline partway through the image, it warns non-fatally and breaks, leaving the image with the scanlines decoded so far (rest uninitialized/zero). This indicates truncated or corrupt JPEG data after the header.
Source
Thrown at ucrop/src/main/jni/CImg.h:57068
if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) {
if (!file) {
cimg::fclose(nfile);
return load_other(nfilename);
} else
throw CImgIOException(_cimg_instance
"load_jpeg(): Failed to load JPEG data from file '%s'.",
cimg_instance,nfilename?nfilename:"(FILE*)");
}
CImg<ucharT> buffer(cinfo.output_width*cinfo.output_components);
JSAMPROW row_pointer[1];
try { assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); }
catch (...) { if (!file) cimg::fclose(nfile); throw; }
T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height,
*ptr_a = _data + 3UL*_width*_height;
while (cinfo.output_scanline<cinfo.output_height) {
*row_pointer = buffer._data;
if (jpeg_read_scanlines(&cinfo,row_pointer,1)!=1) {
cimg::warn(_cimg_instance
"load_jpeg(): Incomplete data in file '%s'.",
cimg_instance,nfilename?nfilename:"(FILE*)");
break;
}
const unsigned char *ptrs = buffer._data;
switch (_spectrum) {
case 1 : {
cimg_forX(*this,x) *(ptr_r++) = (T)*(ptrs++);
} break;
case 3 : {
cimg_forX(*this,x) {
*(ptr_r++) = (T)*(ptrs++);
*(ptr_g++) = (T)*(ptrs++);
*(ptr_b++) = (T)*(ptrs++);
}
} break;
case 4 : {
cimg_forX(*this,x) {View on GitHub (pinned to f788b534b4)
Solutions
- Verify the JPEG file is complete (re-download/re-copy; check end-of-image marker EOI 0xFFD9)
- Validate the source file size against the dimensions declared in the SOF header before decoding
- Treat images loaded after this warning as suspect: check returned dimensions/content before use, or rethrow
- Use a tolerant/robust JPEG decoder or repair tool (e.g. jpegtran with -restart, gfxutil-style repair) for partially recoverable files
Example fix
// before
CImg<unsigned char> img;
img.load_jpeg("truncated.jpg"); // partially decoded, no exception
// after
CImg<unsigned char> img;
img.load_jpeg("truncated.jpg");
std::FILE* f = std::fopen("truncated.jpg", "rb");
bool has_eoi = false; int c, prev = 0;
while ((c = std::fgetc(f)) != EOF) { if (prev == 0xFF && c == 0xD9) has_eoi = true; prev = c; }
std::fclose(f);
if (!has_eoi) throw std::runtime_error("JPEG truncated (no EOI marker)"); Defensive patterns
Strategy: validation
Validate before calling
bool jpeg_complete(const char* path) {
std::FILE* f = std::fopen(path, "rb"); if (!f) return false;
int c, prev = 0, ok = 0;
while ((c = std::fgetc(f)) != EOF) { if (prev == 0xFF && c == 0xD9) { ok = 1; break; } prev = c; }
std::fclose(f);
return ok;
}
if (!jpeg_complete("img.jpg")) throw std::runtime_error("truncated JPEG"); Type guard
bool has_jpeg_magic(const unsigned char* d, size_t n) {
return n >= 4 && d[0] == 0xFF && d[1] == 0xD8 &&
d[n-2] == 0xFF && d[n-3] == 0xD9;
} Prevention
- Check for the JPEG EOI marker (FFD9) before trusting a loaded image
- Re-download files whose size differs from the source
- Validate declared dimensions vs file size before decoding
- Treat images from untrusted sources as suspect; verify content after load
When it happens
Trigger: Loading a JPEG file that ends prematurely (truncated download/copy), a corrupted bitstream where entropy-coded data is cut off, or a FILE* whose read fails mid-decode.
Common situations: Partially downloaded .jpg files, images recovered from damaged storage, interrupted uploads, concatenation/encoding bugs producing headers with dimensions larger than actual data.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- load_jpeg(): Unable to load data from '(FILE*)' unless libjp
- load_jpeg(): Error message returned by libjpeg: %s.
- load_jpeg(): Failed to load JPEG data from file '%s'.
- load_jpeg(): Specified filename is (null).
- load_pnm(): Specified image dimensions in file '%s' exceed f
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/f6ea1318b0a018bb.
Report an issue: GitHub.