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