2
* feColorMatrix filter primitive renderer
5
* Felipe Corrêa da Silva Sanches <felipe.sanches@gmail.com>
7
* Copyright (C) 2007 authors
9
* Released under GNU GPL, read the file 'COPYING' for more information
12
#include "display/nr-filter-colormatrix.h"
13
#include "display/nr-filter-units.h"
14
#include "display/nr-filter-utils.h"
15
#include "libnr/nr-blit.h"
20
FilterColorMatrix::FilterColorMatrix()
24
FilterPrimitive * FilterColorMatrix::create() {
25
return new FilterColorMatrix();
28
FilterColorMatrix::~FilterColorMatrix()
31
int FilterColorMatrix::render(FilterSlot &slot, FilterUnits const &/*units*/) {
32
NRPixBlock *in = slot.get(_input);
34
g_warning("Missing source image for feColorMatrix (in=%d)", _input);
38
NRPixBlock *out = new NRPixBlock;
40
nr_pixblock_setup_fast(out, NR_PIXBLOCK_MODE_R8G8B8A8N,
41
in->area.x0, in->area.y0, in->area.x1, in->area.y1,
44
// this primitive is defined for non-premultiplied RGBA values,
45
// thus convert them to that format
46
if (in->mode != NR_PIXBLOCK_MODE_R8G8B8A8N) {
47
NRPixBlock *original_in = in;
49
nr_pixblock_setup_fast(in, NR_PIXBLOCK_MODE_R8G8B8A8N,
50
original_in->area.x0, original_in->area.y0,
51
original_in->area.x1, original_in->area.y1,
53
nr_blit_pixblock_pixblock(in, original_in);
56
unsigned char *in_data = NR_PIXBLOCK_PX(in);
57
unsigned char *out_data = NR_PIXBLOCK_PX(out);
58
unsigned char r,g,b,a;
59
int x,y,x0,y0,x1,y1,i;
60
double a00,a01,a02,a10,a11,a12,a20,a21,a22, coshue, sinhue;
67
case COLORMATRIX_MATRIX:
68
if (values.size()!=20) {
69
g_warning("ColorMatrix: values parameter error. Wrong size: %i.", static_cast<int>(values.size()));
74
i = ((x-x0) + (x1-x0)*(y-y0))*4;
79
out_data[i] = CLAMP_D_TO_U8( r*values[0] + g*values[1] + b*values[2] + a*values[3] + 255*values[4] );
80
out_data[i+1] = CLAMP_D_TO_U8( r*values[5] + g*values[6] + b*values[7] + a*values[8] + 255*values[9] );
81
out_data[i+2] = CLAMP_D_TO_U8( r*values[10] + g*values[11] + b*values[12] + a*values[13] + 255*values[14] );
82
out_data[i+3] = CLAMP_D_TO_U8( r*values[15] + g*values[16] + b*values[17] + a*values[18] + 255*values[19] );
86
case COLORMATRIX_SATURATE:
89
i = ((x-x0) + (x1-x0)*(y-y0))*4;
94
out_data[i] = CLAMP_D_TO_U8( r*(0.213+0.787*value) + g*(0.715-0.715*value) + b*(0.072-0.072*value) );
95
out_data[i+1] = CLAMP_D_TO_U8( r*(0.213-0.213*value) + g*(0.715+0.285*value) + b*(0.072-0.072*value) );
96
out_data[i+2] = CLAMP_D_TO_U8( r*(0.213-0.213*value) + g*(0.715-0.715*value) + b*(0.072+0.928*value) );
101
case COLORMATRIX_HUEROTATE:
102
coshue = cos(value * M_PI/180.0);
103
sinhue = sin(value * M_PI/180.0);
104
a00 = 0.213 + coshue*( 0.787) + sinhue*(-0.213);
105
a01 = 0.715 + coshue*(-0.715) + sinhue*(-0.715);
106
a02 = 0.072 + coshue*(-0.072) + sinhue*( 0.928);
107
a10 = 0.213 + coshue*(-0.213) + sinhue*( 0.143);
108
a11 = 0.715 + coshue*( 0.285) + sinhue*( 0.140);
109
a12 = 0.072 + coshue*(-0.072) + sinhue*(-0.283);
110
a20 = 0.213 + coshue*(-0.213) + sinhue*(-0.787);
111
a21 = 0.715 + coshue*(-0.715) + sinhue*( 0.715);
112
a22 = 0.072 + coshue*( 0.928) + sinhue*( 0.072);
116
i = ((x-x0) + (x1-x0)*(y-y0))*4;
122
out_data[i] = CLAMP_D_TO_U8( r*a00 + g*a01 + b*a02 );
123
out_data[i+1] = CLAMP_D_TO_U8( r*a10 + g*a11 + b*a12 );
124
out_data[i+2] = CLAMP_D_TO_U8( r*a20 + g*a21 + b*a22 );
129
case COLORMATRIX_LUMINANCETOALPHA:
132
i = ((x-x0) + (x1-x0)*(y-y0))*4;
139
out_data[i+3] = CLAMP_D_TO_U8( r*0.2125 + g*0.7154 + b*0.0721);
143
case COLORMATRIX_ENDTYPE:
147
slot.set(_output, out);
151
void FilterColorMatrix::area_enlarge(NRRectL &/*area*/, Matrix const &/*trans*/)
155
void FilterColorMatrix::set_type(FilterColorMatrixType t){
159
void FilterColorMatrix::set_value(gdouble v){
163
void FilterColorMatrix::set_values(std::vector<gdouble> &v){
172
c-file-style:"stroustrup"
173
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :