-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinPacker.cpp
120 lines (93 loc) · 2.95 KB
/
BinPacker.cpp
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
#include "BinPacker.h"
BinPacker::BinPacker (string imgDir, string binDir, unsigned int padding, string logFile)
: Directory (imgDir, "*.*")
{
m_imgDir = imgDir;
m_binDir = binDir;
m_logFile = logFile;
m_padding = padding;
m_binNum = 0;
mapFileDirs (); //Initialize list of file in each directory
}
BinPacker::~BinPacker()
{}
void BinPacker::mapFileDirs ()
{
list<string> dirs = Directory::getDirs(m_imgDir); //Grabs list of directories in image dir
list<string> files; //Holds list of files
list<string>::iterator it;
for (it = dirs.begin(); it != dirs.end(); it++) //Cycle through all directories
{
files = Directory::getFiles((*it), "*.png"); //Populate list of png's
files.merge (Directory::getFiles((*it), "*.jpg")); //Merge with list of jpg's
m_dirFileMap.insert (pair<string, Files>((*it), files)); //Insert all found images into directory map
}
return;
}
void BinPacker::updateBinPath(int binNum = -1)
{
if (binNum == -1)
binNum++;
else
binNum = binNum;
}
NodeIter BinPacker::addNode(DirectoryFileMapIter i, BIN_SIZE b)
{
DirectoryNodesIter node = m_nodes.find((*i).first);
boost::shared_ptr<ImagePacker> img (new ImagePacker (b, m_padding, m_logFile));
//Check if m_nodes already contains the parent directory
if (node == m_nodes.end()) //Directory Not Found
{
Node imgList; //Declare a new Node object
imgList.push_back (img); //Push the current bin onto the list
m_nodes.insert (make_pair((*i).first, imgList)); //Insert new list onto new node
node = m_nodes.find((*i).first);
}
else//Directory found
(*node).second.push_back (img); //Add bin onto list
return --(*node).second.end();
}
bool BinPacker::removeNode(DirectoryNodesIter i)
{
m_nodes.erase(i);
return true;
}
string BinPacker::getBinPath()
{
stringstream ss;
ss.str ("");
ss << m_binDir
<< "\\Bin"
<< m_binNum
<< ".png";
return ss.str();
}
Devil *BinPacker::createBin (unsigned int size)
{
string path = getBinPath();
m_binNum++;
return new Devil (path, size, size);
}
void BinPacker::packImages()
{
DirectoryFileMap::iterator di = m_dirFileMap.begin();
NodeIter ni;
boost::shared_ptr<Devil> img (new Devil());
ImagePacker * head = NULL;
ImagePacker * temp = NULL;
for (; di != m_dirFileMap.end(); di++)
{
DirFileMapFilesIter fi = (*di).second.begin();
//Push the parent node onto the map
ni = addNode(di, DEFAULT_BIN_SIZE);
for (; fi != (*di).second.end(); fi++)
{
img->Load ((*fi));
temp = (*ni).get();
head = temp;
temp = temp->InsertImg(temp, temp, DEFAULT_BIN_SIZE, img.get());
temp = head;
}
}
return;
}