mirror of
https://github.com/zeldaret/oot.git
synced 2025-06-07 09:01:49 +00:00
Add n64texconv
and bin2c
tools to convert extracted .png and .bin to C arrays during build (#2477)
* n64texconv and bin2c * mv tools/n64texconv tools/assets/ * fix * more light fixes * Silence -Wshadow for libimagequant * Add reference counting gc for palette objects in python bindings * Fix missing alignment in n64texconv_*_to_c functions Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com> * Check palette size in n64texconv_image_from_png * accept memoryview as well as bytes for binary data * minimal doc on n64texconv_quantize_shared * fix a buffer size passed to libimagequant * assert pal count <= 256 on png write * Disable palette size check for input pngs, ZAPD fails the check * No OpenMP for clang * When reading an indexed png into a CI format, requantize if there are too many colors for the target texel size --------- Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>
This commit is contained in:
parent
ec30dcbe4e
commit
3d61fb85ef
41 changed files with 15634 additions and 9 deletions
15
Makefile
15
Makefile
|
@ -320,7 +320,8 @@ CPP := gcc -E
|
|||
MKLDSCRIPT := tools/mkldscript
|
||||
MKDMADATA := tools/mkdmadata
|
||||
ELF2ROM := tools/elf2rom
|
||||
ZAPD := tools/ZAPD/ZAPD.out
|
||||
BIN2C := tools/bin2c
|
||||
N64TEXCONV := tools/assets/n64texconv/n64texconv
|
||||
FADO := tools/fado/fado.elf
|
||||
PYTHON ?= $(VENV)/bin/python3
|
||||
|
||||
|
@ -987,22 +988,22 @@ $(BUILD_DIR)/src/overlays/%_reloc.o: $(BUILD_DIR)/spec
|
|||
$(AS) $(ASFLAGS) $(@:.o=.s) -o $@
|
||||
|
||||
$(BUILD_DIR)/assets/%.inc.c: assets/%.png
|
||||
$(ZAPD) btex -eh -tt $(subst .,,$(suffix $*)) -i $< -o $@
|
||||
$(N64TEXCONV) $(subst .,,$(suffix $*)) "$(findstring u32,$(subst .,,$(suffix $(basename $*))))" $< $@ $(@:.inc.c=.pal.inc.c)
|
||||
|
||||
$(BUILD_DIR)/assets/%.inc.c: $(EXTRACTED_DIR)/assets/%.png
|
||||
$(ZAPD) btex -eh -tt $(subst .,,$(suffix $*)) -i $< -o $@
|
||||
$(N64TEXCONV) $(subst .,,$(suffix $*)) "$(findstring u32,$(subst .,,$(suffix $(basename $*))))" $< $@ $(@:.inc.c=.pal.inc.c)
|
||||
|
||||
$(BUILD_DIR)/assets/%.bin.inc.c: assets/%.bin
|
||||
$(ZAPD) bblb -eh -i $< -o $@
|
||||
$(BIN2C) -t 1 $< $@
|
||||
|
||||
$(BUILD_DIR)/assets/%.bin.inc.c: $(EXTRACTED_DIR)/assets/%.bin
|
||||
$(ZAPD) bblb -eh -i $< -o $@
|
||||
$(BIN2C) -t 1 $< $@
|
||||
|
||||
$(BUILD_DIR)/assets/%.jpg.inc.c: assets/%.jpg
|
||||
$(ZAPD) bren -eh -i $< -o $@
|
||||
$(N64TEXCONV) JFIF "" $< $@
|
||||
|
||||
$(BUILD_DIR)/assets/%.jpg.inc.c: $(EXTRACTED_DIR)/assets/%.jpg
|
||||
$(ZAPD) bren -eh -i $< -o $@
|
||||
$(N64TEXCONV) JFIF "" $< $@
|
||||
|
||||
# Audio
|
||||
|
||||
|
|
1
tools/.gitignore
vendored
1
tools/.gitignore
vendored
|
@ -1,5 +1,6 @@
|
|||
# Output files
|
||||
*.exe
|
||||
bin2c
|
||||
elf2rom
|
||||
makeromfs
|
||||
mkdmadata
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
CFLAGS := -Wall -Wextra -pedantic -std=c99 -g -O2
|
||||
PROGRAMS := elf2rom makeromfs mkdmadata mkldscript preprocess_pragmas reloc_prereq vtxdis
|
||||
CFLAGS := -Wall -Wextra -pedantic -std=gnu99 -g -O2
|
||||
PROGRAMS := bin2c elf2rom makeromfs mkdmadata mkldscript preprocess_pragmas reloc_prereq vtxdis
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
|
@ -39,6 +39,7 @@ all: $(PROGRAMS) $(IDO_RECOMP_5_3_DIR) $(IDO_RECOMP_7_1_DIR) $(EGCS_DIR)
|
|||
$(MAKE) -C fado
|
||||
$(MAKE) -C audio
|
||||
$(MAKE) -C com-plugin
|
||||
$(MAKE) -C assets
|
||||
|
||||
clean:
|
||||
$(RM) $(PROGRAMS) $(addsuffix .exe,$(PROGRAMS))
|
||||
|
@ -47,13 +48,16 @@ clean:
|
|||
$(MAKE) -C fado clean
|
||||
$(MAKE) -C audio clean
|
||||
$(MAKE) -C com-plugin clean
|
||||
$(MAKE) -C assets clean
|
||||
|
||||
distclean: clean
|
||||
$(MAKE) -C audio distclean
|
||||
$(MAKE) -C assets distclean
|
||||
|
||||
.PHONY: all clean distclean
|
||||
|
||||
elf2rom_SOURCES := elf2rom.c elf32.c n64chksum.c util.c
|
||||
bin2c_SOURCES := bin2c.c
|
||||
makeromfs_SOURCES := makeromfs.c n64chksum.c util.c
|
||||
mkdmadata_SOURCES := mkdmadata.c spec.c util.c
|
||||
mkldscript_SOURCES := mkldscript.c spec.c util.c
|
||||
|
|
10
tools/assets/Makefile
Normal file
10
tools/assets/Makefile
Normal file
|
@ -0,0 +1,10 @@
|
|||
all:
|
||||
$(MAKE) -C n64texconv
|
||||
|
||||
clean:
|
||||
$(MAKE) -C n64texconv clean
|
||||
|
||||
distclean: clean
|
||||
$(MAKE) -C n64texconv distclean
|
||||
|
||||
.PHONY: all clean distclean
|
29
tools/assets/n64texconv/.clang-format
Normal file
29
tools/assets/n64texconv/.clang-format
Normal file
|
@ -0,0 +1,29 @@
|
|||
IndentWidth: 4
|
||||
Language: Cpp
|
||||
UseTab: Never
|
||||
ColumnLimit: 120
|
||||
PointerAlignment: Right
|
||||
BreakBeforeBraces: Linux
|
||||
AlwaysBreakAfterReturnType: TopLevel
|
||||
AlignArrayOfStructures: Left
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceBeforeParens: ControlStatementsExceptControlMacros
|
||||
Cpp11BracedListStyle: false
|
||||
IndentCaseLabels: true
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignOperands: true
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AlignEscapedNewlines: Left
|
||||
AlignTrailingComments: true
|
||||
SortIncludes: false
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
ForEachMacros: ['LL_FOREACH']
|
10
tools/assets/n64texconv/.gitignore
vendored
Normal file
10
tools/assets/n64texconv/.gitignore
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
build/
|
||||
|
||||
libn64texconv.a
|
||||
libn64texconv.dll
|
||||
libn64texconv.so
|
||||
n64texconv
|
||||
|
||||
# Tests
|
||||
*.png
|
||||
*.bin
|
627
tools/assets/n64texconv/LICENSE
Normal file
627
tools/assets/n64texconv/LICENSE
Normal file
|
@ -0,0 +1,627 @@
|
|||
|
||||
N64 texture converter (n64texconv).
|
||||
|
||||
The library is under the MIT license (see src/LICENSE), but the app
|
||||
links statically with libimagequant which is under the GPL.
|
||||
|
||||
Copyright (C) 2025 ZeldaRET
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
77
tools/assets/n64texconv/Makefile
Normal file
77
tools/assets/n64texconv/Makefile
Normal file
|
@ -0,0 +1,77 @@
|
|||
BUILD_DIR := build
|
||||
|
||||
# Targets
|
||||
LIB := libn64texconv.a
|
||||
SOLIB := libn64texconv.so
|
||||
APP := n64texconv
|
||||
|
||||
INC := -Ilib/spng -Ilib/libimagequant
|
||||
|
||||
CC := gcc
|
||||
|
||||
WFLAGS := -Wall -Wextra -Wshadow
|
||||
|
||||
ifeq ($(shell $(CC) --version | grep clang),)
|
||||
ARCHFLAGS := -march=native -mtune=native
|
||||
OMPFLAGS := -fopenmp
|
||||
else
|
||||
ARCHFLAGS :=
|
||||
OMPFLAGS :=
|
||||
WFLAGS += -Wno-unknown-pragmas
|
||||
endif
|
||||
|
||||
CFLAGS := $(WFLAGS) $(ARCHFLAGS) -MD -MMD -std=gnu11 -fPIC -ffunction-sections -fdata-sections $(INC)
|
||||
OPTFLAGS := -O3
|
||||
LDFLAGS :=
|
||||
LDLIBS := $(OMPFLAGS) -lz -lm
|
||||
AR := ar
|
||||
ARFLAGS := rcs
|
||||
|
||||
SRC_DIRS := $(shell find src -type d -not -path src/app)
|
||||
LIB_DIRS := $(shell find lib -type d)
|
||||
APP_SRC_DIRS := $(shell find src/app -type d)
|
||||
|
||||
C_FILES := $(foreach dir,$(SRC_DIRS) $(LIB_DIRS),$(wildcard $(dir)/*.c))
|
||||
O_FILES := $(foreach f,$(C_FILES:.c=.o),$(BUILD_DIR)/$f)
|
||||
DEP_FILES := $(foreach f,$(O_FILES:.o=.d),$f)
|
||||
|
||||
APP_C_FILES := $(foreach dir,$(APP_SRC_DIRS),$(wildcard $(dir)/*.c))
|
||||
APP_O_FILES := $(foreach f,$(APP_C_FILES:.c=.o),$(BUILD_DIR)/$f)
|
||||
APP_DEP_FILES := $(foreach f,$(APP_O_FILES:.o=.d),$f)
|
||||
|
||||
FMT_C_FILES := $(foreach dir,$(SRC_DIRS) $(APP_SRC_DIRS),$(wildcard $(dir)/*.c))
|
||||
FMT_H_FILES := $(foreach dir,$(SRC_DIRS) $(APP_SRC_DIRS),$(wildcard $(dir)/*.h))
|
||||
FMT_FILES := $(FMT_C_FILES) $(FMT_H_FILES)
|
||||
|
||||
CLANG_FORMAT := clang-format-14
|
||||
FORMAT_ARGS := -i -style=file
|
||||
|
||||
$(shell mkdir -p $(BUILD_DIR) $(foreach dir,$(SRC_DIRS) $(LIB_DIRS) $(APP_SRC_DIRS),$(BUILD_DIR)/$(dir)))
|
||||
|
||||
$(BUILD_DIR)/lib/libimagequant/%.o: CFLAGS += $(OMPFLAGS) -Wno-sign-compare -Wno-unused-parameter -Wno-shadow
|
||||
|
||||
.PHONY: all clean distclean format
|
||||
|
||||
all: $(LIB) $(SOLIB) $(APP)
|
||||
|
||||
clean:
|
||||
$(RM) -r $(LIB) $(SOLIB) $(APP) $(BUILD_DIR)
|
||||
|
||||
distclean: clean
|
||||
|
||||
format:
|
||||
$(CLANG_FORMAT) $(FORMAT_ARGS) $(FMT_FILES)
|
||||
|
||||
$(LIB): $(O_FILES)
|
||||
$(AR) $(ARFLAGS) $@ $^
|
||||
|
||||
$(SOLIB): $(O_FILES)
|
||||
$(CC) -shared $^ $(LDLIBS) -o $@
|
||||
|
||||
$(APP): $(APP_O_FILES) $(LIB)
|
||||
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) $(OPTFLAGS) -c $< -o $@
|
||||
|
||||
-include $(DEP_FILES) $(APP_DEP_FILES)
|
472
tools/assets/n64texconv/__init__.py
Normal file
472
tools/assets/n64texconv/__init__.py
Normal file
|
@ -0,0 +1,472 @@
|
|||
# SPDX-FileCopyrightText: © 2025 ZeldaRET
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import os, sys
|
||||
from ctypes import CDLL, Structure, POINTER, string_at, byref, cast
|
||||
from ctypes import c_void_p, c_char_p, c_uint8, c_uint, c_bool, c_int, c_size_t
|
||||
from typing import Optional
|
||||
|
||||
def ctypes_buffer_to_string(buffer) -> str:
|
||||
return buffer.value.decode('utf-8')
|
||||
|
||||
def ctypes_pointer_to_bytes(ptr : c_void_p, size : int) -> bytes:
|
||||
return string_at(ptr, size)
|
||||
|
||||
def deref(ptr):
|
||||
if ptr:
|
||||
return ptr.contents
|
||||
return None
|
||||
|
||||
ln64texconv = CDLL(os.path.join(os.path.dirname(__file__), "libn64texconv.so"))
|
||||
|
||||
class RefCounter:
|
||||
def __init__(self) -> None:
|
||||
self.ref_counts = {}
|
||||
|
||||
def add_ref(self, ptr):
|
||||
if not isinstance(ptr, POINTER(N64Palette)):
|
||||
ptr = cast(ptr, POINTER(N64Palette))
|
||||
if not ptr:
|
||||
return
|
||||
key = int.from_bytes(ptr, byteorder=sys.byteorder, signed=False)
|
||||
if key not in self.ref_counts:
|
||||
self.ref_counts[key] = 1
|
||||
else:
|
||||
self.ref_counts[key] += 1
|
||||
|
||||
def num_refs(self, ptr):
|
||||
if not isinstance(ptr, POINTER(N64Palette)):
|
||||
ptr = cast(ptr, POINTER(N64Palette))
|
||||
if not ptr:
|
||||
return 0
|
||||
key = int.from_bytes(ptr, byteorder=sys.byteorder, signed=False)
|
||||
if key not in self.ref_counts:
|
||||
return 0
|
||||
return self.ref_counts[key]
|
||||
|
||||
def rm_ref(self, ptr, free_func):
|
||||
if not isinstance(ptr, POINTER(N64Palette)):
|
||||
ptr = cast(ptr, POINTER(N64Palette))
|
||||
if not ptr:
|
||||
return
|
||||
key = int.from_bytes(ptr, byteorder=sys.byteorder, signed=False)
|
||||
assert key in self.ref_counts
|
||||
count = self.ref_counts.pop(key)
|
||||
count -= 1
|
||||
if count == 0:
|
||||
free_func(ptr)
|
||||
else:
|
||||
self.ref_counts[key] = count
|
||||
|
||||
# Simple reference counter for C allocations
|
||||
_object_refcount = RefCounter()
|
||||
|
||||
#
|
||||
# Private
|
||||
#
|
||||
|
||||
# void n64texconv_free(void *p);
|
||||
ln64texconv.n64texconv_free.argtypes = [c_void_p]
|
||||
ln64texconv.n64texconv_free.restype = None
|
||||
|
||||
#
|
||||
# bin2c.h
|
||||
#
|
||||
|
||||
def bin2c(data : bytes | memoryview, pad_to_size : int = 0, byte_width : int = 8) -> Optional[str]:
|
||||
if byte_width not in (1, 2, 4, 8):
|
||||
raise ValueError("Invalid byte width, must be 1, 2, 4 or 8")
|
||||
buffer = (c_uint8 * len(data)).from_buffer_copy(data)
|
||||
ptr = c_char_p(None)
|
||||
size = c_size_t(0)
|
||||
if ln64texconv.bin2c(byref(ptr), byref(size), buffer, len(data), pad_to_size, byte_width) != 0:
|
||||
return None
|
||||
s = ctypes_buffer_to_string(ptr)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return s
|
||||
|
||||
def bin2c_file(out_path : str, data : bytes | memoryview, pad_to_size : int = 0, byte_width : int = 8) -> bool:
|
||||
if byte_width not in (1, 2, 4, 8):
|
||||
raise ValueError("Invalid byte width, must be 1, 2, 4 or 8")
|
||||
buffer = (c_uint8 * len(data)).from_buffer_copy(data)
|
||||
return ln64texconv.bin2c_file(out_path.encode("utf-8"), buffer, len(data), pad_to_size, byte_width) == 0
|
||||
|
||||
# int bin2c(char **out, size_t *size_out, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width);
|
||||
ln64texconv.bin2c.argtypes = [POINTER(c_char_p), POINTER(c_size_t), c_void_p, c_size_t, c_size_t, c_uint]
|
||||
ln64texconv.bin2c.restype = c_int
|
||||
|
||||
# int bin2c_file(const char *out_path, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width);
|
||||
ln64texconv.bin2c_file.argtypes = [c_char_p, c_void_p, c_size_t, c_size_t, c_uint]
|
||||
ln64texconv.bin2c_file.restype = c_int
|
||||
|
||||
#
|
||||
# jfif.h
|
||||
#
|
||||
|
||||
# struct JFIF
|
||||
class JFIF(Structure):
|
||||
_fields_ = [
|
||||
("data", c_void_p),
|
||||
("data_size", c_size_t),
|
||||
]
|
||||
|
||||
# JFIF_BUFFER_SIZE
|
||||
BUFFER_SIZE = 320 * 240 * 2
|
||||
|
||||
@staticmethod
|
||||
def fromfile(path : str, max_size : int = BUFFER_SIZE) -> Optional["JFIF"]:
|
||||
if not os.path.isfile(path):
|
||||
raise ValueError(f"Cannot open \"{path}\", is not a file")
|
||||
return deref(ln64texconv.jfif_fromfile(path.encode("utf-8"), max_size))
|
||||
|
||||
def to_c(self, pad_to_size : int = BUFFER_SIZE) -> Optional[str]:
|
||||
ptr = c_char_p(None)
|
||||
size = c_size_t(0)
|
||||
if ln64texconv.jfif_to_c(byref(ptr), byref(size), byref(self), pad_to_size) != 0:
|
||||
return None
|
||||
s = ctypes_buffer_to_string(ptr)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return s
|
||||
|
||||
def to_c_file(self, out_path : str, pad_to_size : int = BUFFER_SIZE) -> bool:
|
||||
return ln64texconv.jfif_to_c_file(out_path.encode("utf-8"), byref(self), pad_to_size) == 0
|
||||
|
||||
def __del__(self):
|
||||
ln64texconv.jfif_free(byref(self))
|
||||
|
||||
# struct JFIF *jfif_fromfile(const char *path, size_t max_size);
|
||||
ln64texconv.jfif_fromfile.argtypes = [c_char_p, c_size_t]
|
||||
ln64texconv.jfif_fromfile.restype = POINTER(JFIF)
|
||||
|
||||
# void jfif_free(struct JFIF *jfif);
|
||||
ln64texconv.jfif_free.argtypes = [POINTER(JFIF)]
|
||||
ln64texconv.jfif_free.restype = None
|
||||
|
||||
# int jfif_to_c(char **out, size_t *size_out, struct JFIF *jfif, size_t pad_to_size)
|
||||
ln64texconv.jfif_to_c.argtypes = [POINTER(c_char_p), POINTER(c_size_t), POINTER(JFIF), c_size_t]
|
||||
ln64texconv.jfif_to_c.restype = c_int
|
||||
|
||||
# int jfif_to_c_file(const char *out_path, struct JFIF *jfif, size_t pad_to_size);
|
||||
ln64texconv.jfif_to_c_file.argtypes = [c_char_p, POINTER(JFIF), c_size_t]
|
||||
ln64texconv.jfif_to_c_file.restype = c_int
|
||||
|
||||
#
|
||||
# n64texconv.h
|
||||
#
|
||||
|
||||
FMT_NONE = -1
|
||||
FMT_MAX = 5
|
||||
G_IM_FMT_RGBA = 0
|
||||
G_IM_FMT_YUV = 1
|
||||
G_IM_FMT_CI = 2
|
||||
G_IM_FMT_IA = 3
|
||||
G_IM_FMT_I = 4
|
||||
|
||||
SIZ_NONE = -1
|
||||
SIZ_MAX = 4
|
||||
G_IM_SIZ_4b = 0
|
||||
G_IM_SIZ_8b = 1
|
||||
G_IM_SIZ_16b = 2
|
||||
G_IM_SIZ_32b = 3
|
||||
|
||||
def fmt_name(fmt : int):
|
||||
return {
|
||||
G_IM_FMT_RGBA : "G_IM_FMT_RGBA",
|
||||
G_IM_FMT_YUV : "G_IM_FMT_YUV",
|
||||
G_IM_FMT_CI : "G_IM_FMT_CI",
|
||||
G_IM_FMT_IA : "G_IM_FMT_IA",
|
||||
G_IM_FMT_I : "G_IM_FMT_I",
|
||||
}.get(fmt, str(fmt))
|
||||
|
||||
def siz_name(siz : int):
|
||||
return {
|
||||
G_IM_SIZ_4b : "G_IM_SIZ_4b",
|
||||
G_IM_SIZ_8b : "G_IM_SIZ_8b",
|
||||
G_IM_SIZ_16b : "G_IM_SIZ_16b",
|
||||
G_IM_SIZ_32b : "G_IM_SIZ_32b",
|
||||
}.get(siz, str(siz))
|
||||
|
||||
VALID_FORMAT_COMBINATIONS = (
|
||||
(G_IM_FMT_RGBA, G_IM_SIZ_16b),
|
||||
(G_IM_FMT_RGBA, G_IM_SIZ_32b),
|
||||
(G_IM_FMT_CI, G_IM_SIZ_4b),
|
||||
(G_IM_FMT_CI, G_IM_SIZ_8b),
|
||||
(G_IM_FMT_IA, G_IM_SIZ_4b),
|
||||
(G_IM_FMT_IA, G_IM_SIZ_8b),
|
||||
(G_IM_FMT_IA, G_IM_SIZ_16b),
|
||||
(G_IM_FMT_I, G_IM_SIZ_4b),
|
||||
(G_IM_FMT_I, G_IM_SIZ_8b),
|
||||
)
|
||||
|
||||
# struct color
|
||||
class Color(Structure):
|
||||
_fields_ = [
|
||||
("r", c_uint8),
|
||||
("g", c_uint8),
|
||||
("b", c_uint8),
|
||||
("a", c_uint8),
|
||||
]
|
||||
|
||||
# static inline size_t texel_size_bytes(size_t ntexels, int siz)
|
||||
def texel_size_bytes(ntexels : int, siz : int):
|
||||
return (ntexels // 2) if (siz == G_IM_SIZ_4b) else (ntexels * ((1 << siz) >> 1))
|
||||
|
||||
# struct n64_palette
|
||||
class N64Palette(Structure):
|
||||
_fields_ = [
|
||||
("texels", POINTER(Color)),
|
||||
("fmt", c_int),
|
||||
("count", c_size_t),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def new(count : int, fmt : int) -> Optional["N64Palette"]:
|
||||
if fmt not in (G_IM_FMT_RGBA, G_IM_FMT_IA):
|
||||
raise ValueError("Palette format must be either G_IM_FMT_RGBA or G_IM_FMT_IA")
|
||||
if count > 256:
|
||||
raise ValueError("The largest possible palette size is 256")
|
||||
pal = ln64texconv.n64texconv_palette_new(count, fmt)
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(pal)
|
||||
|
||||
def __del__(self):
|
||||
# Free the underlying palette structure only if the refcount is 0
|
||||
_object_refcount.rm_ref(byref(self), ln64texconv.n64texconv_palette_free)
|
||||
|
||||
def copy(self) -> Optional["N64Palette"]:
|
||||
pal = ln64texconv.n64texconv_palette_copy(byref(self))
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(pal)
|
||||
|
||||
def reformat(self, fmt : int) -> Optional["N64Palette"]:
|
||||
if fmt not in (G_IM_FMT_RGBA, G_IM_FMT_IA):
|
||||
raise ValueError("Palette format must be either G_IM_FMT_RGBA or G_IM_FMT_IA")
|
||||
pal = ln64texconv.n64texconv_palette_reformat(byref(self), fmt)
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(pal)
|
||||
|
||||
@staticmethod
|
||||
def from_png(path : str, fmt : int) -> Optional["N64Palette"]:
|
||||
if fmt not in (G_IM_FMT_RGBA, G_IM_FMT_IA):
|
||||
raise ValueError("Palette format must be either G_IM_FMT_RGBA or G_IM_FMT_IA")
|
||||
if not os.path.isfile(path):
|
||||
raise ValueError(f"Cannot open \"{path}\", is not a file")
|
||||
pal = ln64texconv.n64texconv_palette_from_png(path.encode("utf-8"), fmt)
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(pal)
|
||||
|
||||
@staticmethod
|
||||
def from_bin(data : bytes | memoryview, fmt : int) -> Optional["N64Palette"]:
|
||||
if fmt not in (G_IM_FMT_RGBA, G_IM_FMT_IA):
|
||||
raise ValueError("Palette format must be either G_IM_FMT_RGBA or G_IM_FMT_IA")
|
||||
buffer = (c_uint8 * len(data)).from_buffer_copy(data)
|
||||
pal = ln64texconv.n64texconv_palette_from_bin(buffer, len(data) // 2, fmt)
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(pal)
|
||||
|
||||
def to_png(self, outpath : str) -> bool:
|
||||
return ln64texconv.n64texconv_palette_to_png(outpath.encode("utf-8"), byref(self)) == 0
|
||||
|
||||
def to_bin(self, pad_to_8b : bool) -> Optional[bytes]:
|
||||
nbytes = texel_size_bytes(self.count, G_IM_SIZ_16b)
|
||||
if pad_to_8b:
|
||||
nbytes = (nbytes + 7) & ~7
|
||||
ptr = ln64texconv.n64texconv_palette_to_bin(byref(self), pad_to_8b)
|
||||
if not ptr:
|
||||
return None
|
||||
data = ctypes_pointer_to_bytes(ptr, nbytes)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return data
|
||||
|
||||
def to_c(self, pad_to_8b : bool, byte_width : int) -> Optional[str]:
|
||||
ptr = c_char_p(None)
|
||||
size = c_size_t(0)
|
||||
if ln64texconv.n64texconv_palette_to_c(byref(ptr), byref(size), byref(self), pad_to_8b, byte_width) != 0:
|
||||
return None
|
||||
s = ctypes_buffer_to_string(ptr)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return s
|
||||
|
||||
def to_c_file(self, out_path : str, pad_to_8b : bool, byte_width : int) -> bool:
|
||||
return ln64texconv.n64texconv_palette_to_c_file(out_path.encode("utf-8"), byref(self), pad_to_8b, byte_width) == 0
|
||||
|
||||
# struct n64_palette *n64texconv_palette_new(size_t count, int fmt);
|
||||
ln64texconv.n64texconv_palette_new.argtypes = [c_size_t, c_int]
|
||||
ln64texconv.n64texconv_palette_new.restype = POINTER(N64Palette)
|
||||
|
||||
# void n64texconv_palette_free(struct n64_palette *pal);
|
||||
ln64texconv.n64texconv_palette_free.argtypes = [POINTER(N64Palette)]
|
||||
ln64texconv.n64texconv_palette_free.restype = None
|
||||
|
||||
# struct n64_palette *n64texconv_palette_copy(struct n64_palette *pal);
|
||||
ln64texconv.n64texconv_palette_copy.argtypes = [POINTER(N64Palette)]
|
||||
ln64texconv.n64texconv_palette_copy.restype = POINTER(N64Palette)
|
||||
|
||||
# struct n64_palette *n64texconv_palette_reformat(struct n64_palette *pal, int fmt);
|
||||
ln64texconv.n64texconv_palette_reformat.argtypes = [POINTER(N64Palette), c_int]
|
||||
ln64texconv.n64texconv_palette_reformat.restype = POINTER(N64Palette)
|
||||
|
||||
# struct n64_palette *n64texconv_palette_from_png(const char *path, int fmt);
|
||||
ln64texconv.n64texconv_palette_from_png.argtypes = [c_char_p, c_int]
|
||||
ln64texconv.n64texconv_palette_from_png.restype = POINTER(N64Palette)
|
||||
|
||||
# struct n64_palette *n64texconv_palette_from_bin(void *data, size_t count, int fmt);
|
||||
ln64texconv.n64texconv_palette_from_bin.argtypes = [c_void_p, c_size_t, c_int]
|
||||
ln64texconv.n64texconv_palette_from_bin.restype = POINTER(N64Palette)
|
||||
|
||||
# int n64texconv_palette_to_png(const char *outpath, struct n64_palette *pal);
|
||||
ln64texconv.n64texconv_palette_to_png.argtypes = [c_char_p, POINTER(N64Palette)]
|
||||
ln64texconv.n64texconv_palette_to_png.restype = c_int
|
||||
|
||||
# void *n64texconv_palette_to_bin(struct n64_palette *pal, bool pad_to_8b);
|
||||
ln64texconv.n64texconv_palette_to_bin.argtypes = [POINTER(N64Palette), c_bool]
|
||||
ln64texconv.n64texconv_palette_to_bin.restype = c_void_p
|
||||
|
||||
# int n64texconv_palette_to_c(char **out, size_t *size_out, struct n64_palette *pal, bool pad_to_8b, unsigned int byte_width);
|
||||
ln64texconv.n64texconv_palette_to_c.argtypes = [POINTER(c_char_p), POINTER(c_size_t), POINTER(N64Palette), c_bool, c_uint]
|
||||
ln64texconv.n64texconv_palette_to_c.restype = c_int
|
||||
|
||||
# int n64texconv_palette_to_c_file(const char *out_path, struct n64_palette *pal, bool pad_to_8b, unsigned int byte_width);
|
||||
ln64texconv.n64texconv_palette_to_c_file.argtypes = [c_char_p, POINTER(N64Palette), c_bool, c_uint]
|
||||
ln64texconv.n64texconv_palette_to_c_file.restype = c_int
|
||||
|
||||
# struct n64_image
|
||||
class N64Image(Structure):
|
||||
_fields_ = [
|
||||
("width", c_size_t),
|
||||
("height", c_size_t),
|
||||
("fmt", c_int),
|
||||
("siz", c_int),
|
||||
("pal", POINTER(N64Palette)),
|
||||
("texels", POINTER(Color)),
|
||||
("color_indices", POINTER(c_uint8)),
|
||||
]
|
||||
|
||||
def get_palette(self) -> Optional[N64Palette]:
|
||||
return deref(self.pal)
|
||||
|
||||
@staticmethod
|
||||
def new(width : int, height : int, fmt : int, siz : int, pal : N64Palette = None) -> Optional["N64Image"]:
|
||||
if not any((fmt, siz) == fmtsiz for fmtsiz in VALID_FORMAT_COMBINATIONS):
|
||||
raise ValueError(f"Invalid fmt/siz combination ({fmt_name(fmt)}, {siz_name(siz)})")
|
||||
if pal is not None:
|
||||
_object_refcount.add_ref(byref(pal))
|
||||
return deref(ln64texconv.n64texconv_image_new(width, height, fmt, siz, pal))
|
||||
|
||||
def __del__(self):
|
||||
ln64texconv.n64texconv_image_free(byref(self))
|
||||
# Also free the palette if the reference count drops to 0
|
||||
_object_refcount.rm_ref(self.pal, ln64texconv.n64texconv_palette_free)
|
||||
|
||||
def copy(self) -> Optional["N64Image"]:
|
||||
_object_refcount.add_ref(self.pal)
|
||||
return deref(ln64texconv.n64texconv_image_copy(byref(self)))
|
||||
|
||||
@staticmethod
|
||||
def from_png(path : str, fmt : int, siz : int, pal_fmt : int = FMT_NONE) -> Optional["N64Image"]:
|
||||
if not os.path.isfile(path):
|
||||
raise ValueError(f"Cannot open \"{path}\", is not a file")
|
||||
if not any((fmt, siz) == fmtsiz for fmtsiz in VALID_FORMAT_COMBINATIONS):
|
||||
raise ValueError(f"Invalid fmt/siz combination ({fmt_name(fmt)}, {siz_name(siz)})")
|
||||
if fmt == G_IM_FMT_CI and pal_fmt not in (G_IM_FMT_RGBA, G_IM_FMT_IA):
|
||||
raise ValueError(f"Invalid palette format {fmt_name(pal_fmt)}, must be either G_IM_FMT_RGBA or G_IM_FMT_IA")
|
||||
img = deref(ln64texconv.n64texconv_image_from_png(path.encode("utf-8"), fmt, siz, pal_fmt))
|
||||
_object_refcount.add_ref(img.pal)
|
||||
return img
|
||||
|
||||
@staticmethod
|
||||
def from_bin(data : bytes | memoryview, width : int, height : int, fmt : int, siz : int, pal : Optional[N64Palette] = None,
|
||||
preswapped : bool = False) -> Optional["N64Image"]:
|
||||
if not any((fmt, siz) == fmtsiz for fmtsiz in VALID_FORMAT_COMBINATIONS):
|
||||
raise ValueError(f"Invalid fmt/siz combination ({fmt_name(fmt)}, {siz_name(siz)})")
|
||||
expected_size = texel_size_bytes(width * height, siz)
|
||||
if len(data) < expected_size:
|
||||
raise ValueError(f"Not enough data to extract the specified image. " +
|
||||
f"Expected at least 0x{expected_size:X} bytes but only got 0x{len(data):X} bytes")
|
||||
buffer = (c_uint8 * len(data)).from_buffer_copy(data)
|
||||
if pal:
|
||||
pal = byref(pal)
|
||||
_object_refcount.add_ref(pal)
|
||||
img = ln64texconv.n64texconv_image_from_bin(buffer, width, height, fmt, siz, pal, preswapped)
|
||||
return deref(img)
|
||||
|
||||
def reformat(self, fmt : int, siz : int, pal : Optional[N64Palette] = None) -> Optional["N64Image"]:
|
||||
if not any((fmt, siz) == fmtsiz for fmtsiz in VALID_FORMAT_COMBINATIONS):
|
||||
raise ValueError(f"Invalid fmt/siz combination ({fmt_name(fmt)}, {siz_name(siz)})")
|
||||
if pal:
|
||||
pal = byref(pal)
|
||||
_object_refcount.add_ref(pal)
|
||||
return deref(ln64texconv.n64texconv_image_reformat(byref(self), fmt, siz, pal))
|
||||
|
||||
def to_png(self, outpath : str, intensity_alpha : bool) -> bool:
|
||||
return ln64texconv.n64texconv_image_to_png(outpath.encode("utf-8"), byref(self), intensity_alpha) == 0
|
||||
|
||||
def to_bin(self, pad_to_8b : bool, preswap : bool) -> Optional[bytes]:
|
||||
nbytes = texel_size_bytes(self.width * self.height, self.siz)
|
||||
if pad_to_8b:
|
||||
nbytes = (nbytes + 7) & ~7
|
||||
ptr = ln64texconv.n64texconv_image_to_bin(byref(self), pad_to_8b, preswap)
|
||||
if not ptr:
|
||||
return None
|
||||
data = ctypes_pointer_to_bytes(ptr, nbytes)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return data
|
||||
|
||||
def to_c(self, pad_to_8b : bool, preswap : bool, byte_width : int) -> Optional[str]:
|
||||
ptr = c_char_p(None)
|
||||
size = c_size_t(0)
|
||||
if ln64texconv.n64texconv_image_to_c(byref(ptr), byref(size), byref(self), pad_to_8b, preswap, byte_width) != 0:
|
||||
return None
|
||||
s = ctypes_buffer_to_string(ptr)
|
||||
ln64texconv.n64texconv_free(ptr)
|
||||
return s
|
||||
|
||||
def to_c_file(self, out_path : str, pad_to_8b : bool, preswap : bool, byte_width : int) -> bool:
|
||||
return ln64texconv.n64texconv_image_to_c_file(out_path.encode("utf-8"), byref(self), pad_to_8b, preswap, byte_width) == 0
|
||||
|
||||
def png_extension(self) -> str:
|
||||
return ln64texconv.n64texconv_png_extension(byref(self)).decode("utf-8")
|
||||
|
||||
# struct n64_image *n64texconv_image_new(size_t width, size_t height, int fmt, int siz, struct n64_palette *pal);
|
||||
ln64texconv.n64texconv_image_new.argtypes = [c_size_t, c_size_t, c_int, c_int, POINTER(N64Palette)]
|
||||
ln64texconv.n64texconv_image_new.restype = POINTER(N64Image)
|
||||
|
||||
# void n64texconv_image_free(struct n64_image *img);
|
||||
ln64texconv.n64texconv_image_free.argtypes = [POINTER(N64Image)]
|
||||
ln64texconv.n64texconv_image_free.restype = None
|
||||
|
||||
# struct n64_image *n64texconv_image_copy(struct n64_image *img);
|
||||
ln64texconv.n64texconv_image_copy.argtypes = [POINTER(N64Image)]
|
||||
ln64texconv.n64texconv_image_copy.restype = POINTER(N64Image)
|
||||
|
||||
# struct n64_image *n64texconv_image_from_png(const char *path, int fmt, int siz, int pal_fmt);
|
||||
ln64texconv.n64texconv_image_from_png.argtypes = [c_char_p, c_int, c_int, c_int]
|
||||
ln64texconv.n64texconv_image_from_png.restype = POINTER(N64Image)
|
||||
|
||||
# struct n64_image *n64texconv_image_from_bin(void *data, size_t width, size_t height, int fmt, int siz, struct n64_palette *pal, bool preswapped);
|
||||
ln64texconv.n64texconv_image_from_bin.argtypes = [c_void_p, c_size_t, c_size_t, c_int, c_int, POINTER(N64Palette), c_bool]
|
||||
ln64texconv.n64texconv_image_from_bin.restype = POINTER(N64Image)
|
||||
|
||||
# struct n64_image *n64texconv_image_reformat(struct n64_image *img, int fmt, int siz, struct n64_palette *pal);
|
||||
ln64texconv.n64texconv_image_reformat.argtypes = [POINTER(N64Image), c_int, c_int, POINTER(N64Palette)]
|
||||
ln64texconv.n64texconv_image_reformat.restype = POINTER(N64Image)
|
||||
|
||||
# int n64texconv_image_to_png(const char *outpath, struct n64_image *img, bool intensity_alpha);
|
||||
ln64texconv.n64texconv_image_to_png.argtypes = [c_char_p, POINTER(N64Image), c_bool]
|
||||
ln64texconv.n64texconv_image_to_png.restype = c_int
|
||||
|
||||
# void *n64texconv_image_to_bin(struct n64_image *img, bool pad_to_8b, bool preswap);
|
||||
ln64texconv.n64texconv_image_to_bin.argtypes = [POINTER(N64Image), c_bool, c_bool]
|
||||
ln64texconv.n64texconv_image_to_bin.restype = c_void_p
|
||||
|
||||
# int n64texconv_image_to_c(char **out, size_t *size_out, struct n64_image *img, bool pad_to_8b, bool preswap, unsigned int byte_width);
|
||||
ln64texconv.n64texconv_image_to_c.argtypes = [POINTER(c_char_p), POINTER(c_size_t), POINTER(N64Image), c_bool, c_bool, c_uint]
|
||||
ln64texconv.n64texconv_image_to_c.restype = c_int
|
||||
|
||||
# int n64texconv_image_to_c_file(const char *out_path, struct n64_image *img, bool pad_to_8b, bool preswap, unsigned int byte_width);
|
||||
ln64texconv.n64texconv_image_to_c_file.argtypes = [c_char_p, POINTER(N64Image), c_bool, c_bool, c_uint]
|
||||
ln64texconv.n64texconv_image_to_c_file.restype = c_int
|
||||
|
||||
# const char *n64texconv_png_extension(struct n64_image *img);
|
||||
ln64texconv.n64texconv_png_extension.argtypes = [POINTER(N64Image)]
|
||||
ln64texconv.n64texconv_png_extension.restype = c_char_p
|
641
tools/assets/n64texconv/lib/libimagequant/COPYRIGHT
Normal file
641
tools/assets/n64texconv/lib/libimagequant/COPYRIGHT
Normal file
|
@ -0,0 +1,641 @@
|
|||
|
||||
libimagequant is derived from code by Jef Poskanzer and Greg Roelofs
|
||||
licensed under pngquant's original license (at the end of this file),
|
||||
and contains extensive changes and additions by Kornel Lesiński
|
||||
licensed under GPL v3 or later.
|
||||
|
||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
libimagequant © 2009-2018 by Kornel Lesiński.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
© 1989, 1991 by Jef Poskanzer.
|
||||
© 1997, 2000, 2002 by Greg Roelofs.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software and its
|
||||
documentation for any purpose and without fee is hereby granted, provided
|
||||
that the above copyright notice appear in all copies and that both that
|
||||
copyright notice and this permission notice appear in supporting
|
||||
documentation. This software is provided "as is" without express or
|
||||
implied warranty.
|
132
tools/assets/n64texconv/lib/libimagequant/blur.c
Normal file
132
tools/assets/n64texconv/lib/libimagequant/blur.c
Normal file
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
© 2011-2015 by Kornel Lesiński.
|
||||
|
||||
This file is part of libimagequant.
|
||||
|
||||
libimagequant 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.
|
||||
|
||||
libimagequant 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 libimagequant. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "blur.h"
|
||||
|
||||
/*
|
||||
Blurs image horizontally (width 2*size+1) and writes it transposed to dst (called twice gives 2d blur)
|
||||
*/
|
||||
static void transposing_1d_blur(unsigned char *restrict src, unsigned char *restrict dst, unsigned int width, unsigned int height, const unsigned int size)
|
||||
{
|
||||
assert(size > 0);
|
||||
|
||||
for(unsigned int j=0; j < height; j++) {
|
||||
unsigned char *restrict row = src + j*width;
|
||||
|
||||
// accumulate sum for pixels outside line
|
||||
unsigned int sum;
|
||||
sum = row[0]*size;
|
||||
for(unsigned int i=0; i < size; i++) {
|
||||
sum += row[i];
|
||||
}
|
||||
|
||||
// blur with left side outside line
|
||||
for(unsigned int i=0; i < size; i++) {
|
||||
sum -= row[0];
|
||||
sum += row[i+size];
|
||||
|
||||
dst[i*height + j] = sum / (size*2);
|
||||
}
|
||||
|
||||
for(unsigned int i=size; i < width-size; i++) {
|
||||
sum -= row[i-size];
|
||||
sum += row[i+size];
|
||||
|
||||
dst[i*height + j] = sum / (size*2);
|
||||
}
|
||||
|
||||
// blur with right side outside line
|
||||
for(unsigned int i=width-size; i < width; i++) {
|
||||
sum -= row[i-size];
|
||||
sum += row[width-1];
|
||||
|
||||
dst[i*height + j] = sum / (size*2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks maximum of neighboring pixels (blur + lighten)
|
||||
*/
|
||||
LIQ_PRIVATE void liq_max3(unsigned char *src, unsigned char *dst, unsigned int width, unsigned int height)
|
||||
{
|
||||
for(unsigned int j=0; j < height; j++) {
|
||||
const unsigned char *row = src + j*width,
|
||||
*prevrow = src + (j > 1 ? j-1 : 0)*width,
|
||||
*nextrow = src + MIN(height-1,j+1)*width;
|
||||
|
||||
unsigned char prev,curr=row[0],next=row[0];
|
||||
|
||||
for(unsigned int i=0; i < width-1; i++) {
|
||||
prev=curr;
|
||||
curr=next;
|
||||
next=row[i+1];
|
||||
|
||||
unsigned char t1 = MAX(prev,next);
|
||||
unsigned char t2 = MAX(nextrow[i],prevrow[i]);
|
||||
*dst++ = MAX(curr,MAX(t1,t2));
|
||||
}
|
||||
unsigned char t1 = MAX(curr,next);
|
||||
unsigned char t2 = MAX(nextrow[width-1],prevrow[width-1]);
|
||||
*dst++ = MAX(t1,t2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks minimum of neighboring pixels (blur + darken)
|
||||
*/
|
||||
LIQ_PRIVATE void liq_min3(unsigned char *src, unsigned char *dst, unsigned int width, unsigned int height)
|
||||
{
|
||||
for(unsigned int j=0; j < height; j++) {
|
||||
const unsigned char *row = src + j*width,
|
||||
*prevrow = src + (j > 1 ? j-1 : 0)*width,
|
||||
*nextrow = src + MIN(height-1,j+1)*width;
|
||||
|
||||
unsigned char prev,curr=row[0],next=row[0];
|
||||
|
||||
for(unsigned int i=0; i < width-1; i++) {
|
||||
prev=curr;
|
||||
curr=next;
|
||||
next=row[i+1];
|
||||
|
||||
unsigned char t1 = MIN(prev,next);
|
||||
unsigned char t2 = MIN(nextrow[i],prevrow[i]);
|
||||
*dst++ = MIN(curr,MIN(t1,t2));
|
||||
}
|
||||
unsigned char t1 = MIN(curr,next);
|
||||
unsigned char t2 = MIN(nextrow[width-1],prevrow[width-1]);
|
||||
*dst++ = MIN(t1,t2);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Filters src image and saves it to dst, overwriting tmp in the process.
|
||||
Image must be width*height pixels high. Size controls radius of box blur.
|
||||
*/
|
||||
LIQ_PRIVATE void liq_blur(unsigned char *src, unsigned char *tmp, unsigned char *dst, unsigned int width, unsigned int height, unsigned int size)
|
||||
{
|
||||
assert(size > 0);
|
||||
if (width < 2*size+1 || height < 2*size+1) {
|
||||
return;
|
||||
}
|
||||
transposing_1d_blur(src, tmp, width, height, size);
|
||||
transposing_1d_blur(tmp, dst, height, width, size);
|
||||
}
|
8
tools/assets/n64texconv/lib/libimagequant/blur.h
Normal file
8
tools/assets/n64texconv/lib/libimagequant/blur.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef BLUR_H
|
||||
#define BLUR_H
|
||||
|
||||
LIQ_PRIVATE void liq_blur(unsigned char *src, unsigned char *tmp, unsigned char *dst, unsigned int width, unsigned int height, unsigned int size);
|
||||
LIQ_PRIVATE void liq_max3(unsigned char *src, unsigned char *dst, unsigned int width, unsigned int height);
|
||||
LIQ_PRIVATE void liq_min3(unsigned char *src, unsigned char *dst, unsigned int width, unsigned int height);
|
||||
|
||||
#endif
|
119
tools/assets/n64texconv/lib/libimagequant/kmeans.c
Normal file
119
tools/assets/n64texconv/lib/libimagequant/kmeans.c
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
** © 2011-2016 by Kornel Lesiński.
|
||||
** See COPYRIGHT file for license.
|
||||
*/
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "kmeans.h"
|
||||
#include "nearest.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#else
|
||||
#define omp_get_max_threads() 1
|
||||
#define omp_get_thread_num() 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* K-Means iteration: new palette color is computed from weighted average of colors that map to that palette entry.
|
||||
*/
|
||||
LIQ_PRIVATE void kmeans_init(const colormap *map, const unsigned int max_threads, kmeans_state average_color[])
|
||||
{
|
||||
memset(average_color, 0, sizeof(average_color[0])*(KMEANS_CACHE_LINE_GAP+map->colors)*max_threads);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void kmeans_update_color(const f_pixel acolor, const float value, const colormap *map, unsigned int match, const unsigned int thread, kmeans_state average_color[])
|
||||
{
|
||||
match += thread * (KMEANS_CACHE_LINE_GAP+map->colors);
|
||||
average_color[match].a += acolor.a * value;
|
||||
average_color[match].r += acolor.r * value;
|
||||
average_color[match].g += acolor.g * value;
|
||||
average_color[match].b += acolor.b * value;
|
||||
average_color[match].total += value;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void kmeans_finalize(colormap *map, const unsigned int max_threads, const kmeans_state average_color[])
|
||||
{
|
||||
for (unsigned int i=0; i < map->colors; i++) {
|
||||
double a=0, r=0, g=0, b=0, total=0;
|
||||
|
||||
// Aggregate results from all threads
|
||||
for(unsigned int t=0; t < max_threads; t++) {
|
||||
const unsigned int offset = (KMEANS_CACHE_LINE_GAP+map->colors) * t + i;
|
||||
|
||||
a += average_color[offset].a;
|
||||
r += average_color[offset].r;
|
||||
g += average_color[offset].g;
|
||||
b += average_color[offset].b;
|
||||
total += average_color[offset].total;
|
||||
}
|
||||
|
||||
if (!map->palette[i].fixed) {
|
||||
map->palette[i].popularity = total;
|
||||
if (total) {
|
||||
map->palette[i].acolor = (f_pixel){
|
||||
.a = a / total,
|
||||
.r = r / total,
|
||||
.g = g / total,
|
||||
.b = b / total,
|
||||
};
|
||||
} else {
|
||||
// if a color is useless, make a new one
|
||||
// (it was supposed to be random, but Android NDK has problematic stdlib headers)
|
||||
map->palette[i].acolor.a = map->palette[(i+1)%map->colors].acolor.a;
|
||||
map->palette[i].acolor.r = map->palette[(i+2)%map->colors].acolor.r;
|
||||
map->palette[i].acolor.g = map->palette[(i+3)%map->colors].acolor.g;
|
||||
map->palette[i].acolor.b = map->palette[(i+4)%map->colors].acolor.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LIQ_PRIVATE double kmeans_do_iteration(histogram *hist, colormap *const map, kmeans_callback callback, unsigned int max_threads)
|
||||
{
|
||||
LIQ_ARRAY(kmeans_state, average_color, (KMEANS_CACHE_LINE_GAP+map->colors) * max_threads);
|
||||
kmeans_init(map, max_threads, average_color);
|
||||
struct nearest_map *const n = nearest_init(map);
|
||||
hist_item *const achv = hist->achv;
|
||||
const int hist_size = hist->size;
|
||||
|
||||
double total_diff=0;
|
||||
#if __GNUC__ >= 9 || __clang__
|
||||
#pragma omp parallel for if (hist_size > 2000) \
|
||||
schedule(static) default(none) shared(achv,average_color,callback,hist_size,map,n) reduction(+:total_diff)
|
||||
#else
|
||||
#pragma omp parallel for if (hist_size > 2000) \
|
||||
schedule(static) default(none) shared(average_color,callback) reduction(+:total_diff)
|
||||
#endif
|
||||
for(int j=0; j < hist_size; j++) {
|
||||
float diff;
|
||||
const f_pixel px = achv[j].acolor;
|
||||
const unsigned int match = nearest_search(n, &px, achv[j].tmp.likely_colormap_index, &diff);
|
||||
achv[j].tmp.likely_colormap_index = match;
|
||||
|
||||
if (callback) {
|
||||
// Check how average diff would look like if there was dithering
|
||||
const f_pixel remapped = map->palette[match].acolor;
|
||||
nearest_search(n, &(f_pixel){
|
||||
.a = px.a + px.a - remapped.a,
|
||||
.r = px.r + px.r - remapped.r,
|
||||
.g = px.g + px.g - remapped.g,
|
||||
.b = px.b + px.b - remapped.b,
|
||||
}, match, &diff);
|
||||
|
||||
callback(&achv[j], diff);
|
||||
}
|
||||
|
||||
total_diff += diff * achv[j].perceptual_weight;
|
||||
|
||||
kmeans_update_color(px, achv[j].adjusted_weight, map, match, omp_get_thread_num(), average_color);
|
||||
}
|
||||
|
||||
nearest_free(n);
|
||||
kmeans_finalize(map, max_threads, average_color);
|
||||
|
||||
return total_diff / hist->total_perceptual_weight;
|
||||
}
|
18
tools/assets/n64texconv/lib/libimagequant/kmeans.h
Normal file
18
tools/assets/n64texconv/lib/libimagequant/kmeans.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef KMEANS_H
|
||||
#define KMEANS_H
|
||||
|
||||
// Spread memory touched by different threads at least 64B apart which I assume is the cache line size. This should avoid memory write contention.
|
||||
#define KMEANS_CACHE_LINE_GAP ((64+sizeof(kmeans_state)-1)/sizeof(kmeans_state))
|
||||
|
||||
typedef struct {
|
||||
double a, r, g, b, total;
|
||||
} kmeans_state;
|
||||
|
||||
typedef void (*kmeans_callback)(hist_item *item, float diff);
|
||||
|
||||
LIQ_PRIVATE void kmeans_init(const colormap *map, const unsigned int max_threads, kmeans_state state[]);
|
||||
LIQ_PRIVATE void kmeans_update_color(const f_pixel acolor, const float value, const colormap *map, unsigned int match, const unsigned int thread, kmeans_state average_color[]);
|
||||
LIQ_PRIVATE void kmeans_finalize(colormap *map, const unsigned int max_threads, const kmeans_state state[]);
|
||||
LIQ_PRIVATE double kmeans_do_iteration(histogram *hist, colormap *const map, kmeans_callback callback, const unsigned int max_threads);
|
||||
|
||||
#endif
|
1814
tools/assets/n64texconv/lib/libimagequant/libimagequant.c
Normal file
1814
tools/assets/n64texconv/lib/libimagequant/libimagequant.c
Normal file
File diff suppressed because it is too large
Load diff
151
tools/assets/n64texconv/lib/libimagequant/libimagequant.h
Normal file
151
tools/assets/n64texconv/lib/libimagequant/libimagequant.h
Normal file
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* https://pngquant.org
|
||||
*/
|
||||
|
||||
#ifndef LIBIMAGEQUANT_H
|
||||
#define LIBIMAGEQUANT_H
|
||||
|
||||
#ifdef IMAGEQUANT_EXPORTS
|
||||
#define LIQ_EXPORT __declspec(dllexport)
|
||||
#endif
|
||||
|
||||
#ifndef LIQ_EXPORT
|
||||
#define LIQ_EXPORT extern
|
||||
#endif
|
||||
|
||||
#define LIQ_VERSION 21800
|
||||
#define LIQ_VERSION_STRING "2.18.0"
|
||||
|
||||
#ifndef LIQ_PRIVATE
|
||||
#if defined(__GNUC__) || defined (__llvm__)
|
||||
#define LIQ_PRIVATE __attribute__((visibility("hidden")))
|
||||
#define LIQ_NONNULL __attribute__((nonnull))
|
||||
#define LIQ_USERESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define LIQ_PRIVATE
|
||||
#define LIQ_NONNULL
|
||||
#define LIQ_USERESULT
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct liq_attr liq_attr;
|
||||
typedef struct liq_image liq_image;
|
||||
typedef struct liq_result liq_result;
|
||||
typedef struct liq_histogram liq_histogram;
|
||||
|
||||
typedef struct liq_color {
|
||||
unsigned char r, g, b, a;
|
||||
} liq_color;
|
||||
|
||||
typedef struct liq_palette {
|
||||
unsigned int count;
|
||||
liq_color entries[256];
|
||||
} liq_palette;
|
||||
|
||||
typedef enum liq_error {
|
||||
LIQ_OK = 0,
|
||||
LIQ_QUALITY_TOO_LOW = 99,
|
||||
LIQ_VALUE_OUT_OF_RANGE = 100,
|
||||
LIQ_OUT_OF_MEMORY,
|
||||
LIQ_ABORTED,
|
||||
LIQ_BITMAP_NOT_AVAILABLE,
|
||||
LIQ_BUFFER_TOO_SMALL,
|
||||
LIQ_INVALID_POINTER,
|
||||
LIQ_UNSUPPORTED,
|
||||
} liq_error;
|
||||
|
||||
enum liq_ownership {
|
||||
LIQ_OWN_ROWS=4,
|
||||
LIQ_OWN_PIXELS=8,
|
||||
LIQ_COPY_PIXELS=16,
|
||||
};
|
||||
|
||||
typedef struct liq_histogram_entry {
|
||||
liq_color color;
|
||||
unsigned int count;
|
||||
} liq_histogram_entry;
|
||||
|
||||
LIQ_EXPORT LIQ_USERESULT liq_attr* liq_attr_create(void);
|
||||
LIQ_EXPORT LIQ_USERESULT liq_attr* liq_attr_create_with_allocator(void* (*malloc)(size_t), void (*free)(void*));
|
||||
LIQ_EXPORT LIQ_USERESULT liq_attr* liq_attr_copy(const liq_attr *orig) LIQ_NONNULL;
|
||||
LIQ_EXPORT void liq_attr_destroy(liq_attr *attr) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT LIQ_USERESULT liq_histogram* liq_histogram_create(const liq_attr* attr);
|
||||
LIQ_EXPORT liq_error liq_histogram_add_image(liq_histogram *hist, const liq_attr *attr, liq_image* image) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_histogram_add_colors(liq_histogram *hist, const liq_attr *attr, const liq_histogram_entry entries[], int num_entries, double gamma) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_histogram_add_fixed_color(liq_histogram *hist, liq_color color, double gamma) LIQ_NONNULL;
|
||||
LIQ_EXPORT void liq_histogram_destroy(liq_histogram *hist) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT liq_error liq_set_max_colors(liq_attr* attr, int colors) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_max_colors(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_set_speed(liq_attr* attr, int speed) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_speed(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_set_min_opacity(liq_attr* attr, int min) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_min_opacity(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_set_min_posterization(liq_attr* attr, int bits) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_min_posterization(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_set_quality(liq_attr* attr, int minimum, int maximum) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_min_quality(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_get_max_quality(const liq_attr* attr) LIQ_NONNULL;
|
||||
LIQ_EXPORT void liq_set_last_index_transparent(liq_attr* attr, int is_last) LIQ_NONNULL;
|
||||
|
||||
typedef void liq_log_callback_function(const liq_attr*, const char *message, void* user_info);
|
||||
typedef void liq_log_flush_callback_function(const liq_attr*, void* user_info);
|
||||
LIQ_EXPORT void liq_set_log_callback(liq_attr*, liq_log_callback_function*, void* user_info);
|
||||
LIQ_EXPORT void liq_set_log_flush_callback(liq_attr*, liq_log_flush_callback_function*, void* user_info);
|
||||
|
||||
typedef int liq_progress_callback_function(float progress_percent, void* user_info);
|
||||
LIQ_EXPORT void liq_attr_set_progress_callback(liq_attr*, liq_progress_callback_function*, void* user_info);
|
||||
LIQ_EXPORT void liq_result_set_progress_callback(liq_result*, liq_progress_callback_function*, void* user_info);
|
||||
|
||||
// The rows and their data are not modified. The type of `rows` is non-const only due to a bug in C's typesystem design.
|
||||
LIQ_EXPORT LIQ_USERESULT liq_image *liq_image_create_rgba_rows(const liq_attr *attr, void *const rows[], int width, int height, double gamma) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT liq_image *liq_image_create_rgba(const liq_attr *attr, const void *bitmap, int width, int height, double gamma) LIQ_NONNULL;
|
||||
|
||||
typedef void liq_image_get_rgba_row_callback(liq_color row_out[], int row, int width, void* user_info);
|
||||
LIQ_EXPORT LIQ_USERESULT liq_image *liq_image_create_custom(const liq_attr *attr, liq_image_get_rgba_row_callback *row_callback, void* user_info, int width, int height, double gamma);
|
||||
|
||||
LIQ_EXPORT liq_error liq_image_set_memory_ownership(liq_image *image, int ownership_flags) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_image_set_background(liq_image *img, liq_image *background_image) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_image_set_importance_map(liq_image *img, unsigned char buffer[], size_t buffer_size, enum liq_ownership memory_handling) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_image_add_fixed_color(liq_image *img, liq_color color) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_image_get_width(const liq_image *img) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT int liq_image_get_height(const liq_image *img) LIQ_NONNULL;
|
||||
LIQ_EXPORT void liq_image_destroy(liq_image *img) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT LIQ_USERESULT liq_error liq_histogram_quantize(liq_histogram *const input_hist, liq_attr *const options, liq_result **result_output) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT liq_error liq_image_quantize(liq_image *const input_image, liq_attr *const options, liq_result **result_output) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT liq_error liq_set_dithering_level(liq_result *res, float dither_level) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_set_output_gamma(liq_result* res, double gamma) LIQ_NONNULL;
|
||||
LIQ_EXPORT LIQ_USERESULT double liq_get_output_gamma(const liq_result *result) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT LIQ_USERESULT const liq_palette *liq_get_palette(liq_result *result) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT liq_error liq_write_remapped_image(liq_result *result, liq_image *input_image, void *buffer, size_t buffer_size) LIQ_NONNULL;
|
||||
LIQ_EXPORT liq_error liq_write_remapped_image_rows(liq_result *result, liq_image *input_image, unsigned char **row_pointers) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT double liq_get_quantization_error(const liq_result *result) LIQ_NONNULL;
|
||||
LIQ_EXPORT int liq_get_quantization_quality(const liq_result *result) LIQ_NONNULL;
|
||||
LIQ_EXPORT double liq_get_remapping_error(const liq_result *result) LIQ_NONNULL;
|
||||
LIQ_EXPORT int liq_get_remapping_quality(const liq_result *result) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT void liq_result_destroy(liq_result *) LIQ_NONNULL;
|
||||
|
||||
LIQ_EXPORT int liq_version(void);
|
||||
|
||||
|
||||
// Deprecated
|
||||
LIQ_EXPORT LIQ_USERESULT liq_result *liq_quantize_image(liq_attr *options, liq_image *input_image) LIQ_NONNULL;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,50 @@
|
|||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#define LIQ_TEMP_ROW_WIDTH(img_width) (((img_width) | 15) + 1) /* keep alignment & leave space between rows to avoid cache line contention */
|
||||
#else
|
||||
#define LIQ_TEMP_ROW_WIDTH(img_width) (img_width)
|
||||
#define omp_get_max_threads() 1
|
||||
#define omp_get_thread_num() 0
|
||||
#endif
|
||||
|
||||
struct liq_image {
|
||||
const char *magic_header;
|
||||
void* (*malloc)(size_t);
|
||||
void (*free)(void*);
|
||||
|
||||
f_pixel *f_pixels;
|
||||
liq_color **rows;
|
||||
double gamma;
|
||||
unsigned int width, height;
|
||||
unsigned char *importance_map, *edges, *dither_map;
|
||||
liq_color *pixels, *temp_row;
|
||||
f_pixel *temp_f_row;
|
||||
liq_image_get_rgba_row_callback *row_callback;
|
||||
void *row_callback_user_info;
|
||||
liq_image *background;
|
||||
f_pixel fixed_colors[256];
|
||||
unsigned short fixed_colors_count;
|
||||
bool free_pixels, free_rows, free_rows_internal;
|
||||
};
|
||||
|
||||
typedef struct liq_remapping_result {
|
||||
const char *magic_header;
|
||||
void* (*malloc)(size_t);
|
||||
void (*free)(void*);
|
||||
|
||||
unsigned char *pixels;
|
||||
colormap *palette;
|
||||
liq_progress_callback_function *progress_callback;
|
||||
void *progress_callback_user_info;
|
||||
|
||||
liq_palette int_palette;
|
||||
double gamma, palette_error;
|
||||
float dither_level;
|
||||
unsigned char use_dither_map;
|
||||
unsigned char progress_stage1;
|
||||
} liq_remapping_result;
|
||||
|
||||
|
||||
LIQ_PRIVATE bool liq_image_get_row_f_init(liq_image *img) LIQ_NONNULL;
|
||||
LIQ_PRIVATE const f_pixel *liq_image_get_row_f(liq_image *input_image, unsigned int row) LIQ_NONNULL;
|
||||
LIQ_PRIVATE bool liq_remap_progress(const liq_remapping_result *quant, const float percent) LIQ_NONNULL;
|
476
tools/assets/n64texconv/lib/libimagequant/mediancut.c
Normal file
476
tools/assets/n64texconv/lib/libimagequant/mediancut.c
Normal file
|
@ -0,0 +1,476 @@
|
|||
/*
|
||||
** © 2009-2018 by Kornel Lesiński.
|
||||
** © 1989, 1991 by Jef Poskanzer.
|
||||
** © 1997, 2000, 2002 by Greg Roelofs; based on an idea by Stefan Schneider.
|
||||
**
|
||||
** See COPYRIGHT file for license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "mediancut.h"
|
||||
|
||||
#define index_of_channel(ch) (offsetof(f_pixel,ch)/sizeof(float))
|
||||
|
||||
static f_pixel averagepixels(unsigned int clrs, const hist_item achv[]);
|
||||
|
||||
struct box {
|
||||
f_pixel color;
|
||||
f_pixel variance;
|
||||
double sum, total_error, max_error;
|
||||
unsigned int ind;
|
||||
unsigned int colors;
|
||||
};
|
||||
|
||||
/** Weighted per-channel variance of the box. It's used to decide which channel to split by */
|
||||
static f_pixel box_variance(const hist_item achv[], const struct box *box)
|
||||
{
|
||||
const f_pixel mean = box->color;
|
||||
double variancea=0, variancer=0, varianceg=0, varianceb=0;
|
||||
|
||||
for(unsigned int i = 0; i < box->colors; ++i) {
|
||||
const f_pixel px = achv[box->ind + i].acolor;
|
||||
double weight = achv[box->ind + i].adjusted_weight;
|
||||
variancea += (mean.a - px.a)*(mean.a - px.a)*weight;
|
||||
variancer += (mean.r - px.r)*(mean.r - px.r)*weight;
|
||||
varianceg += (mean.g - px.g)*(mean.g - px.g)*weight;
|
||||
varianceb += (mean.b - px.b)*(mean.b - px.b)*weight;
|
||||
}
|
||||
|
||||
return (f_pixel){
|
||||
.a = variancea,
|
||||
.r = variancer,
|
||||
.g = varianceg,
|
||||
.b = varianceb,
|
||||
};
|
||||
}
|
||||
|
||||
static double box_max_error(const hist_item achv[], const struct box *box)
|
||||
{
|
||||
f_pixel mean = box->color;
|
||||
double max_error = 0;
|
||||
|
||||
for(unsigned int i = 0; i < box->colors; ++i) {
|
||||
const double diff = colordifference(mean, achv[box->ind + i].acolor);
|
||||
if (diff > max_error) {
|
||||
max_error = diff;
|
||||
}
|
||||
}
|
||||
return max_error;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static double color_weight(f_pixel median, hist_item h);
|
||||
|
||||
static inline void hist_item_swap(hist_item *l, hist_item *r)
|
||||
{
|
||||
if (l != r) {
|
||||
hist_item t = *l;
|
||||
*l = *r;
|
||||
*r = t;
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static unsigned int qsort_pivot(const hist_item *const base, const unsigned int len);
|
||||
inline static unsigned int qsort_pivot(const hist_item *const base, const unsigned int len)
|
||||
{
|
||||
if (len < 32) {
|
||||
return len/2;
|
||||
}
|
||||
|
||||
const unsigned int aidx=8, bidx=len/2, cidx=len-1;
|
||||
const unsigned int a=base[aidx].tmp.sort_value, b=base[bidx].tmp.sort_value, c=base[cidx].tmp.sort_value;
|
||||
return (a < b) ? ((b < c) ? bidx : ((a < c) ? cidx : aidx ))
|
||||
: ((b > c) ? bidx : ((a < c) ? aidx : cidx ));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static unsigned int qsort_partition(hist_item *const base, const unsigned int len);
|
||||
inline static unsigned int qsort_partition(hist_item *const base, const unsigned int len)
|
||||
{
|
||||
unsigned int l = 1, r = len;
|
||||
if (len >= 8) {
|
||||
hist_item_swap(&base[0], &base[qsort_pivot(base,len)]);
|
||||
}
|
||||
|
||||
const unsigned int pivot_value = base[0].tmp.sort_value;
|
||||
while (l < r) {
|
||||
if (base[l].tmp.sort_value >= pivot_value) {
|
||||
l++;
|
||||
} else {
|
||||
while(l < --r && base[r].tmp.sort_value <= pivot_value) {}
|
||||
hist_item_swap(&base[l], &base[r]);
|
||||
}
|
||||
}
|
||||
l--;
|
||||
hist_item_swap(&base[0], &base[l]);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
/** quick select algorithm */
|
||||
static void hist_item_sort_range(hist_item base[], unsigned int len, unsigned int sort_start)
|
||||
{
|
||||
for(;;) {
|
||||
const unsigned int l = qsort_partition(base, len), r = l+1;
|
||||
|
||||
if (l > 0 && sort_start < l) {
|
||||
len = l;
|
||||
}
|
||||
else if (r < len && sort_start > r) {
|
||||
base += r; len -= r; sort_start -= r;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
/** sorts array to make sum of weights lower than halfvar one side, returns index of the edge between <halfvar and >halfvar parts of the set */
|
||||
static unsigned int hist_item_sort_halfvar(hist_item base[], unsigned int len, double halfvar)
|
||||
{
|
||||
unsigned int base_idx = 0; // track base-index
|
||||
do {
|
||||
const unsigned int l = qsort_partition(base, len), r = l+1;
|
||||
|
||||
// check if sum of left side is smaller than half,
|
||||
// if it is, then it doesn't need to be sorted
|
||||
double tmpsum = 0.;
|
||||
for(unsigned int t = 0; t <= l && tmpsum < halfvar; ++t) tmpsum += base[t].color_weight;
|
||||
|
||||
// the split is on the left part
|
||||
if (tmpsum >= halfvar) {
|
||||
if (l > 0) {
|
||||
len = l;
|
||||
continue;
|
||||
} else {
|
||||
// reached the end of left part
|
||||
return base_idx;
|
||||
}
|
||||
}
|
||||
// process the right part
|
||||
halfvar -= tmpsum;
|
||||
if (len > r) {
|
||||
base += r;
|
||||
base_idx += r;
|
||||
len -= r; // tail-recursive "call"
|
||||
} else {
|
||||
// reached the end of the right part
|
||||
return base_idx + len;
|
||||
}
|
||||
} while(1);
|
||||
}
|
||||
|
||||
static f_pixel get_median(const struct box *b, hist_item achv[]);
|
||||
|
||||
typedef struct {
|
||||
unsigned int chan; float variance;
|
||||
} channelvariance;
|
||||
|
||||
static int comparevariance(const void *ch1, const void *ch2)
|
||||
{
|
||||
return ((const channelvariance*)ch1)->variance > ((const channelvariance*)ch2)->variance ? -1 :
|
||||
(((const channelvariance*)ch1)->variance < ((const channelvariance*)ch2)->variance ? 1 : 0);
|
||||
}
|
||||
|
||||
/** Finds which channels need to be sorted first and preproceses achv for fast sort */
|
||||
static double prepare_sort(struct box *b, hist_item achv[])
|
||||
{
|
||||
/*
|
||||
** Sort dimensions by their variance, and then sort colors first by dimension with highest variance
|
||||
*/
|
||||
channelvariance channels[4] = {
|
||||
{index_of_channel(a), b->variance.a},
|
||||
{index_of_channel(r), b->variance.r},
|
||||
{index_of_channel(g), b->variance.g},
|
||||
{index_of_channel(b), b->variance.b},
|
||||
};
|
||||
|
||||
qsort(channels, 4, sizeof(channels[0]), comparevariance);
|
||||
|
||||
const unsigned int ind1 = b->ind;
|
||||
const unsigned int colors = b->colors;
|
||||
#if __GNUC__ >= 9 || __clang__
|
||||
#pragma omp parallel for if (colors > 25000) \
|
||||
schedule(static) default(none) shared(achv, channels, colors, ind1)
|
||||
#else
|
||||
#pragma omp parallel for if (colors > 25000) \
|
||||
schedule(static) default(none) shared(achv, channels)
|
||||
#endif
|
||||
for(unsigned int i=0; i < colors; i++) {
|
||||
const float *chans = (const float *)&achv[ind1 + i].acolor;
|
||||
// Only the first channel really matters. When trying median cut many times
|
||||
// with different histogram weights, I don't want sort randomness to influence outcome.
|
||||
achv[ind1 + i].tmp.sort_value = ((unsigned int)(chans[channels[0].chan]*65535.0)<<16) |
|
||||
(unsigned int)((chans[channels[2].chan] + chans[channels[1].chan]/2.0 + chans[channels[3].chan]/4.0)*65535.0);
|
||||
}
|
||||
|
||||
const f_pixel median = get_median(b, achv);
|
||||
|
||||
// box will be split to make color_weight of each side even
|
||||
const unsigned int ind = b->ind, end = ind+b->colors;
|
||||
double totalvar = 0;
|
||||
#pragma omp parallel for if (end - ind > 15000) \
|
||||
schedule(static) default(shared) reduction(+:totalvar)
|
||||
for(unsigned int j=ind; j < end; j++) totalvar += (achv[j].color_weight = color_weight(median, achv[j]));
|
||||
return totalvar / 2.0;
|
||||
}
|
||||
|
||||
/** finds median in unsorted set by sorting only minimum required */
|
||||
static f_pixel get_median(const struct box *b, hist_item achv[])
|
||||
{
|
||||
const unsigned int median_start = (b->colors-1)/2;
|
||||
|
||||
hist_item_sort_range(&(achv[b->ind]), b->colors,
|
||||
median_start);
|
||||
|
||||
if (b->colors&1) return achv[b->ind + median_start].acolor;
|
||||
|
||||
// technically the second color is not guaranteed to be sorted correctly
|
||||
// but most of the time it is good enough to be useful
|
||||
return averagepixels(2, &achv[b->ind + median_start]);
|
||||
}
|
||||
|
||||
/*
|
||||
** Find the best splittable box. -1 if no boxes are splittable.
|
||||
*/
|
||||
static int best_splittable_box(struct box bv[], unsigned int boxes, const double max_mse)
|
||||
{
|
||||
int bi=-1; double maxsum=0;
|
||||
for(unsigned int i=0; i < boxes; i++) {
|
||||
if (bv[i].colors < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// looks only at max variance, because it's only going to split by it
|
||||
const double cv = MAX(bv[i].variance.r, MAX(bv[i].variance.g,bv[i].variance.b));
|
||||
double thissum = bv[i].sum * MAX(bv[i].variance.a, cv);
|
||||
|
||||
if (bv[i].max_error > max_mse) {
|
||||
thissum = thissum* bv[i].max_error/max_mse;
|
||||
}
|
||||
|
||||
if (thissum > maxsum) {
|
||||
maxsum = thissum;
|
||||
bi = i;
|
||||
}
|
||||
}
|
||||
return bi;
|
||||
}
|
||||
|
||||
inline static double color_weight(f_pixel median, hist_item h)
|
||||
{
|
||||
float diff = colordifference(median, h.acolor);
|
||||
return sqrt(diff) * (sqrt(1.0+h.adjusted_weight)-1.0);
|
||||
}
|
||||
|
||||
static void set_colormap_from_boxes(colormap *map, struct box bv[], unsigned int boxes, hist_item *achv);
|
||||
static void adjust_histogram(hist_item *achv, const struct box bv[], unsigned int boxes);
|
||||
|
||||
static double box_error(const struct box *box, const hist_item achv[])
|
||||
{
|
||||
f_pixel avg = box->color;
|
||||
|
||||
double total_error=0;
|
||||
for (unsigned int i = 0; i < box->colors; ++i) {
|
||||
total_error += colordifference(avg, achv[box->ind + i].acolor) * achv[box->ind + i].perceptual_weight;
|
||||
}
|
||||
|
||||
return total_error;
|
||||
}
|
||||
|
||||
|
||||
static bool total_box_error_below_target(double target_mse, struct box bv[], unsigned int boxes, const histogram *hist)
|
||||
{
|
||||
target_mse *= hist->total_perceptual_weight;
|
||||
double total_error=0;
|
||||
|
||||
for(unsigned int i=0; i < boxes; i++) {
|
||||
// error is (re)calculated lazily
|
||||
if (bv[i].total_error >= 0) {
|
||||
total_error += bv[i].total_error;
|
||||
}
|
||||
if (total_error > target_mse) return false;
|
||||
}
|
||||
|
||||
for(unsigned int i=0; i < boxes; i++) {
|
||||
if (bv[i].total_error < 0) {
|
||||
bv[i].total_error = box_error(&bv[i], hist->achv);
|
||||
total_error += bv[i].total_error;
|
||||
}
|
||||
if (total_error > target_mse) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void box_init(struct box *box, const hist_item *achv, const unsigned int ind, const unsigned int colors, const double sum) {
|
||||
assert(colors > 0);
|
||||
assert(sum > 0);
|
||||
|
||||
box->ind = ind;
|
||||
box->colors = colors;
|
||||
box->sum = sum;
|
||||
box->total_error = -1;
|
||||
|
||||
box->color = averagepixels(colors, &achv[ind]);
|
||||
box->variance = box_variance(achv, box);
|
||||
box->max_error = box_max_error(achv, box);
|
||||
}
|
||||
|
||||
/*
|
||||
** Here is the fun part, the median-cut colormap generator. This is based
|
||||
** on Paul Heckbert's paper, "Color Image Quantization for Frame Buffer
|
||||
** Display," SIGGRAPH 1982 Proceedings, page 297.
|
||||
*/
|
||||
LIQ_PRIVATE colormap *mediancut(histogram *hist, unsigned int newcolors, const double target_mse, const double max_mse, void* (*malloc)(size_t), void (*free)(void*))
|
||||
{
|
||||
hist_item *achv = hist->achv;
|
||||
struct box bv[newcolors+16];
|
||||
|
||||
assert(hist->boxes[0].begin == 0);
|
||||
assert(hist->boxes[LIQ_MAXCLUSTER-1].end == hist->size);
|
||||
|
||||
unsigned int boxes = 0;
|
||||
for(int b=0; b < LIQ_MAXCLUSTER; b++) {
|
||||
int begin = hist->boxes[b].begin;
|
||||
int end = hist->boxes[b].end;
|
||||
if (begin == end) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (boxes >= newcolors/3) {
|
||||
boxes = 0;
|
||||
begin = 0;
|
||||
end = hist->boxes[LIQ_MAXCLUSTER-1].end;
|
||||
b = LIQ_MAXCLUSTER;
|
||||
}
|
||||
|
||||
double sum = 0;
|
||||
for(int i=begin; i < end; i++) {
|
||||
sum += achv[i].adjusted_weight;
|
||||
}
|
||||
box_init(&bv[boxes], achv, begin, end-begin, sum);
|
||||
boxes++;
|
||||
}
|
||||
|
||||
assert(boxes < newcolors);
|
||||
|
||||
|
||||
/*
|
||||
** Main loop: split boxes until we have enough.
|
||||
*/
|
||||
while (boxes < newcolors) {
|
||||
|
||||
// first splits boxes that exceed quality limit (to have colors for things like odd green pixel),
|
||||
// later raises the limit to allow large smooth areas/gradients get colors.
|
||||
const double current_max_mse = max_mse + (boxes/(double)newcolors)*16.0*max_mse;
|
||||
const int bi = best_splittable_box(bv, boxes, current_max_mse);
|
||||
if (bi < 0) {
|
||||
break; /* ran out of colors! */
|
||||
}
|
||||
|
||||
unsigned int indx = bv[bi].ind;
|
||||
unsigned int clrs = bv[bi].colors;
|
||||
|
||||
/*
|
||||
Classic implementation tries to get even number of colors or pixels in each subdivision.
|
||||
|
||||
Here, instead of popularity I use (sqrt(popularity)*variance) metric.
|
||||
Each subdivision balances number of pixels (popular colors) and low variance -
|
||||
boxes can be large if they have similar colors. Later boxes with high variance
|
||||
will be more likely to be split.
|
||||
|
||||
Median used as expected value gives much better results than mean.
|
||||
*/
|
||||
|
||||
const double halfvar = prepare_sort(&bv[bi], achv);
|
||||
|
||||
// hist_item_sort_halfvar sorts and sums lowervar at the same time
|
||||
// returns item to break at …minus one, which does smell like an off-by-one error.
|
||||
unsigned int break_at = hist_item_sort_halfvar(&achv[indx], clrs, halfvar);
|
||||
break_at = MIN(clrs-1, break_at + 1);
|
||||
|
||||
/*
|
||||
** Split the box.
|
||||
*/
|
||||
double sm = bv[bi].sum;
|
||||
double lowersum = 0;
|
||||
for(unsigned int i=0; i < break_at; i++) lowersum += achv[indx + i].adjusted_weight;
|
||||
|
||||
box_init(&bv[bi], achv, indx, break_at, lowersum);
|
||||
box_init(&bv[boxes], achv, indx + break_at, clrs - break_at, sm - lowersum);
|
||||
|
||||
++boxes;
|
||||
|
||||
if (total_box_error_below_target(target_mse, bv, boxes, hist)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
colormap *map = pam_colormap(boxes, malloc, free);
|
||||
set_colormap_from_boxes(map, bv, boxes, achv);
|
||||
|
||||
adjust_histogram(achv, bv, boxes);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
static void set_colormap_from_boxes(colormap *map, struct box* bv, unsigned int boxes, hist_item *achv)
|
||||
{
|
||||
/*
|
||||
** Ok, we've got enough boxes. Now choose a representative color for
|
||||
** each box. There are a number of possible ways to make this choice.
|
||||
** One would be to choose the center of the box; this ignores any structure
|
||||
** within the boxes. Another method would be to average all the colors in
|
||||
** the box - this is the method specified in Heckbert's paper.
|
||||
*/
|
||||
|
||||
for(unsigned int bi = 0; bi < boxes; ++bi) {
|
||||
map->palette[bi].acolor = bv[bi].color;
|
||||
|
||||
/* store total color popularity (perceptual_weight is approximation of it) */
|
||||
map->palette[bi].popularity = 0;
|
||||
for(unsigned int i=bv[bi].ind; i < bv[bi].ind+bv[bi].colors; i++) {
|
||||
map->palette[bi].popularity += achv[i].perceptual_weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* increase histogram popularity by difference from the final color (this is used as part of feedback loop) */
|
||||
static void adjust_histogram(hist_item *achv, const struct box* bv, unsigned int boxes)
|
||||
{
|
||||
for(unsigned int bi = 0; bi < boxes; ++bi) {
|
||||
for(unsigned int i=bv[bi].ind; i < bv[bi].ind+bv[bi].colors; i++) {
|
||||
achv[i].tmp.likely_colormap_index = bi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static f_pixel averagepixels(unsigned int clrs, const hist_item achv[])
|
||||
{
|
||||
double r = 0, g = 0, b = 0, a = 0, sum = 0;
|
||||
|
||||
#pragma omp parallel for if (clrs > 25000) \
|
||||
schedule(static) default(shared) reduction(+:a) reduction(+:r) reduction(+:g) reduction(+:b) reduction(+:sum)
|
||||
for(unsigned int i = 0; i < clrs; i++) {
|
||||
const f_pixel px = achv[i].acolor;
|
||||
const double weight = achv[i].adjusted_weight;
|
||||
|
||||
sum += weight;
|
||||
a += px.a * weight;
|
||||
r += px.r * weight;
|
||||
g += px.g * weight;
|
||||
b += px.b * weight;
|
||||
}
|
||||
|
||||
if (sum) {
|
||||
a /= sum;
|
||||
r /= sum;
|
||||
g /= sum;
|
||||
b /= sum;
|
||||
}
|
||||
|
||||
assert(!isnan(r) && !isnan(g) && !isnan(b) && !isnan(a));
|
||||
|
||||
return (f_pixel){.r=r, .g=g, .b=b, .a=a};
|
||||
}
|
6
tools/assets/n64texconv/lib/libimagequant/mediancut.h
Normal file
6
tools/assets/n64texconv/lib/libimagequant/mediancut.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef MEDIANCUT_H
|
||||
#define MEDIANCUT_H
|
||||
|
||||
LIQ_PRIVATE colormap *mediancut(histogram *hist, unsigned int newcolors, const double target_mse, const double max_mse, void* (*malloc)(size_t), void (*free)(void*));
|
||||
|
||||
#endif
|
70
tools/assets/n64texconv/lib/libimagequant/mempool.c
Normal file
70
tools/assets/n64texconv/lib/libimagequant/mempool.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
** © 2009-2017 by Kornel Lesiński.
|
||||
** © 1989, 1991 by Jef Poskanzer.
|
||||
** © 1997, 2000, 2002 by Greg Roelofs; based on an idea by Stefan Schneider.
|
||||
**
|
||||
** See COPYRIGHT file for license.
|
||||
*/
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "mempool.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define ALIGN_MASK 15UL
|
||||
#define MEMPOOL_RESERVED ((sizeof(struct mempool)+ALIGN_MASK) & ~ALIGN_MASK)
|
||||
|
||||
struct mempool {
|
||||
unsigned int used, size;
|
||||
void* (*malloc)(size_t);
|
||||
void (*free)(void*);
|
||||
struct mempool *next;
|
||||
};
|
||||
LIQ_PRIVATE void* mempool_create(mempoolptr *mptr, const unsigned int size, unsigned int max_size, void* (*malloc)(size_t), void (*free)(void*))
|
||||
{
|
||||
if (*mptr && ((*mptr)->used+size) <= (*mptr)->size) {
|
||||
unsigned int prevused = (*mptr)->used;
|
||||
(*mptr)->used += (size+15UL) & ~0xFUL;
|
||||
return ((char*)(*mptr)) + prevused;
|
||||
}
|
||||
|
||||
mempoolptr old = *mptr;
|
||||
if (!max_size) max_size = (1<<17);
|
||||
max_size = size+ALIGN_MASK > max_size ? size+ALIGN_MASK : max_size;
|
||||
|
||||
*mptr = malloc(MEMPOOL_RESERVED + max_size);
|
||||
if (!*mptr) return NULL;
|
||||
**mptr = (struct mempool){
|
||||
.malloc = malloc,
|
||||
.free = free,
|
||||
.size = MEMPOOL_RESERVED + max_size,
|
||||
.used = sizeof(struct mempool),
|
||||
.next = old,
|
||||
};
|
||||
uintptr_t mptr_used_start = (uintptr_t)(*mptr) + (*mptr)->used;
|
||||
(*mptr)->used += (ALIGN_MASK + 1 - (mptr_used_start & ALIGN_MASK)) & ALIGN_MASK; // reserve bytes required to make subsequent allocations aligned
|
||||
assert(!(((uintptr_t)(*mptr) + (*mptr)->used) & ALIGN_MASK));
|
||||
|
||||
return mempool_alloc(mptr, size, size);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void* mempool_alloc(mempoolptr *mptr, const unsigned int size, const unsigned int max_size)
|
||||
{
|
||||
if (((*mptr)->used+size) <= (*mptr)->size) {
|
||||
unsigned int prevused = (*mptr)->used;
|
||||
(*mptr)->used += (size + ALIGN_MASK) & ~ALIGN_MASK;
|
||||
return ((char*)(*mptr)) + prevused;
|
||||
}
|
||||
|
||||
return mempool_create(mptr, size, max_size, (*mptr)->malloc, (*mptr)->free);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void mempool_destroy(mempoolptr m)
|
||||
{
|
||||
while (m) {
|
||||
mempoolptr next = m->next;
|
||||
m->free(m);
|
||||
m = next;
|
||||
}
|
||||
}
|
13
tools/assets/n64texconv/lib/libimagequant/mempool.h
Normal file
13
tools/assets/n64texconv/lib/libimagequant/mempool.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef MEMPOOL_H
|
||||
#define MEMPOOL_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct mempool;
|
||||
typedef struct mempool *mempoolptr;
|
||||
|
||||
LIQ_PRIVATE void* mempool_create(mempoolptr *mptr, const unsigned int size, unsigned int capacity, void* (*malloc)(size_t), void (*free)(void*));
|
||||
LIQ_PRIVATE void* mempool_alloc(mempoolptr *mptr, const unsigned int size, const unsigned int capacity);
|
||||
LIQ_PRIVATE void mempool_destroy(mempoolptr m);
|
||||
|
||||
#endif
|
230
tools/assets/n64texconv/lib/libimagequant/nearest.c
Normal file
230
tools/assets/n64texconv/lib/libimagequant/nearest.c
Normal file
|
@ -0,0 +1,230 @@
|
|||
/*
|
||||
** © 2009-2015 by Kornel Lesiński.
|
||||
** © 1989, 1991 by Jef Poskanzer.
|
||||
** © 1997, 2000, 2002 by Greg Roelofs; based on an idea by Stefan Schneider.
|
||||
**
|
||||
** See COPYRIGHT file for license.
|
||||
*/
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "nearest.h"
|
||||
#include "mempool.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct vp_sort_tmp {
|
||||
float distance_squared;
|
||||
unsigned int idx;
|
||||
} vp_sort_tmp;
|
||||
|
||||
typedef struct vp_search_tmp {
|
||||
float distance;
|
||||
float distance_squared;
|
||||
unsigned int idx;
|
||||
int exclude;
|
||||
} vp_search_tmp;
|
||||
|
||||
struct leaf {
|
||||
f_pixel color;
|
||||
unsigned int idx;
|
||||
};
|
||||
|
||||
typedef struct vp_node {
|
||||
struct vp_node *near, *far;
|
||||
f_pixel vantage_point;
|
||||
float radius, radius_squared;
|
||||
struct leaf *rest;
|
||||
unsigned short idx;
|
||||
unsigned short restcount;
|
||||
} vp_node;
|
||||
|
||||
struct nearest_map {
|
||||
vp_node *root;
|
||||
const colormap_item *palette;
|
||||
float nearest_other_color_dist[256];
|
||||
mempoolptr mempool;
|
||||
};
|
||||
|
||||
static void vp_search_node(const vp_node *node, const f_pixel *const needle, vp_search_tmp *const best_candidate);
|
||||
|
||||
static int vp_compare_distance(const void *ap, const void *bp) {
|
||||
float a = ((const vp_sort_tmp*)ap)->distance_squared;
|
||||
float b = ((const vp_sort_tmp*)bp)->distance_squared;
|
||||
return a > b ? 1 : -1;
|
||||
}
|
||||
|
||||
static void vp_sort_indexes_by_distance(const f_pixel vantage_point, vp_sort_tmp indexes[], int num_indexes, const colormap_item items[]) {
|
||||
for(int i=0; i < num_indexes; i++) {
|
||||
indexes[i].distance_squared = colordifference(vantage_point, items[indexes[i].idx].acolor);
|
||||
}
|
||||
qsort(indexes, num_indexes, sizeof(indexes[0]), vp_compare_distance);
|
||||
}
|
||||
|
||||
/*
|
||||
* Usually it should pick farthest point, but picking most popular point seems to make search quicker anyway
|
||||
*/
|
||||
static int vp_find_best_vantage_point_index(vp_sort_tmp indexes[], int num_indexes, const colormap_item items[]) {
|
||||
int best = 0;
|
||||
float best_popularity = items[indexes[0].idx].popularity;
|
||||
for(int i = 1; i < num_indexes; i++) {
|
||||
if (items[indexes[i].idx].popularity > best_popularity) {
|
||||
best_popularity = items[indexes[i].idx].popularity;
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
static vp_node *vp_create_node(mempoolptr *m, vp_sort_tmp indexes[], int num_indexes, const colormap_item items[]) {
|
||||
if (num_indexes <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vp_node *node = mempool_alloc(m, sizeof(node[0]), 0);
|
||||
|
||||
if (num_indexes == 1) {
|
||||
*node = (vp_node){
|
||||
.vantage_point = items[indexes[0].idx].acolor,
|
||||
.idx = indexes[0].idx,
|
||||
.radius = MAX_DIFF,
|
||||
.radius_squared = MAX_DIFF,
|
||||
};
|
||||
return node;
|
||||
}
|
||||
|
||||
const int ref = vp_find_best_vantage_point_index(indexes, num_indexes, items);
|
||||
const int ref_idx = indexes[ref].idx;
|
||||
|
||||
// Removes the `ref_idx` item from remaining items, because it's included in the current node
|
||||
num_indexes -= 1;
|
||||
indexes[ref] = indexes[num_indexes];
|
||||
|
||||
vp_sort_indexes_by_distance(items[ref_idx].acolor, indexes, num_indexes, items);
|
||||
|
||||
// Remaining items are split by the median distance
|
||||
const int half_idx = num_indexes/2;
|
||||
|
||||
*node = (vp_node){
|
||||
.vantage_point = items[ref_idx].acolor,
|
||||
.idx = ref_idx,
|
||||
.radius = sqrtf(indexes[half_idx].distance_squared),
|
||||
.radius_squared = indexes[half_idx].distance_squared,
|
||||
};
|
||||
if (num_indexes < 7) {
|
||||
node->rest = mempool_alloc(m, sizeof(node->rest[0]) * num_indexes, 0);
|
||||
node->restcount = num_indexes;
|
||||
for(int i=0; i < num_indexes; i++) {
|
||||
node->rest[i].idx = indexes[i].idx;
|
||||
node->rest[i].color = items[indexes[i].idx].acolor;
|
||||
}
|
||||
} else {
|
||||
node->near = vp_create_node(m, indexes, half_idx, items);
|
||||
node->far = vp_create_node(m, &indexes[half_idx], num_indexes - half_idx, items);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE struct nearest_map *nearest_init(const colormap *map) {
|
||||
mempoolptr m = NULL;
|
||||
struct nearest_map *handle = mempool_create(&m, sizeof(handle[0]), sizeof(handle[0]) + sizeof(vp_node)*map->colors+16, map->malloc, map->free);
|
||||
|
||||
LIQ_ARRAY(vp_sort_tmp, indexes, map->colors);
|
||||
|
||||
for(unsigned int i=0; i < map->colors; i++) {
|
||||
indexes[i].idx = i;
|
||||
}
|
||||
|
||||
vp_node *root = vp_create_node(&m, indexes, map->colors, map->palette);
|
||||
*handle = (struct nearest_map){
|
||||
.root = root,
|
||||
.palette = map->palette,
|
||||
.mempool = m,
|
||||
};
|
||||
|
||||
for(unsigned int i=0; i < map->colors; i++) {
|
||||
vp_search_tmp best = {
|
||||
.distance = MAX_DIFF,
|
||||
.distance_squared = MAX_DIFF,
|
||||
.exclude = i,
|
||||
};
|
||||
vp_search_node(root, &map->palette[i].acolor, &best);
|
||||
handle->nearest_other_color_dist[i] = best.distance * best.distance / 4.f; // half of squared distance
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
static void vp_search_node(const vp_node *node, const f_pixel *const needle, vp_search_tmp *const best_candidate) {
|
||||
do {
|
||||
const float distance_squared = colordifference(node->vantage_point, *needle);
|
||||
const float distance = sqrtf(distance_squared);
|
||||
|
||||
if (distance_squared < best_candidate->distance_squared && best_candidate->exclude != node->idx) {
|
||||
best_candidate->distance = distance;
|
||||
best_candidate->distance_squared = distance_squared;
|
||||
best_candidate->idx = node->idx;
|
||||
}
|
||||
|
||||
if (node->restcount) {
|
||||
for(int i=0; i < node->restcount; i++) {
|
||||
const float distance_squared = colordifference(node->rest[i].color, *needle);
|
||||
if (distance_squared < best_candidate->distance_squared && best_candidate->exclude != node->rest[i].idx) {
|
||||
best_candidate->distance = sqrtf(distance_squared);
|
||||
best_candidate->distance_squared = distance_squared;
|
||||
best_candidate->idx = node->rest[i].idx;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Recurse towards most likely candidate first to narrow best candidate's distance as soon as possible
|
||||
if (distance_squared < node->radius_squared) {
|
||||
if (node->near) {
|
||||
vp_search_node(node->near, needle, best_candidate);
|
||||
}
|
||||
// The best node (final answer) may be just ouside the radius, but not farther than
|
||||
// the best distance we know so far. The vp_search_node above should have narrowed
|
||||
// best_candidate->distance, so this path is rarely taken.
|
||||
if (node->far && distance >= node->radius - best_candidate->distance) {
|
||||
node = node->far; // Fast tail recursion
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (node->far) {
|
||||
vp_search_node(node->far, needle, best_candidate);
|
||||
}
|
||||
if (node->near && distance <= node->radius + best_candidate->distance) {
|
||||
node = node->near; // Fast tail recursion
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} while(true);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE unsigned int nearest_search(const struct nearest_map *handle, const f_pixel *px, const int likely_colormap_index, float *diff) {
|
||||
const float guess_diff = colordifference(handle->palette[likely_colormap_index].acolor, *px);
|
||||
if (guess_diff < handle->nearest_other_color_dist[likely_colormap_index]) {
|
||||
if (diff) *diff = guess_diff;
|
||||
return likely_colormap_index;
|
||||
}
|
||||
|
||||
vp_search_tmp best_candidate = {
|
||||
.distance = sqrtf(guess_diff),
|
||||
.distance_squared = guess_diff,
|
||||
.idx = likely_colormap_index,
|
||||
.exclude = -1,
|
||||
};
|
||||
vp_search_node(handle->root, px, &best_candidate);
|
||||
if (diff) {
|
||||
*diff = best_candidate.distance * best_candidate.distance;
|
||||
}
|
||||
return best_candidate.idx;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void nearest_free(struct nearest_map *centroids)
|
||||
{
|
||||
mempool_destroy(centroids->mempool);
|
||||
}
|
14
tools/assets/n64texconv/lib/libimagequant/nearest.h
Normal file
14
tools/assets/n64texconv/lib/libimagequant/nearest.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
//
|
||||
// nearest.h
|
||||
// pngquant
|
||||
//
|
||||
|
||||
#ifndef NEAREST_H
|
||||
#define NEAREST_H
|
||||
|
||||
struct nearest_map;
|
||||
LIQ_PRIVATE struct nearest_map *nearest_init(const colormap *palette);
|
||||
LIQ_PRIVATE unsigned int nearest_search(const struct nearest_map *map, const f_pixel *px, const int palette_index_guess, float *diff);
|
||||
LIQ_PRIVATE void nearest_free(struct nearest_map *map);
|
||||
|
||||
#endif
|
351
tools/assets/n64texconv/lib/libimagequant/pam.c
Normal file
351
tools/assets/n64texconv/lib/libimagequant/pam.c
Normal file
|
@ -0,0 +1,351 @@
|
|||
/* pam.c - pam (portable alpha map) utility library
|
||||
**
|
||||
** © 2009-2017 by Kornel Lesiński.
|
||||
** © 1989, 1991 by Jef Poskanzer.
|
||||
** © 1997, 2000, 2002 by Greg Roelofs; based on an idea by Stefan Schneider.
|
||||
**
|
||||
** See COPYRIGHT file for license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "mempool.h"
|
||||
|
||||
LIQ_PRIVATE bool pam_computeacolorhash(struct acolorhash_table *acht, const liq_color *const pixels[], unsigned int cols, unsigned int rows, const unsigned char *importance_map)
|
||||
{
|
||||
const unsigned int ignorebits = acht->ignorebits;
|
||||
const unsigned int channel_mask = 255U>>ignorebits<<ignorebits;
|
||||
const unsigned int channel_hmask = (255U>>ignorebits) ^ 0xFFU;
|
||||
const unsigned int posterize_mask = channel_mask << 24 | channel_mask << 16 | channel_mask << 8 | channel_mask;
|
||||
const unsigned int posterize_high_mask = channel_hmask << 24 | channel_hmask << 16 | channel_hmask << 8 | channel_hmask;
|
||||
|
||||
const unsigned int hash_size = acht->hash_size;
|
||||
|
||||
/* Go through the entire image, building a hash table of colors. */
|
||||
for(unsigned int row = 0; row < rows; ++row) {
|
||||
|
||||
for(unsigned int col = 0; col < cols; ++col) {
|
||||
unsigned int boost;
|
||||
|
||||
// RGBA color is casted to long for easier hasing/comparisons
|
||||
union rgba_as_int px = {pixels[row][col]};
|
||||
unsigned int hash;
|
||||
if (px.rgba.a) {
|
||||
// mask posterizes all 4 channels in one go
|
||||
px.l = (px.l & posterize_mask) | ((px.l & posterize_high_mask) >> (8-ignorebits));
|
||||
// fancier hashing algorithms didn't improve much
|
||||
hash = px.l % hash_size;
|
||||
|
||||
if (importance_map) {
|
||||
boost = *importance_map++;
|
||||
} else {
|
||||
boost = 255;
|
||||
}
|
||||
} else {
|
||||
// "dirty alpha" has different RGBA values that end up being the same fully transparent color
|
||||
px.l=0; hash=0;
|
||||
|
||||
boost = 2000;
|
||||
if (importance_map) {
|
||||
importance_map++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!pam_add_to_hash(acht, hash, boost, px, row, rows)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
acht->cols = cols;
|
||||
acht->rows += rows;
|
||||
return true;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE bool pam_add_to_hash(struct acolorhash_table *acht, unsigned int hash, unsigned int boost, union rgba_as_int px, unsigned int row, unsigned int rows)
|
||||
{
|
||||
/* head of the hash function stores first 2 colors inline (achl->used = 1..2),
|
||||
to reduce number of allocations of achl->other_items.
|
||||
*/
|
||||
struct acolorhist_arr_head *achl = &acht->buckets[hash];
|
||||
if (achl->inline1.color.l == px.l && achl->used) {
|
||||
achl->inline1.perceptual_weight += boost;
|
||||
return true;
|
||||
}
|
||||
if (achl->used) {
|
||||
if (achl->used > 1) {
|
||||
if (achl->inline2.color.l == px.l) {
|
||||
achl->inline2.perceptual_weight += boost;
|
||||
return true;
|
||||
}
|
||||
// other items are stored as an array (which gets reallocated if needed)
|
||||
struct acolorhist_arr_item *other_items = achl->other_items;
|
||||
unsigned int i = 0;
|
||||
for (; i < achl->used-2; i++) {
|
||||
if (other_items[i].color.l == px.l) {
|
||||
other_items[i].perceptual_weight += boost;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// the array was allocated with spare items
|
||||
if (i < achl->capacity) {
|
||||
other_items[i] = (struct acolorhist_arr_item){
|
||||
.color = px,
|
||||
.perceptual_weight = boost,
|
||||
};
|
||||
achl->used++;
|
||||
++acht->colors;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (++acht->colors > acht->maxcolors) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct acolorhist_arr_item *new_items;
|
||||
unsigned int capacity;
|
||||
if (!other_items) { // there was no array previously, alloc "small" array
|
||||
capacity = 8;
|
||||
if (acht->freestackp <= 0) {
|
||||
// estimate how many colors are going to be + headroom
|
||||
const size_t mempool_size = ((acht->rows + rows-row) * 2 * acht->colors / (acht->rows + row + 1) + 1024) * sizeof(struct acolorhist_arr_item);
|
||||
new_items = mempool_alloc(&acht->mempool, sizeof(struct acolorhist_arr_item)*capacity, mempool_size);
|
||||
} else {
|
||||
// freestack stores previously freed (reallocated) arrays that can be reused
|
||||
// (all pesimistically assumed to be capacity = 8)
|
||||
new_items = acht->freestack[--acht->freestackp];
|
||||
}
|
||||
} else {
|
||||
const unsigned int stacksize = sizeof(acht->freestack)/sizeof(acht->freestack[0]);
|
||||
|
||||
// simply reallocs and copies array to larger capacity
|
||||
capacity = achl->capacity*2 + 16;
|
||||
if (acht->freestackp < stacksize-1) {
|
||||
acht->freestack[acht->freestackp++] = other_items;
|
||||
}
|
||||
const size_t mempool_size = ((acht->rows + rows-row) * 2 * acht->colors / (acht->rows + row + 1) + 32*capacity) * sizeof(struct acolorhist_arr_item);
|
||||
new_items = mempool_alloc(&acht->mempool, sizeof(struct acolorhist_arr_item)*capacity, mempool_size);
|
||||
if (!new_items) return false;
|
||||
memcpy(new_items, other_items, sizeof(other_items[0])*achl->capacity);
|
||||
}
|
||||
|
||||
achl->other_items = new_items;
|
||||
achl->capacity = capacity;
|
||||
new_items[i] = (struct acolorhist_arr_item){
|
||||
.color = px,
|
||||
.perceptual_weight = boost,
|
||||
};
|
||||
achl->used++;
|
||||
} else {
|
||||
// these are elses for first checks whether first and second inline-stored colors are used
|
||||
achl->inline2.color.l = px.l;
|
||||
achl->inline2.perceptual_weight = boost;
|
||||
achl->used = 2;
|
||||
++acht->colors;
|
||||
}
|
||||
} else {
|
||||
achl->inline1.color.l = px.l;
|
||||
achl->inline1.perceptual_weight = boost;
|
||||
achl->used = 1;
|
||||
++acht->colors;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE struct acolorhash_table *pam_allocacolorhash(unsigned int maxcolors, unsigned int surface, unsigned int ignorebits, void* (*malloc)(size_t), void (*free)(void*))
|
||||
{
|
||||
const size_t estimated_colors = MIN(maxcolors, surface/(ignorebits + (surface > 512*512 ? 6 : 5)));
|
||||
const size_t hash_size = estimated_colors < 66000 ? 6673 : (estimated_colors < 200000 ? 12011 : 24019);
|
||||
|
||||
mempoolptr m = NULL;
|
||||
const size_t buckets_size = hash_size * sizeof(struct acolorhist_arr_head);
|
||||
const size_t mempool_size = sizeof(struct acolorhash_table) + buckets_size + estimated_colors * sizeof(struct acolorhist_arr_item);
|
||||
struct acolorhash_table *t = mempool_create(&m, sizeof(*t) + buckets_size, mempool_size, malloc, free);
|
||||
if (!t) return NULL;
|
||||
*t = (struct acolorhash_table){
|
||||
.mempool = m,
|
||||
.hash_size = hash_size,
|
||||
.maxcolors = maxcolors,
|
||||
.ignorebits = ignorebits,
|
||||
};
|
||||
memset(t->buckets, 0, buckets_size);
|
||||
return t;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static float pam_add_to_hist(struct temp_hist_item achv[], unsigned int *j, const struct acolorhist_arr_item *entry, const float max_perceptual_weight, int counts[])
|
||||
{
|
||||
if (entry->perceptual_weight == 0 && *j > 0) {
|
||||
return 0;
|
||||
}
|
||||
const liq_color px = entry->color.rgba;
|
||||
achv[*j].color = px;
|
||||
const short cluster = ((px.r>>7)<<3) | ((px.g>>7)<<2) | ((px.b>>7)<<1) | (px.a>>7);
|
||||
counts[cluster]++;
|
||||
achv[*j].cluster = cluster;
|
||||
const float w = MIN(entry->perceptual_weight/170.f, max_perceptual_weight);
|
||||
achv[*j].weight = w;
|
||||
*j += 1;
|
||||
return w;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE histogram *pam_acolorhashtoacolorhist(const struct acolorhash_table *acht, const double gamma, void* (*malloc)(size_t), void (*free)(void*))
|
||||
{
|
||||
histogram *hist = malloc(sizeof(hist[0]));
|
||||
if (!hist || !acht) return NULL;
|
||||
*hist = (histogram){
|
||||
.achv = malloc(MAX(1,acht->colors) * sizeof(hist->achv[0])),
|
||||
.size = acht->colors,
|
||||
.free = free,
|
||||
.ignorebits = acht->ignorebits,
|
||||
};
|
||||
if (!hist->achv) return NULL;
|
||||
|
||||
/// Clusters form initial boxes for quantization, to ensure extreme colors are better represented
|
||||
int counts[LIQ_MAXCLUSTER] = {};
|
||||
struct temp_hist_item *temp = malloc(MAX(1, acht->colors) * sizeof(temp[0]));
|
||||
|
||||
/* Limit perceptual weight to 1/10th of the image surface area to prevent
|
||||
a single color from dominating all others. */
|
||||
float max_perceptual_weight = 0.1f * acht->cols * acht->rows;
|
||||
double total_weight = 0;
|
||||
|
||||
unsigned int j=0;
|
||||
for(unsigned int i=0; i < acht->hash_size; ++i) {
|
||||
const struct acolorhist_arr_head *const achl = &acht->buckets[i];
|
||||
if (achl->used) {
|
||||
total_weight += pam_add_to_hist(temp, &j, &achl->inline1, max_perceptual_weight, counts);
|
||||
|
||||
if (achl->used > 1) {
|
||||
total_weight += pam_add_to_hist(temp, &j, &achl->inline2, max_perceptual_weight, counts);
|
||||
|
||||
for(unsigned int k=0; k < achl->used-2; k++) {
|
||||
total_weight += pam_add_to_hist(temp, &j, &achl->other_items[k], max_perceptual_weight, counts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
hist->total_perceptual_weight = total_weight;
|
||||
|
||||
int begin = 0;
|
||||
for(int i=0; i < LIQ_MAXCLUSTER; i++) {
|
||||
hist->boxes[i].begin = begin;
|
||||
hist->boxes[i].end = begin;
|
||||
begin = begin + counts[i];
|
||||
}
|
||||
|
||||
hist->size = j;
|
||||
hist->total_perceptual_weight = total_weight;
|
||||
for(unsigned int k=0; k < hist->size; k++) {
|
||||
hist->achv[k].tmp.likely_colormap_index = 0;
|
||||
}
|
||||
if (!j) {
|
||||
free(temp);
|
||||
pam_freeacolorhist(hist);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
float gamma_lut[256];
|
||||
to_f_set_gamma(gamma_lut, gamma);
|
||||
for(int i=0; i < hist->size; i++) {
|
||||
int j = hist->boxes[temp[i].cluster].end++;
|
||||
hist->achv[j].acolor = rgba_to_f(gamma_lut, temp[i].color);
|
||||
hist->achv[j].perceptual_weight = temp[i].weight;
|
||||
hist->achv[j].adjusted_weight = temp[i].weight;
|
||||
}
|
||||
free(temp);
|
||||
|
||||
return hist;
|
||||
}
|
||||
|
||||
|
||||
LIQ_PRIVATE void pam_freeacolorhash(struct acolorhash_table *acht)
|
||||
{
|
||||
if (acht) {
|
||||
mempool_destroy(acht->mempool);
|
||||
}
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void pam_freeacolorhist(histogram *hist)
|
||||
{
|
||||
hist->free(hist->achv);
|
||||
hist->free(hist);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE LIQ_NONNULL colormap *pam_colormap(unsigned int colors, void* (*malloc)(size_t), void (*free)(void*))
|
||||
{
|
||||
assert(colors > 0 && colors < 65536);
|
||||
|
||||
colormap *map;
|
||||
const size_t colors_size = colors * sizeof(map->palette[0]);
|
||||
map = malloc(sizeof(colormap) + colors_size);
|
||||
if (!map) return NULL;
|
||||
*map = (colormap){
|
||||
.malloc = malloc,
|
||||
.free = free,
|
||||
.colors = colors,
|
||||
};
|
||||
memset(map->palette, 0, colors_size);
|
||||
return map;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE colormap *pam_duplicate_colormap(colormap *map)
|
||||
{
|
||||
colormap *dupe = pam_colormap(map->colors, map->malloc, map->free);
|
||||
for(unsigned int i=0; i < map->colors; i++) {
|
||||
dupe->palette[i] = map->palette[i];
|
||||
}
|
||||
return dupe;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void pam_freecolormap(colormap *c)
|
||||
{
|
||||
c->free(c);
|
||||
}
|
||||
|
||||
LIQ_PRIVATE void to_f_set_gamma(float gamma_lut[], const double gamma)
|
||||
{
|
||||
for(int i=0; i < 256; i++) {
|
||||
gamma_lut[i] = pow((double)i/255.0, internal_gamma/gamma);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* fixed colors are always included in the palette, so it would be wasteful to duplicate them in palette from histogram */
|
||||
LIQ_PRIVATE LIQ_NONNULL void remove_fixed_colors_from_histogram(histogram *hist, const int fixed_colors_count, const f_pixel fixed_colors[], const float target_mse)
|
||||
{
|
||||
const float max_difference = MAX(target_mse/2.f, 2.f/256.f/256.f);
|
||||
if (fixed_colors_count) {
|
||||
for(int j=0; j < hist->size; j++) {
|
||||
for(unsigned int i=0; i < fixed_colors_count; i++) {
|
||||
if (colordifference(hist->achv[j].acolor, fixed_colors[i]) < max_difference) {
|
||||
hist->achv[j] = hist->achv[--hist->size]; // remove color from histogram by overwriting with the last entry
|
||||
j--; break; // continue searching histogram
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LIQ_PRIVATE LIQ_NONNULL colormap *histogram_to_palette(const histogram *hist, void* (*malloc)(size_t), void (*free)(void*)) {
|
||||
if (!hist->size) {
|
||||
return NULL;
|
||||
}
|
||||
colormap *acolormap = pam_colormap(hist->size, malloc, free);
|
||||
for(unsigned int i=0; i < hist->size; i++) {
|
||||
acolormap->palette[i].acolor = hist->achv[i].acolor;
|
||||
acolormap->palette[i].popularity = hist->achv[i].perceptual_weight;
|
||||
}
|
||||
return acolormap;
|
||||
}
|
||||
|
||||
LIQ_PRIVATE LIQ_NONNULL void hist_reset_colors(const histogram *hist, const unsigned int colors) {
|
||||
// likely_colormap_index (used and set in kmeans_do_iteration) can't point to index outside colormap
|
||||
if (colors < 256) for(unsigned int j=0; j < hist->size; j++) {
|
||||
if (hist->achv[j].tmp.likely_colormap_index >= colors) {
|
||||
hist->achv[j].tmp.likely_colormap_index = 0; // actual value doesn't matter, as the guess is out of date anyway
|
||||
}
|
||||
}
|
||||
}
|
312
tools/assets/n64texconv/lib/libimagequant/pam.h
Normal file
312
tools/assets/n64texconv/lib/libimagequant/pam.h
Normal file
|
@ -0,0 +1,312 @@
|
|||
/* pam.h - pam (portable alpha map) utility library
|
||||
**
|
||||
** Colormap routines.
|
||||
**
|
||||
** Copyright (C) 1989, 1991 by Jef Poskanzer.
|
||||
** Copyright (C) 1997 by Greg Roelofs.
|
||||
**
|
||||
** Permission to use, copy, modify, and distribute this software and its
|
||||
** documentation for any purpose and without fee is hereby granted, provided
|
||||
** that the above copyright notice appear in all copies and that both that
|
||||
** copyright notice and this permission notice appear in supporting
|
||||
** documentation. This software is provided "as is" without express or
|
||||
** implied warranty.
|
||||
*/
|
||||
|
||||
#ifndef PAM_H
|
||||
#define PAM_H
|
||||
|
||||
// accidental debug assertions make color search much slower,
|
||||
// so force assertions off if there's no explicit setting
|
||||
#if !defined(NDEBUG) && !defined(DEBUG)
|
||||
#define NDEBUG
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef MAX
|
||||
# define MAX(a,b) ((a) > (b)? (a) : (b))
|
||||
# define MIN(a,b) ((a) < (b)? (a) : (b))
|
||||
#endif
|
||||
|
||||
#define MAX_DIFF 1e20
|
||||
|
||||
#ifndef USE_SSE
|
||||
# if defined(__SSE__) && (defined(__amd64__) || defined(__X86_64__) || defined(_WIN64) || defined(WIN32) || defined(__WIN32__))
|
||||
# define USE_SSE 1
|
||||
# else
|
||||
# define USE_SSE 0
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if USE_SSE
|
||||
# include <xmmintrin.h>
|
||||
# ifdef _MSC_VER
|
||||
# include <intrin.h>
|
||||
# define SSE_ALIGN
|
||||
# else
|
||||
# define SSE_ALIGN __attribute__ ((aligned (16)))
|
||||
# if defined(__i386__) && defined(__PIC__)
|
||||
# define cpuid(func,ax,bx,cx,dx)\
|
||||
__asm__ __volatile__ ( \
|
||||
"push %%ebx\n" \
|
||||
"cpuid\n" \
|
||||
"mov %%ebx, %1\n" \
|
||||
"pop %%ebx\n" \
|
||||
: "=a" (ax), "=r" (bx), "=c" (cx), "=d" (dx) \
|
||||
: "a" (func));
|
||||
# else
|
||||
# define cpuid(func,ax,bx,cx,dx)\
|
||||
__asm__ __volatile__ ("cpuid":\
|
||||
"=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
|
||||
# endif
|
||||
#endif
|
||||
#else
|
||||
# define SSE_ALIGN
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#define LIQ_ARRAY(type, var, count) type var[count]
|
||||
#else
|
||||
#define LIQ_ARRAY(type, var, count) type* var = (type*)_alloca(sizeof(type)*(count))
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) || defined (__llvm__)
|
||||
#define ALWAYS_INLINE __attribute__((always_inline)) inline
|
||||
#define NEVER_INLINE __attribute__ ((noinline))
|
||||
#elif defined(_MSC_VER)
|
||||
#define inline __inline
|
||||
#define restrict __restrict
|
||||
#define ALWAYS_INLINE __forceinline
|
||||
#define NEVER_INLINE __declspec(noinline)
|
||||
#else
|
||||
#define ALWAYS_INLINE inline
|
||||
#define NEVER_INLINE
|
||||
#endif
|
||||
|
||||
/* from pam.h */
|
||||
|
||||
typedef struct {
|
||||
float a, r, g, b;
|
||||
} SSE_ALIGN f_pixel;
|
||||
|
||||
static const float internal_gamma = 0.57f;
|
||||
|
||||
LIQ_PRIVATE void to_f_set_gamma(float gamma_lut[], const double gamma);
|
||||
|
||||
#define MIN_OPAQUE_A (1.f / 256.f * LIQ_WEIGHT_A)
|
||||
|
||||
#define LIQ_WEIGHT_A 0.625f
|
||||
#define LIQ_WEIGHT_R 0.5f
|
||||
#define LIQ_WEIGHT_G 1.0f
|
||||
#define LIQ_WEIGHT_B 0.45f
|
||||
#define LIQ_WEIGHT_MSE 0.45 // fudge factor for compensating that colors aren't 0..1 range
|
||||
|
||||
/**
|
||||
Converts 8-bit color to internal gamma and premultiplied alpha.
|
||||
(premultiplied color space is much better for blending of semitransparent colors)
|
||||
*/
|
||||
ALWAYS_INLINE static f_pixel rgba_to_f(const float gamma_lut[], const liq_color px);
|
||||
inline static f_pixel rgba_to_f(const float gamma_lut[], const liq_color px)
|
||||
{
|
||||
float a = px.a/255.f;
|
||||
|
||||
return (f_pixel) {
|
||||
.a = a * LIQ_WEIGHT_A,
|
||||
.r = gamma_lut[px.r] * LIQ_WEIGHT_R * a,
|
||||
.g = gamma_lut[px.g] * LIQ_WEIGHT_G * a,
|
||||
.b = gamma_lut[px.b] * LIQ_WEIGHT_B * a,
|
||||
};
|
||||
}
|
||||
|
||||
inline static liq_color f_to_rgb(const float gamma, const f_pixel px)
|
||||
{
|
||||
if (px.a < MIN_OPAQUE_A) {
|
||||
return (liq_color){0,0,0,0};
|
||||
}
|
||||
|
||||
float r = (LIQ_WEIGHT_A / LIQ_WEIGHT_R) * px.r / px.a,
|
||||
g = (LIQ_WEIGHT_A / LIQ_WEIGHT_G) * px.g / px.a,
|
||||
b = (LIQ_WEIGHT_A / LIQ_WEIGHT_B) * px.b / px.a;
|
||||
|
||||
r = powf(r, gamma/internal_gamma);
|
||||
g = powf(g, gamma/internal_gamma);
|
||||
b = powf(b, gamma/internal_gamma);
|
||||
|
||||
// 256, because numbers are in range 1..255.9999… rounded down
|
||||
r *= 256.f;
|
||||
g *= 256.f;
|
||||
b *= 256.f;
|
||||
float a = (256.f / LIQ_WEIGHT_A) * px.a;
|
||||
|
||||
return (liq_color){
|
||||
.r = r>=255.f ? 255 : r,
|
||||
.g = g>=255.f ? 255 : g,
|
||||
.b = b>=255.f ? 255 : b,
|
||||
.a = a>=255.f ? 255 : a,
|
||||
};
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static float colordifference_ch(const float x, const float y, const float alphas);
|
||||
inline static float colordifference_ch(const float x, const float y, const float alphas)
|
||||
{
|
||||
// maximum of channel blended on white, and blended on black
|
||||
// premultiplied alpha and backgrounds 0/1 shorten the formula
|
||||
const float black = x-y, white = black+alphas;
|
||||
return MAX(black*black, white*white);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static float colordifference_stdc(const f_pixel px, const f_pixel py);
|
||||
inline static float colordifference_stdc(const f_pixel px, const f_pixel py)
|
||||
{
|
||||
// px_b.rgb = px.rgb + 0*(1-px.a) // blend px on black
|
||||
// px_b.a = px.a + 1*(1-px.a)
|
||||
// px_w.rgb = px.rgb + 1*(1-px.a) // blend px on white
|
||||
// px_w.a = px.a + 1*(1-px.a)
|
||||
|
||||
// px_b.rgb = px.rgb // difference same as in opaque RGB
|
||||
// px_b.a = 1
|
||||
// px_w.rgb = px.rgb - px.a // difference simplifies to formula below
|
||||
// px_w.a = 1
|
||||
|
||||
// (px.rgb - px.a) - (py.rgb - py.a)
|
||||
// (px.rgb - py.rgb) + (py.a - px.a)
|
||||
|
||||
const float alphas = py.a-px.a;
|
||||
return colordifference_ch(px.r, py.r, alphas) +
|
||||
colordifference_ch(px.g, py.g, alphas) +
|
||||
colordifference_ch(px.b, py.b, alphas);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static float colordifference(f_pixel px, f_pixel py);
|
||||
inline static float colordifference(f_pixel px, f_pixel py)
|
||||
{
|
||||
#if USE_SSE
|
||||
#ifdef _MSC_VER
|
||||
/* In MSVC we cannot use the align attribute in parameters.
|
||||
* This is used a lot, so we just use an unaligned load.
|
||||
* Also the compiler incorrectly inlines vpx and vpy without
|
||||
* the volatile when optimization is applied for x86_64. */
|
||||
const volatile __m128 vpx = _mm_loadu_ps((const float*)&px);
|
||||
const volatile __m128 vpy = _mm_loadu_ps((const float*)&py);
|
||||
#else
|
||||
const __m128 vpx = _mm_load_ps((const float*)&px);
|
||||
const __m128 vpy = _mm_load_ps((const float*)&py);
|
||||
#endif
|
||||
|
||||
// y.a - x.a
|
||||
__m128 alphas = _mm_sub_ss(vpy, vpx);
|
||||
alphas = _mm_shuffle_ps(alphas,alphas,0); // copy first to all four
|
||||
|
||||
__m128 onblack = _mm_sub_ps(vpx, vpy); // x - y
|
||||
__m128 onwhite = _mm_add_ps(onblack, alphas); // x - y + (y.a - x.a)
|
||||
|
||||
onblack = _mm_mul_ps(onblack, onblack);
|
||||
onwhite = _mm_mul_ps(onwhite, onwhite);
|
||||
const __m128 max = _mm_max_ps(onwhite, onblack);
|
||||
|
||||
// add rgb, not a
|
||||
const __m128 maxhl = _mm_movehl_ps(max, max);
|
||||
const __m128 tmp = _mm_add_ps(max, maxhl);
|
||||
const __m128 sum = _mm_add_ss(maxhl, _mm_shuffle_ps(tmp, tmp, 1));
|
||||
|
||||
const float res = _mm_cvtss_f32(sum);
|
||||
assert(fabs(res - colordifference_stdc(px,py)) < 0.001);
|
||||
return res;
|
||||
#else
|
||||
return colordifference_stdc(px,py);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* from pamcmap.h */
|
||||
union rgba_as_int {
|
||||
liq_color rgba;
|
||||
unsigned int l;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
f_pixel acolor;
|
||||
float adjusted_weight, // perceptual weight changed to tweak how mediancut selects colors
|
||||
perceptual_weight; // number of pixels weighted by importance of different areas of the picture
|
||||
|
||||
float color_weight; // these two change every time histogram subset is sorted
|
||||
union {
|
||||
unsigned int sort_value;
|
||||
unsigned char likely_colormap_index;
|
||||
} tmp;
|
||||
} hist_item;
|
||||
|
||||
#define LIQ_MAXCLUSTER 16
|
||||
|
||||
struct temp_hist_item {
|
||||
liq_color color;
|
||||
float weight;
|
||||
short cluster;
|
||||
};
|
||||
|
||||
struct histogram_box {
|
||||
int begin, end;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
hist_item *achv;
|
||||
void (*free)(void*);
|
||||
double total_perceptual_weight;
|
||||
unsigned int size;
|
||||
unsigned int ignorebits;
|
||||
struct histogram_box boxes[LIQ_MAXCLUSTER];
|
||||
} histogram;
|
||||
|
||||
typedef struct {
|
||||
f_pixel acolor;
|
||||
float popularity;
|
||||
bool fixed; // if true it's user-supplied and must not be changed (e.g in K-Means iteration)
|
||||
} colormap_item;
|
||||
|
||||
typedef struct colormap {
|
||||
unsigned int colors;
|
||||
void* (*malloc)(size_t);
|
||||
void (*free)(void*);
|
||||
colormap_item palette[];
|
||||
} colormap;
|
||||
|
||||
struct acolorhist_arr_item {
|
||||
union rgba_as_int color;
|
||||
unsigned int perceptual_weight;
|
||||
};
|
||||
|
||||
struct acolorhist_arr_head {
|
||||
struct acolorhist_arr_item inline1, inline2;
|
||||
unsigned int used, capacity;
|
||||
struct acolorhist_arr_item *other_items;
|
||||
};
|
||||
|
||||
struct acolorhash_table {
|
||||
struct mempool *mempool;
|
||||
unsigned int ignorebits, maxcolors, colors, cols, rows;
|
||||
unsigned int hash_size;
|
||||
unsigned int freestackp;
|
||||
struct acolorhist_arr_item *freestack[512];
|
||||
struct acolorhist_arr_head buckets[];
|
||||
};
|
||||
|
||||
LIQ_PRIVATE void pam_freeacolorhash(struct acolorhash_table *acht);
|
||||
LIQ_PRIVATE struct acolorhash_table *pam_allocacolorhash(unsigned int maxcolors, unsigned int surface, unsigned int ignorebits, void* (*malloc)(size_t), void (*free)(void*));
|
||||
LIQ_PRIVATE histogram *pam_acolorhashtoacolorhist(const struct acolorhash_table *acht, const double gamma, void* (*malloc)(size_t), void (*free)(void*));
|
||||
LIQ_PRIVATE bool pam_computeacolorhash(struct acolorhash_table *acht, const liq_color *const pixels[], unsigned int cols, unsigned int rows, const unsigned char *importance_map);
|
||||
LIQ_PRIVATE bool pam_add_to_hash(struct acolorhash_table *acht, unsigned int hash, unsigned int boost, union rgba_as_int px, unsigned int row, unsigned int rows);
|
||||
|
||||
LIQ_PRIVATE void pam_freeacolorhist(histogram *h);
|
||||
|
||||
LIQ_PRIVATE colormap *pam_colormap(unsigned int colors, void* (*malloc)(size_t), void (*free)(void*)) LIQ_NONNULL;
|
||||
LIQ_PRIVATE colormap *pam_duplicate_colormap(colormap *map) LIQ_NONNULL;
|
||||
LIQ_PRIVATE void pam_freecolormap(colormap *c);
|
||||
|
||||
LIQ_PRIVATE void remove_fixed_colors_from_histogram(histogram *hist, const int fixed_colors_count, const f_pixel fixed_colors[], const float target_mse) LIQ_NONNULL;
|
||||
LIQ_PRIVATE colormap *histogram_to_palette(const histogram *hist, void* (*malloc)(size_t), void (*free)(void*)) LIQ_NONNULL;
|
||||
LIQ_PRIVATE void hist_reset_colors(const histogram *hist, const unsigned int colors) LIQ_NONNULL;
|
||||
|
||||
#endif
|
300
tools/assets/n64texconv/lib/libimagequant/remap.c
Normal file
300
tools/assets/n64texconv/lib/libimagequant/remap.c
Normal file
|
@ -0,0 +1,300 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libimagequant.h"
|
||||
#include "pam.h"
|
||||
#include "libimagequant_private.h"
|
||||
|
||||
#include "nearest.h"
|
||||
#include "kmeans.h"
|
||||
|
||||
LIQ_PRIVATE LIQ_NONNULL float remap_to_palette(liq_image *const input_image, unsigned char *const *const output_pixels, colormap *const map)
|
||||
{
|
||||
const int rows = input_image->height;
|
||||
const unsigned int cols = input_image->width;
|
||||
double remapping_error=0;
|
||||
|
||||
if (!liq_image_get_row_f_init(input_image)) {
|
||||
return -1;
|
||||
}
|
||||
if (input_image->background && !liq_image_get_row_f_init(input_image->background)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const colormap_item *acolormap = map->palette;
|
||||
|
||||
struct nearest_map *const n = nearest_init(map);
|
||||
liq_image *background = input_image->background;
|
||||
const int transparent_index = background ? nearest_search(n, &(f_pixel){0,0,0,0}, 0, NULL) : -1;
|
||||
if (background && acolormap[transparent_index].acolor.a > 1.f/256.f) {
|
||||
// palette unsuitable for using the bg
|
||||
background = NULL;
|
||||
}
|
||||
|
||||
|
||||
const unsigned int max_threads = omp_get_max_threads();
|
||||
LIQ_ARRAY(kmeans_state, average_color, (KMEANS_CACHE_LINE_GAP+map->colors) * max_threads);
|
||||
kmeans_init(map, max_threads, average_color);
|
||||
|
||||
#if __GNUC__ >= 9 || __clang__
|
||||
#pragma omp parallel for if (rows*cols > 3000) \
|
||||
schedule(static) default(none) shared(background,acolormap,average_color,cols,input_image,map,n,output_pixels,rows,transparent_index) reduction(+:remapping_error)
|
||||
#endif
|
||||
for(int row = 0; row < rows; ++row) {
|
||||
const f_pixel *const row_pixels = liq_image_get_row_f(input_image, row);
|
||||
const f_pixel *const bg_pixels = background && acolormap[transparent_index].acolor.a < MIN_OPAQUE_A ? liq_image_get_row_f(background, row) : NULL;
|
||||
|
||||
unsigned int last_match=0;
|
||||
for(unsigned int col = 0; col < cols; ++col) {
|
||||
float diff;
|
||||
last_match = nearest_search(n, &row_pixels[col], last_match, &diff);
|
||||
if (bg_pixels) {
|
||||
float bg_diff = colordifference(bg_pixels[col], acolormap[last_match].acolor);
|
||||
if (bg_diff <= diff) {
|
||||
diff = bg_diff;
|
||||
last_match = transparent_index;
|
||||
}
|
||||
}
|
||||
output_pixels[row][col] = last_match;
|
||||
|
||||
remapping_error += diff;
|
||||
if (last_match != transparent_index) {
|
||||
kmeans_update_color(row_pixels[col], 1.0, map, last_match, omp_get_thread_num(), average_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
kmeans_finalize(map, max_threads, average_color);
|
||||
|
||||
nearest_free(n);
|
||||
|
||||
return remapping_error / (input_image->width * input_image->height);
|
||||
}
|
||||
|
||||
inline static f_pixel get_dithered_pixel(const float dither_level, const float max_dither_error, const f_pixel thiserr, const f_pixel px)
|
||||
{
|
||||
/* Use Floyd-Steinberg errors to adjust actual color. */
|
||||
const float sr = thiserr.r * dither_level,
|
||||
sg = thiserr.g * dither_level,
|
||||
sb = thiserr.b * dither_level,
|
||||
sa = thiserr.a * dither_level;
|
||||
|
||||
float ratio = 1.0;
|
||||
const float max_overflow = 1.1f;
|
||||
const float max_underflow = -0.1f;
|
||||
|
||||
// allowing some overflow prevents undithered bands caused by clamping of all channels
|
||||
if (px.r + sr > max_overflow) ratio = MIN(ratio, (max_overflow -px.r)/sr);
|
||||
else { if (px.r + sr < max_underflow) ratio = MIN(ratio, (max_underflow-px.r)/sr); }
|
||||
if (px.g + sg > max_overflow) ratio = MIN(ratio, (max_overflow -px.g)/sg);
|
||||
else { if (px.g + sg < max_underflow) ratio = MIN(ratio, (max_underflow-px.g)/sg); }
|
||||
if (px.b + sb > max_overflow) ratio = MIN(ratio, (max_overflow -px.b)/sb);
|
||||
else { if (px.b + sb < max_underflow) ratio = MIN(ratio, (max_underflow-px.b)/sb); }
|
||||
|
||||
float a = px.a + sa;
|
||||
if (a > 1.f) { a = 1.f; }
|
||||
else if (a < 0) { a = 0; }
|
||||
|
||||
// If dithering error is crazy high, don't propagate it that much
|
||||
// This prevents crazy geen pixels popping out of the blue (or red or black! ;)
|
||||
const float dither_error = sr*sr + sg*sg + sb*sb + sa*sa;
|
||||
if (dither_error > max_dither_error) {
|
||||
ratio *= 0.8f;
|
||||
} else if (dither_error < 2.f/256.f/256.f) {
|
||||
// don't dither areas that don't have noticeable error — makes file smaller
|
||||
return px;
|
||||
}
|
||||
|
||||
return (f_pixel) {
|
||||
.r=px.r + sr * ratio,
|
||||
.g=px.g + sg * ratio,
|
||||
.b=px.b + sb * ratio,
|
||||
.a=a,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
Uses edge/noise map to apply dithering only to flat areas. Dithering on edges creates jagged lines, and noisy areas are "naturally" dithered.
|
||||
|
||||
If output_image_is_remapped is true, only pixels noticeably changed by error diffusion will be written to output image.
|
||||
*/
|
||||
LIQ_PRIVATE LIQ_NONNULL bool remap_to_palette_floyd(liq_image *input_image, unsigned char *const output_pixels[], liq_remapping_result *quant, const float max_dither_error, const bool output_image_is_remapped)
|
||||
{
|
||||
const int rows = input_image->height, cols = input_image->width;
|
||||
const unsigned char *dither_map = quant->use_dither_map ? (input_image->dither_map ? input_image->dither_map : input_image->edges) : NULL;
|
||||
|
||||
const colormap *map = quant->palette;
|
||||
const colormap_item *acolormap = map->palette;
|
||||
|
||||
if (!liq_image_get_row_f_init(input_image)) {
|
||||
return false;
|
||||
}
|
||||
if (input_image->background && !liq_image_get_row_f_init(input_image->background)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Initialize Floyd-Steinberg error vectors. */
|
||||
const size_t errwidth = cols+2;
|
||||
f_pixel *restrict thiserr = input_image->malloc(errwidth * sizeof(thiserr[0]) * 2); // +2 saves from checking out of bounds access
|
||||
if (!thiserr) return false;
|
||||
f_pixel *restrict nexterr = thiserr + errwidth;
|
||||
memset(thiserr, 0, errwidth * sizeof(thiserr[0]));
|
||||
|
||||
bool ok = true;
|
||||
struct nearest_map *const n = nearest_init(map);
|
||||
liq_image *background = input_image->background;
|
||||
const int transparent_index = background ? nearest_search(n, &(f_pixel){0,0,0,0}, 0, NULL) : -1;
|
||||
if (background && acolormap[transparent_index].acolor.a > 1.f/256.f) {
|
||||
// palette unsuitable for using the bg
|
||||
background = NULL;
|
||||
}
|
||||
|
||||
// response to this value is non-linear and without it any value < 0.8 would give almost no dithering
|
||||
float base_dithering_level = quant->dither_level;
|
||||
base_dithering_level = 1.f - (1.f-base_dithering_level)*(1.f-base_dithering_level);
|
||||
|
||||
if (dither_map) {
|
||||
base_dithering_level *= 1.f/255.f; // convert byte to float
|
||||
}
|
||||
base_dithering_level *= 15.f/16.f; // prevent small errors from accumulating
|
||||
|
||||
int fs_direction = 1;
|
||||
unsigned int last_match=0;
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
if (liq_remap_progress(quant, quant->progress_stage1 + row * (100.f - quant->progress_stage1) / rows)) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
memset(nexterr, 0, errwidth * sizeof(nexterr[0]));
|
||||
|
||||
int col = (fs_direction > 0) ? 0 : (cols - 1);
|
||||
const f_pixel *const row_pixels = liq_image_get_row_f(input_image, row);
|
||||
const f_pixel *const bg_pixels = background && acolormap[transparent_index].acolor.a < MIN_OPAQUE_A ? liq_image_get_row_f(background, row) : NULL;
|
||||
int undithered_bg_used = 0;
|
||||
|
||||
do {
|
||||
float dither_level = base_dithering_level;
|
||||
if (dither_map) {
|
||||
dither_level *= dither_map[row*cols + col];
|
||||
}
|
||||
|
||||
const f_pixel spx = get_dithered_pixel(dither_level, max_dither_error, thiserr[col + 1], row_pixels[col]);
|
||||
|
||||
const unsigned int guessed_match = output_image_is_remapped ? output_pixels[row][col] : last_match;
|
||||
float dither_diff;
|
||||
last_match = nearest_search(n, &spx, guessed_match, &dither_diff);
|
||||
f_pixel output_px = acolormap[last_match].acolor;
|
||||
// this is for animgifs
|
||||
if (bg_pixels) {
|
||||
// if the background makes better match *with* dithering, it's a definitive win
|
||||
float bg_for_dither_diff = colordifference(spx, bg_pixels[col]);
|
||||
if (bg_for_dither_diff <= dither_diff) {
|
||||
output_px = bg_pixels[col];
|
||||
last_match = transparent_index;
|
||||
} else if (undithered_bg_used > 1) {
|
||||
// the undithered fallback can cause artifacts when too many undithered pixels accumulate a big dithering error
|
||||
// so periodically ignore undithered fallback to prevent that
|
||||
undithered_bg_used = 0;
|
||||
} else {
|
||||
// if dithering is not applied, there's a high risk of creating artifacts (flat areas, error accumulating badly),
|
||||
// OTOH poor dithering disturbs static backgrounds and creates oscilalting frames that break backgrounds
|
||||
// back and forth in two differently bad ways
|
||||
float max_diff = colordifference(row_pixels[col], bg_pixels[col]);
|
||||
float dithered_diff = colordifference(row_pixels[col], output_px);
|
||||
// if dithering is worse than natural difference between frames
|
||||
// (this rule dithers moving areas, but does not dither static areas)
|
||||
if (dithered_diff > max_diff) {
|
||||
// then see if an undithered color is closer to the ideal
|
||||
float undithered_diff = colordifference(row_pixels[col], acolormap[guessed_match].acolor);
|
||||
if (undithered_diff < max_diff) {
|
||||
undithered_bg_used++;
|
||||
output_px = acolormap[guessed_match].acolor;
|
||||
last_match = guessed_match;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output_pixels[row][col] = last_match;
|
||||
|
||||
f_pixel err = {
|
||||
.r = (spx.r - output_px.r),
|
||||
.g = (spx.g - output_px.g),
|
||||
.b = (spx.b - output_px.b),
|
||||
.a = (spx.a - output_px.a),
|
||||
};
|
||||
|
||||
// If dithering error is crazy high, don't propagate it that much
|
||||
// This prevents crazy geen pixels popping out of the blue (or red or black! ;)
|
||||
if (err.r*err.r + err.g*err.g + err.b*err.b + err.a*err.a > max_dither_error) {
|
||||
err.r *= 0.75f;
|
||||
err.g *= 0.75f;
|
||||
err.b *= 0.75f;
|
||||
err.a *= 0.75f;
|
||||
}
|
||||
|
||||
/* Propagate Floyd-Steinberg error terms. */
|
||||
if (fs_direction > 0) {
|
||||
thiserr[col + 2].a += err.a * (7.f/16.f);
|
||||
thiserr[col + 2].r += err.r * (7.f/16.f);
|
||||
thiserr[col + 2].g += err.g * (7.f/16.f);
|
||||
thiserr[col + 2].b += err.b * (7.f/16.f);
|
||||
|
||||
nexterr[col + 2].a = err.a * (1.f/16.f);
|
||||
nexterr[col + 2].r = err.r * (1.f/16.f);
|
||||
nexterr[col + 2].g = err.g * (1.f/16.f);
|
||||
nexterr[col + 2].b = err.b * (1.f/16.f);
|
||||
|
||||
nexterr[col + 1].a += err.a * (5.f/16.f);
|
||||
nexterr[col + 1].r += err.r * (5.f/16.f);
|
||||
nexterr[col + 1].g += err.g * (5.f/16.f);
|
||||
nexterr[col + 1].b += err.b * (5.f/16.f);
|
||||
|
||||
nexterr[col ].a += err.a * (3.f/16.f);
|
||||
nexterr[col ].r += err.r * (3.f/16.f);
|
||||
nexterr[col ].g += err.g * (3.f/16.f);
|
||||
nexterr[col ].b += err.b * (3.f/16.f);
|
||||
|
||||
} else {
|
||||
thiserr[col ].a += err.a * (7.f/16.f);
|
||||
thiserr[col ].r += err.r * (7.f/16.f);
|
||||
thiserr[col ].g += err.g * (7.f/16.f);
|
||||
thiserr[col ].b += err.b * (7.f/16.f);
|
||||
|
||||
nexterr[col ].a = err.a * (1.f/16.f);
|
||||
nexterr[col ].r = err.r * (1.f/16.f);
|
||||
nexterr[col ].g = err.g * (1.f/16.f);
|
||||
nexterr[col ].b = err.b * (1.f/16.f);
|
||||
|
||||
nexterr[col + 1].a += err.a * (5.f/16.f);
|
||||
nexterr[col + 1].r += err.r * (5.f/16.f);
|
||||
nexterr[col + 1].g += err.g * (5.f/16.f);
|
||||
nexterr[col + 1].b += err.b * (5.f/16.f);
|
||||
|
||||
nexterr[col + 2].a += err.a * (3.f/16.f);
|
||||
nexterr[col + 2].r += err.r * (3.f/16.f);
|
||||
nexterr[col + 2].g += err.g * (3.f/16.f);
|
||||
nexterr[col + 2].b += err.b * (3.f/16.f);
|
||||
}
|
||||
|
||||
// remapping is done in zig-zag
|
||||
col += fs_direction;
|
||||
if (fs_direction > 0) {
|
||||
if (col >= cols) break;
|
||||
} else {
|
||||
if (col < 0) break;
|
||||
}
|
||||
} while(1);
|
||||
|
||||
f_pixel *const temperr = thiserr;
|
||||
thiserr = nexterr;
|
||||
nexterr = temperr;
|
||||
fs_direction = -fs_direction;
|
||||
}
|
||||
|
||||
input_image->free(MIN(thiserr, nexterr)); // MIN because pointers were swapped
|
||||
nearest_free(n);
|
||||
|
||||
return ok;
|
||||
}
|
7
tools/assets/n64texconv/lib/libimagequant/remap.h
Normal file
7
tools/assets/n64texconv/lib/libimagequant/remap.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef REMAP_H
|
||||
#define REMAP_H
|
||||
|
||||
LIQ_PRIVATE float remap_to_palette(liq_image *const input_image, unsigned char *const *const output_pixels, colormap *const map) LIQ_NONNULL;
|
||||
LIQ_PRIVATE bool remap_to_palette_floyd(liq_image *input_image, unsigned char *const output_pixels[], liq_remapping_result *quant, const float max_dither_error, const bool output_image_is_remapped) LIQ_NONNULL;
|
||||
|
||||
#endif
|
25
tools/assets/n64texconv/lib/spng/LICENSE
Normal file
25
tools/assets/n64texconv/lib/spng/LICENSE
Normal file
|
@ -0,0 +1,25 @@
|
|||
BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2018-2023, Randy <randy408@protonmail.com>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
6980
tools/assets/n64texconv/lib/spng/spng.c
Normal file
6980
tools/assets/n64texconv/lib/spng/spng.c
Normal file
File diff suppressed because it is too large
Load diff
537
tools/assets/n64texconv/lib/spng/spng.h
Normal file
537
tools/assets/n64texconv/lib/spng/spng.h
Normal file
|
@ -0,0 +1,537 @@
|
|||
/* SPDX-License-Identifier: BSD-2-Clause */
|
||||
#ifndef SPNG_H
|
||||
#define SPNG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(SPNG_STATIC)
|
||||
#if defined(SPNG__BUILD)
|
||||
#define SPNG_API __declspec(dllexport)
|
||||
#else
|
||||
#define SPNG_API __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define SPNG_API
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define SPNG_CDECL __cdecl
|
||||
#else
|
||||
#define SPNG_CDECL
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define SPNG_VERSION_MAJOR 0
|
||||
#define SPNG_VERSION_MINOR 7
|
||||
#define SPNG_VERSION_PATCH 4
|
||||
|
||||
enum spng_errno
|
||||
{
|
||||
SPNG_IO_ERROR = -2,
|
||||
SPNG_IO_EOF = -1,
|
||||
SPNG_OK = 0,
|
||||
SPNG_EINVAL,
|
||||
SPNG_EMEM,
|
||||
SPNG_EOVERFLOW,
|
||||
SPNG_ESIGNATURE,
|
||||
SPNG_EWIDTH,
|
||||
SPNG_EHEIGHT,
|
||||
SPNG_EUSER_WIDTH,
|
||||
SPNG_EUSER_HEIGHT,
|
||||
SPNG_EBIT_DEPTH,
|
||||
SPNG_ECOLOR_TYPE,
|
||||
SPNG_ECOMPRESSION_METHOD,
|
||||
SPNG_EFILTER_METHOD,
|
||||
SPNG_EINTERLACE_METHOD,
|
||||
SPNG_EIHDR_SIZE,
|
||||
SPNG_ENOIHDR,
|
||||
SPNG_ECHUNK_POS,
|
||||
SPNG_ECHUNK_SIZE,
|
||||
SPNG_ECHUNK_CRC,
|
||||
SPNG_ECHUNK_TYPE,
|
||||
SPNG_ECHUNK_UNKNOWN_CRITICAL,
|
||||
SPNG_EDUP_PLTE,
|
||||
SPNG_EDUP_CHRM,
|
||||
SPNG_EDUP_GAMA,
|
||||
SPNG_EDUP_ICCP,
|
||||
SPNG_EDUP_SBIT,
|
||||
SPNG_EDUP_SRGB,
|
||||
SPNG_EDUP_BKGD,
|
||||
SPNG_EDUP_HIST,
|
||||
SPNG_EDUP_TRNS,
|
||||
SPNG_EDUP_PHYS,
|
||||
SPNG_EDUP_TIME,
|
||||
SPNG_EDUP_OFFS,
|
||||
SPNG_EDUP_EXIF,
|
||||
SPNG_ECHRM,
|
||||
SPNG_EPLTE_IDX,
|
||||
SPNG_ETRNS_COLOR_TYPE,
|
||||
SPNG_ETRNS_NO_PLTE,
|
||||
SPNG_EGAMA,
|
||||
SPNG_EICCP_NAME,
|
||||
SPNG_EICCP_COMPRESSION_METHOD,
|
||||
SPNG_ESBIT,
|
||||
SPNG_ESRGB,
|
||||
SPNG_ETEXT,
|
||||
SPNG_ETEXT_KEYWORD,
|
||||
SPNG_EZTXT,
|
||||
SPNG_EZTXT_COMPRESSION_METHOD,
|
||||
SPNG_EITXT,
|
||||
SPNG_EITXT_COMPRESSION_FLAG,
|
||||
SPNG_EITXT_COMPRESSION_METHOD,
|
||||
SPNG_EITXT_LANG_TAG,
|
||||
SPNG_EITXT_TRANSLATED_KEY,
|
||||
SPNG_EBKGD_NO_PLTE,
|
||||
SPNG_EBKGD_PLTE_IDX,
|
||||
SPNG_EHIST_NO_PLTE,
|
||||
SPNG_EPHYS,
|
||||
SPNG_ESPLT_NAME,
|
||||
SPNG_ESPLT_DUP_NAME,
|
||||
SPNG_ESPLT_DEPTH,
|
||||
SPNG_ETIME,
|
||||
SPNG_EOFFS,
|
||||
SPNG_EEXIF,
|
||||
SPNG_EIDAT_TOO_SHORT,
|
||||
SPNG_EIDAT_STREAM,
|
||||
SPNG_EZLIB,
|
||||
SPNG_EFILTER,
|
||||
SPNG_EBUFSIZ,
|
||||
SPNG_EIO,
|
||||
SPNG_EOF,
|
||||
SPNG_EBUF_SET,
|
||||
SPNG_EBADSTATE,
|
||||
SPNG_EFMT,
|
||||
SPNG_EFLAGS,
|
||||
SPNG_ECHUNKAVAIL,
|
||||
SPNG_ENCODE_ONLY,
|
||||
SPNG_EOI,
|
||||
SPNG_ENOPLTE,
|
||||
SPNG_ECHUNK_LIMITS,
|
||||
SPNG_EZLIB_INIT,
|
||||
SPNG_ECHUNK_STDLEN,
|
||||
SPNG_EINTERNAL,
|
||||
SPNG_ECTXTYPE,
|
||||
SPNG_ENOSRC,
|
||||
SPNG_ENODST,
|
||||
SPNG_EOPSTATE,
|
||||
SPNG_ENOTFINAL,
|
||||
};
|
||||
|
||||
enum spng_text_type
|
||||
{
|
||||
SPNG_TEXT = 1,
|
||||
SPNG_ZTXT = 2,
|
||||
SPNG_ITXT = 3
|
||||
};
|
||||
|
||||
enum spng_color_type
|
||||
{
|
||||
SPNG_COLOR_TYPE_GRAYSCALE = 0,
|
||||
SPNG_COLOR_TYPE_TRUECOLOR = 2,
|
||||
SPNG_COLOR_TYPE_INDEXED = 3,
|
||||
SPNG_COLOR_TYPE_GRAYSCALE_ALPHA = 4,
|
||||
SPNG_COLOR_TYPE_TRUECOLOR_ALPHA = 6
|
||||
};
|
||||
|
||||
enum spng_filter
|
||||
{
|
||||
SPNG_FILTER_NONE = 0,
|
||||
SPNG_FILTER_SUB = 1,
|
||||
SPNG_FILTER_UP = 2,
|
||||
SPNG_FILTER_AVERAGE = 3,
|
||||
SPNG_FILTER_PAETH = 4
|
||||
};
|
||||
|
||||
enum spng_filter_choice
|
||||
{
|
||||
SPNG_DISABLE_FILTERING = 0,
|
||||
SPNG_FILTER_CHOICE_NONE = 8,
|
||||
SPNG_FILTER_CHOICE_SUB = 16,
|
||||
SPNG_FILTER_CHOICE_UP = 32,
|
||||
SPNG_FILTER_CHOICE_AVG = 64,
|
||||
SPNG_FILTER_CHOICE_PAETH = 128,
|
||||
SPNG_FILTER_CHOICE_ALL = (8|16|32|64|128)
|
||||
};
|
||||
|
||||
enum spng_interlace_method
|
||||
{
|
||||
SPNG_INTERLACE_NONE = 0,
|
||||
SPNG_INTERLACE_ADAM7 = 1
|
||||
};
|
||||
|
||||
/* Channels are always in byte-order */
|
||||
enum spng_format
|
||||
{
|
||||
SPNG_FMT_RGBA8 = 1,
|
||||
SPNG_FMT_RGBA16 = 2,
|
||||
SPNG_FMT_RGB8 = 4,
|
||||
|
||||
/* Partially implemented, see documentation */
|
||||
SPNG_FMT_GA8 = 16,
|
||||
SPNG_FMT_GA16 = 32,
|
||||
SPNG_FMT_G8 = 64,
|
||||
|
||||
/* No conversion or scaling */
|
||||
SPNG_FMT_PNG = 256,
|
||||
SPNG_FMT_RAW = 512 /* big-endian (everything else is host-endian) */
|
||||
};
|
||||
|
||||
enum spng_ctx_flags
|
||||
{
|
||||
SPNG_CTX_IGNORE_ADLER32 = 1, /* Ignore checksum in DEFLATE streams */
|
||||
SPNG_CTX_ENCODER = 2 /* Create an encoder context */
|
||||
};
|
||||
|
||||
enum spng_decode_flags
|
||||
{
|
||||
SPNG_DECODE_USE_TRNS = 1, /* Deprecated */
|
||||
SPNG_DECODE_USE_GAMA = 2, /* Deprecated */
|
||||
SPNG_DECODE_USE_SBIT = 8, /* Undocumented */
|
||||
|
||||
SPNG_DECODE_TRNS = 1, /* Apply transparency */
|
||||
SPNG_DECODE_GAMMA = 2, /* Apply gamma correction */
|
||||
SPNG_DECODE_PROGRESSIVE = 256 /* Initialize for progressive reads */
|
||||
};
|
||||
|
||||
enum spng_crc_action
|
||||
{
|
||||
/* Default for critical chunks */
|
||||
SPNG_CRC_ERROR = 0,
|
||||
|
||||
/* Discard chunk, invalid for critical chunks.
|
||||
Since v0.6.2: default for ancillary chunks */
|
||||
SPNG_CRC_DISCARD = 1,
|
||||
|
||||
/* Ignore and don't calculate checksum.
|
||||
Since v0.6.2: also ignores checksums in DEFLATE streams */
|
||||
SPNG_CRC_USE = 2
|
||||
};
|
||||
|
||||
enum spng_encode_flags
|
||||
{
|
||||
SPNG_ENCODE_PROGRESSIVE = 1, /* Initialize for progressive writes */
|
||||
SPNG_ENCODE_FINALIZE = 2, /* Finalize PNG after encoding image */
|
||||
};
|
||||
|
||||
struct spng_ihdr
|
||||
{
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t bit_depth;
|
||||
uint8_t color_type;
|
||||
uint8_t compression_method;
|
||||
uint8_t filter_method;
|
||||
uint8_t interlace_method;
|
||||
};
|
||||
|
||||
struct spng_plte_entry
|
||||
{
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
|
||||
uint8_t alpha; /* Reserved for internal use */
|
||||
};
|
||||
|
||||
struct spng_plte
|
||||
{
|
||||
uint32_t n_entries;
|
||||
struct spng_plte_entry entries[256];
|
||||
};
|
||||
|
||||
struct spng_trns
|
||||
{
|
||||
uint16_t gray;
|
||||
|
||||
uint16_t red;
|
||||
uint16_t green;
|
||||
uint16_t blue;
|
||||
|
||||
uint32_t n_type3_entries;
|
||||
uint8_t type3_alpha[256];
|
||||
};
|
||||
|
||||
struct spng_chrm_int
|
||||
{
|
||||
uint32_t white_point_x;
|
||||
uint32_t white_point_y;
|
||||
uint32_t red_x;
|
||||
uint32_t red_y;
|
||||
uint32_t green_x;
|
||||
uint32_t green_y;
|
||||
uint32_t blue_x;
|
||||
uint32_t blue_y;
|
||||
};
|
||||
|
||||
struct spng_chrm
|
||||
{
|
||||
double white_point_x;
|
||||
double white_point_y;
|
||||
double red_x;
|
||||
double red_y;
|
||||
double green_x;
|
||||
double green_y;
|
||||
double blue_x;
|
||||
double blue_y;
|
||||
};
|
||||
|
||||
struct spng_iccp
|
||||
{
|
||||
char profile_name[80];
|
||||
size_t profile_len;
|
||||
char *profile;
|
||||
};
|
||||
|
||||
struct spng_sbit
|
||||
{
|
||||
uint8_t grayscale_bits;
|
||||
uint8_t red_bits;
|
||||
uint8_t green_bits;
|
||||
uint8_t blue_bits;
|
||||
uint8_t alpha_bits;
|
||||
};
|
||||
|
||||
struct spng_text
|
||||
{
|
||||
char keyword[80];
|
||||
int type;
|
||||
|
||||
size_t length;
|
||||
char *text;
|
||||
|
||||
uint8_t compression_flag; /* iTXt only */
|
||||
uint8_t compression_method; /* iTXt, ztXt only */
|
||||
char *language_tag; /* iTXt only */
|
||||
char *translated_keyword; /* iTXt only */
|
||||
};
|
||||
|
||||
struct spng_bkgd
|
||||
{
|
||||
uint16_t gray; /* Only for gray/gray alpha */
|
||||
uint16_t red;
|
||||
uint16_t green;
|
||||
uint16_t blue;
|
||||
uint16_t plte_index; /* Only for indexed color */
|
||||
};
|
||||
|
||||
struct spng_hist
|
||||
{
|
||||
uint16_t frequency[256];
|
||||
};
|
||||
|
||||
struct spng_phys
|
||||
{
|
||||
uint32_t ppu_x, ppu_y;
|
||||
uint8_t unit_specifier;
|
||||
};
|
||||
|
||||
struct spng_splt_entry
|
||||
{
|
||||
uint16_t red;
|
||||
uint16_t green;
|
||||
uint16_t blue;
|
||||
uint16_t alpha;
|
||||
uint16_t frequency;
|
||||
};
|
||||
|
||||
struct spng_splt
|
||||
{
|
||||
char name[80];
|
||||
uint8_t sample_depth;
|
||||
uint32_t n_entries;
|
||||
struct spng_splt_entry *entries;
|
||||
};
|
||||
|
||||
struct spng_time
|
||||
{
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
};
|
||||
|
||||
struct spng_offs
|
||||
{
|
||||
int32_t x, y;
|
||||
uint8_t unit_specifier;
|
||||
};
|
||||
|
||||
struct spng_exif
|
||||
{
|
||||
size_t length;
|
||||
char *data;
|
||||
};
|
||||
|
||||
struct spng_chunk
|
||||
{
|
||||
size_t offset;
|
||||
uint32_t length;
|
||||
uint8_t type[4];
|
||||
uint32_t crc;
|
||||
};
|
||||
|
||||
enum spng_location
|
||||
{
|
||||
SPNG_AFTER_IHDR = 1,
|
||||
SPNG_AFTER_PLTE = 2,
|
||||
SPNG_AFTER_IDAT = 8,
|
||||
};
|
||||
|
||||
struct spng_unknown_chunk
|
||||
{
|
||||
uint8_t type[4];
|
||||
size_t length;
|
||||
void *data;
|
||||
enum spng_location location;
|
||||
};
|
||||
|
||||
enum spng_option
|
||||
{
|
||||
SPNG_KEEP_UNKNOWN_CHUNKS = 1,
|
||||
|
||||
SPNG_IMG_COMPRESSION_LEVEL,
|
||||
SPNG_IMG_WINDOW_BITS,
|
||||
SPNG_IMG_MEM_LEVEL,
|
||||
SPNG_IMG_COMPRESSION_STRATEGY,
|
||||
|
||||
SPNG_TEXT_COMPRESSION_LEVEL,
|
||||
SPNG_TEXT_WINDOW_BITS,
|
||||
SPNG_TEXT_MEM_LEVEL,
|
||||
SPNG_TEXT_COMPRESSION_STRATEGY,
|
||||
|
||||
SPNG_FILTER_CHOICE,
|
||||
SPNG_CHUNK_COUNT_LIMIT,
|
||||
SPNG_ENCODE_TO_BUFFER,
|
||||
};
|
||||
|
||||
typedef void* SPNG_CDECL spng_malloc_fn(size_t size);
|
||||
typedef void* SPNG_CDECL spng_realloc_fn(void* ptr, size_t size);
|
||||
typedef void* SPNG_CDECL spng_calloc_fn(size_t count, size_t size);
|
||||
typedef void SPNG_CDECL spng_free_fn(void* ptr);
|
||||
|
||||
struct spng_alloc
|
||||
{
|
||||
spng_malloc_fn *malloc_fn;
|
||||
spng_realloc_fn *realloc_fn;
|
||||
spng_calloc_fn *calloc_fn;
|
||||
spng_free_fn *free_fn;
|
||||
};
|
||||
|
||||
struct spng_row_info
|
||||
{
|
||||
uint32_t scanline_idx;
|
||||
uint32_t row_num; /* deinterlaced row index */
|
||||
int pass;
|
||||
uint8_t filter;
|
||||
};
|
||||
|
||||
typedef struct spng_ctx spng_ctx;
|
||||
|
||||
typedef int spng_read_fn(spng_ctx *ctx, void *user, void *dest, size_t length);
|
||||
typedef int spng_write_fn(spng_ctx *ctx, void *user, void *src, size_t length);
|
||||
|
||||
typedef int spng_rw_fn(spng_ctx *ctx, void *user, void *dst_src, size_t length);
|
||||
|
||||
SPNG_API spng_ctx *spng_ctx_new(int flags);
|
||||
SPNG_API spng_ctx *spng_ctx_new2(struct spng_alloc *alloc, int flags);
|
||||
SPNG_API void spng_ctx_free(spng_ctx *ctx);
|
||||
|
||||
SPNG_API int spng_set_png_buffer(spng_ctx *ctx, const void *buf, size_t size);
|
||||
SPNG_API int spng_set_png_stream(spng_ctx *ctx, spng_rw_fn *rw_func, void *user);
|
||||
SPNG_API int spng_set_png_file(spng_ctx *ctx, FILE *file);
|
||||
|
||||
SPNG_API void *spng_get_png_buffer(spng_ctx *ctx, size_t *len, int *error);
|
||||
|
||||
SPNG_API int spng_set_image_limits(spng_ctx *ctx, uint32_t width, uint32_t height);
|
||||
SPNG_API int spng_get_image_limits(spng_ctx *ctx, uint32_t *width, uint32_t *height);
|
||||
|
||||
SPNG_API int spng_set_chunk_limits(spng_ctx *ctx, size_t chunk_size, size_t cache_size);
|
||||
SPNG_API int spng_get_chunk_limits(spng_ctx *ctx, size_t *chunk_size, size_t *cache_size);
|
||||
|
||||
SPNG_API int spng_set_crc_action(spng_ctx *ctx, int critical, int ancillary);
|
||||
|
||||
SPNG_API int spng_set_option(spng_ctx *ctx, enum spng_option option, int value);
|
||||
SPNG_API int spng_get_option(spng_ctx *ctx, enum spng_option option, int *value);
|
||||
|
||||
SPNG_API int spng_decoded_image_size(spng_ctx *ctx, int fmt, size_t *len);
|
||||
|
||||
/* Decode */
|
||||
SPNG_API int spng_decode_image(spng_ctx *ctx, void *out, size_t len, int fmt, int flags);
|
||||
|
||||
/* Progressive decode */
|
||||
SPNG_API int spng_decode_scanline(spng_ctx *ctx, void *out, size_t len);
|
||||
SPNG_API int spng_decode_row(spng_ctx *ctx, void *out, size_t len);
|
||||
SPNG_API int spng_decode_chunks(spng_ctx *ctx);
|
||||
|
||||
/* Encode/decode */
|
||||
SPNG_API int spng_get_row_info(spng_ctx *ctx, struct spng_row_info *row_info);
|
||||
|
||||
/* Encode */
|
||||
SPNG_API int spng_encode_image(spng_ctx *ctx, const void *img, size_t len, int fmt, int flags);
|
||||
|
||||
/* Progressive encode */
|
||||
SPNG_API int spng_encode_scanline(spng_ctx *ctx, const void *scanline, size_t len);
|
||||
SPNG_API int spng_encode_row(spng_ctx *ctx, const void *row, size_t len);
|
||||
SPNG_API int spng_encode_chunks(spng_ctx *ctx);
|
||||
|
||||
SPNG_API int spng_get_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
|
||||
SPNG_API int spng_get_plte(spng_ctx *ctx, struct spng_plte *plte);
|
||||
SPNG_API int spng_get_trns(spng_ctx *ctx, struct spng_trns *trns);
|
||||
SPNG_API int spng_get_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
|
||||
SPNG_API int spng_get_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
|
||||
SPNG_API int spng_get_gama(spng_ctx *ctx, double *gamma);
|
||||
SPNG_API int spng_get_gama_int(spng_ctx *ctx, uint32_t *gama_int);
|
||||
SPNG_API int spng_get_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
|
||||
SPNG_API int spng_get_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
|
||||
SPNG_API int spng_get_srgb(spng_ctx *ctx, uint8_t *rendering_intent);
|
||||
SPNG_API int spng_get_text(spng_ctx *ctx, struct spng_text *text, uint32_t *n_text);
|
||||
SPNG_API int spng_get_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
|
||||
SPNG_API int spng_get_hist(spng_ctx *ctx, struct spng_hist *hist);
|
||||
SPNG_API int spng_get_phys(spng_ctx *ctx, struct spng_phys *phys);
|
||||
SPNG_API int spng_get_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t *n_splt);
|
||||
SPNG_API int spng_get_time(spng_ctx *ctx, struct spng_time *time);
|
||||
SPNG_API int spng_get_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t *n_chunks);
|
||||
|
||||
/* Official extensions */
|
||||
SPNG_API int spng_get_offs(spng_ctx *ctx, struct spng_offs *offs);
|
||||
SPNG_API int spng_get_exif(spng_ctx *ctx, struct spng_exif *exif);
|
||||
|
||||
|
||||
SPNG_API int spng_set_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
|
||||
SPNG_API int spng_set_plte(spng_ctx *ctx, struct spng_plte *plte);
|
||||
SPNG_API int spng_set_trns(spng_ctx *ctx, struct spng_trns *trns);
|
||||
SPNG_API int spng_set_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
|
||||
SPNG_API int spng_set_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
|
||||
SPNG_API int spng_set_gama(spng_ctx *ctx, double gamma);
|
||||
SPNG_API int spng_set_gama_int(spng_ctx *ctx, uint32_t gamma);
|
||||
SPNG_API int spng_set_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
|
||||
SPNG_API int spng_set_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
|
||||
SPNG_API int spng_set_srgb(spng_ctx *ctx, uint8_t rendering_intent);
|
||||
SPNG_API int spng_set_text(spng_ctx *ctx, struct spng_text *text, uint32_t n_text);
|
||||
SPNG_API int spng_set_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
|
||||
SPNG_API int spng_set_hist(spng_ctx *ctx, struct spng_hist *hist);
|
||||
SPNG_API int spng_set_phys(spng_ctx *ctx, struct spng_phys *phys);
|
||||
SPNG_API int spng_set_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t n_splt);
|
||||
SPNG_API int spng_set_time(spng_ctx *ctx, struct spng_time *time);
|
||||
SPNG_API int spng_set_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t n_chunks);
|
||||
|
||||
/* Official extensions */
|
||||
SPNG_API int spng_set_offs(spng_ctx *ctx, struct spng_offs *offs);
|
||||
SPNG_API int spng_set_exif(spng_ctx *ctx, struct spng_exif *exif);
|
||||
|
||||
|
||||
SPNG_API const char *spng_strerror(int err);
|
||||
SPNG_API const char *spng_version_string(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SPNG_H */
|
21
tools/assets/n64texconv/src/LICENSE
Normal file
21
tools/assets/n64texconv/src/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2025 ZeldaRET
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
137
tools/assets/n64texconv/src/app/main.c
Normal file
137
tools/assets/n64texconv/src/app/main.c
Normal file
|
@ -0,0 +1,137 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define NORETURN __attribute__((noreturn))
|
||||
#define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#define strequ(s1, s2) (strcmp((s1), (s2)) == 0)
|
||||
|
||||
#include "../libn64texconv/n64texconv.h"
|
||||
#include "../libn64texconv/jfif.h"
|
||||
|
||||
static bool
|
||||
is_regular_file(const char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
stat(path, &sb);
|
||||
return S_ISREG(sb.st_mode);
|
||||
}
|
||||
|
||||
static NORETURN void
|
||||
usage(const char *progname)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"n64texconv: Convert an input png to N64 data in the desired format.\n"
|
||||
"Usage: %s <type> <in.png> <out.c> [pal_out.c]\n"
|
||||
" Valid types: i4 / i8 / ci4 / ci8 / ia4 / ia8 / ia16 / rgba16 / rgba32 / JFIF\n",
|
||||
progname);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static const struct fmt_info {
|
||||
const char *name;
|
||||
int fmt;
|
||||
int siz;
|
||||
} fmt_map[] = {
|
||||
// clang-format off
|
||||
{ "i4", G_IM_FMT_I, G_IM_SIZ_4b, },
|
||||
{ "i8", G_IM_FMT_I, G_IM_SIZ_8b, },
|
||||
{ "ci4", G_IM_FMT_CI, G_IM_SIZ_4b, },
|
||||
{ "ci8", G_IM_FMT_CI, G_IM_SIZ_8b, },
|
||||
{ "ia4", G_IM_FMT_IA, G_IM_SIZ_4b, },
|
||||
{ "ia8", G_IM_FMT_IA, G_IM_SIZ_8b, },
|
||||
{ "ia16", G_IM_FMT_IA, G_IM_SIZ_16b, },
|
||||
{ "rgba16", G_IM_FMT_RGBA, G_IM_SIZ_16b, },
|
||||
{ "rgba32", G_IM_FMT_RGBA, G_IM_SIZ_32b, },
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *progname = argv[0];
|
||||
const char *fmt;
|
||||
const char *array_fmt;
|
||||
const char *in;
|
||||
const char *out;
|
||||
const char *pal_out;
|
||||
|
||||
if (argc < 5)
|
||||
usage(progname);
|
||||
|
||||
fmt = argv[1];
|
||||
array_fmt = argv[2];
|
||||
in = argv[3];
|
||||
out = argv[4];
|
||||
pal_out = (argc > 5) ? argv[5] : NULL;
|
||||
|
||||
unsigned int byte_width = (strequ(array_fmt, "u32") ? 4 : 8);
|
||||
|
||||
if (!is_regular_file(in)) {
|
||||
fprintf(stderr, "Could not open input file %s\n", in);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (strequ(fmt, "JFIF")) {
|
||||
struct JFIF *jfif = jfif_fromfile(in, JFIF_BUFFER_SIZE);
|
||||
if (jfif == NULL) {
|
||||
fprintf(stderr, "Could not open input file %s\n", in);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (jfif_to_c_file(out, jfif, JFIF_BUFFER_SIZE)) {
|
||||
fprintf(stderr, "Could not save output C file %s\n", out);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
jfif_free(jfif);
|
||||
} else {
|
||||
int rv;
|
||||
const struct fmt_info *fmt_info = NULL;
|
||||
for (size_t i = 0; i < ARRLEN(fmt_map); i++) {
|
||||
if (strequ(fmt, fmt_map[i].name)) {
|
||||
fmt_info = &fmt_map[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fmt_info == NULL) {
|
||||
fprintf(stderr, "Error: Invalid fmt %s\n", fmt);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
struct n64_image *img = n64texconv_image_from_png(in, fmt_info->fmt, fmt_info->siz, G_IM_FMT_RGBA);
|
||||
if (img == NULL) {
|
||||
fprintf(stderr, "Could not open input file %s\n", in);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (img->pal != NULL) {
|
||||
if (pal_out == NULL) {
|
||||
fprintf(stderr, "Input file %s is color indexed, a palette output C file must be provided.\n", in);
|
||||
usage(progname);
|
||||
}
|
||||
|
||||
if (rv = n64texconv_palette_to_c_file(pal_out, img->pal, false, byte_width), rv != 0) {
|
||||
fprintf(stderr, "Could not save output C file %s (error %d)\n", pal_out, rv);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (rv = n64texconv_image_to_c_file(out, img, false, false, byte_width), rv != 0) {
|
||||
fprintf(stderr, "Could not save output C file %s (error %d)\n", out, rv);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (img->pal != NULL)
|
||||
n64texconv_palette_free(img->pal);
|
||||
n64texconv_image_free(img);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
130
tools/assets/n64texconv/src/libn64texconv/bin2c.c
Normal file
130
tools/assets/n64texconv/src/libn64texconv/bin2c.c
Normal file
|
@ -0,0 +1,130 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bin2c.h"
|
||||
#include "endian.h"
|
||||
|
||||
#define BYTES_PER_ROW 32
|
||||
#define LINE_MASK (BYTES_PER_ROW - 1)
|
||||
|
||||
int
|
||||
bin2c(char **out, size_t *size_out, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width)
|
||||
{
|
||||
assert(out != NULL);
|
||||
assert(size_out != NULL);
|
||||
assert(bin != NULL);
|
||||
|
||||
if (byte_width != 1 && byte_width != 2 && byte_width != 4 && byte_width != 8)
|
||||
return -2;
|
||||
|
||||
size_t end_size = (pad_to_size > size) ? pad_to_size : size;
|
||||
|
||||
if ((end_size & (byte_width - 1)) != 0)
|
||||
return -3;
|
||||
|
||||
size_t size_out_ = (1 + 1 + 2 * byte_width + 1 + 1) * ((end_size + byte_width - 1) / byte_width) + 2;
|
||||
char *out_ = malloc(size_out_);
|
||||
if (out_ == NULL)
|
||||
return -1;
|
||||
|
||||
char *pos = out_;
|
||||
bool was_newline = false;
|
||||
for (size_t p = 0; p < size; p += byte_width) {
|
||||
size_t rem = byte_width;
|
||||
if (rem > size - p) // For any remaining unaligned data, rest will be padded with 0
|
||||
rem = size - p;
|
||||
|
||||
// Read input
|
||||
uint64_t d = 0;
|
||||
memcpy(&d, &((uint8_t *)bin)[p], rem);
|
||||
|
||||
// Byteswap + shift
|
||||
d = be64toh(d) >> (64 - 8 * byte_width);
|
||||
|
||||
// Write output
|
||||
was_newline = (((p + byte_width) & LINE_MASK) == 0);
|
||||
char end = was_newline ? '\n' : ' ';
|
||||
pos += sprintf(pos, "0x%0*" PRIX64 ",%c", 2 * byte_width, d, end);
|
||||
}
|
||||
|
||||
for (size_t p = (size + byte_width - 1) & ~(byte_width - 1); p < pad_to_size; p += byte_width) {
|
||||
was_newline = (((p + byte_width) & LINE_MASK) == 0);
|
||||
char end = was_newline ? '\n' : ' ';
|
||||
pos += sprintf(pos, "0x%0*" PRIX64 ",%c", 2 * byte_width, (uint64_t)0, end);
|
||||
}
|
||||
|
||||
if (!was_newline)
|
||||
*pos++ = '\n';
|
||||
|
||||
*pos++ = '\0';
|
||||
|
||||
*out = out_;
|
||||
*size_out = size_out_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bin2c_file(const char *out_path, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width)
|
||||
{
|
||||
assert(out_path != NULL);
|
||||
assert(bin != NULL);
|
||||
|
||||
if (byte_width != 1 && byte_width != 2 && byte_width != 4 && byte_width != 8)
|
||||
return -2;
|
||||
|
||||
size_t end_size = (pad_to_size > size) ? pad_to_size : size;
|
||||
|
||||
if ((end_size & (byte_width - 1)) != 0)
|
||||
return -3;
|
||||
|
||||
FILE *of = fopen(out_path, "w");
|
||||
if (of == NULL)
|
||||
return -1;
|
||||
|
||||
bool was_newline = false;
|
||||
for (size_t p = 0; p < size; p += byte_width) {
|
||||
size_t rem = byte_width;
|
||||
if (rem > size - p) // For any remaining unaligned data, rest will be padded with 0
|
||||
rem = size - p;
|
||||
|
||||
// Read input
|
||||
uint64_t d = 0;
|
||||
memcpy(&d, &((uint8_t *)bin)[p], rem);
|
||||
|
||||
// Byteswap + shift
|
||||
d = be64toh(d) >> (64 - 8 * byte_width);
|
||||
|
||||
// Write output
|
||||
was_newline = (((p + byte_width) & LINE_MASK) == 0);
|
||||
char end = was_newline ? '\n' : ' ';
|
||||
|
||||
if (fprintf(of, "0x%0*" PRIX64 ",%c", 2 * byte_width, d, end) < 0)
|
||||
goto error_post_open;
|
||||
}
|
||||
|
||||
for (size_t p = (size + byte_width - 1) & ~(byte_width - 1); p < pad_to_size; p += byte_width) {
|
||||
was_newline = (((p + byte_width) & LINE_MASK) == 0);
|
||||
char end = was_newline ? '\n' : ' ';
|
||||
if (fprintf(of, "0x%0*" PRIX64 ",%c", 2 * byte_width, (uint64_t)0, end) < 0)
|
||||
goto error_post_open;
|
||||
}
|
||||
|
||||
if (!was_newline)
|
||||
fputs("\n", of);
|
||||
|
||||
fclose(of);
|
||||
return 0;
|
||||
error_post_open:
|
||||
fclose(of);
|
||||
if (remove(out_path) != 0)
|
||||
fprintf(stderr, "error calling remove(\"%s\"): %s", out_path, strerror(errno));
|
||||
return -4;
|
||||
}
|
14
tools/assets/n64texconv/src/libn64texconv/bin2c.h
Normal file
14
tools/assets/n64texconv/src/libn64texconv/bin2c.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#ifndef BIN2C_H
|
||||
#define BIN2C_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int
|
||||
bin2c(char **out, size_t *size_out, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width);
|
||||
|
||||
int
|
||||
bin2c_file(const char *out_path, void *bin, size_t size, size_t pad_to_size, unsigned int byte_width);
|
||||
|
||||
#endif
|
69
tools/assets/n64texconv/src/libn64texconv/endian.h
Normal file
69
tools/assets/n64texconv/src/libn64texconv/endian.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#ifndef ENDIAN_H
|
||||
#define ENDIAN_H
|
||||
|
||||
#if defined(__linux__) || defined(__CYGWIN__)
|
||||
#include <endian.h>
|
||||
#elif defined(__APPLE__)
|
||||
#include <libkern/OSByteOrder.h>
|
||||
|
||||
#define htobe16(x) OSSwapHostToBigInt16(x)
|
||||
#define htole16(x) OSSwapHostToLittleInt16(x)
|
||||
#define be16toh(x) OSSwapBigToHostInt16(x)
|
||||
#define le16toh(x) OSSwapLittleToHostInt16(x)
|
||||
|
||||
#define htobe32(x) OSSwapHostToBigInt32(x)
|
||||
#define htole32(x) OSSwapHostToLittleInt32(x)
|
||||
#define be32toh(x) OSSwapBigToHostInt32(x)
|
||||
#define le32toh(x) OSSwapLittleToHostInt32(x)
|
||||
|
||||
#define htobe64(x) OSSwapHostToBigInt64(x)
|
||||
#define htole64(x) OSSwapHostToLittleInt64(x)
|
||||
#define be64toh(x) OSSwapBigToHostInt64(x)
|
||||
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||
#else
|
||||
|
||||
#if !defined(__BYTE_ORDER__)
|
||||
#error "No endian define provided by compiler"
|
||||
#endif
|
||||
|
||||
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
|
||||
#define htobe16(x) (x)
|
||||
#define htole16(x) __builtin_bswap16(x)
|
||||
#define be16toh(x) (x)
|
||||
#define le16toh(x) __builtin_bswap16(x)
|
||||
|
||||
#define htobe32(x) (x)
|
||||
#define htole32(x) __builtin_bswap32(x)
|
||||
#define be32toh(x) (x)
|
||||
#define le32toh(x) __builtin_bswap32(x)
|
||||
|
||||
#define htobe64(x) (x)
|
||||
#define htole64(x) __builtin_bswap64(x)
|
||||
#define be64toh(x) (x)
|
||||
#define le64toh(x) __builtin_bswap64(x)
|
||||
|
||||
#else
|
||||
|
||||
#define htobe16(x) __builtin_bswap16(x)
|
||||
#define htole16(x) (x)
|
||||
#define be16toh(x) __builtin_bswap16(x)
|
||||
#define le16toh(x) (x)
|
||||
|
||||
#define htobe32(x) __builtin_bswap32(x)
|
||||
#define htole32(x) (x)
|
||||
#define be32toh(x) __builtin_bswap32(x)
|
||||
#define le32toh(x) (x)
|
||||
|
||||
#define htobe64(x) __builtin_bswap64(x)
|
||||
#define htole64(x) (x)
|
||||
#define be64toh(x) __builtin_bswap64(x)
|
||||
#define le64toh(x) (x)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
76
tools/assets/n64texconv/src/libn64texconv/jfif.c
Normal file
76
tools/assets/n64texconv/src/libn64texconv/jfif.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "endian.h"
|
||||
#include "bin2c.h"
|
||||
#include "jfif.h"
|
||||
|
||||
struct JFIF *
|
||||
jfif_fromfile(const char *path, size_t max_size)
|
||||
{
|
||||
assert(path != NULL);
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (f == NULL)
|
||||
return NULL;
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
size_t data_size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
struct JFIF *jfif = malloc(((sizeof(struct JFIF) + 3) & ~3) + data_size);
|
||||
if (jfif != NULL) {
|
||||
jfif->data = (void *)(jfif + 1);
|
||||
jfif->data_size = data_size;
|
||||
|
||||
if (fread(jfif->data, 1, data_size, f) != data_size) {
|
||||
free(jfif);
|
||||
jfif = NULL;
|
||||
} else {
|
||||
uint8_t *data8 = jfif->data;
|
||||
uint16_t *data16 = jfif->data;
|
||||
uint32_t *data32 = jfif->data;
|
||||
|
||||
if (be32toh(data32[0]) != 0xFFD8FFE0)
|
||||
printf("[Warning] Missing JPEG marker\n");
|
||||
if (data8[6] != 'J' || data8[7] != 'F' || data8[8] != 'I' || data8[9] != 'F')
|
||||
printf("[Warning] Not JFIF\n");
|
||||
if (data8[11] != 0x01 || data8[12] != 0x01)
|
||||
printf("[Warning] Not JFIF version 1.01\n");
|
||||
if (be16toh(data16[10]) != 0xFFDB)
|
||||
printf("[Warning] Data before DQT\n");
|
||||
if (jfif->data_size > max_size)
|
||||
printf("[Warning] JFIF image too large\n");
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return jfif;
|
||||
}
|
||||
|
||||
void
|
||||
jfif_free(struct JFIF *jfif)
|
||||
{
|
||||
assert(jfif != NULL);
|
||||
free(jfif);
|
||||
}
|
||||
|
||||
int
|
||||
jfif_to_c(char **out, size_t *size_out, struct JFIF *jfif, size_t pad_to_size)
|
||||
{
|
||||
assert(out != NULL);
|
||||
assert(size_out != NULL);
|
||||
return bin2c(out, size_out, jfif->data, jfif->data_size, pad_to_size, 8);
|
||||
}
|
||||
|
||||
int
|
||||
jfif_to_c_file(const char *out_path, struct JFIF *jfif, size_t pad_to_size)
|
||||
{
|
||||
assert(out_path != NULL);
|
||||
assert(jfif != NULL);
|
||||
return bin2c_file(out_path, jfif->data, jfif->data_size, pad_to_size, 8);
|
||||
}
|
28
tools/assets/n64texconv/src/libn64texconv/jfif.h
Normal file
28
tools/assets/n64texconv/src/libn64texconv/jfif.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#ifndef JFIF_H
|
||||
#define JFIF_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct JFIF {
|
||||
void *data;
|
||||
size_t data_size;
|
||||
};
|
||||
|
||||
#define JFIF_BUFFER_SIZE (320 * 240 * sizeof(uint16_t))
|
||||
|
||||
struct JFIF *
|
||||
jfif_fromfile(const char *path, size_t max_size);
|
||||
|
||||
void
|
||||
jfif_free(struct JFIF *jfif);
|
||||
|
||||
int
|
||||
jfif_to_c(char **out, size_t *size_out, struct JFIF *jfif, size_t pad_to_size);
|
||||
|
||||
int
|
||||
jfif_to_c_file(const char *out_path, struct JFIF *jfif, size_t pad_to_size);
|
||||
|
||||
#endif
|
1302
tools/assets/n64texconv/src/libn64texconv/n64texconv.c
Normal file
1302
tools/assets/n64texconv/src/libn64texconv/n64texconv.c
Normal file
File diff suppressed because it is too large
Load diff
122
tools/assets/n64texconv/src/libn64texconv/n64texconv.h
Normal file
122
tools/assets/n64texconv/src/libn64texconv/n64texconv.h
Normal file
|
@ -0,0 +1,122 @@
|
|||
/* SPDX-FileCopyrightText: Copyright (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#ifndef N64TEXCONV_H
|
||||
#define N64TEXCONV_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define FMT_NONE -1
|
||||
#define FMT_MAX 5
|
||||
#define G_IM_FMT_RGBA 0
|
||||
#define G_IM_FMT_YUV 1
|
||||
#define G_IM_FMT_CI 2
|
||||
#define G_IM_FMT_IA 3
|
||||
#define G_IM_FMT_I 4
|
||||
|
||||
#define SIZ_NONE -1
|
||||
#define SIZ_MAX 4
|
||||
#define G_IM_SIZ_4b 0
|
||||
#define G_IM_SIZ_8b 1
|
||||
#define G_IM_SIZ_16b 2
|
||||
#define G_IM_SIZ_32b 3
|
||||
|
||||
struct color {
|
||||
union {
|
||||
struct {
|
||||
uint8_t r, g, b, a;
|
||||
};
|
||||
uint32_t w;
|
||||
};
|
||||
};
|
||||
|
||||
static inline __attribute__((always_inline)) size_t
|
||||
texel_size_bytes(size_t ntexels, int siz)
|
||||
{
|
||||
return (siz == G_IM_SIZ_4b) ? (ntexels / 2) : (ntexels * ((1 << (unsigned)siz) >> 1));
|
||||
}
|
||||
|
||||
struct n64_palette {
|
||||
struct color *texels;
|
||||
int fmt;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
struct n64_palette *
|
||||
n64texconv_palette_new(size_t count, int fmt);
|
||||
|
||||
void
|
||||
n64texconv_palette_free(struct n64_palette *pal);
|
||||
|
||||
struct n64_palette *
|
||||
n64texconv_palette_copy(struct n64_palette *pal);
|
||||
|
||||
struct n64_palette *
|
||||
n64texconv_palette_reformat(struct n64_palette *pal, int fmt);
|
||||
|
||||
struct n64_palette *
|
||||
n64texconv_palette_from_png(const char *path, int fmt);
|
||||
|
||||
struct n64_palette *
|
||||
n64texconv_palette_from_bin(void *data, size_t count, int fmt);
|
||||
|
||||
int
|
||||
n64texconv_palette_to_png(const char *outpath, struct n64_palette *pal);
|
||||
|
||||
void *
|
||||
n64texconv_palette_to_bin(struct n64_palette *pal, bool pad_to_8b);
|
||||
|
||||
int
|
||||
n64texconv_palette_to_c(char **out, size_t *size_out, struct n64_palette *pal, bool pad_to_8b, unsigned int byte_width);
|
||||
|
||||
int
|
||||
n64texconv_palette_to_c_file(const char *out_path, struct n64_palette *pal, bool pad_to_8b, unsigned int byte_width);
|
||||
|
||||
struct n64_image {
|
||||
size_t width;
|
||||
size_t height;
|
||||
int fmt;
|
||||
int siz;
|
||||
struct n64_palette *pal;
|
||||
struct color *texels;
|
||||
uint8_t *color_indices;
|
||||
};
|
||||
|
||||
struct n64_image *
|
||||
n64texconv_image_new(size_t width, size_t height, int fmt, int siz, struct n64_palette *pal);
|
||||
|
||||
void
|
||||
n64texconv_image_free(struct n64_image *img);
|
||||
|
||||
struct n64_image *
|
||||
n64texconv_image_copy(struct n64_image *img);
|
||||
|
||||
struct n64_image *
|
||||
n64texconv_image_from_png(const char *path, int fmt, int siz, int pal_fmt);
|
||||
|
||||
struct n64_image *
|
||||
n64texconv_image_from_bin(void *data, size_t width, size_t height, int fmt, int siz, struct n64_palette *pal,
|
||||
bool preswapped);
|
||||
|
||||
struct n64_image *
|
||||
n64texconv_image_reformat(struct n64_image *img, int fmt, int siz, struct n64_palette *pal);
|
||||
|
||||
int
|
||||
n64texconv_image_to_png(const char *outpath, struct n64_image *img, bool intensity_alpha);
|
||||
|
||||
void *
|
||||
n64texconv_image_to_bin(struct n64_image *img, bool pad_to_8b, bool preswap);
|
||||
|
||||
int
|
||||
n64texconv_image_to_c(char **out, size_t *size_out, struct n64_image *img, bool pad_to_8b, bool preswap,
|
||||
unsigned int byte_width);
|
||||
|
||||
int
|
||||
n64texconv_image_to_c_file(const char *out_path, struct n64_image *img, bool pad_to_8b, bool preswap,
|
||||
unsigned int byte_width);
|
||||
|
||||
const char *
|
||||
n64texconv_png_extension(struct n64_image *img);
|
||||
|
||||
#endif
|
174
tools/bin2c.c
Normal file
174
tools/bin2c.c
Normal file
|
@ -0,0 +1,174 @@
|
|||
/* SPDX-FileCopyrightText: (C) 2025 ZeldaRET */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "endian.h"
|
||||
|
||||
#define NORETURN __attribute__((noreturn))
|
||||
|
||||
static void NORETURN
|
||||
verror(const char *fmt, va_list args)
|
||||
{
|
||||
fputs("\x1b[91merror\x1b[97m: ", stderr);
|
||||
vfprintf(stderr, fmt, args);
|
||||
fputs("\x1b[0m", stderr);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static void NORETURN
|
||||
error(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
verror(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static void NORETURN
|
||||
error_post_open(const char *path_to_rm, FILE *file_to_rm, const char *fmt, ...)
|
||||
{
|
||||
// cleanup output file
|
||||
fclose(file_to_rm);
|
||||
if (remove(path_to_rm) != 0)
|
||||
fprintf(stderr, "error calling remove(): %s", strerror(errno));
|
||||
|
||||
// error as normal
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
verror(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static void NORETURN
|
||||
usage(const char *progname)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: %s -t <fmt> [-pad] input.bin output.inc.c" "\n"
|
||||
" fmt must be one of { 1, 2, 4, 8 }" "\n"
|
||||
" if pad, align to fmt by filling with 0s, otherwise error if input is not aligned" "\n",
|
||||
progname);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#define BYTES_PER_ROW 32
|
||||
#define LINE_MASK (BYTES_PER_ROW - 1)
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *progname = argv[0];
|
||||
const char *inpath = NULL;
|
||||
const char *outpath = NULL;
|
||||
const char *fmt_opt = NULL;
|
||||
int fmt = 0;
|
||||
bool pad = false;
|
||||
|
||||
// Collect arguments
|
||||
|
||||
int narg = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (argv[i][0] == '-') {
|
||||
if (strcmp(argv[i], "-t") == 0) {
|
||||
if (++i == argc)
|
||||
usage(progname);
|
||||
fmt_opt = argv[i];
|
||||
char *end;
|
||||
fmt = strtol(argv[i], &end, 10);
|
||||
if (end != &argv[i][strlen(argv[i])])
|
||||
error("Invalid base 10 integer %s\n", argv[i]);
|
||||
} else if (strcmp(argv[i], "-pad") == 0) {
|
||||
pad = true;
|
||||
} else {
|
||||
usage(progname);
|
||||
}
|
||||
} else {
|
||||
switch (narg) {
|
||||
case 0:
|
||||
inpath = argv[i];
|
||||
break;
|
||||
case 1:
|
||||
outpath = argv[i];
|
||||
break;
|
||||
default:
|
||||
usage(progname);
|
||||
break;
|
||||
}
|
||||
narg++;
|
||||
}
|
||||
}
|
||||
|
||||
// Check arguments
|
||||
|
||||
if (inpath == NULL || outpath == NULL || fmt_opt == NULL)
|
||||
usage(progname);
|
||||
|
||||
if (fmt != 1 && fmt != 2 && fmt != 4 && fmt != 8)
|
||||
error("Invalid fmt option '%s'. Valid options: [1, 2, 4, 8]\n", fmt_opt);
|
||||
|
||||
// Open the input binary file
|
||||
|
||||
FILE *infile = fopen(inpath, "rb");
|
||||
if (infile == NULL)
|
||||
error("Failed to open input file '%s' for reading: %s\n", inpath, strerror(errno));
|
||||
|
||||
// Get size
|
||||
|
||||
if (fseek(infile, 0, SEEK_END) != 0)
|
||||
error("Could not ascertain input file size, could not seek to end: %s\n", strerror(errno));
|
||||
size_t file_size = ftell(infile);
|
||||
if (fseek(infile, 0, SEEK_SET) != 0)
|
||||
error("Could not ascertain input file size, could not seek to start: %s\n", strerror(errno));
|
||||
|
||||
// Check alignment
|
||||
|
||||
if ((file_size & (fmt - 1)) != 0 && !pad) {
|
||||
// Not aligned to data size and don't pad, error
|
||||
error("Input file '%s' size (%lu) is not aligned to %d bytes\n", inpath, file_size, fmt);
|
||||
}
|
||||
|
||||
// Open the output text file
|
||||
|
||||
FILE *outfile = fopen(outpath, "w");
|
||||
if (outfile == NULL)
|
||||
error("Failed to open output file '%s' for writing: %s\n", outpath, strerror(errno));
|
||||
|
||||
// Write data. If the input binary size was not aligned we either don't get this far or the option
|
||||
// to pad with 0s is set.
|
||||
|
||||
bool was_newline = false;
|
||||
for (size_t p = 0; p < file_size; p += fmt) {
|
||||
size_t rem = fmt;
|
||||
if (rem > file_size - p) // For any remaining unaligned data, rest will be padded with 0
|
||||
rem = file_size - p;
|
||||
|
||||
// Read input
|
||||
uint64_t d = 0;
|
||||
if (fread(&d, 1, rem, infile) != rem)
|
||||
error_post_open(outpath, outfile, "Error reading from input file '%s': %s\n", inpath, strerror(errno));
|
||||
|
||||
// Byteswap + shift
|
||||
d = be64toh(d) >> (64 - 8 * fmt);
|
||||
|
||||
// Write output
|
||||
bool was_newline = (((p + fmt) & LINE_MASK) == 0);
|
||||
char end = was_newline ? '\n' : ' ';
|
||||
if (fprintf(outfile, "0x%0*" PRIX64 ",%c", 2 * fmt, d, end) < 0)
|
||||
error_post_open(outpath, outfile, "Error writing to output file '%s': %s\n", outpath, strerror(errno));
|
||||
}
|
||||
if (!was_newline) {
|
||||
if (fputs("\n", outfile) == EOF)
|
||||
error_post_open(outpath, outfile, "Error writing to output file '%s': %s\n", outpath, strerror(errno));
|
||||
}
|
||||
|
||||
fclose(infile);
|
||||
fclose(outfile);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
67
tools/endian.h
Normal file
67
tools/endian.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
#ifndef ENDIAN_H_
|
||||
#define ENDIAN_H_
|
||||
|
||||
#if defined(__linux__) || defined(__CYGWIN__)
|
||||
#include <endian.h>
|
||||
#elif defined(__APPLE__)
|
||||
#include <libkern/OSByteOrder.h>
|
||||
|
||||
#define htobe16(x) OSSwapHostToBigInt16(x)
|
||||
#define htole16(x) OSSwapHostToLittleInt16(x)
|
||||
#define be16toh(x) OSSwapBigToHostInt16(x)
|
||||
#define le16toh(x) OSSwapLittleToHostInt16(x)
|
||||
|
||||
#define htobe32(x) OSSwapHostToBigInt32(x)
|
||||
#define htole32(x) OSSwapHostToLittleInt32(x)
|
||||
#define be32toh(x) OSSwapBigToHostInt32(x)
|
||||
#define le32toh(x) OSSwapLittleToHostInt32(x)
|
||||
|
||||
#define htobe64(x) OSSwapHostToBigInt64(x)
|
||||
#define htole64(x) OSSwapHostToLittleInt64(x)
|
||||
#define be64toh(x) OSSwapBigToHostInt64(x)
|
||||
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||
#else
|
||||
|
||||
#if !defined(__BYTE_ORDER__)
|
||||
#error "No endian define provided by compiler"
|
||||
#endif
|
||||
|
||||
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
|
||||
#define htobe16(x) (x)
|
||||
#define htole16(x) __builtin_bswap16(x)
|
||||
#define be16toh(x) (x)
|
||||
#define le16toh(x) __builtin_bswap16(x)
|
||||
|
||||
#define htobe32(x) (x)
|
||||
#define htole32(x) __builtin_bswap32(x)
|
||||
#define be32toh(x) (x)
|
||||
#define le32toh(x) __builtin_bswap32(x)
|
||||
|
||||
#define htobe64(x) (x)
|
||||
#define htole64(x) __builtin_bswap64(x)
|
||||
#define be64toh(x) (x)
|
||||
#define le64toh(x) __builtin_bswap64(x)
|
||||
|
||||
#else
|
||||
|
||||
#define htobe16(x) __builtin_bswap16(x)
|
||||
#define htole16(x) (x)
|
||||
#define be16toh(x) __builtin_bswap16(x)
|
||||
#define le16toh(x) (x)
|
||||
|
||||
#define htobe32(x) __builtin_bswap32(x)
|
||||
#define htole32(x) (x)
|
||||
#define be32toh(x) __builtin_bswap32(x)
|
||||
#define le32toh(x) (x)
|
||||
|
||||
#define htobe64(x) __builtin_bswap64(x)
|
||||
#define htole64(x) (x)
|
||||
#define be64toh(x) __builtin_bswap64(x)
|
||||
#define le64toh(x) (x)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue