summaryrefslogtreecommitdiff
path: root/src/lib/evas/filters/evas_filter_bump.c
blob: f89ec3c2adb0f913466605724f3a852513c81d05 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* Simple bump map algorithms for the software engine */

#include "evas_filter_private.h"
#include "evas_blend_private.h"

#include <math.h>

#ifdef CLAMP
# undef CLAMP
#endif
#define CLAMP(a,b,c) MIN(MAX((b),(a)),(c))

#define DEFAULT_ZANGLE 45.f

static Eina_Bool _bump_map_cpu_alpha_alpha(Evas_Filter_Command *cmd);
static Eina_Bool _bump_map_cpu_alpha_rgba(Evas_Filter_Command *cmd);

Evas_Filter_Apply_Func
evas_filter_bump_map_cpu_func_get(Evas_Filter_Command *cmd)
{
   int w, h;

   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->input, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->mask, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->output, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->input != cmd->output, NULL);

   w = cmd->input->w;
   h = cmd->input->h;
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->output->w == w, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->output->h == h, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->mask->w == w, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->mask->h == h, NULL);

   // FIXME: Bump map support is not implemented for RGBA input!

   if (cmd->output->alpha_only)
     return _bump_map_cpu_alpha_alpha;
   else
     return _bump_map_cpu_alpha_rgba;
}

static void
_phong_alpha_generate(uint8_t *phong, uint8_t dark, uint8_t color, uint8_t white,
                      float sf)
{
   int x, y;

   // FIXME: Flat surfaces should be of color COLOR when compensate is set
   // FIXME: Include white in the computation for specular light
   (void) white;
   (void) sf;

   /*
   float3 lightDir = light.position - pos3D; //3D position in space of the surface
   float distance = length( lightDir );
   lightDir = lightDir / distance; // = normalize( lightDir );
   distance = distance * distance; //This line may be optimised using Inverse square root

   //Intensity of the diffuse light. Saturate to keep within the 0-1 range.
   float NdotL = dot( normal, lightDir );
   float intensity = saturate( NdotL );

   // Calculate the diffuse light factoring in light color, power and the attenuation
   OUT.Diffuse = intensity * light.diffuseColor * light.diffusePower / distance;

   //Calculate the half vector between the light vector and the view vector.
   //This is faster than calculating the actual reflective vector.
   float3 H = normalize( lightDir + viewDir );

   //Intensity of the specular light
   float NdotH = dot( normal, H );
   intensity = pow( saturate( NdotH ), specularHardness );

   //Sum up the specular light factoring
   OUT.Specular = intensity * light.specularColor * light.specularPower / distance;
   */

   for (y = 0; y < 256; y++)
     for (x = 0; x < 256; x++)
       {
          float dx = (127.5 - x);
          float dy = (127.5 - y);
          float dist = sqrt(dx*dx + dy*dy) * 2.;
          int diff = dark + MAX(255 - dist, 0) * (color - dark) / 255;
          int spec = 0; // TODO
          phong[x + (y << 8)] = MIN(MAX(diff + spec, 0), 255);
       }
}

