Skip to content

Commit

Permalink
harmonize 32/64bit behavior when rounding
Browse files Browse the repository at this point in the history
- use lrint
- avoid floats
- no implicit casts
  • Loading branch information
tbonfort committed Apr 30, 2013
1 parent 9a7bb09 commit 03eff67
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -54,6 +54,10 @@ macro( report_dependency_error component dependency)
) )
endmacro() endmacro()


SET(CMAKE_REQUIRED_INCLUDES "math.h")
if(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_REQUIRED_LIBRARIES "m")
endif(CMAKE_COMPILER_IS_GNUCXX)
check_function_exists("strrstr" HAVE_STRRSTR) check_function_exists("strrstr" HAVE_STRRSTR)
check_function_exists("strcasecmp" HAVE_STRCASECMP) check_function_exists("strcasecmp" HAVE_STRCASECMP)
check_function_exists("strcasestr" HAVE_STRCASESTR) check_function_exists("strcasestr" HAVE_STRCASESTR)
Expand Down
82 changes: 41 additions & 41 deletions mapchart.c
Expand Up @@ -109,7 +109,7 @@ int findChartPoint(mapObj *map, shapeObj *shape, int width, int height, pointObj
} }
} }


void drawRectangle(mapObj *map, imageObj *image, float mx, float my, float Mx, float My, void drawRectangle(mapObj *map, imageObj *image, double mx, double my, double Mx, double My,
styleObj *style) styleObj *style)
{ {
shapeObj shape; shapeObj shape;
Expand All @@ -129,13 +129,13 @@ void drawRectangle(mapObj *map, imageObj *image, float mx, float my, float Mx, f
} }


int msDrawVBarChart(mapObj *map, imageObj *image, pointObj *center, int msDrawVBarChart(mapObj *map, imageObj *image, pointObj *center,
float *values, styleObj **styles, int numvalues, double *values, styleObj **styles, int numvalues,
float barWidth) double barWidth)
{ {


int c; int c;
float left,bottom,cur; /*shortcut to pixel boundaries of the chart*/ double left,bottom,cur; /*shortcut to pixel boundaries of the chart*/
float height = 0; double height = 0;


for(c=0; c<numvalues; c++) { for(c=0; c<numvalues; c++) {
height += values[c]; height += values[c];
Expand All @@ -153,15 +153,15 @@ int msDrawVBarChart(mapObj *map, imageObj *image, pointObj *center,




int msDrawBarChart(mapObj *map, imageObj *image, pointObj *center, int msDrawBarChart(mapObj *map, imageObj *image, pointObj *center,
float *values, styleObj **styles, int numvalues, double *values, styleObj **styles, int numvalues,
float width, float height, float *maxVal, float *minVal, float barWidth) double width, double height, double *maxVal, double *minVal, double barWidth)
{ {


float upperLimit,lowerLimit; double upperLimit,lowerLimit;
float shapeMaxVal,shapeMinVal,pixperval; double shapeMaxVal,shapeMinVal,pixperval;
int c; int c;
float vertOrigin,vertOriginClipped,horizStart,y; double vertOrigin,vertOriginClipped,horizStart,y;
float left,top,bottom; /*shortcut to pixel boundaries of the chart*/ double left,top,bottom; /*shortcut to pixel boundaries of the chart*/


top=center->y-height/2.; top=center->y-height/2.;
bottom=center->y+height/2.; bottom=center->y+height/2.;
Expand Down Expand Up @@ -189,7 +189,7 @@ int msDrawBarChart(mapObj *map, imageObj *image, pointObj *center,
lowerLimit-=0.5; lowerLimit-=0.5;
} }


pixperval=(float)height/(upperLimit-lowerLimit); pixperval=height/(upperLimit-lowerLimit);
vertOrigin=bottom+lowerLimit*pixperval; vertOrigin=bottom+lowerLimit*pixperval;
vertOriginClipped=(vertOrigin<top) ? top : vertOriginClipped=(vertOrigin<top) ? top :
(vertOrigin>bottom) ? bottom : vertOrigin; (vertOrigin>bottom) ? bottom : vertOrigin;
Expand All @@ -199,7 +199,7 @@ int msDrawBarChart(mapObj *map, imageObj *image, pointObj *center,
gdImageRectangle(image->img.gd, left-1,top-1, center.x+width/2.+1,bottom+1,color); gdImageRectangle(image->img.gd, left-1,top-1, center.x+width/2.+1,bottom+1,color);
*/ */
for(c=0; c<numvalues; c++) { for(c=0; c<numvalues; c++) {
int barHeight=values[c]*pixperval; double barHeight=values[c]*pixperval;
/*clip bars*/ /*clip bars*/
y=((vertOrigin-barHeight)<top) ? top : y=((vertOrigin-barHeight)<top) ? top :
(vertOrigin-barHeight>bottom) ? bottom : vertOrigin-barHeight; (vertOrigin-barHeight>bottom) ? bottom : vertOrigin-barHeight;
Expand All @@ -215,8 +215,8 @@ int msDrawBarChart(mapObj *map, imageObj *image, pointObj *center,
} }


int msDrawPieChart(mapObj *map, imageObj *image, int msDrawPieChart(mapObj *map, imageObj *image,
pointObj *center, float diameter, pointObj *center, double diameter,
float *values, styleObj **styles, int numvalues) double *values, styleObj **styles, int numvalues)
{ {
int i; int i;
double dTotal=0.,start=0; double dTotal=0.,start=0;
Expand All @@ -230,7 +230,7 @@ int msDrawPieChart(mapObj *map, imageObj *image,
} }


for(i=0; i < numvalues; i++) { for(i=0; i < numvalues; i++) {
float angle = values[i]; double angle = values[i];
if(angle==0) continue; /*no need to draw. causes artifacts with outlines*/ if(angle==0) continue; /*no need to draw. causes artifacts with outlines*/
angle*=360.0/dTotal; angle*=360.0/dTotal;
msDrawPieSlice(&map->symbolset,image, center, styles[i], diameter/2., start, start+angle); msDrawPieSlice(&map->symbolset,image, center, styles[i], diameter/2., start, start+angle);
Expand All @@ -240,7 +240,7 @@ int msDrawPieChart(mapObj *map, imageObj *image,
return MS_SUCCESS; return MS_SUCCESS;
} }


int getNextShape(mapObj *map, layerObj *layer, float *values, int *nvalues, styleObj **styles, shapeObj *shape) int getNextShape(mapObj *map, layerObj *layer, double *values, int *nvalues, styleObj **styles, shapeObj *shape)
{ {
int status; int status;
int c; int c;
Expand Down Expand Up @@ -273,7 +273,7 @@ int pieLayerProcessDynamicDiameter(layerObj *layer)
{ {
const char *chartRangeProcessingKey=NULL; const char *chartRangeProcessingKey=NULL;
char *attrib; char *attrib;
float mindiameter=-1, maxdiameter, minvalue, maxvalue; double mindiameter=-1, maxdiameter, minvalue, maxvalue;
classObj *newclass; classObj *newclass;
styleObj *newstyle; styleObj *newstyle;
const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" ); const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" );
Expand All @@ -283,7 +283,7 @@ int pieLayerProcessDynamicDiameter(layerObj *layer)
if(chartRangeProcessingKey==NULL) if(chartRangeProcessingKey==NULL)
return MS_FALSE; return MS_FALSE;
attrib = msSmallMalloc(strlen(chartRangeProcessingKey)+1); attrib = msSmallMalloc(strlen(chartRangeProcessingKey)+1);
switch(sscanf(chartRangeProcessingKey,"%s %f %f %f %f",attrib, switch(sscanf(chartRangeProcessingKey,"%s %lf %lf %lf %lf",attrib,
&mindiameter,&maxdiameter,&minvalue,&maxvalue)) { &mindiameter,&maxdiameter,&minvalue,&maxvalue)) {
case 1: /*we only have the attribute*/ case 1: /*we only have the attribute*/
case 5: /*we have the attribute and the four range values*/ case 5: /*we have the attribute and the four range values*/
Expand Down Expand Up @@ -329,8 +329,8 @@ int msDrawPieChartLayer(mapObj *map, layerObj *layer, imageObj *image)
int status=MS_SUCCESS; int status=MS_SUCCESS;
const char *chartRangeProcessingKey=NULL; const char *chartRangeProcessingKey=NULL;
const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" ); const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" );
float diameter, mindiameter=-1, maxdiameter, minvalue, maxvalue, exponent=0; double diameter, mindiameter=-1, maxdiameter, minvalue, maxvalue, exponent=0;
float *values; double *values;
styleObj **styles; styleObj **styles;
pointObj center; pointObj center;
int numvalues = layer->numclasses; /* the number of classes to represent in the graph */ int numvalues = layer->numclasses; /* the number of classes to represent in the graph */
Expand All @@ -341,20 +341,20 @@ int msDrawPieChartLayer(mapObj *map, layerObj *layer, imageObj *image)
if(chartRangeProcessingKey==NULL) if(chartRangeProcessingKey==NULL)
diameter=20; diameter=20;
else { else {
sscanf(chartRangeProcessingKey,"%*s %f %f %f %f %f", sscanf(chartRangeProcessingKey,"%*s %lf %lf %lf %lf %lf",
&mindiameter,&maxdiameter,&minvalue,&maxvalue,&exponent); &mindiameter,&maxdiameter,&minvalue,&maxvalue,&exponent);
} }
} else { } else {
if(sscanf(chartSizeProcessingKey ,"%f",&diameter)!=1) { if(sscanf(chartSizeProcessingKey ,"%lf",&diameter)!=1) {
msSetError(MS_MISCERR, "msDrawChart format error for processing key \"CHART_SIZE\"", "msDrawPieChartLayer()"); msSetError(MS_MISCERR, "msDrawChart format error for processing key \"CHART_SIZE\"", "msDrawPieChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
} }
/* step through the target shapes */ /* step through the target shapes */
msInitShape(&shape); msInitShape(&shape);


values=(float*)calloc(numvalues,sizeof(float)); values=(double*)calloc(numvalues,sizeof(double));
MS_CHECK_ALLOC(values, numvalues*sizeof(float), MS_FAILURE); MS_CHECK_ALLOC(values, numvalues*sizeof(double), MS_FAILURE);
styles = (styleObj**)malloc((numvalues)*sizeof(styleObj*)); styles = (styleObj**)malloc((numvalues)*sizeof(styleObj*));
if (styles == NULL) { if (styles == NULL) {
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawPieChartLayer()", msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawPieChartLayer()",
Expand Down Expand Up @@ -412,31 +412,31 @@ int msDrawVBarChartLayer(mapObj *map, layerObj *layer, imageObj *image)
int status=MS_SUCCESS; int status=MS_SUCCESS;
const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" ); const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" );
const char *chartScaleProcessingKey=msLayerGetProcessingKey( layer,"CHART_SCALE" ); const char *chartScaleProcessingKey=msLayerGetProcessingKey( layer,"CHART_SCALE" );
float barWidth,scale=1.0; double barWidth,scale=1.0;
float *values; double *values;
styleObj **styles; styleObj **styles;
pointObj center; pointObj center;
int numvalues = layer->numclasses; int numvalues = layer->numclasses;
int numvalues_for_shape; int numvalues_for_shape;
if(chartSizeProcessingKey==NULL) { if(chartSizeProcessingKey==NULL) {
barWidth=20; barWidth=20;
} else { } else {
if(sscanf(chartSizeProcessingKey ,"%f",&barWidth) != 1) { if(sscanf(chartSizeProcessingKey ,"%lf",&barWidth) != 1) {
msSetError(MS_MISCERR, "msDrawChart format error for processing key \"CHART_SIZE\"", "msDrawVBarChartLayer()"); msSetError(MS_MISCERR, "msDrawChart format error for processing key \"CHART_SIZE\"", "msDrawVBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
} }


if(chartScaleProcessingKey) { if(chartScaleProcessingKey) {
if(sscanf(chartScaleProcessingKey,"%f",&scale)!=1) { if(sscanf(chartScaleProcessingKey,"%lf",&scale)!=1) {
msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_SCALE\"", "msDrawVBarChartLayer()"); msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_SCALE\"", "msDrawVBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
} }
msInitShape(&shape); msInitShape(&shape);


values=(float*)calloc(numvalues,sizeof(float)); values=(double*)calloc(numvalues,sizeof(double));
MS_CHECK_ALLOC(values, numvalues*sizeof(float), MS_FAILURE); MS_CHECK_ALLOC(values, numvalues*sizeof(double), MS_FAILURE);
styles = (styleObj**)malloc(numvalues*sizeof(styleObj*)); styles = (styleObj**)malloc(numvalues*sizeof(styleObj*));
if (styles == NULL) { if (styles == NULL) {
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawVBarChartLayer()", msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawVBarChartLayer()",
Expand Down Expand Up @@ -478,18 +478,18 @@ int msDrawBarChartLayer(mapObj *map, layerObj *layer, imageObj *image)
const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" ); const char *chartSizeProcessingKey=msLayerGetProcessingKey( layer,"CHART_SIZE" );
const char *barMax=msLayerGetProcessingKey( layer,"CHART_BAR_MAXVAL" ); const char *barMax=msLayerGetProcessingKey( layer,"CHART_BAR_MAXVAL" );
const char *barMin=msLayerGetProcessingKey( layer,"CHART_BAR_MINVAL" ); const char *barMin=msLayerGetProcessingKey( layer,"CHART_BAR_MINVAL" );
float width,height; double width,height;
float barWidth; double barWidth;
float *values; double *values;
styleObj **styles; styleObj **styles;
pointObj center; pointObj center;
float barMaxVal,barMinVal; double barMaxVal,barMinVal;
int numvalues = layer->numclasses; int numvalues = layer->numclasses;
int numvalues_for_shape; int numvalues_for_shape;
if(chartSizeProcessingKey==NULL) { if(chartSizeProcessingKey==NULL) {
width=height=20; width=height=20;
} else { } else {
switch(sscanf(chartSizeProcessingKey ,"%f %f",&width,&height)) { switch(sscanf(chartSizeProcessingKey ,"%lf %lf",&width,&height)) {
case 2: case 2:
break; break;
case 1: case 1:
Expand All @@ -502,13 +502,13 @@ int msDrawBarChartLayer(mapObj *map, layerObj *layer, imageObj *image)
} }


if(barMax) { if(barMax) {
if(sscanf(barMax,"%f",&barMaxVal)!=1) { if(sscanf(barMax,"%lf",&barMaxVal)!=1) {
msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_BAR_MAXVAL\"", "msDrawBarChartLayer()"); msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_BAR_MAXVAL\"", "msDrawBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
} }
if(barMin) { if(barMin) {
if(sscanf(barMin,"%f",&barMinVal)!=1) { if(sscanf(barMin,"%lf",&barMinVal)!=1) {
msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_BAR_MINVAL\"", "msDrawBarChartLayer()"); msSetError(MS_MISCERR, "Error reading value for processing key \"CHART_BAR_MINVAL\"", "msDrawBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
Expand All @@ -517,16 +517,16 @@ int msDrawBarChartLayer(mapObj *map, layerObj *layer, imageObj *image)
msSetError(MS_MISCERR, "\"CHART_BAR_MINVAL\" must be less than \"CHART_BAR_MAXVAL\"", "msDrawBarChartLayer()"); msSetError(MS_MISCERR, "\"CHART_BAR_MINVAL\" must be less than \"CHART_BAR_MAXVAL\"", "msDrawBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }
barWidth=(float)width/(float)layer->numclasses; barWidth=(double)width/(double)layer->numclasses;
if(!barWidth) { if(!barWidth) {
msSetError(MS_MISCERR, "Specified width of chart too small to fit given number of classes", "msDrawBarChartLayer()"); msSetError(MS_MISCERR, "Specified width of chart too small to fit given number of classes", "msDrawBarChartLayer()");
return MS_FAILURE; return MS_FAILURE;
} }


msInitShape(&shape); msInitShape(&shape);


values=(float*)calloc(numvalues,sizeof(float)); values=(double*)calloc(numvalues,sizeof(double));
MS_CHECK_ALLOC(values, numvalues*sizeof(float), MS_FAILURE); MS_CHECK_ALLOC(values, numvalues*sizeof(double), MS_FAILURE);
styles = (styleObj**)malloc(numvalues*sizeof(styleObj*)); styles = (styleObj**)malloc(numvalues*sizeof(styleObj*));
if (styles == NULL) { if (styles == NULL) {
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawBarChartLayer()", msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDrawBarChartLayer()",
Expand Down
2 changes: 1 addition & 1 deletion msautotest
Submodule msautotest updated from c44f02 to 5432f9

0 comments on commit 03eff67

Please sign in to comment.