cosmopolitan/test/tool/viz/lib/bilinearscale_test.c

147 lines
6.8 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/log/log.h"
#include "libc/rand/rand.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "tool/viz/lib/bilinearscale.h"
#include "tool/viz/lib/graphic.h"
TEST(BilinearScale, testWindmill_1x1_to_2x2) {
EXPECT_BINEQ(
u"λλ"
u"λλ",
BilinearScale(1, 2, 2, tgc(tmalloc(1 * 2 * 2)), 1, 1, 1,
tgc(tunbing(u"λ")), 0, 1, 2, 2, 1, 1, .5, .5, 0, 0));
}
TEST(BilinearScale, testWindmill_4x4_to_2x2) {
EXPECT_BINEQ(u"λ "
u" λ",
BilinearScale(1, 2, 2, tgc(tmalloc(2 * 2)), 1, 4, 4,
tgc(tunbing(u"λλ  "
u"λλ  "
u"  λλ"
u"  λλ")),
0, 1, 2, 2, 4, 4, 2, 2, 0, 0));
}
TEST(BilinearScale, testWindmill_8x8_to_4x4) {
EXPECT_BINEQ(u"λλ  "
u"λλ  "
u"  λλ"
u"  λλ",
BilinearScale(1, 4, 4, tgc(tmalloc(4 * 4)), 1, 8, 8,
tgc(tunbing(u"λλλλ    "
u"λλλλ    "
u"λλλλ    "
u"λλλλ    "
u"    λλλλ"
u"    λλλλ"
u"    λλλλ"
u"    λλλλ")),
0, 1, 4, 4, 8, 8, 2, 2, 0, 0));
}
TEST(BilinearScale, testWindmill_4x4_to_8x8) {
EXPECT_BINEQ(u"λλλλ┐   "
u"λλλλ┐   "
u"λλλλ┐   "
u"λλλλ┐   "
u"┐┐┐┐ƒ⁇⁇⁇"
u"    ⁇λλλ"
u"    ⁇λλλ"
u"    ⁇λλλ",
BilinearScale(1, 8, 8, tgc(tmalloc(8 * 8)), 1, 4, 4,
tgc(tunbing(u"λλ  "
u"λλ  "
u"  λλ"
u"  λλ")),
0, 1, 8, 8, 4, 4, .5, .5, 0, 0));
}
TEST(BilinearScale, testWindmill_5x5_to_8x8_withRatioIntent) {
EXPECT_BINEQ(u"λλλλλ   "
u"λλλλλ   "
u"λλλλλ   "
u"¬¬¬╞λTTT"
u"   Tλλλλ"
u"     λλλ"
u"     λλλ"
u"     λλλ",
BilinearScale(1, 8, 8, tgc(tmalloc(8 * 8)), 1, 5, 5,
tgc(tunbing(u"λλλ  "
u"λλλ  "
u"  λλλ"
u"   λλ"
u"   λλ")),
0, 1, 8, 8, 5, 5, 2 / 3., 2 / 3., 0, 0));
}
TEST(BilinearScale, testWindmill_5x5_to_8x8_withoutRatioIntent) {
EXPECT_BINEQ(u"λλλλλÅ  "
u"λλλλλÅ  "
u"λλλλλÅ  "
u"╧╧╧╪λñ//"
u"   /λλλλ"
u"   →Å└λλ"
u"     oλλ"
u"     oλλ",
BilinearScale(1, 8, 8, tgc(tmalloc(8 * 8)), 1, 5, 5,
tgc(tunbing(u"λλλ  "
u"λλλ  "
u"  λλλ"
u"   λλ"
u"   λλ")),
0, 1, 8, 8, 5, 5, 5 / 8., 5 / 8., 0, 0));
}
TEST(BilinearScale, testNyquistTorture) {
EXPECT_BINEQ(u"███ "
u"████"
u" ███"
u"███ ",
BilinearScale(1, 4, 4, tgc(tmalloc(4 * 4)), 1, 8, 8,
tgc(tunbing(u"█ █ █ "
u" █ █ █ "
u"█ █ █ █ "
u" █ █ █ █"
u" █ █ █ "
u" █ █ █ "
u"█ █ █ "
u" █ █ ")),
0, 1, 4, 4, 8, 8, 2, 2, 0, 0));
}
BENCH(BilinearScale, Bench) {
void *src, *dst;
double c, w1, h1, w2, h2;
c = 3;
w1 = 1920;
h1 = 1080;
w2 = 1136;
h2 = 136;
src = tgc(tmemalign(32, w1 * h1 * c));
dst = tgc(tmemalign(32, w2 * h2 * c));
EZBENCH2("BilinearScale", donothing,
BilinearScale(c, h2, w2, dst, c, h1, w1, src, 0, c, h2, w2, h1, w1,
h2 / h1, w2 / w1, 0, 0));
}