1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
38 //-------------------------------
40 //-------------------------------
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
[] =
60 TiledImageBlockAccessor::~TiledImageBlockAccessor(void)
64 bool TiledImageBlockAccessor::isOpen(void)
66 return (_vImages
.size() != 0);
70 TiledImageBlockAccessor::TiledImageBlockAccessor(void) :
80 void TiledImageBlockAccessor::open(const Char8
*szFilename
)
82 fprintf(stderr
, "Open Tiled %s\n", szFilename
);
84 FILE *pFile
= fopen(szFilename
, "r");
88 const Char8
*szDelim
= " \t\n ";
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
);
119 _uiRows
= TypeTraits
<UInt32
>::getFromCString((*cIt
).c_str());
121 fprintf(stderr
, "R: %d\n", _uiRows
);
125 for(UInt32 i
= 0; i
< _uiRows
* _uiColumns
; ++i
)
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());
147 pAccess
= ImageFileHandler::the()->open((*cIt
).c_str());
150 _vImages
.push_back(pAccess
);
156 ImageFileHandler::the()->setPathHandler(pOrgHandler
);
158 if(_vImages
.size() != _uiRows
* _uiColumns
|| _vImages
.size() == 0)
160 fprintf(stderr
, "Images missing %" PRISize
" %d\n",
162 _uiRows
* _uiColumns
);
169 _vSampleDescs
.resize(_vImages
.size());
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",
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();
257 bool TiledImageBlockAccessor::readBlockA16(Vec2i vSampleOrigin
,
260 Int32 iTargetSizeBytes
)
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
++)
285 destIdx
+= (iTextureSize
- (xMax
- xMin
)) * 2;
292 bool TiledImageBlockAccessor::readBlockA16(Vec2i vSampleOrigin
,
295 Int32 iTargetSizeBytes
)
297 Vec2i
vLow (vSampleOrigin
.x(), vSampleOrigin
.y());
299 Vec2i
vHeigh(vSampleOrigin
.x() + iTextureSize
,
300 vSampleOrigin
.y() + iTextureSize
);
307 for(UInt32 i
= 0; i
< _vSampleDescs
.size(); ++i
)
309 if(_vSampleDescs
[i
].contains(vLow
) == true)
316 if(_vSampleDescs
[i
].contains(vHeigh
) == true)
328 if(iLow
== -1 && iHeigh
== -1)
332 for(Int32 y
= 0; y
< iTextureSize
; y
++)
334 for(Int32 x
= 0; x
< iTextureSize
; x
++)
336 pTarget
[destIdx
] = Int16(_fNoDataValue
);
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
,
352 else if( (iHeigh
- iLow
) == 1)
354 read2HBlocksA16(iLow
,
361 else if( (iHeigh
- iLow
) == Int32(_uiColumns
))
363 read2VBlocksA16(iLow
,
370 else if( (iHeigh
- iLow
) == Int32(_uiColumns
+ 1))
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
,
393 for(Int32 y
= 0; y
< iTextureSize
; y
++)
395 for(Int32 x
= 0; x
< iTextureSize
; x
++)
397 pTarget
[destIdx
] = 15000;
407 bool TiledImageBlockAccessor::read2HBlocksA16(Int32 iLow
,
412 Int32 iTargetSizeBytes
)
414 Vec2i
vNewSampleOrigin(vSampleOrigin
.x() - _vSampleDescs
[iLow
]._x0
,
415 vSampleOrigin
.y() - _vSampleDescs
[iLow
]._y0
);
417 _vImages
[iLow
]->readBlockA16(vNewSampleOrigin
,
423 _vI16Buffer
.resize(iTextureSize
* iTextureSize
);
425 vNewSampleOrigin
.setValues(0,
426 vSampleOrigin
.y() - _vSampleDescs
[iLow
]._y0
);
428 _vImages
[iHeigh
]->readBlockA16(vNewSampleOrigin
,
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
];
463 bool TiledImageBlockAccessor::read2VBlocksA16(Int32 iLow
,
468 Int32 iTargetSizeBytes
)
470 Vec2i
vNewSampleOrigin(vSampleOrigin
.x() - _vSampleDescs
[iLow
]._x0
,
471 vSampleOrigin
.y() - _vSampleDescs
[iLow
]._y0
);
473 _vImages
[iLow
]->readBlockA16(vNewSampleOrigin
,
479 _vI16Buffer
.resize(iTextureSize
* iTextureSize
);
481 vNewSampleOrigin
.setValues(vSampleOrigin
.x() - _vSampleDescs
[iLow
]._x0
,
484 _vImages
[iHeigh
]->readBlockA16(vNewSampleOrigin
,
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
];
516 bool TiledImageBlockAccessor::read4BlocksA16(Int32 iLow
,
521 Int32 iTargetSizeBytes
)
523 Vec2i
vNewSampleOrigin(vSampleOrigin
.x() - _vSampleDescs
[iLow
]._x0
,
524 vSampleOrigin
.y() - _vSampleDescs
[iLow
]._y0
);
526 _vImages
[iLow
]->readBlockA16(vNewSampleOrigin
,
532 _vI16Buffer
.resize(iTextureSize
* iTextureSize
);
534 vNewSampleOrigin
.setValues(0,
535 vSampleOrigin
.y() - _vSampleDescs
[iLow
]._y0
);
537 _vImages
[iLow
+ 1]->readBlockA16(vNewSampleOrigin
,
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
];
568 vNewSampleOrigin
.setValues(vSampleOrigin
.x() - _vSampleDescs
[iLow
]._x0
,
571 _vImages
[iLow
+ _uiColumns
]->readBlockA16(vNewSampleOrigin
,
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
];
594 vNewSampleOrigin
.setValues(0, 0);
596 _vImages
[iHeigh
]->readBlockA16(vNewSampleOrigin
,
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
];
627 DefaultBlockAccessor::~DefaultBlockAccessor(void)
632 bool DefaultBlockAccessor::isOpen(void)
637 bool DefaultBlockAccessor::readBlockA16(Vec2i vSampleOrigin
,
640 Int32 iTargetSizeBytes
)
645 bool DefaultBlockAccessor::readBlockA16(Vec2i vSampleOrigin
,
648 Int32 iTargetSizeBytes
)
650 for(Int32 i
= 0; i
< iTextureSize
* iTextureSize
; ++i
)
652 pTarget
[i
] = _iDefaultValue
;
658 DefaultBlockAccessor::DefaultBlockAccessor(void) :
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());
681 _iDefaultValue
= TypeTraits
<Int32
>::getFromCString((*cIt
).c_str());
683 fprintf(stderr
, "Def %d\n", _iDefaultValue
);
690 _vSize
[0] = TypeTraits
<Int32
>::getFromCString((*cIt
).c_str());
697 _vSize
[1] = TypeTraits
<Int32
>::getFromCString((*cIt
).c_str());
699 fprintf(stderr
, "Size %d %d\n", _vSize
[0], _vSize
[1]);
706 _eImgType
= Image::Type(TypeTraits
<UInt32
>::getFromCString((*cIt
).c_str()));
714 Image::PixelFormat(TypeTraits
<UInt32
>::getFromCString((*cIt
).c_str()));
716 fprintf(stderr
, "T: %x F: %x\n",
725 _pGeoRef
= GeoReferenceAttachment::create();
727 _pGeoRef
->editOrigin()[0] =
728 TypeTraits
<Real32
>::getFromCString((*cIt
).c_str());
735 _pGeoRef
->editOrigin()[1] =
736 TypeTraits
<Real32
>::getFromCString((*cIt
).c_str());
743 _pGeoRef
->editPixelSize()[0] =
744 TypeTraits
<Real32
>::getFromCString((*cIt
).c_str());
751 _pGeoRef
->editPixelSize()[1] =
752 TypeTraits
<Real32
>::getFromCString((*cIt
).c_str());
759 _pGeoRef
->editDatum() =
760 TypeTraits
<UInt32
>::getFromCString((*cIt
).c_str());
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",
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
)
824 ImageBlockAccessorPtr
ITFImageFileType::open(const Char8
*fileName
)
826 TiledImageBlockAccessorPtr
returnValue(new TiledImageBlockAccessor
);
828 returnValue
->open(fileName
);
833 //-------------------------------------------------------------------------
834 /*! Constructor used for the singleton object
837 ITFImageFileType::ITFImageFileType(const Char8
*mimeType
,
838 const Char8
*suffixArray
[],
839 UInt16 suffixByteCount
,
848 //-------------------------------------------------------------------------
852 ITFImageFileType::~ITFImageFileType(void)