1 /** 2 * Drawing functions. 3 * 4 * License: 5 * This Source Code Form is subject to the terms of 6 * the Mozilla Public License, v. 2.0. If a copy of 7 * the MPL was not distributed with this file, You 8 * can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * Authors: 11 * Vladimir Panteleev <vladimir@thecybershadow.net> 12 */ 13 14 module ae.utils.graphics.draw; 15 16 import std.algorithm : sort; 17 import std.traits; 18 19 import ae.utils.geometry : TAU; 20 import ae.utils.graphics.view; 21 import ae.utils.math; 22 import ae.utils.meta : structFields, SignedBitsType, UnsignedBitsType; 23 24 version(unittest) import ae.utils.graphics.image; 25 26 // Constraints could be simpler if this was fixed: 27 // https://d.puremagic.com/issues/show_bug.cgi?id=12386 28 29 /// Get the pixel color at the specified coordinates, 30 /// or fall back to the specified default value if 31 /// the coordinates are out of bounds. 32 COLOR safeGet(V, COLOR)(auto ref V v, int x, int y, COLOR def) 33 if (isView!V && is(COLOR : ViewColor!V)) 34 { 35 if (x>=0 && y>=0 && x<v.w && y<v.h) 36 return v[x, y]; 37 else 38 return def; 39 } 40 41 unittest 42 { 43 auto v = onePixel(7); 44 assert(v.safeGet(0, 0, 0) == 7); 45 assert(v.safeGet(0, 1, 0) == 0); 46 } 47 48 /// Set the pixel color at the specified coordinates 49 /// if the coordinates are not out of bounds. 50 void safePut(V, COLOR)(auto ref V v, int x, int y, COLOR value) 51 if (isWritableView!V && is(COLOR : ViewColor!V)) 52 { 53 if (x>=0 && y>=0 && x<v.w && y<v.h) 54 v[x, y] = value; 55 } 56 57 unittest 58 { 59 auto v = Image!int(1, 1); 60 v.safePut(0, 0, 7); 61 v.safePut(0, 1, 9); 62 assert(v[0, 0] == 7); 63 } 64 65 /// Forwards to safePut or opIndex, depending on the 66 /// CHECKED parameter. Allows propagation of a 67 /// CHECKED parameter from other callers. 68 void putPixel(bool CHECKED, V, COLOR)(auto ref V v, int x, int y, COLOR value) 69 if (isWritableView!V && is(COLOR : ViewColor!V)) 70 { 71 static if (CHECKED) 72 v.safePut(x, y, value); 73 else 74 v[x, y] = value; 75 } 76 77 unittest 78 { 79 auto v = Image!int(1, 1); 80 v.putPixel!false(0, 0, 7); 81 v.putPixel!true(0, 1, 9); 82 assert(v[0, 0] == 7); 83 } 84 85 /// Gets a pixel's address from a direct view. 86 ViewColor!V* pixelPtr(V)(auto ref V v, int x, int y) 87 if (isDirectView!V) 88 { 89 return &v.scanline(y)[x]; 90 } 91 92 unittest 93 { 94 auto v = Image!int(1, 1); 95 v[0, 0] = 7; 96 auto p = v.pixelPtr(0, 0); 97 assert(*p == 7); 98 } 99 100 /// Fills a writable view with a solid color. 101 void fill(V, COLOR)(auto ref V v, COLOR c) 102 if (isWritableView!V 103 && is(COLOR : ViewColor!V)) 104 { 105 foreach (y; 0..v.h) 106 { 107 static if (isDirectView!V) 108 v.scanline(y)[] = c; 109 else 110 foreach (x; 0..v.w) 111 v[x, y] = c; 112 } 113 } 114 deprecated alias clear = fill; 115 116 unittest 117 { 118 auto i = onePixel(0).copy(); 119 i.fill(1); 120 assert(i[0, 0] == 1); 121 auto t = i.tile(10, 10); 122 t.fill(2); 123 assert(i[0, 0] == 2); 124 } 125 126 // *************************************************************************** 127 128 enum CheckHLine = 129 q{ 130 static if (CHECKED) 131 { 132 if (x1 >= v.w || x2 <= 0 || y < 0 || y >= v.h || x1 >= x2) return; 133 if (x1 < 0) x1 = 0; 134 if (x2 >= v.w) x2 = v.w; 135 } 136 assert(x1 <= x2); 137 }; 138 139 enum CheckVLine = 140 q{ 141 static if (CHECKED) 142 { 143 if (x < 0 || x >= v.w || y1 >= v.h || y2 <= 0 || y1 >= y2) return; 144 if (y1 < 0) y1 = 0; 145 if (y2 >= v.h) y2 = v.h; 146 } 147 assert(y1 <= y2); 148 }; 149 150 void hline(bool CHECKED=true, V, COLOR)(auto ref V v, int x1, int x2, int y, COLOR c) 151 if (isWritableView!V && is(COLOR : ViewColor!V)) 152 { 153 mixin(CheckHLine); 154 v.scanline(y)[x1..x2] = c; 155 } 156 157 void vline(bool CHECKED=true, V, COLOR)(auto ref V v, int x, int y1, int y2, COLOR c) 158 if (isWritableView!V && is(COLOR : ViewColor!V)) 159 { 160 mixin(CheckVLine); 161 foreach (y; y1..y2) // TODO: optimize 162 v[x, y] = c; 163 } 164 165 void line(bool CHECKED=true, V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, COLOR c) 166 if (isWritableView!V && is(COLOR : ViewColor!V)) 167 { 168 mixin FixMath; 169 170 enum DrawLine = q{ 171 // Axis-independent part. Mixin context: 172 // a0 .. a1 - longer side 173 // b0 .. b1 - shorter side 174 // DrawPixel - mixin to draw a pixel at coordinates (a, b) 175 176 if (a0 == a1) 177 return; 178 179 if (a0 > a1) 180 { 181 swap(a0, a1); 182 swap(b0, b1); 183 } 184 185 // Use fixed-point for b position and offset per 1 pixel along "a" axis 186 assert(b0 < (1L<<coordinateBits) && b1 < (1L<<coordinateBits)); 187 SignedBitsType!(coordinateBits*2) bPos = b0 << coordinateBits; 188 SignedBitsType!(coordinateBits*2) bOff = ((b1-b0) << coordinateBits) / (a1-a0); 189 190 foreach (a; a0..a1+1) 191 { 192 int b = (bPos += bOff) >> coordinateBits; 193 mixin(DrawPixel); 194 } 195 }; 196 197 if (abs(x2-x1) > abs(y2-y1)) 198 { 199 alias x1 a0; 200 alias x2 a1; 201 alias y1 b0; 202 alias y2 b1; 203 enum DrawPixel = q{ v.putPixel!CHECKED(a, b, c); }; 204 mixin(DrawLine); 205 } 206 else 207 { 208 alias y1 a0; 209 alias y2 a1; 210 alias x1 b0; 211 alias x2 b1; 212 enum DrawPixel = q{ v.putPixel!CHECKED(b, a, c); }; 213 mixin(DrawLine); 214 } 215 } 216 217 /// Draws a rectangle with a solid line. 218 /// The coordinates represent bounds (open on the right) for the outside of the rectangle. 219 void rect(bool CHECKED=true, V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, COLOR c) 220 if (isWritableView!V && is(COLOR : ViewColor!V)) 221 { 222 sort2(x1, x2); 223 sort2(y1, y2); 224 v.hline!CHECKED(x1, x2, y1 , c); 225 v.hline!CHECKED(x1, x2, y2-1, c); 226 v.vline!CHECKED(x1 , y1, y2, c); 227 v.vline!CHECKED(x2-1, y1, y2, c); 228 } 229 230 void fillRect(bool CHECKED=true, V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, COLOR b) // [) 231 if (isWritableView!V && is(COLOR : ViewColor!V)) 232 { 233 sort2(x1, x2); 234 sort2(y1, y2); 235 static if (CHECKED) 236 { 237 if (x1 >= v.w || y1 >= v.h || x2 <= 0 || y2 <= 0 || x1==x2 || y1==y2) return; 238 if (x1 < 0) x1 = 0; 239 if (y1 < 0) y1 = 0; 240 if (x2 >= v.w) x2 = v.w; 241 if (y2 >= v.h) y2 = v.h; 242 } 243 foreach (y; y1..y2) 244 v.scanline(y)[x1..x2] = b; 245 } 246 247 void fillRect(bool CHECKED=true, V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, COLOR c, COLOR b) // [) 248 if (isWritableView!V && is(COLOR : ViewColor!V)) 249 { 250 v.rect!CHECKED(x1, y1, x2, y2, c); 251 if (x2-x1>2 && y2-y1>2) 252 v.fillRect!CHECKED(x1+1, y1+1, x2-1, y2-1, b); 253 } 254 255 /// Unchecked! Make sure area is bounded. 256 void uncheckedFloodFill(V, COLOR)(auto ref V v, int x, int y, COLOR c) 257 if (isDirectView!V && is(COLOR : ViewColor!V)) 258 { 259 v.floodFillPtr(&v[x, y], c, v[x, y]); 260 } 261 262 private void floodFillPtr(V, COLOR)(auto ref V v, COLOR* pp, COLOR c, COLOR f) 263 if (isDirectView!V && is(COLOR : ViewColor!V)) 264 { 265 COLOR* p0 = pp; while (*p0==f) p0--; p0++; 266 COLOR* p1 = pp; while (*p1==f) p1++; p1--; 267 auto stride = v.scanline(1).ptr-v.scanline(0).ptr; 268 for (auto p=p0; p<=p1; p++) 269 *p = c; 270 p0 -= stride; p1 -= stride; 271 for (auto p=p0; p<=p1; p++) 272 if (*p == f) 273 v.floodFillPtr(p, c, f); 274 p0 += stride*2; p1 += stride*2; 275 for (auto p=p0; p<=p1; p++) 276 if (*p == f) 277 v.floodFillPtr(p, c, f); 278 } 279 280 void fillCircle(V, COLOR)(auto ref V v, int x, int y, int r, COLOR c) 281 if (isWritableView!V && is(COLOR : ViewColor!V)) 282 { 283 int x0 = x>r?x-r:0; 284 int y0 = y>r?y-r:0; 285 int x1 = min(x+r, v.w-1); 286 int y1 = min(y+r, v.h-1); 287 int rs = sqr(r); 288 // TODO: optimize 289 foreach (py; y0..y1+1) 290 foreach (px; x0..x1+1) 291 if (sqr(x-px) + sqr(y-py) < rs) 292 v[px, py] = c; 293 } 294 295 void fillSector(V, COLOR)(auto ref V v, int x, int y, int r0, int r1, real a0, real a1, COLOR c) 296 if (isWritableView!V && is(COLOR : ViewColor!V)) 297 { 298 int x0 = x>r1?x-r1:0; 299 int y0 = y>r1?y-r1:0; 300 int x1 = min(x+r1, v.w-1); 301 int y1 = min(y+r1, v.h-1); 302 int r0s = sqr(r0); 303 int r1s = sqr(r1); 304 if (a0 > a1) 305 a1 += TAU; 306 foreach (py; y0..y1+1) 307 foreach (px; x0..x1+1) 308 { 309 int dx = px-x; 310 int dy = py-y; 311 int rs = sqr(dx) + sqr(dy); 312 if (r0s <= rs && rs < r1s) 313 { 314 real a = atan2(cast(real)dy, cast(real)dx); 315 if (!(a0 <= a && a <= a1)) 316 continue; 317 a += TAU; 318 if (!(a0 <= a && a <= a1)) 319 continue; 320 v[px, py] = c; 321 } 322 } 323 } 324 325 struct Coord { int x, y; string toString() { import std.string; return format("%s", [this.tupleof]); } } 326 327 void fillPoly(V, COLOR)(auto ref V v, Coord[] coords, COLOR f) 328 if (isWritableView!V && is(COLOR : ViewColor!V)) 329 { 330 int minY, maxY; 331 minY = maxY = coords[0].y; 332 foreach (c; coords[1..$]) 333 minY = min(minY, c.y), 334 maxY = max(maxY, c.y); 335 336 foreach (y; minY..maxY+1) 337 { 338 int[] intersections; 339 for (uint i=0; i<coords.length; i++) 340 { 341 auto c0=coords[i], c1=coords[i==$-1?0:i+1]; 342 if (y==c0.y) 343 { 344 assert(y == coords[i%$].y); 345 int pi = i-1; int py; 346 while ((py=coords[(pi+$)%$].y)==y) 347 pi--; 348 int ni = i+1; int ny; 349 while ((ny=coords[ni%$].y)==y) 350 ni++; 351 if (ni > coords.length) 352 continue; 353 if ((py>y) == (y>ny)) 354 intersections ~= coords[i%$].x; 355 i = ni-1; 356 } 357 else 358 if (c0.y<y && y<c1.y) 359 intersections ~= itpl(c0.x, c1.x, y, c0.y, c1.y); 360 else 361 if (c1.y<y && y<c0.y) 362 intersections ~= itpl(c1.x, c0.x, y, c1.y, c0.y); 363 } 364 365 assert(intersections.length % 2==0); 366 intersections.sort(); 367 for (uint i=0; i<intersections.length; i+=2) 368 v.hline!true(intersections[i], intersections[i+1], y, f); 369 } 370 } 371 372 // No caps 373 void thickLine(V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, int r, COLOR c) 374 if (isWritableView!V && is(COLOR : ViewColor!V)) 375 { 376 int dx = x2-x1; 377 int dy = y2-y1; 378 int d = cast(int)sqrt(cast(float)(sqr(dx)+sqr(dy))); 379 if (d==0) return; 380 381 int nx = dx*r/d; 382 int ny = dy*r/d; 383 384 fillPoly([ 385 Coord(x1-ny, y1+nx), 386 Coord(x1+ny, y1-nx), 387 Coord(x2+ny, y2-nx), 388 Coord(x2-ny, y2+nx), 389 ], c); 390 } 391 392 // No caps 393 void thickLinePoly(V, COLOR)(auto ref V v, Coord[] coords, int r, COLOR c) 394 if (isWritableView!V && is(COLOR : ViewColor!V)) 395 { 396 foreach (i; 0..coords.length) 397 thickLine(coords[i].tupleof, coords[(i+1)%$].tupleof, r, c); 398 } 399 400 // ************************************************************************************************************************************ 401 402 mixin template FixMath(ubyte coordinateBitsParam = 16) 403 { 404 enum coordinateBits = coordinateBitsParam; 405 406 static assert(COLOR.homogenous, "Asymmetric color types not supported, fix me!"); 407 /// Fixed-point type, big enough to hold a coordinate, with fractionary precision corresponding to channel precision. 408 alias fix = SignedBitsType!(COLOR.channelBits + coordinateBits); 409 /// Type to hold temporary values for multiplication and division 410 alias fix2 = SignedBitsType!(COLOR.channelBits*2 + coordinateBits); 411 412 static assert(COLOR.channelBits < 32, "Shift operators are broken for shifts over 32 bits, fix me!"); 413 fix tofix(T:int )(T x) { return cast(fix) (x<<COLOR.channelBits); } 414 fix tofix(T:float)(T x) { return cast(fix) (x*(1<<COLOR.channelBits)); } 415 T fixto(T:int)(fix x) { return cast(T)(x>>COLOR.channelBits); } 416 417 fix fixsqr(fix x) { return cast(fix)((cast(fix2)x*x) >> COLOR.channelBits); } 418 fix fixmul(fix x, fix y) { return cast(fix)((cast(fix2)x*y) >> COLOR.channelBits); } 419 fix fixdiv(fix x, fix y) { return cast(fix)((cast(fix2)x << COLOR.channelBits)/y); } 420 421 static assert(COLOR.ChannelType.sizeof*8 == COLOR.channelBits, "COLORs with ChannelType not corresponding to native type not currently supported, fix me!"); 422 /// Type only large enough to hold a fractionary part of a "fix" (i.e. color channel precision). Used for alpha values, etc. 423 alias COLOR.ChannelType frac; 424 /// Type to hold temporary values for multiplication and division 425 alias UnsignedBitsType!(COLOR.channelBits*2) frac2; 426 427 frac tofrac(T:float)(T x) { return cast(frac) (x*(1<<COLOR.channelBits)); } 428 frac fixfpart(fix x) { return cast(frac)x; } 429 frac fracsqr(frac x ) { return cast(frac)((cast(frac2)x*x) >> COLOR.channelBits); } 430 frac fracmul(frac x, frac y) { return cast(frac)((cast(frac2)x*y) >> COLOR.channelBits); } 431 432 frac tofracBounded(T:float)(T x) { return cast(frac) bound(tofix(x), 0, frac.max); } 433 } 434 435 // ************************************************************************************************************************************ 436 437 void whiteNoise(V)(V v) 438 if (isWritableView!V) 439 { 440 import std.random; 441 alias COLOR = ViewColor!V; 442 443 for (int y=0;y<v.h/2;y++) 444 for (int x=0;x<v.w/2;x++) 445 v[x*2, y*2] = COLOR.monochrome(uniform!(COLOR.ChannelType)()); 446 447 // interpolate 448 enum AVERAGE = q{(a+b)/2}; 449 450 for (int y=0;y<v.h/2;y++) 451 for (int x=0;x<v.w/2-1;x++) 452 v[x*2+1, y*2 ] = COLOR.op!AVERAGE(v[x*2 , y*2], v[x*2+2, y*2 ]); 453 for (int y=0;y<v.h/2-1;y++) 454 for (int x=0;x<v.w/2;x++) 455 v[x*2 , y*2+1] = COLOR.op!AVERAGE(v[x*2 , y*2], v[x*2 , y*2+2]); 456 for (int y=0;y<v.h/2-1;y++) 457 for (int x=0;x<v.w/2-1;x++) 458 v[x*2+1, y*2+1] = COLOR.op!AVERAGE(v[x*2+1, y*2], v[x*2+2, y*2+2]); 459 } 460 461 private template softRoundShape(bool RING) 462 { 463 void softRoundShape(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color) 464 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 465 { 466 mixin FixMath; 467 468 assert(r0 <= r1); 469 assert(r1 <= r2); 470 assert(r2 < 256); // precision constraint - see SqrType 471 //int ix = cast(int)x; 472 //int iy = cast(int)y; 473 //int ir1 = cast(int)sqr(r1-1); 474 //int ir2 = cast(int)sqr(r2+1); 475 int x1 = cast(int)(x-r2-1); if (x1<0) x1=0; 476 int y1 = cast(int)(y-r2-1); if (y1<0) y1=0; 477 int x2 = cast(int)(x+r2+1); if (x2>v.w) x2 = v.w; 478 int y2 = cast(int)(y+r2+1); if (y2>v.h) y2 = v.h; 479 480 static if (RING) 481 auto r0s = r0*r0; 482 auto r1s = r1*r1; 483 auto r2s = r2*r2; 484 //float rds = r2s - r1s; 485 486 fix fx = tofix(x); 487 fix fy = tofix(y); 488 489 static if (RING) 490 fix fr0s = tofix(r0s); 491 fix fr1s = tofix(r1s); 492 fix fr2s = tofix(r2s); 493 494 static if (RING) 495 fix fr10 = fr1s - fr0s; 496 fix fr21 = fr2s - fr1s; 497 498 for (int cy=y1;cy<y2;cy++) 499 { 500 auto row = v.scanline(cy); 501 for (int cx=x1;cx<x2;cx++) 502 { 503 alias SignedBitsType!(2*(8 + COLOR.channelBits)) SqrType; // fit the square of radius expressed as fixed-point 504 fix frs = cast(fix)((sqr(cast(SqrType)fx-tofix(cx)) + sqr(cast(SqrType)fy-tofix(cy))) >> COLOR.channelBits); // shift-right only once instead of once-per-sqr 505 506 //static frac alphafunc(frac x) { return fracsqr(x); } 507 static frac alphafunc(frac x) { return x; } 508 509 static if (RING) 510 { 511 if (frs<fr0s) 512 {} 513 else 514 if (frs<fr2s) 515 { 516 frac alpha; 517 if (frs<fr1s) 518 alpha = alphafunc(cast(frac)fixdiv(frs-fr0s, fr10)); 519 else 520 alpha = ~alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)); 521 row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha); 522 } 523 } 524 else 525 { 526 if (frs<fr1s) 527 row[cx] = color; 528 else 529 if (frs<fr2s) 530 { 531 frac alpha = ~alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)); 532 row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha); 533 } 534 } 535 } 536 } 537 } 538 } 539 540 void softRing(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color) 541 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 542 { 543 v.softRoundShape!true(x, y, r0, r1, r2, color); 544 } 545 546 void softCircle(T, V, COLOR)(auto ref V v, T x, T y, T r1, T r2, COLOR color) 547 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 548 { 549 v.softRoundShape!false(x, y, cast(T)0, r1, r2, color); 550 } 551 552 template aaPutPixel(bool CHECKED=true, bool USE_ALPHA=true) 553 { 554 void aaPutPixel(F:float, V, COLOR, frac)(auto ref V v, F x, F y, COLOR color, frac alpha) 555 if (isWritableView!V && is(COLOR : ViewColor!V)) 556 { 557 mixin FixMath; 558 559 void plot(bool CHECKED2)(int x, int y, frac f) 560 { 561 static if (CHECKED2) 562 if (x<0 || x>=v.w || y<0 || y>=v.h) 563 return; 564 565 COLOR* p = v.pixelPtr(x, y); 566 static if (USE_ALPHA) f = fracmul(f, cast(frac)alpha); 567 *p = COLOR.op!q{.blend(a, b, c)}(color, *p, f); 568 } 569 570 fix fx = tofix(x); 571 fix fy = tofix(y); 572 int ix = fixto!int(fx); 573 int iy = fixto!int(fy); 574 static if (CHECKED) 575 if (ix>=0 && iy>=0 && ix+1<v.w && iy+1<v.h) 576 { 577 plot!false(ix , iy , fracmul(~fixfpart(fx), ~fixfpart(fy))); 578 plot!false(ix , iy+1, fracmul(~fixfpart(fx), fixfpart(fy))); 579 plot!false(ix+1, iy , fracmul( fixfpart(fx), ~fixfpart(fy))); 580 plot!false(ix+1, iy+1, fracmul( fixfpart(fx), fixfpart(fy))); 581 return; 582 } 583 plot!CHECKED(ix , iy , fracmul(~fixfpart(fx), ~fixfpart(fy))); 584 plot!CHECKED(ix , iy+1, fracmul(~fixfpart(fx), fixfpart(fy))); 585 plot!CHECKED(ix+1, iy , fracmul( fixfpart(fx), ~fixfpart(fy))); 586 plot!CHECKED(ix+1, iy+1, fracmul( fixfpart(fx), fixfpart(fy))); 587 } 588 } 589 590 void aaPutPixel(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x, F y, COLOR color) 591 if (isWritableView!V && is(COLOR : ViewColor!V)) 592 { 593 //aaPutPixel!(false, F)(x, y, color, 0); // doesn't work, wtf 594 alias aaPutPixel!(CHECKED, false) f; 595 f(v, x, y, color, 0); 596 } 597 598 void hline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, int x1, int x2, int y, COLOR color, frac alpha) 599 if (isWritableView!V && is(COLOR : ViewColor!V)) 600 { 601 mixin(CheckHLine); 602 603 if (alpha==0) 604 return; 605 else 606 if (alpha==frac.max) 607 v.scanline(y)[x1..x2] = color; 608 else 609 foreach (ref p; v.scanline(y)[x1..x2]) 610 p = COLOR.op!q{.blend(a, b, c)}(color, p, alpha); 611 } 612 613 void vline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, int x, int y1, int y2, COLOR color, frac alpha) 614 if (isWritableView!V && is(COLOR : ViewColor!V)) 615 { 616 mixin(CheckVLine); 617 618 if (alpha==0) 619 return; 620 else 621 if (alpha==frac.max) 622 foreach (y; y1..y2) 623 v[x, y] = color; 624 else 625 foreach (y; y1..y2) 626 { 627 auto p = v.pixelPtr(x, y); 628 *p = COLOR.op!q{.blend(a, b, c)}(color, *p, alpha); 629 } 630 } 631 632 void aaFillRect(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x1, F y1, F x2, F y2, COLOR color) 633 if (isWritableView!V && is(COLOR : ViewColor!V)) 634 { 635 mixin FixMath; 636 637 sort2(x1, x2); 638 sort2(y1, y2); 639 fix x1f = tofix(x1); int x1i = fixto!int(x1f); 640 fix y1f = tofix(y1); int y1i = fixto!int(y1f); 641 fix x2f = tofix(x2); int x2i = fixto!int(x2f); 642 fix y2f = tofix(y2); int y2i = fixto!int(y2f); 643 644 v.vline!CHECKED(x1i, y1i+1, y2i, color, ~fixfpart(x1f)); 645 v.vline!CHECKED(x2i, y1i+1, y2i, color, fixfpart(x2f)); 646 v.hline!CHECKED(x1i+1, x2i, y1i, color, ~fixfpart(y1f)); 647 v.hline!CHECKED(x1i+1, x2i, y2i, color, fixfpart(y2f)); 648 v.aaPutPixel!CHECKED(x1i, y1i, color, fracmul(~fixfpart(x1f), ~fixfpart(y1f))); 649 v.aaPutPixel!CHECKED(x1i, y2i, color, fracmul(~fixfpart(x1f), fixfpart(y2f))); 650 v.aaPutPixel!CHECKED(x2i, y1i, color, fracmul( fixfpart(x2f), ~fixfpart(y1f))); 651 v.aaPutPixel!CHECKED(x2i, y2i, color, fracmul( fixfpart(x2f), fixfpart(y2f))); 652 653 v.fillRect!CHECKED(x1i+1, y1i+1, x2i, y2i, color); 654 } 655 656 void aaLine(bool CHECKED=true, V, COLOR)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color) 657 if (isWritableView!V && is(COLOR : ViewColor!V)) 658 { 659 // Simplistic straight-forward implementation. TODO: optimize 660 if (abs(x1-x2) > abs(y1-y2)) 661 for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1)) 662 v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color); 663 else 664 for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1)) 665 v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color); 666 } 667 668 void aaLine(bool CHECKED=true, V, COLOR, frac)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color, frac alpha) 669 if (isWritableView!V && is(COLOR : ViewColor!V)) 670 { 671 // ditto 672 if (abs(x1-x2) > abs(y1-y2)) 673 for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1)) 674 v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color, alpha); 675 else 676 for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1)) 677 v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color, alpha); 678 } 679 680 unittest 681 { 682 // Test instantiation 683 import ae.utils.graphics.color; 684 auto i = Image!RGB(100, 100); 685 auto c = RGB(1, 2, 3); 686 i.whiteNoise(); 687 i.aaLine(10, 10, 20, 20, c); 688 i.aaLine(10f, 10f, 20f, 20f, c, 100); 689 i.rect(10, 10, 20, 20, c); 690 i.fillRect(10, 10, 20, 20, c); 691 i.aaFillRect(10, 10, 20, 20, c); 692 i.vline(10, 10, 20, c); 693 i.vline(10, 10, 20, c); 694 i.line(10, 10, 20, 20, c); 695 i.fillCircle(10, 10, 10, c); 696 i.fillSector(10, 10, 10, 10, 0.0, TAU, c); 697 i.softRing(50, 50, 10, 15, 20, c); 698 i.softCircle(50, 50, 10, 15, c); 699 i.fillPoly([Coord(10, 10), Coord(10, 20), Coord(20, 20)], c); 700 i.uncheckedFloodFill(15, 15, RGB(4, 5, 6)); 701 } 702 703 // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 704 705 // Outdated code from before the overhaul. 706 // TODO: fix and update 707 708 version(none): 709 710 /// Draws an image. Returns this. 711 typeof(this) draw(bool CHECKED=true, SRCCANVAS)(int x, int y, SRCCANVAS v) 712 if (IsCanvas!SRCCANVAS && is(COLOR == SRCCANVAS.COLOR)) 713 { 714 static if (CHECKED) 715 { 716 if (v.w == 0 || v.h == 0 || 717 x+v.w <= 0 || y+v.h <= 0 || x >= w || y >= h) 718 return this; 719 720 auto r = v.window(0, 0, v.w, v.h); 721 if (x < 0) 722 r = r.window(-x, 0, r.w, r.h), 723 x = 0; 724 if (y < 0) 725 r = r.window(0, -y, r.w, r.h), 726 y = 0; 727 if (x+r.w > w) 728 r = r.window(0, 0, w-x, r.h); 729 if (y+r.h > h) 730 r = r.window(0, 0, r.w, h-y); 731 732 draw!false(x, y, r); 733 } 734 else 735 { 736 assert(v.w > 0 && v.h > 0); 737 assert(x >= 0 && x+v.w <= w && y >= 0 && y+v.h <= h); 738 739 // TODO: alpha blending 740 size_t dstStart = y*stride+x, srcStart = 0; 741 foreach (j; 0..v.h) 742 pixels[dstStart..dstStart+v.w] = v.pixels[srcStart..srcStart+v.w], 743 dstStart += stride, 744 srcStart += v.stride; 745 } 746 747 return this; 748 } 749 750 void subpixelDownscale()() 751 if (structFields!COLOR == ["r","g","b"] || structFields!COLOR == ["b","g","r"]) 752 { 753 Image!COLOR i; 754 i.size(HRX + hr.w*3 + HRX, hr.h); 755 i.draw(0, 0, hr.window(0, 0, HRX, hr.h)); 756 i.window(HRX, 0, HRX+hr.w*3, hr.h).upscaleDraw!(3, 1)(hr); 757 i.draw(HRX + hr.w*3, 0, hr.window(hr.w-HRX, 0, hr.w, hr.h)); 758 alias Color!(COLOR.BaseType, "g") BASE; 759 Image!BASE[3] channels; 760 Image!BASE scratch; 761 scratch.size(hr.w*3, hr.h); 762 763 foreach (int cx, char c; ValueTuple!('r', 'g', 'b')) 764 { 765 auto w = i.window(cx*HRX, 0, cx*HRX+hr.w*3, hr.h); 766 scratch.transformDraw!(`COLOR(c.`~c~`)`)(0, 0, w); 767 channels[cx].size(lr.w, lr.h); 768 channels[cx].downscaleDraw!(3*HRX, HRY)(scratch); 769 } 770 771 foreach (y; 0..lr.h) 772 foreach (x; 0..lr.w) 773 { 774 COLOR c; 775 c.r = channels[0][x, y].g; 776 c.g = channels[1][x, y].g; 777 c.b = channels[2][x, y].g; 778 lr[x, y] = c; 779 } 780 }