11// Copyright (c) TensorStack. All rights reserved.
22// Licensed under the Apache 2.0 License.
33using System ;
4+ using TensorStack . Common . Image ;
45
56namespace TensorStack . Common . Tensor
67{
@@ -16,7 +17,7 @@ public class ImageTensor : Tensor<float>
1617 /// </summary>
1718 /// <param name="tensor">The tensor.</param>
1819 public ImageTensor ( Tensor < float > tensor )
19- : base ( ProcessChannels ( tensor ) , [ 1 , 4 , ..tensor . Dimensions [ ^ 2 ..] ] )
20+ : base ( ProcessChannels ( tensor ) , [ 1 , 4 , .. tensor . Dimensions [ ^ 2 ..] ] )
2021 {
2122 ThrowIfInvalid ( ) ;
2223 }
@@ -175,6 +176,55 @@ public ImageTensor CloneAs()
175176 return Clone ( ) . AsImageTensor ( ) ;
176177 }
177178
179+
180+ /// <summary>
181+ /// Gets the RGBA pixel from ImageTensor
182+ /// </summary>
183+ /// <param name="imageTensor">The image tensor.</param>
184+ /// <param name="x">The x.</param>
185+ /// <param name="y">The y.</param>
186+ /// <returns>ImagePixel.</returns>
187+ /// <exception cref="System.ArgumentOutOfRangeException">Pixel ({x},{y}) out of range ({imageTensor.Width}x{imageTensor.Height}).</exception>
188+ public ImagePixel GetPixel ( int x , int y )
189+ {
190+ if ( x >= Width || y >= Height )
191+ throw new ArgumentOutOfRangeException ( $ "Pixel ({ x } ,{ y } ) out of range ({ Width } x{ Height } ).") ;
192+
193+ var pixelIndex = y * Width + x ;
194+ var span = Memory . Span ;
195+ var stride = Height * Width ;
196+ return new ImagePixel (
197+ span [ 0 * stride + pixelIndex ] ,
198+ span [ 1 * stride + pixelIndex ] ,
199+ span [ 2 * stride + pixelIndex ] ,
200+ span [ 3 * stride + pixelIndex ]
201+ ) ;
202+ }
203+
204+
205+ /// <summary>
206+ /// Sets the RGBA pixel for ImageTensor
207+ /// </summary>
208+ /// <param name="imageTensor">The image tensor.</param>
209+ /// <param name="x">The x.</param>
210+ /// <param name="y">The y.</param>
211+ /// <param name="color">The color.</param>
212+ /// <exception cref="System.ArgumentOutOfRangeException">Pixel ({x},{y}) out of range ({imageTensor.Width}x{imageTensor.Height}).</exception>
213+ public void SetPixel ( int x , int y , ImagePixel color )
214+ {
215+ if ( x >= Width || y >= Height )
216+ throw new ArgumentOutOfRangeException ( $ "Pixel ({ x } ,{ y } ) out of range ({ Width } x{ Height } ).") ;
217+
218+ int pixelIndex = y * Width + x ;
219+ var span = Memory . Span ;
220+ int stride = Height * Width ;
221+ span [ 0 * stride + pixelIndex ] = color . R ;
222+ span [ 1 * stride + pixelIndex ] = color . G ;
223+ span [ 2 * stride + pixelIndex ] = color . B ;
224+ span [ 3 * stride + pixelIndex ] = color . A ;
225+ }
226+
227+
178228 /// <summary>
179229 /// Throws if Dimensions are invalid.
180230 /// </summary>
0 commit comments