summaryrefslogtreecommitdiffstats
path: root/fitsy++/nrrd.C
blob: 867f5d4eb94ad0f04dd75e77eef7b0d8d4b90fff (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
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "nrrd.h"
#include "head.h"
#include "util.h"

FitsNRRD::FitsNRRD(FitsFile* fits)
{
  byteswap_ = fits->byteswap();
  endian_ = fits->endian();

  pBitpix_ = fits->pBitpix();
  pWidth_ = fits->pWidth();
  pHeight_ = fits->pHeight();
  pDepth_ = fits->pDepth();

  size_ = (size_t)pWidth_*pHeight_*pDepth_;
}

FitsNRRD::~FitsNRRD()
{
  if (data_)
    delete [] (char*)data_;
}

int FitsNRRD::initHeader(FitsFile* fits) 
{
  // simple check
  if (!pWidth_ || !pHeight_ || !pBitpix_)
    return 0;

  // create header
  head_ = new FitsHead(pWidth_, pHeight_, pDepth_, pBitpix_);
  if (!head_->isValid())
    return 0;

  // other
  primary_ = fits->primary();
  managePrimary_ = 0;

  inherit_ = head_->inherit();

  return 1;
}

template<class T> FitsNRRDm<T>::FitsNRRDm(FitsFile* fits) 
  : FitsNRRD(fits)
{
}

template <class T> void FitsNRRDm<T>::uncompress(FitsFile* fits)
{
  if (!initHeader(fits))
    return;

  T* dest = new T[size_];
  if (!dest) {
    internalError("Fitsy++ nrrd unable to allocate memory");
    return;
  }
  memset(dest, 0, size_*sizeof(T));
  compressed(dest, (char*)fits->data(), fits->dataSize()-fits->dataSkip());

  data_ = dest;

  dataSize_ = size_;
  dataSkip_ = 0;

  // all done
  valid_ = 1;
}

template class FitsNRRDm<unsigned char>;
template class FitsNRRDm<short>;
template class FitsNRRDm<unsigned short>;
template class FitsNRRDm<int>;
template class FitsNRRDm<long long>;
template class FitsNRRDm<float>;
template class FitsNRRDm<double>;