Orfeo Toolbox  4.0
otbTileMapImageIO.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #include "otbTileMapImageIO.h"
19 
20 #include <cstring>
21 #include <cmath>
22 
23 #include <iostream>
24 #include <fstream>
25 
26 #include "itkMacro.h"
27 
28 #include "itksys/SystemTools.hxx"
29 #include "otbSystem.h"
30 
31 #include "otbGDALImageIO.h"
32 
33 #include "itkTimeProbe.h"
34 #include "otbCurlHelper.h"
35 
36 #include "otbLogo.inc"
37 
38 namespace otb
39 {
40 
42 {
43  // By default set number of dimensions to two.
44  this->SetNumberOfDimensions(2);
45 
46  // By default set pixel type to scalar.
48 
49  // By default set component type to unsigned char
51  m_UseCompression = false;
52  m_CompressionLevel = 4; // Range 0-9; 0 = no file compression, 9 = maximum file compression
53 
54  // Set default spacing to one
55  m_Spacing[0] = 1.0;
56  m_Spacing[1] = 1.0;
57  // Set default origin to zero
58  m_Origin[0] = 0.0;
59  m_Origin[1] = 0.0;
60 
61  m_NbBands = 3;
63 
64  //Resolution depth
65  m_Depth = 8;
66 
67  m_BytePerPixel = 1;
68  m_TileSize = 256;
69 
70  m_UseCache = false;
71  m_ServerName = "";
72  m_CacheDirectory = ".";
73  m_FileSuffix = "png";
75 
76  m_FileNameIsServerName = false;
77 
78  // Set maximum of connections to 10
79  m_MaxConnect = 10;
80 
82 
83  this->AddSupportedWriteExtension(".otb");
84  this->AddSupportedWriteExtension(".OTB");
85 
86  this->AddSupportedReadExtension(".otb");
87  this->AddSupportedReadExtension(".OTB");
88 
90 
91  this->UseStreamedWritingOn();
92  this->UseStreamedReadingOn();
93 }
94 
96 {
97 }
98 
99 // Tell only if the file can be read with TileMap.
100 bool TileMapImageIO::CanReadFile(const char* file)
101 {
102  // First check the extension
103  if (file == NULL)
104  {
105  itkDebugMacro(<< "No filename specified.");
106  return false;
107  }
108 
109  std::string filename = file;
110  std::string::size_type gmPos = filename.rfind(".otb");
111 
112  if ((gmPos != std::string::npos)
113  && (gmPos == filename.length() - 4))
114  {
115  m_FileNameIsServerName = false;
116  return true;
117  }
118  // Filename is http server
119  else if (filename.find("http://") == 0)
120  {
121  m_FileNameIsServerName = true;
122  return true;
123  }
124  return false;
125 }
126 
127 // Used to print information about this object
128 void TileMapImageIO::PrintSelf(std::ostream& os, itk::Indent indent) const
129 {
130  Superclass::PrintSelf(os, indent);
131  os << indent << "Compression Level : " << m_CompressionLevel << "\n";
132 }
133 
134 // Read image with TileMap
135 void TileMapImageIO::Read(void* buffer)
136 {
137  unsigned char * p = static_cast<unsigned char *>(buffer);
138  if (p == NULL)
139  {
140  itkExceptionMacro(<< "Memory allocation error");
141  return;
142  }
143 
144  int totLines = this->GetIORegion().GetSize()[1];
145  int totSamples = this->GetIORegion().GetSize()[0];
146  int firstLine = this->GetIORegion().GetIndex()[1];
147  int firstSample = this->GetIORegion().GetIndex()[0];
148 
149  int nTilesX = (int) ceil(totSamples / static_cast<double>(m_TileSize)) + 1;
150  int nTilesY = (int) ceil(totLines / static_cast<double>(m_TileSize)) + 1;
151 
152  // Clear vectors
153  m_ListFilename.clear();
154  m_ListURLs.clear();
155  m_ListTiles.clear();
156 
157 
158  //Read all the required tiles
159  for (int numTileY = 0; numTileY < nTilesY; numTileY++)
160  {
161  for (int numTileX = 0; numTileX < nTilesX; numTileX++)
162  {
163  double xTile = (firstSample + m_TileSize * numTileX) / ((1 << m_Depth) * static_cast<double>(m_TileSize));
164  double yTile = (firstLine + m_TileSize * numTileY) / ((1 << m_Depth) * static_cast<double>(m_TileSize));
165 
166  std::string lFilename;
167 
168  // Generate Tile filename
169  this->GenerateTileInfo(xTile, yTile, numTileX, numTileY);
170 
171  // Try to read tile from cache
172  if (!this->CanReadFromCache(m_ListTiles.back().filename))
173  {
174  this->GenerateURL(m_ListTiles.back().x, m_ListTiles.back().y);
175  m_ListFilename.push_back(m_ListTiles.back().filename);
176  }
177  }
178  }
179 
180  m_Curl->RetrieveFileMulti(m_ListURLs, m_ListFilename, m_MaxConnect);
181 
182  m_ListURLs.clear();
183 
184  // Generate buffer
185  this->GenerateBuffer(p);
186 
187  otbMsgDevMacro(<< "TileMapImageIO::Read() completed");
188 }
189 
190 /*
191  * This method build tile filename
192  */
193 void TileMapImageIO::GenerateTileInfo(double x, double y, int numTileX, int numTileY)
194 {
195  std::ostringstream quad2;
196  XYToQuadTree2(x, y, quad2);
197 
198  std::ostringstream filename;
199  BuildFileName(quad2, filename);
200 
201  // Build tile informations
202  TileNameAndCoordType lTileInfos;
203  lTileInfos.numTileX = numTileX;
204  lTileInfos.numTileY = numTileY;
205  lTileInfos.x = x;
206  lTileInfos.y = y;
207  lTileInfos.filename = filename.str();
208 
209  // Add to vector
210  m_ListTiles.push_back(lTileInfos);
211 }
212 
213 /*
214  * This method try to read tile from cache
215  */
216 bool TileMapImageIO::CanReadFromCache(const std::string& filename)
217 {
218  // Verify that the file exists and is not a directory
219  bool fileExists = itksys::SystemTools::FileExists(filename.c_str(), true);
220  if (!fileExists)
221  {
222  return false;
223  }
224 
226  imageIO = otb::GDALImageIO::New();
227  return imageIO->CanReadFile(filename.c_str());
228 }
229 
230 /*
231  * This method generate URLs
232  */
233 void TileMapImageIO::GenerateURL(double x, double y)
234 {
235  std::ostringstream urlStream;
236 
237  // Google Map
239  {
240  std::ostringstream quad;
241  XYToQuadTree(x, y, quad);
242 
243  urlStream << m_ServerName;
244  urlStream << quad.str();
245  }
246  // Open Street Map
248  {
249  urlStream << m_ServerName;
250  urlStream << m_Depth;
251  urlStream << "/";
252  urlStream << (long int) (((double) x * (1 << m_Depth)));
253  urlStream << "/";
254  urlStream << (long int) (((double) y * (1 << m_Depth)));
255  urlStream << "." << m_FileSuffix;
256  }
257  // Near Map
259  {
260  urlStream << m_ServerName;
261  urlStream << "hl=en&x=";
262  urlStream << vcl_floor(x * (1 << m_Depth));
263  urlStream << "&y=";
264  urlStream << vcl_floor(y * (1 << m_Depth));
265  urlStream << "&z=";
266  urlStream << m_Depth;
267  urlStream << "&nml=Vert&s=Ga";
268  }
269  // Local addressing
271  {
272  std::ostringstream quad, filename;
273  XYToQuadTree2(x, y, quad);
274  BuildFileName(quad, filename, false);
275  urlStream << m_ServerName;
276  urlStream << filename.str();
277  }
278  else
279  {
280  itkExceptionMacro(<< "TileMapImageIO : Bad addressing Style");
281  }
282 
283  // Add url to vector
284  m_ListURLs.push_back(urlStream.str());
285 }
286 
287 /*
288  * This method generate the output buffer
289  */
290 void TileMapImageIO::GenerateBuffer(unsigned char *p)
291 {
292  int totLines = this->GetIORegion().GetSize()[1];
293  int totSamples = this->GetIORegion().GetSize()[0];
294  int firstLine = this->GetIORegion().GetIndex()[1];
295  int firstSample = this->GetIORegion().GetIndex()[0];
296  int nComponents = this->GetNumberOfComponents();
297 
298  unsigned char * bufferTile = new unsigned char[m_TileSize * m_TileSize * nComponents];
299  for (unsigned int currentTile = 0; currentTile < m_ListTiles.size(); currentTile++)
300  {
301 
302  // Read tile from cache
303  this->ReadTile(m_ListTiles[currentTile].filename, bufferTile);
304 
305  int numTileX = m_ListTiles[currentTile].numTileX;
306  int numTileY = m_ListTiles[currentTile].numTileY;
307 
308  for (int tileJ = 0; tileJ < m_TileSize; tileJ++)
309  {
310  long int yImageOffset = (long int) (m_TileSize * floor(firstLine / static_cast<double>(m_TileSize)) + m_TileSize * numTileY - firstLine + tileJ);
311  if ((yImageOffset >= 0) && (yImageOffset < totLines))
312  {
313  long int xImageOffset = (long int)
314  (m_TileSize * floor(firstSample / static_cast<double>(m_TileSize)) + m_TileSize * numTileX - firstSample);
315  unsigned char * dst = p + nComponents * (xImageOffset + totSamples * yImageOffset);
316  unsigned char * src = bufferTile + nComponents * m_TileSize * tileJ;
317  int size = nComponents * m_TileSize;
318 
319  if (xImageOffset < 0)
320  {
321  dst -= nComponents * xImageOffset;
322  src -= nComponents * xImageOffset;
323  size += nComponents * xImageOffset;
324  }
325  if (xImageOffset + m_TileSize > totSamples)
326  {
327  size += nComponents * (totSamples - xImageOffset - m_TileSize);
328  }
329  if (size > 0)
330  {
331  memcpy(dst, src, size);
332  }
333  }
334  } //end of tile copy
335  } //end of full image copy
336  delete[] bufferTile;
337 }
338 
339 /*
340  * This method read tile in the cache
341  */
342 void TileMapImageIO::ReadTile(const std::string& filename, void * buffer)
343 {
344  otbMsgDevMacro(<< "Retrieving " << filename);
345  unsigned char * bufferCacheFault = NULL;
346 
348  imageIO = otb::GDALImageIO::New();
349 
350  bool lCanRead = imageIO->CanReadFile(filename.c_str());
351 
352  if (lCanRead == true)
353  {
354  imageIO->SetFileName(filename.c_str());
355  imageIO->ReadImageInformation();
356  itk::ImageIORegion ioRegion(2);
357  ioRegion.SetIndex(0, 0);
358  ioRegion.SetIndex(1, 0);
359  ioRegion.SetSize(0, m_TileSize);
360  ioRegion.SetSize(1, m_TileSize);
361  imageIO->SetIORegion(ioRegion);
362 
363  imageIO->Read(buffer);
364  }
365  else
366  {
367  if (bufferCacheFault == NULL)
368  {
369  bufferCacheFault = new unsigned char[m_TileSize * m_TileSize * 3];
370  FillCacheFaults(bufferCacheFault);
371  }
372  memcpy(buffer, bufferCacheFault, m_TileSize * m_TileSize * 3);
373  }
374 }
375 
376 void TileMapImageIO::BuildFileName(const std::ostringstream& quad, std::ostringstream& filename, bool inCache) const
377 {
378 
379  int quadsize = quad.str().size();
380  std::ostringstream directory;
381  if (inCache)
382  {
383  directory << m_CacheDirectory;
384  }
385  //build directory name
386  int i = 0;
387  while ((i < 8) && (i < quadsize))
388  {
389  directory << "/";
390  directory << (quad.str().c_str())[i];
391  ++i;
392  }
393 
394  itksys::SystemTools::MakeDirectory(directory.str().c_str());
395 
396  filename << directory.str();
397  filename << "/";
398  filename << "otb-";
399  filename << quad.str();
400  filename << "." << m_FileSuffix;
401 
402 }
403 
404 /* Fill up dhe image information reading the ascii configuration file */
406 {
407 
408  m_Dimensions[0] = (1 << m_Depth) * m_TileSize;
409  m_Dimensions[1] = (1 << m_Depth) * m_TileSize;
410  otbMsgDevMacro(<< "Get Dimensions : x=" << m_Dimensions[0] << " & y=" << m_Dimensions[1]);
411 
412  // Default Spacing
413  m_Spacing[0] = 1;
414  m_Spacing[1] = 1;
415  m_Origin[0] = 0;
416  m_Origin[1] = 0;
417 
418 
419  if (m_FileName.empty() == true)
420  {
421  itkExceptionMacro(<< "TileMap read : empty image file name file.");
422  }
423 
424  if (m_FileName.find("http://") == 0)
425  {
426  m_FileNameIsServerName = true;
427  }
428 
430  {
431  std::ifstream file(m_FileName.c_str(), std::ifstream::in);
432  std::getline(file, m_ServerName);
433  if (m_ServerName.find("http://") != 0)
434  {
435  itkExceptionMacro(<< "Can't read server name from file");
436  }
437  std::getline(file, m_FileSuffix);
438  std::string mode;
439  std::getline(file, mode);
440  switch (atoi(mode.c_str()))
441  {
442  case 0:
444  break;
445  case 1:
447  break;
448  case 2:
450  break;
451  case 3:
453  break;
454  default:
455  itkExceptionMacro(<< "Addressing style unknown");
456  }
457 
458  otbMsgDevMacro(<< "File parameters: " << m_ServerName << " " << m_FileSuffix << " " << m_AddressMode);
459  }
460  else
461  {
463  if (m_ServerName.find("http://") != 0)
464  {
465  itkExceptionMacro(<< "Can't read server name from file");
466  }
467  std::string osmServer = "http://tile.openstreetmap.org/";
468  std::string nmServer = "http://www.nearmap.com/maps/";
469  std::string otbServer1 = "http://tile.orfeo-toolbox.org/hillShade/";
470 
471  if (m_ServerName == osmServer)
472  {
473  m_FileSuffix = "png";
475  }
476  else if (m_ServerName == nmServer)
477  {
478  m_FileSuffix = "jpg";
480  }
481  else if (m_ServerName == otbServer1)
482  {
483  m_FileSuffix = "jpg";
485  }
486  else
487  {
488  m_FileSuffix = "jpg";
490  }
491 
492  // File suffix and addres mode must be set with accessors
493  otbMsgDevMacro(<< "File parameters: " << m_ServerName << " " << m_FileSuffix << " " << m_AddressMode);
494  }
495 
496  // The OSM tiles are 4 bands png, while HillShade & NearMap are 3 bands jpeg
498  this->SetNumberOfComponents(4);
499  else
500  this->SetNumberOfComponents(3);
501 
502  this->SetNumberOfDimensions(2);
503  this->SetFileTypeToBinary();
504  this->SetComponentType(UCHAR);
505 
506 }
507 
508 bool TileMapImageIO::CanWriteFile(const char* name)
509 {
510  // First if filename is provided
511  if (name == NULL)
512  {
513  itkDebugMacro(<< "No filename specified.");
514  return false;
515  }
516 
517  // Check for file extension
518  std::string filename = name;
519  std::string::size_type gmPos = filename.rfind(".otb");
520  if ((gmPos != std::string::npos)
521  && (gmPos == filename.length() - 3))
522  {
523  return true;
524  }
525  return false;
526 }
527 
529 {
530 }
531 
532 void TileMapImageIO::Write(const void* buffer)
533 {
534 
535  const unsigned char * p = static_cast<const unsigned char *>(buffer);
536  if (p == NULL)
537  {
538  itkExceptionMacro(<< "Memory allocation error");
539  return;
540  }
541 
542  if (m_FlagWriteImageInformation == true)
543  {
544  this->WriteImageInformation();
546  }
547 
548  int totLines = this->GetIORegion().GetSize()[1];
549  int totSamples = this->GetIORegion().GetSize()[0];
550  int firstLine = this->GetIORegion().GetIndex()[1];
551  int firstSample = this->GetIORegion().GetIndex()[0];
552  int originLine = (int) this->GetOrigin(1);
553  int originSample = (int) this->GetOrigin(0);
554  int nComponents = this->GetNumberOfComponents();
555 
556  otbMsgDevMacro(<< "TileMapImageIO::Write: Size " << totLines << ", " << totSamples);
557  otbMsgDevMacro(<< "TileMapImageIO::Write: Index " << firstLine << ", " << firstSample);
558  otbMsgDevMacro(<< "TileMapImageIO::Write: Origin " << originLine << ", " << originSample);
559 
560  otbMsgDevMacro(<< " Image size : " << m_Dimensions[0] << "," << m_Dimensions[1]);
561  otbMsgDevMacro(<< " Region read (IORegion) : " << this->GetIORegion());
562  otbMsgDevMacro(<< " Nb Of Components : " << this->GetNumberOfComponents());
563 
564  otbMsgDevMacro(<< " sizeof(streamsize) : " << sizeof(std::streamsize));
565  otbMsgDevMacro(<< " sizeof(streampos) : " << sizeof(std::streampos));
566  otbMsgDevMacro(<< " sizeof(streamoff) : " << sizeof(std::streamoff));
567  otbMsgDevMacro(<< " sizeof(std::ios::beg) : " << sizeof(std::ios::beg));
568  otbMsgDevMacro(<< " sizeof(size_t) : " << sizeof(size_t));
569  otbMsgDevMacro(<< " sizeof(unsigned long) : " << sizeof(unsigned long));
570 
571  //Using integer division:
572  int nTilesX = (originSample + totSamples - 1) / m_TileSize - originSample / m_TileSize + 1;
573  int nTilesY = (originLine + totLines - 1) / m_TileSize - originLine / m_TileSize + 1;
574  otbMsgDevMacro(<< "Number of tile to process " << nTilesX << "x" << nTilesY);
575 
576  unsigned char * bufferTile = new unsigned char[m_TileSize * m_TileSize * nComponents];
577 
578  //Read all the required tiles
579  for (int numTileY = 0; numTileY < nTilesY; numTileY++)
580  {
581  for (int numTileX = 0; numTileX < nTilesX; numTileX++)
582  {
583 
584  //Set tile buffer to 0
585  for (int iInit = 0; iInit < m_TileSize * m_TileSize * nComponents; iInit++)
586  {
587  bufferTile[iInit] = 0;
588  }
589 
590  for (int tileJ = 0; tileJ < m_TileSize; tileJ++)
591  {
592  long int yImageOffset = (long int) (m_TileSize * floor((originLine + firstLine) / static_cast<double>(m_TileSize))
593  + m_TileSize * numTileY - (originLine + firstLine) + tileJ);
594  if ((yImageOffset >= 0) && (yImageOffset < totLines))
595  {
596  long int xImageOffset = (long int)
597  (m_TileSize *
598  floor((originSample +
599  firstSample) / static_cast<double>(m_TileSize)) + m_TileSize * numTileX - (originSample + firstSample));
600  unsigned char * dst = bufferTile + nComponents * m_TileSize * tileJ;
601  const unsigned char * src = p + nComponents * (xImageOffset + totSamples * yImageOffset);
602  int size = nComponents * m_TileSize;
603  if (xImageOffset < 0)
604  {
605  src -= nComponents * xImageOffset;
606  dst -= nComponents * xImageOffset;
607  size += nComponents * xImageOffset;
608  }
609  if (xImageOffset + m_TileSize > totSamples)
610  {
611  size += nComponents * (totSamples - xImageOffset - m_TileSize);
612  }
613  if (size > 0)
614  {
615  memcpy(dst, src, size);
616  }
617 
618  }
619  } //end of tile copy
620 
621  double xTile = (originSample + firstSample + m_TileSize * numTileX) / ((1 << m_Depth) * static_cast<double>(m_TileSize));
622  double yTile = (originLine + firstLine + m_TileSize * numTileY) / ((1 << m_Depth) * static_cast<double>(m_TileSize));
623  //Write the tile
624  InternalWrite(xTile, yTile, bufferTile);
625 
626  }
627  } //end of full image copy
628 
629  delete[] bufferTile;
630 
631  otbMsgDevMacro(<< "TileMapImageIO::Write() completed");
632 }
633 
634 void TileMapImageIO::InternalWrite(double x, double y, const void* buffer)
635 {
636  std::ostringstream quad;
637 
638  otbMsgDevMacro(<< x << ", " << y);
639 
640  XYToQuadTree2(x, y, quad);
641 
642  std::ostringstream filename;
643  BuildFileName(quad, filename);
644 
646  imageIO = otb::GDALImageIO::New();
647  bool lCanWrite = imageIO->CanWriteFile(filename.str().c_str());
648 
649 
650  if (lCanWrite)
651  {
652  itksys::SystemTools::RemoveFile(filename.str().c_str());
653 
654  imageIO->CanStreamWrite();
655  imageIO->SetNumberOfDimensions(2);
656  imageIO->SetDimensions(0, m_TileSize);
657  imageIO->SetDimensions(1, m_TileSize);
658  imageIO->SetSpacing(0, 1);
659  imageIO->SetSpacing(1, 1);
660  imageIO->SetOrigin(0, 0);
661  imageIO->SetOrigin(1, 0);
662  imageIO->SetNumberOfComponents(3);
663 
664  vnl_vector<double> axisDirection(2);
665 
666  axisDirection[0] = 1;
667  axisDirection[1] = 0;
668  imageIO->SetDirection(0, axisDirection);
669  axisDirection[0] = 0;
670  axisDirection[1] = 1;
671  imageIO->SetDirection(1, axisDirection);
672 
673  imageIO->SetUseCompression(1);
674 
675  imageIO->SetFileName(filename.str().c_str());
676  imageIO->WriteImageInformation();
677 
678  itk::ImageIORegion ioRegion(2);
679  for (unsigned int i = 0; i < 2; ++i)
680  {
681  ioRegion.SetSize(i, m_TileSize);
682  ioRegion.SetIndex(i, 0);
683  }
684 
685  imageIO->SetIORegion(ioRegion);
686 
687  imageIO->Write(buffer);
688  }
689  else
690  {
691  itkExceptionMacro(<< "TileMap write : bad file name.");
692  }
693 
694 }
695 
697 int TileMapImageIO::XYToQuadTree(double x, double y, std::ostringstream& quad) const
698 {
699  int lDepth = m_Depth;
700  while (lDepth--) // (post-decrement)
701  {
702  // make sure we only look at fractional part
703  x -= floor(x);
704  y -= floor(y);
705  int quad_index = ((x >= 0.5 ? 1 : 0) + (y >= 0.5 ? 2 : 0));
706 
707  switch (quad_index)
708  {
709  case 0:
710  quad << "q";
711  break;
712  case 1:
713  quad << "r";
714  break;
715  case 2:
716  quad << "t";
717  break;
718  case 3:
719  quad << "s";
720  break;
721  }
722 // level down
723  x *= 2;
724  y *= 2;
725  }
726 
727  return 0;
728 }
729 
731 int TileMapImageIO::XYToQuadTree2(double x, double y, std::ostringstream& quad) const
732 {
733  int lDepth = m_Depth;
734  while (lDepth--) // (post-decrement)
735  {
736  // make sure we only look at fractional part
737  x -= floor(x);
738  y -= floor(y);
739  int quad_index = ((x >= 0.5 ? 1 : 0) + (y >= 0.5 ? 2 : 0));
740 
741  switch (quad_index)
742  {
743  case 0:
744  quad << "0";
745  break;
746  case 1:
747  quad << "1";
748  break;
749  case 2:
750  quad << "2";
751  break;
752  case 3:
753  quad << "3";
754  break;
755  }
756 // level down
757  x *= 2;
758  y *= 2;
759  }
760 
761  return 0;
762 }
763 
764 unsigned int
766  const ImageIORegion& pasteRegion) const
767 {
768  typedef itk::ImageRegion<2> RegionType;
769  RegionType tileMapRegion;
770  RegionType::IndexType index;
771  index[0] = this->GetOrigin(0);
772  index[1] = this->GetOrigin(1);
773  itk::ImageIORegionAdaptor<2>::Convert(pasteRegion, tileMapRegion, index);
774  return m_TileMapSplitter->GetNumberOfSplits(tileMapRegion, numberOfRequestedSplits);
775 }
776 
779  unsigned int numberOfActualSplits,
780  const ImageIORegion& pasteRegion) const
781 {
782  typedef itk::ImageRegion<2> RegionType;
783  RegionType tileMapRegion;
784  RegionType::IndexType index;
785  index[0] = this->GetOrigin(0);
786  index[1] = this->GetOrigin(1);
787  itk::ImageIORegionAdaptor<2>::Convert(pasteRegion, tileMapRegion, index);
788  ImageIORegion returnRegion;
790  numberOfActualSplits,
791  tileMapRegion), returnRegion, index);
792  return returnRegion;
793 }
794 
796 void TileMapImageIO::FillCacheFaults(void* buffer) const
797 {
798  //replicate to make 256x256x3 pixels
799  for (int line = 0; line < m_TileSize; line++)
800  {
801  for (int col = 0; col < m_TileSize/64; col++)
802  {
803  memcpy(((unsigned char *) buffer) + line * m_TileSize * 3 + 64 * 3 * col,
804  kLogoOtb + (line % 64) * 64 * 3,
805  64 * 3);
806  }
807  }
808 }
809 
810 void TileMapImageIO::SetCacheDirectory(const char* _arg)
811 {
812  if (_arg && (_arg == this->m_CacheDirectory))
813  {
814  return;
815  }
816 
817  if (_arg)
818  {
819  // if existing dir, check writable
820  if ( itksys::SystemTools::FileIsDirectory( _arg ) )
821  {
822  std::ostringstream oss;
823  oss<<_arg<<"/foo";
824  if( itksys::SystemTools::Touch( oss.str().c_str(), true ) == false )
825  {
826  itkExceptionMacro( "Error, no write permission in given CacheDirectory "<<_arg<<".");
827  }
828  else
829  {
830  itksys::SystemTools::RemoveFile( oss.str().c_str() );
831  }
832  }
833  // if existing file
834  else if( itksys::SystemTools::FileExists(_arg) == true )
835  {
836  itkExceptionMacro( "Error, given CacheDirectory "<<_arg<<" is an existing file.");
837  }
838  // doesn't exist, try to create it
839  else if( itksys::SystemTools::MakeDirectory( _arg ) == false )
840  {
841  itkExceptionMacro( "Error, no permission to create the given CacheDirectory "<<_arg<<".");
842  }
843  else
844  {
845  itksys::SystemTools::RemoveADirectory( _arg );
846  }
847  this->m_CacheDirectory = _arg;
848  this->m_UseCache = true;
849  }
850  else
851  {
852  this->m_CacheDirectory = "";
853  this->m_UseCache = false;
854  }
855 
856  this->Modified();
857 }
858 
859 void TileMapImageIO::SetCacheDirectory(const std::string& _arg)
860 {
861  this->SetCacheDirectory(_arg.c_str());
862 }
863 
864 } // end namespace otb

Generated at Sat Mar 8 2014 16:22:35 for Orfeo Toolbox with doxygen 1.8.3.1