From edc04786649704c88b4d002fa67ac948ffa8a13abde0ddb45a3b4b552b35c358 Mon Sep 17 00:00:00 2001 From: Gabriel Lorenzo Date: Sat, 29 Jan 2005 15:26:21 +0000 Subject: [PATCH] =?UTF-8?q?se=20me=20habia=20olvidao=20a=C3=B1adir=20esto?= =?UTF-8?q?=20:P?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/cosas utiles/SDL_gfx/LICENSE | 456 ++ util/cosas utiles/SDL_gfx/README | 277 ++ util/cosas utiles/SDL_gfx/SDL_framerate.c | 84 + util/cosas utiles/SDL_gfx/SDL_framerate.h | 65 + util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.c | 3660 +++++++++++++++++ util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.h | 181 + util/cosas utiles/SDL_gfx/SDL_rotozoom.c | 939 +++++ util/cosas utiles/SDL_gfx/SDL_rotozoom.h | 99 + util/cosas utiles/kplib/kplib.c | 2889 +++++++++++++ util/cosas utiles/kplib/kplib.h | 31 + util/cosas utiles/kplib/leeme.txt | 8 + 11 files changed, 8689 insertions(+) create mode 100644 util/cosas utiles/SDL_gfx/LICENSE create mode 100644 util/cosas utiles/SDL_gfx/README create mode 100644 util/cosas utiles/SDL_gfx/SDL_framerate.c create mode 100644 util/cosas utiles/SDL_gfx/SDL_framerate.h create mode 100644 util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.c create mode 100644 util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.h create mode 100644 util/cosas utiles/SDL_gfx/SDL_rotozoom.c create mode 100644 util/cosas utiles/SDL_gfx/SDL_rotozoom.h create mode 100644 util/cosas utiles/kplib/kplib.c create mode 100644 util/cosas utiles/kplib/kplib.h create mode 100644 util/cosas utiles/kplib/leeme.txt diff --git a/util/cosas utiles/SDL_gfx/LICENSE b/util/cosas utiles/SDL_gfx/LICENSE new file mode 100644 index 0000000..202ab27 --- /dev/null +++ b/util/cosas utiles/SDL_gfx/LICENSE @@ -0,0 +1,456 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +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 this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +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 +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser 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 Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "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 +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY 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 +LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + diff --git a/util/cosas utiles/SDL_gfx/README b/util/cosas utiles/SDL_gfx/README new file mode 100644 index 0000000..c59bf93 --- /dev/null +++ b/util/cosas utiles/SDL_gfx/README @@ -0,0 +1,277 @@ + +SDL_gfx - SDL graphics drawing primitives and other support functions +============================================================================= + +Email aschiffler@appwares.ca to contact the author or better check +author's homepage at http://www.ferzkopp.net for the most up-to-date +contact information. + +This library is licenced under the LGPL, see the file LICENSE for details. + + +Intro +----- + +The SDL_gfx library evolved out of the SDL_gfxPrimitives code which +provided basic drawing routines such as lines, circles or polygons for +SDL Surfaces and adding a couple other useful functions for zooming +images for example and doing basic image processing on byte arrays. + +The current components of the SDL_gfx library are: +- Graphic Primitives (SDL_gfxPrimitves.h) +- Rotozoomer (SDL_rotozoom.h) +- Framerate control (SDL_framerate.h) +- MMX image filters (SDL_imageFilter.h) + +See ./Docs directory for a longer HTML version of this document. + + +Supported Platforms +------------------- + +The library compiles and is tested for a Linux target (gcc compiler) and +a Win32 target (VisualC6/7, xmingw32 cross-compiler). MacOS X target is +reported to work (Project Builder). + +When using the cross-compiler (available on the author's homepage), +the build process generates .DLLs. You can use the command line 'LIB.EXE' +tool to generate VC6 compatible .LIB files for linking purposes. + +Other platforms might work but have not been tested by the author. + + +Notes on Graphics Primitives +---------------------------- + +Care has been taken so that all routines are fully alpha-aware and can +blend any primitive onto the target surface if ALPHA<255. Surface depths +supported are 1,2,3 and 4 bytes per pixel. Surface locking is implemented +in each routine and the library should work well with hardware +accelerated surfaces. + +Currently, The following Anti-Aliased drawing primitives are available: +- AA-line +- AA-polygon +- AA-circle +- AA-ellipse + +[[[ Interface ]]] + +See SDL_gfxPrimitives.h for all the drawing functions. + + +Notes on Rotozoomer +------------------- + +The rotozoom code is not ASSEMBLY quality - but it should be fast enough +even for some realtime effects if the CPU is good or bitmaps small. +With interpolation the routines are typically used for pre-rendering stuff +in higher quality (i.e. smoothing) - that's also a reason why the API differs +from SDL_BlitRect() and creates a new target surface each time rotozoom +is called. The final rendering speed is dependent on the target surface +size as it is beeing xy-scanned when rendering the new surface. + +Note also that the smoothing toggle is dependent on the input surface bit +depth. 8bit surfaces will never be smoothed - only 32bit surfaces will. + +Note that surfaces of other bit depth then 8 and 32 will be converted +on the fly to a 32bit surface using a blit into a temporary surface. This +impacts performance somewhat. + + +[[[ Interface ]]] + +SDL_Surface * rotozoomSurface (SDL_Surface *src, double angle, double zoom, int smooth); + + Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + + + +SDL_Surface * zoomSurface (SDL_Surface *src, double zoomx, double zoomy, int smooth); + + Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + + +Smoothing (interpolation) flags work only on 32bit surfaces: + + #define SMOOTHING_OFF 0 + #define SMOOTHING_ON 1 + + +Notes on framerate functions +---------------------------- + +The framerate functions are used to insert delays into the graphics loop +to maintain a constant framerate. + +The implementation is more sophisticated that the usual + SDL_Delay(1000/FPS); +call since these functions keep track of the desired game time per frame +for a linearly interpolated sequence of future timing points of each frame. +This is done to avoid rounding errors from the inherent instability in the +delay generation and application. + +i.e. the 100th frame of a game running at 50Hz will be accurately +2.00sec after the 1st frame (if the machine can keep up with the drawing). + + +[[[ Interface ]]] + +The functions return 0 or 'value' for sucess and -1 for error. All functions +use a pointer to a framerate-manager variable to operate. + +void SDL_initFramerate(FPSmanager * manager); + + Initialize the framerate manager, set default framerate of 30Hz and + reset delay interpolation. + + +int SDL_setFramerate(FPSmanager * manager, int rate); + + Set a new framerate for the manager and reset delay interpolation. + + +int SDL_getFramerate(FPSmanager * manager); + + Get the currently set framerate of the manager. + + +void SDL_framerateDelay(FPSmanager * manager); + + Generate a delay to accomodate currently set framerate. Call once in the + graphics/rendering loop. If the computer cannot keep up with the rate (i.e. + drawing too slow), the delay is zero and the delay interpolation is reset. + + +Notes on imageFilter functions +------------------------------ + +The imagefilter functions are a collection of MMX optimized routines that +operate on continuous buffers of bytes - typically greyscale images from +framegrabbers and such - performing functions such as image addition and +binarization. All functions (almost .. not the the convolution routines) +have a C implementation that is automatically used on systems without MMX +capabilities. + + +[[[ Interface ]]] + +See the extensive list of routines in SDL_imageFilters.h for info. + + + +Installation and Test +--------------------- + +To compile the library your need the SDL 1.2 installed from source or +with the 'devel' RPM package. + +Run + ./autogen.sh (optional) + ./configure + make + make install + ldconfig + +to compile and install the library. The default location for the +installation is /usr/local/lib and /usr/local/include. + +Run the shell script 'nodebug.sh' before make, to patch the makefile +for optimized compilation: + ./autogen.sh (optional) + ./configure + ./nodebug.sh + make + make install + ldconfig + +To create a Windows DLL using VisualC: + unzip -a VisualC6.zip + vcvars32.bat + copy VisualC/makefile + nmake +or + unzip -a VisualC7.zip +and open the project file. + + +To create a Windows DLL using the xmingw32 cross-compiler: + cross-configure + cross-make + cross-make install + + +To build without MMX code enabled (i.e. PPC architecture): + ./configure --disable-mmx + make + make install + +To build on MacOS X with Project Builder, follow these steps: + * Update your developer tools to the lastest version (December 2002 as + of this revision). + * Install the SDL Developers framework for Mac OS X. + * Download the latest SDL_gfx source distribution and extract the + archive in a convenient location. + * Extract the included OSX-PB.tgz archive into the + top directory of the SDL_gfx distribution (from step 3). This will create a + PB that contains the project files. + * The project has targets for the SDL_gfx framework and the four test + programs. All can be built using the 'deployment' or 'development' build + styles. + + +Test Programs +------------- + +Change to the ./Test directory and run + ./configure + make +to create several test programs for the libraries functions. This requires +the library to be compiled and installed. + + +See the source code .c files for sample code. + + +Thanks +------ + +Thanks to 'AppWares Development Group' for supporting this project - please +visit http://www.appwares.com for more information. + + +Contributors +------------ + +* Non-alpha line drawing code adapted from routine + by Pete Shinners, pete@shinners.org - thanks Pete. + +* More fixes by Karl Bartel, karlb@gmx.net - thanks Karl. + +* Much testing and suggestions for fixes from Danny van Bruggen, + danny@froukepc.dhs.org - thanks Danny. + +* Original AA-circle/-ellipse code idea from Stephane Magnenat, + nct@wg0.ysagoon.com - thanks Stephane. + +* Faster blending routines contributed by Anders Lindström, + cal@swipnet.se - thanks Anders. + +* New AA-circle/-ellipse code based on ideas from Anders Lindström - + thanks Anders. + +* VisualC makefile contributed by Danny van Bruggen, + danny@froukepc.dhs.org - thanks Danny. + +* VisualC7 project file contributed by James Turk, + jturk@conceptofzero.com - thanks James. + +* Project Builder package contributed by Thomas Tongue, + TTongue@imagiware.com - Thanks Thomas. + diff --git a/util/cosas utiles/SDL_gfx/SDL_framerate.c b/util/cosas utiles/SDL_gfx/SDL_framerate.c new file mode 100644 index 0000000..ada83e3 --- /dev/null +++ b/util/cosas utiles/SDL_gfx/SDL_framerate.c @@ -0,0 +1,84 @@ + +/* + + SDL_framerate: framerate manager + + LGPL (c) A. Schiffler + + */ + +#include "SDL_framerate.h" + +/* + Initialize the framerate manager +*/ + +void SDL_initFramerate(FPSmanager * manager) +{ + /* + * Store some sane values + */ + manager->framecount = 0; + manager->rate = FPS_DEFAULT; + manager->rateticks = (1000.0 / (float) FPS_DEFAULT); + manager->lastticks = SDL_GetTicks(); +} + +/* + Set the framerate in Hz +*/ + +int SDL_setFramerate(FPSmanager * manager, int rate) +{ + if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) { + manager->framecount = 0; + manager->rate = rate; + manager->rateticks = (1000.0 / (float) rate); + return (0); + } else { + return (-1); + } +} + +/* + Return the current target framerate in Hz +*/ + +int SDL_getFramerate(FPSmanager * manager) +{ + if (manager == NULL) { + return (-1); + } else { + return (manager->rate); + } +} + +/* + Delay execution to maintain a constant framerate. Calculate fps. +*/ + +void SDL_framerateDelay(FPSmanager * manager) +{ + Uint32 current_ticks; + Uint32 target_ticks; + Uint32 the_delay; + + /* + * Next frame + */ + manager->framecount++; + + /* + * Get/calc ticks + */ + current_ticks = SDL_GetTicks(); + target_ticks = manager->lastticks + (Uint32) ((float) manager->framecount * manager->rateticks); + + if (current_ticks <= target_ticks) { + the_delay = target_ticks - current_ticks; + SDL_Delay(the_delay); + } else { + manager->framecount = 0; + manager->lastticks = SDL_GetTicks(); + } +} diff --git a/util/cosas utiles/SDL_gfx/SDL_framerate.h b/util/cosas utiles/SDL_gfx/SDL_framerate.h new file mode 100644 index 0000000..723b23f --- /dev/null +++ b/util/cosas utiles/SDL_gfx/SDL_framerate.h @@ -0,0 +1,65 @@ + +/* + + SDL_framerate: framerate manager + + LGPL (c) A. Schiffler + + */ + +#ifndef _SDL_framerate_h +#define _SDL_framerate_h + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* --- */ + +#include + +/* --------- Definitions */ + +/* Some rates in Hz */ + +#define FPS_UPPER_LIMIT 200 +#define FPS_LOWER_LIMIT 1 +#define FPS_DEFAULT 30 + +/* --------- Structure variables */ + + typedef struct { + Uint32 framecount; + float rateticks; + Uint32 lastticks; + Uint32 rate; + } FPSmanager; + +/* --------- Function prototypes */ + +#ifdef WIN32 +#ifdef BUILD_DLL +#define DLLINTERFACE __declspec(dllexport) +#else +#define DLLINTERFACE __declspec(dllimport) +#endif +#else +#define DLLINTERFACE +#endif + +/* Functions return 0 or value for sucess and -1 for error */ + + DLLINTERFACE void SDL_initFramerate(FPSmanager * manager); + DLLINTERFACE int SDL_setFramerate(FPSmanager * manager, int rate); + DLLINTERFACE int SDL_getFramerate(FPSmanager * manager); + DLLINTERFACE void SDL_framerateDelay(FPSmanager * manager); + +/* --- */ + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +}; +#endif + +#endif /* _SDL_framerate_h */ diff --git a/util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.c b/util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.c new file mode 100644 index 0000000..de34b67 --- /dev/null +++ b/util/cosas utiles/SDL_gfx/SDL_gfxPrimitives.c @@ -0,0 +1,3660 @@ +/* + + SDL_gfxPrimitives - Graphics primitives for SDL surfaces + + LGPL (c) A. Schiffler + +*/ + +#include +#include +#include +#include + +#include "SDL_gfxPrimitives.h" +#include "SDL_gfxPrimitives_font.h" + +/* -===================- */ + +/* ----- Defines for pixel clipping tests */ + +#define clip_xmin(surface) surface->clip_rect.x +#define clip_xmax(surface) surface->clip_rect.x+surface->clip_rect.w-1 +#define clip_ymin(surface) surface->clip_rect.y +#define clip_ymax(surface) surface->clip_rect.y+surface->clip_rect.h-1 + +/* ----- Pixel - fast, no blending, no locking, clipping */ + +int fastPixelColorNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color) +{ + int bpp; + Uint8 *p; + + /* + * Honor clipping setup at pixel level + */ + if ((x >= clip_xmin(dst)) && (x <= clip_xmax(dst)) && (y >= clip_ymin(dst)) && (y <= clip_ymax(dst))) { + + /* + * Get destination format + */ + bpp = dst->format->BytesPerPixel; + p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp; + switch (bpp) { + case 1: + *p = color; + break; + case 2: + *(Uint16 *) p = color; + break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + p[0] = (color >> 16) & 0xff; + p[1] = (color >> 8) & 0xff; + p[2] = color & 0xff; + } else { + p[0] = color & 0xff; + p[1] = (color >> 8) & 0xff; + p[2] = (color >> 16) & 0xff; + } + break; + case 4: + *(Uint32 *) p = color; + break; + } /* switch */ + + + } + + return (0); +} + +/* ----- Pixel - fast, no blending, no locking, no clipping */ + +/* (faster but dangerous, make sure we stay in surface bounds) */ + +int fastPixelColorNolockNoclip(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color) +{ + int bpp; + Uint8 *p; + + /* + * Get destination format + */ + bpp = dst->format->BytesPerPixel; + p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp; + switch (bpp) { + case 1: + *p = color; + break; + case 2: + *(Uint16 *) p = color; + break; + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + p[0] = (color >> 16) & 0xff; + p[1] = (color >> 8) & 0xff; + p[2] = color & 0xff; + } else { + p[0] = color & 0xff; + p[1] = (color >> 8) & 0xff; + p[2] = (color >> 16) & 0xff; + } + break; + case 4: + *(Uint32 *) p = color; + break; + } /* switch */ + + return (0); +} + +/* ----- Pixel - fast, no blending, locking, clipping */ + +int fastPixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color) +{ + int result; + + /* + * Lock the surface + */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + result = fastPixelColorNolock(dst, x, y, color); + + /* + * Unlock surface + */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +/* ----- Pixel - fast, no blending, locking, RGB input */ + +int fastPixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Uint32 color; + + /* + * Setup color + */ + color = SDL_MapRGBA(dst->format, r, g, b, a); + + /* + * Draw + */ + return (fastPixelColor(dst, x, y, color)); + +} + +/* ----- Pixel - fast, no blending, no locking RGB input */ + +int fastPixelRGBANolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Uint32 color; + + /* + * Setup color + */ + color = SDL_MapRGBA(dst->format, r, g, b, a); + + /* + * Draw + */ + return (fastPixelColorNolock(dst, x, y, color)); +} + +/* PutPixel routine with alpha blending, input color in destination format */ + +/* New, faster routine - default blending pixel */ + +int _putPixelAlpha(SDL_Surface * surface, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha) +{ + Uint32 Rmask = surface->format->Rmask, Gmask = + surface->format->Gmask, Bmask = surface->format->Bmask, Amask = surface->format->Amask; + Uint32 R, G, B, A = 0; + + if (x >= clip_xmin(surface) && x <= clip_xmax(surface) + && y >= clip_ymin(surface) && y <= clip_ymax(surface)) { + + switch (surface->format->BytesPerPixel) { + case 1:{ /* Assuming 8-bpp */ + if (alpha == 255) { + *((Uint8 *) surface->pixels + y * surface->pitch + x) = color; + } else { + Uint8 *pixel = (Uint8 *) surface->pixels + y * surface->pitch + x; + + Uint8 dR = surface->format->palette->colors[*pixel].r; + Uint8 dG = surface->format->palette->colors[*pixel].g; + Uint8 dB = surface->format->palette->colors[*pixel].b; + Uint8 sR = surface->format->palette->colors[color].r; + Uint8 sG = surface->format->palette->colors[color].g; + Uint8 sB = surface->format->palette->colors[color].b; + + dR = dR + ((sR - dR) * alpha >> 8); + dG = dG + ((sG - dG) * alpha >> 8); + dB = dB + ((sB - dB) * alpha >> 8); + + *pixel = SDL_MapRGB(surface->format, dR, dG, dB); + } + } + break; + + case 2:{ /* Probably 15-bpp or 16-bpp */ + if (alpha == 255) { + *((Uint16 *) surface->pixels + y * surface->pitch / 2 + x) = color; + } else { + Uint16 *pixel = (Uint16 *) surface->pixels + y * surface->pitch / 2 + x; + Uint32 dc = *pixel; + + R = ((dc & Rmask) + (((color & Rmask) - (dc & Rmask)) * alpha >> 8)) & Rmask; + G = ((dc & Gmask) + (((color & Gmask) - (dc & Gmask)) * alpha >> 8)) & Gmask; + B = ((dc & Bmask) + (((color & Bmask) - (dc & Bmask)) * alpha >> 8)) & Bmask; + if (Amask) + A = ((dc & Amask) + (((color & Amask) - (dc & Amask)) * alpha >> 8)) & Amask; + + *pixel = R | G | B | A; + } + } + break; + + case 3:{ /* Slow 24-bpp mode, usually not used */ + Uint8 *pix = (Uint8 *) surface->pixels + y * surface->pitch + x * 3; + Uint8 rshift8 = surface->format->Rshift / 8; + Uint8 gshift8 = surface->format->Gshift / 8; + Uint8 bshift8 = surface->format->Bshift / 8; + Uint8 ashift8 = surface->format->Ashift / 8; + + + if (alpha == 255) { + *(pix + rshift8) = color >> surface->format->Rshift; + *(pix + gshift8) = color >> surface->format->Gshift; + *(pix + bshift8) = color >> surface->format->Bshift; + *(pix + ashift8) = color >> surface->format->Ashift; + } else { + Uint8 dR, dG, dB, dA = 0; + Uint8 sR, sG, sB, sA = 0; + + pix = (Uint8 *) surface->pixels + y * surface->pitch + x * 3; + + dR = *((pix) + rshift8); + dG = *((pix) + gshift8); + dB = *((pix) + bshift8); + dA = *((pix) + ashift8); + + sR = (color >> surface->format->Rshift) & 0xff; + sG = (color >> surface->format->Gshift) & 0xff; + sB = (color >> surface->format->Bshift) & 0xff; + sA = (color >> surface->format->Ashift) & 0xff; + + dR = dR + ((sR - dR) * alpha >> 8); + dG = dG + ((sG - dG) * alpha >> 8); + dB = dB + ((sB - dB) * alpha >> 8); + dA = dA + ((sA - dA) * alpha >> 8); + + *((pix) + rshift8) = dR; + *((pix) + gshift8) = dG; + *((pix) + bshift8) = dB; + *((pix) + ashift8) = dA; + } + } + break; + + case 4:{ /* Probably 32-bpp */ + if (alpha == 255) { + *((Uint32 *) surface->pixels + y * surface->pitch / 4 + x) = color; + } else { + Uint32 *pixel = (Uint32 *) surface->pixels + y * surface->pitch / 4 + x; + Uint32 dc = *pixel; + + R = ((dc & Rmask) + (((color & Rmask) - (dc & Rmask)) * alpha >> 8)) & Rmask; + G = ((dc & Gmask) + (((color & Gmask) - (dc & Gmask)) * alpha >> 8)) & Gmask; + B = ((dc & Bmask) + (((color & Bmask) - (dc & Bmask)) * alpha >> 8)) & Bmask; + if (Amask) + A = ((dc & Amask) + (((color & Amask) - (dc & Amask)) * alpha >> 8)) & Amask; + + *pixel = R | G | B | A; + } + } + break; + } + } + + return (0); +} + +/* ----- Pixel - pixel draw with blending enabled if a<255 */ + +int pixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color) +{ + Uint8 alpha; + Uint32 mcolor; + int result = 0; + + /* + * Lock the surface + */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Setup color + */ + alpha = color & 0x000000ff; + mcolor = + SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24, + (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha); + + /* + * Draw + */ + result = _putPixelAlpha(dst, x, y, mcolor, alpha); + + /* + * Unlock the surface + */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +int pixelColorNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color) +{ + Uint8 alpha; + Uint32 mcolor; + int result = 0; + + /* + * Setup color + */ + alpha = color & 0x000000ff; + mcolor = + SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24, + (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha); + + /* + * Draw + */ + result = _putPixelAlpha(dst, x, y, mcolor, alpha); + + return (result); +} + + +/* Filled rectangle with alpha blending, color in destination format */ + +int _filledRectAlpha(SDL_Surface * surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha) +{ + Uint32 Rmask = surface->format->Rmask, Gmask = + surface->format->Gmask, Bmask = surface->format->Bmask, Amask = surface->format->Amask; + Uint32 R, G, B, A = 0; + Sint16 x, y; + + switch (surface->format->BytesPerPixel) { + case 1:{ /* Assuming 8-bpp */ + Uint8 *row, *pixel; + Uint8 dR, dG, dB; + + Uint8 sR = surface->format->palette->colors[color].r; + Uint8 sG = surface->format->palette->colors[color].g; + Uint8 sB = surface->format->palette->colors[color].b; + + for (y = y1; y <= y2; y++) { + row = (Uint8 *) surface->pixels + y * surface->pitch; + for (x = x1; x <= x2; x++) { + pixel = row + x; + + dR = surface->format->palette->colors[*pixel].r; + dG = surface->format->palette->colors[*pixel].g; + dB = surface->format->palette->colors[*pixel].b; + + dR = dR + ((sR - dR) * alpha >> 8); + dG = dG + ((sG - dG) * alpha >> 8); + dB = dB + ((sB - dB) * alpha >> 8); + + *pixel = SDL_MapRGB(surface->format, dR, dG, dB); + } + } + } + break; + + case 2:{ /* Probably 15-bpp or 16-bpp */ + Uint16 *row, *pixel; + Uint32 dR = (color & Rmask), dG = (color & Gmask), dB = (color & Bmask), dA = (color & Amask); + + for (y = y1; y <= y2; y++) { + row = (Uint16 *) surface->pixels + y * surface->pitch / 2; + for (x = x1; x <= x2; x++) { + pixel = row + x; + + R = ((*pixel & Rmask) + ((dR - (*pixel & Rmask)) * alpha >> 8)) & Rmask; + G = ((*pixel & Gmask) + ((dG - (*pixel & Gmask)) * alpha >> 8)) & Gmask; + B = ((*pixel & Bmask) + ((dB - (*pixel & Bmask)) * alpha >> 8)) & Bmask; + if (Amask) + A = ((*pixel & Amask) + ((dA - (*pixel & Amask)) * alpha >> 8)) & Amask; + + *pixel = R | G | B | A; + } + } + } + break; + + case 3:{ /* Slow 24-bpp mode, usually not used */ + Uint8 *row, *pix; + Uint8 dR, dG, dB, dA; + Uint8 rshift8 = surface->format->Rshift / 8; + Uint8 gshift8 = surface->format->Gshift / 8; + Uint8 bshift8 = surface->format->Bshift / 8; + Uint8 ashift8 = surface->format->Ashift / 8; + + Uint8 sR = (color >> surface->format->Rshift) & 0xff; + Uint8 sG = (color >> surface->format->Gshift) & 0xff; + Uint8 sB = (color >> surface->format->Bshift) & 0xff; + Uint8 sA = (color >> surface->format->Ashift) & 0xff; + + for (y = y1; y <= y2; y++) { + row = (Uint8 *) surface->pixels + y * surface->pitch; + for (x = x1; x <= x2; x++) { + pix = row + x * 3; + + dR = *((pix) + rshift8); + dG = *((pix) + gshift8); + dB = *((pix) + bshift8); + dA = *((pix) + ashift8); + + dR = dR + ((sR - dR) * alpha >> 8); + dG = dG + ((sG - dG) * alpha >> 8); + dB = dB + ((sB - dB) * alpha >> 8); + dA = dA + ((sA - dA) * alpha >> 8); + + *((pix) + rshift8) = dR; + *((pix) + gshift8) = dG; + *((pix) + bshift8) = dB; + *((pix) + ashift8) = dA; + } + } + + } + break; + + case 4:{ /* Probably 32-bpp */ + Uint32 *row, *pixel; + Uint32 dR = (color & Rmask), dG = (color & Gmask), dB = (color & Bmask), dA = (color & Amask); + + for (y = y1; y <= y2; y++) { + row = (Uint32 *) surface->pixels + y * surface->pitch / 4; + for (x = x1; x <= x2; x++) { + pixel = row + x; + + R = ((*pixel & Rmask) + ((dR - (*pixel & Rmask)) * alpha >> 8)) & Rmask; + G = ((*pixel & Gmask) + ((dG - (*pixel & Gmask)) * alpha >> 8)) & Gmask; + B = ((*pixel & Bmask) + ((dB - (*pixel & Bmask)) * alpha >> 8)) & Bmask; + if (Amask) + A = ((*pixel & Amask) + ((dA - (*pixel & Amask)) * alpha >> 8)) & Amask; + + *pixel = R | G | B | A; + } + } + } + break; + } + + return (0); +} + +/* Draw rectangle with alpha enabled from RGBA color. */ + +int filledRectAlpha(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color) +{ + Uint8 alpha; + Uint32 mcolor; + int result = 0; + + /* + * Lock the surface + */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Setup color + */ + alpha = color & 0x000000ff; + mcolor = + SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24, + (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha); + + /* + * Draw + */ + result = _filledRectAlpha(dst, x1, y1, x2, y2, mcolor, alpha); + + /* + * Unlock the surface + */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +/* Draw horizontal line with alpha enabled from RGBA color */ + +int HLineAlpha(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color) +{ + return (filledRectAlpha(dst, x1, y, x2, y, color)); +} + + +/* Draw vertical line with alpha enabled from RGBA color */ + +int VLineAlpha(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color) +{ + return (filledRectAlpha(dst, x, y1, x, y2, color)); +} + + +/* Pixel - using alpha weight on color for AA-drawing */ + +int pixelColorWeight(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color, Uint32 weight) +{ + Uint32 a; + + /* + * Get alpha + */ + a = (color & (Uint32) 0x000000ff); + + /* + * Modify Alpha by weight + */ + a = ((a * weight) >> 8); + + return (pixelColor(dst, x, y, (color & (Uint32) 0xffffff00) | (Uint32) a)); +} + +/* Pixel - using alpha weight on color for AA-drawing - no locking */ + +int pixelColorWeightNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color, Uint32 weight) +{ + Uint32 a; + + /* + * Get alpha + */ + a = (color & (Uint32) 0x000000ff); + + /* + * Modify Alpha by weight + */ + a = ((a * weight) >> 8); + + return (pixelColorNolock(dst, x, y, (color & (Uint32) 0xffffff00) | (Uint32) a)); +} + +int pixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Uint32 color; + + /* + * Check Alpha + */ + if (a == 255) { + /* + * No alpha blending required + */ + /* + * Setup color + */ + color = SDL_MapRGBA(dst->format, r, g, b, a); + /* + * Draw + */ + return (fastPixelColor(dst, x, y, color)); + } else { + /* + * Alpha blending required + */ + /* + * Draw + */ + return (pixelColor(dst, x, y, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); + } +} + +/* ----- Horizontal line */ + +/* Just store color including alpha, no blending */ + +int hlineColorStore(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color) +{ + Sint16 left, right, top, bottom; + Uint8 *pixel, *pixellast; + int dx; + int pixx, pixy; + Sint16 w; + Sint16 xtmp; + int result = -1; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Check visibility of hline + */ + if ((x1right) && (x2>right)) { + return(0); + } + if ((ybottom)) { + return (0); + } + + /* + * Clip x + */ + if (x1 < left) { + x1 = left; + } + if (x2 > right) { + x2 = right; + } + + /* + * Swap x1, x2 if required + */ + if (x1 > x2) { + xtmp = x1; + x1 = x2; + x2 = xtmp; + } + + /* + * Calculate width + */ + w = x2 - x1; + + /* + * Sanity check on width + */ + if (w < 0) { + return (0); + } + + /* + * Lock surface + */ + SDL_LockSurface(dst); + + /* + * More variable setup + */ + dx = w; + pixx = dst->format->BytesPerPixel; + pixy = dst->pitch; + pixel = ((Uint8 *) dst->pixels) + pixx * (int) x1 + pixy * (int) y; + + /* + * Draw + */ + switch (dst->format->BytesPerPixel) { + case 1: + memset(pixel, color, dx); + break; + case 2: + pixellast = pixel + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + *(Uint16 *) pixel = color; + } + break; + case 3: + pixellast = pixel + dx + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + pixel[0] = (color >> 16) & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = color & 0xff; + } else { + pixel[0] = color & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = (color >> 16) & 0xff; + } + } + break; + default: /* case 4 */ + dx = dx + dx; + pixellast = pixel + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + *(Uint32 *) pixel = color; + } + break; + } + + /* + * Unlock surface + */ + SDL_UnlockSurface(dst); + + /* + * Set result code + */ + result = 0; + + return (result); +} + +int hlineRGBAStore(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (hlineColorStore(dst, x1, x2, y, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +int hlineColor(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color) +{ + Sint16 left, right, top, bottom; + Uint8 *pixel, *pixellast; + int dx; + int pixx, pixy; + Sint16 w; + Sint16 xtmp; + int result = -1; + Uint8 *colorptr; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Check visibility of hline + */ + if ((x1right) && (x2>right)) { + return(0); + } + if ((ybottom)) { + return (0); + } + + /* + * Clip x + */ + if (x1 < left) { + x1 = left; + } + if (x2 > right) { + x2 = right; + } + + /* + * Swap x1, x2 if required + */ + if (x1 > x2) { + xtmp = x1; + x1 = x2; + x2 = xtmp; + } + + /* + * Calculate width + */ + w = x2 - x1; + + /* + * Sanity check on width + */ + if (w < 0) { + return (0); + } + + /* + * Alpha check + */ + if ((color & 255) == 255) { + + /* + * No alpha-blending required + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + /* + * Lock surface + */ + SDL_LockSurface(dst); + + /* + * More variable setup + */ + dx = w; + pixx = dst->format->BytesPerPixel; + pixy = dst->pitch; + pixel = ((Uint8 *) dst->pixels) + pixx * (int) x1 + pixy * (int) y; + + /* + * Draw + */ + switch (dst->format->BytesPerPixel) { + case 1: + memset(pixel, color, dx); + break; + case 2: + pixellast = pixel + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + *(Uint16 *) pixel = color; + } + break; + case 3: + pixellast = pixel + dx + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + pixel[0] = (color >> 16) & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = color & 0xff; + } else { + pixel[0] = color & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = (color >> 16) & 0xff; + } + } + break; + default: /* case 4 */ + dx = dx + dx; + pixellast = pixel + dx + dx; + for (; pixel <= pixellast; pixel += pixx) { + *(Uint32 *) pixel = color; + } + break; + } + + /* + * Unlock surface + */ + SDL_UnlockSurface(dst); + + /* + * Set result code + */ + result = 0; + + } else { + + /* + * Alpha blending blit + */ + + result = HLineAlpha(dst, x1, x1 + w, y, color); + + } + + return (result); +} + +int hlineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (hlineColor(dst, x1, x2, y, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- Vertical line */ + +int vlineColor(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color) +{ + Sint16 left, right, top, bottom; + Uint8 *pixel, *pixellast; + int dy; + int pixx, pixy; + Sint16 h; + Sint16 ytmp; + int result = -1; + Uint8 *colorptr; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Check visibility of vline + */ + if ((xright)) { + return (0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Clip y + */ + if (y1 < top) { + y1 = top; + } + if (y2 > bottom) { + y2 = bottom; + } + + /* + * Swap y1, y2 if required + */ + if (y1 > y2) { + ytmp = y1; + y1 = y2; + y2 = ytmp; + } + + /* + * Calculate height + */ + h = y2 - y1; + + /* + * Sanity check on height + */ + if (h < 0) { + return (0); + } + + /* + * Alpha check + */ + if ((color & 255) == 255) { + + /* + * No alpha-blending required + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + /* + * Lock surface + */ + SDL_LockSurface(dst); + + /* + * More variable setup + */ + dy = h; + pixx = dst->format->BytesPerPixel; + pixy = dst->pitch; + pixel = ((Uint8 *) dst->pixels) + pixx * (int) x + pixy * (int) y1; + pixellast = pixel + pixy * dy; + + /* + * Draw + */ + switch (dst->format->BytesPerPixel) { + case 1: + for (; pixel <= pixellast; pixel += pixy) { + *(Uint8 *) pixel = color; + } + break; + case 2: + for (; pixel <= pixellast; pixel += pixy) { + *(Uint16 *) pixel = color; + } + break; + case 3: + for (; pixel <= pixellast; pixel += pixy) { + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + pixel[0] = (color >> 16) & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = color & 0xff; + } else { + pixel[0] = color & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = (color >> 16) & 0xff; + } + } + break; + default: /* case 4 */ + for (; pixel <= pixellast; pixel += pixy) { + *(Uint32 *) pixel = color; + } + break; + } + + /* + * Unlock surface + */ + SDL_UnlockSurface(dst); + + /* + * Set result code + */ + result = 0; + + } else { + + /* + * Alpha blending blit + */ + + result = VLineAlpha(dst, x, y1, y1 + h, color); + + } + + return (result); +} + +int vlineRGBA(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (vlineColor(dst, x, y1, y2, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- Rectangle */ + +int rectangleColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color) +{ + int result; + Sint16 w, h, xtmp, ytmp; + + /* + * Swap x1, x2 if required + */ + if (x1 > x2) { + xtmp = x1; + x1 = x2; + x2 = xtmp; + } + + /* + * Swap y1, y2 if required + */ + if (y1 > y2) { + ytmp = y1; + y1 = y2; + y2 = ytmp; + } + + /* + * Calculate width&height + */ + w = x2 - x1; + h = y2 - y1; + + /* + * Sanity check + */ + if ((w < 0) || (h < 0)) { + return (0); + } + + /* + * Test for special cases of straight lines or single point + */ + if (x1 == x2) { + if (y1 == y2) { + return (pixelColor(dst, x1, y1, color)); + } else { + return (vlineColor(dst, x1, y1, y2, color)); + } + } else { + if (y1 == y2) { + return (hlineColor(dst, x1, x2, y1, color)); + } + } + + /* + * Draw rectangle + */ + result = 0; + result |= hlineColor(dst, x1, x2, y1, color); + result |= hlineColor(dst, x1, x2, y2, color); + y1 += 1; + y2 -= 1; + if (y1<=y2) { + result |= vlineColor(dst, x1, y1, y2, color); + result |= vlineColor(dst, x2, y1, y2, color); + } + return (result); + +} + +int rectangleRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (rectangleColor + (dst, x1, y1, x2, y2, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* --------- Clipping routines for line */ + +/* Clipping based heavily on code from */ +/* http://www.ncsa.uiuc.edu/Vis/Graphics/src/clipCohSuth.c */ + +#define CLIP_LEFT_EDGE 0x1 +#define CLIP_RIGHT_EDGE 0x2 +#define CLIP_BOTTOM_EDGE 0x4 +#define CLIP_TOP_EDGE 0x8 +#define CLIP_INSIDE(a) (!a) +#define CLIP_REJECT(a,b) (a&b) +#define CLIP_ACCEPT(a,b) (!(a|b)) + +static int clipEncode(Sint16 x, Sint16 y, Sint16 left, Sint16 top, Sint16 right, Sint16 bottom) +{ + int code = 0; + + if (x < left) { + code |= CLIP_LEFT_EDGE; + } else if (x > right) { + code |= CLIP_RIGHT_EDGE; + } + if (y < top) { + code |= CLIP_TOP_EDGE; + } else if (y > bottom) { + code |= CLIP_BOTTOM_EDGE; + } + return code; +} + +static int clipLine(SDL_Surface * dst, Sint16 * x1, Sint16 * y1, Sint16 * x2, Sint16 * y2) +{ + Sint16 left, right, top, bottom; + int code1, code2; + int draw = 0; + Sint16 swaptmp; + float m; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + while (1) { + code1 = clipEncode(*x1, *y1, left, top, right, bottom); + code2 = clipEncode(*x2, *y2, left, top, right, bottom); + if (CLIP_ACCEPT(code1, code2)) { + draw = 1; + break; + } else if (CLIP_REJECT(code1, code2)) + break; + else { + if (CLIP_INSIDE(code1)) { + swaptmp = *x2; + *x2 = *x1; + *x1 = swaptmp; + swaptmp = *y2; + *y2 = *y1; + *y1 = swaptmp; + swaptmp = code2; + code2 = code1; + code1 = swaptmp; + } + if (*x2 != *x1) { + m = (*y2 - *y1) / (float) (*x2 - *x1); + } else { + m = 1.0f; + } + if (code1 & CLIP_LEFT_EDGE) { + *y1 += (Sint16) ((left - *x1) * m); + *x1 = left; + } else if (code1 & CLIP_RIGHT_EDGE) { + *y1 += (Sint16) ((right - *x1) * m); + *x1 = right; + } else if (code1 & CLIP_BOTTOM_EDGE) { + if (*x2 != *x1) { + *x1 += (Sint16) ((bottom - *y1) / m); + } + *y1 = bottom; + } else if (code1 & CLIP_TOP_EDGE) { + if (*x2 != *x1) { + *x1 += (Sint16) ((top - *y1) / m); + } + *y1 = top; + } + } + } + + return draw; +} + +/* ----- Filled rectangle (Box) */ + +int boxColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color) +{ + Sint16 left, right, top, bottom; + Uint8 *pixel, *pixellast; + int x, dx; + int dy; + int pixx, pixy; + Sint16 w, h, tmp; + int result; + Uint8 *colorptr; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* Check visibility */ + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* Clip all points */ + if (x1right) { + x1=right; + } + if (x2right) { + x2=right; + } + if (y1bottom) { + y1=bottom; + } + if (y2bottom) { + y2=bottom; + } + + /* + * Order coordinates + */ + if (x1 > x2) { + tmp = x1; + x1 = x2; + x2 = tmp; + } + if (y1 > y2) { + tmp = y1; + y1 = y2; + y2 = tmp; + } + + /* + * Test for special cases of straight line or single point + */ + if (x1 == x2) { + if (y1 == y2) { + return (pixelColor(dst, x1, y1, color)); + } else { + return (vlineColor(dst, x1, y1, y2, color)); + } + } + if (y1 == y2) { + return (hlineColor(dst, x1, x2, y1, color)); + } + + + /* + * Calculate width&height + */ + w = x2 - x1; + h = y2 - y1; + + /* + * Alpha check + */ + if ((color & 255) == 255) { + + /* + * No alpha-blending required + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + /* + * Lock surface + */ + SDL_LockSurface(dst); + + /* + * More variable setup + */ + dx = w; + dy = h; + pixx = dst->format->BytesPerPixel; + pixy = dst->pitch; + pixel = ((Uint8 *) dst->pixels) + pixx * (int) x1 + pixy * (int) y1; + pixellast = pixel + pixx * dx + pixy * dy; + + /* + * Draw + */ + switch (dst->format->BytesPerPixel) { + case 1: + for (; pixel <= pixellast; pixel += pixy) { + memset(pixel, (Uint8) color, dx); + } + break; + case 2: + pixy -= (pixx * dx); + for (; pixel <= pixellast; pixel += pixy) { + for (x = 0; x < dx; x++) { + *(Uint16 *) pixel = color; + pixel += pixx; + } + } + break; + case 3: + pixy -= (pixx * dx); + for (; pixel <= pixellast; pixel += pixy) { + for (x = 0; x < dx; x++) { + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + pixel[0] = (color >> 16) & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = color & 0xff; + } else { + pixel[0] = color & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = (color >> 16) & 0xff; + } + pixel += pixx; + } + } + break; + default: /* case 4 */ + pixy -= (pixx * dx); + for (; pixel <= pixellast; pixel += pixy) { + for (x = 0; x < dx; x++) { + *(Uint32 *) pixel = color; + pixel += pixx; + } + } + break; + } + + /* + * Unlock surface + */ + SDL_UnlockSurface(dst); + + result = 0; + + } else { + + result = filledRectAlpha(dst, x1, y1, x1 + w, y1 + h, color); + + } + + return (result); +} + +int boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (boxColor(dst, x1, y1, x2, y2, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- Line */ + +/* Non-alpha line drawing code adapted from routine */ +/* by Pete Shinners, pete@shinners.org */ +/* Originally from pygame, http://pygame.seul.org */ + +#define ABS(a) (((a)<0) ? -(a) : (a)) + +int lineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color) +{ + int pixx, pixy; + int x, y; + int dx, dy; + int ax, ay; + int sx, sy; + int swaptmp; + Uint8 *pixel; + Uint8 *colorptr; + + /* + * Clip line and test if we have to draw + */ + if (!(clipLine(dst, &x1, &y1, &x2, &y2))) { + return (0); + } + + /* + * Test for special cases of straight lines or single point + */ + if (x1 == x2) { + if (y1 < y2) { + return (vlineColor(dst, x1, y1, y2, color)); + } else if (y1 > y2) { + return (vlineColor(dst, x1, y2, y1, color)); + } else { + return (pixelColor(dst, x1, y1, color)); + } + } + if (y1 == y2) { + if (x1 < x2) { + return (hlineColor(dst, x1, x2, y1, color)); + } else if (x1 > x2) { + return (hlineColor(dst, x2, x1, y1, color)); + } + } + + /* + * Variable setup + */ + dx = x2 - x1; + dy = y2 - y1; + sx = (dx >= 0) ? 1 : -1; + sy = (dy >= 0) ? 1 : -1; + + /* Lock surface */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Check for alpha blending + */ + if ((color & 255) == 255) { + + /* + * No alpha blending - use fast pixel routines + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + /* + * More variable setup + */ + dx = sx * dx + 1; + dy = sy * dy + 1; + pixx = dst->format->BytesPerPixel; + pixy = dst->pitch; + pixel = ((Uint8 *) dst->pixels) + pixx * (int) x1 + pixy * (int) y1; + pixx *= sx; + pixy *= sy; + if (dx < dy) { + swaptmp = dx; + dx = dy; + dy = swaptmp; + swaptmp = pixx; + pixx = pixy; + pixy = swaptmp; + } + + /* + * Draw + */ + x = 0; + y = 0; + switch (dst->format->BytesPerPixel) { + case 1: + for (; x < dx; x++, pixel += pixx) { + *pixel = color; + y += dy; + if (y >= dx) { + y -= dx; + pixel += pixy; + } + } + break; + case 2: + for (; x < dx; x++, pixel += pixx) { + *(Uint16 *) pixel = color; + y += dy; + if (y >= dx) { + y -= dx; + pixel += pixy; + } + } + break; + case 3: + for (; x < dx; x++, pixel += pixx) { + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + pixel[0] = (color >> 16) & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = color & 0xff; + } else { + pixel[0] = color & 0xff; + pixel[1] = (color >> 8) & 0xff; + pixel[2] = (color >> 16) & 0xff; + } + y += dy; + if (y >= dx) { + y -= dx; + pixel += pixy; + } + } + break; + default: /* case 4 */ + for (; x < dx; x++, pixel += pixx) { + *(Uint32 *) pixel = color; + y += dy; + if (y >= dx) { + y -= dx; + pixel += pixy; + } + } + break; + } + + } else { + + /* + * Alpha blending required - use single-pixel blits + */ + + ax = ABS(dx) << 1; + ay = ABS(dy) << 1; + x = x1; + y = y1; + if (ax > ay) { + int d = ay - (ax >> 1); + + while (x != x2) { + pixelColorNolock (dst, x, y, color); + if (d > 0 || (d == 0 && sx == 1)) { + y += sy; + d -= ax; + } + x += sx; + d += ay; + } + } else { + int d = ax - (ay >> 1); + + while (y != y2) { + pixelColorNolock (dst, x, y, color); + if (d > 0 || ((d == 0) && (sy == 1))) { + x += sx; + d -= ay; + } + y += sy; + d += ax; + } + } + pixelColorNolock (dst, x, y, color); + + } + + /* Unlock surface */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (0); +} + +int lineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (lineColor(dst, x1, y1, x2, y2, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* AA Line */ + +#define AAlevels 256 +#define AAbits 8 + +/* + +This implementation of the Wu antialiasing code is based on Mike Abrash's +DDJ article which was reprinted as Chapter 42 of his Graphics Programming +Black Book, but has been optimized to work with SDL and utilizes 32-bit +fixed-point arithmetic. (A. Schiffler). + +*/ + +int aalineColorInt(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, int draw_endpoint) +{ + Sint32 xx0, yy0, xx1, yy1; + int result; + Uint32 intshift, erracc, erradj; + Uint32 erracctmp, wgt, wgtcompmask; + int dx, dy, tmp, xdir, y0p1, x0pxdir; + + /* + * Clip line and test if we have to draw + */ + if (!(clipLine(dst, &x1, &y1, &x2, &y2))) { + return (0); + } + + /* + * Keep on working with 32bit numbers + */ + xx0 = x1; + yy0 = y1; + xx1 = x2; + yy1 = y2; + + /* + * Reorder points if required + */ + if (yy0 > yy1) { + tmp = yy0; + yy0 = yy1; + yy1 = tmp; + tmp = xx0; + xx0 = xx1; + xx1 = tmp; + } + + /* + * Calculate distance + */ + dx = xx1 - xx0; + dy = yy1 - yy0; + + /* + * Adjust for negative dx and set xdir + */ + if (dx >= 0) { + xdir = 1; + } else { + xdir = -1; + dx = (-dx); + } + + /* + * Check for special cases + */ + if (dx == 0) { + /* + * Vertical line + */ + return (vlineColor(dst, x1, y1, y2, color)); + } else if (dy == 0) { + /* + * Horizontal line + */ + return (hlineColor(dst, x1, x2, y1, color)); + } else if (dx == dy) { + /* + * Diagonal line + */ + return (lineColor(dst, x1, y1, x2, y2, color)); + } + + /* + * Line is not horizontal, vertical or diagonal + */ + result = 0; + + /* + * Zero accumulator + */ + erracc = 0; + + /* + * # of bits by which to shift erracc to get intensity level + */ + intshift = 32 - AAbits; + /* + * Mask used to flip all bits in an intensity weighting + */ + wgtcompmask = AAlevels - 1; + + /* Lock surface */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Draw the initial pixel in the foreground color + */ + result |= pixelColorNolock(dst, x1, y1, color); + + /* + * x-major or y-major? + */ + if (dy > dx) { + + /* + * y-major. Calculate 16-bit fixed point fractional part of a pixel that + * X advances every time Y advances 1 pixel, truncating the result so that + * we won't overrun the endpoint along the X axis + */ + /* + * Not-so-portable version: erradj = ((Uint64)dx << 32) / (Uint64)dy; + */ + erradj = ((dx << 16) / dy) << 16; + + /* + * draw all pixels other than the first and last + */ + x0pxdir = xx0 + xdir; + while (--dy) { + erracctmp = erracc; + erracc += erradj; + if (erracc <= erracctmp) { + /* + * rollover in error accumulator, x coord advances + */ + xx0 = x0pxdir; + x0pxdir += xdir; + } + yy0++; /* y-major so always advance Y */ + + /* + * the AAbits most significant bits of erracc give us the intensity + * weighting for this pixel, and the complement of the weighting for + * the paired pixel. + */ + wgt = (erracc >> intshift) & 255; + result |= pixelColorWeightNolock (dst, xx0, yy0, color, 255 - wgt); + result |= pixelColorWeightNolock (dst, x0pxdir, yy0, color, wgt); + } + + } else { + + /* + * x-major line. Calculate 16-bit fixed-point fractional part of a pixel + * that Y advances each time X advances 1 pixel, truncating the result so + * that we won't overrun the endpoint along the X axis. + */ + /* + * Not-so-portable version: erradj = ((Uint64)dy << 32) / (Uint64)dx; + */ + erradj = ((dy << 16) / dx) << 16; + + /* + * draw all pixels other than the first and last + */ + y0p1 = yy0 + 1; + while (--dx) { + + erracctmp = erracc; + erracc += erradj; + if (erracc <= erracctmp) { + /* + * Accumulator turned over, advance y + */ + yy0 = y0p1; + y0p1++; + } + xx0 += xdir; /* x-major so always advance X */ + /* + * the AAbits most significant bits of erracc give us the intensity + * weighting for this pixel, and the complement of the weighting for + * the paired pixel. + */ + wgt = (erracc >> intshift) & 255; + result |= pixelColorWeightNolock (dst, xx0, yy0, color, 255 - wgt); + result |= pixelColorWeightNolock (dst, xx0, y0p1, color, wgt); + } + } + + /* + * Do we have to draw the endpoint + */ + if (draw_endpoint) { + /* + * Draw final pixel, always exactly intersected by the line and doesn't + * need to be weighted. + */ + result |= pixelColorNolock (dst, x2, y2, color); + } + + /* Unlock surface */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +int aalineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color) +{ + return (aalineColorInt(dst, x1, y1, x2, y2, color, 1)); +} + +int aalineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + return (aalineColorInt + (dst, x1, y1, x2, y2, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a, 1)); +} + + +/* ----- Circle */ + +/* Note: Based on algorithm from sge library, modified by A. Schiffler */ +/* with multiple pixel-draw removal and other minor speedup changes. */ + +int circleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color) +{ + Sint16 left, right, top, bottom; + int result; + Sint16 x1, y1, x2, y2; + Sint16 cx = 0; + Sint16 cy = r; + Sint16 ocx = (Sint16) 0xffff; + Sint16 ocy = (Sint16) 0xffff; + Sint16 df = 1 - r; + Sint16 d_e = 3; + Sint16 d_se = -2 * r + 5; + Sint16 xpcx, xmcx, xpcy, xmcy; + Sint16 ypcy, ymcy, ypcx, ymcx; + Uint8 *colorptr; + + /* + * Sanity check radius + */ + if (r < 0) { + return (-1); + } + + /* + * Special case for r=0 - draw a point + */ + if (r == 0) { + return (pixelColor(dst, x, y, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of circle is visible + */ + x1 = x - r; + x2 = x + r; + y1 = y - r; + y2 = y + r; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Draw circle + */ + result = 0; + + /* Lock surface */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Alpha Check + */ + if ((color & 255) == 255) { + + /* + * No Alpha - direct memory writes + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + /* + * Draw + */ + do { + if ((ocy != cy) || (ocx != cx)) { + xpcx = x + cx; + xmcx = x - cx; + if (cy > 0) { + ypcy = y + cy; + ymcy = y - cy; + result |= fastPixelColorNolock(dst, xmcx, ypcy, color); + result |= fastPixelColorNolock(dst, xpcx, ypcy, color); + result |= fastPixelColorNolock(dst, xmcx, ymcy, color); + result |= fastPixelColorNolock(dst, xpcx, ymcy, color); + } else { + result |= fastPixelColorNolock(dst, xmcx, y, color); + result |= fastPixelColorNolock(dst, xpcx, y, color); + } + ocy = cy; + xpcy = x + cy; + xmcy = x - cy; + if (cx > 0) { + ypcx = y + cx; + ymcx = y - cx; + result |= fastPixelColorNolock(dst, xmcy, ypcx, color); + result |= fastPixelColorNolock(dst, xpcy, ypcx, color); + result |= fastPixelColorNolock(dst, xmcy, ymcx, color); + result |= fastPixelColorNolock(dst, xpcy, ymcx, color); + } else { + result |= fastPixelColorNolock(dst, xmcy, y, color); + result |= fastPixelColorNolock(dst, xpcy, y, color); + } + ocx = cx; + } + /* + * Update + */ + if (df < 0) { + df += d_e; + d_e += 2; + d_se += 2; + } else { + df += d_se; + d_e += 2; + d_se += 4; + cy--; + } + cx++; + } while (cx <= cy); + + /* + * Unlock surface + */ + SDL_UnlockSurface(dst); + + } else { + + /* + * Using Alpha - blended pixel blits + */ + + do { + /* + * Draw + */ + if ((ocy != cy) || (ocx != cx)) { + xpcx = x + cx; + xmcx = x - cx; + if (cy > 0) { + ypcy = y + cy; + ymcy = y - cy; + result |= pixelColorNolock (dst, xmcx, ypcy, color); + result |= pixelColorNolock (dst, xpcx, ypcy, color); + result |= pixelColorNolock (dst, xmcx, ymcy, color); + result |= pixelColorNolock (dst, xpcx, ymcy, color); + } else { + result |= pixelColorNolock (dst, xmcx, y, color); + result |= pixelColorNolock (dst, xpcx, y, color); + } + ocy = cy; + xpcy = x + cy; + xmcy = x - cy; + if (cx > 0) { + ypcx = y + cx; + ymcx = y - cx; + result |= pixelColorNolock (dst, xmcy, ypcx, color); + result |= pixelColorNolock (dst, xpcy, ypcx, color); + result |= pixelColorNolock (dst, xmcy, ymcx, color); + result |= pixelColorNolock (dst, xpcy, ymcx, color); + } else { + result |= pixelColorNolock (dst, xmcy, y, color); + result |= pixelColorNolock (dst, xpcy, y, color); + } + ocx = cx; + } + /* + * Update + */ + if (df < 0) { + df += d_e; + d_e += 2; + d_se += 2; + } else { + df += d_se; + d_e += 2; + d_se += 4; + cy--; + } + cx++; + } while (cx <= cy); + + } /* Alpha check */ + + /* Unlock surface */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +int circleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (circleColor(dst, x, y, rad, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- AA Circle */ + +/* AA circle is based on AAellipse */ + +int aacircleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color) +{ + return (aaellipseColor(dst, x, y, r, r, color)); +} + +int aacircleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (aaellipseColor + (dst, x, y, rad, rad, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- Filled Circle */ + +/* Note: Based on algorithm from sge library with multiple-hline draw removal */ + +/* and other speedup changes. */ + +int filledCircleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color) +{ + Sint16 left, right, top, bottom; + int result; + Sint16 x1, y1, x2, y2; + Sint16 cx = 0; + Sint16 cy = r; + Sint16 ocx = (Sint16) 0xffff; + Sint16 ocy = (Sint16) 0xffff; + Sint16 df = 1 - r; + Sint16 d_e = 3; + Sint16 d_se = -2 * r + 5; + Sint16 xpcx, xmcx, xpcy, xmcy; + Sint16 ypcy, ymcy, ypcx, ymcx; + + /* + * Sanity check radius + */ + if (r < 0) { + return (-1); + } + + /* + * Special case for r=0 - draw a point + */ + if (r == 0) { + return (pixelColor(dst, x, y, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of circle is visible + */ + x1 = x - r; + x2 = x + r; + y1 = y - r; + y2 = y + r; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Draw + */ + result = 0; + do { + xpcx = x + cx; + xmcx = x - cx; + xpcy = x + cy; + xmcy = x - cy; + if (ocy != cy) { + if (cy > 0) { + ypcy = y + cy; + ymcy = y - cy; + result |= hlineColor(dst, xmcx, xpcx, ypcy, color); + result |= hlineColor(dst, xmcx, xpcx, ymcy, color); + } else { + result |= hlineColor(dst, xmcx, xpcx, y, color); + } + ocy = cy; + } + if (ocx != cx) { + if (cx != cy) { + if (cx > 0) { + ypcx = y + cx; + ymcx = y - cx; + result |= hlineColor(dst, xmcy, xpcy, ymcx, color); + result |= hlineColor(dst, xmcy, xpcy, ypcx, color); + } else { + result |= hlineColor(dst, xmcy, xpcy, y, color); + } + } + ocx = cx; + } + /* + * Update + */ + if (df < 0) { + df += d_e; + d_e += 2; + d_se += 2; + } else { + df += d_se; + d_e += 2; + d_se += 4; + cy--; + } + cx++; + } while (cx <= cy); + + return (result); +} + +int filledCircleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (filledCircleColor + (dst, x, y, rad, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + + +/* ----- Ellipse */ + +/* Note: Based on algorithm from sge library with multiple-hline draw removal */ +/* and other speedup changes. */ + +int ellipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color) +{ + Sint16 left, right, top, bottom; + int result; + Sint16 x1, y1, x2, y2; + int ix, iy; + int h, i, j, k; + int oh, oi, oj, ok; + int xmh, xph, ypk, ymk; + int xmi, xpi, ymj, ypj; + int xmj, xpj, ymi, ypi; + int xmk, xpk, ymh, yph; + Uint8 *colorptr; + + /* + * Sanity check radii + */ + if ((rx < 0) || (ry < 0)) { + return (-1); + } + + /* + * Special case for rx=0 - draw a vline + */ + if (rx == 0) { + return (vlineColor(dst, x, y - ry, y + ry, color)); + } + /* + * Special case for ry=0 - draw a hline + */ + if (ry == 0) { + return (hlineColor(dst, x - rx, x + rx, y, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of ellipse is visible + */ + x1 = x - rx; + x2 = x + rx; + y1 = y - ry; + y2 = y + ry; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Init vars + */ + oh = oi = oj = ok = 0xFFFF; + + /* + * Draw + */ + result = 0; + + /* Lock surface */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* + * Check alpha + */ + if ((color & 255) == 255) { + + /* + * No Alpha - direct memory writes + */ + + /* + * Setup color + */ + colorptr = (Uint8 *) & color; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { + color = SDL_MapRGBA(dst->format, colorptr[0], colorptr[1], colorptr[2], colorptr[3]); + } else { + color = SDL_MapRGBA(dst->format, colorptr[3], colorptr[2], colorptr[1], colorptr[0]); + } + + + if (rx > ry) { + ix = 0; + iy = rx * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * ry) / rx; + k = (i * ry) / rx; + + if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j)) { + xph = x + h; + xmh = x - h; + if (k > 0) { + ypk = y + k; + ymk = y - k; + result |= fastPixelColorNolock(dst, xmh, ypk, color); + result |= fastPixelColorNolock(dst, xph, ypk, color); + result |= fastPixelColorNolock(dst, xmh, ymk, color); + result |= fastPixelColorNolock(dst, xph, ymk, color); + } else { + result |= fastPixelColorNolock(dst, xmh, y, color); + result |= fastPixelColorNolock(dst, xph, y, color); + } + ok = k; + xpi = x + i; + xmi = x - i; + if (j > 0) { + ypj = y + j; + ymj = y - j; + result |= fastPixelColorNolock(dst, xmi, ypj, color); + result |= fastPixelColorNolock(dst, xpi, ypj, color); + result |= fastPixelColorNolock(dst, xmi, ymj, color); + result |= fastPixelColorNolock(dst, xpi, ymj, color); + } else { + result |= fastPixelColorNolock(dst, xmi, y, color); + result |= fastPixelColorNolock(dst, xpi, y, color); + } + oj = j; + } + + ix = ix + iy / rx; + iy = iy - ix / rx; + + } while (i > h); + } else { + ix = 0; + iy = ry * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * rx) / ry; + k = (i * rx) / ry; + + if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h))) { + xmj = x - j; + xpj = x + j; + if (i > 0) { + ypi = y + i; + ymi = y - i; + result |= fastPixelColorNolock(dst, xmj, ypi, color); + result |= fastPixelColorNolock(dst, xpj, ypi, color); + result |= fastPixelColorNolock(dst, xmj, ymi, color); + result |= fastPixelColorNolock(dst, xpj, ymi, color); + } else { + result |= fastPixelColorNolock(dst, xmj, y, color); + result |= fastPixelColorNolock(dst, xpj, y, color); + } + oi = i; + xmk = x - k; + xpk = x + k; + if (h > 0) { + yph = y + h; + ymh = y - h; + result |= fastPixelColorNolock(dst, xmk, yph, color); + result |= fastPixelColorNolock(dst, xpk, yph, color); + result |= fastPixelColorNolock(dst, xmk, ymh, color); + result |= fastPixelColorNolock(dst, xpk, ymh, color); + } else { + result |= fastPixelColorNolock(dst, xmk, y, color); + result |= fastPixelColorNolock(dst, xpk, y, color); + } + oh = h; + } + + ix = ix + iy / ry; + iy = iy - ix / ry; + + } while (i > h); + } + + } else { + + if (rx > ry) { + ix = 0; + iy = rx * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * ry) / rx; + k = (i * ry) / rx; + + if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j)) { + xph = x + h; + xmh = x - h; + if (k > 0) { + ypk = y + k; + ymk = y - k; + result |= pixelColorNolock (dst, xmh, ypk, color); + result |= pixelColorNolock (dst, xph, ypk, color); + result |= pixelColorNolock (dst, xmh, ymk, color); + result |= pixelColorNolock (dst, xph, ymk, color); + } else { + result |= pixelColorNolock (dst, xmh, y, color); + result |= pixelColorNolock (dst, xph, y, color); + } + ok = k; + xpi = x + i; + xmi = x - i; + if (j > 0) { + ypj = y + j; + ymj = y - j; + result |= pixelColorNolock (dst, xmi, ypj, color); + result |= pixelColorNolock (dst, xpi, ypj, color); + result |= pixelColorNolock (dst, xmi, ymj, color); + result |= pixelColor(dst, xpi, ymj, color); + } else { + result |= pixelColorNolock (dst, xmi, y, color); + result |= pixelColorNolock (dst, xpi, y, color); + } + oj = j; + } + + ix = ix + iy / rx; + iy = iy - ix / rx; + + } while (i > h); + } else { + ix = 0; + iy = ry * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * rx) / ry; + k = (i * rx) / ry; + + if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h))) { + xmj = x - j; + xpj = x + j; + if (i > 0) { + ypi = y + i; + ymi = y - i; + result |= pixelColorNolock (dst, xmj, ypi, color); + result |= pixelColorNolock (dst, xpj, ypi, color); + result |= pixelColorNolock (dst, xmj, ymi, color); + result |= pixelColorNolock (dst, xpj, ymi, color); + } else { + result |= pixelColorNolock (dst, xmj, y, color); + result |= pixelColorNolock (dst, xpj, y, color); + } + oi = i; + xmk = x - k; + xpk = x + k; + if (h > 0) { + yph = y + h; + ymh = y - h; + result |= pixelColorNolock (dst, xmk, yph, color); + result |= pixelColorNolock (dst, xpk, yph, color); + result |= pixelColorNolock (dst, xmk, ymh, color); + result |= pixelColorNolock (dst, xpk, ymh, color); + } else { + result |= pixelColorNolock (dst, xmk, y, color); + result |= pixelColorNolock (dst, xpk, y, color); + } + oh = h; + } + + ix = ix + iy / ry; + iy = iy - ix / ry; + + } while (i > h); + } + + } /* Alpha check */ + + /* Unlock surface */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +int ellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (ellipseColor(dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- AA Ellipse */ + +/* Based on code from Anders Lindstroem, based on code from SGE, based on code from TwinLib */ + +int aaellipseColor(SDL_Surface * dst, Sint16 xc, Sint16 yc, Sint16 rx, Sint16 ry, Uint32 color) +{ + Sint16 left, right, top, bottom; + Sint16 x1,y1,x2,y2; + int i; + int a2, b2, ds, dt, dxt, t, s, d; + Sint16 x, y, xs, ys, dyt, xx, yy, xc2, yc2; + float cp; + Uint8 weight, iweight; + int result; + + /* + * Sanity check radii + */ + if ((rx < 0) || (ry < 0)) { + return (-1); + } + + /* + * Special case for rx=0 - draw a vline + */ + if (rx == 0) { + return (vlineColor(dst, xc, yc - ry, yc + ry, color)); + } + /* + * Special case for ry=0 - draw a hline + */ + if (ry == 0) { + return (hlineColor(dst, xc - rx, xc + rx, yc, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of ellipse is visible + */ + x1 = xc - rx; + x2 = xc + rx; + y1 = yc - ry; + y2 = yc + ry; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* Variable setup */ + a2 = rx * rx; + b2 = ry * ry; + + ds = 2 * a2; + dt = 2 * b2; + + xc2 = 2 * xc; + yc2 = 2 * yc; + + dxt = (int) (a2 / sqrt(a2 + b2)); + + t = 0; + s = -2 * a2 * ry; + d = 0; + + x = xc; + y = yc - ry; + + /* Draw */ + result = 0; + + /* Lock surface */ + if (SDL_MUSTLOCK(dst)) { + if (SDL_LockSurface(dst) < 0) { + return (-1); + } + } + + /* "End points" */ + result |= pixelColorNolock(dst, x, y, color); + result |= pixelColorNolock(dst, xc2 - x, y, color); + result |= pixelColorNolock(dst, x, yc2 - y, color); + result |= pixelColorNolock(dst, xc2 - x, yc2 - y, color); + + for (i = 1; i <= dxt; i++) { + x--; + d += t - b2; + + if (d >= 0) + ys = y - 1; + else if ((d - s - a2) > 0) { + if ((2 * d - s - a2) >= 0) + ys = y + 1; + else { + ys = y; + y++; + d -= s + a2; + s += ds; + } + } else { + y++; + ys = y + 1; + d -= s + a2; + s += ds; + } + + t -= dt; + + /* Calculate alpha */ + if (s != 0.0) { + cp = (float) abs(d) / (float) abs(s); + if (cp > 1.0) { + cp = 1.0; + } + } else { + cp = 1.0; + } + + /* Calculate weights */ + weight = (Uint8) (cp * 255); + iweight = 255 - weight; + + /* Upper half */ + xx = xc2 - x; + result |= pixelColorWeightNolock(dst, x, y, color, iweight); + result |= pixelColorWeightNolock(dst, xx, y, color, iweight); + + result |= pixelColorWeightNolock(dst, x, ys, color, weight); + result |= pixelColorWeightNolock(dst, xx, ys, color, weight); + + /* Lower half */ + yy = yc2 - y; + result |= pixelColorWeightNolock(dst, x, yy, color, iweight); + result |= pixelColorWeightNolock(dst, xx, yy, color, iweight); + + yy = yc2 - ys; + result |= pixelColorWeightNolock(dst, x, yy, color, weight); + result |= pixelColorWeightNolock(dst, xx, yy, color, weight); + } + + dyt = abs(y - yc); + + for (i = 1; i <= dyt; i++) { + y++; + d -= s + a2; + + if (d <= 0) + xs = x + 1; + else if ((d + t - b2) < 0) { + if ((2 * d + t - b2) <= 0) + xs = x - 1; + else { + xs = x; + x--; + d += t - b2; + t -= dt; + } + } else { + x--; + xs = x - 1; + d += t - b2; + t -= dt; + } + + s += ds; + + /* Calculate alpha */ + if (t != 0.0) { + cp = (float) abs(d) / (float) abs(t); + if (cp > 1.0) { + cp = 1.0; + } + } else { + cp = 1.0; + } + + /* Calculate weight */ + weight = (Uint8) (cp * 255); + iweight = 255 - weight; + + /* Left half */ + xx = xc2 - x; + yy = yc2 - y; + result |= pixelColorWeightNolock(dst, x, y, color, iweight); + result |= pixelColorWeightNolock(dst, xx, y, color, iweight); + + result |= pixelColorWeightNolock(dst, x, yy, color, iweight); + result |= pixelColorWeightNolock(dst, xx, yy, color, iweight); + + /* Right half */ + xx = 2 * xc - xs; + result |= pixelColorWeightNolock(dst, xs, y, color, weight); + result |= pixelColorWeightNolock(dst, xx, y, color, weight); + + result |= pixelColorWeightNolock(dst, xs, yy, color, weight); + result |= pixelColorWeightNolock(dst, xx, yy, color, weight); + + + } + + /* Unlock surface */ + if (SDL_MUSTLOCK(dst)) { + SDL_UnlockSurface(dst); + } + + return (result); +} + +int aaellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (aaellipseColor + (dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ---- Filled Ellipse */ + +/* Note: */ +/* Based on algorithm from sge library with multiple-hline draw removal */ +/* and other speedup changes. */ + +int filledEllipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color) +{ + Sint16 left, right, top, bottom; + int result; + Sint16 x1, y1, x2, y2; + int ix, iy; + int h, i, j, k; + int oh, oi, oj, ok; + int xmh, xph; + int xmi, xpi; + int xmj, xpj; + int xmk, xpk; + + /* + * Sanity check radii + */ + if ((rx < 0) || (ry < 0)) { + return (-1); + } + + /* + * Special case for rx=0 - draw a vline + */ + if (rx == 0) { + return (vlineColor(dst, x, y - ry, y + ry, color)); + } + /* + * Special case for ry=0 - draw a hline + */ + if (ry == 0) { + return (hlineColor(dst, x - rx, x + rx, y, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of ellipse is visible + */ + x1 = x - rx; + x2 = x + rx; + y1 = y - ry; + y2 = y + ry; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Init vars + */ + oh = oi = oj = ok = 0xFFFF; + + /* + * Draw + */ + result = 0; + if (rx > ry) { + ix = 0; + iy = rx * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * ry) / rx; + k = (i * ry) / rx; + + if ((ok != k) && (oj != k)) { + xph = x + h; + xmh = x - h; + if (k > 0) { + result |= hlineColor(dst, xmh, xph, y + k, color); + result |= hlineColor(dst, xmh, xph, y - k, color); + } else { + result |= hlineColor(dst, xmh, xph, y, color); + } + ok = k; + } + if ((oj != j) && (ok != j) && (k != j)) { + xmi = x - i; + xpi = x + i; + if (j > 0) { + result |= hlineColor(dst, xmi, xpi, y + j, color); + result |= hlineColor(dst, xmi, xpi, y - j, color); + } else { + result |= hlineColor(dst, xmi, xpi, y, color); + } + oj = j; + } + + ix = ix + iy / rx; + iy = iy - ix / rx; + + } while (i > h); + } else { + ix = 0; + iy = ry * 64; + + do { + h = (ix + 32) >> 6; + i = (iy + 32) >> 6; + j = (h * rx) / ry; + k = (i * rx) / ry; + + if ((oi != i) && (oh != i)) { + xmj = x - j; + xpj = x + j; + if (i > 0) { + result |= hlineColor(dst, xmj, xpj, y + i, color); + result |= hlineColor(dst, xmj, xpj, y - i, color); + } else { + result |= hlineColor(dst, xmj, xpj, y, color); + } + oi = i; + } + if ((oh != h) && (oi != h) && (i != h)) { + xmk = x - k; + xpk = x + k; + if (h > 0) { + result |= hlineColor(dst, xmk, xpk, y + h, color); + result |= hlineColor(dst, xmk, xpk, y - h, color); + } else { + result |= hlineColor(dst, xmk, xpk, y, color); + } + oh = h; + } + + ix = ix + iy / ry; + iy = iy - ix / ry; + + } while (i > h); + } + + return (result); +} + + +int filledEllipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (filledEllipseColor + (dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ----- filled pie */ + +/* Low-speed float pie-calc implementation by drawing polygons. */ + +int filledpieColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color) +{ + Sint16 left, right, top, bottom; + Sint16 x1, y1, x2, y2; + int result; + double angle, start_angle, end_angle; + double deltaAngle; + double dr; + int posX, posY; + int numpoints, i; + Sint16 *vx, *vy; + + /* + * Sanity check radii + */ + if (rad < 0) { + return (-1); + } + + /* + * Fixup angles + */ + start = start % 360; + end = end % 360; + + /* + * Special case for rad=0 - draw a point + */ + if (rad == 0) { + return (pixelColor(dst, x, y, color)); + } + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of pie's circle is visible + */ + x1 = x - rad; + x2 = x + rad; + y1 = y - rad; + y2 = y + rad; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Variable setup + */ + dr = (double) rad; + deltaAngle = 3.0 / dr; + start_angle = (double) start *(2.0 * M_PI / 360.0); + end_angle = (double) end *(2.0 * M_PI / 360.0); + if (start > end) { + end_angle += (2.0 * M_PI); + } + + /* Count points (rather than calculate it) */ + numpoints = 1; + angle = start_angle; + while (angle <= end_angle) { + angle += deltaAngle; + numpoints++; + } + + /* Check size of array */ + if (numpoints == 1) { + return (pixelColor(dst, x, y, color)); + } else if (numpoints == 2) { + posX = x + (int) (dr * cos(start_angle)); + posY = y + (int) (dr * sin(start_angle)); + return (lineColor(dst, x, y, posX, posY, color)); + } + + /* Allocate vertex array */ + vx = vy = (Uint16 *) malloc(2 * sizeof(Uint16) * numpoints); + if (vx == NULL) { + return (-1); + } + vy += numpoints; + + /* Center */ + vx[0] = x; + vy[0] = y; + + /* Calculate and store vertices */ + i = 1; + angle = start_angle; + while (angle <= end_angle) { + vx[i] = x + (int) (dr * cos(angle)); + vy[i] = y + (int) (dr * sin(angle)); + angle += deltaAngle; + i++; + } + + /* Draw */ + result = filledPolygonColor(dst, vx, vy, numpoints, color); + + /* Free vertex array */ + free(vx); + + return (result); +} + +int filledpieRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, + Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + return (filledpieColor(dst, x, y, rad, start, end, + ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); + +} + + +/* Trigon */ + +int trigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(polygonColor(dst,vx,vy,3,color)); +} + +int trigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(polygonRGBA(dst,vx,vy,3,r,g,b,a)); +} + +/* AA-Trigon */ + +int aatrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(aapolygonColor(dst,vx,vy,3,color)); +} + +int aatrigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(aapolygonRGBA(dst,vx,vy,3,r,g,b,a)); +} + +/* Filled Trigon */ + +int filledTrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, int color) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(filledPolygonColor(dst,vx,vy,3,color)); +} + +int filledTrigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + Sint16 vx[3]; + Sint16 vy[3]; + + vx[0]=x1; + vx[1]=x2; + vx[2]=x3; + vy[0]=y1; + vy[1]=y2; + vy[2]=y3; + + return(filledPolygonRGBA(dst,vx,vy,3,r,g,b,a)); +} + +/* ---- Polygon */ + +int polygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color) +{ + int result; + int i; + Sint16 *x1, *y1, *x2, *y2; + + /* + * Sanity check + */ + if (n < 3) { + return (-1); + } + + /* + * Pointer setup + */ + x1 = x2 = vx; + y1 = y2 = vy; + x2++; + y2++; + + /* + * Draw + */ + result = 0; + for (i = 1; i < n; i++) { + result |= lineColor(dst, *x1, *y1, *x2, *y2, color); + x1 = x2; + y1 = y2; + x2++; + y2++; + } + result |= lineColor(dst, *x1, *y1, *vx, *vy, color); + + return (result); +} + +int polygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (polygonColor(dst, vx, vy, n, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ---- AA-Polygon */ + +int aapolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color) +{ + int result; + int i; + Sint16 *x1, *y1, *x2, *y2; + + /* + * Sanity check + */ + if (n < 3) { + return (-1); + } + + /* + * Pointer setup + */ + x1 = x2 = vx; + y1 = y2 = vy; + x2++; + y2++; + + /* + * Draw + */ + result = 0; + for (i = 1; i < n; i++) { + result |= aalineColorInt(dst, *x1, *y1, *x2, *y2, color, 0); + x1 = x2; + y1 = y2; + x2++; + y2++; + } + result |= aalineColorInt(dst, *x1, *y1, *vx, *vy, color, 0); + + return (result); +} + +int aapolygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (aapolygonColor(dst, vx, vy, n, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ---- Filled Polygon */ + +int gfxPrimitivesCompareInt(const void *a, const void *b); + +static int *gfxPrimitivesPolyInts = NULL; +static int gfxPrimitivesPolyAllocated = 0; + +int filledPolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int color) +{ + int result; + int i; + int /*x,*/ y, xa, xb; + int miny, maxy; + int x1, y1; + int x2, y2; + int ind1, ind2; + int ints; + + /* + * Sanity check + */ + if (n < 3) { + return -1; + } + + /* + * Allocate temp array, only grow array + */ + if (!gfxPrimitivesPolyAllocated) { + gfxPrimitivesPolyInts = (int *) malloc(sizeof(int) * n); + gfxPrimitivesPolyAllocated = n; + } else { + if (gfxPrimitivesPolyAllocated < n) { + gfxPrimitivesPolyInts = (int *) realloc(gfxPrimitivesPolyInts, sizeof(int) * n); + gfxPrimitivesPolyAllocated = n; + } + } + + /* + * Determine Y maxima + */ + miny = vy[0]; + maxy = vy[0]; + for (i = 1; (i < n); i++) { + if (vy[i] < miny) { + miny = vy[i]; + } else if (vy[i] > maxy) { + maxy = vy[i]; + } + } + + /* + * Draw, scanning y + */ + result = 0; + for (y = miny; (y <= maxy); y++) { + ints = 0; + for (i = 0; (i < n); i++) { + if (!i) { + ind1 = n - 1; + ind2 = 0; + } else { + ind1 = i - 1; + ind2 = i; + } + y1 = vy[ind1]; + y2 = vy[ind2]; + if (y1 < y2) { + x1 = vx[ind1]; + x2 = vx[ind2]; + } else if (y1 > y2) { + y2 = vy[ind1]; + y1 = vy[ind2]; + x2 = vx[ind1]; + x1 = vx[ind2]; + } else { + continue; + } + if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) ) { + gfxPrimitivesPolyInts[ints++] = (65536 * (y - y1) * (x2 - x1)) / (y2 - y1) + (65536 * x1); + } + + } + + qsort(gfxPrimitivesPolyInts, ints, sizeof(int), gfxPrimitivesCompareInt); + + for (i = 0; (i < ints); i += 2) { + xa = gfxPrimitivesPolyInts[i] + 1; + xa = (xa >> 16) + ((xa & 32768) >> 15); + xb = gfxPrimitivesPolyInts[i+1] - 1; + xb = (xb >> 16) + ((xb & 32768) >> 15); + result |= hlineColor(dst, xa, xb, y, color); + } + } + + return (result); +} + +int filledPolygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (filledPolygonColor + (dst, vx, vy, n, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +int gfxPrimitivesCompareInt(const void *a, const void *b) +{ + return (*(const int *) a) - (*(const int *) b); +} + +/* ---- Character (8x8 internal font) */ + +static SDL_Surface *gfxPrimitivesFont[256]; +static Uint32 gfxPrimitivesFontColor[256]; + +int characterColor(SDL_Surface * dst, Sint16 x, Sint16 y, char c, Uint32 color) +{ + Sint16 left, right, top, bottom; + Sint16 x1, y1, x2, y2; + SDL_Rect srect; + SDL_Rect drect; + int result; + int ix, iy, k; + unsigned char *charpos; + unsigned char bits[8] = { 128, 64, 32, 16, 8, 4, 2, 1 }; + unsigned char *bitpos; + Uint8 *curpos; + int forced_redraw; + + /* + * Get clipping boundary + */ + left = dst->clip_rect.x; + right = dst->clip_rect.x + dst->clip_rect.w - 1; + top = dst->clip_rect.y; + bottom = dst->clip_rect.y + dst->clip_rect.h - 1; + + /* + * Test if bounding box of character is visible + */ + x1 = x; + x2 = x + 8; + y1 = y; + y2 = y + 8; + if ((x1right) && (x2>right)) { + return(0); + } + if ((y1bottom) && (y2>bottom)) { + return(0); + } + + /* + * Setup source rectangle for 8x8 bitmap + */ + srect.x = 0; + srect.y = 0; + srect.w = 8; + srect.h = 8; + + /* + * Setup destination rectangle for 8x8 bitmap + */ + drect.x = x; + drect.y = y; + drect.w = 8; + drect.h = 8; + + /* + * Create new 8x8 bitmap surface if not already present + */ + if (gfxPrimitivesFont[(unsigned char) c] == NULL) { + gfxPrimitivesFont[(unsigned char) c] = + SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_HWSURFACE | SDL_SRCALPHA, 8, 8, + 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); + /* + * Check pointer + */ + if (gfxPrimitivesFont[(unsigned char) c] == NULL) { + return (-1); + } + /* + * Definitely redraw + */ + forced_redraw = 1; + } else { + forced_redraw = 0; + } + + /* + * Check if color has changed + */ + if ((gfxPrimitivesFontColor[(unsigned char) c] != color) || (forced_redraw)) { + /* + * Redraw character + */ + SDL_SetAlpha(gfxPrimitivesFont[(unsigned char) c], SDL_SRCALPHA, 255); + gfxPrimitivesFontColor[(unsigned char) c] = color; + + /* + * Variable setup + */ + k = (unsigned char) c; + k *= 8; + charpos = gfxPrimitivesFontdata; + charpos += k; + + /* + * Clear bitmap + */ + curpos = (Uint8 *) gfxPrimitivesFont[(unsigned char) c]->pixels; + memset(curpos, 0, 8 * 8 * 4); + + /* + * Drawing loop + */ + for (iy = 0; iy < 8; iy++) { + bitpos = bits; + for (ix = 0; ix < 8; ix++) { + if ((*charpos & *bitpos) == *bitpos) { + memcpy(curpos, &color, 4); + } + bitpos++; + curpos += 4;; + } + charpos++; + } + } + + /* + * Draw bitmap onto destination surface + */ + result = SDL_BlitSurface(gfxPrimitivesFont[(unsigned char) c], &srect, dst, &drect); + + return (result); +} + +int characterRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, char c, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (characterColor(dst, x, y, c, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +int stringColor(SDL_Surface * dst, Sint16 x, Sint16 y, char *c, Uint32 color) +{ + int result; + int i, length; + char *curchar; + int curx; + + length = strlen(c); + curchar = c; + curx = x; + result = 0; + for (i = 0; i < length; i++) { + result |= characterColor(dst, curx, y, *curchar, color); + curchar++; + curx += 8; + } + + return (result); +} + +int stringRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, char *c, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + /* + * Draw + */ + return (stringColor(dst, x, y, c, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a)); +} + +/* ---- Bezier curve */ + +/* + Calculate bezier interpolator of data array with ndata values at position 't' +*/ + +double evaluateBezier (double *data, int ndata, double t) +{ + double mu, result; + int n,k,kn,nn,nkn; + double blend,muk,munk; + + /* Sanity check bounds */ + if (t<0.0) { + return(data[0]); + } + if (t>=(double)ndata) { + return(data[ndata-1]); + } + + /* Adjust t to the range 0.0 to 1.0 */ + mu=t/(double)ndata; + + /* Calculate interpolate */ + n=ndata-1; + result=0.0; + muk = 1; + munk = pow(1-mu,(double)n); + for (k=0;k<=n;k++) { + nn = n; + kn = k; + nkn = n - k; + blend = muk * munk; + muk *= mu; + munk /= (1-mu); + while (nn >= 1) { + blend *= nn; + nn--; + if (kn > 1) { + blend /= (double)kn; + kn--; + } + if (nkn > 1) { + blend /= (double)nkn; + nkn--; + } + } + result += data[k] * blend; + } + + return(result); +} + +int bezierColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int s, Uint32 color) +{ + int result; + int i; + double *x, *y, t, stepsize; + Sint16 x1, y1, x2, y2; + + /* + * Sanity check + */ + if (n < 3) { + return (-1); + } + if (s < 2) { + return (-1); + } + + /* + * Variable setup + */ + stepsize=(double)1.0/(double)s; + + /* Transfer vertices into float arrays */ + if ((x=(double *)malloc(sizeof(double)*(n+1)))==NULL) { + return(-1); + } + if ((y=(double *)malloc(sizeof(double)*(n+1)))==NULL) { + free(x); + return(-1); + } + for (i=0; i +#ifndef M_PI +#define M_PI 3.141592654 +#endif + +#include + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* ----- Versioning */ + +#define SDL_GFXPRIMITIVES_MAJOR 1 +#define SDL_GFXPRIMITIVES_MINOR 5 + +/* ----- W32 DLL interface */ + +#ifdef WIN32 +#ifdef BUILD_DLL +#define DLLINTERFACE __declspec(dllexport) +#else +#define DLLINTERFACE __declspec(dllimport) +#endif +#else +#define DLLINTERFACE +#endif + +/* ----- Prototypes */ + +/* Note: all ___Color routines expect the color to be in format 0xRRGGBBAA */ + +/* Pixel */ + + DLLINTERFACE int pixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color); + DLLINTERFACE int pixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Horizontal line */ + + DLLINTERFACE int hlineColor(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color); + DLLINTERFACE int hlineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Vertical line */ + + DLLINTERFACE int vlineColor(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color); + DLLINTERFACE int vlineRGBA(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Rectangle */ + + DLLINTERFACE int rectangleColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color); + DLLINTERFACE int rectangleRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, + Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Filled rectangle (Box) */ + + DLLINTERFACE int boxColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color); + DLLINTERFACE int boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, + Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Line */ + + DLLINTERFACE int lineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color); + DLLINTERFACE int lineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, + Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* AA Line */ + DLLINTERFACE int aalineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color); + DLLINTERFACE int aalineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, + Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Circle */ + + DLLINTERFACE int circleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color); + DLLINTERFACE int circleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* AA Circle */ + + DLLINTERFACE int aacircleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color); + DLLINTERFACE int aacircleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, + Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Filled Circle */ + + DLLINTERFACE int filledCircleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color); + DLLINTERFACE int filledCircleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, + Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Ellipse */ + + DLLINTERFACE int ellipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color); + DLLINTERFACE int ellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, + Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* AA Ellipse */ + + DLLINTERFACE int aaellipseColor(SDL_Surface * dst, Sint16 xc, Sint16 yc, Sint16 rx, Sint16 ry, Uint32 color); + DLLINTERFACE int aaellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, + Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Filled Ellipse */ + + DLLINTERFACE int filledEllipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color); + DLLINTERFACE int filledEllipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, + Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a); +/* Filled Pie */ + + DLLINTERFACE int filledpieColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, + Sint16 start, Sint16 end, Uint32 color); + DLLINTERFACE int filledpieRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, + Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Trigon */ + + DLLINTERFACE int trigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color); + DLLINTERFACE int trigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* AA-Trigon */ + + DLLINTERFACE int aatrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color); + DLLINTERFACE int aatrigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Filled Trigon */ + + DLLINTERFACE int filledTrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, int color); + DLLINTERFACE int filledTrigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, + Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Polygon */ + + DLLINTERFACE int polygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color); + DLLINTERFACE int polygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, + int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* AA-Polygon */ + + DLLINTERFACE int aapolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color); + DLLINTERFACE int aapolygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, + int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Filled Polygon */ + + DLLINTERFACE int filledPolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int color); + DLLINTERFACE int filledPolygonRGBA(SDL_Surface * dst, Sint16 * vx, + Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Bezier */ +/* s = number of steps */ + + DLLINTERFACE int bezierColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int s, Uint32 color); + DLLINTERFACE int bezierRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, + int n, int s, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + + +/* 8x8 Characters/Strings */ + + DLLINTERFACE int characterColor(SDL_Surface * dst, Sint16 x, Sint16 y, char c, Uint32 color); + DLLINTERFACE int characterRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, char c, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + DLLINTERFACE int stringColor(SDL_Surface * dst, Sint16 x, Sint16 y, char *c, Uint32 color); + DLLINTERFACE int stringRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, char *c, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +}; +#endif + +#endif /* _SDL_gfxPrimitives_h */ diff --git a/util/cosas utiles/SDL_gfx/SDL_rotozoom.c b/util/cosas utiles/SDL_gfx/SDL_rotozoom.c new file mode 100644 index 0000000..7e2632e --- /dev/null +++ b/util/cosas utiles/SDL_gfx/SDL_rotozoom.c @@ -0,0 +1,939 @@ +/* + + SDL_rotozoom.c - rotozoomer for 32bit or 8bit surfaces + + LGPL (c) A. Schiffler + +*/ + +#ifdef WIN32 +#include +#endif + +#include +#include + +#include "SDL_rotozoom.h" + +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) + +/* + + 32bit Zoomer with optional anti-aliasing by bilinear interpolation. + + Zoomes 32bit RGBA/ABGR 'src' surface to 'dst' surface. + +*/ + +int zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int smooth) +{ + int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep; + tColorRGBA *c00, *c01, *c10, *c11; + tColorRGBA *sp, *csp, *dp; + int sgap, dgap; + + /* + * Variable setup + */ + if (smooth) { + /* + * For interpolation: assume source dimension is one pixel + */ + /* + * smaller to avoid overflow on right and bottom edge. + */ + sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w); + sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h); + } else { + sx = (int) (65536.0 * (float) src->w / (float) dst->w); + sy = (int) (65536.0 * (float) src->h / (float) dst->h); + } + + /* + * Allocate memory for row increments + */ + if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) { + return (-1); + } + if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) { + free(sax); + return (-1); + } + + /* + * Precalculate row increments + */ + csx = 0; + csax = sax; + for (x = 0; x <= dst->w; x++) { + *csax = csx; + csax++; + csx &= 0xffff; + csx += sx; + } + csy = 0; + csay = say; + for (y = 0; y <= dst->h; y++) { + *csay = csy; + csay++; + csy &= 0xffff; + csy += sy; + } + + /* + * Pointer setup + */ + sp = csp = (tColorRGBA *) src->pixels; + dp = (tColorRGBA *) dst->pixels; + sgap = src->pitch - src->w * 4; + dgap = dst->pitch - dst->w * 4; + + /* + * Switch between interpolating and non-interpolating code + */ + if (smooth) { + + /* + * Interpolating Zoom + */ + + /* + * Scan destination + */ + csay = say; + for (y = 0; y < dst->h; y++) { + /* + * Setup color source pointers + */ + c00 = csp; + c01 = csp; + c01++; + c10 = (tColorRGBA *) ((Uint8 *) csp + src->pitch); + c11 = c10; + c11++; + csax = sax; + for (x = 0; x < dst->w; x++) { + + /* + * Interpolate colors + */ + ex = (*csax & 0xffff); + ey = (*csay & 0xffff); + t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff; + t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff; + dp->r = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff; + t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff; + dp->g = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff; + t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff; + dp->b = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff; + t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff; + dp->a = (((t2 - t1) * ey) >> 16) + t1; + + /* + * Advance source pointers + */ + csax++; + sstep = (*csax >> 16); + c00 += sstep; + c01 += sstep; + c10 += sstep; + c11 += sstep; + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer + */ + csay++; + csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch); + /* + * Advance destination pointers + */ + dp = (tColorRGBA *) ((Uint8 *) dp + dgap); + } + + } else { + + /* + * Non-Interpolating Zoom + */ + + csay = say; + for (y = 0; y < dst->h; y++) { + sp = csp; + csax = sax; + for (x = 0; x < dst->w; x++) { + /* + * Draw + */ + *dp = *sp; + /* + * Advance source pointers + */ + csax++; + sp += (*csax >> 16); + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer + */ + csay++; + csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch); + /* + * Advance destination pointers + */ + dp = (tColorRGBA *) ((Uint8 *) dp + dgap); + } + + } + + /* + * Remove temp arrays + */ + free(sax); + free(say); + + return (0); +} + +/* + + 8bit Zoomer without smoothing. + + Zoomes 8bit palette/Y 'src' surface to 'dst' surface. + +*/ + +int zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst) +{ + Uint32 x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy; + Uint8 *sp, *dp, *csp; + int dgap; + + /* + * Variable setup + */ + sx = (Uint32) (65536.0 * (float) src->w / (float) dst->w); + sy = (Uint32) (65536.0 * (float) src->h / (float) dst->h); + + /* + * Allocate memory for row increments + */ + if ((sax = (Uint32 *) malloc(dst->w * sizeof(Uint32))) == NULL) { + return (-1); + } + if ((say = (Uint32 *) malloc(dst->h * sizeof(Uint32))) == NULL) { + if (sax != NULL) { + free(sax); + } + return (-1); + } + + /* + * Precalculate row increments + */ + csx = 0; + csax = sax; + for (x = 0; x < dst->w; x++) { + csx += sx; + *csax = (csx >> 16); + csx &= 0xffff; + csax++; + } + csy = 0; + csay = say; + for (y = 0; y < dst->h; y++) { + csy += sy; + *csay = (csy >> 16); + csy &= 0xffff; + csay++; + } + + csx = 0; + csax = sax; + for (x = 0; x < dst->w; x++) { + csx += (*csax); + csax++; + } + csy = 0; + csay = say; + for (y = 0; y < dst->h; y++) { + csy += (*csay); + csay++; + } + + /* + * Pointer setup + */ + sp = csp = (Uint8 *) src->pixels; + dp = (Uint8 *) dst->pixels; + dgap = dst->pitch - dst->w; + + /* + * Draw + */ + csay = say; + for (y = 0; y < dst->h; y++) { + csax = sax; + sp = csp; + for (x = 0; x < dst->w; x++) { + /* + * Draw + */ + *dp = *sp; + /* + * Advance source pointers + */ + sp += (*csax); + csax++; + /* + * Advance destination pointer + */ + dp++; + } + /* + * Advance source pointer (for row) + */ + csp += ((*csay) * src->pitch); + csay++; + /* + * Advance destination pointers + */ + dp += dgap; + } + + /* + * Remove temp arrays + */ + free(sax); + free(say); + + return (0); +} + +/* + + 32bit Rotozoomer with optional anti-aliasing by bilinear interpolation. + + Rotates and zoomes 32bit RGBA/ABGR 'src' surface to 'dst' surface. + +*/ + +void transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int smooth) +{ + int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh; + tColorRGBA c00, c01, c10, c11; + tColorRGBA *pc, *sp; // *spb; + int gap; + + /* + * Variable setup + */ + xd = ((src->w - dst->w) << 15); + yd = ((src->h - dst->h) << 15); + ax = (cx << 16) - (icos * cx); + ay = (cy << 16) - (isin * cx); + sw = src->w - 1; + sh = src->h - 1; + pc = dst->pixels; + gap = dst->pitch - dst->w * 4; + + /* + * Switch between interpolating and non-interpolating code + */ + if (smooth) { + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (sdx >> 16); + dy = (sdy >> 16); + if ((dx >= -1) && (dy >= -1) && (dx < src->w) && (dy < src->h)) { + if ((dx >= 0) && (dy >= 0) && (dx < sw) && (dy < sh)) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + c00 = *sp; + sp += 1; + c01 = *sp; + sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch); + sp -= 1; + c10 = *sp; + sp += 1; + c11 = *sp; + } else if ((dx == sw) && (dy == sh)) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + c00 = *sp; + c01 = *sp; + c10 = *sp; + c11 = *sp; + } else if ((dx == -1) && (dy == -1)) { + sp = (tColorRGBA *) (src->pixels); + c00 = *sp; + c01 = *sp; + c10 = *sp; + c11 = *sp; + } else if ((dx == -1) && (dy == sh)) { + sp = (tColorRGBA *) (src->pixels); + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + c00 = *sp; + c01 = *sp; + c10 = *sp; + c11 = *sp; + } else if ((dx == sw) && (dy == -1)) { + sp = (tColorRGBA *) (src->pixels); + sp += dx; + c00 = *sp; + c01 = *sp; + c10 = *sp; + c11 = *sp; + } else if (dx == -1) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + c00 = *sp; + c01 = *sp; + c10 = *sp; + sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch); + c11 = *sp; + } else if (dy == -1) { + sp = (tColorRGBA *) (src->pixels); + sp += dx; + c00 = *sp; + c01 = *sp; + c10 = *sp; + sp += 1; + c11 = *sp; + } else if (dx == sw) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + c00 = *sp; + c01 = *sp; + sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch); + c10 = *sp; + c11 = *sp; + } else if (dy == sh) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + c00 = *sp; + sp += 1; + c01 = *sp; + c10 = *sp; + c11 = *sp; + } + /* + * Interpolate colors + */ + ex = (sdx & 0xffff); + ey = (sdy & 0xffff); + t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff; + t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff; + pc->r = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff; + t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff; + pc->g = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff; + t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff; + pc->b = (((t2 - t1) * ey) >> 16) + t1; + t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff; + t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff; + pc->a = (((t2 - t1) * ey) >> 16) + t1; + } + sdx += icos; + sdy += isin; + pc++; + } + pc = (tColorRGBA *) ((Uint8 *) pc + gap); + } + } else { + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (short) (sdx >> 16); + dy = (short) (sdy >> 16); + if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) { + sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy); + sp += dx; + *pc = *sp; + } + sdx += icos; + sdy += isin; + pc++; + } + pc = (tColorRGBA *) ((Uint8 *) pc + gap); + } + } +} + +/* + + 8bit Rotozoomer without smoothing + + Rotates and zoomes 8bit palette/Y 'src' surface to 'dst' surface. + +*/ + +void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos) +{ + int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh; + tColorY *pc, *sp; + int gap; + + /* + * Variable setup + */ + xd = ((src->w - dst->w) << 15); + yd = ((src->h - dst->h) << 15); + ax = (cx << 16) - (icos * cx); + ay = (cy << 16) - (isin * cx); + sw = src->w - 1; + sh = src->h - 1; + pc = dst->pixels; + gap = dst->pitch - dst->w; + /* + * Clear surface to colorkey + */ + memset(pc, (unsigned char) (src->format->colorkey & 0xff), dst->pitch * dst->h); + /* + * Iterate through destination surface + */ + for (y = 0; y < dst->h; y++) { + dy = cy - y; + sdx = (ax + (isin * dy)) + xd; + sdy = (ay - (icos * dy)) + yd; + for (x = 0; x < dst->w; x++) { + dx = (short) (sdx >> 16); + dy = (short) (sdy >> 16); + if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) { + sp = (tColorY *) (src->pixels); + sp += (src->pitch * dy + dx); + *pc = *sp; + } + sdx += icos; + sdy += isin; + pc++; + } + pc += gap; + } +} + +/* + + rotozoomSurface() + + Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +*/ + +#define VALUE_LIMIT 0.001 + + +/* Local rotozoom-size function with trig result return */ + +void rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight, + double *canglezoom, double *sanglezoom) +{ + double x, y, cx, cy, sx, sy; + double radangle; + int dstwidthhalf, dstheighthalf; + + /* + * Determine destination width and height by rotating a centered source box + */ + radangle = angle * (M_PI / 180.0); + *sanglezoom = sin(radangle); + *canglezoom = cos(radangle); + *sanglezoom *= zoom; + *canglezoom *= zoom; + x = width / 2; + y = height / 2; + cx = *canglezoom * x; + cy = *canglezoom * y; + sx = *sanglezoom * x; + sy = *sanglezoom * y; + dstwidthhalf = MAX((int) + ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1); + dstheighthalf = MAX((int) + ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1); + *dstwidth = 2 * dstwidthhalf; + *dstheight = 2 * dstheighthalf; +} + + +/* Publically available rotozoom-size function */ + +void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight) +{ + double dummy_sanglezoom, dummy_canglezoom; + + rotozoomSurfaceSizeTrig(width, height, angle, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom); +} + + +/* Publically available rotozoom function */ + +SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth) +{ + SDL_Surface *rz_src; + SDL_Surface *rz_dst; + double zoominv; + double sanglezoom, canglezoom, sanglezoominv, canglezoominv; + int dstwidthhalf, dstwidth, dstheighthalf, dstheight; + //double x, y, cx, cy, sx, sy; + int is32bit; + int i, src_converted; + + /* + * Sanity check + */ + if (src == NULL) + return (NULL); + + /* + * Determine if source surface is 32bit or 8bit + */ + is32bit = (src->format->BitsPerPixel == 32); + if ((is32bit) || (src->format->BitsPerPixel == 8)) { + /* + * Use source surface 'as is' + */ + rz_src = src; + src_converted = 0; + } else { + /* + * New source surface is 32bit with a defined RGBA ordering + */ + rz_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); + SDL_BlitSurface(src, NULL, rz_src, NULL); + src_converted = 1; + is32bit = 1; + } + + /* + * Sanity check zoom factor + */ + if (zoom < VALUE_LIMIT) { + zoom = VALUE_LIMIT; + } + zoominv = 65536.0 / (zoom * zoom); + + /* + * Check if we have a rotozoom or just a zoom + */ + if (fabs(angle) > VALUE_LIMIT) { + + /* + * Angle!=0: full rotozoom + */ + /* + * ----------------------- + */ + + /* Determine target size */ + rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoom, &dstwidth, &dstheight, &canglezoom, &sanglezoom); + + /* + * Calculate target factors from sin/cos and zoom + */ + sanglezoominv = sanglezoom; + canglezoominv = canglezoom; + sanglezoominv *= zoominv; + canglezoominv *= zoominv; + + /* Calculate half size */ + dstwidthhalf = dstwidth / 2; + dstheighthalf = dstheight / 2; + + /* + * Alloc space to completely contain the rotated surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0); + } + + /* + * Lock source surface + */ + SDL_LockSurface(rz_src); + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the rotation (using alpha) + */ + transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf, + (int) (sanglezoominv), (int) (canglezoominv), smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the rotation + */ + transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf, + (int) (sanglezoominv), (int) (canglezoominv)); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey); + } + /* + * Unlock source surface + */ + SDL_UnlockSurface(rz_src); + + } else { + + /* + * Angle=0: Just a zoom + */ + /* + * -------------------- + */ + + /* + * Calculate target size + */ + zoomSurfaceSize(rz_src->w, rz_src->h, zoom, zoom, &dstwidth, &dstheight); + + /* + * Alloc space to completely contain the zoomed surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0); + } + + /* + * Lock source surface + */ + SDL_LockSurface(rz_src); + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the zooming (using alpha) + */ + zoomSurfaceRGBA(rz_src, rz_dst, smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the zooming + */ + zoomSurfaceY(rz_src, rz_dst); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey); + } + /* + * Unlock source surface + */ + SDL_UnlockSurface(rz_src); + } + + /* + * Cleanup temp surface + */ + if (src_converted) { + SDL_FreeSurface(rz_src); + } + + /* + * Return destination surface + */ + return (rz_dst); +} + +/* + + zoomSurface() + + Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +*/ + +#define VALUE_LIMIT 0.001 + +void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight) +{ + /* + * Sanity check zoom factors + */ + if (zoomx < VALUE_LIMIT) { + zoomx = VALUE_LIMIT; + } + if (zoomy < VALUE_LIMIT) { + zoomy = VALUE_LIMIT; + } + + /* + * Calculate target size + */ + *dstwidth = (int) ((double) width * zoomx); + *dstheight = (int) ((double) height * zoomy); + if (*dstwidth < 1) { + *dstwidth = 1; + } + if (*dstheight < 1) { + *dstheight = 1; + } +} + +SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth) +{ + SDL_Surface *rz_src; + SDL_Surface *rz_dst; + int dstwidth, dstheight; + int is32bit; + int i, src_converted; + + /* + * Sanity check + */ + if (src == NULL) + return (NULL); + + /* + * Determine if source surface is 32bit or 8bit + */ + is32bit = (src->format->BitsPerPixel == 32); + if ((is32bit) || (src->format->BitsPerPixel == 8)) { + /* + * Use source surface 'as is' + */ + rz_src = src; + src_converted = 0; + } else { + /* + * New source surface is 32bit with a defined RGBA ordering + */ + rz_src = + SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); + SDL_BlitSurface(src, NULL, rz_src, NULL); + src_converted = 1; + is32bit = 1; + } + + /* Get size if target */ + zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight); + + /* + * Alloc space to completely contain the zoomed surface + */ + rz_dst = NULL; + if (is32bit) { + /* + * Target surface is 32bit with source RGBA/ABGR ordering + */ + rz_dst = + SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32, + rz_src->format->Rmask, rz_src->format->Gmask, + rz_src->format->Bmask, rz_src->format->Amask); + } else { + /* + * Target surface is 8bit + */ + rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0); + } + + /* + * Lock source surface + */ + SDL_LockSurface(rz_src); + /* + * Check which kind of surface we have + */ + if (is32bit) { + /* + * Call the 32bit transformation routine to do the zooming (using alpha) + */ + zoomSurfaceRGBA(rz_src, rz_dst, smooth); + /* + * Turn on source-alpha support + */ + SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); + } else { + /* + * Copy palette and colorkey info + */ + for (i = 0; i < rz_src->format->palette->ncolors; i++) { + rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; + } + rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* + * Call the 8bit transformation routine to do the zooming + */ + zoomSurfaceY(rz_src, rz_dst); + SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey); + } + /* + * Unlock source surface + */ + SDL_UnlockSurface(rz_src); + + /* + * Cleanup temp surface + */ + if (src_converted) { + SDL_FreeSurface(rz_src); + } + + /* + * Return destination surface + */ + return (rz_dst); +} diff --git a/util/cosas utiles/SDL_gfx/SDL_rotozoom.h b/util/cosas utiles/SDL_gfx/SDL_rotozoom.h new file mode 100644 index 0000000..961a72e --- /dev/null +++ b/util/cosas utiles/SDL_gfx/SDL_rotozoom.h @@ -0,0 +1,99 @@ + +/* + + SDL_rotozoom - rotozoomer + + LGPL (c) A. Schiffler + +*/ + +#ifndef _SDL_rotozoom_h +#define _SDL_rotozoom_h + +#include + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef M_PI +#define M_PI 3.141592654 +#endif + +#include + +/* ---- Defines */ + +#define SMOOTHING_OFF 0 +#define SMOOTHING_ON 1 + +/* ---- Structures */ + + typedef struct tColorRGBA { + Uint8 r; + Uint8 g; + Uint8 b; + Uint8 a; + } tColorRGBA; + + typedef struct tColorY { + Uint8 y; + } tColorY; + + +/* ---- Prototypes */ + +#ifdef WIN32 +#ifdef BUILD_DLL +#define DLLINTERFACE __declspec(dllexport) +#else +#define DLLINTERFACE __declspec(dllimport) +#endif +#else +#define DLLINTERFACE +#endif + +/* + + rotozoomSurface() + + Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +*/ + + DLLINTERFACE SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth); + + +/* Returns the size of the target surface for a rotozoomSurface() call */ + + DLLINTERFACE void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, + int *dstheight); + +/* + + zoomSurface() + + Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. + 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1 + then the destination 32bit surface is anti-aliased. If the surface is not 8bit + or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly. + +*/ + + DLLINTERFACE SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth); + +/* Returns the size of the target surface for a zoomSurface() call */ + + DLLINTERFACE void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight); + + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +}; +#endif + +#endif /* _SDL_rotozoom_h */ diff --git a/util/cosas utiles/kplib/kplib.c b/util/cosas utiles/kplib/kplib.c new file mode 100644 index 0000000..84ac754 --- /dev/null +++ b/util/cosas utiles/kplib/kplib.c @@ -0,0 +1,2889 @@ +/************************************************************************************************** +KPLIB.C: Ken's Picture LIBrary written by Ken Silverman +Copyright (c) 1998-2004 Ken Silverman +Ken Silverman's official web site: http://www.advsys.net/ken + +This source file includes routines for decompression of the following picture formats: + JPG,PNG,GIF,PCX,TGA,CEL +It also includes code to handle ZIP decompression. + +Brief history: +1998?: Wrote KPEG, a JPEG viewer for DOS +2000: Wrote KPNG, a PNG viewer for DOS +2001: Combined KPEG&KPNG, ported to Visual C, and made it into a nice library called KPLIB.C +2002: Added support for: TGA,GIF,CEL,ZIP +2003: Added support for: BMP +05/18/2004: Added support for 8/24 bit PCX + +I offer this code to the community for the benefit of Jonathon Fowler's Duke3D port. + +-Ken S. +**************************************************************************************************/ + //KPLIB.C is used in these dirs: voxlap,wintest,groudraw,polyspri,test,kgl,pano,ksuck + //kpnggetdimen,uninitkpng,kpng legacy code used by these: kpng(old),kube,kubegl + +#ifdef LINUX +#include +typedef long long __int64; +static __inline long _lrotl (long i, int sh) + { return((i>>(-sh))|(i< +#endif +#include +#include +#include +#include +#include +#ifndef NOKPLIBZIP +#ifndef _WIN32 +#include +#include +#else +#define WIN32_LEAN_AND_MEAN +#include +#include +#endif +#endif //NOKPLIBZIP +#ifndef O_BINARY +#define O_BINARY 0 +#endif +#if !defined(max) +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#endif +#if !defined(min) +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif + + //use GCC-specific extension to force symbol name to be something in particular to override underscoring. +#if defined(__GNUC__) && defined(__i386__) && !defined(NOASM) +#define ASMNAME(x) asm(x) +#else +#define ASMNAME(x) +#endif + +static long frameplace, bytesperline, xres, yres, globxoffs, globyoffs; + +static long pow2mask[32]; + + //Hack for peekbits,getbits,suckbits (to prevent lots of duplicate code) + // 0: PNG: do 12-byte chunk_header removal hack + // !=0: ZIP: use 64K buffer (olinbuf) +static long zipfilmode; +typedef struct +{ + FILE *fil; //0:no file open, !=0:open file (either stand-alone or zip) + long comptyp; //0:raw data (can be ZIP or stand-alone), 8:PKZIP LZ77 *flate + long seek0; //0:stand-alone file, !=0: start of zip compressed stream data + long compleng;//Global variable for compression FIFO + long comptell;//Global variable for compression FIFO + long leng; //Uncompressed file size (bytes) + long pos; //Current uncompressed relative file position (0<=pos<=leng) + long endpos; //Temp global variable for kzread + long jmpplc; //Store place where decompression paused + long i; //For stand-alone/ZIP comptyp#0, this is like "uncomptell" + //For ZIP comptyp#8&btype==0 "<64K store", this saves i state + long bfinal; //LZ77 decompression state (for later calls) +} kzfilestate; +static kzfilestate kzfs; + +//Initialized tables (can't be in union) +//jpg: png: +// crmul 16384 abstab10 4096 +// cbmul 16384 hxbit 472 +// dct 4608 pow2mask 128* +// colclip 4096 +// colclipup8 4096 +// colclipup16 4096 +// unzig 256 +// pow2mask 128* +// dcflagor 64 + +long palcol[256] ASMNAME("palcol"), paleng; +unsigned char coltype, bitdepth; + +//============================ KPNGILIB begins =============================== + +#define PROCESSALPHAHERE 0 //Set to 1 for KPNG, 0 in all other cases + +//07/31/2000: KPNG.C first ported to C from READPNG.BAS +//10/11/2000: KPNG.C split into 2 files: KPNG.C and PNGINLIB.C +//11/24/2000: Finished adding support for coltypes 4&6 +//03/31/2001: Added support for Adam7-type interlaced images +//Currently, there is no support for: +// * 16-bit color depth +// * Some useless ancillary chunks, like: gAMA(gamma) & pHYs(aspect ratio) + + //.PNG specific variables: +static long bakcol = 0xff808080, bakr = 0x80, bakg = 0x80, bakb = 0x80; //this used to be public... +static long slidew = 0, slider = 0, xm, xmn[4], xr0, xr1, xplc, yplc, nfplace; +static long clen[320], cclen[19], bitpos, filt, xsiz, ysiz; +static long xsizbpl, ixsiz, ixoff, iyoff, ixstp, iystp, intlac, nbpl, trnsrgb ASMNAME("trnsrgb"); +static long ccind[19] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; +static long hxbit[59][2], ibuf0[288], nbuf0[32], ibuf1[32], nbuf1[32]; +static const unsigned char *filptr; +static unsigned char slidebuf[32768], opixbuf0[4], opixbuf1[4]; +static unsigned char pnginited = 0, olinbuf[65536] ASMNAME("olinbuf"); //WARNING:max xres is: 65536/bpp-1 +static long gotcmov = -2, abstab10[1024] ASMNAME("abstab10"); + + //Variables to speed up dynamic Huffman decoding: +#define LOGQHUFSIZ0 9 +#define LOGQHUFSIZ1 6 +static long qhufval0[1<>8) + ((a&0xff00)<<8) + (a<<24) + (a>>24)); +} + +static inline long bitrev (long b, long c) +{ + long i, j; + for(i=1,j=0,c=(1< 0x8000000) + { + cpuid(0x80000001,cpb); + i |= (cpb[3]&(1<<31)); + if (!((cpid[1]^0x68747541)|(cpid[3]^0x69746e65)|(cpid[2]^0x444d4163))) //AuthenticAMD + i |= (cpb[3]&((1<<22)|(1<<30))); + } + if (i&(1<<25)) i |= (1<<22); //SSE implies MMX+ support + return(i); +} + +static unsigned char fakebuf[8], *nfilptr; +static long nbitpos; +static long peekbits (long n) +{ + return(((*(long *)&filptr[bitpos>>3])>>(bitpos&7))&pow2mask[n]); +} + +static void suckbits (long n) +{ + bitpos += n; if (bitpos < 0) return; + if (!zipfilmode) + { + if (!nfilptr) + { //|===|===|crc|lng|typ|===|===| + // \ fakebuf: / + // |===|===| + //----x O---x O-------- + nbitpos = bswap(*(long *)&filptr[8]); + nfilptr = (unsigned char *)&filptr[nbitpos+12]; + *(long *)&fakebuf[0] = *(long *)&filptr[0]; //Copy last dword of IDAT chunk + if (*(long *)&filptr[12] == 0x54414449) //Copy 1st dword of next IDAT chunk + *(long *)&fakebuf[4] = *(long *)&filptr[16]; + filptr = &fakebuf[4]; bitpos -= 32; + } + else + { + filptr = nfilptr; nfilptr = 0; + bitpos -= ((nbitpos-4)<<3); + } + //if (n < 4) It will crash - do something to fix this! + } + else + { + //NOTE: should only read bytes inside compsize, not 64K!!! :/ + *(long *)&olinbuf[0] = *(long *)&olinbuf[sizeof(olinbuf)-4]; + n = min(kzfs.compleng-kzfs.comptell,sizeof(olinbuf)-4); + fread(&olinbuf[4],n,1,kzfs.fil); + kzfs.comptell += n; + bitpos -= ((sizeof(olinbuf)-4)<<3); + } +} + +static long getbits (long n) { long i = peekbits(n); suckbits(n); return(i); } + +static long hufgetsym (long *hitab, long *hbmax) +{ + long v, n; + + v = n = 0; + do { v = (v<<1)+getbits(1)+hbmax[n]-hbmax[n+1]; n++; } while (v >= 0); + return(hitab[hbmax[n]+v]); +} + +static void qhufgencode (long *hitab, long *hbmax, long *qhval, unsigned char *qhbit, long numbits) +{ + long i, j, k, n, r; + + //r is the bit reverse of i. Ex: if: i = 1011100111, r = 1110011101 + i = r = 0; + for(n=1;n<=numbits;n++) + for(k=hbmax[n-1];k>n)&1) + hbmax[n]-hbmax[n+1]; + // + //n = numbits; + //k = hbmax[n]-r; + // + //j = peekbits(LOGQHUFSIZ); i = qhufval[j]; j = qhufbit[j]; + // + //i = j = 0; + //do + //{ + // i = (i<<1)+getbits(1)+nbuf0[j]-nbuf0[j+1]; j++; + //} while (i >= 0); + //i = ibuf0[nbuf0[j]+i]; + //qhval[r] = k; + + qhbit[r] = 0; //n-32; + i++; + } +} + + //inbuf[inum] : Bit length of each symbol + //inum : Number of indices + //hitab[inum] : Indices from size-ordered list to original symbol + //hbmax[0-31] : Highest index (+1) of n-bit symbol +static void hufgencode (long *inbuf, long inum, long *hitab, long *hbmax) +{ + long i, tbuf[31]; + + for(i=30;i;i--) tbuf[i] = 0; + for(i=inum-1;i>=0;i--) tbuf[inbuf[i]]++; + tbuf[0] = hbmax[0] = 0; //Hack to remove symbols of length 0? + for(i=0;i<31;i++) hbmax[i+1] = hbmax[i]+tbuf[i]; + for(i=0;i>i)&15); + iyoff = ((0x00402010>>i)&15); + if (((ixoff >= xsiz) || (iyoff >= ysiz)) && (intlac >= 2)) { i = -1; intlac--; } + } while (i < 0); + j = ((0x33221100>>i)&15); ixstp = (1<>i)&15); iystp = (1<>3) = 2 + //j=3,ixoff=4 2 ((12+(1<<3)-1 - 4)>>3) = 1 + //j=2,ixoff=2 3 4 5 ((12+(1<<2)-1 - 2)>>2) = 3 + //j=1,ixoff=1 6 7 8 9 a b ((12+(1<<1)-1 - 1)>>1) = 6 + ixsiz = ((xsiz+ixstp-1-ixoff)>>j); //It's confusing! See the above example. + nbpl = (bytesperline<>(coltype<<2))&15)*ixsiz; + switch (bitdepth) + { + case 1: xsizbpl = ((xsizbpl+7)>>3); break; + case 2: xsizbpl = ((xsizbpl+3)>>2); break; + case 4: xsizbpl = ((xsizbpl+1)>>1); break; + } + + memset(olinbuf,0,(xsizbpl+1)*sizeof(olinbuf[0])); + *(long *)&opixbuf0[0] = *(long *)&opixbuf1[0] = 0; + xplc = xsizbpl; yplc = globyoffs+iyoff; xm = 0; filt = -1; + + i = globxoffs+ixoff; i = (((-(i>=0))|(ixstp-1))&i); + k = (((-(yplc>=0))|(iystp-1))&yplc); + nfplace = k*bytesperline + (i<<2) + frameplace; + + //Precalculate x-clipping to screen borders (speeds up putbuf) + //Equation: (0 <= xr <= ixsiz) && (0 <= xr*ixstp+globxoffs+ixoff <= xres) + xr0 = max((-globxoffs-ixoff+(1<>j,0); + xr1 = min((xres-globxoffs-ixoff+(1<>j,ixsiz); + xr0 = ixsiz-xr0; + xr1 = ixsiz-xr1; + + if (coltype == 4) { xr0 = xr0*2; xr1 = xr1*2; } + else if (coltype == 2) { xr0 = xr0*3-2; xr1 = xr1*3-2; } + else if (coltype == 6) { xr0 = xr0*4-2; xr1 = xr1*4-2; } + else + { + switch(bitdepth) + { + case 1: xr0 += ((-ixsiz)&7)+7; + xr1 += ((-ixsiz)&7)+7; break; + case 2: xr0 = ((xr0+((-ixsiz)&3)+3)<<1); + xr1 = ((xr1+((-ixsiz)&3)+3)<<1); break; + case 4: xr0 = ((xr0+((-ixsiz)&1)+1)<<2); + xr1 = ((xr1+((-ixsiz)&1)+1)<<2); break; + } + } + ixstp <<= 2; + return(0); +} + +static long Paeth (long a, long b, long c) +{ + long pa, pb, pc; + + pa = b-c; pb = a-c; pc = labs(pa+pb); pa = labs(pa); pb = labs(pb); + if ((pa <= pb) && (pa <= pc)) return(a); + if (pb <= pc) return(b); else return(c); +} + +#if defined(__WATCOMC__) && !defined(NOASM) + + //NOTE: cmov now has correctly ordered registers (thx to bug fix in 11.0c!) +static long Paeth686 (long, long, long); +#pragma aux Paeth686 =\ + ".686"\ + "mov edx, ecx"\ + "sub edx, eax"\ + "sub edx, ebx"\ + "lea edx, abstab10[edx*4+2048]"\ + "mov esi, [ebx*4+edx]"\ + "mov edi, [ecx*4+edx]"\ + "cmp edi, esi"\ + "cmovge edi, esi"\ + "cmovge ecx, ebx"\ + "cmp edi, [eax*4+edx]"\ + "cmovge ecx, eax"\ + parm nomemory [eax][ebx][ecx]\ + modify exact [ecx edx esi edi]\ + value [ecx] + + //Note: "cmove eax,?" may be faster than "jne ?:and eax,?" but who cares +static void rgbhlineasm (long, long, long, long); +#pragma aux rgbhlineasm =\ + "sub ecx, edx"\ + "jle short endit"\ + "add edx, offset olinbuf"\ + "cmp dword ptr trnsrgb, 0"\ + "jz short begit2"\ + "begit: mov eax, dword ptr [ecx+edx]"\ + "or eax, 0xff000000"\ + "jne short skipit"\ + "and eax, 0xffffff"\ + "skipit: sub ecx, 3"\ + "mov [edi], eax"\ + "lea edi, [edi+ebx]"\ + "jnz short begit"\ + "jmp short endit"\ + "begit2: mov eax, dword ptr [ecx+edx]"\ + "or eax, 0xff000000"\ + "sub ecx, 3"\ + "mov [edi], eax"\ + "lea edi, [edi+ebx]"\ + "jnz short begit2"\ + "endit:"\ + parm [ecx][edx][edi][ebx]\ + modify exact [eax ecx edi]\ + value + +static void pal8hlineasm (long, long, long, long); +#pragma aux pal8hlineasm =\ + "sub ecx, edx"\ + "jle short endit"\ + "add edx, offset olinbuf"\ + "begit: movzx eax, byte ptr [ecx+edx]"\ + "mov eax, dword ptr palcol[eax*4]"\ + "dec ecx"\ + "mov [edi], eax"\ + "lea edi, [edi+ebx]"\ + "jnz short begit"\ + "endit:"\ + parm [ecx][edx][edi][ebx]\ + modify exact [eax ecx edi]\ + value + +#elif defined(_MSC_VER) && !defined(NOASM) + +static _inline long Paeth686 (long a, long b, long c) +{ + _asm + { + mov eax, a + mov ebx, b + mov ecx, c + mov edx, ecx + sub edx, eax + sub edx, ebx + lea edx, abstab10[edx*4+2048] + mov esi, [ebx*4+edx] + mov edi, [ecx*4+edx] + cmp edi, esi + cmovge edi, esi + cmovge ecx, ebx + cmp edi, [eax*4+edx] + cmovl eax, ecx + } +} + +static _inline void rgbhlineasm (long c, long d, long t, long b) +{ + _asm + { + mov ecx, c + mov edx, d + mov edi, t + mov ebx, b + sub ecx, edx + jle short endit + add edx, offset olinbuf + cmp dword ptr trnsrgb, 0 + jz short begit2 + begit: mov eax, dword ptr [ecx+edx] + or eax, 0xff000000 + jne short skipit + and eax, 0xffffff + skipit: sub ecx, 3 + mov [edi], eax + lea edi, [edi+ebx] + jnz short begit + jmp short endit + begit2: mov eax, dword ptr [ecx+edx] + or eax, 0xff000000 + sub ecx, 3 + mov [edi], eax + lea edi, [edi+ebx] + jnz short begit2 + endit: + } +} + +static _inline void pal8hlineasm (long c, long d, long t, long b) +{ + _asm + { + mov ecx, c + mov edx, d + mov edi, t + mov ebx, b + sub ecx, edx + jle short endit + add edx, offset olinbuf + begit: movzx eax, byte ptr [ecx+edx] + mov eax, dword ptr palcol[eax*4] + dec ecx + mov [edi], eax + lea edi, [edi+ebx] + jnz short begit + endit: + } +} + +#elif defined(__GNUC__) && defined(__i386__) && !defined(NOASM) + +static inline long Paeth686 (long a, long b, long c) +{ + __asm__ __volatile__ ( + "movl %%ecx, %%edx\n\tsubl %%eax, %%edx\n\tsubl %%ebx, %%edx\n\t" + "leal (abstab10+2048)(,%%edx,4), %%edx\n\t" + "movl (%%edx,%%ebx,4), %%esi\n\tmovl (%%edx,%%ecx,4), %%edi\n\t" + "cmpl %%esi, %%edi\n\tcmovgel %%esi, %%edi\n\tcmovgel %%ebx, %%ecx\n\t" + "cmpl (%%edx,%%eax,4), %%edi\n\tcmovgel %%eax, %%ecx" + : "+c" (c) : "a" (a), "b" (b) : "esi","edi","memory","cc" + ); + return c; +} + + //Note: "cmove eax,?" may be faster than "jne ?:and eax,?" but who cares +static inline void rgbhlineasm (long c, long d, long t, long b) +{ + __asm__ __volatile__ ( + "subl %%edx, %%ecx\n\tjle 3f\n\taddl $olinbuf, %%edx\n\t" + "cmpl $0, trnsrgb(,1)\n\tjz 2f\n\t" + "0: movl (%%ecx,%%edx,1), %%eax\n\torl $0xff000000, %%eax\n\t" + "jne 1f\n\tandl $0xffffff, %%eax\n\t" + "1: subl $3, %%ecx\n\tmovl %%eax, (%%edi)\n\tleal (%%edi,%%ebx,1), %%edi\n\t" + "jnz 0b\n\tjmp 3f\n\t" + "2: movl (%%ecx,%%edx,1), %%eax\n\torl $0xff000000, %%eax\n\tsubl $3, %%ecx\n\t" + "movl %%eax, (%%edi)\n\tleal (%%edi,%%ebx,1), %%edi\n\tjnz 2b\n\t" + "3:" + : "+c" (c), "+D" (t) : "d" (d), "b" (b) : "eax","memory","cc" + ); +} + +static inline void pal8hlineasm (long c, long d, long t, long b) +{ + __asm__ __volatile__ ( + "subl %%edx, %%ecx\n\tjle 1f\n\taddl $olinbuf, %%edx\n\t" + "0: movzbl (%%ecx,%%edx,1), %%eax\n\tmovl palcol(,%%eax,4), %%eax\n\t" + "decl %%ecx\n\tmovl %%eax, (%%edi)\n\tleal (%%edi,%%ebx,1), %%edi\n\tjnz 0b\n\t" + "1:" + : "+c" (c), "+D" (t) : "d" (d), "b" (b) : "eax","memory","cc" + ); +} + +#else + +static inline long Paeth686 (long a, long b, long c) +{ + return(Paeth(a,b,c)); +} + +static inline void rgbhlineasm (long x, long xr1, long p, long ixstp) +{ + for(;x>xr1;p+=ixstp,x-=3) *(long *)p = *(long *)&olinbuf[x]; +} + +static inline void pal8hlineasm (long x, long xr1, long p, long ixstp) +{ + for(;x>xr1;p+=ixstp,x--) *(long *)p = palcol[olinbuf[x]]; +} + +#endif + +static void putbuf (const unsigned char *buf, long leng) +{ + long i, x, p; + + if (filt < 0) + { + if (leng <= 0) return; + filt = buf[0]; if (filt == gotcmov) filt = 5; + i = 1; + } else i = 0; + + while (i < leng) + { + x = i+xplc; if (x > leng) x = leng; + switch (filt) + { + case 0: + while (i < x) { olinbuf[xplc] = buf[i]; xplc--; i++; } + break; + case 1: + while (i < x) + { + olinbuf[xplc] = (opixbuf1[xm] += buf[i]); + xm = xmn[xm]; xplc--; i++; + } + break; + case 2: + while (i < x) { olinbuf[xplc] += buf[i]; xplc--; i++; } + break; + case 3: + while (i < x) + { + opixbuf1[xm] = olinbuf[xplc] = ((opixbuf1[xm]+olinbuf[xplc])>>1)+buf[i]; + xm = xmn[xm]; xplc--; i++; + } + break; + case 4: + while (i < x) + { + opixbuf1[xm] = (char)(Paeth(opixbuf1[xm],olinbuf[xplc],opixbuf0[xm])+buf[i]); + opixbuf0[xm] = olinbuf[xplc]; + olinbuf[xplc] = opixbuf1[xm]; + xm = xmn[xm]; xplc--; i++; + } + break; + case 5: //Special hack for Paeth686 (Doesn't have to be case 5) + while (i < x) + { + opixbuf1[xm] = (char)(Paeth686(opixbuf1[xm],olinbuf[xplc],opixbuf0[xm])+buf[i]); + opixbuf0[xm] = olinbuf[xplc]; + olinbuf[xplc] = opixbuf1[xm]; + xm = xmn[xm]; xplc--; i++; + } + break; + } + + if (xplc > 0) return; + + //Draw line! + if ((unsigned long)yplc < (unsigned long)yres) + { + x = xr0; p = nfplace; + switch (coltype) + { + case 2: + rgbhlineasm(x,xr1,p,ixstp); //for(;x>xr1;p+=ixstp,x-=3) *(long *)p = *(long *)&olinbuf[x]; + break; + case 4: + for(;x>xr1;p+=ixstp,x-=2) + { +#if (PROCESSALPHAHERE == 1) + //Enable this code to process alpha right here! + if (olinbuf[x-1] == 255) { *(long *)p = palcol[olinbuf[x]]; continue; } + if (!olinbuf[x-1]) { *(long *)p = bakcol; continue; } + //I do >>8, but theoretically should be: /255 + *(char *)(p) = *(char *)(p+1) = *(char *)(p+2) = + (((((long)olinbuf[x])-bakr)*(long)olinbuf[x-1])>>8) + bakr; +#else + *(long *)p = palcol[olinbuf[x]]; + *(char *)(p+3) = olinbuf[x-1]; //A +#endif + } + break; + case 6: + for(;x>xr1;p+=ixstp,x-=4) + { +#if (PROCESSALPHAHERE == 1) + //Enable this code to process alpha right here! + if (olinbuf[x-1] == 255) { *(long *)p = *(long *)&olinbuf[x]; continue; } + if (!olinbuf[x-1]) { *(long *)p = bakcol; continue; } + //I do >>8, but theoretically should be: /255 + *(char *)(p ) = (((((long)olinbuf[x ])-bakr)*(long)olinbuf[x-1])>>8) + bakr; + *(char *)(p+1) = (((((long)olinbuf[x+1])-bakg)*(long)olinbuf[x-1])>>8) + bakg; + *(char *)(p+2) = (((((long)olinbuf[x+2])-bakb)*(long)olinbuf[x-1])>>8) + bakb; +#else + *(char *)(p ) = olinbuf[x ]; //R + *(char *)(p+1) = olinbuf[x+1]; //G + *(char *)(p+2) = olinbuf[x+2]; //B + *(char *)(p+3) = olinbuf[x-1]; //A +#endif + } + break; + default: + switch(bitdepth) + { + case 1: for(;x>xr1;p+=ixstp,x-- ) *(long *)p = palcol[olinbuf[x>>3]>>(x&7)]; break; + case 2: for(;x>xr1;p+=ixstp,x-=2) *(long *)p = palcol[olinbuf[x>>3]>>(x&6)]; break; + case 4: for(;x>xr1;p+=ixstp,x-=4) *(long *)p = palcol[olinbuf[x>>3]>>(x&4)]; break; + case 8: pal8hlineasm(x,xr1,p,ixstp); break; //for(;x>xr1;p+=ixstp,x--) *(long *)p = palcol[olinbuf[x]]; break; + } + break; + } + nfplace += nbpl; + } + + *(long *)&opixbuf0[0] = *(long *)&opixbuf1[0] = 0; + xplc = xsizbpl; yplc += iystp; + if ((intlac) && (yplc >= globyoffs+ysiz)) { intlac--; initpass(); } + if (i < leng) { filt = buf[i++]; if (filt == gotcmov) filt = 5; } else filt = -1; + } +} + +static void initpngtables() +{ + long i, j, k; + + //hxbit[0-58][0-1] is a combination of 4 different tables: + // 1st parameter: [0-29] are distances, [30-58] are lengths + // 2nd parameter: [0]: extra bits, [1]: base number + + j = 1; k = 0; + for(i=0;i<30;i++) + { + hxbit[i][1] = j; j += (1<= 2)); + } + j = 3; k = 0; + for(i=257;i<285;i++) + { + hxbit[i+30-257][1] = j; j += (1<= 264)); + } + hxbit[285+30-257][1] = 258; hxbit[285+30-257][0] = 0; + + for(i=31;i>=0;i--) pow2mask[i] = (1<= 2) return(-1); //"Unsupported interlace type" + intlac = filptr[12]*7; //0=no interlace/1=Adam7 interlace + + //Save code by making grayscale look like a palette color scheme + if ((!coltype) || (coltype == 4)) + { + j = 0xff000000; k = (255 / ((1<=0;i--) palcol[i] = ((bswap(*(long *)&filptr[i*3])>>8)|0xff000000); + } + else if (i == 0x44474b62) //bKGD (must be after PLTE and before IDAT) + { + switch(coltype) + { + case 0: case 4: + bakcol = (((long)filptr[0]<<8)+(long)filptr[1])*255/((1<>16)&255); + bakg = ((bakcol>>8)&255); + bakb = (bakcol&255); + } + else if (i == 0x534e5274) //tRNS (must be after PLTE and before IDAT) + { + switch(coltype) + { + case 0: + palcol[((long)filptr[0]<<8)+(long)filptr[1]] &= 0xffffff; + break; + case 2: + if (bitdepth == 8) + { trnsrgb = (((long)filptr[0])<<16)+(((long)filptr[2])<<8)+((long)filptr[4])+0xff000000; } + //else {} //WARNING: PNG docs say: MUST compare all 48 bits :( + break; + case 3: + for(i=min(leng,paleng)-1;i>=0;i--) + palcol[i] &= ((((long)filptr[i])<<24)|0xffffff); + break; + default:; + } + } + else if (i == 0x54414449) { break; } //IDAT + + filptr = &filptr[leng+4]; //crc = bswap(*(long *)&filptr[-4]); + } + + //Initialize this for the getbits() function + zipfilmode = 0; + filptr = &filptr[leng-4]; bitpos = -((leng-4)<<3); nfilptr = 0; + //if (leng < 4) It will crash - do something to fix this! + + frameplace = daframeplace; + bytesperline = dabytesperline; + xres = daxres; + yres = dayres; + globxoffs = daglobxoffs; + globyoffs = daglobyoffs; + + switch (coltype) + { + case 4: xmn[0] = 1; xmn[1] = 0; break; + case 2: xmn[0] = 1; xmn[1] = 2; xmn[2] = 0; break; + case 6: xmn[0] = 1; xmn[1] = 2; xmn[2] = 3; xmn[3] = 0; break; + default: xmn[0] = 0; break; + } + switch (bitdepth) + { + case 1: for(i=2;i<256;i++) palcol[i] = palcol[i&1]; break; + case 2: for(i=4;i<256;i++) palcol[i] = palcol[i&3]; break; + case 4: for(i=16;i<256;i++) palcol[i] = palcol[i&15]; break; + } + + //coltype: bitdepth: format: + // 0 1,2,4,8,16 I + // 2 8,16 RGB + // 3 1,2,4,8 P + // 4 8,16 IA + // 6 8,16 RGBA + xsizbpl = ((0x04021301>>(coltype<<2))&15)*xsiz; + switch (bitdepth) + { + case 1: xsizbpl = ((xsizbpl+7)>>3); break; + case 2: xsizbpl = ((xsizbpl+3)>>2); break; + case 4: xsizbpl = ((xsizbpl+1)>>1); break; + } + //Tests to see if xsiz > allocated space in olinbuf + //Note: xsizbpl gets re-written inside initpass() + if ((xsizbpl+1)*sizeof(olinbuf[0]) > sizeof(olinbuf)) return(-1); + + initpass(); + + slidew = 0; slider = 16384; + suckbits(16); //Actually 2 fields: 8:compmethflags, 8:addflagscheck + do + { + bfinal = getbits(1); btype = getbits(2); + if (btype == 0) + { + //Raw (uncompressed) + suckbits((-bitpos)&7); //Synchronize to start of next byte + i = getbits(16); if ((getbits(16)^i) != 0xffff) return(-1); + for(;i;i--) + { + if (slidew >= slider) + { + putbuf(&slidebuf[(slider-16384)&32767],16384); slider += 16384; + if ((yplc >= yres) && (intlac < 2)) return(0); + } + slidebuf[(slidew++)&32767] = (char)getbits(8); + } + continue; + } + if (btype == 3) continue; + + if (btype == 1) //Fixed Huffman + { + hlit = 288; hdist = 32; i = 0; + for(;i<144;i++) clen[i] = 8; //Fixed bit sizes (literals) + for(;i<256;i++) clen[i] = 9; //Fixed bit sizes (literals) + for(;i<280;i++) clen[i] = 7; //Fixed bit sizes (EOI,lengths) + for(;i<288;i++) clen[i] = 8; //Fixed bit sizes (lengths) + for(;i<320;i++) clen[i] = 5; //Fixed bit sizes (distances) + } + else //Dynamic Huffman + { + hlit = getbits(5)+257; hdist = getbits(5)+1; j = getbits(4)+4; + for(i=0;i= slider) + { + putbuf(&slidebuf[(slider-16384)&32767],16384); slider += 16384; + if ((yplc >= yres) && (intlac < 2)) return(0); + } + + k = peekbits(LOGQHUFSIZ0); + if (qhufbit0[k]) { i = qhufval0[k]; suckbits((long)qhufbit0[k]); } + else i = hufgetsym(ibuf0,nbuf0); + + if (i < 256) { slidebuf[(slidew++)&32767] = (char)i; continue; } + if (i == 256) break; + i = getbits(hxbit[i+30-257][0]) + hxbit[i+30-257][1]; + + k = peekbits(LOGQHUFSIZ1); + if (qhufbit1[k]) { j = qhufval1[k]; suckbits((long)qhufbit1[k]); } + else j = hufgetsym(ibuf1,nbuf1); + + j = getbits(hxbit[j][0]) + hxbit[j][1]; + for(;i;i--) slidebuf[(slidew++)&32767] = slidebuf[(slidew-j)&32767]; + } + } while (!bfinal); + + slider -= 16384; + if (!((slider^slidew)&32768)) + putbuf(&slidebuf[slider&32767],slidew-slider); + else + { + putbuf(&slidebuf[slider&32767],(-slider)&32767); + putbuf(slidebuf,slidew&32767); + } + return(0); +} + +//============================= KPNGILIB ends ================================ +//============================ KPEGILIB begins =============================== + + //11/01/2000: This code was originally from KPEG.C + // All non 32-bit color drawing was removed + // "Motion" JPG code was removed + // A lot of parameters were added to kpeg() for library usage +static long kpeginited = 0; +static long clipxdim, clipydim; + +static long hufmaxatbit[8][20], hufvalatbit[8][20], hufcnt[8]; +static unsigned char hufnumatbit[8][20], huftable[8][256]; +static long hufquickval[8][1024], hufquickbits[8][1024], hufquickcnt[8]; +static long quantab[4][64], dct[19][64], lastdc[4], unzig[64]; +static unsigned char gnumcomponents, dcflagor[64]; +static long gcompid[4], gcomphsamp[4], gcompvsamp[4], gcompquantab[4]; +static long lcompid[4], lcompdc[4], lcompac[4]; +static long lcomphsamp[4], lcompvsamp[4], lcomphvsamp[4], lcompquantab[4]; +static long lcomphsampmask[4], lcomphsampshift[4], lcompvsampshift[4]; +static long colclip[1024], colclipup8[1024], colclipup16[1024]; +static unsigned char pow2char[8] = {1,2,4,8,16,32,64,128}; + +#if defined(__WATCOMC__) && !defined(NOASM) + +static long mulshr24 (long, long); +#pragma aux mulshr24 =\ + "imul edx"\ + "shrd eax, edx, 24"\ + parm nomemory [eax][edx]\ + modify exact [eax edx] + +static long mulshr32 (long, long); +#pragma aux mulshr32 =\ + "imul edx"\ + parm nomemory [eax][edx]\ + modify exact [eax edx]\ + value [edx] + +#elif defined(_MSC_VER) && !defined(NOASM) + +static _inline long mulshr24 (long a, long d) +{ + _asm + { + mov eax, a + imul d + shrd eax, edx, 24 + } +} + +static _inline long mulshr32 (long a, long d) +{ + _asm + { + mov eax, a + imul d + mov eax, edx + } +} + +#elif defined(__GNUC__) && defined(__i386__) && !defined(NOASM) + +#define mulshr24(a,d) \ + ({ long __a=(a), __d=(d); \ + __asm__ __volatile__ ("imull %%edx; shrdl $24, %%edx, %%eax" \ + : "+a" (__a), "+d" (__d) : : "cc"); \ + __a; }) + +#define mulshr32(a,d) \ + ({ long __a=(a), __d=(d); \ + __asm__ __volatile__ ("imull %%edx" \ + : "+a" (__a), "+d" (__d) : : "cc"); \ + __d; }) + +#else + +static inline long mulshr24 (long a, long b) +{ + return((long)((((__int64)a)*((__int64)b))>>24)); +} + +static inline long mulshr32 (long a, long b) +{ + return((long)((((__int64)a)*((__int64)b))>>32)); +} + +#endif + +#define SQRT2 23726566 //(sqrt(2))<<24 +#define C182 31000253 //(cos(PI/8)*2)<<24 +#define C18S22 43840978 //(cos(PI/8)*sqrt(2)*2)<<24 +#define C38S22 18159528 //(cos(PI*3/8)*sqrt(2)*2)<<24 +static long cosqr16[8] = //cosqr16[i] = ((cos(PI*i/16)*sqrt(2))<<24); + {23726566,23270667,21920489,19727919,16777216,13181774,9079764,4628823}; +static long crmul[4096], cbmul[4096]; + +static void initkpeg () +{ + long i, x, y; + + x = 0; //Back & forth diagonal pattern (aligning bytes for best compression) + for(i=0;i<16;i+=2) + { + for(y=7;y>=0;y--) + if ((unsigned)(i-y) < (unsigned)8) unzig[x++] = (y<<3)+i-y; + for(y=0;y<8;y++) + if ((unsigned)(i+1-y) < (unsigned)8) unzig[x++] = (y<<3)+i+1-y; + } + for(i=63;i>=0;i--) dcflagor[i] = (unsigned char)(1<<(unzig[i]>>3)); + + for(i=0;i<128;i++) colclip[i] = i+128; + for(i=128;i<512;i++) colclip[i] = 255; + for(i=512;i<896;i++) colclip[i] = 0; + for(i=896;i<1024;i++) colclip[i] = i-896; + for(i=0;i<1024;i++) + { + colclipup8[i] = (colclip[i]<<8); + colclipup16[i] = (colclip[i]<<16)+0xff000000; //Hack: set alphas to 255 + } + + for(i=0;i<2048;i++) + { + crmul[(i<<1)+0] = (i-1024)*1470104; //1.402*1048576 + crmul[(i<<1)+1] = (i-1024)*-748830; //-0.71414*1048576 + cbmul[(i<<1)+0] = (i-1024)*-360857; //-0.34414*1048576 + cbmul[(i<<1)+1] = (i-1024)*1858077; //1.772*1048576 + } + + memset((void *)&dct[16][0],0,64*2*sizeof(dct[0][0])); + memset((void *)&dct[18][0],0xa0,64*sizeof(dct[0][0])); + + for(i=31;i>=0;i--) pow2mask[i] = (1<>= 1; v <<= 1; + if (num&pow2) v++; + } + *dabits = 16; + *daval = 0; +} + +static long kpegrend (const char *kfilebuf, long kfilength, + long daframeplace, long dabytesperline, long daxres, long dayres, + long daglobxoffs, long daglobyoffs) +{ + long i, j, p, v, leng, xdim, ydim, index, prec, restartinterval; + long restartcnt, num, curbits, x, y, z, dctcnt, c, cc, daval, dabits; + long xx, yy, zz, xxx, yyy, r, g, b, t0, t1, t2, t3, t4, t5, t6, t7; + long yv, cr, cb, *dc, *dc2, xxxend, yyyend; + long *hqval, *hqbits, hqcnt, *quanptr; + unsigned char ch, marker, numbits, lnumcomponents, dcflag; + const unsigned char *kfileptr; + + if (!kpeginited) { kpeginited = 1; initkpeg(); } + + kfileptr = (unsigned char *)kfilebuf; + + if (*(unsigned short *)kfileptr == 0xd8ff) kfileptr += 2; + else return(-1); //"%s is not a JPEG file\n",filename + + restartinterval = 0; + for(i=0;i<4;i++) lastdc[i] = 0; + for(i=0;i<8;i++) hufcnt[i] = 0; + + coltype = 0; bitdepth = 8; //For PNGOUT + do + { + ch = *kfileptr++; if (ch != 255) continue; + marker = *kfileptr++; + leng = ((long)kfileptr[0]<<8)+(long)kfileptr[1]-2; + kfileptr += 2; + switch(marker) + { + case 0xc0: case 0xc1: case 0xc2: + //processit! + numbits = *kfileptr++; + + ydim = ((long)kfileptr[0]<<8)+(long)kfileptr[1]; + xdim = ((long)kfileptr[2]<<8)+(long)kfileptr[3]; + //printf("%s: %ld / %ld = %ld\n",filename,xdim*ydim*3,kfilength,(xdim*ydim*3)/kfilength); + + frameplace = daframeplace; + bytesperline = dabytesperline; + xres = daxres; + yres = dayres; + globxoffs = daglobxoffs; + globyoffs = daglobyoffs; + + gnumcomponents = kfileptr[4]; + kfileptr += 5; + for(z=0;z>4); + gcompvsamp[z] = (kfileptr[1]&15); + gcompquantab[z] = kfileptr[2]; + kfileptr += 3; + } + break; + case 0xc4: //Huffman table + do + { + ch = *kfileptr++; leng--; + if (ch >= 16) { index = ch-12; } + else { index = ch; } + memcpy((void *)&hufnumatbit[index][1],(void *)kfileptr,16); kfileptr += 16; + leng -= 16; + + v = 0; hufcnt[index] = 0; + hufquickcnt[index] = 0; c = 0; + for(b=1;b<=16;b++) + { + hufmaxatbit[index][b] = v+hufnumatbit[index][b]; + hufvalatbit[index][b] = hufcnt[index]-v; + memcpy((void *)&huftable[index][hufcnt[index]],(void *)kfileptr,(long)hufnumatbit[index][b]); + if (b <= 10) + for(c=0;c0;j--) + { + hufquickval[index][hufquickcnt[index]] = huftable[index][hufcnt[index]+c]; + hufquickbits[index][hufquickcnt[index]] = b; + hufquickcnt[index]++; + } + kfileptr += hufnumatbit[index][b]; + leng -= hufnumatbit[index][b]; + hufcnt[index] += hufnumatbit[index][b]; + v = ((v+hufnumatbit[index][b])<<1); + } + + } while (leng > 0); + break; + case 0xdb: + do + { + ch = *kfileptr++; leng--; + index = (ch&15); + prec = (ch>>4); + for(z=0;z<64;z++) + { + v = (long)(*kfileptr++); + if (prec) v = (v<<8)+((long)(*kfileptr++)); + v <<= 19; + if (unzig[z]&7) v = mulshr24(v,cosqr16[unzig[z]&7]); + if (unzig[z]>>3) v = mulshr24(v,cosqr16[unzig[z]>>3]); + if (index) v >>= 6; + quantab[index][unzig[z]] = v; + } + leng -= 64; + if (prec) leng -= 64; + } while (leng > 0); + break; + case 0xdd: + restartinterval = (((long)kfileptr[0])<<8)+((long)kfileptr[1]); + kfileptr += leng; + break; + case 0xda: case 0xd9: + if ((xdim <= 0) || (ydim <= 0)) return(-1); + + lnumcomponents = *kfileptr++; + if (lnumcomponents > 1) coltype = 2; + for(z=0;z>4); + lcompac[z] = (kfileptr[1]&15); + kfileptr += 2; + for(zz=0;zz>4); + //Al = (kfileptr[2]&15); + kfileptr += 3; + + if ((hufcnt[0] == 0) || (hufcnt[4] == 0)) return(-1); + + clipxdim = min(xdim+globxoffs,xres); + clipydim = min(ydim+globyoffs,yres); + + xx = max(globxoffs,0); xxx = min(globxoffs+xdim,xres); + yy = max(globyoffs,0); yyy = min(globyoffs+ydim,yres); + if ((xx >= xres) || (yy >= yres) || (xxx <= 0) || (yyy <= 0)) return(0); + + restartcnt = restartinterval; marker = 0xd0; + num = 0; curbits = 0; x = 0; y = 0; + while (1) + { + if (kfileptr-(unsigned char *)kfilebuf >= kfilength) + lnumcomponents = 0; //rest of file is missing! + + dctcnt = 0; + for(c=0;c0;cc--) + { + dc = &dct[dctcnt][0]; + + //Get DC + while (curbits < 16) //Getbits + { + ch = *kfileptr++; if (ch == 255) kfileptr++; + num = (num<<8)+((long)ch); curbits += 8; + } + + i = ((num>>(curbits-10))&1023); + if (i < hufquickcnt[lcompdc[c]]) + { dabits = hufquickbits[lcompdc[c]][i]; daval = hufquickval[lcompdc[c]][i]; } + else + huffgetval(lcompdc[c],curbits,num,&daval,&dabits); + + curbits -= dabits; + if (daval) + { + while (curbits < 16) //Getbits + { + ch = *kfileptr++; if (ch == 255) kfileptr++; + num = (num<<8)+((long)ch); curbits += 8; + } + + v = ((unsigned)num >> (curbits-daval)) & pow2mask[daval]; + if (v <= pow2mask[daval-1]) v -= pow2mask[daval]; + lastdc[c] += v; + curbits -= daval; + } + dc[0] = lastdc[c]*quanptr[0]; + + //Get AC + memset((void *)&dc[1],0,63*4); + dcflag = 1; + for(z=1;z<64;z++) + { + while (curbits < 16) //Getbits + { + ch = *kfileptr++; if (ch == 255) kfileptr++; + num = (num<<8)+((long)ch); curbits += 8; + } + + i = ((num>>(curbits-10))&1023); + if (i < hqcnt) + { daval = hqval[i]; curbits -= hqbits[i]; } + else + { + huffgetval(lcompac[c]+4,curbits,num,&daval,&dabits); + curbits -= dabits; + } + + if (!daval) break; + z += (daval>>4); if (z >= 64) break; + daval &= 15; + + while (curbits < 16) //Getbits + { + ch = *kfileptr++; if (ch == 255) kfileptr++; + num = (num<<8)+((long)ch); curbits += 8; + } + + v = ((unsigned)num >> (curbits-daval)) & pow2mask[daval]; + if (v <= pow2mask[daval-1]) v -= pow2mask[daval]; + dcflag |= dcflagor[z]; + dc[unzig[z]] = v*quanptr[unzig[z]]; + curbits -= daval; + } + + for(z=0;z<8;z++,dc+=8) + { + if (!(dcflag&pow2char[z])) continue; + t3 = dc[2] + dc[6]; + t2 = (mulshr32(dc[2]-dc[6],SQRT2<<6)<<2) - t3; + t4 = dc[0] + dc[4]; t5 = dc[0] - dc[4]; + t0 = t4+t3; t3 = t4-t3; t1 = t5+t2; t2 = t5-t2; + t4 = (mulshr32(dc[5]-dc[3]+dc[1]-dc[7],C182<<6)<<2); + t7 = dc[1] + dc[7] + dc[5] + dc[3]; + t6 = (mulshr32(dc[3]-dc[5],C18S22<<5)<<3) + t4 - t7; + t5 = (mulshr32(dc[1]+dc[7]-dc[5]-dc[3],SQRT2<<6)<<2) - t6; + t4 = (mulshr32(dc[1]-dc[7],C38S22<<6)<<2) - t4 + t5; + dc[0] = t0+t7; dc[7] = t0-t7; dc[1] = t1+t6; dc[6] = t1-t6; + dc[2] = t2+t5; dc[5] = t2-t5; dc[4] = t3+t4; dc[3] = t3-t4; + } + dc = &dct[dctcnt][0]; + for(z=7;z>=0;z--,dc++) + { + t3 = dc[2<<3] + dc[6<<3]; + t2 = (mulshr32(dc[2<<3]-dc[6<<3],SQRT2<<6)<<2) - t3; + t4 = dc[0<<3] + dc[4<<3]; t5 = dc[0<<3] - dc[4<<3]; + t0 = t4+t3; t3 = t4-t3; t1 = t5+t2; t2 = t5-t2; + t4 = (mulshr32(dc[5<<3]-dc[3<<3]+dc[1<<3]-dc[7<<3],C182<<6)<<2); + t7 = dc[1<<3] + dc[7<<3] + dc[5<<3] + dc[3<<3]; + t6 = (mulshr32(dc[3<<3]-dc[5<<3],C18S22<<5)<<3) + t4 - t7; + t5 = (mulshr32(dc[1<<3]+dc[7<<3]-dc[5<<3]-dc[3<<3],SQRT2<<6)<<2) - t6; + t4 = (mulshr32(dc[1<<3]-dc[7<<3],C38S22<<6)<<2) - t4 + t5; + dc[0<<3] = t0+t7; dc[7<<3] = t0-t7; dc[1<<3] = t1+t6; dc[6<<3] = t1-t6; + dc[2<<3] = t2+t5; dc[5<<3] = t2-t5; dc[4<<3] = t3+t4; dc[3<<3] = t3-t4; + } + + dctcnt++; + } + } + + dctcnt = 0; dc = &dct[18][0]; dc2 = &dct[16][0]; + r = g = b = 0; cc = 0; + for(yy=0;yy<(lcompvsamp[0]<<3);yy+=8) + for(xx=0;xx<(lcomphsamp[0]<<3);xx+=8,dctcnt++) + { + yyy = y+yy+globyoffs; if ((unsigned)yyy >= (unsigned)clipydim) continue; + xxx = x+xx+globxoffs; if ((unsigned)xxx >= (unsigned)clipxdim) continue; + p = yyy*bytesperline + xxx*4 + frameplace; + if (lnumcomponents > 0) dc = &dct[dctcnt][0]; + if (lnumcomponents > 1) dc2 = &dct[lcomphvsamp[0]][((yy>>lcompvsampshift[0])<<3)+(xx>>lcomphsampshift[0])]; + xxxend = min(clipxdim-(x+xx+globxoffs),8); + yyyend = min(clipydim-(y+yy+globyoffs),8); + if ((lcomphsamp[0] == 1) && (xxxend == 8)) + { + for(yyy=0;yyy>13)&~1; + cb = (dc2[xxx]>>13)&~1; + ((long *)p)[xxx] = colclipup16[(unsigned)(yv+crmul[cr+2048])>>22]+ + colclipup8[(unsigned)(yv+crmul[cr+2049]+cbmul[cb+2048])>>22]+ + colclip[(unsigned)(yv+cbmul[cb+2049])>>22]; + } + p += bytesperline; + dc += 8; + if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; + } + } + else if ((lcomphsamp[0] == 2) && (xxxend == 8)) + { + for(yyy=0;yyy>1)+64]>>13)&~1; + cb = (dc2[(xxx>>1)]>>13)&~1; + i = crmul[cr+2049]+cbmul[cb+2048]; + cr = crmul[cr+2048]; + cb = cbmul[cb+2049]; + ((long *)p)[xxx] = colclipup16[(unsigned)(yv+cr)>>22]+ + colclipup8[(unsigned)(yv+i)>>22]+ + colclip[(unsigned)(yv+cb)>>22]; + yv = dc[xxx+1]; + ((long *)p)[xxx+1] = colclipup16[(unsigned)(yv+cr)>>22]+ + colclipup8[(unsigned)(yv+i)>>22]+ + colclip[(unsigned)(yv+cb)>>22]; + } + p += bytesperline; + dc += 8; + if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; + } + } + else + { + for(yyy=0;yyy>13)&~1; + cb = (dc2[i]>>13)&~1; + i++; + } + ((long *)p)[xxx] = colclipup16[(unsigned)(yv+crmul[cr+2048])>>22]+ + colclipup8[(unsigned)(yv+crmul[cr+2049]+cbmul[cb+2048])>>22]+ + colclip[(unsigned)(yv+cbmul[cb+2049])>>22]; + } + p += bytesperline; + dc += 8; + if (!((yyy+1)&(lcompvsamp[0]-1))) dc2 += 8; + } + } + } + + if (lnumcomponents) //do only when not EOF... + { + restartcnt--; + if (!restartcnt) + { + kfileptr += 1-(curbits>>3); curbits = 0; + if ((kfileptr[-2] != 255) || (kfileptr[-1] != marker)) kfileptr--; + marker++; if (marker >= 0xd8) marker = 0xd0; + restartcnt = restartinterval; + for(i=0;i<4;i++) lastdc[i] = 0; + } + } + + x += (lcomphsamp[0]<<3); + if (x >= xdim) { x = 0; y += (lcompvsamp[0]<<3); if (y >= ydim) return(0); } + } + default: + kfileptr += leng; + break; + } + } while (kfileptr-(unsigned char *)kfilebuf < kfilength); + return(0); +} + +//============================== KPEGILIB ends ============================== +//================================ GIF begins ================================ + +static char suffix[4100], filbuffer[768], tempstack[4096]; +static long prefix[4100]; + +static long kgifrend (const char *kfilebuf, long kfilelength, + long daframeplace, long dabytesperline, long daxres, long dayres, + long daglobxoffs, long daglobyoffs) +{ + long i, x, y, xsiz, ysiz, yinc, xend, xspan, yspan, currstr, numbitgoal; + long lzcols, dat, blocklen, bitcnt, xoff, yoff, transcol, backcol, *lptr; + char numbits, startnumbits, chunkind, ilacefirst; + const char *ptr, *cptr; + + coltype = 3; bitdepth = 8; //For PNGOUT + + if ((kfilebuf[0] != 'G') || (kfilebuf[1] != 'I') || + (kfilebuf[2] != 'F') || (kfilebuf[12])) return(-1); + paleng = (1<<((kfilebuf[10]&7)+1)); + ptr = &kfilebuf[13]; + if (kfilebuf[10]&128) { cptr = ptr; ptr += paleng*3; } + transcol = -1; + while ((chunkind = *ptr++) == '!') + { //! 0xf9 leng flags ?? ?? transcol + if (ptr[0] == 0xf9) { if (ptr[2]&1) transcol = (long)(((unsigned char)ptr[5])); } + ptr++; + do { i = *ptr++; ptr += i; } while (i); + } + if (chunkind != ',') return(-1); + + xoff = *(unsigned short *)&ptr[0]; yoff = *(unsigned short *)&ptr[2]; + xspan = *(unsigned short *)&ptr[4]; yspan = *(unsigned short *)&ptr[6]; ptr += 9; + if (ptr[-1]&64) { yinc = 8; ilacefirst = 1; } + else { yinc = 1; ilacefirst = 0; } + if (ptr[-1]&128) + { + paleng = (1<<((ptr[-1]&7)+1)); + cptr = ptr; ptr += paleng*3; + } + + for(i=0;i= 0) palcol[transcol] &= (~0xff000000); + + //Handle GIF files with different logical&image sizes or non-0 offsets (added 05/15/2004) + xsiz = *(unsigned short *)&kfilebuf[6]; ysiz = *(unsigned short *)&kfilebuf[8]; + if ((xoff != 0) || (yoff != 0) || (xsiz != xspan) || (ysiz != yspan)) + { + long xx[4], yy[4]; + if (kfilebuf[10]&128) backcol = palcol[kfilebuf[11]]; else backcol = 0; + + //Fill border to backcol + xx[0] = max(daglobxoffs , 0); yy[0] = max(daglobyoffs , 0); + xx[1] = min(daglobxoffs+xoff ,daxres); yy[1] = min(daglobyoffs+yoff ,dayres); + xx[2] = max(daglobxoffs+xoff+xspan, 0); yy[2] = min(daglobyoffs+yoff+yspan,dayres); + xx[3] = min(daglobxoffs+xsiz ,daxres); yy[3] = min(daglobyoffs+ysiz ,dayres); + + lptr = (long *)(yy[0]*dabytesperline+daframeplace); + for(y=yy[0];y=0;i--) { suffix[i] = (char)(prefix[i] = i); } + currstr = lzcols+2; numbits = startnumbits; numbitgoal = (lzcols<<1); + blocklen = *ptr++; + memcpy(filbuffer,ptr,blocklen); ptr += blocklen; + bitcnt = 0; + while (1) + { + dat = ((*(long *)&filbuffer[bitcnt>>3])>>(bitcnt&7)) & (numbitgoal-1); + bitcnt += numbits; + if ((bitcnt>>3) > blocklen-3) + { + *(short *)filbuffer = *(short *)&filbuffer[bitcnt>>3]; + i = blocklen-(bitcnt>>3); + blocklen = (long)*ptr++; + memcpy(&filbuffer[i],ptr,blocklen); ptr += blocklen; + bitcnt &= 7; blocklen += i; + } + if (dat == lzcols) + { + currstr = lzcols+2; numbits = startnumbits; numbitgoal = (lzcols<<1); + continue; + } + if ((currstr == numbitgoal) && (numbits < 12)) + { numbits++; numbitgoal <<= 1; } + + prefix[currstr] = dat; + for(i=0;dat>=lzcols;dat=prefix[dat]) tempstack[i++] = suffix[dat]; + tempstack[i] = (char)prefix[dat]; + suffix[currstr-1] = suffix[currstr] = (char)dat; + + for(;i>=0;i--) + { + if ((unsigned long)x < (unsigned long)daxres) + *(long *)(yoff+(x<<2)) = palcol[(long)tempstack[i]]; + x++; + if (x == xend) + { + y += yinc; + if (y >= yspan) + switch(yinc) + { + case 8: if (!ilacefirst) { y = daglobyoffs+2; yinc = 4; break; } + ilacefirst = 0; y = daglobyoffs+4; yinc = 8; break; + case 4: y = daglobyoffs+1; yinc = 2; break; + case 2: case 1: return(0); + } + if ((unsigned long)y < (unsigned long)dayres) + { yoff = y*dabytesperline+daframeplace; x = daglobxoffs; xend = xspan; } + else + { x = daglobxoffs+0x80000000; xend = xspan+0x80000000; } + } + } + currstr++; + } +} + +//=============================== GIF ends ================================== +//============================== CEL begins ================================= + + // //old .CEL format: + //short id = 0x9119, xdim, ydim, xoff, yoff, id = 0x0008; + //long imagebytes, filler[4]; + //char pal6bit[256][3], image[ydim][xdim]; +static long kcelrend (const char *buf, long fleng, + long daframeplace, long dabytesperline, long daxres, long dayres, + long daglobxoffs, long daglobyoffs) +{ + long i, x, y, x0, x1, y0, y1, xsiz, ysiz; + const char *cptr; + + if ((buf[0] != 0x19) || (buf[1] != 0x91) || + (buf[10] != 8) || (buf[11] != 0)) return(-1); + + coltype = 3; bitdepth = 8; paleng = 256; //For PNGOUT + + xsiz = (long)(*(unsigned short *)&buf[2]); if (xsiz <= 0) return(-1); + ysiz = (long)(*(unsigned short *)&buf[4]); if (ysiz <= 0) return(-1); + + cptr = &buf[32]; + for(i=0;i<256;i++) + { + palcol[i] = (((long)cptr[0])<<18) + + (((long)cptr[1])<<10) + + (((long)cptr[2])<< 2) + 0xff000000; + cptr += 3; + } + + x0 = daglobyoffs; x1 = xsiz+daglobyoffs; + y0 = daglobyoffs; y1 = ysiz+daglobyoffs; + for(y=y0;y= 12) || (!((1< 32)) return(-1); + if (header[17]&0xc0) return(-1); + + fptr = &header[header[0]+18]; + xsiz = (long)(*(unsigned short *)&header[12]); if (xsiz <= 0) return(-1); + ysiz = (long)(*(unsigned short *)&header[14]); if (ysiz <= 0) return(-1); + colbyte = ((((long)header[16])+7)>>3); + + if (header[1] == 1) + { + pixbyte = ((((long)header[7])+7)>>3); + cptr = &fptr[-(*(unsigned short *)&header[3])*pixbyte]; + fptr += (*(unsigned short *)&header[5])*pixbyte; + } else pixbyte = colbyte; + + switch(pixbyte) //For PNGOUT + { + case 1: coltype = 0; bitdepth = 8; break; + case 2: case 3: coltype = 2; break; + case 4: coltype = 6; break; + } + + if (!(header[17]&16)) { x0 = 0; x1 = xsiz; xi = 1; } else { x0 = xsiz-1; x1 = -1; xi = -1; } + if (header[17]&32) { y0 = 0; y1 = ysiz; yi = 1; } else { y0 = ysiz-1; y1 = -1; yi = -1; } + x0 += daglobxoffs; y0 += daglobyoffs; + x1 += daglobxoffs; y1 += daglobyoffs; + if (header[2] < 8) rlestat = -2; else rlestat = -1; + + for(y=y0;y!=y1;y+=yi) + for(x=x0;x!=x1;x+=xi) + { + if (rlestat < 128) + { + if ((rlestat&127) == 127) { rlestat = fptr[0]; fptr++; } + if (header[1] == 1) + { + if (colbyte == 1) i = fptr[0]; + else i = (long)(*(unsigned short *)&fptr[0]); + nptr = &cptr[i*pixbyte]; + } else nptr = fptr; + + switch(pixbyte) + { + case 1: i = ((((long)nptr[0])*0x10101)|0xff000000); break; + case 2: i = (long)(*(unsigned short *)&nptr[0]); + i = ((i&0x7c00)<<9) + ((i&0x03e0)<<6) + ((i&0x001f)<<3) + 0xff000000; + break; + case 3: i = (*(long *)&nptr[0]) | 0xff000000; break; + case 4: i = (*(long *)&nptr[0]); break; + } + fptr += colbyte; + } + if (rlestat >= 0) rlestat--; + + if (((unsigned long)x < (unsigned long)daxres) && ((unsigned long)y < (unsigned long)dayres)) + *(long *)(y*dabytesperline+x*4+daframeplace) = i; + } + return(0); +} + +//============================== TARGA ends ================================= +//============================== BMP begins ================================= + //TODO: handle BI_RLE8 and BI_RLE4 (compression types 1&2 respectively) + // ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ + // ³ 0(2): "BM" ³ + // ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿³ 10(4): rastoff³ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ + // ³headsiz=12 (OS/2 1.x)³³ 14(4): headsiz³ ³ All new formats: ³ + //ÚÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÁÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÁÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ + //³ 18(2): xsiz ³ 18(4): xsiz ³ + //³ 20(2): ysiz ³ 22(4): ysiz ³ + //³ 22(2): planes (always 1) ³ 26(2): planes (always 1) ³ + //³ 24(2): cdim (1,4,8,24) ³ 28(2): cdim (1,4,8,16,24,32) ³ + //³ if (cdim < 16) ³ 30(4): compression (0,1,2,3!?,4) ³ + //³ 26(rastoff-14-headsiz): pal(bgr) ³ 34(4): (bitmap data size+3)&3 ³ + //³ ³ 46(4): N colors (0=2^cdim) ³ + //³ ³ if (cdim < 16) ³ + //³ ³ 14+headsiz(rastoff-14-headsiz): pal(bgr0) ³ + //ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ + // ³ rastoff(?): bitmap data ³ + // ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ +static long kbmprend (const char *buf, long fleng, + long daframeplace, long dabytesperline, long daxres, long dayres, + long daglobxoffs, long daglobyoffs) +{ + long i, j, x, y, x0, x1, y0, y1, rastoff, headsiz, xsiz, ysiz, cdim, comp, cptrinc, *lptr; + const char *cptr; + + headsiz = *(long *)&buf[14]; + if (headsiz == 12) //OS/2 1.x (old format) + { + if (*(short *)(&buf[22]) != 1) return(-1); + xsiz = (long)(*(unsigned short *)&buf[18]); + ysiz = (long)(*(unsigned short *)&buf[20]); + cdim = (long)(*(unsigned short *)&buf[24]); + comp = 0; + } + else //All newer formats... + { + if (*(short *)(&buf[26]) != 1) return(-1); + xsiz = *(long *)&buf[18]; + ysiz = *(long *)&buf[22]; + cdim = (long)(*(unsigned short *)&buf[28]); + comp = *(long *)&buf[30]; + } + if ((xsiz <= 0) || (!ysiz)) return(-1); + //cdim must be: (1,4,8,16,24,32) + if (((unsigned long)(cdim-1) >= (unsigned long)32) || (!((1<>1); + } + for(palcol[i+3]=0;palcol[i+3]<32;palcol[i+3]++) + { + if (!(j&1)) break; + j = (((unsigned long)j)>>1); + } + } + } + palcol[0] = 24-(palcol[0]+palcol[3]); palcol[3] = ((-1<<(24-palcol[3]))&0x00ff0000); + palcol[1] = 16-(palcol[1]+palcol[4]); palcol[4] = ((-1<<(16-palcol[4]))&0x0000ff00); + palcol[2] = 8-(palcol[2]+palcol[5]); palcol[5] = ((-1<<( 8-palcol[5]))&0x000000ff); + } + + cptrinc = (((xsiz*cdim+31)>>3)&~3); cptr = &buf[rastoff]; + if (ysiz < 0) { ysiz = -ysiz; } else { cptr = &cptr[(ysiz-1)*cptrinc]; cptrinc = -cptrinc; } + + x0 = daglobxoffs; x1 = xsiz+daglobxoffs; + y0 = daglobyoffs; y1 = ysiz+daglobyoffs; + if ((x0 >= daxres) || (x1 <= 0) || (y0 >= dayres) || (y1 <= 0)) return(0); + if (x0 < 0) x0 = 0; + if (x1 > daxres) x1 = daxres; + for(y=y0;y= (unsigned long)dayres) continue; + lptr = (long *)(y*dabytesperline-(daglobyoffs<<2)+daframeplace); + switch(cdim) + { + case 1: for(x=x0;x>3]>>(x&7^7))&1)]; break; + case 4: for(x=x0;x>1]>>((x&1^1)<<2))&15)]; break; + case 8: for(x=x0;x= x1) { x = x0; y++; } + } + } + else if (nplanes == 3) + { + for(;i;i--) //24-bit PCX + { + if (((unsigned long)x < (unsigned long)daxres) && ((unsigned long)y < (unsigned long)dayres)) + *(char *)(y*dabytesperline+x*4+daframeplace+p) = c; + x++; if (x >= x1) { x = x0; p--; if (p < 0) { p = nplanes-1; y++; } } + } + } + } while (y < y1); + return(0); +} + +//=============================== BMP ends ================================== +//=================== External picture interface begins ====================== + +void kpgetdim (const char *buf, long leng, long *xsiz, long *ysiz) +{ + long *lptr; + const unsigned char *cptr; + unsigned char *ubuf = (unsigned char *)buf; + + (*xsiz) = (*ysiz) = 0; if (leng < 16) return; + if ((ubuf[0] == 0x89) && (ubuf[1] == 0x50)) //.PNG + { + lptr = (long *)buf; + if ((lptr[0] != 0x474e5089) || (lptr[1] != 0x0a1a0a0d)) return; + lptr = &lptr[2]; + while (((unsigned long)lptr-(unsigned long)buf) < (unsigned long)(leng-16)) + { + if (lptr[1] == 0x52444849) //IHDR + { (*xsiz) = bswap(lptr[2]); (*ysiz) = bswap(lptr[3]); break; } + lptr = (long *)((long)lptr + bswap(lptr[0]) + 12); + } + } + else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8)) //.JPG + { + cptr = (unsigned char *)&buf[2]; + while (((unsigned long)cptr-(unsigned long)buf) < (unsigned long)(leng-8)) + { + if (cptr[0] != 255) { cptr = &cptr[1]; continue; } + if ((unsigned long)(cptr[1]-0xc0) < 3) + { + (*ysiz) = ((long)cptr[5]<<8)+(long)cptr[6]; + (*xsiz) = ((long)cptr[7]<<8)+(long)cptr[8]; + break; + } + cptr = &cptr[((long)cptr[2]<<8)+(long)cptr[3]+2]; + } + } + else if ((ubuf[0] == 'G') && (ubuf[1] == 'I') && (ubuf[2] == 'F') && (ubuf[12] == 0)) //.GIF + { + (*xsiz) = (long)(*(unsigned short *)&buf[6]); + (*ysiz) = (long)(*(unsigned short *)&buf[8]); + } + else if ((ubuf[0] == 0x19) && (ubuf[1] == 0x91) && (ubuf[10] == 8) && (ubuf[11] == 0)) //old .CEL/.PIC + { + (*xsiz) = (long)(*(unsigned short *)&buf[2]); + (*ysiz) = (long)(*(unsigned short *)&buf[4]); + } + else if ((ubuf[0] == 'B') && (ubuf[1] == 'M')) //.BMP + { + if (*(long *)(&buf[14]) == 12) //OS/2 1.x (old format) + { + if (*(short *)(&buf[22]) != 1) return; + (*xsiz) = (long)(*(unsigned short *)&buf[18]); + (*ysiz) = (long)(*(unsigned short *)&buf[20]); + } + else //All newer formats... + { + if (*(short *)(&buf[26]) != 1) return; + (*xsiz) = *(long *)&buf[18]; + (*ysiz) = *(long *)&buf[22]; + } + } + else if (*(long *)ubuf == 0x0801050a) //.PCX + { + (*xsiz) = (*(short *)&buf[ 8])-(*(short *)&buf[4])+1; + (*ysiz) = (*(short *)&buf[10])-(*(short *)&buf[6])+1; + } + else + { //Unreliable .TGA identification - this MUST be final case! + if ((leng >= 20) && (!(ubuf[1]&0xfe))) + if ((ubuf[2] < 12) && ((1<= 20) && (!(ubuf[1]&0xfe))) + if ((ubuf[2] < 12) && ((1<>1)&~7); + (*daglobyoffs) = ((((*dayres)-(*picysiz))>>1)&~7); + } + return(kprender(skfilebuf,skfilength,*daframeplace,*dabytesperline, + *daxres,*dayres,*daglobxoffs,*daglobyoffs)); +} + +//======================= Legacy emulation code ends ========================= +#ifndef NOKPLIBZIP + //Brute-force case-insensitive, slash-insensitive, * and ? wildcard matcher + //Given: string i and string j. string j can have wildcards + //Returns: 1:matches, 0:doesn't match +static long wildmatch (const char *i, const char *j) +{ + const char *k; + char c0, c1; + + if (!*j) return(1); + do + { + if (*j == '*') + { + for(k=i,j++;*k;k++) if (wildmatch(k,j)) return(1); + continue; + } + if (!*i) return(0); + if (*j == '?') { i++; j++; continue; } + c0 = *i; if ((c0 >= 'a') && (c0 <= 'z')) c0 -= 32; + c1 = *j; if ((c1 >= 'a') && (c1 <= 'z')) c1 -= 32; + if (c0 == '/') c0 = '\\'; + if (c1 == '/') c1 = '\\'; + if (c0 != c1) return(0); + i++; j++; + } while (*j); + return(!*i); +} + + //Same as: stricmp(st0,st1) except: '/' == '\' +static long filnamcmp (const char *st0, const char *st1) +{ + long i; + char ch0, ch1; + + for(i=0;st0[i];i++) + { + ch0 = st0[i]; if ((ch0 >= 'a') && (ch0 <= 'z')) ch0 -= 32; + ch1 = st1[i]; if ((ch1 >= 'a') && (ch1 <= 'z')) ch1 -= 32; + if (ch0 == '/') ch0 = '\\'; + if (ch1 == '/') ch1 = '\\'; + if (ch0 != ch1) return(-1); + } + if (!st1[i]) return(0); + return(-1); +} + +//===================== ZIP decompression code begins ======================== + + //format: (used by kzaddstack/kzopen to cache file name&start info) + //[char zipnam[?]\0] + //[next hashindex/-1][next index/-1][zipnam index][zipseek][char filnam[?]\0] + //[next hashindex/-1][next index/-1][zipnam index][zipseek][char filnam[?]\0] + //... + //[char zipnam[?]\0] + //[next hashindex/-1][next index/-1][zipnam index][zipseek][char filnam[?]\0] + //[next hashindex/-1][next index/-1][zipnam index][zipseek][char filnam[?]\0] + //... +#define KZHASHINITSIZE 8192 +static char *kzhashbuf = 0; +static long kzhashead[256], kzhashpos, kzlastfnam, kzhashsiz; + +static long kzcheckhashsiz (long siz) +{ + long i; + + if (!kzhashbuf) //Initialize hash table on first call + { + memset(kzhashead,-1,sizeof(kzhashead)); + kzhashbuf = (char *)malloc(KZHASHINITSIZE); if (!kzhashbuf) return(0); + kzhashpos = 0; kzlastfnam = -1; kzhashsiz = KZHASHINITSIZE; + } + if (kzhashpos+siz > kzhashsiz) //Make sure string fits in kzhashbuf + { + i = kzhashsiz; do { i <<= 1; } while (kzhashpos+siz > i); + kzhashbuf = (char *)realloc(kzhashbuf,i); if (!kzhashbuf) return(0); + kzhashsiz = i; + } + return(1); +} + +static long kzcalchash (const char *st) +{ + long i, hashind; + char ch; + + for(i=0,hashind=0;st[i];i++) + { + ch = st[i]; + if ((ch >= 'a') && (ch <= 'z')) ch -= 32; + if (ch == '/') ch = '\\'; + hashind = (ch - hashind*3); + } + return(hashind%(sizeof(kzhashead)/sizeof(kzhashead[0]))); +} + +static long kzcheckhash (const char *filnam, char **zipnam, long *zipseek) +{ + long i; + + if (!kzhashbuf) return(0); + if (filnam[0] == '|') filnam++; + for(i=kzhashead[kzcalchash(filnam)];i>=0;i=(*(long *)&kzhashbuf[i])) + if (!filnamcmp(filnam,&kzhashbuf[i+16])) + { + (*zipnam) = &kzhashbuf[*(long *)&kzhashbuf[i+8]]; + (*zipseek) = *(long *)&kzhashbuf[i+12]; + return(1); + } + return(0); +} + +void kzuninit () +{ + if (kzhashbuf) { free(kzhashbuf); kzhashbuf = 0; } + kzhashpos = kzhashsiz = 0; +} + + //Load ZIP directory into memory (hash) to allow fast access later +void kzaddstack (const char *zipnam) +{ + FILE *fil; + long i, j, hashind, zipnamoffs, numfiles; + char tempbuf[260+46]; + + fil = fopen(zipnam,"rb"); if (!fil) return; + + //Write ZIP filename to hash + i = strlen(zipnam)+1; if (!kzcheckhashsiz(i)) return; + strcpy(&kzhashbuf[kzhashpos],zipnam); + zipnamoffs = kzhashpos; kzhashpos += i; + + fseek(fil,-22,SEEK_END); + fread(tempbuf,22,1,fil); + if (*(long *)&tempbuf[0] == 0x06054b50) //Fast way of finding dir info + { + numfiles = *(short *)&tempbuf[10]; + fseek(fil,*(long *)&tempbuf[16],SEEK_SET); + } + else //Slow way of finding dir info (used when ZIP has junk at end) + { + fseek(fil,0,SEEK_SET); numfiles = 0; + while (1) + { + if (!fread(&j,4,1,fil)) { numfiles = -1; break; } + if (j == 0x02014b50) break; //Found central file header :) + if (j != 0x04034b50) { numfiles = -1; break; } + fread(tempbuf,26,1,fil); + fseek(fil,(*(long *)&tempbuf[14]) + (*(short *)&tempbuf[24]) + (*(short *)&tempbuf[22]),SEEK_CUR); + numfiles++; + } + if (numfiles < 0) { fclose(fil); return; } + fseek(fil,-4,SEEK_CUR); + } + for(i=0;i 2GB-32K bytes + slidew = 0x7fffffff; //Force reload at beginning + + return((long)kzfs.fil); + default: kzfs.fil = 0; return(0); + } + } + return(0); +} + +// -------------------------------------------------------------------------- + +static long srchstat = -1, wildstpathleng; + +#ifndef _WIN32 +static char wildst[260] = ""; +static struct find_t findata; +#else +static char wildst[MAX_PATH] = ""; +static HANDLE hfind = INVALID_HANDLE_VALUE; +static WIN32_FIND_DATA findata; +#endif + +void kzfindfilestart (const char *st) +{ +#ifdef _WIN32 + if (hfind != INVALID_HANDLE_VALUE) + { FindClose(hfind); hfind = INVALID_HANDLE_VALUE; } +#endif + strcpy(wildst,st); + srchstat = -3; +} + +long kzfindfile (char *filnam) +{ + long i; + + filnam[0] = 0; + if (srchstat == -3) + { + if (!wildst[0]) { srchstat = -1; return(0); } + do + { + srchstat = -2; + + //Extract directory from wildcard string for pre-pending + wildstpathleng = 0; + for(i=0;wildst[i];i++) + if ((wildst[i] == '/') || (wildst[i] == '\\')) + wildstpathleng = i+1; + + memcpy(filnam,wildst,wildstpathleng); + +#ifndef _WIN32 + if (_dos_findfirst(wildst,_A_SUBDIR,&findata)) + { if (!kzhashbuf) return(0); srchstat = kzlastfnam; continue; } + i = wildstpathleng; + if (findata.attrib&16) + { + if ((findata.name[0] == '.') && (!findata.name[1])) continue; + filnam[i++] = '\\'; + } + strcpy(&filnam[i],findata.name); +#else + hfind = FindFirstFile(wildst,&findata); + if (hfind == INVALID_HANDLE_VALUE) + { if (!kzhashbuf) return(0); srchstat = kzlastfnam; continue; } + if (findata.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) continue; + i = wildstpathleng; + if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) + { + if ((findata.cFileName[0] == '.') && (!findata.cFileName[1])) continue; + filnam[i++] = '\\'; + } + strcpy(&filnam[i],findata.cFileName); +#endif + return(1); + } while (0); + } + if (srchstat == -2) + while (1) + { + memcpy(filnam,wildst,wildstpathleng); +#ifndef _WIN32 + if (_dos_findnext(&findata)) + { if (!kzhashbuf) return(0); srchstat = kzlastfnam; break; } + i = wildstpathleng; + if (findata.attrib&16) + { + if ((findata.name[0] == '.') && (!findata.name[1])) continue; + filnam[i++] = '\\'; + } + strcpy(&filnam[i],findata.name); +#else + if (!FindNextFile(hfind,&findata)) + { FindClose(hfind); if (!kzhashbuf) return(0); srchstat = kzlastfnam; break; } + if (findata.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) continue; + i = wildstpathleng; + if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) + { + if ((findata.cFileName[0] == '.') && (!findata.cFileName[1])) continue; + filnam[i++] = '\\'; + } + strcpy(&filnam[i],findata.cFileName); +#endif + return(1); + } + while (srchstat >= 0) + { + if (wildmatch(&kzhashbuf[srchstat+16],wildst)) + { + //strcpy(filnam,&kzhashbuf[srchstat+16]); + filnam[0] = '|'; strcpy(&filnam[1],&kzhashbuf[srchstat+16]); + srchstat = *(long *)&kzhashbuf[srchstat+4]; + return(1); + } + srchstat = *(long *)&kzhashbuf[srchstat+4]; + } + return(0); +} + +//File searching code (supports inside ZIP files!) How to use this code: +// char filnam[MAX_PATH]; +// kzfindfilestart("vxl/*.vxl"); +// while (kzfindfile(filnam)) puts(filnam); +//NOTES: +// * Directory names begin with '\' +// * Files inside zip begin with '|' + +// -------------------------------------------------------------------------- + +static char *gzbufptr; +static void putbuf4zip (const unsigned char *buf, long uncomp0, long uncomp1) +{ + long i0, i1; + // uncomp0 ... uncomp1 + // &gzbufptr[kzfs.pos] ... &gzbufptr[kzfs.endpos]; + i0 = max(uncomp0,kzfs.pos); + i1 = min(uncomp1,kzfs.endpos); + if (i0 < i1) memcpy(&gzbufptr[i0],&buf[i0-uncomp0],i1-i0); +} + + //returns number of bytes copied +long kzread (void *buffer, long leng) +{ + long i, j, k, bfinal, btype, hlit, hdist; + + if ((!kzfs.fil) || (leng <= 0)) return(0); + + if (kzfs.comptyp == 0) + { + if (kzfs.pos != kzfs.i) //Seek only when position changes + fseek(kzfs.fil,kzfs.seek0+kzfs.pos,SEEK_SET); + i = min(kzfs.leng-kzfs.pos,leng); + fread(buffer,i,1,kzfs.fil); + kzfs.i += i; //kzfs.i is a local copy of ftell(kzfs.fil); + } + else if (kzfs.comptyp == 8) + { + zipfilmode = 1; + + //Initialize for putbuf4zip + gzbufptr = (char *)buffer; gzbufptr = &gzbufptr[-kzfs.pos]; + kzfs.endpos = min(kzfs.pos+leng,kzfs.leng); + if (kzfs.endpos == kzfs.pos) return(0); //Guard against reading 0 length + + if (kzfs.pos < slidew-32768) // Must go back to start :( + { + if (kzfs.comptell) fseek(kzfs.fil,kzfs.seek0,SEEK_SET); + + slidew = 0; slider = 16384; + kzfs.jmpplc = 0; + + //Initialize for suckbits/peekbits/getbits + kzfs.comptell = min(kzfs.compleng,sizeof(olinbuf)); + fread(&olinbuf[0],kzfs.comptell,1,kzfs.fil); + //Make it re-load when there are < 32 bits left in FIFO + bitpos = -(((long)sizeof(olinbuf)-4)<<3); + //Identity: filptr + (bitpos>>3) = &olinbuf[0] + filptr = &olinbuf[-(bitpos>>3)]; + } + else + { + i = max(slidew-32768,0); j = slider-16384; + + //HACK: Don't unzip anything until you have to... + // (keeps file pointer as low as possible) + if (kzfs.endpos <= slidew) j = kzfs.endpos; + + //write uncompoffs on slidebuf from: i to j + if (!((i^j)&32768)) + putbuf4zip(&slidebuf[i&32767],i,j); + else + { + putbuf4zip(&slidebuf[i&32767],i,j&~32767); + putbuf4zip(slidebuf,j&~32767,j); + } + + //HACK: Don't unzip anything until you have to... + // (keeps file pointer as low as possible) + if (kzfs.endpos <= slidew) goto retkzread; + } + + switch (kzfs.jmpplc) + { + case 0: goto kzreadplc0; + case 1: goto kzreadplc1; + case 2: goto kzreadplc2; + case 3: goto kzreadplc3; + } +kzreadplc0:; + do + { + bfinal = getbits(1); btype = getbits(2); + if (btype == 0) + { + //Raw (uncompressed) + suckbits((-bitpos)&7); //Synchronize to start of next byte + i = getbits(16); if ((getbits(16)^i) != 0xffff) { free(skfilebuf); skfilebuf = 0; return(-1); } + for(;i;i--) + { + if (slidew >= slider) + { + putbuf4zip(&slidebuf[(slider-16384)&32767],slider-16384,slider); slider += 16384; + if (slider-16384 >= kzfs.endpos) + { + kzfs.jmpplc = 1; kzfs.i = i; kzfs.bfinal = bfinal; + goto retkzread; +kzreadplc1:; i = kzfs.i; bfinal = kzfs.bfinal; + } + } + slidebuf[(slidew++)&32767] = (char)getbits(8); + } + continue; + } + if (btype == 3) continue; + + if (btype == 1) //Fixed Huffman + { + hlit = 288; hdist = 32; i = 0; + for(;i<144;i++) clen[i] = 8; //Fixed bit sizes (literals) + for(;i<256;i++) clen[i] = 9; //Fixed bit sizes (literals) + for(;i<280;i++) clen[i] = 7; //Fixed bit sizes (EOI,lengths) + for(;i<288;i++) clen[i] = 8; //Fixed bit sizes (lengths) + for(;i<320;i++) clen[i] = 5; //Fixed bit sizes (distances) + } + else //Dynamic Huffman + { + hlit = getbits(5)+257; hdist = getbits(5)+1; j = getbits(4)+4; + for(i=0;i= slider) + { + putbuf4zip(&slidebuf[(slider-16384)&32767],slider-16384,slider); slider += 16384; + if (slider-16384 >= kzfs.endpos) + { + kzfs.jmpplc = 2; kzfs.bfinal = bfinal; goto retkzread; +kzreadplc2:; bfinal = kzfs.bfinal; + } + } + + k = peekbits(LOGQHUFSIZ0); + if (qhufbit0[k]) { i = qhufval0[k]; suckbits((long)qhufbit0[k]); } + else i = hufgetsym(ibuf0,nbuf0); + + if (i < 256) { slidebuf[(slidew++)&32767] = (char)i; continue; } + if (i == 256) break; + i = getbits(hxbit[i+30-257][0]) + hxbit[i+30-257][1]; + + k = peekbits(LOGQHUFSIZ1); + if (qhufbit1[k]) { j = qhufval1[k]; suckbits((long)qhufbit1[k]); } + else j = hufgetsym(ibuf1,nbuf1); + + j = getbits(hxbit[j][0]) + hxbit[j][1]; + for(;i;i--) slidebuf[(slidew++)&32767] = slidebuf[(slidew-j)&32767]; + } + } while (!bfinal); + + slider -= 16384; + if (!((slider^slidew)&32768)) + putbuf4zip(&slidebuf[slider&32767],slider,slidew); + else + { + putbuf4zip(&slidebuf[slider&32767],slider,slidew&~32767); + putbuf4zip(slidebuf,slidew&~32767,slidew); + } +kzreadplc3:; kzfs.jmpplc = 3; + } + +retkzread:; + i = kzfs.pos; + kzfs.pos += leng; if (kzfs.pos > kzfs.leng) kzfs.pos = kzfs.leng; + return(kzfs.pos-i); +} + +long kzfilelength () +{ + if (!kzfs.fil) return(0); + return(kzfs.leng); +} + + //WARNING: kzseek(<-32768,SEEK_CUR); or: + // kzseek(0,SEEK_END); can make next kzread very slow!!! +void kzseek (long offset, long whence) +{ + if (!kzfs.fil) return; + switch (whence) + { + case SEEK_CUR: kzfs.pos += offset; break; + case SEEK_END: kzfs.pos = kzfs.leng+offset; break; + case SEEK_SET: default: kzfs.pos = offset; + } + if (kzfs.pos < 0) kzfs.pos = 0; + if (kzfs.pos > kzfs.leng) kzfs.pos = kzfs.leng; +} + +long kztell () +{ + if (!kzfs.fil) return(-1); + return(kzfs.pos); +} + +long kzgetc () +{ + char ch; + if (!kzread(&ch,1)) return(-1); + return((long)ch); +} + +long kzeof () +{ + if (!kzfs.fil) return(-1); + return(kzfs.pos >= kzfs.leng); +} + +void kzclose () +{ + if (kzfs.fil) { fclose(kzfs.fil); kzfs.fil = 0; } +} + +//====================== ZIP decompression code ends ========================= +//===================== HANDY PICTURE function begins ======================== + +void kpzload (const char *filnam, long *pic, long *bpl, long *xsiz, long *ysiz) +{ + char *buf; + long leng; + + (*pic) = 0; + if (!kzopen(filnam)) return; + leng = kzfilelength(); + buf = (char *)malloc(leng); if (!buf) return; + kzread(buf,leng); + kzclose(); + + kpgetdim(buf,leng,xsiz,ysiz); + (*bpl) = ((*xsiz)<<2); + (*pic) = (long)malloc((*ysiz)*(*bpl)); if (!(*pic)) { free(buf); return; } + kprender(buf,leng,*pic,*bpl,*xsiz,*ysiz,0,0); + free(buf); +} +//====================== HANDY PICTURE function ends ========================= +#endif //NOKPLIBZIP diff --git a/util/cosas utiles/kplib/kplib.h b/util/cosas utiles/kplib/kplib.h new file mode 100644 index 0000000..e173962 --- /dev/null +++ b/util/cosas utiles/kplib/kplib.h @@ -0,0 +1,31 @@ +/* +KPLIB.H: Ken's Picture LIBrary written by Ken Silverman +Copyright (c) 1998-2004 Ken Silverman +Ken Silverman's official web site: http://www.advsys.net/ken +*/ + +#ifndef __KPLIB_H__ +#define __KPLIB_H__ + + //High-level (easy) picture loading function: +extern void kpzload (const char *, long *, long *, long *, long *); + //Low-level PNG/JPG functions: +extern void kpgetdim (const char *, long, long *, long *); +extern long kprender (const char *, long, long, long, long, long, long, long); + + //ZIP functions: +extern void kzaddstack (const char *); +extern void kzuninit (); +extern long kzopen (const char *); +extern long kzread (void *, long); +extern long kzfilelength (); +extern void kzseek (long, long); +extern long kztell (); +extern long kzgetc (); +extern long kzeof (); +extern void kzclose (); + +extern void kzfindfilestart (const char *); //pass wildcard string +extern long kzfindfile (char *); //you alloc buf, returns 1:found,0:~found + +#endif //__KPLIB_H__ diff --git a/util/cosas utiles/kplib/leeme.txt b/util/cosas utiles/kplib/leeme.txt new file mode 100644 index 0000000..66771c6 --- /dev/null +++ b/util/cosas utiles/kplib/leeme.txt @@ -0,0 +1,8 @@ +Ésta es la librería KPLIB, escrita por Ken Silverman (el ke hizo el motor del Duke3D). +Lo he sacado de su librería Voxlap. El .h es un cacho sacado de voxlap5.h +Puede abrir BMP,JPG,PNG,GIF,PCX,TGA,CEL y ZIP (y puede ke algún otro formato). Funciona en Windows y Linux. Está muy bien porke además es pequeña y simple (normalmente necesitaríamos varias librerías para abrir tantos formatos). Además, puede cargar cualquiera de los mencionados formatos directamente desde un archivo ZIP, lo cual permite usarlos como sistema de ficheros virtual (PAK's). Y por cierto, Silverman hizo varias mejoras al formato PNG, por lo que ésta es una de las mejores librerías que hay para cargar PNG. +Creo que no infringimos ninguna licencia incluyendo esto en eDIV. en el .C no pone nada, pero quizás utilice la licencia BUILD, que básicamente dice que le demos crédito al autor y que no lo utilicemos con fines comerciales sin darle su parte X) no sé a vosotros, pero a mí a estas alturas me importan bastante poco las posibles (o mejor dicho, improbables) aplicaciones comerciales de eDIV. + ++ info en la página de Silverman: http://www.advsys.net/ken + +Er_Makina