MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_GammaCorrect.cpp
Go to the documentation of this file.
00001 /* Copyright (c) 2011, Make, Hack, Void Inc
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *  * Redistributions of source code must retain the above copyright
00007  *    notice, this list of conditions and the following disclaimer.
00008  *  * Redistributions in binary form must reproduce the above copyright
00009  *    notice, this list of conditions and the following disclaimer in the
00010  *    documentation and/or other materials provided with the distribution.
00011  *  * Neither the name of the Make, Hack, Void nor the
00012  *    names of its contributors may be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00016  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00020  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00021  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00022  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 
00027 
00028 #include "MHV_GammaCorrect.h"
00029 #include <avr/pgmspace.h>
00030 
00031 const uint8_t mhv_gammaValues[] PROGMEM = {
00032         0,              0,              0,              0,              0,              0,              0,              0,
00033         0,              0,              0,              0,              0,              0,              0,              0,
00034         0,              0,              0,              0,              0,              0,              1,              1,
00035         1,              1,              1,              1,              1,              1,              1,              1,
00036         1,              2,              2,              2,              2,              2,              2,              2,
00037         2,              3,              3,              3,              3,              3,              4,              4,
00038         4,              4,              4,              5,              5,              5,              5,              6,
00039         6,              6,              6,              7,              7,              7,              7,              8,
00040         8,              8,              9,              9,              9,              10,             10,             10,
00041         11,             11,             12,             12,             12,             13,             13,             14,
00042         14,             15,             15,             15,             16,             16,             17,             17,
00043         18,             18,             19,             19,             20,             20,             21,             22,
00044         22,             23,             23,             24,             25,             25,             26,             26,
00045         27,             28,             28,             29,             30,             30,             31,             32,
00046         33,             33,             34,             35,             36,             36,             37,             38,
00047         39,             40,             40,             41,             42,             43,             44,             45,
00048         46,             46,             47,             48,             49,             50,             51,             52,
00049         53,             54,             55,             56,             57,             58,             59,             60,
00050         61,             62,             63,             64,             65,             67,             68,             69,
00051         70,             71,             72,             73,             75,             76,             77,             78,
00052         80,             81,             82,             83,             85,             86,             87,             89,
00053         90,             91,             93,             94,             95,             97,             98,             99,
00054         101,    102,    104,    105,    107,    108,    110,    111,
00055         113,    114,    116,    117,    119,    121,    122,    124,
00056         125,    127,    129,    130,    132,    134,    135,    137,
00057         139,    141,    142,    144,    146,    148,    150,    151,
00058         153,    155,    157,    159,    161,    163,    165,    166,
00059         168,    170,    172,    174,    176,    178,    180,    182,
00060         184,    186,    189,    191,    193,    195,    197,    199,
00061         201,    204,    206,    208,    210,    212,    215,    217,
00062         219,    221,    224,    226,    228,    231,    233,    235,
00063         238,    240,    243,    245,    248,    250,    253,    255
00064 };
00065 
00066 /* Gamma correct a value via calculation
00067  * This requires libm.a linked in (-lm on the linker command line)
00068  * param        value   the value to gamma correct
00069  * return the gamma corrected value
00070  */
00071 uint8_t mhv_calculatedGammaCorrect(uint8_t value) {
00072         return (uint8_t) round(255 * pow(value / 256, 2.5));
00073 }
00074 
00075 /* Gamma correct a value via calculation
00076  * This uses less program memory than calculated value, and uses less clocks
00077  * param        value   the value to gamma correct
00078  * return the gamma corrected value
00079  */
00080 uint8_t mhv_precalculatedGammaCorrect(uint8_t value) {
00081         return pgm_read_byte(mhv_gammaValues + value);
00082 }