• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paulmthompson / WhiskerToolbox / 17402643318

02 Sep 2025 11:53AM UTC coverage: 71.394% (-0.3%) from 71.68%
17402643318

push

github

paulmthompson
remove unnecessary files

31766 of 44494 relevant lines covered (71.39%)

1386.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.78
/src/DataManager/Media/Media_Data.cpp
1

2
#include "Media/Media_Data.hpp"
3
#ifdef ENABLE_OPENCV
4
#include "OpenCVImageProcessor.hpp"
5
#endif
6

7
#include <algorithm>
8
#include <stdexcept>
9

10
MediaData::MediaData() {
237✔
11
#ifdef ENABLE_OPENCV
12
    // Register OpenCV processor
13
    static bool opencv_registered = false;
14
    if (!opencv_registered) {
237✔
15
        ImageProcessing::registerOpenCVProcessor();
126✔
16
        opencv_registered = true;
126✔
17
    }
18
    
19
    // Default to OpenCV processor
20
    setImageProcessor("opencv");
711✔
21
#endif
22
};
237✔
23

24
MediaData::~MediaData() = default;
237✔
25

26
void MediaData::setFormat(DisplayFormat const format) {
×
27
    _format = format;
×
28
    switch (_format) {
×
29
        case DisplayFormat::Gray:
×
30
            _display_format_bytes = 1;
×
31
            break;
×
32
        case DisplayFormat::Color:
×
33
            _display_format_bytes = 4;
×
34
            break;
×
35
        default:
×
36
            _display_format_bytes = 1;
×
37
            break;
×
38
    }
39
    _resizeDataStorage();
×
40
};
×
41

42
void MediaData::updateHeight(int const height) {
1✔
43
    _height = height;
1✔
44
    _resizeDataStorage();
1✔
45
};
1✔
46

47
void MediaData::updateWidth(int const width) {
1✔
48
    _width = width;
1✔
49
    _resizeDataStorage();
1✔
50
};
1✔
51

52
void MediaData::LoadMedia(std::string const & name) {
×
53
    doLoadMedia(name);
×
54
}
×
55

56
void MediaData::LoadFrame(int const frame_id) {
1✔
57
    doLoadFrame(frame_id);
1✔
58

59
    _last_loaded_frame = frame_id;
1✔
60
}
1✔
61

62
std::vector<uint8_t> const & MediaData::getRawData8(int const frame_number) {
×
63
    if (frame_number != _last_loaded_frame) {
×
64
        LoadFrame(frame_number);
×
65
    }
66

67
    if (MediaStorage::is8Bit(_rawData)) {
×
68
        return std::get<MediaStorage::ImageData8>(_rawData);
×
69
    } else {
70
        // Convert from 32-bit to 8-bit
71
        auto const& float_data = std::get<MediaStorage::ImageData32>(_rawData);
×
72
        _convertTo8Bit(float_data, _temp_8bit_buffer);
×
73
        return _temp_8bit_buffer;
×
74
    }
75
}
76

77
std::vector<float> const & MediaData::getRawData32(int const frame_number) {
×
78
    if (frame_number != _last_loaded_frame) {
×
79
        LoadFrame(frame_number);
×
80
    }
81

82
    if (MediaStorage::is32Bit(_rawData)) {
×
83
        return std::get<MediaStorage::ImageData32>(_rawData);
×
84
    } else {
85
        // Convert from 8-bit to 32-bit
86
        auto const& uint8_data = std::get<MediaStorage::ImageData8>(_rawData);
×
87
        _convertTo32Bit(uint8_data, _temp_32bit_buffer);
×
88
        return _temp_32bit_buffer;
×
89
    }
90
}
91

92
MediaData::DataStorage const& MediaData::getRawDataVariant(int const frame_number) {
×
93
    if (frame_number != _last_loaded_frame) {
×
94
        LoadFrame(frame_number);
×
95
    }
96
    return _rawData;
×
97
}
98

