1
2
3 """
4 components/tools/OmeroPy/omero/util/UploadMask.py
5
6 -----------------------------------------------------------------------------
7 Copyright (C) 2006-2009 University of Dundee. All rights reserved.
8
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 ------------------------------------------------------------------------------
24
25
26 @author Jean-Marie Burel
27 <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
28 @author Donald MacDonald
29 <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
30 @version 3.0
31 <small>
32 (<b>Internal version:</b> $Revision: $Date: $)
33 </small>
34 @since 3.0-Beta4.1
35
36 """
37
38 from OmeroPopo import MaskData
39 from OmeroPopo import ROIData
40 from OmeroPopo import ImageData
41 from OmeroPopo import ROICoordinate
42 import math
43
45
46
47
48
50
51
52 self.roiMap = {};
53
54
55
56
57
58
59
71
72
73
74
75
76
77
78
80 value = None;
81 mask = None;
82 map = {};
83 for x in range(inputImage.getWidth()):
84 for y in range(inputImage.getHeight()):
85
86 value = inputImage.getRGB(x, y);
87 if(value==Color.black.getRGB()):
88 continue;
89 if (not map.has_key(value)):
90 mask = MaskClass(value);
91 map[value] = mask;
92 else:
93 mask = map[value];
94 mask.add(Point(x, y));
95 return map;
96
97
98
99
100
101
103 roiList = []
104 for roi in self.roiMap:
105 roiList.append(roi.getROI(image));
106 return roiList;
107
109
110
111
112
113
115
116
117 self.points = {};
118
119
120 self.colour = value;
121
122
123 self.min = Point();
124 self.max = Point();
125
126
127 self.width = 0;
128
129
130 self.height = 0;
131
132
133
134
135
138
139
140
141
142
143
145 import array
146 bytesArray = array.array('B');
147 for cnt in range(int(math.ceil(self.width*self.height))):
148 bytesArray.append(0);
149 position = 0;
150 for y in range(self.max.y):
151 for x in range(self.max.x):
152 if self.points.has_key(Point(x,y)):
153 self.setBit(bytesArray, position, 1)
154 else:
155 self.setBit(bytesArray, position, 0)
156 position = position + 1;
157
158 byteSwappedArray = bytesArray.byteswap();
159 bytesString = bytesArray.tostring();
160 return bytesString;
161
162
163
164
165
166
167
169 if(len(self.points) == 0):
170 self.min = Point(p);
171 self.max = Point(p);
172 else:
173 self.min.x = min(p.x, min.x);
174 self.min.y = min(p.y, min.y);
175 self.max.x = max(p.x, max.x);
176 self.max.y = max(p.y, max.y);
177 self.width = max.x-min.x+1;
178 self.height = max.y-min.y+1;
179 self.points.add(p);
180
181
182
183
184
185
186
198
199 - def setBit(self, data, bit, val):
200 bytePosition = bit/8;
201 bitPosition = bit%8;
202 data[bytePosition] = data[bytePosition] & ~(0x1<<bitPosition) | (val<<bitPosition);
203
205 bytePosition = bit/8;
206 bitPosition = bit%8;
207 if ((data[bytePosition] & (0x1<<bitPosition))!=0):
208 return 1
209 else:
210 return 0
211
213
214
215
216
217
219
220
221
222
223 self.maskMap = {};
224
225
226
227
228
229
230
232 maskList = [];
233 coord = ROICoordinate(z, t);
234 if(self.maskMap.has_key(coord)):
235 maskList = self.maskMap[coord];
236 else:
237 maskList = [];
238 self.maskMap.put(coord, maskList);
239 maskList.append(mask);
240
241
242
243
244
245
256
257
258
259
261
262
263
264
265
267 if(p != None):
268 x = p.x;
269 y = p.y;
270 else:
271 x = 0;
272 y = 0;
273
274
275
276
277
280
281
282
283
284
287
288
289
290
291
294
295
296
297
298
301