mapoutput.c 37.8 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Project:  MapServer
 * Purpose:  Various support code related to the outputFormatObj.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2002, Frank Warmerdam
 *
 * 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
 * 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 <assert.h>
#include "mapserver.h"


34 35 36

static outputFormatObj *msAllocOutputFormat( mapObj *map, const char *name,
    const char *driver );
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/*************************************************************************

NOTES on outputFormatObj:

typedef struct {
  char *name;
  char *mimetype;
  char *driver;
  int  imagemode; // MS_IMAGEMODE_* value.
  int  transparent;
  int  numformatoptions;
  char **formatoptions;
} outputFormatObj;

 NAME - Associates an internal name with the declaration.  The value used
        has not intrinsic meaning and is just used to associate with the
        MAP level IMAGETYPE.  It is also the "name" used for the format
        in WMS capabilities documents, and FORMAT= requests. (required)

 MIMETYPE - The mime type to use for this format.  If omitted, the value
            will be derived from the DRIVER or default to the value for
            untyped binary data will be used.  (optional - may be NULL)

61
 DRIVER - This indicates which internal driver mechanism is to be used.
62 63 64 65 66
          Anything prefixed by "GDAL/" will be handled by the GDAL driver, with
          the remainder taken as the GDAL format name. (required)

 IMAGEMODE - Has one of "PC256", "RGB", or "RGBA" indicating whether
             the imaging should be done to a 256 pseudo-colored, 24bit RGB, or
67 68 69 70
             32bit RGBA (A=alpha/transparency) result image.  Note that the
             IMAGEMODE actually affects how all the rendering for the map is
             done, long before it is output to a particular output format.
             The default value is the traditional "PC256".  Some output
71 72
             formats can only support some IMAGEMODE values. (optional)

73 74
             Translate as MS_IMAGEMODE_PC256, MS_IMAGEMODE_RGB and
             MS_IMAGEMODE_RGBA.
75 76 77 78

             Not too sure what this should be set to for output formats like
             flash and SVG.

79
 TRANSPARENT - A value of "ON" or "OFF" indicating whether transparency
80 81 82
               support should be enabled.  Same as the old TRANSPARENT flag
               at the MAP level.

83 84
 FORMATOPTION - Contains an argument for the specific driver used.  There may
                be any number of format options for a given OUTPUTFORMAT
85
                declaration.  FORMATOPTION will be used to encode the older
86
                INTERLACE, and QUALITY values.
87 88 89 90 91

                Handled as a MapServer style CharArray.

 *************************************************************************/

92

93 94 95 96
struct defaultOutputFormatEntry {
  const char *name;
  const char *driver;
  const char *mimetype;
97 98 99
} ;

struct defaultOutputFormatEntry defaultoutputformats[] = {
100 101 102 103 104 105 106
  {"png","AGG/PNG","image/png"},
  {"jpeg","AGG/JPEG","image/jpeg"},
#ifdef USE_GD
  {"gif","GD/GIF","image/gif"},
#endif
  {"png8","AGG/PNG8","image/png; mode=8bit"},
  {"png24","AGG/PNG","image/png; mode=24bit"},
107
#ifdef USE_CAIRO
108 109 110
  {"pdf","CAIRO/PDF","application/x-pdf"},
  {"svg","CAIRO/SVG","image/svg+xml"},
  {"cairopng","CAIRO/PNG","image/png"},
111 112
#endif
#ifdef USE_GDAL
113
  {"GTiff","GDAL/GTiff","image/tiff"},
114 115
#endif
#ifdef USE_KML
116 117
  {"kml","KML","application/vnd.google-earth.kml+xml"},
  {"kmz","KMZ","application/vnd.google-earth.kmz"},
118
#endif
119
  {NULL,NULL,NULL}
120 121
};

122 123 124 125 126 127 128
/************************************************************************/
/*                  msPostMapParseOutputFormatSetup()                   */
/************************************************************************/

int msPostMapParseOutputFormatSetup( mapObj *map )

{
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  outputFormatObj *format;

  /* If IMAGETYPE not set use the first user defined OUTPUTFORMAT.
     If none, use the first default format. */
  if( map->imagetype == NULL && map->numoutputformats > 0 )
    map->imagetype = msStrdup(map->outputformatlist[0]->name);
  if( map->imagetype == NULL)
    map->imagetype = msStrdup(defaultoutputformats[0].name);

  /* select the current outputformat into map->outputformat */
  format = msSelectOutputFormat( map, map->imagetype );
  if( format == NULL ) {
    msSetError(MS_MISCERR,
               "Unable to select IMAGETYPE `%s'.",
               "msPostMapParseOutputFormatSetup()",
               map->imagetype ? map->imagetype : "(null)" );
    return MS_FAILURE;
  }
147

148 149
  msApplyOutputFormat( &(map->outputformat), format,
                       map->transparent, map->interlace, map->imagequality );
150

151
  return MS_SUCCESS;
152 153 154 155 156 157
}

