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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Copyright (C) 1999-2017
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
#include "rice.h"
extern "C" {
int fits_rdecomp (unsigned char *c,
int clen,
unsigned int array[],
int nx,
int nblock);
int fits_rdecomp_short (unsigned char *c,
int clen,
unsigned short array[],
int nx,
int nblock);
int fits_rdecomp_byte (unsigned char *c,
int clen,
unsigned char array[],
int nx,
int nblock);
}
template<class T> FitsRicem<T>::FitsRicem(FitsFile* fits)
: FitsCompressm<T>(fits)
{
// rice parameters
block_ = 32;
bytepix_ = 4;
noisebit_ = 4;
char name[] = "ZNAME ";
char val[] = "ZVAL ";
for (int ii=0; ii<9; ii++) {
name[5] = '0'+ii;
val[4] = '0'+ii;
if (fits->find(name)) {
char* str = fits->getString(name);
if (!strncmp(str,"BLOCK",4))
block_ = fits->getInteger(val,32);
else if (!strncmp(str,"BYTEPIX",4))
bytepix_ = fits->getInteger(val,4);
else if (!strncmp(str,"NOISEBIT",4))
noisebit_ = fits->getInteger(val,4);
}
}
FitsCompressm<T>::uncompress(fits);
}
template <class T> int FitsRicem<T>::compressed(T* dest, char* sptr,
char* heap,
int kkstart, int kkstop,
int jjstart, int jjstop,
int iistart, int iistop)
{
double zs = FitsCompressm<T>::bscale_;
if (FitsCompressm<T>::zscale_)
zs = FitsCompressm<T>::zscale_->value(sptr,0);
double zz = FitsCompressm<T>::bzero_;
if (FitsCompressm<T>::zzero_)
zz = FitsCompressm<T>::zzero_->value(sptr,0);
int blank = FitsCompressm<T>::blank_;
if (FitsCompressm<T>::zblank_)
blank = (int)FitsCompressm<T>::zblank_->value(sptr,0);
int icnt=0;
unsigned char* ibuf = (unsigned char*)((FitsBinColumnArray*)FitsCompressm<T>::compress_)->get(heap, sptr, &icnt);
// ibuf can be NULL
if (!ibuf || !icnt)
return 0;
int ocnt = FitsCompressm<T>::tilesize_;
int ll=0;
switch (bytepix_) {
case 1:
{
char* obuf = new char[ocnt];
if (fits_rdecomp_byte(ibuf, icnt, (unsigned char*)obuf, ocnt, block_)) {
internalError("Fitsy++ rice bad inflate result");
return 0;
}
for (int kk=kkstart; kk<kkstop; kk++)
for (int jj=jjstart; jj<jjstop; jj++)
for (int ii=iistart; ii<iistop; ii++,ll++) {
// very carefull about type conversions
size_t id = kk*FitsCompressm<T>::width_*FitsCompressm<T>::height_ + jj*FitsCompressm<T>::width_ + ii;
T val = FitsCompressm<T>::getValue(obuf+ll,zs,zz,blank);
dest[id] = val;
}
if (obuf)
delete [] obuf;
}
break;
case 2:
{
short* obuf = new short[ocnt];
if (fits_rdecomp_short(ibuf, icnt, (unsigned short*)obuf, ocnt, block_)) {
internalError("Fitsy++ rice bad inflate result");
return 0;
}
for (int kk=kkstart; kk<kkstop; kk++)
for (int jj=jjstart; jj<jjstop; jj++)
for (int ii=iistart; ii<iistop; ii++,ll++) {
// very carefull about type conversions
size_t id = kk*FitsCompressm<T>::width_*FitsCompressm<T>::height_ + jj*FitsCompressm<T>::width_ + ii;
T val = FitsCompressm<T>::getValue(obuf+ll,zs,zz,blank);
dest[id] = val;
}
if (obuf)
delete [] obuf;
}
break;
case 4:
{
int* obuf = new int[ocnt];
if (fits_rdecomp(ibuf, icnt, (unsigned int*)obuf, ocnt, block_)) {
internalError("Fitsy++ rice bad inflate result");
return 0;
}
for (int kk=kkstart; kk<kkstop; kk++)
for (int jj=jjstart; jj<jjstop; jj++)
for (int ii=iistart; ii<iistop; ii++,ll++) {
// very carefull about type conversions
int id = kk*FitsCompressm<T>::width_*FitsCompressm<T>::height_ + jj*FitsCompressm<T>::width_ + ii;
T val = FitsCompressm<T>::getValue(obuf+ll,zs,zz,blank);
dest[id] = val;
}
if (obuf)
delete [] obuf;
}
break;
}
return 1;
}
template class FitsRicem<unsigned char>;
template class FitsRicem<short>;
template class FitsRicem<unsigned short>;
template class FitsRicem<int>;
template class FitsRicem<long long>;
template class FitsRicem<float>;
template class FitsRicem<double>;
|