summaryrefslogtreecommitdiff
path: root/silx/gui/_glutils/Texture.py
blob: 9f09a86ef31696838672588f868392414f5ddc39 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This module provides a class wrapping OpenGL 2D and 3D texture."""

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "04/10/2016"


import collections
from ctypes import c_void_p
import logging

import numpy

from . import gl, utils


_logger = logging.getLogger(__name__)


class Texture(object):
    """Base class to wrap OpenGL 2D and 3D texture

    :param internalFormat: OpenGL texture internal format
    :param data: The data to copy to the texture or None for an empty texture
    :type data: numpy.ndarray or None
    :param format_: Input data format if different from internalFormat
    :param shape: If data is None, shape of the texture
    :type shape: 2 or 3-tuple of int (height, width) or (depth, height, width)
    :param int texUnit: The texture unit to use
    :param minFilter: OpenGL texture minimization filter (default: GL_NEAREST)
    :param magFilter: OpenGL texture magnification filter (default: GL_LINEAR)
    :param wrap: Texture wrap mode for dimensions: (t, s) or (r, t, s)
                 If a single value is provided, it used for all dimensions.
    :type wrap: OpenGL wrap mode or 2 or 3-tuple of wrap mode
    """

    def __init__(self, internalFormat, data=None, format_=None,
                 shape=None, texUnit=0,
                 minFilter=None, magFilter=None, wrap=None):

        self._internalFormat = internalFormat
        if format_ is None:
            format_ = self.internalFormat

        if data is None:
            assert shape is not None
        else:
            assert shape is None
            data = numpy.array(data, copy=False, order='C')
            if format_ != gl.GL_RED:
                shape = data.shape[:-1]  # Last dimension is channels
            else:
                shape = data.shape

        assert len(shape) in (2, 3)
        self._shape = tuple(shape)
        self._ndim = len(shape)

        self.texUnit = texUnit

        self._name = gl.glGenTextures(1)
        self.bind(self.texUnit)

        self._minFilter = None
        self.minFilter = minFilter if minFilter is not None else gl.GL_NEAREST

        self._magFilter = None
        self.magFilter = magFilter if magFilter is not None else gl.GL_LINEAR

        if wrap is not None:
            if not isinstance(wrap, collections.Iterable):
                wrap = [wrap] * self.ndim

            assert len(wrap) == self.ndim

            gl.glTexParameter(self.target,
                              gl.GL_TEXTURE_WRAP_S,
                              wrap[-1])
            gl.glTexParameter(self.target,
                              gl.GL_TEXTURE_WRAP_T,
                              wrap[-2])
            if self.ndim == 3:
                gl.glTexParameter(self.target,
                                  gl.GL_TEXTURE_WRAP_R,
                                  wrap[0])

        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

        # This are the defaults, useless to set if not modified
        # gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_ROWS, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_IMAGE_HEIGHT, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_IMAGES, 0)

        if data is None:
            data = c_void_p(0)
            type_ = gl.GL_UNSIGNED_BYTE
        else:
            type_ = utils.numpyToGLType(data.dtype)

        if self.ndim == 2:
            _logger.debug(
                'Creating 2D texture shape: (%d, %d),'
                ' internal format: %s, format: %s, type: %s',
                self.shape[0], self.shape[1],
                str(self.internalFormat), str(format_), str(type_))

            gl.glTexImage2D(
                gl.GL_TEXTURE_2D,
                0,
                self.internalFormat,
                self.shape[1],
                self.shape[0],
                0,
                format_,
                type_,
                data)
        else:
            _logger.debug(
                'Creating 3D texture shape: (%d, %d, %d),'
                ' internal format: %s, format: %s, type: %s',
                self.shape[0], self.shape[1], self.shape[2],
                str(self.internalFormat), str(format_), str(type_))

            gl.glTexImage3D(
                gl.GL_TEXTURE_3D,
                0,
                self.internalFormat,
                self.shape[2],
                self.shape[1],
                self.shape[0],
                0,
                format_,
                type_,
                data)

        gl.glBindTexture(self.target, 0)

    @property
    def target(self):
        """OpenGL target type of this texture"""
        return gl.GL_TEXTURE_2D if self.ndim == 2 else gl.GL_TEXTURE_3D

    @property
    def ndim(self):
        """The number of dimensions: 2 or 3"""
        return self._ndim

    @property
    def internalFormat(self):
        """Texture internal format"""
        return self._internalFormat

    @property
    def shape(self):
        """Shape of the texture: (height, width) or (depth, height, width)"""
        return self._shape

    @property
    def name(self):
        """OpenGL texture name"""
        if self._name is not None:
            return self._name
        else:
            raise RuntimeError(
                "No OpenGL texture resource, discard has already been called")

    @property
    def minFilter(self):
        """Minifying function parameter (GL_TEXTURE_MIN_FILTER)"""
        return self._minFilter

    @minFilter.setter
    def minFilter(self, minFilter):
        if minFilter != self.minFilter:
            self._minFilter = minFilter
            self.bind()
            gl.glTexParameter(self.target,
                              gl.GL_TEXTURE_MIN_FILTER,
                              self.minFilter)

    @property
    def magFilter(self):
        """Magnification function parameter (GL_TEXTURE_MAG_FILTER)"""
        return self._magFilter

    @magFilter.setter
    def magFilter(self, magFilter):
        if magFilter != self.magFilter:
            self._magFilter = magFilter
            self.bind()
            gl.glTexParameter(self.target,
                              gl.GL_TEXTURE_MAG_FILTER,
                              self.magFilter)

    def discard(self):
        """Delete associated OpenGL texture"""
        if self._name is not None:
            gl.glDeleteTextures(self._name)
            self._name = None
        else:
            _logger.warning("Discard as already been called")

    def bind(self, texUnit=None):
        """Bind the texture to a texture unit.

        :param int texUnit: The texture unit to use
        """
        if texUnit is None:
            texUnit = self.texUnit
        gl.glActiveTexture(gl.GL_TEXTURE0 + texUnit)
        gl.glBindTexture(self.target, self.name)

    # with statement

    def __enter__(self):
        self.bind()

    def __exit__(self, exc_type, exc_val, exc_tb):
        gl.glActiveTexture(gl.GL_TEXTURE0 + self.texUnit)
        gl.glBindTexture(self.target, 0)

    def update(self,
               format_,
               data,
               offset=(0, 0, 0),
               texUnit=None):
        """Update the content of the texture.

        Texture is not resized, so data must fit into texture with the
        given offset.

        :param format_: The OpenGL format of the data
        :param data: The data to use to update the texture
        :param offset: The offset in the texture where to copy the data
        :type offset: 2 or 3-tuple of int
        :param int texUnit:
            The texture unit to use (default: the one provided at init)
        """
        data = numpy.array(data, copy=False, order='C')

        assert data.ndim == self.ndim
        assert len(offset) >= self.ndim
        for i in range(self.ndim):
            assert offset[i] + data.shape[i] <= self.shape[i]

        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

        # This are the defaults, useless to set if not modified
        # gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_ROWS, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_IMAGE_HEIGHT, 0)
        # gl.glPixelStorei(gl.GL_UNPACK_SKIP_IMAGES, 0)

        self.bind(texUnit)

        type_ = utils.numpyToGLType(data.dtype)

        if self.ndim == 2:
            gl.glTexSubImage2D(gl.GL_TEXTURE_2D,
                               0,
                               offset[1],
                               offset[0],
                               data.shape[1],
                               data.shape[0],
                               format_,
                               type_,
                               data)
            gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
        else:
            gl.glTexSubImage3D(gl.GL_TEXTURE_3D,
                               0,
                               offset[2],
                               offset[1],
                               offset[0],
                               data.shape[2],
                               data.shape[1],
                               data.shape[0],
                               format_,
                               type_,
                               data)
            gl.glBindTexture(gl.GL_TEXTURE_3D, 0)