1 module daffodil.transform.flip; 2 3 import std.algorithm; 4 5 import daffodil.image; 6 7 /** 8 * Flip ``image`` along ``axis`` in-place. ``axis`` may contain ``x``, ``y`` or 9 * both. 10 */ 11 void flip(string axis, V)(Image!V image) { 12 static if (canFind(axis, 'x')) { 13 foreach (y; 0..image.height) { 14 foreach (x; 0..image.width/2) { 15 auto temp = image[$ - x - 1, y].dup; 16 image[$ - x - 1, y] = image[x, y]; 17 image[x, y] = temp; 18 } 19 } 20 } 21 22 static if (canFind(axis, 'y')) { 23 foreach (x; 0..image.width) { 24 foreach(y; 0..image.height/2) { 25 auto temp = image[x, $ - y - 1].dup; 26 image[x, $ - y - 1] = image[x, y]; 27 image[x, y] = temp; 28 } 29 } 30 } 31 } 32 33 /** 34 * Same as :d:func:`flip` but performs the operation on a copy of ``image``. 35 * Allows for stringing operations together. 36 */ 37 auto flipped(string axis, V)(const Image!V image) { 38 auto output = image.dup; 39 output.flip!axis(); 40 return output; 41 } 42 43 @("flip transformation") 44 unittest { 45 import daffodil; 46 47 auto image = new Image!ubyte(2, 2, 3, RGB); 48 image[0, 0] = [1f, 1f, 1f]; 49 image[0, 1] = [1f, 0f, 0f]; 50 image[1, 0] = [0f, 1f, 0f]; 51 image[1, 1] = [0f, 0f, 1f]; 52 53 image.flip!"x"(); 54 assert(image[0, 0] == [ 0, 255, 0]); 55 assert(image[0, 1] == [ 0, 0, 255]); 56 assert(image[1, 0] == [255, 255, 255]); 57 assert(image[1, 1] == [255, 0, 0]); 58 59 image.flip!"y"(); 60 assert(image[0, 0] == [ 0, 0, 255]); 61 assert(image[0, 1] == [ 0, 255, 0]); 62 assert(image[1, 0] == [255, 0, 0]); 63 assert(image[1, 1] == [255, 255, 255]); 64 65 image = image.flipped!"xy"(); 66 assert(image[0, 0] == [255, 255, 255]); 67 assert(image[0, 1] == [255, 0, 0]); 68 assert(image[1, 0] == [ 0, 255, 0]); 69 assert(image[1, 1] == [ 0, 0, 255]); 70 }