Skip to content

Instantly share code, notes, and snippets.

@Redth
Created May 8, 2013 17:36
Show Gist options
  • Select an option

  • Save Redth/5542107 to your computer and use it in GitHub Desktop.

Select an option

Save Redth/5542107 to your computer and use it in GitHub Desktop.
using System.Drawing;
using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Runtime.InteropServices;
namespace ZXing
{
public partial class RGBLuminanceSource
{
/// <summary>
/// Only for compatibility to older version
/// should be replace by BitmapLuminanceSource and the faster grayscale algorithm
/// </summary>
/// <param name="d"></param>
/// <param name="W"></param>
/// <param name="H"></param>
public RGBLuminanceSource(UIImage d) : base(d.CGImage.Width, d.CGImage.Height)
{
CalculateLuminance (d);
}
private void CalculateLuminance(UIImage d)
{
var imageRef = d.CGImage;
var width = imageRef.Width;
var height = imageRef.Height;
var colorSpace = CGColorSpace.CreateDeviceRGB();
var rawData = Marshal.AllocHGlobal(height * width * 4);
try
{
var context = new CGBitmapContext(rawData, width, height, 8, 4 * width,
colorSpace, CGImageAlphaInfo.PremultipliedLast);
context.DrawImage(new RectangleF(0.0f, 0.0f, (float)width, (float)height), imageRef);
var pixelData = new byte[height * width * 4];
Marshal.Copy(rawData, pixelData, 0, pixelData.Length);
CalculateLuminance(pixelData, BitmapFormat.RGB32);
}
finally
{
Marshal.FreeHGlobal(rawData);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment