diff --git a/images/lena_with_text.jpg b/images/lena_with_text.jpg new file mode 100644 index 0000000..e5a390c Binary files /dev/null and b/images/lena_with_text.jpg differ diff --git a/opencv/cxcore.go b/opencv/cxcore.go index 5c8c927..610d933 100644 --- a/opencv/cxcore.go +++ b/opencv/cxcore.go @@ -566,6 +566,48 @@ func Circle(image *IplImage, pt1 Point, radius int, color Scalar, thickness, lin ) } +const ( + CV_FONT_HERSHEY_SIMPLEX = int(C.CV_FONT_HERSHEY_SIMPLEX) + CV_FONT_HERSHEY_PLAIN = int(C.CV_FONT_HERSHEY_PLAIN) + CV_FONT_HERSHEY_DUPLEX = int(C.CV_FONT_HERSHEY_DUPLEX) + CV_FONT_HERSHEY_COMPLEX = int(C.CV_FONT_HERSHEY_COMPLEX) + CV_FONT_HERSHEY_TRIPLEX = int(C.CV_FONT_HERSHEY_TRIPLEX) + CV_FONT_HERSHEY_COMPLEX_SMALL = int(C.CV_FONT_HERSHEY_COMPLEX_SMALL) + CV_FONT_HERSHEY_SCRIPT_SIMPLEX = int(C.CV_FONT_HERSHEY_SCRIPT_SIMPLEX) + CV_FONT_HERSHEY_SCRIPT_COMPLEX = int(C.CV_FONT_HERSHEY_SCRIPT_COMPLEX) + CV_FONT_ITALIC = int(C.CV_FONT_ITALIC) +) + +type Font struct { + font C.CvFont +} + +//void cvInitFont(CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 ) +func InitFont(fontFace int, hscale, vscale, shear float32, thickness, lineType int) *Font { + font := new(Font) + C.cvInitFont( + &font.font, + C.int(fontFace), + C.double(hscale), + C.double(vscale), + C.double(shear), + C.int(thickness), + C.int(lineType), + ) + return font +} + +// void cvPutText(CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color) +func (this *Font) PutText(image *IplImage, text string, pt1 Point, color Scalar) { + C.cvPutText( + unsafe.Pointer(image), + C.CString(text), + C.cvPoint(C.int(pt1.X), C.int(pt1.Y)), + &this.font, + (C.CvScalar)(color), + ) +} + //CVAPI(void) cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, // CvScalar color, int thickness CV_DEFAULT(1), // int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) ); diff --git a/opencv/cxcore_test.go b/opencv/cxcore_test.go index 653e4e4..5daafe9 100644 --- a/opencv/cxcore_test.go +++ b/opencv/cxcore_test.go @@ -1,9 +1,75 @@ package opencv import ( + "bytes" + "io/ioutil" + "os" + "path" + "runtime" + "syscall" "testing" ) func TestLoadImage2(t *testing.T) { // t.Errorf("aaa") } + +func TestInitFont(t *testing.T) { + // Will assert at the C layer on error + InitFont(CV_FONT_HERSHEY_DUPLEX, 1, 1, 0, 1, 8) +} + +func TestPutText(t *testing.T) { + _, currentfile, _, _ := runtime.Caller(0) + filename := path.Join(path.Dir(currentfile), "../images/lena.jpg") + + image := LoadImage(filename) + if image == nil { + t.Fatal("LoadImage fail") + } + defer image.Release() + + // Write 'Hello' on the image + font := InitFont(CV_FONT_HERSHEY_DUPLEX, 1, 1, 0, 1, 8) + color := NewScalar(255, 255, 255, 0) + + pos := Point{image.Width() / 2, image.Height() / 2} + font.PutText(image, "Hello", pos, color) + + filename = path.Join(path.Dir(currentfile), "../images/lena_with_text.jpg") + + // Uncomment this code to create the test image "../images/lena_with_text.jpg" + // It is part of the repo, and what this test compares against + // + // SaveImage(filename, image, 0) + // println("Saved file", filename) + + tempfilename := path.Join(os.TempDir(), "lena_with_text.jpg") + defer syscall.Unlink(tempfilename) + + SaveImage(tempfilename, image, 0) + + // Compare actual image with expected image + same, err := BinaryCompare(filename, tempfilename) + if err != nil { + t.Fatal(err) + } + if !same { + t.Error("Actual file differs from expected file with text") + } +} + +// Compare two files, return true if exactly the same +func BinaryCompare(file1, file2 string) (bool, error) { + f1, err := ioutil.ReadFile(file1) + if err != nil { + return false, err + } + + f2, err := ioutil.ReadFile(file2) + if err != nil { + return false, err + } + + return bytes.Equal(f1, f2), nil +}