| 1 | /* rgbparade.c |
| 2 | * Copyright (C) 2008 Albert Frisch ([email protected]) |
| 3 | * This file is a Frei0r plugin. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <math.h> |
| 23 | #include <assert.h> |
| 24 | #include "frei0r.h" |
| 25 | |
| 26 | #include <gavl/gavl.h> |
| 27 | |
| 28 | #include "rgbparade_image.h" |
| 29 | |
| 30 | #define OFFSET_R 0 |
| 31 | #define OFFSET_G 8 |
| 32 | #define OFFSET_B 16 |
| 33 | #define OFFSET_A 24 |
| 34 | |
| 35 | #define PARADE_HEIGHT 256 |
| 36 | #define PARADE_STEP 5 |
| 37 | |
| 38 | typedef struct { |
| 39 | double red, green, blue; |
| 40 | } rgb_t; |
| 41 | |
| 42 | typedef struct rgbparade { |
| 43 | int w, h; |
| 44 | unsigned char* scala; |
| 45 | gavl_video_scaler_t* parade_scaler; |
| 46 | gavl_video_frame_t* parade_frame_src; |
| 47 | gavl_video_frame_t* parade_frame_dst; |
| 48 | double mix; |
| 49 | double overlay_sides; |
| 50 | } rgbparade_t; |
| 51 | |
| 52 | int f0r_init() |
| 53 | { |
| 54 | return 1; |
| 55 | } |
| 56 | void f0r_deinit() |
| 57 | { /* empty */ } |
| 58 | |
| 59 | void f0r_get_plugin_info( f0r_plugin_info_t* info ) |
| 60 | { |
| 61 | info->name = "RGB-Parade"; |
| 62 | info->author = "Albert Frisch"; |
| 63 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 64 | info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 65 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 66 | info->major_version = 0; |
| 67 | info->minor_version = 2; |
| 68 | info->num_params = 2; |
| 69 | info->explanation = "Displays a histogram of R, G and B of the video-data"; |
| 70 | } |
| 71 | |
| 72 | void f0r_get_param_info( f0r_param_info_t* info, int param_index ) |
| 73 | { |
| 74 | switch(param_index) |
| 75 | { |
| 76 | case 0: |
| 77 | info->name = "mix"; |
| 78 | info->type = F0R_PARAM_DOUBLE; |
| 79 | info->explanation = "The amount of source image mixed into background of display"; |
| 80 | break; |
| 81 | case 1: |
| 82 | info->name = "overlay sides"; |
| 83 | info->type = F0R_PARAM_BOOL; |
| 84 | info->explanation = "If false, the sides of image are shown without overlay"; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 90 | { |
| 91 | rgbparade_t* inst = (rgbparade_t*)calloc(1, sizeof(*inst)); |
| 92 | inst->w = width; |
| 93 | inst->h = height; |
| 94 | |
| 95 | inst->mix = 0.0; |
| 96 | inst->overlay_sides = 1.0; |
| 97 | |
| 98 | inst->scala = (unsigned char*)malloc( width * height * 4 ); |
| 99 | |
| 100 | gavl_video_scaler_t* video_scaler; |
| 101 | gavl_video_frame_t* frame_src; |
| 102 | gavl_video_frame_t* frame_dst; |
| 103 | |
| 104 | video_scaler = gavl_video_scaler_create(); |
| 105 | frame_src = gavl_video_frame_create( 0 ); |
| 106 | frame_dst = gavl_video_frame_create( 0 ); |
| 107 | frame_dst->strides[0] = width * 4; |
| 108 | frame_src->strides[0] = rgbparade_image.width * 4; |
| 109 | |
| 110 | gavl_video_options_t* options = gavl_video_scaler_get_options( video_scaler ); |
| 111 | gavl_video_format_t format_src; |
| 112 | gavl_video_format_t format_dst; |
| 113 | |
| 114 | memset(&format_src, 0, sizeof(format_src)); |
| 115 | memset(&format_dst, 0, sizeof(format_dst)); |
| 116 | |
| 117 | format_dst.frame_width = inst->w; |
| 118 | format_dst.frame_height = inst->h; |
| 119 | format_dst.image_width = inst->w; |
| 120 | format_dst.image_height = inst->h; |
| 121 | format_dst.pixel_width = 1; |
| 122 | format_dst.pixel_height = 1; |
| 123 | format_dst.pixelformat = GAVL_RGBA_32; |
| 124 | format_dst.interlace_mode = GAVL_INTERLACE_NONE; |
| 125 | |
| 126 | format_src.frame_width = rgbparade_image.width; |
| 127 | format_src.frame_height = rgbparade_image.height; |
| 128 | format_src.image_width = rgbparade_image.width; |
| 129 | format_src.image_height = rgbparade_image.height; |
| 130 | format_src.pixel_width = 1; |
| 131 | format_src.pixel_height = 1; |
| 132 | format_src.pixelformat = GAVL_RGBA_32; |
| 133 | format_src.interlace_mode = GAVL_INTERLACE_NONE; |
| 134 | |
| 135 | gavl_rectangle_f_t src_rect; |
| 136 | gavl_rectangle_i_t dst_rect; |
| 137 | |
| 138 | src_rect.x = 0; |
| 139 | src_rect.y = 0; |
| 140 | src_rect.w = rgbparade_image.width; |
| 141 | src_rect.h = rgbparade_image.height; |
| 142 | dst_rect.x = 0; |
| 143 | dst_rect.y = 0; |
| 144 | dst_rect.w = width; |
| 145 | dst_rect.h = height * 0.995; |
| 146 | |
| 147 | gavl_video_options_set_rectangles( options, &src_rect, &dst_rect ); |
| 148 | gavl_video_scaler_init( video_scaler, &format_src, &format_dst ); |
| 149 | |
| 150 | frame_src->planes[0] = (uint8_t *)rgbparade_image.pixel_data; |
| 151 | frame_dst->planes[0] = (uint8_t *)inst->scala; |
| 152 | |
| 153 | /* Pad the source image to make the stride a multiple of 16. */ |
| 154 | gavl_video_frame_t* padded = gavl_video_frame_create( &format_src ); |
| 155 | gavl_video_frame_copy( &format_src, padded, frame_src ); |
| 156 | |
| 157 | float transparent[4] = { 0.0, 0.0, 0.0, 0.0 }; |
| 158 | gavl_video_frame_fill( frame_dst, &format_dst, transparent ); |
| 159 | |
| 160 | gavl_video_scaler_scale( video_scaler, padded, frame_dst ); |
| 161 | |
| 162 | gavl_video_scaler_destroy(video_scaler); |
| 163 | gavl_video_frame_null( frame_src ); |
| 164 | gavl_video_frame_destroy( frame_src ); |
| 165 | gavl_video_frame_null( frame_dst ); |
| 166 | gavl_video_frame_destroy( frame_dst ); |
| 167 | gavl_video_frame_destroy( padded ); |
| 168 | |
| 169 | options = gavl_video_scaler_get_options( inst->parade_scaler ); |
| 170 | |
| 171 | inst->parade_scaler = gavl_video_scaler_create(); |
| 172 | inst->parade_frame_src = gavl_video_frame_create(0); |
| 173 | inst->parade_frame_dst = gavl_video_frame_create(0); |
| 174 | inst->parade_frame_src->strides[0] = width * 4; |
| 175 | inst->parade_frame_dst->strides[0] = width * 4; |
| 176 | options = gavl_video_scaler_get_options( inst->parade_scaler ); |
| 177 | |
| 178 | format_src.frame_width = width; |
| 179 | format_src.frame_height = PARADE_HEIGHT; |
| 180 | format_src.image_width = width; |
| 181 | format_src.image_height = PARADE_HEIGHT; |
| 182 | format_src.pixel_width = 1; |
| 183 | format_src.pixel_height = 1; |
| 184 | format_src.pixelformat = GAVL_RGBA_32; |
| 185 | format_dst.frame_width = width; |
| 186 | format_dst.frame_height = height; |
| 187 | format_dst.image_width = width; |
| 188 | format_dst.image_height = height; |
| 189 | format_dst.pixel_width = 1; |
| 190 | format_dst.pixel_height = 1; |
| 191 | format_dst.pixelformat = GAVL_RGBA_32; |
| 192 | |
| 193 | gavl_rectangle_f_set_all( &src_rect, &format_src ); |
| 194 | dst_rect.x = width * 0.05; |
| 195 | dst_rect.y = height * 0.011; |
| 196 | dst_rect.w = width * 0.9; |
| 197 | dst_rect.h = height * 0.978; |
| 198 | |
| 199 | gavl_video_options_set_rectangles( options, &src_rect, &dst_rect ); |
| 200 | gavl_video_scaler_init( inst->parade_scaler, &format_src, &format_dst ); |
| 201 | |
| 202 | return (f0r_instance_t)inst; |
| 203 | } |
| 204 | |
| 205 | void f0r_destruct(f0r_instance_t instance) |
| 206 | { |
| 207 | rgbparade_t* inst = (rgbparade_t*)instance; |
| 208 | gavl_video_scaler_destroy( inst->parade_scaler ); |
| 209 | gavl_video_frame_null( inst->parade_frame_src ); |
| 210 | gavl_video_frame_destroy( inst->parade_frame_src ); |
| 211 | gavl_video_frame_null( inst->parade_frame_dst ); |
| 212 | gavl_video_frame_destroy( inst->parade_frame_dst ); |
| 213 | free(inst); |
| 214 | } |
| 215 | |
| 216 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 217 | { |
| 218 | assert(instance); |
| 219 | rgbparade_t* inst = (rgbparade_t*)instance; |
| 220 | |
| 221 | switch(param_index) |
| 222 | { |
| 223 | case 0: |
| 224 | *((double *)param) = inst->mix; |
| 225 | break; |
| 226 | case 1: |
| 227 | *((double *)param) = inst->overlay_sides; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 233 | { |
| 234 | assert(instance); |
| 235 | rgbparade_t* inst = (rgbparade_t*)instance; |
| 236 | |
| 237 | switch(param_index) |
| 238 | { |
| 239 | case 0: |
| 240 | inst->mix = *((double *)param); |
| 241 | break; |
| 242 | case 1: |
| 243 | inst->overlay_sides = *((double *)param); |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void draw_grid(unsigned char* scope, double width, double height) |
| 249 | { |
| 250 | double i, j; |
| 251 | long offset; |
| 252 | |
| 253 | for(j=0;j<6;j++) |
| 254 | { |
| 255 | for(i=0;i<width;++i) |
| 256 | { |
| 257 | offset = j*(height-1)*width/5+i; |
| 258 | scope[offset] = 255; |
| 259 | } |
| 260 | } |
| 261 | for(j=0;j<2;j++) |
| 262 | { |
| 263 | for(i=0;i<height;i++) |
| 264 | { |
| 265 | offset = i*width+j*(width-1); |
| 266 | scope[offset] = 255; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe) |
| 272 | { |
| 273 | assert(instance); |
| 274 | rgbparade_t* inst = (rgbparade_t*)instance; |
| 275 | |
| 276 | int width = inst->w; |
| 277 | int height = inst->h; |
| 278 | double mix = inst->mix; |
| 279 | int len = inst->w * inst->h; |
| 280 | int parade_len = width * PARADE_HEIGHT; |
| 281 | |
| 282 | uint32_t* dst = outframe; |
| 283 | uint32_t* dst_end; |
| 284 | const uint32_t* src = inframe; |
| 285 | const uint32_t* src_end; |
| 286 | uint32_t* parade = (uint32_t*)malloc( parade_len * 4 ); |
| 287 | uint32_t* parade_end; |
| 288 | |
| 289 | long src_x, src_y; |
| 290 | long x, y; |
| 291 | rgb_t rgb; |
| 292 | uint8_t* pixel; |
| 293 | |
| 294 | dst_end = dst + len; |
| 295 | src_end = src + len; |
| 296 | parade_end = parade + parade_len; |
| 297 | |
| 298 | if ( inst->overlay_sides > 0.5) { |
| 299 | while ( dst < dst_end ) { |
| 300 | *(dst++) = 0xFF000000; |
| 301 | } |
| 302 | } else { |
| 303 | while ( dst < dst_end ) { |
| 304 | *(dst++) = *(src++); |
| 305 | } |
| 306 | src -= len; |
| 307 | } |
| 308 | |
| 309 | dst = outframe; |
| 310 | while ( parade < parade_end ) { |
| 311 | *(parade++) = 0xFF000000; |
| 312 | } |
| 313 | parade -= parade_len; |
| 314 | |
| 315 | for ( src_y = 0; src_y < height; src_y++ ) { |
| 316 | for ( src_x = 0; src_x < width; src_x++ ) { |
| 317 | rgb.red = (((*src) & 0x000000FF) >> OFFSET_R); |
| 318 | rgb.green = (((*src) & 0x0000FF00) >> OFFSET_G); |
| 319 | rgb.blue = (((*src) & 0x00FF0000) >> OFFSET_B); |
| 320 | src++; |
| 321 | x = src_x / 3; |
| 322 | y = PARADE_HEIGHT - rgb.red - 1; |
| 323 | if ( x >= 0 && x < width && y >= 0 && y < PARADE_HEIGHT ) { |
| 324 | pixel = (uint8_t*)¶de[x+width*y]; |
| 325 | if ( pixel[0] < (255-PARADE_STEP) ) pixel[0] += PARADE_STEP; |
| 326 | } |
| 327 | x += width / 3; |
| 328 | y = PARADE_HEIGHT - rgb.green - 1; |
| 329 | if ( x >= 0 && x < width && y >= 0 && y < PARADE_HEIGHT ) { |
| 330 | pixel = (uint8_t*)¶de[x+width*y]; |
| 331 | if ( pixel[1] < (255-PARADE_STEP) ) pixel[1] += PARADE_STEP; |
| 332 | } |
| 333 | x += width / 3; |
| 334 | y = PARADE_HEIGHT - rgb.blue - 1; |
| 335 | if ( x >= 0 && x < width && y >= 0 && y < PARADE_HEIGHT ) { |
| 336 | pixel = (uint8_t*)¶de[x+width*y]; |
| 337 | if ( pixel[2] < (255-PARADE_STEP) ) pixel[2] += PARADE_STEP; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | inst->parade_frame_src->planes[0] = (uint8_t *)parade; |
| 343 | inst->parade_frame_dst->planes[0] = (uint8_t *)dst; |
| 344 | |
| 345 | gavl_video_scaler_scale( inst->parade_scaler, inst->parade_frame_src, inst->parade_frame_dst ); |
| 346 | |
| 347 | unsigned char *scala8, *dst8, *dst8_end, *src8; |
| 348 | |
| 349 | scala8 = inst->scala; |
| 350 | src8 = (unsigned char*)inframe; |
| 351 | dst8 = (unsigned char*)outframe; |
| 352 | dst8_end = dst8 + ( len * 4 ); |
| 353 | if (mix > 0.001 ) { // to not lose performance for non-mixing users |
| 354 | while ( dst8 < dst8_end ) { |
| 355 | dst8[0] = ( ( ( scala8[0] - dst8[0] ) * 255 * scala8[3] ) >> 16 ) + dst8[0]; |
| 356 | dst8[1] = ( ( ( scala8[1] - dst8[1] ) * 255 * scala8[3] ) >> 16 ) + dst8[1]; |
| 357 | dst8[2] = ( ( ( scala8[2] - dst8[2] ) * 255 * scala8[3] ) >> 16 ) + dst8[2]; |
| 358 | if (dst8[0] == 0 && dst8[1] == 0 && dst8[2] == 0){ |
| 359 | dst8[0] = src8[0] * mix; |
| 360 | dst8[1] = src8[1] * mix; |
| 361 | dst8[2] = src8[2] * mix; |
| 362 | } |
| 363 | scala8 += 4; |
| 364 | dst8 += 4; |
| 365 | src8 += 4; |
| 366 | } |
| 367 | } else { |
| 368 | while ( dst8 < dst8_end ) { |
| 369 | dst8[0] = ( ( ( scala8[0] - dst8[0] ) * 255 * scala8[3] ) >> 16 ) + dst8[0]; |
| 370 | dst8[1] = ( ( ( scala8[1] - dst8[1] ) * 255 * scala8[3] ) >> 16 ) + dst8[1]; |
| 371 | dst8[2] = ( ( ( scala8[2] - dst8[2] ) * 255 * scala8[3] ) >> 16 ) + dst8[2]; |
| 372 | scala8 += 4; |
| 373 | dst8 += 4; |
| 374 | } |
| 375 | } |
| 376 | free(parade); |
| 377 | } |
| 378 |