App
TBrushGenerator.cpp
Go to the documentation of this file.
1 
6 #include "TBrushGenerator.hpp"
7 
8 namespace app {
9 
10 template TBitmap<px_1x8bit, px_1x8bit> * TBrushGenerator::createRoundBrush(unsigned size, double hardness);
11 template int TBrushGenerator::createRoundBrushIn(TBitmap<px_1x8bit, px_1x8bit> &dst, unsigned size, double hardness);
12 
13 template <typename TPIXEL, typename TCHANNEL>
15  TBitmap<TPIXEL, TCHANNEL> * retVal = new TBitmap<TPIXEL, TCHANNEL>(size, size, MONO, 0);
16  if (retVal == NULL) {
17  return NULL;
18  }
19  if (retVal->getData() == NULL) {
20  delete retVal;
21  return NULL;
22  }
23  if (retVal->chCount != 1) {
24  delete retVal;
25  return NULL;
26  }
27  TPIXEL maxVal = retVal->maxVal;
28  if (size == 1) {
29  retVal->fill(retVal->getRect(), &maxVal);
30  return retVal;
31  }
32  double r = size / 2.0;
33  double rFull = r * hardness;
34  double r2 = r * r;
35  double rFull2 = rFull * rFull;
36  double factor = maxVal / (r - rFull);
37  double minX = -r + 0.5; // Ptz zjistujeme, jestli stred lezi v dane kruznici
38  double minY = -r + 0.5;
39  double x = minX;
40  double y = minY;
41  PointerArea area = retVal->getPointerArea();
42 
43  TPIXEL * ptr = (TPIXEL *) area.getAreaBgn();
44  TPIXEL * ptrRowEnd = (TPIXEL *) area.getAreaRowEnd();
45  TPIXEL * ptrEnd = (TPIXEL *) area.getAreaEnd();
46  unsigned ptrWidth = area.getOrigWidth();
47 
48  while (ptr < ptrEnd) {
49  x = minX;
50  while(ptr < ptrRowEnd) {
51  double rr2 = x * x + y * y;
52  if (rr2 < rFull2) {
53  *ptr = maxVal;
54  } else if (rr2 < r2) {
55  *ptr = factor * (r - sqrt(rr2)) + 0.5;
56  } else {
57  *ptr = 0;
58  }
59  ptr++;
60  x += 1;
61  }
62  y += 1;
63  ptrRowEnd += ptrWidth;
64  }
65  return retVal;
66 }
67 
68 template <typename TPIXEL, typename TCHANNEL>
69 int TBrushGenerator::createRoundBrushIn(TBitmap<TPIXEL, TCHANNEL> &dst, unsigned size, double hardness) {
70  TBitmap<TPIXEL, TCHANNEL> * tmp = createRoundBrush<TPIXEL, TCHANNEL>(size, hardness);
71  if (tmp == NULL) {
72  return 1;
73  }
74  // Bez přetypování je pproblém s přetečením proměnných, výsledný Point má pak souřadnice 245xxxxxxxxx velké číslo místo záporného
75  return tmp->copyTo(dst, tmp->getRect(), Point(((int)dst.getWidth() - (int)tmp->getWidth()) / 2, ((int)dst.getHeight() - (int)tmp->getHeight()) / 2));
76 }
77 } /* namespace app */