DrKLO/Telegram · error

%s: crop dimensions exceed image dimensions %d x %d\n

Error message

%s: crop dimensions exceed image dimensions %d x %d\n

What it means

The -crop region extends beyond the decompressed image: crop_x + crop_width exceeds output_width, or crop_y + crop_height exceeds output_height. Checked after jpeg_start_decompress(). Fatal: exits with failure.

Source

Thrown at TMessagesProj/jni/mozjpeg/djpeg.c:727

      (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
    }
    jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1);
    while (cinfo.output_scanline < cinfo.output_height) {
      num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
                                          dest_mgr->buffer_height);
      (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
    }

  /* Decompress a subregion */
  } else if (crop) {
    JDIMENSION tmp;

    /* Check for valid crop dimensions.  We cannot check these values until
     * after jpeg_start_decompress() is called.
     */
    if (crop_x + crop_width > cinfo.output_width ||
        crop_y + crop_height > cinfo.output_height) {
      fprintf(stderr, "%s: crop dimensions exceed image dimensions %d x %d\n",
              progname, cinfo.output_width, cinfo.output_height);
      exit(EXIT_FAILURE);
    }

    jpeg_crop_scanline(&cinfo, &crop_x, &crop_width);
    if (dest_mgr->calc_buffer_dimensions)
      (*dest_mgr->calc_buffer_dimensions) (&cinfo, dest_mgr);
    else
      ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);

    /* Write output file header.  This is a hack to ensure that the destination
     * manager creates an output image of the proper size.
     */
    tmp = cinfo.output_height;
    cinfo.output_height = crop_height;
    (*dest_mgr->start_output) (&cinfo, dest_mgr);
    cinfo.output_height = tmp;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Read output_width/output_height from jpeg_read_header and compute crop so that X+W <= width and Y+H <= height.
  2. If a tile exceeds bounds, clamp the tile size or reject the request at the caller level.
  3. Use jpeg_crop_scanline via the API so the library adjusts the crop, rather than hardcoding raw geometry.
  4. Recompute crop coordinates whenever the input image's dimensions may have changed.

Example fix

// before
./djpeg -crop 1000x1000+0+0 small.jpg > out.ppm
// after: fit crop inside actual size
# width=640 height=480 -> clamp
./djpeg -crop 640x480+0+0 small.jpg > out.ppm
Defensive patterns

Strategy: validation

Validate before calling

#include <jpeglib.h>
int crop_ok(j_decompress_ptr c, unsigned x, unsigned y, unsigned w, unsigned h) {
  return (x + w) <= c->output_width && (y + h) <= c->output_height;
}

Prevention

When it happens

Trigger: Passing -crop WxH+X+Y (or equivalent) where the rectangle's far corner lies outside the image. Because the check needs output_width/output_height, it can only fire at runtime after the header is parsed.

Common situations: Reusing crop geometry from a larger source image, an image was rotated/transposed upstream changing dimensions, off-by-one in computing X/Y from a grid, or a UI tile request beyond bounds.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/c27c61d6f3fc5daa. Report an issue: GitHub.