| 362 | } |
| 363 | |
| 364 | int saveAsPNG(mapObj *map,rasterBufferObj *rb, streamInfo *info, outputFormatObj *format) { |
| 365 | int force_pc256 = MS_FALSE; |
| 366 | int force_palette = MS_FALSE; |
| 367 | |
| 368 | int ret = MS_FAILURE; |
| 369 | |
| 370 | const char *force_string,*zlib_compression; |
| 371 | int compression = -1; |
| 372 | |
| 373 | zlib_compression = msGetOutputFormatOption( format, "COMPRESSION", NULL); |
| 374 | if(zlib_compression && *zlib_compression) { |
| 375 | char *endptr; |
| 376 | compression = strtol(zlib_compression,&endptr,10); |
| 377 | if(*endptr || compression<-1 || compression>9) { |
| 378 | msSetError(MS_MISCERR,"failed to parse FORMATOPTION \"COMPRESSION=%s\", expecting integer from 0 to 9.","saveAsPNG()",zlib_compression); |
| 379 | return MS_FAILURE; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
| 384 | force_string = msGetOutputFormatOption( format, "QUANTIZE_FORCE", NULL ); |
| 385 | if( force_string && (strcasecmp(force_string,"on") == 0 || strcasecmp(force_string,"yes") == 0 || strcasecmp(force_string,"true") == 0) ) |
| 386 | force_pc256 = MS_TRUE; |
| 387 | |
| 388 | force_string = msGetOutputFormatOption( format, "PALETTE_FORCE", NULL ); |
| 389 | if( force_string && (strcasecmp(force_string,"on") == 0 || strcasecmp(force_string,"yes") == 0 || strcasecmp(force_string,"true") == 0) ) |
| 390 | force_palette = MS_TRUE; |
| 391 | |
| 392 | if(force_pc256 || force_palette) { |
| 393 | rasterBufferObj qrb; |
| 394 | rgbaPixel palette[256], paletteGiven[256]; |
| 395 | unsigned int numPaletteGivenEntries; |
| 396 | memset(&qrb,0,sizeof(rasterBufferObj)); |
| 397 | qrb.type = MS_BUFFER_BYTE_PALETTE; |
| 398 | qrb.width = rb->width; |
| 399 | qrb.height = rb->height; |
| 400 | qrb.data.palette.pixels = (unsigned char*)malloc(qrb.width*qrb.height*sizeof(unsigned char)); |
| 401 | qrb.data.palette.scaling_maxval = 255; |
| 402 | if(force_pc256) { |
| 403 | qrb.data.palette.palette = palette; |
| 404 | qrb.data.palette.num_entries = atoi(msGetOutputFormatOption( format, "QUANTIZE_COLORS", "256")); |
| 405 | ret = msQuantizeRasterBuffer(rb,&(qrb.data.palette.num_entries),qrb.data.palette.palette, |
| 406 | NULL, 0, |
| 407 | &qrb.data.palette.scaling_maxval); |
| 408 | } else { |
| 409 | int colorsWanted = atoi(msGetOutputFormatOption( format, "QUANTIZE_COLORS", "0")); |
| 410 | const char *palettePath = msGetOutputFormatOption( format, "PALETTE", "palette.txt"); |
| 411 | char szPath[MS_MAXPATHLEN]; |
| 412 | if(map) { |
| 413 | msBuildPath(szPath, map->mappath, palettePath); |
| 414 | palettePath = szPath; |
| 415 | } |
| 416 | if(readPalette(palettePath,paletteGiven,&numPaletteGivenEntries,format->transparent) != MS_SUCCESS) { |
| 417 | return MS_FAILURE; |
| 418 | } |
| 419 | |
| 420 | if(numPaletteGivenEntries == 256 || colorsWanted == 0) { |
| 421 | qrb.data.palette.palette = paletteGiven; |
no test coverage detected