1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bridge/cj_frontend/cppview/canvas_image_data.h"
17
18 #include "core/pipeline/pipeline_base.h"
19 namespace OHOS::Ace::Framework {
20 constexpr double DIFF = 1e-10;
21 constexpr int32_t PIXEL_SIZE = 4;
22
NativeImageData()23 NativeImageData::NativeImageData() : FFIData() {}
24
~NativeImageData()25 NativeImageData::~NativeImageData()
26 {
27 LOGI("Native ImageData Destroyed: %{public}" PRId64, GetID());
28 }
29
GetImageDataSize(double width,double height,int32_t & finalWidth,int32_t & finalHeight)30 bool NativeImageData::GetImageDataSize(double width, double height, int32_t& finalWidth, int32_t& finalHeight)
31 {
32 double density = GetDensity();
33 width *= density;
34 height *= density;
35 if (NonPositive(width) || NonPositive(height)) {
36 TAG_LOGE(AceLogTag::ACE_CANVAS,
37 "Failed to construct 'ImageData': The input 'width' or 'height' is non-positive number or not number.");
38 return false;
39 }
40 // Integer Overflow.
41 width += DIFF;
42 height += DIFF;
43 if ((width > INT32_MAX) || (height > INT32_MAX) || ((width > 0) && (height > (INT32_MAX / width / PIXEL_SIZE)))) {
44 return false;
45 }
46 finalWidth = static_cast<int32_t>(width);
47 finalHeight = static_cast<int32_t>(height);
48 return true;
49 }
50
GetHeight() const51 int32_t NativeImageData::GetHeight() const
52 {
53 return height_;
54 }
55
GetWidth() const56 int32_t NativeImageData::GetWidth() const
57 {
58 return width_;
59 }
60
GetData() const61 std::vector<uint8_t> NativeImageData::GetData() const
62 {
63 return data;
64 }
65
GetDensity()66 double NativeImageData::GetDensity()
67 {
68 double density = PipelineBase::GetCurrentDensity();
69 return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density)) ? density : 1.0;
70 }
71 } // namespace OHOS::Ace::Framework