OSDN Git Service

[Refactor] #40477 Separated disturbance-optioncs.c/h from cmd-gameoption.c/h
[hengband/hengband.git] / src / main / x11-gamma-builder.c
1 /*!
2  * @brief X11環境 (の中でもmaid-x11を必要とする特殊な環境)でガンマ値を調整する
3  * @date 2020/05/16
4  * @author Hourier
5  */
6
7 #include "system/angband.h"
8 #include "main/x11-gamma-builder.h"
9
10 /* Table of gamma values */
11 byte gamma_table[256];
12
13 /* Table of ln(x/256) * 256 for x going from 0 -> 255 */
14 static s16b gamma_helper[256] =
15 {
16 0,-1420,-1242,-1138,-1065,-1007,-961,-921,-887,-857,-830,-806,-783,-762,-744,-726,
17 -710,-694,-679,-666,-652,-640,-628,-617,-606,-596,-586,-576,-567,-577,-549,-541,
18 -532,-525,-517,-509,-502,-495,-488,-482,-475,-469,-463,-457,-451,-455,-439,-434,
19 -429,-423,-418,-413,-408,-403,-398,-394,-389,-385,-380,-376,-371,-367,-363,-359,
20 -355,-351,-347,-343,-339,-336,-332,-328,-325,-321,-318,-314,-311,-308,-304,-301,
21 -298,-295,-291,-288,-285,-282,-279,-276,-273,-271,-268,-265,-262,-259,-257,-254,
22 -251,-248,-246,-243,-241,-238,-236,-233,-231,-228,-226,-223,-221,-219,-216,-214,
23 -212,-209,-207,-205,-203,-200,-198,-196,-194,-192,-190,-188,-186,-184,-182,-180,
24 -178,-176,-174,-172,-170,-168,-166,-164,-162,-160,-158,-156,-155,-153,-151,-149,
25 -147,-146,-144,-142,-140,-139,-137,-135,-134,-132,-130,-128,-127,-125,-124,-122,
26 -120,-119,-117,-116,-114,-112,-111,-109,-108,-106,-105,-103,-102,-100,-99,-97,
27 -96,-95,-93,-92,-90,-89,-87,-86,-85,-83,-82,-80,-79,-78,-76,-75,
28 -74,-72,-71,-70,-68,-67,-66,-65,-63,-62,-61,-59,-58,-57,-56,-54,
29 -53,-52,-51,-50,-48,-47,-46,-45,-44,-42,-41,-40,-39,-38,-37,-35,
30 -34,-33,-32,-31,-30,-29,-27,-26,-25,-24,-23,-22,-21,-20,-19,-18,
31 -17,-16,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1
32 };
33
34 /*
35  * Build the gamma table so that floating point isn't needed.
36  *
37  * Note gamma goes from 0->256.  The old value of 100 is now 128.
38  */
39 void build_gamma_table(int gamma)
40 {
41         gamma_table[0] = 0;
42         gamma_table[255] = 255;
43         for (int i = 1; i < 255; i++)
44         {
45                 /*
46                  * Initialise the Taylor series
47                  *
48                  * value and diff have been scaled by 256
49                  */
50                 int n = 1;
51                 long value = 256 * 256;
52                 long diff = ((long)gamma_helper[i]) * (gamma - 256);
53
54                 while (diff)
55                 {
56                         value += diff;
57                         n++;
58
59
60                         /*
61                          * Use the following identiy to calculate the gamma table.
62                          * exp(x) = 1 + x + x^2/2 + x^3/(2*3) + x^4/(2*3*4) +...
63                          *
64                          * n is the current term number.
65                          *
66                          * The gamma_helper array contains a table of
67                          * ln(x/256) * 256
68                          * This is used because a^b = exp(b*ln(a))
69                          *
70                          * In this case:
71                          * a is i / 256
72                          * b is gamma.
73                          *
74                          * Note that everything is scaled by 256 for accuracy,
75                          * plus another factor of 256 for the final result to
76                          * be from 0-255.  Thus gamma_helper[] * gamma must be
77                          * divided by 256*256 each itteration, to get back to
78                          * the original power series.
79                          */
80                         diff = (((diff / 256) * gamma_helper[i]) * (gamma - 256)) / (256 * n);
81                 }
82
83                 /*
84                  * Store the value in the table so that the
85                  * floating point pow function isn't needed .
86                  */
87                 gamma_table[i] = ((long)(value / 256) * i) / 256;
88         }
89 }