|
| 1 | +/* |
| 2 | + * Copyright 2025 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "ImageTools.h" |
| 28 | + |
| 29 | +#include <cmath> |
| 30 | + |
| 31 | +#include "gtest/gtest.h" |
| 32 | + |
| 33 | +using namespace Diligent; |
| 34 | + |
| 35 | +namespace |
| 36 | +{ |
| 37 | + |
| 38 | +TEST(Common_ImageTools, GetImageDifference) |
| 39 | +{ |
| 40 | + constexpr Uint32 Width = 3; |
| 41 | + constexpr Uint32 Height = 2; |
| 42 | + constexpr Uint32 Stride1 = 11; |
| 43 | + constexpr Uint32 Stride2 = 12; |
| 44 | + // clang-format off |
| 45 | + constexpr char Image1[Stride1 * Height] = { |
| 46 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, |
| 47 | + 9, 8, 7, 5, 6, 4, 3, 2, 1, 30, 40, |
| 48 | + }; |
| 49 | + constexpr char Image2[Stride2 * Height] = { |
| 50 | + 1, 2, 3, 5, 8, 8, 7, 8, 9, 10, 20, 30, |
| 51 | +// ^ ^ ^ |
| 52 | +// -1 -3 -2 |
| 53 | + 6, 4, 2, 5, 6, 4, 7, 6, 1, 40, 50, 60, |
| 54 | +// ^ ^ ^ ^ ^ |
| 55 | +// 3 4 5 4 4 |
| 56 | + }; |
| 57 | + // clang-format on |
| 58 | + |
| 59 | + { |
| 60 | + ImageDiffInfo Diff; |
| 61 | + GetImageDifference(Width, Height, 3, Image1, Stride1, Image1, Stride1, 3, Diff); |
| 62 | + EXPECT_EQ(Diff.NumDiffPixels, 0); |
| 63 | + EXPECT_EQ(Diff.NumDiffPixelsAboveThreshold, 0); |
| 64 | + EXPECT_EQ(Diff.MaxDiff, 0); |
| 65 | + EXPECT_EQ(Diff.AvgDiff, 0.f); |
| 66 | + EXPECT_EQ(Diff.RmsDiff, 0.f); |
| 67 | + } |
| 68 | + |
| 69 | + { |
| 70 | + ImageDiffInfo Diff; |
| 71 | + GetImageDifference(Width, Height, 3, Image1, Stride1, Image2, Stride2, 3, Diff); |
| 72 | + EXPECT_EQ(Diff.NumDiffPixels, 3); |
| 73 | + EXPECT_EQ(Diff.NumDiffPixelsAboveThreshold, 2); |
| 74 | + EXPECT_EQ(Diff.MaxDiff, 5); |
| 75 | + EXPECT_FLOAT_EQ(Diff.AvgDiff, 4.f); |
| 76 | + EXPECT_FLOAT_EQ(Diff.RmsDiff, std::sqrt((9.f + 16.f + 25.f) / 3.f)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace |
0 commit comments