Use Slim Reader/Writer lock to replace CRITICAL_SECTION (better performance).
[gdipp.git] / gdipp_client / gamma.cpp
blobbe89ea3b6b63455f1a0e65d8e07401d6ab340b1b
1 #include "stdafx.h"
2 #include "gamma.h"
3 #include "gdipp_lib/scoped_rw_lock.h"
5 namespace gdipp
8 gamma::~gamma()
10 for (std::map<double, BYTE *>::const_iterator iter = _gamma_ramps.begin(); iter != _gamma_ramps.end(); ++iter)
11 delete[] iter->second;
14 const BYTE *gamma::get_ramp(double gamma)
16 std::map<double, BYTE *>::const_iterator iter = _gamma_ramps.find(gamma);
17 if (iter == _gamma_ramps.end())
19 // double-check lock
20 const scoped_rw_lock lock_w(scoped_rw_lock::CLIENT_GAMMA, false);
21 iter = _gamma_ramps.find(gamma);
22 if (iter == _gamma_ramps.end())
23 init_ramp(gamma);
26 return _gamma_ramps[gamma];
29 void gamma::init_ramp(double gamma)
31 BYTE *new_ramp = new BYTE[256];
32 const double gamma_inv = 1 / gamma;
34 for (int i = 0; i < 256; ++i)
36 double a = pow(i / 255.0, gamma);
37 new_ramp[i] = static_cast<BYTE>((pow(i / 255.0, gamma_inv) * 255));
40 _gamma_ramps[gamma] = new_ramp;