File: make_atlas.py

package info (click to toggle)
freedroidrpg 0.13-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 156,568 kB
  • ctags: 6,872
  • sloc: ansic: 56,967; sh: 3,851; makefile: 255; python: 203
file content (131 lines) | stat: -rwxr-xr-x 4,043 bytes parent folder | download
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
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
#
# atlas.py
# Generates a texture atlas.
#
# Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os, sys, time, random
from glob import glob

try:
    from PIL import Image
except:
    print "You need the Python Imaging Library to use this script."
    sys.exit(1)

try: import psyco
except:
    print "You should really install the Psyco module, it will speed up"
    print "the computations ;)"

class Grid(object):
    def __init__(self):
        self.w = self.h = 1
        self.placed = []

    def place(self, w, h):
        while w > self.w: self.w *= 2
        while h > self.h: self.h *= 2

        surface = self.w * self.h
        surf_used = 0
        for ax,ay,aw,ah in self.placed:
            surf_used += aw * ah
        
        print "Ratio used / total : %f" % (surf_used / float(surface))

        if surface - surf_used < w * h:
            if self.w + w <= self.h: self.w *= 2
            else: self.h *= 2

        y, x = self.h, self.w
        while y >= 0:
            while x >= 0:
                for ax,ay,aw,ah in self.placed:
                    while x > 0 and ax < x < ax + aw: x -= 1
                    if x == 0:
                        while y > 0 and ay < y < ay + ah: y -= 1
                canbeplaced = True
                for i in xrange(w):
                    if not canbeplaced: break
                    for j in xrange(h):
                        for ax,ay,aw,ah in self.placed:
                            if ((ax <= x + i < ax + aw and ay <= y + j < ay + ah)
                             or x + i >= self.w or y + j >= self.h):
                                canbeplaced = False
                                break
                        if not canbeplaced: break
                if canbeplaced:
                    self.placed.append((x, y, w, h))
                    return (x, y)
                x -= 16
            x = self.w
            y -= 16

        if self.w + w <= self.h:
            x = self.w
            y = 0
            self.w *= 2
            self.placed.append((x, y, w, h))
            return (x, y)
        else:
            x = 0
            y = self.h
            self.h *= 2
            self.placed.append((x, y, w, h))
            return (x, y)      

def main(argv):
    if len(argv) < 3:
        print "USAGE: %s <files-pattern> <coords-file>" % argv[0]
        return 1
   
    random.seed()

    files = glob(argv[1])

    random.shuffle(files);

    images = [Image.open(f) for f in files]

    g = Grid()
    pos = []
    try:
        for e,i in enumerate(images):
            print "Placing %s" % files[e]
            t = time.time()
            pos.append((files[e], i, g.place(*i.size)))
            print "Placed, size of the grid is now %dx%d" % (g.w, g.h)
            #print "Time needed: %f s" % (time.time() - t)
	    print "Done: %d/%d (%f%%)" % (e+1, len(files), 100*float(e+1)/len(files))
    except KeyboardInterrupt:
        print "Interrupted, press enter to continue or C-C to exit"
        raw_input()

    print
    print "Generation completed ! Now, generating the coords file."
    fp = open(argv[2], 'w')
    fp.write("size %d %d\n" % (g.w, g.h))
    for f,i,p in pos:
        fp.write("%s %d %d\n" % (f, p[0], p[1]))
    print "File generated ! Exiting..."

    return 0

if __name__ == '__main__': sys.exit(main(sys.argv))