public override void Draw(RectangleF rect) { CGContext context = UIGraphics.GetCurrentContext(); switch (this.mask) { case SVProgressHUDMask.Black: { UIColor.FromWhiteAlpha(0f, 0.5f).SetColor(); context.FillRect(this.Bounds); break; } case SVProgressHUDMask.Gradient: { float[] locations = new float[2] { 0.0f, 1.0f }; float[] colors = new float[8] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f }; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGGradient gradient = new CGGradient(colorSpace, colors, locations); colorSpace.Dispose(); PointF center = new PointF(this.Bounds.Size.Width / 2f, this.Bounds.Size.Height / 2f); float radius = Math.Min(this.Bounds.Size.Width, this.Bounds.Size.Height); context.DrawRadialGradient(gradient, center, 0, center, radius, CGGradientDrawingOptions.DrawsAfterEndLocation); gradient.Dispose(); break; } } }
public static UIColor AverageColor(this UIImage image) { CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); byte[] rgba = new byte[4]; CGContext context = new CGBitmapContext(rgba, 1, 1, 8, 4, colorSpace, CGImageAlphaInfo.PremultipliedLast); context.DrawImage(new CGRect(0, 0, 1, 1), image.CGImage); colorSpace.Dispose(); colorSpace = null; context.Dispose(); context = null; if (rgba[3] > 0) { float alpha = ((float)rgba[3]) / 255.0f; float multiplier = alpha / 255.0f; UIColor color = new UIColor(((nfloat)rgba[0]) * multiplier, ((nfloat)rgba[1]) * multiplier, ((nfloat)rgba[2]) * multiplier, alpha); return(color); } else { UIColor color = new UIColor(((nfloat)rgba[0]) * 255.0f, ((nfloat)rgba[1]) * 255.0f, ((nfloat)rgba[2]) * 255.0f, ((nfloat)rgba[3]) * 255.0f); return(color); } }
static UIImage imageWithPDFPage(CGPDFPage page, float scale, CGAffineTransform t) { if (page == null) { return(null); } RectangleF box = page.GetBoxRect(CGPDFBox.Crop); t.Scale(scale, scale); box = new RectangleF(box.Location, new SizeF(box.Size.Width * scale, box.Size.Height * scale)); var pixelWidth = box.Size.Width; CGColorSpace cs = CGColorSpace.CreateDeviceRGB(); //DebugAssert( cs ) ; var _buffer = Marshal.AllocHGlobal((int)(box.Width * box.Height)); UIGraphics.BeginImageContext(box.Size); CGContext c = UIGraphics.GetCurrentContext(); c.SaveState(); c.TranslateCTM(0f, box.Height); c.ScaleCTM(1f, -1f); cs.Dispose(); c.ConcatCTM(t); c.DrawPDFPage(page); c.RestoreState(); var image = UIGraphics.GetImageFromCurrentImageContext(); return(image); }
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(); }
public void Dispose() { if (cgdata != null) { cgdata.Dispose(); cgdata = null; } if (cgimage != null) { cgimage.Dispose(); cgimage = null; } if (image != null) { image.Dispose(); image = null; } if (colorSpace != null) { colorSpace.Dispose(); colorSpace = null; } //if (bits != IntPtr.Zero) // Marshal.FreeHGlobal (bits); bits = null; }
/// <summary> /// Returns the 2d alpha map of the image. /// </summary> /// <returns>The RGB as from image.</returns> /// <param name="image">Image.</param> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="count">Count.</param> private static float[,] GetAlphasFromImage(UIImage image, int x, int y, int count) { nint bytesPerPixel = 4; nint bitsPerComponent = 8; CGImage imageRef = image.CGImage; nint width = imageRef.Width; nint height = imageRef.Height; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); byte[] rawData = new byte[height * width * bytesPerPixel]; nint bytesPerRow = bytesPerPixel * width; CGContext context = new CGBitmapContext(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast); context.DrawImage(new CGRect(0, 0, width, height), imageRef); colorSpace.Dispose(); context.Dispose(); float[,] ret = new float[width, height]; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { ret[w, h] = rawData[bytesPerRow * h + (w * bytesPerPixel) + 3] / 255.0f; } } return(ret); }
public static CGGradient CreateGradient(List <UIColor> colors, List <float> points) { List <nfloat> colorf = new List <nfloat>(); foreach (UIColor c in colors) { nfloat r, g, b, a; c.GetRGBA(out r, out g, out b, out a); colorf.Add(r); colorf.Add(g); colorf.Add(b); colorf.Add(a); } nfloat [] components = colorf.ToArray(); nfloat[] locations = new nfloat[points.Count]; for (int i = 0; i < points.Count; i++) { locations[i] = points[i]; } CGColorSpace space = CGColorSpace.CreateDeviceRGB(); CGGradient normalGradient = new CGGradient(space, components, locations); space.Dispose(); return(normalGradient); }
public static void TexImage2D(Stream data, out int width, out int height) { data.Position = 0; var nsData = NSData.FromStream(data); UIImage image = UIImage.LoadFromData(nsData); if (image == null) { new Exception("could not load image data"); } width = image.CGImage.Width; 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); 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(); }
public static UIColor AverageColor(this UIImage image) { CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); // int bytesPerRow = (int)image.Size.Width * 4; // note that bytes per row should // //be based on width, not height. // byte[] ctxBuffer = new byte[bytesPerRow * (int)image.Size.Height]; // var previewContext = // new CGBitmapContext(ctxBuffer, (int)image.Size.Width, // (int)image.Size.Height, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.Last); byte[] rgba = new byte[4]; CGContext context = new CGBitmapContext(rgba, 1, 1, 8, 4, colorSpace, CGImageAlphaInfo.PremultipliedLast); context.DrawImage(new CGRect(0, 0, 1, 1), image.CGImage); // CGColorSpace.Release(colorSpace); colorSpace.Dispose(); colorSpace = null; context.Dispose(); context = null; // CGContextRelease(context); if (rgba[3] > 0) { float alpha = ((float)rgba[3]) / 255.0f; float multiplier = alpha / 255.0f; UIColor color = new UIColor(((nfloat)rgba[0]) * multiplier, ((nfloat)rgba[1]) * multiplier, ((nfloat)rgba[2]) * multiplier, alpha); return(color); } else { UIColor color = new UIColor(((nfloat)rgba[0]) * 255.0f, ((nfloat)rgba[1]) * 255.0f, ((nfloat)rgba[2]) * 255.0f, ((nfloat)rgba[3]) * 255.0f); return(color); } }
public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight) { UIImage newImage = null; if (null != img) { var w = img.Size.Width; var h = img.Size.Height; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGContext context = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst); context.BeginPath(); var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height); AddRoundedRectToPath(context, rect, cornerWidth, cornerHeight); context.ClosePath(); context.Clip(); var cgImage = img.CGImage; context.DrawImage(new RectangleF(0, 0, w, h), cgImage); cgImage.Dispose(); CGImage imageMasked = ((CGBitmapContext)context).ToImage(); context.Dispose(); colorSpace.Dispose(); newImage = new UIImage(imageMasked); imageMasked.Dispose(); } return(newImage); }
public static CGColor CreateColor(int red, int green, int blue, int alpha) { CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); var color = new CGColor(colorSpace, new nfloat[] { red / 255f, green / 255f, blue / 255f, alpha / 255f }); colorSpace.Dispose(); return(color); }
void CheckIndexedFile(CGImage img) { CGColorSpace cs = img.ColorSpace; Assert.That(cs.Components, Is.EqualTo((nint)1), "Components"); Assert.That(cs.Model, Is.EqualTo(CGColorSpaceModel.Indexed), "GetBaseColorSpace"); var table = cs.GetColorTable(); Assert.That(table.Length, Is.EqualTo(768), "GetColorTable"); cs.Dispose(); }
private Texture(String filename) { #if __ANDROID__ AssetManager assets = GameActivity.Instance.Assets; Bitmap bitmap = null; using (Stream stream = assets.Open(filename)) { bitmap = BitmapFactory.DecodeStream(stream); Width = bitmap.Width; Height = bitmap.Height; } GL.GenTextures(1, out _GLID); GL.BindTexture(All.Texture2D, _GLID); GL.TexParameter(All.Texture2D, All.TextureWrapS, (Int32)All.Repeat); GL.TexParameter(All.Texture2D, All.TextureWrapT, (Int32)All.Repeat); GL.TexParameter(All.Texture2D, All.TextureMinFilter, (Int32)All.Linear); GL.TexParameter(All.Texture2D, All.TextureMagFilter, (Int32)All.Linear); GLUtils.TexImage2D((Int32)All.Texture2D, 0, bitmap, 0); GL.GenerateMipmap(All.Texture2D); GL.BindTexture(All.Texture2D, 0); bitmap.Recycle(); #elif __IOS__ UIImage image = UIImage.FromFile(filename); nint cgWidth = image.CGImage.Width; nint cgHeight = image.CGImage.Height; Width = (Int32)cgWidth; Height = (Int32)cgHeight; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); Byte[] data = new Byte[Width * Height * 4]; CGContext context = new CGBitmapContext(data, cgWidth, cgHeight, 8, 4 * Width, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrderDefault); colorSpace.Dispose(); context.ClearRect(new CGRect(0, 0, cgWidth, cgHeight)); context.DrawImage(new CGRect(0, 0, cgWidth, cgHeight), image.CGImage); GL.GenTextures(1, out _GLID); GL.BindTexture(TextureTarget.Texture2D, _GLID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (Int32)All.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (Int32)All.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (Int32)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (Int32)TextureMagFilter.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data); context.Dispose(); GL.GenerateMipmap(TextureTarget.Texture2D); GL.BindTexture(TextureTarget.Texture2D, 0); #endif LoadedTextures.Add(filename, this); _Filename = filename; }
public UIImage ProcessedImage() { nfloat scale = UIScreen.MainScreen.Scale; CGImage imageRef = _originalImage.CGImage; if (imageRef == null) { return(null); } var bytesPerRow = 0; var bitsPerComponent = imageRef.BitsPerComponent; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); var bitmapInfo = imageRef.BitmapInfo; CGBitmapContext context = new CGBitmapContext(null, (nint)(_targetSize.Width * scale), (nint)(_targetSize.Height * scale), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); colorSpace.Dispose(); if (context == null) { imageRef.Dispose(); return(null); } CGRect targetFrame = LocalCropFrame(); context.InterpolationQuality = CGInterpolationQuality.High; context.SetBlendMode(CGBlendMode.Copy); context.DrawImage(targetFrame, imageRef); var contextImage = context.ToImage(); UIImage finalImage = null; context.Dispose(); if (contextImage != null) { finalImage = UIImage.FromImage(contextImage, scale, UIImageOrientation.Up); contextImage.Dispose(); } imageRef.Dispose(); return(finalImage); }
public static CGGradient CreateNormalGradient(nfloat r1, nfloat g1, nfloat b1, nfloat a1, nfloat r2, nfloat g2, nfloat b2, nfloat a2) { nfloat [] components = new nfloat[] { r1, g1, b1, a1, r2, g2, b2, a2 }; nfloat [] locations = new nfloat[] { 0f, 1f }; CGColorSpace space = CGColorSpace.CreateDeviceRGB(); CGGradient normalGradient = new CGGradient(space, components, locations); space.Dispose(); return(normalGradient); }
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(); }
private UIImage gradientImageWithSize(SizeF size, float[] locations, float[] components, int count) { UIGraphics.BeginImageContextWithOptions(size, false, 0); CGContext context = UIGraphics.GetCurrentContext(); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGGradient colorGradient = new CGGradient(colorSpace, components, locations); colorSpace.Dispose(); context.DrawLinearGradient(colorGradient, new PointF(0, 0), new PointF(size.Width, 0), 0); colorGradient.Dispose(); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
public override void Draw(RectangleF rect) { base.Draw(rect); CGContext context = UIGraphics.GetCurrentContext(); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); float step = 0.166666666666667f; float[] locations = new float[] { 0.00f, step, step *2, step *3, step *4, step *5, 1.0f }; CGColor c1 = new CGColor(1, 0, 1, 1); CGColor c2 = new CGColor(1, 1, 0, 1); CGColor c3 = new CGColor(0, 1, 1, 1); CGColor[] colors = new CGColor[] { UIColor.Red.CGColor, c1, UIColor.Blue.CGColor, c3, UIColor.Green.CGColor, c2, UIColor.Red.CGColor }; CGGradient gradiend = new CGGradient(colorSpace, colors, locations); context.DrawLinearGradient(gradiend, new PointF(rect.Size.Width, 0), new PointF(0, 0), CGGradientDrawingOptions.DrawsBeforeStartLocation); gradiend.Dispose(); colorSpace.Dispose(); } // draw
public void SetOriginalImage(UIImage originalImage, CGRect cropFrame) { LoadIndicator.StartAnimating(); InvokeOnMainThread(() => { CGImage imageRef = originalImage.CGImage; UIImageOrientation imageOrientation = originalImage.Orientation; if (imageRef == null) { return; } var bytesPerRow = 0; var width = imageRef.Width; var height = imageRef.Height; var bitsPerComponent = imageRef.BitsPerComponent; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGBitmapFlags bitmapInfo = imageRef.BitmapInfo; switch (imageOrientation) { case UIImageOrientation.RightMirrored: case UIImageOrientation.LeftMirrored: case UIImageOrientation.Right: case UIImageOrientation.Left: width = imageRef.Height; height = imageRef.Width; break; default: break; } CGSize imageSize = new CGSize(width, height); CGBitmapContext context = new CGBitmapContext(null, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); colorSpace.Dispose(); if (context == null) { imageRef.Dispose(); return; } switch (imageOrientation) { case UIImageOrientation.RightMirrored: case UIImageOrientation.Right: context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2); context.RotateCTM(-((nfloat)Math.PI / 2)); context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2); break; case UIImageOrientation.LeftMirrored: case UIImageOrientation.Left: context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2); context.RotateCTM((nfloat)(Math.PI / 2)); context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2); break; case UIImageOrientation.Down: case UIImageOrientation.DownMirrored: context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2); context.RotateCTM((nfloat)Math.PI); context.TranslateCTM(-imageSize.Width / 2, -imageSize.Height / 2); break; default: break; } context.InterpolationQuality = CGInterpolationQuality.High; context.SetBlendMode(CGBlendMode.Copy); context.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef); CGImage contextImage = context.ToImage(); context.Dispose(); if (contextImage != null) { _originalImage = UIImage.FromImage(contextImage, originalImage.CurrentScale, UIImageOrientation.Up); contextImage.Dispose(); } imageRef.Dispose(); BeginInvokeOnMainThread(() => { CGSize convertedImageSize = new CGSize(_originalImage.Size.Width / _cropSizeRatio, _originalImage.Size.Height / _cropSizeRatio); ImageView.Alpha = 0; ImageView.Image = _originalImage; CGSize sampleImageSize = new CGSize(Math.Max(convertedImageSize.Width, ScrollView.Frame.Size.Width), Math.Max(convertedImageSize.Height, ScrollView.Frame.Size.Height)); ScrollView.MinimumZoomScale = 1; ScrollView.MaximumZoomScale = 1; ScrollView.ZoomScale = 1; ImageView.Frame = new CGRect(0, 0, convertedImageSize.Width, convertedImageSize.Height); nfloat zoomScale = 1; if (convertedImageSize.Width < convertedImageSize.Height) { zoomScale = (ScrollView.Frame.Size.Width / convertedImageSize.Width); } else { zoomScale = (ScrollView.Frame.Size.Height / convertedImageSize.Height); } ScrollView.ContentSize = sampleImageSize; if (zoomScale < 1) { ScrollView.MinimumZoomScale = zoomScale; ScrollView.MaximumZoomScale = 1; ScrollView.ZoomScale = zoomScale; } else { ScrollView.MinimumZoomScale = zoomScale; ScrollView.MaximumZoomScale = zoomScale; ScrollView.ZoomScale = zoomScale; } ScrollView.ContentInset = UIEdgeInsets.Zero; ScrollView.ContentOffset = new CGPoint((ImageView.Frame.Size.Width - ScrollView.Frame.Size.Width) / 2, (ImageView.Frame.Size.Height - ScrollView.Frame.Size.Height) / 2); if (cropFrame.Size.Width > 0 && cropFrame.Size.Height > 0) { nfloat scale = UIScreen.MainScreen.Scale; nfloat newZoomScale = (_targetSize.Width * scale) / cropFrame.Size.Width; ScrollView.ZoomScale = newZoomScale; nfloat heightAdjustment = (_targetSize.Height / _cropSizeRatio) - ScrollView.ContentSize.Height; nfloat offsetY = cropFrame.Y + (heightAdjustment * _cropSizeRatio * scale); ScrollView.ContentOffset = new CGPoint(cropFrame.X / scale / _cropSizeRatio, (offsetY / scale / _cropSizeRatio) - heightAdjustment); } ScrollView.SetNeedsLayout(); UIView.Animate(0.3, () => { LoadIndicator.Alpha = 0; ImageView.Alpha = 1; }, () => { LoadIndicator.StopAnimating(); }); }); }); }
public void Dispose() { _context.Dispose(); _colorSpace.Dispose(); _handle.Free(); }
public override void Draw(RectangleF rectB) { CGColorSpace cs = null; CGContext ctx = null; RectangleF bds; ctx = UIGraphics.GetCurrentContext(); cs = CGColorSpace.CreateDeviceRGB(); if (Vertical) { ctx.TranslateCTM(0, Bounds.Height); ctx.ScaleCTM(1, -1); bds = Bounds; } else { ctx.TranslateCTM(0, Bounds.Height); ctx.RotateCTM(-(float)Math.PI / 2); bds = new RectangleF(0, 0, Bounds.Height, Bounds.Width); } ctx.SetFillColorSpace(cs); ctx.SetStrokeColorSpace(cs); if (NumLights == 0) { float currentTop = 0; if (BgColor != null) { BgColor.SetColor(); ctx.FillRect(bds); } foreach (var thisTresh in ColorThresholds) { var val = Math.Min(thisTresh.MaxValue, Level); var rect = new RectangleF(0, bds.Height * currentTop, bds.Width, bds.Height * (val - currentTop)); thisTresh.Color.SetColor(); ctx.FillRect(rect); if (Level < thisTresh.MaxValue) { break; } currentTop = val; } if (BorderColor != null) { BorderColor.SetColor(); bds.Inflate(-0.5f, -0.5f); ctx.StrokeRect(bds); } } else { float lightMinVal = 0; float insetAmount, lightVSpace; int peakLight = -1; lightVSpace = bds.Height / (float)NumLights; if (lightVSpace < 4) { insetAmount = 0; } else if (lightVSpace < 8) { insetAmount = 0.5f; } else { insetAmount = 1; } if (PeakLevel > 0) { peakLight = (int)PeakLevel * NumLights; if (peakLight >= NumLights) { peakLight = NumLights - 1; } } for (int light_i = 0; light_i < NumLights; light_i++) { float lightMaxVal = (light_i + 1) / (float)NumLights; float lightIntensity; RectangleF lightRect; UIColor lightColor; if (light_i == peakLight) { lightIntensity = 1; } else { lightIntensity = (Level - lightMinVal) / (lightMaxVal - lightMinVal); lightIntensity = Clamp(0, lightIntensity, 1); if (!VariableLightIntensity && lightIntensity > 0) { lightIntensity = 1; } } lightColor = ColorThresholds [0].Color; int color_i = 0; for (; color_i < ColorThresholds.Length - 1; color_i++) { var thisTresh = ColorThresholds [color_i]; var nextTresh = ColorThresholds [color_i + 1]; if (thisTresh.MaxValue <= lightMaxVal) { //Console.WriteLine ("PICKED COLOR at {0}", color_i); lightColor = nextTresh.Color; } } lightRect = new RectangleF(0, bds.Height * light_i / (float)NumLights, bds.Width, bds.Height * (1f / NumLights)); lightRect.Inset(insetAmount, insetAmount); if (BgColor != null) { BgColor.SetColor(); ctx.FillRect(lightRect); } //Console.WriteLine ("Got: {0} {1}", lightColor, UIColor.Red); //lightColor = UIColor.Red; if (lightIntensity == 1) { lightColor.SetColor(); //Console.WriteLine ("Setting color to {0}", lightColor); ctx.FillRect(lightRect); } else if (lightIntensity > 0) { using (var clr = new CGColor(lightColor.CGColor, lightIntensity)){ ctx.SetFillColorWithColor(clr); ctx.FillRect(lightRect); } } if (BorderColor != null) { BorderColor.SetColor(); lightRect.Inset(0.5f, 0.5f); ctx.StrokeRect(lightRect); } lightMinVal = lightMaxVal; } } cs.Dispose(); }
// 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]); }