1 /* 2 * Copyright (C) 2024 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 INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_ 17 #define INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_ 18 19 #include <memory> 20 #include <vector> 21 #ifdef IMAGE_COLORSPACE_FLAG 22 #include "color_space.h" 23 #endif 24 #include "image_type.h" 25 #include "parcel.h" 26 27 namespace OHOS { 28 namespace Media { 29 class RosenImageWrapper; 30 using TransColorProc = bool (*)(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 31 using CustomFreePixelMap = void (*)(void *addr, void *context, uint32_t size); 32 33 typedef struct { 34 float scaleX; 35 float scaleY; 36 float rotateD; 37 float cropLeft; 38 float cropTop; 39 float cropWidth; 40 float cropHeight; 41 float translateX; 42 float translateY; 43 bool flipX; 44 bool flipY; 45 } TransformData; 46 47 struct InitializationOptions { 48 Size size; 49 PixelFormat pixelFormat = PixelFormat::UNKNOWN; 50 AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 51 ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE; 52 bool editable = false; 53 bool useSourceIfMatch = false; 54 }; 55 56 // Build ARGB_8888 pixel value 57 constexpr uint8_t ARGB_MASK = 0xFF; 58 constexpr uint8_t ARGB_A_SHIFT = 24; 59 constexpr uint8_t ARGB_R_SHIFT = 16; 60 constexpr uint8_t ARGB_G_SHIFT = 8; 61 constexpr uint8_t ARGB_B_SHIFT = 0; 62 // Define pixel map malloc max size 600MB 63 constexpr int32_t PIXEL_MAP_MAX_RAM_SIZE = 600 * 1024 * 1024; 64 65 class PixelMap : public Parcelable { 66 public: 67 PixelMap() = default; 68 virtual ~PixelMap(); 69 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, 70 const InitializationOptions &opts); 71 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset, 72 int32_t stride, const InitializationOptions &opts); 73 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const InitializationOptions &opts); 74 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const InitializationOptions &opts); 75 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect, 76 const InitializationOptions &opts); 77 NATIVEEXPORT uint32_t SetImageInfo(ImageInfo &info); 78 NATIVEEXPORT uint32_t SetImageInfo(ImageInfo &info, bool isReused); 79 NATIVEEXPORT const uint8_t *GetPixel(int32_t x, int32_t y); 80 NATIVEEXPORT const uint8_t *GetPixel8(int32_t x, int32_t y); 81 NATIVEEXPORT const uint16_t *GetPixel16(int32_t x, int32_t y); 82 NATIVEEXPORT const uint32_t *GetPixel32(int32_t x, int32_t y); 83 NATIVEEXPORT bool GetARGB32Color(int32_t x, int32_t y, uint32_t &color); 84 NATIVEEXPORT void SetPixelsAddr(void *addr, void *context, uint32_t size, AllocatorType type, 85 CustomFreePixelMap func); 86 NATIVEEXPORT int32_t GetPixelBytes(); 87 NATIVEEXPORT int32_t GetRowBytes(); 88 NATIVEEXPORT int32_t GetByteCount(); 89 NATIVEEXPORT int32_t GetWidth(); 90 NATIVEEXPORT int32_t GetHeight(); 91 NATIVEEXPORT int32_t GetBaseDensity(); 92 NATIVEEXPORT void scale(float xAxis, float yAxis); 93 NATIVEEXPORT void translate(float xAxis, float yAxis); 94 NATIVEEXPORT void rotate(float degrees); 95 NATIVEEXPORT void flip(bool xAxis, bool yAxis); 96 NATIVEEXPORT uint32_t crop(const Rect &rect); 97 NATIVEEXPORT void GetImageInfo(ImageInfo &imageInfo); 98 NATIVEEXPORT PixelFormat GetPixelFormat(); 99 NATIVEEXPORT ColorSpace GetColorSpace(); 100 NATIVEEXPORT AlphaType GetAlphaType(); 101 NATIVEEXPORT uint32_t SetAlpha(const float percent); 102 NATIVEEXPORT const uint8_t *GetPixels(); 103 NATIVEEXPORT uint8_t GetARGB32ColorA(uint32_t color); 104 NATIVEEXPORT uint8_t GetARGB32ColorR(uint32_t color); 105 NATIVEEXPORT uint8_t GetARGB32ColorG(uint32_t color); 106 NATIVEEXPORT uint8_t GetARGB32ColorB(uint32_t color); 107 // Config the pixel map parameter 108 NATIVEEXPORT bool IsSameImage(const PixelMap &other); 109 NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride, 110 const Rect ®ion, uint8_t *dst); 111 NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, uint8_t *dst); 112 NATIVEEXPORT uint32_t ReadPixel(const Position &pos, uint32_t &dst); 113 NATIVEEXPORT uint32_t ResetConfig(const Size &size, const PixelFormat &format); 114 NATIVEEXPORT bool SetAlphaType(const AlphaType &alphaType); 115 NATIVEEXPORT uint32_t WritePixel(const Position &pos, const uint32_t &color); 116 NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset, 117 const uint32_t &stride, const Rect ®ion); 118 NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize); 119 NATIVEEXPORT bool WritePixels(const uint32_t &color); 120 NATIVEEXPORT void FreePixelMap(); 121 NATIVEEXPORT AllocatorType GetAllocatorType(); 122 NATIVEEXPORT void *GetFd() const; 123 GetCapacity()124 NATIVEEXPORT uint32_t GetCapacity() 125 { 126 return pixelsSize_; 127 } 128 IsEditable()129 NATIVEEXPORT bool IsEditable() 130 { 131 return editable_; 132 } 133 134 // judgement whether create pixelmap use source as result IsSourceAsResponse()135 NATIVEEXPORT bool IsSourceAsResponse() 136 { 137 return useSourceAsResponse_; 138 } 139 GetWritablePixels()140 NATIVEEXPORT void *GetWritablePixels() const 141 { 142 return static_cast<void *>(data_); 143 } 144 GetUniqueId()145 NATIVEEXPORT uint32_t GetUniqueId() const 146 { 147 return 0; 148 } 149 GetRowStride()150 NATIVEEXPORT int32_t GetRowStride() const 151 { 152 return rowStride_; 153 } 154 155 NATIVEEXPORT bool Marshalling(Parcel &data) const override; 156 NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data); 157 NATIVEEXPORT bool EncodeTlv(std::vector<uint8_t> &buff) const; 158 NATIVEEXPORT static PixelMap *DecodeTlv(std::vector<uint8_t> &buff); 159 160 #ifdef IMAGE_COLORSPACE_FLAG 161 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----begin---- 162 NATIVEEXPORT void InnerSetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace); 163 NATIVEEXPORT OHOS::ColorManager::ColorSpace InnerGetGrColorSpace(); InnerGetGrColorSpacePtr()164 NATIVEEXPORT std::shared_ptr<OHOS::ColorManager::ColorSpace> InnerGetGrColorSpacePtr() 165 { 166 return grColorSpace_; 167 } 168 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----end------- 169 #endif 170 IsAstc()171 NATIVEEXPORT bool IsAstc() 172 { 173 return isAstc_; 174 } 175 SetAstc(bool isAstc)176 NATIVEEXPORT void SetAstc(bool isAstc) 177 { 178 isAstc_ = isAstc; 179 } 180 GetAstcRealSize(Size & size)181 NATIVEEXPORT void GetAstcRealSize(Size &size) 182 { 183 size = astcrealSize_; 184 } SetAstcRealSize(Size size)185 NATIVEEXPORT void SetAstcRealSize(Size size) 186 { 187 astcrealSize_ = size; 188 } 189 GetTransformData(TransformData & transformData)190 NATIVEEXPORT void GetTransformData(TransformData &transformData) 191 { 192 transformData = transformData_; 193 } 194 SetTransformData(const TransformData & transformData)195 NATIVEEXPORT void SetTransformData(const TransformData &transformData) 196 { 197 transformData_ = transformData; 198 } 199 200 private: 201 static constexpr uint8_t TLV_VARINT_BITS = 7; 202 static constexpr uint8_t TLV_VARINT_MASK = 0x7F; 203 static constexpr uint8_t TLV_VARINT_MORE = 0x80; 204 static constexpr uint8_t TLV_END = 0x00; 205 static constexpr uint8_t TLV_IMAGE_WIDTH = 0x01; 206 static constexpr uint8_t TLV_IMAGE_HEIGHT = 0x02; 207 static constexpr uint8_t TLV_IMAGE_PIXELFORMAT = 0x03; 208 static constexpr uint8_t TLV_IMAGE_COLORSPACE = 0x04; 209 static constexpr uint8_t TLV_IMAGE_ALPHATYPE = 0x05; 210 static constexpr uint8_t TLV_IMAGE_BASEDENSITY = 0x06; 211 static constexpr uint8_t TLV_IMAGE_ALLOCATORTYPE = 0x07; 212 static constexpr uint8_t TLV_IMAGE_DATA = 0x08; 213 static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M 214 static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024; // 32k 215 friend class ImageSource; 216 static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 217 static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 218 static bool ARGB8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 219 static bool RGBA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 220 static bool BGRA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 221 static bool RGB888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 222 static bool CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride, 223 const InitializationOptions &opts); 224 static void UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat &pixelFormat, uint8_t *dstPixels, 225 PixelMap dstPixelMap); 226 static void InitDstImageInfo(const InitializationOptions &opts, const ImageInfo &srcImageInfo, 227 ImageInfo &dstImageInfo); 228 static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap); 229 static bool SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo, 230 const Rect &srcRect, PixelMap &dstPixelMap); 231 static bool IsSameSize(const Size &src, const Size &dst); 232 static bool ScalePixelMap(const Size &targetSize, const Size &dstSize, const ScaleMode &scaleMode, 233 PixelMap &dstPixelMap); 234 bool GetPixelFormatDetail(const PixelFormat format); 235 bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset, 236 const uint32_t &stride, const Rect ®ion); 237 void ReleaseSharedMemory(void *addr, void *context, uint32_t size); SetEditable(bool editable)238 void SetEditable(bool editable) 239 { 240 editable_ = editable; 241 } 242 ResetPixelMap()243 void ResetPixelMap() 244 { 245 rowDataSize_ = 0; 246 pixelBytes_ = 0; 247 colorProc_ = nullptr; 248 } 249 CheckValidParam(int32_t x,int32_t y)250 bool CheckValidParam(int32_t x, int32_t y) 251 { 252 return (data_ == nullptr) || (x >= imageInfo_.size.width) || (x < 0) || (y >= imageInfo_.size.height) || 253 (y < 0) || (pixelsSize_ < static_cast<uint64_t>(rowDataSize_) * imageInfo_.size.height) 254 ? false 255 : true; 256 } 257 258 static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size); 259 bool WriteImageData(Parcel &parcel, size_t size) const; 260 static uint8_t *ReadImageData(Parcel &parcel, int32_t size); 261 static int ReadFileDescriptor(Parcel &parcel); 262 static bool WriteFileDescriptor(Parcel &parcel, int fd); 263 bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo); 264 bool WriteImageInfo(Parcel &parcel) const; 265 void WriteUint8(std::vector<uint8_t> &buff, uint8_t value) const; 266 static uint8_t ReadUint8(std::vector<uint8_t> &buff, int32_t &cursor); 267 uint8_t GetVarintLen(int32_t value) const; 268 void WriteVarint(std::vector<uint8_t> &buff, int32_t value) const; 269 static int32_t ReadVarint(std::vector<uint8_t> &buff, int32_t &cursor); 270 void WriteData(std::vector<uint8_t> &buff, const uint8_t *data, int32_t size) const; 271 static uint8_t *ReadData(std::vector<uint8_t> &buff, int32_t size, int32_t &cursor); 272 static void ReadTlvAttr(std::vector<uint8_t> &buff, ImageInfo &info, int32_t &type, int32_t &size, uint8_t **data); 273 274 uint8_t *data_ = nullptr; 275 // this info SHOULD be the final info for decoded pixelmap, not the original image info 276 ImageInfo imageInfo_; 277 int32_t rowDataSize_ = 0; 278 int32_t rowStride_ = 0; 279 int32_t pixelBytes_ = 0; 280 TransColorProc colorProc_ = nullptr; 281 void *context_ = nullptr; 282 CustomFreePixelMap custFreePixelMap_ = nullptr; 283 AllocatorType allocatorType_ = AllocatorType::HEAP_ALLOC; 284 uint32_t pixelsSize_ = 0; 285 bool editable_ = false; 286 bool useSourceAsResponse_ = false; 287 bool isAstc_ = false; 288 TransformData transformData_ = {1, 1, 0, 0, 0, 0, 0, 0, 0, false, false}; 289 Size astcrealSize_; 290 291 // only used by rosen backend 292 std::shared_ptr<RosenImageWrapper> rosenImageWrapper_; 293 friend class PixelMapRosenUtils; 294 295 #ifdef IMAGE_COLORSPACE_FLAG 296 std::shared_ptr<OHOS::ColorManager::ColorSpace> grColorSpace_ = nullptr; 297 #else 298 std::shared_ptr<uint8_t> grColorSpace_ = nullptr; 299 #endif 300 }; 301 } // namespace Media 302 } // namespace OHOS 303 304 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_ 305