draw_image.cpp 4.2 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "draw/draw_image.h"

Z
zhangguyuan 已提交
18 19
#include "gfx_utils/color.h"
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
20 21 22
#include "imgdecode/cache_manager.h"

namespace OHOS {
N
niulihua 已提交
23
void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask,
M
mamingshuai 已提交
24 25 26 27 28 29
    const ImageInfo* img, const Style& style, uint8_t opaScale)
{
    if (img == nullptr) {
        return;
    }
    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.imageOpa_);
W
wanngtiantian 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    uint8_t pxBitSize = DrawUtils::GetPxSizeByColorMode(img->header.colorMode);
    LutColorMode lutColorMode = LUT_UNKNOW;
    uint8_t size = 0;
    switch (img->header.colorMode) {
        case L1:
            // One index represents 1 bit, 8 : convert to bytes, 1 : 2 color values
            size = (img->dataSize - (img->header.width * img->header.height / 8)) >> 1;
            break;
        case L2:
            // One index represents 2 bit, 4 : convert to bytes, 2 : 4 color values
            size = (img->dataSize - (img->header.width * img->header.height / 4)) >> 2;
            break;
        case L4:
            // One index represents 4 bit, 2 : convert to bytes, 4 : 16 color values
            size = (img->dataSize - (img->header.width * img->header.height / 2)) >> 4;
            break;
        case L8:
            // One index represents 8 bit, 8 : 256 color values
            size = (img->dataSize - (img->header.width * img->header.height)) >> 8;
            break;
        default:
            size = 0;
            break;
    }
    lutColorMode = DrawUtils::GetLutColorModeBySize(size);
N
niulihua 已提交
55
    DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, img->data, opa, pxBitSize,
W
wanngtiantian 已提交
56
                                        static_cast<ColorMode>(img->header.colorMode), lutColorMode);
M
mamingshuai 已提交
57 58
}

N
niulihua 已提交
59
void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask,
M
mamingshuai 已提交
60 61 62 63 64 65 66 67 68 69 70 71
    const char* path, const Style& style, uint8_t opaScale)
{
    if (path == nullptr) {
        return;
    }
    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.imageOpa_);

    CacheEntry entry;
    if (CacheManager::GetInstance().Open(path, style, entry) != RetCode::OK) {
        return;
    }

W
wanngtiantian 已提交
72
    uint8_t pxBitSize = DrawUtils::GetPxSizeByColorMode(entry.GetImageInfo().header.colorMode);
M
mamingshuai 已提交
73
    if (entry.InCache()) {
N
niulihua 已提交
74
        DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, entry.GetImgData(), opa, pxBitSize,
W
wanngtiantian 已提交
75
                                            static_cast<ColorMode>(entry.GetImageInfo().header.colorMode));
M
mamingshuai 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    } else {
        Rect valid = coords;
        if (!valid.Intersect(valid, mask)) {
            return;
        }

        int16_t width = valid.GetWidth();
        if (width <= 0) {
            return;
        }
        uint8_t* buf = static_cast<uint8_t*>(UIMalloc(static_cast<uint32_t>(width) * ((COLOR_DEPTH >> SHIFT_3) + 1)));
        if (buf == nullptr) {
            return;
        }

        Rect line = valid;
        line.SetHeight(1);
        Point start;
        start.x = valid.GetLeft() - coords.GetLeft();
        start.y = valid.GetTop() - coords.GetTop();
        int16_t row;
        for (row = valid.GetTop(); row <= valid.GetBottom(); row++) {
            if (entry.ReadLine(start, width, buf) != RetCode::OK) {
                CacheManager::GetInstance().Close(path);
                UIFree(buf);
                return;
            }
N
niulihua 已提交
103
            DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, line, mask, buf, opa, pxBitSize,
W
wanngtiantian 已提交
104
                                                static_cast<ColorMode>(entry.GetImageInfo().header.colorMode));
M
mamingshuai 已提交
105 106 107 108 109 110 111 112
            line.SetTop(line.GetTop() + 1);
            line.SetBottom(line.GetBottom() + 1);
            start.y++;
        }
        UIFree(buf);
    }
}
} // namespace OHOS