From 279971af571453a88378eb12f41cfe1c8f919631 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate Date: Thu, 21 Sep 2023 13:12:22 +0000 Subject: [PATCH 01/10] [DIP] Add encoding and decoding for png images --- examples/DIPDialect/CMakeLists.txt | 8 ++-- .../Interfaces/buddy/DIP/imgcodecs/loadsave.h | 44 ++++++++++++++++++- .../buddy/DIP/imgcodecs/replenishment.h | 13 ++++++ tests/Interface/core/CMakeLists.txt | 3 ++ tests/Interface/core/ImageContainerTest.cpp | 2 +- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/examples/DIPDialect/CMakeLists.txt b/examples/DIPDialect/CMakeLists.txt index ea73e14a5a..4afd40d606 100644 --- a/examples/DIPDialect/CMakeLists.txt +++ b/examples/DIPDialect/CMakeLists.txt @@ -1,8 +1,10 @@ if(BUDDY_ENABLE_OPENCV) find_package(OpenCV REQUIRED CONFIG) find_package(JPEG REQUIRED) + find_package(PNG REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) - include_directories(${OpenCV_INCLUDE_DIRS}) + include_directories(${JPEG_INCLUDE_DIRS}) + include_directories(${PNG_INCLUDE_DIRS}) endif() add_executable(correlation2D correlation2D.cpp) @@ -12,10 +14,10 @@ add_executable(correlationFFT2D correlationFFT2D.cpp) target_link_libraries(correlationFFT2D ${OpenCV_LIBS} BuddyLibDIP) add_executable(rotation2D rotation2D.cpp) -target_link_libraries(rotation2D ${OpenCV_LIBS} ${JPEG_LIBRARY} BuddyLibDIP) +target_link_libraries(rotation2D ${OpenCV_LIBS} ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) add_executable(resize2D resize2D.cpp) -target_link_libraries(resize2D ${OpenCV_LIBS} ${JPEG_LIBRARY} BuddyLibDIP) +target_link_libraries(resize2D ${OpenCV_LIBS} ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) add_executable(morph2D morph2D.cpp) target_link_libraries(morph2D ${OpenCV_LIBS} BuddyLibDIP) diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h b/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h index 8f8fcf9719..1b39d35777 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h @@ -52,6 +52,7 @@ #include "buddy/DIP/imgcodecs/grfmt_bmp.h" #include "buddy/DIP/imgcodecs/grfmt_jpeg.h" +#include "buddy/DIP/imgcodecs/grfmt_png.h" #include "buddy/DIP/imgcodecs/replenishment.h" namespace dip { @@ -67,6 +68,10 @@ template struct ImageCodecInitializer { // JPEG Support decoders.push_back(std::make_unique>()); encoders.push_back(std::make_unique>()); + + // PNG Support + decoders.push_back(std::make_unique>()); + encoders.push_back(std::make_unique>()); } std::vector>> decoders; std::vector>> encoders; @@ -123,7 +128,6 @@ findDecoder(const String &filename) { /// compare signature against all decoders for (i = 0; i < codecs.decoders.size(); i++) { if (codecs.decoders[i]->checkSignature(signature)) - return codecs.decoders[i]->newDecoder(); } /// If no decoder was found, return base type @@ -165,7 +169,6 @@ Img imread(const String &filename, int flags) { bmpDecoderPtr->readData(Image); return Image; } - // Converts a pointer to JpegDecoder JpegDecoder *JpegDecoderPtr = dynamic_cast *>(decoder.get()); @@ -191,7 +194,33 @@ Img imread(const String &filename, int flags) { Img Image(sizes); JpegDecoderPtr->readData(Image); return Image; + } + // Converts a pointer to BmpDecoder + PngDecoder *PngDecoderPtr = + dynamic_cast *>(decoder.get()); + if (PngDecoderPtr) { + // After creating the BmpDecoder instance, perform related + // operations Defines whether the image is scaled or not + int scale_denom = 1; + PngDecoderPtr->setScale(scale_denom); + // Set image path + PngDecoderPtr->setSource(filename); + // Read image head + PngDecoderPtr->readHeader(); + int channels = PngDecoderPtr->channels(); + if ((flags & IMGRD_COLOR) != 0 || + ((flags & IMGRD_ANYCOLOR) != 0 && channels > 1)) { + channels = 3; + } else { + channels = 1; + } + // Create an Img instance + intptr_t sizes[3] = {PngDecoderPtr->height(), PngDecoderPtr->width(), + channels}; + Img Image(sizes); + PngDecoderPtr->readData(Image); + return Image; } } } @@ -263,6 +292,17 @@ static bool imwrite(const String &filename, Img &img_vec) { code = jpegEncoderPtr->write(img_vec, params); return code; } + + // Convert to a pointer of PngEncoder + PngEncoder *pngEncoderPtr = + dynamic_cast *>(encoder.get()); + if (pngEncoderPtr) { + pngEncoderPtr->setDestination(filename); + bool code = false; + std::vector params; + code = pngEncoderPtr->write(img_vec, params); + return code; + } } return true; diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h b/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h index c396050398..5eb35fecdb 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h @@ -92,6 +92,19 @@ enum ImwriteFlags { IMWRITE_JPEG_LUMA_QUALITY = 5, // Separate chroma quality level, 0 - 100, default is 0 - don't use. IMWRITE_JPEG_CHROMA_QUALITY = 6, + // Use this value for normal data. + IMWRITE_PNG_STRATEGY_DEFAULT = 0, + // Use this value for data produced by a filter (or predictor).Filtered data + // consists mostly of small values with a somewhat random //distribution. In + // this case, the compression algorithm is tuned to compress them better. + IMWRITE_PNG_STRATEGY_FILTERED = 1, + // Use this value to force Huffman encoding only (no string match). + IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2, + // Use this value to limit match distances to one (run-length encoding). + IMWRITE_PNG_STRATEGY_RLE = 3, + // Using this value prevents the use of dynamic Huffman codes, allowing for a + // simpler decoder for special applications. + IMWRITE_PNG_STRATEGY_FIXED = 4, }; class _Size { diff --git a/tests/Interface/core/CMakeLists.txt b/tests/Interface/core/CMakeLists.txt index bc9b2f0cea..6ad3594310 100644 --- a/tests/Interface/core/CMakeLists.txt +++ b/tests/Interface/core/CMakeLists.txt @@ -5,13 +5,16 @@ _add_test_executable(buddy-container-test if(BUDDY_ENABLE_OPENCV) find_package(OpenCV REQUIRED CONFIG) find_package(JPEG REQUIRED) +find_package(PNG REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(${JPEG_INCLUDE_DIRS}) +include_directories(${PNG_INCLUDE_DIRS}) _add_test_executable(buddy-image-container-test ImageContainerTest.cpp LINK_LIBS ${OpenCV_LIBS} ${JPEG_LIBRARY} + ${PNG_LIBRARY} ) endif() diff --git a/tests/Interface/core/ImageContainerTest.cpp b/tests/Interface/core/ImageContainerTest.cpp index 930a55072e..3dd804ce74 100644 --- a/tests/Interface/core/ImageContainerTest.cpp +++ b/tests/Interface/core/ImageContainerTest.cpp @@ -34,7 +34,7 @@ int main() { // The test running directory is in /tests/Interface/core, so the // `imread` function uses the following relative path. Img grayimage = dip::imread( - "../../../../tests/Interface/core/TestGrayImage.bmp", + "../../../../tests/Interface/core/TestGrayImage.png", dip::IMGRD_GRAYSCALE); //===--------------------------------------------------------------------===// // Test image constructor for OpenCV. From 137b201fa10385cdc8675d39dbc26c5b77798b6e Mon Sep 17 00:00:00 2001 From: Guan-schoolmate Date: Thu, 21 Sep 2023 13:18:31 +0000 Subject: [PATCH 02/10] [DIP] add Encoding and decoding of png images --- .../buddy/DIP/imgcodecs/grfmt_png.h | 434 ++++++++++++++++++ 1 file changed, 434 insertions(+) create mode 100644 frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h new file mode 100644 index 0000000000..666d09aac1 --- /dev/null +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h @@ -0,0 +1,434 @@ +//===- Grfmt_png.hpp ---------------------------------------------------===// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this +// license. If you do not agree to this license, do not download, install, copy +// or use the software. +// +// +// Intel License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright +// notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote +// products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" +// and any express or implied warranties, including, but not limited to, the +// implied warranties of merchantability and fitness for a particular purpose +// are disclaimed. In no event shall the Intel Corporation or contributors be +// liable for any direct, indirect, incidental, special, exemplary, or +// consequential damages (including, but not limited to, procurement of +// substitute goods or services; loss of use, data, or profits; or business +// interruption) however caused and on any theory of liability, whether in +// contract, strict liability, or tort (including negligence or otherwise) +// arising in any way out of the use of this software, even if advised of the +// possibility of such damage. +// +// +//===----------------------------------------------------------------------===// +// +// This file is modified from opencv's modules/imgcodecs/src/grfmt_png.hpp file +// +//===----------------------------------------------------------------------===// + +#ifndef _GRFMT_PNG_H_ +#define _GRFMT_PNG_H_ + +#ifndef _LFS64_LARGEFILE +# define _LFS64_LARGEFILE 0 +#endif +#ifndef _FILE_OFFSET_BITS +# define _FILE_OFFSET_BITS 0 +#endif + +#include +#include + +#include "buddy/DIP/imgcodecs/grfmt_base.h" +#include "buddy/DIP/imgcodecs/bitstrm.h" + +#if defined _MSC_VER && _MSC_VER >= 1200 + // interaction between '_setjmp' and C++ object destruction is non-portable + #pragma warning( disable: 4611 ) +#endif + +// the following defines are a hack to avoid multiple problems with frame pointer handling and setjmp +// see http://gcc.gnu.org/ml/gcc/2011-10/msg00324.html for some details +#define mingw_getsp(...) 0 +#define __builtin_frame_address(...) 0 +namespace dip +{ +template +class PngDecoder : public BaseImageDecoder +{ +public: + + PngDecoder(); + virtual ~PngDecoder(); + bool readData( Img& img ) ; + bool readHeader() ; + void close(); + std::unique_ptr> newDecoder() const ; +protected: + + int m_bit_depth; + void* m_png_ptr; // pointer to decompression structure + void* m_info_ptr; // pointer to image information structure + void* m_end_info; // pointer to one more image information structure + FILE* m_f; + int m_color_type; + size_t m_buf_pos; +}; + +template +class PngEncoder : public BaseImageEncoder +{ +public: + PngEncoder(); + virtual ~PngEncoder(); + bool write( Img& img, const std::vector& params ) ; + std::unique_ptr> newEncoder() const; +protected: + static void writeDataToBuf(void* png_ptr, uchar* src, size_t size); +}; + +bool isBigEndian() { + int num = 1; + char* ptr = (char*)# + return (*ptr == 0); +} + + +/////////////////////// PngDecoder /////////////////// +template +PngDecoder::PngDecoder() +{ + this->m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa"; + m_color_type = 0; + m_png_ptr = 0; + m_info_ptr = m_end_info = 0; + m_f = 0; + this->m_buf_supported = true; + m_buf_pos = 0; + m_bit_depth = 0; +} + +template +PngDecoder::~PngDecoder() +{ + close(); +} + +template +std::unique_ptr> PngDecoder::newDecoder() const { + return std::make_unique>(); +} + +template +void PngDecoder::close() +{ + if( m_f ) + { + fclose( m_f ); + m_f = 0; + } + + if( m_png_ptr ) + { + png_structp png_ptr = (png_structp)m_png_ptr; + png_infop info_ptr = (png_infop)m_info_ptr; + png_infop end_info = (png_infop)m_end_info; + png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); + m_png_ptr = m_info_ptr = m_end_info = 0; + } +} + + +template +bool PngDecoder::readHeader() +{ + volatile bool result = false; + close(); + png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 ); + if( png_ptr ) + { + png_infop info_ptr = png_create_info_struct( png_ptr ); + png_infop end_info = png_create_info_struct( png_ptr ); + m_png_ptr = png_ptr; + m_info_ptr = info_ptr; + m_end_info = end_info; + m_buf_pos = 0; + if( info_ptr && end_info ) + { + if( setjmp( png_jmpbuf( png_ptr ) ) == 0 ) + { + m_f = fopen( this->m_filename.c_str(), "rb" ); + if( m_f ) + png_init_io( png_ptr, m_f ); + if(m_f) + { + png_uint_32 wdth, hght; + int bit_depth, color_type, num_trans=0; + png_bytep trans; + png_color_16p trans_values; + png_read_info( png_ptr, info_ptr ); + png_get_IHDR( png_ptr, info_ptr, &wdth, &hght, + &bit_depth, &color_type, 0, 0, 0 ); + this->m_width = (int)wdth; + this->m_height = (int)hght; + m_color_type = color_type; + m_bit_depth = bit_depth; + if( bit_depth <= 8 || bit_depth == 16 ) + { + switch(color_type) + { + case PNG_COLOR_TYPE_RGB: + case PNG_COLOR_TYPE_PALETTE: + png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values); + if( num_trans > 0 ) + this->m_channels = 4; + else + this->m_channels = 3; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + case PNG_COLOR_TYPE_RGB_ALPHA: + this->m_channels = 4; + break; + default: + this->m_channels = 1; + } + result = true; + } + } + } + } + } + if( !result ) + close(); + return result; +} + +template +bool PngDecoder::readData( Img& img ) +{ + volatile bool result = false; + uchar**_buffer = new uchar*[this->m_height]; + uchar** buffer = _buffer; + bool color = img.channels() > 1; + T*data = img.getData(); + png_structp png_ptr = (png_structp)m_png_ptr; + png_infop info_ptr = (png_infop)m_info_ptr; + png_infop end_info = (png_infop)m_end_info; + if( m_png_ptr && m_info_ptr && m_end_info && this->m_width && this->m_height ) + { + if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 ) + { + int y; + + if( m_bit_depth == 16 ) + png_set_strip_16( png_ptr ); + else if( !isBigEndian() ) + png_set_swap( png_ptr ); + + if(img.channels() < 4) + { + /* observation: png_read_image() writes 400 bytes beyond + * end of data when reading a 400x118 color png + * "mpplus_sand.png". OpenCV crashes even with demo + * programs. Looking at the loaded image I'd say we get 4 + * bytes per pixel instead of 3 bytes per pixel. Test + * indicate that it is a good idea to always ask for + * stripping alpha.. 18.11.2004 Axel Walthelm + */ + png_set_strip_alpha( png_ptr ); + } else + png_set_tRNS_to_alpha( png_ptr ); + + if( m_color_type == PNG_COLOR_TYPE_PALETTE ) + png_set_palette_to_rgb( png_ptr ); + + if( (m_color_type & PNG_COLOR_MASK_COLOR) == 0 && m_bit_depth < 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 + + if( (m_color_type & PNG_COLOR_MASK_COLOR) && color ) + png_set_bgr( png_ptr ); // convert RGB to BGR + else if( color ) + png_set_gray_to_rgb( png_ptr ); // Gray->RGB + else + png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray + + png_set_interlace_handling( png_ptr ); + png_read_update_info( png_ptr, info_ptr ); + + size_t step = this->m_width * img.channels(); + uchar *myArry = new uchar[img.getSize()]; + for(int y = 0; y < this->m_height; y++ ) + buffer[y] = myArry + y*step; + + png_read_image( png_ptr, buffer ); + png_read_end( png_ptr, end_info ); + for(int i = 0 ; i < img.getSize() ; i++) + { + data[i] = (T)myArry[i]; + } + +#ifdef PNG_eXIf_SUPPORTED + png_uint_32 num_exif = 0; + png_bytep exif = 0; + + // Exif info could be in info_ptr (intro_info) or end_info per specification + if( png_get_valid(png_ptr, info_ptr, PNG_INFO_eXIf) ) + png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif); + else if( png_get_valid(png_ptr, end_info, PNG_INFO_eXIf) ) + png_get_eXIf_1(png_ptr, end_info, &num_exif, &exif); + +#endif + + result = true; + } + } + + close(); + return result; +} + + +/////////////////////// PngEncoder /////////////////// + +template +PngEncoder::PngEncoder() +{ + this->m_description = "Portable Network Graphics files (*.png)"; + this->m_buf_supported = true; +} + +template +PngEncoder::~PngEncoder() +{ +} + +template +std::unique_ptr> PngEncoder::newEncoder() const { + return std::make_unique>(); +} + +template +void PngEncoder::writeDataToBuf(void* _png_ptr, uchar* src, size_t size) +{ + if( size == 0 ) + return; + png_structp png_ptr = (png_structp)_png_ptr; + PngEncoder* encoder = (PngEncoder*)(png_get_io_ptr(png_ptr)); + size_t cursz = encoder->m_buf->size(); + encoder->m_buf->resize(cursz + size); + memcpy( &(*encoder->m_buf)[cursz], src, size ); +} + + + +template +bool PngEncoder::write( Img& img, const std::vector& params ) +{ + png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 ); + png_infop info_ptr = 0; + FILE * volatile f = 0; + int y, width = img.getSizes()[1], height = img.getSizes()[0]; + int channels = img.channels(); + volatile bool result = false; + uchar**buffer = new uchar*[height]; + T*data = img.getData(); + if( png_ptr ) + { + info_ptr = png_create_info_struct( png_ptr ); + + if( info_ptr ) + { + if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 ) + { + + f = fopen( this->m_filename.c_str(), "wb" ); + if( f ) + png_init_io( png_ptr, (png_FILE_p)f ); + + + int compression_level = -1; // Invalid value to allow setting 0-9 as valid + int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy + bool isBilevel = false; + + if( f ) + { + if( compression_level >= 0 ) + { + png_set_compression_level( png_ptr, compression_level ); + } + else + { + // tune parameters for speed + // (see http://wiki.linuxquestions.org/wiki/Libpng) + png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); + png_set_compression_level(png_ptr, Z_BEST_SPEED); + } + png_set_compression_strategy(png_ptr, compression_strategy); + + png_set_IHDR( png_ptr, info_ptr, width, height, 1 ? isBilevel?1:8 : 16, + channels == 1 ? PNG_COLOR_TYPE_GRAY : + channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT ); + + png_write_info( png_ptr, info_ptr ); + + if (isBilevel) + png_set_packing(png_ptr); + + png_set_bgr( png_ptr ); + if( !isBigEndian() ) + png_set_swap( png_ptr ); + + size_t step = width * img.channels(); + uchar *myArry = new uchar[img.getSize()]; + for(int i = 0 ; i < img.getSize() ; i++) + { + myArry[i] = (uchar)data[i]; + } + + for(int y = 0; y < height; y++ ) + buffer[y] = myArry + y*step; + + png_write_image( png_ptr, buffer ); + png_write_end( png_ptr, info_ptr ); + + result = true; + } + } + } + } + + png_destroy_write_struct( &png_ptr, &info_ptr ); + if(f) fclose( (FILE*)f ); + + return result; +} +} // namespace dip +#endif/*_GRFMT_PNG_H_*/ \ No newline at end of file From 044864e7ffd310feb11d2220dabc017face6f8ca Mon Sep 17 00:00:00 2001 From: Guan-schoolmate Date: Fri, 22 Sep 2023 02:54:54 +0000 Subject: [PATCH 03/10] [DIP] Modify file format --- .../buddy/DIP/imgcodecs/grfmt_png.h | 583 ++++++++---------- 1 file changed, 271 insertions(+), 312 deletions(-) diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h index 666d09aac1..bc423f12e4 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h @@ -49,386 +49,345 @@ #ifndef _GRFMT_PNG_H_ #define _GRFMT_PNG_H_ - #ifndef _LFS64_LARGEFILE -# define _LFS64_LARGEFILE 0 +#define _LFS64_LARGEFILE 0 #endif #ifndef _FILE_OFFSET_BITS -# define _FILE_OFFSET_BITS 0 +#define _FILE_OFFSET_BITS 0 #endif -#include -#include - -#include "buddy/DIP/imgcodecs/grfmt_base.h" #include "buddy/DIP/imgcodecs/bitstrm.h" +#include "buddy/DIP/imgcodecs/grfmt_base.h" +#include +#include #if defined _MSC_VER && _MSC_VER >= 1200 - // interaction between '_setjmp' and C++ object destruction is non-portable - #pragma warning( disable: 4611 ) +// interaction between '_setjmp' and C++ object destruction is non-portable +#pragma warning(disable : 4611) #endif -// the following defines are a hack to avoid multiple problems with frame pointer handling and setjmp -// see http://gcc.gnu.org/ml/gcc/2011-10/msg00324.html for some details +// the following defines are a hack to avoid multiple problems with frame +// pointer handling and setjmp see +// http://gcc.gnu.org/ml/gcc/2011-10/msg00324.html for some details #define mingw_getsp(...) 0 #define __builtin_frame_address(...) 0 -namespace dip -{ + +namespace dip { template -class PngDecoder : public BaseImageDecoder -{ +class PngDecoder : public BaseImageDecoder { public: + PngDecoder(); + virtual ~PngDecoder(); + bool readData(Img &img); + bool readHeader(); + void close(); + std::unique_ptr> newDecoder() const; - PngDecoder(); - virtual ~PngDecoder(); - bool readData( Img& img ) ; - bool readHeader() ; - void close(); - std::unique_ptr> newDecoder() const ; protected: - - int m_bit_depth; - void* m_png_ptr; // pointer to decompression structure - void* m_info_ptr; // pointer to image information structure - void* m_end_info; // pointer to one more image information structure - FILE* m_f; - int m_color_type; - size_t m_buf_pos; + int m_bit_depth; + void *m_png_ptr; // pointer to decompression structure + void *m_info_ptr; // pointer to image information structure + void *m_end_info; // pointer to one more image information structure + FILE *m_f; + int m_color_type; + size_t m_buf_pos; }; template -class PngEncoder : public BaseImageEncoder -{ +class PngEncoder : public BaseImageEncoder { public: - PngEncoder(); - virtual ~PngEncoder(); - bool write( Img& img, const std::vector& params ) ; - std::unique_ptr> newEncoder() const; + PngEncoder(); + virtual ~PngEncoder(); + bool write(Img &img, const std::vector ¶ms); + std::unique_ptr> newEncoder() const; + protected: - static void writeDataToBuf(void* png_ptr, uchar* src, size_t size); + static void writeDataToBuf(void *png_ptr, uchar *src, size_t size); }; bool isBigEndian() { - int num = 1; - char* ptr = (char*)# - return (*ptr == 0); + int num = 1; + char *ptr = (char *)# + return (*ptr == 0); } - /////////////////////// PngDecoder /////////////////// -template -PngDecoder::PngDecoder() -{ - this->m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa"; - m_color_type = 0; - m_png_ptr = 0; - m_info_ptr = m_end_info = 0; - m_f = 0; - this->m_buf_supported = true; - m_buf_pos = 0; - m_bit_depth = 0; +template PngDecoder::PngDecoder() { + this->m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa"; + m_color_type = 0; + m_png_ptr = 0; + m_info_ptr = m_end_info = 0; + m_f = 0; + this->m_buf_supported = true; + m_buf_pos = 0; + m_bit_depth = 0; } -template -PngDecoder::~PngDecoder() -{ - close(); -} +template PngDecoder::~PngDecoder() { close(); } template std::unique_ptr> PngDecoder::newDecoder() const { return std::make_unique>(); } -template -void PngDecoder::close() -{ - if( m_f ) - { - fclose( m_f ); - m_f = 0; - } - - if( m_png_ptr ) - { - png_structp png_ptr = (png_structp)m_png_ptr; - png_infop info_ptr = (png_infop)m_info_ptr; - png_infop end_info = (png_infop)m_end_info; - png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); - m_png_ptr = m_info_ptr = m_end_info = 0; - } +template void PngDecoder::close() { + if (m_f) { + fclose(m_f); + m_f = 0; + } + if (m_png_ptr) { + png_structp png_ptr = (png_structp)m_png_ptr; + png_infop info_ptr = (png_infop)m_info_ptr; + png_infop end_info = (png_infop)m_end_info; + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + m_png_ptr = m_info_ptr = m_end_info = 0; + } } - -template -bool PngDecoder::readHeader() -{ - volatile bool result = false; - close(); - png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 ); - if( png_ptr ) - { - png_infop info_ptr = png_create_info_struct( png_ptr ); - png_infop end_info = png_create_info_struct( png_ptr ); - m_png_ptr = png_ptr; - m_info_ptr = info_ptr; - m_end_info = end_info; - m_buf_pos = 0; - if( info_ptr && end_info ) - { - if( setjmp( png_jmpbuf( png_ptr ) ) == 0 ) - { - m_f = fopen( this->m_filename.c_str(), "rb" ); - if( m_f ) - png_init_io( png_ptr, m_f ); - if(m_f) - { - png_uint_32 wdth, hght; - int bit_depth, color_type, num_trans=0; - png_bytep trans; - png_color_16p trans_values; - png_read_info( png_ptr, info_ptr ); - png_get_IHDR( png_ptr, info_ptr, &wdth, &hght, - &bit_depth, &color_type, 0, 0, 0 ); - this->m_width = (int)wdth; - this->m_height = (int)hght; - m_color_type = color_type; - m_bit_depth = bit_depth; - if( bit_depth <= 8 || bit_depth == 16 ) - { - switch(color_type) - { - case PNG_COLOR_TYPE_RGB: - case PNG_COLOR_TYPE_PALETTE: - png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values); - if( num_trans > 0 ) - this->m_channels = 4; - else - this->m_channels = 3; - break; - case PNG_COLOR_TYPE_GRAY_ALPHA: - case PNG_COLOR_TYPE_RGB_ALPHA: - this->m_channels = 4; - break; - default: - this->m_channels = 1; - } - result = true; - } - } +template bool PngDecoder::readHeader() { + volatile bool result = false; + close(); + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); + if (png_ptr) { + png_infop info_ptr = png_create_info_struct(png_ptr); + png_infop end_info = png_create_info_struct(png_ptr); + m_png_ptr = png_ptr; + m_info_ptr = info_ptr; + m_end_info = end_info; + m_buf_pos = 0; + if (info_ptr && end_info) { + if (setjmp(png_jmpbuf(png_ptr)) == 0) { + m_f = fopen(this->m_filename.c_str(), "rb"); + if (m_f) + png_init_io(png_ptr, m_f); + if (m_f) { + png_uint_32 wdth, hght; + int bit_depth, color_type, num_trans = 0; + png_bytep trans; + png_color_16p trans_values; + png_read_info(png_ptr, info_ptr); + png_get_IHDR(png_ptr, info_ptr, &wdth, &hght, &bit_depth, &color_type, + 0, 0, 0); + this->m_width = (int)wdth; + this->m_height = (int)hght; + m_color_type = color_type; + m_bit_depth = bit_depth; + if (bit_depth <= 8 || bit_depth == 16) { + switch (color_type) { + case PNG_COLOR_TYPE_RGB: + case PNG_COLOR_TYPE_PALETTE: + png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, + &trans_values); + if (num_trans > 0) + this->m_channels = 4; + else + this->m_channels = 3; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + case PNG_COLOR_TYPE_RGB_ALPHA: + this->m_channels = 4; + break; + default: + this->m_channels = 1; } + result = true; + } } + } } - if( !result ) - close(); - return result; + } + if (!result) + close(); + return result; } template -bool PngDecoder::readData( Img& img ) -{ - volatile bool result = false; - uchar**_buffer = new uchar*[this->m_height]; - uchar** buffer = _buffer; - bool color = img.channels() > 1; - T*data = img.getData(); - png_structp png_ptr = (png_structp)m_png_ptr; - png_infop info_ptr = (png_infop)m_info_ptr; - png_infop end_info = (png_infop)m_end_info; - if( m_png_ptr && m_info_ptr && m_end_info && this->m_width && this->m_height ) - { - if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 ) - { - int y; - - if( m_bit_depth == 16 ) - png_set_strip_16( png_ptr ); - else if( !isBigEndian() ) - png_set_swap( png_ptr ); - - if(img.channels() < 4) - { - /* observation: png_read_image() writes 400 bytes beyond - * end of data when reading a 400x118 color png - * "mpplus_sand.png". OpenCV crashes even with demo - * programs. Looking at the loaded image I'd say we get 4 - * bytes per pixel instead of 3 bytes per pixel. Test - * indicate that it is a good idea to always ask for - * stripping alpha.. 18.11.2004 Axel Walthelm - */ - png_set_strip_alpha( png_ptr ); - } else - png_set_tRNS_to_alpha( png_ptr ); - - if( m_color_type == PNG_COLOR_TYPE_PALETTE ) - png_set_palette_to_rgb( png_ptr ); - - if( (m_color_type & PNG_COLOR_MASK_COLOR) == 0 && m_bit_depth < 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 ); +bool PngDecoder::readData(Img &img) { + volatile bool result = false; + uchar **_buffer = new uchar *[this->m_height]; + uchar **buffer = _buffer; + bool color = img.channels() > 1; + T *data = img.getData(); + png_structp png_ptr = (png_structp)m_png_ptr; + png_infop info_ptr = (png_infop)m_info_ptr; + png_infop end_info = (png_infop)m_end_info; + if (m_png_ptr && m_info_ptr && m_end_info && this->m_width && + this->m_height) { + if (setjmp(png_jmpbuf(png_ptr)) == 0) { + int y; + + if (m_bit_depth == 16) + png_set_strip_16(png_ptr); + else if (!isBigEndian()) + png_set_swap(png_ptr); + + if (img.channels() < 4) { + /* observation: png_read_image() writes 400 bytes beyond + * end of data when reading a 400x118 color png + * "mpplus_sand.png". OpenCV crashes even with demo + * programs. Looking at the loaded image I'd say we get 4 + * bytes per pixel instead of 3 bytes per pixel. Test + * indicate that it is a good idea to always ask for + * stripping alpha.. 18.11.2004 Axel Walthelm + */ + png_set_strip_alpha(png_ptr); + } else + png_set_tRNS_to_alpha(png_ptr); + + if (m_color_type == PNG_COLOR_TYPE_PALETTE) + png_set_palette_to_rgb(png_ptr); + + if ((m_color_type & PNG_COLOR_MASK_COLOR) == 0 && m_bit_depth < 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 ); + png_set_gray_1_2_4_to_8(png_ptr); #endif - if( (m_color_type & PNG_COLOR_MASK_COLOR) && color ) - png_set_bgr( png_ptr ); // convert RGB to BGR - else if( color ) - png_set_gray_to_rgb( png_ptr ); // Gray->RGB - else - png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray - - png_set_interlace_handling( png_ptr ); - png_read_update_info( png_ptr, info_ptr ); - - size_t step = this->m_width * img.channels(); - uchar *myArry = new uchar[img.getSize()]; - for(int y = 0; y < this->m_height; y++ ) - buffer[y] = myArry + y*step; - - png_read_image( png_ptr, buffer ); - png_read_end( png_ptr, end_info ); - for(int i = 0 ; i < img.getSize() ; i++) - { - data[i] = (T)myArry[i]; - } + if ((m_color_type & PNG_COLOR_MASK_COLOR) && color) + png_set_bgr(png_ptr); // convert RGB to BGR + else if (color) + png_set_gray_to_rgb(png_ptr); // Gray->RGB + else + png_set_rgb_to_gray(png_ptr, 1, 0.299, 0.587); // RGB->Gray -#ifdef PNG_eXIf_SUPPORTED - png_uint_32 num_exif = 0; - png_bytep exif = 0; - - // Exif info could be in info_ptr (intro_info) or end_info per specification - if( png_get_valid(png_ptr, info_ptr, PNG_INFO_eXIf) ) - png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif); - else if( png_get_valid(png_ptr, end_info, PNG_INFO_eXIf) ) - png_get_eXIf_1(png_ptr, end_info, &num_exif, &exif); - -#endif + png_set_interlace_handling(png_ptr); + png_read_update_info(png_ptr, info_ptr); - result = true; - } - } + size_t step = this->m_width * img.channels(); + uchar *myArry = new uchar[img.getSize()]; + for (int y = 0; y < this->m_height; y++) + buffer[y] = myArry + y * step; - close(); - return result; -} + png_read_image(png_ptr, buffer); + png_read_end(png_ptr, end_info); + for (int i = 0; i < img.getSize(); i++) { + data[i] = (T)myArry[i]; + } +#ifdef PNG_eXIf_SUPPORTED + png_uint_32 num_exif = 0; + png_bytep exif = 0; -/////////////////////// PngEncoder /////////////////// + // Exif info could be in info_ptr (intro_info) or end_info per + // specification + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_eXIf)) + png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif); + else if (png_get_valid(png_ptr, end_info, PNG_INFO_eXIf)) + png_get_eXIf_1(png_ptr, end_info, &num_exif, &exif); -template -PngEncoder::PngEncoder() -{ - this->m_description = "Portable Network Graphics files (*.png)"; - this->m_buf_supported = true; +#endif + result = true; + } + } + close(); + return result; } -template -PngEncoder::~PngEncoder() -{ +/////////////////////// PngEncoder /////////////////// +template PngEncoder::PngEncoder() { + this->m_description = "Portable Network Graphics files (*.png)"; + this->m_buf_supported = true; } +template PngEncoder::~PngEncoder() {} + template std::unique_ptr> PngEncoder::newEncoder() const { return std::make_unique>(); } template -void PngEncoder::writeDataToBuf(void* _png_ptr, uchar* src, size_t size) -{ - if( size == 0 ) - return; - png_structp png_ptr = (png_structp)_png_ptr; - PngEncoder* encoder = (PngEncoder*)(png_get_io_ptr(png_ptr)); - size_t cursz = encoder->m_buf->size(); - encoder->m_buf->resize(cursz + size); - memcpy( &(*encoder->m_buf)[cursz], src, size ); +void PngEncoder::writeDataToBuf(void *_png_ptr, uchar *src, size_t size) { + if (size == 0) + return; + png_structp png_ptr = (png_structp)_png_ptr; + PngEncoder *encoder = (PngEncoder *)(png_get_io_ptr(png_ptr)); + size_t cursz = encoder->m_buf->size(); + encoder->m_buf->resize(cursz + size); + memcpy(&(*encoder->m_buf)[cursz], src, size); } - - template -bool PngEncoder::write( Img& img, const std::vector& params ) -{ - png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 ); - png_infop info_ptr = 0; - FILE * volatile f = 0; - int y, width = img.getSizes()[1], height = img.getSizes()[0]; - int channels = img.channels(); - volatile bool result = false; - uchar**buffer = new uchar*[height]; - T*data = img.getData(); - if( png_ptr ) - { - info_ptr = png_create_info_struct( png_ptr ); - - if( info_ptr ) - { - if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 ) - { - - f = fopen( this->m_filename.c_str(), "wb" ); - if( f ) - png_init_io( png_ptr, (png_FILE_p)f ); - - - int compression_level = -1; // Invalid value to allow setting 0-9 as valid - int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy - bool isBilevel = false; - - if( f ) - { - if( compression_level >= 0 ) - { - png_set_compression_level( png_ptr, compression_level ); - } - else - { - // tune parameters for speed - // (see http://wiki.linuxquestions.org/wiki/Libpng) - png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); - png_set_compression_level(png_ptr, Z_BEST_SPEED); - } - png_set_compression_strategy(png_ptr, compression_strategy); - - png_set_IHDR( png_ptr, info_ptr, width, height, 1 ? isBilevel?1:8 : 16, - channels == 1 ? PNG_COLOR_TYPE_GRAY : - channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT ); - - png_write_info( png_ptr, info_ptr ); - - if (isBilevel) - png_set_packing(png_ptr); - - png_set_bgr( png_ptr ); - if( !isBigEndian() ) - png_set_swap( png_ptr ); - - size_t step = width * img.channels(); - uchar *myArry = new uchar[img.getSize()]; - for(int i = 0 ; i < img.getSize() ; i++) - { - myArry[i] = (uchar)data[i]; - } - - for(int y = 0; y < height; y++ ) - buffer[y] = myArry + y*step; - - png_write_image( png_ptr, buffer ); - png_write_end( png_ptr, info_ptr ); - - result = true; - } - } +bool PngEncoder::write(Img &img, const std::vector ¶ms) { + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); + png_infop info_ptr = 0; + FILE *volatile f = 0; + int y, width = img.getSizes()[1], height = img.getSizes()[0]; + int channels = img.channels(); + volatile bool result = false; + uchar **buffer = new uchar *[height]; + T *data = img.getData(); + if (png_ptr) { + info_ptr = png_create_info_struct(png_ptr); + + if (info_ptr) { + if (setjmp(png_jmpbuf(png_ptr)) == 0) { + + f = fopen(this->m_filename.c_str(), "wb"); + if (f) + png_init_io(png_ptr, (png_FILE_p)f); + + int compression_level = + -1; // Invalid value to allow setting 0-9 as valid + int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy + bool isBilevel = false; + + if (f) { + if (compression_level >= 0) { + png_set_compression_level(png_ptr, compression_level); + } else { + // tune parameters for speed + // (see http://wiki.linuxquestions.org/wiki/Libpng) + png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); + png_set_compression_level(png_ptr, Z_BEST_SPEED); + } + png_set_compression_strategy(png_ptr, compression_strategy); + + png_set_IHDR(png_ptr, info_ptr, width, height, + 1 ? isBilevel ? 1 : 8 : 16, + channels == 1 ? PNG_COLOR_TYPE_GRAY + : channels == 3 ? PNG_COLOR_TYPE_RGB + : PNG_COLOR_TYPE_RGBA, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + if (isBilevel) + png_set_packing(png_ptr); + + png_set_bgr(png_ptr); + if (!isBigEndian()) + png_set_swap(png_ptr); + + size_t step = width * img.channels(); + uchar *myArry = new uchar[img.getSize()]; + for (int i = 0; i < img.getSize(); i++) { + myArry[i] = (uchar)data[i]; + } + + for (int y = 0; y < height; y++) + buffer[y] = myArry + y * step; + + png_write_image(png_ptr, buffer); + png_write_end(png_ptr, info_ptr); + + result = true; } + } } + } - png_destroy_write_struct( &png_ptr, &info_ptr ); - if(f) fclose( (FILE*)f ); + png_destroy_write_struct(&png_ptr, &info_ptr); + if (f) + fclose((FILE *)f); - return result; + return result; } } // namespace dip -#endif/*_GRFMT_PNG_H_*/ \ No newline at end of file +#endif /*_GRFMT_PNG_H_*/ \ No newline at end of file From d89a00d66b79e66e33a1dcff0983e113ac414bd5 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate Date: Mon, 25 Sep 2023 06:37:29 +0000 Subject: [PATCH 04/10] [DIP] Delete the type of write --- .../buddy/DIP/imgcodecs/grfmt_jpeg.h | 39 ------------------- .../buddy/DIP/imgcodecs/grfmt_png.h | 2 +- .../buddy/DIP/imgcodecs/replenishment.h | 30 -------------- 3 files changed, 1 insertion(+), 70 deletions(-) diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h index 38c3f75578..dbfa9db53a 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h @@ -663,45 +663,6 @@ bool JpegEncoder::write(Img &img, int rst_interval = 0; int luma_quality = -1; int chroma_quality = -1; - - for (size_t i = 0; i < params.size(); i += 2) { - if (params[i] == IMWRITE_JPEG_QUALITY) { - quality = params[i + 1]; - quality = MIN(MAX(quality, 0), 100); - } - - if (params[i] == IMWRITE_JPEG_PROGRESSIVE) { - progressive = params[i + 1]; - } - - if (params[i] == IMWRITE_JPEG_OPTIMIZE) { - optimize = params[i + 1]; - } - - if (params[i] == IMWRITE_JPEG_LUMA_QUALITY) { - if (params[i + 1] >= 0) { - luma_quality = MIN(MAX(params[i + 1], 0), 100); - - quality = luma_quality; - - if (chroma_quality < 0) { - chroma_quality = luma_quality; - } - } - } - - if (params[i] == IMWRITE_JPEG_CHROMA_QUALITY) { - if (params[i + 1] >= 0) { - chroma_quality = MIN(MAX(params[i + 1], 0), 100); - } - } - - if (params[i] == IMWRITE_JPEG_RST_INTERVAL) { - rst_interval = params[i + 1]; - rst_interval = MIN(MAX(rst_interval, 0), 65535L); - } - } - jpeg_set_defaults(&cinfo); cinfo.restart_interval = rst_interval; diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h index bc423f12e4..36e6f317ef 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h @@ -334,7 +334,7 @@ bool PngEncoder::write(Img &img, const std::vector ¶ms) { int compression_level = -1; // Invalid value to allow setting 0-9 as valid - int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy + int compression_strategy = 3; // Default strategy bool isBilevel = false; if (f) { diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h b/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h index 5eb35fecdb..2fd5cfe4b2 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/replenishment.h @@ -77,36 +77,6 @@ enum ImreadModes { IMGRD_ANYCOLOR = 4, }; -// Imwrite flags -enum ImwriteFlags { - // For JPEG, it can be a quality from 0 to 100 (the higher is the - // better). Default value is 95. - IMWRITE_JPEG_QUALITY = 1, - // Enable JPEG features, 0 or 1, default is False. - IMWRITE_JPEG_PROGRESSIVE = 2, - // Enable JPEG features, 0 or 1, default is False. - IMWRITE_JPEG_OPTIMIZE = 3, - // JPEG restart interval, 0 - 65535, default is 0 - no restart. - IMWRITE_JPEG_RST_INTERVAL = 4, - // Separate luma quality level, 0 - 100, default is 0 - don't use. - IMWRITE_JPEG_LUMA_QUALITY = 5, - // Separate chroma quality level, 0 - 100, default is 0 - don't use. - IMWRITE_JPEG_CHROMA_QUALITY = 6, - // Use this value for normal data. - IMWRITE_PNG_STRATEGY_DEFAULT = 0, - // Use this value for data produced by a filter (or predictor).Filtered data - // consists mostly of small values with a somewhat random //distribution. In - // this case, the compression algorithm is tuned to compress them better. - IMWRITE_PNG_STRATEGY_FILTERED = 1, - // Use this value to force Huffman encoding only (no string match). - IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2, - // Use this value to limit match distances to one (run-length encoding). - IMWRITE_PNG_STRATEGY_RLE = 3, - // Using this value prevents the use of dynamic Huffman codes, allowing for a - // simpler decoder for special applications. - IMWRITE_PNG_STRATEGY_FIXED = 4, -}; - class _Size { public: _Size(){}; From dfa7fcd8ec400aa01ff98ebc1a8a0dc1ed31b6a8 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Thu, 12 Oct 2023 15:22:29 +0800 Subject: [PATCH 05/10] Modify format and Add jpeg test --- .../buddy/DIP/imgcodecs/grfmt_bmp.h | 38 ++++++++-------- .../buddy/DIP/imgcodecs/grfmt_jpeg.h | 41 +++++++++-------- .../buddy/DIP/imgcodecs/grfmt_png.h | 42 +++++++++--------- tests/Interface/core/TestGrayImage.jpg | Bin 0 -> 363 bytes 4 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 tests/Interface/core/TestGrayImage.jpg diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_bmp.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_bmp.h index 53b7400f8c..b6026184ef 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_bmp.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_bmp.h @@ -1,11 +1,10 @@ -//===- Grfmt_bmp.h ---------------------------------------------------===// +//===-----------------------grfmt_bmp.h----------------------------===// // -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this -// license. If you do not agree to this license, do not download, install, copy -// or use the software. +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // +// By downloading, copying, installing or using the software you agree to +// this license. If you do not agree to this license, do not download, +// install, copy or use the software. // // Intel License Agreement // For Open Source Computer Vision Library @@ -16,17 +15,17 @@ // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright -// notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. +// * Redistribution's in binary form must reproduce the above copyright +// notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote -// products -// derived from this software without specific prior written permission. +// * The name of Intel Corporation may not be used to endorse or promote +// products +// derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" // and any express or implied warranties, including, but not limited to, the @@ -41,11 +40,12 @@ // possibility of such damage. // // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// // -// This file is modified from opencv's modules/imgcodecs/src/grfmt_bmp.hpp file +// This file is modified from opencv's +// modules/imgcodecs/src/grfmt_bmp.hpp file // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// #ifndef _GRFMT_BMP_H_ #define _GRFMT_BMP_H_ @@ -450,4 +450,4 @@ bool BmpEncoder::write(Img &img, const std::vector &) { return true; } } // namespace dip -#endif /*_GRFMT_BMP_H_*/ +#endif /*_GRFMT_BMP_H_*/ \ No newline at end of file diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h index dbfa9db53a..7346455d44 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h @@ -1,11 +1,10 @@ -//===- Grfmt_jpeg.h ---------------------------------------------------===// +//===-----------------------grfmt_jpeg.h----------------------------===// // -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this -// license. If you do not agree to this license, do not download, install, copy -// or use the software. +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // +// By downloading, copying, installing or using the software you agree to +// this license. If you do not agree to this license, do not download, +// install, copy or use the software. // // Intel License Agreement // For Open Source Computer Vision Library @@ -16,17 +15,17 @@ // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright -// notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. +// * Redistribution's in binary form must reproduce the above copyright +// notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote -// products -// derived from this software without specific prior written permission. +// * The name of Intel Corporation may not be used to endorse or promote +// products +// derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" // and any express or implied warranties, including, but not limited to, the @@ -41,11 +40,12 @@ // possibility of such damage. // // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// // -// This file is modified from opencv's modules/imgcodecs/src/grfmt_jpeg.hpp file +// This file is modified from opencv's +// modules/imgcodecs/src/grfmt_jpeg.hpp file // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// #ifndef _GRFMT_JPEG_H_ #define _GRFMT_JPEG_H_ @@ -603,8 +603,7 @@ std::unique_ptr> JpegEncoder::newEncoder() const { } template -bool JpegEncoder::write(Img &img, - const std::vector ¶ms) { +bool JpegEncoder::write(Img &img, const std::vector ¶ms) { this->m_last_error.clear(); @@ -760,4 +759,4 @@ bool JpegEncoder::write(Img &img, return result; } } // namespace dip -#endif /*_GRFMT_JPEG_H_*/ +#endif /*_GRFMT_JPEG_H_*/ \ No newline at end of file diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h index 36e6f317ef..b2ad33a386 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_png.h @@ -1,11 +1,10 @@ -//===- Grfmt_png.hpp ---------------------------------------------------===// +//===-----------------------grfmt_png.h----------------------------===// // -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this -// license. If you do not agree to this license, do not download, install, copy -// or use the software. +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // +// By downloading, copying, installing or using the software you agree to +// this license. If you do not agree to this license, do not download, +// install, copy or use the software. // // Intel License Agreement // For Open Source Computer Vision Library @@ -16,17 +15,17 @@ // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. // -// * Redistribution's in binary form must reproduce the above copyright -// notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. +// * Redistribution's in binary form must reproduce the above copyright +// notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. // -// * The name of Intel Corporation may not be used to endorse or promote -// products -// derived from this software without specific prior written permission. +// * The name of Intel Corporation may not be used to endorse or promote +// products +// derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" // and any express or implied warranties, including, but not limited to, the @@ -41,11 +40,12 @@ // possibility of such damage. // // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// // -// This file is modified from opencv's modules/imgcodecs/src/grfmt_png.hpp file +// This file is modified from opencv's +// modules/imgcodecs/src/grfmt_png.hpp file // -//===----------------------------------------------------------------------===// +//----------------------------------------------------------------------// #ifndef _GRFMT_PNG_H_ #define _GRFMT_PNG_H_ @@ -268,7 +268,8 @@ bool PngDecoder::readData(Img &img) { for (int i = 0; i < img.getSize(); i++) { data[i] = (T)myArry[i]; } - + delete[] myArry; + delete[] _buffer; #ifdef PNG_eXIf_SUPPORTED png_uint_32 num_exif = 0; png_bytep exif = 0; @@ -376,7 +377,8 @@ bool PngEncoder::write(Img &img, const std::vector ¶ms) { png_write_image(png_ptr, buffer); png_write_end(png_ptr, info_ptr); - + delete[] myArry; + delete[] buffer; result = true; } } diff --git a/tests/Interface/core/TestGrayImage.jpg b/tests/Interface/core/TestGrayImage.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5e387a2a361d53c3f029fe611fce13ce4e3480ab GIT binary patch literal 363 zcmex=|HF0u@iAzXIsj8`KXlj|5nweWzS~We&gn?hmRgVdHU@6i$mSee*XG@i-7|a tZ1xO)_x(`*&%omOpP{MpKf|Ft7ymOnx=>&JFRtYL&!_Bv9kc)61OS4)bt(V= literal 0 HcmV?d00001 From 3a6f794329922e5fe7d177e398a225aea8ce22c8 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Mon, 16 Oct 2023 13:48:05 +0800 Subject: [PATCH 06/10] Testing bmp and jpeg images in ImageContainerTest.cpp, binding libjpeg libpng and BuddyLibDIP together to form DIP_LIBS --- examples/DIPDialect/CMakeLists.txt | 21 +- tests/Interface/core/CMakeLists.txt | 18 +- tests/Interface/core/ImageContainerTest.cpp | 239 +++++++++++++++++--- 3 files changed, 234 insertions(+), 44 deletions(-) diff --git a/examples/DIPDialect/CMakeLists.txt b/examples/DIPDialect/CMakeLists.txt index 4afd40d606..441652a63e 100644 --- a/examples/DIPDialect/CMakeLists.txt +++ b/examples/DIPDialect/CMakeLists.txt @@ -1,29 +1,30 @@ if(BUDDY_ENABLE_OPENCV) find_package(OpenCV REQUIRED CONFIG) - find_package(JPEG REQUIRED) - find_package(PNG REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) - include_directories(${JPEG_INCLUDE_DIRS}) - include_directories(${PNG_INCLUDE_DIRS}) endif() +find_package(JPEG REQUIRED) +find_package(PNG REQUIRED) +add_library(DIP_LIBS INTERFACE) +target_link_libraries(DIP_LIBS INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) + add_executable(correlation2D correlation2D.cpp) -target_link_libraries(correlation2D ${OpenCV_LIBS} BuddyLibDIP) +target_link_libraries(correlation2D ${OpenCV_LIBS} DIP_LIBS) add_executable(correlationFFT2D correlationFFT2D.cpp) -target_link_libraries(correlationFFT2D ${OpenCV_LIBS} BuddyLibDIP) +target_link_libraries(correlationFFT2D ${OpenCV_LIBS} DIP_LIBS) add_executable(rotation2D rotation2D.cpp) -target_link_libraries(rotation2D ${OpenCV_LIBS} ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) +target_link_libraries(rotation2D ${OpenCV_LIBS} DIP_LIBS) add_executable(resize2D resize2D.cpp) -target_link_libraries(resize2D ${OpenCV_LIBS} ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) +target_link_libraries(resize2D ${OpenCV_LIBS} DIP_LIBS) add_executable(morph2D morph2D.cpp) -target_link_libraries(morph2D ${OpenCV_LIBS} BuddyLibDIP) +target_link_libraries(morph2D ${OpenCV_LIBS} DIP_LIBS) add_executable(dip-all DIPAll.cpp) -target_link_libraries(dip-all ${OpenCV_LIBS} BuddyLibDIP) +target_link_libraries(dip-all ${OpenCV_LIBS} DIP_LIBS) add_executable(opencv-all OpenCVAll.cpp) target_link_libraries(opencv-all ${OpenCV_LIBS}) diff --git a/tests/Interface/core/CMakeLists.txt b/tests/Interface/core/CMakeLists.txt index 6ad3594310..94fd8d18a8 100644 --- a/tests/Interface/core/CMakeLists.txt +++ b/tests/Interface/core/CMakeLists.txt @@ -3,20 +3,22 @@ _add_test_executable(buddy-container-test ) if(BUDDY_ENABLE_OPENCV) -find_package(OpenCV REQUIRED CONFIG) + find_package(OpenCV REQUIRED CONFIG) + include_directories(${OpenCV_INCLUDE_DIRS}) +endif() + find_package(JPEG REQUIRED) find_package(PNG REQUIRED) -include_directories(${OpenCV_INCLUDE_DIRS}) -include_directories(${JPEG_INCLUDE_DIRS}) -include_directories(${PNG_INCLUDE_DIRS}) +add_library(DIP_LIB INTERFACE) +target_link_libraries(DIP_LIB INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY}) + _add_test_executable(buddy-image-container-test ImageContainerTest.cpp LINK_LIBS ${OpenCV_LIBS} - ${JPEG_LIBRARY} - ${PNG_LIBRARY} + DIP_LIB ) -endif() + _add_test_executable(buddy-audio-container-test AudioContainerTest.cpp @@ -24,4 +26,4 @@ _add_test_executable(buddy-audio-container-test _add_test_executable(buddy-text-container-test TextContainerTest.cpp -) +) \ No newline at end of file diff --git a/tests/Interface/core/ImageContainerTest.cpp b/tests/Interface/core/ImageContainerTest.cpp index 3dd804ce74..442f79ca6c 100644 --- a/tests/Interface/core/ImageContainerTest.cpp +++ b/tests/Interface/core/ImageContainerTest.cpp @@ -33,32 +33,18 @@ int main() { // 195.0, 210.0, 225.0, 240.0 // The test running directory is in /tests/Interface/core, so the // `imread` function uses the following relative path. - Img grayimage = dip::imread( - "../../../../tests/Interface/core/TestGrayImage.png", - dip::IMGRD_GRAYSCALE); + //===--------------------------------------------------------------------===// - // Test image constructor for OpenCV. + // Test bmp format image. //===--------------------------------------------------------------------===// - Img testOpenCVConstructor(grayimage); - // CHECK: 15.0 - fprintf(stderr, "%f\n", testOpenCVConstructor.getData()[0]); - // CHECK: 4, 4 - fprintf(stderr, "%ld, %ld\n", testOpenCVConstructor.getSizes()[0], - testOpenCVConstructor.getSizes()[1]); - // CHECK: 4, 1 - fprintf(stderr, "%ld, %ld\n", testOpenCVConstructor.getStrides()[0], - testOpenCVConstructor.getStrides()[1]); - // CHECK: 2 - fprintf(stderr, "%ld\n", testOpenCVConstructor.getRank()); - // CHECK: 16 - fprintf(stderr, "%ld\n", testOpenCVConstructor.getSize()); - // CHECK: 60.0 - fprintf(stderr, "%f\n", testOpenCVConstructor[3]); + Img grayimage_bmp = dip::imread( + "../../../../tests/Interface/core/TestGrayImage.bmp", + dip::IMGRD_GRAYSCALE); //===--------------------------------------------------------------------===// // Test copy constructor. //===--------------------------------------------------------------------===// - Img testCopyConstructor1(testOpenCVConstructor); + Img testCopyConstructor1(grayimage_bmp); // CHECK: 15.0 fprintf(stderr, "%f\n", testCopyConstructor1[0]); // CHECK: 4, 4 @@ -74,7 +60,7 @@ int main() { // CHECK: 60.0 fprintf(stderr, "%f\n", testCopyConstructor1[3]); - Img testCopyConstructor2 = testOpenCVConstructor; + Img testCopyConstructor2 = grayimage_bmp; // CHECK: 15.0 fprintf(stderr, "%f\n", testCopyConstructor2[0]); // CHECK: 4, 4 @@ -90,11 +76,11 @@ int main() { // CHECK: 60.0 fprintf(stderr, "%f\n", testCopyConstructor2[3]); Img testCopyConstructor3 = - Img(testOpenCVConstructor); + Img(grayimage_bmp); // CHECK: 15.0 fprintf(stderr, "%f\n", testCopyConstructor3[0]); Img *testCopyConstructor4 = - new Img(testOpenCVConstructor); + new Img(grayimage_bmp); // CHECK: 15.0 fprintf(stderr, "%f\n", testCopyConstructor4->getData()[0]); delete testCopyConstructor4; @@ -133,18 +119,219 @@ int main() { fprintf(stderr, "%ld\n", testMoveConstructor2.getSize()); // CHECK: 60.0 fprintf(stderr, "%f\n", testMoveConstructor2[3]); + //===--------------------------------------------------------------------===// // Test overloading bracket operator. //===--------------------------------------------------------------------===// - Img testBracketOperator1(grayimage); + Img testBracketOperator1(grayimage_bmp); // CHECK: 240.0 fprintf(stderr, "%f\n", testBracketOperator1[15]); testBracketOperator1[15] = 90.0; // CHECK: 90.0 fprintf(stderr, "%f\n", testBracketOperator1[15]); - const Img testBracketOperator2(grayimage); + const Img testBracketOperator2(grayimage_bmp); // CHECK: 240.0 fprintf(stderr, "%f\n", testBracketOperator2[15]); + + //===--------------------------------------------------------------------===// + // Test jpeg format image. + //===--------------------------------------------------------------------===// + Img grayimage_jpg = dip::imread( + "../../../../tests/Interface/core/TestGrayImage.jpg", + dip::IMGRD_GRAYSCALE); + + //===--------------------------------------------------------------------===// + // Test copy constructor. + //===--------------------------------------------------------------------===// + Img testCopyConstructor5(grayimage_jpg); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor5[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor5.getSizes()[0], + testCopyConstructor5.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor5.getStrides()[0], + testCopyConstructor5.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testCopyConstructor5.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testCopyConstructor5.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testCopyConstructor5[3]); + + Img testCopyConstructor6 = grayimage_jpg; + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor6[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor6.getSizes()[0], + testCopyConstructor6.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor6.getStrides()[0], + testCopyConstructor6.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testCopyConstructor6.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testCopyConstructor6.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testCopyConstructor6[3]); + Img testCopyConstructor7 = + Img(grayimage_jpg); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor7[0]); + Img *testCopyConstructor8 = + new Img(grayimage_jpg); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor8->getData()[0]); + delete testCopyConstructor8; + + //===--------------------------------------------------------------------===// + // Test move constructor. + //===--------------------------------------------------------------------===// + Img testMoveConstructor3(std::move(testCopyConstructor5)); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testMoveConstructor3[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor3.getSizes()[0], + testMoveConstructor3.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor3.getStrides()[0], + testMoveConstructor3.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testMoveConstructor3.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testMoveConstructor3.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testMoveConstructor3[3]); + + Img testMoveConstructor4 = std::move(testMoveConstructor1); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testMoveConstructor4[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor4.getSizes()[0], + testMoveConstructor4.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor4.getStrides()[0], + testMoveConstructor4.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testMoveConstructor4.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testMoveConstructor4.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testMoveConstructor4[3]); + + //===--------------------------------------------------------------------===// + // Test overloading bracket operator. + //===--------------------------------------------------------------------===// + Img testBracketOperator3(grayimage_jpg); + // CHECK: 240.0 + fprintf(stderr, "%f\n", testBracketOperator3[15]); + testBracketOperator3[15] = 90.0; + // CHECK: 90.0 + fprintf(stderr, "%f\n", testBracketOperator3[15]); + const Img testBracketOperator4(grayimage_jpg); + // CHECK: 240.0 + fprintf(stderr, "%f\n", testBracketOperator4[15]); + + + //===--------------------------------------------------------------------===// + // Test png format image. + //===--------------------------------------------------------------------===// + Img grayimage_png = dip::imread( + "../../../../tests/Interface/core/TestGrayImage.png", + dip::IMGRD_GRAYSCALE); + + //===--------------------------------------------------------------------===// + // Test copy constructor. + //===--------------------------------------------------------------------===// + Img testCopyConstructor9(grayimage_png); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor9[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor9.getSizes()[0], + testCopyConstructor9.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor9.getStrides()[0], + testCopyConstructor9.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testCopyConstructor9.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testCopyConstructor9.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testCopyConstructor9[3]); + + Img testCopyConstructor10 = grayimage_png; + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor10[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor10.getSizes()[0], + testCopyConstructor10.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testCopyConstructor10.getStrides()[0], + testCopyConstructor10.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testCopyConstructor10.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testCopyConstructor10.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testCopyConstructor10[3]); + Img testCopyConstructor11 = + Img(grayimage_png); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor11[0]); + Img *testCopyConstructor12 = + new Img(grayimage_png); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testCopyConstructor12->getData()[0]); + delete testCopyConstructor12; + + //===--------------------------------------------------------------------===// + // Test move constructor. + //===--------------------------------------------------------------------===// + Img testMoveConstructor5(std::move(testCopyConstructor9)); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testMoveConstructor5[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor5.getSizes()[0], + testMoveConstructor5.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor5.getStrides()[0], + testMoveConstructor5.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testMoveConstructor5.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testMoveConstructor5.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testMoveConstructor5[3]); + + Img testMoveConstructor6 = std::move(testMoveConstructor1); + // CHECK: 15.0 + fprintf(stderr, "%f\n", testMoveConstructor6[0]); + // CHECK: 4, 4 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor6.getSizes()[0], + testMoveConstructor6.getSizes()[1]); + // CHECK: 4, 1 + fprintf(stderr, "%ld, %ld\n", testMoveConstructor6.getStrides()[0], + testMoveConstructor6.getStrides()[1]); + // CHECK: 2 + fprintf(stderr, "%ld\n", testMoveConstructor6.getRank()); + // CHECK: 16 + fprintf(stderr, "%ld\n", testMoveConstructor6.getSize()); + // CHECK: 60.0 + fprintf(stderr, "%f\n", testMoveConstructor6[3]); + + //===--------------------------------------------------------------------===// + // Test overloading bracket operator. + //===--------------------------------------------------------------------===// + Img testBracketOperator5(grayimage_png); + // CHECK: 240.0 + fprintf(stderr, "%f\n", testBracketOperator5[15]); + testBracketOperator5[15] = 90.0; + // CHECK: 90.0 + fprintf(stderr, "%f\n", testBracketOperator5[15]); + const Img testBracketOperator6(grayimage_png); + // CHECK: 240.0 + fprintf(stderr, "%f\n", testBracketOperator6[15]); + return 0; -} +} \ No newline at end of file From 1cc211b9c82ca307a6252481dc98a4ef10aa42f5 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Wed, 18 Oct 2023 13:43:09 +0800 Subject: [PATCH 07/10] Modify the logic for finding dip lib --- CMakeLists.txt | 7 +++++++ examples/DIPDialect/CMakeLists.txt | 2 -- tests/Interface/core/CMakeLists.txt | 2 -- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0678c0f92a..6b2c980128 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,13 @@ include(AddLLVM) include(AddMLIR) include(HandleLLVMOptions) +#------------------------------------------------------------------------------- +# Dip lib configuration +#------------------------------------------------------------------------------- + +find_package(JPEG REQUIRED) +find_package(PNG REQUIRED) + #------------------------------------------------------------------------------- # Hardware detection #------------------------------------------------------------------------------- diff --git a/examples/DIPDialect/CMakeLists.txt b/examples/DIPDialect/CMakeLists.txt index 441652a63e..9b34013e92 100644 --- a/examples/DIPDialect/CMakeLists.txt +++ b/examples/DIPDialect/CMakeLists.txt @@ -3,8 +3,6 @@ if(BUDDY_ENABLE_OPENCV) include_directories(${OpenCV_INCLUDE_DIRS}) endif() -find_package(JPEG REQUIRED) -find_package(PNG REQUIRED) add_library(DIP_LIBS INTERFACE) target_link_libraries(DIP_LIBS INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) diff --git a/tests/Interface/core/CMakeLists.txt b/tests/Interface/core/CMakeLists.txt index 94fd8d18a8..cfd184b549 100644 --- a/tests/Interface/core/CMakeLists.txt +++ b/tests/Interface/core/CMakeLists.txt @@ -7,8 +7,6 @@ if(BUDDY_ENABLE_OPENCV) include_directories(${OpenCV_INCLUDE_DIRS}) endif() -find_package(JPEG REQUIRED) -find_package(PNG REQUIRED) add_library(DIP_LIB INTERFACE) target_link_libraries(DIP_LIB INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY}) From d84c784eb12abe2dae60ed4f1f5defd44e9ec75d Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Fri, 20 Oct 2023 21:58:04 +0800 Subject: [PATCH 08/10] change the dip lib link mode --- examples/DIPDialect/CMakeLists.txt | 15 +++++++-------- tests/Interface/core/CMakeLists.txt | 5 ++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/DIPDialect/CMakeLists.txt b/examples/DIPDialect/CMakeLists.txt index 9b34013e92..1f1b1f0ca5 100644 --- a/examples/DIPDialect/CMakeLists.txt +++ b/examples/DIPDialect/CMakeLists.txt @@ -3,26 +3,25 @@ if(BUDDY_ENABLE_OPENCV) include_directories(${OpenCV_INCLUDE_DIRS}) endif() -add_library(DIP_LIBS INTERFACE) -target_link_libraries(DIP_LIBS INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) +set(DIP_LIBS ${JPEG_LIBRARY} ${PNG_LIBRARY} BuddyLibDIP) add_executable(correlation2D correlation2D.cpp) -target_link_libraries(correlation2D ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(correlation2D ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(correlationFFT2D correlationFFT2D.cpp) -target_link_libraries(correlationFFT2D ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(correlationFFT2D ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(rotation2D rotation2D.cpp) -target_link_libraries(rotation2D ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(rotation2D ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(resize2D resize2D.cpp) -target_link_libraries(resize2D ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(resize2D ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(morph2D morph2D.cpp) -target_link_libraries(morph2D ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(morph2D ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(dip-all DIPAll.cpp) -target_link_libraries(dip-all ${OpenCV_LIBS} DIP_LIBS) +target_link_libraries(dip-all ${OpenCV_LIBS} ${DIP_LIBS}) add_executable(opencv-all OpenCVAll.cpp) target_link_libraries(opencv-all ${OpenCV_LIBS}) diff --git a/tests/Interface/core/CMakeLists.txt b/tests/Interface/core/CMakeLists.txt index cfd184b549..5e50f09e4f 100644 --- a/tests/Interface/core/CMakeLists.txt +++ b/tests/Interface/core/CMakeLists.txt @@ -7,14 +7,13 @@ if(BUDDY_ENABLE_OPENCV) include_directories(${OpenCV_INCLUDE_DIRS}) endif() -add_library(DIP_LIB INTERFACE) -target_link_libraries(DIP_LIB INTERFACE ${JPEG_LIBRARY} ${PNG_LIBRARY}) +set(DIP_LIBS ${JPEG_LIBRARY} ${PNG_LIBRARY}) _add_test_executable(buddy-image-container-test ImageContainerTest.cpp LINK_LIBS ${OpenCV_LIBS} - DIP_LIB + ${DIP_LIBS} ) From 4403d9c5190daff0d328a09bae68d133ce5496da Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Tue, 24 Oct 2023 14:41:01 +0800 Subject: [PATCH 09/10] Conflict resolution --- frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h index 540dd58c30..b2263ba24b 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/grfmt_jpeg.h @@ -603,7 +603,8 @@ std::unique_ptr> JpegEncoder::newEncoder() const { } template -bool JpegEncoder::write(Img &img, const std::vector ¶ms) { +bool JpegEncoder::write(Img &img, + const std::vector ¶ms) { this->m_last_error.clear(); @@ -662,10 +663,7 @@ bool JpegEncoder::write(Img &img, const std::vector ¶ms) { int rst_interval = 0; int luma_quality = -1; int chroma_quality = -1; -<<<<<<< HEAD -======= ->>>>>>> upstream/main jpeg_set_defaults(&cinfo); cinfo.restart_interval = rst_interval; From 17f0d7ae4eaafc11ba2fe673ff395bd8907e4ddb Mon Sep 17 00:00:00 2001 From: Guan-schoolmate <643665808@qq.com> Date: Fri, 27 Oct 2023 16:39:24 +0800 Subject: [PATCH 10/10] Conflict resolution --- frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h | 6 ------ tests/Interface/core/ImageContainerTest.cpp | 3 --- 2 files changed, 9 deletions(-) diff --git a/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h b/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h index a1dfb412b5..1b39d35777 100644 --- a/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h +++ b/frontend/Interfaces/buddy/DIP/imgcodecs/loadsave.h @@ -194,7 +194,6 @@ Img imread(const String &filename, int flags) { Img Image(sizes); JpegDecoderPtr->readData(Image); return Image; -<<<<<<< HEAD } // Converts a pointer to BmpDecoder @@ -222,8 +221,6 @@ Img imread(const String &filename, int flags) { Img Image(sizes); PngDecoderPtr->readData(Image); return Image; -======= ->>>>>>> upstream/main } } } @@ -296,7 +293,6 @@ static bool imwrite(const String &filename, Img &img_vec) { return code; } -<<<<<<< HEAD // Convert to a pointer of PngEncoder PngEncoder *pngEncoderPtr = dynamic_cast *>(encoder.get()); @@ -307,8 +303,6 @@ static bool imwrite(const String &filename, Img &img_vec) { code = pngEncoderPtr->write(img_vec, params); return code; } -======= ->>>>>>> upstream/main } return true; diff --git a/tests/Interface/core/ImageContainerTest.cpp b/tests/Interface/core/ImageContainerTest.cpp index 3069f19a9f..442f79ca6c 100644 --- a/tests/Interface/core/ImageContainerTest.cpp +++ b/tests/Interface/core/ImageContainerTest.cpp @@ -233,7 +233,6 @@ int main() { // CHECK: 240.0 fprintf(stderr, "%f\n", testBracketOperator4[15]); -<<<<<<< HEAD //===--------------------------------------------------------------------===// // Test png format image. @@ -334,7 +333,5 @@ int main() { // CHECK: 240.0 fprintf(stderr, "%f\n", testBracketOperator6[15]); -======= ->>>>>>> upstream/main return 0; } \ No newline at end of file