From: hskwk Date: Thu, 26 Feb 2015 12:50:32 +0000 (+0900) Subject: add JPG and TIFF Support via OpenCV for Image::load and Image::save X-Git-Url: http://git.osdn.jp/view?a=commitdiff_plain;h=e1d4d7d10f87906b7b728a40e842938008be764e;p=psychlops%2Fcpp.git add JPG and TIFF Support via OpenCV for Image::load and Image::save --- diff --git a/psychlops/core/graphic/psychlops_g_image.cpp b/psychlops/core/graphic/psychlops_g_image.cpp index ff09c4c..d003429 100644 --- a/psychlops/core/graphic/psychlops_g_image.cpp +++ b/psychlops/core/graphic/psychlops_g_image.cpp @@ -27,9 +27,7 @@ #include "psychlops_g_image.h" #include "../../extension/FileFormat/PNG/psychlops_g_PNG_bridge.h" -#if 0 -#include "../../extension/FileFormat/JPEG/psychlops_g_JPEG_bridge.h" -#endif +#include "../../extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.h" namespace Psychlops { @@ -631,17 +629,20 @@ void Image::pix_ub_bits_mono_(int ix, int iy, double lum) { } std::string::size_type extention_index = filename.find_last_of('.'); if(extention_index==std::string::npos) throw Exception(typeid(*this), "File Type Error", "Specified image type is not supported.*.."); - std::string file_extension = filename.substr(extention_index+1); - if(file_extension=="png") { + std::string file_extension = filename.substr(extention_index+1); + IMAGE_FORMATS::FILE_EXT ext = IMAGE_FORMATS::getImageFileFormatFromExt(file_extension); + if(ext==IMAGE_FORMATS::PNG) { loader = new IMAGE_FORMATS::PNG_BRIDGE; loader->load(File::decodePath(filename).c_str(), this); delete loader; -#if 0 - } else if(file_extension=="jpg") { - loader = new IMAGE_FORMATS::JPEG_BRIDGE; + } else if(ext==IMAGE_FORMATS::JPG) { + loader = new IMAGE_FORMATS::OPENCV_BRIDGE; + loader->load(File::decodePath(filename).c_str(), this); + delete loader; + } else if(ext==IMAGE_FORMATS::TIFF) { + loader = new IMAGE_FORMATS::OPENCV_BRIDGE; loader->load(File::decodePath(filename).c_str(), this); delete loader; -#endif } else { throw Exception(typeid(*this), "File Type Error", "Specified image type is not supported."); } @@ -689,11 +690,20 @@ void Image::pix_ub_bits_mono_(int ix, int iy, double lum) { std::string::size_type extention_index = filename.find_last_of('.'); if(extention_index==std::string::npos) throw Exception(typeid(*this), "File Type Error", "Specified image type is not supported."); - std::string file_extension = filename.substr(extention_index+1); - if(file_extension=="png") { + std::string file_extension = filename.substr(extention_index+1); + IMAGE_FORMATS::FILE_EXT ext = IMAGE_FORMATS::getImageFileFormatFromExt(file_extension); + if(ext==IMAGE_FORMATS::PNG) { loader = new IMAGE_FORMATS::PNG_BRIDGE; loader->save(File::decodePath(filename).c_str(), tmp); delete loader; + } else if(ext==IMAGE_FORMATS::JPG) { + loader = new IMAGE_FORMATS::OPENCV_BRIDGE; + loader->save(File::decodePath(filename).c_str(), tmp); + delete loader; + } else if(ext==IMAGE_FORMATS::TIFF) { + loader = new IMAGE_FORMATS::OPENCV_BRIDGE; + loader->save(File::decodePath(filename).c_str(), tmp); + delete loader; } else { throw Exception(typeid(*this), "File Type Error", "Specified image type is not supported."); } @@ -889,9 +899,31 @@ void Image::pix_ub_bits_mono_(int ix, int iy, double lum) { return *this; } +namespace IMAGE_FORMATS { + + FILE_EXT getImageFileFormatFromExt(const std::string &s) { + std::string ext = s; + std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); + if(ext=="png") { + return PNG; + } else if(ext=="jpg") { + return JPG; + } else if(ext=="jpeg") { + return JPG; + } else if(ext=="jp2") { + return JP2; + } else if(ext=="tiff") { + return TIFF; + } else if(ext=="cvmat") { + return CVMAT_TXT; + } else if(ext=="mat") { + return MATLAB_MAT; + } else { + return UNKNOWN; + } + } + - -namespace IMAGE_FORMATS { IMAGE_FORMAT::~IMAGE_FORMAT() { } void IMAGE_FORMAT::readTargetMemoryAlignment(Image *target) { diff --git a/psychlops/core/graphic/psychlops_g_image.h b/psychlops/core/graphic/psychlops_g_image.h index 6289e84..eca5402 100644 --- a/psychlops/core/graphic/psychlops_g_image.h +++ b/psychlops/core/graphic/psychlops_g_image.h @@ -24,7 +24,10 @@ namespace cv{ class Mat; } namespace Psychlops { -namespace IMAGE_FORMATS { +namespace IMAGE_FORMATS { + enum FILE_EXT { UNKNOWN, PNG, JPG, JP2, TIFF, CVMAT_TXT, MATLAB_MAT }; + static FILE_EXT getImageFileFormatFromExt(const std::string &s); + class IMAGE_FORMAT { protected: float * target_bitmap_f_; diff --git a/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.cpp b/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.cpp new file mode 100644 index 0000000..215d321 --- /dev/null +++ b/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.cpp @@ -0,0 +1,42 @@ +/* + * psychlops_g_PNG_bridge.cpp + * Psychlops Standard Library (Universal) + * + * Last Modified 2006/08/22 by Kenchi HOSOKAWA + * (C) 2006- Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO + */ + +#include +#include +#include + +#include "../../../core/ApplicationInterfaces/psychlops_code_exception.h" +#include "../../../core/graphic/psychlops_g_image.h" +#include "psychlops_g_OPENCV_bridge.h" + + +namespace Psychlops { +namespace IMAGE_FORMATS { + + + OPENCV_BRIDGE::OPENCV_BRIDGE() { + } + OPENCV_BRIDGE::~OPENCV_BRIDGE() { + } + + + void OPENCV_BRIDGE::load(const char *file_name, Image * target) { + std::string file(file_name); + cv::Mat img = cv::imread(file, -1); + target->from(img); + } + void OPENCV_BRIDGE::save(const char *file_name, Image * target) { + std::string file(file_name); + cv::Mat img; + target->to(img); + cv::imwrite(file, img); + } + + +} +} diff --git a/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.h b/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.h new file mode 100644 index 0000000..e8f4b43 --- /dev/null +++ b/psychlops/extension/FileFormat/OpenCV/psychlops_g_OPENCV_bridge.h @@ -0,0 +1,39 @@ +/* + * psychlops_g_PNG_bridge.h + * Psychlops Standard Library (Universal) + * + * Last Modified 2006/08/22 by Kenchi HOSOKAWA + * (C) 2006- Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO + */ + +#ifndef HEADER_PSYCHLOPS_IMAGE_FORMATS_OPENCV +#define HEADER_PSYCHLOPS_IMAGE_FORMATS_OPENCV + +#ifndef __cplusplus +#define __cplusplus +#endif + +#include "../../../core/graphic/psychlops_g_image.h" + + +namespace Psychlops { +namespace IMAGE_FORMATS { + + + class OPENCV_BRIDGE : public IMAGE_FORMAT { + private: + + public: + OPENCV_BRIDGE(); + virtual ~OPENCV_BRIDGE(); + virtual void load(const char *file_name, Image * target); + virtual void save(const char *file_name, Image * target); + + private: + }; + + +} +} + +#endif diff --git a/win32gl/dev/psychlopswin32.cbp b/win32gl/dev/psychlopswin32.cbp index 4661b47..285fb6a 100644 --- a/win32gl/dev/psychlopswin32.cbp +++ b/win32gl/dev/psychlopswin32.cbp @@ -330,6 +330,8 @@ + + diff --git a/win32gl/test/Psychlops_win32cblibtest.cbp b/win32gl/test/Psychlops_win32cblibtest.cbp index 79f0dce..b0cb6ef 100644 --- a/win32gl/test/Psychlops_win32cblibtest.cbp +++ b/win32gl/test/Psychlops_win32cblibtest.cbp @@ -221,6 +221,10 @@ + +