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 static unsafe void TransferData32(PixData pixData, BitmapData imgData, int alphaMask) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; var width = imgData.Width; for (int y = 0; y < height; y++) { byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); for (int x = 0; x < width; x++) { var pixVal = PixColor.FromRgba(pixLine[x]); byte *pixelPtr = imgLine + (x << 2); pixelPtr[0] = pixVal.Blue; pixelPtr[1] = pixVal.Green; pixelPtr[2] = pixVal.Red; pixelPtr[3] = (byte)(alphaMask | pixVal.Alpha); // Allow user to include alpha or not } } }