25 using SiliconStudio.Core;
27 namespace SiliconStudio.
Paradox.Graphics
40 private int rowStride;
42 private int bufferStride;
44 private readonly IntPtr dataPointer;
51 private bool isStrictRowStride;
64 if (dataPointer == IntPtr.Zero)
65 throw new ArgumentException(
"Pointer cannot be equal to IntPtr.Zero",
"dataPointer");
70 this.rowStride = rowStride;
71 this.bufferStride = bufferStride;
72 this.dataPointer = dataPointer;
73 this.pixelSize = format.SizeInBytes();
74 this.isStrictRowStride = (
pixelSize * width) == rowStride;
81 public int Width {
get {
return width; } }
87 public int Height {
get {
return height; } }
105 public int RowStride {
get {
return this.rowStride; } }
111 public int BufferStride {
get {
return this.bufferStride; } }
130 if (this.Width != pixelBuffer.
Width
131 ||
this.Height != pixelBuffer.
Height
132 ||
this.Format != pixelBuffer.
Format)
134 throw new ArgumentException(
"Invalid destination pixelBufferArray. Mush have same Width, Height and Format",
"pixelBuffer");
140 Utilities.CopyMemory(pixelBuffer.DataPointer, this.DataPointer, this.BufferStride);
146 var rowStride = Math.Min(RowStride, pixelBuffer.
RowStride);
149 for(
int i = 0; i < Height; i++)
151 Utilities.CopyMemory(
new IntPtr(dstPointer),
new IntPtr(srcPointer), rowStride);
152 srcPointer += this.RowStride;
153 dstPointer += pixelBuffer.RowStride;
169 Height = this.height,
172 Dimension = TextureDimension.Texture2D,
176 Image.Save(
new [] {
this}, 1, description, imageStream, fileType);
189 public unsafe T GetPixel<T>(
int x,
int y) where T :
struct
191 return Utilities.Read<T>(
new IntPtr(((byte*)this.
DataPointer + RowStride *
y + x * PixelSize)));
204 public unsafe
void SetPixel<T>(
int x,
int y, T value) where T :
struct
206 Utilities.Write(
new IntPtr((byte*)this.
DataPointer + RowStride *
y + x * PixelSize), ref value);
220 public T[] GetPixels<T>(
int yOffset = 0) where T :
struct
222 var sizeOfOutputPixel = Utilities.SizeOf<T>();
223 var totalSize = Width * Height *
pixelSize;
224 if ((totalSize % sizeOfOutputPixel) != 0)
225 throw new ArgumentException(
string.Format(
"Invalid sizeof(T), not a multiple of current size [{0}]in bytes ", totalSize));
227 var buffer =
new T[totalSize / sizeOfOutputPixel];
228 GetPixels(buffer, yOffset);
244 public void GetPixels<T>(T[] pixels,
int yOffset = 0) where T :
struct
246 GetPixels(pixels, yOffset, 0, pixels.Length);
262 public unsafe
void GetPixels<T>(T[] pixels,
int yOffset,
int pixelIndex,
int pixelCount) where T :
struct
264 var pixelPointer = (byte*)this.
DataPointer + yOffset * rowStride;
265 if (isStrictRowStride)
267 Utilities.Read(
new IntPtr(pixelPointer), pixels, 0, pixelCount);
271 var sizeOfOutputPixel = Utilities.SizeOf<T>() * pixelCount;
272 var sizePerWidth = sizeOfOutputPixel / Width;
273 var remainingPixels = sizeOfOutputPixel % Width;
274 for(
int i = 0; i < sizePerWidth; i++)
276 Utilities.Read(
new IntPtr(pixelPointer), pixels, pixelIndex, Width);
277 pixelPointer += rowStride;
280 if (remainingPixels > 0)
282 Utilities.Read(
new IntPtr(pixelPointer), pixels, pixelIndex, remainingPixels);
298 public void SetPixels<T>(T[] sourcePixels,
int yOffset = 0) where T :
struct
300 SetPixels(sourcePixels, yOffset, 0, sourcePixels.Length);
316 public unsafe
void SetPixels<T>(T[] sourcePixels,
int yOffset,
int pixelIndex,
int pixelCount) where T :
struct
318 var pixelPointer = (byte*)this.
DataPointer + yOffset * rowStride;
319 if (isStrictRowStride)
321 Utilities.Write(
new IntPtr(pixelPointer), sourcePixels, 0, pixelCount);
325 var sizeOfOutputPixel = Utilities.SizeOf<T>() * pixelCount;
326 var sizePerWidth = sizeOfOutputPixel / Width;
327 var remainingPixels = sizeOfOutputPixel % Width;
328 for (
int i = 0; i < sizePerWidth; i++)
330 Utilities.Write(
new IntPtr(pixelPointer), sourcePixels, pixelIndex, Width);
331 pixelPointer += rowStride;
334 if (remainingPixels > 0)
336 Utilities.Write(
new IntPtr(pixelPointer), sourcePixels, pixelIndex, remainingPixels);
int RowStride
Gets the row stride in number of bytes.
ImageFileType
Image file format used by Image.Save(string,SiliconStudio.Paradox.Graphics.ImageFileType) ...
PixelBuffer(int width, int height, PixelFormat format, int rowStride, int bufferStride, IntPtr dataPointer)
Initializes a new instance of the PixelBuffer struct.
void Save(Stream imageStream, ImageFileType fileType)
Saves this pixel buffer to a stream.
unsafe void CopyTo(PixelBuffer pixelBuffer)
Copies this pixel buffer to a destination pixel buffer.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
int BufferStride
Gets the total size in bytes of this pixel buffer.
IntPtr DataPointer
Gets the pointer to the pixel buffer.
PixelFormat Format
Gets the format.
An unmanaged buffer of pixels.
int Height
Gets the height.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
PixelFormat
Defines various types of pixel formats.