about summary refs log tree commit diff
path: root/converter/ppm/ppmtoeyuv.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ppm/ppmtoeyuv.c')
-rw-r--r--converter/ppm/ppmtoeyuv.c231
1 files changed, 124 insertions, 107 deletions
diff --git a/converter/ppm/ppmtoeyuv.c b/converter/ppm/ppmtoeyuv.c
index f5ce1156..0cfe9b13 100644
--- a/converter/ppm/ppmtoeyuv.c
+++ b/converter/ppm/ppmtoeyuv.c
@@ -1,9 +1,9 @@
 /* Bryan got this from mm.ftp-cs.berkeley.edu from the package
-   mpeg-encode-1.5b-src under the name ppmtoeyuv.c on March 30, 2000.  
-   The file was dated January 19, 1995.  
+   mpeg-encode-1.5b-src under the name ppmtoeyuv.c on March 30, 2000.
+   The file was dated January 19, 1995.
 
    Bryan changed the program to take an argument as the input filename
-   and fixed a crash when the input image has an odd number of rows or 
+   and fixed a crash when the input image has an odd number of rows or
    columns.
 
    Then Bryan updated the program on March 15, 2001 to use the Netpbm
@@ -12,7 +12,7 @@
 
    There was no attached documentation except for this:  Encoder/Berkeley
    YUV format is merely the concatenation of Y, U, and V data in order.
-   Compare with Abekas YUV, which interlaces Y, U, and V data. 
+   Compare with Abekas YUV, which interlaces Y, U, and V data.
 
    Future enhancement: It may be useful to have an option to do the
    calculations without multiplication tables to save memory at the
@@ -43,7 +43,7 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  */
 
-/*  
+/*
  *  $Header: /n/picasso/users/keving/encode/src/RCS/readframe.c,v 1.1 1993/07/22 22:23:43 keving Exp keving $
  *  $Log: readframe.c,v $
  * Revision 1.1  1993/07/22  22:23:43  keving
@@ -56,6 +56,7 @@
  * HEADER FILES *
  *==============*/
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
@@ -65,43 +66,47 @@
 #include "ppm.h"
 #include "mallocvar.h"
 
-typedef	unsigned char uint8;
+typedef unsigned char uint8;
 
 /* Multiplication tables */
 
 #define YUVMAXVAL 255
 #define HALFYUVMAXVAL 128
-/* multXXX are multiplication tables used in RGB-YCC calculations for 
+/* multXXX are multiplication tables used in RGB-YCC calculations for
    speed.  mult299[x] is x * .299, scaled to a maxval of 255.  These
    are malloc'ed and essentially constant.
 
    We use these tables because it is much faster to do a
    multiplication once for each possible sample value than once for
-   each pixel in the image.  
+   each pixel in the image.
 */
 static float *mult299, *mult587, *mult114;
 static float *mult16874, *mult33126, *mult5;
 static float *mult41869, *mult08131;
 
 static __inline__ float