99
std::vector<uint8_t> MediaData::getProcessedData8(int const frame_number) {
3✔
100
    if (frame_number != _last_loaded_frame) {
3✔
101
        LoadFrame(frame_number);
1✔
102
    }
103

104
    if (_last_processed_frame != _last_loaded_frame) {
3✔
105
        _processData();
1✔
106
    }
107

108
    if (MediaStorage::is8Bit(_processedData)) {
3✔
109
        return std::get<MediaStorage::ImageData8>(_processedData);
3✔
110
    } else {
111
        // Convert from 32-bit to 8-bit
112
        auto const& float_data = std::get<MediaStorage::ImageData32>(_processedData);
×
113
        std::vector<uint8_t> result;
×
114
        _convertTo8Bit(float_data, result);
×
115
        return result;
×
116
    }
×
117
}
118

119
std::vector<float> MediaData::getProcessedData32(int const frame_number) {
×
120
    if (frame_number != _last_loaded_frame) {
×
121
        LoadFrame(frame_number);
×
122
    }
123

124
    if (_last_processed_frame != _last_loaded_frame) {
×
125
        _processData();
×
126
    }
127

128
    if (MediaStorage::is32Bit(_processedData)) {
×
129
        return std::get<MediaStorage::ImageData32>(_processedData);
×
130
    } else {
131
        // Convert from 8-bit to 32-bit
132
        auto const& uint8_data = std::get<MediaStorage::ImageData8>(_processedData);
×
133
        std::vector<float> result;
×
134
        _convertTo32Bit(uint8_data, result);
×
135
        return result;
×
136
    }
×
137
}
138

139
MediaData::DataStorage MediaData::getProcessedDataVariant(int const frame_number) {
×
140
    if (frame_number != _last_loaded_frame) {
×
141
        LoadFrame(frame_number);
×
142
    }
143

144
    if (_last_processed_frame != _last_loaded_frame) {
×
145
        _processData();
×
146
    }
147

148
    return _processedData;
×
149
}
150

151
void MediaData::setRawData(MediaStorage::ImageData8 data) {
1✔
152
    _bit_depth = BitDepth::Bit8;
1✔
153
    _rawData = std::move(data);
1✔
154
    // Only resize processed data to match the new bit depth, don't touch _rawData
155
    if (MediaStorage::is32Bit(_processedData)) {
1✔
156
        int const new_size = _height * _width * _display_format_bytes;
×
157
        _processedData = MediaStorage::ImageData8(static_cast<size_t>(new_size));
×
158
    }
159
    // Invalidate processed data since raw data changed
160
    _last_processed_frame = -1;
1✔
161
}
1✔
162

163
void MediaData::setRawData(MediaStorage::ImageData32 data) {
×
164
    _bit_depth = BitDepth::Bit32;
×
165
    _rawData = std::move(data);
×
166
    // Only resize processed data to match the new bit depth, don't touch _rawData
167
    if (MediaStorage::is8Bit(_processedData)) {
×
168
        int const new_size = _height * _width * _display_format_bytes;
×
169
        _processedData = MediaStorage::ImageData32(static_cast<size_t>(new_size));
×
170
    }
171
    // Invalidate processed data since raw data changed
172
    _last_processed_frame = -1;
×
173
}
×
174

175

176
// ImageProcessor-based methods
177
bool MediaData::setImageProcessor(std::string const& processor_name) {
237✔
178
    auto new_processor = ImageProcessing::ProcessorRegistry::createProcessor(processor_name);
237✔
179
    if (new_processor) {
237✔
180
        _image_processor = std::move(new_processor);
237✔
181
        _processor_name = processor_name;
237✔
182
        // Reprocess data with new processor
183
        if (_last_loaded_frame != -1) {
237✔
184
            _processData();
×
185
            notifyObservers();
×
186
        }
187
        return true;
237✔
188
    }
189
    return false;
×
190
}
237✔
191

192
std::string MediaData::getImageProcessorName() const {
×
193
    return _processor_name;
×
194
}
195

196
void MediaData::addProcessingStep(std::string const& key, std::function<void(void*)> processor) {
×
197
    if (_image_processor) {
×
198
        _image_processor->addProcessingStep(key, std::move(processor));
×
199
        _processData();
×
200
        notifyObservers();
×
201
    }
202
}
×
203

204
void MediaData::removeProcessingStep(std::string const& key) {
×
205
    if (_image_processor) {
×
206
        _image_processor->removeProcessingStep(key);
×
207
        _processData();
×
208
        notifyObservers();
×
209
    }
210
}
×
211

