1 /*
2 * Copyright (C) 2025 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 #include <memory>
17
18 #include "image_source.h"
19 #include "image_type.h"
20 #include "media_errors.h"
21
22 #include "base/image_data.h"
23 #include "base/log.h"
24 #include "base/pixel_map.h"
25
26 namespace OHOS {
27 namespace Ace {
28 namespace Drawable {
29 class ImageDataImpl final : public ImageData {
30 public:
31 ImageDataImpl() = default;
32 ~ImageDataImpl() override = default;
33
ImageDataImpl(std::unique_ptr<Media::ImageSource> && imageSource)34 explicit ImageDataImpl(std::unique_ptr<Media::ImageSource>&& imageSource) : imageSource_(std::move(imageSource)) {}
35
36 std::shared_ptr<PixelMap> CreatePixelMap(const DecodeParmas& params) override;
37
38 private:
39 std::unique_ptr<Media::ImageSource> imageSource_;
40 };
41
Create(const uint8_t * data,uint32_t size)42 std::shared_ptr<ImageData> ImageData::Create(const uint8_t* data, uint32_t size)
43 {
44 uint32_t errorCode;
45 Media::SourceOptions options;
46 auto src = Media::ImageSource::CreateImageSource(data, size, options, errorCode);
47 if (errorCode != Media::SUCCESS) {
48 HILOGE("create image source failed by uint8 data, its size = %{public}d", size);
49 return nullptr;
50 }
51 return std::make_shared<ImageDataImpl>(std::move(src));
52 }
53
CreatePixelMap(const DecodeParmas & params)54 std::shared_ptr<PixelMap> ImageDataImpl::CreatePixelMap(const DecodeParmas& params)
55 {
56 uint32_t errorCode;
57 Media::DecodeOptions decodeOpts;
58 auto pixelMap = imageSource_->CreatePixelMap(decodeOpts, errorCode);
59 if (errorCode != Media::SUCCESS) {
60 HILOGE("create pixel map failed");
61 return nullptr;
62 }
63 return PixelMap::Create(std::move(pixelMap));
64 }
65 } // namespace Drawable
66 } // namespace Ace
67 } // namespace OHOS
68