image.cpp 14.3 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 "common/image.h"
#include "common/image_decode_ability.h"
#include "draw/draw_image.h"
Z
zhangguyuan 已提交
19 20
#include "gfx_utils/file.h"
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include "imgdecode/cache_manager.h"
#if ENABLE_JPEG_AND_PNG
#include "jpeglib.h"
#include "png.h"
#endif
#include "securec.h"

namespace OHOS {
Image::Image() : imageInfo_(nullptr), path_(nullptr), srcType_(IMG_SRC_UNKNOWN), mallocFlag_(false) {}

Image::~Image()
{
    if (srcType_ == IMG_SRC_FILE) {
        CacheManager::GetInstance().Close(path_);
    }
    if (imageInfo_ != nullptr) {
        if (mallocFlag_) {
            if (imageInfo_->data != nullptr) {
                UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo_->data)));
            }
            mallocFlag_ = false;
        }
        UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo_)));
        imageInfo_ = nullptr;
    }
    if (path_ != nullptr) {
        UIFree(reinterpret_cast<void*>(const_cast<char*>(path_)));
        path_ = nullptr;
    }
    srcType_ = IMG_SRC_UNKNOWN;
}

void Image::GetHeader(ImageHeader& header) const
{
    if ((srcType_ == IMG_SRC_VARIABLE) && (imageInfo_ != nullptr)) {
        header = imageInfo_->header;
    } else if ((srcType_ == IMG_SRC_FILE) && (path_ != nullptr)) {
        CacheManager::GetInstance().GetImageHeader(path_, header);
    }
}

#if ENABLE_JPEG_AND_PNG
Image::ImageType Image::CheckImgType(const char* src)
{
    char buf[IMG_BYTES_TO_CHECK] = {0};
#ifdef _WIN32
    int32_t fd = open(src, O_RDONLY | O_BINARY);
#else
    int32_t fd = open(src, O_RDONLY);
#endif
    if (fd < 0) {
72
        GRAPHIC_LOGE("can't open %s\n", src);
M
mamingshuai 已提交
73 74 75 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 174 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        return IMG_UNKNOWN;
    }
    if (read(fd, buf, IMG_BYTES_TO_CHECK) != IMG_BYTES_TO_CHECK) {
        close(fd);
        return IMG_UNKNOWN;
    }
    close(fd);
    if (!png_sig_cmp(reinterpret_cast<png_const_bytep>(buf), 0, IMG_BYTES_TO_CHECK)) {
        return IMG_PNG;
    // 0xFF 0xD8: JPEG file's header
    } else if ((static_cast<uint8_t>(buf[0]) == 0xFF) && (static_cast<uint8_t>(buf[1]) == 0xD8)) {
        return IMG_JPEG;
    }
    return IMG_UNKNOWN;
}
#endif

bool Image::SetStandardSrc(const char* src)
{
    if (src == nullptr) {
        return false;
    }
    const char* ptr = strrchr(src, '.');
    if (ptr == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }

#if ENABLE_JPEG_AND_PNG
    ImageType imageType = CheckImgType(src);
    if (imageType == IMG_PNG) {
        return SetPNGSrc(src);
    } else if (imageType == IMG_JPEG) {
        return SetJPEGSrc(src);
    }
#endif

    size_t strLen = strlen(src) + 1;
    char* imagePath = static_cast<char*>(UIMalloc(static_cast<uint32_t>(strLen)));
    if (imagePath == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }

    if (strcpy_s(imagePath, strLen, src) != EOK) {
        UIFree(reinterpret_cast<void*>(imagePath));
        imagePath = nullptr;
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    path_ = imagePath;
    srcType_ = IMG_SRC_FILE;
    return true;
}

bool Image::SetLiteSrc(const char* src)
{
    if (src == nullptr) {
        return false;
    }
    const char* ptr = strrchr(src, '.');
    if (ptr == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }

    size_t strLen = strlen(src) + 1;
    char* imagePath = static_cast<char*>(UIMalloc(static_cast<uint32_t>(strLen)));
    if (imagePath == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    if (IsImgValid(ptr)) {
        const char* suffixName = "bin";
        if (memcpy_s(imagePath, strLen, src, strLen) != EOK) {
            UIFree(reinterpret_cast<void*>(imagePath));
            imagePath = nullptr;
            srcType_ = IMG_SRC_UNKNOWN;
            return false;
        }
        (ptr - src + imagePath)[1] = '\0'; // remove suffix
        if (strcat_s(imagePath, strLen, suffixName) != EOK) {
            UIFree(reinterpret_cast<void*>(imagePath));
            imagePath = nullptr;
            srcType_ = IMG_SRC_UNKNOWN;
            return false;
        }
    } else {
        if (memcpy_s(imagePath, strLen, src, strLen) != EOK) {
            UIFree(reinterpret_cast<void*>(imagePath));
            imagePath = nullptr;
            srcType_ = IMG_SRC_UNKNOWN;
            return false;
        }
    }
    path_ = imagePath;
    srcType_ = IMG_SRC_FILE;
    return true;
}

bool Image::SetSrc(const char* src)
{
    if (path_ != nullptr) {
        UIFree(reinterpret_cast<void*>(const_cast<char*>(path_)));
        path_ = nullptr;
    }

    if (src != nullptr) {
        uint32_t imageType = ImageDecodeAbility::GetInstance().GetImageDecodeAbility();
        if (((imageType & IMG_SUPPORT_JPEG) == IMG_SUPPORT_JPEG) ||
            ((imageType & IMG_SUPPORT_PNG) == IMG_SUPPORT_PNG)) {
            return SetStandardSrc(src);
        }
        return SetLiteSrc(src);
    } else {
        path_ = src;
        srcType_ = IMG_SRC_UNKNOWN;
    }
    return true;
}

bool Image::SetSrc(const ImageInfo* src)
{
    if (imageInfo_ != nullptr) {
        if (mallocFlag_) {
            if (imageInfo_->data != nullptr) {
                UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo_->data)));
            }
            mallocFlag_ = false;
        }
        UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo_)));
        imageInfo_ = nullptr;
    }

    if (src != nullptr) {
        imageInfo_ = static_cast<ImageInfo*>(UIMalloc(static_cast<uint32_t>(sizeof(ImageInfo))));
        if (imageInfo_ == nullptr) {
            srcType_ = IMG_SRC_UNKNOWN;
            return false;
        }

        if (memcpy_s(const_cast<ImageInfo*>(imageInfo_), sizeof(ImageInfo), src, sizeof(ImageInfo)) != EOK) {
            srcType_ = IMG_SRC_UNKNOWN;
            return false;
        }

        srcType_ = IMG_SRC_VARIABLE;
    } else {
        imageInfo_ = src;
        srcType_ = IMG_SRC_UNKNOWN;
    }
    return true;
}

