1 /*
2  * Copyright (c) 2021-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 #ifndef SNAPSHOT_UTILS_H
17 #define SNAPSHOT_UTILS_H
18 
19 #include <cstdint>
20 #include <pixel_map.h>
21 #include <string>
22 
23 #include "display_manager.h"
24 #include "dm_common.h"
25 
26 namespace OHOS {
27 
28 struct WriteToJpegParam {
29     uint32_t width;
30     uint32_t height;
31     uint32_t stride;
32     Media::PixelFormat format;
33     const uint8_t *data;
34 };
35 
36 struct CmdArguments {
37     Rosen::DisplayId displayId = Rosen::DISPLAY_ID_INVALID;
38     std::string fileName;
39     std::string fileType;
40     int32_t width = -1;
41     int32_t height = -1;
42     bool isDisplayIdSet = false;
43     bool isWidthSet = false;
44     bool isHeightSet = false;
45 };
46 
47 class SnapShotUtils {
48 public:
49     SnapShotUtils() = default;
50     ~SnapShotUtils() = default;
51 
52     static void PrintUsage(const std::string& cmdLine);
53     static bool CheckFileNameValid(const std::string& fileName, std::string fileType = "jpeg");
54     static std::string GenerateFileName(std::string fileType, int offset = 0);
55     static bool CheckWidthAndHeightValid(int32_t w, int32_t h);
56     static bool RGBA8888ToRGB888(const uint8_t* rgba8888Buf, uint8_t* rgb888Buf, int32_t size);
57     static bool RGB565ToRGB888(const uint8_t* rgb565Buf, uint8_t* rgb888Buf, int32_t size);
58     static bool WriteRgb888ToJpeg(FILE* file, uint32_t width, uint32_t height, const uint8_t* data);
59     static bool WriteToJpeg(const std::string& fileName, const WriteToJpegParam& param);
60     static bool WriteToJpeg(int fd, const WriteToJpegParam& param);
61     static bool WriteToJpegWithPixelMap(const std::string& fileName, Media::PixelMap& pixelMap);
62     static bool WriteToJpegWithPixelMap(int fd, Media::PixelMap& pixelMap);
63     static bool ProcessArgs(int argc, char * const argv[], CmdArguments& cmdArgments);
64     static bool CheckWHValid(int32_t param);
65     static bool CheckParamValid(const WriteToJpegParam& param);
66     static bool SaveSnapShot(const std::string& filename, Media::PixelMap& pixelMap, std::string fileType = "jpeg");
67 private:
68     static bool ProcessDisplayId(Rosen::DisplayId& displayId, bool isDisplayIdSet);
69 };
70 }
71 
72 #endif // SNAPSHOT_UTILS_H
73