GammaLookUpTable.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // C# port by: Lars Brubaker
  6. // larsbrubaker@gmail.com
  7. // Copyright (C) 2007
  8. //
  9. // Permission to copy, use, modify, sell and distribute this software
  10. // is granted provided this copyright notice appears in all copies.
  11. // This software is provided "as is" without express or implied
  12. // warranty, and with no claim as to its suitability for any purpose.
  13. //
  14. //----------------------------------------------------------------------------
  15. // Contact: mcseem@antigrain.com
  16. // mcseemagg@yahoo.com
  17. // http://www.antigrain.com
  18. //----------------------------------------------------------------------------
  19. //#ifndef AGG_GAMMA_LUT_INCLUDED
  20. //#define AGG_GAMMA_LUT_INCLUDED
  21. //#include <math.h>
  22. //#include "agg_basics.h"
  23. using System;
  24. namespace MatterHackers.Agg
  25. {
  26. public class GammaLookUpTable
  27. {
  28. private double m_gamma;
  29. private byte[] m_dir_gamma;
  30. private byte[] m_inv_gamma;
  31. private enum gamma_scale_e
  32. {
  33. gamma_shift = 8,
  34. gamma_size = 1 << gamma_shift,
  35. gamma_mask = gamma_size - 1
  36. };
  37. public GammaLookUpTable()
  38. {
  39. m_gamma = (1.0);
  40. m_dir_gamma = new byte[(int)gamma_scale_e.gamma_size];
  41. m_inv_gamma = new byte[(int)gamma_scale_e.gamma_size];
  42. }
  43. public GammaLookUpTable(double gamma)
  44. {
  45. m_gamma = gamma;
  46. m_dir_gamma = new byte[(int)gamma_scale_e.gamma_size];
  47. m_inv_gamma = new byte[(int)gamma_scale_e.gamma_size];
  48. SetGamma(m_gamma);
  49. }
  50. public void SetGamma(double g)
  51. {
  52. m_gamma = g;
  53. for (uint i = 0; i < (uint)gamma_scale_e.gamma_size; i++)
  54. {
  55. m_dir_gamma[i] = (byte)Util.uround(Math.Pow(i / (double)gamma_scale_e.gamma_mask, m_gamma) * (double)gamma_scale_e.gamma_mask);
  56. }
  57. double inv_g = 1.0 / g;
  58. for (uint i = 0; i < (uint)gamma_scale_e.gamma_size; i++)
  59. {
  60. m_inv_gamma[i] = (byte)Util.uround(Math.Pow(i / (double)gamma_scale_e.gamma_mask, inv_g) * (double)gamma_scale_e.gamma_mask);
  61. }
  62. }
  63. public double GetGamma()
  64. {
  65. return m_gamma;
  66. }
  67. public byte dir(int v)
  68. {
  69. return m_dir_gamma[v];
  70. }
  71. public byte inv(int v)
  72. {
  73. return m_inv_gamma[v];
  74. }
  75. };
  76. }