summaryrefslogtreecommitdiffstats
path: root/rasodmg/test/defconv.cc
blob: feee2dceb2ed0ef2d4ec4a5262da59119ee47c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 *  defconv.cc
 *
  *  Covert the DEF source file to another DEF destination file.
 *  Returns 0 for succes, otherwise 1.
 *
 *  Parameters:
 *
 *  --inputfilename <src-file>
 *     the filename of the src file, mandatory
 *  --outputfilename <dest-file>
 *     the filename of the dest file, mandatory
 *
 *  --inputformat name
 *     name of DEF fromat(see data_foramat from mddtypes.hh)
 *  --inputformatparams params
 *     params used by convertFrom methods(see the heder file for you DEF from conversion)
 *  --outputformat name
 *     name of DEF fromat(see data_foramat from mddtypes.hh)
 *  --outputformatparams params
 *     params used by convertFrom methods(see the heder file for you DEF from conversion)
 *
 *  --help
 *     print usage and exit
 */

#ifdef EARLY_TEMPLATE
#define __EXECUTABLE__
#ifdef __GNUG__
#include "raslib/template_inst.hh"
#endif
#endif

#include "defutil.hh"
#include "raslib/miter.hh"

//program data 
const char* paramPrgHelp="--help";
bool defPrgHelp=false;
char* prgHelp=NULL;

const char* paramSrcFileName="--srcfilename";
bool defSrcFileName=false;
char* srcFileName=NULL;

const char* paramSrcFormat="--srcformat";
bool defSrcFormat=false;
char* srcFormatc=NULL;
r_Data_Format srcFormat=r_Array;
char *srcIvc=NULL, *srcTypec=NULL;
r_Minterval srcIv;
r_Type* srcType=NULL;

const char* paramSrcFormatParams="--srcformatparams";
bool defSrcFormatParams=false;
char *srcFormatParams=NULL;

const char* paramDestFileName="--destfilename";
bool defDestFileName=false;
char *destFileName=NULL;

const char* paramDestFormat="--destformat";
bool defDestFormat=false;
char* destFormatc=NULL;
r_Data_Format destFormat=r_Array;
char *destIvc=NULL, *destTypec=NULL;
r_Minterval destIv;
r_Type* destType=NULL;

const char* paramDestFormatParams="--destformatparams";
bool defDestFormatParams=false;
char *destFormatParams=NULL;

//data used to parase formatparams for format Array
const char* fpDomain="domain";
const char* fpType="type";

//structures used for parseParam
const int paramsNo=7;

const char *paramsName[paramsNo]={ paramPrgHelp, paramSrcFileName, paramSrcFormat, paramSrcFormatParams, 
					paramDestFileName, paramDestFormat, paramDestFormatParams};
					
bool *paramsPresent[paramsNo]={ &defPrgHelp, &defSrcFileName, &defSrcFormat, &defSrcFormatParams,
							   &defDestFileName, &defDestFormat, &defDestFormatParams};
							   
char **paramsValue[paramsNo]={ &prgHelp, &srcFileName, &srcFormatc, &srcFormatParams,
							   &destFileName, &destFormatc, &destFormatParams};							   

void printStatus()
{
 cout << "defdiff parameters list:" << endl;
 for (int i=0; i<paramsNo; i++)
   cout << "--Name='" << paramsName[i] 
        << "' Present=" << ((*paramsPresent[i])? "true": "false") 
        << " Value='" << *paramsValue[i]  << "'" <<  endl;
}

void printUsage()
{
  char* fileNameSrc="test.bmp";
  char* fileNameDest="test.tiff";
  
  cout << "defconv v 0.1 - RasDaMan Data Exchange Format Convertor Utility" << endl;
  cout << "Description: Returns " << EXIT_SUCCESS << " for succes, otherwise " << EXIT_FAILURE << endl;
  cout << "For " << paramSrcFormat << "/" << paramDestFormat << " Array you have to specify the "  
       << paramSrcFormatParams << "/" << paramDestFormatParams << " as following:" << endl;
  cout << "\"" << fpDomain << "=<domain_str>," << fpType << "=<type_str>\" (e.g. \"" 
       << fpDomain << "=\"[0:477,0:464]\"," << fpType << "=char\")" << endl;
  cout << "Usage options:" << endl;
  cout << "        " << paramSrcFileName << " <src-file>        ... the filename used as src mandatory" << endl;
  cout << "        " << paramSrcFormat << " name                ... name of DEF fromat(see data_foramat from mddtypes.hh)," << endl;
  cout << "                                              default Array" << endl;
  cout << "        " << paramSrcFormatParams << " params        ... params used by convertFrom methods(see the header file" << endl;
  cout << "                                              for you DEF from conversion), default NULL" << endl;
  cout << "        " << paramDestFileName << " <dest-file>      ... the filename used as dest mandatory" << endl; 
  cout << "        " << paramDestFormat << " name               ... name of DEF fromat(see data_foramat from mddtypes.hh)," << endl;
  cout << "                                              default Array" << endl;
  cout << "        " << paramDestFormatParams << " params        ... params used by convertFrom methods(see the header file" << endl;
  cout << "                                              for you DEF from conversion), default NULL" << endl;
  cout << "        " << paramPrgHelp << "             ... this help" << endl;
  cout << "For example:" << endl;
  cout << "defconv " << paramSrcFileName << " " << fileNameSrc << " " << paramSrcFormat << " " << format_name_bmp <<" " 
  	   << paramDestFileName << " " << fileNameDest  << " " << paramDestFormat <<" " << format_name_tiff << endl;
  cout << "Report bugs to liviu.coman@active­knowledge.com" << endl;
  
}

