forked from dmorissette/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapagg.cpp
1385 lines (1235 loc) · 48.2 KB
/
mapagg.cpp
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: AGG rendering and other AGG related functions.
* Author: Thomas Bonfort and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2007 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:
*
* 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 "fontcache.h"
#include "mapagg.h"
#include <assert.h>
#include "renderers/agg/include/agg_color_rgba.h"
#include "renderers/agg/include/agg_pixfmt_rgba.h"
#include "renderers/agg/include/agg_renderer_base.h"
#include "renderers/agg/include/agg_renderer_scanline.h"
#include "renderers/agg/include/agg_math_stroke.h"
#include "renderers/agg/include/agg_scanline_p.h"
#include "renderers/agg/include/agg_scanline_u.h"
#include "renderers/agg/include/agg_rasterizer_scanline_aa.h"
#include "renderers/agg/include/agg_span_pattern_rgba.h"
#include "renderers/agg/include/agg_span_allocator.h"
#include "renderers/agg/include/agg_span_interpolator_linear.h"
#include "renderers/agg/include/agg_pattern_filters_rgba.h"
#include "renderers/agg/include/agg_image_accessors.h"
#include "renderers/agg/include/agg_conv_stroke.h"
#include "renderers/agg/include/agg_conv_dash.h"
#include "renderers/agg/include/agg_font_freetype.h"
#include "renderers/agg/include/agg_conv_contour.h"
#include "renderers/agg/include/agg_ellipse.h"
#include "renderers/agg/include/agg_gamma_functions.h"
#include "renderers/agg/include/agg_blur.h"
#include "renderers/agg/include/agg_rasterizer_outline_aa.h"
#include "renderers/agg/include/agg_renderer_outline_aa.h"
#include "renderers/agg/include/agg_renderer_outline_image.h"
#include "renderers/agg/include/agg_span_pattern_rgba.h"
#include "renderers/agg/include/agg_span_image_filter_rgba.h"
#include "renderers/agg/include/agg_glyph_raster_bin.h"
#include "renderers/agg/include/agg_renderer_raster_text.h"
#include "renderers/agg/include/agg_path_storage_integer.h"
#include "renderers/agg/include/agg_conv_clipper.h"
#ifdef USE_PIXMAN
#include <pixman.h>
#endif
#ifdef AGG_ALIASED_ENABLED
#include "renderers/agg/include/agg_renderer_primitives.h"
#include "renderers/agg/include/agg_rasterizer_outline.h"
#endif
typedef mapserver::order_bgra band_order;
#define AGG_LINESPACE 1.33
typedef mapserver::int8u band_type;
typedef mapserver::rgba8 color_type;
typedef mapserver::pixel32_type pixel_type;
typedef mapserver::blender_rgba_pre<color_type, band_order> blender_pre;
typedef mapserver::comp_op_adaptor_rgba_pre<color_type, band_order> compop_blender_pre;
typedef mapserver::pixfmt_alpha_blend_rgba<blender_pre, mapserver::rendering_buffer, pixel_type> pixel_format;
typedef mapserver::pixfmt_custom_blend_rgba<compop_blender_pre, mapserver::rendering_buffer> compop_pixel_format;
typedef mapserver::rendering_buffer rendering_buffer;
typedef mapserver::renderer_base<pixel_format> renderer_base;
typedef mapserver::renderer_base<compop_pixel_format> compop_renderer_base;
typedef mapserver::renderer_scanline_aa_solid<renderer_base> renderer_scanline;
typedef mapserver::rasterizer_scanline_aa<> rasterizer_scanline;
typedef mapserver::font_engine_freetype_int16 font_engine_type;
typedef mapserver::font_cache_manager<font_engine_type> font_manager_type;
typedef mapserver::conv_curve<font_manager_type::path_adaptor_type> font_curve_type;
typedef mapserver::glyph_raster_bin<color_type> glyph_gen;
#ifdef AGG_ALIASED_ENABLED
typedef mapserver::renderer_primitives<renderer_base> renderer_primitives;
typedef mapserver::rasterizer_outline<renderer_primitives> rasterizer_outline;
#endif
static color_type AGG_NO_COLOR = color_type(0, 0, 0, 0);
#define aggColor(c) mapserver::rgba8_pre((c)->red, (c)->green, (c)->blue, (c)->alpha)
class aggRendererCache
{
public:
font_engine_type m_feng;
font_manager_type m_fman;
aggRendererCache(): m_fman(m_feng) {}
};
class AGG2Renderer
{
public:
AGG2Renderer()
#ifdef AGG_ALIASED_ENABLED
:
m_renderer_primitives(m_renderer_base),
m_rasterizer_primitives(m_renderer_primitives)
#endif
{
stroke = NULL;
dash = NULL;
stroke_dash = NULL;
}
~AGG2Renderer() {
if(stroke) {
delete stroke;
}
if(dash) {
delete dash;
}
if(stroke_dash) {
delete stroke_dash;
}
}
band_type* buffer;
rendering_buffer m_rendering_buffer;
pixel_format m_pixel_format;
compop_pixel_format m_compop_pixel_format;
renderer_base m_renderer_base;
compop_renderer_base m_compop_renderer_base;
renderer_scanline m_renderer_scanline;
#ifdef AGG_ALIASED_ENABLED
renderer_primitives m_renderer_primitives;
rasterizer_outline m_rasterizer_primitives;
#endif
rasterizer_scanline m_rasterizer_aa;
rasterizer_scanline m_rasterizer_aa_gamma;
mapserver::scanline_p8 sl_poly; /*packed scanlines, works faster when the area is larger
than the perimeter, in number of pixels*/
mapserver::scanline_u8 sl_line; /*unpacked scanlines, works faster if the area is roughly
equal to the perimeter, in number of pixels*/
bool use_alpha;
mapserver::conv_stroke<line_adaptor> *stroke;
mapserver::conv_dash<line_adaptor> *dash;
mapserver::conv_stroke<mapserver::conv_dash<line_adaptor> > *stroke_dash;
double default_gamma;
mapserver::gamma_linear gamma_function;
};
#define AGG_RENDERER(image) ((AGG2Renderer*) (image)->img.plugin)
template<class VertexSource>
static void applyCJC(VertexSource &stroke, int caps, int joins)
{
switch (joins) {
case MS_CJC_ROUND:
stroke.line_join(mapserver::round_join);
break;
case MS_CJC_MITER:
stroke.line_join(mapserver::miter_join);
break;
case MS_CJC_BEVEL:
case MS_CJC_NONE:
stroke.line_join(mapserver::bevel_join);
break;
}
switch (caps) {
case MS_CJC_BUTT:
case MS_CJC_NONE:
stroke.line_cap(mapserver::butt_cap);
break;
case MS_CJC_ROUND:
stroke.line_cap(mapserver::round_cap);
break;
case MS_CJC_SQUARE:
stroke.line_cap(mapserver::square_cap);
break;
}
}
int agg2RenderLine(imageObj *img, shapeObj *p, strokeStyleObj *style)
{
AGG2Renderer *r = AGG_RENDERER(img);
line_adaptor lines = line_adaptor(p);
#ifdef AGG_ALIASED_ENABLED
r->m_rasterizer_primitives.reset();
r->m_renderer_primitives.line_color(aggColor(style->color));
r->m_rasterizer_primitives.add_path(lines);
return MS_SUCCESS;
#endif
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
r->m_renderer_scanline.color(aggColor(style->color));
if (style->patternlength <= 0) {
if(!r->stroke) {
r->stroke = new mapserver::conv_stroke<line_adaptor>(lines);
} else {
r->stroke->attach(lines);
}
r->stroke->width(style->width);
if(style->width>1) {
applyCJC(*r->stroke, style->linecap, style->linejoin);
} else {
r->stroke->inner_join(mapserver::inner_bevel);
r->stroke->line_join(mapserver::bevel_join);
}
r->m_rasterizer_aa.add_path(*r->stroke);
} else {
if(!r->dash) {
r->dash = new mapserver::conv_dash<line_adaptor>(lines);
} else {
r->dash->remove_all_dashes();
r->dash->dash_start(0.0);
r->dash->attach(lines);
}
if(!r->stroke_dash) {
r->stroke_dash = new mapserver::conv_stroke<mapserver::conv_dash<line_adaptor> > (*r->dash);
} else {
r->stroke_dash->attach(*r->dash);
}
int patt_length = 0;
for (int i = 0; i < style->patternlength; i += 2) {
if (i < style->patternlength - 1) {
r->dash->add_dash(MS_MAX(1,MS_NINT(style->pattern[i])),
MS_MAX(1,MS_NINT(style->pattern[i + 1])));
if(style->patternoffset) {
patt_length += MS_MAX(1,MS_NINT(style->pattern[i])) +
MS_MAX(1,MS_NINT(style->pattern[i + 1]));
}
}
}
if(style->patternoffset > 0) {
r->dash->dash_start(patt_length - style->patternoffset);
}
r->stroke_dash->width(style->width);
if(style->width>1) {
applyCJC(*r->stroke_dash, style->linecap, style->linejoin);
} else {
r->stroke_dash->inner_join(mapserver::inner_bevel);
r->stroke_dash->line_join(mapserver::bevel_join);
}
r->m_rasterizer_aa.add_path(*r->stroke_dash);
}
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_line, r->m_renderer_scanline);
return MS_SUCCESS;
}
int agg2RenderLineTiled(imageObj *img, shapeObj *p, imageObj * tile)
{
mapserver::pattern_filter_bilinear_rgba8 fltr;
typedef mapserver::line_image_pattern<mapserver::pattern_filter_bilinear_rgba8> pattern_type;
typedef mapserver::renderer_outline_image<renderer_base, pattern_type> renderer_img_type;
typedef mapserver::rasterizer_outline_aa<renderer_img_type, mapserver::line_coord_sat> rasterizer_img_type;
pattern_type patt(fltr);
AGG2Renderer *r = AGG_RENDERER(img);
AGG2Renderer *tileRenderer = AGG_RENDERER(tile);
line_adaptor lines(p);
patt.create(tileRenderer->m_pixel_format);
renderer_img_type ren_img(r->m_renderer_base, patt);
rasterizer_img_type ras_img(ren_img);
ras_img.add_path(lines);
return MS_SUCCESS;
}
int agg2RenderPolygon(imageObj *img, shapeObj *p, colorObj * color)
{
AGG2Renderer *r = AGG_RENDERER(img);
polygon_adaptor polygons(p);
r->m_rasterizer_aa_gamma.reset();
r->m_rasterizer_aa_gamma.filling_rule(mapserver::fill_even_odd);
r->m_rasterizer_aa_gamma.add_path(polygons);
r->m_renderer_scanline.color(aggColor(color));
mapserver::render_scanlines(r->m_rasterizer_aa_gamma, r->sl_poly, r->m_renderer_scanline);
return MS_SUCCESS;
}
static inline double int26p6_to_dbl(int p)
{
return double(p) / 64.0;
}
template<class PathStorage>
bool decompose_ft_outline(const FT_Outline& outline,
bool flip_y,
const mapserver::trans_affine& mtx,
PathStorage& path)
{
FT_Vector v_last;
FT_Vector v_control;
FT_Vector v_start;
double x1, y1, x2, y2, x3, y3;
FT_Vector* point;
FT_Vector* limit;
char* tags;
int n; // index of contour in outline
int first; // index of first point in contour
char tag; // current point's state
first = 0;
for(n = 0; n < outline.n_contours; n++)
{
int last; // index of last point in contour
last = outline.contours[n];
limit = outline.points + last;
v_start = outline.points[first];
v_last = outline.points[last];
v_control = v_start;
point = outline.points + first;
tags = outline.tags + first;
tag = FT_CURVE_TAG(tags[0]);
// A contour cannot start with a cubic control point!
if(tag == FT_CURVE_TAG_CUBIC) return false;
// check first point to determine origin
if( tag == FT_CURVE_TAG_CONIC)
{
// first point is conic control. Yes, this happens.
if(FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
{
// start at last point if it is on the curve
v_start = v_last;
limit--;
}
else
{
// if both first and last points are conic,
// start at their middle and record its position
// for closure
v_start.x = (v_start.x + v_last.x) / 2;
v_start.y = (v_start.y + v_last.y) / 2;
v_last = v_start;
}
point--;
tags--;
}
x1 = int26p6_to_dbl(v_start.x);
y1 = int26p6_to_dbl(v_start.y);
if(flip_y) y1 = -y1;
mtx.transform(&x1, &y1);
path.move_to(x1,y1);
while(point < limit)
{
point++;
tags++;
tag = FT_CURVE_TAG(tags[0]);
switch(tag)
{
case FT_CURVE_TAG_ON: // emit a single line_to
{
x1 = int26p6_to_dbl(point->x);
y1 = int26p6_to_dbl(point->y);
if(flip_y) y1 = -y1;
mtx.transform(&x1, &y1);
path.line_to(x1,y1);
//path.line_to(conv(point->x), flip_y ? -conv(point->y) : conv(point->y));
continue;
}
case FT_CURVE_TAG_CONIC: // consume conic arcs
{
v_control.x = point->x;
v_control.y = point->y;
Do_Conic:
if(point < limit)
{
FT_Vector vec;
FT_Vector v_middle;
point++;
tags++;
tag = FT_CURVE_TAG(tags[0]);
vec.x = point->x;
vec.y = point->y;
if(tag == FT_CURVE_TAG_ON)
{
x1 = int26p6_to_dbl(v_control.x);
y1 = int26p6_to_dbl(v_control.y);
x2 = int26p6_to_dbl(vec.x);
y2 = int26p6_to_dbl(vec.y);
if(flip_y) { y1 = -y1; y2 = -y2; }
mtx.transform(&x1, &y1);
mtx.transform(&x2, &y2);
path.curve3(x1,y1,x2,y2);
continue;
}
if(tag != FT_CURVE_TAG_CONIC) return false;
v_middle.x = (v_control.x + vec.x) / 2;
v_middle.y = (v_control.y + vec.y) / 2;
x1 = int26p6_to_dbl(v_control.x);
y1 = int26p6_to_dbl(v_control.y);
x2 = int26p6_to_dbl(v_middle.x);
y2 = int26p6_to_dbl(v_middle.y);
if(flip_y) { y1 = -y1; y2 = -y2; }
mtx.transform(&x1, &y1);
mtx.transform(&x2, &y2);
path.curve3(x1,y1,x2,y2);
//path.curve3(conv(v_control.x),
// flip_y ? -conv(v_control.y) : conv(v_control.y),
// conv(v_middle.x),
// flip_y ? -conv(v_middle.y) : conv(v_middle.y));
v_control = vec;
goto Do_Conic;
}
x1 = int26p6_to_dbl(v_control.x);
y1 = int26p6_to_dbl(v_control.y);
x2 = int26p6_to_dbl(v_start.x);
y2 = int26p6_to_dbl(v_start.y);
if(flip_y) { y1 = -y1; y2 = -y2; }
mtx.transform(&x1, &y1);
mtx.transform(&x2, &y2);
path.curve3(x1,y1,x2,y2);
//path.curve3(conv(v_control.x),
// flip_y ? -conv(v_control.y) : conv(v_control.y),
// conv(v_start.x),
// flip_y ? -conv(v_start.y) : conv(v_start.y));
goto Close;
}
default: // FT_CURVE_TAG_CUBIC
{
FT_Vector vec1, vec2;
if(point + 1 > limit || FT_CURVE_TAG(tags[1]) != FT_CURVE_TAG_CUBIC)
{
return false;
}
vec1.x = point[0].x;
vec1.y = point[0].y;
vec2.x = point[1].x;
vec2.y = point[1].y;
point += 2;
tags += 2;
if(point <= limit)
{
FT_Vector vec;
vec.x = point->x;
vec.y = point->y;
x1 = int26p6_to_dbl(vec1.x);
y1 = int26p6_to_dbl(vec1.y);
x2 = int26p6_to_dbl(vec2.x);
y2 = int26p6_to_dbl(vec2.y);
x3 = int26p6_to_dbl(vec.x);
y3 = int26p6_to_dbl(vec.y);
if(flip_y) { y1 = -y1; y2 = -y2; y3 = -y3; }
mtx.transform(&x1, &y1);
mtx.transform(&x2, &y2);
mtx.transform(&x3, &y3);
path.curve4(x1,y1,x2,y2,x3,y3);
//path.curve4(conv(vec1.x),
// flip_y ? -conv(vec1.y) : conv(vec1.y),
// conv(vec2.x),
// flip_y ? -conv(vec2.y) : conv(vec2.y),
// conv(vec.x),
// flip_y ? -conv(vec.y) : conv(vec.y));
continue;
}
x1 = int26p6_to_dbl(vec1.x);
y1 = int26p6_to_dbl(vec1.y);
x2 = int26p6_to_dbl(vec2.x);
y2 = int26p6_to_dbl(vec2.y);
x3 = int26p6_to_dbl(v_start.x);
y3 = int26p6_to_dbl(v_start.y);
if(flip_y) { y1 = -y1; y2 = -y2; y3 = -y3; }
mtx.transform(&x1, &y1);
mtx.transform(&x2, &y2);
mtx.transform(&x3, &y3);
path.curve4(x1,y1,x2,y2,x3,y3);
//path.curve4(conv(vec1.x),
// flip_y ? -conv(vec1.y) : conv(vec1.y),
// conv(vec2.x),
// flip_y ? -conv(vec2.y) : conv(vec2.y),
// conv(v_start.x),
// flip_y ? -conv(v_start.y) : conv(v_start.y));
goto Close;
}
}
}
path.close_polygon();
Close:
first = last + 1;
}
return true;
}
int agg2RenderPolygonTiled(imageObj *img, shapeObj *p, imageObj * tile)
{
assert(img->format->renderer == tile->format->renderer);
AGG2Renderer *r = AGG_RENDERER(img);
AGG2Renderer *tileRenderer = AGG_RENDERER(tile);
polygon_adaptor polygons(p);
typedef mapserver::wrap_mode_repeat wrap_type;
typedef mapserver::image_accessor_wrap<pixel_format,wrap_type,wrap_type> img_source_type;
typedef mapserver::span_pattern_rgba<img_source_type> span_gen_type;
mapserver::span_allocator<mapserver::rgba8> sa;
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_even_odd);
img_source_type img_src(tileRenderer->m_pixel_format);
span_gen_type sg(img_src, 0, 0);
r->m_rasterizer_aa.add_path(polygons);
mapserver::render_scanlines_aa(r->m_rasterizer_aa, r->sl_poly, r->m_renderer_base, sa , sg);
return MS_SUCCESS;
}
int agg2RenderGlyphsPath(imageObj *img, textPathObj *tp, colorObj *c, colorObj *oc, int ow, int isMarker) {
mapserver::path_storage glyphs;
mapserver::trans_affine trans;
AGG2Renderer *r = AGG_RENDERER(img);
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
for(int i=0; i<tp->numglyphs; i++) {
glyphObj *gl = tp->glyphs + i;
trans.reset();
trans.rotate(-gl->rot);
trans.translate(gl->pnt.x, gl->pnt.y);
outline_element *ol = msGetGlyphOutline(gl->face,gl->glyph);
if(!ol) {
return MS_FAILURE;
}
decompose_ft_outline(ol->outline,true,trans,glyphs);
}
mapserver::conv_curve<mapserver::path_storage> m_curves(glyphs);
if (oc) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
mapserver::conv_contour<mapserver::conv_curve<mapserver::path_storage> > cc(m_curves);
cc.width(ow + 1);
r->m_rasterizer_aa.add_path(cc);
r->m_renderer_scanline.color(aggColor(oc));
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_line, r->m_renderer_scanline);
}
if(c) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
r->m_rasterizer_aa.add_path(m_curves);
r->m_renderer_scanline.color(aggColor(c));
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_line, r->m_renderer_scanline);
}
return MS_SUCCESS;
}
mapserver::path_storage imageVectorSymbol(symbolObj *symbol)
{
mapserver::path_storage path;
int is_new=1;
for(int i=0; i < symbol->numpoints; i++) {
if((symbol->points[i].x == -99) && (symbol->points[i].y == -99))
is_new=1;
else {
if(is_new) {
path.move_to(symbol->points[i].x,symbol->points[i].y);
is_new=0;
} else {
path.line_to(symbol->points[i].x,symbol->points[i].y);
}
}
}
return path;
}
int agg2RenderVectorSymbol(imageObj *img, double x, double y,
symbolObj *symbol, symbolStyleObj * style)
{
AGG2Renderer *r = AGG_RENDERER(img);
double ox = symbol->sizex * 0.5;
double oy = symbol->sizey * 0.5;
mapserver::path_storage path = imageVectorSymbol(symbol);
mapserver::trans_affine mtx;
mtx *= mapserver::trans_affine_translation(-ox,-oy);
mtx *= mapserver::trans_affine_scaling(style->scale);
mtx *= mapserver::trans_affine_rotation(-style->rotation);
mtx *= mapserver::trans_affine_translation(x, y);
path.transform(mtx);
if (style->color) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_even_odd);
r->m_rasterizer_aa.add_path(path);
r->m_renderer_scanline.color(aggColor(style->color));
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_poly, r->m_renderer_scanline);
}
if(style->outlinecolor) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
r->m_renderer_scanline.color(aggColor(style->outlinecolor));
mapserver::conv_stroke<mapserver::path_storage> stroke(path);
stroke.width(style->outlinewidth);
r->m_rasterizer_aa.add_path(stroke);
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_poly, r->m_renderer_scanline);
}
return MS_SUCCESS;
}
int agg2RenderPixmapSymbol(imageObj *img, double x, double y, symbolObj *symbol, symbolStyleObj * style)
{
AGG2Renderer *r = AGG_RENDERER(img);
rasterBufferObj *pixmap = symbol->pixmap_buffer;
assert(pixmap->type == MS_BUFFER_BYTE_RGBA);
rendering_buffer b(pixmap->data.rgba.pixels,pixmap->width,pixmap->height,pixmap->data.rgba.row_step);
pixel_format pf(b);
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
if ( (style->rotation != 0 && style->rotation != MS_PI*2.)|| style->scale != 1) {
mapserver::trans_affine image_mtx;
image_mtx *= mapserver::trans_affine_translation(-(pf.width()/2.),-(pf.height()/2.));
/*agg angles are antitrigonometric*/
image_mtx *= mapserver::trans_affine_rotation(-style->rotation);
image_mtx *= mapserver::trans_affine_scaling(style->scale);
image_mtx *= mapserver::trans_affine_translation(x,y);
image_mtx.invert();
typedef mapserver::span_interpolator_linear<> interpolator_type;
interpolator_type interpolator(image_mtx);
mapserver::span_allocator<color_type> sa;
// "hardcoded" bilinear filter
//------------------------------------------
typedef mapserver::span_image_filter_rgba_bilinear_clip<pixel_format, interpolator_type> span_gen_type;
span_gen_type sg(pf, mapserver::rgba(0,0,0,0), interpolator);
mapserver::path_storage pixmap_bbox;
int ims_2 = MS_NINT(MS_MAX(pixmap->width,pixmap->height)*style->scale*1.415)/2+1;
pixmap_bbox.move_to(x-ims_2,y-ims_2);
pixmap_bbox.line_to(x+ims_2,y-ims_2);
pixmap_bbox.line_to(x+ims_2,y+ims_2);
pixmap_bbox.line_to(x-ims_2,y+ims_2);
r->m_rasterizer_aa.add_path(pixmap_bbox);
mapserver::render_scanlines_aa(r->m_rasterizer_aa, r->sl_poly, r->m_renderer_base, sa, sg);
} else {
//just copy the image at the correct location (we place the pixmap on
//the nearest integer pixel to avoid blurring)
r->m_renderer_base.blend_from(pf,0,MS_NINT(x-pixmap->width/2.),MS_NINT(y-pixmap->height/2.));
}
return MS_SUCCESS;
}
int agg2RenderEllipseSymbol(imageObj *image, double x, double y,
symbolObj *symbol, symbolStyleObj * style)
{
AGG2Renderer *r = AGG_RENDERER(image);
mapserver::path_storage path;
mapserver::ellipse ellipse(x,y,symbol->sizex*style->scale/2,symbol->sizey*style->scale/2);
path.concat_path(ellipse);
if( style->rotation != 0) {
mapserver::trans_affine mtx;
mtx *= mapserver::trans_affine_translation(-x,-y);
/*agg angles are antitrigonometric*/
mtx *= mapserver::trans_affine_rotation(-style->rotation);
mtx *= mapserver::trans_affine_translation(x,y);
path.transform(mtx);
}
if(style->color) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_even_odd);
r->m_rasterizer_aa.add_path(path);
r->m_renderer_scanline.color(aggColor(style->color));
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_line, r->m_renderer_scanline);
}
if(style->outlinewidth) {
r->m_rasterizer_aa.reset();
r->m_rasterizer_aa.filling_rule(mapserver::fill_non_zero);
mapserver::conv_stroke<mapserver::path_storage> stroke(path);
stroke.width(style->outlinewidth);
r->m_rasterizer_aa.add_path(stroke);
r->m_renderer_scanline.color(aggColor(style->outlinecolor));
mapserver::render_scanlines(r->m_rasterizer_aa, r->sl_poly, r->m_renderer_scanline);
}
return MS_SUCCESS;
}
int agg2RenderTile(imageObj *img, imageObj *tile, double x, double y)
{
/*
AGG2Renderer *imgRenderer = agg2GetRenderer(img);
AGG2Renderer *tileRenderer = agg2GetRenderer(tile);
*/
return MS_FAILURE;
}
int aggInitializeRasterBuffer(rasterBufferObj *rb, int width, int height, int mode)
{
rb->type = MS_BUFFER_BYTE_RGBA;
rb->data.rgba.pixel_step = 4;
rb->data.rgba.row_step = rb->data.rgba.pixel_step * width;
rb->width = width;
rb->height = height;
int nBytes = rb->data.rgba.row_step * height;
rb->data.rgba.pixels = (band_type*)msSmallCalloc(nBytes,sizeof(band_type));
rb->data.rgba.r = &(rb->data.rgba.pixels[band_order::R]);
rb->data.rgba.g = &(rb->data.rgba.pixels[band_order::G]);
rb->data.rgba.b = &(rb->data.rgba.pixels[band_order::B]);
if(mode == MS_IMAGEMODE_RGBA) {
rb->data.rgba.a = &(rb->data.rgba.pixels[band_order::A]);
}
return MS_SUCCESS;
}
int aggGetRasterBufferHandle(imageObj *img, rasterBufferObj * rb)
{
AGG2Renderer *r = AGG_RENDERER(img);
rb->type =MS_BUFFER_BYTE_RGBA;
rb->data.rgba.pixels = r->buffer;
rb->data.rgba.row_step = r->m_rendering_buffer.stride();
rb->data.rgba.pixel_step = 4;
rb->width = r->m_rendering_buffer.width();
rb->height = r->m_rendering_buffer.height();
rb->data.rgba.r = &(r->buffer[band_order::R]);
rb->data.rgba.g = &(r->buffer[band_order::G]);
rb->data.rgba.b = &(r->buffer[band_order::B]);
if(r->use_alpha)
rb->data.rgba.a = &(r->buffer[band_order::A]);
else
rb->data.rgba.a = NULL;
return MS_SUCCESS;
}
int aggGetRasterBufferCopy(imageObj *img, rasterBufferObj *rb)
{
AGG2Renderer *r = AGG_RENDERER(img);
aggInitializeRasterBuffer(rb, img->width, img->height, MS_IMAGEMODE_RGBA);
int nBytes = r->m_rendering_buffer.stride()*r->m_rendering_buffer.height();
memcpy(rb->data.rgba.pixels,r->buffer, nBytes);
return MS_SUCCESS;
}
int agg2MergeRasterBuffer(imageObj *dest, rasterBufferObj *overlay, double opacity, int srcX, int srcY,
int dstX, int dstY, int width, int height)
{
assert(overlay->type == MS_BUFFER_BYTE_RGBA);
rendering_buffer b(overlay->data.rgba.pixels, overlay->width, overlay->height, overlay->data.rgba.row_step);
pixel_format pf(b);
AGG2Renderer *r = AGG_RENDERER(dest);
mapserver::rect_base<int> src_rect(srcX,srcY,srcX+width,srcY+height);
r->m_renderer_base.blend_from(pf,&src_rect, dstX-srcX, dstY-srcY, unsigned(opacity * 255));
return MS_SUCCESS;
}
/* image i/o */
imageObj *agg2CreateImage(int width, int height, outputFormatObj *format, colorObj * bg)
{
imageObj *image = NULL;
if (format->imagemode != MS_IMAGEMODE_RGB && format->imagemode != MS_IMAGEMODE_RGBA) {
msSetError(MS_MISCERR,
"AGG2 driver only supports RGB or RGBA pixel models.", "agg2CreateImage()");
return image;
}
image = (imageObj *) calloc(1, sizeof (imageObj));
MS_CHECK_ALLOC(image, sizeof (imageObj), NULL);
AGG2Renderer *r = new AGG2Renderer();
/* Compute size on 64bit and check that it is compatible of the platform size_t */
AGG_INT64U bufSize64 = (AGG_INT64U)width * height * 4 * sizeof(band_type);
size_t bufSize = (size_t)bufSize64;
if( (AGG_INT64U)bufSize != bufSize64 ) {
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating " AGG_INT64U_FRMT " bytes.\n", "agg2CreateImage()",
__FILE__, __LINE__, bufSize64);
free(image);
delete r;
return NULL;
}
r->buffer = (band_type*)malloc(bufSize);
if (r->buffer == NULL) {
msSetError(MS_MEMERR, "%s: %d: Out of memory allocating " AGG_INT64U_FRMT " bytes.\n", "agg2CreateImage()",
__FILE__, __LINE__, bufSize64);
free(image);
delete r;
return NULL;
}
r->m_rendering_buffer.attach(r->buffer, width, height, width * 4);
r->m_pixel_format.attach(r->m_rendering_buffer);
r->m_compop_pixel_format.attach(r->m_rendering_buffer);
r->m_renderer_base.attach(r->m_pixel_format);
r->m_compop_renderer_base.attach(r->m_compop_pixel_format);
r->m_renderer_scanline.attach(r->m_renderer_base);
r->default_gamma = atof(msGetOutputFormatOption( format, "GAMMA", "0.75" ));
if(r->default_gamma <= 0.0 || r->default_gamma >= 1.0) {
r->default_gamma = 0.75;
}
r->gamma_function.set(0,r->default_gamma);
r->m_rasterizer_aa_gamma.gamma(r->gamma_function);
if( bg && !format->transparent )
r->m_renderer_base.clear(aggColor(bg));
else
r->m_renderer_base.clear(AGG_NO_COLOR);
if (!bg || format->transparent || format->imagemode == MS_IMAGEMODE_RGBA ) {
r->use_alpha = true;
} else {
r->use_alpha = false;
}
image->img.plugin = (void*) r;
return image;
}
int agg2SaveImage(imageObj *img, mapObj* map, FILE *fp, outputFormatObj * format)
{
return MS_FAILURE;
}
int agg2StartNewLayer(imageObj *img, mapObj*map, layerObj *layer)
{
AGG2Renderer *r = AGG_RENDERER(img);
char *sgamma = msLayerGetProcessingKey( layer, "GAMMA" );
double gamma;
if(sgamma) {
gamma = atof(sgamma);
if(gamma <= 0 || gamma >= 1) gamma = 0.75;
} else {
gamma = r->default_gamma;
}
if(r->gamma_function.end() != gamma) {
r->gamma_function.end(gamma);
r->m_rasterizer_aa_gamma.gamma(r->gamma_function);
}
return MS_SUCCESS;
}
int agg2CloseNewLayer(imageObj *img, mapObj *map, layerObj *layer)
{
return MS_SUCCESS;
}
int agg2FreeImage(imageObj * image)
{
AGG2Renderer *r = AGG_RENDERER(image);
free(r->buffer);
delete r;
image->img.plugin = NULL;
return MS_SUCCESS;
}
int agg2FreeSymbol(symbolObj * symbol)
{
return MS_SUCCESS;
}
int agg2InitCache(void **vcache)
{
aggRendererCache *cache = new aggRendererCache();
*vcache = (void*)cache;
return MS_SUCCESS;
}
int agg2Cleanup(void *vcache)
{
aggRendererCache *cache = (aggRendererCache*)vcache;
delete cache;
return MS_SUCCESS;
}
// ------------------------------------------------------------------------
// Function to create a custom hatch symbol based on an arbitrary angle.
// ------------------------------------------------------------------------
static mapserver::path_storage createHatch(double ox, double oy,
double rx, double ry,
int sx, int sy, double angle, double step)
{
mapserver::path_storage path;
//restrict the angle to [0 180[, i.e ]-pi/2,pi/2] in radians
angle = fmod(angle, 360.0);
if(angle < 0) angle += 360;
if(angle >= 180) angle -= 180;
//treat 2 easy cases which would cause divide by 0 in generic case
if(angle==0) {
double y0 = step-fmod(oy-ry,step);
if((oy - ry) < 0) {
y0 -= step;
}
for(double y=y0; y<sy; y+=step) {
path.move_to(0,y);
path.line_to(sx,y);
}
return path;
}
if(angle==90) {
double x0 = step-fmod(ox-rx,step);
if((ox - rx) < 0) {
x0 -= step;
}
for(double x=x0; x<sx; x+=step) {
path.move_to(x,0);
path.line_to(x,sy);
}
return path;
}
double theta = (90-angle)*MS_DEG_TO_RAD; /* theta in ]-pi/2 , pi/2] */
double ct = cos(theta);
double st = sin(theta);
double invct = 1.0/ct;
double invst = 1.0/st;
double r0; /* distance from first hatch line to the top-left (if angle in 0,pi/2)
or bottom-left (if angle in -pi/2,0) corner of the hatch bbox */
double rmax = sqrt((double)(sx*sx+sy*sy)); /* distance to the furthest hatch we will have to create
TODO: this could be optimized for bounding boxes where width is very different than height for
certain hatch angles */
double rref= rx*ct + ry*st; /* distance to the line passing through the refpoint, origin is
(0,0) of the imageObj */
double rcorner; /* distance to the line passing through the topleft or bottomleft corner
of the hatch bbox (origin is (0,0) of imageObj) */
/* calculate the distance from the refpoint to the top right of the path */
if(angle < 90) {
rcorner = ox*ct + oy*st;
r0 = step - fmod(rcorner-rref,step);
if(rcorner-rref<0) r0 -= step;
} else {
rcorner = ox*ct + (oy+sy)*st;
r0 = step - fmod(rcorner-rref,step);
if(rcorner-rref<0) r0 -= step;
st = -st;
invst = -invst;
}
//parametrize each line as r = x.cos(theta) + y.sin(theta)
for(double r=r0; r<rmax; r+=step) {
int inter=0;
double x,y;
double pt[4]; //array to store the coordinates of intersection of the line with the sides
//in the general case there will only be two intersections
//so pt[4] should be sufficient to store the coordinates of the intersection,
//but we allocate pt[8] to treat the special and rare/unfortunate case when the
//line is a perfect diagonal (and therfore intersects all four sides)
//note that the order for testing is important in this case so that the first