| 1 | /* |
| 2 | * cairoimagegrid.c |
| 3 | * Copyright 2012 Janne Liljeblad |
| 4 | * |
| 5 | * This file is a Frei0r plugin. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <assert.h> |
| 24 | #include <cairo.h> |
| 25 | |
| 26 | #include "frei0r.h" |
| 27 | #include "frei0r/cairo.h" |
| 28 | |
| 29 | #define MAX_ROWS 20 |
| 30 | #define MAX_COLUMNS 20 |
| 31 | |
| 32 | typedef struct cairo_imagegrid_instance |
| 33 | { |
| 34 | unsigned int width; |
| 35 | unsigned int height; |
| 36 | double rows; |
| 37 | double columns; |
| 38 | } cairo_imagegrid_instance_t; |
| 39 | |
| 40 | int f0r_init() |
| 41 | { |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | void f0r_deinit() |
| 46 | { |
| 47 | |
| 48 | } |
| 49 | |
| 50 | void f0r_get_plugin_info(f0r_plugin_info_t* cairo_gradient_info) |
| 51 | { |
| 52 | cairo_gradient_info->name = "cairoimagegrid"; |
| 53 | cairo_gradient_info->author = "Janne Liljeblad"; |
| 54 | cairo_gradient_info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 55 | cairo_gradient_info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 56 | cairo_gradient_info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 57 | cairo_gradient_info->major_version = 0; |
| 58 | cairo_gradient_info->minor_version = 9; |
| 59 | cairo_gradient_info->num_params = 2; |
| 60 | cairo_gradient_info->explanation = "Draws a grid of input images."; |
| 61 | } |
| 62 | |
| 63 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 64 | { |
| 65 | switch(param_index) { |
| 66 | case 0: |
| 67 | info->name = "rows"; |
| 68 | info->type = F0R_PARAM_DOUBLE; |
| 69 | info->explanation = "Number of rows in the image grid. Input range 0 - 1 is interpreted as range 1 - 20"; |
| 70 | break; |
| 71 | case 1: |
| 72 | info->name = "columns"; |
| 73 | info->type = F0R_PARAM_DOUBLE; |
| 74 | info->explanation = "Number of columns in the image grid. Input range 0 - 1 is interpreted as range 1 - 20"; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 80 | { |
| 81 | cairo_imagegrid_instance_t* inst = (cairo_imagegrid_instance_t*)calloc(1, sizeof(*inst)); |
| 82 | inst->width = width; |
| 83 | inst->height = height; |
| 84 | inst->rows = 2.0 / (MAX_ROWS - 1); |
| 85 | inst->columns = 2.0 / (MAX_COLUMNS - 1); |
| 86 | return (f0r_instance_t)inst; |
| 87 | } |
| 88 | |
| 89 | void f0r_destruct(f0r_instance_t instance) |
| 90 | { |
| 91 | free(instance); |
| 92 | } |
| 93 | |
| 94 | void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 95 | { |
| 96 | assert(instance); |
| 97 | cairo_imagegrid_instance_t* inst = (cairo_imagegrid_instance_t*)instance; |
| 98 | |
| 99 | switch(param_index) { |
| 100 | case 0: |
| 101 | inst->rows = *((double*)param); |
| 102 | break; |
| 103 | case 1: |
| 104 | inst->columns = *((double*)param); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index) |
| 110 | { |
| 111 | assert(instance); |
| 112 | cairo_imagegrid_instance_t* inst = (cairo_imagegrid_instance_t*)instance; |
| 113 | |
| 114 | switch(param_index) { |
| 115 | case 0: |
| 116 | *((double*)param) = inst->rows; |
| 117 | break; |
| 118 | case 1: |
| 119 | *((double*)param) = inst->columns; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void draw_grid(cairo_imagegrid_instance_t* inst, unsigned char* dst, const unsigned char* src) |
| 125 | { |
| 126 | int x, y; |
| 127 | int w = inst->width; |
| 128 | int h = inst->height; |
| 129 | int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, w); |
| 130 | |
| 131 | cairo_surface_t* dest_image = cairo_image_surface_create_for_data ((unsigned char*)dst, |
| 132 | CAIRO_FORMAT_ARGB32, |
| 133 | w, |
| 134 | h, |
| 135 | stride); |
| 136 | cairo_t *cr = cairo_create (dest_image); |
| 137 | |
| 138 | cairo_surface_t *image = cairo_image_surface_create_for_data ((unsigned char*)src, |
| 139 | CAIRO_FORMAT_ARGB32, |
| 140 | w, |
| 141 | h, |
| 142 | stride); |
| 143 | |
| 144 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface (image); |
| 145 | |
| 146 | double rows = 1 + (MAX_ROWS - 1) * inst->rows; |
| 147 | double columns = 1 + (MAX_ROWS - 1) * inst->columns; |
| 148 | int pw = (int)(w/columns); |
| 149 | int ph = (int)(h/rows); |
| 150 | |
| 151 | if (w % pw != 0 || h % ph != 0) { |
| 152 | // Destination will have gaps - prefill with black. |
| 153 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); |
| 154 | cairo_rectangle (cr, 0, 0, w, h); |
| 155 | cairo_fill (cr); |
| 156 | } |
| 157 | |
| 158 | cairo_matrix_t matrix; |
| 159 | cairo_matrix_init_scale (&matrix, columns, rows); |
| 160 | cairo_pattern_set_matrix (pattern, &matrix); |
| 161 | |
| 162 | cairo_set_source (cr, pattern); |
| 163 | |
| 164 | cairo_rectangle (cr, 0, 0, pw, ph); |
| 165 | cairo_fill (cr); |
| 166 | |
| 167 | uint32_t *dst32 = (uint32_t *) dst; |
| 168 | |
| 169 | for (y = 0; y < h; y++) { |
| 170 | for (x = 0; x < w; x++) { |
| 171 | dst32[y*w+x] = dst32[(y % ph) * w + (x % pw)]; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | cairo_pattern_destroy (pattern); |
| 176 | cairo_surface_destroy (image); |
| 177 | cairo_surface_destroy (dest_image); |
| 178 | cairo_destroy (cr); |
| 179 | } |
| 180 | |
| 181 | void f0r_update(f0r_instance_t instance, double time, |
| 182 | const uint32_t* inframe, uint32_t* outframe) |
| 183 | { |
| 184 | assert(instance); |
| 185 | cairo_imagegrid_instance_t* inst = (cairo_imagegrid_instance_t*) instance; |
| 186 | const unsigned char* src = (unsigned char*)inframe; |
| 187 | unsigned char* dst = (unsigned char*)outframe; |
| 188 | |
| 189 | draw_grid(inst, dst, src); |
| 190 | } |
| 191 |