#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} -- -- */ #include /* Get stderr */ #ifndef VMS #ifdef SYSV #include /* strlen, strcat, strcpy, strrchr */ #else #include /* strlen, strcat, strcpy, rindex */ #define strchr index /* Needed for a few unenlightened BSD's */ #define strrchr rindex #endif #else #include /* strlen, strcat, strcpy, strrchr */ #endif #include /* X window stuff */ #include /* 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= 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'; }