int checkParam(int& paramIndex, char** param)
{
 int i=0;
 while(i<paramsNo)
 {
  if(!strcmp(param[paramIndex], paramsName[i]))
   break;
  i++;
 }
   
 if(i==paramsNo)
 {
   cout << "checkParam(...) error processing parameters: parameter " 
   		<< param[paramIndex] << " is unknown!" << endl;  
   return EXIT_FAILURE;
 }
 if(*paramsPresent[i])
 {
   cout << "checkParam(...) error processing parameters: parameter " 
   		<< param[paramIndex] << " is present more than once!" << endl;  
   return EXIT_FAILURE;
 }
 
 if(paramsName[i]!=paramPrgHelp)
 {
  *paramsPresent[i]=true;
  paramIndex++;
  *paramsValue[i]=param[paramIndex];
 }
 else
  *paramsPresent[i]=true;
 
 return EXIT_SUCCESS;
}

int parseParams(int argc, char** argv)
{
 int argIndex=0;
 if (argc==1)
 {
   cout << "parseParams(...) parameters " << paramSrcFileName << " <src-file> and " 
        << paramDestFileName << " <dest-file> are mandatatory!" << endl;   
   return EXIT_FAILURE;
 }
 else
 {
   argIndex=1;
   while(argIndex<argc)
   {
    if(checkParam(argIndex, argv)!=EXIT_SUCCESS)
      break;
    argIndex++;
   }
   if(argIndex<argc)
   {
    //error while parsing parameters
    return EXIT_FAILURE; 
   }

   //check rule	  
   if((defSrcFileName && defDestFileName) || (defPrgHelp))
     return EXIT_SUCCESS;
   else
   {
     cout << "parseParams(...) parameters " << paramSrcFileName << " <src-file> and " 
          << paramDestFileName << " <dest-file> are mandatatory!" << endl;
     return EXIT_FAILURE; 
   }
 }
}

int validateParams()
{
 
 //check srcformat
 if(defSrcFormat)
 {
  srcFormat = get_data_format_from_name(srcFormatc);
  if((srcFormat!=r_Array) &&
     (!r_Convertor_Factory::is_supported(srcFormat)))
    {
 	 cout << "convertDEFs(...) conversion " << srcFormatc << " not supported" << endl;
     return EXIT_FAILURE;
    }
 }
 
 //check destformat
 if(defDestFormat)
 {
    destFormat = get_data_format_from_name(destFormatc);
    if((destFormat!=r_Array) &&
       (!r_Convertor_Factory::is_supported(destFormat)))
    {
     cout << "convertDEFs(...) conversion " << destFormatc << " not supported" << endl;
     return EXIT_FAILURE;
    }
 }
 
 //check srcFormatParams if srcFormat=r_Array
 if(srcFormat == r_Array)
 {
  if(!defSrcFormatParams)
  {
   cout << "convertDEFs(...) for srcformat r_Array is necessary to provide the srcformatparams as described in help" << endl;
   return EXIT_FAILURE;
  }
  
  if(parseArrayParams((const char*)srcFormatParams, fpDomain, fpType, srcIvc, srcTypec) != EXIT_SUCCESS)
   return EXIT_FAILURE;   
  
  if(decodeMinterval(srcIvc, srcIv) != EXIT_SUCCESS)
   return EXIT_FAILURE; 
   
  if(decodeType(srcTypec, srcType) != EXIT_SUCCESS)
   return EXIT_FAILURE;    
 }
 
 //check destFormatParams if destFormat=r_Array 
 if(destFormat == r_Array)
 {
  if(!defDestFormatParams)
  {
   cout << "convertDEFs(...) for destformat r_Array is necessary to provide the srcformatparams as described in help" << endl; 
   return EXIT_FAILURE;
  }

  if(parseArrayParams((const char*)destFormatParams, fpDomain, fpType, destIvc, destTypec) != EXIT_SUCCESS)
   return EXIT_FAILURE;   
  
  if(decodeMinterval(destIvc, destIv) != EXIT_SUCCESS)
   return EXIT_FAILURE; 
   
  if(decodeType(destTypec, destType) != EXIT_SUCCESS)
   return EXIT_FAILURE;      
 }
}

