-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMpacsWarp2D.py
More file actions
80 lines (62 loc) · 2.84 KB
/
MpacsWarp2D.py
File metadata and controls
80 lines (62 loc) · 2.84 KB
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
from TextureImage import TextureImage
from OpenGL.GL import *
from PIL import Image
import sys
class MpacsWarp2D:
""" The base class for performing warping in the "2d" profile
specified in the mpcdi file. This warps media according to a 2-d
pfm file and applies a blending map. """
def __init__(self, mpcdi, region):
self.mpcdi = mpcdi
self.region = region
# This class only supports the "2d" profile.
assert self.mpcdi.profile == '2d'
self.windowSize = self.region.Xresolution, self.region.Yresolution
self.mediaFilename = None
self.media = None
self.outputFilename = None
self.includeBlend = True
self.pfm = self.mpcdi.extractPfmFile(self.region.geometryWarpFile.path)
# If an alpha map is included, it is the primary blend map,
# and is used to darken the whites.
if self.region.alphaMap:
self.alpha = self.mpcdi.extractTextureImage(self.region.alphaMap.path)
# This is the gamma value of the embedded alpha map.
self.alphaGamma = self.region.alphaMap.gammaEmbedded
else:
self.alpha = TextureImage(flat = ('L', (self.pfm.xSize, self.pfm.ySize), 255))
self.alphaGamma = 1.0
# If a beta map is included, it is a "black level uplift" map,
# used to brighten the blacks.
if self.region.betaMap:
self.beta = self.mpcdi.extractTextureImage(self.region.betaMap.path)
# This is the gamma value of the embedded beta map.
self.betaGamma = self.region.betaMap.gammaEmbedded
else:
self.beta = TextureImage(flat = ('L', (self.pfm.xSize, self.pfm.ySize), 0))
self.betaGamma = 1.0
# This is the gamma value we will want to display.
self.targetGamma = self.alphaGamma
# This is the gamma value of the source media image.
self.mediaGamma = self.alphaGamma
def setWindowSize(self, windowSize):
self.windowSize = windowSize
def setMediaFilename(self, mediaFilename):
self.mediaFilename = mediaFilename
self.media = TextureImage(self.mediaFilename)
def setOutputFilename(self, outputFilename):
self.outputFilename = outputFilename
def saveOutputImage(self):
""" Saves a screenshot to the indicated filename for reference. """
if not self.outputFilename:
return
external_format = GL_RGBA
component_type = GL_UNSIGNED_BYTE
width, height = self.windowSize
buffer = glReadPixels(0, 0, width, height, external_format, component_type)
img = Image.frombytes(mode="RGBA", size=(width, height), data = buffer)
img = img.transpose(Image.FLIP_TOP_BOTTOM)
img.save(self.outputFilename)
print self.outputFilename
def initGL(self):
self.media.initGL()