File Coverage

blib/lib/PDL/Compression.pm
Criterion Covered Total %
statement 16 17 94.1
branch 2 4 50.0
condition 7 11 63.6
subroutine 4 4 100.0
pod 0 1 0.0
total 29 37 78.3


line stmt bran cond sub pod time code
1             #
2             # GENERATED WITH PDL::PP from lib/PDL/Compression.pd! Don't modify!
3             #
4             package PDL::Compression;
5              
6             our @EXPORT_OK = qw(rice_compress rice_expand );
7             our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8              
9 3     3   618 use PDL::Core;
  3         6  
  3         25  
10 3     3   21 use PDL::Exporter;
  3         4  
  3         21  
11 3     3   13 use DynaLoader;
  3         5  
  3         1194  
12              
13              
14            
15             our @ISA = ( 'PDL::Exporter','DynaLoader' );
16             push @PDL::Core::PP, __PACKAGE__;
17             bootstrap PDL::Compression ;
18              
19              
20              
21              
22              
23              
24              
25              
26             #line 9 "lib/PDL/Compression.pd"
27              
28             =head1 NAME
29              
30             PDL::Compression - compression utilities
31              
32             =head1 DESCRIPTION
33              
34             These routines generally accept some data as a PDL and compress it
35             into a smaller PDL. Algorithms typically work on a single dimension
36             and broadcast over other dimensions, producing a broadcasted table of
37             compressed values if more than one dimension is fed in.
38              
39             The Rice algorithm, in particular, is designed to be identical to the
40             RICE_1 algorithm used in internal FITS-file compression (see PDL::IO::FITS).
41              
42             =head1 SYNOPSIS
43              
44             use PDL::Compression
45              
46             ($y,$xsize) = $x->rice_compress();
47             $c = $y->rice_expand($xsize);
48              
49             =cut
50              
51             use strict;
52             use warnings;
53             #line 54 "lib/PDL/Compression.pm"
54              
55              
56             =head1 FUNCTIONS
57              
58             =cut
59              
60              
61              
62              
63              
64             #line 78 "lib/PDL/Compression.pd"
65              
66             =head1 METHODS
67              
68             =cut
69             #line 70 "lib/PDL/Compression.pm"
70              
71              
72             =head2 rice_compress
73              
74             =for sig
75              
76             Signature: (in(n); [o]out(m=CALC(ceil($SIZE(n) * 1.01))); indx[o]len(); int blocksize)
77             Types: (byte short ushort long)
78              
79             =for usage
80              
81             ($out, $len) = rice_compress($in, $blocksize);
82             rice_compress($in, $out, $len, $blocksize); # all arguments given
83             ($out, $len) = $in->rice_compress($blocksize); # method call
84             $in->rice_compress($out, $len, $blocksize);
85              
86             =for ref
87              
88             Squishes an input PDL along the 0 dimension by Rice compression. In
89             scalar context, you get back only the compressed PDL; in list context,
90             you also get back ancillary information that is required to uncompress
91             the data with rice_uncompress.
92              
93             Multidimensional data are broadcasted over - each row is compressed
94             separately, and the returned PDL is squished to the maximum compressed
95             size of any row. If any of the streams could not be compressed (the
96             algorithm produced longer output), the corresponding length is set to -1
97             and the row is treated as if it had length 0.
98              
99             Rice compression only works on integer data types -- if you have
100             floating point data you must first quantize them.
101              
102             The underlying algorithm is identical to the Rice compressor used in
103             CFITSIO (and is used by PDL::IO::FITS to load and save compressed FITS
104             images).
105              
106             The optional blocksize indicates how many samples are to be compressed
107             as a unit; it defaults to 32.
108              
109             How it works:
110              
111             Rice compression is a subset of Golomb compression, and works on data sets
112             where variation between adjacent samples is typically small compared to the
113             dynamic range of each sample. In this implementation (originally written
114             by Richard White and contributed to CFITSIO in 1999), the data are divided
115             into blocks of samples (by default 32 samples per block). Each block
116             has a running difference applied, and the difference is bit-folded to make
117             it positive definite. High order bits of the difference stream are discarded,
118             and replaced with a unary representation; low order bits are preserved. Unary
119             representation is very efficient for small numbers, but large jumps could
120             give rise to ludicrously large bins in a plain Golomb code; such large jumps
121             ("high entropy" samples) are simply recorded directly in the output stream.
122              
123             Working on astronomical or solar image data, typical compression ratios of
124             2-3 are achieved.
125              
126             =pod
127              
128             Broadcasts over its inputs.
129              
130             =for bad
131              
132             C ignores the bad-value flag of the input ndarrays.
133             It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
134              
135             =cut
136              
137              
138              
139              
140             sub PDL::rice_compress {
141 4     4 0 580 my $in = shift;
142 4   100     16 my $blocksize = shift || 32;
143             ## Reject floating-point inputs
144 4 50 66     17 if( $in->type != byte &&
      66        
      33        
145             $in->type != short &&
146             $in->type != ushort &&
147             $in->type != long
148             ) {
149 0         0 die("rice_compress: input needs to have type byte, short, ushort, or long, not ".($in->type)."\n");
150             }
151 4         23 PDL::_rice_compress_int( $in, my $out=PDL->null, my $len=PDL->null, $blocksize );
152 4         75 my $l = $len->max->sclr;
153 4         96 $out = $out->slice("0:".($l-1))->sever;
154 4 50       79 return wantarray ? ($out, $in->dim(0), $blocksize, $len) : $out;
155             }
156              
157              
158              
159             *rice_compress = \&PDL::rice_compress;
160              
161              
162              
163              
164              
165              
166             =head2 rice_expand
167              
168             =for sig
169              
170             Signature: (in(n); indx len(); [o]out(m); IV dim0 => m; int blocksize)
171             Types: (byte short ushort long)
172              
173             =for usage
174              
175             $out = rice_expand($in, $len, $dim0); # using default value of blocksize=32
176             $out = rice_expand($in, $len, $dim0, $blocksize); # overriding default
177             rice_expand($in, $len, $out, $dim0, $blocksize); # all arguments given
178             $out = $in->rice_expand($len, $dim0); # method call
179             $out = $in->rice_expand($len, $dim0, $blocksize);
180             $in->rice_expand($len, $out, $dim0, $blocksize);
181              
182             =for ref
183              
184             Unsquishes a PDL that has been squished by rice_compress.
185              
186             =pod
187              
188             Broadcasts over its inputs.
189              
190             =for bad
191              
192             C ignores the bad-value flag of the input ndarrays.
193             It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
194              
195             =cut
196              
197              
198              
199              
200             *rice_expand = \&PDL::rice_expand;
201              
202              
203              
204              
205              
206              
207              
208             #line 39 "lib/PDL/Compression.pd"
209              
210             =head1 AUTHORS
211              
212             Copyright (C) 2010 Craig DeForest.
213             All rights reserved. There is no warranty. You are allowed
214             to redistribute this software / documentation under certain
215             conditions. For details, see the file COPYING in the PDL
216             distribution. If this file is separated from the PDL distribution,
217             the copyright notice should be included in the file.
218              
219             The Rice compression library is derived from the similar library in
220             the CFITSIO 3.24 release, and is licensed under yet more more lenient
221             terms than PDL itself; that notice is present in the file "ricecomp.c".
222              
223             =head1 BUGS
224              
225             =over 3
226              
227             =item * Currently headers are ignored.
228              
229             =item * Currently there is only one compression algorithm.
230              
231             =back
232              
233             =head1 TODO
234              
235             =over 3
236              
237             =item * Add object encapsulation
238              
239             =item * Add test suite
240              
241             =back
242              
243             =cut
244             #line 245 "lib/PDL/Compression.pm"
245              
246             # Exit with OK status
247              
248             1;