N
niulihua 已提交
227 228 229 230 231
void Image::DrawImage(BufferInfo& gfxDstBuffer,
                      const Rect& coords,
                      const Rect& mask,
                      const Style& style,
                      uint8_t opaScale) const
M
mamingshuai 已提交
232 233
{
    if (srcType_ == IMG_SRC_VARIABLE) {
N
niulihua 已提交
234
        DrawImage::DrawCommon(gfxDstBuffer, coords, mask, imageInfo_, style, opaScale);
M
mamingshuai 已提交
235
    } else if (srcType_ == IMG_SRC_FILE) {
N
niulihua 已提交
236
        DrawImage::DrawCommon(gfxDstBuffer, coords, mask, path_, style, opaScale);
M
mamingshuai 已提交
237
    } else {
238
        GRAPHIC_LOGE("Image::DrawImage:: failed with error srctype!\n");
M
mamingshuai 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }
}

#if ENABLE_JPEG_AND_PNG
bool Image::SetPNGSrc(const char* src)
{
    FILE* infile = nullptr;
    png_bytep* rowPointer = nullptr;
    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if (png == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    png_infop info = png_create_info_struct(png);
    if (info == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        png_destroy_read_struct(&png, &info, nullptr);
        return false;
    }
    if ((infile = fopen(src, "rb")) == nullptr) {
259
        GRAPHIC_LOGE("can't open %s\n", src);
M
mamingshuai 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        srcType_ = IMG_SRC_UNKNOWN;
        png_destroy_read_struct(&png, &info, nullptr);
        return false;
    }
    png_init_io(png, infile);
    png_read_info(png, info);

    uint8_t pixelByteSize = DrawUtils::GetPxSizeByColorMode(ARGB8888) >> 3; // 3: Shift right 3 bits
    uint16_t width = png_get_image_width(png, info);
    uint16_t height = png_get_image_height(png, info);
    uint8_t colorType = png_get_color_type(png, info);
    uint8_t bitDepth = png_get_bit_depth(png, info);
    uint32_t dataSize = height * width * pixelByteSize;

    if ((colorType == PNG_COLOR_TYPE_GRAY) && (bitDepth < 8)) { // 8: Expand grayscale images to the full 8 bits
        png_set_expand_gray_1_2_4_to_8(png);
    }
    if ((colorType == PNG_COLOR_TYPE_GRAY) || (colorType == PNG_COLOR_TYPE_GRAY_ALPHA)) {
        png_set_gray_to_rgb(png);
    }
    if (colorType == PNG_COLOR_TYPE_PALETTE) {
        png_set_palette_to_rgb(png);
    }
    if (bitDepth == 16) { // 16: Chop 16-bit depth images to 8-bit depth
        png_set_strip_16(png);
    }
    if (png_get_valid(png, info, PNG_INFO_tRNS)) {
        png_set_tRNS_to_alpha(png);
    }
    if (!(colorType & PNG_COLOR_MASK_ALPHA)) {
        png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
    }
    png_set_interlace_handling(png);
    png_read_update_info(png, info);

    rowPointer = static_cast<png_bytep*>(UIMalloc(sizeof(png_bytep) * height));
    if (rowPointer == nullptr) {
        srcType_ = IMG_SRC_UNKNOWN;
        fclose(infile);
        png_destroy_read_struct(&png, &info, nullptr);
        return false;
    }
    for (uint16_t y = 0; y < height; y++) {
        rowPointer[y] = static_cast<png_byte*>(UIMalloc(png_get_rowbytes(png, info)));
        if (rowPointer[y] == nullptr) {
            for (uint16_t i = 0; i < y; i++) {
                UIFree(rowPointer[i]);
                rowPointer[i] = nullptr;
            }
            fclose(infile);
            UIFree(rowPointer);
            srcType_ = IMG_SRC_UNKNOWN;
            png_destroy_read_struct(&png, &info, nullptr);
            return false;
        }
    }
    png_read_image(png, rowPointer);
    fclose(infile);
    png_destroy_read_struct(&png, &info, nullptr);
    ImageInfo* imgInfo = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
    if (imgInfo == nullptr) {
        for (uint16_t i = 0; i < height; i++) {
            UIFree(rowPointer[i]);
            rowPointer[i] = nullptr;
        }
        UIFree(rowPointer);
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    uint8_t* srcData = static_cast<uint8_t*>(UIMalloc(dataSize));
    if (srcData == nullptr) {
        for (uint16_t i = 0; i < height; i++) {
            UIFree(rowPointer[i]);
            rowPointer[i] = nullptr;
        }
        UIFree(rowPointer);
        UIFree(imgInfo);
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    uint32_t n = 0;
    for (uint16_t y = 0; y < height; y++) {
        png_bytep row = rowPointer[y];
        for (uint16_t x = 0; x < width * pixelByteSize; x += pixelByteSize) {
            srcData[n++] = row[x + 2]; // 2: B channel
            srcData[n++] = row[x + 1]; // 1: G channel
            srcData[n++] = row[x + 0]; // 0: R channel
            srcData[n++] = row[x + 3]; // 3: Alpha channel
        }
        UIFree(row);
        row = nullptr;
    }
    UIFree(rowPointer);

    imgInfo->header.width = width;
    imgInfo->header.height = height;
    imgInfo->header.colorMode = ARGB8888;
    imgInfo->dataSize = dataSize;
    imgInfo->data = srcData;

    if (imageInfo_ != nullptr) {
        if (mallocFlag_) {
            if (imageInfo_->data != nullptr) {
            UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo_->data)));
            }
            mallocFlag_ = false;
        }
        UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo_)));
        imageInfo_ = nullptr;
    }
    imageInfo_ = imgInfo;
    mallocFlag_ = true;
    srcType_ = IMG_SRC_VARIABLE;
    return true;
}

