| 1 | /* uvmap.c |
|---|
| 2 | * Copyright (C) 2008 Richard Spindler (richard.spindler@gmail.com) |
|---|
| 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 <assert.h> |
|---|
| 22 | #include <math.h> |
|---|
| 23 | |
|---|
| 24 | #include "frei0r.h" |
|---|
| 25 | |
|---|
| 26 | typedef struct uvmap_instance |
|---|
| 27 | { |
|---|
| 28 | unsigned int width; |
|---|
| 29 | unsigned int height; |
|---|
| 30 | } uvmap_instance_t; |
|---|
| 31 | |
|---|
| 32 | int f0r_init() |
|---|
| 33 | { |
|---|
| 34 | return 1; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | void f0r_deinit() |
|---|
| 38 | { /* no initialization required */ } |
|---|
| 39 | |
|---|
| 40 | void f0r_get_plugin_info(f0r_plugin_info_t* uvmapInfo) |
|---|
| 41 | { |
|---|
| 42 | uvmapInfo->name = "UV Map"; |
|---|
| 43 | uvmapInfo->author = "Richard Spindler"; |
|---|
| 44 | uvmapInfo->plugin_type = F0R_PLUGIN_TYPE_MIXER2; |
|---|
| 45 | uvmapInfo->color_model = F0R_COLOR_MODEL_RGBA8888; |
|---|
| 46 | uvmapInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
|---|
| 47 | uvmapInfo->major_version = 0; |
|---|
| 48 | uvmapInfo->minor_version = 9; |
|---|
| 49 | uvmapInfo->num_params = 0; |
|---|
| 50 | uvmapInfo->explanation = "Uses Input 1 as UV Map to distort Input 2"; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
|---|
| 54 | { |
|---|
| 55 | /* no params */ |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
|---|
| 59 | { |
|---|
| 60 | uvmap_instance_t* inst = (uvmap_instance_t*)calloc(1, sizeof(*inst)); |
|---|
| 61 | inst->width = width; inst->height = height; |
|---|
| 62 | return (f0r_instance_t)inst; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void f0r_destruct(f0r_instance_t instance) |
|---|
| 66 | { |
|---|
| 67 | free(instance); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | void f0r_set_param_value(f0r_instance_t instance, |
|---|
| 71 | f0r_param_t param, int param_index) |
|---|
| 72 | { /* no params */ } |
|---|
| 73 | |
|---|
| 74 | void f0r_get_param_value(f0r_instance_t instance, |
|---|
| 75 | f0r_param_t param, int param_index) |
|---|
| 76 | { /* no params */ } |
|---|
| 77 | |
|---|
| 78 | #if defined(_MSC_VER) |
|---|
| 79 | __inline const long int lrintf(float x){ |
|---|
| 80 | return (long int)(x+0.5); |
|---|
| 81 | } |
|---|
| 82 | #endif /* _MSC_VER */ |
|---|
| 83 | |
|---|
| 84 | void f0r_update2(f0r_instance_t instance, |
|---|
| 85 | double time, |
|---|
| 86 | const uint32_t* inframe1, |
|---|
| 87 | const uint32_t* inframe2, |
|---|
| 88 | const uint32_t* inframe3, |
|---|
| 89 | uint32_t* outframe) |
|---|
| 90 | { |
|---|
| 91 | assert(instance); |
|---|
| 92 | uvmap_instance_t* inst = (uvmap_instance_t*)instance; |
|---|
| 93 | unsigned int w = inst->width; |
|---|
| 94 | unsigned int h = inst->height; |
|---|
| 95 | unsigned int x,y; |
|---|
| 96 | |
|---|
| 97 | uint32_t* dst = outframe; |
|---|
| 98 | const uint32_t* uvmap = inframe1; |
|---|
| 99 | const uint32_t* src = inframe2; |
|---|
| 100 | float fx, fy; |
|---|
| 101 | long px, py; |
|---|
| 102 | for( y = 0; y < h; ++y ) |
|---|
| 103 | for( x = 0; x < w; ++x ) { |
|---|
| 104 | /* The coordinates start in the lower left corner: |
|---|
| 105 | * |
|---|
| 106 | * ^ +-------------+ |
|---|
| 107 | * | | | |
|---|
| 108 | * G | | |
|---|
| 109 | * 0+-------------+ |
|---|
| 110 | * 0 R -> |
|---|
| 111 | * |
|---|
| 112 | */ |
|---|
| 113 | unsigned char* tmpc = (unsigned char*)uvmap; |
|---|
| 114 | fx = ((float)tmpc[0]) / 255.0; |
|---|
| 115 | fy = ((float)tmpc[1]) / 255.0; |
|---|
| 116 | fy = 1.0 - fy; |
|---|
| 117 | |
|---|
| 118 | px = lrintf( w * fx ); |
|---|
| 119 | py = lrintf( h * fy ); |
|---|
| 120 | if ( tmpc[2] > 128 ) { |
|---|
| 121 | *dst++ = src[px+w*py]; |
|---|
| 122 | } else { |
|---|
| 123 | *dst++ = 0x00000000; |
|---|
| 124 | } |
|---|
| 125 | uvmap++; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|