File: mgfyval.c

package info (click to toggle)
saoimage 1.35.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,188 kB
  • ctags: 4,403
  • sloc: ansic: 52,848; makefile: 243; sh: 31
file content (277 lines) | stat: -rw-r--r-- 9,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	mgfyval.c (Magnify Value)
 * Purpose:	Get file pixel value and make a formatted string to show it
 * Subroutine:	get_pixel_val()		returns: int
 * Subroutine:	integer_string()	returns: void
 * Subroutine:	real_string()		returns: void
 * Copyright:	1990 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Note:	Table based on code from Bill Wyatt's showimg
 * Modified:	{0} Michael VanHilst	initial version	     21 February 1990
 *		{1} MVH improved G formatter (c)MVH		   1 Jan 1991
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/*  Get stderr  */

#ifndef VMS
#ifdef SAO_SYSV
#include <string.h>		/*  strlen, strcat, strcpy, strrchr  */
#else
#include <strings.h>		/*  strlen, strcat, strcpy, rindex  */
#define strchr index		/*  Needed for a few unenlightened BSD's  */
#define strrchr rindex
#endif
#else
#include <string.h>		/*  strlen, strcat, strcpy, strrchr  */
#endif

#include <X11/Xlib.h>		/*  X window stuff  */
#include <X11/Xutil.h>		/*  X window manager stuff  */
#include "hfiles/struct.h"	/*  Declare structure types  */
#include "hfiles/constant.h"	/*  Define ARR_ codes  */
#include "hfiles/extern.h"	/*  Extern main parameter structures  */

#ifdef ANSIC
/*  Exported declarations must be centralized before ANSI C can be used  */

int		get_pixel_val(	int bufx, int bufy,
				int* ival, double* dval, int* clip);
void		integer_string(	int ival, int clip, char *string, int width);
void		real_string(	double val, char *string, int width);
static void	mark_overflow(	char *string, int string_width,
				char* label, int label_width);

#else

  void i_transform();
  int get_pixel_val();
  void integer_string(), real_string();
  static void mark_overflow();

#endif


/*  Subroutine:	get_pixel_val
 *  Purpose:	Return pixel value and code representing best format
 *  Returns:	1 if value is an integer, else 0
 *  Note:	Puts integer val in ival
 *  Note:	If float, double or scaled, puts float val in dval
 *  Note:	Sets clip if buffer value is at clipped value edge (IRAF)
 */
#ifdef ANSIC
int get_pixel_val( int bufx, int bufy, int* ival, double* dval, int* clip )
#else
int get_pixel_val ( bufx, bufy, ival, dval, clip )
     int bufx, bufy;	/* shortbuf ("buf") coordinates */
     int *ival;		/* gets integer value */
     double *dval;	/* gets floating point value (unless just integer) */
     int *clip;		/* -1 if at clipping min, 1 if at clipping max */
#endif
{
  int val;

  *clip = 0;
  if( img.fiscaled == 0 ) {
    *ival = buffer.shortbuf[(bufy * coord.buf.width) + bufx];
    return( 1 );
  } else {
    if( (buffer.filebuf == NULL) ||
        (buffer.filebuf == (char *)buffer.shortbuf) ) {
      /* values scaled, originals not available */
      val = buffer.shortbuf[bufx + (bufy * coord.buf.width)];
      *dval = ((double)val * img.fiscale) + img.fibias;
      /*  Print strings with spaces padding out the end  */
      if( val <= buffer.clipmin )
	*clip = -1;
      else if( val >= buffer.clipmax )
	*clip = 1;
    } else {
      /*  Values scaled, originals in filebuf  */
      float fbX, fbY;
      i_transform(&coord.buftofbuf, bufx, bufy, &fbX, &fbY);
      if( img.storage_type == ARR_I4 ) {
	*dval = (double)
	  *((int *)(buffer.filebuf +
		    (((int)fbX + ((int)fbY * coord.fbuf.width)) *
		     sizeof(int))));
      } else if( img.storage_type == ARR_R4 ) {
	*dval = (double)
	  *((float *)(buffer.filebuf +
		      (((int)fbX + ((int)fbY * coord.fbuf.width)) *
		       sizeof(float))));
      } else if( img.storage_type == ARR_R8 ) {
	*dval = *((double *)(buffer.filebuf +
			    (((int)fbX + ((int)fbY * coord.fbuf.width)) *
			     sizeof(double))));
      } else
	*dval = 0.0;
      if( img.fscaled )
	*dval = img.fbias + (*dval * img.fscale);
    }
    *ival = (int)(*dval);
    if( ((double)(*ival)) == *dval )
      return( 1 );
    else
      return( 0 );
  }
}


static char *iform[16] = { "x", "%d", "%2d", "%3d", "%4d", "%5d", "%6d",
			     "%7d", "%8d", "%9d", "%10d", "%11d", "%12d",
			     "%13d", "%14d", "%15d" };
/*  Subroutine:	integer_string
 *  Purpose:	Create an integer string, include clipping mark if appropriate
 *  Note:	to be called with full column width
 *  Note:	2<=width<=15
 */
