+/*
+diff-cgi.c
+The online interface for the differential tool
+04.12.2022
+
+Copyright (C) 2022 Balthasar SzczepaĆski
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+Requires cgilib (http://www.infodrom.org/projects/cgilib/)
+*/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+#include <cgi.h>
+
+#include "online-core.h"
+#include "df.h"
+
+#define RA 0x00
+#define GA 0x57
+#define BA 0xAF
+#define RB 0xBB
+#define GB 0x66
+#define BB 0x22
+
+int diff (
+ char *inpix0, char *inpix1, char *outpix,
+ uint_fast8_t ra, uint_fast8_t ga, uint_fast8_t ba,
+ uint_fast8_t rb, uint_fast8_t gb, uint_fast8_t bb
+);
+
+int main (int argc, char *argv[])
+{
+ s_cgi *cgi;
+ char in0_tmp[256];
+ char in1_tmp[256];
+ char out_tmp[256];
+ char *in0_path;
+ char *in1_path;
+ char *t;
+ uint_fast8_t ra, ga, ba, rb, gb, bb;
+
+ int r=0;
+ int r1=0;
+ int r2=0;
+ int r3=0;
+ int r4=0;
+
+ do {
+ make_tmp_path(in0_tmp, 256, 0, "");
+ make_tmp_path(in1_tmp, 256, 1, "");
+ make_tmp_path(out_tmp, 256, 2, ".png");
+
+ cgi=cgiInit();
+
+ r = get_file(cgi,"inpixf0", in0_tmp, &in0_path);
+ if (r)
+ r = get_file(cgi,"inpix0", in0_tmp, &in0_path);
+ if (r)
+ break;
+
+ r = get_file(cgi,"inpixf1", in1_tmp, &in1_path);
+ if (r)
+ r = get_file(cgi,"inpix1", in1_tmp, &in1_path);
+ if (r)
+ break;
+
+ t = cgiGetValue(cgi, "ra");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &ra);
+ else
+ ra = RA;
+
+ t = cgiGetValue(cgi, "ga");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &ga);
+ else
+ ga = GA;
+
+ t = cgiGetValue(cgi, "ba");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &ba);
+ else
+ ba = BA;
+
+ t = cgiGetValue(cgi, "rb");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &rb);
+ else
+ rb = RB;
+
+ t = cgiGetValue(cgi, "gb");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &gb);
+ else
+ gb = GB;
+
+ t = cgiGetValue(cgi, "bb");
+ if (t != NULL)
+ sscanf(t,"%"SCNuFAST8, &bb);
+ else
+ bb = BB;
+
+ r = diff(in0_path, in1_path, out_tmp, ra, ga, ba, rb, gb, bb);
+ if (r)
+ break;
+
+ r1 = send_file(out_tmp, "image/png", 0);
+ } while (0);
+ if (r)
+ {
+ r1 = send_data(df, df_size, "image/png", 500);
+ }
+ r2 = rm(in0_tmp);
+ r3 = rm(in1_tmp);
+ r4 = rm(out_tmp);
+ if (r)
+ return r;
+ if (r1)
+ return r1;
+ if (r2)
+ return r2;
+ if (r3)
+ return r3;
+ if (r4)
+ return r4;
+ return 0;
+}
+
+int diff (
+ char *inpix0, char *inpix1, char *outpix,
+ uint_fast8_t ra, uint_fast8_t ga, uint_fast8_t ba,
+ uint_fast8_t rb, uint_fast8_t gb, uint_fast8_t bb
+)
+{
+ char s_ra[4];
+ char s_ga[4];
+ char s_ba[4];
+ char s_rb[4];
+ char s_gb[4];
+ char s_bb[4];
+
+ pid_t sub;
+ int r;
+
+ snprintf(s_ra, 4, "%"SCNuFAST8, ra);
+ snprintf(s_ga, 4, "%"SCNuFAST8, ga);
+ snprintf(s_ba, 4, "%"SCNuFAST8, ba);
+ snprintf(s_rb, 4, "%"SCNuFAST8, rb);
+ snprintf(s_gb, 4, "%"SCNuFAST8, gb);
+ snprintf(s_bb, 4, "%"SCNuFAST8, bb);
+
+ sub = fork();
+ if (sub == 0)
+ {
+ r = execl(
+ DIFF_PATH, DIFF_PATH,
+ inpix0, inpix1, outpix,
+ s_ra, s_ga, s_ba,
+ s_rb, s_gb, s_bb,
+ (char *)0
+ );
+ exit(r);
+ }
+ waitpid(sub, &r, 0);
+ return r;
+}
+
+