Skip to content

Commit 6e8ef0d

Browse files
nenadamarktsuchida
authored andcommitted
changes for the MMDevice API fix, added saveAndGet to MMCore to get image to display
1 parent af67cfa commit 6e8ef0d

8 files changed

Lines changed: 201 additions & 45 deletions

File tree

DeviceAdapters/go2scope/AcqZarrStorage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ int AcqZarrStorage::AppendImage(const char* handle, int sizeInBytes, unsigned ch
432432
return DEVICE_OK;
433433
}
434434

435-
int AcqZarrStorage::GetSummaryMeta(const char* handle, char* meta)
435+
int AcqZarrStorage::GetSummaryMeta(const char* handle, char** meta)
436436
{
437437
if (zarrStream == nullptr)
438438
{
@@ -446,13 +446,13 @@ int AcqZarrStorage::GetSummaryMeta(const char* handle, char* meta)
446446
}
447447

448448
// TODO:
449-
meta = new char[1];
449+
*meta = new char[1];
450450
meta[0] = 0;
451451

452452
return DEVICE_OK;
453453
}
454454

455-
int AcqZarrStorage::GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char* meta)
455+
int AcqZarrStorage::GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char** meta)
456456
{
457457
if (zarrStream == nullptr)
458458
{
@@ -466,7 +466,7 @@ int AcqZarrStorage::GetImageMeta(const char* handle, int coordinates[], int numC
466466
}
467467

468468
// TODO:
469-
meta = new char[1];
469+
*meta = new char[1];
470470
meta[0] = 0;
471471

472472
return DEVICE_OK;

DeviceAdapters/go2scope/AcqZarrStorage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class AcqZarrStorage : public CStorageBase<AcqZarrStorage>
5959
int List(const char* path, char** listOfDatasets, int maxItems, int maxItemLength);
6060
int AddImage(const char* handle, int sizeInBytes, unsigned char* pixels, int coordinates[], int numCoordinates, const char* imageMeta);
6161
int AppendImage(const char* handle, int sizeInBytes, unsigned char* pixels, const char* imageMeta);
62-
int GetSummaryMeta(const char* handle, char* meta);
63-
int GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char* meta);
62+
int GetSummaryMeta(const char* handle, char** meta);
63+
int GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char** meta);
6464
const unsigned char* GetImage(const char* handle, int coordinates[], int numCoordinates);
6565
int GetNumberOfDimensions(const char* handle, int& numDimensions);
6666
int GetDimension(const char* handle, int dimension, char* name, int nameLength, char* meaning, int meaningLength);
@@ -70,7 +70,7 @@ class AcqZarrStorage : public CStorageBase<AcqZarrStorage>
7070
bool IsReadOnly(const char* handle);
7171
int GetPath(const char* handle, char* path, int maxPathLength);
7272
int SetCustomMetadata(const char* handle, const char* key, const char* content) { return DEVICE_UNSUPPORTED_COMMAND; }
73-
int GetCustomMetadata(const char* handle, const char* key, char* content) { return DEVICE_UNSUPPORTED_COMMAND; }
73+
int GetCustomMetadata(const char* handle, const char* key, char** content) { return DEVICE_UNSUPPORTED_COMMAND; }
7474

7575

7676
// action interface

DeviceAdapters/go2scope/G2SBigTiffDataset.cpp

Lines changed: 126 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,57 @@ void G2SBigTiffDataset::load(const std::string& path, bool dio)
165165
if(dsname.find(".g2s") == dsname.size() - 4)
166166
dsname = dsname.substr(0, dsname.size() - 4);
167167

