Skip to content

Commit

Permalink
tmxrasterizer: Added --size argument and support local file URLs
Browse files Browse the repository at this point in the history
This allows the tmxrasterizer to be used as a thumbnailer.
  • Loading branch information
bjorn committed Sep 26, 2016
1 parent 943f297 commit 4b09154
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
6 changes: 5 additions & 1 deletion man/tmxrasterizer.1
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "TMXRASTERIZER" "1" "April 2014" "" ""
.TH "TMXRASTERIZER" "1" "September 2016" "" ""
.
.SH "NAME"
\fBtmxrasterizer\fR \- renders a tile map to an image
Expand Down Expand Up @@ -31,6 +31,10 @@ The scale of the output image
The requested size in pixels at which a tile is rendered\. Overrides the \-\-scale option\.
.
.TP
\fB\-\-size\fR SIZE
The output image fits within a SIZE x SIZE square\. Overrides the \-\-scale and \-\-tilesize options\.
.
.TP
\fB\-a\fR \fB\-\-anti\-aliasing\fR
Smooth the output image using anti\-aliasing
.
Expand Down
3 changes: 3 additions & 0 deletions man/tmxrasterizer.1.ronn
Expand Up @@ -22,6 +22,9 @@ This is very helpful for creating small-scale previews, such as mini-maps.
* `-t` `--tilesize` SIZE:
The requested size in pixels at which a tile is rendered.
Overrides the --scale option.
* `--size` SIZE:
The output image fits within a SIZE x SIZE square.
Overrides the --scale and --tilesize options.
* `-a` `--anti-aliasing`:
Smooth the output image using anti-aliasing
* `--ignore-visibility`:
Expand Down
28 changes: 25 additions & 3 deletions src/tmxrasterizer/main.cpp
Expand Up @@ -31,6 +31,7 @@
#include <QGuiApplication>
#include <QDebug>
#include <QStringList>
#include <QUrl>

namespace {

Expand All @@ -40,6 +41,7 @@ struct CommandLineOptions {
, showVersion(false)
, scale(1.0)
, tileSize(0)
, size(0)
, useAntiAliasing(false)
, ignoreVisibility(false)
{}
Expand All @@ -50,6 +52,7 @@ struct CommandLineOptions {
QString fileToSave;
qreal scale;
int tileSize;
int size;
bool useAntiAliasing;
bool ignoreVisibility;
QStringList layersToHide;
Expand All @@ -70,6 +73,8 @@ static void showHelp()
" -s --scale SCALE : The scale of the output image (default: 1)\n"
" -t --tilesize SIZE : The requested size in pixels at which a tile is rendered\n"
" Overrides the --scale option\n"
" --size SIZE : The output image fits within a SIZE x SIZE square\n"
" Overrides the --scale and --tilesize options\n"
" -a --anti-aliasing : Smooth the output image using anti-aliasing\n"
" --ignore-visibility : Ignore all layer visibility flags in the map file, and render all\n"
" layers in the output (default is to omit invisible layers)\n"
Expand Down Expand Up @@ -120,6 +125,18 @@ static void parseCommandLineArguments(CommandLineOptions &options)
options.showHelp = true;
}
}
} else if (arg == QLatin1String("--size")) {
i++;
if (i >= arguments.size()) {
options.showHelp = true;
} else {
bool sizeIsInt;
options.size = arguments.at(i).toInt(&sizeIsInt);
if (!sizeIsInt) {
qWarning() << arguments.at(i) << ": the specified size is not an integer.";
options.showHelp = true;
}
}
} else if (arg == QLatin1String("--hide-layer")) {
i++;
if (i >= arguments.size()) {
Expand All @@ -138,7 +155,11 @@ static void parseCommandLineArguments(CommandLineOptions &options)
qWarning() << "Unknown option" << arg;
options.showHelp = true;
} else if (options.fileToOpen.isEmpty()) {
options.fileToOpen = arg;
const QUrl url(arg);
if (url.isLocalFile())
options.fileToOpen = url.toLocalFile();
else
options.fileToOpen = arg;
} else if (options.fileToSave.isEmpty()) {
options.fileToSave = arg;
} else {
Expand Down Expand Up @@ -177,8 +198,9 @@ int main(int argc, char *argv[])
w.setIgnoreVisibility(options.ignoreVisibility);
w.setLayersToHide(options.layersToHide);


if (options.tileSize > 0) {
if (options.size > 0) {
w.setSize(options.size);
} else if (options.tileSize > 0) {
w.setTileSize(options.tileSize);
} else if (options.scale > 0.0) {
w.setScale(options.scale);
Expand Down
10 changes: 7 additions & 3 deletions src/tmxrasterizer/tmxrasterizer.cpp
Expand Up @@ -46,6 +46,7 @@ using namespace Tiled;
TmxRasterizer::TmxRasterizer():
mScale(1.0),
mTileSize(0),
mSize(0),
mUseAntiAliasing(true),
mIgnoreVisibility(false)
{
Expand Down Expand Up @@ -98,17 +99,20 @@ int TmxRasterizer::render(const QString &mapFileName,
break;
}

QSize mapSize = renderer->mapSize();
qreal xScale, yScale;

if (mTileSize > 0) {
if (mSize > 0) {
xScale = (qreal) mSize / mapSize.width();
yScale = (qreal) mSize / mapSize.height();
xScale = yScale = qMin(1.0, qMin(xScale, yScale));
} else if (mTileSize > 0) {
xScale = (qreal) mTileSize / map->tileWidth();
yScale = (qreal) mTileSize / map->tileHeight();
} else {
xScale = yScale = mScale;
}

QSize mapSize = renderer->mapSize();

QMargins margins = map->computeLayerOffsetMargins();
mapSize.setWidth(mapSize.width() + margins.left() + margins.right());
mapSize.setHeight(mapSize.height() + margins.top() + margins.bottom());
Expand Down
2 changes: 2 additions & 0 deletions src/tmxrasterizer/tmxrasterizer.h
Expand Up @@ -50,6 +50,7 @@ class TmxRasterizer

void setScale(qreal scale) { mScale = scale; }
void setTileSize(int tileSize) { mTileSize = tileSize; }
void setSize(int size) { mSize = size; }
void setAntiAliasing(bool useAntiAliasing) { mUseAntiAliasing = useAntiAliasing; }
void setIgnoreVisibility(bool IgnoreVisibility) { mIgnoreVisibility = IgnoreVisibility; }

Expand All @@ -60,6 +61,7 @@ class TmxRasterizer
private:
qreal mScale;
int mTileSize;
int mSize;
bool mUseAntiAliasing;
bool mIgnoreVisibility;
QStringList mLayersToHide;
Expand Down

0 comments on commit 4b09154

Please sign in to comment.