-luminance(const pixel p) {
+luminance(pixel const p) {
     return mult299[PPM_GETR(p)]
-        + mult587[PPM_GETG(p)] 
+        + mult587[PPM_GETG(p)]
         + mult114[PPM_GETB(p)]
         ;
 }
 
+
+
 static __inline__ float
-chrominance_red(const pixel p) {
-    return mult5[PPM_GETR(p)] 
+chrominanceRed(pixel const p) {
+    return mult5[PPM_GETR(p)]
         + mult41869[PPM_GETG(p)]
         + mult08131[PPM_GETB(p)]
         ;
 }
 
+
+
 static __inline__ float
-chrominance_blue(const pixel p) {
-    return mult16874[PPM_GETR(p)] 
+chrominanceBlue(pixel const p) {
+    return mult16874[PPM_GETR(p)]
         + mult33126[PPM_GETG(p)]
         + mult5[PPM_GETB(p)]
         ;
@@ -110,9 +115,7 @@ chrominance_blue(const pixel p) {
 
 
 static void
-create_multiplication_tables(const pixval maxval) {
-
-    int index;
+createMultiplicationTables(pixval const maxval) {
 
     MALLOCARRAY_NOFAIL(mult299   , maxval+1);
     MALLOCARRAY_NOFAIL(mult587   , maxval+1);
@@ -125,7 +128,9 @@ create_multiplication_tables(const pixval maxval) {
 
     if (maxval == YUVMAXVAL) {
         /* fast path */
-        for ( index = 0; index <= maxval; index++ ) {
+        unsigned int index;
+
+        for (index = 0; index <= maxval; ++index) {
             mult299[index]   =  0.29900*index;
             mult587[index]   =  0.58700*index;
             mult114[index]   =  0.11400*index;
@@ -136,7 +141,8 @@ create_multiplication_tables(const pixval maxval) {
             mult33126[index] = -0.33126*index;
         }
     } else {
-        for ( index = 0; index <= maxval; index++ ) {
+        unsigned int index;
+        for (index = 0; index <= maxval; ++index) {
             mult299[index]   =  0.29900*index*(maxval/YUVMAXVAL);
             mult587[index]   =  0.58700*index*(maxval/YUVMAXVAL);
             mult114[index]   =  0.11400*index*(maxval/YUVMAXVAL);
@@ -146,14 +152,13 @@ create_multiplication_tables(const pixval maxval) {
             mult16874[index] = -0.16874*index*(maxval/YUVMAXVAL);
             mult33126[index] = -0.33126*index*(maxval/YUVMAXVAL);
         }
-        
     }
 }
 
 
 
 static void
-free_multiplication_tables(void) {
+freeMultiplicationTables(void) {
     free(mult299   );
     free(mult587   );
     free(mult114   );
@@ -170,31 +175,33 @@ free_multiplication_tables(void) {
  *
  * PPMtoYUV
  *
- *	convert PPM data into YUV data
- *	assumes that ydivisor = 1
+ *      convert PPM data into YUV data
+ *      assumes that ydivisor = 1
  *
- * RETURNS:	nothing
+ * RETURNS:     nothing
  *
  * SIDE EFFECTS:    none
  *
  * This function processes the input file in 4 pixel squares.  If the
  * Image does not have an even number of rows and columns, the rightmost
  * column or the bottom row gets ignored and output has one fewer row
- * or column than the input. 
+ * or column than the input.
  *
  *===========================================================================*/
-static void 
-PPMtoYUV(pixel ** const ppm_image, const int width, const int height,
-         uint8 *** const orig_yP, 
-         uint8 *** const orig_crP, 
-         uint8 *** const orig_cbP) {
+static void
+ppmToYuv(pixel **     const ppmImage,
+         unsigned int const width,
+         unsigned int const height,
+         uint8 ***    const orig_yP,
+         uint8 ***    const orig_crP,
+         uint8 ***    const orig_cbP) {
 
     int y;
     uint8 ** orig_y;
     uint8 ** orig_cr;
     uint8 ** orig_cb;
 
-    orig_y = *orig_yP;
+    orig_y  = *orig_yP;
     orig_cr = *orig_crP;
     orig_cb = *orig_cbP;
 
@@ -204,17 +211,17 @@ PPMtoYUV(pixel ** const ppm_image, const int width, const int height,
         const pixel *src0, *src1;
           /* Pair of contiguous rows of the ppm input image we are
              converting */
-        int x;
+        unsigned int x;
 
-        src0 = ppm_image[y];
-        src1 = ppm_image[y + 1];
+        src0 = ppmImage[y];
+        src1 = ppmImage[y + 1];
 
         dy0 = orig_y[y];
         dy1 = orig_y[y + 1];
         dcr = orig_cr[y / 2];
         dcb = orig_cb[y / 2];
 
-        for ( x = 0; x + 1 < width; x += 2) {
+        for (x = 0; x + 1 < width; x += 2) {
             dy0[x] = luminance(src0[x]);
             dy1[x] = luminance(src1[x]);
 
@@ -222,17 +229,17 @@ PPMtoYUV(pixel ** const ppm_image, const int width, const int height,
             dy1[x+1] = luminance(src1[x+1]);
 
             dcr[x/2] = ((
-                chrominance_red(src0[x]) +
-                chrominance_red(src1[x]) +
-                chrominance_red(src0[x+1]) +
-                chrominance_red(src1[x+1]) 
+                chrominanceRed(src0[x]) +
+                chrominanceRed(src1[x]) +
+                chrominanceRed(src0[x+1]) +
+                chrominanceRed(src1[x+1])
                 ) / 4) + HALFYUVMAXVAL;
 
             dcb[x/2] = ((
-                chrominance_blue(src0[x]) +
-                chrominance_blue(src1[x]) +
-                chrominance_blue(src0[x+1]) +
-                chrominance_blue(src1[x+1]) 
+                chrominanceBlue(src0[x]) +
+                chrominanceBlue(src1[x]) +
+                chrominanceBlue(src0[x+1]) +
+                chrominanceBlue(src1[x+1])
                 ) / 4) + HALFYUVMAXVAL;
         }
     }
@@ -240,74 +247,81 @@ PPMtoYUV(pixel ** const ppm_image, const int width, const int height,
 
 
 
-static void 
-WriteYUV(FILE *fpointer, const int width, const int height,
-         uint8 ** const orig_y, uint8 ** const orig_cr, uint8 ** const orig_cb)
-{
-    register int y;
+static void
+writeYUV(FILE *       const ofP,
+         unsigned int const width,
+         unsigned int const height,
+         uint8 **     const orig_y,
+         uint8 **     const orig_cr,
+         uint8 **     const orig_cb) {
+
+    unsigned int y;
 
-    for (y = 0; y < height; y++)                        /* Y */
-        fwrite(orig_y[y], 1, width, fpointer);
+    for (y = 0; y < height; ++y)                        /* Y */
+        fwrite(orig_y[y], 1, width, ofP);
 
-    for (y = 0; y < height / 2; y++)                    /* U */
-        fwrite(orig_cb[y], 1, width / 2, fpointer);
+    for (y = 0; y < height / 2; ++y)                    /* U */
+        fwrite(orig_cb[y], 1, width / 2, ofP);
 
-    for (y = 0; y < height / 2; y++)                    /* V */
-        fwrite(orig_cr[y], 1, width / 2, fpointer);
+    for (y = 0; y < height / 2; ++y)                    /* V */
+        fwrite(orig_cr[y], 1, width / 2, ofP);
 }
 
 
 
 static void
-AllocYUV(int       const width, 
+allocYUV(int       const width,
          int       const height,
-         uint8 *** const orig_yP, 
+         uint8 *** const orig_yP,
          uint8 *** const orig_crP,
          uint8 *** const orig_cbP) {
 
-    int y;
+    unsigned int y;
     uint8 ** orig_y;
     uint8 ** orig_cr;
     uint8 ** orig_cb;
 
     MALLOCARRAY_NOFAIL(*orig_yP, height);
     orig_y = *orig_yP;
-    for (y = 0; y < height; y++) 
+    for (y = 0; y < height; ++y)
         MALLOCARRAY_NOFAIL(orig_y[y], width);
 
     MALLOCARRAY_NOFAIL(*orig_crP, height / 2);
     orig_cr = *orig_crP;
-    for (y = 0; y < height / 2; y++) 
+    for (y = 0; y < height / 2; ++y)
         MALLOCARRAY_NOFAIL(orig_cr[y], width / 2);
 
     MALLOCARRAY_NOFAIL(*orig_cbP, height / 2);
     orig_cb = *orig_cbP;
-    for (y = 0; y < height / 2; y++) 
+    for (y = 0; y < height / 2; ++y)
         MALLOCARRAY_NOFAIL(orig_cb[y], width / 2);
 }
 
 
 
 static void
-FreeYUV(const int width, const int height,
-        uint8 ** const orig_y, uint8 ** const orig_cr, uint8 ** const orig_cb){
+freeYUV(unsigned int const width,
+        unsigned int const height,
+        uint8 **     const orig_y,
+        uint8 **     const orig_cr,
+        uint8 **     const orig_cb){
 
-    int y;
+    unsigned int y;
 
     if (orig_y) {
-       for (y = 0; y < height; y++)
+       for (y = 0; y < height; ++y)
            free(orig_y[y]);
        free(orig_y);
     }
 
     if (orig_cr) {
-       for (y = 0; y < height / 2; y++)
+       for (y = 0; y < height / 2; ++y)
            free(orig_cr[y]);
        free(orig_cr);
     }
 
     if (orig_cb) {
-       for (y = 0; y < height / 2; y++)
+       for (y = 0; y < height / 2; ++y)
            free(orig_cb[y]);
        free(orig_cb);
     }
@@ -316,81 +330,84 @@ FreeYUV(const int width, const int height,
 
 
 int
-main(int argc, char **argv) {
-    const char *input_filename;  /* NULL for stdin */
-    FILE * ifp;
+main(int argc, const char **argv) {
+
+    const char *inputFilename;  /* NULL for stdin */
+    FILE * ifP;
     int width, height;
     pixval maxval;
-    pixel **ppm_image;   /* malloc'ed */
+    pixel ** ppmImage;   /* malloc'ed */
     uint8 **orig_y, **orig_cr, **orig_cb;
-        /* orig_y is the height x width array of individual pixel luminances 
+        /* orig_y is the height x width array of individual pixel luminances
            orig_cr and orig_cb are the height/2 x width/2 arrays of average
            red and blue chrominance values over each 4 pixel square.
         */
     int eof;
 
-    /* The following are width, height, and maxval of the image we 
-       processed before this one.  Zero if there was no image before 
+    /* The following are width, height, and maxval of the image we
+       processed before this one.  Zero if there was no image before
        this one.
     */
-    int last_width, last_height;
-    pixval last_maxval;
+    int lastWidth, lastHeight;
+    pixval lastMaxval;
 
-    ppm_init(&argc, argv);
+    pm_proginit(&argc, argv);
 
-    if (argc > 2) {
+    if (argc-1 > 1) {
         pm_error("Program takes either one argument -- "
                 "the input filename -- or no arguments (input is stdin)");
         exit(1);
-    } else if (argc == 2)
-        input_filename = argv[1];
-    else input_filename = NULL;
+    } else if (argc-1 > 0)
+        inputFilename = argv[1];
+    else
+        inputFilename = "-";
 
-    if (input_filename == NULL) ifp = stdin;
-    else ifp = pm_openr(input_filename);
+    ifP = pm_openr(inputFilename);
 
-    eof = FALSE;
-    last_maxval = 0;  /* No previous maxval */
-    last_width = 0;	/* No previous width */
-    last_height = 0;	/* No previous height */
+    eof = false;  /* EOF not yet encountered */
+    lastMaxval = 0;  /* No previous maxval */
+    lastWidth = 0;      /* No previous width */
+    lastHeight = 0;     /* No previous height */
     orig_y = orig_cr = orig_cb = 0;
 
     while (!eof) {
-        ppm_image = ppm_readppm(ifp, &width, &height, &maxval);
+        ppmImage = ppm_readppm(ifP, &width, &height, &maxval);
 
-        if (width % 2 != 0) 
+        if (width % 2 != 0)
             pm_message("Input image has odd number of columns.  The rightmost "
                        "column will be omitted from the output.");
-        if (height % 2 != 0) 
+        if (height % 2 != 0)
             pm_message("Input image has odd number of rows.  The bottom "
                        "row will be omitted from the output.");
 
-        if (maxval != last_maxval) {
+        if (maxval != lastMaxval) {
             /* We're going to need all new multiplication tables. */
-            free_multiplication_tables();
-            create_multiplication_tables(maxval);
+            freeMultiplicationTables();
+            createMultiplicationTables(maxval);
         }
-        last_maxval = maxval;
-        
-        if (height != last_height || width != last_width) {
-            FreeYUV(width, height, orig_y, orig_cr, orig_cb);
+        lastMaxval = maxval;
+
+        if (height != lastHeight || width != lastWidth) {
+            freeYUV(width, height, orig_y, orig_cr, orig_cb);
             /* Need new YUV buffers for different size */
-            AllocYUV(width, height, &orig_y, &orig_cr, &orig_cb);
+            allocYUV(width, height, &orig_y, &orig_cr, &orig_cb);
         }
-        last_height = height;
-        last_width = width;
+        lastHeight = height;
+        lastWidth  = width;
 
-        PPMtoYUV(ppm_image, width, height, &orig_y, &orig_cr, &orig_cb);
+        ppmToYuv(ppmImage, width, height, &orig_y, &orig_cr, &orig_cb);
 
-        WriteYUV(stdout, (width/2)*2, (height/2)*2, orig_y, orig_cr, orig_cb);
+        writeYUV(stdout, (width/2)*2, (height/2)*2, orig_y, orig_cr, orig_cb);
 
-        ppm_freearray(ppm_image, height);
-        ppm_nextimage(ifp, &eof);
+        ppm_freearray(ppmImage, height);
+        ppm_nextimage(ifP, &eof);
     }
-    FreeYUV(width, height, orig_y, orig_cr, orig_cb);
-    free_multiplication_tables();
-    pm_close(ifp);
-        
+    freeYUV(width, height, orig_y, orig_cr, orig_cb);
+    freeMultiplicationTables();
+    pm_close(ifP);
+
     return 0;
 }
 
+
+