-
Notifications
You must be signed in to change notification settings - Fork 165
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
[feature] new img container support PNG #380
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d1fc3c1
add png suport
Hanyonggong 6f3650d
add png suport
Hanyonggong 4048268
finish png suport
Hanyonggong 24e0581
change code style
Hanyonggong bb0f616
add lenet example
Hanyonggong 5c89710
pass lit test
Hanyonggong 3d32e9f
mobilnet support
Hanyonggong 3137405
change format
Hanyonggong a5a8901
Merge branch 'main' into feature-image-png
zhanghb97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,20 @@ | |
#include <cstring> | ||
#include <fstream> | ||
#include <memory> | ||
#include <png.h> | ||
|
||
namespace dip { | ||
enum ImageModes { | ||
DIP_GRAYSCALE = 0, | ||
DIP_RGB = 1, | ||
}; | ||
|
||
inline bool isBigEndian() { | ||
int num = 1; | ||
char *ptr = (char *)# | ||
return (*ptr == 0); | ||
} | ||
|
||
template <typename T, size_t N> class Image : public MemRef<T, N> { | ||
public: | ||
// Constructor initializes the image by loading from a file. | ||
|
@@ -46,6 +53,8 @@ template <typename T, size_t N> class Image : public MemRef<T, N> { | |
switch (this->imageFormat) { | ||
case ImageFormat::BMP: | ||
return "BMP"; | ||
case ImageFormat::PNG: | ||
return "PNG"; | ||
default: | ||
return "Unsupported format"; | ||
} | ||
|
@@ -62,6 +71,7 @@ template <typename T, size_t N> class Image : public MemRef<T, N> { | |
enum class ImageFormat { | ||
ERROR, // Represents an error or unsupported format. | ||
BMP, // BMP file format. | ||
PNG, // PNG file format. | ||
} imageFormat; | ||
// Mode of the image (e.g., DIP_GRAYSCALE, DIP_RGB). | ||
ImageModes imageMode; | ||
|
@@ -77,6 +87,8 @@ template <typename T, size_t N> class Image : public MemRef<T, N> { | |
void determineFormat(const std::vector<uint8_t> &fileData); | ||
// Decodes a BMP image from raw file data. | ||
bool decodeBMP(const std::vector<uint8_t> &fileData); | ||
// Decodes a PNG image from raw file data. | ||
bool decodePNG(const std::vector<uint8_t> &fileData); | ||
}; | ||
|
||
// Image Container Constructor | ||
|
@@ -117,6 +129,12 @@ 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) { | ||
bool success = decodePNG(fileData); | ||
if (!success) { | ||
this->imageFormat = ImageFormat::ERROR; | ||
throw std::runtime_error("Failed to decode PNG file from " + filePath); | ||
}; | ||
} else { | ||
throw std::runtime_error("Unsupported image file format."); | ||
} | ||
|
@@ -125,8 +143,13 @@ Image<T, N>::Image(std::string filePath, ImageModes mode, bool norm) | |
// Determines the image format by inspecting the header of the file data. | ||
template <typename T, std::size_t N> | ||
void Image<T, N>::determineFormat(const std::vector<uint8_t> &fileData) { | ||
std::array<unsigned char, 8> pngHeader = {0x89, 0x50, 0x4E, 0x47, | ||
0x0D, 0x0A, 0x1A, 0x0A}; | ||
if (fileData.size() > 2 && fileData[0] == 'B' && fileData[1] == 'M') { | ||
this->imageFormat = ImageFormat::BMP; | ||
} else if (fileData.size() > 7 && | ||
std::memcmp(fileData.data(), pngHeader.data(), 8) == 0) { | ||
this->imageFormat = ImageFormat::PNG; | ||
} else { | ||
this->imageFormat = ImageFormat::ERROR; | ||
} | ||
|
@@ -390,6 +413,199 @@ bool Image<T, N>::decodeBMP(const std::vector<uint8_t> &fileData) { | |
return true; | ||
} | ||
|
||
// PNG Image File Decoder | ||
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 | ||
// (33 bytes). | ||
if (fileData.size() < 33) { | ||
throw std::runtime_error("Invalid PNG File: too small to contain header"); | ||
} | ||
|
||
// Extract image information from PNG header. Convert Big-Endian to | ||
// Little-Endian. | ||
this->width = (fileData[16] << 24) | (fileData[17] << 16) | | ||
(fileData[18] << 8) | fileData[19]; | ||
this->height = (fileData[20] << 24) | (fileData[21] << 16) | | ||
(fileData[22] << 8) | fileData[23]; | ||
this->bitDepth = *reinterpret_cast<const uint8_t *>(&fileData[24]); | ||
int colorType = *reinterpret_cast<const uint8_t *>(&fileData[25]); | ||
uint8_t interlace = *reinterpret_cast<const uint8_t *>(&fileData[28]); | ||
|
||
// Currently, only the NCHW format with 4 dimensions is supported. | ||
if (N == 4) { | ||
// use libpng to read png image. Initialize libpng parameters | ||
png_structp png_ptr = | ||
png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | ||
if (!png_ptr) { | ||
std::cerr << "png_ptr creation failed" << std::endl; | ||
return false; | ||
} | ||
|
||
png_infop info_ptr = png_create_info_struct(png_ptr); | ||
if (!info_ptr) { | ||
std::cerr << "png_infop creation failed" << std::endl; | ||
return false; | ||
} | ||
|
||
// Set jump point for error handling | ||
if (setjmp(png_jmpbuf(png_ptr))) { | ||
std::cerr << "error during PNG reading" << std::endl; | ||
// close PNG reading and free memory | ||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | ||
return false; | ||
} | ||
|
||
// copy filedata. Read image data from memory. | ||
std::vector<uint8_t> dataCopy = fileData; | ||
png_set_read_fn( | ||
png_ptr, &dataCopy, | ||
[](png_structp png_ptr, png_bytep data, png_size_t length) { | ||
std::vector<uint8_t> *fileData = | ||
static_cast<std::vector<uint8_t> *>(png_get_io_ptr(png_ptr)); | ||
if (fileData->size() < length) { | ||
png_error(png_ptr, "Read error from memory"); | ||
} | ||
std::copy(fileData->begin(), fileData->begin() + length, data); | ||
fileData->erase(fileData->begin(), fileData->begin() + length); | ||
}); | ||
|
||
png_read_info(png_ptr, info_ptr); | ||
|
||
// Convert big or little Endian and convert 16 bits to 8 bits | ||
if (this->bitDepth == 16) | ||
png_set_strip_16(png_ptr); | ||
else if (!isBigEndian()) | ||
png_set_swap(png_ptr); | ||
|
||
// Remove alpha channel | ||
if (colorType & PNG_COLOR_MASK_ALPHA) | ||
png_set_strip_alpha(png_ptr); | ||
|
||
// Convert palette to rgb | ||
if (colorType == PNG_COLOR_TYPE_PALETTE) | ||
png_set_palette_to_rgb(png_ptr); | ||
|
||
// Convert low depth grayscale to 8-bit grayscale | ||
if ((colorType & PNG_COLOR_MASK_COLOR) == 0 && this->bitDepth < 8) | ||
#if (PNG_LIBPNG_VER_MAJOR * 10000 + PNG_LIBPNG_VER_MINOR * 100 + \ | ||
PNG_LIBPNG_VER_RELEASE >= \ | ||
10209) || \ | ||
(PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR == 0 && \ | ||
PNG_LIBPNG_VER_RELEASE >= 18) | ||
png_set_expand_gray_1_2_4_to_8(png_ptr); | ||
#else | ||
png_set_gray_1_2_4_to_8(png_ptr); | ||
#endif | ||
|
||
// Processing interleaved PNG images | ||
if (interlace) | ||
png_set_interlace_handling(png_ptr); | ||
|
||
if (this->imageMode == ImageModes::DIP_GRAYSCALE) { | ||
// TODO: Add batch setting. | ||
this->sizes[0] = 1; | ||
this->sizes[1] = 1; | ||
this->sizes[2] = this->height; | ||
this->sizes[3] = this->width; | ||
this->setStrides(); | ||
size_t size = this->product(this->sizes); | ||
this->allocated = (T *)malloc(sizeof(T) * size); | ||
this->aligned = this->allocated; | ||
|
||
// RGB->Gray | ||
if ((colorType & PNG_COLOR_MASK_COLOR) || | ||
(colorType == PNG_COLOR_TYPE_PALETTE)) | ||
png_set_rgb_to_gray(png_ptr, 1, 0.299, 0.587); | ||
|
||
// Update reading setting | ||
png_read_update_info(png_ptr, info_ptr); | ||
|
||
// Allocate memory for libpng to read images | ||
std::vector<uint8_t> imgData(this->height * this->width); | ||
std::vector<uint8_t *> row_pointers(this->height); | ||
for (size_t y = 0; y < this->height; ++y) { | ||
row_pointers[y] = imgData.data() + y * this->width; | ||
} | ||
|
||
// Reading image | ||
png_read_image(png_ptr, row_pointers.data()); | ||
|
||
// Fullfill data to memref container. | ||
for (size_t i = 0; i < this->height; i++) | ||
for (size_t j = 0; j < this->width; j++) { | ||
size_t memrefIndex = i * this->width + j; | ||
this->aligned[memrefIndex] = | ||
this->isNorm ? static_cast<T>(imgData[memrefIndex]) / 255 | ||
: static_cast<T>(imgData[memrefIndex]); | ||
; | ||
} | ||
} else if (this->imageMode == ImageModes::DIP_RGB) { | ||
// TODO: Add batch setting. | ||
this->sizes[0] = 1; | ||
this->sizes[1] = 3; | ||
this->sizes[2] = this->height; | ||
this->sizes[3] = this->width; | ||
this->setStrides(); | ||
size_t size = this->product(this->sizes); | ||
std::cout << "size" << size << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cout 删除 |
||
this->allocated = (T *)malloc(sizeof(T) * size); | ||
this->aligned = this->allocated; | ||
size_t colorStride = this->height * this->width; | ||
|
||
// Gray->RGB | ||
if (colorType & PNG_COLOR_TYPE_GRAY) | ||
png_set_gray_to_rgb(png_ptr); | ||
|
||
// Update reading setting | ||
png_read_update_info(png_ptr, info_ptr); | ||
|
||
// Allocate memory for libpng to read images | ||
std::vector<uint8_t> imgData(this->height * this->width * 3); | ||
std::vector<uint8_t *> row_pointers(this->height); | ||
for (size_t y = 0; y < this->height; ++y) { | ||
row_pointers[y] = imgData.data() + y * this->width * 3; | ||
} | ||
|
||
// Reading image | ||
png_read_image(png_ptr, row_pointers.data()); | ||
|
||
// Separate pixel data by channel | ||
size_t memrefIndex = 0; | ||
for (size_t i = 0; i < this->height; i++) | ||
for (size_t j = 0; j < this->width; j++) { | ||
// Locate the current pixel. | ||
size_t pixelIndex = ((i * width) + j) * 3; | ||
// Extract the red, green, and blue value from the current pixel. | ||
int redPixel = | ||
*reinterpret_cast<const uint8_t *>(&imgData[pixelIndex]); | ||
int greenPixel = | ||
*reinterpret_cast<const uint8_t *>(&imgData[pixelIndex + 1]); | ||
int bluePixel = | ||
*reinterpret_cast<const uint8_t *>(&imgData[pixelIndex + 2]); | ||
// Store the values into memref container as RGB order. | ||
this->aligned[memrefIndex] = this->isNorm | ||
? static_cast<T>(redPixel) / 255 | ||
: static_cast<T>(redPixel); | ||
this->aligned[memrefIndex + colorStride] = | ||
this->isNorm ? static_cast<T>(greenPixel) / 255 | ||
: static_cast<T>(greenPixel); | ||
this->aligned[memrefIndex + 2 * colorStride] = | ||
this->isNorm ? static_cast<T>(bluePixel) / 255 | ||
: static_cast<T>(bluePixel); | ||
memrefIndex++; | ||
} | ||
} | ||
|
||
// close PNG reading and free memory | ||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | ||
} else { | ||
std::cerr << "Unsupported: " << N << " dimension layout." << std::endl; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace dip | ||
|
||
#endif // FRONTEND_INTERFACES_BUDDY_DIP_IMGCONTAINER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenCV_LIBS can remove