-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmosaiccanvas.cpp
113 lines (96 loc) · 2.83 KB
/
mosaiccanvas.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
/**
* @file mosaiccanvas.h
*/
#include <iostream>
#include <sys/stat.h>
#include <errno.h>
#include <cstdlib>
#include "mosaiccanvas.h"
#include "util/util.h"
using namespace std;
using namespace util;
bool MosaicCanvas::enableOutput = false;
/**
* Constructor.
*
* @param theRows Number of rows to divide the canvas into
* @param theColumns Number of columns to divide the canvas into
*/
MosaicCanvas::MosaicCanvas(int theRows, int theColumns)
: rows(theRows), columns(theColumns)
{
if ((theRows < 1) || (theColumns < 1)) {
cerr << "Error: Cannot set non-positive rows or columns" << endl;
exit(-1);
}
myImages.resize(rows * columns);
}
/**
* Retrieve the number of rows of images
*
* @return The number or rows in the mosaic, or -1 on error
*/
int MosaicCanvas::getRows() const
{
return rows;
}
/**
* Retrieve the number of columns of images
*
* @return The number of columns in the mosaic, or -1 or error
*/
int MosaicCanvas::getColumns() const
{
return columns;
}
void MosaicCanvas::setTile(int row, int column, TileImage* i)
{
if (enableOutput) {
cerr << "\rPopulating Mosaic: setting tile ("
<< row << ", " << column
<< ")" << string(20, ' ') << "\r";
cerr.flush();
}
myImages[row * columns + column] = i;
}
const TileImage& MosaicCanvas::getTile(int row, int column)
{
return images(row, column);
}
PNG MosaicCanvas::drawMosaic(int pixelsPerTile)
{
if (pixelsPerTile <= 0) {
cerr << "ERROR: pixelsPerTile must be > 0" << endl;
exit(-1);
}
int width = columns * pixelsPerTile;
int height = rows * pixelsPerTile;
// Create the image
PNG mosaic(width, height);
// Create list of drawable tiles
for (int row = 0; row < rows; row++) {
if (enableOutput) {
cerr << "\rDrawing Mosaic: resizing tiles ("
<< (row * columns + /*col*/ 0 + 1) << "/" << (rows * columns)
<< ")" << string(20, ' ') << "\r";
cerr.flush();
}
for (int col = 0; col < columns; col++) {
int startX = divide(width * col, getColumns());
int endX = divide(width * (col + 1), getColumns());
int startY = divide(height * row, getRows());
int endY = divide(height * (row + 1), getRows());
if (endX - startX != endY - startY)
cerr << "Error: resolution not constant: x: " << (endX - startX)
<< " y: " << (endY - startY) << endl;
images(row, col).paste(mosaic, startX, startY, endX - startX);
}
}
if (enableOutput) {
cerr << "\r" << string(60, ' ');
cerr << "\rDrawing Mosaic: resizing tiles ("
<< (rows * columns) << "/" << (rows * columns) << ")" << endl;
cerr.flush();
}
return mosaic;
}