Commit 6323cb4e authored by Michael Natterer's avatar Michael Natterer 😴

operations: make emboss always preserve alpha

also clean up the code and fix misleading use of "x" for the y-axis
and the other way around.
parent f705cb01
...@@ -76,14 +76,14 @@ emboss (gfloat *src_buf, ...@@ -76,14 +76,14 @@ emboss (gfloat *src_buf,
const GeglRectangle *src_rect, const GeglRectangle *src_rect,
gfloat *dst_buf, gfloat *dst_buf,
const GeglRectangle *dst_rect, const GeglRectangle *dst_rect,
gint x, GeglEmbossType type,
gint y,
gint floats_per_pixel, gint floats_per_pixel,
gint alpha,
gdouble azimuth, gdouble azimuth,
gdouble elevation, gdouble elevation,
gint width45) gint width45)
{ {
gint y; gint x;
gint offset, verify; gint offset, verify;
gint bytes; gint bytes;
...@@ -97,12 +97,12 @@ emboss (gfloat *src_buf, ...@@ -97,12 +97,12 @@ emboss (gfloat *src_buf,
Nz2 = Nz * Nz; Nz2 = Nz * Nz;
NzLz = Nz * Lz; NzLz = Nz * Lz;
bytes = (alpha) ? floats_per_pixel - 1 : floats_per_pixel; bytes = floats_per_pixel - 1;
verify = src_rect->width * src_rect->height * floats_per_pixel; verify = src_rect->width * src_rect->height * floats_per_pixel;
offset = x * dst_rect->width * floats_per_pixel; offset = y * dst_rect->width * floats_per_pixel;
for (y = 0; y < dst_rect->width; y++) for (x = 0; x < dst_rect->width; x++)
{ {
gint i, j, b, count; gint i, j, b, count;
gfloat Nx, Ny, NdotL; gfloat Nx, Ny, NdotL;
...@@ -115,56 +115,67 @@ emboss (gfloat *src_buf, ...@@ -115,56 +115,67 @@ emboss (gfloat *src_buf,
M[i][j] = 0.0; M[i][j] = 0.0;
for (b = 0; b < bytes; b++) for (b = 0; b < bytes; b++)
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) for (i = 0; i < 3; i++)
{ {
count = ((x+i-1)*src_rect->width + (y+j-1))*floats_per_pixel + bytes; for (j = 0; j < 3; j++)
{
/*verify each time that we are in the source image*/ count = ((y + i - 1) * src_rect->width + (x + j - 1)) * floats_per_pixel + bytes;
if (alpha && count >= 0 && count < verify)
a = src_buf[count]; /* verify each time that we are in the source image */
else if (count >= 0 && count < verify)
a = 1.0; a = src_buf[count];
else
/*calculate recalculate the sorrounding pixels by multiplication*/ a = 1.0;
/*after we have that we can calculate new value of the pixel*/
if ((count - bytes + b) >= 0 && (count - bytes + b) < verify) /* calculate recalculate the sorrounding pixels by
M[i][j] += a * src_buf[count - bytes + b]; * multiplication after we have that we can
* calculate new value of the pixel
*/
if ((count - bytes + b) >= 0 && (count - bytes + b) < verify)
M[i][j] += a * src_buf[count - bytes + b];
}
} }
}
Nx = M[0][0] + M[1][0] + M[2][0] - M[0][2] - M[1][2] - M[2][2]; Nx = M[0][0] + M[1][0] + M[2][0] - M[0][2] - M[1][2] - M[2][2];
Ny = M[2][0] + M[2][1] + M[2][2] - M[0][0] - M[0][1] - M[0][2]; Ny = M[2][0] + M[2][1] + M[2][2] - M[0][0] - M[0][1] - M[0][2];
/*calculating the shading result (same as in gimp)*/ /* calculating the shading result (same as in gimp) */
if ( Nx == 0 && Ny == 0 ) if (Nx == 0 && Ny == 0)
shade = Lz; shade = Lz;
else if ( (NdotL = Nx * Lx + Ny * Ly + NzLz) < 0 ) else if ((NdotL = Nx * Lx + Ny * Ly + NzLz) < 0)
shade = 0; shade = 0;
else else
shade = NdotL / sqrt(Nx*Nx + Ny*Ny + Nz2); shade = NdotL / sqrt (Nx * Nx + Ny * Ny + Nz2);
count = (x*src_rect->width + y)*floats_per_pixel; count = (y * src_rect->width + x) * floats_per_pixel;
/*setting the value of the destination buffer*/ /* setting the value of the destination buffer */
if (bytes == 1) if (type == GEGL_EMBOSS_TYPE_EMBOSS)
dst_buf[offset++] = shade; {
dst_buf[offset++] = shade;
}
else else
{ {
/*recalculating every byte of a pixel*/ /* recalculating every byte of a pixel by multiplying with
/*by multiplying with the shading result*/ * the shading result
*/
for (b = 0; b < bytes; b++) for (b = 0; b < bytes; b++)
if ((count + b) >= 0 && (count + b) < verify) {
dst_buf[offset++] = (src_buf[count+b] * shade) ; if ((count + b) >= 0 && (count + b) < verify)
else dst_buf[offset++] = (src_buf[count + b] * shade);
dst_buf[offset++] = 1.0; else
dst_buf[offset++] = 1.0;
/*preserving alpha*/ }
if (alpha && (count + bytes) >= 0 && (count + bytes) < verify)
dst_buf[offset++] = src_buf[count + bytes];
else
dst_buf[offset++] = 1.0 ;
} }
/* preserving alpha */
if ((count + bytes) >= 0 && (count + bytes) < verify)
dst_buf[offset++] = src_buf[count + bytes];
else
dst_buf[offset++] = 1.0;
} }
} }
...@@ -181,7 +192,7 @@ prepare (GeglOperation *operation) ...@@ -181,7 +192,7 @@ prepare (GeglOperation *operation)
babl_format ("RGBA float")); babl_format ("RGBA float"));
else else
gegl_operation_set_format (operation, "output", gegl_operation_set_format (operation, "output",
babl_format ("Y float")); babl_format ("YA float"));
} }
static gboolean static gboolean
...@@ -198,8 +209,7 @@ process (GeglOperation *operation, ...@@ -198,8 +209,7 @@ process (GeglOperation *operation,
gfloat *src_buf; gfloat *src_buf;
gfloat *dst_buf; gfloat *dst_buf;
const Babl *format; const Babl *format;
gint alpha; gint y;
gint x;
gint floats_per_pixel; gint floats_per_pixel;
/*blur-map or emboss*/ /*blur-map or emboss*/
...@@ -207,13 +217,11 @@ process (GeglOperation *operation, ...@@ -207,13 +217,11 @@ process (GeglOperation *operation,
{ {
format = babl_format ("RGBA float"); format = babl_format ("RGBA float");
floats_per_pixel = 4; floats_per_pixel = 4;
alpha = 1;
} }
else else
{ {
format = babl_format ("Y float"); format = babl_format ("YA float");
floats_per_pixel = 1; floats_per_pixel = 2;
alpha = 0;
} }
rect.x = result->x - op_area->left; rect.x = result->x - op_area->left;
...@@ -228,8 +236,8 @@ process (GeglOperation *operation, ...@@ -228,8 +236,8 @@ process (GeglOperation *operation,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
/*do for every row*/ /*do for every row*/
for (x = 0; x < rect.height; x++) for (y = 0; y < rect.height; y++)
emboss (src_buf, &rect, dst_buf, &rect, x, floats_per_pixel, alpha, emboss (src_buf, &rect, dst_buf, &rect, o->type, y, floats_per_pixel,
DEG_TO_RAD (o->azimuth), DEG_TO_RAD (o->elevation), o->depth); DEG_TO_RAD (o->azimuth), DEG_TO_RAD (o->elevation), o->depth);
gegl_buffer_set (output, &rect, 0, format, gegl_buffer_set (output, &rect, 0, format,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment