1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2004-2011 André Wobst <wobsta@users.sourceforge.net>
5 # Copyright (C) 2011 Michael Schindler<m-schindler@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import struct
, warnings
, binascii
30 import bbox
, canvasitem
, pswriter
, pdfwriter
, trafo
, unit
32 devicenames
= {"L": "/DeviceGray",
34 "CMYK": "/DeviceCMYK"}
35 decodestrings
= {"L": "[0 1]",
36 "RGB": "[0 1 0 1 0 1]",
37 "CMYK": "[0 1 0 1 0 1 0 1]",
41 def ascii85lines(datalen
):
44 return (datalen
+ 56)/60
46 def ascii85stream(file, data
):
47 """Encodes the string data in ASCII85 and writes it to
48 the stream file. The number of lines written to the stream
49 is known just from the length of the data by means of the
50 ascii85lines function. Note that the tailing newline character
51 of the last line is not added by this function, but it is taken
52 into account in the ascii85lines function."""
53 i
= 3 # go on smoothly in case of data length equals zero
55 l
= [None, None, None, None]
56 for i
in range(len(data
)):
60 if i
%60 == 3 and i
!= 3:
64 # l[3], c5 = divmod(256*256*256*l[0]+256*256*l[1]+256*l[2]+l[3], 85)
65 # l[2], c4 = divmod(l[3], 85)
66 # we have to avoid number > 2**31 by
67 l
[3], c5
= divmod(256*256*l
[0]+256*256*l
[1]+256*l
[2]+l
[3], 85)
68 l
[2], c4
= divmod(256*256*3*l
[0]+l
[3], 85)
69 l
[1], c3
= divmod(l
[2], 85)
70 c1
, c2
= divmod(l
[1], 85)
71 file.write(struct
.pack('BBBBB', c1
+33, c2
+33, c3
+33, c4
+33, c5
+33))
75 for j
in range((i
%4) + 1, 4):
77 l
[3], c5
= divmod(256*256*l
[0]+256*256*l
[1]+256*l
[2]+l
[3], 85)
78 l
[2], c4
= divmod(256*256*3*l
[0]+l
[3], 85)
79 l
[1], c3
= divmod(l
[2], 85)
80 c1
, c2
= divmod(l
[1], 85)
81 file.write(struct
.pack('BBBB', c1
+33, c2
+33, c3
+33, c4
+33)[:(i
%4)+2])
83 _asciihexlinelength
= 64
84 def asciihexlines(datalen
):
85 return (datalen
*2 + _asciihexlinelength
- 1) / _asciihexlinelength
87 def asciihexstream(file, data
):
88 hexdata
= binascii
.b2a_hex(data
)
89 for i
in range((len(hexdata
)-1)/_asciihexlinelength
+ 1):
90 file.write(hexdata
[i
*_asciihexlinelength
: i
*_asciihexlinelength
+_asciihexlinelength
])
96 def __init__(self
, width
, height
, mode
, data
, compressed
=None, palette
=None):
97 if width
<= 0 or height
<= 0:
98 raise ValueError("valid image size")
99 if mode
not in ["L", "RGB", "CMYK", "LA", "RGBA", "CMYKA", "AL", "ARGB", "ACMYK"]:
100 raise ValueError("invalid mode")
101 if compressed
is None and len(mode
)*width
*height
!= len(data
):
102 raise ValueError("wrong size of uncompressed data")
103 self
.size
= width
, height
106 self
.compressed
= compressed
107 self
.palette
= palette
110 if self
.compressed
is not None:
111 raise RuntimeError("cannot extract bands from compressed image")
112 bands
= len(self
.mode
)
113 width
, height
= self
.size
114 return [image(width
, height
, "L", "".join([self
.data
[i
*bands
+band
]
115 for i
in range(width
*height
)]))
116 for band
in range(bands
)]
118 def tostring(self
, *args
):
120 raise RuntimeError("encoding not supported in this implementation")
123 def convert(self
, model
):
124 raise RuntimeError("color model conversion not supported in this implementation")
127 class jpegimage(image
):
129 def __init__(self
, file):
133 data
= open(file, "rb").read()
138 if data
[pos
] == "\377" and data
[pos
+1] not in ["\0", "\377"]:
139 # print "marker: 0x%02x \\%03o" % (ord(data[pos+1]), ord(data[pos+1]))
140 if data
[pos
+1] == "\330":
144 elif not nestinglevel
:
145 raise ValueError("begin marker expected")
146 elif data
[pos
+1] == "\331":
151 elif data
[pos
+1] in ["\300", "\301"]:
152 l
, bits
, height
, width
, components
= struct
.unpack(">HBHHB", data
[pos
+2:pos
+10])
154 raise ValueError("implementation limited to 8 bit per component only")
156 mode
= {1: "L", 3: "RGB", 4: "CMYK"}[components
]
158 raise ValueError("invalid number of components")
160 elif data
[pos
+1] == "\340":
161 l
, id, major
, minor
, dpikind
, xdpi
, ydpi
= struct
.unpack(">H5sBBBHH", data
[pos
+2:pos
+16])
163 self
.info
= {"dpi": (xdpi
, ydpi
)}
165 self
.info
= {"dpi": (xdpi
*2.54, ydpi
*2.45)}
166 # else do not provide dpi information
170 raise ValueError("end marker expected")
171 image
.__init
__(self
, width
, height
, mode
, data
[begin
:end
], compressed
="DCT")
174 class PSimagedata(pswriter
.PSresource
):
176 def __init__(self
, name
, data
, singlestring
, maxstrlen
):
177 pswriter
.PSresource
.__init
__(self
, "imagedata", name
)
179 self
.singlestring
= singlestring
180 self
.maxstrlen
= maxstrlen
182 def output(self
, file, writer
, registry
):
183 file.write("%%%%BeginRessource: %s\n" % self
.id)
184 if self
.singlestring
:
185 file.write("%%%%BeginData: %i ASCII Lines\n"
186 "<~" % ascii85lines(len(self
.data
)))
187 ascii85stream(file, self
.data
)
191 datalen
= len(self
.data
)
192 tailpos
= datalen
- datalen
% self
.maxstrlen
193 file.write("%%%%BeginData: %i ASCII Lines\n" %
194 ((tailpos
/self
.maxstrlen
) * ascii85lines(self
.maxstrlen
) +
195 ascii85lines(datalen
-tailpos
)))
197 for i
in xrange(0, tailpos
, self
.maxstrlen
):
199 ascii85stream(file, self
.data
[i
: i
+self
.maxstrlen
])
201 if datalen
!= tailpos
:
203 ascii85stream(file, self
.data
[tailpos
:])
207 file.write("/%s exch def\n" % self
.id)
208 file.write("%%EndRessource\n")
211 class PDFimagepalettedata(pdfwriter
.PDFobject
):
213 def __init__(self
, name
, data
):
214 pdfwriter
.PDFobject
.__init
__(self
, "imagepalettedata", name
)
217 def write(self
, file, writer
, registry
):
219 "/Length %d\n" % len(self
.data
))
222 file.write(self
.data
)
227 class PDFimage(pdfwriter
.PDFobject
):
229 def __init__(self
, name
, width
, height
, palettemode
, palettedata
, mode
,
230 bitspercomponent
, compressmode
, data
, smask
, registry
, addresource
=True):
231 pdfwriter
.PDFobject
.__init
__(self
, "image", name
)
234 if palettedata
is not None:
240 registry
.addresource("XObject", name
, self
, procset
=procset
)
241 if palettedata
is not None:
242 # note that acrobat wants a palette to be an object (which clearly is a bug)
243 self
.PDFpalettedata
= PDFimagepalettedata(name
, palettedata
)
244 registry
.add(self
.PDFpalettedata
)
249 self
.palettemode
= palettemode
250 self
.palettedata
= palettedata
252 self
.bitspercomponent
= bitspercomponent
253 self
.compressmode
= compressmode
257 def write(self
, file, writer
, registry
):
261 "/Width %d\n" % self
.width
)
262 file.write("/Height %d\n" % self
.height
)
263 if self
.palettedata
is not None:
264 file.write("/ColorSpace [ /Indexed %s %i\n" % (devicenames
[self
.palettemode
], len(self
.palettedata
)/3-1))
265 file.write("%d 0 R\n" % registry
.getrefno(self
.PDFpalettedata
))
268 file.write("/ColorSpace %s\n" % devicenames
[self
.mode
])
270 file.write("/SMask %d 0 R\n" % registry
.getrefno(self
.smask
))
271 file.write("/BitsPerComponent %d\n" % self
.bitspercomponent
)
272 file.write("/Length %d\n" % len(self
.data
))
273 if self
.compressmode
:
274 file.write("/Filter /%sDecode\n" % self
.compressmode
)
277 file.write(self
.data
)
282 class bitmap_pt(canvasitem
.canvasitem
):
284 def __init__(self
, xpos_pt
, ypos_pt
, image
, width_pt
=None, height_pt
=None, ratio
=None,
285 PSstoreimage
=0, PSmaxstrlen
=4093, PSbinexpand
=1,
286 compressmode
="Flate", flatecompresslevel
=6,
287 dctquality
=75, dctoptimize
=0, dctprogression
=0):
288 self
.xpos_pt
= xpos_pt
289 self
.ypos_pt
= ypos_pt
292 self
.imagewidth
, self
.imageheight
= image
.size
294 if width_pt
is not None or height_pt
is not None:
295 self
.width_pt
= width_pt
296 self
.height_pt
= height_pt
297 if self
.width_pt
is None:
299 self
.width_pt
= self
.height_pt
* self
.imagewidth
/ float(self
.imageheight
)
301 self
.width_pt
= ratio
* self
.height_pt
302 elif self
.height_pt
is None:
304 self
.height_pt
= self
.width_pt
* self
.imageheight
/ float(self
.imagewidth
)
306 self
.height_pt
= (1.0/ratio
) * self
.width_pt
307 elif ratio
is not None:
308 raise ValueError("can't specify a ratio when setting width_pt and height_pt")
310 if ratio
is not None:
311 raise ValueError("must specify width_pt or height_pt to set a ratio")
312 widthdpi
, heightdpi
= image
.info
["dpi"] # fails when no dpi information available
313 self
.width_pt
= 72.0 * self
.imagewidth
/ float(widthdpi
)
314 self
.height_pt
= 72.0 * self
.imageheight
/ float(heightdpi
)
316 self
.PSstoreimage
= PSstoreimage
317 self
.PSmaxstrlen
= PSmaxstrlen
318 self
.PSbinexpand
= PSbinexpand
319 self
.compressmode
= compressmode
320 self
.flatecompresslevel
= flatecompresslevel
321 self
.dctquality
= dctquality
322 self
.dctoptimize
= dctoptimize
323 self
.dctprogression
= dctprogression
326 self
.imagecompressed
= image
.compressed
328 self
.imagecompressed
= None
329 if self
.compressmode
not in [None, "Flate", "DCT"]:
330 raise ValueError("invalid compressmode '%s'" % self
.compressmode
)
331 if self
.imagecompressed
not in [None, "Flate", "DCT"]:
332 raise ValueError("invalid compressed image '%s'" % self
.imagecompressed
)
333 if self
.compressmode
is not None and self
.imagecompressed
is not None:
334 raise ValueError("compression of a compressed image not supported")
335 if not haszlib
and self
.compressmode
== "Flate":
336 warnings
.warn("zlib module not available, disable compression")
337 self
.compressmode
= None
339 def imagedata(self
, interleavealpha
):
342 returns a tuple (mode, data, alpha, palettemode, palettedata)
343 where mode does not contain antialiasing anymore
346 alpha
= palettemode
= palettedata
= None
349 if mode
.startswith("A"):
356 data
= image(self
.imagewidth
, self
.imageheight
, mode
,
357 "".join(["".join(values
)
358 for values
in zip(*[band
.tostring()
359 for band
in bands
[1:]])]), palette
=data
.palette
)
360 if mode
.endswith("A"):
362 bands
= list(bands
[-1:]) + list(bands
[:-1])
366 # TODO: this is slow, but we don't want to depend on PIL or anything ... still, its incredibly slow to do it with lists and joins
367 data
= image(self
.imagewidth
, self
.imageheight
, "A%s" % mode
,
368 "".join(["".join(values
)
369 for values
in zip(*[band
.tostring()
370 for band
in bands
])]), palette
=data
.palette
)
373 data
= image(self
.imagewidth
, self
.imageheight
, mode
,
374 "".join(["".join(values
)
375 for values
in zip(*[band
.tostring()
376 for band
in bands
[1:]])]), palette
=data
.palette
)
379 palettemode
, palettedata
= data
.palette
.getdata()
380 if palettemode
not in ["L", "RGB", "CMYK"]:
381 warnings
.warn("image with unknown palette mode '%s' converted to rgb image" % palettemode
)
382 data
= data
.convert("RGB")
388 warnings
.warn("specific single channel image mode not natively supported, converted to regular grayscale")
389 data
= data
.convert("L")
391 elif mode
not in ["CMYK", "RGB"]:
392 warnings
.warn("image with unknown mode converted to rgb")
393 data
= data
.convert("RGB")
396 if self
.compressmode
== "Flate":
397 data
= zlib
.compress(data
.tostring(), self
.flatecompresslevel
)
398 elif self
.compressmode
== "DCT":
399 data
= data
.tostring("jpeg", mode
, self
.dctquality
, self
.dctoptimize
, self
.dctprogression
)
401 data
= data
.tostring()
402 if alpha
and not interleavealpha
:
403 if self
.compressmode
== "Flate":
404 alpha
= zlib
.compress(alpha
.tostring(), self
.flatecompresslevel
)
405 elif self
.compressmode
== "DCT":
406 # well, this here is strange, we might want a alphacompressmode ...
407 alpha
= alpha
.tostring("jpeg", mode
, self
.dctquality
, self
.dctoptimize
, self
.dctprogression
)
409 alpha
= alpha
.tostring()
411 return mode
, data
, alpha
, palettemode
, palettedata
414 return bbox
.bbox_pt(self
.xpos_pt
, self
.ypos_pt
,
415 self
.xpos_pt
+self
.width_pt
, self
.ypos_pt
+self
.height_pt
)
417 def processPS(self
, file, writer
, context
, registry
, bbox
):
418 mode
, data
, alpha
, palettemode
, palettedata
= self
.imagedata(True)
419 imagematrixPS
= (trafo
.mirror(0)
420 .translated_pt(-self
.xpos_pt
, self
.ypos_pt
+self
.height_pt
)
421 .scaled_pt(self
.imagewidth
/self
.width_pt
, self
.imageheight
/self
.height_pt
))
424 PSsinglestring
= self
.PSstoreimage
and len(data
) < self
.PSmaxstrlen
426 PSimagename
= "image-%d-%s-singlestring" % (id(self
.image
), self
.compressmode
)
428 PSimagename
= "image-%d-%s-stringarray" % (id(self
.image
), self
.compressmode
)
430 if self
.PSstoreimage
and not PSsinglestring
:
431 registry
.add(pswriter
.PSdefinition("imagedataaccess",
432 "{ /imagedataindex load " # get list index
433 "dup 1 add /imagedataindex exch store " # store increased index
434 "/imagedataid load exch get }")) # select string from array
435 if self
.PSstoreimage
:
436 registry
.add(PSimagedata(PSimagename
, data
, PSsinglestring
, self
.PSmaxstrlen
))
439 file.write("gsave\n")
440 if palettedata
is not None:
441 file.write("[ /Indexed %s %i\n" % (devicenames
[palettemode
], len(palettedata
)/3-1))
442 file.write("%%%%BeginData: %i ASCII Lines\n" % ascii85lines(len(palettedata
)))
444 ascii85stream(file, palettedata
)
447 file.write("] setcolorspace\n")
449 file.write("%s setcolorspace\n" % devicenames
[mode
])
451 if self
.PSstoreimage
and not PSsinglestring
:
452 file.write("/imagedataindex 0 store\n" # not use the stack since interpreters differ in their stack usage
453 "/imagedataid %s store\n" % PSimagename
)
457 file.write("/ImageType 3\n"
460 file.write("/ImageType 1\n"
461 "/Width %i\n" % self
.imagewidth
)
462 file.write("/Height %i\n" % self
.imageheight
)
463 file.write("/BitsPerComponent 8\n"
464 "/ImageMatrix %s\n" % imagematrixPS
)
465 file.write("/Decode %s\n" % decodestrings
[mode
])
467 file.write("/DataSource ")
468 if self
.PSstoreimage
:
470 file.write("/%s load" % PSimagename
)
472 file.write("/imagedataaccess load") # some printers do not allow for inline code here -> we store it in a resource
474 if self
.PSbinexpand
== 2:
475 file.write("currentfile /ASCIIHexDecode filter")
477 file.write("currentfile /ASCII85Decode filter")
478 if self
.compressmode
or self
.imagecompressed
:
479 file.write(" /%sDecode filter" % (self
.compressmode
or self
.imagecompressed
))
485 file.write("/MaskDict\n"
488 "/Width %i\n" % self
.imagewidth
)
489 file.write("/Height %i\n" % self
.imageheight
)
490 file.write("/BitsPerComponent 8\n"
491 "/ImageMatrix %s\n" % imagematrixPS
)
492 file.write("/Decode [1 0]\n"
494 "/InterleaveType 1\n"
497 if self
.PSstoreimage
:
498 file.write("image\n")
500 if self
.PSbinexpand
== 2:
501 file.write("%%%%BeginData: %i ASCII Lines\n"
502 "image\n" % (asciihexlines(len(data
)) + 1))
503 asciihexstream(file, data
)
505 # the datasource is currentstream (plus some filters)
506 file.write("%%%%BeginData: %i ASCII Lines\n"
507 "image\n" % (ascii85lines(len(data
)) + 1))
508 ascii85stream(file, data
)
510 file.write("%%EndData\n")
512 file.write("grestore\n")
514 def processPDF(self
, file, writer
, context
, registry
, bbox
):
515 mode
, data
, alpha
, palettemode
, palettedata
= self
.imagedata(False)
517 name
= "image-%d-%s" % (id(self
.image
), self
.compressmode
or self
.imagecompressed
)
519 alpha
= PDFimage("%s-smask" % name
, self
.imagewidth
, self
.imageheight
,
521 self
.compressmode
, alpha
, None, registry
, addresource
=False)
523 registry
.add(PDFimage(name
, self
.imagewidth
, self
.imageheight
,
524 palettemode
, palettedata
, mode
, 8,
525 self
.compressmode
or self
.imagecompressed
, data
, alpha
, registry
))
528 imagematrixPDF
= (trafo
.scale_pt(self
.width_pt
, self
.height_pt
)
529 .translated_pt(self
.xpos_pt
, self
.ypos_pt
))
532 imagematrixPDF
.processPDF(file, writer
, context
, registry
, bbox
)
533 file.write("/%s Do\n" % name
)
537 class bitmap(bitmap_pt
):
539 def __init__(self
, xpos
, ypos
, image
, width
=None, height
=None, **kwargs
):
540 xpos_pt
= unit
.topt(xpos
)
541 ypos_pt
= unit
.topt(ypos
)
542 if width
is not None:
543 width_pt
= unit
.topt(width
)
546 if height
is not None:
547 height_pt
= unit
.topt(height
)
551 bitmap_pt
.__init
__(self
, xpos_pt
, ypos_pt
, image
, width_pt
=width_pt
, height_pt
=height_pt
, **kwargs
)