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