/************************************************************************/
/*                    msCreateDefaultOutputFormat()                     */
/************************************************************************/

158 159 160
outputFormatObj *msCreateDefaultOutputFormat( mapObj *map,
    const char *driver,
    const char *name )
161 162

{
163 164 165 166
  outputFormatObj *format = NULL;
  if( strcasecmp(driver,"GD/PC256") == 0 ) {
    return msCreateDefaultOutputFormat( map, "GD/PNG", "gdpng" );
  }
167

168 169
  if( strcasecmp(driver,"GD/GIF") == 0 ) {
    if(!name) name="gif";
170
#ifdef USE_GD_GIF
171 172 173 174 175
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/gif");
    format->imagemode = MS_IMAGEMODE_PC256;
    format->extension = msStrdup("gif");
    format->renderer = MS_RENDER_WITH_GD;
176
#else
177
    return msCreateDefaultOutputFormat( map, "AGG/PNG8", name );
178
#endif
179
  }
180

181 182
  if( strcasecmp(driver,"GD/PNG") == 0 ) {
    if(!name) name="gdpng";
183
#ifdef USE_GD_PNG
184 185 186 187 188 189 190
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/png");
    format->imagemode = MS_IMAGEMODE_PC256;
    format->extension = msStrdup("png");
    format->renderer = MS_RENDER_WITH_GD;
#else
    return msCreateDefaultOutputFormat( map, "AGG/PNG8", name );
191
#endif /* USE_GD_PNG */
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  }

  if( strcasecmp(driver,"AGG/PNG") == 0 ) {
    if(!name) name="png24";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/png");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("png");
    format->renderer = MS_RENDER_WITH_AGG;
  }

  if( strcasecmp(driver,"AGG/PNG8") == 0 ) {
    if(!name) name="png8";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/png; mode=8bit");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("png");
    format->renderer = MS_RENDER_WITH_AGG;
    msSetOutputFormatOption( format, "QUANTIZE_FORCE", "on");
    msSetOutputFormatOption( format, "QUANTIZE_COLORS", "256");
  }

  if( strcasecmp(driver,"AGG/JPEG") == 0 ) {
    if(!name) name="jpeg";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/jpeg");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("jpg");
    format->renderer = MS_RENDER_WITH_AGG;
  }
222 223

#if defined(USE_CAIRO)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  if( strcasecmp(driver,"CAIRO/PNG") == 0 ) {
    if(!name) name="cairopng";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/png; mode=24bit");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("png");
    format->renderer = MS_RENDER_WITH_CAIRO_RASTER;
  }
  if( strcasecmp(driver,"CAIRO/JPEG") == 0 ) {
    if(!name) name="cairojpeg";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/jpeg");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("jpg");
    format->renderer = MS_RENDER_WITH_CAIRO_RASTER;
  }
  if( strcasecmp(driver,"CAIRO/PDF") == 0 ) {
    if(!name) name="pdf";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("application/x-pdf");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("pdf");
    format->renderer = MS_RENDER_WITH_CAIRO_PDF;
  }
  if( strcasecmp(driver,"CAIRO/SVG") == 0 ) {
    if(!name) name="svg";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/svg+xml");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("svg");
    format->renderer = MS_RENDER_WITH_CAIRO_SVG;
  }
256
#ifdef _WIN32
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  if( strcasecmp(driver,"CAIRO/WINGDI") == 0 ) {
    if(!name) name="cairowinGDI";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("");
    format->renderer = MS_RENDER_WITH_CAIRO_RASTER;
  }
  if( strcasecmp(driver,"CAIRO/WINGDIPRINT") == 0 ) {
    if(!name) name="cairowinGDIPrint";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("");
    format->renderer = MS_RENDER_WITH_CAIRO_RASTER;
  }
273
#endif
274 275
#endif

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
#if defined(USE_OGL)
  if( strcasecmp(driver,"OGL/PNG") == 0 ) {
    if(!name) name="oglpng24";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("image/png; mode=24bit");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("png");
    format->renderer = MS_RENDER_WITH_OGL;
  }
#endif

#if defined(USE_KML)
  if( strcasecmp(driver,"KML") == 0 ) {
    if(!name) name="kml";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("application/vnd.google-earth.kml+xml");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = msStrdup("kml");
    format->renderer = MS_RENDER_WITH_KML;
    msSetOutputFormatOption( format, "ATTACHMENT", "mapserver.kml");
  }
  if( strcasecmp(driver,"KMZ") == 0 ) {
    if(!name) name="kmz";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = strdup("application/vnd.google-earth.kmz");
    format->imagemode = MS_IMAGEMODE_RGB;
    format->extension = strdup("kmz");
    format->renderer = MS_RENDER_WITH_KML;
    msSetOutputFormatOption( format, "ATTACHMENT", "mapserver.kmz");
  }
