fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / FileIO / OSGITFImageFileType.cpp
blobfd19cf27ac843d58a602a1ad5bc3657a790831d7
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
38 //-------------------------------
39 // Includes
40 //-------------------------------
42 #include <cstdlib>
43 #include <cstdio>
45 #include "OSGConfig.h"
47 #include "OSGITFImageFileType.h"
48 #include "OSGGeoReferenceAttachment.h"
49 #include "OSGImageFileHandler.h"
50 #include "OSGPathHandler.h"
52 // Static Class Varible implementations:
53 static const OSG::Char8 *suffixArray[] =
55 "itf"
58 OSG_BEGIN_NAMESPACE
60 TiledImageBlockAccessor::~TiledImageBlockAccessor(void)
64 bool TiledImageBlockAccessor::isOpen(void)
66 return (_vImages.size() != 0);
70 TiledImageBlockAccessor::TiledImageBlockAccessor(void) :
71 Inherited ( ),
72 _uiRows (0),
73 _uiColumns (0),
74 _vImages ( ),
75 _vSampleDescs( ),
76 _vI16Buffer ( )
80 void TiledImageBlockAccessor::open(const Char8 *szFilename)
82 fprintf(stderr, "Open Tiled %s\n", szFilename);
84 FILE *pFile = fopen(szFilename, "r");
86 Char8 szRow[1024];
88 const Char8 *szDelim = " \t\n ";
90 std::string szBuffer;
92 if(pFile != NULL)
94 while(!feof(pFile))
96 fgets(szRow, 1024, pFile);
98 szBuffer.append(szRow);
101 PathHandler *pOrgHandler = ImageFileHandler::the()->getPathHandler();
102 PathHandler tmpHandler;
104 tmpHandler.setBaseFile(szFilename);
106 ImageFileHandler::the()->setPathHandler(&tmpHandler);
108 fprintf(stderr, "got %s\n", szBuffer.c_str());
110 string_token_iterator cIt (szBuffer, szDelim);
111 string_token_iterator cEnd;
113 _uiColumns = TypeTraits<UInt32>::getFromCString((*cIt).c_str());
115 fprintf(stderr, "C: %d\n", _uiColumns);
117 ++cIt;
119 _uiRows = TypeTraits<UInt32>::getFromCString((*cIt).c_str());
121 fprintf(stderr, "R: %d\n", _uiRows);
123 ++cIt;
125 for(UInt32 i = 0; i < _uiRows * _uiColumns; ++i)
127 if(cIt == cEnd)
129 break;
132 fprintf(stderr, "File -%s-\n", (*cIt).c_str());
134 ImageBlockAccessorPtr pAccess;
136 if(strncmp((*cIt).c_str(), "default:", 7) == 0)
138 DefaultBlockAccessorPtr pDefAccess(
139 new DefaultBlockAccessor());
141 pAccess = pDefAccess;
143 pDefAccess->open((*cIt).c_str());
145 else
147 pAccess = ImageFileHandler::the()->open((*cIt).c_str());
150 _vImages.push_back(pAccess);
152 ++cIt;
156 ImageFileHandler::the()->setPathHandler(pOrgHandler);
158 if(_vImages.size() != _uiRows * _uiColumns || _vImages.size() == 0)
160 fprintf(stderr, "Images missing %" PRISize " %d\n",
161 _vImages.size(),
162 _uiRows * _uiColumns);
164 _vImages.clear();
166 return;
169 _vSampleDescs.resize(_vImages.size());
171 UInt32 uiIdx = 0;
172 UInt32 uiOtherIdx = 0;
174 _vSampleDescs[0].setBounds( 0,
176 _vImages[0]->getSize()[0],
177 _vImages[0]->getSize()[1]);
179 for(UInt32 i = 1; i < _uiRows; ++i)
181 uiIdx = i * _uiColumns;
182 uiOtherIdx = uiIdx - _uiColumns;
184 _vSampleDescs[uiIdx].setBounds(
185 _vSampleDescs[uiOtherIdx]._x0,
186 _vSampleDescs[uiOtherIdx]._y1,
187 _vImages [uiIdx ]->getSize()[0],
188 _vImages [uiIdx ]->getSize()[1]);
191 for(UInt32 i = 1; i < _uiColumns; ++i)
193 _vSampleDescs[i].setBounds(
194 _vSampleDescs[i - 1]._x1,
195 _vSampleDescs[i - 1]._y0,
196 _vImages [i ]->getSize()[0],
197 _vImages [i ]->getSize()[1]);
201 for(UInt32 i = 1; i < _uiRows; ++i)
203 for(UInt32 j = 1; j < _uiColumns; ++j)
205 uiIdx = i * _uiColumns + j;
206 uiOtherIdx = (i - 1) * _uiColumns + (j - 1);
208 _vSampleDescs[uiIdx].setBounds(
209 _vSampleDescs[uiOtherIdx]._x1,
210 _vSampleDescs[uiOtherIdx]._y1,
211 _vImages [uiIdx ]->getSize()[0],
212 _vImages [uiIdx ]->getSize()[1]);
216 for(UInt32 i = 0; i < _uiRows; ++i)
218 for(UInt32 j = 0; j < _uiColumns; ++j)
220 uiIdx = i * _uiColumns + j;
222 fprintf(stderr, "(%d)(%d %d) | %d %d %d %d\n",
223 uiIdx, i, j,
224 _vSampleDescs[uiIdx]._x0,
225 _vSampleDescs[uiIdx]._y0,
226 _vSampleDescs[uiIdx]._x1,
227 _vSampleDescs[uiIdx]._y1);
231 _vSize.setValues(_vSampleDescs.back()._x1,
232 _vSampleDescs.back()._y1);
234 GeoReferenceAttachment *pFirstRef =
235 _vImages.front()->getGeoRef();
237 if(pFirstRef != NULL)
239 _pGeoRef = GeoReferenceAttachment::create();
241 _pGeoRef->setOrigin (pFirstRef->getOrigin ());
242 _pGeoRef->setPixelSize (pFirstRef->getPixelSize ());
243 _pGeoRef->setEllipsoidAxis(pFirstRef->getEllipsoidAxis());
244 _pGeoRef->setDatum (pFirstRef->getDatum ());
245 _pGeoRef->setNoDataValue (pFirstRef->getNoDataValue ());
248 _eImgType = _vImages.front()->getType ();
249 _eImgFormat = _vImages.front()->getFormat();
251 _fNoDataValue = _vImages.front()->getNoDataValue();
253 fclose(pFile);
257 bool TiledImageBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
258 Int32 iTextureSize,
259 UInt16 *pTarget,
260 Int32 iTargetSizeBytes)
262 #if 0
263 UInt32 destIdx = 0;
265 UInt8 *pDst = (UInt8 *) pTarget;
267 Int32 xMin = vSampleOrigin.x();
268 Int32 xMax = vSampleOrigin.x() + iTextureSize;
270 Int32 yMin = vSampleOrigin.y();
271 Int32 yMax = vSampleOrigin.y() + iTextureSize;
273 for(UInt32 y = yMin; y < yMax; y++)
275 for(UInt32 x = xMin; x < xMax; x++)
277 for(UInt32 i = 0; i < 2; i++)
279 pDst[destIdx] = 0;
281 destIdx++;
285 destIdx += (iTextureSize - (xMax - xMin)) * 2;
287 #endif
289 return true;
292 bool TiledImageBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
293 Int32 iTextureSize,
294 Int16 *pTarget,
295 Int32 iTargetSizeBytes)
297 Vec2i vLow (vSampleOrigin.x(), vSampleOrigin.y());
299 Vec2i vHeigh(vSampleOrigin.x() + iTextureSize,
300 vSampleOrigin.y() + iTextureSize);
302 UInt32 iFound = 0;
304 Int32 iLow = -1;
305 Int32 iHeigh = -1;
307 for(UInt32 i = 0; i < _vSampleDescs.size(); ++i)
309 if(_vSampleDescs[i].contains(vLow) == true)
311 iLow = i;
313 ++iFound;
316 if(_vSampleDescs[i].contains(vHeigh) == true)
318 iHeigh = i;
320 ++iFound;
323 if(iFound == 2)
324 break;
328 if(iLow == -1 && iHeigh == -1)
330 UInt32 destIdx = 0;
332 for(Int32 y = 0; y < iTextureSize; y++)
334 for(Int32 x = 0; x < iTextureSize; x++)
336 pTarget[destIdx] = Int16(_fNoDataValue);
338 destIdx++;
342 else if(iLow == iHeigh)
344 Vec2i vNewSampleOrigin(vSampleOrigin.x() - _vSampleDescs[iLow]._x0,
345 vSampleOrigin.y() - _vSampleDescs[iLow]._y0);
347 return _vImages[iLow]->readBlockA16(vNewSampleOrigin,
348 iTextureSize,
349 pTarget,
350 iTargetSizeBytes);
352 else if( (iHeigh - iLow) == 1)
354 read2HBlocksA16(iLow,
355 iHeigh,
356 vSampleOrigin,
357 iTextureSize,
358 pTarget,
359 iTargetSizeBytes);
361 else if( (iHeigh - iLow) == Int32(_uiColumns))
363 read2VBlocksA16(iLow,
364 iHeigh,
365 vSampleOrigin,
366 iTextureSize,
367 pTarget,
368 iTargetSizeBytes);
370 else if( (iHeigh - iLow) == Int32(_uiColumns + 1))
372 read4BlocksA16(iLow,
373 iHeigh,
374 vSampleOrigin,
375 iTextureSize,
376 pTarget,
377 iTargetSizeBytes);
379 else if(iLow != -1 && iHeigh == -1)
381 Vec2i vNewSampleOrigin(vSampleOrigin.x() - _vSampleDescs[iLow]._x0,
382 vSampleOrigin.y() - _vSampleDescs[iLow]._y0);
384 return _vImages[iLow]->readBlockA16(vNewSampleOrigin,
385 iTextureSize,
386 pTarget,
387 iTargetSizeBytes);
389 else
391 UInt32 destIdx = 0;
393 for(Int32 y = 0; y < iTextureSize; y++)
395 for(Int32 x = 0; x < iTextureSize; x++)
397 pTarget[destIdx] = 15000;
399 destIdx++;
404 return true;
407 bool TiledImageBlockAccessor::read2HBlocksA16(Int32 iLow,
408 Int32 iHeigh,
409 Vec2i vSampleOrigin,
410 Int32 iTextureSize,
411 Int16 *pTarget,
412 Int32 iTargetSizeBytes)
414 Vec2i vNewSampleOrigin(vSampleOrigin.x() - _vSampleDescs[iLow]._x0,
415 vSampleOrigin.y() - _vSampleDescs[iLow]._y0);
417 _vImages[iLow]->readBlockA16(vNewSampleOrigin,
418 iTextureSize,
419 pTarget,
420 iTargetSizeBytes);
423 _vI16Buffer.resize(iTextureSize * iTextureSize);
425 vNewSampleOrigin.setValues(0,
426 vSampleOrigin.y() - _vSampleDescs[iLow ]._y0);
428 _vImages[iHeigh]->readBlockA16(vNewSampleOrigin,
429 iTextureSize,
430 &(_vI16Buffer[0]),
431 iTargetSizeBytes);
433 UInt32 destIdx = 0;
434 UInt32 srcIdx = 0;
436 Int32 xMin = vSampleOrigin.x();
437 Int32 xMax = vSampleOrigin.x() + iTextureSize;
439 Int32 yMin = vSampleOrigin.y();
440 Int32 yMax = vSampleOrigin.y() + iTextureSize;
442 for(Int32 y = yMin; y < yMax; y++)
444 srcIdx = (y - yMin) * iTextureSize;
446 for(Int32 x = xMin; x < xMax; x++)
448 if(x >= _vSampleDescs[iHeigh]._x0)
450 pTarget[destIdx] = _vI16Buffer[srcIdx];
452 ++srcIdx;
455 ++destIdx;
460 return true;
463 bool TiledImageBlockAccessor::read2VBlocksA16(Int32 iLow,
464 Int32 iHeigh,
465 Vec2i vSampleOrigin,
466 Int32 iTextureSize,
467 Int16 *pTarget,
468 Int32 iTargetSizeBytes)
470 Vec2i vNewSampleOrigin(vSampleOrigin.x() - _vSampleDescs[iLow]._x0,
471 vSampleOrigin.y() - _vSampleDescs[iLow]._y0);
473 _vImages[iLow]->readBlockA16(vNewSampleOrigin,
474 iTextureSize,
475 pTarget,
476 iTargetSizeBytes);
479 _vI16Buffer.resize(iTextureSize * iTextureSize);
481 vNewSampleOrigin.setValues(vSampleOrigin.x() - _vSampleDescs[iLow ]._x0,
484 _vImages[iHeigh]->readBlockA16(vNewSampleOrigin,
485 iTextureSize,
486 &(_vI16Buffer[0]),
487 iTargetSizeBytes);
489 UInt32 destIdx = 0;
490 UInt32 srcIdx = 0;
492 Int32 xMin = vSampleOrigin.x();
493 Int32 xMax = vSampleOrigin.x() + iTextureSize;
495 Int32 yMin = vSampleOrigin.y();
496 Int32 yMax = vSampleOrigin.y() + iTextureSize;
498 for(Int32 y = yMin; y < yMax; y++)
500 for(Int32 x = xMin; x < xMax; x++)
502 if(y >= _vSampleDescs[iHeigh]._y0)
504 pTarget[destIdx] = _vI16Buffer[srcIdx];
506 ++srcIdx;
509 ++destIdx;
513 return true;
516 bool TiledImageBlockAccessor::read4BlocksA16(Int32 iLow,
517 Int32 iHeigh,
518 Vec2i vSampleOrigin,
519 Int32 iTextureSize,
520 Int16 *pTarget,
521 Int32 iTargetSizeBytes)
523 Vec2i vNewSampleOrigin(vSampleOrigin.x() - _vSampleDescs[iLow]._x0,
524 vSampleOrigin.y() - _vSampleDescs[iLow]._y0);
526 _vImages[iLow]->readBlockA16(vNewSampleOrigin,
527 iTextureSize,
528 pTarget,
529 iTargetSizeBytes);
532 _vI16Buffer.resize(iTextureSize * iTextureSize);
534 vNewSampleOrigin.setValues(0,
535 vSampleOrigin.y() - _vSampleDescs[iLow ]._y0);
537 _vImages[iLow + 1]->readBlockA16(vNewSampleOrigin,
538 iTextureSize,
539 &(_vI16Buffer[0]),
540 iTargetSizeBytes);
542 UInt32 destIdx = 0;
543 UInt32 srcIdx = 0;
545 Int32 xMin = vSampleOrigin.x();
546 Int32 xMax = vSampleOrigin.x() + iTextureSize;
548 Int32 yMin = vSampleOrigin.y();
549 Int32 yMax = vSampleOrigin.y() + iTextureSize;
551 for(Int32 y = yMin; y < yMax; y++)
553 srcIdx = (y - yMin) * iTextureSize;
555 for(Int32 x = xMin; x < xMax; x++)
557 if(x >= _vSampleDescs[iLow + 1]._x0)
559 pTarget[destIdx] = _vI16Buffer[srcIdx];
561 ++srcIdx;
564 ++destIdx;
568 vNewSampleOrigin.setValues(vSampleOrigin.x() - _vSampleDescs[iLow ]._x0,
571 _vImages[iLow + _uiColumns]->readBlockA16(vNewSampleOrigin,
572 iTextureSize,
573 &(_vI16Buffer[0]),
574 iTargetSizeBytes);
576 destIdx = 0;
577 srcIdx = 0;
579 for(Int32 y = yMin; y < yMax; y++)
581 for(Int32 x = xMin; x < xMax; x++)
583 if(y >= _vSampleDescs[iLow + _uiColumns]._y0)
585 pTarget[destIdx] = _vI16Buffer[srcIdx];
587 ++srcIdx;
590 ++destIdx;
594 vNewSampleOrigin.setValues(0, 0);
596 _vImages[iHeigh]->readBlockA16(vNewSampleOrigin,
597 iTextureSize,
598 &(_vI16Buffer[0]),
599 iTargetSizeBytes);
600 destIdx = 0;
601 srcIdx = 0;
603 for(Int32 y = yMin; y < yMax; y++)
605 srcIdx = (y - yMin) * iTextureSize;
607 for(Int32 x = xMin; x < xMax; x++)
609 if(x >= _vSampleDescs[iHeigh]._x0 &&
610 y >= _vSampleDescs[iHeigh]._y0)
612 pTarget[destIdx] = _vI16Buffer[srcIdx];
614 ++srcIdx;
617 ++destIdx;
621 return true;
627 DefaultBlockAccessor::~DefaultBlockAccessor(void)
632 bool DefaultBlockAccessor::isOpen(void)
634 return true;
637 bool DefaultBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
638 Int32 iTextureSize,
639 UInt16 *pTarget,
640 Int32 iTargetSizeBytes)
642 return true;
645 bool DefaultBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
646 Int32 iTextureSize,
647 Int16 *pTarget,
648 Int32 iTargetSizeBytes)
650 for(Int32 i = 0; i < iTextureSize * iTextureSize; ++i)
652 pTarget[i] = _iDefaultValue;
655 return true;
658 DefaultBlockAccessor::DefaultBlockAccessor(void) :
659 Inherited ( ),
660 _iDefaultValue(0)
664 void DefaultBlockAccessor::open(const Char8 *szFilename)
666 fprintf(stderr, "OpenDefault %s\n", szFilename);
668 std::string szFName(szFilename);
669 Char8 const *szDelim = ":";
671 string_token_iterator cIt (szFName, szDelim);
672 string_token_iterator cEnd;
674 fprintf(stderr, "File -%s-\n", (*cIt).c_str());
676 ++cIt;
678 if(cIt == cEnd)
679 return;
681 _iDefaultValue = TypeTraits<Int32>::getFromCString((*cIt).c_str());
683 fprintf(stderr, "Def %d\n", _iDefaultValue);
685 ++cIt;
687 if(cIt == cEnd)
688 return;
690 _vSize[0] = TypeTraits<Int32>::getFromCString((*cIt).c_str());
692 ++cIt;
694 if(cIt == cEnd)
695 return;
697 _vSize[1] = TypeTraits<Int32>::getFromCString((*cIt).c_str());
699 fprintf(stderr, "Size %d %d\n", _vSize[0], _vSize[1]);
701 ++cIt;
703 if(cIt == cEnd)
704 return;
706 _eImgType = Image::Type(TypeTraits<UInt32>::getFromCString((*cIt).c_str()));
708 ++cIt;
710 if(cIt == cEnd)
711 return;
713 _eImgFormat =
714 Image::PixelFormat(TypeTraits<UInt32>::getFromCString((*cIt).c_str()));
716 fprintf(stderr, "T: %x F: %x\n",
717 _eImgType,
718 _eImgFormat);
720 ++cIt;
722 if(cIt == cEnd)
723 return;
725 _pGeoRef = GeoReferenceAttachment::create();
727 _pGeoRef->editOrigin()[0] =
728 TypeTraits<Real32>::getFromCString((*cIt).c_str());
730 ++cIt;
732 if(cIt == cEnd)
733 return;
735 _pGeoRef->editOrigin()[1] =
736 TypeTraits<Real32>::getFromCString((*cIt).c_str());
738 ++cIt;
740 if(cIt == cEnd)
741 return;
743 _pGeoRef->editPixelSize()[0] =
744 TypeTraits<Real32>::getFromCString((*cIt).c_str());
746 ++cIt;
748 if(cIt == cEnd)
749 return;
751 _pGeoRef->editPixelSize()[1] =
752 TypeTraits<Real32>::getFromCString((*cIt).c_str());
754 ++cIt;
756 if(cIt == cEnd)
757 return;
759 _pGeoRef->editDatum() =
760 TypeTraits<UInt32>::getFromCString((*cIt).c_str());
762 ++cIt;
764 if(cIt == cEnd)
765 return;
767 _pGeoRef->editEllipsoidAxis()[0] =
768 TypeTraits<Real32>::getFromCString((*cIt).c_str());
770 _pGeoRef->editEllipsoidAxis()[1] =
771 TypeTraits<Real32>::getFromCString((*cIt).c_str());
773 fprintf(stderr, "GeoRef : %f %f | %f %f | %d | %f %f\n",
774 _pGeoRef->getOrigin()[0],
775 _pGeoRef->getOrigin()[1],
776 _pGeoRef->getPixelSize()[0],
777 _pGeoRef->getPixelSize()[1],
778 _pGeoRef->getDatum(),
779 _pGeoRef->getEllipsoidAxis()[0],
780 _pGeoRef->getEllipsoidAxis()[1]);
785 /*! \class GDALImageFileType
787 Image File Type to read/write and store/restore Image objects as
788 GDAL (tif/tiff suffix) data.
790 To be able to load TIFF images you need the IJG TIFF library,
791 (check the Prerequisites page on www.opensg.org).
792 The lib comes with all Linux distributions.
794 You have to --enable-tif in the configure line to enable
795 the singleton object.
799 ITFImageFileType ITFImageFileType:: _the("image/itf",
800 suffixArray,
801 sizeof(suffixArray),
805 //-------------------------------------------------------------------------
806 /*! Tries to fill the image object with the data read from
807 the given fileName. Returns true on success.
811 //-------------------------------------------------------------------------
812 /*! Tries to write the image object to the given output stream.
813 Returns true on success.
817 bool ITFImageFileType::validateHeader(const Char8 *fileName, bool &implemented)
819 implemented = true;
821 return true;
824 ImageBlockAccessorPtr ITFImageFileType::open(const Char8 *fileName)
826 TiledImageBlockAccessorPtr returnValue(new TiledImageBlockAccessor);
828 returnValue->open(fileName);
830 return returnValue;
833 //-------------------------------------------------------------------------
834 /*! Constructor used for the singleton object
837 ITFImageFileType::ITFImageFileType(const Char8 *mimeType,
838 const Char8 *suffixArray[],
839 UInt16 suffixByteCount,
840 UInt32 flags) :
841 Inherited(mimeType,
842 suffixArray,
843 suffixByteCount,
844 flags )
848 //-------------------------------------------------------------------------
849 /*! Destructor
852 ITFImageFileType::~ITFImageFileType(void)
856 OSG_END_NAMESPACE