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 23 #include "base/geometry/dimension.h" 24 #include "base/memory/ace_type.h" 25 26 namespace OHOS { 27 28 namespace Media { 29 class PixelMap; 30 } 31 32 namespace Ace { 33 34 enum class PixelFormat : int32_t { 35 UNKNOWN = 0, 36 ARGB_8888 = 1, // Each pixel is stored on 4 bytes. 37 RGB_565 = 2, // Each pixel is stored on 2 bytes 38 RGBA_8888 = 3, 39 BGRA_8888 = 4, 40 RGB_888 = 5, 41 ALPHA_8 = 6, 42 RGBA_F16 = 7, 43 NV21 = 8, // Each pixel is stored on 3/2 bytes. 44 NV12 = 9, 45 CMYK = 10, 46 }; 47 48 enum class AlphaType : int32_t { 49 IMAGE_ALPHA_TYPE_UNKNOWN = 0, 50 IMAGE_ALPHA_TYPE_OPAQUE = 1, // image pixels are stored as opaque. 51 IMAGE_ALPHA_TYPE_PREMUL = 2, // image have alpha component, and all pixels have premultiplied by alpha value. 52 IMAGE_ALPHA_TYPE_UNPREMUL = 3, // image have alpha component, and all pixels stored without premultiply alpha value. 53 }; 54 55 struct ImageResizableSlice { 56 Dimension left; 57 Dimension right; 58 Dimension top; 59 Dimension bottom; ToStringImageResizableSlice60 std::string ToString() const 61 { 62 std::string result; 63 result.append("ImageResizableSlice: {"); 64 result.append("left: "); 65 result.append(left.ToString()); 66 result.append(", right: "); 67 result.append(right.ToString()); 68 result.append(", top: "); 69 result.append(top.ToString()); 70 result.append(", bottom: "); 71 result.append(bottom.ToString()); 72 result.append("}"); 73 return result; 74 } 75 bool operator==(const ImageResizableSlice& slice) const 76 { 77 return left == slice.left && right == slice.right && top == slice.top && bottom == slice.bottom; 78 } ValidImageResizableSlice79 bool Valid() const 80 { 81 return left.IsValid() || right.IsValid() || top.IsValid() || bottom.IsValid(); 82 } 83 }; 84 85 enum class AceAntiAliasingOption : int32_t { 86 NONE = 0, 87 LOW = 1, 88 MEDIUM = 2, 89 HIGH = 3, 90 }; 91 92 class ACE_EXPORT PixelMap : public AceType { 93 DECLARE_ACE_TYPE(PixelMap, AceType) 94 95 public: 96 static RefPtr<PixelMap> Create(std::unique_ptr<Media::PixelMap>&& pixmap); 97 static RefPtr<PixelMap> CreatePixelMap(void* sptrAddr); 98 /** 99 * @param ptr: drawable pointer of type Napi::DrawableDescriptor& 100 */ 101 static RefPtr<PixelMap> GetFromDrawable(void* ptr); 102 static RefPtr<PixelMap> CreatePixelMapFromDataAbility(void* uniquePtr); 103 static RefPtr<PixelMap> ConvertSkImageToPixmap( 104 const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height); 105 virtual int32_t GetWidth() const = 0; 106 virtual int32_t GetHeight() const = 0; 107 virtual const uint8_t* GetPixels() const = 0; 108 virtual PixelFormat GetPixelFormat() const = 0; 109 virtual AlphaType GetAlphaType() const = 0; 110 virtual int32_t GetRowStride() const = 0; 111 virtual int32_t GetRowBytes() const = 0; 112 virtual int32_t GetByteCount() const = 0; 113 virtual void* GetPixelManager() const = 0; 114 virtual void* GetRawPixelMapPtr() const = 0; 115 virtual std::string GetId() = 0; 116 virtual std::string GetModifyId() = 0; 117 virtual std::shared_ptr<Media::PixelMap> GetPixelMapSharedPtr() = 0; 118 virtual void* GetWritablePixels() const = 0; 119 virtual void Scale(float xAxis, float yAxis) = 0; 120 virtual void Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) = 0; 121 122 static void* GetReleaseContext(const RefPtr<PixelMap>& pixelMap); 123 // passed to SkImage to release PixelMap shared_ptr 124 static void ReleaseProc(const void* /* pixels */, void* context); 125 virtual void SavePixelMapToFile(const std::string& dst) const = 0; 126 }; 127 128 } // namespace Ace 129 } // namespace OHOS 130 131 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H 132