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 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 ResizableOption { 63 LEFT, 64 RIGHT, 65 TOP, 66 BOTTOM, 67 }; 68 69 struct ImageResizableSlice { 70 Dimension left; 71 Dimension right; 72 Dimension top; 73 Dimension bottom; ToStringImageResizableSlice74 std::string ToString() const 75 { 76 std::string result; 77 result.append("ImageResizableSlice: {"); 78 result.append("left: "); 79 result.append(left.ToString()); 80 result.append(", right: "); 81 result.append(right.ToString()); 82 result.append(", top: "); 83 result.append(top.ToString()); 84 result.append(", bottom: "); 85 result.append(bottom.ToString()); 86 result.append("}"); 87 return result; 88 } 89 bool operator==(const ImageResizableSlice& slice) const 90 { 91 return left == slice.left && right == slice.right && top == slice.top && bottom == slice.bottom; 92 } ValidImageResizableSlice93 bool Valid() const 94 { 95 return left.IsValid() || right.IsValid() || top.IsValid() || bottom.IsValid(); 96 } SetResizableLeftImageResizableSlice97 void SetResizableLeft(const Dimension& sliceDimension) 98 { 99 left = sliceDimension; 100 } SetResizableRightImageResizableSlice101 void SetResizableRight(const Dimension& sliceDimension) 102 { 103 right = sliceDimension; 104 } SetResizableBottomImageResizableSlice105 void SetResizableBottom(const Dimension& sliceDimension) 106 { 107 bottom = sliceDimension; 108 } SetResizableTopImageResizableSlice109 void SetResizableTop(const Dimension& sliceDimension) 110 { 111 top = sliceDimension; 112 } SetEdgeSliceImageResizableSlice113 void SetEdgeSlice(ResizableOption direction, const Dimension& sliceDimension) 114 { 115 switch (direction) { 116 case ResizableOption::TOP: 117 SetResizableTop(sliceDimension); 118 break; 119 case ResizableOption::BOTTOM: 120 SetResizableBottom(sliceDimension); 121 break; 122 case ResizableOption::LEFT: 123 SetResizableLeft(sliceDimension); 124 break; 125 case ResizableOption::RIGHT: 126 SetResizableRight(sliceDimension); 127 break; 128 default: 129 break; 130 } 131 } 132 }; 133 134 enum class AceAntiAliasingOption : int32_t { 135 NONE = 0, 136 LOW = 1, 137 MEDIUM = 2, 138 HIGH = 3, 139 }; 140 141 class ACE_FORCE_EXPORT PixelMap : public AceType { 142 DECLARE_ACE_TYPE(PixelMap, AceType) 143 144 public: 145 static RefPtr<PixelMap> Create(std::unique_ptr<Media::PixelMap>&& pixmap); 146 static RefPtr<PixelMap> CreatePixelMap(void* sptrAddr); 147 static RefPtr<PixelMap> CopyPixelMap(const RefPtr<PixelMap>& pixelMap); 148 static RefPtr<PixelMap> DecodeTlv(std::vector<uint8_t>& buff); 149 150 /** 151 * @param ptr: drawable pointer of type Napi::DrawableDescriptor& 152 */ 153 static RefPtr<PixelMap> GetFromDrawable(void* ptr); 154 static bool GetPxielMapListFromAnimatedDrawable(void* ptr, std::vector<RefPtr<PixelMap>>& pixelMaps, 155 int32_t& duration, int32_t& iterations); 156 static RefPtr<PixelMap> CreatePixelMapFromDataAbility(void* uniquePtr); 157 static RefPtr<PixelMap> ConvertSkImageToPixmap( 158 const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height); 159 virtual int32_t GetWidth() const = 0; 160 virtual int32_t GetHeight() const = 0; 161 virtual bool GetPixelsVec(std::vector<uint8_t>& data) const = 0; 162 virtual const uint8_t* GetPixels() const = 0; 163 virtual PixelFormat GetPixelFormat() const = 0; 164 virtual AlphaType GetAlphaType() const = 0; 165 virtual int32_t GetRowStride() const = 0; 166 virtual int32_t GetRowBytes() const = 0; 167 virtual int32_t GetByteCount() const = 0; 168 virtual void* GetPixelManager() const = 0; 169 virtual void* GetRawPixelMapPtr() const = 0; 170 virtual std::string GetId() = 0; 171 virtual std::string GetModifyId() = 0; 172 virtual std::shared_ptr<Media::PixelMap> GetPixelMapSharedPtr() = 0; 173 virtual void* GetWritablePixels() const = 0; 174 virtual void Scale(float xAxis, float yAxis) = 0; 175 virtual void Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) = 0; 176 177 static void* GetReleaseContext(const RefPtr<PixelMap>& pixelMap); 178 // passed to SkImage to release PixelMap shared_ptr 179 static void ReleaseProc(const void* /* pixels */, void* context); 180 virtual void SavePixelMapToFile(const std::string& dst) const = 0; 181 virtual RefPtr<PixelMap> GetCropPixelMap(const Rect& srcRect) = 0; 182 virtual bool EncodeTlv(std::vector<uint8_t>& buff) = 0; 183 }; 184 185 } // namespace Ace 186 } // namespace OHOS 187 188 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H 189