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 import math
40
42
43
44
45
47
48
49 self.roiMap = {};
50
51
52
53
54
55
56
58 maskMap = self.createMasks(image, z, t);
59
60 for mask in maskMap:
61 roiClass = None;
62 if(self.roiMap.has_key(mask.getColour())):
63 roiClass = roiMap[mask.getColour()];
64 else:
65 roiClass = ROIClass();
66 self.roiMap[mask.getColour()] = roiClass;
67 roiClass.addMask(mask, z, t);
68
69
70
71
72
73
74
75
77 value = None;
78 mask = None;
79 map = {};
80 for x in range(inputImage.getWidth()):
81 for y in range(inputImage.getHeight()):
82
83 value = inputImage.getRGB(x, y);
84 if(value==Color.black.getRGB()):
85 continue;
86 if (not map.has_key(value)):
87 mask = MaskClass(value);
88 map[value] = mask;
89 else:
90 mask = map[value];
91 mask.add(Point(x, y));
92 return map;
93
94
95
96
97
98
100 roiList = []
101 for roi in self.roiMap:
102 roiList.append(roi.getROI(image));
103 return roiList;
104
106
107
108
109
110
112
113
114 self.points = {};
115
116
117 self.colour = value;
118
119
120 self.min = Point();
121 self.max = Point();
122
123
124 self.width = 0;
125
126
127 self.height = 0;
128
129
130
131
132
135
136
137
138
139
140
142 bytesArray = array.array('B');
143 for cnt in range(int(math.ceil(self.width*self.height))):
144 bytesArray.append(0);
145 position = 0;
146 for y in range(self.max.y):
147 for x in range(self.max.x):
148 if self.points.has_key(Point(x,y)):
149 self.setBit(bytesArray, position, 1)
150 else:
151 self.setBit(bytesArray, position, 0)
152 position = position + 1;
153
154 byteSwappedArray = bytesArray.byteswap();
155 bytesString = bytesArray.tostring();
156 return bytesString;
157
158
159
160
161
162
163
165 if(len(self.points) == 0):
166 self.min = Point(p);
167 self.max = Point(p);
168 else:
169 self.min.x = min(p.x, min.x);
170 self.min.y = min(p.y, min.y);
171 self.max.x = max(p.x, max.x);
172 self.max.y = max(p.y, max.y);
173 self.width = max.x-min.x+1;
174 self.height = max.y-min.y+1;
175 self.points.add(p);
176
177
178
179
180
181
182
194
195 - def setBit(self, data, bit, val):
196 bytePosition = bit/8;
197 bitPosition = bit%8;
198 data[bytePosition] = data[bytePosition] & ~(0x1<<bitPosition) | (val<<bitPosition);
199
201 bytePosition = bit/8;
202 bitPosition = bit%8;
203 if ((data[bytePosition] & (0x1<<bitPosition))!=0):
204 return 1
205 else:
206 return 0
207
209
210
211
212
213
215
216
217
218
219 self.maskMap = {};
220
221
222
223
224
225
226
228 maskList = [];
229 coord = ROICoordinate(z, t);
230 if(self.maskMap.has_key(coord)):
231 maskList = self.maskMap[coord];
232 else:
233 maskList = [];
234 maskMap.put(coord, maskList);
235 maskList.append(mask);
236
237
238
239
240
241
243 roi = ROIData();
244 roi.setId(image.getId());
245
246 for coord in self.maskMap:
247 maskList = maskMap[coord];
248 for mask in maskList:
249 toSaveMask = mask.asMaskData(coord.getZSection(), coord.getTimePoint());
250 roi.addShapeData(toSaveMask);
251 return roi;
252
253
254
255
257
258
259
260
261
263 if(p != None):
264 x = p.x;
265 y = p.y;
266 else:
267 x = 0;
268 y = 0;
269
270
271
272
273
276
277
278
279
280
283
284
285
286
287
290
291
292
293
294
297