306
#endif
307 308 309



310
#ifdef USE_GDAL
311 312 313 314 315 316 317 318 319 320 321
  if( strncasecmp(driver,"gdal/",5) == 0 ) {
    if(!name) name=driver+5;
    format = msAllocOutputFormat( map, name, driver );
    if( msInitDefaultGDALOutputFormat( format ) == MS_FAILURE ) {
      if( map != NULL ) {
        map->numoutputformats--;
        map->outputformatlist[map->numoutputformats] = NULL;
      }

      msFreeOutputFormat( format );
      format = NULL;
322
    }
323
  }
324
#endif
325
#ifdef USE_OGR
326 327 328 329 330 331 332 333
  if( strncasecmp(driver,"ogr/",4) == 0 ) {
    if(!name) name=driver+4;
    format = msAllocOutputFormat( map, name, driver );
    if( msInitDefaultOGROutputFormat( format ) == MS_FAILURE ) {
      if( map != NULL ) {
        map->numoutputformats--;
        map->outputformatlist[map->numoutputformats] = NULL;
      }
334

335 336
      msFreeOutputFormat( format );
      format = NULL;
337
    }
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  }
#endif
  if( strcasecmp(driver,"imagemap") == 0 ) {
    if(!name) name="imagemap";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("text/html; driver=imagemap");
    format->extension = msStrdup("html");
    format->imagemode = MS_IMAGEMODE_NULL;
    format->renderer = MS_RENDER_WITH_IMAGEMAP;
  }

  if( strcasecmp(driver,"template") == 0 ) {
    if(!name) name="template";
    format = msAllocOutputFormat( map, name, driver );
    format->mimetype = msStrdup("text/html");
    format->extension = msStrdup("html");
    format->imagemode = MS_IMAGEMODE_FEATURE;
    format->renderer = MS_RENDER_WITH_TEMPLATE;
  }

  if( format != NULL )
    format->inmapfile = MS_FALSE;


  return format;
363 364 365 366 367 368 369
}

/************************************************************************/
/*                    msApplyDefaultOutputFormats()                     */
/************************************************************************/


370
void msApplyDefaultOutputFormats( mapObj *map )
371
{
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  char *saved_imagetype;
  struct defaultOutputFormatEntry *defEntry;

  if( map->imagetype == NULL )
    saved_imagetype = NULL;
  else
    saved_imagetype = msStrdup(map->imagetype);

  defEntry = defaultoutputformats;
  while(defEntry->name) {
    if( msSelectOutputFormat( map, defEntry->name ) == NULL )
      msCreateDefaultOutputFormat( map, defEntry->driver, defEntry->name );
    defEntry++;
  }
  if( map->imagetype != NULL )
    free( map->imagetype );
  map->imagetype = saved_imagetype;
389 390 391 392 393 394 395 396 397
}

/************************************************************************/
/*                         msFreeOutputFormat()                         */
/************************************************************************/

void msFreeOutputFormat( outputFormatObj *format )

{
398 399
  if( format == NULL )
    return;
400 401 402 403
  if( MS_REFCNT_DECR_IS_NOT_ZERO(format) ) {
    return;
  }

404 405 406 407 408 409 410 411 412 413
  if(MS_RENDERER_PLUGIN(format) && format->vtable) {
    format->vtable->cleanup(MS_RENDERER_CACHE(format->vtable));
    free( format->vtable );
  }
  msFree( format->name );
  msFree( format->mimetype );
  msFree( format->driver );
  msFree( format->extension );
  msFreeCharArray( format->formatoptions, format->numformatoptions );
  msFree( format );
414 415 416 417 418 419
}

/************************************************************************/
/*                        msAllocOutputFormat()                         */
/************************************************************************/

420 421
static outputFormatObj *msAllocOutputFormat( mapObj *map, const char *name,
    const char *driver )
422 423

{
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  outputFormatObj *format;

  /* -------------------------------------------------------------------- */
  /*      Allocate the format object.                                     */
  /* -------------------------------------------------------------------- */
  format = (outputFormatObj *) calloc(1,sizeof(outputFormatObj));
  if( format == NULL ) {
    msSetError( MS_MEMERR, NULL, "msAllocOutputFormat()" );
    return NULL;
  }

  /* -------------------------------------------------------------------- */
  /*      Initialize various fields.                                      */
  /* -------------------------------------------------------------------- */
  format->bands = 1;
  format->name = msStrdup(name);
  format->driver = msStrdup(driver);
  format->refcount = 0;
  format->vtable = NULL;
  format->device = NULL;
  format->imagemode = MS_IMAGEMODE_RGB;

  /* -------------------------------------------------------------------- */
  /*      Attach to map.                                                  */
  /* -------------------------------------------------------------------- */
  if( map != NULL ) {
    map->numoutputformats++;
    if( map->outputformatlist == NULL )
      map->outputformatlist = (outputFormatObj **) malloc(sizeof(void*));
    else
      map->outputformatlist = (outputFormatObj **)
                              realloc(map->outputformatlist,
                                      sizeof(void*) * map->numoutputformats );
457

458 459 460
    map->outputformatlist[map->numoutputformats-1] = format;
    format->refcount++;
  }
461

462
  return format;
463 464 465 466 467 468 469 470
}

