OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimImageDataHelper.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 //*************************************************************************
11 // $Id: ossimImageDataHelper.cpp 23162 2015-02-21 14:46:31Z dburken $
14 #include <ossim/base/ossimLine.h>
15 #include <vector>
16 #include <algorithm>
17 
19 {
20  setImageData(imageData);
21 }
22 
23 
25 {
26  theImageData = imageData;
27  if(theImageData)
28  {
31 // thePolyImageRectangle = ossimPolyArea2d(theImageRectangle.ul(),
32 // theImageRectangle.ur(),
33 // theImageRectangle.lr(),
34 // theImageRectangle.ll());
36  }
37 }
38 
40  const ossimPolygon& region,
41  bool clipPoly)
42 {
43  if(!theImageData) return;
44  switch(theImageData->getScalarType())
45  {
46  case OSSIM_UCHAR:
47  {
48  copyInputToThis(reinterpret_cast<const ossim_uint8*>(input),
49  region,
50  clipPoly);
51  break;
52  }
53  case OSSIM_USHORT16:
54  case OSSIM_USHORT11:
55  case OSSIM_USHORT12:
56  case OSSIM_USHORT13:
57  case OSSIM_USHORT14:
58  case OSSIM_USHORT15:
59  {
60  copyInputToThis(reinterpret_cast<const ossim_uint16*>(input),
61  region,
62  clipPoly);
63  break;
64  }
65  case OSSIM_SSHORT16:
66  {
67  copyInputToThis(reinterpret_cast<const ossim_sint16*>(input),
68  region,
69  clipPoly);
70  break;
71  }
72  case OSSIM_SINT32:
73  {
74  copyInputToThis(reinterpret_cast<const ossim_sint32*>(input),
75  region,
76  clipPoly);
77  break;
78  }
79  case OSSIM_FLOAT:
81  {
82  copyInputToThis(reinterpret_cast<const float*>(input),
83  region,
84  clipPoly);
85  break;
86  }
87  case OSSIM_DOUBLE:
89  {
90  copyInputToThis(reinterpret_cast<const double*>(input),
91  region,
92  clipPoly);
93  break;
94  }
96  default:
97  {
98  break;
99  }
100  }
101 }
102 
104  std::vector<ossimPolygon>& regionList,
105  bool clipPoly)
106 {
107  int i = 0;
108 
109  for(i = 0; i < (int)regionList.size();++i)
110  {
111  fill(color,
112  regionList[i],
113  clipPoly);
114  }
115 }
116 
117 
118 void ossimImageDataHelper::fill(const double* values,
119  std::vector<ossimPolygon>& regionList,
120  bool clipPoly)
121 {
122  int i = 0;
123 
124  for(i = 0; i < (int)regionList.size();++i)
125  {
126  fill(values,
127  regionList[i],
128  clipPoly);
129  }
130 }
131 
133  const ossimPolygon& region,
134  bool clipPoly)
135 {
136  double colorArray[3];
137 
138  colorArray[0] = color.getR();
139  colorArray[1] = color.getG();
140  colorArray[2] = color.getB();
141  switch(theImageData->getScalarType())
142  {
143  case OSSIM_UCHAR:
144  {
145  if(theImageData->getNumberOfBands() <= 3)
146  {
147  fill(static_cast<ossim_uint8>(0),
148  (const double*)colorArray,
149  region,
150  clipPoly);
151 
152  }
153  break;
154  }
156  case OSSIM_USHORT16:
157  case OSSIM_USHORT11:
158  case OSSIM_USHORT12:
159  case OSSIM_USHORT13:
160  case OSSIM_USHORT14:
161  case OSSIM_USHORT15:
162  case OSSIM_SSHORT16:
163  case OSSIM_FLOAT:
165  case OSSIM_DOUBLE:
167  default:
168  {
169  break;
170  }
171  }
172 }
173 
175  const ossimIrect& region,
176  bool clipPoly)
177 {
178  double colorArray[3];
179 
180  colorArray[0] = color.getR();
181  colorArray[1] = color.getG();
182  colorArray[2] = color.getB();
183 
184  switch(theImageData->getScalarType())
185  {
186  case OSSIM_UCHAR:
187  {
188  if(theImageData->getNumberOfBands() <= 3)
189  {
190  fill(static_cast<ossim_uint8>(0),
191  colorArray,
192  region,
193  clipPoly);
194 
195  }
196  break;
197  }
199  case OSSIM_USHORT16:
200  case OSSIM_USHORT11:
201  case OSSIM_USHORT12:
202  case OSSIM_USHORT13:
203  case OSSIM_USHORT14:
204  case OSSIM_USHORT15:
205  case OSSIM_SSHORT16:
206  case OSSIM_FLOAT:
208  case OSSIM_DOUBLE:
210  {
211  break;
212  }
213  default:
214  break;
215  }
216 }
217 
218 void ossimImageDataHelper::fill(const double* values,
219  const ossimPolygon& region,
220  bool clipPoly)
221 {
222  switch(theImageData->getScalarType())
223  {
224  case OSSIM_UCHAR:
225  {
226  fill(static_cast<ossim_uint8>(0),
227  values,
228  region,
229  clipPoly);
230  break;
231  }
232  case OSSIM_USHORT16:
233  case OSSIM_USHORT11:
234  case OSSIM_USHORT12:
235  case OSSIM_USHORT13:
236  case OSSIM_USHORT14:
237  case OSSIM_USHORT15:
238  {
239  fill(static_cast<ossim_uint16>(0),
240  values,
241  region,
242  clipPoly);
243  break;
244  }
245  case OSSIM_SSHORT16:
246  {
247  fill(static_cast<ossim_sint16>(0),
248  values,
249  region,
250  clipPoly);
251  break;
252  }
253  case OSSIM_FLOAT:
255  {
256  fill(static_cast<float>(0),
257  values,
258  region,
259  clipPoly);
260  break;
261  }
262  case OSSIM_DOUBLE:
264  {
265  fill(static_cast<double>(0),
266  values,
267  region,
268  clipPoly);
269  break;
270  }
272  default:
273  {
274  break;
275  }
276  }
277 }
278 
279 void ossimImageDataHelper::fill(const double* values,
280  const ossimIrect& region,
281  bool clipPoly)
282 {
283  switch(theImageData->getScalarType())
284  {
285  case OSSIM_UCHAR:
286  {
287  fill(static_cast<ossim_uint8>(0),
288  values,
289  region,
290  clipPoly);
291  break;
292  }
293  case OSSIM_USHORT16:
294  case OSSIM_USHORT11:
295  case OSSIM_USHORT12:
296  case OSSIM_USHORT13:
297  case OSSIM_USHORT14:
298  case OSSIM_USHORT15:
299  {
300  fill(static_cast<ossim_uint16>(0),
301  values,
302  region,
303  clipPoly);
304  break;
305  }
306  case OSSIM_SSHORT16:
307  {
308  fill(static_cast<ossim_sint16>(0),
309  values,
310  region,
311  clipPoly);
312  break;
313  }
314  case OSSIM_FLOAT:
316  {
317  fill(static_cast<float>(0),
318  values,
319  region,
320  clipPoly);
321  break;
322  }
323  case OSSIM_DOUBLE:
325  {
326  fill(static_cast<double>(0),
327  values,
328  region,
329  clipPoly);
330  break;
331  }
333  default:
334  {
335  break;
336  }
337  }
338 }
339 
340 
341 
342 template <class T>
344  const ossimPolygon& region,
345  bool clipPoly)
346 {
347  if(clipPoly)
348  {
349  ossimPolyArea2d polyArea(region);
350  ossimPolyArea2d clipArea = polyArea&thePolyImageRectangle;
351 
352  vector<ossimPolygon> clipList;
353  // clipArea.getAllVisiblePolygons(clipList);
354  clipArea.getVisiblePolygons(clipList);
355 
356  for(ossim_uint32 i = 0; i < clipList.size();++i)
357  {
358  copyInputToThis( inputBuf, clipList[i] );
359  }
360  }
361  else
362  {
363  copyInputToThis( inputBuf, region );
364  }
365 }
366 
367 template <class T>
369  const ossimPolygon& region)
370 {
371  int n;
372  int i;
373  int y;
374  ossim_int32 miny, maxy, minx, maxx;
375  int x1, y1;
376  int x2, y2;
377  int ind1, ind2;
378  // int ints;
379  vector<int> polyInts;
380  T* buf = reinterpret_cast<T*>(theImageData->getBuf());
381 
383  ossim_int32 bandOffset = 0;
384 
385  region.getIntegerBounds(minx, miny, maxx, maxy);
386  ossim_int32 rowOffset = (miny-theOrigin.y)*theImageData->getWidth();
387  n = region.getVertexCount();
388  /* Fix in 1.3: count a vertex only once */
389  for (y = miny; (y <= maxy); y++)
390  {
391  polyInts.clear();
392  // ints = 0;
393  for (i = 0; (i < n); i++)
394  {
395  if (!i)
396  {
397  ind1 = n - 1;
398  ind2 = 0;
399  }
400  else
401  {
402  ind1 = i - 1;
403  ind2 = i;
404  }
405  y1 = ossim::round<int>(region[ind1].y);
406  y2 = ossim::round<int>(region[ind2].y);
407  if (y1 < y2)
408  {
409  x1 = ossim::round<int>(region[ind1].x);
410  x2 = ossim::round<int>(region[ind2].x);
411  }
412  else if (y1 > y2)
413  {
414  y2 = ossim::round<int>(region[ind1].y);
415  y1 = ossim::round<int>(region[ind2].y);
416  x2 = ossim::round<int>(region[ind1].x);
417  x1 = ossim::round<int>(region[ind2].x);
418  }
419  else
420  {
421  continue;
422  }
423  if ((y >= y1) && (y < y2))
424  {
425  polyInts.push_back((y - y1) * (x2 - x1) / (y2 - y1) + x1);
426  }
427  else if ((y == maxy) && (y > y1) && (y <= y2))
428  {
429  polyInts.push_back((y - y1) * (x2 - x1) / (y2 - y1) + x1);
430  }
431  }
432  std::sort(polyInts.begin(), polyInts.end());
433 
434  for (i = 0; (i < (int)polyInts.size()); i += 2)
435  {
436  ossim_int32 startX = polyInts[i]-theOrigin.x;
437  ossim_int32 endX = polyInts[i+1]-theOrigin.x;
438  while(startX <= endX)
439  {
440  bandOffset = 0;
441  int band;
442  for(band = 0; band < (ossim_int32)theImageData->getNumberOfBands();++band)
443  {
444  buf[rowOffset+bandOffset+startX] = (inputBuf[rowOffset+bandOffset+startX]);
445  bandOffset += blockLength;
446  }
447  ++startX;
448  }
449  }
450  rowOffset += theImageData->getWidth();
451  }
452 }
453 
454 template <class T>
455 void ossimImageDataHelper::fill(T dummyVariable,
456  const double* values,
457  const ossimPolygon& region,
458  bool clipPoly)
459 {
460  if(clipPoly)
461  {
462  ossimPolyArea2d polyArea(region);
463  ossimPolyArea2d clipArea = polyArea&thePolyImageRectangle;
464 
465  vector<ossimPolygon> clipList;
466 
467  clipArea.getVisiblePolygons(clipList);
468 
469  for(ossim_uint32 i = 0; i < clipList.size();++i)
470  {
471  fill( dummyVariable, values, clipList[i] );
472  }
473  }
474  else
475  {
476  fill( dummyVariable, values, region );
477  }
478 }
479 
480 template <class T>
481 void ossimImageDataHelper::fill(T dummyVariable,
482  const double* values,
483  const ossimIrect& region,
484  bool clipPoly)
485 {
486  if(clipPoly)
487  {
488  fill(dummyVariable,
489  values,
491  }
492  else
493  {
494  fill(dummyVariable,
495  values,
496  region);
497  }
498 }
499 
500 template <class T>
501 void ossimImageDataHelper::fill(T /* dummyVariable */,
502  const double* values,
503  const ossimPolygon& region)
504 {
505  int n;
506  int i;
507  int y;
508  ossim_int32 miny, maxy, minx, maxx;
509  int x1, y1;
510  int x2, y2;
511  int ind1, ind2;
512  // int ints;
513  vector<int> polyInts;
514  T* buf = reinterpret_cast<T*>(theImageData->getBuf());
515 
517  ossim_int32 bandOffset = 0;
518 
519  region.getIntegerBounds(minx, miny, maxx, maxy);
520  ossim_int32 rowOffset = (miny-theOrigin.y)*theImageData->getWidth();
521  n = region.getVertexCount();
522  /* Fix in 1.3: count a vertex only once */
523  for (y = miny; (y <= maxy); y++)
524  {
525  polyInts.clear();
526  // ints = 0;
527  for (i = 0; (i < n); i++)
528  {
529  if (!i)
530  {
531  ind1 = n - 1;
532  ind2 = 0;
533  }
534  else
535  {
536  ind1 = i - 1;
537  ind2 = i;
538  }
539  y1 = ossim::round<int>(region[ind1].y);
540  y2 = ossim::round<int>(region[ind2].y);
541  if (y1 < y2)
542  {
543  x1 = ossim::round<int>(region[ind1].x);
544  x2 = ossim::round<int>(region[ind2].x);
545  }
546  else if (y1 > y2)
547  {
548  y2 = ossim::round<int>(region[ind1].y);
549  y1 = ossim::round<int>(region[ind2].y);
550  x2 = ossim::round<int>(region[ind1].x);
551  x1 = ossim::round<int>(region[ind2].x);
552  }
553  else
554  {
555  continue;
556  }
557  if ((y >= y1) && (y < y2))
558  {
559  polyInts.push_back((y - y1) * (x2 - x1) / (y2 - y1) + x1);
560  }
561  else if ((y == maxy) && (y > y1) && (y <= y2))
562  {
563  polyInts.push_back((y - y1) * (x2 - x1) / (y2 - y1) + x1);
564  }
565  }
566  std::sort(polyInts.begin(), polyInts.end());
567 
568  for (i = 0; (i < (int)polyInts.size()); i += 2)
569  {
570  ossim_int32 startX = std::abs(polyInts[i]-theOrigin.x);
571  ossim_int32 endX = std::abs(polyInts[i+1]-theOrigin.x);
572  while(startX <= endX)
573  {
574  bandOffset = 0;
575  int band;
576  for(band = 0; band < (int)theImageData->getNumberOfBands();++band)
577  {
578  buf[rowOffset+bandOffset+startX] = (T)(values[band]);
579  bandOffset += blockLength;
580  }
581  ++startX;
582  }
583  }
584  rowOffset += theImageData->getWidth();
585  }
586 }
587 
588 template <class T>
589 void ossimImageDataHelper::fill(T /* dummyVariable */,
590  const double* values,
591  const ossimIrect& region)
592 {
593  T* buf = reinterpret_cast<T*>(theImageData->getBuf());
594 
596  ossim_int32 bandOffset = 0;
597  ossim_int32 miny,maxy;
598  ossim_int32 minx, maxx;
599  ossim_int32 y = 0;
600  miny = region.ul().y-theOrigin.y;
601  maxy = region.lr().y-theOrigin.y;
602  minx = region.ul().x-theOrigin.x;
603  maxx = region.lr().x-theOrigin.x;
604 
605  ossim_int32 rowOffset = (miny)*theImageData->getWidth();
606  ossim_int32 startX = minx;
607  ossim_int32 endX = maxx;
609 
610  for (y = miny; (y <= maxy); ++y)
611  {
612  startX=minx;
613  while(startX <= endX)
614  {
615  bandOffset = 0;
616  int band = 0;
617  for(band = 0; band < bands;++band)
618  {
619  buf[rowOffset+bandOffset+startX] = (T)(values[band]);
620  bandOffset += blockLength;
621  }
622  ++startX;
623  }
624  rowOffset += theImageData->getWidth();
625  }
626 }
627 
16 bit unsigned integer (15 bits used)
virtual ossim_uint32 getWidth() const
void setImageData(ossimImageData *imageData)
virtual ossim_uint32 getNumberOfBands() const
unsigned char getR() const
ossimPolyArea2d thePolyImageRectangle
ossimImageDataHelper(ossimImageData *imageData=0)
ossim_uint32 y
const ossimIpt & ul() const
Definition: ossimIrect.h:274
virtual ossim_uint32 getHeight() const
16 bit unsigned integer (14 bits used)
16 bit unsigned integer (13 bits used)
ossimImageData * theImageData
#define abs(a)
Definition: auxiliary.h:74
void copyInputToThis(const void *input, const ossimPolygon &region, bool clipPoly=true)
void getIntegerBounds(ossim_int32 &minX, ossim_int32 &minY, ossim_int32 &maxX, ossim_int32 &maxY) const
ossim_uint32 getVertexCount() const
os2<< "> n<< " > nendobj n
void fill(const double *values, const ossimIrect &rect, bool clipPoly=true)
32 bit signed integer
unsigned int ossim_uint32
32 bit normalized floating point
virtual ossimIrect getImageRectangle() const
unsigned char getB() const
const ossimIpt & lr() const
Definition: ossimIrect.h:276
ossimIrect clipToRect(const ossimIrect &rect) const
Definition: ossimIrect.cpp:501
unsigned char getG() const
virtual ossimScalarType getScalarType() const
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
ossim_int32 y
Definition: ossimIpt.h:142
virtual const void * getBuf() const
ossim_int32 x
Definition: ossimIpt.h:141
32 bit floating point
bool getVisiblePolygons(vector< ossimPolygon > &polyList) const
16 bit unsigned iteger
64 bit floating point
16 bit signed integer
8 bit unsigned iteger
int ossim_int32
16 bit unsigned integer (12 bits used)