1 /*
2  */
3 
4 /*
5 
6     Copyright (C) 2014 Ferrero Andrea
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 
22  */
23 
24 /*
25 
26     These files are distributed with PhotoFlow - http://aferrero2707.github.io/PhotoFlow/
27 
28  */
29 
30 #include "crop.hh"
31 #include "../base/new_operation.hh"
32 
33 
CropPar()34 PF::CropPar::CropPar():
35   OpParBase(),
36   crop_left("crop_left",this,0),
37   crop_top("crop_top",this,0),
38   crop_width("crop_width",this,0),
39   crop_height("crop_height",this,0),
40   keep_ar("keep_ar",this,0),
41   ar_mode("ar_mode", this, PF::CROP_AR_INPUT,"CROP_AR_INPUT","orignal"),
42   ar_width("ar_width",this,100),
43   ar_height("ar_height",this,100)
44 {
45   ar_mode.add_enum_value(PF::CROP_AR_NONE,"CROP_AR_NONE","none");
46   ar_mode.add_enum_value(PF::CROP_AR_INPUT,"CROP_AR_INPUT","orignal");
47   ar_mode.add_enum_value(PF::CROP_AR_CUSTOM,"CROP_AR_CUSTOM","custom");
48   ar_mode.add_enum_value(PF::CROP_AR_1_1,"CROP_AR_1_1","1:1");
49   ar_mode.add_enum_value(PF::CROP_AR_3_2,"CROP_AR_3_2","3:2");
50   ar_mode.add_enum_value(PF::CROP_AR_2_3,"CROP_AR_2_3","2:3");
51   ar_mode.add_enum_value(PF::CROP_AR_4_3,"CROP_AR_4_3","4:3");
52   ar_mode.add_enum_value(PF::CROP_AR_3_4,"CROP_AR_3_4","3:4");
53   ar_mode.add_enum_value(PF::CROP_AR_A4,"CROP_AR_A4","A4");
54   ar_mode.add_enum_value(PF::CROP_AR_A4R,"CROP_AR_A4R","A4R");
55   ar_mode.add_enum_value(PF::CROP_AR_A3,"CROP_AR_A3","A3");
56   ar_mode.add_enum_value(PF::CROP_AR_A3R,"CROP_AR_A3R","A3R");
57   ar_mode.add_enum_value(PF::CROP_AR_16_9,"CROP_AR_16_9","16:9");
58   ar_mode.add_enum_value(PF::CROP_AR_16_10,"CROP_AR_16_10","16:10");
59 
60   set_type( "crop" );
61 
62   set_default_name( _("crop") );
63 }
64 
65 
66 
build(std::vector<VipsImage * > & in,int first,VipsImage * imap,VipsImage * omap,unsigned int & level)67 VipsImage* PF::CropPar::build(std::vector<VipsImage*>& in, int first,
68 				   VipsImage* imap, VipsImage* omap,
69 				   unsigned int& level)
70 {
71   VipsImage* srcimg = NULL;
72   if( in.size() > 0 ) srcimg = in[0];
73 	if( srcimg == NULL ) return NULL;
74 	VipsImage* out;
75 
76   if( (get_render_mode() == PF_RENDER_PREVIEW) && is_editing() ) {
77 #ifndef NDEBUG
78     std::cout<<"CropPar::build(): editing, returning source image"<<std::endl;
79 #endif
80     PF_REF( srcimg, "CropPar::build(): srcimg ref (editing mode)" );
81     return srcimg;
82   }
83 
84   int scale_factor = 1;
85   for(unsigned int l = 0; l < level; l++ ) {
86     scale_factor *= 2;
87   }
88 
89   if( ((crop_width.get()/scale_factor) < 1) ||
90       ((crop_height.get()/scale_factor) < 1) ) {
91     PF_REF( srcimg, "CropPar::build(): srcimg ref (editing mode)" );
92     return srcimg;
93   }
94 
95   int cleft = crop_left.get()/scale_factor;
96   int ctop = crop_top.get()/scale_factor;
97   int cw = crop_width.get()/scale_factor;
98   int ch = crop_height.get()/scale_factor;
99   if( (cleft+cw) > srcimg->Xsize ) cw = srcimg->Xsize - cleft;
100   if( (ctop+ch) > srcimg->Ysize ) ch = srcimg->Ysize - ctop;
101 
102   if( vips_crop( srcimg, &out, cleft, ctop, cw, ch, NULL ) ) {
103     std::cout<<"WARNIG: CropPar::build(): vips_crop() failed."<<std::endl;
104     std::cout<<"srcimg->Xsize="<<srcimg->Xsize<<"  srcimg->Ysize="<<srcimg->Ysize<<std::endl;
105     std::cout<<"vips_crop( srcimg, &out, "<<crop_left.get()/scale_factor<<", "<<crop_top.get()/scale_factor<<", "
106         <<crop_width.get()/scale_factor<<", "<<crop_height.get()/scale_factor<<", NULL )"<<std::endl;
107     return NULL;
108   }
109   //std::cout<<"srcimg->Xsize="<<srcimg->Xsize<<"  srcimg->Ysize="<<srcimg->Ysize<<std::endl;
110   //std::cout<<"outimg="<<out<<std::endl;
111   //std::cout<<"outimg->Xsize="<<out->Xsize<<"  outimg->Ysize="<<out->Ysize<<std::endl;
112   return out;
113 }
114