mapimagemap.c 25.1 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Project:  MapServer
 * Purpose:  Implements imagemap outputformat support.
 * Author:   Attila Csipa
 *
 ******************************************************************************
 * 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
 * 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 "mapserver.h"
#include "dxfcolor.h"
32

33 34 35 36 37 38 39 40
#include <stdarg.h>
#include <time.h>

#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif

41

42 43

#define MYDEBUG 0
44
#define DEBUG_IF if (MYDEBUG > 2)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/*
 * Client-side imagemap support was originally written by
 * Attila Csipa (http://prometheus.org.yu/me.php).  C. Scott Ananian
 * (http://cscott.net) cleaned up the code somewhat and made it more flexible:
 * you can now customize the generated HREFs and create mouseover and
 * mouseout attributes.
 *
 * Use
 *   IMAGETYPE imagemap
 * to select this driver.
 *
 * The following FORMATOPTIONs are available. If any are set to the empty
 * string the associated attribute will be suppressed.
 *   POLYHREF   the href string to use for the <area> elements.
 *              use a %s to interpolate the area title.
 *              default:   "javascript:Clicked('%s');"
 *   POLYMOUSEOVER the onMouseOver string to use for the <area> elements.
 *              use a %s to interpolate the area title.
 *              default:   "" (ie the attribute is suppressed)
 *   POLYMOUSEOUT the onMouseOut string to use for the <area> elements.
 *              use a %s to interpolate the area title.
 *              default:   "" (ie the attribute is suppressed)
 *   SYMBOLMOUSEOVER and SYMBOLMOUSEOUT are equivalent properties for
 *              <area> tags representing symbols, with the same defaults.
 *   MAPNAME    the string which will be used in the name attribute
 *              of the <map> tag.  There is no %s interpolation.
 *              default: "map1"
 *   SUPPRESS   if "yes", then we will suppress area declarations with
 *              no title.
 *              default: "NO"
76
 *
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
 * For example, http://vevo.verifiedvoting.org/verifier contains this
 * .map file fragment:
 *         OUTPUTFORMAT
 *              NAME imagemap
 *              DRIVER imagemap
 *              FORMATOPTION "POLYHREF=/verifier/map.php?state=%s"
 *              FORMATOPTION "SYMBOLHREF=#"
 *              FORMATOPTION "SUPPRESS=YES"
 *              FORMATOPTION "MAPNAME=map-48"
 *              FORMATOPTION "POLYMOUSEOUT=return nd();"
 *              FORMATOPTION "POLYMOUSEOVER=return overlib('%s');"
 *         END
 */

/*-------------------------------------------------------------------------*/

/* A pString is a variable-length (appendable) string, stored as a triple:
 * character pointer, allocated length, and used length.  The one wrinkle
 * is that we use pointers to the allocated size and character pointer,
 * in order to support refering to fields declared elsewhere (ie in the
 * 'image' data structure) for these.  The 'iprintf' function appends
 * to a pString. */
typedef struct pString {
100 101 102 103 104
  /* these two fields are somewhere else */
  char **string;
  int *alloc_size;
  /* this field is stored locally. */
  int string_len;
105 106 107 108 109 110 111
} pString;
/* These are the pStrings we declare statically.  One is the 'output'
 * imagemap/dxf file; parts of this live in another data structure and
 * are only referenced indirectly here.  The other contains layer-specific
 * information for the dxf output. */
static char *layerlist=NULL;
static int layersize=0;
112
static pString imgStr, layerStr= { &layerlist, &layersize, 0 };
113 114 115 116 117 118 119 120 121 122 123

/* Format strings for the various bits of a client-side imagemap. */
static const char *polyHrefFmt, *polyMOverFmt, *polyMOutFmt;
static const char *symbolHrefFmt, *symbolMOverFmt, *symbolMOutFmt;
static const char *mapName;
/* Should we suppress AREA declarations in imagemaps with no title? */
static int suppressEmpty=0;

/* Prevent buffer-overrun and format-string attacks by "sanitizing" any
 * provided format string to fit the number of expected string arguments
 * (MAX) exactly. */
124 125
static const char *makeFmtSafe(const char *fmt, int MAX)
{
126 127
  /* format string should have exactly 'MAX' %s */

128
  char *result = msSmallMalloc(strlen(fmt)+1+3*MAX), *cp;
129 130 131 132 133 134
  int numstr=0, saw_percent=0;

  strcpy(result, fmt);
  for (cp=result; *cp; cp++) {
    if (saw_percent) {
      if (*cp=='%') {
135
        /* safe */
136
      } else if (*cp=='s' && numstr<MAX) {
137
        numstr++; /* still safe */
138
      } else {
139 140
        /* disable this unsafe format string! */
        *(cp-1)=' ';
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      }
      saw_percent=0;
    } else if (*cp=='%')
      saw_percent=1;
  }
  /* fixup format strings without enough %s in them */
  while (numstr<MAX) {
    strcpy(cp, "%.s"); /* print using zero-length precision */
    cp+=3;
    numstr++;
  }
  return result;
}

/* Append the given printf-style formatted string to the pString 'ps'.
 * This is much cleaner (and faster) than the technique this file
 * used to use! */
158 159 160 161 162 163 164
static void im_iprintf(pString *ps, const char *fmt, ...)
{
  int n, remaining;
  va_list ap;
  do {
    remaining = *(ps->alloc_size) - ps->string_len;
    va_start(ap, fmt);
165
#if defined(HAVE_VSNPRINTF)
166 167
    n = vsnprintf((*(ps->string)) + ps->string_len,
                  remaining, fmt, ap);
168
#else
169 170 171 172 173 174 175 176
    /* If vsnprintf() is not available then require a minimum
     * of 512 bytes of free space to prevent a buffer overflow
     * This is not fully bulletproof but should help, see bug 1613
     */
    if (remaining < 512)
      n = -1;
    else
      n = vsprintf((*(ps->string)) + ps->string_len, fmt, ap);
177
#endif
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    va_end(ap);
    /* if that worked, we're done! */
    if (-1<n && n<remaining) {
      ps->string_len += n;
      return;
    } else { /* double allocated string size */
      *(ps->alloc_size) *= 2;/* these keeps realloc linear.*/
      if (*(ps->alloc_size) < 1024)
        /* careful: initial size can be 0 */
        *(ps->alloc_size)=1024;
      if (n>-1 && *(ps->alloc_size) <= (n + ps->string_len))
        /* ensure at least as much as what is needed */
        *(ps->alloc_size) = n+ps->string_len+1;
      *(ps->string) = (char *) msSmallRealloc
                      (*(ps->string), *(ps->alloc_size));
      /* if realloc doesn't work, we're screwed! */
    }
  } while (1); /* go back and try again. */
196 197 198 199 200 201 202
}



static int lastcolor=-1;
static int matchdxfcolor(colorObj col)
{
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  int best=7;
  int delta=128*255;
  int tcolor = 0;
  if (lastcolor != -1)
    return lastcolor;
  while (tcolor < 256 && (ctable[tcolor].r != col.red || ctable[tcolor].g != col.green || ctable[tcolor].b != col.blue)) {
    if (abs(
          (ctable[tcolor].r - col.red) * (ctable[tcolor].r - col.red)+
          (ctable[tcolor].b - col.blue) * (ctable[tcolor].b - col.blue) +
          (ctable[tcolor].g - col.green) * (ctable[tcolor].g - col.green) < delta)
       ) {
      best = tcolor;
      delta = abs(
                (ctable[tcolor].r - col.red) * (ctable[tcolor].r - col.red)+
                (ctable[tcolor].b - col.blue) * (ctable[tcolor].b - col.blue) +
                (ctable[tcolor].g - col.green) * (ctable[tcolor].g - col.green)
              );
    }
    tcolor++;
222
  }
223 224 225 226
  if (tcolor >= 256) tcolor = best;
  /* DEBUG_IF printf("%d/%d/%d (%d/%d - %d), %d : %d/%d/%d<BR>\n", ctable[tcolor].r, ctable[tcolor].g, ctable[tcolor].b, tcolor, best, delta, lastcolor, col.red, col.green, col.blue); */
  lastcolor = tcolor;
  return tcolor;
227 228 229 230 231
}

static char* lname;
static int dxf;

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
void msImageStartLayerIM(mapObj *map, layerObj *layer, imageObj *image)
{
  DEBUG_IF printf("ImageStartLayerIM\n<BR>");
  free(lname);
  if (layer->name)
    lname = msStrdup(layer->name);
  else
    lname = msStrdup("NONE");
  if (dxf == 2) {
    im_iprintf(&layerStr, "LAYER\n%s\n", lname);
  } else if (dxf) {
    im_iprintf(&layerStr,
               "  0\nLAYER\n  2\n%s\n"
               " 70\n  64\n 6\nCONTINUOUS\n", lname);
  }
  lastcolor = -1;
248 249 250 251 252 253 254 255 256
}

/*
 * Utility function to create a IM image. Returns
 * a pointer to an imageObj structure.
 */
imageObj *msImageCreateIM(int width, int height, outputFormatObj *format,
                          char *imagepath, char *imageurl, double resolution, double defresolution)
{
257 258 259 260 261 262
  imageObj  *image=NULL;
  if (setvbuf(stdout, NULL, _IONBF , 0)) {
    printf("Whoops...");
  };
  DEBUG_IF printf("msImageCreateIM<BR>\n");
  if (width > 0 && height > 0) {
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    image = (imageObj *)msSmallCalloc(1,sizeof(imageObj));
    imgStr.string = &(image->img.imagemap);
    imgStr.alloc_size = &(image->size);

    image->format = format;
    format->refcount++;

    image->width = width;
    image->height = height;
    image->imagepath = NULL;
    image->imageurl = NULL;
    image->resolution = resolution;
    image->resolutionfactor = resolution/defresolution;

    if( strcasecmp("ON",msGetOutputFormatOption( format, "DXF", "OFF" )) == 0) {
      dxf = 1;
      im_iprintf(&layerStr, "  2\nLAYER\n 70\n  10\n");
    } else
      dxf = 0;
282

283 284 285 286
    if( strcasecmp("ON",msGetOutputFormatOption( format, "SCRIPT", "OFF" )) == 0) {
      dxf = 2;
      im_iprintf(&layerStr, "");
    }
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    /* get href formation string options */
    polyHrefFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "POLYHREF", "javascript:Clicked('%s');"), 1);
    polyMOverFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "POLYMOUSEOVER", ""), 1);
    polyMOutFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "POLYMOUSEOUT", ""), 1);
    symbolHrefFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "SYMBOLHREF", "javascript:SymbolClicked();"), 1);
    symbolMOverFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "SYMBOLMOUSEOVER", ""), 1);
    symbolMOutFmt = makeFmtSafe(msGetOutputFormatOption
        ( format, "SYMBOLMOUSEOUT", ""), 1);
    /* get name of client-side image map */
    mapName = msGetOutputFormatOption
      ( format, "MAPNAME", "map1" );
    /* should we suppress area declarations with no title? */
    if( strcasecmp("YES",msGetOutputFormatOption( format, "SUPPRESS", "NO" )) == 0) {
      suppressEmpty=1;
    }
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    lname = msStrdup("NONE");
    *(imgStr.string) = msStrdup("");
    if (*(imgStr.string)) {
      *(imgStr.alloc_size) =
        imgStr.string_len = strlen(*(imgStr.string));
    } else {
      *(imgStr.alloc_size) =
        imgStr.string_len = 0;
    }
    if (imagepath) {
      image->imagepath = msStrdup(imagepath);
    }
    if (imageurl) {
      image->imageurl = msStrdup(imageurl);
    }

    return image;
