aboutsummaryrefslogtreecommitdiff
path: root/modules/mod_blendop/mod_blendop.c
blob: 64980f785877cdeb8f3a9e1104b7d197da6023f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/* ----------------------------------------------------------------- */

#include "bgdrtm.h"
#include "bgddl.h"

#include "libgrbase.h"

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_create_blendop
 *
 *  Create a new blendop table and initialize it with blend_init
 *  A blendop table is a group of two tables that return intermediate
 *  colors to a composite (+) operation that does no saturation:
 *
 *      Src_param   =   Src_color
 *      Dst_param   =   Dst_color
 *      Dst_color   =   Src_param + Dst_param       // No clamp!
 *
 *  The other blend_x funcions change the Src_param and Dst_param formulas.
 *  The Dst_color formula is immutable and embedded in the blitter
 *
 *  PARAMS :
 *      None
 *
 *  RETURN VALUE :
 *      Pointer to the new blendop table or NULL if not enough memory
 */

static int modblendop_create_blendop( INSTANCE * my, int * params )
{
    return ( int ) blend_create();
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_apply
 *
 *  Apply a blend operation to the pixels of a graphic, as if it was
 *  rendered into a black (color 0) background
 *
 *  PARAMS :
 *      map             Pointer to the graphic
 *      blend           Pointer to the blend table
 *
 *  RETURN VALUE :
 *      0               Error
 *      1               OK
 */

static int modblendop_apply( INSTANCE * my, int * params )
{
    GRAPH * graph = bitmap_get( params[0], params[1] );
    if ( !graph ) return 0;
    blend_apply( graph, ( int16_t * )params[2] );
    return 1;

}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_assign
 *
 *  Assign a blend operation to a graphic. The graphic will be
 *  drawn using this blend operation thereafter.
 *
 *  PARAMS :
 *      map             Pointer to the graphic
 *      blend           Pointer to the blend table
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_assign( INSTANCE * my, int * params )
{
    GRAPH * graph = bitmap_get( params[0], params[1] );
    if ( !graph ) return 0;
    blend_assign( graph, ( int16_t * )params[2] );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_free
 *
 *  Free the memory used by a blendop table
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_free( INSTANCE * my, int * params )
{
    blend_free(( int16_t * )params[0] );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_identity
 *
 *  Restore default blendop table
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_identity( INSTANCE * my, int * params )
{
    blend_init(( int16_t * )params[0] );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_grayscale
 *
 *  Initialize a blend table as a grayscale operation (changes the
 *  color value of the source object to grayscale). Three methods
 *  of operation are supported:
 *
 *      1.  Luminance:  result = 0.3R + 0.59G + 0.11B
 *      2.  Desaturate: result = (MAX(R,G,B)+MIN(R,G,B))/2
 *      3.  Maximum:    result = MAX(R,G,B)
 *
 *
 *      Src_param   =   Grayscale_operation(previous Src_param)
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *      method          Method of operation
 *
 *  RETURN VALUE :
 *      0               Error
 *      1               Ok
 */

static int modblendop_grayscale( INSTANCE * my, int * params )
{
    blend_grayscale(( int16_t * )params[0], params[1] );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_translucency
 *
 *  Modify a blend table as a translucency combination operation
 *
 *      Src_param   =   (previous Src_param) * ammount
 *      Dst_param   =   Dst_color * (1.0 - ammount)
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *      ammount         Opacity factor (1.0f = opaque, 0.0f = transparent)
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_translucency( INSTANCE * my, int * params )
{
    blend_translucency(( int16_t * )params[0], *( float * )( &params[1] ) );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_intensity
 *
 *  Modify a blend table as an intensity operation (changes the
 *  color value of the source object as the factor ammount but
 *  does no transparency or other operation with the background)
 *
 *      Src_param   =   (previous Src_param) * ammount
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *      ammount         Color component factor (1.0f leaves source unchanged)
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_intensity( INSTANCE * my, int * params )
{
    blend_intensity(( int16_t * )params[0], *( float * )( &params[1] ) );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_swap
 *
 *  Swaps a blendop table. That is, change the blend operation so
 *  the Dst_color and Src_color are exchanged in the formulas.
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_swap( INSTANCE * my, int * params )
{
    blend_swap(( int16_t * )params[0] );
    return 1;
}

/* ----------------------------------------------------------------- */
/*
 *  FUNCTION : modblendop_tint
 *
 *  Modify a blend table as a tint operation (changes the
 *  color value of the source object as a combination of a given
 *  color and the source color with the factor given)
 *
 *      Src_param = Const_color * ammount
 *                  + (previous Src_Param) * (1.0f-ammount)
 *
 *  PARAMS :
 *      blend           Pointer to the blend table
 *      ammount         Color component factor (1.0f = Full Const color)
 *      cr              Constant color, red component (0-255)
 *      cg              Constant color, green component (0-255)
 *      cb              Constant color, blue component (0-255)
 *
 *  RETURN VALUE :
 *      None
 */

static int modblendop_tint( INSTANCE * my, int * params )
{
    blend_tint(( int16_t * )params[0], *( float * )( &params[1] ), ( uint8_t ) params[2], ( uint8_t ) params[3], ( uint8_t ) params[4] );
    return 1;
}

/* ----------------------------------------------------------------- */
/* exports                                                           */
/* ----------------------------------------------------------------- */

#include "mod_blendop_exports.h"

/* ----------------------------------------------------------------- */