Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <opencv2/core/core.hpp>
00037 #include <opencv2/highgui/highgui.hpp>
00038
00039 #include <iostream>
00040
00041 #include <photo/photo_image.hpp>
00042
00043 photo_image::photo_image( void ) :
00044 width_(0),
00045 height_(0),
00046 bytes_per_pixel_(3),
00047 image_size_(0),
00048 data_(NULL)
00049 {
00050 }
00051
00052 photo_image::~photo_image( void )
00053 {
00054 delete[] data_;
00055 }
00056
00057
00058 int photo_image::getWidth( void )
00059 {
00060 return width_;
00061 }
00062
00063 int photo_image::getHeight( void )
00064 {
00065 return height_;
00066 }
00067
00068 size_t photo_image::getBytesPerPixel( void )
00069 {
00070 return bytes_per_pixel_;
00071 }
00072
00073 size_t photo_image::getImageSize( void )
00074 {
00075 return image_size_;
00076 }
00077
00078 char* photo_image::getDataAddress( void )
00079 {
00080 return data_;
00081 }
00082
00083
00084 void photo_image::photo_image_set_size( int image_width, int image_height, size_t image_bytes_per_pixel )
00085 {
00086 delete[] data_;
00087
00088 width_ = image_width;
00089 height_ = image_height;
00090 bytes_per_pixel_ = image_bytes_per_pixel;
00091 image_size_ = width_ * height_ * bytes_per_pixel_;
00092
00093 data_ = new char[image_size_]();
00094 }
00095
00096
00097 bool photo_image::photo_image_read( std::string filename )
00098 {
00099 int r, c;
00100
00101 cv::Mat img = cv::imread( filename.c_str() );
00102 if( img.empty() )
00103 {
00104 std::cerr << "img.empty() == true" << std::endl;
00105 return false;
00106 }
00107
00108 int w = img.cols;
00109 int h = img.rows;
00110
00111
00112
00113 int d = img.elemSize();
00114
00115
00116 if( width_ != w || height_ != h )
00117 {
00118
00119 photo_image_set_size( w, h, d );
00120 }
00121
00122 size_t n = 0;
00123 for( r = 0; r < height_; ++r )
00124 {
00125 for( c = 0; c < width_; ++c )
00126 {
00127
00128 const cv::Vec3b& pixel = img.at<cv::Vec3b>(r, c);
00129
00130 uint8_t R = pixel[2];
00131 uint8_t G = pixel[1];
00132 uint8_t B = pixel[0];
00133
00134 data_[n++] = R;
00135 data_[n++] = G;
00136 data_[n++] = B;
00137 }
00138 }
00139 return true;
00140 }
00141
00142
00143 bool photo_image::photo_image_write( std::string filename )
00144 {
00145 int r, c;
00146
00147
00148 cv::Mat img (height_, width_, CV_8UC3);
00149
00150 int n = 0;
00151 for( r = 0; r < height_; ++r )
00152 {
00153 for( c = 0; c < width_; ++c )
00154 {
00155 img.at<unsigned char> (r, 3*c+2) = data_[n++];
00156 img.at<unsigned char> (r, 3*c+1) = data_[n++];
00157 img.at<unsigned char> (r, 3*c+0) = data_[n++];
00158 }
00159 }
00160 cv::imwrite( filename.c_str(), img );
00161 return true;
00162 }