{"record":{"id":"50b5e6d01afc7696","repo":"siyuan-note/siyuan","slug":"read-heif-image-dimensions-w","errorCode":null,"errorMessage":"read HEIF image dimensions: %w","messagePattern":"read HEIF image dimensions: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/heif/convert.go","lineNumber":186,"sourceCode":"\t}\n\twidth, height = img.Bounds().Dx(), img.Bounds().Dy()\n\tif !validDimensions(width, height) {\n\t\treturn 0, 0, ErrImageTooLarge\n\t}\n\treturn width, height, nil\n}\n\nfunc decodeImage(source []byte) (img image.Image, err error) {\n\tdefer func() {\n\t\tif recovered := recover(); recovered != nil {\n\t\t\timg = nil\n\t\t\terr = fmt.Errorf(\"decode HEIF image: %v\", recovered)\n\t\t}\n\t}()\n\n\tconfig, err := goheic.DecodeConfigBytes(source)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read HEIF image dimensions: %w\", err)\n\t}\n\tif !validDimensions(config.Width, config.Height) {\n\t\treturn nil, ErrImageTooLarge\n\t}\n\n\timg, err = goheic.DecodeBytes(source, goheic.Options{\n\t\tAutoRotate:     true,\n\t\tFrameSizeLimit: maxPixels,\n\t\tThreads:        1,\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"decode HEIF image: %w\", err)\n\t}\n\treturn img, nil\n}\n\nfunc validDimensions(width, height int) bool {\n\treturn width > 0 && height > 0 && width <= maxDimension && height <= maxDimension &&","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/heif/convert.go#L168-L204","documentation":"This wraps a failure from goheic.DecodeConfigBytes, which parses only the HEIF headers to obtain width/height without full pixel decode. It means the file's container/box structure could not be parsed for dimensions — the input is not a readable HEIF file. The %w wrap preserves the underlying parse error.","triggerScenarios":"convert or ImageSize called with bytes that are not valid HEIF: wrong magic (not ftyp heic/heix/avif), truncated file cut inside the meta boxes, or a structurally invalid ISOBMFF container.","commonSituations":"A JPEG/PNG renamed to .heic; an interrupted upload or sync leaving a truncated file; an exotic HEIF variant (multi-image, uncompressed) the parser does not accept.","solutions":["Verify the file is genuinely HEIF (ftyp box brand heic/heix/mif1/avif) — check the first bytes","Re-download/re-sync the asset; truncation is the most common cause","Open the file in another viewer to confirm integrity","Convert the image to JPEG/PNG if it is a format variant the parser rejects"],"exampleFix":"// before\ndata, _ := os.ReadFile(path)\nw, h, err := heif.ImageSize(data)\n// after\ndata, err := os.ReadFile(path)\nif err != nil {\n\treturn 0, 0, err\n}\nif !looksLikeHeif(data) { // check ftyp brand before calling\n\treturn 0, 0, fmt.Errorf(\"%s is not a HEIF file\", path)\n}\nw, h, err := heif.ImageSize(data)","handlingStrategy":"validation","validationCode":"func isHeifContainer(b []byte) bool {\n\tif len(b) < 12 || string(b[4:8]) != \"ftyp\" {\n\t\treturn false\n\t}\n\tbrand := string(b[8:12])\n\tswitch brand {\n\tcase \"heic\", \"heix\", \"mif1\", \"msf1\", \"hevc\", \"avif\":\n\t\treturn true\n\t}\n\treturn false\n}","typeGuard":"func hasFtypBox(b []byte) bool { return len(b) >= 12 && string(b[4:8]) == \"ftyp\" }","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"read HEIF image dimensions\") {\n\treturn fmt.Errorf(\"%w (file is not a readable HEIF container)\", err)\n}","preventionTips":["Check the ftyp box and brand before calling the library","Verify file integrity (size matches manifest/source) after downloads or sync","Do not rely on file extensions — validate magic bytes","Detect truncation by checking the container's item extents against file size"],"tags":["heif","parse-error","malformed-input","container-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}