- C#转换的8位或16位灰度原始像素数据 [英] C# - Converting 8-bit or 16-bit grayscale raw pixel data

查看:1615
本文介绍了 - C#转换的8位或16位灰度原始像素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够为8位或16位的灰度级的像素数据转换成在.NET框架可以支持的文件格式。

I need to be able to convert 8-bit or 16-bit grayscale pixel data into a file format that the .NET framework can support.

我有可用的数据是填充在每个像素2字节的宽度,高度,方向(左下)和象素格式为4096级灰度(12位分辨率)的

The data I have available is the width, height, orientation (bottom-left) and the pixel format as 4096 shades of gray (12-bit resolution) packed in 2 bytes per pixel.

因此​​,例如,每个像素的范围从0到4096,并且每个像素是2个字节。

So for example each pixel ranges from 0 to 4096, and each pixel is 2 bytes.

我已经尝试使用PixelFormat.Format16bppGrayScale与位图构造函数,它抛出一个GDI +的异常。我已阅读的一切说,这种格式不支持,而且MSDN是错误的。

I have already tried using PixelFormat.Format16bppGrayScale with the Bitmap constructor, and it throws a GDI+ exception. Everything I have read says that this format is not supported and that MSDN is wrong.

我想这个像素缓冲区转换成.NET位图格式(如Format32bppArgb),用尽可能少的图像质量损失越好。

I want to convert this pixel buffer into a .NET Bitmap format (such as Format32bppArgb) with as little image quality loss as possible.

任何人都知道怎么样?

推荐答案

请参阅下面的例子,其中precomputes查找表(LUT),并使用该每个像素进行转换。这个版本涵盖了你的12位的情况;为8位的code是非常相似的,但它是难以跨越像素格式一概而论。

See the example below, which precomputes a lookup table (LUT) and uses that to convert each pixel. This version covers your 12-bit case; for 8-bit the code is very similar, but it is difficult to generalize across pixel formats.

这12位GS A转换,有效的8位GS将丢失数据。但是,您可以调整LUT表集中于一个较小的输入范围,更好的对比度(例如的 DICOM窗口中心/窗口宽度的)。

A conversion from 12-bit GS to effectively 8-bit GS will lose data. However, you can adjust the LUT table to focus on a smaller range of input values with better contrast (ex. DICOM Window Center/Window Width).

class Program
{
    static void Main( string[] args )
    {
        // Test driver - create a Wedge, convert to Bitmap, save to file
        //
        int width = 4095;
        int height = 1200;
        int bits = 12;

        byte[] wedge = Wedge( width, height, bits );

        Bitmap bmp = Convert( wedge, width, height, bits );

        string file = "wedge.png";

        bmp.Save( file );

        Process.Start( file );
    }

    static Bitmap Convert( byte[] input, int width, int height, int bits )
    {
        // Convert byte buffer (2 bytes per pixel) to 32-bit ARGB bitmap

        var bitmap = new Bitmap( width, height, PixelFormat.Format32bppArgb );

        var rect = new Rectangle( 0, 0, width, height );

        var lut = CreateLut( bits );

        var bitmap_data = bitmap.LockBits( rect, ImageLockMode.WriteOnly, bitmap.PixelFormat );

        ConvertCore( width, height, bits, input, bitmap_data, lut );

        bitmap.UnlockBits( bitmap_data );

        return bitmap;
    }

    static unsafe void ConvertCore( int width, int height, int bits, byte[] input, BitmapData output, uint[] lut )
    {
        // Copy pixels from input to output, applying LUT

        ushort mask = (ushort)( ( 1 << bits ) - 1 );

        int in_stride = output.Stride;
        int out_stride = width * 2;

        byte* out_data = (byte*)output.Scan0;

        fixed ( byte* in_data = input )
        {
            for ( int y = 0; y < height; y++ )
            {
                uint* out_row = (uint*)( out_data + ( y * in_stride ) );

                ushort* in_row = (ushort*)( in_data + ( y * out_stride ) );

                for ( int x = 0; x < width; x++ )
                {
                    ushort in_pixel = (ushort)( in_row[ x ] & mask );

                    out_row[ x ] = lut[ in_pixel ];
                }
            }
        }
    }

    static uint[] CreateLut( int bits )
    {
        // Create a linear LUT to convert from grayscale to ARGB

        int max_input = 1 << bits;

        uint[] lut = new uint[ max_input ];

        for ( int i = 0; i < max_input; i++ )
        {
            // map input value to 8-bit range
            //
            byte intensity = (byte)( ( i * 0xFF ) / max_input );

            // create ARGB output value A=255, R=G=B=intensity
            //
            lut[ i ] = (uint)( 0xFF000000L | ( intensity * 0x00010101L ) );
        }

        return lut;
    }

    static byte[] Wedge( int width, int height, int bits )
    {
        // horizontal wedge

        int max = 1 << bits;

        byte[] pixels = new byte[ width * height * 2 ];

        for ( int y = 0; y < height; y++ )
        {
            for ( int x = 0; x < width; x++ )
            {
                int pixel = x % max;

                int addr = ( ( y * width ) + x ) * 2;

                pixels[ addr + 1 ] = (byte)( ( pixel & 0xFF00 ) >> 8 );
                pixels[ addr + 0 ] = (byte)( ( pixel & 0x00FF ) );
            }
        }

        return pixels;
    }
}

这篇关于 - C#转换的8位或16位灰度原始像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