|
| 1 | +/* |
| 2 | + * Solidify - texture push-pull processing utility |
| 3 | + * Copyright (c) 2023-2026 Erium Vladlen. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published |
| 7 | + * by the Free Software Foundation, either version 2.1 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "processing.h" |
| 20 | +#include "settings.h" |
| 21 | + |
| 22 | +#include <OpenImageIO/imagebuf.h> |
| 23 | +#include <OpenImageIO/imageio.h> |
| 24 | + |
| 25 | +#include <cstdint> |
| 26 | +#include <filesystem> |
| 27 | +#include <iostream> |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +namespace fs = std::filesystem; |
| 32 | + |
| 33 | +static int g_failures = 0; |
| 34 | + |
| 35 | +static void expectTrue(bool value, const char* expr, const char* file, int line) |
| 36 | +{ |
| 37 | + if (!value) { |
| 38 | + std::cerr << file << ':' << line << ": expected " << expr << '\n'; |
| 39 | + ++g_failures; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#define EXPECT_TRUE(expr) expectTrue((expr), #expr, __FILE__, __LINE__) |
| 44 | + |
| 45 | +static bool writeRgbaPng(const fs::path& path) |
| 46 | +{ |
| 47 | + constexpr int width = 8; |
| 48 | + constexpr int height = 8; |
| 49 | + OIIO::ImageSpec spec(width, height, 4, OIIO::TypeDesc::UINT8); |
| 50 | + spec.channelnames[3] = "A"; |
| 51 | + spec.alpha_channel = 3; |
| 52 | + |
| 53 | + OIIO::ImageBuf image(spec); |
| 54 | + uint8_t* pixels = static_cast<uint8_t*>(image.localpixels()); |
| 55 | + for (int y = 0; y < height; ++y) { |
| 56 | + for (int x = 0; x < width; ++x) { |
| 57 | + const bool hole = x >= 3 && x <= 4 && y >= 3 && y <= 4; |
| 58 | + const size_t base = (static_cast<size_t>(y) * width + static_cast<size_t>(x)) * 4u; |
| 59 | + const uint8_t alpha = hole ? 0u : 255u; |
| 60 | + pixels[base + 0] = hole ? 0u : 90u; |
| 61 | + pixels[base + 1] = hole ? 0u : 120u; |
| 62 | + pixels[base + 2] = hole ? 0u : 160u; |
| 63 | + pixels[base + 3] = alpha; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return image.write(path.string()); |
| 68 | +} |
| 69 | + |
| 70 | +static void configureProcessing(bool useAlpha) |
| 71 | +{ |
| 72 | + settings.reSettings(); |
| 73 | + settingsDefaults = settings; |
| 74 | + settings.isSolidify = true; |
| 75 | + settings.useAlpha = useAlpha; |
| 76 | + settings.alphaMode = 0; |
| 77 | + settings.normMode = 0; |
| 78 | + settings.repairMode = 0; |
| 79 | + settings.rangeMode = 0; |
| 80 | + settings.swapBasis = 0; |
| 81 | + settings.swapInvertMask = 0; |
| 82 | + settings.grayscaleMode = 0; |
| 83 | + settings.fileFormat = 2; |
| 84 | + settings.bitDepth = -1; |
| 85 | + settings.numThreads = 1; |
| 86 | + settings.queueLimit = 1; |
| 87 | + settings.pngStrategy = PngCompression_Default; |
| 88 | + settings.pngCompressionLevel = 4; |
| 89 | +} |
| 90 | + |
| 91 | +static void testUseAlphaProcessesTextureNamedMask() |
| 92 | +{ |
| 93 | + const fs::path testDir = fs::temp_directory_path() / "solidify_processing_tests"; |
| 94 | + std::error_code ec; |
| 95 | + fs::remove_all(testDir, ec); |
| 96 | + ec.clear(); |
| 97 | + fs::create_directories(testDir, ec); |
| 98 | + EXPECT_TRUE(!ec); |
| 99 | + |
| 100 | + const fs::path albedoPath = testDir / "cloth_albedo.png"; |
| 101 | + const fs::path maskPath = testDir / "cloth_mask.png"; |
| 102 | + EXPECT_TRUE(writeRgbaPng(albedoPath)); |
| 103 | + EXPECT_TRUE(writeRgbaPng(maskPath)); |
| 104 | + |
| 105 | + configureProcessing(true); |
| 106 | + EXPECT_TRUE(doProcessing({ testDir.string() }, nullptr)); |
| 107 | + EXPECT_TRUE(fs::exists(testDir / "cloth_albedo_fill.png")); |
| 108 | + EXPECT_TRUE(fs::exists(testDir / "cloth_mask_fill.png")); |
| 109 | + |
| 110 | + fs::remove(testDir / "cloth_albedo_fill.png", ec); |
| 111 | + ec.clear(); |
| 112 | + fs::remove(testDir / "cloth_mask_fill.png", ec); |
| 113 | + ec.clear(); |
| 114 | + |
| 115 | + configureProcessing(false); |
| 116 | + EXPECT_TRUE(doProcessing({ testDir.string() }, nullptr)); |
| 117 | + EXPECT_TRUE(fs::exists(testDir / "cloth_albedo_fill.png")); |
| 118 | + EXPECT_TRUE(!fs::exists(testDir / "cloth_mask_fill.png")); |
| 119 | + |
| 120 | + fs::remove_all(testDir, ec); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace |
| 124 | + |
| 125 | +int main() |
| 126 | +{ |
| 127 | + testUseAlphaProcessesTextureNamedMask(); |
| 128 | + |
| 129 | + if (g_failures != 0) { |
| 130 | + std::cerr << g_failures << " processing test expectation(s) failed.\n"; |
| 131 | + return 1; |
| 132 | + } |
| 133 | + |
| 134 | + std::cout << "Solidify processing tests passed.\n"; |
| 135 | + return 0; |
| 136 | +} |
0 commit comments