bool Image::SetJPEGSrc(const char* src)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE* infile = nullptr;

    if ((infile = fopen(src, "rb")) == nullptr) {
383
        GRAPHIC_LOGE("can't open %s\n", src);
M
mamingshuai 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, infile);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    uint8_t pixelByteSize = DrawUtils::GetPxSizeByColorMode(ARGB8888) >> 3; // 3: Shift right 3 bits
    uint16_t width = cinfo.output_width;
    uint16_t height = cinfo.output_height;
    uint32_t dataSize = width * height * pixelByteSize;
    uint16_t rowStride = cinfo.output_width * pixelByteSize;
    JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)(reinterpret_cast<j_common_ptr>(&cinfo), JPOOL_IMAGE, rowStride,
                                                   1); // 1: one-row-high array
    ImageInfo* imgInfo = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
    if (imgInfo == nullptr) {
        jpeg_finish_decompress(&cinfo);
        jpeg_destroy_decompress(&cinfo);
        fclose(infile);
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    uint8_t* srcData = static_cast<uint8_t*>(UIMalloc(dataSize));
    if (srcData == nullptr) {
        jpeg_finish_decompress(&cinfo);
        jpeg_destroy_decompress(&cinfo);
        fclose(infile);
        UIFree(imgInfo);
        srcType_ = IMG_SRC_UNKNOWN;
        return false;
    }
    uint32_t n = 0;
    while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines(&cinfo, buffer, 1);       // 1: read one line each time
        for (uint16_t x = 0; x < width * 3; x += 3) { // 3: color components per pixel
            srcData[n++] = buffer[0][x + 2];          // 2: B channel
            srcData[n++] = buffer[0][x + 1];          // 1: G channel
            srcData[n++] = buffer[0][x + 0];          // 0: R channel
            srcData[n++] = 255;                       // 255: set alpha channel
        }
    }
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);

    imgInfo->header.width = width;
    imgInfo->header.height = height;
    imgInfo->header.colorMode = ARGB8888;
    imgInfo->dataSize = dataSize;
    imgInfo->data = srcData;

    if (imageInfo_ != nullptr) {
        if (mallocFlag_) {
            if (imageInfo_->data != nullptr) {
                UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo_->data)));
            }
            mallocFlag_ = false;
        }
        UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo_)));
        imageInfo_ = nullptr;
    }
    imageInfo_ = imgInfo;
    mallocFlag_ = true;
    srcType_ = IMG_SRC_VARIABLE;
    return true;
}
#endif
} // namespace OHOS