private static unsafe void TransferDataFormat1bppIndexed(BitmapData imgData, PixData pixData) { var height = imgData.Height; var width = imgData.Width / 8; for (var y = 0; y < height; y++) { var imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); var pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); for (var x = 0; x < width; x++) { var pixelVal = BitmapHelper.GetDataByte(imgLine, x); PixData.SetDataByte(pixLine, x, pixelVal); } } }
private static unsafe void TransferDataFormat32bppRgb(BitmapData imgData, PixData pixData) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; var width = imgData.Width; for (var y = 0; y < height; y++) { var imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); var pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); for (var x = 0; x < width; x++) { var pixelPtr = imgLine + (x << 2); var blue = *pixelPtr; var green = *(pixelPtr + 1); var red = *(pixelPtr + 2); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255)); } } }
private unsafe void TransferDataFormat24bppRgb(BitmapData imgData, PixData pixData) { 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++) { byte *pixelPtr = imgLine + x * 3; byte blue = pixelPtr[0]; byte green = pixelPtr[1]; byte red = pixelPtr[2]; PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255)); } } }
private unsafe void TransferDataFormat32bppArgb(BitmapData imgData, PixData pixData) { 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++) { byte *pixelPtr = imgLine + (x << 2); byte blue = *pixelPtr; byte green = *(pixelPtr + 1); byte red = *(pixelPtr + 2); byte alpha = *(pixelPtr + 3); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, alpha)); } } }
private unsafe void TransferData(BitmapData imgData, PixData pixData) { var imgFormat = imgData.PixelFormat; var imgDepth = Bitmap.GetPixelFormatSize(imgFormat); var height = imgData.Height; var width = imgData.Width; if (imgDepth == 16) { for (int y = 0; y < height; y++) { ushort *imgLine = (ushort *)imgData.Scan0 + (y * (imgData.Stride >> 1)); uint * pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); if (imgFormat == PixelFormat.Format16bppRgb555) { for (int x = 0; x < width; x++) { ushort imgPixel = BitmapHelper.GetDataUInt16(imgLine, x); PixData.SetDataFourByte(pixLine, x, BitmapHelper.ConvertRgb555ToRGBA(imgPixel)); } } else if (imgFormat == PixelFormat.Format16bppRgb565) { for (int x = 0; x < width; x++) { ushort imgPixel = BitmapHelper.GetDataUInt16(imgLine, x); PixData.SetDataFourByte(pixLine, x, BitmapHelper.ConvertRgb565ToRGBA(imgPixel)); } } else if (imgFormat == PixelFormat.Format16bppArgb1555) { for (int x = 0; x < width; x++) { ushort imgPixel = BitmapHelper.GetDataUInt16(imgLine, x); PixData.SetDataFourByte(pixLine, x, BitmapHelper.ConvertArgb1555ToRGBA(imgPixel)); } } else if (imgFormat == PixelFormat.Format16bppGrayScale) { for (int x = 0; x < width; x++) { ushort imgPixel = BitmapHelper.GetDataUInt16(imgLine, x); PixData.SetDataTwoByte(pixLine, x, imgPixel); } } } } else if (imgDepth == 1 || imgDepth == 4 || imgDepth == 8) { for (int y = 0; y < height; y++) { byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); if (imgFormat == PixelFormat.Format8bppIndexed) { for (int x = 0; x < width; x++) { byte *pixelPtr = imgLine + x; byte red = *pixelPtr; byte green = *(pixelPtr + 1); byte blue = *(pixelPtr + 2); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255)); } } } } else if (imgDepth == 24 || imgDepth == 32) { for (int y = 0; y < height; y++) { byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); if (imgFormat == PixelFormat.Format24bppRgb) { for (int x = 0; x < lineLength; x += 3) { byte *pixelPtr = imgLine + x; byte red = *pixelPtr; byte green = *(pixelPtr + 1); byte blue = *(pixelPtr + 2); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255)); } } else if (imgFormat == PixelFormat.Format32bppArgb) { for (int x = 0; x < lineLength; x += 4) { byte *pixelPtr = imgLine + x; byte alpha = *pixelPtr; byte red = *(pixelPtr + 1); byte green = *(pixelPtr + 2); byte blue = *(pixelPtr + 3); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, alpha)); } } else if (imgFormat == PixelFormat.Format32bppPArgb) { for (int x = 0; x < lineLength; x += 4) { byte *pixelPtr = imgLine + x; byte alpha = *pixelPtr; byte red = *(pixelPtr + 1); byte green = *(pixelPtr + 2); byte blue = *(pixelPtr + 3); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, alpha)); } } else if (imgFormat == PixelFormat.Format32bppRgb) { for (int x = 0; x < lineLength; x += 4) { byte *pixelPtr = imgLine + x; byte red = *pixelPtr; byte green = *(pixelPtr + 1); byte blue = *(pixelPtr + 2); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255)); } } else if (imgFormat == PixelFormat.Canonical) { for (int x = 0; x < lineLength; x += 4) { byte *pixelPtr = imgLine + x; byte red = *pixelPtr; byte green = *(pixelPtr + 1); byte blue = *(pixelPtr + 2); byte alpha = *(pixelPtr + 3); PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, alpha)); } } } } }
public uint ToRGBA() { return(BitmapHelper.EncodeAsRGBA(red, green, blue, alpha)); }
public uint ToRGBA() => BitmapHelper.EncodeAsRGBA(this.Red, this.Green, this.Blue, this.Alpha);