src/platform/python/mgba/image.py (view raw)
1# Copyright (c) 2013-2016 Jeffrey Pfau
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6from ._pylib import ffi, lib
7from . import png
8
9class Image:
10 def __init__(self, width, height, stride=0):
11 self.width = width
12 self.height = height
13 self.stride = stride
14 self.constitute()
15
16 def constitute(self):
17 if self.stride <= 0:
18 self.stride = self.width
19 self.buffer = ffi.new("color_t[{}]".format(self.stride * self.height))
20
21 def savePNG(self, f):
22 p = png.PNG(f)
23 success = p.writeHeader(self)
24 success = success and p.writePixels(self)
25 p.writeClose()
26 return success
27
28def u16ToU32(c):
29 r = c & 0x1F
30 g = (c >> 5) & 0x1F
31 b = (c >> 10) & 0x1F
32 a = (c >> 15) & 1
33 abgr = r << 3
34 abgr |= g << 11
35 abgr |= b << 19
36 abgr |= (a * 0xFF) << 24
37 return abgr
38
39def u32ToU16(c):
40 r = (c >> 3) & 0x1F
41 g = (c >> 11) & 0x1F
42 b = (c >> 19) & 0x1F
43 a = c >> 31
44 abgr = r
45 abgr |= g << 5
46 abgr |= b << 10
47 abgr |= a << 15
48 return abgr
49
50if ffi.sizeof("color_t") == 2:
51 def colorToU16(c):
52 return c
53
54 colorToU32 = u16ToU32
55
56 def u16ToColor(c):
57 return c
58
59 u32ToColor = u32ToU16
60else:
61 def colorToU32(c):
62 return c
63
64 colorToU16 = u32ToU16
65
66 def u32ToColor(c):
67 return c
68
69 u16ToColor = u16ToU32