]> bicyclesonthemoon.info Git - ott/enhance/blob - t-1-cgi.c
t-1 online. That's all tools!
[ott/enhance] / t-1-cgi.c
1 /*
2 t-1-cgi.c
3 The online interface for remapping t-1
4 05.12.2022
5
6 Copyright (C) 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 "t1.h"
36
37 #define N0  3
38 #define N1 85
39
40 int remap (
41         char *inpix, char *outpix,
42         char *palette,
43         uint_fast8_t p0, uint_fast8_t p1
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         char *palette;
53         char noname[]=" ";
54         uint8_t p0, p1;
55         
56         int r=0;
57         int r1=0;
58         int r2=0;
59         int r3=0;
60         
61         do {
62                 make_tmp_path(in_tmp, 256, 0, "");
63                 make_tmp_path(out_tmp, 256, 1, ".png");
64                 
65                 cgi=cgiInit();
66                 
67                 r = get_file(cgi,"inpix2", in_tmp, &in_path);
68                 if (r)
69                         r = get_file(cgi,"inpix", in_tmp, &in_path);
70                 if (r)
71                         break;
72                 
73                 t = cgiGetValue(cgi, "p0");
74                 if (t != NULL)
75                         sscanf(t,"%"SCNuFAST8, &p0);
76                 else
77                         p0 = N0;
78                 
79                 t = cgiGetValue(cgi, "p1");
80                 if (t != NULL)
81                         sscanf(t,"%"SCNuFAST8, &p1);
82                 else
83                         p1 = N1;
84                 
85                 palette = cgiGetValue(cgi, "b64");
86                 if (palette==NULL)
87                         palette = noname;
88                 
89                 r = remap(
90                         in_path, out_tmp,
91                         palette,
92                         p0, p1
93                 );
94                 if (r)
95                         break;
96                 
97                 r1 = send_file(out_tmp, "image/png", 0);
98         } while (0);
99         if (r)
100         {
101                 r1 = send_data(t1, t1_size, "image/png", 500);
102         }
103         r2 = rm(in_tmp);
104         r3 = rm(out_tmp);
105         if (r1)
106                 return r1;
107         if (r2)
108                 return r2;
109         if (r3)
110                 return r3;
111         return 0;
112 }
113
114 int remap (
115         char *inpix, char *outpix,
116         char *palette,
117         uint_fast8_t p0, uint_fast8_t p1
118 )
119 {
120         char s_p0[4];
121         char s_p1[4];
122         
123         pid_t sub;
124         int r;
125         
126         snprintf(s_p0, 4, "%"PRIuFAST8, p0);
127         snprintf(s_p1, 4, "%"PRIuFAST8, p1);
128         
129         sub = fork();
130         if (sub == 0)
131         {
132                 r = execl(
133                         REMAP_T_1_PATH, REMAP_T_1_PATH,
134                         inpix, outpix,
135                         palette,
136                         s_p0, s_p1,
137                         (char *) 0
138                 );
139                 exit(r);
140         }
141         waitpid(sub, &r, 0);
142         return r;
143 }