av/filter/debug.go

97 lines
2.3 KiB
Go

// +build debug
/*
DESCRIPTION
Displays debug information for the motion filters.
AUTHORS
Scott Barnard <scott@ausocean.org>
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 (d *debugFile) newDebugFile() error {
const debugfile = "motion.mjpeg"
d.bwImg = image.NewRGBA(image.Rect(0, 0, 0, 0))
var err error
d.file, err = os.Create(debugfile)
return err
}
// closeDebugFile closes files used for debugging purposes.
func (d *debugFile) closeDebugFile() error {
err := d.file.Close()
if err != nil {
return fmt.Errorf("file cannot be closed: %w", err)
}
return nil
}
// drawToDebugFile draws debugging information to a frame.
func (d *debugFile) drawToDebugFile(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)
}
// resetDebugFileFrame makes an image of given dimensions.
func (d *debugFile) resetDebugFileFrame(w, h int) {
d.bwImg = image.NewRGBA(image.Rect(0, 0, w, h))
}