xref: /OK3568_Linux_fs/external/linux-rga/samples/im2d_api_demo/args.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
3  * Authors:
4  *  PutinLee <putin.lee@rock-chips.com>
5  *  Cerf Yu <cerf.yu@rock-chips.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include "args.h"
27 #include "im2d.hpp"
28 
help_function()29 void help_function() {
30     printf("\n=============================================================================================\n");
31     printf( "   usage: rgaImDemo [--help/-h] [--while/-w=(time)] [--querystring/--querystring=<options>]\n"
32             "                    [--copy] [--resize=<up/down>] [--crop] [--rotate=90/180/270]\n"
33             "                    [--flip=H/V] [--translate] [--blend] [--cvtcolor]\n"
34             "                    [--fill=blue/green/red]\n");
35     printf( "\t --help/-h     Call help\n"
36             "\t --while/w     Set the loop mode. Users can set the number of cycles by themselves.\n"
37             "\t --querystring You can print the version or support information corresponding to the current version of RGA according to the options.\n"
38             "\t               If there is no input options, all versions and support information of the current version of RGA will be printed.\n"
39             "\t               <options>:\n"
40             "\t               vendor       \tPrint vendor information.\n"
41             "\t               version      \tPrint RGA version, and librga/im2d_api version.\n"
42             "\t               maxinput     \tPrint max input resolution.\n"
43             "\t               maxoutput    \tPrint max output resolution.\n"
44             "\t               scalelimit   \tPrint scale limit.\n"
45             "\t               inputformat  \tPrint supported input formats.\n"
46             "\t               outputformat \tPrint supported output formats.\n"
47             "\t               expected     \tPrint expected performance.\n"
48             "\t               all          \tPrint all information.\n"
49             "\t --copy        Copy the image by RGA.The default is 720p to 720p.\n"
50             "\t --resize      resize the image by RGA.You can choose to up(720p->1080p) or down(720p->480p).\n"
51             "\t --crop        Crop the image by RGA.By default, a picture of 300*300 size is cropped from (100,100).\n"
52             "\t --rotate      Rotate the image by RGA.You can choose to rotate 90/180/270 degrees.\n"
53             "\t --flip        Flip the image by RGA.You can choice of horizontal flip or vertical flip.\n"
54             "\t --translate   Translate the image by RGA.Default translation (300,300).\n"
55             "\t --blend       Blend the image by RGA.Default, Porter-Duff 'SRC over DST'.\n"
56             "\t --cvtcolor    Modify the image format and color space by RGA.The default is RGBA8888 to NV12.\n"
57             "\t --fill        Fill the image by RGA to blue, green, red, when you set the option to the corresponding color.\n");
58     printf("=============================================================================================\n\n");
59 }
60 
readArguments(int argc,char * argv[],int * parm)61 int readArguments(int argc, char *argv[], int* parm) {
62     int opt = 0, option_index = 0, mode_code = 0;
63     char string[] = "hw:";
64     static struct option mode_options[] = {
65         { "querystring", optional_argument, NULL, MODE_QUERYSTRING_CHAR   },
66         {        "copy",       no_argument, NULL, MODE_COPY_CHAR          },
67         {      "resize", required_argument, NULL, MODE_RESIZE_CHAR        },
68         {        "crop",       no_argument, NULL, MODE_CROP_CHAR          },
69         {      "rotate", required_argument, NULL, MODE_ROTATE_CHAR        },
70         {        "flip", required_argument, NULL, MODE_FLIP_CHAR          },
71         {   "translate",       no_argument, NULL, MODE_TRANSLATE_CHAR     },
72         {       "blend",       no_argument, NULL, MODE_BLEND_CHAR         },
73         {    "cvtcolor",       no_argument, NULL, MODE_CVTCOLOR_CHAR      },
74         {        "fill", required_argument, NULL, MODE_FILL_CHAR          },
75         {        "help",       no_argument, NULL, 'h'                     },
76         {		"while", required_argument, NULL, 'w'                     },
77         {         NULL ,                 0, NULL, 0                       },
78     };
79 
80     while((opt = getopt_long(argc, argv, string, mode_options, &option_index))!= -1) {
81         printf("Start selecting mode %x\n", mode_code);
82         switch (opt) {
83             case MODE_QUERYSTRING_CHAR :
84                 printf("im2d querystring ..\n");
85 
86                 if (optarg != NULL)
87                     parm[MODE_QUERYSTRING] = readInfo(optarg);
88                 else
89                     parm[MODE_QUERYSTRING] = RGA_ALL;
90 
91                 return MODE_QUERYSTRING;
92 
93             case MODE_COPY_CHAR :
94                 printf("im2d copy ..\n");
95 
96                 mode_code |= MODE_COPY;
97                 return mode_code;
98 
99             case MODE_RESIZE_CHAR :
100                 printf("im2d resize ..\n");
101 
102                 if (optarg != NULL)
103                     parm[MODE_RESIZE] = readParm(optarg);
104                 if (parm[MODE_RESIZE] == -1)
105                     goto out;
106 
107                 mode_code |= MODE_RESIZE;
108                 return mode_code;
109 
110             case MODE_CROP_CHAR :
111                 printf("im2d crop ..\n");
112 
113                 mode_code |= MODE_CROP;
114                 return mode_code;
115 
116             case MODE_ROTATE_CHAR :
117                 printf("im2d rotate ..\n");
118 
119                 if (optarg != NULL)
120                     parm[MODE_ROTATE] = readParm(optarg);
121                 if (parm[MODE_ROTATE] == -1)
122                     goto out;
123 
124                 mode_code |= MODE_ROTATE;
125                 return mode_code;
126 
127             case MODE_FLIP_CHAR :
128                 printf("im2d flip ..\n");
129 
130                 if (optarg != NULL)
131                     parm[MODE_FLIP] = readParm(optarg);
132                 if (parm[MODE_FLIP] == -1)
133                     goto out;
134 
135                 mode_code |= MODE_FLIP;
136                 return mode_code;
137 
138             case MODE_TRANSLATE_CHAR :
139                 printf("im2d translate ..\n");
140 
141                 mode_code |= MODE_TRANSLATE;
142                 return mode_code;
143 
144             case MODE_BLEND_CHAR :
145                 printf("im2d blend ..\n");
146 
147                 mode_code |= MODE_BLEND;
148                 return mode_code;
149 
150             case MODE_CVTCOLOR_CHAR :
151                 printf("im2d cvtcolor ..\n");
152 
153                 mode_code |= MODE_CVTCOLOR;
154                 return mode_code;
155 
156             case MODE_FILL_CHAR :
157                 printf("im2d fill ..\n");
158 
159                 if (optarg != NULL)
160                     parm[MODE_FILL] = readParm(optarg);
161                 if (parm[MODE_FILL] == -1)
162                     goto out;
163 
164                 mode_code |= MODE_FILL;
165                 return mode_code;
166 
167             case 'h' :
168                 help_function();
169                 mode_code |= MODE_NONE;
170                 return mode_code;
171 
172             case 'w' :
173                 printf("im2d while .. ");
174 
175                 if (optarg != NULL)
176                     parm[MODE_WHILE] = atoi(optarg);
177                 if (parm[MODE_WHILE] == 0) {
178                     printf("Option must be a non-zero number.\n");
179                     break;
180                 }
181 
182                 printf("%d time.\n", parm[MODE_WHILE]);
183                 mode_code |= WHILE_FLAG;
184                 break;
185 
186             default :
187                 printf("%s, no options!\n", __FUNCTION__);
188                 goto out;
189         }
190     }
191 out:
192     help_function();
193     return MODE_NONE;
194 }
195 
readInfo(char * targ)196 IM_INFORMATION readInfo(char* targ) {
197     if (strcmp(targ,"d") == 0 || strcmp(targ,"vendor") == 0 ) {
198         printf("im2d querystring .. vendor ...\n");
199         return RGA_VENDOR;
200     } else if (strcmp(targ,"v") == 0 || strcmp(targ,"version") == 0 ) {
201         printf("im2d querystring .. vendor ...\n");
202         return RGA_VERSION;
203     } else if (strcmp(targ,"i") == 0 || strcmp(targ,"maxinput") == 0 ) {
204         printf("im2d querystring .. max input ...\n");
205         return RGA_MAX_INPUT;
206     } else if (strcmp(targ,"o") == 0 || strcmp(targ,"maxoutput") == 0 ) {
207         printf("im2d querystring .. max output ...\n");
208         return RGA_MAX_OUTPUT;
209     } else if (strcmp(targ,"s") == 0 || strcmp(targ,"scalelimit") == 0 ) {
210         printf("im2d querystring .. scale limit ...\n");
211         return RGA_SCALE_LIMIT;
212     } else if (strcmp(targ,"n") == 0 || strcmp(targ,"inputformat") == 0 ) {
213         printf("im2d querystring .. input format ...\n");
214         return RGA_INPUT_FORMAT;
215     } else if (strcmp(targ,"u") == 0 || strcmp(targ,"outputformat") == 0 ) {
216         printf("im2d querystring .. output format ...\n");
217         return RGA_OUTPUT_FORMAT;
218     } else if (strcmp(targ,"e") == 0 || strcmp(targ,"expected") == 0 ) {
219         printf("im2d querystring .. expected ...\n");
220         return RGA_EXPECTED;
221     } else if (strcmp(targ,"a") == 0 || strcmp(targ,"all") == 0 || strcmp(targ," ") == 0) {
222         printf("im2d querystring .. all ...\n");
223         return RGA_ALL;
224     } else {
225         printf("%s, Invalid instruction\n", __FUNCTION__);
226         return RGA_ALL;
227     }
228 }
229 
readParm(char * targ)230 int readParm(char* targ) {
231     if (strcmp(targ,"up") == 0 ) {
232         printf("up resize ...\n");
233         return IM_UP_SCALE;
234     } else if (strcmp(targ,"down") == 0) {
235         printf("down resize ...\n");
236         return IM_DOWN_SCALE;
237     } else if (strcmp(targ,"90") == 0) {
238         printf("rotation 90 ...\n");
239         return IM_HAL_TRANSFORM_ROT_90;
240     } else if (strcmp(targ,"180") == 0) {
241         printf("nrotation 180 ...\n");
242         return IM_HAL_TRANSFORM_ROT_180;
243     } else if (strcmp(targ,"270") == 0) {
244         printf("rotation 270 ...\n");
245         return IM_HAL_TRANSFORM_ROT_270;
246     } else if (strcmp(targ,"H") == 0) {
247         printf("flip H ...\n");
248         return IM_HAL_TRANSFORM_FLIP_H;
249     } else if (strcmp(targ,"V") == 0) {
250         printf("flip V ...\n");
251         return IM_HAL_TRANSFORM_FLIP_V;
252     } else if (strcmp(targ,"blue") == 0) {
253         printf("fill blue ...\n");
254         return BLUE_COLOR;
255     } else if (strcmp(targ,"green") == 0) {
256         printf("fill green ...\n");
257         return GREEN_COLOR;
258     } else if (strcmp(targ,"red") == 0) {
259         printf("fill red ...\n");
260         return RED_COLOR;
261     } else {
262         printf("%s, Invalid parameter\n", __FUNCTION__);
263         return -1;
264     }
265 }
266 
267