mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 12:32:43 +00:00 
			
		
		
		
	LibGfx/ICC: Add evaluate() member functions to both curve types
This commit is contained in:
		
							parent
							
								
									5df35d030f
								
							
						
					
					
						commit
						df6b47609a
					
				
					 1 changed files with 49 additions and 0 deletions
				
			
		|  | @ -13,6 +13,7 @@ | |||
| #include <AK/String.h> | ||||
| #include <AK/Vector.h> | ||||
| #include <LibGfx/ICC/DistinctFourCC.h> | ||||
| #include <math.h> | ||||
| 
 | ||||
| namespace Gfx::ICC { | ||||
| 
 | ||||
|  | @ -159,6 +160,26 @@ public: | |||
|     //      65 535). Function values between the entries shall be obtained through linear interpolation."
 | ||||
|     Vector<u16> const& values() const { return m_values; } | ||||
| 
 | ||||
|     // x must be in [0..1].
 | ||||
|     float evaluate(float x) const | ||||
|     { | ||||
|         VERIFY(0.f <= x && x <= 1.f); | ||||
| 
 | ||||
|         if (values().is_empty()) | ||||
|             return x; | ||||
| 
 | ||||
|         if (values().size() == 1) | ||||
|             return powf(x, values()[0] / (float)0x100); | ||||
| 
 | ||||
|         size_t i = static_cast<size_t>(x * (values().size() - 1)); | ||||
|         if (i == values().size() - 1) | ||||
|             --i; | ||||
| 
 | ||||
|         float f = x * (values().size() - 1) - i; | ||||
| 
 | ||||
|         return (1 - f) * (values()[i] / 65535.f) + f * (values()[i + 1] / 65535.f); | ||||
|     } | ||||
| 
 | ||||
| private: | ||||
|     Vector<u16> m_values; | ||||
| }; | ||||
|  | @ -691,6 +712,34 @@ public: | |||
|         return m_parameters[6]; | ||||
|     } | ||||
| 
 | ||||
|     // x must be in [0..1].
 | ||||
|     float evaluate(float x) const | ||||
|     { | ||||
|         VERIFY(0.f <= x && x <= 1.f); | ||||
| 
 | ||||
|         switch (function_type()) { | ||||
|         case FunctionType::Type0: | ||||
|             return powf(x, (float)g()); | ||||
|         case FunctionType::Type1: | ||||
|             if (x >= -(float)b() / (float)a()) | ||||
|                 return powf((float)a() * x + (float)b(), (float)g()); | ||||
|             return 0; | ||||
|         case FunctionType::Type2: | ||||
|             if (x >= -(float)b() / (float)a()) | ||||
|                 return powf((float)a() * x + (float)b(), (float)g()) + (float)c(); | ||||
|             return (float)c(); | ||||
|         case FunctionType::Type3: | ||||
|             if (x >= (float)d()) | ||||
|                 return powf((float)a() * x + (float)b(), (float)g()); | ||||
|             return (float)c() * x; | ||||
|         case FunctionType::Type4: | ||||
|             if (x >= (float)d()) | ||||
|                 return powf((float)a() * x + (float)b(), (float)g()) + (float)e(); | ||||
|             return (float)c() * x + (float)f(); | ||||
|         } | ||||
|         VERIFY_NOT_REACHED(); | ||||
|     } | ||||
| 
 | ||||
| private: | ||||
|     FunctionType m_function_type; | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Nico Weber
						Nico Weber