PixelFormatWriter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using System;
  2. using Avalonia.Platform;
  3. namespace Avalonia.Media.Imaging;
  4. internal interface IPixelFormatWriter
  5. {
  6. void WriteNext(Rgba8888Pixel pixel);
  7. void Reset(IntPtr address);
  8. }
  9. internal static unsafe class PixelFormatWriter
  10. {
  11. public unsafe struct Rgb24PixelFormatWriter : IPixelFormatWriter
  12. {
  13. private byte* _address;
  14. public void WriteNext(Rgba8888Pixel pixel)
  15. {
  16. var addr = _address;
  17. addr[0] = pixel.R;
  18. addr[1] = pixel.G;
  19. addr[2] = pixel.B;
  20. _address += 3;
  21. }
  22. public void Reset(IntPtr address) => _address = (byte*)address;
  23. }
  24. public unsafe struct Rgba64PixelFormatWriter : IPixelFormatWriter
  25. {
  26. private Rgba64Pixel* _address;
  27. public void WriteNext(Rgba8888Pixel pixel)
  28. {
  29. var addr = _address;
  30. *addr = new Rgba64Pixel((ushort)(pixel.R << 8), (ushort)(pixel.G << 8), (ushort)(pixel.B << 8), (ushort)(pixel.A << 8));
  31. _address++;
  32. }
  33. public void Reset(IntPtr address) => _address = (Rgba64Pixel*)address;
  34. }
  35. public unsafe struct Rgba8888PixelFormatWriter : IPixelFormatWriter
  36. {
  37. private Rgba8888Pixel* _address;
  38. public void WriteNext(Rgba8888Pixel pixel)
  39. {
  40. var addr = _address;
  41. *addr = pixel;
  42. _address++;
  43. }
  44. public void Reset(IntPtr address) => _address = (Rgba8888Pixel*)address;
  45. }
  46. public unsafe struct Bgra8888PixelFormatWriter : IPixelFormatWriter
  47. {
  48. private byte* _address;
  49. public void WriteNext(Rgba8888Pixel pixel)
  50. {
  51. var addr = _address;
  52. addr[0] = pixel.B;
  53. addr[1] = pixel.G;
  54. addr[2] = pixel.R;
  55. addr[3] = pixel.A;
  56. _address += 4;
  57. }
  58. public void Reset(IntPtr address) => _address = (byte*)address;
  59. }
  60. public unsafe struct Bgr24PixelFormatWriter : IPixelFormatWriter
  61. {
  62. private byte* _address;
  63. public void WriteNext(Rgba8888Pixel pixel)
  64. {
  65. var addr = _address;
  66. addr[2] = pixel.R;
  67. addr[1] = pixel.G;
  68. addr[0] = pixel.B;
  69. _address += 3;
  70. }
  71. public void Reset(IntPtr address) => _address = (byte*)address;
  72. }
  73. public unsafe struct Bgra32PixelFormatWriter : IPixelFormatWriter
  74. {
  75. private byte* _address;
  76. public void WriteNext(Rgba8888Pixel pixel)
  77. {
  78. var addr = _address;
  79. addr[3] = pixel.A;
  80. addr[2] = pixel.R;
  81. addr[1] = pixel.G;
  82. addr[0] = pixel.B;
  83. _address += 4;
  84. }
  85. public void Reset(IntPtr address) => _address = (byte*)address;
  86. }
  87. public unsafe struct Bgr565PixelFormatWriter : IPixelFormatWriter
  88. {
  89. private ushort* _address;
  90. public void WriteNext(Rgba8888Pixel pixel)
  91. {
  92. var addr = _address;
  93. *addr = Pack(pixel);
  94. _address++;
  95. }
  96. public void Reset(IntPtr address) => _address = (ushort*)address;
  97. private static ushort Pack(Rgba8888Pixel pixel)
  98. {
  99. return (ushort)((((int)Math.Round(pixel.R / 255F * 31F) & 0x1F) << 11)
  100. | (((int)Math.Round(pixel.G / 255F * 63F) & 0x3F) << 5)
  101. | ((int)Math.Round(pixel.B / 255F * 31F) & 0x1F));
  102. }
  103. }
  104. public unsafe struct Bgr555PixelFormatWriter : IPixelFormatWriter
  105. {
  106. private ushort* _address;
  107. public void WriteNext(Rgba8888Pixel pixel)
  108. {
  109. var addr = _address;
  110. *addr = Pack(pixel);
  111. _address++;
  112. }
  113. public void Reset(IntPtr address) => _address = (ushort*)address;
  114. private static ushort Pack(Rgba8888Pixel pixel)
  115. {
  116. return (ushort)(
  117. (((int)Math.Round(pixel.R / 255F * 31F) & 0x1F) << 10)
  118. | (((int)Math.Round(pixel.G / 255F * 31F) & 0x1F) << 5)
  119. | (((int)Math.Round(pixel.B / 255F * 31F) & 0x1F) << 0));
  120. }
  121. }
  122. public unsafe struct Gray32FloatPixelFormatWriter : IPixelFormatWriter
  123. {
  124. private float* _address;
  125. public void WriteNext(Rgba8888Pixel pixel)
  126. {
  127. var addr = _address;
  128. *addr = Pack(pixel);
  129. _address++;
  130. }
  131. private static float Pack(Rgba8888Pixel pixel)
  132. {
  133. return (float)Math.Pow(pixel.R / 255F, 2.2);
  134. }
  135. public void Reset(IntPtr address) => _address = (float*)address;
  136. }
  137. public unsafe struct BlackWhitePixelFormatWriter : IPixelFormatWriter
  138. {
  139. private int _bit;
  140. private byte* _address;
  141. public void WriteNext(Rgba8888Pixel pixel)
  142. {
  143. var addr = _address;
  144. var grayscale = Math.Round(0.299F * pixel.R + 0.587F * pixel.G + 0.114F * pixel.B);
  145. var value = grayscale > 0x7F ? 1 : 0;
  146. var shift = 7 - _bit;
  147. var mask = 1 << shift;
  148. *addr = (byte)((*addr & ~mask) | value << shift);
  149. _bit++;
  150. if (_bit == 8)
  151. {
  152. _address++;
  153. _bit = 0;
  154. }
  155. }
  156. public void Reset(IntPtr address) => _address = (byte*)address;
  157. }
  158. public unsafe struct Gray2PixelFormatWriter : IPixelFormatWriter
  159. {
  160. private int _bit;
  161. private byte* _address;
  162. public void WriteNext(Rgba8888Pixel pixel)
  163. {
  164. var addr = _address;
  165. var value = 0;
  166. var grayscale = (byte)Math.Round(0.299F * pixel.R + 0.587F * pixel.G + 0.114F * pixel.B);
  167. if (grayscale > 0 && grayscale <= 0x55)
  168. {
  169. //01
  170. value = 1;
  171. }
  172. if (grayscale > 0x55 && grayscale <= 0xAA)
  173. {
  174. //10
  175. value = 2;
  176. }
  177. if (grayscale > 0xAA)
  178. {
  179. //11
  180. value = 3;
  181. }
  182. var shift = 6 - _bit;
  183. var mask = 3 << shift;
  184. *addr = (byte)((*addr & ~mask) | value << shift);
  185. _bit += 2;
  186. if (_bit == 8)
  187. {
  188. _address++;
  189. _bit = 0;
  190. }
  191. }
  192. public void Reset(IntPtr address) => _address = (byte*)address;
  193. }
  194. public unsafe struct Gray4PixelFormatWriter : IPixelFormatWriter
  195. {
  196. private int _bit;
  197. private byte* _address;
  198. public void WriteNext(Rgba8888Pixel pixel)
  199. {
  200. var addr = _address;
  201. var grayscale = (byte)Math.Round(0.299F * pixel.R + 0.587F * pixel.G + 0.114F * pixel.B);
  202. var value = (byte)(grayscale / 255F * 0xF);
  203. var shift = 4 - _bit;
  204. var mask = 0xF << shift;
  205. *addr = (byte)((*addr & ~mask) | value << shift);
  206. _bit += 4;
  207. if (_bit == 8)
  208. {
  209. _address++;
  210. _bit = 0;
  211. }
  212. }
  213. public void Reset(IntPtr address) => _address = (byte*)address;
  214. }
  215. public unsafe struct Gray8PixelFormatWriter : IPixelFormatWriter
  216. {
  217. private byte* _address;
  218. public void WriteNext(Rgba8888Pixel pixel)
  219. {
  220. var addr = _address;
  221. var grayscale = (byte)Math.Round(0.299F * pixel.R + 0.587F * pixel.G + 0.114F * pixel.B);
  222. *addr = grayscale;
  223. _address++;
  224. }
  225. public void Reset(IntPtr address) => _address = (byte*)address;
  226. }
  227. public unsafe struct Gray16PixelFormatWriter : IPixelFormatWriter
  228. {
  229. private ushort* _address;
  230. public void WriteNext(Rgba8888Pixel pixel)
  231. {
  232. var addr = _address;
  233. var grayscale = (ushort)Math.Round((0.299F * pixel.R + 0.587F * pixel.G + 0.114F * pixel.B) * 0x0101);
  234. *addr = grayscale;
  235. _address++;
  236. }
  237. public void Reset(IntPtr address) => _address = (ushort*)address;
  238. }
  239. private static void Write<T>(
  240. ReadOnlySpan<Rgba8888Pixel> pixels,
  241. IntPtr dest,
  242. PixelSize size,
  243. int stride,
  244. AlphaFormat alphaFormat,
  245. AlphaFormat srcAlphaFormat) where T : struct, IPixelFormatWriter
  246. {
  247. var writer = new T();
  248. var w = size.Width;
  249. var h = size.Height;
  250. var count = 0;
  251. for (var y = 0; y < h; y++)
  252. {
  253. writer.Reset(dest + stride * y);
  254. for (var x = 0; x < w; x++)
  255. {
  256. writer.WriteNext(GetConvertedPixel(pixels[count++], srcAlphaFormat, alphaFormat));
  257. }
  258. }
  259. }
  260. private static Rgba8888Pixel GetConvertedPixel(Rgba8888Pixel pixel, AlphaFormat sourceAlpha, AlphaFormat destAlpha)
  261. {
  262. if (sourceAlpha != destAlpha)
  263. {
  264. if (sourceAlpha == AlphaFormat.Premul && destAlpha != AlphaFormat.Premul)
  265. {
  266. return ConvertFromPremultiplied(pixel);
  267. }
  268. if (sourceAlpha != AlphaFormat.Premul && destAlpha == AlphaFormat.Premul)
  269. {
  270. return ConvertToPremultiplied(pixel);
  271. }
  272. }
  273. return pixel;
  274. }
  275. private static Rgba8888Pixel ConvertToPremultiplied(Rgba8888Pixel pixel)
  276. {
  277. var factor = pixel.A / 255F;
  278. return new Rgba8888Pixel
  279. {
  280. R = (byte)(pixel.R * factor),
  281. G = (byte)(pixel.G * factor),
  282. B = (byte)(pixel.B * factor),
  283. A = pixel.A
  284. };
  285. }
  286. private static Rgba8888Pixel ConvertFromPremultiplied(Rgba8888Pixel pixel)
  287. {
  288. var factor = 1F / (pixel.A / 255F);
  289. return new Rgba8888Pixel
  290. {
  291. R = (byte)(pixel.R * factor),
  292. G = (byte)(pixel.G * factor),
  293. B = (byte)(pixel.B * factor),
  294. A = pixel.A
  295. };
  296. }
  297. public static void Write(
  298. ReadOnlySpan<Rgba8888Pixel> pixels,
  299. IntPtr dest,
  300. PixelSize size,
  301. int stride,
  302. PixelFormat format,
  303. AlphaFormat alphaFormat,
  304. AlphaFormat srcAlphaFormat)
  305. {
  306. switch (format.FormatEnum)
  307. {
  308. case PixelFormatEnum.Rgb565:
  309. Write<Bgr565PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  310. break;
  311. case PixelFormatEnum.Rgba8888:
  312. Write<Rgba8888PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  313. break;
  314. case PixelFormatEnum.Bgra8888:
  315. Write<Bgra8888PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  316. break;
  317. case PixelFormatEnum.BlackWhite:
  318. Write<BlackWhitePixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  319. break;
  320. case PixelFormatEnum.Gray2:
  321. Write<Gray2PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  322. break;
  323. case PixelFormatEnum.Gray4:
  324. Write<Gray4PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  325. break;
  326. case PixelFormatEnum.Gray8:
  327. Write<Gray8PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  328. break;
  329. case PixelFormatEnum.Gray16:
  330. Write<Gray16PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  331. break;
  332. case PixelFormatEnum.Gray32Float:
  333. Write<Gray32FloatPixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  334. break;
  335. case PixelFormatEnum.Rgba64:
  336. Write<Rgba64PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  337. break;
  338. case PixelFormatEnum.Rgb24:
  339. Write<Rgb24PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  340. break;
  341. case PixelFormatEnum.Bgr24:
  342. Write<Bgr24PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  343. break;
  344. case PixelFormatEnum.Bgr555:
  345. Write<Bgr555PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  346. break;
  347. case PixelFormatEnum.Bgr565:
  348. Write<Bgr565PixelFormatWriter>(pixels, dest, size, stride, alphaFormat, srcAlphaFormat);
  349. break;
  350. default:
  351. throw new NotSupportedException($"Pixel format {format} is not supported");
  352. }
  353. }
  354. }