DrKLO/Telegram · error

%s: can't set sample factors

Error message

%s: can't set sample factors

What it means

cjpeg calls set_sample_factors() to parse -sample (format HxV[,HxV,...] per component). It returns FALSE if sscanf fails to read H x V, if the separator is not 'x'/'X' between H and V or not ',' between pairs, or if H or V is outside 1..4. The wrapper prints this message and calls usage().

Source

Thrown at TMessagesProj/jni/mozjpeg/cjpeg.c:636

        fprintf(stderr, "%s: can't set quality ratings\n", progname);
        usage();
      }

    if (qtablefile != NULL)     /* process -qtables if it was present */
      if (! read_quant_tables(cinfo, qtablefile, force_baseline)) {
        fprintf(stderr, "%s: can't read qtable file\n", progname);
        usage();
      }

    if (qslotsarg != NULL)      /* process -qslots if it was present */
      if (!set_quant_slots(cinfo, qslotsarg))
        usage();

    /* set_quality_ratings sets default subsampling, so the explicit
       subsampling must be set after it */
    if (samplearg != NULL)      /* process -sample if it was present */
      if (! set_sample_factors(cinfo, samplearg)) {
        fprintf(stderr, "%s: can't set sample factors\n", progname);
        usage();
      }

#ifdef C_PROGRESSIVE_SUPPORTED
    if (simple_progressive)     /* process -progressive; -scans can override */
      jpeg_simple_progression(cinfo);
#endif

#ifdef C_MULTISCAN_FILES_SUPPORTED
    if (scansarg != NULL)       /* process -scans if it was present */
      if (!read_scan_script(cinfo, scansarg))
        usage();
#endif
  }

  return argn;                  /* return index of next arg (file name) */
}

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Use the exact format HxV per component separated by commas, each factor 1..4, e.g. -sample 2x2 or -sample 2x2,1x1,1x1.
  2. Validate the input matches ^[1-4]x[1-4](,[1-4]x[1-4])*$ before passing.
  3. Remember fewer entries than components are backfilled with 1x1.

Example fix

# before
cjpeg -sample 2-2 input.ppm > out.jpg
# after
cjpeg -sample 2x2 input.ppm > out.jpg
Defensive patterns

Strategy: validation

Validate before calling

#!/bin/sh
# Validate -sample: HxV[,HxV,...] each in 1..4
SAMPLE='2x2'
printf '%s' "$SAMPLE" | grep -Eq '^[1-4]x[1-4](,[1-4]x[1-4])*$' || { echo 'bad -sample' >&2; exit 1; }
cjpeg -sample "$SAMPLE" in.ppm > out.jpg

Type guard

// n/a: string-format validation in the caller.

Try / catch

if ! cjpeg -sample "$SAMPLE" in.ppm > out.jpg 2>/tmp/e; then
  grep -q "can't set sample factors\|sampling factors must be" /tmp/e && { echo 'fix -sample (HxV, 1..4)'; exit 1; }
fi

Prevention

When it happens

Trigger: Passing -sample with malformed syntax: -sample 2 (missing xV), -sample 2-2 (wrong separator), -sample 4x2x2 (extra), or a factor of 5 or 0, e.g. -sample 5x1.

Common situations: Confusing the HxV notation with NxN or WxH conventions used elsewhere; typos; values from an encoder GUI that uses a different representation; exceeding the libjpeg 1..4 sampling-factor limit.

Related errors


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