commit 65eccee074ed92db07e92f0cdcb30f80861af793 Author: mappu Date: Sun Nov 20 13:38:15 2016 +1300 libimagequant (2.8-git-a425e83) GPLv3+ diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000..3514d66 --- /dev/null +++ b/COPYRIGHT @@ -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. + +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +libimagequant © 2009-2016 by Kornel Lesiński. + + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + 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. diff --git a/blur.c b/blur.c new file mode 100644 index 0000000..7f0a716 --- /dev/null +++ b/blur.c @@ -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 . +*/ + +#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); +} diff --git a/blur.h b/blur.h new file mode 100644 index 0000000..06ae8cb --- /dev/null +++ b/blur.h @@ -0,0 +1,4 @@ + +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); diff --git a/libimagequant.c b/libimagequant.c new file mode 100644 index 0000000..c17b93d --- /dev/null +++ b/libimagequant.c @@ -0,0 +1,1982 @@ +/* +** © 2009-2016 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 . +*/ +/* Copyright (C) 1989, 1991 by Jef Poskanzer. +** Copyright (C) 1997, 2000, 2002 by Greg Roelofs; based on an idea by +** Stefan Schneider. +** +** 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. +*/ + +#include +#include +#include +#include +#include +#include +#include + +#if !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199900L) && !(defined(_MSC_VER) && _MSC_VER >= 1800) +#error "This program requires C99, e.g. -std=c99 switch in GCC or it requires MSVC 18.0 or higher." +#error "Ignore torrent of syntax errors that may follow. It's only because compiler is set to use too old C version." +#endif + +#ifdef _OPENMP +#include +#else +#define omp_get_max_threads() 1 +#define omp_get_thread_num() 0 +#endif + +#include "libimagequant.h" + +#include "pam.h" +#include "mediancut.h" +#include "nearest.h" +#include "blur.h" +#include "viter.h" + +#define LIQ_HIGH_MEMORY_LIMIT (1<<26) /* avoid allocating buffers larger than 64MB */ + +// each structure has a pointer as a unique identifier that allows type checking at run time +static const char liq_attr_magic[] = "liq_attr"; +static const char liq_image_magic[] = "liq_image"; +static const char liq_result_magic[] = "liq_result"; +static const char liq_histogram_magic[] = "liq_histogram"; +static const char liq_remapping_result_magic[] = "liq_remapping_result"; +static const char liq_freed_magic[] = "free"; +#define CHECK_STRUCT_TYPE(attr, kind) liq_crash_if_invalid_handle_pointer_given((const liq_attr*)attr, kind ## _magic) +#define CHECK_USER_POINTER(ptr) liq_crash_if_invalid_pointer_given(ptr) + +struct liq_attr { + const char *magic_header; + void* (*malloc)(size_t); + void (*free)(void*); + + double target_mse, max_mse, voronoi_iteration_limit; + float min_opaque_val; + unsigned int max_colors, max_histogram_entries; + unsigned int min_posterization_output /* user setting */, min_posterization_input /* speed setting */; + unsigned int voronoi_iterations, feedback_loop_trials; + bool last_index_transparent, use_contrast_maps, use_dither_map, fast_palette; + unsigned char speed; + + unsigned char progress_stage1, progress_stage2, progress_stage3; + liq_progress_callback_function *progress_callback; + void *progress_callback_user_info; + + liq_log_callback_function *log_callback; + void *log_callback_user_info; + liq_log_flush_callback_function *log_flush_callback; + void *log_flush_callback_user_info; +}; + +struct liq_image { + const char *magic_header; + void* (*malloc)(size_t); + void (*free)(void*); + + f_pixel *f_pixels; + rgba_pixel **rows; + double gamma; + unsigned int width, height; + unsigned char *noise, *edges, *dither_map; + rgba_pixel *pixels, *temp_row; + f_pixel *temp_f_row; + liq_image_get_rgba_row_callback *row_callback; + void *row_callback_user_info; + float min_opaque_val; + 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; + bool use_dither_map; unsigned char progress_stage1; +} liq_remapping_result; + +struct liq_result { + const char *magic_header; + void* (*malloc)(size_t); + void (*free)(void*); + + liq_remapping_result *remapping; + colormap *palette; + liq_progress_callback_function *progress_callback; + void *progress_callback_user_info; + + liq_palette int_palette; + float dither_level; + double gamma, palette_error; + int min_posterization_output; + bool use_dither_map, fast_palette; +}; + +struct liq_histogram { + const char *magic_header; + void* (*malloc)(size_t); + void (*free)(void*); + + struct acolorhash_table *acht; + double gamma; + f_pixel fixed_colors[256]; + unsigned short fixed_colors_count; + unsigned short ignorebits; + bool had_image_added; +}; + +static void modify_alpha(liq_image *input_image, rgba_pixel *const row_pixels) LIQ_NONNULL; +static void contrast_maps(liq_image *image) LIQ_NONNULL; +static liq_error finalize_histogram(liq_histogram *input_hist, liq_attr *options, histogram **hist_output) LIQ_NONNULL; +static const rgba_pixel *liq_image_get_row_rgba(liq_image *input_image, unsigned int row) LIQ_NONNULL; +static const f_pixel *liq_image_get_row_f(liq_image *input_image, unsigned int row) LIQ_NONNULL; +static void liq_remapping_result_destroy(liq_remapping_result *result) LIQ_NONNULL; +static liq_error pngquant_quantize(histogram *hist, const liq_attr *options, const int fixed_colors_count, const f_pixel fixed_colors[], const double gamma, bool fixed_result_colors, liq_result **) LIQ_NONNULL; +static liq_error liq_histogram_quantize_internal(liq_histogram *input_hist, liq_attr *attr, bool fixed_result_colors, liq_result **result_output) LIQ_NONNULL; + +LIQ_NONNULL static void liq_verbose_printf(const liq_attr *context, const char *fmt, ...) +{ + if (context->log_callback) { + va_list va; + va_start(va, fmt); + int required_space = vsnprintf(NULL, 0, fmt, va)+1; // +\0 + va_end(va); + + char buf[required_space]; + va_start(va, fmt); + vsnprintf(buf, required_space, fmt, va); + va_end(va); + + context->log_callback(context, buf, context->log_callback_user_info); + } +} + +LIQ_NONNULL inline static void verbose_print(const liq_attr *attr, const char *msg) +{ + if (attr->log_callback) { + attr->log_callback(attr, msg, attr->log_callback_user_info); + } +} + +LIQ_NONNULL static void liq_verbose_printf_flush(liq_attr *attr) +{ + if (attr->log_flush_callback) { + attr->log_flush_callback(attr, attr->log_flush_callback_user_info); + } +} + +LIQ_NONNULL static bool liq_progress(const liq_attr *attr, const float percent) +{ + return attr->progress_callback && !attr->progress_callback(percent, attr->progress_callback_user_info); +} + +LIQ_NONNULL static bool liq_remap_progress(const liq_remapping_result *quant, const float percent) +{ + return quant->progress_callback && !quant->progress_callback(percent, quant->progress_callback_user_info); +} + +#if USE_SSE +inline static bool is_sse_available() +{ +#if (defined(__x86_64__) || defined(__amd64)) + return true; +#else + int a,b,c,d; + cpuid(1, a, b, c, d); + return d & (1<<25); // edx bit 25 is set when SSE is present +#endif +} +#endif + +/* make it clear in backtrace when user-supplied handle points to invalid memory */ +NEVER_INLINE LIQ_EXPORT bool liq_crash_if_invalid_handle_pointer_given(const liq_attr *user_supplied_pointer, const char *const expected_magic_header); +LIQ_EXPORT bool liq_crash_if_invalid_handle_pointer_given(const liq_attr *user_supplied_pointer, const char *const expected_magic_header) +{ + if (!user_supplied_pointer) { + return false; + } + + if (user_supplied_pointer->magic_header == liq_freed_magic) { + fprintf(stderr, "%s used after being freed", expected_magic_header); + // this is not normal error handling, this is programmer error that should crash the program. + // program cannot safely continue if memory has been used after it's been freed. + // abort() is nasty, but security vulnerability may be worse. + abort(); + } + + return user_supplied_pointer->magic_header == expected_magic_header; +} + +NEVER_INLINE LIQ_EXPORT bool liq_crash_if_invalid_pointer_given(const void *pointer); +LIQ_EXPORT bool liq_crash_if_invalid_pointer_given(const void *pointer) +{ + if (!pointer) { + return false; + } + // Force a read from the given (potentially invalid) memory location in order to check early whether this crashes the program or not. + // It doesn't matter what value is read, the code here is just to shut the compiler up about unused read. + char test_access = *((volatile char *)pointer); + return test_access || true; +} + +LIQ_NONNULL static void liq_log_error(const liq_attr *attr, const char *msg) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return; + liq_verbose_printf(attr, " error: %s", msg); +} + +static double quality_to_mse(long quality) +{ + if (quality == 0) { + return MAX_DIFF; + } + if (quality == 100) { + return 0; + } + + // curve fudged to be roughly similar to quality of libjpeg + // except lowest 10 for really low number of colors + const double extra_low_quality_fudge = MAX(0,0.016/(0.001+quality) - 0.001); + return extra_low_quality_fudge + 2.5/pow(210.0 + quality, 1.2) * (100.1-quality)/100.0; +} + +static unsigned int mse_to_quality(double mse) +{ + for(int i=100; i > 0; i--) { + if (mse <= quality_to_mse(i) + 0.000001) { // + epsilon for floating point errors + return i; + } + } + return 0; +} + +/** internally MSE is a sum of all channels with pixels 0..1 range, + but other software gives per-RGB-channel MSE for 0..255 range */ +static double mse_to_standard_mse(double mse) { + return mse * 65536.0/6.0; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_quality(liq_attr* attr, int minimum, int target) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (target < 0 || target > 100 || target < minimum || minimum < 0) return LIQ_VALUE_OUT_OF_RANGE; + + attr->target_mse = quality_to_mse(target); + attr->max_mse = quality_to_mse(minimum); + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_min_quality(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + return mse_to_quality(attr->max_mse); +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_max_quality(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + return mse_to_quality(attr->target_mse); +} + + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_max_colors(liq_attr* attr, int colors) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (colors < 2 || colors > 256) return LIQ_VALUE_OUT_OF_RANGE; + + attr->max_colors = colors; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_max_colors(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + + return attr->max_colors; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_min_posterization(liq_attr *attr, int bits) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (bits < 0 || bits > 4) return LIQ_VALUE_OUT_OF_RANGE; + + attr->min_posterization_output = bits; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_min_posterization(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + + return attr->min_posterization_output; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_speed(liq_attr* attr, int speed) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (speed < 1 || speed > 10) return LIQ_VALUE_OUT_OF_RANGE; + + unsigned int iterations = MAX(8-speed, 0); iterations += iterations * iterations/2; + attr->voronoi_iterations = iterations; + attr->voronoi_iteration_limit = 1.0/(double)(1<<(23-speed)); + attr->feedback_loop_trials = MAX(56-9*speed, 0); + + attr->max_histogram_entries = (1<<17) + (1<<18)*(10-speed); + attr->min_posterization_input = (speed >= 8) ? 1 : 0; + attr->fast_palette = (speed >= 7); + attr->use_dither_map = (speed <= (omp_get_max_threads() > 1 ? 7 : 5)); // parallelized dither map might speed up floyd remapping + attr->use_contrast_maps = (speed <= 7) || attr->use_dither_map; + attr->speed = speed; + + attr->progress_stage1 = attr->use_contrast_maps ? 20 : 8; + if (attr->feedback_loop_trials < 2) attr->progress_stage1 += 30; + attr->progress_stage3 = 50 / (1+speed); + attr->progress_stage2 = 100 - attr->progress_stage1 - attr->progress_stage3; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_speed(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + + return attr->speed; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_output_gamma(liq_result* res, double gamma) +{ + if (!CHECK_STRUCT_TYPE(res, liq_result)) return LIQ_INVALID_POINTER; + if (gamma <= 0 || gamma >= 1.0) return LIQ_VALUE_OUT_OF_RANGE; + + if (res->remapping) { + liq_remapping_result_destroy(res->remapping); + res->remapping = NULL; + } + + res->gamma = gamma; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_min_opacity(liq_attr* attr, int min) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (min < 0 || min > 255) return LIQ_VALUE_OUT_OF_RANGE; + + attr->min_opaque_val = (double)min/255.0; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_min_opacity(const liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return -1; + + return MIN(255, 256.0 * attr->min_opaque_val); +} + +LIQ_EXPORT LIQ_NONNULL void liq_set_last_index_transparent(liq_attr* attr, int is_last) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return; + + attr->last_index_transparent = !!is_last; +} + +LIQ_EXPORT void liq_attr_set_progress_callback(liq_attr *attr, liq_progress_callback_function *callback, void *user_info) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return; + + attr->progress_callback = callback; + attr->progress_callback_user_info = user_info; +} + +LIQ_EXPORT void liq_result_set_progress_callback(liq_result *result, liq_progress_callback_function *callback, void *user_info) +{ + if (!CHECK_STRUCT_TYPE(result, liq_result)) return; + + result->progress_callback = callback; + result->progress_callback_user_info = user_info; +} + +LIQ_EXPORT void liq_set_log_callback(liq_attr *attr, liq_log_callback_function *callback, void* user_info) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return; + + liq_verbose_printf_flush(attr); + attr->log_callback = callback; + attr->log_callback_user_info = user_info; +} + +LIQ_EXPORT void liq_set_log_flush_callback(liq_attr *attr, liq_log_flush_callback_function *callback, void* user_info) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return; + + attr->log_flush_callback = callback; + attr->log_flush_callback_user_info = user_info; +} + +LIQ_EXPORT liq_attr* liq_attr_create() +{ + return liq_attr_create_with_allocator(NULL, NULL); +} + +LIQ_EXPORT LIQ_NONNULL void liq_attr_destroy(liq_attr *attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) { + return; + } + + liq_verbose_printf_flush(attr); + + attr->magic_header = liq_freed_magic; + attr->free(attr); +} + +LIQ_EXPORT LIQ_NONNULL liq_attr* liq_attr_copy(liq_attr *orig) +{ + if (!CHECK_STRUCT_TYPE(orig, liq_attr)) { + return NULL; + } + + liq_attr *attr = orig->malloc(sizeof(liq_attr)); + if (!attr) return NULL; + *attr = *orig; + return attr; +} + +static void *liq_aligned_malloc(size_t size) +{ + unsigned char *ptr = malloc(size + 16); + if (!ptr) { + return NULL; + } + + uintptr_t offset = 16 - ((uintptr_t)ptr & 15); // also reserves 1 byte for ptr[-1] + ptr += offset; + assert(0 == (((uintptr_t)ptr) & 15)); + ptr[-1] = offset ^ 0x59; // store how much pointer was shifted to get the original for free() + return ptr; +} + +LIQ_NONNULL static void liq_aligned_free(void *inptr) +{ + unsigned char *ptr = inptr; + size_t offset = ptr[-1] ^ 0x59; + assert(offset > 0 && offset <= 16); + free(ptr - offset); +} + +LIQ_EXPORT liq_attr* liq_attr_create_with_allocator(void* (*custom_malloc)(size_t), void (*custom_free)(void*)) +{ +#if USE_SSE + if (!is_sse_available()) { + return NULL; + } +#endif + if (!custom_malloc && !custom_free) { + custom_malloc = liq_aligned_malloc; + custom_free = liq_aligned_free; + } else if (!custom_malloc != !custom_free) { + return NULL; // either specify both or none + } + + liq_attr *attr = custom_malloc(sizeof(liq_attr)); + if (!attr) return NULL; + *attr = (liq_attr) { + .magic_header = liq_attr_magic, + .malloc = custom_malloc, + .free = custom_free, + .max_colors = 256, + .min_opaque_val = 1, // whether preserve opaque colors for IE (1.0=no, does not affect alpha) + .last_index_transparent = false, // puts transparent color at last index. This is workaround for blu-ray subtitles. + .target_mse = 0, + .max_mse = MAX_DIFF, + }; + liq_set_speed(attr, 3); + return attr; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_image_add_fixed_color(liq_image *img, liq_color color) +{ + if (!CHECK_STRUCT_TYPE(img, liq_image)) return LIQ_INVALID_POINTER; + if (img->fixed_colors_count > 255) return LIQ_BUFFER_TOO_SMALL; + + float gamma_lut[256]; + to_f_set_gamma(gamma_lut, img->gamma); + img->fixed_colors[img->fixed_colors_count++] = to_f(gamma_lut, (rgba_pixel){ + .r = color.r, + .g = color.g, + .b = color.b, + .a = color.a, + }); + return LIQ_OK; +} + +LIQ_NONNULL static liq_error liq_histogram_add_fixed_color_internal(liq_histogram *hist, f_pixel color) +{ + if (hist->fixed_colors_count > 255) return LIQ_BUFFER_TOO_SMALL; + + hist->fixed_colors[hist->fixed_colors_count++] = color; + return LIQ_OK; +} + +LIQ_NONNULL static bool liq_image_use_low_memory(liq_image *img) +{ + img->temp_f_row = img->malloc(sizeof(img->f_pixels[0]) * img->width * omp_get_max_threads()); + return img->temp_f_row != NULL; +} + +LIQ_NONNULL static bool liq_image_should_use_low_memory(liq_image *img, const bool low_memory_hint) +{ + return img->width * img->height > (low_memory_hint ? LIQ_HIGH_MEMORY_LIMIT/8 : LIQ_HIGH_MEMORY_LIMIT) / sizeof(f_pixel); // Watch out for integer overflow +} + +static liq_image *liq_image_create_internal(const liq_attr *attr, rgba_pixel* rows[], liq_image_get_rgba_row_callback *row_callback, void *row_callback_user_info, int width, int height, double gamma) +{ + if (gamma < 0 || gamma > 1.0) { + liq_log_error(attr, "gamma must be >= 0 and <= 1 (try 1/gamma instead)"); + return NULL; + } + + if (!rows && !row_callback) { + liq_log_error(attr, "missing row data"); + return NULL; + } + + liq_image *img = attr->malloc(sizeof(liq_image)); + if (!img) return NULL; + *img = (liq_image){ + .magic_header = liq_image_magic, + .malloc = attr->malloc, + .free = attr->free, + .width = width, .height = height, + .gamma = gamma ? gamma : 0.45455, + .rows = rows, + .row_callback = row_callback, + .row_callback_user_info = row_callback_user_info, + .min_opaque_val = attr->min_opaque_val, + }; + + if (!rows || attr->min_opaque_val < 1.f) { + img->temp_row = attr->malloc(sizeof(img->temp_row[0]) * width * omp_get_max_threads()); + if (!img->temp_row) return NULL; + } + + // if image is huge or converted pixels are not likely to be reused then don't cache converted pixels + if (liq_image_should_use_low_memory(img, !img->temp_row && !attr->use_contrast_maps && !attr->use_dither_map)) { + verbose_print(attr, " conserving memory"); + if (!liq_image_use_low_memory(img)) return NULL; + } + + if (img->min_opaque_val < 1.f) { + verbose_print(attr, " Working around IE6 bug by making image less transparent..."); + } + + return img; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_image_set_memory_ownership(liq_image *img, int ownership_flags) +{ + if (!CHECK_STRUCT_TYPE(img, liq_image)) return LIQ_INVALID_POINTER; + if (!img->rows || !ownership_flags || (ownership_flags & ~(LIQ_OWN_ROWS|LIQ_OWN_PIXELS))) { + return LIQ_VALUE_OUT_OF_RANGE; + } + + if (ownership_flags & LIQ_OWN_ROWS) { + if (img->free_rows_internal) return LIQ_VALUE_OUT_OF_RANGE; + img->free_rows = true; + } + + if (ownership_flags & LIQ_OWN_PIXELS) { + img->free_pixels = true; + if (!img->pixels) { + // for simplicity of this API there's no explicit bitmap argument, + // so the row with the lowest address is assumed to be at the start of the bitmap + img->pixels = img->rows[0]; + for(unsigned int i=1; i < img->height; i++) { + img->pixels = MIN(img->pixels, img->rows[i]); + } + } + } + + return LIQ_OK; +} + +LIQ_NONNULL static bool check_image_size(const liq_attr *attr, const int width, const int height) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) { + return false; + } + + if (width <= 0 || height <= 0) { + liq_log_error(attr, "width and height must be > 0"); + return false; + } + + if (width > INT_MAX/sizeof(rgba_pixel)/height || width > INT_MAX/16/sizeof(f_pixel) || height > INT_MAX/sizeof(size_t)) { + liq_log_error(attr, "image too large"); + return false; + } + return true; +} + +LIQ_EXPORT 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) +{ + if (!check_image_size(attr, width, height)) { + return NULL; + } + return liq_image_create_internal(attr, NULL, row_callback, user_info, width, height, gamma); +} + +LIQ_EXPORT liq_image *liq_image_create_rgba_rows(const liq_attr *attr, void *const rows[], int width, int height, double gamma) +{ + if (!check_image_size(attr, width, height)) { + return NULL; + } + + for(int i=0; i < height; i++) { + if (!CHECK_USER_POINTER(rows+i) || !CHECK_USER_POINTER(rows[i])) { + liq_log_error(attr, "invalid row pointers"); + return NULL; + } + } + return liq_image_create_internal(attr, (rgba_pixel**)rows, NULL, NULL, width, height, gamma); +} + +LIQ_EXPORT LIQ_NONNULL liq_image *liq_image_create_rgba(const liq_attr *attr, const void* bitmap, int width, int height, double gamma) +{ + if (!check_image_size(attr, width, height)) { + return NULL; + } + if (!CHECK_USER_POINTER(bitmap)) { + liq_log_error(attr, "invalid bitmap pointer"); + return NULL; + } + + rgba_pixel *const pixels = (rgba_pixel *const)bitmap; + rgba_pixel **rows = attr->malloc(sizeof(rows[0])*height); + if (!rows) return NULL; + + for(int i=0; i < height; i++) { + rows[i] = pixels + width * i; + } + + liq_image *image = liq_image_create_internal(attr, rows, NULL, NULL, width, height, gamma); + if (!image) { + attr->free(rows); + return NULL; + } + image->free_rows = true; + image->free_rows_internal = true; + return image; +} + +NEVER_INLINE LIQ_EXPORT void liq_executing_user_callback(liq_image_get_rgba_row_callback *callback, liq_color *temp_row, int row, int width, void *user_info); +LIQ_EXPORT void liq_executing_user_callback(liq_image_get_rgba_row_callback *callback, liq_color *temp_row, int row, int width, void *user_info) +{ + assert(callback); + assert(temp_row); + callback(temp_row, row, width, user_info); +} + +LIQ_NONNULL inline static bool liq_image_has_rgba_pixels(const liq_image *img) +{ + if (!CHECK_STRUCT_TYPE(img, liq_image)) { + return false; + } + return img->rows || (img->temp_row && img->row_callback); +} + +LIQ_NONNULL inline static bool liq_image_can_use_rgba_rows(const liq_image *img) +{ + assert(liq_image_has_rgba_pixels(img)); + + const bool iebug = img->min_opaque_val < 1.f; + return (img->rows && !iebug); +} + +LIQ_NONNULL static const rgba_pixel *liq_image_get_row_rgba(liq_image *img, unsigned int row) +{ + if (liq_image_can_use_rgba_rows(img)) { + return img->rows[row]; + } + + assert(img->temp_row); + rgba_pixel *temp_row = img->temp_row + img->width * omp_get_thread_num(); + if (img->rows) { + memcpy(temp_row, img->rows[row], img->width * sizeof(temp_row[0])); + } else { + liq_executing_user_callback(img->row_callback, (liq_color*)temp_row, row, img->width, img->row_callback_user_info); + } + + if (img->min_opaque_val < 1.f) modify_alpha(img, temp_row); + return temp_row; +} + +LIQ_NONNULL static void convert_row_to_f(liq_image *img, f_pixel *row_f_pixels, const unsigned int row, const float gamma_lut[]) +{ + assert(row_f_pixels); + assert(!USE_SSE || 0 == ((uintptr_t)row_f_pixels & 15)); + + const rgba_pixel *const row_pixels = liq_image_get_row_rgba(img, row); + + for(unsigned int col=0; col < img->width; col++) { + row_f_pixels[col] = to_f(gamma_lut, row_pixels[col]); + } +} + +LIQ_NONNULL static const f_pixel *liq_image_get_row_f(liq_image *img, unsigned int row) +{ + if (!img->f_pixels) { + if (img->temp_f_row) { + float gamma_lut[256]; + to_f_set_gamma(gamma_lut, img->gamma); + f_pixel *row_for_thread = img->temp_f_row + img->width * omp_get_thread_num(); + convert_row_to_f(img, row_for_thread, row, gamma_lut); + return row_for_thread; + } + + assert(omp_get_thread_num() == 0); + if (!liq_image_should_use_low_memory(img, false)) { + img->f_pixels = img->malloc(sizeof(img->f_pixels[0]) * img->width * img->height); + } + if (!img->f_pixels) { + if (!liq_image_use_low_memory(img)) return NULL; + return liq_image_get_row_f(img, row); + } + + float gamma_lut[256]; + to_f_set_gamma(gamma_lut, img->gamma); + for(unsigned int i=0; i < img->height; i++) { + convert_row_to_f(img, &img->f_pixels[i*img->width], i, gamma_lut); + } + } + return img->f_pixels + img->width * row; +} + +LIQ_EXPORT LIQ_NONNULL int liq_image_get_width(const liq_image *input_image) +{ + if (!CHECK_STRUCT_TYPE(input_image, liq_image)) return -1; + return input_image->width; +} + +LIQ_EXPORT LIQ_NONNULL int liq_image_get_height(const liq_image *input_image) +{ + if (!CHECK_STRUCT_TYPE(input_image, liq_image)) return -1; + return input_image->height; +} + +typedef void free_func(void*); + +LIQ_NONNULL static free_func *get_default_free_func(liq_image *img) +{ + // When default allocator is used then user-supplied pointers must be freed with free() + if (img->free_rows_internal || img->free != liq_aligned_free) { + return img->free; + } + return free; +} + +LIQ_NONNULL static void liq_image_free_rgba_source(liq_image *input_image) +{ + if (input_image->free_pixels && input_image->pixels) { + get_default_free_func(input_image)(input_image->pixels); + input_image->pixels = NULL; + } + + if (input_image->free_rows && input_image->rows) { + get_default_free_func(input_image)(input_image->rows); + input_image->rows = NULL; + } +} + +LIQ_EXPORT LIQ_NONNULL void liq_image_destroy(liq_image *input_image) +{ + if (!CHECK_STRUCT_TYPE(input_image, liq_image)) return; + + liq_image_free_rgba_source(input_image); + + if (input_image->noise) { + input_image->free(input_image->noise); + } + + if (input_image->edges) { + input_image->free(input_image->edges); + } + + if (input_image->dither_map) { + input_image->free(input_image->dither_map); + } + + if (input_image->f_pixels) { + input_image->free(input_image->f_pixels); + } + + if (input_image->temp_row) { + input_image->free(input_image->temp_row); + } + + if (input_image->temp_f_row) { + input_image->free(input_image->temp_f_row); + } + + input_image->magic_header = liq_freed_magic; + input_image->free(input_image); +} + +LIQ_EXPORT liq_histogram* liq_histogram_create(liq_attr* attr) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) { + return NULL; + } + + liq_histogram *hist = attr->malloc(sizeof(liq_histogram)); + if (!hist) return NULL; + *hist = (liq_histogram) { + .magic_header = liq_histogram_magic, + .malloc = attr->malloc, + .free = attr->free, + + .ignorebits = MAX(attr->min_posterization_output, attr->min_posterization_input), + }; + return hist; +} + +LIQ_EXPORT LIQ_NONNULL void liq_histogram_destroy(liq_histogram *hist) +{ + if (!CHECK_STRUCT_TYPE(hist, liq_histogram)) return; + hist->magic_header = liq_freed_magic; + + pam_freeacolorhash(hist->acht); + hist->free(hist); +} + +LIQ_EXPORT LIQ_NONNULL liq_result *liq_quantize_image(liq_attr *attr, liq_image *img) +{ + liq_result *res; + if (LIQ_OK != liq_image_quantize(img, attr, &res)) { + return NULL; + } + return res; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_image_quantize(liq_image *const img, liq_attr *const attr, liq_result **result_output) +{ + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (!liq_image_has_rgba_pixels(img)) { + return LIQ_INVALID_POINTER; + } + + liq_histogram *hist = liq_histogram_create(attr); + if (!hist) { + return LIQ_OUT_OF_MEMORY; + } + liq_error err = liq_histogram_add_image(hist, attr, img); + if (LIQ_OK != err) { + return err; + } + + err = liq_histogram_quantize_internal(hist, attr, false, result_output); + liq_histogram_destroy(hist); + + return err; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_histogram_quantize(liq_histogram *input_hist, liq_attr *attr, liq_result **result_output) { + return liq_histogram_quantize_internal(input_hist, attr, true, result_output); +} + +LIQ_NONNULL static liq_error liq_histogram_quantize_internal(liq_histogram *input_hist, liq_attr *attr, bool fixed_result_colors, liq_result **result_output) +{ + if (!CHECK_USER_POINTER(result_output)) return LIQ_INVALID_POINTER; + *result_output = NULL; + + if (!CHECK_STRUCT_TYPE(attr, liq_attr)) return LIQ_INVALID_POINTER; + if (!CHECK_STRUCT_TYPE(input_hist, liq_histogram)) return LIQ_INVALID_POINTER; + + if (liq_progress(attr, 0)) return LIQ_ABORTED; + + histogram *hist; + liq_error err = finalize_histogram(input_hist, attr, &hist); + if (err != LIQ_OK) { + return err; + } + + err = pngquant_quantize(hist, attr, input_hist->fixed_colors_count, input_hist->fixed_colors, input_hist->gamma, fixed_result_colors, result_output); + pam_freeacolorhist(hist); + + return err; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_set_dithering_level(liq_result *res, float dither_level) +{ + if (!CHECK_STRUCT_TYPE(res, liq_result)) return LIQ_INVALID_POINTER; + + if (res->remapping) { + liq_remapping_result_destroy(res->remapping); + res->remapping = NULL; + } + + if (res->dither_level < 0 || res->dither_level > 1.0f) return LIQ_VALUE_OUT_OF_RANGE; + res->dither_level = dither_level; + return LIQ_OK; +} + +LIQ_NONNULL static liq_remapping_result *liq_remapping_result_create(liq_result *result) +{ + if (!CHECK_STRUCT_TYPE(result, liq_result)) { + return NULL; + } + + liq_remapping_result *res = result->malloc(sizeof(liq_remapping_result)); + if (!res) return NULL; + *res = (liq_remapping_result) { + .magic_header = liq_remapping_result_magic, + .malloc = result->malloc, + .free = result->free, + .dither_level = result->dither_level, + .use_dither_map = result->use_dither_map, + .palette_error = result->palette_error, + .gamma = result->gamma, + .palette = pam_duplicate_colormap(result->palette), + .progress_callback = result->progress_callback, + .progress_callback_user_info = result->progress_callback_user_info, + .progress_stage1 = result->use_dither_map ? 20 : 0, + }; + return res; +} + +LIQ_EXPORT LIQ_NONNULL double liq_get_output_gamma(const liq_result *result) +{ + if (!CHECK_STRUCT_TYPE(result, liq_result)) return -1; + + return result->gamma; +} + +LIQ_NONNULL static void liq_remapping_result_destroy(liq_remapping_result *result) +{ + if (!CHECK_STRUCT_TYPE(result, liq_remapping_result)) return; + + if (result->palette) pam_freecolormap(result->palette); + if (result->pixels) result->free(result->pixels); + + result->magic_header = liq_freed_magic; + result->free(result); +} + +LIQ_EXPORT LIQ_NONNULL void liq_result_destroy(liq_result *res) +{ + if (!CHECK_STRUCT_TYPE(res, liq_result)) return; + + memset(&res->int_palette, 0, sizeof(liq_palette)); + + if (res->remapping) { + memset(&res->remapping->int_palette, 0, sizeof(liq_palette)); + liq_remapping_result_destroy(res->remapping); + } + + pam_freecolormap(res->palette); + + res->magic_header = liq_freed_magic; + res->free(res); +} + + +LIQ_EXPORT LIQ_NONNULL double liq_get_quantization_error(liq_result *result) { + if (!CHECK_STRUCT_TYPE(result, liq_result)) return -1; + + if (result->palette_error >= 0) { + return mse_to_standard_mse(result->palette_error); + } + + return -1; +} + +LIQ_EXPORT LIQ_NONNULL double liq_get_remapping_error(liq_result *result) { + if (!CHECK_STRUCT_TYPE(result, liq_result)) return -1; + + if (result->remapping && result->remapping->palette_error >= 0) { + return mse_to_standard_mse(result->remapping->palette_error); + } + + return -1; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_quantization_quality(liq_result *result) { + if (!CHECK_STRUCT_TYPE(result, liq_result)) return -1; + + if (result->palette_error >= 0) { + return mse_to_quality(result->palette_error); + } + + return -1; +} + +LIQ_EXPORT LIQ_NONNULL int liq_get_remapping_quality(liq_result *result) { + if (!CHECK_STRUCT_TYPE(result, liq_result)) return -1; + + if (result->remapping && result->remapping->palette_error >= 0) { + return mse_to_quality(result->remapping->palette_error); + } + + return -1; +} + +LIQ_NONNULL static int compare_popularity(const void *ch1, const void *ch2) +{ + const float v1 = ((const colormap_item*)ch1)->popularity; + const float v2 = ((const colormap_item*)ch2)->popularity; + return v1 > v2 ? -1 : 1; +} + +LIQ_NONNULL static void sort_palette_qsort(colormap *map, int start, int nelem) +{ + if (!nelem) return; + qsort(map->palette + start, nelem, sizeof(map->palette[0]), compare_popularity); +} + +#define SWAP_PALETTE(map, a,b) { \ + const colormap_item tmp = (map)->palette[(a)]; \ + (map)->palette[(a)] = (map)->palette[(b)]; \ + (map)->palette[(b)] = tmp; } + +LIQ_NONNULL static void sort_palette(colormap *map, const liq_attr *options) +{ + /* + ** Step 3.5 [GRR]: remap the palette colors so that all entries with + ** the maximal alpha value (i.e., fully opaque) are at the end and can + ** therefore be omitted from the tRNS chunk. + */ + if (options->last_index_transparent) { + for(unsigned int i=0; i < map->colors; i++) { + if (map->palette[i].acolor.a < 1.0/256.0) { + const unsigned int old = i, transparent_dest = map->colors-1; + + SWAP_PALETTE(map, transparent_dest, old); + + /* colors sorted by popularity make pngs slightly more compressible */ + sort_palette_qsort(map, 0, map->colors-1); + return; + } + } + } + + unsigned int non_fixed_colors = 0; + for(unsigned int i = 0; i < map->colors; i++) { + if (map->palette[i].fixed) { + break; + } + non_fixed_colors++; + } + + /* move transparent colors to the beginning to shrink trns chunk */ + unsigned int num_transparent = 0; + for(unsigned int i = 0; i < non_fixed_colors; i++) { + if (map->palette[i].acolor.a < 255.0/256.0) { + // current transparent color is swapped with earlier opaque one + if (i != num_transparent) { + SWAP_PALETTE(map, num_transparent, i); + i--; + } + num_transparent++; + } + } + + liq_verbose_printf(options, " eliminated opaque tRNS-chunk entries...%d entr%s transparent", num_transparent, (num_transparent == 1)? "y" : "ies"); + + /* colors sorted by popularity make pngs slightly more compressible + * opaque and transparent are sorted separately + */ + sort_palette_qsort(map, 0, num_transparent); + sort_palette_qsort(map, num_transparent, non_fixed_colors - num_transparent); + + if (non_fixed_colors > 9 && map->colors > 16) { + SWAP_PALETTE(map, 7, 1); // slightly improves compression + SWAP_PALETTE(map, 8, 2); + SWAP_PALETTE(map, 9, 3); + } +} + +inline static unsigned int posterize_channel(unsigned int color, unsigned int bits) +{ + return (color & ~((1<> (8-bits)); +} + +LIQ_NONNULL static void set_rounded_palette(liq_palette *const dest, colormap *const map, const double gamma, unsigned int posterize) +{ + float gamma_lut[256]; + to_f_set_gamma(gamma_lut, gamma); + + dest->count = map->colors; + for(unsigned int x = 0; x < map->colors; ++x) { + rgba_pixel px = to_rgb(gamma, map->palette[x].acolor); + + px.r = posterize_channel(px.r, posterize); + px.g = posterize_channel(px.g, posterize); + px.b = posterize_channel(px.b, posterize); + px.a = posterize_channel(px.a, posterize); + + map->palette[x].acolor = to_f(gamma_lut, px); /* saves rounding error introduced by to_rgb, which makes remapping & dithering more accurate */ + + if (!px.a && !map->palette[x].fixed) { + px.r = 71; px.g = 112; px.b = 76; + } + + dest->entries[x] = (liq_color){.r=px.r,.g=px.g,.b=px.b,.a=px.a}; + } +} + +LIQ_EXPORT LIQ_NONNULL const liq_palette *liq_get_palette(liq_result *result) +{ + if (!CHECK_STRUCT_TYPE(result, liq_result)) return NULL; + + if (result->remapping && result->remapping->int_palette.count) { + return &result->remapping->int_palette; + } + + if (!result->int_palette.count) { + set_rounded_palette(&result->int_palette, result->palette, result->gamma, result->min_posterization_output); + } + return &result->int_palette; +} + +LIQ_NONNULL static float remap_to_palette(liq_image *const input_image, unsigned char *const *const output_pixels, colormap *const map, const bool fast) +{ + const int rows = input_image->height; + const unsigned int cols = input_image->width; + double remapping_error=0; + + if (!liq_image_get_row_f(input_image, 0)) { // trigger lazy conversion + return -1; + } + + struct nearest_map *const n = nearest_init(map, fast); + + const unsigned int max_threads = omp_get_max_threads(); + viter_state average_color[(VITER_CACHE_LINE_GAP+map->colors) * max_threads]; + viter_init(map, max_threads, average_color); + + #pragma omp parallel for if (rows*cols > 3000) \ + schedule(static) default(none) shared(average_color) reduction(+:remapping_error) + for(int row = 0; row < rows; ++row) { + const f_pixel *const row_pixels = liq_image_get_row_f(input_image, row); + unsigned int last_match=0; + for(unsigned int col = 0; col < cols; ++col) { + float diff; + output_pixels[row][col] = last_match = nearest_search(n, &row_pixels[col], last_match, &diff); + + remapping_error += diff; + viter_update_color(row_pixels[col], 1.0, map, last_match, omp_get_thread_num(), average_color); + } + } + + viter_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.0) { a = 1.0; } + 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.8; + } 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_NONNULL static 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 unsigned 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; + + /* Initialize Floyd-Steinberg error vectors. */ + f_pixel *restrict thiserr, *restrict nexterr; + const size_t errsize = (cols + 2) * sizeof(*thiserr) * 2; + thiserr = input_image->malloc(errsize); // +2 saves from checking out of bounds access + if (!thiserr) return false; + memset(thiserr, 0, errsize); + nexterr = thiserr + (cols + 2); + + bool ok = true; + struct nearest_map *const n = nearest_init(map, false); + + // 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.0 - (1.0-base_dithering_level)*(1.0-base_dithering_level); + + if (dither_map) { + base_dithering_level *= 1.0/255.0; // convert byte to float + } + base_dithering_level *= 15.0/16.0; // prevent small errors from accumulating + + bool fs_direction = true; + unsigned int last_match=0; + for (unsigned 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, (cols + 2) * sizeof(*nexterr)); + + unsigned int col = (fs_direction) ? 0 : (cols - 1); + const f_pixel *const row_pixels = liq_image_get_row_f(input_image, row); + + 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; + output_pixels[row][col] = last_match = nearest_search(n, &spx, guessed_match, NULL); + + const f_pixel output_px = acolormap[last_match].acolor; + 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.75; + err.g *= 0.75; + err.b *= 0.75; + err.a *= 0.75; + } + + /* Propagate Floyd-Steinberg error terms. */ + if (fs_direction) { + 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 + if (fs_direction) { + ++col; + if (col >= cols) break; + } else { + if (col <= 0) break; + --col; + } + } 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; +} + +/* fixed colors are always included in the palette, so it would be wasteful to duplicate them in palette from histogram */ +LIQ_NONNULL static 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.0, 2.0/256.0/256.0); + 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_EXPORT LIQ_NONNULL liq_error liq_histogram_add_image(liq_histogram *input_hist, liq_attr *options, liq_image *input_image) +{ + const unsigned int cols = input_image->width, rows = input_image->height; + + if (!input_image->noise && options->use_contrast_maps) { + contrast_maps(input_image); + } + + input_hist->gamma = input_image->gamma; + + for(int i = 0; i < input_image->fixed_colors_count; i++) { + liq_error res = liq_histogram_add_fixed_color_internal(input_hist, input_image->fixed_colors[i]); + if (res != LIQ_OK) { + return res; + } + } + + /* + ** Step 2: attempt to make a histogram of the colors, unclustered. + ** If at first we don't succeed, increase ignorebits to increase color + ** coherence and try again. + */ + + if (liq_progress(options, options->progress_stage1 * 0.4f)) return LIQ_ABORTED; + + const bool all_rows_at_once = liq_image_can_use_rgba_rows(input_image); + + // Usual solution is to start from scratch when limit is exceeded, but that's not possible if it's not + // the first image added + const unsigned int max_histogram_entries = input_hist->had_image_added ? ~0 : options->max_histogram_entries; + do { + if (!input_hist->acht) { + input_hist->acht = pam_allocacolorhash(max_histogram_entries, rows*cols, input_hist->ignorebits, options->malloc, options->free); + } + if (!input_hist->acht) return LIQ_OUT_OF_MEMORY; + + // histogram uses noise contrast map for importance. Color accuracy in noisy areas is not very important. + // noise map does not include edges to avoid ruining anti-aliasing + for(unsigned int row=0; row < rows; row++) { + bool added_ok; + if (all_rows_at_once) { + added_ok = pam_computeacolorhash(input_hist->acht, (const rgba_pixel *const *)input_image->rows, cols, rows, input_image->noise); + if (added_ok) break; + } else { + const rgba_pixel* rows_p[1] = { liq_image_get_row_rgba(input_image, row) }; + added_ok = pam_computeacolorhash(input_hist->acht, rows_p, cols, 1, input_image->noise ? &input_image->noise[row * cols] : NULL); + } + if (!added_ok) { + input_hist->ignorebits++; + liq_verbose_printf(options, " too many colors! Scaling colors to improve clustering... %d", input_hist->ignorebits); + pam_freeacolorhash(input_hist->acht); + input_hist->acht = NULL; + if (liq_progress(options, options->progress_stage1 * 0.6f)) return LIQ_ABORTED; + break; + } + } + } while(!input_hist->acht); + + input_hist->had_image_added = true; + + if (input_image->noise) { + input_image->free(input_image->noise); + input_image->noise = NULL; + } + + if (input_image->free_pixels && input_image->f_pixels) { + liq_image_free_rgba_source(input_image); // bow can free the RGBA source if copy has been made in f_pixels + } + + return LIQ_OK; +} + +LIQ_NONNULL static liq_error finalize_histogram(liq_histogram *input_hist, liq_attr *options, histogram **hist_output) +{ + if (liq_progress(options, options->progress_stage1 * 0.9f)) { + return LIQ_ABORTED; + } + + if (!input_hist->acht) { + return LIQ_BITMAP_NOT_AVAILABLE; + } + + histogram *hist = pam_acolorhashtoacolorhist(input_hist->acht, input_hist->gamma, options->malloc, options->free); + pam_freeacolorhash(input_hist->acht); + input_hist->acht = NULL; + + if (!hist) { + return LIQ_OUT_OF_MEMORY; + } + liq_verbose_printf(options, " made histogram...%d colors found", hist->size); + remove_fixed_colors_from_histogram(hist, input_hist->fixed_colors_count, input_hist->fixed_colors, options->target_mse); + + *hist_output = hist; + return LIQ_OK; +} + +LIQ_NONNULL static void modify_alpha(liq_image *input_image, rgba_pixel *const row_pixels) +{ + /* IE6 makes colors with even slightest transparency completely transparent, + thus to improve situation in IE, make colors that are less than ~10% transparent + completely opaque */ + + const float min_opaque_val = input_image->min_opaque_val; + const float almost_opaque_val = min_opaque_val * 169.f/256.f; + const unsigned int almost_opaque_val_int = (min_opaque_val * 169.f/256.f)*255.f; + + for(unsigned int col = 0; col < input_image->width; col++) { + const rgba_pixel px = row_pixels[col]; + + /* ie bug: to avoid visible step caused by forced opaqueness, linearily raise opaqueness of almost-opaque colors */ + if (px.a >= almost_opaque_val_int) { + float al = px.a / 255.f; + al = almost_opaque_val + (al-almost_opaque_val) * (1.f-almost_opaque_val) / (min_opaque_val-almost_opaque_val); + al *= 256.f; + row_pixels[col].a = al >= 255.f ? 255 : al; + } + } +} + +/** + Builds two maps: + noise - approximation of areas with high-frequency noise, except straight edges. 1=flat, 0=noisy. + edges - noise map including all edges + */ +LIQ_NONNULL static void contrast_maps(liq_image *image) +{ + const unsigned int cols = image->width, rows = image->height; + if (cols < 4 || rows < 4 || (3*cols*rows) > LIQ_HIGH_MEMORY_LIMIT) { + return; + } + + unsigned char *restrict noise = image->noise ? image->noise : image->malloc(cols*rows); + image->noise = NULL; + unsigned char *restrict edges = image->edges ? image->edges : image->malloc(cols*rows); + image->edges = NULL; + + unsigned char *restrict tmp = image->malloc(cols*rows); + + if (!noise || !edges || !tmp) { + image->free(noise); + image->free(edges); + image->free(tmp); + return; + } + + const f_pixel *curr_row, *prev_row, *next_row; + curr_row = prev_row = next_row = liq_image_get_row_f(image, 0); + + for (unsigned int j=0; j < rows; j++) { + prev_row = curr_row; + curr_row = next_row; + next_row = liq_image_get_row_f(image, MIN(rows-1,j+1)); + + f_pixel prev, curr = curr_row[0], next=curr; + for (unsigned int i=0; i < cols; i++) { + prev=curr; + curr=next; + next = curr_row[MIN(cols-1,i+1)]; + + // contrast is difference between pixels neighbouring horizontally and vertically + const float a = fabsf(prev.a+next.a - curr.a*2.f), + r = fabsf(prev.r+next.r - curr.r*2.f), + g = fabsf(prev.g+next.g - curr.g*2.f), + b = fabsf(prev.b+next.b - curr.b*2.f); + + const f_pixel prevl = prev_row[i]; + const f_pixel nextl = next_row[i]; + + const float a1 = fabsf(prevl.a+nextl.a - curr.a*2.f), + r1 = fabsf(prevl.r+nextl.r - curr.r*2.f), + g1 = fabsf(prevl.g+nextl.g - curr.g*2.f), + b1 = fabsf(prevl.b+nextl.b - curr.b*2.f); + + const float horiz = MAX(MAX(a,r),MAX(g,b)); + const float vert = MAX(MAX(a1,r1),MAX(g1,b1)); + const float edge = MAX(horiz,vert); + float z = edge - fabsf(horiz-vert)*.5f; + z = 1.f - MAX(z,MIN(horiz,vert)); + z *= z; // noise is amplified + z *= z; + + z *= 256.f; + noise[j*cols+i] = z < 256 ? z : 255; + z = (1.f-edge)*256.f; + edges[j*cols+i] = z < 256 ? z : 255; + } + } + + // noise areas are shrunk and then expanded to remove thin edges from the map + liq_max3(noise, tmp, cols, rows); + liq_max3(tmp, noise, cols, rows); + + liq_blur(noise, tmp, noise, cols, rows, 3); + + liq_max3(noise, tmp, cols, rows); + + liq_min3(tmp, noise, cols, rows); + liq_min3(noise, tmp, cols, rows); + liq_min3(tmp, noise, cols, rows); + + liq_min3(edges, tmp, cols, rows); + liq_max3(tmp, edges, cols, rows); + for(unsigned int i=0; i < cols*rows; i++) edges[i] = MIN(noise[i], edges[i]); + + image->free(tmp); + + image->noise = noise; + image->edges = edges; +} + +/** + * Builds map of neighbor pixels mapped to the same palette entry + * + * For efficiency/simplicity it mainly looks for same consecutive pixels horizontally + * and peeks 1 pixel above/below. Full 2d algorithm doesn't improve it significantly. + * Correct flood fill doesn't have visually good properties. + */ +LIQ_NONNULL static void update_dither_map(unsigned char *const *const row_pointers, liq_image *input_image) +{ + const unsigned int width = input_image->width; + const unsigned int height = input_image->height; + unsigned char *const edges = input_image->edges; + + for(unsigned int row=0; row < height; row++) { + unsigned char lastpixel = row_pointers[row][0]; + unsigned int lastcol=0; + + for(unsigned int col=1; col < width; col++) { + const unsigned char px = row_pointers[row][col]; + + if (px != lastpixel || col == width-1) { + int neighbor_count = 10 * (col-lastcol); + + unsigned int i=lastcol; + while(i < col) { + if (row > 0) { + unsigned char pixelabove = row_pointers[row-1][i]; + if (pixelabove == lastpixel) neighbor_count += 15; + } + if (row < height-1) { + unsigned char pixelbelow = row_pointers[row+1][i]; + if (pixelbelow == lastpixel) neighbor_count += 15; + } + i++; + } + + while(lastcol <= col) { + int e = edges[row*width + lastcol]; + edges[row*width + lastcol++] = (e+128) * (255.f/(255+128)) * (1.f - 20.f / (20 + neighbor_count)); + } + lastpixel = px; + } + } + } + input_image->dither_map = input_image->edges; + input_image->edges = NULL; +} + +/** + * Palette can be NULL, in which case it creates a new palette from scratch. + */ +static colormap *add_fixed_colors_to_palette(colormap *palette, const int max_colors, const f_pixel fixed_colors[], const int fixed_colors_count, void* (*malloc)(size_t), void (*free)(void*)) +{ + if (!fixed_colors_count) return palette; + + colormap *newpal = pam_colormap(MIN(max_colors, (palette ? palette->colors : 0) + fixed_colors_count), malloc, free); + unsigned int i=0; + if (palette && fixed_colors_count < max_colors) { + unsigned int palette_max = MIN(palette->colors, max_colors - fixed_colors_count); + for(; i < palette_max; i++) { + newpal->palette[i] = palette->palette[i]; + } + } + for(int j=0; j < MIN(max_colors, fixed_colors_count); j++) { + newpal->palette[i++] = (colormap_item){ + .acolor = fixed_colors[j], + .fixed = true, + }; + } + if (palette) pam_freecolormap(palette); + return newpal; +} + +LIQ_NONNULL static void adjust_histogram_callback(hist_item *item, float diff) +{ + item->adjusted_weight = (item->perceptual_weight+item->adjusted_weight) * (sqrtf(1.f+diff)); +} + +/** + Repeats mediancut with different histogram weights to find palette with minimum error. + + feedback_loop_trials controls how long the search will take. < 0 skips the iteration. + */ +static colormap *find_best_palette(histogram *hist, const liq_attr *options, const double max_mse, const f_pixel fixed_colors[], const unsigned int fixed_colors_count, double *palette_error_p) +{ + unsigned int max_colors = options->max_colors; + + // if output is posterized it doesn't make sense to aim for perfrect colors, so increase target_mse + // at this point actual gamma is not set, so very conservative posterization estimate is used + const double target_mse = MIN(max_mse, MAX(options->target_mse, pow((1<min_posterization_output)/1024.0, 2))); + int feedback_loop_trials = options->feedback_loop_trials; + colormap *acolormap = NULL; + double least_error = MAX_DIFF; + double target_mse_overshoot = feedback_loop_trials>0 ? 1.05 : 1.0; + const float total_trials = (float)(feedback_loop_trials>0?feedback_loop_trials:1); + + do { + colormap *newmap; + if (hist->size && fixed_colors_count < max_colors) { + newmap = mediancut(hist, max_colors-fixed_colors_count, target_mse * target_mse_overshoot, MAX(MAX(45.0/65536.0, target_mse), least_error)*1.2, + options->malloc, options->free); + } else { + feedback_loop_trials = 0; + newmap = NULL; + } + newmap = add_fixed_colors_to_palette(newmap, max_colors, fixed_colors, fixed_colors_count, options->malloc, options->free); + if (!newmap) { + return NULL; + } + + if (feedback_loop_trials <= 0) { + return newmap; + } + + // after palette has been created, total error (MSE) is calculated to keep the best palette + // at the same time Voronoi iteration is done to improve the palette + // and histogram weights are adjusted based on remapping error to give more weight to poorly matched colors + + const bool first_run_of_target_mse = !acolormap && target_mse > 0; + double total_error = viter_do_iteration(hist, newmap, first_run_of_target_mse ? NULL : adjust_histogram_callback, !acolormap || options->fast_palette); + + // goal is to increase quality or to reduce number of colors used if quality is good enough + if (!acolormap || total_error < least_error || (total_error <= target_mse && newmap->colors < max_colors)) { + if (acolormap) pam_freecolormap(acolormap); + acolormap = newmap; + + if (total_error < target_mse && total_error > 0) { + // voronoi iteration improves quality above what mediancut aims for + // this compensates for it, making mediancut aim for worse + target_mse_overshoot = MIN(target_mse_overshoot*1.25, target_mse/total_error); + } + + least_error = total_error; + + // if number of colors could be reduced, try to keep it that way + // but allow extra color as a bit of wiggle room in case quality can be improved too + max_colors = MIN(newmap->colors+1, max_colors); + + feedback_loop_trials -= 1; // asymptotic improvement could make it go on forever + } else { + for(unsigned int j=0; j < hist->size; j++) { + hist->achv[j].adjusted_weight = (hist->achv[j].perceptual_weight + hist->achv[j].adjusted_weight)/2.0; + } + + target_mse_overshoot = 1.0; + feedback_loop_trials -= 6; + // if error is really bad, it's unlikely to improve, so end sooner + if (total_error > least_error*4) feedback_loop_trials -= 3; + pam_freecolormap(newmap); + } + + float fraction_done = 1.f-MAX(0.f, feedback_loop_trials/total_trials); + if (liq_progress(options, options->progress_stage1 + fraction_done * options->progress_stage2)) break; + liq_verbose_printf(options, " selecting colors...%d%%", (int)(100.f * fraction_done)); + } + while(feedback_loop_trials > 0); + + *palette_error_p = least_error; + return acolormap; +} + +static colormap *histogram_to_palette(const histogram *hist, const liq_attr *options) { + if (!hist->size) { + return NULL; + } + colormap *acolormap = pam_colormap(hist->size, options->malloc, options->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_NONNULL static liq_error pngquant_quantize(histogram *hist, const liq_attr *options, const int fixed_colors_count, const f_pixel fixed_colors[], const double gamma, bool fixed_result_colors, liq_result **result_output) +{ + colormap *acolormap; + double palette_error = -1; + + assert((verbose_print(options, "SLOW debug checks enabled. Recompile with NDEBUG for normal operation."),1)); + + // no point having perfect match with imperfect colors (ignorebits > 0) + const bool fast_palette = options->fast_palette || hist->ignorebits > 0; + const bool few_input_colors = hist->size+fixed_colors_count <= options->max_colors; + + if (liq_progress(options, options->progress_stage1)) return LIQ_ABORTED; + + // If image has few colors to begin with (and no quality degradation is required) + // then it's possible to skip quantization entirely + if (few_input_colors && options->target_mse == 0) { + acolormap = add_fixed_colors_to_palette(histogram_to_palette(hist, options), options->max_colors, fixed_colors, fixed_colors_count, options->malloc, options->free); + palette_error = 0; + } else { + const double max_mse = options->max_mse * (few_input_colors ? 0.33 : 1.0); // when degrading image that's already paletted, require much higher improvement, since pal2pal often looks bad and there's little gain + acolormap = find_best_palette(hist, options, max_mse, fixed_colors, fixed_colors_count, &palette_error); + if (!acolormap) { + return LIQ_VALUE_OUT_OF_RANGE; + } + + // Voronoi iteration approaches local minimum for the palette + const double iteration_limit = options->voronoi_iteration_limit; + unsigned int iterations = options->voronoi_iterations; + + if (!iterations && palette_error < 0 && max_mse < MAX_DIFF) iterations = 1; // otherwise total error is never calculated and MSE limit won't work + + if (iterations) { + // likely_colormap_index (used and set in viter_do_iteration) can't point to index outside colormap + if (acolormap->colors < 256) for(unsigned int j=0; j < hist->size; j++) { + if (hist->achv[j].tmp.likely_colormap_index >= acolormap->colors) { + hist->achv[j].tmp.likely_colormap_index = 0; // actual value doesn't matter, as the guess is out of date anyway + } + } + + verbose_print(options, " moving colormap towards local minimum"); + + double previous_palette_error = MAX_DIFF; + + for(unsigned int i=0; i < iterations; i++) { + palette_error = viter_do_iteration(hist, acolormap, NULL, i==0 || options->fast_palette); + + if (liq_progress(options, options->progress_stage1 + options->progress_stage2 + (i * options->progress_stage3 * 0.9f) / iterations)) { + break; + } + + if (fabs(previous_palette_error-palette_error) < iteration_limit) { + break; + } + + if (palette_error > max_mse*1.5) { // probably hopeless + if (palette_error > max_mse*3.0) break; // definitely hopeless + i++; + } + + previous_palette_error = palette_error; + } + } + + if (palette_error > max_mse) { + liq_verbose_printf(options, " image degradation MSE=%.3f (Q=%d) exceeded limit of %.3f (%d)", + mse_to_standard_mse(palette_error), mse_to_quality(palette_error), + mse_to_standard_mse(max_mse), mse_to_quality(max_mse)); + pam_freecolormap(acolormap); + return LIQ_QUALITY_TOO_LOW; + } + } + + if (liq_progress(options, options->progress_stage1 + options->progress_stage2 + options->progress_stage3 * 0.95f)) { + pam_freecolormap(acolormap); + return LIQ_ABORTED; + } + + sort_palette(acolormap, options); + + // If palette was created from a multi-image histogram, + // then it shouldn't be optimized for one image during remapping + if (fixed_result_colors) { + for(unsigned int i=0; i < acolormap->colors; i++) { + acolormap->palette[i].fixed = true; + } + } + + liq_result *result = options->malloc(sizeof(liq_result)); + if (!result) return LIQ_OUT_OF_MEMORY; + *result = (liq_result){ + .magic_header = liq_result_magic, + .malloc = options->malloc, + .free = options->free, + .palette = acolormap, + .palette_error = palette_error, + .fast_palette = fast_palette, + .use_dither_map = options->use_dither_map, + .gamma = gamma, + .min_posterization_output = options->min_posterization_output, + }; + *result_output = result; + return LIQ_OK; +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_write_remapped_image(liq_result *result, liq_image *input_image, void *buffer, size_t buffer_size) +{ + if (!CHECK_STRUCT_TYPE(result, liq_result)) { + return LIQ_INVALID_POINTER; + } + if (!CHECK_STRUCT_TYPE(input_image, liq_image)) { + return LIQ_INVALID_POINTER; + } + if (!CHECK_USER_POINTER(buffer)) { + return LIQ_INVALID_POINTER; + } + + const size_t required_size = input_image->width * input_image->height; + if (buffer_size < required_size) { + return LIQ_BUFFER_TOO_SMALL; + } + + unsigned char *rows[input_image->height]; + unsigned char *buffer_bytes = buffer; + for(unsigned int i=0; i < input_image->height; i++) { + rows[i] = &buffer_bytes[input_image->width * i]; + } + return liq_write_remapped_image_rows(result, input_image, rows); +} + +LIQ_EXPORT LIQ_NONNULL liq_error liq_write_remapped_image_rows(liq_result *quant, liq_image *input_image, unsigned char **row_pointers) +{ + if (!CHECK_STRUCT_TYPE(quant, liq_result)) return LIQ_INVALID_POINTER; + if (!CHECK_STRUCT_TYPE(input_image, liq_image)) return LIQ_INVALID_POINTER; + for(unsigned int i=0; i < input_image->height; i++) { + if (!CHECK_USER_POINTER(row_pointers+i) || !CHECK_USER_POINTER(row_pointers[i])) return LIQ_INVALID_POINTER; + } + + if (quant->remapping) { + liq_remapping_result_destroy(quant->remapping); + } + liq_remapping_result *const result = quant->remapping = liq_remapping_result_create(quant); + if (!result) return LIQ_OUT_OF_MEMORY; + + if (!input_image->edges && !input_image->dither_map && quant->use_dither_map) { + contrast_maps(input_image); + } + + if (liq_remap_progress(result, result->progress_stage1 * 0.25f)) { + return LIQ_ABORTED; + } + + /* + ** Step 4: map the colors in the image to their closest match in the + ** new colormap, and write 'em out. + */ + + float remapping_error = result->palette_error; + if (result->dither_level == 0) { + set_rounded_palette(&result->int_palette, result->palette, result->gamma, quant->min_posterization_output); + remapping_error = remap_to_palette(input_image, row_pointers, result->palette, quant->fast_palette); + } else { + const bool generate_dither_map = result->use_dither_map && (input_image->edges && !input_image->dither_map); + if (generate_dither_map) { + // If dithering (with dither map) is required, this image is used to find areas that require dithering + remapping_error = remap_to_palette(input_image, row_pointers, result->palette, quant->fast_palette); + update_dither_map(row_pointers, input_image); + } + + if (liq_remap_progress(result, result->progress_stage1 * 0.5f)) { + return LIQ_ABORTED; + } + + // remapping above was the last chance to do voronoi iteration, hence the final palette is set after remapping + set_rounded_palette(&result->int_palette, result->palette, result->gamma, quant->min_posterization_output); + + if (!remap_to_palette_floyd(input_image, row_pointers, result, MAX(remapping_error*2.4, 16.f/256.f), generate_dither_map)) { + return LIQ_ABORTED; + } + } + + // remapping error from dithered image is absurd, so always non-dithered value is used + // palette_error includes some perceptual weighting from histogram which is closer correlated with dssim + // so that should be used when possible. + if (result->palette_error < 0) { + result->palette_error = remapping_error; + } + + return LIQ_OK; +} + +LIQ_EXPORT int liq_version() { + return LIQ_VERSION; +} diff --git a/libimagequant.h b/libimagequant.h new file mode 100644 index 0000000..ae503b1 --- /dev/null +++ b/libimagequant.h @@ -0,0 +1,137 @@ +/* + * 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 20800 +#define LIQ_VERSION_STRING "2.8.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 + +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_error; + +enum liq_ownership {LIQ_OWN_ROWS=4, LIQ_OWN_PIXELS=8}; + +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(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(liq_attr* attr); +LIQ_EXPORT LIQ_USERESULT liq_error liq_histogram_add_image(liq_histogram *hist, liq_attr *attr, liq_image* image); +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_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(liq_result *result) LIQ_NONNULL; +LIQ_EXPORT int liq_get_quantization_quality(liq_result *result) LIQ_NONNULL; +LIQ_EXPORT double liq_get_remapping_error(liq_result *result) LIQ_NONNULL; +LIQ_EXPORT int liq_get_remapping_quality(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 diff --git a/mediancut.c b/mediancut.c new file mode 100644 index 0000000..9d93d49 --- /dev/null +++ b/mediancut.c @@ -0,0 +1,478 @@ +/* +** © 2009-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 . +*/ +/* +** Copyright (C) 1989, 1991 by Jef Poskanzer. +** Copyright (C) 1997, 2000, 2002 by Greg Roelofs; based on an idea by +** Stefan Schneider. +** +** 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. +*/ + +#include +#include + +#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[], const f_pixel center); + +struct box { + f_pixel color; + f_pixel variance; + double sum, total_error, max_error; + unsigned int ind; + unsigned int colors; +}; + +ALWAYS_INLINE static double variance_diff(double val, const double good_enough); +inline static double variance_diff(double val, const double good_enough) +{ + val *= val; + if (val < good_enough*good_enough) return val*0.25; + return val; +} + +/** 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) +{ + f_pixel mean = box->color; + double variancea=0, variancer=0, varianceg=0, varianceb=0; + + for(unsigned int i = 0; i < box->colors; ++i) { + f_pixel px = achv[box->ind + i].acolor; + double weight = achv[box->ind + i].adjusted_weight; + variancea += variance_diff(mean.a - px.a, 2.0/256.0)*weight; + variancer += variance_diff(mean.r - px.r, 1.0/256.0)*weight; + varianceg += variance_diff(mean.g - px.g, 1.0/256.0)*weight; + varianceb += variance_diff(mean.b - px.b, 1.0/256.0)*weight; + } + + return (f_pixel){ + .a = variancea*(4.0/16.0), + .r = variancer*(7.0/16.0), + .g = varianceg*(9.0/16.0), + .b = varianceb*(5.0/16.0), + }; +} + +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 edge between halfvar parts of the set */ +static hist_item *hist_item_sort_halfvar(hist_item *base, unsigned int len, double *const lowervar, const double halfvar) +{ + 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 + unsigned int t = 0; double tmpsum = *lowervar; + while (t <= l && tmpsum < halfvar) tmpsum += base[t++].color_weight; + + if (tmpsum < halfvar) { + *lowervar = tmpsum; + } else { + if (l > 0) { + hist_item *res = hist_item_sort_halfvar(base, l, lowervar, halfvar); + if (res) return res; + } else { + // End of left recursion. This will be executed in order from the first element. + *lowervar += base[0].color_weight; + if (*lowervar > halfvar) return &base[0]; + } + } + + if (len > r) { + base += r; len -= r; // tail-recursive "call" + } else { + *lowervar += base[r].color_weight; + return (*lowervar > halfvar) ? &base[r] : NULL; + } + } 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(r), b->variance.r}, + {index_of_channel(g), b->variance.g}, + {index_of_channel(b), b->variance.b}, + {index_of_channel(a), b->variance.a}, + }; + + qsort(channels, 4, sizeof(channels[0]), comparevariance); + + for(unsigned int i=0; i < b->colors; i++) { + const float *chans = (const float *)&achv[b->ind + 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[b->ind + 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; + 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], (f_pixel){0.5,0.5,0.5,0.5}); +} + +/* + ** 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); + // if color is "good enough", don't split further + if (diff < 1.f/256.f/256.f) diff /= 2.f; + 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 colormap *map, 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; +} + +/* + ** 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]; + + /* + ** Set up the initial box. + */ + bv[0].ind = 0; + bv[0].colors = hist->size; + bv[0].color = averagepixels(bv[0].colors, &achv[bv[0].ind], (f_pixel){0.5,0.5,0.5,0.5}); + bv[0].variance = box_variance(achv, &bv[0]); + bv[0].max_error = box_max_error(achv, &bv[0]); + bv[0].sum = 0; + bv[0].total_error = -1; + for(unsigned int i=0; i < bv[0].colors; i++) bv[0].sum += achv[i].adjusted_weight; + + unsigned int boxes = 1; + + /* + ** 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); + double lowervar=0; + + // 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. + hist_item *break_p = hist_item_sort_halfvar(&achv[indx], clrs, &lowervar, halfvar); + unsigned int break_at = MIN(clrs-1, break_p - &achv[indx] + 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; + + const f_pixel previous_center = bv[bi].color; + bv[bi].colors = break_at; + bv[bi].sum = lowersum; + bv[bi].color = averagepixels(bv[bi].colors, &achv[bv[bi].ind], previous_center); + bv[bi].total_error = -1; + bv[bi].variance = box_variance(achv, &bv[bi]); + bv[bi].max_error = box_max_error(achv, &bv[bi]); + bv[boxes].ind = indx + break_at; + bv[boxes].colors = clrs - break_at; + bv[boxes].sum = sm - lowersum; + bv[boxes].color = averagepixels(bv[boxes].colors, &achv[bv[boxes].ind], previous_center); + bv[boxes].total_error = -1; + bv[boxes].variance = box_variance(achv, &bv[boxes]); + bv[boxes].max_error = box_max_error(achv, &bv[boxes]); + + ++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, map, 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 colormap *map, 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].adjusted_weight *= sqrt(1.0 +colordifference(map->palette[bi].acolor, achv[i].acolor)/2.0); + achv[i].tmp.likely_colormap_index = bi; + } + } +} + +static f_pixel averagepixels(unsigned int clrs, const hist_item achv[], f_pixel center) +{ + double r = 0, g = 0, b = 0, a = 0, sum = 0; + + for(unsigned int i = 0; i < clrs; i++) { + const f_pixel px = achv[i].acolor; + const double weight = achv[i].adjusted_weight; + + sum += weight; + r += px.r * weight; + g += px.g * weight; + b += px.b * weight; + a += px.a * 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}; +} diff --git a/mediancut.h b/mediancut.h new file mode 100644 index 0000000..d97696c --- /dev/null +++ b/mediancut.h @@ -0,0 +1,2 @@ + +LIQ_PRIVATE colormap *mediancut(histogram *hist, unsigned int newcolors, const double target_mse, const double max_mse, void* (*malloc)(size_t), void (*free)(void*)); diff --git a/mempool.c b/mempool.c new file mode 100644 index 0000000..cd345a7 --- /dev/null +++ b/mempool.c @@ -0,0 +1,81 @@ +/* +© 2011-2016 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 . +*/ + +#include "libimagequant.h" +#include "mempool.h" +#include +#include +#include + +#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(mempool *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; + } + + mempool 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(mempool *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(mempool m) +{ + while (m) { + mempool next = m->next; + m->free(m); + m = next; + } +} diff --git a/mempool.h b/mempool.h new file mode 100644 index 0000000..0797e7c --- /dev/null +++ b/mempool.h @@ -0,0 +1,13 @@ +#ifndef MEMPOOL_H +#define MEMPOOL_H + +#include + +struct mempool; +typedef struct mempool *mempool; + +LIQ_PRIVATE void* mempool_create(mempool *mptr, const unsigned int size, unsigned int capacity, void* (*malloc)(size_t), void (*free)(void*)); +LIQ_PRIVATE void* mempool_alloc(mempool *mptr, const unsigned int size, const unsigned int capacity); +LIQ_PRIVATE void mempool_destroy(mempool m); + +#endif diff --git a/nearest.c b/nearest.c new file mode 100644 index 0000000..448ab3a --- /dev/null +++ b/nearest.c @@ -0,0 +1,206 @@ +/* +© 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 . +*/ + +#include "libimagequant.h" +#include "pam.h" +#include "nearest.h" +#include "mempool.h" +#include + +typedef struct vp_sort_tmp { + float distance_squared; + unsigned int idx; +} vp_sort_tmp; + +typedef struct vp_search_tmp { + float distance; + unsigned int idx; + int exclude; +} vp_search_tmp; + +typedef struct vp_node { + struct vp_node *near, *far; + f_pixel vantage_point; + float radius; + unsigned int idx; +} vp_node; + +struct nearest_map { + vp_node *root; + const colormap_item *palette; + float nearest_other_color_dist[256]; + mempool 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(mempool *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, + }; + 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), + }; + 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, const bool fast) { + mempool 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); + + 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, + .exclude = i, + }; + vp_search_node(root, &map->palette[i].acolor, &best); + handle->nearest_other_color_dist[i] = best.distance * best.distance / 4.0; // 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 = sqrtf(colordifference(node->vantage_point, *needle)); + + if (distance < best_candidate->distance && best_candidate->exclude != node->idx) { + best_candidate->distance = distance; + best_candidate->idx = node->idx; + } + + // Recurse towards most likely candidate first to narrow best candidate's distance as soon as possible + if (distance < node->radius) { + 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 { + break; + } + } 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 { + break; + } + } + } 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), + .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); +} diff --git a/nearest.h b/nearest.h new file mode 100644 index 0000000..0a98ca6 --- /dev/null +++ b/nearest.h @@ -0,0 +1,8 @@ +// +// nearest.h +// pngquant +// +struct nearest_map; +LIQ_PRIVATE struct nearest_map *nearest_init(const colormap *palette, const bool fast); +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); diff --git a/pam.c b/pam.c new file mode 100644 index 0000000..aae2927 --- /dev/null +++ b/pam.c @@ -0,0 +1,276 @@ +/* pam.c - pam (portable alpha map) utility library +** +** Copyright (C) 1989, 1991 by Jef Poskanzer. +** Copyright (C) 1997, 2000, 2002 by Greg Roelofs; based on an idea by +** Stefan Schneider. +** © 2009-2016 by Kornel Lesinski. +** +** 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. +*/ + +#include +#include + +#include "libimagequant.h" +#include "pam.h" +#include "mempool.h" + +LIQ_PRIVATE bool pam_computeacolorhash(struct acolorhash_table *acht, const rgba_pixel *const pixels[], unsigned int cols, unsigned int rows, const unsigned char *importance_map) +{ + const unsigned int maxacolors = acht->maxcolors, ignorebits = acht->ignorebits; + const unsigned int channel_mask = 255U>>ignorebits<>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; + struct acolorhist_arr_head *const buckets = acht->buckets; + + unsigned int colors = acht->colors; + const unsigned int hash_size = acht->hash_size; + + const unsigned int stacksize = sizeof(acht->freestack)/sizeof(acht->freestack[0]); + struct acolorhist_arr_item **freestack = acht->freestack; + unsigned int freestackp=acht->freestackp; + + /* Go through the entire image, building a hash table of colors. */ + for(unsigned int row = 0; row < rows; ++row) { + + float boost=1.0; + for(unsigned int col = 0; col < cols; ++col) { + if (importance_map) { + boost = 0.5f+ (double)*importance_map++/255.f; + } + + // 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) { + // "dirty alpha" has different RGBA values that end up being the same fully transparent color + px.l=0; hash=0; + boost = 10; + } else { + // 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; + } + + /* 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 = &buckets[hash]; + if (achl->inline1.color.l == px.l && achl->used) { + achl->inline1.perceptual_weight += boost; + continue; + } + if (achl->used) { + if (achl->used > 1) { + if (achl->inline2.color.l == px.l) { + achl->inline2.perceptual_weight += boost; + continue; + } + // 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; + goto continue_outer_loop; + } + } + + // 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++; + ++colors; + continue; + } + + if (++colors > maxacolors) { + acht->colors = colors; + acht->freestackp = freestackp; + 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 (freestackp <= 0) { + // estimate how many colors are going to be + headroom + const size_t mempool_size = ((acht->rows + rows-row) * 2 * 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 = freestack[--freestackp]; + } + } else { + // simply reallocs and copies array to larger capacity + capacity = achl->capacity*2 + 16; + if (freestackp < stacksize-1) { + freestack[freestackp++] = other_items; + } + const size_t mempool_size = ((acht->rows + rows-row) * 2 * 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; + ++colors; + } + } else { + achl->inline1.color.l = px.l; + achl->inline1.perceptual_weight = boost; + achl->used = 1; + ++colors; + } + + continue_outer_loop:; + } + + } + acht->colors = colors; + acht->cols = cols; + acht->rows += rows; + acht->freestackp = freestackp; + 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); + + mempool 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; +} + +#define PAM_ADD_TO_HIST(entry) { \ + hist->achv[j].acolor = to_f(gamma_lut, entry.color.rgba); \ + total_weight += hist->achv[j].adjusted_weight = hist->achv[j].perceptual_weight = MIN(entry.perceptual_weight, max_perceptual_weight); \ + ++j; \ +} + +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; + + float gamma_lut[256]; + to_f_set_gamma(gamma_lut, gamma); + + /* 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; + + for(unsigned int j=0, i=0; i < acht->hash_size; ++i) { + const struct acolorhist_arr_head *const achl = &acht->buckets[i]; + if (achl->used) { + PAM_ADD_TO_HIST(achl->inline1); + + if (achl->used > 1) { + PAM_ADD_TO_HIST(achl->inline2); + + for(unsigned int k=0; k < achl->used-2; k++) { + PAM_ADD_TO_HIST(achl->other_items[k]); + } + } + } + } + + hist->total_perceptual_weight = total_weight; + 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 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); + } +} + diff --git a/pam.h b/pam.h new file mode 100644 index 0000000..3af703e --- /dev/null +++ b/pam.h @@ -0,0 +1,270 @@ +/* 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 + +#include +#include +#include +#include + +#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 +# ifdef _MSC_VER +# include +# 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 + +#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 { + unsigned char r, g, b, a; +} rgba_pixel; + +typedef struct { + float a, r, g, b; +} SSE_ALIGN f_pixel; + +static const double internal_gamma = 0.5499; + +LIQ_PRIVATE void to_f_set_gamma(float gamma_lut[], const double gamma); + +/** + 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 to_f(const float gamma_lut[], const rgba_pixel px); +inline static f_pixel to_f(const float gamma_lut[], const rgba_pixel px) +{ + float a = px.a/255.f; + + return (f_pixel) { + .a = a, + .r = gamma_lut[px.r]*a, + .g = gamma_lut[px.g]*a, + .b = gamma_lut[px.b]*a, + }; +} + +inline static rgba_pixel to_rgb(const float gamma, const f_pixel px) +{ + if (px.a < 1.f/256.f) { + return (rgba_pixel){0,0,0,0}; + } + + float r = px.r / px.a, + g = px.g / px.a, + b = px.b / px.a, + a = 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; + a *= 256.f; + + return (rgba_pixel){ + .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 double colordifference_ch(const double x, const double y, const double alphas); +inline static double colordifference_ch(const double x, const double y, const double alphas) +{ + // maximum of channel blended on white, and blended on black + // premultiplied alpha and backgrounds 0/1 shorten the formula + const double 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 double 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 + const __m128 vpx = _mm_load_ps((const float*)&px); + const __m128 vpy = _mm_load_ps((const float*)&py); + + // 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 { + rgba_pixel 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; + +typedef struct { + hist_item *achv; + void (*free)(void*); + double total_perceptual_weight; + unsigned int size; + unsigned int ignorebits; +} histogram; + +typedef struct { + f_pixel acolor; + float popularity; + bool fixed; // if true it's user-supplied and must not be changed (e.g in voronoi 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; + float 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 rgba_pixel *const pixels[], unsigned int cols, unsigned int rows, const unsigned char *importance_map); + +LIQ_PRIVATE void pam_freeacolorhist(histogram *h); + +LIQ_PRIVATE colormap *pam_colormap(unsigned int colors, void* (*malloc)(size_t), void (*free)(void*)); +LIQ_PRIVATE colormap *pam_duplicate_colormap(colormap *map); +LIQ_PRIVATE void pam_freecolormap(colormap *c); + +#endif diff --git a/viter.c b/viter.c new file mode 100644 index 0000000..f526326 --- /dev/null +++ b/viter.c @@ -0,0 +1,107 @@ +/* +© 2011-2016 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 . +*/ + +#include "libimagequant.h" +#include "pam.h" +#include "viter.h" +#include "nearest.h" +#include +#include + +#ifdef _OPENMP +#include +#else +#define omp_get_max_threads() 1 +#define omp_get_thread_num() 0 +#endif + +/* + * Voronoi iteration: new palette color is computed from weighted average of colors that map to that palette entry. + */ +LIQ_PRIVATE void viter_init(const colormap *map, const unsigned int max_threads, viter_state average_color[]) +{ + memset(average_color, 0, sizeof(average_color[0])*(VITER_CACHE_LINE_GAP+map->colors)*max_threads); +} + +LIQ_PRIVATE void viter_update_color(const f_pixel acolor, const float value, const colormap *map, unsigned int match, const unsigned int thread, viter_state average_color[]) +{ + match += thread * (VITER_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 viter_finalize(colormap *map, const unsigned int max_threads, const viter_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 = (VITER_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 (total && !map->palette[i].fixed) { + map->palette[i].acolor = (f_pixel){ + .a = a / total, + .r = r / total, + .g = g / total, + .b = b / total, + }; + map->palette[i].popularity = total; + } + } +} + +LIQ_PRIVATE double viter_do_iteration(histogram *hist, colormap *const map, viter_callback callback, const bool fast_palette) +{ + const unsigned int max_threads = omp_get_max_threads(); + viter_state average_color[(VITER_CACHE_LINE_GAP+map->colors) * max_threads]; + viter_init(map, max_threads, average_color); + struct nearest_map *const n = nearest_init(map, fast_palette); + hist_item *const achv = hist->achv; + const int hist_size = hist->size; + + double total_diff=0; + #pragma omp parallel for if (hist_size > 3000) \ + schedule(static) default(none) shared(average_color,callback) reduction(+:total_diff) + for(int j=0; j < hist_size; j++) { + float diff; + unsigned int match = nearest_search(n, &achv[j].acolor, achv[j].tmp.likely_colormap_index, &diff); + achv[j].tmp.likely_colormap_index = match; + total_diff += diff * achv[j].perceptual_weight; + + viter_update_color(achv[j].acolor, achv[j].perceptual_weight, map, match, omp_get_thread_num(), average_color); + + if (callback) callback(&achv[j], diff); + } + + nearest_free(n); + viter_finalize(map, max_threads, average_color); + + return total_diff / hist->total_perceptual_weight; +} diff --git a/viter.h b/viter.h new file mode 100644 index 0000000..bbbaaa1 --- /dev/null +++ b/viter.h @@ -0,0 +1,19 @@ + +#ifndef VITER_H +#define VITER_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 VITER_CACHE_LINE_GAP ((64+sizeof(viter_state)-1)/sizeof(viter_state)) + +typedef struct { + double a, r, g, b, total; +} viter_state; + +typedef void (*viter_callback)(hist_item *item, float diff); + +LIQ_PRIVATE void viter_init(const colormap *map, const unsigned int max_threads, viter_state state[]); +LIQ_PRIVATE void viter_update_color(const f_pixel acolor, const float value, const colormap *map, unsigned int match, const unsigned int thread, viter_state average_color[]); +LIQ_PRIVATE void viter_finalize(colormap *map, const unsigned int max_threads, const viter_state state[]); +LIQ_PRIVATE double viter_do_iteration(histogram *hist, colormap *const map, viter_callback callback, const bool fast_palette); + +#endif