iceWing
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Gcolor.h
Go to the documentation of this file.
1 /* -*- mode: C; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /*
4  * Author: Frank Loemker
5  *
6  * Copyright (C) 1999-2009
7  * Frank Loemker, Applied Computer Science, Faculty of Technology,
8  * Bielefeld University, Germany
9  *
10  * This file is part of iceWing, a graphical plugin shell.
11  *
12  * iceWing is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * iceWing is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  */
26 
27 #ifndef iw_Gcolor_H
28 #define iw_Gcolor_H
29 
30 #include <gtk/gtk.h>
31 #include "Gimage.h"
32 
33 #ifndef gui_lrint
34 
35 #define __USE_ISOC9X 1
36 #define __USE_ISOC99 1
37 #include <math.h>
38 
39 #if defined(__GNUC__) && defined(__i386__)
40 static inline long __gui_lrint_code (double v)
41 {
42  long ret;
43  __asm__ __volatile__ ("fistpl %0" : "=m" (ret) : "t" (v) : "st") ;
44  return ret;
45 }
46 #elif defined(__GLIBC__) && defined(__GNUC__) && __GNUC__ >= 3
47 #define __gui_lrint_code(v) lrint(v)
48 #else
49 static inline long __gui_lrint_code (double v)
50 {
51  if (v > 0)
52  return (long)(v+0.5);
53  else
54  return (long)(v-0.5);
55 }
56 #endif
57 
58 # define gui_lrint(v) __gui_lrint_code(v)
59 
60 #endif
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 /*********************************************************************
67  Perform RGB to HSV conversion. The intervall is allways 0..255.
68 *********************************************************************/
69 void prev_rgbToHsv (guchar rc, guchar gc, guchar bc,
70  guchar *h, guchar *s, guchar *v);
71 
72 /*********************************************************************
73  Perform HSV to RGB conversion. The intervall is allways 0..255.
74 *********************************************************************/
75 void prev_hsvToRgb (guchar h, guchar s, guchar v,
76  guchar *rc, guchar *gc, guchar *bc);
77 
78 /*********************************************************************
79  Perform YUV to RGB conversion with intervall expansion.
80 *********************************************************************/
81 void prev_yuvToRgbVis (guchar yc, guchar uc, guchar vc,
82  guchar *rc, guchar *gc, guchar *bc);
83 
84 /*********************************************************************
85  Perform RGB to YUV conversion with intervall reduction.
86 *********************************************************************/
87 void prev_rgbToYuvVis (guchar rc, guchar gc, guchar bc,
88  guchar *yc, guchar *uc, guchar *vc);
89 
90 /* PRIVATE */
91 #define PREVCOL_SHIFT 10
92 #define PREVCOL_HALF (1 << (PREVCOL_SHIFT - 1))
93 #define PREVCOL_FIX(x) ((int) ((x) * (1<<PREVCOL_SHIFT) + 0.5))
94 
95 #define PREVCOL_MAX_NEG_CROP 1024
96 extern guchar prev_col_cropTbl[256 + 2 * PREVCOL_MAX_NEG_CROP];
97 /* END PRIVATE */
98 
99 /*********************************************************************
100  Perform YUV to RGB conversion with intervall expansion.
101 *********************************************************************/
102 static inline void prev_inline_yuvToRgbVis (guchar yc, guchar uc, guchar vc,
103  guchar *rc, guchar *gc, guchar *bc)
104 {
105  guchar *ct = prev_col_cropTbl + PREVCOL_MAX_NEG_CROP;
106  int U = uc - 128;
107  int V = vc - 128;
108  int r_add = PREVCOL_FIX(1.40252*255.0/224.0) * V;
109  int g_add = - PREVCOL_FIX(0.34434*255.0/224.0) * U
110  - PREVCOL_FIX(0.71440*255.0/224.0) * V;
111  int b_add = PREVCOL_FIX(1.77304*255.0/224.0) * U;
112  int y = PREVCOL_FIX(255.0/219.0) * (yc - 16) + PREVCOL_HALF;
113 
114  *rc = ct[(y + r_add) >> PREVCOL_SHIFT];
115  *gc = ct[(y + g_add) >> PREVCOL_SHIFT];
116  *bc = ct[(y + b_add) >> PREVCOL_SHIFT];
117 }
118 
119 /*********************************************************************
120  Perform RGB to YUV conversion with intervall reduction.
121 *********************************************************************/
122 static inline void prev_inline_rgbToYuvVis (guchar rc, guchar gc, guchar bc,
123  guchar *yc, guchar *uc, guchar *vc)
124 
125 {
126  int y_shift = PREVCOL_FIX(0.299*219.0/255.0) * rc +
127  PREVCOL_FIX(0.587*219.0/255.0) * gc +
128  PREVCOL_FIX(0.114*219.0/255.0) * bc + PREVCOL_HALF;
129  int y = y_shift >> PREVCOL_SHIFT;
130 
131  *uc = ((PREVCOL_FIX(0.564*224.0/255.0) * bc -
132  PREVCOL_FIX(0.564*224.0/219.0) * y + PREVCOL_HALF) >> PREVCOL_SHIFT) + 128;
133  *vc = ((PREVCOL_FIX(0.713*224.0/255.0) * rc -
134  PREVCOL_FIX(0.713*224.0/219.0) * y + PREVCOL_HALF) >> PREVCOL_SHIFT) + 128;
135  *yc = (y_shift + (16 << PREVCOL_SHIFT)) >> PREVCOL_SHIFT;
136 }
137 
138 /*********************************************************************
139  Perform RGB to YUV conversion without intervall change.
140 *********************************************************************/
141 static inline void prev_inline_rgbToYuvCal (guchar rc, guchar gc, guchar bc,
142  guchar *yc, guchar *uc, guchar *vc)
143 {
144  guchar *ct = prev_col_cropTbl + PREVCOL_MAX_NEG_CROP;
145  int y = (PREVCOL_FIX(0.299) * rc +
146  PREVCOL_FIX(0.587) * gc +
147  PREVCOL_FIX(0.114) * bc + PREVCOL_HALF) >> PREVCOL_SHIFT;
148  *yc = y;
149  *uc = ct[((PREVCOL_FIX(0.564*224.0/219.0) * (bc-y) +
150  PREVCOL_HALF) >> PREVCOL_SHIFT) + 128];
151  *vc = ct[((PREVCOL_FIX(0.713*224.0/219.0) * (rc-y) +
152  PREVCOL_HALF) >> PREVCOL_SHIFT) + 128];
153 }
154 
155 /*********************************************************************
156  PRIVATE: Initialize the color module.
157 *********************************************************************/
158 void prev_col_init (void);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif /* iw_Gcolor_H */