File: image.py

package info (click to toggle)
python-qpageview 0.6.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 780 kB
  • sloc: python: 5,215; makefile: 22
file content (224 lines) | stat: -rw-r--r-- 7,688 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
#
# This file is part of the qpageview package.
#
# Copyright (c) 2019 - 2019 by Wilbert Berendsen
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
A page that can display an image, loaded using QImage.

ImagePages are instantiated quite fast. The image is only really loaded on first
display.

"""

from PyQt5.QtCore import QPoint, QRect, QSize, Qt
from PyQt5.QtGui import QImage, QImageIOHandler, QImageReader, QPainter, QTransform

from . import document
from . import locking
from . import page
from . import render


class ImageContainer:
    """Represent an image, is shared among copies of the "same" Page."""
    def __init__(self, image):
        """Init with a QImage."""
        self._image = image

    def size(self):
        return self._image.size()

    def image(self, clip=None):
        if clip is None:
            return self._image
        return self._image.copy(clip)


class ImageLoader(ImageContainer):
    """Represent an image loaded from a file or IO device."""
    def __init__(self, source, autoTransform=True):
        """Init with a filename or QIODevice.

        If autoTransform is True (the default), EXIF rotation is automatically
        applied when loading the image.

        """
        self._size = None
        self.source = source
        self.autoTransform = autoTransform

    def _reader(self):
        """Return a QImageReader for the source."""
        reader = QImageReader(self.source)
        reader.setAutoTransform(self.autoTransform)
        return reader

    def size(self):
        """Return the size of the image.

        If the image can't be loaded, a null size is returned. The resulting
        value is cached.

        """
        if self._size is None:
            self._size = QSize()
            reader = self._reader()
            if reader.canRead():
                size = reader.size()
                if size:
                    if self.autoTransform and reader.transformation() & 4:
                        size.transpose()
                    self._size = size
        return QSize(self._size)

    def image(self, clip=None):
        """Load and return the image.

        If clip is given, it should be a QRect describing the area to load.

        """
        with locking.lock(self):
            reader = self._reader()
            if clip:
                if self.autoTransform:
                    size = reader.size()
                    transf = reader.transformation()
                    m = QTransform()
                    m.translate(size.width() / 2, size.height() / 2)
                    if transf & QImageIOHandler.TransformationMirror:
                        # horizontal mirror
                        m.scale(-1, 1)
                    if transf & QImageIOHandler.TransformationFlip:
                        # vertical mirror
                        m.scale(1, -1)
                    if transf & QImageIOHandler.TransformationRotate90:
                        # rotate 90
                        m.rotate(-90)
                        m.translate(size.height() / -2, size.width() / -2)
                    else:
                        m.translate(size.width() / -2, size.height() / -2)
                    clip = m.mapRect(clip)
                reader.setClipRect(clip)
            return reader.read()


class ImagePage(page.AbstractRenderedPage):
    """A Page that displays an image in any file format supported by Qt."""
    autoTransform = True    # whether to automatically apply exif transformations
    dpi = 96   # TODO: maybe this can be image dependent.

    def __init__(self, container, renderer=None):
        super().__init__(renderer)
        self.setPageSize(container.size())
        self._ic = container

    @classmethod
    def load(cls, filename, renderer=None):
        """Load the image and yield one ImagePage instance if loading was successful."""
        loader = ImageLoader(filename, cls.autoTransform)
        if loader.size():
            yield cls(loader, renderer)

    @classmethod
    def fromImage(cls, image, renderer=None):
        """Instantiate one ImagePage from the supplied QImage.

        As the image is kept in memory, it is not advised to instantiate many
        Page instances this way. Use load() for images on the filesystem.
        The image must be valid, and have a size > 0.

        """
        return cls(ImageContainer(image), renderer)

    def print(self, painter, rect=None, paperColor=None):
        """Paint a page for printing."""
        if rect is None:
            image = self._ic.image()
        else:
            rect = rect.normalized() & self.pageRect()
            # we copy the image, because QSvgGenerator otherwise includes the
            # full image in the resulting SVG file!
            image = self._ic.image(rect.toRect())
        painter.drawImage(QPoint(0, 0), image)

    def image(self, rect=None, dpiX=None, dpiY=None, paperColor=None):
        """Return a QImage of the specified rectangle."""
        if rect is None:
            rect = self.rect()
        else:
            rect = rect & self.rect()
        if dpiX is None:
            dpiX = self.dpi
        if dpiY is None:
            dpiY = dpiX

        s = self.defaultSize()
        m = QTransform()
        m.scale(s.width() * dpiX / self.dpi, s.height() * dpiY / self.dpi)
        m.translate(.5, .5)
        m.rotate(self.computedRotation * 90)
        m.translate(-.5, -.5)
        m.scale(1 / self.pageWidth, 1 / self.pageHeight)

        source = self.transform().inverted()[0].mapRect(rect)
        return self._ic.image(source).transformed(m, Qt.SmoothTransformation)

    def group(self):
        return self._ic

    def mutex(self):
        return self._ic


class ImageDocument(document.MultiSourceDocument):
    """A Document representing a group of images.

    A source may be a filename, a QIODevice or a QImage.

    """
    pageClass = ImagePage

    def createPages(self):
        for s in self.sources():
            if isinstance(s, QImage):
                if not s.isNull():
                    yield self.pageClass.fromImage(s, self.renderer)
            else:
                for p in self.pageClass.load(s, self.renderer):
                    yield p


class ImageRenderer(render.AbstractRenderer):
    def draw(self, page, painter, key, tile, paperColor=None):
        """Draw the specified tile of the page (coordinates in key) on painter."""
        # determine the part to draw; convert tile to viewbox
        source = self.map(key, page.pageRect()).mapRect(QRect(*tile))
        target = QRect(0, 0, tile.w, tile.h)
        if key.rotation & 1:
            target.setSize(target.size().transposed())
        image = page._ic.image(source).scaled(
            target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        painter.drawImage(target, image)



# install a default renderer, so SvgPage can be used directly
ImagePage.renderer = ImageRenderer()