• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 BITMAP_H
17 #define BITMAP_H
18 
19 #include "drawing/engine_adapter/impl_interface/bitmap_impl.h"
20 #include "utils/drawing_macros.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 struct BitmapFormat {
26     ColorType colorType;
27     AlphaType alphaType;
28 };
29 
30 class DRAWING_API Bitmap {
31 public:
32     Bitmap();
33     virtual ~Bitmap();
34     bool Build(int32_t width, int32_t height, const BitmapFormat& format, int32_t stride = 0);
35     bool Build(const ImageInfo& imageInfo, int32_t stride = 0);
36 
37     /**
38      * @brief Gets the width of Bitmap.
39      */
40     int GetWidth() const;
41 
42     /**
43      * @brief Gets the height of Bitmap.
44      */
45     int GetHeight() const;
46 
47     /**
48      * @brief Returns row bytes, the interval from one pixel row to the next. Row bytes
49      * is at least as large as: GetWidth() * GetImageInfo().GetBytesPerPixel().
50      * @return  byte length of pixel row
51      */
52     int GetRowBytes() const;
53     ColorType GetColorType() const;
54     AlphaType GetAlphaType() const;
55     bool ExtractSubset(Bitmap& dst, const Rect& subset) const;
56 
57     /**
58      * @brief Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
59      * and does not exceed Bitmap (GetWidth(), GetHeight()).
60      *
61      * dstInfo specifies width, height, ColorType, AlphaType, and ColorSpace of
62      * destination. dstRowBytes specifics the gap from one destination row to the next.
63      * Returns true if pixels are copied. Returns false if:
64      * - dstInfo has no address
65      * - dstRowBytes is less than dstInfo.GetMinRowBytes()
66      * - PixelRef is nullptr
67      * srcX and srcY may be negative to copy only top or left of source. Returns false
68      * if GetWidth() or GetHeight() is zero or negative.
69      * Returns false if abs(srcX) >= Bitmap GetWidth(), or if abs(srcY) >= Bitmap GetHeight().
70      *
71      * @param dstInfo      destination width, height, ColorType, AlphaType, ColorSpace
72      * @param dstPixels    destination pixel storage
73      * @param dstRowBytes  destination row length
74      * @param srcX         column index whose absolute value is less than GetWidth()
75      * @param srcY         row index whose absolute value is less than GetHeight()
76      * @return             true if pixels are copied to dstPixels
77      */
78     bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
79                     int32_t srcX, int32_t srcY) const;
80 
81     size_t ComputeByteSize() const;
82 
83     /**
84      * @brief Copies Bitmap pixel address, row bytes, and ImageInfo to pixmap, if address
85      * is available, and returns true. If pixel address is not available, return false and
86      * leave pixmap unchanged.
87      * pixmap contents become invalid on any future change to Bitmap.
88      *
89      * @param pixmap storage for pixel state if pixels are readable; otherwise, ignored
90      * @return true if Bitmap has direct access to pixels
91      */
92     bool PeekPixels(Pixmap& pixmap) const;
93 
94     /**
95      * @brief Gets the pointer to Bitmap buffer.
96      */
97     void* GetPixels() const;
98 
99     /**
100      * @brief Replaces PixelRef with pixels, preserving ImageInfo and GetRowBytes().
101      * Sets pixel pointer to (0,0). If pixels is nullptr, or if GetImageInfo().GetColorType()
102      * equals COLORTYPE_UNKNOWN; release reference to PixelRef, and set PixelRef to nullptr.
103      * Caller is responsible for handling ownership pixel memory for the lifetime of Bitmap and PixelRef.
104      *
105      * @param pixel address of pixel storage, managed by caller
106      */
107     void SetPixels(void* pixel);
108 
109     /**
110      * @brief Copies a Rect of pixels from Bitmap to dst Bitmap Pixels.
111      * Copy starts at (srcLeft, srcTop), and does not exceed Bitmap (GetWidth(), GetHeight()).
112      *
113      * dstInfo specifies width, height, ColorType, AlphaType, and ColorSpace of
114      * destination. dstRowBytes specifics the gap from one destination row to the next.
115      * Returns true if pixels are copied. Returns false if:
116      * - dstInfo has no address
117      * - dstRowBytes is less than dstInfo.GetMinRowBytes()
118      * - PixelRef is nullptr
119      * srcX and srcY may be negative to copy only top or left of source. Returns false
120      * if GetWidth() or GetHeight() is zero or negative.
121      * Returns false if abs(srcLeft) >= Bitmap GetWidth(), or if abs(srcTop) >= Bitmap GetHeight().
122      *
123      * @param dst     destination Bitmap
124      * @param srcLeft column index whose absolute value is less than GetWidth()
125      * @param srcTop  row index whose absolute value is less than GetHeight()
126      */
127     void CopyPixels(Bitmap& dst, int srcLeft, int srcTop) const;
128 
129     /**
130      * @brief Sets ImageInfo to info following the rules in setInfo(), and creates PixelRef
131      * containing pixels and rowBytes. releaseProc, if not nullptr, is called immediately on failure
132      * or when pixels are no longer referenced. context may be nullptr. If ImageInfo could not be
133      * set, or rowBytes is less than info.GetMinRowBytes():
134      * calls releaseProc if present, calls Free(), and returns false.
135      * Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if present, returns true.
136      * If ImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr:
137      * when pixels are no longer referenced, calls releaseProc with pixels and context as parameters.
138      * @param info         contains width, height, AlphaType, ColorType, ColorSpace
139      * @param pixels       address or pixel storage; may be nullptr
140      * @param rowBytes     size of pixel row or larger
141      * @param releaseProc  function called when pixels can be deleted; may be nullptr
142      * @param context      caller state passed to releaseProc; may be nullptr
143      * @return             true if ImageInfo is set to info
144      */
145     bool InstallPixels(const ImageInfo& info, void* pixels, size_t rowBytes,
146                        ReleaseProc releaseProc = nullptr, void* context = nullptr);
147     /**
148      * @brief Returns true if pixels can not change. Most
149      * immutable Bitmap checks trigger an assert only on debug builds.
150      * @return true if pixels are immutable
151      */
152     bool IsImmutable();
153 
154     /**
155      * @brief Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
156      * Any other bitmap sharing the same PixelRef are also marked as immutable. Once PixelRef
157      * is marked immutable, the setting cannot be cleared. Writing to immutable Bitmap pixels
158      * triggers an assert on debug builds.
159      */
160     void SetImmutable();
161 
162     /**
163      * @brief Replaces pixel values with c, interpreted as being in the sRGB ColorSpace.
164      * @param color unpremultiplied color
165      */
166     void ClearWithColor(const ColorQuad& color) const;
167 
168     /**
169      * @brief Returns true if GetWidth() or GetHeight() are zero, or if PixelRef is nullptr.
170      * If true, Bitmap has no effect when drawn or drawn into.
171      * @return true if drawing has no effect
172      */
173     bool IsValid() const;
174 
175     /**
176      * @brief Returns true if either width() or height() are zero.
177      * Does not check if SkPixelRef is nullptr; call IsValid() to check GetWidth(), GetHeight(), and PixelRef.
178      * @return true if dimensions do not enclose area
179      */
180     bool IsEmpty() const;
181 
182     /**
183      * @brief Returns pixel at (x, y) as unpremultiplied color. Returns black with
184      * alpha if ColorType is COLORTYPE_ALPHA_8. ColorSpace in ImageInfo is ignored.
185      * Some color precision may be lost in the conversion to unpremultiplied color;
186      * original pixel data may have additional precision.
187      *
188      * @param x column index, zero or greater, and less than GetWidth()
189      * @param y row index, zero or greater, and less than GetHeight()
190      * @return pixel converted to unpremultiplied color
191      */
192     ColorQuad GetColor(int x, int y) const;
193 
194 
195     /**
196      * @brief Resets to its initial state; all fields are set to zero, as if Bitmap
197      * had been initialized by Bitmap(). Sets width, height, row bytes to zero; pixel
198      * address to nullptr; ColorType to COLORTYPE_UNKNOWN; and AlphaType to ALPHATYPE_UNKNOWN.
199      * If PixelRef is allocated, its reference count is decreased by one, releasing its memory
200      * if Bitmap is the sole owner.
201      */
202     void Free();
203     BitmapFormat GetFormat() const;
204     void SetFormat(const BitmapFormat& format);
205 
206     void SetInfo(const ImageInfo& info);
207 
208     /**
209      * @brief  Gets Image info which contains width, height, AlphaType, ColorType, and ColorSpace.
210      * @return Returns ImageInfo describing this Bitmap
211      */
212     ImageInfo GetImageInfo() const;
213 
214     /**
215      * @brief  Gets a constant reference to the Pixmap holding the Bitmap pixel
216      * address, row bytes, and ImageInfo.
217      * @return Returns Pixmap describing this Bitmap
218      */
219     Pixmap GetPixmap() const;
220 
221     /*
222      * @brief  Make new image from Bitmap but never copy Pixels
223      * @note the function never copy Pixels, make sure Pixels is available during using the image
224      */
225     std::shared_ptr<Image> MakeImage() const;
226 
227     /**
228      * @brief Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
229      * Returns false and calls reset() if SkImageInfo could not be set, or memory could not be allocated.
230      * On most platforms, allocating pixel memory may succeed even though there is not sufficient memory
231      * to hold pixels; allocation does not take place until the pixels are written to. The actual behavior
232      * depends on the platform implementation of malloc().
233      * @param info contains width, height, AlphaType, ColorType, ColorSpace
234      * @return true if pixel storage is allocated
235      */
236     bool TryAllocPixels(const ImageInfo& info);
237     template<typename T>
GetImpl()238     T* GetImpl() const
239     {
240         return bmpImplPtr->DowncastingTo<T>();
241     }
242 
243     std::shared_ptr<Data> Serialize() const;
244     bool Deserialize(std::shared_ptr<Data> data);
245 
246 private:
247     std::shared_ptr<BitmapImpl> bmpImplPtr;
248 };
249 } // namespace Drawing
250 } // namespace Rosen
251 } // namespace OHOS
252 #endif
253