| 1 | /* |
|---|
| 2 | * libpipi Pathetic image processing interface library |
|---|
| 3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * wave.c: wave and warping effects |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <math.h> |
|---|
| 25 | |
|---|
| 26 | #include "pipi.h" |
|---|
| 27 | #include "pipi_internals.h" |
|---|
| 28 | |
|---|
| 29 | pipi_image_t *pipi_wave(pipi_image_t *src, double freq, double phase, |
|---|
| 30 | double theta, double xamp, double yamp) |
|---|
| 31 | { |
|---|
| 32 | pipi_image_t *dst; |
|---|
| 33 | pipi_pixels_t *srcp, *dstp; |
|---|
| 34 | float *srcdata, *dstdata; |
|---|
| 35 | double sint, cost; |
|---|
| 36 | int x, y, w, h, i, gray; |
|---|
| 37 | |
|---|
| 38 | w = src->w; |
|---|
| 39 | h = src->h; |
|---|
| 40 | |
|---|
| 41 | gray = (src->last_modified == PIPI_PIXELS_Y_F32); |
|---|
| 42 | |
|---|
| 43 | srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
|---|
| 44 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
|---|
| 45 | srcdata = (float *)srcp->pixels; |
|---|
| 46 | |
|---|
| 47 | dst = pipi_new(w, h); |
|---|
| 48 | dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
|---|
| 49 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
|---|
| 50 | dstdata = (float *)dstp->pixels; |
|---|
| 51 | |
|---|
| 52 | sint = sin(theta); |
|---|
| 53 | cost = cos(theta); |
|---|
| 54 | |
|---|
| 55 | for(y = 0; y < h; y++) |
|---|
| 56 | { |
|---|
| 57 | for(x = 0; x < w; x++) |
|---|
| 58 | { |
|---|
| 59 | double t = cost * (x - w / 2) + sint * (y - h / 2); |
|---|
| 60 | double step = sin(t * freq + phase); |
|---|
| 61 | double dx = xamp; |
|---|
| 62 | double dy = yamp; |
|---|
| 63 | int x2, y2; |
|---|
| 64 | |
|---|
| 65 | if(x < 32) dx = dx * x / 32; |
|---|
| 66 | if(x > w - 1 - 32) dx = dx * (w - 1 - x) / 32; |
|---|
| 67 | if(y < 32) dy = dy * y / 32; |
|---|
| 68 | if(y > h - 1 - 32) dy = dy * (h - 1 - y) / 32; |
|---|
| 69 | |
|---|
| 70 | x2 = x + dx * step; |
|---|
| 71 | y2 = y + dy * step; |
|---|
| 72 | |
|---|
| 73 | /* Just in case... */ |
|---|
| 74 | if(x2 < 0) x2 = 0; |
|---|
| 75 | else if(x2 >= w) x2 = w - 1; |
|---|
| 76 | |
|---|
| 77 | if(y2 < 0) y2 = 0; |
|---|
| 78 | else if(y2 >= h) y2 = h - 1; |
|---|
| 79 | |
|---|
| 80 | if(gray) |
|---|
| 81 | { |
|---|
| 82 | dstdata[y * w + x] = srcdata[y2 * w + x2]; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | for(i = 0; i < 4; i++) |
|---|
| 87 | { |
|---|
| 88 | dstdata[4 * (y * w + x) + i] |
|---|
| 89 | = srcdata[4 * (y2 * w + x2) + i]; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | return dst; |
|---|
| 96 | } |
|---|
| 97 | |
|---|