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