26 static bool CreateHeatmapImageFromData(
string imagePath, notnull array<int> imageData,
int colourMode = COLOUR_MODE_GREYSCALE,
bool invertColour =
false,
int maxValueMode = MAX_MODE_RAW,
bool highlightValuesAboveMax =
false,
int resolutionFactor = 1)
28 int imageDataCount = imageData.Count();
29 float resolution =
Math.Sqrt(imageDataCount);
30 if (resolution != (
int)resolution)
32 int idealResolution =
Math.Round(resolution);
33 if (idealResolution < 1)
36 PrintFormat(
"Invalid image data: resolution is not square (√%1 ≈ %2, expected a perfect int e.g %3×%3 = %4)", imageDataCount, resolution, idealResolution, idealResolution * idealResolution, level:
LogLevel.ERROR);
41 if (maxValueMode == MAX_MODE_2AVG)
43 Debug.BeginTimeMeasure();
47 foreach (
int value : imageData)
57 maxToUse = 2 * maxAvg / div;
59 Debug.EndTimeMeasure(
"Defining average");
62 if (maxValueMode == MAX_MODE_2MEDIAN)
64 Debug.BeginTimeMeasure();
66 array<int> sortedValues = {};
67 sortedValues.Reserve(resolution * resolution);
68 foreach (
int index,
int value : imageData)
71 sortedValues.Insert(value);
75 maxToUse = sortedValues[sortedValues.Count() * 0.5] * 2;
77 Debug.EndTimeMeasure(
"Defining median");
82 Debug.BeginTimeMeasure();
84 foreach (
int value : imageData)
90 Debug.EndTimeMeasure(
"Defining raw max");
97 Debug.BeginTimeMeasure();
102 foreach (
int index,
int value : imageData)
105 intensity01 = (
float)value / maxToUse;
109 if (colourMode == COLOUR_MODE_GREYSCALE)
111 iIntensity =
Math.Round(intensity01 * 255);
114 if (iIntensity > 255)
116 if (highlightValuesAboveMax)
132 iIntensity = 255 - iIntensity;
141 if (colourMode == COLOUR_MODE_THERMAL)
146 if (highlightValuesAboveMax)
152 if (intensity01 <= 0)
162 intensity01 = 1 - intensity01;
165 if (intensity01 < 0.25)
172 if (intensity01 < 0.5)
176 b = 1 + 4 * (0.25 - intensity01);
179 if (intensity01 < 0.75)
181 r = 4 * (intensity01 - 0.5);
188 g = 1 + 4 * (0.75 - intensity01);
193 | ((
int)(255 * r) << 16)
194 | ((
int)(255 * g) << 8)
201 iIntensity =
Math.Round(intensity01 * 255);
204 if (iIntensity > 255)
206 if (highlightValuesAboveMax)
222 colour = 0x00000000 | (iIntensity << 24);
224 colour = 0x00FFFFFF | (iIntensity << 24);
228 imageData[
index] = colour;
231 Debug.EndTimeMeasure(
string.Format(
"Setting pixel values for %1×%1 image", resolution, resolution));
233 if (resolutionFactor < 1)
234 resolutionFactor = 1;
236 while (resolution * resolutionFactor > MAX_RESOLUTION)
238 if (resolutionFactor < 2)
240 int newResolution = resolution;
241 while (newResolution > 1 && newResolution * resolutionFactor > MAX_RESOLUTION)
247 "Resolution %1 is greater than max allowed %2 resolution; using %3",
253 resolution = newResolution;
257 int newResolutionFactor = resolutionFactor;
258 while (newResolutionFactor > 1 && resolution * newResolutionFactor > MAX_RESOLUTION)
260 newResolutionFactor *= 0.5;
264 "Resolution %1 multiplied by %2 (%3) is greater than max allowed %4 resolution; changing multiplier to %5",
267 resolution * resolutionFactor,
272 resolutionFactor = newResolutionFactor;
276 if (resolutionFactor > 1)
278 Debug.BeginTimeMeasure();
279 if (SCR_ImageHelper.IncreaseResolution(imageData, resolution, resolution, resolutionFactor))
281 Debug.EndTimeMeasure(
string.Format(
"Increasing image side by a factor of %1 (from %2×%2 to %3×%3) - SUCCESS", resolutionFactor, resolution, resolution * resolutionFactor));
282 resolution *= resolutionFactor;
286 Debug.EndTimeMeasure(
string.Format(
"Increasing image side by a factor of %1 (from %2×%2 to %3×%3) - FAILURE", resolutionFactor, resolution, resolution * resolutionFactor));
290 Debug.BeginTimeMeasure();
291 bool result = SCR_ImageHelper.TEMP_SaveImageDataWrapper(imagePath, resolution, resolution, imageData);
292 Debug.EndTimeMeasure(
"Writing image " + imagePath);
302 array<int> multipliersRef = multipliers;
303 int multipliersCount;
305 multipliersCount = multipliersRef.Count();
307 if (multipliersCount < 1)
309 multipliersRef = { 1, 2, 4, 8 };
310 multipliersCount = 4;
313 if (multipliersCount > 0)
315 multipliersRef.Sort();
316 for (
int i = multipliersCount - 1; i >= 0; --i)
318 int multiplier = multipliersRef[i];
319 if (multiplier < 1 || multipliersRef.Find(multiplier) != i)
321 multipliersRef.Remove(i);
327 if (multipliersCount > 1)
328 multipliersRef.Sort();
331 foreach (
int i,
int multiplier : multipliersRef)
334 result +=
string.Format(
";%1,×%1,e.g 64×64 becomes %2×%2", multiplier, 64 * multiplier);
337 result +=
"1,1:1 resolution,1 data point = 1 pixel";
339 result +=
string.Format(
"%1,×%1,e.g 64×64 becomes %2×%2", multiplier, 64 * multiplier);