{"record":{"id":"94e7992c43aa193c","repo":"lllyasviel/ControlNet","slug":"malformed-pfm-header-94e799","errorCode":null,"errorMessage":"Malformed PFM header.","messagePattern":"Malformed PFM header\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ldm/modules/midas/utils.py","lineNumber":38,"sourceCode":"        color = None\n        width = None\n        height = None\n        scale = None\n        endian = None\n\n        header = file.readline().rstrip()\n        if header.decode(\"ascii\") == \"PF\":\n            color = True\n        elif header.decode(\"ascii\") == \"Pf\":\n            color = False\n        else:\n            raise Exception(\"Not a PFM file: \" + path)\n\n        dim_match = re.match(r\"^(\\d+)\\s(\\d+)\\s$\", file.readline().decode(\"ascii\"))\n        if dim_match:\n            width, height = list(map(int, dim_match.groups()))\n        else:\n            raise Exception(\"Malformed PFM header.\")\n\n        scale = float(file.readline().decode(\"ascii\").rstrip())\n        if scale < 0:\n            # little-endian\n            endian = \"<\"\n            scale = -scale\n        else:\n            # big-endian\n            endian = \">\"\n\n        data = np.fromfile(file, endian + \"f\")\n        shape = (height, width, 3) if color else (height, width)\n\n        data = np.reshape(data, shape)\n        data = np.flipud(data)\n\n        return data, scale\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/lllyasviel/ControlNet/blob/ed85cd1e25a5ed592f7d8178495b4483de0331bf/ldm/modules/midas/utils.py#L20-L56","documentation":"Raised by read_pfm when the second line of a PFM (Portable Float Map) file does not match the expected 'width height' dimensions pattern (two integers separated by whitespace with trailing newline). The PFM format requires a header of magic number, dimensions line, and scale/endianness line; this error means the dimensions line is corrupt, missing, or the file is truncated.","triggerScenarios":"Calling midas.utils.read_pfm(path) on a file that passed the magic-number check ('PF'/'Pf') but whose second line fails the regex r'^(\\d+)\\s(\\d+)\\s$' — e.g. dimensions on one line with the scale, CRLF line endings, extra spaces after the line, or a file edited/corrupted after the header.","commonSituations":"Using MiDaS depth outputs that were written by a non-standard PFM writer, files transferred with text-mode FTP that mangled line endings, partially downloaded/truncated .pfm files, or hand-crafted test files where header lines were joined.","solutions":["Inspect the first 2-3 lines of the file (e.g. with open(path,'rb').readline() in a REPL) to see the actual bytes of the dimensions line","If line endings are CRLF, convert with dos2unix or rewrite the header to use '\\n' and a single trailing space as the regex requires","If the file is truncated or corrupt, regenerate it from the source (re-run MiDaS write_depth / download again)","If producing PFM yourself, mimic midas.utils.write_pfm: 'PF\\n' or 'Pf\\n', then '%d %d\\n' % (width, height), then the scale line"],"exampleFix":"# before: file with header 'PF\\r\\n640 480\\r\\n-1.0\\r\\n' fails read_pfm\n# after: normalize line endings before parsing\nwith open(path, 'rb') as f:\n    raw = f.read()\nraw = raw.replace(b'\\r\\n', b'\\n')\nimport io\ndata = read_pfm(io.BytesIO(raw))  # or rewrite to a fixed file","handlingStrategy":"validation","validationCode":"import re\ndef pfm_header_ok(path):\n    with open(path, 'rb') as f:\n        magic = f.readline()\n        if magic.strip() not in (b'PF', b'Pf'):\n            return False\n        return re.match(rb'^(\\d+)\\s(\\d+)\\s$', f.readline()) is not None","typeGuard":null,"tryCatchPattern":"try:\n    depth = read_pfm(path)\nexcept Exception as e:\n    if 'PFM' in str(e):\n        raise IOError(f'Bad PFM file {path}: {e}')\n    raise","preventionTips":["Validate PFM headers before batch-processing directories of depth maps","Keep depth files in binary mode during transfer (no text-mode FTP)","When writing PFMs, always use midas.utils.write_pfm so headers match read_pfm's regex"],"tags":["midas","pfm","file-format","depth-estimation","header-parsing"],"backgroundTag":"malformed-image-file-header","analyzedSha":"ed85cd1e25a5ed592f7d8178495b4483de0331bf","analyzedAt":"2026-08-27T12:58:54.167Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}