Skip to content

Commit

Permalink
thanm: bring back image composition
Browse files Browse the repository at this point in the history
Turns out #111 wasn't that much of an issue after all.

This is a partial revert of 58be110, in
a sense that I reimplemented the stuff that I removed in that commit.

Now we have both combined and separate (-u) extraction implemented in a
somewhat elegant manner. Every entry now has a link to the next entry of
the same name. For separate extraction, we simply don't build the links.

Additionally, th19 is now fully supported in the combined mode (except
for replacement and for JPEGs). thanm will opportunistically try to
avoid recompressing PNGs. The -uu flag creates all the necessary
conditions for that.
  • Loading branch information
DankRank committed Sep 2, 2023
1 parent 690fa6a commit 64143c2
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 105 deletions.
4 changes: 2 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ What's new in thtk master
- Game version now must be specified, just as with other tools.
- Support for TH18, TH185, TH19 (full) has been added.
- Support th143/bestshot.anm
- The -u flag can now be used to extract anm files with duplicate filenames.
- Images can now be extracted without adding a border using the -b flag.
- Two new options (-u and -uu) were added to help with rebuilding TH19 files
bit-perfectly. See documentation for more details.

#### thanm.old
- Will be removed in the next release.
Expand Down
6 changes: 1 addition & 5 deletions thanm/anmparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Statement:

Entry:
"entry" IDENTIFIER[entry_name] "{" Properties[prop_list] "}" {
anm_entry_t* entry = (anm_entry_t*)malloc(sizeof(anm_entry_t));
anm_entry_t* entry = (anm_entry_t*)calloc(1, sizeof(anm_entry_t));
entry->header = (anm_header06_t*)calloc(1, sizeof(anm_header06_t));
entry->thtx = (thtx_header_t*)calloc(1, sizeof(thtx_header_t));

Expand All @@ -217,12 +217,8 @@ Entry:
entry->thtx->magic[2] = 'T';
entry->thtx->magic[3] = 'X';

entry->name = NULL;
entry->name2 = NULL;
entry->filename = NULL;
list_init(&entry->sprites);
list_init(&entry->scripts);
entry->data = NULL;

prop_list_entry_t* prop;
#define REQUIRE(x, y, l) { \
Expand Down
1 change: 1 addition & 0 deletions thanm/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ png_write_mem(
png.width = image->width;
png.height = image->height;
png.format = PNG_FORMAT_RGBA;
png.flags = PNG_IMAGE_FLAG_FAST;

png_image_write_to_memory(&png, 0, outsize, 0, image->data, 0, 0);
if (PNG_IMAGE_FAILED(png)) {
Expand Down
90 changes: 75 additions & 15 deletions thanm/thanm.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
.\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
.\" DAMAGE.
.Dd August 30, 2023
.Dd September 3, 2023
.Dt THANM 1
.Os
.Sh NAME
.Nm thanm
.Nd Touhou sprite archive tool
.Sh SYNOPSIS
.Nm
.Op Fl Vfub
.Op Fl Vfuv
.Op Oo Fl l | x | r | c Oc Ar version
.Oo Fl m Ar anmmap Oc Ns Ar ...
.Oo Fl s Ar symbols Oc
Expand All @@ -43,17 +43,17 @@ The
utility performs various actions on sprite archives.
The following commands are available:
.Bl -tag -width Ds
.It Nm Fl l Ar version Oo Fl fu Oc Oo Fl m Ar anmmap Oc Ns Ar ... Ar archive
.It Nm Fl l Ar version Oo Fl fuv Oc Oo Fl m Ar anmmap Oc Ns Ar ... Ar archive
Displays a specification of the archive.
.It Nm Fl x Ar version Oo Fl fub Oc Ar archive Op Ar
.It Nm Fl x Ar version Oo Fl fuv Oc Ar archive Op Ar
Extracts image files.
If no files are specified, all files are extracted.
.It Nm Fl r Ar version Oo Fl fb Oc Ar archive Ar name Ar file
.It Nm Fl r Ar version Oo Fl fuv Oc Ar archive Ar name Ar file
Replaces an entry in the archive.
The name can be obtained by the
.Fl l
command.
.It Nm Fl c Ar version Oo Fl fb Oc Oo Fl m Ar anmmap Oc Ns Ar ... Oo Fl s Ar symbols Oc Ar archive Ar input
.It Nm Fl c Ar version Oo Fl fuv Oc Oo Fl m Ar anmmap Oc Ns Ar ... Oo Fl s Ar symbols Oc Ar archive Ar input
Creates a new archive from a specification obtained by the
.Fl l
command.
Expand All @@ -78,23 +78,83 @@ The
option saves symbol ids to the
.Ar symbols
file.
.It u
.It Fl u
The
.Fl u
option gives unique filenames to extracted images.
.It b
option extracts each texture into a separate file.
When specified twice, x/y offset is ignored.
See
.Sx IMAGE COMPOSITION
for more information.
.It Fl v
The
.Fl b
option disables adding/removing a border to images with x/y offset.
For TH19,
.Nm
won't recompress the image unless it needs to add a border.
JPEGs are never recompressed, regardless of the setting.
.Fl v
option increases verbosity of the output.
It can be specified multiple times.
.El
.Sh EXIT STATUS
The
.Nm
utility exits with 0 on success, 1 on error.
.Sh IMAGE COMPOSITION
Each texture in an ANM file has an associated source image filename.
By default
.Nm
creates textures by cropping the source image
according to the following parameters:
.Va xOffset ,
.Va yOffset ,
.Va THTXWidth ,
.Va THTXHeight .
When extracting the textures, the process is reversed.
However, because multiple textures may come from a single source file,
they have to be composed together during extraction.
Additionally, in either case, the textures have to be converted to/from
the appropriate format.
.Bl -tag -width Ds
.It Composition when creating Po Fl c Pc or replacing Po Fl r Pc :
The source image is read from a file, decoded as PNG, cropped,
and encoded in the texture format.
.It Composition when extracting Po Fl x Pc :
Each texture is decoded into RGBA, and placed on a canvas.
The canvas is encoded as PNG and written to the destination file.
.El
.Pp
Currently there's no way to compose textures from multiple ANM files.
.Pp
The
.Fl u
option extracts each texture into its own file.
It's important to also use the
.Fl u
option for listing
.Pq Fl l ,
because
.Fl c
relies on the alternative filenames being specified in the spec file.
Even with
.Fl u
the images are cropped/padded when they have an offset.
The
.Fl uu
option ignores the offset, thereby disabling cropping/padding.
.Pp
In TH19, the textures are stored using PNG and JPEG compression.
.Nm
tries to avoid recompression when possible,
so composition is only done when necessary.
Uncompressed textures (pre-TH19) are always composed,
even if only to convert them to PNG.
PNGs are only composed only if there's a nonzero offset,
or there are multiple textures referencing the same image.
Otherwise the file is copied directly, without recompression
.Pq Fl uu No forces this behavior .
JPEGs are
.Em never
composed, but it shouldn't be necessary either.
If a JPEG has to be composed,
.Nm
will issue a warning.
.Sh "ANMMAP FILE FORMAT"
Anmmap files, which are added with the
.Fl m
Expand Down
Loading

0 comments on commit 64143c2

Please sign in to comment.