2 * filter_boxblur.c -- blur filter
3 * Copyright (C) ?-2007 Leny Grisel <leny.grisel@laposte.net>
4 * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@ader.ch>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
29 static void PreCompute(uint8_t *image, int32_t *rgb, int width, int height)
34 for (y = 0; y < height; y++)
36 for (x = 0; x < width; x++)
41 for (z = 0; z < 3; z++)
43 if (x > 0) pts[z] += rgb[-3];
44 if (y > 0) pts[z] += rgb[width * -3];
45 if (x>0 && y>0) pts[z] -= rgb[(width + 1) * -3];
53 static int32_t GetRGB(int32_t *rgb, unsigned int w, unsigned int h, unsigned int x, int offsetx, unsigned int y, int offsety, unsigned int z)
55 int xtheo = x * 1 + offsetx;
56 int ytheo = y + offsety;
57 if (xtheo < 0) xtheo = 0; else if (xtheo >= w) xtheo = w - 1;
58 if (ytheo < 0) ytheo = 0; else if (ytheo >= h) ytheo = h - 1;
59 return rgb[3*(xtheo+ytheo*w)+z];
62 static void DoBoxBlur(uint8_t *image, int32_t *rgb, unsigned int width, unsigned int height, unsigned int boxw, unsigned int boxh)
65 float mul = 1.f / ((boxw*2) * (boxh*2));
67 for (y = 0; y < height; y++)
69 for (x = 0; x < width; x++)
71 *image++ = (GetRGB(rgb, width, height, x, +boxw, y, +boxh, 0)
72 + GetRGB(rgb, width, height, x, -boxw, y, -boxh, 0)
73 - GetRGB(rgb, width, height, x, -boxw, y, +boxh, 0)
74 - GetRGB(rgb, width, height, x, +boxw, y, -boxh, 0)) * mul;
75 *image++ = (GetRGB(rgb, width, height, x, +boxw, y, +boxh, 1)
76 + GetRGB(rgb, width, height, x, -boxw, y, -boxh, 1)
77 - GetRGB(rgb, width, height, x, -boxw, y, +boxh, 1)
78 - GetRGB(rgb, width, height, x, +boxw, y, -boxh, 1)) * mul;
79 *image++ = (GetRGB(rgb, width, height, x, +boxw, y, +boxh, 2)
80 + GetRGB(rgb, width, height, x, -boxw, y, -boxh, 2)
81 - GetRGB(rgb, width, height, x, -boxw, y, +boxh, 2)
82 - GetRGB(rgb, width, height, x, +boxw, y, -boxh, 2)) * mul;
87 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
90 *format = mlt_image_rgb24;
91 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
92 short hori = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "hori" );
93 short vert = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "vert" );
95 // Only process if we have no error and a valid colour space
98 double factor = mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "boxblur" );
102 int32_t *rgb = mlt_pool_alloc( 3 * *width * h * sizeof(int32_t) );
103 PreCompute( *image, rgb, *width, h );
104 DoBoxBlur( *image, rgb, *width, h, (int) factor * hori, (int) factor * vert );
105 mlt_pool_release( rgb );
111 /** Filter processing.
114 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
116 // Get the starting blur level
117 double blur = (double) mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "start" );
118 short hori = mlt_properties_get_int(MLT_FILTER_PROPERTIES( this ), "hori" );
119 short vert = mlt_properties_get_int(MLT_FILTER_PROPERTIES( this ), "vert" );
121 // If there is an end adjust gain to the range
122 if ( mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "end" ) != NULL )
124 // Determine the time position of this frame in the transition duration
125 double end = (double) mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "end" );
126 blur += ( end - blur ) * mlt_filter_get_progress( this, frame );
129 // Push the frame filter
130 mlt_properties_set_double( MLT_FRAME_PROPERTIES( frame ), "boxblur", blur );
131 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "hori", hori );
132 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "vert", vert );
133 mlt_frame_push_get_image( frame, filter_get_image );
138 /** Constructor for the filter.
141 mlt_filter filter_boxblur_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
143 mlt_filter this = mlt_filter_new( );
146 this->process = filter_process;
147 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "start", arg == NULL ? "2" : arg);
148 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "hori", arg == NULL ? "1" : arg);
149 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "vert", arg == NULL ? "1" : arg);