int setCell(const char* srcCell, 
	    const r_Type* srcType,
	    const r_Type* destType, 
	    char* destCell,
	    bool rescale=false)
{
 r_Double srcTypeMin=0., srcTypeMax=0.;
 r_Double destTypeMin=0., destTypeMax=0.; 
 r_Double srcVal=0., destVal=0.;
 
 if(!srcCell)
 {
  cout << "setCell(...) srcCell is null" << endl;
  return EXIT_FAILURE;
 }
 if(!srcType)
 {
  cout << "setCell(...) srcType is null" << endl;
  return EXIT_FAILURE;
 } 
 if(!destType)
 {
  cout << "setCell(...) destType is null" << endl;
  return EXIT_FAILURE;
 } 
 if(!destCell)
 {
  cout << "setCell(...) destCell is null" << endl;
  return EXIT_FAILURE;
 } 
 
 try
 {
  srcVal=((r_Primitive_Type*)srcType)->get_value(srcCell);
  if(rescale)
  {
    ((r_Primitive_Type*)srcType)->get_limits(srcTypeMin, srcTypeMax);
    ((r_Primitive_Type*)destType)->get_limits(destTypeMin, destTypeMax);
    destVal=srcVal*destTypeMax/srcTypeMax;
  }
  else
    destVal=srcVal;
  ((r_Primitive_Type*)destType)->set_value(destCell,destVal);
 }
 catch(r_Error& err)
 {
  cout << "setCell(...) error while setting value to dest from src" << endl;
  cout << "Error " << err.get_errorno() << " : " << err.what() << endl;
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}
int convertData(const r_convDesc& descSrc, r_convDesc& descDest)
{
 r_Minterval srcIv, destIv, commonIv;

 r_Bytes srcTypeSize=0, destTypeSize=0;
 char *srcBuffer=NULL, *destBuffer=NULL;

 if(!descSrc.dest)
 {
  cout << "convertData(...) descSrc.dest is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(!descSrc.destType)
 {
  cout << "convertData(...) descSrc.destType is null" << endl;
  return EXIT_FAILURE;
 } 
 
 if(descDest.dest)
 {
  cout << "convertData(...) descDest.dest is not null" << endl;
  return EXIT_FAILURE;
 }
 
 if(!descDest.destType)
 {
  cout << "convertData(...) descDest.destType is null" << endl;
  return EXIT_FAILURE;
 }  
 
 //translate every interval to zero
 try
 {
  srcIv=descSrc.destInterv.create_reverse_translation(descSrc.destInterv.get_origin());
  destIv=descDest.destInterv.create_reverse_translation(descDest.destInterv.get_origin()); 
 }
 catch(r_Error& err)
 {
  cout << "convertData(...) error while translating to zero the following intervals:" << endl;
  cout << "descSrc.destInterv=" << descSrc.destInterv << endl;
  cout << "descDest.destInterv=" << descDest.destInterv << endl;
  cout << "Error " << err.get_errorno() << ":" << err.what() << endl;
  return EXIT_FAILURE;
 }
 
 //check intersection
 try
 {
  commonIv.intersection_of(srcIv, destIv);
 }
 catch(r_Error& err)
 {
  cout << "convertData(...) error while doing the intersection of the following intervals:" << endl;
  cout << "descSrc.destInterv=" << descSrc.destInterv << endl;
  cout << "descDest.destInterv=" << descDest.destInterv << endl;
  cout << "Error " << err.get_errorno() << ":" << err.what() << endl;
  return EXIT_FAILURE;
 }
 
 //get type size
 srcTypeSize=((r_Base_Type*)descSrc.destType)->size();
 destTypeSize=((r_Base_Type*)descDest.destType)->size(); 
 
 //allocate data
 srcBuffer=new char[srcTypeSize];
 if(!srcBuffer)
 {
  cout << "convertData(...) unable to claim memory for srcBuffer" << endl;
  return EXIT_FAILURE;
 } 
 destBuffer=new char[destTypeSize];
 if(!destBuffer)
 {
  cout << "convertData(...) unable to claim memory for destBuffer" << endl;
  delete[] srcBuffer;
  srcBuffer=NULL;
  return EXIT_FAILURE;
 }  
 descDest.dest=new char[descDest.destInterv.cell_count()*destTypeSize];
 if(!descDest.dest)
 {
  cout << "convertData(...) unable to claim memory for descDest.dest" << endl;
  delete[] srcBuffer;
  srcBuffer=NULL;
  delete[] destBuffer;
  destBuffer=NULL;  
  return EXIT_FAILURE;
 }
 memset(descDest.dest,0, descDest.destInterv.cell_count()*destTypeSize);
 
 r_Miter srcIter( &commonIv, &srcIv, srcTypeSize, descSrc.dest );
 r_Miter destIter( &commonIv, &destIv, destTypeSize, descDest.dest );
 while(!srcIter.isDone() || !destIter.isDone())
 {
  memcpy(srcBuffer, srcIter.nextCell(), srcTypeSize);
  if(setCell(srcBuffer, descSrc.destType, descDest.destType, destBuffer)!=EXIT_SUCCESS)
  {
   //abort algorithm
   break;
  }
  memcpy(destIter.nextCell(), destBuffer, destTypeSize);
 }
 
 //clean up
 delete[] srcBuffer;
 srcBuffer=NULL;
 delete[] destBuffer;
 destBuffer=NULL;  
 
 //check algorithm
 if(!srcIter.isDone() || !destIter.isDone())
 {
  cout << "convertData(...) error in algorithm" << endl;
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}



int convertDEFs()
{
 char *dataSrc=NULL, *dataDest=NULL;
 r_ULong dataSrcSize=0, dataDestSize=0;
 r_convDesc descSrc, descDest;
 
 initConvDesc(descSrc);
 initConvDesc(descDest);
 
 //read the source file
 if(readFile(srcFileName, &dataSrc, dataSrcSize)!=EXIT_SUCCESS)
   return EXIT_FAILURE;
 
 if(!dataSrcSize)
 {
   cout << "convertDEFs() size of file " << srcFileName << " is zero" << endl;
   //clean up 
   delete [] dataSrc;
   dataSrc=NULL;
   return EXIT_FAILURE;  
 }
 
 //what is the input format?   
 if(srcFormat !=r_Array)
 {
   //it is safe to use srcType, srcIv because the srcFormat is not r_Array
   srcType=r_Convertor::get_external_type(r_Convertor::ctype_char);
   srcIv  = r_Minterval(1);
   srcIv << r_Sinterval((r_Long)0, (r_Long)(dataSrcSize - 1));
  
  //did conversion work?
  if(convertFrom(srcFormat, srcFormatParams, dataSrc, 
    	         srcIv, srcType, descSrc) != EXIT_SUCCESS)
  {
   //clean up 
   delete [] dataSrc;
   dataSrc=NULL;
   return EXIT_FAILURE;
  }
 }
 else
 {
  descSrc.dest=dataSrc;
  descSrc.destInterv=srcIv;
  descSrc.destType=srcType;
 }
 
 //what is the output format?
 if(destFormat !=r_Array)
 {
  if(convertTo(destFormat, destFormatParams, descSrc.dest, descSrc.destInterv, descSrc.destType, descDest) != EXIT_SUCCESS)
  {
   //clean up  
   cleanConvDesc(descSrc);
   return EXIT_FAILURE;
  }
   //prepare the data for writing
   dataDest=descDest.dest;
   dataDestSize=descDest.destInterv.cell_count() * ((r_Base_Type*)descDest.destType)->size();  
 }
 else
 {
  //prepare for conversion
  descDest.destType=destType;
  descDest.destInterv=destIv;
  descDest.dest=NULL;
  
  if(descSrc.destType->isPrimitiveType())
  {
   if(convertData(descSrc, descDest) != EXIT_SUCCESS)
   {
    //clean up
    cleanConvDesc(descSrc);
    cleanConvDesc(descDest);   
    return EXIT_FAILURE;   
   }
   
   //prepare the data for writing
   dataDest=descDest.dest;
   dataDestSize=descDest.destInterv.cell_count() * ((r_Base_Type*)descDest.destType)->size();
  }
  else
  {
   //we store the data as it is
   dataDest=descSrc.dest;
   dataDestSize=descSrc.destInterv.cell_count() * ((r_Base_Type*)descSrc.destType)->size();   
  }
   
 }
 

 
 //write data into destFileName
 if(writeFile(destFileName, (const char**)&dataDest, dataDestSize)!=EXIT_SUCCESS)
 {
   //clean up
   cleanConvDesc(descSrc);
   cleanConvDesc(descDest);   
   return EXIT_FAILURE;
 } 
 
 //clean up
 cleanConvDesc(descSrc);
 cleanConvDesc(descDest);
   
 return EXIT_SUCCESS;
}

int processRequest()
{
 if(validateParams() != EXIT_SUCCESS)
  return EXIT_FAILURE;
 return convertDEFs();
}

int main(int argc, char **argv)
{
 if(parseParams(argc, argv) != EXIT_SUCCESS)
 {
  printUsage();
  return EXIT_FAILURE;
 }
 else
 {
  if(!defPrgHelp)
  {
   printStatus();
   return processRequest();
  }
  else
  {
   printUsage();
   return EXIT_SUCCESS;
  }
 }
}