MythTV  master
synaesthesia.cpp
Go to the documentation of this file.
1 // Copyright (c) 2000-2001 Brad Hughes <bhughes@trolltech.com>
2 //
3 // Use, modification and distribution is allowed without limitation,
4 // warranty, or liability of any kind.
5 //
6 // modified 12-2004 by Kyle Schlansker to add 64 bit support
7 //
8 
9 #include "config.h"
10 
11 // C++
12 #include <cinttypes>
13 #include <cmath>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <iostream>
17 using namespace std;
18 
19 // Qt
20 #include <QCoreApplication>
21 #include <QPainter>
22 #include <QImage>
23 
24 // MythTV
25 #include <compat.h>
26 #include <mythlogging.h>
27 
28 // MythMusic
29 #include "mainvisual.h"
30 #include "synaesthesia.h"
31 
33 {
34  m_fps = 29;
35 
36  coreInit(); // init cosTable, negSinTable, bitReverse
37  setStarSize(m_starSize); // init scaleDown, maxStarRadius
38  setupPalette(); // init palette
39 }
40 
42 {
43  delete m_outputImage;
44 }
45 
47 {
48 #define sBOUND(x) ((x) > 255 ? 255 : (x))
49 #define sPEAKIFY(x) int(sBOUND((x) - (x)*(255-(x))/255/2))
50 #define sMAX(x,y) ((x) > (y) ? (x) : (y))
51  double fgRed = m_fgRedSlider;
52  double fgGreen = m_fgGreenSlider;
53  double fgBlue = 1.0 - sMAX(m_fgRedSlider,m_fgGreenSlider);
54  //double scale = sMAX(sMAX(fgRed,fgGreen),fgBlue);
55  double scale = (fgRed + fgGreen + fgBlue) / 2.0;
56  fgRed /= scale;
57  fgGreen /= scale;
58  fgBlue /= scale;
59 
60  double bgRed = m_bgRedSlider;
61  double bgGreen = m_bgGreenSlider;
62  double bgBlue = 1.0 - sMAX(m_bgRedSlider,m_bgGreenSlider);
63  //scale = sMAX(sMAX(bgRed,bgGreen),bgBlue);
64  scale = (bgRed + bgGreen + bgBlue) / 2.0;
65  bgRed /= scale;
66  bgGreen /= scale;
67  bgBlue /= scale;
68 
69  for (int i = 0; i < 256; i++) {
70  int f = i & 15;
71  int b = i / 16;
72  //palette[i * 3 + 0] = sPEAKIFY(b*bgRed*16+f*fgRed*16);
73  //palette[i * 3 + 1] = sPEAKIFY(b*bgGreen*16+f*fgGreen*16);
74  //palette[i * 3 + 2] = sPEAKIFY(b*bgBlue*16+f*fgBlue*16);
75 
76  double red = b * bgRed * 16 + f * fgRed * 16;
77  double green = b * bgGreen * 16 + f * fgGreen * 16;
78  double blue = b * bgBlue * 16 + f * fgBlue * 16;
79 
80  double excess = 0.0;
81  for (int j = 0; j < 5; j++)
82  {
83  red += excess / 3;
84  green += excess / 3;
85  blue += excess / 3;
86  excess = 0.0;
87 
88 
89  if (red > 255) { excess += red - 255; red = 255; }
90  if (green > 255) { excess += green - 255; green = 255; }
91  if (blue > 255) { excess += blue - 255; blue = 255; }
92  }
93 
94  double scale2 = (0.5 + (red + green + blue) / 768.0) / 1.5;
95  red *= scale2;
96  green *= scale2;
97  blue *= scale2;
98 
99  m_palette[i * 3 + 0] = sBOUND(int(red));
100  m_palette[i * 3 + 1] = sBOUND(int(green));
101  m_palette[i * 3 + 2] = sBOUND(int(blue));
102  }
103 }
104 
105 void Synaesthesia::resize(const QSize &newsize)
106 {
107  m_size = newsize;
108 
109  m_size.setHeight(m_size.height() / 2);
110  m_size.setWidth((m_size.width() / 4) * 4);
111  m_outputBmp.size(m_size.width(), m_size.height());
112  m_lastOutputBmp.size(m_size.width(), m_size.height());
113  m_lastLastOutputBmp.size(m_size.width(), m_size.height());
114  m_outWidth = m_size.width();
115  m_outHeight = m_size.height();
116 
117  delete m_outputImage;
118 
119  m_size.setHeight(m_size.height() * 2);
120  m_outputImage = new QImage(m_size, QImage::Format_Indexed8);
121 
122  if (!m_outputImage)
123  {
124  LOG(VB_GENERAL, LOG_ERR,
125  "outputImage in Synaesthesia::resize() is NULL");
126  return;
127  }
128 
129  for (int i = 0; i < 256; i++)
130  m_outputImage->setColor(i, qRgba(m_palette[i * 3], m_palette[i * 3 + 1],
131  m_palette[i * 3 + 2], 255));
132 
133 #if 0
134  surface = SDL_SetVideoMode(size.width(), size.height(), 8, 0);
135 
136  if (!surface)
137  {
138  LOG(VB_GENERAL, LOG_ERR, "Couldn't get SDL surface");
139  return;
140  }
141 
142  SDL_Color sdlPalette[256];
143 
144  for (int i = 0; i < 256; i++)
145  {
146  sdlPalette[i].r = m_palette[i * 3];
147  sdlPalette[i].g = m_palette[i * 3 + 1];
148  sdlPalette[i].b = m_palette[i * 3 + 2];
149  }
150 
151  SDL_SetColors(surface, sdlPalette, 0, 256);
152 #endif
153 }
154 
156 {
157  int sum = 0;
158  for (int j = 0; j < LogSize; j++)
159  {
160  sum = (i & 1) + sum * 2;
161  i >>= 1;
162  }
163 
164  return sum;
165 }
166 
167 void Synaesthesia::fft(double *x, double *y)
168 {
169  int n2 = NumSamples;
170  for (int twoToTheK = 1; twoToTheK < NumSamples; twoToTheK *= 2)
171  {
172  int n1 = n2;
173  n2 /= 2;
174  for (int j = 0; j < n2; j++)
175  {
176  double c = m_cosTable[j * twoToTheK & (NumSamples - 1)];
177  double s = m_negSinTable[j * twoToTheK & (NumSamples - 1)];
178  for (int i = j; i < NumSamples; i += n1)
179  {
180  int l = i + n2;
181  double xt = x[i] - x[l];
182  x[i] = (x[i] + x[l]);
183  double yt = y[i] - y[l];
184  y[i] = (y[i] + y[l]);
185  x[l] = xt * c - yt * s;
186  y[l] = xt * s + yt * c;
187  }
188  }
189  }
190 }
191 
192 void Synaesthesia::setStarSize(double lsize)
193 {
194  double fadeModeFudge = (m_fadeMode == Wave ? 0.4 :
195  (m_fadeMode == Flame ? 0.6 : 0.78));
196 
197  int factor = 0;
198  if (lsize > 0.0)
199  factor = int(exp(log(fadeModeFudge) / (lsize * 8.0)) * 255);
200  if (factor > 255)
201  factor = 255;
202 
203  for (int i = 0; i < 256; i++)
204  m_scaleDown[i] = i * factor>>8;
205 
206  m_maxStarRadius = 1;
207  for (int i = 255; i; i = m_scaleDown[i])
208  m_maxStarRadius++;
209 }
210 
212 {
213  for (int i = 0; i < NumSamples; i++)
214  {
215  m_negSinTable[i] = -sin(3.141592 * 2.0 / NumSamples * i);
216  m_cosTable[i] = cos(3.141592 * 2.0 / NumSamples * i);
217  m_bitReverse[i] = bitReverser(i);
218  }
219 }
220 
221 #define output ((unsigned char*)m_outputBmp.data)
222 #define lastOutput ((unsigned char*)m_lastOutputBmp.data)
223 #define lastLastOutput ((unsigned char*)m_lastLastOutputBmp.data)
224 
225 void Synaesthesia::addPixel(int x, int y, int br1, int br2)
226 {
227  if (x < 0 || x > m_outWidth || y < 0 || y >= m_outHeight)
228  return;
229 
230  unsigned char *p = output + x * 2 + y * m_outWidth * 2;
231  if (p[0] + br1 < 255)
232  p[0] += br1;
233  else
234  p[0] = 255;
235  if (p[1] + br2 < 255)
236  p[1] += br2;
237  else
238  p[1] = 255;
239 }
240 
241 void Synaesthesia::addPixelFast(unsigned char *p, int br1, int br2)
242 {
243  if (p[0] + br1 < 255)
244  p[0] += br1;
245  else
246  p[0] = 255;
247  if (p[1] + br2 < 255)
248  p[1] += br2;
249  else
250  p[1] = 255;
251 }
252 
253 unsigned char Synaesthesia::getPixel(int x, int y, int where)
254 {
255  if (x < 0 || y < 0 || x >= m_outWidth || y >= m_outHeight)
256  return 0;
257 
258  return lastOutput[where];
259 }
260 
262 {
263  auto *ptr = (uint32_t *)output;
264  int i = m_outWidth * m_outHeight * 2 / sizeof(uint32_t);
265  do {
266  uint32_t x = *ptr;
267  if (x)
268  {
269  *(ptr++) = x - ((x & (uintptr_t)0xf0f0f0f0) >> 4) -
270  ((x & (uintptr_t)0xe0e0e0e0) >> 5);
271  }
272  else
273  {
274  ptr++;
275  }
276  } while (--i > 0);
277 }
278 
279 void Synaesthesia::fadePixelWave(int x, int y, int where, int step)
280 {
281  short j = short((int(getPixel(x - 1, y, where - 2)) +
282  int(getPixel(x + 1, y, where + 2)) +
283  int(getPixel(x, y - 1, where - step)) +
284  int(getPixel(x, y + 1, where + step))) >> 2) +
285  lastOutput[where];
286 
287  if (!j)
288  {
289  output[where] = 0;
290  return;
291  }
292  j = j - lastLastOutput[where] - 1;
293  if (j < 0)
294  output[where] = 0;
295  else if (j & (255 * 256))
296  output[where] = 255;
297  else
298  output[where] = j;
299 }
300 
302 {
303  unsigned short *t = m_lastLastOutputBmp.data;
304  m_lastLastOutputBmp.data = m_lastOutputBmp.data;
305  m_lastOutputBmp.data = m_outputBmp.data;
306  m_outputBmp.data = t;
307 
308  int step = m_outWidth*2;
309  for (int x = 0, i = 0, j = m_outWidth * (m_outHeight - 1) * 2;
310  x < m_outWidth; x++, i += 2, j += 2)
311  {
312  fadePixelWave(x, 0, i, step);
313  fadePixelWave(x, 0, i + 1, step);
314  fadePixelWave(x, m_outHeight - 1, j, step);
315  fadePixelWave(x, m_outHeight - 1, j + 1, step);
316  }
317 
318  for (int y = 1, i = m_outWidth * 2, j = m_outWidth * 4 - 2;
319  y < m_outHeight; y++, i += step, j += step)
320  {
321  fadePixelWave(0, y, i, step);
322  fadePixelWave(0, y, i + 1, step);
323  fadePixelWave(m_outWidth - 1, y, j, step);
324  fadePixelWave(m_outWidth - 1, y, j + 1, step);
325  }
326 
327  for (int y = 1, start = m_outWidth * 2 + 2, end = m_outWidth * 4 - 2;
328  y < m_outHeight - 1; y++, start += step, end += step)
329  {
330  int i2 = start;
331  do
332  {
333  short j2 = short((int(lastOutput[i2 - 2]) +
334  int(lastOutput[i2 + 2]) +
335  int(lastOutput[i2 - step]) +
336  int(lastOutput[i2 + step])) >> 2) +
337  lastOutput[i2];
338  if (!j2)
339  {
340  output[i2] = 0;
341  }
342  else
343  {
344  j2 = j2 - lastLastOutput[i2] - 1;
345  if (j2 < 0)
346  output[i2] = 0;
347  else if (j2 & (255*256))
348  output[i2] = 255;
349  else
350  output[i2] = j2;
351  }
352  } while(++i2 < end);
353  }
354 }
355 
356 void Synaesthesia::fadePixelHeat(int x, int y, int where, int step)
357 {
358  short j = short((int(getPixel(x - 1, y, where - 2)) +
359  int(getPixel(x + 1, y, where + 2)) +
360  int(getPixel(x, y - 1, where - step)) +
361  int(getPixel(x, y + 1, where + step))) >> 2) +
362  lastOutput[where];
363  if (!j)
364  {
365  output[where] = 0;
366  return;
367  }
368  j = j -lastLastOutput[where] - 1;
369  if (j < 0)
370  output[where] = 0;
371  else if (j & (255 * 256))
372  output[where] = 255;
373  else
374  output[where] = j;
375 }
376 
378 {
379  unsigned short *t = m_lastLastOutputBmp.data;
380  m_lastLastOutputBmp.data = m_lastOutputBmp.data;
381  m_lastOutputBmp.data = m_outputBmp.data;
382  m_outputBmp.data = t;
383 
384  int step = m_outWidth * 2;
385  for (int x = 0, i = 0, j = m_outWidth * (m_outHeight - 1) * 2;
386  x < m_outWidth; x++, i += 2, j += 2)
387  {
388  fadePixelHeat(x, 0, i, step);
389  fadePixelHeat(x, 0, i + 1, step);
390  fadePixelHeat(x, m_outHeight - 1, j, step);
391  fadePixelHeat(x, m_outHeight - 1, j + 1, step);
392  }
393 
394  for(int y = 1, i = m_outWidth * 2, j = m_outWidth * 4 - 2; y < m_outHeight;
395  y++, i += step, j += step)
396  {
397  fadePixelHeat(0, y, i, step);
398  fadePixelHeat(0, y, i + 1, step);
399  fadePixelHeat(m_outWidth - 1, y, j, step);
400  fadePixelHeat(m_outWidth - 1, y, j + 1, step);
401  }
402 
403  for(int y = 1, start = m_outWidth * 2 + 2, end = m_outWidth * 4 - 2;
404  y < m_outHeight - 1; y++, start += step, end += step)
405  {
406  int i2 = start;
407  do
408  {
409  short j2 = short((int(lastOutput[i2 - 2]) +
410  int(lastOutput[i2 + 2]) +
411  int(lastOutput[i2 - step]) +
412  int(lastOutput[i2 + step])) >> 2) +
413  lastOutput[i2];
414  if (!j2)
415  output[i2] = 0;
416  else
417  {
418  j2 = j2 - lastLastOutput[i2] +
419  ((lastLastOutput[i2] - lastOutput[i2]) >> 2) - 1;
420  if (j2 < 0)
421  output[i2] = 0;
422  else if (j2 & (255*256))
423  output[i2] = 255;
424  else
425  output[i2] = j2;
426  }
427  } while(++i2 < end);
428  }
429 }
430 
432 {
433  switch(m_fadeMode)
434  {
435  case Stars: fadeFade(); break;
436  case Flame: fadeHeat(); break;
437  case Wave: fadeWave(); break;
438  default: break;
439  }
440 }
441 
443 {
444  fade();
445 
446  if (!node)
447  return false;
448 
449  double x[NumSamples];
450  double y[NumSamples];
451  double a[NumSamples];
452  double b[NumSamples];
453  int clarity[NumSamples];
454 
455  int brightFactor = int(Brightness * m_brightnessTwiddler / (m_starSize + 0.01));
456 
457  int numSamps = NumSamples;
458  if (node->m_length < NumSamples)
459  numSamps = node->m_length;
460 
461  memset(x, 0, sizeof(x));
462  memset(y, 0, sizeof(y));
463 
464  for (int i = 0; i < numSamps; i++)
465  {
466  x[i] = node->m_left[i];
467  if (node->m_right)
468  y[i] = node->m_right[i];
469  else
470  y[i] = x[i];
471  }
472 
473  fft(x, y);
474 
475  double energy = 0.0;
476 
477  for (int i = 0 + 1; i < NumSamples / 2; i++)
478  {
479  double x1 = x[m_bitReverse[i]];
480  double y1 = y[m_bitReverse[i]];
481  double x2 = x[m_bitReverse[NumSamples - i]];
482  double y2 = y[m_bitReverse[NumSamples - i]];
483  double aa = NAN;
484  double bb = NAN;
485  a[i] = sqrt(aa = (x1 + x2) * (x1 + x2) + (y1 - y2) * (y1 - y2));
486  b[i] = sqrt(bb = (x1 - x2) * (x1 - x2) + (y2 + y2) * (y1 + y2));
487  if (aa + bb != 0.0)
488  {
489  clarity[i] = (int)(((x1 + x2) * (x1 - x2) + (y1 + y2) * (y1 - y2)) /
490  (aa + bb) * 256);
491  }
492  else
493  {
494  clarity[i] = 0;
495  }
496 
497  energy += (aa + bb) * i * i;
498  }
499 
500  energy = sqrt(energy / NumSamples) / 65536.0;
501 
502  //int heightFactor = NumSamples / 2 / outHeight + 1;
503  //int actualHeight = NumSamples / 2 / heightFactor;
504  //int heightAdd = (outHeight + actualHeight) >> 1;
505 
506  double brightFactor2 = (brightFactor / 65536.0 / NumSamples) *
507  sqrt(m_outHeight * m_outWidth / (320.0 * 200.0));
508 
509  m_energyAvg = m_energyAvg * 0.95 + energy * 0.05;
510  if (m_energyAvg > 0.0)
511  brightFactor2 *= 80.0 / (m_energyAvg + 5.0);
512 
513  for (int i = 1; i < NumSamples / 2; i++)
514  {
515  if (a[i] > 0 || b[i] > 0)
516  {
517  int h = (int)(b[i] * m_outWidth / (a[i] + b[i]));
518  int br = (int)((a[i] + b[i]) * i * brightFactor2);
519  int br1 = br * (clarity[i] + 128) >> 8;
520  int br2 = br * (128 - clarity[i]) >> 8;
521  if (br1 < 0) br1 = 0; else if (br1 > 255) br1 = 255;
522  if (br2 < 0) br2 = 0; else if (br2 > 255) br2 = 255;
523 
524  int px = h;
525  int py = m_outHeight - i * m_outHeight / (NumSamples / 2);
526 
527  if (m_pointsAreDiamonds)
528  {
529  addPixel(px, py, br1, br2);
530  br1 = m_scaleDown[br1];
531  br2 = m_scaleDown[br2];
532 
533  for(int j = 1; br1 > 0 || br2 > 0;
534  j++, br1 = m_scaleDown[br1], br2 = m_scaleDown[br2])
535  {
536  for (int k = 0; k < j; k++)
537  {
538  addPixel(px - j + k,py - k, br1, br2);
539  addPixel(px + k, py - j + k, br1, br2);
540  addPixel(px + j - k, py + k, br1, br2);
541  addPixel(px - k, py + j - k, br1, br2);
542  }
543  }
544  }
545  else
546  {
547  if (px < m_maxStarRadius || py < m_maxStarRadius ||
548  px > m_outWidth - m_maxStarRadius ||
549  py > m_outHeight - m_maxStarRadius)
550  {
551  addPixel(px, py, br1, br2);
552  for(int j = 1; br1 > 0 || br2 > 0;
553  j++, br1 = m_scaleDown[br1], br2 = m_scaleDown[br2])
554  {
555  addPixel(px + j, py, br1, br2);
556  addPixel(px, py + j, br1, br2);
557  addPixel(px - j, py, br1, br2);
558  addPixel(px, py - j, br1, br2);
559  }
560  }
561  else
562  {
563  unsigned char *p = output + px * 2 + py * m_outWidth * 2;
564  unsigned char *p1 = p;
565  unsigned char *p2 = p;
566  unsigned char *p3 = p;
567  unsigned char *p4 = p;
568  addPixelFast(p, br1, br2);
569  for (; br1 > 0 || br2 > 0;
570  br1 = m_scaleDown[br1], br2 = m_scaleDown[br2])
571  {
572  p1 += 2;
573  addPixelFast(p1, br1, br2);
574  p2 -= 2;
575  addPixelFast(p2, br1, br2);
576  p3 += m_outWidth * 2;
577  addPixelFast(p3, br1, br2);
578  p4 -= m_outWidth*2;
579  addPixelFast(p4, br1, br2);
580  }
581  }
582  }
583  }
584  }
585 
586  return false;
587 }
588 
589 bool Synaesthesia::draw(QPainter *p, const QColor &back)
590 {
591  if (!m_outputImage)
592  return true;
593 
594  (void)back;
595 
596  auto *ptrOutput = (uint32_t *)output;
597 
598  for (int j = 0; j < m_outHeight * 2; j += 2)
599  {
600  auto *ptrTop = (uint32_t *)(m_outputImage->scanLine(j));
601  auto *ptrBot = (uint32_t *)(m_outputImage->scanLine(j+1));
602 
603  int i = m_outWidth / 4;
604 
605  do
606  {
607  unsigned int const r1 = *(ptrOutput++);
608  unsigned int const r2 = *(ptrOutput++);
609 
610  unsigned int const v = ((r1 & 0x000000f0UL) >> 4) |
611  ((r1 & 0x0000f000UL) >> 8) |
612  ((r1 & 0x00f00000UL) >> 12) |
613  ((r1 & 0xf0000000UL) >> 16);
614 
615  *(ptrTop++) = v | (((r2 & 0x000000f0UL) << 12) |
616  ((r2 & 0x0000f000UL) << 8) |
617  ((r2 & 0x00f00000UL) << 4) |
618  ((r2 & 0xf0000000UL)));
619 
620  *(ptrBot++) = v | (((r2 & 0x000000f0UL) << 12) |
621  ((r2 & 0x0000f000UL) << 8) |
622  ((r2 & 0x00f00000UL) << 4) |
623  ((r2 & 0xf0000000UL)));
624  } while (--i > 0);
625  }
626 
627  p->drawImage(0, 0, *m_outputImage);
628 
629  return true;
630 }
631 
632 static class SynaesthesiaFactory : public VisFactory
633 {
634  public:
635  const QString &name(void) const override // VisFactory
636  {
637  static QString s_name = QCoreApplication::translate("Visualizers",
638  "Synaesthesia");
639  return s_name;
640  }
641 
642  uint plugins(QStringList *list) const override // VisFactory
643  {
644  *list << name();
645  return 1;
646  }
647 
648  VisualBase *create(MainVisual *parent, const QString &pluginName) const override // VisFactory
649  {
650  (void)parent;
651  (void)pluginName;
652  return new Synaesthesia();
653  }
VisualBase * create(MainVisual *parent, const QString &pluginName) const override
void setupPalette(void)
void fadePixelWave(int x, int y, int where, int step)
#define sMAX(x, y)
void fade(void)
uint plugins(QStringList *list) const override
short * m_right
Definition: videovisual.h:35
#define NumSamples
Definition: synaesthesia.h:13
bool draw(QPainter *p, const QColor &back) override
unsigned char getPixel(int x, int y, int where)
static int x2
Definition: mythsocket.cpp:61
static void addPixelFast(unsigned char *p, int br1, int br2)
SynaesthesiaFactory SynaesthesiaFactory
#define Stars
Definition: synaesthesia.h:17
def log(debug, txt)
Definition: utilities.py:7
static int bitReverser(int i)
unsigned int uint
Definition: compat.h:140
#define LogSize
Definition: synaesthesia.h:11
#define sBOUND(x)
#define lastOutput
void fadeWave(void)
void coreInit(void)
void setStarSize(double lsize)
void addPixel(int x, int y, int br1, int br2)
void resize(const QSize &size) override
void fadeFade(void)
void fadeHeat(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
#define Wave
Definition: synaesthesia.h:16
void fadePixelHeat(int x, int y, int where, int step)
long m_length
Definition: videovisual.h:36
#define lastLastOutput
void fft(double *x, double *y)
static guint32 * p2
Definition: goom_core.c:35
~Synaesthesia() override
Synaesthesia(void)
static guint32 * back
Definition: goom_core.c:34
#define Flame
Definition: synaesthesia.h:15
const QString & name(void) const override
bool process(VisualNode *node) override
short * m_left
Definition: videovisual.h:34
static guint32 * p1
Definition: goom_core.c:35
#define output
static int x1
Definition: mythsocket.cpp:60