static Eina_Bool
_bump_map_cpu_alpha_alpha(Evas_Filter_Command *cmd)
{
   uint8_t *src_map, *map_map, *dst_map;
   uint8_t *src, *map, *dst, *map_y1, *map_y2;
   uint8_t dark, color, white;
   uint8_t *phong = NULL;
   Eina_Bool ret = EINA_FALSE;
   int x, y, w, h, lx, ly;
   unsigned int ss, ms, ds, slen, dlen, mlen;
   float xyangle, zangle, sf, lxy;

   w = cmd->input->w;
   h = cmd->input->h;
   EINA_SAFETY_ON_FALSE_RETURN_VAL(w > 2 && h > 2, EINA_FALSE);

   src_map = src = _buffer_map_all(cmd->input->buffer, &slen, E_READ, E_ALPHA, &ss);
   map_map = map = _buffer_map_all(cmd->mask->buffer, &mlen, E_READ, E_ALPHA, &ms);
   dst_map = dst = _buffer_map_all(cmd->output->buffer, &dlen, E_WRITE, E_ALPHA, &ds);
   EINA_SAFETY_ON_FALSE_GOTO(src && dst && map, end);

   xyangle = cmd->bump.xyangle;
   zangle = cmd->bump.zangle;
   sf = cmd->bump.specular_factor;

   dark = cmd->bump.dark >> 24;
   white = cmd->bump.white >> 24;
   color = cmd->bump.color >> 24;

   // Convenience for alpha output only
   if ((!dark && !white && !color) ||
       (dark == 0xff && white == 0xff && color == 0xff))
     {
        INF("Bump colors are all 0 or 255. Using low byte instead of alpha.");
        dark = cmd->bump.dark & 0xff;
        white = cmd->bump.white & 0xff;
        color = cmd->bump.color & 0xff;
     }

   // Compute appropriate lx, ly
   if (fabsf(zangle) >= 90.f)
     {
        WRN("Z angle was defined as %.0f, out of range. Defaults to %.0f.",
            zangle, DEFAULT_ZANGLE);
        zangle = DEFAULT_ZANGLE;
     }

   lxy = sin(fabs(zangle * M_PI / 180.));
   lx = (int) (40.f * (lxy + 1.0) * cos(xyangle * M_PI / 180.));
   ly = (int) (40.f * (lxy + 1.0) * sin(xyangle * M_PI / 180.));
   INF("Using light vector (%d,%d)", lx, ly);

   // Generate light table
   phong = malloc(256 * 256 * sizeof(*phong));
   EINA_SAFETY_ON_NULL_GOTO(phong, end);
   _phong_alpha_generate(phong, dark, color, white, sf);

   for (y = 0; y < h; y++)
     {
        int gx, gy, vx, vy;

        if (!y)
          {
             map_y1 = map;
             map_y2 = map + w;
          }
        else if (y == (h - 1))
          {
             map_y1 = map - w;
             map_y2 = map;
          }
        else
          {
             map_y1 = map - w;
             map_y2 = map + w;
          }

        // x = 0
        gx = (map[1] - map[0]) / 2;
        gy = (*map_y2 - *map_y1) / 2;
        vx = gx + lx + 127;
        vy = (-gy) + ly + 127;

        //printf("dx,dy: %d,%d, lx,ly: %d,%d, vx,vy: %d,%d\n", gx, gy, lx, ly, vx, vy);

        if ((vx & 0xFF00) || (vy & 0xFF00))
          *dst = *src * dark;
        else
          *dst = (*src * phong[(vy << 8) + vx]) >> 8;

        dst++, src++, map_y1++, map_y2++;
        for (x = 1; x < (w - 1); x++, map++, map_y1++, map_y2++, src++, dst++)
          {
             // note: map is one pixel left of (x,y)

             if (!*src)
               {
                  *dst = 0;
                  continue;
               }

             // compute gradient (gx, gy). this gives us the normal vector
             gx = (map[2] - map[0]) / 2;
             gy = (*map_y2 - *map_y1) / 2;

             // compute halfway vector between light and gradient vectors
             vx = gx + lx + 127;
             vy = (-gy) + ly + 127;

             // take light from the phong table
             if ((vx & 0xFF00) || (vy & 0xFF00))
               *dst = *src * dark;
             else
               *dst = (*src * phong[(vy << 8) + vx]) >> 8;
          }

        // x = (w - 1)
        gx = (map[1] - map[0]) / 2;
        gy = (*map_y2 - *map_y1) / 2;
        vx = gx + lx + 127;
        vy = (-gy) + ly + 127;

        if ((vx & 0xFF00) || (vy & 0xFF00))
          *dst = *src * dark;
        else
          *dst = (*src * phong[(vy << 8) + vx]) >> 8;

        map += 2;
        dst++;
        src++;
     }

   ret = EINA_TRUE;

end:
   eo_do(cmd->input->buffer, ector_buffer_unmap(src_map, slen));
   eo_do(cmd->mask->buffer, ector_buffer_unmap(map_map, mlen));
   eo_do(cmd->output->buffer, ector_buffer_unmap(dst_map, dlen));
   free(phong);
   return ret;
}

