/
bufferops.h
executable file
·116 lines (109 loc) · 2.76 KB
/
bufferops.h
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
#ifndef BUFFEROPS_H
#define BUFFEROPS_H
#include "buffer.h"
#include "scalar.h"
template <class T, class U>
inline int ToGray(Buffer<T> &obuf, const Buffer<U> &ibuf)
{
int w, h, d;
const U *ip = ibuf.GetBuffer(&w, &h, &d);
if (!obuf.Resize(w, h, 1)) {
errorf(("Unable to resize output buffer"));
return 0;
}
T *op = obuf.GetBuffer();
switch (d) {
case 1:
return obuf.Load(ibuf);
case 2:
return obuf.Load(ip, 2, 0);
case 3:
case 4:
for (int i = 0; i < w*h; i++) {
Translate(op[0], (U) ((54*ip[0] + 183*ip[1] + 19*ip[2])/256));
op++;
ip += d;
}
return 1;
default:
errorf(("Don't know how to convert %d dimensions to gray", d));
return 0;
}
}
template <class T>
inline int Normalize(Buffer<T> &buffer)
{
double min = DBL_MAX, max = -DBL_MAX;
int i, w, h, d;
T *p = buffer.GetBuffer(&w, &h, &d);
// first pass to get minimum and maximum
for (i = 0; i < w*h*d; i++) {
if (min > *p) min = *p;
if (max < *p) max = *p;
p++;
}
// second pass to normalize
p = buffer.GetBuffer();
double range = max - min;
if (Zero(range)) {
min = 0;
range = Limit(*p);
}
for (i = 0; i < w*h*d; i++) {
*p = (T) (Limit(*p)*(*p - min)/range);
p++;
}
return 1;
}
template <class T>
inline int Negate(Buffer<T> &buffer)
{
int i, w, h, d;
T *p = buffer.GetBuffer(&w, &h, &d);
// first pass to get minimum and maximum
for (i = 0; i < w*h*d; i++) {
*p = Limit(*p) - *p;
p++;
}
return 1;
}
template <class T, class U>
inline int Translate(Buffer<T> &obuf, const Buffer<U> &ibuf)
{
int w, h, d;
U *ip = ibuf.GetBuffer(&w, &h, &d);
if (!obuf.Resize(w, h, d)) return 0;
T *op = obuf.GetBuffer();
for (int i = 0; i < w*h*d; i++)
Translate(op[i], ip[i]);
return 1;
}
template <class T, class U>
inline int Normalize(Buffer<T> &obuf, const Buffer<U> &ibuf)
{
double min = DBL_MAX, max = -DBL_MAX;
int i, w, h, d;
U *ip = ibuf.GetBuffer(&w, &h, &d);
// allocate output buffer
if (!obuf.Resize(w, h, d)) return 0;
// first pass to get minimum and maximum from input buffer
for (i = 0; i < w*h*d; i++) {
if (min > *ip) min = *ip;
if (max < *ip) max = *ip;
ip++;
}
// second pass to normalize and copy
ip = ibuf.GetBuffer();
T* op = obuf.GetBuffer();
double range = max - min;
if (Zero(range)) {
min = 0;
range = Limit(*ip);
}
for (i = 0; i < w*h*d; i++) {
*op = (T) (Limit(*op)*(*ip - min)/range);
ip++; op++;
}
return 1;
}
#endif