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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use decoders::tiff::*;
use std::fmt;
pub struct CFA {
pub name: String,
pub width: usize,
pub height: usize,
pattern: [[usize;48];48],
}
impl CFA {
#[doc(hidden)] pub fn new_from_tag(pat: &TiffEntry) -> CFA {
let mut patname = String::new();
for i in 0..pat.count() {
patname.push(match pat.get_u32(i as usize) {
0 => 'R',
1 => 'G',
2 => 'B',
_ => 'U',
});
}
CFA::new(&patname)
}
pub fn new(patname: &str) -> CFA {
let (width, height) = match patname.len() {
0 => (0,0),
4 => (2,2),
36 => (6,6),
16 => (2,8),
144 => (12,12),
_ => panic!(format!("Unknown CFA size \"{}\"", patname).to_string()),
};
let mut pattern: [[usize;48];48] = [[0;48];48];
if width > 0 {
for (i,c) in patname.bytes().enumerate() {
pattern[i/width][i%width] = match c {
b'R' => 0,
b'G' => 1,
b'B' => 2,
b'E' => 3,
b'M' => 1,
b'Y' => 3,
_ => panic!(format!("Unknown CFA color \"{}\"", c).to_string()),
};
}
for row in 0..48 {
for col in 0..48 {
pattern[row][col] = pattern[row%height][col%width];
}
}
}
CFA {
name: patname.to_string(),
pattern: pattern,
width: width,
height: height,
}
}
pub fn color_at(&self, row: usize, col: usize) -> usize {
self.pattern[(row+48) % 48][(col+48) % 48]
}
pub fn shift(&self, x: usize, y: usize) -> CFA {
let mut pattern: [[usize;48];48] = [[0;48];48];
for row in 0..48 {
for col in 0..48 {
pattern[row][col] = self.color_at(row+y,col+x);
}
}
CFA {
name: format!("shifted-{}-{}-{}", x, y, self.name).to_string(),
pattern: pattern,
width: self.width,
height: self.height,
}
}
}
impl fmt::Debug for CFA {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CFA {{ {} }}", self.name)
}
}
impl Clone for CFA {
fn clone(&self) -> CFA {
let mut cpattern: [[usize;48];48] = [[0;48];48];
for row in 0..48 {
for col in 0..48 {
cpattern[row][col] = self.pattern[row][col];
}
}
CFA {
name: self.name.clone(),
pattern: cpattern,
width: self.width,
height: self.height,
}
}
}