LedLayout.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "LedLayout.h"
  2. #include <algorithm>
  3. namespace WallpaperEngine::Led {
  4. std::vector<LedPixel> layoutDevice (const OpenRgb::Device& device) {
  5. std::vector<LedPixel> layout (device.ledCount);
  6. uint32_t rows = 0;
  7. for (const auto& zone : device.zones) {
  8. rows += zone.matrix.empty () ? 1 : std::max (zone.matrixHeight, 1u);
  9. }
  10. uint32_t firstLed = 0;
  11. uint32_t firstRow = 0;
  12. for (const auto& zone : device.zones) {
  13. const uint32_t height = zone.matrix.empty () ? 1 : std::max (zone.matrixHeight, 1u);
  14. const auto place = [&] (const uint32_t led, const uint32_t column, const uint32_t row, const uint32_t columns) {
  15. if (led >= zone.leds || firstLed + led >= layout.size () || columns == 0) {
  16. return;
  17. }
  18. const double xn = (column + 0.5) / columns;
  19. const double yn = (firstRow + row + 0.5) / rows;
  20. layout[firstLed + led] = {
  21. .x = std::min (static_cast<int> (kFrameWidth * xn), kFrameWidth - 1),
  22. .y = std::min (static_cast<int> (kFrameHeight * yn), kFrameHeight - 1),
  23. };
  24. };
  25. if (zone.matrix.empty ()) {
  26. for (uint32_t led = 0; led < zone.leds; led++) {
  27. place (led, led, 0, zone.leds);
  28. }
  29. } else {
  30. for (uint32_t row = 0; row < zone.matrixHeight; row++) {
  31. for (uint32_t column = 0; column < zone.matrixWidth; column++) {
  32. place (zone.matrix[row * zone.matrixWidth + column], column, row, zone.matrixWidth);
  33. }
  34. }
  35. }
  36. firstLed += zone.leds;
  37. firstRow += height;
  38. }
  39. return layout;
  40. }
  41. void applyBrightness (const std::span<const uint8_t> in, const std::span<uint8_t> out, const Brightness& brightness) {
  42. const size_t pixels = std::min (in.size (), out.size ()) / 3;
  43. for (size_t i = 0; i < pixels; i++) {
  44. const uint8_t* source = in.data () + i * 3;
  45. uint8_t* target = out.data () + i * 3;
  46. float scale = brightness.factor;
  47. if (brightness.boost) {
  48. const int strongest = std::max ({ source[0], source[1], source[2] });
  49. if (strongest == 0) {
  50. target[0] = target[1] = target[2] = 0;
  51. continue;
  52. }
  53. scale *= 255.0f / static_cast<float> (strongest);
  54. }
  55. for (int channel = 0; channel < 3; channel++) {
  56. target[channel] = static_cast<uint8_t> (
  57. std::clamp (static_cast<int> (static_cast<float> (source[channel]) * scale), 0, 255)
  58. );
  59. }
  60. }
  61. }
  62. void sampleDevice (
  63. const std::span<const uint8_t> frame, const std::span<const LedPixel> layout, std::vector<uint8_t>& out
  64. ) {
  65. out.resize (layout.size () * 3);
  66. for (size_t i = 0; i < layout.size (); i++) {
  67. const size_t at = (static_cast<size_t> (layout[i].y) * kFrameWidth + layout[i].x) * 3;
  68. out[i * 3] = frame[at];
  69. out[i * 3 + 1] = frame[at + 1];
  70. out[i * 3 + 2] = frame[at + 2];
  71. }
  72. }
  73. }