summaryrefslogtreecommitdiffstats
path: root/tksao/fitsy++/nrrdgzip.C
blob: 0af4b7d52e267cbf06650c040c80060ece5b5e68 (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
// Copyright (C) 1999-2016
// 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 "nrrdgzip.h"
#include "zlib.h"
#include "util.h"

template<class T> FitsNRRDGzipm<T>::FitsNRRDGzipm(FitsFile* fits) 
  : FitsNRRDm<T>(fits)
{
  FitsNRRDm<T>::uncompress(fits);
}

template <class T> int FitsNRRDGzipm<T>::compressed(T* dest, char* src, 
						    size_t sz)

{
  z_stream zstrm;
  zstrm.zalloc = NULL;
  zstrm.zfree = NULL;
  zstrm.opaque = NULL;
  zstrm.avail_in = sz;
  zstrm.next_in = (Bytef*)src;
  zstrm.avail_out = this->size_*sizeof(T);
  zstrm.next_out = (Bytef*)dest;

  // look for both zlib and gzip headers
  if (inflateInit2(&zstrm, MAX_WBITS+32) != Z_OK) {
    internalError("Fitsy++ gzip inflateInit error");
    return 0;
  }

  if (DebugCompress)
    cerr << "  inflate START: avail_in " << zstrm.avail_in
	 << " avail_out " << zstrm.avail_out
	 << " total_in " << zstrm.total_in 
	 << " total_out " << zstrm.total_out << endl;

  int result = ::inflate(&zstrm, Z_FINISH);

  switch (result) {
  case Z_OK:
    if (DebugCompress)
      cerr << "  inflate OK: avail_in " << zstrm.avail_in
	   << " avail_out " << zstrm.avail_out
	   << " total_in " << zstrm.total_in 
	   << " total_out " << zstrm.total_out << endl;
    break;
  case Z_STREAM_END:
    if (DebugCompress)
      cerr << "  inflate STREAM_END: avail_in " << zstrm.avail_in
	   << " avail_out " << zstrm.avail_out
	   << " total_in " << zstrm.total_in 
	   << " total_out " << zstrm.total_out << endl;
    break;
  case Z_BUF_ERROR:
    if (DebugCompress)
      cerr << "  inflate BUF_ERROR: avail_in " << zstrm.avail_in
	   << " avail_out " << zstrm.avail_out << endl;
    return 0;
  default:
    internalError("Fitsy++ gzip inflate error");
    return 0;
  }

  inflateEnd(&zstrm);

  return 1;
}

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