| 1 |
|
| 2 |
|
| 3 |
def fun2method(inputfile, outputfile_basename, json_path): |
| 4 |
"""converts MIALib C function declarations into JIPLib C++ methods (outputfile_basename.cc file) and C++ method declarations (outputfile_basename.h file). Currently only convert desctuctive functions, i.e., ERROR_TYPE functions, with IMAGE * as first argument (IMAGE ** not yet taken into account). |
| 5 |
|
| 6 |
:param inputfile: string for input file containing extern declarations |
| 7 |
:param outputfile_basename: string for output file basename (i.e. without extension) |
| 8 |
:returns: True on success, False otherwise |
| 9 |
:rtype: |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
import re |
| 14 |
import json |
| 15 |
import sys |
| 16 |
import os |
| 17 |
with open(json_path, 'r') as f: |
| 18 |
old2newDic = json.load(f) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
ifp=open(inputfile, 'r') |
| 26 |
|
| 27 |
f = open(outputfile_basename+'.cc', 'w') |
| 28 |
fh = open(outputfile_basename+'.h', 'w') |
| 29 |
|
| 30 |
|
| 31 |
lines=ifp.readlines() |
| 32 |
|
| 33 |
for line in lines: |
| 34 |
print(line) |
| 35 |
|
| 36 |
name=re.match(r'extern ERROR_TYPE (.*)\((.*)\);',line) |
| 37 |
args=re.split(',', name.group(2)) |
| 38 |
re.sub('\**', '', re.sub('.* ', '', args[0])) |
| 39 |
|
| 40 |
arglist = [] |
| 41 |
|
| 42 |
for i in args: |
| 43 |
atype=re.match(' *(.* \**)', i).group(1) |
| 44 |
aname=re.sub('\**', '', re.sub('.* ', '', i)) |
| 45 |
arglist.append([atype, aname]) |
| 46 |
|
| 47 |
|
| 48 |
a= { "name" : name.group(1), |
| 49 |
"arguments" : arglist } |
| 50 |
|
| 51 |
print(a) |
| 52 |
|
| 53 |
imDeclare=[] |
| 54 |
imRasterArray=[] |
| 55 |
|
| 56 |
GTDeclare=[] |
| 57 |
GTVars=[] |
| 58 |
|
| 59 |
GDTypes = ['GDT_Byte', 'GDT_Int16', 'GDT_UInt16', 'GDT_Int32', 'GDT_UInt32', 'GDT_Float32', 'GDT_Float64'] |
| 60 |
MIATypes = ['uc_', 's_', 'us_', 'i32_', 'u32_', 'f_', 'd_'] |
| 61 |
CTypes = ['unsigned char', 'short int', 'unsigned short int', 'int', 'unsigned int', 'float', 'double'] |
| 62 |
|
| 63 |
methodDeclaration='std::shared_ptr<Jim> Jim::'+old2newDic.get(a.get("name"))+'(' |
| 64 |
print(methodDeclaration) |
| 65 |
|
| 66 |
cSeparator=', ' |
| 67 |
separator = '' |
| 68 |
|
| 69 |
if a.get("arguments")[0][0] != 'IMAGE *': |
| 70 |
print('WARNING: ',line,' does not have IMAGE * as first argument') |
| 71 |
continue |
| 72 |
else: |
| 73 |
imDeclare.append(a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+'=this->getMIA(iband);') |
| 74 |
cCall=a.get("arguments")[0][1] |
| 75 |
|
| 76 |
imCount=1 |
| 77 |
for idx, arg in enumerate(a.get("arguments")[1:len(a.get("arguments"))]): |
| 78 |
print(methodDeclaration) |
| 79 |
print(idx) |
| 80 |
print(idx) |
| 81 |
print(separator) |
| 82 |
print(arg) |
| 83 |
print(arg[0]) |
| 84 |
|
| 85 |
if (arg[0]=='IMAGE *'): |
| 86 |
methodDeclaration+=separator+'Jim& imRaster_'+arg[1] |
| 87 |
imRasterArray.append('imRaster_'+arg[1]) |
| 88 |
imDeclare.append(arg[0]+' '+arg[1]+'=imRaster_'+arg[1]+'.getMIA(iband);') |
| 89 |
elif (arg[0]=='G_TYPE '): |
| 90 |
GTDeclare.append(arg[0]+' '+arg[1]+';') |
| 91 |
GTVars.append(arg[1]) |
| 92 |
methodDeclaration+=separator+'double '+'d_'+arg[1] |
| 93 |
else: |
| 94 |
methodDeclaration+=separator+arg[0]+' '+arg[1] |
| 95 |
|
| 96 |
cCall+=cSeparator+arg[1] |
| 97 |
separator=', ' |
| 98 |
|
| 99 |
|
| 100 |
fh.write(re.sub(r'Jim::','',methodDeclaration+separator+'int band=-1);\n')) |
| 101 |
|
| 102 |
|
| 103 |
methodDeclaration+=separator+'int band)' |
| 104 |
|
| 105 |
f.write(methodDeclaration+'{') |
| 106 |
f.write('\n\ttry{') |
| 107 |
f.write('\n\t\tif(nrOfBand()<=band){') |
| 108 |
f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";') |
| 109 |
f.write('\n\t\t\tthrow(errorString);') |
| 110 |
f.write('\n\t\t}') |
| 111 |
for i in imRasterArray: |
| 112 |
f.write('\n\t\tif('+i+'.nrOfBand()<=band){') |
| 113 |
f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";') |
| 114 |
f.write('\n\t\t\tthrow(errorString);') |
| 115 |
f.write('\n\t\t}') |
| 116 |
|
| 117 |
f.write('\n\t\t//make a copy of this') |
| 118 |
f.write('\n\t\tstd::shared_ptr<Jim> copyImg=this->clone();') |
| 119 |
f.write('\n\t\tstd::vector<unsigned int> bands;') |
| 120 |
f.write('\n\t\tif(band<0){') |
| 121 |
f.write('\n\t\t\tfor(unsigned int iband=0;iband<nrOfBand();++iband)') |
| 122 |
f.write('\n\t\t\t\tbands.push_back(iband);') |
| 123 |
f.write('\n\t\t}') |
| 124 |
f.write('\n\t\telse') |
| 125 |
f.write('\n\t\t\tbands.push_back(band);') |
| 126 |
f.write('\n\t\tfor(std::vector<unsigned int>::const_iterator bit=bands.begin();bit!=bands.end();++bit){') |
| 127 |
f.write('\n\t\t\tunsigned int iband=*bit;') |
| 128 |
|
| 129 |
f.write('\n\t\t\t'+a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+' = 0;') |
| 130 |
llen=len(imDeclare) |
| 131 |
for i in range(1,llen): |
| 132 |
f.write('\n\t\t\t'+imDeclare[i]) |
| 133 |
for i in GTDeclare: |
| 134 |
f.write('\n\t\t\t'+i) |
| 135 |
|
| 136 |
if len(GTDeclare)>0: |
| 137 |
f.write('\n\t\t\tswitch(getDataType()){') |
| 138 |
for idx, GDType in enumerate(GDTypes): |
| 139 |
f.write('\n\t\t\tcase('+GDType+'):') |
| 140 |
for var_idx, GTVar in enumerate(GTVars): |
| 141 |
f.write('\n\t\t\t\t'+GTVar+'.'+MIATypes[idx]+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');') |
| 142 |
if (GDType=='GDT_Byte'): |
| 143 |
f.write('\n\t\t\t\t'+GTVar+'.'+'generic_'+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');') |
| 144 |
f.write('\n\t\t\t\tbreak;') |
| 145 |
f.write(''' |
| 146 |
\t\t\tdefault: |
| 147 |
\t\t\t\tstd::string errorString="Error: data type not supported"; |
| 148 |
\t\t\t\tthrow(errorString); |
| 149 |
\t\t\t\tbreak; |
| 150 |
\t\t\t}''') |
| 151 |
f.write('\n\t\t\t'+a.get("arguments")[0][1]+'=copyImg->getMIA(iband);') |
| 152 |
|
| 153 |
f.write('\n\t\t\tif(::'+a.get("name")+'('+cCall+') == NO_ERROR){') |
| 154 |
f.write('\n\t\t\t\tcopyImg->setMIA(iband);') |
| 155 |
for i in imRasterArray: |
| 156 |
f.write('\n\t\t\t\t'+i+'.setMIA(iband);') |
| 157 |
f.write('\n\t\t\t}') |
| 158 |
|
| 159 |
f.write('\n\t\t\telse{') |
| 160 |
f.write('\n\t\t\t\tcopyImg->setMIA(iband);') |
| 161 |
for i in imRasterArray: |
| 162 |
f.write('\n\t\t\t\t'+i+'.setMIA(iband);') |
| 163 |
f.write('\n\t\t\t\tstd::string errorString="Error: '+a.get("name")+'() function in MIA failed, returning NULL pointer";') |
| 164 |
f.write('\n\t\t\t\tthrow(errorString);') |
| 165 |
f.write('\n\t\t\t}') |
| 166 |
|
| 167 |
f.write('\n\t\t}') |
| 168 |
f.write('\n\t\treturn(copyImg);') |
| 169 |
f.write('\n\t}') |
| 170 |
|
| 171 |
f.write('\n\tcatch(std::string errorString){') |
| 172 |
f.write('\n\t\tstd::cerr << errorString << std::endl;') |
| 173 |
f.write('\n\tthrow;') |
| 174 |
f.write('\n\t}') |
| 175 |
f.write('\n\tcatch(...){') |
| 176 |
f.write('\n\t\tthrow;') |
| 177 |
f.write('\n\t}') |
| 178 |
f.write('\n}\n') |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
ifp.close() |
| 190 |
|
| 191 |
f.close() |
| 192 |
fh.close() |
| 193 |
|
| 194 |
return(True) |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
import sys, getopt |
| 201 |
|
| 202 |
def main(argv): |
| 203 |
inputfile="miallib_errortype" |
| 204 |
outputfile="fun2method_errortype" |
| 205 |
json_path = "old2NewNames.json" |
| 206 |
try: |
| 207 |
opts, args = getopt.getopt(argv,"hi:o:j:",["ifile=","ofile=", "json="]) |
| 208 |
except getopt.GetoptError: |
| 209 |
print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>') |
| 210 |
sys.exit(2) |
| 211 |
for opt, arg in opts: |
| 212 |
if opt == '-h': |
| 213 |
print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>') |
| 214 |
sys.exit() |
| 215 |
elif opt in ("-i", "--ifile"): |
| 216 |
inputfile = arg |
| 217 |
elif opt in ("-o", "--ofile"): |
| 218 |
outputfile = arg |
| 219 |
elif opt in ("-j", "--json"): |
| 220 |
json_path = arg |
| 221 |
print('Input file is "', inputfile) |
| 222 |
print('Output file is "', outputfile) |
| 223 |
print('JSON file is "', json_path) |
| 224 |
|
| 225 |
fun2method(inputfile, outputfile, json_path) |
| 226 |
|
| 227 |
if __name__ == "__main__": |
| 228 |
main(sys.argv[1:]) |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|