168+
// Determine file name prefix
169+
std::string fprefix = "";
170+
for(const auto& entry : std::filesystem::directory_iterator(xp))
171+
{
172+
// Skip auto folder paths
173+
auto fname = entry.path().filename().u8string();
174+
if(fname == "." || fname == "..")
175+
continue;
176+
177+
// Skip folders
178+
if(std::filesystem::is_directory(entry))
179+
continue;
180+
181+
// Skip unsupported file formats
182+
auto fext = entry.path().extension().u8string();
183+
if(fext.size() == 0)
184+
continue;
185+
if(fext[0] == '.')
186+
fext = fext.substr(1);
187+
std::transform(fext.begin(), fext.end(), fext.begin(), [](char c) { return (char)tolower(c); });
188+
if(fext != "tiff" && fext != "tif" && fext != "g2s.tiff" && fext != "g2s.tif")
189+
continue;
190+
191+
auto fx = fname.substr(0, fname.length() - fext.size() - 1);
192+
if(fx.find(".g2s") != std::string::npos)
193+
fx = fx.substr(0, fname.find(".g2s"));
194+
195+
if(fprefix.empty())
196+
// First file -> use the entire file name
197+
fprefix = fx;
198+
else if(fx.find(fprefix) == 0)
199+
// File name starts with the file prefix
200+
continue;
201+
else
202+
{
203+
// File prefix is invalid -> Try shorter file prefix
204+
auto lind = fprefix.find_last_of('_');
205+
while(lind != std::string::npos)
206+
{
207+
fprefix = fprefix.substr(0, lind);
208+
if(fx.find(fprefix) == 0)
209+
break;
210+
lind = fprefix.find_last_of('_');
211+
}
212+
if(fx.find(fprefix) != 0)
213+
throw std::runtime_error("Unable to load a dataset. Data chunk file name missmatch");
214+
}
215+
}
216+
168217
// Enumerate files
218+
std::vector<std::uint32_t> dcindex;
169219
for(const auto& entry : std::filesystem::directory_iterator(xp))
170220
{
171221
// Skip auto folder paths
@@ -187,26 +237,72 @@ void G2SBigTiffDataset::load(const std::string& path, bool dio)
187237
if(fext != "tiff" && fext != "tif" && fext != "g2s.tiff" && fext != "g2s.tif")
188238
continue;
189239

190-
// We found a supported file type -> Add to results list
240+
// Determine absolute path and create chunk descriptor
191241
auto abspath = std::filesystem::absolute(entry).u8string();
192242
auto dchunk = std::make_shared<G2SBigTiffStream>(abspath, directIo);
193-
datachunks.push_back(dchunk);
243+
244+
// Determine chunk index from a file name
245+
std::uint32_t cind = 0;
246+
auto findtoken = fname.substr(0, fname.length() - fext.size() - 1);
247+
if(findtoken.find(".g2s") != std::string::npos)
248+
findtoken = findtoken.substr(0, fname.find(".g2s"));
249+
findtoken = findtoken.substr(fprefix.length());
250+
if(!findtoken.empty())
251+
{
252+
if(findtoken[0] == '_')
253+
findtoken = findtoken.substr(1);
254+
try { cind = std::stoul(findtoken); } catch(...) { }
255+
}
256+
257+
// Check if chunk index is valid and determine insert index
258+
std::int64_t iind = -1;
259+
for(std::size_t i = 0; i < dcindex.size(); i++)
260+
{
261+
if(dcindex[i] >= cind)
262+
{
263+
iind = (std::int64_t)i;
264+
break;
265+
}
266+
}
267+
if(iind < 0)
268+
{
269+
datachunks.push_back(dchunk);
270+
dcindex.push_back(cind);
271+
}
272+
else
273+
{
274+
auto cit = datachunks.begin();
275+
std::advance(cit, iind);
276+
auto iit = dcindex.begin();
277+
std::advance(iit, iind);
278+
279+
datachunks.insert(cit, dchunk);
280+
dcindex.insert(iit, cind);
281+
}
194282
}
195283
if(datachunks.empty())
196284
throw std::runtime_error("Unable to load a dataset. No files found");
197285

198286
// Load first data chunk
199-
samples = 1;
200-
imgcounter = 0;
201-
metadata.clear();
202-
custommeta.clear();
203-
activechunk = datachunks.front();
204-
activechunk->open(false);
205-
activechunk->parse(datasetuid, shape, chunksize, metadata, bitdepth);
206-
imgcounter += activechunk->getImageCount();
207-
resetAxisInfo();
208-
parseAxisInfo();
209-
parseCustomMetadata();
287+
try
288+
{
289+
samples = 1;
290+
imgcounter = 0;
291+
metadata.clear();
292+
custommeta.clear();
293+
activechunk = datachunks.front();
294+
activechunk->open(false);
295+
activechunk->parse(datasetuid, shape, chunksize, metadata, bitdepth);
296+
imgcounter += activechunk->getImageCount();
297+
resetAxisInfo();
298+
parseAxisInfo();
299+
parseCustomMetadata();
300+
}
301+
catch(std::exception&)
302+
{
303+
close();
304+
throw;
305+
}
210306

211307
// Validate dataset parameters
212308
if(activechunk->getChunkIndex() != 0)
@@ -231,11 +327,19 @@ void G2SBigTiffDataset::load(const std::string& path, bool dio)
231327
}
232328

