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);
29 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
32 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
35 vector<MxArray> rhs(prhs, prhs+nrhs);
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);
50 else if (key ==
"JpegProgressive") {
51 params.push_back(cv::IMWRITE_JPEG_PROGRESSIVE);
52 params.push_back(rhs[i+1].toBool() ? 1 : 0);
54 else if (key ==
"JpegOptimize") {
55 params.push_back(cv::IMWRITE_JPEG_OPTIMIZE);
56 params.push_back(rhs[i+1].toBool() ? 1 : 0);
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);
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);
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);
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);
90 else if (key ==
"PngStrategy") {
91 params.push_back(cv::IMWRITE_PNG_STRATEGY);
92 params.push_back(PngStrategyMap[rhs[i+1].toString()]);
94 else if (key ==
"PngBilevel") {
95 params.push_back(cv::IMWRITE_PNG_BILEVEL);
96 params.push_back(rhs[i+1].toBool() ? 1 : 0);
98 else if (key ==
"PxmBinary") {
99 params.push_back(cv::IMWRITE_PXM_BINARY);
100 params.push_back(rhs[i+1].toBool() ? 1 : 0);
102 else if (key ==
"WebpQuality") {
103 int val = rhs[i+1].toInt();
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);
110 else if (key ==
"Params") {
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());
118 else if (key ==
"FlipChannels")
119 flip = rhs[i+1].toBool();
121 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
125 string filename(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)) {
130 cvtColor(img, img, (img.channels()==3 ?
131 cv::COLOR_RGB2BGR : cv::COLOR_RGBA2BGRA));
133 bool success = imwrite(filename, img, params);
135 mexErrMsgIdAndTxt(
"mexopencv:error",
"imwrite failed");
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.