]> bicyclesonthemoon.info Git - ott/enhance/blob - diff-cgi.c
use config tool as submodule
[ott/enhance] / diff-cgi.c
1 /*
2 diff-cgi.c
3 The online interface for the differential tool
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 "df.h"
36
37 #define RA 0x00
38 #define GA 0x57
39 #define BA 0xAF
40 #define RB 0xBB
41 #define GB 0x66
42 #define BB 0x22
43
44 int diff (
45         char *inpix0, char *inpix1, char *outpix,
46         uint_fast8_t ra, uint_fast8_t ga, uint_fast8_t ba,
47         uint_fast8_t rb, uint_fast8_t gb, uint_fast8_t bb
48 );
49
50 int main (int argc, char *argv[])
51 {
52         s_cgi *cgi;
53         char in0_tmp[256];
54         char in1_tmp[256];
55         char out_tmp[256];
56         char *in0_path;
57         char *in1_path;
58         char *t;
59         uint_fast8_t ra, ga, ba, rb, gb, bb;
60         
61         int r=0;
62         int r1=0;
63         int r2=0;
64         int r3=0;
65         int r4=0;
66         
67         do {
68                 make_tmp_path(in0_tmp, 256, 0, "");
69                 make_tmp_path(in1_tmp, 256, 1, "");
70                 make_tmp_path(out_tmp, 256, 2, ".png");
71                 
72                 cgi=cgiInit();
73                 
74                 r = get_file(cgi,"inpixf0", in0_tmp, &in0_path);
75                 if (r)
76                         r = get_file(cgi,"inpix0", in0_tmp, &in0_path);
77                 if (r)
78                         break;
79                 
80                 r = get_file(cgi,"inpixf1", in1_tmp, &in1_path);
81                 if (r)
82                         r = get_file(cgi,"inpix1", in1_tmp, &in1_path);
83                 if (r)
84                         break;
85                 
86                 t = cgiGetValue(cgi, "ra");
87                 if (t != NULL)
88                         sscanf(t,"%"SCNuFAST8, &ra);
89                 else
90                         ra = RA;
91                 
92                 t = cgiGetValue(cgi, "ga");
93                 if (t != NULL)
94                         sscanf(t,"%"SCNuFAST8, &ga);
95                 else
96                         ga = GA;
97                 
98                 t = cgiGetValue(cgi, "ba");
99                 if (t != NULL)
100                         sscanf(t,"%"SCNuFAST8, &ba);
101                 else
102                         ba = BA;
103                 
104                 t = cgiGetValue(cgi, "rb");
105                 if (t != NULL)
106                         sscanf(t,"%"SCNuFAST8, &rb);
107                 else
108                         rb = RB;
109                 
110                 t = cgiGetValue(cgi, "gb");
111                 if (t != NULL)
112                         sscanf(t,"%"SCNuFAST8, &gb);
113                 else
114                         gb = GB;
115                 
116                 t = cgiGetValue(cgi, "bb");
117                 if (t != NULL)
118                         sscanf(t,"%"SCNuFAST8, &bb);
119                 else
120                         bb = BB;
121                 
122                 r = diff(in0_path, in1_path, out_tmp, ra, ga, ba, rb, gb, bb);
123                 if (r)
124                         break;
125                 
126                 r1 = send_file(out_tmp, "image/png", 0);
127         } while (0);
128         if (r)
129         {
130                 r1 = send_data(df, df_size, "image/png", 500);
131         }
132         r2 = rm(in0_tmp);
133         r3 = rm(in1_tmp);
134         r4 = rm(out_tmp);
135         if (r1)
136                 return r1;
137         if (r2)
138                 return r2;
139         if (r3)
140                 return r3;
141         if (r4)
142                 return r4;
143         return 0;
144 }
145
146 int diff (
147         char *inpix0, char *inpix1, char *outpix,
148         uint_fast8_t ra, uint_fast8_t ga, uint_fast8_t ba,
149         uint_fast8_t rb, uint_fast8_t gb, uint_fast8_t bb
150 )
151 {
152         char s_ra[4];
153         char s_ga[4];
154         char s_ba[4];
155         char s_rb[4];
156         char s_gb[4];
157         char s_bb[4];
158         
159         pid_t sub;
160         int r;
161         
162         snprintf(s_ra, 4, "%"PRIuFAST8, ra);
163         snprintf(s_ga, 4, "%"PRIuFAST8, ga);
164         snprintf(s_ba, 4, "%"PRIuFAST8, ba);
165         snprintf(s_rb, 4, "%"PRIuFAST8, rb);
166         snprintf(s_gb, 4, "%"PRIuFAST8, gb);
167         snprintf(s_bb, 4, "%"PRIuFAST8, bb);
168         
169         sub = fork();
170         if (sub == 0)
171         {
172                 r = execl(
173                         DIFF_PATH, DIFF_PATH,
174                         inpix0, inpix1, outpix,
175                         s_ra, s_ga, s_ba,
176                         s_rb, s_gb, s_bb,
177                         (char *)0
178                 );
179                 exit(r);
180         }
181         waitpid(sub, &r, 0);
182         return r;
183 }
184
185