本文整理汇总了C#中CGBitmapContext.ClearRect方法的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext.ClearRect方法的具体用法?C# CGBitmapContext.ClearRect怎么用?C# CGBitmapContext.ClearRect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CGBitmapContext
的用法示例。
在下文中一共展示了CGBitmapContext.ClearRect方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadBitmapData
void LoadBitmapData (int texId)
{
NSData texData = NSData.FromFile (NSBundle.MainBundle.PathForResource ("texture1", "png"));
UIImage image = UIImage.LoadFromData (texData);
if (image == null)
return;
int width = image.CGImage.Width;
int height = image.CGImage.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
byte[] imageData = new byte[height * width * 4];
CGContext context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace,
CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
context.TranslateCTM (0, height);
context.ScaleCTM (1, -1);
colorSpace.Dispose ();
context.ClearRect (new RectangleF (0, 0, width, height));
context.DrawImage (new RectangleF (0, 0, width, height), image.CGImage);
GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose ();
}
示例2: iOSSurfaceSource
/// <summary>
/// Initializes a new instance of the <see cref="iOSSurfaceSource"/> class.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> that contains the surface data.</param>
public iOSSurfaceSource(Stream stream)
{
Contract.Require(stream, nameof(stream));
using (var data = NSData.FromStream(stream))
{
using (var img = UIImage.LoadFromData(data))
{
this.width = (Int32)img.Size.Width;
this.height = (Int32)img.Size.Height;
this.stride = (Int32)img.CGImage.BytesPerRow;
this.bmpData = Marshal.AllocHGlobal(stride * height);
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
using (var bmp = new CGBitmapContext(bmpData, width, height, 8, stride, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
bmp.ClearRect(new CGRect(0, 0, width, height));
bmp.DrawImage(new CGRect(0, 0, width, height), img.CGImage);
}
}
}
}
ReversePremultiplication();
}
示例3: GLTexture
public GLTexture (string inFilename)
{
GL.Enable (EnableCap.Texture2D);
GL.Enable (EnableCap.Blend);
filename = inFilename;
GL.Hint (HintTarget.GenerateMipmapHint, HintMode.Nicest);
GL.GenTextures (1, out texture);
GL.BindTexture (TextureTarget.Texture2D, texture);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) All.Repeat);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) All.Repeat);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Linear);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Linear);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Nearest);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Nearest);
//TODO Remove the Substring method if you don't support iOS versions prior to iOS 6.
string extension = Path.GetExtension (filename).Substring(1);
string baseFilename = Path.GetFileNameWithoutExtension (filename);
string path = NSBundle.MainBundle.PathForResource (baseFilename, extension);
NSData texData = NSData.FromFile (path);
UIImage image = UIImage.LoadFromData (texData);
if (image == null)
return;
nint width = image.CGImage.Width;
nint height = image.CGImage.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
byte [] imageData = new byte[height * width * 4];
CGContext context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace,
CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
context.TranslateCTM (0, height);
context.ScaleCTM (1, -1);
colorSpace.Dispose ();
context.ClearRect (new CGRect (0, 0, width, height));
context.DrawImage (new CGRect (0, 0, width, height), image.CGImage);
GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)width, (int)height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose ();
}
示例4: TexImage2D
public static void TexImage2D(Stream data, out int width, out int height)
{
data.Position = 0;
var nsData = NSData.FromStream(data);
var image = UIImage.LoadFromData(nsData);
if (image == null) throw new Exception ("could not load image data");
width = (int)image.CGImage.Width;
height = (int)image.CGImage.Height;
var colorSpace = CGColorSpace.CreateDeviceRGB();
var imageData = new byte[height * width * 4];
var context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
colorSpace.Dispose();
context.ClearRect(new CGRect(0, 0, width, height));
context.DrawImage(new CGRect(0, 0, width, height), image.CGImage);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose();
}
示例5: InitWithCGImage
private void InitWithCGImage(CGImage image, All filter)
{
int width,height,i;
CGContext context = null;
IntPtr data;
CGColorSpace colorSpace;
IntPtr tempData;
bool hasAlpha;
CGImageAlphaInfo info;
CGAffineTransform transform;
Size imageSize;
SurfaceFormat pixelFormat;
bool sizeToFit = false;
if(image == null)
{
throw new ArgumentException(" uimage is invalid! " );
}
info = image.AlphaInfo;
hasAlpha = ((info == CGImageAlphaInfo.PremultipliedLast) || (info == CGImageAlphaInfo.PremultipliedFirst) || (info == CGImageAlphaInfo.Last) || (info == CGImageAlphaInfo.First) ? true : false);
if (image.ColorSpace != null)
{
pixelFormat = SurfaceFormat.Color;
}
else
{
pixelFormat = SurfaceFormat.Alpha8;
}
imageSize = new Size(image.Width,image.Height);
transform = CGAffineTransform.MakeIdentity();
width = imageSize.Width;
if((width != 1) && ((width & (width - 1))!=0)) {
i = 1;
while((sizeToFit ? 2 * i : i) < width)
i *= 2;
width = i;
}
height = imageSize.Height;
if((height != 1) && ((height & (height - 1))!=0)) {
i = 1;
while((sizeToFit ? 2 * i : i) < height)
i *= 2;
height = i;
}
// TODO: kMaxTextureSize = 1024
while((width > 1024) || (height > 1024))
{
width /= 2;
height /= 2;
transform = CGAffineTransform.MakeScale(0.5f,0.5f);
imageSize.Width /= 2;
imageSize.Height /= 2;
}
switch(pixelFormat)
{
case SurfaceFormat.Color:
colorSpace = CGColorSpace.CreateDeviceRGB();
data = Marshal.AllocHGlobal(height * width * 4);
context = new CGBitmapContext(data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast);
colorSpace.Dispose();
break;
case SurfaceFormat.Alpha8:
data = Marshal.AllocHGlobal(height * width);
context = new CGBitmapContext(data, width, height, 8, width, null, CGImageAlphaInfo.Only);
break;
default:
throw new NotSupportedException("Invalid pixel format");
}
context.ClearRect(new RectangleF(0,0,width,height));
context.TranslateCTM(0, height - imageSize.Height);
if (!transform.IsIdentity)
{
context.ConcatCTM(transform);
}
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
//Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
/*
if(pixelFormat == SurfaceFormat.Rgb32) {
tempData = Marshal.AllocHGlobal(height * width * 2);
int d32;
short d16;
int inPixel32Count=0,outPixel16Count=0;
for(i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int))
{
d32 = Marshal.ReadInt32(data,inPixel32Count);
short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11);
short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5);
short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0);
d16 = (short) (R | G | B);
Marshal.WriteInt16(tempData,outPixel16Count,d16);
//.........这里部分代码省略.........
示例6: ESTexture2D
public ESTexture2D(UIImage uiImage, All filter)
{
CGImage image = uiImage.CGImage;
if(uiImage == null)
throw new ArgumentNullException("uiImage");
// TODO: could use this to implement lower-bandwidth textures
//bool hasAlpha = (image.AlphaInfo == CGImageAlphaInfo.First || image.AlphaInfo == CGImageAlphaInfo.Last
// || image.AlphaInfo == CGImageAlphaInfo.PremultipliedFirst || image.AlphaInfo == CGImageAlphaInfo.PremultipliedLast);
// Image dimentions:
logicalSize = new Point((int)uiImage.Size.Width, (int)uiImage.Size.Height);
pixelWidth = uiImage.CGImage.Width;
pixelHeight = uiImage.CGImage.Height;
// Round up the target texture width and height to powers of two:
potWidth = pixelWidth;
potHeight = pixelHeight;
if(( potWidth & ( potWidth-1)) != 0) { int w = 1; while(w < potWidth) { w *= 2; } potWidth = w; }
if((potHeight & (potHeight-1)) != 0) { int h = 1; while(h < potHeight) { h *= 2; } potHeight = h; }
// Scale down textures that are too large...
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
while((potWidth > 1024) || (potHeight > 1024))
{
potWidth /= 2; // Note: no precision loss - it's a power of two
potHeight /= 2;
pixelWidth /= 2; // Note: precision loss - assume possibility of dropping a pixel at each step is ok
pixelHeight /= 2;
transform.Multiply(CGAffineTransform.MakeScale(0.5f, 0.5f));
}
RecalculateRatio();
lock(textureLoadBufferLockObject)
{
CreateTextureLoadBuffer();
unsafe
{
fixed(byte* data = textureLoadBuffer)
{
var colorSpace = CGColorSpace.CreateDeviceRGB();
var context = new CGBitmapContext(new IntPtr(data), potWidth, potHeight,
8, 4 * potWidth, colorSpace, CGImageAlphaInfo.PremultipliedLast);
context.ClearRect(new RectangleF(0, 0, potWidth, potHeight));
context.TranslateCTM(0, potHeight - pixelHeight); // TODO: this does not play nice with the precision-loss above (keeping half-pixel to the edge)
if(!transform.IsIdentity)
context.ConcatCTM(transform);
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
SetupTexture(new IntPtr(data), filter);
context.Dispose();
colorSpace.Dispose();
}
}
}
}
示例7: Texture2D
public Texture2D(UIImage uiImage)
{
if (uiImage == null) {
throw new ArgumentNullException("uiImage");
}
CGImage image = uiImage.CGImage;
if (image == null) {
throw new InvalidOperationException("Attempted to create a Texture2D from UIImage, but resulting CGImage is null");
}
CGImageAlphaInfo info = image.AlphaInfo;
bool hasAlpha = info == CGImageAlphaInfo.PremultipliedLast || info == CGImageAlphaInfo.PremultipliedFirst || info == CGImageAlphaInfo.Last || info == CGImageAlphaInfo.First;
int bpp = image.BitsPerComponent;
Texture2DPixelFormat pixelFormat;
if (image.ColorSpace != null) {
if (hasAlpha || bpp >= 8) {
pixelFormat = Texture2DPixelFormat.Default;
} else {
pixelFormat = Texture2DPixelFormat.RGB565;
}
} else {
pixelFormat = Texture2DPixelFormat.A8;
}
int width = image.Width;
if (width != 1 && (width & (width - 1)) != 0) {
int i = 1;
while (i < width) {
i *= 2;
}
width = i;
}
int height = image.Height;
if (height != 1 && (height & (height - 1)) != 0) {
int i = 1;
while (i < height) {
i *= 2;
}
height = i;
}
if (width > MaxTextureSize || height > MaxTextureSize) {
throw new InvalidOperationException("Image is too large. Width or height larger than MaxTextureSize");
}
CGColorSpace colorSpace = null;
CGContext context;
byte[] data;
unsafe {
// all formats require w*h*4, except A8 requires just w*h
int dataSize = width * height * 4;
if (pixelFormat == Texture2DPixelFormat.A8) {
dataSize = width * height;
}
data = new byte[dataSize];
fixed (byte* dp = data) {
switch (pixelFormat) {
case Texture2DPixelFormat.RGBA8888:
case Texture2DPixelFormat.RGBA4444:
case Texture2DPixelFormat.RGB5A1:
colorSpace = CGColorSpace.CreateDeviceRGB();
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, 4 * (int)width, colorSpace, CGImageAlphaInfo.PremultipliedLast);
break;
case Texture2DPixelFormat.RGB565:
colorSpace = CGColorSpace.CreateDeviceRGB();
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, 4 * (int)width, colorSpace, CGImageAlphaInfo.NoneSkipLast);
break;
case Texture2DPixelFormat.A8:
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, (int)width, null, CGImageAlphaInfo.Only);
break;
default:
throw new InvalidEnumArgumentException("pixelFormat", (int)pixelFormat, typeof(Texture2DPixelFormat));
}
if (colorSpace != null) {
colorSpace.Dispose();
}
context.ClearRect(new RectangleF(0, 0, width, height));
context.TranslateCTM(0, height - image.Height);
// why is this here? make an identity transform, then immediately not use it? Need to look into this
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
if (!transform.IsIdentity) {
context.ConcatCTM(transform);
}
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
}
}
//.........这里部分代码省略.........
示例8: CreateCompatibleBitmapContext
private CGBitmapContext CreateCompatibleBitmapContext(int width, int height, PixelFormat pixelFormat)
{
int bitsPerComponent, bytesPerRow;
CGColorSpace colorSpace;
CGImageAlphaInfo alphaInfo;
bool premultiplied = false;
int bitsPerPixel = 0;
// CoreGraphics only supports a few options so we have to make do with what we have
// https://developer.apple.com/library/mac/qa/qa1037/_index.html
switch (pixelFormat)
{
case PixelFormat.Format32bppPArgb:
case PixelFormat.DontCare:
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
alphaInfo = CGImageAlphaInfo.PremultipliedLast;
break;
case PixelFormat.Format32bppArgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
alphaInfo = CGImageAlphaInfo.PremultipliedLast;
break;
case PixelFormat.Format32bppRgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
alphaInfo = CGImageAlphaInfo.PremultipliedLast;
break;
case PixelFormat.Format24bppRgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
alphaInfo = CGImageAlphaInfo.PremultipliedLast;
break;
default:
throw new Exception ("Format not supported: " + pixelFormat);
}
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
int size = bytesPerRow * height;
var bitmapBlock = Marshal.AllocHGlobal (size);
var bitmap = new CGBitmapContext (bitmapBlock,
width, height,
bitsPerComponent,
bytesPerRow,
colorSpace,
alphaInfo);
bitmap.ClearRect (new CGRect (0,0,width,height));
//colorSpace.Dispose ();
return bitmap;
}
示例9: Bitmap
public Bitmap(int width, int height, PixelFormat format)
{
imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);
int bitsPerComponent, bytesPerRow;
CGColorSpace colorSpace;
CGBitmapFlags bitmapInfo;
bool premultiplied = false;
int bitsPerPixel = 0;
pixelFormat = format;
// Don't forget to set the Image width and height for size.
imageSize.Width = width;
imageSize.Height = height;
switch (format){
case PixelFormat.Format32bppPArgb:
case PixelFormat.DontCare:
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.PremultipliedFirst;
break;
case PixelFormat.Format32bppArgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.PremultipliedFirst;
break;
case PixelFormat.Format32bppRgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
break;
case PixelFormat.Format24bppRgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
break;
default:
throw new Exception ("Format not supported: " + format);
}
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
int size = bytesPerRow * height;
bitmapBlock = Marshal.AllocHGlobal (size);
var bitmap = new CGBitmapContext (bitmapBlock,
width, height,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
// This works for now but we need to look into initializing the memory area itself
// TODO: Look at what we should do if the image does not have alpha channel
bitmap.ClearRect (new CGRect (0,0,width,height));
var provider = new CGDataProvider (bitmapBlock, size, true);
NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default);
dpiWidth = dpiHeight = ConversionHelpers.MS_DPI;
physicalDimension.Width = width;
physicalDimension.Height = height;
// The physical size may be off on certain implementations. For instance the dpiWidth and dpiHeight
// are read using integers in core graphics but in windows it is a float.
// For example:
// coregraphics dpiWidth = 24 as integer
// windows dpiWidth = 24.999935 as float
// this gives a few pixels difference when calculating the physical size.
// 256 * 96 / 24 = 1024
// 256 * 96 / 24.999935 = 983.04
//
// https://bugzilla.xamarin.com/show_bug.cgi?id=14365
// PR: https://github.com/mono/maccore/pull/57
//
physicalSize = new SizeF (physicalDimension.Width, physicalDimension.Height);
physicalSize.Width *= ConversionHelpers.MS_DPI / dpiWidth;
physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;
rawFormat = ImageFormat.MemoryBmp;
pixelFormat = format;
}
示例10: InitWithCGImage
private void InitWithCGImage(CGImage image)
{
int width, height;
CGBitmapContext bitmap = null;
bool hasAlpha;
CGImageAlphaInfo alphaInfo;
CGColorSpace colorSpace;
int bitsPerComponent, bytesPerRow;
CGBitmapFlags bitmapInfo;
bool premultiplied = false;
int bitsPerPixel = 0;
if (image == null) {
throw new ArgumentException (" image is invalid! " );
}
alphaInfo = image.AlphaInfo;
hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false);
imageSize.Width = (int)image.Width;
imageSize.Height = (int)image.Height;
width = (int)image.Width;
height = (int)image.Height;
// Not sure yet if we need to keep the original image information
// before we change it internally. TODO look at what windows does
// and follow that.
bitmapInfo = image.BitmapInfo;
bitsPerComponent = (int)image.BitsPerComponent;
bitsPerPixel = (int)image.BitsPerPixel;
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
int size = bytesPerRow * height;
colorSpace = image.ColorSpace;
// Right now internally we represent the images all the same
// I left the call here just in case we find that this is not
// possible. Read the comments for non alpha images.
if(colorSpace != null) {
if( hasAlpha ) {
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.PremultipliedLast;
}
else
{
// even for images without alpha we will internally
// represent them as RGB with alpha. There were problems
// if we do not do it this way and creating a bitmap context.
// The images were not drawing correctly and tearing. Also
// creating a Graphics to draw on was a nightmare. This
// should probably be looked into or maybe it is ok and we
// can continue representing internally with this representation
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
}
} else {
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
}
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
size = bytesPerRow * height;
bitmapBlock = Marshal.AllocHGlobal (size);
bitmap = new CGBitmapContext (bitmapBlock,
width, height,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
bitmap.ClearRect (new CGRect (0,0,width,height));
// We need to flip the Y axis to go from right handed to lefted handed coordinate system
var transform = new CGAffineTransform(1, 0, 0, -1, 0, image.Height);
bitmap.ConcatCTM(transform);
bitmap.DrawImage (new CGRect (0, 0, image.Width, image.Height), image);
var provider = new CGDataProvider (bitmapBlock, size, true);
NativeCGImage = new CGImage (width, height, bitsPerComponent,
bitsPerPixel, bytesPerRow,
colorSpace,
bitmapInfo,
provider, null, true, image.RenderingIntent);
colorSpace.Dispose();
bitmap.Dispose();
}
示例11: FromUIImage
static Texture2D FromUIImage(UIImage uiImage, string name)
{
All filter = All.Linear;
CGImage image = uiImage.CGImage;
if(uiImage == null)
throw new ArgumentNullException("uiImage");
// TODO: could use this to implement lower-bandwidth textures
//bool hasAlpha = (image.AlphaInfo == CGImageAlphaInfo.First || image.AlphaInfo == CGImageAlphaInfo.Last
// || image.AlphaInfo == CGImageAlphaInfo.PremultipliedFirst || image.AlphaInfo == CGImageAlphaInfo.PremultipliedLast);
// Image dimentions:
Point logicalSize = new Point((int)uiImage.Size.Width, (int)uiImage.Size.Height);
int pixelWidth = uiImage.CGImage.Width;
int pixelHeight = uiImage.CGImage.Height;
// Round up the target texture width and height to powers of two:
int potWidth = pixelWidth;
int potHeight = pixelHeight;
if(( potWidth & ( potWidth-1)) != 0) { int w = 1; while(w < potWidth) { w *= 2; } potWidth = w; }
if((potHeight & (potHeight-1)) != 0) { int h = 1; while(h < potHeight) { h *= 2; } potHeight = h; }
// Scale down textures that are too large...
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
while((potWidth > 1024) || (potHeight > 1024))
{
potWidth /= 2; // Note: no precision loss - it's a power of two
potHeight /= 2;
pixelWidth /= 2; // Note: precision loss - assume possibility of dropping a pixel at each step is ok
pixelHeight /= 2;
transform.Multiply(CGAffineTransform.MakeScale(0.5f, 0.5f));
}
lock(textureLoadBufferLockObject)
{
CreateTextureLoadBuffer();
unsafe
{
fixed(byte* data = textureLoadBuffer)
{
using(var colorSpace = CGColorSpace.CreateDeviceRGB())
using(var context = new CGBitmapContext(new IntPtr(data), potWidth, potHeight,
8, 4 * potWidth, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
context.ClearRect(new RectangleF(0, 0, potWidth, potHeight));
context.TranslateCTM(0, potHeight - pixelHeight); // TODO: this does not play nice with the precision-loss above (keeping half-pixel to the edge)
if(!transform.IsIdentity)
context.ConcatCTM(transform);
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
uint textureId = 0;
/*textureId = new uint[1];
textureId[0]= 0;
GL.GenTextures(1,textureId);*/
GL.GenTextures(1, ref textureId);
GL.BindTexture(All.Texture2D, textureId);
GL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)filter);
GL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)filter);
GL.TexImage2D(All.Texture2D, 0, (int)All.Rgba, (int)potWidth, (int)potHeight, 0, All.Rgba, All.UnsignedByte, new IntPtr(data));
return new Texture2D(logicalSize.X, logicalSize.Y,
pixelWidth, pixelHeight, potWidth, potHeight,
textureId, name);
}
}
}
}
}
示例12: Finalize
public bool Finalize (IMTLDevice device)
{
if (MetalTexture != null)
return true;
UIImage image = UIImage.FromFile (path);
if (image == null)
return false;
using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ()) {
if (colorSpace == null)
return false;
Width = image.CGImage.Width;
Height = image.CGImage.Height;
nuint width = (nuint)Width;
nuint height = (nuint)Height;
nuint rowBytes = width * 4;
var context = new CGBitmapContext (IntPtr.Zero,
(int)width,
(int)height,
8,
(int)rowBytes,
colorSpace,
CGImageAlphaInfo.PremultipliedLast);
if (context == null)
return false;
var bounds = new CGRect (0f, 0f, width, height);
context.ClearRect (bounds);
// Vertical Reflect
if (flip) {
context.TranslateCTM (width, height);
context.ScaleCTM (-1f, -1f);
}
context.DrawImage (bounds, image.CGImage);
MTLTextureDescriptor texDesc = MTLTextureDescriptor.CreateTexture2DDescriptor (MTLPixelFormat.RGBA8Unorm, width, height, false);
if (texDesc == null)
return false;
MetalTexture = device.CreateTexture (texDesc);
if (MetalTexture == null) {
context.Dispose ();
return false;
}
IntPtr pixels = context.Data;
if (pixels != IntPtr.Zero) {
var region = new MTLRegion ();
region.Origin.X = 0;
region.Origin.Y = 0;
region.Size.Width = (nint)width;
region.Size.Height = (nint)height;
MetalTexture.ReplaceRegion (region, 0, pixels, rowBytes);
}
context.Dispose ();
}
return true;
}
示例13: LoadTexture
// see http://deathbyalgorithm.blogspot.fr/2013/05/opentk-textures.html
public static int LoadTexture(string name, int quality, bool repeat, bool flip_y)
{
string prefix;
#if __IOS__
prefix = "OpenGLDemo.iOS.";
#endif
#if __ANDROID__
prefix = "OpenGLDemo.Droid.";
#endif
var assembly = typeof(App2).GetTypeInfo ().Assembly;
// foreach (var res in assembly.GetManifestResourceNames())
// System.Diagnostics.Debug.WriteLine("found resource: " + res);
Stream stream = assembly.GetManifestResourceStream (prefix + name + ".png");
byte[] imageData;
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
imageData = ms.ToArray();
}
#if __ANDROID__
Bitmap b = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
#elif __IOS__
UIImage image = ImageFromByteArray (imageData);
int width = (int)image.CGImage.Width;
int height = (int)image.CGImage.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
byte[] imageData2 = new byte[height * width * 4];
CGContext context = new CGBitmapContext (imageData2, width, height, 8, 4 * width, colorSpace,
CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
colorSpace.Dispose ();
context.ClearRect (new RectangleF (0, 0, width, height));
context.DrawImage (new RectangleF (0, 0, width, height), image.CGImage);
#endif
int [] textures = new int[1];
//Generate a new texture target in gl
GL.GenTextures(1, textures);
//Will bind the texture newly/empty created with GL.GenTexture
//All gl texture methods targeting Texture2D will relate to this texture
GL.BindTexture(TextureTarget.Texture2D, textures[0]);
//The reason why your texture will show up glColor without setting these parameters is actually
//TextureMinFilters fault as its default is NearestMipmapLinear but we have not established mipmapping
//We are only using one texture at the moment since mipmapping is a collection of textures pre filtered
//I'm assuming it stops after not having a collection to check.
switch (quality)
{
case 1://High quality
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Linear);
break;
//case 0:
default://Low quality
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Nearest);
break;
}
if (repeat)
{
//This will repeat the texture past its bounds set by TexImage2D
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) All.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) All.Repeat);
}
else
{
//This will clamp the texture to the edge, so manipulation will result in skewing
//It can also be useful for getting rid of repeating texture bits at the borders
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) All.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) All.ClampToEdge);
}
#if __ANDROID__
GLUtils.TexImage2D ((int) All.Texture2D, 0, b, 0);
b.Recycle();
#elif __IOS__
GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData2);
#endif
GL.BindTexture(TextureTarget.Texture2D, 0);
return textures[0];
}
示例14: LoadTexture
protected void LoadTexture(UIImage inImage)
{
var pixelsWide = inImage.CGImage.Width;
var pixelsHigh = inImage.CGImage.Height;
var bitmapBytesPerRow = pixelsWide * 4;
var bitmapByteCount = bitmapBytesPerRow * pixelsHigh;
var bitmapData = Marshal.AllocHGlobal(bitmapByteCount);
if (bitmapData == IntPtr.Zero)
{
throw new Exception("Memory not allocated.");
}
var context = new CGBitmapContext(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow,
inImage.CGImage.ColorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
if (context == null)
{
throw new Exception("Context not created");
}
var imageSize = inImage.Size;
var rect = new RectangleF(0.0f, 0.0f, imageSize.Width, imageSize.Height);
context.ClearRect(rect);
context.DrawImage(rect, inImage.CGImage);
var data = context.Data;
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)imageSize.Width,
(int)imageSize.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data);
Marshal.FreeHGlobal(data);
}