Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
float_to_exp.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  *
18  * Modified at BRL 16-May-88 by Mike Muuss to avoid Alliant STDC problem
19  * with <math.h> having defines for "exp" conflicting with local vars.
20  */
21 /*
22  * float_to_exp.c - Convert floating point values to exponent bytes
23  *
24  * Author: John W. Peterson
25  * Computer Science Dept.
26  * University of Utah
27  * Date: Thu Oct 29 1987
28  * Copyright (c) 1987, University of Utah
29  */
30 
31 #include <stdio.h>
32 #include "rle.h"
33 #include <math.h>
34 
35 /*****************************************************************
36  * TAG( float_to_exp )
37  *
38  * Takes an array of count floating point numbers, and makes an array
39  * of count+1 pixels out of it.
40  */
41 void
43 int count;
44 float * floats;
45 rle_pixel * pixels;
46 {
47  register int i;
48  int expon, max_exp = -2000;
49  float * fptr = floats;
50  double f_exp;
51 
52  /* Find largest exponent */
53  /* Use "Block normalization":
54  * ExpScan[x] is largest exponent of the three
55  * color components. Red/Grn/BluScan[1..3] are the
56  * normalized color components.
57  */
58 
59  for (i = 0; i < count; i++)
60  {
61  frexp( *fptr++, &expon );
62  max_exp = (expon > max_exp) ? expon : max_exp;
63  }
64 
65  /* Don't over/underflow */
66  if (max_exp > 128) max_exp = 128;
67  else
68  if (max_exp < -127) max_exp = -127;
69 
70  f_exp = ldexp( 256.0, -max_exp );
71 
72  fptr = floats;
73  for( i = 0; i < count; i++ ) /* Extra casts for broken HP compiler */
74  *pixels++ = (rle_pixel) ((int)(*fptr++ * f_exp));
75 
76  /* Excess 127 exponent */
77  *pixels = (rle_pixel) (max_exp + 127);
78 }
void float_to_exp(int count, float *floats, rle_pixel *pixels)
Definition: float_to_exp.c:42
unsigned char rle_pixel
Definition: rle.h:56