static Eina_Bool
_bump_map_cpu_alpha_rgba(Evas_Filter_Command *cmd)
{
   uint8_t *src_map, *map_map;
   uint8_t *src, *map, *map_y1, *map_y2;
   uint32_t *dst, *dst_map;
   uint32_t dark, color, white, col;
   Eina_Bool compensate, ret = EINA_FALSE;
   int x, y, w, h, lx, ly, lz, gz, NL, diffusion, gzlz, gz2;
   unsigned int ss, ms, ds, slen, dlen, mlen;
   double xyangle, zangle, sf, lxy, elevation;

   w = cmd->input->w;
   h = cmd->input->h;
   EINA_SAFETY_ON_FALSE_RETURN_VAL(w > 2 && h > 2, EINA_FALSE);

   src_map = src = _buffer_map_all(cmd->input->buffer, &slen, E_READ, E_ALPHA, &ss);
   map_map = map = _buffer_map_all(cmd->mask->buffer, &mlen, E_READ, E_ALPHA, &ms);
   dst_map = dst = (uint32_t *) _buffer_map_all(cmd->output->buffer, &dlen, E_WRITE, E_ARGB, &ds);
   EINA_SAFETY_ON_FALSE_GOTO(src && dst && map, end);

   xyangle = cmd->bump.xyangle;
   zangle = cmd->bump.zangle;
   sf = cmd->bump.specular_factor;
   compensate = cmd->bump.compensate;
   elevation = cmd->bump.elevation;

   dark = cmd->bump.dark;
   white = cmd->bump.white;
   color = cmd->bump.color;

   // Compute appropriate lx, ly
   if (fabs(zangle) >= 90.)
     {
        WRN("Z angle was defined as %.0f, out of range. Defaults to %.0f.",
            zangle, DEFAULT_ZANGLE);
        zangle = DEFAULT_ZANGLE;
     }

   lxy = 255. * cos(zangle * M_PI / 180.);
   lx = (int) (lxy * cos(xyangle * M_PI / 180.));
   ly = (int) (lxy * sin(xyangle * M_PI / 180.));
   lz = (int) (255. * sin(zangle));
   INF("Using light vector (%d,%d,%d)", lx, ly, lz);

   if (elevation <= 0)
     {
        WRN("Invalid elevation value of %.0f, using 10 instead.", elevation);
        elevation = 10.0;
     }

   gz = (6*255) / elevation;
   gzlz = gz * lz;
   gz2 = gz * gz;

   // Generate light table
   // FIXME: phong LUT not used (we need two)
   //phong = malloc(256 * 256 * sizeof(*phong));
   //EINA_SAFETY_ON_NULL_RETURN_VAL(phong, EINA_FALSE);
   //_phong_rgba_generate(phong, 1.5, sf, 20, dark, color, white);

   // FIXME: x=0 and x=w-1 are NOT implemented.

   for (y = 0; y < h; y++)
     {
        int gx, gy;

        if (!y)
          {
             map_y1 = map;
             map_y2 = map + w;
          }
        else if (y == (h - 1))
          {
             map_y1 = map - w;
             map_y2 = map;
          }
        else
          {
             map_y1 = map - w;
             map_y2 = map + w;
          }

        for (x = 0; x < w; x++, dst++, src++, map++, map_y1++, map_y2++)
          {
             if (!*src) continue;

             /* Color intensity is defined as:
              * I = Kd*N.L*Cd + Ks*N.H*Cs
              * Where Ks and Kd are 1 in this implementation
              * And Cs is white, Cd is color
              */

             /* Compute N.L
              * N = (gx,gy,gz)
              * L = (lx,ly,lz)   |L| = 255
              */

             if (EINA_LIKELY(x && (x < (w - 1))))
               {
                  gx = map[-1] + map_y1[-1] + map_y2[-1] - map[1] - map_y1[1] - map_y2[1];
                  gy = map_y2[-1] + map_y2[0] + map_y2[1] - map_y1[-1] - map_y1[0] - map_y1[1];
               }
             else if (!x)
               {
                  gx = map[0] + map_y1[0] + map_y2[0] - map[1] - map_y1[1] - map_y2[1];
                  gy = map_y2[0] + map_y2[1] + map_y2[1] - map_y1[0] - map_y1[1] - map_y1[1];
               }
             else
               {
                  gx = map[-1] + map_y1[-1] + map_y2[-1] - map[0] - map_y1[0] - map_y2[0];
                  gy = map_y2[-1] + map_y2[0] + map_y2[0] - map_y1[-1] - map_y1[0] - map_y1[0];
               }

             NL = gx*lx + gy*ly + gzlz;

             if (0 && NL < 0)
               {
                  // TODO: Check this
                  diffusion = lz;
               }
             else
               {
                  int g2 = gx*gx + gy*gy + gz2;
                  diffusion = NL / sqrt(MAX(g2, 1));
                  //diffusion += MAX(0, lz - diffusion);
               }

             if (compensate)
               diffusion = diffusion * 255 / lz;

             diffusion = CLAMP(1, diffusion + 1, 256);
             col = INTERP_256(diffusion, color, dark);

             if (sf > 0)
               {
                  /* Compute N.H
                   * H = (L+V) / |L+V|
                   * V = (0,0,255)
                   * L = (lx,ly,lz)   |L| = 255
                   */

                  // FIXME: All these doubles :)

                  int shine;
                  const double hnorm = sqrt(lx*lx + ly*ly + (lz+255)*(lz+255));
                  const double hx = (double) lx / hnorm;
                  const double hy = (double) ly / hnorm;
                  const double hz = (double) (lz+255) / hnorm;
                  double NHx = hx*gx / 255.0;
                  double NHy = hy*gy / 255.0;
                  double nz = sqrt(255.0*255.0 - gx*gx - gy*gy);
                  double NHz = (hz*nz) / 255.0;
                  double NH = NHx + NHy + NHz;
                  double specular = NH > 0 ? pow(NH, sf) : 0;

                  if (compensate)
                    {
                       const double basespecular = pow(hz, sf);
                       shine = (specular - basespecular) * 255.0 / (1.0 - basespecular);
                    }
                  else shine = specular * 255.0;

                  col = INTERP_256(CLAMP(1, shine + 1, 256), white, col);
               }

             *dst = INTERP_256(*src + 1, col, *dst);
          }
   }

   ret = EINA_TRUE;

end:
   eo_do(cmd->input->buffer, ector_buffer_unmap(src_map, slen));
   eo_do(cmd->mask->buffer, ector_buffer_unmap(map_map, mlen));
   eo_do(cmd->output->buffer, ector_buffer_unmap(dst_map, dlen));
   return ret;
}