Skip to content

Commit

Permalink
Make multi-element safety margin optional via switch
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Dec 9, 2021
1 parent 5b2fca9 commit 302ecdc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ Options:
- **-f | --format:** Pixel format to use when encoding to TEX. The valid options are <dxt1 | dxt3 | dxt5 | rgb | rgba>. Defaults to DXT5
- **-u | --unoptimized:** Do not generate smoothed mipmaps
- **-s | --straight:** Keep straight alpha channel, do not pre-multiply
- **-m | --margin:** Add a 1-px transparent margin to each input image (when more than one). Useful for rare cases of element bleed-over

Requires:
**-i** and **-o**

Notes:
Use `stex -f` to see the supported image formats.
Use `stex -f` to see the supported image formats. The **margin** switch is generally never required and is only available for extremely specific and unlikely cases in which floating point inaccuracies or rounding cause 1 row/column of pixels from one element to be marked as part of another during atlas key generation.

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -230,4 +231,4 @@ When creating this tool I couldn't find any documentation on the Klei TEX format
----------
0x00 - uint8[Data_Size]: Image Data

This tools defaults both flags in the newer header spec (the only one used when writing) to high. If anyone knows the purpose of these flags I'd be grateful if you could share it with me.
This tools defaults both flags in the newer header spec (the only one used when writing) to high. If anyone knows the purpose of these flags I'd be grateful if you could share it with me.
2 changes: 1 addition & 1 deletion src/command/c-pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ ErrorCode CPack::process(const QStringList& commandLine)

// Create atlas
mCore.printMessage(NAME, MSG_CREATE_ATLAS);
KAtlaser atlaser(namedImages);
KAtlaser atlaser(namedImages, mParser.isSet(CL_OPTION_MARGIN));
KAtlas atlas = atlaser.process();

// Create atlas key
Expand Down
7 changes: 6 additions & 1 deletion src/command/c-pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class CPack : public Command
PIXEL_FORMAT_MAP.keys().join(" | ") + ">. " +
"Defaults to DXT5.";

static inline const QString CL_OPT_MARGIN_S_NAME = "m";
static inline const QString CL_OPT_MARGIN_L_NAME = "margin";
static inline const QString CL_OPT_MARGIN_DESC = "Add a 1-px transparent margin to each input image (when more than one). Useful for rare cases of element bleed-over.";

static inline const QString CL_OPT_INPUT_S_NAME = "i";
static inline const QString CL_OPT_INPUT_L_NAME = "input";
static inline const QString CL_OPT_INPUT_DESC = "Directory containing images to pack.";
Expand All @@ -82,10 +86,11 @@ class CPack : public Command
static inline const QCommandLineOption CL_OPTION_STRAIGHT{{CL_OPT_STRAIGHT_S_NAME, CL_OPT_STRAIGHT_L_NAME}, CL_OPT_STRAIGHT_DESC}; // Boolean option
static inline const QCommandLineOption CL_OPTION_UNOPT{{CL_OPT_UNOPT_S_NAME, CL_OPT_UNOPT_L_NAME}, CL_OPT_UNOPT_DESC}; // Boolean option
static inline const QCommandLineOption CL_OPTION_FORMAT{{CL_OPT_FORMAT_S_NAME, CL_OPT_FORMAT_L_NAME}, CL_OPT_FORMAT_DESC, "format"}; // Takes value
static inline const QCommandLineOption CL_OPTION_MARGIN{{CL_OPT_MARGIN_S_NAME, CL_OPT_MARGIN_L_NAME}, CL_OPT_MARGIN_DESC}; // Boolean option
static inline const QCommandLineOption CL_OPTION_INPUT{{CL_OPT_INPUT_S_NAME, CL_OPT_INPUT_L_NAME}, CL_OPT_INPUT_DESC, "input"}; // Takes value
static inline const QCommandLineOption CL_OPTION_OUTPUT{{CL_OPT_OUTPUT_S_NAME, CL_OPT_OUTPUT_L_NAME}, CL_OPT_OUTPUT_DESC, "output"}; // Takes value
static inline const QList<const QCommandLineOption*> CL_OPTIONS_SPECIFIC{&CL_OPTION_INPUT, &CL_OPTION_OUTPUT, &CL_OPTION_STRAIGHT,
&CL_OPTION_UNOPT, &CL_OPTION_FORMAT};
&CL_OPTION_UNOPT, &CL_OPTION_FORMAT, &CL_OPTION_MARGIN};
static inline const QSet<const QCommandLineOption*> CL_OPTIONS_REQUIRED{&CL_OPTION_INPUT, &CL_OPTION_OUTPUT};

public:
Expand Down
17 changes: 14 additions & 3 deletions src/klei/k-atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
//===============================================================================================================

//-Constructor-------------------------------------------------------------------------------------------------
KAtlaser::KAtlaser(const QMap<QString, QImage>& namedImages) : mNamedImages(namedImages) {}
KAtlaser::KAtlaser(const QMap<QString, QImage>& namedImages, bool useMargin) :
mNamedImages(namedImages),
mUseMargin(useMargin)
{}

//-Instance Functions--------------------------------------------------------------------------------------------
//Private:
Expand Down Expand Up @@ -247,9 +250,17 @@ KAtlas KAtlaser::processMultiImage() const
for (i = mNamedImages.constBegin(); i != mNamedImages.constEnd(); i++)
{
QSize boundingBox;
boundingBox.setWidth(i->width() + 1); // Slight safety margin
boundingBox.setHeight(i->height() + 1); // Slight safety margin
boundingBox.setWidth(i->width());
boundingBox.setHeight(i->height());

// Apply safety margin if requested
if(mUseMargin)
{
boundingBox.rwidth()++;
boundingBox.rheight()++;
}

// Add element box
elementBoundingBoxes[i.key()] = boundingBox;

totalArea += boundingBox.height() * boundingBox.width();
Expand Down
3 changes: 2 additions & 1 deletion src/klei/k-atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class KAtlaser
//-Instance Members-------------------------------------------------------------------------------------------------
private:
const QMap<QString, QImage>& mNamedImages;
bool mUseMargin;

//-Constructor-------------------------------------------------------------------------------------------------------
public:
KAtlaser(const QMap<QString, QImage>& namedImages);
KAtlaser(const QMap<QString, QImage>& namedImages, bool useMargin);

//-Instance Functions----------------------------------------------------------------------------------------------
private:
Expand Down

0 comments on commit 302ecdc

Please sign in to comment.