diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-03-24 19:03:05 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-03-24 19:03:05 +0100 |
commit | b2c09e5625f2811b22d6448be9f228d4f530960f (patch) | |
tree | 4f994c95cfcfb875ac81535197107fa9372913b0 /plugingui | |
parent | 72544c3aa7005e6f0e60a505923b6afae4eba84d (diff) |
Antialiased linedrawing endpoint fixes. New drawImageStretched method. Slightly modified box render model.
Diffstat (limited to 'plugingui')
-rw-r--r-- | plugingui/painter.cc | 119 | ||||
-rw-r--r-- | plugingui/painter.h | 3 |
2 files changed, 59 insertions, 63 deletions
diff --git a/plugingui/painter.cc b/plugingui/painter.cc index 7d5499f..68e91d9 100644 --- a/plugingui/painter.cc +++ b/plugingui/painter.cc @@ -53,8 +53,9 @@ void GUI::Painter::plot(int x, int y, double c) { // plot the pixel at (x, y) with brightness c (where 0 ≤ c ≤ 1) - //c += 0.5; - //if(c > 1) c = 1; + //c += 0.2; + //if(c > 0.6) c = 1; + //if(c < 0.4) c = 0; pixbuf->addPixel(x, y, (unsigned char)(colour.red * 255.0), @@ -110,13 +111,17 @@ void GUI::Painter::drawLine(int x0, int y0, int x1, int y1) double xgap = rfpart(x0 + 0.5); double xpxl1 = xend; //this will be used in the main loop double ypxl1 = ipart(yend); + if(steep) { - plot(ypxl1, xpxl1, rfpart(yend) * xgap); - plot(ypxl1+1, xpxl1, fpart(yend) * xgap); + plot(ypxl1, xpxl1, 1); + //plot(ypxl1, xpxl1, rfpart(yend) * xgap); + //plot(ypxl1+1, xpxl1, fpart(yend) * xgap); } else { - plot(xpxl1, ypxl1 , rfpart(yend) * xgap); - plot(xpxl1, ypxl1+1, fpart(yend) * xgap); + plot(xpxl1, ypxl1, 1); + //plot(xpxl1, ypxl1 , rfpart(yend) * xgap); + //plot(xpxl1, ypxl1+1, fpart(yend) * xgap); } + double intery = yend + gradient; // first y-intersection for the main loop // handle second endpoint @@ -126,14 +131,17 @@ void GUI::Painter::drawLine(int x0, int y0, int x1, int y1) xgap = fpart(x1 + 0.5); double xpxl2 = xend; //this will be used in the main loop double ypxl2 = ipart(yend); + if(steep) { - plot(ypxl2 , xpxl2, rfpart(yend) * xgap); - plot(ypxl2+1, xpxl2, fpart(yend) * xgap); + plot(ypxl2, xpxl2, 1); + //plot(ypxl2 , xpxl2, rfpart(yend) * xgap); + //plot(ypxl2+1, xpxl2, fpart(yend) * xgap); } else { - plot(xpxl2, ypxl2, rfpart(yend) * xgap); - plot(xpxl2, ypxl2+1, fpart(yend) * xgap); + plot(xpxl2, ypxl2, 1); + //plot(xpxl2, ypxl2, rfpart(yend) * xgap); + //plot(xpxl2, ypxl2+1, fpart(yend) * xgap); } - + // main loop for(int x = xpxl1 + 1; x <= xpxl2 - 1; x++) { if(steep) { @@ -183,10 +191,10 @@ void GUI::Painter::drawLine(int x0, int y0, int x1, int y1) void GUI::Painter::drawRectangle(int x1, int y1, int x2, int y2) { - drawLine(x1, y1, x2, y1); - drawLine(x2, y1, x2, y2); - drawLine(x1, y2, x2, y2); - drawLine(x1, y1, x1, y2); + drawLine(x1, y1, x2 - 1, y1); + drawLine(x2, y1, x2, y2 - 1); + drawLine(x1 + 1, y2, x2, y2); + drawLine(x1, y1 + 1, x1, y2); } void GUI::Painter::drawFilledRectangle(int x1, int y1, int x2, int y2) @@ -396,70 +404,56 @@ void GUI::Painter::drawImage(int x0, int y0, GUI::Image *image) } } -/* - typedef struct { - Image *topleft; - Image *top; - Image *topright; - Image *left; - Image *right; - Image *bottomleft; - Image *bottom; - Image *bottomright; - } Box; -*/ +void GUI::Painter::drawImageStretched(int x0, int y0, GUI::Image *image, + size_t w, size_t h) +{ + float fw = image->width(); + float fh = image->height(); + + for(size_t x = 0; x < w; x++) { + for(size_t y = 0; y < h; y++) { + int lx = ((float)x/(float)w)*fw; + int ly = ((float)y/(float)h)*fh; + GUI::Colour c = image->getPixel(lx, ly); + pixbuf->setPixel(x0 + x, y0 + y, c.red, c.green, c.blue, c.alpha); + } + } +} + void GUI::Painter::drawBox(int x, int y, Box *box, int width, int height) { int dx = x; int dy = y; - printf("%d %d %d %d %d %d %d %d\n", - box->topLeft->width(), - box->top->width(), - box->topRight->width(), - box->left->width(), - box->right->width(), - box->bottomLeft->width(), - box->bottom->width(), - box->bottomRight->width()); - - printf("%d %d %d %d %d %d %d %d\n", - box->topLeft->height(), - box->top->height(), - box->topRight->height(), - box->left->height(), - box->right->height(), - box->bottomLeft->height(), - box->bottom->height(), - box->bottomRight->height()); - // Top: drawImage(dx, dy, box->topLeft); dx += box->topLeft->width() - 1; - while(dx < (x + width - box->topRight->width())) { - drawImage(dx, dy, box->top); - dx += box->top->width(); - } + drawImageStretched(dx, dy, box->top, + width - box->topRight->width(), + box->top->height()); dx = x + width - box->topRight->width(); drawImage(dx, dy, box->topRight); + // Center + dy = y + box->topLeft->height(); + dx = x + box->left->width(); + drawImageStretched(dx, dy, box->center, + width - box->left->width() - box->right->width(), + height - box->topLeft->height() - box->bottomLeft->height()); + // Mid: dx = x; dy = y + box->topLeft->height(); - while(dy < (y + height - box->bottomLeft->height())) { - drawImage(dx, dy, box->left); - dy += box->left->height(); - } + drawImageStretched(dx, dy, box->left, box->left->width(), + height - box->bottomLeft->height()); dx = x + width - box->right->width(); dy = y + box->topRight->height(); - while(dy < (y + height - box->bottomRight->height())) { - drawImage(dx, dy, box->right); - dy += box->right->height(); - } + drawImageStretched(dx, dy, box->right, box->right->width(), + height - box->bottomRight->height()); // Bottom: dx = x; @@ -467,10 +461,9 @@ void GUI::Painter::drawBox(int x, int y, Box *box, int width, int height) drawImage(dx, dy, box->bottomLeft); dx += box->bottomLeft->width() - 1; - while(dx < (x + width - box->bottomRight->width())) { - drawImage(dx, dy, box->bottom); - dx += box->bottom->width(); - } + drawImageStretched(dx, dy, box->bottom, + width - box->bottomRight->width(), + box->bottom->height()); dx = x + width - box->bottomRight->width(); drawImage(dx, dy, box->bottomRight); diff --git a/plugingui/painter.h b/plugingui/painter.h index 4341fc3..02b8aba 100644 --- a/plugingui/painter.h +++ b/plugingui/painter.h @@ -69,6 +69,8 @@ public: void drawFilledCircle(int x, int y, int r); void drawImage(int x, int y, struct __img__ * img); void drawImage(int x, int y, Image *image); + void drawImageStretched(int x0, int y0, GUI::Image *image, + size_t width, size_t height); typedef struct { Image *topLeft; @@ -79,6 +81,7 @@ public: Image *bottomLeft; Image *bottom; Image *bottomRight; + Image *center; } Box; void drawBox(int x, int y, Box *box, int width, int height); |