Professional Documents
Culture Documents
What and Why Is Gamma Correction - 2016!08!2121.05.21
What and Why Is Gamma Correction - 2016!08!2121.05.21
1 of 19
http://www.scantips.com/lights/gamma2.html
www.scantips.com
What and Why is Gamma
Correction?
All of the world's photo image files
contain Gamma Correction (gamma 2.2
is specified in the specifications, sRGB,
in digital HDTV, and before that, in
analog TV NTSC and PAL).
Because, Gamma Correction oppositely
corrects for the deficiencies of CRT
monitors (which we used for many
years, including television). For this
purpose, all of our digital cameras
and scanners always output images
already corrected
for gamma
(except Raw images defer this step
until later, and 1-bit line art images
don't need gamma).
Gamma is pretty much an invisible
operation, it just always happens in
background, but it does affect our
histogram data. When you edit a
RGB(210, 145, 20) color in
Photoshop, that is gamma data,
which may not be important to us
there. But this also affects the 18%
gray card, which in the histograms
gamma data is near 46% (which is
Not how many of us think of it).
DataFit Curve
Fitting
Nonlinear curve tting, 600+
built in equations, or create
your own
Terms linear and gamma: All digital tonal image data is gamma
encoded (tonal data contains many tones, which is color or
grayscale images). In video use, the common use of these terms is
that gamma data means gamma correction has been added, and
linear data means still linear, it is not gamma encoded (either not
yet encoded, or no longer encoded).
So linear also implies the analog scene data in the lens, and also
implies the linear reproduction that is presented to our eye, same
as we see when either standing in front of the original scene, or
viewing a reproduction on a video screen. Our eye of course always
wants to see only the linear version, like the original scene. Gamma
data is always necessarily "decoded" first before any eye ever sees
8/21/2016 9:05 PM
2 of 19
http://www.scantips.com/lights/gamma2.html
it.
In math, linear means a straight line graph, meaning twice the input
produces twice the output (proportionally, linearly, also 5x input
produces 5x the output, etc.) Gamma is non-linear logarithmic
processing, done to correct for CRT monitors which show data as if
it had been raised to the exponent of 2.2 (roughly numerically
approximate to squared, exponent of 2), with the high values
leaving the low values far behind (lost, dark). So for the CRT, we
have to prepare by first doing Gamma Correction, by applying
exponent 1/2.2 to the data first, offsetting the expected losses, so
the image will come out of the CRT right, i.e., linear again for the
eye to see.
8/21/2016 9:05 PM
3 of 19
http://www.scantips.com/lights/gamma2.html
DataFit Curve
Fitting
Nonlinear curve tting, 600+
built in equations, or create
your own
Our eyes never see gamma data (it's always decoded first). Our camera
sensors also see linear data, images do begin and end as linear, and it
seems every minimal article about our histograms portray them as
linear data. However all our tonal image files and all of their
histograms contain gamma data, which changes the numerical
values you will see in your images. Their data is gamma data, our files
and histograms contain gamma data, the numerical values are gamma
encoded.
Gamma Calculator
Gamma
8/21/2016 9:05 PM
4 of 19
http://www.scantips.com/lights/gamma2.html
Value [0..255]
2.
Percent [0..100%]
3.
Linear or Gamma
4.
Linear or Gamma
5.
Compute
1. Linear to Gamma
Linear value 128 at 50% is
Gamma 2.2 value 186 at 73%
Gamma to Linear
Gamma 2.2 value 128 at 50% is
Linear value 56 at 22%
Numbers Only.
The "values" are the 0..255 values
in a histogram (which are gamma
numbers in the histogram, and
linear numbers at the sensor). The
percentages are of the 255 full
scale histogram value.
Histogram
values
are
integer
[0..255]
values,
so
entering
numbers like 127.5 cannot actually
exist in our JPG files. The calculator
and math will use the decimal
fraction value though, if it pleases
you. And you can show a couple of
decimal places on Option 1, 2, 5
histogram values (if interested in rounding maybe).
8/21/2016 9:05 PM
5 of 19
http://www.scantips.com/lights/gamma2.html
Option 5 (measuring stops down from 255 right end) offers a rough
approximation about how exposure affects the gamma histogram. In
the gamma data, one stop underexposed from 255 should be about 186
or 73%, about 3/4 scale. Or 1/3 stop down from 255 should be about
230 at 90%. However, note that digital cameras also are making a
few simultaneous tonal adjustments, for White Balance, Color
Profile like Vivid, Saturation, Contrast, etc, which shift the histogram
data. Therefore the gamma values in histogram data probably are a
little different than the exact values predicted. It is a ballpark guess.
We never see linear histograms for our photo images, our image files
and histograms are RGB gamma data. These tonal images are always
gamma (but one bit line art is not). The camera sensor chip was linear,
and raw files are still linear, but until we show them to the eye, RGB
image data is gamma. Even raw files show gamma histograms (from an
embedded JPG image included in the raw file).
But gamma in 8-bit files and 8-bit target video space can create tiny
rounding errors in the numbers (possibly "off by one"). This is no big
deal (8 bits is what we do, and it obviously works fine), and this is Not
related to the eye.
8/21/2016 9:05 PM
6 of 19
http://www.scantips.com/lights/gamma2.html
marked in Blue, with input at bottom, and output at left). The straight
45 degree line shows a hypothetical perfect unchanged linear response,
so that any input is the same numerical output (60% input from bottom
is 60% output at left, no change). But the CRT response curve (named
gamma) shows 50% response is down to 22% (so much of the output
will be too dark).
To correct this nonlinear response, the image data is first boosted
nonlinearly, modified to new values equally in the opposite direction of
the losses. This curve is named gamma correction, after which the
image will display properly (linearly) on a nonlinear CRT. Even after
suffering these expected CRT losses, the corrected output will come out
linear (the straight line in graph). That correction curve is shown, the
image data is boosted so that midpoint 50% is raised to 73% in gamma
data (calculator above, Option 2, 50%).
The correct Google search term for this subject is Gamma Correction.
Don't believe everything you read now though. It is the internet, and
there are good sources, and also those that frankly don't know. That
part you see about the purpose of gamma being to aid the human eye
response is utter nonsense, made-up gobbledygook. The eye never
even sees gamma data, the eye only sees the decoded data, exactly
reversed back to be the same original linear version again. That's the
purpose of gamma, meaning gamma is only to correct the response of
CRT, so we can in fact see the original linear image.
The Adobe Levels tool (CTRL L in Elements and Photoshop) has a
gamma option. Adobe Help calls the center slider "Midtones", but
describes it as "The middle Input slider adjusts the gamma in the
image." It is certainly a very good tool to adjust overall image
brightness (much better than "Brightness" tools, which merely add a
constant to all tones, and can cause clipping). This tool raises the
center of the curve, but the endpoints stay fixed (same range is fixed).
It does that by changing gamma, raising the curve shown above. This
center slider of Levels shows 1.00 by default, which means gamma of
1x of existing value (whatever it was, but probably 2.2, and default 1x x
2.2 is still 2.2, no change at 1x). But other slider values are multipliers
of that existing gamma.
8/21/2016 9:05 PM
7 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM
8 of 19
http://www.scantips.com/lights/gamma2.html
Center slider at 2.2, adding gamma 2.2 to already 2.2 (i.e., 2.2
x 2.2 = gamma 4.8 now). Too bright, but we never see this. If
we could see gamma data, our histogram data should look this
way (too bright, the point is data does have gamma 2.2 added
to linear). This is done so when a CRT shows it darker suffering
the gamma losses, it will look right after all.
The Levels center slider is a multiplier of the current image gamma. I
don't find that "multiplier" written about any more, but it was widely
known and discussed 15-20 years ago (CRT days, back when we knew
what gamma was). Gamma used to be very important, but today, we
still encode with 1/2.2, and the LCD monitor must decode with 2.2, and
it's just an automatic no-op now.
Evidence of the tool as multiplier: An eyedropper on the gray road at
the curve ahead in the middle image at gamma 2.2 reads 185 (I'm
looking at the red value). Gamma 2.2 puts that linear value at 126
(midscale). 126 at gamma 1 is 126 (measured in top image, 0.45x x
2.2 = gamma 1). 126 at gamma 4.8 is 220 (measured in bottom image,
2.2x x 2.2 = gamma 4.8). Q.E.D.
Today, our LCD display is considered linear and does not need gamma.
However we still necessarily continue gamma to provide compatibility
with all the world's previous images and video systems. The LCD
display simply uses a chip to decode it first (discarding gamma
correction to necessarily restore the original linear image). Note that
gamma is a Greek letter used for many variables in science (like X is
used in algebra, used many ways), so there are also several other
unrelated uses of the word gamma, in math and physics, or for film
contrast, etc, but all are different unrelated concepts. One use of the
term gamma is to describe the CRT response curve.
Film cannot be shown on a CRT directly (we must digitize an image first
for our computer video system), so CRT is not a concern for film. But
digital cameras and scanners all always automatically add gamma to all
tonal images. A color or grayscale image is tonal (has many tones), but
a one-bit line art image (two colors, black or white, 0 or 1) does not
need or get gamma.
8/21/2016 9:05 PM
9 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM
10 of 19
http://www.scantips.com/lights/gamma2.html
of reciprocal 1/2.2 for Encode, which is reversible math. It's like 8/4
= 2, and 4x2 is 8 again. Reversible math, we simply get the same
value back. However there can be slight 8-bit rounding variations of
gamma in between, which might change the value by a difference of
one sometimes. A small error, but not really a big deal, virtually all of
our images and video systems and printers are 8 bits. If 8 bits were
not acceptable, we would be doing something else.
The reason we use gamma. For many years, CRT was the only video
display we had. But CRT is not linear, and requires heroic efforts to
properly use them for tonal images (photos and TV). The technical
reason we needed gamma is that the CRT light beam intensity efficiency
varies with the tubes electron gun signal voltage. CRT does not use the
decode formula, which was simply the study of what the non-linear CRT
losses already actually do in the act of showing it on a CRT ... the same
effect. The non-linear CRT simply shows the tones, and the response is
sort of as if the values were squared first (2.2 is near 2). These losses
have variable results, depending on the tones value, but the values that
were not bright will pretty much go dark.
How does CRT gamma correction actually do its work? Gamma
2.2 is roughly 2, and my example will use 2 instead because it is
simpler math. Encoding input to the power of 1/2.2 is roughly 1/2 or
square root, which condenses the image gamma data range smaller
(yes Poynton fans, the tones are compressed, CLOSER together). And
78% of the values encode to be boosted above the 127 50% midpoint
(see LUT below, or see curve above). So gamma boosts the low values
higher, they move up more near the big boy bright values. Specifically,
for a numerical example (two tones 225 and 25, and using easier
exponent 2 instead of 2.2), value 225 is 9x brighter then 25 (225/25).
But the square roots are 15 and 5, which is only 3 times more then,
compressed together... 3 is 9 (and only 2.7 times more if we used
2.2). But we simply store the square root, and then the CRT
shows it squared, for no net change, which is the plan. The reason
of course is because the CRT losses are going to show it squared
regardless (specifically, the CRT response result is power of 2.2).
Not to worry, our eye is NEVER EVER going to see any of these
gamma values. Because, then the non-linear CRT gamma output is a
roughly squared response to expand it back (restored to our first 225
and 25 linear values by the actual CRT losses that we planned for). CRT
losses still greatly reduce the low values, but which were first boosted
in preparation for it. So this gamma correction operation can properly
show the dim values linearly again (since dim starts off condensed, up
much closer to the strong values, and then becomes properly dim when
expanded by CRT losses.) It has worked great for many years. But
absolutely nothing about gamma is related to the human eye response.
We don't need to even care how the eye works. :) The eye NEVER sees
any gamma data. The eye merely looks at the final linear reproduction
of our image on the screen, after it is all over. The eye can only tolerate
8/21/2016 9:05 PM
11 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM
12 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM
13 of 19
http://www.scantips.com/lights/gamma2.html
rounding down, or not (just click it, on and off, and watch the values).
Output storage results go into 8-bit integers [0..255], i.e., floating point
fractions are Not stored, and may not be rounded, same as storing into
8-bits would do. Random values might change by one. 8-bits may not
be perfect, but the point is to show worst case isn't too bad.
So 8-bits has effect, but not a big difference. Our accepted computer
color system plan is 8-bits (called 24 bit color, 8 bits each of RGB). We
all use 8-bits and find no problems. Yes, linear values might decode to
come back one less than they went in. A difference of 1 down at 5 or 10
or 20 could possibly be a significant percentage at the low end (where it
is very black, and our monitors can hardy show it anyway), but this is
nothing at higher values. And this change of One is random, no pattern
to it, don't count on the eye to help figure it out. The 8-bit "problem" is
largely only about if the integer should have randomly rounded up
instead of down. The computer can of course compute gamma
conversions to any high precision desired, but the final act of storing a
precise result value into an 8-bit file MAY expediently truncate it to be a
value of maybe one less. It is the tiniest error, which depends on the
decode procedure.
For example, if to be stored in an 8-bit file, linear 20 goes to 80 in 2.2
gamma, which then decodes back to 19 or 20 in 8-bit video space (to
see this, just enter 20 into calculator field 6, choose Option 6, and then
toggle the Truncate gamma values checkbox repeatedly). Then
compare it to value 21. Option 6 is only for this purpose, and Option 7
just counts the values with the different differences for the two
rounding cases. The point is, 8 bits can cause minor "off by one" errors
in gamma data. You probably may never detect this difference in a
screen image. And it's just math, we certainly don't see any way here
that the human eye could help with it? Notice that this difference is Not
just because the gamma data was 8-bits, it is also because the target
video space was 8-bits. But 8-bits is not a big problem. It is our
standard, and it works well.
The math: You can repeat the math yourself for the concept. Here's
how: Gamma must be normalized to a [0..1] data range (divide by 255)
before we do the math. Because when normalized to [0..1], end point
values 0 or 1 to any exponent are still 0 or 1, so the gamma boost
never extends end ranges or causes clipping. The end points are fixed,
it only boosts the midrange, more at the low end (see the graph). Then
our computation must be rounded to an integer, both in the 8-bit file,
and in the 8-bit video space.
This next shows the work to convert linear value 20 to 2.2 gamma
value 80 and then back to linear 19 (19 because it was stored as 8-bit
integers). Again, the computer can do any precise calculations, but
the 8-bit file is limited to storing integers.
Normalize: 20 / 255 = 0.078431 [0..1] range (so this is 7.8% of 255
8/21/2016 9:05 PM
14 of 19
http://www.scantips.com/lights/gamma2.html
full scale)
Gamma value: 0.078431 ^ (1/2.2) = 0.314409 [0..1] range (this is
31% scale on the histogram)
Value = 0.314409 * 255 = 80.174369, rounded to store as integer 80
[0..255] in 8-bit file.
Converting 80 gamma back to linear value 20:
Normalize: 80 / 255 = 0.313725 [0..1] range
Gamma value: 0.313725 ^ 2.2 = 0.078057 [0..1] range
Value = 0.078057 * 255 = 19.90443, stored as 19 [0..255] (8-bit
video space)
19, or 20, depending on how the decoding software processes the
rounding. It has to be an integer, which the receiving hardware can
choose to process by truncating or rounding. If computed, truncation
is simpler and faster than rounding. But IF 20 comes back as 19, this
is what Option 7 calls a difference of one.
8/21/2016 9:05 PM
15 of 19
http://www.scantips.com/lights/gamma2.html
0 21 28 34 39 43 46 50 53 56
59 61 64 66 68 70 72 74 76 78
80 82 84 85 87 89 90 92 93 95
96 98 99 101 102 103 105 106 107 109
110 111 112 114 115 116 117 118 119 120
122 123 124 125 126 127 128 129 130 131
132 133 134 135 136 137 138 139 140 141
142 143 144 144 145 146 147 148 149 150
151 151 152 153 154 155 156 156 157 158
159 160 160 161 162 163 164 164 165 166
167 167 168 169 170 170 171 172 173 173
174 175 175 176 177 178 178 179 180 180
181 182 182 183 184 184 185 186 186 187
188 188 189 190 190 191 192 192 193 194
194 195 195 196 197 197 198 199 199 200
200 201 202 202 203 203 204 205 205 206
206 207 207 208 209 209 210 210 211 212
212 213 213 214 214 215 215 216 217 217
218 218 219 219 220 220 221 221 222 223
223 224 224 225 225 226 226 227 227 228
228 229 229 230 230 231 231 232 232 233
233 234 234 235 235 236 236 237 237 238
238 239 239 240 240 241 241 242 242 243
243 244 244 245 245 246 246 247 247 248
248 249 249 249 250 250 251 251 252 252
253 253 254 254 255 255
8/21/2016 9:05 PM
16 of 19
http://www.scantips.com/lights/gamma2.html
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 2 2 2 2 2
2 2 3 3 3 3 3 4 4 4
4 5 5 5 5 6 6 6 6 7
7 7 8 8 8 9 9 9 10 10
11 11 11 12 12 13 13 13 14 14
15 15 16 16 17 17 18 18 19 19
20 20 21 22 22 23 23 24 25 25
26 26 27 28 28 29 30 30 31 32
33 33 34 35 35 36 37 38 39 39
40 41 42 43 43 44 45 46 47 48
49 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67
68 69 70 71 73 74 75 76 77 78
79 81 82 83 84 85 87 88 89 90
91 93 94 95 97 98 99 100 102 103
105 106 107 109 110 111 113 114 116
119 120 121 123 124 126 127 129 130
133 135 137 138 140 141 143 145 146
149 151 153 154 156 158 159 161 163
166 168 170 172 173 175 177 179 181
184 186 188 190 192 194 196 197 199
203 205 207 209 211 213 215 217 219
223 225 227 229 231 234 236 238 240
244 246 248 251 253 255
117
132
148
165
182
201
221
242
->
->
->
->
150,
151,
151,
152,
150
151
151
152
->
->
->
->
79
81 But what could we change?
81
82
8/21/2016 9:05 PM
17 of 19
http://www.scantips.com/lights/gamma2.html
percentage wise, but in 8 bits, 1,2,3,4 is all they can be called. And of
course, they are the exact values we hope to reproduce (however real
world video monitors probably cannot show levels that black, which
cause is neither gamma nor 8-bits). Notions about the human eye can't
help (the eye never sees gamma data). An analog CRT is all that sees
any gamma data directly, but the eye notion is the full opposite,
imagining gamma is still somehow needed without CRT? Anyway,
gamma is obviously not related to the human eyes response in any
way. Our only problem is that 8-bit values can only show integer values
150, 151, 152, ... but linear 80 computes about gamma 150.5. But off
by one is not a big deal, since there are so many other variables
anyway. White Balance for one for example, Vivid for another, these
skew the camera data. So this is an instance to not sweat the small
stuff.
The LUT is not entirely wasted effort for LCD, because the table can also
be modified to additionally correct for any other color nonlinearity in
this specific device, monitor color calibration procedures for example, or
just routine corrections. The LUT provides the mechanism to expand it
further (if the data is this, then show that). Color correction would
require three such tables, for each of red, green and blue. This table is
for gamma 2.2, but other tables are quickly created. A 12-bit encode
table would need 4096 values, a larger table, but it still reads just as
fast.
The lowest linear values, like 4, 5, 6, may seem to be coarse steps
(percentage-wise), but they are what they are (still the smallest
possible steps of 1), and are what we've got to work with, and are the
exact values that we need to reproduce, regardless if we use gamma or
if we hypothetically somehow could bypass it. These low values are
black tones, and the monitor may not be able to reproduce them well
anyway (monitors cannot show the blackest black).
But gamma is absolutely Not related to the response of the eye, and
gamma is obviously NOT done for the eye (that's nonsense, and of
course, we don't even need to know anything about the eyes
response). The eye never ever sees any gamma data, because gamma
is always first completely decoded back to linear. We couldn't care less
what the eye does with it, the eye does what it does, but it is happiest
to see an exact linear reproduction of the original linear scene, the same
as if it were still standing there looking.
Note that Options 6 & 7 convert linear values to gamma, and then back
to linear, and looks for a difference due to 8-bit rounding. That's all 6
does, but Option 7 does all possible values, to see how things are
going. But our photos were all encoded elsewhere at 12 bits (in the
camera, or in the scanner, or in raw, etc), so encoding is not our 8-bit
issue (it's already done). So my procedure is that Option 6 & 7 always
round the input encoded values, and only uses the Truncate gamma
values checkbox for the decoding, which will convert the 8-bit output
8/21/2016 9:05 PM
18 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM
19 of 19
http://www.scantips.com/lights/gamma2.html
8/21/2016 9:05 PM