1 /* 2 Copyright (c) 2016-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Geometric ransformations of images 31 * 32 * Copyright: Timur Gafarov 2016-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.image.transform; 37 38 import dlib.math.vector; 39 import dlib.math.matrix; 40 import dlib.math.transformation; 41 import dlib.math.utils; 42 43 import dlib.image.image; 44 import dlib.image.color; 45 46 /// Tranforms an image with affine 3x3 matrix 47 SuperImage affineTransformImage(SuperImage img, SuperImage outp, Matrix3x3f m) 48 { 49 SuperImage res; 50 if (outp) 51 res = outp; 52 else 53 res = img.createSameFormat(img.width, img.height); 54 55 foreach(y; 0..res.height) 56 foreach(x; 0..res.width) 57 { 58 Vector2f v1 = Vector2f(x, y).affineTransform2D(m); 59 res[x, y] = bilinearPixel(img, v1.x, v1.y); 60 } 61 62 return res; 63 } 64 65 /// ditto 66 SuperImage affineTransformImage(SuperImage img, Matrix3x3f m) 67 { 68 return affineTransformImage(img, null, m); 69 } 70 71 /// Translates an image (positive x goes right, positive y goes down) 72 SuperImage translateImage(SuperImage img, SuperImage outp, Vector2f t) 73 { 74 Matrix3x3f m = translationMatrix2D(-t); 75 return affineTransformImage(img, outp, m); 76 } 77 78 /// ditto 79 SuperImage translateImage(SuperImage img, Vector2f t) 80 { 81 return translateImage(img, null, t); 82 } 83 84 /// Rotates an image clockwise around its center. Angle is in degrees. 85 SuperImage rotateImage(SuperImage img, SuperImage outp, float angle) 86 { 87 Vector2f center = Vector2f(img.width, img.height) * 0.5f; 88 Matrix3x3f m = 89 translationMatrix2D(center) * 90 rotationMatrix2D(degtorad(angle)) * 91 translationMatrix2D(-center); 92 return affineTransformImage(img, outp, m); 93 } 94 95 /// ditto 96 SuperImage rotateImage(SuperImage img, float angle) 97 { 98 return rotateImage(img, null, angle); 99 } 100 101 /// Scales an image 102 SuperImage scaleImage(SuperImage img, SuperImage outp, Vector2f s) 103 { 104 Matrix3x3f m = scaleMatrix2D(Vector2f(1, 1) / s); 105 return affineTransformImage(img, outp, m); 106 } 107 108 /// ditto 109 SuperImage scaleImage(SuperImage img, Vector2f s) 110 { 111 return scaleImage(img, null, s); 112 } 113 114 /// Uniformly scales an image 115 SuperImage scaleImage(SuperImage img, SuperImage outp, float s) 116 { 117 float sinv = 1.0f / s; 118 Matrix3x3f m = scaleMatrix2D(Vector2f(sinv, sinv)); 119 return affineTransformImage(img, outp, m); 120 } 121 122 /// ditto 123 SuperImage scaleImage(SuperImage img, float s) 124 { 125 return scaleImage(img, null, s); 126 }