Permalink
Browse files

Legend: draw icon for raster gradient classes (#1305)

  • Loading branch information...
rouault committed Jan 5, 2017
1 parent f246af5 commit 19d77c52467d445e678a291aaf0265f49c4e2176
View
@@ -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
@@ -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,
@@ -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;
Binary file not shown.
@@ -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.