1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "resize_computer.h"
17 #include "ic_retcode.h"
18
19 namespace IC {
20 const int RESIZE_CONSTANT = 2;
21
ResizeComputer(const PicInfo & pInfo)22 ResizeComputer::ResizeComputer(const PicInfo &pInfo)
23 {
24 // Initialize parameter for resize computer
25 sw_ = pInfo.widthSrc - 1;
26 sh_ = pInfo.heightSrc - 1;
27 dw_ = pInfo.widthDest - 1;
28 dh_ = pInfo.heightDest - 1;
29 B_ = 0;
30 N_ = 0;
31 x_ = 0;
32 y_ = 0;
33 pLinePrev_ = nullptr;
34 pLineNext_ = nullptr;
35 pA_ = nullptr;
36 pB_ = nullptr;
37 pC_ = nullptr;
38 pD_ = nullptr;
39 }
40
Compute(uint8_t * pDest,uint8_t * src,int pDestSize,int srcSize)41 int ResizeComputer::Compute(uint8_t *pDest, uint8_t *src, int pDestSize, int srcSize)
42 {
43 if (pDest == nullptr || src == nullptr || pDestSize <= 0 || srcSize <= 0) {
44 printf("ResizeComputer::Compute input is nullptr.\n");
45 return IC_RETCODE_FAILURE;
46 }
47 uint8_t *tmp = nullptr;
48
49 // This is linear stretch for picture resize
50 for (int i = 0; i <= dh_; ++i) {
51 tmp = pDest + i * (dw_ + 1) * NUM_CHANNELS;
52 y_ = i * sh_ / dh_;
53 N_ = dh_ - i * sh_ % dh_;
54 pLinePrev_ = src + y_ * (sw_ + 1) * NUM_CHANNELS;
55 y_++;
56 pLineNext_ = (N_ == dh_) ? pLinePrev_ : (src + y_ * (sw_ + 1) * NUM_CHANNELS);
57 for (int j = 0; j <= dw_; ++j) {
58 x_ = j * sw_ / dw_ * NUM_CHANNELS;
59 B_ = dw_ - j * sw_ % dw_;
60 pA_ = pLinePrev_ + x_;
61 pB_ = pA_ + NUM_CHANNELS;
62 pC_ = pLineNext_ + x_;
63 pD_ = pC_ + NUM_CHANNELS;
64 if (B_ == dw_) {
65 pB_ = pA_;
66 pD_ = pC_;
67 }
68 for (int k = 0; k < NUM_CHANNELS; ++k, ++tmp, ++pA_, ++pB_, ++pC_, ++pD_) {
69 *tmp = static_cast<uint8_t>(
70 (B_ * N_ * (*pA_ - *pB_ - *pC_ + *pD_) + dw_ * N_ * (*pB_) +
71 dh_ * B_ * (*pC_) + (dw_ * dh_ - dh_ * B_ - dw_ * N_) * (*pD_) +
72 dw_ * dh_ / RESIZE_CONSTANT) / (dw_ * dh_));
73 }
74 }
75 }
76 return IC_RETCODE_SUCCESS;
77 }
78 } // namespace IC