Skip to content

Commit d365785

Browse files
committed
Use numpy to generate the PDF shading triangles stream, which should be faster and more portable than using struct.
svn path=/trunk/matplotlib/; revision=7624
1 parent ea8c313 commit d365785

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from cStringIO import StringIO
1818
from datetime import datetime
1919
from math import ceil, cos, floor, pi, sin
20-
import struct
2120
try:
2221
set
2322
except NameError:
@@ -1068,11 +1067,10 @@ def writeGouraudTriangles(self):
10681067
gouraudDict[name] = ob
10691068
shape = points.shape
10701069
flat_points = points.reshape((shape[0] * shape[1], 2))
1070+
flat_colors = colors.reshape((shape[0] * shape[1], 4))
10711071
points_min = npy.min(flat_points, axis=0) - (1 << 8)
10721072
points_max = npy.max(flat_points, axis=0) + (1 << 8)
10731073
factor = float(0xffffffff) / (points_max - points_min)
1074-
adjpoints = npy.array((points - points_min) * factor, dtype=npy.uint32)
1075-
adjcolors = npy.array(colors * 255.0, dtype=npy.uint8)
10761074

10771075
self.beginStream(
10781076
ob.id, None,
@@ -1087,10 +1085,16 @@ def writeGouraudTriangles(self):
10871085
0, 1, 0, 1, 0, 1]
10881086
})
10891087

1090-
for tpoints, tcolors in zip(adjpoints, adjcolors):
1091-
for p, c in zip(tpoints, tcolors):
1092-
values = [int(x) for x in [0] + list(p) + list(c[:3])]
1093-
self.write(struct.pack('>BLLBBB', *values))
1088+
streamarr = npy.empty(
1089+
(shape[0] * shape[1],),
1090+
dtype=[('flags', 'u1'),
1091+
('points', '>u4', (2,)),
1092+
('colors', 'u1', (3,))])
1093+
streamarr['flags'] = 0
1094+
streamarr['points'] = (flat_points - points_min) * factor
1095+
streamarr['colors'] = flat_colors[:, :3] * 255.0
1096+
1097+
self.write(streamarr.tostring())
10941098
self.endStream()
10951099
self.writeObject(self.gouraudObject, gouraudDict)
10961100

@@ -1375,11 +1379,20 @@ def draw_gouraud_triangle(self, gc, points, colors, trans):
13751379
colors.reshape((1, 3, 4)), trans)
13761380

13771381
def draw_gouraud_triangles(self, gc, points, colors, trans):
1382+
assert len(points) == len(colors)
1383+
assert points.ndim == 3
1384+
assert points.shape[1] == 3
1385+
assert points.shape[2] == 2
1386+
assert colors.ndim == 3
1387+
assert colors.shape[1] == 3
1388+
assert colors.shape[2] == 4
1389+
13781390
shape = points.shape
13791391
points = points.reshape((shape[0] * shape[1], 2))
13801392
tpoints = trans.transform(points)
13811393
tpoints = tpoints.reshape(shape)
13821394
name = self.file.addGouraudTriangles(tpoints, colors)
1395+
self.check_gc(gc)
13831396
self.file.output(name, Op.shading)
13841397

13851398
def _setup_textpos(self, x, y, descent, angle, oldx=0, oldy=0, olddescent=0, oldangle=0):

0 commit comments

Comments
 (0)