]> bicyclesonthemoon.info Git - ott/enhance/blob - nofading-cgi.c
1c3c410452bc4923ad690d8c6f1278768ca0ec9f
[ott/enhance] / nofading-cgi.c
1 /*
2 nofading-cgi.c
3 The online interface for the fading enhancement tool
4 04.12.2022
5
6 Copyright (C) 2015, 2022  Balthasar Szczepański
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 Requires cgilib (http://www.infodrom.org/projects/cgilib/)
22 */
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <stdint.h>
30 #include <inttypes.h>
31
32 #include <cgi.h>
33
34 #include "online-core.h"
35 #include "nf.h"
36
37 int nofading (
38         char *inpix, char *outpix,
39         uint_fast32_t border,
40         uint_fast8_t individual_channels,
41         uint_fast8_t enhance_alpha,
42         uint_fast8_t remove_alpha
43 );
44
45 int main (int argc, char *argv[])
46 {
47         s_cgi *cgi;
48         char in_tmp[256];
49         char out_tmp[256];
50         char *in_path;
51         char *t;
52         uint_fast32_t border;
53         uint_fast8_t individual_channels, enhance_alpha, remove_alpha;
54         
55         int r=0;
56         int r1=0;
57         int r2=0;
58         int r3=0;
59         
60         do {
61                 make_tmp_path(in_tmp, 256, 0, "");
62                 make_tmp_path(out_tmp, 256, 1, ".png");
63                 
64                 cgi=cgiInit();
65                 
66                 r = get_file(cgi,"inpix", in_tmp, &in_path);
67                 if (r)
68                         break;
69                 
70                 individual_channels = (cgiGetValue(cgi, "c")!=NULL) ? 0 : 1;
71                 enhance_alpha = (cgiGetValue(cgi, "a")!=NULL) ? 1 : 0;
72                 remove_alpha = (cgiGetValue(cgi, "n")!=NULL) ? 1 : 0;
73                 
74                 t = cgiGetValue(cgi, "f");
75                 if (t != NULL)
76                         sscanf(t,"%"SCNuFAST32, &border);
77                 else
78                         border = 0;
79                 
80                 r = nofading(
81                         in_path, out_tmp,
82                         border,
83                         individual_channels, enhance_alpha, remove_alpha
84                 );
85                 if (r)
86                         break;
87                 r1 = send_file(out_tmp, "image/png", 0);
88         } while (0);
89         if (r)
90         {
91                 r1 = send_data(nf, nf_size, "image/png", 500);
92         }
93         r2 = rm(in_tmp);
94         r3 = rm(out_tmp);
95         if (r)
96                 return r;
97         if (r1)
98                 return r1;
99         if (r2)
100                 return r2;
101         if (r3)
102                 return r3;
103         return 0;
104 }
105
106 int nofading (
107         char *inpix, char *outpix,
108         uint_fast32_t border,
109         uint_fast8_t individual_channels,
110         uint_fast8_t enhance_alpha,
111         uint_fast8_t remove_alpha
112 )
113 {
114         char s_border[11];
115         pid_t sub;
116         int r;
117         
118         snprintf(s_border, 11, "%"PRIuFAST32, border);
119         
120         sub = fork();
121         if (sub == 0)
122         {
123                 r = execl(
124                         NOFADING_PATH, NOFADING_PATH,
125                         individual_channels ? "-c" : "-0",
126                         enhance_alpha ? "-a" : "-0",
127                         remove_alpha ? "-n" : "-0",
128                         inpix, outpix,
129                         s_border,
130                         (char *)0
131                 );
132                 exit(r);
133         }
134         waitpid(sub, &r, 0);
135         return r;
136 }