1 /** 2 * Copyright 2019-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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_LITE_IMAGE_UTILS_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_LITE_IMAGE_UTILS_H_ 18 19 #include <csetjmp> 20 #include <memory> 21 #include <random> 22 #include <string> 23 #include <vector> 24 25 #if defined(_WIN32) || defined(_WIN64) 26 #undef HAVE_STDDEF_H 27 #undef HAVE_STDLIB_H 28 #endif 29 30 #include "./jpeglib.h" 31 #include "./jerror.h" 32 #include "minddata/dataset/core/tensor.h" 33 #include "minddata/dataset/kernels/image/lite_cv/image_process.h" 34 #include "minddata/dataset/kernels/tensor_op.h" 35 #include "minddata/dataset/util/status.h" 36 37 #define MAX_PIXEL_VALUE 255 38 39 namespace mindspore { 40 namespace dataset { 41 void JpegErrorExitCustom(j_common_ptr cinfo); 42 43 struct JpegErrorManagerCustom { 44 // "public" fields 45 struct jpeg_error_mgr pub; 46 // for return to caller 47 jmp_buf setjmp_buffer; 48 }; 49 50 bool IsNonEmptyJPEG(const std::shared_ptr<Tensor> &input); 51 52 void JpegSetSource(j_decompress_ptr c_info, const void *data, int64_t data_size); 53 54 Status JpegCropAndDecode(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int x = 0, int y = 0, 55 int w = 0, int h = 0); 56 57 /// \brief Returns cropped ROI of an image 58 /// \param[in] input: Tensor of shape <H,W,C> or <H,W> and any OpenCv compatible type, see CVTensor. 59 /// \param[in] x Starting horizontal position of ROI 60 /// \param[in] y Starting vertical position of ROI 61 /// \param[in] w Width of the ROI 62 /// \param[in] h Height of the ROI 63 /// \param[out] output: Cropped image Tensor of shape <h,w,C> or <h,w> and same input type. 64 Status Crop(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int x, int y, int w, int h); 65 66 /// \brief Returns Decoded image 67 /// Supported images: 68 /// BMP JPEG JPG PNG TIFF 69 /// supported by opencv, if user need more image analysis capabilities, please compile opencv particularlly. 70 /// \param[in] input CVTensor containing the not decoded image 1D bytes 71 /// \param[out] output Decoded image Tensor of shape <H,W,C> and type DE_UINT8. Pixel order is RGB 72 Status Decode(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output); 73 74 /// \brief Get jpeg image width and height 75 /// \param[in] input CVTensor containing the not decoded image 1D bytes 76 /// \param[in] img_width The jpeg image width 77 /// \param[in] img_height The jpeg image height 78 Status GetJpegImageInfo(const std::shared_ptr<Tensor> &input, int *img_width, int *img_height); 79 80 /// \brief Returns Normalized image 81 /// \param[in] input Tensor of shape <H,W,C> in RGB order and any OpenCv compatible type, see CVTensor. 82 /// \param[in] mean Tensor of shape <3> and type DE_FLOAT32 which are mean of each channel in RGB order 83 /// \param[in] std Tensor of shape <3> and type DE_FLOAT32 which are std of each channel in RGB order 84 /// \param[out] output Normalized image Tensor of same input shape and type DE_FLOAT32 85 Status Normalize(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, std::vector<float> vec_mean, 86 std::vector<float> vec_std); 87 88 /// \brief Returns Resized image. 89 /// \param[in] input 90 /// \param[in] output_height Height of output 91 /// \param[in] output_width Width of output 92 /// \param[in] fx Horizontal scale 93 /// \param[in] fy Vertical scale 94 /// \param[in] InterpolationMode The interpolation mode 95 /// \param[out] output Resized image of shape <outputHeight,outputWidth,C> or <outputHeight,outputWidth> 96 /// and same type as input 97 Status Resize(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t output_height, 98 int32_t output_width, double fx = 0.0, double fy = 0.0, 99 InterpolationMode mode = InterpolationMode::kLinear); 100 101 /// \brief Returns Resized image. 102 /// \param[in] inputs input TensorRow 103 /// \param[in] height Height of output 104 /// \param[in] width Width of output 105 /// \param[in] img_orientation Angle method of image rotation 106 /// \param[out] outputs Resized image of shape <height,width,C> and same type as input 107 Status ResizePreserve(const TensorRow &inputs, int32_t height, int32_t width, int32_t img_orientation, 108 TensorRow *outputs); 109 110 /// \brief Take in a 3 channel image in RBG to BGR 111 /// \param[in] input The input image 112 /// \param[out] output The output image 113 /// \return Status code 114 Status RgbToBgr(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output); 115 116 /// \brief Take in a 3 channel image in RBG to GRAY 117 /// \param[in] input The input image 118 /// \param[out] output The output image 119 /// \return Status code 120 Status RgbToGray(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output); 121 122 /// \brief Pads the input image and puts the padded image in the output 123 /// \param[in] input: input Tensor 124 /// \param[out] output: padded Tensor 125 /// \param[in] pad_top Amount of padding done in top 126 /// \param[in] pad_bottom Amount of padding done in bottom 127 /// \param[in] pad_left Amount of padding done in left 128 /// \param[in] pad_right Amount of padding done in right 129 /// \param[in] border_types The interpolation to be done in the border 130 /// \param[in] fill_r Red fill value for pad 131 /// \param[in] fill_g Green fill value for pad 132 /// \param[in] fill_b Blue fill value for pad 133 Status Pad(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, const int32_t &pad_top, 134 const int32_t &pad_bottom, const int32_t &pad_left, const int32_t &pad_right, const BorderType &border_types, 135 uint8_t fill_r = 0, uint8_t fill_g = 0, uint8_t fill_b = 0); 136 137 /// \brief Rotate the input image by orientation 138 /// \param[in] input Input Tensor 139 /// \param[out] output Rotated Tensor 140 /// \param[in] orientation The orientation of EXIF 141 Status Rotate(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, const uint64_t orientation); 142 143 /// \brief Geometrically transform the input image 144 /// \param[in] input Input Tensor 145 /// \param[out] output Transformed Tensor 146 /// \param[in] mat The transformation matrix 147 /// \param[in] interpolation The interpolation mode, support only bilinear for now 148 /// \param[in] fill_r Red fill value for pad 149 /// \param[in] fill_g Green fill value for pad 150 /// \param[in] fill_b Blue fill value for pad 151 Status Affine(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, const std::vector<float_t> &mat, 152 InterpolationMode interpolation, uint8_t fill_r = 0, uint8_t fill_g = 0, uint8_t fill_b = 0); 153 154 /// \brief Filter the input image with a Gaussian kernel 155 /// \param[in] input Input Tensor 156 /// \param[out] output Transformed Tensor 157 /// \param[in] kernel_size_x Gaussian kernel size of width 158 /// \param[in] kernel_size_y Gaussian kernel size of height 159 /// \param[in] sigma_x Gaussian kernel standard deviation of width 160 /// \param[in] sigma_y Gaussian kernel standard deviation of height 161 Status GaussianBlur(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t kernel_size_x, 162 int32_t kernel_size_y, float sigma_x, float sigma_y); 163 164 /// \brief Validate image rank. 165 /// \param[in] op_name operator name. 166 /// \param[in] rank refers to the rank of input image shape. 167 Status ValidateImageRank(const std::string &op_name, int32_t rank); 168 } // namespace dataset 169 } // namespace mindspore 170 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_LITE_IMAGE_UTILS_H_ 171