2020-02-03 06:58:06 +03:00
|
|
|
// +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 (
|
2020-02-04 03:44:09 +03:00
|
|
|
"fmt"
|
2020-02-03 06:58:06 +03:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2020-02-04 03:44:09 +03:00
|
|
|
"image/jpeg"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-02-03 06:58:06 +03:00
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/font/basicfont"
|
|
|
|
"golang.org/x/image/math/fixed"
|
2020-02-03 06:58:06 +03:00
|
|
|
)
|
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
// debugWindows is used for sending debug information to a file.
|
|
|
|
type debugFile struct {
|
|
|
|
bwImg *image.RGBA
|
|
|
|
file io.WriteCloser
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
// newDebugFile creates a file for saving debugging frames to.
|
2020-02-05 09:06:09 +03:00
|
|
|
func newDebugFile() debugFile {
|
2020-02-04 03:44:09 +03:00
|
|
|
const debugfile = "motion.mjpeg"
|
2020-02-05 09:06:09 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2020-02-04 03:44:09 +03:00
|
|
|
}
|
|
|
|
|
2020-02-05 09:06:09 +03:00
|
|
|
// close closes files used for debugging purposes.
|
|
|
|
func (d *debugFile) close() error {
|
2020-02-04 03:44:09 +03:00
|
|
|
err := d.file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("file cannot be closed: %w", err)
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
2020-02-04 03:44:09 +03:00
|
|
|
return nil
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-05 09:06:09 +03:00
|
|
|
// draw draws debugging information to a frame.
|
|
|
|
func (d *debugFile) draw(i, j int, val uint8) {
|
2020-02-04 03:44:09 +03:00
|
|
|
d.bwImg.SetRGBA(i, j, color.RGBA{val, val, val, 0xff})
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
// 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.
|
2020-02-03 06:58:06 +03:00
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
var s string
|
|
|
|
if motion > pix {
|
|
|
|
s = strconv.Itoa(motion) + " Motion"
|
|
|
|
} else {
|
|
|
|
s = strconv.Itoa(motion)
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:44:09 +03:00
|
|
|
(&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)
|
|
|
|
}
|
|
|
|
|
2020-02-05 09:06:09 +03:00
|
|
|
// resetFrame makes an image of given dimensions.
|
|
|
|
func (d *debugFile) resetFrame(w, h int) {
|
2020-02-04 03:44:09 +03:00
|
|
|
d.bwImg = image.NewRGBA(image.Rect(0, 0, w, h))
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|