ChimeraTK-DeviceAccess-DoocsBackend 01.11.02
Loading...
Searching...
No Matches
DoocsBackendImageRegisterAccessor.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
5
6#include <ChimeraTK/MappedImage.h>
7
8#include <doocs/EqData.h>
9
10#include <type_traits>
11#include <utility>
12
13namespace ChimeraTK {
14
15 /********************************************************************************************************************/
16
18 const std::string& path, const std::string& registerPathName, size_t numberOfBytes, size_t wordOffsetInRegister,
19 AccessModeFlags flags)
21 std::move(backend), path, registerPathName, numberOfBytes, wordOffsetInRegister, std::move(flags)) {
22 // check doocs data type
23 if(DoocsBackendRegisterAccessor<uint8_t>::src.type() != DATA_IMAGE) {
24 this->shutdown();
25 throw ChimeraTK::logic_error(std::string("DOOCS data type ") +
26 std::to_string(DoocsBackendRegisterAccessor<uint8_t>::src.type()) +
27 " not supported by DoocsBackendImageRegisterAccessor."); // LCOV_EXCL_LINE (already prevented in the Backend)
28 }
29 if(wordOffsetInRegister > 0) {
30 throw ChimeraTK::logic_error(
31 "DoocsBackendImageRegisterAccessor does not support nonzero offset, register:" + registerPathName);
32 }
33 }
34
35 /********************************************************************************************************************/
36
40
41 /********************************************************************************************************************/
42
43 void DoocsBackendImageRegisterAccessor::doPostRead(TransferType type, bool hasNewData) {
45 if(!hasNewData) return;
46
47 IMH h;
48 int len;
49 uint8_t* vals;
50 bool ok = DoocsBackendRegisterAccessorBase::dst.get_image(&vals, &len, &h);
51 if(!ok) {
52 // this should not happen, since dst.error() was already checked by super class DoocsBackendRegisterAccessor
53 assert(false);
54 // surely we cannot use vals
55 return;
56 }
57 auto sharedThis = boost::static_pointer_cast<NDRegisterAccessor<uint8_t>>(this->shared_from_this());
58 OneDRegisterAccessor<uint8_t> abstractAcc(sharedThis);
59
60 // convert DOOCS image -> MappedImage
61 MappedDoocsImgIn mi(abstractAcc);
62 mi.set(&h, vals);
63 }
64
65 /********************************************************************************************************************/
66
67 ImgFormat MappedDoocsImgIn::getImgFormat(const IMH* h) {
68 switch(h->image_format) {
69 case TTF2_IMAGE_FORMAT_GRAY:
70 if(h->bpp == 1) {
71 return ImgFormat::Gray8;
72 }
73 else if(h->bpp == 2) {
74 return ImgFormat::Gray16;
75 }
76 else {
77 return ImgFormat::Unset;
78 }
79 case TTF2_IMAGE_FORMAT_RGB:
80 return ImgFormat::RGB24;
81 case TTF2_IMAGE_FORMAT_RGBA:
82 return ImgFormat::RGBA32;
83 default:
84 return ImgFormat::Unset;
85 }
86 }
87
88 void MappedDoocsImgIn::set(const IMH* h, const uint8_t* body) {
89 auto imgFormat = getImgFormat(h);
90 if(imgFormat == ImgFormat::Unset) {
91 throw logic_error("MappedDoocsImgIn: unsupported image input");
92 }
93 // following lines assume that bodyLength = bpp*height*width both for DOOCS image and our MappedImage,
94 // which is true for the image formats we currently support.
95 unsigned useLines = h->height;
96 unsigned bytesPerLine = h->width * h->bpp;
97 if(lengthForShape(h->width, h->height, imgFormat) > capacity()) {
98 // truncate image: reduce number of lines
99 useLines = (capacity() - sizeof(ImgHeader)) / bytesPerLine;
100 }
101 setShape(h->width, useLines, imgFormat);
102 // copy meta info
103 auto* hOut = this->header();
104 hOut->effBitsPerPixel = h->ebitpp;
105 hOut->frame = h->frame;
106 hOut->x_start = h->x_start;
107 hOut->y_start = h->y_start;
108 hOut->scale_x = h->scale_x;
109 hOut->scale_y = h->scale_y;
110
111 memcpy(imgBody(), body, useLines * bytesPerLine);
112 // fill unused part with zeros
113 memset(imgBody() + useLines * bytesPerLine, 0, capacity() - sizeof(ImgHeader) - useLines * bytesPerLine);
114 }
115
116 /********************************************************************************************************************/
117
118} // namespace ChimeraTK
void doPostRead(TransferType type, bool hasNewData) override
DoocsBackendImageRegisterAccessor(boost::shared_ptr< DoocsBackend > backend, const std::string &path, const std::string &registerPathName, size_t numberOfBytes, size_t wordOffsetInRegister, AccessModeFlags flags)
numberOfBytes: defines length or byte array.
void shutdown()
All implementations must call this function in their destructor.
void doPostRead(TransferType, bool hasNewData) override
implements conversion from DOOCS image -> MappedImage
void set(const IMH *h, const std::uint8_t *body)