summaryrefslogtreecommitdiffstats
path: root/src/H5Zszip.c
blob: 0b58b177122bd6a2a4d92f05a83bf2251309fe6b (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
/*
 * Copyright © 1999-2001 NCSA
 *                       All rights reserved.
 *
 * Programmer:  Robb Matzke <matzke@llnl.gov>
 *              Friday, August 27, 1999
 */
#include "H5private.h"
#include "H5Eprivate.h"
#include "H5MMprivate.h"
#include "H5Zprivate.h"

#ifdef H5_HAVE_FILTER_SZIP

#ifdef H5_HAVE_SZLIB_H
#   include "szlib.h"
#endif

/* Interface initialization */
#define PABLO_MASK	H5Z_szip_mask
#define INTERFACE_INIT	NULL
static int interface_initialize_g = 0;



/*-------------------------------------------------------------------------
 * Function:	H5Z_filter_szip
 *
 * Purpose:	Implement an I/O filter around the 'rice' algorithm in
 *              libsz
 *
 * Return:	Success:	
 *
 *		Failure:	
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
size_t
H5Z_filter_szip (unsigned flags, 
		 size_t cd_nelmts,
		 const unsigned cd_values[], 
		 size_t nbytes,
		 size_t *buf_size, 
		 void **buf)
{

  size_t ret_value = 0;
  size_t size_out  = 0; 
  size_t size_in   = 0; 
  char *outbuf     = NULL;
  char *newbuf     = NULL;
  int status;
  SZ_com_t sz_param; 

  sz_param.options_mask        = cd_values[0];
  sz_param.bits_per_pixel      = cd_values[1];
  sz_param.pixels_per_block    = cd_values[2];
  sz_param.pixels_per_scanline = cd_values[3];

  FUNC_ENTER_NOAPI(H5Z_filter_szip, 0);

  if (flags & H5Z_FLAG_REVERSE) {
    /* Input; uncompress */

    size_t nalloc;

    newbuf = *buf;
    UINT32DECODE(newbuf,nalloc);

    size_out = nalloc;
    size_in = nbytes;

    if(NULL==(outbuf = H5MM_malloc(nalloc)))
      HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed for szip decompression");

     status = SZ_BufftoBuffDecompress(outbuf, &size_out, newbuf, size_in-4, &sz_param);

     if(status != SZ_OK) {
      HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "szip_filter: decompression failed");
    }

      *buf = newbuf -4;
      H5MM_xfree(*buf);
      *buf = outbuf;
      outbuf = NULL;
      *buf_size = nalloc;
      ret_value = size_out;
  }

    else {

      size_in = nbytes;
      size_out = nbytes;
      if(NULL==(outbuf = H5MM_malloc(size_out+4)))
	 HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "unable to allocate szip destination buffer");

      UINT32ENCODE(outbuf,nbytes);
      status = SZ_BufftoBuffCompress(outbuf, &size_out, *buf, size_in, &sz_param);
      if(SZ_OK!=status)
	HGOTO_ERROR (H5E_PLINE, H5E_CANTINIT, 0, "szip_filter: Compression failed"); 

      if(*buf) H5MM_xfree(*buf);
      *buf = outbuf-4;
      outbuf = NULL;
      *buf_size = size_out+4;
      ret_value = size_out+4;
    }

  done: 
    if(outbuf) H5MM_xfree(outbuf);
    FUNC_LEAVE_NOAPI(ret_value);
  }
  
#endif