1 /* a complicated operation for testing
2  *
3  * 6/10/06
4  *	- hacked in
5  * 27/11/06
6  *	- added im_benchmarkn()
7  * 1/2/11
8  * 	- gtk-doc
9  */
10 
11 /*
12 
13     This file is part of VIPS.
14 
15     VIPS is free software; you can redistribute it and/or modify
16     it under the terms of the GNU Lesser General Public License as published by
17     the Free Software Foundation; either version 2 of the License, or
18     (at your option) any later version.
19 
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU Lesser General Public License for more details.
24 
25     You should have received a copy of the GNU Lesser General Public License
26     along with this program; if not, write to the Free Software
27     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28     02110-1301  USA
29 
30  */
31 
32 /*
33 
34     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
35 
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /*HAVE_CONFIG_H*/
41 #include <vips/intl.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <math.h>
46 
47 #include <vips/vips.h>
48 #include <vips/vips7compat.h>
49 
50 /*
51 
52 VIPS SMP benchmark
53 ------------------
54 
55 This is adapted from the system used to generate images for POD:
56 
57   http://cima.ng-london.org.uk/~john/POD
58 
59 Images from a 10k by 10k studio digital camera are colour processed, resized,
60 cropped and sharpened.
61 
62 The original POD script was written in nip (see below). This operation is a
63 reimplementation in vanilla C to make it easier to run (and less fragile!).
64 
65 This thing was originally processing images off a remote server over a 100mbit
66 network. No attempt was made to make it quick (there was no point): you
67 could make it a lot faster very easily.
68 
69 ------ benchmark in nip2 -----------
70 #!/home/john/vips/bin/nip2 -s
71 
72 // get command-line arguments
73 
74 image_path = argv?1;
75 crop_id = parse_pint argv?2;
76 crop_left = parse_pint argv?3;
77 crop_top = parse_pint argv?4;
78 crop_width = parse_pint argv?5;
79 crop_height = parse_pint argv?6;
80 width = parse_pint argv?7;
81 height = parse_pint argv?8;
82 sharp = parse_pint argv?9;
83 
84 // scale down by this much to undo photographic's relativisation
85 darken = Vector [1.18, 1, 1];
86 
87 // fudge factor in XYZ to get a match under NGC lights on uv-durable paper
88 white_point_adjust = Vector [1.06, 1, 1.01];
89 
90 // brighten by this in XYZ to get relative colorimetry
91 brighten = 1.5;
92 
93 // blacks down by this much in LAB
94 blacks_down = Vector [-2, 0, 0];
95 
96 // sharpen params for 400, 300, 200 and 150 dpi
97 // just change the size of the area we search
98 sharpen_params_table = [
99 	[ 11, 2.5, 40, 20, 0.5, 1.5 ],
100 	[ 7, 2.5, 40, 20, 0.5, 1.5 ],
101 	[ 5, 2.5, 40, 20, 0.5, 1.5 ],
102 	[ 3, 2.5, 40, 20, 0.5, 1.5 ]
103 ];
104 
105 // convert D65 XYZ to D50 XYZ
106 D652D50 = recomb D652D50_direct;
107 
108 stage_crop in
109 	= extract_area crop_left crop_top crop_width crop_height in,
110 		 crop_id != 0
111 	= in;
112 
113 // fit within a width / height
114 stage_shrink image
115 	= image, factor > 1;	// never upscale
116 	= resize factor factor Interpolate.BILINEAR image
117 {
118         hfactor = width / get_width image;
119         vfactor = height / get_height image;
120         factor = min_pair hfactor vfactor;
121 }
122 
123 // unphotoize, go to xyz, convert to D50, adjust white point, back to lab
124 stage_colour in
125         = if in?0 > 99 then Vector [100, 0, 0] else in'''
126 {
127 	// back to absolute
128         in' = in / darken;
129 
130 	xyz = colour_transform_to Image_type.XYZ in';
131 
132 	xyz' = D652D50 xyz * white_point_adjust * brighten;
133 
134         in'' = colour_transform_to Image_type.LAB xyz';
135 
136 	// shadows down
137 	in''' = in'' + blacks_down;
138 }
139 
140 stage_sharp in
141 	= (sharpen params?0 params?1 params?2 params?3 params?4 params?5 @
142 		colour_transform_to Image_type.LABQ) in
143 {
144 	params = sharpen_params_table?sharp;
145 }
146 
147 // This was:
148 //
149 // stage_srgb in
150 //	= (icc_export 8 "$VIPSHOME/share/nip2/data/sRGB.icm" 1 @
151 //		colour_transform_to Image_type.LABQ) in;
152 //
153 // but that uses lcms which is single-threaded. So for this benchmark, we use
154 // VIPS's own ->sRGB converter, which is less accurate but does thread.
155 stage_srgb in
156 	= colour_transform_to Image_type.sRGB in;
157 
158 main = (get_image @ stage_srgb @
159 	stage_sharp @ stage_colour @ stage_shrink @ stage_crop @
160 	colour_transform_to Image_type.LAB @ Image_file) image_path;
161 ------ benchmark in nip2 -----------
162 
163  */
164 
165 /* The main part of the benchmark ... transform labq to labq. Chain several of
166  * these together to get a CPU-bound operation.
167  */
168 static int
benchmark(IMAGE * in,IMAGE * out)169 benchmark( IMAGE *in, IMAGE *out )
170 {
171 	IMAGE *t[18];
172 	double one[3] = { 1.0, 1.0, 1.0 };
173 	double zero[3] = { 0.0, 0.0, 0.0 };
174 	double darken[3] = { 1.0 / 1.18, 1.0, 1.0 };
175 	double whitepoint[3] = { 1.06, 1.0, 1.01 };
176 	double shadow[3] = { -2, 0, 0 };
177 	double white[3] = { 100, 0, 0 };
178 	DOUBLEMASK *d652d50 = im_create_dmaskv( "d652d50", 3, 3,
179 		1.13529, -0.0604663, -0.0606321,
180 		0.0975399, 0.935024, -0.0256156,
181 		-0.0336428, 0.0414702, 0.994135 );
182 
183 	im_add_close_callback( out,
184 		(im_callback_fn) im_free_dmask, d652d50, NULL );
185 
186 	return(
187 		/* Set of descriptors for this operation.
188 		 */
189 		im_open_local_array( out, t, 18, "im_benchmark", "p" ) ||
190 
191 		/* Unpack to float.
192 		 */
193 		im_LabQ2Lab( in, t[0] ) ||
194 
195 		/* Crop 100 pixels off all edges.
196 		 */
197 		im_extract_area( t[0], t[1],
198 			100, 100, t[0]->Xsize - 200, t[0]->Ysize - 200 ) ||
199 
200 		/* Shrink by 10%, bilinear interp.
201 		 */
202 		im_affinei_all( t[1], t[2],
203 			vips_interpolate_bilinear_static(),
204 			0.9, 0, 0, 0.9,
205 			0, 0 ) ||
206 
207 		/* Find L ~= 100 areas (white surround).
208 		 */
209 		im_extract_band( t[2], t[3], 0 ) ||
210 		im_moreconst( t[3], t[4], 99 ) ||
211 
212 		/* Adjust white point and shadows.
213 		 */
214 		im_lintra_vec( 3, darken, t[2], zero, t[5] ) ||
215 		im_Lab2XYZ( t[5], t[6] ) ||
216 		im_recomb( t[6], t[7], d652d50 ) ||
217 		im_lintra_vec( 3, whitepoint, t[7], zero, t[8] ) ||
218 		im_lintra( 1.5, t[8], 0.0, t[9] ) ||
219 		im_XYZ2Lab( t[9], t[10] ) ||
220 		im_lintra_vec( 3, one, t[10], shadow, t[11] ) ||
221 
222 		/* Make a solid white image.
223 		 */
224 		im_black( t[12], t[4]->Xsize, t[4]->Ysize, 3 ) ||
225 		im_lintra_vec( 3, zero, t[12], white, t[13] ) ||
226 
227 		/* Reattach border.
228 		 */
229 		im_ifthenelse( t[4], t[13], t[11], t[14] ) ||
230 
231 		/* Sharpen.
232 		 */
233 		im_Lab2LabQ( t[14], t[15] ) ||
234 		im_sharpen( t[15], out, 11, 2.5, 40, 20, 0.5, 1.5 )
235 	);
236 }
237 
238 /**
239  * im_benchmarkn:
240  * @in: input image
241  * @out: output image
242  * @n: iterations
243  *
244  * This operation runs a complicated set of other operations on image @in,
245  * producing image @out. Use @n to set the number of iterations to run: a
246  * larger number will make the operation more CPU-bound, a smaller number will
247  * make the operation more IO-bound.
248  *
249  * See http://www.vips.ecs.soton.ac.uk/index.php?title=Benchmarks for a
250  * detailed discussion of the benchmark and some sample results.
251  *
252  * See also: im_benchmark2().
253  *
254  * Returns: 0 on success, -1 on error
255  */
256 int
im_benchmarkn(IMAGE * in,IMAGE * out,int n)257 im_benchmarkn( IMAGE *in, IMAGE *out, int n )
258 {
259 	IMAGE *t[2];
260 
261 	if( n == 0 )
262 		/* To sRGB.
263 		 */
264 		return( im_LabQ2disp( in, out, im_col_displays( 7 ) ) );
265 	else
266 		return( im_open_local_array( out, t, 2, "benchmarkn", "p" ) ||
267 			benchmark( in, t[0] ) ||
268 
269 			/* Expand back to the original size again ...
270 			 * benchmark does a 200 pixel crop plus a 10% shrink,
271 			 * so if we chain many of them together the image gets
272 			 * too small.
273 			 */
274 			im_affinei_all( t[0], t[1],
275 				vips_interpolate_bilinear_static(),
276 				(double) in->Xsize / t[0]->Xsize, 0, 0,
277 				(double) in->Ysize / t[0]->Ysize,
278 				0, 0 ) ||
279 
280 			im_benchmarkn( t[1], out, n - 1 ) );
281 }
282 
283 /**
284  * im_benchmark2:
285  * @in: input image
286  * @out: average image value
287  *
288  * This operation runs a single im_benchmarkn() and calculates the average
289  * pixel value. It's useful if you just want to test image input.
290  *
291  * See also: im_benchmarkn().
292  *
293  * Returns: 0 on success, -1 on error
294  */
295 int
im_benchmark2(IMAGE * in,double * out)296 im_benchmark2( IMAGE *in, double *out )
297 {
298         IMAGE *t;
299 
300         return(
301 		!(t = im_open_local( in, "benchmarkn", "p" )) ||
302                 im_benchmarkn( in, t, 1 ) ||
303                 im_avg( t, out )
304 	);
305 }
306 
307