File: fileio_raw.cpp

package info (click to toggle)
odin 2.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,196 kB
  • sloc: cpp: 62,638; sh: 4,541; makefile: 779
file content (98 lines) | stat: -rw-r--r-- 3,138 bytes parent folder | download | duplicates (6)
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
#include "fileio.h"
#include "complexdata.h"

//////////////////////////////////////////////////////////////

template<typename T>
struct RawFormat : public FileFormat {
  STD_string description() const {
    STD_string descr=TypeTraits::type2label((T)0);
    if(descr.find("bit")!=STD_string::npos) {
      descr=replaceStr(descr, "s", "signed ");
      descr=replaceStr(descr, "u", "unsigned ");
      descr=replaceStr(descr, "bit", " bit");
    }
    descr+=" raw data";
    return descr;
  }
  svector suffix() const  {
    svector result; result.resize(1);
    result[0]=TypeTraits::type2label((T)0);
    return result;
  }
  svector dialects() const {return svector();}

  int read(Data<float,4>& data, const STD_string& filename, const FileReadOpts& opts, Protocol& prot) {
    Log<FileIO> odinlog("RawFormat","read");
    TinyVector<int,4> newshape;
    newshape=1;

    unsigned long typesize=sizeof(T);
    if( int(opts.cplx)>0 ) typesize*=2;

    int fsize=filesize(filename.c_str())-opts.skip;

    newshape(0)=prot.seqpars.get_NumOfRepetitions();
    newshape(3)=prot.seqpars.get_MatrixSize(readDirection);
    newshape(2)=prot.seqpars.get_MatrixSize(phaseDirection);
    newshape(1)=(unsigned int)secureDivision(fsize,typesize*product(newshape));

    ODINLOG(odinlog,normalDebug) << "fsize/typesize/newshape(1)=" << fsize << "/" << typesize  << "/" << newshape(1) << STD_endl;

    if(!product(newshape)) {
      ODINLOG(odinlog,errorLog) << "wrong size: " << newshape << STD_endl;
      return -1;
    }
    data.resize(newshape);


    if(int(opts.cplx)>0 ) {
      ComplexData<4> data_cplx(newshape);
      if(data_cplx.Data<STD_complex,4>::read<T>(filename,opts.skip)<0) return -1; // extra scop for MinGW
      if(opts.cplx=="abs")  data=cabs(data_cplx);
      if(opts.cplx=="pha")  data=phase(data_cplx);
      if(opts.cplx=="real") data=creal(data_cplx);
      if(opts.cplx=="imag") data=cimag(data_cplx);
    } else {
      prot.system.set_data_type(TypeTraits::type2label((T)0));
      if(data.Data<float,4>::read<T>(filename,opts.skip)<0) return -1; // extra scop for MinGW
    }

    return data.extent(0)*data.extent(1);
  }

  int write(const Data<float,4>& data, const STD_string& filename, const FileWriteOpts& opts, const Protocol& prot) {
    Log<FileIO> odinlog("RawFormat","write");

    if(opts.append) {
      Data<T,4> data_copy;
      data.Data<float,4>::convert_to(data_copy,!opts.noscale);
      return data_copy.write(filename,appendMode);
    } else return data.Data<float,4>::write<T>(filename,!opts.noscale);
  }

};


//////////////////////////////////////////////////////////////

void register_raw_format() {
  static RawFormat<s8bit>  s8f;
  static RawFormat<u8bit>  u8f;
  static RawFormat<s16bit> s16f;
  static RawFormat<u16bit> u16f;
  static RawFormat<s32bit> s32f;
  static RawFormat<u32bit> u32f;
  static RawFormat<float>  floatf;
  static RawFormat<double> doublef;

  s8f.register_format();
  u8f.register_format();
  s16f.register_format();
  u16f.register_format();
  s32f.register_format();
  u32f.register_format();
  floatf.register_format();
  doublef.register_format();
}