File: maskedheatmap.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (261 lines) | stat: -rw-r--r-- 9,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "maskedheatmap.h"

#include "../algorithms/thresholdconfig.h"

#include <boost/algorithm/string.hpp>

#include <fstream>

using algorithms::ThresholdConfig;

MaskedHeatMap::MaskedHeatMap()
    : _showOriginalMask(true),
      _showAlternativeMask(true),
      _highlighting(false),
      _highlightConfig(new ThresholdConfig()),
      _manualXAxisDescription(false),
      _manualYAxisDescription(false),
      _manualZAxisDescription(false) {
  _highlightConfig->InitializeLengthsSingleSample();
  SetCairoFilter(Cairo::FILTER_NEAREST);
  SignalDrawImage().connect(
      [&](const Cairo::RefPtr<Cairo::ImageSurface>& surface) {
        signalDrawImage(surface);
      });
}

MaskedHeatMap::~MaskedHeatMap() {}

void MaskedHeatMap::Clear() {
  _originalMask.reset();
  _alternativeMask.reset();
  _segmentedImage.reset();
  _highlightConfig.reset(new ThresholdConfig());
  _highlightConfig->InitializeLengthsSingleSample();
  _metaData.reset();
  HeatMap::Clear();
}

Mask2DCPtr MaskedHeatMap::GetActiveMask() const {
  if (!HasImage())
    throw std::runtime_error("GetActiveMask() called without image");
  const bool originalActive = _showOriginalMask && _originalMask;
  const bool altActive = _showAlternativeMask && _alternativeMask;
  if (originalActive) {
    if (altActive) {
      Mask2DPtr mask = Mask2D::MakePtr(*_originalMask);
      mask->Join(*_alternativeMask);
      return mask;
    } else {
      return _originalMask;
    }
  } else {
    if (altActive)
      return _alternativeMask;
    else
      return Mask2D::CreateSetMaskPtr<false>(Image().Width(), Image().Height());
  }
}

TimeFrequencyMetaDataCPtr MaskedHeatMap::GetSelectedMetaData() const {
  TimeFrequencyMetaDataCPtr metaData = _metaData;

  if (metaData) {
    if (XZoomStart() != 0 && metaData->HasObservationTimes()) {
      size_t startTime = std::round(XZoomStart() * Image().Width());
      TimeFrequencyMetaData* newData = new TimeFrequencyMetaData(*metaData);
      metaData = TimeFrequencyMetaDataCPtr(newData);
      std::vector<double> obsTimes = newData->ObservationTimes();
      obsTimes.erase(obsTimes.begin(), obsTimes.begin() + startTime);
      newData->SetObservationTimes(obsTimes);
    }
    if (YZoomStart() != 0 && metaData->HasBand()) {
      size_t startChannel = std::round(YZoomStart() * Image().Height());
      TimeFrequencyMetaData* newData = new TimeFrequencyMetaData(*metaData);
      metaData = TimeFrequencyMetaDataCPtr(newData);
      BandInfo band = newData->Band();
      band.channels.erase(band.channels.begin(),
                          band.channels.begin() + startChannel);
      newData->SetBand(band);
    }
  }
  return metaData;
}

void MaskedHeatMap::SetMetaData(const TimeFrequencyMetaDataCPtr& metaData) {
  _metaData = metaData;

  if (_metaData) {
    if (_metaData->HasObservationTimes()) {
      SetXAxisMin(_metaData->ObservationTimes().front());
      SetXAxisMax(_metaData->ObservationTimes().back());
      SetXAxisDescription("Time (UTC, hh:mm:ss)");
    }

    if (_metaData->HasBand() && !_metaData->Band().channels.empty()) {
      SetYAxisMin(_metaData->Band().channels.front().frequencyHz * 1e-6);
      SetYAxisMax(_metaData->Band().channels.back().frequencyHz * 1e-6);
      SetYAxisDescription("Frequency (MHz)");
    }

    if (!_manualZAxisDescription && !_metaData->ValueDescription().empty()) {
      std::string description = _metaData->ValueDescription();
      if (!_metaData->ValueUnits().empty())
        description = description + " (" + _metaData->ValueUnits() + ")";
      SetZAxisDescription(description);
    }
  }
}

