FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/src/_backend_agg.cpp at master · staticfloat/matplotlib · GitHub
staticfloat
matplotlib
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
src
/
_backend_agg.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
2515 lines (2121 loc) · 71.9 KB
Breadcrumbs
matplotlib
/
src
/
_backend_agg.cpp
Copy path
File metadata and controls
2515 lines (2121 loc) · 71.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
-*- mode: c++; c-basic-offset: 4 -*-
*/
/*
A rewrite of _backend_agg using PyCXX to handle ref counting, etc..
*/
/*
Python API mandates Python.h is included *first*
*/
#
include
"
Python.h
"
#
include
"
ft2font.h
"
#
include
"
_image.h
"
#
include
"
_backend_agg.h
"
#
include
"
mplutils.h
"
#
include
<
iostream
>
#
include
<
fstream
>
#
include
<
cmath
>
#
include
<
cstdio
>
#
include
<
stdexcept
>
#
include
<
time.h
>
#
include
<
algorithm
>
#
include
"
agg_conv_curve.h
"
#
include
"
agg_conv_transform.h
"
#
include
"
agg_image_accessors.h
"
#
include
"
agg_renderer_primitives.h
"
#
include
"
agg_scanline_storage_aa.h
"
#
include
"
agg_scanline_storage_bin.h
"
#
include
"
agg_span_allocator.h
"
#
include
"
agg_span_converter.h
"
#
include
"
agg_span_image_filter_gray.h
"
#
include
"
agg_span_image_filter_rgba.h
"
#
include
"
agg_span_interpolator_linear.h
"
#
include
"
agg_span_pattern_rgba.h
"
#
include
"
agg_span_gouraud_rgba.h
"
#
include
"
agg_conv_shorten_path.h
"
#
include
"
util/agg_color_conv_rgb8.h
"
#
include
"
MPL_isnan.h
"
#
include
"
numpy/arrayobject.h
"
#
include
"
agg_py_transforms.h
"
#
ifndef
M_PI
#
define
M_PI
3.14159265358979323846
#
endif
#
ifndef
M_PI_4
#
define
M_PI_4
0.785398163397448309616
#
endif
#
ifndef
M_PI_2
#
define
M_PI_2
1.57079632679489661923
#
endif
/*
Convert dashes from the Python representation as nested sequences to
the C++ representation as a std::vector<std::pair<double, double> >
(GCAgg::dash_t)
*/
void
convert_dashes
(
const
Py::Tuple& dashes,
double
dpi,
GCAgg::
dash_t
& dashes_out,
double
& dashOffset_out)
{
if
(dashes.
length
() !=
2
)
{
throw
Py::ValueError
(
Printf
(
"
Dash descriptor must be a length 2 tuple; found %d
"
,
dashes.
length
()).
str
()
);
}
dashes_out.
clear
();
dashOffset_out =
0.0
;
if
(dashes[
0
].
ptr
() == Py_None)
{
return
;
}
dashOffset_out =
double
(
Py::Float
(dashes[
0
])) * dpi /
72.0
;
Py::SeqBase<Py::Object> dashSeq = dashes[
1
];
size_t
Ndash = dashSeq.
length
();
if
(Ndash %
2
!=
0
)
{
throw
Py::ValueError
(
Printf
(
"
Dash sequence must be an even length sequence; found %d
"
, Ndash).
str
()
);
}
dashes_out.
clear
();
dashes_out.
reserve
(Ndash /
2
);
double
val0, val1;
for
(
size_t
i =
0
; i < Ndash; i +=
2
)
{
val0 =
double
(
Py::Float
(dashSeq[i])) * dpi /
72.0
;
val1 =
double
(
Py::Float
(dashSeq[i+
1
])) * dpi /
72.0
;
dashes_out.
push_back
(
std::make_pair
(val0, val1));
}
}
Py::Object
BufferRegion::to_string
(
const
Py::Tuple &args)
{
//
owned=true to prevent memory leak
#
if
PY3K
return
Py::Bytes
#
else
return
Py::String
#
endif
(
PyBytes_FromStringAndSize
((
const
char
*)data, height*stride),
true
);
}
Py::Object
BufferRegion::set_x
(
const
Py::Tuple &args)
{
args.
verify_length
(
1
);
size_t
x = (
long
)
Py::Int
(args[
0
]);
rect.
x1
= x;
return
Py::Object
();
}
Py::Object
BufferRegion::set_y
(
const
Py::Tuple &args)
{
args.
verify_length
(
1
);
size_t
y = (
long
)
Py::Int
(args[
0
]);
rect.
y1
= y;
return
Py::Object
();
}
Py::Object
BufferRegion::get_extents
(
const
Py::Tuple &args)
{
args.
verify_length
(
0
);
Py::Tuple
extents
(
4
);
extents[
0
] =
Py::Int
(rect.
x1
);
extents[
1
] =
Py::Int
(rect.
y1
);
extents[
2
] =
Py::Int
(rect.
x2
);
extents[
3
] =
Py::Int
(rect.
y2
);
return
extents;
}
Py::Object
BufferRegion::to_string_argb
(
const
Py::Tuple &args)
{
//
owned=true to prevent memory leak
Py_ssize_t length;
unsigned
char
* pix;
unsigned
char
* begin;
unsigned
char
* end;
unsigned
char
tmp;
size_t
i, j;
PyObject* str =
PyBytes_FromStringAndSize
((
const
char
*)data, height * stride);
if
(
PyBytes_AsStringAndSize
(str, (
char
**)&begin, &length))
{
throw
Py::TypeError
(
"
Could not create memory for blit
"
);
}
pix = begin;
end = begin + (height * stride);
for
(i =
0
; i < (
size_t
)height; ++i)
{
pix = begin + i * stride;
for
(j =
0
; j < (
size_t
)width; ++j)
{
//
Convert rgba to argb
tmp = pix[
2
];
pix[
2
] = pix[
0
];
pix[
0
] = tmp;
pix +=
4
;
}
}
#
if
PY3K
return
Py::Bytes
#
else
return
Py::String
#
endif
(str,
true
);
}
GCAgg::GCAgg
(
const
Py::Object &gc,
double
dpi) :
dpi(dpi), isaa(
true
), dashOffset(
0.0
)
{
_VERBOSE
(
"
GCAgg::GCAgg
"
);
linewidth =
points_to_pixels
(gc.
getAttr
(
"
_linewidth
"
)) ;
alpha =
Py::Float
(gc.
getAttr
(
"
_alpha
"
));
color =
get_color
(gc);
_set_antialiased
(gc);
_set_linecap
(gc);
_set_joinstyle
(gc);
_set_dashes
(gc);
_set_clip_rectangle
(gc);
_set_clip_path
(gc);
_set_snap
(gc);
_set_hatch_path
(gc);
}
void
GCAgg::_set_antialiased
(
const
Py::Object& gc)
{
_VERBOSE
(
"
GCAgg::antialiased
"
);
isaa =
Py::Boolean
(gc.
getAttr
(
"
_antialiased
"
));
}
agg::rgba
GCAgg::get_color
(
const
Py::Object& gc)
{
_VERBOSE
(
"
GCAgg::get_color
"
);
Py::Tuple rgb =
Py::Tuple
(gc.
getAttr
(
"
_rgb
"
));
double
alpha =
Py::Float
(gc.
getAttr
(
"
_alpha
"
));
double
r =
Py::Float
(rgb[
0
]);
double
g =
Py::Float
(rgb[
1
]);
double
b =
Py::Float
(rgb[
2
]);
return
agg::rgba
(r, g, b, alpha);
}
double
GCAgg::points_to_pixels
(
const
Py::Object& points)
{
_VERBOSE
(
"
GCAgg::points_to_pixels
"
);
double
p =
Py::Float
(points) ;
return
p * dpi /
72.0
;
}
void
GCAgg::_set_linecap
(
const
Py::Object& gc)
{
_VERBOSE
(
"
GCAgg::_set_linecap
"
);
std::string capstyle =
Py::String
(gc.
getAttr
(
"
_capstyle
"
));
if
(capstyle ==
"
butt
"
)
{
cap = agg::butt_cap;
}
else
if
(capstyle ==
"
round
"
)
{
cap = agg::round_cap;
}
else
if
(capstyle ==
"
projecting
"
)
{
cap = agg::square_cap;
}
else
{
throw
Py::ValueError
(
Printf
(
"
GC _capstyle attribute must be one of butt, round, projecting; found %s
"
, capstyle.
c_str
()).
str
());
}
}
void
GCAgg::_set_joinstyle
(
const
Py::Object& gc)
{
_VERBOSE
(
"
GCAgg::_set_joinstyle
"
);
std::string joinstyle =
Py::String
(gc.
getAttr
(
"
_joinstyle
"
));
if
(joinstyle ==
"
miter
"
)
{
join = agg::miter_join_revert;
}
else
if
(joinstyle ==
"
round
"
)
{
join = agg::round_join;
}
else
if
(joinstyle ==
"
bevel
"
)
{
join = agg::bevel_join;
}
else
{
throw
Py::ValueError
(
Printf
(
"
GC _joinstyle attribute must be one of butt, round, projecting; found %s
"
, joinstyle.
c_str
()).
str
());
}
}
void
GCAgg::_set_dashes
(
const
Py::Object& gc)
{
//
return the dashOffset, dashes sequence tuple.
_VERBOSE
(
"
GCAgg::_set_dashes
"
);
Py::Object
dash_obj
(gc.
getAttr
(
"
_dashes
"
));
if
(dash_obj.
ptr
() == Py_None)
{
dashes.
clear
();
return
;
}
convert_dashes
(dash_obj, dpi, dashes, dashOffset);
}
void
GCAgg::_set_clip_rectangle
(
const
Py::Object& gc)
{
//
set the clip rectangle from the gc
_VERBOSE
(
"
GCAgg::_set_clip_rectangle
"
);
Py::Object
o
(gc.
getAttr
(
"
_cliprect
"
));
cliprect = o;
}
void
GCAgg::_set_clip_path
(
const
Py::Object& gc)
{
//
set the clip path from the gc
_VERBOSE
(
"
GCAgg::_set_clip_path
"
);
Py::Object method_obj = gc.
getAttr
(
"
get_clip_path
"
);
Py::Callable
method
(method_obj);
Py::Tuple path_and_transform = method.
apply
(
Py::Tuple
());
if
(path_and_transform[
0
].
ptr
() != Py_None)
{
clippath = path_and_transform[
0
];
clippath_trans =
py_to_agg_transformation_matrix
(path_and_transform[
1
].
ptr
());
}
}
void
GCAgg::_set_snap
(
const
Py::Object& gc)
{
//
set the snap setting
_VERBOSE
(
"
GCAgg::_set_snap
"
);
Py::Object method_obj = gc.
getAttr
(
"
get_snap
"
);
Py::Callable
method
(method_obj);
Py::Object py_snap = method.
apply
(
Py::Tuple
());
if
(py_snap.
isNone
())
{
snap_mode =
SNAP_AUTO
;
}
else
if
(py_snap.
isTrue
())
{
snap_mode =
SNAP_TRUE
;
}
else
{
snap_mode =
SNAP_FALSE
;
}
}
void
GCAgg::_set_hatch_path
(
const
Py::Object& gc)
{
_VERBOSE
(
"
GCAgg::_set_hatch_path
"
);
Py::Object method_obj = gc.
getAttr
(
"
get_hatch_path
"
);
Py::Callable
method
(method_obj);
hatchpath = method.
apply
(
Py::Tuple
());
if
(hatchpath.
ptr
() ==
NULL
)
throw
Py::Exception
();
}
const
size_t
RendererAgg::PIXELS_PER_INCH
(
96
);
RendererAgg::RendererAgg
(
unsigned
int
width,
unsigned
int
height,
double
dpi,
int
debug) :
width(width),
height(height),
dpi(dpi),
NUMBYTES(width*height*
4
),
pixBuffer(
NULL
),
renderingBuffer(),
alphaBuffer(
NULL
),
alphaMaskRenderingBuffer(),
alphaMask(alphaMaskRenderingBuffer),
pixfmtAlphaMask(alphaMaskRenderingBuffer),
rendererBaseAlphaMask(),
rendererAlphaMask(),
scanlineAlphaMask(),
slineP8(),
slineBin(),
pixFmt(),
rendererBase(),
rendererAA(),
rendererBin(),
theRasterizer(),
debug(debug)
{
_VERBOSE
(
"
RendererAgg::RendererAgg
"
);
unsigned
stride
(width*
4
);
pixBuffer =
new
agg::int8u[
NUMBYTES
];
renderingBuffer.
attach
(pixBuffer, width, height, stride);
pixFmt.
attach
(renderingBuffer);
rendererBase.
attach
(pixFmt);
rendererBase.
clear
(
agg::rgba
(
1
,
1
,
1
,
0
));
rendererAA.
attach
(rendererBase);
rendererBin.
attach
(rendererBase);
hatchRenderingBuffer.
attach
(hatchBuffer,
HATCH_SIZE
,
HATCH_SIZE
,
HATCH_SIZE
*
4
);
}
void
RendererAgg::create_alpha_buffers
()
{
if
(!alphaBuffer)
{
unsigned
stride
(width*
4
);
alphaBuffer =
new
agg::int8u[
NUMBYTES
];
alphaMaskRenderingBuffer.
attach
(alphaBuffer, width, height, stride);
rendererBaseAlphaMask.
attach
(pixfmtAlphaMask);
rendererAlphaMask.
attach
(rendererBaseAlphaMask);
}
}
template
<
class
R
>
void
RendererAgg::set_clipbox
(
const
Py::Object& cliprect, R& rasterizer)
{
//
set the clip rectangle from the gc
_VERBOSE
(
"
RendererAgg::set_clipbox
"
);
double
l, b, r, t;
if
(
py_convert_bbox
(cliprect.
ptr
(), l, b, r, t))
{
rasterizer.
clip_box
(
std::max
(
int
(
floor
(l -
0.5
)),
0
),
std::max
(
int
(
floor
(height - b -
0.5
)),
0
),
std::min
(
int
(
floor
(r -
0.5
)),
int
(width)),
std::min
(
int
(
floor
(height - t -
0.5
)),
int
(height)));
}
else
{
rasterizer.
clip_box
(
0
,
0
, width, height);
}
_VERBOSE
(
"
RendererAgg::set_clipbox done
"
);
}
std::pair<
bool
, agg::rgba>
RendererAgg::_get_rgba_face
(
const
Py::Object& rgbFace,
double
alpha)
{
_VERBOSE
(
"
RendererAgg::_get_rgba_face
"
);
std::pair<
bool
, agg::rgba> face;
if
(rgbFace.
ptr
() == Py_None)
{
face.
first
=
false
;
}
else
{
face.
first
=
true
;
Py::Tuple rgb =
Py::Tuple
(rgbFace);
face.
second
=
rgb_to_color
(rgb, alpha);
}
return
face;
}
Py::Object
RendererAgg::copy_from_bbox
(
const
Py::Tuple& args)
{
//
copy region in bbox to buffer and return swig/agg buffer object
args.
verify_length
(
1
);
Py::Object box_obj = args[
0
];
double
l, b, r, t;
if
(!
py_convert_bbox
(box_obj.
ptr
(), l, b, r, t))
{
throw
Py::TypeError
(
"
Invalid bbox provided to copy_from_bbox
"
);
}
agg::rect_i
rect
((
int
)l, height - (
int
)t, (
int
)r, height - (
int
)b);
BufferRegion* reg =
NULL
;
try
{
reg =
new
BufferRegion
(rect,
true
);
}
catch
(...)
{
throw
Py::MemoryError
(
"
RendererAgg::copy_from_bbox could not allocate memory for buffer
"
);
}
if
(!reg)
{
throw
Py::MemoryError
(
"
RendererAgg::copy_from_bbox could not allocate memory for buffer
"
);
}
try
{
agg::rendering_buffer rbuf;
rbuf.
attach
(reg->
data
, reg->
width
, reg->
height
, reg->
stride
);
pixfmt
pf
(rbuf);
renderer_base
rb
(pf);
rb.
copy_from
(renderingBuffer, &rect, -rect.
x1
, -rect.
y1
);
}
catch
(...)
{
delete
reg;
throw
Py::RuntimeError
(
"
An unknown error occurred in copy_from_bbox
"
);
}
return
Py::asObject
(reg);
}
Py::Object
RendererAgg::restore_region
(
const
Py::Tuple& args)
{
//
copy BufferRegion to buffer
args.
verify_length
(
1
);
BufferRegion* region =
static_cast
<BufferRegion*>(args[
0
].
ptr
());
if
(region->
data
==
NULL
)
{
throw
Py::ValueError
(
"
Cannot restore_region from NULL data
"
);
}
agg::rendering_buffer rbuf;
rbuf.
attach
(region->
data
,
region->
width
,
region->
height
,
region->
stride
);
rendererBase.
copy_from
(rbuf,
0
, region->
rect
.
x1
, region->
rect
.
y1
);
return
Py::Object
();
}
//
Restore the part of the saved region with offsets
Py::Object
RendererAgg::restore_region2
(
const
Py::Tuple& args)
{
//
copy BufferRegion to buffer
args.
verify_length
(
7
);
int
x
(
0
),
y
(
0
),
xx1
(
0
),
yy1
(
0
),
xx2
(
0
),
yy2
(
0
);
try
{
xx1 =
Py::Int
(args[
1
]);
yy1 =
Py::Int
(args[
2
]);
xx2 =
Py::Int
(args[
3
]);
yy2 =
Py::Int
(args[
4
]);
x =
Py::Int
(args[
5
]);
y =
Py::Int
(args[
6
]);
}
catch
(Py::TypeError)
{
throw
Py::TypeError
(
"
Invalid input arguments to restore_region2
"
);
}
BufferRegion* region =
static_cast
<BufferRegion*>(args[
0
].
ptr
());
if
(region->
data
==
NULL
)
{
throw
Py::ValueError
(
"
Cannot restore_region from NULL data
"
);
}
agg::rect_i
rect
(xx1 - region->
rect
.
x1
, (yy1 - region->
rect
.
y1
),
xx2 - region->
rect
.
x1
, (yy2 - region->
rect
.
y1
));
agg::rendering_buffer rbuf;
rbuf.
attach
(region->
data
,
region->
width
,
region->
height
,
region->
stride
);
rendererBase.
copy_from
(rbuf, &rect, x, y);
return
Py::Object
();
}
bool
RendererAgg::render_clippath
(
const
Py::Object& clippath,
const
agg::trans_affine& clippath_trans)
{
typedef
agg::conv_transform<PathIterator>
transformed_path_t
;
typedef
agg::conv_curve<
transformed_path_t
>
curve_t
;
bool
has_clippath = (clippath.
ptr
() != Py_None);
if
(has_clippath &&
(clippath.
ptr
() != lastclippath.
ptr
() ||
clippath_trans != lastclippath_transform))
{
create_alpha_buffers
();
agg::trans_affine
trans
(clippath_trans);
trans *=
agg::trans_affine_scaling
(
1.0
, -
1.0
);
trans *=
agg::trans_affine_translation
(
0.0
, (
double
)height);
PathIterator
clippath_iter
(clippath);
rendererBaseAlphaMask.
clear
(
agg::gray8
(
0
,
0
));
transformed_path_t
transformed_clippath
(clippath_iter, trans);
agg::conv_curve<
transformed_path_t
>
curved_clippath
(transformed_clippath);
theRasterizer.
add_path
(curved_clippath);
rendererAlphaMask.
color
(
agg::gray8
(
255
,
255
));
agg::render_scanlines
(theRasterizer, scanlineAlphaMask, rendererAlphaMask);
lastclippath = clippath;
lastclippath_transform = clippath_trans;
}
return
has_clippath;
}
#
define
MARKER_CACHE_SIZE
512
Py::Object
RendererAgg::draw_markers
(
const
Py::Tuple& args)
{
typedef
agg::conv_transform<PathIterator>
transformed_path_t
;
typedef
PathSnapper<
transformed_path_t
>
snap_t
;
typedef
agg::conv_curve<
snap_t
>
curve_t
;
typedef
agg::conv_stroke<
curve_t
>
stroke_t
;
typedef
agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
typedef
agg::renderer_base<pixfmt_amask_type> amask_ren_type;
typedef
agg::renderer_scanline_aa_solid<amask_ren_type> amask_aa_renderer_type;
typedef
agg::renderer_scanline_bin_solid<amask_ren_type> amask_bin_renderer_type;
args.
verify_length
(
5
,
6
);
Py::Object gc_obj = args[
0
];
Py::Object marker_path_obj = args[
1
];
agg::trans_affine marker_trans =
py_to_agg_transformation_matrix
(args[
2
].
ptr
());
Py::Object path_obj = args[
3
];
agg::trans_affine trans =
py_to_agg_transformation_matrix
(args[
4
].
ptr
());
Py::Object face_obj;
if
(args.
size
() ==
6
)
{
face_obj = args[
5
];
}
GCAgg
gc
(gc_obj, dpi);
//
Deal with the difference in y-axis direction
marker_trans *=
agg::trans_affine_scaling
(
1.0
, -
1.0
);
trans *=
agg::trans_affine_scaling
(
1.0
, -
1.0
);
trans *=
agg::trans_affine_translation
(
0.5
, (
double
)height +
0.5
);
PathIterator
marker_path
(marker_path_obj);
transformed_path_t
marker_path_transformed
(marker_path, marker_trans);
snap_t
marker_path_snapped
(marker_path_transformed,
gc.
snap_mode
,
marker_path.
total_vertices
(),
gc.
linewidth
);
curve_t
marker_path_curve
(marker_path_snapped);
PathIterator
path
(path_obj);
transformed_path_t
path_transformed
(path, trans);
snap_t
path_snapped
(path_transformed,
SNAP_TRUE
,
path.
total_vertices
(),
0.0
);
curve_t
path_curve
(path_snapped);
path_curve.
rewind
(
0
);
facepair_t
face =
_get_rgba_face
(face_obj, gc.
alpha
);
//
maxim's suggestions for cached scanlines
agg::scanline_storage_aa8 scanlines;
theRasterizer.
reset
();
theRasterizer.
reset_clipping
();
rendererBase.
reset_clipping
(
true
);
agg::int8u staticFillCache[
MARKER_CACHE_SIZE
];
agg::int8u staticStrokeCache[
MARKER_CACHE_SIZE
];
agg::int8u* fillCache = staticFillCache;
agg::int8u* strokeCache = staticStrokeCache;
try
{
unsigned
fillSize =
0
;
if
(face.
first
)
{
theRasterizer.
add_path
(marker_path_curve);
agg::render_scanlines
(theRasterizer, slineP8, scanlines);
fillSize = scanlines.
byte_size
();
if
(fillSize >=
MARKER_CACHE_SIZE
)
{
fillCache =
new
agg::int8u[fillSize];
}
scanlines.
serialize
(fillCache);
}
stroke_t
stroke
(marker_path_curve);
stroke.
width
(gc.
linewidth
);
stroke.
line_cap
(gc.
cap
);
stroke.
line_join
(gc.
join
);
theRasterizer.
reset
();
theRasterizer.
add_path
(stroke);
agg::render_scanlines
(theRasterizer, slineP8, scanlines);
unsigned
strokeSize = scanlines.
byte_size
();
if
(strokeSize >=
MARKER_CACHE_SIZE
)
{
strokeCache =
new
agg::int8u[strokeSize];
}
scanlines.
serialize
(strokeCache);
theRasterizer.
reset_clipping
();
rendererBase.
reset_clipping
(
true
);
set_clipbox
(gc.
cliprect
, rendererBase);
bool
has_clippath =
render_clippath
(gc.
clippath
, gc.
clippath_trans
);
double
x, y;
agg::serialized_scanlines_adaptor_aa8 sa;
agg::serialized_scanlines_adaptor_aa8::embedded_scanline sl;
agg::rect_d
clipping_rect
(
-(scanlines.
min_x
() +
1.0
),
-(scanlines.
min_y
() +
1.0
),
width + scanlines.
max_x
() +
1.0
,
height + scanlines.
max_y
() +
1.0
);
if
(has_clippath)
{
while
(path_curve.
vertex
(&x, &y) != agg::path_cmd_stop)
{
if
(
MPL_notisfinite64
(x) ||
MPL_notisfinite64
(y))
{
continue
;
}
x =
floor
(x);
y =
floor
(y);
//
Cull points outside the boundary of the image.
//
Values that are too large may overflow and create
//
segfaults.
//
http://sourceforge.net/tracker/?func=detail&aid=2865490&group_id=80706&atid=560720
if
(!clipping_rect.
hit_test
(x, y))
{
continue
;
}
pixfmt_amask_type
pfa
(pixFmt, alphaMask);
amask_ren_type
r
(pfa);
amask_aa_renderer_type
ren
(r);
if
(face.
first
)
{
ren.
color
(face.
second
);
sa.
init
(fillCache, fillSize, x, y);
agg::render_scanlines
(sa, sl, ren);
}
ren.
color
(gc.
color
);
sa.
init
(strokeCache, strokeSize, x, y);
agg::render_scanlines
(sa, sl, ren);
}
}
else
{
while
(path_curve.
vertex
(&x, &y) != agg::path_cmd_stop)
{
if
(
MPL_notisfinite64
(x) ||
MPL_notisfinite64
(y))
{
continue
;
}
x =
floor
(x);
y =
floor
(y);
//
Cull points outside the boundary of the image.
//
Values that are too large may overflow and create
//
segfaults.
//
http://sourceforge.net/tracker/?func=detail&aid=2865490&group_id=80706&atid=560720
if
(!clipping_rect.
hit_test
(x, y))
{
continue
;
}
if
(face.
first
)
{
rendererAA.
color
(face.
second
);
sa.
init
(fillCache, fillSize, x, y);
agg::render_scanlines
(sa, sl, rendererAA);
}
rendererAA.
color
(gc.
color
);
sa.
init
(strokeCache, strokeSize, x, y);
agg::render_scanlines
(sa, sl, rendererAA);
}
}
}
catch
(...)
{
if
(fillCache != staticFillCache)
delete[]
fillCache;
if
(strokeCache != staticStrokeCache)
delete[]
strokeCache;
theRasterizer.
reset_clipping
();
rendererBase.
reset_clipping
(
true
);
throw
;
}
if
(fillCache != staticFillCache)
delete[]
fillCache;
if
(strokeCache != staticStrokeCache)
delete[]
strokeCache;
theRasterizer.
reset_clipping
();
rendererBase.
reset_clipping
(
true
);
return
Py::Object
();
}
/*
*
* This is a custom span generator that converts spans in the
* 8-bit inverted greyscale font buffer to rgba that agg can use.
*/
template
<
class
ChildGenerator
>
class
font_to_rgba
{
public:
typedef
ChildGenerator child_type;
typedef
agg::rgba8 color_type;
typedef
typename
child_type::color_type child_color_type;
typedef
agg::span_allocator<child_color_type> span_alloc_type;
private:
child_type* _gen;
color_type _color;
span_alloc_type _allocator;
public:
font_to_rgba
(child_type* gen, color_type color) :
_gen
(gen),
_color
(color)
{
}
inline
void
generate
(color_type* output_span,
int
x,
int
y,
unsigned
len)
{
_allocator.
allocate
(len);
child_color_type* input_span = _allocator.
span
();
_gen->
generate
(input_span, x, y, len);
do
{
*output_span = _color;
output_span->
a
= ((
unsigned
int
)_color.
a
*
(
unsigned
int
)input_span->
v
) >>
8
;
++output_span;
++input_span;
}
while
(--len);
}
void
prepare
()
{
_gen->
prepare
();
}
};
//
MGDTODO: Support clip paths
Py::Object
RendererAgg::draw_text_image
(
const
Py::Tuple& args)
{
_VERBOSE
(
"
RendererAgg::draw_text
"
);
typedef
agg::span_allocator<agg::gray8> gray_span_alloc_type;
typedef
agg::span_allocator<agg::rgba8> color_span_alloc_type;
typedef
agg::span_interpolator_linear<> interpolator_type;
typedef
agg::image_accessor_clip<agg::pixfmt_gray8> image_accessor_type;
typedef
agg::span_image_filter_gray<image_accessor_type,
interpolator_type> image_span_gen_type;
typedef
font_to_rgba<image_span_gen_type> span_gen_type;
typedef
agg::renderer_scanline_aa<renderer_base, color_span_alloc_type,
span_gen_type> renderer_type;
args.
verify_length
(
5
);
const
unsigned
char
* buffer =
NULL
;
int
width, height;
Py::Object image_obj = args[
0
];
if
(
PyArray_Check
(image_obj.
ptr
()))
{
PyObject* image_array =
PyArray_FromObject
(
image_obj.
ptr
(), PyArray_UBYTE,
2
,
2
);
if
(!image_array)
{
throw
Py::ValueError
(
"
First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array.
"
);
}
image_obj =
Py::Object
(image_array,
true
);
buffer = (
unsigned
char
*)
PyArray_DATA
(image_array);
width =
PyArray_DIM
(image_array,
1
);
height =
PyArray_DIM
(image_array,
0
);
}
else
{
FT2Image* image =
static_cast
<FT2Image *>(
Py::getPythonExtensionBase
(image_obj.
ptr
()));
if
(!image->
get_buffer
())
{
throw
Py::ValueError
(
"
First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array.
"
);
}
buffer = image->
get_buffer
();
width = image->
get_width
();
height = image->
get_height
();
}
int
x
(
0
),
y
(
0
);
try
{
x =
Py::Int
(args[
1
]);
y =
Py::Int
(args[
2
]);
}
catch
(Py::TypeError)
{
throw
Py::TypeError
(
"
Invalid input arguments to draw_text_image
"
);
}
double
angle =
Py::Float
(args[
3
]);
GCAgg
gc
(args[
4
], dpi);
theRasterizer.
reset_clipping
();
rendererBase.
reset_clipping
(
true
);
set_clipbox
(gc.
cliprect
, theRasterizer);
agg::rendering_buffer
srcbuf
((agg::int8u*)buffer, width, height, width);
agg::pixfmt_gray8
pixf_img
(srcbuf);
agg::trans_affine mtx;
mtx *=
agg::trans_affine_translation
(
0
, -height);
mtx *=
agg::trans_affine_rotation
(-angle * agg::pi /
180.0
);
mtx *=
agg::trans_affine_translation
(x, y);
agg::path_storage rect;
rect.
move_to
(
0
,
0
);
rect.
line_to
(width,
0
);
rect.
line_to
(width, height);
rect.
line_to
(
0
, height);
rect.
line_to
(
0
,
0
);
agg::conv_transform<agg::path_storage>
rect2
(rect, mtx);
agg::trans_affine
inv_mtx
(mtx);
inv_mtx.
invert
();
agg::image_filter_lut filter;
filter.
calculate
(
agg::image_filter_spline36
());
interpolator_type
interpolator
(inv_mtx);
color_span_alloc_type sa;
image_accessor_type
ia
(pixf_img,
0
);
image_span_gen_type
image_span_generator
(ia, interpolator, filter);
span_gen_type
output_span_generator
(&image_span_generator, gc.
color
);
renderer_type
ri
(rendererBase, sa, output_span_generator);
theRasterizer.
add_path
(rect2);
agg::render_scanlines
(theRasterizer, slineP8, ri);
return
Py::Object
();
}
class
span_conv_alpha
{
public:
typedef
agg::rgba8 color_type;
double
m_alpha;
span_conv_alpha
(
double
alpha) :
m_alpha
(alpha)
{
}
void
prepare
() {}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL