BRL-CAD
icv_operations.c
Go to the documentation of this file.
1 /* I C V _ O P E R A T I O N S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file icv_operations.c
21  *
22  * Tester function for icv_operations.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 
30 #include "bio.h"
31 #include "bu/log.h"
32 #include "bu/getopt.h"
33 #include "bu/str.h"
34 #include "icv.h"
35 
36 void usage()
37 {
38  bu_log("\
39  [ -O +|-|/|* ]\n\
40  [-b -p -d -m] \n\
41  [-o out_file] file_1 file_2 > [out_file]\n");
42 
43  bu_log("#Image Options\n\
44  \t -b for bw image\n\
45  \t -d for dpix image\n\
46  \t -m for b image\n\
47  \t -p for pix image\n");
48 
49 }
50 
51 int main(int argc, char* argv[])
52 {
53  char *out_file = NULL;
54  char *in_file1 = NULL, *in_file2 = NULL;
55  int c;
56  int inx=0, iny=0;
57  char *operation = NULL;
58  icv_image_t *bif1, *bif2, *out_bif;
60 
61 
62  if (argc<2) {
63  usage();
64  return 1;
65  }
66 
67  while ((c = bu_getopt(argc, argv, "O:o:bpdmh?")) != -1) {
68  switch (c) {
69  case 'o':
70  out_file = bu_optarg;
71  break;
72  case 'O' :
73  operation = bu_optarg;
74  break;
75  case 'b' :
76  format = ICV_IMAGE_BW;
77  break;
78  case 'p' :
79  format = ICV_IMAGE_PIX;
80  break;
81  case 'd' :
82  format = ICV_IMAGE_DPIX;
83  break;
84  case 'm' :
85  format = ICV_IMAGE_PPM;
86  break;
87  default:
88  usage();
89  return 1;
90 
91  }
92  }
93  if (bu_optind > argc) {
94  usage();
95  return 1;
96  }
97  in_file1 = argv[bu_optind];
98  bu_optind++;
99 
100  if (bu_optind > argc) {
101  usage();
102  return 1;
103  }
104  in_file2 = argv[bu_optind];
105 
106  bif1 = icv_read(in_file1, format, inx, iny);
107  bif2 = icv_read(in_file2, format, inx, iny);
108 
109  if ( bif1 == NULL || bif2 == NULL ) {
110  bu_log("Error loading the image.\n");
111  return 1;
112  }
113 
114  if (BU_STR_EQUAL(operation, "+"))
115  out_bif = icv_add(bif1, bif1);
116  else if (BU_STR_EQUAL(operation, "-"))
117  out_bif = icv_sub(bif1, bif2);
118  else if (BU_STR_EQUAL(operation, "/"))
119  out_bif = icv_multiply(bif1, bif2);
120  else if (BU_STR_EQUAL(operation, "*"))
121  out_bif = icv_divide(bif1, bif2);
122  else {
123  bu_log("Using Default operation (+)");
124  out_bif = icv_add(bif1, bif2);
125  }
126 
127  if (!out_bif) {
128  bu_log("Error in Operations\n");
129  }
130 
131  icv_write(out_bif,out_file, format);
132  icv_destroy(bif1);
133  icv_destroy(bif2);
134  icv_destroy(out_bif);
135 
136  return 0;
137 }
138 
139 /*
140  * Local Variables:
141  * tab-width: 8
142  * mode: C
143  * indent-tabs-mode: t
144  * c-file-style: "stroustrup"
145  * End:
146  * ex: shiftwidth=4 tabstop=8
147  */
icv_image_t * icv_read(const char *filename, int format, int width, int height)
Definition: fileformat.c:162
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
icv_image_t * icv_divide(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:234
int icv_destroy(icv_image_t *bif)
Definition: fileformat.c:321
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
Definition: icv.h:86
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
int icv_write(icv_image_t *bif, const char *filename, ICV_IMAGE_FORMAT format)
Definition: fileformat.c:186
void usage()
int main(int argc, char *argv[])
ICV_IMAGE_FORMAT
Definition: icv.h:50
icv_image_t * icv_multiply(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:204
icv_image_t * icv_add(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:146
icv_image_t * icv_sub(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:175
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126