37 template<
class StructHeader>
48 unsigned char*
data();
54 StructHeader*
header() {
return reinterpret_cast<StructHeader*
>(
data()); }
112 template<
typename ValType, ImgOptions OPTIONS>
122 ValType&
operator()(
unsigned dx,
unsigned dy,
unsigned channel = 0);
160 template<
typename UserType, ImgOptions OPTIONS = ImgOptions::RowMajor>
162 [[maybe_unused]]
auto* h =
header();
163 assert(h->channels > 0 &&
"call setShape() before interpretedView()!");
164 assert(h->bytesPerPixel == h->channels *
sizeof(UserType) &&
165 "choose correct bytesPerPixel and channels value before conversion!");
168 "inconsistent data ordering col/row major");
179 template<
class StructHeader>
182 : _accToData(accToData) {
183 static_assert(std::is_base_of<OpaqueStructHeader, StructHeader>::value,
184 "MappedStruct expects StructHeader to implement OpaqueStructHeader");
185 if(doInitData == InitData::Yes) {
190 template<
class StructHeader>
192 return _accToData.
data();
195 template<
class StructHeader>
198 return const_cast<MappedStruct*
>(
this)->_accToData.getNElements();
201 template<
class StructHeader>
203 size_t sh =
sizeof(StructHeader);
204 if(capacity() < sh) {
205 throw logic_error(
"buffer provided to MappedStruct is too small for correct initialization");
209 header()->totalLength = sh;
210 memset(p + sh, 0, capacity() - sh);
215 inline void MappedImage::formatsDefinition(
ImgFormat fmt,
unsigned& channels,
unsigned& bytesPerPixel) {
217 case ImgFormat::Unset:
218 assert(
false &&
"ImgFormat::Unset not allowed");
220 case ImgFormat::Gray8:
224 case ImgFormat::Gray16:
228 case ImgFormat::RGB24:
232 case ImgFormat::RGBA32:
236 case ImgFormat::FLOAT1:
237 case ImgFormat::FLOAT2:
238 case ImgFormat::FLOAT3:
239 case ImgFormat::FLOAT4:
240 channels = unsigned(fmt) - unsigned(ImgFormat::FLOAT1) + 1;
241 bytesPerPixel = 4 * channels;
243 case ImgFormat::DOUBLE1:
244 case ImgFormat::DOUBLE2:
245 case ImgFormat::DOUBLE3:
246 case ImgFormat::DOUBLE4:
247 channels = unsigned(fmt) - unsigned(ImgFormat::DOUBLE1) + 1;
248 bytesPerPixel = 8 * channels;
253 inline size_t MappedImage::lengthForShape(
unsigned width,
unsigned height,
ImgFormat fmt) {
255 unsigned bytesPerPixel;
256 formatsDefinition(fmt, channels, bytesPerPixel);
257 return sizeof(
ImgHeader) + (
size_t)width * height * bytesPerPixel;
260 inline void MappedImage::setShape(
unsigned width,
unsigned height,
ImgFormat fmt) {
262 unsigned bytesPerPixel;
263 formatsDefinition(fmt, channels, bytesPerPixel);
264 size_t totalLen = lengthForShape(width, height, fmt);
265 if(totalLen > capacity()) {
266 throw logic_error(
"MappedImage: provided buffer to small for requested image shape");
272 h->totalLength = totalLen;
275 h->channels = channels;
276 h->bytesPerPixel = bytesPerPixel;
279 template<
typename ValType, ImgOptions OPTIONS>
282 assert(dy < h->height);
283 assert(dx < h->width);
284 assert(channel < h->channels);
291 if constexpr((
unsigned)OPTIONS & (
unsigned)ImgOptions::RowMajor) {
292 return vec()[(dy * h->width + dx) * h->channels + channel];
295 return vec()[(dy + dx * h->height) * h->channels + channel];
299 template<
typename ValType, ImgOptions OPTIONS>
304 template<
typename ValType, ImgOptions OPTIONS>
306 return reinterpret_cast<ValType*
>(_mi->imgBody());