mexopencv  0.1
mex interface for opencv library
imencode.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
14 const ConstMap<string,int> PngStrategyMap = ConstMap<string,int>
15  ("Default", cv::IMWRITE_PNG_STRATEGY_DEFAULT)
16  ("Filtered", cv::IMWRITE_PNG_STRATEGY_FILTERED)
17  ("HuffmanOnly", cv::IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY)
18  ("RLE", cv::IMWRITE_PNG_STRATEGY_RLE)
19  ("Fixed", cv::IMWRITE_PNG_STRATEGY_FIXED);
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36 
37  // Option processing
38  vector<int> params;
39  bool flip = true;
40  for (int i=2; i<nrhs; i+=2) {
41  string key(rhs[i].toString());
42  if (key == "JpegQuality") {
43  int val = rhs[i+1].toInt();
44  if (val < 0 || 100 < val)
45  mexErrMsgIdAndTxt("mexopencv:error",
46  "JPEG quality parameter must be in the range [0,100]");
47  params.push_back(cv::IMWRITE_JPEG_QUALITY);
48  params.push_back(val);
49  }
50  else if (key == "JpegProgressive") {
51  params.push_back(cv::IMWRITE_JPEG_PROGRESSIVE);
52  params.push_back(rhs[i+1].toBool() ? 1 : 0);
53  }
54  else if (key == "JpegOptimize") {
55  params.push_back(cv::IMWRITE_JPEG_OPTIMIZE);
56  params.push_back(rhs[i+1].toBool() ? 1 : 0);
57  }
58  else if (key == "JpegResetInterval") {
59  int val = rhs[i+1].toInt();
60  if (val < 0 || 65535 < val)
61  mexErrMsgIdAndTxt("mexopencv:error",
62  "JPEG restart interval must be in the range [0,65535]");
63  params.push_back(cv::IMWRITE_JPEG_RST_INTERVAL);
64  params.push_back(val);
65  }
66  else if (key == "JpegLumaQuality") {
67  int val = rhs[i+1].toInt();
68  if (val < 0 || 100 < val)
69  mexErrMsgIdAndTxt("mexopencv:error",
70  "JPEG luma quality level must be in the range [0,100]");
71  params.push_back(cv::IMWRITE_JPEG_LUMA_QUALITY);
72  params.push_back(val);
73  }
74  else if (key == "JpegChromaQuality") {
75  int val = rhs[i+1].toInt();
76  if (val < 0 || 100 < val)
77  mexErrMsgIdAndTxt("mexopencv:error",
78  "JPEG chroma quality level must be in the range [0,100]");
79  params.push_back(cv::IMWRITE_JPEG_CHROMA_QUALITY);
80  params.push_back(val);
81  }
82  else if (key == "PngCompression") {
83  int val = rhs[i+1].toInt();
84  if (val < 0 || 9 < val)
85  mexErrMsgIdAndTxt("mexopencv:error",
86  "PNG compression level must be in the range [0,9]");
87  params.push_back(cv::IMWRITE_PNG_COMPRESSION);
88  params.push_back(val);
89  }
90  else if (key == "PngStrategy") {
91  params.push_back(cv::IMWRITE_PNG_STRATEGY);
92  params.push_back(PngStrategyMap[rhs[i+1].toString()]);
93  }
94  else if (key == "PngBilevel") {
95  params.push_back(cv::IMWRITE_PNG_BILEVEL);
96  params.push_back(rhs[i+1].toBool() ? 1 : 0);
97  }
98  else if (key == "PxmBinary") {
99  params.push_back(cv::IMWRITE_PXM_BINARY);
100  params.push_back(rhs[i+1].toBool() ? 1 : 0);
101  }
102  else if (key == "WebpQuality") {
103  int val = rhs[i+1].toInt();
104  if (val < 1 /*|| 100 < val*/)
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "WEBP quality must be in the range [0,100]");
107  params.push_back(cv::IMWRITE_WEBP_QUALITY);
108  params.push_back(val);
109  }
110  else if (key == "Params") {
111  // append to parameters by directly passing a vector of integers
112  vector<int> pvec = rhs[i+1].toVector<int>();
113  if ((pvec.size()%2) != 0)
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Params vectors must contain pairs of id/value.");
116  params.insert(params.end(), pvec.begin(), pvec.end());
117  }
118  else if (key == "FlipChannels")
119  flip = rhs[i+1].toBool();
120  else
121  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
122  }
123 
124  // Process
125  string ext(rhs[0].toString());
126  Mat img(rhs[1].toMat(rhs[1].isFloat() ? CV_32F :
127  (rhs[1].isUint16() ? CV_16U : CV_8U)));
128  if (flip && (img.channels() == 3 || img.channels() == 4)) {
129  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
130  cvtColor(img, img, (img.channels()==3 ?
131  cv::COLOR_RGB2BGR : cv::COLOR_RGBA2BGRA));
132  }
133  vector<uchar> buf;
134  bool success = imencode(ext, img, buf, params);
135  if (!success)
136  mexErrMsgIdAndTxt("mexopencv:error", "imencode failed");
137  plhs[0] = MxArray(Mat(buf), mxUINT8_CLASS, false);
138 }
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: imencode.cpp:29
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927