1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "updater_ui_tools.h"
17fb299fa2Sopenharmony_ci#include "common/screen.h"
18fb299fa2Sopenharmony_ci#include "draw/draw_utils.h"
19fb299fa2Sopenharmony_ci#include "gfx_utils/file.h"
20fb299fa2Sopenharmony_ci#include "gfx_utils/graphic_math.h"
21fb299fa2Sopenharmony_ci#include "log/log.h"
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_ciusing namespace OHOS;
24fb299fa2Sopenharmony_ci
25fb299fa2Sopenharmony_cinamespace Updater {
26fb299fa2Sopenharmony_ciconstexpr uint8_t BITMAP_HEADER_SIZE = 54; /* 54 is bitmap header size */
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_civoid UpdaterUiTools::SaveUxBuffToFile(const std::string &filePath)
29fb299fa2Sopenharmony_ci{
30fb299fa2Sopenharmony_ci    ImageInfo imageBit {};
31fb299fa2Sopenharmony_ci    if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
32fb299fa2Sopenharmony_ci        LOG(ERROR) << "uxtools get bitmap failed";
33fb299fa2Sopenharmony_ci        return;
34fb299fa2Sopenharmony_ci    }
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_ci    int32_t fd = open(filePath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
37fb299fa2Sopenharmony_ci    if (fd == -1) {
38fb299fa2Sopenharmony_ci        LOG(ERROR) << "uxtools open file failed";
39fb299fa2Sopenharmony_ci        ImageCacheFree(imageBit);
40fb299fa2Sopenharmony_ci        return;
41fb299fa2Sopenharmony_ci    }
42fb299fa2Sopenharmony_ci
43fb299fa2Sopenharmony_ci    uint8_t sizeByColorMode = DrawUtils::GetByteSizeByColorMode(OHOS::ColorMode::ARGB8888);
44fb299fa2Sopenharmony_ci    uint16_t bfType = 0x4D42;
45fb299fa2Sopenharmony_ci    struct BitmapInfoHeader bitmapInfo = {
46fb299fa2Sopenharmony_ci        .bfSize = imageBit.dataSize + BITMAP_HEADER_SIZE,
47fb299fa2Sopenharmony_ci        .bfOffBits = BITMAP_HEADER_SIZE,
48fb299fa2Sopenharmony_ci        .biSize = 40, /* 40 is bitmap information header size */
49fb299fa2Sopenharmony_ci        .biWidth = imageBit.header.width,
50fb299fa2Sopenharmony_ci        .biHeight = -imageBit.header.height,
51fb299fa2Sopenharmony_ci        .biPlanes = 1,
52fb299fa2Sopenharmony_ci        .biBitCount = sizeByColorMode * 8, /* 8 is uint8_t bit */
53fb299fa2Sopenharmony_ci        .biSizeImage = imageBit.dataSize,
54fb299fa2Sopenharmony_ci    };
55fb299fa2Sopenharmony_ci    if (write(fd, &bfType, sizeof(bfType)) > 0 && write(fd, &bitmapInfo, sizeof(bitmapInfo)) > 0 &&
56fb299fa2Sopenharmony_ci        write(fd, imageBit.data, imageBit.dataSize) > 0) {
57fb299fa2Sopenharmony_ci        LOG(INFO) << "uxtools save file done";
58fb299fa2Sopenharmony_ci    } else {
59fb299fa2Sopenharmony_ci        LOG(ERROR) << "uxtools save file failed";
60fb299fa2Sopenharmony_ci    }
61fb299fa2Sopenharmony_ci    ImageCacheFree(imageBit);
62fb299fa2Sopenharmony_ci    close(fd);
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_ci    return;
65fb299fa2Sopenharmony_ci}
66fb299fa2Sopenharmony_ci} // namespace Updater
67