/usr/share/pyshared/pyx/mesh.py is in python-pyx 0.11.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- encoding: utf-8 -*-
#
#
# Copyright (C) 2006-2011 André Wobst <wobsta@users.sourceforge.net>
#
# This file is part of PyX (http://pyx.sourceforge.net/).
#
# PyX is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PyX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyX; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
# Just a quick'n'dirty ascii art (I'll do a nice PyX plot later on):
#
#
# node1 *
# | \
# | \ neighbor2
# | \
# | \
# neighbor3 |element * node3
# | /
# | /
# | / neighbor1
# | /
# node2 *
import struct, binascii, zlib, os, tempfile
import bbox, canvasitem, color, pdfwriter, unit
class node_pt:
def __init__(self, coords_pt, value):
self.coords_pt = coords_pt
self.value = value
class node(node_pt):
def __init__(self, coords, value):
node_pt.__init__(self, [unit.topt(coord) for coord in coords], value)
class element:
def __init__(self, nodes, neighbors=None):
self.nodes = nodes
self.neighbors = neighbors
def coords24bit_pt(coords_pt, min_pt, max_pt):
return struct.pack(">I", int((coords_pt-min_pt)*16777215.0/(max_pt-min_pt)))[1:]
class PDFGenericResource(pdfwriter.PDFobject):
def __init__(self, type, name, content):
pdfwriter.PDFobject.__init__(self, type, name)
self.content = content
def write(self, file, writer, registry):
file.write(self.content)
class mesh(canvasitem.canvasitem):
def __init__(self, elements, check=1):
self.elements = elements
if check:
colorspacestring = ""
for element in elements:
if len(element.nodes) != 3:
raise ValueError("triangular mesh expected")
try:
for node in element.nodes:
if not colorspacestring:
colorspacestring = node.value.colorspacestring()
elif node.value.colorspacestring() != colorspacestring:
raise ValueError("color space mismatch")
except AttributeError:
raise ValueError("gray, rgb or cmyk color values expected")
for node in element.nodes:
if len(node.coords_pt) != 2:
raise ValueError("two dimensional coordinates expected")
def bbox(self):
return bbox.bbox_pt(min([node.coords_pt[0] for element in self.elements for node in element.nodes]),
min([node.coords_pt[1] for element in self.elements for node in element.nodes]),
max([node.coords_pt[0] for element in self.elements for node in element.nodes]),
max([node.coords_pt[1] for element in self.elements for node in element.nodes]))
def data(self, bbox):
return "".join(["\000%s%s%s" % (coords24bit_pt(node.coords_pt[0], bbox.llx_pt, bbox.urx_pt),
coords24bit_pt(node.coords_pt[1], bbox.lly_pt, bbox.ury_pt),
node.value.to8bitstring())
for element in self.elements for node in element.nodes])
def processPS(self, file, writer, context, registry, bbox):
if writer.mesh_as_bitmap:
from pyx import bitmap, canvas
import Image
c = canvas.canvas()
c.insert(self)
fd, fname = tempfile.mkstemp()
f = os.fdopen(fd, "wb")
f.close()
c.pipeGS(fname, device="pngalpha", resolution=writer.mesh_as_bitmap_resolution)
i = Image.open(fname)
os.unlink(fname)
b = bitmap.bitmap_pt(self.bbox().llx_pt, self.bbox().lly_pt, i)
# we slightly shift the bitmap to re-center it, as the bitmap might contain some additional border
# unfortunately we need to construct another bitmap instance for that ...
b = bitmap.bitmap_pt(self.bbox().llx_pt + 0.5*(self.bbox().width_pt()-b.bbox().width_pt()),
self.bbox().lly_pt + 0.5*(self.bbox().height_pt()-b.bbox().height_pt()), i)
b.processPS(file, writer, context, registry, bbox)
else:
thisbbox = self.bbox()
bbox += thisbbox
file.write("""<< /ShadingType 4
/ColorSpace %s
/BitsPerCoordinate 24
/BitsPerComponent 8
/BitsPerFlag 8
/Decode [%f %f %f %f %s]
/DataSource currentfile /ASCIIHexDecode filter /FlateDecode filter
>> shfill\n""" % (self.elements[0].nodes[0].value.colorspacestring(),
thisbbox.llx_pt, thisbbox.urx_pt, thisbbox.lly_pt, thisbbox.ury_pt,
" ".join(["0 1" for value in self.elements[0].nodes[0].value.to8bitstring()])))
file.write(binascii.b2a_hex(zlib.compress(self.data(thisbbox))))
file.write("\n")
def processPDF(self, file, writer, context, registry, bbox):
if writer.mesh_as_bitmap:
from pyx import bitmap, canvas
import Image
c = canvas.canvas()
c.insert(self)
fd, fname = tempfile.mkstemp()
f = os.fdopen(fd, "wb")
f.close()
c.pipeGS(fname, device="pngalpha", resolution=writer.mesh_as_bitmap_resolution)
i = Image.open(fname)
os.unlink(fname)
b = bitmap.bitmap_pt(self.bbox().llx_pt, self.bbox().lly_pt, i)
# we slightly shift the bitmap to re-center it, as the bitmap might contain some additional border
# unfortunately we need to construct another bitmap instance for that ...
b = bitmap.bitmap_pt(self.bbox().llx_pt + 0.5*(self.bbox().width_pt()-b.bbox().width_pt()),
self.bbox().lly_pt + 0.5*(self.bbox().height_pt()-b.bbox().height_pt()), i)
b.processPDF(file, writer, context, registry, bbox)
else:
thisbbox = self.bbox()
bbox += thisbbox
d = self.data(thisbbox)
if writer.compress:
filter = "/Filter /FlateDecode\n"
d = zlib.compress(d)
else:
filter = ""
name = "shading-%s" % id(self)
shading = PDFGenericResource("shading", name, """<<
/ShadingType 4
/ColorSpace %s
/BitsPerCoordinate 24
/BitsPerComponent 8
/BitsPerFlag 8
/Decode [%f %f %f %f %s]
/Length %i
%s>>
stream
%s
endstream\n""" % (self.elements[0].nodes[0].value.colorspacestring(),
thisbbox.llx_pt, thisbbox.urx_pt, thisbbox.lly_pt, thisbbox.ury_pt,
" ".join(["0 1" for value in self.elements[0].nodes[0].value.to8bitstring()]),
len(d), filter, d))
registry.add(shading)
registry.addresource("Shading", name, shading)
file.write("/%s sh\n" % name)
|