Skip to content

Commit

Permalink
[Container] Add macro to control libpng.
Browse files Browse the repository at this point in the history
Co-authored-by: zhanghb97 <[email protected]>
  • Loading branch information
Hanyonggong and zhanghb97 committed Oct 16, 2024
1 parent aef92be commit ddc1a48
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 66 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand Down
15 changes: 13 additions & 2 deletions frontend/Interfaces/buddy/DIP/ImgContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
#include <cstring>
#include <fstream>
#include <memory>
#include <array>
#ifdef BUDDY_ENABLE_PNG
#include <png.h>
#endif

namespace dip {
enum ImageModes {
Expand Down Expand Up @@ -88,7 +91,9 @@ template <typename T, size_t N> class Image : public MemRef<T, N> {
// Decodes a BMP image from raw file data.
bool decodeBMP(const std::vector<uint8_t> &fileData);
// Decodes a PNG image from raw file data.
#ifdef BUDDY_ENABLE_PNG
bool decodePNG(const std::vector<uint8_t> &fileData);
#endif
};

// Image Container Constructor
Expand Down Expand Up @@ -129,13 +134,17 @@ Image<T, N>::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.");
}
}
Expand Down Expand Up @@ -414,6 +423,7 @@ bool Image<T, N>::decodeBMP(const std::vector<uint8_t> &fileData) {
}

// PNG Image File Decoder
#ifdef BUDDY_ENABLE_PNG
template <typename T, std::size_t N>
bool Image<T, N>::decodePNG(const std::vector<uint8_t> &fileData) {
// Check if the provided data is large enough to contain a minimal PNG header
Expand Down Expand Up @@ -604,6 +614,7 @@ bool Image<T, N>::decodePNG(const std::vector<uint8_t> &fileData) {
}
return true;
}
#endif

} // namespace dip

Expand Down
5 changes: 4 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down
16 changes: 12 additions & 4 deletions tests/Interface/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 <buddy/DIP/ImgContainer.h>

Expand Down Expand Up @@ -167,60 +167,5 @@ int main() {
// CHECK: 0.45490
fprintf(stderr, "%f\n", bmp24bitRGBNorm.getData()[0]);

// Default Gray Scale
dip::Image<float, 4> 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<float, 4> 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<float, 4> 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<float, 4> 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;
}
82 changes: 82 additions & 0 deletions tests/Interface/core/NewImageContainerTestPng.cpp
Original file line number Diff line number Diff line change
@@ -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 <buddy/DIP/ImgContainer.h>

int main() {
// Default Gray Scale
dip::Image<float, 4> 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<float, 4> 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<float, 4> 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<float, 4> 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;
}
6 changes: 5 additions & 1 deletion tests/Interface/core/lit.local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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')
config.excludes.add('NewImageContainerTestBmp.cpp')
config.excludes.add('NewImageContainerTestPng.cpp')

if config.buddy_enable_png != 'ON':
config.excludes.add('NewImageContainerTestPng.cpp')
4 changes: 3 additions & 1 deletion tests/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@"
Expand Down

0 comments on commit ddc1a48

Please sign in to comment.