private unsafe void TransferDataFormat8bppIndexed(BitmapData imgData, PixData pixData) { 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 pixelVal = *(imgLine + x); PixData.SetDataByte(pixLine, x, pixelVal); } } }
private unsafe void TransferData16(PixData pixData, BitmapData imgData) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; var width = imgData.Width; for (int y = 0; y < height; y++) { uint* pixLine = (uint*)pixData.Data + (y * pixData.WordsPerLine); ushort* imgLine = (ushort*)imgData.Scan0 + (y * imgData.Stride); for (int x = 0; x < width; x++) { ushort pixVal = (ushort)PixData.GetDataTwoByte(pixLine, x); imgLine[x] = pixVal; } } }
private unsafe void TransferData16(PixData pixData, BitmapData imgData) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; var width = imgData.Width; for (var y = 0; y < height; y++) { var pixLine = ( uint * )pixData.Data + (y * pixData.WordsPerLine); var imgLine = ( ushort * )imgData.Scan0 + (y * imgData.Stride); for (var x = 0; x < width; x++) { var pixVal = ( ushort )PixData.GetDataTwoByte(pixLine, x); imgLine[x] = pixVal; } } }
private unsafe void TransferData1(PixData pixData, BitmapData imgData) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; var width = imgData.Width / 8; for (int y = 0; y < height; y++) { uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); for (int x = 0; x < width; x++) { byte pixVal = (byte)PixData.GetDataByte(pixLine, x); imgLine[x] = pixVal; } } }
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 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 } } }
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 TransferData32(PixData pixData, BitmapData imgData, int alphaMask) { 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 pixVal = PixColor.FromRgba(pixLine[x]); var 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 } } }
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)); } } } } }
private unsafe void TransferDataFormat1bppIndexed(BitmapData imgData, PixData pixData) { 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 pixelVal = BitmapHelper.GetDataBit(imgLine, x); PixData.SetDataBit(pixLine, x, pixelVal); } } }
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 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 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)); } } } } }