]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
gstoraster: added handling of empty input 198/head
authorJaiLuthra1 <luthrajaiji@gmail.com>
Sat, 1 Feb 2020 19:00:36 +0000 (00:30 +0530)
committerJaiLuthra1 <luthrajaiji@gmail.com>
Sat, 1 Feb 2020 19:00:36 +0000 (00:30 +0530)
Makefile.am
filter/gstoraster.c

index cc623cc68afd53cb002688af22de1bf0297c65ec..3eda2bd4fea3e302dfb6ff3f41e6109a9fc6562a 100644 (file)
@@ -689,12 +689,16 @@ foomatic_rip_LDADD = \
 gstoraster_SOURCES = \
        filter/gstoraster.c \
        cupsfilters/colord.h \
-       cupsfilters/raster.h
+       cupsfilters/raster.h \
+       filter/pdf.cxx \
+       filter/pdf.h
 gstoraster_CFLAGS = \
        $(CUPS_CFLAGS) \
+       $(LIBQPDF_CFLAGS) \
        -I$(srcdir)/cupsfilters/
 gstoraster_LDADD = \
        $(CUPS_LIBS) \
+       $(LIBQPDF_LIBS) \
        libcupsfilters.la
 
 imagetopdf_SOURCES = \
index f3424f32711b822d1182f5be734ecef855c3472c..c2e1f69e01925a15312ae85578d892f3ca82f6cc 100644 (file)
@@ -48,6 +48,7 @@ MIT Open Source License  -  http://www.opensource.org/
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include "pdf.h"
 
 #define PDF_MAX_CHECK_COMMENT_LINES    20
 
@@ -587,6 +588,7 @@ main (int argc, char **argv, char *envp[])
   char *outformat_env = NULL;
   OutFormatType outformat;
   char buf[BUFSIZ];
+  char *filename;
   char *icc_profile = NULL;
   /*char **qualifier = NULL;*/
   char *tmp;
@@ -663,6 +665,8 @@ main (int argc, char **argv, char *envp[])
       fprintf(stderr, "ERROR: Can't create temporary file\n");
       goto out;
     }
+
+    filename = buf;
     /* remove name */
     unlink(buf);
 
@@ -692,15 +696,73 @@ main (int argc, char **argv, char *envp[])
         fprintf(stderr, "ERROR: Can't open input file %s\n",argv[6]);
         goto out;
     }
+    filename = argv[6];
   }
 
   /* find out file type */
   doc_type = parse_doc_type(fp);
   if (doc_type == GS_DOC_TYPE_UNKNOWN) {
+    char buf[1];
+    rewind(fp);
+    if (fread(buf, 1, 1, fp) == 0) {
+      fprintf(stderr, "DEBUG: Input is empty, outputting empty file.\n");
+      status = 0;
+      if (outformat == OUTPUT_FORMAT_RASTER)
+        fprintf(stdout, "RaS2");
+      goto out;
+    }
     fprintf(stderr, "ERROR: Can't detect file type\n");
     goto out;
   }
 
+  if (doc_type == GS_DOC_TYPE_PDF) {  
+    int pages = pdf_pages(filename);
+
+    if (pages == 0) {
+      fprintf(stderr, "DEBUG: No pages left, outputting empty file.\n");
+      status = 0;
+      if (outformat == OUTPUT_FORMAT_RASTER)
+        fprintf(stdout, "RaS2");
+      goto out;
+    }
+    if (pages < 0) {
+      fprintf(stderr, "DEBUG: Unexpected page count\n");
+      goto out;
+    }
+  }
+  else {
+    char gscommand[65536];
+    char output[31] = "";
+    int pagecount;
+    size_t bytes;
+    snprintf(gscommand, 65536, "yes | gs -q -dNOPAUSE -dBATCH -sDEVICE=bbox %s 2>&1 | grep -c HiResBoundingBox",
+             filename);
+
+    FILE *pd = popen(gscommand, "r");
+    if (!pd) {
+      fprintf(stderr, "Failed to execute ghostscript to determine number of input pages!\n");
+      goto out;
+    }
+
+    bytes = fread(output, 1, 31, pd);
+    pclose(pd);
+
+    if (bytes <= 0 || sscanf(output, "%d", &pagecount) < 1)
+      pagecount = -1;
+
+    if (pagecount == 0) {
+      fprintf(stderr, "DEBUG: No pages left, outputting empty file.\n");
+      status = 0;
+      if (outformat == OUTPUT_FORMAT_RASTER)
+        fprintf(stdout, "RaS2");
+      goto out;
+    }
+    if (pagecount < 0) {
+      fprintf(stderr, "DEBUG: Unexpected page count\n");
+      goto out;
+    }
+  }
+
   /*  Check status of color management in CUPS */
   cm_calibrate = cmGetCupsColorCalibrateMode(options, num_options);