Files
cthulhu/internal/ocr/preprocess_test.go

48 lines
1.2 KiB
Go

package ocr
import (
"context"
"image"
"image/color"
"testing"
)
func TestPreprocessScaleThresholdAndLimit(t *testing.T) {
source := image.NewRGBA(image.Rect(4, 5, 6, 6))
source.SetRGBA(4, 5, color.RGBA{R: 20, G: 20, B: 20, A: 255})
source.SetRGBA(5, 5, color.RGBA{R: 220, G: 220, B: 220, A: 255})
processed, err := Preprocess(context.Background(), source, Options{Scale: 2, BlackWhite: true, Threshold: 200})
if err != nil {
t.Fatal(err)
}
if processed.Bounds() != image.Rect(0, 0, 4, 2) {
t.Fatal(processed.Bounds())
}
for x := 0; x < 4; x++ {
r, _, _, _ := processed.At(x, 0).RGBA()
want := uint32(0)
if x >= 2 {
want = 0xffff
}
if r != want {
t.Fatalf("pixel %d = %d", x, r)
}
}
if _, err := Preprocess(context.Background(), source, Options{Scale: 5}); err != ErrLimit {
t.Fatal(err)
}
}
func TestPreprocessInversion(t *testing.T) {
source := image.NewRGBA(image.Rect(0, 0, 1, 1))
source.SetRGBA(0, 0, color.RGBA{R: 10, G: 20, B: 30, A: 255})
processed, err := Preprocess(context.Background(), source, Options{Invert: true})
if err != nil {
t.Fatal(err)
}
r, g, b, _ := processed.At(0, 0).RGBA()
if r != 245*257 || g != 235*257 || b != 225*257 {
t.Fatalf("inverted pixel %d %d %d", r, g, b)
}
}