• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 #include "minddata/dataset/kernels/image/decode_op.h"
17 
18 #ifndef ENABLE_ANDROID
19 #include "minddata/dataset/kernels/image/image_utils.h"
20 #else
21 #include "minddata/dataset/kernels/image/lite_image_utils.h"
22 #endif
23 #include "minddata/dataset/util/status.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 const bool DecodeOp::kDefRgbFormat = true;
28 
DecodeOp(bool rgb)29 DecodeOp::DecodeOp(bool rgb) : is_rgb_format_(rgb) {
30   if (is_rgb_format_) {  // RGB colour mode
31     MS_LOG(DEBUG) << "Decode colour mode is RGB.";
32   } else {
33     MS_LOG(DEBUG) << "Decode colour mode is BGR.";
34   }
35 }
36 
Compute(const std::shared_ptr<Tensor> & input,std::shared_ptr<Tensor> * output)37 Status DecodeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
38   IO_CHECK(input, output);
39   // check the input tensor shape
40   if (input->Rank() != 1) {
41     RETURN_STATUS_UNEXPECTED("Decode: invalid input shape, only support 1D input, got rank: " +
42                              std::to_string(input->Rank()));
43   }
44   if (is_rgb_format_) {  // RGB colour mode
45     return Decode(input, output);
46   } else {  // BGR colour mode
47     RETURN_STATUS_UNEXPECTED("Decode: only support Decoded into RGB image, check input parameter first.");
48   }
49 }
OutputShape(const std::vector<TensorShape> & inputs,std::vector<TensorShape> & outputs)50 Status DecodeOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
51   RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs));
52   outputs.clear();
53   TensorShape out({-1, -1, 3});  // we don't know what is output image size, but we know it should be 3 channels
54   if (inputs[0].Rank() == 1) outputs.emplace_back(out);
55   if (!outputs.empty()) return Status::OK();
56   return Status(
57     StatusCode::kMDUnexpectedError,
58     "Decode: invalid input shape, expected 1D input, but got input dimension is:" + std::to_string(inputs[0].Rank()));
59 }
60 
OutputType(const std::vector<DataType> & inputs,std::vector<DataType> & outputs)61 Status DecodeOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) {
62   RETURN_IF_NOT_OK(TensorOp::OutputType(inputs, outputs));
63   outputs[0] = DataType(DataType::DE_UINT8);
64   return Status::OK();
65 }
66 }  // namespace dataset
67 }  // namespace mindspore
68