xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/guetzli/guetzli_sandboxed.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
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 //     https://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 #include <cstdio>
16 #include <iostream>
17 
18 #include "guetzli_transaction.h"  // NOLINT(build/include)
19 #include "sandboxed_api/util/fileops.h"
20 
21 namespace {
22 
23 constexpr int kDefaultJPEGQuality = 95;
24 constexpr int kDefaultMemlimitMB = 6000;
25 
Usage()26 void Usage() {
27   fprintf(stderr,
28           "Guetzli JPEG compressor. Usage: \n"
29           "guetzli [flags] input_filename output_filename\n"
30           "\n"
31           "Flags:\n"
32           "  --verbose    - Print a verbose trace of all attempts to standard "
33           "output.\n"
34           "  --quality Q  - Visual quality to aim for, expressed as a JPEG "
35           "quality value.\n"
36           "                 Default value is %d.\n"
37           "  --memlimit M - Memory limit in MB. Guetzli will fail if unable to "
38           "stay under\n"
39           "                 the limit. Default limit is %d MB.\n"
40           "  --nomemlimit - Do not limit memory usage.\n",
41           kDefaultJPEGQuality, kDefaultMemlimitMB);
42   exit(1);
43 }
44 
45 }  // namespace
46 
main(int argc,char * argv[])47 int main(int argc, char* argv[]) {
48   int verbose = 0;
49   int quality = kDefaultJPEGQuality;
50   int memlimit_mb = kDefaultMemlimitMB;
51 
52   int opt_idx = 1;
53   for (; opt_idx < argc; opt_idx++) {
54     if (strnlen(argv[opt_idx], 2) < 2 || argv[opt_idx][0] != '-' ||
55         argv[opt_idx][1] != '-')
56       break;
57 
58     if (!strcmp(argv[opt_idx], "--verbose")) {
59       verbose = 1;
60     } else if (!strcmp(argv[opt_idx], "--quality")) {
61       opt_idx++;
62       if (opt_idx >= argc) Usage();
63       quality = atoi(argv[opt_idx]);  // NOLINT(runtime/deprecated_fn)
64     } else if (!strcmp(argv[opt_idx], "--memlimit")) {
65       opt_idx++;
66       if (opt_idx >= argc) Usage();
67       memlimit_mb = atoi(argv[opt_idx]);  // NOLINT(runtime/deprecated_fn)
68     } else if (!strcmp(argv[opt_idx], "--nomemlimit")) {
69       memlimit_mb = -1;
70     } else if (!strcmp(argv[opt_idx], "--")) {
71       opt_idx++;
72       break;
73     } else {
74       fprintf(stderr, "Unknown commandline flag: %s\n", argv[opt_idx]);
75       Usage();
76     }
77   }
78 
79   if (argc - opt_idx != 2) {
80     Usage();
81   }
82 
83   guetzli::sandbox::TransactionParams params = {
84       argv[opt_idx], argv[opt_idx + 1], verbose, quality, memlimit_mb};
85 
86   guetzli::sandbox::GuetzliTransaction transaction(std::move(params));
87   auto result = transaction.Run();
88 
89   if (!result.ok()) {
90     std::cerr << result.ToString() << std::endl;
91     return EXIT_FAILURE;
92   }
93 
94   return EXIT_SUCCESS;
95 }
96