{"record":{"id":"eb00c621331852d4","repo":"siyuan-note/siyuan","slug":"encode-heif-preview-w","errorCode":null,"errorMessage":"encode HEIF preview: %w","messagePattern":"encode HEIF preview: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/heif/convert.go","lineNumber":146,"sourceCode":"\t\treturn nil, ErrImageTooLarge\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\tif mode == ModeThumbnail && img.Bounds().Dx() > thumbnailWidth {\n\t\timg = imaging.Resize(img, thumbnailWidth, 0, imaging.Lanczos)\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\n\tquality := previewQuality\n\tif mode == ModeThumbnail {\n\t\tquality = thumbnailQuality\n\t}\n\toutput := bytes.NewBuffer(make([]byte, 0, estimatedJPEGSize(img)))\n\tif err = jpeg.Encode(output, img, &jpeg.Options{Quality: quality}); err != nil {\n\t\treturn nil, fmt.Errorf(\"encode HEIF preview: %w\", err)\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn output.Bytes(), nil\n}\n\nfunc ImageSize(source []byte) (width, height int, err error) {\n\tif len(source) == 0 {\n\t\treturn 0, 0, errors.New(\"empty HEIF image\")\n\t}\n\tif len(source) > MaxInputBytes {\n\t\treturn 0, 0, ErrInputTooLarge\n\t}\n\tconversionSlots <- struct{}{}\n\tdefer func() {\n\t\t<-conversionSlots\n\t}()","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/heif/convert.go#L128-L164","documentation":"This error wraps a failure from Go's image/jpeg encoder while producing the JPEG preview/thumbnail of a decoded HEIF image. jpeg.Encode essentially only fails when the source image contains pixel values outside the encoder's supported representation (e.g. out-of-range or nil image data), so in practice this indicates the decoder returned an image the JPEG encoder cannot serialize. It is an internal conversion-stage failure, not a problem with the input file itself.","triggerScenarios":"Calling heif convert (via ModePreview or ModeThumbnail paths) on a valid HEIF whose decoded image yields pixel data jpeg.Encode rejects — e.g. a decoded image with color model values outside 0..65535 or a degenerate/empty image buffer.","commonSituations":"Rare; usually seen after upgrading the internal h265heic decoder so it emits a new pixel format, or with malformed-but-decodable HEIF files producing corrupt pixel buffers. Users encounter it while previewing or thumbnailing HEIC/HEIF photos (e.g. iPhone photos) in SiYuan assets.","solutions":["Re-test with a different HEIF source file to confirm it is file-specific corruption","Check the wrapped cause (%w) to identify the exact jpeg.Encode failure and the image's color model","Update the internal h265heic decoder so decoded images are standard *image.NRGBA/*image.YCbCr before encoding","If a specific photo reproducibly fails, re-export it (e.g. as JPEG) outside the app and re-import"],"exampleFix":"// before\nimg, err := decodeImage(source)\n...\nif err = jpeg.Encode(output, img, &jpeg.Options{Quality: quality}); err != nil {\n\treturn nil, fmt.Errorf(\"encode HEIF preview: %w\", err)\n}\n// after\nimg, err := decodeImage(source)\n...\n// normalize to a JPEG-safe pixel format before encoding\nrgba := image.NewNRGBA(img.Bounds())\ndraw.Draw(rgba, rgba.Bounds(), img, img.Bounds().Min, draw.Src)\nif err = jpeg.Encode(output, rgba, &jpeg.Options{Quality: quality}); err != nil {\n\treturn nil, fmt.Errorf(\"encode HEIF preview: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if img == nil || img.Bounds().Empty() {\n\treturn errors.New(\"decoded HEIF image is empty; skip JPEG encoding\")\n}","typeGuard":null,"tryCatchPattern":"if _, err := convert(ctx, data, heif.ModePreview); err != nil {\n\tif strings.Contains(err.Error(), \"encode HEIF preview\") {\n\t\tlog.Warnf(\"HEIF preview encode failed: %v\", err)\n\t\treturn placeholderPreview\n\t}\n\treturn err\n}","preventionTips":["Normalize decoded images to image.NRGBA before JPEG encoding","Reject zero-dimension decoded images before encoding","Keep the decoder pixel format in sync with what image/jpeg supports"],"tags":["image-encoding","jpeg","heif","conversion"],"backgroundTag":"unsupported-operation","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}