326 327 328 329
  } else {
    msSetError(MS_IMGERR,
               "Cannot create IM image of size %d x %d.",
               "msImageCreateIM()", width, height );
330
  }
331
  return image;
332 333 334 335 336
}

/* ------------------------------------------------------------------------- */
/* Draw a single marker symbol of the specified size and color               */
/* ------------------------------------------------------------------------- */
337
void msDrawMarkerSymbolIM(mapObj *map, imageObj* img, pointObj *p, styleObj *style, double scalefactor)
338 339 340 341 342
{
  symbolObj *symbol;
  int ox, oy;
  double size;

343 344 345
  DEBUG_IF printf("msDrawMarkerSymbolIM\n<BR>");

  /* skip this, we don't do text */
346 347 348 349 350 351



  if(!p) return;


352 353
  if(style->symbol > map->symbolset.numsymbols || style->symbol < 0) return; /* no such symbol, 0 is OK */
  symbol = map->symbolset.symbol[style->symbol];
354 355 356
  ox = style->offsetx*scalefactor;
  oy = style->offsety*scalefactor;
  if(style->size == -1) {
357 358 359 360
    size = msSymbolGetDefaultSize( symbol );
    size = MS_NINT(size*scalefactor);
  } else
    size = MS_NINT(style->size*scalefactor);
361 362 363 364 365
  size = MS_MAX(size, style->minsize*img->resolutionfactor);
  size = MS_MIN(size, style->maxsize*img->resolutionfactor);

  if(size < 1) return; /* size too small */

366
  /* DEBUG_IF printf(".%d.%d.%d.", symbol->type, style->symbol, fc); */
367
  if(style->symbol == 0) { /* simply draw a single pixel of the specified color */
368

369 370
    if (dxf) {
      if (dxf==2)
371 372
        im_iprintf (&imgStr, "POINT\n%.0f %.0f\n%d\n",
                    p->x + ox, p->y + oy, matchdxfcolor(style->color));
373
      else
374 375 376 377
        im_iprintf (&imgStr,
                    "  0\nPOINT\n 10\n%f\n 20\n%f\n 30\n0.0\n"
                    " 62\n%6d\n  8\n%s\n",
                    p->x + ox, p->y + oy, matchdxfcolor(style->color), lname);
378 379 380
    } else {
      im_iprintf (&imgStr, "<area ");
      if (strcmp(symbolHrefFmt,"%.s")!=0) {
381 382 383
        im_iprintf (&imgStr, "href=\"");
        im_iprintf (&imgStr, symbolHrefFmt, lname);
        im_iprintf (&imgStr, "\" ");
384 385
      }
      if (strcmp(symbolMOverFmt,"%.s")!=0) {
386 387 388
        im_iprintf (&imgStr, "onMouseOver=\"");
        im_iprintf (&imgStr, symbolMOverFmt, lname);
        im_iprintf (&imgStr, "\" ");
389 390
      }
      if (strcmp(symbolMOutFmt,"%.s")!=0) {
391 392 393
        im_iprintf (&imgStr, "onMouseOut=\"");
        im_iprintf (&imgStr, symbolMOutFmt, lname);
        im_iprintf (&imgStr, "\" ");
394 395
      }
      im_iprintf (&imgStr, "shape=\"circle\" coords=\"%.0f,%.0f, 3\" />\n",
396
                  p->x + ox, p->y + oy);
397 398
    }

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    /* point2 = &( p->line[j].point[i] ); */
    /* if(point1->y == point2->y) {} */
    return;
  }
  DEBUG_IF printf("A");
  switch(symbol->type) {
    case(MS_SYMBOL_TRUETYPE):
      DEBUG_IF printf("T");
      break;
    case(MS_SYMBOL_PIXMAP):
      DEBUG_IF printf("P");
      break;
    case(MS_SYMBOL_VECTOR):
      DEBUG_IF printf("V");
      {
        double d, offset_x, offset_y;

        d = size/symbol->sizey;
        offset_x = MS_NINT(p->x - d*.5*symbol->sizex + ox);
        offset_y = MS_NINT(p->y - d*.5*symbol->sizey + oy);

        /* For now I only render filled vector symbols in imagemap, and no */
        /* dxf support available yet.  */
        if(symbol->filled && !dxf /* && fc >= 0 */ ) {
          /* char *title=(p->numvalues) ? p->values[0] : ""; */
          char *title = "";
          int  j;

          im_iprintf (&imgStr, "<area ");
          if (strcmp(symbolHrefFmt,"%.s")!=0) {
            im_iprintf (&imgStr, "href=\"");
            im_iprintf (&imgStr, symbolHrefFmt, lname);
            im_iprintf (&imgStr, "\" ");
          }
          if (strcmp(symbolMOverFmt,"%.s")!=0) {
            im_iprintf (&imgStr, "onMouseOver=\"");
            im_iprintf (&imgStr, symbolMOverFmt, lname);
            im_iprintf (&imgStr, "\" ");
          }
          if (strcmp(symbolMOutFmt,"%.s")!=0) {
            im_iprintf (&imgStr, "onMouseOut=\"");
            im_iprintf (&imgStr, symbolMOutFmt, lname);
            im_iprintf (&imgStr, "\" ");
          }
443

444
          im_iprintf (&imgStr, "title=\"%s\" shape=\"poly\" coords=\"", title);
445

446 447 448 449 450 451 452
          for(j=0; j < symbol->numpoints; j++) {
            im_iprintf (&imgStr, "%s %d,%d", j == 0 ? "": ",",
                        MS_NINT(d*symbol->points[j].x + offset_x),
                        MS_NINT(d*symbol->points[j].y + offset_y) );
          }
          im_iprintf (&imgStr, "\" />\n");
        } /* end of imagemap, filled case. */
453
      }
454
      break;
455

456 457 458
    default:
      DEBUG_IF printf("DEF");
      break;
459 460 461 462 463 464 465 466
  } /* end switch statement */

  return;
}

