From ade1f77fe487964c26e0a7e171413596ee9abcbf Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Thu, 19 Feb 2015 23:17:07 -0800 Subject: [PATCH] Fix bugs and get tests passing --- Base/src/Image.cpp | 11 +++++++---- Base/test/TestImage.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Base/src/Image.cpp b/Base/src/Image.cpp index bab3457..86e1db5 100644 --- a/Base/src/Image.cpp +++ b/Base/src/Image.cpp @@ -672,17 +672,20 @@ static void IDCT(Image *img) { for (unsigned int u = 0; u < img->GetWidth(); ++u) { float fu = static_cast(u); float fv = static_cast(v); - new_img(x, y) += (*img)(u, v) + + float idct = (*img)(u, v) * cos(((2*fx + 1) * fu * M_PI) / (2 * N)) * cos(((2*fy + 1) * fv * M_PI) / (2 * M)); if (u == 0 && v == 0) { - new_img(x, y) /= N; + idct /= N; } else if (u == 0 || v == 0) { - new_img(x, y) /= sqrt(2) / N; + idct *= sqrt(2) / N; } else { - new_img(x, y) *= 2 / N; + idct *= 2 / N; } + + new_img(x, y) += FasTC::IPixel(idct); } } } diff --git a/Base/test/TestImage.cpp b/Base/test/TestImage.cpp index 70512eb..419bca7 100644 --- a/Base/test/TestImage.cpp +++ b/Base/test/TestImage.cpp @@ -213,6 +213,33 @@ TEST(Image, SplitImage) { } TEST(Image, DCT) { + const uint32 w = 32; + const uint32 h = 32; + + FasTC::Image img(w, h); + for (uint32 j = 0; j < h; ++j) { + for (uint32 i = 0; i < w; ++i) { + img(i, j) = static_cast(1); + } + } + + // Make sure that taking the DCT and inverse DCT returns + // the same image... + FasTC::DiscreteCosineXForm(&img, 8); + + // First make sure they're different + for (uint32 j = 0; j < h; ++j) { + for (uint32 i = 0; i < w; ++i) { + if ( (i % 8) == 0 && (j % 8) == 0 ) { + EXPECT_NEAR(img(i, j), 8.0f, 1e-5); + } else { + EXPECT_NEAR(img(i, j), 0.0f, 1e-5); + } + } + } +} + +TEST(Image, IDCT) { const uint32 w = 32; const uint32 h = 32; @@ -221,7 +248,6 @@ TEST(Image, DCT) { for (uint32 j = 0; j < h; ++j) { for (uint32 i = 0; i < w; ++i) { img(i, j) = static_cast(i + j); -// img(i, j) = static_cast(1); } } @@ -242,7 +268,7 @@ TEST(Image, DCT) { for (uint32 j = 0; j < h; ++j) { for (uint32 i = 0; i < w; ++i) { - EXPECT_NEAR(img(i, j), orig(i, j), 1e-5); + EXPECT_NEAR(img(i, j), orig(i, j), 1e-4); } } }