def add_image(self, img, cache_key): ref = self.get_image(cache_key) if ref is not None: return ref fmt = img.format() image = QImage(img) if (image.depth() == 1 and img.colorTable().size() == 2 and img.colorTable().at(0) == QColor(Qt.black).rgba() and img.colorTable().at(1) == QColor(Qt.white).rgba()): if fmt == QImage.Format_MonoLSB: image = image.convertToFormat(QImage.Format_Mono) fmt = QImage.Format_Mono else: if (fmt != QImage.Format_RGB32 and fmt != QImage.Format_ARGB32): image = image.convertToFormat(QImage.Format_ARGB32) fmt = QImage.Format_ARGB32 w = image.width() h = image.height() d = image.depth() if fmt == QImage.Format_Mono: bytes_per_line = (w + 7) >> 3 data = image.constBits().asstring(bytes_per_line * h) return self.write_image(data, w, h, d, cache_key=cache_key) has_alpha = False soft_mask = None if fmt == QImage.Format_ARGB32: tmask = image.constBits().asstring(4*w*h)[self.alpha_bit::4] sdata = bytearray(tmask) vals = set(sdata) vals.discard(255) # discard opaque pixels has_alpha = bool(vals) if has_alpha: # Blend image onto a white background as otherwise Qt will render # transparent pixels as black background = QImage(image.size(), QImage.Format_ARGB32_Premultiplied) background.fill(Qt.white) painter = QPainter(background) painter.drawImage(0, 0, image) painter.end() image = background ba = QByteArray() buf = QBuffer(ba) image.save(buf, 'jpeg', 94) data = bytes(ba.data()) if has_alpha: soft_mask = self.write_image(tmask, w, h, 8) return self.write_image(data, w, h, 32, dct=True, soft_mask=soft_mask, cache_key=cache_key)
def __init__(self, stream, page_size, compress=False, mark_links=False, debug=print): self.stream = HashingStream(stream) self.compress = compress self.write_line(PDFVER) self.write_line(b'%íì¦"') creator = ('%s %s [http://calibre-ebook.com]'%(__appname__, __version__)) self.write_line('%% Created by %s'%creator) self.objects = IndirectObjects() self.objects.add(PageTree(page_size)) self.objects.add(Catalog(self.page_tree)) self.current_page = Page(self.page_tree, compress=self.compress) self.info = Dictionary({ 'Creator':String(creator), 'Producer':String(creator), 'CreationDate': utcnow(), }) self.stroke_opacities, self.fill_opacities = {}, {} self.font_manager = FontManager(self.objects, self.compress) self.image_cache = {} self.pattern_cache, self.shader_cache = {}, {} self.debug = debug self.links = Links(self, mark_links, page_size) i = QImage(1, 1, QImage.Format_ARGB32) i.fill(qRgba(0, 0, 0, 255)) self.alpha_bit = i.constBits().asstring(4).find(b'\xff')
def get_pixel_map(): ' Get the order of pixels in QImage (RGBA or BGRA usually) ' global _qimage_pixel_map if _qimage_pixel_map is None: i = QImage(1, 1, QImage.Format_ARGB32) i.fill(QColor(0, 1, 2, 3)) raw = bytearray(i.constBits().asstring(4)) _qimage_pixel_map = {c:raw.index(x) for c, x in zip('RGBA', b'\x00\x01\x02\x03')} _qimage_pixel_map = ''.join(sorted(_qimage_pixel_map, key=_qimage_pixel_map.get)) return _qimage_pixel_map
def __init__( self, file_object, page_width, page_height, left_margin, top_margin, right_margin, bottom_margin, width, height, errors=print, debug=print, compress=True, ): QPaintEngine.__init__(self, self.features) self.file_object = file_object self.compress = compress self.page_height, self.page_width = page_height, page_width self.left_margin, self.top_margin = left_margin, top_margin self.right_margin, self.bottom_margin = right_margin, bottom_margin self.pixel_width, self.pixel_height = width, height # Setup a co-ordinate transform that allows us to use co-ords # from Qt's pixel based co-ordinate system with its origin at the top # left corner. PDF's co-ordinate system is based on pts and has its # origin in the bottom left corner. We also have to implement the page # margins. Therefore, we need to translate, scale and reflect about the # x-axis. dy = self.page_height - self.top_margin dx = self.left_margin sx = (self.page_width - self.left_margin - self.right_margin) / self.pixel_width sy = (self.page_height - self.top_margin - self.bottom_margin) / self.pixel_height self.pdf_system = QTransform(sx, 0, 0, -sy, dx, dy) self.do_stroke = True self.do_fill = False self.scale = sqrt(sy ** 2 + sx ** 2) self.xscale, self.yscale = sx, sy self.graphics_state = GraphicsState() self.errors_occurred = False self.errors, self.debug = errors, debug self.text_option = QTextOption() self.text_option.setWrapMode(QTextOption.NoWrap) self.fonts = {} i = QImage(1, 1, QImage.Format_ARGB32) i.fill(qRgba(0, 0, 0, 255)) self.alpha_bit = i.constBits().asstring(4).find(b"\xff") self.current_page_num = 1 self.current_page_inited = False
def qimage_to_magick(img): ans = Image() fmt = get_pixel_map() if not img.hasAlphaChannel(): if img.format() != img.Format_RGB32: img = QImage(img) img.setFormat(QImage.Format_RGB32) fmt = fmt.replace('A', 'P') else: if img.format() != img.Format_ARGB32: img = QImage(img) img.setFormat(img.Format_ARGB32) raw = img.constBits().ascapsule() ans.constitute(img.width(), img.height(), fmt, raw) return ans
def qimage_to_magick(img): ans = Image() if isosx: # For some reson, on OSX MagickConstituteImage fails, and I can't be # bothered figuring out why. Dumping to uncompressed PNG is reasonably # fast. raw = pixmap_to_data(img, 'PNG', quality=100) ans.load(raw) return ans fmt = get_pixel_map() if not img.hasAlphaChannel(): if img.format() != img.Format_RGB32: img = QImage(img) img.setFormat(QImage.Format_RGB32) fmt = fmt.replace('A', 'P') else: if img.format() != img.Format_ARGB32: img = QImage(img) img.setFormat(img.Format_ARGB32) raw = img.constBits().ascapsule() ans.constitute(img.width(), img.height(), fmt, raw) return ans
def add_image(self, img, cache_key): ref = self.get_image(cache_key) if ref is not None: return ref fmt = img.format() image = QImage(img) if (image.depth() == 1 and img.colorTable().size() == 2 and img.colorTable().at(0) == QColor(Qt.black).rgba() and img.colorTable().at(1) == QColor(Qt.white).rgba()): if fmt == QImage.Format_MonoLSB: image = image.convertToFormat(QImage.Format_Mono) fmt = QImage.Format_Mono else: if (fmt != QImage.Format_RGB32 and fmt != QImage.Format_ARGB32): image = image.convertToFormat(QImage.Format_ARGB32) fmt = QImage.Format_ARGB32 w = image.width() h = image.height() d = image.depth() if fmt == QImage.Format_Mono: bytes_per_line = (w + 7) >> 3 data = image.constBits().asstring(bytes_per_line * h) return self.write_image(data, w, h, d, cache_key=cache_key) ba = QByteArray() buf = QBuffer(ba) image.save(buf, 'jpeg', 94) data = bytes(ba.data()) has_alpha = has_mask = False soft_mask = mask = None if fmt == QImage.Format_ARGB32: tmask = image.constBits().asstring(4*w*h)[self.alpha_bit::4] sdata = bytearray(tmask) vals = set(sdata) vals.discard(255) has_mask = bool(vals) vals.discard(0) has_alpha = bool(vals) if has_alpha: soft_mask = self.write_image(tmask, w, h, 8) elif has_mask: # dither the soft mask to 1bit and add it. This also helps PDF # viewers without transparency support bytes_per_line = (w + 7) >> 3 mdata = bytearray(0 for i in xrange(bytes_per_line * h)) spos = mpos = 0 for y in xrange(h): for x in xrange(w): if sdata[spos]: mdata[mpos + x>>3] |= (0x80 >> (x&7)) spos += 1 mpos += bytes_per_line mdata = bytes(mdata) mask = self.write_image(mdata, w, h, 1) return self.write_image(data, w, h, 32, mask=mask, dct=True, soft_mask=soft_mask, cache_key=cache_key)
def add_image(self, img, cache_key): ref = self.get_image(cache_key) if ref is not None: return ref fmt = img.format() image = QImage(img) if (image.depth() == 1 and img.colorTable().size() == 2 and img.colorTable().at(0) == QColor(Qt.black).rgba() and img.colorTable().at(1) == QColor(Qt.white).rgba()): if fmt == QImage.Format_MonoLSB: image = image.convertToFormat(QImage.Format_Mono) fmt = QImage.Format_Mono else: if (fmt != QImage.Format_RGB32 and fmt != QImage.Format_ARGB32): image = image.convertToFormat(QImage.Format_ARGB32) fmt = QImage.Format_ARGB32 w = image.width() h = image.height() d = image.depth() if fmt == QImage.Format_Mono: bytes_per_line = (w + 7) >> 3 data = image.constBits().asstring(bytes_per_line * h) return self.write_image(data, w, h, d, cache_key=cache_key) ba = QByteArray() buf = QBuffer(ba) image.save(buf, 'jpeg', 94) data = bytes(ba.data()) has_alpha = has_mask = False soft_mask = mask = None if fmt == QImage.Format_ARGB32: tmask = image.constBits().asstring(4 * w * h)[self.alpha_bit::4] sdata = bytearray(tmask) vals = set(sdata) vals.discard(255) has_mask = bool(vals) vals.discard(0) has_alpha = bool(vals) if has_alpha: soft_mask = self.write_image(tmask, w, h, 8) elif has_mask: # dither the soft mask to 1bit and add it. This also helps PDF # viewers without transparency support bytes_per_line = (w + 7) >> 3 mdata = bytearray(0 for i in xrange(bytes_per_line * h)) spos = mpos = 0 for y in xrange(h): for x in xrange(w): if sdata[spos]: mdata[mpos + x >> 3] |= (0x80 >> (x & 7)) spos += 1 mpos += bytes_per_line mdata = bytes(mdata) mask = self.write_image(mdata, w, h, 1) return self.write_image(data, w, h, 32, mask=mask, dct=True, soft_mask=soft_mask, cache_key=cache_key)