Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(deepdoc): crop clean media previews without context bands (#21412)

jay77721651a1c65fce4
diff --git a/internal/deepdoc/parser/pdf/util/crop.go b/internal/deepdoc/parser/pdf/util/crop.go
index c79834a4a3..08cb877f86 100644
--- a/internal/deepdoc/parser/pdf/util/crop.go
+++ b/internal/deepdoc/parser/pdf/util/crop.go
@@ -607,21 +607,37 @@ func CropImageRegion(img image.Image, r pdf.DLARegion) (image.Image, error) {
return cropped, nil
}
-// CropSectionPositions is the tag-free variant of CropSectionImage. It
-// crops directly from a typed []pdf.Position — for example decoded from a
-// chunk's _pdf_positions matrix — instead of re-parsing a position tag
-// string. The page-image map is keyed by zero-indexed page number.
+// CropMediaSectionPositions crops typed PDF positions for image/table
+// previews: exactly the position bounds, no context bands, no edge dimming.
//
-// Python: pdf_parser.py:1802 RAGFlowPdfParser.crop()
-func CropSectionPositions(positions []pdf.Position, decodedImages map[int]image.Image, zoom float64) string {
- return encodeCroppedImage(CropSectionPositionsRaster(positions, decodedImages, zoom))
-}
-
-// CropSectionPositionsRaster crops typed PDF positions and returns the raster
-// without encoding it. Callers that need both OCR and a VLM payload can reuse
-// this image and encode it once after OCR.
-func CropSectionPositionsRaster(positions []pdf.Position, decodedImages map[int]image.Image, zoom float64) image.Image {
- return cropSectionPositionsRaster(positions, decodedImages, zoom)
+// Python: cropout in _extract_table_figure (pdf_parser.py) — figure/table
+// previews are clean DLA-snapped crops, while only text previews go through
+// crop() with its 120pt context bands and putalpha(128) edge dimming.
+// Routing media through the text path tacks darkened neighbour content
+// above/below every figure.
+//
+// Implementation reuses buildSectionRasterPlan and drops its top/bottom
+// context segments (isEdge): the body segments are computed identically in
+// both paths, so there is no second page-spanning loop to keep in sync.
+func CropMediaSectionPositions(positions []pdf.Position, decodedImages map[int]image.Image, zoom float64) string {
+ plan, ok := buildSectionRasterPlan(positions, decodedImages, zoom)
+ if !ok {
+ return ""
+ }
+ body := plan.segments[:0]
+ for _, s := range plan.segments {
+ if !s.isEdge {
+ body = append(body, s)
+ }
+ }
+ plan.segments = body
+ if len(plan.segments) == 0 {
+ return ""
+ }
+ if !measureSectionRasterPlan(plan, decodedImages) {
+ return ""
+ }
+ return encodeCroppedImage(renderSectionRaster(plan, decodedImages))
}
// CropSectionPositionsRasterLimited rejects a section before rendering when
diff --git a/internal/deepdoc/parser/pdf/util/crop_test.go b/internal/deepdoc/parser/pdf/util/crop_test.go
index 7f0256d6cb..3961f18020 100644
--- a/internal/deepdoc/parser/pdf/util/crop_test.go
+++ b/internal/deepdoc/parser/pdf/util/crop_test.go
@@ -126,6 +126,61 @@ func TestCropSectionImage_NegativeCoordNotDropped(t *testing.T) {
}
}
+func TestCropMediaSectionPositionsHasNoContextBands(t *testing.T) {
+ // Media previews must be clean position-only crops (Python cropout):
+ // no 120pt context bands, no putalpha(128) edge dimming. A solid-red
+ // page stays solid red top to bottom.
+ pageImages := map[int]image.Image{
+ 0: makeTestPageImage(200, 800, color.RGBA{255, 0, 0, 255}),
+ }
+ positions := []pdf.Position{{PageNumbers: []int{0}, Left: 20, Right: 120, Top: 300, Bottom: 400}}
+ b64 := CropMediaSectionPositions(positions, pageImages, 1)
+ if b64 == "" {
+ t.Fatal("expected non-empty result")
+ }
+ decoded, _ := base64.StdEncoding.DecodeString(b64)
+ img := decodePNG(t, decoded)
+ bounds := img.Bounds()
+ // Body is 100px tall; measureSectionRasterPlan adds one 6px stitch gap.
+ if bounds.Dx() != 100 || bounds.Dy() != 106 {
+ t.Fatalf("media crop bounds = %v, want 100x106 (body only, no context)", bounds)
+ }
+ for _, y := range []int{0, bounds.Dy() - 7} {
+ r, g, b, _ := img.At(0, y).RGBA()
+ if uint8(r>>8) < 200 || uint8(g>>8) != 0 || uint8(b>>8) != 0 {
+ t.Errorf("row %d = (%d,%d,%d), want undimmed red; edge overlay leaked into media crop", y, uint8(r>>8), uint8(g>>8), uint8(b>>8))
+ }
+ }
+}
+
+func TestCropMediaSectionPositionsSpansPages(t *testing.T) {
+ // A figure spanning two pages stitches body-only segments in page
+ // order: no context bands, no dimming, first page on top.
+ pageImages := map[int]image.Image{
+ 0: makeTestPageImage(200, 400, color.RGBA{255, 0, 0, 255}),
+ 1: makeTestPageImage(200, 400, color.RGBA{0, 0, 255, 255}),
+ }
+ positions := []pdf.Position{{PageNumbers: []int{0, 1}, Left: 20, Right: 120, Top: 300, Bottom: 100}}
+ b64 := CropMediaSectionPositions(positions, pageImages, 1)
+ if b64 == "" {
+ t.Fatal("expected non-empty result")
+ }
+ decoded, _ := base64.StdEncoding.DecodeString(b64)
+ img := decodePNG(t, decoded)
+ // Spanning convention: Top is local to the first page, Bottom local
+ // to the last. Page 0 contributes rows [300,400], page 1 rows [0,100],
+ // plus two 6px stitch gaps: 100 + 100 + 12.
+ if bounds := img.Bounds(); bounds.Dx() != 100 || bounds.Dy() != 212 {
+ t.Fatalf("spanning media crop bounds = %v, want 100x212", bounds)
+ }
+ if r, _, _, _ := img.At(0, 0).RGBA(); uint8(r>>8) < 200 {
+ t.Error("top rows should be page-0 red, got dimmed or wrong content")
+ }
+ if _, _, b, _ := img.At(0, 150).RGBA(); uint8(b>>8) < 200 {
+ t.Error("bottom rows should be page-1 blue, got dimmed or wrong content")
+ }
+}
+
func TestCropSectionImage_ContextPadding(t *testing.T) {
pageImages := map[int]image.Image{
0: makeTestPageImage(200, 800, color.RGBA{255, 0, 0, 255}),
diff --git a/internal/deepdoc/parser/pdf/util/crop_text_test.go b/internal/deepdoc/parser/pdf/util/crop_text_test.go
index e40a26656e..7b52b4affb 100644
--- a/internal/deepdoc/parser/pdf/util/crop_text_test.go
+++ b/internal/deepdoc/parser/pdf/util/crop_text_test.go
@@ -1,6 +1,7 @@
package util
import (
+ "encoding/base64"
"fmt"
"image"
"image/color"
@@ -13,8 +14,17 @@ import (
func TestCropTextSectionPositionsKeepsUnresolvableCrop(t *testing.T) {
positions := []pdf.Position{{PageNumbers: []int{0}, Left: 10, Right: 90, Top: 150, Bottom: 200}}
pages := map[int]image.Image{0: makeTestPageImage(100, 400, color.RGBA{80, 80, 80, 255})}
- if got, want := CropTextSectionPositions(positions, pages, 1), CropSectionPositions(positions, pages, 1); got != want {
- t.Fatal("a crop with no clear rows should retain its body and context boundaries")
+ b64 := CropTextSectionPositions(positions, pages, 1)
+ if b64 == "" {
+ t.Fatal("expected non-empty result")
+ }
+ decoded, _ := base64.StdEncoding.DecodeString(b64)
+ img := decodePNG(t, decoded)
+ // Solid page: no clear rows to snap to, so the text path retains its
+ // body and context boundaries — 114px top band + 50px body + 114px
+ // bottom band + three 6px stitch gaps.
+ if bounds := img.Bounds(); bounds.Dx() != 80 || bounds.Dy() != 296 {
+ t.Fatalf("unresolvable crop bounds = %v, want 80x296 (body + context)", bounds)
}
}
diff --git a/internal/ingestion/component/chunker/pdfcrop_cgo.go b/internal/ingestion/component/chunker/pdfcrop_cgo.go
index 7b8bbda5da..7f4a6de87e 100644
--- a/internal/ingestion/component/chunker/pdfcrop_cgo.go
+++ b/internal/ingestion/component/chunker/pdfcrop_cgo.go
@@ -208,7 +208,10 @@ func cropImageChunks(ctx context.Context, engine deepdoctype.PDFEngine, chunks [
if cropChunkType(ck) == "text" {
img = util.CropTextSectionPositions(positions, single, deepdoctype.DlaScale)
} else {
- img = util.CropSectionPositions(positions, single, deepdoctype.DlaScale)
+ // Media previews are clean position-only crops (Python
+ // cropout): no 120pt context bands, no edge dimming. The
+ // text path above is the only one that keeps context.
+ img = util.CropMediaSectionPositions(positions, single, deepdoctype.DlaScale)
}
// Release the page bitmaps now that cropping is done. They are no
// longer needed and may be evicted under the LRU cap; the image
diff --git a/internal/ingestion/component/chunker/pdfcrop_text_test.go b/internal/ingestion/component/chunker/pdfcrop_text_test.go
index 805362ba3c..8c1a8d2de8 100644
--- a/internal/ingestion/component/chunker/pdfcrop_text_test.go
+++ b/internal/ingestion/component/chunker/pdfcrop_text_test.go
@@ -88,12 +88,13 @@ func TestCropImageChunksKeepsSeparateMediaBoxes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- // Original media layout: 94px context, two 50px boxes, 170px
- // context, and four 6px gaps. Touching media boxes remain separate.
- if img.Bounds().Dy() != 388 {
- t.Fatalf("media height = %d, want 388", img.Bounds().Dy())
+ // Media layout (Python cropout): two 50px boxes and two 6px
+ // stitch gaps, no context bands. Touching media boxes remain
+ // separate; the gap between them stays gray.
+ if img.Bounds().Dy() != 112 {
+ t.Fatalf("media height = %d, want 112", img.Bounds().Dy())
}
- if got := color.RGBAModel.Convert(img.At(0, 150)); got != (color.RGBA{245, 245, 245, 255}) {
+ if got := color.RGBAModel.Convert(img.At(0, 52)); got != (color.RGBA{245, 245, 245, 255}) {
t.Fatalf("media box separator = %v, want original gap", got)
}
})
diff --git a/internal/ingestion/component/vision_enhancement_cgo_test.go b/internal/ingestion/component/vision_enhancement_cgo_test.go
index d1cc4d5cd5..8e9e729eba 100644
--- a/internal/ingestion/component/vision_enhancement_cgo_test.go
+++ b/internal/ingestion/component/vision_enhancement_cgo_test.go
@@ -12,7 +12,7 @@ import (
)
// mockVisionEngine returns a fixed image for any rendered page so the
-// on-demand cropper can exercise CropSectionPositions without a real PDF.
+// on-demand cropper can exercise section cropping without a real PDF.
type mockVisionEngine struct {
closed *bool
pageWidth float64