1/*
2 * Copyright (c) 2022-2022 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 "updater_ui_tools.h"
17#include "common/screen.h"
18#include "draw/draw_utils.h"
19#include "gfx_utils/file.h"
20#include "gfx_utils/graphic_math.h"
21#include "log/log.h"
22
23using namespace OHOS;
24
25namespace Updater {
26constexpr uint8_t BITMAP_HEADER_SIZE = 54; /* 54 is bitmap header size */
27
28void UpdaterUiTools::SaveUxBuffToFile(const std::string &filePath)
29{
30    ImageInfo imageBit {};
31    if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
32        LOG(ERROR) << "uxtools get bitmap failed";
33        return;
34    }
35
36    int32_t fd = open(filePath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
37    if (fd == -1) {
38        LOG(ERROR) << "uxtools open file failed";
39        ImageCacheFree(imageBit);
40        return;
41    }
42
43    uint8_t sizeByColorMode = DrawUtils::GetByteSizeByColorMode(OHOS::ColorMode::ARGB8888);
44    uint16_t bfType = 0x4D42;
45    struct BitmapInfoHeader bitmapInfo = {
46        .bfSize = imageBit.dataSize + BITMAP_HEADER_SIZE,
47        .bfOffBits = BITMAP_HEADER_SIZE,
48        .biSize = 40, /* 40 is bitmap information header size */
49        .biWidth = imageBit.header.width,
50        .biHeight = -imageBit.header.height,
51        .biPlanes = 1,
52        .biBitCount = sizeByColorMode * 8, /* 8 is uint8_t bit */
53        .biSizeImage = imageBit.dataSize,
54    };
55    if (write(fd, &bfType, sizeof(bfType)) > 0 && write(fd, &bitmapInfo, sizeof(bitmapInfo)) > 0 &&
56        write(fd, imageBit.data, imageBit.dataSize) > 0) {
57        LOG(INFO) << "uxtools save file done";
58    } else {
59        LOG(ERROR) << "uxtools save file failed";
60    }
61    ImageCacheFree(imageBit);
62    close(fd);
63
64    return;
65}
66} // namespace Updater
67