From ddc1a48beb073340b9aebfe012b32a437557cdf3 Mon Sep 17 00:00:00 2001 From: Hanyonggong <1229369094@qq.com> Date: Tue, 15 Oct 2024 11:05:53 +0800 Subject: [PATCH] [Container] Add macro to control libpng. Co-authored-by: zhanghb97 --- CMakeLists.txt | 5 ++ README.md | 14 ++++ frontend/Interfaces/buddy/DIP/ImgContainer.h | 15 +++- tests/CMakeLists.txt | 5 +- tests/Interface/core/CMakeLists.txt | 16 +++- ...rTest.cpp => NewImageContainerTestBmp.cpp} | 59 +------------ .../core/NewImageContainerTestPng.cpp | 82 +++++++++++++++++++ tests/Interface/core/lit.local.cfg | 6 +- tests/lit.cfg.py | 4 +- tests/lit.site.cfg.py.in | 1 + 10 files changed, 141 insertions(+), 66 deletions(-) rename tests/Interface/core/{NewImageContainerTest.cpp => NewImageContainerTestBmp.cpp} (75%) create mode 100644 tests/Interface/core/NewImageContainerTestPng.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 796f5e344f..cd2379468a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,11 @@ if(BUDDY_MLIR_ENABLE_DIP_LIB) find_package(PNG REQUIRED) endif() +if(BUDDY_ENABLE_PNG) + add_definitions(-DBUDDY_ENABLE_PNG) + find_package(PNG REQUIRED) +endif() + # Generate libraries into `lib` of build directory. set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) diff --git a/README.md b/README.md index d413513ca5..2e44658b02 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,20 @@ $ export LLVM_MLIR_BUILD_DIR=$PWD/../llvm/build $ export PYTHONPATH=${LLVM_MLIR_BUILD_DIR}/tools/mlir/python_packages/mlir_core:${BUDDY_MLIR_BUILD_DIR}/python_packages:${PYTHONPATH} ``` +To configure the build environment for using image processing libraries, follow these steps: + +``` +$ cmake -G Ninja .. \ + -DMLIR_DIR=$PWD/../llvm/build/lib/cmake/mlir \ + -DLLVM_DIR=$PWD/../llvm/build/lib/cmake/llvm \ + -DLLVM_ENABLE_ASSERTIONS=ON \ + -DCMAKE_BUILD_TYPE=RELEASE \ + -DBUDDY_MLIR_ENABLE_DIP_LIB=ON \ + -DBUDDY_ENABLE_PNG=ON +$ ninja +$ ninja check-buddy +``` + To build buddy-mlir with custom LLVM sources: ``` diff --git a/frontend/Interfaces/buddy/DIP/ImgContainer.h b/frontend/Interfaces/buddy/DIP/ImgContainer.h index 382974e967..2525641bff 100644 --- a/frontend/Interfaces/buddy/DIP/ImgContainer.h +++ b/frontend/Interfaces/buddy/DIP/ImgContainer.h @@ -25,7 +25,10 @@ #include #include #include +#include +#ifdef BUDDY_ENABLE_PNG #include +#endif namespace dip { enum ImageModes { @@ -88,7 +91,9 @@ template class Image : public MemRef { // Decodes a BMP image from raw file data. bool decodeBMP(const std::vector &fileData); // Decodes a PNG image from raw file data. +#ifdef BUDDY_ENABLE_PNG bool decodePNG(const std::vector &fileData); +#endif }; // Image Container Constructor @@ -129,13 +134,17 @@ Image::Image(std::string filePath, ImageModes mode, bool norm) this->imageFormat = ImageFormat::ERROR; throw std::runtime_error("Failed to decode BMP file from " + filePath); }; - } else if (this->imageFormat == ImageFormat::PNG) { + } +#ifdef BUDDY_ENABLE_PNG + else if (this->imageFormat == ImageFormat::PNG) { bool success = decodePNG(fileData); if (!success) { this->imageFormat = ImageFormat::ERROR; throw std::runtime_error("Failed to decode PNG file from " + filePath); }; - } else { + } +#endif + else { throw std::runtime_error("Unsupported image file format."); } } @@ -414,6 +423,7 @@ bool Image::decodeBMP(const std::vector &fileData) { } // PNG Image File Decoder +#ifdef BUDDY_ENABLE_PNG template bool Image::decodePNG(const std::vector &fileData) { // Check if the provided data is large enough to contain a minimal PNG header @@ -604,6 +614,7 @@ bool Image::decodePNG(const std::vector &fileData) { } return true; } +#endif } // namespace dip diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bd4b3ef335..2cffa98469 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,7 +22,10 @@ if(BUDDY_ENABLE_OPENCV) endif() if(BUDDY_MLIR_ENABLE_DIP_LIB) - list(APPEND BUDDY_TEST_DEPENDS buddy-new-image-container-test) + list(APPEND BUDDY_TEST_DEPENDS buddy-new-image-container-test-bmp) + if(BUDDY_ENABLE_PNG) + list(APPEND BUDDY_TEST_DEPENDS buddy-new-image-container-test-png) + endif() endif() add_lit_testsuite(check-tests "Running the buddy regression tests..." diff --git a/tests/Interface/core/CMakeLists.txt b/tests/Interface/core/CMakeLists.txt index dd7a191e7f..b84ae71aef 100644 --- a/tests/Interface/core/CMakeLists.txt +++ b/tests/Interface/core/CMakeLists.txt @@ -17,10 +17,18 @@ if(BUDDY_MLIR_ENABLE_DIP_LIB OR BUDDY_ENABLE_OPENCV) ) endif() -if (BUDDY_MLIR_ENABLE_DIP_LIB) - set(NEW_DIP_LIBS ${PNG_LIBRARIES}) - _add_test_executable(buddy-new-image-container-test - NewImageContainerTest.cpp +if(BUDDY_MLIR_ENABLE_DIP_LIB) + set(NEW_DIP_LIBS "") + if(BUDDY_ENABLE_PNG) + list(APPEND NEW_DIP_LIBS ${PNG_LIBRARIES}) + _add_test_executable(buddy-new-image-container-test-png + NewImageContainerTestPng.cpp + LINK_LIBS + ${NEW_DIP_LIBS} + ) + endif() + _add_test_executable(buddy-new-image-container-test-bmp + NewImageContainerTestBmp.cpp LINK_LIBS ${NEW_DIP_LIBS} ) diff --git a/tests/Interface/core/NewImageContainerTest.cpp b/tests/Interface/core/NewImageContainerTestBmp.cpp similarity index 75% rename from tests/Interface/core/NewImageContainerTest.cpp rename to tests/Interface/core/NewImageContainerTestBmp.cpp index c109230790..13f1a9c7cf 100644 --- a/tests/Interface/core/NewImageContainerTest.cpp +++ b/tests/Interface/core/NewImageContainerTestBmp.cpp @@ -1,4 +1,4 @@ -//===- NewImageContainerTest.cpp ------------------------------------------===// +//===- NewImageContainerTestBmp.cpp ---------------------------------------===// // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ // //===----------------------------------------------------------------------===// -// RUN: buddy-new-image-container-test 2>&1 | FileCheck %s +// RUN: buddy-new-image-container-test-bmp 2>&1 | FileCheck %s #include @@ -167,60 +167,5 @@ int main() { // CHECK: 0.45490 fprintf(stderr, "%f\n", bmp24bitRGBNorm.getData()[0]); - // Default Gray Scale - dip::Image pngGrayDefault( - "../../../../tests/Interface/core/TestGrayImage.png", dip::DIP_GRAYSCALE); - // CHECK: PNG - fprintf(stderr, "%s\n", pngGrayDefault.getFormatName().c_str()); - // CHECK: 4 - fprintf(stderr, "%ld\n", pngGrayDefault.getWidth()); - // CHECK: 4 - fprintf(stderr, "%ld\n", pngGrayDefault.getHeight()); - // CHECK: 8 - fprintf(stderr, "%d\n", pngGrayDefault.getBitDepth()); - // CHECK: 15 - fprintf(stderr, "%f\n", pngGrayDefault.getData()[0]); - // Gray Scale + Normalization - dip::Image pngGrayNorm( - "../../../../tests/Interface/core/TestGrayImage.png", dip::DIP_GRAYSCALE, - true /* norm */); - // CHECK: PNG - fprintf(stderr, "%s\n", pngGrayNorm.getFormatName().c_str()); - // CHECK: 4 - fprintf(stderr, "%ld\n", pngGrayNorm.getWidth()); - // CHECK: 4 - fprintf(stderr, "%ld\n", pngGrayNorm.getHeight()); - // CHECK: 8 - fprintf(stderr, "%d\n", pngGrayNorm.getBitDepth()); - // CHECK: 0.058824 - fprintf(stderr, "%f\n", pngGrayNorm.getData()[0]); - - dip::Image pngRGBDefault( - "../../../../tests/Interface/core/TestImage-RGB.png", dip::DIP_RGB); - // CHECK: PNG - fprintf(stderr, "%s\n", pngRGBDefault.getFormatName().c_str()); - // CHECK: 224 - fprintf(stderr, "%ld\n", pngRGBDefault.getWidth()); - // CHECK: 224 - fprintf(stderr, "%ld\n", pngRGBDefault.getHeight()); - // CHECK: 8 - fprintf(stderr, "%d\n", pngRGBDefault.getBitDepth()); - // CHECK: 144 - fprintf(stderr, "%f\n", pngRGBDefault.getData()[0]); - - dip::Image pngRGBNorm( - "../../../../tests/Interface/core/TestImage-RGB.png", dip::DIP_RGB, - true /* norm */); - // CHECK: PNG - fprintf(stderr, "%s\n", pngRGBNorm.getFormatName().c_str()); - // CHECK: 224 - fprintf(stderr, "%ld\n", pngRGBNorm.getWidth()); - // CHECK: 224 - fprintf(stderr, "%ld\n", pngRGBNorm.getHeight()); - // CHECK: 8 - fprintf(stderr, "%d\n", pngRGBNorm.getBitDepth()); - // CHECK: 0.5647 - fprintf(stderr, "%f\n", pngRGBNorm.getData()[0]); - return 0; } diff --git a/tests/Interface/core/NewImageContainerTestPng.cpp b/tests/Interface/core/NewImageContainerTestPng.cpp new file mode 100644 index 0000000000..0f1dea37c3 --- /dev/null +++ b/tests/Interface/core/NewImageContainerTestPng.cpp @@ -0,0 +1,82 @@ +//===- NewImageContainerTestPng.cpp ---------------------------------------===// +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// +// +// This is the image container test file. +// +//===----------------------------------------------------------------------===// + +// RUN: buddy-new-image-container-test-png 2>&1 | FileCheck %s + +#include + +int main() { + // Default Gray Scale + dip::Image pngGrayDefault( + "../../../../tests/Interface/core/TestGrayImage.png", dip::DIP_GRAYSCALE); + // CHECK: PNG + fprintf(stderr, "%s\n", pngGrayDefault.getFormatName().c_str()); + // CHECK: 4 + fprintf(stderr, "%ld\n", pngGrayDefault.getWidth()); + // CHECK: 4 + fprintf(stderr, "%ld\n", pngGrayDefault.getHeight()); + // CHECK: 8 + fprintf(stderr, "%d\n", pngGrayDefault.getBitDepth()); + // CHECK: 15 + fprintf(stderr, "%f\n", pngGrayDefault.getData()[0]); + // Gray Scale + Normalization + dip::Image pngGrayNorm( + "../../../../tests/Interface/core/TestGrayImage.png", dip::DIP_GRAYSCALE, + true /* norm */); + // CHECK: PNG + fprintf(stderr, "%s\n", pngGrayNorm.getFormatName().c_str()); + // CHECK: 4 + fprintf(stderr, "%ld\n", pngGrayNorm.getWidth()); + // CHECK: 4 + fprintf(stderr, "%ld\n", pngGrayNorm.getHeight()); + // CHECK: 8 + fprintf(stderr, "%d\n", pngGrayNorm.getBitDepth()); + // CHECK: 0.058824 + fprintf(stderr, "%f\n", pngGrayNorm.getData()[0]); + + dip::Image pngRGBDefault( + "../../../../tests/Interface/core/TestImage-RGB.png", dip::DIP_RGB); + // CHECK: PNG + fprintf(stderr, "%s\n", pngRGBDefault.getFormatName().c_str()); + // CHECK: 224 + fprintf(stderr, "%ld\n", pngRGBDefault.getWidth()); + // CHECK: 224 + fprintf(stderr, "%ld\n", pngRGBDefault.getHeight()); + // CHECK: 8 + fprintf(stderr, "%d\n", pngRGBDefault.getBitDepth()); + // CHECK: 144 + fprintf(stderr, "%f\n", pngRGBDefault.getData()[0]); + + dip::Image pngRGBNorm( + "../../../../tests/Interface/core/TestImage-RGB.png", dip::DIP_RGB, + true /* norm */); + // CHECK: PNG + fprintf(stderr, "%s\n", pngRGBNorm.getFormatName().c_str()); + // CHECK: 224 + fprintf(stderr, "%ld\n", pngRGBNorm.getWidth()); + // CHECK: 224 + fprintf(stderr, "%ld\n", pngRGBNorm.getHeight()); + // CHECK: 8 + fprintf(stderr, "%d\n", pngRGBNorm.getBitDepth()); + // CHECK: 0.5647 + fprintf(stderr, "%f\n", pngRGBNorm.getData()[0]); + + return 0; +} diff --git a/tests/Interface/core/lit.local.cfg b/tests/Interface/core/lit.local.cfg index 83f1696f7c..f5c2722550 100644 --- a/tests/Interface/core/lit.local.cfg +++ b/tests/Interface/core/lit.local.cfg @@ -2,4 +2,8 @@ if config.buddy_enable_opencv != 'ON': config.excludes.add('ImageContainerTest.cpp') if config.buddy_mlir_enable_dip_lib != 'ON': - config.excludes.add('NewImageContainerTest.cpp') \ No newline at end of file + config.excludes.add('NewImageContainerTestBmp.cpp') + config.excludes.add('NewImageContainerTestPng.cpp') + +if config.buddy_enable_png != 'ON': + config.excludes.add('NewImageContainerTestPng.cpp') diff --git a/tests/lit.cfg.py b/tests/lit.cfg.py index 0ff7379027..2982e2851f 100644 --- a/tests/lit.cfg.py +++ b/tests/lit.cfg.py @@ -108,6 +108,8 @@ tools.append("buddy-image-container-test") if config.buddy_mlir_enable_dip_lib == "ON": - tools.append("buddy-new-image-container-test") + tools.append("buddy-new-image-container-test-bmp") + if config.buddy_enable_png == "ON": + tools.append("buddy-new-image-container-test-png") llvm_config.add_tool_substitutions(tools, tool_dirs) diff --git a/tests/lit.site.cfg.py.in b/tests/lit.site.cfg.py.in index 5f2d7276b0..0011f056f7 100644 --- a/tests/lit.site.cfg.py.in +++ b/tests/lit.site.cfg.py.in @@ -34,6 +34,7 @@ config.buddy_src_root = "@CMAKE_SOURCE_DIR@" config.buddy_obj_root = "@CMAKE_BINARY_DIR@" config.buddy_tools_dir = "@BUDDY_BINARY_DIR@" config.buddy_enable_opencv = "@BUDDY_ENABLE_OPENCV@" +config.buddy_enable_png = "@BUDDY_ENABLE_PNG@" config.buddy_mlir_enable_dip_lib = "@BUDDY_MLIR_ENABLE_DIP_LIB@" config.buddy_mlir_enable_python_packages = "@BUDDY_MLIR_ENABLE_PYTHON_PACKAGES@" config.buddy_python_packages_dir = "@BUDDY_MLIR_PYTHON_PACKAGES_DIR@"