libCZI
Reading and Writing CZI documents made easy
Loading...
Searching...
No Matches
libCZI_Write.h
1// SPDX-FileCopyrightText: 2017-2022 Carl Zeiss Microscopy GmbH
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#pragma once
6
7#include <string>
8#include <tuple>
9#include <memory>
10#include <limits>
11#include "libCZI.h"
12
13#if !defined(_LIBCZISTATICLIB) && !defined(__GNUC__)
14
15// When used as a DLL, we need to tinker with STL-classes used in exported structs/classes, or we get "warning C4251".
16// Note that using a different compiler-version/STL-version is still problematic and should be avoided.
17#if defined(LIBCZI_EXPORTS)
18 // https://web.archive.org/web/20141227011407/http://support.microsoft.com/kb/168958
19 template class LIBCZI_API std::function<bool(int callCnt, size_t offset, const void*& ptr, size_t& size)>;
20 template class LIBCZI_API std::function<const void* (int line)>;
21#else
22 extern template class LIBCZI_API std::function<bool(int callCnt, size_t offset, const void*& ptr, size_t& size)>;
23 extern template class LIBCZI_API std::function<const void* (int line)>;
24#endif
25
26#endif
27
28namespace libCZI
29{
31 class LIBCZI_API ICziWriterInfo
32 {
33 public:
34
40 virtual const IDimBounds* GetDimBounds() const = 0;
41
44 virtual const GUID& GetFileGuid() const = 0;
45
54 virtual bool TryGetMIndexMinMax(int* min, int* max) const = 0;
55
67 virtual bool TryGetReservedSizeForAttachmentDirectory(size_t* size) const = 0;
68
80 virtual bool TryGetReservedSizeForSubBlockDirectory(size_t* size) const = 0;
81
93 virtual bool TryGetReservedSizeForMetadataSegment(size_t* size) const = 0;
94
95 virtual ~ICziWriterInfo() = default;
96 };
97
99 class LIBCZI_API CCziWriterInfo :public libCZI::ICziWriterInfo
100 {
101 private:
102 bool dimBoundsValid;
103 CDimBounds dimBounds;
104 GUID fileGuid;
105 bool mBoundsValid;
106 int mMin, mMax;
107 size_t reservedSizeAttachmentsDir, reservedSizeSubBlkDir, reservedSizeMetadataSegment;
108 bool reservedSizeAttachmentsDirValid, reservedSizeSubBlkDirValid, reservedSizeMetadataSegmentValid;
109 public:
111 CCziWriterInfo() : CCziWriterInfo(GUID{ 0,0,0,{0,0,0,0,0,0,0,0} })
112 {}
113
120 explicit CCziWriterInfo(const GUID& fileGuid, int mMin = 1, int mMax = -1)
121 : fileGuid(fileGuid), reservedSizeAttachmentsDirValid(false), reservedSizeSubBlkDirValid(false), reservedSizeMetadataSegmentValid(false)
122 {
123 this->SetDimBounds(nullptr);
124 this->SetMIndexBounds(mMin, mMax);
125 }
126
134 CCziWriterInfo(const GUID& fileGuid, const IDimBounds& bounds, int mMin = 1, int mMax = -1)
135 : fileGuid(fileGuid), reservedSizeAttachmentsDirValid(false), reservedSizeSubBlkDirValid(false), reservedSizeMetadataSegmentValid(false)
136 {
137 this->SetDimBounds(&bounds);
138 this->SetMIndexBounds(mMin, mMax);
139 }
140
141 const IDimBounds* GetDimBounds() const override { return this->dimBoundsValid ? &this->dimBounds : nullptr; }
142 const GUID& GetFileGuid() const override { return this->fileGuid; }
143 bool TryGetMIndexMinMax(int* min, int* max) const override;
144 bool TryGetReservedSizeForAttachmentDirectory(size_t* size) const override;
145 bool TryGetReservedSizeForSubBlockDirectory(size_t* size) const override;
146 bool TryGetReservedSizeForMetadataSegment(size_t* size) const override;
147
152 void SetReservedSizeForAttachmentsDirectory(bool reserveSpace, size_t s);
153
158 void SetReservedSizeForSubBlockDirectory(bool reserveSpace, size_t s);
159
164 void SetReservedSizeForMetadataSegment(bool reserveSpace, size_t s);
165
168 void SetDimBounds(const IDimBounds* bounds);
169
174 void SetMIndexBounds(int mMin, int mMax);
175 };
176
178 struct LIBCZI_API AddSubBlockInfoBase
179 {
181 AddSubBlockInfoBase() { this->AddSubBlockInfoBase::Clear(); }
182
183 virtual ~AddSubBlockInfoBase() = default;
184
187 int mIndex;
188 int x;
189 int y;
198
203 std::int32_t compressionModeRaw;
204
206 virtual void Clear();
207
214
222 };
223
228 {
230 AddSubBlockInfo() : sizeData(0), sizeMetadata(0), sizeAttachment(0) {}
231
235 explicit AddSubBlockInfo(const AddSubBlockInfoBase& other) :
236 AddSubBlockInfoBase(other), sizeData(0), sizeMetadata(0), sizeAttachment(0)
237 {}
238
239 size_t sizeData;
240
246 std::function<bool(int callCnt, size_t offset, const void*& ptr, size_t& size)> getData;
247
249
255 std::function<bool(int callCnt, size_t offset, const void*& ptr, size_t& size)> getMetaData;
256
258
264 std::function<bool(int callCnt, size_t offset, const void*& ptr, size_t& size)> getAttachment;
265
267 void Clear() override;
268 };
269
274 {
277 ptrData(nullptr), dataSize(0), ptrSbBlkMetadata(nullptr), sbBlkMetadataSize(0), ptrSbBlkAttachment(nullptr), sbBlkAttachmentSize(0)
278 {}
279
280 const void* ptrData;
281 std::uint32_t dataSize;
282
283 const void* ptrSbBlkMetadata;
284 std::uint32_t sbBlkMetadataSize;
285
286 const void* ptrSbBlkAttachment;
287 std::uint32_t sbBlkAttachmentSize;
288
290 void Clear() override;
291 };
292
297 {
300 ptrBitmap(nullptr), strideBitmap(0), ptrSbBlkMetadata(nullptr), sbBlkMetadataSize(0), ptrSbBlkAttachment(nullptr), sbBlkAttachmentSize(0)
301 {}
302
303 const void* ptrBitmap;
304 std::uint32_t strideBitmap;
305
306 const void* ptrSbBlkMetadata;
307 std::uint32_t sbBlkMetadataSize;
308
309 const void* ptrSbBlkAttachment;
310 std::uint32_t sbBlkAttachmentSize;
311
313 void Clear() override;
314 };
315
320 {
322 AddSubBlockInfoLinewiseBitmap() :ptrSbBlkMetadata(nullptr), sbBlkMetadataSize(0), ptrSbBlkAttachment(nullptr), sbBlkAttachmentSize(0)
323 {}
324
327 std::function<const void* (int line)> getBitmapLine;
328
329 const void* ptrSbBlkMetadata;
330 std::uint32_t sbBlkMetadataSize;
331
332 const void* ptrSbBlkAttachment;
333 std::uint32_t sbBlkAttachmentSize;
334
336 void Clear() override;
337 };
338
340 struct LIBCZI_API AddAttachmentInfo
341 {
344
346 std::uint8_t contentFileType[8];
347
349 std::uint8_t name[80];
350
351 const void* ptrData;
352 std::uint32_t dataSize;
353
358 void SetContentFileType(const char* sz)
359 {
360 memset(this->contentFileType, 0, sizeof(this->contentFileType));
361 const auto len = strlen(sz);
362 memcpy(this->contentFileType, sz, (std::min)(sizeof(this->contentFileType), len));
363 }
364
369 void SetName(const char* sz)
370 {
371 memset(this->name, 0, sizeof(this->name));
372 const auto len = strlen(sz);
373 memcpy(this->name, sz, (std::min)(sizeof(this->name), len));
374 }
375 };
376
378 struct LIBCZI_API WriteMetadataInfo
379 {
380 const char* szMetadata;
382 const void* ptrAttachment;
384
386 void Clear() { memset(this, 0, sizeof(*this)); }
387 };
388
391 {
398 std::function<std::tuple<std::string, std::tuple<bool, std::string>>(int)> funcGenerateIdAndNameForChannel;
399 };
400
407 class LIBCZI_API ICziWriter
408 {
409 public:
415 virtual void Create(std::shared_ptr<IOutputStream> stream, std::shared_ptr<ICziWriterInfo> info) = 0;
416
423 virtual void SyncAddSubBlock(const AddSubBlockInfo& addSbBlkInfo) = 0;
424
430 virtual void SyncAddAttachment(const AddAttachmentInfo& addAttachmentInfo) = 0;
431
436 virtual void SyncWriteMetadata(const WriteMetadataInfo& metadataInfo) = 0;
437
441 virtual std::shared_ptr<libCZI::ICziMetadataBuilder> GetPreparedMetadata(const PrepareMetadataInfo& info) = 0;
442
446 virtual void Close() = 0;
447
451
452 virtual ~ICziWriter() = default;
453
458 void SyncAddSubBlock(const AddSubBlockInfoMemPtr& addSbBlkInfo);
459
465
470 void SyncAddSubBlock(const AddSubBlockInfoStridedBitmap& addSbBlkInfoStrideBitmap);
471 };
472
473 //-------------------------------------------------------------------------------------------
474
475 inline void AddSubBlockInfoBase::Clear()
476 {
477 this->coordinate.Clear();
478 this->mIndexValid = false;
479 this->x = (std::numeric_limits<int>::min)();
480 this->y = (std::numeric_limits<int>::min)();
481 this->logicalWidth = 0;
482 this->logicalHeight = 0;
483 this->physicalWidth = 0;
484 this->physicalHeight = 0;
486 this->pyramid_type = libCZI::SubBlockPyramidType::None;
487 this->SetCompressionMode(CompressionMode::UnCompressed);
488 }
489
490 inline void AddSubBlockInfoMemPtr::Clear()
491 {
492 this->AddSubBlockInfoBase::Clear();
493 this->ptrData = nullptr;
494 this->dataSize = 0;
495 this->ptrSbBlkMetadata = nullptr;
496 this->sbBlkMetadataSize = 0;
497 this->ptrSbBlkAttachment = nullptr;
498 this->sbBlkAttachmentSize = 0;
499 }
500
501 inline void AddSubBlockInfoLinewiseBitmap::Clear()
502 {
503 this->AddSubBlockInfoBase::Clear();
504 this->getBitmapLine = nullptr;
505 this->ptrSbBlkMetadata = nullptr;
506 this->sbBlkMetadataSize = 0;
507 this->ptrSbBlkAttachment = nullptr;
508 this->sbBlkAttachmentSize = 0;
509 }
510
511 inline void AddSubBlockInfo::Clear()
512 {
513 this->AddSubBlockInfoBase::Clear();
514 this->sizeData = 0;
515 this->getData = nullptr;
516 this->sizeMetadata = 0;
517 this->getMetaData = nullptr;
518 this->sizeAttachment = 0;
519 this->getAttachment = nullptr;
520 }
521
522 inline void AddSubBlockInfoStridedBitmap::Clear()
523 {
524 this->AddSubBlockInfoBase::Clear();
525 this->ptrBitmap = nullptr;
526 this->strideBitmap = 0;
527 this->ptrSbBlkMetadata = nullptr;
528 this->sbBlkMetadataSize = 0;
529 this->ptrSbBlkAttachment = nullptr;
530 this->sbBlkAttachmentSize = 0;
531 }
532
533 inline /*virtual*/bool CCziWriterInfo::TryGetMIndexMinMax(int* min, int* max) const
534 {
535 if (!this->mBoundsValid)
536 {
537 return false;
538 }
539
540 if (min != nullptr) { *min = this->mMin; }
541 if (max != nullptr) { *max = this->mMax; }
542 return true;
543 }
544
545 inline /*virtual*/bool CCziWriterInfo::TryGetReservedSizeForAttachmentDirectory(size_t* size) const
546 {
547 if (this->reservedSizeAttachmentsDirValid)
548 {
549 if (size != nullptr)
550 {
551 *size = this->reservedSizeAttachmentsDir;
552 }
553
554 return true;
555 }
556
557 return false;
558 }
559
560 inline /*virtual*/bool CCziWriterInfo::TryGetReservedSizeForSubBlockDirectory(size_t* size) const
561 {
562 if (this->reservedSizeSubBlkDirValid)
563 {
564 if (size != nullptr)
565 {
566 *size = this->reservedSizeSubBlkDir;
567 }
568
569 return true;
570 }
571
572 return false;
573 }
574
575 inline /*virtual*/bool CCziWriterInfo::TryGetReservedSizeForMetadataSegment(size_t* size) const
576 {
577 if (this->reservedSizeMetadataSegmentValid)
578 {
579 if (size != nullptr)
580 {
581 *size = this->reservedSizeMetadataSegment;
582 }
583
584 return true;
585 }
586
587 return false;
588 }
589
590 inline void CCziWriterInfo::SetReservedSizeForAttachmentsDirectory(bool reserveSpace, size_t s)
591 {
592 this->reservedSizeAttachmentsDirValid = reserveSpace;
593 if (reserveSpace)
594 {
595 this->reservedSizeAttachmentsDir = s;
596 }
597 }
598
599 inline void CCziWriterInfo::SetReservedSizeForSubBlockDirectory(bool reserveSpace, size_t s)
600 {
601 this->reservedSizeSubBlkDirValid = reserveSpace;
602 if (reserveSpace)
603 {
604 this->reservedSizeSubBlkDir = s;
605 }
606 }
607
608 inline void CCziWriterInfo::SetReservedSizeForMetadataSegment(bool reserveSpace, size_t s)
609 {
610 this->reservedSizeMetadataSegmentValid = reserveSpace;
611 if (reserveSpace)
612 {
613 this->reservedSizeMetadataSegment = s;
614 }
615 }
616
617 inline void CCziWriterInfo::SetDimBounds(const IDimBounds* bounds)
618 {
619 if (bounds == nullptr)
620 {
621 this->dimBoundsValid = false;
622 }
623 else
624 {
625 this->dimBounds = CDimBounds(bounds);
626 this->dimBoundsValid = true;
627 }
628 }
629
630 inline void CCziWriterInfo::SetMIndexBounds(int mMin, int mMax)
631 {
632 if (mMax < mMin)
633 {
634 this->mBoundsValid = false;
635 }
636 else
637 {
638 this->mMin = mMin;
639 this->mMax = mMax;
640 this->mBoundsValid = true;
641 }
642 }
643}
An implementation of the ICziWriterInfo-interface.
Definition libCZI_Write.h:100
CCziWriterInfo()
Default constructor - sets all information to "invalid" and sets fileGuid to GUID_NULL.
Definition libCZI_Write.h:111
CCziWriterInfo(const GUID &fileGuid, const IDimBounds &bounds, int mMin=1, int mMax=-1)
Definition libCZI_Write.h:134
const IDimBounds * GetDimBounds() const override
Definition libCZI_Write.h:141
const GUID & GetFileGuid() const override
Definition libCZI_Write.h:142
CCziWriterInfo(const GUID &fileGuid, int mMin=1, int mMax=-1)
Definition libCZI_Write.h:120
Implementation of a class representing an interval (and implementing the libCZI::IDimBounds-interface...
Definition libCZI_DimCoordinate.h:288
Implementation of a class representing a coordinate (and implementing the IDimCoordinate-interface).
Definition libCZI_DimCoordinate.h:149
Definition libCZI_Write.h:408
virtual void SyncAddAttachment(const AddAttachmentInfo &addAttachmentInfo)=0
void SyncAddSubBlock(const AddSubBlockInfoStridedBitmap &addSbBlkInfoStrideBitmap)
virtual void Close()=0
virtual std::shared_ptr< libCZI::ICziMetadataBuilder > GetPreparedMetadata(const PrepareMetadataInfo &info)=0
virtual libCZI::SubBlockStatistics GetStatistics() const =0
void SyncAddSubBlock(const AddSubBlockInfoMemPtr &addSbBlkInfo)
virtual void Create(std::shared_ptr< IOutputStream > stream, std::shared_ptr< ICziWriterInfo > info)=0
void SyncAddSubBlock(const libCZI::AddSubBlockInfoLinewiseBitmap &addSbInfoLinewise)
virtual void SyncAddSubBlock(const AddSubBlockInfo &addSbBlkInfo)=0
virtual void SyncWriteMetadata(const WriteMetadataInfo &metadataInfo)=0
The options for the CZI-writer.
Definition libCZI_Write.h:32
virtual bool TryGetReservedSizeForMetadataSegment(size_t *size) const =0
virtual const GUID & GetFileGuid() const =0
virtual bool TryGetReservedSizeForSubBlockDirectory(size_t *size) const =0
virtual const IDimBounds * GetDimBounds() const =0
virtual bool TryGetMIndexMinMax(int *min, int *max) const =0
virtual bool TryGetReservedSizeForAttachmentDirectory(size_t *size) const =0
Interface used to represent an interval (for several dimensions).
Definition libCZI_DimCoordinate.h:80
static std::int32_t CompressionModeToCompressionIdentifier(libCZI::CompressionMode mode)
static libCZI::CompressionMode CompressionModeFromRawCompressionIdentifier(std::int32_t m)
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition libCZI.h:31
CompressionMode
An enum specifying the compression method.
Definition libCZI_Pixels.h:161
SubBlockPyramidType
Definition libCZI_Pixels.h:174
@ None
No pyramid (indicating that the subblock is not a pyramid subblock, but a layer-0 subblock).
PixelType
An enum representing a pixel-type.
Definition libCZI_Pixels.h:144
@ Invalid
Invalid pixel type.
This struct describes an attachment to be added to a CZI-file.
Definition libCZI_Write.h:341
void SetContentFileType(const char *sz)
Definition libCZI_Write.h:358
std::uint32_t dataSize
Size of the attachment data (in bytes).
Definition libCZI_Write.h:352
void SetName(const char *sz)
Definition libCZI_Write.h:369
GUID contentGuid
Unique identifier for the content.
Definition libCZI_Write.h:343
const void * ptrData
Pointer to the attachment data.
Definition libCZI_Write.h:351
Information about a subblock.
Definition libCZI_Write.h:179
std::int32_t compressionModeRaw
Definition libCZI_Write.h:203
libCZI::PixelType PixelType
The pixel type of the subblock.
Definition libCZI_Write.h:194
int logicalHeight
The logical height of the subblock (in pixels).
Definition libCZI_Write.h:191
libCZI::SubBlockPyramidType pyramid_type
Definition libCZI_Write.h:195
int physicalWidth
The physical with of the subblock (in pixels).
Definition libCZI_Write.h:192
bool mIndexValid
Whether the field 'mIndex' is valid;.
Definition libCZI_Write.h:186
libCZI::CompressionMode GetCompressionMode() const
Definition libCZI_Write.h:218
void SetCompressionMode(libCZI::CompressionMode m)
Definition libCZI_Write.h:210
int x
The x-coordinate of the subblock.
Definition libCZI_Write.h:188
int mIndex
The M-index of the subblock.
Definition libCZI_Write.h:187
int physicalHeight
The physical height of the subblock (in pixels).
Definition libCZI_Write.h:193
libCZI::CDimCoordinate coordinate
The subblock's coordinate.
Definition libCZI_Write.h:185
int logicalWidth
The logical with of the subblock (in pixels).
Definition libCZI_Write.h:190
AddSubBlockInfoBase()
Default constructor.
Definition libCZI_Write.h:181
int y
The x-coordinate of the subblock.
Definition libCZI_Write.h:189
Definition libCZI_Write.h:228
std::function< bool(int callCnt, size_t offset, const void *&ptr, size_t &size)> getMetaData
Definition libCZI_Write.h:255
AddSubBlockInfo(const AddSubBlockInfoBase &other)
Definition libCZI_Write.h:235
std::function< bool(int callCnt, size_t offset, const void *&ptr, size_t &size)> getAttachment
Definition libCZI_Write.h:264
size_t sizeData
The size of the subblock's data in bytes.
Definition libCZI_Write.h:239
size_t sizeMetadata
The size of the subblock's metadata in bytes (note: max value is (numeric_limits<int>::max() ).
Definition libCZI_Write.h:248
AddSubBlockInfo()
Default constructor.
Definition libCZI_Write.h:230
std::function< bool(int callCnt, size_t offset, const void *&ptr, size_t &size)> getData
Definition libCZI_Write.h:246
size_t sizeAttachment
The size of the subblock's attachment in bytes (note: max value is (numeric_limits<int>::max() ).
Definition libCZI_Write.h:257
Definition libCZI_Write.h:320
std::uint32_t sbBlkMetadataSize
The size of the subblock-metadata in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no s...
Definition libCZI_Write.h:330
AddSubBlockInfoLinewiseBitmap()
Default constructor.
Definition libCZI_Write.h:322
const void * ptrSbBlkMetadata
Pointer to the subblock-metadata.
Definition libCZI_Write.h:329
std::uint32_t sbBlkAttachmentSize
The size of the subblock-attachment in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no...
Definition libCZI_Write.h:333
const void * ptrSbBlkAttachment
Pointer to the subblock-attachment.
Definition libCZI_Write.h:332
Definition libCZI_Write.h:274
AddSubBlockInfoMemPtr()
Default constructor.
Definition libCZI_Write.h:276
std::uint32_t dataSize
The size of the data in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no sub-block-data...
Definition libCZI_Write.h:281
std::uint32_t sbBlkAttachmentSize
The size of the subblock-attachment in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no...
Definition libCZI_Write.h:287
const void * ptrData
Pointer to the data to be put into the subblock.
Definition libCZI_Write.h:280
std::uint32_t sbBlkMetadataSize
The size of the subblock-metadata in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no s...
Definition libCZI_Write.h:284
const void * ptrSbBlkAttachment
Pointer to the subblock-attachment.
Definition libCZI_Write.h:286
const void * ptrSbBlkMetadata
Pointer to the subblock-metadata.
Definition libCZI_Write.h:283
Definition libCZI_Write.h:297
AddSubBlockInfoStridedBitmap()
Default constructor.
Definition libCZI_Write.h:299
const void * ptrBitmap
Pointer to the the bitmap to be put into the subblock. The size of the memory-block must be (strideBi...
Definition libCZI_Write.h:303
std::uint32_t sbBlkAttachmentSize
The size of the subblock-attachment in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no...
Definition libCZI_Write.h:310
const void * ptrSbBlkAttachment
Pointer to the subblock-attachment.
Definition libCZI_Write.h:309
std::uint32_t strideBitmap
The stride of the bitmap.
Definition libCZI_Write.h:304
std::uint32_t sbBlkMetadataSize
The size of the subblock-metadata in bytes. If this is 0, then ptrSbBlkMetadata is not used (and no s...
Definition libCZI_Write.h:307
const void * ptrSbBlkMetadata
Pointer to the subblock-metadata.
Definition libCZI_Write.h:306
Represents a globally unique identifier (GUID) consisting of four unsigned 32-bit integers.
Definition libCZI_Utilities.h:25
Information which is used to construct the metadata-preparation.
Definition libCZI_Write.h:391
Statistics about all sub-blocks found in a CZI-document.
Definition libCZI.h:437
This struct defines the data to be added as metadata-segment. Unused entries (e. g....
Definition libCZI_Write.h:379
const void * ptrAttachment
The metadata-attachment (not commonly used).
Definition libCZI_Write.h:382
size_t szMetadataSize
The size of the XML-string (in bytes)
Definition libCZI_Write.h:381
void Clear()
Clears this object to its blank/initial state.
Definition libCZI_Write.h:386
size_t attachmentSize
The size of the metadata-attachment.
Definition libCZI_Write.h:383
const char * szMetadata
The xml-string (in UTF-8 encoding)
Definition libCZI_Write.h:380