krita

kis_integer_maths.h

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