image.process\transform\transform_16f.mbt

100% · 72 / 72 points · 315 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 图像变换 16-bit/float 变体 Image transforms 16-bit/float variants
  3. 3///
  4. 4/// 16位旋转/翻转 rotate_16 / flip_16
  5. 5/// 16位/浮点旋转/翻转变换 16-bit/float rotate/flip transforms
  6. 6// 图像变换 16-bit/float 变体:rotate / flip
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9///|
  10. 10/// @see rotate_90 8-bit 版本,本函数为 Image16 变体
  11. 11pub fn rotate_90_16(img : @types.Image16) -> @types.Image16 {
  12. 12● let ch = img.channels
  13. 13 let new_w = img.height
  14. 14 let new_h = img.width
  15. 15 let bpp = ch * 2
  16. 16● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  17. 17 for y in 0..<img.height {
  18. 18● for x in 0..<img.width {
  19. 19● let nx = img.height - 1 - y
  20. 20 let ny = x
  21. 21 let src = (y * img.width + x) * bpp
  22. 22 let dst = (ny * new_w + nx) * bpp
  23. 23 for i in 0..<bpp {
  24. 24● out[dst + i] = img.data[src + i]
  25. 25 }
  26. 26 }
  27. 27 }
  28. 28 @types.Image16::{
  29. 29 width: new_w,
  30. 30 height: new_h,
  31. 31 channels: ch,
  32. 32● data: Bytes::from_array(out),
  33. 33 }
  34. 34}
  35. 35
  36. 36///|
  37. 37/// @see rotate_90 8-bit 版本,本函数为 ImageF 变体
  38. 38pub fn rotate_90_f(img : @types.ImageF) -> @types.ImageF {
  39. 39● let ch = img.channels
  40. 40 let new_w = img.height
  41. 41 let new_h = img.width
  42. 42 let bpp = ch * 4
  43. 43● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  44. 44 for y in 0..<img.height {
  45. 45● for x in 0..<img.width {
  46. 46● let nx = img.height - 1 - y
  47. 47 let ny = x
  48. 48 let src = (y * img.width + x) * bpp
  49. 49 let dst = (ny * new_w + nx) * bpp
  50. 50 for i in 0..<bpp {
  51. 51● out[dst + i] = img.data[src + i]
  52. 52 }
  53. 53 }
  54. 54 }
  55. 55 @types.ImageF::{
  56. 56 width: new_w,
  57. 57 height: new_h,
  58. 58 channels: ch,
  59. 59● data: Bytes::from_array(out),
  60. 60 }
  61. 61}
  62. 62
  63. 63///|
  64. 64/// @see rotate_180 8-bit 版本,本函数为 Image16 变体
  65. 65pub fn rotate_180_16(img : @types.Image16) -> @types.Image16 {
  66. 66● let ch = img.channels
  67. 67 let bpp = ch * 2
  68. 68● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  69. 69 for y in 0..<img.height {
  70. 70● for x in 0..<img.width {
  71. 71● let nx = img.width - 1 - x
  72. 72 let ny = img.height - 1 - y
  73. 73 let src = (y * img.width + x) * bpp
  74. 74 let dst = (ny * img.width + nx) * bpp
  75. 75 for i in 0..<bpp {
  76. 76● out[dst + i] = img.data[src + i]
  77. 77 }
  78. 78 }
  79. 79 }
  80. 80 @types.Image16::{
  81. 81 width: img.width,
  82. 82 height: img.height,
  83. 83 channels: ch,
  84. 84● data: Bytes::from_array(out),
  85. 85 }
  86. 86}
  87. 87
  88. 88///|
  89. 89/// @see rotate_180 8-bit 版本,本函数为 ImageF 变体
  90. 90pub fn rotate_180_f(img : @types.ImageF) -> @types.ImageF {
  91. 91● let ch = img.channels
  92. 92 let bpp = ch * 4
  93. 93● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  94. 94 for y in 0..<img.height {
  95. 95● for x in 0..<img.width {
  96. 96● let nx = img.width - 1 - x
  97. 97 let ny = img.height - 1 - y
  98. 98 let src = (y * img.width + x) * bpp
  99. 99 let dst = (ny * img.width + nx) * bpp
  100. 100 for i in 0..<bpp {
  101. 101● out[dst + i] = img.data[src + i]
  102. 102 }
  103. 103 }
  104. 104 }
  105. 105 @types.ImageF::{
  106. 106 width: img.width,
  107. 107 height: img.height,
  108. 108 channels: ch,
  109. 109● data: Bytes::from_array(out),
  110. 110 }
  111. 111}
  112. 112
  113. 113///|
  114. 114/// @see rotate_270 8-bit 版本,本函数为 Image16 变体
  115. 115pub fn rotate_270_16(img : @types.Image16) -> @types.Image16 {
  116. 116● let ch = img.channels
  117. 117 let new_w = img.height
  118. 118 let new_h = img.width
  119. 119 let bpp = ch * 2
  120. 120● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  121. 121 for y in 0..<img.height {
  122. 122● for x in 0..<img.width {
  123. 123● let nx = y
  124. 124 let ny = img.width - 1 - x
  125. 125 let src = (y * img.width + x) * bpp
  126. 126 let dst = (ny * new_w + nx) * bpp
  127. 127 for i in 0..<bpp {
  128. 128● out[dst + i] = img.data[src + i]
  129. 129 }
  130. 130 }
  131. 131 }
  132. 132 @types.Image16::{
  133. 133 width: new_w,
  134. 134 height: new_h,
  135. 135 channels: ch,
  136. 136● data: Bytes::from_array(out),
  137. 137 }
  138. 138}
  139. 139
  140. 140///|
  141. 141/// @see rotate_270 8-bit 版本,本函数为 ImageF 变体
  142. 142pub fn rotate_270_f(img : @types.ImageF) -> @types.ImageF {
  143. 143● let ch = img.channels
  144. 144 let new_w = img.height
  145. 145 let new_h = img.width
  146. 146 let bpp = ch * 4
  147. 147● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  148. 148 for y in 0..<img.height {
  149. 149● for x in 0..<img.width {
  150. 150● let nx = y
  151. 151 let ny = img.width - 1 - x
  152. 152 let src = (y * img.width + x) * bpp
  153. 153 let dst = (ny * new_w + nx) * bpp
  154. 154 for i in 0..<bpp {
  155. 155● out[dst + i] = img.data[src + i]
  156. 156 }
  157. 157 }
  158. 158 }
  159. 159 @types.ImageF::{
  160. 160 width: new_w,
  161. 161 height: new_h,
  162. 162 channels: ch,
  163. 163● data: Bytes::from_array(out),
  164. 164 }
  165. 165}
  166. 166
  167. 167///|
  168. 168/// @see flip_horizontal 8-bit 版本,本函数为 Image16 变体
  169. 169pub fn flip_horizontal_16(img : @types.Image16) -> @types.Image16 {
  170. 170● let ch = img.channels
  171. 171 let bpp = ch * 2
  172. 172● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  173. 173 for y in 0..<img.height {
  174. 174● for x in 0..<img.width {
  175. 175● let nx = img.width - 1 - x
  176. 176 let src = (y * img.width + x) * bpp
  177. 177 let dst = (y * img.width + nx) * bpp
  178. 178 for i in 0..<bpp {
  179. 179● out[dst + i] = img.data[src + i]
  180. 180 }
  181. 181 }
  182. 182 }
  183. 183 @types.Image16::{
  184. 184 width: img.width,
  185. 185 height: img.height,
  186. 186 channels: ch,
  187. 187● data: Bytes::from_array(out),
  188. 188 }
  189. 189}
  190. 190
  191. 191///|
  192. 192/// @see flip_horizontal 8-bit 版本,本函数为 ImageF 变体
  193. 193pub fn flip_horizontal_f(img : @types.ImageF) -> @types.ImageF {
  194. 194● let ch = img.channels
  195. 195 let bpp = ch * 4
  196. 196● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  197. 197 for y in 0..<img.height {
  198. 198● for x in 0..<img.width {
  199. 199● let nx = img.width - 1 - x
  200. 200 let src = (y * img.width + x) * bpp
  201. 201 let dst = (y * img.width + nx) * bpp
  202. 202 for i in 0..<bpp {
  203. 203● out[dst + i] = img.data[src + i]
  204. 204 }
  205. 205 }
  206. 206 }
  207. 207 @types.ImageF::{
  208. 208 width: img.width,
  209. 209 height: img.height,
  210. 210 channels: ch,
  211. 211● data: Bytes::from_array(out),
  212. 212 }
  213. 213}
  214. 214
  215. 215///|
  216. 216/// 16-bit 垂直翻转。上下镜像。
  217. 217/// @see flip_vertical 8-bit 版本,本函数为 Image16 变体
  218. 218pub fn flip_vertical_16(img : @types.Image16) -> @types.Image16 {
  219. 219● let ch = img.channels
  220. 220 let bpp = ch * 2
  221. 221● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  222. 222 for y in 0..<img.height {
  223. 223● let ny = img.height - 1 - y
  224. 224 for x in 0..<img.width {
  225. 225● let src = (y * img.width + x) * bpp
  226. 226 let dst = (ny * img.width + x) * bpp
  227. 227 for i in 0..<bpp {
  228. 228● out[dst + i] = img.data[src + i]
  229. 229 }
  230. 230 }
  231. 231 }
  232. 232 @types.Image16::{
  233. 233 width: img.width,
  234. 234 height: img.height,
  235. 235 channels: ch,
  236. 236● data: Bytes::from_array(out),
  237. 237 }
  238. 238}
  239. 239
  240. 240///|
  241. 241/// float 垂直翻转。上下镜像。
  242. 242/// @see flip_vertical 8-bit 版本,本函数为 ImageF 变体
  243. 243pub fn flip_vertical_f(img : @types.ImageF) -> @types.ImageF {
  244. 244● let ch = img.channels
  245. 245 let bpp = ch * 4
  246. 246● let out : Array[Byte] = Array::make(img.width * img.height * bpp, b'\x00')
  247. 247 for y in 0..<img.height {
  248. 248● let ny = img.height - 1 - y
  249. 249 for x in 0..<img.width {
  250. 250● let src = (y * img.width + x) * bpp
  251. 251 let dst = (ny * img.width + x) * bpp
  252. 252 for i in 0..<bpp {
  253. 253● out[dst + i] = img.data[src + i]
  254. 254 }
  255. 255 }
  256. 256 }
  257. 257 @types.ImageF::{
  258. 258 width: img.width,
  259. 259 height: img.height,
  260. 260 channels: ch,
  261. 261● data: Bytes::from_array(out),
  262. 262 }
  263. 263}
  264. 264
  265. 265///|
  266. 266/// 16-bit 转置。交换宽高,(x,y) -> (y,x)。
  267. 267/// @see transpose 8-bit 版本,本函数为 Image16 变体
  268. 268pub fn transpose_16(img : @types.Image16) -> @types.Image16 {
  269. 269● let ch = img.channels
  270. 270 let new_w = img.height
  271. 271 let new_h = img.width
  272. 272 let bpp = ch * 2
  273. 273● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  274. 274 for y in 0..<img.height {
  275. 275● for x in 0..<img.width {
  276. 276● let src = (y * img.width + x) * bpp
  277. 277 let dst = (x * new_w + y) * bpp
  278. 278 for i in 0..<bpp {
  279. 279● out[dst + i] = img.data[src + i]
  280. 280 }
  281. 281 }
  282. 282 }
  283. 283 @types.Image16::{
  284. 284 width: new_w,
  285. 285 height: new_h,
  286. 286 channels: ch,
  287. 287● data: Bytes::from_array(out),
  288. 288 }
  289. 289}
  290. 290
  291. 291///|
  292. 292/// float 转置。交换宽高,(x,y) -> (y,x)。
  293. 293/// @see transpose 8-bit 版本,本函数为 ImageF 变体
  294. 294pub fn transpose_f(img : @types.ImageF) -> @types.ImageF {
  295. 295● let ch = img.channels
  296. 296 let new_w = img.height
  297. 297 let new_h = img.width
  298. 298 let bpp = ch * 4
  299. 299● let out : Array[Byte] = Array::make(new_w * new_h * bpp, b'\x00')
  300. 300 for y in 0..<img.height {
  301. 301● for x in 0..<img.width {
  302. 302● let src = (y * img.width + x) * bpp
  303. 303 let dst = (x * new_w + y) * bpp
  304. 304 for i in 0..<bpp {
  305. 305● out[dst + i] = img.data[src + i]
  306. 306 }
  307. 307 }
  308. 308 }
  309. 309 @types.ImageF::{
  310. 310 width: new_w,
  311. 311 height: new_h,
  312. 312 channels: ch,
  313. 313● data: Bytes::from_array(out),
  314. 314 }
  315. 315}