public override INifti <float> AddOverlay(INifti <float> overlay) { NiftiFloat32 output = (NiftiFloat32)(this.DeepCopy()); // Caclulate conversion to colour map. var range = Header.cal_max - Header.cal_min; var scale = ColorMap.Length / range; var bias = -(Header.cal_min); // Caclulate conversion to colour map. var rangeOverlay = overlay.Header.cal_max - overlay.Header.cal_min; var scaleOverlay = overlay.ColorMap.Length / rangeOverlay; var biasOverlay = -(overlay.Header.cal_min); for (int i = 0; i < Voxels.Length; ++i) { // Get the colour map index for our value. int idx = (int)((Voxels[i] + bias) * scale); if (idx < 0) { idx = 0; } else if (idx > ColorMap.Length - 1) { idx = ColorMap.Length - 1; } // Get the colour map value for the overlay int idxOverlay = (int)((overlay.Voxels[i] + biasOverlay) * scaleOverlay); if (idxOverlay < 0) { idxOverlay = 0; } else if (idxOverlay > overlay.ColorMap.Length - 1) { idxOverlay = overlay.ColorMap.Length - 1; } var red = (overlay.ColorMap[idxOverlay].R * overlay.ColorMap[idxOverlay].A + ColorMap[idx].R * (255 - overlay.ColorMap[idxOverlay].A)) / 255; var green = (overlay.ColorMap[idxOverlay].G * overlay.ColorMap[idxOverlay].A + ColorMap[idx].G * (255 - overlay.ColorMap[idxOverlay].A)) / 255; var blue = (overlay.ColorMap[idxOverlay].B * overlay.ColorMap[idxOverlay].A + ColorMap[idx].B * (255 - overlay.ColorMap[idxOverlay].A)) / 255; output.Voxels[i] = Convert.ToUInt32((byte)red << 16 | (byte)green << 8 | (byte)blue); } output.ConvertHeaderToRgb(); output.RecalcHeaderMinMax(); return(output); }
public override INifti <float> DeepCopy() { var copy = new NiftiFloat32() { Header = Header.DeepCopy(), Voxels = new float[Voxels.Length] }; Voxels.CopyTo(copy.Voxels, 0); copy.ColorMap = new Color[ColorMap.Length]; ColorMap.CopyTo(copy.ColorMap, 0); return(copy); }