{"record":{"id":"a4387c9ffa521a65","repo":"DrKLO/Telegram","slug":"jpeg-sampling-factors-must-be-1-4","errorCode":null,"errorMessage":"JPEG sampling factors must be 1..4\n","messagePattern":"JPEG sampling factors must be 1\\.\\.4\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/rdswitch.c","lineNumber":624,"sourceCode":"GLOBAL(boolean)\nset_sample_factors (j_compress_ptr cinfo, char *arg)\n/* Process a sample-factors parameter string, of the form\n *     HxV[,HxV,...]\n * If there are more components than parameters, \"1x1\" is assumed for the rest.\n */\n{\n  int ci, val1, val2;\n  char ch1, ch2;\n\n  for (ci = 0; ci < MAX_COMPONENTS; ci++) {\n    if (*arg) {\n      ch2 = ',';                /* if not set by sscanf, will be ',' */\n      if (sscanf(arg, \"%d%c%d%c\", &val1, &ch1, &val2, &ch2) < 3)\n        return FALSE;\n      if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */\n        return FALSE;\n      if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {\n        fprintf(stderr, \"JPEG sampling factors must be 1..4\\n\");\n        return FALSE;\n      }\n      cinfo->comp_info[ci].h_samp_factor = val1;\n      cinfo->comp_info[ci].v_samp_factor = val2;\n      while (*arg && *arg++ != ',');  /* advance to next segment of arg\n                                         string */\n    } else {\n      /* reached end of parameter, set remaining components to 1x1 sampling */\n      cinfo->comp_info[ci].h_samp_factor = 1;\n      cinfo->comp_info[ci].v_samp_factor = 1;\n    }\n  }\n  return TRUE;\n}\n","sourceCodeStart":606,"sourceCodeEnd":639,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/rdswitch.c#L606-L639","documentation":"The set_sample_factors function processes the -sample argument, which sets horizontal and vertical sampling factors per component. JPEG sampling factors must be in the range 1..4. If any factor value is <= 0 or > 4, the function reports this error and returns FALSE. Sampling factors control chroma subsampling (e.g., 2x2 for 4:2:0, 2x1 for 4:2:2).","triggerScenarios":"Passing -sample with a factor outside 1..4: 'cjpeg -sample 5x1 image.bmp' or '-sample 0x2' or negative values. The sscanf parses val1/val2 and the bounds check val1 <= 0 || val1 > 4 fires.","commonSituations":"Typo in the sampling factor string; misunderstanding the valid range; attempting to use extreme subsampling; passing a quality-related number instead of a sampling factor; wrong separator (the format requires 'x' or 'X' between H and V).","solutions":["Use only values 1-4 for both horizontal and vertical sampling factors.","Common settings: 2x2 (4:2:0 subsampling), 2x1 (4:2:2), 1x1 (no subsampling).","For grayscale or high quality, use 1x1."],"exampleFix":"# before\ncjpeg -sample 5x1 image.bmp > out.jpg\n# after\ncjpeg -sample 2x2 image.bmp > out.jpg","handlingStrategy":"validation","validationCode":"// Pre-validate sample factors argument before calling set_sample_factors\nstatic boolean validate_sample_arg(const char *arg) {\n  const char *p = arg;\n  while (*p) {\n    int v1, v2; char c1, c2 = ',';\n    if (sscanf(p, \"%d%c%d%c\", &v1, &c1, &v2, &c2) < 3) return FALSE;\n    if ((c1 != 'x' && c1 != 'X')) return FALSE;\n    if (v1 < 1 || v1 > 4 || v2 < 1 || v2 > 4) return FALSE;\n    while (*p && *p++ != ',') ;\n  }\n  return TRUE;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use only sampling factors in the range 1-4 for both H and V.","Common values: 2x2 (4:2:0), 2x1 (4:2:2), 1x1 (no subsampling).","Use 'x' or 'X' as the separator between horizontal and vertical factors.","Separate multi-component factors with commas."],"tags":["validation","sampling-factor","argument-error","mozjpeg"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}