1 /*
2 * Copyright (C) 2024 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 #ifndef INTERFACES_INNERKITS_INCLUDE_PIXEL_YUV_H
17 #define INTERFACES_INNERKITS_INCLUDE_PIXEL_YUV_H
18 
19 #include "image_converter.h"
20 #include "image_type.h"
21 #include "include/core/SkBitmap.h"
22 #include "include/core/SkCanvas.h"
23 #include "include/core/SkImage.h"
24 #include "memory_manager.h"
25 #include "pixel_map.h"
26 
27 namespace OHOS {
28 namespace Media {
29 
30 struct SkTransYuvInfo {
31     SkRect r;
32     SkImageInfo info;
33     SkBitmap bitmap;
34 };
35 
36 class PixelYuv : public PixelMap {
37 public:
PixelYuv()38     PixelYuv() {}
39     virtual ~PixelYuv();
40     NATIVEEXPORT const uint8_t *GetPixel8(int32_t x, int32_t y) override;
41     NATIVEEXPORT const uint16_t *GetPixel16(int32_t x, int32_t y) override;
42     NATIVEEXPORT const uint32_t *GetPixel32(int32_t x, int32_t y) override;
43     NATIVEEXPORT bool GetARGB32Color(int32_t x, int32_t y, uint32_t &color) override;
44     NATIVEEXPORT uint8_t GetARGB32ColorA(uint32_t color) override;
45     NATIVEEXPORT uint8_t GetARGB32ColorR(uint32_t color) override;
46     NATIVEEXPORT uint8_t GetARGB32ColorG(uint32_t color) override;
47     NATIVEEXPORT uint8_t GetARGB32ColorB(uint32_t color) override;
48     NATIVEEXPORT uint32_t SetAlpha(const float percent) override;
49     NATIVEEXPORT uint32_t getPixelBytesNumber();
50     NATIVEEXPORT int32_t GetByteCount() override;
51     NATIVEEXPORT void rotate(float degrees) override;
52     NATIVEEXPORT uint32_t crop(const Rect &rect) override;
53     NATIVEEXPORT void scale(float xAxis, float yAxis) override;
54     NATIVEEXPORT void scale(float xAxis, float yAxis, const AntiAliasingOption &option) override;
55     NATIVEEXPORT void scale(int32_t dstW, int32_t dstH);
56     NATIVEEXPORT void scale(int32_t dstW, int32_t dstH, const AntiAliasingOption &option);
57     NATIVEEXPORT bool resize(float xAxis, float yAxis) override;
58     NATIVEEXPORT bool resize(int32_t dstW, int32_t dstH);
59     NATIVEEXPORT void flip(bool xAxis, bool yAxis) override;
60     NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset,
61                                       const uint32_t &stride, const Rect &region) override;
62     NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize) override;
63     NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride,
64                                      const Rect &region, uint8_t *dst) override;
65     NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, uint8_t *dst) override;
66     NATIVEEXPORT void translate(float xAxis, float yAxis) override;
67     NATIVEEXPORT uint32_t ReadPixel(const Position &pos, uint32_t &dst) override;
68     NATIVEEXPORT bool WritePixels(const uint32_t &color) override;
69     NATIVEEXPORT uint32_t WritePixel(const Position &pos, const uint32_t &color) override;
70     ColorYuv420 GetYuv420Color(uint32_t x, uint32_t y);
71     NATIVEEXPORT void SetPixelsAddr(void *addr, void *context, uint32_t size, AllocatorType type,
72                                     CustomFreePixelMap func) override;
73     bool YuvRotateConvert(Size &size, int32_t degrees, int32_t &dstWidth, int32_t &dstHeight,
74         OpenSourceLibyuv::RotationMode &rotateNum);
75     bool YuvRotateConvert(Size &srcSize, int32_t degrees, Size &dstSize,
76                           OpenSourceLibyuv::RotationMode &rotateNum, YUVDataInfo yuvDataInfo);
77 protected:
78     bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset, const Rect &region);
79     void SetRowDataSizeForImageInfo(ImageInfo info);
80     static uint32_t GetImageSize(int32_t width, int32_t height, PixelFormat format);
81     uint32_t SetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace, SkTransYuvInfo &src,
82         PixelFormat &format, uint64_t rowStride);
83     std::unique_ptr<AbsMemory> CreateMemory(PixelFormat pixelFormat, std::string memoryTag,
84         int32_t dstWidth, int32_t dstHeight, YUVStrideInfo &dstStrides);
85 #ifdef IMAGE_COLORSPACE_FLAG
86     bool CheckColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace);
87     int32_t ColorSpaceBGRAToYuv(uint8_t *bgraData, SkTransYuvInfo &dst, ImageInfo &imageInfo, PixelFormat &format,
88         const OHOS::ColorManager::ColorSpace &grColorSpace);
89     NATIVEEXPORT uint32_t ApplyColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace) override;
90 #endif
91 private:
92     bool IsP010Yuv();
93     void GetYUVInfoForCopyPixels(YUVDataInfo &yuvDataInfo);
94 };
95 } // namespace Media
96 } // namespace OHOS
97 
98 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_YUV_H
99