• 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 #include <memory>
24 
25 #include "base/geometry/dimension.h"
26 #include "base/geometry/rect.h"
27 #include "base/geometry/ng/size_t.h"
28 #include "base/memory/ace_type.h"
29 
30 namespace OHOS {
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     YCBCR_P010 = 11,
51     YCRCB_P010 = 12,
52     RGBA_1010102 = 14,
53 };
54 
55 enum class AlphaType : int32_t {
56     IMAGE_ALPHA_TYPE_UNKNOWN = 0,
57     IMAGE_ALPHA_TYPE_OPAQUE = 1,   // image pixels are stored as opaque.
58     IMAGE_ALPHA_TYPE_PREMUL = 2,   // image have alpha component, and all pixels have premultiplied by alpha value.
59     IMAGE_ALPHA_TYPE_UNPREMUL = 3, // image have alpha component, and all pixels stored without premultiply alpha value.
60 };
61 
62 enum class AllocatorType : int32_t {
63     // keep same with java AllocatorType
64     DEFAULT = 0,
65     HEAP_ALLOC = 1,
66     SHARE_MEM_ALLOC = 2,
67     CUSTOM_ALLOC = 3,  // external
68     DMA_ALLOC = 4, // SurfaceBuffer
69 };
70 
71 enum class ResizableOption {
72     LEFT,
73     RIGHT,
74     TOP,
75     BOTTOM,
76 };
77 
78 struct ImageResizableSlice {
79     Dimension left;
80     Dimension right;
81     Dimension top;
82     Dimension bottom;
ToStringImageResizableSlice83     std::string ToString() const
84     {
85         std::string result;
86         result.append("ImageResizableSlice: {");
87         result.append("left: ");
88         result.append(left.ToString());
89         result.append(", right: ");
90         result.append(right.ToString());
91         result.append(", top: ");
92         result.append(top.ToString());
93         result.append(", bottom: ");
94         result.append(bottom.ToString());
95         result.append("}");
96         return result;
97     }
98     bool operator==(const ImageResizableSlice& slice) const
99     {
100         return left == slice.left && right == slice.right && top == slice.top && bottom == slice.bottom;
101     }
ValidImageResizableSlice102     bool Valid() const
103     {
104         return left.IsValid() || right.IsValid() || top.IsValid() || bottom.IsValid();
105     }
SetResizableLeftImageResizableSlice106     void SetResizableLeft(const Dimension& sliceDimension)
107     {
108         left = sliceDimension;
109     }
SetResizableRightImageResizableSlice110     void SetResizableRight(const Dimension& sliceDimension)
111     {
112         right = sliceDimension;
113     }
SetResizableBottomImageResizableSlice114     void SetResizableBottom(const Dimension& sliceDimension)
115     {
116         bottom = sliceDimension;
117     }
SetResizableTopImageResizableSlice118     void SetResizableTop(const Dimension& sliceDimension)
119     {
120         top = sliceDimension;
121     }
SetEdgeSliceImageResizableSlice122     void SetEdgeSlice(ResizableOption direction, const Dimension& sliceDimension)
123     {
124         switch (direction) {
125             case ResizableOption::TOP:
126                 SetResizableTop(sliceDimension);
127                 break;
128             case ResizableOption::BOTTOM:
129                 SetResizableBottom(sliceDimension);
130                 break;
131             case ResizableOption::LEFT:
132                 SetResizableLeft(sliceDimension);
133                 break;
134             case ResizableOption::RIGHT:
135                 SetResizableRight(sliceDimension);
136                 break;
137             default:
138                 break;
139         }
140     }
141 };
142 
143 enum class AceAntiAliasingOption : int32_t {
144     NONE = 0,
145     LOW = 1,
146     MEDIUM = 2,
147     HIGH = 3,
148 };
149 
150 enum class ScaleMode : int32_t {
151     FIT_TARGET_SIZE = 0,
152     CENTER_CROP = 1,
153 };
154 
155 struct InitializationOptions {
156     NG::SizeT<int32_t> size;
157     PixelFormat srcPixelFormat = PixelFormat::BGRA_8888;
158     PixelFormat pixelFormat = PixelFormat::UNKNOWN;
159     AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
160     ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE;
161     int32_t srcRowStride = 0;
162     bool editable = false;
163     bool useSourceIfMatch = false;
164     bool useDMA = false;
165 };
166 
167 struct WritePixelsOptions {
168     const uint8_t* source = nullptr;
169     uint64_t bufferSize = 0;
170     uint32_t offset = 0;
171     uint32_t stride = 0;
172     Rect region;
173     PixelFormat srcPixelFormat = PixelFormat::BGRA_8888;
174 };
175 
176 class ACE_FORCE_EXPORT PixelMap : public AceType {
177     DECLARE_ACE_TYPE(PixelMap, AceType)
178 
179 public:
180     static RefPtr<PixelMap> Create(std::unique_ptr<Media::PixelMap>&& pixmap);
181     static RefPtr<PixelMap> Create(const InitializationOptions& opts);
182     static RefPtr<PixelMap> CreatePixelMap(void* sptrAddr);
183     static RefPtr<PixelMap> CopyPixelMap(const RefPtr<PixelMap>& pixelMap);
184     static RefPtr<PixelMap> DecodeTlv(std::vector<uint8_t>& buff);
185 
186     /**
187      * @param ptr: drawable pointer of type Napi::DrawableDescriptor&
188      */
189     static RefPtr<PixelMap> GetFromDrawable(void* ptr);
190     static bool GetPxielMapListFromAnimatedDrawable(void* ptr, std::vector<RefPtr<PixelMap>>& pixelMaps,
191         int32_t& duration, int32_t& iterations);
192     static RefPtr<PixelMap> CreatePixelMapFromDataAbility(void* uniquePtr);
193     static RefPtr<PixelMap> ConvertSkImageToPixmap(
194         const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height);
195     virtual int32_t GetWidth() const = 0;
196     virtual int32_t GetHeight() const = 0;
197     virtual bool GetPixelsVec(std::vector<uint8_t>& data) const = 0;
198     virtual const uint8_t* GetPixels() const = 0;
199     virtual PixelFormat GetPixelFormat() const = 0;
200     virtual AlphaType GetAlphaType() const = 0;
201     virtual int32_t GetRowStride() const = 0;
202     virtual int32_t GetRowBytes() const = 0;
203     virtual int32_t GetByteCount() const = 0;
204     virtual AllocatorType GetAllocatorType() const = 0;
205     virtual bool IsHdr() const = 0;
206     virtual void* GetPixelManager() const = 0;
207     virtual void* GetRawPixelMapPtr() const = 0;
208     virtual std::string GetId() = 0;
209     virtual std::string GetModifyId() = 0;
210     virtual std::shared_ptr<Media::PixelMap> GetPixelMapSharedPtr() = 0;
211     virtual void* GetWritablePixels() const = 0;
212     virtual void Scale(float xAxis, float yAxis) = 0;
213     virtual void Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) = 0;
214 
215     static void* GetReleaseContext(const RefPtr<PixelMap>& pixelMap);
216     // passed to SkImage to release PixelMap shared_ptr
217     static void ReleaseProc(const void* /* pixels */, void* context);
218     virtual void SavePixelMapToFile(const std::string& dst) const = 0;
219     virtual RefPtr<PixelMap> GetCropPixelMap(const Rect& srcRect) = 0;
220     virtual bool EncodeTlv(std::vector<uint8_t>& buff) = 0;
221     virtual uint32_t WritePixels(const WritePixelsOptions& opts) = 0;
222 };
223 
224 } // namespace Ace
225 } // namespace OHOS
226 
227 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H
228