From 35093d2105c78164e6e9d7ad56a6f44ba5b1bbfc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 9 Oct 2025 19:22:22 +0530 Subject: [PATCH] Fix frame origins not be de-serialized --- tools/utils/images/loading.go | 4 ++-- tools/utils/images/to_rgb.go | 23 ++++++++++++----------- tools/utils/images/to_rgba.go | 4 ++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tools/utils/images/loading.go b/tools/utils/images/loading.go index b6a05a729..e46e98f6c 100644 --- a/tools/utils/images/loading.go +++ b/tools/utils/images/loading.go @@ -155,9 +155,9 @@ func ImageFrameFromSerialized(s SerializableImageFrame, data []byte) (aa *ImageF return nil, fmt.Errorf("serialized image data has size: %d != %d", len(data), expected) } if s.Is_opaque { - ans.Img, err = NewNRGBWithContiguousRGBPixels(data, s.Width, s.Height) + ans.Img, err = NewNRGBWithContiguousRGBPixels(data, s.Left, s.Top, s.Width, s.Height) } else { - ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Width, s.Height) + ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Left, s.Top, s.Width, s.Height) } return &ans, err } diff --git a/tools/utils/images/to_rgb.go b/tools/utils/images/to_rgb.go index bfa8e50e2..4d4229e01 100644 --- a/tools/utils/images/to_rgb.go +++ b/tools/utils/images/to_rgb.go @@ -45,17 +45,18 @@ func nrgbModel(c color.Color) color.Color { return c } r, g, b, a := c.RGBA() - if a == 0xffff { + switch a { + case 0xffff: + return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} + case 0: + return NRGBColor{0, 0, 0} + default: + // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a. + r = (r * 0xffff) / a + g = (g * 0xffff) / a + b = (b * 0xffff) / a return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} } - if a == 0 { - return NRGBColor{0, 0, 0} - } - // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a. - r = (r * 0xffff) / a - g = (g * 0xffff) / a - b = (b * 0xffff) / a - return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} } var NRGBModel color.Model = color.ModelFunc(nrgbModel) @@ -429,7 +430,7 @@ func NewNRGB(r image.Rectangle) *NRGB { } } -func NewNRGBWithContiguousRGBPixels(p []byte, width, height int) (*NRGB, error) { +func NewNRGBWithContiguousRGBPixels(p []byte, left, top, width, height int) (*NRGB, error) { const bpp = 3 if expected := bpp * width * height; expected != len(p) { return nil, fmt.Errorf("the image width and height dont match the size of the specified pixel data: width=%d height=%d sz=%d != %d", width, height, len(p), expected) @@ -437,6 +438,6 @@ func NewNRGBWithContiguousRGBPixels(p []byte, width, height int) (*NRGB, error) return &NRGB{ Pix: p, Stride: bpp * width, - Rect: image.Rectangle{image.Point{}, image.Point{width, height}}, + Rect: image.Rectangle{image.Point{left, top}, image.Point{left + width, top + height}}, }, nil } diff --git a/tools/utils/images/to_rgba.go b/tools/utils/images/to_rgba.go index b12e11984..ae7211bc6 100644 --- a/tools/utils/images/to_rgba.go +++ b/tools/utils/images/to_rgba.go @@ -406,7 +406,7 @@ func FitImage(width, height, pwidth, pheight int) (final_width int, final_height return width, height } -func NewNRGBAWithContiguousRGBAPixels(p []byte, width, height int) (*image.NRGBA, error) { +func NewNRGBAWithContiguousRGBAPixels(p []byte, left, top, width, height int) (*image.NRGBA, error) { const bpp = 4 if expected := bpp * width * height; expected != len(p) { return nil, fmt.Errorf("the image width and height dont match the size of the specified pixel data: width=%d height=%d sz=%d != %d", width, height, len(p), expected) @@ -414,6 +414,6 @@ func NewNRGBAWithContiguousRGBAPixels(p []byte, width, height int) (*image.NRGBA return &image.NRGBA{ Pix: p, Stride: bpp * width, - Rect: image.Rectangle{image.Point{}, image.Point{width, height}}, + Rect: image.Rectangle{image.Point{left, top}, image.Point{left + width, left + height}}, }, nil }