9 #include "opencv2/photo.hpp"    18 map<int,Ptr<TonemapDurand> > obj_;
    25 Ptr<TonemapDurand> create_TonemapDurand(
    26     vector<MxArray>::const_iterator first,
    27     vector<MxArray>::const_iterator last)
    29     ptrdiff_t len = std::distance(first, last);
    32     float contrast = 4.0f;
    33     float saturation = 1.0f;
    34     float sigma_space = 2.0f;
    35     float sigma_color = 2.0f;
    36     for (; first != last; first += 2) {
    37         string key(first->toString());
    38         const MxArray& val = *(first + 1);
    41         else if (key == 
"Contrast")
    42             contrast = val.toFloat();
    43         else if (key == 
"Saturation")
    44             saturation = val.toFloat();
    45         else if (key == 
"SigmaSpace")
    46             sigma_space = val.toFloat();
    47         else if (key == 
"SigmaColor")
    48             sigma_color = val.toFloat();
    50             mexErrMsgIdAndTxt(
"mexopencv:error",
    51                 "Unrecognized option %s", key.c_str());
    53     return createTonemapDurand(gamma, contrast, saturation, sigma_space, sigma_color);
    64 void mexFunction(
int nlhs, mxArray *plhs[], 
int nrhs, 
const mxArray *prhs[])
    70     vector<MxArray> rhs(prhs, prhs+nrhs);
    71     int id = rhs[0].toInt();
    72     string method(rhs[1].toString());
    75     if (method == 
"new") {
    77         obj_[++last_id] = create_TonemapDurand(
    78             rhs.begin() + 2, rhs.end());
    84     Ptr<TonemapDurand> obj = obj_[id];
    85     if (method == 
"delete") {
    89     else if (method == 
"clear") {
    93     else if (method == 
"load") {
    94         nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
    96         bool loadFromString = 
false;
    97         for (
int i=3; i<nrhs; i+=2) {
    98             string key(rhs[i].toString());
   100                 objname = rhs[i+1].toString();
   101             else if (key == 
"FromString")
   102                 loadFromString = rhs[i+1].toBool();
   104                 mexErrMsgIdAndTxt(
"mexopencv:error",
   105                     "Unrecognized option %s", key.c_str());
   114         FileStorage fs(rhs[2].toString(), FileStorage::READ +
   115             (loadFromString ? FileStorage::MEMORY : 0));
   116         obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
   118             mexErrMsgIdAndTxt(
"mexopencv:error", 
"Failed to load algorithm");
   121     else if (method == 
"save") {
   123         obj->save(rhs[2].toString());
   125     else if (method == 
"empty") {
   127         plhs[0] = 
MxArray(obj->empty());
   129     else if (method == 
"getDefaultName") {
   131         plhs[0] = 
MxArray(obj->getDefaultName());
   133     else if (method == 
"process") {
   135         Mat src(rhs[2].toMat(CV_32F)), dst;
   136         obj->process(src, dst);
   139     else if (method == 
"get") {
   141         string prop(rhs[2].toString());
   143             plhs[0] = 
MxArray(obj->getGamma());
   144         else if (prop == 
"Contrast")
   145             plhs[0] = 
MxArray(obj->getContrast());
   146         else if (prop == 
"Saturation")
   147             plhs[0] = 
MxArray(obj->getSaturation());
   148         else if (prop == 
"SigmaSpace")
   149             plhs[0] = 
MxArray(obj->getSigmaSpace());
   150         else if (prop == 
"SigmaColor")
   151             plhs[0] = 
MxArray(obj->getSigmaColor());
   153             mexErrMsgIdAndTxt(
"mexopencv:error",
   154                 "Unrecognized property %s", prop.c_str());
   156     else if (method == 
"set") {
   158         string prop(rhs[2].toString());
   160             obj->setGamma(rhs[3].toFloat());
   161         else if (prop == 
"Contrast")
   162             obj->setContrast(rhs[3].toFloat());
   163         else if (prop == 
"Saturation")
   164             obj->setSaturation(rhs[3].toFloat());
   165         else if (prop == 
"SigmaSpace")
   166             obj->setSigmaSpace(rhs[3].toFloat());
   167         else if (prop == 
"SigmaColor")
   168             obj->setSigmaColor(rhs[3].toFloat());
   170             mexErrMsgIdAndTxt(
"mexopencv:error",
   171                 "Unrecognized property %s", prop.c_str());
   174         mexErrMsgIdAndTxt(
"mexopencv:error",
   175             "Unrecognized operation %s", method.c_str());
 
float toFloat() const 
Convert MxArray to float. 
 
mxArray object wrapper for data conversion and manipulation. 
 
void nargchk(bool cond)
Alias for input/ouput arguments number check. 
 
Global constant definitions. 
 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.