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 "scan_line_filter.h"
17 #include "image_log.h"
18 #include "image_utils.h"
19 #include "media_errors.h"
20 #ifndef _WIN32
21 #include "securec.h"
22 #else
23 #include "memory.h"
24 #endif
25
26 namespace OHOS {
27 namespace Media {
ScanlineFilter(const PixelFormat & srcPixelFormat)28 ScanlineFilter::ScanlineFilter(const PixelFormat &srcPixelFormat) : srcBpp_(ImageUtils::GetPixelBytes(srcPixelFormat))
29 {}
30
SetSrcPixelFormat(const PixelFormat & srcPixelFormat)31 void ScanlineFilter::SetSrcPixelFormat(const PixelFormat &srcPixelFormat)
32 {
33 srcBpp_ = ImageUtils::GetPixelBytes(srcPixelFormat);
34 }
35
GetFilterRowType(const int32_t rowNum)36 FilterRowType ScanlineFilter::GetFilterRowType(const int32_t rowNum)
37 {
38 if (rowNum < srcRegion_.top || (rowNum - srcRegion_.top) > srcRegion_.height) {
39 return FilterRowType::NON_REFERENCE_ROW;
40 }
41
42 if ((rowNum - srcRegion_.top) == srcRegion_.height) {
43 return FilterRowType::LAST_REFERENCE_ROW;
44 }
45
46 return FilterRowType::NORMAL_REFERENCE_ROW;
47 }
48
SetSrcRegion(const Rect & region)49 void ScanlineFilter::SetSrcRegion(const Rect ®ion)
50 {
51 srcRegion_ = region;
52 }
53
54 // outer need judgement the src and dst imageInfo pixelFormat and alphaType
SetPixelConvert(const ImageInfo & srcImageInfo,const ImageInfo & dstImageInfo)55 void ScanlineFilter::SetPixelConvert(const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo)
56 {
57 needPixelConvert_ = true;
58 pixelConverter_ = PixelConvert::Create(srcImageInfo, dstImageInfo);
59 }
60
FilterLine(void * destRowPixels,uint32_t destRowBytes,const void * srcRowPixels)61 uint32_t ScanlineFilter::FilterLine(void *destRowPixels, uint32_t destRowBytes, const void *srcRowPixels)
62 {
63 if (destRowPixels == nullptr || srcRowPixels == nullptr) {
64 IMAGE_LOGE("[ScanlineFilter]the src or dest pixel point is null.");
65 return ERR_IMAGE_CROP;
66 }
67 auto startPixel = static_cast<const uint8_t *>(srcRowPixels) + srcRegion_.left * srcBpp_;
68 if (startPixel == nullptr) {
69 IMAGE_LOGE("[ScanlineFilter]the shift src pixel point is null.");
70 return ERR_IMAGE_CROP;
71 }
72 if (!needPixelConvert_) {
73 errno_t ret = memcpy_s(destRowPixels, destRowBytes, startPixel, srcRegion_.width * srcBpp_);
74 if (ret != 0) {
75 IMAGE_LOGE("[ScanlineFilter]memcpy failed,ret=%{public}d.", ret);
76 return ERR_IMAGE_CROP;
77 }
78 } else {
79 if (!ConvertPixels(destRowPixels, startPixel, srcRegion_.width)) {
80 IMAGE_LOGE("[ScanlineFilter]convert color failed.");
81 return ERR_IMAGE_COLOR_CONVERT;
82 }
83 }
84 return SUCCESS;
85 }
86
ConvertPixels(void * destRowPixels,const uint8_t * startPixel,uint32_t reqPixelNum)87 bool ScanlineFilter::ConvertPixels(void *destRowPixels, const uint8_t *startPixel, uint32_t reqPixelNum)
88 {
89 if (destRowPixels == nullptr || startPixel == nullptr) {
90 IMAGE_LOGE("[ScanlineFilter]convert color failed, the destRowPixels or startPixel is null.");
91 return false;
92 }
93
94 if (pixelConverter_ == nullptr) {
95 IMAGE_LOGE("[ScanlineFilter]pixel converter is null");
96 return false;
97 }
98
99 pixelConverter_->Convert(destRowPixels, startPixel, reqPixelNum);
100 return true;
101 }
102 } // namespace Media
103 } // namespace OHOS
104