212
void MediaData::clearProcessingSteps() {
×
213
    if (_image_processor) {
×
214
        _image_processor->clearProcessingSteps();
×
215
        _processData();
×
216
        notifyObservers();
×
217
    }
218
}
×
219

220
bool MediaData::hasProcessingStep(std::string const& key) const {
×
221
    return _image_processor ? _image_processor->hasProcessingStep(key) : false;
×
222
}
223

224
size_t MediaData::getProcessingStepCount() const {
×
225
    return _image_processor ? _image_processor->getProcessingStepCount() : 0;
×
226
}
227

228
void MediaData::setBitDepth(BitDepth depth) {
×
229
    if (_bit_depth != depth) {
×
230
        _bit_depth = depth;
×
231
        _resizeDataStorage();
×
232
        // Invalidate processed data since bit depth changed
233
        _last_processed_frame = -1;
×
234
    }
235
}
×
236

237
void MediaData::_processData() {
1✔
238
    _processedData = _rawData;  // Copy raw data to processed data
1✔
239

240
    // Use ImageProcessor system if available
241
    if (_image_processor && _image_processor->getProcessingStepCount() > 0) {
1✔
242
        // No conversion needed! Both use the same MediaStorage::ImageDataVariant type
243
        _processedData = _image_processor->processImage(_processedData, getImageSize());
×
244
    }
245

246
    _last_processed_frame = _last_loaded_frame;
1✔
247
}
1✔
248

249
void MediaData::_convertTo8Bit(MediaStorage::ImageData32 const& source, MediaStorage::ImageData8& target) const {
×
250
    target.resize(source.size());
×
251
    for (size_t i = 0; i < source.size(); ++i) {
×
252
        // Clamp to [0, 255] range and convert to uint8_t
253
        float clamped = std::max(0.0f, std::min(255.0f, source[i]));
×
254
        target[i] = static_cast<uint8_t>(clamped);
×
255
    }
256
}
×
257

258
void MediaData::_convertTo32Bit(MediaStorage::ImageData8 const& source, MediaStorage::ImageData32& target) const {
×
259
    target.resize(source.size());
×
260
    for (size_t i = 0; i < source.size(); ++i) {
×
261
        target[i] = static_cast<float>(source[i]);
×
262
    }
263
}
×
264

265
void MediaData::_resizeDataStorage() {
2✔
266
    int const new_size = _height * _width * _display_format_bytes;
2✔
267
    
268
    // Only resize if size or bit depth has actually changed
269
    bool needs_resize = false;
2✔
270
    
271
    if (_bit_depth == BitDepth::Bit8) {
2✔
272
        if (!MediaStorage::is8Bit(_rawData) || std::get<MediaStorage::ImageData8>(_rawData).size() != static_cast<size_t>(new_size)) {
2✔
273
            _rawData = MediaStorage::ImageData8(static_cast<size_t>(new_size));
6✔
274
            needs_resize = true;
2✔
275
        }
276
        if (!MediaStorage::is8Bit(_processedData) || std::get<MediaStorage::ImageData8>(_processedData).size() != static_cast<size_t>(new_size)) {
2✔
277
            _processedData = MediaStorage::ImageData8(static_cast<size_t>(new_size));
6✔
278
            needs_resize = true;
2✔
279
        }
280
    } else {
281
        if (!MediaStorage::is32Bit(_rawData) || std::get<MediaStorage::ImageData32>(_rawData).size() != static_cast<size_t>(new_size)) {
×
282
            _rawData = MediaStorage::ImageData32(static_cast<size_t>(new_size));
×
283
            needs_resize = true;
×
284
        }
285
        if (!MediaStorage::is32Bit(_processedData) || std::get<MediaStorage::ImageData32>(_processedData).size() != static_cast<size_t>(new_size)) {
×
286
            _processedData = MediaStorage::ImageData32(static_cast<size_t>(new_size));
×
287
            needs_resize = true;
×
288
        }
289
    }
290
    
291
    // Only resize temp buffers if something actually changed
292
    if (needs_resize) {
2✔
293
        _temp_8bit_buffer.resize(static_cast<size_t>(new_size));
2✔
294
        _temp_32bit_buffer.resize(static_cast<size_t>(new_size));
2✔
295
    }
296
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc