fixed: c++11 build problems ("" operator)
[opensg.git] / Source / System / Image / FileIO / OSGDBImageFileType.cpp
blob6367dbdd887bb0d1119b046e48b42296050df416
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 "OSGDBImageFileType.h"
48 #include "OSGGeoReferenceAttachment.h"
49 #include "OSGImageFunctions.h"
51 #ifndef OSG_DO_DOC
52 # ifdef OSG_WITH_DB
53 # define OSG_DB_ARG(ARG) ARG
54 # else
55 # define OSG_DB_ARG(ARG)
56 # endif
57 #else
58 # define OSG_DB_ARG(ARG) ARG
59 #endif
61 // Static Class Varible implementations:
62 static const OSG::Char8 *suffixArray[] =
64 "db"
67 OSG_BEGIN_NAMESPACE
69 DBHeaderParameter::DBHeaderParameter(void) :
70 _bLastParam(false)
74 DBHeaderParameter::~DBHeaderParameter(void)
78 void DBHeaderParameter::setLastParam(void)
80 _bLastParam = true;
83 bool DBHeaderParameter::isLastParam(void)
85 return _bLastParam;
89 DBHeaderIntParameter::DBHeaderIntParameter(UInt32 &uiParam) :
90 Inherited( ),
91 _uiParam (uiParam)
95 DBHeaderIntParameter::~DBHeaderIntParameter(void)
99 void DBHeaderIntParameter::convert(const Char8 *str)
101 if(str != NULL)
103 sscanf(str, "%u", &_uiParam);
107 DBHeaderFloatParameter::DBHeaderFloatParameter(Real32 &rParam) :
108 Inherited( ),
109 _rParam (rParam)
113 DBHeaderFloatParameter::~DBHeaderFloatParameter(void)
117 void DBHeaderFloatParameter::convert(const Char8 *str)
119 if(str != NULL)
121 sscanf(str, "%f", &_rParam);
125 DBHeader::DBHeader(void) :
126 uiMagic (0 ),
127 uiXSize (0 ),
128 uiYSize (0 ),
129 uiZSize (0 ),
130 uiTSteps (0 ),
131 uiType (0 ),
133 rSWx (0.f),
134 rSWy (0.f),
135 rNWx (0.f),
136 rNWy (0.f),
138 rNEx (0.f),
139 rNEy (0.f),
140 rSEx (0.f),
141 rSEy (0.f),
143 rH0 (0.f),
144 rDh (0.f),
145 rT0 (0.f),
146 rDt (0.f),
148 rScaling (1.f),
149 rBias (0.f),
151 uiExtFormat (0 ),
152 uiImplFormat(0 ),
154 LLWGS84_rSWx(0.f),
155 LLWGS84_rSWy(0.f),
156 LLWGS84_rNWx(0.f),
157 LLWGS84_rNWy(0.f),
159 LLWGS84_rNEx(0.f),
160 LLWGS84_rNEy(0.f),
161 LLWGS84_rSEx(0.f),
162 LLWGS84_rSEy(0.f),
164 uiBytes (0 )
166 std::string szParamName("MAGIC");
168 _mParams[szParamName] = new DBHeaderIntParameter(uiMagic);
170 szParamName = "xsize";
172 _mParams[szParamName] = new DBHeaderIntParameter(uiXSize);
174 szParamName = "ysize";
176 _mParams[szParamName] = new DBHeaderIntParameter(uiYSize);
178 szParamName = "zsize";
180 _mParams[szParamName] = new DBHeaderIntParameter(uiZSize);
182 szParamName = "tsteps";
184 _mParams[szParamName] = new DBHeaderIntParameter(uiTSteps);
186 szParamName = "type";
188 _mParams[szParamName] = new DBHeaderIntParameter(uiType);
192 szParamName = "swx";
194 _mParams[szParamName] = new DBHeaderFloatParameter(rSWx);
196 szParamName = "swy";
198 _mParams[szParamName] = new DBHeaderFloatParameter(rSWy);
200 szParamName = "nwx";
202 _mParams[szParamName] = new DBHeaderFloatParameter(rNWx);
204 szParamName = "nwy";
206 _mParams[szParamName] = new DBHeaderFloatParameter(rNWy);
210 szParamName = "nex";
212 _mParams[szParamName] = new DBHeaderFloatParameter(rNEx);
214 szParamName = "ney";
216 _mParams[szParamName] = new DBHeaderFloatParameter(rNEy);
218 szParamName = "sex";
220 _mParams[szParamName] = new DBHeaderFloatParameter(rSEx);
222 szParamName = "sey";
224 _mParams[szParamName] = new DBHeaderFloatParameter(rSEy);
228 szParamName = "h0";
230 _mParams[szParamName] = new DBHeaderFloatParameter(rH0);
232 szParamName = "dh";
234 _mParams[szParamName] = new DBHeaderFloatParameter(rDh);
236 szParamName = "t0";
238 _mParams[szParamName] = new DBHeaderFloatParameter(rT0);
240 szParamName = "dt";
242 _mParams[szParamName] = new DBHeaderFloatParameter(rDt);
246 szParamName = "scaling";
248 _mParams[szParamName] = new DBHeaderFloatParameter(rScaling);
250 szParamName = "bias";
252 _mParams[szParamName] = new DBHeaderFloatParameter(rBias);
256 szParamName = "extformat";
258 _mParams[szParamName] = new DBHeaderIntParameter(uiExtFormat);
260 szParamName = "implformat";
262 _mParams[szParamName] = new DBHeaderIntParameter(uiImplFormat);
266 szParamName = "LLWGS84_swx";
268 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rSWx);
270 szParamName = "LLWGS84_swy";
272 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rSWy);
274 szParamName = "LLWGS84_nwx";
276 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rNWx);
278 szParamName = "LLWGS84_nwy";
280 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rNWy);
284 szParamName = "LLWGS84_nex";
286 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rNEx);
288 szParamName = "LLWGS84_ney";
290 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rNEy);
292 szParamName = "LLWGS84_sex";
294 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rSEx);
296 szParamName = "LLWGS84_sey";
298 _mParams[szParamName] = new DBHeaderFloatParameter(LLWGS84_rSEy);
302 szParamName = "bytes";
304 _mParams[szParamName] = new DBHeaderIntParameter(uiBytes);
306 _mParams[szParamName]->setLastParam();
309 DBHeader::~DBHeader(void)
311 ParameterMap::iterator mapIt = _mParams.begin();
312 ParameterMap::iterator mapEnd = _mParams.end ();
314 while(mapIt != mapEnd)
316 delete mapIt->second;
318 ++mapIt;
322 bool DBHeader::read(FILE *pIn)
324 bool returnValue = true;
326 if(pIn == NULL)
327 return returnValue;
329 Char8 buffer[1024];
330 bool bEnd = false;
331 Char8 *strIt;
333 while(!bEnd)
335 fgets(buffer, 1024, pIn);
337 strIt = strtok(buffer, "=");
339 if(strIt != NULL)
341 ParameterMap::iterator mapIt = _mParams.find(std::string(strIt));
343 if(mapIt != _mParams.end())
345 bEnd = mapIt->second->isLastParam();
347 strIt = strtok(NULL, "\n");
349 mapIt->second->convert(strIt);
351 else
353 fprintf(stderr, "Unknown parameter %s\n", strIt);
356 else
358 bEnd = true;
362 buffer[0] = fgetc(pIn);
364 if(buffer[0] != '\0')
366 fprintf(stderr, "missing 0\n");
367 returnValue = false;
370 return returnValue;
373 void DBHeader::dump(void) const
375 fprintf(stderr, "MAGIC = %u\n", this->uiMagic );
376 fprintf(stderr, "xsize = %u\n", this->uiXSize );
377 fprintf(stderr, "ysize = %u\n", this->uiYSize );
378 fprintf(stderr, "zsize = %u\n", this->uiZSize );
379 fprintf(stderr, "tsteps = %u\n", this->uiTSteps );
380 fprintf(stderr, "type = %u\n", this->uiType );
381 fprintf(stderr, "swx = %f\n", this->rSWx );
382 fprintf(stderr, "swy = %f\n", this->rSWy );
383 fprintf(stderr, "nwx = %f\n", this->rNWx );
384 fprintf(stderr, "nwy = %f\n", this->rNWy );
385 fprintf(stderr, "nex = %f\n", this->rNEx );
386 fprintf(stderr, "ney = %f\n", this->rNEy );
387 fprintf(stderr, "sex = %f\n", this->rSEx );
388 fprintf(stderr, "sey = %f\n", this->rSEy );
389 fprintf(stderr, "h0 = %f\n", this->rDh );
390 fprintf(stderr, "dh = %f\n", this->rDh );
391 fprintf(stderr, "t0 = %f\n", this->rT0 );
392 fprintf(stderr, "dt = %f\n", this->rDt );
393 fprintf(stderr, "scaling = %f\n", this->rScaling );
394 fprintf(stderr, "bias = %f\n", this->rBias );
395 fprintf(stderr, "extformat = %u\n", this->uiExtFormat );
396 fprintf(stderr, "implformat = %u\n", this->uiImplFormat);
397 fprintf(stderr, "LLWGS84_swx = %f\n", this->LLWGS84_rSWx);
398 fprintf(stderr, "LLWGS84_swy = %f\n", this->LLWGS84_rSWy);
399 fprintf(stderr, "LLWGS84_nwx = %f\n", this->LLWGS84_rNWx);
400 fprintf(stderr, "LLWGS84_nwy = %f\n", this->LLWGS84_rNWy);
401 fprintf(stderr, "LLWGS84_nex = %f\n", this->LLWGS84_rNEx);
402 fprintf(stderr, "LLWGS84_ney = %f\n", this->LLWGS84_rNEy);
403 fprintf(stderr, "LLWGS84_sex = %f\n", this->LLWGS84_rSEx);
404 fprintf(stderr, "LLWGS84_sey = %f\n", this->LLWGS84_rSEy);
405 fprintf(stderr, "bytes = %u\n", this->uiBytes );
408 DBBlockAccessor::~DBBlockAccessor(void)
412 bool DBBlockAccessor::isOpen(void)
414 return false;
418 DBBlockAccessor::DBBlockAccessor(void) :
419 Inherited()
423 void DBBlockAccessor::open(const Char8 *szFilename)
425 #ifdef OSG_WITH_GDALX
426 _pDataset = (GDALDataset *) GDALOpen(szFilename, GA_ReadOnly);
428 if(_pDataset != NULL)
430 _pGeoRef = GeoReferenceAttachment::create();
432 double adfGeoTransform[6];
434 if(_pDataset->GetGeoTransform(adfGeoTransform) == CE_None)
436 _pGeoRef->editOrigin().setValues(adfGeoTransform[0],
437 adfGeoTransform[3]);
439 _pGeoRef->editPixelSize().setValues(adfGeoTransform[1],
440 adfGeoTransform[5]);
442 if(GDALGetProjectionRef(_pDataset) != NULL)
444 OGRSpatialReferenceH hSRS;
446 Char8 *szProjection = (char *) GDALGetProjectionRef(_pDataset);
448 hSRS = OSRNewSpatialReference(NULL);
450 if(OSRImportFromWkt(hSRS, &szProjection) == CE_None)
452 _pGeoRef->editEllipsoidAxis().setValues(
453 OSRGetSemiMajor(hSRS, NULL),
454 OSRGetSemiMinor(hSRS, NULL));
456 const Char8 *szDatum = OSRGetAttrValue(hSRS, "DATUM", 0);
458 if(szDatum != NULL && 0 == strcmp(szDatum, "WGS_1984"))
460 _pGeoRef->editDatum() =
461 GeoReferenceAttachment::WGS84;
463 else
465 fprintf(stderr, "Unknow datum %s\n",
466 szDatum);
468 _pGeoRef->editDatum() =
469 GeoReferenceAttachment::UnknownDatum;
473 OSRDestroySpatialReference(hSRS);
477 int nBlockXSize, nBlockYSize;
478 int bGotMin, bGotMax;
479 double adfMinMax[2];
481 _pBand = _pDataset->GetRasterBand(1);
483 _pBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
485 adfMinMax[0] = _pBand->GetMinimum(&bGotMin);
486 adfMinMax[1] = _pBand->GetMaximum(&bGotMax);
488 if(!(bGotMin && bGotMax))
490 GDALComputeRasterMinMax((GDALRasterBandH) _pBand, TRUE, adfMinMax);
493 if(_pBand != NULL)
495 _eImgFormat = Image::OSG_INVALID_PF;
497 switch(_pDataset->GetRasterCount())
499 case 1:
500 _eImgFormat = Image::OSG_L_PF;
501 break;
502 case 2:
503 _eImgFormat = Image::OSG_LA_PF;
504 break;
505 case 3:
506 _eImgFormat = Image::OSG_RGB_PF;
507 break;
508 case 4:
509 _eImgFormat = Image::OSG_RGBA_PF;
510 break;
513 _eImgType = Image::OSG_INVALID_IMAGEDATATYPE;
515 switch(_pBand->GetRasterDataType())
517 case GDT_Byte:
518 _eImgType = Image::OSG_UINT8_IMAGEDATA;
519 break;
521 case GDT_UInt16:
522 _eImgType = Image::OSG_UINT16_IMAGEDATA;
523 break;
525 case GDT_Int16:
526 _eImgType = Image::OSG_INT16_IMAGEDATA;
527 break;
529 case GDT_UInt32:
530 _eImgType = Image::OSG_UINT32_IMAGEDATA;
531 break;
533 case GDT_Int32:
534 _eImgType = Image::OSG_INT32_IMAGEDATA;
535 break;
537 case GDT_Float32:
538 _eImgType = Image::OSG_FLOAT32_IMAGEDATA;
539 break;
541 case GDT_Float64:
542 case GDT_CInt16:
543 case GDT_CInt32:
544 case GDT_CFloat32:
545 case GDT_CFloat64:
546 default:
547 GDALClose(_pDataset);
548 _pDataset = NULL;
549 break;
553 _vSize[0] = _pDataset->GetRasterXSize();
554 _vSize[1] = _pDataset->GetRasterYSize();
556 _fNoDataValue = _pBand->GetNoDataValue();
558 _pGeoRef->setNoDataValue(_fNoDataValue);
561 #endif
564 bool DBBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
565 Int32 iTextureSize,
566 UInt16 *pTarget,
567 Int32 iTargetSizeBytes)
569 #ifdef OSG_WITH_GDALX
570 OSG_ASSERT(false);
572 UInt32 destIdx = 0;
574 UInt8 *pDst = (UInt8 *) pTarget;
576 Int32 xMin = vSampleOrigin.x();
577 Int32 xMax = vSampleOrigin.x() + iTextureSize;
579 Int32 yMin = vSampleOrigin.y();
580 Int32 yMax = vSampleOrigin.y() + iTextureSize;
582 for(UInt32 y = yMin; y < yMax; y++)
584 for(UInt32 x = xMin; x < xMax; x++)
586 for(UInt32 i = 0; i < 2; i++)
588 pDst[destIdx] = 0;
590 destIdx++;
594 destIdx += (iTextureSize - (xMax - xMin)) * 2;
596 #endif
598 return false;
601 bool DBBlockAccessor::readBlockA16(Vec2i vSampleOrigin,
602 Int32 iTextureSize,
603 Int16 *pTarget,
604 Int32 iTargetSizeBytes)
606 #ifdef OSG_WITH_GDALX
607 Int32 xMin = vSampleOrigin.x();
608 Int32 xMax = vSampleOrigin.x() + iTextureSize;
610 Int32 yMin = vSampleOrigin.y();
611 Int32 yMax = vSampleOrigin.y() + iTextureSize;
613 if(xMax > _vSize[0] || yMax > _vSize[1])
615 UInt32 destIdx = 0;
616 UInt32 srcIdx = 0;
618 _vI16Buffer.resize(iTextureSize * iTextureSize);
620 Int32 iSampleX = osgMin(_vSize[0] - vSampleOrigin.x(), iTextureSize);
621 Int32 iSampleY = osgMin(_vSize[1] - vSampleOrigin.y(), iTextureSize);
623 _pBand->RasterIO(GF_Read,
624 vSampleOrigin.x(),
625 vSampleOrigin.y(),
626 iSampleX,
627 iSampleY,
628 &(_vI16Buffer[0]),
629 iSampleX,
630 iSampleY,
631 GDT_Int16,
636 for(UInt32 y = yMin; y < yMax; y++)
638 for(UInt32 x = xMin; x < xMax; x++)
640 if(x >= _vSize[0] || y >= _vSize[1])
642 pTarget[destIdx] = Int16(_fNoDataValue);
644 else
646 pTarget[destIdx] = _vI16Buffer[srcIdx];
648 srcIdx++;
651 destIdx++;
655 else
657 _pBand->RasterIO(GF_Read,
658 vSampleOrigin.x(),
659 vSampleOrigin.y(),
660 iTextureSize,
661 iTextureSize,
662 pTarget,
663 iTextureSize,
664 iTextureSize,
665 GDT_Int16,
669 #endif
671 return true;
675 /*! \class GDALImageFileType
677 Image File Type to read/write and store/restore Image objects as
678 GDAL (tif/tiff suffix) data.
680 To be able to load TIFF images you need the IJG TIFF library,
681 (check the Prerequisites page on www.opensg.org).
682 The lib comes with all Linux distributions.
684 You have to --enable-tif in the configure line to enable
685 the singleton object.
689 DBImageFileType DBImageFileType:: _the("image/db",
690 suffixArray,
691 sizeof(suffixArray),
692 (OSG_READ_SUPPORTED));
695 //-------------------------------------------------------------------------
696 /*! Tries to fill the image object with the data read from
697 the given fileName. Returns true on success.
701 bool DBImageFileType::read( Image *pImage,
702 const Char8 *fileName)
704 bool returnValue = false;
706 FILE *pIn = fopen(fileName, "rb");
708 if(pIn != NULL)
710 fprintf(stderr, "db::open %p\n", pIn);
712 DBHeader oHeader;
714 returnValue = oHeader.read(pIn);
716 if(returnValue == false)
718 fclose(pIn);
720 return returnValue;
723 // oHeader.dump();
725 if(oHeader.uiExtFormat != 0)
727 fprintf(stderr, "ext == 0 only support");
729 returnValue = false;
731 fclose(pIn);
733 return returnValue;
736 Image::PixelFormat imagePf = Image::OSG_RGBA_PF;
737 Image::Type imageType = Image::OSG_UINT8_IMAGEDATA;
739 switch(oHeader.uiType)
741 case 0:
742 imagePf = Image::OSG_L_PF;
743 imageType = Image::OSG_UINT8_IMAGEDATA;
744 break;
745 case 1:
746 imagePf = Image::OSG_L_PF;
747 imageType = Image::OSG_INT16_IMAGEDATA;
748 break;
749 case 2:
750 imagePf = Image::OSG_L_PF;
751 imageType = Image::OSG_FLOAT32_IMAGEDATA;
752 break;
753 case 3:
754 imagePf = Image::OSG_RGB_PF;
755 imageType = Image::OSG_UINT8_IMAGEDATA;
756 break;
757 case 4:
758 imagePf = Image::OSG_RGBA_PF;
759 imageType = Image::OSG_UINT8_IMAGEDATA;
760 break;
761 case 5:
762 imagePf = Image::OSG_RGB_DXT1;
763 imageType = Image::OSG_UINT8_IMAGEDATA;
764 break;
765 case 6:
766 imagePf = Image::OSG_RGBA_DXT1;
767 imageType = Image::OSG_UINT8_IMAGEDATA;
768 break;
769 default:
770 break;
773 pImage->set(imagePf,
774 oHeader.uiXSize,
775 oHeader.uiYSize,
776 oHeader.uiZSize,
779 0.0,
780 NULL,
781 imageType,
782 true,
785 if(pImage->getSize() == oHeader.uiBytes)
787 fread(pImage->editData(),
788 sizeof(UInt8),
789 oHeader.uiBytes,
790 pIn);
792 #if BYTE_ORDER == LITTLE_ENDIAN
793 switch(oHeader.uiType)
795 case 1:
796 swapAndConvertImageByteOrder<
797 Int16,
798 &clampMin<Int16, -100, 0> >(pImage);
799 break;
800 case 2:
801 swapImageByteOrder<Real32>(pImage);
802 break;
803 default:
804 break;
806 #endif
808 if(oHeader.uiXSize != 0 && oHeader.uiYSize != 0)
810 if(oHeader.LLWGS84_rSWx != 0.f &&
811 oHeader.LLWGS84_rSWy != 0.f &&
812 oHeader.LLWGS84_rNWx != 0.f &&
813 oHeader.LLWGS84_rNWy != 0.f &&
814 oHeader.LLWGS84_rNEx != 0.f &&
815 oHeader.LLWGS84_rNEy != 0.f &&
816 oHeader.LLWGS84_rSEx != 0.f &&
817 oHeader.LLWGS84_rSEy != 0.f )
819 GeoReferenceAttachmentUnrecPtr pGeoRef =
820 GeoReferenceAttachment::create();
822 pGeoRef->setupWGS84Datum();
824 pGeoRef->editOrigin().setValues(oHeader.LLWGS84_rNWx,
825 oHeader.LLWGS84_rNWy);
827 pGeoRef->editPixelSize().setValues(
828 (oHeader.LLWGS84_rSEx -
829 oHeader.LLWGS84_rNWx) / oHeader.uiXSize,
830 (oHeader.LLWGS84_rSEy -
831 oHeader.LLWGS84_rNWy) / oHeader.uiYSize);
833 pImage->addAttachment(pGeoRef);
835 else if(oHeader.rSWx != 0.f &&
836 oHeader.rSWy != 0.f &&
837 oHeader.rNWx != 0.f &&
838 oHeader.rNWy != 0.f &&
839 oHeader.rNEx != 0.f &&
840 oHeader.rNEy != 0.f &&
841 oHeader.rSEx != 0.f &&
842 oHeader.rSEy != 0.f )
844 GeoReferenceAttachmentUnrecPtr pGeoRef =
845 GeoReferenceAttachment::create();
847 pGeoRef->setupWGS84Datum();
849 pGeoRef->editOrigin().setValues(oHeader.rNWx,
850 oHeader.rNWy);
852 pGeoRef->editPixelSize().setValues(
853 (oHeader.rSEx - oHeader.rNWx) / oHeader.uiXSize,
854 (oHeader.rSEy - oHeader.rNWy) / oHeader.uiYSize);
856 pImage->addAttachment(pGeoRef);
860 else
862 fprintf(stderr, "size mismatch %" PRISize " %d\n",
863 pImage->getSize(),
864 oHeader.uiBytes);
866 returnValue = false;
869 fclose(pIn);
872 return returnValue;
874 #ifdef OSG_WITH_GDALX
876 GDALDataset *pDataset;
878 pDataset = (GDALDataset *) GDALOpen(fileName, GA_ReadOnly);
880 if(pDataset != NULL)
882 GeoReferenceAttachmentPtr pGeoRef = GeoReferenceAttachment::create();
884 pImage->addAttachment(pGeoRef);
886 double adfGeoTransform[6];
888 if(pDataset->GetGeoTransform(adfGeoTransform) == CE_None)
890 pGeoRef->editOrigin().setValues(adfGeoTransform[0],
891 adfGeoTransform[3]);
893 pGeoRef->editPixelSize().setValues(adfGeoTransform[1],
894 adfGeoTransform[5]);
896 if(GDALGetProjectionRef(pDataset) != NULL)
898 OGRSpatialReferenceH hSRS;
900 Char8 *szProjection = (char *) GDALGetProjectionRef(pDataset);
902 hSRS = OSRNewSpatialReference(NULL);
904 if(OSRImportFromWkt(hSRS, &szProjection) == CE_None)
906 pGeoRef->editEllipsoidAxis().setValues(
907 OSRGetSemiMajor(hSRS, NULL),
908 OSRGetSemiMinor(hSRS, NULL));
910 const Char8 *szDatum = OSRGetAttrValue(hSRS, "DATUM", 0);
912 if(szDatum != NULL && 0 == strcmp(szDatum, "WGS_1984"))
914 pGeoRef->editDatum() =
915 GeoReferenceAttachment::WGS84;
917 else
919 fprintf(stderr, "Unknow datum %s\n",
920 szDatum);
922 pGeoRef->editDatum() =
923 GeoReferenceAttachment::UnknownDatum;
927 OSRDestroySpatialReference(hSRS);
931 GDALRasterBand *pBand;
932 int nBlockXSize, nBlockYSize;
933 int bGotMin, bGotMax;
934 double adfMinMax[2];
936 pBand = pDataset->GetRasterBand( 1 );
937 pBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
939 adfMinMax[0] = pBand->GetMinimum( &bGotMin );
940 adfMinMax[1] = pBand->GetMaximum( &bGotMax );
942 if(!(bGotMin && bGotMax))
943 GDALComputeRasterMinMax((GDALRasterBandH) pBand, TRUE, adfMinMax);
945 pBand = pDataset->GetRasterBand(1);
947 if(pBand != NULL)
949 Image::PixelFormat ePF = Image::OSG_INVALID_PF;
951 switch(pDataset->GetRasterCount())
953 case 1:
954 ePF = Image::OSG_L_PF;
955 break;
956 case 2:
957 ePF = Image::OSG_LA_PF;
958 break;
959 case 3:
960 ePF = Image::OSG_RGB_PF;
961 break;
962 case 4:
963 ePF = Image::OSG_RGBA_PF;
964 break;
967 Image::Type eDT = Image::OSG_INVALID_IMAGEDATATYPE;
969 switch(pBand->GetRasterDataType())
971 case GDT_Byte:
972 eDT = Image::OSG_UINT8_IMAGEDATA;
973 break;
975 case GDT_UInt16:
976 eDT = Image::OSG_UINT16_IMAGEDATA;
977 break;
979 case GDT_Int16:
980 eDT = Image::OSG_INT16_IMAGEDATA;
981 break;
983 case GDT_UInt32:
984 eDT = Image::OSG_UINT32_IMAGEDATA;
985 break;
987 case GDT_Int32:
988 eDT = Image::OSG_INT32_IMAGEDATA;
989 break;
991 case GDT_Float32:
992 eDT = Image::OSG_FLOAT32_IMAGEDATA;
993 break;
995 case GDT_Float64:
996 case GDT_CInt16:
997 case GDT_CInt32:
998 case GDT_CFloat32:
999 case GDT_CFloat64:
1000 default:
1001 GDALClose(pDataset);
1002 return returnValue;
1003 break;
1007 pImage->set(ePF,
1008 pDataset->GetRasterXSize(),
1009 pDataset->GetRasterYSize(),
1013 0.0,
1014 NULL,
1015 eDT);
1017 UChar8 *dst = pImage->editData();
1019 pBand->RasterIO(GF_Read,
1022 pDataset->GetRasterXSize(),
1023 pDataset->GetRasterYSize(),
1024 dst,
1025 pDataset->GetRasterXSize(),
1026 pDataset->GetRasterYSize(),
1027 pBand->GetRasterDataType(),
1031 pGeoRef->setNoDataValue(pBand->GetNoDataValue());
1033 returnValue = true;
1036 GDALClose(pDataset);
1039 return returnValue;
1040 #endif // OSG_WITH_GDAL
1043 //-------------------------------------------------------------------------
1044 /*! Tries to write the image object to the given output stream.
1045 Returns true on success.
1048 bool DBImageFileType::write(const Image *OSG_DB_ARG(pImage),
1049 const Char8 *OSG_DB_ARG(fileName))
1051 bool retCode = false;
1053 #ifdef OSG_WITH_GDALX
1054 if(pImage->getDimension() < 1 || pImage->getDimension() > 2)
1056 FWARNING(("TIFImageFileType::write: invalid dimension %d!\n",
1057 pImage->getDimension()));
1059 return false;
1062 TIFF *out = TIFFClientOpen("dummy", "wm", (thandle_t)&os,
1063 osReadProc,
1064 osWriteProc,
1065 osSeekProc,
1066 closeProc,
1067 osSizeProc,
1068 mapFileProc,
1069 unmapFileProc);
1071 int lineSize = pImage->getWidth() * pImage->getBpp();
1072 int photometric = 0;
1073 int samplesPerPixel = 0;
1074 const UChar8 *data;
1075 int row;
1077 // TODO: implement all cases correct
1078 switch(pImage->getBpp())
1080 case 1:
1081 samplesPerPixel = 1;
1082 photometric = PHOTOMETRIC_MINISBLACK;
1083 break;
1084 case 2:
1085 samplesPerPixel = 2;
1086 photometric = PHOTOMETRIC_MINISBLACK;
1087 break;
1088 case 3:
1089 samplesPerPixel = 3;
1090 photometric = PHOTOMETRIC_RGB;
1091 break;
1092 case 4:
1093 samplesPerPixel = 4;
1094 photometric = PHOTOMETRIC_RGB;
1095 break;
1098 if(out)
1100 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, pImage->getWidth());
1101 TIFFSetField(out, TIFFTAG_IMAGELENGTH, pImage->getHeight());
1102 TIFFSetField(out, TIFFTAG_XRESOLUTION, pImage->getResX());
1103 TIFFSetField(out, TIFFTAG_YRESOLUTION, pImage->getResY());
1104 TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, pImage->getResUnit());
1105 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1106 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel);
1107 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
1108 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1109 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
1111 if(_options.find("compressionType=LZW") != std::string::npos)
1113 TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
1115 else
1117 TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1120 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, 0));
1122 for(row = 0; row < pImage->getHeight(); row++)
1124 data =
1125 pImage->getData() +
1126 ((pImage->getHeight() - row - 1) * lineSize);
1128 if(TIFFWriteScanline(out,
1129 (tdata_t) const_cast<UChar8 *>(data),
1130 row,
1131 0) < 0)
1133 break;
1137 TIFFClose(out);
1139 retCode = true;
1142 #else
1143 SWARNING << getMimeType()
1144 << " write is not compiled into the current binary "
1145 << std::endl;
1146 #endif
1148 return retCode;
1151 bool DBImageFileType::validateHeader(const Char8 *fileName, bool &implemented)
1153 implemented = true;
1155 return true;
1158 #if 1
1159 ImageBlockAccessorPtr DBImageFileType::open(const Char8 *fileName)
1161 DBBlockAccessorPtr returnValue(new DBBlockAccessor);
1163 returnValue->open(fileName);
1165 return returnValue;
1167 #endif
1169 //-------------------------------------------------------------------------
1170 /*! Constructor used for the singleton object
1173 DBImageFileType::DBImageFileType(const Char8 *mimeType,
1174 const Char8 *suffixArray[],
1175 UInt16 suffixByteCount,
1176 UInt32 flags) :
1177 Inherited(mimeType,
1178 suffixArray,
1179 suffixByteCount,
1180 flags )
1184 //-------------------------------------------------------------------------
1185 /*! Destructor
1188 DBImageFileType::~DBImageFileType(void)
1192 OSG_END_NAMESPACE