]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ShivaVG/src/shImage.h
Update for OpenSceneGraph 3.3.2 API changes.
[simgear.git] / simgear / canvas / ShivaVG / src / shImage.h
1 /*
2  * Copyright (c) 2007 Ivan Leben
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library in the file COPYING;
16  * if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #ifndef __SHIMAGE_H
22 #define __SHIMAGE_H
23
24 #include "shDefs.h"
25
26 /*-----------------------------------------------------------
27  * ColorFormat holds the data necessary to pack/unpack color
28  * components from a pixel of each supported image format
29  *-----------------------------------------------------------*/
30
31 typedef struct
32 {
33   VGImageFormat vgformat;
34   SHuint8 bytes;
35   
36   SHuint32 rmask;
37   SHuint8 rshift;
38   SHuint8 rmax;
39   
40   SHuint32 gmask;
41   SHuint8 gshift;
42   SHuint8 gmax;
43   
44   SHuint32 bmask;
45   SHuint8 bshift;
46   SHuint8 bmax;
47   
48   SHuint32 amask;
49   SHuint8 ashift;
50   SHuint8 amax;
51
52   GLenum glintformat;
53   GLenum glformat;
54   GLenum gltype;
55
56 } SHImageFormatDesc;
57
58 typedef struct
59 {
60   SHfloat r,g,b,a;
61   
62 } SHColor;
63
64 void SHColor_ctor(SHColor *c);
65 void SHColor_dtor(SHColor *c);
66
67 #define _ITEM_T SHColor
68 #define _ARRAY_T SHColorArray
69 #define _FUNC_T shColorArray
70 #define _ARRAY_DECLARE
71 #include "shArrayBase.h"
72
73 typedef struct
74 {
75   SHuint8 *data;
76   SHint width;
77   SHint height;
78   SHImageFormatDesc fd;
79   
80   SHint texwidth;
81   SHint texheight;
82   SHfloat texwidthK;
83   SHfloat texheightK;
84   GLuint texture;
85   
86 } SHImage;
87
88 void SHImage_ctor(SHImage *i);
89 void SHImage_dtor(SHImage *i);
90
91 #define _ITEM_T SHImage*
92 #define _ARRAY_T SHImageArray
93 #define _FUNC_T shImageArray
94 #define _ARRAY_DECLARE
95 #include "shArrayBase.h"
96
97 /*-------------------------------------------------------
98  * Color operators
99  *-------------------------------------------------------*/
100
101 #define CSET(c, rr,gg,bb,aa) { c.r=rr; c.g=gg; c.b=bb; c.a=aa; }
102 #define CSETC(c1, c2) { c1.r=c2.r; c1.g=c2.g; c1.b=c2.b; c1.a=c2.a; }
103
104 #define CSUB(c1, rr,gg,bb,aa) { c.r-=rr; c.g-=gg; c.b-=bb; c.a-=aa; }
105 #define CSUBC(c1, c2) { c1.r-=c2.r; c1.g-=c2.g; c1.b-=c2.b; c1.a-=c2.a; }
106 #define CSUBCTO(c1, c2, c3) { c3.r=c1.r-c2.r; c3.g=c1.g-c2.g;  c3.b=c1.b-c2.b; c3.a=c1.a-c2.a; }
107
108 #define CADD(c1, rr,gg,bb,aa) { c.r+=rr; c.g+=gg; c.b+=bb; c.a+=aa; }
109 #define CADDC(c1, c2) { c1.r+=c2.r; c1.g+=c2.g; c1.b+=c2.b; c1.a+=c2.a; }
110 #define CADDTO(c1, c2, c3) { c3.r=c1.r+c2.r; c3.g=c1.g+c2.g;  c3.b=c1.b+c2.b; c3.a=c1.a+c2.a; }
111 #define CADDCK(c1, c2, k) { c1.r+=k*c2.r; c1.g+=k*c2.g; c1.b+=k*c2.b; c1.a+=k*c2.a; }
112
113 #define CMUL(c, s) { c.r*=s; c.g*=s; c.b*=s; c.a*=s; }
114 #define CDIV(c, s) { c.r/=s; c.g/=s; c.b/=s; c.a/=s; }
115
116 #define CPREMUL(c) { c.r*=c.a; c.g*=c.a; c.b*=c.a; }
117 #define CUNPREMUL(c) { c.r/=c.a; c.g/=c.a; c.b/=c.a; }
118
119 /*-------------------------------------------------------
120  * Color-to-memory functions
121  *-------------------------------------------------------*/
122
123 #define CSTORE_RGBA1D_8(c, rgba, x)  { \
124   rgba[x*4+0] = (SHuint8)SH_FLOOR(c.r * 255); \
125   rgba[x*4+1] = (SHuint8)SH_FLOOR(c.g * 255); \
126   rgba[x*4+2] = (SHuint8)SH_FLOOR(c.b * 255); \
127   rgba[x*4+3] = (SHuint8)SH_FLOOR(c.a * 255); }
128
129 #define CSTORE_RGBA1D_F(c, rgba, x)  { \
130   rgba[x*4+0] = c.r; \
131   rgba[x*4+1] = c.g; \
132   rgba[x*4+2] = c.b; \
133   rgba[x*4+3] = c.a; }
134
135 #define CSTORE_RGBA2D_8(c, rgba, x, y, width) { \
136   rgba[y*width*4+x*4+0] = (SHuint8)SH_FLOOR(c.r * 255); \
137   rgba[y*width*4+x*4+1] = (SHuint8)SH_FLOOR(c.g * 255); \
138   rgba[y*width*4+x*4+2] = (SHuint8)SH_FLOOR(c.b * 255); \
139   rgba[y*width*4+x*4+3] = (SHuint8)SH_FLOOR(c.a * 255); }
140
141 #define CSTORE_RGBA2D_F(c, rgba, x, y, width) { \
142   rgba[y*width*4+x*4+0] = c.r; \
143   rgba[y*width*4+x*4+1] = c.g; \
144   rgba[y*width*4+x*4+2] = c.b; \
145   rgba[y*width*4+x*4+3] = c.a; }
146
147 #define INT2COLCOORD(i, max) ( (SHfloat)i / (SHfloat)max  )
148 #define COL2INTCOORD(c, max) ( (SHuint)SH_FLOOR(c * (SHfloat)max + 0.5f) )
149
150 void shStoreColor(SHColor *c, void *data, SHImageFormatDesc *f);
151 void shLoadColor(SHColor *c, const void *data, SHImageFormatDesc *f);
152
153
154 #endif /* __SHIMAGE_H */