Skip to content

Commit

Permalink
Add --border=edge mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mtheall committed Jan 12, 2022
1 parent 4f5de03 commit bd40135
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 89 deletions.
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tex3ds_SOURCES = source/atlas.cpp \
source/rle.cpp \
source/swizzle.cpp \
source/tex3ds.cpp \
source/utility.cpp \
include/atlas.h \
include/compress.h \
include/encode.h \
Expand All @@ -21,7 +22,8 @@ tex3ds_SOURCES = source/atlas.cpp \
include/quantum.h \
include/rg_etc1.h \
include/subimage.h \
include/swizzle.h
include/swizzle.h \
include/utility.h

mkbcfnt_SOURCES = source/bcfnt.cpp \
source/freetype.cpp \
Expand Down
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
```
Usage: ./tex3ds [OPTIONS...] <input>
Options:
-d, --depends <file> Output dependency file
-f, --format <format> See "Format Options"
-H, --header <file> Output C header to file
-h, --help Show this help message
Expand All @@ -17,9 +18,10 @@ Usage: ./tex3ds [OPTIONS...] <input>
-t, --trim Trim input image(s)
-v, --version Show version and copyright information
-z, --compress <compression> Compress output. See "Compression Options"
--atlas Generate texture atlas
--cubemap Generate a cubemap. See "Cubemap"
--skybox Generate a skybox. See "Skybox"
-a, --atlas Generate texture atlas
-c, --cubemap Generate a cubemap. See "Cubemap"
-s, --skybox Generate a skybox. See "Skybox"
-b, --border <border-type> Inserts a border around each image. See "Border Options"
<input> Input file
```

Expand Down Expand Up @@ -119,7 +121,7 @@ Usage: ./tex3ds [OPTIONS...] <input>
```
-z auto Automatically select best compression (default)
-z none No compression
-z huff, -z huffman Huffman encoding (possible to produce garbage)
-z huff, -z huffman Huffman encoding
-z lzss, -z lz10 LZSS compression
-z lz11 LZ11 compression
-z rle Run-length encoding
Expand All @@ -138,6 +140,15 @@ Usage: ./tex3ds [OPTIONS...] <input>
0x30: Run-length encoding
```

## Border Options

```
Border Options:
-b none No border (default)
-b transparent 1px transparent shared border around images
-b edge 1px color-matched unshared border around images
```

## Cubemap

