// +build debug /* DESCRIPTION Displays debug information for the motion filters. AUTHORS Scott Barnard LICENSE This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License in gpl.txt. If not, see http://www.gnu.org/licenses. */ package filter import ( "fmt" "image" "image/color" "image/jpeg" "io" "os" "strconv" "golang.org/x/image/font" "golang.org/x/image/font/basicfont" "golang.org/x/image/math/fixed" ) // debugWindows is used for sending debug information to a file. type debugFile struct { bwImg *image.RGBA file io.WriteCloser } // newDebugFile creates a file for saving debugging frames to. func newDebugFile() debugFile { const debugfile = "motion.mjpeg" file, err := os.Create(debugfile) if err != nil { panic(fmt.Sprintf("could not create debug file: %w", err)) } return debugFile{ bwImg: image.NewRGBA(image.Rect(0, 0, 0, 0)), file: file, } } // close closes files used for debugging purposes. func (d *debugFile) close() error { err := d.file.Close() if err != nil { return fmt.Errorf("file cannot be closed: %w", err) } return nil } // draw draws debugging information to a frame. func (d *debugFile) draw(i, j int, val uint8) { d.bwImg.SetRGBA(i, j, color.RGBA{val, val, val, 0xff}) } // saveFrame writes a frame showing motion to a file. func (d *debugFile) saveFrame(motion, pix int) error { col := color.RGBA{200, 100, 0, 255} // Red text. var s string if motion > pix { s = strconv.Itoa(motion) + " Motion" } else { s = strconv.Itoa(motion) } (&font.Drawer{ Dst: d.bwImg, Src: image.NewUniform(col), Face: basicfont.Face7x13, Dot: fixed.P(20, 30), }).DrawString(s) return jpeg.Encode(d.file, d.bwImg, nil) } // resetFrame makes an image of given dimensions. func (d *debugFile) resetFrame(w, h int) { d.bwImg = image.NewRGBA(image.Rect(0, 0, w, h)) }