156 {
157 try
158 {
159 if (Schema == null)
160 {
161 Console.WriteLine("Schema is null.");
162 return null;
163 }
164 var r = Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
165 var tileWidth = Schema.Resolutions[tileInfo.Index.Level].TileWidth;
166 var tileHeight = Schema.Resolutions[tileInfo.Index.Level].TileHeight;
167
168
169
170
171
172 var schemaExtent = Schema.Extent;
173
174
175
176
177
178 var curLevelOffsetXPixel = tileInfo.Index.Col * tileWidth;
179 var curLevelOffsetYPixel = tileInfo.Index.Row * tileHeight;
180
181
182 int lev = tileInfo.Index.Level;
185 : Math.Round(Schema.Extent.Width / r));
188 : Math.Round(Schema.Extent.Height / r));
189
190 long readX = (long)curLevelOffsetXPixel;
191 long readY = (long)curLevelOffsetYPixel;
192
193 int curTileWidth = tileWidth;
194 int curTileHeight = tileHeight;
195
196
197
198 if (readX < 0)
199 {
200 curTileWidth += (int)readX;
201 readX = 0;
202 }
203 if (readY < 0)
204 {
205 curTileHeight += (int)readY;
206 readY = 0;
207 }
208
209
210 curTileWidth = (int)Math.Max(0, Math.Min(curTileWidth, levelPixelW - readX));
211 curTileHeight = (int)Math.Max(0, Math.Min(curTileHeight, levelPixelH - readY));
212
213
214 if (curTileWidth <= 0 || curTileHeight <= 0)
215 {
216 return new byte[tileWidth * tileHeight * 4];
217 }
218
220 {
221 Console.WriteLine("SlideImage is null.");
222 return null;
223 }
224
226 tileInfo.Index.Level, coord,
227 readX, readY,
228 curTileWidth, curTileHeight);
229
230 if (bgraData == null || bgraData.Length == 0)
231 return null;
232
233 int expectedSize = tileWidth * tileHeight * 4;
234
235
236
237
238 if (bgraData.Length == expectedSize)
239 return bgraData;
240
241
242
243
244
245
246 int actualW = Math.Min(curTileWidth, tileWidth);
247 int actualH = Math.Min(curTileHeight, tileHeight);
248 int srcStride = actualW * 4;
249
250
251 if (bgraData.Length < actualW * actualH * 4)
252 {
253 int safePixels = bgraData.Length / 4;
254 actualH = safePixels / actualW;
255 if (actualH == 0) return null;
256 }
257
258 byte[] padded = new byte[expectedSize];
259 for (int row = 0; row < actualH; row++)
260 {
261 int srcOffset = row * srcStride;
262 int dstOffset = row * tileWidth * 4;
263 Buffer.BlockCopy(bgraData, srcOffset, padded, dstOffset, srcStride);
264 }
265 return padded;
266 }
267 catch (Exception e)
268 {
269 Console.WriteLine(e.Message + " " + e.StackTrace);
270 }
271 return null;
272 }
List< Resolution > Resolutions
Definition Bio.cs:1979
unsafe byte[] ReadRegion(int level, ZCT zct, long x, long y, long width, long height)
Copy pre-multiplied BGRA data from a whole slide image.
Definition SlideImage.cs:282
BioImage BioImage
Definition SlideImage.cs:25