1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2004-2013 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 binascii
, logging
, struct
30 from . import bbox
, baseclasses
, pswriter
, pdfwriter
, trafo
, unit
32 logger
= logging
.getLogger("pyx")
34 devicenames
= {"L": "/DeviceGray",
36 "CMYK": "/DeviceCMYK"}
37 decodestrings
= {"L": "[0 1]",
38 "RGB": "[0 1 0 1 0 1]",
39 "CMYK": "[0 1 0 1 0 1 0 1]",
43 def ascii85lines(datalen
):
46 return (datalen
+ 56)/60
48 def ascii85stream(file, data
):
49 """Encodes the string data in ASCII85 and writes it to
50 the stream file. The number of lines written to the stream
51 is known just from the length of the data by means of the
52 ascii85lines function. Note that the tailing newline character
53 of the last line is not added by this function, but it is taken
54 into account in the ascii85lines function."""
55 i
= 3 # go on smoothly in case of data length equals zero
57 l
= [None, None, None, None]
58 for i
in range(len(data
)):
62 if i
%60 == 3 and i
!= 3:
66 # l[3], c5 = divmod(256*256*256*l[0]+256*256*l[1]+256*l[2]+l[3], 85)
67 # l[2], c4 = divmod(l[3], 85)
68 # we have to avoid number > 2**31 by
69 l
[3], c5
= divmod(256*256*l
[0]+256*256*l
[1]+256*l
[2]+l
[3], 85)
70 l
[2], c4
= divmod(256*256*3*l
[0]+l
[3], 85)
71 l
[1], c3
= divmod(l
[2], 85)
72 c1
, c2
= divmod(l
[1], 85)
73 file.write_bytes(struct
.pack("BBBBB", c1
+33, c2
+33, c3
+33, c4
+33, c5
+33))
77 for j
in range((i
%4) + 1, 4):
79 l
[3], c5
= divmod(256*256*l
[0]+256*256*l
[1]+256*l
[2]+l
[3], 85)
80 l
[2], c4
= divmod(256*256*3*l
[0]+l
[3], 85)
81 l
[1], c3
= divmod(l
[2], 85)
82 c1
, c2
= divmod(l
[1], 85)
83 file.write_bytes(struct
.pack("BBBB", c1
+33, c2
+33, c3
+33, c4
+33)[:(i
%4)+2])
85 _asciihexlinelength
= 64
86 def asciihexlines(datalen
):
87 return (datalen
*2 + _asciihexlinelength
- 1) / _asciihexlinelength
89 def asciihexstream(file, data
):
90 hexdata
= binascii
.b2a_hex(data
)
91 for i
in range((len(hexdata
)-1)/_asciihexlinelength
+ 1):
92 file.write(hexdata
[i
*_asciihexlinelength
: i
*_asciihexlinelength
+_asciihexlinelength
])
98 def __init__(self
, width
, height
, mode
, data
, compressed
=None, palette
=None):
99 if width
<= 0 or height
<= 0:
100 raise ValueError("valid image size")
101 if mode
not in ["L", "RGB", "CMYK", "LA", "RGBA", "CMYKA", "AL", "ARGB", "ACMYK"]:
102 raise ValueError("invalid mode")
103 if compressed
is None and len(mode
)*width
*height
!= len(data
):
104 raise ValueError("wrong size of uncompressed data")
105 self
.size
= width
, height
108 self
.compressed
= compressed
109 self
.palette
= palette
112 if self
.compressed
is not None:
113 raise RuntimeError("cannot extract bands from compressed image")
114 bands
= len(self
.mode
)
115 width
, height
= self
.size
116 return [image(width
, height
, "L", "".join([self
.data
[i
*bands
+band
]
117 for i
in range(width
*height
)]))
118 for band
in range(bands
)]
120 def tobytes(self
, *args
):
122 raise RuntimeError("encoding not supported in this implementation")
125 def convert(self
, model
):
126 raise RuntimeError("color model conversion not supported in this implementation")
129 class jpegimage(image
):
131 def __init__(self
, file):
135 with
open(file, "rb") as f
:
141 if data
[pos
] == 0o377 and data
[pos
+1] not in [0, 0o377]:
142 # print("marker: 0x%02x \\%03o" % (data[pos+1], data[pos+1]))
143 if data
[pos
+1] == 0o330:
147 elif not nestinglevel
:
148 raise ValueError("begin marker expected")
149 elif data
[pos
+1] == 0o331:
154 elif data
[pos
+1] in [0o300, 0o302]:
155 l
, bits
, height
, width
, components
= struct
.unpack(">HBHHB", data
[pos
+2:pos
+10])
157 raise ValueError("implementation limited to 8 bit per component only")
159 mode
= {1: "L", 3: "RGB", 4: "CMYK"}[components
]
161 raise ValueError("invalid number of components")
163 elif data
[pos
+1] == 0o340:
164 l
, id, major
, minor
, dpikind
, xdpi
, ydpi
= struct
.unpack(">H5sBBBHH", data
[pos
+2:pos
+16])
166 self
.info
= {"dpi": (xdpi
, ydpi
)}
168 self
.info
= {"dpi": (xdpi
*2.54, ydpi
*2.45)}
169 # else do not provide dpi information
173 raise ValueError("end marker expected")
174 image
.__init
__(self
, width
, height
, mode
, data
[begin
:end
], compressed
="DCT")
177 class PSimagedata(pswriter
.PSresource
):
179 def __init__(self
, name
, data
, singlestring
, maxstrlen
):
180 pswriter
.PSresource
.__init
__(self
, "imagedata", name
)
182 self
.singlestring
= singlestring
183 self
.maxstrlen
= maxstrlen
185 def output(self
, file, writer
, registry
):
186 file.write("%%%%BeginRessource: %s\n" % self
.id)
187 if self
.singlestring
:
188 file.write("%%%%BeginData: %i ASCII Lines\n"
189 "<~" % ascii85lines(len(self
.data
)))
190 ascii85stream(file, self
.data
)
194 datalen
= len(self
.data
)
195 tailpos
= datalen
- datalen
% self
.maxstrlen
196 file.write("%%%%BeginData: %i ASCII Lines\n" %
197 ((tailpos
/self
.maxstrlen
) * ascii85lines(self
.maxstrlen
) +
198 ascii85lines(datalen
-tailpos
)))
200 for i
in range(0, tailpos
, self
.maxstrlen
):
202 ascii85stream(file, self
.data
[i
: i
+self
.maxstrlen
])
204 if datalen
!= tailpos
:
206 ascii85stream(file, self
.data
[tailpos
:])
210 file.write("/%s exch def\n" % self
.id)
211 file.write("%%EndRessource\n")
214 class PDFimagepalettedata(pdfwriter
.PDFobject
):
216 def __init__(self
, name
, data
):
217 pdfwriter
.PDFobject
.__init
__(self
, "imagepalettedata", name
)
220 def write(self
, file, writer
, registry
):
222 "/Length %d\n" % len(self
.data
))
225 file.write(self
.data
)
230 class PDFimage(pdfwriter
.PDFobject
):
232 def __init__(self
, name
, width
, height
, palettemode
, palettedata
, mode
,
233 bitspercomponent
, compressmode
, data
, smask
, registry
, addresource
=True):
234 pdfwriter
.PDFobject
.__init
__(self
, "image", name
)
237 if palettedata
is not None:
243 registry
.addresource("XObject", name
, self
, procset
=procset
)
244 if palettedata
is not None:
245 # note that acrobat wants a palette to be an object (which clearly is a bug)
246 self
.PDFpalettedata
= PDFimagepalettedata(name
, palettedata
)
247 registry
.add(self
.PDFpalettedata
)
252 self
.palettemode
= palettemode
253 self
.palettedata
= palettedata
255 self
.bitspercomponent
= bitspercomponent
256 self
.compressmode
= compressmode
260 def write(self
, file, writer
, registry
):
264 "/Width %d\n" % self
.width
)
265 file.write("/Height %d\n" % self
.height
)
266 if self
.palettedata
is not None:
267 file.write("/ColorSpace [ /Indexed %s %i\n" % (devicenames
[self
.palettemode
], len(self
.palettedata
)/3-1))
268 file.write("%d 0 R\n" % registry
.getrefno(self
.PDFpalettedata
))
271 file.write("/ColorSpace %s\n" % devicenames
[self
.mode
])
273 file.write("/SMask %d 0 R\n" % registry
.getrefno(self
.smask
))
274 file.write("/BitsPerComponent %d\n" % self
.bitspercomponent
)
275 file.write("/Length %d\n" % len(self
.data
))
276 if self
.compressmode
:
277 file.write("/Filter /%sDecode\n" % self
.compressmode
)
280 file.write_bytes(self
.data
)
284 class bitmap_trafo(baseclasses
.canvasitem
):
286 def __init__(self
, trafo
, image
,
287 PSstoreimage
=0, PSmaxstrlen
=4093, PSbinexpand
=1,
288 compressmode
="Flate", flatecompresslevel
=6,
289 dctquality
=75, dctoptimize
=0, dctprogression
=0):
290 self
.pdftrafo
= trafo
292 self
.imagewidth
, self
.imageheight
= image
.size
294 self
.PSstoreimage
= PSstoreimage
295 self
.PSmaxstrlen
= PSmaxstrlen
296 self
.PSbinexpand
= PSbinexpand
297 self
.compressmode
= compressmode
298 self
.flatecompresslevel
= flatecompresslevel
299 self
.dctquality
= dctquality
300 self
.dctoptimize
= dctoptimize
301 self
.dctprogression
= dctprogression
304 self
.imagecompressed
= image
.compressed
306 self
.imagecompressed
= None
307 if self
.compressmode
not in [None, "Flate", "DCT"]:
308 raise ValueError("invalid compressmode '%s'" % self
.compressmode
)
309 if self
.imagecompressed
not in [None, "Flate", "DCT"]:
310 raise ValueError("invalid compressed image '%s'" % self
.imagecompressed
)
311 if self
.compressmode
is not None and self
.imagecompressed
is not None:
312 raise ValueError("compression of a compressed image not supported")
313 if not haszlib
and self
.compressmode
== "Flate":
314 logger
.warning("zlib module not available, disable compression")
315 self
.compressmode
= None
317 def imagedata(self
, interleavealpha
):
320 returns a tuple (mode, data, alpha, palettemode, palettedata)
321 where mode does not contain antialiasing anymore
324 alpha
= palettemode
= palettedata
= None
327 if mode
.startswith("A"):
334 data
= image(self
.imagewidth
, self
.imageheight
, mode
,
335 "".join(["".join(values
)
336 for values
in zip(*[band
.tobytes()
337 for band
in bands
[1:]])]), palette
=data
.palette
)
338 if mode
.endswith("A"):
340 bands
= list(bands
[-1:]) + list(bands
[:-1])
344 # 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
345 data
= image(self
.imagewidth
, self
.imageheight
, "A%s" % mode
,
346 "".join(["".join(values
)
347 for values
in zip(*[band
.tobytes()
348 for band
in bands
])]), palette
=data
.palette
)
351 data
= image(self
.imagewidth
, self
.imageheight
, mode
,
352 "".join(["".join(values
)
353 for values
in zip(*[band
.tobytes()
354 for band
in bands
[1:]])]), palette
=data
.palette
)
357 palettemode
, palettedata
= data
.palette
.getdata()
358 if palettemode
not in ["L", "RGB", "CMYK"]:
359 logger
.warning("image with unknown palette mode '%s' converted to rgb image" % palettemode
)
360 data
= data
.convert("RGB")
366 logger
.warning("specific single channel image mode not natively supported, converted to regular grayscale")
367 data
= data
.convert("L")
369 elif mode
not in ["CMYK", "RGB"]:
370 logger
.warning("image with unknown mode converted to rgb")
371 data
= data
.convert("RGB")
374 if self
.compressmode
== "Flate":
375 data
= zlib
.compress(data
.tobytes(), self
.flatecompresslevel
)
376 elif self
.compressmode
== "DCT":
377 data
= data
.tobytes("jpeg", mode
, self
.dctquality
, self
.dctoptimize
, self
.dctprogression
)
379 data
= data
.tobytes()
380 if alpha
and not interleavealpha
:
381 if self
.compressmode
== "Flate":
382 alpha
= zlib
.compress(alpha
.tobytes(), self
.flatecompresslevel
)
383 elif self
.compressmode
== "DCT":
384 # well, this here is strange, we might want a alphacompressmode ...
385 alpha
= alpha
.tobytes("jpeg", mode
, self
.dctquality
, self
.dctoptimize
, self
.dctprogression
)
387 alpha
= alpha
.tobytes()
389 return mode
, data
, alpha
, palettemode
, palettedata
393 bb
.includepoint_pt(*self
.pdftrafo
.apply_pt(0.0, 0.0))
394 bb
.includepoint_pt(*self
.pdftrafo
.apply_pt(0.0, 1.0))
395 bb
.includepoint_pt(*self
.pdftrafo
.apply_pt(1.0, 0.0))
396 bb
.includepoint_pt(*self
.pdftrafo
.apply_pt(1.0, 1.0))
399 def processPS(self
, file, writer
, context
, registry
, bbox
):
400 mode
, data
, alpha
, palettemode
, palettedata
= self
.imagedata(True)
401 pstrafo
= trafo
.translate_pt(0, -1.0).scaled(self
.imagewidth
, -self
.imageheight
)*self
.pdftrafo
.inverse()
403 PSsinglestring
= self
.PSstoreimage
and len(data
) < self
.PSmaxstrlen
405 PSimagename
= "image-%d-%s-singlestring" % (id(self
.image
), self
.compressmode
)
407 PSimagename
= "image-%d-%s-stringarray" % (id(self
.image
), self
.compressmode
)
409 if self
.PSstoreimage
and not PSsinglestring
:
410 registry
.add(pswriter
.PSdefinition("imagedataaccess",
411 b
"{ /imagedataindex load " # get list index
412 b
"dup 1 add /imagedataindex exch store " # store increased index
413 b
"/imagedataid load exch get }")) # select string from array
414 if self
.PSstoreimage
:
415 registry
.add(PSimagedata(PSimagename
, data
, PSsinglestring
, self
.PSmaxstrlen
))
418 file.write("gsave\n")
419 if palettedata
is not None:
420 file.write("[ /Indexed %s %i\n" % (devicenames
[palettemode
], len(palettedata
)/3-1))
421 file.write("%%%%BeginData: %i ASCII Lines\n" % ascii85lines(len(palettedata
)))
423 ascii85stream(file, palettedata
)
426 file.write("] setcolorspace\n")
428 file.write("%s setcolorspace\n" % devicenames
[mode
])
430 if self
.PSstoreimage
and not PSsinglestring
:
431 file.write("/imagedataindex 0 store\n" # not use the stack since interpreters differ in their stack usage
432 "/imagedataid %s store\n" % PSimagename
)
436 file.write("/ImageType 3\n"
439 file.write("/ImageType 1\n"
440 "/Width %i\n" % self
.imagewidth
)
441 file.write("/Height %i\n" % self
.imageheight
)
442 file.write("/BitsPerComponent 8\n"
443 "/ImageMatrix %s\n" % pstrafo
)
444 file.write("/Decode %s\n" % decodestrings
[mode
])
446 file.write("/DataSource ")
447 if self
.PSstoreimage
:
449 file.write("/%s load" % PSimagename
)
451 file.write("/imagedataaccess load") # some printers do not allow for inline code here -> we store it in a resource
453 if self
.PSbinexpand
== 2:
454 file.write("currentfile /ASCIIHexDecode filter")
456 file.write("currentfile /ASCII85Decode filter")
457 if self
.compressmode
or self
.imagecompressed
:
458 file.write(" /%sDecode filter" % (self
.compressmode
or self
.imagecompressed
))
464 file.write("/MaskDict\n"
467 "/Width %i\n" % self
.imagewidth
)
468 file.write("/Height %i\n" % self
.imageheight
)
469 file.write("/BitsPerComponent 8\n"
470 "/ImageMatrix %s\n" % pstrafo
)
471 file.write("/Decode [1 0]\n"
473 "/InterleaveType 1\n"
476 if self
.PSstoreimage
:
477 file.write("image\n")
479 if self
.PSbinexpand
== 2:
480 file.write("%%%%BeginData: %i ASCII Lines\n"
481 "image\n" % (asciihexlines(len(data
)) + 1))
482 asciihexstream(file, data
)
485 # the datasource is currentstream (plus some filters)
486 file.write("%%%%BeginData: %i ASCII Lines\n"
487 "image\n" % (ascii85lines(len(data
)) + 1))
488 ascii85stream(file, data
)
490 file.write("%%EndData\n")
492 file.write("grestore\n")
494 def processPDF(self
, file, writer
, context
, registry
, bbox
):
495 mode
, data
, alpha
, palettemode
, palettedata
= self
.imagedata(False)
497 name
= "image-%d-%s" % (id(self
.image
), self
.compressmode
or self
.imagecompressed
)
499 alpha
= PDFimage("%s-smask" % name
, self
.imagewidth
, self
.imageheight
,
501 self
.compressmode
, alpha
, None, registry
, addresource
=False)
503 registry
.add(PDFimage(name
, self
.imagewidth
, self
.imageheight
,
504 palettemode
, palettedata
, mode
, 8,
505 self
.compressmode
or self
.imagecompressed
, data
, alpha
, registry
))
510 self
.pdftrafo
.processPDF(file, writer
, context
, registry
)
511 file.write("/%s Do\n" % name
)
515 class bitmap_pt(bitmap_trafo
):
517 def __init__(self
, xpos_pt
, ypos_pt
, image
, width_pt
=None, height_pt
=None, ratio
=None, **kwargs
):
518 imagewidth
, imageheight
= image
.size
519 if width_pt
is not None or height_pt
is not None:
522 width_pt
= height_pt
* imagewidth
/ float(imageheight
)
524 width_pt
= ratio
* height_pt
525 elif height_pt
is None:
527 height_pt
= width_pt
* imageheight
/ float(imagewidth
)
529 height_pt
= (1.0/ratio
) * width_pt
530 elif ratio
is not None:
531 raise ValueError("can't specify a ratio when setting width_pt and height_pt")
533 if ratio
is not None:
534 raise ValueError("must specify width_pt or height_pt to set a ratio")
535 widthdpi
, heightdpi
= image
.info
["dpi"] # fails when no dpi information available
536 width_pt
= 72.0 * imagewidth
/ float(widthdpi
)
537 height_pt
= 72.0 * imageheight
/ float(heightdpi
)
539 bitmap_trafo
.__init
__(self
, trafo
.trafo_pt(((float(width_pt
), 0.0), (0.0, float(height_pt
))), (float(xpos_pt
), float(ypos_pt
))), image
, **kwargs
)
542 class bitmap(bitmap_pt
):
544 def __init__(self
, xpos
, ypos
, image
, width
=None, height
=None, **kwargs
):
545 xpos_pt
= unit
.topt(xpos
)
546 ypos_pt
= unit
.topt(ypos
)
547 if width
is not None:
548 width_pt
= unit
.topt(width
)
551 if height
is not None:
552 height_pt
= unit
.topt(height
)
556 bitmap_pt
.__init
__(self
, xpos_pt
, ypos_pt
, image
, width_pt
=width_pt
, height_pt
=height_pt
, **kwargs
)