-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpik.py
65 lines (45 loc) · 1.4 KB
/
pik.py
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
""" Copyright 2015-2017 sta256+mpskit at gmail.com
This file is part of mpskit.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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.
See LICENSE file for more details.
"""
from common import *
from madspack import read_madspack, write_madspack, save_madspack
from PIL import Image
from palette import read_palette_col, attach_palette
"""
PIK (Colonization)
MADSPACK
Section 0: Header
Section 1: Image (8bit indexed)
Section 2: Palette
"""
def read_pik(pik_name):
check_ext(pik_name, '.PIK')
parts = read_madspack(pik_name)
assert len(parts) >= 3
save_madspack(pik_name, parts)
h = read_pik_header(parts[0])
pal = read_palette_col(parts[2])
img = read_pik_image(parts[1], h, pal)
save_image(pik_name, img)
def read_pik_header(f):
h = Header()
h.height = read_uint16(f)
h.width = read_uint16(f)
h.unk1 = read_uint16(f)
h.unk2 = read_uint16(f)
return h
def read_pik_image(part, h, pal):
img = Image.new('P', (h.width, h.height))
attach_palette(img, pal)
pix = img.load()
for j in range(h.height):
for i in range(h.width):
pix[i,j] = read_uint8(part)
return img