/************************************************************************/
/*                        msAppendOutputFormat()                        */
/*                                                                      */
/*      Add an output format  .                                         */
/*      http://mapserver.gis.umn.edu/bugs/show_bug.cgi?id=511           */
/************************************************************************/
471
int msAppendOutputFormat(mapObj *map, outputFormatObj *format)
472
{
473 474 475 476 477 478 479 480 481 482 483 484 485
  /* -------------------------------------------------------------------- */
  /*      Attach to map.                                                  */
  /* -------------------------------------------------------------------- */
  if (map != NULL) {
    map->numoutputformats++;
    if (map->outputformatlist == NULL)
      map->outputformatlist = (outputFormatObj **) malloc(sizeof(void*));
    else
      map->outputformatlist = (outputFormatObj **)
                              realloc(map->outputformatlist,
                                      sizeof(void*) * map->numoutputformats );

    map->outputformatlist[map->numoutputformats-1] = format;
486
    MS_REFCNT_INCR(format);
487 488 489
  }

  return map->numoutputformats;
490 491 492 493 494 495 496 497 498 499
}

/************************************************************************/
/*                        msRemoveOutputFormat()                        */
/*                                                                      */
/*      Remove an output format (by name).                              */
/*      http://mapserver.gis.umn.edu/bugs/show_bug.cgi?id=511           */
/************************************************************************/
int msRemoveOutputFormat(mapObj *map, const char *name)
{
500 501 502 503 504 505 506 507 508 509 510 511
  int i, j;
  /* -------------------------------------------------------------------- */
  /*      Detach from map.                                                */
  /* -------------------------------------------------------------------- */
  if (map != NULL) {
    if (map->outputformatlist == NULL) {
      msSetError(MS_CHILDERR, "Can't remove format from empty outputformatlist", "msRemoveOutputFormat()");
      return MS_FAILURE;
    } else {
      i = msGetOutputFormatIndex(map, name);
      if (i >= 0) {
        map->numoutputformats--;
512 513
	if(MS_REFCNT_DECR_IS_ZERO(map->outputformatlist[i]))
           msFreeOutputFormat( map->outputformatlist[i] );
514 515 516

        for (j=i; j<map->numoutputformats-1; j++) {
          map->outputformatlist[j] = map->outputformatlist[j+1];
517
        }
518 519 520 521 522
      }
      map->outputformatlist = (outputFormatObj **)
                              realloc(map->outputformatlist,
                                      sizeof(void*) * (map->numoutputformats) );
      return MS_SUCCESS;
523
    }
524 525
  }
  return MS_FAILURE;
526 527 528 529 530 531 532 533 534 535
}

/************************************************************************/
/*                       msGetOutputFormatIndex()                       */
/*                                                                      */
/*      Pulled this out of msSelectOutputFormat for use in other cases. */
/************************************************************************/

int msGetOutputFormatIndex(mapObj *map, const char *imagetype)
{
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
  int i;
  /* -------------------------------------------------------------------- */
  /*      Try to find the format in the maps list of formats, first by    */
  /*      mime type, and then by output format name.                      */
  /* -------------------------------------------------------------------- */
  for (i = 0; i < map->numoutputformats; i++) {
    if (map->outputformatlist[i]->mimetype != NULL
        && strcasecmp(imagetype,
                      map->outputformatlist[i]->mimetype) == 0 )
      return i;
  }

  for( i = 0; i < map->numoutputformats; i++ ) {
    if( strcasecmp(imagetype,map->outputformatlist[i]->name) == 0 )
      return i;
  }

  return -1;
554 555 556 557 558 559
}

/************************************************************************/
/*                        msSelectOutputFormat()                        */
/************************************************************************/

560 561


562
outputFormatObj *msSelectOutputFormat( mapObj *map,
563 564 565
                                       const char *imagetype )

