public void CanReadAndWriteData(int depth) { using (var pix = Pix.Create(Width, Height, depth)) { var pixData = pix.GetData(); for (var y = 0; y < Height; y++) { var line = (uint *)pixData.Data + (y * pixData.WordsPerLine); for (var x = 0; x < Width; x++) { var val = (uint)((y * Width + x) % (1 << depth)); uint readVal; if (depth == 1) { PixData.SetDataBit(line, x, val); readVal = PixData.GetDataBit(line, x); } else if (depth == 2) { PixData.SetDataDIBit(line, x, val); readVal = PixData.GetDataDIBit(line, x); } else if (depth == 4) { PixData.SetDataQBit(line, x, val); readVal = PixData.GetDataQBit(line, x); } else if (depth == 8) { PixData.SetDataByte(line, x, val); readVal = PixData.GetDataByte(line, x); } else if (depth == 16) { PixData.SetDataTwoByte(line, x, val); readVal = PixData.GetDataTwoByte(line, x); } else if (depth == 32) { PixData.SetDataFourByte(line, x, val); readVal = PixData.GetDataFourByte(line, x); } else { throw new NotSupportedException(); } Assert.AreEqual(readVal, val); } } } }
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)); } } }