{"record":{"id":"2ebd615e479a4965","repo":"DrKLO/Telegram","slug":"s-transformation-is-not-perfect","errorCode":null,"errorMessage":"%s: transformation is not perfect\n","messagePattern":"(.+?): transformation is not perfect\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegtran.c","lineNumber":579,"sourceCode":"    jpeg_mem_src(&srcinfo, inbuffer, insize);\n  } else\n#endif\n  jpeg_stdio_src(&srcinfo, fp);\n\n  /* Enable saving of extra markers that we want to copy */\n  jcopy_markers_setup(&srcinfo, copyoption);\n\n  /* Read file header */\n  (void)jpeg_read_header(&srcinfo, TRUE);\n\n  /* Any space needed by a transform option must be requested before\n   * jpeg_read_coefficients so that memory allocation will be done right.\n   */\n#if TRANSFORMS_SUPPORTED\n  /* Fail right away if -perfect is given and transformation is not perfect.\n   */\n  if (!jtransform_request_workspace(&srcinfo, &transformoption)) {\n    fprintf(stderr, \"%s: transformation is not perfect\\n\", progname);\n    exit(EXIT_FAILURE);\n  }\n#endif\n\n  /* Read source file as DCT coefficients */\n  src_coef_arrays = jpeg_read_coefficients(&srcinfo);\n\n  /* Initialize destination compression parameters from source values */\n  jpeg_copy_critical_parameters(&srcinfo, &dstinfo);\n\n  /* Adjust destination parameters if required by transform options;\n   * also find out which set of coefficient arrays will hold the output.\n   */\n#if TRANSFORMS_SUPPORTED\n  dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,\n                                                 src_coef_arrays,\n                                                 &transformoption);\n#else","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegtran.c#L561-L597","documentation":"When the -perfect flag is set, jpegtran calls jtransform_request_workspace at jpegtran.c:578. If this returns FALSE, the requested transformation (rotate, flip, crop, transpose) cannot be applied losslessly because the image dimensions are not compatible with the transform's block-alignment requirements. The -perfect flag makes this a hard failure rather than a silent trim.","triggerScenarios":"Rotating a non-square JPEG with -rotate 90 -perfect when width is not a multiple of the MCU height. Cropping with -crop when the target dimensions don't align to JPEG block boundaries (multiples of 8 or 16). Flipping an image whose dimensions aren't multiples of the block size.","commonSituations":"User adds -perfect to guarantee lossless output but the source image has odd dimensions (e.g. 1921x1080). Automated pipeline that always includes -perfect but processes images with varying dimensions. A crop region that doesn't start/end on block boundaries.","solutions":["Remove the -perfect flag to allow jpegtran to trim edges automatically (lossy trim)","Adjust the transform to match block boundaries: crop to multiples of 16 (or 8 for non-progressive)","Pad or pre-resize the image so dimensions are multiples of the MCU size before transforming"],"exampleFix":"// before\njpegtran -perfect -rotate 90 input.jpg -outfile out.jpg\n\n// after\n// remove -perfect to allow trimming\njpegtran -rotate 90 input.jpg -outfile out.jpg\n// or align crop to 16-pixel boundary\njpegtran -perfect -crop 1920x1080+0+0 input.jpg -outfile out.jpg","handlingStrategy":"validation","validationCode":"// Read JPEG dimensions and check MCU alignment before invoking jpegtran -perfect\n#include \"jpeglib.h\"\nint transform_is_safe(const char *path, int transform_type) {\n    struct jpeg_decompress_struct cinfo;\n    struct jpeg_error_mgr jerr;\n    cinfo.err = jpeg_std_error(&jerr);\n    jpeg_create_decompress(&cinfo);\n    FILE *f = fopen(path, \"rb\");\n    if (!f) { jpeg_destroy_decompress(&cinfo); return -1; }\n    jpeg_stdio_src(&cinfo, f);\n    jpeg_read_header(&cinfo, TRUE);\n    int w = cinfo.image_width, h = cinfo.image_height;\n    int block = cinfo.progressive_mode ? 16 : 8;\n    fclose(f); jpeg_destroy_decompress(&cinfo);\n    // for rotate 90: width must be multiple of block\n    // for flip: the flipped dimension must be multiple of block\n    // for crop: all offsets/dims must be multiples of block\n    if (transform_type == ROTATE_90 || transform_type == ROTATE_270)\n        return (w % block == 0) ? 0 : -1;\n    if (transform_type == FLIP_H)\n        return (w % block == 0) ? 0 : -1;\n    if (transform_type == FLIP_V)\n        return (h % block == 0) ? 0 : -1;\n    return 0;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check image dimensions are multiples of the MCU block size (8 or 16) before using -perfect","Remove -perfect if lossless trimming of edge blocks is acceptable","Align crop regions to 16-pixel boundaries when using -perfect"],"tags":["jpeg","jpegtran","transformation","lossless","mcu-alignment"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}