#ifdef ANSIC
void integer_string( int ival, int clip, char *string, int width )
#else
void integer_string ( ival, clip, string, width )
     int ival;
     int clip;
     char *string;
     int width;
#endif
{
  char *edge;

  (void)sprintf(string, iform[width], ival);
  if( clip ) {
    if( string[0] != ' ' ) {
      if( width > 8 )
	real_string((double)ival, &string[1], width-1);
      else
	mark_overflow(string, width, "CLIP", 4);
      edge = string;
    } else
      edge = strrchr(string, ' ');
    /*  If value known to be clipped, indicate true value is beyond this  */
    if( clip > 0 )
      *edge = '>';
    else
      *edge = '<';
  } else {
    if( string[width] != '\0' ) {
      if( width > 8 ) {
	real_string((double)ival, &string[1], width-1);
	*string = ' ';
      } else
	mark_overflow(string, width, "ovfl", 4);
    }
  }
}


/*  Subroutine:	mark_overflow
 *  Purpose:	Print overflow message in place of value
 */
#ifdef ANSIC
static void mark_overflow( char *string, int string_width,
			   char* label, int label_width )
#else
static void mark_overflow ( string, string_width, label, label_width )
     char *string;
     int string_width;
     char *label;
     int label_width;
#endif
{
  int i;

  *string = ' ';
  for( i=0; i<label_width; i++ )
    string[i+1] = label[i];
  for( i=label_width+1; i<string_width; i++ )
    string[i] = ' ';
  string[string_width] = '\0';
}


static double emax[17] = { 1.0, 10.0, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8,
			   1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e17 };
static double emin[18] = {  0.0,  -1.0, -10.0,  -1e2,  -1e3,  -1e4,
			   -1e5,  -1e6,  -1e7,  -1e8,  -1e9, -1e10,
			  -1e11, -1e12, -1e13, -1e14, -1e15, -1e17 };
static char *fform[16] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f",
			     "%.6f", "%.7f", "%.8f", "%.9f", "%.10f",
			     "%.11f", "%.12f", "%.13f", "%.14f", "%.15f" };
static char *eform[16] = { "%.0e", "%.1e", "%.2e", "%.3e", "%.4e", "%.5e",
			     "%.6e", "%.7e", "%.8e", "%.9e", "%.10e",
			     "%.11e", "%.12e", "%.13e", "%.14e", "%.15e" };
/*  Subroutine:	real_string
 *  Purpose:	Print the real value in best format for character space given
 *  Note:	8<=width<=15 (7 makes -1e-10\0, 16 goes beyond lookup arrays)
 *  Note:	width characters beyond string[width] my get messed up
 *  Method:	For speed, compares and formats get values through lookups
 */
#ifdef ANSIC
void real_string( double val, char *string, int width )
#else
void real_string ( val, string, width )
     double val;	/* value to print */
     char *string;	/* string to recieve number (see note 2 above) */
     int width;		/* i: number of characters to use */
#endif
{
  if( val >= 0.0 ) {
    if( (val < 0.001) || (val >= emax[width-1]) ) {
      /*  Outside range for simple %f format  */
      if( (val >= 1e-9) && (val < 1e10) ) {
	/*  Inside range for 1 digit exponent (move digit 2 over)  */
	if( val >= 1.0 ) {
	  /*  Positive number with 1 digit positive exponent  */
	  (void)sprintf(string, eform[width-4], val);
	  string[width-1] = string[width+1];
	} else {
	  /*  Positive number with 1 digit negative exponent  */
	  (void)sprintf(string, eform[width-5], val);
	  string[width-1] = string[width];
	}
      } else if( val >= 1.0 ) {
	/*  Positive number with 2 digit positive exponent  */
	(void)sprintf(string, eform[width-5], val);
	string[width-2] = string[width-1];
	string[width-1] = string[width];
      } else
	/*  Positive number with 2 digit negative exponent  */
	(void)sprintf(string, eform[width-6], val);
    } else
      /*  Positive number in fixed point notation  */
      (void)sprintf(string, fform[width], val);
  } else {
    if( (val > -0.001) || (val <= emin[width]) ) {
      if( (val <= -1e-9) && (val > -1e10) ) {
	/*  Inside range for 1 digit exponent (move digit 2 over)  */
	if( val > -1.0 ) {
	  /*  Negative number with 1 digit negative exponent  */
	  (void)sprintf(string, eform[width-6], val);
	  string[width-1] = string[width];
	} else {
	  /*  Negative number with 1 digit positive exponent  */
	  (void)sprintf(string, eform[width-5], val);
	  string[width-1] = string[width+1];
	}
      } else if( val <= -1.0 ) {
	/*  Negative number with 2 digit positive exponent  */
	(void)sprintf(string, eform[width-6], val);
	string[width-2] = string[width-1];
	string[width-1] = string[width];
      } else
	/*  Negative number with 2 digit negative exponent  */
	(void)sprintf(string, eform[width-7], val);
    } else
      /*  Negative number in fixed point notation  */
      (void)sprintf(string, fform[width], val);
  }
  string[width] = '\0';
}