1 /*
2  * Copyright 2006-2016 Christian Stigen Larsen
3  * Copyright 2020 Christoph Raitzig
4  * Distributed under the GNU General Public License (GPL) v2.
5  */
6 
7 #include "options.h"
8 #include "round.h"
9 
aspect_ratio(const int jpeg_width,const int jpeg_height)10 void aspect_ratio(const int jpeg_width, const int jpeg_height) {
11 
12 	// the 2.0f and 0.5f factors are used for text displays that (usually) have characters
13 	// that are taller than they are wide.
14 
15 	#define CALC_WIDTH ROUND(2.0f * (float) height * (float) jpeg_width / (float) jpeg_height)
16 	#define CALC_HEIGHT ROUND(0.5f * (float) width * (float) jpeg_height / (float) jpeg_width)
17 
18 	// calc width
19 	if ( auto_width && !auto_height ) {
20 		width = CALC_WIDTH;
21 
22 		// adjust for too small dimensions
23 		while ( width==0 ) {
24 			++height;
25 			aspect_ratio(jpeg_width, jpeg_height);
26 		}
27 
28 		while ( termfit==TERM_FIT_AUTO && (width + use_border*2)>term_width ) {
29 			width = term_width - use_border*2;
30 			height = 0;
31 			auto_height = 1;
32 			auto_width = 0;
33 			aspect_ratio(jpeg_width, jpeg_height);
34 		}
35 
36 	}
37 
38 	// calc height
39 	if ( !auto_width && auto_height ) {
40 		height = CALC_HEIGHT;
41 
42 		// adjust for too small dimensions
43 		if ( height==0 ) {
44 			height = ( jpeg_height == 1 )? 0 : 1;
45 		}
46 	}
47 }
48