image_load.cpp 6.3 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "imgdecode/image_load.h"
Z
zhangguyuan 已提交
17 18 19
#include "gfx_utils/file.h"
#include "gfx_utils/graphic_log.h"
#include "gfx_utils/mem_api.h"
M
mamingshuai 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

namespace {
const uint8_t BITMAP_ZIP_LEN = 3;
const uint8_t BITMAP_MID_BIT = 1;
const uint8_t BITMAP_LOW_BIT = 2;
const uint32_t BITMAP_ZIP24_FLAG = 0x456789;
const uint32_t BITMAP_ZIP_FLAG = 0x23456789;
const uint32_t BITMAP_ALPHA_MASK = 0xFF000000;
const uint32_t BITMAP_MAXCON_PIXNUM = 0xCB100;
const uint32_t MOVE_HIGH = 16;
const uint32_t MOVE_LOW = 8;
} // namespace

namespace OHOS {
bool ImageLoad::CreateImage(ImageInfo& imageInfo)
{
    uint32_t bytePerPixel = 4;
    ImageHeader& imageHeader = imageInfo.header;

    switch (imageHeader.colorMode) {
        case ARGB8888:
            bytePerPixel = 4; // 4 bytes per pixel
            break;
        case RGB888:
            bytePerPixel = 3; // 3 bytes per pixel
            break;
        case RGB565:
            bytePerPixel = 2; // 2 bytes per pixel
            break;
        default:
50
            GRAPHIC_LOGE("CreateImage invalid colorMode.");
M
mamingshuai 已提交
51 52 53 54 55 56
            return false;
    }

    imageInfo.dataSize = imageHeader.width * imageHeader.height * bytePerPixel;
    imageInfo.data = static_cast<uint8_t*>(ImageCacheMalloc(imageInfo));
    if (imageInfo.data == nullptr) {
57
        GRAPHIC_LOGE("ImageCacheMalloc error.");
M
mamingshuai 已提交
58 59 60 61 62 63 64 65 66
        return false;
    }

    return true;
}

bool ImageLoad::UncompressImageInZip(ImageInfo& imageInfo, uint8_t* buffer, uint32_t size)
{
    if (!CreateImage(imageInfo)) {
67
        GRAPHIC_LOGE("Create image error.");
M
mamingshuai 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        return false;
    }

    if (imageInfo.header.colorMode == RGB888) {
        return Unzip24Image(buffer, size, imageInfo);
    } else {
        return UnzipImage(buffer, size, imageInfo);
    }
}

bool ImageLoad::UnzipImage(uint8_t* imageBuffer, uint32_t size, ImageInfo& imageInfo)
{
    uint32_t value = 0;
    uint32_t count = 0;

    if ((imageBuffer == nullptr) || (size == 0)) {
84
        GRAPHIC_LOGE("imageHeader is null.");
M
mamingshuai 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        return false;
    }

    uint32_t* source = reinterpret_cast<uint32_t*>(imageBuffer);
    uint32_t* sourceEnd = reinterpret_cast<uint32_t*>(imageBuffer + size);
    uint32_t* dest = nullptr;
    uint32_t* destEnd = nullptr;

    dest = reinterpret_cast<uint32_t*>(const_cast<uint8_t*>(imageInfo.data));
    destEnd = reinterpret_cast<uint32_t*>(const_cast<uint8_t*>(imageInfo.data) + imageInfo.dataSize);

    while ((source < sourceEnd) && (dest < destEnd)) {
        if (*source != BITMAP_ZIP_FLAG) {
            *dest++ = *source++;
        } else {
            source++;
            value = *source++;
            count = *source++;
            if (destEnd < count + dest) {
                break;
            }

            while (count--) {
                *dest++ = value;
            }
        }
    }

    if (dest == destEnd) {
        return true;
    }
    ImageCacheFree(imageInfo);
117
    imageInfo.data = nullptr;
M
mamingshuai 已提交
118 119 120 121 122 123
    return false;
}

bool ImageLoad::Unzip24Image(uint8_t* imageBuffer, uint32_t size, ImageInfo& imageInfo)
{
    if ((imageBuffer == nullptr) || (size == 0)) {
124
        GRAPHIC_LOGE("imageHeader is null.");
M
mamingshuai 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        return false;
    }

    uint8_t* source = reinterpret_cast<uint8_t*>(imageBuffer);
    uint8_t* sourceEnd = reinterpret_cast<uint8_t*>(imageBuffer + size);
    uint32_t* dest = reinterpret_cast<uint32_t*>(const_cast<uint8_t*>(imageInfo.data));
    uint32_t* destEnd = reinterpret_cast<uint32_t*>(const_cast<uint8_t*>(imageInfo.data) + imageInfo.dataSize);
    while ((source < sourceEnd) && (dest < destEnd)) {
        uint32_t count = 0;
        uint32_t value = 0;

        // Little endian
        value = ((*source)) + (*(source + BITMAP_MID_BIT) << MOVE_LOW) + (*(source + BITMAP_LOW_BIT) << MOVE_HIGH);
        source = source + BITMAP_ZIP_LEN;
        if (value != BITMAP_ZIP24_FLAG) {
            *dest = value | BITMAP_ALPHA_MASK;
            dest++;
        } else {
            value = 0;
            value = ((*source)) + (*(source + BITMAP_MID_BIT) << MOVE_LOW) + (*(source + BITMAP_LOW_BIT) << MOVE_HIGH);
            source = source + BITMAP_ZIP_LEN;

            count = 0;
            count = ((*source)) + (*(source + BITMAP_MID_BIT) << MOVE_LOW) + (*(source + BITMAP_LOW_BIT) << MOVE_HIGH);
            source = source + BITMAP_ZIP_LEN;

            if (count > BITMAP_MAXCON_PIXNUM) {
                *dest = BITMAP_ZIP24_FLAG | BITMAP_ALPHA_MASK;
                dest++;
                *dest = value | BITMAP_ALPHA_MASK;
                dest++;
                *dest = count | BITMAP_ALPHA_MASK;
                dest++;
                continue;
            }
            if (static_cast<uintptr_t>(destEnd - dest) < static_cast<uintptr_t>(count)) {
                break;
            }
            while (count--) {
                *dest = value | BITMAP_ALPHA_MASK;
                dest++;
            }
        }
    }

    if (dest == destEnd) {
        return true;
    }
    ImageCacheFree(imageInfo);
174
    imageInfo.data = nullptr;
M
mamingshuai 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    return false;
}

bool ImageLoad::UnZip2ImageInfo(ImageInfo& imageInfo, uint8_t* buffer, uint32_t size)
{
    switch (imageInfo.header.compressMode) {
        case COMPRESS_MODE__ZIP_ALG:
            return UncompressImageInZip(imageInfo, buffer, size);
        default:
            return false;
    }
}

bool ImageLoad::GetImageInfo(int32_t fd, uint32_t size, ImageInfo& imageInfo)
{
    if (size == 0) {
        return false;
    }

    uint8_t* buffer = reinterpret_cast<uint8_t*>(UIMalloc(size));
    if (buffer == nullptr) {
        return false;
    }

    if (read(fd, buffer, size) != static_cast<int32_t>(size)) {
        UIFree(buffer);
201
        GRAPHIC_LOGE("SeekImageFile error.");
M
mamingshuai 已提交
202 203 204 205 206 207 208
        return false;
    }
    bool ret = UnZip2ImageInfo(imageInfo, buffer, size);
    UIFree(buffer);
    return ret;
}
} // namespace OHOS