-
Notifications
You must be signed in to change notification settings - Fork 1
/
CLineIteratorIP.java
223 lines (194 loc) · 4.74 KB
/
CLineIteratorIP.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
*
*/
package ijaux.scale;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
/**
* @author adminprodanov
*
* Complex inline representation
*
* complex numbers encoding even positions : real part, odd position i-part
*
*/
public class CLineIteratorIP extends IJAbstractLineIterator<float[]> {
private ImageProcessor ip=null;
public static boolean debug=false;
public CLineIteratorIP(ImageProcessor ip, int xdir) {
this.ip=ip;
if (xdir>1) throw new IllegalArgumentException ("illegal direction "+ xdir);
dir=xdir;
if (!(ip instanceof FloatProcessor))
throw new IllegalArgumentException ("illegal type "+ ip.getBitDepth());
final int width=ip.getWidth();
if (width%2==1)
throw new IllegalArgumentException ("odd length");
final int height=ip.getHeight();
btype=32;
initframe(width, height, dir);
}
/**
* @param width
* @param height
*/
private void initframe(final int width, final int height, int dir) {
switch (dir) {
case Ox: {
size=height;
blength=width;
break;
}
case Oy: {
size=width;
blength=height<<1;
break;
}
} // end
//if (debug)
System.out.println("buffer length "+blength
+" size " +size);
initbuffer(dir);
}
/*
*
*/
public float[] getLineFloat(ImageProcessor ip, int k, int dir) {
final int width=ip.getWidth();
final int height=ip.getHeight();
final float[] ret=(float[]) xget();
switch (dir) {
case Ox: {
final int lineno=height;
int offset=k*width;
int z=offset/(width*height);
if (debug)
System.out.println("fetching direction Ox " + z);
if (z>=0 && z<lineno) {
Object aux=ip.getPixels();
try {
System.arraycopy(aux , offset , ret, 0, ret.length);
//System.out.println("offset: "+offset);
} catch (Exception e) {
//System.out.println("offset"+(offset % height));
e.printStackTrace();
}
}
return ret;
}
case Oy: {
//final int lineno=width;
int offset=k*height;
k=k % (width);
//k=k<<1;
if (debug)
System.out.println("fetching direction Oy "+k );
int z=offset/(width*height);
if (z>=0 && z<size) {
try {
float[] pixels= (float[]) ip.getPixels();
int sz=height<<1;
if (pixels!=null)
for (int y=0; y<sz; y+=2) {
int m=y/2;
ret[y]=pixels[k+m*width];
ret[y+1]=pixels[k+1+m*width];
//System.out.print( "("+ k +" " +y +"),");
}
} catch (Exception e) {
System.out.println("k "+ k);
e.printStackTrace();
}
}
return ret;
}
}
return null;
}
public void putLine(float[] line, int k ) {
putLineFloat(ip,line,k, dir);
}
public void putLineFloat(ImageProcessor ip, float[] line, int k, int xdir) {
final int width=ip.getWidth();
final int height=ip.getHeight();
switch (xdir) {
case Ox: {
if (debug)
System.out.println("puting line in Ox : " +k);
final int lineno=height;
//System.out.println("lineno "+lineno);
int offset=k*width;
//System.out.println("offset "+offset);
int z=offset /(width*height);
if (z>=0 && z<lineno) {
//System.out.print(","+z);
Object aux=ip.getPixels();
try {
System.arraycopy(line, 0, aux, offset, width);
//System.out.println(":"+(offset ));
} catch (Exception e) {
System.out.println("offset"+(offset));
e.printStackTrace();
}
}
break;
}
case Oy: {
//final int lineno=width;
k=k % (width);
//k=k<<1;
if (debug)
System.out.println("puting line in Oy "+k);
if (k>=0 && k<size) {
//float[] pixels= (float[]) ip.getPixels();
try {
for (int y=0; y<line.length; y+=2) {
int m=y/2;
ip.setf(k, m, line[y]);
ip.setf(k+1, m, line[y+1]);
// pixels[k+m*width] = line[y];
//pixels[k+1+m*width]=line[y+1];
//System.out.print( "("+ k +" " +y +"),");
}
} catch (Exception e) {
System.out.println("k "+ k );
e.printStackTrace();
}
}
break;
}
}
}
@Override
public boolean isSet() {
return !(ip==null);
}
@Override
public float[] next() {
final float[] ret=getLineFloat(ip, cnt, dir);
//System.out.println("cnt "+cnt);
if (dir==Ox) cnt+=1; else cnt+=2;
return ret;
}
@Override
public void fwd() {
if (dir==Ox) cnt+=1; else cnt+=2;
}
@Override
public void bck() {
if (dir==Ox) cnt-=1; else cnt-=2;
}
@Override
public boolean hasNext() {
return cnt<size;
}
public void setIP(ImageProcessor ip) {
if (ip.getBitDepth()!=btype)
throw new IllegalArgumentException("incompatible type " + btype);
this.ip=ip;
initframe(ip.getWidth(),ip.getHeight(), dir);
}
public void reset() {
cnt=0;
}
}