Revision: 57150
          http://sourceforge.net/p/brlcad/code/57150
Author:   mohitdaga
Date:     2013-08-26 21:35:21 +0000 (Mon, 26 Aug 2013)
Log Message:
-----------
Modify bwfilter to use icv api. Also this change enables this utility to 
redirect the output to pipes. And offset is now set with -O instead of -o 
flag.(since output image usage that flag and wanted this to be universal)

Modified Paths:
--------------
    brlcad/trunk/src/util/CMakeLists.txt
    brlcad/trunk/src/util/bwfilter.c

Modified: brlcad/trunk/src/util/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/util/CMakeLists.txt        2013-08-26 21:30:33 UTC (rev 
57149)
+++ brlcad/trunk/src/util/CMakeLists.txt        2013-08-26 21:35:21 UTC (rev 
57150)
@@ -59,7 +59,7 @@
 BRLCAD_ADDEXEC(bw3-pix bw3-pix.c libbu)
 BRLCAD_ADDEXEC(bwcrop bwcrop.c libbu)
 BRLCAD_ADDEXEC(bwdiff bwdiff.c libbu)
-BRLCAD_ADDEXEC(bwfilter bwfilter.c libbu)
+BRLCAD_ADDEXEC(bwfilter bwfilter.c "libicv;libbu")
 BRLCAD_ADDEXEC(bwhist bwhist.c libfb)
 BRLCAD_ADDEXEC(bwhisteq bwhisteq.c libbu)
 BRLCAD_ADDEXEC(bwmod bwmod.c "libbu;${M_LIBRARY}")

Modified: brlcad/trunk/src/util/bwfilter.c
===================================================================
--- brlcad/trunk/src/util/bwfilter.c    2013-08-26 21:30:33 UTC (rev 57149)
+++ brlcad/trunk/src/util/bwfilter.c    2013-08-26 21:35:21 UTC (rev 57150)
@@ -32,55 +32,96 @@
 #include "bio.h"
 
 #include "bu.h"
+#include "icv.h"
 