```
Expand Down Expand Up @@ -176,4 +187,4 @@ Usage: ./mkbcfnt [OPTIONS...] <input>
-s, --size <size> Set font size in points
-v, --version Show version and copyright information
<input> Input file
```
```
5 changes: 3 additions & 2 deletions include/atlas.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2017-2021
* Copyright (c) 2017-2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
Expand Down Expand Up @@ -42,5 +42,6 @@ struct Atlas
Atlas &operator= (const Atlas &other) = delete;
Atlas &operator= (Atlas &&other) = delete;

static Atlas build (const std::vector<std::string> &paths, bool trim, unsigned int border);
static Atlas
build (const std::vector<std::string> &paths, bool trim, unsigned border, unsigned edge);
};
4 changes: 2 additions & 2 deletions include/subimage.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2017-2021
* Copyright (c) 2017-2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
Expand Down Expand Up @@ -71,7 +71,7 @@ struct SubImage
return index < rhs.index;
}

void print (unsigned int width, unsigned int height) const
void print (unsigned width, unsigned height) const
{
if (rotated)
std::printf ("%4zu \"%s\"\n"
Expand Down
29 changes: 29 additions & 0 deletions include/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
*
* tex3ds 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.
*
* tex3ds 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 tex3ds. If not, see <http://www.gnu.org/licenses/>.
*----------------------------------------------------------------------------*/
/** @file utility.h
* @brief Utility functions.
*/
#pragma once

#include "magick_compat.h"

Magick::Image applyTrim (Magick::Image &img);

void applyEdge (Magick::Image &img);
49 changes: 25 additions & 24 deletions source/atlas.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2017-2021
* Copyright (c) 2017-2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
Expand All @@ -23,6 +23,7 @@

#include "atlas.h"
#include "subimage.h"
#include "utility.h"

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -57,7 +58,7 @@ struct Block
Block &operator= (const Block &other) = delete;
Block &operator= (Block &&other) = delete;

Block (size_t index, const Magick::Image &img, unsigned int border)
Block (size_t index, const Magick::Image &img, unsigned border)
: index (index),
img (img),
x (0),
Expand All @@ -68,20 +69,20 @@ struct Block
{
}

SubImage subImage (const Magick::Image &atlas, unsigned int border) const
SubImage subImage (const Magick::Image &atlas, unsigned border, unsigned edge) const
{
size_t width = atlas.columns () + border;
size_t height = atlas.rows () + border;

float left = static_cast<float> (x + border) / width;
float top = 1.0f - (static_cast<float> (y + border) / height);
float right = static_cast<float> (x + w) / width;
float bottom = 1.0f - (static_cast<float> (y + h) / height);
float left = static_cast<float> (x + border + edge) / width;
float top = 1.0f - (static_cast<float> (y + border + edge) / height);
float right = static_cast<float> (x + w - edge) / width;
float bottom = 1.0f - (static_cast<float> (y + h - edge) / height);

assert (left * width == x + border);
assert ((1.0f - top) * height == y + border);
assert (right * width == x + w);
assert ((1.0f - bottom) * height == y + h);
assert (left * width == x + border + edge);
assert ((1.0f - top) * height == y + border + edge);
assert (right * width == x + w - edge);
assert ((1.0f - bottom) * height == y + h - edge);

if (rotated)
return SubImage (index, img.fileName (), bottom, left, top, right, true);
Expand Down Expand Up @@ -111,18 +112,15 @@ struct Packer
std::set<XY> free;

size_t width, height;
unsigned int border;
unsigned border;

Packer () = delete;
Packer (const Packer &other) = delete;
Packer (Packer &&other) = default;
Packer &operator= (const Packer &other) = delete;
Packer &operator= (Packer &&other) = default;

Packer (const std::vector<Magick::Image> &images,
size_t width,
size_t height,
unsigned int border);
Packer (const std::vector<Magick::Image> &images, size_t width, size_t height, unsigned border);

Magick::Image composite () const
{
Expand Down Expand Up @@ -197,7 +195,7 @@ struct Packer
Packer::Packer (const std::vector<Magick::Image> &images,
size_t width,
size_t height,
unsigned int border)
unsigned border)
: placed (), next (), free (), width (width), height (height), border (border)
{
for (const auto &img : images)
Expand Down Expand Up @@ -371,7 +369,10 @@ struct AreaSizeComparator
};
}

Atlas Atlas::build (const std::vector<std::string> &paths, bool trim, unsigned int border)
Atlas Atlas::build (const std::vector<std::string> &paths,
bool trim,
unsigned border,
unsigned edge)
{
std::vector<Magick::Image> images;

Expand All @@ -380,10 +381,10 @@ Atlas Atlas::build (const std::vector<std::string> &paths, bool trim, unsigned i
Magick::Image img (path);

if (trim)
{
img.trim ();
img.page (Magick::Geometry (img.columns (), img.rows ()));
}
img = applyTrim (img);

if (edge)
applyEdge (img);

img.attribute ("index", std::to_string (images.size ()));
images.emplace_back (std::move (img));
Expand Down Expand Up @@ -422,12 +423,12 @@ Atlas Atlas::build (const std::vector<std::string> &paths, bool trim, unsigned i

atlas.img = packer.composite ();
for (auto &block : packer.placed)
atlas.subs.emplace_back (block.subImage (atlas.img, border));
atlas.subs.emplace_back (block.subImage (atlas.img, border, edge));

std::sort (std::begin (atlas.subs), std::end (atlas.subs));
return atlas;
}
}

throw std::runtime_error ("No atlas solution found.\n");
throw std::runtime_error ("No atlas solution found.");
}
6 changes: 3 additions & 3 deletions source/encode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2017-2019
* Copyright (c) 2017-2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
Expand Down Expand Up @@ -74,7 +74,7 @@ void etc1_common (encode::WorkUnit &work, bool alpha)

// encode etc1 block
rg_etc1::pack_etc1_block (
out_block, reinterpret_cast<unsigned int *> (in_block), params);
out_block, reinterpret_cast<unsigned *> (in_block), params);
}

if (work.output)
Expand All @@ -93,7 +93,7 @@ void etc1_common (encode::WorkUnit &work, bool alpha)

if (work.preview)
{
rg_etc1::unpack_etc1_block (out_block, reinterpret_cast<unsigned int *> (in_block));
rg_etc1::unpack_etc1_block (out_block, reinterpret_cast<unsigned *> (in_block));

for (size_t y = 0; y < 4; ++y)
{
Expand Down
6 changes: 3 additions & 3 deletions source/lzss.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------
* Copyright (c) 2017-2019
* Copyright (c) 2017-2022
* Michael Theall (mtheall)
*
* This file is part of tex3ds.
Expand Down Expand Up @@ -328,8 +328,8 @@ void lzssDecode (const void *source, void *dest, size_t size)
uint8_t *dst = (uint8_t *)dest;
uint8_t flags = 0;
uint8_t mask = 0;
unsigned int len;
unsigned int disp;
unsigned len;
unsigned disp;

while (size > 0)
{
Expand Down
Loading

0 comments on commit bd40135

Please sign in to comment.