| 1 | /* sobel.cpp |
| 2 | * Copyright (C) 2005 Jean-Sebastien Senecal ([email protected]) |
| 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 "frei0r.hpp" |
| 21 | #include "frei0r/math.h" |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | class sobel : public frei0r::filter |
| 25 | { |
| 26 | public: |
| 27 | sobel(unsigned int width, unsigned int height) |
| 28 | { |
| 29 | this->width = width; |
| 30 | this->height = height; |
| 31 | } |
| 32 | |
| 33 | virtual void update(double time, |
| 34 | uint32_t* out, |
| 35 | const uint32_t* in) |
| 36 | { |
| 37 | if (width == 0 || height == 0) return; |
| 38 | |
| 39 | std::copy(in, in + width*height, out); |
| 40 | for (unsigned int y=1; y<height-1; ++y) |
| 41 | { |
| 42 | for (unsigned int x=1; x<width-1; ++x) |
| 43 | { |
| 44 | // XXX faut eviter le * width en y allant par rangee |
| 45 | unsigned char *p1 = (unsigned char *)&in[(y-1)*width+(x-1)]; |
| 46 | unsigned char *p2 = (unsigned char *)&in[(y-1)*width+x]; |
| 47 | unsigned char *p3 = (unsigned char *)&in[(y-1)*width+(x+1)]; |
| 48 | unsigned char *p4 = (unsigned char *)&in[y*width+(x-1)]; |
| 49 | unsigned char *p6 = (unsigned char *)&in[y*width+(x+1)]; |
| 50 | unsigned char *p7 = (unsigned char *)&in[(y+1)*width+(x-1)]; |
| 51 | unsigned char *p8 = (unsigned char *)&in[(y+1)*width+x]; |
| 52 | unsigned char *p9 = (unsigned char *)&in[(y+1)*width+(x+1)]; |
| 53 | |
| 54 | unsigned char *g = (unsigned char *)&out[y*width+x]; |
| 55 | |
| 56 | for (int i=0; i<3; ++i) |
| 57 | g[i] = CLAMP0255( |
| 58 | abs(p1[i] + p2[i]*2 + p3[i] - p7[i] - p8[i]*2 - p9[i]) + |
| 59 | abs(p3[i] + p6[i]*2 + p9[i] - p1[i] - p4[i]*2 - p7[i]) ); |
| 60 | |
| 61 | g[3] = ((unsigned char*)&in[y*width+x])[3]; // copy alpha |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | frei0r::construct<sobel> plugin("Sobel", |
| 69 | "Sobel filter", |
| 70 | "Jean-Sebastien Senecal (Drone)", |
| 71 | 0,2, |
| 72 | F0R_COLOR_MODEL_RGBA8888); |
| 73 |