]> git.sesse.net Git - mlt/blob - src/modules/plus/filter_rgblut.c
Use filter properties instead of frame properties
[mlt] / src / modules / plus / filter_rgblut.c
1 /*
2  * filter_rgblut.c -- generic RGB look-up table filter with string interface
3  * Copyright (C) 2014 Janne Liljeblad
4  * Author: Janne Liljeblad
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_tokeniser.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29
30 /** Fill channel lut with integers parsed from property string.
31 */
32 static void fill_channel_lut(int lut[], char* channel_table_str)
33 {
34         mlt_tokeniser tokeniser = mlt_tokeniser_init();
35         mlt_tokeniser_parse_new( tokeniser, channel_table_str, ";" );
36         
37         // Only create lut from string if tokens count exactly right 
38         if ( tokeniser->count == 256 )
39         {
40                 // Fill lut with token values
41                 int i;
42                 int val;
43                 for( i = 0; i < 256; i++ )
44                 {
45                         val = atoi(tokeniser->tokens[i]);
46                         lut[i] = val;
47                 }
48         }
49         else
50         {
51                 // Fill lut with linear no-op table
52                 int i;
53                 for( i = 0; i < 256; i++ )
54                 {
55                         lut[i] = i;
56                 }
57         }
58         mlt_tokeniser_close( tokeniser );
59 }
60
61 /** Do it :-).
62 */
63 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
64 {
65         // Get the image
66         mlt_filter filter = mlt_frame_pop_service( frame );
67
68         *format = mlt_image_rgb24;
69         int error = mlt_frame_get_image( frame, image, format, width, height, 0 );
70
71         // Only process if we have no error and a valid colour space
72         if ( error == 0 )
73         {
74
75                 // Create lut tables from properties for each RGB channel
76                 char* r_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "R_table" );
77                 int r_lut[256];
78                 fill_channel_lut( r_lut, r_str );
79
80                 char* g_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "G_table" );
81                 int g_lut[256];
82                 fill_channel_lut( g_lut, g_str );
83
84                 char* b_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "B_table" );
85                 int b_lut[256];
86                 fill_channel_lut( b_lut, b_str );
87
88                 // Apply look-up tables into image
89                 int i = *width * *height + 1;
90                 uint8_t *p = *image;
91                 uint8_t *r = *image;
92                 while ( --i )
93                 {
94                         *p ++ = r_lut[ *r ++ ];
95                         *p ++ = g_lut[ *r ++ ];
96                         *p ++ = b_lut[ *r ++ ];
97                 }
98         }
99
100         return error;
101 }
102
103 /** Filter processing.
104 */
105
106 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
107 {
108         // Push the frame filter
109         mlt_frame_push_service( frame, filter );
110         mlt_frame_push_get_image( frame, filter_get_image );
111         return frame;
112 }
113
114 /** Constructor for the filter.
115 */
116
117 mlt_filter filter_rgblut_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
118 {
119         mlt_filter filter = mlt_filter_new( );
120         if ( filter != NULL )
121         {
122                 filter->process = filter_process;
123                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "R_table", "unset" );
124                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "G_table", "unset" );
125                 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "B_table", "unset" );
126         }
127         return filter;
128 }
129