/* * 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" #include "gfx_utils/color.h" #include "gfx_utils/graphic_log.h" #include "imgdecode/cache_manager.h" namespace OHOS { void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask, const ImageInfo* img, const Style& style, uint8_t opaScale) { if (img == nullptr) { return; } OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.imageOpa_); 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); DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, img->data, opa, pxBitSize, static_cast(img->header.colorMode), lutColorMode); } void DrawImage::DrawCommon(BufferInfo& gfxDstBuffer, const Rect& coords, const Rect& mask, 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; } uint8_t pxBitSize = DrawUtils::GetPxSizeByColorMode(entry.GetImageInfo().header.colorMode); if (entry.InCache()) { DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, coords, mask, entry.GetImgData(), opa, pxBitSize, static_cast(entry.GetImageInfo().header.colorMode)); } else { Rect valid = coords; if (!valid.Intersect(valid, mask)) { return; } int16_t width = valid.GetWidth(); if (width <= 0) { return; } uint8_t* buf = static_cast(UIMalloc(static_cast(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; } DrawUtils::GetInstance()->DrawImage(gfxDstBuffer, line, mask, buf, opa, pxBitSize, static_cast(entry.GetImageInfo().header.colorMode)); line.SetTop(line.GetTop() + 1); line.SetBottom(line.GetBottom() + 1); start.y++; } UIFree(buf); } } } // namespace OHOS