2011年3月23日水曜日

カラービットマップ

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

  1. {
  2. if(PhotometricInterpretation != "RGB")
  3. retern;
  4. //dataSet: DicomDataSet
  5. Bitmap bitmap;
  6. int columns = dataSet.Columns;
  7. int rows = dataSet.Rows;
  8. byte[] pixelData = dataSet.PixelData;
  9. bitmap = new Bitmap(columns, rows, PixelFormat.Format24bppRgb);
  10. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, columns, rows),
  11. ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  12. int padding = bitmapData.Stride - columns;
  13. byte* pBitmap = (byte*)bitmapData.Scan0;
  14. int j = 0, i = 0;
  15. for (int y = 0; y < rows; y++)
  16. {
  17. j = y * columns;
  18. for (int x = 0; x < columns; x++)
  19. {
  20. i = j + x;
  21. pBitmap[i * 3] = pixelData[i * 3 + 2]; //G
  22. pBitmap[i * 3 + 1] = pixelData[i * 3 + 1]; //B
  23. pBitmap[i * 3 + 2] = pixelData[i * 3]; //R
  24. }
  25. i += padding;
  26. }
  27. bitmap.UnlockBits(bitmapData);
  28. return bitmap;
  29. }
.net 4.0から Parallel.For が使えるようになったので一部訂正。2015-08-12
  1. fixed (byte* pData = pixelData)
  2. {
  3. byte* rgb = pData;
  4. int stride = bmpData.Stride;
  5. Parallel.For(0, rows, y =>
  6. {
  7. for (int x = 0; x < columns; x++)
  8. {
  9. int pos = (x * 3) + y * stride;
  10. pBitmap[pos] = rgb[pos + 2]; //G
  11. pBitmap[pos + 1] = rgb[pos + 1]; //B
  12. pBitmap[pos + 2] = rgb[pos]; //R
  13. }
  14. });
  15. }

0 件のコメント :

コメントを投稿