Example #1
0
        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 RunLengthEncode(AreaDownSample input)
        {
            List<byte> newFile = new List<byte>();
            //Add the effective dimensions as a header
            newFile.AddRange(BitConverter.GetBytes(input.Width / AreaSample.area));
            newFile.AddRange(BitConverter.GetBytes(input.Height / AreaSample.area));

            //Initialise lists to store each channel's information
            List<byte>[] channels = new List<byte>[3];
            for (int i = 0; i < 3; i += 1)
            {
                channels[i] = new List<byte>();
            }

            //Consider each byte of the input image
            for (int i = 4; i < input.ImageData.Length; i += 1)
            {
                //Split it in to the two consituent colours
                byte cur = input.ImageData[i];
                byte top = (byte)((cur & topBits) / toTop);
                byte bottom = (byte)(cur & bottomBits);
                int channel1 = (2 * (i - 4)) % 3;
                int channel2 = (channel1 + 1) % 3;

                //Save them to the appropriate channel
                channels[channel1].Add(top);
                channels[channel2].Add(bottom);
            }

            List<byte>[] compressedChannels = new List<byte>[3];
            for (int i = 0; i < 3; i += 1)
            {
                compressedChannels[i] = new List<byte>();
                byte last = channels[i][0];
                int run = 0;
                for (int j = 0; j < channels[i].Count; j++)
                {
                    byte cur = channels[i][j];
                    if ((run > 0 && last != cur) || run == 15 || j == channels[i].Count - 1)
                    {
                        byte item = (byte)(run * toTop + last);
                        compressedChannels[i].Add(item);
                        run = 0;
                    }
                    run += 1;
                    last = cur;
                }
                newFile.AddRange(compressedChannels[i]);
            }
            ImageData = newFile.ToArray();
        }