5 using System.ComponentModel;
75 public Color this[
int row,
int col]
91 if ( row < 0 || row >=
xnaTexture.Height )
throw new IndexOutOfRangeException(
"row" );
92 if ( col < 0 || col >=
XNATexture.Width )
throw new IndexOutOfRangeException(
"col" );
111 public Color[,]
GetData(
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
114 if ( h < ny ) ny = h;
117 if ( w < nx ) nx = w;
119 if ( nx <= 0 || ny <= 0 )
return new Color[0, 0];
128 for (
int iy = 0; iy < ny; iy++)
130 for (
int ix = 0; ix < nx; ix++)
131 bmp[iy, ix] = buffer[i++];
147 public void SetData(
Color[,] bmp,
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
151 int ny = bmp.GetLength( 0 );
152 int nx = bmp.GetLength( 1 );
155 if ( ny > h ) ny = h;
156 if ( nx > w ) nx = w;
159 if ( nx <= 0 || ny <= 0 )
return;
164 for (
int iy = 0; iy < ny; iy++ )
165 for (
int ix = 0; ix < nx; ix++ )
166 buffer[i++] = bmp[iy, ix];
181 public void SetData(
byte[] byteArr,
int height,
int width)
185 for (
int i = 0; i < height; i++)
187 for (
int j = 0; j < width; j++)
189 int r = byteArr[4 * (i * width + j) + 0];
190 int g = byteArr[4 * (i * width + j) + 1];
191 int b = byteArr[4 * (i * width + j) + 2];
192 int a = byteArr[4 * (i * width + j) + 3];
193 newColor[i, j] =
new Color(r, g, b, a);
231 public uint[,]
GetDataUInt(
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
234 if ( h < ny ) ny = h;
237 if ( w < nx ) nx = w;
239 if ( nx <= 0 || ny <= 0 )
return new uint[0, 0];
242 uint[,] bmp =
new uint[ny, nx];
248 for (
int iy = 0; iy < ny; iy++)
250 for (
int ix = 0; ix < nx; ix++)
251 bmp[iy, ix] = buffer[i++].ToUInt();
266 public uint[][]
GetDataUIntAA(
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
269 if ( h < ny ) ny = h;
272 if ( w < nx ) nx = w;
274 if ( nx <= 0 || ny <= 0 )
return new uint[0][];
277 uint[][] bmp =
new uint[ny][];
283 for (
int iy = 0; iy < ny; iy++ )
285 uint[] row =
new uint[nx];
287 for (
int ix = 0; ix < nx; ix++ )
288 row[ix] = buffer[i++].ToUInt();
303 public void SetData( uint[,] bmp,
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
307 int ny = bmp.GetLength( 0 );
308 int nx = bmp.GetLength( 1 );
311 if ( ny > h ) ny = h;
312 if ( nx > w ) nx = w;
316 if ( nx <= 0 || ny <= 0 )
return;
321 for (
int iy = 0; iy < ny; iy++ )
322 for (
int ix = 0; ix < nx; ix++ )
339 public void SetData( uint[][] bmp,
int ox = 0,
int oy = 0,
int w =
int.MaxValue,
int h =
int.MaxValue )
344 int nx = bmp[0].Length;
347 if ( ny > h ) ny = h;
348 if ( nx > w ) nx = w;
349 if ( nx <= 0 || ny <= 0 )
return;
354 for (
int iy = 0; iy < ny; iy++ )
355 for (
int ix = 0; ix < nx; ix++ )
388 internal Image(
int width,
int height )
392 this._height = height;
402 [EditorBrowsable( EditorBrowsableState.Never )]
406 this.xnaTexture = texture;
407 this._width = texture.Width;
408 this._height = texture.Height;
422 this._height = height;
424 this.InitTexture += delegate { this.
Fill( backColor ); };
434 : this( (int)Math.Round( width ), (int)Math.Round( height ), backColor )
440 if ( width < 1 || height < 1 )
441 throw new ArgumentException( String.Format(
"Image dimensions must be at least 1 x 1! (given: {0} x {1}", width, height ) );
452 throw new InvalidOperationException(
"Cannot initialize dimensions for image!" );
460 if ( xnaTexture !=
null )
466 throw new InvalidOperationException(
"Cannot initialize texture for image!" );
483 FileStream fileStream =
new FileStream(
assetName, FileMode.Open);
485 fileStream.Dispose();
521 for ( rect.Y = 0; rect.Y < h; rect.Y++ )
528 private static void CopyData( Texture2D dest, Texture2D src )
535 for ( rect.Y = 0; rect.Y < h; rect.Y++ )
537 src.GetData<
Color>( 0, rect, scanline, 0, w );
538 dest.SetData<
Color>( 0, rect, scanline, 0, w );
546 int w = srcRect.Width;
547 int h = srcRect.Height;
552 for (
int i = 0; i < h; i++ )
569 return operation(
new Color( c ) ).AsXnaColor();
587 for ( scanRect.Y = 0; scanRect.Y <
xnaTexture.Height; scanRect.Y++ )
593 scanline[j] = operation( scanline[j] );
633 #region static methods
635 #if !WINDOWS_STOREAPP
643 StreamReader sr =
new StreamReader( path );
670 public static Image FromURL(
string url )
677 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
678 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
679 Stream resStream = response.GetResponseStream();
681 MemoryStream memStream =
new MemoryStream();
682 resStream.CopyTo( memStream );
703 for (
int ver = 0; ver < height; ver++ )
705 for (
int hor = 0; hor < width; hor++ )
707 if (transparent) textureColors[i++] =
XnaColor.Transparent;
708 else textureColors[i++] =
XnaColor.Black;
713 for (
int j = 0; j < stars; j++ )
718 for (
int k = 0; k < size / 2; k++ )
722 if ( star + k < textureColors.Length )
723 textureColors[star + k] = starcolor;
725 if ( size % 2 != 0 || size == 2 )
728 int nextStar = star + k + width;
730 if ( nextStar < ( width * height ) )
732 textureColors[nextStar] = starcolor;
738 Texture2D newTexture =
new Texture2D(
Game.
GraphicsDevice, width, height,
false, SurfaceFormat.Color );
739 newTexture.SetData<
XnaColor>( textureColors );
741 return new Image( newTexture );
758 var device = spriteBatch.GraphicsDevice;
761 int textw = ( textDims.X > 1 ) ? Convert.ToInt32( textDims.X ) : 1;
762 int texth = ( textDims.Y > 1 ) ? Convert.ToInt32( textDims.Y ) : 1;
765 RenderTarget2D rt =
new RenderTarget2D( device, textw, texth,
false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8 );
768 device.SetRenderTarget( rt );
769 device.Clear( ClearOptions.Target | ClearOptions.DepthBuffer, backgroundColor.
AsXnaColor(), 1.0f, 0 );
776 device.SetRenderTarget(
null );
779 return new Image( rt );
798 var device = spriteBatch.GraphicsDevice;
801 int textw = ( textDims.X > 1 ) ? Convert.ToInt32( textDims.X ) : 1;
802 int texth = ( textDims.Y > 1 ) ? Convert.ToInt32( textDims.Y ) : 1;
805 RenderTarget2D rt =
new RenderTarget2D( device, img.
Width, img.
Height,
false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8 );
808 device.SetRenderTarget( rt );
809 device.Clear( ClearOptions.Target | ClearOptions.DepthBuffer, backgroundColor.
AsXnaColor(), 1.0f, 0 );
811 float xpos = 0.5f * ( img.
Width - textw ) + (
float)position.
X;
812 float ypos = 0.5f * ( img.
Height - texth ) - (
float)position.
Y;
820 device.SetRenderTarget(
null );
824 return new Image( rt );
855 for (
int ver = 0; ver < imageHeight; ver++ )
857 for (
int hor = 0; hor < imageWidth; hor++ )
859 textureColors[i++] =
XnaColor.Lerp( upper, lower, ( (
float)ver / (
float)imageHeight ) );
863 Texture2D newTexture =
new Texture2D(
Game.
GraphicsDevice, imageWidth, imageHeight,
false, SurfaceFormat.Color );
864 newTexture.SetData<
XnaColor>( textureColors );
865 return new Image( newTexture );
888 res[l] = scanline[r];
889 res[r] = scanline[l];
896 res[l] = scanline[l];
913 for ( scanRect.Y = 0; scanRect.Y < image.
Height; scanRect.Y++ )
917 newTex.SetData<
XnaColor>( 0, scanRect, scanline, 0, image.
Width );
920 return new Image( newTex );
931 for (
int i = 0; i < images.Length; i++ )
932 result[i] =
Mirror( images[i] );
949 for (
int i = 0; i < image.
Height / 2; i++ )
952 scanRectLower.Y = image.
Height - 1 - i;
957 newTex.SetData<
XnaColor>( 0, scanRectUpper, scanlineLower, 0, image.
Width );
958 newTex.SetData<
XnaColor>( 0, scanRectLower, scanlineUpper, 0, image.
Width );
961 if ( image.
Height % 2 == 1 )
964 scanRectUpper.Y = image.
Height / 2;
966 newTex.SetData<
XnaColor>( 0, scanRectUpper, scanlineUpper, 0, image.
Width );
969 return new Image( newTex );
980 for (
int i = 0; i < images.Length; i++ )
981 result[i] =
Flip( images[i] );
998 for ( scanRect.Y = 0; scanRect.Y < image.
Height; scanRect.Y++ )
1002 for (
int i = 0; i < image.
Width; i++ )
1004 if ( scanline[i].A < 255 )
1006 scanline[i].R = (byte)( ( 255 - scanline[i].A ) * xnaColor.R + scanline[i].A * scanline[i].R );
1007 scanline[i].G = (byte)( ( 255 - scanline[i].A ) * xnaColor.G + scanline[i].A * scanline[i].G );
1008 scanline[i].B = (byte)( ( 255 - scanline[i].A ) * xnaColor.B + scanline[i].A * scanline[i].B );
1010 if ( scanline[i].A > 10 )
1017 newTex.SetData<
XnaColor>( 0, scanRect, scanline, 0, image.
Width );
1020 return new Image( newTex );
1032 for (
int i = 0; i < images.Length; i++ )
1033 result[i] =
Color( images[i], color );
1043 for ( scanRect.Y = 0; scanRect.Y < image.
Height; scanRect.Y++ )
1046 for (
int i = 0; i < image.
Width; i++ )
1048 scanline[i].A = alpha;
1050 newTex.SetData<
XnaColor>( 0, scanRect, scanline, 0, image.
Width );
1052 return new Image( newTex );
1057 if ( left.
Height != right.
Height )
throw new InvalidOperationException(
"Cannot tile two images with different height" );
1075 if ( top.
Width != bottom.
Width )
throw new InvalidOperationException(
"Cannot tile two images with different width" );
1093 public Image Area(
int left,
int top,
int right,
int bottom )
1095 int width = right - left;
1096 int height = bottom - top;
1098 if ( width <= 0 )
throw new ArgumentException(
"Left coordinate must be less than right coordinate" );
1099 if ( height <= 0 )
throw new ArgumentException(
"Top coordinate must be less than bottom coordinate" );
1104 Image areaImage =
new Image( width, height );
1120 scanline[i] = backColor;
1122 for ( rect.Y = 0; rect.Y <
xnaTexture.Height; rect.Y++ )
1142 if ( exactAlpha && c.A != srcColor.A )
1145 if (
JyColor.Distance( c, srcColor ) <= tolerance )
1147 if ( !blend )
return destColor;
1148 Vector3 srcDist =
new Vector3( c.R - srcColor.R, c.G - srcColor.G, c.B - srcColor.B );
1149 return new XnaColor( destColor.ToVector3() + srcDist );
1169 return c == srcColor ? destColor : c;
1183 MemoryStream jpegStream =
new MemoryStream();
1185 jpegStream.Seek( 0, SeekOrigin.Begin );
1197 MemoryStream pngStream =
new MemoryStream();
1199 pngStream.Seek( 0, SeekOrigin.Begin );