Пример #1
0
        public static void ReadXYZFormat(string filePath, MagnitudeRgbConversionMode mode = MagnitudeRgbConversionMode.monochromatic, bool zeroCentered = false)
        {
            var file     = IoHelpers.GetFile(filePath);
            var data     = GetXyzData(filePath);
            var rgbArray = ConvertToRgbArray(data, mode);

            rgbArray.SaveRgbArrayAsBmpImage(Path.Combine(file.DirectoryName, $"{Path.GetFileNameWithoutExtension(file.Name)}-read_output.bmp"), zeroCentered);
        }
Пример #2
0
        public static RgbArray ConvertToRGBArray(this Bitmap bmp, MagnitudeRgbConversionMode colorMode = MagnitudeRgbConversionMode.realistic)
        {
            //https://stackoverflow.com/a/1563170

            var        result = new Color[bmp.Height][];
            BitmapData bData  = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            /* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
            byte bitsPerPixel = Convert.ToByte(GetBitsPerPixel(bData.PixelFormat));

            if (bitsPerPixel <= 8)
            {
                Console.WriteLine("WARNING, WILL BLOW UP, currently supportert bmp formats with 8b/pixel or more");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            else if (bitsPerPixel != 24)
            {
                Console.WriteLine("WARNING, MIGHT BLOW UP, currently tested only with 24bit bmp, should work with 16bit/pixel and higher");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }

            /*the size of the image in bytes */
            // stride = width * bytesPerpixel + (width % 4) - 4 because it needs to be 32-bit adressable
            int size = bData.Stride * bData.Height;

            /*Allocate buffer for image*/
            byte[] data = new byte[size];

            /*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
            System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, data, 0, size);
            var bytesPerPixel = bitsPerPixel / 8;

            // bmp format scans starts at bottom left and goes to top right
            // data[i] is the first of 3 bytes of color
            // colors are in BGR format
            int x = 0;

            for (int y = bmp.Height - 1; y >= 0; y--)
            {
                result[y] = new Color[bmp.Width];
                for (x = 0; x < bmp.Width; x++)
                {
                    var strideOverhand = bmp.Width % 4;
                    var widthTotal     = (bmp.Width * bytesPerPixel + strideOverhand);
                    var pixelStart     = y * widthTotal + x * bytesPerPixel;
                    result[y][x] = GetColor(data[pixelStart + 2], data[pixelStart + 1], data[pixelStart], colorMode);
                }
            }

            /* This override copies the data back into the location specified */
            System.Runtime.InteropServices.Marshal.Copy(data, 0, bData.Scan0, data.Length);

            bmp.UnlockBits(bData);

            return(new RgbArray(result));
        }
Пример #3
0
        public static Color GetColor(int magnitude, MagnitudeRgbConversionMode conversionMode)
        {
            switch (conversionMode)
            {
            case MagnitudeRgbConversionMode.monochromatic:
                return(Color.FromArgb(magnitude, magnitude, magnitude));

            case MagnitudeRgbConversionMode.scaledHue:
                throw new NotImplementedException("Future development");

            case MagnitudeRgbConversionMode.realistic:
            default:
                throw new NotImplementedException("Future development");
            }
        }
Пример #4
0
        public static Color GetColor(int r, int g, int b, MagnitudeRgbConversionMode conversionMode)
        {
            switch (conversionMode)
            {
            case MagnitudeRgbConversionMode.monochromatic:
                var magnitude = (int)CS152Helpers.GetMagnitude(r, g, b);
                return(Color.FromArgb(magnitude, magnitude, magnitude));

            case MagnitudeRgbConversionMode.scaledHue:
                throw new NotImplementedException("Future development");

            case MagnitudeRgbConversionMode.realistic:
            default:
                return(Color.FromArgb(r, g, b));
            }
        }
Пример #5
0
        public static RgbArray ConvertToRgbArray(SortedDictionary <float, SortedDictionary <float, float> > xyzData, MagnitudeRgbConversionMode conversionMode)
        {
            EnsureRectangleSymmetry(xyzData);

            var xyzDataEnum = xyzData.GetEnumerator();
            var firstRow    = xyzData.First();

            var height   = xyzData.Count;
            var width    = firstRow.Value.Count;
            var rgbArray = new Color[height][];
            int count    = 0;

            while (xyzDataEnum.MoveNext())
            {
                var row = xyzDataEnum.Current;
                var y   = count / width;
                rgbArray[height - y - 1] = new Color[width];

                var rowEnum = row.Value.GetEnumerator();

                while (rowEnum.MoveNext())
                {
                    var x               = count % width;
                    var pixel           = rowEnum.Current;
                    var scaledMagnitude = (int)CS152Helpers.ScaleValue(pixel.Value, -11000, 8500, 0, 255);
                    rgbArray[height - y - 1][width - x - 1] = Bitmapper.GetColor(scaledMagnitude, conversionMode);
                    count++;
                }
            }

            return(new RgbArray(rgbArray));
        }