• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H
18 
19 #include <chrono>
20 #include <fstream>
21 #include <string>
22 #include <vector>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/memory/ace_type.h"
26 
27 namespace OHOS {
28 
29 namespace Ace {
30 class Rect;
31 }
32 namespace Media {
33 class PixelMap;
34 }
35 
36 namespace Ace {
37 
38 enum class PixelFormat : int32_t {
39     UNKNOWN = 0,
40     ARGB_8888 = 1, // Each pixel is stored on 4 bytes.
41     RGB_565 = 2,   // Each pixel is stored on 2 bytes
42     RGBA_8888 = 3,
43     BGRA_8888 = 4,
44     RGB_888 = 5,
45     ALPHA_8 = 6,
46     RGBA_F16 = 7,
47     NV21 = 8, // Each pixel is stored on 3/2 bytes.
48     NV12 = 9,
49     CMYK = 10,
50     RGBA_1010102 = 14,
51 };
52 
53 enum class AlphaType : int32_t {
54     IMAGE_ALPHA_TYPE_UNKNOWN = 0,
55     IMAGE_ALPHA_TYPE_OPAQUE = 1,   // image pixels are stored as opaque.
56     IMAGE_ALPHA_TYPE_PREMUL = 2,   // image have alpha component, and all pixels have premultiplied by alpha value.
57     IMAGE_ALPHA_TYPE_UNPREMUL = 3, // image have alpha component, and all pixels stored without premultiply alpha value.
58 };
59 
60 enum class ResizableOption {
61     LEFT,
62     RIGHT,
63     TOP,
64     BOTTOM,
65 };
66 
67 struct ImageResizableSlice {
68     Dimension left;
69     Dimension right;
70     Dimension top;
71     Dimension bottom;
ToStringImageResizableSlice72     std::string ToString() const
73     {
74         std::string result;
75         result.append("ImageResizableSlice: {");
76         result.append("left: ");
77         result.append(left.ToString());
78         result.append(", right: ");
79         result.append(right.ToString());
80         result.append(", top: ");
81         result.append(top.ToString());
82         result.append(", bottom: ");
83         result.append(bottom.ToString());
84         result.append("}");
85         return result;
86     }
87     bool operator==(const ImageResizableSlice& slice) const
88     {
89         return left == slice.left && right == slice.right && top == slice.top && bottom == slice.bottom;
90     }
ValidImageResizableSlice91     bool Valid() const
92     {
93         return left.IsValid() || right.IsValid() || top.IsValid() || bottom.IsValid();
94     }
SetResizableLeftImageResizableSlice95     void SetResizableLeft(const Dimension& sliceDimension)
96     {
97         left = sliceDimension;
98     }
SetResizableRightImageResizableSlice99     void SetResizableRight(const Dimension& sliceDimension)
100     {
101         right = sliceDimension;
102     }
SetResizableBottomImageResizableSlice103     void SetResizableBottom(const Dimension& sliceDimension)
104     {
105         bottom = sliceDimension;
106     }
SetResizableTopImageResizableSlice107     void SetResizableTop(const Dimension& sliceDimension)
108     {
109         top = sliceDimension;
110     }
SetEdgeSliceImageResizableSlice111     void SetEdgeSlice(ResizableOption direction, const Dimension& sliceDimension)
112     {
113         switch (direction) {
114             case ResizableOption::TOP:
115                 SetResizableTop(sliceDimension);
116                 break;
117             case ResizableOption::BOTTOM:
118                 SetResizableBottom(sliceDimension);
119                 break;
120             case ResizableOption::LEFT:
121                 SetResizableLeft(sliceDimension);
122                 break;
123             case ResizableOption::RIGHT:
124                 SetResizableRight(sliceDimension);
125                 break;
126             default:
127                 break;
128         }
129     }
130 };
131 
132 enum class AceAntiAliasingOption : int32_t {
133     NONE = 0,
134     LOW = 1,
135     MEDIUM = 2,
136     HIGH = 3,
137 };
138 
139 class ACE_FORCE_EXPORT PixelMap : public AceType {
140     DECLARE_ACE_TYPE(PixelMap, AceType)
141 
142 public:
143     static RefPtr<PixelMap> Create(std::unique_ptr<Media::PixelMap>&& pixmap);
144     static RefPtr<PixelMap> CreatePixelMap(void* sptrAddr);
145     static RefPtr<PixelMap> CopyPixelMap(const RefPtr<PixelMap>& pixelMap);
146     static RefPtr<PixelMap> DecodeTlv(std::vector<uint8_t>& buff);
147 
148     /**
149      * @param ptr: drawable pointer of type Napi::DrawableDescriptor&
150      */
151     static RefPtr<PixelMap> GetFromDrawable(void* ptr);
152     static bool GetPxielMapListFromAnimatedDrawable(void* ptr, std::vector<RefPtr<PixelMap>>& pixelMaps,
153         int32_t& duration, int32_t& iterations);
154     static RefPtr<PixelMap> CreatePixelMapFromDataAbility(void* uniquePtr);
155     static RefPtr<PixelMap> ConvertSkImageToPixmap(
156         const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height);
157     virtual int32_t GetWidth() const = 0;
158     virtual int32_t GetHeight() const = 0;
159     virtual bool GetPixelsVec(std::vector<uint8_t>& data) const = 0;
160     virtual const uint8_t* GetPixels() const = 0;
161     virtual PixelFormat GetPixelFormat() const = 0;
162     virtual AlphaType GetAlphaType() const = 0;
163     virtual int32_t GetRowStride() const = 0;
164     virtual int32_t GetRowBytes() const = 0;
165     virtual int32_t GetByteCount() const = 0;
166     virtual void* GetPixelManager() const = 0;
167     virtual void* GetRawPixelMapPtr() const = 0;
168     virtual std::string GetId() = 0;
169     virtual std::string GetModifyId() = 0;
170     virtual std::shared_ptr<Media::PixelMap> GetPixelMapSharedPtr() = 0;
171     virtual void* GetWritablePixels() const = 0;
172     virtual void Scale(float xAxis, float yAxis) = 0;
173     virtual void Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) = 0;
174 
175     static void* GetReleaseContext(const RefPtr<PixelMap>& pixelMap);
176     // passed to SkImage to release PixelMap shared_ptr
177     static void ReleaseProc(const void* /* pixels */, void* context);
178     virtual void SavePixelMapToFile(const std::string& dst) const = 0;
179     virtual RefPtr<PixelMap> GetCropPixelMap(const Rect& srcRect) = 0;
180     virtual bool EncodeTlv(std::vector<uint8_t>& buff) = 0;
181 };
182 
183 } // namespace Ace
184 } // namespace OHOS
185 
186 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H
187