| 1 | #include "frei0r.h" |
| 2 | #include "frei0r/math.h" |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | static uint32_t average(const uint32_t* start, |
| 8 | int bxsize, |
| 9 | int bysize, int xsize); |
| 10 | |
| 11 | static void fill_block(uint32_t* start, |
| 12 | int bxsize, |
| 13 | int bysize, |
| 14 | int xsize, uint32_t col); |
| 15 | |
| 16 | |
| 17 | typedef struct pixelizer_instance |
| 18 | { |
| 19 | unsigned int width; |
| 20 | unsigned int height; |
| 21 | unsigned int block_size_x; |
| 22 | unsigned int block_size_y; |
| 23 | unsigned char pass_alpha; |
| 24 | } pixelizer_instance_t; |
| 25 | |
| 26 | int f0r_init() |
| 27 | { |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | void f0r_deinit() |
| 32 | { /* no initialization required */ } |
| 33 | |
| 34 | void f0r_get_plugin_info(f0r_plugin_info_t* pixelizerInfo) |
| 35 | { |
| 36 | pixelizerInfo->name = "pixeliz0r"; |
| 37 | pixelizerInfo->author = "Gephex crew"; |
| 38 | pixelizerInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 39 | pixelizerInfo->color_model = F0R_COLOR_MODEL_PACKED32; |
| 40 | pixelizerInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 41 | pixelizerInfo->major_version = 2; |
| 42 | pixelizerInfo->minor_version = 0; |
| 43 | pixelizerInfo->num_params = 3; |
| 44 | pixelizerInfo->explanation = "Pixelize input image."; |
| 45 | } |
| 46 | |
| 47 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 48 | { |
| 49 | switch(param_index) |
| 50 | { |
| 51 | case 0: |
| 52 | info->name = "Block width"; |
| 53 | info->type = F0R_PARAM_DOUBLE; |
| 54 | info->explanation = "Horizontal size of one 'pixel'"; |
| 55 | break; |
| 56 | case 1: |
| 57 | info->name = "Block height"; |
| 58 | info->type = F0R_PARAM_DOUBLE; |
| 59 | info->explanation = "Vertical size of one 'pixel'"; |
| 60 | break; |
| 61 | case 2: |
| 62 | info->name = "Pass-through alpha"; |
| 63 | info->type = F0R_PARAM_BOOL; |
| 64 | info->explanation = "Do not average the alpha channel over each block"; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 70 | { |
| 71 | pixelizer_instance_t* inst = (pixelizer_instance_t*)calloc(1, sizeof(*inst)); |
| 72 | inst->width = width; inst->height = height; |
| 73 | inst->block_size_x = 8; inst->block_size_y = 8; |
| 74 | return (f0r_instance_t)inst; |
| 75 | } |
| 76 | |
| 77 | void f0r_destruct(f0r_instance_t instance) |
| 78 | { |
| 79 | free(instance); |
| 80 | } |
| 81 | |
| 82 | void f0r_set_param_value(f0r_instance_t instance, |
| 83 | f0r_param_t param, int param_index) |
| 84 | { |
| 85 | assert(instance); |
| 86 | pixelizer_instance_t* inst = (pixelizer_instance_t*)instance; |
| 87 | |
| 88 | switch(param_index) |
| 89 | { |
| 90 | case 0: |
| 91 | // scale to [1..width] |
| 92 | inst->block_size_x = MAX(1 + ( *((double*)param) * (inst->width/2)), 1); |
| 93 | break; |
| 94 | case 1: |
| 95 | // scale to [1..height] |
| 96 | inst->block_size_y = MAX(1 + ( *((double*)param) * (inst->height/2)), 1); |
| 97 | break; |
| 98 | case 2: |
| 99 | inst->pass_alpha = *((double*)param) >= 0.5; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void f0r_get_param_value(f0r_instance_t instance, |
| 105 | f0r_param_t param, int param_index) |
| 106 | { |
| 107 | assert(instance); |
| 108 | pixelizer_instance_t* inst = (pixelizer_instance_t*)instance; |
| 109 | |
| 110 | switch(param_index) |
| 111 | { |
| 112 | case 0: |
| 113 | // scale back to [0..1] |
| 114 | *((double*)param) = (double)(inst->block_size_x-1)/(inst->width/2); |
| 115 | break; |
| 116 | case 1: |
| 117 | // scale back to [0..1] |
| 118 | *((double*)param) = (double)(inst->block_size_y-1)/(inst->height/2); |
| 119 | break; |
| 120 | case 2: |
| 121 | *((double*)param) = inst->pass_alpha ? 1.0 : 0.0; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void f0r_update(f0r_instance_t instance, double time, |
| 127 | const uint32_t* inframe, uint32_t* outframe) |
| 128 | { |
| 129 | assert(instance); |
| 130 | pixelizer_instance_t* inst = (pixelizer_instance_t*)instance; |
| 131 | unsigned int xsize = inst->width; |
| 132 | unsigned int ysize = inst->height; |
| 133 | unsigned int bsizex = inst->block_size_x; |
| 134 | unsigned int bsizey = inst->block_size_y; |
| 135 | unsigned int offset = 0; |
| 136 | unsigned int blocks_x, blocks_y, xrest, yrest, xi, yi; |
| 137 | |
| 138 | blocks_x = xsize / bsizex; |
| 139 | blocks_y = ysize / bsizey; |
| 140 | xrest = xsize - blocks_x*bsizex; |
| 141 | yrest = ysize - blocks_y*bsizey; |
| 142 | |
| 143 | uint32_t* dst = outframe; |
| 144 | const uint32_t* src = inframe; |
| 145 | |
| 146 | |
| 147 | if (bsizex == 1 && bsizey == 1) |
| 148 | { |
| 149 | memcpy(dst, src, xsize*ysize*sizeof(uint32_t)); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | // now get average for every block and write the average color to the output |
| 154 | for (yi = 0; yi < blocks_y; ++yi) |
| 155 | { |
| 156 | offset = yi*bsizey*xsize; |
| 157 | for (xi = 0; xi < blocks_x; ++xi) |
| 158 | { |
| 159 | uint32_t col = average(src + offset, bsizex, bsizey, xsize); |
| 160 | fill_block(dst + offset, bsizex, bsizey, xsize, col); |
| 161 | offset += bsizex; |
| 162 | } |
| 163 | if (xrest > 0) |
| 164 | { |
| 165 | uint32_t col = average(src + offset, xrest, bsizey, xsize); |
| 166 | fill_block(dst + offset, xrest, bsizey, xsize, col); |
| 167 | } |
| 168 | } |
| 169 | // check for last line |
| 170 | if (yrest > 0) |
| 171 | { |
| 172 | offset = blocks_y*bsizey*xsize; |
| 173 | for (xi = 0; xi < blocks_x; ++xi) |
| 174 | { |
| 175 | uint32_t col = average(src + offset, bsizex, yrest, xsize); |
| 176 | fill_block(dst + offset, bsizex, yrest, xsize, col); |
| 177 | offset += bsizex; |
| 178 | } |
| 179 | if (xrest > 0) |
| 180 | { |
| 181 | uint32_t col = average(src + offset, xrest, yrest, xsize); |
| 182 | fill_block(dst + offset, xrest, yrest, xsize, col); |
| 183 | } |
| 184 | } |
| 185 | if (inst->pass_alpha) |
| 186 | { |
| 187 | for (xi = 0; xi < inst->width * inst->height; ++xi) |
| 188 | { |
| 189 | dst[xi] = (src[xi] & 0xff000000) | (dst[xi] & 0x00ffffff); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static uint32_t average(const uint32_t* start, |
| 196 | int bxsize, int bysize, int xsize) |
| 197 | { |
| 198 | const uint32_t* p = start; |
| 199 | uint32_t alpha = 0, red = 0, green = 0, blue = 0; |
| 200 | uint32_t avg_alpha, avg_red, avg_green, avg_blue; |
| 201 | int x, y; |
| 202 | const uint32_t* pp; |
| 203 | uint32_t c; |
| 204 | |
| 205 | for (y = 0; y < bysize; ++y) |
| 206 | { |
| 207 | pp = p; |
| 208 | for (x = 0; x < bxsize; ++x) |
| 209 | { |
| 210 | c = *(pp++); |
| 211 | alpha += (c & 0xff000000) >> 24; |
| 212 | red += (c & 0x00ff0000) >> 16; |
| 213 | green += (c & 0x0000ff00) >> 8; |
| 214 | blue += (c & 0x000000ff); |
| 215 | } |
| 216 | p += xsize; |
| 217 | } |
| 218 | |
| 219 | avg_alpha = (alpha / (bxsize*bysize)) & 0xff; |
| 220 | avg_red = (red / (bxsize*bysize)) & 0xff; |
| 221 | avg_green = (green / (bxsize*bysize)) & 0xff; |
| 222 | avg_blue = (blue / (bxsize*bysize)) & 0xff; |
| 223 | |
| 224 | return (avg_alpha << 24) + (avg_red << 16) + (avg_green << 8) + avg_blue; |
| 225 | } |
| 226 | |
| 227 | static void fill_block(uint32_t* start, int bxsize, int bysize, |
| 228 | int xsize, uint32_t col) |
| 229 | { |
| 230 | uint32_t* p = start; |
| 231 | int x, y; |
| 232 | uint32_t* pp; |
| 233 | |
| 234 | for (y = 0; y < bysize; ++y) |
| 235 | { |
| 236 | pp = p; |
| 237 | for (x = 0; x < bxsize; ++x) |
| 238 | { |
| 239 | *(pp++) = col; |
| 240 | } |
| 241 | p += xsize; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 |