1 /**
2 * Copyright 2020-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 #include <algorithm>
17
18 #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_resize_jpeg_op.h"
22 #endif
23
24 #include "minddata/dataset/kernels/ir/validators.h"
25
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // SoftDvppDecodeResizeJpegOperation
SoftDvppDecodeResizeJpegOperation(const std::vector<int32_t> & size)31 SoftDvppDecodeResizeJpegOperation::SoftDvppDecodeResizeJpegOperation(const std::vector<int32_t> &size) : size_(size) {}
32
33 SoftDvppDecodeResizeJpegOperation::~SoftDvppDecodeResizeJpegOperation() = default;
34
Name() const35 std::string SoftDvppDecodeResizeJpegOperation::Name() const { return kSoftDvppDecodeResizeJpegOperation; }
36
ValidateParams()37 Status SoftDvppDecodeResizeJpegOperation::ValidateParams() {
38 RETURN_IF_NOT_OK(ValidateVectorSize("SoftDvppDecodeResizeJpeg", size_));
39 constexpr int32_t value_one = 1;
40 constexpr int32_t value_two = 2;
41 for (size_t i = 0; i < size_.size(); i++) {
42 if (size_[i] % value_two == value_one) {
43 std::string err_msg = "SoftDvppDecodeResizeJpeg: size[" + std::to_string(i) +
44 "] must be even values, got: " + std::to_string(size_[i]);
45 MS_LOG(ERROR) << err_msg;
46 RETURN_STATUS_SYNTAX_ERROR(err_msg);
47 }
48 }
49 return Status::OK();
50 }
51
Build()52 std::shared_ptr<TensorOp> SoftDvppDecodeResizeJpegOperation::Build() {
53 constexpr size_t dimension_zero = 0;
54 constexpr size_t dimension_one = 1;
55 constexpr size_t size_two = 2;
56
57 // If size is a single value, the smaller edge of the image will be
58 // resized to this value with the same image aspect ratio.
59 int32_t height = size_[dimension_zero];
60 int32_t width = 0;
61
62 // User specified the width value.
63 if (size_.size() == size_two) {
64 width = size_[dimension_one];
65 }
66 std::shared_ptr<SoftDvppDecodeResizeJpegOp> tensor_op = std::make_shared<SoftDvppDecodeResizeJpegOp>(height, width);
67 return tensor_op;
68 }
69
to_json(nlohmann::json * out_json)70 Status SoftDvppDecodeResizeJpegOperation::to_json(nlohmann::json *out_json) {
71 (*out_json)["size"] = size_;
72 return Status::OK();
73 }
74
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)75 Status SoftDvppDecodeResizeJpegOperation::from_json(nlohmann::json op_params,
76 std::shared_ptr<TensorOperation> *operation) {
77 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
78 std::vector<int32_t> size = op_params["size"];
79 *operation = std::make_shared<vision::SoftDvppDecodeResizeJpegOperation>(size);
80 return Status::OK();
81 }
82
83 #endif
84 } // namespace vision
85 } // namespace dataset
86 } // namespace mindspore
87