about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--converter/other/pnmtopng.c2
-rw-r--r--converter/ppm/ilbmtoppm.c18
-rw-r--r--converter/ppm/picttoppm.c2
-rw-r--r--converter/ppm/ppmtoilbm.c24
-rw-r--r--other/pamarith.c4
-rw-r--r--other/pamdepth.c5
6 files changed, 29 insertions, 26 deletions
diff --git a/converter/other/pnmtopng.c b/converter/other/pnmtopng.c
index 1b806cca..72177507 100644
--- a/converter/other/pnmtopng.c
+++ b/converter/other/pnmtopng.c
@@ -2014,7 +2014,7 @@ createPngPalette(pixel              palette_pnm[],
     for (i = 0; i < transSize; ++i) {
         unsigned int const newmv = PALETTEMAXVAL;
         unsigned int const oldmv = alpha_maxval;
-        trans[i] = (trans_pnm[i] * newmv + (oldmv/2)) / oldmv;
+        trans[i] = ROUNDDIV(trans_pnm[i] * newmv, oldmv);
     }
 }
 
diff --git a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c
index f9f9bac3..8bb72662 100644
--- a/converter/ppm/ilbmtoppm.c
+++ b/converter/ppm/ilbmtoppm.c
@@ -1252,7 +1252,7 @@ dcol_to_ppm(FILE *         const ifP,
     unsigned int const greenplanes = dcol->g;
     unsigned int const blueplanes  = dcol->b;
     
-    int col, row, i;
+    int col, row;
     rawtype *Rrow, *Grow, *Brow;
     pixval maxval, redmaxval, greenmaxval, bluemaxval;
     pixval *redtable, *greentable, *bluetable;
@@ -1298,13 +1298,15 @@ dcol_to_ppm(FILE *         const ifP,
     MALLOCARRAY_NOFAIL(greentable, greenmaxval +1);
     MALLOCARRAY_NOFAIL(bluetable,  bluemaxval  +1);
 
-    for( i = 0; i <= redmaxval; i++ )
-        redtable[i] = (i * maxval + redmaxval/2)/redmaxval;
-    for( i = 0; i <= greenmaxval; i++ )
-        greentable[i] = (i * maxval + greenmaxval/2)/greenmaxval;
-    for( i = 0; i <= bluemaxval; i++ )
-        bluetable[i] = (i * maxval + bluemaxval/2)/bluemaxval;
-
+    {
+        unsigned int i;
+        for (i = 0; i <= redmaxval; ++i)
+            redtable[i] = ROUNDDIV(i * maxval, redmaxval);
+        for (i = 0; i <= greenmaxval; ++i)
+            greentable[i] = ROUNDDIV(i * maxval, greenmaxval);
+        for (i = 0; i <= bluemaxval; ++i)
+            bluetable[i] = ROUNDDIV(i * maxval, bluemaxval);
+    }
     if( transpName ) {
         MALLOCVAR_NOFAIL(transpColor);
         *transpColor = ppm_parsecolor(transpName, maxval);
diff --git a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c
index 079f07ca..02f2afec 100644
--- a/converter/ppm/picttoppm.c
+++ b/converter/ppm/picttoppm.c
@@ -1112,7 +1112,7 @@ static pixval
 redepth(pixval const c,
         pixval const oldMaxval) {
     
-    return (c * PPM_MAXMAXVAL + oldMaxval / 2) / oldMaxval;
+    return ROUNDDIV(c * PPM_MAXMAXVAL, oldMaxval);
 }
 
 
diff --git a/converter/ppm/ppmtoilbm.c b/converter/ppm/ppmtoilbm.c
index c0d58edb..4a1b5cb7 100644
--- a/converter/ppm/ppmtoilbm.c
+++ b/converter/ppm/ppmtoilbm.c
@@ -433,14 +433,14 @@ compute_ham_cmap(cols, rows, maxval, maxcolors, colorsP, hbits)
                     tmp = hmap[i].b - b; dist += tmp * tmp;
 
                     if( dist <= maxdist ) {
-                        int sum = hmap[i].count + hmap[col].count;
-
-                        hmap[i].r = (hmap[i].r * hmap[i].count + 
-                                     r * hmap[col].count + sum/2)/sum;
-                        hmap[i].g = (hmap[i].g * hmap[i].count + 
-                                     g * hmap[col].count + sum/2)/sum;
-                        hmap[i].b = (hmap[i].b * hmap[i].count + 
-                                     b * hmap[col].count + sum/2)/sum;
+                        unsigned int sum = hmap[i].count + hmap[col].count;
+
+                        hmap[i].r = ROUNDDIV(hmap[i].r * hmap[i].count + 
+                                             r * hmap[col].count, sum);
+                        hmap[i].g = ROUNDDIV(hmap[i].g * hmap[i].count + 
+                                             g * hmap[col].count, sum);
+                        hmap[i].b = ROUNDDIV(hmap[i].b * hmap[i].count + 
+                                             b * hmap[col].count, sum);
                         hmap[i].count = sum;
 
                         hmap[col] = hmap[i];    /* temp store */
@@ -1776,12 +1776,12 @@ static int *
 make_val_table(oldmaxval, newmaxval)
     int oldmaxval, newmaxval;
 {
-    int i;
-    int *table;
+    unsigned int i;
+    int * table;
 
     MALLOCARRAY_NOFAIL(table, oldmaxval + 1);
-    for(i = 0; i <= oldmaxval; i++ )
-        table[i] = (i * newmaxval + oldmaxval/2) / oldmaxval;
+    for (i = 0; i <= oldmaxval; ++i)
+        table[i] = ROUNDDIV(i * newmaxval, oldmaxval);
 
     return table;
 }
diff --git a/other/pamarith.c b/other/pamarith.c
index d79f1e60..7d973222 100644
--- a/other/pamarith.c
+++ b/other/pamarith.c
@@ -508,7 +508,7 @@ sampleMean(sample       const operands[],
         if (UINT_MAX - operands[i] < sum)
             pm_error("Arithmetic overflow adding samples for mean");
     }
-    return (sum + operandCt/2) / operandCt;
+    return ROUNDDIV(sum, operandCt);
 }
 
 
@@ -656,7 +656,7 @@ applyUnNormalizedFunction(enum function const function,
         break;
     case FN_DIVIDE:
         result = (operands[1] > operands[0]) ?
-            (operands[0] * maxval + operands[1]/2) / operands[1] : maxval;
+            ROUNDDIV(operands[0] * maxval, operands[1]) : maxval;
         break;
 
     case FN_AND:
diff --git a/other/pamdepth.c b/other/pamdepth.c
index 0c4490ed..ee59a408 100644
--- a/other/pamdepth.c
+++ b/other/pamdepth.c
@@ -11,8 +11,9 @@
 =============================================================================*/
 #include <assert.h>
 
-#include "shhopt.h"
+#include "pm_c_util.h"
 #include "mallocvar.h"
+#include "shhopt.h"
 #include "pam.h"
 
 struct cmdlineInfo {
@@ -89,7 +90,7 @@ createSampleMap(sample   const oldMaxval,
     MALLOCARRAY_NOFAIL(sampleMap, oldMaxval+1);
 
     for (i = 0; i <= oldMaxval; ++i)
-        sampleMap[i] = (i * newMaxval + oldMaxval / 2) / oldMaxval;
+        sampleMap[i] = ROUNDDIV(i * newMaxval, oldMaxval);
 
     *sampleMapP = sampleMap;
 }