]> bicyclesonthemoon.info Git - ott/enhance/blobdiff - nofading-cgi.c
newpixbot online
[ott/enhance] / nofading-cgi.c
index 2c20885b80599c0e8c4c7a9e245b528736b96111..1c3c410452bc4923ad690d8c6f1278768ca0ec9f 100644 (file)
-// nofadingd.cpp
-// The online interface for the fading enhancement tool
-// 04.03.2015
-// 
-// Copyright (C) 2015  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/>.
-// 
+/*
+nofading-cgi.c
+The online interface for the fading enhancement tool
+04.12.2022
 
-// Requires cgilib (http://www.infodrom.org/projects/cgilib/)
-// line 146 of cgi.h must be changed from:
-// extern }
-// to:
-// }
+Copyright (C) 2015, 2022  Balthasar Szczepański
 
-by balthasar_s
-    Fri Jun 12, 2015 6:59 am UTC
-     
-    Forum: Individual XKCD Comic Threads
-    Topic: 1190: "Time"
-    Replies: 93408
-    Views: 10895653
+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 <cgi.h>
 #include <stdio.h>
-#include <stdlib.h>
+#include <stdint.h>
 #include <unistd.h>
-#include <string.h>
+#include <stdlib.h>
 #include <sys/wait.h>
-#include <sys/stat.h>
+#include <stdint.h>
+#include <inttypes.h>
 
-#define WGET_PATH  "/usr/bin/wget"
-#define ENH_PATH   "./nofading"
-#define RM_PATH    "/bin/rm"
-#define LOG_PATH   "/eizm/log/nofading/nofading.log"
-#define USERAGENT  "No fading (1190.bicyclesonthemoon.dnsd.info/nofading/nofading.htm)"
-#define MSTD_PATH  "/pro/nofading/nf.png"
-
-int main (int argc, char *argv[]);
-void mustard(int mustard);
+#include <cgi.h>
 
-FILE *pix;
+#include "online-core.h"
+#include "nf.h"
 
-char url[1024];
-char path[256];
-s_cgi *cgi;
-int t;
-pid_t sub;
+int nofading (
+       char *inpix, char *outpix,
+       uint_fast32_t border,
+       uint_fast8_t individual_channels,
+       uint_fast8_t enhance_alpha,
+       uint_fast8_t remove_alpha
+);
 
 int main (int argc, char *argv[])
 {
-       unsigned char buf[1024];
-       struct stat st;
-       unsigned long p;
-       unsigned short q;
-       char **up;
-
-       cgi=cgiInit();
-       
-       up=cgiGetFiles(cgi);
-       if (up)
-       {
-               sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
-               sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
-       }
-       else
-       {
-               sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
-               sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
-               
-               //Why is  there no spawnl() in linux?
-               sub=fork();
-               if(sub==0)
-               {
-                       t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
-                       exit(t);
-               }
-               waitpid(sub,&t,0);
-               if(t)mustard(t);
-       }
-       
-       sub=fork();
-       if(sub==0)
-       {
-               t=execl(ENH_PATH,ENH_PATH,path,path,(cgiGetValue(cgi,"f")!=NULL)?(cgiGetValue(cgi,"f")):"0","q",cgiGetValue(cgi,"a")!=NULL?"0":"a",(char *)0);
-               exit(t);
-       }
-       waitpid(sub,&t,0);
-       if(t)mustard(t);
+       s_cgi *cgi;
+       char in_tmp[256];
+       char out_tmp[256];
+       char *in_path;
+       char *t;
+       uint_fast32_t border;
+       uint_fast8_t individual_channels, enhance_alpha, remove_alpha;
        
-       pix=fopen(path,"rb");
-       if(pix==NULL)
-               mustard(123);
-       fstat(fileno(pix),&st);
-       p=st.st_size/1024;
-       q=st.st_size%1024;
+       int r=0;
+       int r1=0;
+       int r2=0;
+       int r3=0;
        
-       printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
-       printf("Content-type: image/png\n\n");
-       for(unsigned long i=0;i<p;++i)
-       {
-               fread(buf,1,1024,pix);
-               fwrite(buf,1,1024,stdout);
-       }
-       if(q!=0)
+       do {
+               make_tmp_path(in_tmp, 256, 0, "");
+               make_tmp_path(out_tmp, 256, 1, ".png");
+               
+               cgi=cgiInit();
+               
+               r = get_file(cgi,"inpix", in_tmp, &in_path);
+               if (r)
+                       break;
+               
+               individual_channels = (cgiGetValue(cgi, "c")!=NULL) ? 0 : 1;
+               enhance_alpha = (cgiGetValue(cgi, "a")!=NULL) ? 1 : 0;
+               remove_alpha = (cgiGetValue(cgi, "n")!=NULL) ? 1 : 0;
+               
+               t = cgiGetValue(cgi, "f");
+               if (t != NULL)
+                       sscanf(t,"%"SCNuFAST32, &border);
+               else
+                       border = 0;
+               
+               r = nofading(
+                       in_path, out_tmp,
+                       border,
+                       individual_channels, enhance_alpha, remove_alpha
+               );
+               if (r)
+                       break;
+               r1 = send_file(out_tmp, "image/png", 0);
+       } while (0);
+       if (r)
        {
-               fread(buf,1,q,pix);
-               fwrite(buf,1,q,stdout);
+               r1 = send_data(nf, nf_size, "image/png", 500);
        }
-       fclose(pix);
-       fflush(stdout);
-       mustard(0);//no mustard
+       r2 = rm(in_tmp);
+       r3 = rm(out_tmp);
+       if (r)
+               return r;
+       if (r1)
+               return r1;
+       if (r2)
+               return r2;
+       if (r3)
+               return r3;
+       return 0;
 }
 
-void mustard(int mustard)
+int nofading (
+       char *inpix, char *outpix,
+       uint_fast32_t border,
+       uint_fast8_t individual_channels,
+       uint_fast8_t enhance_alpha,
+       uint_fast8_t remove_alpha
+)
 {
-       if(mustard)
-       {
-               cgiRedirect(MSTD_PATH);
-       }
-       sub=fork();
-       if(sub==0)
-       {
-               t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
-               exit(t);
-       }
-       waitpid(sub,&t,0);
-       pix=fopen(LOG_PATH,"at");
-       if(pix!=NULL)
+       char s_border[11];
+       pid_t sub;
+       int r;
+       
+       snprintf(s_border, 11, "%"PRIuFAST32, border);
+       
+       sub = fork();
+       if (sub == 0)
        {
-               fprintf(pix,"%x %s\n",mustard,url);
-               fclose(pix);
+               r = execl(
+                       NOFADING_PATH, NOFADING_PATH,
+                       individual_channels ? "-c" : "-0",
+                       enhance_alpha ? "-a" : "-0",
+                       remove_alpha ? "-n" : "-0",
+                       inpix, outpix,
+                       s_border,
+                       (char *)0
+               );
+               exit(r);
        }
-       exit(mustard);
-}
\ No newline at end of file
+       waitpid(sub, &r, 0);
+       return r;
+}