feat: add image icon detection and filtering functionality
- Introduced a new function `isIconImage` to identify small images and decorative elements based on pixel dimensions and file size. - Updated the image resolution logic to filter out identified icon images from markdown content, enhancing the quality of stored images. - Added unit tests for icon image detection to ensure accurate functionality across various image sizes. These changes improve image handling by effectively filtering out non-essential images, leading to cleaner document processing.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package docparser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -12,6 +17,30 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
// minImageDimension is the minimum width/height in pixels; images smaller
|
||||
// than this on either axis are treated as icons and filtered out.
|
||||
minImageDimension = 128
|
||||
// minImageBytes is the minimum file size in bytes; very small images are
|
||||
// almost certainly icons or decorative elements.
|
||||
minImageBytes = 512 // 512 bytes
|
||||
)
|
||||
|
||||
// isIconImage returns true if the image data looks like a small icon or
|
||||
// decorative element that should be filtered out. It checks pixel dimensions
|
||||
// when decodable, and falls back to raw byte size otherwise.
|
||||
func isIconImage(data []byte) bool {
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
// Cannot decode dimensions — fall back to size-only heuristic.
|
||||
return len(data) < minImageBytes
|
||||
}
|
||||
if cfg.Width < minImageDimension || cfg.Height < minImageDimension {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StoredImage describes an image that has been saved to storage.
|
||||
type StoredImage struct {
|
||||
OriginalRef string // reference in the original markdown
|
||||
@@ -72,6 +101,13 @@ func (r *ImageResolver) ResolveAndStore(
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter out small icons and decorative images
|
||||
if isIconImage(ref.ImageData) {
|
||||
// Remove the image reference from markdown entirely
|
||||
markdown = markdown[:m[0]] + markdown[m[1]:]
|
||||
continue
|
||||
}
|
||||
|
||||
// Determine extension
|
||||
ext := extFromMime(ref.MimeType)
|
||||
if ext == "" {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package docparser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// createTestPNG generates a minimal PNG image with the given dimensions.
|
||||
func createTestPNG(w, h int) []byte {
|
||||
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
img.Set(x, y, color.RGBA{R: 128, G: 128, B: 128, A: 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
_ = png.Encode(&buf, img)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestIsIconImage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
name: "tiny bytes (< 2KB)",
|
||||
data: make([]byte, 1024),
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
name: "small icon 32x32",
|
||||
data: createTestPNG(32, 32),
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
name: "small icon 48x48",
|
||||
data: createTestPNG(48, 48),
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
name: "borderline 64x64",
|
||||
data: createTestPNG(64, 64),
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
name: "normal image 200x150",
|
||||
data: createTestPNG(200, 150),
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
name: "wide but short 200x30",
|
||||
data: createTestPNG(200, 30),
|
||||
expect: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := isIconImage(tt.data)
|
||||
if got != tt.expect {
|
||||
t.Errorf("isIconImage() = %v, want %v (data len=%d)", got, tt.expect, len(tt.data))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user