-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum.c
137 lines (111 loc) · 3.51 KB
/
sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* FUNCTION:
* take sum of two things
*
* HISTORY:
* Created Linas Vepstas October 1989
* Updates March 1996
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opers.h"
#include "fileio.h"
/*-------------------------------------------------------------------*/
void sum (float a[], float b[], int len) {
int i;
for (i=0; i<len; i++) {
a[i] += b[i];
}
}
/*-------------------------------------------------------------------*/
void diff (float a[], float b[], int len) {
int i;
for (i=0; i<len; i++) {
a[i] -= b[i];
}
}
/*-------------------------------------------------------------------*/
void mask (float a[], float b[], int len) {
int i;
for (i=0; i<len; i++) {
if (b[i] > 0.5) a[i] = 0.0;
}
}
/*-------------------------------------------------------------------*/
void paste (float a[], float b[], int len) {
int i;
for (i=0; i<len; i++) {
if (b[i] > 0.01) a[i] = b[i];
}
}
/*-------------------------------------------------------------------*/
void angle (float a[], float b[], int len) {
int i;
for (i=0; i<len; i++) {
a[i] = M_PI + atan2 (a[i], b[i]);
a[i] *= 0.5 / M_PI;
}
}
/*-------------------------------------------------------------------*/
void curl (float a[], float b[], int nx, int ny) {
int i, j;
for (j=0; j<ny-1; j++) {
for (i=0; i<nx-1; i++) {
a[nx*j+i] -= a[nx*(j+1)+i];
b[nx*j+i] -= b[nx*j+i+1];
a[nx*j+i] = b[nx*j+i] - a[nx*j+i];
}
}
}
/*-------------------------------------------------------------------*/
void divergence (float a[], float b[], int nx, int ny) {
int i, j;
for (j=0; j<ny-1; j++) {
for (i=0; i<nx-1; i++) {
a[nx*j+i] -= a[nx*j+i+1];
b[nx*j+i] -= b[nx*(j+1)+i];
a[nx*j+i] += b[nx*j+i];
}
}
}
/*-------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
if (argc < 4) {
printf ("Usage: %s <input file> <input file> <output file>\n", argv[0]);
return 1;
}
/*----------------------------------------------------*/
/* open input file */
const char* suff;
unsigned int data_width, data_height;/* data array dimensions */
float* data_a = read_floats(argv[1], &suff, &data_width, &data_height);
printf ("input file %s.%s has width %d height %d\n",
argv[1], suff, data_width, data_height);
float* data_b = read_floats(argv[2], &suff, &data_width, &data_height);
printf ("input file %s.%s has width %d height %d\n",
argv[2], suff, data_width, data_height);
int globlen = data_width*data_height;
/*----------------------------------------------------*/
/* strip out leading pathame */
char *cmd = rindex (argv[0], '/');
if (!cmd) {
cmd = argv[0];
} else {
cmd++;
}
if (!strcmp (cmd, "sum")) sum (data_a, data_b, globlen);
if (!strcmp (cmd, "diff")) diff (data_a, data_b, globlen);
if (!strcmp (cmd, "mask")) mask (data_a, data_b, globlen);
if (!strcmp (cmd, "paste")) paste (data_a, data_b, globlen);
if (!strcmp (cmd, "angle")) angle (data_a, data_b, globlen);
if (!strcmp (cmd, "curl")) curl (data_a, data_b, data_width, data_height);
if (!strcmp (cmd, "div")) divergence (data_a, data_b, data_width, data_height);
/*----------------------------------------------------*/
/* open output file */
write_floats(argv[3], suff, data_a, data_width, data_height);
return 0;
}
/* --------------------------- END OF FILE ------------------------- */