21 #include "imageclasses/ImgReaderGdal.h"
22 #include "imageclasses/ImgWriterGdal.h"
23 #include "base/Optionpk.h"
27 int main(
int argc,
char **argv) {
36 Optionpk<short> dim_opt(
"dim",
"dim",
"number of columns and rows in legend.", 100);
40 Optionpk<string> colorTable_opt(
"ct",
"ct",
"color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
41 Optionpk<string> oformat_opt(
"of",
"oformat",
"Output image format (see also gdal_translate). Empty string: inherit from input image",
"GTiff");
42 Optionpk<string> option_opt(
"co",
"co",
"Creation option for output file. Multiple options can be specified.");
43 Optionpk<string> description_opt(
"d",
"description",
"Set image description");
46 legend_opt.setHide(1);
51 doProcess=input_opt.retrieveOption(argc,argv);
52 output_opt.retrieveOption(argc,argv);
53 legend_opt.retrieveOption(argc,argv);
54 dim_opt.retrieveOption(argc,argv);
55 min_opt.retrieveOption(argc,argv);
56 max_opt.retrieveOption(argc,argv);
57 grey_opt.retrieveOption(argc,argv);
58 colorTable_opt.retrieveOption(argc,argv);
59 description_opt.retrieveOption(argc,argv);
60 oformat_opt.retrieveOption(argc,argv);
61 option_opt.retrieveOption(argc,argv);
62 verbose_opt.retrieveOption(argc,argv);
64 catch(
string predefinedString){
65 std::cout << predefinedString << std::endl;
70 cout <<
"Usage: pkcreatect -i input.txt -o output [-ct colortable | -min value -max value]" << endl;
72 std::cout <<
"short option -h shows basic options only, use long option --help to show all options" << std::endl;
76 GDALColorTable colorTable;
77 GDALColorEntry sEntry;
78 if(colorTable_opt.empty()){
80 for(
int i=min_opt[0];i<=max_opt[0];++i){
82 sEntry.c1=255*(i-min_opt[0])/(max_opt[0]-min_opt[0]);
83 sEntry.c2=255*(i-min_opt[0])/(max_opt[0]-min_opt[0]);
84 sEntry.c3=255*(i-min_opt[0])/(max_opt[0]-min_opt[0]);
90 double delta=max_opt[0]-min_opt[0];
91 if(i<(min_opt[0]+0.25*delta)){
93 sEntry.c2=255*4*(i-min_opt[0])/delta;
95 else if(i<(min_opt[0]+0.5*delta)){
97 sEntry.c3=255*(1+4*(min_opt[0]+0.25*delta-i)/delta);
99 else if(i<(min_opt[0]+0.75*delta)){
100 sEntry.c1=255*4*(i-min_opt[0]-0.5*delta)/delta;
104 sEntry.c2=255*(1+4*(min_opt[0]+0.75*delta-i)/delta);
108 colorTable.SetColorEntry(i,&sEntry);
109 if(output_opt.empty())
110 cout << i <<
" " << sEntry.c1 <<
" " << sEntry.c2 <<
" " << sEntry.c3 <<
" " << sEntry.c4 << endl;
114 short ncol=dim_opt[0];
116 if(legend_opt.size()){
120 nrow=max_opt[0]-min_opt[0]+1;
123 vector<string> pngOption;
125 pngOption.push_back(
"");
126 legendWriter.open(legend_opt[0],ncol,nrow,1,GDT_Byte,oformat_opt[0],option_opt);
127 if(colorTable_opt.size()){
128 if(colorTable_opt[0]!=
"none")
129 legendWriter.setColorTable(colorTable_opt[0]);
132 legendWriter.setColorTable(&colorTable);
133 if(legend_opt.size()){
134 for(
int irow=0;irow<legendWriter.nrOfRow();++irow){
135 vector<char> buffer(legendWriter.nrOfCol());
136 for(
int icol=0;icol<legendWriter.nrOfCol();++icol)
137 buffer[icol]=min_opt[0]+irow*static_cast<short>(max_opt[0]-min_opt[0]+1)/legendWriter.nrOfRow();
138 legendWriter.writeData(buffer,GDT_Byte,legendWriter.nrOfRow()-1-irow);
148 if(input_opt.size()&&output_opt.size()){
151 if(option_opt.findSubstring(
"INTERLEAVE=")==option_opt.end()){
152 string theInterleave=
"INTERLEAVE=";
153 theInterleave+=imgReader.getInterleave();
154 option_opt.push_back(theInterleave);
157 imgWriter.open(output_opt[0],imgReader.nrOfCol(),imgReader.nrOfRow(),1,GDT_Byte,oformat_opt[0],option_opt);
159 imgWriter.copyGeoTransform(imgReader);
160 if(colorTable_opt.size()){
161 if(colorTable_opt[0]!=
"none")
162 imgWriter.setColorTable(colorTable_opt[0]);
165 imgWriter.setColorTable(&colorTable);
166 if(description_opt.size())
167 imgWriter.setImageDescription(description_opt[0]);
168 switch(imgReader.getDataType()){
171 for(
int irow=0;irow<imgReader.nrOfRow();++irow){
172 imgReader.readData(buffer,GDT_Byte,irow);
173 imgWriter.writeData(buffer,GDT_Byte,irow);
178 vector<short> buffer;
179 cout <<
"Warning: copying short to unsigned short without conversion, use gdal_translate -scale if needed..." << endl;
180 for(
int irow=0;irow<imgReader.nrOfRow();++irow){
181 imgReader.readData(buffer,GDT_Int16,irow,0);
182 imgWriter.writeData(buffer,GDT_Int16,irow,0);
187 vector<unsigned short> buffer;
188 for(
int irow=0;irow<imgReader.nrOfRow();++irow){
189 imgReader.readData(buffer,GDT_UInt16,irow,0);
190 imgWriter.writeData(buffer,GDT_UInt16,irow,0);
195 cerr <<
"data type " << imgReader.getDataType() <<
" not supported for adding a colortable" << endl;
201 if(legend_opt.size())
202 legendWriter.close();