-
-#define MAXLINE (8*1024)
-#define DEFAULT_WIDTH 512
-unsigned char line1[MAXLINE], line2[MAXLINE], line3[MAXLINE], obuf[MAXLINE];
-unsigned char *top, *middle, *bottom, *temp;
-
 /* The filter kernels */
 struct kernels {
     char *name;
     char *uname;               /* What is needed to recognize it */
-    int kern[9];
     int kerndiv;       /* Divisor for kernel */
     int kernoffset;    /* To be added to result */
+    ICV_FILTER filter;
 } kernel[] = {
-    { "Low Pass", "lo", {3, 5, 3, 5, 10, 5, 3, 5, 3}, 42, 0 },
-    { "Laplacian", "la", {-1, -1, -1, -1, 8, -1, -1, -1, -1}, 16, 128 },
-    { "High Pass", "hi", {-1, -2, -1, -2, 13, -2, -1, -2, -1}, 1, 0 },
-    { "Horizontal Gradient", "hg", {1, 0, -1, 1, 0, -1, 1, 0, -1}, 6, 128},
-    { "Vertical Gradient", "vg", {1, 1, 1, 0, 0, 0, -1, -1, -1}, 6, 128 },
-    { "Boxcar Average", "b", {1, 1, 1, 1, 1, 1, 1, 1, 1}, 9, 0 },
-    { NULL, NULL, {0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0 },
+    { "Low Pass", "lo", 42, 0, ICV_FILTER_LOW_PASS },
+    { "Laplacian", "la", 16, 128, ICV_FILTER_LAPLACIAN },
+    { "High Pass", "hi",  1, 0, ICV_FILTER_HIGH_PASS },
+    { "Horizontal Gradient", "hg", 6, 128, ICV_FILTER_HORIZONTAL_GRAD },
+    { "Vertical Gradient", "vg", 6, 128, ICV_FILTER_VERTICAL_GRAD },
+    { "Boxcar Average", "b", 9, 0, ICV_FILTER_BOXCAR_AVERAGE },
+    { NULL, NULL, 0, 0, ICV_FILTER_NULL }
 };
 
-
-int *kern;
 int kerndiv;
 int kernoffset;
-int width = DEFAULT_WIDTH;
-int height = DEFAULT_WIDTH;
+double kerndiv_diff, kernoffset_diff;
+ICV_FILTER filter_type;
+int inx = 512;
+int iny = 512;   /* Default Width */
 int verbose = 0;
 int dflag = 0; /* Different divisor specified */
 int oflag = 0; /* Different offset specified */
 
-char *file_name;
-FILE *infp;
-
 void select_filter(char *str), dousage(void);
 
 char usage[] = "\
-Usage: bwfilter [-f type] [-v] [-d div] [-o offset]\n\
-       [-s squaresize] [-w width] [-n height] [file.bw] > file.bw\n";
+Usage: bwfilter [-f type] [-v] [-d div] [-O offset]\n\
+       [-s squaresize] [-w width] [-n height] [-o out_file.bw] [file.bw] > 
out_file.bw\n";
 
+char *in_file = NULL;
+char *out_file = NULL;
+
+/*
+ * S E L E C T _ F I L T E R
+ *
+ * Looks at the command line string and selects a filter
+ * based on it.
+ */
+void
+select_filter(char *str)
+{
+    int i;
+
+    i = 0;
+    while (kernel[i].name != NULL) {
+       if (bu_strncmp(str, kernel[i].uname, strlen(kernel[i].uname)) == 0)
+           break;
+       i++;
+    }
+
+    if (kernel[i].name == NULL) {
+       /* No match, output list and exit */
+       fprintf(stderr, "Unrecognized filter type \"%s\"\n", str);
+       dousage();
+       bu_exit (3, NULL);
+    }
+    filter_type = kernel[i].filter;
+    /* Have a match, set up that kernel */
+    if (dflag == 1)
+       if(kernel[i].filter != ICV_FILTER_NULL)
+           kerndiv_diff = kerndiv/kernel[i].kerndiv;
+    if (oflag == 1)
+       kernoffset_diff = ICV_CONV_8BIT(kernoffset - 
kernel[i].kernoffset)/(double) kerndiv;
+}
+
+void
+dousage(void)
+{
+    int i;
+    fputs(usage, stderr);
+    fputs("Possible arguments for -f (type):\n", stderr);
+    i = 0;
+    while (kernel[i].name != NULL) {
+       fprintf(stderr, "  %-10s%s\n", kernel[i].uname, kernel[i].name);
+       i++;
+    }
+}
+
 int
 get_args(int argc, char **argv)
 {
     int c;
 
-    while ((c = bu_getopt(argc, argv, "vf:d:o:w:n:s:h?")) != -1) {
+    while ((c = bu_getopt(argc, argv, "vf:d:O:w:n:s:h?")) != -1) {
        switch (c) {
            case 'v':
                verbose++;
@@ -92,57 +133,52 @@
                dflag++;
                kerndiv = atoi(bu_optarg);
                break;
-           case 'o':
+           case 'O':
                oflag++;
                kernoffset = atoi(bu_optarg);
                break;
            case 'w':
-               width = atoi(bu_optarg);
+               inx = atoi(bu_optarg);
                break;
+           case 'o':
+               out_file = bu_optarg;
+               break;
            case 'n':
-               height = atoi(bu_optarg);
+               iny = atoi(bu_optarg);
                break;
            case 's':
-               width = height = atoi(bu_optarg);
+               inx = iny = atoi(bu_optarg);
                break;
            default:            /* '?' */
                return 0;
        }
     }
-
     if (bu_optind >= argc) {
-       if (isatty(fileno(stdin)))
+       if (isatty(fileno(stdin))) {
            return 0;
-       file_name = "-";
-       infp = stdin;
+       }
     } else {
-       file_name = argv[bu_optind];
-       if ((infp = fopen(file_name, "r")) == NULL) {
-           fprintf(stderr,
-                   "bwfilter: cannot open \"%s\" for reading\n",
-                   file_name);
-           return 0;
-       }
+       in_file = argv[bu_optind];
+       bu_optind++;
+       return 1;
     }
 
-    if (isatty(fileno(stdout)))
+    if (!isatty(fileno(stdout)) && out_file!=NULL) {
        return 0;
+    }
 
-    if (argc > ++bu_optind)
-       fprintf(stderr, "bwfilter: excess argument(s) ignored\n");
+    if (argc > ++bu_optind) {
+       bu_log("bwrfilter: excess argument(s) ignored\n");
+    }
 
-    return 1;          /* OK */
+    return 1;
 }
 
 
 int
 main(int argc, char **argv)
 {
-    int x, y;
-    int value, r1, r2, r3;
-    int max, min;
-    size_t ret;
-
+    icv_image_t *img;
     /* Select Default Filter (low pass) */
     select_filter("low");
 
@@ -150,139 +186,18 @@
        dousage();
        bu_exit (1, NULL);
     }
+    img = icv_read(in_file, ICV_IMAGE_BW, inx, iny);
+    if (img == NULL)
+       return 1;
 
-    if (width > MAXLINE) {
-       fprintf(stderr, "bwfilter:  limited to scanlines of %d\n", MAXLINE);
-       bu_exit (1, NULL);
-    }
+    icv_filter(img, filter_type);
 
-    /*
-     * Read in bottom and middle lines.
-     * Write out bottom untouched.
-     */
-    bottom = &line1[0];
-    middle = &line2[0];
-    top    = &line3[0];
-    ret = fread(bottom, sizeof(char), width, infp);
-    if (ret == 0)
-       perror("fread");
 
-    ret = fread(middle, sizeof(char), width, infp);
-    if (ret == 0)
-       perror("fread");
-
-    ret = fwrite(bottom, sizeof(char), width, stdout);
-    if (ret == 0)
-       perror("fwrite");
-
-    if (verbose) {
-       for (x = 0; x < 11; x++)
-           fprintf(stderr, "kern[%d] = %d\n", x, kern[x]);
-    }
-
-    max = 0;
-    min = 255;
-
-    for (y = 1; y < height-1; y++) {
-       /* read in top line */
-       ret = fread(top, sizeof(char), width, infp);
-       if (ret == 0)
-           perror("fread");
-
-       obuf[0] = middle[0];
-       /* Filter a line */
-       for (x = 1; x < width-1; x++) {
-           r1 = top[x-1] * kern[0] + top[x] * kern[1] + top[x+1] * kern[2];
-           r2 = middle[x-1] * kern[3] + middle[x] * kern[4] + middle[x+1] * 
kern[5];
-           r3 = bottom[x-1] * kern[6] + bottom[x] * kern[7] + bottom[x+1] * 
kern[8];
-           value = (r1+r2+r3) / kerndiv + kernoffset;
-           if (value > max) max = value;
-           if (value < min) min = value;
-           if (verbose && (value > 255 || value < 0)) {
-               fprintf(stderr, "Value %d\n", value);
-               fprintf(stderr, "r1=%d, r2=%d, r3=%d\n", r1, r2, r3);
-           }
-           if (value < 0)
-               obuf[x] = 0;
-           else if (value > 255)
-               obuf[x] = 255;
-           else
-               obuf[x] = value;
-       }
-       obuf[width-1] = middle[width-1];
-       ret = fwrite(obuf, sizeof(char), width, stdout);
-       if (ret == 0)
-           perror("fwrite");
-
-       /* Adjust row pointers */
-       temp = bottom;
-       bottom = middle;
-       middle = top;
-       top = temp;
-    }
-    /* write out last line untouched */
-    ret = fwrite(top, sizeof(char), width, stdout);
-    if (ret == 0)
-       perror("fwrite");
-
-    /* Give advise on scaling factors */
-    if (verbose)
-       fprintf(stderr, "Max = %d,  Min = %d\n", max, min);
-
+    icv_write(img, out_file, ICV_IMAGE_BW);
     return 0;
 }
 
-
 /*
- * S E L E C T _ F I L T E R
- *
- * Looks at the command line string and selects a filter
- * based on it.
- */
-void
-select_filter(char *str)
-{
-    int i;
-
-    i = 0;
-    while (kernel[i].name != NULL) {
-       if (bu_strncmp(str, kernel[i].uname, strlen(kernel[i].uname)) == 0)
-           break;
-       i++;
-    }
-
-    if (kernel[i].name == NULL) {
-       /* No match, output list and exit */
-       fprintf(stderr, "Unrecognized filter type \"%s\"\n", str);
-       dousage();
-       bu_exit (3, NULL);
-    }
-
-    /* Have a match, set up that kernel */
-    kern = &kernel[i].kern[0];
-    if (dflag == 0)
-       kerndiv = kernel[i].kerndiv;
-    if (oflag == 0)
-       kernoffset = kernel[i].kernoffset;
-}
-
-
-void
-dousage(void)
-{
-    int i;
-
-    fputs(usage, stderr);
-    fputs("Possible arguments for -f (type):\n", stderr);
-    i = 0;
-    while (kernel[i].name != NULL) {
-       fprintf(stderr, "  %-10s%s\n", kernel[i].uname, kernel[i].name);
-       i++;
-    }
-}
-
-
-/*
  * Local Variables:
  * mode: C
  * tab-width: 8

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to