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 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 <getopt.h>
17#include <iostream>
18#include "command_parser.h"
19
20namespace OHOS::VDI::HEIF {
21using namespace std;
22
23enum ShortOption {
24    OPT_UNKONWN = 0,
25    OPT_HELP,
26    OPT_PRIMARY_IMG,
27    OPT_AUXILIARY_IMG,
28    OPT_THUMBNAIL_IMG,
29    OPT_GAIN_MAP,
30    OPT_EXIF_DATA,
31    OPT_USER_DATA,
32    OPT_ICC_PROFILE,
33    OPT_IT35,
34    OPT_MIRROR,
35    OPT_ROTATE,
36    OPT_OUTPUT = 'o'
37};
38
39static struct option g_longOptions[] = {
40    {"help",            no_argument,        nullptr, static_cast<int>(ShortOption::OPT_HELP)},
41    {"primaryImg",      required_argument,  nullptr, static_cast<int>(ShortOption::OPT_PRIMARY_IMG)},
42    {"auxiliaryImg",    required_argument,  nullptr, static_cast<int>(ShortOption::OPT_AUXILIARY_IMG)},
43    {"thumbnailImg",    required_argument,  nullptr, static_cast<int>(ShortOption::OPT_THUMBNAIL_IMG)},
44    {"gainMap",         required_argument,  nullptr, static_cast<int>(ShortOption::OPT_GAIN_MAP)},
45    {"exifData",        required_argument,  nullptr, static_cast<int>(ShortOption::OPT_EXIF_DATA)},
46    {"userData",        required_argument,  nullptr, static_cast<int>(ShortOption::OPT_USER_DATA)},
47    {"iccProfile",      required_argument,  nullptr, static_cast<int>(ShortOption::OPT_ICC_PROFILE)},
48    {"it35",            required_argument,  nullptr, static_cast<int>(ShortOption::OPT_IT35)},
49    {"mirror",          required_argument,  nullptr, static_cast<int>(ShortOption::OPT_MIRROR)},
50    {"rotate",          required_argument,  nullptr, static_cast<int>(ShortOption::OPT_ROTATE)},
51    {"out",             required_argument,  nullptr, static_cast<int>(ShortOption::OPT_OUTPUT)},
52    {nullptr,           no_argument,        nullptr, static_cast<int>(ShortOption::OPT_UNKONWN)},
53};
54
55void ShowUsage()
56{
57    std::string rotateValueOpt = "0: ANTI_CLOCKWISE_90, 1: ANTI_CLOCKWISE_180, 2: ANTI_CLOCKWISE_270";
58    std::cout << "Heif Hardware encode Demo Options:" << std::endl;
59    std::cout << " --help               help info." << std::endl;
60    std::cout << " --primaryImg         full path for primary image file." << std::endl;
61    std::cout << " --auxiliaryImg       (optional) full path for auxiliary image file." << std::endl;
62    std::cout << " --thumbnailImg       (optional) full path for thumbnail image file." << std::endl;
63    std::cout << " --gainMap            (optional) full path for gainMap file." << std::endl;
64    std::cout << " --exifData           (optional) full path for exifData file." << std::endl;
65    std::cout << " --userData           (optional) full path for userData file." << std::endl;
66    std::cout << " --iccProfile         (optional) full path for iccProfile file." << std::endl;
67    std::cout << " --it35               (optional) full path for it35 file." << std::endl;
68    std::cout << " --mirror             (optional) image mirror info. 0: HORIZONTAL, 1: VERTICAL" << std::endl;
69    std::cout << " --rotate             (optional) image rotate info. " << rotateValueOpt << std::endl;
70    std::cout << " -o, --out            (optional) full path for output file." << std::endl;
71}
72
73CommandOpt Parse(int argc, char *argv[])
74{
75    CommandOpt opt;
76    int c;
77    while ((c = getopt_long(argc, argv, "o:", g_longOptions, nullptr)) != -1) {
78        switch (static_cast<ShortOption>(c)) {
79            case ShortOption::OPT_HELP:
80                ShowUsage();
81                break;
82            case ShortOption::OPT_PRIMARY_IMG:
83                opt.primaryImgPath = string(optarg);
84                break;
85            case ShortOption::OPT_AUXILIARY_IMG:
86                opt.auxiliaryImgPath = string(optarg);
87                break;
88            case ShortOption::OPT_THUMBNAIL_IMG:
89                opt.thumbnailImgPath = string(optarg);
90                break;
91            case ShortOption::OPT_GAIN_MAP:
92                opt.gainMapPath = string(optarg);
93                break;
94            case ShortOption::OPT_EXIF_DATA:
95                opt.exifDataPath = string(optarg);
96                break;
97            case ShortOption::OPT_USER_DATA:
98                opt.userDataPath = string(optarg);
99                break;
100            case ShortOption::OPT_ICC_PROFILE:
101                opt.iccProfilePath = string(optarg);
102                break;
103            case ShortOption::OPT_IT35:
104                opt.it35Path = string(optarg);
105                break;
106            case ShortOption::OPT_MIRROR:
107                opt.mirrorInfo = static_cast<ImageMirror>(stol(optarg));
108                break;
109            case ShortOption::OPT_ROTATE:
110                opt.rotateInfo = static_cast<ImageRotation>(stol(optarg));
111                break;
112            case ShortOption::OPT_OUTPUT:
113                opt.outputPath = string(optarg);
114                break;
115            default:
116                break;
117        }
118    }
119    return opt;
120}
121
122static string GetMirrorPrintInfo(ImageMirror info)
123{
124    if (info == ImageMirror::NONE) {
125        return "ImageMirror::NONE";
126    }
127    if (info == ImageMirror::HORIZONTAL) {
128        return "ImageMirror::HORIZONTAL";
129    }
130    if (info == ImageMirror::VERTICAL) {
131        return "ImageMirror::VERTICAL";
132    }
133    return "unknown mirror info";
134}
135
136static string GetRotatePrintInfo(ImageRotation info)
137{
138    if (info == ImageRotation::NONE) {
139        return "ImageRotation::NONE";
140    }
141    if (info == ImageRotation::ANTI_CLOCKWISE_90) {
142        return "ImageRotation::ANTI_CLOCKWISE_90";
143    }
144    if (info == ImageRotation::ANTI_CLOCKWISE_180) {
145        return "ImageRotation::ANTI_CLOCKWISE_180";
146    }
147    if (info == ImageRotation::ANTI_CLOCKWISE_270) {
148        return "ImageRotation::ANTI_CLOCKWISE_270";
149    }
150    return "unknown rotate info";
151}
152
153void CommandOpt::Print() const
154{
155    std::cout << "=========================== OPT INFO ===========================" << endl;
156    std::cout << "   primaryImgPath : " << primaryImgPath << endl;
157    std::cout << " auxiliaryImgPath : " << auxiliaryImgPath << endl;
158    std::cout << " thumbnailImgPath : " << thumbnailImgPath << endl;
159    std::cout << "      gainMapPath : " << gainMapPath << endl;
160    std::cout << "     exifDataPath : " << exifDataPath << endl;
161    std::cout << "     userDataPath : " << userDataPath << endl;
162    std::cout << "   iccProfilePath : " << iccProfilePath << endl;
163    std::cout << "         it35Path : " << it35Path << endl;
164    std::cout << "       mirrorInfo : " << GetMirrorPrintInfo(mirrorInfo) << endl;
165    std::cout << "       rotateInfo : " << GetRotatePrintInfo(rotateInfo) << endl;
166    std::cout << "       outputPath : " << outputPath << endl;
167    std::cout << "=================================================================" << endl;
168}
169}
170