blib/lib/PDL/Compression.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 50.0% 2 / 4 Conditioncond 72.7% 8 / 11 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 78.0% 39 / 50 CC 7 SCAR 17.6
CC
7
Cov
85%
rice_compress
7.9
BEGIN
1.0
BEGIN
1.0
CRAP 5.8
« blib/lib/PDL/Char.pm ↑ Summary lib/PDL/FFT.pd »
1 #
2 # GENERATED WITH PDL::PP from lib/PDL/Compression.pd! Don't modify!
3 #
4 package PDL::Compression;
5  
6 3 our @EXPORT_OK = qw(rice_compress rice_expand );
7 3 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8  
9 3 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14
15 3 our @ISA = ( 'PDL::Exporter','DynaLoader' );
16 3 push @PDL::Core::PP, __PACKAGE__;
17 3 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<rice_compress> 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 4 ▶sub PDL::rice_compress {
Subroutine rice_compress: called CC=7 SCAR=20.6
Pod undocumented
141 4 my $in = shift;
142 4 ▶ my $blocksize = shift || 32;
Condition100%2/2
shift() || 32
l!l
31
MC/DC100%1/1
shift() || 32
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
143 ## Reject floating-point inputs
144 4 ▶ if( $in->type != byte &&
Branch: 1 branch
BranchTrueFalseTotal
if ($in->type != byte() and $in->type != short() and $in->type != ushort() and $in->type != long()) 0 4 4
Condition66%6/9
$in->type != byte() and $in->type != short() and $in->type != ushort() and $in->type != long()
!ll&&!rl&&r
130
$in->type != byte() and $in->type != short() and $in->type != ushort()
!ll&&!rl&&r
103
$in->type != byte() and $in->type != short()
!ll&&!rl&&r
103
MC/DC0%0/4
$in->type != byte() and $in->type != short() and $in->type != ushort() and $in->type != long()
$in->type != byte() $in->type != short() $in->type != ushort() $in->type != long()
Truth table40%2/5
A and B and C and D
A= $in->type != byte()B= $in->type != short()C= $in->type != ushort()D= $in->type != long()
ABCDresult
0XXX0
10XX0
110X0
11100
11111
145 $in->type != short &&
146 $in->type != ushort &&
147 $in->type != long
148 ) {
149 0 die("rice_compress: input needs to have type byte, short, ushort, or long, not ".($in->type)."\n");
150 }
151 4 PDL::_rice_compress_int( $in, my $out=PDL->null, my $len=PDL->null, $blocksize );
152 4 my $l = $len->max->sclr;
153 4 $out = $out->slice("0:".($l-1))->sever;
154 4 ▶ return wantarray ? ($out, $in->dim(0), $blocksize, $len) : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
155 }
156  
157  
158  
159 3 *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<rice_expand> 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 3 *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 3 1;
« blib/lib/PDL/Char.pm ↑ Summary lib/PDL/FFT.pd »