public override void Initialize() { base.Initialize(); PImage original = new PImage(Resources.__2); original.X = 0; original.Y = 0; Canvas.Layer.AddChild(original); PText origLabel = new PText("Original, Size: " + Resources._2.Length); origLabel.ConstrainWidthToTextWidth = false; origLabel.SetBounds(0, Resources.__2.Height, Resources.__2.Width, 50); origLabel.TextAlignment = StringAlignment.Center; Canvas.Layer.AddChild(origLabel); AreaSample area = new AreaSample(Resources.__2); PImage areaSampled = new PImage(area.toBitmap()); areaSampled.X = original.Width; areaSampled.Y = 0; Canvas.Layer.AddChild(areaSampled); PText comLabel = new PText("Area sampled, Size: " + area.size + ", Ratio: " + ((float)area.size / Resources._2.Length)); comLabel.ConstrainWidthToTextWidth = false; comLabel.SetBounds(origLabel.Bounds.Right, origLabel.Y, areaSampled.Width, 50); comLabel.TextAlignment = StringAlignment.Center; Canvas.Layer.AddChild(comLabel); AreaDownSample ad = new AreaDownSample(Resources.__2); PImage adown = new PImage(ad.toBitmap()); adown.X = areaSampled.Bounds.Right; adown.Y = 0; Canvas.Layer.AddChild(adown); PText adLabel = new PText("Area down sampled, Size: " + ad.size + ", Ratio: " + ((float)ad.size / Resources._2.Length)); adLabel.ConstrainWidthToTextWidth = false; adLabel.SetBounds(comLabel.Bounds.Right, origLabel.Y, areaSampled.Width, 50); adLabel.TextAlignment = StringAlignment.Center; Canvas.Layer.AddChild(adLabel); RunLengthEncode run = new RunLengthEncode(Resources.__2); PImage runlen = new PImage(run.toBitmap()); runlen.X = adown.Bounds.Right; runlen.Y = 0; Canvas.Layer.AddChild(runlen); PText runLabel = new PText("Run length encoded, Size: " + ad.size + ", Ratio: " + ((float)run.size / Resources._2.Length)); runLabel.ConstrainWidthToTextWidth = false; runLabel.SetBounds(adLabel.Bounds.Right, origLabel.Y, areaSampled.Width, 50); runLabel.TextAlignment = StringAlignment.Center; Canvas.Layer.AddChild(runLabel); }
public AreaDownSample(AreaSample input) { float relativeCompression = bytesPerPixel / AreaSample.bytesPerPixel; int imSize = (int)Math.Ceiling((input.ImageData.Length - AreaSample.leadingBytes) * relativeCompression + leadingBytes); ImageData = new byte[imSize]; Array.Copy(input.ImageData, ImageData, AreaSample.leadingBytes); int pos = leadingBytes; for (int i = AreaSample.leadingBytes; i < input.ImageData.Length; i += AreaSample.bytesPerPixel) { int red = input.ImageData[i] / sampleRate; int green = input.ImageData[i + 1] / sampleRate; int blue = input.ImageData[i + 2] / sampleRate; //If the next group starts at the beginning of a byte if (i % 2 == 0) { //The first byte is Red Green ImageData[pos] = (byte)(red * toTop + green); //Ensure the top half of the second byte is empty ImageData[pos + 1] &= bottomBits; //The second byte is Blue Unknown ImageData[pos + 1] |= (byte)(blue * toTop); //Increment the position by 1 pos += 1; } //If the next group starts at the end of a byte else { //Ensure the bottom half of the first byte is empty ImageData[pos] &= topBits; //The first byte is unknown red ImageData[pos] |= (byte)red; //The second byte is green blue ImageData[pos + 1] = (byte)(green * toTop + blue); //Increment the position by 2 pos += 2; } } }