1/*
2 * Copyright (c) 2020-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#include "draw/draw_image.h"
17
18#include "gfx_utils/color.h"
19#include "gfx_utils/graphic_log.h"
20#include "imgdecode/cache_manager.h"
21
22namespace OHOS {
23void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask,
24    const ImageInfo* img, const Style& style, uint8_t opaScale)
25{
26    if (img == nullptr) {
27        return;
28    }
29    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.imageOpa_);
30    uint8_t pxBitSize = DrawUtils::GetPxSizeByColorMode(img->header.colorMode);
31    DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, img->data, opa, pxBitSize,
32                                        static_cast<ColorMode>(img->header.colorMode));
33}
34
35void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask,
36    const char* path, const Style& style, uint8_t opaScale)
37{
38    if (path == nullptr) {
39        return;
40    }
41    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.imageOpa_);
42
43    CacheEntry entry;
44    if (CacheManager::GetInstance().Open(path, style, entry) != RetCode::OK) {
45        return;
46    }
47
48    uint8_t pxBitSize = DrawUtils::GetPxSizeByColorMode(entry.GetImageInfo().header.colorMode);
49    if (entry.InCache()) {
50        DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, entry.GetImgData(), opa, pxBitSize,
51                                            static_cast<ColorMode>(entry.GetImageInfo().header.colorMode));
52    } else {
53        Rect valid = coords;
54        if (!valid.Intersect(valid, mask)) {
55            return;
56        }
57
58        int16_t width = valid.GetWidth();
59        if (width <= 0) {
60            return;
61        }
62        uint8_t* buf = static_cast<uint8_t*>(UIMalloc(static_cast<uint32_t>(width) * ((COLOR_DEPTH >> SHIFT_3) + 1)));
63        if (buf == nullptr) {
64            return;
65        }
66
67        Rect line = valid;
68        line.SetHeight(1);
69        Point start;
70        start.x = valid.GetLeft() - coords.GetLeft();
71        start.y = valid.GetTop() - coords.GetTop();
72        for (int16_t row = valid.GetTop(); row <= valid.GetBottom(); row++) {
73            if (entry.ReadLine(start, width, buf) != RetCode::OK) {
74                CacheManager::GetInstance().Close(path);
75                UIFree(buf);
76                return;
77            }
78            DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, line, mask, buf, opa, pxBitSize,
79                                                static_cast<ColorMode>(entry.GetImageInfo().header.colorMode));
80            line.SetTop(line.GetTop() + 1);
81            line.SetBottom(line.GetBottom() + 1);
82            start.y++;
83        }
84        UIFree(buf);
85    }
86}
87} // namespace OHOS
88