sym2img.c 5.29 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Project:  MapServer
 * Purpose:  Commandline utility to render symbols to a raster.
 * Author:   Steve Lime and the MapServer team.
 *
 ******************************************************************************
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
18
 * The above copyright notice and this permission notice shall be included in
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include <gd.h>
#include <gdfontt.h>

#include "mapserver.h"

35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

#define MAXCOLORS 256
#define CELLSIZE 100
#define NCOLS 5
#define RATIO .5
#define LBUF 5 /* space to reserve around line symbols */

int main(int argc, char *argv[])
{
  FILE *stream;
  int ns,n,k;
  gdImagePtr img;
  shapeObj p;
  int i,j;
  int ncols,nrows;
  char buffer[256];
  int gray, green, red, black, white;
  classObj class;
  symbolSetObj symbolSet;

56
  /* ---- check the number of arguments, return syntax if not correct ---- */
57
  if( argc < 2 ) {
58 59
    fprintf(stdout, "Syntax: sym2img [symbolset] [outfile]\n" );
    exit(0);
60 61 62 63 64 65 66 67 68
  }

  /* Initialize the polygon/polyline */
  p.line = (lineObj *)malloc(sizeof(lineObj));
  p.numlines = 1;
  p.line[0].point = (pointObj *)malloc(sizeof(pointObj)*4);
  p.line[0].numpoints = 4;

  /* Initialize the symbol and font sets */
69
  symbolSet.filename = msStrdup(argv[1]);
70

71
  /*
72 73
  ** load the symbol file
  */
74
  if(msLoadSymbolSet(&symbolSet) == -1) {
75
    msWriteError(stderr);
76
    exit(1);
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
  ns = symbolSet.numsymbols;

  if(ns < NCOLS) {
    ncols = ns;
    nrows = 1;
  } else {
    ncols = NCOLS;
    nrows = (int)ceil((double)ns/NCOLS);
  }

  img = gdImageCreate(ncols*CELLSIZE, nrows*CELLSIZE);

  gray = gdImageColorAllocate(img, 222, 222, 222);
  white = gdImageColorAllocate(img, 255, 255, 255);
  green = gdImageColorAllocate(img, 40, 170, 40);
  black = gdImageColorAllocate(img, 0, 0, 0);
  red = gdImageColorAllocate(img, 255, 0, 0);

  class.color = red;
  class.backgroundcolor = white;
  class.outlinecolor = black;

  n=0;

103 104 105 106 107
  for(i=0; n<ns; i+=CELLSIZE) {
    k=0;
    for(j=0; n<ns; j+=CELLSIZE) {
      if (k == ncols) break;
      k++;
108 109 110 111 112
      gdImageFilledRectangle(img,j,i,j+CELLSIZE, i+CELLSIZE, gray);

      class.symbol = n;

      switch(symbolSet.type) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        case(MS_MARKERSET):
          class.sizescaled = RATIO*CELLSIZE;
          p.line[0].point[0].x = MS_NINT(j + CELLSIZE/2);
          p.line[0].point[0].y = MS_NINT(i + CELLSIZE/2);
          p.line[0].numpoints = 1;
          msDrawMarkerSymbol(&(symbolSet), img, &(p.line[0].point[0]), &(class));
          break;

        case(MS_LINESET):
          class.sizescaled = 1;
          p.line[0].point[0].x = j;
          p.line[0].point[0].y = i + (CELLSIZE-LBUF) - 1;
          p.line[0].point[1].x = j + MS_NINT((CELLSIZE-LBUF)/3.0) - 1;
          p.line[0].point[1].y = i;
          p.line[0].point[2].x = j + MS_NINT(2*(CELLSIZE-LBUF)/3.0) - 1;
          p.line[0].point[2].y = i + (CELLSIZE-LBUF) - 1;
          p.line[0].point[3].x = j + (CELLSIZE-LBUF) - 1;
          p.line[0].point[3].y = i;
          p.line[0].numpoints = 4;
132
          msDrawLineSymbol(map, img, &p, &(class));
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
          break;

        case(MS_SHADESET):
          class.sizescaled = 5;
          p.line[0].point[0].x = j;
          p.line[0].point[0].y = i;
          p.line[0].point[1].x = j + CELLSIZE-1;
          p.line[0].point[1].y = i;
          p.line[0].point[2].x = j + CELLSIZE-1;
          p.line[0].point[2].y = i + CELLSIZE-1 ;
          p.line[0].point[3].x = j;
          p.line[0].point[3].y = i + CELLSIZE-1;
          p.line[0].numpoints = 4;
          msDrawShadeSymbol(&(symbolSet), img, &p, &(class));
          break;

        default:
          break;
151 152 153
      }

      if(symbolSet.symbol[n]->name)
154
        snprintf(buffer, sizeof(buffer), "%d - %s", n, symbolSet.symbol[n]->name);
155
      else
156
        snprintf(buffer, sizeof(buffer), "%d", n);
157 158 159 160 161 162
      gdImageString(img, gdFontTiny, j+1, i+1, buffer, black);

      n++;
    }
  }

163
  if((stream = fopen(argv[2],"wb")) == NULL) { /* open the file */
164
    fprintf(stderr, "Unable to open output file: %s\n", argv[2]);
165
    exit(1);
166
  }
167
#ifndef USE_GD_GIF
168 169 170 171 172 173 174 175 176 177 178
  gdImageGif(img, stream);
#else
  gdImagePng(img, stream);
#endif
  gdImageDestroy(img);
  free(symbolSet.filename);
  fclose(stream);

  return(MS_TRUE);
}