Skip to content

Commit

Permalink
Legend: draw icon for raster gradient classes (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 5, 2017
1 parent f246af5 commit 19d77c5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
67 changes: 64 additions & 3 deletions maplegend.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@
#define VMARGIN 5 /* margin at top and bottom of legend graphic */
#define HMARGIN 5 /* margin at left and right of legend graphic */


static int msDrawGradientSymbol(rendererVTableObj* renderer,
imageObj* image_draw,
double x_center,
double y_center,
int width,
int height,
styleObj* style)
{
int i, j;
unsigned char *r,*g,*b,*a;
symbolObj symbol;
rasterBufferObj* rb;
symbolStyleObj symbolStyle;
int ret;

initSymbol(&symbol);
rb = (rasterBufferObj*)calloc(1,sizeof(rasterBufferObj));
symbol.pixmap_buffer = rb;
rb->type = MS_BUFFER_BYTE_RGBA;
rb->width = width;
rb->height = height;
rb->data.rgba.row_step = rb->width * 4;
rb->data.rgba.pixel_step = 4;
rb->data.rgba.pixels = (unsigned char*)malloc(
rb->width*rb->height*4*sizeof(unsigned char));
b = rb->data.rgba.b = &rb->data.rgba.pixels[0];
g = rb->data.rgba.g = &rb->data.rgba.pixels[1];
r = rb->data.rgba.r = &rb->data.rgba.pixels[2];
a = rb->data.rgba.a = &rb->data.rgba.pixels[3];
for( j = 0; j < rb->height; j++ )
{
for( i = 0; i < rb->width; i++ )
{
msValueToRange(style, style->minvalue +
(double)i / rb->width * (style->maxvalue - style->minvalue), MS_COLORSPACE_RGB);
b[4*(j * rb->width + i)] = style->color.blue;
g[4*(j * rb->width + i)] = style->color.green;
r[4*(j * rb->width + i)] = style->color.red;
a[4*(j * rb->width + i)] = style->color.alpha;
}
}
INIT_SYMBOL_STYLE(symbolStyle);
ret = renderer->renderPixmapSymbol(image_draw, x_center, y_center, &symbol, &symbolStyle);
msFreeSymbol(&symbol);
return ret;
}

/*
* generic function for drawing a legend icon. (added for bug #2348)
* renderer specific drawing functions shouldn't be called directly, but through
Expand Down Expand Up @@ -262,8 +310,22 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
if (theclass->styles[i]->_geomtransform.type == MS_GEOMTRANSFORM_NONE ||
theclass->styles[i]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT ||
theclass->styles[i]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOLY) {
ret = msDrawShadeSymbol(map, image_draw, &box, theclass->styles[i], lp->scalefactor * image_draw->resolutionfactor);
if(UNLIKELY(ret == MS_FAILURE)) goto legend_icon_cleanup;

if (MS_VALID_COLOR(theclass->styles[i]->mincolor))
{
ret = msDrawGradientSymbol(renderer,
image_draw,
dstX + width / 2.,
dstY + height / 2.0,
width - 2 * polygon_contraction,
height - 2 * polygon_contraction,
theclass->styles[i]);
}
else
{
ret = msDrawShadeSymbol(map, image_draw, &box, theclass->styles[i], lp->scalefactor * image_draw->resolutionfactor);
}
if(UNLIKELY(ret == MS_FAILURE)) goto legend_icon_cleanup;
}
else {
ret = msDrawTransformedShape(map, image_draw, &box,
Expand Down Expand Up @@ -394,7 +456,6 @@ int msDrawLegendIcon(mapObj *map, layerObj *lp, classObj *theclass,
return ret;
}


imageObj *msCreateLegendIcon(mapObj* map, layerObj* lp, classObj* class, int width, int height, int scale_independant)
{
imageObj *image;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions msautotest/renderers/legend_raster_gradient.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# RUN_PARMS: legend_raster_gradient.png [MAPSERV] QUERY_STRING="map=[MAPFILE]&mode=legend&map.imagetype=png" > [RESULT_DEMIME]

MAP

NAME TEST
STATUS ON
SIZE 200 150
EXTENT 0.5 0.5 399.5 299.5
IMAGECOLOR 255 255 0

IMAGETYPE png24_t

OUTPUTFORMAT
NAME png24_t
DRIVER "GDAL/PNG"
IMAGEMODE RGBA
TRANSPARENT ON
END

LAYER
NAME rgb
TYPE raster
STATUS default
DATA ../gdal/data/rgba.tif
PROCESSING "BANDS=1,2,3"

CLASS
NAME ""
EXPRESSION ([pixel] >= 0.01 AND [pixel] < 0.05)
STYLE
COLORRANGE 0 0 0 255 255 255
DATARANGE 0.01 0.05
END
END

END

END # of map file

0 comments on commit 19d77c5

Please sign in to comment.