{
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  int index;
  outputFormatObj *format = NULL;

  if( map == NULL || imagetype == NULL || strlen(imagetype) == 0 )
    return NULL;

  /* -------------------------------------------------------------------- */
  /*      Try to find the format in the maps list of formats, first by    */
  /*      mime type, and then by output format name.                      */
  /* -------------------------------------------------------------------- */
  index = msGetOutputFormatIndex(map, imagetype);
  if (index >= 0) {
    format = map->outputformatlist[index];
  } else {
    struct defaultOutputFormatEntry *formatEntry = defaultoutputformats;
    while(formatEntry->name) {
      if(!strcasecmp(imagetype,formatEntry->name) || !strcasecmp(imagetype,formatEntry->mimetype)) {
        format = msCreateDefaultOutputFormat( map, formatEntry->driver, formatEntry->name );
        break;
      }
      formatEntry++;
587
    }
588

589
  }
590

591 592 593 594 595
  if (format) {
    if (map->imagetype)
      free(map->imagetype);
    map->imagetype = msStrdup(format->name);
  }
596

597 598 599 600
  if( format != NULL )
    msOutputFormatValidate( format, MS_FALSE );

  return format;
601 602 603 604 605 606
}

/************************************************************************/
/*                        msApplyOutputFormat()                         */
/************************************************************************/

607
void msApplyOutputFormat( outputFormatObj **target,
608
                          outputFormatObj *format,
609 610
                          int transparent,
                          int interlaced,
611 612 613
                          int imagequality )

{
614 615 616
  int       change_needed = MS_FALSE;
  int       old_imagequality, old_interlaced;
  outputFormatObj *formatToFree = NULL;
617

618
  assert( target != NULL );
619

620
  if( *target != NULL && MS_REFCNT_DECR_IS_ZERO( (*target) ) ) {
621 622 623
    formatToFree = *target;
    *target = NULL;
  }
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  if( format == NULL ) {
    if( formatToFree )
      msFreeOutputFormat( formatToFree );
    return;
  }

  msOutputFormatValidate( format, MS_FALSE );

  /* -------------------------------------------------------------------- */
  /*      Do we need to change any values?  If not, then just apply       */
  /*      and return.                                                     */
  /* -------------------------------------------------------------------- */
  if( transparent != MS_NOOVERRIDE && !format->transparent != !transparent )
    change_needed = MS_TRUE;

  old_imagequality = atoi(msGetOutputFormatOption( format, "QUALITY", "75"));
  if( imagequality != MS_NOOVERRIDE && old_imagequality != imagequality )
    change_needed = MS_TRUE;

  old_interlaced =
    strcasecmp(msGetOutputFormatOption( format, "INTERLACE", "ON"),
               "OFF") != 0;
  if( interlaced != MS_NOOVERRIDE && !interlaced != !old_interlaced )
    change_needed = MS_TRUE;

  if( change_needed ) {
    char new_value[128];

    if( format->refcount > 0 )
      format = msCloneOutputFormat( format );

    if( transparent != MS_NOOVERRIDE ) {
      format->transparent = transparent;
      if( format->imagemode == MS_IMAGEMODE_RGB )
        format->imagemode = MS_IMAGEMODE_RGBA;
    }
661

662 663 664
    if( imagequality != MS_NOOVERRIDE && imagequality != old_imagequality ) {
      snprintf( new_value, sizeof(new_value), "%d", imagequality );
      msSetOutputFormatOption( format, "QUALITY", new_value );
665 666
    }

667 668 669 670 671
    if( interlaced != MS_NOOVERRIDE && !interlaced != !old_interlaced ) {
      if( interlaced )
        msSetOutputFormatOption( format, "INTERLACE", "ON" );
      else
        msSetOutputFormatOption( format, "INTERLACE", "OFF" );
672
    }
673
  }
674

675 676 677 678 679 680 681 682 683
  *target = format;
  format->refcount++;
  if( MS_RENDERER_PLUGIN(format) ) {
    msInitializeRendererVTable(format);
  }


  if( formatToFree )
    msFreeOutputFormat( formatToFree );
684 685 686 687 688 689 690 691 692
}

/************************************************************************/
/*                        msCloneOutputFormat()                         */
/************************************************************************/

outputFormatObj *msCloneOutputFormat( outputFormatObj *src )

{
693 694
  outputFormatObj *dst;
  int             i;
695

696
  dst = msAllocOutputFormat( NULL, src->name, src->driver );
697

698 699 700 701 702
  msFree( dst->mimetype );
  if( src->mimetype )
    dst->mimetype = msStrdup( src->mimetype );
  else
    dst->mimetype = NULL;
703

704 705 706 707 708 709 710 711
  msFree( dst->extension );
  if( src->extension )
    dst->extension = msStrdup( src->extension );
  else
    dst->extension = NULL;

  dst->imagemode = src->imagemode;
  dst->renderer = src->renderer;
712

713 714
  dst->transparent = src->transparent;
  dst->bands = src->bands;
715

716 717 718
  dst->numformatoptions = src->numformatoptions;
  dst->formatoptions = (char **)
                       malloc(sizeof(char *) * src->numformatoptions );
719

720 721
  for( i = 0; i < src->numformatoptions; i++ )
    dst->formatoptions[i] = msStrdup(src->formatoptions[i]);
722

723 724 725
  dst->inmapfile = src->inmapfile;

  return dst;
726 727 728 729 730 731 732 733 734
}

