/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program 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 along
* with this program. If not, see .
*/
#include "zm_catch2.h"
#include "zm_config.h"
#include "zm_image.h"
#include "zm_poly.h"
#include "zm_rgb.h"
extern "C" {
#include
#include
}
#include
#include
namespace {
struct Fmt {
const char *name;
unsigned int colours;
unsigned int subpix;
AVPixelFormat pix;
};
const Fmt kFormats[] = {
{"GRAY8", ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE, AV_PIX_FMT_GRAY8},
{"RGB24", ZM_COLOUR_RGB24, ZM_SUBPIX_ORDER_RGB, AV_PIX_FMT_RGB24},
{"BGR24", ZM_COLOUR_RGB24, ZM_SUBPIX_ORDER_BGR, AV_PIX_FMT_BGR24},
{"RGBA", ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_RGBA, AV_PIX_FMT_RGBA},
{"BGRA", ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_BGRA, AV_PIX_FMT_BGRA},
{"ARGB", ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_ARGB, AV_PIX_FMT_ARGB},
{"ABGR", ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_ABGR, AV_PIX_FMT_ABGR},
{"YUV420P", ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_YUV420P, AV_PIX_FMT_YUV420P},
};
const unsigned int kDims[] = {1, 2, 3, 4, 5, 7, 15, 17, 31, 33};
void bootstrap() {
config.font_file_location = "data/fonts/04_valid.zmfnt";
}
// Plane geometry of an Image in the layout Image uses internally.
struct Planes {
uint8_t *data[4] = {};
int stride[4] = {};
unsigned int w[3] = {}; // plane width in bytes
unsigned int h[3] = {};
int n = 0;
};
Planes planes_of(Image &image, const Fmt &f, unsigned int w, unsigned int h) {
Planes p;
REQUIRE(av_image_fill_arrays(p.data, p.stride, image.Buffer(), f.pix, w, h, 32) > 0);
const AVPixFmtDescriptor *d = av_pix_fmt_desc_get(f.pix);
if (d->flags & AV_PIX_FMT_FLAG_PLANAR) {
p.n = 3;
for (int i = 0; i < 3; i++) {
p.w[i] = i ? (w + 1) >> d->log2_chroma_w : w;
p.h[i] = i ? (h + 1) >> d->log2_chroma_h : h;
}
} else {
p.n = 1;
p.w[0] = w * av_get_bits_per_pixel(d) / 8;
p.h[0] = h;
}
return p;
}
uint8_t pat(int plane, unsigned int x, unsigned int y) {
return static_cast(1 + ((plane * 61 + x * 7 + y * 13) % 250));
}
void fill_pattern(Image &image, const Fmt &f, unsigned int w, unsigned int h) {
Planes p = planes_of(image, f, w, h);
memset(image.Buffer(), 0xEE, image.Size());
for (int i = 0; i < p.n; i++)
for (unsigned int y = 0; y < p.h[i]; y++)
for (unsigned int x = 0; x < p.w[i]; x++)
p.data[i][y * p.stride[i] + x] = pat(i, x, y);
}
} // namespace
namespace {
// Packed byte at (x, y) of plane 0, counted in samples of `bpp` bytes.
uint8_t sample(Image &image, unsigned int x, unsigned int y, unsigned int b = 0) {
return image.Buffer()[y * image.LineSize() + x * (image.Colours() == 1 ? 1 : image.Colours()) + b];
}
} // namespace
TEST_CASE("Image::Deinterlace_Linear stays in bounds for a one row image", "[image][transform]") {
bootstrap();
for (const Fmt &f : kFormats) {
for (unsigned int w : {1u, 3u, 17u}) {
INFO(f.name << " " << w << "x1");
Image image(w, 1, f.colours, f.subpix);
fill_pattern(image, f, w, 1);
image.Deinterlace_Linear();
}
}
}
TEST_CASE("Image::Deinterlace_Linear keeps the last row of an odd height image", "[image][transform]") {
bootstrap();
// Rows 0, 2 and 4 are the kept field; row 4 is the last row of a 5 row image
// and has no row below it, so it must be left alone.
Image image(3, 5, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE);
for (unsigned int y = 0; y < 5; y++)
for (unsigned int x = 0; x < 3; x++)
image.Buffer()[y * image.LineSize() + x] = static_cast(y * 40);
image.Deinterlace_Linear();
for (unsigned int x = 0; x < 3; x++) {
REQUIRE(sample(image, x, 0) == 0);
REQUIRE(sample(image, x, 1) == 40);
REQUIRE(sample(image, x, 2) == 80);
REQUIRE(sample(image, x, 3) == 120);
REQUIRE(sample(image, x, 4) == 160);
}
}