OSDN Git Service

add JPG and TIFF Support via OpenCV for Image::load and Image::save
[psychlops/cpp.git] / psychlops / core / graphic / psychlops_g_image.cpp
index ff09c4c..d003429 100644 (file)
@@ -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);\r
+               IMAGE_FORMATS::FILE_EXT ext = IMAGE_FORMATS::getImageFileFormatFromExt(file_extension);\r
+               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);\r
+               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 {\r
+\r
+       FILE_EXT getImageFileFormatFromExt(const std::string &s) {\r
+               std::string ext = s;\r
+               std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);\r
+               if(ext=="png") {\r
+                       return PNG;\r
+               } else if(ext=="jpg") {\r
+                       return JPG;\r
+               } else if(ext=="jpeg") {\r
+                       return JPG;\r
+               } else if(ext=="jp2") {\r
+                       return JP2;\r
+               } else if(ext=="tiff") {\r
+                       return TIFF;\r
+               } else if(ext=="cvmat") {\r
+                       return CVMAT_TXT;\r
+               } else if(ext=="mat") {\r
+                       return MATLAB_MAT;\r
+               } else {\r
+                       return UNKNOWN;\r
+               }\r
+       }\r
+\r
 
-
-namespace IMAGE_FORMATS {
        IMAGE_FORMAT::~IMAGE_FORMAT() {
        }
        void IMAGE_FORMAT::readTargetMemoryAlignment(Image *target) {