F:/KPlato/koffice/libs/pigment/KoIntegerMaths.h

Aller à la documentation de ce fichier.
00001 /*
00002  *  Copyright (c) 2005 Adrian Page <adrian@pagenet.plus.com>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KIS_INTEGER_MATHS_H
00021 #define KIS_INTEGER_MATHS_H
00022 
00023 #define UINT8_MAX 255u
00024 #define UINT8_MIN 0u
00025 
00026 #define UINT16_MAX 65535u
00027 #define UINT16_MIN 0u
00028 
00029 #define UINT32_MAX (4294967295u)
00030 #define UINT32_MIN 0u
00031 
00032 #define INT16_MAX 32767
00033 #define INT16_MIN -32768
00034 
00035 #define CLAMP(x,l,u) ((x)<(l)?(l):((x)>(u)?(u):(x)))
00036 
00037 
00039 inline uint UINT8_SCALEBY(uint a, uint b)
00040 {
00041     uint c = a * b + 0x80u;
00042     return (c >> 8) + c;
00043 }
00044 
00045 inline uint UINT8_MULT(uint a, uint b)
00046 {
00047     uint c = a * b + 0x80u;
00048     return ((c >> 8) + c) >> 8;
00049 }
00050 
00051 inline uint UINT8_DIVIDE(uint a, uint b)
00052 {
00053     uint c = (a * UINT8_MAX + (b / 2u)) / b;
00054     return c;
00055 }
00056 
00057 inline uint UINT8_BLEND(uint a, uint b, uint alpha)
00058 {
00059     // Basically we do a*alpha + b*(1-alpha)
00060     // However refactored to (a-b)*alpha + b  since that saves a multiplication
00061     // Signed arithmetic is needed since a-b might be negative
00062     int c = ((int(a) - int(b)) * int(alpha)) >> 8;
00063 
00064     return uint(c + b);
00065 }
00066 
00067 inline uint UINT16_MULT(uint a, uint b)
00068 {
00069     uint c = a * b + 0x8000u;
00070     return ((c >> 16) + c) >> 16;
00071 }
00072 
00073 inline int INT16_MULT(int a, int b)
00074 {
00075     return (a*b) / INT16_MAX;
00076 }
00077 
00078 inline uint UINT16_DIVIDE(uint a, uint b)
00079 {
00080     uint c = (a * UINT16_MAX + (b / 2u)) / b;
00081     return c;
00082 }
00083 
00084 inline uint UINT16_BLEND(uint a, uint b, uint alpha)
00085 {
00086     // Basically we do a*alpha + b*(1-alpha)
00087     // However refactored to (a-b)*alpha + b  since that saves a multiplication
00088     // Signed arithmetic is needed since a-b might be negative
00089     int c = ((int(a) - int(b)) * int(alpha)) >> 16;
00090     return uint(c + b);
00091 }
00092 
00093 inline uint UINT8_TO_UINT16(uint c)
00094 {
00095     return c | (c<<8);
00096 }
00097 
00098 inline uint UINT16_TO_UINT8(uint c)
00099 {
00100     //return round(c / 257.0);
00101     //For all UINT16 this calculation is the same and a lot faster (off by c/65656 which for every c is 0)
00102     c = c - (c >> 8) + 128;
00103     return c >>8;
00104 }
00105 
00106 inline int INT16_BLEND(int a, int b, uint alpha)
00107 {
00108     // Basically we do a*alpha + b*(1-alpha)
00109     // However refactored to (a-b)*alpha + b  since that saves a multiplication
00110     int c = ((int(a) - int(b)) * int(alpha)) >> 16;
00111     return c + b;
00112 }
00113 
00114 #endif
00115 

Généré le Wed Nov 22 23:41:13 2006 pour KPlato par  doxygen 1.5.1-p1