forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapimageio.c
1114 lines (970 loc) · 36 KB
/
mapimageio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
76
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
222
223
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Low level PNG/JPEG/GIF image io native functions
* Author: Thomas Bonfort (tbonfort)
*
******************************************************************************
* Copyright (c) 2009 Thomas Bonfort
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* 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 <png.h>
#include <setjmp.h>
#include <assert.h>
#include <jpeglib.h>
#include <stdlib.h>
#ifdef USE_GIF
#include <gif_lib.h>
#endif
typedef struct _streamInfo {
FILE *fp;
bufferObj *buffer;
} streamInfo;
void png_write_data_to_stream(png_structp png_ptr, png_bytep data, png_size_t length)
{
FILE *fp = ((streamInfo*)png_get_io_ptr(png_ptr))->fp;
msIO_fwrite(data,length,1,fp);
}
void png_write_data_to_buffer(png_structp png_ptr, png_bytep data, png_size_t length)
{
bufferObj *buffer = ((streamInfo*)png_get_io_ptr(png_ptr))->buffer;
msBufferAppend(buffer,data,length);
}
void png_flush_data(png_structp png_ptr)
{
/* do nothing */
}
typedef struct {
struct jpeg_destination_mgr pub;
unsigned char *data;
} ms_destination_mgr;
typedef struct {
ms_destination_mgr mgr;
FILE *stream;
} ms_stream_destination_mgr;
typedef struct {
ms_destination_mgr mgr;
bufferObj *buffer;
} ms_buffer_destination_mgr;
#define OUTPUT_BUF_SIZE 4096
void
jpeg_init_destination (j_compress_ptr cinfo)
{
ms_destination_mgr *dest = (ms_destination_mgr*) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->data = (unsigned char *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof (unsigned char));
dest->pub.next_output_byte = dest->data;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
void jpeg_stream_term_destination (j_compress_ptr cinfo)
{
ms_stream_destination_mgr *dest = (ms_stream_destination_mgr*) cinfo->dest;
msIO_fwrite(dest->mgr.data, OUTPUT_BUF_SIZE-dest->mgr.pub.free_in_buffer, 1, dest->stream);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
void jpeg_buffer_term_destination (j_compress_ptr cinfo)
{
ms_buffer_destination_mgr *dest = (ms_buffer_destination_mgr*) cinfo->dest;
msBufferAppend(dest->buffer, dest->mgr.data, OUTPUT_BUF_SIZE-dest->mgr.pub.free_in_buffer);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
int jpeg_stream_empty_output_buffer (j_compress_ptr cinfo)
{
ms_stream_destination_mgr *dest = (ms_stream_destination_mgr*) cinfo->dest;
msIO_fwrite(dest->mgr.data, OUTPUT_BUF_SIZE, 1, dest->stream);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
int jpeg_buffer_empty_output_buffer (j_compress_ptr cinfo)
{
ms_buffer_destination_mgr *dest = (ms_buffer_destination_mgr*) cinfo->dest;
msBufferAppend(dest->buffer, dest->mgr.data, OUTPUT_BUF_SIZE);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
static void msJPEGErrorExit(j_common_ptr cinfo)
{
jmp_buf* pJmpBuffer = (jmp_buf* ) cinfo->client_data;
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);
msSetError(MS_MISCERR,"libjpeg: %s","jpeg_ErrorExit()", buffer);
/* Return control to the setjmp point */
longjmp(*pJmpBuffer, 1);
}
int saveAsJPEG(mapObj *map, rasterBufferObj *rb, streamInfo *info,
outputFormatObj *format)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int quality;
const char* pszOptimized;
int optimized;
int arithmetic;
ms_destination_mgr *dest;
JSAMPLE *rowdata = NULL;
unsigned int row;
jmp_buf setjmp_buffer;
quality = atoi(msGetOutputFormatOption( format, "QUALITY", "75"));
pszOptimized = msGetOutputFormatOption( format, "OPTIMIZED", "YES");
optimized = EQUAL(pszOptimized, "YES") || EQUAL(pszOptimized, "ON") ||
EQUAL(pszOptimized, "TRUE");
arithmetic = EQUAL(pszOptimized, "ARITHMETIC");
if (setjmp(setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
free(rowdata);
return MS_FAILURE;
}
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = msJPEGErrorExit;
cinfo.client_data = (void *) &(setjmp_buffer);
jpeg_create_compress(&cinfo);
if (cinfo.dest == NULL) {
if(info->fp) {
cinfo.dest = (struct jpeg_destination_mgr *)
(*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
sizeof (ms_stream_destination_mgr));
((ms_stream_destination_mgr*)cinfo.dest)->mgr.pub.empty_output_buffer = jpeg_stream_empty_output_buffer;
((ms_stream_destination_mgr*)cinfo.dest)->mgr.pub.term_destination = jpeg_stream_term_destination;
((ms_stream_destination_mgr*)cinfo.dest)->stream = info->fp;
} else {
cinfo.dest = (struct jpeg_destination_mgr *)
(*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
sizeof (ms_buffer_destination_mgr));
((ms_buffer_destination_mgr*)cinfo.dest)->mgr.pub.empty_output_buffer = jpeg_buffer_empty_output_buffer;
((ms_buffer_destination_mgr*)cinfo.dest)->mgr.pub.term_destination = jpeg_buffer_term_destination;
((ms_buffer_destination_mgr*)cinfo.dest)->buffer = info->buffer;
}
}
dest = (ms_destination_mgr*) cinfo.dest;
dest->pub.init_destination = jpeg_init_destination;
cinfo.image_width = rb->width;
cinfo.image_height = rb->height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
if( arithmetic )
cinfo.arith_code = TRUE;
else if( optimized )
cinfo.optimize_coding = TRUE;
if( arithmetic || optimized ) {
if (map == NULL || msGetConfigOption(map, "JPEGMEM") == NULL) {
/* If the user doesn't provide a value for JPEGMEM, we want to be sure */
/* that at least the image size will be used before creating the temporary file */
cinfo.mem->max_memory_to_use =
MAX(cinfo.mem->max_memory_to_use, cinfo.input_components * rb->width * rb->height);
}
}
jpeg_start_compress(&cinfo, TRUE);
rowdata = (JSAMPLE*)malloc(rb->width*cinfo.input_components*sizeof(JSAMPLE));
for(row=0; row<rb->height; row++) {
JSAMPLE *pixptr = rowdata;
int col;
unsigned char *r,*g,*b;
r=rb->data.rgba.r+row*rb->data.rgba.row_step;
g=rb->data.rgba.g+row*rb->data.rgba.row_step;
b=rb->data.rgba.b+row*rb->data.rgba.row_step;
for(col=0; col<rb->width; col++) {
*(pixptr++) = *r;
*(pixptr++) = *g;
*(pixptr++) = *b;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
(void) jpeg_write_scanlines(&cinfo, &rowdata, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
free(rowdata);
return MS_SUCCESS;
}
/*
* sort a given list of rgba entries so that all the opaque pixels are at the end
*/
int remapPaletteForPNG(rasterBufferObj *rb, rgbPixel *rgb, unsigned char *a, int *num_a)
{
int bot_idx, top_idx, x;
int remap[256];
unsigned int maxval = rb->data.palette.scaling_maxval;
assert(rb->type == MS_BUFFER_BYTE_PALETTE);
/*
** remap the palette colors so that all entries with
** the maximal alpha value (i.e., fully opaque) are at the end and can
** therefore be omitted from the tRNS chunk. Note that the ordering of
** opaque entries is reversed from how they were previously arranged
** --not that this should matter to anyone.
*/
for (top_idx = rb->data.palette.num_entries-1, bot_idx = x = 0; x < rb->data.palette.num_entries; ++x) {
if (rb->data.palette.palette[x].a == maxval)
remap[x] = top_idx--;
else
remap[x] = bot_idx++;
}
/* sanity check: top and bottom indices should have just crossed paths */
if (bot_idx != top_idx + 1) {
msSetError(MS_MISCERR,"quantization sanity check failed","createPNGPalette()");
return MS_FAILURE;
}
*num_a = bot_idx;
for(x=0; x<rb->width*rb->height; x++)
rb->data.palette.pixels[x] = remap[rb->data.palette.pixels[x]];
for (x = 0; x < rb->data.palette.num_entries; ++x) {
if(maxval == 255) {
a[remap[x]] = rb->data.palette.palette[x].a;
rgb[remap[x]].r = rb->data.palette.palette[x].r;
rgb[remap[x]].g = rb->data.palette.palette[x].g;
rgb[remap[x]].b = rb->data.palette.palette[x].b;
} else {
/* rescale palette */
rgb[remap[x]].r = (rb->data.palette.palette[x].r * 255 + (maxval >> 1)) / maxval;
rgb[remap[x]].g = (rb->data.palette.palette[x].g * 255 + (maxval >> 1)) / maxval;
rgb[remap[x]].b = (rb->data.palette.palette[x].b * 255 + (maxval >> 1)) / maxval;
a[remap[x]] = (rb->data.palette.palette[x].a * 255 + (maxval >> 1)) / maxval;
}
if(a[remap[x]] != 255) {
/* un-premultiply pixels */
double da = 255.0/a[remap[x]];
rgb[remap[x]].r *= da;
rgb[remap[x]].g *= da;
rgb[remap[x]].b *= da;
}
}
return MS_SUCCESS;
}
int savePalettePNG(rasterBufferObj *rb, streamInfo *info, int compression)
{
png_infop info_ptr;
rgbPixel rgb[256];
unsigned char a[256];
int num_a;
int row,sample_depth;
png_structp png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
assert(rb->type == MS_BUFFER_BYTE_PALETTE);
if (!png_ptr)
return (MS_FAILURE);
png_set_compression_level(png_ptr, compression);
png_set_filter (png_ptr,0, PNG_FILTER_NONE);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr,
(png_infopp)NULL);
return (MS_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return (MS_FAILURE);
}
if(info->fp)
png_set_write_fn(png_ptr,info, png_write_data_to_stream, png_flush_data);
else
png_set_write_fn(png_ptr,info, png_write_data_to_buffer, png_flush_data);
if (rb->data.palette.num_entries <= 2)
sample_depth = 1;
else if (rb->data.palette.num_entries <= 4)
sample_depth = 2;
else if (rb->data.palette.num_entries <= 16)
sample_depth = 4;
else
sample_depth = 8;
png_set_IHDR(png_ptr, info_ptr, rb->width, rb->height,
sample_depth, PNG_COLOR_TYPE_PALETTE,
0, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
remapPaletteForPNG(rb,rgb,a,&num_a);
png_set_PLTE(png_ptr, info_ptr, (png_colorp)(rgb),rb->data.palette.num_entries);
if(num_a)
png_set_tRNS(png_ptr, info_ptr, a,num_a, NULL);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
for(row=0; row<rb->height; row++) {
unsigned char *rowptr = &(rb->data.palette.pixels[row*rb->width]);
png_write_row(png_ptr, rowptr);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return MS_SUCCESS;
}
int readPalette(const char *palette, rgbaPixel *entries, unsigned int *nEntries, int useAlpha)
{
FILE *stream = NULL;
char buffer[MS_BUFFER_LENGTH];
int r,g,b,a;
*nEntries = 0;
stream = fopen(palette, "r");
if(!stream) {
msSetError(MS_IOERR, "Error opening palette file %s.", "readPalette()", palette);
return MS_FAILURE;
}
while(fgets(buffer, MS_BUFFER_LENGTH, stream) && *nEntries<256) {
/* while there are colors to load */
if(buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\r')
continue; /* skip comments and blank lines */
if(!useAlpha) {
if(3 != sscanf(buffer,"%d,%d,%d\n",&r,&g,&b)) {
fclose(stream);
msSetError(MS_MISCERR,"failed to parse color %d r,g,b triplet in line \"%s\" from file %s","readPalette()",*nEntries+1,buffer,palette);
return MS_FAILURE;
}
} else {
if(4 != sscanf(buffer,"%d,%d,%d,%d\n",&r,&g,&b,&a)) {
fclose(stream);
msSetError(MS_MISCERR,"failed to parse color %d r,g,b,a quadruplet in line \"%s\" from file %s","readPalette()",*nEntries+1,buffer,palette);
return MS_FAILURE;
}
}
if(useAlpha && a != 255) {
double da = a/255.0;
entries[*nEntries].r = r * da;
entries[*nEntries].g = g * da;
entries[*nEntries].b = b * da;
entries[*nEntries].a = a;
} else {
entries[*nEntries].r = r;
entries[*nEntries].g = g;
entries[*nEntries].b = b;
entries[*nEntries].a = 255;
}
(*nEntries)++;
}
fclose(stream);
return MS_SUCCESS;
}
int saveAsPNG(mapObj *map,rasterBufferObj *rb, streamInfo *info, outputFormatObj *format)
{
int force_pc256 = MS_FALSE;
int force_palette = MS_FALSE;
int ret = MS_FAILURE;
const char *force_string,*zlib_compression;
int compression = -1;
zlib_compression = msGetOutputFormatOption( format, "COMPRESSION", NULL);
if(zlib_compression && *zlib_compression) {
char *endptr;
compression = strtol(zlib_compression,&endptr,10);
if(*endptr || compression<-1 || compression>9) {
msSetError(MS_MISCERR,"failed to parse FORMATOPTION \"COMPRESSION=%s\", expecting integer from 0 to 9.","saveAsPNG()",zlib_compression);
return MS_FAILURE;
}
}
force_string = msGetOutputFormatOption( format, "QUANTIZE_FORCE", NULL );
if( force_string && (strcasecmp(force_string,"on") == 0 || strcasecmp(force_string,"yes") == 0 || strcasecmp(force_string,"true") == 0) )
force_pc256 = MS_TRUE;
force_string = msGetOutputFormatOption( format, "PALETTE_FORCE", NULL );
if( force_string && (strcasecmp(force_string,"on") == 0 || strcasecmp(force_string,"yes") == 0 || strcasecmp(force_string,"true") == 0) )
force_palette = MS_TRUE;
if(force_pc256 || force_palette) {
rasterBufferObj qrb;
rgbaPixel palette[256], paletteGiven[256];
unsigned int numPaletteGivenEntries;
memset(&qrb,0,sizeof(rasterBufferObj));
qrb.type = MS_BUFFER_BYTE_PALETTE;
qrb.width = rb->width;
qrb.height = rb->height;
qrb.data.palette.pixels = (unsigned char*)malloc(qrb.width*qrb.height*sizeof(unsigned char));
qrb.data.palette.scaling_maxval = 255;
if(force_pc256) {
qrb.data.palette.palette = palette;
qrb.data.palette.num_entries = atoi(msGetOutputFormatOption( format, "QUANTIZE_COLORS", "256"));
ret = msQuantizeRasterBuffer(rb,&(qrb.data.palette.num_entries),qrb.data.palette.palette,
NULL, 0,
&qrb.data.palette.scaling_maxval);
} else {
int colorsWanted = atoi(msGetOutputFormatOption( format, "QUANTIZE_COLORS", "0"));
const char *palettePath = msGetOutputFormatOption( format, "PALETTE", "palette.txt");
char szPath[MS_MAXPATHLEN];
if(map) {
msBuildPath(szPath, map->mappath, palettePath);
palettePath = szPath;
}
if(readPalette(palettePath,paletteGiven,&numPaletteGivenEntries,format->transparent) != MS_SUCCESS) {
return MS_FAILURE;
}
if(numPaletteGivenEntries == 256 || colorsWanted == 0) {
qrb.data.palette.palette = paletteGiven;
qrb.data.palette.num_entries = numPaletteGivenEntries;
ret = MS_SUCCESS;
/* we have a full palette and don't want an additional quantization step */
} else {
/* quantize the image, and mix our colours in the resulting palette */
qrb.data.palette.palette = palette;
qrb.data.palette.num_entries = MS_MAX(colorsWanted,numPaletteGivenEntries);
ret = msQuantizeRasterBuffer(rb,&(qrb.data.palette.num_entries),qrb.data.palette.palette,
paletteGiven,numPaletteGivenEntries,
&qrb.data.palette.scaling_maxval);
}
}
if(ret != MS_FAILURE) {
ret = msClassifyRasterBuffer(rb,&qrb);
ret = savePalettePNG(&qrb,info,compression);
}
msFree(qrb.data.palette.pixels);
return ret;
} else if(rb->type == MS_BUFFER_BYTE_RGBA) {
png_infop info_ptr;
int color_type;
int row;
unsigned int *rowdata;
png_structp png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
if (!png_ptr)
return (MS_FAILURE);
png_set_compression_level(png_ptr, compression);
png_set_filter (png_ptr,0, PNG_FILTER_NONE);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr,
(png_infopp)NULL);
return (MS_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return (MS_FAILURE);
}
if(info->fp)
png_set_write_fn(png_ptr,info, png_write_data_to_stream, png_flush_data);
else
png_set_write_fn(png_ptr,info, png_write_data_to_buffer, png_flush_data);
if(rb->data.rgba.a)
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
else
color_type = PNG_COLOR_TYPE_RGB;
png_set_IHDR(png_ptr, info_ptr, rb->width, rb->height,
8, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
if(!rb->data.rgba.a && rb->data.rgba.pixel_step==4)
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
rowdata = (unsigned int*)malloc(rb->width*sizeof(unsigned int));
for(row=0; row<rb->height; row++) {
unsigned int *pixptr = rowdata;
int col;
unsigned char *a,*r,*g,*b;
r=rb->data.rgba.r+row*rb->data.rgba.row_step;
g=rb->data.rgba.g+row*rb->data.rgba.row_step;
b=rb->data.rgba.b+row*rb->data.rgba.row_step;
if(rb->data.rgba.a) {
a=rb->data.rgba.a+row*rb->data.rgba.row_step;
for(col=0; col<rb->width; col++) {
if(*a) {
double da = *a/255.0;
unsigned char *pix = (unsigned char*)pixptr;
pix[0] = *r/da;
pix[1] = *g/da;
pix[2] = *b/da;
pix[3] = *a;
} else {
*pixptr=0;
}
pixptr++;
a+=rb->data.rgba.pixel_step;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
} else {
for(col=0; col<rb->width; col++) {
unsigned char *pix = (unsigned char*)pixptr;
pix[0] = *r;
pix[1] = *g;
pix[2] = *b;
pixptr++;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
}
png_write_row(png_ptr,(png_bytep)rowdata);
}
png_write_end(png_ptr, info_ptr);
free(rowdata);
png_destroy_write_struct(&png_ptr, &info_ptr);
return MS_SUCCESS;
} else {
msSetError(MS_MISCERR,"Unknown buffer type","saveAsPNG()");
return MS_FAILURE;
}
}
/* For platforms with incomplete ANSI defines. Fortunately,
SEEK_SET is defined to be zero by the standard. */
#ifndef SEEK_SET
#define SEEK_SET 0
#endif /* SEEK_SET */
int readPNG(char *path, rasterBufferObj *rb)
{
png_uint_32 width,height;
unsigned char *a,*r,*g,*b;
int bit_depth,color_type,i;
unsigned char **row_pointers;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
FILE *stream = fopen(path,"rb");
if(!stream)
return MS_FAILURE;
/* could pass pointers to user-defined error handlers instead of NULLs: */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(stream);
return MS_FAILURE; /* out of memory */
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(stream);
return MS_FAILURE; /* out of memory */
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
fclose(stream);
return MS_FAILURE;
}
png_init_io(png_ptr,stream);
png_read_info(png_ptr,info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height,
&bit_depth, &color_type,
NULL,NULL,NULL);
rb->width = width;
rb->height = height;
rb->type = MS_BUFFER_BYTE_RGBA;
rb->data.rgba.pixels = (unsigned char*)malloc(width*height*4*sizeof(unsigned char));
row_pointers = (unsigned char**)malloc(height*sizeof(unsigned char*));
rb->data.rgba.pixel_step=4;
rb->data.rgba.row_step = width*4;
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(i=0; i<height; i++) {
row_pointers[i] = &(rb->data.rgba.pixels[i*rb->data.rgba.row_step]);
}
if (color_type == PNG_COLOR_TYPE_PALETTE)
/* expand palette images to RGB */
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
/* expand low bit-depth grayscale to 8bits */
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
/* expand transparency chunks to full alpha */
png_set_expand(png_ptr);
if (bit_depth == 16)
/* scale 16bits down to 8 */
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
/* convert grayscale to rgba */
png_set_gray_to_rgb(png_ptr);
png_set_bgr(png_ptr);
if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
assert(png_get_rowbytes(png_ptr, info_ptr) == rb->data.rgba.row_step);
png_read_image(png_ptr, row_pointers);
free(row_pointers);
row_pointers=NULL;
png_read_end(png_ptr,NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
/*premultiply data*/
for(i=0; i<width*height; i++) {
if(*a < 255) {
if(*a == 0) {
*r=*g=*b=0;
} else {
*r = (*r * *a + 255) >> 8;
*g = (*g * *a + 255) >> 8;
*b = (*b * *a + 255) >> 8;
}
}
a+=4;
b+=4;
g+=4;
r+=4;
}
fclose(stream);
return MS_SUCCESS;
}
int msSaveRasterBuffer(mapObj *map, rasterBufferObj *rb, FILE *stream,
outputFormatObj *format)
{
if(strcasestr(format->driver,"/png")) {
streamInfo info;
info.fp = stream;
info.buffer = NULL;
return saveAsPNG(map, rb,&info,format);
} else if(strcasestr(format->driver,"/jpeg")) {
streamInfo info;
info.fp = stream;
info.buffer=NULL;
return saveAsJPEG(map, rb,&info,format);
} else {
msSetError(MS_MISCERR,"unsupported image format\n", "msSaveRasterBuffer()");
return MS_FAILURE;
}
}
int msSaveRasterBufferToBuffer(rasterBufferObj *data, bufferObj *buffer,
outputFormatObj *format)
{
if(strcasestr(format->driver,"/png")) {
streamInfo info;
info.fp = NULL;
info.buffer = buffer;
return saveAsPNG(NULL, data,&info,format);
} else if(strcasestr(format->driver,"/jpeg")) {
streamInfo info;
info.fp = NULL;
info.buffer=buffer;
return saveAsJPEG(NULL, data,&info,format);
} else {
msSetError(MS_MISCERR,"unsupported image format\n", "msSaveRasterBuffer()");
return MS_FAILURE;
}
}
#ifdef USE_GIF
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
static char const *gif_error_msg(int code)
#else
static char const *gif_error_msg()
#endif
{
static char msg[80];
#if (!defined GIFLIB_MAJOR) || (GIFLIB_MAJOR < 5)
int code = GifLastError();
#endif
switch (code) {
case E_GIF_ERR_OPEN_FAILED: /* should not see this */
return "Failed to open given file";
case E_GIF_ERR_WRITE_FAILED:
return "Write failed";
case E_GIF_ERR_HAS_SCRN_DSCR: /* should not see this */
return "Screen descriptor already passed to giflib";
case E_GIF_ERR_HAS_IMAG_DSCR: /* should not see this */
return "Image descriptor already passed to giflib";
case E_GIF_ERR_NO_COLOR_MAP: /* should not see this */
return "Neither global nor local color map set";
case E_GIF_ERR_DATA_TOO_BIG: /* should not see this */
return "Too much pixel data passed to giflib";
case E_GIF_ERR_NOT_ENOUGH_MEM:
return "Out of memory";
case E_GIF_ERR_DISK_IS_FULL:
return "Disk is full";
case E_GIF_ERR_CLOSE_FAILED: /* should not see this */
return "File close failed";
case E_GIF_ERR_NOT_WRITEABLE: /* should not see this */
return "File not writable";
case D_GIF_ERR_OPEN_FAILED:
return "Failed to open file";
case D_GIF_ERR_READ_FAILED:
return "Failed to read from file";
case D_GIF_ERR_NOT_GIF_FILE:
return "File is not a GIF file";
case D_GIF_ERR_NO_SCRN_DSCR:
return "No screen descriptor detected - invalid file";
case D_GIF_ERR_NO_IMAG_DSCR:
return "No image descriptor detected - invalid file";
case D_GIF_ERR_NO_COLOR_MAP:
return "No global or local color map found";
case D_GIF_ERR_WRONG_RECORD:
return "Wrong record type detected - invalid file?";
case D_GIF_ERR_DATA_TOO_BIG:
return "Data in file too big for image";
case D_GIF_ERR_NOT_ENOUGH_MEM:
return "Out of memory";
case D_GIF_ERR_CLOSE_FAILED:
return "Close failed";
case D_GIF_ERR_NOT_READABLE:
return "File not opened for read";
case D_GIF_ERR_IMAGE_DEFECT:
return "Defective image";
case D_GIF_ERR_EOF_TOO_SOON:
return "Unexpected EOF - invalid file";
default:
sprintf(msg, "Unknown giflib error code %d", code);
return msg;
}
}
/* not fully implemented and tested */
/* missing: set the first pointer to a,r,g,b */
int readGIF(char *path, rasterBufferObj *rb)
{
int i, j, codeSize, extCode, firstImageRead = MS_FALSE;
unsigned char *r,*g,*b,*a;
int transIdx = -1;
GifFileType *image;
GifPixelType *line;
GifRecordType recordType;
GifByteType *codeBlock, *extension;
ColorMapObject *cmap;
int interlacedOffsets[] = {0,4,2,1};
int interlacedJumps[] = {8,8,4,2};
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
int errcode;
#endif
rb->type = MS_BUFFER_BYTE_RGBA;
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
image = DGifOpenFileName(path, &errcode);
if (image == NULL) {
msSetError(MS_MISCERR,"failed to load gif image: %s","readGIF()", gif_error_msg(errcode));
return MS_FAILURE;
}
#else
image = DGifOpenFileName(path);
if (image == NULL) {
msSetError(MS_MISCERR,"failed to load gif image: %s","readGIF()", gif_error_msg());
return MS_FAILURE;
}
#endif
rb->width = image->SWidth;
rb->height = image->SHeight;
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];
cmap = (image->Image.ColorMap)?image->Image.ColorMap:image->SColorMap;
for(i=0; i<rb->width*rb->height; i++) {
*a = 255;
*r = cmap->Colors[image->SBackGroundColor].Red;
*g = cmap->Colors[image->SBackGroundColor].Green;
*b = cmap->Colors[image->SBackGroundColor].Blue;
a+=rb->data.rgba.pixel_step;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
do {
if (DGifGetRecordType(image, &recordType) == GIF_ERROR) {
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()", gif_error_msg(image->Error));
#else
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()", gif_error_msg());
#endif
return MS_FAILURE;
}
switch (recordType) {
case SCREEN_DESC_RECORD_TYPE:
DGifGetScreenDesc(image);
break;
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(image) == GIF_ERROR) {
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()", gif_error_msg(image->Error));
#else
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()", gif_error_msg());
#endif
return MS_FAILURE;
}
if (!firstImageRead) {
int row = image->Image.Top;
int col = image->Image.Left;
int width = image->Image.Width;
int height = image->Image.Height;
/* sanity check: */
if(col+width>rb->width || row+height>rb->height) {
msSetError(MS_MISCERR,"corrupted gif image, img size exceeds screen size","readGIF()");
return MS_FAILURE;
}
line = (GifPixelType *) malloc(width * sizeof(GifPixelType));
if(image->Image.Interlace) {
int count;
for(count=0; count<4; count++) {
for(i=row+interlacedOffsets[count]; i<row+height; i+=interlacedJumps[count]) {
int offset = i * rb->data.rgba.row_step + col * rb->data.rgba.pixel_step;
a = rb->data.rgba.a + offset;
r = rb->data.rgba.r + offset;
g = rb->data.rgba.g + offset;
b = rb->data.rgba.b + offset;
if (DGifGetLine(image, line, width) == GIF_ERROR) {
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()",gif_error_msg(image->Error));
#else
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()",gif_error_msg());
#endif
return MS_FAILURE;
}
for(j=0; j<width; j++) {
GifPixelType pix = line[j];
if(transIdx == -1 || pix != transIdx) {
*a = 255;
*r = cmap->Colors[pix].Red;
*g = cmap->Colors[pix].Green;
*b = cmap->Colors[pix].Blue;
} else {
*a = *r = *g = *b = 0;
}
a+=rb->data.rgba.pixel_step;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
}
}
} else {
for (i = 0; i < height; i++) {
int offset = i * rb->data.rgba.row_step + col * rb->data.rgba.pixel_step;
a = rb->data.rgba.a + offset;
r = rb->data.rgba.r + offset;
g = rb->data.rgba.g + offset;
b = rb->data.rgba.b + offset;
if (DGifGetLine(image, line, width) == GIF_ERROR) {
#if defined GIFLIB_MAJOR && GIFLIB_MAJOR >= 5
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()",gif_error_msg(image->Error));
#else
msSetError(MS_MISCERR,"corrupted gif image?: %s","readGIF()",gif_error_msg());
#endif
return MS_FAILURE;
}
for(j=0; j<width; j++) {
GifPixelType pix = line[j];
if(transIdx == -1 || pix != transIdx) {
*a = 255;
*r = cmap->Colors[pix].Red;
*g = cmap->Colors[pix].Green;
*b = cmap->Colors[pix].Blue;
} else {
*a = *r = *g = *b = 0;
}
a+=rb->data.rgba.pixel_step;
r+=rb->data.rgba.pixel_step;
g+=rb->data.rgba.pixel_step;
b+=rb->data.rgba.pixel_step;
}
}