/* ------------------------------------------------------------------------- */
/* Draw a line symbol of the specified size and color                        */
/* ------------------------------------------------------------------------- */
467
void msDrawLineSymbolIM(mapObj *map, imageObj* img, shapeObj *p, styleObj *style, double scalefactor)
468 469 470
{
  symbolObj *symbol;
  int i,j,l;
471 472 473
  char first = 1;
  double size;
  DEBUG_IF printf("msDrawLineSymbolIM<BR>\n");
474 475 476 477 478


  if(!p) return;
  if(p->numlines <= 0) return;

479 480
  if(style->symbol > map->symbolset.numsymbols || style->symbol < 0) return; /* no such symbol, 0 is OK */
  symbol = map->symbolset.symbol[style->symbol];
481
  if(style->size == -1) {
482 483 484 485
    size = msSymbolGetDefaultSize( symbol );
    size = MS_NINT(size*scalefactor);
  } else
    size = MS_NINT(style->size*scalefactor);
486 487 488 489 490
  size = MS_MAX(size, style->minsize*img->resolutionfactor);
  size = MS_MIN(size, style->maxsize*img->resolutionfactor);

  if (suppressEmpty && p->numvalues==0) return;/* suppress area with empty title */
  if(style->symbol == 0) { /* just draw a single width line */
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    for(l=0,j=0; j<p->numlines; j++) {
      if (dxf == 2) {
        im_iprintf (&imgStr, "LINE\n%d\n", matchdxfcolor(style->color));
      } else if (dxf) {
        im_iprintf (&imgStr, "  0\nPOLYLINE\n 70\n     0\n 62\n%6d\n  8\n%s\n", matchdxfcolor(style->color), lname);
      } else {
        char *title;

        first = 1;
        title=(p->numvalues) ? p->values[0] : "";
        im_iprintf (&imgStr, "<area ");
        if (strcmp(polyHrefFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "href=\"");
          im_iprintf (&imgStr, polyHrefFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        if (strcmp(polyMOverFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "onMouseOver=\"");
          im_iprintf (&imgStr, polyMOverFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        if (strcmp(polyMOutFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "onMouseOut=\"");
          im_iprintf (&imgStr, polyMOutFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        im_iprintf (&imgStr, "title=\"%s\" shape=\"poly\" coords=\"", title);
518
      }
519 520 521 522 523 524 525 526 527 528
      /* point1 = &( p->line[j].point[p->line[j].numpoints-1] ); */
      for(i=0; i < p->line[j].numpoints; i++,l++) {
        if (dxf == 2) {
          im_iprintf (&imgStr, "%.0f %.0f\n", p->line[j].point[i].x, p->line[j].point[i].y);
        } else if (dxf) {
          im_iprintf (&imgStr, "  0\nVERTEX\n 10\n%f\n 20\n%f\n 30\n%f\n", p->line[j].point[i].x, p->line[j].point[i].y, 0.0);
        } else {
          im_iprintf (&imgStr, "%s %.0f,%.0f", first ? "": ",", p->line[j].point[i].x, p->line[j].point[i].y);
        }
        first = 0;
529

530 531 532 533
        /* point2 = &( p->line[j].point[i] ); */
        /* if(point1->y == point2->y) {} */
      }
      im_iprintf (&imgStr, dxf ? (dxf == 2 ? "": "  0\nSEQEND\n") : "\" />\n");
534 535
    }

536 537
    /* DEBUG_IF printf ("%d, ",strlen(img->img.imagemap) ); */
    return;
538
  }
539 540 541

  DEBUG_IF printf("-%d-",symbol->type);

542 543 544 545 546 547 548
  return;
}


/* ------------------------------------------------------------------------- */
/* Draw a shade symbol of the specified size and color                       */
/* ------------------------------------------------------------------------- */
549
void msDrawShadeSymbolIM(mapObj *map, imageObj* img, shapeObj *p, styleObj *style, double scalefactor)
550 551 552
{
  symbolObj *symbol;
  int i,j,l;
553 554
  char first = 1;
  double size;
555

556
  DEBUG_IF printf("msDrawShadeSymbolIM\n<BR>");
557 558 559
  if(!p) return;
  if(p->numlines <= 0) return;

560
  symbol = map->symbolset.symbol[style->symbol];
561
  if(style->size == -1) {
562 563 564 565
    size = msSymbolGetDefaultSize( symbol );
    size = MS_NINT(size*scalefactor);
  } else
    size = MS_NINT(style->size*scalefactor);
566 567 568 569
  size = MS_MAX(size, style->minsize*img->resolutionfactor);
  size = MS_MIN(size, style->maxsize*img->resolutionfactor);

  if (suppressEmpty && p->numvalues==0) return;/* suppress area with empty title */
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  if(style->symbol == 0) { /* simply draw a single pixel of the specified color //     */
    for(l=0,j=0; j<p->numlines; j++) {
      if (dxf == 2) {
        im_iprintf (&imgStr, "POLY\n%d\n", matchdxfcolor(style->color));
      } else if (dxf) {
        im_iprintf (&imgStr, "  0\nPOLYLINE\n 73\n     1\n 62\n%6d\n  8\n%s\n", matchdxfcolor(style->color), lname);
      } else {
        char *title=(p->numvalues) ? p->values[0] : "";
        first = 1;
        im_iprintf (&imgStr, "<area ");
        if (strcmp(polyHrefFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "href=\"");
          im_iprintf (&imgStr, polyHrefFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        if (strcmp(polyMOverFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "onMouseOver=\"");
          im_iprintf (&imgStr, polyMOverFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        if (strcmp(polyMOutFmt,"%.s")!=0) {
          im_iprintf (&imgStr, "onMouseOut=\"");
          im_iprintf (&imgStr, polyMOutFmt, title);
          im_iprintf (&imgStr, "\" ");
        }
        im_iprintf (&imgStr, "title=\"%s\" shape=\"poly\" coords=\"", title);
      }
597

598 599 600 601 602 603 604 605 606 607
      /* point1 = &( p->line[j].point[p->line[j].numpoints-1] ); */
      for(i=0; i < p->line[j].numpoints; i++,l++) {
        if (dxf == 2) {
          im_iprintf (&imgStr, "%.0f %.0f\n", p->line[j].point[i].x, p->line[j].point[i].y);
        } else if (dxf) {
          im_iprintf (&imgStr, "  0\nVERTEX\n 10\n%f\n 20\n%f\n 30\n%f\n", p->line[j].point[i].x, p->line[j].point[i].y, 0.0);
        } else {
          im_iprintf (&imgStr, "%s %.0f,%.0f", first ? "": ",", p->line[j].point[i].x, p->line[j].point[i].y);
        }
        first = 0;
608

609 610
        /* point2 = &( p->line[j].point[i] ); */
        /* if(point1->y == point2->y) {} */
611
      }
612
      im_iprintf (&imgStr, dxf ? (dxf == 2 ? "": "  0\nSEQEND\n") : "\" />\n");
613 614
    }

615
    return;
616
  }
617 618
  /* DEBUG_IF printf ("d"); */
  DEBUG_IF printf("-%d-",symbol->type);
619 620 621 622 623 624
  return;
}

/*
 * Simply draws a label based on the label point and the supplied label object.
 */
625
int msDrawTextIM(mapObj *map, imageObj* img, pointObj labelPnt, char *string, labelObj *label, double scalefactor)
626 627 628 629 630 631
{
  DEBUG_IF printf("msDrawText<BR>\n");
  if(!string) return(0); /* not errors, just don't want to do anything */
  if(strlen(string) == 0) return(0);
  if(!dxf) return (0);

632 633 634 635
  if (dxf == 2) {
    im_iprintf (&imgStr, "TEXT\n%d\n%s\n%.0f\n%.0f\n%.0f\n" , matchdxfcolor(label->color), string, labelPnt.x, labelPnt.y, -label->angle);
  } else if (dxf) {
    im_iprintf (&imgStr, "  0\nTEXT\n  1\n%s\n 10\n%f\n 20\n%f\n 30\n0.0\n 40\n%f\n 50\n%f\n 62\n%6d\n  8\n%s\n 73\n   2\n 72\n   1\n" , string, labelPnt.x, labelPnt.y, label->size * scalefactor *100, -label->angle, matchdxfcolor(label->color), lname);
636 637 638 639 640 641 642 643 644 645 646
  }
  return(0);
}

/*
 * Save an image to a file named filename, if filename is NULL it goes to stdout
 */

int msSaveImageIM(imageObj* img, char *filename, outputFormatObj *format )

{
647 648 649
  FILE *stream;
  char workbuffer[5000];
  int nSize=0, size=0, iIndice=0;
650

651
  DEBUG_IF printf("msSaveImageIM\n<BR>");
652

653
  if(filename != NULL && strlen(filename) > 0) {
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    stream = fopen(filename, "wb");
    if(!stream) {
      msSetError(MS_IOERR, "(%s)", "msSaveImage()", filename);
      return(MS_FAILURE);
    }
  } else { /* use stdout */

#ifdef _WIN32
    /*
     * Change stdout mode to binary on win32 platforms
     */
    if(_setmode( _fileno(stdout), _O_BINARY) == -1) {
      msSetError(MS_IOERR, "Unable to change stdout to binary mode.", "msSaveImage()");
      return(MS_FAILURE);
    }
#endif
    stream = stdout;
  }

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  if( strcasecmp(format->driver,"imagemap") == 0 ) {
    DEBUG_IF printf("ALLOCD %d<BR>\n", img->size);
    /* DEBUG_IF printf("F %s<BR>\n", img->img.imagemap); */
    DEBUG_IF printf("FLEN %d<BR>\n", (int)strlen(img->img.imagemap));
    if (dxf == 2) {
      msIO_fprintf(stream, "%s", layerlist);
    } else if (dxf) {
      msIO_fprintf(stream, "  0\nSECTION\n  2\nHEADER\n  9\n$ACADVER\n  1\nAC1009\n0\nENDSEC\n  0\nSECTION\n  2\nTABLES\n  0\nTABLE\n%s0\nENDTAB\n0\nENDSEC\n  0\nSECTION\n  2\nBLOCKS\n0\nENDSEC\n  0\nSECTION\n  2\nENTITIES\n", layerlist);
    } else {
      msIO_fprintf(stream, "<map name=\"%s\" width=\"%d\" height=\"%d\">\n", mapName, img->width, img->height);
    }
    nSize = sizeof(workbuffer);

    size = strlen(img->img.imagemap);
    if (size > nSize) {
      iIndice = 0;
      while ((iIndice + nSize) <= size) {
        snprintf(workbuffer, sizeof(workbuffer), "%s", img->img.imagemap+iIndice );
        workbuffer[nSize-1] = '\0';
        msIO_fwrite(workbuffer, strlen(workbuffer), 1, stream);
        iIndice +=nSize-1;
      }
      if (iIndice < size) {
        sprintf(workbuffer, "%s", img->img.imagemap+iIndice );
697
        msIO_fprintf(stream, "%s", workbuffer);
698 699 700 701 702 703 704 705 706 707 708 709 710 711
      }
    } else
      msIO_fwrite(img->img.imagemap, size, 1, stream);
    if( strcasecmp("OFF",msGetOutputFormatOption( format, "SKIPENDTAG", "OFF" )) == 0) {
      if (dxf == 2)
        msIO_fprintf(stream, "END");
      else if (dxf)
        msIO_fprintf(stream, "0\nENDSEC\n0\nEOF\n");
      else
        msIO_fprintf(stream, "</map>");
    }
  } else {
    msSetError(MS_MISCERR, "Unknown output image type driver: %s.",
               "msSaveImage()", format->driver );
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    return(MS_FAILURE);
  }

  if(filename != NULL && strlen(filename) > 0) fclose(stream);
  return(MS_SUCCESS);
}


/*
 * Free gdImagePtr
 */
void msFreeImageIM(imageObj* img)
{
  free(img->img.imagemap);
}