xref: /haiku/src/add-ons/translators/wonderbrush/support/blending.cpp (revision 73a2ffba3bb47d5fa3811f0e609f7afa815db9bc)
1 /*
2  * Copyright 2006, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include <math.h>
10 
11 #include "blending.h"
12 
13 #if GAMMA_BLEND
14 
15 // speed tests done on Pentium M, 1450 MHz
16 // blending two 800x600 bitmaps with "50" on each component (including alpha): 1500000 usecs
17 // -"-														  using gamma LUT:  651000 usecs
18 // -"-													   no use of floorf():  572000 usecs
19 
20 // -"-												   uint16 integer version:   72000 usecs
21 // -"-																   inline:   60200 usecs
22 
23 // for comparison:
24 // -"-								inline only, no LUTs, no gamma correction:   44000 usecs
25 // -"-							   + premultiplied alpha (less MULs, no DIVs):   16500 usecs
26 
27 
28 const float kGamma = 2.2;
29 const float kInverseGamma = 1.0 / kGamma;
30 
31 uint16* kGammaTable = NULL;
32 uint8* kInverseGammaTable = NULL;
33 
34 
35 // convert_to_gamma
36 uint16
convert_to_gamma(uint8 value)37 convert_to_gamma(uint8 value)
38 {
39 	return kGammaTable[value];
40 }
41 
42 // init_gamma_blending
43 void
init_gamma_blending()44 init_gamma_blending()
45 {
46 	// init LUT R'G'B' [0...255] -> RGB [0...25500]
47 	if (!kGammaTable)
48 		kGammaTable = new uint16[256];
49 	for (uint32 i = 0; i < 256; i++)
50 		kGammaTable[i] = (uint16)(powf((float)i / 255.0, kGamma) * 25500.0);
51 
52 	// init LUT RGB [0...25500] -> R'G'B' [0...255]
53 	if (!kInverseGammaTable)
54 		kInverseGammaTable = new uint8[25501];
55 	for (uint32 i = 0; i < 25501; i++)
56 		kInverseGammaTable[i] = (uint8)(powf((float)i / 25500.0, kInverseGamma) * 255.0);
57 }
58 
59 // init_gamma_blending
60 void
uninit_gamma_blending()61 uninit_gamma_blending()
62 {
63 	delete[] kGammaTable;
64 	kGammaTable = NULL;
65 	delete[] kInverseGammaTable;
66 	kInverseGammaTable = NULL;
67 }
68 
69 
70 #endif // GAMMA_BLEND
71