| 1 | /* rgbsplit0r.c |
| 2 | * Copyright (C) 2016 IDENT Software ~ http://identsoft.org |
| 3 | * Inspired by the witch house and web culture |
| 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 | |
| 25 | #include "frei0r.h" |
| 26 | |
| 27 | typedef struct rgbsplit0r_instance |
| 28 | { |
| 29 | unsigned int width; |
| 30 | unsigned int height; |
| 31 | unsigned int shiftX; |
| 32 | unsigned int shiftY; |
| 33 | |
| 34 | } rgbsplit0r_instance_t; |
| 35 | |
| 36 | |
| 37 | inline static void rgbsplit0r_extract_color(uint32_t *pixelIn, uint32_t *pixelOut, |
| 38 | short int colorIndex) |
| 39 | { |
| 40 | uint8_t *pxIn = (uint8_t *)pixelIn, |
| 41 | *pxOut = (uint8_t *)pixelOut; |
| 42 | |
| 43 | switch (colorIndex) |
| 44 | { |
| 45 | case 0 : |
| 46 | pxOut[0] = pxIn[0]; |
| 47 | pxOut[1] = 0; |
| 48 | pxOut[2] = 0; |
| 49 | break; |
| 50 | |
| 51 | case 1 : |
| 52 | pxOut[1] = pxIn[1]; |
| 53 | pxOut[0] = 0; |
| 54 | pxOut[2] = 0; |
| 55 | break; |
| 56 | |
| 57 | case 2 : |
| 58 | pxOut[2] = pxIn[2]; |
| 59 | pxOut[1] = 0; |
| 60 | pxOut[0] = 0; |
| 61 | break; |
| 62 | } |
| 63 | pxOut[3] = pxIn[3]; |
| 64 | } |
| 65 | |
| 66 | int f0r_init() |
| 67 | { |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | void f0r_deinit() |
| 72 | { /* no initialization required */ } |
| 73 | |
| 74 | void f0r_get_plugin_info(f0r_plugin_info_t* rgbsplit0rInfo) |
| 75 | { |
| 76 | rgbsplit0rInfo->name = "rgbsplit0r"; |
| 77 | rgbsplit0rInfo->author = "IDENT Software"; |
| 78 | rgbsplit0rInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 79 | rgbsplit0rInfo->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 80 | rgbsplit0rInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 81 | rgbsplit0rInfo->major_version = 1; |
| 82 | rgbsplit0rInfo->minor_version = 1; |
| 83 | rgbsplit0rInfo->num_params = 2; |
| 84 | rgbsplit0rInfo->explanation = "RGB splitting and shifting"; |
| 85 | } |
| 86 | |
| 87 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 88 | { |
| 89 | switch(param_index) { |
| 90 | case 0: |
| 91 | { |
| 92 | info->name = "Vertical split distance"; |
| 93 | info->type = F0R_PARAM_DOUBLE; |
| 94 | info->explanation = "How far should layers be moved vertically from each other"; |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | case 1: |
| 99 | { |
| 100 | info->name = "Horizontal split distance"; |
| 101 | info->type = F0R_PARAM_DOUBLE; |
| 102 | info->explanation = "How far should layers be moved horizontally from each other"; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 109 | { |
| 110 | rgbsplit0r_instance_t* inst = (rgbsplit0r_instance_t*)calloc(1, sizeof(*inst)); |
| 111 | inst->width = width; inst->height = height; |
| 112 | inst->shiftY = 0; |
| 113 | inst->shiftX = 0; |
| 114 | return (f0r_instance_t)inst; |
| 115 | } |
| 116 | |
| 117 | void f0r_destruct(f0r_instance_t instance) |
| 118 | { |
| 119 | free(instance); |
| 120 | } |
| 121 | |
| 122 | void f0r_set_param_value(f0r_instance_t instance, |
| 123 | f0r_param_t param, int param_index) |
| 124 | { |
| 125 | assert(instance); |
| 126 | rgbsplit0r_instance_t *inst = (rgbsplit0r_instance_t*)instance; |
| 127 | |
| 128 | switch (param_index) |
| 129 | { |
| 130 | |
| 131 | case 0 : // vertical shift |
| 132 | { |
| 133 | // scale to [-1/16..1/16] |
| 134 | double shiftY = *((double*)param) - 0.5; |
| 135 | |
| 136 | // Convert to range from 0 to one eighth of height |
| 137 | if (inst->height > 0) |
| 138 | shiftY = ((inst->height / 8) * shiftY); |
| 139 | else |
| 140 | shiftY = 0; |
| 141 | |
| 142 | inst->shiftY = (unsigned int)shiftY; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | case 1 : // horizontal shift |
| 147 | { |
| 148 | // scale to [-1/16..1/16] |
| 149 | double shiftX = *((double*)param) - 0.5; |
| 150 | |
| 151 | // Convert to range from 0 to one eighth of width |
| 152 | if (inst->width > 0) |
| 153 | shiftX = ((inst->width / 8) * shiftX); |
| 154 | else |
| 155 | shiftX = 0; |
| 156 | |
| 157 | inst->shiftX = (unsigned int)shiftX; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void f0r_get_param_value(f0r_instance_t instance, |
| 164 | f0r_param_t param, int param_index) |
| 165 | { |
| 166 | |
| 167 | assert(instance); |
| 168 | rgbsplit0r_instance_t *inst = (rgbsplit0r_instance_t*)instance; |
| 169 | |
| 170 | switch (param_index) |
| 171 | { |
| 172 | case 0 : // vertical shift |
| 173 | { |
| 174 | // convert plugin's param to frei0r range |
| 175 | if (inst->height > 0) |
| 176 | *((double*)param) = (inst->shiftY) / (inst->height / 8) + 0.5; |
| 177 | else |
| 178 | *((double*)param) = 0.5; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | case 1 : // horizontal shift |
| 183 | { |
| 184 | // convert plugin's param to frei0r range |
| 185 | if (inst->width > 0) |
| 186 | *((double*)param) = (inst->shiftX) / (inst->width / 8) + 0.5; |
| 187 | else |
| 188 | *((double*)param) = 0.5; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void f0r_update(f0r_instance_t instance, double time, |
| 197 | const uint32_t* src, uint32_t* dst) |
| 198 | { |
| 199 | assert(instance); |
| 200 | rgbsplit0r_instance_t* inst = (rgbsplit0r_instance_t*)instance; |
| 201 | unsigned int x, y; |
| 202 | |
| 203 | for (y = 0; y < inst->height; y++) |
| 204 | for (x = 0; x < inst->width; x++) |
| 205 | { |
| 206 | uint32_t pxR = 0, pxG = 0, pxB = 0; |
| 207 | |
| 208 | // First make a blue layer shifted back |
| 209 | if (((int)x >= (int)inst->shiftX) && |
| 210 | ((int)y >= (int)inst->shiftY) && |
| 211 | ((x - inst->shiftX) < inst->width) && |
| 212 | ((y - inst->shiftY) < inst->height)) |
| 213 | { |
| 214 | rgbsplit0r_extract_color((uint32_t *)(src + |
| 215 | (x - inst->shiftX) + |
| 216 | (y - inst->shiftY)*inst->width), |
| 217 | &pxB, 2); |
| 218 | } |
| 219 | |
| 220 | // The red layer is shifted forward |
| 221 | if ((x + inst->shiftX < inst->width) && |
| 222 | (y + inst->shiftY < inst->height)) |
| 223 | { |
| 224 | rgbsplit0r_extract_color((uint32_t *)(src + |
| 225 | (x + inst->shiftX) + |
| 226 | (y + inst->shiftY)*inst->width), |
| 227 | &pxR, 0); |
| 228 | } |
| 229 | |
| 230 | // Green layer is on its place |
| 231 | rgbsplit0r_extract_color((uint32_t *)(src + x + (y*inst->width)), |
| 232 | &pxG, 1); |
| 233 | |
| 234 | *(dst + x + (y*inst->width)) = (pxG | pxB | pxR); |
| 235 | } |
| 236 | } |