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