file_img_decoder.cpp 4.6 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */

Z
zhangguyuan 已提交
16 17
#include "gfx_utils/file.h"
#include "gfx_utils/mem_api.h"
M
mamingshuai 已提交
18 19 20 21
#include "imgdecode/file_img_decoder.h"
#include "imgdecode/image_load.h"

namespace OHOS {
Z
zhdengc 已提交
22 23 24 25 26 27
FileImgDecoder& FileImgDecoder::GetInstance()
{
    static FileImgDecoder instance;
    return instance;
}

M
mamingshuai 已提交
28 29 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
RetCode FileImgDecoder::Open(ImgResDsc& dsc)
{
#ifdef _WIN32
    int32_t fd = open(dsc.path, O_RDONLY | O_BINARY);
#else
    int32_t fd = open(dsc.path, O_RDONLY);
#endif
    if (fd == -1) {
        return RetCode::FAIL;
    }
    dsc.fd = fd;

    dsc.imgInfo.data = nullptr;
    dsc.inCache_ = false;
    uint8_t colorMode = dsc.imgInfo.header.colorMode;
    if (IsImgValidMode(colorMode)) {
        return RetCode::OK;
    } else {
        return RetCode::FAIL;
    }
}

RetCode FileImgDecoder::Close(ImgResDsc& dsc)
{
    if (dsc.imgInfo.data != nullptr) {
        ImageCacheFree(dsc.imgInfo);
        dsc.imgInfo.data = nullptr;
    }
    if (dsc.fd && (dsc.fd != -1)) {
        close(dsc.fd);
        dsc.fd = -1;
    }

    return RetCode::OK;
}

RetCode FileImgDecoder::GetHeader(ImgResDsc& dsc)
{
    int32_t fd;
    int32_t readCount;
#ifdef _WIN32
    fd = open(dsc.path, O_BINARY);
#else
    fd = open(dsc.path, O_RDONLY);
#endif
    if (fd == -1) {
        return RetCode::FAIL;
    }

    readCount = read(fd, &dsc.imgInfo.header, sizeof(ImageHeader));
    close(fd);
    dsc.fd = -1;
    if (readCount != sizeof(ImageHeader)) {
        dsc.imgInfo.header.width = 0;
        dsc.imgInfo.header.height = 0;
L
liuyuxiang-bear 已提交
83
        dsc.imgInfo.header.colorMode = UNKNOWN;
M
mamingshuai 已提交
84 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
        return RetCode::FAIL;
    }

    return RetCode::OK;
}

RetCode FileImgDecoder::ReadLine(ImgResDsc& dsc, const Point& start, int16_t len, uint8_t* buf)
{
    if (IsImgValidMode(dsc.imgInfo.header.colorMode)) {
        return ReadLineTrueColor(dsc, start, len, buf);
    }
    return RetCode::FAIL;
}

RetCode FileImgDecoder::ReadToCache(ImgResDsc& dsc)
{
    struct stat info;
    if (!dsc.inCache_) {
        lseek(dsc.fd, 0, SEEK_SET);
        int32_t readCount = read(dsc.fd, &dsc.imgInfo.header, sizeof(ImageHeader));
        if (readCount != sizeof(ImageHeader)) {
            return RetCode::FAIL;
        }

        int32_t ret = fstat(dsc.fd, &info);
        if (ret != 0) {
            return RetCode::FAIL;
        }
        uint32_t pxCount = info.st_size - readCount;
        if (dsc.imgInfo.data != nullptr) {
            ImageCacheFree(dsc.imgInfo);
115
            dsc.imgInfo.data = nullptr;
M
mamingshuai 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        }

        bool readSuccess = false;
        if (dsc.imgInfo.header.compressMode != COMPRESS_MODE_NONE) {
            readSuccess = ImageLoad::GetImageInfo(dsc.fd, pxCount, dsc.imgInfo);
        } else {
            dsc.imgInfo.dataSize = pxCount;
            dsc.imgInfo.data = reinterpret_cast<uint8_t*>(ImageCacheMalloc(dsc.imgInfo));
            if (dsc.imgInfo.data == nullptr) {
                return RetCode::OK;
            }
            uint8_t* tmp = const_cast<uint8_t*>(dsc.imgInfo.data);
            readSuccess = (static_cast<int32_t>(pxCount) == read(dsc.fd, reinterpret_cast<void*>(tmp), pxCount));
        }
        if (!readSuccess) {
            ImageCacheFree(dsc.imgInfo);
132
            dsc.imgInfo.data = nullptr;
M
mamingshuai 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            dsc.imgInfo.dataSize = 0;
            close(dsc.fd);
            dsc.fd = -1;
            return RetCode::OK;
        }
        dsc.inCache_ = true;
        close(dsc.fd);
        dsc.fd = -1;
    }

    return RetCode::OK;
}

RetCode FileImgDecoder::ReadLineTrueColor(ImgResDsc& dsc, const Point& start, int16_t len, uint8_t* buf)
{
W
wanngtiantian 已提交
148
    uint8_t pxSizeInBit = DrawUtils::GetPxSizeByColorMode(dsc.imgInfo.header.colorMode);
M
mamingshuai 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    off_t res;

    uint32_t pos = ((start.y * dsc.imgInfo.header.width + start.x) * pxSizeInBit) >> BYTE_TO_BIT_SHIFT;
    pos += sizeof(ImageHeader); /* Skip the header */
    res = lseek(dsc.fd, pos, SEEK_SET);
    if (res == -1) {
        return RetCode::FAIL;
    }
    uint32_t btr = len * (pxSizeInBit >> BYTE_TO_BIT_SHIFT);
    int32_t br = read(dsc.fd, buf, btr);
    if ((br == -1) || (btr != static_cast<uint32_t>(br))) {
        return RetCode::FAIL;
    }

    return RetCode::OK;
}
} // namespace OHOS