"Fossies" - the Fresh Open Source Software Archive

Member "PDL-2.080/GENERATED/PDL/Compression.pm" (28 May 2022, 6213 Bytes) of package /linux/misc/PDL-2.080.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Compression.pm" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.079_vs_2.080.

    1 #
    2 # GENERATED WITH PDL::PP! 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 use PDL::Core;
   10 use PDL::Exporter;
   11 use DynaLoader;
   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 #line 5 "compression.pd"
   25 
   26 
   27 =head1 NAME
   28 
   29 PDL::Compression - compression utilities
   30 
   31 =head1 DESCRIPTION
   32 
   33 These routines generally accept some data as a PDL and compress it
   34 into a smaller PDL.  Algorithms typically work on a single dimension
   35 and broadcast over other dimensions, producing a broadcasted table of
   36 compressed values if more than one dimension is fed in.
   37 
   38 The Rice algorithm, in particular, is designed to be identical to the
   39 RICE_1 algorithm used in internal FITS-file compression (see PDL::IO::FITS).
   40 
   41 =head1 SYNOPSIS
   42 
   43  use PDL::Compression
   44 
   45  ($y,$xsize) = $x->rice_compress();
   46  $c = $y->rice_expand($xsize);
   47 
   48 =cut
   49 
   50 use strict;
   51 use warnings;
   52 #line 53 "Compression.pm"
   53 
   54 
   55 
   56 
   57 
   58 
   59 =head1 FUNCTIONS
   60 
   61 =cut
   62 
   63 
   64 
   65 
   66 #line 74 "compression.pd"
   67 
   68 
   69 =head1 METHODS
   70 
   71 =cut
   72 #line 73 "Compression.pm"
   73 
   74 
   75 
   76 #line 948 "../../blib/lib/PDL/PP.pm"
   77 
   78 
   79 
   80 =head2 rice_compress
   81 
   82 =for sig
   83 
   84   Signature: (in(n); [o]out(m); int[o]len(); int 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 =for usage
  127 
  128   $out = $pdl->rice_compress($blocksize);
  129   ($out, $len, $blocksize, $dim0) = $pdl->rice_compress;
  130 
  131   $new = $out->rice_expand;
  132 
  133 
  134 =for bad
  135 
  136 rice_compress ignores the bad-value flag of the input ndarrays.
  137 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
  138 
  139 
  140 =cut
  141 #line 142 "Compression.pm"
  142 
  143 
  144 
  145 #line 949 "../../blib/lib/PDL/PP.pm"
  146 
  147 sub PDL::rice_compress {
  148     my $in = shift;
  149     my $blocksize = shift || 32;
  150     ## Reject floating-point inputs
  151     if( $in->type != byte &&
  152         $in->type != short &&
  153     $in->type != ushort &&
  154     $in->type != long
  155     ) {
  156     die("rice_compress: input needs to have type byte, short, ushort, or long, not ".($in->type)."\n");
  157     }
  158     # output buffer starts the same size; truncate at the end.
  159     my ($out) = zeroes($in);
  160     # lengths go here
  161     my ($len) = zeroes(long, $in->slice("(0)")->dims);
  162     PDL::_rice_compress_int( $in, $out, $len, $blocksize  );
  163     my $l = $len->max;
  164     $out = $out->slice("0:".($l-1))->sever;
  165     return wantarray ? ($out, $in->dim(0), $blocksize, $len) : $out;
  166 }
  167 #line 168 "Compression.pm"
  168 
  169 
  170 
  171 #line 950 "../../blib/lib/PDL/PP.pm"
  172 
  173 *rice_compress = \&PDL::rice_compress;
  174 #line 175 "Compression.pm"
  175 
  176 
  177 
  178 #line 948 "../../blib/lib/PDL/PP.pm"
  179 
  180 
  181 
  182 =head2 rice_expand
  183 
  184 =for sig
  185 
  186   Signature: (in(n); [o]out(m); int blocksize)
  187 
  188 =for ref
  189 
  190 Unsquishes a PDL that has been squished by rice_compress.
  191 
  192 =for usage
  193 
  194      ($out, $len, $blocksize, $dim0) = $pdl->rice_compress;
  195      $copy = $out->rice_expand($dim0, $blocksize);
  196 
  197 
  198 =for bad
  199 
  200 rice_expand ignores the bad-value flag of the input ndarrays.
  201 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
  202 
  203 
  204 =cut
  205 #line 206 "Compression.pm"
  206 
  207 
  208 
  209 #line 949 "../../blib/lib/PDL/PP.pm"
  210 
  211 sub PDL::rice_expand {
  212     my $squished = shift;
  213     my $dim0 =shift;
  214     my $blocksize = shift || 32;
  215     # Allocate output array
  216     my $out = zeroes( $squished->slice("(0),*$dim0") );
  217     PDL::_rice_expand_int( $squished, $out, $blocksize );
  218     return $out;
  219 }
  220 #line 221 "Compression.pm"
  221 
  222 
  223 
  224 #line 950 "../../blib/lib/PDL/PP.pm"
  225 
  226 *rice_expand = \&PDL::rice_expand;
  227 #line 228 "Compression.pm"
  228 
  229 
  230 
  231 
  232 
  233 #line 35 "compression.pd"
  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 #line 271 "Compression.pm"
  271 
  272 
  273 
  274 
  275 # Exit with OK status
  276 
  277 1;