/************************************************************************/
/*                      msGetOutputFormatOption()                       */
/*                                                                      */
/*      Fetch the value of a particular option.  It is assumed the      */
/*      options are in "KEY=VALUE" format.                              */
/************************************************************************/

735 736
const char *msGetOutputFormatOption( outputFormatObj *format,
                                     const char *optionkey,
737 738 739
                                     const char *defaultresult )

{
740
  int    i, len = strlen(optionkey);
741

742 743 744 745 746
  for( i = 0; i < format->numformatoptions; i++ ) {
    if( strncasecmp(format->formatoptions[i],optionkey,len) == 0
        && format->formatoptions[i][len] == '=' )
      return format->formatoptions[i] + len + 1;
  }
747

748
  return defaultresult;
749 750 751 752 753 754 755 756 757 758
}

/************************************************************************/
/*                      msSetOutputFormatOption()                       */
/************************************************************************/

void msSetOutputFormatOption( outputFormatObj *format,
                              const char *key, const char *value )

{
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
  char *newline;
  int   i, len;

  /* -------------------------------------------------------------------- */
  /*      Format the name=value pair into a newly allocated string.       */
  /* -------------------------------------------------------------------- */
  newline = (char *) malloc(strlen(key)+strlen(value)+2);
  if( newline == NULL ) {
    assert( newline != NULL );
    return;
  }

  sprintf( newline, "%s=%s", key, value );

  /* -------------------------------------------------------------------- */
  /*      Does this key already occur?  If so replace it.                 */
  /* -------------------------------------------------------------------- */
  len = strlen(key);

  for( i = 0; i < format->numformatoptions; i++ ) {
    if( strncasecmp(format->formatoptions[i],key,len) == 0
        && format->formatoptions[i][len] == '=' ) {
      free( format->formatoptions[i] );
      format->formatoptions[i] = newline;
      return;
784
    }
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  }

  /* -------------------------------------------------------------------- */
  /*      otherwise, we need to grow the list.                            */
  /* -------------------------------------------------------------------- */
  format->numformatoptions++;
  format->formatoptions = (char **)
                          realloc( format->formatoptions,
                                   sizeof(char*) * format->numformatoptions );

  format->formatoptions[format->numformatoptions-1] = newline;

  /* -------------------------------------------------------------------- */
  /*      Capture generic value(s) we are interested in.                  */
  /* -------------------------------------------------------------------- */
  if( strcasecmp(key,"BAND_COUNT") == 0 )
    format->bands = atoi(value);
802 803 804 805 806 807 808 809 810
}

/************************************************************************/
/*                     msGetOutputFormatMimeList()                      */
/************************************************************************/

void msGetOutputFormatMimeList( mapObj *map, char **mime_list, int max_mime )

{
811 812 813 814 815
  int mime_count = 0, i;

  msApplyDefaultOutputFormats(map);
  for( i = 0; i < map->numoutputformats && mime_count < max_mime; i++ ) {
    int  j;
816

817 818 819 820 821 822 823
    if( map->outputformatlist[i]->mimetype == NULL )
      continue;

    for( j = 0; j < mime_count; j++ ) {
      if( strcasecmp(mime_list[j],
                     map->outputformatlist[i]->mimetype) == 0 )
        break;
824 825
    }

826 827 828 829 830 831
    if( j == mime_count )
      mime_list[mime_count++] = map->outputformatlist[i]->mimetype;
  }

  if( mime_count < max_mime )
    mime_list[mime_count] = NULL;
832 833 834 835 836 837 838 839
}

/************************************************************************/
/*                     msGetOutputFormatMimeList()                      */
/************************************************************************/
void msGetOutputFormatMimeListImg( mapObj *map, char **mime_list, int max_mime )

{
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  int mime_count = 0, i,j;
  const char *format_list = NULL;
  char **tokens = NULL;
  int numtokens = 0;
  outputFormatObj *format;

  msApplyDefaultOutputFormats(map);
  format_list = msOWSLookupMetadata(&(map->web.metadata), "M","getlegendgraphic_formatlist");
  if ( format_list && strlen(format_list) > 0)
    tokens = msStringSplit(format_list,  ',', &numtokens);

  if (tokens && numtokens > 0) {
    for(j = 0; j < numtokens; j++ ) {
      format = msSelectOutputFormat(map, tokens[j]);
      if (format != NULL) {
        mime_list[mime_count++] = format->mimetype;
      }
857
    }
858 859 860 861 862 863 864 865 866 867 868 869 870
    msFreeCharArray(tokens, numtokens);
  } else
    for( i = 0; i < map->numoutputformats && mime_count < max_mime; i++ ) {
      int  j;

      if( map->outputformatlist[i]->mimetype == NULL )
        continue;

      for( j = 0; j < mime_count; j++ ) {
        if( strcasecmp(mime_list[j],
                       map->outputformatlist[i]->mimetype) == 0 )
          break;
      }
871

872 873 874 875 876 877 878
      if( j == mime_count && map->outputformatlist[i]->driver &&
          (
#ifdef USE_GD
            strncasecmp(map->outputformatlist[i]->driver, "GD/", 3)==0 ||
#endif
            strncasecmp(map->outputformatlist[i]->driver, "AGG/", 4)==0))
        mime_list[mime_count++] = map->outputformatlist[i]->mimetype;
879 880
    }

881 882
  if( mime_count < max_mime )
    mime_list[mime_count] = NULL;
883 884 885 886 887 888 889 890
}

