/// <summary> /// Create a new blank image /// </summary> /// <param name="width">width</param> /// <param name="height">height</param> /// <param name="channels">channel flags</param> public ManagedImage(int width, int height, ImageChannels channels) { Width = width; Height = height; Channels = channels; int n = width * height; if ((channels & ImageChannels.Gray) != 0) { Red = new byte[n]; } else if ((channels & ImageChannels.Color) != 0) { Red = new byte[n]; Green = new byte[n]; Blue = new byte[n]; } if ((channels & ImageChannels.Alpha) != 0) { Alpha = new byte[n]; } if ((channels & ImageChannels.Bump) != 0) { Bump = new byte[n]; } }
public static void *ImageLoadData(IntPtr handle, ImageChannels channels) { byte *data; _ImageLoadData(handle.ToPointer(), &data, channels); return(data); }
public static void *ImageGetLoadedData(IntPtr handle, out ImageChannels channels) { byte * data; ImageChannels c; _ImageGetLoadedData(handle.ToPointer(), &data, &c); channels = c; return(data); }
/// <summary> /// Convert the channels in the image. Channels are created or destroyed as required. /// </summary> /// <param name="channels">new channel flags</param> public void ConvertChannels(ImageChannels channels) { if (Channels == channels) { return; } int n = Width * Height; ImageChannels add = Channels ^ channels & channels; ImageChannels del = Channels ^ channels & Channels; if ((add & ImageChannels.Color) != 0) { Red = new byte[n]; Green = new byte[n]; Blue = new byte[n]; } else if ((del & ImageChannels.Color) != 0) { Red = null; Green = null; Blue = null; } if ((add & ImageChannels.Alpha) != 0) { Alpha = new byte[n]; FillArray(Alpha, 255); } else if ((del & ImageChannels.Alpha) != 0) { Alpha = null; } if ((add & ImageChannels.Bump) != 0) { Bump = new byte[n]; } else if ((del & ImageChannels.Bump) != 0) { Bump = null; } Channels = channels; }
/// <summary> /// Create a new blank image /// </summary> /// <param name="width">width</param> /// <param name="height">height</param> /// <param name="channels">channel flags</param> public Image(int width, int height, ImageChannels channels) { Width = width; Height = height; Channels = channels; int n = width * height; if ((channels & ImageChannels.Color) != 0) { Red = new byte[n]; Green = new byte[n]; Blue = new byte[n]; } if ((channels & ImageChannels.Bump) != 0) Bump = new byte[n]; if ((channels & ImageChannels.Alpha) != 0) Alpha = new byte[n]; }
/// <summary> /// Convert the channels in the image. Channels are created or destroyed as required. /// </summary> /// <param name="channels">new channel flags</param> public void ConvertChannels(ImageChannels channels) { if (Channels == channels) return; int n = Width * Height; ImageChannels add = Channels ^ channels & channels; ImageChannels del = Channels ^ channels & Channels; if ((add & ImageChannels.Color) != 0) { Red = new byte[n]; Green = new byte[n]; Blue = new byte[n]; } else if ((del & ImageChannels.Color) != 0) { Red = null; Green = null; Blue = null; } if ((add & ImageChannels.Alpha) != 0) { Alpha = new byte[n]; FillArray(Alpha, 255); } else if ((del & ImageChannels.Alpha) != 0) Alpha = null; if ((add & ImageChannels.Bump) != 0) Bump = new byte[n]; else if ((del & ImageChannels.Bump) != 0) Bump = null; Channels = channels; }
/// <summary> /// /// </summary> /// <param name="bitmap"></param> public ManagedImage(System.Drawing.Bitmap bitmap) { Width = bitmap.Width; Height = bitmap.Height; int pixelCount = Width * Height; if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { Channels = ImageChannels.Alpha | ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; Alpha = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); unsafe { byte* pixel = (byte*)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGRA and we need to turn that in to RGBA Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); Alpha[i] = *(pixel++); } } bitmap.UnlockBits(bd); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format16bppGrayScale) { Channels = ImageChannels.Gray; Red = new byte[pixelCount]; throw new NotImplementedException("16bpp grayscale image support is incomplete"); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) { Channels = ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pixel = (byte*)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGR and we need to turn that in to RGB Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); } } bitmap.UnlockBits(bd); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb) { Channels = ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); unsafe { byte* pixel = (byte*)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGR and we need to turn that in to RGB Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); pixel++; // Skip over the empty byte where the Alpha info would normally be } } bitmap.UnlockBits(bd); } else { throw new NotSupportedException("Unrecognized pixel format: " + bitmap.PixelFormat.ToString()); } }
private void StartChannelButton_MouseDown(object sender, MouseButtonEventArgs e) { ImageChannels.GetChannels(_sourceImagePath, _resultFolderPath); MessageBox.Show("Finished!", "Result", MessageBoxButtons.OK); }
/// <summary> /// /// </summary> /// <param name="bitmap"></param> public ManagedImage(System.Drawing.Bitmap bitmap) { Width = bitmap.Width; Height = bitmap.Height; int pixelCount = Width * Height; if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { Channels = ImageChannels.Alpha | ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; Alpha = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); unsafe { byte *pixel = (byte *)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGRA and we need to turn that in to RGBA Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); Alpha[i] = *(pixel++); } } bitmap.UnlockBits(bd); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format16bppGrayScale) { Channels = ImageChannels.Gray; Red = new byte[pixelCount]; throw new NotImplementedException("16bpp grayscale image support is incomplete"); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) { Channels = ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte *pixel = (byte *)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGR and we need to turn that in to RGB Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); } } bitmap.UnlockBits(bd); } else if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb) { Channels = ImageChannels.Color; Red = new byte[pixelCount]; Green = new byte[pixelCount]; Blue = new byte[pixelCount]; System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); unsafe { byte *pixel = (byte *)bd.Scan0; for (int i = 0; i < pixelCount; i++) { // GDI+ gives us BGR and we need to turn that in to RGB Blue[i] = *(pixel++); Green[i] = *(pixel++); Red[i] = *(pixel++); pixel++; // Skip over the empty byte where the Alpha info would normally be } } bitmap.UnlockBits(bd); } else { throw new NotSupportedException("Unrecognized pixel format: " + bitmap.PixelFormat.ToString()); } }