/[escript]/branches/diaplayground/ripley/src/Brick.cpp
ViewVC logotype

Diff of /branches/diaplayground/ripley/src/Brick.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4353 by caltinay, Fri Apr 5 00:14:35 2013 UTC revision 4357 by caltinay, Thu Apr 11 06:07:14 2013 UTC
# Line 479  void Brick::readBinaryGrid(escript::Data Line 479  void Brick::readBinaryGrid(escript::Data
479      f.close();      f.close();
480  }  }
481    
482  void Brick::writeBinaryGrid(const escript::Data& in, string filename, int byteOrder) const  void Brick::writeBinaryGrid(const escript::Data& in, string filename,
483                                int byteOrder, int dataType) const
484    {
485        // the mapping is not universally correct but should work on our
486        // supported platforms
487        switch (dataType) {
488            case DATATYPE_INT32:
489                writeBinaryGridImpl<int>(in, filename, byteOrder);
490                break;
491            case DATATYPE_FLOAT32:
492                writeBinaryGridImpl<float>(in, filename, byteOrder);
493                break;
494            case DATATYPE_FLOAT64:
495                writeBinaryGridImpl<double>(in, filename, byteOrder);
496                break;
497            default:
498                throw RipleyException("writeBinaryGrid(): invalid or unsupported datatype");
499        }
500    }
501    
502    template<typename ValueType>
503    void Brick::writeBinaryGridImpl(const escript::Data& in,
504                                    const string& filename, int byteOrder) const
505  {  {
506      // check function space and determine number of points      // check function space and determine number of points
507      int myN0, myN1, myN2;      int myN0, myN1, myN2;
# Line 504  void Brick::writeBinaryGrid(const escrip Line 526  void Brick::writeBinaryGrid(const escrip
526    
527      const int numComp = in.getDataPointSize();      const int numComp = in.getDataPointSize();
528      const int dpp = in.getNumDataPointsPerSample();      const int dpp = in.getNumDataPointsPerSample();
529      const int fileSize = sizeof(float)*numComp*dpp*totalN0*totalN1*totalN2;      const int fileSize = sizeof(ValueType)*numComp*dpp*totalN0*totalN1*totalN2;
530    
531      if (numComp > 1 || dpp > 1)      if (numComp > 1 || dpp > 1)
532          throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");          throw RipleyException("writeBinaryGrid(): only scalar, single-value data supported");
# Line 519  void Brick::writeBinaryGrid(const escrip Line 541  void Brick::writeBinaryGrid(const escrip
541      for (index_t z=0; z<myN2; z++) {      for (index_t z=0; z<myN2; z++) {
542          for (index_t y=0; y<myN1; y++) {          for (index_t y=0; y<myN1; y++) {
543              const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0              const int fileofs = (m_offset[0]+(m_offset[1]+y)*totalN0
544                                  +(m_offset[2]+z)*totalN0*totalN1)*sizeof(float);                                  +(m_offset[2]+z)*totalN0*totalN1)*sizeof(ValueType);
545              ostringstream oss;              ostringstream oss;
546    
547              for (index_t x=0; x<myN0; x++) {              for (index_t x=0; x<myN0; x++) {
548                  const double* sample = _in->getSampleDataRO(z*myN0*myN1+y*myN0+x);                  const double* sample = _in->getSampleDataRO(z*myN0*myN1+y*myN0+x);
549                  float fvalue = (float)(*sample);                  ValueType fvalue = static_cast<ValueType>(*sample);
550                  if (byteOrder == RIPLEY_BYTE_ORDER) {                  if (byteOrder == BYTEORDER_NATIVE) {
551                      oss.write((char*)&fvalue, sizeof(fvalue));                      oss.write((char*)&fvalue, sizeof(fvalue));
552                  } else {                  } else {
553                      char* value = reinterpret_cast<char*>(&fvalue);                      char* value = reinterpret_cast<char*>(&fvalue);
554                      oss.write(RIPLEY_BYTE_SWAP32(value), sizeof(fvalue));                      oss.write(byte_swap32(value), sizeof(fvalue));
555                  }                  }
556              }              }
557              fw->writeAt(oss, fileofs);              fw->writeAt(oss, fileofs);

Legend:
Removed from v.4353  
changed lines
  Added in v.4357

  ViewVC Help
Powered by ViewVC 1.1.26