Ejemplo n.º 1
0
        /// <summary>
        /// Convert Image to grayscale indexed image (like GIF)
        /// </summary>
        /// <param name="originalImage"></param>
        /// <returns></returns>
        public static QImage ConvertToGrayScaleIndexed(QImage originalImage)
        {
            var newImage = new QImage(originalImage.Size, QImage.Format.FormatGrayscale8);

            newImage.ColorCount = 256;
            for (int i = 0; i < 256; i++)
            {
                newImage.SetColor(i, qrgb.QRgb(i, i, i));
            }


            // Add color table for image
            // [looking at example](http://stackoverflow.com/questions/5504768/how-to-use-a-color-lut-in-qt-qimage)

            for (int x = 0; x < originalImage.Width; x++)
            {
                for (int y = 0; y < originalImage.Height; y++)
                {
                    var point = new QPoint(x, y); // pixel position

                    UInt32 pixel = originalImage.Pixel(point);
                    var    gray  = qrgb.QGray(pixel);
                    newImage.SetPixel(point, qrgb.QRgb(gray, gray, gray));
                }
            }
            //newImage.Save ( @"d:\dump.png", "png" );
            return(newImage);
        }