| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifndef __HQX_COMMON_H_ |
| 23 |
#define __HQX_COMMON_H_ |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdint.h> |
| 27 |
|
| 28 |
#define MASK_2 0x0000FF00 |
| 29 |
#define MASK_13 0x00FF00FF |
| 30 |
#define MASK_RGB 0x00FFFFFF |
| 31 |
#define MASK_ALPHA 0xFF000000 |
| 32 |
|
| 33 |
#define Ymask 0x00FF0000 |
| 34 |
#define Umask 0x0000FF00 |
| 35 |
#define Vmask 0x000000FF |
| 36 |
#define trY 0x00300000 |
| 37 |
#define trU 0x00000700 |
| 38 |
#define trV 0x00000006 |
| 39 |
|
| 40 |
|
| 41 |
extern uint32_t RGBtoYUV[16777216]; |
| 42 |
|
| 43 |
static inline uint32_t rgb_to_yuv(uint32_t c) |
| 44 |
{ |
| 45 |
|
| 46 |
return RGBtoYUV[MASK_RGB & c]; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
static inline int yuv_diff(uint32_t yuv1, uint32_t yuv2) { |
| 51 |
return (( abs((yuv1 & Ymask) - (yuv2 & Ymask)) > trY ) || |
| 52 |
( abs((yuv1 & Umask) - (yuv2 & Umask)) > trU ) || |
| 53 |
( abs((yuv1 & Vmask) - (yuv2 & Vmask)) > trV ) ); |
| 54 |
} |
| 55 |
|
| 56 |
static inline int Diff(uint32_t c1, uint32_t c2) |
| 57 |
{ |
| 58 |
return yuv_diff(rgb_to_yuv(c1), rgb_to_yuv(c2)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static inline uint32_t Interpolate_2(uint32_t c1, int w1, uint32_t c2, int w2, int s) |
| 63 |
{ |
| 64 |
if (c1 == c2) { |
| 65 |
return c1; |
| 66 |
} |
| 67 |
return |
| 68 |
(((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2) << (24-s)) & MASK_ALPHA) + |
| 69 |
((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2) >> s) & MASK_2) + |
| 70 |
((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2) >> s) & MASK_13); |
| 71 |
} |
| 72 |
|
| 73 |
static inline uint32_t Interpolate_3(uint32_t c1, int w1, uint32_t c2, int w2, uint32_t c3, int w3, int s) |
| 74 |
{ |
| 75 |
return |
| 76 |
(((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2 + ((c3 & MASK_ALPHA) >> 24) * w3) << (24-s)) & MASK_ALPHA) + |
| 77 |
((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2 + (c3 & MASK_2) * w3) >> s) & MASK_2) + |
| 78 |
((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2 + (c3 & MASK_13) * w3) >> s) & MASK_13); |
| 79 |
} |
| 80 |
|
| 81 |
static inline void Interp1(uint32_t * pc, uint32_t c1, uint32_t c2) |
| 82 |
{ |
| 83 |
|
| 84 |
*pc = Interpolate_2(c1, 3, c2, 1, 2); |
| 85 |
} |
| 86 |
|
| 87 |
static inline void Interp2(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 88 |
{ |
| 89 |
|
| 90 |
*pc = Interpolate_3(c1, 2, c2, 1, c3, 1, 2); |
| 91 |
} |
| 92 |
|
| 93 |
static inline void Interp3(uint32_t * pc, uint32_t c1, uint32_t c2) |
| 94 |
{ |
| 95 |
|
| 96 |
*pc = Interpolate_2(c1, 7, c2, 1, 3); |
| 97 |
} |
| 98 |
|
| 99 |
static inline void Interp4(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 100 |
{ |
| 101 |
|
| 102 |
*pc = Interpolate_3(c1, 2, c2, 7, c3, 7, 4); |
| 103 |
} |
| 104 |
|
| 105 |
static inline void Interp5(uint32_t * pc, uint32_t c1, uint32_t c2) |
| 106 |
{ |
| 107 |
|
| 108 |
*pc = Interpolate_2(c1, 1, c2, 1, 1); |
| 109 |
} |
| 110 |
|
| 111 |
static inline void Interp6(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 112 |
{ |
| 113 |
|
| 114 |
*pc = Interpolate_3(c1, 5, c2, 2, c3, 1, 3); |
| 115 |
} |
| 116 |
|
| 117 |
static inline void Interp7(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 118 |
{ |
| 119 |
|
| 120 |
*pc = Interpolate_3(c1, 6, c2, 1, c3, 1, 3); |
| 121 |
} |
| 122 |
|
| 123 |
static inline void Interp8(uint32_t * pc, uint32_t c1, uint32_t c2) |
| 124 |
{ |
| 125 |
|
| 126 |
*pc = Interpolate_2(c1, 5, c2, 3, 3); |
| 127 |
} |
| 128 |
|
| 129 |
static inline void Interp9(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 130 |
{ |
| 131 |
|
| 132 |
*pc = Interpolate_3(c1, 2, c2, 3, c3, 3, 3); |
| 133 |
} |
| 134 |
|
| 135 |
static inline void Interp10(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) |
| 136 |
{ |
| 137 |
|
| 138 |
*pc = Interpolate_3(c1, 14, c2, 1, c3, 1, 4); |
| 139 |
} |
| 140 |
|
| 141 |
#endif |