paddle_image_preprocess.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "lite/utils/cv/paddle_image_preprocess.h"
#include <math.h>
#include <algorithm>
#include <climits>
#include "lite/utils/cv/image2tensor.h"
#include "lite/utils/cv/image_convert.h"
#include "lite/utils/cv/image_flip.h"
#include "lite/utils/cv/image_resize.h"
#include "lite/utils/cv/image_rotate.h"
namespace paddle {
namespace lite {
namespace utils {
namespace cv {
#define PI 3.14159265f
#define Degrees2Radians(degrees) ((degrees) * (SK_ScalarPI / 180))
#define Radians2Degrees(radians) ((radians) * (180 / SK_ScalarPI))
#define ScalarNearlyZero (1.0f / (1 << 12))
// init
33
__attribute__((visibility("default")))
34 35 36 37 38 39 40
ImagePreprocess::ImagePreprocess(ImageFormat srcFormat,
                                 ImageFormat dstFormat,
                                 TransParam param) {
  this->srcFormat_ = srcFormat;
  this->dstFormat_ = dstFormat;
  this->transParam_ = param;
}
41 42
__attribute__((visibility("default"))) void ImagePreprocess::imageConvert(
    const uint8_t* src, uint8_t* dst) {
43 44 45 46 47 48 49 50 51
  ImageConvert img_convert;
  img_convert.choose(src,
                     dst,
                     this->srcFormat_,
                     this->dstFormat_,
                     this->transParam_.iw,
                     this->transParam_.ih);
}

52 53 54 55 56
__attribute__((visibility("default"))) void ImagePreprocess::imageConvert(
    const uint8_t* src,
    uint8_t* dst,
    ImageFormat srcFormat,
    ImageFormat dstFormat) {
57 58 59 60 61 62 63 64 65
  ImageConvert img_convert;
  img_convert.choose(src,
                     dst,
                     srcFormat,
                     dstFormat,
                     this->transParam_.iw,
                     this->transParam_.ih);
}

66 67 68 69 70 71 72 73
__attribute__((visibility("default"))) void ImagePreprocess::imageResize(
    const uint8_t* src,
    uint8_t* dst,
    ImageFormat srcFormat,
    int srcw,
    int srch,
    int dstw,
    int dsth) {
H
HappyAngel 已提交
74 75
  ImageResize img_resize;
  img_resize.choose(src, dst, srcFormat, srcw, srch, dstw, dsth);
76 77
}

78 79
__attribute__((visibility("default"))) void ImagePreprocess::imageResize(
    const uint8_t* src, uint8_t* dst) {
80 81 82 83 84
  int srcw = this->transParam_.iw;
  int srch = this->transParam_.ih;
  int dstw = this->transParam_.ow;
  int dsth = this->transParam_.oh;
  auto srcFormat = this->dstFormat_;
H
HappyAngel 已提交
85 86
  ImageResize img_resize;
  img_resize.choose(src, dst, srcFormat, srcw, srch, dstw, dsth);
87 88
}

89 90 91 92 93 94 95
__attribute__((visibility("default"))) void ImagePreprocess::imageRotate(
    const uint8_t* src,
    uint8_t* dst,
    ImageFormat srcFormat,
    int srcw,
    int srch,
    float degree) {
H
HappyAngel 已提交
96 97
  ImageRotate img_rotate;
  img_rotate.choose(src, dst, srcFormat, srcw, srch, degree);
98 99
}

100 101
__attribute__((visibility("default"))) void ImagePreprocess::imageRotate(
    const uint8_t* src, uint8_t* dst) {
102 103 104 105
  auto srcw = this->transParam_.ow;
  auto srch = this->transParam_.oh;
  auto srcFormat = this->dstFormat_;
  auto degree = this->transParam_.rotate_param;
H
HappyAngel 已提交
106 107
  ImageRotate img_rotate;
  img_rotate.choose(src, dst, srcFormat, srcw, srch, degree);
108 109
}

110 111 112 113 114 115 116
__attribute__((visibility("default"))) void ImagePreprocess::imageFlip(
    const uint8_t* src,
    uint8_t* dst,
    ImageFormat srcFormat,
    int srcw,
    int srch,
    FlipParam flip_param) {
H
HappyAngel 已提交
117 118
  ImageFlip img_flip;
  img_flip.choose(src, dst, srcFormat, srcw, srch, flip_param);
119 120
}

121 122
__attribute__((visibility("default"))) void ImagePreprocess::imageFlip(
    const uint8_t* src, uint8_t* dst) {
123 124 125 126
  auto srcw = this->transParam_.ow;
  auto srch = this->transParam_.oh;
  auto srcFormat = this->dstFormat_;
  auto flip_param = this->transParam_.flip_param;
H
HappyAngel 已提交
127 128
  ImageFlip img_flip;
  img_flip.choose(src, dst, srcFormat, srcw, srch, flip_param);
129 130
}

131 132 133 134 135 136 137 138 139
__attribute__((visibility("default"))) void ImagePreprocess::image2Tensor(
    const uint8_t* src,
    Tensor* dstTensor,
    ImageFormat srcFormat,
    int srcw,
    int srch,
    LayoutType layout,
    float* means,
    float* scales) {
140 141 142 143 144
  Image2Tensor img2tensor;
  img2tensor.choose(
      src, dstTensor, srcFormat, layout, srcw, srch, means, scales);
}

145 146 147 148 149 150
__attribute__((visibility("default"))) void ImagePreprocess::image2Tensor(
    const uint8_t* src,
    Tensor* dstTensor,
    LayoutType layout,
    float* means,
    float* scales) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  Image2Tensor img2tensor;
  img2tensor.choose(src,
                    dstTensor,
                    this->dstFormat_,
                    layout,
                    this->transParam_.ow,
                    this->transParam_.oh,
                    means,
                    scales);
}

}  // namespace cv
}  // namespace utils
}  // namespace lite
}  // namespace paddle