/************************************************************************/
/*                  msGetOutputFormatMimeListWMS()                      */
/************************************************************************/

void msGetOutputFormatMimeListWMS( mapObj *map, char **mime_list, int max_mime )
{
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
  int mime_count = 0, i,j;
  const char *format_list = NULL;
  char **tokens = NULL;
  int numtokens = 0;
  outputFormatObj *format;
  msApplyDefaultOutputFormats(map);
  format_list = msOWSLookupMetadata(&(map->web.metadata), "M","getmap_formatlist");
  if ( format_list && strlen(format_list) > 0)
    tokens = msStringSplit(format_list,  ',', &numtokens);

  if (tokens && numtokens > 0) {
    for(j = 0; j < numtokens; j++ ) {
      format = msSelectOutputFormat(map, tokens[j]);
      if (format != NULL) {
        mime_list[mime_count++] = format->mimetype;
      }
907
    }
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
    msFreeCharArray(tokens, numtokens);
  } else {
    for( i = 0; i < map->numoutputformats && mime_count < max_mime; i++ ) {
      int  j;

      if( map->outputformatlist[i]->mimetype == NULL )
        continue;

      for( j = 0; j < mime_count; j++ ) {
        if( strcasecmp(mime_list[j],
                       map->outputformatlist[i]->mimetype) == 0 )
          break;
      }

      if( j == mime_count && map->outputformatlist[i]->driver &&
          (
#ifdef USE_GD
            strncasecmp(map->outputformatlist[i]->driver, "GD/", 3)==0 ||
#endif
            strncasecmp(map->outputformatlist[i]->driver, "GDAL/", 5)==0 ||
            strncasecmp(map->outputformatlist[i]->driver, "AGG/", 4)==0 ||
            strcasecmp(map->outputformatlist[i]->driver, "CAIRO/SVG")==0 ||
            strcasecmp(map->outputformatlist[i]->driver, "CAIRO/PDF")==0 ||
            strcasecmp(map->outputformatlist[i]->driver, "kml")==0 ||
            strcasecmp(map->outputformatlist[i]->driver, "kmz")==0))
        mime_list[mime_count++] = map->outputformatlist[i]->mimetype;
934
    }
935 936 937
  }
  if( mime_count < max_mime )
    mime_list[mime_count] = NULL;
938 939 940 941 942 943 944 945 946 947 948
}


/************************************************************************/
/*                       msOutputFormatValidate()                       */
/*                                                                      */
/*      Do some validation of the output format, and report to debug    */
/*      output if it doesn't seem valid.  Fixup in place as best as     */
/*      possible.                                                       */
/************************************************************************/

949
int msOutputFormatValidate( outputFormatObj *format, int issue_error )
950 951

