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 void Build(int32_t width, int32_t height, const BitmapFormat& format, int32_t stride = 0); 35 void 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 int GetRowBytes() const; 48 ColorType GetColorType() const; 49 AlphaType GetAlphaType() const; 50 bool ExtractSubset(Bitmap& dst, const Rect& subset) const; 51 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 52 int32_t srcX, int32_t srcY) const; 53 bool PeekPixels(Pixmap& pixmap) const; 54 55 size_t ComputeByteSize() const; 56 57 /* 58 * @brief Gets the pointer to Bitmap buffer. 59 */ 60 void* GetPixels() const; 61 void SetPixels(void* pixel); 62 void CopyPixels(Bitmap& dst, int srcLeft, int srcTop) const; 63 bool InstallPixels(const ImageInfo& info, void* pixels, size_t rowBytes, 64 ReleaseProc releaseProc = nullptr, void* context = nullptr); 65 bool IsImmutable(); 66 void SetImmutable(); 67 void ClearWithColor(const ColorQuad& color) const; 68 bool IsValid() const; 69 bool IsEmpty() const; 70 ColorQuad GetColor(int x, int y) const; 71 void Free(); 72 BitmapFormat GetFormat() const; 73 void SetFormat(const BitmapFormat& format); 74 void SetInfo(const ImageInfo& info); 75 ImageInfo GetImageInfo() const; 76 Pixmap GetPixmap() const; 77 /* 78 * @brief Make new image from Bitmap but never copy Pixels 79 * @note the function never copy Pixels, make sure Pixels is available during using the image 80 */ 81 std::shared_ptr<Image> MakeImage() const; 82 bool TryAllocPixels(const ImageInfo& info); 83 template<typename T> GetImpl()84 T* GetImpl() const 85 { 86 return bmpImplPtr->DowncastingTo<T>(); 87 } 88 89 std::shared_ptr<Data> Serialize() const; 90 bool Deserialize(std::shared_ptr<Data> data); 91 92 private: 93 std::shared_ptr<BitmapImpl> bmpImplPtr; 94 BitmapFormat format_; 95 ImageInfo imageInfo_; 96 }; 97 } // namespace Drawing 98 } // namespace Rosen 99 } // namespace OHOS 100 #endif 101