PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/base/Drivers/S3Trio64/BitmapInfoHeader.cs

#
C# | 88 lines | 67 code | 11 blank | 10 comment | 11 complexity | fdf8734816f4e8777d8d591993b26764 MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // File: BitmapInfoHeader.cs
  8. //
  9. // Note:
  10. //
  11. using System;
  12. using System.Runtime.InteropServices;
  13. using Microsoft.SingSharp;
  14. using Microsoft.Singularity;
  15. using Microsoft.Singularity.Channels;
  16. namespace Microsoft.Singularity.Io
  17. {
  18. [CLSCompliant(false)]
  19. [StructLayout(LayoutKind.Explicit)]
  20. public pointerfree struct BITMAPINFOHEADER {
  21. [FieldOffset( 0)] public uint biSize;
  22. [FieldOffset( 4)] public int biWidth;
  23. [FieldOffset( 8)] public int biHeight;
  24. [FieldOffset(12)] public ushort biPlanes;
  25. [FieldOffset(14)] public ushort biBitCount;
  26. [FieldOffset(16)] public uint biCompression;
  27. [FieldOffset(20)] public uint biSizeImage;
  28. [FieldOffset(24)] public int biXPelsPerMeter;
  29. [FieldOffset(28)] public int biYPelsPerMeter;
  30. [FieldOffset(30)] public uint biClrUsed;
  31. [FieldOffset(32)] public uint biClrImportant;
  32. public static BITMAPINFOHEADER Read(byte[]! in ExHeap buffer,
  33. int offset, out int used)
  34. {
  35. if (buffer == null || offset < 0 ||
  36. offset + sizeof(BITMAPINFOHEADER) > buffer.Length) {
  37. unchecked {
  38. Tracing.Log(Tracing.Error,
  39. "BITMAPINFOHEADER Read(offset={0}, length={1})",
  40. (UIntPtr)(uint)offset,
  41. (UIntPtr)(uint)buffer.Length);
  42. }
  43. throw new OverflowException("Read source invalid.");
  44. }
  45. used = offset + sizeof(BITMAPINFOHEADER);
  46. ref BITMAPINFOHEADER bmih = ref buffer[offset];
  47. return bmih;
  48. }
  49. public RGB[] ReadPalette(byte[]! in ExHeap buffer, int offset, out int used)
  50. {
  51. if (biClrUsed == 0 && biBitCount >= 24) {
  52. used = offset;
  53. return null;
  54. }
  55. int count = (biClrUsed != 0) ? (int)biClrUsed : (1 << biBitCount);
  56. if (buffer == null || offset < 0 ||
  57. offset + count * sizeof(uint) > buffer.Length) {
  58. unchecked {
  59. Tracing.Log(Tracing.Error,
  60. "RGB Read(offset={0}, count={1}, length={2}",
  61. (UIntPtr)(uint)offset,
  62. (UIntPtr)(uint)count,
  63. (UIntPtr)(uint)buffer.Length);
  64. }
  65. throw new OverflowException("Read source invalid.");
  66. }
  67. RGB[] values = new RGB [count];
  68. used = offset;
  69. offset += sizeof(uint);
  70. for (int i = 0; i < count; i++) {
  71. values[i] = new RGB(buffer[2+offset], buffer[1+offset], buffer[0+offset]);
  72. offset += sizeof(uint);
  73. }
  74. return values;
  75. }
  76. }
  77. } // namespace Microsoft.Singularity.Io