| 1 | /* |
| 2 | * primaries |
| 3 | * 2009 Hedde Bosman |
| 4 | * |
| 5 | * This source code is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This source code 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. Please refer |
| 13 | * to the GNU Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Public License along |
| 16 | * with this source code; if not, write to: Free Software Foundation, |
| 17 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | * |
| 19 | */ |
| 20 | #include "frei0r.hpp" |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <vector> |
| 24 | #include <utility> |
| 25 | #include <cassert> |
| 26 | |
| 27 | #include <iostream> |
| 28 | |
| 29 | union px_t { |
| 30 | uint32_t u; |
| 31 | unsigned char c[4]; // 0=B, 1=G,2=R,3=A ? i think :P |
| 32 | }; |
| 33 | |
| 34 | class primaries : public frei0r::filter { |
| 35 | private: |
| 36 | double factor; |
| 37 | |
| 38 | public: |
| 39 | primaries(unsigned int width, unsigned int height) { |
| 40 | factor = 1; |
| 41 | register_param(factor, "Factor", "influence of mean px value. > 32 = 0"); |
| 42 | } |
| 43 | ~primaries() { |
| 44 | } |
| 45 | |
| 46 | virtual void update(double time, |
| 47 | uint32_t* out, |
| 48 | const uint32_t* in) { |
| 49 | unsigned char mean = 0; |
| 50 | |
| 51 | int f = factor+1; // f = [2,inf) |
| 52 | int factor127 = (f*f-3)*127; |
| 53 | int factorTot = f*f; |
| 54 | if (factor127 < 0) { |
| 55 | factor127 = 0; |
| 56 | factorTot = 3; |
| 57 | } |
| 58 | |
| 59 | for (unsigned int i = 0; i < size; i++) { |
| 60 | px_t pi; |
| 61 | pi.u = in[i]; |
| 62 | |
| 63 | if (f > 32) // influence of mean color value does hardly change after this value |
| 64 | mean = 127; |
| 65 | else |
| 66 | mean = (pi.c[0] + pi.c[1] + pi.c[2] + factor127)/factorTot; |
| 67 | pi.c[0] = (pi.c[0] > mean ? 255 : 0); |
| 68 | pi.c[1] = (pi.c[1] > mean ? 255 : 0); |
| 69 | pi.c[2] = (pi.c[2] > mean ? 255 : 0); |
| 70 | |
| 71 | out[i] = pi.u; |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | frei0r::construct<primaries> plugin("primaries", |
| 78 | "Reduce image to primary colors", |
| 79 | "Hedde Bosman", |
| 80 | 0,2); |
| 81 |