88#include " MMVII_Image2D.h"
99
1010#include < string>
11- #include < gdal_priv.h>
11+ #include " gdal_priv.h"
12+ #include " FileLock.h"
1213
1314/*
1415 * GDAL API interface to MMVII to read/write image file
@@ -118,90 +119,99 @@ class GdalIO {
118119 , mGdalNbChan (0 )
119120 , mNbImg (0 )
120121 {}
121- void operator ()(IoMode aMode, const cDataFileIm2D &aDF, const tvIm& aVecImV2, const cPt2di & aP0File,double aDyn,const cRect2& aR2)
122+ void operator ()(IoMode aMode, const cDataFileIm2D &aDF, const tvIm& aVecImV2, const cPt2di & aP0File, double aDyn, const cRect2& aR2)
122123 {
123- mDataFile = &aDF;
124- auto aName = mDataFile ->Name ();
125- mGdalNbChan = mDataFile ->NbChannel ();
126- mNbImg = static_cast <int >(aVecImV2.size ());
124+ mDataFile = &aDF;
125+ mGdalNbChan = mDataFile ->NbChannel ();
126+ mNbImg = static_cast <int >(aVecImV2.size ());
127127
128128 auto aIm2D = aVecImV2[0 ];
129129 cRect2 aRectFullIm (aIm2D->P0 (), aIm2D->P1 ());
130- cRect2 aRectIm = (aR2 == cRect2::TheEmptyBox) ? aRectFullIm : aR2;
131- cRect2 aRectFile (aRectIm.Translate (aP0File)) ;
130+ cRect2 aRectIm = (aR2 == cRect2::TheEmptyBox) ? aRectFullIm : aR2;
131+ cRect2 aRectFile (aRectIm.Translate (aP0File));
132+
133+ auto aName = mDataFile ->Name ();
134+ MMVII_INTERNAL_ASSERT_strong (aRectFile.IncludedIn (aDF), " Read/write out of image file (" + aName + " )" );
135+ MMVII_INTERNAL_ASSERT_strong (aRectIm.IncludedIn (aRectFullIm), " Read/write out of Image buffer (" + aName + " )" );
136+
137+ if (aMode == IoMode::Read)
138+ {
139+ Read (aVecImV2, aRectIm, aRectFile, aDyn);
140+ }
141+ else
142+ {
143+ FileLock gdalLock (aName);
144+ Write (aVecImV2, aRectIm, aRectFile, aDyn);
145+ }
146+ }
132147
133- MMVII_INTERNAL_ASSERT_strong (aRectFile.IncludedIn (aDF), " Read/write out of image file (" + aName + " )" );
134- MMVII_INTERNAL_ASSERT_strong (aRectIm.IncludedIn (aRectFullIm), " Read/write out of Image buffer (" + aName + " )" );
148+ private:
149+ void Read (const tvIm& aVecImV2, const cRect2& aRectIm, const cRect2& aRectFile, double aDyn)
150+ {
151+ auto aName = mDataFile ->Name ();
152+
153+ if (mDataFile ->IsCreateAtFirstWrite ())
154+ MMVII_INTERNAL_ERROR (" GDAL read: image file created with CreateOnWrite() must be written before trying to read it (" + aName + " )" );
155+
156+ mGdalDataset = cGdalApi::OpenDataset (aName, GA_ReadOnly, cGdalApi::eOnError::RaiseError);
157+
158+ if (mGdalNbChan == mNbImg && mNbImg != 0 )
159+ GdalReadNtoN (aVecImV2, aRectIm, aRectFile, aDyn); // file N -> N img channels
160+ else if (mGdalNbChan == 1 && mNbImg != 0 )
161+ GdalRead1toN (aVecImV2, aRectIm, aRectFile, aDyn); // file 1 -> N img channels
162+ else if (mGdalNbChan != 0 && mNbImg == 1 )
163+ GdalReadNto1 (aVecImV2, aRectIm, aRectFile, aDyn); // file N -> 1 img channel
164+ else
165+ MMVII_INTERNAL_ERROR (" Gdal read: Images vector size: " + std::to_string (mNbImg ) + " , file channels: " + std::to_string (mGdalNbChan ) + " (" + aName + " )" );
166+
167+ cGdalApi::CloseDataset (mGdalDataset );
168+ }
169+
170+ void Write (const tvIm& aVecImV2, const cRect2& aRectIm, const cRect2& aRectFile, double aDyn)
171+ {
172+ auto aName = mDataFile ->Name ();
173+ bool notUpdatable = false ;
135174
136- auto notUpdatable = false ;
137175 if (mDataFile ->IsCreateAtFirstWrite ())
138176 {
139- if (aMode != IoMode::Write)
140- {
141- MMVII_INTERNAL_ERROR (" GDAL read: image file created with CreateOnWrite() must be written before trying to read it (" + aName + " )" );
142- }
143- if (aRectFile.Sz () != aDF.Sz () || aRectFile.P0 () != cPt2di (0 ,0 ))
144- {
177+ if (aRectFile.Sz () != mDataFile ->Sz () || aRectFile.P0 () != cPt2di (0 , 0 ))
145178 MMVII_INTERNAL_ERROR (" GDAL write: image file created with CreateOnWrite() must be fully written on first write (" + aName + " )" );
146- }
147- // Delayed file creation, Dataset can be created in memory depending of file format driver
148- mGdalDataset = cGdalApi::CreateDataset (aDF, ¬Updatable);
149- if (notUpdatable) {
150- cGdalApi::SetCreatedNoUpdate (*mDataFile );
151- } else {
152- cGdalApi::SetCreated (*mDataFile );
153- }
179+
180+ mGdalDataset = cGdalApi::CreateDataset (*mDataFile , ¬Updatable);
181+ notUpdatable ? cGdalApi::SetCreatedNoUpdate (*mDataFile ) : cGdalApi::SetCreated (*mDataFile );
154182 }
155- else if (mDataFile ->IsCreatedNoUpdate () && aMode == IoMode::Write )
183+ else if (mDataFile ->IsCreatedNoUpdate ())
156184 {
157- if (aRectFile.Sz () != aDF.Sz () || aRectFile.P0 () != cPt2di (0 ,0 ))
158- {
185+ if (aRectFile.Sz () != mDataFile ->Sz () || aRectFile.P0 () != cPt2di (0 , 0 ))
159186 MMVII_INTERNAL_ERROR (" GDAL write: this image file format must be fully written on each write (" + aName + " )" );
160- }
161- mGdalDataset = cGdalApi::CreateDataset (aDF , ¬Updatable);
187+
188+ mGdalDataset = cGdalApi::CreateDataset (* mDataFile , ¬Updatable);
162189 }
163190 else
164191 {
165- mGdalDataset = cGdalApi::OpenDataset (aDF. Name (), aMode==IoMode::Read ? GA_ReadOnly : GA_Update, cGdalApi::eOnError::RaiseError);
192+ mGdalDataset = cGdalApi::OpenDataset (aName, GA_Update, cGdalApi::eOnError::RaiseError);
166193 }
167194
168- if (aMode == IoMode::Read) {
169- if (mGdalNbChan == mNbImg && mNbImg != 0 ) {
170- GdalReadNtoN (aVecImV2,aRectIm,aRectFile,aDyn); // file N -> N img channels
171- } else if (mGdalNbChan == 1 && mNbImg != 0 ) {
172- GdalRead1toN (aVecImV2,aRectIm,aRectFile,aDyn); // file 1 -> N img channels
173- } else if (mGdalNbChan != 0 && mNbImg == 1 ) {
174- GdalReadNto1 (aVecImV2,aRectIm,aRectFile,aDyn); // file N -> 1 img channels
175- } else {
176- MMVII_INTERNAL_ERROR (" Gdal read: Images vector size: " + std::to_string (mNbImg ) + " , file channels: " + std::to_string (mGdalNbChan ) + " (" + aName + " )" );
177- }
178- cGdalApi::CloseDataset (mGdalDataset );
179- } else {
180- FileLock gdalLock (aName);
181- // if (! notUpdatable)
182- // gdalLock.lock(aName);
183- if (mGdalNbChan == mNbImg && mNbImg != 0 ) {
184- GdalWriteNtoN (aVecImV2,aRectIm,aRectFile,aDyn); // img N -> N file channels
185- } else if (mGdalNbChan != 0 && mNbImg == 1 ) {
186- GdalWrite1toN (aVecImV2,aRectIm,aRectFile,aDyn); // img 1 -> N file channels
187- } else {
188- MMVII_INTERNAL_ERROR (" Gdal write: Images vector size: " + std::to_string (mNbImg ) + " , file channels: " + std::to_string (mGdalNbChan ) + " (" + aName + " )" );
189- }
195+ if (mGdalNbChan == mNbImg && mNbImg != 0 )
196+ GdalWriteNtoN (aVecImV2, aRectIm, aRectFile, aDyn); // img N -> N file channels
197+ else if (mGdalNbChan != 0 && mNbImg == 1 )
198+ GdalWrite1toN (aVecImV2, aRectIm, aRectFile, aDyn); // img 1 -> N file channels
199+ else
200+ MMVII_INTERNAL_ERROR (" Gdal write: Images vector size: " + std::to_string (mNbImg ) + " , file channels: " + std::to_string (mGdalNbChan ) + " (" + aName + " )" );
190201
191- if (notUpdatable) {
192- // Copy image from memory to file if image file driver needs it
193- remove (aName.c_str ());
194- auto aGdalDriver = cGdalApi::GetDriver (aName);
195- auto aGdalOptions = cGdalApi::GetCreateOptions (aGdalDriver, mDataFile ->CreateOptions ());
196- remove (aName.c_str ());
197- auto mFinalDataset = aGdalDriver->CreateCopy (aName.c_str (), mGdalDataset , FALSE , aGdalOptions.List (), NULL , NULL );
198- cGdalApi::CloseDataset (mFinalDataset );
199- }
200- cGdalApi::CloseDataset (mGdalDataset );
202+ if (notUpdatable)
203+ {
204+ // Copy image from memory to file if image file driver needs it
205+ auto aGdalDriver = cGdalApi::GetDriver (aName);
206+ auto aGdalOptions = cGdalApi::GetCreateOptions (aGdalDriver, mDataFile ->CreateOptions ());
207+ remove (aName.c_str ());
208+ auto mFinalDataset = aGdalDriver->CreateCopy (aName.c_str (), mGdalDataset , FALSE , aGdalOptions.List (), NULL , NULL );
209+ cGdalApi::CloseDataset (mFinalDataset );
201210 }
211+
212+ cGdalApi::CloseDataset (mGdalDataset );
202213 }
203214
204- private:
205215 // Helper class: manage N (1 by default) image buffers read from/write to file with GDAL
206216 // "Inherits" from outer class (GdalIO) the template parameter "TypeFile"
207217 class GDalBuffer
@@ -213,11 +223,6 @@ class GdalIO {
213223 {
214224 auto aSize = sizeof (TypeFile)*aRectIm.Sz ().x ()*aRectIm.Sz ().y ();
215225 std::generate (mBuffer .begin (), mBuffer .end (),[aSize](){return static_cast <TypeFile*>(cMemManager::Calloc (1 ,aSize));});
216- /* for (auto& aBuf : mBuffer)
217- {
218- aBuf = static_cast<TypeFile*>(cMemManager::Calloc(1,aSize));
219- }
220- */
221226 }
222227 ~GDalBuffer ()
223228 {
0 commit comments