src/platform/python/mgba/png.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 vfs
8
9MODE_RGB = 0
10MODE_RGBA = 1
11MODE_INDEX = 2
12
13class PNG:
14 def __init__(self, f, mode=MODE_RGB):
15 self.vf = vfs.open(f)
16 self.mode = mode
17
18 def writeHeader(self, image):
19 self._png = lib.PNGWriteOpen(self.vf.handle)
20 if self.mode == MODE_RGB:
21 self._info = lib.PNGWriteHeader(self._png, image.width, image.height)
22 if self.mode == MODE_RGBA:
23 self._info = lib.PNGWriteHeaderA(self._png, image.width, image.height)
24 if self.mode == MODE_INDEX:
25 self._info = lib.PNGWriteHeader8(self._png, image.width, image.height)
26 return self._info != ffi.NULL
27
28 def writePixels(self, image):
29 if self.mode == MODE_RGB:
30 return lib.PNGWritePixels(self._png, image.width, image.height, image.stride, image.buffer)
31 if self.mode == MODE_RGBA:
32 return lib.PNGWritePixelsA(self._png, image.width, image.height, image.stride, image.buffer)
33 if self.mode == MODE_INDEX:
34 return lib.PNGWritePixels8(self._png, image.width, image.height, image.stride, image.buffer)
35
36 def writeClose(self):
37 lib.PNGWriteClose(self._png, self._info)
38 del self._png
39 del self._info