about summary refs log tree commit diff
path: root/lib/libpgm2.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libpgm2.c')
-rw-r--r--lib/libpgm2.c40
1 files changed, 24 insertions, 16 deletions
diff --git a/lib/libpgm2.c b/lib/libpgm2.c
index 80b5cf42..ec3539a2 100644
--- a/lib/libpgm2.c
+++ b/lib/libpgm2.c
@@ -15,34 +15,42 @@
 
 #include "netpbm/pm_c_util.h"
 #include "netpbm/mallocvar.h"
+#include "libpgm.h"
 #include "pgm.h"
 
 
 
 void
-pgm_writepgminit(FILE * const fileP, 
-                 int    const cols, 
-                 int    const rows, 
-                 gray   const maxval, 
+pgm_writepgminit(FILE * const fileP,
+                 int    const cols,
+                 int    const rows,
+                 gray   const maxval,
                  int    const forceplain) {
 
     bool const plainFormat = forceplain || pm_plain_output;
 
-    if (maxval > PGM_OVERALLMAXVAL && !plainFormat) 
+    /* For Caller's convenience, we include validating computability of the
+       image parameters, since Caller may be using them in arithmetic after
+       our return.
+    */
+    pgm_validateComputableSize(cols, rows);
+    pgm_validateComputableMaxval(maxval);
+
+    if (maxval > PGM_OVERALLMAXVAL && !plainFormat)
         pm_error("too-large maxval passed to ppm_writepgminit(): %d.\n"
                  "Maximum allowed by the PGM format is %d.",
                  maxval, PGM_OVERALLMAXVAL);
 
-    fprintf(fileP, "%c%c\n%d %d\n%d\n", 
-            PGM_MAGIC1, 
-            plainFormat || maxval >= 1<<16 ? PGM_MAGIC2 : RPGM_MAGIC2, 
+    fprintf(fileP, "%c%c\n%d %d\n%d\n",
+            PGM_MAGIC1,
+            plainFormat || maxval >= 1<<16 ? PGM_MAGIC2 : RPGM_MAGIC2,
             cols, rows, maxval );
 }
 
 
 
 static void
-putus(unsigned short const n, 
+putus(unsigned short const n,
       FILE *         const fileP) {
 
     if (n >= 10)
@@ -85,7 +93,7 @@ format2bpsRow(const gray    * const grayrow,
 
     for (col = 0; col < cols; ++col) {
         gray const val = grayrow[col];
-        
+
         rowBuffer[bufferCursor++] = val >> 8;
         rowBuffer[bufferCursor++] = (unsigned char) val;
     }
@@ -134,8 +142,8 @@ writepgmrowraw(FILE *       const fileP,
 
 static void
 writepgmrowplain(FILE *       const fileP,
-                 const gray * const grayrow, 
-                 unsigned int const cols, 
+                 const gray * const grayrow,
+                 unsigned int const cols,
                  gray         const maxval) {
 
     int col, charcount;
@@ -163,10 +171,10 @@ writepgmrowplain(FILE *       const fileP,
 
 
 void
-pgm_writepgmrow(FILE *       const fileP, 
-                const gray * const grayrow, 
-                int          const cols, 
-                gray         const maxval, 
+pgm_writepgmrow(FILE *       const fileP,
+                const gray * const grayrow,
+                int          const cols,
+                gray         const maxval,
                 int          const forceplain) {
 
     if (forceplain || pm_plain_output || maxval >= 1<<16)