Beispiel #1
0
        private void GetMetaColors(object sender, RoutedEventArgs e)
        {
            var imageLength  = ViewModel.Properties.PixelHeight * ViewModel.Properties.PixelWidth * 3;
            var imagePtr     = Marshal.AllocHGlobal(imageLength);
            var outColorsPtr = Marshal.AllocHGlobal(ViewModel.MetaArgs.MetaCount * 3);
            var pixels       = ViewModel.Image.GetPixels();

            for (var index = 0; index < pixels.Length; index++)
            {
                var pixel = pixels[index];
                Marshal.WriteByte(imagePtr, index * 3 + 2, (byte)pixel);        // B
                Marshal.WriteByte(imagePtr, index * 3 + 1, (byte)(pixel >> 8)); // G
                Marshal.WriteByte(imagePtr, index * 3, (byte)(pixel >> 16));    // R
            }
            MetaHelper.meta_calculate(imagePtr, imageLength, ViewModel.MetaArgs.MetaCount, outColorsPtr,
                                      ViewModel.MetaArgs.MaxError);
            ViewModel.MetaColors.Clear();
            for (var index = 0; index < ViewModel.MetaArgs.MetaCount; index++)
            {
                var r = Marshal.ReadByte(outColorsPtr, index * 3);
                var g = Marshal.ReadByte(outColorsPtr, index * 3 + 1);
                var b = Marshal.ReadByte(outColorsPtr, index * 3 + 2);
                ViewModel.MetaColors.Add(Color.FromRgb(r, g, b));
            }
            Marshal.FreeHGlobal(imagePtr);
            Marshal.FreeHGlobal(outColorsPtr);
        }