RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(native): build 0-origin raster in FromImage/Decode for cropped sub-images (#19308)
xugangqiangc4a019096110
diff --git a/internal/deepdoc/native/croptest/crop_test.go b/internal/deepdoc/native/croptest/crop_test.gonew file mode 100644index 0000000000..fd9776fafb--- /dev/null+++ b/internal/deepdoc/native/croptest/crop_test.go@@ -0,0 +1,79 @@+//go:build cgo++// Package croptest holds regression tests for the pure image-raster helpers+// in the parent native package that do NOT require the DeepDoc testdata+// (ONNX models / fixtures). The parent package's TestMain unconditionally+// skips the whole test binary when that testdata has not been fetched, so a+// pure-logic test placed there would never run in the default build. Keeping+// these tests in a separate package yields an independent test binary that+// runs under `go test ./...` without any external assets.+package croptest++import (+ "image"+ "image/color"+ "testing"++ "ragflow/internal/deepdoc/native"+)++// fillPattern writes a distinct colour to every pixel so a wrong raster+// offset shows up as a value mismatch, not just as a panic.+func fillPattern(img *image.RGBA) {+ b := img.Bounds()+ for y := b.Min.Y; y < b.Max.Y; y++ {+ for x := b.Min.X; x < b.Max.X; x++ {+ img.SetRGBA(x, y, color.RGBA{+ R: uint8((x*3 + y) & 0xff),+ G: uint8((y*5 + 1) & 0xff),+ B: uint8((x + y*2) & 0xff),+ A: 255,+ })+ }+ }+}++// TestFromImageOrigin asserts FromImage handles images whose Bounds().Min is+// not the origin. OCR feeds cropped sub-images (SubImage returns a rectangle+// with a non-zero Min), and indexing the RGBA raster with 0-based coordinates+// while RGBA.PixOffset subtracts Rect.Min yields a negative offset (panic).+func TestFromImageOrigin(t *testing.T) {+ const srcW, srcH = 40, 30++ cases := []struct {+ name string+ rect image.Rectangle+ }{+ {"zero origin", image.Rect(0, 0, 20, 15)},+ {"non-zero origin", image.Rect(7, 5, 27, 20)},+ {"origin offset only in Y", image.Rect(0, 9, 12, 24)},+ }++ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ src := image.NewRGBA(image.Rect(0, 0, srcW, srcH))+ fillPattern(src)++ got, err := native.FromImage(src.SubImage(c.rect))+ if err != nil {+ t.Fatalf("FromImage: unexpected error: %v", err)+ }+ if got.W != c.rect.Dx() || got.H != c.rect.Dy() {+ t.Fatalf("size = %dx%d, want %dx%d", got.W, got.H, c.rect.Dx(), c.rect.Dy())+ }+ if want := got.W * got.H * 3; len(got.Pix) != want {+ t.Fatalf("len(Pix) = %d, want %d", len(got.Pix), want)+ }+ for y := 0; y < got.H; y++ {+ for x := 0; x < got.W; x++ {+ want := src.RGBAAt(c.rect.Min.X+x, c.rect.Min.Y+y)+ d := (y*got.W + x) * 3+ if got.Pix[d] != want.R || got.Pix[d+1] != want.G || got.Pix[d+2] != want.B {+ t.Fatalf("pixel (%d,%d) = (%d,%d,%d), want (%d,%d,%d)",+ x, y, got.Pix[d], got.Pix[d+1], got.Pix[d+2], want.R, want.G, want.B)+ }+ }+ }+ })+ }+}diff --git a/internal/deepdoc/native/image.go b/internal/deepdoc/native/image.goindex c307b8d0f2..2c66034fa8 100644--- a/internal/deepdoc/native/image.go+++ b/internal/deepdoc/native/image.go@@ -55,26 +55,19 @@ func checkImageBounds(b image.Rectangle) error {return nil}-// Decode reads an image file (any format Go's image package can decode,-// e.g. PNG/JPEG) and returns it as RGB pixels. Format is auto-detected,-// matching the Python service's format-agnostic decode.-func Decode(path string) (*Image, error) {- f, err := os.Open(path)- if err != nil {- return nil, err- }- defer f.Close()- img, _, err := image.Decode(f)- if err != nil {- return nil, err- }+// rasterRGBA converts a bounds-checked image.Image into the RGB row-major+// raster the recognizers consume. The source may carry a non-zero Bounds().Min+// (e.g. an OCR SubImage crop): drawing it into a fresh 0-origin RGBA avoids the+// negative PixOffset that 0-based indexing into an offset raster would trigger.+// Decode and FromImage both funnel through this single conversion path.+func rasterRGBA(img image.Image) (*Image, error) {if err := checkImageBounds(img.Bounds()); err != nil {return nil, err}b := img.Bounds()- rgba := image.NewRGBA(b)- draw.Draw(rgba, b, img, b.Min, draw.Src)w, h := b.Dx(), b.Dy()+ rgba := image.NewRGBA(image.Rect(0, 0, w, h))+ draw.Draw(rgba, rgba.Bounds(), img, b.Min, draw.Src)pix := make([]byte, w*h*3)for y := 0; y < h; y++ {for x := 0; x < w; x++ {@@ -88,29 +81,28 @@ func Decode(path string) (*Image, error) {return &Image{W: w, H: h, Pix: pix}, nil}+// Decode reads an image file (any format Go's image package can decode,+// e.g. PNG/JPEG) and returns it as RGB pixels. Format is auto-detected,+// matching the Python service's format-agnostic decode.+func Decode(path string) (*Image, error) {+ f, err := os.Open(path)+ if err != nil {+ return nil, err+ }+ defer f.Close()+ img, _, err := image.Decode(f)+ if err != nil {+ return nil, err+ }+ return rasterRGBA(img)+}+// FromImage converts a standard library image.Image into the RGB row-major// raster the recognizers consume. It is the in-process analogue of Decode// (which reads from a file): the Go DeepDoc client and the PDF parser already// hold an image.Image, so decoding to disk and back is unnecessary.func FromImage(img image.Image) (*Image, error) {- b := img.Bounds()- if err := checkImageBounds(b); err != nil {- return nil, err- }- rgba := image.NewRGBA(b)- draw.Draw(rgba, b, img, b.Min, draw.Src)- w, h := b.Dx(), b.Dy()- pix := make([]byte, w*h*3)- for y := 0; y < h; y++ {- for x := 0; x < w; x++ {- o := rgba.PixOffset(x, y)- d := (y*w + x) * 3- pix[d] = rgba.Pix[o] // R- pix[d+1] = rgba.Pix[o+1] // G- pix[d+2] = rgba.Pix[o+2] // B- }- }- return &Image{W: w, H: h, Pix: pix}, nil+ return rasterRGBA(img)}// FromImages converts a slice of standard library images into the RGB