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 (a0 <= a+TAU && a+TAU <= a1)) 317 v[px, py] = c; 318 } 319 } 320 } 321 322 struct Coord { int x, y; string toString() { import std.string; return format("%s", [this.tupleof]); } } 323 324 void fillPoly(V, COLOR)(auto ref V v, Coord[] coords, COLOR f) 325 if (isWritableView!V && is(COLOR : ViewColor!V)) 326 { 327 int minY, maxY; 328 minY = maxY = coords[0].y; 329 foreach (c; coords[1..$]) 330 minY = min(minY, c.y), 331 maxY = max(maxY, c.y); 332 333 foreach (y; minY..maxY+1) 334 { 335 int[] intersections; 336 for (uint i=0; i<coords.length; i++) 337 { 338 auto c0=coords[i], c1=coords[i==$-1?0:i+1]; 339 if (y==c0.y) 340 { 341 assert(y == coords[i%$].y); 342 int pi = i-1; int py; 343 while ((py=coords[(pi+$)%$].y)==y) 344 pi--; 345 int ni = i+1; int ny; 346 while ((ny=coords[ni%$].y)==y) 347 ni++; 348 if (ni > coords.length) 349 continue; 350 if ((py>y) == (y>ny)) 351 intersections ~= coords[i%$].x; 352 i = ni-1; 353 } 354 else 355 if (c0.y<y && y<c1.y) 356 intersections ~= itpl(c0.x, c1.x, y, c0.y, c1.y); 357 else 358 if (c1.y<y && y<c0.y) 359 intersections ~= itpl(c1.x, c0.x, y, c1.y, c0.y); 360 } 361 362 assert(intersections.length % 2==0); 363 intersections.sort(); 364 for (uint i=0; i<intersections.length; i+=2) 365 v.hline!true(intersections[i], intersections[i+1], y, f); 366 } 367 } 368 369 // No caps 370 void thickLine(V, COLOR)(auto ref V v, int x1, int y1, int x2, int y2, int r, COLOR c) 371 if (isWritableView!V && is(COLOR : ViewColor!V)) 372 { 373 int dx = x2-x1; 374 int dy = y2-y1; 375 int d = cast(int)sqrt(cast(float)(sqr(dx)+sqr(dy))); 376 if (d==0) return; 377 378 int nx = dx*r/d; 379 int ny = dy*r/d; 380 381 fillPoly([ 382 Coord(x1-ny, y1+nx), 383 Coord(x1+ny, y1-nx), 384 Coord(x2+ny, y2-nx), 385 Coord(x2-ny, y2+nx), 386 ], c); 387 } 388 389 // No caps 390 void thickLinePoly(V, COLOR)(auto ref V v, Coord[] coords, int r, COLOR c) 391 if (isWritableView!V && is(COLOR : ViewColor!V)) 392 { 393 foreach (i; 0..coords.length) 394 thickLine(coords[i].tupleof, coords[(i+1)%$].tupleof, r, c); 395 } 396 397 // ************************************************************************************************************************************ 398 399 mixin template FixMath(ubyte coordinateBitsParam = 16) 400 { 401 enum coordinateBits = coordinateBitsParam; 402 403 static assert(COLOR.homogenous, "Asymmetric color types not supported, fix me!"); 404 /// Fixed-point type, big enough to hold a coordinate, with fractionary precision corresponding to channel precision. 405 alias fix = SignedBitsType!(COLOR.channelBits + coordinateBits); 406 /// Type to hold temporary values for multiplication and division 407 alias fix2 = SignedBitsType!(COLOR.channelBits*2 + coordinateBits); 408 409 static assert(COLOR.channelBits < 32, "Shift operators are broken for shifts over 32 bits, fix me!"); 410 fix tofix(T:int )(T x) { return cast(fix) (x<<COLOR.channelBits); } 411 fix tofix(T:float)(T x) { return cast(fix) (x*(1<<COLOR.channelBits)); } 412 T fixto(T:int)(fix x) { return cast(T)(x>>COLOR.channelBits); } 413 414 fix fixsqr(fix x) { return cast(fix)((cast(fix2)x*x) >> COLOR.channelBits); } 415 fix fixmul(fix x, fix y) { return cast(fix)((cast(fix2)x*y) >> COLOR.channelBits); } 416 fix fixdiv(fix x, fix y) { return cast(fix)((cast(fix2)x << COLOR.channelBits)/y); } 417 418 static assert(COLOR.ChannelType.sizeof*8 == COLOR.channelBits, "COLORs with ChannelType not corresponding to native type not currently supported, fix me!"); 419 /// Type only large enough to hold a fractionary part of a "fix" (i.e. color channel precision). Used for alpha values, etc. 420 alias COLOR.ChannelType frac; 421 /// Type to hold temporary values for multiplication and division 422 alias UnsignedBitsType!(COLOR.channelBits*2) frac2; 423 424 frac tofrac(T:float)(T x) { return cast(frac) (x*(1<<COLOR.channelBits)); } 425 frac fixfpart(fix x) { return cast(frac)x; } 426 frac fracsqr(frac x ) { return cast(frac)((cast(frac2)x*x) >> COLOR.channelBits); } 427 frac fracmul(frac x, frac y) { return cast(frac)((cast(frac2)x*y) >> COLOR.channelBits); } 428 429 frac tofracBounded(T:float)(T x) { return cast(frac) bound(tofix(x), 0, frac.max); } 430 } 431 432 // ************************************************************************************************************************************ 433 434 void whiteNoise(V)(V v) 435 if (isWritableView!V) 436 { 437 import std.random; 438 alias COLOR = ViewColor!V; 439 440 for (int y=0;y<v.h/2;y++) 441 for (int x=0;x<v.w/2;x++) 442 v[x*2, y*2] = COLOR.monochrome(uniform!(COLOR.ChannelType)()); 443 444 // interpolate 445 enum AVERAGE = q{(a+b)/2}; 446 447 for (int y=0;y<v.h/2;y++) 448 for (int x=0;x<v.w/2-1;x++) 449 v[x*2+1, y*2 ] = COLOR.op!AVERAGE(v[x*2 , y*2], v[x*2+2, y*2 ]); 450 for (int y=0;y<v.h/2-1;y++) 451 for (int x=0;x<v.w/2;x++) 452 v[x*2 , y*2+1] = COLOR.op!AVERAGE(v[x*2 , y*2], v[x*2 , y*2+2]); 453 for (int y=0;y<v.h/2-1;y++) 454 for (int x=0;x<v.w/2-1;x++) 455 v[x*2+1, y*2+1] = COLOR.op!AVERAGE(v[x*2+1, y*2], v[x*2+2, y*2+2]); 456 } 457 458 private template softRoundShape(bool RING) 459 { 460 void softRoundShape(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color) 461 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 462 { 463 mixin FixMath; 464 465 assert(r0 <= r1); 466 assert(r1 <= r2); 467 assert(r2 < 256); // precision constraint - see SqrType 468 //int ix = cast(int)x; 469 //int iy = cast(int)y; 470 //int ir1 = cast(int)sqr(r1-1); 471 //int ir2 = cast(int)sqr(r2+1); 472 int x1 = cast(int)(x-r2-1); if (x1<0) x1=0; 473 int y1 = cast(int)(y-r2-1); if (y1<0) y1=0; 474 int x2 = cast(int)(x+r2+1); if (x2>v.w) x2 = v.w; 475 int y2 = cast(int)(y+r2+1); if (y2>v.h) y2 = v.h; 476 477 static if (RING) 478 auto r0s = r0*r0; 479 auto r1s = r1*r1; 480 auto r2s = r2*r2; 481 //float rds = r2s - r1s; 482 483 fix fx = tofix(x); 484 fix fy = tofix(y); 485 486 static if (RING) 487 fix fr0s = tofix(r0s); 488 fix fr1s = tofix(r1s); 489 fix fr2s = tofix(r2s); 490 491 static if (RING) 492 fix fr10 = fr1s - fr0s; 493 fix fr21 = fr2s - fr1s; 494 495 for (int cy=y1;cy<y2;cy++) 496 { 497 auto row = v.scanline(cy); 498 for (int cx=x1;cx<x2;cx++) 499 { 500 alias SignedBitsType!(2*(8 + COLOR.channelBits)) SqrType; // fit the square of radius expressed as fixed-point 501 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 502 503 //static frac alphafunc(frac x) { return fracsqr(x); } 504 static frac alphafunc(frac x) { return x; } 505 506 static if (RING) 507 { 508 if (frs<fr0s) 509 {} 510 else 511 if (frs<fr2s) 512 { 513 frac alpha; 514 if (frs<fr1s) 515 alpha = alphafunc(cast(frac)fixdiv(frs-fr0s, fr10)); 516 else 517 alpha = ~alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)); 518 row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha); 519 } 520 } 521 else 522 { 523 if (frs<fr1s) 524 row[cx] = color; 525 else 526 if (frs<fr2s) 527 { 528 frac alpha = ~alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)); 529 row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha); 530 } 531 } 532 } 533 } 534 } 535 } 536 537 void softRing(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color) 538 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 539 { 540 v.softRoundShape!true(x, y, r0, r1, r2, color); 541 } 542 543 void softCircle(T, V, COLOR)(auto ref V v, T x, T y, T r1, T r2, COLOR color) 544 if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V)) 545 { 546 v.softRoundShape!false(x, y, cast(T)0, r1, r2, color); 547 } 548 549 template aaPutPixel(bool CHECKED=true, bool USE_ALPHA=true) 550 { 551 void aaPutPixel(F:float, V, COLOR, frac)(auto ref V v, F x, F y, COLOR color, frac alpha) 552 if (isWritableView!V && is(COLOR : ViewColor!V)) 553 { 554 mixin FixMath; 555 556 void plot(bool CHECKED2)(int x, int y, frac f) 557 { 558 static if (CHECKED2) 559 if (x<0 || x>=v.w || y<0 || y>=v.h) 560 return; 561 562 COLOR* p = v.pixelPtr(x, y); 563 static if (USE_ALPHA) f = fracmul(f, cast(frac)alpha); 564 *p = COLOR.op!q{.blend(a, b, c)}(color, *p, f); 565 } 566 567 fix fx = tofix(x); 568 fix fy = tofix(y); 569 int ix = fixto!int(fx); 570 int iy = fixto!int(fy); 571 static if (CHECKED) 572 if (ix>=0 && iy>=0 && ix+1<v.w && iy+1<v.h) 573 { 574 plot!false(ix , iy , fracmul(~fixfpart(fx), ~fixfpart(fy))); 575 plot!false(ix , iy+1, fracmul(~fixfpart(fx), fixfpart(fy))); 576 plot!false(ix+1, iy , fracmul( fixfpart(fx), ~fixfpart(fy))); 577 plot!false(ix+1, iy+1, fracmul( fixfpart(fx), fixfpart(fy))); 578 return; 579 } 580 plot!CHECKED(ix , iy , fracmul(~fixfpart(fx), ~fixfpart(fy))); 581 plot!CHECKED(ix , iy+1, fracmul(~fixfpart(fx), fixfpart(fy))); 582 plot!CHECKED(ix+1, iy , fracmul( fixfpart(fx), ~fixfpart(fy))); 583 plot!CHECKED(ix+1, iy+1, fracmul( fixfpart(fx), fixfpart(fy))); 584 } 585 } 586 587 void aaPutPixel(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x, F y, COLOR color) 588 if (isWritableView!V && is(COLOR : ViewColor!V)) 589 { 590 //aaPutPixel!(false, F)(x, y, color, 0); // doesn't work, wtf 591 alias aaPutPixel!(CHECKED, false) f; 592 f(v, x, y, color, 0); 593 } 594 595 void hline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, int x1, int x2, int y, COLOR color, frac alpha) 596 if (isWritableView!V && is(COLOR : ViewColor!V)) 597 { 598 mixin(CheckHLine); 599 600 if (alpha==0) 601 return; 602 else 603 if (alpha==frac.max) 604 v.scanline(y)[x1..x2] = color; 605 else 606 foreach (ref p; v.scanline(y)[x1..x2]) 607 p = COLOR.op!q{.blend(a, b, c)}(color, p, alpha); 608 } 609 610 void vline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, int x, int y1, int y2, COLOR color, frac alpha) 611 if (isWritableView!V && is(COLOR : ViewColor!V)) 612 { 613 mixin(CheckVLine); 614 615 if (alpha==0) 616 return; 617 else 618 if (alpha==frac.max) 619 foreach (y; y1..y2) 620 v[x, y] = color; 621 else 622 foreach (y; y1..y2) 623 { 624 auto p = v.pixelPtr(x, y); 625 *p = COLOR.op!q{.blend(a, b, c)}(color, *p, alpha); 626 } 627 } 628 629 void aaFillRect(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x1, F y1, F x2, F y2, COLOR color) 630 if (isWritableView!V && is(COLOR : ViewColor!V)) 631 { 632 mixin FixMath; 633 634 sort2(x1, x2); 635 sort2(y1, y2); 636 fix x1f = tofix(x1); int x1i = fixto!int(x1f); 637 fix y1f = tofix(y1); int y1i = fixto!int(y1f); 638 fix x2f = tofix(x2); int x2i = fixto!int(x2f); 639 fix y2f = tofix(y2); int y2i = fixto!int(y2f); 640 641 v.vline!CHECKED(x1i, y1i+1, y2i, color, ~fixfpart(x1f)); 642 v.vline!CHECKED(x2i, y1i+1, y2i, color, fixfpart(x2f)); 643 v.hline!CHECKED(x1i+1, x2i, y1i, color, ~fixfpart(y1f)); 644 v.hline!CHECKED(x1i+1, x2i, y2i, color, fixfpart(y2f)); 645 v.aaPutPixel!CHECKED(x1i, y1i, color, fracmul(~fixfpart(x1f), ~fixfpart(y1f))); 646 v.aaPutPixel!CHECKED(x1i, y2i, color, fracmul(~fixfpart(x1f), fixfpart(y2f))); 647 v.aaPutPixel!CHECKED(x2i, y1i, color, fracmul( fixfpart(x2f), ~fixfpart(y1f))); 648 v.aaPutPixel!CHECKED(x2i, y2i, color, fracmul( fixfpart(x2f), fixfpart(y2f))); 649 650 v.fillRect!CHECKED(x1i+1, y1i+1, x2i, y2i, color); 651 } 652 653 void aaLine(bool CHECKED=true, V, COLOR)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color) 654 if (isWritableView!V && is(COLOR : ViewColor!V)) 655 { 656 // Simplistic straight-forward implementation. TODO: optimize 657 if (abs(x1-x2) > abs(y1-y2)) 658 for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1)) 659 v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color); 660 else 661 for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1)) 662 v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color); 663 } 664 665 void aaLine(bool CHECKED=true, V, COLOR, frac)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color, frac alpha) 666 if (isWritableView!V && is(COLOR : ViewColor!V)) 667 { 668 // ditto 669 if (abs(x1-x2) > abs(y1-y2)) 670 for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1)) 671 v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color, alpha); 672 else 673 for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1)) 674 v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color, alpha); 675 } 676 677 unittest 678 { 679 // Test instantiation 680 import ae.utils.graphics.color; 681 auto i = Image!RGB(100, 100); 682 auto c = RGB(1, 2, 3); 683 i.whiteNoise(); 684 i.aaLine(10, 10, 20, 20, c); 685 i.aaLine(10f, 10f, 20f, 20f, c, 100); 686 i.rect(10, 10, 20, 20, c); 687 i.fillRect(10, 10, 20, 20, c); 688 i.aaFillRect(10, 10, 20, 20, c); 689 i.vline(10, 10, 20, c); 690 i.vline(10, 10, 20, c); 691 i.line(10, 10, 20, 20, c); 692 i.fillCircle(10, 10, 10, c); 693 i.fillSector(10, 10, 10, 10, 0.0, TAU, c); 694 i.softRing(50, 50, 10, 15, 20, c); 695 i.softCircle(50, 50, 10, 15, c); 696 i.fillPoly([Coord(10, 10), Coord(10, 20), Coord(20, 20)], c); 697 i.uncheckedFloodFill(15, 15, RGB(4, 5, 6)); 698 } 699 700 // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 701 702 // Outdated code from before the overhaul. 703 // TODO: fix and update 704 705 version(none): 706 707 /// Draws an image. Returns this. 708 typeof(this) draw(bool CHECKED=true, SRCCANVAS)(int x, int y, SRCCANVAS v) 709 if (IsCanvas!SRCCANVAS && is(COLOR == SRCCANVAS.COLOR)) 710 { 711 static if (CHECKED) 712 { 713 if (v.w == 0 || v.h == 0 || 714 x+v.w <= 0 || y+v.h <= 0 || x >= w || y >= h) 715 return this; 716 717 auto r = v.window(0, 0, v.w, v.h); 718 if (x < 0) 719 r = r.window(-x, 0, r.w, r.h), 720 x = 0; 721 if (y < 0) 722 r = r.window(0, -y, r.w, r.h), 723 y = 0; 724 if (x+r.w > w) 725 r = r.window(0, 0, w-x, r.h); 726 if (y+r.h > h) 727 r = r.window(0, 0, r.w, h-y); 728 729 draw!false(x, y, r); 730 } 731 else 732 { 733 assert(v.w > 0 && v.h > 0); 734 assert(x >= 0 && x+v.w <= w && y >= 0 && y+v.h <= h); 735 736 // TODO: alpha blending 737 size_t dstStart = y*stride+x, srcStart = 0; 738 foreach (j; 0..v.h) 739 pixels[dstStart..dstStart+v.w] = v.pixels[srcStart..srcStart+v.w], 740 dstStart += stride, 741 srcStart += v.stride; 742 } 743 744 return this; 745 } 746 747 void subpixelDownscale()() 748 if (structFields!COLOR == ["r","g","b"] || structFields!COLOR == ["b","g","r"]) 749 { 750 Image!COLOR i; 751 i.size(HRX + hr.w*3 + HRX, hr.h); 752 i.draw(0, 0, hr.window(0, 0, HRX, hr.h)); 753 i.window(HRX, 0, HRX+hr.w*3, hr.h).upscaleDraw!(3, 1)(hr); 754 i.draw(HRX + hr.w*3, 0, hr.window(hr.w-HRX, 0, hr.w, hr.h)); 755 alias Color!(COLOR.BaseType, "g") BASE; 756 Image!BASE[3] channels; 757 Image!BASE scratch; 758 scratch.size(hr.w*3, hr.h); 759 760 foreach (int cx, char c; ValueTuple!('r', 'g', 'b')) 761 { 762 auto w = i.window(cx*HRX, 0, cx*HRX+hr.w*3, hr.h); 763 scratch.transformDraw!(`COLOR(c.`~c~`)`)(0, 0, w); 764 channels[cx].size(lr.w, lr.h); 765 channels[cx].downscaleDraw!(3*HRX, HRY)(scratch); 766 } 767 768 foreach (y; 0..lr.h) 769 foreach (x; 0..lr.w) 770 { 771 COLOR c; 772 c.r = channels[0][x, y].g; 773 c.g = channels[1][x, y].g; 774 c.b = channels[2][x, y].g; 775 lr[x, y] = c; 776 } 777 }