1 /* dE76.c
2  *
3  * Modified:
4  * 16/11/94 JC
5  *	- partialed!
6  * 31/10/09
7  * 	- use im__colour_binary()
8  * 	- gtkdoc comment
9  * 25/10/12
10  * 	- redone as a class
11  */
12 
13 /*
14 
15     This file is part of VIPS.
16 
17     VIPS is free software; you can redistribute it and/or modify
18     it under the terms of the GNU Lesser General Public License as published by
19     the Free Software Foundation; either version 2 of the License, or
20     (at your option) any later version.
21 
22     This program is distributed in the hope that it will be useful,
23     but WITHOUT ANY WARRANTY; without even the implied warranty of
24     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25     GNU Lesser General Public License for more details.
26 
27     You should have received a copy of the GNU Lesser General Public License
28     along with this program; if not, write to the Free Software
29     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30     02110-1301  USA
31 
32  */
33 
34 /*
35 
36     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
37 
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif /*HAVE_CONFIG_H*/
43 #include <vips/intl.h>
44 
45 #include <math.h>
46 
47 #include <vips/vips.h>
48 #include <vips/debug.h>
49 
50 #include "pcolour.h"
51 
52 typedef struct _VipsdE76 {
53 	VipsColourDifference parent_instance;
54 
55 } VipsdE76;
56 
57 typedef VipsColourDifferenceClass VipsdE76Class;
58 
59 G_DEFINE_TYPE( VipsdE76, vips_dE76, VIPS_TYPE_COLOUR_DIFFERENCE );
60 
61 /**
62  * vips_pythagoras:
63  * @L1: Input coordinate 1
64  * @a1: Input coordinate 1
65  * @b1: Input coordinate 1
66  * @L2: Input coordinate 2
67  * @a2: Input coordinate 2
68  * @b2: Input coordinate 2
69  *
70  * Pythagorean distance between two points in colour space. Lab/XYZ/CMC etc.
71  */
72 float
vips_pythagoras(float L1,float a1,float b1,float L2,float a2,float b2)73 vips_pythagoras( float L1, float a1, float b1, float L2, float a2, float b2 )
74 {
75 	float dL = L1 - L2;
76 	float da = a1 - a2;
77 	float db = b1 - b2;
78 
79 	return( sqrt( dL * dL + da * da + db * db ) );
80 }
81 
82 /* Find the difference between two buffers of LAB data.
83  */
84 void
vips__pythagoras_line(VipsColour * colour,VipsPel * out,VipsPel ** in,int width)85 vips__pythagoras_line( VipsColour *colour,
86 	VipsPel *out, VipsPel **in, int width )
87 {
88 	float * restrict p1 = (float *) in[0];
89 	float * restrict p2 = (float *) in[1];
90 	float * restrict q = (float *) out;
91 
92 	int x;
93 
94 	for( x = 0; x < width; x++ ) {
95 		float dL = p1[0] - p2[0];
96 		float da = p1[1] - p2[1];
97 		float db = p1[2] - p2[2];
98 
99 		q[x] = sqrt( dL * dL + da * da + db * db );
100 
101 		p1 += 3;
102 		p2 += 3;
103 	}
104 }
105 
106 static void
vips_dE76_class_init(VipsdE76Class * class)107 vips_dE76_class_init( VipsdE76Class *class )
108 {
109 	VipsObjectClass *object_class = (VipsObjectClass *) class;
110 	VipsColourClass *colour_class = VIPS_COLOUR_CLASS( class );
111 
112 	object_class->nickname = "dE76";
113 	object_class->description = _( "calculate dE76" );
114 
115 	colour_class->process_line = vips__pythagoras_line;
116 }
117 
118 static void
vips_dE76_init(VipsdE76 * dE76)119 vips_dE76_init( VipsdE76 *dE76 )
120 {
121 	VipsColourDifference *difference = VIPS_COLOUR_DIFFERENCE( dE76 );
122 
123 	difference->interpretation = VIPS_INTERPRETATION_LAB;
124 }
125 
126 /**
127  * vips_dE76:
128  * @left: first input image
129  * @right: second input image
130  * @out: (out): output image
131  * @...: %NULL-terminated list of optional named arguments
132  *
133  * Calculate dE 76.
134  *
135  * Returns: 0 on success, -1 on error
136  */
137 int
vips_dE76(VipsImage * left,VipsImage * right,VipsImage ** out,...)138 vips_dE76( VipsImage *left, VipsImage *right, VipsImage **out, ... )
139 {
140 	va_list ap;
141 	int result;
142 
143 	va_start( ap, out );
144 	result = vips_call_split( "dE76", ap, left, right, out );
145 	va_end( ap );
146 
147 	return( result );
148 }
149