protected unsafe void ModifyR5G6B5(ModifyPixel callback) { int w = OriginalSize.Width; int h = OriginalSize.Height; IntPtr lockresult = Lock(); short* directacces = (short*)(void*)lockresult; int pitch = Pitch / 2; for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { Color col; if (callback(x, y, out col)) { uint color = (uint)col.NativeColor; directacces[x + y * pitch] = (short)(0x8000 | (color & 0x1F) | ((color >> 1) & 0x7FE0)); } } Unlock(); }
/// <summary> /// Modifies the texture using a simple callback called on each pixel's modification. /// </summary> /// <param name="callback">Callback called for each pixel</param> public void Modify(ModifyPixel callback) { switch (ColorFormat) { case ColorFormat.A1R5G5B5: ModifyA1R5G5B5(callback); break; case ColorFormat.R5G6B5: ModifyR5G6B5(callback); break; case ColorFormat.A8R8G8B8: ModifyA8R8G8B8(callback); break; default: throw new NotImplementedException(ColorFormat + " modifying options are not implemented. Consider using unsafe acess or Get/SetPixel instead."); } }
protected unsafe void ModifyA8R8G8B8(ModifyPixel callback) { int w = OriginalSize.Width; int h = OriginalSize.Height; IntPtr lockresult = Lock(); int* directacces = (int*)(void*)lockresult; int pitch = Pitch / 4; for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { Color col; if (callback(x, y, out col)) directacces[x + y * pitch] = col.NativeColor; } Unlock(); }
unsafe protected void ModifyA1R5G5B5(ModifyPixel callback) { int w = OriginalSize.Width; int h = OriginalSize.Height; IntPtr lockresult = Lock(); short* directacces = (short*)(void*)lockresult; int pitch = Pitch / 2; for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { Color col; if (callback(x, y, out col)) { uint color = (uint)col.NativeColor; directacces[x + y * pitch] = (short)((color & 0x80000000) >> 16 | (color & 0x00F80000) >> 9 | (color & 0x0000F800) >> 6 | (color & 0x000000F8) >> 3); } } Unlock(); }