public static Bitmap Convert(Pix pix, bool includeAlpha = false) { var pixelFormat = GetPixelFormat(pix); var depth = pix.Depth; var img = new Bitmap(pix.Width, pix.Height, pixelFormat); BitmapData imgData = null; PixData pixData = null; try { // TODO: Set X and Y resolution // transfer pixel data if ((pixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) { TransferPalette(pix, img); } // transfer data pixData = pix.GetData(); imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, pixelFormat); if (depth == 32) { TransferData32(pixData, imgData, includeAlpha ? 0 : 255); } else if (depth == 16) { TransferData16(pixData, imgData); } else if (depth == 8) { TransferData8(pixData, imgData); } else if (depth == 1) { TransferData1(pixData, imgData); } return(img); } catch (Exception) { img.Dispose(); throw; } finally { if (imgData != null) { img.UnlockBits(imgData); } } }
public static Pix ToPix(Bitmap img) { int pixDepth = GetPixDepth(img.PixelFormat); Pix pix = Pix.Create(img.Width, img.Height, pixDepth); pix.XRes = (int)Math.Round(img.HorizontalResolution); pix.YRes = (int)Math.Round(img.VerticalResolution); BitmapData bitmapData = null; try { if ((img.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) { CopyColormap(img, pix); } bitmapData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat); PixData data = pix.GetData(); if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb) { TransferDataFormat32bppArgb(bitmapData, data); } else if (bitmapData.PixelFormat == PixelFormat.Format32bppRgb) { TransferDataFormat32bppRgb(bitmapData, data); } else if (bitmapData.PixelFormat == PixelFormat.Format24bppRgb) { TransferDataFormat24bppRgb(bitmapData, data); } else if (bitmapData.PixelFormat == PixelFormat.Format8bppIndexed) { TransferDataFormat8bppIndexed(bitmapData, data); } else if (bitmapData.PixelFormat == PixelFormat.Format1bppIndexed) { TransferDataFormat1bppIndexed(bitmapData, data); } return(pix); } catch (Exception) { pix.Dispose(); throw; } finally { if (bitmapData != null) { img.UnlockBits(bitmapData); } } }
private unsafe PixColor GetPixel(Pix pix, int x, int y) { var pixDepth = pix.Depth; var pixData = pix.GetData(); var pixLine = (uint *)pixData.Data + pixData.WordsPerLine * y; uint pixValue; if (pixDepth == 1) { pixValue = PixData.GetDataBit(pixLine, x); } else if (pixDepth == 4) { pixValue = PixData.GetDataQBit(pixLine, x); } else if (pixDepth == 8) { pixValue = PixData.GetDataByte(pixLine, x); } else if (pixDepth == 32) { pixValue = PixData.GetDataFourByte(pixLine, x); } else { throw new ArgumentException(String.Format("Bit depth of {0} is not supported.", pix.Depth), "pix"); } if (pix.Colormap != null) { return(pix.Colormap[(int)pixValue]); } else { if (pixDepth == 32) { return(PixColor.FromRgba(pixValue)); } else { byte grayscale = (byte)(pixValue * 255 / ((1 << 16) - 1)); return(new PixColor(grayscale, grayscale, grayscale)); } } }
private unsafe PixColor GetPixel(Pix pix, int x, int y) { var pixData = pix.GetData(); if (pix.Colormap != null) { var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; var pixValue = (int)PixData.GetDataByte(pixLine, x); return pix.Colormap[pixValue]; } else { var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; return PixColor.FromRgba(pixLine[x]); } }
private unsafe PixColor GetPixel(Pix pix, int x, int y) { var pixDepth = pix.Depth; var pixData = pix.GetData(); var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; uint pixValue; if (pixDepth == 1) { pixValue = PixData.GetDataBit(pixLine, x); } else if (pixDepth == 4) { pixValue = PixData.GetDataQBit(pixLine, x); } else if (pixDepth == 8) { pixValue = PixData.GetDataByte(pixLine, x); } else if (pixDepth == 32) { pixValue = PixData.GetDataFourByte(pixLine, x); } else { throw new ArgumentException(String.Format("Bit depth of {0} is not supported.", pix.Depth), "pix"); } if (pix.Colormap != null) { return pix.Colormap[(int)pixValue]; } else { if (pixDepth == 32) { return PixColor.FromRgba(pixValue); } else { byte grayscale = (byte)(pixValue * 255 / ((1 << 16) - 1)); return new PixColor(grayscale, grayscale, grayscale); } } }