Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DIP] add libpng macro #406

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 12 additions & 2 deletions frontend/Interfaces/buddy/DIP/ImgContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <cstring>
#include <fstream>
#include <memory>
#ifdef BUDDY_ENABLE_PNG
#include <png.h>
#endif

namespace dip {
enum ImageModes {
Expand Down Expand Up @@ -88,7 +90,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 +133,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 +422,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 +613,7 @@ bool Image<T, N>::decodePNG(const std::vector<uint8_t> &fileData) {
}
return true;
}
#endif

} // namespace dip

Expand Down
3 changes: 2 additions & 1 deletion tests/Interface/core/NewImageContainerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ int main() {
// CHECK: 0.45490
fprintf(stderr, "%f\n", bmp24bitRGBNorm.getData()[0]);

#ifdef BUDDY_ENABLE_PNG
// Default Gray Scale
dip::Image<float, 4> pngGrayDefault(
"../../../../tests/Interface/core/TestGrayImage.png", dip::DIP_GRAYSCALE);
Expand Down Expand Up @@ -221,6 +222,6 @@ int main() {
fprintf(stderr, "%d\n", pngRGBNorm.getBitDepth());
// CHECK: 0.5647
fprintf(stderr, "%f\n", pngRGBNorm.getData()[0]);

#endif
return 0;
}