233329
// Parse headers for other data chunks
234-
for(std::size_t i = 1; i < datachunks.size(); i++)
330+
try
235331
{
236-
validateDataChunk((std::uint32_t)i, false);
237-
imgcounter += datachunks[i]->getImageCount();
238-
datachunks[i]->close();
332+
for(std::size_t i = 1; i < datachunks.size(); i++)
333+
{
334+
validateDataChunk((std::uint32_t)i, false);
335+
imgcounter += datachunks[i]->getImageCount();
336+
datachunks[i]->close();
337+
}
338+
}
339+
catch(std::exception&)
340+
{
341+
close();
342+
throw;
239343
}
240344
}
241345

@@ -726,6 +830,11 @@ void G2SBigTiffDataset::validateDataChunk(std::uint32_t chunkind, bool index)
726830
datachunks[chunkind]->close();
727831
throw std::runtime_error("Invalid data chunk. Pixel format missmatch");
728832
}
833+
if(datachunks[chunkind]->getChunkIndex() > 0 && datachunks[chunkind]->getChunkIndex() != chunkind)
834+
{
835+
datachunks[chunkind]->close();
836+
throw std::runtime_error("Invalid data chunk. Chunk index missmatch");
837+
}
729838
}
730839

731840
/**

DeviceAdapters/go2scope/G2SBigTiffStorage.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ int G2SBigTiffStorage::List(const char* path, char** listOfDatasets, int maxItem
591591
if(!exs || !isdir)
592592
return ERR_TIFF_INVALID_PATH;
593593
auto allfnd = scanDir(path, listOfDatasets, maxItems, maxItemLength, 0);
594+
595+
// TODO: review memory allocation and whether the limit can be removed
594596
return allfnd ? DEVICE_OK : ERR_TIFF_STRING_TOO_LONG;
595597
}
596598
catch(std::exception& e)
@@ -690,7 +692,7 @@ int G2SBigTiffStorage::AppendImage(const char* handle, int sizeInBytes, unsigned
690692
* @param meta Metadata buffer [out]
691693
* @return Status code
692694
*/
693-
int G2SBigTiffStorage::GetSummaryMeta(const char* handle, char* meta) noexcept
695+
int G2SBigTiffStorage::GetSummaryMeta(const char* handle, char** meta) noexcept
694696
{
695697
if(handle == nullptr)
696698
return DEVICE_INVALID_INPUT_PARAM;
@@ -707,13 +709,13 @@ int G2SBigTiffStorage::GetSummaryMeta(const char* handle, char* meta) noexcept
707709
{
708710
// Copy metadata string
709711
auto fs = reinterpret_cast<G2SBigTiffDataset*>(it->second.FileHandle);
710-
meta = new char[fs->getMetadata().size() + 1];
711-
strncpy(meta, fs->getMetadata().c_str(), fs->getMetadata().size() + 1);
712+
*meta = new char[fs->getMetadata().size() + 1];
713+
strncpy(*meta, fs->getMetadata().c_str(), fs->getMetadata().size() + 1);
712714
return DEVICE_OK;
713715
}
714716
catch(std::exception& e)
715717
{
716-
LogMessage("GetSummaryMeta error: " +std::string(e.what()));
718+
LogMessage("GetSummaryMeta error: " + std::string(e.what()));
717719
return ERR_TIFF_CORRUPTED_METADATA;
718720
}
719721
}
@@ -729,7 +731,7 @@ int G2SBigTiffStorage::GetSummaryMeta(const char* handle, char* meta) noexcept
729731
* @param bufSize Buffer size
730732
* @return Status code
731733
*/
732-
int G2SBigTiffStorage::GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char* meta) noexcept
734+
int G2SBigTiffStorage::GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char** meta) noexcept
733735
{
734736
if(handle == nullptr || coordinates == nullptr || numCoordinates == 0)
735737
return DEVICE_INVALID_INPUT_PARAM;
@@ -759,13 +761,13 @@ int G2SBigTiffStorage::GetImageMeta(const char* handle, int coordinates[], int n
759761
try
760762
{
761763
auto fmeta = fs->getImageMetadata(coords);
762-
meta = new char[fmeta.size() + 1];
763-
strncpy(meta, fs->getMetadata().c_str(), fmeta.size() + 1);
764+
*meta = new char[fmeta.size() + 1];
765+
strncpy(*meta, fmeta.c_str(), fmeta.size() + 1);
764766
return DEVICE_OK;
765767
}
766768
catch(std::exception& e)
767769
{
768-
LogMessage("GetImageMeta error: " +std::string(e.what()));
770+
LogMessage("GetImageMeta error: " + std::string(e.what()));
769771
return ERR_TIFF_CORRUPTED_METADATA;
770772
}
771773
}
@@ -1029,7 +1031,7 @@ int G2SBigTiffStorage::SetCustomMetadata(const char* handle, const char* key, co
10291031
* @param content Metadata entry value / content [out]
10301032
* @return Status code
10311033
*/
1032-
int G2SBigTiffStorage::GetCustomMetadata(const char* handle, const char* key, char* content) noexcept
1034+
int G2SBigTiffStorage::GetCustomMetadata(const char* handle, const char* key, char** content) noexcept
10331035
{
10341036
if(handle == nullptr || key == nullptr)
10351037
return DEVICE_INVALID_INPUT_PARAM;
@@ -1045,8 +1047,8 @@ int G2SBigTiffStorage::GetCustomMetadata(const char* handle, const char* key, ch
10451047
try
10461048
{
10471049
auto mval = fs->getCustomMetadata(key);
1048-
content = new char[mval.size() + 1];
1049-
strncpy(content, mval.c_str(), mval.size() + 1);
1050+
*content = new char[mval.size() + 1];
1051+
strncpy(*content, mval.c_str(), mval.size() + 1);
10501052
return DEVICE_OK;
10511053
}
10521054
catch(std::exception& e)

DeviceAdapters/go2scope/G2SBigTiffStorage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ class G2SBigTiffStorage : public CStorageBase<G2SBigTiffStorage>
6666
int List(const char* path, char** listOfDatasets, int maxItems, int maxItemLength) noexcept;
6767
int AddImage(const char* handle, int sizeInBytes, unsigned char* pixels, int coordinates[], int numCoordinates, const char* imageMeta) noexcept;
6868
int AppendImage(const char* handle, int sizeInBytes, unsigned char* pixels, const char* imageMeta) noexcept;
69-
int GetSummaryMeta(const char* handle, char* meta) noexcept;
70-
int GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char* meta) noexcept;
69+
int GetSummaryMeta(const char* handle, char** meta) noexcept;
70+
int GetImageMeta(const char* handle, int coordinates[], int numCoordinates, char** meta) noexcept;
7171
const unsigned char* GetImage(const char* handle, int coordinates[], int numCoordinates) noexcept;
7272
int GetNumberOfDimensions(const char* handle, int& numDimensions) noexcept;
7373
int GetDimension(const char* handle, int dimension, char* name, int nameLength, char* meaning, int meaningLength) noexcept;
7474
int GetCoordinate(const char* handle, int dimension, int coordinate, char* name, int nameLength) noexcept;
7575
int GetImageCount(const char* handle, int& imgcnt) noexcept;
7676
int SetCustomMetadata(const char* handle, const char* key, const char* content) noexcept;
77-
int GetCustomMetadata(const char* handle, const char* key, char* content) noexcept;
77+
int GetCustomMetadata(const char* handle, const char* key, char** content) noexcept;
7878
bool IsOpen(const char* handle) noexcept;
7979
bool IsReadOnly(const char* handle) noexcept;
8080
int GetPath(const char* handle, char* path, int maxPathLength) noexcept;

MMCore/Devices/StorageInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ int StorageInstance::GetSummaryMeta(const char* handle, std::string& meta)
149149
{
150150
char* cMeta(nullptr);
151151
RequireInitialized(__func__);
152-
int ret = GetImpl()->GetSummaryMeta(handle, cMeta);
152+
int ret = GetImpl()->GetSummaryMeta(handle, &cMeta);
153153
if (ret == DEVICE_OK)
154154
{
155155
if (cMeta)
@@ -163,7 +163,7 @@ int StorageInstance::GetImageMeta(const char* handle, const std::vector<int>& co
163163
{
164164
char* cMeta(nullptr);
165165
RequireInitialized(__func__);
166-
int ret = GetImpl()->GetImageMeta(handle, const_cast<int*>(&coordinates[0]), (int)coordinates.size(), cMeta);
166+
int ret = GetImpl()->GetImageMeta(handle, const_cast<int*>(&coordinates[0]), (int)coordinates.size(), &cMeta);
167167
if (ret == DEVICE_OK)
168168
{
169169
if (cMeta)
@@ -177,7 +177,7 @@ int StorageInstance::GetCustomMeta(const char* handle, const std::string& key, s
177177
{
178178
char* cMeta(nullptr);
179179
RequireInitialized(__func__);
180-
int ret = GetImpl()->GetCustomMetadata(handle, key.c_str(), cMeta);
180+
int ret = GetImpl()->GetCustomMetadata(handle, key.c_str(), &cMeta);
181181
if (ret == DEVICE_OK)
182182
{
183183
if (cMeta)

0 commit comments

Comments
 (0)