99static int verbose_flag = 0 ;
1010static int thumb_width = 0 ; // < 0 means % of input
1111static int thumb_height = 0 ; // < 0 means % of input
12+ static int crop_top = 0 ; // Pixels to crop from the top
13+ static int crop_bottom = 0 ; // Pixels to crop from the bottom
14+ static int crop_left = 0 ; // Pixels to crop from the left
15+ static int crop_right = 0 ; // Pixels to crop from the right
1216static int max_dimension = 0 ; // > 0 means we reduce max(w,h) to max_dimension, with aspect preserved
1317static int outbound_flag = 0 ; // Ensure specified dimensions will be covered
18+ static int crop_flag = 0 ; // Crop thumbnail after scaling
1419static int thumb_quality = 85 ; // Quality value from 1 to 100
1520static char * thumb_comment = NULL ;
1621static struct option long_options [] =
@@ -20,6 +25,7 @@ static struct option long_options[] =
2025 {"height" , required_argument , 0 , 'h' },
2126 {"max" , required_argument , 0 , 'm' },
2227 {"outbound" , no_argument , 0 , 'o' },
28+ {"crop" , no_argument , 0 , 'r' },
2329 {"quality" , required_argument , 0 , 'q' },
2430 {"comment" , required_argument , 0 , 'c' },
2531 {0 , 0 , 0 , 0 }
@@ -34,6 +40,7 @@ usage(const char *myname)
3440 " -h, --height=<heigth>[%%] set thumbnail heigth [%% of input]\n"
3541 " -m, --max=<maximum> reduce max(w,h) to maximum, with aspect preserved\n"
3642 " -o, --outbound cover at least the specified size (no upscaling or cropping)\n"
43+ " -r, --crop crop the resulting thumbnail (to be used with --outbound)\n"
3744 " -c, --comment=<comment> put a comment in thumbnail\n"
3845 " -q, --quality=<quality> set thumbnail quality (1-100)\n" , myname );
3946 exit (0 );
@@ -48,7 +55,7 @@ main(int argc, char **argv)
4855 char * input_file = NULL , * output_file = NULL ;
4956 char * p ;
5057
51- while ((c = getopt_long (argc , argv , "w:h:voc :m:q:" , long_options , & option_index )) != -1 ) {
58+ while ((c = getopt_long (argc , argv , "w:h:vorc :m:q:" , long_options , & option_index )) != -1 ) {
5259 switch (c ) {
5360 case 0 :
5461 usage (argv [0 ]);
@@ -93,6 +100,9 @@ main(int argc, char **argv)
93100 case 'o' :
94101 outbound_flag = 1 ;
95102 break ;
103+ case 'r' :
104+ crop_flag = 1 ;
105+ break ;
96106 case 'c' :
97107 thumb_comment = strdup (optarg );
98108 if (verbose_flag ) printf ("thumb_comment = %s\n" , thumb_comment );
@@ -130,6 +140,7 @@ main(int argc, char **argv)
130140 const char * com ;
131141 Epeg_Thumbnail_Info info ;
132142 int w , h ;
143+ int scaled_w , scaled_h ;
133144
134145 com = epeg_comment_get (im );
135146 if (verbose_flag ) if (com ) printf ("Comment: %s\n" , com );
@@ -161,17 +172,39 @@ main(int argc, char **argv)
161172 thumb_height = max_dimension ;
162173 thumb_width = max_dimension * w / h ;
163174 }
175+ if (outbound_flag && crop_flag ) {
176+ crop_top = (thumb_height - max_dimension ) / 2 ;
177+ crop_bottom = (thumb_height - max_dimension - crop_top );
178+ crop_left = (thumb_width - max_dimension ) / 2 ;
179+ crop_right = (thumb_width - max_dimension - crop_left );
180+ }
164181 } else if (outbound_flag ) {
165- thumb_width = MAX (thumb_width , thumb_height * w / h );
166- thumb_height = MAX (thumb_height , thumb_width * h / w );
182+ scaled_w = thumb_height * w / h ;
183+ scaled_h = thumb_width * h / w ;
184+ if (scaled_w > thumb_width ) {
185+ if (crop_flag ) {
186+ crop_left = (scaled_w - thumb_width ) / 2 ;
187+ crop_right = scaled_w - thumb_width - crop_left ;
188+ }
189+ thumb_width = scaled_w ;
190+ }
191+ if (scaled_h > thumb_height ) {
192+ if (crop_flag ) {
193+ crop_top = (scaled_h - thumb_height ) / 2 ;
194+ crop_bottom = scaled_h - thumb_height - crop_top ;
195+ }
196+ thumb_height = scaled_h ;
197+ }
167198 }
168199 }
169200
170201 if (verbose_flag ) printf ("Thumb size: %dx%d\n" , thumb_width , thumb_height );
202+ if (verbose_flag ) printf ("Crop (TxBxLxR): %dx%dx%dx%d\n" , crop_top , crop_bottom , crop_left , crop_right );
171203 epeg_decode_size_set (im , thumb_width , thumb_height );
172204 epeg_quality_set (im , thumb_quality );
173205 epeg_thumbnail_comments_enable (im , 1 );
174206 epeg_comment_set (im , thumb_comment );
207+ epeg_crop_set (im , crop_top , crop_bottom , crop_left , crop_right );
175208 epeg_file_output_set (im , output_file );
176209 epeg_encode (im );
177210 epeg_close (im );
0 commit comments