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