Codebase list ciftilib / 964be1e
add NiftiHeader functions for timestep and description Tim Coalson 5 years ago
2 changed file(s) with 43 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
239239 ret[1][3] = m_header.qoffset_y;
240240 ret[2][3] = m_header.qoffset_z;
241241 } else {
242 cerr << "found quaternion with length greater than 1 in nifti header" << endl;
242 cerr << "warning: found quaternion with length greater than 1 in nifti header, using ANALYZE coordinates!" << endl;
243243 ret[0][0] = m_header.pixdim[1];
244244 ret[1][1] = m_header.pixdim[2];
245245 ret[2][2] = m_header.pixdim[3];
246246 }
247247 } else {//fall back to analyze and complain
248 cerr << "no sform or qform code found, using ANALYZE coordinates!" << endl;
248 cerr << "warning: no sform or qform code found, using ANALYZE coordinates!" << endl;
249249 ret[0][0] = m_header.pixdim[1];
250250 ret[1][1] = m_header.pixdim[2];
251251 ret[2][2] = m_header.pixdim[3];
265265 case NIFTI_UNITS_MM:
266266 break;
267267 default:
268 cerr << "unrecognized spatial unit in nifti header" << endl;
268 cerr << "warning: unrecognized spatial unit in nifti header" << endl;
269269 }
270270 return ret.getMatrix();
271 }
272
273 double NiftiHeader::getTimeStep() const
274 {
275 int timeUnit = XYZT_TO_TIME(m_header.xyzt_units);
276 double ret = m_header.pixdim[4];
277 switch (timeUnit)
278 {
279 case NIFTI_UNITS_USEC:
280 ret /= 1000000.0;
281 break;
282 case NIFTI_UNITS_MSEC:
283 ret /= 1000.0;
284 break;
285 default:
286 cerr << "warning: non-time units code " + AString_number(timeUnit) + " used in nifti header, pretending units are seconds" << endl;
287 case NIFTI_UNITS_SEC:
288 break;
289 }
290 return ret;
271291 }
272292
273293 AString NiftiHeader::toString() const
387407 for (; i < 16; ++i) m_header.intent_name[i] = '\0';
388408 }
389409
410 void NiftiHeader::setDescription(const char descrip[80])
411 {
412 int i;//custom strncpy-like code to fill nulls to the end
413 for (i = 0; i < 80 && descrip[i] != '\0'; ++i) m_header.descrip[i] = descrip[i];
414 for (; i < 80; ++i) m_header.descrip[i] = '\0';
415 }
416
390417 void NiftiHeader::setSForm(const vector<vector<float> >& sForm)
391418 {
392419 CiftiAssert(sForm.size() >= 3);//programmer error to pass badly sized matrix
396423 CiftiAssert(sForm[i].size() >= 4);//ditto
397424 if (sForm[i].size() < 4) throw CiftiException("internal error: setSForm matrix badly sized");
398425 }
399 m_header.xyzt_units = SPACE_TIME_TO_XYZT(NIFTI_UNITS_MM, NIFTI_UNITS_SEC);//overwrite whatever units we read in
426 int timeUnit = XYZT_TO_TIME(m_header.xyzt_units);
427 m_header.xyzt_units = SPACE_TIME_TO_XYZT(NIFTI_UNITS_MM, timeUnit);//overwrite whatever spatial unit we read in
400428 for (int i = 0; i < 4; i++)
401429 {
402430 m_header.srow_x[i] = sForm[0][i];
443471 m_header.qoffset_y = sForm[1][3];
444472 m_header.qoffset_z = sForm[2][3];
445473 }
474 }
475
476 void NiftiHeader::setTimeStep(const double& seconds)
477 {
478 int spaceUnit = XYZT_TO_SPACE(m_header.xyzt_units);//save the current space units so we don't clobber it...
479 m_header.xyzt_units = SPACE_TIME_TO_XYZT(spaceUnit, NIFTI_UNITS_SEC);//overwrite the time part of the units with seconds
480 m_header.pixdim[4] = seconds;
446481 }
447482
448483 void NiftiHeader::clearDataScaling()
5757
5858 std::vector<int64_t> getDimensions() const;
5959 std::vector<std::vector<float> > getSForm() const;
60 double getTimeStep() const;//seconds
6061 int64_t getDataOffset() const { return m_header.vox_offset; }
6162 int16_t getDataType() const { return m_header.datatype; }
6263 int32_t getIntentCode() const { return m_header.intent_code; }
6364 const char* getIntentName() const { return m_header.intent_name; }//NOTE: 16 BYTES, MAY NOT HAVE A NULL TERMINATOR
65 const char* getDescription() const { return m_header.descrip; }//NOTE: 80 BYTES, MAY NOT HAVE A NULL TERMINATOR
6466 bool getDataScaling(double& mult, double& offset) const;//returns false if scaling not needed
6567 int getNumComponents() const;
6668 AString toString() const;
6769
6870 void setDimensions(const std::vector<int64_t>& dimsIn);
6971 void setSForm(const std::vector<std::vector<float> > &sForm);
72 void setTimeStep(const double& seconds);
7073 void setIntent(const int32_t& code, const char name[16]);
74 void setDescription(const char descrip[80]);
7175 void setDataType(const int16_t& type);
7276 void clearDataScaling();
7377 void setDataScaling(const double& mult, const double& offset);