2011年3月23日水曜日

カラービットマップ

pixelDataからカラービットマップを取得
BGR > RGB の変換が必要

{
if(PhotometricInterpretation != "RGB")
    retern;
//dataSet: DicomDataSet
Bitmap bitmap;
int columns = dataSet.Columns;
int rows = dataSet.Rows;
byte[] pixelData = dataSet.PixelData;
bitmap = new Bitmap(columns, rows, PixelFormat.Format24bppRgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, columns, rows), 
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
int padding = bitmapData.Stride - columns;
byte* pBitmap = (byte*)bitmapData.Scan0;
int j = 0, i = 0;
for (int y = 0; y < rows; y++)
{
    j = y * columns;
    for (int x = 0; x < columns; x++)
    {
        i = j + x;
        pBitmap[i * 3]     = pixelData[i * 3 + 2];   //G
        pBitmap[i * 3 + 1] = pixelData[i * 3 + 1];   //B
        pBitmap[i * 3 + 2] = pixelData[i * 3];       //R
    }
    i += padding;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
.net 4.0から Parallel.For が使えるようになったので一部訂正。2015-08-12
fixed (byte* pData = pixelData)
{
    byte* rgb = pData;
    int stride = bmpData.Stride;
    Parallel.For(0, rows, y =>
    {
        for (int x = 0; x < columns; x++)
        {
            int pos = (x * 3) + y * stride;
            pBitmap[pos]     = rgb[pos + 2];   //G  
            pBitmap[pos + 1] = rgb[pos + 1];   //B
            pBitmap[pos + 2] = rgb[pos];       //R
        }
    });
}

0 件のコメント :

コメントを投稿