{
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
  int result = MS_TRUE;
  char *driver_ext;

  format->bands = atoi(msGetOutputFormatOption( format, "BAND_COUNT", "1" ));

  /* Enforce the requirement that JPEG be RGB and TRANSPARENT=OFF */
  driver_ext = strstr(format->driver,"/");
  if( driver_ext && ++driver_ext && !strcasecmp(driver_ext,"JPEG")) {
    if( format->transparent ) {
      if( issue_error )
        msSetError( MS_MISCERR,
                    "JPEG OUTPUTFORMAT %s has TRANSPARENT set ON, but this is not supported.\n"
                    "It has been disabled.\n",
                    "msOutputFormatValidate()", format->name );
      else
        msDebug( "JPEG OUTPUTFORMAT %s has TRANSPARENT set ON, but this is not supported.\n"
                 "It has been disabled.\n",
                 format->name );
970

971 972
      format->transparent = MS_FALSE;
      result = MS_FALSE;
973
    }
974 975 976 977 978 979 980 981 982 983
    if( format->imagemode == MS_IMAGEMODE_RGBA ) {
      if( issue_error )
        msSetError( MS_MISCERR,
                    "JPEG OUTPUTFORMAT %s has IMAGEMODE RGBA, but this is not supported.\n"
                    "IMAGEMODE forced to RGB.\n",
                    "msOutputFormatValidate()", format->name );
      else
        msDebug( "JPEG OUTPUTFORMAT %s has IMAGEMODE RGBA, but this is not supported.\n"
                 "IMAGEMODE forced to RGB.\n",
                 format->name );
984

985 986
      format->imagemode = MS_IMAGEMODE_RGB;
      result = MS_FALSE;
987
    }
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  }

  if( format->transparent && format->imagemode == MS_IMAGEMODE_RGB ) {
    if( issue_error )
      msSetError( MS_MISCERR,
                  "OUTPUTFORMAT %s has TRANSPARENT set ON, but an IMAGEMODE\n"
                  "of RGB instead of RGBA.  Changing imagemode to RGBA.\n",
                  "msOutputFormatValidate()", format->name );
    else
      msDebug("OUTPUTFORMAT %s has TRANSPARENT set ON, but an IMAGEMODE\n"
              "of RGB instead of RGBA.  Changing imagemode to RGBA.\n",
              format->name );

    format->imagemode = MS_IMAGEMODE_RGBA;
    result = MS_FALSE;
  }

  /* Special checking around RAWMODE image modes. */
  if( format->imagemode == MS_IMAGEMODE_INT16
      || format->imagemode == MS_IMAGEMODE_FLOAT32
      || format->imagemode == MS_IMAGEMODE_BYTE ) {
    if( strncmp(format->driver,"GDAL/",5) != 0 ) {
      result = MS_FALSE;
      if( issue_error )
        msSetError( MS_MISCERR,
                    "OUTPUTFORMAT %s has IMAGEMODE BYTE/INT16/FLOAT32, but this is only supported for GDAL drivers.",
                    "msOutputFormatValidate()", format->name );
      else
        msDebug( "OUTPUTFORMAT %s has IMAGEMODE BYTE/INT16/FLOAT32, but this is only supported for GDAL drivers.",
1017 1018
                 format->name );
    }
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    if( format->renderer != MS_RENDER_WITH_RAWDATA ) /* see bug 724 */
      format->renderer = MS_RENDER_WITH_RAWDATA;
  }
#ifdef USE_GD
  if(format->renderer == MS_RENDER_WITH_GD && format->imagemode != MS_IMAGEMODE_PC256) {
    if( issue_error )
      msSetError( MS_MISCERR,
                  "OUTPUTFORMAT %s has IMAGEMODE RGB/RGBA, which is not supported for GD drivers.",
                  "msOutputFormatValidate()", format->name );
    else
      msDebug( "OUTPUTFORMAT %s has IMAGEMODE RGB/RGBA, which is not supported for GD drivers.",
               format->name );
    format->renderer = MS_RENDER_WITH_AGG;
  }
#endif

  return result;
1037 1038
}

1039 1040 1041 1042
/************************************************************************/
/*                     msInitializeRendererVTable()                     */
/************************************************************************/

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
int msInitializeRendererVTable(outputFormatObj *format)
{
  assert(format);
  if(format->vtable) {
    return MS_SUCCESS;
  }
  format->vtable = (rendererVTableObj*)calloc(1,sizeof(rendererVTableObj));

  msInitializeDummyRenderer(format->vtable);

  switch(format->renderer) {
    case MS_RENDER_WITH_AGG:
      return msPopulateRendererVTableAGG(format->vtable);
#ifdef USE_GD
    case MS_RENDER_WITH_GD:
      return msPopulateRendererVTableGD(format->vtable);
#endif
1060
#ifdef USE_CAIRO
1061 1062 1063 1064 1065 1066
    case MS_RENDER_WITH_CAIRO_RASTER:
      return msPopulateRendererVTableCairoRaster(format->vtable);
    case MS_RENDER_WITH_CAIRO_PDF:
      return msPopulateRendererVTableCairoPDF(format->vtable);
    case MS_RENDER_WITH_CAIRO_SVG:
      return msPopulateRendererVTableCairoSVG(format->vtable);
1067 1068
#endif
#ifdef USE_OGL
1069 1070
    case MS_RENDER_WITH_OGL:
      return msPopulateRendererVTableOGL(format->vtable);
1071 1072
#endif
#ifdef USE_KML
1073 1074
    case MS_RENDER_WITH_KML:
      return msPopulateRendererVTableKML(format->vtable);
1075 1076
#endif
#ifdef USE_OGR
1077 1078
    case MS_RENDER_WITH_OGR:
      return msPopulateRendererVTableOGR(format->vtable);
1079
#endif
1080 1081 1082 1083 1084 1085 1086
    default:
      msSetError(MS_MISCERR, "unsupported RendererVtable renderer %d",
                 "msInitializeRendererVTable()",format->renderer);
      return MS_FAILURE;
  }
  /* this code should never be executed */
  return MS_FAILURE;
1087 1088
}