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:
@@ -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