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

#include "envi.h"
#include "head.h"

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

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

  pEncoding_ = FitsFile::BSQ;

  pCRPIX3_ = fits->pCRPIX3();
  pCRVAL3_ = fits->pCRVAL3();
  pCDELT3_ = fits->pCDELT3();

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

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

int FitsENVI::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;

  // WCS?
  if (pCRPIX3_>0 || pCRVAL3_>0 || pCDELT3_!=1) {
    head_->appendString("CTYPE1","LINEAR", NULL);
    head_->appendReal("CRPIX1",1, 9, NULL);
    head_->appendReal("CRVAL1",1, 9, NULL);
    head_->appendReal("CDELT1",1, 9, NULL);

    head_->appendString("CTYPE2","LINEAR", NULL);
    head_->appendReal("CRPIX2",1, 9, NULL);
    head_->appendReal("CRVAL2",1, 9, NULL);
    head_->appendReal("CDELT2",1, 9, NULL);

    head_->appendString("CTYPE3","WAVELENGTH", NULL);
    head_->appendReal("CRPIX3",pCRPIX3_, 9, NULL);
    head_->appendReal("CRVAL3",pCRVAL3_, 9, NULL);
    head_->appendReal("CDELT3",pCDELT3_, 9, NULL);
  }

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

  inherit_ = head_->inherit();

  return 1;
}

template <class T> FitsENVIBIPm<T>::FitsENVIBIPm(FitsFile* fits)
  : FitsENVI(fits)
{
  if (!initHeader(fits))
    return;

  T* dest = new T[size_];
  if (!dest) {
    internalError("Fitsy++ envi unable to allocate memory");
    return;
  }
  memset(dest, 0, size_*sizeof(T));

  T* ptr = (T*)fits->data();
  for (int jj=0; jj<pHeight_; jj++)
    for (int ii=0; ii<pWidth_; ii++)
      for (int kk=0; kk<pDepth_; kk++)
	dest[kk*pWidth_*pHeight_ + jj*pWidth_ + ii] = *ptr++;

  data_ = dest;

  dataSize_ = size_;
  dataSkip_ = 0;

  // all done
  valid_ = 1;
}

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

template <class T> FitsENVIBILm<T>::FitsENVIBILm(FitsFile* fits)
  : FitsENVI(fits)
{
  if (!initHeader(fits))
    return;

  T* dest = new T[size_];
  if (!dest) {
    internalError("Fitsy++ envi unable to allocate memory");
    return;
  }
  memset(dest, 0, size_*sizeof(T));

  T* ptr = (T*)fits->data();
  for (int jj=0; jj<pHeight_; jj++)
    for (int kk=0; kk<pDepth_; kk++)
      for (int ii=0; ii<pWidth_; ii++)
	dest[kk*pWidth_*pHeight_ + jj*pWidth_ + ii] = *ptr++;

  data_ = dest;

  dataSize_ = size_;
  dataSkip_ = 0;

  // all done
  valid_ = 1;
}

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