• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "tools/converter/preprocess/opencv_utils.h"
18 #include <vector>
19 #include "src/common/log_adapter.h"
20 #include "include/errorcode.h"
21 namespace mindspore {
22 namespace lite {
23 namespace preprocess {
ConvertColorConversionCodes(const std::string & format)24 cv::ColorConversionCodes ConvertColorConversionCodes(const std::string &format) {
25   if (format == "RGB") {
26     return cv::COLOR_BGR2RGB;
27   } else if (format == "GRAY") {
28     return cv::COLOR_BGR2GRAY;
29   } else {
30     MS_LOG(ERROR) << "Unsupported format:" << format;
31     return cv::COLOR_COLORCVT_MAX;
32   }
33 }
34 
ConvertColorConversionCodes(preprocess::ImageToFormat format)35 cv::ColorConversionCodes ConvertColorConversionCodes(preprocess::ImageToFormat format) {
36   if (format == RGB) {
37     return cv::COLOR_BGR2RGB;
38   } else if (format == GRAY) {
39     return cv::COLOR_BGR2GRAY;
40   } else {
41     MS_LOG(ERROR) << "Unsupported format:" << format;
42     return cv::COLOR_COLORCVT_MAX;
43   }
44 }
45 
ConvertResizeMethod(const std::string & method)46 cv::InterpolationFlags ConvertResizeMethod(const std::string &method) {
47   if (method == "NEAREST") {
48     return cv::INTER_NEAREST;
49   } else if (method == "LINEAR") {
50     return cv::INTER_LINEAR;
51   } else if (method == "CUBIC") {
52     return cv::INTER_CUBIC;
53   } else {
54     MS_LOG(ERROR) << "INPUT ILLEGAL: resize_method must be NEAREST|LINEAR|CUBIC.";
55     return cv::INTER_MAX;
56   }
57 }
58 
GetMatData(const cv::Mat & mat,void ** data,size_t * size)59 int GetMatData(const cv::Mat &mat, void **data, size_t *size) {
60   if (data == nullptr || size == nullptr) {
61     MS_LOG(ERROR) << "data or size is nullptr.";
62     return RET_NULL_PTR;
63   }
64   cv::Mat mat_local = mat;
65   // if the input Mat's memory is not continuous, copy it to one block of memory
66   if (!mat.isContinuous()) {
67     mat_local = mat.clone();
68   }
69   (*size) = 0;
70   for (int i = 0; i < mat.rows; ++i) {
71     (*size) += static_cast<size_t>(mat.cols) * mat.elemSize();
72   }
73 
74   (*data) = new char[*size];
75   if (memcpy_s(*data, *size, mat_local.data,
76                static_cast<size_t>(mat.rows * mat.cols * mat.channels()) * sizeof(float)) != EOK) {
77     MS_LOG(ERROR) << "memcpy failed.";
78     return RET_ERROR;
79   }
80   return RET_OK;
81 }
82 }  // namespace preprocess
83 }  // namespace lite
84 }  // namespace mindspore
85