void MaskedHeatMap::signalDrawImage(
    const Cairo::RefPtr<Cairo::ImageSurface>& surface) const {
  Mask2DCPtr mask = GetActiveMask();
  Mask2DCPtr originalMask = _originalMask;
  Mask2DCPtr alternativeMask = _alternativeMask;
  Mask2DPtr highlightMask;
  if (_highlighting) {
    highlightMask =
        Mask2D::CreateSetMaskPtr<false>(Image().Width(), Image().Height());
    _highlightConfig->Execute(GetImage2D().get(), highlightMask.get(), true,
                              10.0, 10.0);
  }

  const size_t xFactor = ImageToSurfaceXFactor();
  if (xFactor > 1) {
    mask = Mask2D::MakePtr(mask->ShrinkHorizontally(xFactor));
    if (originalMask) {
      originalMask = Mask2D::MakePtr(originalMask->ShrinkHorizontally(xFactor));
    }
    if (alternativeMask) {
      alternativeMask =
          Mask2D::MakePtr(alternativeMask->ShrinkHorizontally(xFactor));
    }
    if (highlightMask) {
      highlightMask =
          Mask2D::MakePtr(highlightMask->ShrinkHorizontally(xFactor));
    }
  }

  const size_t yFactor = ImageToSurfaceYFactor();
  if (yFactor > 1) {
    mask = Mask2D::MakePtr(mask->ShrinkVertically(yFactor));
    if (originalMask) {
      originalMask = Mask2D::MakePtr(originalMask->ShrinkVertically(yFactor));
    }
    if (alternativeMask) {
      alternativeMask =
          Mask2D::MakePtr(alternativeMask->ShrinkVertically(yFactor));
    }
    if (highlightMask) {
      highlightMask = Mask2D::MakePtr(highlightMask->ShrinkVertically(yFactor));
    }
  }

  const bool originalActive = _showOriginalMask && originalMask;
  const bool altActive = _showAlternativeMask && alternativeMask;
  int orMaskR, orMaskG, orMaskB;
  int altMaskR, altMaskG, altMaskB;
  if (GetColorMap() == ColorMap::Viridis) {
    orMaskR = 0;
    orMaskG = 0;
    orMaskB = 0;
    altMaskR = 255;
    altMaskG = 255;
    altMaskB = 255;
  } else {
    orMaskR = 255;
    orMaskG = 0;
    orMaskB = 255;
    altMaskR = 255;
    altMaskG = 255;
    altMaskB = 0;
  }

  unsigned char* data = surface->get_data();
  const size_t rowStride = surface->get_stride();

  const std::array<size_t, 2> imageXRange = ImageXRange();
  const std::array<size_t, 2> imageYRange = ImageYRange();
  const std::array<size_t, 2> surfaceXRange = {imageXRange[0] / xFactor,
                                               imageXRange[1] / xFactor};
  const std::array<size_t, 2> surfaceYRange = {imageYRange[0] / yFactor,
                                               imageYRange[1] / yFactor};

  if (_highlighting || originalActive || altActive) {
    for (size_t y = surfaceYRange[0]; y != surfaceYRange[1]; ++y) {
      guint8* rowpointer = data + rowStride * (surfaceYRange[1] - y - 1);
      for (size_t x = surfaceXRange[0]; x != surfaceXRange[1]; ++x) {
        const size_t xa = (x - surfaceXRange[0]) * 4;
        if (_highlighting && highlightMask->Value(x, y)) {
          rowpointer[xa + 0] = 0;
          rowpointer[xa + 1] = 0;
          rowpointer[xa + 2] = 255;
          rowpointer[xa + 3] = 255;
        } else if (originalActive && originalMask->Value(x, y)) {
          rowpointer[xa + 0] = orMaskB;
          rowpointer[xa + 1] = orMaskG;
          rowpointer[xa + 2] = orMaskR;
          rowpointer[xa + 3] = 255;
        } else if (altActive && alternativeMask->Value(x, y)) {
          rowpointer[xa + 0] = altMaskB;
          rowpointer[xa + 1] = altMaskG;
          rowpointer[xa + 2] = altMaskR;
          rowpointer[xa + 3] = 255;
        }
      }
    }
  }

  if (xFactor == 1 && yFactor == 1 && _segmentedImage != nullptr) {
    for (size_t y = imageYRange[0]; y < imageXRange[1]; ++y) {
      guint8* rowpointer = data + rowStride * (imageXRange[1] - y - 1);
      for (size_t x = imageXRange[0]; x < imageXRange[1]; ++x) {
        if (_segmentedImage->Value(x, y) != 0) {
          int xa = (x - imageXRange[0]) * 4;
          rowpointer[xa] = IntMap::R(_segmentedImage->Value(x, y));
          rowpointer[xa + 1] = IntMap::G(_segmentedImage->Value(x, y));
          rowpointer[xa + 2] = IntMap::B(_segmentedImage->Value(x, y));
          rowpointer[xa + 3] = IntMap::A(_segmentedImage->Value(x, y));
        }
      }
    }
  }
}

void MaskedHeatMap::SaveByExtension(const std::string& filename, size_t width,
                                    size_t height) {
  const char* eMsg =
      "Saving image to file failed: could not determine file type from "
      "filename extension -- maybe the type is not supported. Supported types "
      "are .png, .svg or .pdf.";
  if (filename.size() < 4) throw std::runtime_error(eMsg);
  std::string ext = filename.substr(filename.size() - 4);
  boost::to_lower<std::string>(ext);
  if (ext == ".png")
    SavePng(filename, width, height);
  else if (ext == ".svg")
    SaveSvg(filename, width, height);
  else if (ext == ".pdf")
    SavePdf(filename, width, height);
  else
    throw std::runtime_error(eMsg);
}

void MaskedHeatMap::SaveText(const std::string& filename) const {
  if (HasImage()) {
    const Image2DCPtr image = GetImage2D();
    const size_t startX = (size_t)std::round(XZoomStart() * image->Width());
    const size_t startY = (size_t)std::round(YZoomStart() * image->Height());
    const size_t endX = (size_t)std::round(XZoomEnd() * image->Width());
    const size_t endY = (size_t)std::round(YZoomEnd() * image->Height());
    const size_t imageWidth = endX - startX;
    const size_t imageHeight = endY - startY;
    std::ofstream file(filename.c_str());
    file << imageWidth << '\n' << imageHeight << '\n';
    for (size_t y = startY; y != endY; ++y) {
      for (size_t x = startX; x != endX; ++x) {
        file << image->Value(x, y) << '\n';
      }
    }
  }
}