3dphotoshoot

changeset 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents 8cfe061ddd5f
children 2d48f65da357
files android/Makefile libs/imago/COPYING libs/imago/COPYING.LESSER libs/imago/README.rst libs/imago/conv.c libs/imago/file_jpeg.c libs/imago/file_png.c libs/imago/file_ppm.c libs/imago/file_rgbe.c libs/imago/file_tga.c libs/imago/ftype_module.c libs/imago/ftype_module.h libs/imago/imago2.c libs/imago/imago2.h libs/imago/imago_gl.c libs/imago/modules.c libs/libjpeg/README libs/libjpeg/cderror.h libs/libjpeg/jcapimin.c libs/libjpeg/jcapistd.c libs/libjpeg/jccoefct.c libs/libjpeg/jccolor.c libs/libjpeg/jcdctmgr.c libs/libjpeg/jchuff.c libs/libjpeg/jchuff.h libs/libjpeg/jcinit.c libs/libjpeg/jcmainct.c libs/libjpeg/jcmarker.c libs/libjpeg/jcmaster.c libs/libjpeg/jcomapi.c libs/libjpeg/jconfig.h libs/libjpeg/jcparam.c libs/libjpeg/jcphuff.c libs/libjpeg/jcprepct.c libs/libjpeg/jcsample.c libs/libjpeg/jctrans.c libs/libjpeg/jdapimin.c libs/libjpeg/jdapistd.c libs/libjpeg/jdatadst.c libs/libjpeg/jdatasrc.c libs/libjpeg/jdcoefct.c libs/libjpeg/jdcolor.c libs/libjpeg/jdct.h libs/libjpeg/jddctmgr.c libs/libjpeg/jdhuff.c libs/libjpeg/jdhuff.h libs/libjpeg/jdinput.c libs/libjpeg/jdmainct.c libs/libjpeg/jdmarker.c libs/libjpeg/jdmaster.c libs/libjpeg/jdmerge.c libs/libjpeg/jdphuff.c libs/libjpeg/jdpostct.c libs/libjpeg/jdsample.c libs/libjpeg/jdtrans.c libs/libjpeg/jerror.c libs/libjpeg/jerror.h libs/libjpeg/jfdctflt.c libs/libjpeg/jfdctfst.c libs/libjpeg/jfdctint.c libs/libjpeg/jidctflt.c libs/libjpeg/jidctfst.c libs/libjpeg/jidctint.c libs/libjpeg/jidctred.c libs/libjpeg/jinclude.h libs/libjpeg/jmemmgr.c libs/libjpeg/jmemnobs.c libs/libjpeg/jmemsys.h libs/libjpeg/jmorecfg.h libs/libjpeg/jpegint.h libs/libjpeg/jpeglib.h libs/libjpeg/jquant1.c libs/libjpeg/jquant2.c libs/libjpeg/jutils.c libs/libjpeg/jversion.h libs/libpng/LICENSE libs/libpng/png.c libs/libpng/png.h libs/libpng/pngconf.h libs/libpng/pngerror.c libs/libpng/pnggccrd.c libs/libpng/pngget.c libs/libpng/pngmem.c libs/libpng/pngpread.c libs/libpng/pngread.c libs/libpng/pngrio.c libs/libpng/pngrtran.c libs/libpng/pngrutil.c libs/libpng/pngset.c libs/libpng/pngtrans.c libs/libpng/pngvcrd.c libs/libpng/pngwio.c libs/libpng/pngwrite.c libs/libpng/pngwtran.c libs/libpng/pngwutil.c proj.mk
diffstat 96 files changed, 53743 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/android/Makefile	Sun Jun 07 02:29:10 2015 +0300
     1.2 +++ b/android/Makefile	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -29,7 +29,7 @@
     1.4  CXXFLAGS = -Wall -g $(defs) -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 \
     1.5  		 $(android_inc) -I$(root)/src/android -I$(root)/src/gles $(incpaths)
     1.6  CFLAGS = -std=c99 $(CXXFLAGS)
     1.7 -LDFLAGS = -Wl,--fix-cortex-a8 -Wl,-z,defs $(android_libs) $(libpaths) -lm
     1.8 +LDFLAGS = -Wl,--fix-cortex-a8 -Wl,-z,defs $(android_libs) $(libpaths) $(libs)
     1.9  
    1.10  .PHONY: debug
    1.11  debug: $(apk-debug)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/libs/imago/COPYING	Sun Jun 07 17:25:49 2015 +0300
     2.3 @@ -0,0 +1,674 @@
     2.4 +                    GNU GENERAL PUBLIC LICENSE
     2.5 +                       Version 3, 29 June 2007
     2.6 +
     2.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
     2.8 + Everyone is permitted to copy and distribute verbatim copies
     2.9 + of this license document, but changing it is not allowed.
    2.10 +
    2.11 +                            Preamble
    2.12 +
    2.13 +  The GNU General Public License is a free, copyleft license for
    2.14 +software and other kinds of works.
    2.15 +
    2.16 +  The licenses for most software and other practical works are designed
    2.17 +to take away your freedom to share and change the works.  By contrast,
    2.18 +the GNU General Public License is intended to guarantee your freedom to
    2.19 +share and change all versions of a program--to make sure it remains free
    2.20 +software for all its users.  We, the Free Software Foundation, use the
    2.21 +GNU General Public License for most of our software; it applies also to
    2.22 +any other work released this way by its authors.  You can apply it to
    2.23 +your programs, too.
    2.24 +
    2.25 +  When we speak of free software, we are referring to freedom, not
    2.26 +price.  Our General Public Licenses are designed to make sure that you
    2.27 +have the freedom to distribute copies of free software (and charge for
    2.28 +them if you wish), that you receive source code or can get it if you
    2.29 +want it, that you can change the software or use pieces of it in new
    2.30 +free programs, and that you know you can do these things.
    2.31 +
    2.32 +  To protect your rights, we need to prevent others from denying you
    2.33 +these rights or asking you to surrender the rights.  Therefore, you have
    2.34 +certain responsibilities if you distribute copies of the software, or if
    2.35 +you modify it: responsibilities to respect the freedom of others.
    2.36 +
    2.37 +  For example, if you distribute copies of such a program, whether
    2.38 +gratis or for a fee, you must pass on to the recipients the same
    2.39 +freedoms that you received.  You must make sure that they, too, receive
    2.40 +or can get the source code.  And you must show them these terms so they
    2.41 +know their rights.
    2.42 +
    2.43 +  Developers that use the GNU GPL protect your rights with two steps:
    2.44 +(1) assert copyright on the software, and (2) offer you this License
    2.45 +giving you legal permission to copy, distribute and/or modify it.
    2.46 +
    2.47 +  For the developers' and authors' protection, the GPL clearly explains
    2.48 +that there is no warranty for this free software.  For both users' and
    2.49 +authors' sake, the GPL requires that modified versions be marked as
    2.50 +changed, so that their problems will not be attributed erroneously to
    2.51 +authors of previous versions.
    2.52 +
    2.53 +  Some devices are designed to deny users access to install or run
    2.54 +modified versions of the software inside them, although the manufacturer
    2.55 +can do so.  This is fundamentally incompatible with the aim of
    2.56 +protecting users' freedom to change the software.  The systematic
    2.57 +pattern of such abuse occurs in the area of products for individuals to
    2.58 +use, which is precisely where it is most unacceptable.  Therefore, we
    2.59 +have designed this version of the GPL to prohibit the practice for those
    2.60 +products.  If such problems arise substantially in other domains, we
    2.61 +stand ready to extend this provision to those domains in future versions
    2.62 +of the GPL, as needed to protect the freedom of users.
    2.63 +
    2.64 +  Finally, every program is threatened constantly by software patents.
    2.65 +States should not allow patents to restrict development and use of
    2.66 +software on general-purpose computers, but in those that do, we wish to
    2.67 +avoid the special danger that patents applied to a free program could
    2.68 +make it effectively proprietary.  To prevent this, the GPL assures that
    2.69 +patents cannot be used to render the program non-free.
    2.70 +
    2.71 +  The precise terms and conditions for copying, distribution and
    2.72 +modification follow.
    2.73 +
    2.74 +                       TERMS AND CONDITIONS
    2.75 +
    2.76 +  0. Definitions.
    2.77 +
    2.78 +  "This License" refers to version 3 of the GNU General Public License.
    2.79 +
    2.80 +  "Copyright" also means copyright-like laws that apply to other kinds of
    2.81 +works, such as semiconductor masks.
    2.82 +
    2.83 +  "The Program" refers to any copyrightable work licensed under this
    2.84 +License.  Each licensee is addressed as "you".  "Licensees" and
    2.85 +"recipients" may be individuals or organizations.
    2.86 +
    2.87 +  To "modify" a work means to copy from or adapt all or part of the work
    2.88 +in a fashion requiring copyright permission, other than the making of an
    2.89 +exact copy.  The resulting work is called a "modified version" of the
    2.90 +earlier work or a work "based on" the earlier work.
    2.91 +
    2.92 +  A "covered work" means either the unmodified Program or a work based
    2.93 +on the Program.
    2.94 +
    2.95 +  To "propagate" a work means to do anything with it that, without
    2.96 +permission, would make you directly or secondarily liable for
    2.97 +infringement under applicable copyright law, except executing it on a
    2.98 +computer or modifying a private copy.  Propagation includes copying,
    2.99 +distribution (with or without modification), making available to the
   2.100 +public, and in some countries other activities as well.
   2.101 +
   2.102 +  To "convey" a work means any kind of propagation that enables other
   2.103 +parties to make or receive copies.  Mere interaction with a user through
   2.104 +a computer network, with no transfer of a copy, is not conveying.
   2.105 +
   2.106 +  An interactive user interface displays "Appropriate Legal Notices"
   2.107 +to the extent that it includes a convenient and prominently visible
   2.108 +feature that (1) displays an appropriate copyright notice, and (2)
   2.109 +tells the user that there is no warranty for the work (except to the
   2.110 +extent that warranties are provided), that licensees may convey the
   2.111 +work under this License, and how to view a copy of this License.  If
   2.112 +the interface presents a list of user commands or options, such as a
   2.113 +menu, a prominent item in the list meets this criterion.
   2.114 +
   2.115 +  1. Source Code.
   2.116 +
   2.117 +  The "source code" for a work means the preferred form of the work
   2.118 +for making modifications to it.  "Object code" means any non-source
   2.119 +form of a work.
   2.120 +
   2.121 +  A "Standard Interface" means an interface that either is an official
   2.122 +standard defined by a recognized standards body, or, in the case of
   2.123 +interfaces specified for a particular programming language, one that
   2.124 +is widely used among developers working in that language.
   2.125 +
   2.126 +  The "System Libraries" of an executable work include anything, other
   2.127 +than the work as a whole, that (a) is included in the normal form of
   2.128 +packaging a Major Component, but which is not part of that Major
   2.129 +Component, and (b) serves only to enable use of the work with that
   2.130 +Major Component, or to implement a Standard Interface for which an
   2.131 +implementation is available to the public in source code form.  A
   2.132 +"Major Component", in this context, means a major essential component
   2.133 +(kernel, window system, and so on) of the specific operating system
   2.134 +(if any) on which the executable work runs, or a compiler used to
   2.135 +produce the work, or an object code interpreter used to run it.
   2.136 +
   2.137 +  The "Corresponding Source" for a work in object code form means all
   2.138 +the source code needed to generate, install, and (for an executable
   2.139 +work) run the object code and to modify the work, including scripts to
   2.140 +control those activities.  However, it does not include the work's
   2.141 +System Libraries, or general-purpose tools or generally available free
   2.142 +programs which are used unmodified in performing those activities but
   2.143 +which are not part of the work.  For example, Corresponding Source
   2.144 +includes interface definition files associated with source files for
   2.145 +the work, and the source code for shared libraries and dynamically
   2.146 +linked subprograms that the work is specifically designed to require,
   2.147 +such as by intimate data communication or control flow between those
   2.148 +subprograms and other parts of the work.
   2.149 +
   2.150 +  The Corresponding Source need not include anything that users
   2.151 +can regenerate automatically from other parts of the Corresponding
   2.152 +Source.
   2.153 +
   2.154 +  The Corresponding Source for a work in source code form is that
   2.155 +same work.
   2.156 +
   2.157 +  2. Basic Permissions.
   2.158 +
   2.159 +  All rights granted under this License are granted for the term of
   2.160 +copyright on the Program, and are irrevocable provided the stated
   2.161 +conditions are met.  This License explicitly affirms your unlimited
   2.162 +permission to run the unmodified Program.  The output from running a
   2.163 +covered work is covered by this License only if the output, given its
   2.164 +content, constitutes a covered work.  This License acknowledges your
   2.165 +rights of fair use or other equivalent, as provided by copyright law.
   2.166 +
   2.167 +  You may make, run and propagate covered works that you do not
   2.168 +convey, without conditions so long as your license otherwise remains
   2.169 +in force.  You may convey covered works to others for the sole purpose
   2.170 +of having them make modifications exclusively for you, or provide you
   2.171 +with facilities for running those works, provided that you comply with
   2.172 +the terms of this License in conveying all material for which you do
   2.173 +not control copyright.  Those thus making or running the covered works
   2.174 +for you must do so exclusively on your behalf, under your direction
   2.175 +and control, on terms that prohibit them from making any copies of
   2.176 +your copyrighted material outside their relationship with you.
   2.177 +
   2.178 +  Conveying under any other circumstances is permitted solely under
   2.179 +the conditions stated below.  Sublicensing is not allowed; section 10
   2.180 +makes it unnecessary.
   2.181 +
   2.182 +  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
   2.183 +
   2.184 +  No covered work shall be deemed part of an effective technological
   2.185 +measure under any applicable law fulfilling obligations under article
   2.186 +11 of the WIPO copyright treaty adopted on 20 December 1996, or
   2.187 +similar laws prohibiting or restricting circumvention of such
   2.188 +measures.
   2.189 +
   2.190 +  When you convey a covered work, you waive any legal power to forbid
   2.191 +circumvention of technological measures to the extent such circumvention
   2.192 +is effected by exercising rights under this License with respect to
   2.193 +the covered work, and you disclaim any intention to limit operation or
   2.194 +modification of the work as a means of enforcing, against the work's
   2.195 +users, your or third parties' legal rights to forbid circumvention of
   2.196 +technological measures.
   2.197 +
   2.198 +  4. Conveying Verbatim Copies.
   2.199 +
   2.200 +  You may convey verbatim copies of the Program's source code as you
   2.201 +receive it, in any medium, provided that you conspicuously and
   2.202 +appropriately publish on each copy an appropriate copyright notice;
   2.203 +keep intact all notices stating that this License and any
   2.204 +non-permissive terms added in accord with section 7 apply to the code;
   2.205 +keep intact all notices of the absence of any warranty; and give all
   2.206 +recipients a copy of this License along with the Program.
   2.207 +
   2.208 +  You may charge any price or no price for each copy that you convey,
   2.209 +and you may offer support or warranty protection for a fee.
   2.210 +
   2.211 +  5. Conveying Modified Source Versions.
   2.212 +
   2.213 +  You may convey a work based on the Program, or the modifications to
   2.214 +produce it from the Program, in the form of source code under the
   2.215 +terms of section 4, provided that you also meet all of these conditions:
   2.216 +
   2.217 +    a) The work must carry prominent notices stating that you modified
   2.218 +    it, and giving a relevant date.
   2.219 +
   2.220 +    b) The work must carry prominent notices stating that it is
   2.221 +    released under this License and any conditions added under section
   2.222 +    7.  This requirement modifies the requirement in section 4 to
   2.223 +    "keep intact all notices".
   2.224 +
   2.225 +    c) You must license the entire work, as a whole, under this
   2.226 +    License to anyone who comes into possession of a copy.  This
   2.227 +    License will therefore apply, along with any applicable section 7
   2.228 +    additional terms, to the whole of the work, and all its parts,
   2.229 +    regardless of how they are packaged.  This License gives no
   2.230 +    permission to license the work in any other way, but it does not
   2.231 +    invalidate such permission if you have separately received it.
   2.232 +
   2.233 +    d) If the work has interactive user interfaces, each must display
   2.234 +    Appropriate Legal Notices; however, if the Program has interactive
   2.235 +    interfaces that do not display Appropriate Legal Notices, your
   2.236 +    work need not make them do so.
   2.237 +
   2.238 +  A compilation of a covered work with other separate and independent
   2.239 +works, which are not by their nature extensions of the covered work,
   2.240 +and which are not combined with it such as to form a larger program,
   2.241 +in or on a volume of a storage or distribution medium, is called an
   2.242 +"aggregate" if the compilation and its resulting copyright are not
   2.243 +used to limit the access or legal rights of the compilation's users
   2.244 +beyond what the individual works permit.  Inclusion of a covered work
   2.245 +in an aggregate does not cause this License to apply to the other
   2.246 +parts of the aggregate.
   2.247 +
   2.248 +  6. Conveying Non-Source Forms.
   2.249 +
   2.250 +  You may convey a covered work in object code form under the terms
   2.251 +of sections 4 and 5, provided that you also convey the
   2.252 +machine-readable Corresponding Source under the terms of this License,
   2.253 +in one of these ways:
   2.254 +
   2.255 +    a) Convey the object code in, or embodied in, a physical product
   2.256 +    (including a physical distribution medium), accompanied by the
   2.257 +    Corresponding Source fixed on a durable physical medium
   2.258 +    customarily used for software interchange.
   2.259 +
   2.260 +    b) Convey the object code in, or embodied in, a physical product
   2.261 +    (including a physical distribution medium), accompanied by a
   2.262 +    written offer, valid for at least three years and valid for as
   2.263 +    long as you offer spare parts or customer support for that product
   2.264 +    model, to give anyone who possesses the object code either (1) a
   2.265 +    copy of the Corresponding Source for all the software in the
   2.266 +    product that is covered by this License, on a durable physical
   2.267 +    medium customarily used for software interchange, for a price no
   2.268 +    more than your reasonable cost of physically performing this
   2.269 +    conveying of source, or (2) access to copy the
   2.270 +    Corresponding Source from a network server at no charge.
   2.271 +
   2.272 +    c) Convey individual copies of the object code with a copy of the
   2.273 +    written offer to provide the Corresponding Source.  This
   2.274 +    alternative is allowed only occasionally and noncommercially, and
   2.275 +    only if you received the object code with such an offer, in accord
   2.276 +    with subsection 6b.
   2.277 +
   2.278 +    d) Convey the object code by offering access from a designated
   2.279 +    place (gratis or for a charge), and offer equivalent access to the
   2.280 +    Corresponding Source in the same way through the same place at no
   2.281 +    further charge.  You need not require recipients to copy the
   2.282 +    Corresponding Source along with the object code.  If the place to
   2.283 +    copy the object code is a network server, the Corresponding Source
   2.284 +    may be on a different server (operated by you or a third party)
   2.285 +    that supports equivalent copying facilities, provided you maintain
   2.286 +    clear directions next to the object code saying where to find the
   2.287 +    Corresponding Source.  Regardless of what server hosts the
   2.288 +    Corresponding Source, you remain obligated to ensure that it is
   2.289 +    available for as long as needed to satisfy these requirements.
   2.290 +
   2.291 +    e) Convey the object code using peer-to-peer transmission, provided
   2.292 +    you inform other peers where the object code and Corresponding
   2.293 +    Source of the work are being offered to the general public at no
   2.294 +    charge under subsection 6d.
   2.295 +
   2.296 +  A separable portion of the object code, whose source code is excluded
   2.297 +from the Corresponding Source as a System Library, need not be
   2.298 +included in conveying the object code work.
   2.299 +
   2.300 +  A "User Product" is either (1) a "consumer product", which means any
   2.301 +tangible personal property which is normally used for personal, family,
   2.302 +or household purposes, or (2) anything designed or sold for incorporation
   2.303 +into a dwelling.  In determining whether a product is a consumer product,
   2.304 +doubtful cases shall be resolved in favor of coverage.  For a particular
   2.305 +product received by a particular user, "normally used" refers to a
   2.306 +typical or common use of that class of product, regardless of the status
   2.307 +of the particular user or of the way in which the particular user
   2.308 +actually uses, or expects or is expected to use, the product.  A product
   2.309 +is a consumer product regardless of whether the product has substantial
   2.310 +commercial, industrial or non-consumer uses, unless such uses represent
   2.311 +the only significant mode of use of the product.
   2.312 +
   2.313 +  "Installation Information" for a User Product means any methods,
   2.314 +procedures, authorization keys, or other information required to install
   2.315 +and execute modified versions of a covered work in that User Product from
   2.316 +a modified version of its Corresponding Source.  The information must
   2.317 +suffice to ensure that the continued functioning of the modified object
   2.318 +code is in no case prevented or interfered with solely because
   2.319 +modification has been made.
   2.320 +
   2.321 +  If you convey an object code work under this section in, or with, or
   2.322 +specifically for use in, a User Product, and the conveying occurs as
   2.323 +part of a transaction in which the right of possession and use of the
   2.324 +User Product is transferred to the recipient in perpetuity or for a
   2.325 +fixed term (regardless of how the transaction is characterized), the
   2.326 +Corresponding Source conveyed under this section must be accompanied
   2.327 +by the Installation Information.  But this requirement does not apply
   2.328 +if neither you nor any third party retains the ability to install
   2.329 +modified object code on the User Product (for example, the work has
   2.330 +been installed in ROM).
   2.331 +
   2.332 +  The requirement to provide Installation Information does not include a
   2.333 +requirement to continue to provide support service, warranty, or updates
   2.334 +for a work that has been modified or installed by the recipient, or for
   2.335 +the User Product in which it has been modified or installed.  Access to a
   2.336 +network may be denied when the modification itself materially and
   2.337 +adversely affects the operation of the network or violates the rules and
   2.338 +protocols for communication across the network.
   2.339 +
   2.340 +  Corresponding Source conveyed, and Installation Information provided,
   2.341 +in accord with this section must be in a format that is publicly
   2.342 +documented (and with an implementation available to the public in
   2.343 +source code form), and must require no special password or key for
   2.344 +unpacking, reading or copying.
   2.345 +
   2.346 +  7. Additional Terms.
   2.347 +
   2.348 +  "Additional permissions" are terms that supplement the terms of this
   2.349 +License by making exceptions from one or more of its conditions.
   2.350 +Additional permissions that are applicable to the entire Program shall
   2.351 +be treated as though they were included in this License, to the extent
   2.352 +that they are valid under applicable law.  If additional permissions
   2.353 +apply only to part of the Program, that part may be used separately
   2.354 +under those permissions, but the entire Program remains governed by
   2.355 +this License without regard to the additional permissions.
   2.356 +
   2.357 +  When you convey a copy of a covered work, you may at your option
   2.358 +remove any additional permissions from that copy, or from any part of
   2.359 +it.  (Additional permissions may be written to require their own
   2.360 +removal in certain cases when you modify the work.)  You may place
   2.361 +additional permissions on material, added by you to a covered work,
   2.362 +for which you have or can give appropriate copyright permission.
   2.363 +
   2.364 +  Notwithstanding any other provision of this License, for material you
   2.365 +add to a covered work, you may (if authorized by the copyright holders of
   2.366 +that material) supplement the terms of this License with terms:
   2.367 +
   2.368 +    a) Disclaiming warranty or limiting liability differently from the
   2.369 +    terms of sections 15 and 16 of this License; or
   2.370 +
   2.371 +    b) Requiring preservation of specified reasonable legal notices or
   2.372 +    author attributions in that material or in the Appropriate Legal
   2.373 +    Notices displayed by works containing it; or
   2.374 +
   2.375 +    c) Prohibiting misrepresentation of the origin of that material, or
   2.376 +    requiring that modified versions of such material be marked in
   2.377 +    reasonable ways as different from the original version; or
   2.378 +
   2.379 +    d) Limiting the use for publicity purposes of names of licensors or
   2.380 +    authors of the material; or
   2.381 +
   2.382 +    e) Declining to grant rights under trademark law for use of some
   2.383 +    trade names, trademarks, or service marks; or
   2.384 +
   2.385 +    f) Requiring indemnification of licensors and authors of that
   2.386 +    material by anyone who conveys the material (or modified versions of
   2.387 +    it) with contractual assumptions of liability to the recipient, for
   2.388 +    any liability that these contractual assumptions directly impose on
   2.389 +    those licensors and authors.
   2.390 +
   2.391 +  All other non-permissive additional terms are considered "further
   2.392 +restrictions" within the meaning of section 10.  If the Program as you
   2.393 +received it, or any part of it, contains a notice stating that it is
   2.394 +governed by this License along with a term that is a further
   2.395 +restriction, you may remove that term.  If a license document contains
   2.396 +a further restriction but permits relicensing or conveying under this
   2.397 +License, you may add to a covered work material governed by the terms
   2.398 +of that license document, provided that the further restriction does
   2.399 +not survive such relicensing or conveying.
   2.400 +
   2.401 +  If you add terms to a covered work in accord with this section, you
   2.402 +must place, in the relevant source files, a statement of the
   2.403 +additional terms that apply to those files, or a notice indicating
   2.404 +where to find the applicable terms.
   2.405 +
   2.406 +  Additional terms, permissive or non-permissive, may be stated in the
   2.407 +form of a separately written license, or stated as exceptions;
   2.408 +the above requirements apply either way.
   2.409 +
   2.410 +  8. Termination.
   2.411 +
   2.412 +  You may not propagate or modify a covered work except as expressly
   2.413 +provided under this License.  Any attempt otherwise to propagate or
   2.414 +modify it is void, and will automatically terminate your rights under
   2.415 +this License (including any patent licenses granted under the third
   2.416 +paragraph of section 11).
   2.417 +
   2.418 +  However, if you cease all violation of this License, then your
   2.419 +license from a particular copyright holder is reinstated (a)
   2.420 +provisionally, unless and until the copyright holder explicitly and
   2.421 +finally terminates your license, and (b) permanently, if the copyright
   2.422 +holder fails to notify you of the violation by some reasonable means
   2.423 +prior to 60 days after the cessation.
   2.424 +
   2.425 +  Moreover, your license from a particular copyright holder is
   2.426 +reinstated permanently if the copyright holder notifies you of the
   2.427 +violation by some reasonable means, this is the first time you have
   2.428 +received notice of violation of this License (for any work) from that
   2.429 +copyright holder, and you cure the violation prior to 30 days after
   2.430 +your receipt of the notice.
   2.431 +
   2.432 +  Termination of your rights under this section does not terminate the
   2.433 +licenses of parties who have received copies or rights from you under
   2.434 +this License.  If your rights have been terminated and not permanently
   2.435 +reinstated, you do not qualify to receive new licenses for the same
   2.436 +material under section 10.
   2.437 +
   2.438 +  9. Acceptance Not Required for Having Copies.
   2.439 +
   2.440 +  You are not required to accept this License in order to receive or
   2.441 +run a copy of the Program.  Ancillary propagation of a covered work
   2.442 +occurring solely as a consequence of using peer-to-peer transmission
   2.443 +to receive a copy likewise does not require acceptance.  However,
   2.444 +nothing other than this License grants you permission to propagate or
   2.445 +modify any covered work.  These actions infringe copyright if you do
   2.446 +not accept this License.  Therefore, by modifying or propagating a
   2.447 +covered work, you indicate your acceptance of this License to do so.
   2.448 +
   2.449 +  10. Automatic Licensing of Downstream Recipients.
   2.450 +
   2.451 +  Each time you convey a covered work, the recipient automatically
   2.452 +receives a license from the original licensors, to run, modify and
   2.453 +propagate that work, subject to this License.  You are not responsible
   2.454 +for enforcing compliance by third parties with this License.
   2.455 +
   2.456 +  An "entity transaction" is a transaction transferring control of an
   2.457 +organization, or substantially all assets of one, or subdividing an
   2.458 +organization, or merging organizations.  If propagation of a covered
   2.459 +work results from an entity transaction, each party to that
   2.460 +transaction who receives a copy of the work also receives whatever
   2.461 +licenses to the work the party's predecessor in interest had or could
   2.462 +give under the previous paragraph, plus a right to possession of the
   2.463 +Corresponding Source of the work from the predecessor in interest, if
   2.464 +the predecessor has it or can get it with reasonable efforts.
   2.465 +
   2.466 +  You may not impose any further restrictions on the exercise of the
   2.467 +rights granted or affirmed under this License.  For example, you may
   2.468 +not impose a license fee, royalty, or other charge for exercise of
   2.469 +rights granted under this License, and you may not initiate litigation
   2.470 +(including a cross-claim or counterclaim in a lawsuit) alleging that
   2.471 +any patent claim is infringed by making, using, selling, offering for
   2.472 +sale, or importing the Program or any portion of it.
   2.473 +
   2.474 +  11. Patents.
   2.475 +
   2.476 +  A "contributor" is a copyright holder who authorizes use under this
   2.477 +License of the Program or a work on which the Program is based.  The
   2.478 +work thus licensed is called the contributor's "contributor version".
   2.479 +
   2.480 +  A contributor's "essential patent claims" are all patent claims
   2.481 +owned or controlled by the contributor, whether already acquired or
   2.482 +hereafter acquired, that would be infringed by some manner, permitted
   2.483 +by this License, of making, using, or selling its contributor version,
   2.484 +but do not include claims that would be infringed only as a
   2.485 +consequence of further modification of the contributor version.  For
   2.486 +purposes of this definition, "control" includes the right to grant
   2.487 +patent sublicenses in a manner consistent with the requirements of
   2.488 +this License.
   2.489 +
   2.490 +  Each contributor grants you a non-exclusive, worldwide, royalty-free
   2.491 +patent license under the contributor's essential patent claims, to
   2.492 +make, use, sell, offer for sale, import and otherwise run, modify and
   2.493 +propagate the contents of its contributor version.
   2.494 +
   2.495 +  In the following three paragraphs, a "patent license" is any express
   2.496 +agreement or commitment, however denominated, not to enforce a patent
   2.497 +(such as an express permission to practice a patent or covenant not to
   2.498 +sue for patent infringement).  To "grant" such a patent license to a
   2.499 +party means to make such an agreement or commitment not to enforce a
   2.500 +patent against the party.
   2.501 +
   2.502 +  If you convey a covered work, knowingly relying on a patent license,
   2.503 +and the Corresponding Source of the work is not available for anyone
   2.504 +to copy, free of charge and under the terms of this License, through a
   2.505 +publicly available network server or other readily accessible means,
   2.506 +then you must either (1) cause the Corresponding Source to be so
   2.507 +available, or (2) arrange to deprive yourself of the benefit of the
   2.508 +patent license for this particular work, or (3) arrange, in a manner
   2.509 +consistent with the requirements of this License, to extend the patent
   2.510 +license to downstream recipients.  "Knowingly relying" means you have
   2.511 +actual knowledge that, but for the patent license, your conveying the
   2.512 +covered work in a country, or your recipient's use of the covered work
   2.513 +in a country, would infringe one or more identifiable patents in that
   2.514 +country that you have reason to believe are valid.
   2.515 +
   2.516 +  If, pursuant to or in connection with a single transaction or
   2.517 +arrangement, you convey, or propagate by procuring conveyance of, a
   2.518 +covered work, and grant a patent license to some of the parties
   2.519 +receiving the covered work authorizing them to use, propagate, modify
   2.520 +or convey a specific copy of the covered work, then the patent license
   2.521 +you grant is automatically extended to all recipients of the covered
   2.522 +work and works based on it.
   2.523 +
   2.524 +  A patent license is "discriminatory" if it does not include within
   2.525 +the scope of its coverage, prohibits the exercise of, or is
   2.526 +conditioned on the non-exercise of one or more of the rights that are
   2.527 +specifically granted under this License.  You may not convey a covered
   2.528 +work if you are a party to an arrangement with a third party that is
   2.529 +in the business of distributing software, under which you make payment
   2.530 +to the third party based on the extent of your activity of conveying
   2.531 +the work, and under which the third party grants, to any of the
   2.532 +parties who would receive the covered work from you, a discriminatory
   2.533 +patent license (a) in connection with copies of the covered work
   2.534 +conveyed by you (or copies made from those copies), or (b) primarily
   2.535 +for and in connection with specific products or compilations that
   2.536 +contain the covered work, unless you entered into that arrangement,
   2.537 +or that patent license was granted, prior to 28 March 2007.
   2.538 +
   2.539 +  Nothing in this License shall be construed as excluding or limiting
   2.540 +any implied license or other defenses to infringement that may
   2.541 +otherwise be available to you under applicable patent law.
   2.542 +
   2.543 +  12. No Surrender of Others' Freedom.
   2.544 +
   2.545 +  If conditions are imposed on you (whether by court order, agreement or
   2.546 +otherwise) that contradict the conditions of this License, they do not
   2.547 +excuse you from the conditions of this License.  If you cannot convey a
   2.548 +covered work so as to satisfy simultaneously your obligations under this
   2.549 +License and any other pertinent obligations, then as a consequence you may
   2.550 +not convey it at all.  For example, if you agree to terms that obligate you
   2.551 +to collect a royalty for further conveying from those to whom you convey
   2.552 +the Program, the only way you could satisfy both those terms and this
   2.553 +License would be to refrain entirely from conveying the Program.
   2.554 +
   2.555 +  13. Use with the GNU Affero General Public License.
   2.556 +
   2.557 +  Notwithstanding any other provision of this License, you have
   2.558 +permission to link or combine any covered work with a work licensed
   2.559 +under version 3 of the GNU Affero General Public License into a single
   2.560 +combined work, and to convey the resulting work.  The terms of this
   2.561 +License will continue to apply to the part which is the covered work,
   2.562 +but the special requirements of the GNU Affero General Public License,
   2.563 +section 13, concerning interaction through a network will apply to the
   2.564 +combination as such.
   2.565 +
   2.566 +  14. Revised Versions of this License.
   2.567 +
   2.568 +  The Free Software Foundation may publish revised and/or new versions of
   2.569 +the GNU General Public License from time to time.  Such new versions will
   2.570 +be similar in spirit to the present version, but may differ in detail to
   2.571 +address new problems or concerns.
   2.572 +
   2.573 +  Each version is given a distinguishing version number.  If the
   2.574 +Program specifies that a certain numbered version of the GNU General
   2.575 +Public License "or any later version" applies to it, you have the
   2.576 +option of following the terms and conditions either of that numbered
   2.577 +version or of any later version published by the Free Software
   2.578 +Foundation.  If the Program does not specify a version number of the
   2.579 +GNU General Public License, you may choose any version ever published
   2.580 +by the Free Software Foundation.
   2.581 +
   2.582 +  If the Program specifies that a proxy can decide which future
   2.583 +versions of the GNU General Public License can be used, that proxy's
   2.584 +public statement of acceptance of a version permanently authorizes you
   2.585 +to choose that version for the Program.
   2.586 +
   2.587 +  Later license versions may give you additional or different
   2.588 +permissions.  However, no additional obligations are imposed on any
   2.589 +author or copyright holder as a result of your choosing to follow a
   2.590 +later version.
   2.591 +
   2.592 +  15. Disclaimer of Warranty.
   2.593 +
   2.594 +  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
   2.595 +APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
   2.596 +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
   2.597 +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
   2.598 +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   2.599 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
   2.600 +IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
   2.601 +ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
   2.602 +
   2.603 +  16. Limitation of Liability.
   2.604 +
   2.605 +  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
   2.606 +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
   2.607 +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
   2.608 +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
   2.609 +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
   2.610 +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
   2.611 +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
   2.612 +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
   2.613 +SUCH DAMAGES.
   2.614 +
   2.615 +  17. Interpretation of Sections 15 and 16.
   2.616 +
   2.617 +  If the disclaimer of warranty and limitation of liability provided
   2.618 +above cannot be given local legal effect according to their terms,
   2.619 +reviewing courts shall apply local law that most closely approximates
   2.620 +an absolute waiver of all civil liability in connection with the
   2.621 +Program, unless a warranty or assumption of liability accompanies a
   2.622 +copy of the Program in return for a fee.
   2.623 +
   2.624 +                     END OF TERMS AND CONDITIONS
   2.625 +
   2.626 +            How to Apply These Terms to Your New Programs
   2.627 +
   2.628 +  If you develop a new program, and you want it to be of the greatest
   2.629 +possible use to the public, the best way to achieve this is to make it
   2.630 +free software which everyone can redistribute and change under these terms.
   2.631 +
   2.632 +  To do so, attach the following notices to the program.  It is safest
   2.633 +to attach them to the start of each source file to most effectively
   2.634 +state the exclusion of warranty; and each file should have at least
   2.635 +the "copyright" line and a pointer to where the full notice is found.
   2.636 +
   2.637 +    <one line to give the program's name and a brief idea of what it does.>
   2.638 +    Copyright (C) <year>  <name of author>
   2.639 +
   2.640 +    This program is free software: you can redistribute it and/or modify
   2.641 +    it under the terms of the GNU General Public License as published by
   2.642 +    the Free Software Foundation, either version 3 of the License, or
   2.643 +    (at your option) any later version.
   2.644 +
   2.645 +    This program is distributed in the hope that it will be useful,
   2.646 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   2.647 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   2.648 +    GNU General Public License for more details.
   2.649 +
   2.650 +    You should have received a copy of the GNU General Public License
   2.651 +    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   2.652 +
   2.653 +Also add information on how to contact you by electronic and paper mail.
   2.654 +
   2.655 +  If the program does terminal interaction, make it output a short
   2.656 +notice like this when it starts in an interactive mode:
   2.657 +
   2.658 +    <program>  Copyright (C) <year>  <name of author>
   2.659 +    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
   2.660 +    This is free software, and you are welcome to redistribute it
   2.661 +    under certain conditions; type `show c' for details.
   2.662 +
   2.663 +The hypothetical commands `show w' and `show c' should show the appropriate
   2.664 +parts of the General Public License.  Of course, your program's commands
   2.665 +might be different; for a GUI interface, you would use an "about box".
   2.666 +
   2.667 +  You should also get your employer (if you work as a programmer) or school,
   2.668 +if any, to sign a "copyright disclaimer" for the program, if necessary.
   2.669 +For more information on this, and how to apply and follow the GNU GPL, see
   2.670 +<http://www.gnu.org/licenses/>.
   2.671 +
   2.672 +  The GNU General Public License does not permit incorporating your program
   2.673 +into proprietary programs.  If your program is a subroutine library, you
   2.674 +may consider it more useful to permit linking proprietary applications with
   2.675 +the library.  If this is what you want to do, use the GNU Lesser General
   2.676 +Public License instead of this License.  But first, please read
   2.677 +<http://www.gnu.org/philosophy/why-not-lgpl.html>.
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/libs/imago/COPYING.LESSER	Sun Jun 07 17:25:49 2015 +0300
     3.3 @@ -0,0 +1,165 @@
     3.4 +                   GNU LESSER GENERAL PUBLIC LICENSE
     3.5 +                       Version 3, 29 June 2007
     3.6 +
     3.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
     3.8 + Everyone is permitted to copy and distribute verbatim copies
     3.9 + of this license document, but changing it is not allowed.
    3.10 +
    3.11 +
    3.12 +  This version of the GNU Lesser General Public License incorporates
    3.13 +the terms and conditions of version 3 of the GNU General Public
    3.14 +License, supplemented by the additional permissions listed below.
    3.15 +
    3.16 +  0. Additional Definitions.
    3.17 +
    3.18 +  As used herein, "this License" refers to version 3 of the GNU Lesser
    3.19 +General Public License, and the "GNU GPL" refers to version 3 of the GNU
    3.20 +General Public License.
    3.21 +
    3.22 +  "The Library" refers to a covered work governed by this License,
    3.23 +other than an Application or a Combined Work as defined below.
    3.24 +
    3.25 +  An "Application" is any work that makes use of an interface provided
    3.26 +by the Library, but which is not otherwise based on the Library.
    3.27 +Defining a subclass of a class defined by the Library is deemed a mode
    3.28 +of using an interface provided by the Library.
    3.29 +
    3.30 +  A "Combined Work" is a work produced by combining or linking an
    3.31 +Application with the Library.  The particular version of the Library
    3.32 +with which the Combined Work was made is also called the "Linked
    3.33 +Version".
    3.34 +
    3.35 +  The "Minimal Corresponding Source" for a Combined Work means the
    3.36 +Corresponding Source for the Combined Work, excluding any source code
    3.37 +for portions of the Combined Work that, considered in isolation, are
    3.38 +based on the Application, and not on the Linked Version.
    3.39 +
    3.40 +  The "Corresponding Application Code" for a Combined Work means the
    3.41 +object code and/or source code for the Application, including any data
    3.42 +and utility programs needed for reproducing the Combined Work from the
    3.43 +Application, but excluding the System Libraries of the Combined Work.
    3.44 +
    3.45 +  1. Exception to Section 3 of the GNU GPL.
    3.46 +
    3.47 +  You may convey a covered work under sections 3 and 4 of this License
    3.48 +without being bound by section 3 of the GNU GPL.
    3.49 +
    3.50 +  2. Conveying Modified Versions.
    3.51 +
    3.52 +  If you modify a copy of the Library, and, in your modifications, a
    3.53 +facility refers to a function or data to be supplied by an Application
    3.54 +that uses the facility (other than as an argument passed when the
    3.55 +facility is invoked), then you may convey a copy of the modified
    3.56 +version:
    3.57 +
    3.58 +   a) under this License, provided that you make a good faith effort to
    3.59 +   ensure that, in the event an Application does not supply the
    3.60 +   function or data, the facility still operates, and performs
    3.61 +   whatever part of its purpose remains meaningful, or
    3.62 +
    3.63 +   b) under the GNU GPL, with none of the additional permissions of
    3.64 +   this License applicable to that copy.
    3.65 +
    3.66 +  3. Object Code Incorporating Material from Library Header Files.
    3.67 +
    3.68 +  The object code form of an Application may incorporate material from
    3.69 +a header file that is part of the Library.  You may convey such object
    3.70 +code under terms of your choice, provided that, if the incorporated
    3.71 +material is not limited to numerical parameters, data structure
    3.72 +layouts and accessors, or small macros, inline functions and templates
    3.73 +(ten or fewer lines in length), you do both of the following:
    3.74 +
    3.75 +   a) Give prominent notice with each copy of the object code that the
    3.76 +   Library is used in it and that the Library and its use are
    3.77 +   covered by this License.
    3.78 +
    3.79 +   b) Accompany the object code with a copy of the GNU GPL and this license
    3.80 +   document.
    3.81 +
    3.82 +  4. Combined Works.
    3.83 +
    3.84 +  You may convey a Combined Work under terms of your choice that,
    3.85 +taken together, effectively do not restrict modification of the
    3.86 +portions of the Library contained in the Combined Work and reverse
    3.87 +engineering for debugging such modifications, if you also do each of
    3.88 +the following:
    3.89 +
    3.90 +   a) Give prominent notice with each copy of the Combined Work that
    3.91 +   the Library is used in it and that the Library and its use are
    3.92 +   covered by this License.
    3.93 +
    3.94 +   b) Accompany the Combined Work with a copy of the GNU GPL and this license
    3.95 +   document.
    3.96 +
    3.97 +   c) For a Combined Work that displays copyright notices during
    3.98 +   execution, include the copyright notice for the Library among
    3.99 +   these notices, as well as a reference directing the user to the
   3.100 +   copies of the GNU GPL and this license document.
   3.101 +
   3.102 +   d) Do one of the following:
   3.103 +
   3.104 +       0) Convey the Minimal Corresponding Source under the terms of this
   3.105 +       License, and the Corresponding Application Code in a form
   3.106 +       suitable for, and under terms that permit, the user to
   3.107 +       recombine or relink the Application with a modified version of
   3.108 +       the Linked Version to produce a modified Combined Work, in the
   3.109 +       manner specified by section 6 of the GNU GPL for conveying
   3.110 +       Corresponding Source.
   3.111 +
   3.112 +       1) Use a suitable shared library mechanism for linking with the
   3.113 +       Library.  A suitable mechanism is one that (a) uses at run time
   3.114 +       a copy of the Library already present on the user's computer
   3.115 +       system, and (b) will operate properly with a modified version
   3.116 +       of the Library that is interface-compatible with the Linked
   3.117 +       Version.
   3.118 +
   3.119 +   e) Provide Installation Information, but only if you would otherwise
   3.120 +   be required to provide such information under section 6 of the
   3.121 +   GNU GPL, and only to the extent that such information is
   3.122 +   necessary to install and execute a modified version of the
   3.123 +   Combined Work produced by recombining or relinking the
   3.124 +   Application with a modified version of the Linked Version. (If
   3.125 +   you use option 4d0, the Installation Information must accompany
   3.126 +   the Minimal Corresponding Source and Corresponding Application
   3.127 +   Code. If you use option 4d1, you must provide the Installation
   3.128 +   Information in the manner specified by section 6 of the GNU GPL
   3.129 +   for conveying Corresponding Source.)
   3.130 +
   3.131 +  5. Combined Libraries.
   3.132 +
   3.133 +  You may place library facilities that are a work based on the
   3.134 +Library side by side in a single library together with other library
   3.135 +facilities that are not Applications and are not covered by this
   3.136 +License, and convey such a combined library under terms of your
   3.137 +choice, if you do both of the following:
   3.138 +
   3.139 +   a) Accompany the combined library with a copy of the same work based
   3.140 +   on the Library, uncombined with any other library facilities,
   3.141 +   conveyed under the terms of this License.
   3.142 +
   3.143 +   b) Give prominent notice with the combined library that part of it
   3.144 +   is a work based on the Library, and explaining where to find the
   3.145 +   accompanying uncombined form of the same work.
   3.146 +
   3.147 +  6. Revised Versions of the GNU Lesser General Public License.
   3.148 +
   3.149 +  The Free Software Foundation may publish revised and/or new versions
   3.150 +of the GNU Lesser General Public License from time to time. Such new
   3.151 +versions will be similar in spirit to the present version, but may
   3.152 +differ in detail to address new problems or concerns.
   3.153 +
   3.154 +  Each version is given a distinguishing version number. If the
   3.155 +Library as you received it specifies that a certain numbered version
   3.156 +of the GNU Lesser General Public License "or any later version"
   3.157 +applies to it, you have the option of following the terms and
   3.158 +conditions either of that published version or of any later version
   3.159 +published by the Free Software Foundation. If the Library as you
   3.160 +received it does not specify a version number of the GNU Lesser
   3.161 +General Public License, you may choose any version of the GNU Lesser
   3.162 +General Public License ever published by the Free Software Foundation.
   3.163 +
   3.164 +  If the Library as you received it specifies that a proxy can decide
   3.165 +whether future versions of the GNU Lesser General Public License shall
   3.166 +apply, that proxy's public statement of acceptance of any version is
   3.167 +permanent authorization for you to choose that version for the
   3.168 +Library.
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/libs/imago/README.rst	Sun Jun 07 17:25:49 2015 +0300
     4.3 @@ -0,0 +1,49 @@
     4.4 +libimago
     4.5 +========
     4.6 +
     4.7 +Overview
     4.8 +--------
     4.9 +Imago is a simple C library for reading and writing images in many different
    4.10 +image file formats.
    4.11 +
    4.12 +Download
    4.13 +--------
    4.14 +Latest release: http://nuclear.mutantstargoat.com/sw/libimago/files/libimago-2.0.tar.gz
    4.15 +
    4.16 +Grab the source code from github: https://github.com/jtsiomb/libimago
    4.17 +
    4.18 +web site: http://nuclear.mutantstargoat.com/sw/libimago
    4.19 +
    4.20 +License
    4.21 +-------
    4.22 +
    4.23 +Copyright (C) 2011-2015 John Tsiombikas <nuclear@member.fsf.org>
    4.24 +
    4.25 +You may freely use, modify and/or redistribute libimago, under the terms of the
    4.26 +GNU Lesser General Public License (LGPL) version 3 (or at your option, any
    4.27 +later version published by the Free Software Foundation). See COPYING_ and
    4.28 +COPYING.LESSER_ for details.
    4.29 +
    4.30 +Usage example
    4.31 +-------------
    4.32 +
    4.33 +heck out the example program under ``test/``, and the *heavily*
    4.34 +commented ``imago2.h`` header file, to find out how to use libimago.
    4.35 +
    4.36 +The simplest way to load image data in RGBA 32bit is:
    4.37 +
    4.38 +.. code:: c
    4.39 +
    4.40 + int width, height;
    4.41 + unsigned char *pixels = img_load_pixels("foo.png", &width, &height, IMG_FMT_RGBA32);
    4.42 + img_free_pixels(pixels);
    4.43 +
    4.44 +There's also an optional interface for loading an image and creating an OpenGL
    4.45 +texture out of it in a single call:
    4.46 +
    4.47 +.. code:: c
    4.48 +
    4.49 + unsigned int texture = img_gltexture_load("foo.png");
    4.50 +
    4.51 +.. _COPYING: http://www.gnu.org/licenses/gpl
    4.52 +.. _COPYING.LESSER: http://www.gnu.org/licenses/lgpl
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/libs/imago/conv.c	Sun Jun 07 17:25:49 2015 +0300
     5.3 @@ -0,0 +1,260 @@
     5.4 +/*
     5.5 +libimago - a multi-format image file input/output library.
     5.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     5.7 +
     5.8 +This program is free software: you can redistribute it and/or modify
     5.9 +it under the terms of the GNU Lesser General Public License as published
    5.10 +by the Free Software Foundation, either version 3 of the License, or
    5.11 +(at your option) any later version.
    5.12 +
    5.13 +This program is distributed in the hope that it will be useful,
    5.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.16 +GNU Lesser General Public License for more details.
    5.17 +
    5.18 +You should have received a copy of the GNU Lesser General Public License
    5.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    5.20 +*/
    5.21 +#include <string.h>
    5.22 +#include "imago2.h"
    5.23 +
    5.24 +/* pixel-format conversions are sub-optimal at the moment to avoid
    5.25 + * writing a lot of code. optimize at some point ?
    5.26 + */
    5.27 +
    5.28 +#define CLAMP(x, a, b)	((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
    5.29 +
    5.30 +struct pixel {
    5.31 +	float r, g, b, a;
    5.32 +};
    5.33 +
    5.34 +static void unpack_grey8(struct pixel *unp, void *pptr, int count);
    5.35 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
    5.36 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
    5.37 +static void unpack_greyf(struct pixel *unp, void *pptr, int count);
    5.38 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
    5.39 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
    5.40 +
    5.41 +static void pack_grey8(void *pptr, struct pixel *unp, int count);
    5.42 +static void pack_rgb24(void *pptr, struct pixel *unp, int count);
    5.43 +static void pack_rgba32(void *pptr, struct pixel *unp, int count);
    5.44 +static void pack_greyf(void *pptr, struct pixel *unp, int count);
    5.45 +static void pack_rgbf(void *pptr, struct pixel *unp, int count);
    5.46 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
    5.47 +
    5.48 +/* XXX keep in sync with enum img_fmt at imago2.h */
    5.49 +static void (*unpack[])(struct pixel*, void*, int) = {
    5.50 +	unpack_grey8,
    5.51 +	unpack_rgb24,
    5.52 +	unpack_rgba32,
    5.53 +	unpack_greyf,
    5.54 +	unpack_rgbf,
    5.55 +	unpack_rgbaf
    5.56 +};
    5.57 +
    5.58 +/* XXX keep in sync with enum img_fmt at imago2.h */
    5.59 +static void (*pack[])(void*, struct pixel*, int) = {
    5.60 +	pack_grey8,
    5.61 +	pack_rgb24,
    5.62 +	pack_rgba32,
    5.63 +	pack_greyf,
    5.64 +	pack_rgbf,
    5.65 +	pack_rgbaf
    5.66 +};
    5.67 +
    5.68 +
    5.69 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
    5.70 +{
    5.71 +	struct pixel pbuf[8];
    5.72 +	int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1);
    5.73 +	int i, num_pix = img->width * img->height;
    5.74 +	int num_iter = num_pix / bufsz;
    5.75 +	char *sptr, *dptr;
    5.76 +	struct img_pixmap nimg;
    5.77 +
    5.78 +	if(img->fmt == tofmt) {
    5.79 +		return 0;	/* nothing to do */
    5.80 +	}
    5.81 +
    5.82 +	img_init(&nimg);
    5.83 +	if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) {
    5.84 +		img_destroy(&nimg);
    5.85 +		return -1;
    5.86 +	}
    5.87 +
    5.88 +	sptr = img->pixels;
    5.89 +	dptr = nimg.pixels;
    5.90 +
    5.91 +	for(i=0; i<num_iter; i++) {
    5.92 +		unpack[img->fmt](pbuf, sptr, bufsz);
    5.93 +		pack[tofmt](dptr, pbuf, bufsz);
    5.94 +
    5.95 +		sptr += bufsz * img->pixelsz;
    5.96 +		dptr += bufsz * nimg.pixelsz;
    5.97 +	}
    5.98 +
    5.99 +	img_copy(img, &nimg);
   5.100 +	img_destroy(&nimg);
   5.101 +	return 0;
   5.102 +}
   5.103 +
   5.104 +/* the following functions *could* benefit from SIMD */
   5.105 +
   5.106 +static void unpack_grey8(struct pixel *unp, void *pptr, int count)
   5.107 +{
   5.108 +	int i;
   5.109 +	unsigned char *pix = pptr;
   5.110 +
   5.111 +	for(i=0; i<count; i++) {
   5.112 +		unp->r = unp->g = unp->b = (float)*pix++ / 255.0;
   5.113 +		unp->a = 1.0;
   5.114 +		unp++;
   5.115 +	}
   5.116 +}
   5.117 +
   5.118 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count)
   5.119 +{
   5.120 +	int i;
   5.121 +	unsigned char *pix = pptr;
   5.122 +
   5.123 +	for(i=0; i<count; i++) {
   5.124 +		unp->r = (float)*pix++ / 255.0;
   5.125 +		unp->g = (float)*pix++ / 255.0;
   5.126 +		unp->b = (float)*pix++ / 255.0;
   5.127 +		unp->a = 1.0;
   5.128 +		unp++;
   5.129 +	}
   5.130 +}
   5.131 +
   5.132 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
   5.133 +{
   5.134 +	int i;
   5.135 +	unsigned char *pix = pptr;
   5.136 +
   5.137 +	for(i=0; i<count; i++) {
   5.138 +		unp->r = (float)*pix++ / 255.0;
   5.139 +		unp->g = (float)*pix++ / 255.0;
   5.140 +		unp->b = (float)*pix++ / 255.0;
   5.141 +		unp->a = (float)*pix++ / 255.0;
   5.142 +		unp++;
   5.143 +	}
   5.144 +}
   5.145 +
   5.146 +static void unpack_greyf(struct pixel *unp, void *pptr, int count)
   5.147 +{
   5.148 +	int i;
   5.149 +	float *pix = pptr;
   5.150 +
   5.151 +	for(i=0; i<count; i++) {
   5.152 +		unp->r = unp->g = unp->b = *pix++;
   5.153 +		unp->a = 1.0;
   5.154 +		unp++;
   5.155 +	}
   5.156 +}
   5.157 +
   5.158 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count)
   5.159 +{
   5.160 +	int i;
   5.161 +	float *pix = pptr;
   5.162 +
   5.163 +	for(i=0; i<count; i++) {
   5.164 +		unp->r = *pix++;
   5.165 +		unp->g = *pix++;
   5.166 +		unp->b = *pix++;
   5.167 +		unp->a = 1.0;
   5.168 +		unp++;
   5.169 +	}
   5.170 +}
   5.171 +
   5.172 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
   5.173 +{
   5.174 +	int i;
   5.175 +	float *pix = pptr;
   5.176 +
   5.177 +	for(i=0; i<count; i++) {
   5.178 +		unp->r = *pix++;
   5.179 +		unp->g = *pix++;
   5.180 +		unp->b = *pix++;
   5.181 +		unp->a = *pix++;
   5.182 +		unp++;
   5.183 +	}
   5.184 +}
   5.185 +
   5.186 +
   5.187 +static void pack_grey8(void *pptr, struct pixel *unp, int count)
   5.188 +{
   5.189 +	int i;
   5.190 +	unsigned char *pix = pptr;
   5.191 +
   5.192 +	for(i=0; i<count; i++) {
   5.193 +		int lum = (int)(255.0 * (unp->r + unp->g + unp->b) / 3.0);
   5.194 +		*pix++ = CLAMP(lum, 0, 255);
   5.195 +		unp++;
   5.196 +	}
   5.197 +}
   5.198 +
   5.199 +static void pack_rgb24(void *pptr, struct pixel *unp, int count)
   5.200 +{
   5.201 +	int i;
   5.202 +	unsigned char *pix = pptr;
   5.203 +
   5.204 +	for(i=0; i<count; i++) {
   5.205 +		int r = (int)(unp->r * 255.0);
   5.206 +		int g = (int)(unp->g * 255.0);
   5.207 +		int b = (int)(unp->b * 255.0);
   5.208 +
   5.209 +		*pix++ = CLAMP(r, 0, 255);
   5.210 +		*pix++ = CLAMP(g, 0, 255);
   5.211 +		*pix++ = CLAMP(b, 0, 255);
   5.212 +		unp++;
   5.213 +	}
   5.214 +}
   5.215 +
   5.216 +static void pack_rgba32(void *pptr, struct pixel *unp, int count)
   5.217 +{
   5.218 +	int i;
   5.219 +	unsigned char *pix = pptr;
   5.220 +
   5.221 +	for(i=0; i<count; i++) {
   5.222 +		int r = (int)(unp->r * 255.0);
   5.223 +		int g = (int)(unp->g * 255.0);
   5.224 +		int b = (int)(unp->b * 255.0);
   5.225 +		int a = (int)(unp->a * 255.0);
   5.226 +
   5.227 +		*pix++ = CLAMP(r, 0, 255);
   5.228 +		*pix++ = CLAMP(g, 0, 255);
   5.229 +		*pix++ = CLAMP(b, 0, 255);
   5.230 +		*pix++ = CLAMP(a, 0, 255);
   5.231 +		unp++;
   5.232 +	}
   5.233 +}
   5.234 +
   5.235 +static void pack_greyf(void *pptr, struct pixel *unp, int count)
   5.236 +{
   5.237 +	int i;
   5.238 +	float *pix = pptr;
   5.239 +
   5.240 +	for(i=0; i<count; i++) {
   5.241 +		*pix++ = (unp->r + unp->g + unp->b) / 3.0;
   5.242 +		unp++;
   5.243 +	}
   5.244 +}
   5.245 +
   5.246 +static void pack_rgbf(void *pptr, struct pixel *unp, int count)
   5.247 +{
   5.248 +	int i;
   5.249 +	float *pix = pptr;
   5.250 +
   5.251 +	for(i=0; i<count; i++) {
   5.252 +		*pix++ = unp->r;
   5.253 +		*pix++ = unp->g;
   5.254 +		*pix++ = unp->b;
   5.255 +		unp++;
   5.256 +	}
   5.257 +}
   5.258 +
   5.259 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
   5.260 +{
   5.261 +	memcpy(pptr, unp, count * sizeof *unp);
   5.262 +}
   5.263 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/libs/imago/file_jpeg.c	Sun Jun 07 17:25:49 2015 +0300
     6.3 @@ -0,0 +1,294 @@
     6.4 +/*
     6.5 +libimago - a multi-format image file input/output library.
     6.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     6.7 +
     6.8 +This program is free software: you can redistribute it and/or modify
     6.9 +it under the terms of the GNU Lesser General Public License as published
    6.10 +by the Free Software Foundation, either version 3 of the License, or
    6.11 +(at your option) any later version.
    6.12 +
    6.13 +This program is distributed in the hope that it will be useful,
    6.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.16 +GNU Lesser General Public License for more details.
    6.17 +
    6.18 +You should have received a copy of the GNU Lesser General Public License
    6.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    6.20 +*/
    6.21 +
    6.22 +/* -- JPEG module -- */
    6.23 +
    6.24 +#include <stdio.h>
    6.25 +#include <stdlib.h>
    6.26 +#include <string.h>
    6.27 +
    6.28 +#ifdef WIN32
    6.29 +#include <windows.h>
    6.30 +#define HAVE_BOOLEAN
    6.31 +#endif
    6.32 +
    6.33 +#include <jpeglib.h>
    6.34 +#include "imago2.h"
    6.35 +#include "ftype_module.h"
    6.36 +
    6.37 +#define INPUT_BUF_SIZE	512
    6.38 +#define OUTPUT_BUF_SIZE	512
    6.39 +
    6.40 +/* data source manager: adapted from jdatasrc.c */
    6.41 +struct src_mgr {
    6.42 +	struct jpeg_source_mgr pub;
    6.43 +
    6.44 +	struct img_io *io;
    6.45 +	unsigned char buffer[INPUT_BUF_SIZE];
    6.46 +	int start_of_file;
    6.47 +};
    6.48 +
    6.49 +/* data destination manager: adapted from jdatadst.c */
    6.50 +struct dst_mgr {
    6.51 +	struct jpeg_destination_mgr pub;
    6.52 +
    6.53 +	struct img_io *io;
    6.54 +	unsigned char buffer[OUTPUT_BUF_SIZE];
    6.55 +};
    6.56 +
    6.57 +static int check(struct img_io *io);
    6.58 +static int read(struct img_pixmap *img, struct img_io *io);
    6.59 +static int write(struct img_pixmap *img, struct img_io *io);
    6.60 +
    6.61 +/* read source functions */
    6.62 +static void init_source(j_decompress_ptr jd);
    6.63 +static boolean fill_input_buffer(j_decompress_ptr jd);
    6.64 +static void skip_input_data(j_decompress_ptr jd, long num_bytes);
    6.65 +static void term_source(j_decompress_ptr jd);
    6.66 +
    6.67 +/* write destination functions */
    6.68 +static void init_destination(j_compress_ptr jc);
    6.69 +static boolean empty_output_buffer(j_compress_ptr jc);
    6.70 +static void term_destination(j_compress_ptr jc);
    6.71 +
    6.72 +int img_register_jpeg(void)
    6.73 +{
    6.74 +	static struct ftype_module mod = {".jpg", check, read, write};
    6.75 +	return img_register_module(&mod);
    6.76 +}
    6.77 +
    6.78 +
    6.79 +static int check(struct img_io *io)
    6.80 +{
    6.81 +	unsigned char sig[10];
    6.82 +
    6.83 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    6.84 +
    6.85 +	if(io->read(sig, 10, io->uptr) < 10) {
    6.86 +		io->seek(pos, SEEK_SET, io->uptr);
    6.87 +		return -1;
    6.88 +	}
    6.89 +
    6.90 +	if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 && memcmp(sig, "\xff\xd8\xff\xe1", 4) != 0
    6.91 +			&& memcmp(sig + 6, "JFIF", 4) != 0) {
    6.92 +		io->seek(pos, SEEK_SET, io->uptr);
    6.93 +		return -1;
    6.94 +	}
    6.95 +	io->seek(pos, SEEK_SET, io->uptr);
    6.96 +	return 0;
    6.97 +}
    6.98 +
    6.99 +static int read(struct img_pixmap *img, struct img_io *io)
   6.100 +{
   6.101 +	int i, nlines = 0;
   6.102 +	struct jpeg_decompress_struct cinfo;
   6.103 +	struct jpeg_error_mgr jerr;
   6.104 +	struct src_mgr src;
   6.105 +	unsigned char **scanlines;
   6.106 +
   6.107 +	io->seek(0, SEEK_CUR, io->uptr);
   6.108 +
   6.109 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX change... */
   6.110 +	jpeg_create_decompress(&cinfo);
   6.111 +
   6.112 +	src.pub.init_source = init_source;
   6.113 +	src.pub.fill_input_buffer = fill_input_buffer;
   6.114 +	src.pub.skip_input_data = skip_input_data;
   6.115 +	src.pub.resync_to_restart = jpeg_resync_to_restart;
   6.116 +	src.pub.term_source = term_source;
   6.117 +	src.pub.next_input_byte = 0;
   6.118 +	src.pub.bytes_in_buffer = 0;
   6.119 +	src.io = io;
   6.120 +	cinfo.src = (struct jpeg_source_mgr*)&src;
   6.121 +
   6.122 +	jpeg_read_header(&cinfo, 1);
   6.123 +	cinfo.out_color_space = JCS_RGB;
   6.124 +
   6.125 +	if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
   6.126 +		jpeg_destroy_decompress(&cinfo);
   6.127 +		return -1;
   6.128 +	}
   6.129 +
   6.130 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   6.131 +		jpeg_destroy_decompress(&cinfo);
   6.132 +		return -1;
   6.133 +	}
   6.134 +	scanlines[0] = img->pixels;
   6.135 +	for(i=1; i<img->height; i++) {
   6.136 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   6.137 +	}
   6.138 +
   6.139 +	jpeg_start_decompress(&cinfo);
   6.140 +	while(nlines < img->height) {
   6.141 +		int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   6.142 +		nlines += res;
   6.143 +	}
   6.144 +	jpeg_finish_decompress(&cinfo);
   6.145 +	jpeg_destroy_decompress(&cinfo);
   6.146 +
   6.147 +	free(scanlines);
   6.148 +	return 0;
   6.149 +}
   6.150 +
   6.151 +static int write(struct img_pixmap *img, struct img_io *io)
   6.152 +{
   6.153 +	int i, nlines = 0;
   6.154 +	struct jpeg_compress_struct cinfo;
   6.155 +	struct jpeg_error_mgr jerr;
   6.156 +	struct dst_mgr dest;
   6.157 +	struct img_pixmap tmpimg;
   6.158 +	unsigned char **scanlines;
   6.159 +
   6.160 +	img_init(&tmpimg);
   6.161 +
   6.162 +	if(img->fmt != IMG_FMT_RGB24) {
   6.163 +		if(img_copy(&tmpimg, img) == -1) {
   6.164 +			return -1;
   6.165 +		}
   6.166 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   6.167 +			img_destroy(&tmpimg);
   6.168 +			return -1;
   6.169 +		}
   6.170 +		img = &tmpimg;
   6.171 +	}
   6.172 +
   6.173 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   6.174 +		img_destroy(&tmpimg);
   6.175 +		return -1;
   6.176 +	}
   6.177 +	scanlines[0] = img->pixels;
   6.178 +	for(i=1; i<img->height; i++) {
   6.179 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   6.180 +	}
   6.181 +
   6.182 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX */
   6.183 +	jpeg_create_compress(&cinfo);
   6.184 +
   6.185 +	dest.pub.init_destination = init_destination;
   6.186 +	dest.pub.empty_output_buffer = empty_output_buffer;
   6.187 +	dest.pub.term_destination = term_destination;
   6.188 +	dest.io = io;
   6.189 +	cinfo.dest = (struct jpeg_destination_mgr*)&dest;
   6.190 +
   6.191 +	cinfo.image_width = img->width;
   6.192 +	cinfo.image_height = img->height;
   6.193 +	cinfo.input_components = 3;
   6.194 +	cinfo.in_color_space = JCS_RGB;
   6.195 +
   6.196 +	jpeg_set_defaults(&cinfo);
   6.197 +
   6.198 +	jpeg_start_compress(&cinfo, 1);
   6.199 +	while(nlines < img->height) {
   6.200 +		int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   6.201 +		nlines += res;
   6.202 +	}
   6.203 +	jpeg_finish_compress(&cinfo);
   6.204 +	jpeg_destroy_compress(&cinfo);
   6.205 +
   6.206 +	free(scanlines);
   6.207 +	img_destroy(&tmpimg);
   6.208 +	return 0;
   6.209 +}
   6.210 +
   6.211 +/* -- read source functions --
   6.212 + * the following functions are adapted from jdatasrc.c in jpeglib
   6.213 + */
   6.214 +static void init_source(j_decompress_ptr jd)
   6.215 +{
   6.216 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.217 +	src->start_of_file = 1;
   6.218 +}
   6.219 +
   6.220 +static boolean fill_input_buffer(j_decompress_ptr jd)
   6.221 +{
   6.222 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.223 +	size_t nbytes;
   6.224 +
   6.225 +	nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
   6.226 +
   6.227 +	if(nbytes <= 0) {
   6.228 +		if(src->start_of_file) {
   6.229 +			return 0;
   6.230 +		}
   6.231 +		/* insert a fake EOI marker */
   6.232 +		src->buffer[0] = 0xff;
   6.233 +		src->buffer[1] = JPEG_EOI;
   6.234 +		nbytes = 2;
   6.235 +	}
   6.236 +
   6.237 +	src->pub.next_input_byte = src->buffer;
   6.238 +	src->pub.bytes_in_buffer = nbytes;
   6.239 +	src->start_of_file = 0;
   6.240 +	return 1;
   6.241 +}
   6.242 +
   6.243 +static void skip_input_data(j_decompress_ptr jd, long num_bytes)
   6.244 +{
   6.245 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.246 +
   6.247 +	if(num_bytes > 0) {
   6.248 +		while(num_bytes > (long)src->pub.bytes_in_buffer) {
   6.249 +			num_bytes -= (long)src->pub.bytes_in_buffer;
   6.250 +			fill_input_buffer(jd);
   6.251 +		}
   6.252 +		src->pub.next_input_byte += (size_t)num_bytes;
   6.253 +		src->pub.bytes_in_buffer -= (size_t)num_bytes;
   6.254 +	}
   6.255 +}
   6.256 +
   6.257 +static void term_source(j_decompress_ptr jd)
   6.258 +{
   6.259 +	/* nothing to see here, move along */
   6.260 +}
   6.261 +
   6.262 +
   6.263 +/* -- write destination functions --
   6.264 + * the following functions are adapted from jdatadst.c in jpeglib
   6.265 + */
   6.266 +static void init_destination(j_compress_ptr jc)
   6.267 +{
   6.268 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.269 +
   6.270 +	dest->pub.next_output_byte = dest->buffer;
   6.271 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   6.272 +}
   6.273 +
   6.274 +static boolean empty_output_buffer(j_compress_ptr jc)
   6.275 +{
   6.276 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.277 +
   6.278 +	if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
   6.279 +		return 0;
   6.280 +	}
   6.281 +
   6.282 +	dest->pub.next_output_byte = dest->buffer;
   6.283 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   6.284 +	return 1;
   6.285 +}
   6.286 +
   6.287 +static void term_destination(j_compress_ptr jc)
   6.288 +{
   6.289 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.290 +	size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
   6.291 +
   6.292 +	/* write any remaining data in the buffer */
   6.293 +	if(datacount > 0) {
   6.294 +		dest->io->write(dest->buffer, datacount, dest->io->uptr);
   6.295 +	}
   6.296 +	/* XXX flush? ... */
   6.297 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/libs/imago/file_png.c	Sun Jun 07 17:25:49 2015 +0300
     7.3 @@ -0,0 +1,261 @@
     7.4 +/*
     7.5 +libimago - a multi-format image file input/output library.
     7.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     7.7 +
     7.8 +This program is free software: you can redistribute it and/or modify
     7.9 +it under the terms of the GNU Lesser General Public License as published
    7.10 +by the Free Software Foundation, either version 3 of the License, or
    7.11 +(at your option) any later version.
    7.12 +
    7.13 +This program is distributed in the hope that it will be useful,
    7.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.16 +GNU Lesser General Public License for more details.
    7.17 +
    7.18 +You should have received a copy of the GNU Lesser General Public License
    7.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    7.20 +*/
    7.21 +
    7.22 +/* -- PNG module -- */
    7.23 +
    7.24 +#include <stdlib.h>
    7.25 +#include <png.h>
    7.26 +#include "imago2.h"
    7.27 +#include "ftype_module.h"
    7.28 +
    7.29 +static int check_file(struct img_io *io);
    7.30 +static int read_file(struct img_pixmap *img, struct img_io *io);
    7.31 +static int write_file(struct img_pixmap *img, struct img_io *io);
    7.32 +
    7.33 +static void read_func(png_struct *png, unsigned char *data, size_t len);
    7.34 +static void write_func(png_struct *png, unsigned char *data, size_t len);
    7.35 +static void flush_func(png_struct *png);
    7.36 +
    7.37 +static int png_type_to_fmt(int color_type, int channel_bits);
    7.38 +static int fmt_to_png_type(enum img_fmt fmt);
    7.39 +
    7.40 +
    7.41 +int img_register_png(void)
    7.42 +{
    7.43 +	static struct ftype_module mod = {".png", check_file, read_file, write_file};
    7.44 +	return img_register_module(&mod);
    7.45 +}
    7.46 +
    7.47 +static int check_file(struct img_io *io)
    7.48 +{
    7.49 +	unsigned char sig[8];
    7.50 +	int res;
    7.51 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    7.52 +
    7.53 +	if(io->read(sig, 8, io->uptr) < 8) {
    7.54 +		io->seek(pos, SEEK_SET, io->uptr);
    7.55 +		return -1;
    7.56 +	}
    7.57 +
    7.58 +	res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
    7.59 +	io->seek(pos, SEEK_SET, io->uptr);
    7.60 +	return res;
    7.61 +}
    7.62 +
    7.63 +static int read_file(struct img_pixmap *img, struct img_io *io)
    7.64 +{
    7.65 +	png_struct *png;
    7.66 +	png_info *info;
    7.67 +	int channel_bits, color_type, ilace_type, compression, filtering, fmt;
    7.68 +	png_uint_32 xsz, ysz;
    7.69 +
    7.70 +	if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
    7.71 +		return -1;
    7.72 +	}
    7.73 +
    7.74 +	if(!(info = png_create_info_struct(png))) {
    7.75 +		png_destroy_read_struct(&png, 0, 0);
    7.76 +		return -1;
    7.77 +	}
    7.78 +
    7.79 +	if(setjmp(png_jmpbuf(png))) {
    7.80 +		png_destroy_read_struct(&png, &info, 0);
    7.81 +		return -1;
    7.82 +	}
    7.83 +
    7.84 +	png_set_read_fn(png, io, read_func);
    7.85 +	png_set_sig_bytes(png, 0);
    7.86 +	png_read_png(png, info, 0, 0);
    7.87 +
    7.88 +	png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
    7.89 +			&compression, &filtering);
    7.90 +	if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
    7.91 +		png_destroy_read_struct(&png, &info, 0);
    7.92 +		return -1;
    7.93 +	}
    7.94 +
    7.95 +	if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
    7.96 +		png_destroy_read_struct(&png, &info, 0);
    7.97 +		return -1;
    7.98 +	}
    7.99 +
   7.100 +
   7.101 +	if(channel_bits == 8) {
   7.102 +		unsigned int i;
   7.103 +		unsigned char **lineptr = (unsigned char**)png_get_rows(png, info);
   7.104 +		unsigned char *dest = img->pixels;
   7.105 +
   7.106 +		for(i=0; i<ysz; i++) {
   7.107 +			memcpy(dest, lineptr[i], xsz * img->pixelsz);
   7.108 +			dest += xsz * img->pixelsz;
   7.109 +		}
   7.110 +	} else {
   7.111 +		unsigned int i, j, num_elem;
   7.112 +		unsigned char **lineptr = (unsigned char**)png_get_rows(png, info);
   7.113 +		float *dest = img->pixels;
   7.114 +
   7.115 +		num_elem = img->pixelsz / sizeof(float);
   7.116 +		for(i=0; i<ysz; i++) {
   7.117 +			for(j=0; j<xsz * num_elem; j++) {
   7.118 +				unsigned short val = (lineptr[i][j * 2] << 8) | lineptr[i][j * 2 + 1];
   7.119 +				*dest++ = (float)val / 65535.0;
   7.120 +			}
   7.121 +		}
   7.122 +	}
   7.123 +
   7.124 +
   7.125 +	png_destroy_read_struct(&png, &info, 0);
   7.126 +	return 0;
   7.127 +}
   7.128 +
   7.129 +
   7.130 +static int write_file(struct img_pixmap *img, struct img_io *io)
   7.131 +{
   7.132 +	png_struct *png;
   7.133 +	png_info *info;
   7.134 +	png_text txt;
   7.135 +	struct img_pixmap tmpimg;
   7.136 +	unsigned char **rows;
   7.137 +	unsigned char *pixptr;
   7.138 +	int i, coltype;
   7.139 +
   7.140 +	img_init(&tmpimg);
   7.141 +
   7.142 +	if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
   7.143 +		return -1;
   7.144 +	}
   7.145 +	if(!(info = png_create_info_struct(png))) {
   7.146 +		png_destroy_write_struct(&png, 0);
   7.147 +		return -1;
   7.148 +	}
   7.149 +
   7.150 +	/* if the input image is floating-point, we need to convert it to integer */
   7.151 +	if(img_is_float(img)) {
   7.152 +		if(img_copy(&tmpimg, img) == -1) {
   7.153 +			return -1;
   7.154 +		}
   7.155 +		if(img_to_integer(&tmpimg) == -1) {
   7.156 +			img_destroy(&tmpimg);
   7.157 +			return -1;
   7.158 +		}
   7.159 +		img = &tmpimg;
   7.160 +	}
   7.161 +
   7.162 +	txt.compression = PNG_TEXT_COMPRESSION_NONE;
   7.163 +	txt.key = "Software";
   7.164 +	txt.text = "libimago2";
   7.165 +	txt.text_length = 0;
   7.166 +
   7.167 +	if(setjmp(png_jmpbuf(png))) {
   7.168 +		png_destroy_write_struct(&png, &info);
   7.169 +		img_destroy(&tmpimg);
   7.170 +		return -1;
   7.171 +	}
   7.172 +	png_set_write_fn(png, io, write_func, flush_func);
   7.173 +
   7.174 +	coltype = fmt_to_png_type(img->fmt);
   7.175 +	png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
   7.176 +			PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
   7.177 +	png_set_text(png, info, &txt, 1);
   7.178 +
   7.179 +	if(!(rows = malloc(img->height * sizeof *rows))) {
   7.180 +		png_destroy_write_struct(&png, &info);
   7.181 +		img_destroy(&tmpimg);
   7.182 +		return -1;
   7.183 +	}
   7.184 +
   7.185 +	pixptr = img->pixels;
   7.186 +	for(i=0; i<img->height; i++) {
   7.187 +		rows[i] = pixptr;
   7.188 +		pixptr += img->width * img->pixelsz;
   7.189 +	}
   7.190 +	png_set_rows(png, info, rows);
   7.191 +
   7.192 +	png_write_png(png, info, 0, 0);
   7.193 +	png_write_end(png, info);
   7.194 +	png_destroy_write_struct(&png, &info);
   7.195 +
   7.196 +	free(rows);
   7.197 +
   7.198 +	img_destroy(&tmpimg);
   7.199 +	return 0;
   7.200 +}
   7.201 +
   7.202 +static void read_func(png_struct *png, unsigned char *data, size_t len)
   7.203 +{
   7.204 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   7.205 +
   7.206 +	if(io->read(data, len, io->uptr) == -1) {
   7.207 +		longjmp(png_jmpbuf(png), 1);
   7.208 +	}
   7.209 +}
   7.210 +
   7.211 +static void write_func(png_struct *png, unsigned char *data, size_t len)
   7.212 +{
   7.213 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   7.214 +
   7.215 +	if(io->write(data, len, io->uptr) == -1) {
   7.216 +		longjmp(png_jmpbuf(png), 1);
   7.217 +	}
   7.218 +}
   7.219 +
   7.220 +static void flush_func(png_struct *png)
   7.221 +{
   7.222 +	/* XXX does it matter that we can't flush? */
   7.223 +}
   7.224 +
   7.225 +static int png_type_to_fmt(int color_type, int channel_bits)
   7.226 +{
   7.227 +	/* only 8 and 16 bits per channel ar supported at the moment */
   7.228 +	if(channel_bits != 8 && channel_bits != 16) {
   7.229 +		return -1;
   7.230 +	}
   7.231 +
   7.232 +	switch(color_type) {
   7.233 +	case PNG_COLOR_TYPE_RGB:
   7.234 +		return channel_bits == 16 ? IMG_FMT_RGBF : IMG_FMT_RGB24;
   7.235 +
   7.236 +	case PNG_COLOR_TYPE_RGB_ALPHA:
   7.237 +		return channel_bits == 16 ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
   7.238 +
   7.239 +	case PNG_COLOR_TYPE_GRAY:
   7.240 +		return channel_bits == 16 ? IMG_FMT_GREYF : IMG_FMT_GREY8;
   7.241 +
   7.242 +	default:
   7.243 +		break;
   7.244 +	}
   7.245 +	return -1;
   7.246 +}
   7.247 +
   7.248 +static int fmt_to_png_type(enum img_fmt fmt)
   7.249 +{
   7.250 +	switch(fmt) {
   7.251 +	case IMG_FMT_GREY8:
   7.252 +		return PNG_COLOR_TYPE_GRAY;
   7.253 +
   7.254 +	case IMG_FMT_RGB24:
   7.255 +		return PNG_COLOR_TYPE_RGB;
   7.256 +
   7.257 +	case IMG_FMT_RGBA32:
   7.258 +		return PNG_COLOR_TYPE_RGBA;
   7.259 +
   7.260 +	default:
   7.261 +		break;
   7.262 +	}
   7.263 +	return -1;
   7.264 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/libs/imago/file_ppm.c	Sun Jun 07 17:25:49 2015 +0300
     8.3 @@ -0,0 +1,153 @@
     8.4 +/*
     8.5 +libimago - a multi-format image file input/output library.
     8.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     8.7 +
     8.8 +This program is free software: you can redistribute it and/or modify
     8.9 +it under the terms of the GNU Lesser General Public License as published
    8.10 +by the Free Software Foundation, either version 3 of the License, or
    8.11 +(at your option) any later version.
    8.12 +
    8.13 +This program is distributed in the hope that it will be useful,
    8.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.16 +GNU Lesser General Public License for more details.
    8.17 +
    8.18 +You should have received a copy of the GNU Lesser General Public License
    8.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    8.20 +*/
    8.21 +
    8.22 +/* -- Portable Pixmap (PPM) module -- */
    8.23 +
    8.24 +#include <string.h>
    8.25 +#include "imago2.h"
    8.26 +#include "ftype_module.h"
    8.27 +
    8.28 +static int check(struct img_io *io);
    8.29 +static int read(struct img_pixmap *img, struct img_io *io);
    8.30 +static int write(struct img_pixmap *img, struct img_io *io);
    8.31 +
    8.32 +int img_register_ppm(void)
    8.33 +{
    8.34 +	static struct ftype_module mod = {".ppm", check, read, write};
    8.35 +	return img_register_module(&mod);
    8.36 +}
    8.37 +
    8.38 +
    8.39 +static int check(struct img_io *io)
    8.40 +{
    8.41 +	char id[2];
    8.42 +	int res = -1;
    8.43 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    8.44 +
    8.45 +	if(io->read(id, 2, io->uptr) < 2) {
    8.46 +		io->seek(pos, SEEK_SET, io->uptr);
    8.47 +		return -1;
    8.48 +	}
    8.49 +
    8.50 +	if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
    8.51 +		res = 0;
    8.52 +	}
    8.53 +	io->seek(pos, SEEK_SET, io->uptr);
    8.54 +	return res;
    8.55 +}
    8.56 +
    8.57 +static int iofgetc(struct img_io *io)
    8.58 +{
    8.59 +	char c;
    8.60 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
    8.61 +}
    8.62 +
    8.63 +static char *iofgets(char *buf, int size, struct img_io *io)
    8.64 +{
    8.65 +	int c;
    8.66 +	char *ptr = buf;
    8.67 +
    8.68 +	while(--size > 0 && (c = iofgetc(io)) != -1) {
    8.69 +		*ptr++ = c;
    8.70 +		if(c == '\n') break;
    8.71 +	}
    8.72 +	*ptr = 0;
    8.73 +
    8.74 +	return ptr == buf ? 0 : buf;
    8.75 +}
    8.76 +
    8.77 +/* TODO: implement P3 reading */
    8.78 +static int read(struct img_pixmap *img, struct img_io *io)
    8.79 +{
    8.80 +	char buf[256];
    8.81 +	int xsz, ysz, maxval, got_hdrlines = 1;
    8.82 +
    8.83 +	if(!iofgets(buf, sizeof buf, io)) {
    8.84 +		return -1;
    8.85 +	}
    8.86 +	if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
    8.87 +		return -1;
    8.88 +	}
    8.89 +
    8.90 +	while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
    8.91 +		if(buf[0] == '#') continue;
    8.92 +
    8.93 +		switch(got_hdrlines) {
    8.94 +		case 1:
    8.95 +			if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
    8.96 +				return -1;
    8.97 +			}
    8.98 +			break;
    8.99 +
   8.100 +		case 2:
   8.101 +			if(sscanf(buf, "%d\n", &maxval) < 1) {
   8.102 +				return -1;
   8.103 +			}
   8.104 +		default:
   8.105 +			break;
   8.106 +		}
   8.107 +		got_hdrlines++;
   8.108 +	}
   8.109 +
   8.110 +	if(xsz < 1 || ysz < 1 || maxval != 255) {
   8.111 +		return -1;
   8.112 +	}
   8.113 +
   8.114 +	if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
   8.115 +		return -1;
   8.116 +	}
   8.117 +
   8.118 +	if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < (unsigned int)(xsz * ysz * 3)) {
   8.119 +		return -1;
   8.120 +	}
   8.121 +	return 0;
   8.122 +}
   8.123 +
   8.124 +static int write(struct img_pixmap *img, struct img_io *io)
   8.125 +{
   8.126 +	int sz;
   8.127 +	char buf[256];
   8.128 +	struct img_pixmap tmpimg;
   8.129 +
   8.130 +	img_init(&tmpimg);
   8.131 +
   8.132 +	if(img->fmt != IMG_FMT_RGB24) {
   8.133 +		if(img_copy(&tmpimg, img) == -1) {
   8.134 +			return -1;
   8.135 +		}
   8.136 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   8.137 +			return -1;
   8.138 +		}
   8.139 +		img = &tmpimg;
   8.140 +	}
   8.141 +
   8.142 +	sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
   8.143 +	if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
   8.144 +		img_destroy(&tmpimg);
   8.145 +		return -1;
   8.146 +	}
   8.147 +
   8.148 +	sz = img->width * img->height * 3;
   8.149 +	if(io->write(img->pixels, sz, io->uptr) < (unsigned int)sz) {
   8.150 +		img_destroy(&tmpimg);
   8.151 +		return -1;
   8.152 +	}
   8.153 +
   8.154 +	img_destroy(&tmpimg);
   8.155 +	return 0;
   8.156 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/libs/imago/file_rgbe.c	Sun Jun 07 17:25:49 2015 +0300
     9.3 @@ -0,0 +1,501 @@
     9.4 +/* This file contains code to read and write four byte rgbe file format
     9.5 + * developed by Greg Ward.  It handles the conversions between rgbe and
     9.6 + * pixels consisting of floats.  The data is assumed to be an array of floats.
     9.7 + * By default there are three floats per pixel in the order red, green, blue.
     9.8 + * (RGBE_DATA_??? values control this.)
     9.9 + *
    9.10 + * written by Bruce Walter  (bjw@graphics.cornell.edu)  5/26/95
    9.11 + * based on code written by Greg Ward
    9.12 + * minor modifications by John Tsiombikas (nuclear@member.fsf.org) apr.9 2007
    9.13 + */
    9.14 +
    9.15 +#include <stdio.h>
    9.16 +#include <stdlib.h>
    9.17 +#include <string.h>
    9.18 +#include <math.h>
    9.19 +#include <ctype.h>
    9.20 +#include <errno.h>
    9.21 +#include "imago2.h"
    9.22 +#include "ftype_module.h"
    9.23 +
    9.24 +
    9.25 +typedef struct {
    9.26 +	int valid;				/* indicate which fields are valid */
    9.27 +	char programtype[16];	/* listed at beginning of file to identify it
    9.28 +							 * after "#?".  defaults to "RGBE" */
    9.29 +	float gamma;			/* image has already been gamma corrected with
    9.30 +							 * given gamma.  defaults to 1.0 (no correction) */
    9.31 +	float exposure;			/* a value of 1.0 in an image corresponds to
    9.32 +							 * <exposure> watts/steradian/m^2.
    9.33 +							 * defaults to 1.0 */
    9.34 +} rgbe_header_info;
    9.35 +
    9.36 +
    9.37 +static int check(struct img_io *io);
    9.38 +static int read(struct img_pixmap *img, struct img_io *io);
    9.39 +static int write(struct img_pixmap *img, struct img_io *io);
    9.40 +
    9.41 +static int rgbe_read_header(struct img_io *io, int *width, int *height, rgbe_header_info * info);
    9.42 +static int rgbe_read_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines);
    9.43 +
    9.44 +
    9.45 +int img_register_rgbe(void)
    9.46 +{
    9.47 +	static struct ftype_module mod = {".rgbe", check, read, write};
    9.48 +	return img_register_module(&mod);
    9.49 +}
    9.50 +
    9.51 +
    9.52 +static int check(struct img_io *io)
    9.53 +{
    9.54 +	int xsz, ysz, res;
    9.55 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    9.56 +
    9.57 +	rgbe_header_info hdr;
    9.58 +	res = rgbe_read_header(io, &xsz, &ysz, &hdr);
    9.59 +
    9.60 +	io->seek(pos, SEEK_SET, io->uptr);
    9.61 +	return res;
    9.62 +}
    9.63 +
    9.64 +static int read(struct img_pixmap *img, struct img_io *io)
    9.65 +{
    9.66 +	int xsz, ysz;
    9.67 +	rgbe_header_info hdr;
    9.68 +
    9.69 +	if(rgbe_read_header(io, &xsz, &ysz, &hdr) == -1) {
    9.70 +		return -1;
    9.71 +	}
    9.72 +
    9.73 +	if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGBF, 0) == -1) {
    9.74 +		return -1;
    9.75 +	}
    9.76 +	if(rgbe_read_pixels_rle(io, img->pixels, xsz, ysz) == -1) {
    9.77 +		return -1;
    9.78 +	}
    9.79 +	return 0;
    9.80 +}
    9.81 +
    9.82 +static int write(struct img_pixmap *img, struct img_io *io)
    9.83 +{
    9.84 +	return -1;	/* TODO */
    9.85 +}
    9.86 +
    9.87 +
    9.88 +static int iofgetc(struct img_io *io)
    9.89 +{
    9.90 +	char c;
    9.91 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
    9.92 +}
    9.93 +
    9.94 +static char *iofgets(char *buf, int size, struct img_io *io)
    9.95 +{
    9.96 +	int c;
    9.97 +	char *ptr = buf;
    9.98 +
    9.99 +	while(--size > 0 && (c = iofgetc(io)) != -1) {
   9.100 +		*ptr++ = c;
   9.101 +		if(c == '\n') break;
   9.102 +	}
   9.103 +	*ptr = 0;
   9.104 +
   9.105 +	return ptr == buf ? 0 : buf;
   9.106 +}
   9.107 +
   9.108 +
   9.109 +/* flags indicating which fields in an rgbe_header_info are valid */
   9.110 +#define RGBE_VALID_PROGRAMTYPE 0x01
   9.111 +#define RGBE_VALID_GAMMA       0x02
   9.112 +#define RGBE_VALID_EXPOSURE    0x04
   9.113 +
   9.114 +/* return codes for rgbe routines */
   9.115 +#define RGBE_RETURN_SUCCESS 0
   9.116 +#define RGBE_RETURN_FAILURE -1
   9.117 +
   9.118 +
   9.119 +#if defined(__cplusplus) || defined(GNUC) || __STDC_VERSION >= 199901L
   9.120 +#define INLINE inline
   9.121 +#else
   9.122 +#define INLINE
   9.123 +#endif
   9.124 +
   9.125 +/* offsets to red, green, and blue components in a data (float) pixel */
   9.126 +#define RGBE_DATA_RED	0
   9.127 +#define RGBE_DATA_GREEN  1
   9.128 +#define RGBE_DATA_BLUE   2
   9.129 +
   9.130 +/* number of floats per pixel */
   9.131 +#define RGBE_DATA_SIZE   3
   9.132 +
   9.133 +enum rgbe_error_codes {
   9.134 +	rgbe_read_error,
   9.135 +	rgbe_write_error,
   9.136 +	rgbe_format_error,
   9.137 +	rgbe_memory_error
   9.138 +};
   9.139 +
   9.140 +
   9.141 +/* default error routine.  change this to change error handling */
   9.142 +static int rgbe_error(int rgbe_error_code, char *msg)
   9.143 +{
   9.144 +	switch (rgbe_error_code) {
   9.145 +	case rgbe_read_error:
   9.146 +		fprintf(stderr, "RGBE read error: %s\n", strerror(errno));
   9.147 +		break;
   9.148 +
   9.149 +	case rgbe_write_error:
   9.150 +		fprintf(stderr, "RGBE write error: %s\n", strerror(errno));
   9.151 +		break;
   9.152 +
   9.153 +	case rgbe_format_error:
   9.154 +		fprintf(stderr, "RGBE bad file format: %s\n", msg);
   9.155 +		break;
   9.156 +
   9.157 +	default:
   9.158 +	case rgbe_memory_error:
   9.159 +		fprintf(stderr, "RGBE error: %s\n", msg);
   9.160 +	}
   9.161 +	return RGBE_RETURN_FAILURE;
   9.162 +}
   9.163 +
   9.164 +/* standard conversion from float pixels to rgbe pixels */
   9.165 +/*static INLINE void float2rgbe(unsigned char rgbe[4], float red, float green, float blue)
   9.166 +{
   9.167 +	float v;
   9.168 +	int e;
   9.169 +
   9.170 +	v = red;
   9.171 +	if(green > v)
   9.172 +		v = green;
   9.173 +	if(blue > v)
   9.174 +		v = blue;
   9.175 +	if(v < 1e-32) {
   9.176 +		rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
   9.177 +	} else {
   9.178 +		v = frexp(v, &e) * 256.0 / v;
   9.179 +		rgbe[0] = (unsigned char)(red * v);
   9.180 +		rgbe[1] = (unsigned char)(green * v);
   9.181 +		rgbe[2] = (unsigned char)(blue * v);
   9.182 +		rgbe[3] = (unsigned char)(e + 128);
   9.183 +	}
   9.184 +}*/
   9.185 +
   9.186 +/* standard conversion from rgbe to float pixels */
   9.187 +/* note: Ward uses ldexp(col+0.5,exp-(128+8)). However we wanted pixels */
   9.188 +/*       in the range [0,1] to map back into the range [0,1]. */
   9.189 +static INLINE void rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4])
   9.190 +{
   9.191 +	float f;
   9.192 +
   9.193 +	if(rgbe[3]) {				/*nonzero pixel */
   9.194 +		f = ldexp(1.0, rgbe[3] - (int)(128 + 8));
   9.195 +		*red = rgbe[0] * f;
   9.196 +		*green = rgbe[1] * f;
   9.197 +		*blue = rgbe[2] * f;
   9.198 +	} else
   9.199 +		*red = *green = *blue = 0.0;
   9.200 +}
   9.201 +
   9.202 +#if 0
   9.203 +/* default minimal header. modify if you want more information in header */
   9.204 +static int rgbe_write_header(FILE * fp, int width, int height, rgbe_header_info * info)
   9.205 +{
   9.206 +	char *programtype = "RGBE";
   9.207 +
   9.208 +	if(info && (info->valid & RGBE_VALID_PROGRAMTYPE))
   9.209 +		programtype = info->programtype;
   9.210 +	if(fprintf(fp, "#?%s\n", programtype) < 0)
   9.211 +		return rgbe_error(rgbe_write_error, NULL);
   9.212 +	/* The #? is to identify file type, the programtype is optional. */
   9.213 +	if(info && (info->valid & RGBE_VALID_GAMMA)) {
   9.214 +		if(fprintf(fp, "GAMMA=%g\n", info->gamma) < 0)
   9.215 +			return rgbe_error(rgbe_write_error, NULL);
   9.216 +	}
   9.217 +	if(info && (info->valid & RGBE_VALID_EXPOSURE)) {
   9.218 +		if(fprintf(fp, "EXPOSURE=%g\n", info->exposure) < 0)
   9.219 +			return rgbe_error(rgbe_write_error, NULL);
   9.220 +	}
   9.221 +	if(fprintf(fp, "FORMAT=32-bit_rle_rgbe\n\n") < 0)
   9.222 +		return rgbe_error(rgbe_write_error, NULL);
   9.223 +	if(fprintf(fp, "-Y %d +X %d\n", height, width) < 0)
   9.224 +		return rgbe_error(rgbe_write_error, NULL);
   9.225 +	return RGBE_RETURN_SUCCESS;
   9.226 +}
   9.227 +#endif
   9.228 +
   9.229 +/* minimal header reading.  modify if you want to parse more information */
   9.230 +static int rgbe_read_header(struct img_io *io, int *width, int *height, rgbe_header_info * info)
   9.231 +{
   9.232 +	char buf[128];
   9.233 +	float tempf;
   9.234 +	int i;
   9.235 +
   9.236 +	if(info) {
   9.237 +		info->valid = 0;
   9.238 +		info->programtype[0] = 0;
   9.239 +		info->gamma = info->exposure = 1.0;
   9.240 +	}
   9.241 +	if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == NULL)
   9.242 +		return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
   9.243 +	if((buf[0] != '#') || (buf[1] != '?')) {
   9.244 +		/* if you want to require the magic token then uncomment the next line */
   9.245 +		/*return rgbe_error(rgbe_format_error,"bad initial token"); */
   9.246 +	} else if(info) {
   9.247 +		info->valid |= RGBE_VALID_PROGRAMTYPE;
   9.248 +		for(i = 0; i < sizeof(info->programtype) - 1; i++) {
   9.249 +			if((buf[i + 2] == 0) || isspace(buf[i + 2]))
   9.250 +				break;
   9.251 +			info->programtype[i] = buf[i + 2];
   9.252 +		}
   9.253 +		info->programtype[i] = 0;
   9.254 +		if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
   9.255 +			return rgbe_error(rgbe_read_error, NULL);
   9.256 +	}
   9.257 +	for(;;) {
   9.258 +		if((buf[0] == 0) || (buf[0] == '\n'))
   9.259 +			return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "no FORMAT specifier found");*/
   9.260 +		else if(strcmp(buf, "FORMAT=32-bit_rle_rgbe\n") == 0)
   9.261 +			break;				/* format found so break out of loop */
   9.262 +		else if(info && (sscanf(buf, "GAMMA=%g", &tempf) == 1)) {
   9.263 +			info->gamma = tempf;
   9.264 +			info->valid |= RGBE_VALID_GAMMA;
   9.265 +		} else if(info && (sscanf(buf, "EXPOSURE=%g", &tempf) == 1)) {
   9.266 +			info->exposure = tempf;
   9.267 +			info->valid |= RGBE_VALID_EXPOSURE;
   9.268 +		}
   9.269 +		if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
   9.270 +			return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
   9.271 +	}
   9.272 +	if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
   9.273 +		return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
   9.274 +	if(strcmp(buf, "\n") != 0)
   9.275 +		return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "missing blank line after FORMAT specifier");*/
   9.276 +	if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
   9.277 +		return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
   9.278 +	if(sscanf(buf, "-Y %d +X %d", height, width) < 2)
   9.279 +		return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "missing image size specifier");*/
   9.280 +	return RGBE_RETURN_SUCCESS;
   9.281 +}
   9.282 +
   9.283 +#if 0
   9.284 +/* simple write routine that does not use run length encoding */
   9.285 +
   9.286 +/* These routines can be made faster by allocating a larger buffer and
   9.287 +   fread-ing and fwrite-ing the data in larger chunks */
   9.288 +static int rgbe_write_pixels(FILE * fp, float *data, int numpixels)
   9.289 +{
   9.290 +	unsigned char rgbe[4];
   9.291 +
   9.292 +	while(numpixels-- > 0) {
   9.293 +		float2rgbe(rgbe, data[RGBE_DATA_RED], data[RGBE_DATA_GREEN], data[RGBE_DATA_BLUE]);
   9.294 +		data += RGBE_DATA_SIZE;
   9.295 +		if(fwrite(rgbe, sizeof(rgbe), 1, fp) < 1)
   9.296 +			return rgbe_error(rgbe_write_error, NULL);
   9.297 +	}
   9.298 +	return RGBE_RETURN_SUCCESS;
   9.299 +}
   9.300 +#endif
   9.301 +
   9.302 +/* simple read routine.  will not correctly handle run length encoding */
   9.303 +static int rgbe_read_pixels(struct img_io *io, float *data, int numpixels)
   9.304 +{
   9.305 +	unsigned char rgbe[4];
   9.306 +
   9.307 +	while(numpixels-- > 0) {
   9.308 +		if(io->read(rgbe, sizeof(rgbe), io->uptr) < 1)
   9.309 +			return rgbe_error(rgbe_read_error, NULL);
   9.310 +		rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe);
   9.311 +		data += RGBE_DATA_SIZE;
   9.312 +	}
   9.313 +	return RGBE_RETURN_SUCCESS;
   9.314 +}
   9.315 +
   9.316 +#if 0
   9.317 +/* The code below is only needed for the run-length encoded files. */
   9.318 +
   9.319 +/* Run length encoding adds considerable complexity but does */
   9.320 +
   9.321 +/* save some space.  For each scanline, each channel (r,g,b,e) is */
   9.322 +
   9.323 +/* encoded separately for better compression. */
   9.324 +
   9.325 +static int rgbe_write_bytes_rle(struct img_io *io, unsigned char *data, int numbytes)
   9.326 +{
   9.327 +#define MINRUNLENGTH 4
   9.328 +	int cur, beg_run, run_count, old_run_count, nonrun_count;
   9.329 +	unsigned char buf[2];
   9.330 +
   9.331 +	cur = 0;
   9.332 +	while(cur < numbytes) {
   9.333 +		beg_run = cur;
   9.334 +		/* find next run of length at least 4 if one exists */
   9.335 +		run_count = old_run_count = 0;
   9.336 +		while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) {
   9.337 +			beg_run += run_count;
   9.338 +			old_run_count = run_count;
   9.339 +			run_count = 1;
   9.340 +			while((beg_run + run_count < numbytes) && (run_count < 127)
   9.341 +				  && (data[beg_run] == data[beg_run + run_count]))
   9.342 +				run_count++;
   9.343 +		}
   9.344 +		/* if data before next big run is a short run then write it as such */
   9.345 +		if((old_run_count > 1) && (old_run_count == beg_run - cur)) {
   9.346 +			buf[0] = 128 + old_run_count;	/*write short run */
   9.347 +			buf[1] = data[cur];
   9.348 +			if(fwrite(buf, sizeof(buf[0]) * 2, 1, fp) < 1)
   9.349 +				return rgbe_error(rgbe_write_error, NULL);
   9.350 +			cur = beg_run;
   9.351 +		}
   9.352 +		/* write out bytes until we reach the start of the next run */
   9.353 +		while(cur < beg_run) {
   9.354 +			nonrun_count = beg_run - cur;
   9.355 +			if(nonrun_count > 128)
   9.356 +				nonrun_count = 128;
   9.357 +			buf[0] = nonrun_count;
   9.358 +			if(fwrite(buf, sizeof(buf[0]), 1, fp) < 1)
   9.359 +				return rgbe_error(rgbe_write_error, NULL);
   9.360 +			if(fwrite(&data[cur], sizeof(data[0]) * nonrun_count, 1, fp) < 1)
   9.361 +				return rgbe_error(rgbe_write_error, NULL);
   9.362 +			cur += nonrun_count;
   9.363 +		}
   9.364 +		/* write out next run if one was found */
   9.365 +		if(run_count >= MINRUNLENGTH) {
   9.366 +			buf[0] = 128 + run_count;
   9.367 +			buf[1] = data[beg_run];
   9.368 +			if(fwrite(buf, sizeof(buf[0]) * 2, 1, fp) < 1)
   9.369 +				return rgbe_error(rgbe_write_error, NULL);
   9.370 +			cur += run_count;
   9.371 +		}
   9.372 +	}
   9.373 +	return RGBE_RETURN_SUCCESS;
   9.374 +#undef MINRUNLENGTH
   9.375 +}
   9.376 +
   9.377 +static int rgbe_write_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines)
   9.378 +{
   9.379 +	unsigned char rgbe[4];
   9.380 +	unsigned char *buffer;
   9.381 +	int i, err;
   9.382 +
   9.383 +	if((scanline_width < 8) || (scanline_width > 0x7fff))
   9.384 +		/* run length encoding is not allowed so write flat */
   9.385 +		return rgbe_write_pixels(io, data, scanline_width * num_scanlines);
   9.386 +	buffer = (unsigned char *)malloc(sizeof(unsigned char) * 4 * scanline_width);
   9.387 +	if(buffer == NULL)
   9.388 +		/* no buffer space so write flat */
   9.389 +		return rgbe_write_pixels(fp, data, scanline_width * num_scanlines);
   9.390 +	while(num_scanlines-- > 0) {
   9.391 +		rgbe[0] = 2;
   9.392 +		rgbe[1] = 2;
   9.393 +		rgbe[2] = scanline_width >> 8;
   9.394 +		rgbe[3] = scanline_width & 0xFF;
   9.395 +		if(fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) {
   9.396 +			free(buffer);
   9.397 +			return rgbe_error(rgbe_write_error, NULL);
   9.398 +		}
   9.399 +		for(i = 0; i < scanline_width; i++) {
   9.400 +			float2rgbe(rgbe, data[RGBE_DATA_RED], data[RGBE_DATA_GREEN], data[RGBE_DATA_BLUE]);
   9.401 +			buffer[i] = rgbe[0];
   9.402 +			buffer[i + scanline_width] = rgbe[1];
   9.403 +			buffer[i + 2 * scanline_width] = rgbe[2];
   9.404 +			buffer[i + 3 * scanline_width] = rgbe[3];
   9.405 +			data += RGBE_DATA_SIZE;
   9.406 +		}
   9.407 +		/* write out each of the four channels separately run length encoded */
   9.408 +		/* first red, then green, then blue, then exponent */
   9.409 +		for(i = 0; i < 4; i++) {
   9.410 +			if((err = rgbe_write_bytes_rle(fp, &buffer[i * scanline_width],
   9.411 +										  scanline_width)) != RGBE_RETURN_SUCCESS) {
   9.412 +				free(buffer);
   9.413 +				return err;
   9.414 +			}
   9.415 +		}
   9.416 +	}
   9.417 +	free(buffer);
   9.418 +	return RGBE_RETURN_SUCCESS;
   9.419 +}
   9.420 +#endif
   9.421 +
   9.422 +static int rgbe_read_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines)
   9.423 +{
   9.424 +	unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end;
   9.425 +	int i, count;
   9.426 +	unsigned char buf[2];
   9.427 +
   9.428 +	if((scanline_width < 8) || (scanline_width > 0x7fff))
   9.429 +		/* run length encoding is not allowed so read flat */
   9.430 +		return rgbe_read_pixels(io, data, scanline_width * num_scanlines);
   9.431 +	scanline_buffer = NULL;
   9.432 +	/* read in each successive scanline */
   9.433 +	while(num_scanlines > 0) {
   9.434 +		if(io->read(rgbe, sizeof(rgbe), io->uptr) < 1) {
   9.435 +			free(scanline_buffer);
   9.436 +			return rgbe_error(rgbe_read_error, NULL);
   9.437 +		}
   9.438 +		if((rgbe[0] != 2) || (rgbe[1] != 2) || (rgbe[2] & 0x80)) {
   9.439 +			/* this file is not run length encoded */
   9.440 +			rgbe2float(&data[0], &data[1], &data[2], rgbe);
   9.441 +			data += RGBE_DATA_SIZE;
   9.442 +			free(scanline_buffer);
   9.443 +			return rgbe_read_pixels(io, data, scanline_width * num_scanlines - 1);
   9.444 +		}
   9.445 +		if((((int)rgbe[2]) << 8 | rgbe[3]) != scanline_width) {
   9.446 +			free(scanline_buffer);
   9.447 +			return rgbe_error(rgbe_format_error, "wrong scanline width");
   9.448 +		}
   9.449 +		if(scanline_buffer == NULL)
   9.450 +			scanline_buffer = (unsigned char *)
   9.451 +				malloc(sizeof(unsigned char) * 4 * scanline_width);
   9.452 +		if(scanline_buffer == NULL)
   9.453 +			return rgbe_error(rgbe_memory_error, "unable to allocate buffer space");
   9.454 +
   9.455 +		ptr = &scanline_buffer[0];
   9.456 +		/* read each of the four channels for the scanline into the buffer */
   9.457 +		for(i = 0; i < 4; i++) {
   9.458 +			ptr_end = &scanline_buffer[(i + 1) * scanline_width];
   9.459 +			while(ptr < ptr_end) {
   9.460 +				if(io->read(buf, sizeof(buf[0]) * 2, io->uptr) < 1) {
   9.461 +					free(scanline_buffer);
   9.462 +					return rgbe_error(rgbe_read_error, NULL);
   9.463 +				}
   9.464 +				if(buf[0] > 128) {
   9.465 +					/* a run of the same value */
   9.466 +					count = buf[0] - 128;
   9.467 +					if((count == 0) || (count > ptr_end - ptr)) {
   9.468 +						free(scanline_buffer);
   9.469 +						return rgbe_error(rgbe_format_error, "bad scanline data");
   9.470 +					}
   9.471 +					while(count-- > 0)
   9.472 +						*ptr++ = buf[1];
   9.473 +				} else {
   9.474 +					/* a non-run */
   9.475 +					count = buf[0];
   9.476 +					if((count == 0) || (count > ptr_end - ptr)) {
   9.477 +						free(scanline_buffer);
   9.478 +						return rgbe_error(rgbe_format_error, "bad scanline data");
   9.479 +					}
   9.480 +					*ptr++ = buf[1];
   9.481 +					if(--count > 0) {
   9.482 +						if(io->read(ptr, sizeof(*ptr) * count, io->uptr) < 1) {
   9.483 +							free(scanline_buffer);
   9.484 +							return rgbe_error(rgbe_read_error, NULL);
   9.485 +						}
   9.486 +						ptr += count;
   9.487 +					}
   9.488 +				}
   9.489 +			}
   9.490 +		}
   9.491 +		/* now convert data from buffer into floats */
   9.492 +		for(i = 0; i < scanline_width; i++) {
   9.493 +			rgbe[0] = scanline_buffer[i];
   9.494 +			rgbe[1] = scanline_buffer[i + scanline_width];
   9.495 +			rgbe[2] = scanline_buffer[i + 2 * scanline_width];
   9.496 +			rgbe[3] = scanline_buffer[i + 3 * scanline_width];
   9.497 +			rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe);
   9.498 +			data += RGBE_DATA_SIZE;
   9.499 +		}
   9.500 +		num_scanlines--;
   9.501 +	}
   9.502 +	free(scanline_buffer);
   9.503 +	return RGBE_RETURN_SUCCESS;
   9.504 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/libs/imago/file_tga.c	Sun Jun 07 17:25:49 2015 +0300
    10.3 @@ -0,0 +1,250 @@
    10.4 +/*
    10.5 +libimago - a multi-format image file input/output library.
    10.6 +Copyright (C) 2010-2015 John Tsiombikas <nuclear@member.fsf.org>
    10.7 +
    10.8 +This program is free software: you can redistribute it and/or modify
    10.9 +it under the terms of the GNU Lesser General Public License as published
   10.10 +by the Free Software Foundation, either version 3 of the License, or
   10.11 +(at your option) any later version.
   10.12 +
   10.13 +This program is distributed in the hope that it will be useful,
   10.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.16 +GNU Lesser General Public License for more details.
   10.17 +
   10.18 +You should have received a copy of the GNU Lesser General Public License
   10.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   10.20 +*/
   10.21 +
   10.22 +/* -- Targa (tga) module -- */
   10.23 +
   10.24 +#include <string.h>
   10.25 +#include <stdlib.h>
   10.26 +#include <stdint.h>
   10.27 +#include "imago2.h"
   10.28 +#include "ftype_module.h"
   10.29 +
   10.30 +
   10.31 +#if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
   10.32 +    (defined(__alpha__) || defined(__alpha)) || \
   10.33 +     defined(__arm__) || \
   10.34 +    (defined(__mips__) && defined(__MIPSEL__)) || \
   10.35 +     defined(__SYMBIAN32__) || \
   10.36 +     defined(__x86_64__) || \
   10.37 +     defined(__LITTLE_ENDIAN__)
   10.38 +/* little endian */
   10.39 +#define read_int16_le(f)	read_int16(f)
   10.40 +#else
   10.41 +/* big endian */
   10.42 +#define read_int16_le(f)	read_int16_inv(f)
   10.43 +#endif	/* endian check */
   10.44 +
   10.45 +
   10.46 +enum {
   10.47 +	IMG_NONE,
   10.48 +	IMG_CMAP,
   10.49 +	IMG_RGBA,
   10.50 +	IMG_BW,
   10.51 +
   10.52 +	IMG_RLE_CMAP = 9,
   10.53 +	IMG_RLE_RGBA,
   10.54 +	IMG_RLE_BW
   10.55 +};
   10.56 +
   10.57 +#define IS_RLE(x)	((x) >= IMG_RLE_CMAP)
   10.58 +#define IS_RGBA(x)	((x) == IMG_RGBA || (x) == IMG_RLE_RGBA)
   10.59 +
   10.60 +
   10.61 +struct tga_header {
   10.62 +	uint8_t idlen;			/* id field length */
   10.63 +	uint8_t cmap_type;		/* color map type (0:no color map, 1:color map present) */
   10.64 +	uint8_t img_type;		/* image type:
   10.65 +							 * 0: no image data
   10.66 +							 *	1: uncomp. color-mapped		 9: RLE color-mapped
   10.67 +							 *	2: uncomp. true color		10: RLE true color
   10.68 +							 *	3: uncomp. black/white		11: RLE black/white */
   10.69 +	uint16_t cmap_first;	/* color map first entry index */
   10.70 +	uint16_t cmap_len;		/* color map length */
   10.71 +	uint8_t cmap_entry_sz;	/* color map entry size */
   10.72 +	uint16_t img_x;			/* X-origin of the image */
   10.73 +	uint16_t img_y;			/* Y-origin of the image */
   10.74 +	uint16_t img_width;		/* image width */
   10.75 +	uint16_t img_height;	/* image height */
   10.76 +	uint8_t img_bpp;		/* bits per pixel */
   10.77 +	uint8_t img_desc;		/* descriptor:
   10.78 +							 * bits 0 - 3: alpha or overlay bits
   10.79 +							 * bits 5 & 4: origin (0 = bottom/left, 1 = top/right)
   10.80 +							 * bits 7 & 6: data interleaving */
   10.81 +};
   10.82 +
   10.83 +struct tga_footer {
   10.84 +	uint32_t ext_off;		/* extension area offset */
   10.85 +	uint32_t devdir_off;	/* developer directory offset */
   10.86 +	char sig[18];				/* signature with . and \0 */
   10.87 +};
   10.88 +
   10.89 +
   10.90 +static int check(struct img_io *io);
   10.91 +static int read(struct img_pixmap *img, struct img_io *io);
   10.92 +static int write(struct img_pixmap *img, struct img_io *io);
   10.93 +static int read_pixel(struct img_io *io, int rdalpha, uint32_t *pix);
   10.94 +static int16_t read_int16(struct img_io *io);
   10.95 +static int16_t read_int16_inv(struct img_io *io);
   10.96 +
   10.97 +int img_register_tga(void)
   10.98 +{
   10.99 +	static struct ftype_module mod = {".tga", check, read, write};
  10.100 +	return img_register_module(&mod);
  10.101 +}
  10.102 +
  10.103 +
  10.104 +static int check(struct img_io *io)
  10.105 +{
  10.106 +	struct tga_footer foot;
  10.107 +	int res = -1;
  10.108 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
  10.109 +	io->seek(-18, SEEK_END, io->uptr);
  10.110 +
  10.111 +	if(io->read(foot.sig, 17, io->uptr) < 17) {
  10.112 +		io->seek(pos, SEEK_SET, io->uptr);
  10.113 +		return -1;
  10.114 +	}
  10.115 +
  10.116 +	if(memcmp(foot.sig, "TRUEVISION-XFILE.", 17) == 0) {
  10.117 +		res = 0;
  10.118 +	}
  10.119 +	io->seek(pos, SEEK_SET, io->uptr);
  10.120 +	return res;
  10.121 +}
  10.122 +
  10.123 +static int iofgetc(struct img_io *io)
  10.124 +{
  10.125 +	char c;
  10.126 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
  10.127 +}
  10.128 +
  10.129 +static int read(struct img_pixmap *img, struct img_io *io)
  10.130 +{
  10.131 +	struct tga_header hdr;
  10.132 +	unsigned long x, y;
  10.133 +	int i, c;
  10.134 +	uint32_t ppixel = 0;
  10.135 +	int rle_mode = 0, rle_pix_left = 0;
  10.136 +	int rdalpha;
  10.137 +
  10.138 +	/* read header */
  10.139 +	hdr.idlen = iofgetc(io);
  10.140 +	hdr.cmap_type = iofgetc(io);
  10.141 +	hdr.img_type = iofgetc(io);
  10.142 +	hdr.cmap_first = read_int16_le(io);
  10.143 +	hdr.cmap_len = read_int16_le(io);
  10.144 +	hdr.cmap_entry_sz = iofgetc(io);
  10.145 +	hdr.img_x = read_int16_le(io);
  10.146 +	hdr.img_y = read_int16_le(io);
  10.147 +	hdr.img_width = read_int16_le(io);
  10.148 +	hdr.img_height = read_int16_le(io);
  10.149 +	hdr.img_bpp = iofgetc(io);
  10.150 +	if((c = iofgetc(io)) == -1) {
  10.151 +		return -1;
  10.152 +	}
  10.153 +	hdr.img_desc = c;
  10.154 +
  10.155 +	if(!IS_RGBA(hdr.img_type)) {
  10.156 +		fprintf(stderr, "only true color tga images supported\n");
  10.157 +		return -1;
  10.158 +	}
  10.159 +
  10.160 +	io->seek(hdr.idlen, SEEK_CUR, io);	/* skip the image ID */
  10.161 +
  10.162 +	/* skip the color map if it exists */
  10.163 +	if(hdr.cmap_type == 1) {
  10.164 +		io->seek(hdr.cmap_len * hdr.cmap_entry_sz / 8, SEEK_CUR, io);
  10.165 +	}
  10.166 +
  10.167 +	x = hdr.img_width;
  10.168 +	y = hdr.img_height;
  10.169 +	rdalpha = hdr.img_desc & 0xf;
  10.170 +
  10.171 +	/* TODO make this IMG_FMT_RGB24 if there's no alpha channel */
  10.172 +	if(img_set_pixels(img, x, y, IMG_FMT_RGBA32, 0) == -1) {
  10.173 +		return -1;
  10.174 +	}
  10.175 +
  10.176 +	for(i=0; i<y; i++) {
  10.177 +		uint32_t *ptr;
  10.178 +		int j;
  10.179 +
  10.180 +		ptr = (uint32_t*)img->pixels + ((hdr.img_desc & 0x20) ? i : y - (i + 1)) * x;
  10.181 +
  10.182 +		for(j=0; j<x; j++) {
  10.183 +			/* if the image is raw, then just read the next pixel */
  10.184 +			if(!IS_RLE(hdr.img_type)) {
  10.185 +				if(read_pixel(io, rdalpha, &ppixel) == -1) {
  10.186 +					return -1;
  10.187 +				}
  10.188 +			} else {
  10.189 +				/* otherwise, for RLE... */
  10.190 +
  10.191 +				/* if we have pixels left in the packet ... */
  10.192 +				if(rle_pix_left) {
  10.193 +					/* if it's a raw packet, read the next pixel, otherwise keep the same */
  10.194 +					if(!rle_mode) {
  10.195 +						if(read_pixel(io, rdalpha, &ppixel) == -1) {
  10.196 +							return -1;
  10.197 +						}
  10.198 +					}
  10.199 +					--rle_pix_left;
  10.200 +				} else {
  10.201 +					/* read RLE packet header */
  10.202 +					unsigned char phdr = iofgetc(io);
  10.203 +					rle_mode = (phdr & 128);		/* last bit shows the mode for this packet (1: rle, 0: raw) */
  10.204 +					rle_pix_left = (phdr & ~128);	/* the rest gives the count of pixels minus one (we also read one here, so no +1) */
  10.205 +					/* and read the first pixel of the packet */
  10.206 +					if(read_pixel(io, rdalpha, &ppixel) == -1) {
  10.207 +						return -1;
  10.208 +					}
  10.209 +				}
  10.210 +			}
  10.211 +
  10.212 +			*ptr++ = ppixel;
  10.213 +		}
  10.214 +	}
  10.215 +
  10.216 +	return 0;
  10.217 +}
  10.218 +
  10.219 +static int write(struct img_pixmap *img, struct img_io *io)
  10.220 +{
  10.221 +	return -1;	/* TODO */
  10.222 +}
  10.223 +
  10.224 +#define PACK_COLOR32(r,g,b,a) \
  10.225 +	((((a) & 0xff) << 24) | \
  10.226 +	 (((r) & 0xff) << 0) | \
  10.227 +	 (((g) & 0xff) << 8) | \
  10.228 +	 (((b) & 0xff) << 16))
  10.229 +
  10.230 +static int read_pixel(struct img_io *io, int rdalpha, uint32_t *pix)
  10.231 +{
  10.232 +	int r, g, b, a;
  10.233 +	b = iofgetc(io);
  10.234 +	g = iofgetc(io);
  10.235 +	r = iofgetc(io);
  10.236 +	a = rdalpha ? iofgetc(io) : 0xff;
  10.237 +	*pix = PACK_COLOR32(r, g, b, a);
  10.238 +	return a == -1 || r == -1 ? -1 : 0;
  10.239 +}
  10.240 +
  10.241 +static int16_t read_int16(struct img_io *io)
  10.242 +{
  10.243 +	int16_t v;
  10.244 +	io->read(&v, 2, io);
  10.245 +	return v;
  10.246 +}
  10.247 +
  10.248 +static int16_t read_int16_inv(struct img_io *io)
  10.249 +{
  10.250 +	int16_t v;
  10.251 +	io->read(&v, 2, io);
  10.252 +	return ((v >> 8) & 0xff) | (v << 8);
  10.253 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/libs/imago/ftype_module.c	Sun Jun 07 17:25:49 2015 +0300
    11.3 @@ -0,0 +1,118 @@
    11.4 +/*
    11.5 +libimago - a multi-format image file input/output library.
    11.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    11.7 +
    11.8 +This program is free software: you can redistribute it and/or modify
    11.9 +it under the terms of the GNU Lesser General Public License as published
   11.10 +by the Free Software Foundation, either version 3 of the License, or
   11.11 +(at your option) any later version.
   11.12 +
   11.13 +This program is distributed in the hope that it will be useful,
   11.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.16 +GNU Lesser General Public License for more details.
   11.17 +
   11.18 +You should have received a copy of the GNU Lesser General Public License
   11.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   11.20 +*/
   11.21 +
   11.22 +#include <stdlib.h>
   11.23 +#include <string.h>
   11.24 +#include "ftype_module.h"
   11.25 +
   11.26 +static struct list_node {
   11.27 +	struct ftype_module *module;
   11.28 +	struct list_node *next;
   11.29 +} *modules;
   11.30 +
   11.31 +/* defined in modules.c which is generated by configure */
   11.32 +void img_modules_init();
   11.33 +
   11.34 +static int done_init;
   11.35 +
   11.36 +int img_register_module(struct ftype_module *mod)
   11.37 +{
   11.38 +	struct list_node *node;
   11.39 +
   11.40 +	if(!(node = malloc(sizeof *node))) {
   11.41 +		return -1;
   11.42 +	}
   11.43 +
   11.44 +	node->module = mod;
   11.45 +	node->next = modules;
   11.46 +	modules = node;
   11.47 +	return 0;
   11.48 +}
   11.49 +
   11.50 +struct ftype_module *img_find_format_module(struct img_io *io)
   11.51 +{
   11.52 +	struct list_node *node;
   11.53 +
   11.54 +	if(!done_init) {
   11.55 +		img_modules_init();
   11.56 +		done_init = 1;
   11.57 +	}
   11.58 +
   11.59 +	node = modules;
   11.60 +	while(node) {
   11.61 +		if(node->module->check(io) != -1) {
   11.62 +			return node->module;
   11.63 +		}
   11.64 +		node = node->next;
   11.65 +	}
   11.66 +	return 0;
   11.67 +}
   11.68 +
   11.69 +struct ftype_module *img_guess_format(const char *fname)
   11.70 +{
   11.71 +	struct list_node *node;
   11.72 +	char *suffix;
   11.73 +	int suffix_len;
   11.74 +
   11.75 +	if(!done_init) {
   11.76 +		img_modules_init();
   11.77 +		done_init = 1;
   11.78 +	}
   11.79 +
   11.80 +	if(!(suffix = strrchr(fname, '.'))) {
   11.81 +		return 0;	/* no suffix, can't guess ... */
   11.82 +	}
   11.83 +	suffix_len = (int)strlen(suffix);
   11.84 +
   11.85 +	node = modules;
   11.86 +	while(node) {
   11.87 +		char *suflist = node->module->suffix;
   11.88 +		char *start, *end;
   11.89 +
   11.90 +		while(*suflist) {
   11.91 +			if(!(start = strstr(suflist, suffix))) {
   11.92 +				break;
   11.93 +			}
   11.94 +			end = start + suffix_len;
   11.95 +
   11.96 +			if(*end == ':' || *end == 0) {
   11.97 +				return node->module;	/* found it */
   11.98 +			}
   11.99 +			suflist = end;
  11.100 +		}
  11.101 +
  11.102 +		node = node->next;
  11.103 +	}
  11.104 +	return 0;
  11.105 +}
  11.106 +
  11.107 +struct ftype_module *img_get_module(int idx)
  11.108 +{
  11.109 +	struct list_node *node;
  11.110 +
  11.111 +	if(!done_init) {
  11.112 +		img_modules_init();
  11.113 +		done_init = 1;
  11.114 +	}
  11.115 +
  11.116 +	node = modules;
  11.117 +	while(node && idx--) {
  11.118 +		node = node->next;
  11.119 +	}
  11.120 +	return node->module;
  11.121 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/libs/imago/ftype_module.h	Sun Jun 07 17:25:49 2015 +0300
    12.3 @@ -0,0 +1,39 @@
    12.4 +/*
    12.5 +libimago - a multi-format image file input/output library.
    12.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    12.7 +
    12.8 +This program is free software: you can redistribute it and/or modify
    12.9 +it under the terms of the GNU Lesser General Public License as published
   12.10 +by the Free Software Foundation, either version 3 of the License, or
   12.11 +(at your option) any later version.
   12.12 +
   12.13 +This program is distributed in the hope that it will be useful,
   12.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   12.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12.16 +GNU Lesser General Public License for more details.
   12.17 +
   12.18 +You should have received a copy of the GNU Lesser General Public License
   12.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   12.20 +*/
   12.21 +
   12.22 +#ifndef FTYPE_MODULE_H_
   12.23 +#define FTYPE_MODULE_H_
   12.24 +
   12.25 +#include "imago2.h"
   12.26 +
   12.27 +struct ftype_module {
   12.28 +	char *suffix;	/* used for format autodetection during saving only */
   12.29 +
   12.30 +	int (*check)(struct img_io *io);
   12.31 +	int (*read)(struct img_pixmap *img, struct img_io *io);
   12.32 +	int (*write)(struct img_pixmap *img, struct img_io *io);
   12.33 +};
   12.34 +
   12.35 +int img_register_module(struct ftype_module *mod);
   12.36 +
   12.37 +struct ftype_module *img_find_format_module(struct img_io *io);
   12.38 +struct ftype_module *img_guess_format(const char *fname);
   12.39 +struct ftype_module *img_get_module(int idx);
   12.40 +
   12.41 +
   12.42 +#endif	/* FTYPE_MODULE_H_ */
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/libs/imago/imago2.c	Sun Jun 07 17:25:49 2015 +0300
    13.3 @@ -0,0 +1,449 @@
    13.4 +/*
    13.5 +libimago - a multi-format image file input/output library.
    13.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    13.7 +
    13.8 +This program is free software: you can redistribute it and/or modify
    13.9 +it under the terms of the GNU Lesser General Public License as published
   13.10 +by the Free Software Foundation, either version 3 of the License, or
   13.11 +(at your option) any later version.
   13.12 +
   13.13 +This program is distributed in the hope that it will be useful,
   13.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   13.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13.16 +GNU Lesser General Public License for more details.
   13.17 +
   13.18 +You should have received a copy of the GNU Lesser General Public License
   13.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   13.20 +*/
   13.21 +
   13.22 +#include <stdio.h>
   13.23 +#include <stdlib.h>
   13.24 +#include <string.h>
   13.25 +#include "imago2.h"
   13.26 +#include "ftype_module.h"
   13.27 +
   13.28 +static int pixel_size(enum img_fmt fmt);
   13.29 +static size_t def_read(void *buf, size_t bytes, void *uptr);
   13.30 +static size_t def_write(void *buf, size_t bytes, void *uptr);
   13.31 +static long def_seek(long offset, int whence, void *uptr);
   13.32 +
   13.33 +
   13.34 +void img_init(struct img_pixmap *img)
   13.35 +{
   13.36 +	img->pixels = 0;
   13.37 +	img->width = img->height = 0;
   13.38 +	img->fmt = IMG_FMT_RGBA32;
   13.39 +	img->pixelsz = pixel_size(img->fmt);
   13.40 +	img->name = 0;
   13.41 +}
   13.42 +
   13.43 +
   13.44 +void img_destroy(struct img_pixmap *img)
   13.45 +{
   13.46 +	free(img->pixels);
   13.47 +	img->pixels = 0;	/* just in case... */
   13.48 +	img->width = img->height = 0xbadbeef;
   13.49 +	free(img->name);
   13.50 +}
   13.51 +
   13.52 +struct img_pixmap *img_create(void)
   13.53 +{
   13.54 +	struct img_pixmap *p;
   13.55 +
   13.56 +	if(!(p = malloc(sizeof *p))) {
   13.57 +		return 0;
   13.58 +	}
   13.59 +	img_init(p);
   13.60 +	return p;
   13.61 +}
   13.62 +
   13.63 +void img_free(struct img_pixmap *img)
   13.64 +{
   13.65 +	img_destroy(img);
   13.66 +	free(img);
   13.67 +}
   13.68 +
   13.69 +int img_set_name(struct img_pixmap *img, const char *name)
   13.70 +{
   13.71 +	char *tmp;
   13.72 +
   13.73 +	if(!(tmp = malloc(strlen(name) + 1))) {
   13.74 +		return -1;
   13.75 +	}
   13.76 +	strcpy(tmp, name);
   13.77 +	img->name = tmp;
   13.78 +	return 0;
   13.79 +}
   13.80 +
   13.81 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
   13.82 +{
   13.83 +	if(img->pixels) {
   13.84 +		return img_convert(img, fmt);
   13.85 +	}
   13.86 +	img->fmt = fmt;
   13.87 +	return 0;
   13.88 +}
   13.89 +
   13.90 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
   13.91 +{
   13.92 +	return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
   13.93 +}
   13.94 +
   13.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
   13.96 +{
   13.97 +	void *newpix;
   13.98 +	int pixsz = pixel_size(fmt);
   13.99 +
  13.100 +	if(!(newpix = malloc(w * h * pixsz))) {
  13.101 +		return -1;
  13.102 +	}
  13.103 +
  13.104 +	if(pix) {
  13.105 +		memcpy(newpix, pix, w * h * pixsz);
  13.106 +	} else {
  13.107 +		memset(newpix, 0, w * h * pixsz);
  13.108 +	}
  13.109 +
  13.110 +	free(img->pixels);
  13.111 +	img->pixels = newpix;
  13.112 +	img->width = w;
  13.113 +	img->height = h;
  13.114 +	img->pixelsz = pixsz;
  13.115 +	img->fmt = fmt;
  13.116 +	return 0;
  13.117 +}
  13.118 +
  13.119 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
  13.120 +{
  13.121 +	struct img_pixmap img;
  13.122 +
  13.123 +	img_init(&img);
  13.124 +
  13.125 +	if(img_load(&img, fname) == -1) {
  13.126 +		return 0;
  13.127 +	}
  13.128 +	if(img.fmt != fmt) {
  13.129 +		if(img_convert(&img, fmt) == -1) {
  13.130 +			img_destroy(&img);
  13.131 +			return 0;
  13.132 +		}
  13.133 +	}
  13.134 +
  13.135 +	*xsz = img.width;
  13.136 +	*ysz = img.height;
  13.137 +	return img.pixels;
  13.138 +}
  13.139 +
  13.140 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
  13.141 +{
  13.142 +	struct img_pixmap img;
  13.143 +
  13.144 +	img_init(&img);
  13.145 +	img.fmt = fmt;
  13.146 +	img.name = (char*)fname;
  13.147 +	img.width = xsz;
  13.148 +	img.height = ysz;
  13.149 +	img.pixels = pix;
  13.150 +
  13.151 +	return img_save(&img, fname);
  13.152 +}
  13.153 +
  13.154 +void img_free_pixels(void *pix)
  13.155 +{
  13.156 +	free(pix);
  13.157 +}
  13.158 +
  13.159 +int img_load(struct img_pixmap *img, const char *fname)
  13.160 +{
  13.161 +	int res;
  13.162 +	FILE *fp;
  13.163 +
  13.164 +	if(!(fp = fopen(fname, "rb"))) {
  13.165 +		return -1;
  13.166 +	}
  13.167 +	res = img_read_file(img, fp);
  13.168 +	fclose(fp);
  13.169 +	return res;
  13.170 +}
  13.171 +
  13.172 +/* TODO implement filetype selection */
  13.173 +int img_save(struct img_pixmap *img, const char *fname)
  13.174 +{
  13.175 +	int res;
  13.176 +	FILE *fp;
  13.177 +
  13.178 +	img_set_name(img, fname);
  13.179 +
  13.180 +	if(!(fp = fopen(fname, "wb"))) {
  13.181 +		return -1;
  13.182 +	}
  13.183 +	res = img_write_file(img, fp);
  13.184 +	fclose(fp);
  13.185 +	return res;
  13.186 +}
  13.187 +
  13.188 +int img_read_file(struct img_pixmap *img, FILE *fp)
  13.189 +{
  13.190 +	struct img_io io = {0, def_read, def_write, def_seek};
  13.191 +
  13.192 +	io.uptr = fp;
  13.193 +	return img_read(img, &io);
  13.194 +}
  13.195 +
  13.196 +int img_write_file(struct img_pixmap *img, FILE *fp)
  13.197 +{
  13.198 +	struct img_io io = {0, def_read, def_write, def_seek};
  13.199 +
  13.200 +	io.uptr = fp;
  13.201 +	return img_write(img, &io);
  13.202 +}
  13.203 +
  13.204 +int img_read(struct img_pixmap *img, struct img_io *io)
  13.205 +{
  13.206 +	struct ftype_module *mod;
  13.207 +
  13.208 +	if((mod = img_find_format_module(io))) {
  13.209 +		return mod->read(img, io);
  13.210 +	}
  13.211 +	return -1;
  13.212 +}
  13.213 +
  13.214 +int img_write(struct img_pixmap *img, struct img_io *io)
  13.215 +{
  13.216 +	struct ftype_module *mod;
  13.217 +
  13.218 +	if(!img->name || !(mod = img_guess_format(img->name))) {
  13.219 +		/* TODO throw some sort of warning? */
  13.220 +		/* TODO implement some sort of module priority or let the user specify? */
  13.221 +		if(!(mod = img_get_module(0))) {
  13.222 +			return -1;
  13.223 +		}
  13.224 +	}
  13.225 +
  13.226 +	return mod->write(img, io);
  13.227 +}
  13.228 +
  13.229 +int img_to_float(struct img_pixmap *img)
  13.230 +{
  13.231 +	enum img_fmt targ_fmt;
  13.232 +
  13.233 +	switch(img->fmt) {
  13.234 +	case IMG_FMT_GREY8:
  13.235 +		targ_fmt = IMG_FMT_GREYF;
  13.236 +		break;
  13.237 +
  13.238 +	case IMG_FMT_RGB24:
  13.239 +		targ_fmt = IMG_FMT_RGBF;
  13.240 +		break;
  13.241 +
  13.242 +	case IMG_FMT_RGBA32:
  13.243 +		targ_fmt = IMG_FMT_RGBAF;
  13.244 +		break;
  13.245 +
  13.246 +	default:
  13.247 +		return 0;	/* already float */
  13.248 +	}
  13.249 +
  13.250 +	return img_convert(img, targ_fmt);
  13.251 +}
  13.252 +
  13.253 +int img_to_integer(struct img_pixmap *img)
  13.254 +{
  13.255 +	enum img_fmt targ_fmt;
  13.256 +
  13.257 +	switch(img->fmt) {
  13.258 +	case IMG_FMT_GREYF:
  13.259 +		targ_fmt = IMG_FMT_GREY8;
  13.260 +		break;
  13.261 +
  13.262 +	case IMG_FMT_RGBF:
  13.263 +		targ_fmt = IMG_FMT_RGB24;
  13.264 +		break;
  13.265 +
  13.266 +	case IMG_FMT_RGBAF:
  13.267 +		targ_fmt = IMG_FMT_RGBA32;
  13.268 +		break;
  13.269 +
  13.270 +	default:
  13.271 +		return 0;	/* already integer */
  13.272 +	}
  13.273 +
  13.274 +	return img_convert(img, targ_fmt);
  13.275 +}
  13.276 +
  13.277 +int img_is_float(struct img_pixmap *img)
  13.278 +{
  13.279 +	return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
  13.280 +}
  13.281 +
  13.282 +int img_has_alpha(struct img_pixmap *img)
  13.283 +{
  13.284 +	if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
  13.285 +		return 1;
  13.286 +	}
  13.287 +	return 0;
  13.288 +}
  13.289 +
  13.290 +
  13.291 +void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
  13.292 +{
  13.293 +	char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
  13.294 +	memcpy(dest, pixel, img->pixelsz);
  13.295 +}
  13.296 +
  13.297 +void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
  13.298 +{
  13.299 +	char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
  13.300 +	memcpy(pixel, dest, img->pixelsz);
  13.301 +}
  13.302 +
  13.303 +void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
  13.304 +{
  13.305 +	img_setpixel4i(img, x, y, pix, pix, pix, pix);
  13.306 +}
  13.307 +
  13.308 +void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
  13.309 +{
  13.310 +	img_setpixel4f(img, x, y, pix, pix, pix, pix);
  13.311 +}
  13.312 +
  13.313 +void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
  13.314 +{
  13.315 +	if(img_is_float(img)) {
  13.316 +		img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  13.317 +	} else {
  13.318 +		unsigned char pixel[4];
  13.319 +		pixel[0] = r;
  13.320 +		pixel[1] = g;
  13.321 +		pixel[2] = b;
  13.322 +		pixel[3] = a;
  13.323 +
  13.324 +		img_setpixel(img, x, y, pixel);
  13.325 +	}
  13.326 +}
  13.327 +
  13.328 +void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
  13.329 +{
  13.330 +	if(img_is_float(img)) {
  13.331 +		float pixel[4];
  13.332 +		pixel[0] = r;
  13.333 +		pixel[1] = g;
  13.334 +		pixel[2] = b;
  13.335 +		pixel[3] = a;
  13.336 +
  13.337 +		img_setpixel(img, x, y, pixel);
  13.338 +	} else {
  13.339 +		img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
  13.340 +	}
  13.341 +}
  13.342 +
  13.343 +void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
  13.344 +{
  13.345 +	int junk[3];
  13.346 +	img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
  13.347 +}
  13.348 +
  13.349 +void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
  13.350 +{
  13.351 +	float junk[3];
  13.352 +	img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
  13.353 +}
  13.354 +
  13.355 +void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
  13.356 +{
  13.357 +	if(img_is_float(img)) {
  13.358 +		float pixel[4] = {0, 0, 0, 0};
  13.359 +		img_getpixel(img, x, y, pixel);
  13.360 +		*r = pixel[0] * 255.0;
  13.361 +		*g = pixel[1] * 255.0;
  13.362 +		*b = pixel[2] * 255.0;
  13.363 +		*a = pixel[3] * 255.0;
  13.364 +	} else {
  13.365 +		unsigned char pixel[4];
  13.366 +		img_getpixel(img, x, y, pixel);
  13.367 +		*r = pixel[0];
  13.368 +		*g = pixel[1];
  13.369 +		*b = pixel[2];
  13.370 +		*a = pixel[3];
  13.371 +	}
  13.372 +}
  13.373 +
  13.374 +void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
  13.375 +{
  13.376 +	if(img_is_float(img)) {
  13.377 +		float pixel[4] = {0, 0, 0, 0};
  13.378 +		img_getpixel(img, x, y, pixel);
  13.379 +		*r = pixel[0];
  13.380 +		*g = pixel[1];
  13.381 +		*b = pixel[2];
  13.382 +		*a = pixel[3];
  13.383 +	} else {
  13.384 +		unsigned char pixel[4];
  13.385 +		img_getpixel(img, x, y, pixel);
  13.386 +		*r = pixel[0] / 255.0;
  13.387 +		*g = pixel[1] / 255.0;
  13.388 +		*b = pixel[2] / 255.0;
  13.389 +		*a = pixel[3] / 255.0;
  13.390 +	}
  13.391 +}
  13.392 +
  13.393 +void img_io_set_user_data(struct img_io *io, void *uptr)
  13.394 +{
  13.395 +	io->uptr = uptr;
  13.396 +}
  13.397 +
  13.398 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
  13.399 +{
  13.400 +	io->read = read;
  13.401 +}
  13.402 +
  13.403 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
  13.404 +{
  13.405 +	io->write = write;
  13.406 +}
  13.407 +
  13.408 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
  13.409 +{
  13.410 +	io->seek = seek;
  13.411 +}
  13.412 +
  13.413 +
  13.414 +static int pixel_size(enum img_fmt fmt)
  13.415 +{
  13.416 +	switch(fmt) {
  13.417 +	case IMG_FMT_GREY8:
  13.418 +		return 1;
  13.419 +	case IMG_FMT_RGB24:
  13.420 +		return 3;
  13.421 +	case IMG_FMT_RGBA32:
  13.422 +		return 4;
  13.423 +	case IMG_FMT_GREYF:
  13.424 +		return sizeof(float);
  13.425 +	case IMG_FMT_RGBF:
  13.426 +		return 3 * sizeof(float);
  13.427 +	case IMG_FMT_RGBAF:
  13.428 +		return 4 * sizeof(float);
  13.429 +	default:
  13.430 +		break;
  13.431 +	}
  13.432 +	return 0;
  13.433 +}
  13.434 +
  13.435 +static size_t def_read(void *buf, size_t bytes, void *uptr)
  13.436 +{
  13.437 +	return uptr ? fread(buf, 1, bytes, uptr) : 0;
  13.438 +}
  13.439 +
  13.440 +static size_t def_write(void *buf, size_t bytes, void *uptr)
  13.441 +{
  13.442 +	return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
  13.443 +}
  13.444 +
  13.445 +static long def_seek(long offset, int whence, void *uptr)
  13.446 +{
  13.447 +	if(!uptr || fseek(uptr, offset, whence) == -1) {
  13.448 +		return -1;
  13.449 +	}
  13.450 +	return ftell(uptr);
  13.451 +}
  13.452 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/libs/imago/imago2.h	Sun Jun 07 17:25:49 2015 +0300
    14.3 @@ -0,0 +1,222 @@
    14.4 +/*
    14.5 +libimago - a multi-format image file input/output library.
    14.6 +Copyright (C) 2010-2012 John Tsiombikas <nuclear@member.fsf.org>
    14.7 +
    14.8 +This program is free software: you can redistribute it and/or modify
    14.9 +it under the terms of the GNU Lesser General Public License as published
   14.10 +by the Free Software Foundation, either version 3 of the License, or
   14.11 +(at your option) any later version.
   14.12 +
   14.13 +This program is distributed in the hope that it will be useful,
   14.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   14.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14.16 +GNU Lesser General Public License for more details.
   14.17 +
   14.18 +You should have received a copy of the GNU Lesser General Public License
   14.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   14.20 +*/
   14.21 +
   14.22 +#ifndef IMAGO2_H_
   14.23 +#define IMAGO2_H_
   14.24 +
   14.25 +#include <stdio.h>
   14.26 +
   14.27 +#ifdef __cplusplus
   14.28 +#define IMG_OPTARG(arg, val)	arg = val
   14.29 +#else
   14.30 +#define IMG_OPTARG(arg, val)	arg
   14.31 +#endif
   14.32 +
   14.33 +/* XXX if you change this make sure to also change pack/unpack arrays in conv.c */
   14.34 +enum img_fmt {
   14.35 +	IMG_FMT_GREY8,
   14.36 +	IMG_FMT_RGB24,
   14.37 +	IMG_FMT_RGBA32,
   14.38 +	IMG_FMT_GREYF,
   14.39 +	IMG_FMT_RGBF,
   14.40 +	IMG_FMT_RGBAF,
   14.41 +
   14.42 +	NUM_IMG_FMT
   14.43 +};
   14.44 +
   14.45 +struct img_pixmap {
   14.46 +	void *pixels;
   14.47 +	int width, height;
   14.48 +	enum img_fmt fmt;
   14.49 +	int pixelsz;
   14.50 +	char *name;
   14.51 +};
   14.52 +
   14.53 +struct img_io {
   14.54 +	void *uptr;	/* user-data */
   14.55 +
   14.56 +	size_t (*read)(void *buf, size_t bytes, void *uptr);
   14.57 +	size_t (*write)(void *buf, size_t bytes, void *uptr);
   14.58 +	long (*seek)(long offs, int whence, void *uptr);
   14.59 +};
   14.60 +
   14.61 +#ifdef __cplusplus
   14.62 +extern "C" {
   14.63 +#endif
   14.64 +
   14.65 +/* initialize the img_pixmap structure */
   14.66 +void img_init(struct img_pixmap *img);
   14.67 +/* destroys the img_pixmap structure, freeing the pixel buffer (if available)
   14.68 + * and any other memory held by the pixmap.
   14.69 + */
   14.70 +void img_destroy(struct img_pixmap *img);
   14.71 +
   14.72 +/* convenience function that allocates an img_pixmap struct and then initializes it.
   14.73 + * returns null if the malloc fails.
   14.74 + */
   14.75 +struct img_pixmap *img_create(void);
   14.76 +/* frees a pixmap previously allocated with img_create (free followed by img_destroy) */
   14.77 +void img_free(struct img_pixmap *img);
   14.78 +
   14.79 +int img_set_name(struct img_pixmap *img, const char *name);
   14.80 +
   14.81 +/* set the image pixel format */
   14.82 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt);
   14.83 +
   14.84 +/* copies one pixmap to another.
   14.85 + * equivalent to: img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels)
   14.86 + */
   14.87 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src);
   14.88 +
   14.89 +/* allocates a pixel buffer of the specified dimensions and format, and copies the
   14.90 + * pixels given through the pix pointer into it.
   14.91 + * the pix pointer can be null, in which case there's no copy, just allocation.
   14.92 + *
   14.93 + * C++: fmt and pix have default parameters IMG_FMT_RGBA32 and null respectively.
   14.94 + */
   14.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32), IMG_OPTARG(void *pix, 0));
   14.96 +
   14.97 +/* Simplified image loading
   14.98 + * Loads the specified file, and returns a pointer to an array of pixels of the
   14.99 + * requested pixel format. The width and height of the image are returned through
  14.100 + * the xsz and ysz pointers.
  14.101 + * If the image cannot be loaded, the function returns null.
  14.102 + *
  14.103 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  14.104 + */
  14.105 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  14.106 +
  14.107 +/* Simplified image saving
  14.108 + * Reads an array of pixels supplied through the pix pointer, of dimensions xsz
  14.109 + * and ysz, and pixel-format fmt, and saves it to a file.
  14.110 + * The output filetype is guessed by the filename suffix.
  14.111 + *
  14.112 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  14.113 + */
  14.114 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  14.115 +
  14.116 +/* Frees the memory allocated by img_load_pixels */
  14.117 +void img_free_pixels(void *pix);
  14.118 +
  14.119 +/* Loads an image file into the supplied pixmap */
  14.120 +int img_load(struct img_pixmap *img, const char *fname);
  14.121 +/* Saves the supplied pixmap to a file. The output filetype is guessed by the filename suffix */
  14.122 +int img_save(struct img_pixmap *img, const char *fname);
  14.123 +
  14.124 +/* Reads an image from an open FILE* into the supplied pixmap */
  14.125 +int img_read_file(struct img_pixmap *img, FILE *fp);
  14.126 +/* Writes the supplied pixmap to an open FILE* */
  14.127 +int img_write_file(struct img_pixmap *img, FILE *fp);
  14.128 +
  14.129 +/* Reads an image using user-defined file-i/o functions (see img_io_set_*) */
  14.130 +int img_read(struct img_pixmap *img, struct img_io *io);
  14.131 +/* Writes an image using user-defined file-i/o functions (see img_io_set_*) */
  14.132 +int img_write(struct img_pixmap *img, struct img_io *io);
  14.133 +
  14.134 +/* Converts an image to the specified pixel format */
  14.135 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt);
  14.136 +
  14.137 +/* Converts an image from an integer pixel format to the corresponding floating point one */
  14.138 +int img_to_float(struct img_pixmap *img);
  14.139 +/* Converts an image from a floating point pixel format to the corresponding integer one */
  14.140 +int img_to_integer(struct img_pixmap *img);
  14.141 +
  14.142 +/* Returns non-zero (true) if the supplied image is in a floating point pixel format */
  14.143 +int img_is_float(struct img_pixmap *img);
  14.144 +/* Returns non-zero (true) if the supplied image has an alpha channel */
  14.145 +int img_has_alpha(struct img_pixmap *img);
  14.146 +
  14.147 +
  14.148 +/* don't use these for anything performance-critical */
  14.149 +void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel);
  14.150 +void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel);
  14.151 +
  14.152 +void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix);
  14.153 +void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix);
  14.154 +void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a);
  14.155 +void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a);
  14.156 +
  14.157 +void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix);
  14.158 +void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix);
  14.159 +void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a);
  14.160 +void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a);
  14.161 +
  14.162 +
  14.163 +/* OpenGL helper functions */
  14.164 +
  14.165 +/* Returns the equivalent OpenGL "format" as expected by the 7th argument of glTexImage2D */
  14.166 +unsigned int img_fmt_glfmt(enum img_fmt fmt);
  14.167 +/* Returns the equivalent OpenGL "type" as expected by the 8th argument of glTexImage2D */
  14.168 +unsigned int img_fmt_gltype(enum img_fmt fmt);
  14.169 +/* Returns the equivalent OpenGL "internal format" as expected by the 3rd argument of glTexImage2D */
  14.170 +unsigned int img_fmt_glintfmt(enum img_fmt fmt);
  14.171 +
  14.172 +/* Same as above, based on the pixel format of the supplied image */
  14.173 +unsigned int img_glfmt(struct img_pixmap *img);
  14.174 +unsigned int img_gltype(struct img_pixmap *img);
  14.175 +unsigned int img_glintfmt(struct img_pixmap *img);
  14.176 +
  14.177 +/* Creates an OpenGL texture from the image, and returns the texture id, or 0 for failure */
  14.178 +unsigned int img_gltexture(struct img_pixmap *img);
  14.179 +
  14.180 +/* Load an image and create an OpenGL texture out of it */
  14.181 +unsigned int img_gltexture_load(const char *fname);
  14.182 +unsigned int img_gltexture_read_file(FILE *fp);
  14.183 +unsigned int img_gltexture_read(struct img_io *io);
  14.184 +
  14.185 +/* These functions can be used to fill an img_io struct before it's passed to
  14.186 + * one of the user-defined i/o image reading/writing functions (img_read/img_write).
  14.187 + *
  14.188 + * User-defined i/o functions:
  14.189 + *
  14.190 + * - size_t read_func(void *buffer, size_t bytes, void *user_ptr)
  14.191 + * Must try to fill the buffer with the specified number of bytes, and return
  14.192 + * the number of bytes actually read.
  14.193 + *
  14.194 + * - size_t write_func(void *buffer, size_t bytes, void *user_ptr)
  14.195 + * Must write the specified number of bytes from the supplied buffer and return
  14.196 + * the number of bytes actually written.
  14.197 + *
  14.198 + * - long seek_func(long offset, int whence, void *user_ptr)
  14.199 + * Must seek offset bytes from: the beginning of the file if whence is SEEK_SET,
  14.200 + * the current position if whence is SEEK_CUR, or the end of the file if whence is
  14.201 + * SEEK_END, and return the resulting file offset from the beginning of the file.
  14.202 + * (i.e. seek_func(0, SEEK_CUR, user_ptr); must be equivalent to an ftell).
  14.203 + *
  14.204 + * All three functions get the user-data pointer set through img_io_set_user_data
  14.205 + * as their last argument.
  14.206 + *
  14.207 + * Note: obviously you don't need to set a write function if you're only going
  14.208 + * to call img_read, or the read and seek function if you're only going to call
  14.209 + * img_write.
  14.210 + *
  14.211 + * Note: if the user-supplied write function is buffered, make sure to flush
  14.212 + * (or close the file) after img_write returns.
  14.213 + */
  14.214 +void img_io_set_user_data(struct img_io *io, void *uptr);
  14.215 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*));
  14.216 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*));
  14.217 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*));
  14.218 +
  14.219 +
  14.220 +#ifdef __cplusplus
  14.221 +}
  14.222 +#endif
  14.223 +
  14.224 +
  14.225 +#endif	/* IMAGO_H_ */
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/libs/imago/imago_gl.c	Sun Jun 07 17:25:49 2015 +0300
    15.3 @@ -0,0 +1,231 @@
    15.4 +#include "imago2.h"
    15.5 +
    15.6 +/* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
    15.7 +#define GL_UNSIGNED_BYTE		0x1401
    15.8 +#define GL_FLOAT				0x1406
    15.9 +
   15.10 +#define GL_LUMINANCE			0x1909
   15.11 +#define GL_RGB					0x1907
   15.12 +#define GL_RGBA					0x1908
   15.13 +
   15.14 +#define GL_RGBA32F				0x8814
   15.15 +#define GL_RGB32F				0x8815
   15.16 +#define GL_LUMINANCE32F			0x8818
   15.17 +
   15.18 +#define GL_TEXTURE_2D			0x0de1
   15.19 +#define GL_TEXTURE_WRAP_S		0x2802
   15.20 +#define GL_TEXTURE_WRAP_T		0x2803
   15.21 +#define GL_TEXTURE_MAG_FILTER	0x2800
   15.22 +#define GL_TEXTURE_MIN_FILTER	0x2801
   15.23 +#define GL_LINEAR				0x2601
   15.24 +#define GL_REPEAT				0x2901
   15.25 +
   15.26 +
   15.27 +typedef unsigned int GLenum;
   15.28 +typedef unsigned int GLuint;
   15.29 +typedef int GLint;
   15.30 +typedef int GLsizei;
   15.31 +typedef void GLvoid;
   15.32 +
   15.33 +/* for the same reason I'll load GL functions dynamically */
   15.34 +#ifndef WIN32
   15.35 +typedef void (*gl_gen_textures_func)(GLsizei, GLuint*);
   15.36 +typedef void (*gl_bind_texture_func)(GLenum, GLuint);
   15.37 +typedef void (*gl_tex_parameteri_func)(GLenum, GLenum, GLint);
   15.38 +typedef void (*gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
   15.39 +#else
   15.40 +typedef void (__stdcall *gl_gen_textures_func)(GLsizei, GLuint*);
   15.41 +typedef void (__stdcall *gl_bind_texture_func)(GLenum, GLuint);
   15.42 +typedef void (__stdcall *gl_tex_parameteri_func)(GLenum, GLenum, GLint);
   15.43 +typedef void (__stdcall *gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
   15.44 +#endif
   15.45 +
   15.46 +static gl_gen_textures_func gl_gen_textures;
   15.47 +static gl_bind_texture_func gl_bind_texture;
   15.48 +static gl_tex_parameteri_func gl_tex_parameteri;
   15.49 +static gl_tex_image2d_func gl_tex_image2d;
   15.50 +
   15.51 +static int load_glfunc(void);
   15.52 +
   15.53 +unsigned int img_fmt_glfmt(enum img_fmt fmt)
   15.54 +{
   15.55 +	switch(fmt) {
   15.56 +	case IMG_FMT_GREY8:
   15.57 +	case IMG_FMT_GREYF:
   15.58 +		return GL_LUMINANCE;
   15.59 +
   15.60 +	case IMG_FMT_RGB24:
   15.61 +	case IMG_FMT_RGBF:
   15.62 +		return GL_RGB;
   15.63 +
   15.64 +	case IMG_FMT_RGBA32:
   15.65 +	case IMG_FMT_RGBAF:
   15.66 +		return GL_RGBA;
   15.67 +
   15.68 +	default:
   15.69 +		break;
   15.70 +	}
   15.71 +	return 0;
   15.72 +}
   15.73 +
   15.74 +unsigned int img_fmt_gltype(enum img_fmt fmt)
   15.75 +{
   15.76 +	switch(fmt) {
   15.77 +	case IMG_FMT_GREY8:
   15.78 +	case IMG_FMT_RGB24:
   15.79 +	case IMG_FMT_RGBA32:
   15.80 +		return GL_UNSIGNED_BYTE;
   15.81 +
   15.82 +	case IMG_FMT_GREYF:
   15.83 +	case IMG_FMT_RGBF:
   15.84 +	case IMG_FMT_RGBAF:
   15.85 +		return GL_FLOAT;
   15.86 +
   15.87 +	default:
   15.88 +		break;
   15.89 +	}
   15.90 +	return 0;
   15.91 +}
   15.92 +
   15.93 +unsigned int img_fmt_glintfmt(enum img_fmt fmt)
   15.94 +{
   15.95 +	switch(fmt) {
   15.96 +	case IMG_FMT_GREY8:
   15.97 +		return GL_LUMINANCE;
   15.98 +	case IMG_FMT_RGB24:
   15.99 +		return GL_RGB;
  15.100 +	case IMG_FMT_RGBA32:
  15.101 +		return GL_RGBA;
  15.102 +	case IMG_FMT_GREYF:
  15.103 +		return GL_LUMINANCE32F;
  15.104 +	case IMG_FMT_RGBF:
  15.105 +		return GL_RGB32F;
  15.106 +	case IMG_FMT_RGBAF:
  15.107 +		return GL_RGBA32F;
  15.108 +	default:
  15.109 +		break;
  15.110 +	}
  15.111 +	return 0;
  15.112 +}
  15.113 +
  15.114 +unsigned int img_glfmt(struct img_pixmap *img)
  15.115 +{
  15.116 +	return img_fmt_glfmt(img->fmt);
  15.117 +}
  15.118 +
  15.119 +unsigned int img_gltype(struct img_pixmap *img)
  15.120 +{
  15.121 +	return img_fmt_gltype(img->fmt);
  15.122 +}
  15.123 +
  15.124 +unsigned int img_glintfmt(struct img_pixmap *img)
  15.125 +{
  15.126 +	return img_fmt_glintfmt(img->fmt);
  15.127 +}
  15.128 +
  15.129 +unsigned int img_gltexture(struct img_pixmap *img)
  15.130 +{
  15.131 +	unsigned int tex;
  15.132 +	unsigned int intfmt, fmt, type;
  15.133 +
  15.134 +	if(!gl_gen_textures) {
  15.135 +		if(load_glfunc() == -1) {
  15.136 +			fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
  15.137 +			return 0;
  15.138 +		}
  15.139 +	}
  15.140 +
  15.141 +	intfmt = img_glintfmt(img);
  15.142 +	fmt = img_glfmt(img);
  15.143 +	type = img_gltype(img);
  15.144 +
  15.145 +	gl_gen_textures(1, &tex);
  15.146 +	gl_bind_texture(GL_TEXTURE_2D, tex);
  15.147 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  15.148 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  15.149 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  15.150 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  15.151 +	gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
  15.152 +	return tex;
  15.153 +}
  15.154 +
  15.155 +unsigned int img_gltexture_load(const char *fname)
  15.156 +{
  15.157 +	struct img_pixmap img;
  15.158 +	unsigned int tex;
  15.159 +
  15.160 +	img_init(&img);
  15.161 +	if(img_load(&img, fname) == -1) {
  15.162 +		img_destroy(&img);
  15.163 +		return 0;
  15.164 +	}
  15.165 +
  15.166 +	tex = img_gltexture(&img);
  15.167 +	img_destroy(&img);
  15.168 +	return tex;
  15.169 +}
  15.170 +
  15.171 +unsigned int img_gltexture_read_file(FILE *fp)
  15.172 +{
  15.173 +	struct img_pixmap img;
  15.174 +	unsigned int tex;
  15.175 +
  15.176 +	img_init(&img);
  15.177 +	if(img_read_file(&img, fp) == -1) {
  15.178 +		img_destroy(&img);
  15.179 +		return 0;
  15.180 +	}
  15.181 +
  15.182 +	tex = img_gltexture(&img);
  15.183 +	img_destroy(&img);
  15.184 +	return tex;
  15.185 +}
  15.186 +
  15.187 +unsigned int img_gltexture_read(struct img_io *io)
  15.188 +{
  15.189 +	struct img_pixmap img;
  15.190 +	unsigned int tex;
  15.191 +
  15.192 +	img_init(&img);
  15.193 +	if(img_read(&img, io) == -1) {
  15.194 +		img_destroy(&img);
  15.195 +		return 0;
  15.196 +	}
  15.197 +
  15.198 +	tex = img_gltexture(&img);
  15.199 +	img_destroy(&img);
  15.200 +	return tex;
  15.201 +}
  15.202 +
  15.203 +#if defined(__unix__) || defined(__APPLE__)
  15.204 +#ifndef __USE_GNU
  15.205 +#define __USE_GNU
  15.206 +#endif
  15.207 +
  15.208 +#include <dlfcn.h>
  15.209 +#endif
  15.210 +#ifdef WIN32
  15.211 +#include <windows.h>
  15.212 +#endif
  15.213 +
  15.214 +static int load_glfunc(void)
  15.215 +{
  15.216 +#if defined(__unix__) || defined(__APPLE__)
  15.217 +	gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
  15.218 +	gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
  15.219 +	gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
  15.220 +	gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
  15.221 +#endif
  15.222 +
  15.223 +#ifdef WIN32
  15.224 +	HANDLE dll = LoadLibrary("opengl32.dll");
  15.225 +	if(dll) {
  15.226 +		gl_gen_textures = (gl_gen_textures_func)GetProcAddress(dll, "glGenTextures");
  15.227 +		gl_bind_texture = (gl_bind_texture_func)GetProcAddress(dll, "glBindTexture");
  15.228 +		gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(dll, "glTexParameteri");
  15.229 +		gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(dll, "glTexImage2D");
  15.230 +	}
  15.231 +#endif
  15.232 +
  15.233 +	return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;
  15.234 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/libs/imago/modules.c	Sun Jun 07 17:25:49 2015 +0300
    16.3 @@ -0,0 +1,15 @@
    16.4 +/* this file is generated by ./configure, do not edit */
    16.5 +int img_register_jpeg();
    16.6 +int img_register_png();
    16.7 +int img_register_ppm();
    16.8 +int img_register_rgbe();
    16.9 +int img_register_tga();
   16.10 +
   16.11 +void img_modules_init(void)
   16.12 +{
   16.13 +	img_register_jpeg();
   16.14 +	img_register_png();
   16.15 +	img_register_ppm();
   16.16 +	img_register_rgbe();
   16.17 +	img_register_tga();
   16.18 +}
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/libs/libjpeg/README	Sun Jun 07 17:25:49 2015 +0300
    17.3 @@ -0,0 +1,326 @@
    17.4 +The Independent JPEG Group's JPEG software
    17.5 +==========================================
    17.6 +
    17.7 +README for release 8c of 16-Jan-2011
    17.8 +====================================
    17.9 +
   17.10 +This distribution contains the eighth public release of the Independent JPEG
   17.11 +Group's free JPEG software.  You are welcome to redistribute this software and
   17.12 +to use it for any purpose, subject to the conditions under LEGAL ISSUES, below.
   17.13 +
   17.14 +This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone,
   17.15 +Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson,
   17.16 +Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers,
   17.17 +and other members of the Independent JPEG Group.
   17.18 +
   17.19 +IJG is not affiliated with the official ISO JPEG standards committee.
   17.20 +
   17.21 +
   17.22 +DOCUMENTATION ROADMAP
   17.23 +=====================
   17.24 +
   17.25 +This file contains the following sections:
   17.26 +
   17.27 +OVERVIEW            General description of JPEG and the IJG software.
   17.28 +LEGAL ISSUES        Copyright, lack of warranty, terms of distribution.
   17.29 +REFERENCES          Where to learn more about JPEG.
   17.30 +ARCHIVE LOCATIONS   Where to find newer versions of this software.
   17.31 +ACKNOWLEDGMENTS     Special thanks.
   17.32 +FILE FORMAT WARS    Software *not* to get.
   17.33 +TO DO               Plans for future IJG releases.
   17.34 +
   17.35 +Other documentation files in the distribution are:
   17.36 +
   17.37 +User documentation:
   17.38 +  install.txt       How to configure and install the IJG software.
   17.39 +  usage.txt         Usage instructions for cjpeg, djpeg, jpegtran,
   17.40 +                    rdjpgcom, and wrjpgcom.
   17.41 +  *.1               Unix-style man pages for programs (same info as usage.txt).
   17.42 +  wizard.txt        Advanced usage instructions for JPEG wizards only.
   17.43 +  change.log        Version-to-version change highlights.
   17.44 +Programmer and internal documentation:
   17.45 +  libjpeg.txt       How to use the JPEG library in your own programs.
   17.46 +  example.c         Sample code for calling the JPEG library.
   17.47 +  structure.txt     Overview of the JPEG library's internal structure.
   17.48 +  filelist.txt      Road map of IJG files.
   17.49 +  coderules.txt     Coding style rules --- please read if you contribute code.
   17.50 +
   17.51 +Please read at least the files install.txt and usage.txt.  Some information
   17.52 +can also be found in the JPEG FAQ (Frequently Asked Questions) article.  See
   17.53 +ARCHIVE LOCATIONS below to find out where to obtain the FAQ article.
   17.54 +
   17.55 +If you want to understand how the JPEG code works, we suggest reading one or
   17.56 +more of the REFERENCES, then looking at the documentation files (in roughly
   17.57 +the order listed) before diving into the code.
   17.58 +
   17.59 +
   17.60 +OVERVIEW
   17.61 +========
   17.62 +
   17.63 +This package contains C software to implement JPEG image encoding, decoding,
   17.64 +and transcoding.  JPEG (pronounced "jay-peg") is a standardized compression
   17.65 +method for full-color and gray-scale images.
   17.66 +
   17.67 +This software implements JPEG baseline, extended-sequential, and progressive
   17.68 +compression processes.  Provision is made for supporting all variants of these
   17.69 +processes, although some uncommon parameter settings aren't implemented yet.
   17.70 +We have made no provision for supporting the hierarchical or lossless
   17.71 +processes defined in the standard.
   17.72 +
   17.73 +We provide a set of library routines for reading and writing JPEG image files,
   17.74 +plus two sample applications "cjpeg" and "djpeg", which use the library to
   17.75 +perform conversion between JPEG and some other popular image file formats.
   17.76 +The library is intended to be reused in other applications.
   17.77 +
   17.78 +In order to support file conversion and viewing software, we have included
   17.79 +considerable functionality beyond the bare JPEG coding/decoding capability;
   17.80 +for example, the color quantization modules are not strictly part of JPEG
   17.81 +decoding, but they are essential for output to colormapped file formats or
   17.82 +colormapped displays.  These extra functions can be compiled out of the
   17.83 +library if not required for a particular application.
   17.84 +
   17.85 +We have also included "jpegtran", a utility for lossless transcoding between
   17.86 +different JPEG processes, and "rdjpgcom" and "wrjpgcom", two simple
   17.87 +applications for inserting and extracting textual comments in JFIF files.
   17.88 +
   17.89 +The emphasis in designing this software has been on achieving portability and
   17.90 +flexibility, while also making it fast enough to be useful.  In particular,
   17.91 +the software is not intended to be read as a tutorial on JPEG.  (See the
   17.92 +REFERENCES section for introductory material.)  Rather, it is intended to
   17.93 +be reliable, portable, industrial-strength code.  We do not claim to have
   17.94 +achieved that goal in every aspect of the software, but we strive for it.
   17.95 +
   17.96 +We welcome the use of this software as a component of commercial products.
   17.97 +No royalty is required, but we do ask for an acknowledgement in product
   17.98 +documentation, as described under LEGAL ISSUES.
   17.99 +
  17.100 +
  17.101 +LEGAL ISSUES
  17.102 +============
  17.103 +
  17.104 +In plain English:
  17.105 +
  17.106 +1. We don't promise that this software works.  (But if you find any bugs,
  17.107 +   please let us know!)
  17.108 +2. You can use this software for whatever you want.  You don't have to pay us.
  17.109 +3. You may not pretend that you wrote this software.  If you use it in a
  17.110 +   program, you must acknowledge somewhere in your documentation that
  17.111 +   you've used the IJG code.
  17.112 +
  17.113 +In legalese:
  17.114 +
  17.115 +The authors make NO WARRANTY or representation, either express or implied,
  17.116 +with respect to this software, its quality, accuracy, merchantability, or
  17.117 +fitness for a particular purpose.  This software is provided "AS IS", and you,
  17.118 +its user, assume the entire risk as to its quality and accuracy.
  17.119 +
  17.120 +This software is copyright (C) 1991-2011, Thomas G. Lane, Guido Vollbeding.
  17.121 +All Rights Reserved except as specified below.
  17.122 +
  17.123 +Permission is hereby granted to use, copy, modify, and distribute this
  17.124 +software (or portions thereof) for any purpose, without fee, subject to these
  17.125 +conditions:
  17.126 +(1) If any part of the source code for this software is distributed, then this
  17.127 +README file must be included, with this copyright and no-warranty notice
  17.128 +unaltered; and any additions, deletions, or changes to the original files
  17.129 +must be clearly indicated in accompanying documentation.
  17.130 +(2) If only executable code is distributed, then the accompanying
  17.131 +documentation must state that "this software is based in part on the work of
  17.132 +the Independent JPEG Group".
  17.133 +(3) Permission for use of this software is granted only if the user accepts
  17.134 +full responsibility for any undesirable consequences; the authors accept
  17.135 +NO LIABILITY for damages of any kind.
  17.136 +
  17.137 +These conditions apply to any software derived from or based on the IJG code,
  17.138 +not just to the unmodified library.  If you use our work, you ought to
  17.139 +acknowledge us.
  17.140 +
  17.141 +Permission is NOT granted for the use of any IJG author's name or company name
  17.142 +in advertising or publicity relating to this software or products derived from
  17.143 +it.  This software may be referred to only as "the Independent JPEG Group's
  17.144 +software".
  17.145 +
  17.146 +We specifically permit and encourage the use of this software as the basis of
  17.147 +commercial products, provided that all warranty or liability claims are
  17.148 +assumed by the product vendor.
  17.149 +
  17.150 +
  17.151 +ansi2knr.c is included in this distribution by permission of L. Peter Deutsch,
  17.152 +sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA.
  17.153 +ansi2knr.c is NOT covered by the above copyright and conditions, but instead
  17.154 +by the usual distribution terms of the Free Software Foundation; principally,
  17.155 +that you must include source code if you redistribute it.  (See the file
  17.156 +ansi2knr.c for full details.)  However, since ansi2knr.c is not needed as part
  17.157 +of any program generated from the IJG code, this does not limit you more than
  17.158 +the foregoing paragraphs do.
  17.159 +
  17.160 +The Unix configuration script "configure" was produced with GNU Autoconf.
  17.161 +It is copyright by the Free Software Foundation but is freely distributable.
  17.162 +The same holds for its supporting scripts (config.guess, config.sub,
  17.163 +ltmain.sh).  Another support script, install-sh, is copyright by X Consortium
  17.164 +but is also freely distributable.
  17.165 +
  17.166 +The IJG distribution formerly included code to read and write GIF files.
  17.167 +To avoid entanglement with the Unisys LZW patent, GIF reading support has
  17.168 +been removed altogether, and the GIF writer has been simplified to produce
  17.169 +"uncompressed GIFs".  This technique does not use the LZW algorithm; the
  17.170 +resulting GIF files are larger than usual, but are readable by all standard
  17.171 +GIF decoders.
  17.172 +
  17.173 +We are required to state that
  17.174 +    "The Graphics Interchange Format(c) is the Copyright property of
  17.175 +    CompuServe Incorporated.  GIF(sm) is a Service Mark property of
  17.176 +    CompuServe Incorporated."
  17.177 +
  17.178 +
  17.179 +REFERENCES
  17.180 +==========
  17.181 +
  17.182 +We recommend reading one or more of these references before trying to
  17.183 +understand the innards of the JPEG software.
  17.184 +
  17.185 +The best short technical introduction to the JPEG compression algorithm is
  17.186 +	Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
  17.187 +	Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
  17.188 +(Adjacent articles in that issue discuss MPEG motion picture compression,
  17.189 +applications of JPEG, and related topics.)  If you don't have the CACM issue
  17.190 +handy, a PostScript file containing a revised version of Wallace's article is
  17.191 +available at http://www.ijg.org/files/wallace.ps.gz.  The file (actually
  17.192 +a preprint for an article that appeared in IEEE Trans. Consumer Electronics)
  17.193 +omits the sample images that appeared in CACM, but it includes corrections
  17.194 +and some added material.  Note: the Wallace article is copyright ACM and IEEE,
  17.195 +and it may not be used for commercial purposes.
  17.196 +
  17.197 +A somewhat less technical, more leisurely introduction to JPEG can be found in
  17.198 +"The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by
  17.199 +M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1.  This book provides
  17.200 +good explanations and example C code for a multitude of compression methods
  17.201 +including JPEG.  It is an excellent source if you are comfortable reading C
  17.202 +code but don't know much about data compression in general.  The book's JPEG
  17.203 +sample code is far from industrial-strength, but when you are ready to look
  17.204 +at a full implementation, you've got one here...
  17.205 +
  17.206 +The best currently available description of JPEG is the textbook "JPEG Still
  17.207 +Image Data Compression Standard" by William B. Pennebaker and Joan L.
  17.208 +Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
  17.209 +Price US$59.95, 638 pp.  The book includes the complete text of the ISO JPEG
  17.210 +standards (DIS 10918-1 and draft DIS 10918-2).
  17.211 +Although this is by far the most detailed and comprehensive exposition of
  17.212 +JPEG publicly available, we point out that it is still missing an explanation
  17.213 +of the most essential properties and algorithms of the underlying DCT
  17.214 +technology.
  17.215 +If you think that you know about DCT-based JPEG after reading this book,
  17.216 +then you are in delusion.  The real fundamentals and corresponding potential
  17.217 +of DCT-based JPEG are not publicly known so far, and that is the reason for
  17.218 +all the mistaken developments taking place in the image coding domain.
  17.219 +
  17.220 +The original JPEG standard is divided into two parts, Part 1 being the actual
  17.221 +specification, while Part 2 covers compliance testing methods.  Part 1 is
  17.222 +titled "Digital Compression and Coding of Continuous-tone Still Images,
  17.223 +Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS
  17.224 +10918-1, ITU-T T.81.  Part 2 is titled "Digital Compression and Coding of
  17.225 +Continuous-tone Still Images, Part 2: Compliance testing" and has document
  17.226 +numbers ISO/IEC IS 10918-2, ITU-T T.83.
  17.227 +IJG JPEG 8 introduces an implementation of the JPEG SmartScale extension
  17.228 +which is specified in a contributed document at ITU and ISO with title "ITU-T
  17.229 +JPEG-Plus Proposal for Extending ITU-T T.81 for Advanced Image Coding", April
  17.230 +2006, Geneva, Switzerland.  The latest version of the document is Revision 3.
  17.231 +
  17.232 +The JPEG standard does not specify all details of an interchangeable file
  17.233 +format.  For the omitted details we follow the "JFIF" conventions, revision
  17.234 +1.02.  JFIF 1.02 has been adopted as an Ecma International Technical Report
  17.235 +and thus received a formal publication status.  It is available as a free
  17.236 +download in PDF format from
  17.237 +http://www.ecma-international.org/publications/techreports/E-TR-098.htm.
  17.238 +A PostScript version of the JFIF document is available at
  17.239 +http://www.ijg.org/files/jfif.ps.gz.  There is also a plain text version at
  17.240 +http://www.ijg.org/files/jfif.txt.gz, but it is missing the figures.
  17.241 +
  17.242 +The TIFF 6.0 file format specification can be obtained by FTP from
  17.243 +ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz.  The JPEG incorporation scheme
  17.244 +found in the TIFF 6.0 spec of 3-June-92 has a number of serious problems.
  17.245 +IJG does not recommend use of the TIFF 6.0 design (TIFF Compression tag 6).
  17.246 +Instead, we recommend the JPEG design proposed by TIFF Technical Note #2
  17.247 +(Compression tag 7).  Copies of this Note can be obtained from
  17.248 +http://www.ijg.org/files/.  It is expected that the next revision
  17.249 +of the TIFF spec will replace the 6.0 JPEG design with the Note's design.
  17.250 +Although IJG's own code does not support TIFF/JPEG, the free libtiff library
  17.251 +uses our library to implement TIFF/JPEG per the Note.
  17.252 +
  17.253 +
  17.254 +ARCHIVE LOCATIONS
  17.255 +=================
  17.256 +
  17.257 +The "official" archive site for this software is www.ijg.org.
  17.258 +The most recent released version can always be found there in
  17.259 +directory "files".  This particular version will be archived as
  17.260 +http://www.ijg.org/files/jpegsrc.v8c.tar.gz, and in Windows-compatible
  17.261 +"zip" archive format as http://www.ijg.org/files/jpegsr8c.zip.
  17.262 +
  17.263 +The JPEG FAQ (Frequently Asked Questions) article is a source of some
  17.264 +general information about JPEG.
  17.265 +It is available on the World Wide Web at http://www.faqs.org/faqs/jpeg-faq/
  17.266 +and other news.answers archive sites, including the official news.answers
  17.267 +archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/.
  17.268 +If you don't have Web or FTP access, send e-mail to mail-server@rtfm.mit.edu
  17.269 +with body
  17.270 +	send usenet/news.answers/jpeg-faq/part1
  17.271 +	send usenet/news.answers/jpeg-faq/part2
  17.272 +
  17.273 +
  17.274 +ACKNOWLEDGMENTS
  17.275 +===============
  17.276 +
  17.277 +Thank to Juergen Bruder for providing me with a copy of the common DCT
  17.278 +algorithm article, only to find out that I had come to the same result
  17.279 +in a more direct and comprehensible way with a more generative approach.
  17.280 +
  17.281 +Thank to Istvan Sebestyen and Joan L. Mitchell for inviting me to the
  17.282 +ITU JPEG (Study Group 16) meeting in Geneva, Switzerland.
  17.283 +
  17.284 +Thank to Thomas Wiegand and Gary Sullivan for inviting me to the
  17.285 +Joint Video Team (MPEG & ITU) meeting in Geneva, Switzerland.
  17.286 +
  17.287 +Thank to John Korejwa and Massimo Ballerini for inviting me to
  17.288 +fruitful consultations in Boston, MA and Milan, Italy.
  17.289 +
  17.290 +Thank to Hendrik Elstner, Roland Fassauer, Simone Zuck, Guenther
  17.291 +Maier-Gerber, Walter Stoeber, Fred Schmitz, and Norbert Braunagel
  17.292 +for corresponding business development.
  17.293 +
  17.294 +Thank to Nico Zschach and Dirk Stelling of the technical support team
  17.295 +at the Digital Images company in Halle for providing me with extra
  17.296 +equipment for configuration tests.
  17.297 +
  17.298 +Thank to Richard F. Lyon (then of Foveon Inc.) for fruitful
  17.299 +communication about JPEG configuration in Sigma Photo Pro software.
  17.300 +
  17.301 +Thank to Andrew Finkenstadt for hosting the ijg.org site.
  17.302 +
  17.303 +Last but not least special thank to Thomas G. Lane for the original
  17.304 +design and development of this singular software package.
  17.305 +
  17.306 +
  17.307 +FILE FORMAT WARS
  17.308 +================
  17.309 +
  17.310 +The ISO JPEG standards committee actually promotes different formats like
  17.311 +"JPEG 2000" or "JPEG XR" which are incompatible with original DCT-based
  17.312 +JPEG and which are based on faulty technologies.  IJG therefore does not
  17.313 +and will not support such momentary mistakes (see REFERENCES).
  17.314 +We have little or no sympathy for the promotion of these formats.  Indeed,
  17.315 +one of the original reasons for developing this free software was to help
  17.316 +force convergence on common, interoperable format standards for JPEG files.
  17.317 +Don't use an incompatible file format!
  17.318 +(In any case, our decoder will remain capable of reading existing JPEG
  17.319 +image files indefinitely.)
  17.320 +
  17.321 +
  17.322 +TO DO
  17.323 +=====
  17.324 +
  17.325 +Version 8 is the first release of a new generation JPEG standard
  17.326 +to overcome the limitations of the original JPEG specification.
  17.327 +More features are being prepared for coming releases...
  17.328 +
  17.329 +Please send bug reports, offers of help, etc. to jpeg-info@uc.ag.
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/libs/libjpeg/cderror.h	Sun Jun 07 17:25:49 2015 +0300
    18.3 @@ -0,0 +1,132 @@
    18.4 +/*
    18.5 + * cderror.h
    18.6 + *
    18.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    18.8 + * This file is part of the Independent JPEG Group's software.
    18.9 + * For conditions of distribution and use, see the accompanying README file.
   18.10 + *
   18.11 + * This file defines the error and message codes for the cjpeg/djpeg
   18.12 + * applications.  These strings are not needed as part of the JPEG library
   18.13 + * proper.
   18.14 + * Edit this file to add new codes, or to translate the message strings to
   18.15 + * some other language.
   18.16 + */
   18.17 +
   18.18 +/*
   18.19 + * To define the enum list of message codes, include this file without
   18.20 + * defining macro JMESSAGE.  To create a message string table, include it
   18.21 + * again with a suitable JMESSAGE definition (see jerror.c for an example).
   18.22 + */
   18.23 +#ifndef JMESSAGE
   18.24 +#ifndef CDERROR_H
   18.25 +#define CDERROR_H
   18.26 +/* First time through, define the enum list */
   18.27 +#define JMAKE_ENUM_LIST
   18.28 +#else
   18.29 +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
   18.30 +#define JMESSAGE(code,string)
   18.31 +#endif /* CDERROR_H */
   18.32 +#endif /* JMESSAGE */
   18.33 +
   18.34 +#ifdef JMAKE_ENUM_LIST
   18.35 +
   18.36 +typedef enum {
   18.37 +
   18.38 +#define JMESSAGE(code,string)	code ,
   18.39 +
   18.40 +#endif /* JMAKE_ENUM_LIST */
   18.41 +
   18.42 +JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
   18.43 +
   18.44 +#ifdef BMP_SUPPORTED
   18.45 +JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
   18.46 +JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
   18.47 +JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
   18.48 +JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
   18.49 +JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
   18.50 +JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
   18.51 +JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
   18.52 +JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
   18.53 +JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
   18.54 +JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
   18.55 +JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
   18.56 +#endif /* BMP_SUPPORTED */
   18.57 +
   18.58 +#ifdef GIF_SUPPORTED
   18.59 +JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
   18.60 +JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
   18.61 +JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
   18.62 +JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
   18.63 +JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
   18.64 +JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
   18.65 +JMESSAGE(JTRC_GIF_BADVERSION,
   18.66 +	 "Warning: unexpected GIF version number '%c%c%c'")
   18.67 +JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
   18.68 +JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
   18.69 +JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
   18.70 +JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
   18.71 +JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
   18.72 +JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
   18.73 +#endif /* GIF_SUPPORTED */
   18.74 +
   18.75 +#ifdef PPM_SUPPORTED
   18.76 +JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
   18.77 +JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
   18.78 +JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
   18.79 +JMESSAGE(JTRC_PGM, "%ux%u PGM image")
   18.80 +JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
   18.81 +JMESSAGE(JTRC_PPM, "%ux%u PPM image")
   18.82 +JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
   18.83 +#endif /* PPM_SUPPORTED */
   18.84 +
   18.85 +#ifdef RLE_SUPPORTED
   18.86 +JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
   18.87 +JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
   18.88 +JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
   18.89 +JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
   18.90 +JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
   18.91 +JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
   18.92 +JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
   18.93 +JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
   18.94 +JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
   18.95 +JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
   18.96 +JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
   18.97 +JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
   18.98 +JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
   18.99 +JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
  18.100 +#endif /* RLE_SUPPORTED */
  18.101 +
  18.102 +#ifdef TARGA_SUPPORTED
  18.103 +JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
  18.104 +JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
  18.105 +JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
  18.106 +JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
  18.107 +JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
  18.108 +JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
  18.109 +#else
  18.110 +JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
  18.111 +#endif /* TARGA_SUPPORTED */
  18.112 +
  18.113 +JMESSAGE(JERR_BAD_CMAP_FILE,
  18.114 +	 "Color map file is invalid or of unsupported format")
  18.115 +JMESSAGE(JERR_TOO_MANY_COLORS,
  18.116 +	 "Output file format cannot handle %d colormap entries")
  18.117 +JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
  18.118 +#ifdef TARGA_SUPPORTED
  18.119 +JMESSAGE(JERR_UNKNOWN_FORMAT,
  18.120 +	 "Unrecognized input file format --- perhaps you need -targa")
  18.121 +#else
  18.122 +JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
  18.123 +#endif
  18.124 +JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
  18.125 +
  18.126 +#ifdef JMAKE_ENUM_LIST
  18.127 +
  18.128 +  JMSG_LASTADDONCODE
  18.129 +} ADDON_MESSAGE_CODE;
  18.130 +
  18.131 +#undef JMAKE_ENUM_LIST
  18.132 +#endif /* JMAKE_ENUM_LIST */
  18.133 +
  18.134 +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
  18.135 +#undef JMESSAGE
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/libs/libjpeg/jcapimin.c	Sun Jun 07 17:25:49 2015 +0300
    19.3 @@ -0,0 +1,280 @@
    19.4 +/*
    19.5 + * jcapimin.c
    19.6 + *
    19.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    19.8 + * This file is part of the Independent JPEG Group's software.
    19.9 + * For conditions of distribution and use, see the accompanying README file.
   19.10 + *
   19.11 + * This file contains application interface code for the compression half
   19.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   19.13 + * needed in either the normal full-compression case or the transcoding-only
   19.14 + * case.
   19.15 + *
   19.16 + * Most of the routines intended to be called directly by an application
   19.17 + * are in this file or in jcapistd.c.  But also see jcparam.c for
   19.18 + * parameter-setup helper routines, jcomapi.c for routines shared by
   19.19 + * compression and decompression, and jctrans.c for the transcoding case.
   19.20 + */
   19.21 +
   19.22 +#define JPEG_INTERNALS
   19.23 +#include "jinclude.h"
   19.24 +#include "jpeglib.h"
   19.25 +
   19.26 +
   19.27 +/*
   19.28 + * Initialization of a JPEG compression object.
   19.29 + * The error manager must already be set up (in case memory manager fails).
   19.30 + */
   19.31 +
   19.32 +GLOBAL(void)
   19.33 +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
   19.34 +{
   19.35 +  int i;
   19.36 +
   19.37 +  /* Guard against version mismatches between library and caller. */
   19.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   19.39 +  if (version != JPEG_LIB_VERSION)
   19.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   19.41 +  if (structsize != SIZEOF(struct jpeg_compress_struct))
   19.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   19.43 +	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
   19.44 +
   19.45 +  /* For debugging purposes, we zero the whole master structure.
   19.46 +   * But the application has already set the err pointer, and may have set
   19.47 +   * client_data, so we have to save and restore those fields.
   19.48 +   * Note: if application hasn't set client_data, tools like Purify may
   19.49 +   * complain here.
   19.50 +   */
   19.51 +  {
   19.52 +    struct jpeg_error_mgr * err = cinfo->err;
   19.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   19.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
   19.55 +    cinfo->err = err;
   19.56 +    cinfo->client_data = client_data;
   19.57 +  }
   19.58 +  cinfo->is_decompressor = FALSE;
   19.59 +
   19.60 +  /* Initialize a memory manager instance for this object */
   19.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   19.62 +
   19.63 +  /* Zero out pointers to permanent structures. */
   19.64 +  cinfo->progress = NULL;
   19.65 +  cinfo->dest = NULL;
   19.66 +
   19.67 +  cinfo->comp_info = NULL;
   19.68 +
   19.69 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   19.70 +    cinfo->quant_tbl_ptrs[i] = NULL;
   19.71 +
   19.72 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   19.73 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   19.74 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   19.75 +  }
   19.76 +
   19.77 +  cinfo->script_space = NULL;
   19.78 +
   19.79 +  cinfo->input_gamma = 1.0;	/* in case application forgets */
   19.80 +
   19.81 +  /* OK, I'm ready */
   19.82 +  cinfo->global_state = CSTATE_START;
   19.83 +}
   19.84 +
   19.85 +
   19.86 +/*
   19.87 + * Destruction of a JPEG compression object
   19.88 + */
   19.89 +
   19.90 +GLOBAL(void)
   19.91 +jpeg_destroy_compress (j_compress_ptr cinfo)
   19.92 +{
   19.93 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   19.94 +}
   19.95 +
   19.96 +
   19.97 +/*
   19.98 + * Abort processing of a JPEG compression operation,
   19.99 + * but don't destroy the object itself.
  19.100 + */
  19.101 +
  19.102 +GLOBAL(void)
  19.103 +jpeg_abort_compress (j_compress_ptr cinfo)
  19.104 +{
  19.105 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  19.106 +}
  19.107 +
  19.108 +
  19.109 +/*
  19.110 + * Forcibly suppress or un-suppress all quantization and Huffman tables.
  19.111 + * Marks all currently defined tables as already written (if suppress)
  19.112 + * or not written (if !suppress).  This will control whether they get emitted
  19.113 + * by a subsequent jpeg_start_compress call.
  19.114 + *
  19.115 + * This routine is exported for use by applications that want to produce
  19.116 + * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  19.117 + * since it is called by jpeg_start_compress, we put it here --- otherwise
  19.118 + * jcparam.o would be linked whether the application used it or not.
  19.119 + */
  19.120 +
  19.121 +GLOBAL(void)
  19.122 +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  19.123 +{
  19.124 +  int i;
  19.125 +  JQUANT_TBL * qtbl;
  19.126 +  JHUFF_TBL * htbl;
  19.127 +
  19.128 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  19.129 +    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  19.130 +      qtbl->sent_table = suppress;
  19.131 +  }
  19.132 +
  19.133 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  19.134 +    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  19.135 +      htbl->sent_table = suppress;
  19.136 +    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  19.137 +      htbl->sent_table = suppress;
  19.138 +  }
  19.139 +}
  19.140 +
  19.141 +
  19.142 +/*
  19.143 + * Finish JPEG compression.
  19.144 + *
  19.145 + * If a multipass operating mode was selected, this may do a great deal of
  19.146 + * work including most of the actual output.
  19.147 + */
  19.148 +
  19.149 +GLOBAL(void)
  19.150 +jpeg_finish_compress (j_compress_ptr cinfo)
  19.151 +{
  19.152 +  JDIMENSION iMCU_row;
  19.153 +
  19.154 +  if (cinfo->global_state == CSTATE_SCANNING ||
  19.155 +      cinfo->global_state == CSTATE_RAW_OK) {
  19.156 +    /* Terminate first pass */
  19.157 +    if (cinfo->next_scanline < cinfo->image_height)
  19.158 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  19.159 +    (*cinfo->master->finish_pass) (cinfo);
  19.160 +  } else if (cinfo->global_state != CSTATE_WRCOEFS)
  19.161 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  19.162 +  /* Perform any remaining passes */
  19.163 +  while (! cinfo->master->is_last_pass) {
  19.164 +    (*cinfo->master->prepare_for_pass) (cinfo);
  19.165 +    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  19.166 +      if (cinfo->progress != NULL) {
  19.167 +	cinfo->progress->pass_counter = (long) iMCU_row;
  19.168 +	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  19.169 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  19.170 +      }
  19.171 +      /* We bypass the main controller and invoke coef controller directly;
  19.172 +       * all work is being done from the coefficient buffer.
  19.173 +       */
  19.174 +      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  19.175 +	ERREXIT(cinfo, JERR_CANT_SUSPEND);
  19.176 +    }
  19.177 +    (*cinfo->master->finish_pass) (cinfo);
  19.178 +  }
  19.179 +  /* Write EOI, do final cleanup */
  19.180 +  (*cinfo->marker->write_file_trailer) (cinfo);
  19.181 +  (*cinfo->dest->term_destination) (cinfo);
  19.182 +  /* We can use jpeg_abort to release memory and reset global_state */
  19.183 +  jpeg_abort((j_common_ptr) cinfo);
  19.184 +}
  19.185 +
  19.186 +
  19.187 +/*
  19.188 + * Write a special marker.
  19.189 + * This is only recommended for writing COM or APPn markers.
  19.190 + * Must be called after jpeg_start_compress() and before
  19.191 + * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  19.192 + */
  19.193 +
  19.194 +GLOBAL(void)
  19.195 +jpeg_write_marker (j_compress_ptr cinfo, int marker,
  19.196 +		   const JOCTET *dataptr, unsigned int datalen)
  19.197 +{
  19.198 +  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
  19.199 +
  19.200 +  if (cinfo->next_scanline != 0 ||
  19.201 +      (cinfo->global_state != CSTATE_SCANNING &&
  19.202 +       cinfo->global_state != CSTATE_RAW_OK &&
  19.203 +       cinfo->global_state != CSTATE_WRCOEFS))
  19.204 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  19.205 +
  19.206 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  19.207 +  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
  19.208 +  while (datalen--) {
  19.209 +    (*write_marker_byte) (cinfo, *dataptr);
  19.210 +    dataptr++;
  19.211 +  }
  19.212 +}
  19.213 +
  19.214 +/* Same, but piecemeal. */
  19.215 +
  19.216 +GLOBAL(void)
  19.217 +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  19.218 +{
  19.219 +  if (cinfo->next_scanline != 0 ||
  19.220 +      (cinfo->global_state != CSTATE_SCANNING &&
  19.221 +       cinfo->global_state != CSTATE_RAW_OK &&
  19.222 +       cinfo->global_state != CSTATE_WRCOEFS))
  19.223 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  19.224 +
  19.225 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  19.226 +}
  19.227 +
  19.228 +GLOBAL(void)
  19.229 +jpeg_write_m_byte (j_compress_ptr cinfo, int val)
  19.230 +{
  19.231 +  (*cinfo->marker->write_marker_byte) (cinfo, val);
  19.232 +}
  19.233 +
  19.234 +
  19.235 +/*
  19.236 + * Alternate compression function: just write an abbreviated table file.
  19.237 + * Before calling this, all parameters and a data destination must be set up.
  19.238 + *
  19.239 + * To produce a pair of files containing abbreviated tables and abbreviated
  19.240 + * image data, one would proceed as follows:
  19.241 + *
  19.242 + *		initialize JPEG object
  19.243 + *		set JPEG parameters
  19.244 + *		set destination to table file
  19.245 + *		jpeg_write_tables(cinfo);
  19.246 + *		set destination to image file
  19.247 + *		jpeg_start_compress(cinfo, FALSE);
  19.248 + *		write data...
  19.249 + *		jpeg_finish_compress(cinfo);
  19.250 + *
  19.251 + * jpeg_write_tables has the side effect of marking all tables written
  19.252 + * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  19.253 + * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  19.254 + */
  19.255 +
  19.256 +GLOBAL(void)
  19.257 +jpeg_write_tables (j_compress_ptr cinfo)
  19.258 +{
  19.259 +  if (cinfo->global_state != CSTATE_START)
  19.260 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  19.261 +
  19.262 +  /* (Re)initialize error mgr and destination modules */
  19.263 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  19.264 +  (*cinfo->dest->init_destination) (cinfo);
  19.265 +  /* Initialize the marker writer ... bit of a crock to do it here. */
  19.266 +  jinit_marker_writer(cinfo);
  19.267 +  /* Write them tables! */
  19.268 +  (*cinfo->marker->write_tables_only) (cinfo);
  19.269 +  /* And clean up. */
  19.270 +  (*cinfo->dest->term_destination) (cinfo);
  19.271 +  /*
  19.272 +   * In library releases up through v6a, we called jpeg_abort() here to free
  19.273 +   * any working memory allocated by the destination manager and marker
  19.274 +   * writer.  Some applications had a problem with that: they allocated space
  19.275 +   * of their own from the library memory manager, and didn't want it to go
  19.276 +   * away during write_tables.  So now we do nothing.  This will cause a
  19.277 +   * memory leak if an app calls write_tables repeatedly without doing a full
  19.278 +   * compression cycle or otherwise resetting the JPEG object.  However, that
  19.279 +   * seems less bad than unexpectedly freeing memory in the normal case.
  19.280 +   * An app that prefers the old behavior can call jpeg_abort for itself after
  19.281 +   * each call to jpeg_write_tables().
  19.282 +   */
  19.283 +}
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/libs/libjpeg/jcapistd.c	Sun Jun 07 17:25:49 2015 +0300
    20.3 @@ -0,0 +1,161 @@
    20.4 +/*
    20.5 + * jcapistd.c
    20.6 + *
    20.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    20.8 + * This file is part of the Independent JPEG Group's software.
    20.9 + * For conditions of distribution and use, see the accompanying README file.
   20.10 + *
   20.11 + * This file contains application interface code for the compression half
   20.12 + * of the JPEG library.  These are the "standard" API routines that are
   20.13 + * used in the normal full-compression case.  They are not used by a
   20.14 + * transcoding-only application.  Note that if an application links in
   20.15 + * jpeg_start_compress, it will end up linking in the entire compressor.
   20.16 + * We thus must separate this file from jcapimin.c to avoid linking the
   20.17 + * whole compression library into a transcoder.
   20.18 + */
   20.19 +
   20.20 +#define JPEG_INTERNALS
   20.21 +#include "jinclude.h"
   20.22 +#include "jpeglib.h"
   20.23 +
   20.24 +
   20.25 +/*
   20.26 + * Compression initialization.
   20.27 + * Before calling this, all parameters and a data destination must be set up.
   20.28 + *
   20.29 + * We require a write_all_tables parameter as a failsafe check when writing
   20.30 + * multiple datastreams from the same compression object.  Since prior runs
   20.31 + * will have left all the tables marked sent_table=TRUE, a subsequent run
   20.32 + * would emit an abbreviated stream (no tables) by default.  This may be what
   20.33 + * is wanted, but for safety's sake it should not be the default behavior:
   20.34 + * programmers should have to make a deliberate choice to emit abbreviated
   20.35 + * images.  Therefore the documentation and examples should encourage people
   20.36 + * to pass write_all_tables=TRUE; then it will take active thought to do the
   20.37 + * wrong thing.
   20.38 + */
   20.39 +
   20.40 +GLOBAL(void)
   20.41 +jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
   20.42 +{
   20.43 +  if (cinfo->global_state != CSTATE_START)
   20.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   20.45 +
   20.46 +  if (write_all_tables)
   20.47 +    jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
   20.48 +
   20.49 +  /* (Re)initialize error mgr and destination modules */
   20.50 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   20.51 +  (*cinfo->dest->init_destination) (cinfo);
   20.52 +  /* Perform master selection of active modules */
   20.53 +  jinit_compress_master(cinfo);
   20.54 +  /* Set up for the first pass */
   20.55 +  (*cinfo->master->prepare_for_pass) (cinfo);
   20.56 +  /* Ready for application to drive first pass through jpeg_write_scanlines
   20.57 +   * or jpeg_write_raw_data.
   20.58 +   */
   20.59 +  cinfo->next_scanline = 0;
   20.60 +  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
   20.61 +}
   20.62 +
   20.63 +
   20.64 +/*
   20.65 + * Write some scanlines of data to the JPEG compressor.
   20.66 + *
   20.67 + * The return value will be the number of lines actually written.
   20.68 + * This should be less than the supplied num_lines only in case that
   20.69 + * the data destination module has requested suspension of the compressor,
   20.70 + * or if more than image_height scanlines are passed in.
   20.71 + *
   20.72 + * Note: we warn about excess calls to jpeg_write_scanlines() since
   20.73 + * this likely signals an application programmer error.  However,
   20.74 + * excess scanlines passed in the last valid call are *silently* ignored,
   20.75 + * so that the application need not adjust num_lines for end-of-image
   20.76 + * when using a multiple-scanline buffer.
   20.77 + */
   20.78 +
   20.79 +GLOBAL(JDIMENSION)
   20.80 +jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
   20.81 +		      JDIMENSION num_lines)
   20.82 +{
   20.83 +  JDIMENSION row_ctr, rows_left;
   20.84 +
   20.85 +  if (cinfo->global_state != CSTATE_SCANNING)
   20.86 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   20.87 +  if (cinfo->next_scanline >= cinfo->image_height)
   20.88 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
   20.89 +
   20.90 +  /* Call progress monitor hook if present */
   20.91 +  if (cinfo->progress != NULL) {
   20.92 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
   20.93 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
   20.94 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   20.95 +  }
   20.96 +
   20.97 +  /* Give master control module another chance if this is first call to
   20.98 +   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
   20.99 +   * delayed so that application can write COM, etc, markers between
  20.100 +   * jpeg_start_compress and jpeg_write_scanlines.
  20.101 +   */
  20.102 +  if (cinfo->master->call_pass_startup)
  20.103 +    (*cinfo->master->pass_startup) (cinfo);
  20.104 +
  20.105 +  /* Ignore any extra scanlines at bottom of image. */
  20.106 +  rows_left = cinfo->image_height - cinfo->next_scanline;
  20.107 +  if (num_lines > rows_left)
  20.108 +    num_lines = rows_left;
  20.109 +
  20.110 +  row_ctr = 0;
  20.111 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  20.112 +  cinfo->next_scanline += row_ctr;
  20.113 +  return row_ctr;
  20.114 +}
  20.115 +
  20.116 +
  20.117 +/*
  20.118 + * Alternate entry point to write raw data.
  20.119 + * Processes exactly one iMCU row per call, unless suspended.
  20.120 + */
  20.121 +
  20.122 +GLOBAL(JDIMENSION)
  20.123 +jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  20.124 +		     JDIMENSION num_lines)
  20.125 +{
  20.126 +  JDIMENSION lines_per_iMCU_row;
  20.127 +
  20.128 +  if (cinfo->global_state != CSTATE_RAW_OK)
  20.129 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  20.130 +  if (cinfo->next_scanline >= cinfo->image_height) {
  20.131 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  20.132 +    return 0;
  20.133 +  }
  20.134 +
  20.135 +  /* Call progress monitor hook if present */
  20.136 +  if (cinfo->progress != NULL) {
  20.137 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  20.138 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
  20.139 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  20.140 +  }
  20.141 +
  20.142 +  /* Give master control module another chance if this is first call to
  20.143 +   * jpeg_write_raw_data.  This lets output of the frame/scan headers be
  20.144 +   * delayed so that application can write COM, etc, markers between
  20.145 +   * jpeg_start_compress and jpeg_write_raw_data.
  20.146 +   */
  20.147 +  if (cinfo->master->call_pass_startup)
  20.148 +    (*cinfo->master->pass_startup) (cinfo);
  20.149 +
  20.150 +  /* Verify that at least one iMCU row has been passed. */
  20.151 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  20.152 +  if (num_lines < lines_per_iMCU_row)
  20.153 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
  20.154 +
  20.155 +  /* Directly compress the row. */
  20.156 +  if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  20.157 +    /* If compressor did not consume the whole row, suspend processing. */
  20.158 +    return 0;
  20.159 +  }
  20.160 +
  20.161 +  /* OK, we processed one iMCU row. */
  20.162 +  cinfo->next_scanline += lines_per_iMCU_row;
  20.163 +  return lines_per_iMCU_row;
  20.164 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/libs/libjpeg/jccoefct.c	Sun Jun 07 17:25:49 2015 +0300
    21.3 @@ -0,0 +1,449 @@
    21.4 +/*
    21.5 + * jccoefct.c
    21.6 + *
    21.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    21.8 + * This file is part of the Independent JPEG Group's software.
    21.9 + * For conditions of distribution and use, see the accompanying README file.
   21.10 + *
   21.11 + * This file contains the coefficient buffer controller for compression.
   21.12 + * This controller is the top level of the JPEG compressor proper.
   21.13 + * The coefficient buffer lies between forward-DCT and entropy encoding steps.
   21.14 + */
   21.15 +
   21.16 +#define JPEG_INTERNALS
   21.17 +#include "jinclude.h"
   21.18 +#include "jpeglib.h"
   21.19 +
   21.20 +
   21.21 +/* We use a full-image coefficient buffer when doing Huffman optimization,
   21.22 + * and also for writing multiple-scan JPEG files.  In all cases, the DCT
   21.23 + * step is run during the first pass, and subsequent passes need only read
   21.24 + * the buffered coefficients.
   21.25 + */
   21.26 +#ifdef ENTROPY_OPT_SUPPORTED
   21.27 +#define FULL_COEF_BUFFER_SUPPORTED
   21.28 +#else
   21.29 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   21.30 +#define FULL_COEF_BUFFER_SUPPORTED
   21.31 +#endif
   21.32 +#endif
   21.33 +
   21.34 +
   21.35 +/* Private buffer controller object */
   21.36 +
   21.37 +typedef struct {
   21.38 +  struct jpeg_c_coef_controller pub; /* public fields */
   21.39 +
   21.40 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
   21.41 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
   21.42 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
   21.43 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
   21.44 +
   21.45 +  /* For single-pass compression, it's sufficient to buffer just one MCU
   21.46 +   * (although this may prove a bit slow in practice).  We allocate a
   21.47 +   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
   21.48 +   * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
   21.49 +   * it's not really very big; this is to keep the module interfaces unchanged
   21.50 +   * when a large coefficient buffer is necessary.)
   21.51 +   * In multi-pass modes, this array points to the current MCU's blocks
   21.52 +   * within the virtual arrays.
   21.53 +   */
   21.54 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
   21.55 +
   21.56 +  /* In multi-pass modes, we need a virtual block array for each component. */
   21.57 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
   21.58 +} my_coef_controller;
   21.59 +
   21.60 +typedef my_coef_controller * my_coef_ptr;
   21.61 +
   21.62 +
   21.63 +/* Forward declarations */
   21.64 +METHODDEF(boolean) compress_data
   21.65 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   21.66 +#ifdef FULL_COEF_BUFFER_SUPPORTED
   21.67 +METHODDEF(boolean) compress_first_pass
   21.68 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   21.69 +METHODDEF(boolean) compress_output
   21.70 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   21.71 +#endif
   21.72 +
   21.73 +
   21.74 +LOCAL(void)
   21.75 +start_iMCU_row (j_compress_ptr cinfo)
   21.76 +/* Reset within-iMCU-row counters for a new row */
   21.77 +{
   21.78 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   21.79 +
   21.80 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
   21.81 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
   21.82 +   * But at the bottom of the image, process only what's left.
   21.83 +   */
   21.84 +  if (cinfo->comps_in_scan > 1) {
   21.85 +    coef->MCU_rows_per_iMCU_row = 1;
   21.86 +  } else {
   21.87 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
   21.88 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
   21.89 +    else
   21.90 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
   21.91 +  }
   21.92 +
   21.93 +  coef->mcu_ctr = 0;
   21.94 +  coef->MCU_vert_offset = 0;
   21.95 +}
   21.96 +
   21.97 +
   21.98 +/*
   21.99 + * Initialize for a processing pass.
  21.100 + */
  21.101 +
  21.102 +METHODDEF(void)
  21.103 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  21.104 +{
  21.105 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  21.106 +
  21.107 +  coef->iMCU_row_num = 0;
  21.108 +  start_iMCU_row(cinfo);
  21.109 +
  21.110 +  switch (pass_mode) {
  21.111 +  case JBUF_PASS_THRU:
  21.112 +    if (coef->whole_image[0] != NULL)
  21.113 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.114 +    coef->pub.compress_data = compress_data;
  21.115 +    break;
  21.116 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  21.117 +  case JBUF_SAVE_AND_PASS:
  21.118 +    if (coef->whole_image[0] == NULL)
  21.119 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.120 +    coef->pub.compress_data = compress_first_pass;
  21.121 +    break;
  21.122 +  case JBUF_CRANK_DEST:
  21.123 +    if (coef->whole_image[0] == NULL)
  21.124 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.125 +    coef->pub.compress_data = compress_output;
  21.126 +    break;
  21.127 +#endif
  21.128 +  default:
  21.129 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.130 +    break;
  21.131 +  }
  21.132 +}
  21.133 +
  21.134 +
  21.135 +/*
  21.136 + * Process some data in the single-pass case.
  21.137 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  21.138 + * per call, ie, v_samp_factor block rows for each component in the image.
  21.139 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  21.140 + *
  21.141 + * NB: input_buf contains a plane for each component in image,
  21.142 + * which we index according to the component's SOF position.
  21.143 + */
  21.144 +
  21.145 +METHODDEF(boolean)
  21.146 +compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  21.147 +{
  21.148 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  21.149 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  21.150 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  21.151 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  21.152 +  int blkn, bi, ci, yindex, yoffset, blockcnt;
  21.153 +  JDIMENSION ypos, xpos;
  21.154 +  jpeg_component_info *compptr;
  21.155 +
  21.156 +  /* Loop to write as much as one whole iMCU row */
  21.157 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  21.158 +       yoffset++) {
  21.159 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  21.160 +	 MCU_col_num++) {
  21.161 +      /* Determine where data comes from in input_buf and do the DCT thing.
  21.162 +       * Each call on forward_DCT processes a horizontal row of DCT blocks
  21.163 +       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  21.164 +       * sequentially.  Dummy blocks at the right or bottom edge are filled in
  21.165 +       * specially.  The data in them does not matter for image reconstruction,
  21.166 +       * so we fill them with values that will encode to the smallest amount of
  21.167 +       * data, viz: all zeroes in the AC entries, DC entries equal to previous
  21.168 +       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
  21.169 +       */
  21.170 +      blkn = 0;
  21.171 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  21.172 +	compptr = cinfo->cur_comp_info[ci];
  21.173 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  21.174 +						: compptr->last_col_width;
  21.175 +	xpos = MCU_col_num * compptr->MCU_sample_width;
  21.176 +	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  21.177 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  21.178 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  21.179 +	      yoffset+yindex < compptr->last_row_height) {
  21.180 +	    (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  21.181 +					 input_buf[compptr->component_index],
  21.182 +					 coef->MCU_buffer[blkn],
  21.183 +					 ypos, xpos, (JDIMENSION) blockcnt);
  21.184 +	    if (blockcnt < compptr->MCU_width) {
  21.185 +	      /* Create some dummy blocks at the right edge of the image. */
  21.186 +	      jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  21.187 +			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  21.188 +	      for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  21.189 +		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  21.190 +	      }
  21.191 +	    }
  21.192 +	  } else {
  21.193 +	    /* Create a row of dummy blocks at the bottom of the image. */
  21.194 +	    jzero_far((void FAR *) coef->MCU_buffer[blkn],
  21.195 +		      compptr->MCU_width * SIZEOF(JBLOCK));
  21.196 +	    for (bi = 0; bi < compptr->MCU_width; bi++) {
  21.197 +	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  21.198 +	    }
  21.199 +	  }
  21.200 +	  blkn += compptr->MCU_width;
  21.201 +	  ypos += DCTSIZE;
  21.202 +	}
  21.203 +      }
  21.204 +      /* Try to write the MCU.  In event of a suspension failure, we will
  21.205 +       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  21.206 +       */
  21.207 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  21.208 +	/* Suspension forced; update state counters and exit */
  21.209 +	coef->MCU_vert_offset = yoffset;
  21.210 +	coef->mcu_ctr = MCU_col_num;
  21.211 +	return FALSE;
  21.212 +      }
  21.213 +    }
  21.214 +    /* Completed an MCU row, but perhaps not an iMCU row */
  21.215 +    coef->mcu_ctr = 0;
  21.216 +  }
  21.217 +  /* Completed the iMCU row, advance counters for next one */
  21.218 +  coef->iMCU_row_num++;
  21.219 +  start_iMCU_row(cinfo);
  21.220 +  return TRUE;
  21.221 +}
  21.222 +
  21.223 +
  21.224 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  21.225 +
  21.226 +/*
  21.227 + * Process some data in the first pass of a multi-pass case.
  21.228 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  21.229 + * per call, ie, v_samp_factor block rows for each component in the image.
  21.230 + * This amount of data is read from the source buffer, DCT'd and quantized,
  21.231 + * and saved into the virtual arrays.  We also generate suitable dummy blocks
  21.232 + * as needed at the right and lower edges.  (The dummy blocks are constructed
  21.233 + * in the virtual arrays, which have been padded appropriately.)  This makes
  21.234 + * it possible for subsequent passes not to worry about real vs. dummy blocks.
  21.235 + *
  21.236 + * We must also emit the data to the entropy encoder.  This is conveniently
  21.237 + * done by calling compress_output() after we've loaded the current strip
  21.238 + * of the virtual arrays.
  21.239 + *
  21.240 + * NB: input_buf contains a plane for each component in image.  All
  21.241 + * components are DCT'd and loaded into the virtual arrays in this pass.
  21.242 + * However, it may be that only a subset of the components are emitted to
  21.243 + * the entropy encoder during this first pass; be careful about looking
  21.244 + * at the scan-dependent variables (MCU dimensions, etc).
  21.245 + */
  21.246 +
  21.247 +METHODDEF(boolean)
  21.248 +compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  21.249 +{
  21.250 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  21.251 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  21.252 +  JDIMENSION blocks_across, MCUs_across, MCUindex;
  21.253 +  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  21.254 +  JCOEF lastDC;
  21.255 +  jpeg_component_info *compptr;
  21.256 +  JBLOCKARRAY buffer;
  21.257 +  JBLOCKROW thisblockrow, lastblockrow;
  21.258 +
  21.259 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  21.260 +       ci++, compptr++) {
  21.261 +    /* Align the virtual buffer for this component. */
  21.262 +    buffer = (*cinfo->mem->access_virt_barray)
  21.263 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
  21.264 +       coef->iMCU_row_num * compptr->v_samp_factor,
  21.265 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
  21.266 +    /* Count non-dummy DCT block rows in this iMCU row. */
  21.267 +    if (coef->iMCU_row_num < last_iMCU_row)
  21.268 +      block_rows = compptr->v_samp_factor;
  21.269 +    else {
  21.270 +      /* NB: can't use last_row_height here, since may not be set! */
  21.271 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  21.272 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  21.273 +    }
  21.274 +    blocks_across = compptr->width_in_blocks;
  21.275 +    h_samp_factor = compptr->h_samp_factor;
  21.276 +    /* Count number of dummy blocks to be added at the right margin. */
  21.277 +    ndummy = (int) (blocks_across % h_samp_factor);
  21.278 +    if (ndummy > 0)
  21.279 +      ndummy = h_samp_factor - ndummy;
  21.280 +    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
  21.281 +     * on forward_DCT processes a complete horizontal row of DCT blocks.
  21.282 +     */
  21.283 +    for (block_row = 0; block_row < block_rows; block_row++) {
  21.284 +      thisblockrow = buffer[block_row];
  21.285 +      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  21.286 +				   input_buf[ci], thisblockrow,
  21.287 +				   (JDIMENSION) (block_row * DCTSIZE),
  21.288 +				   (JDIMENSION) 0, blocks_across);
  21.289 +      if (ndummy > 0) {
  21.290 +	/* Create dummy blocks at the right edge of the image. */
  21.291 +	thisblockrow += blocks_across; /* => first dummy block */
  21.292 +	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  21.293 +	lastDC = thisblockrow[-1][0];
  21.294 +	for (bi = 0; bi < ndummy; bi++) {
  21.295 +	  thisblockrow[bi][0] = lastDC;
  21.296 +	}
  21.297 +      }
  21.298 +    }
  21.299 +    /* If at end of image, create dummy block rows as needed.
  21.300 +     * The tricky part here is that within each MCU, we want the DC values
  21.301 +     * of the dummy blocks to match the last real block's DC value.
  21.302 +     * This squeezes a few more bytes out of the resulting file...
  21.303 +     */
  21.304 +    if (coef->iMCU_row_num == last_iMCU_row) {
  21.305 +      blocks_across += ndummy;	/* include lower right corner */
  21.306 +      MCUs_across = blocks_across / h_samp_factor;
  21.307 +      for (block_row = block_rows; block_row < compptr->v_samp_factor;
  21.308 +	   block_row++) {
  21.309 +	thisblockrow = buffer[block_row];
  21.310 +	lastblockrow = buffer[block_row-1];
  21.311 +	jzero_far((void FAR *) thisblockrow,
  21.312 +		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
  21.313 +	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  21.314 +	  lastDC = lastblockrow[h_samp_factor-1][0];
  21.315 +	  for (bi = 0; bi < h_samp_factor; bi++) {
  21.316 +	    thisblockrow[bi][0] = lastDC;
  21.317 +	  }
  21.318 +	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
  21.319 +	  lastblockrow += h_samp_factor;
  21.320 +	}
  21.321 +      }
  21.322 +    }
  21.323 +  }
  21.324 +  /* NB: compress_output will increment iMCU_row_num if successful.
  21.325 +   * A suspension return will result in redoing all the work above next time.
  21.326 +   */
  21.327 +
  21.328 +  /* Emit data to the entropy encoder, sharing code with subsequent passes */
  21.329 +  return compress_output(cinfo, input_buf);
  21.330 +}
  21.331 +
  21.332 +
  21.333 +/*
  21.334 + * Process some data in subsequent passes of a multi-pass case.
  21.335 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  21.336 + * per call, ie, v_samp_factor block rows for each component in the scan.
  21.337 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  21.338 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  21.339 + *
  21.340 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  21.341 + */
  21.342 +
  21.343 +METHODDEF(boolean)
  21.344 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  21.345 +{
  21.346 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  21.347 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  21.348 +  int blkn, ci, xindex, yindex, yoffset;
  21.349 +  JDIMENSION start_col;
  21.350 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  21.351 +  JBLOCKROW buffer_ptr;
  21.352 +  jpeg_component_info *compptr;
  21.353 +
  21.354 +  /* Align the virtual buffers for the components used in this scan.
  21.355 +   * NB: during first pass, this is safe only because the buffers will
  21.356 +   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  21.357 +   */
  21.358 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  21.359 +    compptr = cinfo->cur_comp_info[ci];
  21.360 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  21.361 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  21.362 +       coef->iMCU_row_num * compptr->v_samp_factor,
  21.363 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  21.364 +  }
  21.365 +
  21.366 +  /* Loop to process one whole iMCU row */
  21.367 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  21.368 +       yoffset++) {
  21.369 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  21.370 +	 MCU_col_num++) {
  21.371 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  21.372 +      blkn = 0;			/* index of current DCT block within MCU */
  21.373 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  21.374 +	compptr = cinfo->cur_comp_info[ci];
  21.375 +	start_col = MCU_col_num * compptr->MCU_width;
  21.376 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  21.377 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  21.378 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  21.379 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
  21.380 +	  }
  21.381 +	}
  21.382 +      }
  21.383 +      /* Try to write the MCU. */
  21.384 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  21.385 +	/* Suspension forced; update state counters and exit */
  21.386 +	coef->MCU_vert_offset = yoffset;
  21.387 +	coef->mcu_ctr = MCU_col_num;
  21.388 +	return FALSE;
  21.389 +      }
  21.390 +    }
  21.391 +    /* Completed an MCU row, but perhaps not an iMCU row */
  21.392 +    coef->mcu_ctr = 0;
  21.393 +  }
  21.394 +  /* Completed the iMCU row, advance counters for next one */
  21.395 +  coef->iMCU_row_num++;
  21.396 +  start_iMCU_row(cinfo);
  21.397 +  return TRUE;
  21.398 +}
  21.399 +
  21.400 +#endif /* FULL_COEF_BUFFER_SUPPORTED */
  21.401 +
  21.402 +
  21.403 +/*
  21.404 + * Initialize coefficient buffer controller.
  21.405 + */
  21.406 +
  21.407 +GLOBAL(void)
  21.408 +jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  21.409 +{
  21.410 +  my_coef_ptr coef;
  21.411 +
  21.412 +  coef = (my_coef_ptr)
  21.413 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.414 +				SIZEOF(my_coef_controller));
  21.415 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  21.416 +  coef->pub.start_pass = start_pass_coef;
  21.417 +
  21.418 +  /* Create the coefficient buffer. */
  21.419 +  if (need_full_buffer) {
  21.420 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  21.421 +    /* Allocate a full-image virtual array for each component, */
  21.422 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
  21.423 +    int ci;
  21.424 +    jpeg_component_info *compptr;
  21.425 +
  21.426 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  21.427 +	 ci++, compptr++) {
  21.428 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  21.429 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  21.430 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  21.431 +				(long) compptr->h_samp_factor),
  21.432 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  21.433 +				(long) compptr->v_samp_factor),
  21.434 +	 (JDIMENSION) compptr->v_samp_factor);
  21.435 +    }
  21.436 +#else
  21.437 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  21.438 +#endif
  21.439 +  } else {
  21.440 +    /* We only need a single-MCU buffer. */
  21.441 +    JBLOCKROW buffer;
  21.442 +    int i;
  21.443 +
  21.444 +    buffer = (JBLOCKROW)
  21.445 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.446 +				  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  21.447 +    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  21.448 +      coef->MCU_buffer[i] = buffer + i;
  21.449 +    }
  21.450 +    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  21.451 +  }
  21.452 +}
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/libs/libjpeg/jccolor.c	Sun Jun 07 17:25:49 2015 +0300
    22.3 @@ -0,0 +1,459 @@
    22.4 +/*
    22.5 + * jccolor.c
    22.6 + *
    22.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    22.8 + * This file is part of the Independent JPEG Group's software.
    22.9 + * For conditions of distribution and use, see the accompanying README file.
   22.10 + *
   22.11 + * This file contains input colorspace conversion routines.
   22.12 + */
   22.13 +
   22.14 +#define JPEG_INTERNALS
   22.15 +#include "jinclude.h"
   22.16 +#include "jpeglib.h"
   22.17 +
   22.18 +
   22.19 +/* Private subobject */
   22.20 +
   22.21 +typedef struct {
   22.22 +  struct jpeg_color_converter pub; /* public fields */
   22.23 +
   22.24 +  /* Private state for RGB->YCC conversion */
   22.25 +  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
   22.26 +} my_color_converter;
   22.27 +
   22.28 +typedef my_color_converter * my_cconvert_ptr;
   22.29 +
   22.30 +
   22.31 +/**************** RGB -> YCbCr conversion: most common case **************/
   22.32 +
   22.33 +/*
   22.34 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
   22.35 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
   22.36 + * The conversion equations to be implemented are therefore
   22.37 + *	Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
   22.38 + *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
   22.39 + *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
   22.40 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
   22.41 + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
   22.42 + * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
   22.43 + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
   22.44 + * were not represented exactly.  Now we sacrifice exact representation of
   22.45 + * maximum red and maximum blue in order to get exact grayscales.
   22.46 + *
   22.47 + * To avoid floating-point arithmetic, we represent the fractional constants
   22.48 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
   22.49 + * the products by 2^16, with appropriate rounding, to get the correct answer.
   22.50 + *
   22.51 + * For even more speed, we avoid doing any multiplications in the inner loop
   22.52 + * by precalculating the constants times R,G,B for all possible values.
   22.53 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
   22.54 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
   22.55 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
   22.56 + * colorspace anyway.
   22.57 + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
   22.58 + * in the tables to save adding them separately in the inner loop.
   22.59 + */
   22.60 +
   22.61 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   22.62 +#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
   22.63 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   22.64 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   22.65 +
   22.66 +/* We allocate one big table and divide it up into eight parts, instead of
   22.67 + * doing eight alloc_small requests.  This lets us use a single table base
   22.68 + * address, which can be held in a register in the inner loops on many
   22.69 + * machines (more than can hold all eight addresses, anyway).
   22.70 + */
   22.71 +
   22.72 +#define R_Y_OFF		0			/* offset to R => Y section */
   22.73 +#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
   22.74 +#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
   22.75 +#define R_CB_OFF	(3*(MAXJSAMPLE+1))
   22.76 +#define G_CB_OFF	(4*(MAXJSAMPLE+1))
   22.77 +#define B_CB_OFF	(5*(MAXJSAMPLE+1))
   22.78 +#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */
   22.79 +#define G_CR_OFF	(6*(MAXJSAMPLE+1))
   22.80 +#define B_CR_OFF	(7*(MAXJSAMPLE+1))
   22.81 +#define TABLE_SIZE	(8*(MAXJSAMPLE+1))
   22.82 +
   22.83 +
   22.84 +/*
   22.85 + * Initialize for RGB->YCC colorspace conversion.
   22.86 + */
   22.87 +
   22.88 +METHODDEF(void)
   22.89 +rgb_ycc_start (j_compress_ptr cinfo)
   22.90 +{
   22.91 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   22.92 +  INT32 * rgb_ycc_tab;
   22.93 +  INT32 i;
   22.94 +
   22.95 +  /* Allocate and fill in the conversion tables. */
   22.96 +  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
   22.97 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   22.98 +				(TABLE_SIZE * SIZEOF(INT32)));
   22.99 +
  22.100 +  for (i = 0; i <= MAXJSAMPLE; i++) {
  22.101 +    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  22.102 +    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  22.103 +    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
  22.104 +    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  22.105 +    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  22.106 +    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  22.107 +     * This ensures that the maximum output will round to MAXJSAMPLE
  22.108 +     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  22.109 +     */
  22.110 +    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  22.111 +/*  B=>Cb and R=>Cr tables are the same
  22.112 +    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  22.113 +*/
  22.114 +    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  22.115 +    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  22.116 +  }
  22.117 +}
  22.118 +
  22.119 +
  22.120 +/*
  22.121 + * Convert some rows of samples to the JPEG colorspace.
  22.122 + *
  22.123 + * Note that we change from the application's interleaved-pixel format
  22.124 + * to our internal noninterleaved, one-plane-per-component format.
  22.125 + * The input buffer is therefore three times as wide as the output buffer.
  22.126 + *
  22.127 + * A starting row offset is provided only for the output buffer.  The caller
  22.128 + * can easily adjust the passed input_buf value to accommodate any row
  22.129 + * offset required on that side.
  22.130 + */
  22.131 +
  22.132 +METHODDEF(void)
  22.133 +rgb_ycc_convert (j_compress_ptr cinfo,
  22.134 +		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  22.135 +		 JDIMENSION output_row, int num_rows)
  22.136 +{
  22.137 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  22.138 +  register int r, g, b;
  22.139 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  22.140 +  register JSAMPROW inptr;
  22.141 +  register JSAMPROW outptr0, outptr1, outptr2;
  22.142 +  register JDIMENSION col;
  22.143 +  JDIMENSION num_cols = cinfo->image_width;
  22.144 +
  22.145 +  while (--num_rows >= 0) {
  22.146 +    inptr = *input_buf++;
  22.147 +    outptr0 = output_buf[0][output_row];
  22.148 +    outptr1 = output_buf[1][output_row];
  22.149 +    outptr2 = output_buf[2][output_row];
  22.150 +    output_row++;
  22.151 +    for (col = 0; col < num_cols; col++) {
  22.152 +      r = GETJSAMPLE(inptr[RGB_RED]);
  22.153 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  22.154 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  22.155 +      inptr += RGB_PIXELSIZE;
  22.156 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  22.157 +       * must be too; we do not need an explicit range-limiting operation.
  22.158 +       * Hence the value being shifted is never negative, and we don't
  22.159 +       * need the general RIGHT_SHIFT macro.
  22.160 +       */
  22.161 +      /* Y */
  22.162 +      outptr0[col] = (JSAMPLE)
  22.163 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  22.164 +		 >> SCALEBITS);
  22.165 +      /* Cb */
  22.166 +      outptr1[col] = (JSAMPLE)
  22.167 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  22.168 +		 >> SCALEBITS);
  22.169 +      /* Cr */
  22.170 +      outptr2[col] = (JSAMPLE)
  22.171 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  22.172 +		 >> SCALEBITS);
  22.173 +    }
  22.174 +  }
  22.175 +}
  22.176 +
  22.177 +
  22.178 +/**************** Cases other than RGB -> YCbCr **************/
  22.179 +
  22.180 +
  22.181 +/*
  22.182 + * Convert some rows of samples to the JPEG colorspace.
  22.183 + * This version handles RGB->grayscale conversion, which is the same
  22.184 + * as the RGB->Y portion of RGB->YCbCr.
  22.185 + * We assume rgb_ycc_start has been called (we only use the Y tables).
  22.186 + */
  22.187 +
  22.188 +METHODDEF(void)
  22.189 +rgb_gray_convert (j_compress_ptr cinfo,
  22.190 +		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  22.191 +		  JDIMENSION output_row, int num_rows)
  22.192 +{
  22.193 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  22.194 +  register int r, g, b;
  22.195 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  22.196 +  register JSAMPROW inptr;
  22.197 +  register JSAMPROW outptr;
  22.198 +  register JDIMENSION col;
  22.199 +  JDIMENSION num_cols = cinfo->image_width;
  22.200 +
  22.201 +  while (--num_rows >= 0) {
  22.202 +    inptr = *input_buf++;
  22.203 +    outptr = output_buf[0][output_row];
  22.204 +    output_row++;
  22.205 +    for (col = 0; col < num_cols; col++) {
  22.206 +      r = GETJSAMPLE(inptr[RGB_RED]);
  22.207 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  22.208 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  22.209 +      inptr += RGB_PIXELSIZE;
  22.210 +      /* Y */
  22.211 +      outptr[col] = (JSAMPLE)
  22.212 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  22.213 +		 >> SCALEBITS);
  22.214 +    }
  22.215 +  }
  22.216 +}
  22.217 +
  22.218 +
  22.219 +/*
  22.220 + * Convert some rows of samples to the JPEG colorspace.
  22.221 + * This version handles Adobe-style CMYK->YCCK conversion,
  22.222 + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  22.223 + * conversion as above, while passing K (black) unchanged.
  22.224 + * We assume rgb_ycc_start has been called.
  22.225 + */
  22.226 +
  22.227 +METHODDEF(void)
  22.228 +cmyk_ycck_convert (j_compress_ptr cinfo,
  22.229 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  22.230 +		   JDIMENSION output_row, int num_rows)
  22.231 +{
  22.232 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  22.233 +  register int r, g, b;
  22.234 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  22.235 +  register JSAMPROW inptr;
  22.236 +  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  22.237 +  register JDIMENSION col;
  22.238 +  JDIMENSION num_cols = cinfo->image_width;
  22.239 +
  22.240 +  while (--num_rows >= 0) {
  22.241 +    inptr = *input_buf++;
  22.242 +    outptr0 = output_buf[0][output_row];
  22.243 +    outptr1 = output_buf[1][output_row];
  22.244 +    outptr2 = output_buf[2][output_row];
  22.245 +    outptr3 = output_buf[3][output_row];
  22.246 +    output_row++;
  22.247 +    for (col = 0; col < num_cols; col++) {
  22.248 +      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  22.249 +      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  22.250 +      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  22.251 +      /* K passes through as-is */
  22.252 +      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
  22.253 +      inptr += 4;
  22.254 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  22.255 +       * must be too; we do not need an explicit range-limiting operation.
  22.256 +       * Hence the value being shifted is never negative, and we don't
  22.257 +       * need the general RIGHT_SHIFT macro.
  22.258 +       */
  22.259 +      /* Y */
  22.260 +      outptr0[col] = (JSAMPLE)
  22.261 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  22.262 +		 >> SCALEBITS);
  22.263 +      /* Cb */
  22.264 +      outptr1[col] = (JSAMPLE)
  22.265 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  22.266 +		 >> SCALEBITS);
  22.267 +      /* Cr */
  22.268 +      outptr2[col] = (JSAMPLE)
  22.269 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  22.270 +		 >> SCALEBITS);
  22.271 +    }
  22.272 +  }
  22.273 +}
  22.274 +
  22.275 +
  22.276 +/*
  22.277 + * Convert some rows of samples to the JPEG colorspace.
  22.278 + * This version handles grayscale output with no conversion.
  22.279 + * The source can be either plain grayscale or YCbCr (since Y == gray).
  22.280 + */
  22.281 +
  22.282 +METHODDEF(void)
  22.283 +grayscale_convert (j_compress_ptr cinfo,
  22.284 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  22.285 +		   JDIMENSION output_row, int num_rows)
  22.286 +{
  22.287 +  register JSAMPROW inptr;
  22.288 +  register JSAMPROW outptr;
  22.289 +  register JDIMENSION col;
  22.290 +  JDIMENSION num_cols = cinfo->image_width;
  22.291 +  int instride = cinfo->input_components;
  22.292 +
  22.293 +  while (--num_rows >= 0) {
  22.294 +    inptr = *input_buf++;
  22.295 +    outptr = output_buf[0][output_row];
  22.296 +    output_row++;
  22.297 +    for (col = 0; col < num_cols; col++) {
  22.298 +      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
  22.299 +      inptr += instride;
  22.300 +    }
  22.301 +  }
  22.302 +}
  22.303 +
  22.304 +
  22.305 +/*
  22.306 + * Convert some rows of samples to the JPEG colorspace.
  22.307 + * This version handles multi-component colorspaces without conversion.
  22.308 + * We assume input_components == num_components.
  22.309 + */
  22.310 +
  22.311 +METHODDEF(void)
  22.312 +null_convert (j_compress_ptr cinfo,
  22.313 +	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  22.314 +	      JDIMENSION output_row, int num_rows)
  22.315 +{
  22.316 +  register JSAMPROW inptr;
  22.317 +  register JSAMPROW outptr;
  22.318 +  register JDIMENSION col;
  22.319 +  register int ci;
  22.320 +  int nc = cinfo->num_components;
  22.321 +  JDIMENSION num_cols = cinfo->image_width;
  22.322 +
  22.323 +  while (--num_rows >= 0) {
  22.324 +    /* It seems fastest to make a separate pass for each component. */
  22.325 +    for (ci = 0; ci < nc; ci++) {
  22.326 +      inptr = *input_buf;
  22.327 +      outptr = output_buf[ci][output_row];
  22.328 +      for (col = 0; col < num_cols; col++) {
  22.329 +	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  22.330 +	inptr += nc;
  22.331 +      }
  22.332 +    }
  22.333 +    input_buf++;
  22.334 +    output_row++;
  22.335 +  }
  22.336 +}
  22.337 +
  22.338 +
  22.339 +/*
  22.340 + * Empty method for start_pass.
  22.341 + */
  22.342 +
  22.343 +METHODDEF(void)
  22.344 +null_method (j_compress_ptr cinfo)
  22.345 +{
  22.346 +  /* no work needed */
  22.347 +}
  22.348 +
  22.349 +
  22.350 +/*
  22.351 + * Module initialization routine for input colorspace conversion.
  22.352 + */
  22.353 +
  22.354 +GLOBAL(void)
  22.355 +jinit_color_converter (j_compress_ptr cinfo)
  22.356 +{
  22.357 +  my_cconvert_ptr cconvert;
  22.358 +
  22.359 +  cconvert = (my_cconvert_ptr)
  22.360 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  22.361 +				SIZEOF(my_color_converter));
  22.362 +  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  22.363 +  /* set start_pass to null method until we find out differently */
  22.364 +  cconvert->pub.start_pass = null_method;
  22.365 +
  22.366 +  /* Make sure input_components agrees with in_color_space */
  22.367 +  switch (cinfo->in_color_space) {
  22.368 +  case JCS_GRAYSCALE:
  22.369 +    if (cinfo->input_components != 1)
  22.370 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  22.371 +    break;
  22.372 +
  22.373 +  case JCS_RGB:
  22.374 +#if RGB_PIXELSIZE != 3
  22.375 +    if (cinfo->input_components != RGB_PIXELSIZE)
  22.376 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  22.377 +    break;
  22.378 +#endif /* else share code with YCbCr */
  22.379 +
  22.380 +  case JCS_YCbCr:
  22.381 +    if (cinfo->input_components != 3)
  22.382 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  22.383 +    break;
  22.384 +
  22.385 +  case JCS_CMYK:
  22.386 +  case JCS_YCCK:
  22.387 +    if (cinfo->input_components != 4)
  22.388 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  22.389 +    break;
  22.390 +
  22.391 +  default:			/* JCS_UNKNOWN can be anything */
  22.392 +    if (cinfo->input_components < 1)
  22.393 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  22.394 +    break;
  22.395 +  }
  22.396 +
  22.397 +  /* Check num_components, set conversion method based on requested space */
  22.398 +  switch (cinfo->jpeg_color_space) {
  22.399 +  case JCS_GRAYSCALE:
  22.400 +    if (cinfo->num_components != 1)
  22.401 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  22.402 +    if (cinfo->in_color_space == JCS_GRAYSCALE)
  22.403 +      cconvert->pub.color_convert = grayscale_convert;
  22.404 +    else if (cinfo->in_color_space == JCS_RGB) {
  22.405 +      cconvert->pub.start_pass = rgb_ycc_start;
  22.406 +      cconvert->pub.color_convert = rgb_gray_convert;
  22.407 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  22.408 +      cconvert->pub.color_convert = grayscale_convert;
  22.409 +    else
  22.410 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.411 +    break;
  22.412 +
  22.413 +  case JCS_RGB:
  22.414 +    if (cinfo->num_components != 3)
  22.415 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  22.416 +    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  22.417 +      cconvert->pub.color_convert = null_convert;
  22.418 +    else
  22.419 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.420 +    break;
  22.421 +
  22.422 +  case JCS_YCbCr:
  22.423 +    if (cinfo->num_components != 3)
  22.424 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  22.425 +    if (cinfo->in_color_space == JCS_RGB) {
  22.426 +      cconvert->pub.start_pass = rgb_ycc_start;
  22.427 +      cconvert->pub.color_convert = rgb_ycc_convert;
  22.428 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  22.429 +      cconvert->pub.color_convert = null_convert;
  22.430 +    else
  22.431 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.432 +    break;
  22.433 +
  22.434 +  case JCS_CMYK:
  22.435 +    if (cinfo->num_components != 4)
  22.436 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  22.437 +    if (cinfo->in_color_space == JCS_CMYK)
  22.438 +      cconvert->pub.color_convert = null_convert;
  22.439 +    else
  22.440 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.441 +    break;
  22.442 +
  22.443 +  case JCS_YCCK:
  22.444 +    if (cinfo->num_components != 4)
  22.445 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  22.446 +    if (cinfo->in_color_space == JCS_CMYK) {
  22.447 +      cconvert->pub.start_pass = rgb_ycc_start;
  22.448 +      cconvert->pub.color_convert = cmyk_ycck_convert;
  22.449 +    } else if (cinfo->in_color_space == JCS_YCCK)
  22.450 +      cconvert->pub.color_convert = null_convert;
  22.451 +    else
  22.452 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.453 +    break;
  22.454 +
  22.455 +  default:			/* allow null conversion of JCS_UNKNOWN */
  22.456 +    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  22.457 +	cinfo->num_components != cinfo->input_components)
  22.458 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  22.459 +    cconvert->pub.color_convert = null_convert;
  22.460 +    break;
  22.461 +  }
  22.462 +}
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/libs/libjpeg/jcdctmgr.c	Sun Jun 07 17:25:49 2015 +0300
    23.3 @@ -0,0 +1,387 @@
    23.4 +/*
    23.5 + * jcdctmgr.c
    23.6 + *
    23.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    23.8 + * This file is part of the Independent JPEG Group's software.
    23.9 + * For conditions of distribution and use, see the accompanying README file.
   23.10 + *
   23.11 + * This file contains the forward-DCT management logic.
   23.12 + * This code selects a particular DCT implementation to be used,
   23.13 + * and it performs related housekeeping chores including coefficient
   23.14 + * quantization.
   23.15 + */
   23.16 +
   23.17 +#define JPEG_INTERNALS
   23.18 +#include "jinclude.h"
   23.19 +#include "jpeglib.h"
   23.20 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   23.21 +
   23.22 +
   23.23 +/* Private subobject for this module */
   23.24 +
   23.25 +typedef struct {
   23.26 +  struct jpeg_forward_dct pub;	/* public fields */
   23.27 +
   23.28 +  /* Pointer to the DCT routine actually in use */
   23.29 +  forward_DCT_method_ptr do_dct;
   23.30 +
   23.31 +  /* The actual post-DCT divisors --- not identical to the quant table
   23.32 +   * entries, because of scaling (especially for an unnormalized DCT).
   23.33 +   * Each table is given in normal array order.
   23.34 +   */
   23.35 +  DCTELEM * divisors[NUM_QUANT_TBLS];
   23.36 +
   23.37 +#ifdef DCT_FLOAT_SUPPORTED
   23.38 +  /* Same as above for the floating-point case. */
   23.39 +  float_DCT_method_ptr do_float_dct;
   23.40 +  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
   23.41 +#endif
   23.42 +} my_fdct_controller;
   23.43 +
   23.44 +typedef my_fdct_controller * my_fdct_ptr;
   23.45 +
   23.46 +
   23.47 +/*
   23.48 + * Initialize for a processing pass.
   23.49 + * Verify that all referenced Q-tables are present, and set up
   23.50 + * the divisor table for each one.
   23.51 + * In the current implementation, DCT of all components is done during
   23.52 + * the first pass, even if only some components will be output in the
   23.53 + * first scan.  Hence all components should be examined here.
   23.54 + */
   23.55 +
   23.56 +METHODDEF(void)
   23.57 +start_pass_fdctmgr (j_compress_ptr cinfo)
   23.58 +{
   23.59 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
   23.60 +  int ci, qtblno, i;
   23.61 +  jpeg_component_info *compptr;
   23.62 +  JQUANT_TBL * qtbl;
   23.63 +  DCTELEM * dtbl;
   23.64 +
   23.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   23.66 +       ci++, compptr++) {
   23.67 +    qtblno = compptr->quant_tbl_no;
   23.68 +    /* Make sure specified quantization table is present */
   23.69 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
   23.70 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
   23.71 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
   23.72 +    qtbl = cinfo->quant_tbl_ptrs[qtblno];
   23.73 +    /* Compute divisors for this quant table */
   23.74 +    /* We may do this more than once for same table, but it's not a big deal */
   23.75 +    switch (cinfo->dct_method) {
   23.76 +#ifdef DCT_ISLOW_SUPPORTED
   23.77 +    case JDCT_ISLOW:
   23.78 +      /* For LL&M IDCT method, divisors are equal to raw quantization
   23.79 +       * coefficients multiplied by 8 (to counteract scaling).
   23.80 +       */
   23.81 +      if (fdct->divisors[qtblno] == NULL) {
   23.82 +	fdct->divisors[qtblno] = (DCTELEM *)
   23.83 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   23.84 +				      DCTSIZE2 * SIZEOF(DCTELEM));
   23.85 +      }
   23.86 +      dtbl = fdct->divisors[qtblno];
   23.87 +      for (i = 0; i < DCTSIZE2; i++) {
   23.88 +	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
   23.89 +      }
   23.90 +      break;
   23.91 +#endif
   23.92 +#ifdef DCT_IFAST_SUPPORTED
   23.93 +    case JDCT_IFAST:
   23.94 +      {
   23.95 +	/* For AA&N IDCT method, divisors are equal to quantization
   23.96 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   23.97 +	 *   scalefactor[0] = 1
   23.98 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   23.99 +	 * We apply a further scale factor of 8.
  23.100 +	 */
  23.101 +#define CONST_BITS 14
  23.102 +	static const INT16 aanscales[DCTSIZE2] = {
  23.103 +	  /* precomputed values scaled up by 14 bits */
  23.104 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  23.105 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  23.106 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  23.107 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  23.108 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  23.109 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  23.110 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  23.111 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  23.112 +	};
  23.113 +	SHIFT_TEMPS
  23.114 +
  23.115 +	if (fdct->divisors[qtblno] == NULL) {
  23.116 +	  fdct->divisors[qtblno] = (DCTELEM *)
  23.117 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  23.118 +					DCTSIZE2 * SIZEOF(DCTELEM));
  23.119 +	}
  23.120 +	dtbl = fdct->divisors[qtblno];
  23.121 +	for (i = 0; i < DCTSIZE2; i++) {
  23.122 +	  dtbl[i] = (DCTELEM)
  23.123 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  23.124 +				  (INT32) aanscales[i]),
  23.125 +		    CONST_BITS-3);
  23.126 +	}
  23.127 +      }
  23.128 +      break;
  23.129 +#endif
  23.130 +#ifdef DCT_FLOAT_SUPPORTED
  23.131 +    case JDCT_FLOAT:
  23.132 +      {
  23.133 +	/* For float AA&N IDCT method, divisors are equal to quantization
  23.134 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  23.135 +	 *   scalefactor[0] = 1
  23.136 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  23.137 +	 * We apply a further scale factor of 8.
  23.138 +	 * What's actually stored is 1/divisor so that the inner loop can
  23.139 +	 * use a multiplication rather than a division.
  23.140 +	 */
  23.141 +	FAST_FLOAT * fdtbl;
  23.142 +	int row, col;
  23.143 +	static const double aanscalefactor[DCTSIZE] = {
  23.144 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
  23.145 +	  1.0, 0.785694958, 0.541196100, 0.275899379
  23.146 +	};
  23.147 +
  23.148 +	if (fdct->float_divisors[qtblno] == NULL) {
  23.149 +	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  23.150 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  23.151 +					DCTSIZE2 * SIZEOF(FAST_FLOAT));
  23.152 +	}
  23.153 +	fdtbl = fdct->float_divisors[qtblno];
  23.154 +	i = 0;
  23.155 +	for (row = 0; row < DCTSIZE; row++) {
  23.156 +	  for (col = 0; col < DCTSIZE; col++) {
  23.157 +	    fdtbl[i] = (FAST_FLOAT)
  23.158 +	      (1.0 / (((double) qtbl->quantval[i] *
  23.159 +		       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  23.160 +	    i++;
  23.161 +	  }
  23.162 +	}
  23.163 +      }
  23.164 +      break;
  23.165 +#endif
  23.166 +    default:
  23.167 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  23.168 +      break;
  23.169 +    }
  23.170 +  }
  23.171 +}
  23.172 +
  23.173 +
  23.174 +/*
  23.175 + * Perform forward DCT on one or more blocks of a component.
  23.176 + *
  23.177 + * The input samples are taken from the sample_data[] array starting at
  23.178 + * position start_row/start_col, and moving to the right for any additional
  23.179 + * blocks. The quantized coefficients are returned in coef_blocks[].
  23.180 + */
  23.181 +
  23.182 +METHODDEF(void)
  23.183 +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  23.184 +	     JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  23.185 +	     JDIMENSION start_row, JDIMENSION start_col,
  23.186 +	     JDIMENSION num_blocks)
  23.187 +/* This version is used for integer DCT implementations. */
  23.188 +{
  23.189 +  /* This routine is heavily used, so it's worth coding it tightly. */
  23.190 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  23.191 +  forward_DCT_method_ptr do_dct = fdct->do_dct;
  23.192 +  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  23.193 +  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
  23.194 +  JDIMENSION bi;
  23.195 +
  23.196 +  sample_data += start_row;	/* fold in the vertical offset once */
  23.197 +
  23.198 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  23.199 +    /* Load data into workspace, applying unsigned->signed conversion */
  23.200 +    { register DCTELEM *workspaceptr;
  23.201 +      register JSAMPROW elemptr;
  23.202 +      register int elemr;
  23.203 +
  23.204 +      workspaceptr = workspace;
  23.205 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  23.206 +	elemptr = sample_data[elemr] + start_col;
  23.207 +#if DCTSIZE == 8		/* unroll the inner loop */
  23.208 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.209 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.210 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.211 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.212 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.213 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.214 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.215 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.216 +#else
  23.217 +	{ register int elemc;
  23.218 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  23.219 +	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  23.220 +	  }
  23.221 +	}
  23.222 +#endif
  23.223 +      }
  23.224 +    }
  23.225 +
  23.226 +    /* Perform the DCT */
  23.227 +    (*do_dct) (workspace);
  23.228 +
  23.229 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  23.230 +    { register DCTELEM temp, qval;
  23.231 +      register int i;
  23.232 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  23.233 +
  23.234 +      for (i = 0; i < DCTSIZE2; i++) {
  23.235 +	qval = divisors[i];
  23.236 +	temp = workspace[i];
  23.237 +	/* Divide the coefficient value by qval, ensuring proper rounding.
  23.238 +	 * Since C does not specify the direction of rounding for negative
  23.239 +	 * quotients, we have to force the dividend positive for portability.
  23.240 +	 *
  23.241 +	 * In most files, at least half of the output values will be zero
  23.242 +	 * (at default quantization settings, more like three-quarters...)
  23.243 +	 * so we should ensure that this case is fast.  On many machines,
  23.244 +	 * a comparison is enough cheaper than a divide to make a special test
  23.245 +	 * a win.  Since both inputs will be nonnegative, we need only test
  23.246 +	 * for a < b to discover whether a/b is 0.
  23.247 +	 * If your machine's division is fast enough, define FAST_DIVIDE.
  23.248 +	 */
  23.249 +#ifdef FAST_DIVIDE
  23.250 +#define DIVIDE_BY(a,b)	a /= b
  23.251 +#else
  23.252 +#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
  23.253 +#endif
  23.254 +	if (temp < 0) {
  23.255 +	  temp = -temp;
  23.256 +	  temp += qval>>1;	/* for rounding */
  23.257 +	  DIVIDE_BY(temp, qval);
  23.258 +	  temp = -temp;
  23.259 +	} else {
  23.260 +	  temp += qval>>1;	/* for rounding */
  23.261 +	  DIVIDE_BY(temp, qval);
  23.262 +	}
  23.263 +	output_ptr[i] = (JCOEF) temp;
  23.264 +      }
  23.265 +    }
  23.266 +  }
  23.267 +}
  23.268 +
  23.269 +
  23.270 +#ifdef DCT_FLOAT_SUPPORTED
  23.271 +
  23.272 +METHODDEF(void)
  23.273 +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  23.274 +		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  23.275 +		   JDIMENSION start_row, JDIMENSION start_col,
  23.276 +		   JDIMENSION num_blocks)
  23.277 +/* This version is used for floating-point DCT implementations. */
  23.278 +{
  23.279 +  /* This routine is heavily used, so it's worth coding it tightly. */
  23.280 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  23.281 +  float_DCT_method_ptr do_dct = fdct->do_float_dct;
  23.282 +  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  23.283 +  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  23.284 +  JDIMENSION bi;
  23.285 +
  23.286 +  sample_data += start_row;	/* fold in the vertical offset once */
  23.287 +
  23.288 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  23.289 +    /* Load data into workspace, applying unsigned->signed conversion */
  23.290 +    { register FAST_FLOAT *workspaceptr;
  23.291 +      register JSAMPROW elemptr;
  23.292 +      register int elemr;
  23.293 +
  23.294 +      workspaceptr = workspace;
  23.295 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  23.296 +	elemptr = sample_data[elemr] + start_col;
  23.297 +#if DCTSIZE == 8		/* unroll the inner loop */
  23.298 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.299 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.300 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.301 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.302 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.303 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.304 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.305 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.306 +#else
  23.307 +	{ register int elemc;
  23.308 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  23.309 +	    *workspaceptr++ = (FAST_FLOAT)
  23.310 +	      (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  23.311 +	  }
  23.312 +	}
  23.313 +#endif
  23.314 +      }
  23.315 +    }
  23.316 +
  23.317 +    /* Perform the DCT */
  23.318 +    (*do_dct) (workspace);
  23.319 +
  23.320 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  23.321 +    { register FAST_FLOAT temp;
  23.322 +      register int i;
  23.323 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  23.324 +
  23.325 +      for (i = 0; i < DCTSIZE2; i++) {
  23.326 +	/* Apply the quantization and scaling factor */
  23.327 +	temp = workspace[i] * divisors[i];
  23.328 +	/* Round to nearest integer.
  23.329 +	 * Since C does not specify the direction of rounding for negative
  23.330 +	 * quotients, we have to force the dividend positive for portability.
  23.331 +	 * The maximum coefficient size is +-16K (for 12-bit data), so this
  23.332 +	 * code should work for either 16-bit or 32-bit ints.
  23.333 +	 */
  23.334 +	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  23.335 +      }
  23.336 +    }
  23.337 +  }
  23.338 +}
  23.339 +
  23.340 +#endif /* DCT_FLOAT_SUPPORTED */
  23.341 +
  23.342 +
  23.343 +/*
  23.344 + * Initialize FDCT manager.
  23.345 + */
  23.346 +
  23.347 +GLOBAL(void)
  23.348 +jinit_forward_dct (j_compress_ptr cinfo)
  23.349 +{
  23.350 +  my_fdct_ptr fdct;
  23.351 +  int i;
  23.352 +
  23.353 +  fdct = (my_fdct_ptr)
  23.354 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  23.355 +				SIZEOF(my_fdct_controller));
  23.356 +  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  23.357 +  fdct->pub.start_pass = start_pass_fdctmgr;
  23.358 +
  23.359 +  switch (cinfo->dct_method) {
  23.360 +#ifdef DCT_ISLOW_SUPPORTED
  23.361 +  case JDCT_ISLOW:
  23.362 +    fdct->pub.forward_DCT = forward_DCT;
  23.363 +    fdct->do_dct = jpeg_fdct_islow;
  23.364 +    break;
  23.365 +#endif
  23.366 +#ifdef DCT_IFAST_SUPPORTED
  23.367 +  case JDCT_IFAST:
  23.368 +    fdct->pub.forward_DCT = forward_DCT;
  23.369 +    fdct->do_dct = jpeg_fdct_ifast;
  23.370 +    break;
  23.371 +#endif
  23.372 +#ifdef DCT_FLOAT_SUPPORTED
  23.373 +  case JDCT_FLOAT:
  23.374 +    fdct->pub.forward_DCT = forward_DCT_float;
  23.375 +    fdct->do_float_dct = jpeg_fdct_float;
  23.376 +    break;
  23.377 +#endif
  23.378 +  default:
  23.379 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  23.380 +    break;
  23.381 +  }
  23.382 +
  23.383 +  /* Mark divisor tables unallocated */
  23.384 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  23.385 +    fdct->divisors[i] = NULL;
  23.386 +#ifdef DCT_FLOAT_SUPPORTED
  23.387 +    fdct->float_divisors[i] = NULL;
  23.388 +#endif
  23.389 +  }
  23.390 +}
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/libs/libjpeg/jchuff.c	Sun Jun 07 17:25:49 2015 +0300
    24.3 @@ -0,0 +1,909 @@
    24.4 +/*
    24.5 + * jchuff.c
    24.6 + *
    24.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    24.8 + * This file is part of the Independent JPEG Group's software.
    24.9 + * For conditions of distribution and use, see the accompanying README file.
   24.10 + *
   24.11 + * This file contains Huffman entropy encoding routines.
   24.12 + *
   24.13 + * Much of the complexity here has to do with supporting output suspension.
   24.14 + * If the data destination module demands suspension, we want to be able to
   24.15 + * back up to the start of the current MCU.  To do this, we copy state
   24.16 + * variables into local working storage, and update them back to the
   24.17 + * permanent JPEG objects only upon successful completion of an MCU.
   24.18 + */
   24.19 +
   24.20 +#define JPEG_INTERNALS
   24.21 +#include "jinclude.h"
   24.22 +#include "jpeglib.h"
   24.23 +#include "jchuff.h"		/* Declarations shared with jcphuff.c */
   24.24 +
   24.25 +
   24.26 +/* Expanded entropy encoder object for Huffman encoding.
   24.27 + *
   24.28 + * The savable_state subrecord contains fields that change within an MCU,
   24.29 + * but must not be updated permanently until we complete the MCU.
   24.30 + */
   24.31 +
   24.32 +typedef struct {
   24.33 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   24.34 +  int put_bits;			/* # of bits now in it */
   24.35 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   24.36 +} savable_state;
   24.37 +
   24.38 +/* This macro is to work around compilers with missing or broken
   24.39 + * structure assignment.  You'll need to fix this code if you have
   24.40 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   24.41 + */
   24.42 +
   24.43 +#ifndef NO_STRUCT_ASSIGN
   24.44 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   24.45 +#else
   24.46 +#if MAX_COMPS_IN_SCAN == 4
   24.47 +#define ASSIGN_STATE(dest,src)  \
   24.48 +	((dest).put_buffer = (src).put_buffer, \
   24.49 +	 (dest).put_bits = (src).put_bits, \
   24.50 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
   24.51 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   24.52 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   24.53 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   24.54 +#endif
   24.55 +#endif
   24.56 +
   24.57 +
   24.58 +typedef struct {
   24.59 +  struct jpeg_entropy_encoder pub; /* public fields */
   24.60 +
   24.61 +  savable_state saved;		/* Bit buffer & DC state at start of MCU */
   24.62 +
   24.63 +  /* These fields are NOT loaded into local working state. */
   24.64 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   24.65 +  int next_restart_num;		/* next restart number to write (0-7) */
   24.66 +
   24.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   24.68 +  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   24.69 +  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   24.70 +
   24.71 +#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
   24.72 +  long * dc_count_ptrs[NUM_HUFF_TBLS];
   24.73 +  long * ac_count_ptrs[NUM_HUFF_TBLS];
   24.74 +#endif
   24.75 +} huff_entropy_encoder;
   24.76 +
   24.77 +typedef huff_entropy_encoder * huff_entropy_ptr;
   24.78 +
   24.79 +/* Working state while writing an MCU.
   24.80 + * This struct contains all the fields that are needed by subroutines.
   24.81 + */
   24.82 +
   24.83 +typedef struct {
   24.84 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   24.85 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   24.86 +  savable_state cur;		/* Current bit buffer & DC state */
   24.87 +  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
   24.88 +} working_state;
   24.89 +
   24.90 +
   24.91 +/* Forward declarations */
   24.92 +METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
   24.93 +					JBLOCKROW *MCU_data));
   24.94 +METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
   24.95 +#ifdef ENTROPY_OPT_SUPPORTED
   24.96 +METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
   24.97 +					  JBLOCKROW *MCU_data));
   24.98 +METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
   24.99 +#endif
  24.100 +
  24.101 +
  24.102 +/*
  24.103 + * Initialize for a Huffman-compressed scan.
  24.104 + * If gather_statistics is TRUE, we do not output anything during the scan,
  24.105 + * just count the Huffman symbols used and generate Huffman code tables.
  24.106 + */
  24.107 +
  24.108 +METHODDEF(void)
  24.109 +start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
  24.110 +{
  24.111 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  24.112 +  int ci, dctbl, actbl;
  24.113 +  jpeg_component_info * compptr;
  24.114 +
  24.115 +  if (gather_statistics) {
  24.116 +#ifdef ENTROPY_OPT_SUPPORTED
  24.117 +    entropy->pub.encode_mcu = encode_mcu_gather;
  24.118 +    entropy->pub.finish_pass = finish_pass_gather;
  24.119 +#else
  24.120 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  24.121 +#endif
  24.122 +  } else {
  24.123 +    entropy->pub.encode_mcu = encode_mcu_huff;
  24.124 +    entropy->pub.finish_pass = finish_pass_huff;
  24.125 +  }
  24.126 +
  24.127 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  24.128 +    compptr = cinfo->cur_comp_info[ci];
  24.129 +    dctbl = compptr->dc_tbl_no;
  24.130 +    actbl = compptr->ac_tbl_no;
  24.131 +    if (gather_statistics) {
  24.132 +#ifdef ENTROPY_OPT_SUPPORTED
  24.133 +      /* Check for invalid table indexes */
  24.134 +      /* (make_c_derived_tbl does this in the other path) */
  24.135 +      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
  24.136 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  24.137 +      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
  24.138 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  24.139 +      /* Allocate and zero the statistics tables */
  24.140 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  24.141 +      if (entropy->dc_count_ptrs[dctbl] == NULL)
  24.142 +	entropy->dc_count_ptrs[dctbl] = (long *)
  24.143 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.144 +				      257 * SIZEOF(long));
  24.145 +      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
  24.146 +      if (entropy->ac_count_ptrs[actbl] == NULL)
  24.147 +	entropy->ac_count_ptrs[actbl] = (long *)
  24.148 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.149 +				      257 * SIZEOF(long));
  24.150 +      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
  24.151 +#endif
  24.152 +    } else {
  24.153 +      /* Compute derived values for Huffman tables */
  24.154 +      /* We may do this more than once for a table, but it's not expensive */
  24.155 +      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
  24.156 +			      & entropy->dc_derived_tbls[dctbl]);
  24.157 +      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
  24.158 +			      & entropy->ac_derived_tbls[actbl]);
  24.159 +    }
  24.160 +    /* Initialize DC predictions to 0 */
  24.161 +    entropy->saved.last_dc_val[ci] = 0;
  24.162 +  }
  24.163 +
  24.164 +  /* Initialize bit buffer to empty */
  24.165 +  entropy->saved.put_buffer = 0;
  24.166 +  entropy->saved.put_bits = 0;
  24.167 +
  24.168 +  /* Initialize restart stuff */
  24.169 +  entropy->restarts_to_go = cinfo->restart_interval;
  24.170 +  entropy->next_restart_num = 0;
  24.171 +}
  24.172 +
  24.173 +
  24.174 +/*
  24.175 + * Compute the derived values for a Huffman table.
  24.176 + * This routine also performs some validation checks on the table.
  24.177 + *
  24.178 + * Note this is also used by jcphuff.c.
  24.179 + */
  24.180 +
  24.181 +GLOBAL(void)
  24.182 +jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
  24.183 +			 c_derived_tbl ** pdtbl)
  24.184 +{
  24.185 +  JHUFF_TBL *htbl;
  24.186 +  c_derived_tbl *dtbl;
  24.187 +  int p, i, l, lastp, si, maxsymbol;
  24.188 +  char huffsize[257];
  24.189 +  unsigned int huffcode[257];
  24.190 +  unsigned int code;
  24.191 +
  24.192 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
  24.193 +   * paralleling the order of the symbols themselves in htbl->huffval[].
  24.194 +   */
  24.195 +
  24.196 +  /* Find the input Huffman table */
  24.197 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  24.198 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  24.199 +  htbl =
  24.200 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  24.201 +  if (htbl == NULL)
  24.202 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  24.203 +
  24.204 +  /* Allocate a workspace if we haven't already done so. */
  24.205 +  if (*pdtbl == NULL)
  24.206 +    *pdtbl = (c_derived_tbl *)
  24.207 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.208 +				  SIZEOF(c_derived_tbl));
  24.209 +  dtbl = *pdtbl;
  24.210 +  
  24.211 +  /* Figure C.1: make table of Huffman code length for each symbol */
  24.212 +
  24.213 +  p = 0;
  24.214 +  for (l = 1; l <= 16; l++) {
  24.215 +    i = (int) htbl->bits[l];
  24.216 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
  24.217 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  24.218 +    while (i--)
  24.219 +      huffsize[p++] = (char) l;
  24.220 +  }
  24.221 +  huffsize[p] = 0;
  24.222 +  lastp = p;
  24.223 +  
  24.224 +  /* Figure C.2: generate the codes themselves */
  24.225 +  /* We also validate that the counts represent a legal Huffman code tree. */
  24.226 +
  24.227 +  code = 0;
  24.228 +  si = huffsize[0];
  24.229 +  p = 0;
  24.230 +  while (huffsize[p]) {
  24.231 +    while (((int) huffsize[p]) == si) {
  24.232 +      huffcode[p++] = code;
  24.233 +      code++;
  24.234 +    }
  24.235 +    /* code is now 1 more than the last code used for codelength si; but
  24.236 +     * it must still fit in si bits, since no code is allowed to be all ones.
  24.237 +     */
  24.238 +    if (((INT32) code) >= (((INT32) 1) << si))
  24.239 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  24.240 +    code <<= 1;
  24.241 +    si++;
  24.242 +  }
  24.243 +  
  24.244 +  /* Figure C.3: generate encoding tables */
  24.245 +  /* These are code and size indexed by symbol value */
  24.246 +
  24.247 +  /* Set all codeless symbols to have code length 0;
  24.248 +   * this lets us detect duplicate VAL entries here, and later
  24.249 +   * allows emit_bits to detect any attempt to emit such symbols.
  24.250 +   */
  24.251 +  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
  24.252 +
  24.253 +  /* This is also a convenient place to check for out-of-range
  24.254 +   * and duplicated VAL entries.  We allow 0..255 for AC symbols
  24.255 +   * but only 0..15 for DC.  (We could constrain them further
  24.256 +   * based on data depth and mode, but this seems enough.)
  24.257 +   */
  24.258 +  maxsymbol = isDC ? 15 : 255;
  24.259 +
  24.260 +  for (p = 0; p < lastp; p++) {
  24.261 +    i = htbl->huffval[p];
  24.262 +    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
  24.263 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  24.264 +    dtbl->ehufco[i] = huffcode[p];
  24.265 +    dtbl->ehufsi[i] = huffsize[p];
  24.266 +  }
  24.267 +}
  24.268 +
  24.269 +
  24.270 +/* Outputting bytes to the file */
  24.271 +
  24.272 +/* Emit a byte, taking 'action' if must suspend. */
  24.273 +#define emit_byte(state,val,action)  \
  24.274 +	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
  24.275 +	  if (--(state)->free_in_buffer == 0)  \
  24.276 +	    if (! dump_buffer(state))  \
  24.277 +	      { action; } }
  24.278 +
  24.279 +
  24.280 +LOCAL(boolean)
  24.281 +dump_buffer (working_state * state)
  24.282 +/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  24.283 +{
  24.284 +  struct jpeg_destination_mgr * dest = state->cinfo->dest;
  24.285 +
  24.286 +  if (! (*dest->empty_output_buffer) (state->cinfo))
  24.287 +    return FALSE;
  24.288 +  /* After a successful buffer dump, must reset buffer pointers */
  24.289 +  state->next_output_byte = dest->next_output_byte;
  24.290 +  state->free_in_buffer = dest->free_in_buffer;
  24.291 +  return TRUE;
  24.292 +}
  24.293 +
  24.294 +
  24.295 +/* Outputting bits to the file */
  24.296 +
  24.297 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  24.298 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  24.299 + * in one call, and we never retain more than 7 bits in put_buffer
  24.300 + * between calls, so 24 bits are sufficient.
  24.301 + */
  24.302 +
  24.303 +INLINE
  24.304 +LOCAL(boolean)
  24.305 +emit_bits (working_state * state, unsigned int code, int size)
  24.306 +/* Emit some bits; return TRUE if successful, FALSE if must suspend */
  24.307 +{
  24.308 +  /* This routine is heavily used, so it's worth coding tightly. */
  24.309 +  register INT32 put_buffer = (INT32) code;
  24.310 +  register int put_bits = state->cur.put_bits;
  24.311 +
  24.312 +  /* if size is 0, caller used an invalid Huffman table entry */
  24.313 +  if (size == 0)
  24.314 +    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
  24.315 +
  24.316 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  24.317 +  
  24.318 +  put_bits += size;		/* new number of bits in buffer */
  24.319 +  
  24.320 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  24.321 +
  24.322 +  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  24.323 +  
  24.324 +  while (put_bits >= 8) {
  24.325 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  24.326 +    
  24.327 +    emit_byte(state, c, return FALSE);
  24.328 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  24.329 +      emit_byte(state, 0, return FALSE);
  24.330 +    }
  24.331 +    put_buffer <<= 8;
  24.332 +    put_bits -= 8;
  24.333 +  }
  24.334 +
  24.335 +  state->cur.put_buffer = put_buffer; /* update state variables */
  24.336 +  state->cur.put_bits = put_bits;
  24.337 +
  24.338 +  return TRUE;
  24.339 +}
  24.340 +
  24.341 +
  24.342 +LOCAL(boolean)
  24.343 +flush_bits (working_state * state)
  24.344 +{
  24.345 +  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
  24.346 +    return FALSE;
  24.347 +  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
  24.348 +  state->cur.put_bits = 0;
  24.349 +  return TRUE;
  24.350 +}
  24.351 +
  24.352 +
  24.353 +/* Encode a single block's worth of coefficients */
  24.354 +
  24.355 +LOCAL(boolean)
  24.356 +encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
  24.357 +		  c_derived_tbl *dctbl, c_derived_tbl *actbl)
  24.358 +{
  24.359 +  register int temp, temp2;
  24.360 +  register int nbits;
  24.361 +  register int k, r, i;
  24.362 +  
  24.363 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  24.364 +  
  24.365 +  temp = temp2 = block[0] - last_dc_val;
  24.366 +
  24.367 +  if (temp < 0) {
  24.368 +    temp = -temp;		/* temp is abs value of input */
  24.369 +    /* For a negative input, want temp2 = bitwise complement of abs(input) */
  24.370 +    /* This code assumes we are on a two's complement machine */
  24.371 +    temp2--;
  24.372 +  }
  24.373 +  
  24.374 +  /* Find the number of bits needed for the magnitude of the coefficient */
  24.375 +  nbits = 0;
  24.376 +  while (temp) {
  24.377 +    nbits++;
  24.378 +    temp >>= 1;
  24.379 +  }
  24.380 +  /* Check for out-of-range coefficient values.
  24.381 +   * Since we're encoding a difference, the range limit is twice as much.
  24.382 +   */
  24.383 +  if (nbits > MAX_COEF_BITS+1)
  24.384 +    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  24.385 +  
  24.386 +  /* Emit the Huffman-coded symbol for the number of bits */
  24.387 +  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
  24.388 +    return FALSE;
  24.389 +
  24.390 +  /* Emit that number of bits of the value, if positive, */
  24.391 +  /* or the complement of its magnitude, if negative. */
  24.392 +  if (nbits)			/* emit_bits rejects calls with size 0 */
  24.393 +    if (! emit_bits(state, (unsigned int) temp2, nbits))
  24.394 +      return FALSE;
  24.395 +
  24.396 +  /* Encode the AC coefficients per section F.1.2.2 */
  24.397 +  
  24.398 +  r = 0;			/* r = run length of zeros */
  24.399 +  
  24.400 +  for (k = 1; k < DCTSIZE2; k++) {
  24.401 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  24.402 +      r++;
  24.403 +    } else {
  24.404 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  24.405 +      while (r > 15) {
  24.406 +	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
  24.407 +	  return FALSE;
  24.408 +	r -= 16;
  24.409 +      }
  24.410 +
  24.411 +      temp2 = temp;
  24.412 +      if (temp < 0) {
  24.413 +	temp = -temp;		/* temp is abs value of input */
  24.414 +	/* This code assumes we are on a two's complement machine */
  24.415 +	temp2--;
  24.416 +      }
  24.417 +      
  24.418 +      /* Find the number of bits needed for the magnitude of the coefficient */
  24.419 +      nbits = 1;		/* there must be at least one 1 bit */
  24.420 +      while ((temp >>= 1))
  24.421 +	nbits++;
  24.422 +      /* Check for out-of-range coefficient values */
  24.423 +      if (nbits > MAX_COEF_BITS)
  24.424 +	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  24.425 +      
  24.426 +      /* Emit Huffman symbol for run length / number of bits */
  24.427 +      i = (r << 4) + nbits;
  24.428 +      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
  24.429 +	return FALSE;
  24.430 +
  24.431 +      /* Emit that number of bits of the value, if positive, */
  24.432 +      /* or the complement of its magnitude, if negative. */
  24.433 +      if (! emit_bits(state, (unsigned int) temp2, nbits))
  24.434 +	return FALSE;
  24.435 +      
  24.436 +      r = 0;
  24.437 +    }
  24.438 +  }
  24.439 +
  24.440 +  /* If the last coef(s) were zero, emit an end-of-block code */
  24.441 +  if (r > 0)
  24.442 +    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
  24.443 +      return FALSE;
  24.444 +
  24.445 +  return TRUE;
  24.446 +}
  24.447 +
  24.448 +
  24.449 +/*
  24.450 + * Emit a restart marker & resynchronize predictions.
  24.451 + */
  24.452 +
  24.453 +LOCAL(boolean)
  24.454 +emit_restart (working_state * state, int restart_num)
  24.455 +{
  24.456 +  int ci;
  24.457 +
  24.458 +  if (! flush_bits(state))
  24.459 +    return FALSE;
  24.460 +
  24.461 +  emit_byte(state, 0xFF, return FALSE);
  24.462 +  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  24.463 +
  24.464 +  /* Re-initialize DC predictions to 0 */
  24.465 +  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  24.466 +    state->cur.last_dc_val[ci] = 0;
  24.467 +
  24.468 +  /* The restart counter is not updated until we successfully write the MCU. */
  24.469 +
  24.470 +  return TRUE;
  24.471 +}
  24.472 +
  24.473 +
  24.474 +/*
  24.475 + * Encode and output one MCU's worth of Huffman-compressed coefficients.
  24.476 + */
  24.477 +
  24.478 +METHODDEF(boolean)
  24.479 +encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  24.480 +{
  24.481 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  24.482 +  working_state state;
  24.483 +  int blkn, ci;
  24.484 +  jpeg_component_info * compptr;
  24.485 +
  24.486 +  /* Load up working state */
  24.487 +  state.next_output_byte = cinfo->dest->next_output_byte;
  24.488 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  24.489 +  ASSIGN_STATE(state.cur, entropy->saved);
  24.490 +  state.cinfo = cinfo;
  24.491 +
  24.492 +  /* Emit restart marker if needed */
  24.493 +  if (cinfo->restart_interval) {
  24.494 +    if (entropy->restarts_to_go == 0)
  24.495 +      if (! emit_restart(&state, entropy->next_restart_num))
  24.496 +	return FALSE;
  24.497 +  }
  24.498 +
  24.499 +  /* Encode the MCU data blocks */
  24.500 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  24.501 +    ci = cinfo->MCU_membership[blkn];
  24.502 +    compptr = cinfo->cur_comp_info[ci];
  24.503 +    if (! encode_one_block(&state,
  24.504 +			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
  24.505 +			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
  24.506 +			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  24.507 +      return FALSE;
  24.508 +    /* Update last_dc_val */
  24.509 +    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  24.510 +  }
  24.511 +
  24.512 +  /* Completed MCU, so update state */
  24.513 +  cinfo->dest->next_output_byte = state.next_output_byte;
  24.514 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  24.515 +  ASSIGN_STATE(entropy->saved, state.cur);
  24.516 +
  24.517 +  /* Update restart-interval state too */
  24.518 +  if (cinfo->restart_interval) {
  24.519 +    if (entropy->restarts_to_go == 0) {
  24.520 +      entropy->restarts_to_go = cinfo->restart_interval;
  24.521 +      entropy->next_restart_num++;
  24.522 +      entropy->next_restart_num &= 7;
  24.523 +    }
  24.524 +    entropy->restarts_to_go--;
  24.525 +  }
  24.526 +
  24.527 +  return TRUE;
  24.528 +}
  24.529 +
  24.530 +
  24.531 +/*
  24.532 + * Finish up at the end of a Huffman-compressed scan.
  24.533 + */
  24.534 +
  24.535 +METHODDEF(void)
  24.536 +finish_pass_huff (j_compress_ptr cinfo)
  24.537 +{
  24.538 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  24.539 +  working_state state;
  24.540 +
  24.541 +  /* Load up working state ... flush_bits needs it */
  24.542 +  state.next_output_byte = cinfo->dest->next_output_byte;
  24.543 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  24.544 +  ASSIGN_STATE(state.cur, entropy->saved);
  24.545 +  state.cinfo = cinfo;
  24.546 +
  24.547 +  /* Flush out the last data */
  24.548 +  if (! flush_bits(&state))
  24.549 +    ERREXIT(cinfo, JERR_CANT_SUSPEND);
  24.550 +
  24.551 +  /* Update state */
  24.552 +  cinfo->dest->next_output_byte = state.next_output_byte;
  24.553 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  24.554 +  ASSIGN_STATE(entropy->saved, state.cur);
  24.555 +}
  24.556 +
  24.557 +
  24.558 +/*
  24.559 + * Huffman coding optimization.
  24.560 + *
  24.561 + * We first scan the supplied data and count the number of uses of each symbol
  24.562 + * that is to be Huffman-coded. (This process MUST agree with the code above.)
  24.563 + * Then we build a Huffman coding tree for the observed counts.
  24.564 + * Symbols which are not needed at all for the particular image are not
  24.565 + * assigned any code, which saves space in the DHT marker as well as in
  24.566 + * the compressed data.
  24.567 + */
  24.568 +
  24.569 +#ifdef ENTROPY_OPT_SUPPORTED
  24.570 +
  24.571 +
  24.572 +/* Process a single block's worth of coefficients */
  24.573 +
  24.574 +LOCAL(void)
  24.575 +htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
  24.576 +		 long dc_counts[], long ac_counts[])
  24.577 +{
  24.578 +  register int temp;
  24.579 +  register int nbits;
  24.580 +  register int k, r;
  24.581 +  
  24.582 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  24.583 +  
  24.584 +  temp = block[0] - last_dc_val;
  24.585 +  if (temp < 0)
  24.586 +    temp = -temp;
  24.587 +  
  24.588 +  /* Find the number of bits needed for the magnitude of the coefficient */
  24.589 +  nbits = 0;
  24.590 +  while (temp) {
  24.591 +    nbits++;
  24.592 +    temp >>= 1;
  24.593 +  }
  24.594 +  /* Check for out-of-range coefficient values.
  24.595 +   * Since we're encoding a difference, the range limit is twice as much.
  24.596 +   */
  24.597 +  if (nbits > MAX_COEF_BITS+1)
  24.598 +    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  24.599 +
  24.600 +  /* Count the Huffman symbol for the number of bits */
  24.601 +  dc_counts[nbits]++;
  24.602 +  
  24.603 +  /* Encode the AC coefficients per section F.1.2.2 */
  24.604 +  
  24.605 +  r = 0;			/* r = run length of zeros */
  24.606 +  
  24.607 +  for (k = 1; k < DCTSIZE2; k++) {
  24.608 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  24.609 +      r++;
  24.610 +    } else {
  24.611 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  24.612 +      while (r > 15) {
  24.613 +	ac_counts[0xF0]++;
  24.614 +	r -= 16;
  24.615 +      }
  24.616 +      
  24.617 +      /* Find the number of bits needed for the magnitude of the coefficient */
  24.618 +      if (temp < 0)
  24.619 +	temp = -temp;
  24.620 +      
  24.621 +      /* Find the number of bits needed for the magnitude of the coefficient */
  24.622 +      nbits = 1;		/* there must be at least one 1 bit */
  24.623 +      while ((temp >>= 1))
  24.624 +	nbits++;
  24.625 +      /* Check for out-of-range coefficient values */
  24.626 +      if (nbits > MAX_COEF_BITS)
  24.627 +	ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  24.628 +      
  24.629 +      /* Count Huffman symbol for run length / number of bits */
  24.630 +      ac_counts[(r << 4) + nbits]++;
  24.631 +      
  24.632 +      r = 0;
  24.633 +    }
  24.634 +  }
  24.635 +
  24.636 +  /* If the last coef(s) were zero, emit an end-of-block code */
  24.637 +  if (r > 0)
  24.638 +    ac_counts[0]++;
  24.639 +}
  24.640 +
  24.641 +
  24.642 +/*
  24.643 + * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  24.644 + * No data is actually output, so no suspension return is possible.
  24.645 + */
  24.646 +
  24.647 +METHODDEF(boolean)
  24.648 +encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  24.649 +{
  24.650 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  24.651 +  int blkn, ci;
  24.652 +  jpeg_component_info * compptr;
  24.653 +
  24.654 +  /* Take care of restart intervals if needed */
  24.655 +  if (cinfo->restart_interval) {
  24.656 +    if (entropy->restarts_to_go == 0) {
  24.657 +      /* Re-initialize DC predictions to 0 */
  24.658 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  24.659 +	entropy->saved.last_dc_val[ci] = 0;
  24.660 +      /* Update restart state */
  24.661 +      entropy->restarts_to_go = cinfo->restart_interval;
  24.662 +    }
  24.663 +    entropy->restarts_to_go--;
  24.664 +  }
  24.665 +
  24.666 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  24.667 +    ci = cinfo->MCU_membership[blkn];
  24.668 +    compptr = cinfo->cur_comp_info[ci];
  24.669 +    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  24.670 +		    entropy->dc_count_ptrs[compptr->dc_tbl_no],
  24.671 +		    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  24.672 +    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  24.673 +  }
  24.674 +
  24.675 +  return TRUE;
  24.676 +}
  24.677 +
  24.678 +
  24.679 +/*
  24.680 + * Generate the best Huffman code table for the given counts, fill htbl.
  24.681 + * Note this is also used by jcphuff.c.
  24.682 + *
  24.683 + * The JPEG standard requires that no symbol be assigned a codeword of all
  24.684 + * one bits (so that padding bits added at the end of a compressed segment
  24.685 + * can't look like a valid code).  Because of the canonical ordering of
  24.686 + * codewords, this just means that there must be an unused slot in the
  24.687 + * longest codeword length category.  Section K.2 of the JPEG spec suggests
  24.688 + * reserving such a slot by pretending that symbol 256 is a valid symbol
  24.689 + * with count 1.  In theory that's not optimal; giving it count zero but
  24.690 + * including it in the symbol set anyway should give a better Huffman code.
  24.691 + * But the theoretically better code actually seems to come out worse in
  24.692 + * practice, because it produces more all-ones bytes (which incur stuffed
  24.693 + * zero bytes in the final file).  In any case the difference is tiny.
  24.694 + *
  24.695 + * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  24.696 + * If some symbols have a very small but nonzero probability, the Huffman tree
  24.697 + * must be adjusted to meet the code length restriction.  We currently use
  24.698 + * the adjustment method suggested in JPEG section K.2.  This method is *not*
  24.699 + * optimal; it may not choose the best possible limited-length code.  But
  24.700 + * typically only very-low-frequency symbols will be given less-than-optimal
  24.701 + * lengths, so the code is almost optimal.  Experimental comparisons against
  24.702 + * an optimal limited-length-code algorithm indicate that the difference is
  24.703 + * microscopic --- usually less than a hundredth of a percent of total size.
  24.704 + * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
  24.705 + */
  24.706 +
  24.707 +GLOBAL(void)
  24.708 +jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
  24.709 +{
  24.710 +#define MAX_CLEN 32		/* assumed maximum initial code length */
  24.711 +  UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
  24.712 +  int codesize[257];		/* codesize[k] = code length of symbol k */
  24.713 +  int others[257];		/* next symbol in current branch of tree */
  24.714 +  int c1, c2;
  24.715 +  int p, i, j;
  24.716 +  long v;
  24.717 +
  24.718 +  /* This algorithm is explained in section K.2 of the JPEG standard */
  24.719 +
  24.720 +  MEMZERO(bits, SIZEOF(bits));
  24.721 +  MEMZERO(codesize, SIZEOF(codesize));
  24.722 +  for (i = 0; i < 257; i++)
  24.723 +    others[i] = -1;		/* init links to empty */
  24.724 +  
  24.725 +  freq[256] = 1;		/* make sure 256 has a nonzero count */
  24.726 +  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  24.727 +   * that no real symbol is given code-value of all ones, because 256
  24.728 +   * will be placed last in the largest codeword category.
  24.729 +   */
  24.730 +
  24.731 +  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  24.732 +
  24.733 +  for (;;) {
  24.734 +    /* Find the smallest nonzero frequency, set c1 = its symbol */
  24.735 +    /* In case of ties, take the larger symbol number */
  24.736 +    c1 = -1;
  24.737 +    v = 1000000000L;
  24.738 +    for (i = 0; i <= 256; i++) {
  24.739 +      if (freq[i] && freq[i] <= v) {
  24.740 +	v = freq[i];
  24.741 +	c1 = i;
  24.742 +      }
  24.743 +    }
  24.744 +
  24.745 +    /* Find the next smallest nonzero frequency, set c2 = its symbol */
  24.746 +    /* In case of ties, take the larger symbol number */
  24.747 +    c2 = -1;
  24.748 +    v = 1000000000L;
  24.749 +    for (i = 0; i <= 256; i++) {
  24.750 +      if (freq[i] && freq[i] <= v && i != c1) {
  24.751 +	v = freq[i];
  24.752 +	c2 = i;
  24.753 +      }
  24.754 +    }
  24.755 +
  24.756 +    /* Done if we've merged everything into one frequency */
  24.757 +    if (c2 < 0)
  24.758 +      break;
  24.759 +    
  24.760 +    /* Else merge the two counts/trees */
  24.761 +    freq[c1] += freq[c2];
  24.762 +    freq[c2] = 0;
  24.763 +
  24.764 +    /* Increment the codesize of everything in c1's tree branch */
  24.765 +    codesize[c1]++;
  24.766 +    while (others[c1] >= 0) {
  24.767 +      c1 = others[c1];
  24.768 +      codesize[c1]++;
  24.769 +    }
  24.770 +    
  24.771 +    others[c1] = c2;		/* chain c2 onto c1's tree branch */
  24.772 +    
  24.773 +    /* Increment the codesize of everything in c2's tree branch */
  24.774 +    codesize[c2]++;
  24.775 +    while (others[c2] >= 0) {
  24.776 +      c2 = others[c2];
  24.777 +      codesize[c2]++;
  24.778 +    }
  24.779 +  }
  24.780 +
  24.781 +  /* Now count the number of symbols of each code length */
  24.782 +  for (i = 0; i <= 256; i++) {
  24.783 +    if (codesize[i]) {
  24.784 +      /* The JPEG standard seems to think that this can't happen, */
  24.785 +      /* but I'm paranoid... */
  24.786 +      if (codesize[i] > MAX_CLEN)
  24.787 +	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  24.788 +
  24.789 +      bits[codesize[i]]++;
  24.790 +    }
  24.791 +  }
  24.792 +
  24.793 +  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  24.794 +   * Huffman procedure assigned any such lengths, we must adjust the coding.
  24.795 +   * Here is what the JPEG spec says about how this next bit works:
  24.796 +   * Since symbols are paired for the longest Huffman code, the symbols are
  24.797 +   * removed from this length category two at a time.  The prefix for the pair
  24.798 +   * (which is one bit shorter) is allocated to one of the pair; then,
  24.799 +   * skipping the BITS entry for that prefix length, a code word from the next
  24.800 +   * shortest nonzero BITS entry is converted into a prefix for two code words
  24.801 +   * one bit longer.
  24.802 +   */
  24.803 +  
  24.804 +  for (i = MAX_CLEN; i > 16; i--) {
  24.805 +    while (bits[i] > 0) {
  24.806 +      j = i - 2;		/* find length of new prefix to be used */
  24.807 +      while (bits[j] == 0)
  24.808 +	j--;
  24.809 +      
  24.810 +      bits[i] -= 2;		/* remove two symbols */
  24.811 +      bits[i-1]++;		/* one goes in this length */
  24.812 +      bits[j+1] += 2;		/* two new symbols in this length */
  24.813 +      bits[j]--;		/* symbol of this length is now a prefix */
  24.814 +    }
  24.815 +  }
  24.816 +
  24.817 +  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  24.818 +  while (bits[i] == 0)		/* find largest codelength still in use */
  24.819 +    i--;
  24.820 +  bits[i]--;
  24.821 +  
  24.822 +  /* Return final symbol counts (only for lengths 0..16) */
  24.823 +  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  24.824 +  
  24.825 +  /* Return a list of the symbols sorted by code length */
  24.826 +  /* It's not real clear to me why we don't need to consider the codelength
  24.827 +   * changes made above, but the JPEG spec seems to think this works.
  24.828 +   */
  24.829 +  p = 0;
  24.830 +  for (i = 1; i <= MAX_CLEN; i++) {
  24.831 +    for (j = 0; j <= 255; j++) {
  24.832 +      if (codesize[j] == i) {
  24.833 +	htbl->huffval[p] = (UINT8) j;
  24.834 +	p++;
  24.835 +      }
  24.836 +    }
  24.837 +  }
  24.838 +
  24.839 +  /* Set sent_table FALSE so updated table will be written to JPEG file. */
  24.840 +  htbl->sent_table = FALSE;
  24.841 +}
  24.842 +
  24.843 +
  24.844 +/*
  24.845 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  24.846 + */
  24.847 +
  24.848 +METHODDEF(void)
  24.849 +finish_pass_gather (j_compress_ptr cinfo)
  24.850 +{
  24.851 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  24.852 +  int ci, dctbl, actbl;
  24.853 +  jpeg_component_info * compptr;
  24.854 +  JHUFF_TBL **htblptr;
  24.855 +  boolean did_dc[NUM_HUFF_TBLS];
  24.856 +  boolean did_ac[NUM_HUFF_TBLS];
  24.857 +
  24.858 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  24.859 +   * per table, because it clobbers the input frequency counts!
  24.860 +   */
  24.861 +  MEMZERO(did_dc, SIZEOF(did_dc));
  24.862 +  MEMZERO(did_ac, SIZEOF(did_ac));
  24.863 +
  24.864 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  24.865 +    compptr = cinfo->cur_comp_info[ci];
  24.866 +    dctbl = compptr->dc_tbl_no;
  24.867 +    actbl = compptr->ac_tbl_no;
  24.868 +    if (! did_dc[dctbl]) {
  24.869 +      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
  24.870 +      if (*htblptr == NULL)
  24.871 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  24.872 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  24.873 +      did_dc[dctbl] = TRUE;
  24.874 +    }
  24.875 +    if (! did_ac[actbl]) {
  24.876 +      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
  24.877 +      if (*htblptr == NULL)
  24.878 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  24.879 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  24.880 +      did_ac[actbl] = TRUE;
  24.881 +    }
  24.882 +  }
  24.883 +}
  24.884 +
  24.885 +
  24.886 +#endif /* ENTROPY_OPT_SUPPORTED */
  24.887 +
  24.888 +
  24.889 +/*
  24.890 + * Module initialization routine for Huffman entropy encoding.
  24.891 + */
  24.892 +
  24.893 +GLOBAL(void)
  24.894 +jinit_huff_encoder (j_compress_ptr cinfo)
  24.895 +{
  24.896 +  huff_entropy_ptr entropy;
  24.897 +  int i;
  24.898 +
  24.899 +  entropy = (huff_entropy_ptr)
  24.900 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.901 +				SIZEOF(huff_entropy_encoder));
  24.902 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  24.903 +  entropy->pub.start_pass = start_pass_huff;
  24.904 +
  24.905 +  /* Mark tables unallocated */
  24.906 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  24.907 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  24.908 +#ifdef ENTROPY_OPT_SUPPORTED
  24.909 +    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  24.910 +#endif
  24.911 +  }
  24.912 +}
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/libs/libjpeg/jchuff.h	Sun Jun 07 17:25:49 2015 +0300
    25.3 @@ -0,0 +1,47 @@
    25.4 +/*
    25.5 + * jchuff.h
    25.6 + *
    25.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    25.8 + * This file is part of the Independent JPEG Group's software.
    25.9 + * For conditions of distribution and use, see the accompanying README file.
   25.10 + *
   25.11 + * This file contains declarations for Huffman entropy encoding routines
   25.12 + * that are shared between the sequential encoder (jchuff.c) and the
   25.13 + * progressive encoder (jcphuff.c).  No other modules need to see these.
   25.14 + */
   25.15 +
   25.16 +/* The legal range of a DCT coefficient is
   25.17 + *  -1024 .. +1023  for 8-bit data;
   25.18 + * -16384 .. +16383 for 12-bit data.
   25.19 + * Hence the magnitude should always fit in 10 or 14 bits respectively.
   25.20 + */
   25.21 +
   25.22 +#if BITS_IN_JSAMPLE == 8
   25.23 +#define MAX_COEF_BITS 10
   25.24 +#else
   25.25 +#define MAX_COEF_BITS 14
   25.26 +#endif
   25.27 +
   25.28 +/* Derived data constructed for each Huffman table */
   25.29 +
   25.30 +typedef struct {
   25.31 +  unsigned int ehufco[256];	/* code for each symbol */
   25.32 +  char ehufsi[256];		/* length of code for each symbol */
   25.33 +  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
   25.34 +} c_derived_tbl;
   25.35 +
   25.36 +/* Short forms of external names for systems with brain-damaged linkers. */
   25.37 +
   25.38 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   25.39 +#define jpeg_make_c_derived_tbl	jMkCDerived
   25.40 +#define jpeg_gen_optimal_table	jGenOptTbl
   25.41 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   25.42 +
   25.43 +/* Expand a Huffman table definition into the derived format */
   25.44 +EXTERN(void) jpeg_make_c_derived_tbl
   25.45 +	JPP((j_compress_ptr cinfo, boolean isDC, int tblno,
   25.46 +	     c_derived_tbl ** pdtbl));
   25.47 +
   25.48 +/* Generate an optimal table definition given the specified counts */
   25.49 +EXTERN(void) jpeg_gen_optimal_table
   25.50 +	JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]));
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/libs/libjpeg/jcinit.c	Sun Jun 07 17:25:49 2015 +0300
    26.3 @@ -0,0 +1,72 @@
    26.4 +/*
    26.5 + * jcinit.c
    26.6 + *
    26.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    26.8 + * This file is part of the Independent JPEG Group's software.
    26.9 + * For conditions of distribution and use, see the accompanying README file.
   26.10 + *
   26.11 + * This file contains initialization logic for the JPEG compressor.
   26.12 + * This routine is in charge of selecting the modules to be executed and
   26.13 + * making an initialization call to each one.
   26.14 + *
   26.15 + * Logically, this code belongs in jcmaster.c.  It's split out because
   26.16 + * linking this routine implies linking the entire compression library.
   26.17 + * For a transcoding-only application, we want to be able to use jcmaster.c
   26.18 + * without linking in the whole library.
   26.19 + */
   26.20 +
   26.21 +#define JPEG_INTERNALS
   26.22 +#include "jinclude.h"
   26.23 +#include "jpeglib.h"
   26.24 +
   26.25 +
   26.26 +/*
   26.27 + * Master selection of compression modules.
   26.28 + * This is done once at the start of processing an image.  We determine
   26.29 + * which modules will be used and give them appropriate initialization calls.
   26.30 + */
   26.31 +
   26.32 +GLOBAL(void)
   26.33 +jinit_compress_master (j_compress_ptr cinfo)
   26.34 +{
   26.35 +  /* Initialize master control (includes parameter checking/processing) */
   26.36 +  jinit_c_master_control(cinfo, FALSE /* full compression */);
   26.37 +
   26.38 +  /* Preprocessing */
   26.39 +  if (! cinfo->raw_data_in) {
   26.40 +    jinit_color_converter(cinfo);
   26.41 +    jinit_downsampler(cinfo);
   26.42 +    jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
   26.43 +  }
   26.44 +  /* Forward DCT */
   26.45 +  jinit_forward_dct(cinfo);
   26.46 +  /* Entropy encoding: either Huffman or arithmetic coding. */
   26.47 +  if (cinfo->arith_code) {
   26.48 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   26.49 +  } else {
   26.50 +    if (cinfo->progressive_mode) {
   26.51 +#ifdef C_PROGRESSIVE_SUPPORTED
   26.52 +      jinit_phuff_encoder(cinfo);
   26.53 +#else
   26.54 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   26.55 +#endif
   26.56 +    } else
   26.57 +      jinit_huff_encoder(cinfo);
   26.58 +  }
   26.59 +
   26.60 +  /* Need a full-image coefficient buffer in any multi-pass mode. */
   26.61 +  jinit_c_coef_controller(cinfo,
   26.62 +		(boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
   26.63 +  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
   26.64 +
   26.65 +  jinit_marker_writer(cinfo);
   26.66 +
   26.67 +  /* We can now tell the memory manager to allocate virtual arrays. */
   26.68 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
   26.69 +
   26.70 +  /* Write the datastream header (SOI) immediately.
   26.71 +   * Frame and scan headers are postponed till later.
   26.72 +   * This lets application insert special markers after the SOI.
   26.73 +   */
   26.74 +  (*cinfo->marker->write_file_header) (cinfo);
   26.75 +}
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/libs/libjpeg/jcmainct.c	Sun Jun 07 17:25:49 2015 +0300
    27.3 @@ -0,0 +1,293 @@
    27.4 +/*
    27.5 + * jcmainct.c
    27.6 + *
    27.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    27.8 + * This file is part of the Independent JPEG Group's software.
    27.9 + * For conditions of distribution and use, see the accompanying README file.
   27.10 + *
   27.11 + * This file contains the main buffer controller for compression.
   27.12 + * The main buffer lies between the pre-processor and the JPEG
   27.13 + * compressor proper; it holds downsampled data in the JPEG colorspace.
   27.14 + */
   27.15 +
   27.16 +#define JPEG_INTERNALS
   27.17 +#include "jinclude.h"
   27.18 +#include "jpeglib.h"
   27.19 +
   27.20 +
   27.21 +/* Note: currently, there is no operating mode in which a full-image buffer
   27.22 + * is needed at this step.  If there were, that mode could not be used with
   27.23 + * "raw data" input, since this module is bypassed in that case.  However,
   27.24 + * we've left the code here for possible use in special applications.
   27.25 + */
   27.26 +#undef FULL_MAIN_BUFFER_SUPPORTED
   27.27 +
   27.28 +
   27.29 +/* Private buffer controller object */
   27.30 +
   27.31 +typedef struct {
   27.32 +  struct jpeg_c_main_controller pub; /* public fields */
   27.33 +
   27.34 +  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
   27.35 +  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
   27.36 +  boolean suspended;		/* remember if we suspended output */
   27.37 +  J_BUF_MODE pass_mode;		/* current operating mode */
   27.38 +
   27.39 +  /* If using just a strip buffer, this points to the entire set of buffers
   27.40 +   * (we allocate one for each component).  In the full-image case, this
   27.41 +   * points to the currently accessible strips of the virtual arrays.
   27.42 +   */
   27.43 +  JSAMPARRAY buffer[MAX_COMPONENTS];
   27.44 +
   27.45 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   27.46 +  /* If using full-image storage, this array holds pointers to virtual-array
   27.47 +   * control blocks for each component.  Unused if not full-image storage.
   27.48 +   */
   27.49 +  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
   27.50 +#endif
   27.51 +} my_main_controller;
   27.52 +
   27.53 +typedef my_main_controller * my_main_ptr;
   27.54 +
   27.55 +
   27.56 +/* Forward declarations */
   27.57 +METHODDEF(void) process_data_simple_main
   27.58 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   27.59 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   27.60 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   27.61 +METHODDEF(void) process_data_buffer_main
   27.62 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   27.63 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   27.64 +#endif
   27.65 +
   27.66 +
   27.67 +/*
   27.68 + * Initialize for a processing pass.
   27.69 + */
   27.70 +
   27.71 +METHODDEF(void)
   27.72 +start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   27.73 +{
   27.74 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   27.75 +
   27.76 +  /* Do nothing in raw-data mode. */
   27.77 +  if (cinfo->raw_data_in)
   27.78 +    return;
   27.79 +
   27.80 +  main->cur_iMCU_row = 0;	/* initialize counters */
   27.81 +  main->rowgroup_ctr = 0;
   27.82 +  main->suspended = FALSE;
   27.83 +  main->pass_mode = pass_mode;	/* save mode for use by process_data */
   27.84 +
   27.85 +  switch (pass_mode) {
   27.86 +  case JBUF_PASS_THRU:
   27.87 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   27.88 +    if (main->whole_image[0] != NULL)
   27.89 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   27.90 +#endif
   27.91 +    main->pub.process_data = process_data_simple_main;
   27.92 +    break;
   27.93 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   27.94 +  case JBUF_SAVE_SOURCE:
   27.95 +  case JBUF_CRANK_DEST:
   27.96 +  case JBUF_SAVE_AND_PASS:
   27.97 +    if (main->whole_image[0] == NULL)
   27.98 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   27.99 +    main->pub.process_data = process_data_buffer_main;
  27.100 +    break;
  27.101 +#endif
  27.102 +  default:
  27.103 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  27.104 +    break;
  27.105 +  }
  27.106 +}
  27.107 +
  27.108 +
  27.109 +/*
  27.110 + * Process some data.
  27.111 + * This routine handles the simple pass-through mode,
  27.112 + * where we have only a strip buffer.
  27.113 + */
  27.114 +
  27.115 +METHODDEF(void)
  27.116 +process_data_simple_main (j_compress_ptr cinfo,
  27.117 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  27.118 +			  JDIMENSION in_rows_avail)
  27.119 +{
  27.120 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  27.121 +
  27.122 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  27.123 +    /* Read input data if we haven't filled the main buffer yet */
  27.124 +    if (main->rowgroup_ctr < DCTSIZE)
  27.125 +      (*cinfo->prep->pre_process_data) (cinfo,
  27.126 +					input_buf, in_row_ctr, in_rows_avail,
  27.127 +					main->buffer, &main->rowgroup_ctr,
  27.128 +					(JDIMENSION) DCTSIZE);
  27.129 +
  27.130 +    /* If we don't have a full iMCU row buffered, return to application for
  27.131 +     * more data.  Note that preprocessor will always pad to fill the iMCU row
  27.132 +     * at the bottom of the image.
  27.133 +     */
  27.134 +    if (main->rowgroup_ctr != DCTSIZE)
  27.135 +      return;
  27.136 +
  27.137 +    /* Send the completed row to the compressor */
  27.138 +    if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  27.139 +      /* If compressor did not consume the whole row, then we must need to
  27.140 +       * suspend processing and return to the application.  In this situation
  27.141 +       * we pretend we didn't yet consume the last input row; otherwise, if
  27.142 +       * it happened to be the last row of the image, the application would
  27.143 +       * think we were done.
  27.144 +       */
  27.145 +      if (! main->suspended) {
  27.146 +	(*in_row_ctr)--;
  27.147 +	main->suspended = TRUE;
  27.148 +      }
  27.149 +      return;
  27.150 +    }
  27.151 +    /* We did finish the row.  Undo our little suspension hack if a previous
  27.152 +     * call suspended; then mark the main buffer empty.
  27.153 +     */
  27.154 +    if (main->suspended) {
  27.155 +      (*in_row_ctr)++;
  27.156 +      main->suspended = FALSE;
  27.157 +    }
  27.158 +    main->rowgroup_ctr = 0;
  27.159 +    main->cur_iMCU_row++;
  27.160 +  }
  27.161 +}
  27.162 +
  27.163 +
  27.164 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  27.165 +
  27.166 +/*
  27.167 + * Process some data.
  27.168 + * This routine handles all of the modes that use a full-size buffer.
  27.169 + */
  27.170 +
  27.171 +METHODDEF(void)
  27.172 +process_data_buffer_main (j_compress_ptr cinfo,
  27.173 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  27.174 +			  JDIMENSION in_rows_avail)
  27.175 +{
  27.176 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  27.177 +  int ci;
  27.178 +  jpeg_component_info *compptr;
  27.179 +  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
  27.180 +
  27.181 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  27.182 +    /* Realign the virtual buffers if at the start of an iMCU row. */
  27.183 +    if (main->rowgroup_ctr == 0) {
  27.184 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  27.185 +	   ci++, compptr++) {
  27.186 +	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  27.187 +	  ((j_common_ptr) cinfo, main->whole_image[ci],
  27.188 +	   main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
  27.189 +	   (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
  27.190 +      }
  27.191 +      /* In a read pass, pretend we just read some source data. */
  27.192 +      if (! writing) {
  27.193 +	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
  27.194 +	main->rowgroup_ctr = DCTSIZE;
  27.195 +      }
  27.196 +    }
  27.197 +
  27.198 +    /* If a write pass, read input data until the current iMCU row is full. */
  27.199 +    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  27.200 +    if (writing) {
  27.201 +      (*cinfo->prep->pre_process_data) (cinfo,
  27.202 +					input_buf, in_row_ctr, in_rows_avail,
  27.203 +					main->buffer, &main->rowgroup_ctr,
  27.204 +					(JDIMENSION) DCTSIZE);
  27.205 +      /* Return to application if we need more data to fill the iMCU row. */
  27.206 +      if (main->rowgroup_ctr < DCTSIZE)
  27.207 +	return;
  27.208 +    }
  27.209 +
  27.210 +    /* Emit data, unless this is a sink-only pass. */
  27.211 +    if (main->pass_mode != JBUF_SAVE_SOURCE) {
  27.212 +      if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  27.213 +	/* If compressor did not consume the whole row, then we must need to
  27.214 +	 * suspend processing and return to the application.  In this situation
  27.215 +	 * we pretend we didn't yet consume the last input row; otherwise, if
  27.216 +	 * it happened to be the last row of the image, the application would
  27.217 +	 * think we were done.
  27.218 +	 */
  27.219 +	if (! main->suspended) {
  27.220 +	  (*in_row_ctr)--;
  27.221 +	  main->suspended = TRUE;
  27.222 +	}
  27.223 +	return;
  27.224 +      }
  27.225 +      /* We did finish the row.  Undo our little suspension hack if a previous
  27.226 +       * call suspended; then mark the main buffer empty.
  27.227 +       */
  27.228 +      if (main->suspended) {
  27.229 +	(*in_row_ctr)++;
  27.230 +	main->suspended = FALSE;
  27.231 +      }
  27.232 +    }
  27.233 +
  27.234 +    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
  27.235 +    main->rowgroup_ctr = 0;
  27.236 +    main->cur_iMCU_row++;
  27.237 +  }
  27.238 +}
  27.239 +
  27.240 +#endif /* FULL_MAIN_BUFFER_SUPPORTED */
  27.241 +
  27.242 +
  27.243 +/*
  27.244 + * Initialize main buffer controller.
  27.245 + */
  27.246 +
  27.247 +GLOBAL(void)
  27.248 +jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  27.249 +{
  27.250 +  my_main_ptr main;
  27.251 +  int ci;
  27.252 +  jpeg_component_info *compptr;
  27.253 +
  27.254 +  main = (my_main_ptr)
  27.255 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  27.256 +				SIZEOF(my_main_controller));
  27.257 +  cinfo->main = (struct jpeg_c_main_controller *) main;
  27.258 +  main->pub.start_pass = start_pass_main;
  27.259 +
  27.260 +  /* We don't need to create a buffer in raw-data mode. */
  27.261 +  if (cinfo->raw_data_in)
  27.262 +    return;
  27.263 +
  27.264 +  /* Create the buffer.  It holds downsampled data, so each component
  27.265 +   * may be of a different size.
  27.266 +   */
  27.267 +  if (need_full_buffer) {
  27.268 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  27.269 +    /* Allocate a full-image virtual array for each component */
  27.270 +    /* Note we pad the bottom to a multiple of the iMCU height */
  27.271 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  27.272 +	 ci++, compptr++) {
  27.273 +      main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  27.274 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  27.275 +	 compptr->width_in_blocks * DCTSIZE,
  27.276 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  27.277 +				(long) compptr->v_samp_factor) * DCTSIZE,
  27.278 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  27.279 +    }
  27.280 +#else
  27.281 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  27.282 +#endif
  27.283 +  } else {
  27.284 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  27.285 +    main->whole_image[0] = NULL; /* flag for no virtual arrays */
  27.286 +#endif
  27.287 +    /* Allocate a strip buffer for each component */
  27.288 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  27.289 +	 ci++, compptr++) {
  27.290 +      main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  27.291 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  27.292 +	 compptr->width_in_blocks * DCTSIZE,
  27.293 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  27.294 +    }
  27.295 +  }
  27.296 +}
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/libs/libjpeg/jcmarker.c	Sun Jun 07 17:25:49 2015 +0300
    28.3 @@ -0,0 +1,664 @@
    28.4 +/*
    28.5 + * jcmarker.c
    28.6 + *
    28.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    28.8 + * This file is part of the Independent JPEG Group's software.
    28.9 + * For conditions of distribution and use, see the accompanying README file.
   28.10 + *
   28.11 + * This file contains routines to write JPEG datastream markers.
   28.12 + */
   28.13 +
   28.14 +#define JPEG_INTERNALS
   28.15 +#include "jinclude.h"
   28.16 +#include "jpeglib.h"
   28.17 +
   28.18 +
   28.19 +typedef enum {			/* JPEG marker codes */
   28.20 +  M_SOF0  = 0xc0,
   28.21 +  M_SOF1  = 0xc1,
   28.22 +  M_SOF2  = 0xc2,
   28.23 +  M_SOF3  = 0xc3,
   28.24 +  
   28.25 +  M_SOF5  = 0xc5,
   28.26 +  M_SOF6  = 0xc6,
   28.27 +  M_SOF7  = 0xc7,
   28.28 +  
   28.29 +  M_JPG   = 0xc8,
   28.30 +  M_SOF9  = 0xc9,
   28.31 +  M_SOF10 = 0xca,
   28.32 +  M_SOF11 = 0xcb,
   28.33 +  
   28.34 +  M_SOF13 = 0xcd,
   28.35 +  M_SOF14 = 0xce,
   28.36 +  M_SOF15 = 0xcf,
   28.37 +  
   28.38 +  M_DHT   = 0xc4,
   28.39 +  
   28.40 +  M_DAC   = 0xcc,
   28.41 +  
   28.42 +  M_RST0  = 0xd0,
   28.43 +  M_RST1  = 0xd1,
   28.44 +  M_RST2  = 0xd2,
   28.45 +  M_RST3  = 0xd3,
   28.46 +  M_RST4  = 0xd4,
   28.47 +  M_RST5  = 0xd5,
   28.48 +  M_RST6  = 0xd6,
   28.49 +  M_RST7  = 0xd7,
   28.50 +  
   28.51 +  M_SOI   = 0xd8,
   28.52 +  M_EOI   = 0xd9,
   28.53 +  M_SOS   = 0xda,
   28.54 +  M_DQT   = 0xdb,
   28.55 +  M_DNL   = 0xdc,
   28.56 +  M_DRI   = 0xdd,
   28.57 +  M_DHP   = 0xde,
   28.58 +  M_EXP   = 0xdf,
   28.59 +  
   28.60 +  M_APP0  = 0xe0,
   28.61 +  M_APP1  = 0xe1,
   28.62 +  M_APP2  = 0xe2,
   28.63 +  M_APP3  = 0xe3,
   28.64 +  M_APP4  = 0xe4,
   28.65 +  M_APP5  = 0xe5,
   28.66 +  M_APP6  = 0xe6,
   28.67 +  M_APP7  = 0xe7,
   28.68 +  M_APP8  = 0xe8,
   28.69 +  M_APP9  = 0xe9,
   28.70 +  M_APP10 = 0xea,
   28.71 +  M_APP11 = 0xeb,
   28.72 +  M_APP12 = 0xec,
   28.73 +  M_APP13 = 0xed,
   28.74 +  M_APP14 = 0xee,
   28.75 +  M_APP15 = 0xef,
   28.76 +  
   28.77 +  M_JPG0  = 0xf0,
   28.78 +  M_JPG13 = 0xfd,
   28.79 +  M_COM   = 0xfe,
   28.80 +  
   28.81 +  M_TEM   = 0x01,
   28.82 +  
   28.83 +  M_ERROR = 0x100
   28.84 +} JPEG_MARKER;
   28.85 +
   28.86 +
   28.87 +/* Private state */
   28.88 +
   28.89 +typedef struct {
   28.90 +  struct jpeg_marker_writer pub; /* public fields */
   28.91 +
   28.92 +  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
   28.93 +} my_marker_writer;
   28.94 +
   28.95 +typedef my_marker_writer * my_marker_ptr;
   28.96 +
   28.97 +
   28.98 +/*
   28.99 + * Basic output routines.
  28.100 + *
  28.101 + * Note that we do not support suspension while writing a marker.
  28.102 + * Therefore, an application using suspension must ensure that there is
  28.103 + * enough buffer space for the initial markers (typ. 600-700 bytes) before
  28.104 + * calling jpeg_start_compress, and enough space to write the trailing EOI
  28.105 + * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
  28.106 + * modes are not supported at all with suspension, so those two are the only
  28.107 + * points where markers will be written.
  28.108 + */
  28.109 +
  28.110 +LOCAL(void)
  28.111 +emit_byte (j_compress_ptr cinfo, int val)
  28.112 +/* Emit a byte */
  28.113 +{
  28.114 +  struct jpeg_destination_mgr * dest = cinfo->dest;
  28.115 +
  28.116 +  *(dest->next_output_byte)++ = (JOCTET) val;
  28.117 +  if (--dest->free_in_buffer == 0) {
  28.118 +    if (! (*dest->empty_output_buffer) (cinfo))
  28.119 +      ERREXIT(cinfo, JERR_CANT_SUSPEND);
  28.120 +  }
  28.121 +}
  28.122 +
  28.123 +
  28.124 +LOCAL(void)
  28.125 +emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
  28.126 +/* Emit a marker code */
  28.127 +{
  28.128 +  emit_byte(cinfo, 0xFF);
  28.129 +  emit_byte(cinfo, (int) mark);
  28.130 +}
  28.131 +
  28.132 +
  28.133 +LOCAL(void)
  28.134 +emit_2bytes (j_compress_ptr cinfo, int value)
  28.135 +/* Emit a 2-byte integer; these are always MSB first in JPEG files */
  28.136 +{
  28.137 +  emit_byte(cinfo, (value >> 8) & 0xFF);
  28.138 +  emit_byte(cinfo, value & 0xFF);
  28.139 +}
  28.140 +
  28.141 +
  28.142 +/*
  28.143 + * Routines to write specific marker types.
  28.144 + */
  28.145 +
  28.146 +LOCAL(int)
  28.147 +emit_dqt (j_compress_ptr cinfo, int index)
  28.148 +/* Emit a DQT marker */
  28.149 +/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
  28.150 +{
  28.151 +  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
  28.152 +  int prec;
  28.153 +  int i;
  28.154 +
  28.155 +  if (qtbl == NULL)
  28.156 +    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
  28.157 +
  28.158 +  prec = 0;
  28.159 +  for (i = 0; i < DCTSIZE2; i++) {
  28.160 +    if (qtbl->quantval[i] > 255)
  28.161 +      prec = 1;
  28.162 +  }
  28.163 +
  28.164 +  if (! qtbl->sent_table) {
  28.165 +    emit_marker(cinfo, M_DQT);
  28.166 +
  28.167 +    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
  28.168 +
  28.169 +    emit_byte(cinfo, index + (prec<<4));
  28.170 +
  28.171 +    for (i = 0; i < DCTSIZE2; i++) {
  28.172 +      /* The table entries must be emitted in zigzag order. */
  28.173 +      unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
  28.174 +      if (prec)
  28.175 +	emit_byte(cinfo, (int) (qval >> 8));
  28.176 +      emit_byte(cinfo, (int) (qval & 0xFF));
  28.177 +    }
  28.178 +
  28.179 +    qtbl->sent_table = TRUE;
  28.180 +  }
  28.181 +
  28.182 +  return prec;
  28.183 +}
  28.184 +
  28.185 +
  28.186 +LOCAL(void)
  28.187 +emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
  28.188 +/* Emit a DHT marker */
  28.189 +{
  28.190 +  JHUFF_TBL * htbl;
  28.191 +  int length, i;
  28.192 +  
  28.193 +  if (is_ac) {
  28.194 +    htbl = cinfo->ac_huff_tbl_ptrs[index];
  28.195 +    index += 0x10;		/* output index has AC bit set */
  28.196 +  } else {
  28.197 +    htbl = cinfo->dc_huff_tbl_ptrs[index];
  28.198 +  }
  28.199 +
  28.200 +  if (htbl == NULL)
  28.201 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
  28.202 +  
  28.203 +  if (! htbl->sent_table) {
  28.204 +    emit_marker(cinfo, M_DHT);
  28.205 +    
  28.206 +    length = 0;
  28.207 +    for (i = 1; i <= 16; i++)
  28.208 +      length += htbl->bits[i];
  28.209 +    
  28.210 +    emit_2bytes(cinfo, length + 2 + 1 + 16);
  28.211 +    emit_byte(cinfo, index);
  28.212 +    
  28.213 +    for (i = 1; i <= 16; i++)
  28.214 +      emit_byte(cinfo, htbl->bits[i]);
  28.215 +    
  28.216 +    for (i = 0; i < length; i++)
  28.217 +      emit_byte(cinfo, htbl->huffval[i]);
  28.218 +    
  28.219 +    htbl->sent_table = TRUE;
  28.220 +  }
  28.221 +}
  28.222 +
  28.223 +
  28.224 +LOCAL(void)
  28.225 +emit_dac (j_compress_ptr cinfo)
  28.226 +/* Emit a DAC marker */
  28.227 +/* Since the useful info is so small, we want to emit all the tables in */
  28.228 +/* one DAC marker.  Therefore this routine does its own scan of the table. */
  28.229 +{
  28.230 +#ifdef C_ARITH_CODING_SUPPORTED
  28.231 +  char dc_in_use[NUM_ARITH_TBLS];
  28.232 +  char ac_in_use[NUM_ARITH_TBLS];
  28.233 +  int length, i;
  28.234 +  jpeg_component_info *compptr;
  28.235 +  
  28.236 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  28.237 +    dc_in_use[i] = ac_in_use[i] = 0;
  28.238 +  
  28.239 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  28.240 +    compptr = cinfo->cur_comp_info[i];
  28.241 +    dc_in_use[compptr->dc_tbl_no] = 1;
  28.242 +    ac_in_use[compptr->ac_tbl_no] = 1;
  28.243 +  }
  28.244 +  
  28.245 +  length = 0;
  28.246 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  28.247 +    length += dc_in_use[i] + ac_in_use[i];
  28.248 +  
  28.249 +  emit_marker(cinfo, M_DAC);
  28.250 +  
  28.251 +  emit_2bytes(cinfo, length*2 + 2);
  28.252 +  
  28.253 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  28.254 +    if (dc_in_use[i]) {
  28.255 +      emit_byte(cinfo, i);
  28.256 +      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
  28.257 +    }
  28.258 +    if (ac_in_use[i]) {
  28.259 +      emit_byte(cinfo, i + 0x10);
  28.260 +      emit_byte(cinfo, cinfo->arith_ac_K[i]);
  28.261 +    }
  28.262 +  }
  28.263 +#endif /* C_ARITH_CODING_SUPPORTED */
  28.264 +}
  28.265 +
  28.266 +
  28.267 +LOCAL(void)
  28.268 +emit_dri (j_compress_ptr cinfo)
  28.269 +/* Emit a DRI marker */
  28.270 +{
  28.271 +  emit_marker(cinfo, M_DRI);
  28.272 +  
  28.273 +  emit_2bytes(cinfo, 4);	/* fixed length */
  28.274 +
  28.275 +  emit_2bytes(cinfo, (int) cinfo->restart_interval);
  28.276 +}
  28.277 +
  28.278 +
  28.279 +LOCAL(void)
  28.280 +emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
  28.281 +/* Emit a SOF marker */
  28.282 +{
  28.283 +  int ci;
  28.284 +  jpeg_component_info *compptr;
  28.285 +  
  28.286 +  emit_marker(cinfo, code);
  28.287 +  
  28.288 +  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
  28.289 +
  28.290 +  /* Make sure image isn't bigger than SOF field can handle */
  28.291 +  if ((long) cinfo->image_height > 65535L ||
  28.292 +      (long) cinfo->image_width > 65535L)
  28.293 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
  28.294 +
  28.295 +  emit_byte(cinfo, cinfo->data_precision);
  28.296 +  emit_2bytes(cinfo, (int) cinfo->image_height);
  28.297 +  emit_2bytes(cinfo, (int) cinfo->image_width);
  28.298 +
  28.299 +  emit_byte(cinfo, cinfo->num_components);
  28.300 +
  28.301 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.302 +       ci++, compptr++) {
  28.303 +    emit_byte(cinfo, compptr->component_id);
  28.304 +    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
  28.305 +    emit_byte(cinfo, compptr->quant_tbl_no);
  28.306 +  }
  28.307 +}
  28.308 +
  28.309 +
  28.310 +LOCAL(void)
  28.311 +emit_sos (j_compress_ptr cinfo)
  28.312 +/* Emit a SOS marker */
  28.313 +{
  28.314 +  int i, td, ta;
  28.315 +  jpeg_component_info *compptr;
  28.316 +  
  28.317 +  emit_marker(cinfo, M_SOS);
  28.318 +  
  28.319 +  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
  28.320 +  
  28.321 +  emit_byte(cinfo, cinfo->comps_in_scan);
  28.322 +  
  28.323 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  28.324 +    compptr = cinfo->cur_comp_info[i];
  28.325 +    emit_byte(cinfo, compptr->component_id);
  28.326 +    td = compptr->dc_tbl_no;
  28.327 +    ta = compptr->ac_tbl_no;
  28.328 +    if (cinfo->progressive_mode) {
  28.329 +      /* Progressive mode: only DC or only AC tables are used in one scan;
  28.330 +       * furthermore, Huffman coding of DC refinement uses no table at all.
  28.331 +       * We emit 0 for unused field(s); this is recommended by the P&M text
  28.332 +       * but does not seem to be specified in the standard.
  28.333 +       */
  28.334 +      if (cinfo->Ss == 0) {
  28.335 +	ta = 0;			/* DC scan */
  28.336 +	if (cinfo->Ah != 0 && !cinfo->arith_code)
  28.337 +	  td = 0;		/* no DC table either */
  28.338 +      } else {
  28.339 +	td = 0;			/* AC scan */
  28.340 +      }
  28.341 +    }
  28.342 +    emit_byte(cinfo, (td << 4) + ta);
  28.343 +  }
  28.344 +
  28.345 +  emit_byte(cinfo, cinfo->Ss);
  28.346 +  emit_byte(cinfo, cinfo->Se);
  28.347 +  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
  28.348 +}
  28.349 +
  28.350 +
  28.351 +LOCAL(void)
  28.352 +emit_jfif_app0 (j_compress_ptr cinfo)
  28.353 +/* Emit a JFIF-compliant APP0 marker */
  28.354 +{
  28.355 +  /*
  28.356 +   * Length of APP0 block	(2 bytes)
  28.357 +   * Block ID			(4 bytes - ASCII "JFIF")
  28.358 +   * Zero byte			(1 byte to terminate the ID string)
  28.359 +   * Version Major, Minor	(2 bytes - major first)
  28.360 +   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
  28.361 +   * Xdpu			(2 bytes - dots per unit horizontal)
  28.362 +   * Ydpu			(2 bytes - dots per unit vertical)
  28.363 +   * Thumbnail X size		(1 byte)
  28.364 +   * Thumbnail Y size		(1 byte)
  28.365 +   */
  28.366 +  
  28.367 +  emit_marker(cinfo, M_APP0);
  28.368 +  
  28.369 +  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
  28.370 +
  28.371 +  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
  28.372 +  emit_byte(cinfo, 0x46);
  28.373 +  emit_byte(cinfo, 0x49);
  28.374 +  emit_byte(cinfo, 0x46);
  28.375 +  emit_byte(cinfo, 0);
  28.376 +  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
  28.377 +  emit_byte(cinfo, cinfo->JFIF_minor_version);
  28.378 +  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  28.379 +  emit_2bytes(cinfo, (int) cinfo->X_density);
  28.380 +  emit_2bytes(cinfo, (int) cinfo->Y_density);
  28.381 +  emit_byte(cinfo, 0);		/* No thumbnail image */
  28.382 +  emit_byte(cinfo, 0);
  28.383 +}
  28.384 +
  28.385 +
  28.386 +LOCAL(void)
  28.387 +emit_adobe_app14 (j_compress_ptr cinfo)
  28.388 +/* Emit an Adobe APP14 marker */
  28.389 +{
  28.390 +  /*
  28.391 +   * Length of APP14 block	(2 bytes)
  28.392 +   * Block ID			(5 bytes - ASCII "Adobe")
  28.393 +   * Version Number		(2 bytes - currently 100)
  28.394 +   * Flags0			(2 bytes - currently 0)
  28.395 +   * Flags1			(2 bytes - currently 0)
  28.396 +   * Color transform		(1 byte)
  28.397 +   *
  28.398 +   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
  28.399 +   * now in circulation seem to use Version = 100, so that's what we write.
  28.400 +   *
  28.401 +   * We write the color transform byte as 1 if the JPEG color space is
  28.402 +   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
  28.403 +   * whether the encoder performed a transformation, which is pretty useless.
  28.404 +   */
  28.405 +  
  28.406 +  emit_marker(cinfo, M_APP14);
  28.407 +  
  28.408 +  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
  28.409 +
  28.410 +  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
  28.411 +  emit_byte(cinfo, 0x64);
  28.412 +  emit_byte(cinfo, 0x6F);
  28.413 +  emit_byte(cinfo, 0x62);
  28.414 +  emit_byte(cinfo, 0x65);
  28.415 +  emit_2bytes(cinfo, 100);	/* Version */
  28.416 +  emit_2bytes(cinfo, 0);	/* Flags0 */
  28.417 +  emit_2bytes(cinfo, 0);	/* Flags1 */
  28.418 +  switch (cinfo->jpeg_color_space) {
  28.419 +  case JCS_YCbCr:
  28.420 +    emit_byte(cinfo, 1);	/* Color transform = 1 */
  28.421 +    break;
  28.422 +  case JCS_YCCK:
  28.423 +    emit_byte(cinfo, 2);	/* Color transform = 2 */
  28.424 +    break;
  28.425 +  default:
  28.426 +    emit_byte(cinfo, 0);	/* Color transform = 0 */
  28.427 +    break;
  28.428 +  }
  28.429 +}
  28.430 +
  28.431 +
  28.432 +/*
  28.433 + * These routines allow writing an arbitrary marker with parameters.
  28.434 + * The only intended use is to emit COM or APPn markers after calling
  28.435 + * write_file_header and before calling write_frame_header.
  28.436 + * Other uses are not guaranteed to produce desirable results.
  28.437 + * Counting the parameter bytes properly is the caller's responsibility.
  28.438 + */
  28.439 +
  28.440 +METHODDEF(void)
  28.441 +write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  28.442 +/* Emit an arbitrary marker header */
  28.443 +{
  28.444 +  if (datalen > (unsigned int) 65533)		/* safety check */
  28.445 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  28.446 +
  28.447 +  emit_marker(cinfo, (JPEG_MARKER) marker);
  28.448 +
  28.449 +  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
  28.450 +}
  28.451 +
  28.452 +METHODDEF(void)
  28.453 +write_marker_byte (j_compress_ptr cinfo, int val)
  28.454 +/* Emit one byte of marker parameters following write_marker_header */
  28.455 +{
  28.456 +  emit_byte(cinfo, val);
  28.457 +}
  28.458 +
  28.459 +
  28.460 +/*
  28.461 + * Write datastream header.
  28.462 + * This consists of an SOI and optional APPn markers.
  28.463 + * We recommend use of the JFIF marker, but not the Adobe marker,
  28.464 + * when using YCbCr or grayscale data.  The JFIF marker should NOT
  28.465 + * be used for any other JPEG colorspace.  The Adobe marker is helpful
  28.466 + * to distinguish RGB, CMYK, and YCCK colorspaces.
  28.467 + * Note that an application can write additional header markers after
  28.468 + * jpeg_start_compress returns.
  28.469 + */
  28.470 +
  28.471 +METHODDEF(void)
  28.472 +write_file_header (j_compress_ptr cinfo)
  28.473 +{
  28.474 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  28.475 +
  28.476 +  emit_marker(cinfo, M_SOI);	/* first the SOI */
  28.477 +
  28.478 +  /* SOI is defined to reset restart interval to 0 */
  28.479 +  marker->last_restart_interval = 0;
  28.480 +
  28.481 +  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
  28.482 +    emit_jfif_app0(cinfo);
  28.483 +  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
  28.484 +    emit_adobe_app14(cinfo);
  28.485 +}
  28.486 +
  28.487 +
  28.488 +/*
  28.489 + * Write frame header.
  28.490 + * This consists of DQT and SOFn markers.
  28.491 + * Note that we do not emit the SOF until we have emitted the DQT(s).
  28.492 + * This avoids compatibility problems with incorrect implementations that
  28.493 + * try to error-check the quant table numbers as soon as they see the SOF.
  28.494 + */
  28.495 +
  28.496 +METHODDEF(void)
  28.497 +write_frame_header (j_compress_ptr cinfo)
  28.498 +{
  28.499 +  int ci, prec;
  28.500 +  boolean is_baseline;
  28.501 +  jpeg_component_info *compptr;
  28.502 +  
  28.503 +  /* Emit DQT for each quantization table.
  28.504 +   * Note that emit_dqt() suppresses any duplicate tables.
  28.505 +   */
  28.506 +  prec = 0;
  28.507 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.508 +       ci++, compptr++) {
  28.509 +    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
  28.510 +  }
  28.511 +  /* now prec is nonzero iff there are any 16-bit quant tables. */
  28.512 +
  28.513 +  /* Check for a non-baseline specification.
  28.514 +   * Note we assume that Huffman table numbers won't be changed later.
  28.515 +   */
  28.516 +  if (cinfo->arith_code || cinfo->progressive_mode ||
  28.517 +      cinfo->data_precision != 8) {
  28.518 +    is_baseline = FALSE;
  28.519 +  } else {
  28.520 +    is_baseline = TRUE;
  28.521 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  28.522 +	 ci++, compptr++) {
  28.523 +      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
  28.524 +	is_baseline = FALSE;
  28.525 +    }
  28.526 +    if (prec && is_baseline) {
  28.527 +      is_baseline = FALSE;
  28.528 +      /* If it's baseline except for quantizer size, warn the user */
  28.529 +      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
  28.530 +    }
  28.531 +  }
  28.532 +
  28.533 +  /* Emit the proper SOF marker */
  28.534 +  if (cinfo->arith_code) {
  28.535 +    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
  28.536 +  } else {
  28.537 +    if (cinfo->progressive_mode)
  28.538 +      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
  28.539 +    else if (is_baseline)
  28.540 +      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
  28.541 +    else
  28.542 +      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
  28.543 +  }
  28.544 +}
  28.545 +
  28.546 +
  28.547 +/*
  28.548 + * Write scan header.
  28.549 + * This consists of DHT or DAC markers, optional DRI, and SOS.
  28.550 + * Compressed data will be written following the SOS.
  28.551 + */
  28.552 +
  28.553 +METHODDEF(void)
  28.554 +write_scan_header (j_compress_ptr cinfo)
  28.555 +{
  28.556 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  28.557 +  int i;
  28.558 +  jpeg_component_info *compptr;
  28.559 +
  28.560 +  if (cinfo->arith_code) {
  28.561 +    /* Emit arith conditioning info.  We may have some duplication
  28.562 +     * if the file has multiple scans, but it's so small it's hardly
  28.563 +     * worth worrying about.
  28.564 +     */
  28.565 +    emit_dac(cinfo);
  28.566 +  } else {
  28.567 +    /* Emit Huffman tables.
  28.568 +     * Note that emit_dht() suppresses any duplicate tables.
  28.569 +     */
  28.570 +    for (i = 0; i < cinfo->comps_in_scan; i++) {
  28.571 +      compptr = cinfo->cur_comp_info[i];
  28.572 +      if (cinfo->progressive_mode) {
  28.573 +	/* Progressive mode: only DC or only AC tables are used in one scan */
  28.574 +	if (cinfo->Ss == 0) {
  28.575 +	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
  28.576 +	    emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  28.577 +	} else {
  28.578 +	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  28.579 +	}
  28.580 +      } else {
  28.581 +	/* Sequential mode: need both DC and AC tables */
  28.582 +	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  28.583 +	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  28.584 +      }
  28.585 +    }
  28.586 +  }
  28.587 +
  28.588 +  /* Emit DRI if required --- note that DRI value could change for each scan.
  28.589 +   * We avoid wasting space with unnecessary DRIs, however.
  28.590 +   */
  28.591 +  if (cinfo->restart_interval != marker->last_restart_interval) {
  28.592 +    emit_dri(cinfo);
  28.593 +    marker->last_restart_interval = cinfo->restart_interval;
  28.594 +  }
  28.595 +
  28.596 +  emit_sos(cinfo);
  28.597 +}
  28.598 +
  28.599 +
  28.600 +/*
  28.601 + * Write datastream trailer.
  28.602 + */
  28.603 +
  28.604 +METHODDEF(void)
  28.605 +write_file_trailer (j_compress_ptr cinfo)
  28.606 +{
  28.607 +  emit_marker(cinfo, M_EOI);
  28.608 +}
  28.609 +
  28.610 +
  28.611 +/*
  28.612 + * Write an abbreviated table-specification datastream.
  28.613 + * This consists of SOI, DQT and DHT tables, and EOI.
  28.614 + * Any table that is defined and not marked sent_table = TRUE will be
  28.615 + * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
  28.616 + */
  28.617 +
  28.618 +METHODDEF(void)
  28.619 +write_tables_only (j_compress_ptr cinfo)
  28.620 +{
  28.621 +  int i;
  28.622 +
  28.623 +  emit_marker(cinfo, M_SOI);
  28.624 +
  28.625 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  28.626 +    if (cinfo->quant_tbl_ptrs[i] != NULL)
  28.627 +      (void) emit_dqt(cinfo, i);
  28.628 +  }
  28.629 +
  28.630 +  if (! cinfo->arith_code) {
  28.631 +    for (i = 0; i < NUM_HUFF_TBLS; i++) {
  28.632 +      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
  28.633 +	emit_dht(cinfo, i, FALSE);
  28.634 +      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
  28.635 +	emit_dht(cinfo, i, TRUE);
  28.636 +    }
  28.637 +  }
  28.638 +
  28.639 +  emit_marker(cinfo, M_EOI);
  28.640 +}
  28.641 +
  28.642 +
  28.643 +/*
  28.644 + * Initialize the marker writer module.
  28.645 + */
  28.646 +
  28.647 +GLOBAL(void)
  28.648 +jinit_marker_writer (j_compress_ptr cinfo)
  28.649 +{
  28.650 +  my_marker_ptr marker;
  28.651 +
  28.652 +  /* Create the subobject */
  28.653 +  marker = (my_marker_ptr)
  28.654 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  28.655 +				SIZEOF(my_marker_writer));
  28.656 +  cinfo->marker = (struct jpeg_marker_writer *) marker;
  28.657 +  /* Initialize method pointers */
  28.658 +  marker->pub.write_file_header = write_file_header;
  28.659 +  marker->pub.write_frame_header = write_frame_header;
  28.660 +  marker->pub.write_scan_header = write_scan_header;
  28.661 +  marker->pub.write_file_trailer = write_file_trailer;
  28.662 +  marker->pub.write_tables_only = write_tables_only;
  28.663 +  marker->pub.write_marker_header = write_marker_header;
  28.664 +  marker->pub.write_marker_byte = write_marker_byte;
  28.665 +  /* Initialize private state */
  28.666 +  marker->last_restart_interval = 0;
  28.667 +}
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/libs/libjpeg/jcmaster.c	Sun Jun 07 17:25:49 2015 +0300
    29.3 @@ -0,0 +1,590 @@
    29.4 +/*
    29.5 + * jcmaster.c
    29.6 + *
    29.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    29.8 + * This file is part of the Independent JPEG Group's software.
    29.9 + * For conditions of distribution and use, see the accompanying README file.
   29.10 + *
   29.11 + * This file contains master control logic for the JPEG compressor.
   29.12 + * These routines are concerned with parameter validation, initial setup,
   29.13 + * and inter-pass control (determining the number of passes and the work 
   29.14 + * to be done in each pass).
   29.15 + */
   29.16 +
   29.17 +#define JPEG_INTERNALS
   29.18 +#include "jinclude.h"
   29.19 +#include "jpeglib.h"
   29.20 +
   29.21 +
   29.22 +/* Private state */
   29.23 +
   29.24 +typedef enum {
   29.25 +	main_pass,		/* input data, also do first output step */
   29.26 +	huff_opt_pass,		/* Huffman code optimization pass */
   29.27 +	output_pass		/* data output pass */
   29.28 +} c_pass_type;
   29.29 +
   29.30 +typedef struct {
   29.31 +  struct jpeg_comp_master pub;	/* public fields */
   29.32 +
   29.33 +  c_pass_type pass_type;	/* the type of the current pass */
   29.34 +
   29.35 +  int pass_number;		/* # of passes completed */
   29.36 +  int total_passes;		/* total # of passes needed */
   29.37 +
   29.38 +  int scan_number;		/* current index in scan_info[] */
   29.39 +} my_comp_master;
   29.40 +
   29.41 +typedef my_comp_master * my_master_ptr;
   29.42 +
   29.43 +
   29.44 +/*
   29.45 + * Support routines that do various essential calculations.
   29.46 + */
   29.47 +
   29.48 +LOCAL(void)
   29.49 +initial_setup (j_compress_ptr cinfo)
   29.50 +/* Do computations that are needed before master selection phase */
   29.51 +{
   29.52 +  int ci;
   29.53 +  jpeg_component_info *compptr;
   29.54 +  long samplesperrow;
   29.55 +  JDIMENSION jd_samplesperrow;
   29.56 +
   29.57 +  /* Sanity check on image dimensions */
   29.58 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
   29.59 +      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
   29.60 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
   29.61 +
   29.62 +  /* Make sure image isn't bigger than I can handle */
   29.63 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   29.64 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   29.65 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   29.66 +
   29.67 +  /* Width of an input scanline must be representable as JDIMENSION. */
   29.68 +  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
   29.69 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
   29.70 +  if ((long) jd_samplesperrow != samplesperrow)
   29.71 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   29.72 +
   29.73 +  /* For now, precision must match compiled-in value... */
   29.74 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
   29.75 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
   29.76 +
   29.77 +  /* Check that number of components won't exceed internal array sizes */
   29.78 +  if (cinfo->num_components > MAX_COMPONENTS)
   29.79 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   29.80 +	     MAX_COMPONENTS);
   29.81 +
   29.82 +  /* Compute maximum sampling factors; check factor validity */
   29.83 +  cinfo->max_h_samp_factor = 1;
   29.84 +  cinfo->max_v_samp_factor = 1;
   29.85 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   29.86 +       ci++, compptr++) {
   29.87 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
   29.88 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
   29.89 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
   29.90 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
   29.91 +				   compptr->h_samp_factor);
   29.92 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   29.93 +				   compptr->v_samp_factor);
   29.94 +  }
   29.95 +
   29.96 +  /* Compute dimensions of components */
   29.97 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   29.98 +       ci++, compptr++) {
   29.99 +    /* Fill in the correct component_index value; don't rely on application */
  29.100 +    compptr->component_index = ci;
  29.101 +    /* For compression, we never do DCT scaling. */
  29.102 +    compptr->DCT_scaled_size = DCTSIZE;
  29.103 +    /* Size in DCT blocks */
  29.104 +    compptr->width_in_blocks = (JDIMENSION)
  29.105 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  29.106 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
  29.107 +    compptr->height_in_blocks = (JDIMENSION)
  29.108 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  29.109 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  29.110 +    /* Size in samples */
  29.111 +    compptr->downsampled_width = (JDIMENSION)
  29.112 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  29.113 +		    (long) cinfo->max_h_samp_factor);
  29.114 +    compptr->downsampled_height = (JDIMENSION)
  29.115 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  29.116 +		    (long) cinfo->max_v_samp_factor);
  29.117 +    /* Mark component needed (this flag isn't actually used for compression) */
  29.118 +    compptr->component_needed = TRUE;
  29.119 +  }
  29.120 +
  29.121 +  /* Compute number of fully interleaved MCU rows (number of times that
  29.122 +   * main controller will call coefficient controller).
  29.123 +   */
  29.124 +  cinfo->total_iMCU_rows = (JDIMENSION)
  29.125 +    jdiv_round_up((long) cinfo->image_height,
  29.126 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
  29.127 +}
  29.128 +
  29.129 +
  29.130 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  29.131 +
  29.132 +LOCAL(void)
  29.133 +validate_script (j_compress_ptr cinfo)
  29.134 +/* Verify that the scan script in cinfo->scan_info[] is valid; also
  29.135 + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  29.136 + */
  29.137 +{
  29.138 +  const jpeg_scan_info * scanptr;
  29.139 +  int scanno, ncomps, ci, coefi, thisi;
  29.140 +  int Ss, Se, Ah, Al;
  29.141 +  boolean component_sent[MAX_COMPONENTS];
  29.142 +#ifdef C_PROGRESSIVE_SUPPORTED
  29.143 +  int * last_bitpos_ptr;
  29.144 +  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  29.145 +  /* -1 until that coefficient has been seen; then last Al for it */
  29.146 +#endif
  29.147 +
  29.148 +  if (cinfo->num_scans <= 0)
  29.149 +    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  29.150 +
  29.151 +  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  29.152 +   * for progressive JPEG, no scan can have this.
  29.153 +   */
  29.154 +  scanptr = cinfo->scan_info;
  29.155 +  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  29.156 +#ifdef C_PROGRESSIVE_SUPPORTED
  29.157 +    cinfo->progressive_mode = TRUE;
  29.158 +    last_bitpos_ptr = & last_bitpos[0][0];
  29.159 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  29.160 +      for (coefi = 0; coefi < DCTSIZE2; coefi++)
  29.161 +	*last_bitpos_ptr++ = -1;
  29.162 +#else
  29.163 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  29.164 +#endif
  29.165 +  } else {
  29.166 +    cinfo->progressive_mode = FALSE;
  29.167 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  29.168 +      component_sent[ci] = FALSE;
  29.169 +  }
  29.170 +
  29.171 +  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  29.172 +    /* Validate component indexes */
  29.173 +    ncomps = scanptr->comps_in_scan;
  29.174 +    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  29.175 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  29.176 +    for (ci = 0; ci < ncomps; ci++) {
  29.177 +      thisi = scanptr->component_index[ci];
  29.178 +      if (thisi < 0 || thisi >= cinfo->num_components)
  29.179 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  29.180 +      /* Components must appear in SOF order within each scan */
  29.181 +      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  29.182 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  29.183 +    }
  29.184 +    /* Validate progression parameters */
  29.185 +    Ss = scanptr->Ss;
  29.186 +    Se = scanptr->Se;
  29.187 +    Ah = scanptr->Ah;
  29.188 +    Al = scanptr->Al;
  29.189 +    if (cinfo->progressive_mode) {
  29.190 +#ifdef C_PROGRESSIVE_SUPPORTED
  29.191 +      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  29.192 +       * seems wrong: the upper bound ought to depend on data precision.
  29.193 +       * Perhaps they really meant 0..N+1 for N-bit precision.
  29.194 +       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  29.195 +       * out-of-range reconstructed DC values during the first DC scan,
  29.196 +       * which might cause problems for some decoders.
  29.197 +       */
  29.198 +#if BITS_IN_JSAMPLE == 8
  29.199 +#define MAX_AH_AL 10
  29.200 +#else
  29.201 +#define MAX_AH_AL 13
  29.202 +#endif
  29.203 +      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  29.204 +	  Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
  29.205 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.206 +      if (Ss == 0) {
  29.207 +	if (Se != 0)		/* DC and AC together not OK */
  29.208 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.209 +      } else {
  29.210 +	if (ncomps != 1)	/* AC scans must be for only one component */
  29.211 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.212 +      }
  29.213 +      for (ci = 0; ci < ncomps; ci++) {
  29.214 +	last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  29.215 +	if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  29.216 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.217 +	for (coefi = Ss; coefi <= Se; coefi++) {
  29.218 +	  if (last_bitpos_ptr[coefi] < 0) {
  29.219 +	    /* first scan of this coefficient */
  29.220 +	    if (Ah != 0)
  29.221 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.222 +	  } else {
  29.223 +	    /* not first scan */
  29.224 +	    if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  29.225 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.226 +	  }
  29.227 +	  last_bitpos_ptr[coefi] = Al;
  29.228 +	}
  29.229 +      }
  29.230 +#endif
  29.231 +    } else {
  29.232 +      /* For sequential JPEG, all progression parameters must be these: */
  29.233 +      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  29.234 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  29.235 +      /* Make sure components are not sent twice */
  29.236 +      for (ci = 0; ci < ncomps; ci++) {
  29.237 +	thisi = scanptr->component_index[ci];
  29.238 +	if (component_sent[thisi])
  29.239 +	  ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  29.240 +	component_sent[thisi] = TRUE;
  29.241 +      }
  29.242 +    }
  29.243 +  }
  29.244 +
  29.245 +  /* Now verify that everything got sent. */
  29.246 +  if (cinfo->progressive_mode) {
  29.247 +#ifdef C_PROGRESSIVE_SUPPORTED
  29.248 +    /* For progressive mode, we only check that at least some DC data
  29.249 +     * got sent for each component; the spec does not require that all bits
  29.250 +     * of all coefficients be transmitted.  Would it be wiser to enforce
  29.251 +     * transmission of all coefficient bits??
  29.252 +     */
  29.253 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  29.254 +      if (last_bitpos[ci][0] < 0)
  29.255 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  29.256 +    }
  29.257 +#endif
  29.258 +  } else {
  29.259 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  29.260 +      if (! component_sent[ci])
  29.261 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  29.262 +    }
  29.263 +  }
  29.264 +}
  29.265 +
  29.266 +#endif /* C_MULTISCAN_FILES_SUPPORTED */
  29.267 +
  29.268 +
  29.269 +LOCAL(void)
  29.270 +select_scan_parameters (j_compress_ptr cinfo)
  29.271 +/* Set up the scan parameters for the current scan */
  29.272 +{
  29.273 +  int ci;
  29.274 +
  29.275 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  29.276 +  if (cinfo->scan_info != NULL) {
  29.277 +    /* Prepare for current scan --- the script is already validated */
  29.278 +    my_master_ptr master = (my_master_ptr) cinfo->master;
  29.279 +    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  29.280 +
  29.281 +    cinfo->comps_in_scan = scanptr->comps_in_scan;
  29.282 +    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  29.283 +      cinfo->cur_comp_info[ci] =
  29.284 +	&cinfo->comp_info[scanptr->component_index[ci]];
  29.285 +    }
  29.286 +    cinfo->Ss = scanptr->Ss;
  29.287 +    cinfo->Se = scanptr->Se;
  29.288 +    cinfo->Ah = scanptr->Ah;
  29.289 +    cinfo->Al = scanptr->Al;
  29.290 +  }
  29.291 +  else
  29.292 +#endif
  29.293 +  {
  29.294 +    /* Prepare for single sequential-JPEG scan containing all components */
  29.295 +    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  29.296 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  29.297 +	       MAX_COMPS_IN_SCAN);
  29.298 +    cinfo->comps_in_scan = cinfo->num_components;
  29.299 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  29.300 +      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  29.301 +    }
  29.302 +    cinfo->Ss = 0;
  29.303 +    cinfo->Se = DCTSIZE2-1;
  29.304 +    cinfo->Ah = 0;
  29.305 +    cinfo->Al = 0;
  29.306 +  }
  29.307 +}
  29.308 +
  29.309 +
  29.310 +LOCAL(void)
  29.311 +per_scan_setup (j_compress_ptr cinfo)
  29.312 +/* Do computations that are needed before processing a JPEG scan */
  29.313 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  29.314 +{
  29.315 +  int ci, mcublks, tmp;
  29.316 +  jpeg_component_info *compptr;
  29.317 +  
  29.318 +  if (cinfo->comps_in_scan == 1) {
  29.319 +    
  29.320 +    /* Noninterleaved (single-component) scan */
  29.321 +    compptr = cinfo->cur_comp_info[0];
  29.322 +    
  29.323 +    /* Overall image size in MCUs */
  29.324 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
  29.325 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  29.326 +    
  29.327 +    /* For noninterleaved scan, always one block per MCU */
  29.328 +    compptr->MCU_width = 1;
  29.329 +    compptr->MCU_height = 1;
  29.330 +    compptr->MCU_blocks = 1;
  29.331 +    compptr->MCU_sample_width = DCTSIZE;
  29.332 +    compptr->last_col_width = 1;
  29.333 +    /* For noninterleaved scans, it is convenient to define last_row_height
  29.334 +     * as the number of block rows present in the last iMCU row.
  29.335 +     */
  29.336 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  29.337 +    if (tmp == 0) tmp = compptr->v_samp_factor;
  29.338 +    compptr->last_row_height = tmp;
  29.339 +    
  29.340 +    /* Prepare array describing MCU composition */
  29.341 +    cinfo->blocks_in_MCU = 1;
  29.342 +    cinfo->MCU_membership[0] = 0;
  29.343 +    
  29.344 +  } else {
  29.345 +    
  29.346 +    /* Interleaved (multi-component) scan */
  29.347 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  29.348 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  29.349 +	       MAX_COMPS_IN_SCAN);
  29.350 +    
  29.351 +    /* Overall image size in MCUs */
  29.352 +    cinfo->MCUs_per_row = (JDIMENSION)
  29.353 +      jdiv_round_up((long) cinfo->image_width,
  29.354 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
  29.355 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
  29.356 +      jdiv_round_up((long) cinfo->image_height,
  29.357 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
  29.358 +    
  29.359 +    cinfo->blocks_in_MCU = 0;
  29.360 +    
  29.361 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  29.362 +      compptr = cinfo->cur_comp_info[ci];
  29.363 +      /* Sampling factors give # of blocks of component in each MCU */
  29.364 +      compptr->MCU_width = compptr->h_samp_factor;
  29.365 +      compptr->MCU_height = compptr->v_samp_factor;
  29.366 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  29.367 +      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  29.368 +      /* Figure number of non-dummy blocks in last MCU column & row */
  29.369 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  29.370 +      if (tmp == 0) tmp = compptr->MCU_width;
  29.371 +      compptr->last_col_width = tmp;
  29.372 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  29.373 +      if (tmp == 0) tmp = compptr->MCU_height;
  29.374 +      compptr->last_row_height = tmp;
  29.375 +      /* Prepare array describing MCU composition */
  29.376 +      mcublks = compptr->MCU_blocks;
  29.377 +      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  29.378 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  29.379 +      while (mcublks-- > 0) {
  29.380 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  29.381 +      }
  29.382 +    }
  29.383 +    
  29.384 +  }
  29.385 +
  29.386 +  /* Convert restart specified in rows to actual MCU count. */
  29.387 +  /* Note that count must fit in 16 bits, so we provide limiting. */
  29.388 +  if (cinfo->restart_in_rows > 0) {
  29.389 +    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  29.390 +    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  29.391 +  }
  29.392 +}
  29.393 +
  29.394 +
  29.395 +/*
  29.396 + * Per-pass setup.
  29.397 + * This is called at the beginning of each pass.  We determine which modules
  29.398 + * will be active during this pass and give them appropriate start_pass calls.
  29.399 + * We also set is_last_pass to indicate whether any more passes will be
  29.400 + * required.
  29.401 + */
  29.402 +
  29.403 +METHODDEF(void)
  29.404 +prepare_for_pass (j_compress_ptr cinfo)
  29.405 +{
  29.406 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  29.407 +
  29.408 +  switch (master->pass_type) {
  29.409 +  case main_pass:
  29.410 +    /* Initial pass: will collect input data, and do either Huffman
  29.411 +     * optimization or data output for the first scan.
  29.412 +     */
  29.413 +    select_scan_parameters(cinfo);
  29.414 +    per_scan_setup(cinfo);
  29.415 +    if (! cinfo->raw_data_in) {
  29.416 +      (*cinfo->cconvert->start_pass) (cinfo);
  29.417 +      (*cinfo->downsample->start_pass) (cinfo);
  29.418 +      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  29.419 +    }
  29.420 +    (*cinfo->fdct->start_pass) (cinfo);
  29.421 +    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  29.422 +    (*cinfo->coef->start_pass) (cinfo,
  29.423 +				(master->total_passes > 1 ?
  29.424 +				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  29.425 +    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  29.426 +    if (cinfo->optimize_coding) {
  29.427 +      /* No immediate data output; postpone writing frame/scan headers */
  29.428 +      master->pub.call_pass_startup = FALSE;
  29.429 +    } else {
  29.430 +      /* Will write frame/scan headers at first jpeg_write_scanlines call */
  29.431 +      master->pub.call_pass_startup = TRUE;
  29.432 +    }
  29.433 +    break;
  29.434 +#ifdef ENTROPY_OPT_SUPPORTED
  29.435 +  case huff_opt_pass:
  29.436 +    /* Do Huffman optimization for a scan after the first one. */
  29.437 +    select_scan_parameters(cinfo);
  29.438 +    per_scan_setup(cinfo);
  29.439 +    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  29.440 +      (*cinfo->entropy->start_pass) (cinfo, TRUE);
  29.441 +      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  29.442 +      master->pub.call_pass_startup = FALSE;
  29.443 +      break;
  29.444 +    }
  29.445 +    /* Special case: Huffman DC refinement scans need no Huffman table
  29.446 +     * and therefore we can skip the optimization pass for them.
  29.447 +     */
  29.448 +    master->pass_type = output_pass;
  29.449 +    master->pass_number++;
  29.450 +    /*FALLTHROUGH*/
  29.451 +#endif
  29.452 +  case output_pass:
  29.453 +    /* Do a data-output pass. */
  29.454 +    /* We need not repeat per-scan setup if prior optimization pass did it. */
  29.455 +    if (! cinfo->optimize_coding) {
  29.456 +      select_scan_parameters(cinfo);
  29.457 +      per_scan_setup(cinfo);
  29.458 +    }
  29.459 +    (*cinfo->entropy->start_pass) (cinfo, FALSE);
  29.460 +    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  29.461 +    /* We emit frame/scan headers now */
  29.462 +    if (master->scan_number == 0)
  29.463 +      (*cinfo->marker->write_frame_header) (cinfo);
  29.464 +    (*cinfo->marker->write_scan_header) (cinfo);
  29.465 +    master->pub.call_pass_startup = FALSE;
  29.466 +    break;
  29.467 +  default:
  29.468 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  29.469 +  }
  29.470 +
  29.471 +  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  29.472 +
  29.473 +  /* Set up progress monitor's pass info if present */
  29.474 +  if (cinfo->progress != NULL) {
  29.475 +    cinfo->progress->completed_passes = master->pass_number;
  29.476 +    cinfo->progress->total_passes = master->total_passes;
  29.477 +  }
  29.478 +}
  29.479 +
  29.480 +
  29.481 +/*
  29.482 + * Special start-of-pass hook.
  29.483 + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  29.484 + * In single-pass processing, we need this hook because we don't want to
  29.485 + * write frame/scan headers during jpeg_start_compress; we want to let the
  29.486 + * application write COM markers etc. between jpeg_start_compress and the
  29.487 + * jpeg_write_scanlines loop.
  29.488 + * In multi-pass processing, this routine is not used.
  29.489 + */
  29.490 +
  29.491 +METHODDEF(void)
  29.492 +pass_startup (j_compress_ptr cinfo)
  29.493 +{
  29.494 +  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  29.495 +
  29.496 +  (*cinfo->marker->write_frame_header) (cinfo);
  29.497 +  (*cinfo->marker->write_scan_header) (cinfo);
  29.498 +}
  29.499 +
  29.500 +
  29.501 +/*
  29.502 + * Finish up at end of pass.
  29.503 + */
  29.504 +
  29.505 +METHODDEF(void)
  29.506 +finish_pass_master (j_compress_ptr cinfo)
  29.507 +{
  29.508 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  29.509 +
  29.510 +  /* The entropy coder always needs an end-of-pass call,
  29.511 +   * either to analyze statistics or to flush its output buffer.
  29.512 +   */
  29.513 +  (*cinfo->entropy->finish_pass) (cinfo);
  29.514 +
  29.515 +  /* Update state for next pass */
  29.516 +  switch (master->pass_type) {
  29.517 +  case main_pass:
  29.518 +    /* next pass is either output of scan 0 (after optimization)
  29.519 +     * or output of scan 1 (if no optimization).
  29.520 +     */
  29.521 +    master->pass_type = output_pass;
  29.522 +    if (! cinfo->optimize_coding)
  29.523 +      master->scan_number++;
  29.524 +    break;
  29.525 +  case huff_opt_pass:
  29.526 +    /* next pass is always output of current scan */
  29.527 +    master->pass_type = output_pass;
  29.528 +    break;
  29.529 +  case output_pass:
  29.530 +    /* next pass is either optimization or output of next scan */
  29.531 +    if (cinfo->optimize_coding)
  29.532 +      master->pass_type = huff_opt_pass;
  29.533 +    master->scan_number++;
  29.534 +    break;
  29.535 +  }
  29.536 +
  29.537 +  master->pass_number++;
  29.538 +}
  29.539 +
  29.540 +
  29.541 +/*
  29.542 + * Initialize master compression control.
  29.543 + */
  29.544 +
  29.545 +GLOBAL(void)
  29.546 +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  29.547 +{
  29.548 +  my_master_ptr master;
  29.549 +
  29.550 +  master = (my_master_ptr)
  29.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  29.552 +				  SIZEOF(my_comp_master));
  29.553 +  cinfo->master = (struct jpeg_comp_master *) master;
  29.554 +  master->pub.prepare_for_pass = prepare_for_pass;
  29.555 +  master->pub.pass_startup = pass_startup;
  29.556 +  master->pub.finish_pass = finish_pass_master;
  29.557 +  master->pub.is_last_pass = FALSE;
  29.558 +
  29.559 +  /* Validate parameters, determine derived values */
  29.560 +  initial_setup(cinfo);
  29.561 +
  29.562 +  if (cinfo->scan_info != NULL) {
  29.563 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  29.564 +    validate_script(cinfo);
  29.565 +#else
  29.566 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  29.567 +#endif
  29.568 +  } else {
  29.569 +    cinfo->progressive_mode = FALSE;
  29.570 +    cinfo->num_scans = 1;
  29.571 +  }
  29.572 +
  29.573 +  if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
  29.574 +    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  29.575 +
  29.576 +  /* Initialize my private state */
  29.577 +  if (transcode_only) {
  29.578 +    /* no main pass in transcoding */
  29.579 +    if (cinfo->optimize_coding)
  29.580 +      master->pass_type = huff_opt_pass;
  29.581 +    else
  29.582 +      master->pass_type = output_pass;
  29.583 +  } else {
  29.584 +    /* for normal compression, first pass is always this type: */
  29.585 +    master->pass_type = main_pass;
  29.586 +  }
  29.587 +  master->scan_number = 0;
  29.588 +  master->pass_number = 0;
  29.589 +  if (cinfo->optimize_coding)
  29.590 +    master->total_passes = cinfo->num_scans * 2;
  29.591 +  else
  29.592 +    master->total_passes = cinfo->num_scans;
  29.593 +}
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/libs/libjpeg/jcomapi.c	Sun Jun 07 17:25:49 2015 +0300
    30.3 @@ -0,0 +1,106 @@
    30.4 +/*
    30.5 + * jcomapi.c
    30.6 + *
    30.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    30.8 + * This file is part of the Independent JPEG Group's software.
    30.9 + * For conditions of distribution and use, see the accompanying README file.
   30.10 + *
   30.11 + * This file contains application interface routines that are used for both
   30.12 + * compression and decompression.
   30.13 + */
   30.14 +
   30.15 +#define JPEG_INTERNALS
   30.16 +#include "jinclude.h"
   30.17 +#include "jpeglib.h"
   30.18 +
   30.19 +
   30.20 +/*
   30.21 + * Abort processing of a JPEG compression or decompression operation,
   30.22 + * but don't destroy the object itself.
   30.23 + *
   30.24 + * For this, we merely clean up all the nonpermanent memory pools.
   30.25 + * Note that temp files (virtual arrays) are not allowed to belong to
   30.26 + * the permanent pool, so we will be able to close all temp files here.
   30.27 + * Closing a data source or destination, if necessary, is the application's
   30.28 + * responsibility.
   30.29 + */
   30.30 +
   30.31 +GLOBAL(void)
   30.32 +jpeg_abort (j_common_ptr cinfo)
   30.33 +{
   30.34 +  int pool;
   30.35 +
   30.36 +  /* Do nothing if called on a not-initialized or destroyed JPEG object. */
   30.37 +  if (cinfo->mem == NULL)
   30.38 +    return;
   30.39 +
   30.40 +  /* Releasing pools in reverse order might help avoid fragmentation
   30.41 +   * with some (brain-damaged) malloc libraries.
   30.42 +   */
   30.43 +  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
   30.44 +    (*cinfo->mem->free_pool) (cinfo, pool);
   30.45 +  }
   30.46 +
   30.47 +  /* Reset overall state for possible reuse of object */
   30.48 +  if (cinfo->is_decompressor) {
   30.49 +    cinfo->global_state = DSTATE_START;
   30.50 +    /* Try to keep application from accessing now-deleted marker list.
   30.51 +     * A bit kludgy to do it here, but this is the most central place.
   30.52 +     */
   30.53 +    ((j_decompress_ptr) cinfo)->marker_list = NULL;
   30.54 +  } else {
   30.55 +    cinfo->global_state = CSTATE_START;
   30.56 +  }
   30.57 +}
   30.58 +
   30.59 +
   30.60 +/*
   30.61 + * Destruction of a JPEG object.
   30.62 + *
   30.63 + * Everything gets deallocated except the master jpeg_compress_struct itself
   30.64 + * and the error manager struct.  Both of these are supplied by the application
   30.65 + * and must be freed, if necessary, by the application.  (Often they are on
   30.66 + * the stack and so don't need to be freed anyway.)
   30.67 + * Closing a data source or destination, if necessary, is the application's
   30.68 + * responsibility.
   30.69 + */
   30.70 +
   30.71 +GLOBAL(void)
   30.72 +jpeg_destroy (j_common_ptr cinfo)
   30.73 +{
   30.74 +  /* We need only tell the memory manager to release everything. */
   30.75 +  /* NB: mem pointer is NULL if memory mgr failed to initialize. */
   30.76 +  if (cinfo->mem != NULL)
   30.77 +    (*cinfo->mem->self_destruct) (cinfo);
   30.78 +  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
   30.79 +  cinfo->global_state = 0;	/* mark it destroyed */
   30.80 +}
   30.81 +
   30.82 +
   30.83 +/*
   30.84 + * Convenience routines for allocating quantization and Huffman tables.
   30.85 + * (Would jutils.c be a more reasonable place to put these?)
   30.86 + */
   30.87 +
   30.88 +GLOBAL(JQUANT_TBL *)
   30.89 +jpeg_alloc_quant_table (j_common_ptr cinfo)
   30.90 +{
   30.91 +  JQUANT_TBL *tbl;
   30.92 +
   30.93 +  tbl = (JQUANT_TBL *)
   30.94 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
   30.95 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
   30.96 +  return tbl;
   30.97 +}
   30.98 +
   30.99 +
  30.100 +GLOBAL(JHUFF_TBL *)
  30.101 +jpeg_alloc_huff_table (j_common_ptr cinfo)
  30.102 +{
  30.103 +  JHUFF_TBL *tbl;
  30.104 +
  30.105 +  tbl = (JHUFF_TBL *)
  30.106 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
  30.107 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
  30.108 +  return tbl;
  30.109 +}
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/libs/libjpeg/jconfig.h	Sun Jun 07 17:25:49 2015 +0300
    31.3 @@ -0,0 +1,45 @@
    31.4 +/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
    31.5 +/* see jconfig.doc for explanations */
    31.6 +
    31.7 +#define HAVE_PROTOTYPES
    31.8 +#define HAVE_UNSIGNED_CHAR
    31.9 +#define HAVE_UNSIGNED_SHORT
   31.10 +/* #define void char */
   31.11 +/* #define const */
   31.12 +#undef CHAR_IS_UNSIGNED
   31.13 +#define HAVE_STDDEF_H
   31.14 +#define HAVE_STDLIB_H
   31.15 +#undef NEED_BSD_STRINGS
   31.16 +#undef NEED_SYS_TYPES_H
   31.17 +#undef NEED_FAR_POINTERS	/* we presume a 32-bit flat memory model */
   31.18 +#undef NEED_SHORT_EXTERNAL_NAMES
   31.19 +#undef INCOMPLETE_TYPES_BROKEN
   31.20 +
   31.21 +/* Define "boolean" as unsigned char, not int, per Windows custom */
   31.22 +#ifndef __RPCNDR_H__		/* don't conflict if rpcndr.h already read */
   31.23 +typedef unsigned char boolean;
   31.24 +#endif
   31.25 +#define HAVE_BOOLEAN		/* prevent jmorecfg.h from redefining it */
   31.26 +
   31.27 +
   31.28 +#ifdef JPEG_INTERNALS
   31.29 +
   31.30 +#undef RIGHT_SHIFT_IS_UNSIGNED
   31.31 +
   31.32 +#endif /* JPEG_INTERNALS */
   31.33 +
   31.34 +#ifdef JPEG_CJPEG_DJPEG
   31.35 +
   31.36 +#define BMP_SUPPORTED		/* BMP image file format */
   31.37 +#define GIF_SUPPORTED		/* GIF image file format */
   31.38 +#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
   31.39 +#undef RLE_SUPPORTED		/* Utah RLE image file format */
   31.40 +#define TARGA_SUPPORTED		/* Targa image file format */
   31.41 +
   31.42 +#define TWO_FILE_COMMANDLINE	/* optional */
   31.43 +#define USE_SETMODE		/* Microsoft has setmode() */
   31.44 +#undef NEED_SIGNAL_CATCHER
   31.45 +#undef DONT_USE_B_MODE
   31.46 +#undef PROGRESS_REPORT		/* optional */
   31.47 +
   31.48 +#endif /* JPEG_CJPEG_DJPEG */
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/libs/libjpeg/jcparam.c	Sun Jun 07 17:25:49 2015 +0300
    32.3 @@ -0,0 +1,610 @@
    32.4 +/*
    32.5 + * jcparam.c
    32.6 + *
    32.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    32.8 + * This file is part of the Independent JPEG Group's software.
    32.9 + * For conditions of distribution and use, see the accompanying README file.
   32.10 + *
   32.11 + * This file contains optional default-setting code for the JPEG compressor.
   32.12 + * Applications do not have to use this file, but those that don't use it
   32.13 + * must know a lot more about the innards of the JPEG code.
   32.14 + */
   32.15 +
   32.16 +#define JPEG_INTERNALS
   32.17 +#include "jinclude.h"
   32.18 +#include "jpeglib.h"
   32.19 +
   32.20 +
   32.21 +/*
   32.22 + * Quantization table setup routines
   32.23 + */
   32.24 +
   32.25 +GLOBAL(void)
   32.26 +jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
   32.27 +		      const unsigned int *basic_table,
   32.28 +		      int scale_factor, boolean force_baseline)
   32.29 +/* Define a quantization table equal to the basic_table times
   32.30 + * a scale factor (given as a percentage).
   32.31 + * If force_baseline is TRUE, the computed quantization table entries
   32.32 + * are limited to 1..255 for JPEG baseline compatibility.
   32.33 + */
   32.34 +{
   32.35 +  JQUANT_TBL ** qtblptr;
   32.36 +  int i;
   32.37 +  long temp;
   32.38 +
   32.39 +  /* Safety check to ensure start_compress not called yet. */
   32.40 +  if (cinfo->global_state != CSTATE_START)
   32.41 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   32.42 +
   32.43 +  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
   32.44 +    ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
   32.45 +
   32.46 +  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
   32.47 +
   32.48 +  if (*qtblptr == NULL)
   32.49 +    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   32.50 +
   32.51 +  for (i = 0; i < DCTSIZE2; i++) {
   32.52 +    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
   32.53 +    /* limit the values to the valid range */
   32.54 +    if (temp <= 0L) temp = 1L;
   32.55 +    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
   32.56 +    if (force_baseline && temp > 255L)
   32.57 +      temp = 255L;		/* limit to baseline range if requested */
   32.58 +    (*qtblptr)->quantval[i] = (UINT16) temp;
   32.59 +  }
   32.60 +
   32.61 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
   32.62 +  (*qtblptr)->sent_table = FALSE;
   32.63 +}
   32.64 +
   32.65 +
   32.66 +GLOBAL(void)
   32.67 +jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
   32.68 +			 boolean force_baseline)
   32.69 +/* Set or change the 'quality' (quantization) setting, using default tables
   32.70 + * and a straight percentage-scaling quality scale.  In most cases it's better
   32.71 + * to use jpeg_set_quality (below); this entry point is provided for
   32.72 + * applications that insist on a linear percentage scaling.
   32.73 + */
   32.74 +{
   32.75 +  /* These are the sample quantization tables given in JPEG spec section K.1.
   32.76 +   * The spec says that the values given produce "good" quality, and
   32.77 +   * when divided by 2, "very good" quality.
   32.78 +   */
   32.79 +  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
   32.80 +    16,  11,  10,  16,  24,  40,  51,  61,
   32.81 +    12,  12,  14,  19,  26,  58,  60,  55,
   32.82 +    14,  13,  16,  24,  40,  57,  69,  56,
   32.83 +    14,  17,  22,  29,  51,  87,  80,  62,
   32.84 +    18,  22,  37,  56,  68, 109, 103,  77,
   32.85 +    24,  35,  55,  64,  81, 104, 113,  92,
   32.86 +    49,  64,  78,  87, 103, 121, 120, 101,
   32.87 +    72,  92,  95,  98, 112, 100, 103,  99
   32.88 +  };
   32.89 +  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
   32.90 +    17,  18,  24,  47,  99,  99,  99,  99,
   32.91 +    18,  21,  26,  66,  99,  99,  99,  99,
   32.92 +    24,  26,  56,  99,  99,  99,  99,  99,
   32.93 +    47,  66,  99,  99,  99,  99,  99,  99,
   32.94 +    99,  99,  99,  99,  99,  99,  99,  99,
   32.95 +    99,  99,  99,  99,  99,  99,  99,  99,
   32.96 +    99,  99,  99,  99,  99,  99,  99,  99,
   32.97 +    99,  99,  99,  99,  99,  99,  99,  99
   32.98 +  };
   32.99 +
  32.100 +  /* Set up two quantization tables using the specified scaling */
  32.101 +  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  32.102 +		       scale_factor, force_baseline);
  32.103 +  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  32.104 +		       scale_factor, force_baseline);
  32.105 +}
  32.106 +
  32.107 +
  32.108 +GLOBAL(int)
  32.109 +jpeg_quality_scaling (int quality)
  32.110 +/* Convert a user-specified quality rating to a percentage scaling factor
  32.111 + * for an underlying quantization table, using our recommended scaling curve.
  32.112 + * The input 'quality' factor should be 0 (terrible) to 100 (very good).
  32.113 + */
  32.114 +{
  32.115 +  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
  32.116 +  if (quality <= 0) quality = 1;
  32.117 +  if (quality > 100) quality = 100;
  32.118 +
  32.119 +  /* The basic table is used as-is (scaling 100) for a quality of 50.
  32.120 +   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  32.121 +   * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  32.122 +   * to make all the table entries 1 (hence, minimum quantization loss).
  32.123 +   * Qualities 1..50 are converted to scaling percentage 5000/Q.
  32.124 +   */
  32.125 +  if (quality < 50)
  32.126 +    quality = 5000 / quality;
  32.127 +  else
  32.128 +    quality = 200 - quality*2;
  32.129 +
  32.130 +  return quality;
  32.131 +}
  32.132 +
  32.133 +
  32.134 +GLOBAL(void)
  32.135 +jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
  32.136 +/* Set or change the 'quality' (quantization) setting, using default tables.
  32.137 + * This is the standard quality-adjusting entry point for typical user
  32.138 + * interfaces; only those who want detailed control over quantization tables
  32.139 + * would use the preceding three routines directly.
  32.140 + */
  32.141 +{
  32.142 +  /* Convert user 0-100 rating to percentage scaling */
  32.143 +  quality = jpeg_quality_scaling(quality);
  32.144 +
  32.145 +  /* Set up standard quality tables */
  32.146 +  jpeg_set_linear_quality(cinfo, quality, force_baseline);
  32.147 +}
  32.148 +
  32.149 +
  32.150 +/*
  32.151 + * Huffman table setup routines
  32.152 + */
  32.153 +
  32.154 +LOCAL(void)
  32.155 +add_huff_table (j_compress_ptr cinfo,
  32.156 +		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
  32.157 +/* Define a Huffman table */
  32.158 +{
  32.159 +  int nsymbols, len;
  32.160 +
  32.161 +  if (*htblptr == NULL)
  32.162 +    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  32.163 +
  32.164 +  /* Copy the number-of-symbols-of-each-code-length counts */
  32.165 +  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  32.166 +
  32.167 +  /* Validate the counts.  We do this here mainly so we can copy the right
  32.168 +   * number of symbols from the val[] array, without risking marching off
  32.169 +   * the end of memory.  jchuff.c will do a more thorough test later.
  32.170 +   */
  32.171 +  nsymbols = 0;
  32.172 +  for (len = 1; len <= 16; len++)
  32.173 +    nsymbols += bits[len];
  32.174 +  if (nsymbols < 1 || nsymbols > 256)
  32.175 +    ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  32.176 +
  32.177 +  MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
  32.178 +
  32.179 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
  32.180 +  (*htblptr)->sent_table = FALSE;
  32.181 +}
  32.182 +
  32.183 +
  32.184 +LOCAL(void)
  32.185 +std_huff_tables (j_compress_ptr cinfo)
  32.186 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  32.187 +/* IMPORTANT: these are only valid for 8-bit data precision! */
  32.188 +{
  32.189 +  static const UINT8 bits_dc_luminance[17] =
  32.190 +    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  32.191 +  static const UINT8 val_dc_luminance[] =
  32.192 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  32.193 +  
  32.194 +  static const UINT8 bits_dc_chrominance[17] =
  32.195 +    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  32.196 +  static const UINT8 val_dc_chrominance[] =
  32.197 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  32.198 +  
  32.199 +  static const UINT8 bits_ac_luminance[17] =
  32.200 +    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  32.201 +  static const UINT8 val_ac_luminance[] =
  32.202 +    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  32.203 +      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  32.204 +      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  32.205 +      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  32.206 +      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  32.207 +      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  32.208 +      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  32.209 +      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  32.210 +      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  32.211 +      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  32.212 +      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  32.213 +      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  32.214 +      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  32.215 +      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  32.216 +      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  32.217 +      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  32.218 +      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  32.219 +      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  32.220 +      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  32.221 +      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  32.222 +      0xf9, 0xfa };
  32.223 +  
  32.224 +  static const UINT8 bits_ac_chrominance[17] =
  32.225 +    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  32.226 +  static const UINT8 val_ac_chrominance[] =
  32.227 +    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  32.228 +      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  32.229 +      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  32.230 +      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  32.231 +      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  32.232 +      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  32.233 +      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  32.234 +      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  32.235 +      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  32.236 +      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  32.237 +      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  32.238 +      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  32.239 +      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  32.240 +      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  32.241 +      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  32.242 +      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  32.243 +      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  32.244 +      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  32.245 +      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  32.246 +      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  32.247 +      0xf9, 0xfa };
  32.248 +  
  32.249 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
  32.250 +		 bits_dc_luminance, val_dc_luminance);
  32.251 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
  32.252 +		 bits_ac_luminance, val_ac_luminance);
  32.253 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
  32.254 +		 bits_dc_chrominance, val_dc_chrominance);
  32.255 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
  32.256 +		 bits_ac_chrominance, val_ac_chrominance);
  32.257 +}
  32.258 +
  32.259 +
  32.260 +/*
  32.261 + * Default parameter setup for compression.
  32.262 + *
  32.263 + * Applications that don't choose to use this routine must do their
  32.264 + * own setup of all these parameters.  Alternately, you can call this
  32.265 + * to establish defaults and then alter parameters selectively.  This
  32.266 + * is the recommended approach since, if we add any new parameters,
  32.267 + * your code will still work (they'll be set to reasonable defaults).
  32.268 + */
  32.269 +
  32.270 +GLOBAL(void)
  32.271 +jpeg_set_defaults (j_compress_ptr cinfo)
  32.272 +{
  32.273 +  int i;
  32.274 +
  32.275 +  /* Safety check to ensure start_compress not called yet. */
  32.276 +  if (cinfo->global_state != CSTATE_START)
  32.277 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  32.278 +
  32.279 +  /* Allocate comp_info array large enough for maximum component count.
  32.280 +   * Array is made permanent in case application wants to compress
  32.281 +   * multiple images at same param settings.
  32.282 +   */
  32.283 +  if (cinfo->comp_info == NULL)
  32.284 +    cinfo->comp_info = (jpeg_component_info *)
  32.285 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  32.286 +				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));
  32.287 +
  32.288 +  /* Initialize everything not dependent on the color space */
  32.289 +
  32.290 +  cinfo->data_precision = BITS_IN_JSAMPLE;
  32.291 +  /* Set up two quantization tables using default quality of 75 */
  32.292 +  jpeg_set_quality(cinfo, 75, TRUE);
  32.293 +  /* Set up two Huffman tables */
  32.294 +  std_huff_tables(cinfo);
  32.295 +
  32.296 +  /* Initialize default arithmetic coding conditioning */
  32.297 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  32.298 +    cinfo->arith_dc_L[i] = 0;
  32.299 +    cinfo->arith_dc_U[i] = 1;
  32.300 +    cinfo->arith_ac_K[i] = 5;
  32.301 +  }
  32.302 +
  32.303 +  /* Default is no multiple-scan output */
  32.304 +  cinfo->scan_info = NULL;
  32.305 +  cinfo->num_scans = 0;
  32.306 +
  32.307 +  /* Expect normal source image, not raw downsampled data */
  32.308 +  cinfo->raw_data_in = FALSE;
  32.309 +
  32.310 +  /* Use Huffman coding, not arithmetic coding, by default */
  32.311 +  cinfo->arith_code = FALSE;
  32.312 +
  32.313 +  /* By default, don't do extra passes to optimize entropy coding */
  32.314 +  cinfo->optimize_coding = FALSE;
  32.315 +  /* The standard Huffman tables are only valid for 8-bit data precision.
  32.316 +   * If the precision is higher, force optimization on so that usable
  32.317 +   * tables will be computed.  This test can be removed if default tables
  32.318 +   * are supplied that are valid for the desired precision.
  32.319 +   */
  32.320 +  if (cinfo->data_precision > 8)
  32.321 +    cinfo->optimize_coding = TRUE;
  32.322 +
  32.323 +  /* By default, use the simpler non-cosited sampling alignment */
  32.324 +  cinfo->CCIR601_sampling = FALSE;
  32.325 +
  32.326 +  /* No input smoothing */
  32.327 +  cinfo->smoothing_factor = 0;
  32.328 +
  32.329 +  /* DCT algorithm preference */
  32.330 +  cinfo->dct_method = JDCT_DEFAULT;
  32.331 +
  32.332 +  /* No restart markers */
  32.333 +  cinfo->restart_interval = 0;
  32.334 +  cinfo->restart_in_rows = 0;
  32.335 +
  32.336 +  /* Fill in default JFIF marker parameters.  Note that whether the marker
  32.337 +   * will actually be written is determined by jpeg_set_colorspace.
  32.338 +   *
  32.339 +   * By default, the library emits JFIF version code 1.01.
  32.340 +   * An application that wants to emit JFIF 1.02 extension markers should set
  32.341 +   * JFIF_minor_version to 2.  We could probably get away with just defaulting
  32.342 +   * to 1.02, but there may still be some decoders in use that will complain
  32.343 +   * about that; saying 1.01 should minimize compatibility problems.
  32.344 +   */
  32.345 +  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  32.346 +  cinfo->JFIF_minor_version = 1;
  32.347 +  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  32.348 +  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  32.349 +  cinfo->Y_density = 1;
  32.350 +
  32.351 +  /* Choose JPEG colorspace based on input space, set defaults accordingly */
  32.352 +
  32.353 +  jpeg_default_colorspace(cinfo);
  32.354 +}
  32.355 +
  32.356 +
  32.357 +/*
  32.358 + * Select an appropriate JPEG colorspace for in_color_space.
  32.359 + */
  32.360 +
  32.361 +GLOBAL(void)
  32.362 +jpeg_default_colorspace (j_compress_ptr cinfo)
  32.363 +{
  32.364 +  switch (cinfo->in_color_space) {
  32.365 +  case JCS_GRAYSCALE:
  32.366 +    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  32.367 +    break;
  32.368 +  case JCS_RGB:
  32.369 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  32.370 +    break;
  32.371 +  case JCS_YCbCr:
  32.372 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  32.373 +    break;
  32.374 +  case JCS_CMYK:
  32.375 +    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
  32.376 +    break;
  32.377 +  case JCS_YCCK:
  32.378 +    jpeg_set_colorspace(cinfo, JCS_YCCK);
  32.379 +    break;
  32.380 +  case JCS_UNKNOWN:
  32.381 +    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  32.382 +    break;
  32.383 +  default:
  32.384 +    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  32.385 +  }
  32.386 +}
  32.387 +
  32.388 +
  32.389 +/*
  32.390 + * Set the JPEG colorspace, and choose colorspace-dependent default values.
  32.391 + */
  32.392 +
  32.393 +GLOBAL(void)
  32.394 +jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
  32.395 +{
  32.396 +  jpeg_component_info * compptr;
  32.397 +  int ci;
  32.398 +
  32.399 +#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
  32.400 +  (compptr = &cinfo->comp_info[index], \
  32.401 +   compptr->component_id = (id), \
  32.402 +   compptr->h_samp_factor = (hsamp), \
  32.403 +   compptr->v_samp_factor = (vsamp), \
  32.404 +   compptr->quant_tbl_no = (quant), \
  32.405 +   compptr->dc_tbl_no = (dctbl), \
  32.406 +   compptr->ac_tbl_no = (actbl) )
  32.407 +
  32.408 +  /* Safety check to ensure start_compress not called yet. */
  32.409 +  if (cinfo->global_state != CSTATE_START)
  32.410 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  32.411 +
  32.412 +  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
  32.413 +   * tables 1 for chrominance components.
  32.414 +   */
  32.415 +
  32.416 +  cinfo->jpeg_color_space = colorspace;
  32.417 +
  32.418 +  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  32.419 +  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
  32.420 +
  32.421 +  switch (colorspace) {
  32.422 +  case JCS_GRAYSCALE:
  32.423 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  32.424 +    cinfo->num_components = 1;
  32.425 +    /* JFIF specifies component ID 1 */
  32.426 +    SET_COMP(0, 1, 1,1, 0, 0,0);
  32.427 +    break;
  32.428 +  case JCS_RGB:
  32.429 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
  32.430 +    cinfo->num_components = 3;
  32.431 +    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
  32.432 +    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
  32.433 +    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
  32.434 +    break;
  32.435 +  case JCS_YCbCr:
  32.436 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  32.437 +    cinfo->num_components = 3;
  32.438 +    /* JFIF specifies component IDs 1,2,3 */
  32.439 +    /* We default to 2x2 subsamples of chrominance */
  32.440 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  32.441 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  32.442 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  32.443 +    break;
  32.444 +  case JCS_CMYK:
  32.445 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
  32.446 +    cinfo->num_components = 4;
  32.447 +    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
  32.448 +    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
  32.449 +    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
  32.450 +    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
  32.451 +    break;
  32.452 +  case JCS_YCCK:
  32.453 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
  32.454 +    cinfo->num_components = 4;
  32.455 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  32.456 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  32.457 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  32.458 +    SET_COMP(3, 4, 2,2, 0, 0,0);
  32.459 +    break;
  32.460 +  case JCS_UNKNOWN:
  32.461 +    cinfo->num_components = cinfo->input_components;
  32.462 +    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
  32.463 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  32.464 +	       MAX_COMPONENTS);
  32.465 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  32.466 +      SET_COMP(ci, ci, 1,1, 0, 0,0);
  32.467 +    }
  32.468 +    break;
  32.469 +  default:
  32.470 +    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  32.471 +  }
  32.472 +}
  32.473 +
  32.474 +
  32.475 +#ifdef C_PROGRESSIVE_SUPPORTED
  32.476 +
  32.477 +LOCAL(jpeg_scan_info *)
  32.478 +fill_a_scan (jpeg_scan_info * scanptr, int ci,
  32.479 +	     int Ss, int Se, int Ah, int Al)
  32.480 +/* Support routine: generate one scan for specified component */
  32.481 +{
  32.482 +  scanptr->comps_in_scan = 1;
  32.483 +  scanptr->component_index[0] = ci;
  32.484 +  scanptr->Ss = Ss;
  32.485 +  scanptr->Se = Se;
  32.486 +  scanptr->Ah = Ah;
  32.487 +  scanptr->Al = Al;
  32.488 +  scanptr++;
  32.489 +  return scanptr;
  32.490 +}
  32.491 +
  32.492 +LOCAL(jpeg_scan_info *)
  32.493 +fill_scans (jpeg_scan_info * scanptr, int ncomps,
  32.494 +	    int Ss, int Se, int Ah, int Al)
  32.495 +/* Support routine: generate one scan for each component */
  32.496 +{
  32.497 +  int ci;
  32.498 +
  32.499 +  for (ci = 0; ci < ncomps; ci++) {
  32.500 +    scanptr->comps_in_scan = 1;
  32.501 +    scanptr->component_index[0] = ci;
  32.502 +    scanptr->Ss = Ss;
  32.503 +    scanptr->Se = Se;
  32.504 +    scanptr->Ah = Ah;
  32.505 +    scanptr->Al = Al;
  32.506 +    scanptr++;
  32.507 +  }
  32.508 +  return scanptr;
  32.509 +}
  32.510 +
  32.511 +LOCAL(jpeg_scan_info *)
  32.512 +fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
  32.513 +/* Support routine: generate interleaved DC scan if possible, else N scans */
  32.514 +{
  32.515 +  int ci;
  32.516 +
  32.517 +  if (ncomps <= MAX_COMPS_IN_SCAN) {
  32.518 +    /* Single interleaved DC scan */
  32.519 +    scanptr->comps_in_scan = ncomps;
  32.520 +    for (ci = 0; ci < ncomps; ci++)
  32.521 +      scanptr->component_index[ci] = ci;
  32.522 +    scanptr->Ss = scanptr->Se = 0;
  32.523 +    scanptr->Ah = Ah;
  32.524 +    scanptr->Al = Al;
  32.525 +    scanptr++;
  32.526 +  } else {
  32.527 +    /* Noninterleaved DC scan for each component */
  32.528 +    scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  32.529 +  }
  32.530 +  return scanptr;
  32.531 +}
  32.532 +
  32.533 +
  32.534 +/*
  32.535 + * Create a recommended progressive-JPEG script.
  32.536 + * cinfo->num_components and cinfo->jpeg_color_space must be correct.
  32.537 + */
  32.538 +
  32.539 +GLOBAL(void)
  32.540 +jpeg_simple_progression (j_compress_ptr cinfo)
  32.541 +{
  32.542 +  int ncomps = cinfo->num_components;
  32.543 +  int nscans;
  32.544 +  jpeg_scan_info * scanptr;
  32.545 +
  32.546 +  /* Safety check to ensure start_compress not called yet. */
  32.547 +  if (cinfo->global_state != CSTATE_START)
  32.548 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  32.549 +
  32.550 +  /* Figure space needed for script.  Calculation must match code below! */
  32.551 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  32.552 +    /* Custom script for YCbCr color images. */
  32.553 +    nscans = 10;
  32.554 +  } else {
  32.555 +    /* All-purpose script for other color spaces. */
  32.556 +    if (ncomps > MAX_COMPS_IN_SCAN)
  32.557 +      nscans = 6 * ncomps;	/* 2 DC + 4 AC scans per component */
  32.558 +    else
  32.559 +      nscans = 2 + 4 * ncomps;	/* 2 DC scans; 4 AC scans per component */
  32.560 +  }
  32.561 +
  32.562 +  /* Allocate space for script.
  32.563 +   * We need to put it in the permanent pool in case the application performs
  32.564 +   * multiple compressions without changing the settings.  To avoid a memory
  32.565 +   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
  32.566 +   * object, we try to re-use previously allocated space, and we allocate
  32.567 +   * enough space to handle YCbCr even if initially asked for grayscale.
  32.568 +   */
  32.569 +  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
  32.570 +    cinfo->script_space_size = MAX(nscans, 10);
  32.571 +    cinfo->script_space = (jpeg_scan_info *)
  32.572 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  32.573 +			cinfo->script_space_size * SIZEOF(jpeg_scan_info));
  32.574 +  }
  32.575 +  scanptr = cinfo->script_space;
  32.576 +  cinfo->scan_info = scanptr;
  32.577 +  cinfo->num_scans = nscans;
  32.578 +
  32.579 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  32.580 +    /* Custom script for YCbCr color images. */
  32.581 +    /* Initial DC scan */
  32.582 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  32.583 +    /* Initial AC scan: get some luma data out in a hurry */
  32.584 +    scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  32.585 +    /* Chroma data is too small to be worth expending many scans on */
  32.586 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  32.587 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  32.588 +    /* Complete spectral selection for luma AC */
  32.589 +    scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  32.590 +    /* Refine next bit of luma AC */
  32.591 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  32.592 +    /* Finish DC successive approximation */
  32.593 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  32.594 +    /* Finish AC successive approximation */
  32.595 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  32.596 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  32.597 +    /* Luma bottom bit comes last since it's usually largest scan */
  32.598 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  32.599 +  } else {
  32.600 +    /* All-purpose script for other color spaces. */
  32.601 +    /* Successive approximation first pass */
  32.602 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  32.603 +    scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  32.604 +    scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  32.605 +    /* Successive approximation second pass */
  32.606 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  32.607 +    /* Successive approximation final pass */
  32.608 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  32.609 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  32.610 +  }
  32.611 +}
  32.612 +
  32.613 +#endif /* C_PROGRESSIVE_SUPPORTED */
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/libs/libjpeg/jcphuff.c	Sun Jun 07 17:25:49 2015 +0300
    33.3 @@ -0,0 +1,833 @@
    33.4 +/*
    33.5 + * jcphuff.c
    33.6 + *
    33.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    33.8 + * This file is part of the Independent JPEG Group's software.
    33.9 + * For conditions of distribution and use, see the accompanying README file.
   33.10 + *
   33.11 + * This file contains Huffman entropy encoding routines for progressive JPEG.
   33.12 + *
   33.13 + * We do not support output suspension in this module, since the library
   33.14 + * currently does not allow multiple-scan files to be written with output
   33.15 + * suspension.
   33.16 + */
   33.17 +
   33.18 +#define JPEG_INTERNALS
   33.19 +#include "jinclude.h"
   33.20 +#include "jpeglib.h"
   33.21 +#include "jchuff.h"		/* Declarations shared with jchuff.c */
   33.22 +
   33.23 +#ifdef C_PROGRESSIVE_SUPPORTED
   33.24 +
   33.25 +/* Expanded entropy encoder object for progressive Huffman encoding. */
   33.26 +
   33.27 +typedef struct {
   33.28 +  struct jpeg_entropy_encoder pub; /* public fields */
   33.29 +
   33.30 +  /* Mode flag: TRUE for optimization, FALSE for actual data output */
   33.31 +  boolean gather_statistics;
   33.32 +
   33.33 +  /* Bit-level coding status.
   33.34 +   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
   33.35 +   */
   33.36 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   33.37 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   33.38 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   33.39 +  int put_bits;			/* # of bits now in it */
   33.40 +  j_compress_ptr cinfo;		/* link to cinfo (needed for dump_buffer) */
   33.41 +
   33.42 +  /* Coding status for DC components */
   33.43 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   33.44 +
   33.45 +  /* Coding status for AC components */
   33.46 +  int ac_tbl_no;		/* the table number of the single component */
   33.47 +  unsigned int EOBRUN;		/* run length of EOBs */
   33.48 +  unsigned int BE;		/* # of buffered correction bits before MCU */
   33.49 +  char * bit_buffer;		/* buffer for correction bits (1 per char) */
   33.50 +  /* packing correction bits tightly would save some space but cost time... */
   33.51 +
   33.52 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   33.53 +  int next_restart_num;		/* next restart number to write (0-7) */
   33.54 +
   33.55 +  /* Pointers to derived tables (these workspaces have image lifespan).
   33.56 +   * Since any one scan codes only DC or only AC, we only need one set
   33.57 +   * of tables, not one for DC and one for AC.
   33.58 +   */
   33.59 +  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
   33.60 +
   33.61 +  /* Statistics tables for optimization; again, one set is enough */
   33.62 +  long * count_ptrs[NUM_HUFF_TBLS];
   33.63 +} phuff_entropy_encoder;
   33.64 +
   33.65 +typedef phuff_entropy_encoder * phuff_entropy_ptr;
   33.66 +
   33.67 +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
   33.68 + * buffer can hold.  Larger sizes may slightly improve compression, but
   33.69 + * 1000 is already well into the realm of overkill.
   33.70 + * The minimum safe size is 64 bits.
   33.71 + */
   33.72 +
   33.73 +#define MAX_CORR_BITS  1000	/* Max # of correction bits I can buffer */
   33.74 +
   33.75 +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
   33.76 + * We assume that int right shift is unsigned if INT32 right shift is,
   33.77 + * which should be safe.
   33.78 + */
   33.79 +
   33.80 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
   33.81 +#define ISHIFT_TEMPS	int ishift_temp;
   33.82 +#define IRIGHT_SHIFT(x,shft)  \
   33.83 +	((ishift_temp = (x)) < 0 ? \
   33.84 +	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
   33.85 +	 (ishift_temp >> (shft)))
   33.86 +#else
   33.87 +#define ISHIFT_TEMPS
   33.88 +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
   33.89 +#endif
   33.90 +
   33.91 +/* Forward declarations */
   33.92 +METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
   33.93 +					    JBLOCKROW *MCU_data));
   33.94 +METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
   33.95 +					    JBLOCKROW *MCU_data));
   33.96 +METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
   33.97 +					     JBLOCKROW *MCU_data));
   33.98 +METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
   33.99 +					     JBLOCKROW *MCU_data));
  33.100 +METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
  33.101 +METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
  33.102 +
  33.103 +
  33.104 +/*
  33.105 + * Initialize for a Huffman-compressed scan using progressive JPEG.
  33.106 + */
  33.107 +
  33.108 +METHODDEF(void)
  33.109 +start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
  33.110 +{  
  33.111 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.112 +  boolean is_DC_band;
  33.113 +  int ci, tbl;
  33.114 +  jpeg_component_info * compptr;
  33.115 +
  33.116 +  entropy->cinfo = cinfo;
  33.117 +  entropy->gather_statistics = gather_statistics;
  33.118 +
  33.119 +  is_DC_band = (cinfo->Ss == 0);
  33.120 +
  33.121 +  /* We assume jcmaster.c already validated the scan parameters. */
  33.122 +
  33.123 +  /* Select execution routines */
  33.124 +  if (cinfo->Ah == 0) {
  33.125 +    if (is_DC_band)
  33.126 +      entropy->pub.encode_mcu = encode_mcu_DC_first;
  33.127 +    else
  33.128 +      entropy->pub.encode_mcu = encode_mcu_AC_first;
  33.129 +  } else {
  33.130 +    if (is_DC_band)
  33.131 +      entropy->pub.encode_mcu = encode_mcu_DC_refine;
  33.132 +    else {
  33.133 +      entropy->pub.encode_mcu = encode_mcu_AC_refine;
  33.134 +      /* AC refinement needs a correction bit buffer */
  33.135 +      if (entropy->bit_buffer == NULL)
  33.136 +	entropy->bit_buffer = (char *)
  33.137 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  33.138 +				      MAX_CORR_BITS * SIZEOF(char));
  33.139 +    }
  33.140 +  }
  33.141 +  if (gather_statistics)
  33.142 +    entropy->pub.finish_pass = finish_pass_gather_phuff;
  33.143 +  else
  33.144 +    entropy->pub.finish_pass = finish_pass_phuff;
  33.145 +
  33.146 +  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
  33.147 +   * for AC coefficients.
  33.148 +   */
  33.149 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  33.150 +    compptr = cinfo->cur_comp_info[ci];
  33.151 +    /* Initialize DC predictions to 0 */
  33.152 +    entropy->last_dc_val[ci] = 0;
  33.153 +    /* Get table index */
  33.154 +    if (is_DC_band) {
  33.155 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  33.156 +	continue;
  33.157 +      tbl = compptr->dc_tbl_no;
  33.158 +    } else {
  33.159 +      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  33.160 +    }
  33.161 +    if (gather_statistics) {
  33.162 +      /* Check for invalid table index */
  33.163 +      /* (make_c_derived_tbl does this in the other path) */
  33.164 +      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
  33.165 +        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
  33.166 +      /* Allocate and zero the statistics tables */
  33.167 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  33.168 +      if (entropy->count_ptrs[tbl] == NULL)
  33.169 +	entropy->count_ptrs[tbl] = (long *)
  33.170 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  33.171 +				      257 * SIZEOF(long));
  33.172 +      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
  33.173 +    } else {
  33.174 +      /* Compute derived values for Huffman table */
  33.175 +      /* We may do this more than once for a table, but it's not expensive */
  33.176 +      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  33.177 +			      & entropy->derived_tbls[tbl]);
  33.178 +    }
  33.179 +  }
  33.180 +
  33.181 +  /* Initialize AC stuff */
  33.182 +  entropy->EOBRUN = 0;
  33.183 +  entropy->BE = 0;
  33.184 +
  33.185 +  /* Initialize bit buffer to empty */
  33.186 +  entropy->put_buffer = 0;
  33.187 +  entropy->put_bits = 0;
  33.188 +
  33.189 +  /* Initialize restart stuff */
  33.190 +  entropy->restarts_to_go = cinfo->restart_interval;
  33.191 +  entropy->next_restart_num = 0;
  33.192 +}
  33.193 +
  33.194 +
  33.195 +/* Outputting bytes to the file.
  33.196 + * NB: these must be called only when actually outputting,
  33.197 + * that is, entropy->gather_statistics == FALSE.
  33.198 + */
  33.199 +
  33.200 +/* Emit a byte */
  33.201 +#define emit_byte(entropy,val)  \
  33.202 +	{ *(entropy)->next_output_byte++ = (JOCTET) (val);  \
  33.203 +	  if (--(entropy)->free_in_buffer == 0)  \
  33.204 +	    dump_buffer(entropy); }
  33.205 +
  33.206 +
  33.207 +LOCAL(void)
  33.208 +dump_buffer (phuff_entropy_ptr entropy)
  33.209 +/* Empty the output buffer; we do not support suspension in this module. */
  33.210 +{
  33.211 +  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
  33.212 +
  33.213 +  if (! (*dest->empty_output_buffer) (entropy->cinfo))
  33.214 +    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  33.215 +  /* After a successful buffer dump, must reset buffer pointers */
  33.216 +  entropy->next_output_byte = dest->next_output_byte;
  33.217 +  entropy->free_in_buffer = dest->free_in_buffer;
  33.218 +}
  33.219 +
  33.220 +
  33.221 +/* Outputting bits to the file */
  33.222 +
  33.223 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  33.224 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  33.225 + * in one call, and we never retain more than 7 bits in put_buffer
  33.226 + * between calls, so 24 bits are sufficient.
  33.227 + */
  33.228 +
  33.229 +INLINE
  33.230 +LOCAL(void)
  33.231 +emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  33.232 +/* Emit some bits, unless we are in gather mode */
  33.233 +{
  33.234 +  /* This routine is heavily used, so it's worth coding tightly. */
  33.235 +  register INT32 put_buffer = (INT32) code;
  33.236 +  register int put_bits = entropy->put_bits;
  33.237 +
  33.238 +  /* if size is 0, caller used an invalid Huffman table entry */
  33.239 +  if (size == 0)
  33.240 +    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  33.241 +
  33.242 +  if (entropy->gather_statistics)
  33.243 +    return;			/* do nothing if we're only getting stats */
  33.244 +
  33.245 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  33.246 +  
  33.247 +  put_bits += size;		/* new number of bits in buffer */
  33.248 +  
  33.249 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  33.250 +
  33.251 +  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  33.252 +
  33.253 +  while (put_bits >= 8) {
  33.254 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  33.255 +    
  33.256 +    emit_byte(entropy, c);
  33.257 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  33.258 +      emit_byte(entropy, 0);
  33.259 +    }
  33.260 +    put_buffer <<= 8;
  33.261 +    put_bits -= 8;
  33.262 +  }
  33.263 +
  33.264 +  entropy->put_buffer = put_buffer; /* update variables */
  33.265 +  entropy->put_bits = put_bits;
  33.266 +}
  33.267 +
  33.268 +
  33.269 +LOCAL(void)
  33.270 +flush_bits (phuff_entropy_ptr entropy)
  33.271 +{
  33.272 +  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  33.273 +  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
  33.274 +  entropy->put_bits = 0;
  33.275 +}
  33.276 +
  33.277 +
  33.278 +/*
  33.279 + * Emit (or just count) a Huffman symbol.
  33.280 + */
  33.281 +
  33.282 +INLINE
  33.283 +LOCAL(void)
  33.284 +emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  33.285 +{
  33.286 +  if (entropy->gather_statistics)
  33.287 +    entropy->count_ptrs[tbl_no][symbol]++;
  33.288 +  else {
  33.289 +    c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  33.290 +    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  33.291 +  }
  33.292 +}
  33.293 +
  33.294 +
  33.295 +/*
  33.296 + * Emit bits from a correction bit buffer.
  33.297 + */
  33.298 +
  33.299 +LOCAL(void)
  33.300 +emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  33.301 +		    unsigned int nbits)
  33.302 +{
  33.303 +  if (entropy->gather_statistics)
  33.304 +    return;			/* no real work */
  33.305 +
  33.306 +  while (nbits > 0) {
  33.307 +    emit_bits(entropy, (unsigned int) (*bufstart), 1);
  33.308 +    bufstart++;
  33.309 +    nbits--;
  33.310 +  }
  33.311 +}
  33.312 +
  33.313 +
  33.314 +/*
  33.315 + * Emit any pending EOBRUN symbol.
  33.316 + */
  33.317 +
  33.318 +LOCAL(void)
  33.319 +emit_eobrun (phuff_entropy_ptr entropy)
  33.320 +{
  33.321 +  register int temp, nbits;
  33.322 +
  33.323 +  if (entropy->EOBRUN > 0) {	/* if there is any pending EOBRUN */
  33.324 +    temp = entropy->EOBRUN;
  33.325 +    nbits = 0;
  33.326 +    while ((temp >>= 1))
  33.327 +      nbits++;
  33.328 +    /* safety check: shouldn't happen given limited correction-bit buffer */
  33.329 +    if (nbits > 14)
  33.330 +      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  33.331 +
  33.332 +    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  33.333 +    if (nbits)
  33.334 +      emit_bits(entropy, entropy->EOBRUN, nbits);
  33.335 +
  33.336 +    entropy->EOBRUN = 0;
  33.337 +
  33.338 +    /* Emit any buffered correction bits */
  33.339 +    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  33.340 +    entropy->BE = 0;
  33.341 +  }
  33.342 +}
  33.343 +
  33.344 +
  33.345 +/*
  33.346 + * Emit a restart marker & resynchronize predictions.
  33.347 + */
  33.348 +
  33.349 +LOCAL(void)
  33.350 +emit_restart (phuff_entropy_ptr entropy, int restart_num)
  33.351 +{
  33.352 +  int ci;
  33.353 +
  33.354 +  emit_eobrun(entropy);
  33.355 +
  33.356 +  if (! entropy->gather_statistics) {
  33.357 +    flush_bits(entropy);
  33.358 +    emit_byte(entropy, 0xFF);
  33.359 +    emit_byte(entropy, JPEG_RST0 + restart_num);
  33.360 +  }
  33.361 +
  33.362 +  if (entropy->cinfo->Ss == 0) {
  33.363 +    /* Re-initialize DC predictions to 0 */
  33.364 +    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  33.365 +      entropy->last_dc_val[ci] = 0;
  33.366 +  } else {
  33.367 +    /* Re-initialize all AC-related fields to 0 */
  33.368 +    entropy->EOBRUN = 0;
  33.369 +    entropy->BE = 0;
  33.370 +  }
  33.371 +}
  33.372 +
  33.373 +
  33.374 +/*
  33.375 + * MCU encoding for DC initial scan (either spectral selection,
  33.376 + * or first pass of successive approximation).
  33.377 + */
  33.378 +
  33.379 +METHODDEF(boolean)
  33.380 +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  33.381 +{
  33.382 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.383 +  register int temp, temp2;
  33.384 +  register int nbits;
  33.385 +  int blkn, ci;
  33.386 +  int Al = cinfo->Al;
  33.387 +  JBLOCKROW block;
  33.388 +  jpeg_component_info * compptr;
  33.389 +  ISHIFT_TEMPS
  33.390 +
  33.391 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  33.392 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  33.393 +
  33.394 +  /* Emit restart marker if needed */
  33.395 +  if (cinfo->restart_interval)
  33.396 +    if (entropy->restarts_to_go == 0)
  33.397 +      emit_restart(entropy, entropy->next_restart_num);
  33.398 +
  33.399 +  /* Encode the MCU data blocks */
  33.400 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  33.401 +    block = MCU_data[blkn];
  33.402 +    ci = cinfo->MCU_membership[blkn];
  33.403 +    compptr = cinfo->cur_comp_info[ci];
  33.404 +
  33.405 +    /* Compute the DC value after the required point transform by Al.
  33.406 +     * This is simply an arithmetic right shift.
  33.407 +     */
  33.408 +    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  33.409 +
  33.410 +    /* DC differences are figured on the point-transformed values. */
  33.411 +    temp = temp2 - entropy->last_dc_val[ci];
  33.412 +    entropy->last_dc_val[ci] = temp2;
  33.413 +
  33.414 +    /* Encode the DC coefficient difference per section G.1.2.1 */
  33.415 +    temp2 = temp;
  33.416 +    if (temp < 0) {
  33.417 +      temp = -temp;		/* temp is abs value of input */
  33.418 +      /* For a negative input, want temp2 = bitwise complement of abs(input) */
  33.419 +      /* This code assumes we are on a two's complement machine */
  33.420 +      temp2--;
  33.421 +    }
  33.422 +    
  33.423 +    /* Find the number of bits needed for the magnitude of the coefficient */
  33.424 +    nbits = 0;
  33.425 +    while (temp) {
  33.426 +      nbits++;
  33.427 +      temp >>= 1;
  33.428 +    }
  33.429 +    /* Check for out-of-range coefficient values.
  33.430 +     * Since we're encoding a difference, the range limit is twice as much.
  33.431 +     */
  33.432 +    if (nbits > MAX_COEF_BITS+1)
  33.433 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  33.434 +    
  33.435 +    /* Count/emit the Huffman-coded symbol for the number of bits */
  33.436 +    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  33.437 +    
  33.438 +    /* Emit that number of bits of the value, if positive, */
  33.439 +    /* or the complement of its magnitude, if negative. */
  33.440 +    if (nbits)			/* emit_bits rejects calls with size 0 */
  33.441 +      emit_bits(entropy, (unsigned int) temp2, nbits);
  33.442 +  }
  33.443 +
  33.444 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  33.445 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  33.446 +
  33.447 +  /* Update restart-interval state too */
  33.448 +  if (cinfo->restart_interval) {
  33.449 +    if (entropy->restarts_to_go == 0) {
  33.450 +      entropy->restarts_to_go = cinfo->restart_interval;
  33.451 +      entropy->next_restart_num++;
  33.452 +      entropy->next_restart_num &= 7;
  33.453 +    }
  33.454 +    entropy->restarts_to_go--;
  33.455 +  }
  33.456 +
  33.457 +  return TRUE;
  33.458 +}
  33.459 +
  33.460 +
  33.461 +/*
  33.462 + * MCU encoding for AC initial scan (either spectral selection,
  33.463 + * or first pass of successive approximation).
  33.464 + */
  33.465 +
  33.466 +METHODDEF(boolean)
  33.467 +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  33.468 +{
  33.469 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.470 +  register int temp, temp2;
  33.471 +  register int nbits;
  33.472 +  register int r, k;
  33.473 +  int Se = cinfo->Se;
  33.474 +  int Al = cinfo->Al;
  33.475 +  JBLOCKROW block;
  33.476 +
  33.477 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  33.478 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  33.479 +
  33.480 +  /* Emit restart marker if needed */
  33.481 +  if (cinfo->restart_interval)
  33.482 +    if (entropy->restarts_to_go == 0)
  33.483 +      emit_restart(entropy, entropy->next_restart_num);
  33.484 +
  33.485 +  /* Encode the MCU data block */
  33.486 +  block = MCU_data[0];
  33.487 +
  33.488 +  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  33.489 +  
  33.490 +  r = 0;			/* r = run length of zeros */
  33.491 +   
  33.492 +  for (k = cinfo->Ss; k <= Se; k++) {
  33.493 +    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  33.494 +      r++;
  33.495 +      continue;
  33.496 +    }
  33.497 +    /* We must apply the point transform by Al.  For AC coefficients this
  33.498 +     * is an integer division with rounding towards 0.  To do this portably
  33.499 +     * in C, we shift after obtaining the absolute value; so the code is
  33.500 +     * interwoven with finding the abs value (temp) and output bits (temp2).
  33.501 +     */
  33.502 +    if (temp < 0) {
  33.503 +      temp = -temp;		/* temp is abs value of input */
  33.504 +      temp >>= Al;		/* apply the point transform */
  33.505 +      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  33.506 +      temp2 = ~temp;
  33.507 +    } else {
  33.508 +      temp >>= Al;		/* apply the point transform */
  33.509 +      temp2 = temp;
  33.510 +    }
  33.511 +    /* Watch out for case that nonzero coef is zero after point transform */
  33.512 +    if (temp == 0) {
  33.513 +      r++;
  33.514 +      continue;
  33.515 +    }
  33.516 +
  33.517 +    /* Emit any pending EOBRUN */
  33.518 +    if (entropy->EOBRUN > 0)
  33.519 +      emit_eobrun(entropy);
  33.520 +    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  33.521 +    while (r > 15) {
  33.522 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  33.523 +      r -= 16;
  33.524 +    }
  33.525 +
  33.526 +    /* Find the number of bits needed for the magnitude of the coefficient */
  33.527 +    nbits = 1;			/* there must be at least one 1 bit */
  33.528 +    while ((temp >>= 1))
  33.529 +      nbits++;
  33.530 +    /* Check for out-of-range coefficient values */
  33.531 +    if (nbits > MAX_COEF_BITS)
  33.532 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  33.533 +
  33.534 +    /* Count/emit Huffman symbol for run length / number of bits */
  33.535 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
  33.536 +
  33.537 +    /* Emit that number of bits of the value, if positive, */
  33.538 +    /* or the complement of its magnitude, if negative. */
  33.539 +    emit_bits(entropy, (unsigned int) temp2, nbits);
  33.540 +
  33.541 +    r = 0;			/* reset zero run length */
  33.542 +  }
  33.543 +
  33.544 +  if (r > 0) {			/* If there are trailing zeroes, */
  33.545 +    entropy->EOBRUN++;		/* count an EOB */
  33.546 +    if (entropy->EOBRUN == 0x7FFF)
  33.547 +      emit_eobrun(entropy);	/* force it out to avoid overflow */
  33.548 +  }
  33.549 +
  33.550 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  33.551 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  33.552 +
  33.553 +  /* Update restart-interval state too */
  33.554 +  if (cinfo->restart_interval) {
  33.555 +    if (entropy->restarts_to_go == 0) {
  33.556 +      entropy->restarts_to_go = cinfo->restart_interval;
  33.557 +      entropy->next_restart_num++;
  33.558 +      entropy->next_restart_num &= 7;
  33.559 +    }
  33.560 +    entropy->restarts_to_go--;
  33.561 +  }
  33.562 +
  33.563 +  return TRUE;
  33.564 +}
  33.565 +
  33.566 +
  33.567 +/*
  33.568 + * MCU encoding for DC successive approximation refinement scan.
  33.569 + * Note: we assume such scans can be multi-component, although the spec
  33.570 + * is not very clear on the point.
  33.571 + */
  33.572 +
  33.573 +METHODDEF(boolean)
  33.574 +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  33.575 +{
  33.576 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.577 +  register int temp;
  33.578 +  int blkn;
  33.579 +  int Al = cinfo->Al;
  33.580 +  JBLOCKROW block;
  33.581 +
  33.582 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  33.583 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  33.584 +
  33.585 +  /* Emit restart marker if needed */
  33.586 +  if (cinfo->restart_interval)
  33.587 +    if (entropy->restarts_to_go == 0)
  33.588 +      emit_restart(entropy, entropy->next_restart_num);
  33.589 +
  33.590 +  /* Encode the MCU data blocks */
  33.591 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  33.592 +    block = MCU_data[blkn];
  33.593 +
  33.594 +    /* We simply emit the Al'th bit of the DC coefficient value. */
  33.595 +    temp = (*block)[0];
  33.596 +    emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  33.597 +  }
  33.598 +
  33.599 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  33.600 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  33.601 +
  33.602 +  /* Update restart-interval state too */
  33.603 +  if (cinfo->restart_interval) {
  33.604 +    if (entropy->restarts_to_go == 0) {
  33.605 +      entropy->restarts_to_go = cinfo->restart_interval;
  33.606 +      entropy->next_restart_num++;
  33.607 +      entropy->next_restart_num &= 7;
  33.608 +    }
  33.609 +    entropy->restarts_to_go--;
  33.610 +  }
  33.611 +
  33.612 +  return TRUE;
  33.613 +}
  33.614 +
  33.615 +
  33.616 +/*
  33.617 + * MCU encoding for AC successive approximation refinement scan.
  33.618 + */
  33.619 +
  33.620 +METHODDEF(boolean)
  33.621 +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  33.622 +{
  33.623 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.624 +  register int temp;
  33.625 +  register int r, k;
  33.626 +  int EOB;
  33.627 +  char *BR_buffer;
  33.628 +  unsigned int BR;
  33.629 +  int Se = cinfo->Se;
  33.630 +  int Al = cinfo->Al;
  33.631 +  JBLOCKROW block;
  33.632 +  int absvalues[DCTSIZE2];
  33.633 +
  33.634 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  33.635 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  33.636 +
  33.637 +  /* Emit restart marker if needed */
  33.638 +  if (cinfo->restart_interval)
  33.639 +    if (entropy->restarts_to_go == 0)
  33.640 +      emit_restart(entropy, entropy->next_restart_num);
  33.641 +
  33.642 +  /* Encode the MCU data block */
  33.643 +  block = MCU_data[0];
  33.644 +
  33.645 +  /* It is convenient to make a pre-pass to determine the transformed
  33.646 +   * coefficients' absolute values and the EOB position.
  33.647 +   */
  33.648 +  EOB = 0;
  33.649 +  for (k = cinfo->Ss; k <= Se; k++) {
  33.650 +    temp = (*block)[jpeg_natural_order[k]];
  33.651 +    /* We must apply the point transform by Al.  For AC coefficients this
  33.652 +     * is an integer division with rounding towards 0.  To do this portably
  33.653 +     * in C, we shift after obtaining the absolute value.
  33.654 +     */
  33.655 +    if (temp < 0)
  33.656 +      temp = -temp;		/* temp is abs value of input */
  33.657 +    temp >>= Al;		/* apply the point transform */
  33.658 +    absvalues[k] = temp;	/* save abs value for main pass */
  33.659 +    if (temp == 1)
  33.660 +      EOB = k;			/* EOB = index of last newly-nonzero coef */
  33.661 +  }
  33.662 +
  33.663 +  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  33.664 +  
  33.665 +  r = 0;			/* r = run length of zeros */
  33.666 +  BR = 0;			/* BR = count of buffered bits added now */
  33.667 +  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  33.668 +
  33.669 +  for (k = cinfo->Ss; k <= Se; k++) {
  33.670 +    if ((temp = absvalues[k]) == 0) {
  33.671 +      r++;
  33.672 +      continue;
  33.673 +    }
  33.674 +
  33.675 +    /* Emit any required ZRLs, but not if they can be folded into EOB */
  33.676 +    while (r > 15 && k <= EOB) {
  33.677 +      /* emit any pending EOBRUN and the BE correction bits */
  33.678 +      emit_eobrun(entropy);
  33.679 +      /* Emit ZRL */
  33.680 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  33.681 +      r -= 16;
  33.682 +      /* Emit buffered correction bits that must be associated with ZRL */
  33.683 +      emit_buffered_bits(entropy, BR_buffer, BR);
  33.684 +      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  33.685 +      BR = 0;
  33.686 +    }
  33.687 +
  33.688 +    /* If the coef was previously nonzero, it only needs a correction bit.
  33.689 +     * NOTE: a straight translation of the spec's figure G.7 would suggest
  33.690 +     * that we also need to test r > 15.  But if r > 15, we can only get here
  33.691 +     * if k > EOB, which implies that this coefficient is not 1.
  33.692 +     */
  33.693 +    if (temp > 1) {
  33.694 +      /* The correction bit is the next bit of the absolute value. */
  33.695 +      BR_buffer[BR++] = (char) (temp & 1);
  33.696 +      continue;
  33.697 +    }
  33.698 +
  33.699 +    /* Emit any pending EOBRUN and the BE correction bits */
  33.700 +    emit_eobrun(entropy);
  33.701 +
  33.702 +    /* Count/emit Huffman symbol for run length / number of bits */
  33.703 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
  33.704 +
  33.705 +    /* Emit output bit for newly-nonzero coef */
  33.706 +    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
  33.707 +    emit_bits(entropy, (unsigned int) temp, 1);
  33.708 +
  33.709 +    /* Emit buffered correction bits that must be associated with this code */
  33.710 +    emit_buffered_bits(entropy, BR_buffer, BR);
  33.711 +    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  33.712 +    BR = 0;
  33.713 +    r = 0;			/* reset zero run length */
  33.714 +  }
  33.715 +
  33.716 +  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */
  33.717 +    entropy->EOBRUN++;		/* count an EOB */
  33.718 +    entropy->BE += BR;		/* concat my correction bits to older ones */
  33.719 +    /* We force out the EOB if we risk either:
  33.720 +     * 1. overflow of the EOB counter;
  33.721 +     * 2. overflow of the correction bit buffer during the next MCU.
  33.722 +     */
  33.723 +    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
  33.724 +      emit_eobrun(entropy);
  33.725 +  }
  33.726 +
  33.727 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  33.728 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  33.729 +
  33.730 +  /* Update restart-interval state too */
  33.731 +  if (cinfo->restart_interval) {
  33.732 +    if (entropy->restarts_to_go == 0) {
  33.733 +      entropy->restarts_to_go = cinfo->restart_interval;
  33.734 +      entropy->next_restart_num++;
  33.735 +      entropy->next_restart_num &= 7;
  33.736 +    }
  33.737 +    entropy->restarts_to_go--;
  33.738 +  }
  33.739 +
  33.740 +  return TRUE;
  33.741 +}
  33.742 +
  33.743 +
  33.744 +/*
  33.745 + * Finish up at the end of a Huffman-compressed progressive scan.
  33.746 + */
  33.747 +
  33.748 +METHODDEF(void)
  33.749 +finish_pass_phuff (j_compress_ptr cinfo)
  33.750 +{   
  33.751 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.752 +
  33.753 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  33.754 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  33.755 +
  33.756 +  /* Flush out any buffered data */
  33.757 +  emit_eobrun(entropy);
  33.758 +  flush_bits(entropy);
  33.759 +
  33.760 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  33.761 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  33.762 +}
  33.763 +
  33.764 +
  33.765 +/*
  33.766 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  33.767 + */
  33.768 +
  33.769 +METHODDEF(void)
  33.770 +finish_pass_gather_phuff (j_compress_ptr cinfo)
  33.771 +{
  33.772 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  33.773 +  boolean is_DC_band;
  33.774 +  int ci, tbl;
  33.775 +  jpeg_component_info * compptr;
  33.776 +  JHUFF_TBL **htblptr;
  33.777 +  boolean did[NUM_HUFF_TBLS];
  33.778 +
  33.779 +  /* Flush out buffered data (all we care about is counting the EOB symbol) */
  33.780 +  emit_eobrun(entropy);
  33.781 +
  33.782 +  is_DC_band = (cinfo->Ss == 0);
  33.783 +
  33.784 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  33.785 +   * per table, because it clobbers the input frequency counts!
  33.786 +   */
  33.787 +  MEMZERO(did, SIZEOF(did));
  33.788 +
  33.789 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  33.790 +    compptr = cinfo->cur_comp_info[ci];
  33.791 +    if (is_DC_band) {
  33.792 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  33.793 +	continue;
  33.794 +      tbl = compptr->dc_tbl_no;
  33.795 +    } else {
  33.796 +      tbl = compptr->ac_tbl_no;
  33.797 +    }
  33.798 +    if (! did[tbl]) {
  33.799 +      if (is_DC_band)
  33.800 +        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  33.801 +      else
  33.802 +        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  33.803 +      if (*htblptr == NULL)
  33.804 +        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  33.805 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  33.806 +      did[tbl] = TRUE;
  33.807 +    }
  33.808 +  }
  33.809 +}
  33.810 +
  33.811 +
  33.812 +/*
  33.813 + * Module initialization routine for progressive Huffman entropy encoding.
  33.814 + */
  33.815 +
  33.816 +GLOBAL(void)
  33.817 +jinit_phuff_encoder (j_compress_ptr cinfo)
  33.818 +{
  33.819 +  phuff_entropy_ptr entropy;
  33.820 +  int i;
  33.821 +
  33.822 +  entropy = (phuff_entropy_ptr)
  33.823 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  33.824 +				SIZEOF(phuff_entropy_encoder));
  33.825 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  33.826 +  entropy->pub.start_pass = start_pass_phuff;
  33.827 +
  33.828 +  /* Mark tables unallocated */
  33.829 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  33.830 +    entropy->derived_tbls[i] = NULL;
  33.831 +    entropy->count_ptrs[i] = NULL;
  33.832 +  }
  33.833 +  entropy->bit_buffer = NULL;	/* needed only in AC refinement scan */
  33.834 +}
  33.835 +
  33.836 +#endif /* C_PROGRESSIVE_SUPPORTED */
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/libs/libjpeg/jcprepct.c	Sun Jun 07 17:25:49 2015 +0300
    34.3 @@ -0,0 +1,354 @@
    34.4 +/*
    34.5 + * jcprepct.c
    34.6 + *
    34.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    34.8 + * This file is part of the Independent JPEG Group's software.
    34.9 + * For conditions of distribution and use, see the accompanying README file.
   34.10 + *
   34.11 + * This file contains the compression preprocessing controller.
   34.12 + * This controller manages the color conversion, downsampling,
   34.13 + * and edge expansion steps.
   34.14 + *
   34.15 + * Most of the complexity here is associated with buffering input rows
   34.16 + * as required by the downsampler.  See the comments at the head of
   34.17 + * jcsample.c for the downsampler's needs.
   34.18 + */
   34.19 +
   34.20 +#define JPEG_INTERNALS
   34.21 +#include "jinclude.h"
   34.22 +#include "jpeglib.h"
   34.23 +
   34.24 +
   34.25 +/* At present, jcsample.c can request context rows only for smoothing.
   34.26 + * In the future, we might also need context rows for CCIR601 sampling
   34.27 + * or other more-complex downsampling procedures.  The code to support
   34.28 + * context rows should be compiled only if needed.
   34.29 + */
   34.30 +#ifdef INPUT_SMOOTHING_SUPPORTED
   34.31 +#define CONTEXT_ROWS_SUPPORTED
   34.32 +#endif
   34.33 +
   34.34 +
   34.35 +/*
   34.36 + * For the simple (no-context-row) case, we just need to buffer one
   34.37 + * row group's worth of pixels for the downsampling step.  At the bottom of
   34.38 + * the image, we pad to a full row group by replicating the last pixel row.
   34.39 + * The downsampler's last output row is then replicated if needed to pad
   34.40 + * out to a full iMCU row.
   34.41 + *
   34.42 + * When providing context rows, we must buffer three row groups' worth of
   34.43 + * pixels.  Three row groups are physically allocated, but the row pointer
   34.44 + * arrays are made five row groups high, with the extra pointers above and
   34.45 + * below "wrapping around" to point to the last and first real row groups.
   34.46 + * This allows the downsampler to access the proper context rows.
   34.47 + * At the top and bottom of the image, we create dummy context rows by
   34.48 + * copying the first or last real pixel row.  This copying could be avoided
   34.49 + * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
   34.50 + * trouble on the compression side.
   34.51 + */
   34.52 +
   34.53 +
   34.54 +/* Private buffer controller object */
   34.55 +
   34.56 +typedef struct {
   34.57 +  struct jpeg_c_prep_controller pub; /* public fields */
   34.58 +
   34.59 +  /* Downsampling input buffer.  This buffer holds color-converted data
   34.60 +   * until we have enough to do a downsample step.
   34.61 +   */
   34.62 +  JSAMPARRAY color_buf[MAX_COMPONENTS];
   34.63 +
   34.64 +  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
   34.65 +  int next_buf_row;		/* index of next row to store in color_buf */
   34.66 +
   34.67 +#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
   34.68 +  int this_row_group;		/* starting row index of group to process */
   34.69 +  int next_buf_stop;		/* downsample when we reach this index */
   34.70 +#endif
   34.71 +} my_prep_controller;
   34.72 +
   34.73 +typedef my_prep_controller * my_prep_ptr;
   34.74 +
   34.75 +
   34.76 +/*
   34.77 + * Initialize for a processing pass.
   34.78 + */
   34.79 +
   34.80 +METHODDEF(void)
   34.81 +start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   34.82 +{
   34.83 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
   34.84 +
   34.85 +  if (pass_mode != JBUF_PASS_THRU)
   34.86 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   34.87 +
   34.88 +  /* Initialize total-height counter for detecting bottom of image */
   34.89 +  prep->rows_to_go = cinfo->image_height;
   34.90 +  /* Mark the conversion buffer empty */
   34.91 +  prep->next_buf_row = 0;
   34.92 +#ifdef CONTEXT_ROWS_SUPPORTED
   34.93 +  /* Preset additional state variables for context mode.
   34.94 +   * These aren't used in non-context mode, so we needn't test which mode.
   34.95 +   */
   34.96 +  prep->this_row_group = 0;
   34.97 +  /* Set next_buf_stop to stop after two row groups have been read in. */
   34.98 +  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
   34.99 +#endif
  34.100 +}
  34.101 +
  34.102 +
  34.103 +/*
  34.104 + * Expand an image vertically from height input_rows to height output_rows,
  34.105 + * by duplicating the bottom row.
  34.106 + */
  34.107 +
  34.108 +LOCAL(void)
  34.109 +expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  34.110 +		    int input_rows, int output_rows)
  34.111 +{
  34.112 +  register int row;
  34.113 +
  34.114 +  for (row = input_rows; row < output_rows; row++) {
  34.115 +    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  34.116 +		      1, num_cols);
  34.117 +  }
  34.118 +}
  34.119 +
  34.120 +
  34.121 +/*
  34.122 + * Process some data in the simple no-context case.
  34.123 + *
  34.124 + * Preprocessor output data is counted in "row groups".  A row group
  34.125 + * is defined to be v_samp_factor sample rows of each component.
  34.126 + * Downsampling will produce this much data from each max_v_samp_factor
  34.127 + * input rows.
  34.128 + */
  34.129 +
  34.130 +METHODDEF(void)
  34.131 +pre_process_data (j_compress_ptr cinfo,
  34.132 +		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  34.133 +		  JDIMENSION in_rows_avail,
  34.134 +		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  34.135 +		  JDIMENSION out_row_groups_avail)
  34.136 +{
  34.137 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  34.138 +  int numrows, ci;
  34.139 +  JDIMENSION inrows;
  34.140 +  jpeg_component_info * compptr;
  34.141 +
  34.142 +  while (*in_row_ctr < in_rows_avail &&
  34.143 +	 *out_row_group_ctr < out_row_groups_avail) {
  34.144 +    /* Do color conversion to fill the conversion buffer. */
  34.145 +    inrows = in_rows_avail - *in_row_ctr;
  34.146 +    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  34.147 +    numrows = (int) MIN((JDIMENSION) numrows, inrows);
  34.148 +    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  34.149 +				       prep->color_buf,
  34.150 +				       (JDIMENSION) prep->next_buf_row,
  34.151 +				       numrows);
  34.152 +    *in_row_ctr += numrows;
  34.153 +    prep->next_buf_row += numrows;
  34.154 +    prep->rows_to_go -= numrows;
  34.155 +    /* If at bottom of image, pad to fill the conversion buffer. */
  34.156 +    if (prep->rows_to_go == 0 &&
  34.157 +	prep->next_buf_row < cinfo->max_v_samp_factor) {
  34.158 +      for (ci = 0; ci < cinfo->num_components; ci++) {
  34.159 +	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  34.160 +			   prep->next_buf_row, cinfo->max_v_samp_factor);
  34.161 +      }
  34.162 +      prep->next_buf_row = cinfo->max_v_samp_factor;
  34.163 +    }
  34.164 +    /* If we've filled the conversion buffer, empty it. */
  34.165 +    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  34.166 +      (*cinfo->downsample->downsample) (cinfo,
  34.167 +					prep->color_buf, (JDIMENSION) 0,
  34.168 +					output_buf, *out_row_group_ctr);
  34.169 +      prep->next_buf_row = 0;
  34.170 +      (*out_row_group_ctr)++;
  34.171 +    }
  34.172 +    /* If at bottom of image, pad the output to a full iMCU height.
  34.173 +     * Note we assume the caller is providing a one-iMCU-height output buffer!
  34.174 +     */
  34.175 +    if (prep->rows_to_go == 0 &&
  34.176 +	*out_row_group_ctr < out_row_groups_avail) {
  34.177 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  34.178 +	   ci++, compptr++) {
  34.179 +	expand_bottom_edge(output_buf[ci],
  34.180 +			   compptr->width_in_blocks * DCTSIZE,
  34.181 +			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
  34.182 +			   (int) (out_row_groups_avail * compptr->v_samp_factor));
  34.183 +      }
  34.184 +      *out_row_group_ctr = out_row_groups_avail;
  34.185 +      break;			/* can exit outer loop without test */
  34.186 +    }
  34.187 +  }
  34.188 +}
  34.189 +
  34.190 +
  34.191 +#ifdef CONTEXT_ROWS_SUPPORTED
  34.192 +
  34.193 +/*
  34.194 + * Process some data in the context case.
  34.195 + */
  34.196 +
  34.197 +METHODDEF(void)
  34.198 +pre_process_context (j_compress_ptr cinfo,
  34.199 +		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  34.200 +		     JDIMENSION in_rows_avail,
  34.201 +		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  34.202 +		     JDIMENSION out_row_groups_avail)
  34.203 +{
  34.204 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  34.205 +  int numrows, ci;
  34.206 +  int buf_height = cinfo->max_v_samp_factor * 3;
  34.207 +  JDIMENSION inrows;
  34.208 +
  34.209 +  while (*out_row_group_ctr < out_row_groups_avail) {
  34.210 +    if (*in_row_ctr < in_rows_avail) {
  34.211 +      /* Do color conversion to fill the conversion buffer. */
  34.212 +      inrows = in_rows_avail - *in_row_ctr;
  34.213 +      numrows = prep->next_buf_stop - prep->next_buf_row;
  34.214 +      numrows = (int) MIN((JDIMENSION) numrows, inrows);
  34.215 +      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  34.216 +					 prep->color_buf,
  34.217 +					 (JDIMENSION) prep->next_buf_row,
  34.218 +					 numrows);
  34.219 +      /* Pad at top of image, if first time through */
  34.220 +      if (prep->rows_to_go == cinfo->image_height) {
  34.221 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  34.222 +	  int row;
  34.223 +	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  34.224 +	    jcopy_sample_rows(prep->color_buf[ci], 0,
  34.225 +			      prep->color_buf[ci], -row,
  34.226 +			      1, cinfo->image_width);
  34.227 +	  }
  34.228 +	}
  34.229 +      }
  34.230 +      *in_row_ctr += numrows;
  34.231 +      prep->next_buf_row += numrows;
  34.232 +      prep->rows_to_go -= numrows;
  34.233 +    } else {
  34.234 +      /* Return for more data, unless we are at the bottom of the image. */
  34.235 +      if (prep->rows_to_go != 0)
  34.236 +	break;
  34.237 +      /* When at bottom of image, pad to fill the conversion buffer. */
  34.238 +      if (prep->next_buf_row < prep->next_buf_stop) {
  34.239 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  34.240 +	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  34.241 +			     prep->next_buf_row, prep->next_buf_stop);
  34.242 +	}
  34.243 +	prep->next_buf_row = prep->next_buf_stop;
  34.244 +      }
  34.245 +    }
  34.246 +    /* If we've gotten enough data, downsample a row group. */
  34.247 +    if (prep->next_buf_row == prep->next_buf_stop) {
  34.248 +      (*cinfo->downsample->downsample) (cinfo,
  34.249 +					prep->color_buf,
  34.250 +					(JDIMENSION) prep->this_row_group,
  34.251 +					output_buf, *out_row_group_ctr);
  34.252 +      (*out_row_group_ctr)++;
  34.253 +      /* Advance pointers with wraparound as necessary. */
  34.254 +      prep->this_row_group += cinfo->max_v_samp_factor;
  34.255 +      if (prep->this_row_group >= buf_height)
  34.256 +	prep->this_row_group = 0;
  34.257 +      if (prep->next_buf_row >= buf_height)
  34.258 +	prep->next_buf_row = 0;
  34.259 +      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  34.260 +    }
  34.261 +  }
  34.262 +}
  34.263 +
  34.264 +
  34.265 +/*
  34.266 + * Create the wrapped-around downsampling input buffer needed for context mode.
  34.267 + */
  34.268 +
  34.269 +LOCAL(void)
  34.270 +create_context_buffer (j_compress_ptr cinfo)
  34.271 +{
  34.272 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  34.273 +  int rgroup_height = cinfo->max_v_samp_factor;
  34.274 +  int ci, i;
  34.275 +  jpeg_component_info * compptr;
  34.276 +  JSAMPARRAY true_buffer, fake_buffer;
  34.277 +
  34.278 +  /* Grab enough space for fake row pointers for all the components;
  34.279 +   * we need five row groups' worth of pointers for each component.
  34.280 +   */
  34.281 +  fake_buffer = (JSAMPARRAY)
  34.282 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  34.283 +				(cinfo->num_components * 5 * rgroup_height) *
  34.284 +				SIZEOF(JSAMPROW));
  34.285 +
  34.286 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  34.287 +       ci++, compptr++) {
  34.288 +    /* Allocate the actual buffer space (3 row groups) for this component.
  34.289 +     * We make the buffer wide enough to allow the downsampler to edge-expand
  34.290 +     * horizontally within the buffer, if it so chooses.
  34.291 +     */
  34.292 +    true_buffer = (*cinfo->mem->alloc_sarray)
  34.293 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
  34.294 +       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  34.295 +		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  34.296 +       (JDIMENSION) (3 * rgroup_height));
  34.297 +    /* Copy true buffer row pointers into the middle of the fake row array */
  34.298 +    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  34.299 +	    3 * rgroup_height * SIZEOF(JSAMPROW));
  34.300 +    /* Fill in the above and below wraparound pointers */
  34.301 +    for (i = 0; i < rgroup_height; i++) {
  34.302 +      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  34.303 +      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  34.304 +    }
  34.305 +    prep->color_buf[ci] = fake_buffer + rgroup_height;
  34.306 +    fake_buffer += 5 * rgroup_height; /* point to space for next component */
  34.307 +  }
  34.308 +}
  34.309 +
  34.310 +#endif /* CONTEXT_ROWS_SUPPORTED */
  34.311 +
  34.312 +
  34.313 +/*
  34.314 + * Initialize preprocessing controller.
  34.315 + */
  34.316 +
  34.317 +GLOBAL(void)
  34.318 +jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  34.319 +{
  34.320 +  my_prep_ptr prep;
  34.321 +  int ci;
  34.322 +  jpeg_component_info * compptr;
  34.323 +
  34.324 +  if (need_full_buffer)		/* safety check */
  34.325 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  34.326 +
  34.327 +  prep = (my_prep_ptr)
  34.328 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  34.329 +				SIZEOF(my_prep_controller));
  34.330 +  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  34.331 +  prep->pub.start_pass = start_pass_prep;
  34.332 +
  34.333 +  /* Allocate the color conversion buffer.
  34.334 +   * We make the buffer wide enough to allow the downsampler to edge-expand
  34.335 +   * horizontally within the buffer, if it so chooses.
  34.336 +   */
  34.337 +  if (cinfo->downsample->need_context_rows) {
  34.338 +    /* Set up to provide context rows */
  34.339 +#ifdef CONTEXT_ROWS_SUPPORTED
  34.340 +    prep->pub.pre_process_data = pre_process_context;
  34.341 +    create_context_buffer(cinfo);
  34.342 +#else
  34.343 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  34.344 +#endif
  34.345 +  } else {
  34.346 +    /* No context, just make it tall enough for one row group */
  34.347 +    prep->pub.pre_process_data = pre_process_data;
  34.348 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  34.349 +	 ci++, compptr++) {
  34.350 +      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  34.351 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  34.352 +	 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  34.353 +			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  34.354 +	 (JDIMENSION) cinfo->max_v_samp_factor);
  34.355 +    }
  34.356 +  }
  34.357 +}
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/libs/libjpeg/jcsample.c	Sun Jun 07 17:25:49 2015 +0300
    35.3 @@ -0,0 +1,519 @@
    35.4 +/*
    35.5 + * jcsample.c
    35.6 + *
    35.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    35.8 + * This file is part of the Independent JPEG Group's software.
    35.9 + * For conditions of distribution and use, see the accompanying README file.
   35.10 + *
   35.11 + * This file contains downsampling routines.
   35.12 + *
   35.13 + * Downsampling input data is counted in "row groups".  A row group
   35.14 + * is defined to be max_v_samp_factor pixel rows of each component,
   35.15 + * from which the downsampler produces v_samp_factor sample rows.
   35.16 + * A single row group is processed in each call to the downsampler module.
   35.17 + *
   35.18 + * The downsampler is responsible for edge-expansion of its output data
   35.19 + * to fill an integral number of DCT blocks horizontally.  The source buffer
   35.20 + * may be modified if it is helpful for this purpose (the source buffer is
   35.21 + * allocated wide enough to correspond to the desired output width).
   35.22 + * The caller (the prep controller) is responsible for vertical padding.
   35.23 + *
   35.24 + * The downsampler may request "context rows" by setting need_context_rows
   35.25 + * during startup.  In this case, the input arrays will contain at least
   35.26 + * one row group's worth of pixels above and below the passed-in data;
   35.27 + * the caller will create dummy rows at image top and bottom by replicating
   35.28 + * the first or last real pixel row.
   35.29 + *
   35.30 + * An excellent reference for image resampling is
   35.31 + *   Digital Image Warping, George Wolberg, 1990.
   35.32 + *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
   35.33 + *
   35.34 + * The downsampling algorithm used here is a simple average of the source
   35.35 + * pixels covered by the output pixel.  The hi-falutin sampling literature
   35.36 + * refers to this as a "box filter".  In general the characteristics of a box
   35.37 + * filter are not very good, but for the specific cases we normally use (1:1
   35.38 + * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
   35.39 + * nearly so bad.  If you intend to use other sampling ratios, you'd be well
   35.40 + * advised to improve this code.
   35.41 + *
   35.42 + * A simple input-smoothing capability is provided.  This is mainly intended
   35.43 + * for cleaning up color-dithered GIF input files (if you find it inadequate,
   35.44 + * we suggest using an external filtering program such as pnmconvol).  When
   35.45 + * enabled, each input pixel P is replaced by a weighted sum of itself and its
   35.46 + * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
   35.47 + * where SF = (smoothing_factor / 1024).
   35.48 + * Currently, smoothing is only supported for 2h2v sampling factors.
   35.49 + */
   35.50 +
   35.51 +#define JPEG_INTERNALS
   35.52 +#include "jinclude.h"
   35.53 +#include "jpeglib.h"
   35.54 +
   35.55 +
   35.56 +/* Pointer to routine to downsample a single component */
   35.57 +typedef JMETHOD(void, downsample1_ptr,
   35.58 +		(j_compress_ptr cinfo, jpeg_component_info * compptr,
   35.59 +		 JSAMPARRAY input_data, JSAMPARRAY output_data));
   35.60 +
   35.61 +/* Private subobject */
   35.62 +
   35.63 +typedef struct {
   35.64 +  struct jpeg_downsampler pub;	/* public fields */
   35.65 +
   35.66 +  /* Downsampling method pointers, one per component */
   35.67 +  downsample1_ptr methods[MAX_COMPONENTS];
   35.68 +} my_downsampler;
   35.69 +
   35.70 +typedef my_downsampler * my_downsample_ptr;
   35.71 +
   35.72 +
   35.73 +/*
   35.74 + * Initialize for a downsampling pass.
   35.75 + */
   35.76 +
   35.77 +METHODDEF(void)
   35.78 +start_pass_downsample (j_compress_ptr cinfo)
   35.79 +{
   35.80 +  /* no work for now */
   35.81 +}
   35.82 +
   35.83 +
   35.84 +/*
   35.85 + * Expand a component horizontally from width input_cols to width output_cols,
   35.86 + * by duplicating the rightmost samples.
   35.87 + */
   35.88 +
   35.89 +LOCAL(void)
   35.90 +expand_right_edge (JSAMPARRAY image_data, int num_rows,
   35.91 +		   JDIMENSION input_cols, JDIMENSION output_cols)
   35.92 +{
   35.93 +  register JSAMPROW ptr;
   35.94 +  register JSAMPLE pixval;
   35.95 +  register int count;
   35.96 +  int row;
   35.97 +  int numcols = (int) (output_cols - input_cols);
   35.98 +
   35.99 +  if (numcols > 0) {
  35.100 +    for (row = 0; row < num_rows; row++) {
  35.101 +      ptr = image_data[row] + input_cols;
  35.102 +      pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
  35.103 +      for (count = numcols; count > 0; count--)
  35.104 +	*ptr++ = pixval;
  35.105 +    }
  35.106 +  }
  35.107 +}
  35.108 +
  35.109 +
  35.110 +/*
  35.111 + * Do downsampling for a whole row group (all components).
  35.112 + *
  35.113 + * In this version we simply downsample each component independently.
  35.114 + */
  35.115 +
  35.116 +METHODDEF(void)
  35.117 +sep_downsample (j_compress_ptr cinfo,
  35.118 +		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
  35.119 +		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
  35.120 +{
  35.121 +  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
  35.122 +  int ci;
  35.123 +  jpeg_component_info * compptr;
  35.124 +  JSAMPARRAY in_ptr, out_ptr;
  35.125 +
  35.126 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  35.127 +       ci++, compptr++) {
  35.128 +    in_ptr = input_buf[ci] + in_row_index;
  35.129 +    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
  35.130 +    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
  35.131 +  }
  35.132 +}
  35.133 +
  35.134 +
  35.135 +/*
  35.136 + * Downsample pixel values of a single component.
  35.137 + * One row group is processed per call.
  35.138 + * This version handles arbitrary integral sampling ratios, without smoothing.
  35.139 + * Note that this version is not actually used for customary sampling ratios.
  35.140 + */
  35.141 +
  35.142 +METHODDEF(void)
  35.143 +int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  35.144 +		JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.145 +{
  35.146 +  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  35.147 +  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
  35.148 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  35.149 +  JSAMPROW inptr, outptr;
  35.150 +  INT32 outvalue;
  35.151 +
  35.152 +  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  35.153 +  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  35.154 +  numpix = h_expand * v_expand;
  35.155 +  numpix2 = numpix/2;
  35.156 +
  35.157 +  /* Expand input data enough to let all the output samples be generated
  35.158 +   * by the standard loop.  Special-casing padded output would be more
  35.159 +   * efficient.
  35.160 +   */
  35.161 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  35.162 +		    cinfo->image_width, output_cols * h_expand);
  35.163 +
  35.164 +  inrow = 0;
  35.165 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  35.166 +    outptr = output_data[outrow];
  35.167 +    for (outcol = 0, outcol_h = 0; outcol < output_cols;
  35.168 +	 outcol++, outcol_h += h_expand) {
  35.169 +      outvalue = 0;
  35.170 +      for (v = 0; v < v_expand; v++) {
  35.171 +	inptr = input_data[inrow+v] + outcol_h;
  35.172 +	for (h = 0; h < h_expand; h++) {
  35.173 +	  outvalue += (INT32) GETJSAMPLE(*inptr++);
  35.174 +	}
  35.175 +      }
  35.176 +      *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
  35.177 +    }
  35.178 +    inrow += v_expand;
  35.179 +  }
  35.180 +}
  35.181 +
  35.182 +
  35.183 +/*
  35.184 + * Downsample pixel values of a single component.
  35.185 + * This version handles the special case of a full-size component,
  35.186 + * without smoothing.
  35.187 + */
  35.188 +
  35.189 +METHODDEF(void)
  35.190 +fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  35.191 +		     JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.192 +{
  35.193 +  /* Copy the data */
  35.194 +  jcopy_sample_rows(input_data, 0, output_data, 0,
  35.195 +		    cinfo->max_v_samp_factor, cinfo->image_width);
  35.196 +  /* Edge-expand */
  35.197 +  expand_right_edge(output_data, cinfo->max_v_samp_factor,
  35.198 +		    cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
  35.199 +}
  35.200 +
  35.201 +
  35.202 +/*
  35.203 + * Downsample pixel values of a single component.
  35.204 + * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  35.205 + * without smoothing.
  35.206 + *
  35.207 + * A note about the "bias" calculations: when rounding fractional values to
  35.208 + * integer, we do not want to always round 0.5 up to the next integer.
  35.209 + * If we did that, we'd introduce a noticeable bias towards larger values.
  35.210 + * Instead, this code is arranged so that 0.5 will be rounded up or down at
  35.211 + * alternate pixel locations (a simple ordered dither pattern).
  35.212 + */
  35.213 +
  35.214 +METHODDEF(void)
  35.215 +h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  35.216 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.217 +{
  35.218 +  int outrow;
  35.219 +  JDIMENSION outcol;
  35.220 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  35.221 +  register JSAMPROW inptr, outptr;
  35.222 +  register int bias;
  35.223 +
  35.224 +  /* Expand input data enough to let all the output samples be generated
  35.225 +   * by the standard loop.  Special-casing padded output would be more
  35.226 +   * efficient.
  35.227 +   */
  35.228 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  35.229 +		    cinfo->image_width, output_cols * 2);
  35.230 +
  35.231 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  35.232 +    outptr = output_data[outrow];
  35.233 +    inptr = input_data[outrow];
  35.234 +    bias = 0;			/* bias = 0,1,0,1,... for successive samples */
  35.235 +    for (outcol = 0; outcol < output_cols; outcol++) {
  35.236 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
  35.237 +			      + bias) >> 1);
  35.238 +      bias ^= 1;		/* 0=>1, 1=>0 */
  35.239 +      inptr += 2;
  35.240 +    }
  35.241 +  }
  35.242 +}
  35.243 +
  35.244 +
  35.245 +/*
  35.246 + * Downsample pixel values of a single component.
  35.247 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  35.248 + * without smoothing.
  35.249 + */
  35.250 +
  35.251 +METHODDEF(void)
  35.252 +h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  35.253 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.254 +{
  35.255 +  int inrow, outrow;
  35.256 +  JDIMENSION outcol;
  35.257 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  35.258 +  register JSAMPROW inptr0, inptr1, outptr;
  35.259 +  register int bias;
  35.260 +
  35.261 +  /* Expand input data enough to let all the output samples be generated
  35.262 +   * by the standard loop.  Special-casing padded output would be more
  35.263 +   * efficient.
  35.264 +   */
  35.265 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  35.266 +		    cinfo->image_width, output_cols * 2);
  35.267 +
  35.268 +  inrow = 0;
  35.269 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  35.270 +    outptr = output_data[outrow];
  35.271 +    inptr0 = input_data[inrow];
  35.272 +    inptr1 = input_data[inrow+1];
  35.273 +    bias = 1;			/* bias = 1,2,1,2,... for successive samples */
  35.274 +    for (outcol = 0; outcol < output_cols; outcol++) {
  35.275 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  35.276 +			      GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
  35.277 +			      + bias) >> 2);
  35.278 +      bias ^= 3;		/* 1=>2, 2=>1 */
  35.279 +      inptr0 += 2; inptr1 += 2;
  35.280 +    }
  35.281 +    inrow += 2;
  35.282 +  }
  35.283 +}
  35.284 +
  35.285 +
  35.286 +#ifdef INPUT_SMOOTHING_SUPPORTED
  35.287 +
  35.288 +/*
  35.289 + * Downsample pixel values of a single component.
  35.290 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  35.291 + * with smoothing.  One row of context is required.
  35.292 + */
  35.293 +
  35.294 +METHODDEF(void)
  35.295 +h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  35.296 +			JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.297 +{
  35.298 +  int inrow, outrow;
  35.299 +  JDIMENSION colctr;
  35.300 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  35.301 +  register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  35.302 +  INT32 membersum, neighsum, memberscale, neighscale;
  35.303 +
  35.304 +  /* Expand input data enough to let all the output samples be generated
  35.305 +   * by the standard loop.  Special-casing padded output would be more
  35.306 +   * efficient.
  35.307 +   */
  35.308 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  35.309 +		    cinfo->image_width, output_cols * 2);
  35.310 +
  35.311 +  /* We don't bother to form the individual "smoothed" input pixel values;
  35.312 +   * we can directly compute the output which is the average of the four
  35.313 +   * smoothed values.  Each of the four member pixels contributes a fraction
  35.314 +   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
  35.315 +   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
  35.316 +   * output.  The four corner-adjacent neighbor pixels contribute a fraction
  35.317 +   * SF to just one smoothed pixel, or SF/4 to the final output; while the
  35.318 +   * eight edge-adjacent neighbors contribute SF to each of two smoothed
  35.319 +   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
  35.320 +   * factors are scaled by 2^16 = 65536.
  35.321 +   * Also recall that SF = smoothing_factor / 1024.
  35.322 +   */
  35.323 +
  35.324 +  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
  35.325 +  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
  35.326 +
  35.327 +  inrow = 0;
  35.328 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  35.329 +    outptr = output_data[outrow];
  35.330 +    inptr0 = input_data[inrow];
  35.331 +    inptr1 = input_data[inrow+1];
  35.332 +    above_ptr = input_data[inrow-1];
  35.333 +    below_ptr = input_data[inrow+2];
  35.334 +
  35.335 +    /* Special case for first column: pretend column -1 is same as column 0 */
  35.336 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  35.337 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  35.338 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  35.339 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  35.340 +	       GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
  35.341 +	       GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
  35.342 +    neighsum += neighsum;
  35.343 +    neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
  35.344 +		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
  35.345 +    membersum = membersum * memberscale + neighsum * neighscale;
  35.346 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  35.347 +    inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  35.348 +
  35.349 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  35.350 +      /* sum of pixels directly mapped to this output element */
  35.351 +      membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  35.352 +		  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  35.353 +      /* sum of edge-neighbor pixels */
  35.354 +      neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  35.355 +		 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  35.356 +		 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
  35.357 +		 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
  35.358 +      /* The edge-neighbors count twice as much as corner-neighbors */
  35.359 +      neighsum += neighsum;
  35.360 +      /* Add in the corner-neighbors */
  35.361 +      neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
  35.362 +		  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
  35.363 +      /* form final output scaled up by 2^16 */
  35.364 +      membersum = membersum * memberscale + neighsum * neighscale;
  35.365 +      /* round, descale and output it */
  35.366 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  35.367 +      inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  35.368 +    }
  35.369 +
  35.370 +    /* Special case for last column */
  35.371 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  35.372 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  35.373 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  35.374 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  35.375 +	       GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
  35.376 +	       GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
  35.377 +    neighsum += neighsum;
  35.378 +    neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
  35.379 +		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
  35.380 +    membersum = membersum * memberscale + neighsum * neighscale;
  35.381 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  35.382 +
  35.383 +    inrow += 2;
  35.384 +  }
  35.385 +}
  35.386 +
  35.387 +
  35.388 +/*
  35.389 + * Downsample pixel values of a single component.
  35.390 + * This version handles the special case of a full-size component,
  35.391 + * with smoothing.  One row of context is required.
  35.392 + */
  35.393 +
  35.394 +METHODDEF(void)
  35.395 +fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
  35.396 +			    JSAMPARRAY input_data, JSAMPARRAY output_data)
  35.397 +{
  35.398 +  int outrow;
  35.399 +  JDIMENSION colctr;
  35.400 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  35.401 +  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  35.402 +  INT32 membersum, neighsum, memberscale, neighscale;
  35.403 +  int colsum, lastcolsum, nextcolsum;
  35.404 +
  35.405 +  /* Expand input data enough to let all the output samples be generated
  35.406 +   * by the standard loop.  Special-casing padded output would be more
  35.407 +   * efficient.
  35.408 +   */
  35.409 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  35.410 +		    cinfo->image_width, output_cols);
  35.411 +
  35.412 +  /* Each of the eight neighbor pixels contributes a fraction SF to the
  35.413 +   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
  35.414 +   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
  35.415 +   * Also recall that SF = smoothing_factor / 1024.
  35.416 +   */
  35.417 +
  35.418 +  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  35.419 +  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
  35.420 +
  35.421 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  35.422 +    outptr = output_data[outrow];
  35.423 +    inptr = input_data[outrow];
  35.424 +    above_ptr = input_data[outrow-1];
  35.425 +    below_ptr = input_data[outrow+1];
  35.426 +
  35.427 +    /* Special case for first column */
  35.428 +    colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
  35.429 +	     GETJSAMPLE(*inptr);
  35.430 +    membersum = GETJSAMPLE(*inptr++);
  35.431 +    nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  35.432 +		 GETJSAMPLE(*inptr);
  35.433 +    neighsum = colsum + (colsum - membersum) + nextcolsum;
  35.434 +    membersum = membersum * memberscale + neighsum * neighscale;
  35.435 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  35.436 +    lastcolsum = colsum; colsum = nextcolsum;
  35.437 +
  35.438 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  35.439 +      membersum = GETJSAMPLE(*inptr++);
  35.440 +      above_ptr++; below_ptr++;
  35.441 +      nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  35.442 +		   GETJSAMPLE(*inptr);
  35.443 +      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
  35.444 +      membersum = membersum * memberscale + neighsum * neighscale;
  35.445 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  35.446 +      lastcolsum = colsum; colsum = nextcolsum;
  35.447 +    }
  35.448 +
  35.449 +    /* Special case for last column */
  35.450 +    membersum = GETJSAMPLE(*inptr);
  35.451 +    neighsum = lastcolsum + (colsum - membersum) + colsum;
  35.452 +    membersum = membersum * memberscale + neighsum * neighscale;
  35.453 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  35.454 +
  35.455 +  }
  35.456 +}
  35.457 +
  35.458 +#endif /* INPUT_SMOOTHING_SUPPORTED */
  35.459 +
  35.460 +
  35.461 +/*
  35.462 + * Module initialization routine for downsampling.
  35.463 + * Note that we must select a routine for each component.
  35.464 + */
  35.465 +
  35.466 +GLOBAL(void)
  35.467 +jinit_downsampler (j_compress_ptr cinfo)
  35.468 +{
  35.469 +  my_downsample_ptr downsample;
  35.470 +  int ci;
  35.471 +  jpeg_component_info * compptr;
  35.472 +  boolean smoothok = TRUE;
  35.473 +
  35.474 +  downsample = (my_downsample_ptr)
  35.475 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  35.476 +				SIZEOF(my_downsampler));
  35.477 +  cinfo->downsample = (struct jpeg_downsampler *) downsample;
  35.478 +  downsample->pub.start_pass = start_pass_downsample;
  35.479 +  downsample->pub.downsample = sep_downsample;
  35.480 +  downsample->pub.need_context_rows = FALSE;
  35.481 +
  35.482 +  if (cinfo->CCIR601_sampling)
  35.483 +    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  35.484 +
  35.485 +  /* Verify we can handle the sampling factors, and set up method pointers */
  35.486 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  35.487 +       ci++, compptr++) {
  35.488 +    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  35.489 +	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  35.490 +#ifdef INPUT_SMOOTHING_SUPPORTED
  35.491 +      if (cinfo->smoothing_factor) {
  35.492 +	downsample->methods[ci] = fullsize_smooth_downsample;
  35.493 +	downsample->pub.need_context_rows = TRUE;
  35.494 +      } else
  35.495 +#endif
  35.496 +	downsample->methods[ci] = fullsize_downsample;
  35.497 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  35.498 +	       compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  35.499 +      smoothok = FALSE;
  35.500 +      downsample->methods[ci] = h2v1_downsample;
  35.501 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  35.502 +	       compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
  35.503 +#ifdef INPUT_SMOOTHING_SUPPORTED
  35.504 +      if (cinfo->smoothing_factor) {
  35.505 +	downsample->methods[ci] = h2v2_smooth_downsample;
  35.506 +	downsample->pub.need_context_rows = TRUE;
  35.507 +      } else
  35.508 +#endif
  35.509 +	downsample->methods[ci] = h2v2_downsample;
  35.510 +    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  35.511 +	       (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
  35.512 +      smoothok = FALSE;
  35.513 +      downsample->methods[ci] = int_downsample;
  35.514 +    } else
  35.515 +      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  35.516 +  }
  35.517 +
  35.518 +#ifdef INPUT_SMOOTHING_SUPPORTED
  35.519 +  if (cinfo->smoothing_factor && !smoothok)
  35.520 +    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
  35.521 +#endif
  35.522 +}
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/libs/libjpeg/jctrans.c	Sun Jun 07 17:25:49 2015 +0300
    36.3 @@ -0,0 +1,388 @@
    36.4 +/*
    36.5 + * jctrans.c
    36.6 + *
    36.7 + * Copyright (C) 1995-1998, Thomas G. Lane.
    36.8 + * This file is part of the Independent JPEG Group's software.
    36.9 + * For conditions of distribution and use, see the accompanying README file.
   36.10 + *
   36.11 + * This file contains library routines for transcoding compression,
   36.12 + * that is, writing raw DCT coefficient arrays to an output JPEG file.
   36.13 + * The routines in jcapimin.c will also be needed by a transcoder.
   36.14 + */
   36.15 +
   36.16 +#define JPEG_INTERNALS
   36.17 +#include "jinclude.h"
   36.18 +#include "jpeglib.h"
   36.19 +
   36.20 +
   36.21 +/* Forward declarations */
   36.22 +LOCAL(void) transencode_master_selection
   36.23 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   36.24 +LOCAL(void) transencode_coef_controller
   36.25 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   36.26 +
   36.27 +
   36.28 +/*
   36.29 + * Compression initialization for writing raw-coefficient data.
   36.30 + * Before calling this, all parameters and a data destination must be set up.
   36.31 + * Call jpeg_finish_compress() to actually write the data.
   36.32 + *
   36.33 + * The number of passed virtual arrays must match cinfo->num_components.
   36.34 + * Note that the virtual arrays need not be filled or even realized at
   36.35 + * the time write_coefficients is called; indeed, if the virtual arrays
   36.36 + * were requested from this compression object's memory manager, they
   36.37 + * typically will be realized during this routine and filled afterwards.
   36.38 + */
   36.39 +
   36.40 +GLOBAL(void)
   36.41 +jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
   36.42 +{
   36.43 +  if (cinfo->global_state != CSTATE_START)
   36.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   36.45 +  /* Mark all tables to be written */
   36.46 +  jpeg_suppress_tables(cinfo, FALSE);
   36.47 +  /* (Re)initialize error mgr and destination modules */
   36.48 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   36.49 +  (*cinfo->dest->init_destination) (cinfo);
   36.50 +  /* Perform master selection of active modules */
   36.51 +  transencode_master_selection(cinfo, coef_arrays);
   36.52 +  /* Wait for jpeg_finish_compress() call */
   36.53 +  cinfo->next_scanline = 0;	/* so jpeg_write_marker works */
   36.54 +  cinfo->global_state = CSTATE_WRCOEFS;
   36.55 +}
   36.56 +
   36.57 +
   36.58 +/*
   36.59 + * Initialize the compression object with default parameters,
   36.60 + * then copy from the source object all parameters needed for lossless
   36.61 + * transcoding.  Parameters that can be varied without loss (such as
   36.62 + * scan script and Huffman optimization) are left in their default states.
   36.63 + */
   36.64 +
   36.65 +GLOBAL(void)
   36.66 +jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
   36.67 +			       j_compress_ptr dstinfo)
   36.68 +{
   36.69 +  JQUANT_TBL ** qtblptr;
   36.70 +  jpeg_component_info *incomp, *outcomp;
   36.71 +  JQUANT_TBL *c_quant, *slot_quant;
   36.72 +  int tblno, ci, coefi;
   36.73 +
   36.74 +  /* Safety check to ensure start_compress not called yet. */
   36.75 +  if (dstinfo->global_state != CSTATE_START)
   36.76 +    ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
   36.77 +  /* Copy fundamental image dimensions */
   36.78 +  dstinfo->image_width = srcinfo->image_width;
   36.79 +  dstinfo->image_height = srcinfo->image_height;
   36.80 +  dstinfo->input_components = srcinfo->num_components;
   36.81 +  dstinfo->in_color_space = srcinfo->jpeg_color_space;
   36.82 +  /* Initialize all parameters to default values */
   36.83 +  jpeg_set_defaults(dstinfo);
   36.84 +  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
   36.85 +   * Fix it to get the right header markers for the image colorspace.
   36.86 +   */
   36.87 +  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
   36.88 +  dstinfo->data_precision = srcinfo->data_precision;
   36.89 +  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
   36.90 +  /* Copy the source's quantization tables. */
   36.91 +  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
   36.92 +    if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
   36.93 +      qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
   36.94 +      if (*qtblptr == NULL)
   36.95 +	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
   36.96 +      MEMCOPY((*qtblptr)->quantval,
   36.97 +	      srcinfo->quant_tbl_ptrs[tblno]->quantval,
   36.98 +	      SIZEOF((*qtblptr)->quantval));
   36.99 +      (*qtblptr)->sent_table = FALSE;
  36.100 +    }
  36.101 +  }
  36.102 +  /* Copy the source's per-component info.
  36.103 +   * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
  36.104 +   */
  36.105 +  dstinfo->num_components = srcinfo->num_components;
  36.106 +  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
  36.107 +    ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
  36.108 +	     MAX_COMPONENTS);
  36.109 +  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
  36.110 +       ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
  36.111 +    outcomp->component_id = incomp->component_id;
  36.112 +    outcomp->h_samp_factor = incomp->h_samp_factor;
  36.113 +    outcomp->v_samp_factor = incomp->v_samp_factor;
  36.114 +    outcomp->quant_tbl_no = incomp->quant_tbl_no;
  36.115 +    /* Make sure saved quantization table for component matches the qtable
  36.116 +     * slot.  If not, the input file re-used this qtable slot.
  36.117 +     * IJG encoder currently cannot duplicate this.
  36.118 +     */
  36.119 +    tblno = outcomp->quant_tbl_no;
  36.120 +    if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
  36.121 +	srcinfo->quant_tbl_ptrs[tblno] == NULL)
  36.122 +      ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
  36.123 +    slot_quant = srcinfo->quant_tbl_ptrs[tblno];
  36.124 +    c_quant = incomp->quant_table;
  36.125 +    if (c_quant != NULL) {
  36.126 +      for (coefi = 0; coefi < DCTSIZE2; coefi++) {
  36.127 +	if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
  36.128 +	  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
  36.129 +      }
  36.130 +    }
  36.131 +    /* Note: we do not copy the source's Huffman table assignments;
  36.132 +     * instead we rely on jpeg_set_colorspace to have made a suitable choice.
  36.133 +     */
  36.134 +  }
  36.135 +  /* Also copy JFIF version and resolution information, if available.
  36.136 +   * Strictly speaking this isn't "critical" info, but it's nearly
  36.137 +   * always appropriate to copy it if available.  In particular,
  36.138 +   * if the application chooses to copy JFIF 1.02 extension markers from
  36.139 +   * the source file, we need to copy the version to make sure we don't
  36.140 +   * emit a file that has 1.02 extensions but a claimed version of 1.01.
  36.141 +   * We will *not*, however, copy version info from mislabeled "2.01" files.
  36.142 +   */
  36.143 +  if (srcinfo->saw_JFIF_marker) {
  36.144 +    if (srcinfo->JFIF_major_version == 1) {
  36.145 +      dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
  36.146 +      dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
  36.147 +    }
  36.148 +    dstinfo->density_unit = srcinfo->density_unit;
  36.149 +    dstinfo->X_density = srcinfo->X_density;
  36.150 +    dstinfo->Y_density = srcinfo->Y_density;
  36.151 +  }
  36.152 +}
  36.153 +
  36.154 +
  36.155 +/*
  36.156 + * Master selection of compression modules for transcoding.
  36.157 + * This substitutes for jcinit.c's initialization of the full compressor.
  36.158 + */
  36.159 +
  36.160 +LOCAL(void)
  36.161 +transencode_master_selection (j_compress_ptr cinfo,
  36.162 +			      jvirt_barray_ptr * coef_arrays)
  36.163 +{
  36.164 +  /* Although we don't actually use input_components for transcoding,
  36.165 +   * jcmaster.c's initial_setup will complain if input_components is 0.
  36.166 +   */
  36.167 +  cinfo->input_components = 1;
  36.168 +  /* Initialize master control (includes parameter checking/processing) */
  36.169 +  jinit_c_master_control(cinfo, TRUE /* transcode only */);
  36.170 +
  36.171 +  /* Entropy encoding: either Huffman or arithmetic coding. */
  36.172 +  if (cinfo->arith_code) {
  36.173 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  36.174 +  } else {
  36.175 +    if (cinfo->progressive_mode) {
  36.176 +#ifdef C_PROGRESSIVE_SUPPORTED
  36.177 +      jinit_phuff_encoder(cinfo);
  36.178 +#else
  36.179 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  36.180 +#endif
  36.181 +    } else
  36.182 +      jinit_huff_encoder(cinfo);
  36.183 +  }
  36.184 +
  36.185 +  /* We need a special coefficient buffer controller. */
  36.186 +  transencode_coef_controller(cinfo, coef_arrays);
  36.187 +
  36.188 +  jinit_marker_writer(cinfo);
  36.189 +
  36.190 +  /* We can now tell the memory manager to allocate virtual arrays. */
  36.191 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  36.192 +
  36.193 +  /* Write the datastream header (SOI, JFIF) immediately.
  36.194 +   * Frame and scan headers are postponed till later.
  36.195 +   * This lets application insert special markers after the SOI.
  36.196 +   */
  36.197 +  (*cinfo->marker->write_file_header) (cinfo);
  36.198 +}
  36.199 +
  36.200 +
  36.201 +/*
  36.202 + * The rest of this file is a special implementation of the coefficient
  36.203 + * buffer controller.  This is similar to jccoefct.c, but it handles only
  36.204 + * output from presupplied virtual arrays.  Furthermore, we generate any
  36.205 + * dummy padding blocks on-the-fly rather than expecting them to be present
  36.206 + * in the arrays.
  36.207 + */
  36.208 +
  36.209 +/* Private buffer controller object */
  36.210 +
  36.211 +typedef struct {
  36.212 +  struct jpeg_c_coef_controller pub; /* public fields */
  36.213 +
  36.214 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
  36.215 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
  36.216 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  36.217 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
  36.218 +
  36.219 +  /* Virtual block array for each component. */
  36.220 +  jvirt_barray_ptr * whole_image;
  36.221 +
  36.222 +  /* Workspace for constructing dummy blocks at right/bottom edges. */
  36.223 +  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
  36.224 +} my_coef_controller;
  36.225 +
  36.226 +typedef my_coef_controller * my_coef_ptr;
  36.227 +
  36.228 +
  36.229 +LOCAL(void)
  36.230 +start_iMCU_row (j_compress_ptr cinfo)
  36.231 +/* Reset within-iMCU-row counters for a new row */
  36.232 +{
  36.233 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  36.234 +
  36.235 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
  36.236 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  36.237 +   * But at the bottom of the image, process only what's left.
  36.238 +   */
  36.239 +  if (cinfo->comps_in_scan > 1) {
  36.240 +    coef->MCU_rows_per_iMCU_row = 1;
  36.241 +  } else {
  36.242 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  36.243 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  36.244 +    else
  36.245 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  36.246 +  }
  36.247 +
  36.248 +  coef->mcu_ctr = 0;
  36.249 +  coef->MCU_vert_offset = 0;
  36.250 +}
  36.251 +
  36.252 +
  36.253 +/*
  36.254 + * Initialize for a processing pass.
  36.255 + */
  36.256 +
  36.257 +METHODDEF(void)
  36.258 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  36.259 +{
  36.260 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  36.261 +
  36.262 +  if (pass_mode != JBUF_CRANK_DEST)
  36.263 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  36.264 +
  36.265 +  coef->iMCU_row_num = 0;
  36.266 +  start_iMCU_row(cinfo);
  36.267 +}
  36.268 +
  36.269 +
  36.270 +/*
  36.271 + * Process some data.
  36.272 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  36.273 + * per call, ie, v_samp_factor block rows for each component in the scan.
  36.274 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  36.275 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  36.276 + *
  36.277 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  36.278 + */
  36.279 +
  36.280 +METHODDEF(boolean)
  36.281 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  36.282 +{
  36.283 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  36.284 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  36.285 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  36.286 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  36.287 +  int blkn, ci, xindex, yindex, yoffset, blockcnt;
  36.288 +  JDIMENSION start_col;
  36.289 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  36.290 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  36.291 +  JBLOCKROW buffer_ptr;
  36.292 +  jpeg_component_info *compptr;
  36.293 +
  36.294 +  /* Align the virtual buffers for the components used in this scan. */
  36.295 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  36.296 +    compptr = cinfo->cur_comp_info[ci];
  36.297 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  36.298 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  36.299 +       coef->iMCU_row_num * compptr->v_samp_factor,
  36.300 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  36.301 +  }
  36.302 +
  36.303 +  /* Loop to process one whole iMCU row */
  36.304 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  36.305 +       yoffset++) {
  36.306 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  36.307 +	 MCU_col_num++) {
  36.308 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  36.309 +      blkn = 0;			/* index of current DCT block within MCU */
  36.310 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  36.311 +	compptr = cinfo->cur_comp_info[ci];
  36.312 +	start_col = MCU_col_num * compptr->MCU_width;
  36.313 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  36.314 +						: compptr->last_col_width;
  36.315 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  36.316 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  36.317 +	      yindex+yoffset < compptr->last_row_height) {
  36.318 +	    /* Fill in pointers to real blocks in this row */
  36.319 +	    buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  36.320 +	    for (xindex = 0; xindex < blockcnt; xindex++)
  36.321 +	      MCU_buffer[blkn++] = buffer_ptr++;
  36.322 +	  } else {
  36.323 +	    /* At bottom of image, need a whole row of dummy blocks */
  36.324 +	    xindex = 0;
  36.325 +	  }
  36.326 +	  /* Fill in any dummy blocks needed in this row.
  36.327 +	   * Dummy blocks are filled in the same way as in jccoefct.c:
  36.328 +	   * all zeroes in the AC entries, DC entries equal to previous
  36.329 +	   * block's DC value.  The init routine has already zeroed the
  36.330 +	   * AC entries, so we need only set the DC entries correctly.
  36.331 +	   */
  36.332 +	  for (; xindex < compptr->MCU_width; xindex++) {
  36.333 +	    MCU_buffer[blkn] = coef->dummy_buffer[blkn];
  36.334 +	    MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
  36.335 +	    blkn++;
  36.336 +	  }
  36.337 +	}
  36.338 +      }
  36.339 +      /* Try to write the MCU. */
  36.340 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
  36.341 +	/* Suspension forced; update state counters and exit */
  36.342 +	coef->MCU_vert_offset = yoffset;
  36.343 +	coef->mcu_ctr = MCU_col_num;
  36.344 +	return FALSE;
  36.345 +      }
  36.346 +    }
  36.347 +    /* Completed an MCU row, but perhaps not an iMCU row */
  36.348 +    coef->mcu_ctr = 0;
  36.349 +  }
  36.350 +  /* Completed the iMCU row, advance counters for next one */
  36.351 +  coef->iMCU_row_num++;
  36.352 +  start_iMCU_row(cinfo);
  36.353 +  return TRUE;
  36.354 +}
  36.355 +
  36.356 +
  36.357 +/*
  36.358 + * Initialize coefficient buffer controller.
  36.359 + *
  36.360 + * Each passed coefficient array must be the right size for that
  36.361 + * coefficient: width_in_blocks wide and height_in_blocks high,
  36.362 + * with unitheight at least v_samp_factor.
  36.363 + */
  36.364 +
  36.365 +LOCAL(void)
  36.366 +transencode_coef_controller (j_compress_ptr cinfo,
  36.367 +			     jvirt_barray_ptr * coef_arrays)
  36.368 +{
  36.369 +  my_coef_ptr coef;
  36.370 +  JBLOCKROW buffer;
  36.371 +  int i;
  36.372 +
  36.373 +  coef = (my_coef_ptr)
  36.374 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  36.375 +				SIZEOF(my_coef_controller));
  36.376 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  36.377 +  coef->pub.start_pass = start_pass_coef;
  36.378 +  coef->pub.compress_data = compress_output;
  36.379 +
  36.380 +  /* Save pointer to virtual arrays */
  36.381 +  coef->whole_image = coef_arrays;
  36.382 +
  36.383 +  /* Allocate and pre-zero space for dummy DCT blocks. */
  36.384 +  buffer = (JBLOCKROW)
  36.385 +    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  36.386 +				C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  36.387 +  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  36.388 +  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  36.389 +    coef->dummy_buffer[i] = buffer + i;
  36.390 +  }
  36.391 +}
    37.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.2 +++ b/libs/libjpeg/jdapimin.c	Sun Jun 07 17:25:49 2015 +0300
    37.3 @@ -0,0 +1,395 @@
    37.4 +/*
    37.5 + * jdapimin.c
    37.6 + *
    37.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    37.8 + * This file is part of the Independent JPEG Group's software.
    37.9 + * For conditions of distribution and use, see the accompanying README file.
   37.10 + *
   37.11 + * This file contains application interface code for the decompression half
   37.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   37.13 + * needed in either the normal full-decompression case or the
   37.14 + * transcoding-only case.
   37.15 + *
   37.16 + * Most of the routines intended to be called directly by an application
   37.17 + * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
   37.18 + * shared by compression and decompression, and jdtrans.c for the transcoding
   37.19 + * case.
   37.20 + */
   37.21 +
   37.22 +#define JPEG_INTERNALS
   37.23 +#include "jinclude.h"
   37.24 +#include "jpeglib.h"
   37.25 +
   37.26 +
   37.27 +/*
   37.28 + * Initialization of a JPEG decompression object.
   37.29 + * The error manager must already be set up (in case memory manager fails).
   37.30 + */
   37.31 +
   37.32 +GLOBAL(void)
   37.33 +jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
   37.34 +{
   37.35 +  int i;
   37.36 +
   37.37 +  /* Guard against version mismatches between library and caller. */
   37.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   37.39 +  if (version != JPEG_LIB_VERSION)
   37.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   37.41 +  if (structsize != SIZEOF(struct jpeg_decompress_struct))
   37.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   37.43 +	     (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
   37.44 +
   37.45 +  /* For debugging purposes, we zero the whole master structure.
   37.46 +   * But the application has already set the err pointer, and may have set
   37.47 +   * client_data, so we have to save and restore those fields.
   37.48 +   * Note: if application hasn't set client_data, tools like Purify may
   37.49 +   * complain here.
   37.50 +   */
   37.51 +  {
   37.52 +    struct jpeg_error_mgr * err = cinfo->err;
   37.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   37.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
   37.55 +    cinfo->err = err;
   37.56 +    cinfo->client_data = client_data;
   37.57 +  }
   37.58 +  cinfo->is_decompressor = TRUE;
   37.59 +
   37.60 +  /* Initialize a memory manager instance for this object */
   37.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   37.62 +
   37.63 +  /* Zero out pointers to permanent structures. */
   37.64 +  cinfo->progress = NULL;
   37.65 +  cinfo->src = NULL;
   37.66 +
   37.67 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   37.68 +    cinfo->quant_tbl_ptrs[i] = NULL;
   37.69 +
   37.70 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   37.71 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   37.72 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   37.73 +  }
   37.74 +
   37.75 +  /* Initialize marker processor so application can override methods
   37.76 +   * for COM, APPn markers before calling jpeg_read_header.
   37.77 +   */
   37.78 +  cinfo->marker_list = NULL;
   37.79 +  jinit_marker_reader(cinfo);
   37.80 +
   37.81 +  /* And initialize the overall input controller. */
   37.82 +  jinit_input_controller(cinfo);
   37.83 +
   37.84 +  /* OK, I'm ready */
   37.85 +  cinfo->global_state = DSTATE_START;
   37.86 +}
   37.87 +
   37.88 +
   37.89 +/*
   37.90 + * Destruction of a JPEG decompression object
   37.91 + */
   37.92 +
   37.93 +GLOBAL(void)
   37.94 +jpeg_destroy_decompress (j_decompress_ptr cinfo)
   37.95 +{
   37.96 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   37.97 +}
   37.98 +
   37.99 +
  37.100 +/*
  37.101 + * Abort processing of a JPEG decompression operation,
  37.102 + * but don't destroy the object itself.
  37.103 + */
  37.104 +
  37.105 +GLOBAL(void)
  37.106 +jpeg_abort_decompress (j_decompress_ptr cinfo)
  37.107 +{
  37.108 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  37.109 +}
  37.110 +
  37.111 +
  37.112 +/*
  37.113 + * Set default decompression parameters.
  37.114 + */
  37.115 +
  37.116 +LOCAL(void)
  37.117 +default_decompress_parms (j_decompress_ptr cinfo)
  37.118 +{
  37.119 +  /* Guess the input colorspace, and set output colorspace accordingly. */
  37.120 +  /* (Wish JPEG committee had provided a real way to specify this...) */
  37.121 +  /* Note application may override our guesses. */
  37.122 +  switch (cinfo->num_components) {
  37.123 +  case 1:
  37.124 +    cinfo->jpeg_color_space = JCS_GRAYSCALE;
  37.125 +    cinfo->out_color_space = JCS_GRAYSCALE;
  37.126 +    break;
  37.127 +    
  37.128 +  case 3:
  37.129 +    if (cinfo->saw_JFIF_marker) {
  37.130 +      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  37.131 +    } else if (cinfo->saw_Adobe_marker) {
  37.132 +      switch (cinfo->Adobe_transform) {
  37.133 +      case 0:
  37.134 +	cinfo->jpeg_color_space = JCS_RGB;
  37.135 +	break;
  37.136 +      case 1:
  37.137 +	cinfo->jpeg_color_space = JCS_YCbCr;
  37.138 +	break;
  37.139 +      default:
  37.140 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  37.141 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  37.142 +	break;
  37.143 +      }
  37.144 +    } else {
  37.145 +      /* Saw no special markers, try to guess from the component IDs */
  37.146 +      int cid0 = cinfo->comp_info[0].component_id;
  37.147 +      int cid1 = cinfo->comp_info[1].component_id;
  37.148 +      int cid2 = cinfo->comp_info[2].component_id;
  37.149 +
  37.150 +      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  37.151 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  37.152 +      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  37.153 +	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  37.154 +      else {
  37.155 +	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  37.156 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  37.157 +      }
  37.158 +    }
  37.159 +    /* Always guess RGB is proper output colorspace. */
  37.160 +    cinfo->out_color_space = JCS_RGB;
  37.161 +    break;
  37.162 +    
  37.163 +  case 4:
  37.164 +    if (cinfo->saw_Adobe_marker) {
  37.165 +      switch (cinfo->Adobe_transform) {
  37.166 +      case 0:
  37.167 +	cinfo->jpeg_color_space = JCS_CMYK;
  37.168 +	break;
  37.169 +      case 2:
  37.170 +	cinfo->jpeg_color_space = JCS_YCCK;
  37.171 +	break;
  37.172 +      default:
  37.173 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  37.174 +	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  37.175 +	break;
  37.176 +      }
  37.177 +    } else {
  37.178 +      /* No special markers, assume straight CMYK. */
  37.179 +      cinfo->jpeg_color_space = JCS_CMYK;
  37.180 +    }
  37.181 +    cinfo->out_color_space = JCS_CMYK;
  37.182 +    break;
  37.183 +    
  37.184 +  default:
  37.185 +    cinfo->jpeg_color_space = JCS_UNKNOWN;
  37.186 +    cinfo->out_color_space = JCS_UNKNOWN;
  37.187 +    break;
  37.188 +  }
  37.189 +
  37.190 +  /* Set defaults for other decompression parameters. */
  37.191 +  cinfo->scale_num = 1;		/* 1:1 scaling */
  37.192 +  cinfo->scale_denom = 1;
  37.193 +  cinfo->output_gamma = 1.0;
  37.194 +  cinfo->buffered_image = FALSE;
  37.195 +  cinfo->raw_data_out = FALSE;
  37.196 +  cinfo->dct_method = JDCT_DEFAULT;
  37.197 +  cinfo->do_fancy_upsampling = TRUE;
  37.198 +  cinfo->do_block_smoothing = TRUE;
  37.199 +  cinfo->quantize_colors = FALSE;
  37.200 +  /* We set these in case application only sets quantize_colors. */
  37.201 +  cinfo->dither_mode = JDITHER_FS;
  37.202 +#ifdef QUANT_2PASS_SUPPORTED
  37.203 +  cinfo->two_pass_quantize = TRUE;
  37.204 +#else
  37.205 +  cinfo->two_pass_quantize = FALSE;
  37.206 +#endif
  37.207 +  cinfo->desired_number_of_colors = 256;
  37.208 +  cinfo->colormap = NULL;
  37.209 +  /* Initialize for no mode change in buffered-image mode. */
  37.210 +  cinfo->enable_1pass_quant = FALSE;
  37.211 +  cinfo->enable_external_quant = FALSE;
  37.212 +  cinfo->enable_2pass_quant = FALSE;
  37.213 +}
  37.214 +
  37.215 +
  37.216 +/*
  37.217 + * Decompression startup: read start of JPEG datastream to see what's there.
  37.218 + * Need only initialize JPEG object and supply a data source before calling.
  37.219 + *
  37.220 + * This routine will read as far as the first SOS marker (ie, actual start of
  37.221 + * compressed data), and will save all tables and parameters in the JPEG
  37.222 + * object.  It will also initialize the decompression parameters to default
  37.223 + * values, and finally return JPEG_HEADER_OK.  On return, the application may
  37.224 + * adjust the decompression parameters and then call jpeg_start_decompress.
  37.225 + * (Or, if the application only wanted to determine the image parameters,
  37.226 + * the data need not be decompressed.  In that case, call jpeg_abort or
  37.227 + * jpeg_destroy to release any temporary space.)
  37.228 + * If an abbreviated (tables only) datastream is presented, the routine will
  37.229 + * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  37.230 + * re-use the JPEG object to read the abbreviated image datastream(s).
  37.231 + * It is unnecessary (but OK) to call jpeg_abort in this case.
  37.232 + * The JPEG_SUSPENDED return code only occurs if the data source module
  37.233 + * requests suspension of the decompressor.  In this case the application
  37.234 + * should load more source data and then re-call jpeg_read_header to resume
  37.235 + * processing.
  37.236 + * If a non-suspending data source is used and require_image is TRUE, then the
  37.237 + * return code need not be inspected since only JPEG_HEADER_OK is possible.
  37.238 + *
  37.239 + * This routine is now just a front end to jpeg_consume_input, with some
  37.240 + * extra error checking.
  37.241 + */
  37.242 +
  37.243 +GLOBAL(int)
  37.244 +jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  37.245 +{
  37.246 +  int retcode;
  37.247 +
  37.248 +  if (cinfo->global_state != DSTATE_START &&
  37.249 +      cinfo->global_state != DSTATE_INHEADER)
  37.250 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  37.251 +
  37.252 +  retcode = jpeg_consume_input(cinfo);
  37.253 +
  37.254 +  switch (retcode) {
  37.255 +  case JPEG_REACHED_SOS:
  37.256 +    retcode = JPEG_HEADER_OK;
  37.257 +    break;
  37.258 +  case JPEG_REACHED_EOI:
  37.259 +    if (require_image)		/* Complain if application wanted an image */
  37.260 +      ERREXIT(cinfo, JERR_NO_IMAGE);
  37.261 +    /* Reset to start state; it would be safer to require the application to
  37.262 +     * call jpeg_abort, but we can't change it now for compatibility reasons.
  37.263 +     * A side effect is to free any temporary memory (there shouldn't be any).
  37.264 +     */
  37.265 +    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  37.266 +    retcode = JPEG_HEADER_TABLES_ONLY;
  37.267 +    break;
  37.268 +  case JPEG_SUSPENDED:
  37.269 +    /* no work */
  37.270 +    break;
  37.271 +  }
  37.272 +
  37.273 +  return retcode;
  37.274 +}
  37.275 +
  37.276 +
  37.277 +/*
  37.278 + * Consume data in advance of what the decompressor requires.
  37.279 + * This can be called at any time once the decompressor object has
  37.280 + * been created and a data source has been set up.
  37.281 + *
  37.282 + * This routine is essentially a state machine that handles a couple
  37.283 + * of critical state-transition actions, namely initial setup and
  37.284 + * transition from header scanning to ready-for-start_decompress.
  37.285 + * All the actual input is done via the input controller's consume_input
  37.286 + * method.
  37.287 + */
  37.288 +
  37.289 +GLOBAL(int)
  37.290 +jpeg_consume_input (j_decompress_ptr cinfo)
  37.291 +{
  37.292 +  int retcode = JPEG_SUSPENDED;
  37.293 +
  37.294 +  /* NB: every possible DSTATE value should be listed in this switch */
  37.295 +  switch (cinfo->global_state) {
  37.296 +  case DSTATE_START:
  37.297 +    /* Start-of-datastream actions: reset appropriate modules */
  37.298 +    (*cinfo->inputctl->reset_input_controller) (cinfo);
  37.299 +    /* Initialize application's data source module */
  37.300 +    (*cinfo->src->init_source) (cinfo);
  37.301 +    cinfo->global_state = DSTATE_INHEADER;
  37.302 +    /*FALLTHROUGH*/
  37.303 +  case DSTATE_INHEADER:
  37.304 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
  37.305 +    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  37.306 +      /* Set up default parameters based on header data */
  37.307 +      default_decompress_parms(cinfo);
  37.308 +      /* Set global state: ready for start_decompress */
  37.309 +      cinfo->global_state = DSTATE_READY;
  37.310 +    }
  37.311 +    break;
  37.312 +  case DSTATE_READY:
  37.313 +    /* Can't advance past first SOS until start_decompress is called */
  37.314 +    retcode = JPEG_REACHED_SOS;
  37.315 +    break;
  37.316 +  case DSTATE_PRELOAD:
  37.317 +  case DSTATE_PRESCAN:
  37.318 +  case DSTATE_SCANNING:
  37.319 +  case DSTATE_RAW_OK:
  37.320 +  case DSTATE_BUFIMAGE:
  37.321 +  case DSTATE_BUFPOST:
  37.322 +  case DSTATE_STOPPING:
  37.323 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
  37.324 +    break;
  37.325 +  default:
  37.326 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  37.327 +  }
  37.328 +  return retcode;
  37.329 +}
  37.330 +
  37.331 +
  37.332 +/*
  37.333 + * Have we finished reading the input file?
  37.334 + */
  37.335 +
  37.336 +GLOBAL(boolean)
  37.337 +jpeg_input_complete (j_decompress_ptr cinfo)
  37.338 +{
  37.339 +  /* Check for valid jpeg object */
  37.340 +  if (cinfo->global_state < DSTATE_START ||
  37.341 +      cinfo->global_state > DSTATE_STOPPING)
  37.342 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  37.343 +  return cinfo->inputctl->eoi_reached;
  37.344 +}
  37.345 +
  37.346 +
  37.347 +/*
  37.348 + * Is there more than one scan?
  37.349 + */
  37.350 +
  37.351 +GLOBAL(boolean)
  37.352 +jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  37.353 +{
  37.354 +  /* Only valid after jpeg_read_header completes */
  37.355 +  if (cinfo->global_state < DSTATE_READY ||
  37.356 +      cinfo->global_state > DSTATE_STOPPING)
  37.357 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  37.358 +  return cinfo->inputctl->has_multiple_scans;
  37.359 +}
  37.360 +
  37.361 +
  37.362 +/*
  37.363 + * Finish JPEG decompression.
  37.364 + *
  37.365 + * This will normally just verify the file trailer and release temp storage.
  37.366 + *
  37.367 + * Returns FALSE if suspended.  The return value need be inspected only if
  37.368 + * a suspending data source is used.
  37.369 + */
  37.370 +
  37.371 +GLOBAL(boolean)
  37.372 +jpeg_finish_decompress (j_decompress_ptr cinfo)
  37.373 +{
  37.374 +  if ((cinfo->global_state == DSTATE_SCANNING ||
  37.375 +       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  37.376 +    /* Terminate final pass of non-buffered mode */
  37.377 +    if (cinfo->output_scanline < cinfo->output_height)
  37.378 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  37.379 +    (*cinfo->master->finish_output_pass) (cinfo);
  37.380 +    cinfo->global_state = DSTATE_STOPPING;
  37.381 +  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  37.382 +    /* Finishing after a buffered-image operation */
  37.383 +    cinfo->global_state = DSTATE_STOPPING;
  37.384 +  } else if (cinfo->global_state != DSTATE_STOPPING) {
  37.385 +    /* STOPPING = repeat call after a suspension, anything else is error */
  37.386 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  37.387 +  }
  37.388 +  /* Read until EOI */
  37.389 +  while (! cinfo->inputctl->eoi_reached) {
  37.390 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  37.391 +      return FALSE;		/* Suspend, come back later */
  37.392 +  }
  37.393 +  /* Do final cleanup */
  37.394 +  (*cinfo->src->term_source) (cinfo);
  37.395 +  /* We can use jpeg_abort to release memory and reset global_state */
  37.396 +  jpeg_abort((j_common_ptr) cinfo);
  37.397 +  return TRUE;
  37.398 +}
    38.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.2 +++ b/libs/libjpeg/jdapistd.c	Sun Jun 07 17:25:49 2015 +0300
    38.3 @@ -0,0 +1,275 @@
    38.4 +/*
    38.5 + * jdapistd.c
    38.6 + *
    38.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    38.8 + * This file is part of the Independent JPEG Group's software.
    38.9 + * For conditions of distribution and use, see the accompanying README file.
   38.10 + *
   38.11 + * This file contains application interface code for the decompression half
   38.12 + * of the JPEG library.  These are the "standard" API routines that are
   38.13 + * used in the normal full-decompression case.  They are not used by a
   38.14 + * transcoding-only application.  Note that if an application links in
   38.15 + * jpeg_start_decompress, it will end up linking in the entire decompressor.
   38.16 + * We thus must separate this file from jdapimin.c to avoid linking the
   38.17 + * whole decompression library into a transcoder.
   38.18 + */
   38.19 +
   38.20 +#define JPEG_INTERNALS
   38.21 +#include "jinclude.h"
   38.22 +#include "jpeglib.h"
   38.23 +
   38.24 +
   38.25 +/* Forward declarations */
   38.26 +LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
   38.27 +
   38.28 +
   38.29 +/*
   38.30 + * Decompression initialization.
   38.31 + * jpeg_read_header must be completed before calling this.
   38.32 + *
   38.33 + * If a multipass operating mode was selected, this will do all but the
   38.34 + * last pass, and thus may take a great deal of time.
   38.35 + *
   38.36 + * Returns FALSE if suspended.  The return value need be inspected only if
   38.37 + * a suspending data source is used.
   38.38 + */
   38.39 +
   38.40 +GLOBAL(boolean)
   38.41 +jpeg_start_decompress (j_decompress_ptr cinfo)
   38.42 +{
   38.43 +  if (cinfo->global_state == DSTATE_READY) {
   38.44 +    /* First call: initialize master control, select active modules */
   38.45 +    jinit_master_decompress(cinfo);
   38.46 +    if (cinfo->buffered_image) {
   38.47 +      /* No more work here; expecting jpeg_start_output next */
   38.48 +      cinfo->global_state = DSTATE_BUFIMAGE;
   38.49 +      return TRUE;
   38.50 +    }
   38.51 +    cinfo->global_state = DSTATE_PRELOAD;
   38.52 +  }
   38.53 +  if (cinfo->global_state == DSTATE_PRELOAD) {
   38.54 +    /* If file has multiple scans, absorb them all into the coef buffer */
   38.55 +    if (cinfo->inputctl->has_multiple_scans) {
   38.56 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   38.57 +      for (;;) {
   38.58 +	int retcode;
   38.59 +	/* Call progress monitor hook if present */
   38.60 +	if (cinfo->progress != NULL)
   38.61 +	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   38.62 +	/* Absorb some more input */
   38.63 +	retcode = (*cinfo->inputctl->consume_input) (cinfo);
   38.64 +	if (retcode == JPEG_SUSPENDED)
   38.65 +	  return FALSE;
   38.66 +	if (retcode == JPEG_REACHED_EOI)
   38.67 +	  break;
   38.68 +	/* Advance progress counter if appropriate */
   38.69 +	if (cinfo->progress != NULL &&
   38.70 +	    (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
   38.71 +	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
   38.72 +	    /* jdmaster underestimated number of scans; ratchet up one scan */
   38.73 +	    cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
   38.74 +	  }
   38.75 +	}
   38.76 +      }
   38.77 +#else
   38.78 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   38.79 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   38.80 +    }
   38.81 +    cinfo->output_scan_number = cinfo->input_scan_number;
   38.82 +  } else if (cinfo->global_state != DSTATE_PRESCAN)
   38.83 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   38.84 +  /* Perform any dummy output passes, and set up for the final pass */
   38.85 +  return output_pass_setup(cinfo);
   38.86 +}
   38.87 +
   38.88 +
   38.89 +/*
   38.90 + * Set up for an output pass, and perform any dummy pass(es) needed.
   38.91 + * Common subroutine for jpeg_start_decompress and jpeg_start_output.
   38.92 + * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
   38.93 + * Exit: If done, returns TRUE and sets global_state for proper output mode.
   38.94 + *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
   38.95 + */
   38.96 +
   38.97 +LOCAL(boolean)
   38.98 +output_pass_setup (j_decompress_ptr cinfo)
   38.99 +{
  38.100 +  if (cinfo->global_state != DSTATE_PRESCAN) {
  38.101 +    /* First call: do pass setup */
  38.102 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
  38.103 +    cinfo->output_scanline = 0;
  38.104 +    cinfo->global_state = DSTATE_PRESCAN;
  38.105 +  }
  38.106 +  /* Loop over any required dummy passes */
  38.107 +  while (cinfo->master->is_dummy_pass) {
  38.108 +#ifdef QUANT_2PASS_SUPPORTED
  38.109 +    /* Crank through the dummy pass */
  38.110 +    while (cinfo->output_scanline < cinfo->output_height) {
  38.111 +      JDIMENSION last_scanline;
  38.112 +      /* Call progress monitor hook if present */
  38.113 +      if (cinfo->progress != NULL) {
  38.114 +	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  38.115 +	cinfo->progress->pass_limit = (long) cinfo->output_height;
  38.116 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  38.117 +      }
  38.118 +      /* Process some data */
  38.119 +      last_scanline = cinfo->output_scanline;
  38.120 +      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  38.121 +				    &cinfo->output_scanline, (JDIMENSION) 0);
  38.122 +      if (cinfo->output_scanline == last_scanline)
  38.123 +	return FALSE;		/* No progress made, must suspend */
  38.124 +    }
  38.125 +    /* Finish up dummy pass, and set up for another one */
  38.126 +    (*cinfo->master->finish_output_pass) (cinfo);
  38.127 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
  38.128 +    cinfo->output_scanline = 0;
  38.129 +#else
  38.130 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  38.131 +#endif /* QUANT_2PASS_SUPPORTED */
  38.132 +  }
  38.133 +  /* Ready for application to drive output pass through
  38.134 +   * jpeg_read_scanlines or jpeg_read_raw_data.
  38.135 +   */
  38.136 +  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
  38.137 +  return TRUE;
  38.138 +}
  38.139 +
  38.140 +
  38.141 +/*
  38.142 + * Read some scanlines of data from the JPEG decompressor.
  38.143 + *
  38.144 + * The return value will be the number of lines actually read.
  38.145 + * This may be less than the number requested in several cases,
  38.146 + * including bottom of image, data source suspension, and operating
  38.147 + * modes that emit multiple scanlines at a time.
  38.148 + *
  38.149 + * Note: we warn about excess calls to jpeg_read_scanlines() since
  38.150 + * this likely signals an application programmer error.  However,
  38.151 + * an oversize buffer (max_lines > scanlines remaining) is not an error.
  38.152 + */
  38.153 +
  38.154 +GLOBAL(JDIMENSION)
  38.155 +jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  38.156 +		     JDIMENSION max_lines)
  38.157 +{
  38.158 +  JDIMENSION row_ctr;
  38.159 +
  38.160 +  if (cinfo->global_state != DSTATE_SCANNING)
  38.161 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  38.162 +  if (cinfo->output_scanline >= cinfo->output_height) {
  38.163 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  38.164 +    return 0;
  38.165 +  }
  38.166 +
  38.167 +  /* Call progress monitor hook if present */
  38.168 +  if (cinfo->progress != NULL) {
  38.169 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  38.170 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
  38.171 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  38.172 +  }
  38.173 +
  38.174 +  /* Process some data */
  38.175 +  row_ctr = 0;
  38.176 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  38.177 +  cinfo->output_scanline += row_ctr;
  38.178 +  return row_ctr;
  38.179 +}
  38.180 +
  38.181 +
  38.182 +/*
  38.183 + * Alternate entry point to read raw data.
  38.184 + * Processes exactly one iMCU row per call, unless suspended.
  38.185 + */
  38.186 +
  38.187 +GLOBAL(JDIMENSION)
  38.188 +jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
  38.189 +		    JDIMENSION max_lines)
  38.190 +{
  38.191 +  JDIMENSION lines_per_iMCU_row;
  38.192 +
  38.193 +  if (cinfo->global_state != DSTATE_RAW_OK)
  38.194 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  38.195 +  if (cinfo->output_scanline >= cinfo->output_height) {
  38.196 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  38.197 +    return 0;
  38.198 +  }
  38.199 +
  38.200 +  /* Call progress monitor hook if present */
  38.201 +  if (cinfo->progress != NULL) {
  38.202 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  38.203 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
  38.204 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  38.205 +  }
  38.206 +
  38.207 +  /* Verify that at least one iMCU row can be returned. */
  38.208 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
  38.209 +  if (max_lines < lines_per_iMCU_row)
  38.210 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
  38.211 +
  38.212 +  /* Decompress directly into user's buffer. */
  38.213 +  if (! (*cinfo->coef->decompress_data) (cinfo, data))
  38.214 +    return 0;			/* suspension forced, can do nothing more */
  38.215 +
  38.216 +  /* OK, we processed one iMCU row. */
  38.217 +  cinfo->output_scanline += lines_per_iMCU_row;
  38.218 +  return lines_per_iMCU_row;
  38.219 +}
  38.220 +
  38.221 +
  38.222 +/* Additional entry points for buffered-image mode. */
  38.223 +
  38.224 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  38.225 +
  38.226 +/*
  38.227 + * Initialize for an output pass in buffered-image mode.
  38.228 + */
  38.229 +
  38.230 +GLOBAL(boolean)
  38.231 +jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
  38.232 +{
  38.233 +  if (cinfo->global_state != DSTATE_BUFIMAGE &&
  38.234 +      cinfo->global_state != DSTATE_PRESCAN)
  38.235 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  38.236 +  /* Limit scan number to valid range */
  38.237 +  if (scan_number <= 0)
  38.238 +    scan_number = 1;
  38.239 +  if (cinfo->inputctl->eoi_reached &&
  38.240 +      scan_number > cinfo->input_scan_number)
  38.241 +    scan_number = cinfo->input_scan_number;
  38.242 +  cinfo->output_scan_number = scan_number;
  38.243 +  /* Perform any dummy output passes, and set up for the real pass */
  38.244 +  return output_pass_setup(cinfo);
  38.245 +}
  38.246 +
  38.247 +
  38.248 +/*
  38.249 + * Finish up after an output pass in buffered-image mode.
  38.250 + *
  38.251 + * Returns FALSE if suspended.  The return value need be inspected only if
  38.252 + * a suspending data source is used.
  38.253 + */
  38.254 +
  38.255 +GLOBAL(boolean)
  38.256 +jpeg_finish_output (j_decompress_ptr cinfo)
  38.257 +{
  38.258 +  if ((cinfo->global_state == DSTATE_SCANNING ||
  38.259 +       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  38.260 +    /* Terminate this pass. */
  38.261 +    /* We do not require the whole pass to have been completed. */
  38.262 +    (*cinfo->master->finish_output_pass) (cinfo);
  38.263 +    cinfo->global_state = DSTATE_BUFPOST;
  38.264 +  } else if (cinfo->global_state != DSTATE_BUFPOST) {
  38.265 +    /* BUFPOST = repeat call after a suspension, anything else is error */
  38.266 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  38.267 +  }
  38.268 +  /* Read markers looking for SOS or EOI */
  38.269 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  38.270 +	 ! cinfo->inputctl->eoi_reached) {
  38.271 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  38.272 +      return FALSE;		/* Suspend, come back later */
  38.273 +  }
  38.274 +  cinfo->global_state = DSTATE_BUFIMAGE;
  38.275 +  return TRUE;
  38.276 +}
  38.277 +
  38.278 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
    39.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.2 +++ b/libs/libjpeg/jdatadst.c	Sun Jun 07 17:25:49 2015 +0300
    39.3 @@ -0,0 +1,151 @@
    39.4 +/*
    39.5 + * jdatadst.c
    39.6 + *
    39.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    39.8 + * This file is part of the Independent JPEG Group's software.
    39.9 + * For conditions of distribution and use, see the accompanying README file.
   39.10 + *
   39.11 + * This file contains compression data destination routines for the case of
   39.12 + * emitting JPEG data to a file (or any stdio stream).  While these routines
   39.13 + * are sufficient for most applications, some will want to use a different
   39.14 + * destination manager.
   39.15 + * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
   39.16 + * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
   39.17 + * than 8 bits on your machine, you may need to do some tweaking.
   39.18 + */
   39.19 +
   39.20 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   39.21 +#include "jinclude.h"
   39.22 +#include "jpeglib.h"
   39.23 +#include "jerror.h"
   39.24 +
   39.25 +
   39.26 +/* Expanded data destination object for stdio output */
   39.27 +
   39.28 +typedef struct {
   39.29 +  struct jpeg_destination_mgr pub; /* public fields */
   39.30 +
   39.31 +  FILE * outfile;		/* target stream */
   39.32 +  JOCTET * buffer;		/* start of buffer */
   39.33 +} my_destination_mgr;
   39.34 +
   39.35 +typedef my_destination_mgr * my_dest_ptr;
   39.36 +
   39.37 +#define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
   39.38 +
   39.39 +
   39.40 +/*
   39.41 + * Initialize destination --- called by jpeg_start_compress
   39.42 + * before any data is actually written.
   39.43 + */
   39.44 +
   39.45 +METHODDEF(void)
   39.46 +init_destination (j_compress_ptr cinfo)
   39.47 +{
   39.48 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
   39.49 +
   39.50 +  /* Allocate the output buffer --- it will be released when done with image */
   39.51 +  dest->buffer = (JOCTET *)
   39.52 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   39.53 +				  OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
   39.54 +
   39.55 +  dest->pub.next_output_byte = dest->buffer;
   39.56 +  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   39.57 +}
   39.58 +
   39.59 +
   39.60 +/*
   39.61 + * Empty the output buffer --- called whenever buffer fills up.
   39.62 + *
   39.63 + * In typical applications, this should write the entire output buffer
   39.64 + * (ignoring the current state of next_output_byte & free_in_buffer),
   39.65 + * reset the pointer & count to the start of the buffer, and return TRUE
   39.66 + * indicating that the buffer has been dumped.
   39.67 + *
   39.68 + * In applications that need to be able to suspend compression due to output
   39.69 + * overrun, a FALSE return indicates that the buffer cannot be emptied now.
   39.70 + * In this situation, the compressor will return to its caller (possibly with
   39.71 + * an indication that it has not accepted all the supplied scanlines).  The
   39.72 + * application should resume compression after it has made more room in the
   39.73 + * output buffer.  Note that there are substantial restrictions on the use of
   39.74 + * suspension --- see the documentation.
   39.75 + *
   39.76 + * When suspending, the compressor will back up to a convenient restart point
   39.77 + * (typically the start of the current MCU). next_output_byte & free_in_buffer
   39.78 + * indicate where the restart point will be if the current call returns FALSE.
   39.79 + * Data beyond this point will be regenerated after resumption, so do not
   39.80 + * write it out when emptying the buffer externally.
   39.81 + */
   39.82 +
   39.83 +METHODDEF(boolean)
   39.84 +empty_output_buffer (j_compress_ptr cinfo)
   39.85 +{
   39.86 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
   39.87 +
   39.88 +  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
   39.89 +      (size_t) OUTPUT_BUF_SIZE)
   39.90 +    ERREXIT(cinfo, JERR_FILE_WRITE);
   39.91 +
   39.92 +  dest->pub.next_output_byte = dest->buffer;
   39.93 +  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   39.94 +
   39.95 +  return TRUE;
   39.96 +}
   39.97 +
   39.98 +
   39.99 +/*
  39.100 + * Terminate destination --- called by jpeg_finish_compress
  39.101 + * after all data has been written.  Usually needs to flush buffer.
  39.102 + *
  39.103 + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  39.104 + * application must deal with any cleanup that should happen even
  39.105 + * for error exit.
  39.106 + */
  39.107 +
  39.108 +METHODDEF(void)
  39.109 +term_destination (j_compress_ptr cinfo)
  39.110 +{
  39.111 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  39.112 +  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  39.113 +
  39.114 +  /* Write any data remaining in the buffer */
  39.115 +  if (datacount > 0) {
  39.116 +    if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  39.117 +      ERREXIT(cinfo, JERR_FILE_WRITE);
  39.118 +  }
  39.119 +  fflush(dest->outfile);
  39.120 +  /* Make sure we wrote the output file OK */
  39.121 +  if (ferror(dest->outfile))
  39.122 +    ERREXIT(cinfo, JERR_FILE_WRITE);
  39.123 +}
  39.124 +
  39.125 +
  39.126 +/*
  39.127 + * Prepare for output to a stdio stream.
  39.128 + * The caller must have already opened the stream, and is responsible
  39.129 + * for closing it after finishing compression.
  39.130 + */
  39.131 +
  39.132 +GLOBAL(void)
  39.133 +jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
  39.134 +{
  39.135 +  my_dest_ptr dest;
  39.136 +
  39.137 +  /* The destination object is made permanent so that multiple JPEG images
  39.138 +   * can be written to the same file without re-executing jpeg_stdio_dest.
  39.139 +   * This makes it dangerous to use this manager and a different destination
  39.140 +   * manager serially with the same JPEG object, because their private object
  39.141 +   * sizes may be different.  Caveat programmer.
  39.142 +   */
  39.143 +  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
  39.144 +    cinfo->dest = (struct jpeg_destination_mgr *)
  39.145 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  39.146 +				  SIZEOF(my_destination_mgr));
  39.147 +  }
  39.148 +
  39.149 +  dest = (my_dest_ptr) cinfo->dest;
  39.150 +  dest->pub.init_destination = init_destination;
  39.151 +  dest->pub.empty_output_buffer = empty_output_buffer;
  39.152 +  dest->pub.term_destination = term_destination;
  39.153 +  dest->outfile = outfile;
  39.154 +}
    40.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.2 +++ b/libs/libjpeg/jdatasrc.c	Sun Jun 07 17:25:49 2015 +0300
    40.3 @@ -0,0 +1,212 @@
    40.4 +/*
    40.5 + * jdatasrc.c
    40.6 + *
    40.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    40.8 + * This file is part of the Independent JPEG Group's software.
    40.9 + * For conditions of distribution and use, see the accompanying README file.
   40.10 + *
   40.11 + * This file contains decompression data source routines for the case of
   40.12 + * reading JPEG data from a file (or any stdio stream).  While these routines
   40.13 + * are sufficient for most applications, some will want to use a different
   40.14 + * source manager.
   40.15 + * IMPORTANT: we assume that fread() will correctly transcribe an array of
   40.16 + * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
   40.17 + * than 8 bits on your machine, you may need to do some tweaking.
   40.18 + */
   40.19 +
   40.20 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   40.21 +#include "jinclude.h"
   40.22 +#include "jpeglib.h"
   40.23 +#include "jerror.h"
   40.24 +
   40.25 +
   40.26 +/* Expanded data source object for stdio input */
   40.27 +
   40.28 +typedef struct {
   40.29 +  struct jpeg_source_mgr pub;	/* public fields */
   40.30 +
   40.31 +  FILE * infile;		/* source stream */
   40.32 +  JOCTET * buffer;		/* start of buffer */
   40.33 +  boolean start_of_file;	/* have we gotten any data yet? */
   40.34 +} my_source_mgr;
   40.35 +
   40.36 +typedef my_source_mgr * my_src_ptr;
   40.37 +
   40.38 +#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */
   40.39 +
   40.40 +
   40.41 +/*
   40.42 + * Initialize source --- called by jpeg_read_header
   40.43 + * before any data is actually read.
   40.44 + */
   40.45 +
   40.46 +METHODDEF(void)
   40.47 +init_source (j_decompress_ptr cinfo)
   40.48 +{
   40.49 +  my_src_ptr src = (my_src_ptr) cinfo->src;
   40.50 +
   40.51 +  /* We reset the empty-input-file flag for each image,
   40.52 +   * but we don't clear the input buffer.
   40.53 +   * This is correct behavior for reading a series of images from one source.
   40.54 +   */
   40.55 +  src->start_of_file = TRUE;
   40.56 +}
   40.57 +
   40.58 +
   40.59 +/*
   40.60 + * Fill the input buffer --- called whenever buffer is emptied.
   40.61 + *
   40.62 + * In typical applications, this should read fresh data into the buffer
   40.63 + * (ignoring the current state of next_input_byte & bytes_in_buffer),
   40.64 + * reset the pointer & count to the start of the buffer, and return TRUE
   40.65 + * indicating that the buffer has been reloaded.  It is not necessary to
   40.66 + * fill the buffer entirely, only to obtain at least one more byte.
   40.67 + *
   40.68 + * There is no such thing as an EOF return.  If the end of the file has been
   40.69 + * reached, the routine has a choice of ERREXIT() or inserting fake data into
   40.70 + * the buffer.  In most cases, generating a warning message and inserting a
   40.71 + * fake EOI marker is the best course of action --- this will allow the
   40.72 + * decompressor to output however much of the image is there.  However,
   40.73 + * the resulting error message is misleading if the real problem is an empty
   40.74 + * input file, so we handle that case specially.
   40.75 + *
   40.76 + * In applications that need to be able to suspend compression due to input
   40.77 + * not being available yet, a FALSE return indicates that no more data can be
   40.78 + * obtained right now, but more may be forthcoming later.  In this situation,
   40.79 + * the decompressor will return to its caller (with an indication of the
   40.80 + * number of scanlines it has read, if any).  The application should resume
   40.81 + * decompression after it has loaded more data into the input buffer.  Note
   40.82 + * that there are substantial restrictions on the use of suspension --- see
   40.83 + * the documentation.
   40.84 + *
   40.85 + * When suspending, the decompressor will back up to a convenient restart point
   40.86 + * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
   40.87 + * indicate where the restart point will be if the current call returns FALSE.
   40.88 + * Data beyond this point must be rescanned after resumption, so move it to
   40.89 + * the front of the buffer rather than discarding it.
   40.90 + */
   40.91 +
   40.92 +METHODDEF(boolean)
   40.93 +fill_input_buffer (j_decompress_ptr cinfo)
   40.94 +{
   40.95 +  my_src_ptr src = (my_src_ptr) cinfo->src;
   40.96 +  size_t nbytes;
   40.97 +
   40.98 +  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
   40.99 +
  40.100 +  if (nbytes <= 0) {
  40.101 +    if (src->start_of_file)	/* Treat empty input file as fatal error */
  40.102 +      ERREXIT(cinfo, JERR_INPUT_EMPTY);
  40.103 +    WARNMS(cinfo, JWRN_JPEG_EOF);
  40.104 +    /* Insert a fake EOI marker */
  40.105 +    src->buffer[0] = (JOCTET) 0xFF;
  40.106 +    src->buffer[1] = (JOCTET) JPEG_EOI;
  40.107 +    nbytes = 2;
  40.108 +  }
  40.109 +
  40.110 +  src->pub.next_input_byte = src->buffer;
  40.111 +  src->pub.bytes_in_buffer = nbytes;
  40.112 +  src->start_of_file = FALSE;
  40.113 +
  40.114 +  return TRUE;
  40.115 +}
  40.116 +
  40.117 +
  40.118 +/*
  40.119 + * Skip data --- used to skip over a potentially large amount of
  40.120 + * uninteresting data (such as an APPn marker).
  40.121 + *
  40.122 + * Writers of suspendable-input applications must note that skip_input_data
  40.123 + * is not granted the right to give a suspension return.  If the skip extends
  40.124 + * beyond the data currently in the buffer, the buffer can be marked empty so
  40.125 + * that the next read will cause a fill_input_buffer call that can suspend.
  40.126 + * Arranging for additional bytes to be discarded before reloading the input
  40.127 + * buffer is the application writer's problem.
  40.128 + */
  40.129 +
  40.130 +METHODDEF(void)
  40.131 +skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  40.132 +{
  40.133 +  my_src_ptr src = (my_src_ptr) cinfo->src;
  40.134 +
  40.135 +  /* Just a dumb implementation for now.  Could use fseek() except
  40.136 +   * it doesn't work on pipes.  Not clear that being smart is worth
  40.137 +   * any trouble anyway --- large skips are infrequent.
  40.138 +   */
  40.139 +  if (num_bytes > 0) {
  40.140 +    while (num_bytes > (long) src->pub.bytes_in_buffer) {
  40.141 +      num_bytes -= (long) src->pub.bytes_in_buffer;
  40.142 +      (void) fill_input_buffer(cinfo);
  40.143 +      /* note we assume that fill_input_buffer will never return FALSE,
  40.144 +       * so suspension need not be handled.
  40.145 +       */
  40.146 +    }
  40.147 +    src->pub.next_input_byte += (size_t) num_bytes;
  40.148 +    src->pub.bytes_in_buffer -= (size_t) num_bytes;
  40.149 +  }
  40.150 +}
  40.151 +
  40.152 +
  40.153 +/*
  40.154 + * An additional method that can be provided by data source modules is the
  40.155 + * resync_to_restart method for error recovery in the presence of RST markers.
  40.156 + * For the moment, this source module just uses the default resync method
  40.157 + * provided by the JPEG library.  That method assumes that no backtracking
  40.158 + * is possible.
  40.159 + */
  40.160 +
  40.161 +
  40.162 +/*
  40.163 + * Terminate source --- called by jpeg_finish_decompress
  40.164 + * after all data has been read.  Often a no-op.
  40.165 + *
  40.166 + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  40.167 + * application must deal with any cleanup that should happen even
  40.168 + * for error exit.
  40.169 + */
  40.170 +
  40.171 +METHODDEF(void)
  40.172 +term_source (j_decompress_ptr cinfo)
  40.173 +{
  40.174 +  /* no work necessary here */
  40.175 +}
  40.176 +
  40.177 +
  40.178 +/*
  40.179 + * Prepare for input from a stdio stream.
  40.180 + * The caller must have already opened the stream, and is responsible
  40.181 + * for closing it after finishing decompression.
  40.182 + */
  40.183 +
  40.184 +GLOBAL(void)
  40.185 +jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
  40.186 +{
  40.187 +  my_src_ptr src;
  40.188 +
  40.189 +  /* The source object and input buffer are made permanent so that a series
  40.190 +   * of JPEG images can be read from the same file by calling jpeg_stdio_src
  40.191 +   * only before the first one.  (If we discarded the buffer at the end of
  40.192 +   * one image, we'd likely lose the start of the next one.)
  40.193 +   * This makes it unsafe to use this manager and a different source
  40.194 +   * manager serially with the same JPEG object.  Caveat programmer.
  40.195 +   */
  40.196 +  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
  40.197 +    cinfo->src = (struct jpeg_source_mgr *)
  40.198 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  40.199 +				  SIZEOF(my_source_mgr));
  40.200 +    src = (my_src_ptr) cinfo->src;
  40.201 +    src->buffer = (JOCTET *)
  40.202 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  40.203 +				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
  40.204 +  }
  40.205 +
  40.206 +  src = (my_src_ptr) cinfo->src;
  40.207 +  src->pub.init_source = init_source;
  40.208 +  src->pub.fill_input_buffer = fill_input_buffer;
  40.209 +  src->pub.skip_input_data = skip_input_data;
  40.210 +  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  40.211 +  src->pub.term_source = term_source;
  40.212 +  src->infile = infile;
  40.213 +  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  40.214 +  src->pub.next_input_byte = NULL; /* until buffer loaded */
  40.215 +}
    41.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.2 +++ b/libs/libjpeg/jdcoefct.c	Sun Jun 07 17:25:49 2015 +0300
    41.3 @@ -0,0 +1,736 @@
    41.4 +/*
    41.5 + * jdcoefct.c
    41.6 + *
    41.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    41.8 + * This file is part of the Independent JPEG Group's software.
    41.9 + * For conditions of distribution and use, see the accompanying README file.
   41.10 + *
   41.11 + * This file contains the coefficient buffer controller for decompression.
   41.12 + * This controller is the top level of the JPEG decompressor proper.
   41.13 + * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
   41.14 + *
   41.15 + * In buffered-image mode, this controller is the interface between
   41.16 + * input-oriented processing and output-oriented processing.
   41.17 + * Also, the input side (only) is used when reading a file for transcoding.
   41.18 + */
   41.19 +
   41.20 +#define JPEG_INTERNALS
   41.21 +#include "jinclude.h"
   41.22 +#include "jpeglib.h"
   41.23 +
   41.24 +/* Block smoothing is only applicable for progressive JPEG, so: */
   41.25 +#ifndef D_PROGRESSIVE_SUPPORTED
   41.26 +#undef BLOCK_SMOOTHING_SUPPORTED
   41.27 +#endif
   41.28 +
   41.29 +/* Private buffer controller object */
   41.30 +
   41.31 +typedef struct {
   41.32 +  struct jpeg_d_coef_controller pub; /* public fields */
   41.33 +
   41.34 +  /* These variables keep track of the current location of the input side. */
   41.35 +  /* cinfo->input_iMCU_row is also used for this. */
   41.36 +  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
   41.37 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
   41.38 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
   41.39 +
   41.40 +  /* The output side's location is represented by cinfo->output_iMCU_row. */
   41.41 +
   41.42 +  /* In single-pass modes, it's sufficient to buffer just one MCU.
   41.43 +   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
   41.44 +   * and let the entropy decoder write into that workspace each time.
   41.45 +   * (On 80x86, the workspace is FAR even though it's not really very big;
   41.46 +   * this is to keep the module interfaces unchanged when a large coefficient
   41.47 +   * buffer is necessary.)
   41.48 +   * In multi-pass modes, this array points to the current MCU's blocks
   41.49 +   * within the virtual arrays; it is used only by the input side.
   41.50 +   */
   41.51 +  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
   41.52 +
   41.53 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   41.54 +  /* In multi-pass modes, we need a virtual block array for each component. */
   41.55 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
   41.56 +#endif
   41.57 +
   41.58 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   41.59 +  /* When doing block smoothing, we latch coefficient Al values here */
   41.60 +  int * coef_bits_latch;
   41.61 +#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
   41.62 +#endif
   41.63 +} my_coef_controller;
   41.64 +
   41.65 +typedef my_coef_controller * my_coef_ptr;
   41.66 +
   41.67 +/* Forward declarations */
   41.68 +METHODDEF(int) decompress_onepass
   41.69 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   41.70 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   41.71 +METHODDEF(int) decompress_data
   41.72 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   41.73 +#endif
   41.74 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   41.75 +LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
   41.76 +METHODDEF(int) decompress_smooth_data
   41.77 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   41.78 +#endif
   41.79 +
   41.80 +
   41.81 +LOCAL(void)
   41.82 +start_iMCU_row (j_decompress_ptr cinfo)
   41.83 +/* Reset within-iMCU-row counters for a new row (input side) */
   41.84 +{
   41.85 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   41.86 +
   41.87 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
   41.88 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
   41.89 +   * But at the bottom of the image, process only what's left.
   41.90 +   */
   41.91 +  if (cinfo->comps_in_scan > 1) {
   41.92 +    coef->MCU_rows_per_iMCU_row = 1;
   41.93 +  } else {
   41.94 +    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
   41.95 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
   41.96 +    else
   41.97 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
   41.98 +  }
   41.99 +
  41.100 +  coef->MCU_ctr = 0;
  41.101 +  coef->MCU_vert_offset = 0;
  41.102 +}
  41.103 +
  41.104 +
  41.105 +/*
  41.106 + * Initialize for an input processing pass.
  41.107 + */
  41.108 +
  41.109 +METHODDEF(void)
  41.110 +start_input_pass (j_decompress_ptr cinfo)
  41.111 +{
  41.112 +  cinfo->input_iMCU_row = 0;
  41.113 +  start_iMCU_row(cinfo);
  41.114 +}
  41.115 +
  41.116 +
  41.117 +/*
  41.118 + * Initialize for an output processing pass.
  41.119 + */
  41.120 +
  41.121 +METHODDEF(void)
  41.122 +start_output_pass (j_decompress_ptr cinfo)
  41.123 +{
  41.124 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  41.125 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.126 +
  41.127 +  /* If multipass, check to see whether to use block smoothing on this pass */
  41.128 +  if (coef->pub.coef_arrays != NULL) {
  41.129 +    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
  41.130 +      coef->pub.decompress_data = decompress_smooth_data;
  41.131 +    else
  41.132 +      coef->pub.decompress_data = decompress_data;
  41.133 +  }
  41.134 +#endif
  41.135 +  cinfo->output_iMCU_row = 0;
  41.136 +}
  41.137 +
  41.138 +
  41.139 +/*
  41.140 + * Decompress and return some data in the single-pass case.
  41.141 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  41.142 + * Input and output must run in lockstep since we have only a one-MCU buffer.
  41.143 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  41.144 + *
  41.145 + * NB: output_buf contains a plane for each component in image,
  41.146 + * which we index according to the component's SOF position.
  41.147 + */
  41.148 +
  41.149 +METHODDEF(int)
  41.150 +decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  41.151 +{
  41.152 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.153 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  41.154 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  41.155 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  41.156 +  int blkn, ci, xindex, yindex, yoffset, useful_width;
  41.157 +  JSAMPARRAY output_ptr;
  41.158 +  JDIMENSION start_col, output_col;
  41.159 +  jpeg_component_info *compptr;
  41.160 +  inverse_DCT_method_ptr inverse_DCT;
  41.161 +
  41.162 +  /* Loop to process as much as one whole iMCU row */
  41.163 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  41.164 +       yoffset++) {
  41.165 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
  41.166 +	 MCU_col_num++) {
  41.167 +      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
  41.168 +      jzero_far((void FAR *) coef->MCU_buffer[0],
  41.169 +		(size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
  41.170 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  41.171 +	/* Suspension forced; update state counters and exit */
  41.172 +	coef->MCU_vert_offset = yoffset;
  41.173 +	coef->MCU_ctr = MCU_col_num;
  41.174 +	return JPEG_SUSPENDED;
  41.175 +      }
  41.176 +      /* Determine where data should go in output_buf and do the IDCT thing.
  41.177 +       * We skip dummy blocks at the right and bottom edges (but blkn gets
  41.178 +       * incremented past them!).  Note the inner loop relies on having
  41.179 +       * allocated the MCU_buffer[] blocks sequentially.
  41.180 +       */
  41.181 +      blkn = 0;			/* index of current DCT block within MCU */
  41.182 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  41.183 +	compptr = cinfo->cur_comp_info[ci];
  41.184 +	/* Don't bother to IDCT an uninteresting component. */
  41.185 +	if (! compptr->component_needed) {
  41.186 +	  blkn += compptr->MCU_blocks;
  41.187 +	  continue;
  41.188 +	}
  41.189 +	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
  41.190 +	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  41.191 +						    : compptr->last_col_width;
  41.192 +	output_ptr = output_buf[compptr->component_index] +
  41.193 +	  yoffset * compptr->DCT_scaled_size;
  41.194 +	start_col = MCU_col_num * compptr->MCU_sample_width;
  41.195 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  41.196 +	  if (cinfo->input_iMCU_row < last_iMCU_row ||
  41.197 +	      yoffset+yindex < compptr->last_row_height) {
  41.198 +	    output_col = start_col;
  41.199 +	    for (xindex = 0; xindex < useful_width; xindex++) {
  41.200 +	      (*inverse_DCT) (cinfo, compptr,
  41.201 +			      (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
  41.202 +			      output_ptr, output_col);
  41.203 +	      output_col += compptr->DCT_scaled_size;
  41.204 +	    }
  41.205 +	  }
  41.206 +	  blkn += compptr->MCU_width;
  41.207 +	  output_ptr += compptr->DCT_scaled_size;
  41.208 +	}
  41.209 +      }
  41.210 +    }
  41.211 +    /* Completed an MCU row, but perhaps not an iMCU row */
  41.212 +    coef->MCU_ctr = 0;
  41.213 +  }
  41.214 +  /* Completed the iMCU row, advance counters for next one */
  41.215 +  cinfo->output_iMCU_row++;
  41.216 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  41.217 +    start_iMCU_row(cinfo);
  41.218 +    return JPEG_ROW_COMPLETED;
  41.219 +  }
  41.220 +  /* Completed the scan */
  41.221 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
  41.222 +  return JPEG_SCAN_COMPLETED;
  41.223 +}
  41.224 +
  41.225 +
  41.226 +/*
  41.227 + * Dummy consume-input routine for single-pass operation.
  41.228 + */
  41.229 +
  41.230 +METHODDEF(int)
  41.231 +dummy_consume_data (j_decompress_ptr cinfo)
  41.232 +{
  41.233 +  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
  41.234 +}
  41.235 +
  41.236 +
  41.237 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  41.238 +
  41.239 +/*
  41.240 + * Consume input data and store it in the full-image coefficient buffer.
  41.241 + * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  41.242 + * ie, v_samp_factor block rows for each component in the scan.
  41.243 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  41.244 + */
  41.245 +
  41.246 +METHODDEF(int)
  41.247 +consume_data (j_decompress_ptr cinfo)
  41.248 +{
  41.249 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.250 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  41.251 +  int blkn, ci, xindex, yindex, yoffset;
  41.252 +  JDIMENSION start_col;
  41.253 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  41.254 +  JBLOCKROW buffer_ptr;
  41.255 +  jpeg_component_info *compptr;
  41.256 +
  41.257 +  /* Align the virtual buffers for the components used in this scan. */
  41.258 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  41.259 +    compptr = cinfo->cur_comp_info[ci];
  41.260 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  41.261 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  41.262 +       cinfo->input_iMCU_row * compptr->v_samp_factor,
  41.263 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
  41.264 +    /* Note: entropy decoder expects buffer to be zeroed,
  41.265 +     * but this is handled automatically by the memory manager
  41.266 +     * because we requested a pre-zeroed array.
  41.267 +     */
  41.268 +  }
  41.269 +
  41.270 +  /* Loop to process one whole iMCU row */
  41.271 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  41.272 +       yoffset++) {
  41.273 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
  41.274 +	 MCU_col_num++) {
  41.275 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  41.276 +      blkn = 0;			/* index of current DCT block within MCU */
  41.277 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  41.278 +	compptr = cinfo->cur_comp_info[ci];
  41.279 +	start_col = MCU_col_num * compptr->MCU_width;
  41.280 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  41.281 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  41.282 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  41.283 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
  41.284 +	  }
  41.285 +	}
  41.286 +      }
  41.287 +      /* Try to fetch the MCU. */
  41.288 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  41.289 +	/* Suspension forced; update state counters and exit */
  41.290 +	coef->MCU_vert_offset = yoffset;
  41.291 +	coef->MCU_ctr = MCU_col_num;
  41.292 +	return JPEG_SUSPENDED;
  41.293 +      }
  41.294 +    }
  41.295 +    /* Completed an MCU row, but perhaps not an iMCU row */
  41.296 +    coef->MCU_ctr = 0;
  41.297 +  }
  41.298 +  /* Completed the iMCU row, advance counters for next one */
  41.299 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  41.300 +    start_iMCU_row(cinfo);
  41.301 +    return JPEG_ROW_COMPLETED;
  41.302 +  }
  41.303 +  /* Completed the scan */
  41.304 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
  41.305 +  return JPEG_SCAN_COMPLETED;
  41.306 +}
  41.307 +
  41.308 +
  41.309 +/*
  41.310 + * Decompress and return some data in the multi-pass case.
  41.311 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  41.312 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  41.313 + *
  41.314 + * NB: output_buf contains a plane for each component in image.
  41.315 + */
  41.316 +
  41.317 +METHODDEF(int)
  41.318 +decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  41.319 +{
  41.320 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.321 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  41.322 +  JDIMENSION block_num;
  41.323 +  int ci, block_row, block_rows;
  41.324 +  JBLOCKARRAY buffer;
  41.325 +  JBLOCKROW buffer_ptr;
  41.326 +  JSAMPARRAY output_ptr;
  41.327 +  JDIMENSION output_col;
  41.328 +  jpeg_component_info *compptr;
  41.329 +  inverse_DCT_method_ptr inverse_DCT;
  41.330 +
  41.331 +  /* Force some input to be done if we are getting ahead of the input. */
  41.332 +  while (cinfo->input_scan_number < cinfo->output_scan_number ||
  41.333 +	 (cinfo->input_scan_number == cinfo->output_scan_number &&
  41.334 +	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
  41.335 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  41.336 +      return JPEG_SUSPENDED;
  41.337 +  }
  41.338 +
  41.339 +  /* OK, output from the virtual arrays. */
  41.340 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.341 +       ci++, compptr++) {
  41.342 +    /* Don't bother to IDCT an uninteresting component. */
  41.343 +    if (! compptr->component_needed)
  41.344 +      continue;
  41.345 +    /* Align the virtual buffer for this component. */
  41.346 +    buffer = (*cinfo->mem->access_virt_barray)
  41.347 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
  41.348 +       cinfo->output_iMCU_row * compptr->v_samp_factor,
  41.349 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  41.350 +    /* Count non-dummy DCT block rows in this iMCU row. */
  41.351 +    if (cinfo->output_iMCU_row < last_iMCU_row)
  41.352 +      block_rows = compptr->v_samp_factor;
  41.353 +    else {
  41.354 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
  41.355 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  41.356 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  41.357 +    }
  41.358 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
  41.359 +    output_ptr = output_buf[ci];
  41.360 +    /* Loop over all DCT blocks to be processed. */
  41.361 +    for (block_row = 0; block_row < block_rows; block_row++) {
  41.362 +      buffer_ptr = buffer[block_row];
  41.363 +      output_col = 0;
  41.364 +      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
  41.365 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
  41.366 +			output_ptr, output_col);
  41.367 +	buffer_ptr++;
  41.368 +	output_col += compptr->DCT_scaled_size;
  41.369 +      }
  41.370 +      output_ptr += compptr->DCT_scaled_size;
  41.371 +    }
  41.372 +  }
  41.373 +
  41.374 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  41.375 +    return JPEG_ROW_COMPLETED;
  41.376 +  return JPEG_SCAN_COMPLETED;
  41.377 +}
  41.378 +
  41.379 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  41.380 +
  41.381 +
  41.382 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  41.383 +
  41.384 +/*
  41.385 + * This code applies interblock smoothing as described by section K.8
  41.386 + * of the JPEG standard: the first 5 AC coefficients are estimated from
  41.387 + * the DC values of a DCT block and its 8 neighboring blocks.
  41.388 + * We apply smoothing only for progressive JPEG decoding, and only if
  41.389 + * the coefficients it can estimate are not yet known to full precision.
  41.390 + */
  41.391 +
  41.392 +/* Natural-order array positions of the first 5 zigzag-order coefficients */
  41.393 +#define Q01_POS  1
  41.394 +#define Q10_POS  8
  41.395 +#define Q20_POS  16
  41.396 +#define Q11_POS  9
  41.397 +#define Q02_POS  2
  41.398 +
  41.399 +/*
  41.400 + * Determine whether block smoothing is applicable and safe.
  41.401 + * We also latch the current states of the coef_bits[] entries for the
  41.402 + * AC coefficients; otherwise, if the input side of the decompressor
  41.403 + * advances into a new scan, we might think the coefficients are known
  41.404 + * more accurately than they really are.
  41.405 + */
  41.406 +
  41.407 +LOCAL(boolean)
  41.408 +smoothing_ok (j_decompress_ptr cinfo)
  41.409 +{
  41.410 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.411 +  boolean smoothing_useful = FALSE;
  41.412 +  int ci, coefi;
  41.413 +  jpeg_component_info *compptr;
  41.414 +  JQUANT_TBL * qtable;
  41.415 +  int * coef_bits;
  41.416 +  int * coef_bits_latch;
  41.417 +
  41.418 +  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
  41.419 +    return FALSE;
  41.420 +
  41.421 +  /* Allocate latch area if not already done */
  41.422 +  if (coef->coef_bits_latch == NULL)
  41.423 +    coef->coef_bits_latch = (int *)
  41.424 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  41.425 +				  cinfo->num_components *
  41.426 +				  (SAVED_COEFS * SIZEOF(int)));
  41.427 +  coef_bits_latch = coef->coef_bits_latch;
  41.428 +
  41.429 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.430 +       ci++, compptr++) {
  41.431 +    /* All components' quantization values must already be latched. */
  41.432 +    if ((qtable = compptr->quant_table) == NULL)
  41.433 +      return FALSE;
  41.434 +    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
  41.435 +    if (qtable->quantval[0] == 0 ||
  41.436 +	qtable->quantval[Q01_POS] == 0 ||
  41.437 +	qtable->quantval[Q10_POS] == 0 ||
  41.438 +	qtable->quantval[Q20_POS] == 0 ||
  41.439 +	qtable->quantval[Q11_POS] == 0 ||
  41.440 +	qtable->quantval[Q02_POS] == 0)
  41.441 +      return FALSE;
  41.442 +    /* DC values must be at least partly known for all components. */
  41.443 +    coef_bits = cinfo->coef_bits[ci];
  41.444 +    if (coef_bits[0] < 0)
  41.445 +      return FALSE;
  41.446 +    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  41.447 +    for (coefi = 1; coefi <= 5; coefi++) {
  41.448 +      coef_bits_latch[coefi] = coef_bits[coefi];
  41.449 +      if (coef_bits[coefi] != 0)
  41.450 +	smoothing_useful = TRUE;
  41.451 +    }
  41.452 +    coef_bits_latch += SAVED_COEFS;
  41.453 +  }
  41.454 +
  41.455 +  return smoothing_useful;
  41.456 +}
  41.457 +
  41.458 +
  41.459 +/*
  41.460 + * Variant of decompress_data for use when doing block smoothing.
  41.461 + */
  41.462 +
  41.463 +METHODDEF(int)
  41.464 +decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  41.465 +{
  41.466 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  41.467 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  41.468 +  JDIMENSION block_num, last_block_column;
  41.469 +  int ci, block_row, block_rows, access_rows;
  41.470 +  JBLOCKARRAY buffer;
  41.471 +  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  41.472 +  JSAMPARRAY output_ptr;
  41.473 +  JDIMENSION output_col;
  41.474 +  jpeg_component_info *compptr;
  41.475 +  inverse_DCT_method_ptr inverse_DCT;
  41.476 +  boolean first_row, last_row;
  41.477 +  JBLOCK workspace;
  41.478 +  int *coef_bits;
  41.479 +  JQUANT_TBL *quanttbl;
  41.480 +  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  41.481 +  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  41.482 +  int Al, pred;
  41.483 +
  41.484 +  /* Force some input to be done if we are getting ahead of the input. */
  41.485 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  41.486 +	 ! cinfo->inputctl->eoi_reached) {
  41.487 +    if (cinfo->input_scan_number == cinfo->output_scan_number) {
  41.488 +      /* If input is working on current scan, we ordinarily want it to
  41.489 +       * have completed the current row.  But if input scan is DC,
  41.490 +       * we want it to keep one row ahead so that next block row's DC
  41.491 +       * values are up to date.
  41.492 +       */
  41.493 +      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
  41.494 +      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
  41.495 +	break;
  41.496 +    }
  41.497 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  41.498 +      return JPEG_SUSPENDED;
  41.499 +  }
  41.500 +
  41.501 +  /* OK, output from the virtual arrays. */
  41.502 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.503 +       ci++, compptr++) {
  41.504 +    /* Don't bother to IDCT an uninteresting component. */
  41.505 +    if (! compptr->component_needed)
  41.506 +      continue;
  41.507 +    /* Count non-dummy DCT block rows in this iMCU row. */
  41.508 +    if (cinfo->output_iMCU_row < last_iMCU_row) {
  41.509 +      block_rows = compptr->v_samp_factor;
  41.510 +      access_rows = block_rows * 2; /* this and next iMCU row */
  41.511 +      last_row = FALSE;
  41.512 +    } else {
  41.513 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
  41.514 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  41.515 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  41.516 +      access_rows = block_rows; /* this iMCU row only */
  41.517 +      last_row = TRUE;
  41.518 +    }
  41.519 +    /* Align the virtual buffer for this component. */
  41.520 +    if (cinfo->output_iMCU_row > 0) {
  41.521 +      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
  41.522 +      buffer = (*cinfo->mem->access_virt_barray)
  41.523 +	((j_common_ptr) cinfo, coef->whole_image[ci],
  41.524 +	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  41.525 +	 (JDIMENSION) access_rows, FALSE);
  41.526 +      buffer += compptr->v_samp_factor;	/* point to current iMCU row */
  41.527 +      first_row = FALSE;
  41.528 +    } else {
  41.529 +      buffer = (*cinfo->mem->access_virt_barray)
  41.530 +	((j_common_ptr) cinfo, coef->whole_image[ci],
  41.531 +	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
  41.532 +      first_row = TRUE;
  41.533 +    }
  41.534 +    /* Fetch component-dependent info */
  41.535 +    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  41.536 +    quanttbl = compptr->quant_table;
  41.537 +    Q00 = quanttbl->quantval[0];
  41.538 +    Q01 = quanttbl->quantval[Q01_POS];
  41.539 +    Q10 = quanttbl->quantval[Q10_POS];
  41.540 +    Q20 = quanttbl->quantval[Q20_POS];
  41.541 +    Q11 = quanttbl->quantval[Q11_POS];
  41.542 +    Q02 = quanttbl->quantval[Q02_POS];
  41.543 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
  41.544 +    output_ptr = output_buf[ci];
  41.545 +    /* Loop over all DCT blocks to be processed. */
  41.546 +    for (block_row = 0; block_row < block_rows; block_row++) {
  41.547 +      buffer_ptr = buffer[block_row];
  41.548 +      if (first_row && block_row == 0)
  41.549 +	prev_block_row = buffer_ptr;
  41.550 +      else
  41.551 +	prev_block_row = buffer[block_row-1];
  41.552 +      if (last_row && block_row == block_rows-1)
  41.553 +	next_block_row = buffer_ptr;
  41.554 +      else
  41.555 +	next_block_row = buffer[block_row+1];
  41.556 +      /* We fetch the surrounding DC values using a sliding-register approach.
  41.557 +       * Initialize all nine here so as to do the right thing on narrow pics.
  41.558 +       */
  41.559 +      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
  41.560 +      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
  41.561 +      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
  41.562 +      output_col = 0;
  41.563 +      last_block_column = compptr->width_in_blocks - 1;
  41.564 +      for (block_num = 0; block_num <= last_block_column; block_num++) {
  41.565 +	/* Fetch current DCT block into workspace so we can modify it. */
  41.566 +	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
  41.567 +	/* Update DC values */
  41.568 +	if (block_num < last_block_column) {
  41.569 +	  DC3 = (int) prev_block_row[1][0];
  41.570 +	  DC6 = (int) buffer_ptr[1][0];
  41.571 +	  DC9 = (int) next_block_row[1][0];
  41.572 +	}
  41.573 +	/* Compute coefficient estimates per K.8.
  41.574 +	 * An estimate is applied only if coefficient is still zero,
  41.575 +	 * and is not known to be fully accurate.
  41.576 +	 */
  41.577 +	/* AC01 */
  41.578 +	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
  41.579 +	  num = 36 * Q00 * (DC4 - DC6);
  41.580 +	  if (num >= 0) {
  41.581 +	    pred = (int) (((Q01<<7) + num) / (Q01<<8));
  41.582 +	    if (Al > 0 && pred >= (1<<Al))
  41.583 +	      pred = (1<<Al)-1;
  41.584 +	  } else {
  41.585 +	    pred = (int) (((Q01<<7) - num) / (Q01<<8));
  41.586 +	    if (Al > 0 && pred >= (1<<Al))
  41.587 +	      pred = (1<<Al)-1;
  41.588 +	    pred = -pred;
  41.589 +	  }
  41.590 +	  workspace[1] = (JCOEF) pred;
  41.591 +	}
  41.592 +	/* AC10 */
  41.593 +	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
  41.594 +	  num = 36 * Q00 * (DC2 - DC8);
  41.595 +	  if (num >= 0) {
  41.596 +	    pred = (int) (((Q10<<7) + num) / (Q10<<8));
  41.597 +	    if (Al > 0 && pred >= (1<<Al))
  41.598 +	      pred = (1<<Al)-1;
  41.599 +	  } else {
  41.600 +	    pred = (int) (((Q10<<7) - num) / (Q10<<8));
  41.601 +	    if (Al > 0 && pred >= (1<<Al))
  41.602 +	      pred = (1<<Al)-1;
  41.603 +	    pred = -pred;
  41.604 +	  }
  41.605 +	  workspace[8] = (JCOEF) pred;
  41.606 +	}
  41.607 +	/* AC20 */
  41.608 +	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
  41.609 +	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
  41.610 +	  if (num >= 0) {
  41.611 +	    pred = (int) (((Q20<<7) + num) / (Q20<<8));
  41.612 +	    if (Al > 0 && pred >= (1<<Al))
  41.613 +	      pred = (1<<Al)-1;
  41.614 +	  } else {
  41.615 +	    pred = (int) (((Q20<<7) - num) / (Q20<<8));
  41.616 +	    if (Al > 0 && pred >= (1<<Al))
  41.617 +	      pred = (1<<Al)-1;
  41.618 +	    pred = -pred;
  41.619 +	  }
  41.620 +	  workspace[16] = (JCOEF) pred;
  41.621 +	}
  41.622 +	/* AC11 */
  41.623 +	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
  41.624 +	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  41.625 +	  if (num >= 0) {
  41.626 +	    pred = (int) (((Q11<<7) + num) / (Q11<<8));
  41.627 +	    if (Al > 0 && pred >= (1<<Al))
  41.628 +	      pred = (1<<Al)-1;
  41.629 +	  } else {
  41.630 +	    pred = (int) (((Q11<<7) - num) / (Q11<<8));
  41.631 +	    if (Al > 0 && pred >= (1<<Al))
  41.632 +	      pred = (1<<Al)-1;
  41.633 +	    pred = -pred;
  41.634 +	  }
  41.635 +	  workspace[9] = (JCOEF) pred;
  41.636 +	}
  41.637 +	/* AC02 */
  41.638 +	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
  41.639 +	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
  41.640 +	  if (num >= 0) {
  41.641 +	    pred = (int) (((Q02<<7) + num) / (Q02<<8));
  41.642 +	    if (Al > 0 && pred >= (1<<Al))
  41.643 +	      pred = (1<<Al)-1;
  41.644 +	  } else {
  41.645 +	    pred = (int) (((Q02<<7) - num) / (Q02<<8));
  41.646 +	    if (Al > 0 && pred >= (1<<Al))
  41.647 +	      pred = (1<<Al)-1;
  41.648 +	    pred = -pred;
  41.649 +	  }
  41.650 +	  workspace[2] = (JCOEF) pred;
  41.651 +	}
  41.652 +	/* OK, do the IDCT */
  41.653 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
  41.654 +			output_ptr, output_col);
  41.655 +	/* Advance for next column */
  41.656 +	DC1 = DC2; DC2 = DC3;
  41.657 +	DC4 = DC5; DC5 = DC6;
  41.658 +	DC7 = DC8; DC8 = DC9;
  41.659 +	buffer_ptr++, prev_block_row++, next_block_row++;
  41.660 +	output_col += compptr->DCT_scaled_size;
  41.661 +      }
  41.662 +      output_ptr += compptr->DCT_scaled_size;
  41.663 +    }
  41.664 +  }
  41.665 +
  41.666 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  41.667 +    return JPEG_ROW_COMPLETED;
  41.668 +  return JPEG_SCAN_COMPLETED;
  41.669 +}
  41.670 +
  41.671 +#endif /* BLOCK_SMOOTHING_SUPPORTED */
  41.672 +
  41.673 +
  41.674 +/*
  41.675 + * Initialize coefficient buffer controller.
  41.676 + */
  41.677 +
  41.678 +GLOBAL(void)
  41.679 +jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  41.680 +{
  41.681 +  my_coef_ptr coef;
  41.682 +
  41.683 +  coef = (my_coef_ptr)
  41.684 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  41.685 +				SIZEOF(my_coef_controller));
  41.686 +  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  41.687 +  coef->pub.start_input_pass = start_input_pass;
  41.688 +  coef->pub.start_output_pass = start_output_pass;
  41.689 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  41.690 +  coef->coef_bits_latch = NULL;
  41.691 +#endif
  41.692 +
  41.693 +  /* Create the coefficient buffer. */
  41.694 +  if (need_full_buffer) {
  41.695 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  41.696 +    /* Allocate a full-image virtual array for each component, */
  41.697 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
  41.698 +    /* Note we ask for a pre-zeroed array. */
  41.699 +    int ci, access_rows;
  41.700 +    jpeg_component_info *compptr;
  41.701 +
  41.702 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.703 +	 ci++, compptr++) {
  41.704 +      access_rows = compptr->v_samp_factor;
  41.705 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  41.706 +      /* If block smoothing could be used, need a bigger window */
  41.707 +      if (cinfo->progressive_mode)
  41.708 +	access_rows *= 3;
  41.709 +#endif
  41.710 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  41.711 +	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
  41.712 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  41.713 +				(long) compptr->h_samp_factor),
  41.714 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  41.715 +				(long) compptr->v_samp_factor),
  41.716 +	 (JDIMENSION) access_rows);
  41.717 +    }
  41.718 +    coef->pub.consume_data = consume_data;
  41.719 +    coef->pub.decompress_data = decompress_data;
  41.720 +    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  41.721 +#else
  41.722 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  41.723 +#endif
  41.724 +  } else {
  41.725 +    /* We only need a single-MCU buffer. */
  41.726 +    JBLOCKROW buffer;
  41.727 +    int i;
  41.728 +
  41.729 +    buffer = (JBLOCKROW)
  41.730 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  41.731 +				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  41.732 +    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  41.733 +      coef->MCU_buffer[i] = buffer + i;
  41.734 +    }
  41.735 +    coef->pub.consume_data = dummy_consume_data;
  41.736 +    coef->pub.decompress_data = decompress_onepass;
  41.737 +    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  41.738 +  }
  41.739 +}
    42.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.2 +++ b/libs/libjpeg/jdcolor.c	Sun Jun 07 17:25:49 2015 +0300
    42.3 @@ -0,0 +1,396 @@
    42.4 +/*
    42.5 + * jdcolor.c
    42.6 + *
    42.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    42.8 + * This file is part of the Independent JPEG Group's software.
    42.9 + * For conditions of distribution and use, see the accompanying README file.
   42.10 + *
   42.11 + * This file contains output colorspace conversion routines.
   42.12 + */
   42.13 +
   42.14 +#define JPEG_INTERNALS
   42.15 +#include "jinclude.h"
   42.16 +#include "jpeglib.h"
   42.17 +
   42.18 +
   42.19 +/* Private subobject */
   42.20 +
   42.21 +typedef struct {
   42.22 +  struct jpeg_color_deconverter pub; /* public fields */
   42.23 +
   42.24 +  /* Private state for YCC->RGB conversion */
   42.25 +  int * Cr_r_tab;		/* => table for Cr to R conversion */
   42.26 +  int * Cb_b_tab;		/* => table for Cb to B conversion */
   42.27 +  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
   42.28 +  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
   42.29 +} my_color_deconverter;
   42.30 +
   42.31 +typedef my_color_deconverter * my_cconvert_ptr;
   42.32 +
   42.33 +
   42.34 +/**************** YCbCr -> RGB conversion: most common case **************/
   42.35 +
   42.36 +/*
   42.37 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
   42.38 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
   42.39 + * The conversion equations to be implemented are therefore
   42.40 + *	R = Y                + 1.40200 * Cr
   42.41 + *	G = Y - 0.34414 * Cb - 0.71414 * Cr
   42.42 + *	B = Y + 1.77200 * Cb
   42.43 + * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
   42.44 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
   42.45 + *
   42.46 + * To avoid floating-point arithmetic, we represent the fractional constants
   42.47 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
   42.48 + * the products by 2^16, with appropriate rounding, to get the correct answer.
   42.49 + * Notice that Y, being an integral input, does not contribute any fraction
   42.50 + * so it need not participate in the rounding.
   42.51 + *
   42.52 + * For even more speed, we avoid doing any multiplications in the inner loop
   42.53 + * by precalculating the constants times Cb and Cr for all possible values.
   42.54 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
   42.55 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
   42.56 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
   42.57 + * colorspace anyway.
   42.58 + * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
   42.59 + * values for the G calculation are left scaled up, since we must add them
   42.60 + * together before rounding.
   42.61 + */
   42.62 +
   42.63 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   42.64 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   42.65 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   42.66 +
   42.67 +
   42.68 +/*
   42.69 + * Initialize tables for YCC->RGB colorspace conversion.
   42.70 + */
   42.71 +
   42.72 +LOCAL(void)
   42.73 +build_ycc_rgb_table (j_decompress_ptr cinfo)
   42.74 +{
   42.75 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   42.76 +  int i;
   42.77 +  INT32 x;
   42.78 +  SHIFT_TEMPS
   42.79 +
   42.80 +  cconvert->Cr_r_tab = (int *)
   42.81 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   42.82 +				(MAXJSAMPLE+1) * SIZEOF(int));
   42.83 +  cconvert->Cb_b_tab = (int *)
   42.84 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   42.85 +				(MAXJSAMPLE+1) * SIZEOF(int));
   42.86 +  cconvert->Cr_g_tab = (INT32 *)
   42.87 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   42.88 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   42.89 +  cconvert->Cb_g_tab = (INT32 *)
   42.90 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   42.91 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   42.92 +
   42.93 +  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
   42.94 +    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
   42.95 +    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
   42.96 +    /* Cr=>R value is nearest int to 1.40200 * x */
   42.97 +    cconvert->Cr_r_tab[i] = (int)
   42.98 +		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
   42.99 +    /* Cb=>B value is nearest int to 1.77200 * x */
  42.100 +    cconvert->Cb_b_tab[i] = (int)
  42.101 +		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  42.102 +    /* Cr=>G value is scaled-up -0.71414 * x */
  42.103 +    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  42.104 +    /* Cb=>G value is scaled-up -0.34414 * x */
  42.105 +    /* We also add in ONE_HALF so that need not do it in inner loop */
  42.106 +    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  42.107 +  }
  42.108 +}
  42.109 +
  42.110 +
  42.111 +/*
  42.112 + * Convert some rows of samples to the output colorspace.
  42.113 + *
  42.114 + * Note that we change from noninterleaved, one-plane-per-component format
  42.115 + * to interleaved-pixel format.  The output buffer is therefore three times
  42.116 + * as wide as the input buffer.
  42.117 + * A starting row offset is provided only for the input buffer.  The caller
  42.118 + * can easily adjust the passed output_buf value to accommodate any row
  42.119 + * offset required on that side.
  42.120 + */
  42.121 +
  42.122 +METHODDEF(void)
  42.123 +ycc_rgb_convert (j_decompress_ptr cinfo,
  42.124 +		 JSAMPIMAGE input_buf, JDIMENSION input_row,
  42.125 +		 JSAMPARRAY output_buf, int num_rows)
  42.126 +{
  42.127 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  42.128 +  register int y, cb, cr;
  42.129 +  register JSAMPROW outptr;
  42.130 +  register JSAMPROW inptr0, inptr1, inptr2;
  42.131 +  register JDIMENSION col;
  42.132 +  JDIMENSION num_cols = cinfo->output_width;
  42.133 +  /* copy these pointers into registers if possible */
  42.134 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  42.135 +  register int * Crrtab = cconvert->Cr_r_tab;
  42.136 +  register int * Cbbtab = cconvert->Cb_b_tab;
  42.137 +  register INT32 * Crgtab = cconvert->Cr_g_tab;
  42.138 +  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  42.139 +  SHIFT_TEMPS
  42.140 +
  42.141 +  while (--num_rows >= 0) {
  42.142 +    inptr0 = input_buf[0][input_row];
  42.143 +    inptr1 = input_buf[1][input_row];
  42.144 +    inptr2 = input_buf[2][input_row];
  42.145 +    input_row++;
  42.146 +    outptr = *output_buf++;
  42.147 +    for (col = 0; col < num_cols; col++) {
  42.148 +      y  = GETJSAMPLE(inptr0[col]);
  42.149 +      cb = GETJSAMPLE(inptr1[col]);
  42.150 +      cr = GETJSAMPLE(inptr2[col]);
  42.151 +      /* Range-limiting is essential due to noise introduced by DCT losses. */
  42.152 +      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
  42.153 +      outptr[RGB_GREEN] = range_limit[y +
  42.154 +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  42.155 +						 SCALEBITS))];
  42.156 +      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
  42.157 +      outptr += RGB_PIXELSIZE;
  42.158 +    }
  42.159 +  }
  42.160 +}
  42.161 +
  42.162 +
  42.163 +/**************** Cases other than YCbCr -> RGB **************/
  42.164 +
  42.165 +
  42.166 +/*
  42.167 + * Color conversion for no colorspace change: just copy the data,
  42.168 + * converting from separate-planes to interleaved representation.
  42.169 + */
  42.170 +
  42.171 +METHODDEF(void)
  42.172 +null_convert (j_decompress_ptr cinfo,
  42.173 +	      JSAMPIMAGE input_buf, JDIMENSION input_row,
  42.174 +	      JSAMPARRAY output_buf, int num_rows)
  42.175 +{
  42.176 +  register JSAMPROW inptr, outptr;
  42.177 +  register JDIMENSION count;
  42.178 +  register int num_components = cinfo->num_components;
  42.179 +  JDIMENSION num_cols = cinfo->output_width;
  42.180 +  int ci;
  42.181 +
  42.182 +  while (--num_rows >= 0) {
  42.183 +    for (ci = 0; ci < num_components; ci++) {
  42.184 +      inptr = input_buf[ci][input_row];
  42.185 +      outptr = output_buf[0] + ci;
  42.186 +      for (count = num_cols; count > 0; count--) {
  42.187 +	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
  42.188 +	outptr += num_components;
  42.189 +      }
  42.190 +    }
  42.191 +    input_row++;
  42.192 +    output_buf++;
  42.193 +  }
  42.194 +}
  42.195 +
  42.196 +
  42.197 +/*
  42.198 + * Color conversion for grayscale: just copy the data.
  42.199 + * This also works for YCbCr -> grayscale conversion, in which
  42.200 + * we just copy the Y (luminance) component and ignore chrominance.
  42.201 + */
  42.202 +
  42.203 +METHODDEF(void)
  42.204 +grayscale_convert (j_decompress_ptr cinfo,
  42.205 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
  42.206 +		   JSAMPARRAY output_buf, int num_rows)
  42.207 +{
  42.208 +  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
  42.209 +		    num_rows, cinfo->output_width);
  42.210 +}
  42.211 +
  42.212 +
  42.213 +/*
  42.214 + * Convert grayscale to RGB: just duplicate the graylevel three times.
  42.215 + * This is provided to support applications that don't want to cope
  42.216 + * with grayscale as a separate case.
  42.217 + */
  42.218 +
  42.219 +METHODDEF(void)
  42.220 +gray_rgb_convert (j_decompress_ptr cinfo,
  42.221 +		  JSAMPIMAGE input_buf, JDIMENSION input_row,
  42.222 +		  JSAMPARRAY output_buf, int num_rows)
  42.223 +{
  42.224 +  register JSAMPROW inptr, outptr;
  42.225 +  register JDIMENSION col;
  42.226 +  JDIMENSION num_cols = cinfo->output_width;
  42.227 +
  42.228 +  while (--num_rows >= 0) {
  42.229 +    inptr = input_buf[0][input_row++];
  42.230 +    outptr = *output_buf++;
  42.231 +    for (col = 0; col < num_cols; col++) {
  42.232 +      /* We can dispense with GETJSAMPLE() here */
  42.233 +      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
  42.234 +      outptr += RGB_PIXELSIZE;
  42.235 +    }
  42.236 +  }
  42.237 +}
  42.238 +
  42.239 +
  42.240 +/*
  42.241 + * Adobe-style YCCK->CMYK conversion.
  42.242 + * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  42.243 + * conversion as above, while passing K (black) unchanged.
  42.244 + * We assume build_ycc_rgb_table has been called.
  42.245 + */
  42.246 +
  42.247 +METHODDEF(void)
  42.248 +ycck_cmyk_convert (j_decompress_ptr cinfo,
  42.249 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
  42.250 +		   JSAMPARRAY output_buf, int num_rows)
  42.251 +{
  42.252 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  42.253 +  register int y, cb, cr;
  42.254 +  register JSAMPROW outptr;
  42.255 +  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  42.256 +  register JDIMENSION col;
  42.257 +  JDIMENSION num_cols = cinfo->output_width;
  42.258 +  /* copy these pointers into registers if possible */
  42.259 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  42.260 +  register int * Crrtab = cconvert->Cr_r_tab;
  42.261 +  register int * Cbbtab = cconvert->Cb_b_tab;
  42.262 +  register INT32 * Crgtab = cconvert->Cr_g_tab;
  42.263 +  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  42.264 +  SHIFT_TEMPS
  42.265 +
  42.266 +  while (--num_rows >= 0) {
  42.267 +    inptr0 = input_buf[0][input_row];
  42.268 +    inptr1 = input_buf[1][input_row];
  42.269 +    inptr2 = input_buf[2][input_row];
  42.270 +    inptr3 = input_buf[3][input_row];
  42.271 +    input_row++;
  42.272 +    outptr = *output_buf++;
  42.273 +    for (col = 0; col < num_cols; col++) {
  42.274 +      y  = GETJSAMPLE(inptr0[col]);
  42.275 +      cb = GETJSAMPLE(inptr1[col]);
  42.276 +      cr = GETJSAMPLE(inptr2[col]);
  42.277 +      /* Range-limiting is essential due to noise introduced by DCT losses. */
  42.278 +      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
  42.279 +      outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
  42.280 +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  42.281 +						 SCALEBITS)))];
  42.282 +      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
  42.283 +      /* K passes through unchanged */
  42.284 +      outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
  42.285 +      outptr += 4;
  42.286 +    }
  42.287 +  }
  42.288 +}
  42.289 +
  42.290 +
  42.291 +/*
  42.292 + * Empty method for start_pass.
  42.293 + */
  42.294 +
  42.295 +METHODDEF(void)
  42.296 +start_pass_dcolor (j_decompress_ptr cinfo)
  42.297 +{
  42.298 +  /* no work needed */
  42.299 +}
  42.300 +
  42.301 +
  42.302 +/*
  42.303 + * Module initialization routine for output colorspace conversion.
  42.304 + */
  42.305 +
  42.306 +GLOBAL(void)
  42.307 +jinit_color_deconverter (j_decompress_ptr cinfo)
  42.308 +{
  42.309 +  my_cconvert_ptr cconvert;
  42.310 +  int ci;
  42.311 +
  42.312 +  cconvert = (my_cconvert_ptr)
  42.313 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  42.314 +				SIZEOF(my_color_deconverter));
  42.315 +  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  42.316 +  cconvert->pub.start_pass = start_pass_dcolor;
  42.317 +
  42.318 +  /* Make sure num_components agrees with jpeg_color_space */
  42.319 +  switch (cinfo->jpeg_color_space) {
  42.320 +  case JCS_GRAYSCALE:
  42.321 +    if (cinfo->num_components != 1)
  42.322 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  42.323 +    break;
  42.324 +
  42.325 +  case JCS_RGB:
  42.326 +  case JCS_YCbCr:
  42.327 +    if (cinfo->num_components != 3)
  42.328 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  42.329 +    break;
  42.330 +
  42.331 +  case JCS_CMYK:
  42.332 +  case JCS_YCCK:
  42.333 +    if (cinfo->num_components != 4)
  42.334 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  42.335 +    break;
  42.336 +
  42.337 +  default:			/* JCS_UNKNOWN can be anything */
  42.338 +    if (cinfo->num_components < 1)
  42.339 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  42.340 +    break;
  42.341 +  }
  42.342 +
  42.343 +  /* Set out_color_components and conversion method based on requested space.
  42.344 +   * Also clear the component_needed flags for any unused components,
  42.345 +   * so that earlier pipeline stages can avoid useless computation.
  42.346 +   */
  42.347 +
  42.348 +  switch (cinfo->out_color_space) {
  42.349 +  case JCS_GRAYSCALE:
  42.350 +    cinfo->out_color_components = 1;
  42.351 +    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  42.352 +	cinfo->jpeg_color_space == JCS_YCbCr) {
  42.353 +      cconvert->pub.color_convert = grayscale_convert;
  42.354 +      /* For color->grayscale conversion, only the Y (0) component is needed */
  42.355 +      for (ci = 1; ci < cinfo->num_components; ci++)
  42.356 +	cinfo->comp_info[ci].component_needed = FALSE;
  42.357 +    } else
  42.358 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  42.359 +    break;
  42.360 +
  42.361 +  case JCS_RGB:
  42.362 +    cinfo->out_color_components = RGB_PIXELSIZE;
  42.363 +    if (cinfo->jpeg_color_space == JCS_YCbCr) {
  42.364 +      cconvert->pub.color_convert = ycc_rgb_convert;
  42.365 +      build_ycc_rgb_table(cinfo);
  42.366 +    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  42.367 +      cconvert->pub.color_convert = gray_rgb_convert;
  42.368 +    } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
  42.369 +      cconvert->pub.color_convert = null_convert;
  42.370 +    } else
  42.371 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  42.372 +    break;
  42.373 +
  42.374 +  case JCS_CMYK:
  42.375 +    cinfo->out_color_components = 4;
  42.376 +    if (cinfo->jpeg_color_space == JCS_YCCK) {
  42.377 +      cconvert->pub.color_convert = ycck_cmyk_convert;
  42.378 +      build_ycc_rgb_table(cinfo);
  42.379 +    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  42.380 +      cconvert->pub.color_convert = null_convert;
  42.381 +    } else
  42.382 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  42.383 +    break;
  42.384 +
  42.385 +  default:
  42.386 +    /* Permit null conversion to same output space */
  42.387 +    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  42.388 +      cinfo->out_color_components = cinfo->num_components;
  42.389 +      cconvert->pub.color_convert = null_convert;
  42.390 +    } else			/* unsupported non-null conversion */
  42.391 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  42.392 +    break;
  42.393 +  }
  42.394 +
  42.395 +  if (cinfo->quantize_colors)
  42.396 +    cinfo->output_components = 1; /* single colormapped output component */
  42.397 +  else
  42.398 +    cinfo->output_components = cinfo->out_color_components;
  42.399 +}
    43.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    43.2 +++ b/libs/libjpeg/jdct.h	Sun Jun 07 17:25:49 2015 +0300
    43.3 @@ -0,0 +1,176 @@
    43.4 +/*
    43.5 + * jdct.h
    43.6 + *
    43.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    43.8 + * This file is part of the Independent JPEG Group's software.
    43.9 + * For conditions of distribution and use, see the accompanying README file.
   43.10 + *
   43.11 + * This include file contains common declarations for the forward and
   43.12 + * inverse DCT modules.  These declarations are private to the DCT managers
   43.13 + * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
   43.14 + * The individual DCT algorithms are kept in separate files to ease 
   43.15 + * machine-dependent tuning (e.g., assembly coding).
   43.16 + */
   43.17 +
   43.18 +
   43.19 +/*
   43.20 + * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
   43.21 + * the DCT is to be performed in-place in that buffer.  Type DCTELEM is int
   43.22 + * for 8-bit samples, INT32 for 12-bit samples.  (NOTE: Floating-point DCT
   43.23 + * implementations use an array of type FAST_FLOAT, instead.)
   43.24 + * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
   43.25 + * The DCT outputs are returned scaled up by a factor of 8; they therefore
   43.26 + * have a range of +-8K for 8-bit data, +-128K for 12-bit data.  This
   43.27 + * convention improves accuracy in integer implementations and saves some
   43.28 + * work in floating-point ones.
   43.29 + * Quantization of the output coefficients is done by jcdctmgr.c.
   43.30 + */
   43.31 +
   43.32 +#if BITS_IN_JSAMPLE == 8
   43.33 +typedef int DCTELEM;		/* 16 or 32 bits is fine */
   43.34 +#else
   43.35 +typedef INT32 DCTELEM;		/* must have 32 bits */
   43.36 +#endif
   43.37 +
   43.38 +typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
   43.39 +typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
   43.40 +
   43.41 +
   43.42 +/*
   43.43 + * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
   43.44 + * to an output sample array.  The routine must dequantize the input data as
   43.45 + * well as perform the IDCT; for dequantization, it uses the multiplier table
   43.46 + * pointed to by compptr->dct_table.  The output data is to be placed into the
   43.47 + * sample array starting at a specified column.  (Any row offset needed will
   43.48 + * be applied to the array pointer before it is passed to the IDCT code.)
   43.49 + * Note that the number of samples emitted by the IDCT routine is
   43.50 + * DCT_scaled_size * DCT_scaled_size.
   43.51 + */
   43.52 +
   43.53 +/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
   43.54 +
   43.55 +/*
   43.56 + * Each IDCT routine has its own ideas about the best dct_table element type.
   43.57 + */
   43.58 +
   43.59 +typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
   43.60 +#if BITS_IN_JSAMPLE == 8
   43.61 +typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
   43.62 +#define IFAST_SCALE_BITS  2	/* fractional bits in scale factors */
   43.63 +#else
   43.64 +typedef INT32 IFAST_MULT_TYPE;	/* need 32 bits for scaled quantizers */
   43.65 +#define IFAST_SCALE_BITS  13	/* fractional bits in scale factors */
   43.66 +#endif
   43.67 +typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
   43.68 +
   43.69 +
   43.70 +/*
   43.71 + * Each IDCT routine is responsible for range-limiting its results and
   43.72 + * converting them to unsigned form (0..MAXJSAMPLE).  The raw outputs could
   43.73 + * be quite far out of range if the input data is corrupt, so a bulletproof
   43.74 + * range-limiting step is required.  We use a mask-and-table-lookup method
   43.75 + * to do the combined operations quickly.  See the comments with
   43.76 + * prepare_range_limit_table (in jdmaster.c) for more info.
   43.77 + */
   43.78 +
   43.79 +#define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit + CENTERJSAMPLE)
   43.80 +
   43.81 +#define RANGE_MASK  (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
   43.82 +
   43.83 +
   43.84 +/* Short forms of external names for systems with brain-damaged linkers. */
   43.85 +
   43.86 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   43.87 +#define jpeg_fdct_islow		jFDislow
   43.88 +#define jpeg_fdct_ifast		jFDifast
   43.89 +#define jpeg_fdct_float		jFDfloat
   43.90 +#define jpeg_idct_islow		jRDislow
   43.91 +#define jpeg_idct_ifast		jRDifast
   43.92 +#define jpeg_idct_float		jRDfloat
   43.93 +#define jpeg_idct_4x4		jRD4x4
   43.94 +#define jpeg_idct_2x2		jRD2x2
   43.95 +#define jpeg_idct_1x1		jRD1x1
   43.96 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   43.97 +
   43.98 +/* Extern declarations for the forward and inverse DCT routines. */
   43.99 +
  43.100 +EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
  43.101 +EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
  43.102 +EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));
  43.103 +
  43.104 +EXTERN(void) jpeg_idct_islow
  43.105 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.106 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.107 +EXTERN(void) jpeg_idct_ifast
  43.108 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.109 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.110 +EXTERN(void) jpeg_idct_float
  43.111 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.112 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.113 +EXTERN(void) jpeg_idct_4x4
  43.114 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.115 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.116 +EXTERN(void) jpeg_idct_2x2
  43.117 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.118 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.119 +EXTERN(void) jpeg_idct_1x1
  43.120 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  43.121 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  43.122 +
  43.123 +
  43.124 +/*
  43.125 + * Macros for handling fixed-point arithmetic; these are used by many
  43.126 + * but not all of the DCT/IDCT modules.
  43.127 + *
  43.128 + * All values are expected to be of type INT32.
  43.129 + * Fractional constants are scaled left by CONST_BITS bits.
  43.130 + * CONST_BITS is defined within each module using these macros,
  43.131 + * and may differ from one module to the next.
  43.132 + */
  43.133 +
  43.134 +#define ONE	((INT32) 1)
  43.135 +#define CONST_SCALE (ONE << CONST_BITS)
  43.136 +
  43.137 +/* Convert a positive real constant to an integer scaled by CONST_SCALE.
  43.138 + * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
  43.139 + * thus causing a lot of useless floating-point operations at run time.
  43.140 + */
  43.141 +
  43.142 +#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))
  43.143 +
  43.144 +/* Descale and correctly round an INT32 value that's scaled by N bits.
  43.145 + * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  43.146 + * the fudge factor is correct for either sign of X.
  43.147 + */
  43.148 +
  43.149 +#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
  43.150 +
  43.151 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  43.152 + * This macro is used only when the two inputs will actually be no more than
  43.153 + * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
  43.154 + * full 32x32 multiply.  This provides a useful speedup on many machines.
  43.155 + * Unfortunately there is no way to specify a 16x16->32 multiply portably
  43.156 + * in C, but some C compilers will do the right thing if you provide the
  43.157 + * correct combination of casts.
  43.158 + */
  43.159 +
  43.160 +#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
  43.161 +#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
  43.162 +#endif
  43.163 +#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
  43.164 +#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT32) (const)))
  43.165 +#endif
  43.166 +
  43.167 +#ifndef MULTIPLY16C16		/* default definition */
  43.168 +#define MULTIPLY16C16(var,const)  ((var) * (const))
  43.169 +#endif
  43.170 +
  43.171 +/* Same except both inputs are variables. */
  43.172 +
  43.173 +#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
  43.174 +#define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
  43.175 +#endif
  43.176 +
  43.177 +#ifndef MULTIPLY16V16		/* default definition */
  43.178 +#define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
  43.179 +#endif
    44.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    44.2 +++ b/libs/libjpeg/jddctmgr.c	Sun Jun 07 17:25:49 2015 +0300
    44.3 @@ -0,0 +1,269 @@
    44.4 +/*
    44.5 + * jddctmgr.c
    44.6 + *
    44.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    44.8 + * This file is part of the Independent JPEG Group's software.
    44.9 + * For conditions of distribution and use, see the accompanying README file.
   44.10 + *
   44.11 + * This file contains the inverse-DCT management logic.
   44.12 + * This code selects a particular IDCT implementation to be used,
   44.13 + * and it performs related housekeeping chores.  No code in this file
   44.14 + * is executed per IDCT step, only during output pass setup.
   44.15 + *
   44.16 + * Note that the IDCT routines are responsible for performing coefficient
   44.17 + * dequantization as well as the IDCT proper.  This module sets up the
   44.18 + * dequantization multiplier table needed by the IDCT routine.
   44.19 + */
   44.20 +
   44.21 +#define JPEG_INTERNALS
   44.22 +#include "jinclude.h"
   44.23 +#include "jpeglib.h"
   44.24 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   44.25 +
   44.26 +
   44.27 +/*
   44.28 + * The decompressor input side (jdinput.c) saves away the appropriate
   44.29 + * quantization table for each component at the start of the first scan
   44.30 + * involving that component.  (This is necessary in order to correctly
   44.31 + * decode files that reuse Q-table slots.)
   44.32 + * When we are ready to make an output pass, the saved Q-table is converted
   44.33 + * to a multiplier table that will actually be used by the IDCT routine.
   44.34 + * The multiplier table contents are IDCT-method-dependent.  To support
   44.35 + * application changes in IDCT method between scans, we can remake the
   44.36 + * multiplier tables if necessary.
   44.37 + * In buffered-image mode, the first output pass may occur before any data
   44.38 + * has been seen for some components, and thus before their Q-tables have
   44.39 + * been saved away.  To handle this case, multiplier tables are preset
   44.40 + * to zeroes; the result of the IDCT will be a neutral gray level.
   44.41 + */
   44.42 +
   44.43 +
   44.44 +/* Private subobject for this module */
   44.45 +
   44.46 +typedef struct {
   44.47 +  struct jpeg_inverse_dct pub;	/* public fields */
   44.48 +
   44.49 +  /* This array contains the IDCT method code that each multiplier table
   44.50 +   * is currently set up for, or -1 if it's not yet set up.
   44.51 +   * The actual multiplier tables are pointed to by dct_table in the
   44.52 +   * per-component comp_info structures.
   44.53 +   */
   44.54 +  int cur_method[MAX_COMPONENTS];
   44.55 +} my_idct_controller;
   44.56 +
   44.57 +typedef my_idct_controller * my_idct_ptr;
   44.58 +
   44.59 +
   44.60 +/* Allocated multiplier tables: big enough for any supported variant */
   44.61 +
   44.62 +typedef union {
   44.63 +  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
   44.64 +#ifdef DCT_IFAST_SUPPORTED
   44.65 +  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
   44.66 +#endif
   44.67 +#ifdef DCT_FLOAT_SUPPORTED
   44.68 +  FLOAT_MULT_TYPE float_array[DCTSIZE2];
   44.69 +#endif
   44.70 +} multiplier_table;
   44.71 +
   44.72 +
   44.73 +/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
   44.74 + * so be sure to compile that code if either ISLOW or SCALING is requested.
   44.75 + */
   44.76 +#ifdef DCT_ISLOW_SUPPORTED
   44.77 +#define PROVIDE_ISLOW_TABLES
   44.78 +#else
   44.79 +#ifdef IDCT_SCALING_SUPPORTED
   44.80 +#define PROVIDE_ISLOW_TABLES
   44.81 +#endif
   44.82 +#endif
   44.83 +
   44.84 +
   44.85 +/*
   44.86 + * Prepare for an output pass.
   44.87 + * Here we select the proper IDCT routine for each component and build
   44.88 + * a matching multiplier table.
   44.89 + */
   44.90 +
   44.91 +METHODDEF(void)
   44.92 +start_pass (j_decompress_ptr cinfo)
   44.93 +{
   44.94 +  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
   44.95 +  int ci, i;
   44.96 +  jpeg_component_info *compptr;
   44.97 +  int method = 0;
   44.98 +  inverse_DCT_method_ptr method_ptr = NULL;
   44.99 +  JQUANT_TBL * qtbl;
  44.100 +
  44.101 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  44.102 +       ci++, compptr++) {
  44.103 +    /* Select the proper IDCT routine for this component's scaling */
  44.104 +    switch (compptr->DCT_scaled_size) {
  44.105 +#ifdef IDCT_SCALING_SUPPORTED
  44.106 +    case 1:
  44.107 +      method_ptr = jpeg_idct_1x1;
  44.108 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  44.109 +      break;
  44.110 +    case 2:
  44.111 +      method_ptr = jpeg_idct_2x2;
  44.112 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  44.113 +      break;
  44.114 +    case 4:
  44.115 +      method_ptr = jpeg_idct_4x4;
  44.116 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  44.117 +      break;
  44.118 +#endif
  44.119 +    case DCTSIZE:
  44.120 +      switch (cinfo->dct_method) {
  44.121 +#ifdef DCT_ISLOW_SUPPORTED
  44.122 +      case JDCT_ISLOW:
  44.123 +	method_ptr = jpeg_idct_islow;
  44.124 +	method = JDCT_ISLOW;
  44.125 +	break;
  44.126 +#endif
  44.127 +#ifdef DCT_IFAST_SUPPORTED
  44.128 +      case JDCT_IFAST:
  44.129 +	method_ptr = jpeg_idct_ifast;
  44.130 +	method = JDCT_IFAST;
  44.131 +	break;
  44.132 +#endif
  44.133 +#ifdef DCT_FLOAT_SUPPORTED
  44.134 +      case JDCT_FLOAT:
  44.135 +	method_ptr = jpeg_idct_float;
  44.136 +	method = JDCT_FLOAT;
  44.137 +	break;
  44.138 +#endif
  44.139 +      default:
  44.140 +	ERREXIT(cinfo, JERR_NOT_COMPILED);
  44.141 +	break;
  44.142 +      }
  44.143 +      break;
  44.144 +    default:
  44.145 +      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
  44.146 +      break;
  44.147 +    }
  44.148 +    idct->pub.inverse_DCT[ci] = method_ptr;
  44.149 +    /* Create multiplier table from quant table.
  44.150 +     * However, we can skip this if the component is uninteresting
  44.151 +     * or if we already built the table.  Also, if no quant table
  44.152 +     * has yet been saved for the component, we leave the
  44.153 +     * multiplier table all-zero; we'll be reading zeroes from the
  44.154 +     * coefficient controller's buffer anyway.
  44.155 +     */
  44.156 +    if (! compptr->component_needed || idct->cur_method[ci] == method)
  44.157 +      continue;
  44.158 +    qtbl = compptr->quant_table;
  44.159 +    if (qtbl == NULL)		/* happens if no data yet for component */
  44.160 +      continue;
  44.161 +    idct->cur_method[ci] = method;
  44.162 +    switch (method) {
  44.163 +#ifdef PROVIDE_ISLOW_TABLES
  44.164 +    case JDCT_ISLOW:
  44.165 +      {
  44.166 +	/* For LL&M IDCT method, multipliers are equal to raw quantization
  44.167 +	 * coefficients, but are stored as ints to ensure access efficiency.
  44.168 +	 */
  44.169 +	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  44.170 +	for (i = 0; i < DCTSIZE2; i++) {
  44.171 +	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
  44.172 +	}
  44.173 +      }
  44.174 +      break;
  44.175 +#endif
  44.176 +#ifdef DCT_IFAST_SUPPORTED
  44.177 +    case JDCT_IFAST:
  44.178 +      {
  44.179 +	/* For AA&N IDCT method, multipliers are equal to quantization
  44.180 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  44.181 +	 *   scalefactor[0] = 1
  44.182 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  44.183 +	 * For integer operation, the multiplier table is to be scaled by
  44.184 +	 * IFAST_SCALE_BITS.
  44.185 +	 */
  44.186 +	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  44.187 +#define CONST_BITS 14
  44.188 +	static const INT16 aanscales[DCTSIZE2] = {
  44.189 +	  /* precomputed values scaled up by 14 bits */
  44.190 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  44.191 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  44.192 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  44.193 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  44.194 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  44.195 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  44.196 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  44.197 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  44.198 +	};
  44.199 +	SHIFT_TEMPS
  44.200 +
  44.201 +	for (i = 0; i < DCTSIZE2; i++) {
  44.202 +	  ifmtbl[i] = (IFAST_MULT_TYPE)
  44.203 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  44.204 +				  (INT32) aanscales[i]),
  44.205 +		    CONST_BITS-IFAST_SCALE_BITS);
  44.206 +	}
  44.207 +      }
  44.208 +      break;
  44.209 +#endif
  44.210 +#ifdef DCT_FLOAT_SUPPORTED
  44.211 +    case JDCT_FLOAT:
  44.212 +      {
  44.213 +	/* For float AA&N IDCT method, multipliers are equal to quantization
  44.214 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  44.215 +	 *   scalefactor[0] = 1
  44.216 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  44.217 +	 */
  44.218 +	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  44.219 +	int row, col;
  44.220 +	static const double aanscalefactor[DCTSIZE] = {
  44.221 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
  44.222 +	  1.0, 0.785694958, 0.541196100, 0.275899379
  44.223 +	};
  44.224 +
  44.225 +	i = 0;
  44.226 +	for (row = 0; row < DCTSIZE; row++) {
  44.227 +	  for (col = 0; col < DCTSIZE; col++) {
  44.228 +	    fmtbl[i] = (FLOAT_MULT_TYPE)
  44.229 +	      ((double) qtbl->quantval[i] *
  44.230 +	       aanscalefactor[row] * aanscalefactor[col]);
  44.231 +	    i++;
  44.232 +	  }
  44.233 +	}
  44.234 +      }
  44.235 +      break;
  44.236 +#endif
  44.237 +    default:
  44.238 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  44.239 +      break;
  44.240 +    }
  44.241 +  }
  44.242 +}
  44.243 +
  44.244 +
  44.245 +/*
  44.246 + * Initialize IDCT manager.
  44.247 + */
  44.248 +
  44.249 +GLOBAL(void)
  44.250 +jinit_inverse_dct (j_decompress_ptr cinfo)
  44.251 +{
  44.252 +  my_idct_ptr idct;
  44.253 +  int ci;
  44.254 +  jpeg_component_info *compptr;
  44.255 +
  44.256 +  idct = (my_idct_ptr)
  44.257 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  44.258 +				SIZEOF(my_idct_controller));
  44.259 +  cinfo->idct = (struct jpeg_inverse_dct *) idct;
  44.260 +  idct->pub.start_pass = start_pass;
  44.261 +
  44.262 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  44.263 +       ci++, compptr++) {
  44.264 +    /* Allocate and pre-zero a multiplier table for each component */
  44.265 +    compptr->dct_table =
  44.266 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  44.267 +				  SIZEOF(multiplier_table));
  44.268 +    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
  44.269 +    /* Mark multiplier table not yet set up for any method */
  44.270 +    idct->cur_method[ci] = -1;
  44.271 +  }
  44.272 +}
    45.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    45.2 +++ b/libs/libjpeg/jdhuff.c	Sun Jun 07 17:25:49 2015 +0300
    45.3 @@ -0,0 +1,651 @@
    45.4 +/*
    45.5 + * jdhuff.c
    45.6 + *
    45.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    45.8 + * This file is part of the Independent JPEG Group's software.
    45.9 + * For conditions of distribution and use, see the accompanying README file.
   45.10 + *
   45.11 + * This file contains Huffman entropy decoding routines.
   45.12 + *
   45.13 + * Much of the complexity here has to do with supporting input suspension.
   45.14 + * If the data source module demands suspension, we want to be able to back
   45.15 + * up to the start of the current MCU.  To do this, we copy state variables
   45.16 + * into local working storage, and update them back to the permanent
   45.17 + * storage only upon successful completion of an MCU.
   45.18 + */
   45.19 +
   45.20 +#define JPEG_INTERNALS
   45.21 +#include "jinclude.h"
   45.22 +#include "jpeglib.h"
   45.23 +#include "jdhuff.h"		/* Declarations shared with jdphuff.c */
   45.24 +
   45.25 +
   45.26 +/*
   45.27 + * Expanded entropy decoder object for Huffman decoding.
   45.28 + *
   45.29 + * The savable_state subrecord contains fields that change within an MCU,
   45.30 + * but must not be updated permanently until we complete the MCU.
   45.31 + */
   45.32 +
   45.33 +typedef struct {
   45.34 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   45.35 +} savable_state;
   45.36 +
   45.37 +/* This macro is to work around compilers with missing or broken
   45.38 + * structure assignment.  You'll need to fix this code if you have
   45.39 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   45.40 + */
   45.41 +
   45.42 +#ifndef NO_STRUCT_ASSIGN
   45.43 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   45.44 +#else
   45.45 +#if MAX_COMPS_IN_SCAN == 4
   45.46 +#define ASSIGN_STATE(dest,src)  \
   45.47 +	((dest).last_dc_val[0] = (src).last_dc_val[0], \
   45.48 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   45.49 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   45.50 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   45.51 +#endif
   45.52 +#endif
   45.53 +
   45.54 +
   45.55 +typedef struct {
   45.56 +  struct jpeg_entropy_decoder pub; /* public fields */
   45.57 +
   45.58 +  /* These fields are loaded into local variables at start of each MCU.
   45.59 +   * In case of suspension, we exit WITHOUT updating them.
   45.60 +   */
   45.61 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
   45.62 +  savable_state saved;		/* Other state at start of MCU */
   45.63 +
   45.64 +  /* These fields are NOT loaded into local working state. */
   45.65 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   45.66 +
   45.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   45.68 +  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   45.69 +  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   45.70 +
   45.71 +  /* Precalculated info set up by start_pass for use in decode_mcu: */
   45.72 +
   45.73 +  /* Pointers to derived tables to be used for each block within an MCU */
   45.74 +  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   45.75 +  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   45.76 +  /* Whether we care about the DC and AC coefficient values for each block */
   45.77 +  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
   45.78 +  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
   45.79 +} huff_entropy_decoder;
   45.80 +
   45.81 +typedef huff_entropy_decoder * huff_entropy_ptr;
   45.82 +
   45.83 +
   45.84 +/*
   45.85 + * Initialize for a Huffman-compressed scan.
   45.86 + */
   45.87 +
   45.88 +METHODDEF(void)
   45.89 +start_pass_huff_decoder (j_decompress_ptr cinfo)
   45.90 +{
   45.91 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   45.92 +  int ci, blkn, dctbl, actbl;
   45.93 +  jpeg_component_info * compptr;
   45.94 +
   45.95 +  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
   45.96 +   * This ought to be an error condition, but we make it a warning because
   45.97 +   * there are some baseline files out there with all zeroes in these bytes.
   45.98 +   */
   45.99 +  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
  45.100 +      cinfo->Ah != 0 || cinfo->Al != 0)
  45.101 +    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  45.102 +
  45.103 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  45.104 +    compptr = cinfo->cur_comp_info[ci];
  45.105 +    dctbl = compptr->dc_tbl_no;
  45.106 +    actbl = compptr->ac_tbl_no;
  45.107 +    /* Compute derived values for Huffman tables */
  45.108 +    /* We may do this more than once for a table, but it's not expensive */
  45.109 +    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
  45.110 +			    & entropy->dc_derived_tbls[dctbl]);
  45.111 +    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
  45.112 +			    & entropy->ac_derived_tbls[actbl]);
  45.113 +    /* Initialize DC predictions to 0 */
  45.114 +    entropy->saved.last_dc_val[ci] = 0;
  45.115 +  }
  45.116 +
  45.117 +  /* Precalculate decoding info for each block in an MCU of this scan */
  45.118 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  45.119 +    ci = cinfo->MCU_membership[blkn];
  45.120 +    compptr = cinfo->cur_comp_info[ci];
  45.121 +    /* Precalculate which table to use for each block */
  45.122 +    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  45.123 +    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  45.124 +    /* Decide whether we really care about the coefficient values */
  45.125 +    if (compptr->component_needed) {
  45.126 +      entropy->dc_needed[blkn] = TRUE;
  45.127 +      /* we don't need the ACs if producing a 1/8th-size image */
  45.128 +      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
  45.129 +    } else {
  45.130 +      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
  45.131 +    }
  45.132 +  }
  45.133 +
  45.134 +  /* Initialize bitread state variables */
  45.135 +  entropy->bitstate.bits_left = 0;
  45.136 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  45.137 +  entropy->pub.insufficient_data = FALSE;
  45.138 +
  45.139 +  /* Initialize restart counter */
  45.140 +  entropy->restarts_to_go = cinfo->restart_interval;
  45.141 +}
  45.142 +
  45.143 +
  45.144 +/*
  45.145 + * Compute the derived values for a Huffman table.
  45.146 + * This routine also performs some validation checks on the table.
  45.147 + *
  45.148 + * Note this is also used by jdphuff.c.
  45.149 + */
  45.150 +
  45.151 +GLOBAL(void)
  45.152 +jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
  45.153 +			 d_derived_tbl ** pdtbl)
  45.154 +{
  45.155 +  JHUFF_TBL *htbl;
  45.156 +  d_derived_tbl *dtbl;
  45.157 +  int p, i, l, si, numsymbols;
  45.158 +  int lookbits, ctr;
  45.159 +  char huffsize[257];
  45.160 +  unsigned int huffcode[257];
  45.161 +  unsigned int code;
  45.162 +
  45.163 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
  45.164 +   * paralleling the order of the symbols themselves in htbl->huffval[].
  45.165 +   */
  45.166 +
  45.167 +  /* Find the input Huffman table */
  45.168 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  45.169 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  45.170 +  htbl =
  45.171 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  45.172 +  if (htbl == NULL)
  45.173 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  45.174 +
  45.175 +  /* Allocate a workspace if we haven't already done so. */
  45.176 +  if (*pdtbl == NULL)
  45.177 +    *pdtbl = (d_derived_tbl *)
  45.178 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.179 +				  SIZEOF(d_derived_tbl));
  45.180 +  dtbl = *pdtbl;
  45.181 +  dtbl->pub = htbl;		/* fill in back link */
  45.182 +  
  45.183 +  /* Figure C.1: make table of Huffman code length for each symbol */
  45.184 +
  45.185 +  p = 0;
  45.186 +  for (l = 1; l <= 16; l++) {
  45.187 +    i = (int) htbl->bits[l];
  45.188 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
  45.189 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  45.190 +    while (i--)
  45.191 +      huffsize[p++] = (char) l;
  45.192 +  }
  45.193 +  huffsize[p] = 0;
  45.194 +  numsymbols = p;
  45.195 +  
  45.196 +  /* Figure C.2: generate the codes themselves */
  45.197 +  /* We also validate that the counts represent a legal Huffman code tree. */
  45.198 +  
  45.199 +  code = 0;
  45.200 +  si = huffsize[0];
  45.201 +  p = 0;
  45.202 +  while (huffsize[p]) {
  45.203 +    while (((int) huffsize[p]) == si) {
  45.204 +      huffcode[p++] = code;
  45.205 +      code++;
  45.206 +    }
  45.207 +    /* code is now 1 more than the last code used for codelength si; but
  45.208 +     * it must still fit in si bits, since no code is allowed to be all ones.
  45.209 +     */
  45.210 +    if (((INT32) code) >= (((INT32) 1) << si))
  45.211 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  45.212 +    code <<= 1;
  45.213 +    si++;
  45.214 +  }
  45.215 +
  45.216 +  /* Figure F.15: generate decoding tables for bit-sequential decoding */
  45.217 +
  45.218 +  p = 0;
  45.219 +  for (l = 1; l <= 16; l++) {
  45.220 +    if (htbl->bits[l]) {
  45.221 +      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
  45.222 +       * minus the minimum code of length l
  45.223 +       */
  45.224 +      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
  45.225 +      p += htbl->bits[l];
  45.226 +      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
  45.227 +    } else {
  45.228 +      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
  45.229 +    }
  45.230 +  }
  45.231 +  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
  45.232 +
  45.233 +  /* Compute lookahead tables to speed up decoding.
  45.234 +   * First we set all the table entries to 0, indicating "too long";
  45.235 +   * then we iterate through the Huffman codes that are short enough and
  45.236 +   * fill in all the entries that correspond to bit sequences starting
  45.237 +   * with that code.
  45.238 +   */
  45.239 +
  45.240 +  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
  45.241 +
  45.242 +  p = 0;
  45.243 +  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
  45.244 +    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
  45.245 +      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
  45.246 +      /* Generate left-justified code followed by all possible bit sequences */
  45.247 +      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
  45.248 +      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
  45.249 +	dtbl->look_nbits[lookbits] = l;
  45.250 +	dtbl->look_sym[lookbits] = htbl->huffval[p];
  45.251 +	lookbits++;
  45.252 +      }
  45.253 +    }
  45.254 +  }
  45.255 +
  45.256 +  /* Validate symbols as being reasonable.
  45.257 +   * For AC tables, we make no check, but accept all byte values 0..255.
  45.258 +   * For DC tables, we require the symbols to be in range 0..15.
  45.259 +   * (Tighter bounds could be applied depending on the data depth and mode,
  45.260 +   * but this is sufficient to ensure safe decoding.)
  45.261 +   */
  45.262 +  if (isDC) {
  45.263 +    for (i = 0; i < numsymbols; i++) {
  45.264 +      int sym = htbl->huffval[i];
  45.265 +      if (sym < 0 || sym > 15)
  45.266 +	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  45.267 +    }
  45.268 +  }
  45.269 +}
  45.270 +
  45.271 +
  45.272 +/*
  45.273 + * Out-of-line code for bit fetching (shared with jdphuff.c).
  45.274 + * See jdhuff.h for info about usage.
  45.275 + * Note: current values of get_buffer and bits_left are passed as parameters,
  45.276 + * but are returned in the corresponding fields of the state struct.
  45.277 + *
  45.278 + * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  45.279 + * of get_buffer to be used.  (On machines with wider words, an even larger
  45.280 + * buffer could be used.)  However, on some machines 32-bit shifts are
  45.281 + * quite slow and take time proportional to the number of places shifted.
  45.282 + * (This is true with most PC compilers, for instance.)  In this case it may
  45.283 + * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
  45.284 + * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
  45.285 + */
  45.286 +
  45.287 +#ifdef SLOW_SHIFT_32
  45.288 +#define MIN_GET_BITS  15	/* minimum allowable value */
  45.289 +#else
  45.290 +#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
  45.291 +#endif
  45.292 +
  45.293 +
  45.294 +GLOBAL(boolean)
  45.295 +jpeg_fill_bit_buffer (bitread_working_state * state,
  45.296 +		      register bit_buf_type get_buffer, register int bits_left,
  45.297 +		      int nbits)
  45.298 +/* Load up the bit buffer to a depth of at least nbits */
  45.299 +{
  45.300 +  /* Copy heavily used state fields into locals (hopefully registers) */
  45.301 +  register const JOCTET * next_input_byte = state->next_input_byte;
  45.302 +  register size_t bytes_in_buffer = state->bytes_in_buffer;
  45.303 +  j_decompress_ptr cinfo = state->cinfo;
  45.304 +
  45.305 +  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
  45.306 +  /* (It is assumed that no request will be for more than that many bits.) */
  45.307 +  /* We fail to do so only if we hit a marker or are forced to suspend. */
  45.308 +
  45.309 +  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
  45.310 +    while (bits_left < MIN_GET_BITS) {
  45.311 +      register int c;
  45.312 +
  45.313 +      /* Attempt to read a byte */
  45.314 +      if (bytes_in_buffer == 0) {
  45.315 +	if (! (*cinfo->src->fill_input_buffer) (cinfo))
  45.316 +	  return FALSE;
  45.317 +	next_input_byte = cinfo->src->next_input_byte;
  45.318 +	bytes_in_buffer = cinfo->src->bytes_in_buffer;
  45.319 +      }
  45.320 +      bytes_in_buffer--;
  45.321 +      c = GETJOCTET(*next_input_byte++);
  45.322 +
  45.323 +      /* If it's 0xFF, check and discard stuffed zero byte */
  45.324 +      if (c == 0xFF) {
  45.325 +	/* Loop here to discard any padding FF's on terminating marker,
  45.326 +	 * so that we can save a valid unread_marker value.  NOTE: we will
  45.327 +	 * accept multiple FF's followed by a 0 as meaning a single FF data
  45.328 +	 * byte.  This data pattern is not valid according to the standard.
  45.329 +	 */
  45.330 +	do {
  45.331 +	  if (bytes_in_buffer == 0) {
  45.332 +	    if (! (*cinfo->src->fill_input_buffer) (cinfo))
  45.333 +	      return FALSE;
  45.334 +	    next_input_byte = cinfo->src->next_input_byte;
  45.335 +	    bytes_in_buffer = cinfo->src->bytes_in_buffer;
  45.336 +	  }
  45.337 +	  bytes_in_buffer--;
  45.338 +	  c = GETJOCTET(*next_input_byte++);
  45.339 +	} while (c == 0xFF);
  45.340 +
  45.341 +	if (c == 0) {
  45.342 +	  /* Found FF/00, which represents an FF data byte */
  45.343 +	  c = 0xFF;
  45.344 +	} else {
  45.345 +	  /* Oops, it's actually a marker indicating end of compressed data.
  45.346 +	   * Save the marker code for later use.
  45.347 +	   * Fine point: it might appear that we should save the marker into
  45.348 +	   * bitread working state, not straight into permanent state.  But
  45.349 +	   * once we have hit a marker, we cannot need to suspend within the
  45.350 +	   * current MCU, because we will read no more bytes from the data
  45.351 +	   * source.  So it is OK to update permanent state right away.
  45.352 +	   */
  45.353 +	  cinfo->unread_marker = c;
  45.354 +	  /* See if we need to insert some fake zero bits. */
  45.355 +	  goto no_more_bytes;
  45.356 +	}
  45.357 +      }
  45.358 +
  45.359 +      /* OK, load c into get_buffer */
  45.360 +      get_buffer = (get_buffer << 8) | c;
  45.361 +      bits_left += 8;
  45.362 +    } /* end while */
  45.363 +  } else {
  45.364 +  no_more_bytes:
  45.365 +    /* We get here if we've read the marker that terminates the compressed
  45.366 +     * data segment.  There should be enough bits in the buffer register
  45.367 +     * to satisfy the request; if so, no problem.
  45.368 +     */
  45.369 +    if (nbits > bits_left) {
  45.370 +      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
  45.371 +       * the data stream, so that we can produce some kind of image.
  45.372 +       * We use a nonvolatile flag to ensure that only one warning message
  45.373 +       * appears per data segment.
  45.374 +       */
  45.375 +      if (! cinfo->entropy->insufficient_data) {
  45.376 +	WARNMS(cinfo, JWRN_HIT_MARKER);
  45.377 +	cinfo->entropy->insufficient_data = TRUE;
  45.378 +      }
  45.379 +      /* Fill the buffer with zero bits */
  45.380 +      get_buffer <<= MIN_GET_BITS - bits_left;
  45.381 +      bits_left = MIN_GET_BITS;
  45.382 +    }
  45.383 +  }
  45.384 +
  45.385 +  /* Unload the local registers */
  45.386 +  state->next_input_byte = next_input_byte;
  45.387 +  state->bytes_in_buffer = bytes_in_buffer;
  45.388 +  state->get_buffer = get_buffer;
  45.389 +  state->bits_left = bits_left;
  45.390 +
  45.391 +  return TRUE;
  45.392 +}
  45.393 +
  45.394 +
  45.395 +/*
  45.396 + * Out-of-line code for Huffman code decoding.
  45.397 + * See jdhuff.h for info about usage.
  45.398 + */
  45.399 +
  45.400 +GLOBAL(int)
  45.401 +jpeg_huff_decode (bitread_working_state * state,
  45.402 +		  register bit_buf_type get_buffer, register int bits_left,
  45.403 +		  d_derived_tbl * htbl, int min_bits)
  45.404 +{
  45.405 +  register int l = min_bits;
  45.406 +  register INT32 code;
  45.407 +
  45.408 +  /* HUFF_DECODE has determined that the code is at least min_bits */
  45.409 +  /* bits long, so fetch that many bits in one swoop. */
  45.410 +
  45.411 +  CHECK_BIT_BUFFER(*state, l, return -1);
  45.412 +  code = GET_BITS(l);
  45.413 +
  45.414 +  /* Collect the rest of the Huffman code one bit at a time. */
  45.415 +  /* This is per Figure F.16 in the JPEG spec. */
  45.416 +
  45.417 +  while (code > htbl->maxcode[l]) {
  45.418 +    code <<= 1;
  45.419 +    CHECK_BIT_BUFFER(*state, 1, return -1);
  45.420 +    code |= GET_BITS(1);
  45.421 +    l++;
  45.422 +  }
  45.423 +
  45.424 +  /* Unload the local registers */
  45.425 +  state->get_buffer = get_buffer;
  45.426 +  state->bits_left = bits_left;
  45.427 +
  45.428 +  /* With garbage input we may reach the sentinel value l = 17. */
  45.429 +
  45.430 +  if (l > 16) {
  45.431 +    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
  45.432 +    return 0;			/* fake a zero as the safest result */
  45.433 +  }
  45.434 +
  45.435 +  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
  45.436 +}
  45.437 +
  45.438 +
  45.439 +/*
  45.440 + * Figure F.12: extend sign bit.
  45.441 + * On some machines, a shift and add will be faster than a table lookup.
  45.442 + */
  45.443 +
  45.444 +#ifdef AVOID_TABLES
  45.445 +
  45.446 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  45.447 +
  45.448 +#else
  45.449 +
  45.450 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  45.451 +
  45.452 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
  45.453 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  45.454 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  45.455 +
  45.456 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  45.457 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  45.458 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  45.459 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  45.460 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  45.461 +
  45.462 +#endif /* AVOID_TABLES */
  45.463 +
  45.464 +
  45.465 +/*
  45.466 + * Check for a restart marker & resynchronize decoder.
  45.467 + * Returns FALSE if must suspend.
  45.468 + */
  45.469 +
  45.470 +LOCAL(boolean)
  45.471 +process_restart (j_decompress_ptr cinfo)
  45.472 +{
  45.473 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  45.474 +  int ci;
  45.475 +
  45.476 +  /* Throw away any unused bits remaining in bit buffer; */
  45.477 +  /* include any full bytes in next_marker's count of discarded bytes */
  45.478 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  45.479 +  entropy->bitstate.bits_left = 0;
  45.480 +
  45.481 +  /* Advance past the RSTn marker */
  45.482 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
  45.483 +    return FALSE;
  45.484 +
  45.485 +  /* Re-initialize DC predictions to 0 */
  45.486 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  45.487 +    entropy->saved.last_dc_val[ci] = 0;
  45.488 +
  45.489 +  /* Reset restart counter */
  45.490 +  entropy->restarts_to_go = cinfo->restart_interval;
  45.491 +
  45.492 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
  45.493 +   * against a marker.  In that case we will end up treating the next data
  45.494 +   * segment as empty, and we can avoid producing bogus output pixels by
  45.495 +   * leaving the flag set.
  45.496 +   */
  45.497 +  if (cinfo->unread_marker == 0)
  45.498 +    entropy->pub.insufficient_data = FALSE;
  45.499 +
  45.500 +  return TRUE;
  45.501 +}
  45.502 +
  45.503 +
  45.504 +/*
  45.505 + * Decode and return one MCU's worth of Huffman-compressed coefficients.
  45.506 + * The coefficients are reordered from zigzag order into natural array order,
  45.507 + * but are not dequantized.
  45.508 + *
  45.509 + * The i'th block of the MCU is stored into the block pointed to by
  45.510 + * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  45.511 + * (Wholesale zeroing is usually a little faster than retail...)
  45.512 + *
  45.513 + * Returns FALSE if data source requested suspension.  In that case no
  45.514 + * changes have been made to permanent state.  (Exception: some output
  45.515 + * coefficients may already have been assigned.  This is harmless for
  45.516 + * this module, since we'll just re-assign them on the next call.)
  45.517 + */
  45.518 +
  45.519 +METHODDEF(boolean)
  45.520 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  45.521 +{
  45.522 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  45.523 +  int blkn;
  45.524 +  BITREAD_STATE_VARS;
  45.525 +  savable_state state;
  45.526 +
  45.527 +  /* Process restart marker if needed; may have to suspend */
  45.528 +  if (cinfo->restart_interval) {
  45.529 +    if (entropy->restarts_to_go == 0)
  45.530 +      if (! process_restart(cinfo))
  45.531 +	return FALSE;
  45.532 +  }
  45.533 +
  45.534 +  /* If we've run out of data, just leave the MCU set to zeroes.
  45.535 +   * This way, we return uniform gray for the remainder of the segment.
  45.536 +   */
  45.537 +  if (! entropy->pub.insufficient_data) {
  45.538 +
  45.539 +    /* Load up working state */
  45.540 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  45.541 +    ASSIGN_STATE(state, entropy->saved);
  45.542 +
  45.543 +    /* Outer loop handles each block in the MCU */
  45.544 +
  45.545 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  45.546 +      JBLOCKROW block = MCU_data[blkn];
  45.547 +      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
  45.548 +      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
  45.549 +      register int s, k, r;
  45.550 +
  45.551 +      /* Decode a single block's worth of coefficients */
  45.552 +
  45.553 +      /* Section F.2.2.1: decode the DC coefficient difference */
  45.554 +      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
  45.555 +      if (s) {
  45.556 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
  45.557 +	r = GET_BITS(s);
  45.558 +	s = HUFF_EXTEND(r, s);
  45.559 +      }
  45.560 +
  45.561 +      if (entropy->dc_needed[blkn]) {
  45.562 +	/* Convert DC difference to actual value, update last_dc_val */
  45.563 +	int ci = cinfo->MCU_membership[blkn];
  45.564 +	s += state.last_dc_val[ci];
  45.565 +	state.last_dc_val[ci] = s;
  45.566 +	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
  45.567 +	(*block)[0] = (JCOEF) s;
  45.568 +      }
  45.569 +
  45.570 +      if (entropy->ac_needed[blkn]) {
  45.571 +
  45.572 +	/* Section F.2.2.2: decode the AC coefficients */
  45.573 +	/* Since zeroes are skipped, output area must be cleared beforehand */
  45.574 +	for (k = 1; k < DCTSIZE2; k++) {
  45.575 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
  45.576 +      
  45.577 +	  r = s >> 4;
  45.578 +	  s &= 15;
  45.579 +      
  45.580 +	  if (s) {
  45.581 +	    k += r;
  45.582 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
  45.583 +	    r = GET_BITS(s);
  45.584 +	    s = HUFF_EXTEND(r, s);
  45.585 +	    /* Output coefficient in natural (dezigzagged) order.
  45.586 +	     * Note: the extra entries in jpeg_natural_order[] will save us
  45.587 +	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
  45.588 +	     */
  45.589 +	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
  45.590 +	  } else {
  45.591 +	    if (r != 15)
  45.592 +	      break;
  45.593 +	    k += 15;
  45.594 +	  }
  45.595 +	}
  45.596 +
  45.597 +      } else {
  45.598 +
  45.599 +	/* Section F.2.2.2: decode the AC coefficients */
  45.600 +	/* In this path we just discard the values */
  45.601 +	for (k = 1; k < DCTSIZE2; k++) {
  45.602 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
  45.603 +      
  45.604 +	  r = s >> 4;
  45.605 +	  s &= 15;
  45.606 +      
  45.607 +	  if (s) {
  45.608 +	    k += r;
  45.609 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
  45.610 +	    DROP_BITS(s);
  45.611 +	  } else {
  45.612 +	    if (r != 15)
  45.613 +	      break;
  45.614 +	    k += 15;
  45.615 +	  }
  45.616 +	}
  45.617 +
  45.618 +      }
  45.619 +    }
  45.620 +
  45.621 +    /* Completed MCU, so update state */
  45.622 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  45.623 +    ASSIGN_STATE(entropy->saved, state);
  45.624 +  }
  45.625 +
  45.626 +  /* Account for restart interval (no-op if not using restarts) */
  45.627 +  entropy->restarts_to_go--;
  45.628 +
  45.629 +  return TRUE;
  45.630 +}
  45.631 +
  45.632 +
  45.633 +/*
  45.634 + * Module initialization routine for Huffman entropy decoding.
  45.635 + */
  45.636 +
  45.637 +GLOBAL(void)
  45.638 +jinit_huff_decoder (j_decompress_ptr cinfo)
  45.639 +{
  45.640 +  huff_entropy_ptr entropy;
  45.641 +  int i;
  45.642 +
  45.643 +  entropy = (huff_entropy_ptr)
  45.644 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.645 +				SIZEOF(huff_entropy_decoder));
  45.646 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  45.647 +  entropy->pub.start_pass = start_pass_huff_decoder;
  45.648 +  entropy->pub.decode_mcu = decode_mcu;
  45.649 +
  45.650 +  /* Mark tables unallocated */
  45.651 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  45.652 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  45.653 +  }
  45.654 +}
    46.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    46.2 +++ b/libs/libjpeg/jdhuff.h	Sun Jun 07 17:25:49 2015 +0300
    46.3 @@ -0,0 +1,201 @@
    46.4 +/*
    46.5 + * jdhuff.h
    46.6 + *
    46.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    46.8 + * This file is part of the Independent JPEG Group's software.
    46.9 + * For conditions of distribution and use, see the accompanying README file.
   46.10 + *
   46.11 + * This file contains declarations for Huffman entropy decoding routines
   46.12 + * that are shared between the sequential decoder (jdhuff.c) and the
   46.13 + * progressive decoder (jdphuff.c).  No other modules need to see these.
   46.14 + */
   46.15 +
   46.16 +/* Short forms of external names for systems with brain-damaged linkers. */
   46.17 +
   46.18 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   46.19 +#define jpeg_make_d_derived_tbl	jMkDDerived
   46.20 +#define jpeg_fill_bit_buffer	jFilBitBuf
   46.21 +#define jpeg_huff_decode	jHufDecode
   46.22 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   46.23 +
   46.24 +
   46.25 +/* Derived data constructed for each Huffman table */
   46.26 +
   46.27 +#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
   46.28 +
   46.29 +typedef struct {
   46.30 +  /* Basic tables: (element [0] of each array is unused) */
   46.31 +  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
   46.32 +  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
   46.33 +  INT32 valoffset[17];		/* huffval[] offset for codes of length k */
   46.34 +  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
   46.35 +   * the smallest code of length k; so given a code of length k, the
   46.36 +   * corresponding symbol is huffval[code + valoffset[k]]
   46.37 +   */
   46.38 +
   46.39 +  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
   46.40 +  JHUFF_TBL *pub;
   46.41 +
   46.42 +  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
   46.43 +   * the input data stream.  If the next Huffman code is no more
   46.44 +   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
   46.45 +   * the corresponding symbol directly from these tables.
   46.46 +   */
   46.47 +  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
   46.48 +  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
   46.49 +} d_derived_tbl;
   46.50 +
   46.51 +/* Expand a Huffman table definition into the derived format */
   46.52 +EXTERN(void) jpeg_make_d_derived_tbl
   46.53 +	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
   46.54 +	     d_derived_tbl ** pdtbl));
   46.55 +
   46.56 +
   46.57 +/*
   46.58 + * Fetching the next N bits from the input stream is a time-critical operation
   46.59 + * for the Huffman decoders.  We implement it with a combination of inline
   46.60 + * macros and out-of-line subroutines.  Note that N (the number of bits
   46.61 + * demanded at one time) never exceeds 15 for JPEG use.
   46.62 + *
   46.63 + * We read source bytes into get_buffer and dole out bits as needed.
   46.64 + * If get_buffer already contains enough bits, they are fetched in-line
   46.65 + * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
   46.66 + * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
   46.67 + * as full as possible (not just to the number of bits needed; this
   46.68 + * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
   46.69 + * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
   46.70 + * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
   46.71 + * at least the requested number of bits --- dummy zeroes are inserted if
   46.72 + * necessary.
   46.73 + */
   46.74 +
   46.75 +typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
   46.76 +#define BIT_BUF_SIZE  32	/* size of buffer in bits */
   46.77 +
   46.78 +/* If long is > 32 bits on your machine, and shifting/masking longs is
   46.79 + * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
   46.80 + * appropriately should be a win.  Unfortunately we can't define the size
   46.81 + * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
   46.82 + * because not all machines measure sizeof in 8-bit bytes.
   46.83 + */
   46.84 +
   46.85 +typedef struct {		/* Bitreading state saved across MCUs */
   46.86 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   46.87 +  int bits_left;		/* # of unused bits in it */
   46.88 +} bitread_perm_state;
   46.89 +
   46.90 +typedef struct {		/* Bitreading working state within an MCU */
   46.91 +  /* Current data source location */
   46.92 +  /* We need a copy, rather than munging the original, in case of suspension */
   46.93 +  const JOCTET * next_input_byte; /* => next byte to read from source */
   46.94 +  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
   46.95 +  /* Bit input buffer --- note these values are kept in register variables,
   46.96 +   * not in this struct, inside the inner loops.
   46.97 +   */
   46.98 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   46.99 +  int bits_left;		/* # of unused bits in it */
  46.100 +  /* Pointer needed by jpeg_fill_bit_buffer. */
  46.101 +  j_decompress_ptr cinfo;	/* back link to decompress master record */
  46.102 +} bitread_working_state;
  46.103 +
  46.104 +/* Macros to declare and load/save bitread local variables. */
  46.105 +#define BITREAD_STATE_VARS  \
  46.106 +	register bit_buf_type get_buffer;  \
  46.107 +	register int bits_left;  \
  46.108 +	bitread_working_state br_state
  46.109 +
  46.110 +#define BITREAD_LOAD_STATE(cinfop,permstate)  \
  46.111 +	br_state.cinfo = cinfop; \
  46.112 +	br_state.next_input_byte = cinfop->src->next_input_byte; \
  46.113 +	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
  46.114 +	get_buffer = permstate.get_buffer; \
  46.115 +	bits_left = permstate.bits_left;
  46.116 +
  46.117 +#define BITREAD_SAVE_STATE(cinfop,permstate)  \
  46.118 +	cinfop->src->next_input_byte = br_state.next_input_byte; \
  46.119 +	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
  46.120 +	permstate.get_buffer = get_buffer; \
  46.121 +	permstate.bits_left = bits_left
  46.122 +
  46.123 +/*
  46.124 + * These macros provide the in-line portion of bit fetching.
  46.125 + * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
  46.126 + * before using GET_BITS, PEEK_BITS, or DROP_BITS.
  46.127 + * The variables get_buffer and bits_left are assumed to be locals,
  46.128 + * but the state struct might not be (jpeg_huff_decode needs this).
  46.129 + *	CHECK_BIT_BUFFER(state,n,action);
  46.130 + *		Ensure there are N bits in get_buffer; if suspend, take action.
  46.131 + *      val = GET_BITS(n);
  46.132 + *		Fetch next N bits.
  46.133 + *      val = PEEK_BITS(n);
  46.134 + *		Fetch next N bits without removing them from the buffer.
  46.135 + *	DROP_BITS(n);
  46.136 + *		Discard next N bits.
  46.137 + * The value N should be a simple variable, not an expression, because it
  46.138 + * is evaluated multiple times.
  46.139 + */
  46.140 +
  46.141 +#define CHECK_BIT_BUFFER(state,nbits,action) \
  46.142 +	{ if (bits_left < (nbits)) {  \
  46.143 +	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
  46.144 +	      { action; }  \
  46.145 +	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
  46.146 +
  46.147 +#define GET_BITS(nbits) \
  46.148 +	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
  46.149 +
  46.150 +#define PEEK_BITS(nbits) \
  46.151 +	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))
  46.152 +
  46.153 +#define DROP_BITS(nbits) \
  46.154 +	(bits_left -= (nbits))
  46.155 +
  46.156 +/* Load up the bit buffer to a depth of at least nbits */
  46.157 +EXTERN(boolean) jpeg_fill_bit_buffer
  46.158 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
  46.159 +	     register int bits_left, int nbits));
  46.160 +
  46.161 +
  46.162 +/*
  46.163 + * Code for extracting next Huffman-coded symbol from input bit stream.
  46.164 + * Again, this is time-critical and we make the main paths be macros.
  46.165 + *
  46.166 + * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
  46.167 + * without looping.  Usually, more than 95% of the Huffman codes will be 8
  46.168 + * or fewer bits long.  The few overlength codes are handled with a loop,
  46.169 + * which need not be inline code.
  46.170 + *
  46.171 + * Notes about the HUFF_DECODE macro:
  46.172 + * 1. Near the end of the data segment, we may fail to get enough bits
  46.173 + *    for a lookahead.  In that case, we do it the hard way.
  46.174 + * 2. If the lookahead table contains no entry, the next code must be
  46.175 + *    more than HUFF_LOOKAHEAD bits long.
  46.176 + * 3. jpeg_huff_decode returns -1 if forced to suspend.
  46.177 + */
  46.178 +
  46.179 +#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
  46.180 +{ register int nb, look; \
  46.181 +  if (bits_left < HUFF_LOOKAHEAD) { \
  46.182 +    if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
  46.183 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
  46.184 +    if (bits_left < HUFF_LOOKAHEAD) { \
  46.185 +      nb = 1; goto slowlabel; \
  46.186 +    } \
  46.187 +  } \
  46.188 +  look = PEEK_BITS(HUFF_LOOKAHEAD); \
  46.189 +  if ((nb = htbl->look_nbits[look]) != 0) { \
  46.190 +    DROP_BITS(nb); \
  46.191 +    result = htbl->look_sym[look]; \
  46.192 +  } else { \
  46.193 +    nb = HUFF_LOOKAHEAD+1; \
  46.194 +slowlabel: \
  46.195 +    if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
  46.196 +	{ failaction; } \
  46.197 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
  46.198 +  } \
  46.199 +}
  46.200 +
  46.201 +/* Out-of-line case for Huffman code fetching */
  46.202 +EXTERN(int) jpeg_huff_decode
  46.203 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
  46.204 +	     register int bits_left, d_derived_tbl * htbl, int min_bits));
    47.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    47.2 +++ b/libs/libjpeg/jdinput.c	Sun Jun 07 17:25:49 2015 +0300
    47.3 @@ -0,0 +1,381 @@
    47.4 +/*
    47.5 + * jdinput.c
    47.6 + *
    47.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    47.8 + * This file is part of the Independent JPEG Group's software.
    47.9 + * For conditions of distribution and use, see the accompanying README file.
   47.10 + *
   47.11 + * This file contains input control logic for the JPEG decompressor.
   47.12 + * These routines are concerned with controlling the decompressor's input
   47.13 + * processing (marker reading and coefficient decoding).  The actual input
   47.14 + * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
   47.15 + */
   47.16 +
   47.17 +#define JPEG_INTERNALS
   47.18 +#include "jinclude.h"
   47.19 +#include "jpeglib.h"
   47.20 +
   47.21 +
   47.22 +/* Private state */
   47.23 +
   47.24 +typedef struct {
   47.25 +  struct jpeg_input_controller pub; /* public fields */
   47.26 +
   47.27 +  boolean inheaders;		/* TRUE until first SOS is reached */
   47.28 +} my_input_controller;
   47.29 +
   47.30 +typedef my_input_controller * my_inputctl_ptr;
   47.31 +
   47.32 +
   47.33 +/* Forward declarations */
   47.34 +METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
   47.35 +
   47.36 +
   47.37 +/*
   47.38 + * Routines to calculate various quantities related to the size of the image.
   47.39 + */
   47.40 +
   47.41 +LOCAL(void)
   47.42 +initial_setup (j_decompress_ptr cinfo)
   47.43 +/* Called once, when first SOS marker is reached */
   47.44 +{
   47.45 +  int ci;
   47.46 +  jpeg_component_info *compptr;
   47.47 +
   47.48 +  /* Make sure image isn't bigger than I can handle */
   47.49 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   47.50 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   47.51 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   47.52 +
   47.53 +  /* For now, precision must match compiled-in value... */
   47.54 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
   47.55 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
   47.56 +
   47.57 +  /* Check that number of components won't exceed internal array sizes */
   47.58 +  if (cinfo->num_components > MAX_COMPONENTS)
   47.59 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   47.60 +	     MAX_COMPONENTS);
   47.61 +
   47.62 +  /* Compute maximum sampling factors; check factor validity */
   47.63 +  cinfo->max_h_samp_factor = 1;
   47.64 +  cinfo->max_v_samp_factor = 1;
   47.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   47.66 +       ci++, compptr++) {
   47.67 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
   47.68 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
   47.69 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
   47.70 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
   47.71 +				   compptr->h_samp_factor);
   47.72 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   47.73 +				   compptr->v_samp_factor);
   47.74 +  }
   47.75 +
   47.76 +  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
   47.77 +   * In the full decompressor, this will be overridden by jdmaster.c;
   47.78 +   * but in the transcoder, jdmaster.c is not used, so we must do it here.
   47.79 +   */
   47.80 +  cinfo->min_DCT_scaled_size = DCTSIZE;
   47.81 +
   47.82 +  /* Compute dimensions of components */
   47.83 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   47.84 +       ci++, compptr++) {
   47.85 +    compptr->DCT_scaled_size = DCTSIZE;
   47.86 +    /* Size in DCT blocks */
   47.87 +    compptr->width_in_blocks = (JDIMENSION)
   47.88 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   47.89 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   47.90 +    compptr->height_in_blocks = (JDIMENSION)
   47.91 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   47.92 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   47.93 +    /* downsampled_width and downsampled_height will also be overridden by
   47.94 +     * jdmaster.c if we are doing full decompression.  The transcoder library
   47.95 +     * doesn't use these values, but the calling application might.
   47.96 +     */
   47.97 +    /* Size in samples */
   47.98 +    compptr->downsampled_width = (JDIMENSION)
   47.99 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  47.100 +		    (long) cinfo->max_h_samp_factor);
  47.101 +    compptr->downsampled_height = (JDIMENSION)
  47.102 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  47.103 +		    (long) cinfo->max_v_samp_factor);
  47.104 +    /* Mark component needed, until color conversion says otherwise */
  47.105 +    compptr->component_needed = TRUE;
  47.106 +    /* Mark no quantization table yet saved for component */
  47.107 +    compptr->quant_table = NULL;
  47.108 +  }
  47.109 +
  47.110 +  /* Compute number of fully interleaved MCU rows. */
  47.111 +  cinfo->total_iMCU_rows = (JDIMENSION)
  47.112 +    jdiv_round_up((long) cinfo->image_height,
  47.113 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
  47.114 +
  47.115 +  /* Decide whether file contains multiple scans */
  47.116 +  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  47.117 +    cinfo->inputctl->has_multiple_scans = TRUE;
  47.118 +  else
  47.119 +    cinfo->inputctl->has_multiple_scans = FALSE;
  47.120 +}
  47.121 +
  47.122 +
  47.123 +LOCAL(void)
  47.124 +per_scan_setup (j_decompress_ptr cinfo)
  47.125 +/* Do computations that are needed before processing a JPEG scan */
  47.126 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  47.127 +{
  47.128 +  int ci, mcublks, tmp;
  47.129 +  jpeg_component_info *compptr;
  47.130 +  
  47.131 +  if (cinfo->comps_in_scan == 1) {
  47.132 +    
  47.133 +    /* Noninterleaved (single-component) scan */
  47.134 +    compptr = cinfo->cur_comp_info[0];
  47.135 +    
  47.136 +    /* Overall image size in MCUs */
  47.137 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
  47.138 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  47.139 +    
  47.140 +    /* For noninterleaved scan, always one block per MCU */
  47.141 +    compptr->MCU_width = 1;
  47.142 +    compptr->MCU_height = 1;
  47.143 +    compptr->MCU_blocks = 1;
  47.144 +    compptr->MCU_sample_width = compptr->DCT_scaled_size;
  47.145 +    compptr->last_col_width = 1;
  47.146 +    /* For noninterleaved scans, it is convenient to define last_row_height
  47.147 +     * as the number of block rows present in the last iMCU row.
  47.148 +     */
  47.149 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  47.150 +    if (tmp == 0) tmp = compptr->v_samp_factor;
  47.151 +    compptr->last_row_height = tmp;
  47.152 +    
  47.153 +    /* Prepare array describing MCU composition */
  47.154 +    cinfo->blocks_in_MCU = 1;
  47.155 +    cinfo->MCU_membership[0] = 0;
  47.156 +    
  47.157 +  } else {
  47.158 +    
  47.159 +    /* Interleaved (multi-component) scan */
  47.160 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  47.161 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  47.162 +	       MAX_COMPS_IN_SCAN);
  47.163 +    
  47.164 +    /* Overall image size in MCUs */
  47.165 +    cinfo->MCUs_per_row = (JDIMENSION)
  47.166 +      jdiv_round_up((long) cinfo->image_width,
  47.167 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
  47.168 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
  47.169 +      jdiv_round_up((long) cinfo->image_height,
  47.170 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
  47.171 +    
  47.172 +    cinfo->blocks_in_MCU = 0;
  47.173 +    
  47.174 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  47.175 +      compptr = cinfo->cur_comp_info[ci];
  47.176 +      /* Sampling factors give # of blocks of component in each MCU */
  47.177 +      compptr->MCU_width = compptr->h_samp_factor;
  47.178 +      compptr->MCU_height = compptr->v_samp_factor;
  47.179 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  47.180 +      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  47.181 +      /* Figure number of non-dummy blocks in last MCU column & row */
  47.182 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  47.183 +      if (tmp == 0) tmp = compptr->MCU_width;
  47.184 +      compptr->last_col_width = tmp;
  47.185 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  47.186 +      if (tmp == 0) tmp = compptr->MCU_height;
  47.187 +      compptr->last_row_height = tmp;
  47.188 +      /* Prepare array describing MCU composition */
  47.189 +      mcublks = compptr->MCU_blocks;
  47.190 +      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  47.191 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  47.192 +      while (mcublks-- > 0) {
  47.193 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  47.194 +      }
  47.195 +    }
  47.196 +    
  47.197 +  }
  47.198 +}
  47.199 +
  47.200 +
  47.201 +/*
  47.202 + * Save away a copy of the Q-table referenced by each component present
  47.203 + * in the current scan, unless already saved during a prior scan.
  47.204 + *
  47.205 + * In a multiple-scan JPEG file, the encoder could assign different components
  47.206 + * the same Q-table slot number, but change table definitions between scans
  47.207 + * so that each component uses a different Q-table.  (The IJG encoder is not
  47.208 + * currently capable of doing this, but other encoders might.)  Since we want
  47.209 + * to be able to dequantize all the components at the end of the file, this
  47.210 + * means that we have to save away the table actually used for each component.
  47.211 + * We do this by copying the table at the start of the first scan containing
  47.212 + * the component.
  47.213 + * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  47.214 + * slot between scans of a component using that slot.  If the encoder does so
  47.215 + * anyway, this decoder will simply use the Q-table values that were current
  47.216 + * at the start of the first scan for the component.
  47.217 + *
  47.218 + * The decompressor output side looks only at the saved quant tables,
  47.219 + * not at the current Q-table slots.
  47.220 + */
  47.221 +
  47.222 +LOCAL(void)
  47.223 +latch_quant_tables (j_decompress_ptr cinfo)
  47.224 +{
  47.225 +  int ci, qtblno;
  47.226 +  jpeg_component_info *compptr;
  47.227 +  JQUANT_TBL * qtbl;
  47.228 +
  47.229 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  47.230 +    compptr = cinfo->cur_comp_info[ci];
  47.231 +    /* No work if we already saved Q-table for this component */
  47.232 +    if (compptr->quant_table != NULL)
  47.233 +      continue;
  47.234 +    /* Make sure specified quantization table is present */
  47.235 +    qtblno = compptr->quant_tbl_no;
  47.236 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  47.237 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
  47.238 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  47.239 +    /* OK, save away the quantization table */
  47.240 +    qtbl = (JQUANT_TBL *)
  47.241 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  47.242 +				  SIZEOF(JQUANT_TBL));
  47.243 +    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  47.244 +    compptr->quant_table = qtbl;
  47.245 +  }
  47.246 +}
  47.247 +
  47.248 +
  47.249 +/*
  47.250 + * Initialize the input modules to read a scan of compressed data.
  47.251 + * The first call to this is done by jdmaster.c after initializing
  47.252 + * the entire decompressor (during jpeg_start_decompress).
  47.253 + * Subsequent calls come from consume_markers, below.
  47.254 + */
  47.255 +
  47.256 +METHODDEF(void)
  47.257 +start_input_pass (j_decompress_ptr cinfo)
  47.258 +{
  47.259 +  per_scan_setup(cinfo);
  47.260 +  latch_quant_tables(cinfo);
  47.261 +  (*cinfo->entropy->start_pass) (cinfo);
  47.262 +  (*cinfo->coef->start_input_pass) (cinfo);
  47.263 +  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  47.264 +}
  47.265 +
  47.266 +
  47.267 +/*
  47.268 + * Finish up after inputting a compressed-data scan.
  47.269 + * This is called by the coefficient controller after it's read all
  47.270 + * the expected data of the scan.
  47.271 + */
  47.272 +
  47.273 +METHODDEF(void)
  47.274 +finish_input_pass (j_decompress_ptr cinfo)
  47.275 +{
  47.276 +  cinfo->inputctl->consume_input = consume_markers;
  47.277 +}
  47.278 +
  47.279 +
  47.280 +/*
  47.281 + * Read JPEG markers before, between, or after compressed-data scans.
  47.282 + * Change state as necessary when a new scan is reached.
  47.283 + * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  47.284 + *
  47.285 + * The consume_input method pointer points either here or to the
  47.286 + * coefficient controller's consume_data routine, depending on whether
  47.287 + * we are reading a compressed data segment or inter-segment markers.
  47.288 + */
  47.289 +
  47.290 +METHODDEF(int)
  47.291 +consume_markers (j_decompress_ptr cinfo)
  47.292 +{
  47.293 +  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  47.294 +  int val;
  47.295 +
  47.296 +  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  47.297 +    return JPEG_REACHED_EOI;
  47.298 +
  47.299 +  val = (*cinfo->marker->read_markers) (cinfo);
  47.300 +
  47.301 +  switch (val) {
  47.302 +  case JPEG_REACHED_SOS:	/* Found SOS */
  47.303 +    if (inputctl->inheaders) {	/* 1st SOS */
  47.304 +      initial_setup(cinfo);
  47.305 +      inputctl->inheaders = FALSE;
  47.306 +      /* Note: start_input_pass must be called by jdmaster.c
  47.307 +       * before any more input can be consumed.  jdapimin.c is
  47.308 +       * responsible for enforcing this sequencing.
  47.309 +       */
  47.310 +    } else {			/* 2nd or later SOS marker */
  47.311 +      if (! inputctl->pub.has_multiple_scans)
  47.312 +	ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  47.313 +      start_input_pass(cinfo);
  47.314 +    }
  47.315 +    break;
  47.316 +  case JPEG_REACHED_EOI:	/* Found EOI */
  47.317 +    inputctl->pub.eoi_reached = TRUE;
  47.318 +    if (inputctl->inheaders) {	/* Tables-only datastream, apparently */
  47.319 +      if (cinfo->marker->saw_SOF)
  47.320 +	ERREXIT(cinfo, JERR_SOF_NO_SOS);
  47.321 +    } else {
  47.322 +      /* Prevent infinite loop in coef ctlr's decompress_data routine
  47.323 +       * if user set output_scan_number larger than number of scans.
  47.324 +       */
  47.325 +      if (cinfo->output_scan_number > cinfo->input_scan_number)
  47.326 +	cinfo->output_scan_number = cinfo->input_scan_number;
  47.327 +    }
  47.328 +    break;
  47.329 +  case JPEG_SUSPENDED:
  47.330 +    break;
  47.331 +  }
  47.332 +
  47.333 +  return val;
  47.334 +}
  47.335 +
  47.336 +
  47.337 +/*
  47.338 + * Reset state to begin a fresh datastream.
  47.339 + */
  47.340 +
  47.341 +METHODDEF(void)
  47.342 +reset_input_controller (j_decompress_ptr cinfo)
  47.343 +{
  47.344 +  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  47.345 +
  47.346 +  inputctl->pub.consume_input = consume_markers;
  47.347 +  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  47.348 +  inputctl->pub.eoi_reached = FALSE;
  47.349 +  inputctl->inheaders = TRUE;
  47.350 +  /* Reset other modules */
  47.351 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  47.352 +  (*cinfo->marker->reset_marker_reader) (cinfo);
  47.353 +  /* Reset progression state -- would be cleaner if entropy decoder did this */
  47.354 +  cinfo->coef_bits = NULL;
  47.355 +}
  47.356 +
  47.357 +
  47.358 +/*
  47.359 + * Initialize the input controller module.
  47.360 + * This is called only once, when the decompression object is created.
  47.361 + */
  47.362 +
  47.363 +GLOBAL(void)
  47.364 +jinit_input_controller (j_decompress_ptr cinfo)
  47.365 +{
  47.366 +  my_inputctl_ptr inputctl;
  47.367 +
  47.368 +  /* Create subobject in permanent pool */
  47.369 +  inputctl = (my_inputctl_ptr)
  47.370 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  47.371 +				SIZEOF(my_input_controller));
  47.372 +  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  47.373 +  /* Initialize method pointers */
  47.374 +  inputctl->pub.consume_input = consume_markers;
  47.375 +  inputctl->pub.reset_input_controller = reset_input_controller;
  47.376 +  inputctl->pub.start_input_pass = start_input_pass;
  47.377 +  inputctl->pub.finish_input_pass = finish_input_pass;
  47.378 +  /* Initialize state: can't use reset_input_controller since we don't
  47.379 +   * want to try to reset other modules yet.
  47.380 +   */
  47.381 +  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  47.382 +  inputctl->pub.eoi_reached = FALSE;
  47.383 +  inputctl->inheaders = TRUE;
  47.384 +}
    48.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    48.2 +++ b/libs/libjpeg/jdmainct.c	Sun Jun 07 17:25:49 2015 +0300
    48.3 @@ -0,0 +1,512 @@
    48.4 +/*
    48.5 + * jdmainct.c
    48.6 + *
    48.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    48.8 + * This file is part of the Independent JPEG Group's software.
    48.9 + * For conditions of distribution and use, see the accompanying README file.
   48.10 + *
   48.11 + * This file contains the main buffer controller for decompression.
   48.12 + * The main buffer lies between the JPEG decompressor proper and the
   48.13 + * post-processor; it holds downsampled data in the JPEG colorspace.
   48.14 + *
   48.15 + * Note that this code is bypassed in raw-data mode, since the application
   48.16 + * supplies the equivalent of the main buffer in that case.
   48.17 + */
   48.18 +
   48.19 +#define JPEG_INTERNALS
   48.20 +#include "jinclude.h"
   48.21 +#include "jpeglib.h"
   48.22 +
   48.23 +
   48.24 +/*
   48.25 + * In the current system design, the main buffer need never be a full-image
   48.26 + * buffer; any full-height buffers will be found inside the coefficient or
   48.27 + * postprocessing controllers.  Nonetheless, the main controller is not
   48.28 + * trivial.  Its responsibility is to provide context rows for upsampling/
   48.29 + * rescaling, and doing this in an efficient fashion is a bit tricky.
   48.30 + *
   48.31 + * Postprocessor input data is counted in "row groups".  A row group
   48.32 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
   48.33 + * sample rows of each component.  (We require DCT_scaled_size values to be
   48.34 + * chosen such that these numbers are integers.  In practice DCT_scaled_size
   48.35 + * values will likely be powers of two, so we actually have the stronger
   48.36 + * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
   48.37 + * Upsampling will typically produce max_v_samp_factor pixel rows from each
   48.38 + * row group (times any additional scale factor that the upsampler is
   48.39 + * applying).
   48.40 + *
   48.41 + * The coefficient controller will deliver data to us one iMCU row at a time;
   48.42 + * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
   48.43 + * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
   48.44 + * to one row of MCUs when the image is fully interleaved.)  Note that the
   48.45 + * number of sample rows varies across components, but the number of row
   48.46 + * groups does not.  Some garbage sample rows may be included in the last iMCU
   48.47 + * row at the bottom of the image.
   48.48 + *
   48.49 + * Depending on the vertical scaling algorithm used, the upsampler may need
   48.50 + * access to the sample row(s) above and below its current input row group.
   48.51 + * The upsampler is required to set need_context_rows TRUE at global selection
   48.52 + * time if so.  When need_context_rows is FALSE, this controller can simply
   48.53 + * obtain one iMCU row at a time from the coefficient controller and dole it
   48.54 + * out as row groups to the postprocessor.
   48.55 + *
   48.56 + * When need_context_rows is TRUE, this controller guarantees that the buffer
   48.57 + * passed to postprocessing contains at least one row group's worth of samples
   48.58 + * above and below the row group(s) being processed.  Note that the context
   48.59 + * rows "above" the first passed row group appear at negative row offsets in
   48.60 + * the passed buffer.  At the top and bottom of the image, the required
   48.61 + * context rows are manufactured by duplicating the first or last real sample
   48.62 + * row; this avoids having special cases in the upsampling inner loops.
   48.63 + *
   48.64 + * The amount of context is fixed at one row group just because that's a
   48.65 + * convenient number for this controller to work with.  The existing
   48.66 + * upsamplers really only need one sample row of context.  An upsampler
   48.67 + * supporting arbitrary output rescaling might wish for more than one row
   48.68 + * group of context when shrinking the image; tough, we don't handle that.
   48.69 + * (This is justified by the assumption that downsizing will be handled mostly
   48.70 + * by adjusting the DCT_scaled_size values, so that the actual scale factor at
   48.71 + * the upsample step needn't be much less than one.)
   48.72 + *
   48.73 + * To provide the desired context, we have to retain the last two row groups
   48.74 + * of one iMCU row while reading in the next iMCU row.  (The last row group
   48.75 + * can't be processed until we have another row group for its below-context,
   48.76 + * and so we have to save the next-to-last group too for its above-context.)
   48.77 + * We could do this most simply by copying data around in our buffer, but
   48.78 + * that'd be very slow.  We can avoid copying any data by creating a rather
   48.79 + * strange pointer structure.  Here's how it works.  We allocate a workspace
   48.80 + * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
   48.81 + * of row groups per iMCU row).  We create two sets of redundant pointers to
   48.82 + * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
   48.83 + * pointer lists look like this:
   48.84 + *                   M+1                          M-1
   48.85 + * master pointer --> 0         master pointer --> 0
   48.86 + *                    1                            1
   48.87 + *                   ...                          ...
   48.88 + *                   M-3                          M-3
   48.89 + *                   M-2                           M
   48.90 + *                   M-1                          M+1
   48.91 + *                    M                           M-2
   48.92 + *                   M+1                          M-1
   48.93 + *                    0                            0
   48.94 + * We read alternate iMCU rows using each master pointer; thus the last two
   48.95 + * row groups of the previous iMCU row remain un-overwritten in the workspace.
   48.96 + * The pointer lists are set up so that the required context rows appear to
   48.97 + * be adjacent to the proper places when we pass the pointer lists to the
   48.98 + * upsampler.
   48.99 + *
  48.100 + * The above pictures describe the normal state of the pointer lists.
  48.101 + * At top and bottom of the image, we diddle the pointer lists to duplicate
  48.102 + * the first or last sample row as necessary (this is cheaper than copying
  48.103 + * sample rows around).
  48.104 + *
  48.105 + * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
  48.106 + * situation each iMCU row provides only one row group so the buffering logic
  48.107 + * must be different (eg, we must read two iMCU rows before we can emit the
  48.108 + * first row group).  For now, we simply do not support providing context
  48.109 + * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
  48.110 + * be worth providing --- if someone wants a 1/8th-size preview, they probably
  48.111 + * want it quick and dirty, so a context-free upsampler is sufficient.
  48.112 + */
  48.113 +
  48.114 +
  48.115 +/* Private buffer controller object */
  48.116 +
  48.117 +typedef struct {
  48.118 +  struct jpeg_d_main_controller pub; /* public fields */
  48.119 +
  48.120 +  /* Pointer to allocated workspace (M or M+2 row groups). */
  48.121 +  JSAMPARRAY buffer[MAX_COMPONENTS];
  48.122 +
  48.123 +  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
  48.124 +  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
  48.125 +
  48.126 +  /* Remaining fields are only used in the context case. */
  48.127 +
  48.128 +  /* These are the master pointers to the funny-order pointer lists. */
  48.129 +  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
  48.130 +
  48.131 +  int whichptr;			/* indicates which pointer set is now in use */
  48.132 +  int context_state;		/* process_data state machine status */
  48.133 +  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
  48.134 +  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
  48.135 +} my_main_controller;
  48.136 +
  48.137 +typedef my_main_controller * my_main_ptr;
  48.138 +
  48.139 +/* context_state values: */
  48.140 +#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
  48.141 +#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
  48.142 +#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */
  48.143 +
  48.144 +
  48.145 +/* Forward declarations */
  48.146 +METHODDEF(void) process_data_simple_main
  48.147 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  48.148 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  48.149 +METHODDEF(void) process_data_context_main
  48.150 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  48.151 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  48.152 +#ifdef QUANT_2PASS_SUPPORTED
  48.153 +METHODDEF(void) process_data_crank_post
  48.154 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  48.155 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  48.156 +#endif
  48.157 +
  48.158 +
  48.159 +LOCAL(void)
  48.160 +alloc_funny_pointers (j_decompress_ptr cinfo)
  48.161 +/* Allocate space for the funny pointer lists.
  48.162 + * This is done only once, not once per pass.
  48.163 + */
  48.164 +{
  48.165 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.166 +  int ci, rgroup;
  48.167 +  int M = cinfo->min_DCT_scaled_size;
  48.168 +  jpeg_component_info *compptr;
  48.169 +  JSAMPARRAY xbuf;
  48.170 +
  48.171 +  /* Get top-level space for component array pointers.
  48.172 +   * We alloc both arrays with one call to save a few cycles.
  48.173 +   */
  48.174 +  main->xbuffer[0] = (JSAMPIMAGE)
  48.175 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.176 +				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
  48.177 +  main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
  48.178 +
  48.179 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  48.180 +       ci++, compptr++) {
  48.181 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  48.182 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  48.183 +    /* Get space for pointer lists --- M+4 row groups in each list.
  48.184 +     * We alloc both pointer lists with one call to save a few cycles.
  48.185 +     */
  48.186 +    xbuf = (JSAMPARRAY)
  48.187 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.188 +				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
  48.189 +    xbuf += rgroup;		/* want one row group at negative offsets */
  48.190 +    main->xbuffer[0][ci] = xbuf;
  48.191 +    xbuf += rgroup * (M + 4);
  48.192 +    main->xbuffer[1][ci] = xbuf;
  48.193 +  }
  48.194 +}
  48.195 +
  48.196 +
  48.197 +LOCAL(void)
  48.198 +make_funny_pointers (j_decompress_ptr cinfo)
  48.199 +/* Create the funny pointer lists discussed in the comments above.
  48.200 + * The actual workspace is already allocated (in main->buffer),
  48.201 + * and the space for the pointer lists is allocated too.
  48.202 + * This routine just fills in the curiously ordered lists.
  48.203 + * This will be repeated at the beginning of each pass.
  48.204 + */
  48.205 +{
  48.206 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.207 +  int ci, i, rgroup;
  48.208 +  int M = cinfo->min_DCT_scaled_size;
  48.209 +  jpeg_component_info *compptr;
  48.210 +  JSAMPARRAY buf, xbuf0, xbuf1;
  48.211 +
  48.212 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  48.213 +       ci++, compptr++) {
  48.214 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  48.215 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  48.216 +    xbuf0 = main->xbuffer[0][ci];
  48.217 +    xbuf1 = main->xbuffer[1][ci];
  48.218 +    /* First copy the workspace pointers as-is */
  48.219 +    buf = main->buffer[ci];
  48.220 +    for (i = 0; i < rgroup * (M + 2); i++) {
  48.221 +      xbuf0[i] = xbuf1[i] = buf[i];
  48.222 +    }
  48.223 +    /* In the second list, put the last four row groups in swapped order */
  48.224 +    for (i = 0; i < rgroup * 2; i++) {
  48.225 +      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
  48.226 +      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
  48.227 +    }
  48.228 +    /* The wraparound pointers at top and bottom will be filled later
  48.229 +     * (see set_wraparound_pointers, below).  Initially we want the "above"
  48.230 +     * pointers to duplicate the first actual data line.  This only needs
  48.231 +     * to happen in xbuffer[0].
  48.232 +     */
  48.233 +    for (i = 0; i < rgroup; i++) {
  48.234 +      xbuf0[i - rgroup] = xbuf0[0];
  48.235 +    }
  48.236 +  }
  48.237 +}
  48.238 +
  48.239 +
  48.240 +LOCAL(void)
  48.241 +set_wraparound_pointers (j_decompress_ptr cinfo)
  48.242 +/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
  48.243 + * This changes the pointer list state from top-of-image to the normal state.
  48.244 + */
  48.245 +{
  48.246 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.247 +  int ci, i, rgroup;
  48.248 +  int M = cinfo->min_DCT_scaled_size;
  48.249 +  jpeg_component_info *compptr;
  48.250 +  JSAMPARRAY xbuf0, xbuf1;
  48.251 +
  48.252 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  48.253 +       ci++, compptr++) {
  48.254 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  48.255 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  48.256 +    xbuf0 = main->xbuffer[0][ci];
  48.257 +    xbuf1 = main->xbuffer[1][ci];
  48.258 +    for (i = 0; i < rgroup; i++) {
  48.259 +      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
  48.260 +      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
  48.261 +      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
  48.262 +      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
  48.263 +    }
  48.264 +  }
  48.265 +}
  48.266 +
  48.267 +
  48.268 +LOCAL(void)
  48.269 +set_bottom_pointers (j_decompress_ptr cinfo)
  48.270 +/* Change the pointer lists to duplicate the last sample row at the bottom
  48.271 + * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
  48.272 + * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
  48.273 + */
  48.274 +{
  48.275 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.276 +  int ci, i, rgroup, iMCUheight, rows_left;
  48.277 +  jpeg_component_info *compptr;
  48.278 +  JSAMPARRAY xbuf;
  48.279 +
  48.280 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  48.281 +       ci++, compptr++) {
  48.282 +    /* Count sample rows in one iMCU row and in one row group */
  48.283 +    iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
  48.284 +    rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
  48.285 +    /* Count nondummy sample rows remaining for this component */
  48.286 +    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
  48.287 +    if (rows_left == 0) rows_left = iMCUheight;
  48.288 +    /* Count nondummy row groups.  Should get same answer for each component,
  48.289 +     * so we need only do it once.
  48.290 +     */
  48.291 +    if (ci == 0) {
  48.292 +      main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
  48.293 +    }
  48.294 +    /* Duplicate the last real sample row rgroup*2 times; this pads out the
  48.295 +     * last partial rowgroup and ensures at least one full rowgroup of context.
  48.296 +     */
  48.297 +    xbuf = main->xbuffer[main->whichptr][ci];
  48.298 +    for (i = 0; i < rgroup * 2; i++) {
  48.299 +      xbuf[rows_left + i] = xbuf[rows_left-1];
  48.300 +    }
  48.301 +  }
  48.302 +}
  48.303 +
  48.304 +
  48.305 +/*
  48.306 + * Initialize for a processing pass.
  48.307 + */
  48.308 +
  48.309 +METHODDEF(void)
  48.310 +start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
  48.311 +{
  48.312 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.313 +
  48.314 +  switch (pass_mode) {
  48.315 +  case JBUF_PASS_THRU:
  48.316 +    if (cinfo->upsample->need_context_rows) {
  48.317 +      main->pub.process_data = process_data_context_main;
  48.318 +      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
  48.319 +      main->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
  48.320 +      main->context_state = CTX_PREPARE_FOR_IMCU;
  48.321 +      main->iMCU_row_ctr = 0;
  48.322 +    } else {
  48.323 +      /* Simple case with no context needed */
  48.324 +      main->pub.process_data = process_data_simple_main;
  48.325 +    }
  48.326 +    main->buffer_full = FALSE;	/* Mark buffer empty */
  48.327 +    main->rowgroup_ctr = 0;
  48.328 +    break;
  48.329 +#ifdef QUANT_2PASS_SUPPORTED
  48.330 +  case JBUF_CRANK_DEST:
  48.331 +    /* For last pass of 2-pass quantization, just crank the postprocessor */
  48.332 +    main->pub.process_data = process_data_crank_post;
  48.333 +    break;
  48.334 +#endif
  48.335 +  default:
  48.336 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  48.337 +    break;
  48.338 +  }
  48.339 +}
  48.340 +
  48.341 +
  48.342 +/*
  48.343 + * Process some data.
  48.344 + * This handles the simple case where no context is required.
  48.345 + */
  48.346 +
  48.347 +METHODDEF(void)
  48.348 +process_data_simple_main (j_decompress_ptr cinfo,
  48.349 +			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  48.350 +			  JDIMENSION out_rows_avail)
  48.351 +{
  48.352 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.353 +  JDIMENSION rowgroups_avail;
  48.354 +
  48.355 +  /* Read input data if we haven't filled the main buffer yet */
  48.356 +  if (! main->buffer_full) {
  48.357 +    if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
  48.358 +      return;			/* suspension forced, can do nothing more */
  48.359 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
  48.360 +  }
  48.361 +
  48.362 +  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
  48.363 +  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
  48.364 +  /* Note: at the bottom of the image, we may pass extra garbage row groups
  48.365 +   * to the postprocessor.  The postprocessor has to check for bottom
  48.366 +   * of image anyway (at row resolution), so no point in us doing it too.
  48.367 +   */
  48.368 +
  48.369 +  /* Feed the postprocessor */
  48.370 +  (*cinfo->post->post_process_data) (cinfo, main->buffer,
  48.371 +				     &main->rowgroup_ctr, rowgroups_avail,
  48.372 +				     output_buf, out_row_ctr, out_rows_avail);
  48.373 +
  48.374 +  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
  48.375 +  if (main->rowgroup_ctr >= rowgroups_avail) {
  48.376 +    main->buffer_full = FALSE;
  48.377 +    main->rowgroup_ctr = 0;
  48.378 +  }
  48.379 +}
  48.380 +
  48.381 +
  48.382 +/*
  48.383 + * Process some data.
  48.384 + * This handles the case where context rows must be provided.
  48.385 + */
  48.386 +
  48.387 +METHODDEF(void)
  48.388 +process_data_context_main (j_decompress_ptr cinfo,
  48.389 +			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  48.390 +			   JDIMENSION out_rows_avail)
  48.391 +{
  48.392 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  48.393 +
  48.394 +  /* Read input data if we haven't filled the main buffer yet */
  48.395 +  if (! main->buffer_full) {
  48.396 +    if (! (*cinfo->coef->decompress_data) (cinfo,
  48.397 +					   main->xbuffer[main->whichptr]))
  48.398 +      return;			/* suspension forced, can do nothing more */
  48.399 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
  48.400 +    main->iMCU_row_ctr++;	/* count rows received */
  48.401 +  }
  48.402 +
  48.403 +  /* Postprocessor typically will not swallow all the input data it is handed
  48.404 +   * in one call (due to filling the output buffer first).  Must be prepared
  48.405 +   * to exit and restart.  This switch lets us keep track of how far we got.
  48.406 +   * Note that each case falls through to the next on successful completion.
  48.407 +   */
  48.408 +  switch (main->context_state) {
  48.409 +  case CTX_POSTPONED_ROW:
  48.410 +    /* Call postprocessor using previously set pointers for postponed row */
  48.411 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
  48.412 +			&main->rowgroup_ctr, main->rowgroups_avail,
  48.413 +			output_buf, out_row_ctr, out_rows_avail);
  48.414 +    if (main->rowgroup_ctr < main->rowgroups_avail)
  48.415 +      return;			/* Need to suspend */
  48.416 +    main->context_state = CTX_PREPARE_FOR_IMCU;
  48.417 +    if (*out_row_ctr >= out_rows_avail)
  48.418 +      return;			/* Postprocessor exactly filled output buf */
  48.419 +    /*FALLTHROUGH*/
  48.420 +  case CTX_PREPARE_FOR_IMCU:
  48.421 +    /* Prepare to process first M-1 row groups of this iMCU row */
  48.422 +    main->rowgroup_ctr = 0;
  48.423 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
  48.424 +    /* Check for bottom of image: if so, tweak pointers to "duplicate"
  48.425 +     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
  48.426 +     */
  48.427 +    if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
  48.428 +      set_bottom_pointers(cinfo);
  48.429 +    main->context_state = CTX_PROCESS_IMCU;
  48.430 +    /*FALLTHROUGH*/
  48.431 +  case CTX_PROCESS_IMCU:
  48.432 +    /* Call postprocessor using previously set pointers */
  48.433 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
  48.434 +			&main->rowgroup_ctr, main->rowgroups_avail,
  48.435 +			output_buf, out_row_ctr, out_rows_avail);
  48.436 +    if (main->rowgroup_ctr < main->rowgroups_avail)
  48.437 +      return;			/* Need to suspend */
  48.438 +    /* After the first iMCU, change wraparound pointers to normal state */
  48.439 +    if (main->iMCU_row_ctr == 1)
  48.440 +      set_wraparound_pointers(cinfo);
  48.441 +    /* Prepare to load new iMCU row using other xbuffer list */
  48.442 +    main->whichptr ^= 1;	/* 0=>1 or 1=>0 */
  48.443 +    main->buffer_full = FALSE;
  48.444 +    /* Still need to process last row group of this iMCU row, */
  48.445 +    /* which is saved at index M+1 of the other xbuffer */
  48.446 +    main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
  48.447 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
  48.448 +    main->context_state = CTX_POSTPONED_ROW;
  48.449 +  }
  48.450 +}
  48.451 +
  48.452 +
  48.453 +/*
  48.454 + * Process some data.
  48.455 + * Final pass of two-pass quantization: just call the postprocessor.
  48.456 + * Source data will be the postprocessor controller's internal buffer.
  48.457 + */
  48.458 +
  48.459 +#ifdef QUANT_2PASS_SUPPORTED
  48.460 +
  48.461 +METHODDEF(void)
  48.462 +process_data_crank_post (j_decompress_ptr cinfo,
  48.463 +			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  48.464 +			 JDIMENSION out_rows_avail)
  48.465 +{
  48.466 +  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
  48.467 +				     (JDIMENSION *) NULL, (JDIMENSION) 0,
  48.468 +				     output_buf, out_row_ctr, out_rows_avail);
  48.469 +}
  48.470 +
  48.471 +#endif /* QUANT_2PASS_SUPPORTED */
  48.472 +
  48.473 +
  48.474 +/*
  48.475 + * Initialize main buffer controller.
  48.476 + */
  48.477 +
  48.478 +GLOBAL(void)
  48.479 +jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  48.480 +{
  48.481 +  my_main_ptr main;
  48.482 +  int ci, rgroup, ngroups;
  48.483 +  jpeg_component_info *compptr;
  48.484 +
  48.485 +  main = (my_main_ptr)
  48.486 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.487 +				SIZEOF(my_main_controller));
  48.488 +  cinfo->main = (struct jpeg_d_main_controller *) main;
  48.489 +  main->pub.start_pass = start_pass_main;
  48.490 +
  48.491 +  if (need_full_buffer)		/* shouldn't happen */
  48.492 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  48.493 +
  48.494 +  /* Allocate the workspace.
  48.495 +   * ngroups is the number of row groups we need.
  48.496 +   */
  48.497 +  if (cinfo->upsample->need_context_rows) {
  48.498 +    if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
  48.499 +      ERREXIT(cinfo, JERR_NOTIMPL);
  48.500 +    alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
  48.501 +    ngroups = cinfo->min_DCT_scaled_size + 2;
  48.502 +  } else {
  48.503 +    ngroups = cinfo->min_DCT_scaled_size;
  48.504 +  }
  48.505 +
  48.506 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  48.507 +       ci++, compptr++) {
  48.508 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  48.509 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  48.510 +    main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  48.511 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.512 +			 compptr->width_in_blocks * compptr->DCT_scaled_size,
  48.513 +			 (JDIMENSION) (rgroup * ngroups));
  48.514 +  }
  48.515 +}
    49.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    49.2 +++ b/libs/libjpeg/jdmarker.c	Sun Jun 07 17:25:49 2015 +0300
    49.3 @@ -0,0 +1,1360 @@
    49.4 +/*
    49.5 + * jdmarker.c
    49.6 + *
    49.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    49.8 + * This file is part of the Independent JPEG Group's software.
    49.9 + * For conditions of distribution and use, see the accompanying README file.
   49.10 + *
   49.11 + * This file contains routines to decode JPEG datastream markers.
   49.12 + * Most of the complexity arises from our desire to support input
   49.13 + * suspension: if not all of the data for a marker is available,
   49.14 + * we must exit back to the application.  On resumption, we reprocess
   49.15 + * the marker.
   49.16 + */
   49.17 +
   49.18 +#define JPEG_INTERNALS
   49.19 +#include "jinclude.h"
   49.20 +#include "jpeglib.h"
   49.21 +
   49.22 +
   49.23 +typedef enum {			/* JPEG marker codes */
   49.24 +  M_SOF0  = 0xc0,
   49.25 +  M_SOF1  = 0xc1,
   49.26 +  M_SOF2  = 0xc2,
   49.27 +  M_SOF3  = 0xc3,
   49.28 +  
   49.29 +  M_SOF5  = 0xc5,
   49.30 +  M_SOF6  = 0xc6,
   49.31 +  M_SOF7  = 0xc7,
   49.32 +  
   49.33 +  M_JPG   = 0xc8,
   49.34 +  M_SOF9  = 0xc9,
   49.35 +  M_SOF10 = 0xca,
   49.36 +  M_SOF11 = 0xcb,
   49.37 +  
   49.38 +  M_SOF13 = 0xcd,
   49.39 +  M_SOF14 = 0xce,
   49.40 +  M_SOF15 = 0xcf,
   49.41 +  
   49.42 +  M_DHT   = 0xc4,
   49.43 +  
   49.44 +  M_DAC   = 0xcc,
   49.45 +  
   49.46 +  M_RST0  = 0xd0,
   49.47 +  M_RST1  = 0xd1,
   49.48 +  M_RST2  = 0xd2,
   49.49 +  M_RST3  = 0xd3,
   49.50 +  M_RST4  = 0xd4,
   49.51 +  M_RST5  = 0xd5,
   49.52 +  M_RST6  = 0xd6,
   49.53 +  M_RST7  = 0xd7,
   49.54 +  
   49.55 +  M_SOI   = 0xd8,
   49.56 +  M_EOI   = 0xd9,
   49.57 +  M_SOS   = 0xda,
   49.58 +  M_DQT   = 0xdb,
   49.59 +  M_DNL   = 0xdc,
   49.60 +  M_DRI   = 0xdd,
   49.61 +  M_DHP   = 0xde,
   49.62 +  M_EXP   = 0xdf,
   49.63 +  
   49.64 +  M_APP0  = 0xe0,
   49.65 +  M_APP1  = 0xe1,
   49.66 +  M_APP2  = 0xe2,
   49.67 +  M_APP3  = 0xe3,
   49.68 +  M_APP4  = 0xe4,
   49.69 +  M_APP5  = 0xe5,
   49.70 +  M_APP6  = 0xe6,
   49.71 +  M_APP7  = 0xe7,
   49.72 +  M_APP8  = 0xe8,
   49.73 +  M_APP9  = 0xe9,
   49.74 +  M_APP10 = 0xea,
   49.75 +  M_APP11 = 0xeb,
   49.76 +  M_APP12 = 0xec,
   49.77 +  M_APP13 = 0xed,
   49.78 +  M_APP14 = 0xee,
   49.79 +  M_APP15 = 0xef,
   49.80 +  
   49.81 +  M_JPG0  = 0xf0,
   49.82 +  M_JPG13 = 0xfd,
   49.83 +  M_COM   = 0xfe,
   49.84 +  
   49.85 +  M_TEM   = 0x01,
   49.86 +  
   49.87 +  M_ERROR = 0x100
   49.88 +} JPEG_MARKER;
   49.89 +
   49.90 +
   49.91 +/* Private state */
   49.92 +
   49.93 +typedef struct {
   49.94 +  struct jpeg_marker_reader pub; /* public fields */
   49.95 +
   49.96 +  /* Application-overridable marker processing methods */
   49.97 +  jpeg_marker_parser_method process_COM;
   49.98 +  jpeg_marker_parser_method process_APPn[16];
   49.99 +
  49.100 +  /* Limit on marker data length to save for each marker type */
  49.101 +  unsigned int length_limit_COM;
  49.102 +  unsigned int length_limit_APPn[16];
  49.103 +
  49.104 +  /* Status of COM/APPn marker saving */
  49.105 +  jpeg_saved_marker_ptr cur_marker;	/* NULL if not processing a marker */
  49.106 +  unsigned int bytes_read;		/* data bytes read so far in marker */
  49.107 +  /* Note: cur_marker is not linked into marker_list until it's all read. */
  49.108 +} my_marker_reader;
  49.109 +
  49.110 +typedef my_marker_reader * my_marker_ptr;
  49.111 +
  49.112 +
  49.113 +/*
  49.114 + * Macros for fetching data from the data source module.
  49.115 + *
  49.116 + * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
  49.117 + * the current restart point; we update them only when we have reached a
  49.118 + * suitable place to restart if a suspension occurs.
  49.119 + */
  49.120 +
  49.121 +/* Declare and initialize local copies of input pointer/count */
  49.122 +#define INPUT_VARS(cinfo)  \
  49.123 +	struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
  49.124 +	const JOCTET * next_input_byte = datasrc->next_input_byte;  \
  49.125 +	size_t bytes_in_buffer = datasrc->bytes_in_buffer
  49.126 +
  49.127 +/* Unload the local copies --- do this only at a restart boundary */
  49.128 +#define INPUT_SYNC(cinfo)  \
  49.129 +	( datasrc->next_input_byte = next_input_byte,  \
  49.130 +	  datasrc->bytes_in_buffer = bytes_in_buffer )
  49.131 +
  49.132 +/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
  49.133 +#define INPUT_RELOAD(cinfo)  \
  49.134 +	( next_input_byte = datasrc->next_input_byte,  \
  49.135 +	  bytes_in_buffer = datasrc->bytes_in_buffer )
  49.136 +
  49.137 +/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
  49.138 + * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  49.139 + * but we must reload the local copies after a successful fill.
  49.140 + */
  49.141 +#define MAKE_BYTE_AVAIL(cinfo,action)  \
  49.142 +	if (bytes_in_buffer == 0) {  \
  49.143 +	  if (! (*datasrc->fill_input_buffer) (cinfo))  \
  49.144 +	    { action; }  \
  49.145 +	  INPUT_RELOAD(cinfo);  \
  49.146 +	}
  49.147 +
  49.148 +/* Read a byte into variable V.
  49.149 + * If must suspend, take the specified action (typically "return FALSE").
  49.150 + */
  49.151 +#define INPUT_BYTE(cinfo,V,action)  \
  49.152 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  49.153 +		  bytes_in_buffer--; \
  49.154 +		  V = GETJOCTET(*next_input_byte++); )
  49.155 +
  49.156 +/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
  49.157 + * V should be declared unsigned int or perhaps INT32.
  49.158 + */
  49.159 +#define INPUT_2BYTES(cinfo,V,action)  \
  49.160 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  49.161 +		  bytes_in_buffer--; \
  49.162 +		  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
  49.163 +		  MAKE_BYTE_AVAIL(cinfo,action); \
  49.164 +		  bytes_in_buffer--; \
  49.165 +		  V += GETJOCTET(*next_input_byte++); )
  49.166 +
  49.167 +
  49.168 +/*
  49.169 + * Routines to process JPEG markers.
  49.170 + *
  49.171 + * Entry condition: JPEG marker itself has been read and its code saved
  49.172 + *   in cinfo->unread_marker; input restart point is just after the marker.
  49.173 + *
  49.174 + * Exit: if return TRUE, have read and processed any parameters, and have
  49.175 + *   updated the restart point to point after the parameters.
  49.176 + *   If return FALSE, was forced to suspend before reaching end of
  49.177 + *   marker parameters; restart point has not been moved.  Same routine
  49.178 + *   will be called again after application supplies more input data.
  49.179 + *
  49.180 + * This approach to suspension assumes that all of a marker's parameters
  49.181 + * can fit into a single input bufferload.  This should hold for "normal"
  49.182 + * markers.  Some COM/APPn markers might have large parameter segments
  49.183 + * that might not fit.  If we are simply dropping such a marker, we use
  49.184 + * skip_input_data to get past it, and thereby put the problem on the
  49.185 + * source manager's shoulders.  If we are saving the marker's contents
  49.186 + * into memory, we use a slightly different convention: when forced to
  49.187 + * suspend, the marker processor updates the restart point to the end of
  49.188 + * what it's consumed (ie, the end of the buffer) before returning FALSE.
  49.189 + * On resumption, cinfo->unread_marker still contains the marker code,
  49.190 + * but the data source will point to the next chunk of marker data.
  49.191 + * The marker processor must retain internal state to deal with this.
  49.192 + *
  49.193 + * Note that we don't bother to avoid duplicate trace messages if a
  49.194 + * suspension occurs within marker parameters.  Other side effects
  49.195 + * require more care.
  49.196 + */
  49.197 +
  49.198 +
  49.199 +LOCAL(boolean)
  49.200 +get_soi (j_decompress_ptr cinfo)
  49.201 +/* Process an SOI marker */
  49.202 +{
  49.203 +  int i;
  49.204 +  
  49.205 +  TRACEMS(cinfo, 1, JTRC_SOI);
  49.206 +
  49.207 +  if (cinfo->marker->saw_SOI)
  49.208 +    ERREXIT(cinfo, JERR_SOI_DUPLICATE);
  49.209 +
  49.210 +  /* Reset all parameters that are defined to be reset by SOI */
  49.211 +
  49.212 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  49.213 +    cinfo->arith_dc_L[i] = 0;
  49.214 +    cinfo->arith_dc_U[i] = 1;
  49.215 +    cinfo->arith_ac_K[i] = 5;
  49.216 +  }
  49.217 +  cinfo->restart_interval = 0;
  49.218 +
  49.219 +  /* Set initial assumptions for colorspace etc */
  49.220 +
  49.221 +  cinfo->jpeg_color_space = JCS_UNKNOWN;
  49.222 +  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
  49.223 +
  49.224 +  cinfo->saw_JFIF_marker = FALSE;
  49.225 +  cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
  49.226 +  cinfo->JFIF_minor_version = 1;
  49.227 +  cinfo->density_unit = 0;
  49.228 +  cinfo->X_density = 1;
  49.229 +  cinfo->Y_density = 1;
  49.230 +  cinfo->saw_Adobe_marker = FALSE;
  49.231 +  cinfo->Adobe_transform = 0;
  49.232 +
  49.233 +  cinfo->marker->saw_SOI = TRUE;
  49.234 +
  49.235 +  return TRUE;
  49.236 +}
  49.237 +
  49.238 +
  49.239 +LOCAL(boolean)
  49.240 +get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
  49.241 +/* Process a SOFn marker */
  49.242 +{
  49.243 +  INT32 length;
  49.244 +  int c, ci;
  49.245 +  jpeg_component_info * compptr;
  49.246 +  INPUT_VARS(cinfo);
  49.247 +
  49.248 +  cinfo->progressive_mode = is_prog;
  49.249 +  cinfo->arith_code = is_arith;
  49.250 +
  49.251 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.252 +
  49.253 +  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
  49.254 +  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
  49.255 +  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
  49.256 +  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
  49.257 +
  49.258 +  length -= 8;
  49.259 +
  49.260 +  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
  49.261 +	   (int) cinfo->image_width, (int) cinfo->image_height,
  49.262 +	   cinfo->num_components);
  49.263 +
  49.264 +  if (cinfo->marker->saw_SOF)
  49.265 +    ERREXIT(cinfo, JERR_SOF_DUPLICATE);
  49.266 +
  49.267 +  /* We don't support files in which the image height is initially specified */
  49.268 +  /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  49.269 +  /* might as well have a general sanity check. */
  49.270 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  49.271 +      || cinfo->num_components <= 0)
  49.272 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  49.273 +
  49.274 +  if (length != (cinfo->num_components * 3))
  49.275 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.276 +
  49.277 +  if (cinfo->comp_info == NULL)	/* do only once, even if suspend */
  49.278 +    cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
  49.279 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
  49.280 +			 cinfo->num_components * SIZEOF(jpeg_component_info));
  49.281 +  
  49.282 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  49.283 +       ci++, compptr++) {
  49.284 +    compptr->component_index = ci;
  49.285 +    INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
  49.286 +    INPUT_BYTE(cinfo, c, return FALSE);
  49.287 +    compptr->h_samp_factor = (c >> 4) & 15;
  49.288 +    compptr->v_samp_factor = (c     ) & 15;
  49.289 +    INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
  49.290 +
  49.291 +    TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
  49.292 +	     compptr->component_id, compptr->h_samp_factor,
  49.293 +	     compptr->v_samp_factor, compptr->quant_tbl_no);
  49.294 +  }
  49.295 +
  49.296 +  cinfo->marker->saw_SOF = TRUE;
  49.297 +
  49.298 +  INPUT_SYNC(cinfo);
  49.299 +  return TRUE;
  49.300 +}
  49.301 +
  49.302 +
  49.303 +LOCAL(boolean)
  49.304 +get_sos (j_decompress_ptr cinfo)
  49.305 +/* Process a SOS marker */
  49.306 +{
  49.307 +  INT32 length;
  49.308 +  int i, ci, n, c, cc;
  49.309 +  jpeg_component_info * compptr;
  49.310 +  INPUT_VARS(cinfo);
  49.311 +
  49.312 +  if (! cinfo->marker->saw_SOF)
  49.313 +    ERREXIT(cinfo, JERR_SOS_NO_SOF);
  49.314 +
  49.315 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.316 +
  49.317 +  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
  49.318 +
  49.319 +  TRACEMS1(cinfo, 1, JTRC_SOS, n);
  49.320 +
  49.321 +  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
  49.322 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.323 +
  49.324 +  cinfo->comps_in_scan = n;
  49.325 +
  49.326 +  /* Collect the component-spec parameters */
  49.327 +
  49.328 +  for (i = 0; i < n; i++) {
  49.329 +    INPUT_BYTE(cinfo, cc, return FALSE);
  49.330 +    INPUT_BYTE(cinfo, c, return FALSE);
  49.331 +    
  49.332 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  49.333 +	 ci++, compptr++) {
  49.334 +      if (cc == compptr->component_id)
  49.335 +	goto id_found;
  49.336 +    }
  49.337 +
  49.338 +    ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
  49.339 +
  49.340 +  id_found:
  49.341 +
  49.342 +    cinfo->cur_comp_info[i] = compptr;
  49.343 +    compptr->dc_tbl_no = (c >> 4) & 15;
  49.344 +    compptr->ac_tbl_no = (c     ) & 15;
  49.345 +    
  49.346 +    TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
  49.347 +	     compptr->dc_tbl_no, compptr->ac_tbl_no);
  49.348 +  }
  49.349 +
  49.350 +  /* Collect the additional scan parameters Ss, Se, Ah/Al. */
  49.351 +  INPUT_BYTE(cinfo, c, return FALSE);
  49.352 +  cinfo->Ss = c;
  49.353 +  INPUT_BYTE(cinfo, c, return FALSE);
  49.354 +  cinfo->Se = c;
  49.355 +  INPUT_BYTE(cinfo, c, return FALSE);
  49.356 +  cinfo->Ah = (c >> 4) & 15;
  49.357 +  cinfo->Al = (c     ) & 15;
  49.358 +
  49.359 +  TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
  49.360 +	   cinfo->Ah, cinfo->Al);
  49.361 +
  49.362 +  /* Prepare to scan data & restart markers */
  49.363 +  cinfo->marker->next_restart_num = 0;
  49.364 +
  49.365 +  /* Count another SOS marker */
  49.366 +  cinfo->input_scan_number++;
  49.367 +
  49.368 +  INPUT_SYNC(cinfo);
  49.369 +  return TRUE;
  49.370 +}
  49.371 +
  49.372 +
  49.373 +#ifdef D_ARITH_CODING_SUPPORTED
  49.374 +
  49.375 +LOCAL(boolean)
  49.376 +get_dac (j_decompress_ptr cinfo)
  49.377 +/* Process a DAC marker */
  49.378 +{
  49.379 +  INT32 length;
  49.380 +  int index, val;
  49.381 +  INPUT_VARS(cinfo);
  49.382 +
  49.383 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.384 +  length -= 2;
  49.385 +  
  49.386 +  while (length > 0) {
  49.387 +    INPUT_BYTE(cinfo, index, return FALSE);
  49.388 +    INPUT_BYTE(cinfo, val, return FALSE);
  49.389 +
  49.390 +    length -= 2;
  49.391 +
  49.392 +    TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
  49.393 +
  49.394 +    if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  49.395 +      ERREXIT1(cinfo, JERR_DAC_INDEX, index);
  49.396 +
  49.397 +    if (index >= NUM_ARITH_TBLS) { /* define AC table */
  49.398 +      cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  49.399 +    } else {			/* define DC table */
  49.400 +      cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  49.401 +      cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  49.402 +      if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  49.403 +	ERREXIT1(cinfo, JERR_DAC_VALUE, val);
  49.404 +    }
  49.405 +  }
  49.406 +
  49.407 +  if (length != 0)
  49.408 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.409 +
  49.410 +  INPUT_SYNC(cinfo);
  49.411 +  return TRUE;
  49.412 +}
  49.413 +
  49.414 +#else /* ! D_ARITH_CODING_SUPPORTED */
  49.415 +
  49.416 +#define get_dac(cinfo)  skip_variable(cinfo)
  49.417 +
  49.418 +#endif /* D_ARITH_CODING_SUPPORTED */
  49.419 +
  49.420 +
  49.421 +LOCAL(boolean)
  49.422 +get_dht (j_decompress_ptr cinfo)
  49.423 +/* Process a DHT marker */
  49.424 +{
  49.425 +  INT32 length;
  49.426 +  UINT8 bits[17];
  49.427 +  UINT8 huffval[256];
  49.428 +  int i, index, count;
  49.429 +  JHUFF_TBL **htblptr;
  49.430 +  INPUT_VARS(cinfo);
  49.431 +
  49.432 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.433 +  length -= 2;
  49.434 +  
  49.435 +  while (length > 16) {
  49.436 +    INPUT_BYTE(cinfo, index, return FALSE);
  49.437 +
  49.438 +    TRACEMS1(cinfo, 1, JTRC_DHT, index);
  49.439 +      
  49.440 +    bits[0] = 0;
  49.441 +    count = 0;
  49.442 +    for (i = 1; i <= 16; i++) {
  49.443 +      INPUT_BYTE(cinfo, bits[i], return FALSE);
  49.444 +      count += bits[i];
  49.445 +    }
  49.446 +
  49.447 +    length -= 1 + 16;
  49.448 +
  49.449 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  49.450 +	     bits[1], bits[2], bits[3], bits[4],
  49.451 +	     bits[5], bits[6], bits[7], bits[8]);
  49.452 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  49.453 +	     bits[9], bits[10], bits[11], bits[12],
  49.454 +	     bits[13], bits[14], bits[15], bits[16]);
  49.455 +
  49.456 +    /* Here we just do minimal validation of the counts to avoid walking
  49.457 +     * off the end of our table space.  jdhuff.c will check more carefully.
  49.458 +     */
  49.459 +    if (count > 256 || ((INT32) count) > length)
  49.460 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  49.461 +
  49.462 +    for (i = 0; i < count; i++)
  49.463 +      INPUT_BYTE(cinfo, huffval[i], return FALSE);
  49.464 +
  49.465 +    length -= count;
  49.466 +
  49.467 +    if (index & 0x10) {		/* AC table definition */
  49.468 +      index -= 0x10;
  49.469 +      htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  49.470 +    } else {			/* DC table definition */
  49.471 +      htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  49.472 +    }
  49.473 +
  49.474 +    if (index < 0 || index >= NUM_HUFF_TBLS)
  49.475 +      ERREXIT1(cinfo, JERR_DHT_INDEX, index);
  49.476 +
  49.477 +    if (*htblptr == NULL)
  49.478 +      *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  49.479 +  
  49.480 +    MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  49.481 +    MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  49.482 +  }
  49.483 +
  49.484 +  if (length != 0)
  49.485 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.486 +
  49.487 +  INPUT_SYNC(cinfo);
  49.488 +  return TRUE;
  49.489 +}
  49.490 +
  49.491 +
  49.492 +LOCAL(boolean)
  49.493 +get_dqt (j_decompress_ptr cinfo)
  49.494 +/* Process a DQT marker */
  49.495 +{
  49.496 +  INT32 length;
  49.497 +  int n, i, prec;
  49.498 +  unsigned int tmp;
  49.499 +  JQUANT_TBL *quant_ptr;
  49.500 +  INPUT_VARS(cinfo);
  49.501 +
  49.502 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.503 +  length -= 2;
  49.504 +
  49.505 +  while (length > 0) {
  49.506 +    INPUT_BYTE(cinfo, n, return FALSE);
  49.507 +    prec = n >> 4;
  49.508 +    n &= 0x0F;
  49.509 +
  49.510 +    TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
  49.511 +
  49.512 +    if (n >= NUM_QUANT_TBLS)
  49.513 +      ERREXIT1(cinfo, JERR_DQT_INDEX, n);
  49.514 +      
  49.515 +    if (cinfo->quant_tbl_ptrs[n] == NULL)
  49.516 +      cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  49.517 +    quant_ptr = cinfo->quant_tbl_ptrs[n];
  49.518 +
  49.519 +    for (i = 0; i < DCTSIZE2; i++) {
  49.520 +      if (prec)
  49.521 +	INPUT_2BYTES(cinfo, tmp, return FALSE);
  49.522 +      else
  49.523 +	INPUT_BYTE(cinfo, tmp, return FALSE);
  49.524 +      /* We convert the zigzag-order table to natural array order. */
  49.525 +      quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
  49.526 +    }
  49.527 +
  49.528 +    if (cinfo->err->trace_level >= 2) {
  49.529 +      for (i = 0; i < DCTSIZE2; i += 8) {
  49.530 +	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
  49.531 +		 quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
  49.532 +		 quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
  49.533 +		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
  49.534 +		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
  49.535 +      }
  49.536 +    }
  49.537 +
  49.538 +    length -= DCTSIZE2+1;
  49.539 +    if (prec) length -= DCTSIZE2;
  49.540 +  }
  49.541 +
  49.542 +  if (length != 0)
  49.543 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.544 +
  49.545 +  INPUT_SYNC(cinfo);
  49.546 +  return TRUE;
  49.547 +}
  49.548 +
  49.549 +
  49.550 +LOCAL(boolean)
  49.551 +get_dri (j_decompress_ptr cinfo)
  49.552 +/* Process a DRI marker */
  49.553 +{
  49.554 +  INT32 length;
  49.555 +  unsigned int tmp;
  49.556 +  INPUT_VARS(cinfo);
  49.557 +
  49.558 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.559 +  
  49.560 +  if (length != 4)
  49.561 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  49.562 +
  49.563 +  INPUT_2BYTES(cinfo, tmp, return FALSE);
  49.564 +
  49.565 +  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
  49.566 +
  49.567 +  cinfo->restart_interval = tmp;
  49.568 +
  49.569 +  INPUT_SYNC(cinfo);
  49.570 +  return TRUE;
  49.571 +}
  49.572 +
  49.573 +
  49.574 +/*
  49.575 + * Routines for processing APPn and COM markers.
  49.576 + * These are either saved in memory or discarded, per application request.
  49.577 + * APP0 and APP14 are specially checked to see if they are
  49.578 + * JFIF and Adobe markers, respectively.
  49.579 + */
  49.580 +
  49.581 +#define APP0_DATA_LEN	14	/* Length of interesting data in APP0 */
  49.582 +#define APP14_DATA_LEN	12	/* Length of interesting data in APP14 */
  49.583 +#define APPN_DATA_LEN	14	/* Must be the largest of the above!! */
  49.584 +
  49.585 +
  49.586 +LOCAL(void)
  49.587 +examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
  49.588 +	      unsigned int datalen, INT32 remaining)
  49.589 +/* Examine first few bytes from an APP0.
  49.590 + * Take appropriate action if it is a JFIF marker.
  49.591 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
  49.592 + */
  49.593 +{
  49.594 +  INT32 totallen = (INT32) datalen + remaining;
  49.595 +
  49.596 +  if (datalen >= APP0_DATA_LEN &&
  49.597 +      GETJOCTET(data[0]) == 0x4A &&
  49.598 +      GETJOCTET(data[1]) == 0x46 &&
  49.599 +      GETJOCTET(data[2]) == 0x49 &&
  49.600 +      GETJOCTET(data[3]) == 0x46 &&
  49.601 +      GETJOCTET(data[4]) == 0) {
  49.602 +    /* Found JFIF APP0 marker: save info */
  49.603 +    cinfo->saw_JFIF_marker = TRUE;
  49.604 +    cinfo->JFIF_major_version = GETJOCTET(data[5]);
  49.605 +    cinfo->JFIF_minor_version = GETJOCTET(data[6]);
  49.606 +    cinfo->density_unit = GETJOCTET(data[7]);
  49.607 +    cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
  49.608 +    cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
  49.609 +    /* Check version.
  49.610 +     * Major version must be 1, anything else signals an incompatible change.
  49.611 +     * (We used to treat this as an error, but now it's a nonfatal warning,
  49.612 +     * because some bozo at Hijaak couldn't read the spec.)
  49.613 +     * Minor version should be 0..2, but process anyway if newer.
  49.614 +     */
  49.615 +    if (cinfo->JFIF_major_version != 1)
  49.616 +      WARNMS2(cinfo, JWRN_JFIF_MAJOR,
  49.617 +	      cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
  49.618 +    /* Generate trace messages */
  49.619 +    TRACEMS5(cinfo, 1, JTRC_JFIF,
  49.620 +	     cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
  49.621 +	     cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  49.622 +    /* Validate thumbnail dimensions and issue appropriate messages */
  49.623 +    if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
  49.624 +      TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
  49.625 +	       GETJOCTET(data[12]), GETJOCTET(data[13]));
  49.626 +    totallen -= APP0_DATA_LEN;
  49.627 +    if (totallen !=
  49.628 +	((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
  49.629 +      TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
  49.630 +  } else if (datalen >= 6 &&
  49.631 +      GETJOCTET(data[0]) == 0x4A &&
  49.632 +      GETJOCTET(data[1]) == 0x46 &&
  49.633 +      GETJOCTET(data[2]) == 0x58 &&
  49.634 +      GETJOCTET(data[3]) == 0x58 &&
  49.635 +      GETJOCTET(data[4]) == 0) {
  49.636 +    /* Found JFIF "JFXX" extension APP0 marker */
  49.637 +    /* The library doesn't actually do anything with these,
  49.638 +     * but we try to produce a helpful trace message.
  49.639 +     */
  49.640 +    switch (GETJOCTET(data[5])) {
  49.641 +    case 0x10:
  49.642 +      TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
  49.643 +      break;
  49.644 +    case 0x11:
  49.645 +      TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
  49.646 +      break;
  49.647 +    case 0x13:
  49.648 +      TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
  49.649 +      break;
  49.650 +    default:
  49.651 +      TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
  49.652 +	       GETJOCTET(data[5]), (int) totallen);
  49.653 +      break;
  49.654 +    }
  49.655 +  } else {
  49.656 +    /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
  49.657 +    TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
  49.658 +  }
  49.659 +}
  49.660 +
  49.661 +
  49.662 +LOCAL(void)
  49.663 +examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
  49.664 +	       unsigned int datalen, INT32 remaining)
  49.665 +/* Examine first few bytes from an APP14.
  49.666 + * Take appropriate action if it is an Adobe marker.
  49.667 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
  49.668 + */
  49.669 +{
  49.670 +  unsigned int version, flags0, flags1, transform;
  49.671 +
  49.672 +  if (datalen >= APP14_DATA_LEN &&
  49.673 +      GETJOCTET(data[0]) == 0x41 &&
  49.674 +      GETJOCTET(data[1]) == 0x64 &&
  49.675 +      GETJOCTET(data[2]) == 0x6F &&
  49.676 +      GETJOCTET(data[3]) == 0x62 &&
  49.677 +      GETJOCTET(data[4]) == 0x65) {
  49.678 +    /* Found Adobe APP14 marker */
  49.679 +    version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
  49.680 +    flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
  49.681 +    flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
  49.682 +    transform = GETJOCTET(data[11]);
  49.683 +    TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  49.684 +    cinfo->saw_Adobe_marker = TRUE;
  49.685 +    cinfo->Adobe_transform = (UINT8) transform;
  49.686 +  } else {
  49.687 +    /* Start of APP14 does not match "Adobe", or too short */
  49.688 +    TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
  49.689 +  }
  49.690 +}
  49.691 +
  49.692 +
  49.693 +METHODDEF(boolean)
  49.694 +get_interesting_appn (j_decompress_ptr cinfo)
  49.695 +/* Process an APP0 or APP14 marker without saving it */
  49.696 +{
  49.697 +  INT32 length;
  49.698 +  JOCTET b[APPN_DATA_LEN];
  49.699 +  unsigned int i, numtoread;
  49.700 +  INPUT_VARS(cinfo);
  49.701 +
  49.702 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.703 +  length -= 2;
  49.704 +
  49.705 +  /* get the interesting part of the marker data */
  49.706 +  if (length >= APPN_DATA_LEN)
  49.707 +    numtoread = APPN_DATA_LEN;
  49.708 +  else if (length > 0)
  49.709 +    numtoread = (unsigned int) length;
  49.710 +  else
  49.711 +    numtoread = 0;
  49.712 +  for (i = 0; i < numtoread; i++)
  49.713 +    INPUT_BYTE(cinfo, b[i], return FALSE);
  49.714 +  length -= numtoread;
  49.715 +
  49.716 +  /* process it */
  49.717 +  switch (cinfo->unread_marker) {
  49.718 +  case M_APP0:
  49.719 +    examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
  49.720 +    break;
  49.721 +  case M_APP14:
  49.722 +    examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
  49.723 +    break;
  49.724 +  default:
  49.725 +    /* can't get here unless jpeg_save_markers chooses wrong processor */
  49.726 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  49.727 +    break;
  49.728 +  }
  49.729 +
  49.730 +  /* skip any remaining data -- could be lots */
  49.731 +  INPUT_SYNC(cinfo);
  49.732 +  if (length > 0)
  49.733 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  49.734 +
  49.735 +  return TRUE;
  49.736 +}
  49.737 +
  49.738 +
  49.739 +#ifdef SAVE_MARKERS_SUPPORTED
  49.740 +
  49.741 +METHODDEF(boolean)
  49.742 +save_marker (j_decompress_ptr cinfo)
  49.743 +/* Save an APPn or COM marker into the marker list */
  49.744 +{
  49.745 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  49.746 +  jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
  49.747 +  unsigned int bytes_read, data_length;
  49.748 +  JOCTET FAR * data;
  49.749 +  INT32 length = 0;
  49.750 +  INPUT_VARS(cinfo);
  49.751 +
  49.752 +  if (cur_marker == NULL) {
  49.753 +    /* begin reading a marker */
  49.754 +    INPUT_2BYTES(cinfo, length, return FALSE);
  49.755 +    length -= 2;
  49.756 +    if (length >= 0) {		/* watch out for bogus length word */
  49.757 +      /* figure out how much we want to save */
  49.758 +      unsigned int limit;
  49.759 +      if (cinfo->unread_marker == (int) M_COM)
  49.760 +	limit = marker->length_limit_COM;
  49.761 +      else
  49.762 +	limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
  49.763 +      if ((unsigned int) length < limit)
  49.764 +	limit = (unsigned int) length;
  49.765 +      /* allocate and initialize the marker item */
  49.766 +      cur_marker = (jpeg_saved_marker_ptr)
  49.767 +	(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  49.768 +				    SIZEOF(struct jpeg_marker_struct) + limit);
  49.769 +      cur_marker->next = NULL;
  49.770 +      cur_marker->marker = (UINT8) cinfo->unread_marker;
  49.771 +      cur_marker->original_length = (unsigned int) length;
  49.772 +      cur_marker->data_length = limit;
  49.773 +      /* data area is just beyond the jpeg_marker_struct */
  49.774 +      data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
  49.775 +      marker->cur_marker = cur_marker;
  49.776 +      marker->bytes_read = 0;
  49.777 +      bytes_read = 0;
  49.778 +      data_length = limit;
  49.779 +    } else {
  49.780 +      /* deal with bogus length word */
  49.781 +      bytes_read = data_length = 0;
  49.782 +      data = NULL;
  49.783 +    }
  49.784 +  } else {
  49.785 +    /* resume reading a marker */
  49.786 +    bytes_read = marker->bytes_read;
  49.787 +    data_length = cur_marker->data_length;
  49.788 +    data = cur_marker->data + bytes_read;
  49.789 +  }
  49.790 +
  49.791 +  while (bytes_read < data_length) {
  49.792 +    INPUT_SYNC(cinfo);		/* move the restart point to here */
  49.793 +    marker->bytes_read = bytes_read;
  49.794 +    /* If there's not at least one byte in buffer, suspend */
  49.795 +    MAKE_BYTE_AVAIL(cinfo, return FALSE);
  49.796 +    /* Copy bytes with reasonable rapidity */
  49.797 +    while (bytes_read < data_length && bytes_in_buffer > 0) {
  49.798 +      *data++ = *next_input_byte++;
  49.799 +      bytes_in_buffer--;
  49.800 +      bytes_read++;
  49.801 +    }
  49.802 +  }
  49.803 +
  49.804 +  /* Done reading what we want to read */
  49.805 +  if (cur_marker != NULL) {	/* will be NULL if bogus length word */
  49.806 +    /* Add new marker to end of list */
  49.807 +    if (cinfo->marker_list == NULL) {
  49.808 +      cinfo->marker_list = cur_marker;
  49.809 +    } else {
  49.810 +      jpeg_saved_marker_ptr prev = cinfo->marker_list;
  49.811 +      while (prev->next != NULL)
  49.812 +	prev = prev->next;
  49.813 +      prev->next = cur_marker;
  49.814 +    }
  49.815 +    /* Reset pointer & calc remaining data length */
  49.816 +    data = cur_marker->data;
  49.817 +    length = cur_marker->original_length - data_length;
  49.818 +  }
  49.819 +  /* Reset to initial state for next marker */
  49.820 +  marker->cur_marker = NULL;
  49.821 +
  49.822 +  /* Process the marker if interesting; else just make a generic trace msg */
  49.823 +  switch (cinfo->unread_marker) {
  49.824 +  case M_APP0:
  49.825 +    examine_app0(cinfo, data, data_length, length);
  49.826 +    break;
  49.827 +  case M_APP14:
  49.828 +    examine_app14(cinfo, data, data_length, length);
  49.829 +    break;
  49.830 +  default:
  49.831 +    TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
  49.832 +	     (int) (data_length + length));
  49.833 +    break;
  49.834 +  }
  49.835 +
  49.836 +  /* skip any remaining data -- could be lots */
  49.837 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  49.838 +  if (length > 0)
  49.839 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  49.840 +
  49.841 +  return TRUE;
  49.842 +}
  49.843 +
  49.844 +#endif /* SAVE_MARKERS_SUPPORTED */
  49.845 +
  49.846 +
  49.847 +METHODDEF(boolean)
  49.848 +skip_variable (j_decompress_ptr cinfo)
  49.849 +/* Skip over an unknown or uninteresting variable-length marker */
  49.850 +{
  49.851 +  INT32 length;
  49.852 +  INPUT_VARS(cinfo);
  49.853 +
  49.854 +  INPUT_2BYTES(cinfo, length, return FALSE);
  49.855 +  length -= 2;
  49.856 +  
  49.857 +  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  49.858 +
  49.859 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  49.860 +  if (length > 0)
  49.861 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  49.862 +
  49.863 +  return TRUE;
  49.864 +}
  49.865 +
  49.866 +
  49.867 +/*
  49.868 + * Find the next JPEG marker, save it in cinfo->unread_marker.
  49.869 + * Returns FALSE if had to suspend before reaching a marker;
  49.870 + * in that case cinfo->unread_marker is unchanged.
  49.871 + *
  49.872 + * Note that the result might not be a valid marker code,
  49.873 + * but it will never be 0 or FF.
  49.874 + */
  49.875 +
  49.876 +LOCAL(boolean)
  49.877 +next_marker (j_decompress_ptr cinfo)
  49.878 +{
  49.879 +  int c;
  49.880 +  INPUT_VARS(cinfo);
  49.881 +
  49.882 +  for (;;) {
  49.883 +    INPUT_BYTE(cinfo, c, return FALSE);
  49.884 +    /* Skip any non-FF bytes.
  49.885 +     * This may look a bit inefficient, but it will not occur in a valid file.
  49.886 +     * We sync after each discarded byte so that a suspending data source
  49.887 +     * can discard the byte from its buffer.
  49.888 +     */
  49.889 +    while (c != 0xFF) {
  49.890 +      cinfo->marker->discarded_bytes++;
  49.891 +      INPUT_SYNC(cinfo);
  49.892 +      INPUT_BYTE(cinfo, c, return FALSE);
  49.893 +    }
  49.894 +    /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
  49.895 +     * pad bytes, so don't count them in discarded_bytes.  We assume there
  49.896 +     * will not be so many consecutive FF bytes as to overflow a suspending
  49.897 +     * data source's input buffer.
  49.898 +     */
  49.899 +    do {
  49.900 +      INPUT_BYTE(cinfo, c, return FALSE);
  49.901 +    } while (c == 0xFF);
  49.902 +    if (c != 0)
  49.903 +      break;			/* found a valid marker, exit loop */
  49.904 +    /* Reach here if we found a stuffed-zero data sequence (FF/00).
  49.905 +     * Discard it and loop back to try again.
  49.906 +     */
  49.907 +    cinfo->marker->discarded_bytes += 2;
  49.908 +    INPUT_SYNC(cinfo);
  49.909 +  }
  49.910 +
  49.911 +  if (cinfo->marker->discarded_bytes != 0) {
  49.912 +    WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  49.913 +    cinfo->marker->discarded_bytes = 0;
  49.914 +  }
  49.915 +
  49.916 +  cinfo->unread_marker = c;
  49.917 +
  49.918 +  INPUT_SYNC(cinfo);
  49.919 +  return TRUE;
  49.920 +}
  49.921 +
  49.922 +
  49.923 +LOCAL(boolean)
  49.924 +first_marker (j_decompress_ptr cinfo)
  49.925 +/* Like next_marker, but used to obtain the initial SOI marker. */
  49.926 +/* For this marker, we do not allow preceding garbage or fill; otherwise,
  49.927 + * we might well scan an entire input file before realizing it ain't JPEG.
  49.928 + * If an application wants to process non-JFIF files, it must seek to the
  49.929 + * SOI before calling the JPEG library.
  49.930 + */
  49.931 +{
  49.932 +  int c, c2;
  49.933 +  INPUT_VARS(cinfo);
  49.934 +
  49.935 +  INPUT_BYTE(cinfo, c, return FALSE);
  49.936 +  INPUT_BYTE(cinfo, c2, return FALSE);
  49.937 +  if (c != 0xFF || c2 != (int) M_SOI)
  49.938 +    ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  49.939 +
  49.940 +  cinfo->unread_marker = c2;
  49.941 +
  49.942 +  INPUT_SYNC(cinfo);
  49.943 +  return TRUE;
  49.944 +}
  49.945 +
  49.946 +
  49.947 +/*
  49.948 + * Read markers until SOS or EOI.
  49.949 + *
  49.950 + * Returns same codes as are defined for jpeg_consume_input:
  49.951 + * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  49.952 + */
  49.953 +
  49.954 +METHODDEF(int)
  49.955 +read_markers (j_decompress_ptr cinfo)
  49.956 +{
  49.957 +  /* Outer loop repeats once for each marker. */
  49.958 +  for (;;) {
  49.959 +    /* Collect the marker proper, unless we already did. */
  49.960 +    /* NB: first_marker() enforces the requirement that SOI appear first. */
  49.961 +    if (cinfo->unread_marker == 0) {
  49.962 +      if (! cinfo->marker->saw_SOI) {
  49.963 +	if (! first_marker(cinfo))
  49.964 +	  return JPEG_SUSPENDED;
  49.965 +      } else {
  49.966 +	if (! next_marker(cinfo))
  49.967 +	  return JPEG_SUSPENDED;
  49.968 +      }
  49.969 +    }
  49.970 +    /* At this point cinfo->unread_marker contains the marker code and the
  49.971 +     * input point is just past the marker proper, but before any parameters.
  49.972 +     * A suspension will cause us to return with this state still true.
  49.973 +     */
  49.974 +    switch (cinfo->unread_marker) {
  49.975 +    case M_SOI:
  49.976 +      if (! get_soi(cinfo))
  49.977 +	return JPEG_SUSPENDED;
  49.978 +      break;
  49.979 +
  49.980 +    case M_SOF0:		/* Baseline */
  49.981 +    case M_SOF1:		/* Extended sequential, Huffman */
  49.982 +      if (! get_sof(cinfo, FALSE, FALSE))
  49.983 +	return JPEG_SUSPENDED;
  49.984 +      break;
  49.985 +
  49.986 +    case M_SOF2:		/* Progressive, Huffman */
  49.987 +      if (! get_sof(cinfo, TRUE, FALSE))
  49.988 +	return JPEG_SUSPENDED;
  49.989 +      break;
  49.990 +
  49.991 +    case M_SOF9:		/* Extended sequential, arithmetic */
  49.992 +      if (! get_sof(cinfo, FALSE, TRUE))
  49.993 +	return JPEG_SUSPENDED;
  49.994 +      break;
  49.995 +
  49.996 +    case M_SOF10:		/* Progressive, arithmetic */
  49.997 +      if (! get_sof(cinfo, TRUE, TRUE))
  49.998 +	return JPEG_SUSPENDED;
  49.999 +      break;
 49.1000 +
 49.1001 +    /* Currently unsupported SOFn types */
 49.1002 +    case M_SOF3:		/* Lossless, Huffman */
 49.1003 +    case M_SOF5:		/* Differential sequential, Huffman */
 49.1004 +    case M_SOF6:		/* Differential progressive, Huffman */
 49.1005 +    case M_SOF7:		/* Differential lossless, Huffman */
 49.1006 +    case M_JPG:			/* Reserved for JPEG extensions */
 49.1007 +    case M_SOF11:		/* Lossless, arithmetic */
 49.1008 +    case M_SOF13:		/* Differential sequential, arithmetic */
 49.1009 +    case M_SOF14:		/* Differential progressive, arithmetic */
 49.1010 +    case M_SOF15:		/* Differential lossless, arithmetic */
 49.1011 +      ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
 49.1012 +      break;
 49.1013 +
 49.1014 +    case M_SOS:
 49.1015 +      if (! get_sos(cinfo))
 49.1016 +	return JPEG_SUSPENDED;
 49.1017 +      cinfo->unread_marker = 0;	/* processed the marker */
 49.1018 +      return JPEG_REACHED_SOS;
 49.1019 +    
 49.1020 +    case M_EOI:
 49.1021 +      TRACEMS(cinfo, 1, JTRC_EOI);
 49.1022 +      cinfo->unread_marker = 0;	/* processed the marker */
 49.1023 +      return JPEG_REACHED_EOI;
 49.1024 +      
 49.1025 +    case M_DAC:
 49.1026 +      if (! get_dac(cinfo))
 49.1027 +	return JPEG_SUSPENDED;
 49.1028 +      break;
 49.1029 +      
 49.1030 +    case M_DHT:
 49.1031 +      if (! get_dht(cinfo))
 49.1032 +	return JPEG_SUSPENDED;
 49.1033 +      break;
 49.1034 +      
 49.1035 +    case M_DQT:
 49.1036 +      if (! get_dqt(cinfo))
 49.1037 +	return JPEG_SUSPENDED;
 49.1038 +      break;
 49.1039 +      
 49.1040 +    case M_DRI:
 49.1041 +      if (! get_dri(cinfo))
 49.1042 +	return JPEG_SUSPENDED;
 49.1043 +      break;
 49.1044 +      
 49.1045 +    case M_APP0:
 49.1046 +    case M_APP1:
 49.1047 +    case M_APP2:
 49.1048 +    case M_APP3:
 49.1049 +    case M_APP4:
 49.1050 +    case M_APP5:
 49.1051 +    case M_APP6:
 49.1052 +    case M_APP7:
 49.1053 +    case M_APP8:
 49.1054 +    case M_APP9:
 49.1055 +    case M_APP10:
 49.1056 +    case M_APP11:
 49.1057 +    case M_APP12:
 49.1058 +    case M_APP13:
 49.1059 +    case M_APP14:
 49.1060 +    case M_APP15:
 49.1061 +      if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
 49.1062 +		cinfo->unread_marker - (int) M_APP0]) (cinfo))
 49.1063 +	return JPEG_SUSPENDED;
 49.1064 +      break;
 49.1065 +      
 49.1066 +    case M_COM:
 49.1067 +      if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
 49.1068 +	return JPEG_SUSPENDED;
 49.1069 +      break;
 49.1070 +
 49.1071 +    case M_RST0:		/* these are all parameterless */
 49.1072 +    case M_RST1:
 49.1073 +    case M_RST2:
 49.1074 +    case M_RST3:
 49.1075 +    case M_RST4:
 49.1076 +    case M_RST5:
 49.1077 +    case M_RST6:
 49.1078 +    case M_RST7:
 49.1079 +    case M_TEM:
 49.1080 +      TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
 49.1081 +      break;
 49.1082 +
 49.1083 +    case M_DNL:			/* Ignore DNL ... perhaps the wrong thing */
 49.1084 +      if (! skip_variable(cinfo))
 49.1085 +	return JPEG_SUSPENDED;
 49.1086 +      break;
 49.1087 +
 49.1088 +    default:			/* must be DHP, EXP, JPGn, or RESn */
 49.1089 +      /* For now, we treat the reserved markers as fatal errors since they are
 49.1090 +       * likely to be used to signal incompatible JPEG Part 3 extensions.
 49.1091 +       * Once the JPEG 3 version-number marker is well defined, this code
 49.1092 +       * ought to change!
 49.1093 +       */
 49.1094 +      ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
 49.1095 +      break;
 49.1096 +    }
 49.1097 +    /* Successfully processed marker, so reset state variable */
 49.1098 +    cinfo->unread_marker = 0;
 49.1099 +  } /* end loop */
 49.1100 +}
 49.1101 +
 49.1102 +
 49.1103 +/*
 49.1104 + * Read a restart marker, which is expected to appear next in the datastream;
 49.1105 + * if the marker is not there, take appropriate recovery action.
 49.1106 + * Returns FALSE if suspension is required.
 49.1107 + *
 49.1108 + * This is called by the entropy decoder after it has read an appropriate
 49.1109 + * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
 49.1110 + * has already read a marker from the data source.  Under normal conditions
 49.1111 + * cinfo->unread_marker will be reset to 0 before returning; if not reset,
 49.1112 + * it holds a marker which the decoder will be unable to read past.
 49.1113 + */
 49.1114 +
 49.1115 +METHODDEF(boolean)
 49.1116 +read_restart_marker (j_decompress_ptr cinfo)
 49.1117 +{
 49.1118 +  /* Obtain a marker unless we already did. */
 49.1119 +  /* Note that next_marker will complain if it skips any data. */
 49.1120 +  if (cinfo->unread_marker == 0) {
 49.1121 +    if (! next_marker(cinfo))
 49.1122 +      return FALSE;
 49.1123 +  }
 49.1124 +
 49.1125 +  if (cinfo->unread_marker ==
 49.1126 +      ((int) M_RST0 + cinfo->marker->next_restart_num)) {
 49.1127 +    /* Normal case --- swallow the marker and let entropy decoder continue */
 49.1128 +    TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
 49.1129 +    cinfo->unread_marker = 0;
 49.1130 +  } else {
 49.1131 +    /* Uh-oh, the restart markers have been messed up. */
 49.1132 +    /* Let the data source manager determine how to resync. */
 49.1133 +    if (! (*cinfo->src->resync_to_restart) (cinfo,
 49.1134 +					    cinfo->marker->next_restart_num))
 49.1135 +      return FALSE;
 49.1136 +  }
 49.1137 +
 49.1138 +  /* Update next-restart state */
 49.1139 +  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
 49.1140 +
 49.1141 +  return TRUE;
 49.1142 +}
 49.1143 +
 49.1144 +
 49.1145 +/*
 49.1146 + * This is the default resync_to_restart method for data source managers
 49.1147 + * to use if they don't have any better approach.  Some data source managers
 49.1148 + * may be able to back up, or may have additional knowledge about the data
 49.1149 + * which permits a more intelligent recovery strategy; such managers would
 49.1150 + * presumably supply their own resync method.
 49.1151 + *
 49.1152 + * read_restart_marker calls resync_to_restart if it finds a marker other than
 49.1153 + * the restart marker it was expecting.  (This code is *not* used unless
 49.1154 + * a nonzero restart interval has been declared.)  cinfo->unread_marker is
 49.1155 + * the marker code actually found (might be anything, except 0 or FF).
 49.1156 + * The desired restart marker number (0..7) is passed as a parameter.
 49.1157 + * This routine is supposed to apply whatever error recovery strategy seems
 49.1158 + * appropriate in order to position the input stream to the next data segment.
 49.1159 + * Note that cinfo->unread_marker is treated as a marker appearing before
 49.1160 + * the current data-source input point; usually it should be reset to zero
 49.1161 + * before returning.
 49.1162 + * Returns FALSE if suspension is required.
 49.1163 + *
 49.1164 + * This implementation is substantially constrained by wanting to treat the
 49.1165 + * input as a data stream; this means we can't back up.  Therefore, we have
 49.1166 + * only the following actions to work with:
 49.1167 + *   1. Simply discard the marker and let the entropy decoder resume at next
 49.1168 + *      byte of file.
 49.1169 + *   2. Read forward until we find another marker, discarding intervening
 49.1170 + *      data.  (In theory we could look ahead within the current bufferload,
 49.1171 + *      without having to discard data if we don't find the desired marker.
 49.1172 + *      This idea is not implemented here, in part because it makes behavior
 49.1173 + *      dependent on buffer size and chance buffer-boundary positions.)
 49.1174 + *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
 49.1175 + *      This will cause the entropy decoder to process an empty data segment,
 49.1176 + *      inserting dummy zeroes, and then we will reprocess the marker.
 49.1177 + *
 49.1178 + * #2 is appropriate if we think the desired marker lies ahead, while #3 is
 49.1179 + * appropriate if the found marker is a future restart marker (indicating
 49.1180 + * that we have missed the desired restart marker, probably because it got
 49.1181 + * corrupted).
 49.1182 + * We apply #2 or #3 if the found marker is a restart marker no more than
 49.1183 + * two counts behind or ahead of the expected one.  We also apply #2 if the
 49.1184 + * found marker is not a legal JPEG marker code (it's certainly bogus data).
 49.1185 + * If the found marker is a restart marker more than 2 counts away, we do #1
 49.1186 + * (too much risk that the marker is erroneous; with luck we will be able to
 49.1187 + * resync at some future point).
 49.1188 + * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
 49.1189 + * overrunning the end of a scan.  An implementation limited to single-scan
 49.1190 + * files might find it better to apply #2 for markers other than EOI, since
 49.1191 + * any other marker would have to be bogus data in that case.
 49.1192 + */
 49.1193 +
 49.1194 +GLOBAL(boolean)
 49.1195 +jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
 49.1196 +{
 49.1197 +  int marker = cinfo->unread_marker;
 49.1198 +  int action = 1;
 49.1199 +  
 49.1200 +  /* Always put up a warning. */
 49.1201 +  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
 49.1202 +  
 49.1203 +  /* Outer loop handles repeated decision after scanning forward. */
 49.1204 +  for (;;) {
 49.1205 +    if (marker < (int) M_SOF0)
 49.1206 +      action = 2;		/* invalid marker */
 49.1207 +    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
 49.1208 +      action = 3;		/* valid non-restart marker */
 49.1209 +    else {
 49.1210 +      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
 49.1211 +	  marker == ((int) M_RST0 + ((desired+2) & 7)))
 49.1212 +	action = 3;		/* one of the next two expected restarts */
 49.1213 +      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
 49.1214 +	       marker == ((int) M_RST0 + ((desired-2) & 7)))
 49.1215 +	action = 2;		/* a prior restart, so advance */
 49.1216 +      else
 49.1217 +	action = 1;		/* desired restart or too far away */
 49.1218 +    }
 49.1219 +    TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
 49.1220 +    switch (action) {
 49.1221 +    case 1:
 49.1222 +      /* Discard marker and let entropy decoder resume processing. */
 49.1223 +      cinfo->unread_marker = 0;
 49.1224 +      return TRUE;
 49.1225 +    case 2:
 49.1226 +      /* Scan to the next marker, and repeat the decision loop. */
 49.1227 +      if (! next_marker(cinfo))
 49.1228 +	return FALSE;
 49.1229 +      marker = cinfo->unread_marker;
 49.1230 +      break;
 49.1231 +    case 3:
 49.1232 +      /* Return without advancing past this marker. */
 49.1233 +      /* Entropy decoder will be forced to process an empty segment. */
 49.1234 +      return TRUE;
 49.1235 +    }
 49.1236 +  } /* end loop */
 49.1237 +}
 49.1238 +
 49.1239 +
 49.1240 +/*
 49.1241 + * Reset marker processing state to begin a fresh datastream.
 49.1242 + */
 49.1243 +
 49.1244 +METHODDEF(void)
 49.1245 +reset_marker_reader (j_decompress_ptr cinfo)
 49.1246 +{
 49.1247 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 49.1248 +
 49.1249 +  cinfo->comp_info = NULL;		/* until allocated by get_sof */
 49.1250 +  cinfo->input_scan_number = 0;		/* no SOS seen yet */
 49.1251 +  cinfo->unread_marker = 0;		/* no pending marker */
 49.1252 +  marker->pub.saw_SOI = FALSE;		/* set internal state too */
 49.1253 +  marker->pub.saw_SOF = FALSE;
 49.1254 +  marker->pub.discarded_bytes = 0;
 49.1255 +  marker->cur_marker = NULL;
 49.1256 +}
 49.1257 +
 49.1258 +
 49.1259 +/*
 49.1260 + * Initialize the marker reader module.
 49.1261 + * This is called only once, when the decompression object is created.
 49.1262 + */
 49.1263 +
 49.1264 +GLOBAL(void)
 49.1265 +jinit_marker_reader (j_decompress_ptr cinfo)
 49.1266 +{
 49.1267 +  my_marker_ptr marker;
 49.1268 +  int i;
 49.1269 +
 49.1270 +  /* Create subobject in permanent pool */
 49.1271 +  marker = (my_marker_ptr)
 49.1272 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
 49.1273 +				SIZEOF(my_marker_reader));
 49.1274 +  cinfo->marker = (struct jpeg_marker_reader *) marker;
 49.1275 +  /* Initialize public method pointers */
 49.1276 +  marker->pub.reset_marker_reader = reset_marker_reader;
 49.1277 +  marker->pub.read_markers = read_markers;
 49.1278 +  marker->pub.read_restart_marker = read_restart_marker;
 49.1279 +  /* Initialize COM/APPn processing.
 49.1280 +   * By default, we examine and then discard APP0 and APP14,
 49.1281 +   * but simply discard COM and all other APPn.
 49.1282 +   */
 49.1283 +  marker->process_COM = skip_variable;
 49.1284 +  marker->length_limit_COM = 0;
 49.1285 +  for (i = 0; i < 16; i++) {
 49.1286 +    marker->process_APPn[i] = skip_variable;
 49.1287 +    marker->length_limit_APPn[i] = 0;
 49.1288 +  }
 49.1289 +  marker->process_APPn[0] = get_interesting_appn;
 49.1290 +  marker->process_APPn[14] = get_interesting_appn;
 49.1291 +  /* Reset marker processing state */
 49.1292 +  reset_marker_reader(cinfo);
 49.1293 +}
 49.1294 +
 49.1295 +
 49.1296 +/*
 49.1297 + * Control saving of COM and APPn markers into marker_list.
 49.1298 + */
 49.1299 +
 49.1300 +#ifdef SAVE_MARKERS_SUPPORTED
 49.1301 +
 49.1302 +GLOBAL(void)
 49.1303 +jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
 49.1304 +		   unsigned int length_limit)
 49.1305 +{
 49.1306 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 49.1307 +  long maxlength;
 49.1308 +  jpeg_marker_parser_method processor;
 49.1309 +
 49.1310 +  /* Length limit mustn't be larger than what we can allocate
 49.1311 +   * (should only be a concern in a 16-bit environment).
 49.1312 +   */
 49.1313 +  maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
 49.1314 +  if (((long) length_limit) > maxlength)
 49.1315 +    length_limit = (unsigned int) maxlength;
 49.1316 +
 49.1317 +  /* Choose processor routine to use.
 49.1318 +   * APP0/APP14 have special requirements.
 49.1319 +   */
 49.1320 +  if (length_limit) {
 49.1321 +    processor = save_marker;
 49.1322 +    /* If saving APP0/APP14, save at least enough for our internal use. */
 49.1323 +    if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
 49.1324 +      length_limit = APP0_DATA_LEN;
 49.1325 +    else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
 49.1326 +      length_limit = APP14_DATA_LEN;
 49.1327 +  } else {
 49.1328 +    processor = skip_variable;
 49.1329 +    /* If discarding APP0/APP14, use our regular on-the-fly processor. */
 49.1330 +    if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
 49.1331 +      processor = get_interesting_appn;
 49.1332 +  }
 49.1333 +
 49.1334 +  if (marker_code == (int) M_COM) {
 49.1335 +    marker->process_COM = processor;
 49.1336 +    marker->length_limit_COM = length_limit;
 49.1337 +  } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
 49.1338 +    marker->process_APPn[marker_code - (int) M_APP0] = processor;
 49.1339 +    marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
 49.1340 +  } else
 49.1341 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
 49.1342 +}
 49.1343 +
 49.1344 +#endif /* SAVE_MARKERS_SUPPORTED */
 49.1345 +
 49.1346 +
 49.1347 +/*
 49.1348 + * Install a special processing method for COM or APPn markers.
 49.1349 + */
 49.1350 +
 49.1351 +GLOBAL(void)
 49.1352 +jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
 49.1353 +			   jpeg_marker_parser_method routine)
 49.1354 +{
 49.1355 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 49.1356 +
 49.1357 +  if (marker_code == (int) M_COM)
 49.1358 +    marker->process_COM = routine;
 49.1359 +  else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
 49.1360 +    marker->process_APPn[marker_code - (int) M_APP0] = routine;
 49.1361 +  else
 49.1362 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
 49.1363 +}
    50.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    50.2 +++ b/libs/libjpeg/jdmaster.c	Sun Jun 07 17:25:49 2015 +0300
    50.3 @@ -0,0 +1,557 @@
    50.4 +/*
    50.5 + * jdmaster.c
    50.6 + *
    50.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    50.8 + * This file is part of the Independent JPEG Group's software.
    50.9 + * For conditions of distribution and use, see the accompanying README file.
   50.10 + *
   50.11 + * This file contains master control logic for the JPEG decompressor.
   50.12 + * These routines are concerned with selecting the modules to be executed
   50.13 + * and with determining the number of passes and the work to be done in each
   50.14 + * pass.
   50.15 + */
   50.16 +
   50.17 +#define JPEG_INTERNALS
   50.18 +#include "jinclude.h"
   50.19 +#include "jpeglib.h"
   50.20 +
   50.21 +
   50.22 +/* Private state */
   50.23 +
   50.24 +typedef struct {
   50.25 +  struct jpeg_decomp_master pub; /* public fields */
   50.26 +
   50.27 +  int pass_number;		/* # of passes completed */
   50.28 +
   50.29 +  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
   50.30 +
   50.31 +  /* Saved references to initialized quantizer modules,
   50.32 +   * in case we need to switch modes.
   50.33 +   */
   50.34 +  struct jpeg_color_quantizer * quantizer_1pass;
   50.35 +  struct jpeg_color_quantizer * quantizer_2pass;
   50.36 +} my_decomp_master;
   50.37 +
   50.38 +typedef my_decomp_master * my_master_ptr;
   50.39 +
   50.40 +
   50.41 +/*
   50.42 + * Determine whether merged upsample/color conversion should be used.
   50.43 + * CRUCIAL: this must match the actual capabilities of jdmerge.c!
   50.44 + */
   50.45 +
   50.46 +LOCAL(boolean)
   50.47 +use_merged_upsample (j_decompress_ptr cinfo)
   50.48 +{
   50.49 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   50.50 +  /* Merging is the equivalent of plain box-filter upsampling */
   50.51 +  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
   50.52 +    return FALSE;
   50.53 +  /* jdmerge.c only supports YCC=>RGB color conversion */
   50.54 +  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
   50.55 +      cinfo->out_color_space != JCS_RGB ||
   50.56 +      cinfo->out_color_components != RGB_PIXELSIZE)
   50.57 +    return FALSE;
   50.58 +  /* and it only handles 2h1v or 2h2v sampling ratios */
   50.59 +  if (cinfo->comp_info[0].h_samp_factor != 2 ||
   50.60 +      cinfo->comp_info[1].h_samp_factor != 1 ||
   50.61 +      cinfo->comp_info[2].h_samp_factor != 1 ||
   50.62 +      cinfo->comp_info[0].v_samp_factor >  2 ||
   50.63 +      cinfo->comp_info[1].v_samp_factor != 1 ||
   50.64 +      cinfo->comp_info[2].v_samp_factor != 1)
   50.65 +    return FALSE;
   50.66 +  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
   50.67 +  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
   50.68 +      cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
   50.69 +      cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
   50.70 +    return FALSE;
   50.71 +  /* ??? also need to test for upsample-time rescaling, when & if supported */
   50.72 +  return TRUE;			/* by golly, it'll work... */
   50.73 +#else
   50.74 +  return FALSE;
   50.75 +#endif
   50.76 +}
   50.77 +
   50.78 +
   50.79 +/*
   50.80 + * Compute output image dimensions and related values.
   50.81 + * NOTE: this is exported for possible use by application.
   50.82 + * Hence it mustn't do anything that can't be done twice.
   50.83 + * Also note that it may be called before the master module is initialized!
   50.84 + */
   50.85 +
   50.86 +GLOBAL(void)
   50.87 +jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
   50.88 +/* Do computations that are needed before master selection phase */
   50.89 +{
   50.90 +#ifdef IDCT_SCALING_SUPPORTED
   50.91 +  int ci;
   50.92 +  jpeg_component_info *compptr;
   50.93 +#endif
   50.94 +
   50.95 +  /* Prevent application from calling me at wrong times */
   50.96 +  if (cinfo->global_state != DSTATE_READY)
   50.97 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   50.98 +
   50.99 +#ifdef IDCT_SCALING_SUPPORTED
  50.100 +
  50.101 +  /* Compute actual output image dimensions and DCT scaling choices. */
  50.102 +  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
  50.103 +    /* Provide 1/8 scaling */
  50.104 +    cinfo->output_width = (JDIMENSION)
  50.105 +      jdiv_round_up((long) cinfo->image_width, 8L);
  50.106 +    cinfo->output_height = (JDIMENSION)
  50.107 +      jdiv_round_up((long) cinfo->image_height, 8L);
  50.108 +    cinfo->min_DCT_scaled_size = 1;
  50.109 +  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
  50.110 +    /* Provide 1/4 scaling */
  50.111 +    cinfo->output_width = (JDIMENSION)
  50.112 +      jdiv_round_up((long) cinfo->image_width, 4L);
  50.113 +    cinfo->output_height = (JDIMENSION)
  50.114 +      jdiv_round_up((long) cinfo->image_height, 4L);
  50.115 +    cinfo->min_DCT_scaled_size = 2;
  50.116 +  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
  50.117 +    /* Provide 1/2 scaling */
  50.118 +    cinfo->output_width = (JDIMENSION)
  50.119 +      jdiv_round_up((long) cinfo->image_width, 2L);
  50.120 +    cinfo->output_height = (JDIMENSION)
  50.121 +      jdiv_round_up((long) cinfo->image_height, 2L);
  50.122 +    cinfo->min_DCT_scaled_size = 4;
  50.123 +  } else {
  50.124 +    /* Provide 1/1 scaling */
  50.125 +    cinfo->output_width = cinfo->image_width;
  50.126 +    cinfo->output_height = cinfo->image_height;
  50.127 +    cinfo->min_DCT_scaled_size = DCTSIZE;
  50.128 +  }
  50.129 +  /* In selecting the actual DCT scaling for each component, we try to
  50.130 +   * scale up the chroma components via IDCT scaling rather than upsampling.
  50.131 +   * This saves time if the upsampler gets to use 1:1 scaling.
  50.132 +   * Note this code assumes that the supported DCT scalings are powers of 2.
  50.133 +   */
  50.134 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  50.135 +       ci++, compptr++) {
  50.136 +    int ssize = cinfo->min_DCT_scaled_size;
  50.137 +    while (ssize < DCTSIZE &&
  50.138 +	   (compptr->h_samp_factor * ssize * 2 <=
  50.139 +	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
  50.140 +	   (compptr->v_samp_factor * ssize * 2 <=
  50.141 +	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
  50.142 +      ssize = ssize * 2;
  50.143 +    }
  50.144 +    compptr->DCT_scaled_size = ssize;
  50.145 +  }
  50.146 +
  50.147 +  /* Recompute downsampled dimensions of components;
  50.148 +   * application needs to know these if using raw downsampled data.
  50.149 +   */
  50.150 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  50.151 +       ci++, compptr++) {
  50.152 +    /* Size in samples, after IDCT scaling */
  50.153 +    compptr->downsampled_width = (JDIMENSION)
  50.154 +      jdiv_round_up((long) cinfo->image_width *
  50.155 +		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
  50.156 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
  50.157 +    compptr->downsampled_height = (JDIMENSION)
  50.158 +      jdiv_round_up((long) cinfo->image_height *
  50.159 +		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
  50.160 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  50.161 +  }
  50.162 +
  50.163 +#else /* !IDCT_SCALING_SUPPORTED */
  50.164 +
  50.165 +  /* Hardwire it to "no scaling" */
  50.166 +  cinfo->output_width = cinfo->image_width;
  50.167 +  cinfo->output_height = cinfo->image_height;
  50.168 +  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
  50.169 +   * and has computed unscaled downsampled_width and downsampled_height.
  50.170 +   */
  50.171 +
  50.172 +#endif /* IDCT_SCALING_SUPPORTED */
  50.173 +
  50.174 +  /* Report number of components in selected colorspace. */
  50.175 +  /* Probably this should be in the color conversion module... */
  50.176 +  switch (cinfo->out_color_space) {
  50.177 +  case JCS_GRAYSCALE:
  50.178 +    cinfo->out_color_components = 1;
  50.179 +    break;
  50.180 +  case JCS_RGB:
  50.181 +#if RGB_PIXELSIZE != 3
  50.182 +    cinfo->out_color_components = RGB_PIXELSIZE;
  50.183 +    break;
  50.184 +#endif /* else share code with YCbCr */
  50.185 +  case JCS_YCbCr:
  50.186 +    cinfo->out_color_components = 3;
  50.187 +    break;
  50.188 +  case JCS_CMYK:
  50.189 +  case JCS_YCCK:
  50.190 +    cinfo->out_color_components = 4;
  50.191 +    break;
  50.192 +  default:			/* else must be same colorspace as in file */
  50.193 +    cinfo->out_color_components = cinfo->num_components;
  50.194 +    break;
  50.195 +  }
  50.196 +  cinfo->output_components = (cinfo->quantize_colors ? 1 :
  50.197 +			      cinfo->out_color_components);
  50.198 +
  50.199 +  /* See if upsampler will want to emit more than one row at a time */
  50.200 +  if (use_merged_upsample(cinfo))
  50.201 +    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  50.202 +  else
  50.203 +    cinfo->rec_outbuf_height = 1;
  50.204 +}
  50.205 +
  50.206 +
  50.207 +/*
  50.208 + * Several decompression processes need to range-limit values to the range
  50.209 + * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  50.210 + * due to noise introduced by quantization, roundoff error, etc.  These
  50.211 + * processes are inner loops and need to be as fast as possible.  On most
  50.212 + * machines, particularly CPUs with pipelines or instruction prefetch,
  50.213 + * a (subscript-check-less) C table lookup
  50.214 + *		x = sample_range_limit[x];
  50.215 + * is faster than explicit tests
  50.216 + *		if (x < 0)  x = 0;
  50.217 + *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  50.218 + * These processes all use a common table prepared by the routine below.
  50.219 + *
  50.220 + * For most steps we can mathematically guarantee that the initial value
  50.221 + * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  50.222 + * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  50.223 + * limiting step (just after the IDCT), a wildly out-of-range value is 
  50.224 + * possible if the input data is corrupt.  To avoid any chance of indexing
  50.225 + * off the end of memory and getting a bad-pointer trap, we perform the
  50.226 + * post-IDCT limiting thus:
  50.227 + *		x = range_limit[x & MASK];
  50.228 + * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  50.229 + * samples.  Under normal circumstances this is more than enough range and
  50.230 + * a correct output will be generated; with bogus input data the mask will
  50.231 + * cause wraparound, and we will safely generate a bogus-but-in-range output.
  50.232 + * For the post-IDCT step, we want to convert the data from signed to unsigned
  50.233 + * representation by adding CENTERJSAMPLE at the same time that we limit it.
  50.234 + * So the post-IDCT limiting table ends up looking like this:
  50.235 + *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  50.236 + *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  50.237 + *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  50.238 + *   0,1,...,CENTERJSAMPLE-1
  50.239 + * Negative inputs select values from the upper half of the table after
  50.240 + * masking.
  50.241 + *
  50.242 + * We can save some space by overlapping the start of the post-IDCT table
  50.243 + * with the simpler range limiting table.  The post-IDCT table begins at
  50.244 + * sample_range_limit + CENTERJSAMPLE.
  50.245 + *
  50.246 + * Note that the table is allocated in near data space on PCs; it's small
  50.247 + * enough and used often enough to justify this.
  50.248 + */
  50.249 +
  50.250 +LOCAL(void)
  50.251 +prepare_range_limit_table (j_decompress_ptr cinfo)
  50.252 +/* Allocate and fill in the sample_range_limit table */
  50.253 +{
  50.254 +  JSAMPLE * table;
  50.255 +  int i;
  50.256 +
  50.257 +  table = (JSAMPLE *)
  50.258 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  50.259 +		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  50.260 +  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
  50.261 +  cinfo->sample_range_limit = table;
  50.262 +  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  50.263 +  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  50.264 +  /* Main part of "simple" table: limit[x] = x */
  50.265 +  for (i = 0; i <= MAXJSAMPLE; i++)
  50.266 +    table[i] = (JSAMPLE) i;
  50.267 +  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
  50.268 +  /* End of simple table, rest of first half of post-IDCT table */
  50.269 +  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  50.270 +    table[i] = MAXJSAMPLE;
  50.271 +  /* Second half of post-IDCT table */
  50.272 +  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  50.273 +	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  50.274 +  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  50.275 +	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  50.276 +}
  50.277 +
  50.278 +
  50.279 +/*
  50.280 + * Master selection of decompression modules.
  50.281 + * This is done once at jpeg_start_decompress time.  We determine
  50.282 + * which modules will be used and give them appropriate initialization calls.
  50.283 + * We also initialize the decompressor input side to begin consuming data.
  50.284 + *
  50.285 + * Since jpeg_read_header has finished, we know what is in the SOF
  50.286 + * and (first) SOS markers.  We also have all the application parameter
  50.287 + * settings.
  50.288 + */
  50.289 +
  50.290 +LOCAL(void)
  50.291 +master_selection (j_decompress_ptr cinfo)
  50.292 +{
  50.293 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  50.294 +  boolean use_c_buffer;
  50.295 +  long samplesperrow;
  50.296 +  JDIMENSION jd_samplesperrow;
  50.297 +
  50.298 +  /* Initialize dimensions and other stuff */
  50.299 +  jpeg_calc_output_dimensions(cinfo);
  50.300 +  prepare_range_limit_table(cinfo);
  50.301 +
  50.302 +  /* Width of an output scanline must be representable as JDIMENSION. */
  50.303 +  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  50.304 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
  50.305 +  if ((long) jd_samplesperrow != samplesperrow)
  50.306 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  50.307 +
  50.308 +  /* Initialize my private state */
  50.309 +  master->pass_number = 0;
  50.310 +  master->using_merged_upsample = use_merged_upsample(cinfo);
  50.311 +
  50.312 +  /* Color quantizer selection */
  50.313 +  master->quantizer_1pass = NULL;
  50.314 +  master->quantizer_2pass = NULL;
  50.315 +  /* No mode changes if not using buffered-image mode. */
  50.316 +  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
  50.317 +    cinfo->enable_1pass_quant = FALSE;
  50.318 +    cinfo->enable_external_quant = FALSE;
  50.319 +    cinfo->enable_2pass_quant = FALSE;
  50.320 +  }
  50.321 +  if (cinfo->quantize_colors) {
  50.322 +    if (cinfo->raw_data_out)
  50.323 +      ERREXIT(cinfo, JERR_NOTIMPL);
  50.324 +    /* 2-pass quantizer only works in 3-component color space. */
  50.325 +    if (cinfo->out_color_components != 3) {
  50.326 +      cinfo->enable_1pass_quant = TRUE;
  50.327 +      cinfo->enable_external_quant = FALSE;
  50.328 +      cinfo->enable_2pass_quant = FALSE;
  50.329 +      cinfo->colormap = NULL;
  50.330 +    } else if (cinfo->colormap != NULL) {
  50.331 +      cinfo->enable_external_quant = TRUE;
  50.332 +    } else if (cinfo->two_pass_quantize) {
  50.333 +      cinfo->enable_2pass_quant = TRUE;
  50.334 +    } else {
  50.335 +      cinfo->enable_1pass_quant = TRUE;
  50.336 +    }
  50.337 +
  50.338 +    if (cinfo->enable_1pass_quant) {
  50.339 +#ifdef QUANT_1PASS_SUPPORTED
  50.340 +      jinit_1pass_quantizer(cinfo);
  50.341 +      master->quantizer_1pass = cinfo->cquantize;
  50.342 +#else
  50.343 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  50.344 +#endif
  50.345 +    }
  50.346 +
  50.347 +    /* We use the 2-pass code to map to external colormaps. */
  50.348 +    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  50.349 +#ifdef QUANT_2PASS_SUPPORTED
  50.350 +      jinit_2pass_quantizer(cinfo);
  50.351 +      master->quantizer_2pass = cinfo->cquantize;
  50.352 +#else
  50.353 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  50.354 +#endif
  50.355 +    }
  50.356 +    /* If both quantizers are initialized, the 2-pass one is left active;
  50.357 +     * this is necessary for starting with quantization to an external map.
  50.358 +     */
  50.359 +  }
  50.360 +
  50.361 +  /* Post-processing: in particular, color conversion first */
  50.362 +  if (! cinfo->raw_data_out) {
  50.363 +    if (master->using_merged_upsample) {
  50.364 +#ifdef UPSAMPLE_MERGING_SUPPORTED
  50.365 +      jinit_merged_upsampler(cinfo); /* does color conversion too */
  50.366 +#else
  50.367 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  50.368 +#endif
  50.369 +    } else {
  50.370 +      jinit_color_deconverter(cinfo);
  50.371 +      jinit_upsampler(cinfo);
  50.372 +    }
  50.373 +    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  50.374 +  }
  50.375 +  /* Inverse DCT */
  50.376 +  jinit_inverse_dct(cinfo);
  50.377 +  /* Entropy decoding: either Huffman or arithmetic coding. */
  50.378 +  if (cinfo->arith_code) {
  50.379 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  50.380 +  } else {
  50.381 +    if (cinfo->progressive_mode) {
  50.382 +#ifdef D_PROGRESSIVE_SUPPORTED
  50.383 +      jinit_phuff_decoder(cinfo);
  50.384 +#else
  50.385 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  50.386 +#endif
  50.387 +    } else
  50.388 +      jinit_huff_decoder(cinfo);
  50.389 +  }
  50.390 +
  50.391 +  /* Initialize principal buffer controllers. */
  50.392 +  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  50.393 +  jinit_d_coef_controller(cinfo, use_c_buffer);
  50.394 +
  50.395 +  if (! cinfo->raw_data_out)
  50.396 +    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  50.397 +
  50.398 +  /* We can now tell the memory manager to allocate virtual arrays. */
  50.399 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  50.400 +
  50.401 +  /* Initialize input side of decompressor to consume first scan. */
  50.402 +  (*cinfo->inputctl->start_input_pass) (cinfo);
  50.403 +
  50.404 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  50.405 +  /* If jpeg_start_decompress will read the whole file, initialize
  50.406 +   * progress monitoring appropriately.  The input step is counted
  50.407 +   * as one pass.
  50.408 +   */
  50.409 +  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
  50.410 +      cinfo->inputctl->has_multiple_scans) {
  50.411 +    int nscans;
  50.412 +    /* Estimate number of scans to set pass_limit. */
  50.413 +    if (cinfo->progressive_mode) {
  50.414 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  50.415 +      nscans = 2 + 3 * cinfo->num_components;
  50.416 +    } else {
  50.417 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  50.418 +      nscans = cinfo->num_components;
  50.419 +    }
  50.420 +    cinfo->progress->pass_counter = 0L;
  50.421 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  50.422 +    cinfo->progress->completed_passes = 0;
  50.423 +    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  50.424 +    /* Count the input pass as done */
  50.425 +    master->pass_number++;
  50.426 +  }
  50.427 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  50.428 +}
  50.429 +
  50.430 +
  50.431 +/*
  50.432 + * Per-pass setup.
  50.433 + * This is called at the beginning of each output pass.  We determine which
  50.434 + * modules will be active during this pass and give them appropriate
  50.435 + * start_pass calls.  We also set is_dummy_pass to indicate whether this
  50.436 + * is a "real" output pass or a dummy pass for color quantization.
  50.437 + * (In the latter case, jdapistd.c will crank the pass to completion.)
  50.438 + */
  50.439 +
  50.440 +METHODDEF(void)
  50.441 +prepare_for_output_pass (j_decompress_ptr cinfo)
  50.442 +{
  50.443 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  50.444 +
  50.445 +  if (master->pub.is_dummy_pass) {
  50.446 +#ifdef QUANT_2PASS_SUPPORTED
  50.447 +    /* Final pass of 2-pass quantization */
  50.448 +    master->pub.is_dummy_pass = FALSE;
  50.449 +    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  50.450 +    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  50.451 +    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  50.452 +#else
  50.453 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  50.454 +#endif /* QUANT_2PASS_SUPPORTED */
  50.455 +  } else {
  50.456 +    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  50.457 +      /* Select new quantization method */
  50.458 +      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  50.459 +	cinfo->cquantize = master->quantizer_2pass;
  50.460 +	master->pub.is_dummy_pass = TRUE;
  50.461 +      } else if (cinfo->enable_1pass_quant) {
  50.462 +	cinfo->cquantize = master->quantizer_1pass;
  50.463 +      } else {
  50.464 +	ERREXIT(cinfo, JERR_MODE_CHANGE);
  50.465 +      }
  50.466 +    }
  50.467 +    (*cinfo->idct->start_pass) (cinfo);
  50.468 +    (*cinfo->coef->start_output_pass) (cinfo);
  50.469 +    if (! cinfo->raw_data_out) {
  50.470 +      if (! master->using_merged_upsample)
  50.471 +	(*cinfo->cconvert->start_pass) (cinfo);
  50.472 +      (*cinfo->upsample->start_pass) (cinfo);
  50.473 +      if (cinfo->quantize_colors)
  50.474 +	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  50.475 +      (*cinfo->post->start_pass) (cinfo,
  50.476 +	    (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  50.477 +      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  50.478 +    }
  50.479 +  }
  50.480 +
  50.481 +  /* Set up progress monitor's pass info if present */
  50.482 +  if (cinfo->progress != NULL) {
  50.483 +    cinfo->progress->completed_passes = master->pass_number;
  50.484 +    cinfo->progress->total_passes = master->pass_number +
  50.485 +				    (master->pub.is_dummy_pass ? 2 : 1);
  50.486 +    /* In buffered-image mode, we assume one more output pass if EOI not
  50.487 +     * yet reached, but no more passes if EOI has been reached.
  50.488 +     */
  50.489 +    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
  50.490 +      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  50.491 +    }
  50.492 +  }
  50.493 +}
  50.494 +
  50.495 +
  50.496 +/*
  50.497 + * Finish up at end of an output pass.
  50.498 + */
  50.499 +
  50.500 +METHODDEF(void)
  50.501 +finish_output_pass (j_decompress_ptr cinfo)
  50.502 +{
  50.503 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  50.504 +
  50.505 +  if (cinfo->quantize_colors)
  50.506 +    (*cinfo->cquantize->finish_pass) (cinfo);
  50.507 +  master->pass_number++;
  50.508 +}
  50.509 +
  50.510 +
  50.511 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  50.512 +
  50.513 +/*
  50.514 + * Switch to a new external colormap between output passes.
  50.515 + */
  50.516 +
  50.517 +GLOBAL(void)
  50.518 +jpeg_new_colormap (j_decompress_ptr cinfo)
  50.519 +{
  50.520 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  50.521 +
  50.522 +  /* Prevent application from calling me at wrong times */
  50.523 +  if (cinfo->global_state != DSTATE_BUFIMAGE)
  50.524 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  50.525 +
  50.526 +  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  50.527 +      cinfo->colormap != NULL) {
  50.528 +    /* Select 2-pass quantizer for external colormap use */
  50.529 +    cinfo->cquantize = master->quantizer_2pass;
  50.530 +    /* Notify quantizer of colormap change */
  50.531 +    (*cinfo->cquantize->new_color_map) (cinfo);
  50.532 +    master->pub.is_dummy_pass = FALSE; /* just in case */
  50.533 +  } else
  50.534 +    ERREXIT(cinfo, JERR_MODE_CHANGE);
  50.535 +}
  50.536 +
  50.537 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  50.538 +
  50.539 +
  50.540 +/*
  50.541 + * Initialize master decompression control and select active modules.
  50.542 + * This is performed at the start of jpeg_start_decompress.
  50.543 + */
  50.544 +
  50.545 +GLOBAL(void)
  50.546 +jinit_master_decompress (j_decompress_ptr cinfo)
  50.547 +{
  50.548 +  my_master_ptr master;
  50.549 +
  50.550 +  master = (my_master_ptr)
  50.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  50.552 +				  SIZEOF(my_decomp_master));
  50.553 +  cinfo->master = (struct jpeg_decomp_master *) master;
  50.554 +  master->pub.prepare_for_output_pass = prepare_for_output_pass;
  50.555 +  master->pub.finish_output_pass = finish_output_pass;
  50.556 +
  50.557 +  master->pub.is_dummy_pass = FALSE;
  50.558 +
  50.559 +  master_selection(cinfo);
  50.560 +}
    51.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    51.2 +++ b/libs/libjpeg/jdmerge.c	Sun Jun 07 17:25:49 2015 +0300
    51.3 @@ -0,0 +1,400 @@
    51.4 +/*
    51.5 + * jdmerge.c
    51.6 + *
    51.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    51.8 + * This file is part of the Independent JPEG Group's software.
    51.9 + * For conditions of distribution and use, see the accompanying README file.
   51.10 + *
   51.11 + * This file contains code for merged upsampling/color conversion.
   51.12 + *
   51.13 + * This file combines functions from jdsample.c and jdcolor.c;
   51.14 + * read those files first to understand what's going on.
   51.15 + *
   51.16 + * When the chroma components are to be upsampled by simple replication
   51.17 + * (ie, box filtering), we can save some work in color conversion by
   51.18 + * calculating all the output pixels corresponding to a pair of chroma
   51.19 + * samples at one time.  In the conversion equations
   51.20 + *	R = Y           + K1 * Cr
   51.21 + *	G = Y + K2 * Cb + K3 * Cr
   51.22 + *	B = Y + K4 * Cb
   51.23 + * only the Y term varies among the group of pixels corresponding to a pair
   51.24 + * of chroma samples, so the rest of the terms can be calculated just once.
   51.25 + * At typical sampling ratios, this eliminates half or three-quarters of the
   51.26 + * multiplications needed for color conversion.
   51.27 + *
   51.28 + * This file currently provides implementations for the following cases:
   51.29 + *	YCbCr => RGB color conversion only.
   51.30 + *	Sampling ratios of 2h1v or 2h2v.
   51.31 + *	No scaling needed at upsample time.
   51.32 + *	Corner-aligned (non-CCIR601) sampling alignment.
   51.33 + * Other special cases could be added, but in most applications these are
   51.34 + * the only common cases.  (For uncommon cases we fall back on the more
   51.35 + * general code in jdsample.c and jdcolor.c.)
   51.36 + */
   51.37 +
   51.38 +#define JPEG_INTERNALS
   51.39 +#include "jinclude.h"
   51.40 +#include "jpeglib.h"
   51.41 +
   51.42 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   51.43 +
   51.44 +
   51.45 +/* Private subobject */
   51.46 +
   51.47 +typedef struct {
   51.48 +  struct jpeg_upsampler pub;	/* public fields */
   51.49 +
   51.50 +  /* Pointer to routine to do actual upsampling/conversion of one row group */
   51.51 +  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
   51.52 +			   JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
   51.53 +			   JSAMPARRAY output_buf));
   51.54 +
   51.55 +  /* Private state for YCC->RGB conversion */
   51.56 +  int * Cr_r_tab;		/* => table for Cr to R conversion */
   51.57 +  int * Cb_b_tab;		/* => table for Cb to B conversion */
   51.58 +  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
   51.59 +  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
   51.60 +
   51.61 +  /* For 2:1 vertical sampling, we produce two output rows at a time.
   51.62 +   * We need a "spare" row buffer to hold the second output row if the
   51.63 +   * application provides just a one-row buffer; we also use the spare
   51.64 +   * to discard the dummy last row if the image height is odd.
   51.65 +   */
   51.66 +  JSAMPROW spare_row;
   51.67 +  boolean spare_full;		/* T if spare buffer is occupied */
   51.68 +
   51.69 +  JDIMENSION out_row_width;	/* samples per output row */
   51.70 +  JDIMENSION rows_to_go;	/* counts rows remaining in image */
   51.71 +} my_upsampler;
   51.72 +
   51.73 +typedef my_upsampler * my_upsample_ptr;
   51.74 +
   51.75 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   51.76 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   51.77 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   51.78 +
   51.79 +
   51.80 +/*
   51.81 + * Initialize tables for YCC->RGB colorspace conversion.
   51.82 + * This is taken directly from jdcolor.c; see that file for more info.
   51.83 + */
   51.84 +
   51.85 +LOCAL(void)
   51.86 +build_ycc_rgb_table (j_decompress_ptr cinfo)
   51.87 +{
   51.88 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   51.89 +  int i;
   51.90 +  INT32 x;
   51.91 +  SHIFT_TEMPS
   51.92 +
   51.93 +  upsample->Cr_r_tab = (int *)
   51.94 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   51.95 +				(MAXJSAMPLE+1) * SIZEOF(int));
   51.96 +  upsample->Cb_b_tab = (int *)
   51.97 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   51.98 +				(MAXJSAMPLE+1) * SIZEOF(int));
   51.99 +  upsample->Cr_g_tab = (INT32 *)
  51.100 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.101 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
  51.102 +  upsample->Cb_g_tab = (INT32 *)
  51.103 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.104 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
  51.105 +
  51.106 +  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  51.107 +    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  51.108 +    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  51.109 +    /* Cr=>R value is nearest int to 1.40200 * x */
  51.110 +    upsample->Cr_r_tab[i] = (int)
  51.111 +		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
  51.112 +    /* Cb=>B value is nearest int to 1.77200 * x */
  51.113 +    upsample->Cb_b_tab[i] = (int)
  51.114 +		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  51.115 +    /* Cr=>G value is scaled-up -0.71414 * x */
  51.116 +    upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  51.117 +    /* Cb=>G value is scaled-up -0.34414 * x */
  51.118 +    /* We also add in ONE_HALF so that need not do it in inner loop */
  51.119 +    upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  51.120 +  }
  51.121 +}
  51.122 +
  51.123 +
  51.124 +/*
  51.125 + * Initialize for an upsampling pass.
  51.126 + */
  51.127 +
  51.128 +METHODDEF(void)
  51.129 +start_pass_merged_upsample (j_decompress_ptr cinfo)
  51.130 +{
  51.131 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.132 +
  51.133 +  /* Mark the spare buffer empty */
  51.134 +  upsample->spare_full = FALSE;
  51.135 +  /* Initialize total-height counter for detecting bottom of image */
  51.136 +  upsample->rows_to_go = cinfo->output_height;
  51.137 +}
  51.138 +
  51.139 +
  51.140 +/*
  51.141 + * Control routine to do upsampling (and color conversion).
  51.142 + *
  51.143 + * The control routine just handles the row buffering considerations.
  51.144 + */
  51.145 +
  51.146 +METHODDEF(void)
  51.147 +merged_2v_upsample (j_decompress_ptr cinfo,
  51.148 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  51.149 +		    JDIMENSION in_row_groups_avail,
  51.150 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  51.151 +		    JDIMENSION out_rows_avail)
  51.152 +/* 2:1 vertical sampling case: may need a spare row. */
  51.153 +{
  51.154 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.155 +  JSAMPROW work_ptrs[2];
  51.156 +  JDIMENSION num_rows;		/* number of rows returned to caller */
  51.157 +
  51.158 +  if (upsample->spare_full) {
  51.159 +    /* If we have a spare row saved from a previous cycle, just return it. */
  51.160 +    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
  51.161 +		      1, upsample->out_row_width);
  51.162 +    num_rows = 1;
  51.163 +    upsample->spare_full = FALSE;
  51.164 +  } else {
  51.165 +    /* Figure number of rows to return to caller. */
  51.166 +    num_rows = 2;
  51.167 +    /* Not more than the distance to the end of the image. */
  51.168 +    if (num_rows > upsample->rows_to_go)
  51.169 +      num_rows = upsample->rows_to_go;
  51.170 +    /* And not more than what the client can accept: */
  51.171 +    out_rows_avail -= *out_row_ctr;
  51.172 +    if (num_rows > out_rows_avail)
  51.173 +      num_rows = out_rows_avail;
  51.174 +    /* Create output pointer array for upsampler. */
  51.175 +    work_ptrs[0] = output_buf[*out_row_ctr];
  51.176 +    if (num_rows > 1) {
  51.177 +      work_ptrs[1] = output_buf[*out_row_ctr + 1];
  51.178 +    } else {
  51.179 +      work_ptrs[1] = upsample->spare_row;
  51.180 +      upsample->spare_full = TRUE;
  51.181 +    }
  51.182 +    /* Now do the upsampling. */
  51.183 +    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  51.184 +  }
  51.185 +
  51.186 +  /* Adjust counts */
  51.187 +  *out_row_ctr += num_rows;
  51.188 +  upsample->rows_to_go -= num_rows;
  51.189 +  /* When the buffer is emptied, declare this input row group consumed */
  51.190 +  if (! upsample->spare_full)
  51.191 +    (*in_row_group_ctr)++;
  51.192 +}
  51.193 +
  51.194 +
  51.195 +METHODDEF(void)
  51.196 +merged_1v_upsample (j_decompress_ptr cinfo,
  51.197 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  51.198 +		    JDIMENSION in_row_groups_avail,
  51.199 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  51.200 +		    JDIMENSION out_rows_avail)
  51.201 +/* 1:1 vertical sampling case: much easier, never need a spare row. */
  51.202 +{
  51.203 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.204 +
  51.205 +  /* Just do the upsampling. */
  51.206 +  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  51.207 +			 output_buf + *out_row_ctr);
  51.208 +  /* Adjust counts */
  51.209 +  (*out_row_ctr)++;
  51.210 +  (*in_row_group_ctr)++;
  51.211 +}
  51.212 +
  51.213 +
  51.214 +/*
  51.215 + * These are the routines invoked by the control routines to do
  51.216 + * the actual upsampling/conversion.  One row group is processed per call.
  51.217 + *
  51.218 + * Note: since we may be writing directly into application-supplied buffers,
  51.219 + * we have to be honest about the output width; we can't assume the buffer
  51.220 + * has been rounded up to an even width.
  51.221 + */
  51.222 +
  51.223 +
  51.224 +/*
  51.225 + * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  51.226 + */
  51.227 +
  51.228 +METHODDEF(void)
  51.229 +h2v1_merged_upsample (j_decompress_ptr cinfo,
  51.230 +		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  51.231 +		      JSAMPARRAY output_buf)
  51.232 +{
  51.233 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.234 +  register int y, cred, cgreen, cblue;
  51.235 +  int cb, cr;
  51.236 +  register JSAMPROW outptr;
  51.237 +  JSAMPROW inptr0, inptr1, inptr2;
  51.238 +  JDIMENSION col;
  51.239 +  /* copy these pointers into registers if possible */
  51.240 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  51.241 +  int * Crrtab = upsample->Cr_r_tab;
  51.242 +  int * Cbbtab = upsample->Cb_b_tab;
  51.243 +  INT32 * Crgtab = upsample->Cr_g_tab;
  51.244 +  INT32 * Cbgtab = upsample->Cb_g_tab;
  51.245 +  SHIFT_TEMPS
  51.246 +
  51.247 +  inptr0 = input_buf[0][in_row_group_ctr];
  51.248 +  inptr1 = input_buf[1][in_row_group_ctr];
  51.249 +  inptr2 = input_buf[2][in_row_group_ctr];
  51.250 +  outptr = output_buf[0];
  51.251 +  /* Loop for each pair of output pixels */
  51.252 +  for (col = cinfo->output_width >> 1; col > 0; col--) {
  51.253 +    /* Do the chroma part of the calculation */
  51.254 +    cb = GETJSAMPLE(*inptr1++);
  51.255 +    cr = GETJSAMPLE(*inptr2++);
  51.256 +    cred = Crrtab[cr];
  51.257 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  51.258 +    cblue = Cbbtab[cb];
  51.259 +    /* Fetch 2 Y values and emit 2 pixels */
  51.260 +    y  = GETJSAMPLE(*inptr0++);
  51.261 +    outptr[RGB_RED] =   range_limit[y + cred];
  51.262 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  51.263 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  51.264 +    outptr += RGB_PIXELSIZE;
  51.265 +    y  = GETJSAMPLE(*inptr0++);
  51.266 +    outptr[RGB_RED] =   range_limit[y + cred];
  51.267 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  51.268 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  51.269 +    outptr += RGB_PIXELSIZE;
  51.270 +  }
  51.271 +  /* If image width is odd, do the last output column separately */
  51.272 +  if (cinfo->output_width & 1) {
  51.273 +    cb = GETJSAMPLE(*inptr1);
  51.274 +    cr = GETJSAMPLE(*inptr2);
  51.275 +    cred = Crrtab[cr];
  51.276 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  51.277 +    cblue = Cbbtab[cb];
  51.278 +    y  = GETJSAMPLE(*inptr0);
  51.279 +    outptr[RGB_RED] =   range_limit[y + cred];
  51.280 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  51.281 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  51.282 +  }
  51.283 +}
  51.284 +
  51.285 +
  51.286 +/*
  51.287 + * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  51.288 + */
  51.289 +
  51.290 +METHODDEF(void)
  51.291 +h2v2_merged_upsample (j_decompress_ptr cinfo,
  51.292 +		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  51.293 +		      JSAMPARRAY output_buf)
  51.294 +{
  51.295 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.296 +  register int y, cred, cgreen, cblue;
  51.297 +  int cb, cr;
  51.298 +  register JSAMPROW outptr0, outptr1;
  51.299 +  JSAMPROW inptr00, inptr01, inptr1, inptr2;
  51.300 +  JDIMENSION col;
  51.301 +  /* copy these pointers into registers if possible */
  51.302 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  51.303 +  int * Crrtab = upsample->Cr_r_tab;
  51.304 +  int * Cbbtab = upsample->Cb_b_tab;
  51.305 +  INT32 * Crgtab = upsample->Cr_g_tab;
  51.306 +  INT32 * Cbgtab = upsample->Cb_g_tab;
  51.307 +  SHIFT_TEMPS
  51.308 +
  51.309 +  inptr00 = input_buf[0][in_row_group_ctr*2];
  51.310 +  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
  51.311 +  inptr1 = input_buf[1][in_row_group_ctr];
  51.312 +  inptr2 = input_buf[2][in_row_group_ctr];
  51.313 +  outptr0 = output_buf[0];
  51.314 +  outptr1 = output_buf[1];
  51.315 +  /* Loop for each group of output pixels */
  51.316 +  for (col = cinfo->output_width >> 1; col > 0; col--) {
  51.317 +    /* Do the chroma part of the calculation */
  51.318 +    cb = GETJSAMPLE(*inptr1++);
  51.319 +    cr = GETJSAMPLE(*inptr2++);
  51.320 +    cred = Crrtab[cr];
  51.321 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  51.322 +    cblue = Cbbtab[cb];
  51.323 +    /* Fetch 4 Y values and emit 4 pixels */
  51.324 +    y  = GETJSAMPLE(*inptr00++);
  51.325 +    outptr0[RGB_RED] =   range_limit[y + cred];
  51.326 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  51.327 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  51.328 +    outptr0 += RGB_PIXELSIZE;
  51.329 +    y  = GETJSAMPLE(*inptr00++);
  51.330 +    outptr0[RGB_RED] =   range_limit[y + cred];
  51.331 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  51.332 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  51.333 +    outptr0 += RGB_PIXELSIZE;
  51.334 +    y  = GETJSAMPLE(*inptr01++);
  51.335 +    outptr1[RGB_RED] =   range_limit[y + cred];
  51.336 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  51.337 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  51.338 +    outptr1 += RGB_PIXELSIZE;
  51.339 +    y  = GETJSAMPLE(*inptr01++);
  51.340 +    outptr1[RGB_RED] =   range_limit[y + cred];
  51.341 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  51.342 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  51.343 +    outptr1 += RGB_PIXELSIZE;
  51.344 +  }
  51.345 +  /* If image width is odd, do the last output column separately */
  51.346 +  if (cinfo->output_width & 1) {
  51.347 +    cb = GETJSAMPLE(*inptr1);
  51.348 +    cr = GETJSAMPLE(*inptr2);
  51.349 +    cred = Crrtab[cr];
  51.350 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  51.351 +    cblue = Cbbtab[cb];
  51.352 +    y  = GETJSAMPLE(*inptr00);
  51.353 +    outptr0[RGB_RED] =   range_limit[y + cred];
  51.354 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  51.355 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  51.356 +    y  = GETJSAMPLE(*inptr01);
  51.357 +    outptr1[RGB_RED] =   range_limit[y + cred];
  51.358 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  51.359 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  51.360 +  }
  51.361 +}
  51.362 +
  51.363 +
  51.364 +/*
  51.365 + * Module initialization routine for merged upsampling/color conversion.
  51.366 + *
  51.367 + * NB: this is called under the conditions determined by use_merged_upsample()
  51.368 + * in jdmaster.c.  That routine MUST correspond to the actual capabilities
  51.369 + * of this module; no safety checks are made here.
  51.370 + */
  51.371 +
  51.372 +GLOBAL(void)
  51.373 +jinit_merged_upsampler (j_decompress_ptr cinfo)
  51.374 +{
  51.375 +  my_upsample_ptr upsample;
  51.376 +
  51.377 +  upsample = (my_upsample_ptr)
  51.378 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.379 +				SIZEOF(my_upsampler));
  51.380 +  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  51.381 +  upsample->pub.start_pass = start_pass_merged_upsample;
  51.382 +  upsample->pub.need_context_rows = FALSE;
  51.383 +
  51.384 +  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  51.385 +
  51.386 +  if (cinfo->max_v_samp_factor == 2) {
  51.387 +    upsample->pub.upsample = merged_2v_upsample;
  51.388 +    upsample->upmethod = h2v2_merged_upsample;
  51.389 +    /* Allocate a spare row buffer */
  51.390 +    upsample->spare_row = (JSAMPROW)
  51.391 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.392 +		(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
  51.393 +  } else {
  51.394 +    upsample->pub.upsample = merged_1v_upsample;
  51.395 +    upsample->upmethod = h2v1_merged_upsample;
  51.396 +    /* No spare row needed */
  51.397 +    upsample->spare_row = NULL;
  51.398 +  }
  51.399 +
  51.400 +  build_ycc_rgb_table(cinfo);
  51.401 +}
  51.402 +
  51.403 +#endif /* UPSAMPLE_MERGING_SUPPORTED */
    52.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    52.2 +++ b/libs/libjpeg/jdphuff.c	Sun Jun 07 17:25:49 2015 +0300
    52.3 @@ -0,0 +1,668 @@
    52.4 +/*
    52.5 + * jdphuff.c
    52.6 + *
    52.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    52.8 + * This file is part of the Independent JPEG Group's software.
    52.9 + * For conditions of distribution and use, see the accompanying README file.
   52.10 + *
   52.11 + * This file contains Huffman entropy decoding routines for progressive JPEG.
   52.12 + *
   52.13 + * Much of the complexity here has to do with supporting input suspension.
   52.14 + * If the data source module demands suspension, we want to be able to back
   52.15 + * up to the start of the current MCU.  To do this, we copy state variables
   52.16 + * into local working storage, and update them back to the permanent
   52.17 + * storage only upon successful completion of an MCU.
   52.18 + */
   52.19 +
   52.20 +#define JPEG_INTERNALS
   52.21 +#include "jinclude.h"
   52.22 +#include "jpeglib.h"
   52.23 +#include "jdhuff.h"		/* Declarations shared with jdhuff.c */
   52.24 +
   52.25 +
   52.26 +#ifdef D_PROGRESSIVE_SUPPORTED
   52.27 +
   52.28 +/*
   52.29 + * Expanded entropy decoder object for progressive Huffman decoding.
   52.30 + *
   52.31 + * The savable_state subrecord contains fields that change within an MCU,
   52.32 + * but must not be updated permanently until we complete the MCU.
   52.33 + */
   52.34 +
   52.35 +typedef struct {
   52.36 +  unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
   52.37 +  int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
   52.38 +} savable_state;
   52.39 +
   52.40 +/* This macro is to work around compilers with missing or broken
   52.41 + * structure assignment.  You'll need to fix this code if you have
   52.42 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   52.43 + */
   52.44 +
   52.45 +#ifndef NO_STRUCT_ASSIGN
   52.46 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   52.47 +#else
   52.48 +#if MAX_COMPS_IN_SCAN == 4
   52.49 +#define ASSIGN_STATE(dest,src)  \
   52.50 +	((dest).EOBRUN = (src).EOBRUN, \
   52.51 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
   52.52 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   52.53 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   52.54 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   52.55 +#endif
   52.56 +#endif
   52.57 +
   52.58 +
   52.59 +typedef struct {
   52.60 +  struct jpeg_entropy_decoder pub; /* public fields */
   52.61 +
   52.62 +  /* These fields are loaded into local variables at start of each MCU.
   52.63 +   * In case of suspension, we exit WITHOUT updating them.
   52.64 +   */
   52.65 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
   52.66 +  savable_state saved;		/* Other state at start of MCU */
   52.67 +
   52.68 +  /* These fields are NOT loaded into local working state. */
   52.69 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   52.70 +
   52.71 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   52.72 +  d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
   52.73 +
   52.74 +  d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
   52.75 +} phuff_entropy_decoder;
   52.76 +
   52.77 +typedef phuff_entropy_decoder * phuff_entropy_ptr;
   52.78 +
   52.79 +/* Forward declarations */
   52.80 +METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
   52.81 +					    JBLOCKROW *MCU_data));
   52.82 +METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
   52.83 +					    JBLOCKROW *MCU_data));
   52.84 +METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
   52.85 +					     JBLOCKROW *MCU_data));
   52.86 +METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
   52.87 +					     JBLOCKROW *MCU_data));
   52.88 +
   52.89 +
   52.90 +/*
   52.91 + * Initialize for a Huffman-compressed scan.
   52.92 + */
   52.93 +
   52.94 +METHODDEF(void)
   52.95 +start_pass_phuff_decoder (j_decompress_ptr cinfo)
   52.96 +{
   52.97 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   52.98 +  boolean is_DC_band, bad;
   52.99 +  int ci, coefi, tbl;
  52.100 +  int *coef_bit_ptr;
  52.101 +  jpeg_component_info * compptr;
  52.102 +
  52.103 +  is_DC_band = (cinfo->Ss == 0);
  52.104 +
  52.105 +  /* Validate scan parameters */
  52.106 +  bad = FALSE;
  52.107 +  if (is_DC_band) {
  52.108 +    if (cinfo->Se != 0)
  52.109 +      bad = TRUE;
  52.110 +  } else {
  52.111 +    /* need not check Ss/Se < 0 since they came from unsigned bytes */
  52.112 +    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
  52.113 +      bad = TRUE;
  52.114 +    /* AC scans may have only one component */
  52.115 +    if (cinfo->comps_in_scan != 1)
  52.116 +      bad = TRUE;
  52.117 +  }
  52.118 +  if (cinfo->Ah != 0) {
  52.119 +    /* Successive approximation refinement scan: must have Al = Ah-1. */
  52.120 +    if (cinfo->Al != cinfo->Ah-1)
  52.121 +      bad = TRUE;
  52.122 +  }
  52.123 +  if (cinfo->Al > 13)		/* need not check for < 0 */
  52.124 +    bad = TRUE;
  52.125 +  /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
  52.126 +   * but the spec doesn't say so, and we try to be liberal about what we
  52.127 +   * accept.  Note: large Al values could result in out-of-range DC
  52.128 +   * coefficients during early scans, leading to bizarre displays due to
  52.129 +   * overflows in the IDCT math.  But we won't crash.
  52.130 +   */
  52.131 +  if (bad)
  52.132 +    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  52.133 +	     cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  52.134 +  /* Update progression status, and verify that scan order is legal.
  52.135 +   * Note that inter-scan inconsistencies are treated as warnings
  52.136 +   * not fatal errors ... not clear if this is right way to behave.
  52.137 +   */
  52.138 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  52.139 +    int cindex = cinfo->cur_comp_info[ci]->component_index;
  52.140 +    coef_bit_ptr = & cinfo->coef_bits[cindex][0];
  52.141 +    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  52.142 +      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  52.143 +    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  52.144 +      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  52.145 +      if (cinfo->Ah != expected)
  52.146 +	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  52.147 +      coef_bit_ptr[coefi] = cinfo->Al;
  52.148 +    }
  52.149 +  }
  52.150 +
  52.151 +  /* Select MCU decoding routine */
  52.152 +  if (cinfo->Ah == 0) {
  52.153 +    if (is_DC_band)
  52.154 +      entropy->pub.decode_mcu = decode_mcu_DC_first;
  52.155 +    else
  52.156 +      entropy->pub.decode_mcu = decode_mcu_AC_first;
  52.157 +  } else {
  52.158 +    if (is_DC_band)
  52.159 +      entropy->pub.decode_mcu = decode_mcu_DC_refine;
  52.160 +    else
  52.161 +      entropy->pub.decode_mcu = decode_mcu_AC_refine;
  52.162 +  }
  52.163 +
  52.164 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  52.165 +    compptr = cinfo->cur_comp_info[ci];
  52.166 +    /* Make sure requested tables are present, and compute derived tables.
  52.167 +     * We may build same derived table more than once, but it's not expensive.
  52.168 +     */
  52.169 +    if (is_DC_band) {
  52.170 +      if (cinfo->Ah == 0) {	/* DC refinement needs no table */
  52.171 +	tbl = compptr->dc_tbl_no;
  52.172 +	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
  52.173 +				& entropy->derived_tbls[tbl]);
  52.174 +      }
  52.175 +    } else {
  52.176 +      tbl = compptr->ac_tbl_no;
  52.177 +      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
  52.178 +			      & entropy->derived_tbls[tbl]);
  52.179 +      /* remember the single active table */
  52.180 +      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
  52.181 +    }
  52.182 +    /* Initialize DC predictions to 0 */
  52.183 +    entropy->saved.last_dc_val[ci] = 0;
  52.184 +  }
  52.185 +
  52.186 +  /* Initialize bitread state variables */
  52.187 +  entropy->bitstate.bits_left = 0;
  52.188 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  52.189 +  entropy->pub.insufficient_data = FALSE;
  52.190 +
  52.191 +  /* Initialize private state variables */
  52.192 +  entropy->saved.EOBRUN = 0;
  52.193 +
  52.194 +  /* Initialize restart counter */
  52.195 +  entropy->restarts_to_go = cinfo->restart_interval;
  52.196 +}
  52.197 +
  52.198 +
  52.199 +/*
  52.200 + * Figure F.12: extend sign bit.
  52.201 + * On some machines, a shift and add will be faster than a table lookup.
  52.202 + */
  52.203 +
  52.204 +#ifdef AVOID_TABLES
  52.205 +
  52.206 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  52.207 +
  52.208 +#else
  52.209 +
  52.210 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  52.211 +
  52.212 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
  52.213 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  52.214 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  52.215 +
  52.216 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  52.217 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  52.218 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  52.219 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  52.220 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  52.221 +
  52.222 +#endif /* AVOID_TABLES */
  52.223 +
  52.224 +
  52.225 +/*
  52.226 + * Check for a restart marker & resynchronize decoder.
  52.227 + * Returns FALSE if must suspend.
  52.228 + */
  52.229 +
  52.230 +LOCAL(boolean)
  52.231 +process_restart (j_decompress_ptr cinfo)
  52.232 +{
  52.233 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  52.234 +  int ci;
  52.235 +
  52.236 +  /* Throw away any unused bits remaining in bit buffer; */
  52.237 +  /* include any full bytes in next_marker's count of discarded bytes */
  52.238 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  52.239 +  entropy->bitstate.bits_left = 0;
  52.240 +
  52.241 +  /* Advance past the RSTn marker */
  52.242 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
  52.243 +    return FALSE;
  52.244 +
  52.245 +  /* Re-initialize DC predictions to 0 */
  52.246 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  52.247 +    entropy->saved.last_dc_val[ci] = 0;
  52.248 +  /* Re-init EOB run count, too */
  52.249 +  entropy->saved.EOBRUN = 0;
  52.250 +
  52.251 +  /* Reset restart counter */
  52.252 +  entropy->restarts_to_go = cinfo->restart_interval;
  52.253 +
  52.254 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
  52.255 +   * against a marker.  In that case we will end up treating the next data
  52.256 +   * segment as empty, and we can avoid producing bogus output pixels by
  52.257 +   * leaving the flag set.
  52.258 +   */
  52.259 +  if (cinfo->unread_marker == 0)
  52.260 +    entropy->pub.insufficient_data = FALSE;
  52.261 +
  52.262 +  return TRUE;
  52.263 +}
  52.264 +
  52.265 +
  52.266 +/*
  52.267 + * Huffman MCU decoding.
  52.268 + * Each of these routines decodes and returns one MCU's worth of
  52.269 + * Huffman-compressed coefficients. 
  52.270 + * The coefficients are reordered from zigzag order into natural array order,
  52.271 + * but are not dequantized.
  52.272 + *
  52.273 + * The i'th block of the MCU is stored into the block pointed to by
  52.274 + * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  52.275 + *
  52.276 + * We return FALSE if data source requested suspension.  In that case no
  52.277 + * changes have been made to permanent state.  (Exception: some output
  52.278 + * coefficients may already have been assigned.  This is harmless for
  52.279 + * spectral selection, since we'll just re-assign them on the next call.
  52.280 + * Successive approximation AC refinement has to be more careful, however.)
  52.281 + */
  52.282 +
  52.283 +/*
  52.284 + * MCU decoding for DC initial scan (either spectral selection,
  52.285 + * or first pass of successive approximation).
  52.286 + */
  52.287 +
  52.288 +METHODDEF(boolean)
  52.289 +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  52.290 +{   
  52.291 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  52.292 +  int Al = cinfo->Al;
  52.293 +  register int s, r;
  52.294 +  int blkn, ci;
  52.295 +  JBLOCKROW block;
  52.296 +  BITREAD_STATE_VARS;
  52.297 +  savable_state state;
  52.298 +  d_derived_tbl * tbl;
  52.299 +  jpeg_component_info * compptr;
  52.300 +
  52.301 +  /* Process restart marker if needed; may have to suspend */
  52.302 +  if (cinfo->restart_interval) {
  52.303 +    if (entropy->restarts_to_go == 0)
  52.304 +      if (! process_restart(cinfo))
  52.305 +	return FALSE;
  52.306 +  }
  52.307 +
  52.308 +  /* If we've run out of data, just leave the MCU set to zeroes.
  52.309 +   * This way, we return uniform gray for the remainder of the segment.
  52.310 +   */
  52.311 +  if (! entropy->pub.insufficient_data) {
  52.312 +
  52.313 +    /* Load up working state */
  52.314 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  52.315 +    ASSIGN_STATE(state, entropy->saved);
  52.316 +
  52.317 +    /* Outer loop handles each block in the MCU */
  52.318 +
  52.319 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  52.320 +      block = MCU_data[blkn];
  52.321 +      ci = cinfo->MCU_membership[blkn];
  52.322 +      compptr = cinfo->cur_comp_info[ci];
  52.323 +      tbl = entropy->derived_tbls[compptr->dc_tbl_no];
  52.324 +
  52.325 +      /* Decode a single block's worth of coefficients */
  52.326 +
  52.327 +      /* Section F.2.2.1: decode the DC coefficient difference */
  52.328 +      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
  52.329 +      if (s) {
  52.330 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
  52.331 +	r = GET_BITS(s);
  52.332 +	s = HUFF_EXTEND(r, s);
  52.333 +      }
  52.334 +
  52.335 +      /* Convert DC difference to actual value, update last_dc_val */
  52.336 +      s += state.last_dc_val[ci];
  52.337 +      state.last_dc_val[ci] = s;
  52.338 +      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
  52.339 +      (*block)[0] = (JCOEF) (s << Al);
  52.340 +    }
  52.341 +
  52.342 +    /* Completed MCU, so update state */
  52.343 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  52.344 +    ASSIGN_STATE(entropy->saved, state);
  52.345 +  }
  52.346 +
  52.347 +  /* Account for restart interval (no-op if not using restarts) */
  52.348 +  entropy->restarts_to_go--;
  52.349 +
  52.350 +  return TRUE;
  52.351 +}
  52.352 +
  52.353 +
  52.354 +/*
  52.355 + * MCU decoding for AC initial scan (either spectral selection,
  52.356 + * or first pass of successive approximation).
  52.357 + */
  52.358 +
  52.359 +METHODDEF(boolean)
  52.360 +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  52.361 +{   
  52.362 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  52.363 +  int Se = cinfo->Se;
  52.364 +  int Al = cinfo->Al;
  52.365 +  register int s, k, r;
  52.366 +  unsigned int EOBRUN;
  52.367 +  JBLOCKROW block;
  52.368 +  BITREAD_STATE_VARS;
  52.369 +  d_derived_tbl * tbl;
  52.370 +
  52.371 +  /* Process restart marker if needed; may have to suspend */
  52.372 +  if (cinfo->restart_interval) {
  52.373 +    if (entropy->restarts_to_go == 0)
  52.374 +      if (! process_restart(cinfo))
  52.375 +	return FALSE;
  52.376 +  }
  52.377 +
  52.378 +  /* If we've run out of data, just leave the MCU set to zeroes.
  52.379 +   * This way, we return uniform gray for the remainder of the segment.
  52.380 +   */
  52.381 +  if (! entropy->pub.insufficient_data) {
  52.382 +
  52.383 +    /* Load up working state.
  52.384 +     * We can avoid loading/saving bitread state if in an EOB run.
  52.385 +     */
  52.386 +    EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */
  52.387 +
  52.388 +    /* There is always only one block per MCU */
  52.389 +
  52.390 +    if (EOBRUN > 0)		/* if it's a band of zeroes... */
  52.391 +      EOBRUN--;			/* ...process it now (we do nothing) */
  52.392 +    else {
  52.393 +      BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  52.394 +      block = MCU_data[0];
  52.395 +      tbl = entropy->ac_derived_tbl;
  52.396 +
  52.397 +      for (k = cinfo->Ss; k <= Se; k++) {
  52.398 +	HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
  52.399 +	r = s >> 4;
  52.400 +	s &= 15;
  52.401 +	if (s) {
  52.402 +	  k += r;
  52.403 +	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
  52.404 +	  r = GET_BITS(s);
  52.405 +	  s = HUFF_EXTEND(r, s);
  52.406 +	  /* Scale and output coefficient in natural (dezigzagged) order */
  52.407 +	  (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
  52.408 +	} else {
  52.409 +	  if (r == 15) {	/* ZRL */
  52.410 +	    k += 15;		/* skip 15 zeroes in band */
  52.411 +	  } else {		/* EOBr, run length is 2^r + appended bits */
  52.412 +	    EOBRUN = 1 << r;
  52.413 +	    if (r) {		/* EOBr, r > 0 */
  52.414 +	      CHECK_BIT_BUFFER(br_state, r, return FALSE);
  52.415 +	      r = GET_BITS(r);
  52.416 +	      EOBRUN += r;
  52.417 +	    }
  52.418 +	    EOBRUN--;		/* this band is processed at this moment */
  52.419 +	    break;		/* force end-of-band */
  52.420 +	  }
  52.421 +	}
  52.422 +      }
  52.423 +
  52.424 +      BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  52.425 +    }
  52.426 +
  52.427 +    /* Completed MCU, so update state */
  52.428 +    entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
  52.429 +  }
  52.430 +
  52.431 +  /* Account for restart interval (no-op if not using restarts) */
  52.432 +  entropy->restarts_to_go--;
  52.433 +
  52.434 +  return TRUE;
  52.435 +}
  52.436 +
  52.437 +
  52.438 +/*
  52.439 + * MCU decoding for DC successive approximation refinement scan.
  52.440 + * Note: we assume such scans can be multi-component, although the spec
  52.441 + * is not very clear on the point.
  52.442 + */
  52.443 +
  52.444 +METHODDEF(boolean)
  52.445 +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  52.446 +{   
  52.447 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  52.448 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  52.449 +  int blkn;
  52.450 +  JBLOCKROW block;
  52.451 +  BITREAD_STATE_VARS;
  52.452 +
  52.453 +  /* Process restart marker if needed; may have to suspend */
  52.454 +  if (cinfo->restart_interval) {
  52.455 +    if (entropy->restarts_to_go == 0)
  52.456 +      if (! process_restart(cinfo))
  52.457 +	return FALSE;
  52.458 +  }
  52.459 +
  52.460 +  /* Not worth the cycles to check insufficient_data here,
  52.461 +   * since we will not change the data anyway if we read zeroes.
  52.462 +   */
  52.463 +
  52.464 +  /* Load up working state */
  52.465 +  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  52.466 +
  52.467 +  /* Outer loop handles each block in the MCU */
  52.468 +
  52.469 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  52.470 +    block = MCU_data[blkn];
  52.471 +
  52.472 +    /* Encoded data is simply the next bit of the two's-complement DC value */
  52.473 +    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
  52.474 +    if (GET_BITS(1))
  52.475 +      (*block)[0] |= p1;
  52.476 +    /* Note: since we use |=, repeating the assignment later is safe */
  52.477 +  }
  52.478 +
  52.479 +  /* Completed MCU, so update state */
  52.480 +  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  52.481 +
  52.482 +  /* Account for restart interval (no-op if not using restarts) */
  52.483 +  entropy->restarts_to_go--;
  52.484 +
  52.485 +  return TRUE;
  52.486 +}
  52.487 +
  52.488 +
  52.489 +/*
  52.490 + * MCU decoding for AC successive approximation refinement scan.
  52.491 + */
  52.492 +
  52.493 +METHODDEF(boolean)
  52.494 +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  52.495 +{   
  52.496 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  52.497 +  int Se = cinfo->Se;
  52.498 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  52.499 +  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
  52.500 +  register int s, k, r;
  52.501 +  unsigned int EOBRUN;
  52.502 +  JBLOCKROW block;
  52.503 +  JCOEFPTR thiscoef;
  52.504 +  BITREAD_STATE_VARS;
  52.505 +  d_derived_tbl * tbl;
  52.506 +  int num_newnz;
  52.507 +  int newnz_pos[DCTSIZE2];
  52.508 +
  52.509 +  /* Process restart marker if needed; may have to suspend */
  52.510 +  if (cinfo->restart_interval) {
  52.511 +    if (entropy->restarts_to_go == 0)
  52.512 +      if (! process_restart(cinfo))
  52.513 +	return FALSE;
  52.514 +  }
  52.515 +
  52.516 +  /* If we've run out of data, don't modify the MCU.
  52.517 +   */
  52.518 +  if (! entropy->pub.insufficient_data) {
  52.519 +
  52.520 +    /* Load up working state */
  52.521 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  52.522 +    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  52.523 +
  52.524 +    /* There is always only one block per MCU */
  52.525 +    block = MCU_data[0];
  52.526 +    tbl = entropy->ac_derived_tbl;
  52.527 +
  52.528 +    /* If we are forced to suspend, we must undo the assignments to any newly
  52.529 +     * nonzero coefficients in the block, because otherwise we'd get confused
  52.530 +     * next time about which coefficients were already nonzero.
  52.531 +     * But we need not undo addition of bits to already-nonzero coefficients;
  52.532 +     * instead, we can test the current bit to see if we already did it.
  52.533 +     */
  52.534 +    num_newnz = 0;
  52.535 +
  52.536 +    /* initialize coefficient loop counter to start of band */
  52.537 +    k = cinfo->Ss;
  52.538 +
  52.539 +    if (EOBRUN == 0) {
  52.540 +      for (; k <= Se; k++) {
  52.541 +	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
  52.542 +	r = s >> 4;
  52.543 +	s &= 15;
  52.544 +	if (s) {
  52.545 +	  if (s != 1)		/* size of new coef should always be 1 */
  52.546 +	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
  52.547 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  52.548 +	  if (GET_BITS(1))
  52.549 +	    s = p1;		/* newly nonzero coef is positive */
  52.550 +	  else
  52.551 +	    s = m1;		/* newly nonzero coef is negative */
  52.552 +	} else {
  52.553 +	  if (r != 15) {
  52.554 +	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
  52.555 +	    if (r) {
  52.556 +	      CHECK_BIT_BUFFER(br_state, r, goto undoit);
  52.557 +	      r = GET_BITS(r);
  52.558 +	      EOBRUN += r;
  52.559 +	    }
  52.560 +	    break;		/* rest of block is handled by EOB logic */
  52.561 +	  }
  52.562 +	  /* note s = 0 for processing ZRL */
  52.563 +	}
  52.564 +	/* Advance over already-nonzero coefs and r still-zero coefs,
  52.565 +	 * appending correction bits to the nonzeroes.  A correction bit is 1
  52.566 +	 * if the absolute value of the coefficient must be increased.
  52.567 +	 */
  52.568 +	do {
  52.569 +	  thiscoef = *block + jpeg_natural_order[k];
  52.570 +	  if (*thiscoef != 0) {
  52.571 +	    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  52.572 +	    if (GET_BITS(1)) {
  52.573 +	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
  52.574 +		if (*thiscoef >= 0)
  52.575 +		  *thiscoef += p1;
  52.576 +		else
  52.577 +		  *thiscoef += m1;
  52.578 +	      }
  52.579 +	    }
  52.580 +	  } else {
  52.581 +	    if (--r < 0)
  52.582 +	      break;		/* reached target zero coefficient */
  52.583 +	  }
  52.584 +	  k++;
  52.585 +	} while (k <= Se);
  52.586 +	if (s) {
  52.587 +	  int pos = jpeg_natural_order[k];
  52.588 +	  /* Output newly nonzero coefficient */
  52.589 +	  (*block)[pos] = (JCOEF) s;
  52.590 +	  /* Remember its position in case we have to suspend */
  52.591 +	  newnz_pos[num_newnz++] = pos;
  52.592 +	}
  52.593 +      }
  52.594 +    }
  52.595 +
  52.596 +    if (EOBRUN > 0) {
  52.597 +      /* Scan any remaining coefficient positions after the end-of-band
  52.598 +       * (the last newly nonzero coefficient, if any).  Append a correction
  52.599 +       * bit to each already-nonzero coefficient.  A correction bit is 1
  52.600 +       * if the absolute value of the coefficient must be increased.
  52.601 +       */
  52.602 +      for (; k <= Se; k++) {
  52.603 +	thiscoef = *block + jpeg_natural_order[k];
  52.604 +	if (*thiscoef != 0) {
  52.605 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  52.606 +	  if (GET_BITS(1)) {
  52.607 +	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
  52.608 +	      if (*thiscoef >= 0)
  52.609 +		*thiscoef += p1;
  52.610 +	      else
  52.611 +		*thiscoef += m1;
  52.612 +	    }
  52.613 +	  }
  52.614 +	}
  52.615 +      }
  52.616 +      /* Count one block completed in EOB run */
  52.617 +      EOBRUN--;
  52.618 +    }
  52.619 +
  52.620 +    /* Completed MCU, so update state */
  52.621 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  52.622 +    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  52.623 +  }
  52.624 +
  52.625 +  /* Account for restart interval (no-op if not using restarts) */
  52.626 +  entropy->restarts_to_go--;
  52.627 +
  52.628 +  return TRUE;
  52.629 +
  52.630 +undoit:
  52.631 +  /* Re-zero any output coefficients that we made newly nonzero */
  52.632 +  while (num_newnz > 0)
  52.633 +    (*block)[newnz_pos[--num_newnz]] = 0;
  52.634 +
  52.635 +  return FALSE;
  52.636 +}
  52.637 +
  52.638 +
  52.639 +/*
  52.640 + * Module initialization routine for progressive Huffman entropy decoding.
  52.641 + */
  52.642 +
  52.643 +GLOBAL(void)
  52.644 +jinit_phuff_decoder (j_decompress_ptr cinfo)
  52.645 +{
  52.646 +  phuff_entropy_ptr entropy;
  52.647 +  int *coef_bit_ptr;
  52.648 +  int ci, i;
  52.649 +
  52.650 +  entropy = (phuff_entropy_ptr)
  52.651 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  52.652 +				SIZEOF(phuff_entropy_decoder));
  52.653 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  52.654 +  entropy->pub.start_pass = start_pass_phuff_decoder;
  52.655 +
  52.656 +  /* Mark derived tables unallocated */
  52.657 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  52.658 +    entropy->derived_tbls[i] = NULL;
  52.659 +  }
  52.660 +
  52.661 +  /* Create progression status table */
  52.662 +  cinfo->coef_bits = (int (*)[DCTSIZE2])
  52.663 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  52.664 +				cinfo->num_components*DCTSIZE2*SIZEOF(int));
  52.665 +  coef_bit_ptr = & cinfo->coef_bits[0][0];
  52.666 +  for (ci = 0; ci < cinfo->num_components; ci++) 
  52.667 +    for (i = 0; i < DCTSIZE2; i++)
  52.668 +      *coef_bit_ptr++ = -1;
  52.669 +}
  52.670 +
  52.671 +#endif /* D_PROGRESSIVE_SUPPORTED */
    53.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    53.2 +++ b/libs/libjpeg/jdpostct.c	Sun Jun 07 17:25:49 2015 +0300
    53.3 @@ -0,0 +1,290 @@
    53.4 +/*
    53.5 + * jdpostct.c
    53.6 + *
    53.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    53.8 + * This file is part of the Independent JPEG Group's software.
    53.9 + * For conditions of distribution and use, see the accompanying README file.
   53.10 + *
   53.11 + * This file contains the decompression postprocessing controller.
   53.12 + * This controller manages the upsampling, color conversion, and color
   53.13 + * quantization/reduction steps; specifically, it controls the buffering
   53.14 + * between upsample/color conversion and color quantization/reduction.
   53.15 + *
   53.16 + * If no color quantization/reduction is required, then this module has no
   53.17 + * work to do, and it just hands off to the upsample/color conversion code.
   53.18 + * An integrated upsample/convert/quantize process would replace this module
   53.19 + * entirely.
   53.20 + */
   53.21 +
   53.22 +#define JPEG_INTERNALS
   53.23 +#include "jinclude.h"
   53.24 +#include "jpeglib.h"
   53.25 +
   53.26 +
   53.27 +/* Private buffer controller object */
   53.28 +
   53.29 +typedef struct {
   53.30 +  struct jpeg_d_post_controller pub; /* public fields */
   53.31 +
   53.32 +  /* Color quantization source buffer: this holds output data from
   53.33 +   * the upsample/color conversion step to be passed to the quantizer.
   53.34 +   * For two-pass color quantization, we need a full-image buffer;
   53.35 +   * for one-pass operation, a strip buffer is sufficient.
   53.36 +   */
   53.37 +  jvirt_sarray_ptr whole_image;	/* virtual array, or NULL if one-pass */
   53.38 +  JSAMPARRAY buffer;		/* strip buffer, or current strip of virtual */
   53.39 +  JDIMENSION strip_height;	/* buffer size in rows */
   53.40 +  /* for two-pass mode only: */
   53.41 +  JDIMENSION starting_row;	/* row # of first row in current strip */
   53.42 +  JDIMENSION next_row;		/* index of next row to fill/empty in strip */
   53.43 +} my_post_controller;
   53.44 +
   53.45 +typedef my_post_controller * my_post_ptr;
   53.46 +
   53.47 +
   53.48 +/* Forward declarations */
   53.49 +METHODDEF(void) post_process_1pass
   53.50 +	JPP((j_decompress_ptr cinfo,
   53.51 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   53.52 +	     JDIMENSION in_row_groups_avail,
   53.53 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   53.54 +	     JDIMENSION out_rows_avail));
   53.55 +#ifdef QUANT_2PASS_SUPPORTED
   53.56 +METHODDEF(void) post_process_prepass
   53.57 +	JPP((j_decompress_ptr cinfo,
   53.58 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   53.59 +	     JDIMENSION in_row_groups_avail,
   53.60 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   53.61 +	     JDIMENSION out_rows_avail));
   53.62 +METHODDEF(void) post_process_2pass
   53.63 +	JPP((j_decompress_ptr cinfo,
   53.64 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   53.65 +	     JDIMENSION in_row_groups_avail,
   53.66 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   53.67 +	     JDIMENSION out_rows_avail));
   53.68 +#endif
   53.69 +
   53.70 +
   53.71 +/*
   53.72 + * Initialize for a processing pass.
   53.73 + */
   53.74 +
   53.75 +METHODDEF(void)
   53.76 +start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
   53.77 +{
   53.78 +  my_post_ptr post = (my_post_ptr) cinfo->post;
   53.79 +
   53.80 +  switch (pass_mode) {
   53.81 +  case JBUF_PASS_THRU:
   53.82 +    if (cinfo->quantize_colors) {
   53.83 +      /* Single-pass processing with color quantization. */
   53.84 +      post->pub.post_process_data = post_process_1pass;
   53.85 +      /* We could be doing buffered-image output before starting a 2-pass
   53.86 +       * color quantization; in that case, jinit_d_post_controller did not
   53.87 +       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
   53.88 +       */
   53.89 +      if (post->buffer == NULL) {
   53.90 +	post->buffer = (*cinfo->mem->access_virt_sarray)
   53.91 +	  ((j_common_ptr) cinfo, post->whole_image,
   53.92 +	   (JDIMENSION) 0, post->strip_height, TRUE);
   53.93 +      }
   53.94 +    } else {
   53.95 +      /* For single-pass processing without color quantization,
   53.96 +       * I have no work to do; just call the upsampler directly.
   53.97 +       */
   53.98 +      post->pub.post_process_data = cinfo->upsample->upsample;
   53.99 +    }
  53.100 +    break;
  53.101 +#ifdef QUANT_2PASS_SUPPORTED
  53.102 +  case JBUF_SAVE_AND_PASS:
  53.103 +    /* First pass of 2-pass quantization */
  53.104 +    if (post->whole_image == NULL)
  53.105 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  53.106 +    post->pub.post_process_data = post_process_prepass;
  53.107 +    break;
  53.108 +  case JBUF_CRANK_DEST:
  53.109 +    /* Second pass of 2-pass quantization */
  53.110 +    if (post->whole_image == NULL)
  53.111 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  53.112 +    post->pub.post_process_data = post_process_2pass;
  53.113 +    break;
  53.114 +#endif /* QUANT_2PASS_SUPPORTED */
  53.115 +  default:
  53.116 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  53.117 +    break;
  53.118 +  }
  53.119 +  post->starting_row = post->next_row = 0;
  53.120 +}
  53.121 +
  53.122 +
  53.123 +/*
  53.124 + * Process some data in the one-pass (strip buffer) case.
  53.125 + * This is used for color precision reduction as well as one-pass quantization.
  53.126 + */
  53.127 +
  53.128 +METHODDEF(void)
  53.129 +post_process_1pass (j_decompress_ptr cinfo,
  53.130 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  53.131 +		    JDIMENSION in_row_groups_avail,
  53.132 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  53.133 +		    JDIMENSION out_rows_avail)
  53.134 +{
  53.135 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  53.136 +  JDIMENSION num_rows, max_rows;
  53.137 +
  53.138 +  /* Fill the buffer, but not more than what we can dump out in one go. */
  53.139 +  /* Note we rely on the upsampler to detect bottom of image. */
  53.140 +  max_rows = out_rows_avail - *out_row_ctr;
  53.141 +  if (max_rows > post->strip_height)
  53.142 +    max_rows = post->strip_height;
  53.143 +  num_rows = 0;
  53.144 +  (*cinfo->upsample->upsample) (cinfo,
  53.145 +		input_buf, in_row_group_ctr, in_row_groups_avail,
  53.146 +		post->buffer, &num_rows, max_rows);
  53.147 +  /* Quantize and emit data. */
  53.148 +  (*cinfo->cquantize->color_quantize) (cinfo,
  53.149 +		post->buffer, output_buf + *out_row_ctr, (int) num_rows);
  53.150 +  *out_row_ctr += num_rows;
  53.151 +}
  53.152 +
  53.153 +
  53.154 +#ifdef QUANT_2PASS_SUPPORTED
  53.155 +
  53.156 +/*
  53.157 + * Process some data in the first pass of 2-pass quantization.
  53.158 + */
  53.159 +
  53.160 +METHODDEF(void)
  53.161 +post_process_prepass (j_decompress_ptr cinfo,
  53.162 +		      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  53.163 +		      JDIMENSION in_row_groups_avail,
  53.164 +		      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  53.165 +		      JDIMENSION out_rows_avail)
  53.166 +{
  53.167 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  53.168 +  JDIMENSION old_next_row, num_rows;
  53.169 +
  53.170 +  /* Reposition virtual buffer if at start of strip. */
  53.171 +  if (post->next_row == 0) {
  53.172 +    post->buffer = (*cinfo->mem->access_virt_sarray)
  53.173 +	((j_common_ptr) cinfo, post->whole_image,
  53.174 +	 post->starting_row, post->strip_height, TRUE);
  53.175 +  }
  53.176 +
  53.177 +  /* Upsample some data (up to a strip height's worth). */
  53.178 +  old_next_row = post->next_row;
  53.179 +  (*cinfo->upsample->upsample) (cinfo,
  53.180 +		input_buf, in_row_group_ctr, in_row_groups_avail,
  53.181 +		post->buffer, &post->next_row, post->strip_height);
  53.182 +
  53.183 +  /* Allow quantizer to scan new data.  No data is emitted, */
  53.184 +  /* but we advance out_row_ctr so outer loop can tell when we're done. */
  53.185 +  if (post->next_row > old_next_row) {
  53.186 +    num_rows = post->next_row - old_next_row;
  53.187 +    (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
  53.188 +					 (JSAMPARRAY) NULL, (int) num_rows);
  53.189 +    *out_row_ctr += num_rows;
  53.190 +  }
  53.191 +
  53.192 +  /* Advance if we filled the strip. */
  53.193 +  if (post->next_row >= post->strip_height) {
  53.194 +    post->starting_row += post->strip_height;
  53.195 +    post->next_row = 0;
  53.196 +  }
  53.197 +}
  53.198 +
  53.199 +
  53.200 +/*
  53.201 + * Process some data in the second pass of 2-pass quantization.
  53.202 + */
  53.203 +
  53.204 +METHODDEF(void)
  53.205 +post_process_2pass (j_decompress_ptr cinfo,
  53.206 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  53.207 +		    JDIMENSION in_row_groups_avail,
  53.208 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  53.209 +		    JDIMENSION out_rows_avail)
  53.210 +{
  53.211 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  53.212 +  JDIMENSION num_rows, max_rows;
  53.213 +
  53.214 +  /* Reposition virtual buffer if at start of strip. */
  53.215 +  if (post->next_row == 0) {
  53.216 +    post->buffer = (*cinfo->mem->access_virt_sarray)
  53.217 +	((j_common_ptr) cinfo, post->whole_image,
  53.218 +	 post->starting_row, post->strip_height, FALSE);
  53.219 +  }
  53.220 +
  53.221 +  /* Determine number of rows to emit. */
  53.222 +  num_rows = post->strip_height - post->next_row; /* available in strip */
  53.223 +  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
  53.224 +  if (num_rows > max_rows)
  53.225 +    num_rows = max_rows;
  53.226 +  /* We have to check bottom of image here, can't depend on upsampler. */
  53.227 +  max_rows = cinfo->output_height - post->starting_row;
  53.228 +  if (num_rows > max_rows)
  53.229 +    num_rows = max_rows;
  53.230 +
  53.231 +  /* Quantize and emit data. */
  53.232 +  (*cinfo->cquantize->color_quantize) (cinfo,
  53.233 +		post->buffer + post->next_row, output_buf + *out_row_ctr,
  53.234 +		(int) num_rows);
  53.235 +  *out_row_ctr += num_rows;
  53.236 +
  53.237 +  /* Advance if we filled the strip. */
  53.238 +  post->next_row += num_rows;
  53.239 +  if (post->next_row >= post->strip_height) {
  53.240 +    post->starting_row += post->strip_height;
  53.241 +    post->next_row = 0;
  53.242 +  }
  53.243 +}
  53.244 +
  53.245 +#endif /* QUANT_2PASS_SUPPORTED */
  53.246 +
  53.247 +
  53.248 +/*
  53.249 + * Initialize postprocessing controller.
  53.250 + */
  53.251 +
  53.252 +GLOBAL(void)
  53.253 +jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  53.254 +{
  53.255 +  my_post_ptr post;
  53.256 +
  53.257 +  post = (my_post_ptr)
  53.258 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  53.259 +				SIZEOF(my_post_controller));
  53.260 +  cinfo->post = (struct jpeg_d_post_controller *) post;
  53.261 +  post->pub.start_pass = start_pass_dpost;
  53.262 +  post->whole_image = NULL;	/* flag for no virtual arrays */
  53.263 +  post->buffer = NULL;		/* flag for no strip buffer */
  53.264 +
  53.265 +  /* Create the quantization buffer, if needed */
  53.266 +  if (cinfo->quantize_colors) {
  53.267 +    /* The buffer strip height is max_v_samp_factor, which is typically
  53.268 +     * an efficient number of rows for upsampling to return.
  53.269 +     * (In the presence of output rescaling, we might want to be smarter?)
  53.270 +     */
  53.271 +    post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
  53.272 +    if (need_full_buffer) {
  53.273 +      /* Two-pass color quantization: need full-image storage. */
  53.274 +      /* We round up the number of rows to a multiple of the strip height. */
  53.275 +#ifdef QUANT_2PASS_SUPPORTED
  53.276 +      post->whole_image = (*cinfo->mem->request_virt_sarray)
  53.277 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  53.278 +	 cinfo->output_width * cinfo->out_color_components,
  53.279 +	 (JDIMENSION) jround_up((long) cinfo->output_height,
  53.280 +				(long) post->strip_height),
  53.281 +	 post->strip_height);
  53.282 +#else
  53.283 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  53.284 +#endif /* QUANT_2PASS_SUPPORTED */
  53.285 +    } else {
  53.286 +      /* One-pass color quantization: just make a strip buffer. */
  53.287 +      post->buffer = (*cinfo->mem->alloc_sarray)
  53.288 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  53.289 +	 cinfo->output_width * cinfo->out_color_components,
  53.290 +	 post->strip_height);
  53.291 +    }
  53.292 +  }
  53.293 +}
    54.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    54.2 +++ b/libs/libjpeg/jdsample.c	Sun Jun 07 17:25:49 2015 +0300
    54.3 @@ -0,0 +1,478 @@
    54.4 +/*
    54.5 + * jdsample.c
    54.6 + *
    54.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    54.8 + * This file is part of the Independent JPEG Group's software.
    54.9 + * For conditions of distribution and use, see the accompanying README file.
   54.10 + *
   54.11 + * This file contains upsampling routines.
   54.12 + *
   54.13 + * Upsampling input data is counted in "row groups".  A row group
   54.14 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
   54.15 + * sample rows of each component.  Upsampling will normally produce
   54.16 + * max_v_samp_factor pixel rows from each row group (but this could vary
   54.17 + * if the upsampler is applying a scale factor of its own).
   54.18 + *
   54.19 + * An excellent reference for image resampling is
   54.20 + *   Digital Image Warping, George Wolberg, 1990.
   54.21 + *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
   54.22 + */
   54.23 +
   54.24 +#define JPEG_INTERNALS
   54.25 +#include "jinclude.h"
   54.26 +#include "jpeglib.h"
   54.27 +
   54.28 +
   54.29 +/* Pointer to routine to upsample a single component */
   54.30 +typedef JMETHOD(void, upsample1_ptr,
   54.31 +		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
   54.32 +		 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
   54.33 +
   54.34 +/* Private subobject */
   54.35 +
   54.36 +typedef struct {
   54.37 +  struct jpeg_upsampler pub;	/* public fields */
   54.38 +
   54.39 +  /* Color conversion buffer.  When using separate upsampling and color
   54.40 +   * conversion steps, this buffer holds one upsampled row group until it
   54.41 +   * has been color converted and output.
   54.42 +   * Note: we do not allocate any storage for component(s) which are full-size,
   54.43 +   * ie do not need rescaling.  The corresponding entry of color_buf[] is
   54.44 +   * simply set to point to the input data array, thereby avoiding copying.
   54.45 +   */
   54.46 +  JSAMPARRAY color_buf[MAX_COMPONENTS];
   54.47 +
   54.48 +  /* Per-component upsampling method pointers */
   54.49 +  upsample1_ptr methods[MAX_COMPONENTS];
   54.50 +
   54.51 +  int next_row_out;		/* counts rows emitted from color_buf */
   54.52 +  JDIMENSION rows_to_go;	/* counts rows remaining in image */
   54.53 +
   54.54 +  /* Height of an input row group for each component. */
   54.55 +  int rowgroup_height[MAX_COMPONENTS];
   54.56 +
   54.57 +  /* These arrays save pixel expansion factors so that int_expand need not
   54.58 +   * recompute them each time.  They are unused for other upsampling methods.
   54.59 +   */
   54.60 +  UINT8 h_expand[MAX_COMPONENTS];
   54.61 +  UINT8 v_expand[MAX_COMPONENTS];
   54.62 +} my_upsampler;
   54.63 +
   54.64 +typedef my_upsampler * my_upsample_ptr;
   54.65 +
   54.66 +
   54.67 +/*
   54.68 + * Initialize for an upsampling pass.
   54.69 + */
   54.70 +
   54.71 +METHODDEF(void)
   54.72 +start_pass_upsample (j_decompress_ptr cinfo)
   54.73 +{
   54.74 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   54.75 +
   54.76 +  /* Mark the conversion buffer empty */
   54.77 +  upsample->next_row_out = cinfo->max_v_samp_factor;
   54.78 +  /* Initialize total-height counter for detecting bottom of image */
   54.79 +  upsample->rows_to_go = cinfo->output_height;
   54.80 +}
   54.81 +
   54.82 +
   54.83 +/*
   54.84 + * Control routine to do upsampling (and color conversion).
   54.85 + *
   54.86 + * In this version we upsample each component independently.
   54.87 + * We upsample one row group into the conversion buffer, then apply
   54.88 + * color conversion a row at a time.
   54.89 + */
   54.90 +
   54.91 +METHODDEF(void)
   54.92 +sep_upsample (j_decompress_ptr cinfo,
   54.93 +	      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   54.94 +	      JDIMENSION in_row_groups_avail,
   54.95 +	      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   54.96 +	      JDIMENSION out_rows_avail)
   54.97 +{
   54.98 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   54.99 +  int ci;
  54.100 +  jpeg_component_info * compptr;
  54.101 +  JDIMENSION num_rows;
  54.102 +
  54.103 +  /* Fill the conversion buffer, if it's empty */
  54.104 +  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
  54.105 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  54.106 +	 ci++, compptr++) {
  54.107 +      /* Invoke per-component upsample method.  Notice we pass a POINTER
  54.108 +       * to color_buf[ci], so that fullsize_upsample can change it.
  54.109 +       */
  54.110 +      (*upsample->methods[ci]) (cinfo, compptr,
  54.111 +	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
  54.112 +	upsample->color_buf + ci);
  54.113 +    }
  54.114 +    upsample->next_row_out = 0;
  54.115 +  }
  54.116 +
  54.117 +  /* Color-convert and emit rows */
  54.118 +
  54.119 +  /* How many we have in the buffer: */
  54.120 +  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  54.121 +  /* Not more than the distance to the end of the image.  Need this test
  54.122 +   * in case the image height is not a multiple of max_v_samp_factor:
  54.123 +   */
  54.124 +  if (num_rows > upsample->rows_to_go) 
  54.125 +    num_rows = upsample->rows_to_go;
  54.126 +  /* And not more than what the client can accept: */
  54.127 +  out_rows_avail -= *out_row_ctr;
  54.128 +  if (num_rows > out_rows_avail)
  54.129 +    num_rows = out_rows_avail;
  54.130 +
  54.131 +  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
  54.132 +				     (JDIMENSION) upsample->next_row_out,
  54.133 +				     output_buf + *out_row_ctr,
  54.134 +				     (int) num_rows);
  54.135 +
  54.136 +  /* Adjust counts */
  54.137 +  *out_row_ctr += num_rows;
  54.138 +  upsample->rows_to_go -= num_rows;
  54.139 +  upsample->next_row_out += num_rows;
  54.140 +  /* When the buffer is emptied, declare this input row group consumed */
  54.141 +  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
  54.142 +    (*in_row_group_ctr)++;
  54.143 +}
  54.144 +
  54.145 +
  54.146 +/*
  54.147 + * These are the routines invoked by sep_upsample to upsample pixel values
  54.148 + * of a single component.  One row group is processed per call.
  54.149 + */
  54.150 +
  54.151 +
  54.152 +/*
  54.153 + * For full-size components, we just make color_buf[ci] point at the
  54.154 + * input buffer, and thus avoid copying any data.  Note that this is
  54.155 + * safe only because sep_upsample doesn't declare the input row group
  54.156 + * "consumed" until we are done color converting and emitting it.
  54.157 + */
  54.158 +
  54.159 +METHODDEF(void)
  54.160 +fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.161 +		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.162 +{
  54.163 +  *output_data_ptr = input_data;
  54.164 +}
  54.165 +
  54.166 +
  54.167 +/*
  54.168 + * This is a no-op version used for "uninteresting" components.
  54.169 + * These components will not be referenced by color conversion.
  54.170 + */
  54.171 +
  54.172 +METHODDEF(void)
  54.173 +noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.174 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.175 +{
  54.176 +  *output_data_ptr = NULL;	/* safety check */
  54.177 +}
  54.178 +
  54.179 +
  54.180 +/*
  54.181 + * This version handles any integral sampling ratios.
  54.182 + * This is not used for typical JPEG files, so it need not be fast.
  54.183 + * Nor, for that matter, is it particularly accurate: the algorithm is
  54.184 + * simple replication of the input pixel onto the corresponding output
  54.185 + * pixels.  The hi-falutin sampling literature refers to this as a
  54.186 + * "box filter".  A box filter tends to introduce visible artifacts,
  54.187 + * so if you are actually going to use 3:1 or 4:1 sampling ratios
  54.188 + * you would be well advised to improve this code.
  54.189 + */
  54.190 +
  54.191 +METHODDEF(void)
  54.192 +int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.193 +	      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.194 +{
  54.195 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  54.196 +  JSAMPARRAY output_data = *output_data_ptr;
  54.197 +  register JSAMPROW inptr, outptr;
  54.198 +  register JSAMPLE invalue;
  54.199 +  register int h;
  54.200 +  JSAMPROW outend;
  54.201 +  int h_expand, v_expand;
  54.202 +  int inrow, outrow;
  54.203 +
  54.204 +  h_expand = upsample->h_expand[compptr->component_index];
  54.205 +  v_expand = upsample->v_expand[compptr->component_index];
  54.206 +
  54.207 +  inrow = outrow = 0;
  54.208 +  while (outrow < cinfo->max_v_samp_factor) {
  54.209 +    /* Generate one output row with proper horizontal expansion */
  54.210 +    inptr = input_data[inrow];
  54.211 +    outptr = output_data[outrow];
  54.212 +    outend = outptr + cinfo->output_width;
  54.213 +    while (outptr < outend) {
  54.214 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  54.215 +      for (h = h_expand; h > 0; h--) {
  54.216 +	*outptr++ = invalue;
  54.217 +      }
  54.218 +    }
  54.219 +    /* Generate any additional output rows by duplicating the first one */
  54.220 +    if (v_expand > 1) {
  54.221 +      jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  54.222 +			v_expand-1, cinfo->output_width);
  54.223 +    }
  54.224 +    inrow++;
  54.225 +    outrow += v_expand;
  54.226 +  }
  54.227 +}
  54.228 +
  54.229 +
  54.230 +/*
  54.231 + * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  54.232 + * It's still a box filter.
  54.233 + */
  54.234 +
  54.235 +METHODDEF(void)
  54.236 +h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.237 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.238 +{
  54.239 +  JSAMPARRAY output_data = *output_data_ptr;
  54.240 +  register JSAMPROW inptr, outptr;
  54.241 +  register JSAMPLE invalue;
  54.242 +  JSAMPROW outend;
  54.243 +  int inrow;
  54.244 +
  54.245 +  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  54.246 +    inptr = input_data[inrow];
  54.247 +    outptr = output_data[inrow];
  54.248 +    outend = outptr + cinfo->output_width;
  54.249 +    while (outptr < outend) {
  54.250 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  54.251 +      *outptr++ = invalue;
  54.252 +      *outptr++ = invalue;
  54.253 +    }
  54.254 +  }
  54.255 +}
  54.256 +
  54.257 +
  54.258 +/*
  54.259 + * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  54.260 + * It's still a box filter.
  54.261 + */
  54.262 +
  54.263 +METHODDEF(void)
  54.264 +h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.265 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.266 +{
  54.267 +  JSAMPARRAY output_data = *output_data_ptr;
  54.268 +  register JSAMPROW inptr, outptr;
  54.269 +  register JSAMPLE invalue;
  54.270 +  JSAMPROW outend;
  54.271 +  int inrow, outrow;
  54.272 +
  54.273 +  inrow = outrow = 0;
  54.274 +  while (outrow < cinfo->max_v_samp_factor) {
  54.275 +    inptr = input_data[inrow];
  54.276 +    outptr = output_data[outrow];
  54.277 +    outend = outptr + cinfo->output_width;
  54.278 +    while (outptr < outend) {
  54.279 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  54.280 +      *outptr++ = invalue;
  54.281 +      *outptr++ = invalue;
  54.282 +    }
  54.283 +    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  54.284 +		      1, cinfo->output_width);
  54.285 +    inrow++;
  54.286 +    outrow += 2;
  54.287 +  }
  54.288 +}
  54.289 +
  54.290 +
  54.291 +/*
  54.292 + * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
  54.293 + *
  54.294 + * The upsampling algorithm is linear interpolation between pixel centers,
  54.295 + * also known as a "triangle filter".  This is a good compromise between
  54.296 + * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  54.297 + * of the way between input pixel centers.
  54.298 + *
  54.299 + * A note about the "bias" calculations: when rounding fractional values to
  54.300 + * integer, we do not want to always round 0.5 up to the next integer.
  54.301 + * If we did that, we'd introduce a noticeable bias towards larger values.
  54.302 + * Instead, this code is arranged so that 0.5 will be rounded up or down at
  54.303 + * alternate pixel locations (a simple ordered dither pattern).
  54.304 + */
  54.305 +
  54.306 +METHODDEF(void)
  54.307 +h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.308 +		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.309 +{
  54.310 +  JSAMPARRAY output_data = *output_data_ptr;
  54.311 +  register JSAMPROW inptr, outptr;
  54.312 +  register int invalue;
  54.313 +  register JDIMENSION colctr;
  54.314 +  int inrow;
  54.315 +
  54.316 +  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  54.317 +    inptr = input_data[inrow];
  54.318 +    outptr = output_data[inrow];
  54.319 +    /* Special case for first column */
  54.320 +    invalue = GETJSAMPLE(*inptr++);
  54.321 +    *outptr++ = (JSAMPLE) invalue;
  54.322 +    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  54.323 +
  54.324 +    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  54.325 +      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  54.326 +      invalue = GETJSAMPLE(*inptr++) * 3;
  54.327 +      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
  54.328 +      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  54.329 +    }
  54.330 +
  54.331 +    /* Special case for last column */
  54.332 +    invalue = GETJSAMPLE(*inptr);
  54.333 +    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
  54.334 +    *outptr++ = (JSAMPLE) invalue;
  54.335 +  }
  54.336 +}
  54.337 +
  54.338 +
  54.339 +/*
  54.340 + * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
  54.341 + * Again a triangle filter; see comments for h2v1 case, above.
  54.342 + *
  54.343 + * It is OK for us to reference the adjacent input rows because we demanded
  54.344 + * context from the main buffer controller (see initialization code).
  54.345 + */
  54.346 +
  54.347 +METHODDEF(void)
  54.348 +h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  54.349 +		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  54.350 +{
  54.351 +  JSAMPARRAY output_data = *output_data_ptr;
  54.352 +  register JSAMPROW inptr0, inptr1, outptr;
  54.353 +#if BITS_IN_JSAMPLE == 8
  54.354 +  register int thiscolsum, lastcolsum, nextcolsum;
  54.355 +#else
  54.356 +  register INT32 thiscolsum, lastcolsum, nextcolsum;
  54.357 +#endif
  54.358 +  register JDIMENSION colctr;
  54.359 +  int inrow, outrow, v;
  54.360 +
  54.361 +  inrow = outrow = 0;
  54.362 +  while (outrow < cinfo->max_v_samp_factor) {
  54.363 +    for (v = 0; v < 2; v++) {
  54.364 +      /* inptr0 points to nearest input row, inptr1 points to next nearest */
  54.365 +      inptr0 = input_data[inrow];
  54.366 +      if (v == 0)		/* next nearest is row above */
  54.367 +	inptr1 = input_data[inrow-1];
  54.368 +      else			/* next nearest is row below */
  54.369 +	inptr1 = input_data[inrow+1];
  54.370 +      outptr = output_data[outrow++];
  54.371 +
  54.372 +      /* Special case for first column */
  54.373 +      thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  54.374 +      nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  54.375 +      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  54.376 +      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  54.377 +      lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  54.378 +
  54.379 +      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  54.380 +	/* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  54.381 +	/* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  54.382 +	nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  54.383 +	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  54.384 +	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  54.385 +	lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  54.386 +      }
  54.387 +
  54.388 +      /* Special case for last column */
  54.389 +      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  54.390 +      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
  54.391 +    }
  54.392 +    inrow++;
  54.393 +  }
  54.394 +}
  54.395 +
  54.396 +
  54.397 +/*
  54.398 + * Module initialization routine for upsampling.
  54.399 + */
  54.400 +
  54.401 +GLOBAL(void)
  54.402 +jinit_upsampler (j_decompress_ptr cinfo)
  54.403 +{
  54.404 +  my_upsample_ptr upsample;
  54.405 +  int ci;
  54.406 +  jpeg_component_info * compptr;
  54.407 +  boolean need_buffer, do_fancy;
  54.408 +  int h_in_group, v_in_group, h_out_group, v_out_group;
  54.409 +
  54.410 +  upsample = (my_upsample_ptr)
  54.411 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  54.412 +				SIZEOF(my_upsampler));
  54.413 +  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  54.414 +  upsample->pub.start_pass = start_pass_upsample;
  54.415 +  upsample->pub.upsample = sep_upsample;
  54.416 +  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  54.417 +
  54.418 +  if (cinfo->CCIR601_sampling)	/* this isn't supported */
  54.419 +    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  54.420 +
  54.421 +  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
  54.422 +   * so don't ask for it.
  54.423 +   */
  54.424 +  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
  54.425 +
  54.426 +  /* Verify we can handle the sampling factors, select per-component methods,
  54.427 +   * and create storage as needed.
  54.428 +   */
  54.429 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  54.430 +       ci++, compptr++) {
  54.431 +    /* Compute size of an "input group" after IDCT scaling.  This many samples
  54.432 +     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  54.433 +     */
  54.434 +    h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
  54.435 +		 cinfo->min_DCT_scaled_size;
  54.436 +    v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  54.437 +		 cinfo->min_DCT_scaled_size;
  54.438 +    h_out_group = cinfo->max_h_samp_factor;
  54.439 +    v_out_group = cinfo->max_v_samp_factor;
  54.440 +    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  54.441 +    need_buffer = TRUE;
  54.442 +    if (! compptr->component_needed) {
  54.443 +      /* Don't bother to upsample an uninteresting component. */
  54.444 +      upsample->methods[ci] = noop_upsample;
  54.445 +      need_buffer = FALSE;
  54.446 +    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
  54.447 +      /* Fullsize components can be processed without any work. */
  54.448 +      upsample->methods[ci] = fullsize_upsample;
  54.449 +      need_buffer = FALSE;
  54.450 +    } else if (h_in_group * 2 == h_out_group &&
  54.451 +	       v_in_group == v_out_group) {
  54.452 +      /* Special cases for 2h1v upsampling */
  54.453 +      if (do_fancy && compptr->downsampled_width > 2)
  54.454 +	upsample->methods[ci] = h2v1_fancy_upsample;
  54.455 +      else
  54.456 +	upsample->methods[ci] = h2v1_upsample;
  54.457 +    } else if (h_in_group * 2 == h_out_group &&
  54.458 +	       v_in_group * 2 == v_out_group) {
  54.459 +      /* Special cases for 2h2v upsampling */
  54.460 +      if (do_fancy && compptr->downsampled_width > 2) {
  54.461 +	upsample->methods[ci] = h2v2_fancy_upsample;
  54.462 +	upsample->pub.need_context_rows = TRUE;
  54.463 +      } else
  54.464 +	upsample->methods[ci] = h2v2_upsample;
  54.465 +    } else if ((h_out_group % h_in_group) == 0 &&
  54.466 +	       (v_out_group % v_in_group) == 0) {
  54.467 +      /* Generic integral-factors upsampling method */
  54.468 +      upsample->methods[ci] = int_upsample;
  54.469 +      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  54.470 +      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  54.471 +    } else
  54.472 +      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  54.473 +    if (need_buffer) {
  54.474 +      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  54.475 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  54.476 +	 (JDIMENSION) jround_up((long) cinfo->output_width,
  54.477 +				(long) cinfo->max_h_samp_factor),
  54.478 +	 (JDIMENSION) cinfo->max_v_samp_factor);
  54.479 +    }
  54.480 +  }
  54.481 +}
    55.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    55.2 +++ b/libs/libjpeg/jdtrans.c	Sun Jun 07 17:25:49 2015 +0300
    55.3 @@ -0,0 +1,143 @@
    55.4 +/*
    55.5 + * jdtrans.c
    55.6 + *
    55.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    55.8 + * This file is part of the Independent JPEG Group's software.
    55.9 + * For conditions of distribution and use, see the accompanying README file.
   55.10 + *
   55.11 + * This file contains library routines for transcoding decompression,
   55.12 + * that is, reading raw DCT coefficient arrays from an input JPEG file.
   55.13 + * The routines in jdapimin.c will also be needed by a transcoder.
   55.14 + */
   55.15 +
   55.16 +#define JPEG_INTERNALS
   55.17 +#include "jinclude.h"
   55.18 +#include "jpeglib.h"
   55.19 +
   55.20 +
   55.21 +/* Forward declarations */
   55.22 +LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
   55.23 +
   55.24 +
   55.25 +/*
   55.26 + * Read the coefficient arrays from a JPEG file.
   55.27 + * jpeg_read_header must be completed before calling this.
   55.28 + *
   55.29 + * The entire image is read into a set of virtual coefficient-block arrays,
   55.30 + * one per component.  The return value is a pointer to the array of
   55.31 + * virtual-array descriptors.  These can be manipulated directly via the
   55.32 + * JPEG memory manager, or handed off to jpeg_write_coefficients().
   55.33 + * To release the memory occupied by the virtual arrays, call
   55.34 + * jpeg_finish_decompress() when done with the data.
   55.35 + *
   55.36 + * An alternative usage is to simply obtain access to the coefficient arrays
   55.37 + * during a buffered-image-mode decompression operation.  This is allowed
   55.38 + * after any jpeg_finish_output() call.  The arrays can be accessed until
   55.39 + * jpeg_finish_decompress() is called.  (Note that any call to the library
   55.40 + * may reposition the arrays, so don't rely on access_virt_barray() results
   55.41 + * to stay valid across library calls.)
   55.42 + *
   55.43 + * Returns NULL if suspended.  This case need be checked only if
   55.44 + * a suspending data source is used.
   55.45 + */
   55.46 +
   55.47 +GLOBAL(jvirt_barray_ptr *)
   55.48 +jpeg_read_coefficients (j_decompress_ptr cinfo)
   55.49 +{
   55.50 +  if (cinfo->global_state == DSTATE_READY) {
   55.51 +    /* First call: initialize active modules */
   55.52 +    transdecode_master_selection(cinfo);
   55.53 +    cinfo->global_state = DSTATE_RDCOEFS;
   55.54 +  }
   55.55 +  if (cinfo->global_state == DSTATE_RDCOEFS) {
   55.56 +    /* Absorb whole file into the coef buffer */
   55.57 +    for (;;) {
   55.58 +      int retcode;
   55.59 +      /* Call progress monitor hook if present */
   55.60 +      if (cinfo->progress != NULL)
   55.61 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   55.62 +      /* Absorb some more input */
   55.63 +      retcode = (*cinfo->inputctl->consume_input) (cinfo);
   55.64 +      if (retcode == JPEG_SUSPENDED)
   55.65 +	return NULL;
   55.66 +      if (retcode == JPEG_REACHED_EOI)
   55.67 +	break;
   55.68 +      /* Advance progress counter if appropriate */
   55.69 +      if (cinfo->progress != NULL &&
   55.70 +	  (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
   55.71 +	if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
   55.72 +	  /* startup underestimated number of scans; ratchet up one scan */
   55.73 +	  cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
   55.74 +	}
   55.75 +      }
   55.76 +    }
   55.77 +    /* Set state so that jpeg_finish_decompress does the right thing */
   55.78 +    cinfo->global_state = DSTATE_STOPPING;
   55.79 +  }
   55.80 +  /* At this point we should be in state DSTATE_STOPPING if being used
   55.81 +   * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
   55.82 +   * to the coefficients during a full buffered-image-mode decompression.
   55.83 +   */
   55.84 +  if ((cinfo->global_state == DSTATE_STOPPING ||
   55.85 +       cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
   55.86 +    return cinfo->coef->coef_arrays;
   55.87 +  }
   55.88 +  /* Oops, improper usage */
   55.89 +  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   55.90 +  return NULL;			/* keep compiler happy */
   55.91 +}
   55.92 +
   55.93 +
   55.94 +/*
   55.95 + * Master selection of decompression modules for transcoding.
   55.96 + * This substitutes for jdmaster.c's initialization of the full decompressor.
   55.97 + */
   55.98 +
   55.99 +LOCAL(void)
  55.100 +transdecode_master_selection (j_decompress_ptr cinfo)
  55.101 +{
  55.102 +  /* This is effectively a buffered-image operation. */
  55.103 +  cinfo->buffered_image = TRUE;
  55.104 +
  55.105 +  /* Entropy decoding: either Huffman or arithmetic coding. */
  55.106 +  if (cinfo->arith_code) {
  55.107 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  55.108 +  } else {
  55.109 +    if (cinfo->progressive_mode) {
  55.110 +#ifdef D_PROGRESSIVE_SUPPORTED
  55.111 +      jinit_phuff_decoder(cinfo);
  55.112 +#else
  55.113 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  55.114 +#endif
  55.115 +    } else
  55.116 +      jinit_huff_decoder(cinfo);
  55.117 +  }
  55.118 +
  55.119 +  /* Always get a full-image coefficient buffer. */
  55.120 +  jinit_d_coef_controller(cinfo, TRUE);
  55.121 +
  55.122 +  /* We can now tell the memory manager to allocate virtual arrays. */
  55.123 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  55.124 +
  55.125 +  /* Initialize input side of decompressor to consume first scan. */
  55.126 +  (*cinfo->inputctl->start_input_pass) (cinfo);
  55.127 +
  55.128 +  /* Initialize progress monitoring. */
  55.129 +  if (cinfo->progress != NULL) {
  55.130 +    int nscans;
  55.131 +    /* Estimate number of scans to set pass_limit. */
  55.132 +    if (cinfo->progressive_mode) {
  55.133 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  55.134 +      nscans = 2 + 3 * cinfo->num_components;
  55.135 +    } else if (cinfo->inputctl->has_multiple_scans) {
  55.136 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  55.137 +      nscans = cinfo->num_components;
  55.138 +    } else {
  55.139 +      nscans = 1;
  55.140 +    }
  55.141 +    cinfo->progress->pass_counter = 0L;
  55.142 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  55.143 +    cinfo->progress->completed_passes = 0;
  55.144 +    cinfo->progress->total_passes = 1;
  55.145 +  }
  55.146 +}
    56.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    56.2 +++ b/libs/libjpeg/jerror.c	Sun Jun 07 17:25:49 2015 +0300
    56.3 @@ -0,0 +1,252 @@
    56.4 +/*
    56.5 + * jerror.c
    56.6 + *
    56.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    56.8 + * This file is part of the Independent JPEG Group's software.
    56.9 + * For conditions of distribution and use, see the accompanying README file.
   56.10 + *
   56.11 + * This file contains simple error-reporting and trace-message routines.
   56.12 + * These are suitable for Unix-like systems and others where writing to
   56.13 + * stderr is the right thing to do.  Many applications will want to replace
   56.14 + * some or all of these routines.
   56.15 + *
   56.16 + * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
   56.17 + * you get a Windows-specific hack to display error messages in a dialog box.
   56.18 + * It ain't much, but it beats dropping error messages into the bit bucket,
   56.19 + * which is what happens to output to stderr under most Windows C compilers.
   56.20 + *
   56.21 + * These routines are used by both the compression and decompression code.
   56.22 + */
   56.23 +
   56.24 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   56.25 +#include "jinclude.h"
   56.26 +#include "jpeglib.h"
   56.27 +#include "jversion.h"
   56.28 +#include "jerror.h"
   56.29 +
   56.30 +#ifdef USE_WINDOWS_MESSAGEBOX
   56.31 +#include <windows.h>
   56.32 +#endif
   56.33 +
   56.34 +#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
   56.35 +#define EXIT_FAILURE  1
   56.36 +#endif
   56.37 +
   56.38 +
   56.39 +/*
   56.40 + * Create the message string table.
   56.41 + * We do this from the master message list in jerror.h by re-reading
   56.42 + * jerror.h with a suitable definition for macro JMESSAGE.
   56.43 + * The message table is made an external symbol just in case any applications
   56.44 + * want to refer to it directly.
   56.45 + */
   56.46 +
   56.47 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   56.48 +#define jpeg_std_message_table	jMsgTable
   56.49 +#endif
   56.50 +
   56.51 +#define JMESSAGE(code,string)	string ,
   56.52 +
   56.53 +const char * const jpeg_std_message_table[] = {
   56.54 +#include "jerror.h"
   56.55 +  NULL
   56.56 +};
   56.57 +
   56.58 +
   56.59 +/*
   56.60 + * Error exit handler: must not return to caller.
   56.61 + *
   56.62 + * Applications may override this if they want to get control back after
   56.63 + * an error.  Typically one would longjmp somewhere instead of exiting.
   56.64 + * The setjmp buffer can be made a private field within an expanded error
   56.65 + * handler object.  Note that the info needed to generate an error message
   56.66 + * is stored in the error object, so you can generate the message now or
   56.67 + * later, at your convenience.
   56.68 + * You should make sure that the JPEG object is cleaned up (with jpeg_abort
   56.69 + * or jpeg_destroy) at some point.
   56.70 + */
   56.71 +
   56.72 +METHODDEF(void)
   56.73 +error_exit (j_common_ptr cinfo)
   56.74 +{
   56.75 +  /* Always display the message */
   56.76 +  (*cinfo->err->output_message) (cinfo);
   56.77 +
   56.78 +  /* Let the memory manager delete any temp files before we die */
   56.79 +  jpeg_destroy(cinfo);
   56.80 +
   56.81 +  exit(EXIT_FAILURE);
   56.82 +}
   56.83 +
   56.84 +
   56.85 +/*
   56.86 + * Actual output of an error or trace message.
   56.87 + * Applications may override this method to send JPEG messages somewhere
   56.88 + * other than stderr.
   56.89 + *
   56.90 + * On Windows, printing to stderr is generally completely useless,
   56.91 + * so we provide optional code to produce an error-dialog popup.
   56.92 + * Most Windows applications will still prefer to override this routine,
   56.93 + * but if they don't, it'll do something at least marginally useful.
   56.94 + *
   56.95 + * NOTE: to use the library in an environment that doesn't support the
   56.96 + * C stdio library, you may have to delete the call to fprintf() entirely,
   56.97 + * not just not use this routine.
   56.98 + */
   56.99 +
  56.100 +METHODDEF(void)
  56.101 +output_message (j_common_ptr cinfo)
  56.102 +{
  56.103 +  char buffer[JMSG_LENGTH_MAX];
  56.104 +
  56.105 +  /* Create the message */
  56.106 +  (*cinfo->err->format_message) (cinfo, buffer);
  56.107 +
  56.108 +#ifdef USE_WINDOWS_MESSAGEBOX
  56.109 +  /* Display it in a message dialog box */
  56.110 +  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
  56.111 +	     MB_OK | MB_ICONERROR);
  56.112 +#else
  56.113 +  /* Send it to stderr, adding a newline */
  56.114 +  fprintf(stderr, "%s\n", buffer);
  56.115 +#endif
  56.116 +}
  56.117 +
  56.118 +
  56.119 +/*
  56.120 + * Decide whether to emit a trace or warning message.
  56.121 + * msg_level is one of:
  56.122 + *   -1: recoverable corrupt-data warning, may want to abort.
  56.123 + *    0: important advisory messages (always display to user).
  56.124 + *    1: first level of tracing detail.
  56.125 + *    2,3,...: successively more detailed tracing messages.
  56.126 + * An application might override this method if it wanted to abort on warnings
  56.127 + * or change the policy about which messages to display.
  56.128 + */
  56.129 +
  56.130 +METHODDEF(void)
  56.131 +emit_message (j_common_ptr cinfo, int msg_level)
  56.132 +{
  56.133 +  struct jpeg_error_mgr * err = cinfo->err;
  56.134 +
  56.135 +  if (msg_level < 0) {
  56.136 +    /* It's a warning message.  Since corrupt files may generate many warnings,
  56.137 +     * the policy implemented here is to show only the first warning,
  56.138 +     * unless trace_level >= 3.
  56.139 +     */
  56.140 +    if (err->num_warnings == 0 || err->trace_level >= 3)
  56.141 +      (*err->output_message) (cinfo);
  56.142 +    /* Always count warnings in num_warnings. */
  56.143 +    err->num_warnings++;
  56.144 +  } else {
  56.145 +    /* It's a trace message.  Show it if trace_level >= msg_level. */
  56.146 +    if (err->trace_level >= msg_level)
  56.147 +      (*err->output_message) (cinfo);
  56.148 +  }
  56.149 +}
  56.150 +
  56.151 +
  56.152 +/*
  56.153 + * Format a message string for the most recent JPEG error or message.
  56.154 + * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  56.155 + * characters.  Note that no '\n' character is added to the string.
  56.156 + * Few applications should need to override this method.
  56.157 + */
  56.158 +
  56.159 +METHODDEF(void)
  56.160 +format_message (j_common_ptr cinfo, char * buffer)
  56.161 +{
  56.162 +  struct jpeg_error_mgr * err = cinfo->err;
  56.163 +  int msg_code = err->msg_code;
  56.164 +  const char * msgtext = NULL;
  56.165 +  const char * msgptr;
  56.166 +  char ch;
  56.167 +  boolean isstring;
  56.168 +
  56.169 +  /* Look up message string in proper table */
  56.170 +  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  56.171 +    msgtext = err->jpeg_message_table[msg_code];
  56.172 +  } else if (err->addon_message_table != NULL &&
  56.173 +	     msg_code >= err->first_addon_message &&
  56.174 +	     msg_code <= err->last_addon_message) {
  56.175 +    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  56.176 +  }
  56.177 +
  56.178 +  /* Defend against bogus message number */
  56.179 +  if (msgtext == NULL) {
  56.180 +    err->msg_parm.i[0] = msg_code;
  56.181 +    msgtext = err->jpeg_message_table[0];
  56.182 +  }
  56.183 +
  56.184 +  /* Check for string parameter, as indicated by %s in the message text */
  56.185 +  isstring = FALSE;
  56.186 +  msgptr = msgtext;
  56.187 +  while ((ch = *msgptr++) != '\0') {
  56.188 +    if (ch == '%') {
  56.189 +      if (*msgptr == 's') isstring = TRUE;
  56.190 +      break;
  56.191 +    }
  56.192 +  }
  56.193 +
  56.194 +  /* Format the message into the passed buffer */
  56.195 +  if (isstring)
  56.196 +    sprintf(buffer, msgtext, err->msg_parm.s);
  56.197 +  else
  56.198 +    sprintf(buffer, msgtext,
  56.199 +	    err->msg_parm.i[0], err->msg_parm.i[1],
  56.200 +	    err->msg_parm.i[2], err->msg_parm.i[3],
  56.201 +	    err->msg_parm.i[4], err->msg_parm.i[5],
  56.202 +	    err->msg_parm.i[6], err->msg_parm.i[7]);
  56.203 +}
  56.204 +
  56.205 +
  56.206 +/*
  56.207 + * Reset error state variables at start of a new image.
  56.208 + * This is called during compression startup to reset trace/error
  56.209 + * processing to default state, without losing any application-specific
  56.210 + * method pointers.  An application might possibly want to override
  56.211 + * this method if it has additional error processing state.
  56.212 + */
  56.213 +
  56.214 +METHODDEF(void)
  56.215 +reset_error_mgr (j_common_ptr cinfo)
  56.216 +{
  56.217 +  cinfo->err->num_warnings = 0;
  56.218 +  /* trace_level is not reset since it is an application-supplied parameter */
  56.219 +  cinfo->err->msg_code = 0;	/* may be useful as a flag for "no error" */
  56.220 +}
  56.221 +
  56.222 +
  56.223 +/*
  56.224 + * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  56.225 + * Typical call is:
  56.226 + *	struct jpeg_compress_struct cinfo;
  56.227 + *	struct jpeg_error_mgr err;
  56.228 + *
  56.229 + *	cinfo.err = jpeg_std_error(&err);
  56.230 + * after which the application may override some of the methods.
  56.231 + */
  56.232 +
  56.233 +GLOBAL(struct jpeg_error_mgr *)
  56.234 +jpeg_std_error (struct jpeg_error_mgr * err)
  56.235 +{
  56.236 +  err->error_exit = error_exit;
  56.237 +  err->emit_message = emit_message;
  56.238 +  err->output_message = output_message;
  56.239 +  err->format_message = format_message;
  56.240 +  err->reset_error_mgr = reset_error_mgr;
  56.241 +
  56.242 +  err->trace_level = 0;		/* default = no tracing */
  56.243 +  err->num_warnings = 0;	/* no warnings emitted yet */
  56.244 +  err->msg_code = 0;		/* may be useful as a flag for "no error" */
  56.245 +
  56.246 +  /* Initialize message table pointers */
  56.247 +  err->jpeg_message_table = jpeg_std_message_table;
  56.248 +  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  56.249 +
  56.250 +  err->addon_message_table = NULL;
  56.251 +  err->first_addon_message = 0;	/* for safety */
  56.252 +  err->last_addon_message = 0;
  56.253 +
  56.254 +  return err;
  56.255 +}
    57.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    57.2 +++ b/libs/libjpeg/jerror.h	Sun Jun 07 17:25:49 2015 +0300
    57.3 @@ -0,0 +1,291 @@
    57.4 +/*
    57.5 + * jerror.h
    57.6 + *
    57.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    57.8 + * This file is part of the Independent JPEG Group's software.
    57.9 + * For conditions of distribution and use, see the accompanying README file.
   57.10 + *
   57.11 + * This file defines the error and message codes for the JPEG library.
   57.12 + * Edit this file to add new codes, or to translate the message strings to
   57.13 + * some other language.
   57.14 + * A set of error-reporting macros are defined too.  Some applications using
   57.15 + * the JPEG library may wish to include this file to get the error codes
   57.16 + * and/or the macros.
   57.17 + */
   57.18 +
   57.19 +/*
   57.20 + * To define the enum list of message codes, include this file without
   57.21 + * defining macro JMESSAGE.  To create a message string table, include it
   57.22 + * again with a suitable JMESSAGE definition (see jerror.c for an example).
   57.23 + */
   57.24 +#ifndef JMESSAGE
   57.25 +#ifndef JERROR_H
   57.26 +/* First time through, define the enum list */
   57.27 +#define JMAKE_ENUM_LIST
   57.28 +#else
   57.29 +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
   57.30 +#define JMESSAGE(code,string)
   57.31 +#endif /* JERROR_H */
   57.32 +#endif /* JMESSAGE */
   57.33 +
   57.34 +#ifdef JMAKE_ENUM_LIST
   57.35 +
   57.36 +typedef enum {
   57.37 +
   57.38 +#define JMESSAGE(code,string)	code ,
   57.39 +
   57.40 +#endif /* JMAKE_ENUM_LIST */
   57.41 +
   57.42 +JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
   57.43 +
   57.44 +/* For maintenance convenience, list is alphabetical by message code name */
   57.45 +JMESSAGE(JERR_ARITH_NOTIMPL,
   57.46 +	 "Sorry, there are legal restrictions on arithmetic coding")
   57.47 +JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
   57.48 +JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
   57.49 +JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
   57.50 +JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
   57.51 +JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
   57.52 +JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
   57.53 +JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
   57.54 +JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
   57.55 +JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
   57.56 +JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
   57.57 +JMESSAGE(JERR_BAD_LIB_VERSION,
   57.58 +	 "Wrong JPEG library version: library is %d, caller expects %d")
   57.59 +JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
   57.60 +JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
   57.61 +JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
   57.62 +JMESSAGE(JERR_BAD_PROGRESSION,
   57.63 +	 "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
   57.64 +JMESSAGE(JERR_BAD_PROG_SCRIPT,
   57.65 +	 "Invalid progressive parameters at scan script entry %d")
   57.66 +JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
   57.67 +JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
   57.68 +JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
   57.69 +JMESSAGE(JERR_BAD_STRUCT_SIZE,
   57.70 +	 "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
   57.71 +JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
   57.72 +JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
   57.73 +JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
   57.74 +JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
   57.75 +JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
   57.76 +JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
   57.77 +JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
   57.78 +JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
   57.79 +JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
   57.80 +JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
   57.81 +JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
   57.82 +JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
   57.83 +JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
   57.84 +JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
   57.85 +JMESSAGE(JERR_FILE_READ, "Input file read error")
   57.86 +JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
   57.87 +JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
   57.88 +JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
   57.89 +JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
   57.90 +JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
   57.91 +JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
   57.92 +JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
   57.93 +JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
   57.94 +	 "Cannot transcode due to multiple use of quantization table %d")
   57.95 +JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
   57.96 +JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
   57.97 +JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
   57.98 +JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
   57.99 +JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
  57.100 +JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
  57.101 +JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
  57.102 +JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
  57.103 +JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
  57.104 +JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
  57.105 +JMESSAGE(JERR_QUANT_COMPONENTS,
  57.106 +	 "Cannot quantize more than %d color components")
  57.107 +JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
  57.108 +JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
  57.109 +JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
  57.110 +JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
  57.111 +JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
  57.112 +JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
  57.113 +JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
  57.114 +JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
  57.115 +JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
  57.116 +JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
  57.117 +JMESSAGE(JERR_TFILE_WRITE,
  57.118 +	 "Write failed on temporary file --- out of disk space?")
  57.119 +JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
  57.120 +JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
  57.121 +JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
  57.122 +JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
  57.123 +JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
  57.124 +JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
  57.125 +JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
  57.126 +JMESSAGE(JMSG_VERSION, JVERSION)
  57.127 +JMESSAGE(JTRC_16BIT_TABLES,
  57.128 +	 "Caution: quantization tables are too coarse for baseline JPEG")
  57.129 +JMESSAGE(JTRC_ADOBE,
  57.130 +	 "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
  57.131 +JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
  57.132 +JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
  57.133 +JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
  57.134 +JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
  57.135 +JMESSAGE(JTRC_DQT, "Define Quantization Table %d  precision %d")
  57.136 +JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
  57.137 +JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
  57.138 +JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
  57.139 +JMESSAGE(JTRC_EOI, "End Of Image")
  57.140 +JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
  57.141 +JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d  %d")
  57.142 +JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
  57.143 +	 "Warning: thumbnail image size does not match data length %u")
  57.144 +JMESSAGE(JTRC_JFIF_EXTENSION,
  57.145 +	 "JFIF extension marker: type 0x%02x, length %u")
  57.146 +JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
  57.147 +JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
  57.148 +JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
  57.149 +JMESSAGE(JTRC_QUANTVALS, "        %4u %4u %4u %4u %4u %4u %4u %4u")
  57.150 +JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
  57.151 +JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
  57.152 +JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
  57.153 +JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
  57.154 +JMESSAGE(JTRC_RST, "RST%d")
  57.155 +JMESSAGE(JTRC_SMOOTH_NOTIMPL,
  57.156 +	 "Smoothing not supported with nonstandard sampling ratios")
  57.157 +JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
  57.158 +JMESSAGE(JTRC_SOF_COMPONENT, "    Component %d: %dhx%dv q=%d")
  57.159 +JMESSAGE(JTRC_SOI, "Start of Image")
  57.160 +JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
  57.161 +JMESSAGE(JTRC_SOS_COMPONENT, "    Component %d: dc=%d ac=%d")
  57.162 +JMESSAGE(JTRC_SOS_PARAMS, "  Ss=%d, Se=%d, Ah=%d, Al=%d")
  57.163 +JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
  57.164 +JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
  57.165 +JMESSAGE(JTRC_THUMB_JPEG,
  57.166 +	 "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
  57.167 +JMESSAGE(JTRC_THUMB_PALETTE,
  57.168 +	 "JFIF extension marker: palette thumbnail image, length %u")
  57.169 +JMESSAGE(JTRC_THUMB_RGB,
  57.170 +	 "JFIF extension marker: RGB thumbnail image, length %u")
  57.171 +JMESSAGE(JTRC_UNKNOWN_IDS,
  57.172 +	 "Unrecognized component IDs %d %d %d, assuming YCbCr")
  57.173 +JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
  57.174 +JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
  57.175 +JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
  57.176 +JMESSAGE(JWRN_BOGUS_PROGRESSION,
  57.177 +	 "Inconsistent progression sequence for component %d coefficient %d")
  57.178 +JMESSAGE(JWRN_EXTRANEOUS_DATA,
  57.179 +	 "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
  57.180 +JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
  57.181 +JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
  57.182 +JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
  57.183 +JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
  57.184 +JMESSAGE(JWRN_MUST_RESYNC,
  57.185 +	 "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
  57.186 +JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
  57.187 +JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
  57.188 +
  57.189 +#ifdef JMAKE_ENUM_LIST
  57.190 +
  57.191 +  JMSG_LASTMSGCODE
  57.192 +} J_MESSAGE_CODE;
  57.193 +
  57.194 +#undef JMAKE_ENUM_LIST
  57.195 +#endif /* JMAKE_ENUM_LIST */
  57.196 +
  57.197 +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
  57.198 +#undef JMESSAGE
  57.199 +
  57.200 +
  57.201 +#ifndef JERROR_H
  57.202 +#define JERROR_H
  57.203 +
  57.204 +/* Macros to simplify using the error and trace message stuff */
  57.205 +/* The first parameter is either type of cinfo pointer */
  57.206 +
  57.207 +/* Fatal errors (print message and exit) */
  57.208 +#define ERREXIT(cinfo,code)  \
  57.209 +  ((cinfo)->err->msg_code = (code), \
  57.210 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.211 +#define ERREXIT1(cinfo,code,p1)  \
  57.212 +  ((cinfo)->err->msg_code = (code), \
  57.213 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.214 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.215 +#define ERREXIT2(cinfo,code,p1,p2)  \
  57.216 +  ((cinfo)->err->msg_code = (code), \
  57.217 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.218 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  57.219 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.220 +#define ERREXIT3(cinfo,code,p1,p2,p3)  \
  57.221 +  ((cinfo)->err->msg_code = (code), \
  57.222 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.223 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  57.224 +   (cinfo)->err->msg_parm.i[2] = (p3), \
  57.225 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.226 +#define ERREXIT4(cinfo,code,p1,p2,p3,p4)  \
  57.227 +  ((cinfo)->err->msg_code = (code), \
  57.228 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.229 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  57.230 +   (cinfo)->err->msg_parm.i[2] = (p3), \
  57.231 +   (cinfo)->err->msg_parm.i[3] = (p4), \
  57.232 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.233 +#define ERREXITS(cinfo,code,str)  \
  57.234 +  ((cinfo)->err->msg_code = (code), \
  57.235 +   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
  57.236 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  57.237 +
  57.238 +#define MAKESTMT(stuff)		do { stuff } while (0)
  57.239 +
  57.240 +/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
  57.241 +#define WARNMS(cinfo,code)  \
  57.242 +  ((cinfo)->err->msg_code = (code), \
  57.243 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  57.244 +#define WARNMS1(cinfo,code,p1)  \
  57.245 +  ((cinfo)->err->msg_code = (code), \
  57.246 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.247 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  57.248 +#define WARNMS2(cinfo,code,p1,p2)  \
  57.249 +  ((cinfo)->err->msg_code = (code), \
  57.250 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.251 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  57.252 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  57.253 +
  57.254 +/* Informational/debugging messages */
  57.255 +#define TRACEMS(cinfo,lvl,code)  \
  57.256 +  ((cinfo)->err->msg_code = (code), \
  57.257 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  57.258 +#define TRACEMS1(cinfo,lvl,code,p1)  \
  57.259 +  ((cinfo)->err->msg_code = (code), \
  57.260 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.261 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  57.262 +#define TRACEMS2(cinfo,lvl,code,p1,p2)  \
  57.263 +  ((cinfo)->err->msg_code = (code), \
  57.264 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  57.265 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  57.266 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  57.267 +#define TRACEMS3(cinfo,lvl,code,p1,p2,p3)  \
  57.268 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  57.269 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
  57.270 +	   (cinfo)->err->msg_code = (code); \
  57.271 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  57.272 +#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4)  \
  57.273 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  57.274 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  57.275 +	   (cinfo)->err->msg_code = (code); \
  57.276 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  57.277 +#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5)  \
  57.278 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  57.279 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  57.280 +	   _mp[4] = (p5); \
  57.281 +	   (cinfo)->err->msg_code = (code); \
  57.282 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  57.283 +#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8)  \
  57.284 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  57.285 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  57.286 +	   _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
  57.287 +	   (cinfo)->err->msg_code = (code); \
  57.288 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  57.289 +#define TRACEMSS(cinfo,lvl,code,str)  \
  57.290 +  ((cinfo)->err->msg_code = (code), \
  57.291 +   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
  57.292 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  57.293 +
  57.294 +#endif /* JERROR_H */
    58.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    58.2 +++ b/libs/libjpeg/jfdctflt.c	Sun Jun 07 17:25:49 2015 +0300
    58.3 @@ -0,0 +1,168 @@
    58.4 +/*
    58.5 + * jfdctflt.c
    58.6 + *
    58.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    58.8 + * This file is part of the Independent JPEG Group's software.
    58.9 + * For conditions of distribution and use, see the accompanying README file.
   58.10 + *
   58.11 + * This file contains a floating-point implementation of the
   58.12 + * forward DCT (Discrete Cosine Transform).
   58.13 + *
   58.14 + * This implementation should be more accurate than either of the integer
   58.15 + * DCT implementations.  However, it may not give the same results on all
   58.16 + * machines because of differences in roundoff behavior.  Speed will depend
   58.17 + * on the hardware's floating point capacity.
   58.18 + *
   58.19 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   58.20 + * on each column.  Direct algorithms are also available, but they are
   58.21 + * much more complex and seem not to be any faster when reduced to code.
   58.22 + *
   58.23 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   58.24 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   58.25 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   58.26 + * JPEG textbook (see REFERENCES section in file README).  The following code
   58.27 + * is based directly on figure 4-8 in P&M.
   58.28 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   58.29 + * possible to arrange the computation so that many of the multiplies are
   58.30 + * simple scalings of the final outputs.  These multiplies can then be
   58.31 + * folded into the multiplications or divisions by the JPEG quantization
   58.32 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   58.33 + * to be done in the DCT itself.
   58.34 + * The primary disadvantage of this method is that with a fixed-point
   58.35 + * implementation, accuracy is lost due to imprecise representation of the
   58.36 + * scaled quantization values.  However, that problem does not arise if
   58.37 + * we use floating point arithmetic.
   58.38 + */
   58.39 +
   58.40 +#define JPEG_INTERNALS
   58.41 +#include "jinclude.h"
   58.42 +#include "jpeglib.h"
   58.43 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   58.44 +
   58.45 +#ifdef DCT_FLOAT_SUPPORTED
   58.46 +
   58.47 +
   58.48 +/*
   58.49 + * This module is specialized to the case DCTSIZE = 8.
   58.50 + */
   58.51 +
   58.52 +#if DCTSIZE != 8
   58.53 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   58.54 +#endif
   58.55 +
   58.56 +
   58.57 +/*
   58.58 + * Perform the forward DCT on one block of samples.
   58.59 + */
   58.60 +
   58.61 +GLOBAL(void)
   58.62 +jpeg_fdct_float (FAST_FLOAT * data)
   58.63 +{
   58.64 +  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   58.65 +  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
   58.66 +  FAST_FLOAT z1, z2, z3, z4, z5, z11, z13;
   58.67 +  FAST_FLOAT *dataptr;
   58.68 +  int ctr;
   58.69 +
   58.70 +  /* Pass 1: process rows. */
   58.71 +
   58.72 +  dataptr = data;
   58.73 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
   58.74 +    tmp0 = dataptr[0] + dataptr[7];
   58.75 +    tmp7 = dataptr[0] - dataptr[7];
   58.76 +    tmp1 = dataptr[1] + dataptr[6];
   58.77 +    tmp6 = dataptr[1] - dataptr[6];
   58.78 +    tmp2 = dataptr[2] + dataptr[5];
   58.79 +    tmp5 = dataptr[2] - dataptr[5];
   58.80 +    tmp3 = dataptr[3] + dataptr[4];
   58.81 +    tmp4 = dataptr[3] - dataptr[4];
   58.82 +    
   58.83 +    /* Even part */
   58.84 +    
   58.85 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
   58.86 +    tmp13 = tmp0 - tmp3;
   58.87 +    tmp11 = tmp1 + tmp2;
   58.88 +    tmp12 = tmp1 - tmp2;
   58.89 +    
   58.90 +    dataptr[0] = tmp10 + tmp11; /* phase 3 */
   58.91 +    dataptr[4] = tmp10 - tmp11;
   58.92 +    
   58.93 +    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
   58.94 +    dataptr[2] = tmp13 + z1;	/* phase 5 */
   58.95 +    dataptr[6] = tmp13 - z1;
   58.96 +    
   58.97 +    /* Odd part */
   58.98 +
   58.99 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  58.100 +    tmp11 = tmp5 + tmp6;
  58.101 +    tmp12 = tmp6 + tmp7;
  58.102 +
  58.103 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  58.104 +    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
  58.105 +    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
  58.106 +    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
  58.107 +    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
  58.108 +
  58.109 +    z11 = tmp7 + z3;		/* phase 5 */
  58.110 +    z13 = tmp7 - z3;
  58.111 +
  58.112 +    dataptr[5] = z13 + z2;	/* phase 6 */
  58.113 +    dataptr[3] = z13 - z2;
  58.114 +    dataptr[1] = z11 + z4;
  58.115 +    dataptr[7] = z11 - z4;
  58.116 +
  58.117 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  58.118 +  }
  58.119 +
  58.120 +  /* Pass 2: process columns. */
  58.121 +
  58.122 +  dataptr = data;
  58.123 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  58.124 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  58.125 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  58.126 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  58.127 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  58.128 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  58.129 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  58.130 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  58.131 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  58.132 +    
  58.133 +    /* Even part */
  58.134 +    
  58.135 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  58.136 +    tmp13 = tmp0 - tmp3;
  58.137 +    tmp11 = tmp1 + tmp2;
  58.138 +    tmp12 = tmp1 - tmp2;
  58.139 +    
  58.140 +    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  58.141 +    dataptr[DCTSIZE*4] = tmp10 - tmp11;
  58.142 +    
  58.143 +    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
  58.144 +    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  58.145 +    dataptr[DCTSIZE*6] = tmp13 - z1;
  58.146 +    
  58.147 +    /* Odd part */
  58.148 +
  58.149 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  58.150 +    tmp11 = tmp5 + tmp6;
  58.151 +    tmp12 = tmp6 + tmp7;
  58.152 +
  58.153 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  58.154 +    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
  58.155 +    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
  58.156 +    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
  58.157 +    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
  58.158 +
  58.159 +    z11 = tmp7 + z3;		/* phase 5 */
  58.160 +    z13 = tmp7 - z3;
  58.161 +
  58.162 +    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  58.163 +    dataptr[DCTSIZE*3] = z13 - z2;
  58.164 +    dataptr[DCTSIZE*1] = z11 + z4;
  58.165 +    dataptr[DCTSIZE*7] = z11 - z4;
  58.166 +
  58.167 +    dataptr++;			/* advance pointer to next column */
  58.168 +  }
  58.169 +}
  58.170 +
  58.171 +#endif /* DCT_FLOAT_SUPPORTED */
    59.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    59.2 +++ b/libs/libjpeg/jfdctfst.c	Sun Jun 07 17:25:49 2015 +0300
    59.3 @@ -0,0 +1,224 @@
    59.4 +/*
    59.5 + * jfdctfst.c
    59.6 + *
    59.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    59.8 + * This file is part of the Independent JPEG Group's software.
    59.9 + * For conditions of distribution and use, see the accompanying README file.
   59.10 + *
   59.11 + * This file contains a fast, not so accurate integer implementation of the
   59.12 + * forward DCT (Discrete Cosine Transform).
   59.13 + *
   59.14 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   59.15 + * on each column.  Direct algorithms are also available, but they are
   59.16 + * much more complex and seem not to be any faster when reduced to code.
   59.17 + *
   59.18 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   59.19 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   59.20 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   59.21 + * JPEG textbook (see REFERENCES section in file README).  The following code
   59.22 + * is based directly on figure 4-8 in P&M.
   59.23 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   59.24 + * possible to arrange the computation so that many of the multiplies are
   59.25 + * simple scalings of the final outputs.  These multiplies can then be
   59.26 + * folded into the multiplications or divisions by the JPEG quantization
   59.27 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   59.28 + * to be done in the DCT itself.
   59.29 + * The primary disadvantage of this method is that with fixed-point math,
   59.30 + * accuracy is lost due to imprecise representation of the scaled
   59.31 + * quantization values.  The smaller the quantization table entry, the less
   59.32 + * precise the scaled value, so this implementation does worse with high-
   59.33 + * quality-setting files than with low-quality ones.
   59.34 + */
   59.35 +
   59.36 +#define JPEG_INTERNALS
   59.37 +#include "jinclude.h"
   59.38 +#include "jpeglib.h"
   59.39 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   59.40 +
   59.41 +#ifdef DCT_IFAST_SUPPORTED
   59.42 +
   59.43 +
   59.44 +/*
   59.45 + * This module is specialized to the case DCTSIZE = 8.
   59.46 + */
   59.47 +
   59.48 +#if DCTSIZE != 8
   59.49 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   59.50 +#endif
   59.51 +
   59.52 +
   59.53 +/* Scaling decisions are generally the same as in the LL&M algorithm;
   59.54 + * see jfdctint.c for more details.  However, we choose to descale
   59.55 + * (right shift) multiplication products as soon as they are formed,
   59.56 + * rather than carrying additional fractional bits into subsequent additions.
   59.57 + * This compromises accuracy slightly, but it lets us save a few shifts.
   59.58 + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
   59.59 + * everywhere except in the multiplications proper; this saves a good deal
   59.60 + * of work on 16-bit-int machines.
   59.61 + *
   59.62 + * Again to save a few shifts, the intermediate results between pass 1 and
   59.63 + * pass 2 are not upscaled, but are represented only to integral precision.
   59.64 + *
   59.65 + * A final compromise is to represent the multiplicative constants to only
   59.66 + * 8 fractional bits, rather than 13.  This saves some shifting work on some
   59.67 + * machines, and may also reduce the cost of multiplication (since there
   59.68 + * are fewer one-bits in the constants).
   59.69 + */
   59.70 +
   59.71 +#define CONST_BITS  8
   59.72 +
   59.73 +
   59.74 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   59.75 + * causing a lot of useless floating-point operations at run time.
   59.76 + * To get around this we use the following pre-calculated constants.
   59.77 + * If you change CONST_BITS you may want to add appropriate values.
   59.78 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   59.79 + */
   59.80 +
   59.81 +#if CONST_BITS == 8
   59.82 +#define FIX_0_382683433  ((INT32)   98)		/* FIX(0.382683433) */
   59.83 +#define FIX_0_541196100  ((INT32)  139)		/* FIX(0.541196100) */
   59.84 +#define FIX_0_707106781  ((INT32)  181)		/* FIX(0.707106781) */
   59.85 +#define FIX_1_306562965  ((INT32)  334)		/* FIX(1.306562965) */
   59.86 +#else
   59.87 +#define FIX_0_382683433  FIX(0.382683433)
   59.88 +#define FIX_0_541196100  FIX(0.541196100)
   59.89 +#define FIX_0_707106781  FIX(0.707106781)
   59.90 +#define FIX_1_306562965  FIX(1.306562965)
   59.91 +#endif
   59.92 +
   59.93 +
   59.94 +/* We can gain a little more speed, with a further compromise in accuracy,
   59.95 + * by omitting the addition in a descaling shift.  This yields an incorrectly
   59.96 + * rounded result half the time...
   59.97 + */
   59.98 +
   59.99 +#ifndef USE_ACCURATE_ROUNDING
  59.100 +#undef DESCALE
  59.101 +#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
  59.102 +#endif
  59.103 +
  59.104 +
  59.105 +/* Multiply a DCTELEM variable by an INT32 constant, and immediately
  59.106 + * descale to yield a DCTELEM result.
  59.107 + */
  59.108 +
  59.109 +#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  59.110 +
  59.111 +
  59.112 +/*
  59.113 + * Perform the forward DCT on one block of samples.
  59.114 + */
  59.115 +
  59.116 +GLOBAL(void)
  59.117 +jpeg_fdct_ifast (DCTELEM * data)
  59.118 +{
  59.119 +  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  59.120 +  DCTELEM tmp10, tmp11, tmp12, tmp13;
  59.121 +  DCTELEM z1, z2, z3, z4, z5, z11, z13;
  59.122 +  DCTELEM *dataptr;
  59.123 +  int ctr;
  59.124 +  SHIFT_TEMPS
  59.125 +
  59.126 +  /* Pass 1: process rows. */
  59.127 +
  59.128 +  dataptr = data;
  59.129 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  59.130 +    tmp0 = dataptr[0] + dataptr[7];
  59.131 +    tmp7 = dataptr[0] - dataptr[7];
  59.132 +    tmp1 = dataptr[1] + dataptr[6];
  59.133 +    tmp6 = dataptr[1] - dataptr[6];
  59.134 +    tmp2 = dataptr[2] + dataptr[5];
  59.135 +    tmp5 = dataptr[2] - dataptr[5];
  59.136 +    tmp3 = dataptr[3] + dataptr[4];
  59.137 +    tmp4 = dataptr[3] - dataptr[4];
  59.138 +    
  59.139 +    /* Even part */
  59.140 +    
  59.141 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  59.142 +    tmp13 = tmp0 - tmp3;
  59.143 +    tmp11 = tmp1 + tmp2;
  59.144 +    tmp12 = tmp1 - tmp2;
  59.145 +    
  59.146 +    dataptr[0] = tmp10 + tmp11; /* phase 3 */
  59.147 +    dataptr[4] = tmp10 - tmp11;
  59.148 +    
  59.149 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  59.150 +    dataptr[2] = tmp13 + z1;	/* phase 5 */
  59.151 +    dataptr[6] = tmp13 - z1;
  59.152 +    
  59.153 +    /* Odd part */
  59.154 +
  59.155 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  59.156 +    tmp11 = tmp5 + tmp6;
  59.157 +    tmp12 = tmp6 + tmp7;
  59.158 +
  59.159 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  59.160 +    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  59.161 +    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  59.162 +    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  59.163 +    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  59.164 +
  59.165 +    z11 = tmp7 + z3;		/* phase 5 */
  59.166 +    z13 = tmp7 - z3;
  59.167 +
  59.168 +    dataptr[5] = z13 + z2;	/* phase 6 */
  59.169 +    dataptr[3] = z13 - z2;
  59.170 +    dataptr[1] = z11 + z4;
  59.171 +    dataptr[7] = z11 - z4;
  59.172 +
  59.173 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  59.174 +  }
  59.175 +
  59.176 +  /* Pass 2: process columns. */
  59.177 +
  59.178 +  dataptr = data;
  59.179 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  59.180 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  59.181 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  59.182 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  59.183 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  59.184 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  59.185 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  59.186 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  59.187 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  59.188 +    
  59.189 +    /* Even part */
  59.190 +    
  59.191 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  59.192 +    tmp13 = tmp0 - tmp3;
  59.193 +    tmp11 = tmp1 + tmp2;
  59.194 +    tmp12 = tmp1 - tmp2;
  59.195 +    
  59.196 +    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  59.197 +    dataptr[DCTSIZE*4] = tmp10 - tmp11;
  59.198 +    
  59.199 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  59.200 +    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  59.201 +    dataptr[DCTSIZE*6] = tmp13 - z1;
  59.202 +    
  59.203 +    /* Odd part */
  59.204 +
  59.205 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  59.206 +    tmp11 = tmp5 + tmp6;
  59.207 +    tmp12 = tmp6 + tmp7;
  59.208 +
  59.209 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  59.210 +    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  59.211 +    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  59.212 +    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  59.213 +    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  59.214 +
  59.215 +    z11 = tmp7 + z3;		/* phase 5 */
  59.216 +    z13 = tmp7 - z3;
  59.217 +
  59.218 +    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  59.219 +    dataptr[DCTSIZE*3] = z13 - z2;
  59.220 +    dataptr[DCTSIZE*1] = z11 + z4;
  59.221 +    dataptr[DCTSIZE*7] = z11 - z4;
  59.222 +
  59.223 +    dataptr++;			/* advance pointer to next column */
  59.224 +  }
  59.225 +}
  59.226 +
  59.227 +#endif /* DCT_IFAST_SUPPORTED */
    60.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    60.2 +++ b/libs/libjpeg/jfdctint.c	Sun Jun 07 17:25:49 2015 +0300
    60.3 @@ -0,0 +1,283 @@
    60.4 +/*
    60.5 + * jfdctint.c
    60.6 + *
    60.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    60.8 + * This file is part of the Independent JPEG Group's software.
    60.9 + * For conditions of distribution and use, see the accompanying README file.
   60.10 + *
   60.11 + * This file contains a slow-but-accurate integer implementation of the
   60.12 + * forward DCT (Discrete Cosine Transform).
   60.13 + *
   60.14 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   60.15 + * on each column.  Direct algorithms are also available, but they are
   60.16 + * much more complex and seem not to be any faster when reduced to code.
   60.17 + *
   60.18 + * This implementation is based on an algorithm described in
   60.19 + *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
   60.20 + *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
   60.21 + *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
   60.22 + * The primary algorithm described there uses 11 multiplies and 29 adds.
   60.23 + * We use their alternate method with 12 multiplies and 32 adds.
   60.24 + * The advantage of this method is that no data path contains more than one
   60.25 + * multiplication; this allows a very simple and accurate implementation in
   60.26 + * scaled fixed-point arithmetic, with a minimal number of shifts.
   60.27 + */
   60.28 +
   60.29 +#define JPEG_INTERNALS
   60.30 +#include "jinclude.h"
   60.31 +#include "jpeglib.h"
   60.32 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   60.33 +
   60.34 +#ifdef DCT_ISLOW_SUPPORTED
   60.35 +
   60.36 +
   60.37 +/*
   60.38 + * This module is specialized to the case DCTSIZE = 8.
   60.39 + */
   60.40 +
   60.41 +#if DCTSIZE != 8
   60.42 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   60.43 +#endif
   60.44 +
   60.45 +
   60.46 +/*
   60.47 + * The poop on this scaling stuff is as follows:
   60.48 + *
   60.49 + * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
   60.50 + * larger than the true DCT outputs.  The final outputs are therefore
   60.51 + * a factor of N larger than desired; since N=8 this can be cured by
   60.52 + * a simple right shift at the end of the algorithm.  The advantage of
   60.53 + * this arrangement is that we save two multiplications per 1-D DCT,
   60.54 + * because the y0 and y4 outputs need not be divided by sqrt(N).
   60.55 + * In the IJG code, this factor of 8 is removed by the quantization step
   60.56 + * (in jcdctmgr.c), NOT in this module.
   60.57 + *
   60.58 + * We have to do addition and subtraction of the integer inputs, which
   60.59 + * is no problem, and multiplication by fractional constants, which is
   60.60 + * a problem to do in integer arithmetic.  We multiply all the constants
   60.61 + * by CONST_SCALE and convert them to integer constants (thus retaining
   60.62 + * CONST_BITS bits of precision in the constants).  After doing a
   60.63 + * multiplication we have to divide the product by CONST_SCALE, with proper
   60.64 + * rounding, to produce the correct output.  This division can be done
   60.65 + * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
   60.66 + * as long as possible so that partial sums can be added together with
   60.67 + * full fractional precision.
   60.68 + *
   60.69 + * The outputs of the first pass are scaled up by PASS1_BITS bits so that
   60.70 + * they are represented to better-than-integral precision.  These outputs
   60.71 + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
   60.72 + * with the recommended scaling.  (For 12-bit sample data, the intermediate
   60.73 + * array is INT32 anyway.)
   60.74 + *
   60.75 + * To avoid overflow of the 32-bit intermediate results in pass 2, we must
   60.76 + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
   60.77 + * shows that the values given below are the most effective.
   60.78 + */
   60.79 +
   60.80 +#if BITS_IN_JSAMPLE == 8
   60.81 +#define CONST_BITS  13
   60.82 +#define PASS1_BITS  2
   60.83 +#else
   60.84 +#define CONST_BITS  13
   60.85 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   60.86 +#endif
   60.87 +
   60.88 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   60.89 + * causing a lot of useless floating-point operations at run time.
   60.90 + * To get around this we use the following pre-calculated constants.
   60.91 + * If you change CONST_BITS you may want to add appropriate values.
   60.92 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   60.93 + */
   60.94 +
   60.95 +#if CONST_BITS == 13
   60.96 +#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
   60.97 +#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
   60.98 +#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
   60.99 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
  60.100 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
  60.101 +#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
  60.102 +#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
  60.103 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
  60.104 +#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
  60.105 +#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
  60.106 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
  60.107 +#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
  60.108 +#else
  60.109 +#define FIX_0_298631336  FIX(0.298631336)
  60.110 +#define FIX_0_390180644  FIX(0.390180644)
  60.111 +#define FIX_0_541196100  FIX(0.541196100)
  60.112 +#define FIX_0_765366865  FIX(0.765366865)
  60.113 +#define FIX_0_899976223  FIX(0.899976223)
  60.114 +#define FIX_1_175875602  FIX(1.175875602)
  60.115 +#define FIX_1_501321110  FIX(1.501321110)
  60.116 +#define FIX_1_847759065  FIX(1.847759065)
  60.117 +#define FIX_1_961570560  FIX(1.961570560)
  60.118 +#define FIX_2_053119869  FIX(2.053119869)
  60.119 +#define FIX_2_562915447  FIX(2.562915447)
  60.120 +#define FIX_3_072711026  FIX(3.072711026)
  60.121 +#endif
  60.122 +
  60.123 +
  60.124 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  60.125 + * For 8-bit samples with the recommended scaling, all the variable
  60.126 + * and constant values involved are no more than 16 bits wide, so a
  60.127 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  60.128 + * For 12-bit samples, a full 32-bit multiplication will be needed.
  60.129 + */
  60.130 +
  60.131 +#if BITS_IN_JSAMPLE == 8
  60.132 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  60.133 +#else
  60.134 +#define MULTIPLY(var,const)  ((var) * (const))
  60.135 +#endif
  60.136 +
  60.137 +
  60.138 +/*
  60.139 + * Perform the forward DCT on one block of samples.
  60.140 + */
  60.141 +
  60.142 +GLOBAL(void)
  60.143 +jpeg_fdct_islow (DCTELEM * data)
  60.144 +{
  60.145 +  INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  60.146 +  INT32 tmp10, tmp11, tmp12, tmp13;
  60.147 +  INT32 z1, z2, z3, z4, z5;
  60.148 +  DCTELEM *dataptr;
  60.149 +  int ctr;
  60.150 +  SHIFT_TEMPS
  60.151 +
  60.152 +  /* Pass 1: process rows. */
  60.153 +  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
  60.154 +  /* furthermore, we scale the results by 2**PASS1_BITS. */
  60.155 +
  60.156 +  dataptr = data;
  60.157 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  60.158 +    tmp0 = dataptr[0] + dataptr[7];
  60.159 +    tmp7 = dataptr[0] - dataptr[7];
  60.160 +    tmp1 = dataptr[1] + dataptr[6];
  60.161 +    tmp6 = dataptr[1] - dataptr[6];
  60.162 +    tmp2 = dataptr[2] + dataptr[5];
  60.163 +    tmp5 = dataptr[2] - dataptr[5];
  60.164 +    tmp3 = dataptr[3] + dataptr[4];
  60.165 +    tmp4 = dataptr[3] - dataptr[4];
  60.166 +    
  60.167 +    /* Even part per LL&M figure 1 --- note that published figure is faulty;
  60.168 +     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
  60.169 +     */
  60.170 +    
  60.171 +    tmp10 = tmp0 + tmp3;
  60.172 +    tmp13 = tmp0 - tmp3;
  60.173 +    tmp11 = tmp1 + tmp2;
  60.174 +    tmp12 = tmp1 - tmp2;
  60.175 +    
  60.176 +    dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
  60.177 +    dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
  60.178 +    
  60.179 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
  60.180 +    dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
  60.181 +				   CONST_BITS-PASS1_BITS);
  60.182 +    dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
  60.183 +				   CONST_BITS-PASS1_BITS);
  60.184 +    
  60.185 +    /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
  60.186 +     * cK represents cos(K*pi/16).
  60.187 +     * i0..i3 in the paper are tmp4..tmp7 here.
  60.188 +     */
  60.189 +    
  60.190 +    z1 = tmp4 + tmp7;
  60.191 +    z2 = tmp5 + tmp6;
  60.192 +    z3 = tmp4 + tmp6;
  60.193 +    z4 = tmp5 + tmp7;
  60.194 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  60.195 +    
  60.196 +    tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  60.197 +    tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  60.198 +    tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  60.199 +    tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  60.200 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  60.201 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  60.202 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  60.203 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  60.204 +    
  60.205 +    z3 += z5;
  60.206 +    z4 += z5;
  60.207 +    
  60.208 +    dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
  60.209 +    dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
  60.210 +    dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
  60.211 +    dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
  60.212 +    
  60.213 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  60.214 +  }
  60.215 +
  60.216 +  /* Pass 2: process columns.
  60.217 +   * We remove the PASS1_BITS scaling, but leave the results scaled up
  60.218 +   * by an overall factor of 8.
  60.219 +   */
  60.220 +
  60.221 +  dataptr = data;
  60.222 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  60.223 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  60.224 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  60.225 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  60.226 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  60.227 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  60.228 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  60.229 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  60.230 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  60.231 +    
  60.232 +    /* Even part per LL&M figure 1 --- note that published figure is faulty;
  60.233 +     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
  60.234 +     */
  60.235 +    
  60.236 +    tmp10 = tmp0 + tmp3;
  60.237 +    tmp13 = tmp0 - tmp3;
  60.238 +    tmp11 = tmp1 + tmp2;
  60.239 +    tmp12 = tmp1 - tmp2;
  60.240 +    
  60.241 +    dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
  60.242 +    dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
  60.243 +    
  60.244 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
  60.245 +    dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
  60.246 +					   CONST_BITS+PASS1_BITS);
  60.247 +    dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
  60.248 +					   CONST_BITS+PASS1_BITS);
  60.249 +    
  60.250 +    /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
  60.251 +     * cK represents cos(K*pi/16).
  60.252 +     * i0..i3 in the paper are tmp4..tmp7 here.
  60.253 +     */
  60.254 +    
  60.255 +    z1 = tmp4 + tmp7;
  60.256 +    z2 = tmp5 + tmp6;
  60.257 +    z3 = tmp4 + tmp6;
  60.258 +    z4 = tmp5 + tmp7;
  60.259 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  60.260 +    
  60.261 +    tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  60.262 +    tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  60.263 +    tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  60.264 +    tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  60.265 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  60.266 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  60.267 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  60.268 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  60.269 +    
  60.270 +    z3 += z5;
  60.271 +    z4 += z5;
  60.272 +    
  60.273 +    dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
  60.274 +					   CONST_BITS+PASS1_BITS);
  60.275 +    dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
  60.276 +					   CONST_BITS+PASS1_BITS);
  60.277 +    dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
  60.278 +					   CONST_BITS+PASS1_BITS);
  60.279 +    dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
  60.280 +					   CONST_BITS+PASS1_BITS);
  60.281 +    
  60.282 +    dataptr++;			/* advance pointer to next column */
  60.283 +  }
  60.284 +}
  60.285 +
  60.286 +#endif /* DCT_ISLOW_SUPPORTED */
    61.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    61.2 +++ b/libs/libjpeg/jidctflt.c	Sun Jun 07 17:25:49 2015 +0300
    61.3 @@ -0,0 +1,242 @@
    61.4 +/*
    61.5 + * jidctflt.c
    61.6 + *
    61.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    61.8 + * This file is part of the Independent JPEG Group's software.
    61.9 + * For conditions of distribution and use, see the accompanying README file.
   61.10 + *
   61.11 + * This file contains a floating-point implementation of the
   61.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   61.13 + * must also perform dequantization of the input coefficients.
   61.14 + *
   61.15 + * This implementation should be more accurate than either of the integer
   61.16 + * IDCT implementations.  However, it may not give the same results on all
   61.17 + * machines because of differences in roundoff behavior.  Speed will depend
   61.18 + * on the hardware's floating point capacity.
   61.19 + *
   61.20 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   61.21 + * on each row (or vice versa, but it's more convenient to emit a row at
   61.22 + * a time).  Direct algorithms are also available, but they are much more
   61.23 + * complex and seem not to be any faster when reduced to code.
   61.24 + *
   61.25 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   61.26 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   61.27 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   61.28 + * JPEG textbook (see REFERENCES section in file README).  The following code
   61.29 + * is based directly on figure 4-8 in P&M.
   61.30 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   61.31 + * possible to arrange the computation so that many of the multiplies are
   61.32 + * simple scalings of the final outputs.  These multiplies can then be
   61.33 + * folded into the multiplications or divisions by the JPEG quantization
   61.34 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   61.35 + * to be done in the DCT itself.
   61.36 + * The primary disadvantage of this method is that with a fixed-point
   61.37 + * implementation, accuracy is lost due to imprecise representation of the
   61.38 + * scaled quantization values.  However, that problem does not arise if
   61.39 + * we use floating point arithmetic.
   61.40 + */
   61.41 +
   61.42 +#define JPEG_INTERNALS
   61.43 +#include "jinclude.h"
   61.44 +#include "jpeglib.h"
   61.45 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   61.46 +
   61.47 +#ifdef DCT_FLOAT_SUPPORTED
   61.48 +
   61.49 +
   61.50 +/*
   61.51 + * This module is specialized to the case DCTSIZE = 8.
   61.52 + */
   61.53 +
   61.54 +#if DCTSIZE != 8
   61.55 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   61.56 +#endif
   61.57 +
   61.58 +
   61.59 +/* Dequantize a coefficient by multiplying it by the multiplier-table
   61.60 + * entry; produce a float result.
   61.61 + */
   61.62 +
   61.63 +#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
   61.64 +
   61.65 +
   61.66 +/*
   61.67 + * Perform dequantization and inverse DCT on one block of coefficients.
   61.68 + */
   61.69 +
   61.70 +GLOBAL(void)
   61.71 +jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   61.72 +		 JCOEFPTR coef_block,
   61.73 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
   61.74 +{
   61.75 +  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   61.76 +  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
   61.77 +  FAST_FLOAT z5, z10, z11, z12, z13;
   61.78 +  JCOEFPTR inptr;
   61.79 +  FLOAT_MULT_TYPE * quantptr;
   61.80 +  FAST_FLOAT * wsptr;
   61.81 +  JSAMPROW outptr;
   61.82 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
   61.83 +  int ctr;
   61.84 +  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
   61.85 +  SHIFT_TEMPS
   61.86 +
   61.87 +  /* Pass 1: process columns from input, store into work array. */
   61.88 +
   61.89 +  inptr = coef_block;
   61.90 +  quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
   61.91 +  wsptr = workspace;
   61.92 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
   61.93 +    /* Due to quantization, we will usually find that many of the input
   61.94 +     * coefficients are zero, especially the AC terms.  We can exploit this
   61.95 +     * by short-circuiting the IDCT calculation for any column in which all
   61.96 +     * the AC terms are zero.  In that case each output is equal to the
   61.97 +     * DC coefficient (with scale factor as needed).
   61.98 +     * With typical images and quantization tables, half or more of the
   61.99 +     * column DCT calculations can be simplified this way.
  61.100 +     */
  61.101 +    
  61.102 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  61.103 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  61.104 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  61.105 +	inptr[DCTSIZE*7] == 0) {
  61.106 +      /* AC terms all zero */
  61.107 +      FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  61.108 +      
  61.109 +      wsptr[DCTSIZE*0] = dcval;
  61.110 +      wsptr[DCTSIZE*1] = dcval;
  61.111 +      wsptr[DCTSIZE*2] = dcval;
  61.112 +      wsptr[DCTSIZE*3] = dcval;
  61.113 +      wsptr[DCTSIZE*4] = dcval;
  61.114 +      wsptr[DCTSIZE*5] = dcval;
  61.115 +      wsptr[DCTSIZE*6] = dcval;
  61.116 +      wsptr[DCTSIZE*7] = dcval;
  61.117 +      
  61.118 +      inptr++;			/* advance pointers to next column */
  61.119 +      quantptr++;
  61.120 +      wsptr++;
  61.121 +      continue;
  61.122 +    }
  61.123 +    
  61.124 +    /* Even part */
  61.125 +
  61.126 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  61.127 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  61.128 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  61.129 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  61.130 +
  61.131 +    tmp10 = tmp0 + tmp2;	/* phase 3 */
  61.132 +    tmp11 = tmp0 - tmp2;
  61.133 +
  61.134 +    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
  61.135 +    tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
  61.136 +
  61.137 +    tmp0 = tmp10 + tmp13;	/* phase 2 */
  61.138 +    tmp3 = tmp10 - tmp13;
  61.139 +    tmp1 = tmp11 + tmp12;
  61.140 +    tmp2 = tmp11 - tmp12;
  61.141 +    
  61.142 +    /* Odd part */
  61.143 +
  61.144 +    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  61.145 +    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  61.146 +    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  61.147 +    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  61.148 +
  61.149 +    z13 = tmp6 + tmp5;		/* phase 6 */
  61.150 +    z10 = tmp6 - tmp5;
  61.151 +    z11 = tmp4 + tmp7;
  61.152 +    z12 = tmp4 - tmp7;
  61.153 +
  61.154 +    tmp7 = z11 + z13;		/* phase 5 */
  61.155 +    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
  61.156 +
  61.157 +    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  61.158 +    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  61.159 +    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  61.160 +
  61.161 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  61.162 +    tmp5 = tmp11 - tmp6;
  61.163 +    tmp4 = tmp10 + tmp5;
  61.164 +
  61.165 +    wsptr[DCTSIZE*0] = tmp0 + tmp7;
  61.166 +    wsptr[DCTSIZE*7] = tmp0 - tmp7;
  61.167 +    wsptr[DCTSIZE*1] = tmp1 + tmp6;
  61.168 +    wsptr[DCTSIZE*6] = tmp1 - tmp6;
  61.169 +    wsptr[DCTSIZE*2] = tmp2 + tmp5;
  61.170 +    wsptr[DCTSIZE*5] = tmp2 - tmp5;
  61.171 +    wsptr[DCTSIZE*4] = tmp3 + tmp4;
  61.172 +    wsptr[DCTSIZE*3] = tmp3 - tmp4;
  61.173 +
  61.174 +    inptr++;			/* advance pointers to next column */
  61.175 +    quantptr++;
  61.176 +    wsptr++;
  61.177 +  }
  61.178 +  
  61.179 +  /* Pass 2: process rows from work array, store into output array. */
  61.180 +  /* Note that we must descale the results by a factor of 8 == 2**3. */
  61.181 +
  61.182 +  wsptr = workspace;
  61.183 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  61.184 +    outptr = output_buf[ctr] + output_col;
  61.185 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  61.186 +     * However, the column calculation has created many nonzero AC terms, so
  61.187 +     * the simplification applies less often (typically 5% to 10% of the time).
  61.188 +     * And testing floats for zero is relatively expensive, so we don't bother.
  61.189 +     */
  61.190 +    
  61.191 +    /* Even part */
  61.192 +
  61.193 +    tmp10 = wsptr[0] + wsptr[4];
  61.194 +    tmp11 = wsptr[0] - wsptr[4];
  61.195 +
  61.196 +    tmp13 = wsptr[2] + wsptr[6];
  61.197 +    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
  61.198 +
  61.199 +    tmp0 = tmp10 + tmp13;
  61.200 +    tmp3 = tmp10 - tmp13;
  61.201 +    tmp1 = tmp11 + tmp12;
  61.202 +    tmp2 = tmp11 - tmp12;
  61.203 +
  61.204 +    /* Odd part */
  61.205 +
  61.206 +    z13 = wsptr[5] + wsptr[3];
  61.207 +    z10 = wsptr[5] - wsptr[3];
  61.208 +    z11 = wsptr[1] + wsptr[7];
  61.209 +    z12 = wsptr[1] - wsptr[7];
  61.210 +
  61.211 +    tmp7 = z11 + z13;
  61.212 +    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
  61.213 +
  61.214 +    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  61.215 +    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  61.216 +    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  61.217 +
  61.218 +    tmp6 = tmp12 - tmp7;
  61.219 +    tmp5 = tmp11 - tmp6;
  61.220 +    tmp4 = tmp10 + tmp5;
  61.221 +
  61.222 +    /* Final output stage: scale down by a factor of 8 and range-limit */
  61.223 +
  61.224 +    outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
  61.225 +			    & RANGE_MASK];
  61.226 +    outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
  61.227 +			    & RANGE_MASK];
  61.228 +    outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
  61.229 +			    & RANGE_MASK];
  61.230 +    outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
  61.231 +			    & RANGE_MASK];
  61.232 +    outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
  61.233 +			    & RANGE_MASK];
  61.234 +    outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
  61.235 +			    & RANGE_MASK];
  61.236 +    outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
  61.237 +			    & RANGE_MASK];
  61.238 +    outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
  61.239 +			    & RANGE_MASK];
  61.240 +    
  61.241 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  61.242 +  }
  61.243 +}
  61.244 +
  61.245 +#endif /* DCT_FLOAT_SUPPORTED */
    62.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    62.2 +++ b/libs/libjpeg/jidctfst.c	Sun Jun 07 17:25:49 2015 +0300
    62.3 @@ -0,0 +1,368 @@
    62.4 +/*
    62.5 + * jidctfst.c
    62.6 + *
    62.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    62.8 + * This file is part of the Independent JPEG Group's software.
    62.9 + * For conditions of distribution and use, see the accompanying README file.
   62.10 + *
   62.11 + * This file contains a fast, not so accurate integer implementation of the
   62.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   62.13 + * must also perform dequantization of the input coefficients.
   62.14 + *
   62.15 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   62.16 + * on each row (or vice versa, but it's more convenient to emit a row at
   62.17 + * a time).  Direct algorithms are also available, but they are much more
   62.18 + * complex and seem not to be any faster when reduced to code.
   62.19 + *
   62.20 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   62.21 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   62.22 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   62.23 + * JPEG textbook (see REFERENCES section in file README).  The following code
   62.24 + * is based directly on figure 4-8 in P&M.
   62.25 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   62.26 + * possible to arrange the computation so that many of the multiplies are
   62.27 + * simple scalings of the final outputs.  These multiplies can then be
   62.28 + * folded into the multiplications or divisions by the JPEG quantization
   62.29 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   62.30 + * to be done in the DCT itself.
   62.31 + * The primary disadvantage of this method is that with fixed-point math,
   62.32 + * accuracy is lost due to imprecise representation of the scaled
   62.33 + * quantization values.  The smaller the quantization table entry, the less
   62.34 + * precise the scaled value, so this implementation does worse with high-
   62.35 + * quality-setting files than with low-quality ones.
   62.36 + */
   62.37 +
   62.38 +#define JPEG_INTERNALS
   62.39 +#include "jinclude.h"
   62.40 +#include "jpeglib.h"
   62.41 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   62.42 +
   62.43 +#ifdef DCT_IFAST_SUPPORTED
   62.44 +
   62.45 +
   62.46 +/*
   62.47 + * This module is specialized to the case DCTSIZE = 8.
   62.48 + */
   62.49 +
   62.50 +#if DCTSIZE != 8
   62.51 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   62.52 +#endif
   62.53 +
   62.54 +
   62.55 +/* Scaling decisions are generally the same as in the LL&M algorithm;
   62.56 + * see jidctint.c for more details.  However, we choose to descale
   62.57 + * (right shift) multiplication products as soon as they are formed,
   62.58 + * rather than carrying additional fractional bits into subsequent additions.
   62.59 + * This compromises accuracy slightly, but it lets us save a few shifts.
   62.60 + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
   62.61 + * everywhere except in the multiplications proper; this saves a good deal
   62.62 + * of work on 16-bit-int machines.
   62.63 + *
   62.64 + * The dequantized coefficients are not integers because the AA&N scaling
   62.65 + * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
   62.66 + * so that the first and second IDCT rounds have the same input scaling.
   62.67 + * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
   62.68 + * avoid a descaling shift; this compromises accuracy rather drastically
   62.69 + * for small quantization table entries, but it saves a lot of shifts.
   62.70 + * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
   62.71 + * so we use a much larger scaling factor to preserve accuracy.
   62.72 + *
   62.73 + * A final compromise is to represent the multiplicative constants to only
   62.74 + * 8 fractional bits, rather than 13.  This saves some shifting work on some
   62.75 + * machines, and may also reduce the cost of multiplication (since there
   62.76 + * are fewer one-bits in the constants).
   62.77 + */
   62.78 +
   62.79 +#if BITS_IN_JSAMPLE == 8
   62.80 +#define CONST_BITS  8
   62.81 +#define PASS1_BITS  2
   62.82 +#else
   62.83 +#define CONST_BITS  8
   62.84 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   62.85 +#endif
   62.86 +
   62.87 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   62.88 + * causing a lot of useless floating-point operations at run time.
   62.89 + * To get around this we use the following pre-calculated constants.
   62.90 + * If you change CONST_BITS you may want to add appropriate values.
   62.91 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   62.92 + */
   62.93 +
   62.94 +#if CONST_BITS == 8
   62.95 +#define FIX_1_082392200  ((INT32)  277)		/* FIX(1.082392200) */
   62.96 +#define FIX_1_414213562  ((INT32)  362)		/* FIX(1.414213562) */
   62.97 +#define FIX_1_847759065  ((INT32)  473)		/* FIX(1.847759065) */
   62.98 +#define FIX_2_613125930  ((INT32)  669)		/* FIX(2.613125930) */
   62.99 +#else
  62.100 +#define FIX_1_082392200  FIX(1.082392200)
  62.101 +#define FIX_1_414213562  FIX(1.414213562)
  62.102 +#define FIX_1_847759065  FIX(1.847759065)
  62.103 +#define FIX_2_613125930  FIX(2.613125930)
  62.104 +#endif
  62.105 +
  62.106 +
  62.107 +/* We can gain a little more speed, with a further compromise in accuracy,
  62.108 + * by omitting the addition in a descaling shift.  This yields an incorrectly
  62.109 + * rounded result half the time...
  62.110 + */
  62.111 +
  62.112 +#ifndef USE_ACCURATE_ROUNDING
  62.113 +#undef DESCALE
  62.114 +#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
  62.115 +#endif
  62.116 +
  62.117 +
  62.118 +/* Multiply a DCTELEM variable by an INT32 constant, and immediately
  62.119 + * descale to yield a DCTELEM result.
  62.120 + */
  62.121 +
  62.122 +#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  62.123 +
  62.124 +
  62.125 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  62.126 + * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
  62.127 + * multiplication will do.  For 12-bit data, the multiplier table is
  62.128 + * declared INT32, so a 32-bit multiply will be used.
  62.129 + */
  62.130 +
  62.131 +#if BITS_IN_JSAMPLE == 8
  62.132 +#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
  62.133 +#else
  62.134 +#define DEQUANTIZE(coef,quantval)  \
  62.135 +	DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
  62.136 +#endif
  62.137 +
  62.138 +
  62.139 +/* Like DESCALE, but applies to a DCTELEM and produces an int.
  62.140 + * We assume that int right shift is unsigned if INT32 right shift is.
  62.141 + */
  62.142 +
  62.143 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
  62.144 +#define ISHIFT_TEMPS	DCTELEM ishift_temp;
  62.145 +#if BITS_IN_JSAMPLE == 8
  62.146 +#define DCTELEMBITS  16		/* DCTELEM may be 16 or 32 bits */
  62.147 +#else
  62.148 +#define DCTELEMBITS  32		/* DCTELEM must be 32 bits */
  62.149 +#endif
  62.150 +#define IRIGHT_SHIFT(x,shft)  \
  62.151 +    ((ishift_temp = (x)) < 0 ? \
  62.152 +     (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
  62.153 +     (ishift_temp >> (shft)))
  62.154 +#else
  62.155 +#define ISHIFT_TEMPS
  62.156 +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
  62.157 +#endif
  62.158 +
  62.159 +#ifdef USE_ACCURATE_ROUNDING
  62.160 +#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
  62.161 +#else
  62.162 +#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
  62.163 +#endif
  62.164 +
  62.165 +
  62.166 +/*
  62.167 + * Perform dequantization and inverse DCT on one block of coefficients.
  62.168 + */
  62.169 +
  62.170 +GLOBAL(void)
  62.171 +jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  62.172 +		 JCOEFPTR coef_block,
  62.173 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
  62.174 +{
  62.175 +  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  62.176 +  DCTELEM tmp10, tmp11, tmp12, tmp13;
  62.177 +  DCTELEM z5, z10, z11, z12, z13;
  62.178 +  JCOEFPTR inptr;
  62.179 +  IFAST_MULT_TYPE * quantptr;
  62.180 +  int * wsptr;
  62.181 +  JSAMPROW outptr;
  62.182 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  62.183 +  int ctr;
  62.184 +  int workspace[DCTSIZE2];	/* buffers data between passes */
  62.185 +  SHIFT_TEMPS			/* for DESCALE */
  62.186 +  ISHIFT_TEMPS			/* for IDESCALE */
  62.187 +
  62.188 +  /* Pass 1: process columns from input, store into work array. */
  62.189 +
  62.190 +  inptr = coef_block;
  62.191 +  quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
  62.192 +  wsptr = workspace;
  62.193 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
  62.194 +    /* Due to quantization, we will usually find that many of the input
  62.195 +     * coefficients are zero, especially the AC terms.  We can exploit this
  62.196 +     * by short-circuiting the IDCT calculation for any column in which all
  62.197 +     * the AC terms are zero.  In that case each output is equal to the
  62.198 +     * DC coefficient (with scale factor as needed).
  62.199 +     * With typical images and quantization tables, half or more of the
  62.200 +     * column DCT calculations can be simplified this way.
  62.201 +     */
  62.202 +    
  62.203 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  62.204 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  62.205 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  62.206 +	inptr[DCTSIZE*7] == 0) {
  62.207 +      /* AC terms all zero */
  62.208 +      int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  62.209 +
  62.210 +      wsptr[DCTSIZE*0] = dcval;
  62.211 +      wsptr[DCTSIZE*1] = dcval;
  62.212 +      wsptr[DCTSIZE*2] = dcval;
  62.213 +      wsptr[DCTSIZE*3] = dcval;
  62.214 +      wsptr[DCTSIZE*4] = dcval;
  62.215 +      wsptr[DCTSIZE*5] = dcval;
  62.216 +      wsptr[DCTSIZE*6] = dcval;
  62.217 +      wsptr[DCTSIZE*7] = dcval;
  62.218 +      
  62.219 +      inptr++;			/* advance pointers to next column */
  62.220 +      quantptr++;
  62.221 +      wsptr++;
  62.222 +      continue;
  62.223 +    }
  62.224 +    
  62.225 +    /* Even part */
  62.226 +
  62.227 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  62.228 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  62.229 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  62.230 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  62.231 +
  62.232 +    tmp10 = tmp0 + tmp2;	/* phase 3 */
  62.233 +    tmp11 = tmp0 - tmp2;
  62.234 +
  62.235 +    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
  62.236 +    tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
  62.237 +
  62.238 +    tmp0 = tmp10 + tmp13;	/* phase 2 */
  62.239 +    tmp3 = tmp10 - tmp13;
  62.240 +    tmp1 = tmp11 + tmp12;
  62.241 +    tmp2 = tmp11 - tmp12;
  62.242 +    
  62.243 +    /* Odd part */
  62.244 +
  62.245 +    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  62.246 +    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  62.247 +    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  62.248 +    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  62.249 +
  62.250 +    z13 = tmp6 + tmp5;		/* phase 6 */
  62.251 +    z10 = tmp6 - tmp5;
  62.252 +    z11 = tmp4 + tmp7;
  62.253 +    z12 = tmp4 - tmp7;
  62.254 +
  62.255 +    tmp7 = z11 + z13;		/* phase 5 */
  62.256 +    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  62.257 +
  62.258 +    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  62.259 +    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
  62.260 +    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
  62.261 +
  62.262 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  62.263 +    tmp5 = tmp11 - tmp6;
  62.264 +    tmp4 = tmp10 + tmp5;
  62.265 +
  62.266 +    wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
  62.267 +    wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
  62.268 +    wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
  62.269 +    wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
  62.270 +    wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
  62.271 +    wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
  62.272 +    wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
  62.273 +    wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
  62.274 +
  62.275 +    inptr++;			/* advance pointers to next column */
  62.276 +    quantptr++;
  62.277 +    wsptr++;
  62.278 +  }
  62.279 +  
  62.280 +  /* Pass 2: process rows from work array, store into output array. */
  62.281 +  /* Note that we must descale the results by a factor of 8 == 2**3, */
  62.282 +  /* and also undo the PASS1_BITS scaling. */
  62.283 +
  62.284 +  wsptr = workspace;
  62.285 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  62.286 +    outptr = output_buf[ctr] + output_col;
  62.287 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  62.288 +     * However, the column calculation has created many nonzero AC terms, so
  62.289 +     * the simplification applies less often (typically 5% to 10% of the time).
  62.290 +     * On machines with very fast multiplication, it's possible that the
  62.291 +     * test takes more time than it's worth.  In that case this section
  62.292 +     * may be commented out.
  62.293 +     */
  62.294 +    
  62.295 +#ifndef NO_ZERO_ROW_TEST
  62.296 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  62.297 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  62.298 +      /* AC terms all zero */
  62.299 +      JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
  62.300 +				  & RANGE_MASK];
  62.301 +      
  62.302 +      outptr[0] = dcval;
  62.303 +      outptr[1] = dcval;
  62.304 +      outptr[2] = dcval;
  62.305 +      outptr[3] = dcval;
  62.306 +      outptr[4] = dcval;
  62.307 +      outptr[5] = dcval;
  62.308 +      outptr[6] = dcval;
  62.309 +      outptr[7] = dcval;
  62.310 +
  62.311 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  62.312 +      continue;
  62.313 +    }
  62.314 +#endif
  62.315 +    
  62.316 +    /* Even part */
  62.317 +
  62.318 +    tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
  62.319 +    tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
  62.320 +
  62.321 +    tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
  62.322 +    tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
  62.323 +	    - tmp13;
  62.324 +
  62.325 +    tmp0 = tmp10 + tmp13;
  62.326 +    tmp3 = tmp10 - tmp13;
  62.327 +    tmp1 = tmp11 + tmp12;
  62.328 +    tmp2 = tmp11 - tmp12;
  62.329 +
  62.330 +    /* Odd part */
  62.331 +
  62.332 +    z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
  62.333 +    z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
  62.334 +    z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
  62.335 +    z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
  62.336 +
  62.337 +    tmp7 = z11 + z13;		/* phase 5 */
  62.338 +    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  62.339 +
  62.340 +    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  62.341 +    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
  62.342 +    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
  62.343 +
  62.344 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  62.345 +    tmp5 = tmp11 - tmp6;
  62.346 +    tmp4 = tmp10 + tmp5;
  62.347 +
  62.348 +    /* Final output stage: scale down by a factor of 8 and range-limit */
  62.349 +
  62.350 +    outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  62.351 +			    & RANGE_MASK];
  62.352 +    outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  62.353 +			    & RANGE_MASK];
  62.354 +    outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  62.355 +			    & RANGE_MASK];
  62.356 +    outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  62.357 +			    & RANGE_MASK];
  62.358 +    outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  62.359 +			    & RANGE_MASK];
  62.360 +    outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  62.361 +			    & RANGE_MASK];
  62.362 +    outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  62.363 +			    & RANGE_MASK];
  62.364 +    outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  62.365 +			    & RANGE_MASK];
  62.366 +
  62.367 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  62.368 +  }
  62.369 +}
  62.370 +
  62.371 +#endif /* DCT_IFAST_SUPPORTED */
    63.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    63.2 +++ b/libs/libjpeg/jidctint.c	Sun Jun 07 17:25:49 2015 +0300
    63.3 @@ -0,0 +1,389 @@
    63.4 +/*
    63.5 + * jidctint.c
    63.6 + *
    63.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    63.8 + * This file is part of the Independent JPEG Group's software.
    63.9 + * For conditions of distribution and use, see the accompanying README file.
   63.10 + *
   63.11 + * This file contains a slow-but-accurate integer implementation of the
   63.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   63.13 + * must also perform dequantization of the input coefficients.
   63.14 + *
   63.15 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   63.16 + * on each row (or vice versa, but it's more convenient to emit a row at
   63.17 + * a time).  Direct algorithms are also available, but they are much more
   63.18 + * complex and seem not to be any faster when reduced to code.
   63.19 + *
   63.20 + * This implementation is based on an algorithm described in
   63.21 + *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
   63.22 + *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
   63.23 + *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
   63.24 + * The primary algorithm described there uses 11 multiplies and 29 adds.
   63.25 + * We use their alternate method with 12 multiplies and 32 adds.
   63.26 + * The advantage of this method is that no data path contains more than one
   63.27 + * multiplication; this allows a very simple and accurate implementation in
   63.28 + * scaled fixed-point arithmetic, with a minimal number of shifts.
   63.29 + */
   63.30 +
   63.31 +#define JPEG_INTERNALS
   63.32 +#include "jinclude.h"
   63.33 +#include "jpeglib.h"
   63.34 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   63.35 +
   63.36 +#ifdef DCT_ISLOW_SUPPORTED
   63.37 +
   63.38 +
   63.39 +/*
   63.40 + * This module is specialized to the case DCTSIZE = 8.
   63.41 + */
   63.42 +
   63.43 +#if DCTSIZE != 8
   63.44 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   63.45 +#endif
   63.46 +
   63.47 +
   63.48 +/*
   63.49 + * The poop on this scaling stuff is as follows:
   63.50 + *
   63.51 + * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
   63.52 + * larger than the true IDCT outputs.  The final outputs are therefore
   63.53 + * a factor of N larger than desired; since N=8 this can be cured by
   63.54 + * a simple right shift at the end of the algorithm.  The advantage of
   63.55 + * this arrangement is that we save two multiplications per 1-D IDCT,
   63.56 + * because the y0 and y4 inputs need not be divided by sqrt(N).
   63.57 + *
   63.58 + * We have to do addition and subtraction of the integer inputs, which
   63.59 + * is no problem, and multiplication by fractional constants, which is
   63.60 + * a problem to do in integer arithmetic.  We multiply all the constants
   63.61 + * by CONST_SCALE and convert them to integer constants (thus retaining
   63.62 + * CONST_BITS bits of precision in the constants).  After doing a
   63.63 + * multiplication we have to divide the product by CONST_SCALE, with proper
   63.64 + * rounding, to produce the correct output.  This division can be done
   63.65 + * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
   63.66 + * as long as possible so that partial sums can be added together with
   63.67 + * full fractional precision.
   63.68 + *
   63.69 + * The outputs of the first pass are scaled up by PASS1_BITS bits so that
   63.70 + * they are represented to better-than-integral precision.  These outputs
   63.71 + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
   63.72 + * with the recommended scaling.  (To scale up 12-bit sample data further, an
   63.73 + * intermediate INT32 array would be needed.)
   63.74 + *
   63.75 + * To avoid overflow of the 32-bit intermediate results in pass 2, we must
   63.76 + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
   63.77 + * shows that the values given below are the most effective.
   63.78 + */
   63.79 +
   63.80 +#if BITS_IN_JSAMPLE == 8
   63.81 +#define CONST_BITS  13
   63.82 +#define PASS1_BITS  2
   63.83 +#else
   63.84 +#define CONST_BITS  13
   63.85 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   63.86 +#endif
   63.87 +
   63.88 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   63.89 + * causing a lot of useless floating-point operations at run time.
   63.90 + * To get around this we use the following pre-calculated constants.
   63.91 + * If you change CONST_BITS you may want to add appropriate values.
   63.92 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   63.93 + */
   63.94 +
   63.95 +#if CONST_BITS == 13
   63.96 +#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
   63.97 +#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
   63.98 +#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
   63.99 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
  63.100 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
  63.101 +#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
  63.102 +#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
  63.103 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
  63.104 +#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
  63.105 +#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
  63.106 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
  63.107 +#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
  63.108 +#else
  63.109 +#define FIX_0_298631336  FIX(0.298631336)
  63.110 +#define FIX_0_390180644  FIX(0.390180644)
  63.111 +#define FIX_0_541196100  FIX(0.541196100)
  63.112 +#define FIX_0_765366865  FIX(0.765366865)
  63.113 +#define FIX_0_899976223  FIX(0.899976223)
  63.114 +#define FIX_1_175875602  FIX(1.175875602)
  63.115 +#define FIX_1_501321110  FIX(1.501321110)
  63.116 +#define FIX_1_847759065  FIX(1.847759065)
  63.117 +#define FIX_1_961570560  FIX(1.961570560)
  63.118 +#define FIX_2_053119869  FIX(2.053119869)
  63.119 +#define FIX_2_562915447  FIX(2.562915447)
  63.120 +#define FIX_3_072711026  FIX(3.072711026)
  63.121 +#endif
  63.122 +
  63.123 +
  63.124 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  63.125 + * For 8-bit samples with the recommended scaling, all the variable
  63.126 + * and constant values involved are no more than 16 bits wide, so a
  63.127 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  63.128 + * For 12-bit samples, a full 32-bit multiplication will be needed.
  63.129 + */
  63.130 +
  63.131 +#if BITS_IN_JSAMPLE == 8
  63.132 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  63.133 +#else
  63.134 +#define MULTIPLY(var,const)  ((var) * (const))
  63.135 +#endif
  63.136 +
  63.137 +
  63.138 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  63.139 + * entry; produce an int result.  In this module, both inputs and result
  63.140 + * are 16 bits or less, so either int or short multiply will work.
  63.141 + */
  63.142 +
  63.143 +#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  63.144 +
  63.145 +
  63.146 +/*
  63.147 + * Perform dequantization and inverse DCT on one block of coefficients.
  63.148 + */
  63.149 +
  63.150 +GLOBAL(void)
  63.151 +jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  63.152 +		 JCOEFPTR coef_block,
  63.153 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
  63.154 +{
  63.155 +  INT32 tmp0, tmp1, tmp2, tmp3;
  63.156 +  INT32 tmp10, tmp11, tmp12, tmp13;
  63.157 +  INT32 z1, z2, z3, z4, z5;
  63.158 +  JCOEFPTR inptr;
  63.159 +  ISLOW_MULT_TYPE * quantptr;
  63.160 +  int * wsptr;
  63.161 +  JSAMPROW outptr;
  63.162 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  63.163 +  int ctr;
  63.164 +  int workspace[DCTSIZE2];	/* buffers data between passes */
  63.165 +  SHIFT_TEMPS
  63.166 +
  63.167 +  /* Pass 1: process columns from input, store into work array. */
  63.168 +  /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  63.169 +  /* furthermore, we scale the results by 2**PASS1_BITS. */
  63.170 +
  63.171 +  inptr = coef_block;
  63.172 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  63.173 +  wsptr = workspace;
  63.174 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
  63.175 +    /* Due to quantization, we will usually find that many of the input
  63.176 +     * coefficients are zero, especially the AC terms.  We can exploit this
  63.177 +     * by short-circuiting the IDCT calculation for any column in which all
  63.178 +     * the AC terms are zero.  In that case each output is equal to the
  63.179 +     * DC coefficient (with scale factor as needed).
  63.180 +     * With typical images and quantization tables, half or more of the
  63.181 +     * column DCT calculations can be simplified this way.
  63.182 +     */
  63.183 +    
  63.184 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  63.185 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  63.186 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  63.187 +	inptr[DCTSIZE*7] == 0) {
  63.188 +      /* AC terms all zero */
  63.189 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  63.190 +      
  63.191 +      wsptr[DCTSIZE*0] = dcval;
  63.192 +      wsptr[DCTSIZE*1] = dcval;
  63.193 +      wsptr[DCTSIZE*2] = dcval;
  63.194 +      wsptr[DCTSIZE*3] = dcval;
  63.195 +      wsptr[DCTSIZE*4] = dcval;
  63.196 +      wsptr[DCTSIZE*5] = dcval;
  63.197 +      wsptr[DCTSIZE*6] = dcval;
  63.198 +      wsptr[DCTSIZE*7] = dcval;
  63.199 +      
  63.200 +      inptr++;			/* advance pointers to next column */
  63.201 +      quantptr++;
  63.202 +      wsptr++;
  63.203 +      continue;
  63.204 +    }
  63.205 +    
  63.206 +    /* Even part: reverse the even part of the forward DCT. */
  63.207 +    /* The rotator is sqrt(2)*c(-6). */
  63.208 +    
  63.209 +    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  63.210 +    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  63.211 +    
  63.212 +    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  63.213 +    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  63.214 +    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  63.215 +    
  63.216 +    z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  63.217 +    z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  63.218 +
  63.219 +    tmp0 = (z2 + z3) << CONST_BITS;
  63.220 +    tmp1 = (z2 - z3) << CONST_BITS;
  63.221 +    
  63.222 +    tmp10 = tmp0 + tmp3;
  63.223 +    tmp13 = tmp0 - tmp3;
  63.224 +    tmp11 = tmp1 + tmp2;
  63.225 +    tmp12 = tmp1 - tmp2;
  63.226 +    
  63.227 +    /* Odd part per figure 8; the matrix is unitary and hence its
  63.228 +     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  63.229 +     */
  63.230 +    
  63.231 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  63.232 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  63.233 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  63.234 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  63.235 +    
  63.236 +    z1 = tmp0 + tmp3;
  63.237 +    z2 = tmp1 + tmp2;
  63.238 +    z3 = tmp0 + tmp2;
  63.239 +    z4 = tmp1 + tmp3;
  63.240 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  63.241 +    
  63.242 +    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  63.243 +    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  63.244 +    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  63.245 +    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  63.246 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  63.247 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  63.248 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  63.249 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  63.250 +    
  63.251 +    z3 += z5;
  63.252 +    z4 += z5;
  63.253 +    
  63.254 +    tmp0 += z1 + z3;
  63.255 +    tmp1 += z2 + z4;
  63.256 +    tmp2 += z2 + z3;
  63.257 +    tmp3 += z1 + z4;
  63.258 +    
  63.259 +    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  63.260 +    
  63.261 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  63.262 +    wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  63.263 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  63.264 +    wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  63.265 +    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  63.266 +    wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  63.267 +    wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  63.268 +    wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  63.269 +    
  63.270 +    inptr++;			/* advance pointers to next column */
  63.271 +    quantptr++;
  63.272 +    wsptr++;
  63.273 +  }
  63.274 +  
  63.275 +  /* Pass 2: process rows from work array, store into output array. */
  63.276 +  /* Note that we must descale the results by a factor of 8 == 2**3, */
  63.277 +  /* and also undo the PASS1_BITS scaling. */
  63.278 +
  63.279 +  wsptr = workspace;
  63.280 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  63.281 +    outptr = output_buf[ctr] + output_col;
  63.282 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  63.283 +     * However, the column calculation has created many nonzero AC terms, so
  63.284 +     * the simplification applies less often (typically 5% to 10% of the time).
  63.285 +     * On machines with very fast multiplication, it's possible that the
  63.286 +     * test takes more time than it's worth.  In that case this section
  63.287 +     * may be commented out.
  63.288 +     */
  63.289 +    
  63.290 +#ifndef NO_ZERO_ROW_TEST
  63.291 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  63.292 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  63.293 +      /* AC terms all zero */
  63.294 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  63.295 +				  & RANGE_MASK];
  63.296 +      
  63.297 +      outptr[0] = dcval;
  63.298 +      outptr[1] = dcval;
  63.299 +      outptr[2] = dcval;
  63.300 +      outptr[3] = dcval;
  63.301 +      outptr[4] = dcval;
  63.302 +      outptr[5] = dcval;
  63.303 +      outptr[6] = dcval;
  63.304 +      outptr[7] = dcval;
  63.305 +
  63.306 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  63.307 +      continue;
  63.308 +    }
  63.309 +#endif
  63.310 +    
  63.311 +    /* Even part: reverse the even part of the forward DCT. */
  63.312 +    /* The rotator is sqrt(2)*c(-6). */
  63.313 +    
  63.314 +    z2 = (INT32) wsptr[2];
  63.315 +    z3 = (INT32) wsptr[6];
  63.316 +    
  63.317 +    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  63.318 +    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  63.319 +    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  63.320 +    
  63.321 +    tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
  63.322 +    tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
  63.323 +    
  63.324 +    tmp10 = tmp0 + tmp3;
  63.325 +    tmp13 = tmp0 - tmp3;
  63.326 +    tmp11 = tmp1 + tmp2;
  63.327 +    tmp12 = tmp1 - tmp2;
  63.328 +    
  63.329 +    /* Odd part per figure 8; the matrix is unitary and hence its
  63.330 +     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  63.331 +     */
  63.332 +    
  63.333 +    tmp0 = (INT32) wsptr[7];
  63.334 +    tmp1 = (INT32) wsptr[5];
  63.335 +    tmp2 = (INT32) wsptr[3];
  63.336 +    tmp3 = (INT32) wsptr[1];
  63.337 +    
  63.338 +    z1 = tmp0 + tmp3;
  63.339 +    z2 = tmp1 + tmp2;
  63.340 +    z3 = tmp0 + tmp2;
  63.341 +    z4 = tmp1 + tmp3;
  63.342 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  63.343 +    
  63.344 +    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  63.345 +    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  63.346 +    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  63.347 +    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  63.348 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  63.349 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  63.350 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  63.351 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  63.352 +    
  63.353 +    z3 += z5;
  63.354 +    z4 += z5;
  63.355 +    
  63.356 +    tmp0 += z1 + z3;
  63.357 +    tmp1 += z2 + z4;
  63.358 +    tmp2 += z2 + z3;
  63.359 +    tmp3 += z1 + z4;
  63.360 +    
  63.361 +    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  63.362 +    
  63.363 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
  63.364 +					  CONST_BITS+PASS1_BITS+3)
  63.365 +			    & RANGE_MASK];
  63.366 +    outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
  63.367 +					  CONST_BITS+PASS1_BITS+3)
  63.368 +			    & RANGE_MASK];
  63.369 +    outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
  63.370 +					  CONST_BITS+PASS1_BITS+3)
  63.371 +			    & RANGE_MASK];
  63.372 +    outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
  63.373 +					  CONST_BITS+PASS1_BITS+3)
  63.374 +			    & RANGE_MASK];
  63.375 +    outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
  63.376 +					  CONST_BITS+PASS1_BITS+3)
  63.377 +			    & RANGE_MASK];
  63.378 +    outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
  63.379 +					  CONST_BITS+PASS1_BITS+3)
  63.380 +			    & RANGE_MASK];
  63.381 +    outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
  63.382 +					  CONST_BITS+PASS1_BITS+3)
  63.383 +			    & RANGE_MASK];
  63.384 +    outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
  63.385 +					  CONST_BITS+PASS1_BITS+3)
  63.386 +			    & RANGE_MASK];
  63.387 +    
  63.388 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  63.389 +  }
  63.390 +}
  63.391 +
  63.392 +#endif /* DCT_ISLOW_SUPPORTED */
    64.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    64.2 +++ b/libs/libjpeg/jidctred.c	Sun Jun 07 17:25:49 2015 +0300
    64.3 @@ -0,0 +1,398 @@
    64.4 +/*
    64.5 + * jidctred.c
    64.6 + *
    64.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    64.8 + * This file is part of the Independent JPEG Group's software.
    64.9 + * For conditions of distribution and use, see the accompanying README file.
   64.10 + *
   64.11 + * This file contains inverse-DCT routines that produce reduced-size output:
   64.12 + * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
   64.13 + *
   64.14 + * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
   64.15 + * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
   64.16 + * with an 8-to-4 step that produces the four averages of two adjacent outputs
   64.17 + * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
   64.18 + * These steps were derived by computing the corresponding values at the end
   64.19 + * of the normal LL&M code, then simplifying as much as possible.
   64.20 + *
   64.21 + * 1x1 is trivial: just take the DC coefficient divided by 8.
   64.22 + *
   64.23 + * See jidctint.c for additional comments.
   64.24 + */
   64.25 +
   64.26 +#define JPEG_INTERNALS
   64.27 +#include "jinclude.h"
   64.28 +#include "jpeglib.h"
   64.29 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   64.30 +
   64.31 +#ifdef IDCT_SCALING_SUPPORTED
   64.32 +
   64.33 +
   64.34 +/*
   64.35 + * This module is specialized to the case DCTSIZE = 8.
   64.36 + */
   64.37 +
   64.38 +#if DCTSIZE != 8
   64.39 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   64.40 +#endif
   64.41 +
   64.42 +
   64.43 +/* Scaling is the same as in jidctint.c. */
   64.44 +
   64.45 +#if BITS_IN_JSAMPLE == 8
   64.46 +#define CONST_BITS  13
   64.47 +#define PASS1_BITS  2
   64.48 +#else
   64.49 +#define CONST_BITS  13
   64.50 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   64.51 +#endif
   64.52 +
   64.53 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   64.54 + * causing a lot of useless floating-point operations at run time.
   64.55 + * To get around this we use the following pre-calculated constants.
   64.56 + * If you change CONST_BITS you may want to add appropriate values.
   64.57 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   64.58 + */
   64.59 +
   64.60 +#if CONST_BITS == 13
   64.61 +#define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */
   64.62 +#define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */
   64.63 +#define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */
   64.64 +#define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */
   64.65 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
   64.66 +#define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */
   64.67 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
   64.68 +#define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */
   64.69 +#define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */
   64.70 +#define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */
   64.71 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
   64.72 +#define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */
   64.73 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
   64.74 +#define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */
   64.75 +#else
   64.76 +#define FIX_0_211164243  FIX(0.211164243)
   64.77 +#define FIX_0_509795579  FIX(0.509795579)
   64.78 +#define FIX_0_601344887  FIX(0.601344887)
   64.79 +#define FIX_0_720959822  FIX(0.720959822)
   64.80 +#define FIX_0_765366865  FIX(0.765366865)
   64.81 +#define FIX_0_850430095  FIX(0.850430095)
   64.82 +#define FIX_0_899976223  FIX(0.899976223)
   64.83 +#define FIX_1_061594337  FIX(1.061594337)
   64.84 +#define FIX_1_272758580  FIX(1.272758580)
   64.85 +#define FIX_1_451774981  FIX(1.451774981)
   64.86 +#define FIX_1_847759065  FIX(1.847759065)
   64.87 +#define FIX_2_172734803  FIX(2.172734803)
   64.88 +#define FIX_2_562915447  FIX(2.562915447)
   64.89 +#define FIX_3_624509785  FIX(3.624509785)
   64.90 +#endif
   64.91 +
   64.92 +
   64.93 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
   64.94 + * For 8-bit samples with the recommended scaling, all the variable
   64.95 + * and constant values involved are no more than 16 bits wide, so a
   64.96 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
   64.97 + * For 12-bit samples, a full 32-bit multiplication will be needed.
   64.98 + */
   64.99 +
  64.100 +#if BITS_IN_JSAMPLE == 8
  64.101 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  64.102 +#else
  64.103 +#define MULTIPLY(var,const)  ((var) * (const))
  64.104 +#endif
  64.105 +
  64.106 +
  64.107 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  64.108 + * entry; produce an int result.  In this module, both inputs and result
  64.109 + * are 16 bits or less, so either int or short multiply will work.
  64.110 + */
  64.111 +
  64.112 +#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  64.113 +
  64.114 +
  64.115 +/*
  64.116 + * Perform dequantization and inverse DCT on one block of coefficients,
  64.117 + * producing a reduced-size 4x4 output block.
  64.118 + */
  64.119 +
  64.120 +GLOBAL(void)
  64.121 +jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  64.122 +	       JCOEFPTR coef_block,
  64.123 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  64.124 +{
  64.125 +  INT32 tmp0, tmp2, tmp10, tmp12;
  64.126 +  INT32 z1, z2, z3, z4;
  64.127 +  JCOEFPTR inptr;
  64.128 +  ISLOW_MULT_TYPE * quantptr;
  64.129 +  int * wsptr;
  64.130 +  JSAMPROW outptr;
  64.131 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  64.132 +  int ctr;
  64.133 +  int workspace[DCTSIZE*4];	/* buffers data between passes */
  64.134 +  SHIFT_TEMPS
  64.135 +
  64.136 +  /* Pass 1: process columns from input, store into work array. */
  64.137 +
  64.138 +  inptr = coef_block;
  64.139 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  64.140 +  wsptr = workspace;
  64.141 +  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  64.142 +    /* Don't bother to process column 4, because second pass won't use it */
  64.143 +    if (ctr == DCTSIZE-4)
  64.144 +      continue;
  64.145 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  64.146 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
  64.147 +	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
  64.148 +      /* AC terms all zero; we need not examine term 4 for 4x4 output */
  64.149 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  64.150 +      
  64.151 +      wsptr[DCTSIZE*0] = dcval;
  64.152 +      wsptr[DCTSIZE*1] = dcval;
  64.153 +      wsptr[DCTSIZE*2] = dcval;
  64.154 +      wsptr[DCTSIZE*3] = dcval;
  64.155 +      
  64.156 +      continue;
  64.157 +    }
  64.158 +    
  64.159 +    /* Even part */
  64.160 +    
  64.161 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  64.162 +    tmp0 <<= (CONST_BITS+1);
  64.163 +    
  64.164 +    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  64.165 +    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  64.166 +
  64.167 +    tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  64.168 +    
  64.169 +    tmp10 = tmp0 + tmp2;
  64.170 +    tmp12 = tmp0 - tmp2;
  64.171 +    
  64.172 +    /* Odd part */
  64.173 +    
  64.174 +    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  64.175 +    z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  64.176 +    z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  64.177 +    z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  64.178 +    
  64.179 +    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  64.180 +	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  64.181 +	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  64.182 +	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  64.183 +    
  64.184 +    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  64.185 +	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  64.186 +	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  64.187 +	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  64.188 +
  64.189 +    /* Final output stage */
  64.190 +    
  64.191 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  64.192 +    wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  64.193 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  64.194 +    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  64.195 +  }
  64.196 +  
  64.197 +  /* Pass 2: process 4 rows from work array, store into output array. */
  64.198 +
  64.199 +  wsptr = workspace;
  64.200 +  for (ctr = 0; ctr < 4; ctr++) {
  64.201 +    outptr = output_buf[ctr] + output_col;
  64.202 +    /* It's not clear whether a zero row test is worthwhile here ... */
  64.203 +
  64.204 +#ifndef NO_ZERO_ROW_TEST
  64.205 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
  64.206 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  64.207 +      /* AC terms all zero */
  64.208 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  64.209 +				  & RANGE_MASK];
  64.210 +      
  64.211 +      outptr[0] = dcval;
  64.212 +      outptr[1] = dcval;
  64.213 +      outptr[2] = dcval;
  64.214 +      outptr[3] = dcval;
  64.215 +      
  64.216 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  64.217 +      continue;
  64.218 +    }
  64.219 +#endif
  64.220 +    
  64.221 +    /* Even part */
  64.222 +    
  64.223 +    tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
  64.224 +    
  64.225 +    tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
  64.226 +	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
  64.227 +    
  64.228 +    tmp10 = tmp0 + tmp2;
  64.229 +    tmp12 = tmp0 - tmp2;
  64.230 +    
  64.231 +    /* Odd part */
  64.232 +    
  64.233 +    z1 = (INT32) wsptr[7];
  64.234 +    z2 = (INT32) wsptr[5];
  64.235 +    z3 = (INT32) wsptr[3];
  64.236 +    z4 = (INT32) wsptr[1];
  64.237 +    
  64.238 +    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  64.239 +	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  64.240 +	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  64.241 +	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  64.242 +    
  64.243 +    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  64.244 +	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  64.245 +	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  64.246 +	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  64.247 +
  64.248 +    /* Final output stage */
  64.249 +    
  64.250 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  64.251 +					  CONST_BITS+PASS1_BITS+3+1)
  64.252 +			    & RANGE_MASK];
  64.253 +    outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  64.254 +					  CONST_BITS+PASS1_BITS+3+1)
  64.255 +			    & RANGE_MASK];
  64.256 +    outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  64.257 +					  CONST_BITS+PASS1_BITS+3+1)
  64.258 +			    & RANGE_MASK];
  64.259 +    outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  64.260 +					  CONST_BITS+PASS1_BITS+3+1)
  64.261 +			    & RANGE_MASK];
  64.262 +    
  64.263 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  64.264 +  }
  64.265 +}
  64.266 +
  64.267 +
  64.268 +/*
  64.269 + * Perform dequantization and inverse DCT on one block of coefficients,
  64.270 + * producing a reduced-size 2x2 output block.
  64.271 + */
  64.272 +
  64.273 +GLOBAL(void)
  64.274 +jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  64.275 +	       JCOEFPTR coef_block,
  64.276 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  64.277 +{
  64.278 +  INT32 tmp0, tmp10, z1;
  64.279 +  JCOEFPTR inptr;
  64.280 +  ISLOW_MULT_TYPE * quantptr;
  64.281 +  int * wsptr;
  64.282 +  JSAMPROW outptr;
  64.283 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  64.284 +  int ctr;
  64.285 +  int workspace[DCTSIZE*2];	/* buffers data between passes */
  64.286 +  SHIFT_TEMPS
  64.287 +
  64.288 +  /* Pass 1: process columns from input, store into work array. */
  64.289 +
  64.290 +  inptr = coef_block;
  64.291 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  64.292 +  wsptr = workspace;
  64.293 +  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  64.294 +    /* Don't bother to process columns 2,4,6 */
  64.295 +    if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  64.296 +      continue;
  64.297 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
  64.298 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
  64.299 +      /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  64.300 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  64.301 +      
  64.302 +      wsptr[DCTSIZE*0] = dcval;
  64.303 +      wsptr[DCTSIZE*1] = dcval;
  64.304 +      
  64.305 +      continue;
  64.306 +    }
  64.307 +    
  64.308 +    /* Even part */
  64.309 +    
  64.310 +    z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  64.311 +    tmp10 = z1 << (CONST_BITS+2);
  64.312 +    
  64.313 +    /* Odd part */
  64.314 +
  64.315 +    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  64.316 +    tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  64.317 +    z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  64.318 +    tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  64.319 +    z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  64.320 +    tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  64.321 +    z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  64.322 +    tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  64.323 +
  64.324 +    /* Final output stage */
  64.325 +    
  64.326 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  64.327 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  64.328 +  }
  64.329 +  
  64.330 +  /* Pass 2: process 2 rows from work array, store into output array. */
  64.331 +
  64.332 +  wsptr = workspace;
  64.333 +  for (ctr = 0; ctr < 2; ctr++) {
  64.334 +    outptr = output_buf[ctr] + output_col;
  64.335 +    /* It's not clear whether a zero row test is worthwhile here ... */
  64.336 +
  64.337 +#ifndef NO_ZERO_ROW_TEST
  64.338 +    if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
  64.339 +      /* AC terms all zero */
  64.340 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  64.341 +				  & RANGE_MASK];
  64.342 +      
  64.343 +      outptr[0] = dcval;
  64.344 +      outptr[1] = dcval;
  64.345 +      
  64.346 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  64.347 +      continue;
  64.348 +    }
  64.349 +#endif
  64.350 +    
  64.351 +    /* Even part */
  64.352 +    
  64.353 +    tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
  64.354 +    
  64.355 +    /* Odd part */
  64.356 +
  64.357 +    tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  64.358 +	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  64.359 +	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  64.360 +	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  64.361 +
  64.362 +    /* Final output stage */
  64.363 +    
  64.364 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  64.365 +					  CONST_BITS+PASS1_BITS+3+2)
  64.366 +			    & RANGE_MASK];
  64.367 +    outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  64.368 +					  CONST_BITS+PASS1_BITS+3+2)
  64.369 +			    & RANGE_MASK];
  64.370 +    
  64.371 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  64.372 +  }
  64.373 +}
  64.374 +
  64.375 +
  64.376 +/*
  64.377 + * Perform dequantization and inverse DCT on one block of coefficients,
  64.378 + * producing a reduced-size 1x1 output block.
  64.379 + */
  64.380 +
  64.381 +GLOBAL(void)
  64.382 +jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  64.383 +	       JCOEFPTR coef_block,
  64.384 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  64.385 +{
  64.386 +  int dcval;
  64.387 +  ISLOW_MULT_TYPE * quantptr;
  64.388 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  64.389 +  SHIFT_TEMPS
  64.390 +
  64.391 +  /* We hardly need an inverse DCT routine for this: just take the
  64.392 +   * average pixel value, which is one-eighth of the DC coefficient.
  64.393 +   */
  64.394 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  64.395 +  dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  64.396 +  dcval = (int) DESCALE((INT32) dcval, 3);
  64.397 +
  64.398 +  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  64.399 +}
  64.400 +
  64.401 +#endif /* IDCT_SCALING_SUPPORTED */
    65.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    65.2 +++ b/libs/libjpeg/jinclude.h	Sun Jun 07 17:25:49 2015 +0300
    65.3 @@ -0,0 +1,91 @@
    65.4 +/*
    65.5 + * jinclude.h
    65.6 + *
    65.7 + * Copyright (C) 1991-1994, Thomas G. Lane.
    65.8 + * This file is part of the Independent JPEG Group's software.
    65.9 + * For conditions of distribution and use, see the accompanying README file.
   65.10 + *
   65.11 + * This file exists to provide a single place to fix any problems with
   65.12 + * including the wrong system include files.  (Common problems are taken
   65.13 + * care of by the standard jconfig symbols, but on really weird systems
   65.14 + * you may have to edit this file.)
   65.15 + *
   65.16 + * NOTE: this file is NOT intended to be included by applications using the
   65.17 + * JPEG library.  Most applications need only include jpeglib.h.
   65.18 + */
   65.19 +
   65.20 +
   65.21 +/* Include auto-config file to find out which system include files we need. */
   65.22 +
   65.23 +#include "jconfig.h"		/* auto configuration options */
   65.24 +#define JCONFIG_INCLUDED	/* so that jpeglib.h doesn't do it again */
   65.25 +
   65.26 +/*
   65.27 + * We need the NULL macro and size_t typedef.
   65.28 + * On an ANSI-conforming system it is sufficient to include <stddef.h>.
   65.29 + * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
   65.30 + * pull in <sys/types.h> as well.
   65.31 + * Note that the core JPEG library does not require <stdio.h>;
   65.32 + * only the default error handler and data source/destination modules do.
   65.33 + * But we must pull it in because of the references to FILE in jpeglib.h.
   65.34 + * You can remove those references if you want to compile without <stdio.h>.
   65.35 + */
   65.36 +
   65.37 +#ifdef HAVE_STDDEF_H
   65.38 +#include <stddef.h>
   65.39 +#endif
   65.40 +
   65.41 +#ifdef HAVE_STDLIB_H
   65.42 +#include <stdlib.h>
   65.43 +#endif
   65.44 +
   65.45 +#ifdef NEED_SYS_TYPES_H
   65.46 +#include <sys/types.h>
   65.47 +#endif
   65.48 +
   65.49 +#include <stdio.h>
   65.50 +
   65.51 +/*
   65.52 + * We need memory copying and zeroing functions, plus strncpy().
   65.53 + * ANSI and System V implementations declare these in <string.h>.
   65.54 + * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
   65.55 + * Some systems may declare memset and memcpy in <memory.h>.
   65.56 + *
   65.57 + * NOTE: we assume the size parameters to these functions are of type size_t.
   65.58 + * Change the casts in these macros if not!
   65.59 + */
   65.60 +
   65.61 +#ifdef NEED_BSD_STRINGS
   65.62 +
   65.63 +#include <strings.h>
   65.64 +#define MEMZERO(target,size)	bzero((void *)(target), (size_t)(size))
   65.65 +#define MEMCOPY(dest,src,size)	bcopy((const void *)(src), (void *)(dest), (size_t)(size))
   65.66 +
   65.67 +#else /* not BSD, assume ANSI/SysV string lib */
   65.68 +
   65.69 +#include <string.h>
   65.70 +#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
   65.71 +#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))
   65.72 +
   65.73 +#endif
   65.74 +
   65.75 +/*
   65.76 + * In ANSI C, and indeed any rational implementation, size_t is also the
   65.77 + * type returned by sizeof().  However, it seems there are some irrational
   65.78 + * implementations out there, in which sizeof() returns an int even though
   65.79 + * size_t is defined as long or unsigned long.  To ensure consistent results
   65.80 + * we always use this SIZEOF() macro in place of using sizeof() directly.
   65.81 + */
   65.82 +
   65.83 +#define SIZEOF(object)	((size_t) sizeof(object))
   65.84 +
   65.85 +/*
   65.86 + * The modules that use fread() and fwrite() always invoke them through
   65.87 + * these macros.  On some systems you may need to twiddle the argument casts.
   65.88 + * CAUTION: argument order is different from underlying functions!
   65.89 + */
   65.90 +
   65.91 +#define JFREAD(file,buf,sizeofbuf)  \
   65.92 +  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
   65.93 +#define JFWRITE(file,buf,sizeofbuf)  \
   65.94 +  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
    66.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    66.2 +++ b/libs/libjpeg/jmemmgr.c	Sun Jun 07 17:25:49 2015 +0300
    66.3 @@ -0,0 +1,1118 @@
    66.4 +/*
    66.5 + * jmemmgr.c
    66.6 + *
    66.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    66.8 + * This file is part of the Independent JPEG Group's software.
    66.9 + * For conditions of distribution and use, see the accompanying README file.
   66.10 + *
   66.11 + * This file contains the JPEG system-independent memory management
   66.12 + * routines.  This code is usable across a wide variety of machines; most
   66.13 + * of the system dependencies have been isolated in a separate file.
   66.14 + * The major functions provided here are:
   66.15 + *   * pool-based allocation and freeing of memory;
   66.16 + *   * policy decisions about how to divide available memory among the
   66.17 + *     virtual arrays;
   66.18 + *   * control logic for swapping virtual arrays between main memory and
   66.19 + *     backing storage.
   66.20 + * The separate system-dependent file provides the actual backing-storage
   66.21 + * access code, and it contains the policy decision about how much total
   66.22 + * main memory to use.
   66.23 + * This file is system-dependent in the sense that some of its functions
   66.24 + * are unnecessary in some systems.  For example, if there is enough virtual
   66.25 + * memory so that backing storage will never be used, much of the virtual
   66.26 + * array control logic could be removed.  (Of course, if you have that much
   66.27 + * memory then you shouldn't care about a little bit of unused code...)
   66.28 + */
   66.29 +
   66.30 +#define JPEG_INTERNALS
   66.31 +#define AM_MEMORY_MANAGER	/* we define jvirt_Xarray_control structs */
   66.32 +#include "jinclude.h"
   66.33 +#include "jpeglib.h"
   66.34 +#include "jmemsys.h"		/* import the system-dependent declarations */
   66.35 +
   66.36 +#ifndef NO_GETENV
   66.37 +#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare getenv() */
   66.38 +extern char * getenv JPP((const char * name));
   66.39 +#endif
   66.40 +#endif
   66.41 +
   66.42 +
   66.43 +/*
   66.44 + * Some important notes:
   66.45 + *   The allocation routines provided here must never return NULL.
   66.46 + *   They should exit to error_exit if unsuccessful.
   66.47 + *
   66.48 + *   It's not a good idea to try to merge the sarray and barray routines,
   66.49 + *   even though they are textually almost the same, because samples are
   66.50 + *   usually stored as bytes while coefficients are shorts or ints.  Thus,
   66.51 + *   in machines where byte pointers have a different representation from
   66.52 + *   word pointers, the resulting machine code could not be the same.
   66.53 + */
   66.54 +
   66.55 +
   66.56 +/*
   66.57 + * Many machines require storage alignment: longs must start on 4-byte
   66.58 + * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
   66.59 + * always returns pointers that are multiples of the worst-case alignment
   66.60 + * requirement, and we had better do so too.
   66.61 + * There isn't any really portable way to determine the worst-case alignment
   66.62 + * requirement.  This module assumes that the alignment requirement is
   66.63 + * multiples of sizeof(ALIGN_TYPE).
   66.64 + * By default, we define ALIGN_TYPE as double.  This is necessary on some
   66.65 + * workstations (where doubles really do need 8-byte alignment) and will work
   66.66 + * fine on nearly everything.  If your machine has lesser alignment needs,
   66.67 + * you can save a few bytes by making ALIGN_TYPE smaller.
   66.68 + * The only place I know of where this will NOT work is certain Macintosh
   66.69 + * 680x0 compilers that define double as a 10-byte IEEE extended float.
   66.70 + * Doing 10-byte alignment is counterproductive because longwords won't be
   66.71 + * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have
   66.72 + * such a compiler.
   66.73 + */
   66.74 +
   66.75 +#ifndef ALIGN_TYPE		/* so can override from jconfig.h */
   66.76 +#define ALIGN_TYPE  double
   66.77 +#endif
   66.78 +
   66.79 +
   66.80 +/*
   66.81 + * We allocate objects from "pools", where each pool is gotten with a single
   66.82 + * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
   66.83 + * overhead within a pool, except for alignment padding.  Each pool has a
   66.84 + * header with a link to the next pool of the same class.
   66.85 + * Small and large pool headers are identical except that the latter's
   66.86 + * link pointer must be FAR on 80x86 machines.
   66.87 + * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
   66.88 + * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
   66.89 + * of the alignment requirement of ALIGN_TYPE.
   66.90 + */
   66.91 +
   66.92 +typedef union small_pool_struct * small_pool_ptr;
   66.93 +
   66.94 +typedef union small_pool_struct {
   66.95 +  struct {
   66.96 +    small_pool_ptr next;	/* next in list of pools */
   66.97 +    size_t bytes_used;		/* how many bytes already used within pool */
   66.98 +    size_t bytes_left;		/* bytes still available in this pool */
   66.99 +  } hdr;
  66.100 +  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
  66.101 +} small_pool_hdr;
  66.102 +
  66.103 +typedef union large_pool_struct FAR * large_pool_ptr;
  66.104 +
  66.105 +typedef union large_pool_struct {
  66.106 +  struct {
  66.107 +    large_pool_ptr next;	/* next in list of pools */
  66.108 +    size_t bytes_used;		/* how many bytes already used within pool */
  66.109 +    size_t bytes_left;		/* bytes still available in this pool */
  66.110 +  } hdr;
  66.111 +  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
  66.112 +} large_pool_hdr;
  66.113 +
  66.114 +
  66.115 +/*
  66.116 + * Here is the full definition of a memory manager object.
  66.117 + */
  66.118 +
  66.119 +typedef struct {
  66.120 +  struct jpeg_memory_mgr pub;	/* public fields */
  66.121 +
  66.122 +  /* Each pool identifier (lifetime class) names a linked list of pools. */
  66.123 +  small_pool_ptr small_list[JPOOL_NUMPOOLS];
  66.124 +  large_pool_ptr large_list[JPOOL_NUMPOOLS];
  66.125 +
  66.126 +  /* Since we only have one lifetime class of virtual arrays, only one
  66.127 +   * linked list is necessary (for each datatype).  Note that the virtual
  66.128 +   * array control blocks being linked together are actually stored somewhere
  66.129 +   * in the small-pool list.
  66.130 +   */
  66.131 +  jvirt_sarray_ptr virt_sarray_list;
  66.132 +  jvirt_barray_ptr virt_barray_list;
  66.133 +
  66.134 +  /* This counts total space obtained from jpeg_get_small/large */
  66.135 +  long total_space_allocated;
  66.136 +
  66.137 +  /* alloc_sarray and alloc_barray set this value for use by virtual
  66.138 +   * array routines.
  66.139 +   */
  66.140 +  JDIMENSION last_rowsperchunk;	/* from most recent alloc_sarray/barray */
  66.141 +} my_memory_mgr;
  66.142 +
  66.143 +typedef my_memory_mgr * my_mem_ptr;
  66.144 +
  66.145 +
  66.146 +/*
  66.147 + * The control blocks for virtual arrays.
  66.148 + * Note that these blocks are allocated in the "small" pool area.
  66.149 + * System-dependent info for the associated backing store (if any) is hidden
  66.150 + * inside the backing_store_info struct.
  66.151 + */
  66.152 +
  66.153 +struct jvirt_sarray_control {
  66.154 +  JSAMPARRAY mem_buffer;	/* => the in-memory buffer */
  66.155 +  JDIMENSION rows_in_array;	/* total virtual array height */
  66.156 +  JDIMENSION samplesperrow;	/* width of array (and of memory buffer) */
  66.157 +  JDIMENSION maxaccess;		/* max rows accessed by access_virt_sarray */
  66.158 +  JDIMENSION rows_in_mem;	/* height of memory buffer */
  66.159 +  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  66.160 +  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  66.161 +  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  66.162 +  boolean pre_zero;		/* pre-zero mode requested? */
  66.163 +  boolean dirty;		/* do current buffer contents need written? */
  66.164 +  boolean b_s_open;		/* is backing-store data valid? */
  66.165 +  jvirt_sarray_ptr next;	/* link to next virtual sarray control block */
  66.166 +  backing_store_info b_s_info;	/* System-dependent control info */
  66.167 +};
  66.168 +
  66.169 +struct jvirt_barray_control {
  66.170 +  JBLOCKARRAY mem_buffer;	/* => the in-memory buffer */
  66.171 +  JDIMENSION rows_in_array;	/* total virtual array height */
  66.172 +  JDIMENSION blocksperrow;	/* width of array (and of memory buffer) */
  66.173 +  JDIMENSION maxaccess;		/* max rows accessed by access_virt_barray */
  66.174 +  JDIMENSION rows_in_mem;	/* height of memory buffer */
  66.175 +  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  66.176 +  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  66.177 +  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  66.178 +  boolean pre_zero;		/* pre-zero mode requested? */
  66.179 +  boolean dirty;		/* do current buffer contents need written? */
  66.180 +  boolean b_s_open;		/* is backing-store data valid? */
  66.181 +  jvirt_barray_ptr next;	/* link to next virtual barray control block */
  66.182 +  backing_store_info b_s_info;	/* System-dependent control info */
  66.183 +};
  66.184 +
  66.185 +
  66.186 +#ifdef MEM_STATS		/* optional extra stuff for statistics */
  66.187 +
  66.188 +LOCAL(void)
  66.189 +print_mem_stats (j_common_ptr cinfo, int pool_id)
  66.190 +{
  66.191 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.192 +  small_pool_ptr shdr_ptr;
  66.193 +  large_pool_ptr lhdr_ptr;
  66.194 +
  66.195 +  /* Since this is only a debugging stub, we can cheat a little by using
  66.196 +   * fprintf directly rather than going through the trace message code.
  66.197 +   * This is helpful because message parm array can't handle longs.
  66.198 +   */
  66.199 +  fprintf(stderr, "Freeing pool %d, total space = %ld\n",
  66.200 +	  pool_id, mem->total_space_allocated);
  66.201 +
  66.202 +  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
  66.203 +       lhdr_ptr = lhdr_ptr->hdr.next) {
  66.204 +    fprintf(stderr, "  Large chunk used %ld\n",
  66.205 +	    (long) lhdr_ptr->hdr.bytes_used);
  66.206 +  }
  66.207 +
  66.208 +  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
  66.209 +       shdr_ptr = shdr_ptr->hdr.next) {
  66.210 +    fprintf(stderr, "  Small chunk used %ld free %ld\n",
  66.211 +	    (long) shdr_ptr->hdr.bytes_used,
  66.212 +	    (long) shdr_ptr->hdr.bytes_left);
  66.213 +  }
  66.214 +}
  66.215 +
  66.216 +#endif /* MEM_STATS */
  66.217 +
  66.218 +
  66.219 +LOCAL(void)
  66.220 +out_of_memory (j_common_ptr cinfo, int which)
  66.221 +/* Report an out-of-memory error and stop execution */
  66.222 +/* If we compiled MEM_STATS support, report alloc requests before dying */
  66.223 +{
  66.224 +#ifdef MEM_STATS
  66.225 +  cinfo->err->trace_level = 2;	/* force self_destruct to report stats */
  66.226 +#endif
  66.227 +  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
  66.228 +}
  66.229 +
  66.230 +
  66.231 +/*
  66.232 + * Allocation of "small" objects.
  66.233 + *
  66.234 + * For these, we use pooled storage.  When a new pool must be created,
  66.235 + * we try to get enough space for the current request plus a "slop" factor,
  66.236 + * where the slop will be the amount of leftover space in the new pool.
  66.237 + * The speed vs. space tradeoff is largely determined by the slop values.
  66.238 + * A different slop value is provided for each pool class (lifetime),
  66.239 + * and we also distinguish the first pool of a class from later ones.
  66.240 + * NOTE: the values given work fairly well on both 16- and 32-bit-int
  66.241 + * machines, but may be too small if longs are 64 bits or more.
  66.242 + */
  66.243 +
  66.244 +static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 
  66.245 +{
  66.246 +	1600,			/* first PERMANENT pool */
  66.247 +	16000			/* first IMAGE pool */
  66.248 +};
  66.249 +
  66.250 +static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 
  66.251 +{
  66.252 +	0,			/* additional PERMANENT pools */
  66.253 +	5000			/* additional IMAGE pools */
  66.254 +};
  66.255 +
  66.256 +#define MIN_SLOP  50		/* greater than 0 to avoid futile looping */
  66.257 +
  66.258 +
  66.259 +METHODDEF(void *)
  66.260 +alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  66.261 +/* Allocate a "small" object */
  66.262 +{
  66.263 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.264 +  small_pool_ptr hdr_ptr, prev_hdr_ptr;
  66.265 +  char * data_ptr;
  66.266 +  size_t odd_bytes, min_request, slop;
  66.267 +
  66.268 +  /* Check for unsatisfiable request (do now to ensure no overflow below) */
  66.269 +  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
  66.270 +    out_of_memory(cinfo, 1);	/* request exceeds malloc's ability */
  66.271 +
  66.272 +  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  66.273 +  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  66.274 +  if (odd_bytes > 0)
  66.275 +    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  66.276 +
  66.277 +  /* See if space is available in any existing pool */
  66.278 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  66.279 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  66.280 +  prev_hdr_ptr = NULL;
  66.281 +  hdr_ptr = mem->small_list[pool_id];
  66.282 +  while (hdr_ptr != NULL) {
  66.283 +    if (hdr_ptr->hdr.bytes_left >= sizeofobject)
  66.284 +      break;			/* found pool with enough space */
  66.285 +    prev_hdr_ptr = hdr_ptr;
  66.286 +    hdr_ptr = hdr_ptr->hdr.next;
  66.287 +  }
  66.288 +
  66.289 +  /* Time to make a new pool? */
  66.290 +  if (hdr_ptr == NULL) {
  66.291 +    /* min_request is what we need now, slop is what will be leftover */
  66.292 +    min_request = sizeofobject + SIZEOF(small_pool_hdr);
  66.293 +    if (prev_hdr_ptr == NULL)	/* first pool in class? */
  66.294 +      slop = first_pool_slop[pool_id];
  66.295 +    else
  66.296 +      slop = extra_pool_slop[pool_id];
  66.297 +    /* Don't ask for more than MAX_ALLOC_CHUNK */
  66.298 +    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
  66.299 +      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
  66.300 +    /* Try to get space, if fail reduce slop and try again */
  66.301 +    for (;;) {
  66.302 +      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
  66.303 +      if (hdr_ptr != NULL)
  66.304 +	break;
  66.305 +      slop /= 2;
  66.306 +      if (slop < MIN_SLOP)	/* give up when it gets real small */
  66.307 +	out_of_memory(cinfo, 2); /* jpeg_get_small failed */
  66.308 +    }
  66.309 +    mem->total_space_allocated += min_request + slop;
  66.310 +    /* Success, initialize the new pool header and add to end of list */
  66.311 +    hdr_ptr->hdr.next = NULL;
  66.312 +    hdr_ptr->hdr.bytes_used = 0;
  66.313 +    hdr_ptr->hdr.bytes_left = sizeofobject + slop;
  66.314 +    if (prev_hdr_ptr == NULL)	/* first pool in class? */
  66.315 +      mem->small_list[pool_id] = hdr_ptr;
  66.316 +    else
  66.317 +      prev_hdr_ptr->hdr.next = hdr_ptr;
  66.318 +  }
  66.319 +
  66.320 +  /* OK, allocate the object from the current pool */
  66.321 +  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
  66.322 +  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
  66.323 +  hdr_ptr->hdr.bytes_used += sizeofobject;
  66.324 +  hdr_ptr->hdr.bytes_left -= sizeofobject;
  66.325 +
  66.326 +  return (void *) data_ptr;
  66.327 +}
  66.328 +
  66.329 +
  66.330 +/*
  66.331 + * Allocation of "large" objects.
  66.332 + *
  66.333 + * The external semantics of these are the same as "small" objects,
  66.334 + * except that FAR pointers are used on 80x86.  However the pool
  66.335 + * management heuristics are quite different.  We assume that each
  66.336 + * request is large enough that it may as well be passed directly to
  66.337 + * jpeg_get_large; the pool management just links everything together
  66.338 + * so that we can free it all on demand.
  66.339 + * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
  66.340 + * structures.  The routines that create these structures (see below)
  66.341 + * deliberately bunch rows together to ensure a large request size.
  66.342 + */
  66.343 +
  66.344 +METHODDEF(void FAR *)
  66.345 +alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  66.346 +/* Allocate a "large" object */
  66.347 +{
  66.348 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.349 +  large_pool_ptr hdr_ptr;
  66.350 +  size_t odd_bytes;
  66.351 +
  66.352 +  /* Check for unsatisfiable request (do now to ensure no overflow below) */
  66.353 +  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
  66.354 +    out_of_memory(cinfo, 3);	/* request exceeds malloc's ability */
  66.355 +
  66.356 +  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  66.357 +  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  66.358 +  if (odd_bytes > 0)
  66.359 +    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  66.360 +
  66.361 +  /* Always make a new pool */
  66.362 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  66.363 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  66.364 +
  66.365 +  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
  66.366 +					    SIZEOF(large_pool_hdr));
  66.367 +  if (hdr_ptr == NULL)
  66.368 +    out_of_memory(cinfo, 4);	/* jpeg_get_large failed */
  66.369 +  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
  66.370 +
  66.371 +  /* Success, initialize the new pool header and add to list */
  66.372 +  hdr_ptr->hdr.next = mem->large_list[pool_id];
  66.373 +  /* We maintain space counts in each pool header for statistical purposes,
  66.374 +   * even though they are not needed for allocation.
  66.375 +   */
  66.376 +  hdr_ptr->hdr.bytes_used = sizeofobject;
  66.377 +  hdr_ptr->hdr.bytes_left = 0;
  66.378 +  mem->large_list[pool_id] = hdr_ptr;
  66.379 +
  66.380 +  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
  66.381 +}
  66.382 +
  66.383 +
  66.384 +/*
  66.385 + * Creation of 2-D sample arrays.
  66.386 + * The pointers are in near heap, the samples themselves in FAR heap.
  66.387 + *
  66.388 + * To minimize allocation overhead and to allow I/O of large contiguous
  66.389 + * blocks, we allocate the sample rows in groups of as many rows as possible
  66.390 + * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
  66.391 + * NB: the virtual array control routines, later in this file, know about
  66.392 + * this chunking of rows.  The rowsperchunk value is left in the mem manager
  66.393 + * object so that it can be saved away if this sarray is the workspace for
  66.394 + * a virtual array.
  66.395 + */
  66.396 +
  66.397 +METHODDEF(JSAMPARRAY)
  66.398 +alloc_sarray (j_common_ptr cinfo, int pool_id,
  66.399 +	      JDIMENSION samplesperrow, JDIMENSION numrows)
  66.400 +/* Allocate a 2-D sample array */
  66.401 +{
  66.402 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.403 +  JSAMPARRAY result;
  66.404 +  JSAMPROW workspace;
  66.405 +  JDIMENSION rowsperchunk, currow, i;
  66.406 +  long ltemp;
  66.407 +
  66.408 +  /* Calculate max # of rows allowed in one allocation chunk */
  66.409 +  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  66.410 +	  ((long) samplesperrow * SIZEOF(JSAMPLE));
  66.411 +  if (ltemp <= 0)
  66.412 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  66.413 +  if (ltemp < (long) numrows)
  66.414 +    rowsperchunk = (JDIMENSION) ltemp;
  66.415 +  else
  66.416 +    rowsperchunk = numrows;
  66.417 +  mem->last_rowsperchunk = rowsperchunk;
  66.418 +
  66.419 +  /* Get space for row pointers (small object) */
  66.420 +  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
  66.421 +				    (size_t) (numrows * SIZEOF(JSAMPROW)));
  66.422 +
  66.423 +  /* Get the rows themselves (large objects) */
  66.424 +  currow = 0;
  66.425 +  while (currow < numrows) {
  66.426 +    rowsperchunk = MIN(rowsperchunk, numrows - currow);
  66.427 +    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
  66.428 +	(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
  66.429 +		  * SIZEOF(JSAMPLE)));
  66.430 +    for (i = rowsperchunk; i > 0; i--) {
  66.431 +      result[currow++] = workspace;
  66.432 +      workspace += samplesperrow;
  66.433 +    }
  66.434 +  }
  66.435 +
  66.436 +  return result;
  66.437 +}
  66.438 +
  66.439 +
  66.440 +/*
  66.441 + * Creation of 2-D coefficient-block arrays.
  66.442 + * This is essentially the same as the code for sample arrays, above.
  66.443 + */
  66.444 +
  66.445 +METHODDEF(JBLOCKARRAY)
  66.446 +alloc_barray (j_common_ptr cinfo, int pool_id,
  66.447 +	      JDIMENSION blocksperrow, JDIMENSION numrows)
  66.448 +/* Allocate a 2-D coefficient-block array */
  66.449 +{
  66.450 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.451 +  JBLOCKARRAY result;
  66.452 +  JBLOCKROW workspace;
  66.453 +  JDIMENSION rowsperchunk, currow, i;
  66.454 +  long ltemp;
  66.455 +
  66.456 +  /* Calculate max # of rows allowed in one allocation chunk */
  66.457 +  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  66.458 +	  ((long) blocksperrow * SIZEOF(JBLOCK));
  66.459 +  if (ltemp <= 0)
  66.460 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  66.461 +  if (ltemp < (long) numrows)
  66.462 +    rowsperchunk = (JDIMENSION) ltemp;
  66.463 +  else
  66.464 +    rowsperchunk = numrows;
  66.465 +  mem->last_rowsperchunk = rowsperchunk;
  66.466 +
  66.467 +  /* Get space for row pointers (small object) */
  66.468 +  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
  66.469 +				     (size_t) (numrows * SIZEOF(JBLOCKROW)));
  66.470 +
  66.471 +  /* Get the rows themselves (large objects) */
  66.472 +  currow = 0;
  66.473 +  while (currow < numrows) {
  66.474 +    rowsperchunk = MIN(rowsperchunk, numrows - currow);
  66.475 +    workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
  66.476 +	(size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
  66.477 +		  * SIZEOF(JBLOCK)));
  66.478 +    for (i = rowsperchunk; i > 0; i--) {
  66.479 +      result[currow++] = workspace;
  66.480 +      workspace += blocksperrow;
  66.481 +    }
  66.482 +  }
  66.483 +
  66.484 +  return result;
  66.485 +}
  66.486 +
  66.487 +
  66.488 +/*
  66.489 + * About virtual array management:
  66.490 + *
  66.491 + * The above "normal" array routines are only used to allocate strip buffers
  66.492 + * (as wide as the image, but just a few rows high).  Full-image-sized buffers
  66.493 + * are handled as "virtual" arrays.  The array is still accessed a strip at a
  66.494 + * time, but the memory manager must save the whole array for repeated
  66.495 + * accesses.  The intended implementation is that there is a strip buffer in
  66.496 + * memory (as high as is possible given the desired memory limit), plus a
  66.497 + * backing file that holds the rest of the array.
  66.498 + *
  66.499 + * The request_virt_array routines are told the total size of the image and
  66.500 + * the maximum number of rows that will be accessed at once.  The in-memory
  66.501 + * buffer must be at least as large as the maxaccess value.
  66.502 + *
  66.503 + * The request routines create control blocks but not the in-memory buffers.
  66.504 + * That is postponed until realize_virt_arrays is called.  At that time the
  66.505 + * total amount of space needed is known (approximately, anyway), so free
  66.506 + * memory can be divided up fairly.
  66.507 + *
  66.508 + * The access_virt_array routines are responsible for making a specific strip
  66.509 + * area accessible (after reading or writing the backing file, if necessary).
  66.510 + * Note that the access routines are told whether the caller intends to modify
  66.511 + * the accessed strip; during a read-only pass this saves having to rewrite
  66.512 + * data to disk.  The access routines are also responsible for pre-zeroing
  66.513 + * any newly accessed rows, if pre-zeroing was requested.
  66.514 + *
  66.515 + * In current usage, the access requests are usually for nonoverlapping
  66.516 + * strips; that is, successive access start_row numbers differ by exactly
  66.517 + * num_rows = maxaccess.  This means we can get good performance with simple
  66.518 + * buffer dump/reload logic, by making the in-memory buffer be a multiple
  66.519 + * of the access height; then there will never be accesses across bufferload
  66.520 + * boundaries.  The code will still work with overlapping access requests,
  66.521 + * but it doesn't handle bufferload overlaps very efficiently.
  66.522 + */
  66.523 +
  66.524 +
  66.525 +METHODDEF(jvirt_sarray_ptr)
  66.526 +request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  66.527 +		     JDIMENSION samplesperrow, JDIMENSION numrows,
  66.528 +		     JDIMENSION maxaccess)
  66.529 +/* Request a virtual 2-D sample array */
  66.530 +{
  66.531 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.532 +  jvirt_sarray_ptr result;
  66.533 +
  66.534 +  /* Only IMAGE-lifetime virtual arrays are currently supported */
  66.535 +  if (pool_id != JPOOL_IMAGE)
  66.536 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  66.537 +
  66.538 +  /* get control block */
  66.539 +  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
  66.540 +					  SIZEOF(struct jvirt_sarray_control));
  66.541 +
  66.542 +  result->mem_buffer = NULL;	/* marks array not yet realized */
  66.543 +  result->rows_in_array = numrows;
  66.544 +  result->samplesperrow = samplesperrow;
  66.545 +  result->maxaccess = maxaccess;
  66.546 +  result->pre_zero = pre_zero;
  66.547 +  result->b_s_open = FALSE;	/* no associated backing-store object */
  66.548 +  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
  66.549 +  mem->virt_sarray_list = result;
  66.550 +
  66.551 +  return result;
  66.552 +}
  66.553 +
  66.554 +
  66.555 +METHODDEF(jvirt_barray_ptr)
  66.556 +request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  66.557 +		     JDIMENSION blocksperrow, JDIMENSION numrows,
  66.558 +		     JDIMENSION maxaccess)
  66.559 +/* Request a virtual 2-D coefficient-block array */
  66.560 +{
  66.561 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.562 +  jvirt_barray_ptr result;
  66.563 +
  66.564 +  /* Only IMAGE-lifetime virtual arrays are currently supported */
  66.565 +  if (pool_id != JPOOL_IMAGE)
  66.566 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  66.567 +
  66.568 +  /* get control block */
  66.569 +  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
  66.570 +					  SIZEOF(struct jvirt_barray_control));
  66.571 +
  66.572 +  result->mem_buffer = NULL;	/* marks array not yet realized */
  66.573 +  result->rows_in_array = numrows;
  66.574 +  result->blocksperrow = blocksperrow;
  66.575 +  result->maxaccess = maxaccess;
  66.576 +  result->pre_zero = pre_zero;
  66.577 +  result->b_s_open = FALSE;	/* no associated backing-store object */
  66.578 +  result->next = mem->virt_barray_list; /* add to list of virtual arrays */
  66.579 +  mem->virt_barray_list = result;
  66.580 +
  66.581 +  return result;
  66.582 +}
  66.583 +
  66.584 +
  66.585 +METHODDEF(void)
  66.586 +realize_virt_arrays (j_common_ptr cinfo)
  66.587 +/* Allocate the in-memory buffers for any unrealized virtual arrays */
  66.588 +{
  66.589 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.590 +  long space_per_minheight, maximum_space, avail_mem;
  66.591 +  long minheights, max_minheights;
  66.592 +  jvirt_sarray_ptr sptr;
  66.593 +  jvirt_barray_ptr bptr;
  66.594 +
  66.595 +  /* Compute the minimum space needed (maxaccess rows in each buffer)
  66.596 +   * and the maximum space needed (full image height in each buffer).
  66.597 +   * These may be of use to the system-dependent jpeg_mem_available routine.
  66.598 +   */
  66.599 +  space_per_minheight = 0;
  66.600 +  maximum_space = 0;
  66.601 +  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  66.602 +    if (sptr->mem_buffer == NULL) { /* if not realized yet */
  66.603 +      space_per_minheight += (long) sptr->maxaccess *
  66.604 +			     (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  66.605 +      maximum_space += (long) sptr->rows_in_array *
  66.606 +		       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  66.607 +    }
  66.608 +  }
  66.609 +  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  66.610 +    if (bptr->mem_buffer == NULL) { /* if not realized yet */
  66.611 +      space_per_minheight += (long) bptr->maxaccess *
  66.612 +			     (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  66.613 +      maximum_space += (long) bptr->rows_in_array *
  66.614 +		       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  66.615 +    }
  66.616 +  }
  66.617 +
  66.618 +  if (space_per_minheight <= 0)
  66.619 +    return;			/* no unrealized arrays, no work */
  66.620 +
  66.621 +  /* Determine amount of memory to actually use; this is system-dependent. */
  66.622 +  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
  66.623 +				 mem->total_space_allocated);
  66.624 +
  66.625 +  /* If the maximum space needed is available, make all the buffers full
  66.626 +   * height; otherwise parcel it out with the same number of minheights
  66.627 +   * in each buffer.
  66.628 +   */
  66.629 +  if (avail_mem >= maximum_space)
  66.630 +    max_minheights = 1000000000L;
  66.631 +  else {
  66.632 +    max_minheights = avail_mem / space_per_minheight;
  66.633 +    /* If there doesn't seem to be enough space, try to get the minimum
  66.634 +     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
  66.635 +     */
  66.636 +    if (max_minheights <= 0)
  66.637 +      max_minheights = 1;
  66.638 +  }
  66.639 +
  66.640 +  /* Allocate the in-memory buffers and initialize backing store as needed. */
  66.641 +
  66.642 +  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  66.643 +    if (sptr->mem_buffer == NULL) { /* if not realized yet */
  66.644 +      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
  66.645 +      if (minheights <= max_minheights) {
  66.646 +	/* This buffer fits in memory */
  66.647 +	sptr->rows_in_mem = sptr->rows_in_array;
  66.648 +      } else {
  66.649 +	/* It doesn't fit in memory, create backing store. */
  66.650 +	sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
  66.651 +	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
  66.652 +				(long) sptr->rows_in_array *
  66.653 +				(long) sptr->samplesperrow *
  66.654 +				(long) SIZEOF(JSAMPLE));
  66.655 +	sptr->b_s_open = TRUE;
  66.656 +      }
  66.657 +      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
  66.658 +				      sptr->samplesperrow, sptr->rows_in_mem);
  66.659 +      sptr->rowsperchunk = mem->last_rowsperchunk;
  66.660 +      sptr->cur_start_row = 0;
  66.661 +      sptr->first_undef_row = 0;
  66.662 +      sptr->dirty = FALSE;
  66.663 +    }
  66.664 +  }
  66.665 +
  66.666 +  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  66.667 +    if (bptr->mem_buffer == NULL) { /* if not realized yet */
  66.668 +      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
  66.669 +      if (minheights <= max_minheights) {
  66.670 +	/* This buffer fits in memory */
  66.671 +	bptr->rows_in_mem = bptr->rows_in_array;
  66.672 +      } else {
  66.673 +	/* It doesn't fit in memory, create backing store. */
  66.674 +	bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
  66.675 +	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
  66.676 +				(long) bptr->rows_in_array *
  66.677 +				(long) bptr->blocksperrow *
  66.678 +				(long) SIZEOF(JBLOCK));
  66.679 +	bptr->b_s_open = TRUE;
  66.680 +      }
  66.681 +      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
  66.682 +				      bptr->blocksperrow, bptr->rows_in_mem);
  66.683 +      bptr->rowsperchunk = mem->last_rowsperchunk;
  66.684 +      bptr->cur_start_row = 0;
  66.685 +      bptr->first_undef_row = 0;
  66.686 +      bptr->dirty = FALSE;
  66.687 +    }
  66.688 +  }
  66.689 +}
  66.690 +
  66.691 +
  66.692 +LOCAL(void)
  66.693 +do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
  66.694 +/* Do backing store read or write of a virtual sample array */
  66.695 +{
  66.696 +  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  66.697 +
  66.698 +  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
  66.699 +  file_offset = ptr->cur_start_row * bytesperrow;
  66.700 +  /* Loop to read or write each allocation chunk in mem_buffer */
  66.701 +  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  66.702 +    /* One chunk, but check for short chunk at end of buffer */
  66.703 +    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  66.704 +    /* Transfer no more than is currently defined */
  66.705 +    thisrow = (long) ptr->cur_start_row + i;
  66.706 +    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  66.707 +    /* Transfer no more than fits in file */
  66.708 +    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  66.709 +    if (rows <= 0)		/* this chunk might be past end of file! */
  66.710 +      break;
  66.711 +    byte_count = rows * bytesperrow;
  66.712 +    if (writing)
  66.713 +      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  66.714 +					    (void FAR *) ptr->mem_buffer[i],
  66.715 +					    file_offset, byte_count);
  66.716 +    else
  66.717 +      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  66.718 +					   (void FAR *) ptr->mem_buffer[i],
  66.719 +					   file_offset, byte_count);
  66.720 +    file_offset += byte_count;
  66.721 +  }
  66.722 +}
  66.723 +
  66.724 +
  66.725 +LOCAL(void)
  66.726 +do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
  66.727 +/* Do backing store read or write of a virtual coefficient-block array */
  66.728 +{
  66.729 +  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  66.730 +
  66.731 +  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
  66.732 +  file_offset = ptr->cur_start_row * bytesperrow;
  66.733 +  /* Loop to read or write each allocation chunk in mem_buffer */
  66.734 +  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  66.735 +    /* One chunk, but check for short chunk at end of buffer */
  66.736 +    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  66.737 +    /* Transfer no more than is currently defined */
  66.738 +    thisrow = (long) ptr->cur_start_row + i;
  66.739 +    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  66.740 +    /* Transfer no more than fits in file */
  66.741 +    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  66.742 +    if (rows <= 0)		/* this chunk might be past end of file! */
  66.743 +      break;
  66.744 +    byte_count = rows * bytesperrow;
  66.745 +    if (writing)
  66.746 +      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  66.747 +					    (void FAR *) ptr->mem_buffer[i],
  66.748 +					    file_offset, byte_count);
  66.749 +    else
  66.750 +      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  66.751 +					   (void FAR *) ptr->mem_buffer[i],
  66.752 +					   file_offset, byte_count);
  66.753 +    file_offset += byte_count;
  66.754 +  }
  66.755 +}
  66.756 +
  66.757 +
  66.758 +METHODDEF(JSAMPARRAY)
  66.759 +access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
  66.760 +		    JDIMENSION start_row, JDIMENSION num_rows,
  66.761 +		    boolean writable)
  66.762 +/* Access the part of a virtual sample array starting at start_row */
  66.763 +/* and extending for num_rows rows.  writable is true if  */
  66.764 +/* caller intends to modify the accessed area. */
  66.765 +{
  66.766 +  JDIMENSION end_row = start_row + num_rows;
  66.767 +  JDIMENSION undef_row;
  66.768 +
  66.769 +  /* debugging check */
  66.770 +  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  66.771 +      ptr->mem_buffer == NULL)
  66.772 +    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.773 +
  66.774 +  /* Make the desired part of the virtual array accessible */
  66.775 +  if (start_row < ptr->cur_start_row ||
  66.776 +      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  66.777 +    if (! ptr->b_s_open)
  66.778 +      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  66.779 +    /* Flush old buffer contents if necessary */
  66.780 +    if (ptr->dirty) {
  66.781 +      do_sarray_io(cinfo, ptr, TRUE);
  66.782 +      ptr->dirty = FALSE;
  66.783 +    }
  66.784 +    /* Decide what part of virtual array to access.
  66.785 +     * Algorithm: if target address > current window, assume forward scan,
  66.786 +     * load starting at target address.  If target address < current window,
  66.787 +     * assume backward scan, load so that target area is top of window.
  66.788 +     * Note that when switching from forward write to forward read, will have
  66.789 +     * start_row = 0, so the limiting case applies and we load from 0 anyway.
  66.790 +     */
  66.791 +    if (start_row > ptr->cur_start_row) {
  66.792 +      ptr->cur_start_row = start_row;
  66.793 +    } else {
  66.794 +      /* use long arithmetic here to avoid overflow & unsigned problems */
  66.795 +      long ltemp;
  66.796 +
  66.797 +      ltemp = (long) end_row - (long) ptr->rows_in_mem;
  66.798 +      if (ltemp < 0)
  66.799 +	ltemp = 0;		/* don't fall off front end of file */
  66.800 +      ptr->cur_start_row = (JDIMENSION) ltemp;
  66.801 +    }
  66.802 +    /* Read in the selected part of the array.
  66.803 +     * During the initial write pass, we will do no actual read
  66.804 +     * because the selected part is all undefined.
  66.805 +     */
  66.806 +    do_sarray_io(cinfo, ptr, FALSE);
  66.807 +  }
  66.808 +  /* Ensure the accessed part of the array is defined; prezero if needed.
  66.809 +   * To improve locality of access, we only prezero the part of the array
  66.810 +   * that the caller is about to access, not the entire in-memory array.
  66.811 +   */
  66.812 +  if (ptr->first_undef_row < end_row) {
  66.813 +    if (ptr->first_undef_row < start_row) {
  66.814 +      if (writable)		/* writer skipped over a section of array */
  66.815 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.816 +      undef_row = start_row;	/* but reader is allowed to read ahead */
  66.817 +    } else {
  66.818 +      undef_row = ptr->first_undef_row;
  66.819 +    }
  66.820 +    if (writable)
  66.821 +      ptr->first_undef_row = end_row;
  66.822 +    if (ptr->pre_zero) {
  66.823 +      size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
  66.824 +      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  66.825 +      end_row -= ptr->cur_start_row;
  66.826 +      while (undef_row < end_row) {
  66.827 +	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  66.828 +	undef_row++;
  66.829 +      }
  66.830 +    } else {
  66.831 +      if (! writable)		/* reader looking at undefined data */
  66.832 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.833 +    }
  66.834 +  }
  66.835 +  /* Flag the buffer dirty if caller will write in it */
  66.836 +  if (writable)
  66.837 +    ptr->dirty = TRUE;
  66.838 +  /* Return address of proper part of the buffer */
  66.839 +  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  66.840 +}
  66.841 +
  66.842 +
  66.843 +METHODDEF(JBLOCKARRAY)
  66.844 +access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
  66.845 +		    JDIMENSION start_row, JDIMENSION num_rows,
  66.846 +		    boolean writable)
  66.847 +/* Access the part of a virtual block array starting at start_row */
  66.848 +/* and extending for num_rows rows.  writable is true if  */
  66.849 +/* caller intends to modify the accessed area. */
  66.850 +{
  66.851 +  JDIMENSION end_row = start_row + num_rows;
  66.852 +  JDIMENSION undef_row;
  66.853 +
  66.854 +  /* debugging check */
  66.855 +  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  66.856 +      ptr->mem_buffer == NULL)
  66.857 +    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.858 +
  66.859 +  /* Make the desired part of the virtual array accessible */
  66.860 +  if (start_row < ptr->cur_start_row ||
  66.861 +      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  66.862 +    if (! ptr->b_s_open)
  66.863 +      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  66.864 +    /* Flush old buffer contents if necessary */
  66.865 +    if (ptr->dirty) {
  66.866 +      do_barray_io(cinfo, ptr, TRUE);
  66.867 +      ptr->dirty = FALSE;
  66.868 +    }
  66.869 +    /* Decide what part of virtual array to access.
  66.870 +     * Algorithm: if target address > current window, assume forward scan,
  66.871 +     * load starting at target address.  If target address < current window,
  66.872 +     * assume backward scan, load so that target area is top of window.
  66.873 +     * Note that when switching from forward write to forward read, will have
  66.874 +     * start_row = 0, so the limiting case applies and we load from 0 anyway.
  66.875 +     */
  66.876 +    if (start_row > ptr->cur_start_row) {
  66.877 +      ptr->cur_start_row = start_row;
  66.878 +    } else {
  66.879 +      /* use long arithmetic here to avoid overflow & unsigned problems */
  66.880 +      long ltemp;
  66.881 +
  66.882 +      ltemp = (long) end_row - (long) ptr->rows_in_mem;
  66.883 +      if (ltemp < 0)
  66.884 +	ltemp = 0;		/* don't fall off front end of file */
  66.885 +      ptr->cur_start_row = (JDIMENSION) ltemp;
  66.886 +    }
  66.887 +    /* Read in the selected part of the array.
  66.888 +     * During the initial write pass, we will do no actual read
  66.889 +     * because the selected part is all undefined.
  66.890 +     */
  66.891 +    do_barray_io(cinfo, ptr, FALSE);
  66.892 +  }
  66.893 +  /* Ensure the accessed part of the array is defined; prezero if needed.
  66.894 +   * To improve locality of access, we only prezero the part of the array
  66.895 +   * that the caller is about to access, not the entire in-memory array.
  66.896 +   */
  66.897 +  if (ptr->first_undef_row < end_row) {
  66.898 +    if (ptr->first_undef_row < start_row) {
  66.899 +      if (writable)		/* writer skipped over a section of array */
  66.900 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.901 +      undef_row = start_row;	/* but reader is allowed to read ahead */
  66.902 +    } else {
  66.903 +      undef_row = ptr->first_undef_row;
  66.904 +    }
  66.905 +    if (writable)
  66.906 +      ptr->first_undef_row = end_row;
  66.907 +    if (ptr->pre_zero) {
  66.908 +      size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
  66.909 +      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  66.910 +      end_row -= ptr->cur_start_row;
  66.911 +      while (undef_row < end_row) {
  66.912 +	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  66.913 +	undef_row++;
  66.914 +      }
  66.915 +    } else {
  66.916 +      if (! writable)		/* reader looking at undefined data */
  66.917 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  66.918 +    }
  66.919 +  }
  66.920 +  /* Flag the buffer dirty if caller will write in it */
  66.921 +  if (writable)
  66.922 +    ptr->dirty = TRUE;
  66.923 +  /* Return address of proper part of the buffer */
  66.924 +  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  66.925 +}
  66.926 +
  66.927 +
  66.928 +/*
  66.929 + * Release all objects belonging to a specified pool.
  66.930 + */
  66.931 +
  66.932 +METHODDEF(void)
  66.933 +free_pool (j_common_ptr cinfo, int pool_id)
  66.934 +{
  66.935 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  66.936 +  small_pool_ptr shdr_ptr;
  66.937 +  large_pool_ptr lhdr_ptr;
  66.938 +  size_t space_freed;
  66.939 +
  66.940 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  66.941 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  66.942 +
  66.943 +#ifdef MEM_STATS
  66.944 +  if (cinfo->err->trace_level > 1)
  66.945 +    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
  66.946 +#endif
  66.947 +
  66.948 +  /* If freeing IMAGE pool, close any virtual arrays first */
  66.949 +  if (pool_id == JPOOL_IMAGE) {
  66.950 +    jvirt_sarray_ptr sptr;
  66.951 +    jvirt_barray_ptr bptr;
  66.952 +
  66.953 +    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  66.954 +      if (sptr->b_s_open) {	/* there may be no backing store */
  66.955 +	sptr->b_s_open = FALSE;	/* prevent recursive close if error */
  66.956 +	(*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
  66.957 +      }
  66.958 +    }
  66.959 +    mem->virt_sarray_list = NULL;
  66.960 +    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  66.961 +      if (bptr->b_s_open) {	/* there may be no backing store */
  66.962 +	bptr->b_s_open = FALSE;	/* prevent recursive close if error */
  66.963 +	(*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
  66.964 +      }
  66.965 +    }
  66.966 +    mem->virt_barray_list = NULL;
  66.967 +  }
  66.968 +
  66.969 +  /* Release large objects */
  66.970 +  lhdr_ptr = mem->large_list[pool_id];
  66.971 +  mem->large_list[pool_id] = NULL;
  66.972 +
  66.973 +  while (lhdr_ptr != NULL) {
  66.974 +    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
  66.975 +    space_freed = lhdr_ptr->hdr.bytes_used +
  66.976 +		  lhdr_ptr->hdr.bytes_left +
  66.977 +		  SIZEOF(large_pool_hdr);
  66.978 +    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
  66.979 +    mem->total_space_allocated -= space_freed;
  66.980 +    lhdr_ptr = next_lhdr_ptr;
  66.981 +  }
  66.982 +
  66.983 +  /* Release small objects */
  66.984 +  shdr_ptr = mem->small_list[pool_id];
  66.985 +  mem->small_list[pool_id] = NULL;
  66.986 +
  66.987 +  while (shdr_ptr != NULL) {
  66.988 +    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
  66.989 +    space_freed = shdr_ptr->hdr.bytes_used +
  66.990 +		  shdr_ptr->hdr.bytes_left +
  66.991 +		  SIZEOF(small_pool_hdr);
  66.992 +    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
  66.993 +    mem->total_space_allocated -= space_freed;
  66.994 +    shdr_ptr = next_shdr_ptr;
  66.995 +  }
  66.996 +}
  66.997 +
  66.998 +
  66.999 +/*
 66.1000 + * Close up shop entirely.
 66.1001 + * Note that this cannot be called unless cinfo->mem is non-NULL.
 66.1002 + */
 66.1003 +
 66.1004 +METHODDEF(void)
 66.1005 +self_destruct (j_common_ptr cinfo)
 66.1006 +{
 66.1007 +  int pool;
 66.1008 +
 66.1009 +  /* Close all backing store, release all memory.
 66.1010 +   * Releasing pools in reverse order might help avoid fragmentation
 66.1011 +   * with some (brain-damaged) malloc libraries.
 66.1012 +   */
 66.1013 +  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
 66.1014 +    free_pool(cinfo, pool);
 66.1015 +  }
 66.1016 +
 66.1017 +  /* Release the memory manager control block too. */
 66.1018 +  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
 66.1019 +  cinfo->mem = NULL;		/* ensures I will be called only once */
 66.1020 +
 66.1021 +  jpeg_mem_term(cinfo);		/* system-dependent cleanup */
 66.1022 +}
 66.1023 +
 66.1024 +
 66.1025 +/*
 66.1026 + * Memory manager initialization.
 66.1027 + * When this is called, only the error manager pointer is valid in cinfo!
 66.1028 + */
 66.1029 +
 66.1030 +GLOBAL(void)
 66.1031 +jinit_memory_mgr (j_common_ptr cinfo)
 66.1032 +{
 66.1033 +  my_mem_ptr mem;
 66.1034 +  long max_to_use;
 66.1035 +  int pool;
 66.1036 +  size_t test_mac;
 66.1037 +
 66.1038 +  cinfo->mem = NULL;		/* for safety if init fails */
 66.1039 +
 66.1040 +  /* Check for configuration errors.
 66.1041 +   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
 66.1042 +   * doesn't reflect any real hardware alignment requirement.
 66.1043 +   * The test is a little tricky: for X>0, X and X-1 have no one-bits
 66.1044 +   * in common if and only if X is a power of 2, ie has only one one-bit.
 66.1045 +   * Some compilers may give an "unreachable code" warning here; ignore it.
 66.1046 +   */
 66.1047 +  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
 66.1048 +    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
 66.1049 +  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
 66.1050 +   * a multiple of SIZEOF(ALIGN_TYPE).
 66.1051 +   * Again, an "unreachable code" warning may be ignored here.
 66.1052 +   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
 66.1053 +   */
 66.1054 +  test_mac = (size_t) MAX_ALLOC_CHUNK;
 66.1055 +  if ((long) test_mac != MAX_ALLOC_CHUNK ||
 66.1056 +      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
 66.1057 +    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
 66.1058 +
 66.1059 +  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
 66.1060 +
 66.1061 +  /* Attempt to allocate memory manager's control block */
 66.1062 +  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
 66.1063 +
 66.1064 +  if (mem == NULL) {
 66.1065 +    jpeg_mem_term(cinfo);	/* system-dependent cleanup */
 66.1066 +    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
 66.1067 +  }
 66.1068 +
 66.1069 +  /* OK, fill in the method pointers */
 66.1070 +  mem->pub.alloc_small = alloc_small;
 66.1071 +  mem->pub.alloc_large = alloc_large;
 66.1072 +  mem->pub.alloc_sarray = alloc_sarray;
 66.1073 +  mem->pub.alloc_barray = alloc_barray;
 66.1074 +  mem->pub.request_virt_sarray = request_virt_sarray;
 66.1075 +  mem->pub.request_virt_barray = request_virt_barray;
 66.1076 +  mem->pub.realize_virt_arrays = realize_virt_arrays;
 66.1077 +  mem->pub.access_virt_sarray = access_virt_sarray;
 66.1078 +  mem->pub.access_virt_barray = access_virt_barray;
 66.1079 +  mem->pub.free_pool = free_pool;
 66.1080 +  mem->pub.self_destruct = self_destruct;
 66.1081 +
 66.1082 +  /* Make MAX_ALLOC_CHUNK accessible to other modules */
 66.1083 +  mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
 66.1084 +
 66.1085 +  /* Initialize working state */
 66.1086 +  mem->pub.max_memory_to_use = max_to_use;
 66.1087 +
 66.1088 +  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
 66.1089 +    mem->small_list[pool] = NULL;
 66.1090 +    mem->large_list[pool] = NULL;
 66.1091 +  }
 66.1092 +  mem->virt_sarray_list = NULL;
 66.1093 +  mem->virt_barray_list = NULL;
 66.1094 +
 66.1095 +  mem->total_space_allocated = SIZEOF(my_memory_mgr);
 66.1096 +
 66.1097 +  /* Declare ourselves open for business */
 66.1098 +  cinfo->mem = & mem->pub;
 66.1099 +
 66.1100 +  /* Check for an environment variable JPEGMEM; if found, override the
 66.1101 +   * default max_memory setting from jpeg_mem_init.  Note that the
 66.1102 +   * surrounding application may again override this value.
 66.1103 +   * If your system doesn't support getenv(), define NO_GETENV to disable
 66.1104 +   * this feature.
 66.1105 +   */
 66.1106 +#ifndef NO_GETENV
 66.1107 +  { char * memenv;
 66.1108 +
 66.1109 +    if ((memenv = getenv("JPEGMEM")) != NULL) {
 66.1110 +      char ch = 'x';
 66.1111 +
 66.1112 +      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
 66.1113 +	if (ch == 'm' || ch == 'M')
 66.1114 +	  max_to_use *= 1000L;
 66.1115 +	mem->pub.max_memory_to_use = max_to_use * 1000L;
 66.1116 +      }
 66.1117 +    }
 66.1118 +  }
 66.1119 +#endif
 66.1120 +
 66.1121 +}
    67.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    67.2 +++ b/libs/libjpeg/jmemnobs.c	Sun Jun 07 17:25:49 2015 +0300
    67.3 @@ -0,0 +1,109 @@
    67.4 +/*
    67.5 + * jmemnobs.c
    67.6 + *
    67.7 + * Copyright (C) 1992-1996, Thomas G. Lane.
    67.8 + * This file is part of the Independent JPEG Group's software.
    67.9 + * For conditions of distribution and use, see the accompanying README file.
   67.10 + *
   67.11 + * This file provides a really simple implementation of the system-
   67.12 + * dependent portion of the JPEG memory manager.  This implementation
   67.13 + * assumes that no backing-store files are needed: all required space
   67.14 + * can be obtained from malloc().
   67.15 + * This is very portable in the sense that it'll compile on almost anything,
   67.16 + * but you'd better have lots of main memory (or virtual memory) if you want
   67.17 + * to process big images.
   67.18 + * Note that the max_memory_to_use option is ignored by this implementation.
   67.19 + */
   67.20 +
   67.21 +#define JPEG_INTERNALS
   67.22 +#include "jinclude.h"
   67.23 +#include "jpeglib.h"
   67.24 +#include "jmemsys.h"		/* import the system-dependent declarations */
   67.25 +
   67.26 +#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
   67.27 +extern void * malloc JPP((size_t size));
   67.28 +extern void free JPP((void *ptr));
   67.29 +#endif
   67.30 +
   67.31 +
   67.32 +/*
   67.33 + * Memory allocation and freeing are controlled by the regular library
   67.34 + * routines malloc() and free().
   67.35 + */
   67.36 +
   67.37 +GLOBAL(void *)
   67.38 +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
   67.39 +{
   67.40 +  return (void *) malloc(sizeofobject);
   67.41 +}
   67.42 +
   67.43 +GLOBAL(void)
   67.44 +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
   67.45 +{
   67.46 +  free(object);
   67.47 +}
   67.48 +
   67.49 +
   67.50 +/*
   67.51 + * "Large" objects are treated the same as "small" ones.
   67.52 + * NB: although we include FAR keywords in the routine declarations,
   67.53 + * this file won't actually work in 80x86 small/medium model; at least,
   67.54 + * you probably won't be able to process useful-size images in only 64KB.
   67.55 + */
   67.56 +
   67.57 +GLOBAL(void FAR *)
   67.58 +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
   67.59 +{
   67.60 +  return (void FAR *) malloc(sizeofobject);
   67.61 +}
   67.62 +
   67.63 +GLOBAL(void)
   67.64 +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
   67.65 +{
   67.66 +  free(object);
   67.67 +}
   67.68 +
   67.69 +
   67.70 +/*
   67.71 + * This routine computes the total memory space available for allocation.
   67.72 + * Here we always say, "we got all you want bud!"
   67.73 + */
   67.74 +
   67.75 +GLOBAL(long)
   67.76 +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
   67.77 +		    long max_bytes_needed, long already_allocated)
   67.78 +{
   67.79 +  return max_bytes_needed;
   67.80 +}
   67.81 +
   67.82 +
   67.83 +/*
   67.84 + * Backing store (temporary file) management.
   67.85 + * Since jpeg_mem_available always promised the moon,
   67.86 + * this should never be called and we can just error out.
   67.87 + */
   67.88 +
   67.89 +GLOBAL(void)
   67.90 +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
   67.91 +			 long total_bytes_needed)
   67.92 +{
   67.93 +  ERREXIT(cinfo, JERR_NO_BACKING_STORE);
   67.94 +}
   67.95 +
   67.96 +
   67.97 +/*
   67.98 + * These routines take care of any system-dependent initialization and
   67.99 + * cleanup required.  Here, there isn't any.
  67.100 + */
  67.101 +
  67.102 +GLOBAL(long)
  67.103 +jpeg_mem_init (j_common_ptr cinfo)
  67.104 +{
  67.105 +  return 0;			/* just set max_memory_to_use to 0 */
  67.106 +}
  67.107 +
  67.108 +GLOBAL(void)
  67.109 +jpeg_mem_term (j_common_ptr cinfo)
  67.110 +{
  67.111 +  /* no work */
  67.112 +}
    68.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    68.2 +++ b/libs/libjpeg/jmemsys.h	Sun Jun 07 17:25:49 2015 +0300
    68.3 @@ -0,0 +1,198 @@
    68.4 +/*
    68.5 + * jmemsys.h
    68.6 + *
    68.7 + * Copyright (C) 1992-1997, Thomas G. Lane.
    68.8 + * This file is part of the Independent JPEG Group's software.
    68.9 + * For conditions of distribution and use, see the accompanying README file.
   68.10 + *
   68.11 + * This include file defines the interface between the system-independent
   68.12 + * and system-dependent portions of the JPEG memory manager.  No other
   68.13 + * modules need include it.  (The system-independent portion is jmemmgr.c;
   68.14 + * there are several different versions of the system-dependent portion.)
   68.15 + *
   68.16 + * This file works as-is for the system-dependent memory managers supplied
   68.17 + * in the IJG distribution.  You may need to modify it if you write a
   68.18 + * custom memory manager.  If system-dependent changes are needed in
   68.19 + * this file, the best method is to #ifdef them based on a configuration
   68.20 + * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
   68.21 + * and USE_MAC_MEMMGR.
   68.22 + */
   68.23 +
   68.24 +
   68.25 +/* Short forms of external names for systems with brain-damaged linkers. */
   68.26 +
   68.27 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   68.28 +#define jpeg_get_small		jGetSmall
   68.29 +#define jpeg_free_small		jFreeSmall
   68.30 +#define jpeg_get_large		jGetLarge
   68.31 +#define jpeg_free_large		jFreeLarge
   68.32 +#define jpeg_mem_available	jMemAvail
   68.33 +#define jpeg_open_backing_store	jOpenBackStore
   68.34 +#define jpeg_mem_init		jMemInit
   68.35 +#define jpeg_mem_term		jMemTerm
   68.36 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   68.37 +
   68.38 +
   68.39 +/*
   68.40 + * These two functions are used to allocate and release small chunks of
   68.41 + * memory.  (Typically the total amount requested through jpeg_get_small is
   68.42 + * no more than 20K or so; this will be requested in chunks of a few K each.)
   68.43 + * Behavior should be the same as for the standard library functions malloc
   68.44 + * and free; in particular, jpeg_get_small must return NULL on failure.
   68.45 + * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
   68.46 + * size of the object being freed, just in case it's needed.
   68.47 + * On an 80x86 machine using small-data memory model, these manage near heap.
   68.48 + */
   68.49 +
   68.50 +EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
   68.51 +EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
   68.52 +				  size_t sizeofobject));
   68.53 +
   68.54 +/*
   68.55 + * These two functions are used to allocate and release large chunks of
   68.56 + * memory (up to the total free space designated by jpeg_mem_available).
   68.57 + * The interface is the same as above, except that on an 80x86 machine,
   68.58 + * far pointers are used.  On most other machines these are identical to
   68.59 + * the jpeg_get/free_small routines; but we keep them separate anyway,
   68.60 + * in case a different allocation strategy is desirable for large chunks.
   68.61 + */
   68.62 +
   68.63 +EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
   68.64 +				       size_t sizeofobject));
   68.65 +EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
   68.66 +				  size_t sizeofobject));
   68.67 +
   68.68 +/*
   68.69 + * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
   68.70 + * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
   68.71 + * matter, but that case should never come into play).  This macro is needed
   68.72 + * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
   68.73 + * On those machines, we expect that jconfig.h will provide a proper value.
   68.74 + * On machines with 32-bit flat address spaces, any large constant may be used.
   68.75 + *
   68.76 + * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
   68.77 + * size_t and will be a multiple of sizeof(align_type).
   68.78 + */
   68.79 +
   68.80 +#ifndef MAX_ALLOC_CHUNK		/* may be overridden in jconfig.h */
   68.81 +#define MAX_ALLOC_CHUNK  1000000000L
   68.82 +#endif
   68.83 +
   68.84 +/*
   68.85 + * This routine computes the total space still available for allocation by
   68.86 + * jpeg_get_large.  If more space than this is needed, backing store will be
   68.87 + * used.  NOTE: any memory already allocated must not be counted.
   68.88 + *
   68.89 + * There is a minimum space requirement, corresponding to the minimum
   68.90 + * feasible buffer sizes; jmemmgr.c will request that much space even if
   68.91 + * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
   68.92 + * all working storage in memory, is also passed in case it is useful.
   68.93 + * Finally, the total space already allocated is passed.  If no better
   68.94 + * method is available, cinfo->mem->max_memory_to_use - already_allocated
   68.95 + * is often a suitable calculation.
   68.96 + *
   68.97 + * It is OK for jpeg_mem_available to underestimate the space available
   68.98 + * (that'll just lead to more backing-store access than is really necessary).
   68.99 + * However, an overestimate will lead to failure.  Hence it's wise to subtract
  68.100 + * a slop factor from the true available space.  5% should be enough.
  68.101 + *
  68.102 + * On machines with lots of virtual memory, any large constant may be returned.
  68.103 + * Conversely, zero may be returned to always use the minimum amount of memory.
  68.104 + */
  68.105 +
  68.106 +EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
  68.107 +				     long min_bytes_needed,
  68.108 +				     long max_bytes_needed,
  68.109 +				     long already_allocated));
  68.110 +
  68.111 +
  68.112 +/*
  68.113 + * This structure holds whatever state is needed to access a single
  68.114 + * backing-store object.  The read/write/close method pointers are called
  68.115 + * by jmemmgr.c to manipulate the backing-store object; all other fields
  68.116 + * are private to the system-dependent backing store routines.
  68.117 + */
  68.118 +
  68.119 +#define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */
  68.120 +
  68.121 +
  68.122 +#ifdef USE_MSDOS_MEMMGR		/* DOS-specific junk */
  68.123 +
  68.124 +typedef unsigned short XMSH;	/* type of extended-memory handles */
  68.125 +typedef unsigned short EMSH;	/* type of expanded-memory handles */
  68.126 +
  68.127 +typedef union {
  68.128 +  short file_handle;		/* DOS file handle if it's a temp file */
  68.129 +  XMSH xms_handle;		/* handle if it's a chunk of XMS */
  68.130 +  EMSH ems_handle;		/* handle if it's a chunk of EMS */
  68.131 +} handle_union;
  68.132 +
  68.133 +#endif /* USE_MSDOS_MEMMGR */
  68.134 +
  68.135 +#ifdef USE_MAC_MEMMGR		/* Mac-specific junk */
  68.136 +#include <Files.h>
  68.137 +#endif /* USE_MAC_MEMMGR */
  68.138 +
  68.139 +
  68.140 +typedef struct backing_store_struct * backing_store_ptr;
  68.141 +
  68.142 +typedef struct backing_store_struct {
  68.143 +  /* Methods for reading/writing/closing this backing-store object */
  68.144 +  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
  68.145 +				     backing_store_ptr info,
  68.146 +				     void FAR * buffer_address,
  68.147 +				     long file_offset, long byte_count));
  68.148 +  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
  68.149 +				      backing_store_ptr info,
  68.150 +				      void FAR * buffer_address,
  68.151 +				      long file_offset, long byte_count));
  68.152 +  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
  68.153 +				      backing_store_ptr info));
  68.154 +
  68.155 +  /* Private fields for system-dependent backing-store management */
  68.156 +#ifdef USE_MSDOS_MEMMGR
  68.157 +  /* For the MS-DOS manager (jmemdos.c), we need: */
  68.158 +  handle_union handle;		/* reference to backing-store storage object */
  68.159 +  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
  68.160 +#else
  68.161 +#ifdef USE_MAC_MEMMGR
  68.162 +  /* For the Mac manager (jmemmac.c), we need: */
  68.163 +  short temp_file;		/* file reference number to temp file */
  68.164 +  FSSpec tempSpec;		/* the FSSpec for the temp file */
  68.165 +  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
  68.166 +#else
  68.167 +  /* For a typical implementation with temp files, we need: */
  68.168 +  FILE * temp_file;		/* stdio reference to temp file */
  68.169 +  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
  68.170 +#endif
  68.171 +#endif
  68.172 +} backing_store_info;
  68.173 +
  68.174 +
  68.175 +/*
  68.176 + * Initial opening of a backing-store object.  This must fill in the
  68.177 + * read/write/close pointers in the object.  The read/write routines
  68.178 + * may take an error exit if the specified maximum file size is exceeded.
  68.179 + * (If jpeg_mem_available always returns a large value, this routine can
  68.180 + * just take an error exit.)
  68.181 + */
  68.182 +
  68.183 +EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
  68.184 +					  backing_store_ptr info,
  68.185 +					  long total_bytes_needed));
  68.186 +
  68.187 +
  68.188 +/*
  68.189 + * These routines take care of any system-dependent initialization and
  68.190 + * cleanup required.  jpeg_mem_init will be called before anything is
  68.191 + * allocated (and, therefore, nothing in cinfo is of use except the error
  68.192 + * manager pointer).  It should return a suitable default value for
  68.193 + * max_memory_to_use; this may subsequently be overridden by the surrounding
  68.194 + * application.  (Note that max_memory_to_use is only important if
  68.195 + * jpeg_mem_available chooses to consult it ... no one else will.)
  68.196 + * jpeg_mem_term may assume that all requested memory has been freed and that
  68.197 + * all opened backing-store objects have been closed.
  68.198 + */
  68.199 +
  68.200 +EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
  68.201 +EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
    69.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    69.2 +++ b/libs/libjpeg/jmorecfg.h	Sun Jun 07 17:25:49 2015 +0300
    69.3 @@ -0,0 +1,367 @@
    69.4 +/*
    69.5 + * jmorecfg.h
    69.6 + *
    69.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    69.8 + * This file is part of the Independent JPEG Group's software.
    69.9 + * For conditions of distribution and use, see the accompanying README file.
   69.10 + *
   69.11 + * This file contains additional configuration options that customize the
   69.12 + * JPEG software for special applications or support machine-dependent
   69.13 + * optimizations.  Most users will not need to touch this file.
   69.14 + */
   69.15 +
   69.16 +
   69.17 +/*
   69.18 + * Define BITS_IN_JSAMPLE as either
   69.19 + *   8   for 8-bit sample values (the usual setting)
   69.20 + *   12  for 12-bit sample values
   69.21 + * Only 8 and 12 are legal data precisions for lossy JPEG according to the
   69.22 + * JPEG standard, and the IJG code does not support anything else!
   69.23 + * We do not support run-time selection of data precision, sorry.
   69.24 + */
   69.25 +
   69.26 +#define BITS_IN_JSAMPLE  8	/* use 8 or 12 */
   69.27 +
   69.28 +
   69.29 +/*
   69.30 + * Maximum number of components (color channels) allowed in JPEG image.
   69.31 + * To meet the letter of the JPEG spec, set this to 255.  However, darn
   69.32 + * few applications need more than 4 channels (maybe 5 for CMYK + alpha
   69.33 + * mask).  We recommend 10 as a reasonable compromise; use 4 if you are
   69.34 + * really short on memory.  (Each allowed component costs a hundred or so
   69.35 + * bytes of storage, whether actually used in an image or not.)
   69.36 + */
   69.37 +
   69.38 +#define MAX_COMPONENTS  10	/* maximum number of image components */
   69.39 +
   69.40 +
   69.41 +/*
   69.42 + * Basic data types.
   69.43 + * You may need to change these if you have a machine with unusual data
   69.44 + * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
   69.45 + * or "long" not 32 bits.  We don't care whether "int" is 16 or 32 bits,
   69.46 + * but it had better be at least 16.
   69.47 + */
   69.48 +
   69.49 +/* Representation of a single sample (pixel element value).
   69.50 + * We frequently allocate large arrays of these, so it's important to keep
   69.51 + * them small.  But if you have memory to burn and access to char or short
   69.52 + * arrays is very slow on your hardware, you might want to change these.
   69.53 + */
   69.54 +
   69.55 +#if BITS_IN_JSAMPLE == 8
   69.56 +/* JSAMPLE should be the smallest type that will hold the values 0..255.
   69.57 + * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
   69.58 + */
   69.59 +
   69.60 +#ifdef HAVE_UNSIGNED_CHAR
   69.61 +
   69.62 +typedef unsigned char JSAMPLE;
   69.63 +#define GETJSAMPLE(value)  ((int) (value))
   69.64 +
   69.65 +#else /* not HAVE_UNSIGNED_CHAR */
   69.66 +
   69.67 +typedef char JSAMPLE;
   69.68 +#ifdef CHAR_IS_UNSIGNED
   69.69 +#define GETJSAMPLE(value)  ((int) (value))
   69.70 +#else
   69.71 +#define GETJSAMPLE(value)  ((int) (value) & 0xFF)
   69.72 +#endif /* CHAR_IS_UNSIGNED */
   69.73 +
   69.74 +#endif /* HAVE_UNSIGNED_CHAR */
   69.75 +
   69.76 +#define MAXJSAMPLE	255
   69.77 +#define CENTERJSAMPLE	128
   69.78 +
   69.79 +#endif /* BITS_IN_JSAMPLE == 8 */
   69.80 +
   69.81 +
   69.82 +#if BITS_IN_JSAMPLE == 12
   69.83 +/* JSAMPLE should be the smallest type that will hold the values 0..4095.
   69.84 + * On nearly all machines "short" will do nicely.
   69.85 + */
   69.86 +
   69.87 +typedef short JSAMPLE;
   69.88 +#define GETJSAMPLE(value)  ((int) (value))
   69.89 +
   69.90 +#define MAXJSAMPLE	4095
   69.91 +#define CENTERJSAMPLE	2048
   69.92 +
   69.93 +#endif /* BITS_IN_JSAMPLE == 12 */
   69.94 +
   69.95 +
   69.96 +/* Representation of a DCT frequency coefficient.
   69.97 + * This should be a signed value of at least 16 bits; "short" is usually OK.
   69.98 + * Again, we allocate large arrays of these, but you can change to int
   69.99 + * if you have memory to burn and "short" is really slow.
  69.100 + */
  69.101 +
  69.102 +typedef short JCOEF;
  69.103 +
  69.104 +
  69.105 +/* Compressed datastreams are represented as arrays of JOCTET.
  69.106 + * These must be EXACTLY 8 bits wide, at least once they are written to
  69.107 + * external storage.  Note that when using the stdio data source/destination
  69.108 + * managers, this is also the data type passed to fread/fwrite.
  69.109 + */
  69.110 +
  69.111 +#ifdef HAVE_UNSIGNED_CHAR
  69.112 +
  69.113 +typedef unsigned char JOCTET;
  69.114 +#define GETJOCTET(value)  (value)
  69.115 +
  69.116 +#else /* not HAVE_UNSIGNED_CHAR */
  69.117 +
  69.118 +typedef char JOCTET;
  69.119 +#ifdef CHAR_IS_UNSIGNED
  69.120 +#define GETJOCTET(value)  (value)
  69.121 +#else
  69.122 +#define GETJOCTET(value)  ((value) & 0xFF)
  69.123 +#endif /* CHAR_IS_UNSIGNED */
  69.124 +
  69.125 +#endif /* HAVE_UNSIGNED_CHAR */
  69.126 +
  69.127 +
  69.128 +/* These typedefs are used for various table entries and so forth.
  69.129 + * They must be at least as wide as specified; but making them too big
  69.130 + * won't cost a huge amount of memory, so we don't provide special
  69.131 + * extraction code like we did for JSAMPLE.  (In other words, these
  69.132 + * typedefs live at a different point on the speed/space tradeoff curve.)
  69.133 + */
  69.134 +
  69.135 +/* UINT8 must hold at least the values 0..255. */
  69.136 +
  69.137 +#ifdef HAVE_UNSIGNED_CHAR
  69.138 +typedef unsigned char UINT8;
  69.139 +#else /* not HAVE_UNSIGNED_CHAR */
  69.140 +#ifdef CHAR_IS_UNSIGNED
  69.141 +typedef char UINT8;
  69.142 +#else /* not CHAR_IS_UNSIGNED */
  69.143 +typedef short UINT8;
  69.144 +#endif /* CHAR_IS_UNSIGNED */
  69.145 +#endif /* HAVE_UNSIGNED_CHAR */
  69.146 +
  69.147 +/* UINT16 must hold at least the values 0..65535. */
  69.148 +
  69.149 +#ifdef HAVE_UNSIGNED_SHORT
  69.150 +typedef unsigned short UINT16;
  69.151 +#else /* not HAVE_UNSIGNED_SHORT */
  69.152 +typedef unsigned int UINT16;
  69.153 +#endif /* HAVE_UNSIGNED_SHORT */
  69.154 +
  69.155 +/* INT16 must hold at least the values -32768..32767. */
  69.156 +
  69.157 +#ifndef XMD_H			/* X11/xmd.h correctly defines INT16 */
  69.158 +typedef short INT16;
  69.159 +#endif
  69.160 +
  69.161 +/* INT32 must hold at least signed 32-bit values. */
  69.162 +
  69.163 +#ifndef XMD_H			/* X11/xmd.h correctly defines INT32 */
  69.164 +typedef int INT32;
  69.165 +#endif
  69.166 +
  69.167 +/* Datatype used for image dimensions.  The JPEG standard only supports
  69.168 + * images up to 64K*64K due to 16-bit fields in SOF markers.  Therefore
  69.169 + * "unsigned int" is sufficient on all machines.  However, if you need to
  69.170 + * handle larger images and you don't mind deviating from the spec, you
  69.171 + * can change this datatype.
  69.172 + */
  69.173 +
  69.174 +typedef unsigned int JDIMENSION;
  69.175 +
  69.176 +#define JPEG_MAX_DIMENSION  65500L  /* a tad under 64K to prevent overflows */
  69.177 +
  69.178 +
  69.179 +/* These macros are used in all function definitions and extern declarations.
  69.180 + * You could modify them if you need to change function linkage conventions;
  69.181 + * in particular, you'll need to do that to make the library a Windows DLL.
  69.182 + * Another application is to make all functions global for use with debuggers
  69.183 + * or code profilers that require it.
  69.184 + */
  69.185 +
  69.186 +/* a function called through method pointers: */
  69.187 +#define METHODDEF(type)		static type
  69.188 +/* a function used only in its module: */
  69.189 +#define LOCAL(type)		static type
  69.190 +/* a function referenced thru EXTERNs: */
  69.191 +#define GLOBAL(type)		type
  69.192 +/* a reference to a GLOBAL function: */
  69.193 +#define EXTERN(type)		extern type
  69.194 +
  69.195 +
  69.196 +/* This macro is used to declare a "method", that is, a function pointer.
  69.197 + * We want to supply prototype parameters if the compiler can cope.
  69.198 + * Note that the arglist parameter must be parenthesized!
  69.199 + * Again, you can customize this if you need special linkage keywords.
  69.200 + */
  69.201 +
  69.202 +#ifdef HAVE_PROTOTYPES
  69.203 +#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
  69.204 +#else
  69.205 +#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
  69.206 +#endif
  69.207 +
  69.208 +
  69.209 +/* Here is the pseudo-keyword for declaring pointers that must be "far"
  69.210 + * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  69.211 + * by just saying "FAR *" where such a pointer is needed.  In a few places
  69.212 + * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  69.213 + */
  69.214 +
  69.215 +#ifdef FAR
  69.216 +#undef FAR
  69.217 +#endif
  69.218 +
  69.219 +#ifdef NEED_FAR_POINTERS
  69.220 +#define FAR  far
  69.221 +#else
  69.222 +#define FAR
  69.223 +#endif
  69.224 +
  69.225 +
  69.226 +/*
  69.227 + * On a few systems, type boolean and/or its values FALSE, TRUE may appear
  69.228 + * in standard header files.  Or you may have conflicts with application-
  69.229 + * specific header files that you want to include together with these files.
  69.230 + * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
  69.231 + */
  69.232 +
  69.233 +#ifndef HAVE_BOOLEAN
  69.234 +typedef int boolean;
  69.235 +#endif
  69.236 +#ifndef FALSE			/* in case these macros already exist */
  69.237 +#define FALSE	0		/* values of boolean */
  69.238 +#endif
  69.239 +#ifndef TRUE
  69.240 +#define TRUE	1
  69.241 +#endif
  69.242 +
  69.243 +
  69.244 +/*
  69.245 + * The remaining options affect code selection within the JPEG library,
  69.246 + * but they don't need to be visible to most applications using the library.
  69.247 + * To minimize application namespace pollution, the symbols won't be
  69.248 + * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
  69.249 + */
  69.250 +
  69.251 +#ifdef JPEG_INTERNALS
  69.252 +#define JPEG_INTERNAL_OPTIONS
  69.253 +#endif
  69.254 +
  69.255 +#ifdef JPEG_INTERNAL_OPTIONS
  69.256 +
  69.257 +
  69.258 +/*
  69.259 + * These defines indicate whether to include various optional functions.
  69.260 + * Undefining some of these symbols will produce a smaller but less capable
  69.261 + * library.  Note that you can leave certain source files out of the
  69.262 + * compilation/linking process if you've #undef'd the corresponding symbols.
  69.263 + * (You may HAVE to do that if your compiler doesn't like null source files.)
  69.264 + */
  69.265 +
  69.266 +/* Arithmetic coding is unsupported for legal reasons.  Complaints to IBM. */
  69.267 +
  69.268 +/* Capability options common to encoder and decoder: */
  69.269 +
  69.270 +#define DCT_ISLOW_SUPPORTED	/* slow but accurate integer algorithm */
  69.271 +#define DCT_IFAST_SUPPORTED	/* faster, less accurate integer method */
  69.272 +#define DCT_FLOAT_SUPPORTED	/* floating-point: accurate, fast on fast HW */
  69.273 +
  69.274 +/* Encoder capability options: */
  69.275 +
  69.276 +#undef  C_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
  69.277 +#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
  69.278 +#define C_PROGRESSIVE_SUPPORTED	    /* Progressive JPEG? (Requires MULTISCAN)*/
  69.279 +#define ENTROPY_OPT_SUPPORTED	    /* Optimization of entropy coding parms? */
  69.280 +/* Note: if you selected 12-bit data precision, it is dangerous to turn off
  69.281 + * ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only good for 8-bit
  69.282 + * precision, so jchuff.c normally uses entropy optimization to compute
  69.283 + * usable tables for higher precision.  If you don't want to do optimization,
  69.284 + * you'll have to supply different default Huffman tables.
  69.285 + * The exact same statements apply for progressive JPEG: the default tables
  69.286 + * don't work for progressive mode.  (This may get fixed, however.)
  69.287 + */
  69.288 +#define INPUT_SMOOTHING_SUPPORTED   /* Input image smoothing option? */
  69.289 +
  69.290 +/* Decoder capability options: */
  69.291 +
  69.292 +#undef  D_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
  69.293 +#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
  69.294 +#define D_PROGRESSIVE_SUPPORTED	    /* Progressive JPEG? (Requires MULTISCAN)*/
  69.295 +#define SAVE_MARKERS_SUPPORTED	    /* jpeg_save_markers() needed? */
  69.296 +#define BLOCK_SMOOTHING_SUPPORTED   /* Block smoothing? (Progressive only) */
  69.297 +#define IDCT_SCALING_SUPPORTED	    /* Output rescaling via IDCT? */
  69.298 +#undef  UPSAMPLE_SCALING_SUPPORTED  /* Output rescaling at upsample stage? */
  69.299 +#define UPSAMPLE_MERGING_SUPPORTED  /* Fast path for sloppy upsampling? */
  69.300 +#define QUANT_1PASS_SUPPORTED	    /* 1-pass color quantization? */
  69.301 +#define QUANT_2PASS_SUPPORTED	    /* 2-pass color quantization? */
  69.302 +
  69.303 +/* more capability options later, no doubt */
  69.304 +
  69.305 +
  69.306 +/*
  69.307 + * Ordering of RGB data in scanlines passed to or from the application.
  69.308 + * If your application wants to deal with data in the order B,G,R, just
  69.309 + * change these macros.  You can also deal with formats such as R,G,B,X
  69.310 + * (one extra byte per pixel) by changing RGB_PIXELSIZE.  Note that changing
  69.311 + * the offsets will also change the order in which colormap data is organized.
  69.312 + * RESTRICTIONS:
  69.313 + * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
  69.314 + * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
  69.315 + *    useful if you are using JPEG color spaces other than YCbCr or grayscale.
  69.316 + * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
  69.317 + *    is not 3 (they don't understand about dummy color components!).  So you
  69.318 + *    can't use color quantization if you change that value.
  69.319 + */
  69.320 +
  69.321 +#define RGB_RED		0	/* Offset of Red in an RGB scanline element */
  69.322 +#define RGB_GREEN	1	/* Offset of Green */
  69.323 +#define RGB_BLUE	2	/* Offset of Blue */
  69.324 +#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */
  69.325 +
  69.326 +
  69.327 +/* Definitions for speed-related optimizations. */
  69.328 +
  69.329 +
  69.330 +/* If your compiler supports inline functions, define INLINE
  69.331 + * as the inline keyword; otherwise define it as empty.
  69.332 + */
  69.333 +
  69.334 +#ifndef INLINE
  69.335 +#ifdef __GNUC__			/* for instance, GNU C knows about inline */
  69.336 +#define INLINE __inline__
  69.337 +#endif
  69.338 +#ifndef INLINE
  69.339 +#define INLINE			/* default is to define it as empty */
  69.340 +#endif
  69.341 +#endif
  69.342 +
  69.343 +
  69.344 +/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
  69.345 + * two 16-bit shorts is faster than multiplying two ints.  Define MULTIPLIER
  69.346 + * as short on such a machine.  MULTIPLIER must be at least 16 bits wide.
  69.347 + */
  69.348 +
  69.349 +#ifndef MULTIPLIER
  69.350 +#define MULTIPLIER  int		/* type for fastest integer multiply */
  69.351 +#endif
  69.352 +
  69.353 +
  69.354 +/* FAST_FLOAT should be either float or double, whichever is done faster
  69.355 + * by your compiler.  (Note that this type is only used in the floating point
  69.356 + * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
  69.357 + * Typically, float is faster in ANSI C compilers, while double is faster in
  69.358 + * pre-ANSI compilers (because they insist on converting to double anyway).
  69.359 + * The code below therefore chooses float if we have ANSI-style prototypes.
  69.360 + */
  69.361 +
  69.362 +#ifndef FAST_FLOAT
  69.363 +#ifdef HAVE_PROTOTYPES
  69.364 +#define FAST_FLOAT  float
  69.365 +#else
  69.366 +#define FAST_FLOAT  double
  69.367 +#endif
  69.368 +#endif
  69.369 +
  69.370 +#endif /* JPEG_INTERNAL_OPTIONS */
    70.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    70.2 +++ b/libs/libjpeg/jpegint.h	Sun Jun 07 17:25:49 2015 +0300
    70.3 @@ -0,0 +1,392 @@
    70.4 +/*
    70.5 + * jpegint.h
    70.6 + *
    70.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    70.8 + * This file is part of the Independent JPEG Group's software.
    70.9 + * For conditions of distribution and use, see the accompanying README file.
   70.10 + *
   70.11 + * This file provides common declarations for the various JPEG modules.
   70.12 + * These declarations are considered internal to the JPEG library; most
   70.13 + * applications using the library shouldn't need to include this file.
   70.14 + */
   70.15 +
   70.16 +
   70.17 +/* Declarations for both compression & decompression */
   70.18 +
   70.19 +typedef enum {			/* Operating modes for buffer controllers */
   70.20 +	JBUF_PASS_THRU,		/* Plain stripwise operation */
   70.21 +	/* Remaining modes require a full-image buffer to have been created */
   70.22 +	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
   70.23 +	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
   70.24 +	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
   70.25 +} J_BUF_MODE;
   70.26 +
   70.27 +/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
   70.28 +#define CSTATE_START	100	/* after create_compress */
   70.29 +#define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
   70.30 +#define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
   70.31 +#define CSTATE_WRCOEFS	103	/* jpeg_write_coefficients done */
   70.32 +#define DSTATE_START	200	/* after create_decompress */
   70.33 +#define DSTATE_INHEADER	201	/* reading header markers, no SOS yet */
   70.34 +#define DSTATE_READY	202	/* found SOS, ready for start_decompress */
   70.35 +#define DSTATE_PRELOAD	203	/* reading multiscan file in start_decompress*/
   70.36 +#define DSTATE_PRESCAN	204	/* performing dummy pass for 2-pass quant */
   70.37 +#define DSTATE_SCANNING	205	/* start_decompress done, read_scanlines OK */
   70.38 +#define DSTATE_RAW_OK	206	/* start_decompress done, read_raw_data OK */
   70.39 +#define DSTATE_BUFIMAGE	207	/* expecting jpeg_start_output */
   70.40 +#define DSTATE_BUFPOST	208	/* looking for SOS/EOI in jpeg_finish_output */
   70.41 +#define DSTATE_RDCOEFS	209	/* reading file in jpeg_read_coefficients */
   70.42 +#define DSTATE_STOPPING	210	/* looking for EOI in jpeg_finish_decompress */
   70.43 +
   70.44 +
   70.45 +/* Declarations for compression modules */
   70.46 +
   70.47 +/* Master control module */
   70.48 +struct jpeg_comp_master {
   70.49 +  JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
   70.50 +  JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
   70.51 +  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
   70.52 +
   70.53 +  /* State variables made visible to other modules */
   70.54 +  boolean call_pass_startup;	/* True if pass_startup must be called */
   70.55 +  boolean is_last_pass;		/* True during last pass */
   70.56 +};
   70.57 +
   70.58 +/* Main buffer control (downsampled-data buffer) */
   70.59 +struct jpeg_c_main_controller {
   70.60 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   70.61 +  JMETHOD(void, process_data, (j_compress_ptr cinfo,
   70.62 +			       JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
   70.63 +			       JDIMENSION in_rows_avail));
   70.64 +};
   70.65 +
   70.66 +/* Compression preprocessing (downsampling input buffer control) */
   70.67 +struct jpeg_c_prep_controller {
   70.68 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   70.69 +  JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
   70.70 +				   JSAMPARRAY input_buf,
   70.71 +				   JDIMENSION *in_row_ctr,
   70.72 +				   JDIMENSION in_rows_avail,
   70.73 +				   JSAMPIMAGE output_buf,
   70.74 +				   JDIMENSION *out_row_group_ctr,
   70.75 +				   JDIMENSION out_row_groups_avail));
   70.76 +};
   70.77 +
   70.78 +/* Coefficient buffer control */
   70.79 +struct jpeg_c_coef_controller {
   70.80 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   70.81 +  JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
   70.82 +				   JSAMPIMAGE input_buf));
   70.83 +};
   70.84 +
   70.85 +/* Colorspace conversion */
   70.86 +struct jpeg_color_converter {
   70.87 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
   70.88 +  JMETHOD(void, color_convert, (j_compress_ptr cinfo,
   70.89 +				JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
   70.90 +				JDIMENSION output_row, int num_rows));
   70.91 +};
   70.92 +
   70.93 +/* Downsampling */
   70.94 +struct jpeg_downsampler {
   70.95 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
   70.96 +  JMETHOD(void, downsample, (j_compress_ptr cinfo,
   70.97 +			     JSAMPIMAGE input_buf, JDIMENSION in_row_index,
   70.98 +			     JSAMPIMAGE output_buf,
   70.99 +			     JDIMENSION out_row_group_index));
  70.100 +
  70.101 +  boolean need_context_rows;	/* TRUE if need rows above & below */
  70.102 +};
  70.103 +
  70.104 +/* Forward DCT (also controls coefficient quantization) */
  70.105 +struct jpeg_forward_dct {
  70.106 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  70.107 +  /* perhaps this should be an array??? */
  70.108 +  JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
  70.109 +			      jpeg_component_info * compptr,
  70.110 +			      JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  70.111 +			      JDIMENSION start_row, JDIMENSION start_col,
  70.112 +			      JDIMENSION num_blocks));
  70.113 +};
  70.114 +
  70.115 +/* Entropy encoding */
  70.116 +struct jpeg_entropy_encoder {
  70.117 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
  70.118 +  JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
  70.119 +  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
  70.120 +};
  70.121 +
  70.122 +/* Marker writing */
  70.123 +struct jpeg_marker_writer {
  70.124 +  JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
  70.125 +  JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
  70.126 +  JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
  70.127 +  JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
  70.128 +  JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
  70.129 +  /* These routines are exported to allow insertion of extra markers */
  70.130 +  /* Probably only COM and APPn markers should be written this way */
  70.131 +  JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
  70.132 +				      unsigned int datalen));
  70.133 +  JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
  70.134 +};
  70.135 +
  70.136 +
  70.137 +/* Declarations for decompression modules */
  70.138 +
  70.139 +/* Master control module */
  70.140 +struct jpeg_decomp_master {
  70.141 +  JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
  70.142 +  JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
  70.143 +
  70.144 +  /* State variables made visible to other modules */
  70.145 +  boolean is_dummy_pass;	/* True during 1st pass for 2-pass quant */
  70.146 +};
  70.147 +
  70.148 +/* Input control module */
  70.149 +struct jpeg_input_controller {
  70.150 +  JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
  70.151 +  JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
  70.152 +  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  70.153 +  JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
  70.154 +
  70.155 +  /* State variables made visible to other modules */
  70.156 +  boolean has_multiple_scans;	/* True if file has multiple scans */
  70.157 +  boolean eoi_reached;		/* True when EOI has been consumed */
  70.158 +};
  70.159 +
  70.160 +/* Main buffer control (downsampled-data buffer) */
  70.161 +struct jpeg_d_main_controller {
  70.162 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  70.163 +  JMETHOD(void, process_data, (j_decompress_ptr cinfo,
  70.164 +			       JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  70.165 +			       JDIMENSION out_rows_avail));
  70.166 +};
  70.167 +
  70.168 +/* Coefficient buffer control */
  70.169 +struct jpeg_d_coef_controller {
  70.170 +  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  70.171 +  JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
  70.172 +  JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
  70.173 +  JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
  70.174 +				 JSAMPIMAGE output_buf));
  70.175 +  /* Pointer to array of coefficient virtual arrays, or NULL if none */
  70.176 +  jvirt_barray_ptr *coef_arrays;
  70.177 +};
  70.178 +
  70.179 +/* Decompression postprocessing (color quantization buffer control) */
  70.180 +struct jpeg_d_post_controller {
  70.181 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  70.182 +  JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
  70.183 +				    JSAMPIMAGE input_buf,
  70.184 +				    JDIMENSION *in_row_group_ctr,
  70.185 +				    JDIMENSION in_row_groups_avail,
  70.186 +				    JSAMPARRAY output_buf,
  70.187 +				    JDIMENSION *out_row_ctr,
  70.188 +				    JDIMENSION out_rows_avail));
  70.189 +};
  70.190 +
  70.191 +/* Marker reading & parsing */
  70.192 +struct jpeg_marker_reader {
  70.193 +  JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
  70.194 +  /* Read markers until SOS or EOI.
  70.195 +   * Returns same codes as are defined for jpeg_consume_input:
  70.196 +   * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  70.197 +   */
  70.198 +  JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
  70.199 +  /* Read a restart marker --- exported for use by entropy decoder only */
  70.200 +  jpeg_marker_parser_method read_restart_marker;
  70.201 +
  70.202 +  /* State of marker reader --- nominally internal, but applications
  70.203 +   * supplying COM or APPn handlers might like to know the state.
  70.204 +   */
  70.205 +  boolean saw_SOI;		/* found SOI? */
  70.206 +  boolean saw_SOF;		/* found SOF? */
  70.207 +  int next_restart_num;		/* next restart number expected (0-7) */
  70.208 +  unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
  70.209 +};
  70.210 +
  70.211 +/* Entropy decoding */
  70.212 +struct jpeg_entropy_decoder {
  70.213 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  70.214 +  JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
  70.215 +				JBLOCKROW *MCU_data));
  70.216 +
  70.217 +  /* This is here to share code between baseline and progressive decoders; */
  70.218 +  /* other modules probably should not use it */
  70.219 +  boolean insufficient_data;	/* set TRUE after emitting warning */
  70.220 +};
  70.221 +
  70.222 +/* Inverse DCT (also performs dequantization) */
  70.223 +typedef JMETHOD(void, inverse_DCT_method_ptr,
  70.224 +		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
  70.225 +		 JCOEFPTR coef_block,
  70.226 +		 JSAMPARRAY output_buf, JDIMENSION output_col));
  70.227 +
  70.228 +struct jpeg_inverse_dct {
  70.229 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  70.230 +  /* It is useful to allow each component to have a separate IDCT method. */
  70.231 +  inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
  70.232 +};
  70.233 +
  70.234 +/* Upsampling (note that upsampler must also call color converter) */
  70.235 +struct jpeg_upsampler {
  70.236 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  70.237 +  JMETHOD(void, upsample, (j_decompress_ptr cinfo,
  70.238 +			   JSAMPIMAGE input_buf,
  70.239 +			   JDIMENSION *in_row_group_ctr,
  70.240 +			   JDIMENSION in_row_groups_avail,
  70.241 +			   JSAMPARRAY output_buf,
  70.242 +			   JDIMENSION *out_row_ctr,
  70.243 +			   JDIMENSION out_rows_avail));
  70.244 +
  70.245 +  boolean need_context_rows;	/* TRUE if need rows above & below */
  70.246 +};
  70.247 +
  70.248 +/* Colorspace conversion */
  70.249 +struct jpeg_color_deconverter {
  70.250 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  70.251 +  JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
  70.252 +				JSAMPIMAGE input_buf, JDIMENSION input_row,
  70.253 +				JSAMPARRAY output_buf, int num_rows));
  70.254 +};
  70.255 +
  70.256 +/* Color quantization or color precision reduction */
  70.257 +struct jpeg_color_quantizer {
  70.258 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
  70.259 +  JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
  70.260 +				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
  70.261 +				 int num_rows));
  70.262 +  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
  70.263 +  JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
  70.264 +};
  70.265 +
  70.266 +
  70.267 +/* Miscellaneous useful macros */
  70.268 +
  70.269 +#undef MAX
  70.270 +#define MAX(a,b)	((a) > (b) ? (a) : (b))
  70.271 +#undef MIN
  70.272 +#define MIN(a,b)	((a) < (b) ? (a) : (b))
  70.273 +
  70.274 +
  70.275 +/* We assume that right shift corresponds to signed division by 2 with
  70.276 + * rounding towards minus infinity.  This is correct for typical "arithmetic
  70.277 + * shift" instructions that shift in copies of the sign bit.  But some
  70.278 + * C compilers implement >> with an unsigned shift.  For these machines you
  70.279 + * must define RIGHT_SHIFT_IS_UNSIGNED.
  70.280 + * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  70.281 + * It is only applied with constant shift counts.  SHIFT_TEMPS must be
  70.282 + * included in the variables of any routine using RIGHT_SHIFT.
  70.283 + */
  70.284 +
  70.285 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
  70.286 +#define SHIFT_TEMPS	INT32 shift_temp;
  70.287 +#define RIGHT_SHIFT(x,shft)  \
  70.288 +	((shift_temp = (x)) < 0 ? \
  70.289 +	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  70.290 +	 (shift_temp >> (shft)))
  70.291 +#else
  70.292 +#define SHIFT_TEMPS
  70.293 +#define RIGHT_SHIFT(x,shft)	((x) >> (shft))
  70.294 +#endif
  70.295 +
  70.296 +
  70.297 +/* Short forms of external names for systems with brain-damaged linkers. */
  70.298 +
  70.299 +#ifdef NEED_SHORT_EXTERNAL_NAMES
  70.300 +#define jinit_compress_master	jICompress
  70.301 +#define jinit_c_master_control	jICMaster
  70.302 +#define jinit_c_main_controller	jICMainC
  70.303 +#define jinit_c_prep_controller	jICPrepC
  70.304 +#define jinit_c_coef_controller	jICCoefC
  70.305 +#define jinit_color_converter	jICColor
  70.306 +#define jinit_downsampler	jIDownsampler
  70.307 +#define jinit_forward_dct	jIFDCT
  70.308 +#define jinit_huff_encoder	jIHEncoder
  70.309 +#define jinit_phuff_encoder	jIPHEncoder
  70.310 +#define jinit_marker_writer	jIMWriter
  70.311 +#define jinit_master_decompress	jIDMaster
  70.312 +#define jinit_d_main_controller	jIDMainC
  70.313 +#define jinit_d_coef_controller	jIDCoefC
  70.314 +#define jinit_d_post_controller	jIDPostC
  70.315 +#define jinit_input_controller	jIInCtlr
  70.316 +#define jinit_marker_reader	jIMReader
  70.317 +#define jinit_huff_decoder	jIHDecoder
  70.318 +#define jinit_phuff_decoder	jIPHDecoder
  70.319 +#define jinit_inverse_dct	jIIDCT
  70.320 +#define jinit_upsampler		jIUpsampler
  70.321 +#define jinit_color_deconverter	jIDColor
  70.322 +#define jinit_1pass_quantizer	jI1Quant
  70.323 +#define jinit_2pass_quantizer	jI2Quant
  70.324 +#define jinit_merged_upsampler	jIMUpsampler
  70.325 +#define jinit_memory_mgr	jIMemMgr
  70.326 +#define jdiv_round_up		jDivRound
  70.327 +#define jround_up		jRound
  70.328 +#define jcopy_sample_rows	jCopySamples
  70.329 +#define jcopy_block_row		jCopyBlocks
  70.330 +#define jzero_far		jZeroFar
  70.331 +#define jpeg_zigzag_order	jZIGTable
  70.332 +#define jpeg_natural_order	jZAGTable
  70.333 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
  70.334 +
  70.335 +
  70.336 +/* Compression module initialization routines */
  70.337 +EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
  70.338 +EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
  70.339 +					 boolean transcode_only));
  70.340 +EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
  70.341 +					  boolean need_full_buffer));
  70.342 +EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
  70.343 +					  boolean need_full_buffer));
  70.344 +EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
  70.345 +					  boolean need_full_buffer));
  70.346 +EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
  70.347 +EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
  70.348 +EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
  70.349 +EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
  70.350 +EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
  70.351 +EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
  70.352 +/* Decompression module initialization routines */
  70.353 +EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
  70.354 +EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
  70.355 +					  boolean need_full_buffer));
  70.356 +EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
  70.357 +					  boolean need_full_buffer));
  70.358 +EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
  70.359 +					  boolean need_full_buffer));
  70.360 +EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
  70.361 +EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
  70.362 +EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
  70.363 +EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
  70.364 +EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
  70.365 +EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
  70.366 +EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
  70.367 +EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
  70.368 +EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
  70.369 +EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
  70.370 +/* Memory manager initialization */
  70.371 +EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
  70.372 +
  70.373 +/* Utility routines in jutils.c */
  70.374 +EXTERN(long) jdiv_round_up JPP((long a, long b));
  70.375 +EXTERN(long) jround_up JPP((long a, long b));
  70.376 +EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
  70.377 +				    JSAMPARRAY output_array, int dest_row,
  70.378 +				    int num_rows, JDIMENSION num_cols));
  70.379 +EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
  70.380 +				  JDIMENSION num_blocks));
  70.381 +EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
  70.382 +/* Constant tables in jutils.c */
  70.383 +#if 0				/* This table is not actually needed in v6a */
  70.384 +extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
  70.385 +#endif
  70.386 +extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
  70.387 +
  70.388 +/* Suppress undefined-structure complaints if necessary. */
  70.389 +
  70.390 +#ifdef INCOMPLETE_TYPES_BROKEN
  70.391 +#ifndef AM_MEMORY_MANAGER	/* only jmemmgr.c defines these */
  70.392 +struct jvirt_sarray_control { long dummy; };
  70.393 +struct jvirt_barray_control { long dummy; };
  70.394 +#endif
  70.395 +#endif /* INCOMPLETE_TYPES_BROKEN */
    71.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    71.2 +++ b/libs/libjpeg/jpeglib.h	Sun Jun 07 17:25:49 2015 +0300
    71.3 @@ -0,0 +1,1096 @@
    71.4 +/*
    71.5 + * jpeglib.h
    71.6 + *
    71.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    71.8 + * This file is part of the Independent JPEG Group's software.
    71.9 + * For conditions of distribution and use, see the accompanying README file.
   71.10 + *
   71.11 + * This file defines the application interface for the JPEG library.
   71.12 + * Most applications using the library need only include this file,
   71.13 + * and perhaps jerror.h if they want to know the exact error codes.
   71.14 + */
   71.15 +
   71.16 +#ifndef JPEGLIB_H
   71.17 +#define JPEGLIB_H
   71.18 +
   71.19 +/*
   71.20 + * First we include the configuration files that record how this
   71.21 + * installation of the JPEG library is set up.  jconfig.h can be
   71.22 + * generated automatically for many systems.  jmorecfg.h contains
   71.23 + * manual configuration options that most people need not worry about.
   71.24 + */
   71.25 +
   71.26 +#ifndef JCONFIG_INCLUDED	/* in case jinclude.h already did */
   71.27 +#include "jconfig.h"		/* widely used configuration options */
   71.28 +#endif
   71.29 +#include "jmorecfg.h"		/* seldom changed options */
   71.30 +
   71.31 +
   71.32 +/* Version ID for the JPEG library.
   71.33 + * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
   71.34 + */
   71.35 +
   71.36 +#define JPEG_LIB_VERSION  62	/* Version 6b */
   71.37 +
   71.38 +
   71.39 +/* Various constants determining the sizes of things.
   71.40 + * All of these are specified by the JPEG standard, so don't change them
   71.41 + * if you want to be compatible.
   71.42 + */
   71.43 +
   71.44 +#define DCTSIZE		    8	/* The basic DCT block is 8x8 samples */
   71.45 +#define DCTSIZE2	    64	/* DCTSIZE squared; # of elements in a block */
   71.46 +#define NUM_QUANT_TBLS      4	/* Quantization tables are numbered 0..3 */
   71.47 +#define NUM_HUFF_TBLS       4	/* Huffman tables are numbered 0..3 */
   71.48 +#define NUM_ARITH_TBLS      16	/* Arith-coding tables are numbered 0..15 */
   71.49 +#define MAX_COMPS_IN_SCAN   4	/* JPEG limit on # of components in one scan */
   71.50 +#define MAX_SAMP_FACTOR     4	/* JPEG limit on sampling factors */
   71.51 +/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard;
   71.52 + * the PostScript DCT filter can emit files with many more than 10 blocks/MCU.
   71.53 + * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU
   71.54 + * to handle it.  We even let you do this from the jconfig.h file.  However,
   71.55 + * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe
   71.56 + * sometimes emits noncompliant files doesn't mean you should too.
   71.57 + */
   71.58 +#define C_MAX_BLOCKS_IN_MCU   10 /* compressor's limit on blocks per MCU */
   71.59 +#ifndef D_MAX_BLOCKS_IN_MCU
   71.60 +#define D_MAX_BLOCKS_IN_MCU   10 /* decompressor's limit on blocks per MCU */
   71.61 +#endif
   71.62 +
   71.63 +
   71.64 +/* Data structures for images (arrays of samples and of DCT coefficients).
   71.65 + * On 80x86 machines, the image arrays are too big for near pointers,
   71.66 + * but the pointer arrays can fit in near memory.
   71.67 + */
   71.68 +
   71.69 +typedef JSAMPLE FAR *JSAMPROW;	/* ptr to one image row of pixel samples. */
   71.70 +typedef JSAMPROW *JSAMPARRAY;	/* ptr to some rows (a 2-D sample array) */
   71.71 +typedef JSAMPARRAY *JSAMPIMAGE;	/* a 3-D sample array: top index is color */
   71.72 +
   71.73 +typedef JCOEF JBLOCK[DCTSIZE2];	/* one block of coefficients */
   71.74 +typedef JBLOCK FAR *JBLOCKROW;	/* pointer to one row of coefficient blocks */
   71.75 +typedef JBLOCKROW *JBLOCKARRAY;		/* a 2-D array of coefficient blocks */
   71.76 +typedef JBLOCKARRAY *JBLOCKIMAGE;	/* a 3-D array of coefficient blocks */
   71.77 +
   71.78 +typedef JCOEF FAR *JCOEFPTR;	/* useful in a couple of places */
   71.79 +
   71.80 +
   71.81 +/* Types for JPEG compression parameters and working tables. */
   71.82 +
   71.83 +
   71.84 +/* DCT coefficient quantization tables. */
   71.85 +
   71.86 +typedef struct {
   71.87 +  /* This array gives the coefficient quantizers in natural array order
   71.88 +   * (not the zigzag order in which they are stored in a JPEG DQT marker).
   71.89 +   * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
   71.90 +   */
   71.91 +  UINT16 quantval[DCTSIZE2];	/* quantization step for each coefficient */
   71.92 +  /* This field is used only during compression.  It's initialized FALSE when
   71.93 +   * the table is created, and set TRUE when it's been output to the file.
   71.94 +   * You could suppress output of a table by setting this to TRUE.
   71.95 +   * (See jpeg_suppress_tables for an example.)
   71.96 +   */
   71.97 +  boolean sent_table;		/* TRUE when table has been output */
   71.98 +} JQUANT_TBL;
   71.99 +
  71.100 +
  71.101 +/* Huffman coding tables. */
  71.102 +
  71.103 +typedef struct {
  71.104 +  /* These two fields directly represent the contents of a JPEG DHT marker */
  71.105 +  UINT8 bits[17];		/* bits[k] = # of symbols with codes of */
  71.106 +				/* length k bits; bits[0] is unused */
  71.107 +  UINT8 huffval[256];		/* The symbols, in order of incr code length */
  71.108 +  /* This field is used only during compression.  It's initialized FALSE when
  71.109 +   * the table is created, and set TRUE when it's been output to the file.
  71.110 +   * You could suppress output of a table by setting this to TRUE.
  71.111 +   * (See jpeg_suppress_tables for an example.)
  71.112 +   */
  71.113 +  boolean sent_table;		/* TRUE when table has been output */
  71.114 +} JHUFF_TBL;
  71.115 +
  71.116 +
  71.117 +/* Basic info about one component (color channel). */
  71.118 +
  71.119 +typedef struct {
  71.120 +  /* These values are fixed over the whole image. */
  71.121 +  /* For compression, they must be supplied by parameter setup; */
  71.122 +  /* for decompression, they are read from the SOF marker. */
  71.123 +  int component_id;		/* identifier for this component (0..255) */
  71.124 +  int component_index;		/* its index in SOF or cinfo->comp_info[] */
  71.125 +  int h_samp_factor;		/* horizontal sampling factor (1..4) */
  71.126 +  int v_samp_factor;		/* vertical sampling factor (1..4) */
  71.127 +  int quant_tbl_no;		/* quantization table selector (0..3) */
  71.128 +  /* These values may vary between scans. */
  71.129 +  /* For compression, they must be supplied by parameter setup; */
  71.130 +  /* for decompression, they are read from the SOS marker. */
  71.131 +  /* The decompressor output side may not use these variables. */
  71.132 +  int dc_tbl_no;		/* DC entropy table selector (0..3) */
  71.133 +  int ac_tbl_no;		/* AC entropy table selector (0..3) */
  71.134 +  
  71.135 +  /* Remaining fields should be treated as private by applications. */
  71.136 +  
  71.137 +  /* These values are computed during compression or decompression startup: */
  71.138 +  /* Component's size in DCT blocks.
  71.139 +   * Any dummy blocks added to complete an MCU are not counted; therefore
  71.140 +   * these values do not depend on whether a scan is interleaved or not.
  71.141 +   */
  71.142 +  JDIMENSION width_in_blocks;
  71.143 +  JDIMENSION height_in_blocks;
  71.144 +  /* Size of a DCT block in samples.  Always DCTSIZE for compression.
  71.145 +   * For decompression this is the size of the output from one DCT block,
  71.146 +   * reflecting any scaling we choose to apply during the IDCT step.
  71.147 +   * Values of 1,2,4,8 are likely to be supported.  Note that different
  71.148 +   * components may receive different IDCT scalings.
  71.149 +   */
  71.150 +  int DCT_scaled_size;
  71.151 +  /* The downsampled dimensions are the component's actual, unpadded number
  71.152 +   * of samples at the main buffer (preprocessing/compression interface), thus
  71.153 +   * downsampled_width = ceil(image_width * Hi/Hmax)
  71.154 +   * and similarly for height.  For decompression, IDCT scaling is included, so
  71.155 +   * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
  71.156 +   */
  71.157 +  JDIMENSION downsampled_width;	 /* actual width in samples */
  71.158 +  JDIMENSION downsampled_height; /* actual height in samples */
  71.159 +  /* This flag is used only for decompression.  In cases where some of the
  71.160 +   * components will be ignored (eg grayscale output from YCbCr image),
  71.161 +   * we can skip most computations for the unused components.
  71.162 +   */
  71.163 +  boolean component_needed;	/* do we need the value of this component? */
  71.164 +
  71.165 +  /* These values are computed before starting a scan of the component. */
  71.166 +  /* The decompressor output side may not use these variables. */
  71.167 +  int MCU_width;		/* number of blocks per MCU, horizontally */
  71.168 +  int MCU_height;		/* number of blocks per MCU, vertically */
  71.169 +  int MCU_blocks;		/* MCU_width * MCU_height */
  71.170 +  int MCU_sample_width;		/* MCU width in samples, MCU_width*DCT_scaled_size */
  71.171 +  int last_col_width;		/* # of non-dummy blocks across in last MCU */
  71.172 +  int last_row_height;		/* # of non-dummy blocks down in last MCU */
  71.173 +
  71.174 +  /* Saved quantization table for component; NULL if none yet saved.
  71.175 +   * See jdinput.c comments about the need for this information.
  71.176 +   * This field is currently used only for decompression.
  71.177 +   */
  71.178 +  JQUANT_TBL * quant_table;
  71.179 +
  71.180 +  /* Private per-component storage for DCT or IDCT subsystem. */
  71.181 +  void * dct_table;
  71.182 +} jpeg_component_info;
  71.183 +
  71.184 +
  71.185 +/* The script for encoding a multiple-scan file is an array of these: */
  71.186 +
  71.187 +typedef struct {
  71.188 +  int comps_in_scan;		/* number of components encoded in this scan */
  71.189 +  int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
  71.190 +  int Ss, Se;			/* progressive JPEG spectral selection parms */
  71.191 +  int Ah, Al;			/* progressive JPEG successive approx. parms */
  71.192 +} jpeg_scan_info;
  71.193 +
  71.194 +/* The decompressor can save APPn and COM markers in a list of these: */
  71.195 +
  71.196 +typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr;
  71.197 +
  71.198 +struct jpeg_marker_struct {
  71.199 +  jpeg_saved_marker_ptr next;	/* next in list, or NULL */
  71.200 +  UINT8 marker;			/* marker code: JPEG_COM, or JPEG_APP0+n */
  71.201 +  unsigned int original_length;	/* # bytes of data in the file */
  71.202 +  unsigned int data_length;	/* # bytes of data saved at data[] */
  71.203 +  JOCTET FAR * data;		/* the data contained in the marker */
  71.204 +  /* the marker length word is not counted in data_length or original_length */
  71.205 +};
  71.206 +
  71.207 +/* Known color spaces. */
  71.208 +
  71.209 +typedef enum {
  71.210 +	JCS_UNKNOWN,		/* error/unspecified */
  71.211 +	JCS_GRAYSCALE,		/* monochrome */
  71.212 +	JCS_RGB,		/* red/green/blue */
  71.213 +	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
  71.214 +	JCS_CMYK,		/* C/M/Y/K */
  71.215 +	JCS_YCCK		/* Y/Cb/Cr/K */
  71.216 +} J_COLOR_SPACE;
  71.217 +
  71.218 +/* DCT/IDCT algorithm options. */
  71.219 +
  71.220 +typedef enum {
  71.221 +	JDCT_ISLOW,		/* slow but accurate integer algorithm */
  71.222 +	JDCT_IFAST,		/* faster, less accurate integer method */
  71.223 +	JDCT_FLOAT		/* floating-point: accurate, fast on fast HW */
  71.224 +} J_DCT_METHOD;
  71.225 +
  71.226 +#ifndef JDCT_DEFAULT		/* may be overridden in jconfig.h */
  71.227 +#define JDCT_DEFAULT  JDCT_ISLOW
  71.228 +#endif
  71.229 +#ifndef JDCT_FASTEST		/* may be overridden in jconfig.h */
  71.230 +#define JDCT_FASTEST  JDCT_IFAST
  71.231 +#endif
  71.232 +
  71.233 +/* Dithering options for decompression. */
  71.234 +
  71.235 +typedef enum {
  71.236 +	JDITHER_NONE,		/* no dithering */
  71.237 +	JDITHER_ORDERED,	/* simple ordered dither */
  71.238 +	JDITHER_FS		/* Floyd-Steinberg error diffusion dither */
  71.239 +} J_DITHER_MODE;
  71.240 +
  71.241 +
  71.242 +/* Common fields between JPEG compression and decompression master structs. */
  71.243 +
  71.244 +#define jpeg_common_fields \
  71.245 +  struct jpeg_error_mgr * err;	/* Error handler module */\
  71.246 +  struct jpeg_memory_mgr * mem;	/* Memory manager module */\
  71.247 +  struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
  71.248 +  void * client_data;		/* Available for use by application */\
  71.249 +  boolean is_decompressor;	/* So common code can tell which is which */\
  71.250 +  int global_state		/* For checking call sequence validity */
  71.251 +
  71.252 +/* Routines that are to be used by both halves of the library are declared
  71.253 + * to receive a pointer to this structure.  There are no actual instances of
  71.254 + * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
  71.255 + */
  71.256 +struct jpeg_common_struct {
  71.257 +  jpeg_common_fields;		/* Fields common to both master struct types */
  71.258 +  /* Additional fields follow in an actual jpeg_compress_struct or
  71.259 +   * jpeg_decompress_struct.  All three structs must agree on these
  71.260 +   * initial fields!  (This would be a lot cleaner in C++.)
  71.261 +   */
  71.262 +};
  71.263 +
  71.264 +typedef struct jpeg_common_struct * j_common_ptr;
  71.265 +typedef struct jpeg_compress_struct * j_compress_ptr;
  71.266 +typedef struct jpeg_decompress_struct * j_decompress_ptr;
  71.267 +
  71.268 +
  71.269 +/* Master record for a compression instance */
  71.270 +
  71.271 +struct jpeg_compress_struct {
  71.272 +  jpeg_common_fields;		/* Fields shared with jpeg_decompress_struct */
  71.273 +
  71.274 +  /* Destination for compressed data */
  71.275 +  struct jpeg_destination_mgr * dest;
  71.276 +
  71.277 +  /* Description of source image --- these fields must be filled in by
  71.278 +   * outer application before starting compression.  in_color_space must
  71.279 +   * be correct before you can even call jpeg_set_defaults().
  71.280 +   */
  71.281 +
  71.282 +  JDIMENSION image_width;	/* input image width */
  71.283 +  JDIMENSION image_height;	/* input image height */
  71.284 +  int input_components;		/* # of color components in input image */
  71.285 +  J_COLOR_SPACE in_color_space;	/* colorspace of input image */
  71.286 +
  71.287 +  double input_gamma;		/* image gamma of input image */
  71.288 +
  71.289 +  /* Compression parameters --- these fields must be set before calling
  71.290 +   * jpeg_start_compress().  We recommend calling jpeg_set_defaults() to
  71.291 +   * initialize everything to reasonable defaults, then changing anything
  71.292 +   * the application specifically wants to change.  That way you won't get
  71.293 +   * burnt when new parameters are added.  Also note that there are several
  71.294 +   * helper routines to simplify changing parameters.
  71.295 +   */
  71.296 +
  71.297 +  int data_precision;		/* bits of precision in image data */
  71.298 +
  71.299 +  int num_components;		/* # of color components in JPEG image */
  71.300 +  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
  71.301 +
  71.302 +  jpeg_component_info * comp_info;
  71.303 +  /* comp_info[i] describes component that appears i'th in SOF */
  71.304 +  
  71.305 +  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
  71.306 +  /* ptrs to coefficient quantization tables, or NULL if not defined */
  71.307 +  
  71.308 +  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  71.309 +  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  71.310 +  /* ptrs to Huffman coding tables, or NULL if not defined */
  71.311 +  
  71.312 +  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  71.313 +  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  71.314 +  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  71.315 +
  71.316 +  int num_scans;		/* # of entries in scan_info array */
  71.317 +  const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */
  71.318 +  /* The default value of scan_info is NULL, which causes a single-scan
  71.319 +   * sequential JPEG file to be emitted.  To create a multi-scan file,
  71.320 +   * set num_scans and scan_info to point to an array of scan definitions.
  71.321 +   */
  71.322 +
  71.323 +  boolean raw_data_in;		/* TRUE=caller supplies downsampled data */
  71.324 +  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
  71.325 +  boolean optimize_coding;	/* TRUE=optimize entropy encoding parms */
  71.326 +  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
  71.327 +  int smoothing_factor;		/* 1..100, or 0 for no input smoothing */
  71.328 +  J_DCT_METHOD dct_method;	/* DCT algorithm selector */
  71.329 +
  71.330 +  /* The restart interval can be specified in absolute MCUs by setting
  71.331 +   * restart_interval, or in MCU rows by setting restart_in_rows
  71.332 +   * (in which case the correct restart_interval will be figured
  71.333 +   * for each scan).
  71.334 +   */
  71.335 +  unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
  71.336 +  int restart_in_rows;		/* if > 0, MCU rows per restart interval */
  71.337 +
  71.338 +  /* Parameters controlling emission of special markers. */
  71.339 +
  71.340 +  boolean write_JFIF_header;	/* should a JFIF marker be written? */
  71.341 +  UINT8 JFIF_major_version;	/* What to write for the JFIF version number */
  71.342 +  UINT8 JFIF_minor_version;
  71.343 +  /* These three values are not used by the JPEG code, merely copied */
  71.344 +  /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  71.345 +  /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  71.346 +  /* ratio is defined by X_density/Y_density even when density_unit=0. */
  71.347 +  UINT8 density_unit;		/* JFIF code for pixel size units */
  71.348 +  UINT16 X_density;		/* Horizontal pixel density */
  71.349 +  UINT16 Y_density;		/* Vertical pixel density */
  71.350 +  boolean write_Adobe_marker;	/* should an Adobe marker be written? */
  71.351 +  
  71.352 +  /* State variable: index of next scanline to be written to
  71.353 +   * jpeg_write_scanlines().  Application may use this to control its
  71.354 +   * processing loop, e.g., "while (next_scanline < image_height)".
  71.355 +   */
  71.356 +
  71.357 +  JDIMENSION next_scanline;	/* 0 .. image_height-1  */
  71.358 +
  71.359 +  /* Remaining fields are known throughout compressor, but generally
  71.360 +   * should not be touched by a surrounding application.
  71.361 +   */
  71.362 +
  71.363 +  /*
  71.364 +   * These fields are computed during compression startup
  71.365 +   */
  71.366 +  boolean progressive_mode;	/* TRUE if scan script uses progressive mode */
  71.367 +  int max_h_samp_factor;	/* largest h_samp_factor */
  71.368 +  int max_v_samp_factor;	/* largest v_samp_factor */
  71.369 +
  71.370 +  JDIMENSION total_iMCU_rows;	/* # of iMCU rows to be input to coef ctlr */
  71.371 +  /* The coefficient controller receives data in units of MCU rows as defined
  71.372 +   * for fully interleaved scans (whether the JPEG file is interleaved or not).
  71.373 +   * There are v_samp_factor * DCTSIZE sample rows of each component in an
  71.374 +   * "iMCU" (interleaved MCU) row.
  71.375 +   */
  71.376 +  
  71.377 +  /*
  71.378 +   * These fields are valid during any one scan.
  71.379 +   * They describe the components and MCUs actually appearing in the scan.
  71.380 +   */
  71.381 +  int comps_in_scan;		/* # of JPEG components in this scan */
  71.382 +  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  71.383 +  /* *cur_comp_info[i] describes component that appears i'th in SOS */
  71.384 +  
  71.385 +  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  71.386 +  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
  71.387 +  
  71.388 +  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  71.389 +  int MCU_membership[C_MAX_BLOCKS_IN_MCU];
  71.390 +  /* MCU_membership[i] is index in cur_comp_info of component owning */
  71.391 +  /* i'th block in an MCU */
  71.392 +
  71.393 +  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */
  71.394 +
  71.395 +  /*
  71.396 +   * Links to compression subobjects (methods and private variables of modules)
  71.397 +   */
  71.398 +  struct jpeg_comp_master * master;
  71.399 +  struct jpeg_c_main_controller * main;
  71.400 +  struct jpeg_c_prep_controller * prep;
  71.401 +  struct jpeg_c_coef_controller * coef;
  71.402 +  struct jpeg_marker_writer * marker;
  71.403 +  struct jpeg_color_converter * cconvert;
  71.404 +  struct jpeg_downsampler * downsample;
  71.405 +  struct jpeg_forward_dct * fdct;
  71.406 +  struct jpeg_entropy_encoder * entropy;
  71.407 +  jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
  71.408 +  int script_space_size;
  71.409 +};
  71.410 +
  71.411 +
  71.412 +/* Master record for a decompression instance */
  71.413 +
  71.414 +struct jpeg_decompress_struct {
  71.415 +  jpeg_common_fields;		/* Fields shared with jpeg_compress_struct */
  71.416 +
  71.417 +  /* Source of compressed data */
  71.418 +  struct jpeg_source_mgr * src;
  71.419 +
  71.420 +  /* Basic description of image --- filled in by jpeg_read_header(). */
  71.421 +  /* Application may inspect these values to decide how to process image. */
  71.422 +
  71.423 +  JDIMENSION image_width;	/* nominal image width (from SOF marker) */
  71.424 +  JDIMENSION image_height;	/* nominal image height */
  71.425 +  int num_components;		/* # of color components in JPEG image */
  71.426 +  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
  71.427 +
  71.428 +  /* Decompression processing parameters --- these fields must be set before
  71.429 +   * calling jpeg_start_decompress().  Note that jpeg_read_header() initializes
  71.430 +   * them to default values.
  71.431 +   */
  71.432 +
  71.433 +  J_COLOR_SPACE out_color_space; /* colorspace for output */
  71.434 +
  71.435 +  unsigned int scale_num, scale_denom; /* fraction by which to scale image */
  71.436 +
  71.437 +  double output_gamma;		/* image gamma wanted in output */
  71.438 +
  71.439 +  boolean buffered_image;	/* TRUE=multiple output passes */
  71.440 +  boolean raw_data_out;		/* TRUE=downsampled data wanted */
  71.441 +
  71.442 +  J_DCT_METHOD dct_method;	/* IDCT algorithm selector */
  71.443 +  boolean do_fancy_upsampling;	/* TRUE=apply fancy upsampling */
  71.444 +  boolean do_block_smoothing;	/* TRUE=apply interblock smoothing */
  71.445 +
  71.446 +  boolean quantize_colors;	/* TRUE=colormapped output wanted */
  71.447 +  /* the following are ignored if not quantize_colors: */
  71.448 +  J_DITHER_MODE dither_mode;	/* type of color dithering to use */
  71.449 +  boolean two_pass_quantize;	/* TRUE=use two-pass color quantization */
  71.450 +  int desired_number_of_colors;	/* max # colors to use in created colormap */
  71.451 +  /* these are significant only in buffered-image mode: */
  71.452 +  boolean enable_1pass_quant;	/* enable future use of 1-pass quantizer */
  71.453 +  boolean enable_external_quant;/* enable future use of external colormap */
  71.454 +  boolean enable_2pass_quant;	/* enable future use of 2-pass quantizer */
  71.455 +
  71.456 +  /* Description of actual output image that will be returned to application.
  71.457 +   * These fields are computed by jpeg_start_decompress().
  71.458 +   * You can also use jpeg_calc_output_dimensions() to determine these values
  71.459 +   * in advance of calling jpeg_start_decompress().
  71.460 +   */
  71.461 +
  71.462 +  JDIMENSION output_width;	/* scaled image width */
  71.463 +  JDIMENSION output_height;	/* scaled image height */
  71.464 +  int out_color_components;	/* # of color components in out_color_space */
  71.465 +  int output_components;	/* # of color components returned */
  71.466 +  /* output_components is 1 (a colormap index) when quantizing colors;
  71.467 +   * otherwise it equals out_color_components.
  71.468 +   */
  71.469 +  int rec_outbuf_height;	/* min recommended height of scanline buffer */
  71.470 +  /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
  71.471 +   * high, space and time will be wasted due to unnecessary data copying.
  71.472 +   * Usually rec_outbuf_height will be 1 or 2, at most 4.
  71.473 +   */
  71.474 +
  71.475 +  /* When quantizing colors, the output colormap is described by these fields.
  71.476 +   * The application can supply a colormap by setting colormap non-NULL before
  71.477 +   * calling jpeg_start_decompress; otherwise a colormap is created during
  71.478 +   * jpeg_start_decompress or jpeg_start_output.
  71.479 +   * The map has out_color_components rows and actual_number_of_colors columns.
  71.480 +   */
  71.481 +  int actual_number_of_colors;	/* number of entries in use */
  71.482 +  JSAMPARRAY colormap;		/* The color map as a 2-D pixel array */
  71.483 +
  71.484 +  /* State variables: these variables indicate the progress of decompression.
  71.485 +   * The application may examine these but must not modify them.
  71.486 +   */
  71.487 +
  71.488 +  /* Row index of next scanline to be read from jpeg_read_scanlines().
  71.489 +   * Application may use this to control its processing loop, e.g.,
  71.490 +   * "while (output_scanline < output_height)".
  71.491 +   */
  71.492 +  JDIMENSION output_scanline;	/* 0 .. output_height-1  */
  71.493 +
  71.494 +  /* Current input scan number and number of iMCU rows completed in scan.
  71.495 +   * These indicate the progress of the decompressor input side.
  71.496 +   */
  71.497 +  int input_scan_number;	/* Number of SOS markers seen so far */
  71.498 +  JDIMENSION input_iMCU_row;	/* Number of iMCU rows completed */
  71.499 +
  71.500 +  /* The "output scan number" is the notional scan being displayed by the
  71.501 +   * output side.  The decompressor will not allow output scan/row number
  71.502 +   * to get ahead of input scan/row, but it can fall arbitrarily far behind.
  71.503 +   */
  71.504 +  int output_scan_number;	/* Nominal scan number being displayed */
  71.505 +  JDIMENSION output_iMCU_row;	/* Number of iMCU rows read */
  71.506 +
  71.507 +  /* Current progression status.  coef_bits[c][i] indicates the precision
  71.508 +   * with which component c's DCT coefficient i (in zigzag order) is known.
  71.509 +   * It is -1 when no data has yet been received, otherwise it is the point
  71.510 +   * transform (shift) value for the most recent scan of the coefficient
  71.511 +   * (thus, 0 at completion of the progression).
  71.512 +   * This pointer is NULL when reading a non-progressive file.
  71.513 +   */
  71.514 +  int (*coef_bits)[DCTSIZE2];	/* -1 or current Al value for each coef */
  71.515 +
  71.516 +  /* Internal JPEG parameters --- the application usually need not look at
  71.517 +   * these fields.  Note that the decompressor output side may not use
  71.518 +   * any parameters that can change between scans.
  71.519 +   */
  71.520 +
  71.521 +  /* Quantization and Huffman tables are carried forward across input
  71.522 +   * datastreams when processing abbreviated JPEG datastreams.
  71.523 +   */
  71.524 +
  71.525 +  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
  71.526 +  /* ptrs to coefficient quantization tables, or NULL if not defined */
  71.527 +
  71.528 +  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  71.529 +  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  71.530 +  /* ptrs to Huffman coding tables, or NULL if not defined */
  71.531 +
  71.532 +  /* These parameters are never carried across datastreams, since they
  71.533 +   * are given in SOF/SOS markers or defined to be reset by SOI.
  71.534 +   */
  71.535 +
  71.536 +  int data_precision;		/* bits of precision in image data */
  71.537 +
  71.538 +  jpeg_component_info * comp_info;
  71.539 +  /* comp_info[i] describes component that appears i'th in SOF */
  71.540 +
  71.541 +  boolean progressive_mode;	/* TRUE if SOFn specifies progressive mode */
  71.542 +  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
  71.543 +
  71.544 +  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  71.545 +  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  71.546 +  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  71.547 +
  71.548 +  unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
  71.549 +
  71.550 +  /* These fields record data obtained from optional markers recognized by
  71.551 +   * the JPEG library.
  71.552 +   */
  71.553 +  boolean saw_JFIF_marker;	/* TRUE iff a JFIF APP0 marker was found */
  71.554 +  /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */
  71.555 +  UINT8 JFIF_major_version;	/* JFIF version number */
  71.556 +  UINT8 JFIF_minor_version;
  71.557 +  UINT8 density_unit;		/* JFIF code for pixel size units */
  71.558 +  UINT16 X_density;		/* Horizontal pixel density */
  71.559 +  UINT16 Y_density;		/* Vertical pixel density */
  71.560 +  boolean saw_Adobe_marker;	/* TRUE iff an Adobe APP14 marker was found */
  71.561 +  UINT8 Adobe_transform;	/* Color transform code from Adobe marker */
  71.562 +
  71.563 +  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
  71.564 +
  71.565 +  /* Aside from the specific data retained from APPn markers known to the
  71.566 +   * library, the uninterpreted contents of any or all APPn and COM markers
  71.567 +   * can be saved in a list for examination by the application.
  71.568 +   */
  71.569 +  jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */
  71.570 +
  71.571 +  /* Remaining fields are known throughout decompressor, but generally
  71.572 +   * should not be touched by a surrounding application.
  71.573 +   */
  71.574 +
  71.575 +  /*
  71.576 +   * These fields are computed during decompression startup
  71.577 +   */
  71.578 +  int max_h_samp_factor;	/* largest h_samp_factor */
  71.579 +  int max_v_samp_factor;	/* largest v_samp_factor */
  71.580 +
  71.581 +  int min_DCT_scaled_size;	/* smallest DCT_scaled_size of any component */
  71.582 +
  71.583 +  JDIMENSION total_iMCU_rows;	/* # of iMCU rows in image */
  71.584 +  /* The coefficient controller's input and output progress is measured in
  71.585 +   * units of "iMCU" (interleaved MCU) rows.  These are the same as MCU rows
  71.586 +   * in fully interleaved JPEG scans, but are used whether the scan is
  71.587 +   * interleaved or not.  We define an iMCU row as v_samp_factor DCT block
  71.588 +   * rows of each component.  Therefore, the IDCT output contains
  71.589 +   * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
  71.590 +   */
  71.591 +
  71.592 +  JSAMPLE * sample_range_limit; /* table for fast range-limiting */
  71.593 +
  71.594 +  /*
  71.595 +   * These fields are valid during any one scan.
  71.596 +   * They describe the components and MCUs actually appearing in the scan.
  71.597 +   * Note that the decompressor output side must not use these fields.
  71.598 +   */
  71.599 +  int comps_in_scan;		/* # of JPEG components in this scan */
  71.600 +  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  71.601 +  /* *cur_comp_info[i] describes component that appears i'th in SOS */
  71.602 +
  71.603 +  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  71.604 +  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
  71.605 +
  71.606 +  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  71.607 +  int MCU_membership[D_MAX_BLOCKS_IN_MCU];
  71.608 +  /* MCU_membership[i] is index in cur_comp_info of component owning */
  71.609 +  /* i'th block in an MCU */
  71.610 +
  71.611 +  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */
  71.612 +
  71.613 +  /* This field is shared between entropy decoder and marker parser.
  71.614 +   * It is either zero or the code of a JPEG marker that has been
  71.615 +   * read from the data source, but has not yet been processed.
  71.616 +   */
  71.617 +  int unread_marker;
  71.618 +
  71.619 +  /*
  71.620 +   * Links to decompression subobjects (methods, private variables of modules)
  71.621 +   */
  71.622 +  struct jpeg_decomp_master * master;
  71.623 +  struct jpeg_d_main_controller * main;
  71.624 +  struct jpeg_d_coef_controller * coef;
  71.625 +  struct jpeg_d_post_controller * post;
  71.626 +  struct jpeg_input_controller * inputctl;
  71.627 +  struct jpeg_marker_reader * marker;
  71.628 +  struct jpeg_entropy_decoder * entropy;
  71.629 +  struct jpeg_inverse_dct * idct;
  71.630 +  struct jpeg_upsampler * upsample;
  71.631 +  struct jpeg_color_deconverter * cconvert;
  71.632 +  struct jpeg_color_quantizer * cquantize;
  71.633 +};
  71.634 +
  71.635 +
  71.636 +/* "Object" declarations for JPEG modules that may be supplied or called
  71.637 + * directly by the surrounding application.
  71.638 + * As with all objects in the JPEG library, these structs only define the
  71.639 + * publicly visible methods and state variables of a module.  Additional
  71.640 + * private fields may exist after the public ones.
  71.641 + */
  71.642 +
  71.643 +
  71.644 +/* Error handler object */
  71.645 +
  71.646 +struct jpeg_error_mgr {
  71.647 +  /* Error exit handler: does not return to caller */
  71.648 +  JMETHOD(void, error_exit, (j_common_ptr cinfo));
  71.649 +  /* Conditionally emit a trace or warning message */
  71.650 +  JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
  71.651 +  /* Routine that actually outputs a trace or error message */
  71.652 +  JMETHOD(void, output_message, (j_common_ptr cinfo));
  71.653 +  /* Format a message string for the most recent JPEG error or message */
  71.654 +  JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
  71.655 +#define JMSG_LENGTH_MAX  200	/* recommended size of format_message buffer */
  71.656 +  /* Reset error state variables at start of a new image */
  71.657 +  JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
  71.658 +  
  71.659 +  /* The message ID code and any parameters are saved here.
  71.660 +   * A message can have one string parameter or up to 8 int parameters.
  71.661 +   */
  71.662 +  int msg_code;
  71.663 +#define JMSG_STR_PARM_MAX  80
  71.664 +  union {
  71.665 +    int i[8];
  71.666 +    char s[JMSG_STR_PARM_MAX];
  71.667 +  } msg_parm;
  71.668 +  
  71.669 +  /* Standard state variables for error facility */
  71.670 +  
  71.671 +  int trace_level;		/* max msg_level that will be displayed */
  71.672 +  
  71.673 +  /* For recoverable corrupt-data errors, we emit a warning message,
  71.674 +   * but keep going unless emit_message chooses to abort.  emit_message
  71.675 +   * should count warnings in num_warnings.  The surrounding application
  71.676 +   * can check for bad data by seeing if num_warnings is nonzero at the
  71.677 +   * end of processing.
  71.678 +   */
  71.679 +  long num_warnings;		/* number of corrupt-data warnings */
  71.680 +
  71.681 +  /* These fields point to the table(s) of error message strings.
  71.682 +   * An application can change the table pointer to switch to a different
  71.683 +   * message list (typically, to change the language in which errors are
  71.684 +   * reported).  Some applications may wish to add additional error codes
  71.685 +   * that will be handled by the JPEG library error mechanism; the second
  71.686 +   * table pointer is used for this purpose.
  71.687 +   *
  71.688 +   * First table includes all errors generated by JPEG library itself.
  71.689 +   * Error code 0 is reserved for a "no such error string" message.
  71.690 +   */
  71.691 +  const char * const * jpeg_message_table; /* Library errors */
  71.692 +  int last_jpeg_message;    /* Table contains strings 0..last_jpeg_message */
  71.693 +  /* Second table can be added by application (see cjpeg/djpeg for example).
  71.694 +   * It contains strings numbered first_addon_message..last_addon_message.
  71.695 +   */
  71.696 +  const char * const * addon_message_table; /* Non-library errors */
  71.697 +  int first_addon_message;	/* code for first string in addon table */
  71.698 +  int last_addon_message;	/* code for last string in addon table */
  71.699 +};
  71.700 +
  71.701 +
  71.702 +/* Progress monitor object */
  71.703 +
  71.704 +struct jpeg_progress_mgr {
  71.705 +  JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
  71.706 +
  71.707 +  long pass_counter;		/* work units completed in this pass */
  71.708 +  long pass_limit;		/* total number of work units in this pass */
  71.709 +  int completed_passes;		/* passes completed so far */
  71.710 +  int total_passes;		/* total number of passes expected */
  71.711 +};
  71.712 +
  71.713 +
  71.714 +/* Data destination object for compression */
  71.715 +
  71.716 +struct jpeg_destination_mgr {
  71.717 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
  71.718 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
  71.719 +
  71.720 +  JMETHOD(void, init_destination, (j_compress_ptr cinfo));
  71.721 +  JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
  71.722 +  JMETHOD(void, term_destination, (j_compress_ptr cinfo));
  71.723 +};
  71.724 +
  71.725 +
  71.726 +/* Data source object for decompression */
  71.727 +
  71.728 +struct jpeg_source_mgr {
  71.729 +  const JOCTET * next_input_byte; /* => next byte to read from buffer */
  71.730 +  size_t bytes_in_buffer;	/* # of bytes remaining in buffer */
  71.731 +
  71.732 +  JMETHOD(void, init_source, (j_decompress_ptr cinfo));
  71.733 +  JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
  71.734 +  JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
  71.735 +  JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
  71.736 +  JMETHOD(void, term_source, (j_decompress_ptr cinfo));
  71.737 +};
  71.738 +
  71.739 +
  71.740 +/* Memory manager object.
  71.741 + * Allocates "small" objects (a few K total), "large" objects (tens of K),
  71.742 + * and "really big" objects (virtual arrays with backing store if needed).
  71.743 + * The memory manager does not allow individual objects to be freed; rather,
  71.744 + * each created object is assigned to a pool, and whole pools can be freed
  71.745 + * at once.  This is faster and more convenient than remembering exactly what
  71.746 + * to free, especially where malloc()/free() are not too speedy.
  71.747 + * NB: alloc routines never return NULL.  They exit to error_exit if not
  71.748 + * successful.
  71.749 + */
  71.750 +
  71.751 +#define JPOOL_PERMANENT	0	/* lasts until master record is destroyed */
  71.752 +#define JPOOL_IMAGE	1	/* lasts until done with image/datastream */
  71.753 +#define JPOOL_NUMPOOLS	2
  71.754 +
  71.755 +typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
  71.756 +typedef struct jvirt_barray_control * jvirt_barray_ptr;
  71.757 +
  71.758 +
  71.759 +struct jpeg_memory_mgr {
  71.760 +  /* Method pointers */
  71.761 +  JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
  71.762 +				size_t sizeofobject));
  71.763 +  JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
  71.764 +				     size_t sizeofobject));
  71.765 +  JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
  71.766 +				     JDIMENSION samplesperrow,
  71.767 +				     JDIMENSION numrows));
  71.768 +  JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
  71.769 +				      JDIMENSION blocksperrow,
  71.770 +				      JDIMENSION numrows));
  71.771 +  JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
  71.772 +						  int pool_id,
  71.773 +						  boolean pre_zero,
  71.774 +						  JDIMENSION samplesperrow,
  71.775 +						  JDIMENSION numrows,
  71.776 +						  JDIMENSION maxaccess));
  71.777 +  JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
  71.778 +						  int pool_id,
  71.779 +						  boolean pre_zero,
  71.780 +						  JDIMENSION blocksperrow,
  71.781 +						  JDIMENSION numrows,
  71.782 +						  JDIMENSION maxaccess));
  71.783 +  JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
  71.784 +  JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
  71.785 +					   jvirt_sarray_ptr ptr,
  71.786 +					   JDIMENSION start_row,
  71.787 +					   JDIMENSION num_rows,
  71.788 +					   boolean writable));
  71.789 +  JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
  71.790 +					    jvirt_barray_ptr ptr,
  71.791 +					    JDIMENSION start_row,
  71.792 +					    JDIMENSION num_rows,
  71.793 +					    boolean writable));
  71.794 +  JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
  71.795 +  JMETHOD(void, self_destruct, (j_common_ptr cinfo));
  71.796 +
  71.797 +  /* Limit on memory allocation for this JPEG object.  (Note that this is
  71.798 +   * merely advisory, not a guaranteed maximum; it only affects the space
  71.799 +   * used for virtual-array buffers.)  May be changed by outer application
  71.800 +   * after creating the JPEG object.
  71.801 +   */
  71.802 +  long max_memory_to_use;
  71.803 +
  71.804 +  /* Maximum allocation request accepted by alloc_large. */
  71.805 +  long max_alloc_chunk;
  71.806 +};
  71.807 +
  71.808 +
  71.809 +/* Routine signature for application-supplied marker processing methods.
  71.810 + * Need not pass marker code since it is stored in cinfo->unread_marker.
  71.811 + */
  71.812 +typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
  71.813 +
  71.814 +
  71.815 +/* Declarations for routines called by application.
  71.816 + * The JPP macro hides prototype parameters from compilers that can't cope.
  71.817 + * Note JPP requires double parentheses.
  71.818 + */
  71.819 +
  71.820 +#ifdef HAVE_PROTOTYPES
  71.821 +#define JPP(arglist)	arglist
  71.822 +#else
  71.823 +#define JPP(arglist)	()
  71.824 +#endif
  71.825 +
  71.826 +
  71.827 +/* Short forms of external names for systems with brain-damaged linkers.
  71.828 + * We shorten external names to be unique in the first six letters, which
  71.829 + * is good enough for all known systems.
  71.830 + * (If your compiler itself needs names to be unique in less than 15 
  71.831 + * characters, you are out of luck.  Get a better compiler.)
  71.832 + */
  71.833 +
  71.834 +#ifdef NEED_SHORT_EXTERNAL_NAMES
  71.835 +#define jpeg_std_error		jStdError
  71.836 +#define jpeg_CreateCompress	jCreaCompress
  71.837 +#define jpeg_CreateDecompress	jCreaDecompress
  71.838 +#define jpeg_destroy_compress	jDestCompress
  71.839 +#define jpeg_destroy_decompress	jDestDecompress
  71.840 +#define jpeg_stdio_dest		jStdDest
  71.841 +#define jpeg_stdio_src		jStdSrc
  71.842 +#define jpeg_set_defaults	jSetDefaults
  71.843 +#define jpeg_set_colorspace	jSetColorspace
  71.844 +#define jpeg_default_colorspace	jDefColorspace
  71.845 +#define jpeg_set_quality	jSetQuality
  71.846 +#define jpeg_set_linear_quality	jSetLQuality
  71.847 +#define jpeg_add_quant_table	jAddQuantTable
  71.848 +#define jpeg_quality_scaling	jQualityScaling
  71.849 +#define jpeg_simple_progression	jSimProgress
  71.850 +#define jpeg_suppress_tables	jSuppressTables
  71.851 +#define jpeg_alloc_quant_table	jAlcQTable
  71.852 +#define jpeg_alloc_huff_table	jAlcHTable
  71.853 +#define jpeg_start_compress	jStrtCompress
  71.854 +#define jpeg_write_scanlines	jWrtScanlines
  71.855 +#define jpeg_finish_compress	jFinCompress
  71.856 +#define jpeg_write_raw_data	jWrtRawData
  71.857 +#define jpeg_write_marker	jWrtMarker
  71.858 +#define jpeg_write_m_header	jWrtMHeader
  71.859 +#define jpeg_write_m_byte	jWrtMByte
  71.860 +#define jpeg_write_tables	jWrtTables
  71.861 +#define jpeg_read_header	jReadHeader
  71.862 +#define jpeg_start_decompress	jStrtDecompress
  71.863 +#define jpeg_read_scanlines	jReadScanlines
  71.864 +#define jpeg_finish_decompress	jFinDecompress
  71.865 +#define jpeg_read_raw_data	jReadRawData
  71.866 +#define jpeg_has_multiple_scans	jHasMultScn
  71.867 +#define jpeg_start_output	jStrtOutput
  71.868 +#define jpeg_finish_output	jFinOutput
  71.869 +#define jpeg_input_complete	jInComplete
  71.870 +#define jpeg_new_colormap	jNewCMap
  71.871 +#define jpeg_consume_input	jConsumeInput
  71.872 +#define jpeg_calc_output_dimensions	jCalcDimensions
  71.873 +#define jpeg_save_markers	jSaveMarkers
  71.874 +#define jpeg_set_marker_processor	jSetMarker
  71.875 +#define jpeg_read_coefficients	jReadCoefs
  71.876 +#define jpeg_write_coefficients	jWrtCoefs
  71.877 +#define jpeg_copy_critical_parameters	jCopyCrit
  71.878 +#define jpeg_abort_compress	jAbrtCompress
  71.879 +#define jpeg_abort_decompress	jAbrtDecompress
  71.880 +#define jpeg_abort		jAbort
  71.881 +#define jpeg_destroy		jDestroy
  71.882 +#define jpeg_resync_to_restart	jResyncRestart
  71.883 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
  71.884 +
  71.885 +
  71.886 +/* Default error-management setup */
  71.887 +EXTERN(struct jpeg_error_mgr *) jpeg_std_error
  71.888 +	JPP((struct jpeg_error_mgr * err));
  71.889 +
  71.890 +/* Initialization of JPEG compression objects.
  71.891 + * jpeg_create_compress() and jpeg_create_decompress() are the exported
  71.892 + * names that applications should call.  These expand to calls on
  71.893 + * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
  71.894 + * passed for version mismatch checking.
  71.895 + * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
  71.896 + */
  71.897 +#define jpeg_create_compress(cinfo) \
  71.898 +    jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
  71.899 +			(size_t) sizeof(struct jpeg_compress_struct))
  71.900 +#define jpeg_create_decompress(cinfo) \
  71.901 +    jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
  71.902 +			  (size_t) sizeof(struct jpeg_decompress_struct))
  71.903 +EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
  71.904 +				      int version, size_t structsize));
  71.905 +EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
  71.906 +					int version, size_t structsize));
  71.907 +/* Destruction of JPEG compression objects */
  71.908 +EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
  71.909 +EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
  71.910 +
  71.911 +/* Standard data source and destination managers: stdio streams. */
  71.912 +/* Caller is responsible for opening the file before and closing after. */
  71.913 +EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile));
  71.914 +EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
  71.915 +
  71.916 +/* Default parameter setup for compression */
  71.917 +EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
  71.918 +/* Compression parameter setup aids */
  71.919 +EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
  71.920 +				      J_COLOR_SPACE colorspace));
  71.921 +EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
  71.922 +EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
  71.923 +				   boolean force_baseline));
  71.924 +EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
  71.925 +					  int scale_factor,
  71.926 +					  boolean force_baseline));
  71.927 +EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
  71.928 +				       const unsigned int *basic_table,
  71.929 +				       int scale_factor,
  71.930 +				       boolean force_baseline));
  71.931 +EXTERN(int) jpeg_quality_scaling JPP((int quality));
  71.932 +EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
  71.933 +EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
  71.934 +				       boolean suppress));
  71.935 +EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
  71.936 +EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
  71.937 +
  71.938 +/* Main entry points for compression */
  71.939 +EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
  71.940 +				      boolean write_all_tables));
  71.941 +EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
  71.942 +					     JSAMPARRAY scanlines,
  71.943 +					     JDIMENSION num_lines));
  71.944 +EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
  71.945 +
  71.946 +/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
  71.947 +EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
  71.948 +					    JSAMPIMAGE data,
  71.949 +					    JDIMENSION num_lines));
  71.950 +
  71.951 +/* Write a special marker.  See libjpeg.doc concerning safe usage. */
  71.952 +EXTERN(void) jpeg_write_marker
  71.953 +	JPP((j_compress_ptr cinfo, int marker,
  71.954 +	     const JOCTET * dataptr, unsigned int datalen));
  71.955 +/* Same, but piecemeal. */
  71.956 +EXTERN(void) jpeg_write_m_header
  71.957 +	JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
  71.958 +EXTERN(void) jpeg_write_m_byte
  71.959 +	JPP((j_compress_ptr cinfo, int val));
  71.960 +
  71.961 +/* Alternate compression function: just write an abbreviated table file */
  71.962 +EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));
  71.963 +
  71.964 +/* Decompression startup: read start of JPEG datastream to see what's there */
  71.965 +EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
  71.966 +				  boolean require_image));
  71.967 +/* Return value is one of: */
  71.968 +#define JPEG_SUSPENDED		0 /* Suspended due to lack of input data */
  71.969 +#define JPEG_HEADER_OK		1 /* Found valid image datastream */
  71.970 +#define JPEG_HEADER_TABLES_ONLY	2 /* Found valid table-specs-only datastream */
  71.971 +/* If you pass require_image = TRUE (normal case), you need not check for
  71.972 + * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
  71.973 + * JPEG_SUSPENDED is only possible if you use a data source module that can
  71.974 + * give a suspension return (the stdio source module doesn't).
  71.975 + */
  71.976 +
  71.977 +/* Main entry points for decompression */
  71.978 +EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
  71.979 +EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
  71.980 +					    JSAMPARRAY scanlines,
  71.981 +					    JDIMENSION max_lines));
  71.982 +EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
  71.983 +
  71.984 +/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
  71.985 +EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
  71.986 +					   JSAMPIMAGE data,
  71.987 +					   JDIMENSION max_lines));
  71.988 +
  71.989 +/* Additional entry points for buffered-image mode. */
  71.990 +EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
  71.991 +EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
  71.992 +				       int scan_number));
  71.993 +EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
  71.994 +EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
  71.995 +EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
  71.996 +EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
  71.997 +/* Return value is one of: */
  71.998 +/* #define JPEG_SUSPENDED	0    Suspended due to lack of input data */
  71.999 +#define JPEG_REACHED_SOS	1 /* Reached start of new scan */
 71.1000 +#define JPEG_REACHED_EOI	2 /* Reached end of image */
 71.1001 +#define JPEG_ROW_COMPLETED	3 /* Completed one iMCU row */
 71.1002 +#define JPEG_SCAN_COMPLETED	4 /* Completed last iMCU row of a scan */
 71.1003 +
 71.1004 +/* Precalculate output dimensions for current decompression parameters. */
 71.1005 +EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
 71.1006 +
 71.1007 +/* Control saving of COM and APPn markers into marker_list. */
 71.1008 +EXTERN(void) jpeg_save_markers
 71.1009 +	JPP((j_decompress_ptr cinfo, int marker_code,
 71.1010 +	     unsigned int length_limit));
 71.1011 +
 71.1012 +/* Install a special processing method for COM or APPn markers. */
 71.1013 +EXTERN(void) jpeg_set_marker_processor
 71.1014 +	JPP((j_decompress_ptr cinfo, int marker_code,
 71.1015 +	     jpeg_marker_parser_method routine));
 71.1016 +
 71.1017 +/* Read or write raw DCT coefficients --- useful for lossless transcoding. */
 71.1018 +EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
 71.1019 +EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
 71.1020 +					  jvirt_barray_ptr * coef_arrays));
 71.1021 +EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
 71.1022 +						j_compress_ptr dstinfo));
 71.1023 +
 71.1024 +/* If you choose to abort compression or decompression before completing
 71.1025 + * jpeg_finish_(de)compress, then you need to clean up to release memory,
 71.1026 + * temporary files, etc.  You can just call jpeg_destroy_(de)compress
 71.1027 + * if you're done with the JPEG object, but if you want to clean it up and
 71.1028 + * reuse it, call this:
 71.1029 + */
 71.1030 +EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
 71.1031 +EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
 71.1032 +
 71.1033 +/* Generic versions of jpeg_abort and jpeg_destroy that work on either
 71.1034 + * flavor of JPEG object.  These may be more convenient in some places.
 71.1035 + */
 71.1036 +EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
 71.1037 +EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));
 71.1038 +
 71.1039 +/* Default restart-marker-resync procedure for use by data source modules */
 71.1040 +EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
 71.1041 +					    int desired));
 71.1042 +
 71.1043 +
 71.1044 +/* These marker codes are exported since applications and data source modules
 71.1045 + * are likely to want to use them.
 71.1046 + */
 71.1047 +
 71.1048 +#define JPEG_RST0	0xD0	/* RST0 marker code */
 71.1049 +#define JPEG_EOI	0xD9	/* EOI marker code */
 71.1050 +#define JPEG_APP0	0xE0	/* APP0 marker code */
 71.1051 +#define JPEG_COM	0xFE	/* COM marker code */
 71.1052 +
 71.1053 +
 71.1054 +/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
 71.1055 + * for structure definitions that are never filled in, keep it quiet by
 71.1056 + * supplying dummy definitions for the various substructures.
 71.1057 + */
 71.1058 +
 71.1059 +#ifdef INCOMPLETE_TYPES_BROKEN
 71.1060 +#ifndef JPEG_INTERNALS		/* will be defined in jpegint.h */
 71.1061 +struct jvirt_sarray_control { long dummy; };
 71.1062 +struct jvirt_barray_control { long dummy; };
 71.1063 +struct jpeg_comp_master { long dummy; };
 71.1064 +struct jpeg_c_main_controller { long dummy; };
 71.1065 +struct jpeg_c_prep_controller { long dummy; };
 71.1066 +struct jpeg_c_coef_controller { long dummy; };
 71.1067 +struct jpeg_marker_writer { long dummy; };
 71.1068 +struct jpeg_color_converter { long dummy; };
 71.1069 +struct jpeg_downsampler { long dummy; };
 71.1070 +struct jpeg_forward_dct { long dummy; };
 71.1071 +struct jpeg_entropy_encoder { long dummy; };
 71.1072 +struct jpeg_decomp_master { long dummy; };
 71.1073 +struct jpeg_d_main_controller { long dummy; };
 71.1074 +struct jpeg_d_coef_controller { long dummy; };
 71.1075 +struct jpeg_d_post_controller { long dummy; };
 71.1076 +struct jpeg_input_controller { long dummy; };
 71.1077 +struct jpeg_marker_reader { long dummy; };
 71.1078 +struct jpeg_entropy_decoder { long dummy; };
 71.1079 +struct jpeg_inverse_dct { long dummy; };
 71.1080 +struct jpeg_upsampler { long dummy; };
 71.1081 +struct jpeg_color_deconverter { long dummy; };
 71.1082 +struct jpeg_color_quantizer { long dummy; };
 71.1083 +#endif /* JPEG_INTERNALS */
 71.1084 +#endif /* INCOMPLETE_TYPES_BROKEN */
 71.1085 +
 71.1086 +
 71.1087 +/*
 71.1088 + * The JPEG library modules define JPEG_INTERNALS before including this file.
 71.1089 + * The internal structure declarations are read only when that is true.
 71.1090 + * Applications using the library should not include jpegint.h, but may wish
 71.1091 + * to include jerror.h.
 71.1092 + */
 71.1093 +
 71.1094 +#ifdef JPEG_INTERNALS
 71.1095 +#include "jpegint.h"		/* fetch private declarations */
 71.1096 +#include "jerror.h"		/* fetch error codes too */
 71.1097 +#endif
 71.1098 +
 71.1099 +#endif /* JPEGLIB_H */
    72.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    72.2 +++ b/libs/libjpeg/jquant1.c	Sun Jun 07 17:25:49 2015 +0300
    72.3 @@ -0,0 +1,856 @@
    72.4 +/*
    72.5 + * jquant1.c
    72.6 + *
    72.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    72.8 + * This file is part of the Independent JPEG Group's software.
    72.9 + * For conditions of distribution and use, see the accompanying README file.
   72.10 + *
   72.11 + * This file contains 1-pass color quantization (color mapping) routines.
   72.12 + * These routines provide mapping to a fixed color map using equally spaced
   72.13 + * color values.  Optional Floyd-Steinberg or ordered dithering is available.
   72.14 + */
   72.15 +
   72.16 +#define JPEG_INTERNALS
   72.17 +#include "jinclude.h"
   72.18 +#include "jpeglib.h"
   72.19 +
   72.20 +#ifdef QUANT_1PASS_SUPPORTED
   72.21 +
   72.22 +
   72.23 +/*
   72.24 + * The main purpose of 1-pass quantization is to provide a fast, if not very
   72.25 + * high quality, colormapped output capability.  A 2-pass quantizer usually
   72.26 + * gives better visual quality; however, for quantized grayscale output this
   72.27 + * quantizer is perfectly adequate.  Dithering is highly recommended with this
   72.28 + * quantizer, though you can turn it off if you really want to.
   72.29 + *
   72.30 + * In 1-pass quantization the colormap must be chosen in advance of seeing the
   72.31 + * image.  We use a map consisting of all combinations of Ncolors[i] color
   72.32 + * values for the i'th component.  The Ncolors[] values are chosen so that
   72.33 + * their product, the total number of colors, is no more than that requested.
   72.34 + * (In most cases, the product will be somewhat less.)
   72.35 + *
   72.36 + * Since the colormap is orthogonal, the representative value for each color
   72.37 + * component can be determined without considering the other components;
   72.38 + * then these indexes can be combined into a colormap index by a standard
   72.39 + * N-dimensional-array-subscript calculation.  Most of the arithmetic involved
   72.40 + * can be precalculated and stored in the lookup table colorindex[].
   72.41 + * colorindex[i][j] maps pixel value j in component i to the nearest
   72.42 + * representative value (grid plane) for that component; this index is
   72.43 + * multiplied by the array stride for component i, so that the
   72.44 + * index of the colormap entry closest to a given pixel value is just
   72.45 + *    sum( colorindex[component-number][pixel-component-value] )
   72.46 + * Aside from being fast, this scheme allows for variable spacing between
   72.47 + * representative values with no additional lookup cost.
   72.48 + *
   72.49 + * If gamma correction has been applied in color conversion, it might be wise
   72.50 + * to adjust the color grid spacing so that the representative colors are
   72.51 + * equidistant in linear space.  At this writing, gamma correction is not
   72.52 + * implemented by jdcolor, so nothing is done here.
   72.53 + */
   72.54 +
   72.55 +
   72.56 +/* Declarations for ordered dithering.
   72.57 + *
   72.58 + * We use a standard 16x16 ordered dither array.  The basic concept of ordered
   72.59 + * dithering is described in many references, for instance Dale Schumacher's
   72.60 + * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
   72.61 + * In place of Schumacher's comparisons against a "threshold" value, we add a
   72.62 + * "dither" value to the input pixel and then round the result to the nearest
   72.63 + * output value.  The dither value is equivalent to (0.5 - threshold) times
   72.64 + * the distance between output values.  For ordered dithering, we assume that
   72.65 + * the output colors are equally spaced; if not, results will probably be
   72.66 + * worse, since the dither may be too much or too little at a given point.
   72.67 + *
   72.68 + * The normal calculation would be to form pixel value + dither, range-limit
   72.69 + * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
   72.70 + * We can skip the separate range-limiting step by extending the colorindex
   72.71 + * table in both directions.
   72.72 + */
   72.73 +
   72.74 +#define ODITHER_SIZE  16	/* dimension of dither matrix */
   72.75 +/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
   72.76 +#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE)	/* # cells in matrix */
   72.77 +#define ODITHER_MASK  (ODITHER_SIZE-1) /* mask for wrapping around counters */
   72.78 +
   72.79 +typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
   72.80 +typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
   72.81 +
   72.82 +static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
   72.83 +  /* Bayer's order-4 dither array.  Generated by the code given in
   72.84 +   * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
   72.85 +   * The values in this array must range from 0 to ODITHER_CELLS-1.
   72.86 +   */
   72.87 +  {   0,192, 48,240, 12,204, 60,252,  3,195, 51,243, 15,207, 63,255 },
   72.88 +  { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
   72.89 +  {  32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
   72.90 +  { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
   72.91 +  {   8,200, 56,248,  4,196, 52,244, 11,203, 59,251,  7,199, 55,247 },
   72.92 +  { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
   72.93 +  {  40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
   72.94 +  { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
   72.95 +  {   2,194, 50,242, 14,206, 62,254,  1,193, 49,241, 13,205, 61,253 },
   72.96 +  { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
   72.97 +  {  34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
   72.98 +  { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
   72.99 +  {  10,202, 58,250,  6,198, 54,246,  9,201, 57,249,  5,197, 53,245 },
  72.100 +  { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
  72.101 +  {  42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
  72.102 +  { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
  72.103 +};
  72.104 +
  72.105 +
  72.106 +/* Declarations for Floyd-Steinberg dithering.
  72.107 + *
  72.108 + * Errors are accumulated into the array fserrors[], at a resolution of
  72.109 + * 1/16th of a pixel count.  The error at a given pixel is propagated
  72.110 + * to its not-yet-processed neighbors using the standard F-S fractions,
  72.111 + *		...	(here)	7/16
  72.112 + *		3/16	5/16	1/16
  72.113 + * We work left-to-right on even rows, right-to-left on odd rows.
  72.114 + *
  72.115 + * We can get away with a single array (holding one row's worth of errors)
  72.116 + * by using it to store the current row's errors at pixel columns not yet
  72.117 + * processed, but the next row's errors at columns already processed.  We
  72.118 + * need only a few extra variables to hold the errors immediately around the
  72.119 + * current column.  (If we are lucky, those variables are in registers, but
  72.120 + * even if not, they're probably cheaper to access than array elements are.)
  72.121 + *
  72.122 + * The fserrors[] array is indexed [component#][position].
  72.123 + * We provide (#columns + 2) entries per component; the extra entry at each
  72.124 + * end saves us from special-casing the first and last pixels.
  72.125 + *
  72.126 + * Note: on a wide image, we might not have enough room in a PC's near data
  72.127 + * segment to hold the error array; so it is allocated with alloc_large.
  72.128 + */
  72.129 +
  72.130 +#if BITS_IN_JSAMPLE == 8
  72.131 +typedef INT16 FSERROR;		/* 16 bits should be enough */
  72.132 +typedef int LOCFSERROR;		/* use 'int' for calculation temps */
  72.133 +#else
  72.134 +typedef INT32 FSERROR;		/* may need more than 16 bits */
  72.135 +typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
  72.136 +#endif
  72.137 +
  72.138 +typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
  72.139 +
  72.140 +
  72.141 +/* Private subobject */
  72.142 +
  72.143 +#define MAX_Q_COMPS 4		/* max components I can handle */
  72.144 +
  72.145 +typedef struct {
  72.146 +  struct jpeg_color_quantizer pub; /* public fields */
  72.147 +
  72.148 +  /* Initially allocated colormap is saved here */
  72.149 +  JSAMPARRAY sv_colormap;	/* The color map as a 2-D pixel array */
  72.150 +  int sv_actual;		/* number of entries in use */
  72.151 +
  72.152 +  JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
  72.153 +  /* colorindex[i][j] = index of color closest to pixel value j in component i,
  72.154 +   * premultiplied as described above.  Since colormap indexes must fit into
  72.155 +   * JSAMPLEs, the entries of this array will too.
  72.156 +   */
  72.157 +  boolean is_padded;		/* is the colorindex padded for odither? */
  72.158 +
  72.159 +  int Ncolors[MAX_Q_COMPS];	/* # of values alloced to each component */
  72.160 +
  72.161 +  /* Variables for ordered dithering */
  72.162 +  int row_index;		/* cur row's vertical index in dither matrix */
  72.163 +  ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
  72.164 +
  72.165 +  /* Variables for Floyd-Steinberg dithering */
  72.166 +  FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
  72.167 +  boolean on_odd_row;		/* flag to remember which row we are on */
  72.168 +} my_cquantizer;
  72.169 +
  72.170 +typedef my_cquantizer * my_cquantize_ptr;
  72.171 +
  72.172 +
  72.173 +/*
  72.174 + * Policy-making subroutines for create_colormap and create_colorindex.
  72.175 + * These routines determine the colormap to be used.  The rest of the module
  72.176 + * only assumes that the colormap is orthogonal.
  72.177 + *
  72.178 + *  * select_ncolors decides how to divvy up the available colors
  72.179 + *    among the components.
  72.180 + *  * output_value defines the set of representative values for a component.
  72.181 + *  * largest_input_value defines the mapping from input values to
  72.182 + *    representative values for a component.
  72.183 + * Note that the latter two routines may impose different policies for
  72.184 + * different components, though this is not currently done.
  72.185 + */
  72.186 +
  72.187 +
  72.188 +LOCAL(int)
  72.189 +select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
  72.190 +/* Determine allocation of desired colors to components, */
  72.191 +/* and fill in Ncolors[] array to indicate choice. */
  72.192 +/* Return value is total number of colors (product of Ncolors[] values). */
  72.193 +{
  72.194 +  int nc = cinfo->out_color_components; /* number of color components */
  72.195 +  int max_colors = cinfo->desired_number_of_colors;
  72.196 +  int total_colors, iroot, i, j;
  72.197 +  boolean changed;
  72.198 +  long temp;
  72.199 +  static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
  72.200 +
  72.201 +  /* We can allocate at least the nc'th root of max_colors per component. */
  72.202 +  /* Compute floor(nc'th root of max_colors). */
  72.203 +  iroot = 1;
  72.204 +  do {
  72.205 +    iroot++;
  72.206 +    temp = iroot;		/* set temp = iroot ** nc */
  72.207 +    for (i = 1; i < nc; i++)
  72.208 +      temp *= iroot;
  72.209 +  } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
  72.210 +  iroot--;			/* now iroot = floor(root) */
  72.211 +
  72.212 +  /* Must have at least 2 color values per component */
  72.213 +  if (iroot < 2)
  72.214 +    ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
  72.215 +
  72.216 +  /* Initialize to iroot color values for each component */
  72.217 +  total_colors = 1;
  72.218 +  for (i = 0; i < nc; i++) {
  72.219 +    Ncolors[i] = iroot;
  72.220 +    total_colors *= iroot;
  72.221 +  }
  72.222 +  /* We may be able to increment the count for one or more components without
  72.223 +   * exceeding max_colors, though we know not all can be incremented.
  72.224 +   * Sometimes, the first component can be incremented more than once!
  72.225 +   * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
  72.226 +   * In RGB colorspace, try to increment G first, then R, then B.
  72.227 +   */
  72.228 +  do {
  72.229 +    changed = FALSE;
  72.230 +    for (i = 0; i < nc; i++) {
  72.231 +      j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
  72.232 +      /* calculate new total_colors if Ncolors[j] is incremented */
  72.233 +      temp = total_colors / Ncolors[j];
  72.234 +      temp *= Ncolors[j]+1;	/* done in long arith to avoid oflo */
  72.235 +      if (temp > (long) max_colors)
  72.236 +	break;			/* won't fit, done with this pass */
  72.237 +      Ncolors[j]++;		/* OK, apply the increment */
  72.238 +      total_colors = (int) temp;
  72.239 +      changed = TRUE;
  72.240 +    }
  72.241 +  } while (changed);
  72.242 +
  72.243 +  return total_colors;
  72.244 +}
  72.245 +
  72.246 +
  72.247 +LOCAL(int)
  72.248 +output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
  72.249 +/* Return j'th output value, where j will range from 0 to maxj */
  72.250 +/* The output values must fall in 0..MAXJSAMPLE in increasing order */
  72.251 +{
  72.252 +  /* We always provide values 0 and MAXJSAMPLE for each component;
  72.253 +   * any additional values are equally spaced between these limits.
  72.254 +   * (Forcing the upper and lower values to the limits ensures that
  72.255 +   * dithering can't produce a color outside the selected gamut.)
  72.256 +   */
  72.257 +  return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
  72.258 +}
  72.259 +
  72.260 +
  72.261 +LOCAL(int)
  72.262 +largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
  72.263 +/* Return largest input value that should map to j'th output value */
  72.264 +/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
  72.265 +{
  72.266 +  /* Breakpoints are halfway between values returned by output_value */
  72.267 +  return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
  72.268 +}
  72.269 +
  72.270 +
  72.271 +/*
  72.272 + * Create the colormap.
  72.273 + */
  72.274 +
  72.275 +LOCAL(void)
  72.276 +create_colormap (j_decompress_ptr cinfo)
  72.277 +{
  72.278 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.279 +  JSAMPARRAY colormap;		/* Created colormap */
  72.280 +  int total_colors;		/* Number of distinct output colors */
  72.281 +  int i,j,k, nci, blksize, blkdist, ptr, val;
  72.282 +
  72.283 +  /* Select number of colors for each component */
  72.284 +  total_colors = select_ncolors(cinfo, cquantize->Ncolors);
  72.285 +
  72.286 +  /* Report selected color counts */
  72.287 +  if (cinfo->out_color_components == 3)
  72.288 +    TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
  72.289 +	     total_colors, cquantize->Ncolors[0],
  72.290 +	     cquantize->Ncolors[1], cquantize->Ncolors[2]);
  72.291 +  else
  72.292 +    TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
  72.293 +
  72.294 +  /* Allocate and fill in the colormap. */
  72.295 +  /* The colors are ordered in the map in standard row-major order, */
  72.296 +  /* i.e. rightmost (highest-indexed) color changes most rapidly. */
  72.297 +
  72.298 +  colormap = (*cinfo->mem->alloc_sarray)
  72.299 +    ((j_common_ptr) cinfo, JPOOL_IMAGE,
  72.300 +     (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
  72.301 +
  72.302 +  /* blksize is number of adjacent repeated entries for a component */
  72.303 +  /* blkdist is distance between groups of identical entries for a component */
  72.304 +  blkdist = total_colors;
  72.305 +
  72.306 +  for (i = 0; i < cinfo->out_color_components; i++) {
  72.307 +    /* fill in colormap entries for i'th color component */
  72.308 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  72.309 +    blksize = blkdist / nci;
  72.310 +    for (j = 0; j < nci; j++) {
  72.311 +      /* Compute j'th output value (out of nci) for component */
  72.312 +      val = output_value(cinfo, i, j, nci-1);
  72.313 +      /* Fill in all colormap entries that have this value of this component */
  72.314 +      for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
  72.315 +	/* fill in blksize entries beginning at ptr */
  72.316 +	for (k = 0; k < blksize; k++)
  72.317 +	  colormap[i][ptr+k] = (JSAMPLE) val;
  72.318 +      }
  72.319 +    }
  72.320 +    blkdist = blksize;		/* blksize of this color is blkdist of next */
  72.321 +  }
  72.322 +
  72.323 +  /* Save the colormap in private storage,
  72.324 +   * where it will survive color quantization mode changes.
  72.325 +   */
  72.326 +  cquantize->sv_colormap = colormap;
  72.327 +  cquantize->sv_actual = total_colors;
  72.328 +}
  72.329 +
  72.330 +
  72.331 +/*
  72.332 + * Create the color index table.
  72.333 + */
  72.334 +
  72.335 +LOCAL(void)
  72.336 +create_colorindex (j_decompress_ptr cinfo)
  72.337 +{
  72.338 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.339 +  JSAMPROW indexptr;
  72.340 +  int i,j,k, nci, blksize, val, pad;
  72.341 +
  72.342 +  /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
  72.343 +   * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
  72.344 +   * This is not necessary in the other dithering modes.  However, we
  72.345 +   * flag whether it was done in case user changes dithering mode.
  72.346 +   */
  72.347 +  if (cinfo->dither_mode == JDITHER_ORDERED) {
  72.348 +    pad = MAXJSAMPLE*2;
  72.349 +    cquantize->is_padded = TRUE;
  72.350 +  } else {
  72.351 +    pad = 0;
  72.352 +    cquantize->is_padded = FALSE;
  72.353 +  }
  72.354 +
  72.355 +  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
  72.356 +    ((j_common_ptr) cinfo, JPOOL_IMAGE,
  72.357 +     (JDIMENSION) (MAXJSAMPLE+1 + pad),
  72.358 +     (JDIMENSION) cinfo->out_color_components);
  72.359 +
  72.360 +  /* blksize is number of adjacent repeated entries for a component */
  72.361 +  blksize = cquantize->sv_actual;
  72.362 +
  72.363 +  for (i = 0; i < cinfo->out_color_components; i++) {
  72.364 +    /* fill in colorindex entries for i'th color component */
  72.365 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  72.366 +    blksize = blksize / nci;
  72.367 +
  72.368 +    /* adjust colorindex pointers to provide padding at negative indexes. */
  72.369 +    if (pad)
  72.370 +      cquantize->colorindex[i] += MAXJSAMPLE;
  72.371 +
  72.372 +    /* in loop, val = index of current output value, */
  72.373 +    /* and k = largest j that maps to current val */
  72.374 +    indexptr = cquantize->colorindex[i];
  72.375 +    val = 0;
  72.376 +    k = largest_input_value(cinfo, i, 0, nci-1);
  72.377 +    for (j = 0; j <= MAXJSAMPLE; j++) {
  72.378 +      while (j > k)		/* advance val if past boundary */
  72.379 +	k = largest_input_value(cinfo, i, ++val, nci-1);
  72.380 +      /* premultiply so that no multiplication needed in main processing */
  72.381 +      indexptr[j] = (JSAMPLE) (val * blksize);
  72.382 +    }
  72.383 +    /* Pad at both ends if necessary */
  72.384 +    if (pad)
  72.385 +      for (j = 1; j <= MAXJSAMPLE; j++) {
  72.386 +	indexptr[-j] = indexptr[0];
  72.387 +	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
  72.388 +      }
  72.389 +  }
  72.390 +}
  72.391 +
  72.392 +
  72.393 +/*
  72.394 + * Create an ordered-dither array for a component having ncolors
  72.395 + * distinct output values.
  72.396 + */
  72.397 +
  72.398 +LOCAL(ODITHER_MATRIX_PTR)
  72.399 +make_odither_array (j_decompress_ptr cinfo, int ncolors)
  72.400 +{
  72.401 +  ODITHER_MATRIX_PTR odither;
  72.402 +  int j,k;
  72.403 +  INT32 num,den;
  72.404 +
  72.405 +  odither = (ODITHER_MATRIX_PTR)
  72.406 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  72.407 +				SIZEOF(ODITHER_MATRIX));
  72.408 +  /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
  72.409 +   * Hence the dither value for the matrix cell with fill order f
  72.410 +   * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
  72.411 +   * On 16-bit-int machine, be careful to avoid overflow.
  72.412 +   */
  72.413 +  den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
  72.414 +  for (j = 0; j < ODITHER_SIZE; j++) {
  72.415 +    for (k = 0; k < ODITHER_SIZE; k++) {
  72.416 +      num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
  72.417 +	    * MAXJSAMPLE;
  72.418 +      /* Ensure round towards zero despite C's lack of consistency
  72.419 +       * about rounding negative values in integer division...
  72.420 +       */
  72.421 +      odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
  72.422 +    }
  72.423 +  }
  72.424 +  return odither;
  72.425 +}
  72.426 +
  72.427 +
  72.428 +/*
  72.429 + * Create the ordered-dither tables.
  72.430 + * Components having the same number of representative colors may 
  72.431 + * share a dither table.
  72.432 + */
  72.433 +
  72.434 +LOCAL(void)
  72.435 +create_odither_tables (j_decompress_ptr cinfo)
  72.436 +{
  72.437 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.438 +  ODITHER_MATRIX_PTR odither;
  72.439 +  int i, j, nci;
  72.440 +
  72.441 +  for (i = 0; i < cinfo->out_color_components; i++) {
  72.442 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  72.443 +    odither = NULL;		/* search for matching prior component */
  72.444 +    for (j = 0; j < i; j++) {
  72.445 +      if (nci == cquantize->Ncolors[j]) {
  72.446 +	odither = cquantize->odither[j];
  72.447 +	break;
  72.448 +      }
  72.449 +    }
  72.450 +    if (odither == NULL)	/* need a new table? */
  72.451 +      odither = make_odither_array(cinfo, nci);
  72.452 +    cquantize->odither[i] = odither;
  72.453 +  }
  72.454 +}
  72.455 +
  72.456 +
  72.457 +/*
  72.458 + * Map some rows of pixels to the output colormapped representation.
  72.459 + */
  72.460 +
  72.461 +METHODDEF(void)
  72.462 +color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  72.463 +		JSAMPARRAY output_buf, int num_rows)
  72.464 +/* General case, no dithering */
  72.465 +{
  72.466 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.467 +  JSAMPARRAY colorindex = cquantize->colorindex;
  72.468 +  register int pixcode, ci;
  72.469 +  register JSAMPROW ptrin, ptrout;
  72.470 +  int row;
  72.471 +  JDIMENSION col;
  72.472 +  JDIMENSION width = cinfo->output_width;
  72.473 +  register int nc = cinfo->out_color_components;
  72.474 +
  72.475 +  for (row = 0; row < num_rows; row++) {
  72.476 +    ptrin = input_buf[row];
  72.477 +    ptrout = output_buf[row];
  72.478 +    for (col = width; col > 0; col--) {
  72.479 +      pixcode = 0;
  72.480 +      for (ci = 0; ci < nc; ci++) {
  72.481 +	pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
  72.482 +      }
  72.483 +      *ptrout++ = (JSAMPLE) pixcode;
  72.484 +    }
  72.485 +  }
  72.486 +}
  72.487 +
  72.488 +
  72.489 +METHODDEF(void)
  72.490 +color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  72.491 +		 JSAMPARRAY output_buf, int num_rows)
  72.492 +/* Fast path for out_color_components==3, no dithering */
  72.493 +{
  72.494 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.495 +  register int pixcode;
  72.496 +  register JSAMPROW ptrin, ptrout;
  72.497 +  JSAMPROW colorindex0 = cquantize->colorindex[0];
  72.498 +  JSAMPROW colorindex1 = cquantize->colorindex[1];
  72.499 +  JSAMPROW colorindex2 = cquantize->colorindex[2];
  72.500 +  int row;
  72.501 +  JDIMENSION col;
  72.502 +  JDIMENSION width = cinfo->output_width;
  72.503 +
  72.504 +  for (row = 0; row < num_rows; row++) {
  72.505 +    ptrin = input_buf[row];
  72.506 +    ptrout = output_buf[row];
  72.507 +    for (col = width; col > 0; col--) {
  72.508 +      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
  72.509 +      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
  72.510 +      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
  72.511 +      *ptrout++ = (JSAMPLE) pixcode;
  72.512 +    }
  72.513 +  }
  72.514 +}
  72.515 +
  72.516 +
  72.517 +METHODDEF(void)
  72.518 +quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  72.519 +		     JSAMPARRAY output_buf, int num_rows)
  72.520 +/* General case, with ordered dithering */
  72.521 +{
  72.522 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.523 +  register JSAMPROW input_ptr;
  72.524 +  register JSAMPROW output_ptr;
  72.525 +  JSAMPROW colorindex_ci;
  72.526 +  int * dither;			/* points to active row of dither matrix */
  72.527 +  int row_index, col_index;	/* current indexes into dither matrix */
  72.528 +  int nc = cinfo->out_color_components;
  72.529 +  int ci;
  72.530 +  int row;
  72.531 +  JDIMENSION col;
  72.532 +  JDIMENSION width = cinfo->output_width;
  72.533 +
  72.534 +  for (row = 0; row < num_rows; row++) {
  72.535 +    /* Initialize output values to 0 so can process components separately */
  72.536 +    jzero_far((void FAR *) output_buf[row],
  72.537 +	      (size_t) (width * SIZEOF(JSAMPLE)));
  72.538 +    row_index = cquantize->row_index;
  72.539 +    for (ci = 0; ci < nc; ci++) {
  72.540 +      input_ptr = input_buf[row] + ci;
  72.541 +      output_ptr = output_buf[row];
  72.542 +      colorindex_ci = cquantize->colorindex[ci];
  72.543 +      dither = cquantize->odither[ci][row_index];
  72.544 +      col_index = 0;
  72.545 +
  72.546 +      for (col = width; col > 0; col--) {
  72.547 +	/* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
  72.548 +	 * select output value, accumulate into output code for this pixel.
  72.549 +	 * Range-limiting need not be done explicitly, as we have extended
  72.550 +	 * the colorindex table to produce the right answers for out-of-range
  72.551 +	 * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
  72.552 +	 * required amount of padding.
  72.553 +	 */
  72.554 +	*output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
  72.555 +	input_ptr += nc;
  72.556 +	output_ptr++;
  72.557 +	col_index = (col_index + 1) & ODITHER_MASK;
  72.558 +      }
  72.559 +    }
  72.560 +    /* Advance row index for next row */
  72.561 +    row_index = (row_index + 1) & ODITHER_MASK;
  72.562 +    cquantize->row_index = row_index;
  72.563 +  }
  72.564 +}
  72.565 +
  72.566 +
  72.567 +METHODDEF(void)
  72.568 +quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  72.569 +		      JSAMPARRAY output_buf, int num_rows)
  72.570 +/* Fast path for out_color_components==3, with ordered dithering */
  72.571 +{
  72.572 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.573 +  register int pixcode;
  72.574 +  register JSAMPROW input_ptr;
  72.575 +  register JSAMPROW output_ptr;
  72.576 +  JSAMPROW colorindex0 = cquantize->colorindex[0];
  72.577 +  JSAMPROW colorindex1 = cquantize->colorindex[1];
  72.578 +  JSAMPROW colorindex2 = cquantize->colorindex[2];
  72.579 +  int * dither0;		/* points to active row of dither matrix */
  72.580 +  int * dither1;
  72.581 +  int * dither2;
  72.582 +  int row_index, col_index;	/* current indexes into dither matrix */
  72.583 +  int row;
  72.584 +  JDIMENSION col;
  72.585 +  JDIMENSION width = cinfo->output_width;
  72.586 +
  72.587 +  for (row = 0; row < num_rows; row++) {
  72.588 +    row_index = cquantize->row_index;
  72.589 +    input_ptr = input_buf[row];
  72.590 +    output_ptr = output_buf[row];
  72.591 +    dither0 = cquantize->odither[0][row_index];
  72.592 +    dither1 = cquantize->odither[1][row_index];
  72.593 +    dither2 = cquantize->odither[2][row_index];
  72.594 +    col_index = 0;
  72.595 +
  72.596 +    for (col = width; col > 0; col--) {
  72.597 +      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
  72.598 +					dither0[col_index]]);
  72.599 +      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
  72.600 +					dither1[col_index]]);
  72.601 +      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
  72.602 +					dither2[col_index]]);
  72.603 +      *output_ptr++ = (JSAMPLE) pixcode;
  72.604 +      col_index = (col_index + 1) & ODITHER_MASK;
  72.605 +    }
  72.606 +    row_index = (row_index + 1) & ODITHER_MASK;
  72.607 +    cquantize->row_index = row_index;
  72.608 +  }
  72.609 +}
  72.610 +
  72.611 +
  72.612 +METHODDEF(void)
  72.613 +quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  72.614 +		    JSAMPARRAY output_buf, int num_rows)
  72.615 +/* General case, with Floyd-Steinberg dithering */
  72.616 +{
  72.617 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.618 +  register LOCFSERROR cur;	/* current error or pixel value */
  72.619 +  LOCFSERROR belowerr;		/* error for pixel below cur */
  72.620 +  LOCFSERROR bpreverr;		/* error for below/prev col */
  72.621 +  LOCFSERROR bnexterr;		/* error for below/next col */
  72.622 +  LOCFSERROR delta;
  72.623 +  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  72.624 +  register JSAMPROW input_ptr;
  72.625 +  register JSAMPROW output_ptr;
  72.626 +  JSAMPROW colorindex_ci;
  72.627 +  JSAMPROW colormap_ci;
  72.628 +  int pixcode;
  72.629 +  int nc = cinfo->out_color_components;
  72.630 +  int dir;			/* 1 for left-to-right, -1 for right-to-left */
  72.631 +  int dirnc;			/* dir * nc */
  72.632 +  int ci;
  72.633 +  int row;
  72.634 +  JDIMENSION col;
  72.635 +  JDIMENSION width = cinfo->output_width;
  72.636 +  JSAMPLE *range_limit = cinfo->sample_range_limit;
  72.637 +  SHIFT_TEMPS
  72.638 +
  72.639 +  for (row = 0; row < num_rows; row++) {
  72.640 +    /* Initialize output values to 0 so can process components separately */
  72.641 +    jzero_far((void FAR *) output_buf[row],
  72.642 +	      (size_t) (width * SIZEOF(JSAMPLE)));
  72.643 +    for (ci = 0; ci < nc; ci++) {
  72.644 +      input_ptr = input_buf[row] + ci;
  72.645 +      output_ptr = output_buf[row];
  72.646 +      if (cquantize->on_odd_row) {
  72.647 +	/* work right to left in this row */
  72.648 +	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
  72.649 +	output_ptr += width-1;
  72.650 +	dir = -1;
  72.651 +	dirnc = -nc;
  72.652 +	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
  72.653 +      } else {
  72.654 +	/* work left to right in this row */
  72.655 +	dir = 1;
  72.656 +	dirnc = nc;
  72.657 +	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
  72.658 +      }
  72.659 +      colorindex_ci = cquantize->colorindex[ci];
  72.660 +      colormap_ci = cquantize->sv_colormap[ci];
  72.661 +      /* Preset error values: no error propagated to first pixel from left */
  72.662 +      cur = 0;
  72.663 +      /* and no error propagated to row below yet */
  72.664 +      belowerr = bpreverr = 0;
  72.665 +
  72.666 +      for (col = width; col > 0; col--) {
  72.667 +	/* cur holds the error propagated from the previous pixel on the
  72.668 +	 * current line.  Add the error propagated from the previous line
  72.669 +	 * to form the complete error correction term for this pixel, and
  72.670 +	 * round the error term (which is expressed * 16) to an integer.
  72.671 +	 * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
  72.672 +	 * for either sign of the error value.
  72.673 +	 * Note: errorptr points to *previous* column's array entry.
  72.674 +	 */
  72.675 +	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
  72.676 +	/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
  72.677 +	 * The maximum error is +- MAXJSAMPLE; this sets the required size
  72.678 +	 * of the range_limit array.
  72.679 +	 */
  72.680 +	cur += GETJSAMPLE(*input_ptr);
  72.681 +	cur = GETJSAMPLE(range_limit[cur]);
  72.682 +	/* Select output value, accumulate into output code for this pixel */
  72.683 +	pixcode = GETJSAMPLE(colorindex_ci[cur]);
  72.684 +	*output_ptr += (JSAMPLE) pixcode;
  72.685 +	/* Compute actual representation error at this pixel */
  72.686 +	/* Note: we can do this even though we don't have the final */
  72.687 +	/* pixel code, because the colormap is orthogonal. */
  72.688 +	cur -= GETJSAMPLE(colormap_ci[pixcode]);
  72.689 +	/* Compute error fractions to be propagated to adjacent pixels.
  72.690 +	 * Add these into the running sums, and simultaneously shift the
  72.691 +	 * next-line error sums left by 1 column.
  72.692 +	 */
  72.693 +	bnexterr = cur;
  72.694 +	delta = cur * 2;
  72.695 +	cur += delta;		/* form error * 3 */
  72.696 +	errorptr[0] = (FSERROR) (bpreverr + cur);
  72.697 +	cur += delta;		/* form error * 5 */
  72.698 +	bpreverr = belowerr + cur;
  72.699 +	belowerr = bnexterr;
  72.700 +	cur += delta;		/* form error * 7 */
  72.701 +	/* At this point cur contains the 7/16 error value to be propagated
  72.702 +	 * to the next pixel on the current line, and all the errors for the
  72.703 +	 * next line have been shifted over. We are therefore ready to move on.
  72.704 +	 */
  72.705 +	input_ptr += dirnc;	/* advance input ptr to next column */
  72.706 +	output_ptr += dir;	/* advance output ptr to next column */
  72.707 +	errorptr += dir;	/* advance errorptr to current column */
  72.708 +      }
  72.709 +      /* Post-loop cleanup: we must unload the final error value into the
  72.710 +       * final fserrors[] entry.  Note we need not unload belowerr because
  72.711 +       * it is for the dummy column before or after the actual array.
  72.712 +       */
  72.713 +      errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
  72.714 +    }
  72.715 +    cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
  72.716 +  }
  72.717 +}
  72.718 +
  72.719 +
  72.720 +/*
  72.721 + * Allocate workspace for Floyd-Steinberg errors.
  72.722 + */
  72.723 +
  72.724 +LOCAL(void)
  72.725 +alloc_fs_workspace (j_decompress_ptr cinfo)
  72.726 +{
  72.727 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.728 +  size_t arraysize;
  72.729 +  int i;
  72.730 +
  72.731 +  arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
  72.732 +  for (i = 0; i < cinfo->out_color_components; i++) {
  72.733 +    cquantize->fserrors[i] = (FSERRPTR)
  72.734 +      (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
  72.735 +  }
  72.736 +}
  72.737 +
  72.738 +
  72.739 +/*
  72.740 + * Initialize for one-pass color quantization.
  72.741 + */
  72.742 +
  72.743 +METHODDEF(void)
  72.744 +start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
  72.745 +{
  72.746 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  72.747 +  size_t arraysize;
  72.748 +  int i;
  72.749 +
  72.750 +  /* Install my colormap. */
  72.751 +  cinfo->colormap = cquantize->sv_colormap;
  72.752 +  cinfo->actual_number_of_colors = cquantize->sv_actual;
  72.753 +
  72.754 +  /* Initialize for desired dithering mode. */
  72.755 +  switch (cinfo->dither_mode) {
  72.756 +  case JDITHER_NONE:
  72.757 +    if (cinfo->out_color_components == 3)
  72.758 +      cquantize->pub.color_quantize = color_quantize3;
  72.759 +    else
  72.760 +      cquantize->pub.color_quantize = color_quantize;
  72.761 +    break;
  72.762 +  case JDITHER_ORDERED:
  72.763 +    if (cinfo->out_color_components == 3)
  72.764 +      cquantize->pub.color_quantize = quantize3_ord_dither;
  72.765 +    else
  72.766 +      cquantize->pub.color_quantize = quantize_ord_dither;
  72.767 +    cquantize->row_index = 0;	/* initialize state for ordered dither */
  72.768 +    /* If user changed to ordered dither from another mode,
  72.769 +     * we must recreate the color index table with padding.
  72.770 +     * This will cost extra space, but probably isn't very likely.
  72.771 +     */
  72.772 +    if (! cquantize->is_padded)
  72.773 +      create_colorindex(cinfo);
  72.774 +    /* Create ordered-dither tables if we didn't already. */
  72.775 +    if (cquantize->odither[0] == NULL)
  72.776 +      create_odither_tables(cinfo);
  72.777 +    break;
  72.778 +  case JDITHER_FS:
  72.779 +    cquantize->pub.color_quantize = quantize_fs_dither;
  72.780 +    cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
  72.781 +    /* Allocate Floyd-Steinberg workspace if didn't already. */
  72.782 +    if (cquantize->fserrors[0] == NULL)
  72.783 +      alloc_fs_workspace(cinfo);
  72.784 +    /* Initialize the propagated errors to zero. */
  72.785 +    arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
  72.786 +    for (i = 0; i < cinfo->out_color_components; i++)
  72.787 +      jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
  72.788 +    break;
  72.789 +  default:
  72.790 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  72.791 +    break;
  72.792 +  }
  72.793 +}
  72.794 +
  72.795 +
  72.796 +/*
  72.797 + * Finish up at the end of the pass.
  72.798 + */
  72.799 +
  72.800 +METHODDEF(void)
  72.801 +finish_pass_1_quant (j_decompress_ptr cinfo)
  72.802 +{
  72.803 +  /* no work in 1-pass case */
  72.804 +}
  72.805 +
  72.806 +
  72.807 +/*
  72.808 + * Switch to a new external colormap between output passes.
  72.809 + * Shouldn't get to this module!
  72.810 + */
  72.811 +
  72.812 +METHODDEF(void)
  72.813 +new_color_map_1_quant (j_decompress_ptr cinfo)
  72.814 +{
  72.815 +  ERREXIT(cinfo, JERR_MODE_CHANGE);
  72.816 +}
  72.817 +
  72.818 +
  72.819 +/*
  72.820 + * Module initialization routine for 1-pass color quantization.
  72.821 + */
  72.822 +
  72.823 +GLOBAL(void)
  72.824 +jinit_1pass_quantizer (j_decompress_ptr cinfo)
  72.825 +{
  72.826 +  my_cquantize_ptr cquantize;
  72.827 +
  72.828 +  cquantize = (my_cquantize_ptr)
  72.829 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  72.830 +				SIZEOF(my_cquantizer));
  72.831 +  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  72.832 +  cquantize->pub.start_pass = start_pass_1_quant;
  72.833 +  cquantize->pub.finish_pass = finish_pass_1_quant;
  72.834 +  cquantize->pub.new_color_map = new_color_map_1_quant;
  72.835 +  cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
  72.836 +  cquantize->odither[0] = NULL;	/* Also flag odither arrays not allocated */
  72.837 +
  72.838 +  /* Make sure my internal arrays won't overflow */
  72.839 +  if (cinfo->out_color_components > MAX_Q_COMPS)
  72.840 +    ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
  72.841 +  /* Make sure colormap indexes can be represented by JSAMPLEs */
  72.842 +  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
  72.843 +    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
  72.844 +
  72.845 +  /* Create the colormap and color index table. */
  72.846 +  create_colormap(cinfo);
  72.847 +  create_colorindex(cinfo);
  72.848 +
  72.849 +  /* Allocate Floyd-Steinberg workspace now if requested.
  72.850 +   * We do this now since it is FAR storage and may affect the memory
  72.851 +   * manager's space calculations.  If the user changes to FS dither
  72.852 +   * mode in a later pass, we will allocate the space then, and will
  72.853 +   * possibly overrun the max_memory_to_use setting.
  72.854 +   */
  72.855 +  if (cinfo->dither_mode == JDITHER_FS)
  72.856 +    alloc_fs_workspace(cinfo);
  72.857 +}
  72.858 +
  72.859 +#endif /* QUANT_1PASS_SUPPORTED */
    73.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    73.2 +++ b/libs/libjpeg/jquant2.c	Sun Jun 07 17:25:49 2015 +0300
    73.3 @@ -0,0 +1,1310 @@
    73.4 +/*
    73.5 + * jquant2.c
    73.6 + *
    73.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    73.8 + * This file is part of the Independent JPEG Group's software.
    73.9 + * For conditions of distribution and use, see the accompanying README file.
   73.10 + *
   73.11 + * This file contains 2-pass color quantization (color mapping) routines.
   73.12 + * These routines provide selection of a custom color map for an image,
   73.13 + * followed by mapping of the image to that color map, with optional
   73.14 + * Floyd-Steinberg dithering.
   73.15 + * It is also possible to use just the second pass to map to an arbitrary
   73.16 + * externally-given color map.
   73.17 + *
   73.18 + * Note: ordered dithering is not supported, since there isn't any fast
   73.19 + * way to compute intercolor distances; it's unclear that ordered dither's
   73.20 + * fundamental assumptions even hold with an irregularly spaced color map.
   73.21 + */
   73.22 +
   73.23 +#define JPEG_INTERNALS
   73.24 +#include "jinclude.h"
   73.25 +#include "jpeglib.h"
   73.26 +
   73.27 +#ifdef QUANT_2PASS_SUPPORTED
   73.28 +
   73.29 +
   73.30 +/*
   73.31 + * This module implements the well-known Heckbert paradigm for color
   73.32 + * quantization.  Most of the ideas used here can be traced back to
   73.33 + * Heckbert's seminal paper
   73.34 + *   Heckbert, Paul.  "Color Image Quantization for Frame Buffer Display",
   73.35 + *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
   73.36 + *
   73.37 + * In the first pass over the image, we accumulate a histogram showing the
   73.38 + * usage count of each possible color.  To keep the histogram to a reasonable
   73.39 + * size, we reduce the precision of the input; typical practice is to retain
   73.40 + * 5 or 6 bits per color, so that 8 or 4 different input values are counted
   73.41 + * in the same histogram cell.
   73.42 + *
   73.43 + * Next, the color-selection step begins with a box representing the whole
   73.44 + * color space, and repeatedly splits the "largest" remaining box until we
   73.45 + * have as many boxes as desired colors.  Then the mean color in each
   73.46 + * remaining box becomes one of the possible output colors.
   73.47 + * 
   73.48 + * The second pass over the image maps each input pixel to the closest output
   73.49 + * color (optionally after applying a Floyd-Steinberg dithering correction).
   73.50 + * This mapping is logically trivial, but making it go fast enough requires
   73.51 + * considerable care.
   73.52 + *
   73.53 + * Heckbert-style quantizers vary a good deal in their policies for choosing
   73.54 + * the "largest" box and deciding where to cut it.  The particular policies
   73.55 + * used here have proved out well in experimental comparisons, but better ones
   73.56 + * may yet be found.
   73.57 + *
   73.58 + * In earlier versions of the IJG code, this module quantized in YCbCr color
   73.59 + * space, processing the raw upsampled data without a color conversion step.
   73.60 + * This allowed the color conversion math to be done only once per colormap
   73.61 + * entry, not once per pixel.  However, that optimization precluded other
   73.62 + * useful optimizations (such as merging color conversion with upsampling)
   73.63 + * and it also interfered with desired capabilities such as quantizing to an
   73.64 + * externally-supplied colormap.  We have therefore abandoned that approach.
   73.65 + * The present code works in the post-conversion color space, typically RGB.
   73.66 + *
   73.67 + * To improve the visual quality of the results, we actually work in scaled
   73.68 + * RGB space, giving G distances more weight than R, and R in turn more than
   73.69 + * B.  To do everything in integer math, we must use integer scale factors.
   73.70 + * The 2/3/1 scale factors used here correspond loosely to the relative
   73.71 + * weights of the colors in the NTSC grayscale equation.
   73.72 + * If you want to use this code to quantize a non-RGB color space, you'll
   73.73 + * probably need to change these scale factors.
   73.74 + */
   73.75 +
   73.76 +#define R_SCALE 2		/* scale R distances by this much */
   73.77 +#define G_SCALE 3		/* scale G distances by this much */
   73.78 +#define B_SCALE 1		/* and B by this much */
   73.79 +
   73.80 +/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
   73.81 + * in jmorecfg.h.  As the code stands, it will do the right thing for R,G,B
   73.82 + * and B,G,R orders.  If you define some other weird order in jmorecfg.h,
   73.83 + * you'll get compile errors until you extend this logic.  In that case
   73.84 + * you'll probably want to tweak the histogram sizes too.
   73.85 + */
   73.86 +
   73.87 +#if RGB_RED == 0
   73.88 +#define C0_SCALE R_SCALE
   73.89 +#endif
   73.90 +#if RGB_BLUE == 0
   73.91 +#define C0_SCALE B_SCALE
   73.92 +#endif
   73.93 +#if RGB_GREEN == 1
   73.94 +#define C1_SCALE G_SCALE
   73.95 +#endif
   73.96 +#if RGB_RED == 2
   73.97 +#define C2_SCALE R_SCALE
   73.98 +#endif
   73.99 +#if RGB_BLUE == 2
  73.100 +#define C2_SCALE B_SCALE
  73.101 +#endif
  73.102 +
  73.103 +
  73.104 +/*
  73.105 + * First we have the histogram data structure and routines for creating it.
  73.106 + *
  73.107 + * The number of bits of precision can be adjusted by changing these symbols.
  73.108 + * We recommend keeping 6 bits for G and 5 each for R and B.
  73.109 + * If you have plenty of memory and cycles, 6 bits all around gives marginally
  73.110 + * better results; if you are short of memory, 5 bits all around will save
  73.111 + * some space but degrade the results.
  73.112 + * To maintain a fully accurate histogram, we'd need to allocate a "long"
  73.113 + * (preferably unsigned long) for each cell.  In practice this is overkill;
  73.114 + * we can get by with 16 bits per cell.  Few of the cell counts will overflow,
  73.115 + * and clamping those that do overflow to the maximum value will give close-
  73.116 + * enough results.  This reduces the recommended histogram size from 256Kb
  73.117 + * to 128Kb, which is a useful savings on PC-class machines.
  73.118 + * (In the second pass the histogram space is re-used for pixel mapping data;
  73.119 + * in that capacity, each cell must be able to store zero to the number of
  73.120 + * desired colors.  16 bits/cell is plenty for that too.)
  73.121 + * Since the JPEG code is intended to run in small memory model on 80x86
  73.122 + * machines, we can't just allocate the histogram in one chunk.  Instead
  73.123 + * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each
  73.124 + * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
  73.125 + * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries.  Note that
  73.126 + * on 80x86 machines, the pointer row is in near memory but the actual
  73.127 + * arrays are in far memory (same arrangement as we use for image arrays).
  73.128 + */
  73.129 +
  73.130 +#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
  73.131 +
  73.132 +/* These will do the right thing for either R,G,B or B,G,R color order,
  73.133 + * but you may not like the results for other color orders.
  73.134 + */
  73.135 +#define HIST_C0_BITS  5		/* bits of precision in R/B histogram */
  73.136 +#define HIST_C1_BITS  6		/* bits of precision in G histogram */
  73.137 +#define HIST_C2_BITS  5		/* bits of precision in B/R histogram */
  73.138 +
  73.139 +/* Number of elements along histogram axes. */
  73.140 +#define HIST_C0_ELEMS  (1<<HIST_C0_BITS)
  73.141 +#define HIST_C1_ELEMS  (1<<HIST_C1_BITS)
  73.142 +#define HIST_C2_ELEMS  (1<<HIST_C2_BITS)
  73.143 +
  73.144 +/* These are the amounts to shift an input value to get a histogram index. */
  73.145 +#define C0_SHIFT  (BITS_IN_JSAMPLE-HIST_C0_BITS)
  73.146 +#define C1_SHIFT  (BITS_IN_JSAMPLE-HIST_C1_BITS)
  73.147 +#define C2_SHIFT  (BITS_IN_JSAMPLE-HIST_C2_BITS)
  73.148 +
  73.149 +
  73.150 +typedef UINT16 histcell;	/* histogram cell; prefer an unsigned type */
  73.151 +
  73.152 +typedef histcell FAR * histptr;	/* for pointers to histogram cells */
  73.153 +
  73.154 +typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
  73.155 +typedef hist1d FAR * hist2d;	/* type for the 2nd-level pointers */
  73.156 +typedef hist2d * hist3d;	/* type for top-level pointer */
  73.157 +
  73.158 +
  73.159 +/* Declarations for Floyd-Steinberg dithering.
  73.160 + *
  73.161 + * Errors are accumulated into the array fserrors[], at a resolution of
  73.162 + * 1/16th of a pixel count.  The error at a given pixel is propagated
  73.163 + * to its not-yet-processed neighbors using the standard F-S fractions,
  73.164 + *		...	(here)	7/16
  73.165 + *		3/16	5/16	1/16
  73.166 + * We work left-to-right on even rows, right-to-left on odd rows.
  73.167 + *
  73.168 + * We can get away with a single array (holding one row's worth of errors)
  73.169 + * by using it to store the current row's errors at pixel columns not yet
  73.170 + * processed, but the next row's errors at columns already processed.  We
  73.171 + * need only a few extra variables to hold the errors immediately around the
  73.172 + * current column.  (If we are lucky, those variables are in registers, but
  73.173 + * even if not, they're probably cheaper to access than array elements are.)
  73.174 + *
  73.175 + * The fserrors[] array has (#columns + 2) entries; the extra entry at
  73.176 + * each end saves us from special-casing the first and last pixels.
  73.177 + * Each entry is three values long, one value for each color component.
  73.178 + *
  73.179 + * Note: on a wide image, we might not have enough room in a PC's near data
  73.180 + * segment to hold the error array; so it is allocated with alloc_large.
  73.181 + */
  73.182 +
  73.183 +#if BITS_IN_JSAMPLE == 8
  73.184 +typedef INT16 FSERROR;		/* 16 bits should be enough */
  73.185 +typedef int LOCFSERROR;		/* use 'int' for calculation temps */
  73.186 +#else
  73.187 +typedef INT32 FSERROR;		/* may need more than 16 bits */
  73.188 +typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
  73.189 +#endif
  73.190 +
  73.191 +typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
  73.192 +
  73.193 +
  73.194 +/* Private subobject */
  73.195 +
  73.196 +typedef struct {
  73.197 +  struct jpeg_color_quantizer pub; /* public fields */
  73.198 +
  73.199 +  /* Space for the eventually created colormap is stashed here */
  73.200 +  JSAMPARRAY sv_colormap;	/* colormap allocated at init time */
  73.201 +  int desired;			/* desired # of colors = size of colormap */
  73.202 +
  73.203 +  /* Variables for accumulating image statistics */
  73.204 +  hist3d histogram;		/* pointer to the histogram */
  73.205 +
  73.206 +  boolean needs_zeroed;		/* TRUE if next pass must zero histogram */
  73.207 +
  73.208 +  /* Variables for Floyd-Steinberg dithering */
  73.209 +  FSERRPTR fserrors;		/* accumulated errors */
  73.210 +  boolean on_odd_row;		/* flag to remember which row we are on */
  73.211 +  int * error_limiter;		/* table for clamping the applied error */
  73.212 +} my_cquantizer;
  73.213 +
  73.214 +typedef my_cquantizer * my_cquantize_ptr;
  73.215 +
  73.216 +
  73.217 +/*
  73.218 + * Prescan some rows of pixels.
  73.219 + * In this module the prescan simply updates the histogram, which has been
  73.220 + * initialized to zeroes by start_pass.
  73.221 + * An output_buf parameter is required by the method signature, but no data
  73.222 + * is actually output (in fact the buffer controller is probably passing a
  73.223 + * NULL pointer).
  73.224 + */
  73.225 +
  73.226 +METHODDEF(void)
  73.227 +prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  73.228 +		  JSAMPARRAY output_buf, int num_rows)
  73.229 +{
  73.230 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.231 +  register JSAMPROW ptr;
  73.232 +  register histptr histp;
  73.233 +  register hist3d histogram = cquantize->histogram;
  73.234 +  int row;
  73.235 +  JDIMENSION col;
  73.236 +  JDIMENSION width = cinfo->output_width;
  73.237 +
  73.238 +  for (row = 0; row < num_rows; row++) {
  73.239 +    ptr = input_buf[row];
  73.240 +    for (col = width; col > 0; col--) {
  73.241 +      /* get pixel value and index into the histogram */
  73.242 +      histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
  73.243 +			 [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
  73.244 +			 [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
  73.245 +      /* increment, check for overflow and undo increment if so. */
  73.246 +      if (++(*histp) <= 0)
  73.247 +	(*histp)--;
  73.248 +      ptr += 3;
  73.249 +    }
  73.250 +  }
  73.251 +}
  73.252 +
  73.253 +
  73.254 +/*
  73.255 + * Next we have the really interesting routines: selection of a colormap
  73.256 + * given the completed histogram.
  73.257 + * These routines work with a list of "boxes", each representing a rectangular
  73.258 + * subset of the input color space (to histogram precision).
  73.259 + */
  73.260 +
  73.261 +typedef struct {
  73.262 +  /* The bounds of the box (inclusive); expressed as histogram indexes */
  73.263 +  int c0min, c0max;
  73.264 +  int c1min, c1max;
  73.265 +  int c2min, c2max;
  73.266 +  /* The volume (actually 2-norm) of the box */
  73.267 +  INT32 volume;
  73.268 +  /* The number of nonzero histogram cells within this box */
  73.269 +  long colorcount;
  73.270 +} box;
  73.271 +
  73.272 +typedef box * boxptr;
  73.273 +
  73.274 +
  73.275 +LOCAL(boxptr)
  73.276 +find_biggest_color_pop (boxptr boxlist, int numboxes)
  73.277 +/* Find the splittable box with the largest color population */
  73.278 +/* Returns NULL if no splittable boxes remain */
  73.279 +{
  73.280 +  register boxptr boxp;
  73.281 +  register int i;
  73.282 +  register long maxc = 0;
  73.283 +  boxptr which = NULL;
  73.284 +  
  73.285 +  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  73.286 +    if (boxp->colorcount > maxc && boxp->volume > 0) {
  73.287 +      which = boxp;
  73.288 +      maxc = boxp->colorcount;
  73.289 +    }
  73.290 +  }
  73.291 +  return which;
  73.292 +}
  73.293 +
  73.294 +
  73.295 +LOCAL(boxptr)
  73.296 +find_biggest_volume (boxptr boxlist, int numboxes)
  73.297 +/* Find the splittable box with the largest (scaled) volume */
  73.298 +/* Returns NULL if no splittable boxes remain */
  73.299 +{
  73.300 +  register boxptr boxp;
  73.301 +  register int i;
  73.302 +  register INT32 maxv = 0;
  73.303 +  boxptr which = NULL;
  73.304 +  
  73.305 +  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  73.306 +    if (boxp->volume > maxv) {
  73.307 +      which = boxp;
  73.308 +      maxv = boxp->volume;
  73.309 +    }
  73.310 +  }
  73.311 +  return which;
  73.312 +}
  73.313 +
  73.314 +
  73.315 +LOCAL(void)
  73.316 +update_box (j_decompress_ptr cinfo, boxptr boxp)
  73.317 +/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
  73.318 +/* and recompute its volume and population */
  73.319 +{
  73.320 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.321 +  hist3d histogram = cquantize->histogram;
  73.322 +  histptr histp;
  73.323 +  int c0,c1,c2;
  73.324 +  int c0min,c0max,c1min,c1max,c2min,c2max;
  73.325 +  INT32 dist0,dist1,dist2;
  73.326 +  long ccount;
  73.327 +  
  73.328 +  c0min = boxp->c0min;  c0max = boxp->c0max;
  73.329 +  c1min = boxp->c1min;  c1max = boxp->c1max;
  73.330 +  c2min = boxp->c2min;  c2max = boxp->c2max;
  73.331 +  
  73.332 +  if (c0max > c0min)
  73.333 +    for (c0 = c0min; c0 <= c0max; c0++)
  73.334 +      for (c1 = c1min; c1 <= c1max; c1++) {
  73.335 +	histp = & histogram[c0][c1][c2min];
  73.336 +	for (c2 = c2min; c2 <= c2max; c2++)
  73.337 +	  if (*histp++ != 0) {
  73.338 +	    boxp->c0min = c0min = c0;
  73.339 +	    goto have_c0min;
  73.340 +	  }
  73.341 +      }
  73.342 + have_c0min:
  73.343 +  if (c0max > c0min)
  73.344 +    for (c0 = c0max; c0 >= c0min; c0--)
  73.345 +      for (c1 = c1min; c1 <= c1max; c1++) {
  73.346 +	histp = & histogram[c0][c1][c2min];
  73.347 +	for (c2 = c2min; c2 <= c2max; c2++)
  73.348 +	  if (*histp++ != 0) {
  73.349 +	    boxp->c0max = c0max = c0;
  73.350 +	    goto have_c0max;
  73.351 +	  }
  73.352 +      }
  73.353 + have_c0max:
  73.354 +  if (c1max > c1min)
  73.355 +    for (c1 = c1min; c1 <= c1max; c1++)
  73.356 +      for (c0 = c0min; c0 <= c0max; c0++) {
  73.357 +	histp = & histogram[c0][c1][c2min];
  73.358 +	for (c2 = c2min; c2 <= c2max; c2++)
  73.359 +	  if (*histp++ != 0) {
  73.360 +	    boxp->c1min = c1min = c1;
  73.361 +	    goto have_c1min;
  73.362 +	  }
  73.363 +      }
  73.364 + have_c1min:
  73.365 +  if (c1max > c1min)
  73.366 +    for (c1 = c1max; c1 >= c1min; c1--)
  73.367 +      for (c0 = c0min; c0 <= c0max; c0++) {
  73.368 +	histp = & histogram[c0][c1][c2min];
  73.369 +	for (c2 = c2min; c2 <= c2max; c2++)
  73.370 +	  if (*histp++ != 0) {
  73.371 +	    boxp->c1max = c1max = c1;
  73.372 +	    goto have_c1max;
  73.373 +	  }
  73.374 +      }
  73.375 + have_c1max:
  73.376 +  if (c2max > c2min)
  73.377 +    for (c2 = c2min; c2 <= c2max; c2++)
  73.378 +      for (c0 = c0min; c0 <= c0max; c0++) {
  73.379 +	histp = & histogram[c0][c1min][c2];
  73.380 +	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  73.381 +	  if (*histp != 0) {
  73.382 +	    boxp->c2min = c2min = c2;
  73.383 +	    goto have_c2min;
  73.384 +	  }
  73.385 +      }
  73.386 + have_c2min:
  73.387 +  if (c2max > c2min)
  73.388 +    for (c2 = c2max; c2 >= c2min; c2--)
  73.389 +      for (c0 = c0min; c0 <= c0max; c0++) {
  73.390 +	histp = & histogram[c0][c1min][c2];
  73.391 +	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  73.392 +	  if (*histp != 0) {
  73.393 +	    boxp->c2max = c2max = c2;
  73.394 +	    goto have_c2max;
  73.395 +	  }
  73.396 +      }
  73.397 + have_c2max:
  73.398 +
  73.399 +  /* Update box volume.
  73.400 +   * We use 2-norm rather than real volume here; this biases the method
  73.401 +   * against making long narrow boxes, and it has the side benefit that
  73.402 +   * a box is splittable iff norm > 0.
  73.403 +   * Since the differences are expressed in histogram-cell units,
  73.404 +   * we have to shift back to JSAMPLE units to get consistent distances;
  73.405 +   * after which, we scale according to the selected distance scale factors.
  73.406 +   */
  73.407 +  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  73.408 +  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  73.409 +  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  73.410 +  boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
  73.411 +  
  73.412 +  /* Now scan remaining volume of box and compute population */
  73.413 +  ccount = 0;
  73.414 +  for (c0 = c0min; c0 <= c0max; c0++)
  73.415 +    for (c1 = c1min; c1 <= c1max; c1++) {
  73.416 +      histp = & histogram[c0][c1][c2min];
  73.417 +      for (c2 = c2min; c2 <= c2max; c2++, histp++)
  73.418 +	if (*histp != 0) {
  73.419 +	  ccount++;
  73.420 +	}
  73.421 +    }
  73.422 +  boxp->colorcount = ccount;
  73.423 +}
  73.424 +
  73.425 +
  73.426 +LOCAL(int)
  73.427 +median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
  73.428 +	    int desired_colors)
  73.429 +/* Repeatedly select and split the largest box until we have enough boxes */
  73.430 +{
  73.431 +  int n,lb;
  73.432 +  int c0,c1,c2,cmax;
  73.433 +  register boxptr b1,b2;
  73.434 +
  73.435 +  while (numboxes < desired_colors) {
  73.436 +    /* Select box to split.
  73.437 +     * Current algorithm: by population for first half, then by volume.
  73.438 +     */
  73.439 +    if (numboxes*2 <= desired_colors) {
  73.440 +      b1 = find_biggest_color_pop(boxlist, numboxes);
  73.441 +    } else {
  73.442 +      b1 = find_biggest_volume(boxlist, numboxes);
  73.443 +    }
  73.444 +    if (b1 == NULL)		/* no splittable boxes left! */
  73.445 +      break;
  73.446 +    b2 = &boxlist[numboxes];	/* where new box will go */
  73.447 +    /* Copy the color bounds to the new box. */
  73.448 +    b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  73.449 +    b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  73.450 +    /* Choose which axis to split the box on.
  73.451 +     * Current algorithm: longest scaled axis.
  73.452 +     * See notes in update_box about scaling distances.
  73.453 +     */
  73.454 +    c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
  73.455 +    c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
  73.456 +    c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
  73.457 +    /* We want to break any ties in favor of green, then red, blue last.
  73.458 +     * This code does the right thing for R,G,B or B,G,R color orders only.
  73.459 +     */
  73.460 +#if RGB_RED == 0
  73.461 +    cmax = c1; n = 1;
  73.462 +    if (c0 > cmax) { cmax = c0; n = 0; }
  73.463 +    if (c2 > cmax) { n = 2; }
  73.464 +#else
  73.465 +    cmax = c1; n = 1;
  73.466 +    if (c2 > cmax) { cmax = c2; n = 2; }
  73.467 +    if (c0 > cmax) { n = 0; }
  73.468 +#endif
  73.469 +    /* Choose split point along selected axis, and update box bounds.
  73.470 +     * Current algorithm: split at halfway point.
  73.471 +     * (Since the box has been shrunk to minimum volume,
  73.472 +     * any split will produce two nonempty subboxes.)
  73.473 +     * Note that lb value is max for lower box, so must be < old max.
  73.474 +     */
  73.475 +    switch (n) {
  73.476 +    case 0:
  73.477 +      lb = (b1->c0max + b1->c0min) / 2;
  73.478 +      b1->c0max = lb;
  73.479 +      b2->c0min = lb+1;
  73.480 +      break;
  73.481 +    case 1:
  73.482 +      lb = (b1->c1max + b1->c1min) / 2;
  73.483 +      b1->c1max = lb;
  73.484 +      b2->c1min = lb+1;
  73.485 +      break;
  73.486 +    case 2:
  73.487 +      lb = (b1->c2max + b1->c2min) / 2;
  73.488 +      b1->c2max = lb;
  73.489 +      b2->c2min = lb+1;
  73.490 +      break;
  73.491 +    }
  73.492 +    /* Update stats for boxes */
  73.493 +    update_box(cinfo, b1);
  73.494 +    update_box(cinfo, b2);
  73.495 +    numboxes++;
  73.496 +  }
  73.497 +  return numboxes;
  73.498 +}
  73.499 +
  73.500 +
  73.501 +LOCAL(void)
  73.502 +compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
  73.503 +/* Compute representative color for a box, put it in colormap[icolor] */
  73.504 +{
  73.505 +  /* Current algorithm: mean weighted by pixels (not colors) */
  73.506 +  /* Note it is important to get the rounding correct! */
  73.507 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.508 +  hist3d histogram = cquantize->histogram;
  73.509 +  histptr histp;
  73.510 +  int c0,c1,c2;
  73.511 +  int c0min,c0max,c1min,c1max,c2min,c2max;
  73.512 +  long count;
  73.513 +  long total = 0;
  73.514 +  long c0total = 0;
  73.515 +  long c1total = 0;
  73.516 +  long c2total = 0;
  73.517 +  
  73.518 +  c0min = boxp->c0min;  c0max = boxp->c0max;
  73.519 +  c1min = boxp->c1min;  c1max = boxp->c1max;
  73.520 +  c2min = boxp->c2min;  c2max = boxp->c2max;
  73.521 +  
  73.522 +  for (c0 = c0min; c0 <= c0max; c0++)
  73.523 +    for (c1 = c1min; c1 <= c1max; c1++) {
  73.524 +      histp = & histogram[c0][c1][c2min];
  73.525 +      for (c2 = c2min; c2 <= c2max; c2++) {
  73.526 +	if ((count = *histp++) != 0) {
  73.527 +	  total += count;
  73.528 +	  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
  73.529 +	  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
  73.530 +	  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
  73.531 +	}
  73.532 +      }
  73.533 +    }
  73.534 +  
  73.535 +  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  73.536 +  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  73.537 +  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  73.538 +}
  73.539 +
  73.540 +
  73.541 +LOCAL(void)
  73.542 +select_colors (j_decompress_ptr cinfo, int desired_colors)
  73.543 +/* Master routine for color selection */
  73.544 +{
  73.545 +  boxptr boxlist;
  73.546 +  int numboxes;
  73.547 +  int i;
  73.548 +
  73.549 +  /* Allocate workspace for box list */
  73.550 +  boxlist = (boxptr) (*cinfo->mem->alloc_small)
  73.551 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
  73.552 +  /* Initialize one box containing whole space */
  73.553 +  numboxes = 1;
  73.554 +  boxlist[0].c0min = 0;
  73.555 +  boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
  73.556 +  boxlist[0].c1min = 0;
  73.557 +  boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
  73.558 +  boxlist[0].c2min = 0;
  73.559 +  boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
  73.560 +  /* Shrink it to actually-used volume and set its statistics */
  73.561 +  update_box(cinfo, & boxlist[0]);
  73.562 +  /* Perform median-cut to produce final box list */
  73.563 +  numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
  73.564 +  /* Compute the representative color for each box, fill colormap */
  73.565 +  for (i = 0; i < numboxes; i++)
  73.566 +    compute_color(cinfo, & boxlist[i], i);
  73.567 +  cinfo->actual_number_of_colors = numboxes;
  73.568 +  TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
  73.569 +}
  73.570 +
  73.571 +
  73.572 +/*
  73.573 + * These routines are concerned with the time-critical task of mapping input
  73.574 + * colors to the nearest color in the selected colormap.
  73.575 + *
  73.576 + * We re-use the histogram space as an "inverse color map", essentially a
  73.577 + * cache for the results of nearest-color searches.  All colors within a
  73.578 + * histogram cell will be mapped to the same colormap entry, namely the one
  73.579 + * closest to the cell's center.  This may not be quite the closest entry to
  73.580 + * the actual input color, but it's almost as good.  A zero in the cache
  73.581 + * indicates we haven't found the nearest color for that cell yet; the array
  73.582 + * is cleared to zeroes before starting the mapping pass.  When we find the
  73.583 + * nearest color for a cell, its colormap index plus one is recorded in the
  73.584 + * cache for future use.  The pass2 scanning routines call fill_inverse_cmap
  73.585 + * when they need to use an unfilled entry in the cache.
  73.586 + *
  73.587 + * Our method of efficiently finding nearest colors is based on the "locally
  73.588 + * sorted search" idea described by Heckbert and on the incremental distance
  73.589 + * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
  73.590 + * Gems II (James Arvo, ed.  Academic Press, 1991).  Thomas points out that
  73.591 + * the distances from a given colormap entry to each cell of the histogram can
  73.592 + * be computed quickly using an incremental method: the differences between
  73.593 + * distances to adjacent cells themselves differ by a constant.  This allows a
  73.594 + * fairly fast implementation of the "brute force" approach of computing the
  73.595 + * distance from every colormap entry to every histogram cell.  Unfortunately,
  73.596 + * it needs a work array to hold the best-distance-so-far for each histogram
  73.597 + * cell (because the inner loop has to be over cells, not colormap entries).
  73.598 + * The work array elements have to be INT32s, so the work array would need
  73.599 + * 256Kb at our recommended precision.  This is not feasible in DOS machines.
  73.600 + *
  73.601 + * To get around these problems, we apply Thomas' method to compute the
  73.602 + * nearest colors for only the cells within a small subbox of the histogram.
  73.603 + * The work array need be only as big as the subbox, so the memory usage
  73.604 + * problem is solved.  Furthermore, we need not fill subboxes that are never
  73.605 + * referenced in pass2; many images use only part of the color gamut, so a
  73.606 + * fair amount of work is saved.  An additional advantage of this
  73.607 + * approach is that we can apply Heckbert's locality criterion to quickly
  73.608 + * eliminate colormap entries that are far away from the subbox; typically
  73.609 + * three-fourths of the colormap entries are rejected by Heckbert's criterion,
  73.610 + * and we need not compute their distances to individual cells in the subbox.
  73.611 + * The speed of this approach is heavily influenced by the subbox size: too
  73.612 + * small means too much overhead, too big loses because Heckbert's criterion
  73.613 + * can't eliminate as many colormap entries.  Empirically the best subbox
  73.614 + * size seems to be about 1/512th of the histogram (1/8th in each direction).
  73.615 + *
  73.616 + * Thomas' article also describes a refined method which is asymptotically
  73.617 + * faster than the brute-force method, but it is also far more complex and
  73.618 + * cannot efficiently be applied to small subboxes.  It is therefore not
  73.619 + * useful for programs intended to be portable to DOS machines.  On machines
  73.620 + * with plenty of memory, filling the whole histogram in one shot with Thomas'
  73.621 + * refined method might be faster than the present code --- but then again,
  73.622 + * it might not be any faster, and it's certainly more complicated.
  73.623 + */
  73.624 +
  73.625 +
  73.626 +/* log2(histogram cells in update box) for each axis; this can be adjusted */
  73.627 +#define BOX_C0_LOG  (HIST_C0_BITS-3)
  73.628 +#define BOX_C1_LOG  (HIST_C1_BITS-3)
  73.629 +#define BOX_C2_LOG  (HIST_C2_BITS-3)
  73.630 +
  73.631 +#define BOX_C0_ELEMS  (1<<BOX_C0_LOG) /* # of hist cells in update box */
  73.632 +#define BOX_C1_ELEMS  (1<<BOX_C1_LOG)
  73.633 +#define BOX_C2_ELEMS  (1<<BOX_C2_LOG)
  73.634 +
  73.635 +#define BOX_C0_SHIFT  (C0_SHIFT + BOX_C0_LOG)
  73.636 +#define BOX_C1_SHIFT  (C1_SHIFT + BOX_C1_LOG)
  73.637 +#define BOX_C2_SHIFT  (C2_SHIFT + BOX_C2_LOG)
  73.638 +
  73.639 +
  73.640 +/*
  73.641 + * The next three routines implement inverse colormap filling.  They could
  73.642 + * all be folded into one big routine, but splitting them up this way saves
  73.643 + * some stack space (the mindist[] and bestdist[] arrays need not coexist)
  73.644 + * and may allow some compilers to produce better code by registerizing more
  73.645 + * inner-loop variables.
  73.646 + */
  73.647 +
  73.648 +LOCAL(int)
  73.649 +find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  73.650 +		    JSAMPLE colorlist[])
  73.651 +/* Locate the colormap entries close enough to an update box to be candidates
  73.652 + * for the nearest entry to some cell(s) in the update box.  The update box
  73.653 + * is specified by the center coordinates of its first cell.  The number of
  73.654 + * candidate colormap entries is returned, and their colormap indexes are
  73.655 + * placed in colorlist[].
  73.656 + * This routine uses Heckbert's "locally sorted search" criterion to select
  73.657 + * the colors that need further consideration.
  73.658 + */
  73.659 +{
  73.660 +  int numcolors = cinfo->actual_number_of_colors;
  73.661 +  int maxc0, maxc1, maxc2;
  73.662 +  int centerc0, centerc1, centerc2;
  73.663 +  int i, x, ncolors;
  73.664 +  INT32 minmaxdist, min_dist, max_dist, tdist;
  73.665 +  INT32 mindist[MAXNUMCOLORS];	/* min distance to colormap entry i */
  73.666 +
  73.667 +  /* Compute true coordinates of update box's upper corner and center.
  73.668 +   * Actually we compute the coordinates of the center of the upper-corner
  73.669 +   * histogram cell, which are the upper bounds of the volume we care about.
  73.670 +   * Note that since ">>" rounds down, the "center" values may be closer to
  73.671 +   * min than to max; hence comparisons to them must be "<=", not "<".
  73.672 +   */
  73.673 +  maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
  73.674 +  centerc0 = (minc0 + maxc0) >> 1;
  73.675 +  maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
  73.676 +  centerc1 = (minc1 + maxc1) >> 1;
  73.677 +  maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
  73.678 +  centerc2 = (minc2 + maxc2) >> 1;
  73.679 +
  73.680 +  /* For each color in colormap, find:
  73.681 +   *  1. its minimum squared-distance to any point in the update box
  73.682 +   *     (zero if color is within update box);
  73.683 +   *  2. its maximum squared-distance to any point in the update box.
  73.684 +   * Both of these can be found by considering only the corners of the box.
  73.685 +   * We save the minimum distance for each color in mindist[];
  73.686 +   * only the smallest maximum distance is of interest.
  73.687 +   */
  73.688 +  minmaxdist = 0x7FFFFFFFL;
  73.689 +
  73.690 +  for (i = 0; i < numcolors; i++) {
  73.691 +    /* We compute the squared-c0-distance term, then add in the other two. */
  73.692 +    x = GETJSAMPLE(cinfo->colormap[0][i]);
  73.693 +    if (x < minc0) {
  73.694 +      tdist = (x - minc0) * C0_SCALE;
  73.695 +      min_dist = tdist*tdist;
  73.696 +      tdist = (x - maxc0) * C0_SCALE;
  73.697 +      max_dist = tdist*tdist;
  73.698 +    } else if (x > maxc0) {
  73.699 +      tdist = (x - maxc0) * C0_SCALE;
  73.700 +      min_dist = tdist*tdist;
  73.701 +      tdist = (x - minc0) * C0_SCALE;
  73.702 +      max_dist = tdist*tdist;
  73.703 +    } else {
  73.704 +      /* within cell range so no contribution to min_dist */
  73.705 +      min_dist = 0;
  73.706 +      if (x <= centerc0) {
  73.707 +	tdist = (x - maxc0) * C0_SCALE;
  73.708 +	max_dist = tdist*tdist;
  73.709 +      } else {
  73.710 +	tdist = (x - minc0) * C0_SCALE;
  73.711 +	max_dist = tdist*tdist;
  73.712 +      }
  73.713 +    }
  73.714 +
  73.715 +    x = GETJSAMPLE(cinfo->colormap[1][i]);
  73.716 +    if (x < minc1) {
  73.717 +      tdist = (x - minc1) * C1_SCALE;
  73.718 +      min_dist += tdist*tdist;
  73.719 +      tdist = (x - maxc1) * C1_SCALE;
  73.720 +      max_dist += tdist*tdist;
  73.721 +    } else if (x > maxc1) {
  73.722 +      tdist = (x - maxc1) * C1_SCALE;
  73.723 +      min_dist += tdist*tdist;
  73.724 +      tdist = (x - minc1) * C1_SCALE;
  73.725 +      max_dist += tdist*tdist;
  73.726 +    } else {
  73.727 +      /* within cell range so no contribution to min_dist */
  73.728 +      if (x <= centerc1) {
  73.729 +	tdist = (x - maxc1) * C1_SCALE;
  73.730 +	max_dist += tdist*tdist;
  73.731 +      } else {
  73.732 +	tdist = (x - minc1) * C1_SCALE;
  73.733 +	max_dist += tdist*tdist;
  73.734 +      }
  73.735 +    }
  73.736 +
  73.737 +    x = GETJSAMPLE(cinfo->colormap[2][i]);
  73.738 +    if (x < minc2) {
  73.739 +      tdist = (x - minc2) * C2_SCALE;
  73.740 +      min_dist += tdist*tdist;
  73.741 +      tdist = (x - maxc2) * C2_SCALE;
  73.742 +      max_dist += tdist*tdist;
  73.743 +    } else if (x > maxc2) {
  73.744 +      tdist = (x - maxc2) * C2_SCALE;
  73.745 +      min_dist += tdist*tdist;
  73.746 +      tdist = (x - minc2) * C2_SCALE;
  73.747 +      max_dist += tdist*tdist;
  73.748 +    } else {
  73.749 +      /* within cell range so no contribution to min_dist */
  73.750 +      if (x <= centerc2) {
  73.751 +	tdist = (x - maxc2) * C2_SCALE;
  73.752 +	max_dist += tdist*tdist;
  73.753 +      } else {
  73.754 +	tdist = (x - minc2) * C2_SCALE;
  73.755 +	max_dist += tdist*tdist;
  73.756 +      }
  73.757 +    }
  73.758 +
  73.759 +    mindist[i] = min_dist;	/* save away the results */
  73.760 +    if (max_dist < minmaxdist)
  73.761 +      minmaxdist = max_dist;
  73.762 +  }
  73.763 +
  73.764 +  /* Now we know that no cell in the update box is more than minmaxdist
  73.765 +   * away from some colormap entry.  Therefore, only colors that are
  73.766 +   * within minmaxdist of some part of the box need be considered.
  73.767 +   */
  73.768 +  ncolors = 0;
  73.769 +  for (i = 0; i < numcolors; i++) {
  73.770 +    if (mindist[i] <= minmaxdist)
  73.771 +      colorlist[ncolors++] = (JSAMPLE) i;
  73.772 +  }
  73.773 +  return ncolors;
  73.774 +}
  73.775 +
  73.776 +
  73.777 +LOCAL(void)
  73.778 +find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  73.779 +		  int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  73.780 +/* Find the closest colormap entry for each cell in the update box,
  73.781 + * given the list of candidate colors prepared by find_nearby_colors.
  73.782 + * Return the indexes of the closest entries in the bestcolor[] array.
  73.783 + * This routine uses Thomas' incremental distance calculation method to
  73.784 + * find the distance from a colormap entry to successive cells in the box.
  73.785 + */
  73.786 +{
  73.787 +  int ic0, ic1, ic2;
  73.788 +  int i, icolor;
  73.789 +  register INT32 * bptr;	/* pointer into bestdist[] array */
  73.790 +  JSAMPLE * cptr;		/* pointer into bestcolor[] array */
  73.791 +  INT32 dist0, dist1;		/* initial distance values */
  73.792 +  register INT32 dist2;		/* current distance in inner loop */
  73.793 +  INT32 xx0, xx1;		/* distance increments */
  73.794 +  register INT32 xx2;
  73.795 +  INT32 inc0, inc1, inc2;	/* initial values for increments */
  73.796 +  /* This array holds the distance to the nearest-so-far color for each cell */
  73.797 +  INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  73.798 +
  73.799 +  /* Initialize best-distance for each cell of the update box */
  73.800 +  bptr = bestdist;
  73.801 +  for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
  73.802 +    *bptr++ = 0x7FFFFFFFL;
  73.803 +  
  73.804 +  /* For each color selected by find_nearby_colors,
  73.805 +   * compute its distance to the center of each cell in the box.
  73.806 +   * If that's less than best-so-far, update best distance and color number.
  73.807 +   */
  73.808 +  
  73.809 +  /* Nominal steps between cell centers ("x" in Thomas article) */
  73.810 +#define STEP_C0  ((1 << C0_SHIFT) * C0_SCALE)
  73.811 +#define STEP_C1  ((1 << C1_SHIFT) * C1_SCALE)
  73.812 +#define STEP_C2  ((1 << C2_SHIFT) * C2_SCALE)
  73.813 +  
  73.814 +  for (i = 0; i < numcolors; i++) {
  73.815 +    icolor = GETJSAMPLE(colorlist[i]);
  73.816 +    /* Compute (square of) distance from minc0/c1/c2 to this color */
  73.817 +    inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
  73.818 +    dist0 = inc0*inc0;
  73.819 +    inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
  73.820 +    dist0 += inc1*inc1;
  73.821 +    inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
  73.822 +    dist0 += inc2*inc2;
  73.823 +    /* Form the initial difference increments */
  73.824 +    inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
  73.825 +    inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
  73.826 +    inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
  73.827 +    /* Now loop over all cells in box, updating distance per Thomas method */
  73.828 +    bptr = bestdist;
  73.829 +    cptr = bestcolor;
  73.830 +    xx0 = inc0;
  73.831 +    for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
  73.832 +      dist1 = dist0;
  73.833 +      xx1 = inc1;
  73.834 +      for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
  73.835 +	dist2 = dist1;
  73.836 +	xx2 = inc2;
  73.837 +	for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
  73.838 +	  if (dist2 < *bptr) {
  73.839 +	    *bptr = dist2;
  73.840 +	    *cptr = (JSAMPLE) icolor;
  73.841 +	  }
  73.842 +	  dist2 += xx2;
  73.843 +	  xx2 += 2 * STEP_C2 * STEP_C2;
  73.844 +	  bptr++;
  73.845 +	  cptr++;
  73.846 +	}
  73.847 +	dist1 += xx1;
  73.848 +	xx1 += 2 * STEP_C1 * STEP_C1;
  73.849 +      }
  73.850 +      dist0 += xx0;
  73.851 +      xx0 += 2 * STEP_C0 * STEP_C0;
  73.852 +    }
  73.853 +  }
  73.854 +}
  73.855 +
  73.856 +
  73.857 +LOCAL(void)
  73.858 +fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
  73.859 +/* Fill the inverse-colormap entries in the update box that contains */
  73.860 +/* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
  73.861 +/* we can fill as many others as we wish.) */
  73.862 +{
  73.863 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.864 +  hist3d histogram = cquantize->histogram;
  73.865 +  int minc0, minc1, minc2;	/* lower left corner of update box */
  73.866 +  int ic0, ic1, ic2;
  73.867 +  register JSAMPLE * cptr;	/* pointer into bestcolor[] array */
  73.868 +  register histptr cachep;	/* pointer into main cache array */
  73.869 +  /* This array lists the candidate colormap indexes. */
  73.870 +  JSAMPLE colorlist[MAXNUMCOLORS];
  73.871 +  int numcolors;		/* number of candidate colors */
  73.872 +  /* This array holds the actually closest colormap index for each cell. */
  73.873 +  JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  73.874 +
  73.875 +  /* Convert cell coordinates to update box ID */
  73.876 +  c0 >>= BOX_C0_LOG;
  73.877 +  c1 >>= BOX_C1_LOG;
  73.878 +  c2 >>= BOX_C2_LOG;
  73.879 +
  73.880 +  /* Compute true coordinates of update box's origin corner.
  73.881 +   * Actually we compute the coordinates of the center of the corner
  73.882 +   * histogram cell, which are the lower bounds of the volume we care about.
  73.883 +   */
  73.884 +  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  73.885 +  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  73.886 +  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
  73.887 +  
  73.888 +  /* Determine which colormap entries are close enough to be candidates
  73.889 +   * for the nearest entry to some cell in the update box.
  73.890 +   */
  73.891 +  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  73.892 +
  73.893 +  /* Determine the actually nearest colors. */
  73.894 +  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  73.895 +		   bestcolor);
  73.896 +
  73.897 +  /* Save the best color numbers (plus 1) in the main cache array */
  73.898 +  c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
  73.899 +  c1 <<= BOX_C1_LOG;
  73.900 +  c2 <<= BOX_C2_LOG;
  73.901 +  cptr = bestcolor;
  73.902 +  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
  73.903 +    for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
  73.904 +      cachep = & histogram[c0+ic0][c1+ic1][c2];
  73.905 +      for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
  73.906 +	*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  73.907 +      }
  73.908 +    }
  73.909 +  }
  73.910 +}
  73.911 +
  73.912 +
  73.913 +/*
  73.914 + * Map some rows of pixels to the output colormapped representation.
  73.915 + */
  73.916 +
  73.917 +METHODDEF(void)
  73.918 +pass2_no_dither (j_decompress_ptr cinfo,
  73.919 +		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  73.920 +/* This version performs no dithering */
  73.921 +{
  73.922 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.923 +  hist3d histogram = cquantize->histogram;
  73.924 +  register JSAMPROW inptr, outptr;
  73.925 +  register histptr cachep;
  73.926 +  register int c0, c1, c2;
  73.927 +  int row;
  73.928 +  JDIMENSION col;
  73.929 +  JDIMENSION width = cinfo->output_width;
  73.930 +
  73.931 +  for (row = 0; row < num_rows; row++) {
  73.932 +    inptr = input_buf[row];
  73.933 +    outptr = output_buf[row];
  73.934 +    for (col = width; col > 0; col--) {
  73.935 +      /* get pixel value and index into the cache */
  73.936 +      c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
  73.937 +      c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
  73.938 +      c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
  73.939 +      cachep = & histogram[c0][c1][c2];
  73.940 +      /* If we have not seen this color before, find nearest colormap entry */
  73.941 +      /* and update the cache */
  73.942 +      if (*cachep == 0)
  73.943 +	fill_inverse_cmap(cinfo, c0,c1,c2);
  73.944 +      /* Now emit the colormap index for this cell */
  73.945 +      *outptr++ = (JSAMPLE) (*cachep - 1);
  73.946 +    }
  73.947 +  }
  73.948 +}
  73.949 +
  73.950 +
  73.951 +METHODDEF(void)
  73.952 +pass2_fs_dither (j_decompress_ptr cinfo,
  73.953 +		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  73.954 +/* This version performs Floyd-Steinberg dithering */
  73.955 +{
  73.956 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  73.957 +  hist3d histogram = cquantize->histogram;
  73.958 +  register LOCFSERROR cur0, cur1, cur2;	/* current error or pixel value */
  73.959 +  LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  73.960 +  LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  73.961 +  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  73.962 +  JSAMPROW inptr;		/* => current input pixel */
  73.963 +  JSAMPROW outptr;		/* => current output pixel */
  73.964 +  histptr cachep;
  73.965 +  int dir;			/* +1 or -1 depending on direction */
  73.966 +  int dir3;			/* 3*dir, for advancing inptr & errorptr */
  73.967 +  int row;
  73.968 +  JDIMENSION col;
  73.969 +  JDIMENSION width = cinfo->output_width;
  73.970 +  JSAMPLE *range_limit = cinfo->sample_range_limit;
  73.971 +  int *error_limit = cquantize->error_limiter;
  73.972 +  JSAMPROW colormap0 = cinfo->colormap[0];
  73.973 +  JSAMPROW colormap1 = cinfo->colormap[1];
  73.974 +  JSAMPROW colormap2 = cinfo->colormap[2];
  73.975 +  SHIFT_TEMPS
  73.976 +
  73.977 +  for (row = 0; row < num_rows; row++) {
  73.978 +    inptr = input_buf[row];
  73.979 +    outptr = output_buf[row];
  73.980 +    if (cquantize->on_odd_row) {
  73.981 +      /* work right to left in this row */
  73.982 +      inptr += (width-1) * 3;	/* so point to rightmost pixel */
  73.983 +      outptr += width-1;
  73.984 +      dir = -1;
  73.985 +      dir3 = -3;
  73.986 +      errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
  73.987 +      cquantize->on_odd_row = FALSE; /* flip for next time */
  73.988 +    } else {
  73.989 +      /* work left to right in this row */
  73.990 +      dir = 1;
  73.991 +      dir3 = 3;
  73.992 +      errorptr = cquantize->fserrors; /* => entry before first real column */
  73.993 +      cquantize->on_odd_row = TRUE; /* flip for next time */
  73.994 +    }
  73.995 +    /* Preset error values: no error propagated to first pixel from left */
  73.996 +    cur0 = cur1 = cur2 = 0;
  73.997 +    /* and no error propagated to row below yet */
  73.998 +    belowerr0 = belowerr1 = belowerr2 = 0;
  73.999 +    bpreverr0 = bpreverr1 = bpreverr2 = 0;
 73.1000 +
 73.1001 +    for (col = width; col > 0; col--) {
 73.1002 +      /* curN holds the error propagated from the previous pixel on the
 73.1003 +       * current line.  Add the error propagated from the previous line
 73.1004 +       * to form the complete error correction term for this pixel, and
 73.1005 +       * round the error term (which is expressed * 16) to an integer.
 73.1006 +       * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
 73.1007 +       * for either sign of the error value.
 73.1008 +       * Note: errorptr points to *previous* column's array entry.
 73.1009 +       */
 73.1010 +      cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
 73.1011 +      cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
 73.1012 +      cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
 73.1013 +      /* Limit the error using transfer function set by init_error_limit.
 73.1014 +       * See comments with init_error_limit for rationale.
 73.1015 +       */
 73.1016 +      cur0 = error_limit[cur0];
 73.1017 +      cur1 = error_limit[cur1];
 73.1018 +      cur2 = error_limit[cur2];
 73.1019 +      /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
 73.1020 +       * The maximum error is +- MAXJSAMPLE (or less with error limiting);
 73.1021 +       * this sets the required size of the range_limit array.
 73.1022 +       */
 73.1023 +      cur0 += GETJSAMPLE(inptr[0]);
 73.1024 +      cur1 += GETJSAMPLE(inptr[1]);
 73.1025 +      cur2 += GETJSAMPLE(inptr[2]);
 73.1026 +      cur0 = GETJSAMPLE(range_limit[cur0]);
 73.1027 +      cur1 = GETJSAMPLE(range_limit[cur1]);
 73.1028 +      cur2 = GETJSAMPLE(range_limit[cur2]);
 73.1029 +      /* Index into the cache with adjusted pixel value */
 73.1030 +      cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
 73.1031 +      /* If we have not seen this color before, find nearest colormap */
 73.1032 +      /* entry and update the cache */
 73.1033 +      if (*cachep == 0)
 73.1034 +	fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
 73.1035 +      /* Now emit the colormap index for this cell */
 73.1036 +      { register int pixcode = *cachep - 1;
 73.1037 +	*outptr = (JSAMPLE) pixcode;
 73.1038 +	/* Compute representation error for this pixel */
 73.1039 +	cur0 -= GETJSAMPLE(colormap0[pixcode]);
 73.1040 +	cur1 -= GETJSAMPLE(colormap1[pixcode]);
 73.1041 +	cur2 -= GETJSAMPLE(colormap2[pixcode]);
 73.1042 +      }
 73.1043 +      /* Compute error fractions to be propagated to adjacent pixels.
 73.1044 +       * Add these into the running sums, and simultaneously shift the
 73.1045 +       * next-line error sums left by 1 column.
 73.1046 +       */
 73.1047 +      { register LOCFSERROR bnexterr, delta;
 73.1048 +
 73.1049 +	bnexterr = cur0;	/* Process component 0 */
 73.1050 +	delta = cur0 * 2;
 73.1051 +	cur0 += delta;		/* form error * 3 */
 73.1052 +	errorptr[0] = (FSERROR) (bpreverr0 + cur0);
 73.1053 +	cur0 += delta;		/* form error * 5 */
 73.1054 +	bpreverr0 = belowerr0 + cur0;
 73.1055 +	belowerr0 = bnexterr;
 73.1056 +	cur0 += delta;		/* form error * 7 */
 73.1057 +	bnexterr = cur1;	/* Process component 1 */
 73.1058 +	delta = cur1 * 2;
 73.1059 +	cur1 += delta;		/* form error * 3 */
 73.1060 +	errorptr[1] = (FSERROR) (bpreverr1 + cur1);
 73.1061 +	cur1 += delta;		/* form error * 5 */
 73.1062 +	bpreverr1 = belowerr1 + cur1;
 73.1063 +	belowerr1 = bnexterr;
 73.1064 +	cur1 += delta;		/* form error * 7 */
 73.1065 +	bnexterr = cur2;	/* Process component 2 */
 73.1066 +	delta = cur2 * 2;
 73.1067 +	cur2 += delta;		/* form error * 3 */
 73.1068 +	errorptr[2] = (FSERROR) (bpreverr2 + cur2);
 73.1069 +	cur2 += delta;		/* form error * 5 */
 73.1070 +	bpreverr2 = belowerr2 + cur2;
 73.1071 +	belowerr2 = bnexterr;
 73.1072 +	cur2 += delta;		/* form error * 7 */
 73.1073 +      }
 73.1074 +      /* At this point curN contains the 7/16 error value to be propagated
 73.1075 +       * to the next pixel on the current line, and all the errors for the
 73.1076 +       * next line have been shifted over.  We are therefore ready to move on.
 73.1077 +       */
 73.1078 +      inptr += dir3;		/* Advance pixel pointers to next column */
 73.1079 +      outptr += dir;
 73.1080 +      errorptr += dir3;		/* advance errorptr to current column */
 73.1081 +    }
 73.1082 +    /* Post-loop cleanup: we must unload the final error values into the
 73.1083 +     * final fserrors[] entry.  Note we need not unload belowerrN because
 73.1084 +     * it is for the dummy column before or after the actual array.
 73.1085 +     */
 73.1086 +    errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
 73.1087 +    errorptr[1] = (FSERROR) bpreverr1;
 73.1088 +    errorptr[2] = (FSERROR) bpreverr2;
 73.1089 +  }
 73.1090 +}
 73.1091 +
 73.1092 +
 73.1093 +/*
 73.1094 + * Initialize the error-limiting transfer function (lookup table).
 73.1095 + * The raw F-S error computation can potentially compute error values of up to
 73.1096 + * +- MAXJSAMPLE.  But we want the maximum correction applied to a pixel to be
 73.1097 + * much less, otherwise obviously wrong pixels will be created.  (Typical
 73.1098 + * effects include weird fringes at color-area boundaries, isolated bright
 73.1099 + * pixels in a dark area, etc.)  The standard advice for avoiding this problem
 73.1100 + * is to ensure that the "corners" of the color cube are allocated as output
 73.1101 + * colors; then repeated errors in the same direction cannot cause cascading
 73.1102 + * error buildup.  However, that only prevents the error from getting
 73.1103 + * completely out of hand; Aaron Giles reports that error limiting improves
 73.1104 + * the results even with corner colors allocated.
 73.1105 + * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
 73.1106 + * well, but the smoother transfer function used below is even better.  Thanks
 73.1107 + * to Aaron Giles for this idea.
 73.1108 + */
 73.1109 +
 73.1110 +LOCAL(void)
 73.1111 +init_error_limit (j_decompress_ptr cinfo)
 73.1112 +/* Allocate and fill in the error_limiter table */
 73.1113 +{
 73.1114 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 73.1115 +  int * table;
 73.1116 +  int in, out;
 73.1117 +
 73.1118 +  table = (int *) (*cinfo->mem->alloc_small)
 73.1119 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
 73.1120 +  table += MAXJSAMPLE;		/* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
 73.1121 +  cquantize->error_limiter = table;
 73.1122 +
 73.1123 +#define STEPSIZE ((MAXJSAMPLE+1)/16)
 73.1124 +  /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
 73.1125 +  out = 0;
 73.1126 +  for (in = 0; in < STEPSIZE; in++, out++) {
 73.1127 +    table[in] = out; table[-in] = -out;
 73.1128 +  }
 73.1129 +  /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
 73.1130 +  for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
 73.1131 +    table[in] = out; table[-in] = -out;
 73.1132 +  }
 73.1133 +  /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
 73.1134 +  for (; in <= MAXJSAMPLE; in++) {
 73.1135 +    table[in] = out; table[-in] = -out;
 73.1136 +  }
 73.1137 +#undef STEPSIZE
 73.1138 +}
 73.1139 +
 73.1140 +
 73.1141 +/*
 73.1142 + * Finish up at the end of each pass.
 73.1143 + */
 73.1144 +
 73.1145 +METHODDEF(void)
 73.1146 +finish_pass1 (j_decompress_ptr cinfo)
 73.1147 +{
 73.1148 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 73.1149 +
 73.1150 +  /* Select the representative colors and fill in cinfo->colormap */
 73.1151 +  cinfo->colormap = cquantize->sv_colormap;
 73.1152 +  select_colors(cinfo, cquantize->desired);
 73.1153 +  /* Force next pass to zero the color index table */
 73.1154 +  cquantize->needs_zeroed = TRUE;
 73.1155 +}
 73.1156 +
 73.1157 +
 73.1158 +METHODDEF(void)
 73.1159 +finish_pass2 (j_decompress_ptr cinfo)
 73.1160 +{
 73.1161 +  /* no work */
 73.1162 +}
 73.1163 +
 73.1164 +
 73.1165 +/*
 73.1166 + * Initialize for each processing pass.
 73.1167 + */
 73.1168 +
 73.1169 +METHODDEF(void)
 73.1170 +start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
 73.1171 +{
 73.1172 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 73.1173 +  hist3d histogram = cquantize->histogram;
 73.1174 +  int i;
 73.1175 +
 73.1176 +  /* Only F-S dithering or no dithering is supported. */
 73.1177 +  /* If user asks for ordered dither, give him F-S. */
 73.1178 +  if (cinfo->dither_mode != JDITHER_NONE)
 73.1179 +    cinfo->dither_mode = JDITHER_FS;
 73.1180 +
 73.1181 +  if (is_pre_scan) {
 73.1182 +    /* Set up method pointers */
 73.1183 +    cquantize->pub.color_quantize = prescan_quantize;
 73.1184 +    cquantize->pub.finish_pass = finish_pass1;
 73.1185 +    cquantize->needs_zeroed = TRUE; /* Always zero histogram */
 73.1186 +  } else {
 73.1187 +    /* Set up method pointers */
 73.1188 +    if (cinfo->dither_mode == JDITHER_FS)
 73.1189 +      cquantize->pub.color_quantize = pass2_fs_dither;
 73.1190 +    else
 73.1191 +      cquantize->pub.color_quantize = pass2_no_dither;
 73.1192 +    cquantize->pub.finish_pass = finish_pass2;
 73.1193 +
 73.1194 +    /* Make sure color count is acceptable */
 73.1195 +    i = cinfo->actual_number_of_colors;
 73.1196 +    if (i < 1)
 73.1197 +      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
 73.1198 +    if (i > MAXNUMCOLORS)
 73.1199 +      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
 73.1200 +
 73.1201 +    if (cinfo->dither_mode == JDITHER_FS) {
 73.1202 +      size_t arraysize = (size_t) ((cinfo->output_width + 2) *
 73.1203 +				   (3 * SIZEOF(FSERROR)));
 73.1204 +      /* Allocate Floyd-Steinberg workspace if we didn't already. */
 73.1205 +      if (cquantize->fserrors == NULL)
 73.1206 +	cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
 73.1207 +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
 73.1208 +      /* Initialize the propagated errors to zero. */
 73.1209 +      jzero_far((void FAR *) cquantize->fserrors, arraysize);
 73.1210 +      /* Make the error-limit table if we didn't already. */
 73.1211 +      if (cquantize->error_limiter == NULL)
 73.1212 +	init_error_limit(cinfo);
 73.1213 +      cquantize->on_odd_row = FALSE;
 73.1214 +    }
 73.1215 +
 73.1216 +  }
 73.1217 +  /* Zero the histogram or inverse color map, if necessary */
 73.1218 +  if (cquantize->needs_zeroed) {
 73.1219 +    for (i = 0; i < HIST_C0_ELEMS; i++) {
 73.1220 +      jzero_far((void FAR *) histogram[i],
 73.1221 +		HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
 73.1222 +    }
 73.1223 +    cquantize->needs_zeroed = FALSE;
 73.1224 +  }
 73.1225 +}
 73.1226 +
 73.1227 +
 73.1228 +/*
 73.1229 + * Switch to a new external colormap between output passes.
 73.1230 + */
 73.1231 +
 73.1232 +METHODDEF(void)
 73.1233 +new_color_map_2_quant (j_decompress_ptr cinfo)
 73.1234 +{
 73.1235 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 73.1236 +
 73.1237 +  /* Reset the inverse color map */
 73.1238 +  cquantize->needs_zeroed = TRUE;
 73.1239 +}
 73.1240 +
 73.1241 +
 73.1242 +/*
 73.1243 + * Module initialization routine for 2-pass color quantization.
 73.1244 + */
 73.1245 +
 73.1246 +GLOBAL(void)
 73.1247 +jinit_2pass_quantizer (j_decompress_ptr cinfo)
 73.1248 +{
 73.1249 +  my_cquantize_ptr cquantize;
 73.1250 +  int i;
 73.1251 +
 73.1252 +  cquantize = (my_cquantize_ptr)
 73.1253 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 73.1254 +				SIZEOF(my_cquantizer));
 73.1255 +  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
 73.1256 +  cquantize->pub.start_pass = start_pass_2_quant;
 73.1257 +  cquantize->pub.new_color_map = new_color_map_2_quant;
 73.1258 +  cquantize->fserrors = NULL;	/* flag optional arrays not allocated */
 73.1259 +  cquantize->error_limiter = NULL;
 73.1260 +
 73.1261 +  /* Make sure jdmaster didn't give me a case I can't handle */
 73.1262 +  if (cinfo->out_color_components != 3)
 73.1263 +    ERREXIT(cinfo, JERR_NOTIMPL);
 73.1264 +
 73.1265 +  /* Allocate the histogram/inverse colormap storage */
 73.1266 +  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
 73.1267 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
 73.1268 +  for (i = 0; i < HIST_C0_ELEMS; i++) {
 73.1269 +    cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
 73.1270 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
 73.1271 +       HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
 73.1272 +  }
 73.1273 +  cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
 73.1274 +
 73.1275 +  /* Allocate storage for the completed colormap, if required.
 73.1276 +   * We do this now since it is FAR storage and may affect
 73.1277 +   * the memory manager's space calculations.
 73.1278 +   */
 73.1279 +  if (cinfo->enable_2pass_quant) {
 73.1280 +    /* Make sure color count is acceptable */
 73.1281 +    int desired = cinfo->desired_number_of_colors;
 73.1282 +    /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
 73.1283 +    if (desired < 8)
 73.1284 +      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
 73.1285 +    /* Make sure colormap indexes can be represented by JSAMPLEs */
 73.1286 +    if (desired > MAXNUMCOLORS)
 73.1287 +      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
 73.1288 +    cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
 73.1289 +      ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
 73.1290 +    cquantize->desired = desired;
 73.1291 +  } else
 73.1292 +    cquantize->sv_colormap = NULL;
 73.1293 +
 73.1294 +  /* Only F-S dithering or no dithering is supported. */
 73.1295 +  /* If user asks for ordered dither, give him F-S. */
 73.1296 +  if (cinfo->dither_mode != JDITHER_NONE)
 73.1297 +    cinfo->dither_mode = JDITHER_FS;
 73.1298 +
 73.1299 +  /* Allocate Floyd-Steinberg workspace if necessary.
 73.1300 +   * This isn't really needed until pass 2, but again it is FAR storage.
 73.1301 +   * Although we will cope with a later change in dither_mode,
 73.1302 +   * we do not promise to honor max_memory_to_use if dither_mode changes.
 73.1303 +   */
 73.1304 +  if (cinfo->dither_mode == JDITHER_FS) {
 73.1305 +    cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
 73.1306 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
 73.1307 +       (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
 73.1308 +    /* Might as well create the error-limiting table too. */
 73.1309 +    init_error_limit(cinfo);
 73.1310 +  }
 73.1311 +}
 73.1312 +
 73.1313 +#endif /* QUANT_2PASS_SUPPORTED */
    74.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    74.2 +++ b/libs/libjpeg/jutils.c	Sun Jun 07 17:25:49 2015 +0300
    74.3 @@ -0,0 +1,179 @@
    74.4 +/*
    74.5 + * jutils.c
    74.6 + *
    74.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    74.8 + * This file is part of the Independent JPEG Group's software.
    74.9 + * For conditions of distribution and use, see the accompanying README file.
   74.10 + *
   74.11 + * This file contains tables and miscellaneous utility routines needed
   74.12 + * for both compression and decompression.
   74.13 + * Note we prefix all global names with "j" to minimize conflicts with
   74.14 + * a surrounding application.
   74.15 + */
   74.16 +
   74.17 +#define JPEG_INTERNALS
   74.18 +#include "jinclude.h"
   74.19 +#include "jpeglib.h"
   74.20 +
   74.21 +
   74.22 +/*
   74.23 + * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
   74.24 + * of a DCT block read in natural order (left to right, top to bottom).
   74.25 + */
   74.26 +
   74.27 +#if 0				/* This table is not actually needed in v6a */
   74.28 +
   74.29 +const int jpeg_zigzag_order[DCTSIZE2] = {
   74.30 +   0,  1,  5,  6, 14, 15, 27, 28,
   74.31 +   2,  4,  7, 13, 16, 26, 29, 42,
   74.32 +   3,  8, 12, 17, 25, 30, 41, 43,
   74.33 +   9, 11, 18, 24, 31, 40, 44, 53,
   74.34 +  10, 19, 23, 32, 39, 45, 52, 54,
   74.35 +  20, 22, 33, 38, 46, 51, 55, 60,
   74.36 +  21, 34, 37, 47, 50, 56, 59, 61,
   74.37 +  35, 36, 48, 49, 57, 58, 62, 63
   74.38 +};
   74.39 +
   74.40 +#endif
   74.41 +
   74.42 +/*
   74.43 + * jpeg_natural_order[i] is the natural-order position of the i'th element
   74.44 + * of zigzag order.
   74.45 + *
   74.46 + * When reading corrupted data, the Huffman decoders could attempt
   74.47 + * to reference an entry beyond the end of this array (if the decoded
   74.48 + * zero run length reaches past the end of the block).  To prevent
   74.49 + * wild stores without adding an inner-loop test, we put some extra
   74.50 + * "63"s after the real entries.  This will cause the extra coefficient
   74.51 + * to be stored in location 63 of the block, not somewhere random.
   74.52 + * The worst case would be a run-length of 15, which means we need 16
   74.53 + * fake entries.
   74.54 + */
   74.55 +
   74.56 +const int jpeg_natural_order[DCTSIZE2+16] = {
   74.57 +  0,  1,  8, 16,  9,  2,  3, 10,
   74.58 + 17, 24, 32, 25, 18, 11,  4,  5,
   74.59 + 12, 19, 26, 33, 40, 48, 41, 34,
   74.60 + 27, 20, 13,  6,  7, 14, 21, 28,
   74.61 + 35, 42, 49, 56, 57, 50, 43, 36,
   74.62 + 29, 22, 15, 23, 30, 37, 44, 51,
   74.63 + 58, 59, 52, 45, 38, 31, 39, 46,
   74.64 + 53, 60, 61, 54, 47, 55, 62, 63,
   74.65 + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
   74.66 + 63, 63, 63, 63, 63, 63, 63, 63
   74.67 +};
   74.68 +
   74.69 +
   74.70 +/*
   74.71 + * Arithmetic utilities
   74.72 + */
   74.73 +
   74.74 +GLOBAL(long)
   74.75 +jdiv_round_up (long a, long b)
   74.76 +/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
   74.77 +/* Assumes a >= 0, b > 0 */
   74.78 +{
   74.79 +  return (a + b - 1L) / b;
   74.80 +}
   74.81 +
   74.82 +
   74.83 +GLOBAL(long)
   74.84 +jround_up (long a, long b)
   74.85 +/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
   74.86 +/* Assumes a >= 0, b > 0 */
   74.87 +{
   74.88 +  a += b - 1L;
   74.89 +  return a - (a % b);
   74.90 +}
   74.91 +
   74.92 +
   74.93 +/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
   74.94 + * and coefficient-block arrays.  This won't work on 80x86 because the arrays
   74.95 + * are FAR and we're assuming a small-pointer memory model.  However, some
   74.96 + * DOS compilers provide far-pointer versions of memcpy() and memset() even
   74.97 + * in the small-model libraries.  These will be used if USE_FMEM is defined.
   74.98 + * Otherwise, the routines below do it the hard way.  (The performance cost
   74.99 + * is not all that great, because these routines aren't very heavily used.)
  74.100 + */
  74.101 +
  74.102 +#ifndef NEED_FAR_POINTERS	/* normal case, same as regular macros */
  74.103 +#define FMEMCOPY(dest,src,size)	MEMCOPY(dest,src,size)
  74.104 +#define FMEMZERO(target,size)	MEMZERO(target,size)
  74.105 +#else				/* 80x86 case, define if we can */
  74.106 +#ifdef USE_FMEM
  74.107 +#define FMEMCOPY(dest,src,size)	_fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
  74.108 +#define FMEMZERO(target,size)	_fmemset((void FAR *)(target), 0, (size_t)(size))
  74.109 +#endif
  74.110 +#endif
  74.111 +
  74.112 +
  74.113 +GLOBAL(void)
  74.114 +jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
  74.115 +		   JSAMPARRAY output_array, int dest_row,
  74.116 +		   int num_rows, JDIMENSION num_cols)
  74.117 +/* Copy some rows of samples from one place to another.
  74.118 + * num_rows rows are copied from input_array[source_row++]
  74.119 + * to output_array[dest_row++]; these areas may overlap for duplication.
  74.120 + * The source and destination arrays must be at least as wide as num_cols.
  74.121 + */
  74.122 +{
  74.123 +  register JSAMPROW inptr, outptr;
  74.124 +#ifdef FMEMCOPY
  74.125 +  register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
  74.126 +#else
  74.127 +  register JDIMENSION count;
  74.128 +#endif
  74.129 +  register int row;
  74.130 +
  74.131 +  input_array += source_row;
  74.132 +  output_array += dest_row;
  74.133 +
  74.134 +  for (row = num_rows; row > 0; row--) {
  74.135 +    inptr = *input_array++;
  74.136 +    outptr = *output_array++;
  74.137 +#ifdef FMEMCOPY
  74.138 +    FMEMCOPY(outptr, inptr, count);
  74.139 +#else
  74.140 +    for (count = num_cols; count > 0; count--)
  74.141 +      *outptr++ = *inptr++;	/* needn't bother with GETJSAMPLE() here */
  74.142 +#endif
  74.143 +  }
  74.144 +}
  74.145 +
  74.146 +
  74.147 +GLOBAL(void)
  74.148 +jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
  74.149 +		 JDIMENSION num_blocks)
  74.150 +/* Copy a row of coefficient blocks from one place to another. */
  74.151 +{
  74.152 +#ifdef FMEMCOPY
  74.153 +  FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
  74.154 +#else
  74.155 +  register JCOEFPTR inptr, outptr;
  74.156 +  register long count;
  74.157 +
  74.158 +  inptr = (JCOEFPTR) input_row;
  74.159 +  outptr = (JCOEFPTR) output_row;
  74.160 +  for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
  74.161 +    *outptr++ = *inptr++;
  74.162 +  }
  74.163 +#endif
  74.164 +}
  74.165 +
  74.166 +
  74.167 +GLOBAL(void)
  74.168 +jzero_far (void FAR * target, size_t bytestozero)
  74.169 +/* Zero out a chunk of FAR memory. */
  74.170 +/* This might be sample-array data, block-array data, or alloc_large data. */
  74.171 +{
  74.172 +#ifdef FMEMZERO
  74.173 +  FMEMZERO(target, bytestozero);
  74.174 +#else
  74.175 +  register char FAR * ptr = (char FAR *) target;
  74.176 +  register size_t count;
  74.177 +
  74.178 +  for (count = bytestozero; count > 0; count--) {
  74.179 +    *ptr++ = 0;
  74.180 +  }
  74.181 +#endif
  74.182 +}
    75.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    75.2 +++ b/libs/libjpeg/jversion.h	Sun Jun 07 17:25:49 2015 +0300
    75.3 @@ -0,0 +1,14 @@
    75.4 +/*
    75.5 + * jversion.h
    75.6 + *
    75.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    75.8 + * This file is part of the Independent JPEG Group's software.
    75.9 + * For conditions of distribution and use, see the accompanying README file.
   75.10 + *
   75.11 + * This file contains software version identification.
   75.12 + */
   75.13 +
   75.14 +
   75.15 +#define JVERSION	"6b  27-Mar-1998"
   75.16 +
   75.17 +#define JCOPYRIGHT	"Copyright (C) 1998, Thomas G. Lane"
    76.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    76.2 +++ b/libs/libpng/LICENSE	Sun Jun 07 17:25:49 2015 +0300
    76.3 @@ -0,0 +1,111 @@
    76.4 +
    76.5 +This copy of the libpng notices is provided for your convenience.  In case of
    76.6 +any discrepancy between this copy and the notices in the file png.h that is
    76.7 +included in the libpng distribution, the latter shall prevail.
    76.8 +
    76.9 +COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
   76.10 +
   76.11 +If you modify libpng you may insert additional notices immediately following
   76.12 +this sentence.
   76.13 +
   76.14 +This code is released under the libpng license.
   76.15 +
   76.16 +libpng versions 1.2.6, August 15, 2004, through 1.5.4, July 7, 2011, are
   76.17 +Copyright (c) 2004, 2006-2011 Glenn Randers-Pehrson, and are
   76.18 +distributed according to the same disclaimer and license as libpng-1.2.5
   76.19 +with the following individual added to the list of Contributing Authors
   76.20 +
   76.21 +   Cosmin Truta
   76.22 +
   76.23 +libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are
   76.24 +Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
   76.25 +distributed according to the same disclaimer and license as libpng-1.0.6
   76.26 +with the following individuals added to the list of Contributing Authors
   76.27 +
   76.28 +   Simon-Pierre Cadieux
   76.29 +   Eric S. Raymond
   76.30 +   Gilles Vollant
   76.31 +
   76.32 +and with the following additions to the disclaimer:
   76.33 +
   76.34 +   There is no warranty against interference with your enjoyment of the
   76.35 +   library or against infringement.  There is no warranty that our
   76.36 +   efforts or the library will fulfill any of your particular purposes
   76.37 +   or needs.  This library is provided with all faults, and the entire
   76.38 +   risk of satisfactory quality, performance, accuracy, and effort is with
   76.39 +   the user.
   76.40 +
   76.41 +libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
   76.42 +Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are
   76.43 +distributed according to the same disclaimer and license as libpng-0.96,
   76.44 +with the following individuals added to the list of Contributing Authors:
   76.45 +
   76.46 +   Tom Lane
   76.47 +   Glenn Randers-Pehrson
   76.48 +   Willem van Schaik
   76.49 +
   76.50 +libpng versions 0.89, June 1996, through 0.96, May 1997, are
   76.51 +Copyright (c) 1996, 1997 Andreas Dilger
   76.52 +Distributed according to the same disclaimer and license as libpng-0.88,
   76.53 +with the following individuals added to the list of Contributing Authors:
   76.54 +
   76.55 +   John Bowler
   76.56 +   Kevin Bracey
   76.57 +   Sam Bushell
   76.58 +   Magnus Holmgren
   76.59 +   Greg Roelofs
   76.60 +   Tom Tanner
   76.61 +
   76.62 +libpng versions 0.5, May 1995, through 0.88, January 1996, are
   76.63 +Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
   76.64 +
   76.65 +For the purposes of this copyright and license, "Contributing Authors"
   76.66 +is defined as the following set of individuals:
   76.67 +
   76.68 +   Andreas Dilger
   76.69 +   Dave Martindale
   76.70 +   Guy Eric Schalnat
   76.71 +   Paul Schmidt
   76.72 +   Tim Wegner
   76.73 +
   76.74 +The PNG Reference Library is supplied "AS IS".  The Contributing Authors
   76.75 +and Group 42, Inc. disclaim all warranties, expressed or implied,
   76.76 +including, without limitation, the warranties of merchantability and of
   76.77 +fitness for any purpose.  The Contributing Authors and Group 42, Inc.
   76.78 +assume no liability for direct, indirect, incidental, special, exemplary,
   76.79 +or consequential damages, which may result from the use of the PNG
   76.80 +Reference Library, even if advised of the possibility of such damage.
   76.81 +
   76.82 +Permission is hereby granted to use, copy, modify, and distribute this
   76.83 +source code, or portions hereof, for any purpose, without fee, subject
   76.84 +to the following restrictions:
   76.85 +
   76.86 +1. The origin of this source code must not be misrepresented.
   76.87 +
   76.88 +2. Altered versions must be plainly marked as such and must not
   76.89 +   be misrepresented as being the original source.
   76.90 +
   76.91 +3. This Copyright notice may not be removed or altered from any
   76.92 +   source or altered source distribution.
   76.93 +
   76.94 +The Contributing Authors and Group 42, Inc. specifically permit, without
   76.95 +fee, and encourage the use of this source code as a component to
   76.96 +supporting the PNG file format in commercial products.  If you use this
   76.97 +source code in a product, acknowledgment is not required but would be
   76.98 +appreciated.
   76.99 +
  76.100 +
  76.101 +A "png_get_copyright" function is available, for convenient use in "about"
  76.102 +boxes and the like:
  76.103 +
  76.104 +   printf("%s",png_get_copyright(NULL));
  76.105 +
  76.106 +Also, the PNG logo (in PNG format, of course) is supplied in the
  76.107 +files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
  76.108 +
  76.109 +Libpng is OSI Certified Open Source Software.  OSI Certified Open Source is a
  76.110 +certification mark of the Open Source Initiative.
  76.111 +
  76.112 +Glenn Randers-Pehrson
  76.113 +glennrp at users.sourceforge.net
  76.114 +July 7, 2011
    77.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    77.2 +++ b/libs/libpng/png.c	Sun Jun 07 17:25:49 2015 +0300
    77.3 @@ -0,0 +1,799 @@
    77.4 +
    77.5 +/* png.c - location for general purpose libpng functions
    77.6 + *
    77.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    77.8 + * For conditions of distribution and use, see copyright notice in png.h
    77.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   77.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   77.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   77.12 + */
   77.13 +
   77.14 +#define PNG_INTERNAL
   77.15 +#define PNG_NO_EXTERN
   77.16 +#include "png.h"
   77.17 +
   77.18 +/* Generate a compiler error if there is an old png.h in the search path. */
   77.19 +typedef version_1_2_33 Your_png_h_is_not_version_1_2_33;
   77.20 +
   77.21 +/* Version information for C files.  This had better match the version
   77.22 + * string defined in png.h.  */
   77.23 +
   77.24 +#ifdef PNG_USE_GLOBAL_ARRAYS
   77.25 +/* png_libpng_ver was changed to a function in version 1.0.5c */
   77.26 +PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
   77.27 +
   77.28 +#ifdef PNG_READ_SUPPORTED
   77.29 +
   77.30 +/* png_sig was changed to a function in version 1.0.5c */
   77.31 +/* Place to hold the signature string for a PNG file. */
   77.32 +PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   77.33 +#endif /* PNG_READ_SUPPORTED */
   77.34 +
   77.35 +/* Invoke global declarations for constant strings for known chunk types */
   77.36 +PNG_IHDR;
   77.37 +PNG_IDAT;
   77.38 +PNG_IEND;
   77.39 +PNG_PLTE;
   77.40 +PNG_bKGD;
   77.41 +PNG_cHRM;
   77.42 +PNG_gAMA;
   77.43 +PNG_hIST;
   77.44 +PNG_iCCP;
   77.45 +PNG_iTXt;
   77.46 +PNG_oFFs;
   77.47 +PNG_pCAL;
   77.48 +PNG_sCAL;
   77.49 +PNG_pHYs;
   77.50 +PNG_sBIT;
   77.51 +PNG_sPLT;
   77.52 +PNG_sRGB;
   77.53 +PNG_tEXt;
   77.54 +PNG_tIME;
   77.55 +PNG_tRNS;
   77.56 +PNG_zTXt;
   77.57 +
   77.58 +#ifdef PNG_READ_SUPPORTED
   77.59 +/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
   77.60 +
   77.61 +/* start of interlace block */
   77.62 +PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
   77.63 +
   77.64 +/* offset to next interlace block */
   77.65 +PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
   77.66 +
   77.67 +/* start of interlace block in the y direction */
   77.68 +PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
   77.69 +
   77.70 +/* offset to next interlace block in the y direction */
   77.71 +PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
   77.72 +
   77.73 +/* Height of interlace block.  This is not currently used - if you need
   77.74 + * it, uncomment it here and in png.h
   77.75 +PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
   77.76 +*/
   77.77 +
   77.78 +/* Mask to determine which pixels are valid in a pass */
   77.79 +PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
   77.80 +
   77.81 +/* Mask to determine which pixels to overwrite while displaying */
   77.82 +PNG_CONST int FARDATA png_pass_dsp_mask[]
   77.83 +   = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
   77.84 +
   77.85 +#endif /* PNG_READ_SUPPORTED */
   77.86 +#endif /* PNG_USE_GLOBAL_ARRAYS */
   77.87 +
   77.88 +/* Tells libpng that we have already handled the first "num_bytes" bytes
   77.89 + * of the PNG file signature.  If the PNG data is embedded into another
   77.90 + * stream we can set num_bytes = 8 so that libpng will not attempt to read
   77.91 + * or write any of the magic bytes before it starts on the IHDR.
   77.92 + */
   77.93 +
   77.94 +#ifdef PNG_READ_SUPPORTED
   77.95 +void PNGAPI
   77.96 +png_set_sig_bytes(png_structp png_ptr, int num_bytes)
   77.97 +{
   77.98 +   if (png_ptr == NULL) return;
   77.99 +   png_debug(1, "in png_set_sig_bytes\n");
  77.100 +   if (num_bytes > 8)
  77.101 +      png_error(png_ptr, "Too many bytes for PNG signature.");
  77.102 +
  77.103 +   png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  77.104 +}
  77.105 +
  77.106 +/* Checks whether the supplied bytes match the PNG signature.  We allow
  77.107 + * checking less than the full 8-byte signature so that those apps that
  77.108 + * already read the first few bytes of a file to determine the file type
  77.109 + * can simply check the remaining bytes for extra assurance.  Returns
  77.110 + * an integer less than, equal to, or greater than zero if sig is found,
  77.111 + * respectively, to be less than, to match, or be greater than the correct
  77.112 + * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  77.113 + */
  77.114 +int PNGAPI
  77.115 +png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  77.116 +{
  77.117 +   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  77.118 +   if (num_to_check > 8)
  77.119 +      num_to_check = 8;
  77.120 +   else if (num_to_check < 1)
  77.121 +      return (-1);
  77.122 +
  77.123 +   if (start > 7)
  77.124 +      return (-1);
  77.125 +
  77.126 +   if (start + num_to_check > 8)
  77.127 +      num_to_check = 8 - start;
  77.128 +
  77.129 +   return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
  77.130 +}
  77.131 +
  77.132 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  77.133 +/* (Obsolete) function to check signature bytes.  It does not allow one
  77.134 + * to check a partial signature.  This function might be removed in the
  77.135 + * future - use png_sig_cmp().  Returns true (nonzero) if the file is PNG.
  77.136 + */
  77.137 +int PNGAPI
  77.138 +png_check_sig(png_bytep sig, int num)
  77.139 +{
  77.140 +  return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
  77.141 +}
  77.142 +#endif
  77.143 +#endif /* PNG_READ_SUPPORTED */
  77.144 +
  77.145 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  77.146 +/* Function to allocate memory for zlib and clear it to 0. */
  77.147 +#ifdef PNG_1_0_X
  77.148 +voidpf PNGAPI
  77.149 +#else
  77.150 +voidpf /* private */
  77.151 +#endif
  77.152 +png_zalloc(voidpf png_ptr, uInt items, uInt size)
  77.153 +{
  77.154 +   png_voidp ptr;
  77.155 +   png_structp p=(png_structp)png_ptr;
  77.156 +   png_uint_32 save_flags=p->flags;
  77.157 +   png_uint_32 num_bytes;
  77.158 +
  77.159 +   if (png_ptr == NULL) return (NULL);
  77.160 +   if (items > PNG_UINT_32_MAX/size)
  77.161 +   {
  77.162 +     png_warning (p, "Potential overflow in png_zalloc()");
  77.163 +     return (NULL);
  77.164 +   }
  77.165 +   num_bytes = (png_uint_32)items * size;
  77.166 +
  77.167 +   p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  77.168 +   ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
  77.169 +   p->flags=save_flags;
  77.170 +
  77.171 +#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
  77.172 +   if (ptr == NULL)
  77.173 +       return ((voidpf)ptr);
  77.174 +
  77.175 +   if (num_bytes > (png_uint_32)0x8000L)
  77.176 +   {
  77.177 +      png_memset(ptr, 0, (png_size_t)0x8000L);
  77.178 +      png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  77.179 +         (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  77.180 +   }
  77.181 +   else
  77.182 +   {
  77.183 +      png_memset(ptr, 0, (png_size_t)num_bytes);
  77.184 +   }
  77.185 +#endif
  77.186 +   return ((voidpf)ptr);
  77.187 +}
  77.188 +
  77.189 +/* function to free memory for zlib */
  77.190 +#ifdef PNG_1_0_X
  77.191 +void PNGAPI
  77.192 +#else
  77.193 +void /* private */
  77.194 +#endif
  77.195 +png_zfree(voidpf png_ptr, voidpf ptr)
  77.196 +{
  77.197 +   png_free((png_structp)png_ptr, (png_voidp)ptr);
  77.198 +}
  77.199 +
  77.200 +/* Reset the CRC variable to 32 bits of 1's.  Care must be taken
  77.201 + * in case CRC is > 32 bits to leave the top bits 0.
  77.202 + */
  77.203 +void /* PRIVATE */
  77.204 +png_reset_crc(png_structp png_ptr)
  77.205 +{
  77.206 +   png_ptr->crc = crc32(0, Z_NULL, 0);
  77.207 +}
  77.208 +
  77.209 +/* Calculate the CRC over a section of data.  We can only pass as
  77.210 + * much data to this routine as the largest single buffer size.  We
  77.211 + * also check that this data will actually be used before going to the
  77.212 + * trouble of calculating it.
  77.213 + */
  77.214 +void /* PRIVATE */
  77.215 +png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  77.216 +{
  77.217 +   int need_crc = 1;
  77.218 +
  77.219 +   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  77.220 +   {
  77.221 +      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  77.222 +          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  77.223 +         need_crc = 0;
  77.224 +   }
  77.225 +   else                                                    /* critical */
  77.226 +   {
  77.227 +      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  77.228 +         need_crc = 0;
  77.229 +   }
  77.230 +
  77.231 +   if (need_crc)
  77.232 +      png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
  77.233 +}
  77.234 +
  77.235 +/* Allocate the memory for an info_struct for the application.  We don't
  77.236 + * really need the png_ptr, but it could potentially be useful in the
  77.237 + * future.  This should be used in favour of malloc(png_sizeof(png_info))
  77.238 + * and png_info_init() so that applications that want to use a shared
  77.239 + * libpng don't have to be recompiled if png_info changes size.
  77.240 + */
  77.241 +png_infop PNGAPI
  77.242 +png_create_info_struct(png_structp png_ptr)
  77.243 +{
  77.244 +   png_infop info_ptr;
  77.245 +
  77.246 +   png_debug(1, "in png_create_info_struct\n");
  77.247 +   if (png_ptr == NULL) return (NULL);
  77.248 +#ifdef PNG_USER_MEM_SUPPORTED
  77.249 +   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
  77.250 +      png_ptr->malloc_fn, png_ptr->mem_ptr);
  77.251 +#else
  77.252 +   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  77.253 +#endif
  77.254 +   if (info_ptr != NULL)
  77.255 +      png_info_init_3(&info_ptr, png_sizeof(png_info));
  77.256 +
  77.257 +   return (info_ptr);
  77.258 +}
  77.259 +
  77.260 +/* This function frees the memory associated with a single info struct.
  77.261 + * Normally, one would use either png_destroy_read_struct() or
  77.262 + * png_destroy_write_struct() to free an info struct, but this may be
  77.263 + * useful for some applications.
  77.264 + */
  77.265 +void PNGAPI
  77.266 +png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  77.267 +{
  77.268 +   png_infop info_ptr = NULL;
  77.269 +   if (png_ptr == NULL) return;
  77.270 +
  77.271 +   png_debug(1, "in png_destroy_info_struct\n");
  77.272 +   if (info_ptr_ptr != NULL)
  77.273 +      info_ptr = *info_ptr_ptr;
  77.274 +
  77.275 +   if (info_ptr != NULL)
  77.276 +   {
  77.277 +      png_info_destroy(png_ptr, info_ptr);
  77.278 +
  77.279 +#ifdef PNG_USER_MEM_SUPPORTED
  77.280 +      png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
  77.281 +          png_ptr->mem_ptr);
  77.282 +#else
  77.283 +      png_destroy_struct((png_voidp)info_ptr);
  77.284 +#endif
  77.285 +      *info_ptr_ptr = NULL;
  77.286 +   }
  77.287 +}
  77.288 +
  77.289 +/* Initialize the info structure.  This is now an internal function (0.89)
  77.290 + * and applications using it are urged to use png_create_info_struct()
  77.291 + * instead.
  77.292 + */
  77.293 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  77.294 +#undef png_info_init
  77.295 +void PNGAPI
  77.296 +png_info_init(png_infop info_ptr)
  77.297 +{
  77.298 +   /* We only come here via pre-1.0.12-compiled applications */
  77.299 +   png_info_init_3(&info_ptr, 0);
  77.300 +}
  77.301 +#endif
  77.302 +
  77.303 +void PNGAPI
  77.304 +png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
  77.305 +{
  77.306 +   png_infop info_ptr = *ptr_ptr;
  77.307 +
  77.308 +   if (info_ptr == NULL) return;
  77.309 +
  77.310 +   png_debug(1, "in png_info_init_3\n");
  77.311 +
  77.312 +   if (png_sizeof(png_info) > png_info_struct_size)
  77.313 +     {
  77.314 +       png_destroy_struct(info_ptr);
  77.315 +       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  77.316 +       *ptr_ptr = info_ptr;
  77.317 +     }
  77.318 +
  77.319 +   /* set everything to 0 */
  77.320 +   png_memset(info_ptr, 0, png_sizeof(png_info));
  77.321 +}
  77.322 +
  77.323 +#ifdef PNG_FREE_ME_SUPPORTED
  77.324 +void PNGAPI
  77.325 +png_data_freer(png_structp png_ptr, png_infop info_ptr,
  77.326 +   int freer, png_uint_32 mask)
  77.327 +{
  77.328 +   png_debug(1, "in png_data_freer\n");
  77.329 +   if (png_ptr == NULL || info_ptr == NULL)
  77.330 +      return;
  77.331 +   if (freer == PNG_DESTROY_WILL_FREE_DATA)
  77.332 +      info_ptr->free_me |= mask;
  77.333 +   else if (freer == PNG_USER_WILL_FREE_DATA)
  77.334 +      info_ptr->free_me &= ~mask;
  77.335 +   else
  77.336 +      png_warning(png_ptr,
  77.337 +         "Unknown freer parameter in png_data_freer.");
  77.338 +}
  77.339 +#endif
  77.340 +
  77.341 +void PNGAPI
  77.342 +png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
  77.343 +   int num)
  77.344 +{
  77.345 +   png_debug(1, "in png_free_data\n");
  77.346 +   if (png_ptr == NULL || info_ptr == NULL)
  77.347 +      return;
  77.348 +
  77.349 +#if defined(PNG_TEXT_SUPPORTED)
  77.350 +/* free text item num or (if num == -1) all text items */
  77.351 +#ifdef PNG_FREE_ME_SUPPORTED
  77.352 +if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  77.353 +#else
  77.354 +if (mask & PNG_FREE_TEXT)
  77.355 +#endif
  77.356 +{
  77.357 +   if (num != -1)
  77.358 +   {
  77.359 +     if (info_ptr->text && info_ptr->text[num].key)
  77.360 +     {
  77.361 +         png_free(png_ptr, info_ptr->text[num].key);
  77.362 +         info_ptr->text[num].key = NULL;
  77.363 +     }
  77.364 +   }
  77.365 +   else
  77.366 +   {
  77.367 +       int i;
  77.368 +       for (i = 0; i < info_ptr->num_text; i++)
  77.369 +           png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  77.370 +       png_free(png_ptr, info_ptr->text);
  77.371 +       info_ptr->text = NULL;
  77.372 +       info_ptr->num_text=0;
  77.373 +   }
  77.374 +}
  77.375 +#endif
  77.376 +
  77.377 +#if defined(PNG_tRNS_SUPPORTED)
  77.378 +/* free any tRNS entry */
  77.379 +#ifdef PNG_FREE_ME_SUPPORTED
  77.380 +if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  77.381 +#else
  77.382 +if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
  77.383 +#endif
  77.384 +{
  77.385 +    png_free(png_ptr, info_ptr->trans);
  77.386 +    info_ptr->trans = NULL;
  77.387 +    info_ptr->valid &= ~PNG_INFO_tRNS;
  77.388 +#ifndef PNG_FREE_ME_SUPPORTED
  77.389 +    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  77.390 +#endif
  77.391 +}
  77.392 +#endif
  77.393 +
  77.394 +#if defined(PNG_sCAL_SUPPORTED)
  77.395 +/* free any sCAL entry */
  77.396 +#ifdef PNG_FREE_ME_SUPPORTED
  77.397 +if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  77.398 +#else
  77.399 +if (mask & PNG_FREE_SCAL)
  77.400 +#endif
  77.401 +{
  77.402 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  77.403 +    png_free(png_ptr, info_ptr->scal_s_width);
  77.404 +    png_free(png_ptr, info_ptr->scal_s_height);
  77.405 +    info_ptr->scal_s_width = NULL;
  77.406 +    info_ptr->scal_s_height = NULL;
  77.407 +#endif
  77.408 +    info_ptr->valid &= ~PNG_INFO_sCAL;
  77.409 +}
  77.410 +#endif
  77.411 +
  77.412 +#if defined(PNG_pCAL_SUPPORTED)
  77.413 +/* free any pCAL entry */
  77.414 +#ifdef PNG_FREE_ME_SUPPORTED
  77.415 +if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  77.416 +#else
  77.417 +if (mask & PNG_FREE_PCAL)
  77.418 +#endif
  77.419 +{
  77.420 +    png_free(png_ptr, info_ptr->pcal_purpose);
  77.421 +    png_free(png_ptr, info_ptr->pcal_units);
  77.422 +    info_ptr->pcal_purpose = NULL;
  77.423 +    info_ptr->pcal_units = NULL;
  77.424 +    if (info_ptr->pcal_params != NULL)
  77.425 +    {
  77.426 +        int i;
  77.427 +        for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
  77.428 +        {
  77.429 +          png_free(png_ptr, info_ptr->pcal_params[i]);
  77.430 +          info_ptr->pcal_params[i]=NULL;
  77.431 +        }
  77.432 +        png_free(png_ptr, info_ptr->pcal_params);
  77.433 +        info_ptr->pcal_params = NULL;
  77.434 +    }
  77.435 +    info_ptr->valid &= ~PNG_INFO_pCAL;
  77.436 +}
  77.437 +#endif
  77.438 +
  77.439 +#if defined(PNG_iCCP_SUPPORTED)
  77.440 +/* free any iCCP entry */
  77.441 +#ifdef PNG_FREE_ME_SUPPORTED
  77.442 +if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  77.443 +#else
  77.444 +if (mask & PNG_FREE_ICCP)
  77.445 +#endif
  77.446 +{
  77.447 +    png_free(png_ptr, info_ptr->iccp_name);
  77.448 +    png_free(png_ptr, info_ptr->iccp_profile);
  77.449 +    info_ptr->iccp_name = NULL;
  77.450 +    info_ptr->iccp_profile = NULL;
  77.451 +    info_ptr->valid &= ~PNG_INFO_iCCP;
  77.452 +}
  77.453 +#endif
  77.454 +
  77.455 +#if defined(PNG_sPLT_SUPPORTED)
  77.456 +/* free a given sPLT entry, or (if num == -1) all sPLT entries */
  77.457 +#ifdef PNG_FREE_ME_SUPPORTED
  77.458 +if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  77.459 +#else
  77.460 +if (mask & PNG_FREE_SPLT)
  77.461 +#endif
  77.462 +{
  77.463 +   if (num != -1)
  77.464 +   {
  77.465 +      if (info_ptr->splt_palettes)
  77.466 +      {
  77.467 +          png_free(png_ptr, info_ptr->splt_palettes[num].name);
  77.468 +          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  77.469 +          info_ptr->splt_palettes[num].name = NULL;
  77.470 +          info_ptr->splt_palettes[num].entries = NULL;
  77.471 +      }
  77.472 +   }
  77.473 +   else
  77.474 +   {
  77.475 +       if (info_ptr->splt_palettes_num)
  77.476 +       {
  77.477 +         int i;
  77.478 +         for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  77.479 +            png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
  77.480 +
  77.481 +         png_free(png_ptr, info_ptr->splt_palettes);
  77.482 +         info_ptr->splt_palettes = NULL;
  77.483 +         info_ptr->splt_palettes_num = 0;
  77.484 +       }
  77.485 +       info_ptr->valid &= ~PNG_INFO_sPLT;
  77.486 +   }
  77.487 +}
  77.488 +#endif
  77.489 +
  77.490 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  77.491 +  if (png_ptr->unknown_chunk.data)
  77.492 +  {
  77.493 +    png_free(png_ptr, png_ptr->unknown_chunk.data);
  77.494 +    png_ptr->unknown_chunk.data = NULL;
  77.495 +  }
  77.496 +
  77.497 +#ifdef PNG_FREE_ME_SUPPORTED
  77.498 +if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  77.499 +#else
  77.500 +if (mask & PNG_FREE_UNKN)
  77.501 +#endif
  77.502 +{
  77.503 +   if (num != -1)
  77.504 +   {
  77.505 +       if (info_ptr->unknown_chunks)
  77.506 +       {
  77.507 +          png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  77.508 +          info_ptr->unknown_chunks[num].data = NULL;
  77.509 +       }
  77.510 +   }
  77.511 +   else
  77.512 +   {
  77.513 +       int i;
  77.514 +
  77.515 +       if (info_ptr->unknown_chunks_num)
  77.516 +       {
  77.517 +         for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
  77.518 +            png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
  77.519 +
  77.520 +         png_free(png_ptr, info_ptr->unknown_chunks);
  77.521 +         info_ptr->unknown_chunks = NULL;
  77.522 +         info_ptr->unknown_chunks_num = 0;
  77.523 +       }
  77.524 +   }
  77.525 +}
  77.526 +#endif
  77.527 +
  77.528 +#if defined(PNG_hIST_SUPPORTED)
  77.529 +/* free any hIST entry */
  77.530 +#ifdef PNG_FREE_ME_SUPPORTED
  77.531 +if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
  77.532 +#else
  77.533 +if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
  77.534 +#endif
  77.535 +{
  77.536 +    png_free(png_ptr, info_ptr->hist);
  77.537 +    info_ptr->hist = NULL;
  77.538 +    info_ptr->valid &= ~PNG_INFO_hIST;
  77.539 +#ifndef PNG_FREE_ME_SUPPORTED
  77.540 +    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  77.541 +#endif
  77.542 +}
  77.543 +#endif
  77.544 +
  77.545 +/* free any PLTE entry that was internally allocated */
  77.546 +#ifdef PNG_FREE_ME_SUPPORTED
  77.547 +if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  77.548 +#else
  77.549 +if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
  77.550 +#endif
  77.551 +{
  77.552 +    png_zfree(png_ptr, info_ptr->palette);
  77.553 +    info_ptr->palette = NULL;
  77.554 +    info_ptr->valid &= ~PNG_INFO_PLTE;
  77.555 +#ifndef PNG_FREE_ME_SUPPORTED
  77.556 +    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  77.557 +#endif
  77.558 +    info_ptr->num_palette = 0;
  77.559 +}
  77.560 +
  77.561 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  77.562 +/* free any image bits attached to the info structure */
  77.563 +#ifdef PNG_FREE_ME_SUPPORTED
  77.564 +if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  77.565 +#else
  77.566 +if (mask & PNG_FREE_ROWS)
  77.567 +#endif
  77.568 +{
  77.569 +    if (info_ptr->row_pointers)
  77.570 +    {
  77.571 +       int row;
  77.572 +       for (row = 0; row < (int)info_ptr->height; row++)
  77.573 +       {
  77.574 +          png_free(png_ptr, info_ptr->row_pointers[row]);
  77.575 +          info_ptr->row_pointers[row]=NULL;
  77.576 +       }
  77.577 +       png_free(png_ptr, info_ptr->row_pointers);
  77.578 +       info_ptr->row_pointers=NULL;
  77.579 +    }
  77.580 +    info_ptr->valid &= ~PNG_INFO_IDAT;
  77.581 +}
  77.582 +#endif
  77.583 +
  77.584 +#ifdef PNG_FREE_ME_SUPPORTED
  77.585 +   if (num == -1)
  77.586 +     info_ptr->free_me &= ~mask;
  77.587 +   else
  77.588 +     info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
  77.589 +#endif
  77.590 +}
  77.591 +
  77.592 +/* This is an internal routine to free any memory that the info struct is
  77.593 + * pointing to before re-using it or freeing the struct itself.  Recall
  77.594 + * that png_free() checks for NULL pointers for us.
  77.595 + */
  77.596 +void /* PRIVATE */
  77.597 +png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  77.598 +{
  77.599 +   png_debug(1, "in png_info_destroy\n");
  77.600 +
  77.601 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  77.602 +
  77.603 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  77.604 +   if (png_ptr->num_chunk_list)
  77.605 +   {
  77.606 +       png_free(png_ptr, png_ptr->chunk_list);
  77.607 +       png_ptr->chunk_list=NULL;
  77.608 +       png_ptr->num_chunk_list = 0;
  77.609 +   }
  77.610 +#endif
  77.611 +
  77.612 +   png_info_init_3(&info_ptr, png_sizeof(png_info));
  77.613 +}
  77.614 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  77.615 +
  77.616 +/* This function returns a pointer to the io_ptr associated with the user
  77.617 + * functions.  The application should free any memory associated with this
  77.618 + * pointer before png_write_destroy() or png_read_destroy() are called.
  77.619 + */
  77.620 +png_voidp PNGAPI
  77.621 +png_get_io_ptr(png_structp png_ptr)
  77.622 +{
  77.623 +   if (png_ptr == NULL) return (NULL);
  77.624 +   return (png_ptr->io_ptr);
  77.625 +}
  77.626 +
  77.627 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  77.628 +#if !defined(PNG_NO_STDIO)
  77.629 +/* Initialize the default input/output functions for the PNG file.  If you
  77.630 + * use your own read or write routines, you can call either png_set_read_fn()
  77.631 + * or png_set_write_fn() instead of png_init_io().  If you have defined
  77.632 + * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
  77.633 + * necessarily available.
  77.634 + */
  77.635 +void PNGAPI
  77.636 +png_init_io(png_structp png_ptr, png_FILE_p fp)
  77.637 +{
  77.638 +   png_debug(1, "in png_init_io\n");
  77.639 +   if (png_ptr == NULL) return;
  77.640 +   png_ptr->io_ptr = (png_voidp)fp;
  77.641 +}
  77.642 +#endif
  77.643 +
  77.644 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
  77.645 +/* Convert the supplied time into an RFC 1123 string suitable for use in
  77.646 + * a "Creation Time" or other text-based time string.
  77.647 + */
  77.648 +png_charp PNGAPI
  77.649 +png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
  77.650 +{
  77.651 +   static PNG_CONST char short_months[12][4] =
  77.652 +        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  77.653 +         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  77.654 +
  77.655 +   if (png_ptr == NULL) return (NULL);
  77.656 +   if (png_ptr->time_buffer == NULL)
  77.657 +   {
  77.658 +      png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
  77.659 +         png_sizeof(char)));
  77.660 +   }
  77.661 +
  77.662 +#if defined(_WIN32_WCE)
  77.663 +   {
  77.664 +      wchar_t time_buf[29];
  77.665 +      wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
  77.666 +          ptime->day % 32, short_months[(ptime->month - 1) % 12],
  77.667 +        ptime->year, ptime->hour % 24, ptime->minute % 60,
  77.668 +          ptime->second % 61);
  77.669 +      WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
  77.670 +          NULL, NULL);
  77.671 +   }
  77.672 +#else
  77.673 +#ifdef USE_FAR_KEYWORD
  77.674 +   {
  77.675 +      char near_time_buf[29];
  77.676 +      png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
  77.677 +          ptime->day % 32, short_months[(ptime->month - 1) % 12],
  77.678 +          ptime->year, ptime->hour % 24, ptime->minute % 60,
  77.679 +          ptime->second % 61);
  77.680 +      png_memcpy(png_ptr->time_buffer, near_time_buf,
  77.681 +          29*png_sizeof(char));
  77.682 +   }
  77.683 +#else
  77.684 +   png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
  77.685 +       ptime->day % 32, short_months[(ptime->month - 1) % 12],
  77.686 +       ptime->year, ptime->hour % 24, ptime->minute % 60,
  77.687 +       ptime->second % 61);
  77.688 +#endif
  77.689 +#endif /* _WIN32_WCE */
  77.690 +   return ((png_charp)png_ptr->time_buffer);
  77.691 +}
  77.692 +#endif /* PNG_TIME_RFC1123_SUPPORTED */
  77.693 +
  77.694 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  77.695 +
  77.696 +png_charp PNGAPI
  77.697 +png_get_copyright(png_structp png_ptr)
  77.698 +{
  77.699 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  77.700 +   return ((png_charp) "\n libpng version 1.2.33 - October 31, 2008\n\
  77.701 +   Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
  77.702 +   Copyright (c) 1996-1997 Andreas Dilger\n\
  77.703 +   Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
  77.704 +}
  77.705 +
  77.706 +/* The following return the library version as a short string in the
  77.707 + * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
  77.708 + * used with your application, print out PNG_LIBPNG_VER_STRING, which
  77.709 + * is defined in png.h.
  77.710 + * Note: now there is no difference between png_get_libpng_ver() and
  77.711 + * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
  77.712 + * it is guaranteed that png.c uses the correct version of png.h.
  77.713 + */
  77.714 +png_charp PNGAPI
  77.715 +png_get_libpng_ver(png_structp png_ptr)
  77.716 +{
  77.717 +   /* Version of *.c files used when building libpng */
  77.718 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  77.719 +   return ((png_charp) PNG_LIBPNG_VER_STRING);
  77.720 +}
  77.721 +
  77.722 +png_charp PNGAPI
  77.723 +png_get_header_ver(png_structp png_ptr)
  77.724 +{
  77.725 +   /* Version of *.h files used when building libpng */
  77.726 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  77.727 +   return ((png_charp) PNG_LIBPNG_VER_STRING);
  77.728 +}
  77.729 +
  77.730 +png_charp PNGAPI
  77.731 +png_get_header_version(png_structp png_ptr)
  77.732 +{
  77.733 +   /* Returns longer string containing both version and date */
  77.734 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  77.735 +   return ((png_charp) PNG_HEADER_VERSION_STRING
  77.736 +#ifndef PNG_READ_SUPPORTED
  77.737 +   "     (NO READ SUPPORT)"
  77.738 +#endif
  77.739 +   "\n");
  77.740 +}
  77.741 +
  77.742 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  77.743 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  77.744 +int PNGAPI
  77.745 +png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
  77.746 +{
  77.747 +   /* check chunk_name and return "keep" value if it's on the list, else 0 */
  77.748 +   int i;
  77.749 +   png_bytep p;
  77.750 +   if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
  77.751 +      return 0;
  77.752 +   p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
  77.753 +   for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
  77.754 +      if (!png_memcmp(chunk_name, p, 4))
  77.755 +        return ((int)*(p + 4));
  77.756 +   return 0;
  77.757 +}
  77.758 +#endif
  77.759 +
  77.760 +/* This function, added to libpng-1.0.6g, is untested. */
  77.761 +int PNGAPI
  77.762 +png_reset_zstream(png_structp png_ptr)
  77.763 +{
  77.764 +   if (png_ptr == NULL) return Z_STREAM_ERROR;
  77.765 +   return (inflateReset(&png_ptr->zstream));
  77.766 +}
  77.767 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  77.768 +
  77.769 +/* This function was added to libpng-1.0.7 */
  77.770 +png_uint_32 PNGAPI
  77.771 +png_access_version_number(void)
  77.772 +{
  77.773 +   /* Version of *.c files used when building libpng */
  77.774 +   return((png_uint_32) PNG_LIBPNG_VER);
  77.775 +}
  77.776 +
  77.777 +
  77.778 +#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
  77.779 +#if !defined(PNG_1_0_X)
  77.780 +/* this function was added to libpng 1.2.0 */
  77.781 +int PNGAPI
  77.782 +png_mmx_support(void)
  77.783 +{
  77.784 +   /* obsolete, to be removed from libpng-1.4.0 */
  77.785 +    return -1;
  77.786 +}
  77.787 +#endif /* PNG_1_0_X */
  77.788 +#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
  77.789 +
  77.790 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  77.791 +#ifdef PNG_SIZE_T
  77.792 +/* Added at libpng version 1.2.6 */
  77.793 +   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
  77.794 +png_size_t PNGAPI
  77.795 +png_convert_size(size_t size)
  77.796 +{
  77.797 +  if (size > (png_size_t)-1)
  77.798 +     PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
  77.799 +  return ((png_size_t)size);
  77.800 +}
  77.801 +#endif /* PNG_SIZE_T */
  77.802 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
    78.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    78.2 +++ b/libs/libpng/png.h	Sun Jun 07 17:25:49 2015 +0300
    78.3 @@ -0,0 +1,3597 @@
    78.4 +/* png.h - header file for PNG reference library
    78.5 + *
    78.6 + * libpng version 1.2.33 - October 31, 2008
    78.7 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    78.8 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    78.9 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   78.10 + *
   78.11 + * Authors and maintainers:
   78.12 + *  libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
   78.13 + *  libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
   78.14 + *  libpng versions 0.97, January 1998, through 1.2.33 - October 31, 2008: Glenn
   78.15 + *  See also "Contributing Authors", below.
   78.16 + *
   78.17 + * Note about libpng version numbers:
   78.18 + *
   78.19 + *    Due to various miscommunications, unforeseen code incompatibilities
   78.20 + *    and occasional factors outside the authors' control, version numbering
   78.21 + *    on the library has not always been consistent and straightforward.
   78.22 + *    The following table summarizes matters since version 0.89c, which was
   78.23 + *    the first widely used release:
   78.24 + *
   78.25 + *    source                 png.h  png.h  shared-lib
   78.26 + *    version                string   int  version
   78.27 + *    -------                ------ -----  ----------
   78.28 + *    0.89c "1.0 beta 3"     0.89      89  1.0.89
   78.29 + *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]
   78.30 + *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]
   78.31 + *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]
   78.32 + *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]
   78.33 + *    0.97c                  0.97      97  2.0.97
   78.34 + *    0.98                   0.98      98  2.0.98
   78.35 + *    0.99                   0.99      98  2.0.99
   78.36 + *    0.99a-m                0.99      99  2.0.99
   78.37 + *    1.00                   1.00     100  2.1.0 [100 should be 10000]
   78.38 + *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]
   78.39 + *    1.0.1       png.h string is   10001  2.1.0
   78.40 + *    1.0.1a-e    identical to the  10002  from here on, the shared library
   78.41 + *    1.0.2       source version)   10002  is 2.V where V is the source code
   78.42 + *    1.0.2a-b                      10003  version, except as noted.
   78.43 + *    1.0.3                         10003
   78.44 + *    1.0.3a-d                      10004
   78.45 + *    1.0.4                         10004
   78.46 + *    1.0.4a-f                      10005
   78.47 + *    1.0.5 (+ 2 patches)           10005
   78.48 + *    1.0.5a-d                      10006
   78.49 + *    1.0.5e-r                      10100 (not source compatible)
   78.50 + *    1.0.5s-v                      10006 (not binary compatible)
   78.51 + *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)
   78.52 + *    1.0.6d-f                      10007 (still binary incompatible)
   78.53 + *    1.0.6g                        10007
   78.54 + *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)
   78.55 + *    1.0.6i                        10007  10.6i
   78.56 + *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)
   78.57 + *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)
   78.58 + *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)
   78.59 + *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)
   78.60 + *    1.0.7                    1    10007  (still compatible)
   78.61 + *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4
   78.62 + *    1.0.8rc1                 1    10008  2.1.0.8rc1
   78.63 + *    1.0.8                    1    10008  2.1.0.8
   78.64 + *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6
   78.65 + *    1.0.9rc1                 1    10009  2.1.0.9rc1
   78.66 + *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10
   78.67 + *    1.0.9rc2                 1    10009  2.1.0.9rc2
   78.68 + *    1.0.9                    1    10009  2.1.0.9
   78.69 + *    1.0.10beta1              1    10010  2.1.0.10beta1
   78.70 + *    1.0.10rc1                1    10010  2.1.0.10rc1
   78.71 + *    1.0.10                   1    10010  2.1.0.10
   78.72 + *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3
   78.73 + *    1.0.11rc1                1    10011  2.1.0.11rc1
   78.74 + *    1.0.11                   1    10011  2.1.0.11
   78.75 + *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2
   78.76 + *    1.0.12rc1                2    10012  2.1.0.12rc1
   78.77 + *    1.0.12                   2    10012  2.1.0.12
   78.78 + *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)
   78.79 + *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2
   78.80 + *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5
   78.81 + *    1.2.0rc1                 3    10200  3.1.2.0rc1
   78.82 + *    1.2.0                    3    10200  3.1.2.0
   78.83 + *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4
   78.84 + *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2
   78.85 + *    1.2.1                    3    10201  3.1.2.1
   78.86 + *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6
   78.87 + *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1
   78.88 + *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1
   78.89 + *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1
   78.90 + *    1.0.13                  10    10013  10.so.0.1.0.13
   78.91 + *    1.2.2                   12    10202  12.so.0.1.2.2
   78.92 + *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6
   78.93 + *    1.2.3                   12    10203  12.so.0.1.2.3
   78.94 + *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3
   78.95 + *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1
   78.96 + *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1
   78.97 + *    1.0.14                  10    10014  10.so.0.1.0.14
   78.98 + *    1.2.4                   13    10204  12.so.0.1.2.4
   78.99 + *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2
  78.100 + *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3
  78.101 + *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3
  78.102 + *    1.0.15                  10    10015  10.so.0.1.0.15
  78.103 + *    1.2.5                   13    10205  12.so.0.1.2.5
  78.104 + *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4
  78.105 + *    1.0.16                  10    10016  10.so.0.1.0.16
  78.106 + *    1.2.6                   13    10206  12.so.0.1.2.6
  78.107 + *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2
  78.108 + *    1.0.17rc1               10    10017  10.so.0.1.0.17rc1
  78.109 + *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1
  78.110 + *    1.0.17                  10    10017  10.so.0.1.0.17
  78.111 + *    1.2.7                   13    10207  12.so.0.1.2.7
  78.112 + *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5
  78.113 + *    1.0.18rc1-5             10    10018  10.so.0.1.0.18rc1-5
  78.114 + *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5
  78.115 + *    1.0.18                  10    10018  10.so.0.1.0.18
  78.116 + *    1.2.8                   13    10208  12.so.0.1.2.8
  78.117 + *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3
  78.118 + *    1.2.9beta4-11           13    10209  12.so.0.9[.0]
  78.119 + *    1.2.9rc1                13    10209  12.so.0.9[.0]
  78.120 + *    1.2.9                   13    10209  12.so.0.9[.0]
  78.121 + *    1.2.10beta1-8           13    10210  12.so.0.10[.0]
  78.122 + *    1.2.10rc1-3             13    10210  12.so.0.10[.0]
  78.123 + *    1.2.10                  13    10210  12.so.0.10[.0]
  78.124 + *    1.2.11beta1-4           13    10211  12.so.0.11[.0]
  78.125 + *    1.0.19rc1-5             10    10019  10.so.0.19[.0]
  78.126 + *    1.2.11rc1-5             13    10211  12.so.0.11[.0]
  78.127 + *    1.0.19                  10    10019  10.so.0.19[.0]
  78.128 + *    1.2.11                  13    10211  12.so.0.11[.0]
  78.129 + *    1.0.20                  10    10020  10.so.0.20[.0]
  78.130 + *    1.2.12                  13    10212  12.so.0.12[.0]
  78.131 + *    1.2.13beta1             13    10213  12.so.0.13[.0]
  78.132 + *    1.0.21                  10    10021  10.so.0.21[.0]
  78.133 + *    1.2.13                  13    10213  12.so.0.13[.0]
  78.134 + *    1.2.14beta1-2           13    10214  12.so.0.14[.0]
  78.135 + *    1.0.22rc1               10    10022  10.so.0.22[.0]
  78.136 + *    1.2.14rc1               13    10214  12.so.0.14[.0]
  78.137 + *    1.0.22                  10    10022  10.so.0.22[.0]
  78.138 + *    1.2.14                  13    10214  12.so.0.14[.0]
  78.139 + *    1.2.15beta1-6           13    10215  12.so.0.15[.0]
  78.140 + *    1.0.23rc1-5             10    10023  10.so.0.23[.0]
  78.141 + *    1.2.15rc1-5             13    10215  12.so.0.15[.0]
  78.142 + *    1.0.23                  10    10023  10.so.0.23[.0]
  78.143 + *    1.2.15                  13    10215  12.so.0.15[.0]
  78.144 + *    1.2.16beta1-2           13    10216  12.so.0.16[.0]
  78.145 + *    1.2.16rc1               13    10216  12.so.0.16[.0]
  78.146 + *    1.0.24                  10    10024  10.so.0.24[.0]
  78.147 + *    1.2.16                  13    10216  12.so.0.16[.0]
  78.148 + *    1.2.17beta1-2           13    10217  12.so.0.17[.0]
  78.149 + *    1.0.25rc1               10    10025  10.so.0.25[.0]
  78.150 + *    1.2.17rc1-3             13    10217  12.so.0.17[.0]
  78.151 + *    1.0.25                  10    10025  10.so.0.25[.0]
  78.152 + *    1.2.17                  13    10217  12.so.0.17[.0]
  78.153 + *    1.0.26                  10    10026  10.so.0.26[.0]
  78.154 + *    1.2.18                  13    10218  12.so.0.18[.0]
  78.155 + *    1.2.19beta1-31          13    10219  12.so.0.19[.0]
  78.156 + *    1.0.27rc1-6             10    10027  10.so.0.27[.0]
  78.157 + *    1.2.19rc1-6             13    10219  12.so.0.19[.0]
  78.158 + *    1.0.27                  10    10027  10.so.0.27[.0]
  78.159 + *    1.2.19                  13    10219  12.so.0.19[.0]
  78.160 + *    1.2.20beta01-04         13    10220  12.so.0.20[.0]
  78.161 + *    1.0.28rc1-6             10    10028  10.so.0.28[.0]
  78.162 + *    1.2.20rc1-6             13    10220  12.so.0.20[.0]
  78.163 + *    1.0.28                  10    10028  10.so.0.28[.0]
  78.164 + *    1.2.20                  13    10220  12.so.0.20[.0]
  78.165 + *    1.2.21beta1-2           13    10221  12.so.0.21[.0]
  78.166 + *    1.2.21rc1-3             13    10221  12.so.0.21[.0]
  78.167 + *    1.0.29                  10    10029  10.so.0.29[.0]
  78.168 + *    1.2.21                  13    10221  12.so.0.21[.0]
  78.169 + *    1.2.22beta1-4           13    10222  12.so.0.22[.0]
  78.170 + *    1.0.30rc1               10    10030  10.so.0.30[.0]
  78.171 + *    1.2.22rc1               13    10222  12.so.0.22[.0]
  78.172 + *    1.0.30                  10    10030  10.so.0.30[.0]
  78.173 + *    1.2.22                  13    10222  12.so.0.22[.0]
  78.174 + *    1.2.23beta01-05         13    10223  12.so.0.23[.0]
  78.175 + *    1.2.23rc01              13    10223  12.so.0.23[.0]
  78.176 + *    1.2.23                  13    10223  12.so.0.23[.0]
  78.177 + *    1.2.24beta01-02         13    10224  12.so.0.24[.0]
  78.178 + *    1.2.24rc01              13    10224  12.so.0.24[.0]
  78.179 + *    1.2.24                  13    10224  12.so.0.24[.0]
  78.180 + *    1.2.25beta01-06         13    10225  12.so.0.25[.0]
  78.181 + *    1.2.25rc01-02           13    10225  12.so.0.25[.0]
  78.182 + *    1.0.31                  10    10031  10.so.0.31[.0]
  78.183 + *    1.2.25                  13    10225  12.so.0.25[.0]
  78.184 + *    1.2.26beta01-06         13    10226  12.so.0.26[.0]
  78.185 + *    1.2.26rc01              13    10226  12.so.0.26[.0]
  78.186 + *    1.2.26                  13    10226  12.so.0.26[.0]
  78.187 + *    1.0.32                  10    10032  10.so.0.32[.0]
  78.188 + *    1.2.27beta01-06         13    10227  12.so.0.27[.0]
  78.189 + *    1.2.27rc01              13    10227  12.so.0.27[.0]
  78.190 + *    1.0.33                  10    10033  10.so.0.33[.0]
  78.191 + *    1.2.27                  13    10227  12.so.0.27[.0]
  78.192 + *    1.0.34                  10    10034  10.so.0.34[.0]
  78.193 + *    1.2.28                  13    10228  12.so.0.28[.0]
  78.194 + *    1.2.29beta01-03         13    10229  12.so.0.29[.0]
  78.195 + *    1.2.29rc01              13    10229  12.so.0.29[.0]
  78.196 + *    1.0.35                  10    10035  10.so.0.35[.0]
  78.197 + *    1.2.29                  13    10229  12.so.0.29[.0]
  78.198 + *    1.0.37                  10    10037  10.so.0.37[.0]
  78.199 + *    1.2.30beta01-04         13    10230  12.so.0.30[.0]
  78.200 + *    1.0.38rc01-08           10    10038  10.so.0.38[.0]
  78.201 + *    1.2.30rc01-08           13    10230  12.so.0.30[.0]
  78.202 + *    1.0.38                  10    10038  10.so.0.38[.0]
  78.203 + *    1.2.30                  13    10230  12.so.0.30[.0]
  78.204 + *    1.0.39rc01-03           10    10039  10.so.0.39[.0]
  78.205 + *    1.2.31rc01-03           13    10231  12.so.0.31[.0]
  78.206 + *    1.0.39                  10    10039  10.so.0.39[.0]
  78.207 + *    1.2.31                  13    10231  12.so.0.31[.0]
  78.208 + *    1.2.32beta01-02         13    10232  12.so.0.32[.0]
  78.209 + *    1.0.40rc01              10    10040  10.so.0.40[.0]
  78.210 + *    1.2.32rc01              13    10232  12.so.0.32[.0]
  78.211 + *    1.0.40                  10    10040  10.so.0.40[.0]
  78.212 + *    1.2.32                  13    10232  12.so.0.32[.0]
  78.213 + *    1.2.33beta01-02         13    10233  12.so.0.33[.0]
  78.214 + *    1.2.33rc01-02           13    10233  12.so.0.33[.0]
  78.215 + *    1.0.41rc01              10    10041  10.so.0.41[.0]
  78.216 + *    1.2.33                  13    10233  12.so.0.33[.0]
  78.217 + *    1.0.41                  10    10041  10.so.0.41[.0]
  78.218 + *
  78.219 + *    Henceforth the source version will match the shared-library major
  78.220 + *    and minor numbers; the shared-library major version number will be
  78.221 + *    used for changes in backward compatibility, as it is intended.  The
  78.222 + *    PNG_LIBPNG_VER macro, which is not used within libpng but is available
  78.223 + *    for applications, is an unsigned integer of the form xyyzz corresponding
  78.224 + *    to the source version x.y.z (leading zeros in y and z).  Beta versions
  78.225 + *    were given the previous public release number plus a letter, until
  78.226 + *    version 1.0.6j; from then on they were given the upcoming public
  78.227 + *    release number plus "betaNN" or "rcNN".
  78.228 + *
  78.229 + *    Binary incompatibility exists only when applications make direct access
  78.230 + *    to the info_ptr or png_ptr members through png.h, and the compiled
  78.231 + *    application is loaded with a different version of the library.
  78.232 + *
  78.233 + *    DLLNUM will change each time there are forward or backward changes
  78.234 + *    in binary compatibility (e.g., when a new feature is added).
  78.235 + *
  78.236 + * See libpng.txt or libpng.3 for more information.  The PNG specification
  78.237 + * is available as a W3C Recommendation and as an ISO Specification,
  78.238 + * <http://www.w3.org/TR/2003/REC-PNG-20031110/
  78.239 + */
  78.240 +
  78.241 +/*
  78.242 + * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
  78.243 + *
  78.244 + * If you modify libpng you may insert additional notices immediately following
  78.245 + * this sentence.
  78.246 + *
  78.247 + * libpng versions 1.2.6, August 15, 2004, through 1.2.33, October 31, 2008, are
  78.248 + * Copyright (c) 2004, 2006-2008 Glenn Randers-Pehrson, and are
  78.249 + * distributed according to the same disclaimer and license as libpng-1.2.5
  78.250 + * with the following individual added to the list of Contributing Authors:
  78.251 + *
  78.252 + *    Cosmin Truta
  78.253 + *
  78.254 + * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are
  78.255 + * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
  78.256 + * distributed according to the same disclaimer and license as libpng-1.0.6
  78.257 + * with the following individuals added to the list of Contributing Authors:
  78.258 + *
  78.259 + *    Simon-Pierre Cadieux
  78.260 + *    Eric S. Raymond
  78.261 + *    Gilles Vollant
  78.262 + *
  78.263 + * and with the following additions to the disclaimer:
  78.264 + *
  78.265 + *    There is no warranty against interference with your enjoyment of the
  78.266 + *    library or against infringement.  There is no warranty that our
  78.267 + *    efforts or the library will fulfill any of your particular purposes
  78.268 + *    or needs.  This library is provided with all faults, and the entire
  78.269 + *    risk of satisfactory quality, performance, accuracy, and effort is with
  78.270 + *    the user.
  78.271 + *
  78.272 + * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
  78.273 + * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are
  78.274 + * distributed according to the same disclaimer and license as libpng-0.96,
  78.275 + * with the following individuals added to the list of Contributing Authors:
  78.276 + *
  78.277 + *    Tom Lane
  78.278 + *    Glenn Randers-Pehrson
  78.279 + *    Willem van Schaik
  78.280 + *
  78.281 + * libpng versions 0.89, June 1996, through 0.96, May 1997, are
  78.282 + * Copyright (c) 1996, 1997 Andreas Dilger
  78.283 + * Distributed according to the same disclaimer and license as libpng-0.88,
  78.284 + * with the following individuals added to the list of Contributing Authors:
  78.285 + *
  78.286 + *    John Bowler
  78.287 + *    Kevin Bracey
  78.288 + *    Sam Bushell
  78.289 + *    Magnus Holmgren
  78.290 + *    Greg Roelofs
  78.291 + *    Tom Tanner
  78.292 + *
  78.293 + * libpng versions 0.5, May 1995, through 0.88, January 1996, are
  78.294 + * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  78.295 + *
  78.296 + * For the purposes of this copyright and license, "Contributing Authors"
  78.297 + * is defined as the following set of individuals:
  78.298 + *
  78.299 + *    Andreas Dilger
  78.300 + *    Dave Martindale
  78.301 + *    Guy Eric Schalnat
  78.302 + *    Paul Schmidt
  78.303 + *    Tim Wegner
  78.304 + *
  78.305 + * The PNG Reference Library is supplied "AS IS".  The Contributing Authors
  78.306 + * and Group 42, Inc. disclaim all warranties, expressed or implied,
  78.307 + * including, without limitation, the warranties of merchantability and of
  78.308 + * fitness for any purpose.  The Contributing Authors and Group 42, Inc.
  78.309 + * assume no liability for direct, indirect, incidental, special, exemplary,
  78.310 + * or consequential damages, which may result from the use of the PNG
  78.311 + * Reference Library, even if advised of the possibility of such damage.
  78.312 + *
  78.313 + * Permission is hereby granted to use, copy, modify, and distribute this
  78.314 + * source code, or portions hereof, for any purpose, without fee, subject
  78.315 + * to the following restrictions:
  78.316 + *
  78.317 + * 1. The origin of this source code must not be misrepresented.
  78.318 + *
  78.319 + * 2. Altered versions must be plainly marked as such and
  78.320 + * must not be misrepresented as being the original source.
  78.321 + *
  78.322 + * 3. This Copyright notice may not be removed or altered from
  78.323 + *    any source or altered source distribution.
  78.324 + *
  78.325 + * The Contributing Authors and Group 42, Inc. specifically permit, without
  78.326 + * fee, and encourage the use of this source code as a component to
  78.327 + * supporting the PNG file format in commercial products.  If you use this
  78.328 + * source code in a product, acknowledgment is not required but would be
  78.329 + * appreciated.
  78.330 + */
  78.331 +
  78.332 +/*
  78.333 + * A "png_get_copyright" function is available, for convenient use in "about"
  78.334 + * boxes and the like:
  78.335 + *
  78.336 + * printf("%s",png_get_copyright(NULL));
  78.337 + *
  78.338 + * Also, the PNG logo (in PNG format, of course) is supplied in the
  78.339 + * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
  78.340 + */
  78.341 +
  78.342 +/*
  78.343 + * Libpng is OSI Certified Open Source Software.  OSI Certified is a
  78.344 + * certification mark of the Open Source Initiative.
  78.345 + */
  78.346 +
  78.347 +/*
  78.348 + * The contributing authors would like to thank all those who helped
  78.349 + * with testing, bug fixes, and patience.  This wouldn't have been
  78.350 + * possible without all of you.
  78.351 + *
  78.352 + * Thanks to Frank J. T. Wojcik for helping with the documentation.
  78.353 + */
  78.354 +
  78.355 +/*
  78.356 + * Y2K compliance in libpng:
  78.357 + * =========================
  78.358 + *
  78.359 + *    October 31, 2008
  78.360 + *
  78.361 + *    Since the PNG Development group is an ad-hoc body, we can't make
  78.362 + *    an official declaration.
  78.363 + *
  78.364 + *    This is your unofficial assurance that libpng from version 0.71 and
  78.365 + *    upward through 1.2.33 are Y2K compliant.  It is my belief that earlier
  78.366 + *    versions were also Y2K compliant.
  78.367 + *
  78.368 + *    Libpng only has three year fields.  One is a 2-byte unsigned integer
  78.369 + *    that will hold years up to 65535.  The other two hold the date in text
  78.370 + *    format, and will hold years up to 9999.
  78.371 + *
  78.372 + *    The integer is
  78.373 + *        "png_uint_16 year" in png_time_struct.
  78.374 + *
  78.375 + *    The strings are
  78.376 + *        "png_charp time_buffer" in png_struct and
  78.377 + *        "near_time_buffer", which is a local character string in png.c.
  78.378 + *
  78.379 + *    There are seven time-related functions:
  78.380 + *        png.c: png_convert_to_rfc_1123() in png.c
  78.381 + *          (formerly png_convert_to_rfc_1152() in error)
  78.382 + *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
  78.383 + *        png_convert_from_time_t() in pngwrite.c
  78.384 + *        png_get_tIME() in pngget.c
  78.385 + *        png_handle_tIME() in pngrutil.c, called in pngread.c
  78.386 + *        png_set_tIME() in pngset.c
  78.387 + *        png_write_tIME() in pngwutil.c, called in pngwrite.c
  78.388 + *
  78.389 + *    All handle dates properly in a Y2K environment.  The
  78.390 + *    png_convert_from_time_t() function calls gmtime() to convert from system
  78.391 + *    clock time, which returns (year - 1900), which we properly convert to
  78.392 + *    the full 4-digit year.  There is a possibility that applications using
  78.393 + *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
  78.394 + *    function, or that they are incorrectly passing only a 2-digit year
  78.395 + *    instead of "year - 1900" into the png_convert_from_struct_tm() function,
  78.396 + *    but this is not under our control.  The libpng documentation has always
  78.397 + *    stated that it works with 4-digit years, and the APIs have been
  78.398 + *    documented as such.
  78.399 + *
  78.400 + *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
  78.401 + *    integer to hold the year, and can hold years as large as 65535.
  78.402 + *
  78.403 + *    zlib, upon which libpng depends, is also Y2K compliant.  It contains
  78.404 + *    no date-related code.
  78.405 + *
  78.406 + *       Glenn Randers-Pehrson
  78.407 + *       libpng maintainer
  78.408 + *       PNG Development Group
  78.409 + */
  78.410 +
  78.411 +#ifndef PNG_H
  78.412 +#define PNG_H
  78.413 +
  78.414 +/* This is not the place to learn how to use libpng.  The file libpng.txt
  78.415 + * describes how to use libpng, and the file example.c summarizes it
  78.416 + * with some code on which to build.  This file is useful for looking
  78.417 + * at the actual function definitions and structure components.
  78.418 + */
  78.419 +
  78.420 +/* Version information for png.h - this should match the version in png.c */
  78.421 +#define PNG_LIBPNG_VER_STRING "1.2.33"
  78.422 +#define PNG_HEADER_VERSION_STRING \
  78.423 +   " libpng version 1.2.33 - October 31, 2008\n"
  78.424 +
  78.425 +#define PNG_LIBPNG_VER_SONUM   0
  78.426 +#define PNG_LIBPNG_VER_DLLNUM  13
  78.427 +
  78.428 +/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
  78.429 +#define PNG_LIBPNG_VER_MAJOR   1
  78.430 +#define PNG_LIBPNG_VER_MINOR   2
  78.431 +#define PNG_LIBPNG_VER_RELEASE 33
  78.432 +/* This should match the numeric part of the final component of
  78.433 + * PNG_LIBPNG_VER_STRING, omitting any leading zero: */
  78.434 +
  78.435 +#define PNG_LIBPNG_VER_BUILD  0
  78.436 +
  78.437 +/* Release Status */
  78.438 +#define PNG_LIBPNG_BUILD_ALPHA    1
  78.439 +#define PNG_LIBPNG_BUILD_BETA     2
  78.440 +#define PNG_LIBPNG_BUILD_RC       3
  78.441 +#define PNG_LIBPNG_BUILD_STABLE   4
  78.442 +#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7
  78.443 +  
  78.444 +/* Release-Specific Flags */
  78.445 +#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with
  78.446 +                                       PNG_LIBPNG_BUILD_STABLE only */
  78.447 +#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
  78.448 +                                       PNG_LIBPNG_BUILD_SPECIAL */
  78.449 +#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
  78.450 +                                       PNG_LIBPNG_BUILD_PRIVATE */
  78.451 +
  78.452 +#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE
  78.453 +
  78.454 +/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
  78.455 + * We must not include leading zeros.
  78.456 + * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
  78.457 + * version 1.0.0 was mis-numbered 100 instead of 10000).  From
  78.458 + * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release */
  78.459 +#define PNG_LIBPNG_VER 10233 /* 1.2.33 */
  78.460 +
  78.461 +#ifndef PNG_VERSION_INFO_ONLY
  78.462 +/* include the compression library's header */
  78.463 +#include "zlib.h"
  78.464 +#endif
  78.465 +
  78.466 +/* include all user configurable info, including optional assembler routines */
  78.467 +#include "pngconf.h"
  78.468 +
  78.469 +/*
  78.470 + * Added at libpng-1.2.8 */
  78.471 +/* Ref MSDN: Private as priority over Special
  78.472 + * VS_FF_PRIVATEBUILD File *was not* built using standard release
  78.473 + * procedures. If this value is given, the StringFileInfo block must
  78.474 + * contain a PrivateBuild string. 
  78.475 + *
  78.476 + * VS_FF_SPECIALBUILD File *was* built by the original company using
  78.477 + * standard release procedures but is a variation of the standard
  78.478 + * file of the same version number. If this value is given, the
  78.479 + * StringFileInfo block must contain a SpecialBuild string. 
  78.480 + */
  78.481 +
  78.482 +#if defined(PNG_USER_PRIVATEBUILD)
  78.483 +#  define PNG_LIBPNG_BUILD_TYPE \
  78.484 +          (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
  78.485 +#else
  78.486 +#  if defined(PNG_LIBPNG_SPECIALBUILD)
  78.487 +#    define PNG_LIBPNG_BUILD_TYPE \
  78.488 +            (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
  78.489 +#  else
  78.490 +#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
  78.491 +#  endif
  78.492 +#endif
  78.493 +
  78.494 +#ifndef PNG_VERSION_INFO_ONLY
  78.495 +
  78.496 +/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
  78.497 +#ifdef __cplusplus
  78.498 +extern "C" {
  78.499 +#endif /* __cplusplus */
  78.500 +
  78.501 +/* This file is arranged in several sections.  The first section contains
  78.502 + * structure and type definitions.  The second section contains the external
  78.503 + * library functions, while the third has the internal library functions,
  78.504 + * which applications aren't expected to use directly.
  78.505 + */
  78.506 +
  78.507 +#ifndef PNG_NO_TYPECAST_NULL
  78.508 +#define int_p_NULL                (int *)NULL
  78.509 +#define png_bytep_NULL            (png_bytep)NULL
  78.510 +#define png_bytepp_NULL           (png_bytepp)NULL
  78.511 +#define png_doublep_NULL          (png_doublep)NULL
  78.512 +#define png_error_ptr_NULL        (png_error_ptr)NULL
  78.513 +#define png_flush_ptr_NULL        (png_flush_ptr)NULL
  78.514 +#define png_free_ptr_NULL         (png_free_ptr)NULL
  78.515 +#define png_infopp_NULL           (png_infopp)NULL
  78.516 +#define png_malloc_ptr_NULL       (png_malloc_ptr)NULL
  78.517 +#define png_read_status_ptr_NULL  (png_read_status_ptr)NULL
  78.518 +#define png_rw_ptr_NULL           (png_rw_ptr)NULL
  78.519 +#define png_structp_NULL          (png_structp)NULL
  78.520 +#define png_uint_16p_NULL         (png_uint_16p)NULL
  78.521 +#define png_voidp_NULL            (png_voidp)NULL
  78.522 +#define png_write_status_ptr_NULL (png_write_status_ptr)NULL
  78.523 +#else
  78.524 +#define int_p_NULL                NULL
  78.525 +#define png_bytep_NULL            NULL
  78.526 +#define png_bytepp_NULL           NULL
  78.527 +#define png_doublep_NULL          NULL
  78.528 +#define png_error_ptr_NULL        NULL
  78.529 +#define png_flush_ptr_NULL        NULL
  78.530 +#define png_free_ptr_NULL         NULL
  78.531 +#define png_infopp_NULL           NULL
  78.532 +#define png_malloc_ptr_NULL       NULL
  78.533 +#define png_read_status_ptr_NULL  NULL
  78.534 +#define png_rw_ptr_NULL           NULL
  78.535 +#define png_structp_NULL          NULL
  78.536 +#define png_uint_16p_NULL         NULL
  78.537 +#define png_voidp_NULL            NULL
  78.538 +#define png_write_status_ptr_NULL NULL
  78.539 +#endif
  78.540 +
  78.541 +/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
  78.542 +#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
  78.543 +/* Version information for C files, stored in png.c.  This had better match
  78.544 + * the version above.
  78.545 + */
  78.546 +#ifdef PNG_USE_GLOBAL_ARRAYS
  78.547 +PNG_EXPORT_VAR (PNG_CONST char) png_libpng_ver[18];
  78.548 +  /* need room for 99.99.99beta99z */
  78.549 +#else
  78.550 +#define png_libpng_ver png_get_header_ver(NULL)
  78.551 +#endif
  78.552 +
  78.553 +#ifdef PNG_USE_GLOBAL_ARRAYS
  78.554 +/* This was removed in version 1.0.5c */
  78.555 +/* Structures to facilitate easy interlacing.  See png.c for more details */
  78.556 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_start[7];
  78.557 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_inc[7];
  78.558 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_ystart[7];
  78.559 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_yinc[7];
  78.560 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_mask[7];
  78.561 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_dsp_mask[7];
  78.562 +/* This isn't currently used.  If you need it, see png.c for more details.
  78.563 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_height[7];
  78.564 +*/
  78.565 +#endif
  78.566 +
  78.567 +#endif /* PNG_NO_EXTERN */
  78.568 +
  78.569 +/* Three color definitions.  The order of the red, green, and blue, (and the
  78.570 + * exact size) is not important, although the size of the fields need to
  78.571 + * be png_byte or png_uint_16 (as defined below).
  78.572 + */
  78.573 +typedef struct png_color_struct
  78.574 +{
  78.575 +   png_byte red;
  78.576 +   png_byte green;
  78.577 +   png_byte blue;
  78.578 +} png_color;
  78.579 +typedef png_color FAR * png_colorp;
  78.580 +typedef png_color FAR * FAR * png_colorpp;
  78.581 +
  78.582 +typedef struct png_color_16_struct
  78.583 +{
  78.584 +   png_byte index;    /* used for palette files */
  78.585 +   png_uint_16 red;   /* for use in red green blue files */
  78.586 +   png_uint_16 green;
  78.587 +   png_uint_16 blue;
  78.588 +   png_uint_16 gray;  /* for use in grayscale files */
  78.589 +} png_color_16;
  78.590 +typedef png_color_16 FAR * png_color_16p;
  78.591 +typedef png_color_16 FAR * FAR * png_color_16pp;
  78.592 +
  78.593 +typedef struct png_color_8_struct
  78.594 +{
  78.595 +   png_byte red;   /* for use in red green blue files */
  78.596 +   png_byte green;
  78.597 +   png_byte blue;
  78.598 +   png_byte gray;  /* for use in grayscale files */
  78.599 +   png_byte alpha; /* for alpha channel files */
  78.600 +} png_color_8;
  78.601 +typedef png_color_8 FAR * png_color_8p;
  78.602 +typedef png_color_8 FAR * FAR * png_color_8pp;
  78.603 +
  78.604 +/*
  78.605 + * The following two structures are used for the in-core representation
  78.606 + * of sPLT chunks.
  78.607 + */
  78.608 +typedef struct png_sPLT_entry_struct
  78.609 +{
  78.610 +   png_uint_16 red;
  78.611 +   png_uint_16 green;
  78.612 +   png_uint_16 blue;
  78.613 +   png_uint_16 alpha;
  78.614 +   png_uint_16 frequency;
  78.615 +} png_sPLT_entry;
  78.616 +typedef png_sPLT_entry FAR * png_sPLT_entryp;
  78.617 +typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;
  78.618 +
  78.619 +/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples
  78.620 + *  occupy the LSB of their respective members, and the MSB of each member
  78.621 + *  is zero-filled.  The frequency member always occupies the full 16 bits.
  78.622 + */
  78.623 +
  78.624 +typedef struct png_sPLT_struct
  78.625 +{
  78.626 +   png_charp name;           /* palette name */
  78.627 +   png_byte depth;           /* depth of palette samples */
  78.628 +   png_sPLT_entryp entries;  /* palette entries */
  78.629 +   png_int_32 nentries;      /* number of palette entries */
  78.630 +} png_sPLT_t;
  78.631 +typedef png_sPLT_t FAR * png_sPLT_tp;
  78.632 +typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;
  78.633 +
  78.634 +#ifdef PNG_TEXT_SUPPORTED
  78.635 +/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,
  78.636 + * and whether that contents is compressed or not.  The "key" field
  78.637 + * points to a regular zero-terminated C string.  The "text", "lang", and
  78.638 + * "lang_key" fields can be regular C strings, empty strings, or NULL pointers.
  78.639 + * However, the * structure returned by png_get_text() will always contain
  78.640 + * regular zero-terminated C strings (possibly empty), never NULL pointers,
  78.641 + * so they can be safely used in printf() and other string-handling functions.
  78.642 + */
  78.643 +typedef struct png_text_struct
  78.644 +{
  78.645 +   int  compression;       /* compression value:
  78.646 +                             -1: tEXt, none
  78.647 +                              0: zTXt, deflate
  78.648 +                              1: iTXt, none
  78.649 +                              2: iTXt, deflate  */
  78.650 +   png_charp key;          /* keyword, 1-79 character description of "text" */
  78.651 +   png_charp text;         /* comment, may be an empty string (ie "")
  78.652 +                              or a NULL pointer */
  78.653 +   png_size_t text_length; /* length of the text string */
  78.654 +#ifdef PNG_iTXt_SUPPORTED
  78.655 +   png_size_t itxt_length; /* length of the itxt string */
  78.656 +   png_charp lang;         /* language code, 0-79 characters
  78.657 +                              or a NULL pointer */
  78.658 +   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
  78.659 +                              chars or a NULL pointer */
  78.660 +#endif
  78.661 +} png_text;
  78.662 +typedef png_text FAR * png_textp;
  78.663 +typedef png_text FAR * FAR * png_textpp;
  78.664 +#endif
  78.665 +
  78.666 +/* Supported compression types for text in PNG files (tEXt, and zTXt).
  78.667 + * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */
  78.668 +#define PNG_TEXT_COMPRESSION_NONE_WR -3
  78.669 +#define PNG_TEXT_COMPRESSION_zTXt_WR -2
  78.670 +#define PNG_TEXT_COMPRESSION_NONE    -1
  78.671 +#define PNG_TEXT_COMPRESSION_zTXt     0
  78.672 +#define PNG_ITXT_COMPRESSION_NONE     1
  78.673 +#define PNG_ITXT_COMPRESSION_zTXt     2
  78.674 +#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */
  78.675 +
  78.676 +/* png_time is a way to hold the time in an machine independent way.
  78.677 + * Two conversions are provided, both from time_t and struct tm.  There
  78.678 + * is no portable way to convert to either of these structures, as far
  78.679 + * as I know.  If you know of a portable way, send it to me.  As a side
  78.680 + * note - PNG has always been Year 2000 compliant!
  78.681 + */
  78.682 +typedef struct png_time_struct
  78.683 +{
  78.684 +   png_uint_16 year; /* full year, as in, 1995 */
  78.685 +   png_byte month;   /* month of year, 1 - 12 */
  78.686 +   png_byte day;     /* day of month, 1 - 31 */
  78.687 +   png_byte hour;    /* hour of day, 0 - 23 */
  78.688 +   png_byte minute;  /* minute of hour, 0 - 59 */
  78.689 +   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
  78.690 +} png_time;
  78.691 +typedef png_time FAR * png_timep;
  78.692 +typedef png_time FAR * FAR * png_timepp;
  78.693 +
  78.694 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  78.695 +/* png_unknown_chunk is a structure to hold queued chunks for which there is
  78.696 + * no specific support.  The idea is that we can use this to queue
  78.697 + * up private chunks for output even though the library doesn't actually
  78.698 + * know about their semantics.
  78.699 + */
  78.700 +#define PNG_CHUNK_NAME_LENGTH 5
  78.701 +typedef struct png_unknown_chunk_t
  78.702 +{
  78.703 +    png_byte name[PNG_CHUNK_NAME_LENGTH];
  78.704 +    png_byte *data;
  78.705 +    png_size_t size;
  78.706 +
  78.707 +    /* libpng-using applications should NOT directly modify this byte. */
  78.708 +    png_byte location; /* mode of operation at read time */
  78.709 +}
  78.710 +png_unknown_chunk;
  78.711 +typedef png_unknown_chunk FAR * png_unknown_chunkp;
  78.712 +typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
  78.713 +#endif
  78.714 +
  78.715 +/* png_info is a structure that holds the information in a PNG file so
  78.716 + * that the application can find out the characteristics of the image.
  78.717 + * If you are reading the file, this structure will tell you what is
  78.718 + * in the PNG file.  If you are writing the file, fill in the information
  78.719 + * you want to put into the PNG file, then call png_write_info().
  78.720 + * The names chosen should be very close to the PNG specification, so
  78.721 + * consult that document for information about the meaning of each field.
  78.722 + *
  78.723 + * With libpng < 0.95, it was only possible to directly set and read the
  78.724 + * the values in the png_info_struct, which meant that the contents and
  78.725 + * order of the values had to remain fixed.  With libpng 0.95 and later,
  78.726 + * however, there are now functions that abstract the contents of
  78.727 + * png_info_struct from the application, so this makes it easier to use
  78.728 + * libpng with dynamic libraries, and even makes it possible to use
  78.729 + * libraries that don't have all of the libpng ancillary chunk-handing
  78.730 + * functionality.
  78.731 + *
  78.732 + * In any case, the order of the parameters in png_info_struct should NOT
  78.733 + * be changed for as long as possible to keep compatibility with applications
  78.734 + * that use the old direct-access method with png_info_struct.
  78.735 + *
  78.736 + * The following members may have allocated storage attached that should be
  78.737 + * cleaned up before the structure is discarded: palette, trans, text,
  78.738 + * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile,
  78.739 + * splt_palettes, scal_unit, row_pointers, and unknowns.   By default, these
  78.740 + * are automatically freed when the info structure is deallocated, if they were
  78.741 + * allocated internally by libpng.  This behavior can be changed by means
  78.742 + * of the png_data_freer() function.
  78.743 + *
  78.744 + * More allocation details: all the chunk-reading functions that
  78.745 + * change these members go through the corresponding png_set_*
  78.746 + * functions.  A function to clear these members is available: see
  78.747 + * png_free_data().  The png_set_* functions do not depend on being
  78.748 + * able to point info structure members to any of the storage they are
  78.749 + * passed (they make their own copies), EXCEPT that the png_set_text
  78.750 + * functions use the same storage passed to them in the text_ptr or
  78.751 + * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns
  78.752 + * functions do not make their own copies.
  78.753 + */
  78.754 +typedef struct png_info_struct
  78.755 +{
  78.756 +   /* the following are necessary for every PNG file */
  78.757 +   png_uint_32 width;       /* width of image in pixels (from IHDR) */
  78.758 +   png_uint_32 height;      /* height of image in pixels (from IHDR) */
  78.759 +   png_uint_32 valid;       /* valid chunk data (see PNG_INFO_ below) */
  78.760 +   png_uint_32 rowbytes;    /* bytes needed to hold an untransformed row */
  78.761 +   png_colorp palette;      /* array of color values (valid & PNG_INFO_PLTE) */
  78.762 +   png_uint_16 num_palette; /* number of color entries in "palette" (PLTE) */
  78.763 +   png_uint_16 num_trans;   /* number of transparent palette color (tRNS) */
  78.764 +   png_byte bit_depth;      /* 1, 2, 4, 8, or 16 bits/channel (from IHDR) */
  78.765 +   png_byte color_type;     /* see PNG_COLOR_TYPE_ below (from IHDR) */
  78.766 +   /* The following three should have been named *_method not *_type */
  78.767 +   png_byte compression_type; /* must be PNG_COMPRESSION_TYPE_BASE (IHDR) */
  78.768 +   png_byte filter_type;    /* must be PNG_FILTER_TYPE_BASE (from IHDR) */
  78.769 +   png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
  78.770 +
  78.771 +   /* The following is informational only on read, and not used on writes. */
  78.772 +   png_byte channels;       /* number of data channels per pixel (1, 2, 3, 4) */
  78.773 +   png_byte pixel_depth;    /* number of bits per pixel */
  78.774 +   png_byte spare_byte;     /* to align the data, and for future use */
  78.775 +   png_byte signature[8];   /* magic bytes read by libpng from start of file */
  78.776 +
  78.777 +   /* The rest of the data is optional.  If you are reading, check the
  78.778 +    * valid field to see if the information in these are valid.  If you
  78.779 +    * are writing, set the valid field to those chunks you want written,
  78.780 +    * and initialize the appropriate fields below.
  78.781 +    */
  78.782 +
  78.783 +#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  78.784 +   /* The gAMA chunk describes the gamma characteristics of the system
  78.785 +    * on which the image was created, normally in the range [1.0, 2.5].
  78.786 +    * Data is valid if (valid & PNG_INFO_gAMA) is non-zero.
  78.787 +    */
  78.788 +   float gamma; /* gamma value of image, if (valid & PNG_INFO_gAMA) */
  78.789 +#endif
  78.790 +
  78.791 +#if defined(PNG_sRGB_SUPPORTED)
  78.792 +    /* GR-P, 0.96a */
  78.793 +    /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */
  78.794 +   png_byte srgb_intent; /* sRGB rendering intent [0, 1, 2, or 3] */
  78.795 +#endif
  78.796 +
  78.797 +#if defined(PNG_TEXT_SUPPORTED)
  78.798 +   /* The tEXt, and zTXt chunks contain human-readable textual data in
  78.799 +    * uncompressed, compressed, and optionally compressed forms, respectively.
  78.800 +    * The data in "text" is an array of pointers to uncompressed,
  78.801 +    * null-terminated C strings. Each chunk has a keyword that describes the
  78.802 +    * textual data contained in that chunk.  Keywords are not required to be
  78.803 +    * unique, and the text string may be empty.  Any number of text chunks may
  78.804 +    * be in an image.
  78.805 +    */
  78.806 +   int num_text; /* number of comments read/to write */
  78.807 +   int max_text; /* current size of text array */
  78.808 +   png_textp text; /* array of comments read/to write */
  78.809 +#endif /* PNG_TEXT_SUPPORTED */
  78.810 +
  78.811 +#if defined(PNG_tIME_SUPPORTED)
  78.812 +   /* The tIME chunk holds the last time the displayed image data was
  78.813 +    * modified.  See the png_time struct for the contents of this struct.
  78.814 +    */
  78.815 +   png_time mod_time;
  78.816 +#endif
  78.817 +
  78.818 +#if defined(PNG_sBIT_SUPPORTED)
  78.819 +   /* The sBIT chunk specifies the number of significant high-order bits
  78.820 +    * in the pixel data.  Values are in the range [1, bit_depth], and are
  78.821 +    * only specified for the channels in the pixel data.  The contents of
  78.822 +    * the low-order bits is not specified.  Data is valid if
  78.823 +    * (valid & PNG_INFO_sBIT) is non-zero.
  78.824 +    */
  78.825 +   png_color_8 sig_bit; /* significant bits in color channels */
  78.826 +#endif
  78.827 +
  78.828 +#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \
  78.829 +defined(PNG_READ_BACKGROUND_SUPPORTED)
  78.830 +   /* The tRNS chunk supplies transparency data for paletted images and
  78.831 +    * other image types that don't need a full alpha channel.  There are
  78.832 +    * "num_trans" transparency values for a paletted image, stored in the
  78.833 +    * same order as the palette colors, starting from index 0.  Values
  78.834 +    * for the data are in the range [0, 255], ranging from fully transparent
  78.835 +    * to fully opaque, respectively.  For non-paletted images, there is a
  78.836 +    * single color specified that should be treated as fully transparent.
  78.837 +    * Data is valid if (valid & PNG_INFO_tRNS) is non-zero.
  78.838 +    */
  78.839 +   png_bytep trans; /* transparent values for paletted image */
  78.840 +   png_color_16 trans_values; /* transparent color for non-palette image */
  78.841 +#endif
  78.842 +
  78.843 +#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  78.844 +   /* The bKGD chunk gives the suggested image background color if the
  78.845 +    * display program does not have its own background color and the image
  78.846 +    * is needs to composited onto a background before display.  The colors
  78.847 +    * in "background" are normally in the same color space/depth as the
  78.848 +    * pixel data.  Data is valid if (valid & PNG_INFO_bKGD) is non-zero.
  78.849 +    */
  78.850 +   png_color_16 background;
  78.851 +#endif
  78.852 +
  78.853 +#if defined(PNG_oFFs_SUPPORTED)
  78.854 +   /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards
  78.855 +    * and downwards from the top-left corner of the display, page, or other
  78.856 +    * application-specific co-ordinate space.  See the PNG_OFFSET_ defines
  78.857 +    * below for the unit types.  Valid if (valid & PNG_INFO_oFFs) non-zero.
  78.858 +    */
  78.859 +   png_int_32 x_offset; /* x offset on page */
  78.860 +   png_int_32 y_offset; /* y offset on page */
  78.861 +   png_byte offset_unit_type; /* offset units type */
  78.862 +#endif
  78.863 +
  78.864 +#if defined(PNG_pHYs_SUPPORTED)
  78.865 +   /* The pHYs chunk gives the physical pixel density of the image for
  78.866 +    * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_
  78.867 +    * defines below).  Data is valid if (valid & PNG_INFO_pHYs) is non-zero.
  78.868 +    */
  78.869 +   png_uint_32 x_pixels_per_unit; /* horizontal pixel density */
  78.870 +   png_uint_32 y_pixels_per_unit; /* vertical pixel density */
  78.871 +   png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */
  78.872 +#endif
  78.873 +
  78.874 +#if defined(PNG_hIST_SUPPORTED)
  78.875 +   /* The hIST chunk contains the relative frequency or importance of the
  78.876 +    * various palette entries, so that a viewer can intelligently select a
  78.877 +    * reduced-color palette, if required.  Data is an array of "num_palette"
  78.878 +    * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST)
  78.879 +    * is non-zero.
  78.880 +    */
  78.881 +   png_uint_16p hist;
  78.882 +#endif
  78.883 +
  78.884 +#ifdef PNG_cHRM_SUPPORTED
  78.885 +   /* The cHRM chunk describes the CIE color characteristics of the monitor
  78.886 +    * on which the PNG was created.  This data allows the viewer to do gamut
  78.887 +    * mapping of the input image to ensure that the viewer sees the same
  78.888 +    * colors in the image as the creator.  Values are in the range
  78.889 +    * [0.0, 0.8].  Data valid if (valid & PNG_INFO_cHRM) non-zero.
  78.890 +    */
  78.891 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  78.892 +   float x_white;
  78.893 +   float y_white;
  78.894 +   float x_red;
  78.895 +   float y_red;
  78.896 +   float x_green;
  78.897 +   float y_green;
  78.898 +   float x_blue;
  78.899 +   float y_blue;
  78.900 +#endif
  78.901 +#endif
  78.902 +
  78.903 +#if defined(PNG_pCAL_SUPPORTED)
  78.904 +   /* The pCAL chunk describes a transformation between the stored pixel
  78.905 +    * values and original physical data values used to create the image.
  78.906 +    * The integer range [0, 2^bit_depth - 1] maps to the floating-point
  78.907 +    * range given by [pcal_X0, pcal_X1], and are further transformed by a
  78.908 +    * (possibly non-linear) transformation function given by "pcal_type"
  78.909 +    * and "pcal_params" into "pcal_units".  Please see the PNG_EQUATION_
  78.910 +    * defines below, and the PNG-Group's PNG extensions document for a
  78.911 +    * complete description of the transformations and how they should be
  78.912 +    * implemented, and for a description of the ASCII parameter strings.
  78.913 +    * Data values are valid if (valid & PNG_INFO_pCAL) non-zero.
  78.914 +    */
  78.915 +   png_charp pcal_purpose;  /* pCAL chunk description string */
  78.916 +   png_int_32 pcal_X0;      /* minimum value */
  78.917 +   png_int_32 pcal_X1;      /* maximum value */
  78.918 +   png_charp pcal_units;    /* Latin-1 string giving physical units */
  78.919 +   png_charpp pcal_params;  /* ASCII strings containing parameter values */
  78.920 +   png_byte pcal_type;      /* equation type (see PNG_EQUATION_ below) */
  78.921 +   png_byte pcal_nparams;   /* number of parameters given in pcal_params */
  78.922 +#endif
  78.923 +
  78.924 +/* New members added in libpng-1.0.6 */
  78.925 +#ifdef PNG_FREE_ME_SUPPORTED
  78.926 +   png_uint_32 free_me;     /* flags items libpng is responsible for freeing */
  78.927 +#endif
  78.928 +
  78.929 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  78.930 +   /* storage for unknown chunks that the library doesn't recognize. */
  78.931 +   png_unknown_chunkp unknown_chunks;
  78.932 +   png_size_t unknown_chunks_num;
  78.933 +#endif
  78.934 +
  78.935 +#if defined(PNG_iCCP_SUPPORTED)
  78.936 +   /* iCCP chunk data. */
  78.937 +   png_charp iccp_name;     /* profile name */
  78.938 +   png_charp iccp_profile;  /* International Color Consortium profile data */
  78.939 +                            /* Note to maintainer: should be png_bytep */
  78.940 +   png_uint_32 iccp_proflen;  /* ICC profile data length */
  78.941 +   png_byte iccp_compression; /* Always zero */
  78.942 +#endif
  78.943 +
  78.944 +#if defined(PNG_sPLT_SUPPORTED)
  78.945 +   /* data on sPLT chunks (there may be more than one). */
  78.946 +   png_sPLT_tp splt_palettes;
  78.947 +   png_uint_32 splt_palettes_num;
  78.948 +#endif
  78.949 +
  78.950 +#if defined(PNG_sCAL_SUPPORTED)
  78.951 +   /* The sCAL chunk describes the actual physical dimensions of the
  78.952 +    * subject matter of the graphic.  The chunk contains a unit specification
  78.953 +    * a byte value, and two ASCII strings representing floating-point
  78.954 +    * values.  The values are width and height corresponsing to one pixel
  78.955 +    * in the image.  This external representation is converted to double
  78.956 +    * here.  Data values are valid if (valid & PNG_INFO_sCAL) is non-zero.
  78.957 +    */
  78.958 +   png_byte scal_unit;         /* unit of physical scale */
  78.959 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  78.960 +   double scal_pixel_width;    /* width of one pixel */
  78.961 +   double scal_pixel_height;   /* height of one pixel */
  78.962 +#endif
  78.963 +#ifdef PNG_FIXED_POINT_SUPPORTED
  78.964 +   png_charp scal_s_width;     /* string containing height */
  78.965 +   png_charp scal_s_height;    /* string containing width */
  78.966 +#endif
  78.967 +#endif
  78.968 +
  78.969 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  78.970 +   /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS) non-zero */
  78.971 +   /* Data valid if (valid & PNG_INFO_IDAT) non-zero */
  78.972 +   png_bytepp row_pointers;        /* the image bits */
  78.973 +#endif
  78.974 +
  78.975 +#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)
  78.976 +   png_fixed_point int_gamma; /* gamma of image, if (valid & PNG_INFO_gAMA) */
  78.977 +#endif
  78.978 +
  78.979 +#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)
  78.980 +   png_fixed_point int_x_white;
  78.981 +   png_fixed_point int_y_white;
  78.982 +   png_fixed_point int_x_red;
  78.983 +   png_fixed_point int_y_red;
  78.984 +   png_fixed_point int_x_green;
  78.985 +   png_fixed_point int_y_green;
  78.986 +   png_fixed_point int_x_blue;
  78.987 +   png_fixed_point int_y_blue;
  78.988 +#endif
  78.989 +
  78.990 +} png_info;
  78.991 +
  78.992 +typedef png_info FAR * png_infop;
  78.993 +typedef png_info FAR * FAR * png_infopp;
  78.994 +
  78.995 +/* Maximum positive integer used in PNG is (2^31)-1 */
  78.996 +#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
  78.997 +#define PNG_UINT_32_MAX ((png_uint_32)(-1))
  78.998 +#define PNG_SIZE_MAX ((png_size_t)(-1))
  78.999 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.1000 +/* PNG_MAX_UINT is deprecated; use PNG_UINT_31_MAX instead. */
 78.1001 +#define PNG_MAX_UINT PNG_UINT_31_MAX
 78.1002 +#endif
 78.1003 +
 78.1004 +/* These describe the color_type field in png_info. */
 78.1005 +/* color type masks */
 78.1006 +#define PNG_COLOR_MASK_PALETTE    1
 78.1007 +#define PNG_COLOR_MASK_COLOR      2
 78.1008 +#define PNG_COLOR_MASK_ALPHA      4
 78.1009 +
 78.1010 +/* color types.  Note that not all combinations are legal */
 78.1011 +#define PNG_COLOR_TYPE_GRAY 0
 78.1012 +#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
 78.1013 +#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
 78.1014 +#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
 78.1015 +#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
 78.1016 +/* aliases */
 78.1017 +#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
 78.1018 +#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
 78.1019 +
 78.1020 +/* This is for compression type. PNG 1.0-1.2 only define the single type. */
 78.1021 +#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
 78.1022 +#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
 78.1023 +
 78.1024 +/* This is for filter type. PNG 1.0-1.2 only define the single type. */
 78.1025 +#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */
 78.1026 +#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
 78.1027 +#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
 78.1028 +
 78.1029 +/* These are for the interlacing type.  These values should NOT be changed. */
 78.1030 +#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */
 78.1031 +#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */
 78.1032 +#define PNG_INTERLACE_LAST        2 /* Not a valid value */
 78.1033 +
 78.1034 +/* These are for the oFFs chunk.  These values should NOT be changed. */
 78.1035 +#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */
 78.1036 +#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */
 78.1037 +#define PNG_OFFSET_LAST           2 /* Not a valid value */
 78.1038 +
 78.1039 +/* These are for the pCAL chunk.  These values should NOT be changed. */
 78.1040 +#define PNG_EQUATION_LINEAR       0 /* Linear transformation */
 78.1041 +#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */
 78.1042 +#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */
 78.1043 +#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
 78.1044 +#define PNG_EQUATION_LAST         4 /* Not a valid value */
 78.1045 +
 78.1046 +/* These are for the sCAL chunk.  These values should NOT be changed. */
 78.1047 +#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */
 78.1048 +#define PNG_SCALE_METER           1 /* meters per pixel */
 78.1049 +#define PNG_SCALE_RADIAN          2 /* radians per pixel */
 78.1050 +#define PNG_SCALE_LAST            3 /* Not a valid value */
 78.1051 +
 78.1052 +/* These are for the pHYs chunk.  These values should NOT be changed. */
 78.1053 +#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */
 78.1054 +#define PNG_RESOLUTION_METER      1 /* pixels/meter */
 78.1055 +#define PNG_RESOLUTION_LAST       2 /* Not a valid value */
 78.1056 +
 78.1057 +/* These are for the sRGB chunk.  These values should NOT be changed. */
 78.1058 +#define PNG_sRGB_INTENT_PERCEPTUAL 0
 78.1059 +#define PNG_sRGB_INTENT_RELATIVE   1
 78.1060 +#define PNG_sRGB_INTENT_SATURATION 2
 78.1061 +#define PNG_sRGB_INTENT_ABSOLUTE   3
 78.1062 +#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
 78.1063 +
 78.1064 +/* This is for text chunks */
 78.1065 +#define PNG_KEYWORD_MAX_LENGTH     79
 78.1066 +
 78.1067 +/* Maximum number of entries in PLTE/sPLT/tRNS arrays */
 78.1068 +#define PNG_MAX_PALETTE_LENGTH    256
 78.1069 +
 78.1070 +/* These determine if an ancillary chunk's data has been successfully read
 78.1071 + * from the PNG header, or if the application has filled in the corresponding
 78.1072 + * data in the info_struct to be written into the output file.  The values
 78.1073 + * of the PNG_INFO_<chunk> defines should NOT be changed.
 78.1074 + */
 78.1075 +#define PNG_INFO_gAMA 0x0001
 78.1076 +#define PNG_INFO_sBIT 0x0002
 78.1077 +#define PNG_INFO_cHRM 0x0004
 78.1078 +#define PNG_INFO_PLTE 0x0008
 78.1079 +#define PNG_INFO_tRNS 0x0010
 78.1080 +#define PNG_INFO_bKGD 0x0020
 78.1081 +#define PNG_INFO_hIST 0x0040
 78.1082 +#define PNG_INFO_pHYs 0x0080
 78.1083 +#define PNG_INFO_oFFs 0x0100
 78.1084 +#define PNG_INFO_tIME 0x0200
 78.1085 +#define PNG_INFO_pCAL 0x0400
 78.1086 +#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
 78.1087 +#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
 78.1088 +#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
 78.1089 +#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
 78.1090 +#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */
 78.1091 +
 78.1092 +/* This is used for the transformation routines, as some of them
 78.1093 + * change these values for the row.  It also should enable using
 78.1094 + * the routines for other purposes.
 78.1095 + */
 78.1096 +typedef struct png_row_info_struct
 78.1097 +{
 78.1098 +   png_uint_32 width; /* width of row */
 78.1099 +   png_uint_32 rowbytes; /* number of bytes in row */
 78.1100 +   png_byte color_type; /* color type of row */
 78.1101 +   png_byte bit_depth; /* bit depth of row */
 78.1102 +   png_byte channels; /* number of channels (1, 2, 3, or 4) */
 78.1103 +   png_byte pixel_depth; /* bits per pixel (depth * channels) */
 78.1104 +} png_row_info;
 78.1105 +
 78.1106 +typedef png_row_info FAR * png_row_infop;
 78.1107 +typedef png_row_info FAR * FAR * png_row_infopp;
 78.1108 +
 78.1109 +/* These are the function types for the I/O functions and for the functions
 78.1110 + * that allow the user to override the default I/O functions with his or her
 78.1111 + * own.  The png_error_ptr type should match that of user-supplied warning
 78.1112 + * and error functions, while the png_rw_ptr type should match that of the
 78.1113 + * user read/write data functions.
 78.1114 + */
 78.1115 +typedef struct png_struct_def png_struct;
 78.1116 +typedef png_struct FAR * png_structp;
 78.1117 +
 78.1118 +typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
 78.1119 +typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));
 78.1120 +typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));
 78.1121 +typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,
 78.1122 +   int));
 78.1123 +typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,
 78.1124 +   int));
 78.1125 +
 78.1126 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.1127 +typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop));
 78.1128 +typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
 78.1129 +typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
 78.1130 +   png_uint_32, int));
 78.1131 +#endif
 78.1132 +
 78.1133 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 78.1134 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 78.1135 +    defined(PNG_LEGACY_SUPPORTED)
 78.1136 +typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,
 78.1137 +    png_row_infop, png_bytep));
 78.1138 +#endif
 78.1139 +
 78.1140 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
 78.1141 +typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp));
 78.1142 +#endif
 78.1143 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 78.1144 +typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));
 78.1145 +#endif
 78.1146 +
 78.1147 +/* Transform masks for the high-level interface */
 78.1148 +#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */
 78.1149 +#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */
 78.1150 +#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */
 78.1151 +#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */
 78.1152 +#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */
 78.1153 +#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */
 78.1154 +#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */
 78.1155 +#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */
 78.1156 +#define PNG_TRANSFORM_BGR            0x0080    /* read and write */
 78.1157 +#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */
 78.1158 +#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */
 78.1159 +#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */
 78.1160 +#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* WRITE only */
 78.1161 +
 78.1162 +/* Flags for MNG supported features */
 78.1163 +#define PNG_FLAG_MNG_EMPTY_PLTE     0x01
 78.1164 +#define PNG_FLAG_MNG_FILTER_64      0x04
 78.1165 +#define PNG_ALL_MNG_FEATURES        0x05
 78.1166 +
 78.1167 +typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t));
 78.1168 +typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));
 78.1169 +
 78.1170 +/* The structure that holds the information to read and write PNG files.
 78.1171 + * The only people who need to care about what is inside of this are the
 78.1172 + * people who will be modifying the library for their own special needs.
 78.1173 + * It should NOT be accessed directly by an application, except to store
 78.1174 + * the jmp_buf.
 78.1175 + */
 78.1176 +
 78.1177 +struct png_struct_def
 78.1178 +{
 78.1179 +#ifdef PNG_SETJMP_SUPPORTED
 78.1180 +   jmp_buf jmpbuf;            /* used in png_error */
 78.1181 +#endif
 78.1182 +   png_error_ptr error_fn;    /* function for printing errors and aborting */
 78.1183 +   png_error_ptr warning_fn;  /* function for printing warnings */
 78.1184 +   png_voidp error_ptr;       /* user supplied struct for error functions */
 78.1185 +   png_rw_ptr write_data_fn;  /* function for writing output data */
 78.1186 +   png_rw_ptr read_data_fn;   /* function for reading input data */
 78.1187 +   png_voidp io_ptr;          /* ptr to application struct for I/O functions */
 78.1188 +
 78.1189 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 78.1190 +   png_user_transform_ptr read_user_transform_fn; /* user read transform */
 78.1191 +#endif
 78.1192 +
 78.1193 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 78.1194 +   png_user_transform_ptr write_user_transform_fn; /* user write transform */
 78.1195 +#endif
 78.1196 +
 78.1197 +/* These were added in libpng-1.0.2 */
 78.1198 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 78.1199 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 78.1200 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 78.1201 +   png_voidp user_transform_ptr; /* user supplied struct for user transform */
 78.1202 +   png_byte user_transform_depth;    /* bit depth of user transformed pixels */
 78.1203 +   png_byte user_transform_channels; /* channels in user transformed pixels */
 78.1204 +#endif
 78.1205 +#endif
 78.1206 +
 78.1207 +   png_uint_32 mode;          /* tells us where we are in the PNG file */
 78.1208 +   png_uint_32 flags;         /* flags indicating various things to libpng */
 78.1209 +   png_uint_32 transformations; /* which transformations to perform */
 78.1210 +
 78.1211 +   z_stream zstream;          /* pointer to decompression structure (below) */
 78.1212 +   png_bytep zbuf;            /* buffer for zlib */
 78.1213 +   png_size_t zbuf_size;      /* size of zbuf */
 78.1214 +   int zlib_level;            /* holds zlib compression level */
 78.1215 +   int zlib_method;           /* holds zlib compression method */
 78.1216 +   int zlib_window_bits;      /* holds zlib compression window bits */
 78.1217 +   int zlib_mem_level;        /* holds zlib compression memory level */
 78.1218 +   int zlib_strategy;         /* holds zlib compression strategy */
 78.1219 +
 78.1220 +   png_uint_32 width;         /* width of image in pixels */
 78.1221 +   png_uint_32 height;        /* height of image in pixels */
 78.1222 +   png_uint_32 num_rows;      /* number of rows in current pass */
 78.1223 +   png_uint_32 usr_width;     /* width of row at start of write */
 78.1224 +   png_uint_32 rowbytes;      /* size of row in bytes */
 78.1225 +   png_uint_32 irowbytes;     /* size of current interlaced row in bytes */
 78.1226 +   png_uint_32 iwidth;        /* width of current interlaced row in pixels */
 78.1227 +   png_uint_32 row_number;    /* current row in interlace pass */
 78.1228 +   png_bytep prev_row;        /* buffer to save previous (unfiltered) row */
 78.1229 +   png_bytep row_buf;         /* buffer to save current (unfiltered) row */
 78.1230 +#ifndef PNG_NO_WRITE_FILTER
 78.1231 +   png_bytep sub_row;         /* buffer to save "sub" row when filtering */
 78.1232 +   png_bytep up_row;          /* buffer to save "up" row when filtering */
 78.1233 +   png_bytep avg_row;         /* buffer to save "avg" row when filtering */
 78.1234 +   png_bytep paeth_row;       /* buffer to save "Paeth" row when filtering */
 78.1235 +#endif
 78.1236 +   png_row_info row_info;     /* used for transformation routines */
 78.1237 +
 78.1238 +   png_uint_32 idat_size;     /* current IDAT size for read */
 78.1239 +   png_uint_32 crc;           /* current chunk CRC value */
 78.1240 +   png_colorp palette;        /* palette from the input file */
 78.1241 +   png_uint_16 num_palette;   /* number of color entries in palette */
 78.1242 +   png_uint_16 num_trans;     /* number of transparency values */
 78.1243 +   png_byte chunk_name[5];    /* null-terminated name of current chunk */
 78.1244 +   png_byte compression;      /* file compression type (always 0) */
 78.1245 +   png_byte filter;           /* file filter type (always 0) */
 78.1246 +   png_byte interlaced;       /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
 78.1247 +   png_byte pass;             /* current interlace pass (0 - 6) */
 78.1248 +   png_byte do_filter;        /* row filter flags (see PNG_FILTER_ below ) */
 78.1249 +   png_byte color_type;       /* color type of file */
 78.1250 +   png_byte bit_depth;        /* bit depth of file */
 78.1251 +   png_byte usr_bit_depth;    /* bit depth of users row */
 78.1252 +   png_byte pixel_depth;      /* number of bits per pixel */
 78.1253 +   png_byte channels;         /* number of channels in file */
 78.1254 +   png_byte usr_channels;     /* channels at start of write */
 78.1255 +   png_byte sig_bytes;        /* magic bytes read/written from start of file */
 78.1256 +
 78.1257 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
 78.1258 +#ifdef PNG_LEGACY_SUPPORTED
 78.1259 +   png_byte filler;           /* filler byte for pixel expansion */
 78.1260 +#else
 78.1261 +   png_uint_16 filler;           /* filler bytes for pixel expansion */
 78.1262 +#endif
 78.1263 +#endif
 78.1264 +
 78.1265 +#if defined(PNG_bKGD_SUPPORTED)
 78.1266 +   png_byte background_gamma_type;
 78.1267 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1268 +   float background_gamma;
 78.1269 +#  endif
 78.1270 +   png_color_16 background;   /* background color in screen gamma space */
 78.1271 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 78.1272 +   png_color_16 background_1; /* background normalized to gamma 1.0 */
 78.1273 +#endif
 78.1274 +#endif /* PNG_bKGD_SUPPORTED */
 78.1275 +
 78.1276 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 78.1277 +   png_flush_ptr output_flush_fn;/* Function for flushing output */
 78.1278 +   png_uint_32 flush_dist;    /* how many rows apart to flush, 0 - no flush */
 78.1279 +   png_uint_32 flush_rows;    /* number of rows written since last flush */
 78.1280 +#endif
 78.1281 +
 78.1282 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.1283 +   int gamma_shift;      /* number of "insignificant" bits 16-bit gamma */
 78.1284 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1285 +   float gamma;          /* file gamma value */
 78.1286 +   float screen_gamma;   /* screen gamma value (display_exponent) */
 78.1287 +#endif
 78.1288 +#endif
 78.1289 +
 78.1290 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.1291 +   png_bytep gamma_table;     /* gamma table for 8-bit depth files */
 78.1292 +   png_bytep gamma_from_1;    /* converts from 1.0 to screen */
 78.1293 +   png_bytep gamma_to_1;      /* converts from file to 1.0 */
 78.1294 +   png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */
 78.1295 +   png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
 78.1296 +   png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
 78.1297 +#endif
 78.1298 +
 78.1299 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
 78.1300 +   png_color_8 sig_bit;       /* significant bits in each available channel */
 78.1301 +#endif
 78.1302 +
 78.1303 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
 78.1304 +   png_color_8 shift;         /* shift for significant bit tranformation */
 78.1305 +#endif
 78.1306 +
 78.1307 +#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
 78.1308 + || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.1309 +   png_bytep trans;           /* transparency values for paletted files */
 78.1310 +   png_color_16 trans_values; /* transparency values for non-paletted files */
 78.1311 +#endif
 78.1312 +
 78.1313 +   png_read_status_ptr read_row_fn;   /* called after each row is decoded */
 78.1314 +   png_write_status_ptr write_row_fn; /* called after each row is encoded */
 78.1315 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.1316 +   png_progressive_info_ptr info_fn; /* called after header data fully read */
 78.1317 +   png_progressive_row_ptr row_fn;   /* called after each prog. row is decoded */
 78.1318 +   png_progressive_end_ptr end_fn;   /* called after image is complete */
 78.1319 +   png_bytep save_buffer_ptr;        /* current location in save_buffer */
 78.1320 +   png_bytep save_buffer;            /* buffer for previously read data */
 78.1321 +   png_bytep current_buffer_ptr;     /* current location in current_buffer */
 78.1322 +   png_bytep current_buffer;         /* buffer for recently used data */
 78.1323 +   png_uint_32 push_length;          /* size of current input chunk */
 78.1324 +   png_uint_32 skip_length;          /* bytes to skip in input data */
 78.1325 +   png_size_t save_buffer_size;      /* amount of data now in save_buffer */
 78.1326 +   png_size_t save_buffer_max;       /* total size of save_buffer */
 78.1327 +   png_size_t buffer_size;           /* total amount of available input data */
 78.1328 +   png_size_t current_buffer_size;   /* amount of data now in current_buffer */
 78.1329 +   int process_mode;                 /* what push library is currently doing */
 78.1330 +   int cur_palette;                  /* current push library palette index */
 78.1331 +
 78.1332 +#  if defined(PNG_TEXT_SUPPORTED)
 78.1333 +     png_size_t current_text_size;   /* current size of text input data */
 78.1334 +     png_size_t current_text_left;   /* how much text left to read in input */
 78.1335 +     png_charp current_text;         /* current text chunk buffer */
 78.1336 +     png_charp current_text_ptr;     /* current location in current_text */
 78.1337 +#  endif /* PNG_TEXT_SUPPORTED */
 78.1338 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 78.1339 +
 78.1340 +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
 78.1341 +/* for the Borland special 64K segment handler */
 78.1342 +   png_bytepp offset_table_ptr;
 78.1343 +   png_bytep offset_table;
 78.1344 +   png_uint_16 offset_table_number;
 78.1345 +   png_uint_16 offset_table_count;
 78.1346 +   png_uint_16 offset_table_count_free;
 78.1347 +#endif
 78.1348 +
 78.1349 +#if defined(PNG_READ_DITHER_SUPPORTED)
 78.1350 +   png_bytep palette_lookup;         /* lookup table for dithering */
 78.1351 +   png_bytep dither_index;           /* index translation for palette files */
 78.1352 +#endif
 78.1353 +
 78.1354 +#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED)
 78.1355 +   png_uint_16p hist;                /* histogram */
 78.1356 +#endif
 78.1357 +
 78.1358 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 78.1359 +   png_byte heuristic_method;        /* heuristic for row filter selection */
 78.1360 +   png_byte num_prev_filters;        /* number of weights for previous rows */
 78.1361 +   png_bytep prev_filters;           /* filter type(s) of previous row(s) */
 78.1362 +   png_uint_16p filter_weights;      /* weight(s) for previous line(s) */
 78.1363 +   png_uint_16p inv_filter_weights;  /* 1/weight(s) for previous line(s) */
 78.1364 +   png_uint_16p filter_costs;        /* relative filter calculation cost */
 78.1365 +   png_uint_16p inv_filter_costs;    /* 1/relative filter calculation cost */
 78.1366 +#endif
 78.1367 +
 78.1368 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 78.1369 +   png_charp time_buffer;            /* String to hold RFC 1123 time text */
 78.1370 +#endif
 78.1371 +
 78.1372 +/* New members added in libpng-1.0.6 */
 78.1373 +
 78.1374 +#ifdef PNG_FREE_ME_SUPPORTED
 78.1375 +   png_uint_32 free_me;       /* flags items libpng is responsible for freeing */
 78.1376 +#endif
 78.1377 +
 78.1378 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
 78.1379 +   png_voidp user_chunk_ptr;
 78.1380 +   png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
 78.1381 +#endif
 78.1382 +
 78.1383 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 78.1384 +   int num_chunk_list;
 78.1385 +   png_bytep chunk_list;
 78.1386 +#endif
 78.1387 +
 78.1388 +/* New members added in libpng-1.0.3 */
 78.1389 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 78.1390 +   png_byte rgb_to_gray_status;
 78.1391 +   /* These were changed from png_byte in libpng-1.0.6 */
 78.1392 +   png_uint_16 rgb_to_gray_red_coeff;
 78.1393 +   png_uint_16 rgb_to_gray_green_coeff;
 78.1394 +   png_uint_16 rgb_to_gray_blue_coeff;
 78.1395 +#endif
 78.1396 +
 78.1397 +/* New member added in libpng-1.0.4 (renamed in 1.0.9) */
 78.1398 +#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
 78.1399 +    defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 78.1400 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 78.1401 +/* changed from png_byte to png_uint_32 at version 1.2.0 */
 78.1402 +#ifdef PNG_1_0_X
 78.1403 +   png_byte mng_features_permitted;
 78.1404 +#else
 78.1405 +   png_uint_32 mng_features_permitted;
 78.1406 +#endif /* PNG_1_0_X */
 78.1407 +#endif
 78.1408 +
 78.1409 +/* New member added in libpng-1.0.7 */
 78.1410 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.1411 +   png_fixed_point int_gamma;
 78.1412 +#endif
 78.1413 +
 78.1414 +/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */
 78.1415 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 78.1416 +   png_byte filter_type;
 78.1417 +#endif
 78.1418 +
 78.1419 +#if defined(PNG_1_0_X)
 78.1420 +/* New member added in libpng-1.0.10, ifdef'ed out in 1.2.0 */
 78.1421 +   png_uint_32 row_buf_size;
 78.1422 +#endif
 78.1423 +
 78.1424 +/* New members added in libpng-1.2.0 */
 78.1425 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 78.1426 +#  if !defined(PNG_1_0_X)
 78.1427 +#    if defined(PNG_MMX_CODE_SUPPORTED)
 78.1428 +   png_byte     mmx_bitdepth_threshold;
 78.1429 +   png_uint_32  mmx_rowbytes_threshold;
 78.1430 +#    endif
 78.1431 +   png_uint_32  asm_flags;
 78.1432 +#  endif
 78.1433 +#endif
 78.1434 +
 78.1435 +/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
 78.1436 +#ifdef PNG_USER_MEM_SUPPORTED
 78.1437 +   png_voidp mem_ptr;                /* user supplied struct for mem functions */
 78.1438 +   png_malloc_ptr malloc_fn;         /* function for allocating memory */
 78.1439 +   png_free_ptr free_fn;             /* function for freeing memory */
 78.1440 +#endif
 78.1441 +
 78.1442 +/* New member added in libpng-1.0.13 and 1.2.0 */
 78.1443 +   png_bytep big_row_buf;         /* buffer to save current (unfiltered) row */
 78.1444 +
 78.1445 +#if defined(PNG_READ_DITHER_SUPPORTED)
 78.1446 +/* The following three members were added at version 1.0.14 and 1.2.4 */
 78.1447 +   png_bytep dither_sort;            /* working sort array */
 78.1448 +   png_bytep index_to_palette;       /* where the original index currently is */
 78.1449 +                                     /* in the palette */
 78.1450 +   png_bytep palette_to_index;       /* which original index points to this */
 78.1451 +                                     /* palette color */
 78.1452 +#endif
 78.1453 +
 78.1454 +/* New members added in libpng-1.0.16 and 1.2.6 */
 78.1455 +   png_byte compression_type;
 78.1456 +
 78.1457 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 78.1458 +   png_uint_32 user_width_max;
 78.1459 +   png_uint_32 user_height_max;
 78.1460 +#endif
 78.1461 +
 78.1462 +/* New member added in libpng-1.0.25 and 1.2.17 */
 78.1463 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 78.1464 +   /* storage for unknown chunk that the library doesn't recognize. */
 78.1465 +   png_unknown_chunk unknown_chunk;
 78.1466 +#endif
 78.1467 +
 78.1468 +/* New members added in libpng-1.2.26 */
 78.1469 +  png_uint_32 old_big_row_buf_size, old_prev_row_size;
 78.1470 +
 78.1471 +/* New member added in libpng-1.2.30 */
 78.1472 +  png_charp chunkdata;  /* buffer for reading chunk data */
 78.1473 +
 78.1474 +};
 78.1475 +
 78.1476 +
 78.1477 +/* This triggers a compiler error in png.c, if png.c and png.h
 78.1478 + * do not agree upon the version number.
 78.1479 + */
 78.1480 +typedef png_structp version_1_2_33;
 78.1481 +
 78.1482 +typedef png_struct FAR * FAR * png_structpp;
 78.1483 +
 78.1484 +/* Here are the function definitions most commonly used.  This is not
 78.1485 + * the place to find out how to use libpng.  See libpng.txt for the
 78.1486 + * full explanation, see example.c for the summary.  This just provides
 78.1487 + * a simple one line description of the use of each function.
 78.1488 + */
 78.1489 +
 78.1490 +/* Returns the version number of the library */
 78.1491 +extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));
 78.1492 +
 78.1493 +/* Tell lib we have already handled the first <num_bytes> magic bytes.
 78.1494 + * Handling more than 8 bytes from the beginning of the file is an error.
 78.1495 + */
 78.1496 +extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
 78.1497 +   int num_bytes));
 78.1498 +
 78.1499 +/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
 78.1500 + * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
 78.1501 + * signature, and non-zero otherwise.  Having num_to_check == 0 or
 78.1502 + * start > 7 will always fail (ie return non-zero).
 78.1503 + */
 78.1504 +extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
 78.1505 +   png_size_t num_to_check));
 78.1506 +
 78.1507 +/* Simple signature checking function.  This is the same as calling
 78.1508 + * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).
 78.1509 + */
 78.1510 +extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
 78.1511 +
 78.1512 +/* Allocate and initialize png_ptr struct for reading, and any other memory. */
 78.1513 +extern PNG_EXPORT(png_structp,png_create_read_struct)
 78.1514 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 78.1515 +   png_error_ptr error_fn, png_error_ptr warn_fn));
 78.1516 +
 78.1517 +/* Allocate and initialize png_ptr struct for writing, and any other memory */
 78.1518 +extern PNG_EXPORT(png_structp,png_create_write_struct)
 78.1519 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 78.1520 +   png_error_ptr error_fn, png_error_ptr warn_fn));
 78.1521 +
 78.1522 +#ifdef PNG_WRITE_SUPPORTED
 78.1523 +extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size)
 78.1524 +   PNGARG((png_structp png_ptr));
 78.1525 +#endif
 78.1526 +
 78.1527 +#ifdef PNG_WRITE_SUPPORTED
 78.1528 +extern PNG_EXPORT(void,png_set_compression_buffer_size)
 78.1529 +   PNGARG((png_structp png_ptr, png_uint_32 size));
 78.1530 +#endif
 78.1531 +
 78.1532 +/* Reset the compression stream */
 78.1533 +extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));
 78.1534 +
 78.1535 +/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */
 78.1536 +#ifdef PNG_USER_MEM_SUPPORTED
 78.1537 +extern PNG_EXPORT(png_structp,png_create_read_struct_2)
 78.1538 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 78.1539 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
 78.1540 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 78.1541 +extern PNG_EXPORT(png_structp,png_create_write_struct_2)
 78.1542 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 78.1543 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
 78.1544 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 78.1545 +#endif
 78.1546 +
 78.1547 +/* Write a PNG chunk - size, type, (optional) data, CRC. */
 78.1548 +extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,
 78.1549 +   png_bytep chunk_name, png_bytep data, png_size_t length));
 78.1550 +
 78.1551 +/* Write the start of a PNG chunk - length and chunk name. */
 78.1552 +extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,
 78.1553 +   png_bytep chunk_name, png_uint_32 length));
 78.1554 +
 78.1555 +/* Write the data of a PNG chunk started with png_write_chunk_start(). */
 78.1556 +extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,
 78.1557 +   png_bytep data, png_size_t length));
 78.1558 +
 78.1559 +/* Finish a chunk started with png_write_chunk_start() (includes CRC). */
 78.1560 +extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));
 78.1561 +
 78.1562 +/* Allocate and initialize the info structure */
 78.1563 +extern PNG_EXPORT(png_infop,png_create_info_struct)
 78.1564 +   PNGARG((png_structp png_ptr));
 78.1565 +
 78.1566 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.1567 +/* Initialize the info structure (old interface - DEPRECATED) */
 78.1568 +extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr));
 78.1569 +#undef png_info_init
 78.1570 +#define png_info_init(info_ptr) png_info_init_3(&info_ptr,\
 78.1571 +    png_sizeof(png_info));
 78.1572 +#endif
 78.1573 +
 78.1574 +extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,
 78.1575 +    png_size_t png_info_struct_size));
 78.1576 +
 78.1577 +/* Writes all the PNG information before the image. */
 78.1578 +extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,
 78.1579 +   png_infop info_ptr));
 78.1580 +extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,
 78.1581 +   png_infop info_ptr));
 78.1582 +
 78.1583 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 78.1584 +/* read the information before the actual image data. */
 78.1585 +extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,
 78.1586 +   png_infop info_ptr));
 78.1587 +#endif
 78.1588 +
 78.1589 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 78.1590 +extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)
 78.1591 +   PNGARG((png_structp png_ptr, png_timep ptime));
 78.1592 +#endif
 78.1593 +
 78.1594 +#if !defined(_WIN32_WCE)
 78.1595 +/* "time.h" functions are not supported on WindowsCE */
 78.1596 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 78.1597 +/* convert from a struct tm to png_time */
 78.1598 +extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,
 78.1599 +   struct tm FAR * ttime));
 78.1600 +
 78.1601 +/* convert from time_t to png_time.  Uses gmtime() */
 78.1602 +extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,
 78.1603 +   time_t ttime));
 78.1604 +#endif /* PNG_WRITE_tIME_SUPPORTED */
 78.1605 +#endif /* _WIN32_WCE */
 78.1606 +
 78.1607 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 78.1608 +/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */
 78.1609 +extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));
 78.1610 +#if !defined(PNG_1_0_X)
 78.1611 +extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp
 78.1612 +  png_ptr));
 78.1613 +#endif
 78.1614 +extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));
 78.1615 +extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));
 78.1616 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.1617 +/* Deprecated */
 78.1618 +extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr));
 78.1619 +#endif
 78.1620 +#endif
 78.1621 +
 78.1622 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 78.1623 +/* Use blue, green, red order for pixels. */
 78.1624 +extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));
 78.1625 +#endif
 78.1626 +
 78.1627 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 78.1628 +/* Expand the grayscale to 24-bit RGB if necessary. */
 78.1629 +extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));
 78.1630 +#endif
 78.1631 +
 78.1632 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 78.1633 +/* Reduce RGB to grayscale. */
 78.1634 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1635 +extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
 78.1636 +   int error_action, double red, double green ));
 78.1637 +#endif
 78.1638 +extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,
 78.1639 +   int error_action, png_fixed_point red, png_fixed_point green ));
 78.1640 +extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
 78.1641 +   png_ptr));
 78.1642 +#endif
 78.1643 +
 78.1644 +extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
 78.1645 +   png_colorp palette));
 78.1646 +
 78.1647 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 78.1648 +extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));
 78.1649 +#endif
 78.1650 +
 78.1651 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
 78.1652 +    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 78.1653 +extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));
 78.1654 +#endif
 78.1655 +
 78.1656 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
 78.1657 +    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 78.1658 +extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));
 78.1659 +#endif
 78.1660 +
 78.1661 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
 78.1662 +/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */
 78.1663 +extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,
 78.1664 +   png_uint_32 filler, int flags));
 78.1665 +/* The values of the PNG_FILLER_ defines should NOT be changed */
 78.1666 +#define PNG_FILLER_BEFORE 0
 78.1667 +#define PNG_FILLER_AFTER 1
 78.1668 +/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */
 78.1669 +#if !defined(PNG_1_0_X)
 78.1670 +extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,
 78.1671 +   png_uint_32 filler, int flags));
 78.1672 +#endif
 78.1673 +#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */
 78.1674 +
 78.1675 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
 78.1676 +/* Swap bytes in 16-bit depth files. */
 78.1677 +extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));
 78.1678 +#endif
 78.1679 +
 78.1680 +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
 78.1681 +/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */
 78.1682 +extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));
 78.1683 +#endif
 78.1684 +
 78.1685 +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 78.1686 +/* Swap packing order of pixels in bytes. */
 78.1687 +extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));
 78.1688 +#endif
 78.1689 +
 78.1690 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
 78.1691 +/* Converts files to legal bit depths. */
 78.1692 +extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,
 78.1693 +   png_color_8p true_bits));
 78.1694 +#endif
 78.1695 +
 78.1696 +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
 78.1697 +    defined(PNG_WRITE_INTERLACING_SUPPORTED)
 78.1698 +/* Have the code handle the interlacing.  Returns the number of passes. */
 78.1699 +extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));
 78.1700 +#endif
 78.1701 +
 78.1702 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
 78.1703 +/* Invert monochrome files */
 78.1704 +extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));
 78.1705 +#endif
 78.1706 +
 78.1707 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.1708 +/* Handle alpha and tRNS by replacing with a background color. */
 78.1709 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1710 +extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,
 78.1711 +   png_color_16p background_color, int background_gamma_code,
 78.1712 +   int need_expand, double background_gamma));
 78.1713 +#endif
 78.1714 +#define PNG_BACKGROUND_GAMMA_UNKNOWN 0
 78.1715 +#define PNG_BACKGROUND_GAMMA_SCREEN  1
 78.1716 +#define PNG_BACKGROUND_GAMMA_FILE    2
 78.1717 +#define PNG_BACKGROUND_GAMMA_UNIQUE  3
 78.1718 +#endif
 78.1719 +
 78.1720 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 78.1721 +/* strip the second byte of information from a 16-bit depth file. */
 78.1722 +extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));
 78.1723 +#endif
 78.1724 +
 78.1725 +#if defined(PNG_READ_DITHER_SUPPORTED)
 78.1726 +/* Turn on dithering, and reduce the palette to the number of colors available. */
 78.1727 +extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr,
 78.1728 +   png_colorp palette, int num_palette, int maximum_colors,
 78.1729 +   png_uint_16p histogram, int full_dither));
 78.1730 +#endif
 78.1731 +
 78.1732 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 78.1733 +/* Handle gamma correction. Screen_gamma=(display_exponent) */
 78.1734 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1735 +extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,
 78.1736 +   double screen_gamma, double default_file_gamma));
 78.1737 +#endif
 78.1738 +#endif
 78.1739 +
 78.1740 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.1741 +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 78.1742 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 78.1743 +/* Permit or disallow empty PLTE (0: not permitted, 1: permitted) */
 78.1744 +/* Deprecated and will be removed.  Use png_permit_mng_features() instead. */
 78.1745 +extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr,
 78.1746 +   int empty_plte_permitted));
 78.1747 +#endif
 78.1748 +#endif
 78.1749 +
 78.1750 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 78.1751 +/* Set how many lines between output flushes - 0 for no flushing */
 78.1752 +extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));
 78.1753 +/* Flush the current PNG output buffer */
 78.1754 +extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));
 78.1755 +#endif
 78.1756 +
 78.1757 +/* optional update palette with requested transformations */
 78.1758 +extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));
 78.1759 +
 78.1760 +/* optional call to update the users info structure */
 78.1761 +extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,
 78.1762 +   png_infop info_ptr));
 78.1763 +
 78.1764 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 78.1765 +/* read one or more rows of image data. */
 78.1766 +extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,
 78.1767 +   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));
 78.1768 +#endif
 78.1769 +
 78.1770 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 78.1771 +/* read a row of data. */
 78.1772 +extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,
 78.1773 +   png_bytep row,
 78.1774 +   png_bytep display_row));
 78.1775 +#endif
 78.1776 +
 78.1777 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 78.1778 +/* read the whole image into memory at once. */
 78.1779 +extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,
 78.1780 +   png_bytepp image));
 78.1781 +#endif
 78.1782 +
 78.1783 +/* write a row of image data */
 78.1784 +extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,
 78.1785 +   png_bytep row));
 78.1786 +
 78.1787 +/* write a few rows of image data */
 78.1788 +extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,
 78.1789 +   png_bytepp row, png_uint_32 num_rows));
 78.1790 +
 78.1791 +/* write the image data */
 78.1792 +extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
 78.1793 +   png_bytepp image));
 78.1794 +
 78.1795 +/* writes the end of the PNG file. */
 78.1796 +extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
 78.1797 +   png_infop info_ptr));
 78.1798 +
 78.1799 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 78.1800 +/* read the end of the PNG file. */
 78.1801 +extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,
 78.1802 +   png_infop info_ptr));
 78.1803 +#endif
 78.1804 +
 78.1805 +/* free any memory associated with the png_info_struct */
 78.1806 +extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,
 78.1807 +   png_infopp info_ptr_ptr));
 78.1808 +
 78.1809 +/* free any memory associated with the png_struct and the png_info_structs */
 78.1810 +extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp
 78.1811 +   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));
 78.1812 +
 78.1813 +/* free all memory used by the read (old method - NOT DLL EXPORTED) */
 78.1814 +extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.1815 +   png_infop end_info_ptr));
 78.1816 +
 78.1817 +/* free any memory associated with the png_struct and the png_info_structs */
 78.1818 +extern PNG_EXPORT(void,png_destroy_write_struct)
 78.1819 +   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));
 78.1820 +
 78.1821 +/* free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */
 78.1822 +extern void png_write_destroy PNGARG((png_structp png_ptr));
 78.1823 +
 78.1824 +/* set the libpng method of handling chunk CRC errors */
 78.1825 +extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,
 78.1826 +   int crit_action, int ancil_action));
 78.1827 +
 78.1828 +/* Values for png_set_crc_action() to say how to handle CRC errors in
 78.1829 + * ancillary and critical chunks, and whether to use the data contained
 78.1830 + * therein.  Note that it is impossible to "discard" data in a critical
 78.1831 + * chunk.  For versions prior to 0.90, the action was always error/quit,
 78.1832 + * whereas in version 0.90 and later, the action for CRC errors in ancillary
 78.1833 + * chunks is warn/discard.  These values should NOT be changed.
 78.1834 + *
 78.1835 + *      value                       action:critical     action:ancillary
 78.1836 + */
 78.1837 +#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */
 78.1838 +#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */
 78.1839 +#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */
 78.1840 +#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */
 78.1841 +#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */
 78.1842 +#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */
 78.1843 +
 78.1844 +/* These functions give the user control over the scan-line filtering in
 78.1845 + * libpng and the compression methods used by zlib.  These functions are
 78.1846 + * mainly useful for testing, as the defaults should work with most users.
 78.1847 + * Those users who are tight on memory or want faster performance at the
 78.1848 + * expense of compression can modify them.  See the compression library
 78.1849 + * header file (zlib.h) for an explination of the compression functions.
 78.1850 + */
 78.1851 +
 78.1852 +/* set the filtering method(s) used by libpng.  Currently, the only valid
 78.1853 + * value for "method" is 0.
 78.1854 + */
 78.1855 +extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,
 78.1856 +   int filters));
 78.1857 +
 78.1858 +/* Flags for png_set_filter() to say which filters to use.  The flags
 78.1859 + * are chosen so that they don't conflict with real filter types
 78.1860 + * below, in case they are supplied instead of the #defined constants.
 78.1861 + * These values should NOT be changed.
 78.1862 + */
 78.1863 +#define PNG_NO_FILTERS     0x00
 78.1864 +#define PNG_FILTER_NONE    0x08
 78.1865 +#define PNG_FILTER_SUB     0x10
 78.1866 +#define PNG_FILTER_UP      0x20
 78.1867 +#define PNG_FILTER_AVG     0x40
 78.1868 +#define PNG_FILTER_PAETH   0x80
 78.1869 +#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
 78.1870 +                         PNG_FILTER_AVG | PNG_FILTER_PAETH)
 78.1871 +
 78.1872 +/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.
 78.1873 + * These defines should NOT be changed.
 78.1874 + */
 78.1875 +#define PNG_FILTER_VALUE_NONE  0
 78.1876 +#define PNG_FILTER_VALUE_SUB   1
 78.1877 +#define PNG_FILTER_VALUE_UP    2
 78.1878 +#define PNG_FILTER_VALUE_AVG   3
 78.1879 +#define PNG_FILTER_VALUE_PAETH 4
 78.1880 +#define PNG_FILTER_VALUE_LAST  5
 78.1881 +
 78.1882 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */
 78.1883 +/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_
 78.1884 + * defines, either the default (minimum-sum-of-absolute-differences), or
 78.1885 + * the experimental method (weighted-minimum-sum-of-absolute-differences).
 78.1886 + *
 78.1887 + * Weights are factors >= 1.0, indicating how important it is to keep the
 78.1888 + * filter type consistent between rows.  Larger numbers mean the current
 78.1889 + * filter is that many times as likely to be the same as the "num_weights"
 78.1890 + * previous filters.  This is cumulative for each previous row with a weight.
 78.1891 + * There needs to be "num_weights" values in "filter_weights", or it can be
 78.1892 + * NULL if the weights aren't being specified.  Weights have no influence on
 78.1893 + * the selection of the first row filter.  Well chosen weights can (in theory)
 78.1894 + * improve the compression for a given image.
 78.1895 + *
 78.1896 + * Costs are factors >= 1.0 indicating the relative decoding costs of a
 78.1897 + * filter type.  Higher costs indicate more decoding expense, and are
 78.1898 + * therefore less likely to be selected over a filter with lower computational
 78.1899 + * costs.  There needs to be a value in "filter_costs" for each valid filter
 78.1900 + * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't
 78.1901 + * setting the costs.  Costs try to improve the speed of decompression without
 78.1902 + * unduly increasing the compressed image size.
 78.1903 + *
 78.1904 + * A negative weight or cost indicates the default value is to be used, and
 78.1905 + * values in the range [0.0, 1.0) indicate the value is to remain unchanged.
 78.1906 + * The default values for both weights and costs are currently 1.0, but may
 78.1907 + * change if good general weighting/cost heuristics can be found.  If both
 78.1908 + * the weights and costs are set to 1.0, this degenerates the WEIGHTED method
 78.1909 + * to the UNWEIGHTED method, but with added encoding time/computation.
 78.1910 + */
 78.1911 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.1912 +extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,
 78.1913 +   int heuristic_method, int num_weights, png_doublep filter_weights,
 78.1914 +   png_doublep filter_costs));
 78.1915 +#endif
 78.1916 +#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
 78.1917 +
 78.1918 +/* Heuristic used for row filter selection.  These defines should NOT be
 78.1919 + * changed.
 78.1920 + */
 78.1921 +#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
 78.1922 +#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
 78.1923 +#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
 78.1924 +#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */
 78.1925 +
 78.1926 +/* Set the library compression level.  Currently, valid values range from
 78.1927 + * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
 78.1928 + * (0 - no compression, 9 - "maximal" compression).  Note that tests have
 78.1929 + * shown that zlib compression levels 3-6 usually perform as well as level 9
 78.1930 + * for PNG images, and do considerably fewer caclulations.  In the future,
 78.1931 + * these values may not correspond directly to the zlib compression levels.
 78.1932 + */
 78.1933 +extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,
 78.1934 +   int level));
 78.1935 +
 78.1936 +extern PNG_EXPORT(void,png_set_compression_mem_level)
 78.1937 +   PNGARG((png_structp png_ptr, int mem_level));
 78.1938 +
 78.1939 +extern PNG_EXPORT(void,png_set_compression_strategy)
 78.1940 +   PNGARG((png_structp png_ptr, int strategy));
 78.1941 +
 78.1942 +extern PNG_EXPORT(void,png_set_compression_window_bits)
 78.1943 +   PNGARG((png_structp png_ptr, int window_bits));
 78.1944 +
 78.1945 +extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,
 78.1946 +   int method));
 78.1947 +
 78.1948 +/* These next functions are called for input/output, memory, and error
 78.1949 + * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,
 78.1950 + * and call standard C I/O routines such as fread(), fwrite(), and
 78.1951 + * fprintf().  These functions can be made to use other I/O routines
 78.1952 + * at run time for those applications that need to handle I/O in a
 78.1953 + * different manner by calling png_set_???_fn().  See libpng.txt for
 78.1954 + * more information.
 78.1955 + */
 78.1956 +
 78.1957 +#if !defined(PNG_NO_STDIO)
 78.1958 +/* Initialize the input/output for the PNG file to the default functions. */
 78.1959 +extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp));
 78.1960 +#endif
 78.1961 +
 78.1962 +/* Replace the (error and abort), and warning functions with user
 78.1963 + * supplied functions.  If no messages are to be printed you must still
 78.1964 + * write and use replacement functions. The replacement error_fn should
 78.1965 + * still do a longjmp to the last setjmp location if you are using this
 78.1966 + * method of error handling.  If error_fn or warning_fn is NULL, the
 78.1967 + * default function will be used.
 78.1968 + */
 78.1969 +
 78.1970 +extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
 78.1971 +   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));
 78.1972 +
 78.1973 +/* Return the user pointer associated with the error functions */
 78.1974 +extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));
 78.1975 +
 78.1976 +/* Replace the default data output functions with a user supplied one(s).
 78.1977 + * If buffered output is not used, then output_flush_fn can be set to NULL.
 78.1978 + * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
 78.1979 + * output_flush_fn will be ignored (and thus can be NULL).
 78.1980 + */
 78.1981 +extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,
 78.1982 +   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
 78.1983 +
 78.1984 +/* Replace the default data input function with a user supplied one. */
 78.1985 +extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,
 78.1986 +   png_voidp io_ptr, png_rw_ptr read_data_fn));
 78.1987 +
 78.1988 +/* Return the user pointer associated with the I/O functions */
 78.1989 +extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));
 78.1990 +
 78.1991 +extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,
 78.1992 +   png_read_status_ptr read_row_fn));
 78.1993 +
 78.1994 +extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,
 78.1995 +   png_write_status_ptr write_row_fn));
 78.1996 +
 78.1997 +#ifdef PNG_USER_MEM_SUPPORTED
 78.1998 +/* Replace the default memory allocation functions with user supplied one(s). */
 78.1999 +extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,
 78.2000 +   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 78.2001 +/* Return the user pointer associated with the memory functions */
 78.2002 +extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));
 78.2003 +#endif
 78.2004 +
 78.2005 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 78.2006 +    defined(PNG_LEGACY_SUPPORTED)
 78.2007 +extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp
 78.2008 +   png_ptr, png_user_transform_ptr read_user_transform_fn));
 78.2009 +#endif
 78.2010 +
 78.2011 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 78.2012 +    defined(PNG_LEGACY_SUPPORTED)
 78.2013 +extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp
 78.2014 +   png_ptr, png_user_transform_ptr write_user_transform_fn));
 78.2015 +#endif
 78.2016 +
 78.2017 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 78.2018 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 78.2019 +    defined(PNG_LEGACY_SUPPORTED)
 78.2020 +extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp
 78.2021 +   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,
 78.2022 +   int user_transform_channels));
 78.2023 +/* Return the user pointer associated with the user transform functions */
 78.2024 +extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)
 78.2025 +   PNGARG((png_structp png_ptr));
 78.2026 +#endif
 78.2027 +
 78.2028 +#ifdef PNG_USER_CHUNKS_SUPPORTED
 78.2029 +extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,
 78.2030 +   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
 78.2031 +extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp
 78.2032 +   png_ptr));
 78.2033 +#endif
 78.2034 +
 78.2035 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.2036 +/* Sets the function callbacks for the push reader, and a pointer to a
 78.2037 + * user-defined structure available to the callback functions.
 78.2038 + */
 78.2039 +extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,
 78.2040 +   png_voidp progressive_ptr,
 78.2041 +   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
 78.2042 +   png_progressive_end_ptr end_fn));
 78.2043 +
 78.2044 +/* returns the user pointer associated with the push read functions */
 78.2045 +extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
 78.2046 +   PNGARG((png_structp png_ptr));
 78.2047 +
 78.2048 +/* function to be called when data becomes available */
 78.2049 +extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,
 78.2050 +   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));
 78.2051 +
 78.2052 +/* function that combines rows.  Not very much different than the
 78.2053 + * png_combine_row() call.  Is this even used?????
 78.2054 + */
 78.2055 +extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,
 78.2056 +   png_bytep old_row, png_bytep new_row));
 78.2057 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 78.2058 +
 78.2059 +extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,
 78.2060 +   png_uint_32 size));
 78.2061 +
 78.2062 +#if defined(PNG_1_0_X)
 78.2063 +#  define png_malloc_warn png_malloc
 78.2064 +#else
 78.2065 +/* Added at libpng version 1.2.4 */
 78.2066 +extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,
 78.2067 +   png_uint_32 size));
 78.2068 +#endif
 78.2069 +
 78.2070 +/* frees a pointer allocated by png_malloc() */
 78.2071 +extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));
 78.2072 +
 78.2073 +#if defined(PNG_1_0_X)
 78.2074 +/* Function to allocate memory for zlib. */
 78.2075 +extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items,
 78.2076 +   uInt size));
 78.2077 +
 78.2078 +/* Function to free memory for zlib */
 78.2079 +extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr));
 78.2080 +#endif
 78.2081 +
 78.2082 +/* Free data that was allocated internally */
 78.2083 +extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,
 78.2084 +   png_infop info_ptr, png_uint_32 free_me, int num));
 78.2085 +#ifdef PNG_FREE_ME_SUPPORTED
 78.2086 +/* Reassign responsibility for freeing existing data, whether allocated
 78.2087 + * by libpng or by the application */
 78.2088 +extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,
 78.2089 +   png_infop info_ptr, int freer, png_uint_32 mask));
 78.2090 +#endif
 78.2091 +/* assignments for png_data_freer */
 78.2092 +#define PNG_DESTROY_WILL_FREE_DATA 1
 78.2093 +#define PNG_SET_WILL_FREE_DATA 1
 78.2094 +#define PNG_USER_WILL_FREE_DATA 2
 78.2095 +/* Flags for png_ptr->free_me and info_ptr->free_me */
 78.2096 +#define PNG_FREE_HIST 0x0008
 78.2097 +#define PNG_FREE_ICCP 0x0010
 78.2098 +#define PNG_FREE_SPLT 0x0020
 78.2099 +#define PNG_FREE_ROWS 0x0040
 78.2100 +#define PNG_FREE_PCAL 0x0080
 78.2101 +#define PNG_FREE_SCAL 0x0100
 78.2102 +#define PNG_FREE_UNKN 0x0200
 78.2103 +#define PNG_FREE_LIST 0x0400
 78.2104 +#define PNG_FREE_PLTE 0x1000
 78.2105 +#define PNG_FREE_TRNS 0x2000
 78.2106 +#define PNG_FREE_TEXT 0x4000
 78.2107 +#define PNG_FREE_ALL  0x7fff
 78.2108 +#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */
 78.2109 +
 78.2110 +#ifdef PNG_USER_MEM_SUPPORTED
 78.2111 +extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,
 78.2112 +   png_uint_32 size));
 78.2113 +extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,
 78.2114 +   png_voidp ptr));
 78.2115 +#endif
 78.2116 +
 78.2117 +extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr,
 78.2118 +   png_voidp s1, png_voidp s2, png_uint_32 size));
 78.2119 +
 78.2120 +extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr,
 78.2121 +   png_voidp s1, int value, png_uint_32 size));
 78.2122 +
 78.2123 +#if defined(USE_FAR_KEYWORD)  /* memory model conversion function */
 78.2124 +extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,
 78.2125 +   int check));
 78.2126 +#endif /* USE_FAR_KEYWORD */
 78.2127 +
 78.2128 +#ifndef PNG_NO_ERROR_TEXT
 78.2129 +/* Fatal error in PNG image of libpng - can't continue */
 78.2130 +extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,
 78.2131 +   png_const_charp error_message));
 78.2132 +
 78.2133 +/* The same, but the chunk name is prepended to the error string. */
 78.2134 +extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,
 78.2135 +   png_const_charp error_message));
 78.2136 +#else
 78.2137 +/* Fatal error in PNG image of libpng - can't continue */
 78.2138 +extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr));
 78.2139 +#endif
 78.2140 +
 78.2141 +#ifndef PNG_NO_WARNINGS
 78.2142 +/* Non-fatal error in libpng.  Can continue, but may have a problem. */
 78.2143 +extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,
 78.2144 +   png_const_charp warning_message));
 78.2145 +
 78.2146 +#ifdef PNG_READ_SUPPORTED
 78.2147 +/* Non-fatal error in libpng, chunk name is prepended to message. */
 78.2148 +extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,
 78.2149 +   png_const_charp warning_message));
 78.2150 +#endif /* PNG_READ_SUPPORTED */
 78.2151 +#endif /* PNG_NO_WARNINGS */
 78.2152 +
 78.2153 +/* The png_set_<chunk> functions are for storing values in the png_info_struct.
 78.2154 + * Similarly, the png_get_<chunk> calls are used to read values from the
 78.2155 + * png_info_struct, either storing the parameters in the passed variables, or
 78.2156 + * setting pointers into the png_info_struct where the data is stored.  The
 78.2157 + * png_get_<chunk> functions return a non-zero value if the data was available
 78.2158 + * in info_ptr, or return zero and do not change any of the parameters if the
 78.2159 + * data was not available.
 78.2160 + *
 78.2161 + * These functions should be used instead of directly accessing png_info
 78.2162 + * to avoid problems with future changes in the size and internal layout of
 78.2163 + * png_info_struct.
 78.2164 + */
 78.2165 +/* Returns "flag" if chunk data is valid in info_ptr. */
 78.2166 +extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,
 78.2167 +png_infop info_ptr, png_uint_32 flag));
 78.2168 +
 78.2169 +/* Returns number of bytes needed to hold a transformed row. */
 78.2170 +extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr,
 78.2171 +png_infop info_ptr));
 78.2172 +
 78.2173 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 78.2174 +/* Returns row_pointers, which is an array of pointers to scanlines that was
 78.2175 +returned from png_read_png(). */
 78.2176 +extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,
 78.2177 +png_infop info_ptr));
 78.2178 +/* Set row_pointers, which is an array of pointers to scanlines for use
 78.2179 +by png_write_png(). */
 78.2180 +extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,
 78.2181 +   png_infop info_ptr, png_bytepp row_pointers));
 78.2182 +#endif
 78.2183 +
 78.2184 +/* Returns number of color channels in image. */
 78.2185 +extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,
 78.2186 +png_infop info_ptr));
 78.2187 +
 78.2188 +#ifdef PNG_EASY_ACCESS_SUPPORTED
 78.2189 +/* Returns image width in pixels. */
 78.2190 +extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp
 78.2191 +png_ptr, png_infop info_ptr));
 78.2192 +
 78.2193 +/* Returns image height in pixels. */
 78.2194 +extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp
 78.2195 +png_ptr, png_infop info_ptr));
 78.2196 +
 78.2197 +/* Returns image bit_depth. */
 78.2198 +extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp
 78.2199 +png_ptr, png_infop info_ptr));
 78.2200 +
 78.2201 +/* Returns image color_type. */
 78.2202 +extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp
 78.2203 +png_ptr, png_infop info_ptr));
 78.2204 +
 78.2205 +/* Returns image filter_type. */
 78.2206 +extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp
 78.2207 +png_ptr, png_infop info_ptr));
 78.2208 +
 78.2209 +/* Returns image interlace_type. */
 78.2210 +extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp
 78.2211 +png_ptr, png_infop info_ptr));
 78.2212 +
 78.2213 +/* Returns image compression_type. */
 78.2214 +extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp
 78.2215 +png_ptr, png_infop info_ptr));
 78.2216 +
 78.2217 +/* Returns image resolution in pixels per meter, from pHYs chunk data. */
 78.2218 +extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp
 78.2219 +png_ptr, png_infop info_ptr));
 78.2220 +extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp
 78.2221 +png_ptr, png_infop info_ptr));
 78.2222 +extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp
 78.2223 +png_ptr, png_infop info_ptr));
 78.2224 +
 78.2225 +/* Returns pixel aspect ratio, computed from pHYs chunk data.  */
 78.2226 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2227 +extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp
 78.2228 +png_ptr, png_infop info_ptr));
 78.2229 +#endif
 78.2230 +
 78.2231 +/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */
 78.2232 +extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp
 78.2233 +png_ptr, png_infop info_ptr));
 78.2234 +extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp
 78.2235 +png_ptr, png_infop info_ptr));
 78.2236 +extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp
 78.2237 +png_ptr, png_infop info_ptr));
 78.2238 +extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp
 78.2239 +png_ptr, png_infop info_ptr));
 78.2240 +
 78.2241 +#endif /* PNG_EASY_ACCESS_SUPPORTED */
 78.2242 +
 78.2243 +/* Returns pointer to signature string read from PNG header */
 78.2244 +extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,
 78.2245 +png_infop info_ptr));
 78.2246 +
 78.2247 +#if defined(PNG_bKGD_SUPPORTED)
 78.2248 +extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,
 78.2249 +   png_infop info_ptr, png_color_16p *background));
 78.2250 +#endif
 78.2251 +
 78.2252 +#if defined(PNG_bKGD_SUPPORTED)
 78.2253 +extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,
 78.2254 +   png_infop info_ptr, png_color_16p background));
 78.2255 +#endif
 78.2256 +
 78.2257 +#if defined(PNG_cHRM_SUPPORTED)
 78.2258 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2259 +extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,
 78.2260 +   png_infop info_ptr, double *white_x, double *white_y, double *red_x,
 78.2261 +   double *red_y, double *green_x, double *green_y, double *blue_x,
 78.2262 +   double *blue_y));
 78.2263 +#endif
 78.2264 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.2265 +extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,
 78.2266 +   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point
 78.2267 +   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,
 78.2268 +   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point
 78.2269 +   *int_blue_x, png_fixed_point *int_blue_y));
 78.2270 +#endif
 78.2271 +#endif
 78.2272 +
 78.2273 +#if defined(PNG_cHRM_SUPPORTED)
 78.2274 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2275 +extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,
 78.2276 +   png_infop info_ptr, double white_x, double white_y, double red_x,
 78.2277 +   double red_y, double green_x, double green_y, double blue_x, double blue_y));
 78.2278 +#endif
 78.2279 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.2280 +extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,
 78.2281 +   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,
 78.2282 +   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
 78.2283 +   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
 78.2284 +   png_fixed_point int_blue_y));
 78.2285 +#endif
 78.2286 +#endif
 78.2287 +
 78.2288 +#if defined(PNG_gAMA_SUPPORTED)
 78.2289 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2290 +extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,
 78.2291 +   png_infop info_ptr, double *file_gamma));
 78.2292 +#endif
 78.2293 +extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,
 78.2294 +   png_infop info_ptr, png_fixed_point *int_file_gamma));
 78.2295 +#endif
 78.2296 +
 78.2297 +#if defined(PNG_gAMA_SUPPORTED)
 78.2298 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2299 +extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,
 78.2300 +   png_infop info_ptr, double file_gamma));
 78.2301 +#endif
 78.2302 +extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,
 78.2303 +   png_infop info_ptr, png_fixed_point int_file_gamma));
 78.2304 +#endif
 78.2305 +
 78.2306 +#if defined(PNG_hIST_SUPPORTED)
 78.2307 +extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,
 78.2308 +   png_infop info_ptr, png_uint_16p *hist));
 78.2309 +#endif
 78.2310 +
 78.2311 +#if defined(PNG_hIST_SUPPORTED)
 78.2312 +extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,
 78.2313 +   png_infop info_ptr, png_uint_16p hist));
 78.2314 +#endif
 78.2315 +
 78.2316 +extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,
 78.2317 +   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
 78.2318 +   int *bit_depth, int *color_type, int *interlace_method,
 78.2319 +   int *compression_method, int *filter_method));
 78.2320 +
 78.2321 +extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
 78.2322 +   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
 78.2323 +   int color_type, int interlace_method, int compression_method,
 78.2324 +   int filter_method));
 78.2325 +
 78.2326 +#if defined(PNG_oFFs_SUPPORTED)
 78.2327 +extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,
 78.2328 +   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,
 78.2329 +   int *unit_type));
 78.2330 +#endif
 78.2331 +
 78.2332 +#if defined(PNG_oFFs_SUPPORTED)
 78.2333 +extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,
 78.2334 +   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,
 78.2335 +   int unit_type));
 78.2336 +#endif
 78.2337 +
 78.2338 +#if defined(PNG_pCAL_SUPPORTED)
 78.2339 +extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,
 78.2340 +   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,
 78.2341 +   int *type, int *nparams, png_charp *units, png_charpp *params));
 78.2342 +#endif
 78.2343 +
 78.2344 +#if defined(PNG_pCAL_SUPPORTED)
 78.2345 +extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,
 78.2346 +   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,
 78.2347 +   int type, int nparams, png_charp units, png_charpp params));
 78.2348 +#endif
 78.2349 +
 78.2350 +#if defined(PNG_pHYs_SUPPORTED)
 78.2351 +extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,
 78.2352 +   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
 78.2353 +#endif
 78.2354 +
 78.2355 +#if defined(PNG_pHYs_SUPPORTED)
 78.2356 +extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,
 78.2357 +   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));
 78.2358 +#endif
 78.2359 +
 78.2360 +extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,
 78.2361 +   png_infop info_ptr, png_colorp *palette, int *num_palette));
 78.2362 +
 78.2363 +extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,
 78.2364 +   png_infop info_ptr, png_colorp palette, int num_palette));
 78.2365 +
 78.2366 +#if defined(PNG_sBIT_SUPPORTED)
 78.2367 +extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,
 78.2368 +   png_infop info_ptr, png_color_8p *sig_bit));
 78.2369 +#endif
 78.2370 +
 78.2371 +#if defined(PNG_sBIT_SUPPORTED)
 78.2372 +extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,
 78.2373 +   png_infop info_ptr, png_color_8p sig_bit));
 78.2374 +#endif
 78.2375 +
 78.2376 +#if defined(PNG_sRGB_SUPPORTED)
 78.2377 +extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,
 78.2378 +   png_infop info_ptr, int *intent));
 78.2379 +#endif
 78.2380 +
 78.2381 +#if defined(PNG_sRGB_SUPPORTED)
 78.2382 +extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,
 78.2383 +   png_infop info_ptr, int intent));
 78.2384 +extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,
 78.2385 +   png_infop info_ptr, int intent));
 78.2386 +#endif
 78.2387 +
 78.2388 +#if defined(PNG_iCCP_SUPPORTED)
 78.2389 +extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,
 78.2390 +   png_infop info_ptr, png_charpp name, int *compression_type,
 78.2391 +   png_charpp profile, png_uint_32 *proflen));
 78.2392 +   /* Note to maintainer: profile should be png_bytepp */
 78.2393 +#endif
 78.2394 +
 78.2395 +#if defined(PNG_iCCP_SUPPORTED)
 78.2396 +extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,
 78.2397 +   png_infop info_ptr, png_charp name, int compression_type,
 78.2398 +   png_charp profile, png_uint_32 proflen));
 78.2399 +   /* Note to maintainer: profile should be png_bytep */
 78.2400 +#endif
 78.2401 +
 78.2402 +#if defined(PNG_sPLT_SUPPORTED)
 78.2403 +extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,
 78.2404 +   png_infop info_ptr, png_sPLT_tpp entries));
 78.2405 +#endif
 78.2406 +
 78.2407 +#if defined(PNG_sPLT_SUPPORTED)
 78.2408 +extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,
 78.2409 +   png_infop info_ptr, png_sPLT_tp entries, int nentries));
 78.2410 +#endif
 78.2411 +
 78.2412 +#if defined(PNG_TEXT_SUPPORTED)
 78.2413 +/* png_get_text also returns the number of text chunks in *num_text */
 78.2414 +extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,
 78.2415 +   png_infop info_ptr, png_textp *text_ptr, int *num_text));
 78.2416 +#endif
 78.2417 +
 78.2418 +/*
 78.2419 + *  Note while png_set_text() will accept a structure whose text,
 78.2420 + *  language, and  translated keywords are NULL pointers, the structure
 78.2421 + *  returned by png_get_text will always contain regular
 78.2422 + *  zero-terminated C strings.  They might be empty strings but
 78.2423 + *  they will never be NULL pointers.
 78.2424 + */
 78.2425 +
 78.2426 +#if defined(PNG_TEXT_SUPPORTED)
 78.2427 +extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,
 78.2428 +   png_infop info_ptr, png_textp text_ptr, int num_text));
 78.2429 +#endif
 78.2430 +
 78.2431 +#if defined(PNG_tIME_SUPPORTED)
 78.2432 +extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,
 78.2433 +   png_infop info_ptr, png_timep *mod_time));
 78.2434 +#endif
 78.2435 +
 78.2436 +#if defined(PNG_tIME_SUPPORTED)
 78.2437 +extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,
 78.2438 +   png_infop info_ptr, png_timep mod_time));
 78.2439 +#endif
 78.2440 +
 78.2441 +#if defined(PNG_tRNS_SUPPORTED)
 78.2442 +extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,
 78.2443 +   png_infop info_ptr, png_bytep *trans, int *num_trans,
 78.2444 +   png_color_16p *trans_values));
 78.2445 +#endif
 78.2446 +
 78.2447 +#if defined(PNG_tRNS_SUPPORTED)
 78.2448 +extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,
 78.2449 +   png_infop info_ptr, png_bytep trans, int num_trans,
 78.2450 +   png_color_16p trans_values));
 78.2451 +#endif
 78.2452 +
 78.2453 +#if defined(PNG_tRNS_SUPPORTED)
 78.2454 +#endif
 78.2455 +
 78.2456 +#if defined(PNG_sCAL_SUPPORTED)
 78.2457 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2458 +extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,
 78.2459 +   png_infop info_ptr, int *unit, double *width, double *height));
 78.2460 +#else
 78.2461 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.2462 +extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,
 78.2463 +   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));
 78.2464 +#endif
 78.2465 +#endif
 78.2466 +#endif /* PNG_sCAL_SUPPORTED */
 78.2467 +
 78.2468 +#if defined(PNG_sCAL_SUPPORTED)
 78.2469 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.2470 +extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,
 78.2471 +   png_infop info_ptr, int unit, double width, double height));
 78.2472 +#else
 78.2473 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.2474 +extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,
 78.2475 +   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));
 78.2476 +#endif
 78.2477 +#endif
 78.2478 +#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */
 78.2479 +
 78.2480 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 78.2481 +/* provide a list of chunks and how they are to be handled, if the built-in
 78.2482 +   handling or default unknown chunk handling is not desired.  Any chunks not
 78.2483 +   listed will be handled in the default manner.  The IHDR and IEND chunks
 78.2484 +   must not be listed.
 78.2485 +      keep = 0: follow default behaviour
 78.2486 +           = 1: do not keep
 78.2487 +           = 2: keep only if safe-to-copy
 78.2488 +           = 3: keep even if unsafe-to-copy
 78.2489 +*/
 78.2490 +extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp
 78.2491 +   png_ptr, int keep, png_bytep chunk_list, int num_chunks));
 78.2492 +extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,
 78.2493 +   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));
 78.2494 +extern PNG_EXPORT(void, png_set_unknown_chunk_location)
 78.2495 +   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));
 78.2496 +extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp
 78.2497 +   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));
 78.2498 +#endif
 78.2499 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 78.2500 +PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep
 78.2501 +   chunk_name));
 78.2502 +#endif
 78.2503 +
 78.2504 +/* Png_free_data() will turn off the "valid" flag for anything it frees.
 78.2505 +   If you need to turn it off for a chunk that your application has freed,
 78.2506 +   you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */
 78.2507 +extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,
 78.2508 +   png_infop info_ptr, int mask));
 78.2509 +
 78.2510 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 78.2511 +/* The "params" pointer is currently not used and is for future expansion. */
 78.2512 +extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,
 78.2513 +                        png_infop info_ptr,
 78.2514 +                        int transforms,
 78.2515 +                        png_voidp params));
 78.2516 +extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,
 78.2517 +                        png_infop info_ptr,
 78.2518 +                        int transforms,
 78.2519 +                        png_voidp params));
 78.2520 +#endif
 78.2521 +
 78.2522 +/* Define PNG_DEBUG at compile time for debugging information.  Higher
 78.2523 + * numbers for PNG_DEBUG mean more debugging information.  This has
 78.2524 + * only been added since version 0.95 so it is not implemented throughout
 78.2525 + * libpng yet, but more support will be added as needed.
 78.2526 + */
 78.2527 +#ifdef PNG_DEBUG
 78.2528 +#if (PNG_DEBUG > 0)
 78.2529 +#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER)
 78.2530 +#include <crtdbg.h>
 78.2531 +#if (PNG_DEBUG > 1)
 78.2532 +#define png_debug(l,m)  _RPT0(_CRT_WARN,m)
 78.2533 +#define png_debug1(l,m,p1)  _RPT1(_CRT_WARN,m,p1)
 78.2534 +#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m,p1,p2)
 78.2535 +#endif
 78.2536 +#else /* PNG_DEBUG_FILE || !_MSC_VER */
 78.2537 +#ifndef PNG_DEBUG_FILE
 78.2538 +#define PNG_DEBUG_FILE stderr
 78.2539 +#endif /* PNG_DEBUG_FILE */
 78.2540 +#if (PNG_DEBUG > 1)
 78.2541 +#define png_debug(l,m) \
 78.2542 +{ \
 78.2543 +     int num_tabs=l; \
 78.2544 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 78.2545 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \
 78.2546 +}
 78.2547 +#define png_debug1(l,m,p1) \
 78.2548 +{ \
 78.2549 +     int num_tabs=l; \
 78.2550 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 78.2551 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \
 78.2552 +}
 78.2553 +#define png_debug2(l,m,p1,p2) \
 78.2554 +{ \
 78.2555 +     int num_tabs=l; \
 78.2556 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 78.2557 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \
 78.2558 +}
 78.2559 +#endif /* (PNG_DEBUG > 1) */
 78.2560 +#endif /* _MSC_VER */
 78.2561 +#endif /* (PNG_DEBUG > 0) */
 78.2562 +#endif /* PNG_DEBUG */
 78.2563 +#ifndef png_debug
 78.2564 +#define png_debug(l, m)
 78.2565 +#endif
 78.2566 +#ifndef png_debug1
 78.2567 +#define png_debug1(l, m, p1)
 78.2568 +#endif
 78.2569 +#ifndef png_debug2
 78.2570 +#define png_debug2(l, m, p1, p2)
 78.2571 +#endif
 78.2572 +
 78.2573 +extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
 78.2574 +extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));
 78.2575 +extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr));
 78.2576 +extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));
 78.2577 +
 78.2578 +#ifdef PNG_MNG_FEATURES_SUPPORTED
 78.2579 +extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp
 78.2580 +   png_ptr, png_uint_32 mng_features_permitted));
 78.2581 +#endif
 78.2582 +
 78.2583 +/* For use in png_set_keep_unknown, added to version 1.2.6 */
 78.2584 +#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
 78.2585 +#define PNG_HANDLE_CHUNK_NEVER        1
 78.2586 +#define PNG_HANDLE_CHUNK_IF_SAFE      2
 78.2587 +#define PNG_HANDLE_CHUNK_ALWAYS       3
 78.2588 +
 78.2589 +/* Added to version 1.2.0 */
 78.2590 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 78.2591 +#if defined(PNG_MMX_CODE_SUPPORTED)
 78.2592 +#define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED  0x01  /* not user-settable */
 78.2593 +#define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU    0x02  /* not user-settable */
 78.2594 +#define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  0x04
 78.2595 +#define PNG_ASM_FLAG_MMX_READ_INTERLACE    0x08
 78.2596 +#define PNG_ASM_FLAG_MMX_READ_FILTER_SUB   0x10
 78.2597 +#define PNG_ASM_FLAG_MMX_READ_FILTER_UP    0x20
 78.2598 +#define PNG_ASM_FLAG_MMX_READ_FILTER_AVG   0x40
 78.2599 +#define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80
 78.2600 +#define PNG_ASM_FLAGS_INITIALIZED          0x80000000  /* not user-settable */
 78.2601 +
 78.2602 +#define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  \
 78.2603 +                           | PNG_ASM_FLAG_MMX_READ_INTERLACE    \
 78.2604 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_SUB   \
 78.2605 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_UP    \
 78.2606 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_AVG   \
 78.2607 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH )
 78.2608 +#define PNG_MMX_WRITE_FLAGS ( 0 )
 78.2609 +
 78.2610 +#define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \
 78.2611 +                      | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU   \
 78.2612 +                      | PNG_MMX_READ_FLAGS                \
 78.2613 +                      | PNG_MMX_WRITE_FLAGS )
 78.2614 +
 78.2615 +#define PNG_SELECT_READ   1
 78.2616 +#define PNG_SELECT_WRITE  2
 78.2617 +#endif /* PNG_MMX_CODE_SUPPORTED */
 78.2618 +
 78.2619 +#if !defined(PNG_1_0_X)
 78.2620 +/* pngget.c */
 78.2621 +extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask)
 78.2622 +   PNGARG((int flag_select, int *compilerID));
 78.2623 +
 78.2624 +/* pngget.c */
 78.2625 +extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask)
 78.2626 +   PNGARG((int flag_select));
 78.2627 +
 78.2628 +/* pngget.c */
 78.2629 +extern PNG_EXPORT(png_uint_32,png_get_asm_flags)
 78.2630 +   PNGARG((png_structp png_ptr));
 78.2631 +
 78.2632 +/* pngget.c */
 78.2633 +extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold)
 78.2634 +   PNGARG((png_structp png_ptr));
 78.2635 +
 78.2636 +/* pngget.c */
 78.2637 +extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold)
 78.2638 +   PNGARG((png_structp png_ptr));
 78.2639 +
 78.2640 +/* pngset.c */
 78.2641 +extern PNG_EXPORT(void,png_set_asm_flags)
 78.2642 +   PNGARG((png_structp png_ptr, png_uint_32 asm_flags));
 78.2643 +
 78.2644 +/* pngset.c */
 78.2645 +extern PNG_EXPORT(void,png_set_mmx_thresholds)
 78.2646 +   PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold,
 78.2647 +   png_uint_32 mmx_rowbytes_threshold));
 78.2648 +
 78.2649 +#endif /* PNG_1_0_X */
 78.2650 +
 78.2651 +#if !defined(PNG_1_0_X)
 78.2652 +/* png.c, pnggccrd.c, or pngvcrd.c */
 78.2653 +extern PNG_EXPORT(int,png_mmx_support) PNGARG((void));
 78.2654 +#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */
 78.2655 +
 78.2656 +/* Strip the prepended error numbers ("#nnn ") from error and warning
 78.2657 + * messages before passing them to the error or warning handler. */
 78.2658 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
 78.2659 +extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp
 78.2660 +   png_ptr, png_uint_32 strip_mode));
 78.2661 +#endif
 78.2662 +
 78.2663 +#endif /* PNG_1_0_X */
 78.2664 +
 78.2665 +/* Added at libpng-1.2.6 */
 78.2666 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 78.2667 +extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp
 78.2668 +   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));
 78.2669 +extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp
 78.2670 +   png_ptr));
 78.2671 +extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp
 78.2672 +   png_ptr));
 78.2673 +#endif
 78.2674 +
 78.2675 +
 78.2676 +/* Maintainer: Put new public prototypes here ^, in libpng.3, and project defs */
 78.2677 +
 78.2678 +#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
 78.2679 +/* With these routines we avoid an integer divide, which will be slower on
 78.2680 + * most machines.  However, it does take more operations than the corresponding
 78.2681 + * divide method, so it may be slower on a few RISC systems.  There are two
 78.2682 + * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
 78.2683 + *
 78.2684 + * Note that the rounding factors are NOT supposed to be the same!  128 and
 78.2685 + * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
 78.2686 + * standard method.
 78.2687 + *
 78.2688 + * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
 78.2689 + */
 78.2690 +
 78.2691 + /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */
 78.2692 +
 78.2693 +#  define png_composite(composite, fg, alpha, bg)                            \
 78.2694 +     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \
 78.2695 +                        +        (png_uint_16)(bg)*(png_uint_16)(255 -       \
 78.2696 +                        (png_uint_16)(alpha)) + (png_uint_16)128);           \
 78.2697 +       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
 78.2698 +
 78.2699 +#  define png_composite_16(composite, fg, alpha, bg)                         \
 78.2700 +     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \
 78.2701 +                        + (png_uint_32)(bg)*(png_uint_32)(65535L -           \
 78.2702 +                        (png_uint_32)(alpha)) + (png_uint_32)32768L);        \
 78.2703 +       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
 78.2704 +
 78.2705 +#else  /* standard method using integer division */
 78.2706 +
 78.2707 +#  define png_composite(composite, fg, alpha, bg)                            \
 78.2708 +     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \
 78.2709 +       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
 78.2710 +       (png_uint_16)127) / 255)
 78.2711 +
 78.2712 +#  define png_composite_16(composite, fg, alpha, bg)                         \
 78.2713 +     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
 78.2714 +       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \
 78.2715 +       (png_uint_32)32767) / (png_uint_32)65535L)
 78.2716 +
 78.2717 +#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
 78.2718 +
 78.2719 +/* Inline macros to do direct reads of bytes from the input buffer.  These
 78.2720 + * require that you are using an architecture that uses PNG byte ordering
 78.2721 + * (MSB first) and supports unaligned data storage.  I think that PowerPC
 78.2722 + * in big-endian mode and 680x0 are the only ones that will support this.
 78.2723 + * The x86 line of processors definitely do not.  The png_get_int_32()
 78.2724 + * routine also assumes we are using two's complement format for negative
 78.2725 + * values, which is almost certainly true.
 78.2726 + */
 78.2727 +#if defined(PNG_READ_BIG_ENDIAN_SUPPORTED)
 78.2728 +#  define png_get_uint_32(buf) ( *((png_uint_32p) (buf)))
 78.2729 +#  define png_get_uint_16(buf) ( *((png_uint_16p) (buf)))
 78.2730 +#  define png_get_int_32(buf)  ( *((png_int_32p)  (buf)))
 78.2731 +#else
 78.2732 +extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
 78.2733 +extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
 78.2734 +extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
 78.2735 +#endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */
 78.2736 +extern PNG_EXPORT(png_uint_32,png_get_uint_31)
 78.2737 +  PNGARG((png_structp png_ptr, png_bytep buf));
 78.2738 +/* No png_get_int_16 -- may be added if there's a real need for it. */
 78.2739 +
 78.2740 +/* Place a 32-bit number into a buffer in PNG byte order (big-endian).
 78.2741 + */
 78.2742 +extern PNG_EXPORT(void,png_save_uint_32)
 78.2743 +   PNGARG((png_bytep buf, png_uint_32 i));
 78.2744 +extern PNG_EXPORT(void,png_save_int_32)
 78.2745 +   PNGARG((png_bytep buf, png_int_32 i));
 78.2746 +
 78.2747 +/* Place a 16-bit number into a buffer in PNG byte order.
 78.2748 + * The parameter is declared unsigned int, not png_uint_16,
 78.2749 + * just to avoid potential problems on pre-ANSI C compilers.
 78.2750 + */
 78.2751 +extern PNG_EXPORT(void,png_save_uint_16)
 78.2752 +   PNGARG((png_bytep buf, unsigned int i));
 78.2753 +/* No png_save_int_16 -- may be added if there's a real need for it. */
 78.2754 +
 78.2755 +/* ************************************************************************* */
 78.2756 +
 78.2757 +/* These next functions are used internally in the code.  They generally
 78.2758 + * shouldn't be used unless you are writing code to add or replace some
 78.2759 + * functionality in libpng.  More information about most functions can
 78.2760 + * be found in the files where the functions are located.
 78.2761 + */
 78.2762 +
 78.2763 +
 78.2764 +/* Various modes of operation, that are visible to applications because
 78.2765 + * they are used for unknown chunk location.
 78.2766 + */
 78.2767 +#define PNG_HAVE_IHDR               0x01
 78.2768 +#define PNG_HAVE_PLTE               0x02
 78.2769 +#define PNG_HAVE_IDAT               0x04
 78.2770 +#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */
 78.2771 +#define PNG_HAVE_IEND               0x10
 78.2772 +
 78.2773 +#if defined(PNG_INTERNAL)
 78.2774 +
 78.2775 +/* More modes of operation.  Note that after an init, mode is set to
 78.2776 + * zero automatically when the structure is created.
 78.2777 + */
 78.2778 +#define PNG_HAVE_gAMA               0x20
 78.2779 +#define PNG_HAVE_cHRM               0x40
 78.2780 +#define PNG_HAVE_sRGB               0x80
 78.2781 +#define PNG_HAVE_CHUNK_HEADER      0x100
 78.2782 +#define PNG_WROTE_tIME             0x200
 78.2783 +#define PNG_WROTE_INFO_BEFORE_PLTE 0x400
 78.2784 +#define PNG_BACKGROUND_IS_GRAY     0x800
 78.2785 +#define PNG_HAVE_PNG_SIGNATURE    0x1000
 78.2786 +#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */
 78.2787 +
 78.2788 +/* flags for the transformations the PNG library does on the image data */
 78.2789 +#define PNG_BGR                0x0001
 78.2790 +#define PNG_INTERLACE          0x0002
 78.2791 +#define PNG_PACK               0x0004
 78.2792 +#define PNG_SHIFT              0x0008
 78.2793 +#define PNG_SWAP_BYTES         0x0010
 78.2794 +#define PNG_INVERT_MONO        0x0020
 78.2795 +#define PNG_DITHER             0x0040
 78.2796 +#define PNG_BACKGROUND         0x0080
 78.2797 +#define PNG_BACKGROUND_EXPAND  0x0100
 78.2798 +                          /*   0x0200 unused */
 78.2799 +#define PNG_16_TO_8            0x0400
 78.2800 +#define PNG_RGBA               0x0800
 78.2801 +#define PNG_EXPAND             0x1000
 78.2802 +#define PNG_GAMMA              0x2000
 78.2803 +#define PNG_GRAY_TO_RGB        0x4000
 78.2804 +#define PNG_FILLER             0x8000L
 78.2805 +#define PNG_PACKSWAP          0x10000L
 78.2806 +#define PNG_SWAP_ALPHA        0x20000L
 78.2807 +#define PNG_STRIP_ALPHA       0x40000L
 78.2808 +#define PNG_INVERT_ALPHA      0x80000L
 78.2809 +#define PNG_USER_TRANSFORM   0x100000L
 78.2810 +#define PNG_RGB_TO_GRAY_ERR  0x200000L
 78.2811 +#define PNG_RGB_TO_GRAY_WARN 0x400000L
 78.2812 +#define PNG_RGB_TO_GRAY      0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
 78.2813 +                       /*    0x800000L     Unused */
 78.2814 +#define PNG_ADD_ALPHA       0x1000000L  /* Added to libpng-1.2.7 */
 78.2815 +#define PNG_EXPAND_tRNS     0x2000000L  /* Added to libpng-1.2.9 */
 78.2816 +                       /*   0x4000000L  unused */
 78.2817 +                       /*   0x8000000L  unused */
 78.2818 +                       /*  0x10000000L  unused */
 78.2819 +                       /*  0x20000000L  unused */
 78.2820 +                       /*  0x40000000L  unused */
 78.2821 +
 78.2822 +/* flags for png_create_struct */
 78.2823 +#define PNG_STRUCT_PNG   0x0001
 78.2824 +#define PNG_STRUCT_INFO  0x0002
 78.2825 +
 78.2826 +/* Scaling factor for filter heuristic weighting calculations */
 78.2827 +#define PNG_WEIGHT_SHIFT 8
 78.2828 +#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
 78.2829 +#define PNG_COST_SHIFT 3
 78.2830 +#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))
 78.2831 +
 78.2832 +/* flags for the png_ptr->flags rather than declaring a byte for each one */
 78.2833 +#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY     0x0001
 78.2834 +#define PNG_FLAG_ZLIB_CUSTOM_LEVEL        0x0002
 78.2835 +#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL    0x0004
 78.2836 +#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS  0x0008
 78.2837 +#define PNG_FLAG_ZLIB_CUSTOM_METHOD       0x0010
 78.2838 +#define PNG_FLAG_ZLIB_FINISHED            0x0020
 78.2839 +#define PNG_FLAG_ROW_INIT                 0x0040
 78.2840 +#define PNG_FLAG_FILLER_AFTER             0x0080
 78.2841 +#define PNG_FLAG_CRC_ANCILLARY_USE        0x0100
 78.2842 +#define PNG_FLAG_CRC_ANCILLARY_NOWARN     0x0200
 78.2843 +#define PNG_FLAG_CRC_CRITICAL_USE         0x0400
 78.2844 +#define PNG_FLAG_CRC_CRITICAL_IGNORE      0x0800
 78.2845 +#define PNG_FLAG_FREE_PLTE                0x1000
 78.2846 +#define PNG_FLAG_FREE_TRNS                0x2000
 78.2847 +#define PNG_FLAG_FREE_HIST                0x4000
 78.2848 +#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS      0x8000L
 78.2849 +#define PNG_FLAG_KEEP_UNSAFE_CHUNKS       0x10000L
 78.2850 +#define PNG_FLAG_LIBRARY_MISMATCH         0x20000L
 78.2851 +#define PNG_FLAG_STRIP_ERROR_NUMBERS      0x40000L
 78.2852 +#define PNG_FLAG_STRIP_ERROR_TEXT         0x80000L
 78.2853 +#define PNG_FLAG_MALLOC_NULL_MEM_OK       0x100000L
 78.2854 +#define PNG_FLAG_ADD_ALPHA                0x200000L  /* Added to libpng-1.2.8 */
 78.2855 +#define PNG_FLAG_STRIP_ALPHA              0x400000L  /* Added to libpng-1.2.8 */
 78.2856 +                                  /*      0x800000L  unused */
 78.2857 +                                  /*     0x1000000L  unused */
 78.2858 +                                  /*     0x2000000L  unused */
 78.2859 +                                  /*     0x4000000L  unused */
 78.2860 +                                  /*     0x8000000L  unused */
 78.2861 +                                  /*    0x10000000L  unused */
 78.2862 +                                  /*    0x20000000L  unused */
 78.2863 +                                  /*    0x40000000L  unused */
 78.2864 +
 78.2865 +#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
 78.2866 +                                     PNG_FLAG_CRC_ANCILLARY_NOWARN)
 78.2867 +
 78.2868 +#define PNG_FLAG_CRC_CRITICAL_MASK  (PNG_FLAG_CRC_CRITICAL_USE | \
 78.2869 +                                     PNG_FLAG_CRC_CRITICAL_IGNORE)
 78.2870 +
 78.2871 +#define PNG_FLAG_CRC_MASK           (PNG_FLAG_CRC_ANCILLARY_MASK | \
 78.2872 +                                     PNG_FLAG_CRC_CRITICAL_MASK)
 78.2873 +
 78.2874 +/* save typing and make code easier to understand */
 78.2875 +
 78.2876 +#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
 78.2877 +   abs((int)((c1).green) - (int)((c2).green)) + \
 78.2878 +   abs((int)((c1).blue) - (int)((c2).blue)))
 78.2879 +
 78.2880 +/* Added to libpng-1.2.6 JB */
 78.2881 +#define PNG_ROWBYTES(pixel_bits, width) \
 78.2882 +    ((pixel_bits) >= 8 ? \
 78.2883 +    ((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \
 78.2884 +    (( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) )
 78.2885 +
 78.2886 +/* PNG_OUT_OF_RANGE returns true if value is outside the range
 78.2887 +   ideal-delta..ideal+delta.  Each argument is evaluated twice.
 78.2888 +   "ideal" and "delta" should be constants, normally simple
 78.2889 +   integers, "value" a variable. Added to libpng-1.2.6 JB */
 78.2890 +#define PNG_OUT_OF_RANGE(value, ideal, delta) \
 78.2891 +        ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
 78.2892 +
 78.2893 +/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
 78.2894 +#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
 78.2895 +/* place to hold the signature string for a PNG file. */
 78.2896 +#ifdef PNG_USE_GLOBAL_ARRAYS
 78.2897 +   PNG_EXPORT_VAR (PNG_CONST png_byte FARDATA) png_sig[8];
 78.2898 +#else
 78.2899 +#endif
 78.2900 +#endif /* PNG_NO_EXTERN */
 78.2901 +
 78.2902 +/* Constant strings for known chunk types.  If you need to add a chunk,
 78.2903 + * define the name here, and add an invocation of the macro in png.c and
 78.2904 + * wherever it's needed.
 78.2905 + */
 78.2906 +#define PNG_IHDR png_byte png_IHDR[5] = { 73,  72,  68,  82, '\0'}
 78.2907 +#define PNG_IDAT png_byte png_IDAT[5] = { 73,  68,  65,  84, '\0'}
 78.2908 +#define PNG_IEND png_byte png_IEND[5] = { 73,  69,  78,  68, '\0'}
 78.2909 +#define PNG_PLTE png_byte png_PLTE[5] = { 80,  76,  84,  69, '\0'}
 78.2910 +#define PNG_bKGD png_byte png_bKGD[5] = { 98,  75,  71,  68, '\0'}
 78.2911 +#define PNG_cHRM png_byte png_cHRM[5] = { 99,  72,  82,  77, '\0'}
 78.2912 +#define PNG_gAMA png_byte png_gAMA[5] = {103,  65,  77,  65, '\0'}
 78.2913 +#define PNG_hIST png_byte png_hIST[5] = {104,  73,  83,  84, '\0'}
 78.2914 +#define PNG_iCCP png_byte png_iCCP[5] = {105,  67,  67,  80, '\0'}
 78.2915 +#define PNG_iTXt png_byte png_iTXt[5] = {105,  84,  88, 116, '\0'}
 78.2916 +#define PNG_oFFs png_byte png_oFFs[5] = {111,  70,  70, 115, '\0'}
 78.2917 +#define PNG_pCAL png_byte png_pCAL[5] = {112,  67,  65,  76, '\0'}
 78.2918 +#define PNG_sCAL png_byte png_sCAL[5] = {115,  67,  65,  76, '\0'}
 78.2919 +#define PNG_pHYs png_byte png_pHYs[5] = {112,  72,  89, 115, '\0'}
 78.2920 +#define PNG_sBIT png_byte png_sBIT[5] = {115,  66,  73,  84, '\0'}
 78.2921 +#define PNG_sPLT png_byte png_sPLT[5] = {115,  80,  76,  84, '\0'}
 78.2922 +#define PNG_sRGB png_byte png_sRGB[5] = {115,  82,  71,  66, '\0'}
 78.2923 +#define PNG_tEXt png_byte png_tEXt[5] = {116,  69,  88, 116, '\0'}
 78.2924 +#define PNG_tIME png_byte png_tIME[5] = {116,  73,  77,  69, '\0'}
 78.2925 +#define PNG_tRNS png_byte png_tRNS[5] = {116,  82,  78,  83, '\0'}
 78.2926 +#define PNG_zTXt png_byte png_zTXt[5] = {122,  84,  88, 116, '\0'}
 78.2927 +
 78.2928 +#ifdef PNG_USE_GLOBAL_ARRAYS
 78.2929 +PNG_EXPORT_VAR (png_byte FARDATA) png_IHDR[5];
 78.2930 +PNG_EXPORT_VAR (png_byte FARDATA) png_IDAT[5];
 78.2931 +PNG_EXPORT_VAR (png_byte FARDATA) png_IEND[5];
 78.2932 +PNG_EXPORT_VAR (png_byte FARDATA) png_PLTE[5];
 78.2933 +PNG_EXPORT_VAR (png_byte FARDATA) png_bKGD[5];
 78.2934 +PNG_EXPORT_VAR (png_byte FARDATA) png_cHRM[5];
 78.2935 +PNG_EXPORT_VAR (png_byte FARDATA) png_gAMA[5];
 78.2936 +PNG_EXPORT_VAR (png_byte FARDATA) png_hIST[5];
 78.2937 +PNG_EXPORT_VAR (png_byte FARDATA) png_iCCP[5];
 78.2938 +PNG_EXPORT_VAR (png_byte FARDATA) png_iTXt[5];
 78.2939 +PNG_EXPORT_VAR (png_byte FARDATA) png_oFFs[5];
 78.2940 +PNG_EXPORT_VAR (png_byte FARDATA) png_pCAL[5];
 78.2941 +PNG_EXPORT_VAR (png_byte FARDATA) png_sCAL[5];
 78.2942 +PNG_EXPORT_VAR (png_byte FARDATA) png_pHYs[5];
 78.2943 +PNG_EXPORT_VAR (png_byte FARDATA) png_sBIT[5];
 78.2944 +PNG_EXPORT_VAR (png_byte FARDATA) png_sPLT[5];
 78.2945 +PNG_EXPORT_VAR (png_byte FARDATA) png_sRGB[5];
 78.2946 +PNG_EXPORT_VAR (png_byte FARDATA) png_tEXt[5];
 78.2947 +PNG_EXPORT_VAR (png_byte FARDATA) png_tIME[5];
 78.2948 +PNG_EXPORT_VAR (png_byte FARDATA) png_tRNS[5];
 78.2949 +PNG_EXPORT_VAR (png_byte FARDATA) png_zTXt[5];
 78.2950 +#endif /* PNG_USE_GLOBAL_ARRAYS */
 78.2951 +
 78.2952 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.2953 +/* Initialize png_ptr struct for reading, and allocate any other memory.
 78.2954 + * (old interface - DEPRECATED - use png_create_read_struct instead).
 78.2955 + */
 78.2956 +extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr));
 78.2957 +#undef png_read_init
 78.2958 +#define png_read_init(png_ptr) png_read_init_3(&png_ptr, \
 78.2959 +    PNG_LIBPNG_VER_STRING,  png_sizeof(png_struct));
 78.2960 +#endif
 78.2961 +
 78.2962 +extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr,
 78.2963 +    png_const_charp user_png_ver, png_size_t png_struct_size));
 78.2964 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.2965 +extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr,
 78.2966 +    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
 78.2967 +    png_info_size));
 78.2968 +#endif
 78.2969 +
 78.2970 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 78.2971 +/* Initialize png_ptr struct for writing, and allocate any other memory.
 78.2972 + * (old interface - DEPRECATED - use png_create_write_struct instead).
 78.2973 + */
 78.2974 +extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr));
 78.2975 +#undef png_write_init
 78.2976 +#define png_write_init(png_ptr) png_write_init_3(&png_ptr, \
 78.2977 +    PNG_LIBPNG_VER_STRING, png_sizeof(png_struct));
 78.2978 +#endif
 78.2979 +
 78.2980 +extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr,
 78.2981 +    png_const_charp user_png_ver, png_size_t png_struct_size));
 78.2982 +extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr,
 78.2983 +    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
 78.2984 +    png_info_size));
 78.2985 +
 78.2986 +/* Allocate memory for an internal libpng struct */
 78.2987 +PNG_EXTERN png_voidp png_create_struct PNGARG((int type));
 78.2988 +
 78.2989 +/* Free memory from internal libpng struct */
 78.2990 +PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr));
 78.2991 +
 78.2992 +PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr
 78.2993 +  malloc_fn, png_voidp mem_ptr));
 78.2994 +PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr,
 78.2995 +   png_free_ptr free_fn, png_voidp mem_ptr));
 78.2996 +
 78.2997 +/* Free any memory that info_ptr points to and reset struct. */
 78.2998 +PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr,
 78.2999 +   png_infop info_ptr));
 78.3000 +
 78.3001 +#ifndef PNG_1_0_X
 78.3002 +/* Function to allocate memory for zlib. */
 78.3003 +PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size));
 78.3004 +
 78.3005 +/* Function to free memory for zlib */
 78.3006 +PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr));
 78.3007 +
 78.3008 +#ifdef PNG_SIZE_T
 78.3009 +/* Function to convert a sizeof an item to png_sizeof item */
 78.3010 +   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
 78.3011 +#endif
 78.3012 +
 78.3013 +/* Next four functions are used internally as callbacks.  PNGAPI is required
 78.3014 + * but not PNG_EXPORT.  PNGAPI added at libpng version 1.2.3. */
 78.3015 +
 78.3016 +PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr,
 78.3017 +   png_bytep data, png_size_t length));
 78.3018 +
 78.3019 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.3020 +PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr,
 78.3021 +   png_bytep buffer, png_size_t length));
 78.3022 +#endif
 78.3023 +
 78.3024 +PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr,
 78.3025 +   png_bytep data, png_size_t length));
 78.3026 +
 78.3027 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 78.3028 +#if !defined(PNG_NO_STDIO)
 78.3029 +PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr));
 78.3030 +#endif
 78.3031 +#endif
 78.3032 +#else /* PNG_1_0_X */
 78.3033 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.3034 +PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr,
 78.3035 +   png_bytep buffer, png_size_t length));
 78.3036 +#endif
 78.3037 +#endif /* PNG_1_0_X */
 78.3038 +
 78.3039 +/* Reset the CRC variable */
 78.3040 +PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr));
 78.3041 +
 78.3042 +/* Write the "data" buffer to whatever output you are using. */
 78.3043 +PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
 78.3044 +   png_size_t length));
 78.3045 +
 78.3046 +/* Read data from whatever input you are using into the "data" buffer */
 78.3047 +PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
 78.3048 +   png_size_t length));
 78.3049 +
 78.3050 +/* Read bytes into buf, and update png_ptr->crc */
 78.3051 +PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf,
 78.3052 +   png_size_t length));
 78.3053 +
 78.3054 +/* Decompress data in a chunk that uses compression */
 78.3055 +#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \
 78.3056 +    defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
 78.3057 +PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr,
 78.3058 +   int comp_type, png_size_t chunklength,
 78.3059 +   png_size_t prefix_length, png_size_t *data_length));
 78.3060 +#endif
 78.3061 +
 78.3062 +/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */
 78.3063 +PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip));
 78.3064 +
 78.3065 +/* Read the CRC from the file and compare it to the libpng calculated CRC */
 78.3066 +PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr));
 78.3067 +
 78.3068 +/* Calculate the CRC over a section of data.  Note that we are only
 78.3069 + * passing a maximum of 64K on systems that have this as a memory limit,
 78.3070 + * since this is the maximum buffer size we can specify.
 78.3071 + */
 78.3072 +PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
 78.3073 +   png_size_t length));
 78.3074 +
 78.3075 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 78.3076 +PNG_EXTERN void png_flush PNGARG((png_structp png_ptr));
 78.3077 +#endif
 78.3078 +
 78.3079 +/* simple function to write the signature */
 78.3080 +PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr));
 78.3081 +
 78.3082 +/* write various chunks */
 78.3083 +
 78.3084 +/* Write the IHDR chunk, and update the png_struct with the necessary
 78.3085 + * information.
 78.3086 + */
 78.3087 +PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
 78.3088 +   png_uint_32 height,
 78.3089 +   int bit_depth, int color_type, int compression_method, int filter_method,
 78.3090 +   int interlace_method));
 78.3091 +
 78.3092 +PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
 78.3093 +   png_uint_32 num_pal));
 78.3094 +
 78.3095 +PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
 78.3096 +   png_size_t length));
 78.3097 +
 78.3098 +PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr));
 78.3099 +
 78.3100 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
 78.3101 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.3102 +PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma));
 78.3103 +#endif
 78.3104 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.3105 +PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point
 78.3106 +    file_gamma));
 78.3107 +#endif
 78.3108 +#endif
 78.3109 +
 78.3110 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
 78.3111 +PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
 78.3112 +   int color_type));
 78.3113 +#endif
 78.3114 +
 78.3115 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
 78.3116 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 78.3117 +PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr,
 78.3118 +   double white_x, double white_y,
 78.3119 +   double red_x, double red_y, double green_x, double green_y,
 78.3120 +   double blue_x, double blue_y));
 78.3121 +#endif
 78.3122 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.3123 +PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr,
 78.3124 +   png_fixed_point int_white_x, png_fixed_point int_white_y,
 78.3125 +   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
 78.3126 +   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
 78.3127 +   png_fixed_point int_blue_y));
 78.3128 +#endif
 78.3129 +#endif
 78.3130 +
 78.3131 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
 78.3132 +PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr,
 78.3133 +   int intent));
 78.3134 +#endif
 78.3135 +
 78.3136 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
 78.3137 +PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr,
 78.3138 +   png_charp name, int compression_type,
 78.3139 +   png_charp profile, int proflen));
 78.3140 +   /* Note to maintainer: profile should be png_bytep */
 78.3141 +#endif
 78.3142 +
 78.3143 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
 78.3144 +PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr,
 78.3145 +   png_sPLT_tp palette));
 78.3146 +#endif
 78.3147 +
 78.3148 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
 78.3149 +PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
 78.3150 +   png_color_16p values, int number, int color_type));
 78.3151 +#endif
 78.3152 +
 78.3153 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
 78.3154 +PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr,
 78.3155 +   png_color_16p values, int color_type));
 78.3156 +#endif
 78.3157 +
 78.3158 +#if defined(PNG_WRITE_hIST_SUPPORTED)
 78.3159 +PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
 78.3160 +   int num_hist));
 78.3161 +#endif
 78.3162 +
 78.3163 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
 78.3164 +    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
 78.3165 +PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr,
 78.3166 +   png_charp key, png_charpp new_key));
 78.3167 +#endif
 78.3168 +
 78.3169 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
 78.3170 +PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
 78.3171 +   png_charp text, png_size_t text_len));
 78.3172 +#endif
 78.3173 +
 78.3174 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
 78.3175 +PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
 78.3176 +   png_charp text, png_size_t text_len, int compression));
 78.3177 +#endif
 78.3178 +
 78.3179 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
 78.3180 +PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr,
 78.3181 +   int compression, png_charp key, png_charp lang, png_charp lang_key,
 78.3182 +   png_charp text));
 78.3183 +#endif
 78.3184 +
 78.3185 +#if defined(PNG_TEXT_SUPPORTED)  /* Added at version 1.0.14 and 1.2.4 */
 78.3186 +PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr,
 78.3187 +   png_infop info_ptr, png_textp text_ptr, int num_text));
 78.3188 +#endif
 78.3189 +
 78.3190 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
 78.3191 +PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr,
 78.3192 +   png_int_32 x_offset, png_int_32 y_offset, int unit_type));
 78.3193 +#endif
 78.3194 +
 78.3195 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
 78.3196 +PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose,
 78.3197 +   png_int_32 X0, png_int_32 X1, int type, int nparams,
 78.3198 +   png_charp units, png_charpp params));
 78.3199 +#endif
 78.3200 +
 78.3201 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
 78.3202 +PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr,
 78.3203 +   png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
 78.3204 +   int unit_type));
 78.3205 +#endif
 78.3206 +
 78.3207 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 78.3208 +PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr,
 78.3209 +   png_timep mod_time));
 78.3210 +#endif
 78.3211 +
 78.3212 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
 78.3213 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
 78.3214 +PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr,
 78.3215 +   int unit, double width, double height));
 78.3216 +#else
 78.3217 +#ifdef PNG_FIXED_POINT_SUPPORTED
 78.3218 +PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr,
 78.3219 +   int unit, png_charp width, png_charp height));
 78.3220 +#endif
 78.3221 +#endif
 78.3222 +#endif
 78.3223 +
 78.3224 +/* Called when finished processing a row of data */
 78.3225 +PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));
 78.3226 +
 78.3227 +/* Internal use only.   Called before first row of data */
 78.3228 +PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr));
 78.3229 +
 78.3230 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 78.3231 +PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr));
 78.3232 +#endif
 78.3233 +
 78.3234 +/* combine a row of data, dealing with alpha, etc. if requested */
 78.3235 +PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row,
 78.3236 +   int mask));
 78.3237 +
 78.3238 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
 78.3239 +/* expand an interlaced row */
 78.3240 +/* OLD pre-1.0.9 interface:
 78.3241 +PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info,
 78.3242 +   png_bytep row, int pass, png_uint_32 transformations));
 78.3243 + */
 78.3244 +PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr));
 78.3245 +#endif
 78.3246 +
 78.3247 +/* GRR TO DO (2.0 or whenever):  simplify other internal calling interfaces */
 78.3248 +
 78.3249 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
 78.3250 +/* grab pixels out of a row for an interlaced pass */
 78.3251 +PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info,
 78.3252 +   png_bytep row, int pass));
 78.3253 +#endif
 78.3254 +
 78.3255 +/* unfilter a row */
 78.3256 +PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr,
 78.3257 +   png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter));
 78.3258 +
 78.3259 +/* Choose the best filter to use and filter the row data */
 78.3260 +PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr,
 78.3261 +   png_row_infop row_info));
 78.3262 +
 78.3263 +/* Write out the filtered row. */
 78.3264 +PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr,
 78.3265 +   png_bytep filtered_row));
 78.3266 +/* finish a row while reading, dealing with interlacing passes, etc. */
 78.3267 +PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr));
 78.3268 +
 78.3269 +/* initialize the row buffers, etc. */
 78.3270 +PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr));
 78.3271 +/* optional call to update the users info structure */
 78.3272 +PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
 78.3273 +   png_infop info_ptr));
 78.3274 +
 78.3275 +/* these are the functions that do the transformations */
 78.3276 +#if defined(PNG_READ_FILLER_SUPPORTED)
 78.3277 +PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
 78.3278 +   png_bytep row, png_uint_32 filler, png_uint_32 flags));
 78.3279 +#endif
 78.3280 +
 78.3281 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 78.3282 +PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info,
 78.3283 +   png_bytep row));
 78.3284 +#endif
 78.3285 +
 78.3286 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 78.3287 +PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info,
 78.3288 +   png_bytep row));
 78.3289 +#endif
 78.3290 +
 78.3291 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 78.3292 +PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info,
 78.3293 +   png_bytep row));
 78.3294 +#endif
 78.3295 +
 78.3296 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 78.3297 +PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info,
 78.3298 +   png_bytep row));
 78.3299 +#endif
 78.3300 +
 78.3301 +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
 78.3302 +    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 78.3303 +PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info,
 78.3304 +   png_bytep row, png_uint_32 flags));
 78.3305 +#endif
 78.3306 +
 78.3307 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
 78.3308 +PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row));
 78.3309 +#endif
 78.3310 +
 78.3311 +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 78.3312 +PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row));
 78.3313 +#endif
 78.3314 +
 78.3315 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 78.3316 +PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
 78.3317 +   row_info, png_bytep row));
 78.3318 +#endif
 78.3319 +
 78.3320 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 78.3321 +PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
 78.3322 +   png_bytep row));
 78.3323 +#endif
 78.3324 +
 78.3325 +#if defined(PNG_READ_PACK_SUPPORTED)
 78.3326 +PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row));
 78.3327 +#endif
 78.3328 +
 78.3329 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 78.3330 +PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row,
 78.3331 +   png_color_8p sig_bits));
 78.3332 +#endif
 78.3333 +
 78.3334 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
 78.3335 +PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row));
 78.3336 +#endif
 78.3337 +
 78.3338 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 78.3339 +PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row));
 78.3340 +#endif
 78.3341 +
 78.3342 +#if defined(PNG_READ_DITHER_SUPPORTED)
 78.3343 +PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info,
 78.3344 +   png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));
 78.3345 +
 78.3346 +#  if defined(PNG_CORRECT_PALETTE_SUPPORTED)
 78.3347 +PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr,
 78.3348 +   png_colorp palette, int num_palette));
 78.3349 +#  endif
 78.3350 +#endif
 78.3351 +
 78.3352 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 78.3353 +PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row));
 78.3354 +#endif
 78.3355 +
 78.3356 +#if defined(PNG_WRITE_PACK_SUPPORTED)
 78.3357 +PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info,
 78.3358 +   png_bytep row, png_uint_32 bit_depth));
 78.3359 +#endif
 78.3360 +
 78.3361 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
 78.3362 +PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row,
 78.3363 +   png_color_8p bit_depth));
 78.3364 +#endif
 78.3365 +
 78.3366 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 78.3367 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 78.3368 +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
 78.3369 +   png_color_16p trans_values, png_color_16p background,
 78.3370 +   png_color_16p background_1,
 78.3371 +   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
 78.3372 +   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
 78.3373 +   png_uint_16pp gamma_16_to_1, int gamma_shift));
 78.3374 +#else
 78.3375 +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
 78.3376 +   png_color_16p trans_values, png_color_16p background));
 78.3377 +#endif
 78.3378 +#endif
 78.3379 +
 78.3380 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 78.3381 +PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
 78.3382 +   png_bytep gamma_table, png_uint_16pp gamma_16_table,
 78.3383 +   int gamma_shift));
 78.3384 +#endif
 78.3385 +
 78.3386 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 78.3387 +PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info,
 78.3388 +   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
 78.3389 +PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info,
 78.3390 +   png_bytep row, png_color_16p trans_value));
 78.3391 +#endif
 78.3392 +
 78.3393 +/* The following decodes the appropriate chunks, and does error correction,
 78.3394 + * then calls the appropriate callback for the chunk if it is valid.
 78.3395 + */
 78.3396 +
 78.3397 +/* decode the IHDR chunk */
 78.3398 +PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3399 +   png_uint_32 length));
 78.3400 +PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3401 +   png_uint_32 length));
 78.3402 +PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3403 +   png_uint_32 length));
 78.3404 +
 78.3405 +#if defined(PNG_READ_bKGD_SUPPORTED)
 78.3406 +PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3407 +   png_uint_32 length));
 78.3408 +#endif
 78.3409 +
 78.3410 +#if defined(PNG_READ_cHRM_SUPPORTED)
 78.3411 +PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3412 +   png_uint_32 length));
 78.3413 +#endif
 78.3414 +
 78.3415 +#if defined(PNG_READ_gAMA_SUPPORTED)
 78.3416 +PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3417 +   png_uint_32 length));
 78.3418 +#endif
 78.3419 +
 78.3420 +#if defined(PNG_READ_hIST_SUPPORTED)
 78.3421 +PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3422 +   png_uint_32 length));
 78.3423 +#endif
 78.3424 +
 78.3425 +#if defined(PNG_READ_iCCP_SUPPORTED)
 78.3426 +extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3427 +   png_uint_32 length));
 78.3428 +#endif /* PNG_READ_iCCP_SUPPORTED */
 78.3429 +
 78.3430 +#if defined(PNG_READ_iTXt_SUPPORTED)
 78.3431 +PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3432 +   png_uint_32 length));
 78.3433 +#endif
 78.3434 +
 78.3435 +#if defined(PNG_READ_oFFs_SUPPORTED)
 78.3436 +PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3437 +   png_uint_32 length));
 78.3438 +#endif
 78.3439 +
 78.3440 +#if defined(PNG_READ_pCAL_SUPPORTED)
 78.3441 +PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3442 +   png_uint_32 length));
 78.3443 +#endif
 78.3444 +
 78.3445 +#if defined(PNG_READ_pHYs_SUPPORTED)
 78.3446 +PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3447 +   png_uint_32 length));
 78.3448 +#endif
 78.3449 +
 78.3450 +#if defined(PNG_READ_sBIT_SUPPORTED)
 78.3451 +PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3452 +   png_uint_32 length));
 78.3453 +#endif
 78.3454 +
 78.3455 +#if defined(PNG_READ_sCAL_SUPPORTED)
 78.3456 +PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3457 +   png_uint_32 length));
 78.3458 +#endif
 78.3459 +
 78.3460 +#if defined(PNG_READ_sPLT_SUPPORTED)
 78.3461 +extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3462 +   png_uint_32 length));
 78.3463 +#endif /* PNG_READ_sPLT_SUPPORTED */
 78.3464 +
 78.3465 +#if defined(PNG_READ_sRGB_SUPPORTED)
 78.3466 +PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3467 +   png_uint_32 length));
 78.3468 +#endif
 78.3469 +
 78.3470 +#if defined(PNG_READ_tEXt_SUPPORTED)
 78.3471 +PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3472 +   png_uint_32 length));
 78.3473 +#endif
 78.3474 +
 78.3475 +#if defined(PNG_READ_tIME_SUPPORTED)
 78.3476 +PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3477 +   png_uint_32 length));
 78.3478 +#endif
 78.3479 +
 78.3480 +#if defined(PNG_READ_tRNS_SUPPORTED)
 78.3481 +PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3482 +   png_uint_32 length));
 78.3483 +#endif
 78.3484 +
 78.3485 +#if defined(PNG_READ_zTXt_SUPPORTED)
 78.3486 +PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 78.3487 +   png_uint_32 length));
 78.3488 +#endif
 78.3489 +
 78.3490 +PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
 78.3491 +   png_infop info_ptr, png_uint_32 length));
 78.3492 +
 78.3493 +PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr,
 78.3494 +   png_bytep chunk_name));
 78.3495 +
 78.3496 +/* handle the transformations for reading and writing */
 78.3497 +PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr));
 78.3498 +PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr));
 78.3499 +
 78.3500 +PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr));
 78.3501 +
 78.3502 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 78.3503 +PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr,
 78.3504 +   png_infop info_ptr));
 78.3505 +PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr,
 78.3506 +   png_infop info_ptr));
 78.3507 +PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr));
 78.3508 +PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr,
 78.3509 +   png_uint_32 length));
 78.3510 +PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr));
 78.3511 +PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr));
 78.3512 +PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr,
 78.3513 +   png_bytep buffer, png_size_t buffer_length));
 78.3514 +PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr));
 78.3515 +PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr,
 78.3516 +   png_bytep buffer, png_size_t buffer_length));
 78.3517 +PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr));
 78.3518 +PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr,
 78.3519 +   png_infop info_ptr, png_uint_32 length));
 78.3520 +PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr,
 78.3521 +   png_infop info_ptr));
 78.3522 +PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr,
 78.3523 +   png_infop info_ptr));
 78.3524 +PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
 78.3525 +PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr,
 78.3526 +   png_infop info_ptr));
 78.3527 +PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr,
 78.3528 +   png_infop info_ptr));
 78.3529 +PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr));
 78.3530 +#if defined(PNG_READ_tEXt_SUPPORTED)
 78.3531 +PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr,
 78.3532 +   png_infop info_ptr, png_uint_32 length));
 78.3533 +PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr,
 78.3534 +   png_infop info_ptr));
 78.3535 +#endif
 78.3536 +#if defined(PNG_READ_zTXt_SUPPORTED)
 78.3537 +PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr,
 78.3538 +   png_infop info_ptr, png_uint_32 length));
 78.3539 +PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr,
 78.3540 +   png_infop info_ptr));
 78.3541 +#endif
 78.3542 +#if defined(PNG_READ_iTXt_SUPPORTED)
 78.3543 +PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr,
 78.3544 +   png_infop info_ptr, png_uint_32 length));
 78.3545 +PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr,
 78.3546 +   png_infop info_ptr));
 78.3547 +#endif
 78.3548 +
 78.3549 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 78.3550 +
 78.3551 +#ifdef PNG_MNG_FEATURES_SUPPORTED
 78.3552 +PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info,
 78.3553 +   png_bytep row));
 78.3554 +PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info,
 78.3555 +   png_bytep row));
 78.3556 +#endif
 78.3557 +
 78.3558 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 78.3559 +#if defined(PNG_MMX_CODE_SUPPORTED)
 78.3560 +/* png.c */ /* PRIVATE */
 78.3561 +PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr));
 78.3562 +#endif
 78.3563 +#endif
 78.3564 +
 78.3565 +#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
 78.3566 +PNG_EXTERN png_uint_32 png_get_pixels_per_inch PNGARG((png_structp png_ptr,
 78.3567 +png_infop info_ptr));
 78.3568 +
 78.3569 +PNG_EXTERN png_uint_32 png_get_x_pixels_per_inch PNGARG((png_structp png_ptr,
 78.3570 +png_infop info_ptr));
 78.3571 +
 78.3572 +PNG_EXTERN png_uint_32 png_get_y_pixels_per_inch PNGARG((png_structp png_ptr,
 78.3573 +png_infop info_ptr));
 78.3574 +
 78.3575 +PNG_EXTERN float png_get_x_offset_inches PNGARG((png_structp png_ptr,
 78.3576 +png_infop info_ptr));
 78.3577 +
 78.3578 +PNG_EXTERN float png_get_y_offset_inches PNGARG((png_structp png_ptr,
 78.3579 +png_infop info_ptr));
 78.3580 +
 78.3581 +#if defined(PNG_pHYs_SUPPORTED)
 78.3582 +PNG_EXTERN png_uint_32 png_get_pHYs_dpi PNGARG((png_structp png_ptr,
 78.3583 +png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
 78.3584 +#endif /* PNG_pHYs_SUPPORTED */
 78.3585 +#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
 78.3586 +
 78.3587 +/* Read the chunk header (length + type name) */
 78.3588 +PNG_EXTERN png_uint_32 png_read_chunk_header PNGARG((png_structp png_ptr));
 78.3589 +
 78.3590 +/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
 78.3591 +
 78.3592 +#endif /* PNG_INTERNAL */
 78.3593 +
 78.3594 +#ifdef __cplusplus
 78.3595 +}
 78.3596 +#endif
 78.3597 +
 78.3598 +#endif /* PNG_VERSION_INFO_ONLY */
 78.3599 +/* do not put anything past this line */
 78.3600 +#endif /* PNG_H */
    79.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    79.2 +++ b/libs/libpng/pngconf.h	Sun Jun 07 17:25:49 2015 +0300
    79.3 @@ -0,0 +1,1481 @@
    79.4 +
    79.5 +/* pngconf.h - machine configurable file for libpng
    79.6 + *
    79.7 + * libpng version 1.2.33 - October 31, 2008
    79.8 + * For conditions of distribution and use, see copyright notice in png.h
    79.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   79.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   79.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   79.12 + */
   79.13 +
   79.14 +/* Any machine specific code is near the front of this file, so if you
   79.15 + * are configuring libpng for a machine, you may want to read the section
   79.16 + * starting here down to where it starts to typedef png_color, png_text,
   79.17 + * and png_info.
   79.18 + */
   79.19 +
   79.20 +#ifndef PNGCONF_H
   79.21 +#define PNGCONF_H
   79.22 +
   79.23 +#define PNG_1_2_X
   79.24 +
   79.25 +/* 
   79.26 + * PNG_USER_CONFIG has to be defined on the compiler command line. This
   79.27 + * includes the resource compiler for Windows DLL configurations.
   79.28 + */
   79.29 +#ifdef PNG_USER_CONFIG
   79.30 +#  ifndef PNG_USER_PRIVATEBUILD
   79.31 +#    define PNG_USER_PRIVATEBUILD
   79.32 +#  endif
   79.33 +#include "pngusr.h"
   79.34 +#endif
   79.35 +
   79.36 +/* PNG_CONFIGURE_LIBPNG is set by the "configure" script. */
   79.37 +#ifdef PNG_CONFIGURE_LIBPNG
   79.38 +#ifdef HAVE_CONFIG_H
   79.39 +#include "config.h"
   79.40 +#endif
   79.41 +#endif
   79.42 +
   79.43 +/*
   79.44 + * Added at libpng-1.2.8
   79.45 + *  
   79.46 + * If you create a private DLL you need to define in "pngusr.h" the followings:
   79.47 + * #define PNG_USER_PRIVATEBUILD <Describes by whom and why this version of
   79.48 + *        the DLL was built>
   79.49 + *  e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons."
   79.50 + * #define PNG_USER_DLLFNAME_POSTFIX <two-letter postfix that serve to
   79.51 + *        distinguish your DLL from those of the official release. These
   79.52 + *        correspond to the trailing letters that come after the version
   79.53 + *        number and must match your private DLL name>
   79.54 + *  e.g. // private DLL "libpng13gx.dll"
   79.55 + *       #define PNG_USER_DLLFNAME_POSTFIX "gx"
   79.56 + * 
   79.57 + * The following macros are also at your disposal if you want to complete the 
   79.58 + * DLL VERSIONINFO structure.
   79.59 + * - PNG_USER_VERSIONINFO_COMMENTS
   79.60 + * - PNG_USER_VERSIONINFO_COMPANYNAME
   79.61 + * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS
   79.62 + */
   79.63 +
   79.64 +#ifdef __STDC__
   79.65 +#ifdef SPECIALBUILD
   79.66 +#  pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\
   79.67 + are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.")
   79.68 +#endif
   79.69 +
   79.70 +#ifdef PRIVATEBUILD
   79.71 +# pragma message("PRIVATEBUILD is deprecated.\
   79.72 + Use PNG_USER_PRIVATEBUILD instead.")
   79.73 +# define PNG_USER_PRIVATEBUILD PRIVATEBUILD
   79.74 +#endif
   79.75 +#endif /* __STDC__ */
   79.76 +
   79.77 +#ifndef PNG_VERSION_INFO_ONLY
   79.78 +
   79.79 +/* End of material added to libpng-1.2.8 */
   79.80 +
   79.81 +/* Added at libpng-1.2.19, removed at libpng-1.2.20 because it caused trouble
   79.82 +   Restored at libpng-1.2.21 */
   79.83 +#if !defined(PNG_NO_WARN_UNINITIALIZED_ROW) && \
   79.84 +    !defined(PNG_WARN_UNINITIALIZED_ROW)
   79.85 +#  define PNG_WARN_UNINITIALIZED_ROW 1
   79.86 +#endif
   79.87 +/* End of material added at libpng-1.2.19/1.2.21 */
   79.88 +
   79.89 +/* This is the size of the compression buffer, and thus the size of
   79.90 + * an IDAT chunk.  Make this whatever size you feel is best for your
   79.91 + * machine.  One of these will be allocated per png_struct.  When this
   79.92 + * is full, it writes the data to the disk, and does some other
   79.93 + * calculations.  Making this an extremely small size will slow
   79.94 + * the library down, but you may want to experiment to determine
   79.95 + * where it becomes significant, if you are concerned with memory
   79.96 + * usage.  Note that zlib allocates at least 32Kb also.  For readers,
   79.97 + * this describes the size of the buffer available to read the data in.
   79.98 + * Unless this gets smaller than the size of a row (compressed),
   79.99 + * it should not make much difference how big this is.
  79.100 + */
  79.101 +
  79.102 +#ifndef PNG_ZBUF_SIZE
  79.103 +#  define PNG_ZBUF_SIZE 8192
  79.104 +#endif
  79.105 +
  79.106 +/* Enable if you want a write-only libpng */
  79.107 +
  79.108 +#ifndef PNG_NO_READ_SUPPORTED
  79.109 +#  define PNG_READ_SUPPORTED
  79.110 +#endif
  79.111 +
  79.112 +/* Enable if you want a read-only libpng */
  79.113 +
  79.114 +#ifndef PNG_NO_WRITE_SUPPORTED
  79.115 +#  define PNG_WRITE_SUPPORTED
  79.116 +#endif
  79.117 +
  79.118 +/* Enabled by default in 1.2.0.  You can disable this if you don't need to
  79.119 +   support PNGs that are embedded in MNG datastreams */
  79.120 +#if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES)
  79.121 +#  ifndef PNG_MNG_FEATURES_SUPPORTED
  79.122 +#    define PNG_MNG_FEATURES_SUPPORTED
  79.123 +#  endif
  79.124 +#endif
  79.125 +
  79.126 +#ifndef PNG_NO_FLOATING_POINT_SUPPORTED
  79.127 +#  ifndef PNG_FLOATING_POINT_SUPPORTED
  79.128 +#    define PNG_FLOATING_POINT_SUPPORTED
  79.129 +#  endif
  79.130 +#endif
  79.131 +
  79.132 +/* If you are running on a machine where you cannot allocate more
  79.133 + * than 64K of memory at once, uncomment this.  While libpng will not
  79.134 + * normally need that much memory in a chunk (unless you load up a very
  79.135 + * large file), zlib needs to know how big of a chunk it can use, and
  79.136 + * libpng thus makes sure to check any memory allocation to verify it
  79.137 + * will fit into memory.
  79.138 +#define PNG_MAX_MALLOC_64K
  79.139 + */
  79.140 +#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
  79.141 +#  define PNG_MAX_MALLOC_64K
  79.142 +#endif
  79.143 +
  79.144 +/* Special munging to support doing things the 'cygwin' way:
  79.145 + * 'Normal' png-on-win32 defines/defaults:
  79.146 + *   PNG_BUILD_DLL -- building dll
  79.147 + *   PNG_USE_DLL   -- building an application, linking to dll
  79.148 + *   (no define)   -- building static library, or building an
  79.149 + *                    application and linking to the static lib
  79.150 + * 'Cygwin' defines/defaults:
  79.151 + *   PNG_BUILD_DLL -- (ignored) building the dll
  79.152 + *   (no define)   -- (ignored) building an application, linking to the dll
  79.153 + *   PNG_STATIC    -- (ignored) building the static lib, or building an 
  79.154 + *                    application that links to the static lib.
  79.155 + *   ALL_STATIC    -- (ignored) building various static libs, or building an 
  79.156 + *                    application that links to the static libs.
  79.157 + * Thus,
  79.158 + * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and
  79.159 + * this bit of #ifdefs will define the 'correct' config variables based on
  79.160 + * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but
  79.161 + * unnecessary.
  79.162 + *
  79.163 + * Also, the precedence order is:
  79.164 + *   ALL_STATIC (since we can't #undef something outside our namespace)
  79.165 + *   PNG_BUILD_DLL
  79.166 + *   PNG_STATIC
  79.167 + *   (nothing) == PNG_USE_DLL
  79.168 + * 
  79.169 + * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent
  79.170 + *   of auto-import in binutils, we no longer need to worry about 
  79.171 + *   __declspec(dllexport) / __declspec(dllimport) and friends.  Therefore,
  79.172 + *   we don't need to worry about PNG_STATIC or ALL_STATIC when it comes
  79.173 + *   to __declspec() stuff.  However, we DO need to worry about 
  79.174 + *   PNG_BUILD_DLL and PNG_STATIC because those change some defaults
  79.175 + *   such as CONSOLE_IO and whether GLOBAL_ARRAYS are allowed.
  79.176 + */
  79.177 +#if defined(__CYGWIN__)
  79.178 +#  if defined(ALL_STATIC)
  79.179 +#    if defined(PNG_BUILD_DLL)
  79.180 +#      undef PNG_BUILD_DLL
  79.181 +#    endif
  79.182 +#    if defined(PNG_USE_DLL)
  79.183 +#      undef PNG_USE_DLL
  79.184 +#    endif
  79.185 +#    if defined(PNG_DLL)
  79.186 +#      undef PNG_DLL
  79.187 +#    endif
  79.188 +#    if !defined(PNG_STATIC)
  79.189 +#      define PNG_STATIC
  79.190 +#    endif
  79.191 +#  else
  79.192 +#    if defined (PNG_BUILD_DLL)
  79.193 +#      if defined(PNG_STATIC)
  79.194 +#        undef PNG_STATIC
  79.195 +#      endif
  79.196 +#      if defined(PNG_USE_DLL)
  79.197 +#        undef PNG_USE_DLL
  79.198 +#      endif
  79.199 +#      if !defined(PNG_DLL)
  79.200 +#        define PNG_DLL
  79.201 +#      endif
  79.202 +#    else
  79.203 +#      if defined(PNG_STATIC)
  79.204 +#        if defined(PNG_USE_DLL)
  79.205 +#          undef PNG_USE_DLL
  79.206 +#        endif
  79.207 +#        if defined(PNG_DLL)
  79.208 +#          undef PNG_DLL
  79.209 +#        endif
  79.210 +#      else
  79.211 +#        if !defined(PNG_USE_DLL)
  79.212 +#          define PNG_USE_DLL
  79.213 +#        endif
  79.214 +#        if !defined(PNG_DLL)
  79.215 +#          define PNG_DLL
  79.216 +#        endif
  79.217 +#      endif  
  79.218 +#    endif  
  79.219 +#  endif
  79.220 +#endif
  79.221 +
  79.222 +/* This protects us against compilers that run on a windowing system
  79.223 + * and thus don't have or would rather us not use the stdio types:
  79.224 + * stdin, stdout, and stderr.  The only one currently used is stderr
  79.225 + * in png_error() and png_warning().  #defining PNG_NO_CONSOLE_IO will
  79.226 + * prevent these from being compiled and used. #defining PNG_NO_STDIO
  79.227 + * will also prevent these, plus will prevent the entire set of stdio
  79.228 + * macros and functions (FILE *, printf, etc.) from being compiled and used,
  79.229 + * unless (PNG_DEBUG > 0) has been #defined.
  79.230 + *
  79.231 + * #define PNG_NO_CONSOLE_IO
  79.232 + * #define PNG_NO_STDIO
  79.233 + */
  79.234 +
  79.235 +#if defined(_WIN32_WCE)
  79.236 +#  include <windows.h>
  79.237 +   /* Console I/O functions are not supported on WindowsCE */
  79.238 +#  define PNG_NO_CONSOLE_IO
  79.239 +#  ifdef PNG_DEBUG
  79.240 +#    undef PNG_DEBUG
  79.241 +#  endif
  79.242 +#endif
  79.243 +
  79.244 +#ifdef PNG_BUILD_DLL
  79.245 +#  ifndef PNG_CONSOLE_IO_SUPPORTED
  79.246 +#    ifndef PNG_NO_CONSOLE_IO
  79.247 +#      define PNG_NO_CONSOLE_IO
  79.248 +#    endif
  79.249 +#  endif
  79.250 +#endif
  79.251 +
  79.252 +#  ifdef PNG_NO_STDIO
  79.253 +#    ifndef PNG_NO_CONSOLE_IO
  79.254 +#      define PNG_NO_CONSOLE_IO
  79.255 +#    endif
  79.256 +#    ifdef PNG_DEBUG
  79.257 +#      if (PNG_DEBUG > 0)
  79.258 +#        include <stdio.h>
  79.259 +#      endif
  79.260 +#    endif
  79.261 +#  else
  79.262 +#    if !defined(_WIN32_WCE)
  79.263 +/* "stdio.h" functions are not supported on WindowsCE */
  79.264 +#      include <stdio.h>
  79.265 +#    endif
  79.266 +#  endif
  79.267 +
  79.268 +/* This macro protects us against machines that don't have function
  79.269 + * prototypes (ie K&R style headers).  If your compiler does not handle
  79.270 + * function prototypes, define this macro and use the included ansi2knr.
  79.271 + * I've always been able to use _NO_PROTO as the indicator, but you may
  79.272 + * need to drag the empty declaration out in front of here, or change the
  79.273 + * ifdef to suit your own needs.
  79.274 + */
  79.275 +#ifndef PNGARG
  79.276 +
  79.277 +#ifdef OF /* zlib prototype munger */
  79.278 +#  define PNGARG(arglist) OF(arglist)
  79.279 +#else
  79.280 +
  79.281 +#ifdef _NO_PROTO
  79.282 +#  define PNGARG(arglist) ()
  79.283 +#  ifndef PNG_TYPECAST_NULL
  79.284 +#     define PNG_TYPECAST_NULL
  79.285 +#  endif
  79.286 +#else
  79.287 +#  define PNGARG(arglist) arglist
  79.288 +#endif /* _NO_PROTO */
  79.289 +
  79.290 +
  79.291 +#endif /* OF */
  79.292 +
  79.293 +#endif /* PNGARG */
  79.294 +
  79.295 +/* Try to determine if we are compiling on a Mac.  Note that testing for
  79.296 + * just __MWERKS__ is not good enough, because the Codewarrior is now used
  79.297 + * on non-Mac platforms.
  79.298 + */
  79.299 +#ifndef MACOS
  79.300 +#  if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \
  79.301 +      defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC)
  79.302 +#    define MACOS
  79.303 +#  endif
  79.304 +#endif
  79.305 +
  79.306 +/* enough people need this for various reasons to include it here */
  79.307 +#if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE)
  79.308 +#  include <sys/types.h>
  79.309 +#endif
  79.310 +
  79.311 +#if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED)
  79.312 +#  define PNG_SETJMP_SUPPORTED
  79.313 +#endif
  79.314 +
  79.315 +#ifdef PNG_SETJMP_SUPPORTED
  79.316 +/* This is an attempt to force a single setjmp behaviour on Linux.  If
  79.317 + * the X config stuff didn't define _BSD_SOURCE we wouldn't need this.
  79.318 + */
  79.319 +
  79.320 +#  ifdef __linux__
  79.321 +#    ifdef _BSD_SOURCE
  79.322 +#      define PNG_SAVE_BSD_SOURCE
  79.323 +#      undef _BSD_SOURCE
  79.324 +#    endif
  79.325 +#    ifdef _SETJMP_H
  79.326 +     /* If you encounter a compiler error here, see the explanation
  79.327 +      * near the end of INSTALL.
  79.328 +      */
  79.329 +         __pngconf.h__ already includes setjmp.h;
  79.330 +         __dont__ include it again.;
  79.331 +#    endif
  79.332 +#  endif /* __linux__ */
  79.333 +
  79.334 +   /* include setjmp.h for error handling */
  79.335 +#  include <setjmp.h>
  79.336 +
  79.337 +#  ifdef __linux__
  79.338 +#    ifdef PNG_SAVE_BSD_SOURCE
  79.339 +#      ifndef _BSD_SOURCE
  79.340 +#        define _BSD_SOURCE
  79.341 +#      endif
  79.342 +#      undef PNG_SAVE_BSD_SOURCE
  79.343 +#    endif
  79.344 +#  endif /* __linux__ */
  79.345 +#endif /* PNG_SETJMP_SUPPORTED */
  79.346 +
  79.347 +#ifdef BSD
  79.348 +#  include <strings.h>
  79.349 +#else
  79.350 +#  include <string.h>
  79.351 +#endif
  79.352 +
  79.353 +/* Other defines for things like memory and the like can go here.  */
  79.354 +#ifdef PNG_INTERNAL
  79.355 +
  79.356 +#include <stdlib.h>
  79.357 +
  79.358 +/* The functions exported by PNG_EXTERN are PNG_INTERNAL functions, which
  79.359 + * aren't usually used outside the library (as far as I know), so it is
  79.360 + * debatable if they should be exported at all.  In the future, when it is
  79.361 + * possible to have run-time registry of chunk-handling functions, some of
  79.362 + * these will be made available again.
  79.363 +#define PNG_EXTERN extern
  79.364 + */
  79.365 +#define PNG_EXTERN
  79.366 +
  79.367 +/* Other defines specific to compilers can go here.  Try to keep
  79.368 + * them inside an appropriate ifdef/endif pair for portability.
  79.369 + */
  79.370 +
  79.371 +#if defined(PNG_FLOATING_POINT_SUPPORTED)
  79.372 +#  if defined(MACOS)
  79.373 +     /* We need to check that <math.h> hasn't already been included earlier
  79.374 +      * as it seems it doesn't agree with <fp.h>, yet we should really use
  79.375 +      * <fp.h> if possible.
  79.376 +      */
  79.377 +#    if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__)
  79.378 +#      include <fp.h>
  79.379 +#    endif
  79.380 +#  else
  79.381 +#    include <math.h>
  79.382 +#  endif
  79.383 +#  if defined(_AMIGA) && defined(__SASC) && defined(_M68881)
  79.384 +     /* Amiga SAS/C: We must include builtin FPU functions when compiling using
  79.385 +      * MATH=68881
  79.386 +      */
  79.387 +#    include <m68881.h>
  79.388 +#  endif
  79.389 +#endif
  79.390 +
  79.391 +/* Codewarrior on NT has linking problems without this. */
  79.392 +#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__)
  79.393 +#  define PNG_ALWAYS_EXTERN
  79.394 +#endif
  79.395 +
  79.396 +/* This provides the non-ANSI (far) memory allocation routines. */
  79.397 +#if defined(__TURBOC__) && defined(__MSDOS__)
  79.398 +#  include <mem.h>
  79.399 +#  include <alloc.h>
  79.400 +#endif
  79.401 +
  79.402 +/* I have no idea why is this necessary... */
  79.403 +#if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \
  79.404 +    defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__))
  79.405 +#  include <malloc.h>
  79.406 +#endif
  79.407 +
  79.408 +/* This controls how fine the dithering gets.  As this allocates
  79.409 + * a largish chunk of memory (32K), those who are not as concerned
  79.410 + * with dithering quality can decrease some or all of these.
  79.411 + */
  79.412 +#ifndef PNG_DITHER_RED_BITS
  79.413 +#  define PNG_DITHER_RED_BITS 5
  79.414 +#endif
  79.415 +#ifndef PNG_DITHER_GREEN_BITS
  79.416 +#  define PNG_DITHER_GREEN_BITS 5
  79.417 +#endif
  79.418 +#ifndef PNG_DITHER_BLUE_BITS
  79.419 +#  define PNG_DITHER_BLUE_BITS 5
  79.420 +#endif
  79.421 +
  79.422 +/* This controls how fine the gamma correction becomes when you
  79.423 + * are only interested in 8 bits anyway.  Increasing this value
  79.424 + * results in more memory being used, and more pow() functions
  79.425 + * being called to fill in the gamma tables.  Don't set this value
  79.426 + * less then 8, and even that may not work (I haven't tested it).
  79.427 + */
  79.428 +
  79.429 +#ifndef PNG_MAX_GAMMA_8
  79.430 +#  define PNG_MAX_GAMMA_8 11
  79.431 +#endif
  79.432 +
  79.433 +/* This controls how much a difference in gamma we can tolerate before
  79.434 + * we actually start doing gamma conversion.
  79.435 + */
  79.436 +#ifndef PNG_GAMMA_THRESHOLD
  79.437 +#  define PNG_GAMMA_THRESHOLD 0.05
  79.438 +#endif
  79.439 +
  79.440 +#endif /* PNG_INTERNAL */
  79.441 +
  79.442 +/* The following uses const char * instead of char * for error
  79.443 + * and warning message functions, so some compilers won't complain.
  79.444 + * If you do not want to use const, define PNG_NO_CONST here.
  79.445 + */
  79.446 +
  79.447 +#ifndef PNG_NO_CONST
  79.448 +#  define PNG_CONST const
  79.449 +#else
  79.450 +#  define PNG_CONST
  79.451 +#endif
  79.452 +
  79.453 +/* The following defines give you the ability to remove code from the
  79.454 + * library that you will not be using.  I wish I could figure out how to
  79.455 + * automate this, but I can't do that without making it seriously hard
  79.456 + * on the users.  So if you are not using an ability, change the #define
  79.457 + * to and #undef, and that part of the library will not be compiled.  If
  79.458 + * your linker can't find a function, you may want to make sure the
  79.459 + * ability is defined here.  Some of these depend upon some others being
  79.460 + * defined.  I haven't figured out all the interactions here, so you may
  79.461 + * have to experiment awhile to get everything to compile.  If you are
  79.462 + * creating or using a shared library, you probably shouldn't touch this,
  79.463 + * as it will affect the size of the structures, and this will cause bad
  79.464 + * things to happen if the library and/or application ever change.
  79.465 + */
  79.466 +
  79.467 +/* Any features you will not be using can be undef'ed here */
  79.468 +
  79.469 +/* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user
  79.470 + * to turn it off with "*TRANSFORMS_NOT_SUPPORTED" or *PNG_NO_*_TRANSFORMS
  79.471 + * on the compile line, then pick and choose which ones to define without
  79.472 + * having to edit this file. It is safe to use the *TRANSFORMS_NOT_SUPPORTED
  79.473 + * if you only want to have a png-compliant reader/writer but don't need
  79.474 + * any of the extra transformations.  This saves about 80 kbytes in a
  79.475 + * typical installation of the library. (PNG_NO_* form added in version
  79.476 + * 1.0.1c, for consistency)
  79.477 + */
  79.478 +
  79.479 +/* The size of the png_text structure changed in libpng-1.0.6 when
  79.480 + * iTXt support was added.  iTXt support was turned off by default through
  79.481 + * libpng-1.2.x, to support old apps that malloc the png_text structure
  79.482 + * instead of calling png_set_text() and letting libpng malloc it.  It
  79.483 + * was turned on by default in libpng-1.3.0.
  79.484 + */
  79.485 +
  79.486 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  79.487 +#  ifndef PNG_NO_iTXt_SUPPORTED
  79.488 +#    define PNG_NO_iTXt_SUPPORTED
  79.489 +#  endif
  79.490 +#  ifndef PNG_NO_READ_iTXt
  79.491 +#    define PNG_NO_READ_iTXt
  79.492 +#  endif
  79.493 +#  ifndef PNG_NO_WRITE_iTXt
  79.494 +#    define PNG_NO_WRITE_iTXt
  79.495 +#  endif
  79.496 +#endif
  79.497 +
  79.498 +#if !defined(PNG_NO_iTXt_SUPPORTED)
  79.499 +#  if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt)
  79.500 +#    define PNG_READ_iTXt
  79.501 +#  endif
  79.502 +#  if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt)
  79.503 +#    define PNG_WRITE_iTXt
  79.504 +#  endif
  79.505 +#endif
  79.506 +
  79.507 +/* The following support, added after version 1.0.0, can be turned off here en
  79.508 + * masse by defining PNG_LEGACY_SUPPORTED in case you need binary compatibility
  79.509 + * with old applications that require the length of png_struct and png_info
  79.510 + * to remain unchanged.
  79.511 + */
  79.512 +
  79.513 +#ifdef PNG_LEGACY_SUPPORTED
  79.514 +#  define PNG_NO_FREE_ME
  79.515 +#  define PNG_NO_READ_UNKNOWN_CHUNKS
  79.516 +#  define PNG_NO_WRITE_UNKNOWN_CHUNKS
  79.517 +#  define PNG_NO_READ_USER_CHUNKS
  79.518 +#  define PNG_NO_READ_iCCP
  79.519 +#  define PNG_NO_WRITE_iCCP
  79.520 +#  define PNG_NO_READ_iTXt
  79.521 +#  define PNG_NO_WRITE_iTXt
  79.522 +#  define PNG_NO_READ_sCAL
  79.523 +#  define PNG_NO_WRITE_sCAL
  79.524 +#  define PNG_NO_READ_sPLT
  79.525 +#  define PNG_NO_WRITE_sPLT
  79.526 +#  define PNG_NO_INFO_IMAGE
  79.527 +#  define PNG_NO_READ_RGB_TO_GRAY
  79.528 +#  define PNG_NO_READ_USER_TRANSFORM
  79.529 +#  define PNG_NO_WRITE_USER_TRANSFORM
  79.530 +#  define PNG_NO_USER_MEM
  79.531 +#  define PNG_NO_READ_EMPTY_PLTE
  79.532 +#  define PNG_NO_MNG_FEATURES
  79.533 +#  define PNG_NO_FIXED_POINT_SUPPORTED
  79.534 +#endif
  79.535 +
  79.536 +/* Ignore attempt to turn off both floating and fixed point support */
  79.537 +#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \
  79.538 +    !defined(PNG_NO_FIXED_POINT_SUPPORTED)
  79.539 +#  define PNG_FIXED_POINT_SUPPORTED
  79.540 +#endif
  79.541 +
  79.542 +#ifndef PNG_NO_FREE_ME
  79.543 +#  define PNG_FREE_ME_SUPPORTED
  79.544 +#endif
  79.545 +
  79.546 +#if defined(PNG_READ_SUPPORTED)
  79.547 +
  79.548 +#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \
  79.549 +      !defined(PNG_NO_READ_TRANSFORMS)
  79.550 +#  define PNG_READ_TRANSFORMS_SUPPORTED
  79.551 +#endif
  79.552 +
  79.553 +#ifdef PNG_READ_TRANSFORMS_SUPPORTED
  79.554 +#  ifndef PNG_NO_READ_EXPAND
  79.555 +#    define PNG_READ_EXPAND_SUPPORTED
  79.556 +#  endif
  79.557 +#  ifndef PNG_NO_READ_SHIFT
  79.558 +#    define PNG_READ_SHIFT_SUPPORTED
  79.559 +#  endif
  79.560 +#  ifndef PNG_NO_READ_PACK
  79.561 +#    define PNG_READ_PACK_SUPPORTED
  79.562 +#  endif
  79.563 +#  ifndef PNG_NO_READ_BGR
  79.564 +#    define PNG_READ_BGR_SUPPORTED
  79.565 +#  endif
  79.566 +#  ifndef PNG_NO_READ_SWAP
  79.567 +#    define PNG_READ_SWAP_SUPPORTED
  79.568 +#  endif
  79.569 +#  ifndef PNG_NO_READ_PACKSWAP
  79.570 +#    define PNG_READ_PACKSWAP_SUPPORTED
  79.571 +#  endif
  79.572 +#  ifndef PNG_NO_READ_INVERT
  79.573 +#    define PNG_READ_INVERT_SUPPORTED
  79.574 +#  endif
  79.575 +#  ifndef PNG_NO_READ_DITHER
  79.576 +#    define PNG_READ_DITHER_SUPPORTED
  79.577 +#  endif
  79.578 +#  ifndef PNG_NO_READ_BACKGROUND
  79.579 +#    define PNG_READ_BACKGROUND_SUPPORTED
  79.580 +#  endif
  79.581 +#  ifndef PNG_NO_READ_16_TO_8
  79.582 +#    define PNG_READ_16_TO_8_SUPPORTED
  79.583 +#  endif
  79.584 +#  ifndef PNG_NO_READ_FILLER
  79.585 +#    define PNG_READ_FILLER_SUPPORTED
  79.586 +#  endif
  79.587 +#  ifndef PNG_NO_READ_GAMMA
  79.588 +#    define PNG_READ_GAMMA_SUPPORTED
  79.589 +#  endif
  79.590 +#  ifndef PNG_NO_READ_GRAY_TO_RGB
  79.591 +#    define PNG_READ_GRAY_TO_RGB_SUPPORTED
  79.592 +#  endif
  79.593 +#  ifndef PNG_NO_READ_SWAP_ALPHA
  79.594 +#    define PNG_READ_SWAP_ALPHA_SUPPORTED
  79.595 +#  endif
  79.596 +#  ifndef PNG_NO_READ_INVERT_ALPHA
  79.597 +#    define PNG_READ_INVERT_ALPHA_SUPPORTED
  79.598 +#  endif
  79.599 +#  ifndef PNG_NO_READ_STRIP_ALPHA
  79.600 +#    define PNG_READ_STRIP_ALPHA_SUPPORTED
  79.601 +#  endif
  79.602 +#  ifndef PNG_NO_READ_USER_TRANSFORM
  79.603 +#    define PNG_READ_USER_TRANSFORM_SUPPORTED
  79.604 +#  endif
  79.605 +#  ifndef PNG_NO_READ_RGB_TO_GRAY
  79.606 +#    define PNG_READ_RGB_TO_GRAY_SUPPORTED
  79.607 +#  endif
  79.608 +#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
  79.609 +
  79.610 +#if !defined(PNG_NO_PROGRESSIVE_READ) && \
  79.611 + !defined(PNG_PROGRESSIVE_READ_SUPPORTED) /* if you don't do progressive   */
  79.612 +#  define PNG_PROGRESSIVE_READ_SUPPORTED  /* reading.  This is not talking */
  79.613 +#endif                            /* about interlacing capability!  You'll */
  79.614 +           /* still have interlacing unless you change the following line: */
  79.615 +
  79.616 +#define PNG_READ_INTERLACING_SUPPORTED /* required in PNG-compliant decoders */
  79.617 +
  79.618 +#ifndef PNG_NO_READ_COMPOSITE_NODIV
  79.619 +#  ifndef PNG_NO_READ_COMPOSITED_NODIV  /* libpng-1.0.x misspelling */
  79.620 +#    define PNG_READ_COMPOSITE_NODIV_SUPPORTED  /* well tested on Intel, SGI */
  79.621 +#  endif
  79.622 +#endif
  79.623 +
  79.624 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  79.625 +/* Deprecated, will be removed from version 2.0.0.
  79.626 +   Use PNG_MNG_FEATURES_SUPPORTED instead. */
  79.627 +#ifndef PNG_NO_READ_EMPTY_PLTE
  79.628 +#  define PNG_READ_EMPTY_PLTE_SUPPORTED
  79.629 +#endif
  79.630 +#endif
  79.631 +
  79.632 +#endif /* PNG_READ_SUPPORTED */
  79.633 +
  79.634 +#if defined(PNG_WRITE_SUPPORTED)
  79.635 +
  79.636 +# if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \
  79.637 +    !defined(PNG_NO_WRITE_TRANSFORMS)
  79.638 +#  define PNG_WRITE_TRANSFORMS_SUPPORTED
  79.639 +#endif
  79.640 +
  79.641 +#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
  79.642 +#  ifndef PNG_NO_WRITE_SHIFT
  79.643 +#    define PNG_WRITE_SHIFT_SUPPORTED
  79.644 +#  endif
  79.645 +#  ifndef PNG_NO_WRITE_PACK
  79.646 +#    define PNG_WRITE_PACK_SUPPORTED
  79.647 +#  endif
  79.648 +#  ifndef PNG_NO_WRITE_BGR
  79.649 +#    define PNG_WRITE_BGR_SUPPORTED
  79.650 +#  endif
  79.651 +#  ifndef PNG_NO_WRITE_SWAP
  79.652 +#    define PNG_WRITE_SWAP_SUPPORTED
  79.653 +#  endif
  79.654 +#  ifndef PNG_NO_WRITE_PACKSWAP
  79.655 +#    define PNG_WRITE_PACKSWAP_SUPPORTED
  79.656 +#  endif
  79.657 +#  ifndef PNG_NO_WRITE_INVERT
  79.658 +#    define PNG_WRITE_INVERT_SUPPORTED
  79.659 +#  endif
  79.660 +#  ifndef PNG_NO_WRITE_FILLER
  79.661 +#    define PNG_WRITE_FILLER_SUPPORTED   /* same as WRITE_STRIP_ALPHA */
  79.662 +#  endif
  79.663 +#  ifndef PNG_NO_WRITE_SWAP_ALPHA
  79.664 +#    define PNG_WRITE_SWAP_ALPHA_SUPPORTED
  79.665 +#  endif
  79.666 +#  ifndef PNG_NO_WRITE_INVERT_ALPHA
  79.667 +#    define PNG_WRITE_INVERT_ALPHA_SUPPORTED
  79.668 +#  endif
  79.669 +#  ifndef PNG_NO_WRITE_USER_TRANSFORM
  79.670 +#    define PNG_WRITE_USER_TRANSFORM_SUPPORTED
  79.671 +#  endif
  79.672 +#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
  79.673 +
  79.674 +#if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \
  79.675 +    !defined(PNG_WRITE_INTERLACING_SUPPORTED)
  79.676 +#define PNG_WRITE_INTERLACING_SUPPORTED  /* not required for PNG-compliant
  79.677 +                                            encoders, but can cause trouble
  79.678 +                                            if left undefined */
  79.679 +#endif
  79.680 +
  79.681 +#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \
  79.682 +    !defined(PNG_WRITE_WEIGHTED_FILTER) && \
  79.683 +     defined(PNG_FLOATING_POINT_SUPPORTED)
  79.684 +#  define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  79.685 +#endif
  79.686 +
  79.687 +#ifndef PNG_NO_WRITE_FLUSH
  79.688 +#  define PNG_WRITE_FLUSH_SUPPORTED
  79.689 +#endif
  79.690 +
  79.691 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  79.692 +/* Deprecated, see PNG_MNG_FEATURES_SUPPORTED, above */
  79.693 +#ifndef PNG_NO_WRITE_EMPTY_PLTE
  79.694 +#  define PNG_WRITE_EMPTY_PLTE_SUPPORTED
  79.695 +#endif
  79.696 +#endif
  79.697 +
  79.698 +#endif /* PNG_WRITE_SUPPORTED */
  79.699 +
  79.700 +#ifndef PNG_1_0_X
  79.701 +#  ifndef PNG_NO_ERROR_NUMBERS
  79.702 +#    define PNG_ERROR_NUMBERS_SUPPORTED
  79.703 +#  endif
  79.704 +#endif /* PNG_1_0_X */
  79.705 +
  79.706 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  79.707 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  79.708 +#  ifndef PNG_NO_USER_TRANSFORM_PTR
  79.709 +#    define PNG_USER_TRANSFORM_PTR_SUPPORTED
  79.710 +#  endif
  79.711 +#endif
  79.712 +
  79.713 +#ifndef PNG_NO_STDIO
  79.714 +#  define PNG_TIME_RFC1123_SUPPORTED
  79.715 +#endif
  79.716 +
  79.717 +/* This adds extra functions in pngget.c for accessing data from the
  79.718 + * info pointer (added in version 0.99)
  79.719 + * png_get_image_width()
  79.720 + * png_get_image_height()
  79.721 + * png_get_bit_depth()
  79.722 + * png_get_color_type()
  79.723 + * png_get_compression_type()
  79.724 + * png_get_filter_type()
  79.725 + * png_get_interlace_type()
  79.726 + * png_get_pixel_aspect_ratio()
  79.727 + * png_get_pixels_per_meter()
  79.728 + * png_get_x_offset_pixels()
  79.729 + * png_get_y_offset_pixels()
  79.730 + * png_get_x_offset_microns()
  79.731 + * png_get_y_offset_microns()
  79.732 + */
  79.733 +#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED)
  79.734 +#  define PNG_EASY_ACCESS_SUPPORTED
  79.735 +#endif
  79.736 +
  79.737 +/* PNG_ASSEMBLER_CODE was enabled by default in version 1.2.0 
  79.738 + * and removed from version 1.2.20.  The following will be removed
  79.739 + * from libpng-1.4.0
  79.740 +*/
  79.741 +
  79.742 +#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_OPTIMIZED_CODE)
  79.743 +#  ifndef PNG_OPTIMIZED_CODE_SUPPORTED
  79.744 +#    define PNG_OPTIMIZED_CODE_SUPPORTED
  79.745 +#  endif
  79.746 +#endif
  79.747 +
  79.748 +#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE)
  79.749 +#  ifndef PNG_ASSEMBLER_CODE_SUPPORTED
  79.750 +#    define PNG_ASSEMBLER_CODE_SUPPORTED
  79.751 +#  endif
  79.752 +
  79.753 +#  if defined(__GNUC__) && defined(__x86_64__) && (__GNUC__ < 4)
  79.754 +     /* work around 64-bit gcc compiler bugs in gcc-3.x */
  79.755 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  79.756 +#      define PNG_NO_MMX_CODE
  79.757 +#    endif
  79.758 +#  endif
  79.759 +
  79.760 +#  if defined(__APPLE__)
  79.761 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  79.762 +#      define PNG_NO_MMX_CODE
  79.763 +#    endif
  79.764 +#  endif
  79.765 +
  79.766 +#  if (defined(__MWERKS__) && ((__MWERKS__ < 0x0900) || macintosh))
  79.767 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  79.768 +#      define PNG_NO_MMX_CODE
  79.769 +#    endif
  79.770 +#  endif
  79.771 +
  79.772 +#  if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  79.773 +#    define PNG_MMX_CODE_SUPPORTED
  79.774 +#  endif
  79.775 +
  79.776 +#endif
  79.777 +/* end of obsolete code to be removed from libpng-1.4.0 */
  79.778 +
  79.779 +#if !defined(PNG_1_0_X)
  79.780 +#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED)
  79.781 +#  define PNG_USER_MEM_SUPPORTED
  79.782 +#endif
  79.783 +#endif /* PNG_1_0_X */
  79.784 +
  79.785 +/* Added at libpng-1.2.6 */
  79.786 +#if !defined(PNG_1_0_X)
  79.787 +#ifndef PNG_SET_USER_LIMITS_SUPPORTED
  79.788 +#if !defined(PNG_NO_SET_USER_LIMITS) && !defined(PNG_SET_USER_LIMITS_SUPPORTED)
  79.789 +#  define PNG_SET_USER_LIMITS_SUPPORTED
  79.790 +#endif
  79.791 +#endif
  79.792 +#endif /* PNG_1_0_X */
  79.793 +
  79.794 +/* Added at libpng-1.0.16 and 1.2.6.  To accept all valid PNGS no matter
  79.795 + * how large, set these limits to 0x7fffffffL
  79.796 + */
  79.797 +#ifndef PNG_USER_WIDTH_MAX
  79.798 +#  define PNG_USER_WIDTH_MAX 1000000L
  79.799 +#endif
  79.800 +#ifndef PNG_USER_HEIGHT_MAX
  79.801 +#  define PNG_USER_HEIGHT_MAX 1000000L
  79.802 +#endif
  79.803 +
  79.804 +/* These are currently experimental features, define them if you want */
  79.805 +
  79.806 +/* very little testing */
  79.807 +/*
  79.808 +#ifdef PNG_READ_SUPPORTED
  79.809 +#  ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
  79.810 +#    define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
  79.811 +#  endif
  79.812 +#endif
  79.813 +*/
  79.814 +
  79.815 +/* This is only for PowerPC big-endian and 680x0 systems */
  79.816 +/* some testing */
  79.817 +/*
  79.818 +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
  79.819 +#  define PNG_READ_BIG_ENDIAN_SUPPORTED
  79.820 +#endif
  79.821 +*/
  79.822 +
  79.823 +/* Buggy compilers (e.g., gcc 2.7.2.2) need this */
  79.824 +/*
  79.825 +#define PNG_NO_POINTER_INDEXING
  79.826 +*/
  79.827 +
  79.828 +/* These functions are turned off by default, as they will be phased out. */
  79.829 +/*
  79.830 +#define  PNG_USELESS_TESTS_SUPPORTED
  79.831 +#define  PNG_CORRECT_PALETTE_SUPPORTED
  79.832 +*/
  79.833 +
  79.834 +/* Any chunks you are not interested in, you can undef here.  The
  79.835 + * ones that allocate memory may be expecially important (hIST,
  79.836 + * tEXt, zTXt, tRNS, pCAL).  Others will just save time and make png_info
  79.837 + * a bit smaller.
  79.838 + */
  79.839 +
  79.840 +#if defined(PNG_READ_SUPPORTED) && \
  79.841 +    !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
  79.842 +    !defined(PNG_NO_READ_ANCILLARY_CHUNKS)
  79.843 +#  define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
  79.844 +#endif
  79.845 +
  79.846 +#if defined(PNG_WRITE_SUPPORTED) && \
  79.847 +    !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
  79.848 +    !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS)
  79.849 +#  define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
  79.850 +#endif
  79.851 +
  79.852 +#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
  79.853 +
  79.854 +#ifdef PNG_NO_READ_TEXT
  79.855 +#  define PNG_NO_READ_iTXt
  79.856 +#  define PNG_NO_READ_tEXt
  79.857 +#  define PNG_NO_READ_zTXt
  79.858 +#endif
  79.859 +#ifndef PNG_NO_READ_bKGD
  79.860 +#  define PNG_READ_bKGD_SUPPORTED
  79.861 +#  define PNG_bKGD_SUPPORTED
  79.862 +#endif
  79.863 +#ifndef PNG_NO_READ_cHRM
  79.864 +#  define PNG_READ_cHRM_SUPPORTED
  79.865 +#  define PNG_cHRM_SUPPORTED
  79.866 +#endif
  79.867 +#ifndef PNG_NO_READ_gAMA
  79.868 +#  define PNG_READ_gAMA_SUPPORTED
  79.869 +#  define PNG_gAMA_SUPPORTED
  79.870 +#endif
  79.871 +#ifndef PNG_NO_READ_hIST
  79.872 +#  define PNG_READ_hIST_SUPPORTED
  79.873 +#  define PNG_hIST_SUPPORTED
  79.874 +#endif
  79.875 +#ifndef PNG_NO_READ_iCCP
  79.876 +#  define PNG_READ_iCCP_SUPPORTED
  79.877 +#  define PNG_iCCP_SUPPORTED
  79.878 +#endif
  79.879 +#ifndef PNG_NO_READ_iTXt
  79.880 +#  ifndef PNG_READ_iTXt_SUPPORTED
  79.881 +#    define PNG_READ_iTXt_SUPPORTED
  79.882 +#  endif
  79.883 +#  ifndef PNG_iTXt_SUPPORTED
  79.884 +#    define PNG_iTXt_SUPPORTED
  79.885 +#  endif
  79.886 +#endif
  79.887 +#ifndef PNG_NO_READ_oFFs
  79.888 +#  define PNG_READ_oFFs_SUPPORTED
  79.889 +#  define PNG_oFFs_SUPPORTED
  79.890 +#endif
  79.891 +#ifndef PNG_NO_READ_pCAL
  79.892 +#  define PNG_READ_pCAL_SUPPORTED
  79.893 +#  define PNG_pCAL_SUPPORTED
  79.894 +#endif
  79.895 +#ifndef PNG_NO_READ_sCAL
  79.896 +#  define PNG_READ_sCAL_SUPPORTED
  79.897 +#  define PNG_sCAL_SUPPORTED
  79.898 +#endif
  79.899 +#ifndef PNG_NO_READ_pHYs
  79.900 +#  define PNG_READ_pHYs_SUPPORTED
  79.901 +#  define PNG_pHYs_SUPPORTED
  79.902 +#endif
  79.903 +#ifndef PNG_NO_READ_sBIT
  79.904 +#  define PNG_READ_sBIT_SUPPORTED
  79.905 +#  define PNG_sBIT_SUPPORTED
  79.906 +#endif
  79.907 +#ifndef PNG_NO_READ_sPLT
  79.908 +#  define PNG_READ_sPLT_SUPPORTED
  79.909 +#  define PNG_sPLT_SUPPORTED
  79.910 +#endif
  79.911 +#ifndef PNG_NO_READ_sRGB
  79.912 +#  define PNG_READ_sRGB_SUPPORTED
  79.913 +#  define PNG_sRGB_SUPPORTED
  79.914 +#endif
  79.915 +#ifndef PNG_NO_READ_tEXt
  79.916 +#  define PNG_READ_tEXt_SUPPORTED
  79.917 +#  define PNG_tEXt_SUPPORTED
  79.918 +#endif
  79.919 +#ifndef PNG_NO_READ_tIME
  79.920 +#  define PNG_READ_tIME_SUPPORTED
  79.921 +#  define PNG_tIME_SUPPORTED
  79.922 +#endif
  79.923 +#ifndef PNG_NO_READ_tRNS
  79.924 +#  define PNG_READ_tRNS_SUPPORTED
  79.925 +#  define PNG_tRNS_SUPPORTED
  79.926 +#endif
  79.927 +#ifndef PNG_NO_READ_zTXt
  79.928 +#  define PNG_READ_zTXt_SUPPORTED
  79.929 +#  define PNG_zTXt_SUPPORTED
  79.930 +#endif
  79.931 +#ifndef PNG_NO_READ_UNKNOWN_CHUNKS
  79.932 +#  define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  79.933 +#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
  79.934 +#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
  79.935 +#  endif
  79.936 +#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
  79.937 +#    define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  79.938 +#  endif
  79.939 +#endif
  79.940 +#if !defined(PNG_NO_READ_USER_CHUNKS) && \
  79.941 +     defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  79.942 +#  define PNG_READ_USER_CHUNKS_SUPPORTED
  79.943 +#  define PNG_USER_CHUNKS_SUPPORTED
  79.944 +#  ifdef PNG_NO_READ_UNKNOWN_CHUNKS
  79.945 +#    undef PNG_NO_READ_UNKNOWN_CHUNKS
  79.946 +#  endif
  79.947 +#  ifdef PNG_NO_HANDLE_AS_UNKNOWN
  79.948 +#    undef PNG_NO_HANDLE_AS_UNKNOWN
  79.949 +#  endif
  79.950 +#endif
  79.951 +#ifndef PNG_NO_READ_OPT_PLTE
  79.952 +#  define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */
  79.953 +#endif                      /* optional PLTE chunk in RGB and RGBA images */
  79.954 +#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \
  79.955 +    defined(PNG_READ_zTXt_SUPPORTED)
  79.956 +#  define PNG_READ_TEXT_SUPPORTED
  79.957 +#  define PNG_TEXT_SUPPORTED
  79.958 +#endif
  79.959 +
  79.960 +#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */
  79.961 +
  79.962 +#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
  79.963 +
  79.964 +#ifdef PNG_NO_WRITE_TEXT
  79.965 +#  define PNG_NO_WRITE_iTXt
  79.966 +#  define PNG_NO_WRITE_tEXt
  79.967 +#  define PNG_NO_WRITE_zTXt
  79.968 +#endif
  79.969 +#ifndef PNG_NO_WRITE_bKGD
  79.970 +#  define PNG_WRITE_bKGD_SUPPORTED
  79.971 +#  ifndef PNG_bKGD_SUPPORTED
  79.972 +#    define PNG_bKGD_SUPPORTED
  79.973 +#  endif
  79.974 +#endif
  79.975 +#ifndef PNG_NO_WRITE_cHRM
  79.976 +#  define PNG_WRITE_cHRM_SUPPORTED
  79.977 +#  ifndef PNG_cHRM_SUPPORTED
  79.978 +#    define PNG_cHRM_SUPPORTED
  79.979 +#  endif
  79.980 +#endif
  79.981 +#ifndef PNG_NO_WRITE_gAMA
  79.982 +#  define PNG_WRITE_gAMA_SUPPORTED
  79.983 +#  ifndef PNG_gAMA_SUPPORTED
  79.984 +#    define PNG_gAMA_SUPPORTED
  79.985 +#  endif
  79.986 +#endif
  79.987 +#ifndef PNG_NO_WRITE_hIST
  79.988 +#  define PNG_WRITE_hIST_SUPPORTED
  79.989 +#  ifndef PNG_hIST_SUPPORTED
  79.990 +#    define PNG_hIST_SUPPORTED
  79.991 +#  endif
  79.992 +#endif
  79.993 +#ifndef PNG_NO_WRITE_iCCP
  79.994 +#  define PNG_WRITE_iCCP_SUPPORTED
  79.995 +#  ifndef PNG_iCCP_SUPPORTED
  79.996 +#    define PNG_iCCP_SUPPORTED
  79.997 +#  endif
  79.998 +#endif
  79.999 +#ifndef PNG_NO_WRITE_iTXt
 79.1000 +#  ifndef PNG_WRITE_iTXt_SUPPORTED
 79.1001 +#    define PNG_WRITE_iTXt_SUPPORTED
 79.1002 +#  endif
 79.1003 +#  ifndef PNG_iTXt_SUPPORTED
 79.1004 +#    define PNG_iTXt_SUPPORTED
 79.1005 +#  endif
 79.1006 +#endif
 79.1007 +#ifndef PNG_NO_WRITE_oFFs
 79.1008 +#  define PNG_WRITE_oFFs_SUPPORTED
 79.1009 +#  ifndef PNG_oFFs_SUPPORTED
 79.1010 +#    define PNG_oFFs_SUPPORTED
 79.1011 +#  endif
 79.1012 +#endif
 79.1013 +#ifndef PNG_NO_WRITE_pCAL
 79.1014 +#  define PNG_WRITE_pCAL_SUPPORTED
 79.1015 +#  ifndef PNG_pCAL_SUPPORTED
 79.1016 +#    define PNG_pCAL_SUPPORTED
 79.1017 +#  endif
 79.1018 +#endif
 79.1019 +#ifndef PNG_NO_WRITE_sCAL
 79.1020 +#  define PNG_WRITE_sCAL_SUPPORTED
 79.1021 +#  ifndef PNG_sCAL_SUPPORTED
 79.1022 +#    define PNG_sCAL_SUPPORTED
 79.1023 +#  endif
 79.1024 +#endif
 79.1025 +#ifndef PNG_NO_WRITE_pHYs
 79.1026 +#  define PNG_WRITE_pHYs_SUPPORTED
 79.1027 +#  ifndef PNG_pHYs_SUPPORTED
 79.1028 +#    define PNG_pHYs_SUPPORTED
 79.1029 +#  endif
 79.1030 +#endif
 79.1031 +#ifndef PNG_NO_WRITE_sBIT
 79.1032 +#  define PNG_WRITE_sBIT_SUPPORTED
 79.1033 +#  ifndef PNG_sBIT_SUPPORTED
 79.1034 +#    define PNG_sBIT_SUPPORTED
 79.1035 +#  endif
 79.1036 +#endif
 79.1037 +#ifndef PNG_NO_WRITE_sPLT
 79.1038 +#  define PNG_WRITE_sPLT_SUPPORTED
 79.1039 +#  ifndef PNG_sPLT_SUPPORTED
 79.1040 +#    define PNG_sPLT_SUPPORTED
 79.1041 +#  endif
 79.1042 +#endif
 79.1043 +#ifndef PNG_NO_WRITE_sRGB
 79.1044 +#  define PNG_WRITE_sRGB_SUPPORTED
 79.1045 +#  ifndef PNG_sRGB_SUPPORTED
 79.1046 +#    define PNG_sRGB_SUPPORTED
 79.1047 +#  endif
 79.1048 +#endif
 79.1049 +#ifndef PNG_NO_WRITE_tEXt
 79.1050 +#  define PNG_WRITE_tEXt_SUPPORTED
 79.1051 +#  ifndef PNG_tEXt_SUPPORTED
 79.1052 +#    define PNG_tEXt_SUPPORTED
 79.1053 +#  endif
 79.1054 +#endif
 79.1055 +#ifndef PNG_NO_WRITE_tIME
 79.1056 +#  define PNG_WRITE_tIME_SUPPORTED
 79.1057 +#  ifndef PNG_tIME_SUPPORTED
 79.1058 +#    define PNG_tIME_SUPPORTED
 79.1059 +#  endif
 79.1060 +#endif
 79.1061 +#ifndef PNG_NO_WRITE_tRNS
 79.1062 +#  define PNG_WRITE_tRNS_SUPPORTED
 79.1063 +#  ifndef PNG_tRNS_SUPPORTED
 79.1064 +#    define PNG_tRNS_SUPPORTED
 79.1065 +#  endif
 79.1066 +#endif
 79.1067 +#ifndef PNG_NO_WRITE_zTXt
 79.1068 +#  define PNG_WRITE_zTXt_SUPPORTED
 79.1069 +#  ifndef PNG_zTXt_SUPPORTED
 79.1070 +#    define PNG_zTXt_SUPPORTED
 79.1071 +#  endif
 79.1072 +#endif
 79.1073 +#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS
 79.1074 +#  define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
 79.1075 +#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
 79.1076 +#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
 79.1077 +#  endif
 79.1078 +#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
 79.1079 +#     ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 79.1080 +#       define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 79.1081 +#     endif
 79.1082 +#  endif
 79.1083 +#endif
 79.1084 +#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
 79.1085 +    defined(PNG_WRITE_zTXt_SUPPORTED)
 79.1086 +#  define PNG_WRITE_TEXT_SUPPORTED
 79.1087 +#  ifndef PNG_TEXT_SUPPORTED
 79.1088 +#    define PNG_TEXT_SUPPORTED
 79.1089 +#  endif
 79.1090 +#endif
 79.1091 +
 79.1092 +#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */
 79.1093 +
 79.1094 +/* Turn this off to disable png_read_png() and
 79.1095 + * png_write_png() and leave the row_pointers member
 79.1096 + * out of the info structure.
 79.1097 + */
 79.1098 +#ifndef PNG_NO_INFO_IMAGE
 79.1099 +#  define PNG_INFO_IMAGE_SUPPORTED
 79.1100 +#endif
 79.1101 +
 79.1102 +/* need the time information for reading tIME chunks */
 79.1103 +#if defined(PNG_tIME_SUPPORTED)
 79.1104 +#  if !defined(_WIN32_WCE)
 79.1105 +     /* "time.h" functions are not supported on WindowsCE */
 79.1106 +#    include <time.h>
 79.1107 +#  endif
 79.1108 +#endif
 79.1109 +
 79.1110 +/* Some typedefs to get us started.  These should be safe on most of the
 79.1111 + * common platforms.  The typedefs should be at least as large as the
 79.1112 + * numbers suggest (a png_uint_32 must be at least 32 bits long), but they
 79.1113 + * don't have to be exactly that size.  Some compilers dislike passing
 79.1114 + * unsigned shorts as function parameters, so you may be better off using
 79.1115 + * unsigned int for png_uint_16.  Likewise, for 64-bit systems, you may
 79.1116 + * want to have unsigned int for png_uint_32 instead of unsigned long.
 79.1117 + */
 79.1118 +
 79.1119 +typedef unsigned long png_uint_32;
 79.1120 +typedef long png_int_32;
 79.1121 +typedef unsigned short png_uint_16;
 79.1122 +typedef short png_int_16;
 79.1123 +typedef unsigned char png_byte;
 79.1124 +
 79.1125 +/* This is usually size_t.  It is typedef'ed just in case you need it to
 79.1126 +   change (I'm not sure if you will or not, so I thought I'd be safe) */
 79.1127 +#ifdef PNG_SIZE_T
 79.1128 +   typedef PNG_SIZE_T png_size_t;
 79.1129 +#  define png_sizeof(x) png_convert_size(sizeof(x))
 79.1130 +#else
 79.1131 +   typedef size_t png_size_t;
 79.1132 +#  define png_sizeof(x) sizeof(x)
 79.1133 +#endif
 79.1134 +
 79.1135 +/* The following is needed for medium model support.  It cannot be in the
 79.1136 + * PNG_INTERNAL section.  Needs modification for other compilers besides
 79.1137 + * MSC.  Model independent support declares all arrays and pointers to be
 79.1138 + * large using the far keyword.  The zlib version used must also support
 79.1139 + * model independent data.  As of version zlib 1.0.4, the necessary changes
 79.1140 + * have been made in zlib.  The USE_FAR_KEYWORD define triggers other
 79.1141 + * changes that are needed. (Tim Wegner)
 79.1142 + */
 79.1143 +
 79.1144 +/* Separate compiler dependencies (problem here is that zlib.h always
 79.1145 +   defines FAR. (SJT) */
 79.1146 +#ifdef __BORLANDC__
 79.1147 +#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
 79.1148 +#    define LDATA 1
 79.1149 +#  else
 79.1150 +#    define LDATA 0
 79.1151 +#  endif
 79.1152 +   /* GRR:  why is Cygwin in here?  Cygwin is not Borland C... */
 79.1153 +#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)
 79.1154 +#    define PNG_MAX_MALLOC_64K
 79.1155 +#    if (LDATA != 1)
 79.1156 +#      ifndef FAR
 79.1157 +#        define FAR __far
 79.1158 +#      endif
 79.1159 +#      define USE_FAR_KEYWORD
 79.1160 +#    endif   /* LDATA != 1 */
 79.1161 +     /* Possibly useful for moving data out of default segment.
 79.1162 +      * Uncomment it if you want. Could also define FARDATA as
 79.1163 +      * const if your compiler supports it. (SJT)
 79.1164 +#    define FARDATA FAR
 79.1165 +      */
 79.1166 +#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */
 79.1167 +#endif   /* __BORLANDC__ */
 79.1168 +
 79.1169 +
 79.1170 +/* Suggest testing for specific compiler first before testing for
 79.1171 + * FAR.  The Watcom compiler defines both __MEDIUM__ and M_I86MM,
 79.1172 + * making reliance oncertain keywords suspect. (SJT)
 79.1173 + */
 79.1174 +
 79.1175 +/* MSC Medium model */
 79.1176 +#if defined(FAR)
 79.1177 +#  if defined(M_I86MM)
 79.1178 +#    define USE_FAR_KEYWORD
 79.1179 +#    define FARDATA FAR
 79.1180 +#    include <dos.h>
 79.1181 +#  endif
 79.1182 +#endif
 79.1183 +
 79.1184 +/* SJT: default case */
 79.1185 +#ifndef FAR
 79.1186 +#  define FAR
 79.1187 +#endif
 79.1188 +
 79.1189 +/* At this point FAR is always defined */
 79.1190 +#ifndef FARDATA
 79.1191 +#  define FARDATA
 79.1192 +#endif
 79.1193 +
 79.1194 +/* Typedef for floating-point numbers that are converted
 79.1195 +   to fixed-point with a multiple of 100,000, e.g., int_gamma */
 79.1196 +typedef png_int_32 png_fixed_point;
 79.1197 +
 79.1198 +/* Add typedefs for pointers */
 79.1199 +typedef void            FAR * png_voidp;
 79.1200 +typedef png_byte        FAR * png_bytep;
 79.1201 +typedef png_uint_32     FAR * png_uint_32p;
 79.1202 +typedef png_int_32      FAR * png_int_32p;
 79.1203 +typedef png_uint_16     FAR * png_uint_16p;
 79.1204 +typedef png_int_16      FAR * png_int_16p;
 79.1205 +typedef PNG_CONST char  FAR * png_const_charp;
 79.1206 +typedef char            FAR * png_charp;
 79.1207 +typedef png_fixed_point FAR * png_fixed_point_p;
 79.1208 +
 79.1209 +#ifndef PNG_NO_STDIO
 79.1210 +#if defined(_WIN32_WCE)
 79.1211 +typedef HANDLE                png_FILE_p;
 79.1212 +#else
 79.1213 +typedef FILE                * png_FILE_p;
 79.1214 +#endif
 79.1215 +#endif
 79.1216 +
 79.1217 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 79.1218 +typedef double          FAR * png_doublep;
 79.1219 +#endif
 79.1220 +
 79.1221 +/* Pointers to pointers; i.e. arrays */
 79.1222 +typedef png_byte        FAR * FAR * png_bytepp;
 79.1223 +typedef png_uint_32     FAR * FAR * png_uint_32pp;
 79.1224 +typedef png_int_32      FAR * FAR * png_int_32pp;
 79.1225 +typedef png_uint_16     FAR * FAR * png_uint_16pp;
 79.1226 +typedef png_int_16      FAR * FAR * png_int_16pp;
 79.1227 +typedef PNG_CONST char  FAR * FAR * png_const_charpp;
 79.1228 +typedef char            FAR * FAR * png_charpp;
 79.1229 +typedef png_fixed_point FAR * FAR * png_fixed_point_pp;
 79.1230 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 79.1231 +typedef double          FAR * FAR * png_doublepp;
 79.1232 +#endif
 79.1233 +
 79.1234 +/* Pointers to pointers to pointers; i.e., pointer to array */
 79.1235 +typedef char            FAR * FAR * FAR * png_charppp;
 79.1236 +
 79.1237 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
 79.1238 +/* SPC -  Is this stuff deprecated? */
 79.1239 +/* It'll be removed as of libpng-1.3.0 - GR-P */
 79.1240 +/* libpng typedefs for types in zlib. If zlib changes
 79.1241 + * or another compression library is used, then change these.
 79.1242 + * Eliminates need to change all the source files.
 79.1243 + */
 79.1244 +typedef charf *         png_zcharp;
 79.1245 +typedef charf * FAR *   png_zcharpp;
 79.1246 +typedef z_stream FAR *  png_zstreamp;
 79.1247 +#endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */
 79.1248 +
 79.1249 +/*
 79.1250 + * Define PNG_BUILD_DLL if the module being built is a Windows
 79.1251 + * LIBPNG DLL.
 79.1252 + *
 79.1253 + * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL.
 79.1254 + * It is equivalent to Microsoft predefined macro _DLL that is
 79.1255 + * automatically defined when you compile using the share
 79.1256 + * version of the CRT (C Run-Time library)
 79.1257 + *
 79.1258 + * The cygwin mods make this behavior a little different:
 79.1259 + * Define PNG_BUILD_DLL if you are building a dll for use with cygwin
 79.1260 + * Define PNG_STATIC if you are building a static library for use with cygwin,
 79.1261 + *   -or- if you are building an application that you want to link to the
 79.1262 + *   static library.
 79.1263 + * PNG_USE_DLL is defined by default (no user action needed) unless one of
 79.1264 + *   the other flags is defined.
 79.1265 + */
 79.1266 +
 79.1267 +#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL))
 79.1268 +#  define PNG_DLL
 79.1269 +#endif
 79.1270 +/* If CYGWIN, then disallow GLOBAL ARRAYS unless building a static lib.
 79.1271 + * When building a static lib, default to no GLOBAL ARRAYS, but allow
 79.1272 + * command-line override
 79.1273 + */
 79.1274 +#if defined(__CYGWIN__)
 79.1275 +#  if !defined(PNG_STATIC)
 79.1276 +#    if defined(PNG_USE_GLOBAL_ARRAYS)
 79.1277 +#      undef PNG_USE_GLOBAL_ARRAYS
 79.1278 +#    endif
 79.1279 +#    if !defined(PNG_USE_LOCAL_ARRAYS)
 79.1280 +#      define PNG_USE_LOCAL_ARRAYS
 79.1281 +#    endif
 79.1282 +#  else
 79.1283 +#    if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS)
 79.1284 +#      if defined(PNG_USE_GLOBAL_ARRAYS)
 79.1285 +#        undef PNG_USE_GLOBAL_ARRAYS
 79.1286 +#      endif
 79.1287 +#    endif
 79.1288 +#  endif
 79.1289 +#  if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
 79.1290 +#    define PNG_USE_LOCAL_ARRAYS
 79.1291 +#  endif
 79.1292 +#endif
 79.1293 +
 79.1294 +/* Do not use global arrays (helps with building DLL's)
 79.1295 + * They are no longer used in libpng itself, since version 1.0.5c,
 79.1296 + * but might be required for some pre-1.0.5c applications.
 79.1297 + */
 79.1298 +#if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
 79.1299 +#  if defined(PNG_NO_GLOBAL_ARRAYS) || \
 79.1300 +      (defined(__GNUC__) && defined(PNG_DLL)) || defined(_MSC_VER)
 79.1301 +#    define PNG_USE_LOCAL_ARRAYS
 79.1302 +#  else
 79.1303 +#    define PNG_USE_GLOBAL_ARRAYS
 79.1304 +#  endif
 79.1305 +#endif
 79.1306 +
 79.1307 +#if defined(__CYGWIN__)
 79.1308 +#  undef PNGAPI
 79.1309 +#  define PNGAPI __cdecl
 79.1310 +#  undef PNG_IMPEXP
 79.1311 +#  define PNG_IMPEXP
 79.1312 +#endif  
 79.1313 +
 79.1314 +/* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall",
 79.1315 + * you may get warnings regarding the linkage of png_zalloc and png_zfree.
 79.1316 + * Don't ignore those warnings; you must also reset the default calling
 79.1317 + * convention in your compiler to match your PNGAPI, and you must build
 79.1318 + * zlib and your applications the same way you build libpng.
 79.1319 + */
 79.1320 +
 79.1321 +#if defined(__MINGW32__) && !defined(PNG_MODULEDEF)
 79.1322 +#  ifndef PNG_NO_MODULEDEF
 79.1323 +#    define PNG_NO_MODULEDEF
 79.1324 +#  endif
 79.1325 +#endif
 79.1326 +
 79.1327 +#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF)
 79.1328 +#  define PNG_IMPEXP
 79.1329 +#endif
 79.1330 +
 79.1331 +#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \
 79.1332 +    (( defined(_Windows) || defined(_WINDOWS) || \
 79.1333 +       defined(WIN32) || defined(_WIN32) || defined(__WIN32__) ))
 79.1334 +
 79.1335 +#  ifndef PNGAPI
 79.1336 +#     if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
 79.1337 +#        define PNGAPI __cdecl
 79.1338 +#     else
 79.1339 +#        define PNGAPI _cdecl
 79.1340 +#     endif
 79.1341 +#  endif
 79.1342 +
 79.1343 +#  if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \
 79.1344 +       0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */)
 79.1345 +#     define PNG_IMPEXP
 79.1346 +#  endif
 79.1347 +
 79.1348 +#  if !defined(PNG_IMPEXP)
 79.1349 +
 79.1350 +#     define PNG_EXPORT_TYPE1(type,symbol)  PNG_IMPEXP type PNGAPI symbol
 79.1351 +#     define PNG_EXPORT_TYPE2(type,symbol)  type PNG_IMPEXP PNGAPI symbol
 79.1352 +
 79.1353 +      /* Borland/Microsoft */
 79.1354 +#     if defined(_MSC_VER) || defined(__BORLANDC__)
 79.1355 +#        if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500)
 79.1356 +#           define PNG_EXPORT PNG_EXPORT_TYPE1
 79.1357 +#        else
 79.1358 +#           define PNG_EXPORT PNG_EXPORT_TYPE2
 79.1359 +#           if defined(PNG_BUILD_DLL)
 79.1360 +#              define PNG_IMPEXP __export
 79.1361 +#           else
 79.1362 +#              define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in
 79.1363 +                                                 VC++ */
 79.1364 +#           endif                             /* Exists in Borland C++ for
 79.1365 +                                                 C++ classes (== huge) */
 79.1366 +#        endif
 79.1367 +#     endif
 79.1368 +
 79.1369 +#     if !defined(PNG_IMPEXP)
 79.1370 +#        if defined(PNG_BUILD_DLL)
 79.1371 +#           define PNG_IMPEXP __declspec(dllexport)
 79.1372 +#        else
 79.1373 +#           define PNG_IMPEXP __declspec(dllimport)
 79.1374 +#        endif
 79.1375 +#     endif
 79.1376 +#  endif  /* PNG_IMPEXP */
 79.1377 +#else /* !(DLL || non-cygwin WINDOWS) */
 79.1378 +#   if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)
 79.1379 +#      ifndef PNGAPI
 79.1380 +#         define PNGAPI _System
 79.1381 +#      endif
 79.1382 +#   else
 79.1383 +#      if 0 /* ... other platforms, with other meanings */
 79.1384 +#      endif
 79.1385 +#   endif
 79.1386 +#endif
 79.1387 +
 79.1388 +#ifndef PNGAPI
 79.1389 +#  define PNGAPI
 79.1390 +#endif
 79.1391 +#ifndef PNG_IMPEXP
 79.1392 +#  define PNG_IMPEXP
 79.1393 +#endif
 79.1394 +
 79.1395 +#ifdef PNG_BUILDSYMS
 79.1396 +#  ifndef PNG_EXPORT
 79.1397 +#    define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END
 79.1398 +#  endif
 79.1399 +#  ifdef PNG_USE_GLOBAL_ARRAYS
 79.1400 +#    ifndef PNG_EXPORT_VAR
 79.1401 +#      define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT
 79.1402 +#    endif
 79.1403 +#  endif
 79.1404 +#endif
 79.1405 +
 79.1406 +#ifndef PNG_EXPORT
 79.1407 +#  define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol
 79.1408 +#endif
 79.1409 +
 79.1410 +#ifdef PNG_USE_GLOBAL_ARRAYS
 79.1411 +#  ifndef PNG_EXPORT_VAR
 79.1412 +#    define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type
 79.1413 +#  endif
 79.1414 +#endif
 79.1415 +
 79.1416 +/* User may want to use these so they are not in PNG_INTERNAL. Any library
 79.1417 + * functions that are passed far data must be model independent.
 79.1418 + */
 79.1419 +
 79.1420 +#ifndef PNG_ABORT
 79.1421 +#  define PNG_ABORT() abort()
 79.1422 +#endif
 79.1423 +
 79.1424 +#ifdef PNG_SETJMP_SUPPORTED
 79.1425 +#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
 79.1426 +#else
 79.1427 +#  define png_jmpbuf(png_ptr) \
 79.1428 +   (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED)
 79.1429 +#endif
 79.1430 +
 79.1431 +#if defined(USE_FAR_KEYWORD)  /* memory model independent fns */
 79.1432 +/* use this to make far-to-near assignments */
 79.1433 +#  define CHECK   1
 79.1434 +#  define NOCHECK 0
 79.1435 +#  define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK))
 79.1436 +#  define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK))
 79.1437 +#  define png_snprintf _fsnprintf   /* Added to v 1.2.19 */
 79.1438 +#  define png_strlen  _fstrlen
 79.1439 +#  define png_memcmp  _fmemcmp    /* SJT: added */
 79.1440 +#  define png_memcpy  _fmemcpy
 79.1441 +#  define png_memset  _fmemset
 79.1442 +#else /* use the usual functions */
 79.1443 +#  define CVT_PTR(ptr)         (ptr)
 79.1444 +#  define CVT_PTR_NOCHECK(ptr) (ptr)
 79.1445 +#  ifndef PNG_NO_SNPRINTF
 79.1446 +#    ifdef _MSC_VER
 79.1447 +#      define png_snprintf _snprintf   /* Added to v 1.2.19 */
 79.1448 +#      define png_snprintf2 _snprintf
 79.1449 +#      define png_snprintf6 _snprintf
 79.1450 +#    else
 79.1451 +#      define png_snprintf snprintf   /* Added to v 1.2.19 */
 79.1452 +#      define png_snprintf2 snprintf
 79.1453 +#      define png_snprintf6 snprintf
 79.1454 +#    endif
 79.1455 +#  else
 79.1456 +     /* You don't have or don't want to use snprintf().  Caution: Using
 79.1457 +      * sprintf instead of snprintf exposes your application to accidental
 79.1458 +      * or malevolent buffer overflows.  If you don't have snprintf()
 79.1459 +      * as a general rule you should provide one (you can get one from
 79.1460 +      * Portable OpenSSH). */
 79.1461 +#    define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1)
 79.1462 +#    define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2)
 79.1463 +#    define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \
 79.1464 +        sprintf(s1,fmt,x1,x2,x3,x4,x5,x6)
 79.1465 +#  endif
 79.1466 +#  define png_strlen  strlen
 79.1467 +#  define png_memcmp  memcmp      /* SJT: added */
 79.1468 +#  define png_memcpy  memcpy
 79.1469 +#  define png_memset  memset
 79.1470 +#endif
 79.1471 +/* End of memory model independent support */
 79.1472 +
 79.1473 +/* Just a little check that someone hasn't tried to define something
 79.1474 + * contradictory.
 79.1475 + */
 79.1476 +#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K)
 79.1477 +#  undef PNG_ZBUF_SIZE
 79.1478 +#  define PNG_ZBUF_SIZE 65536L
 79.1479 +#endif
 79.1480 +
 79.1481 +/* Added at libpng-1.2.8 */
 79.1482 +#endif /* PNG_VERSION_INFO_ONLY */
 79.1483 +
 79.1484 +#endif /* PNGCONF_H */
    80.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    80.2 +++ b/libs/libpng/pngerror.c	Sun Jun 07 17:25:49 2015 +0300
    80.3 @@ -0,0 +1,345 @@
    80.4 +
    80.5 +/* pngerror.c - stub functions for i/o and memory allocation
    80.6 + *
    80.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    80.8 + * For conditions of distribution and use, see copyright notice in png.h
    80.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   80.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   80.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   80.12 + *
   80.13 + * This file provides a location for all error handling.  Users who
   80.14 + * need special error handling are expected to write replacement functions
   80.15 + * and use png_set_error_fn() to use those functions.  See the instructions
   80.16 + * at each function.
   80.17 + */
   80.18 +
   80.19 +#define PNG_INTERNAL
   80.20 +#include "png.h"
   80.21 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   80.22 +
   80.23 +static void /* PRIVATE */
   80.24 +png_default_error PNGARG((png_structp png_ptr,
   80.25 +  png_const_charp error_message));
   80.26 +#ifndef PNG_NO_WARNINGS
   80.27 +static void /* PRIVATE */
   80.28 +png_default_warning PNGARG((png_structp png_ptr,
   80.29 +  png_const_charp warning_message));
   80.30 +#endif /* PNG_NO_WARNINGS */
   80.31 +
   80.32 +/* This function is called whenever there is a fatal error.  This function
   80.33 + * should not be changed.  If there is a need to handle errors differently,
   80.34 + * you should supply a replacement error function and use png_set_error_fn()
   80.35 + * to replace the error function at run-time.
   80.36 + */
   80.37 +#ifndef PNG_NO_ERROR_TEXT
   80.38 +void PNGAPI
   80.39 +png_error(png_structp png_ptr, png_const_charp error_message)
   80.40 +{
   80.41 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   80.42 +   char msg[16];
   80.43 +   if (png_ptr != NULL)
   80.44 +   {
   80.45 +     if (png_ptr->flags&
   80.46 +       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
   80.47 +     {
   80.48 +       if (*error_message == '#')
   80.49 +       {
   80.50 +         /* Strip "#nnnn " from beginning of error message. */
   80.51 +           int offset;
   80.52 +           for (offset = 1; offset<15; offset++)
   80.53 +              if (error_message[offset] == ' ')
   80.54 +                  break;
   80.55 +           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
   80.56 +           {
   80.57 +              int i;
   80.58 +              for (i = 0; i < offset - 1; i++)
   80.59 +                 msg[i] = error_message[i + 1];
   80.60 +              msg[i - 1] = '\0';
   80.61 +              error_message = msg;
   80.62 +           }
   80.63 +           else
   80.64 +              error_message += offset;
   80.65 +       }
   80.66 +       else
   80.67 +       {
   80.68 +           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
   80.69 +           {
   80.70 +              msg[0] = '0';
   80.71 +              msg[1] = '\0';
   80.72 +              error_message = msg;
   80.73 +           }
   80.74 +       }
   80.75 +     }
   80.76 +   }
   80.77 +#endif
   80.78 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
   80.79 +      (*(png_ptr->error_fn))(png_ptr, error_message);
   80.80 +
   80.81 +   /* If the custom handler doesn't exist, or if it returns,
   80.82 +      use the default handler, which will not return. */
   80.83 +   png_default_error(png_ptr, error_message);
   80.84 +}
   80.85 +#else
   80.86 +void PNGAPI
   80.87 +png_err(png_structp png_ptr)
   80.88 +{
   80.89 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
   80.90 +      (*(png_ptr->error_fn))(png_ptr, '\0');
   80.91 +
   80.92 +   /* If the custom handler doesn't exist, or if it returns,
   80.93 +      use the default handler, which will not return. */
   80.94 +   png_default_error(png_ptr, '\0');
   80.95 +}
   80.96 +#endif /* PNG_NO_ERROR_TEXT */
   80.97 +
   80.98 +#ifndef PNG_NO_WARNINGS
   80.99 +/* This function is called whenever there is a non-fatal error.  This function
  80.100 + * should not be changed.  If there is a need to handle warnings differently,
  80.101 + * you should supply a replacement warning function and use
  80.102 + * png_set_error_fn() to replace the warning function at run-time.
  80.103 + */
  80.104 +void PNGAPI
  80.105 +png_warning(png_structp png_ptr, png_const_charp warning_message)
  80.106 +{
  80.107 +   int offset = 0;
  80.108 +   if (png_ptr != NULL)
  80.109 +   {
  80.110 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  80.111 +   if (png_ptr->flags&
  80.112 +     (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  80.113 +#endif
  80.114 +     {
  80.115 +       if (*warning_message == '#')
  80.116 +       {
  80.117 +           for (offset = 1; offset < 15; offset++)
  80.118 +              if (warning_message[offset] == ' ')
  80.119 +                  break;
  80.120 +       }
  80.121 +     }
  80.122 +     if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  80.123 +        (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
  80.124 +   }
  80.125 +   else
  80.126 +      png_default_warning(png_ptr, warning_message + offset);
  80.127 +}
  80.128 +#endif /* PNG_NO_WARNINGS */
  80.129 +
  80.130 +
  80.131 +/* These utilities are used internally to build an error message that relates
  80.132 + * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
  80.133 + * this is used to prefix the message.  The message is limited in length
  80.134 + * to 63 bytes, the name characters are output as hex digits wrapped in []
  80.135 + * if the character is invalid.
  80.136 + */
  80.137 +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  80.138 +static PNG_CONST char png_digit[16] = {
  80.139 +   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  80.140 +   'A', 'B', 'C', 'D', 'E', 'F'
  80.141 +};
  80.142 +
  80.143 +#define PNG_MAX_ERROR_TEXT 64
  80.144 +
  80.145 +#if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
  80.146 +static void /* PRIVATE */
  80.147 +png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  80.148 +   error_message)
  80.149 +{
  80.150 +   int iout = 0, iin = 0;
  80.151 +
  80.152 +   while (iin < 4)
  80.153 +   {
  80.154 +      int c = png_ptr->chunk_name[iin++];
  80.155 +      if (isnonalpha(c))
  80.156 +      {
  80.157 +         buffer[iout++] = '[';
  80.158 +         buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  80.159 +         buffer[iout++] = png_digit[c & 0x0f];
  80.160 +         buffer[iout++] = ']';
  80.161 +      }
  80.162 +      else
  80.163 +      {
  80.164 +         buffer[iout++] = (png_byte)c;
  80.165 +      }
  80.166 +   }
  80.167 +
  80.168 +   if (error_message == NULL)
  80.169 +      buffer[iout] = '\0';
  80.170 +   else
  80.171 +   {
  80.172 +      buffer[iout++] = ':';
  80.173 +      buffer[iout++] = ' ';
  80.174 +      png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);
  80.175 +      buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';
  80.176 +   }
  80.177 +}
  80.178 +
  80.179 +#ifdef PNG_READ_SUPPORTED
  80.180 +void PNGAPI
  80.181 +png_chunk_error(png_structp png_ptr, png_const_charp error_message)
  80.182 +{
  80.183 +   char msg[18+PNG_MAX_ERROR_TEXT];
  80.184 +   if (png_ptr == NULL)
  80.185 +     png_error(png_ptr, error_message);
  80.186 +   else
  80.187 +   {
  80.188 +     png_format_buffer(png_ptr, msg, error_message);
  80.189 +     png_error(png_ptr, msg);
  80.190 +   }
  80.191 +}
  80.192 +#endif /* PNG_READ_SUPPORTED */
  80.193 +#endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
  80.194 +
  80.195 +#ifndef PNG_NO_WARNINGS
  80.196 +void PNGAPI
  80.197 +png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  80.198 +{
  80.199 +   char msg[18+PNG_MAX_ERROR_TEXT];
  80.200 +   if (png_ptr == NULL)
  80.201 +     png_warning(png_ptr, warning_message);
  80.202 +   else
  80.203 +   {
  80.204 +     png_format_buffer(png_ptr, msg, warning_message);
  80.205 +     png_warning(png_ptr, msg);
  80.206 +   }
  80.207 +}
  80.208 +#endif /* PNG_NO_WARNINGS */
  80.209 +
  80.210 +
  80.211 +/* This is the default error handling function.  Note that replacements for
  80.212 + * this function MUST NOT RETURN, or the program will likely crash.  This
  80.213 + * function is used by default, or if the program supplies NULL for the
  80.214 + * error function pointer in png_set_error_fn().
  80.215 + */
  80.216 +static void /* PRIVATE */
  80.217 +png_default_error(png_structp png_ptr, png_const_charp error_message)
  80.218 +{
  80.219 +#ifndef PNG_NO_CONSOLE_IO
  80.220 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  80.221 +   if (*error_message == '#')
  80.222 +   {
  80.223 +     /* Strip "#nnnn " from beginning of warning message. */
  80.224 +     int offset;
  80.225 +     char error_number[16];
  80.226 +     for (offset = 0; offset<15; offset++)
  80.227 +     {
  80.228 +         error_number[offset] = error_message[offset + 1];
  80.229 +         if (error_message[offset] == ' ')
  80.230 +             break;
  80.231 +     }
  80.232 +     if ((offset > 1) && (offset < 15))
  80.233 +     {
  80.234 +       error_number[offset - 1] = '\0';
  80.235 +       fprintf(stderr, "libpng error no. %s: %s\n", error_number,
  80.236 +          error_message + offset + 1);
  80.237 +     }
  80.238 +     else
  80.239 +       fprintf(stderr, "libpng error: %s, offset=%d\n", error_message, offset);
  80.240 +   }
  80.241 +   else
  80.242 +#endif
  80.243 +   fprintf(stderr, "libpng error: %s\n", error_message);
  80.244 +#endif
  80.245 +
  80.246 +#ifdef PNG_SETJMP_SUPPORTED
  80.247 +   if (png_ptr)
  80.248 +   {
  80.249 +#  ifdef USE_FAR_KEYWORD
  80.250 +   {
  80.251 +      jmp_buf jmpbuf;
  80.252 +      png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  80.253 +      longjmp(jmpbuf, 1);
  80.254 +   }
  80.255 +#  else
  80.256 +   longjmp(png_ptr->jmpbuf, 1);
  80.257 +#  endif
  80.258 +   }
  80.259 +#else
  80.260 +   PNG_ABORT();
  80.261 +#endif
  80.262 +#ifdef PNG_NO_CONSOLE_IO
  80.263 +   error_message = error_message; /* make compiler happy */
  80.264 +#endif
  80.265 +}
  80.266 +
  80.267 +#ifndef PNG_NO_WARNINGS
  80.268 +/* This function is called when there is a warning, but the library thinks
  80.269 + * it can continue anyway.  Replacement functions don't have to do anything
  80.270 + * here if you don't want them to.  In the default configuration, png_ptr is
  80.271 + * not used, but it is passed in case it may be useful.
  80.272 + */
  80.273 +static void /* PRIVATE */
  80.274 +png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  80.275 +{
  80.276 +#ifndef PNG_NO_CONSOLE_IO
  80.277 +#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
  80.278 +   if (*warning_message == '#')
  80.279 +   {
  80.280 +     int offset;
  80.281 +     char warning_number[16];
  80.282 +     for (offset = 0; offset < 15; offset++)
  80.283 +     {
  80.284 +        warning_number[offset] = warning_message[offset + 1];
  80.285 +        if (warning_message[offset] == ' ')
  80.286 +            break;
  80.287 +     }
  80.288 +     if ((offset > 1) && (offset < 15))
  80.289 +     {
  80.290 +       warning_number[offset + 1] = '\0';
  80.291 +       fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
  80.292 +          warning_message + offset);
  80.293 +     }
  80.294 +     else
  80.295 +       fprintf(stderr, "libpng warning: %s\n", warning_message);
  80.296 +   }
  80.297 +   else
  80.298 +#  endif
  80.299 +     fprintf(stderr, "libpng warning: %s\n", warning_message);
  80.300 +#else
  80.301 +   warning_message = warning_message; /* make compiler happy */
  80.302 +#endif
  80.303 +   png_ptr = png_ptr; /* make compiler happy */
  80.304 +}
  80.305 +#endif /* PNG_NO_WARNINGS */
  80.306 +
  80.307 +/* This function is called when the application wants to use another method
  80.308 + * of handling errors and warnings.  Note that the error function MUST NOT
  80.309 + * return to the calling routine or serious problems will occur.  The return
  80.310 + * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  80.311 + */
  80.312 +void PNGAPI
  80.313 +png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  80.314 +   png_error_ptr error_fn, png_error_ptr warning_fn)
  80.315 +{
  80.316 +   if (png_ptr == NULL)
  80.317 +      return;
  80.318 +   png_ptr->error_ptr = error_ptr;
  80.319 +   png_ptr->error_fn = error_fn;
  80.320 +   png_ptr->warning_fn = warning_fn;
  80.321 +}
  80.322 +
  80.323 +
  80.324 +/* This function returns a pointer to the error_ptr associated with the user
  80.325 + * functions.  The application should free any memory associated with this
  80.326 + * pointer before png_write_destroy and png_read_destroy are called.
  80.327 + */
  80.328 +png_voidp PNGAPI
  80.329 +png_get_error_ptr(png_structp png_ptr)
  80.330 +{
  80.331 +   if (png_ptr == NULL)
  80.332 +      return NULL;
  80.333 +   return ((png_voidp)png_ptr->error_ptr);
  80.334 +}
  80.335 +
  80.336 +
  80.337 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  80.338 +void PNGAPI
  80.339 +png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  80.340 +{
  80.341 +   if (png_ptr != NULL)
  80.342 +   {
  80.343 +     png_ptr->flags &=
  80.344 +       ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  80.345 +   }
  80.346 +}
  80.347 +#endif
  80.348 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    81.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    81.2 +++ b/libs/libpng/pnggccrd.c	Sun Jun 07 17:25:49 2015 +0300
    81.3 @@ -0,0 +1,103 @@
    81.4 +/* pnggccrd.c was removed from libpng-1.2.20. */
    81.5 +
    81.6 +/* This code snippet is for use by configure's compilation test. */
    81.7 +
    81.8 +#if (!defined _MSC_VER) && \
    81.9 +    defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
   81.10 +    defined(PNG_MMX_CODE_SUPPORTED)
   81.11 +
   81.12 +int PNGAPI png_dummy_mmx_support(void);
   81.13 +
   81.14 +static int _mmx_supported = 2; // 0: no MMX; 1: MMX supported; 2: not tested
   81.15 +
   81.16 +int PNGAPI
   81.17 +png_dummy_mmx_support(void) __attribute__((noinline));
   81.18 +
   81.19 +int PNGAPI
   81.20 +png_dummy_mmx_support(void)
   81.21 +{
   81.22 +   int result;
   81.23 +#if defined(PNG_MMX_CODE_SUPPORTED)  // superfluous, but what the heck
   81.24 +    __asm__ __volatile__ (
   81.25 +#if defined(__x86_64__)
   81.26 +        "pushq %%rbx          \n\t"  // rbx gets clobbered by CPUID instruction
   81.27 +        "pushq %%rcx          \n\t"  // so does rcx...
   81.28 +        "pushq %%rdx          \n\t"  // ...and rdx (but rcx & rdx safe on Linux)
   81.29 +        "pushfq               \n\t"  // save Eflag to stack
   81.30 +        "popq %%rax           \n\t"  // get Eflag from stack into rax
   81.31 +        "movq %%rax, %%rcx    \n\t"  // make another copy of Eflag in rcx
   81.32 +        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
   81.33 +        "pushq %%rax          \n\t"  // save modified Eflag back to stack
   81.34 +        "popfq                \n\t"  // restore modified value to Eflag reg
   81.35 +        "pushfq               \n\t"  // save Eflag to stack
   81.36 +        "popq %%rax           \n\t"  // get Eflag from stack
   81.37 +        "pushq %%rcx          \n\t"  // save original Eflag to stack
   81.38 +        "popfq                \n\t"  // restore original Eflag
   81.39 +#else
   81.40 +        "pushl %%ebx          \n\t"  // ebx gets clobbered by CPUID instruction
   81.41 +        "pushl %%ecx          \n\t"  // so does ecx...
   81.42 +        "pushl %%edx          \n\t"  // ...and edx (but ecx & edx safe on Linux)
   81.43 +        "pushfl               \n\t"  // save Eflag to stack
   81.44 +        "popl %%eax           \n\t"  // get Eflag from stack into eax
   81.45 +        "movl %%eax, %%ecx    \n\t"  // make another copy of Eflag in ecx
   81.46 +        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
   81.47 +        "pushl %%eax          \n\t"  // save modified Eflag back to stack
   81.48 +        "popfl                \n\t"  // restore modified value to Eflag reg
   81.49 +        "pushfl               \n\t"  // save Eflag to stack
   81.50 +        "popl %%eax           \n\t"  // get Eflag from stack
   81.51 +        "pushl %%ecx          \n\t"  // save original Eflag to stack
   81.52 +        "popfl                \n\t"  // restore original Eflag
   81.53 +#endif
   81.54 +        "xorl %%ecx, %%eax    \n\t"  // compare new Eflag with original Eflag
   81.55 +        "jz 0f                \n\t"  // if same, CPUID instr. is not supported
   81.56 +
   81.57 +        "xorl %%eax, %%eax    \n\t"  // set eax to zero
   81.58 +//      ".byte  0x0f, 0xa2    \n\t"  // CPUID instruction (two-byte opcode)
   81.59 +        "cpuid                \n\t"  // get the CPU identification info
   81.60 +        "cmpl $1, %%eax       \n\t"  // make sure eax return non-zero value
   81.61 +        "jl 0f                \n\t"  // if eax is zero, MMX is not supported
   81.62 +
   81.63 +        "xorl %%eax, %%eax    \n\t"  // set eax to zero and...
   81.64 +        "incl %%eax           \n\t"  // ...increment eax to 1.  This pair is
   81.65 +                                     // faster than the instruction "mov eax, 1"
   81.66 +        "cpuid                \n\t"  // get the CPU identification info again
   81.67 +        "andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23)
   81.68 +        "cmpl $0, %%edx       \n\t"  // 0 = MMX not supported
   81.69 +        "jz 0f                \n\t"  // non-zero = yes, MMX IS supported
   81.70 +
   81.71 +        "movl $1, %%eax       \n\t"  // set return value to 1
   81.72 +        "jmp  1f              \n\t"  // DONE:  have MMX support
   81.73 +
   81.74 +    "0:                       \n\t"  // .NOT_SUPPORTED: target label for jump instructions
   81.75 +        "movl $0, %%eax       \n\t"  // set return value to 0
   81.76 +    "1:                       \n\t"  // .RETURN: target label for jump instructions
   81.77 +#if defined(__x86_64__)
   81.78 +        "popq %%rdx           \n\t"  // restore rdx
   81.79 +        "popq %%rcx           \n\t"  // restore rcx
   81.80 +        "popq %%rbx           \n\t"  // restore rbx
   81.81 +#else
   81.82 +        "popl %%edx           \n\t"  // restore edx
   81.83 +        "popl %%ecx           \n\t"  // restore ecx
   81.84 +        "popl %%ebx           \n\t"  // restore ebx
   81.85 +#endif
   81.86 +
   81.87 +//      "ret                  \n\t"  // DONE:  no MMX support
   81.88 +                                     // (fall through to standard C "ret")
   81.89 +
   81.90 +        : "=a" (result)              // output list
   81.91 +
   81.92 +        :                            // any variables used on input (none)
   81.93 +
   81.94 +                                     // no clobber list
   81.95 +//      , "%ebx", "%ecx", "%edx"     // GRR:  we handle these manually
   81.96 +//      , "memory"   // if write to a variable gcc thought was in a reg
   81.97 +//      , "cc"       // "condition codes" (flag bits)
   81.98 +    );
   81.99 +    _mmx_supported = result;
  81.100 +#else
  81.101 +    _mmx_supported = 0;
  81.102 +#endif /* PNG_MMX_CODE_SUPPORTED */
  81.103 +
  81.104 +    return _mmx_supported;
  81.105 +}
  81.106 +#endif
    82.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    82.2 +++ b/libs/libpng/pngget.c	Sun Jun 07 17:25:49 2015 +0300
    82.3 @@ -0,0 +1,900 @@
    82.4 +
    82.5 +/* pngget.c - retrieval of values from info struct
    82.6 + *
    82.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    82.8 + * For conditions of distribution and use, see copyright notice in png.h
    82.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   82.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   82.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   82.12 + */
   82.13 +
   82.14 +#define PNG_INTERNAL
   82.15 +#include "png.h"
   82.16 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   82.17 +
   82.18 +png_uint_32 PNGAPI
   82.19 +png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
   82.20 +{
   82.21 +   if (png_ptr != NULL && info_ptr != NULL)
   82.22 +      return(info_ptr->valid & flag);
   82.23 +   else
   82.24 +      return(0);
   82.25 +}
   82.26 +
   82.27 +png_uint_32 PNGAPI
   82.28 +png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
   82.29 +{
   82.30 +   if (png_ptr != NULL && info_ptr != NULL)
   82.31 +      return(info_ptr->rowbytes);
   82.32 +   else
   82.33 +      return(0);
   82.34 +}
   82.35 +
   82.36 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
   82.37 +png_bytepp PNGAPI
   82.38 +png_get_rows(png_structp png_ptr, png_infop info_ptr)
   82.39 +{
   82.40 +   if (png_ptr != NULL && info_ptr != NULL)
   82.41 +      return(info_ptr->row_pointers);
   82.42 +   else
   82.43 +      return(0);
   82.44 +}
   82.45 +#endif
   82.46 +
   82.47 +#ifdef PNG_EASY_ACCESS_SUPPORTED
   82.48 +/* easy access to info, added in libpng-0.99 */
   82.49 +png_uint_32 PNGAPI
   82.50 +png_get_image_width(png_structp png_ptr, png_infop info_ptr)
   82.51 +{
   82.52 +   if (png_ptr != NULL && info_ptr != NULL)
   82.53 +   {
   82.54 +      return info_ptr->width;
   82.55 +   }
   82.56 +   return (0);
   82.57 +}
   82.58 +
   82.59 +png_uint_32 PNGAPI
   82.60 +png_get_image_height(png_structp png_ptr, png_infop info_ptr)
   82.61 +{
   82.62 +   if (png_ptr != NULL && info_ptr != NULL)
   82.63 +   {
   82.64 +      return info_ptr->height;
   82.65 +   }
   82.66 +   return (0);
   82.67 +}
   82.68 +
   82.69 +png_byte PNGAPI
   82.70 +png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
   82.71 +{
   82.72 +   if (png_ptr != NULL && info_ptr != NULL)
   82.73 +   {
   82.74 +      return info_ptr->bit_depth;
   82.75 +   }
   82.76 +   return (0);
   82.77 +}
   82.78 +
   82.79 +png_byte PNGAPI
   82.80 +png_get_color_type(png_structp png_ptr, png_infop info_ptr)
   82.81 +{
   82.82 +   if (png_ptr != NULL && info_ptr != NULL)
   82.83 +   {
   82.84 +      return info_ptr->color_type;
   82.85 +   }
   82.86 +   return (0);
   82.87 +}
   82.88 +
   82.89 +png_byte PNGAPI
   82.90 +png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
   82.91 +{
   82.92 +   if (png_ptr != NULL && info_ptr != NULL)
   82.93 +   {
   82.94 +      return info_ptr->filter_type;
   82.95 +   }
   82.96 +   return (0);
   82.97 +}
   82.98 +
   82.99 +png_byte PNGAPI
  82.100 +png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
  82.101 +{
  82.102 +   if (png_ptr != NULL && info_ptr != NULL)
  82.103 +   {
  82.104 +      return info_ptr->interlace_type;
  82.105 +   }
  82.106 +   return (0);
  82.107 +}
  82.108 +
  82.109 +png_byte PNGAPI
  82.110 +png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
  82.111 +{
  82.112 +   if (png_ptr != NULL && info_ptr != NULL)
  82.113 +   {
  82.114 +      return info_ptr->compression_type;
  82.115 +   }
  82.116 +   return (0);
  82.117 +}
  82.118 +
  82.119 +png_uint_32 PNGAPI
  82.120 +png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  82.121 +{
  82.122 +   if (png_ptr != NULL && info_ptr != NULL)
  82.123 +#if defined(PNG_pHYs_SUPPORTED)
  82.124 +   if (info_ptr->valid & PNG_INFO_pHYs)
  82.125 +   {
  82.126 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
  82.127 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
  82.128 +          return (0);
  82.129 +      else return (info_ptr->x_pixels_per_unit);
  82.130 +   }
  82.131 +#else
  82.132 +   return (0);
  82.133 +#endif
  82.134 +   return (0);
  82.135 +}
  82.136 +
  82.137 +png_uint_32 PNGAPI
  82.138 +png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  82.139 +{
  82.140 +   if (png_ptr != NULL && info_ptr != NULL)
  82.141 +#if defined(PNG_pHYs_SUPPORTED)
  82.142 +   if (info_ptr->valid & PNG_INFO_pHYs)
  82.143 +   {
  82.144 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
  82.145 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
  82.146 +          return (0);
  82.147 +      else return (info_ptr->y_pixels_per_unit);
  82.148 +   }
  82.149 +#else
  82.150 +   return (0);
  82.151 +#endif
  82.152 +   return (0);
  82.153 +}
  82.154 +
  82.155 +png_uint_32 PNGAPI
  82.156 +png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  82.157 +{
  82.158 +   if (png_ptr != NULL && info_ptr != NULL)
  82.159 +#if defined(PNG_pHYs_SUPPORTED)
  82.160 +   if (info_ptr->valid & PNG_INFO_pHYs)
  82.161 +   {
  82.162 +      png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
  82.163 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
  82.164 +         info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
  82.165 +          return (0);
  82.166 +      else return (info_ptr->x_pixels_per_unit);
  82.167 +   }
  82.168 +#else
  82.169 +   return (0);
  82.170 +#endif
  82.171 +   return (0);
  82.172 +}
  82.173 +
  82.174 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  82.175 +float PNGAPI
  82.176 +png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
  82.177 +   {
  82.178 +   if (png_ptr != NULL && info_ptr != NULL)
  82.179 +#if defined(PNG_pHYs_SUPPORTED)
  82.180 +   if (info_ptr->valid & PNG_INFO_pHYs)
  82.181 +   {
  82.182 +      png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
  82.183 +      if (info_ptr->x_pixels_per_unit == 0)
  82.184 +         return ((float)0.0);
  82.185 +      else
  82.186 +         return ((float)((float)info_ptr->y_pixels_per_unit
  82.187 +            /(float)info_ptr->x_pixels_per_unit));
  82.188 +   }
  82.189 +#else
  82.190 +   return (0.0);
  82.191 +#endif
  82.192 +   return ((float)0.0);
  82.193 +}
  82.194 +#endif
  82.195 +
  82.196 +png_int_32 PNGAPI
  82.197 +png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
  82.198 +{
  82.199 +   if (png_ptr != NULL && info_ptr != NULL)
  82.200 +#if defined(PNG_oFFs_SUPPORTED)
  82.201 +   if (info_ptr->valid & PNG_INFO_oFFs)
  82.202 +   {
  82.203 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
  82.204 +      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
  82.205 +          return (0);
  82.206 +      else return (info_ptr->x_offset);
  82.207 +   }
  82.208 +#else
  82.209 +   return (0);
  82.210 +#endif
  82.211 +   return (0);
  82.212 +}
  82.213 +
  82.214 +png_int_32 PNGAPI
  82.215 +png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
  82.216 +{
  82.217 +   if (png_ptr != NULL && info_ptr != NULL)
  82.218 +#if defined(PNG_oFFs_SUPPORTED)
  82.219 +   if (info_ptr->valid & PNG_INFO_oFFs)
  82.220 +   {
  82.221 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
  82.222 +      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
  82.223 +          return (0);
  82.224 +      else return (info_ptr->y_offset);
  82.225 +   }
  82.226 +#else
  82.227 +   return (0);
  82.228 +#endif
  82.229 +   return (0);
  82.230 +}
  82.231 +
  82.232 +png_int_32 PNGAPI
  82.233 +png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
  82.234 +{
  82.235 +   if (png_ptr != NULL && info_ptr != NULL)
  82.236 +#if defined(PNG_oFFs_SUPPORTED)
  82.237 +   if (info_ptr->valid & PNG_INFO_oFFs)
  82.238 +   {
  82.239 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
  82.240 +      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
  82.241 +          return (0);
  82.242 +      else return (info_ptr->x_offset);
  82.243 +   }
  82.244 +#else
  82.245 +   return (0);
  82.246 +#endif
  82.247 +   return (0);
  82.248 +}
  82.249 +
  82.250 +png_int_32 PNGAPI
  82.251 +png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
  82.252 +{
  82.253 +   if (png_ptr != NULL && info_ptr != NULL)
  82.254 +#if defined(PNG_oFFs_SUPPORTED)
  82.255 +   if (info_ptr->valid & PNG_INFO_oFFs)
  82.256 +   {
  82.257 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
  82.258 +      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
  82.259 +          return (0);
  82.260 +      else return (info_ptr->y_offset);
  82.261 +   }
  82.262 +#else
  82.263 +   return (0);
  82.264 +#endif
  82.265 +   return (0);
  82.266 +}
  82.267 +
  82.268 +#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
  82.269 +png_uint_32 PNGAPI
  82.270 +png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  82.271 +{
  82.272 +   return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)
  82.273 +     *.0254 +.5));
  82.274 +}
  82.275 +
  82.276 +png_uint_32 PNGAPI
  82.277 +png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  82.278 +{
  82.279 +   return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)
  82.280 +     *.0254 +.5));
  82.281 +}
  82.282 +
  82.283 +png_uint_32 PNGAPI
  82.284 +png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  82.285 +{
  82.286 +   return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)
  82.287 +     *.0254 +.5));
  82.288 +}
  82.289 +
  82.290 +float PNGAPI
  82.291 +png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
  82.292 +{
  82.293 +   return ((float)png_get_x_offset_microns(png_ptr, info_ptr)
  82.294 +     *.00003937);
  82.295 +}
  82.296 +
  82.297 +float PNGAPI
  82.298 +png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
  82.299 +{
  82.300 +   return ((float)png_get_y_offset_microns(png_ptr, info_ptr)
  82.301 +     *.00003937);
  82.302 +}
  82.303 +
  82.304 +#if defined(PNG_pHYs_SUPPORTED)
  82.305 +png_uint_32 PNGAPI
  82.306 +png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
  82.307 +   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
  82.308 +{
  82.309 +   png_uint_32 retval = 0;
  82.310 +
  82.311 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  82.312 +   {
  82.313 +      png_debug1(1, "in %s retrieval function\n", "pHYs");
  82.314 +      if (res_x != NULL)
  82.315 +      {
  82.316 +         *res_x = info_ptr->x_pixels_per_unit;
  82.317 +         retval |= PNG_INFO_pHYs;
  82.318 +      }
  82.319 +      if (res_y != NULL)
  82.320 +      {
  82.321 +         *res_y = info_ptr->y_pixels_per_unit;
  82.322 +         retval |= PNG_INFO_pHYs;
  82.323 +      }
  82.324 +      if (unit_type != NULL)
  82.325 +      {
  82.326 +         *unit_type = (int)info_ptr->phys_unit_type;
  82.327 +         retval |= PNG_INFO_pHYs;
  82.328 +         if (*unit_type == 1)
  82.329 +         {
  82.330 +            if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
  82.331 +            if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
  82.332 +         }
  82.333 +      }
  82.334 +   }
  82.335 +   return (retval);
  82.336 +}
  82.337 +#endif /* PNG_pHYs_SUPPORTED */
  82.338 +#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
  82.339 +
  82.340 +/* png_get_channels really belongs in here, too, but it's been around longer */
  82.341 +
  82.342 +#endif  /* PNG_EASY_ACCESS_SUPPORTED */
  82.343 +
  82.344 +png_byte PNGAPI
  82.345 +png_get_channels(png_structp png_ptr, png_infop info_ptr)
  82.346 +{
  82.347 +   if (png_ptr != NULL && info_ptr != NULL)
  82.348 +      return(info_ptr->channels);
  82.349 +   else
  82.350 +      return (0);
  82.351 +}
  82.352 +
  82.353 +png_bytep PNGAPI
  82.354 +png_get_signature(png_structp png_ptr, png_infop info_ptr)
  82.355 +{
  82.356 +   if (png_ptr != NULL && info_ptr != NULL)
  82.357 +      return(info_ptr->signature);
  82.358 +   else
  82.359 +      return (NULL);
  82.360 +}
  82.361 +
  82.362 +#if defined(PNG_bKGD_SUPPORTED)
  82.363 +png_uint_32 PNGAPI
  82.364 +png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
  82.365 +   png_color_16p *background)
  82.366 +{
  82.367 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
  82.368 +      && background != NULL)
  82.369 +   {
  82.370 +      png_debug1(1, "in %s retrieval function\n", "bKGD");
  82.371 +      *background = &(info_ptr->background);
  82.372 +      return (PNG_INFO_bKGD);
  82.373 +   }
  82.374 +   return (0);
  82.375 +}
  82.376 +#endif
  82.377 +
  82.378 +#if defined(PNG_cHRM_SUPPORTED)
  82.379 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  82.380 +png_uint_32 PNGAPI
  82.381 +png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
  82.382 +   double *white_x, double *white_y, double *red_x, double *red_y,
  82.383 +   double *green_x, double *green_y, double *blue_x, double *blue_y)
  82.384 +{
  82.385 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  82.386 +   {
  82.387 +      png_debug1(1, "in %s retrieval function\n", "cHRM");
  82.388 +      if (white_x != NULL)
  82.389 +         *white_x = (double)info_ptr->x_white;
  82.390 +      if (white_y != NULL)
  82.391 +         *white_y = (double)info_ptr->y_white;
  82.392 +      if (red_x != NULL)
  82.393 +         *red_x = (double)info_ptr->x_red;
  82.394 +      if (red_y != NULL)
  82.395 +         *red_y = (double)info_ptr->y_red;
  82.396 +      if (green_x != NULL)
  82.397 +         *green_x = (double)info_ptr->x_green;
  82.398 +      if (green_y != NULL)
  82.399 +         *green_y = (double)info_ptr->y_green;
  82.400 +      if (blue_x != NULL)
  82.401 +         *blue_x = (double)info_ptr->x_blue;
  82.402 +      if (blue_y != NULL)
  82.403 +         *blue_y = (double)info_ptr->y_blue;
  82.404 +      return (PNG_INFO_cHRM);
  82.405 +   }
  82.406 +   return (0);
  82.407 +}
  82.408 +#endif
  82.409 +#ifdef PNG_FIXED_POINT_SUPPORTED
  82.410 +png_uint_32 PNGAPI
  82.411 +png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  82.412 +   png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
  82.413 +   png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
  82.414 +   png_fixed_point *blue_x, png_fixed_point *blue_y)
  82.415 +{
  82.416 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  82.417 +   {
  82.418 +      png_debug1(1, "in %s retrieval function\n", "cHRM");
  82.419 +      if (white_x != NULL)
  82.420 +         *white_x = info_ptr->int_x_white;
  82.421 +      if (white_y != NULL)
  82.422 +         *white_y = info_ptr->int_y_white;
  82.423 +      if (red_x != NULL)
  82.424 +         *red_x = info_ptr->int_x_red;
  82.425 +      if (red_y != NULL)
  82.426 +         *red_y = info_ptr->int_y_red;
  82.427 +      if (green_x != NULL)
  82.428 +         *green_x = info_ptr->int_x_green;
  82.429 +      if (green_y != NULL)
  82.430 +         *green_y = info_ptr->int_y_green;
  82.431 +      if (blue_x != NULL)
  82.432 +         *blue_x = info_ptr->int_x_blue;
  82.433 +      if (blue_y != NULL)
  82.434 +         *blue_y = info_ptr->int_y_blue;
  82.435 +      return (PNG_INFO_cHRM);
  82.436 +   }
  82.437 +   return (0);
  82.438 +}
  82.439 +#endif
  82.440 +#endif
  82.441 +
  82.442 +#if defined(PNG_gAMA_SUPPORTED)
  82.443 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  82.444 +png_uint_32 PNGAPI
  82.445 +png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
  82.446 +{
  82.447 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  82.448 +      && file_gamma != NULL)
  82.449 +   {
  82.450 +      png_debug1(1, "in %s retrieval function\n", "gAMA");
  82.451 +      *file_gamma = (double)info_ptr->gamma;
  82.452 +      return (PNG_INFO_gAMA);
  82.453 +   }
  82.454 +   return (0);
  82.455 +}
  82.456 +#endif
  82.457 +#ifdef PNG_FIXED_POINT_SUPPORTED
  82.458 +png_uint_32 PNGAPI
  82.459 +png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,
  82.460 +    png_fixed_point *int_file_gamma)
  82.461 +{
  82.462 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  82.463 +      && int_file_gamma != NULL)
  82.464 +   {
  82.465 +      png_debug1(1, "in %s retrieval function\n", "gAMA");
  82.466 +      *int_file_gamma = info_ptr->int_gamma;
  82.467 +      return (PNG_INFO_gAMA);
  82.468 +   }
  82.469 +   return (0);
  82.470 +}
  82.471 +#endif
  82.472 +#endif
  82.473 +
  82.474 +#if defined(PNG_sRGB_SUPPORTED)
  82.475 +png_uint_32 PNGAPI
  82.476 +png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
  82.477 +{
  82.478 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
  82.479 +      && file_srgb_intent != NULL)
  82.480 +   {
  82.481 +      png_debug1(1, "in %s retrieval function\n", "sRGB");
  82.482 +      *file_srgb_intent = (int)info_ptr->srgb_intent;
  82.483 +      return (PNG_INFO_sRGB);
  82.484 +   }
  82.485 +   return (0);
  82.486 +}
  82.487 +#endif
  82.488 +
  82.489 +#if defined(PNG_iCCP_SUPPORTED)
  82.490 +png_uint_32 PNGAPI
  82.491 +png_get_iCCP(png_structp png_ptr, png_infop info_ptr,
  82.492 +             png_charpp name, int *compression_type,
  82.493 +             png_charpp profile, png_uint_32 *proflen)
  82.494 +{
  82.495 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
  82.496 +      && name != NULL && profile != NULL && proflen != NULL)
  82.497 +   {
  82.498 +      png_debug1(1, "in %s retrieval function\n", "iCCP");
  82.499 +      *name = info_ptr->iccp_name;
  82.500 +      *profile = info_ptr->iccp_profile;
  82.501 +      /* compression_type is a dummy so the API won't have to change
  82.502 +         if we introduce multiple compression types later. */
  82.503 +      *proflen = (int)info_ptr->iccp_proflen;
  82.504 +      *compression_type = (int)info_ptr->iccp_compression;
  82.505 +      return (PNG_INFO_iCCP);
  82.506 +   }
  82.507 +   return (0);
  82.508 +}
  82.509 +#endif
  82.510 +
  82.511 +#if defined(PNG_sPLT_SUPPORTED)
  82.512 +png_uint_32 PNGAPI
  82.513 +png_get_sPLT(png_structp png_ptr, png_infop info_ptr,
  82.514 +             png_sPLT_tpp spalettes)
  82.515 +{
  82.516 +   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
  82.517 +   {
  82.518 +     *spalettes = info_ptr->splt_palettes;
  82.519 +     return ((png_uint_32)info_ptr->splt_palettes_num);
  82.520 +   }
  82.521 +   return (0);
  82.522 +}
  82.523 +#endif
  82.524 +
  82.525 +#if defined(PNG_hIST_SUPPORTED)
  82.526 +png_uint_32 PNGAPI
  82.527 +png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
  82.528 +{
  82.529 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
  82.530 +      && hist != NULL)
  82.531 +   {
  82.532 +      png_debug1(1, "in %s retrieval function\n", "hIST");
  82.533 +      *hist = info_ptr->hist;
  82.534 +      return (PNG_INFO_hIST);
  82.535 +   }
  82.536 +   return (0);
  82.537 +}
  82.538 +#endif
  82.539 +
  82.540 +png_uint_32 PNGAPI
  82.541 +png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
  82.542 +   png_uint_32 *width, png_uint_32 *height, int *bit_depth,
  82.543 +   int *color_type, int *interlace_type, int *compression_type,
  82.544 +   int *filter_type)
  82.545 +
  82.546 +{
  82.547 +   if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
  82.548 +      bit_depth != NULL && color_type != NULL)
  82.549 +   {
  82.550 +      png_debug1(1, "in %s retrieval function\n", "IHDR");
  82.551 +      *width = info_ptr->width;
  82.552 +      *height = info_ptr->height;
  82.553 +      *bit_depth = info_ptr->bit_depth;
  82.554 +      if (info_ptr->bit_depth < 1 || info_ptr->bit_depth > 16)
  82.555 +        png_error(png_ptr, "Invalid bit depth");
  82.556 +      *color_type = info_ptr->color_type;
  82.557 +      if (info_ptr->color_type > 6)
  82.558 +        png_error(png_ptr, "Invalid color type");
  82.559 +      if (compression_type != NULL)
  82.560 +         *compression_type = info_ptr->compression_type;
  82.561 +      if (filter_type != NULL)
  82.562 +         *filter_type = info_ptr->filter_type;
  82.563 +      if (interlace_type != NULL)
  82.564 +         *interlace_type = info_ptr->interlace_type;
  82.565 +
  82.566 +      /* check for potential overflow of rowbytes */
  82.567 +      if (*width == 0 || *width > PNG_UINT_31_MAX)
  82.568 +        png_error(png_ptr, "Invalid image width");
  82.569 +      if (*height == 0 || *height > PNG_UINT_31_MAX)
  82.570 +        png_error(png_ptr, "Invalid image height");
  82.571 +      if (info_ptr->width > (PNG_UINT_32_MAX
  82.572 +                 >> 3)      /* 8-byte RGBA pixels */
  82.573 +                 - 64       /* bigrowbuf hack */
  82.574 +                 - 1        /* filter byte */
  82.575 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  82.576 +                 - 8)       /* extra max_pixel_depth pad */
  82.577 +      {
  82.578 +         png_warning(png_ptr,
  82.579 +            "Width too large for libpng to process image data.");
  82.580 +      }
  82.581 +      return (1);
  82.582 +   }
  82.583 +   return (0);
  82.584 +}
  82.585 +
  82.586 +#if defined(PNG_oFFs_SUPPORTED)
  82.587 +png_uint_32 PNGAPI
  82.588 +png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
  82.589 +   png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
  82.590 +{
  82.591 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
  82.592 +      && offset_x != NULL && offset_y != NULL && unit_type != NULL)
  82.593 +   {
  82.594 +      png_debug1(1, "in %s retrieval function\n", "oFFs");
  82.595 +      *offset_x = info_ptr->x_offset;
  82.596 +      *offset_y = info_ptr->y_offset;
  82.597 +      *unit_type = (int)info_ptr->offset_unit_type;
  82.598 +      return (PNG_INFO_oFFs);
  82.599 +   }
  82.600 +   return (0);
  82.601 +}
  82.602 +#endif
  82.603 +
  82.604 +#if defined(PNG_pCAL_SUPPORTED)
  82.605 +png_uint_32 PNGAPI
  82.606 +png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
  82.607 +   png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
  82.608 +   png_charp *units, png_charpp *params)
  82.609 +{
  82.610 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
  82.611 +      && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
  82.612 +      nparams != NULL && units != NULL && params != NULL)
  82.613 +   {
  82.614 +      png_debug1(1, "in %s retrieval function\n", "pCAL");
  82.615 +      *purpose = info_ptr->pcal_purpose;
  82.616 +      *X0 = info_ptr->pcal_X0;
  82.617 +      *X1 = info_ptr->pcal_X1;
  82.618 +      *type = (int)info_ptr->pcal_type;
  82.619 +      *nparams = (int)info_ptr->pcal_nparams;
  82.620 +      *units = info_ptr->pcal_units;
  82.621 +      *params = info_ptr->pcal_params;
  82.622 +      return (PNG_INFO_pCAL);
  82.623 +   }
  82.624 +   return (0);
  82.625 +}
  82.626 +#endif
  82.627 +
  82.628 +#if defined(PNG_sCAL_SUPPORTED)
  82.629 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  82.630 +png_uint_32 PNGAPI
  82.631 +png_get_sCAL(png_structp png_ptr, png_infop info_ptr,
  82.632 +             int *unit, double *width, double *height)
  82.633 +{
  82.634 +    if (png_ptr != NULL && info_ptr != NULL &&
  82.635 +       (info_ptr->valid & PNG_INFO_sCAL))
  82.636 +    {
  82.637 +        *unit = info_ptr->scal_unit;
  82.638 +        *width = info_ptr->scal_pixel_width;
  82.639 +        *height = info_ptr->scal_pixel_height;
  82.640 +        return (PNG_INFO_sCAL);
  82.641 +    }
  82.642 +    return(0);
  82.643 +}
  82.644 +#else
  82.645 +#ifdef PNG_FIXED_POINT_SUPPORTED
  82.646 +png_uint_32 PNGAPI
  82.647 +png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  82.648 +             int *unit, png_charpp width, png_charpp height)
  82.649 +{
  82.650 +    if (png_ptr != NULL && info_ptr != NULL &&
  82.651 +       (info_ptr->valid & PNG_INFO_sCAL))
  82.652 +    {
  82.653 +        *unit = info_ptr->scal_unit;
  82.654 +        *width = info_ptr->scal_s_width;
  82.655 +        *height = info_ptr->scal_s_height;
  82.656 +        return (PNG_INFO_sCAL);
  82.657 +    }
  82.658 +    return(0);
  82.659 +}
  82.660 +#endif
  82.661 +#endif
  82.662 +#endif
  82.663 +
  82.664 +#if defined(PNG_pHYs_SUPPORTED)
  82.665 +png_uint_32 PNGAPI
  82.666 +png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
  82.667 +   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
  82.668 +{
  82.669 +   png_uint_32 retval = 0;
  82.670 +
  82.671 +   if (png_ptr != NULL && info_ptr != NULL &&
  82.672 +      (info_ptr->valid & PNG_INFO_pHYs))
  82.673 +   {
  82.674 +      png_debug1(1, "in %s retrieval function\n", "pHYs");
  82.675 +      if (res_x != NULL)
  82.676 +      {
  82.677 +         *res_x = info_ptr->x_pixels_per_unit;
  82.678 +         retval |= PNG_INFO_pHYs;
  82.679 +      }
  82.680 +      if (res_y != NULL)
  82.681 +      {
  82.682 +         *res_y = info_ptr->y_pixels_per_unit;
  82.683 +         retval |= PNG_INFO_pHYs;
  82.684 +      }
  82.685 +      if (unit_type != NULL)
  82.686 +      {
  82.687 +         *unit_type = (int)info_ptr->phys_unit_type;
  82.688 +         retval |= PNG_INFO_pHYs;
  82.689 +      }
  82.690 +   }
  82.691 +   return (retval);
  82.692 +}
  82.693 +#endif
  82.694 +
  82.695 +png_uint_32 PNGAPI
  82.696 +png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
  82.697 +   int *num_palette)
  82.698 +{
  82.699 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
  82.700 +       && palette != NULL)
  82.701 +   {
  82.702 +      png_debug1(1, "in %s retrieval function\n", "PLTE");
  82.703 +      *palette = info_ptr->palette;
  82.704 +      *num_palette = info_ptr->num_palette;
  82.705 +      png_debug1(3, "num_palette = %d\n", *num_palette);
  82.706 +      return (PNG_INFO_PLTE);
  82.707 +   }
  82.708 +   return (0);
  82.709 +}
  82.710 +
  82.711 +#if defined(PNG_sBIT_SUPPORTED)
  82.712 +png_uint_32 PNGAPI
  82.713 +png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
  82.714 +{
  82.715 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
  82.716 +      && sig_bit != NULL)
  82.717 +   {
  82.718 +      png_debug1(1, "in %s retrieval function\n", "sBIT");
  82.719 +      *sig_bit = &(info_ptr->sig_bit);
  82.720 +      return (PNG_INFO_sBIT);
  82.721 +   }
  82.722 +   return (0);
  82.723 +}
  82.724 +#endif
  82.725 +
  82.726 +#if defined(PNG_TEXT_SUPPORTED)
  82.727 +png_uint_32 PNGAPI
  82.728 +png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
  82.729 +   int *num_text)
  82.730 +{
  82.731 +   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
  82.732 +   {
  82.733 +      png_debug1(1, "in %s retrieval function\n",
  82.734 +         (png_ptr->chunk_name[0] == '\0' ? "text"
  82.735 +             : (png_const_charp)png_ptr->chunk_name));
  82.736 +      if (text_ptr != NULL)
  82.737 +         *text_ptr = info_ptr->text;
  82.738 +      if (num_text != NULL)
  82.739 +         *num_text = info_ptr->num_text;
  82.740 +      return ((png_uint_32)info_ptr->num_text);
  82.741 +   }
  82.742 +   if (num_text != NULL)
  82.743 +     *num_text = 0;
  82.744 +   return(0);
  82.745 +}
  82.746 +#endif
  82.747 +
  82.748 +#if defined(PNG_tIME_SUPPORTED)
  82.749 +png_uint_32 PNGAPI
  82.750 +png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
  82.751 +{
  82.752 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
  82.753 +       && mod_time != NULL)
  82.754 +   {
  82.755 +      png_debug1(1, "in %s retrieval function\n", "tIME");
  82.756 +      *mod_time = &(info_ptr->mod_time);
  82.757 +      return (PNG_INFO_tIME);
  82.758 +   }
  82.759 +   return (0);
  82.760 +}
  82.761 +#endif
  82.762 +
  82.763 +#if defined(PNG_tRNS_SUPPORTED)
  82.764 +png_uint_32 PNGAPI
  82.765 +png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
  82.766 +   png_bytep *trans, int *num_trans, png_color_16p *trans_values)
  82.767 +{
  82.768 +   png_uint_32 retval = 0;
  82.769 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  82.770 +   {
  82.771 +      png_debug1(1, "in %s retrieval function\n", "tRNS");
  82.772 +      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  82.773 +      {
  82.774 +          if (trans != NULL)
  82.775 +          {
  82.776 +             *trans = info_ptr->trans;
  82.777 +             retval |= PNG_INFO_tRNS;
  82.778 +          }
  82.779 +          if (trans_values != NULL)
  82.780 +             *trans_values = &(info_ptr->trans_values);
  82.781 +      }
  82.782 +      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
  82.783 +      {
  82.784 +          if (trans_values != NULL)
  82.785 +          {
  82.786 +             *trans_values = &(info_ptr->trans_values);
  82.787 +             retval |= PNG_INFO_tRNS;
  82.788 +          }
  82.789 +          if (trans != NULL)
  82.790 +             *trans = NULL;
  82.791 +      }
  82.792 +      if (num_trans != NULL)
  82.793 +      {
  82.794 +         *num_trans = info_ptr->num_trans;
  82.795 +         retval |= PNG_INFO_tRNS;
  82.796 +      }
  82.797 +   }
  82.798 +   return (retval);
  82.799 +}
  82.800 +#endif
  82.801 +
  82.802 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  82.803 +png_uint_32 PNGAPI
  82.804 +png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
  82.805 +             png_unknown_chunkpp unknowns)
  82.806 +{
  82.807 +   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
  82.808 +   {
  82.809 +     *unknowns = info_ptr->unknown_chunks;
  82.810 +     return ((png_uint_32)info_ptr->unknown_chunks_num);
  82.811 +   }
  82.812 +   return (0);
  82.813 +}
  82.814 +#endif
  82.815 +
  82.816 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  82.817 +png_byte PNGAPI
  82.818 +png_get_rgb_to_gray_status (png_structp png_ptr)
  82.819 +{
  82.820 +   return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0);
  82.821 +}
  82.822 +#endif
  82.823 +
  82.824 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
  82.825 +png_voidp PNGAPI
  82.826 +png_get_user_chunk_ptr(png_structp png_ptr)
  82.827 +{
  82.828 +   return (png_ptr? png_ptr->user_chunk_ptr : NULL);
  82.829 +}
  82.830 +#endif
  82.831 +
  82.832 +#ifdef PNG_WRITE_SUPPORTED
  82.833 +png_uint_32 PNGAPI
  82.834 +png_get_compression_buffer_size(png_structp png_ptr)
  82.835 +{
  82.836 +   return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L);
  82.837 +}
  82.838 +#endif
  82.839 +
  82.840 +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  82.841 +#ifndef PNG_1_0_X
  82.842 +/* this function was added to libpng 1.2.0 and should exist by default */
  82.843 +png_uint_32 PNGAPI
  82.844 +png_get_asm_flags (png_structp png_ptr)
  82.845 +{
  82.846 +    /* obsolete, to be removed from libpng-1.4.0 */
  82.847 +    return (png_ptr? 0L: 0L);
  82.848 +}
  82.849 +
  82.850 +/* this function was added to libpng 1.2.0 and should exist by default */
  82.851 +png_uint_32 PNGAPI
  82.852 +png_get_asm_flagmask (int flag_select)
  82.853 +{
  82.854 +    /* obsolete, to be removed from libpng-1.4.0 */
  82.855 +    flag_select=flag_select;
  82.856 +    return 0L;
  82.857 +}
  82.858 +
  82.859 +    /* GRR:  could add this:   && defined(PNG_MMX_CODE_SUPPORTED) */
  82.860 +/* this function was added to libpng 1.2.0 */
  82.861 +png_uint_32 PNGAPI
  82.862 +png_get_mmx_flagmask (int flag_select, int *compilerID)
  82.863 +{
  82.864 +    /* obsolete, to be removed from libpng-1.4.0 */
  82.865 +    flag_select=flag_select;
  82.866 +    *compilerID = -1;   /* unknown (i.e., no asm/MMX code compiled) */
  82.867 +    return 0L;
  82.868 +}
  82.869 +
  82.870 +/* this function was added to libpng 1.2.0 */
  82.871 +png_byte PNGAPI
  82.872 +png_get_mmx_bitdepth_threshold (png_structp png_ptr)
  82.873 +{
  82.874 +    /* obsolete, to be removed from libpng-1.4.0 */
  82.875 +    return (png_ptr? 0: 0);
  82.876 +}
  82.877 +
  82.878 +/* this function was added to libpng 1.2.0 */
  82.879 +png_uint_32 PNGAPI
  82.880 +png_get_mmx_rowbytes_threshold (png_structp png_ptr)
  82.881 +{
  82.882 +    /* obsolete, to be removed from libpng-1.4.0 */
  82.883 +    return (png_ptr? 0L: 0L);
  82.884 +}
  82.885 +#endif /* ?PNG_1_0_X */
  82.886 +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
  82.887 +
  82.888 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  82.889 +/* these functions were added to libpng 1.2.6 */
  82.890 +png_uint_32 PNGAPI
  82.891 +png_get_user_width_max (png_structp png_ptr)
  82.892 +{
  82.893 +    return (png_ptr? png_ptr->user_width_max : 0);
  82.894 +}
  82.895 +png_uint_32 PNGAPI
  82.896 +png_get_user_height_max (png_structp png_ptr)
  82.897 +{
  82.898 +    return (png_ptr? png_ptr->user_height_max : 0);
  82.899 +}
  82.900 +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  82.901 + 
  82.902 +
  82.903 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    83.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    83.2 +++ b/libs/libpng/pngmem.c	Sun Jun 07 17:25:49 2015 +0300
    83.3 @@ -0,0 +1,609 @@
    83.4 +
    83.5 +/* pngmem.c - stub functions for memory allocation
    83.6 + *
    83.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    83.8 + * For conditions of distribution and use, see copyright notice in png.h
    83.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   83.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   83.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   83.12 + *
   83.13 + * This file provides a location for all memory allocation.  Users who
   83.14 + * need special memory handling are expected to supply replacement
   83.15 + * functions for png_malloc() and png_free(), and to use
   83.16 + * png_create_read_struct_2() and png_create_write_struct_2() to
   83.17 + * identify the replacement functions.
   83.18 + */
   83.19 +
   83.20 +#define PNG_INTERNAL
   83.21 +#include "png.h"
   83.22 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   83.23 +
   83.24 +/* Borland DOS special memory handler */
   83.25 +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
   83.26 +/* if you change this, be sure to change the one in png.h also */
   83.27 +
   83.28 +/* Allocate memory for a png_struct.  The malloc and memset can be replaced
   83.29 +   by a single call to calloc() if this is thought to improve performance. */
   83.30 +png_voidp /* PRIVATE */
   83.31 +png_create_struct(int type)
   83.32 +{
   83.33 +#ifdef PNG_USER_MEM_SUPPORTED
   83.34 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
   83.35 +}
   83.36 +
   83.37 +/* Alternate version of png_create_struct, for use with user-defined malloc. */
   83.38 +png_voidp /* PRIVATE */
   83.39 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
   83.40 +{
   83.41 +#endif /* PNG_USER_MEM_SUPPORTED */
   83.42 +   png_size_t size;
   83.43 +   png_voidp struct_ptr;
   83.44 +
   83.45 +   if (type == PNG_STRUCT_INFO)
   83.46 +     size = png_sizeof(png_info);
   83.47 +   else if (type == PNG_STRUCT_PNG)
   83.48 +     size = png_sizeof(png_struct);
   83.49 +   else
   83.50 +     return (png_get_copyright(NULL));
   83.51 +
   83.52 +#ifdef PNG_USER_MEM_SUPPORTED
   83.53 +   if (malloc_fn != NULL)
   83.54 +   {
   83.55 +      png_struct dummy_struct;
   83.56 +      png_structp png_ptr = &dummy_struct;
   83.57 +      png_ptr->mem_ptr=mem_ptr;
   83.58 +      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
   83.59 +   }
   83.60 +   else
   83.61 +#endif /* PNG_USER_MEM_SUPPORTED */
   83.62 +      struct_ptr = (png_voidp)farmalloc(size);
   83.63 +   if (struct_ptr != NULL)
   83.64 +      png_memset(struct_ptr, 0, size);
   83.65 +   return (struct_ptr);
   83.66 +}
   83.67 +
   83.68 +/* Free memory allocated by a png_create_struct() call */
   83.69 +void /* PRIVATE */
   83.70 +png_destroy_struct(png_voidp struct_ptr)
   83.71 +{
   83.72 +#ifdef PNG_USER_MEM_SUPPORTED
   83.73 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
   83.74 +}
   83.75 +
   83.76 +/* Free memory allocated by a png_create_struct() call */
   83.77 +void /* PRIVATE */
   83.78 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   83.79 +    png_voidp mem_ptr)
   83.80 +{
   83.81 +#endif
   83.82 +   if (struct_ptr != NULL)
   83.83 +   {
   83.84 +#ifdef PNG_USER_MEM_SUPPORTED
   83.85 +      if (free_fn != NULL)
   83.86 +      {
   83.87 +         png_struct dummy_struct;
   83.88 +         png_structp png_ptr = &dummy_struct;
   83.89 +         png_ptr->mem_ptr=mem_ptr;
   83.90 +         (*(free_fn))(png_ptr, struct_ptr);
   83.91 +         return;
   83.92 +      }
   83.93 +#endif /* PNG_USER_MEM_SUPPORTED */
   83.94 +      farfree (struct_ptr);
   83.95 +   }
   83.96 +}
   83.97 +
   83.98 +/* Allocate memory.  For reasonable files, size should never exceed
   83.99 + * 64K.  However, zlib may allocate more then 64K if you don't tell
  83.100 + * it not to.  See zconf.h and png.h for more information. zlib does
  83.101 + * need to allocate exactly 64K, so whatever you call here must
  83.102 + * have the ability to do that.
  83.103 + *
  83.104 + * Borland seems to have a problem in DOS mode for exactly 64K.
  83.105 + * It gives you a segment with an offset of 8 (perhaps to store its
  83.106 + * memory stuff).  zlib doesn't like this at all, so we have to
  83.107 + * detect and deal with it.  This code should not be needed in
  83.108 + * Windows or OS/2 modes, and only in 16 bit mode.  This code has
  83.109 + * been updated by Alexander Lehmann for version 0.89 to waste less
  83.110 + * memory.
  83.111 + *
  83.112 + * Note that we can't use png_size_t for the "size" declaration,
  83.113 + * since on some systems a png_size_t is a 16-bit quantity, and as a
  83.114 + * result, we would be truncating potentially larger memory requests
  83.115 + * (which should cause a fatal error) and introducing major problems.
  83.116 + */
  83.117 +
  83.118 +png_voidp PNGAPI
  83.119 +png_malloc(png_structp png_ptr, png_uint_32 size)
  83.120 +{
  83.121 +   png_voidp ret;
  83.122 +
  83.123 +   if (png_ptr == NULL || size == 0)
  83.124 +      return (NULL);
  83.125 +
  83.126 +#ifdef PNG_USER_MEM_SUPPORTED
  83.127 +   if (png_ptr->malloc_fn != NULL)
  83.128 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  83.129 +   else
  83.130 +       ret = (png_malloc_default(png_ptr, size));
  83.131 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.132 +       png_error(png_ptr, "Out of memory!");
  83.133 +   return (ret);
  83.134 +}
  83.135 +
  83.136 +png_voidp PNGAPI
  83.137 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
  83.138 +{
  83.139 +   png_voidp ret;
  83.140 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.141 +
  83.142 +   if (png_ptr == NULL || size == 0)
  83.143 +      return (NULL);
  83.144 +
  83.145 +#ifdef PNG_MAX_MALLOC_64K
  83.146 +   if (size > (png_uint_32)65536L)
  83.147 +   {
  83.148 +      png_warning(png_ptr, "Cannot Allocate > 64K");
  83.149 +      ret = NULL;
  83.150 +   }
  83.151 +   else
  83.152 +#endif
  83.153 +
  83.154 +   if (size != (size_t)size)
  83.155 +     ret = NULL;
  83.156 +   else if (size == (png_uint_32)65536L)
  83.157 +   {
  83.158 +      if (png_ptr->offset_table == NULL)
  83.159 +      {
  83.160 +         /* try to see if we need to do any of this fancy stuff */
  83.161 +         ret = farmalloc(size);
  83.162 +         if (ret == NULL || ((png_size_t)ret & 0xffff))
  83.163 +         {
  83.164 +            int num_blocks;
  83.165 +            png_uint_32 total_size;
  83.166 +            png_bytep table;
  83.167 +            int i;
  83.168 +            png_byte huge * hptr;
  83.169 +
  83.170 +            if (ret != NULL)
  83.171 +            {
  83.172 +               farfree(ret);
  83.173 +               ret = NULL;
  83.174 +            }
  83.175 +
  83.176 +            if (png_ptr->zlib_window_bits > 14)
  83.177 +               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  83.178 +            else
  83.179 +               num_blocks = 1;
  83.180 +            if (png_ptr->zlib_mem_level >= 7)
  83.181 +               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  83.182 +            else
  83.183 +               num_blocks++;
  83.184 +
  83.185 +            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  83.186 +
  83.187 +            table = farmalloc(total_size);
  83.188 +
  83.189 +            if (table == NULL)
  83.190 +            {
  83.191 +#ifndef PNG_USER_MEM_SUPPORTED
  83.192 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.193 +                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
  83.194 +               else
  83.195 +                  png_warning(png_ptr, "Out Of Memory.");
  83.196 +#endif
  83.197 +               return (NULL);
  83.198 +            }
  83.199 +
  83.200 +            if ((png_size_t)table & 0xfff0)
  83.201 +            {
  83.202 +#ifndef PNG_USER_MEM_SUPPORTED
  83.203 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.204 +                  png_error(png_ptr,
  83.205 +                    "Farmalloc didn't return normalized pointer");
  83.206 +               else
  83.207 +                  png_warning(png_ptr,
  83.208 +                    "Farmalloc didn't return normalized pointer");
  83.209 +#endif
  83.210 +               return (NULL);
  83.211 +            }
  83.212 +
  83.213 +            png_ptr->offset_table = table;
  83.214 +            png_ptr->offset_table_ptr = farmalloc(num_blocks *
  83.215 +               png_sizeof(png_bytep));
  83.216 +
  83.217 +            if (png_ptr->offset_table_ptr == NULL)
  83.218 +            {
  83.219 +#ifndef PNG_USER_MEM_SUPPORTED
  83.220 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.221 +                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
  83.222 +               else
  83.223 +                  png_warning(png_ptr, "Out Of memory.");
  83.224 +#endif
  83.225 +               return (NULL);
  83.226 +            }
  83.227 +
  83.228 +            hptr = (png_byte huge *)table;
  83.229 +            if ((png_size_t)hptr & 0xf)
  83.230 +            {
  83.231 +               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  83.232 +               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
  83.233 +            }
  83.234 +            for (i = 0; i < num_blocks; i++)
  83.235 +            {
  83.236 +               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  83.237 +               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
  83.238 +            }
  83.239 +
  83.240 +            png_ptr->offset_table_number = num_blocks;
  83.241 +            png_ptr->offset_table_count = 0;
  83.242 +            png_ptr->offset_table_count_free = 0;
  83.243 +         }
  83.244 +      }
  83.245 +
  83.246 +      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  83.247 +      {
  83.248 +#ifndef PNG_USER_MEM_SUPPORTED
  83.249 +         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.250 +            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
  83.251 +         else
  83.252 +            png_warning(png_ptr, "Out of Memory.");
  83.253 +#endif
  83.254 +         return (NULL);
  83.255 +      }
  83.256 +
  83.257 +      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  83.258 +   }
  83.259 +   else
  83.260 +      ret = farmalloc(size);
  83.261 +
  83.262 +#ifndef PNG_USER_MEM_SUPPORTED
  83.263 +   if (ret == NULL)
  83.264 +   {
  83.265 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.266 +         png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
  83.267 +      else
  83.268 +         png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
  83.269 +   }
  83.270 +#endif
  83.271 +
  83.272 +   return (ret);
  83.273 +}
  83.274 +
  83.275 +/* free a pointer allocated by png_malloc().  In the default
  83.276 +   configuration, png_ptr is not used, but is passed in case it
  83.277 +   is needed.  If ptr is NULL, return without taking any action. */
  83.278 +
  83.279 +void PNGAPI
  83.280 +png_free(png_structp png_ptr, png_voidp ptr)
  83.281 +{
  83.282 +   if (png_ptr == NULL || ptr == NULL)
  83.283 +      return;
  83.284 +
  83.285 +#ifdef PNG_USER_MEM_SUPPORTED
  83.286 +   if (png_ptr->free_fn != NULL)
  83.287 +   {
  83.288 +      (*(png_ptr->free_fn))(png_ptr, ptr);
  83.289 +      return;
  83.290 +   }
  83.291 +   else png_free_default(png_ptr, ptr);
  83.292 +}
  83.293 +
  83.294 +void PNGAPI
  83.295 +png_free_default(png_structp png_ptr, png_voidp ptr)
  83.296 +{
  83.297 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.298 +
  83.299 +   if (png_ptr == NULL || ptr == NULL) return;
  83.300 +
  83.301 +   if (png_ptr->offset_table != NULL)
  83.302 +   {
  83.303 +      int i;
  83.304 +
  83.305 +      for (i = 0; i < png_ptr->offset_table_count; i++)
  83.306 +      {
  83.307 +         if (ptr == png_ptr->offset_table_ptr[i])
  83.308 +         {
  83.309 +            ptr = NULL;
  83.310 +            png_ptr->offset_table_count_free++;
  83.311 +            break;
  83.312 +         }
  83.313 +      }
  83.314 +      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  83.315 +      {
  83.316 +         farfree(png_ptr->offset_table);
  83.317 +         farfree(png_ptr->offset_table_ptr);
  83.318 +         png_ptr->offset_table = NULL;
  83.319 +         png_ptr->offset_table_ptr = NULL;
  83.320 +      }
  83.321 +   }
  83.322 +
  83.323 +   if (ptr != NULL)
  83.324 +   {
  83.325 +      farfree(ptr);
  83.326 +   }
  83.327 +}
  83.328 +
  83.329 +#else /* Not the Borland DOS special memory handler */
  83.330 +
  83.331 +/* Allocate memory for a png_struct or a png_info.  The malloc and
  83.332 +   memset can be replaced by a single call to calloc() if this is thought
  83.333 +   to improve performance noticably. */
  83.334 +png_voidp /* PRIVATE */
  83.335 +png_create_struct(int type)
  83.336 +{
  83.337 +#ifdef PNG_USER_MEM_SUPPORTED
  83.338 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  83.339 +}
  83.340 +
  83.341 +/* Allocate memory for a png_struct or a png_info.  The malloc and
  83.342 +   memset can be replaced by a single call to calloc() if this is thought
  83.343 +   to improve performance noticably. */
  83.344 +png_voidp /* PRIVATE */
  83.345 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  83.346 +{
  83.347 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.348 +   png_size_t size;
  83.349 +   png_voidp struct_ptr;
  83.350 +
  83.351 +   if (type == PNG_STRUCT_INFO)
  83.352 +      size = png_sizeof(png_info);
  83.353 +   else if (type == PNG_STRUCT_PNG)
  83.354 +      size = png_sizeof(png_struct);
  83.355 +   else
  83.356 +      return (NULL);
  83.357 +
  83.358 +#ifdef PNG_USER_MEM_SUPPORTED
  83.359 +   if (malloc_fn != NULL)
  83.360 +   {
  83.361 +      png_struct dummy_struct;
  83.362 +      png_structp png_ptr = &dummy_struct;
  83.363 +      png_ptr->mem_ptr=mem_ptr;
  83.364 +      struct_ptr = (*(malloc_fn))(png_ptr, size);
  83.365 +      if (struct_ptr != NULL)
  83.366 +         png_memset(struct_ptr, 0, size);
  83.367 +      return (struct_ptr);
  83.368 +   }
  83.369 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.370 +
  83.371 +#if defined(__TURBOC__) && !defined(__FLAT__)
  83.372 +   struct_ptr = (png_voidp)farmalloc(size);
  83.373 +#else
  83.374 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  83.375 +   struct_ptr = (png_voidp)halloc(size, 1);
  83.376 +# else
  83.377 +   struct_ptr = (png_voidp)malloc(size);
  83.378 +# endif
  83.379 +#endif
  83.380 +   if (struct_ptr != NULL)
  83.381 +      png_memset(struct_ptr, 0, size);
  83.382 +
  83.383 +   return (struct_ptr);
  83.384 +}
  83.385 +
  83.386 +
  83.387 +/* Free memory allocated by a png_create_struct() call */
  83.388 +void /* PRIVATE */
  83.389 +png_destroy_struct(png_voidp struct_ptr)
  83.390 +{
  83.391 +#ifdef PNG_USER_MEM_SUPPORTED
  83.392 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  83.393 +}
  83.394 +
  83.395 +/* Free memory allocated by a png_create_struct() call */
  83.396 +void /* PRIVATE */
  83.397 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  83.398 +    png_voidp mem_ptr)
  83.399 +{
  83.400 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.401 +   if (struct_ptr != NULL)
  83.402 +   {
  83.403 +#ifdef PNG_USER_MEM_SUPPORTED
  83.404 +      if (free_fn != NULL)
  83.405 +      {
  83.406 +         png_struct dummy_struct;
  83.407 +         png_structp png_ptr = &dummy_struct;
  83.408 +         png_ptr->mem_ptr=mem_ptr;
  83.409 +         (*(free_fn))(png_ptr, struct_ptr);
  83.410 +         return;
  83.411 +      }
  83.412 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.413 +#if defined(__TURBOC__) && !defined(__FLAT__)
  83.414 +      farfree(struct_ptr);
  83.415 +#else
  83.416 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  83.417 +      hfree(struct_ptr);
  83.418 +# else
  83.419 +      free(struct_ptr);
  83.420 +# endif
  83.421 +#endif
  83.422 +   }
  83.423 +}
  83.424 +
  83.425 +/* Allocate memory.  For reasonable files, size should never exceed
  83.426 +   64K.  However, zlib may allocate more then 64K if you don't tell
  83.427 +   it not to.  See zconf.h and png.h for more information.  zlib does
  83.428 +   need to allocate exactly 64K, so whatever you call here must
  83.429 +   have the ability to do that. */
  83.430 +
  83.431 +png_voidp PNGAPI
  83.432 +png_malloc(png_structp png_ptr, png_uint_32 size)
  83.433 +{
  83.434 +   png_voidp ret;
  83.435 +
  83.436 +#ifdef PNG_USER_MEM_SUPPORTED
  83.437 +   if (png_ptr == NULL || size == 0)
  83.438 +      return (NULL);
  83.439 +
  83.440 +   if (png_ptr->malloc_fn != NULL)
  83.441 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  83.442 +   else
  83.443 +       ret = (png_malloc_default(png_ptr, size));
  83.444 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.445 +       png_error(png_ptr, "Out of Memory!");
  83.446 +   return (ret);
  83.447 +}
  83.448 +
  83.449 +png_voidp PNGAPI
  83.450 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
  83.451 +{
  83.452 +   png_voidp ret;
  83.453 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.454 +
  83.455 +   if (png_ptr == NULL || size == 0)
  83.456 +      return (NULL);
  83.457 +
  83.458 +#ifdef PNG_MAX_MALLOC_64K
  83.459 +   if (size > (png_uint_32)65536L)
  83.460 +   {
  83.461 +#ifndef PNG_USER_MEM_SUPPORTED
  83.462 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.463 +         png_error(png_ptr, "Cannot Allocate > 64K");
  83.464 +      else
  83.465 +#endif
  83.466 +         return NULL;
  83.467 +   }
  83.468 +#endif
  83.469 +
  83.470 + /* Check for overflow */
  83.471 +#if defined(__TURBOC__) && !defined(__FLAT__)
  83.472 + if (size != (unsigned long)size)
  83.473 +   ret = NULL;
  83.474 + else
  83.475 +   ret = farmalloc(size);
  83.476 +#else
  83.477 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  83.478 + if (size != (unsigned long)size)
  83.479 +   ret = NULL;
  83.480 + else
  83.481 +   ret = halloc(size, 1);
  83.482 +# else
  83.483 + if (size != (size_t)size)
  83.484 +   ret = NULL;
  83.485 + else
  83.486 +   ret = malloc((size_t)size);
  83.487 +# endif
  83.488 +#endif
  83.489 +
  83.490 +#ifndef PNG_USER_MEM_SUPPORTED
  83.491 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  83.492 +      png_error(png_ptr, "Out of Memory");
  83.493 +#endif
  83.494 +
  83.495 +   return (ret);
  83.496 +}
  83.497 +
  83.498 +/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
  83.499 +   without taking any action. */
  83.500 +void PNGAPI
  83.501 +png_free(png_structp png_ptr, png_voidp ptr)
  83.502 +{
  83.503 +   if (png_ptr == NULL || ptr == NULL)
  83.504 +      return;
  83.505 +
  83.506 +#ifdef PNG_USER_MEM_SUPPORTED
  83.507 +   if (png_ptr->free_fn != NULL)
  83.508 +   {
  83.509 +      (*(png_ptr->free_fn))(png_ptr, ptr);
  83.510 +      return;
  83.511 +   }
  83.512 +   else png_free_default(png_ptr, ptr);
  83.513 +}
  83.514 +void PNGAPI
  83.515 +png_free_default(png_structp png_ptr, png_voidp ptr)
  83.516 +{
  83.517 +   if (png_ptr == NULL || ptr == NULL)
  83.518 +      return;
  83.519 +
  83.520 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.521 +
  83.522 +#if defined(__TURBOC__) && !defined(__FLAT__)
  83.523 +   farfree(ptr);
  83.524 +#else
  83.525 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  83.526 +   hfree(ptr);
  83.527 +# else
  83.528 +   free(ptr);
  83.529 +# endif
  83.530 +#endif
  83.531 +}
  83.532 +
  83.533 +#endif /* Not Borland DOS special memory handler */
  83.534 +
  83.535 +#if defined(PNG_1_0_X)
  83.536 +#  define png_malloc_warn png_malloc
  83.537 +#else
  83.538 +/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
  83.539 + * function will set up png_malloc() to issue a png_warning and return NULL
  83.540 + * instead of issuing a png_error, if it fails to allocate the requested
  83.541 + * memory.
  83.542 + */
  83.543 +png_voidp PNGAPI
  83.544 +png_malloc_warn(png_structp png_ptr, png_uint_32 size)
  83.545 +{
  83.546 +   png_voidp ptr;
  83.547 +   png_uint_32 save_flags;
  83.548 +   if (png_ptr == NULL) return (NULL);
  83.549 +
  83.550 +   save_flags = png_ptr->flags;
  83.551 +   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  83.552 +   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
  83.553 +   png_ptr->flags=save_flags;
  83.554 +   return(ptr);
  83.555 +}
  83.556 +#endif
  83.557 +
  83.558 +png_voidp PNGAPI
  83.559 +png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
  83.560 +   png_uint_32 length)
  83.561 +{
  83.562 +   png_size_t size;
  83.563 +
  83.564 +   size = (png_size_t)length;
  83.565 +   if ((png_uint_32)size != length)
  83.566 +      png_error(png_ptr, "Overflow in png_memcpy_check.");
  83.567 +
  83.568 +   return(png_memcpy (s1, s2, size));
  83.569 +}
  83.570 +
  83.571 +png_voidp PNGAPI
  83.572 +png_memset_check (png_structp png_ptr, png_voidp s1, int value,
  83.573 +   png_uint_32 length)
  83.574 +{
  83.575 +   png_size_t size;
  83.576 +
  83.577 +   size = (png_size_t)length;
  83.578 +   if ((png_uint_32)size != length)
  83.579 +      png_error(png_ptr, "Overflow in png_memset_check.");
  83.580 +
  83.581 +   return (png_memset (s1, value, size));
  83.582 +
  83.583 +}
  83.584 +
  83.585 +#ifdef PNG_USER_MEM_SUPPORTED
  83.586 +/* This function is called when the application wants to use another method
  83.587 + * of allocating and freeing memory.
  83.588 + */
  83.589 +void PNGAPI
  83.590 +png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  83.591 +  malloc_fn, png_free_ptr free_fn)
  83.592 +{
  83.593 +   if (png_ptr != NULL)
  83.594 +   {
  83.595 +      png_ptr->mem_ptr = mem_ptr;
  83.596 +      png_ptr->malloc_fn = malloc_fn;
  83.597 +      png_ptr->free_fn = free_fn;
  83.598 +   }
  83.599 +}
  83.600 +
  83.601 +/* This function returns a pointer to the mem_ptr associated with the user
  83.602 + * functions.  The application should free any memory associated with this
  83.603 + * pointer before png_write_destroy and png_read_destroy are called.
  83.604 + */
  83.605 +png_voidp PNGAPI
  83.606 +png_get_mem_ptr(png_structp png_ptr)
  83.607 +{
  83.608 +   if (png_ptr == NULL) return (NULL);
  83.609 +   return ((png_voidp)png_ptr->mem_ptr);
  83.610 +}
  83.611 +#endif /* PNG_USER_MEM_SUPPORTED */
  83.612 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    84.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    84.2 +++ b/libs/libpng/pngpread.c	Sun Jun 07 17:25:49 2015 +0300
    84.3 @@ -0,0 +1,1594 @@
    84.4 +
    84.5 +/* pngpread.c - read a png file in push mode
    84.6 + *
    84.7 + * Last changed in libpng 1.2.32 [September 18, 2008]
    84.8 + * For conditions of distribution and use, see copyright notice in png.h
    84.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   84.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   84.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   84.12 + */
   84.13 +
   84.14 +#define PNG_INTERNAL
   84.15 +#include "png.h"
   84.16 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   84.17 +
   84.18 +/* push model modes */
   84.19 +#define PNG_READ_SIG_MODE   0
   84.20 +#define PNG_READ_CHUNK_MODE 1
   84.21 +#define PNG_READ_IDAT_MODE  2
   84.22 +#define PNG_SKIP_MODE       3
   84.23 +#define PNG_READ_tEXt_MODE  4
   84.24 +#define PNG_READ_zTXt_MODE  5
   84.25 +#define PNG_READ_DONE_MODE  6
   84.26 +#define PNG_READ_iTXt_MODE  7
   84.27 +#define PNG_ERROR_MODE      8
   84.28 +
   84.29 +void PNGAPI
   84.30 +png_process_data(png_structp png_ptr, png_infop info_ptr,
   84.31 +   png_bytep buffer, png_size_t buffer_size)
   84.32 +{
   84.33 +   if (png_ptr == NULL || info_ptr == NULL) return;
   84.34 +   png_push_restore_buffer(png_ptr, buffer, buffer_size);
   84.35 +
   84.36 +   while (png_ptr->buffer_size)
   84.37 +   {
   84.38 +      png_process_some_data(png_ptr, info_ptr);
   84.39 +   }
   84.40 +}
   84.41 +
   84.42 +/* What we do with the incoming data depends on what we were previously
   84.43 + * doing before we ran out of data...
   84.44 + */
   84.45 +void /* PRIVATE */
   84.46 +png_process_some_data(png_structp png_ptr, png_infop info_ptr)
   84.47 +{
   84.48 +   if (png_ptr == NULL) return;
   84.49 +   switch (png_ptr->process_mode)
   84.50 +   {
   84.51 +      case PNG_READ_SIG_MODE:
   84.52 +      {
   84.53 +         png_push_read_sig(png_ptr, info_ptr);
   84.54 +         break;
   84.55 +      }
   84.56 +      case PNG_READ_CHUNK_MODE:
   84.57 +      {
   84.58 +         png_push_read_chunk(png_ptr, info_ptr);
   84.59 +         break;
   84.60 +      }
   84.61 +      case PNG_READ_IDAT_MODE:
   84.62 +      {
   84.63 +         png_push_read_IDAT(png_ptr);
   84.64 +         break;
   84.65 +      }
   84.66 +#if defined(PNG_READ_tEXt_SUPPORTED)
   84.67 +      case PNG_READ_tEXt_MODE:
   84.68 +      {
   84.69 +         png_push_read_tEXt(png_ptr, info_ptr);
   84.70 +         break;
   84.71 +      }
   84.72 +#endif
   84.73 +#if defined(PNG_READ_zTXt_SUPPORTED)
   84.74 +      case PNG_READ_zTXt_MODE:
   84.75 +      {
   84.76 +         png_push_read_zTXt(png_ptr, info_ptr);
   84.77 +         break;
   84.78 +      }
   84.79 +#endif
   84.80 +#if defined(PNG_READ_iTXt_SUPPORTED)
   84.81 +      case PNG_READ_iTXt_MODE:
   84.82 +      {
   84.83 +         png_push_read_iTXt(png_ptr, info_ptr);
   84.84 +         break;
   84.85 +      }
   84.86 +#endif
   84.87 +      case PNG_SKIP_MODE:
   84.88 +      {
   84.89 +         png_push_crc_finish(png_ptr);
   84.90 +         break;
   84.91 +      }
   84.92 +      default:
   84.93 +      {
   84.94 +         png_ptr->buffer_size = 0;
   84.95 +         break;
   84.96 +      }
   84.97 +   }
   84.98 +}
   84.99 +
  84.100 +/* Read any remaining signature bytes from the stream and compare them with
  84.101 + * the correct PNG signature.  It is possible that this routine is called
  84.102 + * with bytes already read from the signature, either because they have been
  84.103 + * checked by the calling application, or because of multiple calls to this
  84.104 + * routine.
  84.105 + */
  84.106 +void /* PRIVATE */
  84.107 +png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
  84.108 +{
  84.109 +   png_size_t num_checked = png_ptr->sig_bytes,
  84.110 +             num_to_check = 8 - num_checked;
  84.111 +
  84.112 +   if (png_ptr->buffer_size < num_to_check)
  84.113 +   {
  84.114 +      num_to_check = png_ptr->buffer_size;
  84.115 +   }
  84.116 +
  84.117 +   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
  84.118 +      num_to_check);
  84.119 +   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
  84.120 +
  84.121 +   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  84.122 +   {
  84.123 +      if (num_checked < 4 &&
  84.124 +          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  84.125 +         png_error(png_ptr, "Not a PNG file");
  84.126 +      else
  84.127 +         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  84.128 +   }
  84.129 +   else
  84.130 +   {
  84.131 +      if (png_ptr->sig_bytes >= 8)
  84.132 +      {
  84.133 +         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  84.134 +      }
  84.135 +   }
  84.136 +}
  84.137 +
  84.138 +void /* PRIVATE */
  84.139 +png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
  84.140 +{
  84.141 +#ifdef PNG_USE_LOCAL_ARRAYS
  84.142 +      PNG_CONST PNG_IHDR;
  84.143 +      PNG_CONST PNG_IDAT;
  84.144 +      PNG_CONST PNG_IEND;
  84.145 +      PNG_CONST PNG_PLTE;
  84.146 +#if defined(PNG_READ_bKGD_SUPPORTED)
  84.147 +      PNG_CONST PNG_bKGD;
  84.148 +#endif
  84.149 +#if defined(PNG_READ_cHRM_SUPPORTED)
  84.150 +      PNG_CONST PNG_cHRM;
  84.151 +#endif
  84.152 +#if defined(PNG_READ_gAMA_SUPPORTED)
  84.153 +      PNG_CONST PNG_gAMA;
  84.154 +#endif
  84.155 +#if defined(PNG_READ_hIST_SUPPORTED)
  84.156 +      PNG_CONST PNG_hIST;
  84.157 +#endif
  84.158 +#if defined(PNG_READ_iCCP_SUPPORTED)
  84.159 +      PNG_CONST PNG_iCCP;
  84.160 +#endif
  84.161 +#if defined(PNG_READ_iTXt_SUPPORTED)
  84.162 +      PNG_CONST PNG_iTXt;
  84.163 +#endif
  84.164 +#if defined(PNG_READ_oFFs_SUPPORTED)
  84.165 +      PNG_CONST PNG_oFFs;
  84.166 +#endif
  84.167 +#if defined(PNG_READ_pCAL_SUPPORTED)
  84.168 +      PNG_CONST PNG_pCAL;
  84.169 +#endif
  84.170 +#if defined(PNG_READ_pHYs_SUPPORTED)
  84.171 +      PNG_CONST PNG_pHYs;
  84.172 +#endif
  84.173 +#if defined(PNG_READ_sBIT_SUPPORTED)
  84.174 +      PNG_CONST PNG_sBIT;
  84.175 +#endif
  84.176 +#if defined(PNG_READ_sCAL_SUPPORTED)
  84.177 +      PNG_CONST PNG_sCAL;
  84.178 +#endif
  84.179 +#if defined(PNG_READ_sRGB_SUPPORTED)
  84.180 +      PNG_CONST PNG_sRGB;
  84.181 +#endif
  84.182 +#if defined(PNG_READ_sPLT_SUPPORTED)
  84.183 +      PNG_CONST PNG_sPLT;
  84.184 +#endif
  84.185 +#if defined(PNG_READ_tEXt_SUPPORTED)
  84.186 +      PNG_CONST PNG_tEXt;
  84.187 +#endif
  84.188 +#if defined(PNG_READ_tIME_SUPPORTED)
  84.189 +      PNG_CONST PNG_tIME;
  84.190 +#endif
  84.191 +#if defined(PNG_READ_tRNS_SUPPORTED)
  84.192 +      PNG_CONST PNG_tRNS;
  84.193 +#endif
  84.194 +#if defined(PNG_READ_zTXt_SUPPORTED)
  84.195 +      PNG_CONST PNG_zTXt;
  84.196 +#endif
  84.197 +#endif /* PNG_USE_LOCAL_ARRAYS */
  84.198 +   /* First we make sure we have enough data for the 4 byte chunk name
  84.199 +    * and the 4 byte chunk length before proceeding with decoding the
  84.200 +    * chunk data.  To fully decode each of these chunks, we also make
  84.201 +    * sure we have enough data in the buffer for the 4 byte CRC at the
  84.202 +    * end of every chunk (except IDAT, which is handled separately).
  84.203 +    */
  84.204 +   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  84.205 +   {
  84.206 +      png_byte chunk_length[4];
  84.207 +
  84.208 +      if (png_ptr->buffer_size < 8)
  84.209 +      {
  84.210 +         png_push_save_buffer(png_ptr);
  84.211 +         return;
  84.212 +      }
  84.213 +
  84.214 +      png_push_fill_buffer(png_ptr, chunk_length, 4);
  84.215 +      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  84.216 +      png_reset_crc(png_ptr);
  84.217 +      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  84.218 +      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  84.219 +      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  84.220 +   }
  84.221 +
  84.222 +   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  84.223 +     if (png_ptr->mode & PNG_AFTER_IDAT)
  84.224 +        png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  84.225 +
  84.226 +   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
  84.227 +   {
  84.228 +      if (png_ptr->push_length != 13)
  84.229 +         png_error(png_ptr, "Invalid IHDR length");
  84.230 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.231 +      {
  84.232 +         png_push_save_buffer(png_ptr);
  84.233 +         return;
  84.234 +      }
  84.235 +      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
  84.236 +   }
  84.237 +   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
  84.238 +   {
  84.239 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.240 +      {
  84.241 +         png_push_save_buffer(png_ptr);
  84.242 +         return;
  84.243 +      }
  84.244 +      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
  84.245 +
  84.246 +      png_ptr->process_mode = PNG_READ_DONE_MODE;
  84.247 +      png_push_have_end(png_ptr, info_ptr);
  84.248 +   }
  84.249 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  84.250 +   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
  84.251 +   {
  84.252 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.253 +      {
  84.254 +         png_push_save_buffer(png_ptr);
  84.255 +         return;
  84.256 +      }
  84.257 +      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  84.258 +         png_ptr->mode |= PNG_HAVE_IDAT;
  84.259 +      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  84.260 +      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  84.261 +         png_ptr->mode |= PNG_HAVE_PLTE;
  84.262 +      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  84.263 +      {
  84.264 +         if (!(png_ptr->mode & PNG_HAVE_IHDR))
  84.265 +            png_error(png_ptr, "Missing IHDR before IDAT");
  84.266 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  84.267 +                  !(png_ptr->mode & PNG_HAVE_PLTE))
  84.268 +            png_error(png_ptr, "Missing PLTE before IDAT");
  84.269 +      }
  84.270 +   }
  84.271 +#endif
  84.272 +   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  84.273 +   {
  84.274 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.275 +      {
  84.276 +         png_push_save_buffer(png_ptr);
  84.277 +         return;
  84.278 +      }
  84.279 +      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
  84.280 +   }
  84.281 +   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  84.282 +   {
  84.283 +      /* If we reach an IDAT chunk, this means we have read all of the
  84.284 +       * header chunks, and we can start reading the image (or if this
  84.285 +       * is called after the image has been read - we have an error).
  84.286 +       */
  84.287 +     if (!(png_ptr->mode & PNG_HAVE_IHDR))
  84.288 +       png_error(png_ptr, "Missing IHDR before IDAT");
  84.289 +     else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  84.290 +         !(png_ptr->mode & PNG_HAVE_PLTE))
  84.291 +       png_error(png_ptr, "Missing PLTE before IDAT");
  84.292 +
  84.293 +      if (png_ptr->mode & PNG_HAVE_IDAT)
  84.294 +      {
  84.295 +         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  84.296 +           if (png_ptr->push_length == 0)
  84.297 +              return;
  84.298 +
  84.299 +         if (png_ptr->mode & PNG_AFTER_IDAT)
  84.300 +            png_error(png_ptr, "Too many IDAT's found");
  84.301 +      }
  84.302 +
  84.303 +      png_ptr->idat_size = png_ptr->push_length;
  84.304 +      png_ptr->mode |= PNG_HAVE_IDAT;
  84.305 +      png_ptr->process_mode = PNG_READ_IDAT_MODE;
  84.306 +      png_push_have_info(png_ptr, info_ptr);
  84.307 +      png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  84.308 +      png_ptr->zstream.next_out = png_ptr->row_buf;
  84.309 +      return;
  84.310 +   }
  84.311 +#if defined(PNG_READ_gAMA_SUPPORTED)
  84.312 +   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
  84.313 +   {
  84.314 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.315 +      {
  84.316 +         png_push_save_buffer(png_ptr);
  84.317 +         return;
  84.318 +      }
  84.319 +      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
  84.320 +   }
  84.321 +#endif
  84.322 +#if defined(PNG_READ_sBIT_SUPPORTED)
  84.323 +   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
  84.324 +   {
  84.325 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.326 +      {
  84.327 +         png_push_save_buffer(png_ptr);
  84.328 +         return;
  84.329 +      }
  84.330 +      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
  84.331 +   }
  84.332 +#endif
  84.333 +#if defined(PNG_READ_cHRM_SUPPORTED)
  84.334 +   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
  84.335 +   {
  84.336 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.337 +      {
  84.338 +         png_push_save_buffer(png_ptr);
  84.339 +         return;
  84.340 +      }
  84.341 +      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
  84.342 +   }
  84.343 +#endif
  84.344 +#if defined(PNG_READ_sRGB_SUPPORTED)
  84.345 +   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
  84.346 +   {
  84.347 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.348 +      {
  84.349 +         png_push_save_buffer(png_ptr);
  84.350 +         return;
  84.351 +      }
  84.352 +      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
  84.353 +   }
  84.354 +#endif
  84.355 +#if defined(PNG_READ_iCCP_SUPPORTED)
  84.356 +   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
  84.357 +   {
  84.358 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.359 +      {
  84.360 +         png_push_save_buffer(png_ptr);
  84.361 +         return;
  84.362 +      }
  84.363 +      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
  84.364 +   }
  84.365 +#endif
  84.366 +#if defined(PNG_READ_sPLT_SUPPORTED)
  84.367 +   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
  84.368 +   {
  84.369 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.370 +      {
  84.371 +         png_push_save_buffer(png_ptr);
  84.372 +         return;
  84.373 +      }
  84.374 +      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
  84.375 +   }
  84.376 +#endif
  84.377 +#if defined(PNG_READ_tRNS_SUPPORTED)
  84.378 +   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
  84.379 +   {
  84.380 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.381 +      {
  84.382 +         png_push_save_buffer(png_ptr);
  84.383 +         return;
  84.384 +      }
  84.385 +      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
  84.386 +   }
  84.387 +#endif
  84.388 +#if defined(PNG_READ_bKGD_SUPPORTED)
  84.389 +   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
  84.390 +   {
  84.391 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.392 +      {
  84.393 +         png_push_save_buffer(png_ptr);
  84.394 +         return;
  84.395 +      }
  84.396 +      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
  84.397 +   }
  84.398 +#endif
  84.399 +#if defined(PNG_READ_hIST_SUPPORTED)
  84.400 +   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
  84.401 +   {
  84.402 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.403 +      {
  84.404 +         png_push_save_buffer(png_ptr);
  84.405 +         return;
  84.406 +      }
  84.407 +      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
  84.408 +   }
  84.409 +#endif
  84.410 +#if defined(PNG_READ_pHYs_SUPPORTED)
  84.411 +   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
  84.412 +   {
  84.413 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.414 +      {
  84.415 +         png_push_save_buffer(png_ptr);
  84.416 +         return;
  84.417 +      }
  84.418 +      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
  84.419 +   }
  84.420 +#endif
  84.421 +#if defined(PNG_READ_oFFs_SUPPORTED)
  84.422 +   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
  84.423 +   {
  84.424 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.425 +      {
  84.426 +         png_push_save_buffer(png_ptr);
  84.427 +         return;
  84.428 +      }
  84.429 +      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
  84.430 +   }
  84.431 +#endif
  84.432 +#if defined(PNG_READ_pCAL_SUPPORTED)
  84.433 +   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
  84.434 +   {
  84.435 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.436 +      {
  84.437 +         png_push_save_buffer(png_ptr);
  84.438 +         return;
  84.439 +      }
  84.440 +      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
  84.441 +   }
  84.442 +#endif
  84.443 +#if defined(PNG_READ_sCAL_SUPPORTED)
  84.444 +   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
  84.445 +   {
  84.446 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.447 +      {
  84.448 +         png_push_save_buffer(png_ptr);
  84.449 +         return;
  84.450 +      }
  84.451 +      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
  84.452 +   }
  84.453 +#endif
  84.454 +#if defined(PNG_READ_tIME_SUPPORTED)
  84.455 +   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
  84.456 +   {
  84.457 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.458 +      {
  84.459 +         png_push_save_buffer(png_ptr);
  84.460 +         return;
  84.461 +      }
  84.462 +      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
  84.463 +   }
  84.464 +#endif
  84.465 +#if defined(PNG_READ_tEXt_SUPPORTED)
  84.466 +   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
  84.467 +   {
  84.468 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.469 +      {
  84.470 +         png_push_save_buffer(png_ptr);
  84.471 +         return;
  84.472 +      }
  84.473 +      png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
  84.474 +   }
  84.475 +#endif
  84.476 +#if defined(PNG_READ_zTXt_SUPPORTED)
  84.477 +   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
  84.478 +   {
  84.479 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.480 +      {
  84.481 +         png_push_save_buffer(png_ptr);
  84.482 +         return;
  84.483 +      }
  84.484 +      png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
  84.485 +   }
  84.486 +#endif
  84.487 +#if defined(PNG_READ_iTXt_SUPPORTED)
  84.488 +   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
  84.489 +   {
  84.490 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.491 +      {
  84.492 +         png_push_save_buffer(png_ptr);
  84.493 +         return;
  84.494 +      }
  84.495 +      png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
  84.496 +   }
  84.497 +#endif
  84.498 +   else
  84.499 +   {
  84.500 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  84.501 +      {
  84.502 +         png_push_save_buffer(png_ptr);
  84.503 +         return;
  84.504 +      }
  84.505 +      png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  84.506 +   }
  84.507 +
  84.508 +   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  84.509 +}
  84.510 +
  84.511 +void /* PRIVATE */
  84.512 +png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
  84.513 +{
  84.514 +   png_ptr->process_mode = PNG_SKIP_MODE;
  84.515 +   png_ptr->skip_length = skip;
  84.516 +}
  84.517 +
  84.518 +void /* PRIVATE */
  84.519 +png_push_crc_finish(png_structp png_ptr)
  84.520 +{
  84.521 +   if (png_ptr->skip_length && png_ptr->save_buffer_size)
  84.522 +   {
  84.523 +      png_size_t save_size;
  84.524 +
  84.525 +      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
  84.526 +         save_size = (png_size_t)png_ptr->skip_length;
  84.527 +      else
  84.528 +         save_size = png_ptr->save_buffer_size;
  84.529 +
  84.530 +      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  84.531 +
  84.532 +      png_ptr->skip_length -= save_size;
  84.533 +      png_ptr->buffer_size -= save_size;
  84.534 +      png_ptr->save_buffer_size -= save_size;
  84.535 +      png_ptr->save_buffer_ptr += save_size;
  84.536 +   }
  84.537 +   if (png_ptr->skip_length && png_ptr->current_buffer_size)
  84.538 +   {
  84.539 +      png_size_t save_size;
  84.540 +
  84.541 +      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
  84.542 +         save_size = (png_size_t)png_ptr->skip_length;
  84.543 +      else
  84.544 +         save_size = png_ptr->current_buffer_size;
  84.545 +
  84.546 +      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  84.547 +
  84.548 +      png_ptr->skip_length -= save_size;
  84.549 +      png_ptr->buffer_size -= save_size;
  84.550 +      png_ptr->current_buffer_size -= save_size;
  84.551 +      png_ptr->current_buffer_ptr += save_size;
  84.552 +   }
  84.553 +   if (!png_ptr->skip_length)
  84.554 +   {
  84.555 +      if (png_ptr->buffer_size < 4)
  84.556 +      {
  84.557 +         png_push_save_buffer(png_ptr);
  84.558 +         return;
  84.559 +      }
  84.560 +
  84.561 +      png_crc_finish(png_ptr, 0);
  84.562 +      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  84.563 +   }
  84.564 +}
  84.565 +
  84.566 +void PNGAPI
  84.567 +png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
  84.568 +{
  84.569 +   png_bytep ptr;
  84.570 +
  84.571 +   if (png_ptr == NULL) return;
  84.572 +   ptr = buffer;
  84.573 +   if (png_ptr->save_buffer_size)
  84.574 +   {
  84.575 +      png_size_t save_size;
  84.576 +
  84.577 +      if (length < png_ptr->save_buffer_size)
  84.578 +         save_size = length;
  84.579 +      else
  84.580 +         save_size = png_ptr->save_buffer_size;
  84.581 +
  84.582 +      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  84.583 +      length -= save_size;
  84.584 +      ptr += save_size;
  84.585 +      png_ptr->buffer_size -= save_size;
  84.586 +      png_ptr->save_buffer_size -= save_size;
  84.587 +      png_ptr->save_buffer_ptr += save_size;
  84.588 +   }
  84.589 +   if (length && png_ptr->current_buffer_size)
  84.590 +   {
  84.591 +      png_size_t save_size;
  84.592 +
  84.593 +      if (length < png_ptr->current_buffer_size)
  84.594 +         save_size = length;
  84.595 +      else
  84.596 +         save_size = png_ptr->current_buffer_size;
  84.597 +
  84.598 +      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  84.599 +      png_ptr->buffer_size -= save_size;
  84.600 +      png_ptr->current_buffer_size -= save_size;
  84.601 +      png_ptr->current_buffer_ptr += save_size;
  84.602 +   }
  84.603 +}
  84.604 +
  84.605 +void /* PRIVATE */
  84.606 +png_push_save_buffer(png_structp png_ptr)
  84.607 +{
  84.608 +   if (png_ptr->save_buffer_size)
  84.609 +   {
  84.610 +      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  84.611 +      {
  84.612 +         png_size_t i, istop;
  84.613 +         png_bytep sp;
  84.614 +         png_bytep dp;
  84.615 +
  84.616 +         istop = png_ptr->save_buffer_size;
  84.617 +         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  84.618 +            i < istop; i++, sp++, dp++)
  84.619 +         {
  84.620 +            *dp = *sp;
  84.621 +         }
  84.622 +      }
  84.623 +   }
  84.624 +   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  84.625 +      png_ptr->save_buffer_max)
  84.626 +   {
  84.627 +      png_size_t new_max;
  84.628 +      png_bytep old_buffer;
  84.629 +
  84.630 +      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  84.631 +         (png_ptr->current_buffer_size + 256))
  84.632 +      {
  84.633 +        png_error(png_ptr, "Potential overflow of save_buffer");
  84.634 +      }
  84.635 +      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  84.636 +      old_buffer = png_ptr->save_buffer;
  84.637 +      png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
  84.638 +         (png_uint_32)new_max);
  84.639 +      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  84.640 +      png_free(png_ptr, old_buffer);
  84.641 +      png_ptr->save_buffer_max = new_max;
  84.642 +   }
  84.643 +   if (png_ptr->current_buffer_size)
  84.644 +   {
  84.645 +      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  84.646 +         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  84.647 +      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  84.648 +      png_ptr->current_buffer_size = 0;
  84.649 +   }
  84.650 +   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  84.651 +   png_ptr->buffer_size = 0;
  84.652 +}
  84.653 +
  84.654 +void /* PRIVATE */
  84.655 +png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
  84.656 +   png_size_t buffer_length)
  84.657 +{
  84.658 +   png_ptr->current_buffer = buffer;
  84.659 +   png_ptr->current_buffer_size = buffer_length;
  84.660 +   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  84.661 +   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  84.662 +}
  84.663 +
  84.664 +void /* PRIVATE */
  84.665 +png_push_read_IDAT(png_structp png_ptr)
  84.666 +{
  84.667 +#ifdef PNG_USE_LOCAL_ARRAYS
  84.668 +   PNG_CONST PNG_IDAT;
  84.669 +#endif
  84.670 +   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  84.671 +   {
  84.672 +      png_byte chunk_length[4];
  84.673 +
  84.674 +      if (png_ptr->buffer_size < 8)
  84.675 +      {
  84.676 +         png_push_save_buffer(png_ptr);
  84.677 +         return;
  84.678 +      }
  84.679 +
  84.680 +      png_push_fill_buffer(png_ptr, chunk_length, 4);
  84.681 +      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  84.682 +      png_reset_crc(png_ptr);
  84.683 +      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  84.684 +      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  84.685 +
  84.686 +      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  84.687 +      {
  84.688 +         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  84.689 +         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  84.690 +            png_error(png_ptr, "Not enough compressed data");
  84.691 +         return;
  84.692 +      }
  84.693 +
  84.694 +      png_ptr->idat_size = png_ptr->push_length;
  84.695 +   }
  84.696 +   if (png_ptr->idat_size && png_ptr->save_buffer_size)
  84.697 +   {
  84.698 +      png_size_t save_size;
  84.699 +
  84.700 +      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
  84.701 +      {
  84.702 +         save_size = (png_size_t)png_ptr->idat_size;
  84.703 +         /* check for overflow */
  84.704 +         if ((png_uint_32)save_size != png_ptr->idat_size)
  84.705 +            png_error(png_ptr, "save_size overflowed in pngpread");
  84.706 +      }
  84.707 +      else
  84.708 +         save_size = png_ptr->save_buffer_size;
  84.709 +
  84.710 +      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  84.711 +      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  84.712 +         png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  84.713 +      png_ptr->idat_size -= save_size;
  84.714 +      png_ptr->buffer_size -= save_size;
  84.715 +      png_ptr->save_buffer_size -= save_size;
  84.716 +      png_ptr->save_buffer_ptr += save_size;
  84.717 +   }
  84.718 +   if (png_ptr->idat_size && png_ptr->current_buffer_size)
  84.719 +   {
  84.720 +      png_size_t save_size;
  84.721 +
  84.722 +      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
  84.723 +      {
  84.724 +         save_size = (png_size_t)png_ptr->idat_size;
  84.725 +         /* check for overflow */
  84.726 +         if ((png_uint_32)save_size != png_ptr->idat_size)
  84.727 +            png_error(png_ptr, "save_size overflowed in pngpread");
  84.728 +      }
  84.729 +      else
  84.730 +         save_size = png_ptr->current_buffer_size;
  84.731 +
  84.732 +      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  84.733 +      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  84.734 +        png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  84.735 +
  84.736 +      png_ptr->idat_size -= save_size;
  84.737 +      png_ptr->buffer_size -= save_size;
  84.738 +      png_ptr->current_buffer_size -= save_size;
  84.739 +      png_ptr->current_buffer_ptr += save_size;
  84.740 +   }
  84.741 +   if (!png_ptr->idat_size)
  84.742 +   {
  84.743 +      if (png_ptr->buffer_size < 4)
  84.744 +      {
  84.745 +         png_push_save_buffer(png_ptr);
  84.746 +         return;
  84.747 +      }
  84.748 +
  84.749 +      png_crc_finish(png_ptr, 0);
  84.750 +      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  84.751 +      png_ptr->mode |= PNG_AFTER_IDAT;
  84.752 +   }
  84.753 +}
  84.754 +
  84.755 +void /* PRIVATE */
  84.756 +png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
  84.757 +   png_size_t buffer_length)
  84.758 +{
  84.759 +   int ret;
  84.760 +
  84.761 +   if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
  84.762 +      png_error(png_ptr, "Extra compression data");
  84.763 +
  84.764 +   png_ptr->zstream.next_in = buffer;
  84.765 +   png_ptr->zstream.avail_in = (uInt)buffer_length;
  84.766 +   for (;;)
  84.767 +   {
  84.768 +      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  84.769 +      if (ret != Z_OK)
  84.770 +      {
  84.771 +         if (ret == Z_STREAM_END)
  84.772 +         {
  84.773 +            if (png_ptr->zstream.avail_in)
  84.774 +               png_error(png_ptr, "Extra compressed data");
  84.775 +            if (!(png_ptr->zstream.avail_out))
  84.776 +            {
  84.777 +               png_push_process_row(png_ptr);
  84.778 +            }
  84.779 +
  84.780 +            png_ptr->mode |= PNG_AFTER_IDAT;
  84.781 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  84.782 +            break;
  84.783 +         }
  84.784 +         else if (ret == Z_BUF_ERROR)
  84.785 +            break;
  84.786 +         else
  84.787 +            png_error(png_ptr, "Decompression Error");
  84.788 +      }
  84.789 +      if (!(png_ptr->zstream.avail_out))
  84.790 +      {
  84.791 +         if ((
  84.792 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  84.793 +             png_ptr->interlaced && png_ptr->pass > 6) ||
  84.794 +             (!png_ptr->interlaced &&
  84.795 +#endif
  84.796 +             png_ptr->row_number == png_ptr->num_rows))
  84.797 +         {
  84.798 +           if (png_ptr->zstream.avail_in)
  84.799 +             png_warning(png_ptr, "Too much data in IDAT chunks");
  84.800 +           png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  84.801 +           break;
  84.802 +         }
  84.803 +         png_push_process_row(png_ptr);
  84.804 +         png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  84.805 +         png_ptr->zstream.next_out = png_ptr->row_buf;
  84.806 +      }
  84.807 +      else
  84.808 +         break;
  84.809 +   }
  84.810 +}
  84.811 +
  84.812 +void /* PRIVATE */
  84.813 +png_push_process_row(png_structp png_ptr)
  84.814 +{
  84.815 +   png_ptr->row_info.color_type = png_ptr->color_type;
  84.816 +   png_ptr->row_info.width = png_ptr->iwidth;
  84.817 +   png_ptr->row_info.channels = png_ptr->channels;
  84.818 +   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  84.819 +   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  84.820 +
  84.821 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  84.822 +       png_ptr->row_info.width);
  84.823 +
  84.824 +   png_read_filter_row(png_ptr, &(png_ptr->row_info),
  84.825 +      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  84.826 +      (int)(png_ptr->row_buf[0]));
  84.827 +
  84.828 +   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  84.829 +      png_ptr->rowbytes + 1);
  84.830 +
  84.831 +   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
  84.832 +      png_do_read_transformations(png_ptr);
  84.833 +
  84.834 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  84.835 +   /* blow up interlaced rows to full size */
  84.836 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  84.837 +   {
  84.838 +      if (png_ptr->pass < 6)
  84.839 +/*       old interface (pre-1.0.9):
  84.840 +         png_do_read_interlace(&(png_ptr->row_info),
  84.841 +            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  84.842 + */
  84.843 +         png_do_read_interlace(png_ptr);
  84.844 +
  84.845 +    switch (png_ptr->pass)
  84.846 +    {
  84.847 +         case 0:
  84.848 +         {
  84.849 +            int i;
  84.850 +            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  84.851 +            {
  84.852 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.853 +               png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
  84.854 +            }
  84.855 +            if (png_ptr->pass == 2) /* pass 1 might be empty */
  84.856 +            {
  84.857 +               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  84.858 +               {
  84.859 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  84.860 +                  png_read_push_finish_row(png_ptr);
  84.861 +               }
  84.862 +            }
  84.863 +            if (png_ptr->pass == 4 && png_ptr->height <= 4)
  84.864 +            {
  84.865 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  84.866 +               {
  84.867 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  84.868 +                  png_read_push_finish_row(png_ptr);
  84.869 +               }
  84.870 +            }
  84.871 +            if (png_ptr->pass == 6 && png_ptr->height <= 4)
  84.872 +            {
  84.873 +                png_push_have_row(png_ptr, png_bytep_NULL);
  84.874 +                png_read_push_finish_row(png_ptr);
  84.875 +            }
  84.876 +            break;
  84.877 +         }
  84.878 +         case 1:
  84.879 +         {
  84.880 +            int i;
  84.881 +            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  84.882 +            {
  84.883 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.884 +               png_read_push_finish_row(png_ptr);
  84.885 +            }
  84.886 +            if (png_ptr->pass == 2) /* skip top 4 generated rows */
  84.887 +            {
  84.888 +               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  84.889 +               {
  84.890 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  84.891 +                  png_read_push_finish_row(png_ptr);
  84.892 +               }
  84.893 +            }
  84.894 +            break;
  84.895 +         }
  84.896 +         case 2:
  84.897 +         {
  84.898 +            int i;
  84.899 +            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  84.900 +            {
  84.901 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.902 +               png_read_push_finish_row(png_ptr);
  84.903 +            }
  84.904 +            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  84.905 +            {
  84.906 +               png_push_have_row(png_ptr, png_bytep_NULL);
  84.907 +               png_read_push_finish_row(png_ptr);
  84.908 +            }
  84.909 +            if (png_ptr->pass == 4) /* pass 3 might be empty */
  84.910 +            {
  84.911 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  84.912 +               {
  84.913 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  84.914 +                  png_read_push_finish_row(png_ptr);
  84.915 +               }
  84.916 +            }
  84.917 +            break;
  84.918 +         }
  84.919 +         case 3:
  84.920 +         {
  84.921 +            int i;
  84.922 +            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  84.923 +            {
  84.924 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.925 +               png_read_push_finish_row(png_ptr);
  84.926 +            }
  84.927 +            if (png_ptr->pass == 4) /* skip top two generated rows */
  84.928 +            {
  84.929 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  84.930 +               {
  84.931 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  84.932 +                  png_read_push_finish_row(png_ptr);
  84.933 +               }
  84.934 +            }
  84.935 +            break;
  84.936 +         }
  84.937 +         case 4:
  84.938 +         {
  84.939 +            int i;
  84.940 +            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  84.941 +            {
  84.942 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.943 +               png_read_push_finish_row(png_ptr);
  84.944 +            }
  84.945 +            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  84.946 +            {
  84.947 +               png_push_have_row(png_ptr, png_bytep_NULL);
  84.948 +               png_read_push_finish_row(png_ptr);
  84.949 +            }
  84.950 +            if (png_ptr->pass == 6) /* pass 5 might be empty */
  84.951 +            {
  84.952 +               png_push_have_row(png_ptr, png_bytep_NULL);
  84.953 +               png_read_push_finish_row(png_ptr);
  84.954 +            }
  84.955 +            break;
  84.956 +         }
  84.957 +         case 5:
  84.958 +         {
  84.959 +            int i;
  84.960 +            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  84.961 +            {
  84.962 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.963 +               png_read_push_finish_row(png_ptr);
  84.964 +            }
  84.965 +            if (png_ptr->pass == 6) /* skip top generated row */
  84.966 +            {
  84.967 +               png_push_have_row(png_ptr, png_bytep_NULL);
  84.968 +               png_read_push_finish_row(png_ptr);
  84.969 +            }
  84.970 +            break;
  84.971 +         }
  84.972 +         case 6:
  84.973 +         {
  84.974 +            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.975 +            png_read_push_finish_row(png_ptr);
  84.976 +            if (png_ptr->pass != 6)
  84.977 +               break;
  84.978 +            png_push_have_row(png_ptr, png_bytep_NULL);
  84.979 +            png_read_push_finish_row(png_ptr);
  84.980 +         }
  84.981 +      }
  84.982 +   }
  84.983 +   else
  84.984 +#endif
  84.985 +   {
  84.986 +      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  84.987 +      png_read_push_finish_row(png_ptr);
  84.988 +   }
  84.989 +}
  84.990 +
  84.991 +void /* PRIVATE */
  84.992 +png_read_push_finish_row(png_structp png_ptr)
  84.993 +{
  84.994 +#ifdef PNG_USE_LOCAL_ARRAYS
  84.995 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  84.996 +
  84.997 +   /* start of interlace block */
  84.998 +   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  84.999 +
 84.1000 +   /* offset to next interlace block */
 84.1001 +   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
 84.1002 +
 84.1003 +   /* start of interlace block in the y direction */
 84.1004 +   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
 84.1005 +
 84.1006 +   /* offset to next interlace block in the y direction */
 84.1007 +   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
 84.1008 +
 84.1009 +   /* Height of interlace block.  This is not currently used - if you need
 84.1010 +    * it, uncomment it here and in png.h
 84.1011 +   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
 84.1012 +   */
 84.1013 +#endif
 84.1014 +
 84.1015 +   png_ptr->row_number++;
 84.1016 +   if (png_ptr->row_number < png_ptr->num_rows)
 84.1017 +      return;
 84.1018 +
 84.1019 +   if (png_ptr->interlaced)
 84.1020 +   {
 84.1021 +      png_ptr->row_number = 0;
 84.1022 +      png_memset_check(png_ptr, png_ptr->prev_row, 0,
 84.1023 +         png_ptr->rowbytes + 1);
 84.1024 +      do
 84.1025 +      {
 84.1026 +         png_ptr->pass++;
 84.1027 +         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
 84.1028 +             (png_ptr->pass == 3 && png_ptr->width < 3) ||
 84.1029 +             (png_ptr->pass == 5 && png_ptr->width < 2))
 84.1030 +           png_ptr->pass++;
 84.1031 +
 84.1032 +         if (png_ptr->pass > 7)
 84.1033 +            png_ptr->pass--;
 84.1034 +         if (png_ptr->pass >= 7)
 84.1035 +            break;
 84.1036 +
 84.1037 +         png_ptr->iwidth = (png_ptr->width +
 84.1038 +            png_pass_inc[png_ptr->pass] - 1 -
 84.1039 +            png_pass_start[png_ptr->pass]) /
 84.1040 +            png_pass_inc[png_ptr->pass];
 84.1041 +
 84.1042 +         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
 84.1043 +            png_ptr->iwidth) + 1;
 84.1044 +
 84.1045 +         if (png_ptr->transformations & PNG_INTERLACE)
 84.1046 +            break;
 84.1047 +
 84.1048 +         png_ptr->num_rows = (png_ptr->height +
 84.1049 +            png_pass_yinc[png_ptr->pass] - 1 -
 84.1050 +            png_pass_ystart[png_ptr->pass]) /
 84.1051 +            png_pass_yinc[png_ptr->pass];
 84.1052 +
 84.1053 +      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
 84.1054 +   }
 84.1055 +}
 84.1056 +
 84.1057 +#if defined(PNG_READ_tEXt_SUPPORTED)
 84.1058 +void /* PRIVATE */
 84.1059 +png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 84.1060 +   length)
 84.1061 +{
 84.1062 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 84.1063 +      {
 84.1064 +         png_error(png_ptr, "Out of place tEXt");
 84.1065 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 84.1066 +      }
 84.1067 +
 84.1068 +#ifdef PNG_MAX_MALLOC_64K
 84.1069 +   png_ptr->skip_length = 0;  /* This may not be necessary */
 84.1070 +
 84.1071 +   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
 84.1072 +   {
 84.1073 +      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
 84.1074 +      png_ptr->skip_length = length - (png_uint_32)65535L;
 84.1075 +      length = (png_uint_32)65535L;
 84.1076 +   }
 84.1077 +#endif
 84.1078 +
 84.1079 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 84.1080 +      (png_uint_32)(length + 1));
 84.1081 +   png_ptr->current_text[length] = '\0';
 84.1082 +   png_ptr->current_text_ptr = png_ptr->current_text;
 84.1083 +   png_ptr->current_text_size = (png_size_t)length;
 84.1084 +   png_ptr->current_text_left = (png_size_t)length;
 84.1085 +   png_ptr->process_mode = PNG_READ_tEXt_MODE;
 84.1086 +}
 84.1087 +
 84.1088 +void /* PRIVATE */
 84.1089 +png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
 84.1090 +{
 84.1091 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 84.1092 +   {
 84.1093 +      png_size_t text_size;
 84.1094 +
 84.1095 +      if (png_ptr->buffer_size < png_ptr->current_text_left)
 84.1096 +         text_size = png_ptr->buffer_size;
 84.1097 +      else
 84.1098 +         text_size = png_ptr->current_text_left;
 84.1099 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 84.1100 +      png_ptr->current_text_left -= text_size;
 84.1101 +      png_ptr->current_text_ptr += text_size;
 84.1102 +   }
 84.1103 +   if (!(png_ptr->current_text_left))
 84.1104 +   {
 84.1105 +      png_textp text_ptr;
 84.1106 +      png_charp text;
 84.1107 +      png_charp key;
 84.1108 +      int ret;
 84.1109 +
 84.1110 +      if (png_ptr->buffer_size < 4)
 84.1111 +      {
 84.1112 +         png_push_save_buffer(png_ptr);
 84.1113 +         return;
 84.1114 +      }
 84.1115 +
 84.1116 +      png_push_crc_finish(png_ptr);
 84.1117 +
 84.1118 +#if defined(PNG_MAX_MALLOC_64K)
 84.1119 +      if (png_ptr->skip_length)
 84.1120 +         return;
 84.1121 +#endif
 84.1122 +
 84.1123 +      key = png_ptr->current_text;
 84.1124 +
 84.1125 +      for (text = key; *text; text++)
 84.1126 +         /* empty loop */ ;
 84.1127 +
 84.1128 +      if (text < key + png_ptr->current_text_size)
 84.1129 +         text++;
 84.1130 +
 84.1131 +      text_ptr = (png_textp)png_malloc(png_ptr,
 84.1132 +         (png_uint_32)png_sizeof(png_text));
 84.1133 +      text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
 84.1134 +      text_ptr->key = key;
 84.1135 +#ifdef PNG_iTXt_SUPPORTED
 84.1136 +      text_ptr->lang = NULL;
 84.1137 +      text_ptr->lang_key = NULL;
 84.1138 +#endif
 84.1139 +      text_ptr->text = text;
 84.1140 +
 84.1141 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 84.1142 +
 84.1143 +      png_free(png_ptr, key);
 84.1144 +      png_free(png_ptr, text_ptr);
 84.1145 +      png_ptr->current_text = NULL;
 84.1146 +
 84.1147 +      if (ret)
 84.1148 +        png_warning(png_ptr, "Insufficient memory to store text chunk.");
 84.1149 +   }
 84.1150 +}
 84.1151 +#endif
 84.1152 +
 84.1153 +#if defined(PNG_READ_zTXt_SUPPORTED)
 84.1154 +void /* PRIVATE */
 84.1155 +png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 84.1156 +   length)
 84.1157 +{
 84.1158 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 84.1159 +      {
 84.1160 +         png_error(png_ptr, "Out of place zTXt");
 84.1161 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 84.1162 +      }
 84.1163 +
 84.1164 +#ifdef PNG_MAX_MALLOC_64K
 84.1165 +   /* We can't handle zTXt chunks > 64K, since we don't have enough space
 84.1166 +    * to be able to store the uncompressed data.  Actually, the threshold
 84.1167 +    * is probably around 32K, but it isn't as definite as 64K is.
 84.1168 +    */
 84.1169 +   if (length > (png_uint_32)65535L)
 84.1170 +   {
 84.1171 +      png_warning(png_ptr, "zTXt chunk too large to fit in memory");
 84.1172 +      png_push_crc_skip(png_ptr, length);
 84.1173 +      return;
 84.1174 +   }
 84.1175 +#endif
 84.1176 +
 84.1177 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 84.1178 +      (png_uint_32)(length + 1));
 84.1179 +   png_ptr->current_text[length] = '\0';
 84.1180 +   png_ptr->current_text_ptr = png_ptr->current_text;
 84.1181 +   png_ptr->current_text_size = (png_size_t)length;
 84.1182 +   png_ptr->current_text_left = (png_size_t)length;
 84.1183 +   png_ptr->process_mode = PNG_READ_zTXt_MODE;
 84.1184 +}
 84.1185 +
 84.1186 +void /* PRIVATE */
 84.1187 +png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
 84.1188 +{
 84.1189 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 84.1190 +   {
 84.1191 +      png_size_t text_size;
 84.1192 +
 84.1193 +      if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
 84.1194 +         text_size = png_ptr->buffer_size;
 84.1195 +      else
 84.1196 +         text_size = png_ptr->current_text_left;
 84.1197 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 84.1198 +      png_ptr->current_text_left -= text_size;
 84.1199 +      png_ptr->current_text_ptr += text_size;
 84.1200 +   }
 84.1201 +   if (!(png_ptr->current_text_left))
 84.1202 +   {
 84.1203 +      png_textp text_ptr;
 84.1204 +      png_charp text;
 84.1205 +      png_charp key;
 84.1206 +      int ret;
 84.1207 +      png_size_t text_size, key_size;
 84.1208 +
 84.1209 +      if (png_ptr->buffer_size < 4)
 84.1210 +      {
 84.1211 +         png_push_save_buffer(png_ptr);
 84.1212 +         return;
 84.1213 +      }
 84.1214 +
 84.1215 +      png_push_crc_finish(png_ptr);
 84.1216 +
 84.1217 +      key = png_ptr->current_text;
 84.1218 +
 84.1219 +      for (text = key; *text; text++)
 84.1220 +         /* empty loop */ ;
 84.1221 +
 84.1222 +      /* zTXt can't have zero text */
 84.1223 +      if (text >= key + png_ptr->current_text_size)
 84.1224 +      {
 84.1225 +         png_ptr->current_text = NULL;
 84.1226 +         png_free(png_ptr, key);
 84.1227 +         return;
 84.1228 +      }
 84.1229 +
 84.1230 +      text++;
 84.1231 +
 84.1232 +      if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
 84.1233 +      {
 84.1234 +         png_ptr->current_text = NULL;
 84.1235 +         png_free(png_ptr, key);
 84.1236 +         return;
 84.1237 +      }
 84.1238 +
 84.1239 +      text++;
 84.1240 +
 84.1241 +      png_ptr->zstream.next_in = (png_bytep )text;
 84.1242 +      png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
 84.1243 +         (text - key));
 84.1244 +      png_ptr->zstream.next_out = png_ptr->zbuf;
 84.1245 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 84.1246 +
 84.1247 +      key_size = text - key;
 84.1248 +      text_size = 0;
 84.1249 +      text = NULL;
 84.1250 +      ret = Z_STREAM_END;
 84.1251 +
 84.1252 +      while (png_ptr->zstream.avail_in)
 84.1253 +      {
 84.1254 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
 84.1255 +         if (ret != Z_OK && ret != Z_STREAM_END)
 84.1256 +         {
 84.1257 +            inflateReset(&png_ptr->zstream);
 84.1258 +            png_ptr->zstream.avail_in = 0;
 84.1259 +            png_ptr->current_text = NULL;
 84.1260 +            png_free(png_ptr, key);
 84.1261 +            png_free(png_ptr, text);
 84.1262 +            return;
 84.1263 +         }
 84.1264 +         if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
 84.1265 +         {
 84.1266 +            if (text == NULL)
 84.1267 +            {
 84.1268 +               text = (png_charp)png_malloc(png_ptr,
 84.1269 +                     (png_uint_32)(png_ptr->zbuf_size
 84.1270 +                     - png_ptr->zstream.avail_out + key_size + 1));
 84.1271 +               png_memcpy(text + key_size, png_ptr->zbuf,
 84.1272 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
 84.1273 +               png_memcpy(text, key, key_size);
 84.1274 +               text_size = key_size + png_ptr->zbuf_size -
 84.1275 +                  png_ptr->zstream.avail_out;
 84.1276 +               *(text + text_size) = '\0';
 84.1277 +            }
 84.1278 +            else
 84.1279 +            {
 84.1280 +               png_charp tmp;
 84.1281 +
 84.1282 +               tmp = text;
 84.1283 +               text = (png_charp)png_malloc(png_ptr, text_size +
 84.1284 +                  (png_uint_32)(png_ptr->zbuf_size 
 84.1285 +                  - png_ptr->zstream.avail_out + 1));
 84.1286 +               png_memcpy(text, tmp, text_size);
 84.1287 +               png_free(png_ptr, tmp);
 84.1288 +               png_memcpy(text + text_size, png_ptr->zbuf,
 84.1289 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
 84.1290 +               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
 84.1291 +               *(text + text_size) = '\0';
 84.1292 +            }
 84.1293 +            if (ret != Z_STREAM_END)
 84.1294 +            {
 84.1295 +               png_ptr->zstream.next_out = png_ptr->zbuf;
 84.1296 +               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 84.1297 +            }
 84.1298 +         }
 84.1299 +         else
 84.1300 +         {
 84.1301 +            break;
 84.1302 +         }
 84.1303 +
 84.1304 +         if (ret == Z_STREAM_END)
 84.1305 +            break;
 84.1306 +      }
 84.1307 +
 84.1308 +      inflateReset(&png_ptr->zstream);
 84.1309 +      png_ptr->zstream.avail_in = 0;
 84.1310 +
 84.1311 +      if (ret != Z_STREAM_END)
 84.1312 +      {
 84.1313 +         png_ptr->current_text = NULL;
 84.1314 +         png_free(png_ptr, key);
 84.1315 +         png_free(png_ptr, text);
 84.1316 +         return;
 84.1317 +      }
 84.1318 +
 84.1319 +      png_ptr->current_text = NULL;
 84.1320 +      png_free(png_ptr, key);
 84.1321 +      key = text;
 84.1322 +      text += key_size;
 84.1323 +
 84.1324 +      text_ptr = (png_textp)png_malloc(png_ptr,
 84.1325 +          (png_uint_32)png_sizeof(png_text));
 84.1326 +      text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
 84.1327 +      text_ptr->key = key;
 84.1328 +#ifdef PNG_iTXt_SUPPORTED
 84.1329 +      text_ptr->lang = NULL;
 84.1330 +      text_ptr->lang_key = NULL;
 84.1331 +#endif
 84.1332 +      text_ptr->text = text;
 84.1333 +
 84.1334 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 84.1335 +
 84.1336 +      png_free(png_ptr, key);
 84.1337 +      png_free(png_ptr, text_ptr);
 84.1338 +
 84.1339 +      if (ret)
 84.1340 +        png_warning(png_ptr, "Insufficient memory to store text chunk.");
 84.1341 +   }
 84.1342 +}
 84.1343 +#endif
 84.1344 +
 84.1345 +#if defined(PNG_READ_iTXt_SUPPORTED)
 84.1346 +void /* PRIVATE */
 84.1347 +png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 84.1348 +   length)
 84.1349 +{
 84.1350 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 84.1351 +      {
 84.1352 +         png_error(png_ptr, "Out of place iTXt");
 84.1353 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 84.1354 +      }
 84.1355 +
 84.1356 +#ifdef PNG_MAX_MALLOC_64K
 84.1357 +   png_ptr->skip_length = 0;  /* This may not be necessary */
 84.1358 +
 84.1359 +   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
 84.1360 +   {
 84.1361 +      png_warning(png_ptr, "iTXt chunk too large to fit in memory");
 84.1362 +      png_ptr->skip_length = length - (png_uint_32)65535L;
 84.1363 +      length = (png_uint_32)65535L;
 84.1364 +   }
 84.1365 +#endif
 84.1366 +
 84.1367 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 84.1368 +      (png_uint_32)(length + 1));
 84.1369 +   png_ptr->current_text[length] = '\0';
 84.1370 +   png_ptr->current_text_ptr = png_ptr->current_text;
 84.1371 +   png_ptr->current_text_size = (png_size_t)length;
 84.1372 +   png_ptr->current_text_left = (png_size_t)length;
 84.1373 +   png_ptr->process_mode = PNG_READ_iTXt_MODE;
 84.1374 +}
 84.1375 +
 84.1376 +void /* PRIVATE */
 84.1377 +png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
 84.1378 +{
 84.1379 +
 84.1380 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 84.1381 +   {
 84.1382 +      png_size_t text_size;
 84.1383 +
 84.1384 +      if (png_ptr->buffer_size < png_ptr->current_text_left)
 84.1385 +         text_size = png_ptr->buffer_size;
 84.1386 +      else
 84.1387 +         text_size = png_ptr->current_text_left;
 84.1388 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 84.1389 +      png_ptr->current_text_left -= text_size;
 84.1390 +      png_ptr->current_text_ptr += text_size;
 84.1391 +   }
 84.1392 +   if (!(png_ptr->current_text_left))
 84.1393 +   {
 84.1394 +      png_textp text_ptr;
 84.1395 +      png_charp key;
 84.1396 +      int comp_flag;
 84.1397 +      png_charp lang;
 84.1398 +      png_charp lang_key;
 84.1399 +      png_charp text;
 84.1400 +      int ret;
 84.1401 +
 84.1402 +      if (png_ptr->buffer_size < 4)
 84.1403 +      {
 84.1404 +         png_push_save_buffer(png_ptr);
 84.1405 +         return;
 84.1406 +      }
 84.1407 +
 84.1408 +      png_push_crc_finish(png_ptr);
 84.1409 +
 84.1410 +#if defined(PNG_MAX_MALLOC_64K)
 84.1411 +      if (png_ptr->skip_length)
 84.1412 +         return;
 84.1413 +#endif
 84.1414 +
 84.1415 +      key = png_ptr->current_text;
 84.1416 +
 84.1417 +      for (lang = key; *lang; lang++)
 84.1418 +         /* empty loop */ ;
 84.1419 +
 84.1420 +      if (lang < key + png_ptr->current_text_size - 3)
 84.1421 +         lang++;
 84.1422 +
 84.1423 +      comp_flag = *lang++;
 84.1424 +      lang++;     /* skip comp_type, always zero */
 84.1425 +
 84.1426 +      for (lang_key = lang; *lang_key; lang_key++)
 84.1427 +         /* empty loop */ ;
 84.1428 +      lang_key++;        /* skip NUL separator */
 84.1429 +
 84.1430 +      text=lang_key;
 84.1431 +      if (lang_key < key + png_ptr->current_text_size - 1)
 84.1432 +      {
 84.1433 +        for (; *text; text++)
 84.1434 +           /* empty loop */ ;
 84.1435 +      }
 84.1436 +
 84.1437 +      if (text < key + png_ptr->current_text_size)
 84.1438 +         text++;
 84.1439 +
 84.1440 +      text_ptr = (png_textp)png_malloc(png_ptr,
 84.1441 +         (png_uint_32)png_sizeof(png_text));
 84.1442 +      text_ptr->compression = comp_flag + 2;
 84.1443 +      text_ptr->key = key;
 84.1444 +      text_ptr->lang = lang;
 84.1445 +      text_ptr->lang_key = lang_key;
 84.1446 +      text_ptr->text = text;
 84.1447 +      text_ptr->text_length = 0;
 84.1448 +      text_ptr->itxt_length = png_strlen(text);
 84.1449 +
 84.1450 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 84.1451 +
 84.1452 +      png_ptr->current_text = NULL;
 84.1453 +
 84.1454 +      png_free(png_ptr, text_ptr);
 84.1455 +      if (ret)
 84.1456 +        png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
 84.1457 +   }
 84.1458 +}
 84.1459 +#endif
 84.1460 +
 84.1461 +/* This function is called when we haven't found a handler for this
 84.1462 + * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk
 84.1463 + * name or a critical chunk), the chunk is (currently) silently ignored.
 84.1464 + */
 84.1465 +void /* PRIVATE */
 84.1466 +png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
 84.1467 +   length)
 84.1468 +{
 84.1469 +   png_uint_32 skip = 0;
 84.1470 +
 84.1471 +   if (!(png_ptr->chunk_name[0] & 0x20))
 84.1472 +   {
 84.1473 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 84.1474 +      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 84.1475 +         PNG_HANDLE_CHUNK_ALWAYS
 84.1476 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 84.1477 +         && png_ptr->read_user_chunk_fn == NULL
 84.1478 +#endif
 84.1479 +         )
 84.1480 +#endif
 84.1481 +         png_chunk_error(png_ptr, "unknown critical chunk");
 84.1482 +
 84.1483 +      info_ptr = info_ptr; /* to quiet some compiler warnings */
 84.1484 +   }
 84.1485 +
 84.1486 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 84.1487 +   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
 84.1488 +   {
 84.1489 +#ifdef PNG_MAX_MALLOC_64K
 84.1490 +      if (length > (png_uint_32)65535L)
 84.1491 +      {
 84.1492 +          png_warning(png_ptr, "unknown chunk too large to fit in memory");
 84.1493 +          skip = length - (png_uint_32)65535L;
 84.1494 +          length = (png_uint_32)65535L;
 84.1495 +      }
 84.1496 +#endif
 84.1497 +      png_memcpy((png_charp)png_ptr->unknown_chunk.name,
 84.1498 +                 (png_charp)png_ptr->chunk_name, 
 84.1499 +                 png_sizeof(png_ptr->unknown_chunk.name));
 84.1500 +      png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
 84.1501 +        = '\0';
 84.1502 +
 84.1503 +      png_ptr->unknown_chunk.size = (png_size_t)length;
 84.1504 +      if (length == 0)
 84.1505 +         png_ptr->unknown_chunk.data = NULL;
 84.1506 +      else
 84.1507 +      {
 84.1508 +         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
 84.1509 +       (png_uint_32)length);
 84.1510 +         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
 84.1511 +      }
 84.1512 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 84.1513 +      if (png_ptr->read_user_chunk_fn != NULL)
 84.1514 +      {
 84.1515 +         /* callback to user unknown chunk handler */
 84.1516 +         int ret;
 84.1517 +         ret = (*(png_ptr->read_user_chunk_fn))
 84.1518 +           (png_ptr, &png_ptr->unknown_chunk);
 84.1519 +         if (ret < 0)
 84.1520 +            png_chunk_error(png_ptr, "error in user chunk");
 84.1521 +         if (ret == 0)
 84.1522 +         {
 84.1523 +            if (!(png_ptr->chunk_name[0] & 0x20))
 84.1524 +               if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 84.1525 +                    PNG_HANDLE_CHUNK_ALWAYS)
 84.1526 +                  png_chunk_error(png_ptr, "unknown critical chunk");
 84.1527 +            png_set_unknown_chunks(png_ptr, info_ptr,
 84.1528 +               &png_ptr->unknown_chunk, 1);
 84.1529 +         }
 84.1530 +      }
 84.1531 +      else
 84.1532 +#endif
 84.1533 +        png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
 84.1534 +      png_free(png_ptr, png_ptr->unknown_chunk.data);
 84.1535 +      png_ptr->unknown_chunk.data = NULL;
 84.1536 +   }
 84.1537 +   else
 84.1538 +#endif
 84.1539 +      skip=length;
 84.1540 +   png_push_crc_skip(png_ptr, skip);
 84.1541 +}
 84.1542 +
 84.1543 +void /* PRIVATE */
 84.1544 +png_push_have_info(png_structp png_ptr, png_infop info_ptr)
 84.1545 +{
 84.1546 +   if (png_ptr->info_fn != NULL)
 84.1547 +      (*(png_ptr->info_fn))(png_ptr, info_ptr);
 84.1548 +}
 84.1549 +
 84.1550 +void /* PRIVATE */
 84.1551 +png_push_have_end(png_structp png_ptr, png_infop info_ptr)
 84.1552 +{
 84.1553 +   if (png_ptr->end_fn != NULL)
 84.1554 +      (*(png_ptr->end_fn))(png_ptr, info_ptr);
 84.1555 +}
 84.1556 +
 84.1557 +void /* PRIVATE */
 84.1558 +png_push_have_row(png_structp png_ptr, png_bytep row)
 84.1559 +{
 84.1560 +   if (png_ptr->row_fn != NULL)
 84.1561 +      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
 84.1562 +         (int)png_ptr->pass);
 84.1563 +}
 84.1564 +
 84.1565 +void PNGAPI
 84.1566 +png_progressive_combine_row (png_structp png_ptr,
 84.1567 +   png_bytep old_row, png_bytep new_row)
 84.1568 +{
 84.1569 +#ifdef PNG_USE_LOCAL_ARRAYS
 84.1570 +   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
 84.1571 +      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
 84.1572 +#endif
 84.1573 +   if (png_ptr == NULL) return;
 84.1574 +   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
 84.1575 +      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
 84.1576 +}
 84.1577 +
 84.1578 +void PNGAPI
 84.1579 +png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
 84.1580 +   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
 84.1581 +   png_progressive_end_ptr end_fn)
 84.1582 +{
 84.1583 +   if (png_ptr == NULL) return;
 84.1584 +   png_ptr->info_fn = info_fn;
 84.1585 +   png_ptr->row_fn = row_fn;
 84.1586 +   png_ptr->end_fn = end_fn;
 84.1587 +
 84.1588 +   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
 84.1589 +}
 84.1590 +
 84.1591 +png_voidp PNGAPI
 84.1592 +png_get_progressive_ptr(png_structp png_ptr)
 84.1593 +{
 84.1594 +   if (png_ptr == NULL) return (NULL);
 84.1595 +   return png_ptr->io_ptr;
 84.1596 +}
 84.1597 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
    85.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    85.2 +++ b/libs/libpng/pngread.c	Sun Jun 07 17:25:49 2015 +0300
    85.3 @@ -0,0 +1,1459 @@
    85.4 +
    85.5 +/* pngread.c - read a PNG file
    85.6 + *
    85.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    85.8 + * For conditions of distribution and use, see copyright notice in png.h
    85.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   85.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   85.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   85.12 + *
   85.13 + * This file contains routines that an application calls directly to
   85.14 + * read a PNG file or stream.
   85.15 + */
   85.16 +
   85.17 +#define PNG_INTERNAL
   85.18 +#include "png.h"
   85.19 +#if defined(PNG_READ_SUPPORTED)
   85.20 +
   85.21 +/* Create a PNG structure for reading, and allocate any memory needed. */
   85.22 +png_structp PNGAPI
   85.23 +png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
   85.24 +   png_error_ptr error_fn, png_error_ptr warn_fn)
   85.25 +{
   85.26 +
   85.27 +#ifdef PNG_USER_MEM_SUPPORTED
   85.28 +   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
   85.29 +      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
   85.30 +}
   85.31 +
   85.32 +/* Alternate create PNG structure for reading, and allocate any memory needed. */
   85.33 +png_structp PNGAPI
   85.34 +png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
   85.35 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   85.36 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
   85.37 +{
   85.38 +#endif /* PNG_USER_MEM_SUPPORTED */
   85.39 +
   85.40 +#ifdef PNG_SETJMP_SUPPORTED
   85.41 +   volatile
   85.42 +#endif
   85.43 +   png_structp png_ptr;
   85.44 +
   85.45 +#ifdef PNG_SETJMP_SUPPORTED
   85.46 +#ifdef USE_FAR_KEYWORD
   85.47 +   jmp_buf jmpbuf;
   85.48 +#endif
   85.49 +#endif
   85.50 +
   85.51 +   int i;
   85.52 +
   85.53 +   png_debug(1, "in png_create_read_struct\n");
   85.54 +#ifdef PNG_USER_MEM_SUPPORTED
   85.55 +   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
   85.56 +      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
   85.57 +#else
   85.58 +   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
   85.59 +#endif
   85.60 +   if (png_ptr == NULL)
   85.61 +      return (NULL);
   85.62 +
   85.63 +   /* added at libpng-1.2.6 */
   85.64 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   85.65 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   85.66 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
   85.67 +#endif
   85.68 +
   85.69 +#ifdef PNG_SETJMP_SUPPORTED
   85.70 +#ifdef USE_FAR_KEYWORD
   85.71 +   if (setjmp(jmpbuf))
   85.72 +#else
   85.73 +   if (setjmp(png_ptr->jmpbuf))
   85.74 +#endif
   85.75 +   {
   85.76 +      png_free(png_ptr, png_ptr->zbuf);
   85.77 +      png_ptr->zbuf = NULL;
   85.78 +#ifdef PNG_USER_MEM_SUPPORTED
   85.79 +      png_destroy_struct_2((png_voidp)png_ptr,
   85.80 +         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
   85.81 +#else
   85.82 +      png_destroy_struct((png_voidp)png_ptr);
   85.83 +#endif
   85.84 +      return (NULL);
   85.85 +   }
   85.86 +#ifdef USE_FAR_KEYWORD
   85.87 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
   85.88 +#endif
   85.89 +#endif
   85.90 +
   85.91 +#ifdef PNG_USER_MEM_SUPPORTED
   85.92 +   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
   85.93 +#endif
   85.94 +
   85.95 +   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
   85.96 +
   85.97 +   if (user_png_ver)
   85.98 +   {
   85.99 +     i = 0;
  85.100 +     do
  85.101 +     {
  85.102 +       if (user_png_ver[i] != png_libpng_ver[i])
  85.103 +          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  85.104 +     } while (png_libpng_ver[i++]);
  85.105 +   }
  85.106 +   else
  85.107 +        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  85.108 +   
  85.109 +
  85.110 +   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  85.111 +   {
  85.112 +     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  85.113 +      * we must recompile any applications that use any older library version.
  85.114 +      * For versions after libpng 1.0, we will be compatible, so we need
  85.115 +      * only check the first digit.
  85.116 +      */
  85.117 +     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  85.118 +         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  85.119 +         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  85.120 +     {
  85.121 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  85.122 +        char msg[80];
  85.123 +        if (user_png_ver)
  85.124 +        {
  85.125 +          png_snprintf(msg, 80,
  85.126 +             "Application was compiled with png.h from libpng-%.20s",
  85.127 +             user_png_ver);
  85.128 +          png_warning(png_ptr, msg);
  85.129 +        }
  85.130 +        png_snprintf(msg, 80,
  85.131 +             "Application  is  running with png.c from libpng-%.20s",
  85.132 +           png_libpng_ver);
  85.133 +        png_warning(png_ptr, msg);
  85.134 +#endif
  85.135 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  85.136 +        png_ptr->flags = 0;
  85.137 +#endif
  85.138 +        png_error(png_ptr,
  85.139 +           "Incompatible libpng version in application and library");
  85.140 +     }
  85.141 +   }
  85.142 +
  85.143 +   /* initialize zbuf - compression buffer */
  85.144 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  85.145 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  85.146 +     (png_uint_32)png_ptr->zbuf_size);
  85.147 +   png_ptr->zstream.zalloc = png_zalloc;
  85.148 +   png_ptr->zstream.zfree = png_zfree;
  85.149 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  85.150 +
  85.151 +   switch (inflateInit(&png_ptr->zstream))
  85.152 +   {
  85.153 +     case Z_OK: /* Do nothing */ break;
  85.154 +     case Z_MEM_ERROR:
  85.155 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
  85.156 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
  85.157 +     default: png_error(png_ptr, "Unknown zlib error");
  85.158 +   }
  85.159 +
  85.160 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  85.161 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  85.162 +
  85.163 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  85.164 +
  85.165 +#ifdef PNG_SETJMP_SUPPORTED
  85.166 +/* Applications that neglect to set up their own setjmp() and then encounter
  85.167 +   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
  85.168 +   abort instead of returning. */
  85.169 +#ifdef USE_FAR_KEYWORD
  85.170 +   if (setjmp(jmpbuf))
  85.171 +      PNG_ABORT();
  85.172 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  85.173 +#else
  85.174 +   if (setjmp(png_ptr->jmpbuf))
  85.175 +      PNG_ABORT();
  85.176 +#endif
  85.177 +#endif
  85.178 +   return (png_ptr);
  85.179 +}
  85.180 +
  85.181 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  85.182 +/* Initialize PNG structure for reading, and allocate any memory needed.
  85.183 +   This interface is deprecated in favour of the png_create_read_struct(),
  85.184 +   and it will disappear as of libpng-1.3.0. */
  85.185 +#undef png_read_init
  85.186 +void PNGAPI
  85.187 +png_read_init(png_structp png_ptr)
  85.188 +{
  85.189 +   /* We only come here via pre-1.0.7-compiled applications */
  85.190 +   png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  85.191 +}
  85.192 +
  85.193 +void PNGAPI
  85.194 +png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  85.195 +   png_size_t png_struct_size, png_size_t png_info_size)
  85.196 +{
  85.197 +   /* We only come here via pre-1.0.12-compiled applications */
  85.198 +   if (png_ptr == NULL) return;
  85.199 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  85.200 +   if (png_sizeof(png_struct) > png_struct_size ||
  85.201 +      png_sizeof(png_info) > png_info_size)
  85.202 +   {
  85.203 +      char msg[80];
  85.204 +      png_ptr->warning_fn = NULL;
  85.205 +      if (user_png_ver)
  85.206 +      {
  85.207 +        png_snprintf(msg, 80,
  85.208 +           "Application was compiled with png.h from libpng-%.20s",
  85.209 +           user_png_ver);
  85.210 +        png_warning(png_ptr, msg);
  85.211 +      }
  85.212 +      png_snprintf(msg, 80,
  85.213 +         "Application  is  running with png.c from libpng-%.20s",
  85.214 +         png_libpng_ver);
  85.215 +      png_warning(png_ptr, msg);
  85.216 +   }
  85.217 +#endif
  85.218 +   if (png_sizeof(png_struct) > png_struct_size)
  85.219 +     {
  85.220 +       png_ptr->error_fn = NULL;
  85.221 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  85.222 +       png_ptr->flags = 0;
  85.223 +#endif
  85.224 +       png_error(png_ptr,
  85.225 +       "The png struct allocated by the application for reading is too small.");
  85.226 +     }
  85.227 +   if (png_sizeof(png_info) > png_info_size)
  85.228 +     {
  85.229 +       png_ptr->error_fn = NULL;
  85.230 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  85.231 +       png_ptr->flags = 0;
  85.232 +#endif
  85.233 +       png_error(png_ptr,
  85.234 +         "The info struct allocated by application for reading is too small.");
  85.235 +     }
  85.236 +   png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
  85.237 +}
  85.238 +#endif /* PNG_1_0_X || PNG_1_2_X */
  85.239 +
  85.240 +void PNGAPI
  85.241 +png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  85.242 +   png_size_t png_struct_size)
  85.243 +{
  85.244 +#ifdef PNG_SETJMP_SUPPORTED
  85.245 +   jmp_buf tmp_jmp;  /* to save current jump buffer */
  85.246 +#endif
  85.247 +
  85.248 +   int i = 0;
  85.249 +
  85.250 +   png_structp png_ptr=*ptr_ptr;
  85.251 +
  85.252 +   if (png_ptr == NULL) return;
  85.253 +
  85.254 +   do
  85.255 +   {
  85.256 +     if (user_png_ver[i] != png_libpng_ver[i])
  85.257 +     {
  85.258 +#ifdef PNG_LEGACY_SUPPORTED
  85.259 +       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  85.260 +#else
  85.261 +       png_ptr->warning_fn = NULL;
  85.262 +       png_warning(png_ptr,
  85.263 +        "Application uses deprecated png_read_init() and should be recompiled.");
  85.264 +       break;
  85.265 +#endif
  85.266 +     }
  85.267 +   } while (png_libpng_ver[i++]);
  85.268 +
  85.269 +   png_debug(1, "in png_read_init_3\n");
  85.270 +
  85.271 +#ifdef PNG_SETJMP_SUPPORTED
  85.272 +   /* save jump buffer and error functions */
  85.273 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  85.274 +#endif
  85.275 +
  85.276 +   if (png_sizeof(png_struct) > png_struct_size)
  85.277 +   {
  85.278 +      png_destroy_struct(png_ptr);
  85.279 +      *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  85.280 +      png_ptr = *ptr_ptr;
  85.281 +   }
  85.282 +
  85.283 +   /* reset all variables to 0 */
  85.284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
  85.285 +
  85.286 +#ifdef PNG_SETJMP_SUPPORTED
  85.287 +   /* restore jump buffer */
  85.288 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  85.289 +#endif
  85.290 +
  85.291 +   /* added at libpng-1.2.6 */
  85.292 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  85.293 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  85.294 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  85.295 +#endif
  85.296 +
  85.297 +   /* initialize zbuf - compression buffer */
  85.298 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  85.299 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  85.300 +     (png_uint_32)png_ptr->zbuf_size);
  85.301 +   png_ptr->zstream.zalloc = png_zalloc;
  85.302 +   png_ptr->zstream.zfree = png_zfree;
  85.303 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  85.304 +
  85.305 +   switch (inflateInit(&png_ptr->zstream))
  85.306 +   {
  85.307 +     case Z_OK: /* Do nothing */ break;
  85.308 +     case Z_MEM_ERROR:
  85.309 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
  85.310 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
  85.311 +     default: png_error(png_ptr, "Unknown zlib error");
  85.312 +   }
  85.313 +
  85.314 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  85.315 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  85.316 +
  85.317 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  85.318 +}
  85.319 +
  85.320 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.321 +/* Read the information before the actual image data.  This has been
  85.322 + * changed in v0.90 to allow reading a file that already has the magic
  85.323 + * bytes read from the stream.  You can tell libpng how many bytes have
  85.324 + * been read from the beginning of the stream (up to the maximum of 8)
  85.325 + * via png_set_sig_bytes(), and we will only check the remaining bytes
  85.326 + * here.  The application can then have access to the signature bytes we
  85.327 + * read if it is determined that this isn't a valid PNG file.
  85.328 + */
  85.329 +void PNGAPI
  85.330 +png_read_info(png_structp png_ptr, png_infop info_ptr)
  85.331 +{
  85.332 +   if (png_ptr == NULL || info_ptr == NULL) return;
  85.333 +   png_debug(1, "in png_read_info\n");
  85.334 +   /* If we haven't checked all of the PNG signature bytes, do so now. */
  85.335 +   if (png_ptr->sig_bytes < 8)
  85.336 +   {
  85.337 +      png_size_t num_checked = png_ptr->sig_bytes,
  85.338 +                 num_to_check = 8 - num_checked;
  85.339 +
  85.340 +      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  85.341 +      png_ptr->sig_bytes = 8;
  85.342 +
  85.343 +      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  85.344 +      {
  85.345 +         if (num_checked < 4 &&
  85.346 +             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  85.347 +            png_error(png_ptr, "Not a PNG file");
  85.348 +         else
  85.349 +            png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  85.350 +      }
  85.351 +      if (num_checked < 3)
  85.352 +         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  85.353 +   }
  85.354 +
  85.355 +   for (;;)
  85.356 +   {
  85.357 +#ifdef PNG_USE_LOCAL_ARRAYS
  85.358 +      PNG_CONST PNG_IHDR;
  85.359 +      PNG_CONST PNG_IDAT;
  85.360 +      PNG_CONST PNG_IEND;
  85.361 +      PNG_CONST PNG_PLTE;
  85.362 +#if defined(PNG_READ_bKGD_SUPPORTED)
  85.363 +      PNG_CONST PNG_bKGD;
  85.364 +#endif
  85.365 +#if defined(PNG_READ_cHRM_SUPPORTED)
  85.366 +      PNG_CONST PNG_cHRM;
  85.367 +#endif
  85.368 +#if defined(PNG_READ_gAMA_SUPPORTED)
  85.369 +      PNG_CONST PNG_gAMA;
  85.370 +#endif
  85.371 +#if defined(PNG_READ_hIST_SUPPORTED)
  85.372 +      PNG_CONST PNG_hIST;
  85.373 +#endif
  85.374 +#if defined(PNG_READ_iCCP_SUPPORTED)
  85.375 +      PNG_CONST PNG_iCCP;
  85.376 +#endif
  85.377 +#if defined(PNG_READ_iTXt_SUPPORTED)
  85.378 +      PNG_CONST PNG_iTXt;
  85.379 +#endif
  85.380 +#if defined(PNG_READ_oFFs_SUPPORTED)
  85.381 +      PNG_CONST PNG_oFFs;
  85.382 +#endif
  85.383 +#if defined(PNG_READ_pCAL_SUPPORTED)
  85.384 +      PNG_CONST PNG_pCAL;
  85.385 +#endif
  85.386 +#if defined(PNG_READ_pHYs_SUPPORTED)
  85.387 +      PNG_CONST PNG_pHYs;
  85.388 +#endif
  85.389 +#if defined(PNG_READ_sBIT_SUPPORTED)
  85.390 +      PNG_CONST PNG_sBIT;
  85.391 +#endif
  85.392 +#if defined(PNG_READ_sCAL_SUPPORTED)
  85.393 +      PNG_CONST PNG_sCAL;
  85.394 +#endif
  85.395 +#if defined(PNG_READ_sPLT_SUPPORTED)
  85.396 +      PNG_CONST PNG_sPLT;
  85.397 +#endif
  85.398 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.399 +      PNG_CONST PNG_sRGB;
  85.400 +#endif
  85.401 +#if defined(PNG_READ_tEXt_SUPPORTED)
  85.402 +      PNG_CONST PNG_tEXt;
  85.403 +#endif
  85.404 +#if defined(PNG_READ_tIME_SUPPORTED)
  85.405 +      PNG_CONST PNG_tIME;
  85.406 +#endif
  85.407 +#if defined(PNG_READ_tRNS_SUPPORTED)
  85.408 +      PNG_CONST PNG_tRNS;
  85.409 +#endif
  85.410 +#if defined(PNG_READ_zTXt_SUPPORTED)
  85.411 +      PNG_CONST PNG_zTXt;
  85.412 +#endif
  85.413 +#endif /* PNG_USE_LOCAL_ARRAYS */
  85.414 +      png_uint_32 length = png_read_chunk_header(png_ptr);
  85.415 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  85.416 +
  85.417 +      /* This should be a binary subdivision search or a hash for
  85.418 +       * matching the chunk name rather than a linear search.
  85.419 +       */
  85.420 +      if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.421 +        if (png_ptr->mode & PNG_AFTER_IDAT)
  85.422 +          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  85.423 +
  85.424 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
  85.425 +         png_handle_IHDR(png_ptr, info_ptr, length);
  85.426 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
  85.427 +         png_handle_IEND(png_ptr, info_ptr, length);
  85.428 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  85.429 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
  85.430 +      {
  85.431 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.432 +            png_ptr->mode |= PNG_HAVE_IDAT;
  85.433 +         png_handle_unknown(png_ptr, info_ptr, length);
  85.434 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
  85.435 +            png_ptr->mode |= PNG_HAVE_PLTE;
  85.436 +         else if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.437 +         {
  85.438 +            if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.439 +               png_error(png_ptr, "Missing IHDR before IDAT");
  85.440 +            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  85.441 +                     !(png_ptr->mode & PNG_HAVE_PLTE))
  85.442 +               png_error(png_ptr, "Missing PLTE before IDAT");
  85.443 +            break;
  85.444 +         }
  85.445 +      }
  85.446 +#endif
  85.447 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
  85.448 +         png_handle_PLTE(png_ptr, info_ptr, length);
  85.449 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.450 +      {
  85.451 +         if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.452 +            png_error(png_ptr, "Missing IHDR before IDAT");
  85.453 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  85.454 +                  !(png_ptr->mode & PNG_HAVE_PLTE))
  85.455 +            png_error(png_ptr, "Missing PLTE before IDAT");
  85.456 +
  85.457 +         png_ptr->idat_size = length;
  85.458 +         png_ptr->mode |= PNG_HAVE_IDAT;
  85.459 +         break;
  85.460 +      }
  85.461 +#if defined(PNG_READ_bKGD_SUPPORTED)
  85.462 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
  85.463 +         png_handle_bKGD(png_ptr, info_ptr, length);
  85.464 +#endif
  85.465 +#if defined(PNG_READ_cHRM_SUPPORTED)
  85.466 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
  85.467 +         png_handle_cHRM(png_ptr, info_ptr, length);
  85.468 +#endif
  85.469 +#if defined(PNG_READ_gAMA_SUPPORTED)
  85.470 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
  85.471 +         png_handle_gAMA(png_ptr, info_ptr, length);
  85.472 +#endif
  85.473 +#if defined(PNG_READ_hIST_SUPPORTED)
  85.474 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
  85.475 +         png_handle_hIST(png_ptr, info_ptr, length);
  85.476 +#endif
  85.477 +#if defined(PNG_READ_oFFs_SUPPORTED)
  85.478 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
  85.479 +         png_handle_oFFs(png_ptr, info_ptr, length);
  85.480 +#endif
  85.481 +#if defined(PNG_READ_pCAL_SUPPORTED)
  85.482 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
  85.483 +         png_handle_pCAL(png_ptr, info_ptr, length);
  85.484 +#endif
  85.485 +#if defined(PNG_READ_sCAL_SUPPORTED)
  85.486 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
  85.487 +         png_handle_sCAL(png_ptr, info_ptr, length);
  85.488 +#endif
  85.489 +#if defined(PNG_READ_pHYs_SUPPORTED)
  85.490 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
  85.491 +         png_handle_pHYs(png_ptr, info_ptr, length);
  85.492 +#endif
  85.493 +#if defined(PNG_READ_sBIT_SUPPORTED)
  85.494 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
  85.495 +         png_handle_sBIT(png_ptr, info_ptr, length);
  85.496 +#endif
  85.497 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.498 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
  85.499 +         png_handle_sRGB(png_ptr, info_ptr, length);
  85.500 +#endif
  85.501 +#if defined(PNG_READ_iCCP_SUPPORTED)
  85.502 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
  85.503 +         png_handle_iCCP(png_ptr, info_ptr, length);
  85.504 +#endif
  85.505 +#if defined(PNG_READ_sPLT_SUPPORTED)
  85.506 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
  85.507 +         png_handle_sPLT(png_ptr, info_ptr, length);
  85.508 +#endif
  85.509 +#if defined(PNG_READ_tEXt_SUPPORTED)
  85.510 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
  85.511 +         png_handle_tEXt(png_ptr, info_ptr, length);
  85.512 +#endif
  85.513 +#if defined(PNG_READ_tIME_SUPPORTED)
  85.514 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
  85.515 +         png_handle_tIME(png_ptr, info_ptr, length);
  85.516 +#endif
  85.517 +#if defined(PNG_READ_tRNS_SUPPORTED)
  85.518 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
  85.519 +         png_handle_tRNS(png_ptr, info_ptr, length);
  85.520 +#endif
  85.521 +#if defined(PNG_READ_zTXt_SUPPORTED)
  85.522 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
  85.523 +         png_handle_zTXt(png_ptr, info_ptr, length);
  85.524 +#endif
  85.525 +#if defined(PNG_READ_iTXt_SUPPORTED)
  85.526 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
  85.527 +         png_handle_iTXt(png_ptr, info_ptr, length);
  85.528 +#endif
  85.529 +      else
  85.530 +         png_handle_unknown(png_ptr, info_ptr, length);
  85.531 +   }
  85.532 +}
  85.533 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  85.534 +
  85.535 +/* optional call to update the users info_ptr structure */
  85.536 +void PNGAPI
  85.537 +png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  85.538 +{
  85.539 +   png_debug(1, "in png_read_update_info\n");
  85.540 +   if (png_ptr == NULL) return;
  85.541 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  85.542 +      png_read_start_row(png_ptr);
  85.543 +   else
  85.544 +      png_warning(png_ptr,
  85.545 +      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
  85.546 +   png_read_transform_info(png_ptr, info_ptr);
  85.547 +}
  85.548 +
  85.549 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.550 +/* Initialize palette, background, etc, after transformations
  85.551 + * are set, but before any reading takes place.  This allows
  85.552 + * the user to obtain a gamma-corrected palette, for example.
  85.553 + * If the user doesn't call this, we will do it ourselves.
  85.554 + */
  85.555 +void PNGAPI
  85.556 +png_start_read_image(png_structp png_ptr)
  85.557 +{
  85.558 +   png_debug(1, "in png_start_read_image\n");
  85.559 +   if (png_ptr == NULL) return;
  85.560 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  85.561 +      png_read_start_row(png_ptr);
  85.562 +}
  85.563 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  85.564 +
  85.565 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.566 +void PNGAPI
  85.567 +png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  85.568 +{
  85.569 +#ifdef PNG_USE_LOCAL_ARRAYS
  85.570 +   PNG_CONST PNG_IDAT;
  85.571 +   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  85.572 +      0xff};
  85.573 +   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  85.574 +#endif
  85.575 +   int ret;
  85.576 +   if (png_ptr == NULL) return;
  85.577 +   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
  85.578 +      png_ptr->row_number, png_ptr->pass);
  85.579 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  85.580 +      png_read_start_row(png_ptr);
  85.581 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  85.582 +   {
  85.583 +   /* check for transforms that have been set but were defined out */
  85.584 +#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  85.585 +   if (png_ptr->transformations & PNG_INVERT_MONO)
  85.586 +      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
  85.587 +#endif
  85.588 +#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  85.589 +   if (png_ptr->transformations & PNG_FILLER)
  85.590 +      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
  85.591 +#endif
  85.592 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
  85.593 +   if (png_ptr->transformations & PNG_PACKSWAP)
  85.594 +      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
  85.595 +#endif
  85.596 +#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  85.597 +   if (png_ptr->transformations & PNG_PACK)
  85.598 +      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
  85.599 +#endif
  85.600 +#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  85.601 +   if (png_ptr->transformations & PNG_SHIFT)
  85.602 +      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
  85.603 +#endif
  85.604 +#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  85.605 +   if (png_ptr->transformations & PNG_BGR)
  85.606 +      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
  85.607 +#endif
  85.608 +#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  85.609 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
  85.610 +      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
  85.611 +#endif
  85.612 +   }
  85.613 +
  85.614 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  85.615 +   /* if interlaced and we do not need a new row, combine row and return */
  85.616 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  85.617 +   {
  85.618 +      switch (png_ptr->pass)
  85.619 +      {
  85.620 +         case 0:
  85.621 +            if (png_ptr->row_number & 0x07)
  85.622 +            {
  85.623 +               if (dsp_row != NULL)
  85.624 +                  png_combine_row(png_ptr, dsp_row,
  85.625 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.626 +               png_read_finish_row(png_ptr);
  85.627 +               return;
  85.628 +            }
  85.629 +            break;
  85.630 +         case 1:
  85.631 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  85.632 +            {
  85.633 +               if (dsp_row != NULL)
  85.634 +                  png_combine_row(png_ptr, dsp_row,
  85.635 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.636 +               png_read_finish_row(png_ptr);
  85.637 +               return;
  85.638 +            }
  85.639 +            break;
  85.640 +         case 2:
  85.641 +            if ((png_ptr->row_number & 0x07) != 4)
  85.642 +            {
  85.643 +               if (dsp_row != NULL && (png_ptr->row_number & 4))
  85.644 +                  png_combine_row(png_ptr, dsp_row,
  85.645 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.646 +               png_read_finish_row(png_ptr);
  85.647 +               return;
  85.648 +            }
  85.649 +            break;
  85.650 +         case 3:
  85.651 +            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  85.652 +            {
  85.653 +               if (dsp_row != NULL)
  85.654 +                  png_combine_row(png_ptr, dsp_row,
  85.655 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.656 +               png_read_finish_row(png_ptr);
  85.657 +               return;
  85.658 +            }
  85.659 +            break;
  85.660 +         case 4:
  85.661 +            if ((png_ptr->row_number & 3) != 2)
  85.662 +            {
  85.663 +               if (dsp_row != NULL && (png_ptr->row_number & 2))
  85.664 +                  png_combine_row(png_ptr, dsp_row,
  85.665 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.666 +               png_read_finish_row(png_ptr);
  85.667 +               return;
  85.668 +            }
  85.669 +            break;
  85.670 +         case 5:
  85.671 +            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  85.672 +            {
  85.673 +               if (dsp_row != NULL)
  85.674 +                  png_combine_row(png_ptr, dsp_row,
  85.675 +                     png_pass_dsp_mask[png_ptr->pass]);
  85.676 +               png_read_finish_row(png_ptr);
  85.677 +               return;
  85.678 +            }
  85.679 +            break;
  85.680 +         case 6:
  85.681 +            if (!(png_ptr->row_number & 1))
  85.682 +            {
  85.683 +               png_read_finish_row(png_ptr);
  85.684 +               return;
  85.685 +            }
  85.686 +            break;
  85.687 +      }
  85.688 +   }
  85.689 +#endif
  85.690 +
  85.691 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
  85.692 +      png_error(png_ptr, "Invalid attempt to read row data");
  85.693 +
  85.694 +   png_ptr->zstream.next_out = png_ptr->row_buf;
  85.695 +   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  85.696 +   do
  85.697 +   {
  85.698 +      if (!(png_ptr->zstream.avail_in))
  85.699 +      {
  85.700 +         while (!png_ptr->idat_size)
  85.701 +         {
  85.702 +            png_crc_finish(png_ptr, 0);
  85.703 +
  85.704 +            png_ptr->idat_size = png_read_chunk_header(png_ptr);
  85.705 +            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  85.706 +               png_error(png_ptr, "Not enough image data");
  85.707 +         }
  85.708 +         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  85.709 +         png_ptr->zstream.next_in = png_ptr->zbuf;
  85.710 +         if (png_ptr->zbuf_size > png_ptr->idat_size)
  85.711 +            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  85.712 +         png_crc_read(png_ptr, png_ptr->zbuf,
  85.713 +            (png_size_t)png_ptr->zstream.avail_in);
  85.714 +         png_ptr->idat_size -= png_ptr->zstream.avail_in;
  85.715 +      }
  85.716 +      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  85.717 +      if (ret == Z_STREAM_END)
  85.718 +      {
  85.719 +         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  85.720 +            png_ptr->idat_size)
  85.721 +            png_error(png_ptr, "Extra compressed data");
  85.722 +         png_ptr->mode |= PNG_AFTER_IDAT;
  85.723 +         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  85.724 +         break;
  85.725 +      }
  85.726 +      if (ret != Z_OK)
  85.727 +         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  85.728 +                   "Decompression error");
  85.729 +
  85.730 +   } while (png_ptr->zstream.avail_out);
  85.731 +
  85.732 +   png_ptr->row_info.color_type = png_ptr->color_type;
  85.733 +   png_ptr->row_info.width = png_ptr->iwidth;
  85.734 +   png_ptr->row_info.channels = png_ptr->channels;
  85.735 +   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  85.736 +   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  85.737 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  85.738 +       png_ptr->row_info.width);
  85.739 +
  85.740 +   if (png_ptr->row_buf[0])
  85.741 +   png_read_filter_row(png_ptr, &(png_ptr->row_info),
  85.742 +      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  85.743 +      (int)(png_ptr->row_buf[0]));
  85.744 +
  85.745 +   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  85.746 +      png_ptr->rowbytes + 1);
  85.747 +
  85.748 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  85.749 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  85.750 +      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  85.751 +   {
  85.752 +      /* Intrapixel differencing */
  85.753 +      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  85.754 +   }
  85.755 +#endif
  85.756 +
  85.757 +
  85.758 +   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
  85.759 +      png_do_read_transformations(png_ptr);
  85.760 +
  85.761 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  85.762 +   /* blow up interlaced rows to full size */
  85.763 +   if (png_ptr->interlaced &&
  85.764 +      (png_ptr->transformations & PNG_INTERLACE))
  85.765 +   {
  85.766 +      if (png_ptr->pass < 6)
  85.767 +/*       old interface (pre-1.0.9):
  85.768 +         png_do_read_interlace(&(png_ptr->row_info),
  85.769 +            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  85.770 + */
  85.771 +         png_do_read_interlace(png_ptr);
  85.772 +
  85.773 +      if (dsp_row != NULL)
  85.774 +         png_combine_row(png_ptr, dsp_row,
  85.775 +            png_pass_dsp_mask[png_ptr->pass]);
  85.776 +      if (row != NULL)
  85.777 +         png_combine_row(png_ptr, row,
  85.778 +            png_pass_mask[png_ptr->pass]);
  85.779 +   }
  85.780 +   else
  85.781 +#endif
  85.782 +   {
  85.783 +      if (row != NULL)
  85.784 +         png_combine_row(png_ptr, row, 0xff);
  85.785 +      if (dsp_row != NULL)
  85.786 +         png_combine_row(png_ptr, dsp_row, 0xff);
  85.787 +   }
  85.788 +   png_read_finish_row(png_ptr);
  85.789 +
  85.790 +   if (png_ptr->read_row_fn != NULL)
  85.791 +      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  85.792 +}
  85.793 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  85.794 +
  85.795 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.796 +/* Read one or more rows of image data.  If the image is interlaced,
  85.797 + * and png_set_interlace_handling() has been called, the rows need to
  85.798 + * contain the contents of the rows from the previous pass.  If the
  85.799 + * image has alpha or transparency, and png_handle_alpha()[*] has been
  85.800 + * called, the rows contents must be initialized to the contents of the
  85.801 + * screen.
  85.802 + *
  85.803 + * "row" holds the actual image, and pixels are placed in it
  85.804 + * as they arrive.  If the image is displayed after each pass, it will
  85.805 + * appear to "sparkle" in.  "display_row" can be used to display a
  85.806 + * "chunky" progressive image, with finer detail added as it becomes
  85.807 + * available.  If you do not want this "chunky" display, you may pass
  85.808 + * NULL for display_row.  If you do not want the sparkle display, and
  85.809 + * you have not called png_handle_alpha(), you may pass NULL for rows.
  85.810 + * If you have called png_handle_alpha(), and the image has either an
  85.811 + * alpha channel or a transparency chunk, you must provide a buffer for
  85.812 + * rows.  In this case, you do not have to provide a display_row buffer
  85.813 + * also, but you may.  If the image is not interlaced, or if you have
  85.814 + * not called png_set_interlace_handling(), the display_row buffer will
  85.815 + * be ignored, so pass NULL to it.
  85.816 + *
  85.817 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  85.818 + */
  85.819 +
  85.820 +void PNGAPI
  85.821 +png_read_rows(png_structp png_ptr, png_bytepp row,
  85.822 +   png_bytepp display_row, png_uint_32 num_rows)
  85.823 +{
  85.824 +   png_uint_32 i;
  85.825 +   png_bytepp rp;
  85.826 +   png_bytepp dp;
  85.827 +
  85.828 +   png_debug(1, "in png_read_rows\n");
  85.829 +   if (png_ptr == NULL) return;
  85.830 +   rp = row;
  85.831 +   dp = display_row;
  85.832 +   if (rp != NULL && dp != NULL)
  85.833 +      for (i = 0; i < num_rows; i++)
  85.834 +      {
  85.835 +         png_bytep rptr = *rp++;
  85.836 +         png_bytep dptr = *dp++;
  85.837 +
  85.838 +         png_read_row(png_ptr, rptr, dptr);
  85.839 +      }
  85.840 +   else if (rp != NULL)
  85.841 +      for (i = 0; i < num_rows; i++)
  85.842 +      {
  85.843 +         png_bytep rptr = *rp;
  85.844 +         png_read_row(png_ptr, rptr, png_bytep_NULL);
  85.845 +         rp++;
  85.846 +      }
  85.847 +   else if (dp != NULL)
  85.848 +      for (i = 0; i < num_rows; i++)
  85.849 +      {
  85.850 +         png_bytep dptr = *dp;
  85.851 +         png_read_row(png_ptr, png_bytep_NULL, dptr);
  85.852 +         dp++;
  85.853 +      }
  85.854 +}
  85.855 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  85.856 +
  85.857 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.858 +/* Read the entire image.  If the image has an alpha channel or a tRNS
  85.859 + * chunk, and you have called png_handle_alpha()[*], you will need to
  85.860 + * initialize the image to the current image that PNG will be overlaying.
  85.861 + * We set the num_rows again here, in case it was incorrectly set in
  85.862 + * png_read_start_row() by a call to png_read_update_info() or
  85.863 + * png_start_read_image() if png_set_interlace_handling() wasn't called
  85.864 + * prior to either of these functions like it should have been.  You can
  85.865 + * only call this function once.  If you desire to have an image for
  85.866 + * each pass of a interlaced image, use png_read_rows() instead.
  85.867 + *
  85.868 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  85.869 + */
  85.870 +void PNGAPI
  85.871 +png_read_image(png_structp png_ptr, png_bytepp image)
  85.872 +{
  85.873 +   png_uint_32 i, image_height;
  85.874 +   int pass, j;
  85.875 +   png_bytepp rp;
  85.876 +
  85.877 +   png_debug(1, "in png_read_image\n");
  85.878 +   if (png_ptr == NULL) return;
  85.879 +
  85.880 +#ifdef PNG_READ_INTERLACING_SUPPORTED
  85.881 +   pass = png_set_interlace_handling(png_ptr);
  85.882 +#else
  85.883 +   if (png_ptr->interlaced)
  85.884 +      png_error(png_ptr,
  85.885 +        "Cannot read interlaced image -- interlace handler disabled.");
  85.886 +   pass = 1;
  85.887 +#endif
  85.888 +
  85.889 +
  85.890 +   image_height=png_ptr->height;
  85.891 +   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
  85.892 +
  85.893 +   for (j = 0; j < pass; j++)
  85.894 +   {
  85.895 +      rp = image;
  85.896 +      for (i = 0; i < image_height; i++)
  85.897 +      {
  85.898 +         png_read_row(png_ptr, *rp, png_bytep_NULL);
  85.899 +         rp++;
  85.900 +      }
  85.901 +   }
  85.902 +}
  85.903 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  85.904 +
  85.905 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  85.906 +/* Read the end of the PNG file.  Will not read past the end of the
  85.907 + * file, will verify the end is accurate, and will read any comments
  85.908 + * or time information at the end of the file, if info is not NULL.
  85.909 + */
  85.910 +void PNGAPI
  85.911 +png_read_end(png_structp png_ptr, png_infop info_ptr)
  85.912 +{
  85.913 +   png_debug(1, "in png_read_end\n");
  85.914 +   if (png_ptr == NULL) return;
  85.915 +   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  85.916 +
  85.917 +   do
  85.918 +   {
  85.919 +#ifdef PNG_USE_LOCAL_ARRAYS
  85.920 +      PNG_CONST PNG_IHDR;
  85.921 +      PNG_CONST PNG_IDAT;
  85.922 +      PNG_CONST PNG_IEND;
  85.923 +      PNG_CONST PNG_PLTE;
  85.924 +#if defined(PNG_READ_bKGD_SUPPORTED)
  85.925 +      PNG_CONST PNG_bKGD;
  85.926 +#endif
  85.927 +#if defined(PNG_READ_cHRM_SUPPORTED)
  85.928 +      PNG_CONST PNG_cHRM;
  85.929 +#endif
  85.930 +#if defined(PNG_READ_gAMA_SUPPORTED)
  85.931 +      PNG_CONST PNG_gAMA;
  85.932 +#endif
  85.933 +#if defined(PNG_READ_hIST_SUPPORTED)
  85.934 +      PNG_CONST PNG_hIST;
  85.935 +#endif
  85.936 +#if defined(PNG_READ_iCCP_SUPPORTED)
  85.937 +      PNG_CONST PNG_iCCP;
  85.938 +#endif
  85.939 +#if defined(PNG_READ_iTXt_SUPPORTED)
  85.940 +      PNG_CONST PNG_iTXt;
  85.941 +#endif
  85.942 +#if defined(PNG_READ_oFFs_SUPPORTED)
  85.943 +      PNG_CONST PNG_oFFs;
  85.944 +#endif
  85.945 +#if defined(PNG_READ_pCAL_SUPPORTED)
  85.946 +      PNG_CONST PNG_pCAL;
  85.947 +#endif
  85.948 +#if defined(PNG_READ_pHYs_SUPPORTED)
  85.949 +      PNG_CONST PNG_pHYs;
  85.950 +#endif
  85.951 +#if defined(PNG_READ_sBIT_SUPPORTED)
  85.952 +      PNG_CONST PNG_sBIT;
  85.953 +#endif
  85.954 +#if defined(PNG_READ_sCAL_SUPPORTED)
  85.955 +      PNG_CONST PNG_sCAL;
  85.956 +#endif
  85.957 +#if defined(PNG_READ_sPLT_SUPPORTED)
  85.958 +      PNG_CONST PNG_sPLT;
  85.959 +#endif
  85.960 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.961 +      PNG_CONST PNG_sRGB;
  85.962 +#endif
  85.963 +#if defined(PNG_READ_tEXt_SUPPORTED)
  85.964 +      PNG_CONST PNG_tEXt;
  85.965 +#endif
  85.966 +#if defined(PNG_READ_tIME_SUPPORTED)
  85.967 +      PNG_CONST PNG_tIME;
  85.968 +#endif
  85.969 +#if defined(PNG_READ_tRNS_SUPPORTED)
  85.970 +      PNG_CONST PNG_tRNS;
  85.971 +#endif
  85.972 +#if defined(PNG_READ_zTXt_SUPPORTED)
  85.973 +      PNG_CONST PNG_zTXt;
  85.974 +#endif
  85.975 +#endif /* PNG_USE_LOCAL_ARRAYS */
  85.976 +      png_uint_32 length = png_read_chunk_header(png_ptr);
  85.977 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  85.978 +
  85.979 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
  85.980 +         png_handle_IHDR(png_ptr, info_ptr, length);
  85.981 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
  85.982 +         png_handle_IEND(png_ptr, info_ptr, length);
  85.983 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  85.984 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
  85.985 +      {
  85.986 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.987 +         {
  85.988 +            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  85.989 +               png_error(png_ptr, "Too many IDAT's found");
  85.990 +         }
  85.991 +         png_handle_unknown(png_ptr, info_ptr, length);
  85.992 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
  85.993 +            png_ptr->mode |= PNG_HAVE_PLTE;
  85.994 +      }
  85.995 +#endif
  85.996 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
  85.997 +      {
  85.998 +         /* Zero length IDATs are legal after the last IDAT has been
  85.999 +          * read, but not after other chunks have been read.
 85.1000 +          */
 85.1001 +         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
 85.1002 +            png_error(png_ptr, "Too many IDAT's found");
 85.1003 +         png_crc_finish(png_ptr, length);
 85.1004 +      }
 85.1005 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
 85.1006 +         png_handle_PLTE(png_ptr, info_ptr, length);
 85.1007 +#if defined(PNG_READ_bKGD_SUPPORTED)
 85.1008 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
 85.1009 +         png_handle_bKGD(png_ptr, info_ptr, length);
 85.1010 +#endif
 85.1011 +#if defined(PNG_READ_cHRM_SUPPORTED)
 85.1012 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
 85.1013 +         png_handle_cHRM(png_ptr, info_ptr, length);
 85.1014 +#endif
 85.1015 +#if defined(PNG_READ_gAMA_SUPPORTED)
 85.1016 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
 85.1017 +         png_handle_gAMA(png_ptr, info_ptr, length);
 85.1018 +#endif
 85.1019 +#if defined(PNG_READ_hIST_SUPPORTED)
 85.1020 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
 85.1021 +         png_handle_hIST(png_ptr, info_ptr, length);
 85.1022 +#endif
 85.1023 +#if defined(PNG_READ_oFFs_SUPPORTED)
 85.1024 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
 85.1025 +         png_handle_oFFs(png_ptr, info_ptr, length);
 85.1026 +#endif
 85.1027 +#if defined(PNG_READ_pCAL_SUPPORTED)
 85.1028 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
 85.1029 +         png_handle_pCAL(png_ptr, info_ptr, length);
 85.1030 +#endif
 85.1031 +#if defined(PNG_READ_sCAL_SUPPORTED)
 85.1032 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
 85.1033 +         png_handle_sCAL(png_ptr, info_ptr, length);
 85.1034 +#endif
 85.1035 +#if defined(PNG_READ_pHYs_SUPPORTED)
 85.1036 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
 85.1037 +         png_handle_pHYs(png_ptr, info_ptr, length);
 85.1038 +#endif
 85.1039 +#if defined(PNG_READ_sBIT_SUPPORTED)
 85.1040 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
 85.1041 +         png_handle_sBIT(png_ptr, info_ptr, length);
 85.1042 +#endif
 85.1043 +#if defined(PNG_READ_sRGB_SUPPORTED)
 85.1044 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
 85.1045 +         png_handle_sRGB(png_ptr, info_ptr, length);
 85.1046 +#endif
 85.1047 +#if defined(PNG_READ_iCCP_SUPPORTED)
 85.1048 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
 85.1049 +         png_handle_iCCP(png_ptr, info_ptr, length);
 85.1050 +#endif
 85.1051 +#if defined(PNG_READ_sPLT_SUPPORTED)
 85.1052 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
 85.1053 +         png_handle_sPLT(png_ptr, info_ptr, length);
 85.1054 +#endif
 85.1055 +#if defined(PNG_READ_tEXt_SUPPORTED)
 85.1056 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
 85.1057 +         png_handle_tEXt(png_ptr, info_ptr, length);
 85.1058 +#endif
 85.1059 +#if defined(PNG_READ_tIME_SUPPORTED)
 85.1060 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
 85.1061 +         png_handle_tIME(png_ptr, info_ptr, length);
 85.1062 +#endif
 85.1063 +#if defined(PNG_READ_tRNS_SUPPORTED)
 85.1064 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
 85.1065 +         png_handle_tRNS(png_ptr, info_ptr, length);
 85.1066 +#endif
 85.1067 +#if defined(PNG_READ_zTXt_SUPPORTED)
 85.1068 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
 85.1069 +         png_handle_zTXt(png_ptr, info_ptr, length);
 85.1070 +#endif
 85.1071 +#if defined(PNG_READ_iTXt_SUPPORTED)
 85.1072 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
 85.1073 +         png_handle_iTXt(png_ptr, info_ptr, length);
 85.1074 +#endif
 85.1075 +      else
 85.1076 +         png_handle_unknown(png_ptr, info_ptr, length);
 85.1077 +   } while (!(png_ptr->mode & PNG_HAVE_IEND));
 85.1078 +}
 85.1079 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
 85.1080 +
 85.1081 +/* free all memory used by the read */
 85.1082 +void PNGAPI
 85.1083 +png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
 85.1084 +   png_infopp end_info_ptr_ptr)
 85.1085 +{
 85.1086 +   png_structp png_ptr = NULL;
 85.1087 +   png_infop info_ptr = NULL, end_info_ptr = NULL;
 85.1088 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1089 +   png_free_ptr free_fn = NULL;
 85.1090 +   png_voidp mem_ptr = NULL;
 85.1091 +#endif
 85.1092 +
 85.1093 +   png_debug(1, "in png_destroy_read_struct\n");
 85.1094 +   if (png_ptr_ptr != NULL)
 85.1095 +      png_ptr = *png_ptr_ptr;
 85.1096 +   if (png_ptr == NULL)
 85.1097 +      return;
 85.1098 +
 85.1099 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1100 +   free_fn = png_ptr->free_fn;
 85.1101 +   mem_ptr = png_ptr->mem_ptr;
 85.1102 +#endif
 85.1103 +
 85.1104 +   if (info_ptr_ptr != NULL)
 85.1105 +      info_ptr = *info_ptr_ptr;
 85.1106 +
 85.1107 +   if (end_info_ptr_ptr != NULL)
 85.1108 +      end_info_ptr = *end_info_ptr_ptr;
 85.1109 +
 85.1110 +   png_read_destroy(png_ptr, info_ptr, end_info_ptr);
 85.1111 +
 85.1112 +   if (info_ptr != NULL)
 85.1113 +   {
 85.1114 +#if defined(PNG_TEXT_SUPPORTED)
 85.1115 +      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
 85.1116 +#endif
 85.1117 +
 85.1118 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1119 +      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
 85.1120 +          (png_voidp)mem_ptr);
 85.1121 +#else
 85.1122 +      png_destroy_struct((png_voidp)info_ptr);
 85.1123 +#endif
 85.1124 +      *info_ptr_ptr = NULL;
 85.1125 +   }
 85.1126 +
 85.1127 +   if (end_info_ptr != NULL)
 85.1128 +   {
 85.1129 +#if defined(PNG_READ_TEXT_SUPPORTED)
 85.1130 +      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
 85.1131 +#endif
 85.1132 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1133 +      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
 85.1134 +         (png_voidp)mem_ptr);
 85.1135 +#else
 85.1136 +      png_destroy_struct((png_voidp)end_info_ptr);
 85.1137 +#endif
 85.1138 +      *end_info_ptr_ptr = NULL;
 85.1139 +   }
 85.1140 +
 85.1141 +   if (png_ptr != NULL)
 85.1142 +   {
 85.1143 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1144 +      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
 85.1145 +          (png_voidp)mem_ptr);
 85.1146 +#else
 85.1147 +      png_destroy_struct((png_voidp)png_ptr);
 85.1148 +#endif
 85.1149 +      *png_ptr_ptr = NULL;
 85.1150 +   }
 85.1151 +}
 85.1152 +
 85.1153 +/* free all memory used by the read (old method) */
 85.1154 +void /* PRIVATE */
 85.1155 +png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
 85.1156 +{
 85.1157 +#ifdef PNG_SETJMP_SUPPORTED
 85.1158 +   jmp_buf tmp_jmp;
 85.1159 +#endif
 85.1160 +   png_error_ptr error_fn;
 85.1161 +   png_error_ptr warning_fn;
 85.1162 +   png_voidp error_ptr;
 85.1163 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1164 +   png_free_ptr free_fn;
 85.1165 +#endif
 85.1166 +
 85.1167 +   png_debug(1, "in png_read_destroy\n");
 85.1168 +   if (info_ptr != NULL)
 85.1169 +      png_info_destroy(png_ptr, info_ptr);
 85.1170 +
 85.1171 +   if (end_info_ptr != NULL)
 85.1172 +      png_info_destroy(png_ptr, end_info_ptr);
 85.1173 +
 85.1174 +   png_free(png_ptr, png_ptr->zbuf);
 85.1175 +   png_free(png_ptr, png_ptr->big_row_buf);
 85.1176 +   png_free(png_ptr, png_ptr->prev_row);
 85.1177 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1178 +#if defined(PNG_READ_DITHER_SUPPORTED)
 85.1179 +   png_free(png_ptr, png_ptr->palette_lookup);
 85.1180 +   png_free(png_ptr, png_ptr->dither_index);
 85.1181 +#endif
 85.1182 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 85.1183 +   png_free(png_ptr, png_ptr->gamma_table);
 85.1184 +#endif
 85.1185 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 85.1186 +   png_free(png_ptr, png_ptr->gamma_from_1);
 85.1187 +   png_free(png_ptr, png_ptr->gamma_to_1);
 85.1188 +#endif
 85.1189 +#ifdef PNG_FREE_ME_SUPPORTED
 85.1190 +   if (png_ptr->free_me & PNG_FREE_PLTE)
 85.1191 +      png_zfree(png_ptr, png_ptr->palette);
 85.1192 +   png_ptr->free_me &= ~PNG_FREE_PLTE;
 85.1193 +#else
 85.1194 +   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
 85.1195 +      png_zfree(png_ptr, png_ptr->palette);
 85.1196 +   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
 85.1197 +#endif
 85.1198 +#if defined(PNG_tRNS_SUPPORTED) || \
 85.1199 +    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 85.1200 +#ifdef PNG_FREE_ME_SUPPORTED
 85.1201 +   if (png_ptr->free_me & PNG_FREE_TRNS)
 85.1202 +      png_free(png_ptr, png_ptr->trans);
 85.1203 +   png_ptr->free_me &= ~PNG_FREE_TRNS;
 85.1204 +#else
 85.1205 +   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
 85.1206 +      png_free(png_ptr, png_ptr->trans);
 85.1207 +   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
 85.1208 +#endif
 85.1209 +#endif
 85.1210 +#if defined(PNG_READ_hIST_SUPPORTED)
 85.1211 +#ifdef PNG_FREE_ME_SUPPORTED
 85.1212 +   if (png_ptr->free_me & PNG_FREE_HIST)
 85.1213 +      png_free(png_ptr, png_ptr->hist);
 85.1214 +   png_ptr->free_me &= ~PNG_FREE_HIST;
 85.1215 +#else
 85.1216 +   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
 85.1217 +      png_free(png_ptr, png_ptr->hist);
 85.1218 +   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
 85.1219 +#endif
 85.1220 +#endif
 85.1221 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 85.1222 +   if (png_ptr->gamma_16_table != NULL)
 85.1223 +   {
 85.1224 +      int i;
 85.1225 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 85.1226 +      for (i = 0; i < istop; i++)
 85.1227 +      {
 85.1228 +         png_free(png_ptr, png_ptr->gamma_16_table[i]);
 85.1229 +      }
 85.1230 +   png_free(png_ptr, png_ptr->gamma_16_table);
 85.1231 +   }
 85.1232 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 85.1233 +   if (png_ptr->gamma_16_from_1 != NULL)
 85.1234 +   {
 85.1235 +      int i;
 85.1236 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 85.1237 +      for (i = 0; i < istop; i++)
 85.1238 +      {
 85.1239 +         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
 85.1240 +      }
 85.1241 +   png_free(png_ptr, png_ptr->gamma_16_from_1);
 85.1242 +   }
 85.1243 +   if (png_ptr->gamma_16_to_1 != NULL)
 85.1244 +   {
 85.1245 +      int i;
 85.1246 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 85.1247 +      for (i = 0; i < istop; i++)
 85.1248 +      {
 85.1249 +         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
 85.1250 +      }
 85.1251 +   png_free(png_ptr, png_ptr->gamma_16_to_1);
 85.1252 +   }
 85.1253 +#endif
 85.1254 +#endif
 85.1255 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 85.1256 +   png_free(png_ptr, png_ptr->time_buffer);
 85.1257 +#endif
 85.1258 +
 85.1259 +   inflateEnd(&png_ptr->zstream);
 85.1260 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 85.1261 +   png_free(png_ptr, png_ptr->save_buffer);
 85.1262 +#endif
 85.1263 +
 85.1264 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 85.1265 +#ifdef PNG_TEXT_SUPPORTED
 85.1266 +   png_free(png_ptr, png_ptr->current_text);
 85.1267 +#endif /* PNG_TEXT_SUPPORTED */
 85.1268 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 85.1269 +
 85.1270 +   /* Save the important info out of the png_struct, in case it is
 85.1271 +    * being used again.
 85.1272 +    */
 85.1273 +#ifdef PNG_SETJMP_SUPPORTED
 85.1274 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
 85.1275 +#endif
 85.1276 +
 85.1277 +   error_fn = png_ptr->error_fn;
 85.1278 +   warning_fn = png_ptr->warning_fn;
 85.1279 +   error_ptr = png_ptr->error_ptr;
 85.1280 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1281 +   free_fn = png_ptr->free_fn;
 85.1282 +#endif
 85.1283 +
 85.1284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
 85.1285 +
 85.1286 +   png_ptr->error_fn = error_fn;
 85.1287 +   png_ptr->warning_fn = warning_fn;
 85.1288 +   png_ptr->error_ptr = error_ptr;
 85.1289 +#ifdef PNG_USER_MEM_SUPPORTED
 85.1290 +   png_ptr->free_fn = free_fn;
 85.1291 +#endif
 85.1292 +
 85.1293 +#ifdef PNG_SETJMP_SUPPORTED
 85.1294 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
 85.1295 +#endif
 85.1296 +
 85.1297 +}
 85.1298 +
 85.1299 +void PNGAPI
 85.1300 +png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
 85.1301 +{
 85.1302 +   if (png_ptr == NULL) return;
 85.1303 +   png_ptr->read_row_fn = read_row_fn;
 85.1304 +}
 85.1305 +
 85.1306 +
 85.1307 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 85.1308 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 85.1309 +void PNGAPI
 85.1310 +png_read_png(png_structp png_ptr, png_infop info_ptr,
 85.1311 +                           int transforms,
 85.1312 +                           voidp params)
 85.1313 +{
 85.1314 +   int row;
 85.1315 +
 85.1316 +   if (png_ptr == NULL) return;
 85.1317 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 85.1318 +   /* invert the alpha channel from opacity to transparency
 85.1319 +    */
 85.1320 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
 85.1321 +       png_set_invert_alpha(png_ptr);
 85.1322 +#endif
 85.1323 +
 85.1324 +   /* png_read_info() gives us all of the information from the
 85.1325 +    * PNG file before the first IDAT (image data chunk).
 85.1326 +    */
 85.1327 +   png_read_info(png_ptr, info_ptr);
 85.1328 +   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
 85.1329 +      png_error(png_ptr, "Image is too high to process with png_read_png()");
 85.1330 +
 85.1331 +   /* -------------- image transformations start here ------------------- */
 85.1332 +
 85.1333 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 85.1334 +   /* tell libpng to strip 16 bit/color files down to 8 bits per color
 85.1335 +    */
 85.1336 +   if (transforms & PNG_TRANSFORM_STRIP_16)
 85.1337 +       png_set_strip_16(png_ptr);
 85.1338 +#endif
 85.1339 +
 85.1340 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 85.1341 +   /* Strip alpha bytes from the input data without combining with
 85.1342 +    * the background (not recommended).
 85.1343 +    */
 85.1344 +   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
 85.1345 +       png_set_strip_alpha(png_ptr);
 85.1346 +#endif
 85.1347 +
 85.1348 +#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
 85.1349 +   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
 85.1350 +    * byte into separate bytes (useful for paletted and grayscale images).
 85.1351 +    */
 85.1352 +   if (transforms & PNG_TRANSFORM_PACKING)
 85.1353 +       png_set_packing(png_ptr);
 85.1354 +#endif
 85.1355 +
 85.1356 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.1357 +   /* Change the order of packed pixels to least significant bit first
 85.1358 +    * (not useful if you are using png_set_packing).
 85.1359 +    */
 85.1360 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
 85.1361 +       png_set_packswap(png_ptr);
 85.1362 +#endif
 85.1363 +
 85.1364 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 85.1365 +   /* Expand paletted colors into true RGB triplets
 85.1366 +    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
 85.1367 +    * Expand paletted or RGB images with transparency to full alpha
 85.1368 +    * channels so the data will be available as RGBA quartets.
 85.1369 +    */
 85.1370 +   if (transforms & PNG_TRANSFORM_EXPAND)
 85.1371 +       if ((png_ptr->bit_depth < 8) ||
 85.1372 +           (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
 85.1373 +           (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
 85.1374 +         png_set_expand(png_ptr);
 85.1375 +#endif
 85.1376 +
 85.1377 +   /* We don't handle background color or gamma transformation or dithering.
 85.1378 +    */
 85.1379 +
 85.1380 +#if defined(PNG_READ_INVERT_SUPPORTED)
 85.1381 +   /* invert monochrome files to have 0 as white and 1 as black
 85.1382 +    */
 85.1383 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
 85.1384 +       png_set_invert_mono(png_ptr);
 85.1385 +#endif
 85.1386 +
 85.1387 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 85.1388 +   /* If you want to shift the pixel values from the range [0,255] or
 85.1389 +    * [0,65535] to the original [0,7] or [0,31], or whatever range the
 85.1390 +    * colors were originally in:
 85.1391 +    */
 85.1392 +   if ((transforms & PNG_TRANSFORM_SHIFT)
 85.1393 +       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
 85.1394 +   {
 85.1395 +      png_color_8p sig_bit;
 85.1396 +
 85.1397 +      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
 85.1398 +      png_set_shift(png_ptr, sig_bit);
 85.1399 +   }
 85.1400 +#endif
 85.1401 +
 85.1402 +#if defined(PNG_READ_BGR_SUPPORTED)
 85.1403 +   /* flip the RGB pixels to BGR (or RGBA to BGRA)
 85.1404 +    */
 85.1405 +   if (transforms & PNG_TRANSFORM_BGR)
 85.1406 +       png_set_bgr(png_ptr);
 85.1407 +#endif
 85.1408 +
 85.1409 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 85.1410 +   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
 85.1411 +    */
 85.1412 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
 85.1413 +       png_set_swap_alpha(png_ptr);
 85.1414 +#endif
 85.1415 +
 85.1416 +#if defined(PNG_READ_SWAP_SUPPORTED)
 85.1417 +   /* swap bytes of 16 bit files to least significant byte first
 85.1418 +    */
 85.1419 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
 85.1420 +       png_set_swap(png_ptr);
 85.1421 +#endif
 85.1422 +
 85.1423 +   /* We don't handle adding filler bytes */
 85.1424 +
 85.1425 +   /* Optional call to gamma correct and add the background to the palette
 85.1426 +    * and update info structure.  REQUIRED if you are expecting libpng to
 85.1427 +    * update the palette for you (i.e., you selected such a transform above).
 85.1428 +    */
 85.1429 +   png_read_update_info(png_ptr, info_ptr);
 85.1430 +
 85.1431 +   /* -------------- image transformations end here ------------------- */
 85.1432 +
 85.1433 +#ifdef PNG_FREE_ME_SUPPORTED
 85.1434 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
 85.1435 +#endif
 85.1436 +   if (info_ptr->row_pointers == NULL)
 85.1437 +   {
 85.1438 +      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
 85.1439 +         info_ptr->height * png_sizeof(png_bytep));
 85.1440 +#ifdef PNG_FREE_ME_SUPPORTED
 85.1441 +      info_ptr->free_me |= PNG_FREE_ROWS;
 85.1442 +#endif
 85.1443 +      for (row = 0; row < (int)info_ptr->height; row++)
 85.1444 +      {
 85.1445 +         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
 85.1446 +            png_get_rowbytes(png_ptr, info_ptr));
 85.1447 +      }
 85.1448 +   }
 85.1449 +
 85.1450 +   png_read_image(png_ptr, info_ptr->row_pointers);
 85.1451 +   info_ptr->valid |= PNG_INFO_IDAT;
 85.1452 +
 85.1453 +   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
 85.1454 +   png_read_end(png_ptr, info_ptr);
 85.1455 +
 85.1456 +   transforms = transforms; /* quiet compiler warnings */
 85.1457 +   params = params;
 85.1458 +
 85.1459 +}
 85.1460 +#endif /* PNG_INFO_IMAGE_SUPPORTED */
 85.1461 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
 85.1462 +#endif /* PNG_READ_SUPPORTED */
    86.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    86.2 +++ b/libs/libpng/pngrio.c	Sun Jun 07 17:25:49 2015 +0300
    86.3 @@ -0,0 +1,166 @@
    86.4 +
    86.5 +/* pngrio.c - functions for data input
    86.6 + *
    86.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    86.8 + * For conditions of distribution and use, see copyright notice in png.h
    86.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   86.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   86.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   86.12 + *
   86.13 + * This file provides a location for all input.  Users who need
   86.14 + * special handling are expected to write a function that has the same
   86.15 + * arguments as this and performs a similar function, but that possibly
   86.16 + * has a different input method.  Note that you shouldn't change this
   86.17 + * function, but rather write a replacement function and then make
   86.18 + * libpng use it at run time with png_set_read_fn(...).
   86.19 + */
   86.20 +
   86.21 +#define PNG_INTERNAL
   86.22 +#include "png.h"
   86.23 +#if defined(PNG_READ_SUPPORTED)
   86.24 +
   86.25 +/* Read the data from whatever input you are using.  The default routine
   86.26 +   reads from a file pointer.  Note that this routine sometimes gets called
   86.27 +   with very small lengths, so you should implement some kind of simple
   86.28 +   buffering if you are using unbuffered reads.  This should never be asked
   86.29 +   to read more then 64K on a 16 bit machine. */
   86.30 +void /* PRIVATE */
   86.31 +png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   86.32 +{
   86.33 +   png_debug1(4, "reading %d bytes\n", (int)length);
   86.34 +   if (png_ptr->read_data_fn != NULL)
   86.35 +      (*(png_ptr->read_data_fn))(png_ptr, data, length);
   86.36 +   else
   86.37 +      png_error(png_ptr, "Call to NULL read function");
   86.38 +}
   86.39 +
   86.40 +#if !defined(PNG_NO_STDIO)
   86.41 +/* This is the function that does the actual reading of data.  If you are
   86.42 +   not reading from a standard C stream, you should create a replacement
   86.43 +   read_data function and use it at run time with png_set_read_fn(), rather
   86.44 +   than changing the library. */
   86.45 +#ifndef USE_FAR_KEYWORD
   86.46 +void PNGAPI
   86.47 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   86.48 +{
   86.49 +   png_size_t check;
   86.50 +
   86.51 +   if (png_ptr == NULL) return;
   86.52 +   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
   86.53 +    * instead of an int, which is what fread() actually returns.
   86.54 +    */
   86.55 +#if defined(_WIN32_WCE)
   86.56 +   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   86.57 +      check = 0;
   86.58 +#else
   86.59 +   check = (png_size_t)fread(data, (png_size_t)1, length,
   86.60 +      (png_FILE_p)png_ptr->io_ptr);
   86.61 +#endif
   86.62 +
   86.63 +   if (check != length)
   86.64 +      png_error(png_ptr, "Read Error");
   86.65 +}
   86.66 +#else
   86.67 +/* this is the model-independent version. Since the standard I/O library
   86.68 +   can't handle far buffers in the medium and small models, we have to copy
   86.69 +   the data.
   86.70 +*/
   86.71 +
   86.72 +#define NEAR_BUF_SIZE 1024
   86.73 +#define MIN(a,b) (a <= b ? a : b)
   86.74 +
   86.75 +static void PNGAPI
   86.76 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   86.77 +{
   86.78 +   int check;
   86.79 +   png_byte *n_data;
   86.80 +   png_FILE_p io_ptr;
   86.81 +
   86.82 +   if (png_ptr == NULL) return;
   86.83 +   /* Check if data really is near. If so, use usual code. */
   86.84 +   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   86.85 +   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   86.86 +   if ((png_bytep)n_data == data)
   86.87 +   {
   86.88 +#if defined(_WIN32_WCE)
   86.89 +      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   86.90 +         check = 0;
   86.91 +#else
   86.92 +      check = fread(n_data, 1, length, io_ptr);
   86.93 +#endif
   86.94 +   }
   86.95 +   else
   86.96 +   {
   86.97 +      png_byte buf[NEAR_BUF_SIZE];
   86.98 +      png_size_t read, remaining, err;
   86.99 +      check = 0;
  86.100 +      remaining = length;
  86.101 +      do
  86.102 +      {
  86.103 +         read = MIN(NEAR_BUF_SIZE, remaining);
  86.104 +#if defined(_WIN32_WCE)
  86.105 +         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
  86.106 +            err = 0;
  86.107 +#else
  86.108 +         err = fread(buf, (png_size_t)1, read, io_ptr);
  86.109 +#endif
  86.110 +         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  86.111 +         if (err != read)
  86.112 +            break;
  86.113 +         else
  86.114 +            check += err;
  86.115 +         data += read;
  86.116 +         remaining -= read;
  86.117 +      }
  86.118 +      while (remaining != 0);
  86.119 +   }
  86.120 +   if ((png_uint_32)check != (png_uint_32)length)
  86.121 +      png_error(png_ptr, "read Error");
  86.122 +}
  86.123 +#endif
  86.124 +#endif
  86.125 +
  86.126 +/* This function allows the application to supply a new input function
  86.127 +   for libpng if standard C streams aren't being used.
  86.128 +
  86.129 +   This function takes as its arguments:
  86.130 +   png_ptr      - pointer to a png input data structure
  86.131 +   io_ptr       - pointer to user supplied structure containing info about
  86.132 +                  the input functions.  May be NULL.
  86.133 +   read_data_fn - pointer to a new input function that takes as its
  86.134 +                  arguments a pointer to a png_struct, a pointer to
  86.135 +                  a location where input data can be stored, and a 32-bit
  86.136 +                  unsigned int that is the number of bytes to be read.
  86.137 +                  To exit and output any fatal error messages the new write
  86.138 +                  function should call png_error(png_ptr, "Error msg"). */
  86.139 +void PNGAPI
  86.140 +png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  86.141 +   png_rw_ptr read_data_fn)
  86.142 +{
  86.143 +   if (png_ptr == NULL) return;
  86.144 +   png_ptr->io_ptr = io_ptr;
  86.145 +
  86.146 +#if !defined(PNG_NO_STDIO)
  86.147 +   if (read_data_fn != NULL)
  86.148 +      png_ptr->read_data_fn = read_data_fn;
  86.149 +   else
  86.150 +      png_ptr->read_data_fn = png_default_read_data;
  86.151 +#else
  86.152 +   png_ptr->read_data_fn = read_data_fn;
  86.153 +#endif
  86.154 +
  86.155 +   /* It is an error to write to a read device */
  86.156 +   if (png_ptr->write_data_fn != NULL)
  86.157 +   {
  86.158 +      png_ptr->write_data_fn = NULL;
  86.159 +      png_warning(png_ptr,
  86.160 +         "It's an error to set both read_data_fn and write_data_fn in the ");
  86.161 +      png_warning(png_ptr,
  86.162 +         "same structure.  Resetting write_data_fn to NULL.");
  86.163 +   }
  86.164 +
  86.165 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  86.166 +   png_ptr->output_flush_fn = NULL;
  86.167 +#endif
  86.168 +}
  86.169 +#endif /* PNG_READ_SUPPORTED */
    87.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    87.2 +++ b/libs/libpng/pngrtran.c	Sun Jun 07 17:25:49 2015 +0300
    87.3 @@ -0,0 +1,4296 @@
    87.4 +
    87.5 +/* pngrtran.c - transforms the data in a row for PNG readers
    87.6 + *
    87.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    87.8 + * For conditions of distribution and use, see copyright notice in png.h
    87.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   87.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   87.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   87.12 + *
   87.13 + * This file contains functions optionally called by an application
   87.14 + * in order to tell libpng how to handle data when reading a PNG.
   87.15 + * Transformations that are used in both reading and writing are
   87.16 + * in pngtrans.c.
   87.17 + */
   87.18 +
   87.19 +#define PNG_INTERNAL
   87.20 +#include "png.h"
   87.21 +#if defined(PNG_READ_SUPPORTED)
   87.22 +
   87.23 +/* Set the action on getting a CRC error for an ancillary or critical chunk. */
   87.24 +void PNGAPI
   87.25 +png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
   87.26 +{
   87.27 +   png_debug(1, "in png_set_crc_action\n");
   87.28 +   /* Tell libpng how we react to CRC errors in critical chunks */
   87.29 +   if (png_ptr == NULL) return;
   87.30 +   switch (crit_action)
   87.31 +   {
   87.32 +      case PNG_CRC_NO_CHANGE:                        /* leave setting as is */
   87.33 +         break;
   87.34 +      case PNG_CRC_WARN_USE:                               /* warn/use data */
   87.35 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   87.36 +         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
   87.37 +         break;
   87.38 +      case PNG_CRC_QUIET_USE:                             /* quiet/use data */
   87.39 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   87.40 +         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
   87.41 +                           PNG_FLAG_CRC_CRITICAL_IGNORE;
   87.42 +         break;
   87.43 +      case PNG_CRC_WARN_DISCARD:    /* not a valid action for critical data */
   87.44 +         png_warning(png_ptr,
   87.45 +            "Can't discard critical data on CRC error.");
   87.46 +      case PNG_CRC_ERROR_QUIT:                                /* error/quit */
   87.47 +      case PNG_CRC_DEFAULT:
   87.48 +      default:
   87.49 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   87.50 +         break;
   87.51 +   }
   87.52 +
   87.53 +   switch (ancil_action)
   87.54 +   {
   87.55 +      case PNG_CRC_NO_CHANGE:                       /* leave setting as is */
   87.56 +         break;
   87.57 +      case PNG_CRC_WARN_USE:                              /* warn/use data */
   87.58 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   87.59 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
   87.60 +         break;
   87.61 +      case PNG_CRC_QUIET_USE:                            /* quiet/use data */
   87.62 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   87.63 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
   87.64 +                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
   87.65 +         break;
   87.66 +      case PNG_CRC_ERROR_QUIT:                               /* error/quit */
   87.67 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   87.68 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
   87.69 +         break;
   87.70 +      case PNG_CRC_WARN_DISCARD:                      /* warn/discard data */
   87.71 +      case PNG_CRC_DEFAULT:
   87.72 +      default:
   87.73 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   87.74 +         break;
   87.75 +   }
   87.76 +}
   87.77 +
   87.78 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
   87.79 +    defined(PNG_FLOATING_POINT_SUPPORTED)
   87.80 +/* handle alpha and tRNS via a background color */
   87.81 +void PNGAPI
   87.82 +png_set_background(png_structp png_ptr,
   87.83 +   png_color_16p background_color, int background_gamma_code,
   87.84 +   int need_expand, double background_gamma)
   87.85 +{
   87.86 +   png_debug(1, "in png_set_background\n");
   87.87 +   if (png_ptr == NULL) return;
   87.88 +   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
   87.89 +   {
   87.90 +      png_warning(png_ptr, "Application must supply a known background gamma");
   87.91 +      return;
   87.92 +   }
   87.93 +
   87.94 +   png_ptr->transformations |= PNG_BACKGROUND;
   87.95 +   png_memcpy(&(png_ptr->background), background_color,
   87.96 +      png_sizeof(png_color_16));
   87.97 +   png_ptr->background_gamma = (float)background_gamma;
   87.98 +   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
   87.99 +   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
  87.100 +}
  87.101 +#endif
  87.102 +
  87.103 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
  87.104 +/* strip 16 bit depth files to 8 bit depth */
  87.105 +void PNGAPI
  87.106 +png_set_strip_16(png_structp png_ptr)
  87.107 +{
  87.108 +   png_debug(1, "in png_set_strip_16\n");
  87.109 +   if (png_ptr == NULL) return;
  87.110 +   png_ptr->transformations |= PNG_16_TO_8;
  87.111 +}
  87.112 +#endif
  87.113 +
  87.114 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  87.115 +void PNGAPI
  87.116 +png_set_strip_alpha(png_structp png_ptr)
  87.117 +{
  87.118 +   png_debug(1, "in png_set_strip_alpha\n");
  87.119 +   if (png_ptr == NULL) return;
  87.120 +   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
  87.121 +}
  87.122 +#endif
  87.123 +
  87.124 +#if defined(PNG_READ_DITHER_SUPPORTED)
  87.125 +/* Dither file to 8 bit.  Supply a palette, the current number
  87.126 + * of elements in the palette, the maximum number of elements
  87.127 + * allowed, and a histogram if possible.  If the current number
  87.128 + * of colors is greater then the maximum number, the palette will be
  87.129 + * modified to fit in the maximum number.  "full_dither" indicates
  87.130 + * whether we need a dithering cube set up for RGB images, or if we
  87.131 + * simply are reducing the number of colors in a paletted image.
  87.132 + */
  87.133 +
  87.134 +typedef struct png_dsort_struct
  87.135 +{
  87.136 +   struct png_dsort_struct FAR * next;
  87.137 +   png_byte left;
  87.138 +   png_byte right;
  87.139 +} png_dsort;
  87.140 +typedef png_dsort FAR *       png_dsortp;
  87.141 +typedef png_dsort FAR * FAR * png_dsortpp;
  87.142 +
  87.143 +void PNGAPI
  87.144 +png_set_dither(png_structp png_ptr, png_colorp palette,
  87.145 +   int num_palette, int maximum_colors, png_uint_16p histogram,
  87.146 +   int full_dither)
  87.147 +{
  87.148 +   png_debug(1, "in png_set_dither\n");
  87.149 +   if (png_ptr == NULL) return;
  87.150 +   png_ptr->transformations |= PNG_DITHER;
  87.151 +
  87.152 +   if (!full_dither)
  87.153 +   {
  87.154 +      int i;
  87.155 +
  87.156 +      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
  87.157 +         (png_uint_32)(num_palette * png_sizeof(png_byte)));
  87.158 +      for (i = 0; i < num_palette; i++)
  87.159 +         png_ptr->dither_index[i] = (png_byte)i;
  87.160 +   }
  87.161 +
  87.162 +   if (num_palette > maximum_colors)
  87.163 +   {
  87.164 +      if (histogram != NULL)
  87.165 +      {
  87.166 +         /* This is easy enough, just throw out the least used colors.
  87.167 +            Perhaps not the best solution, but good enough. */
  87.168 +
  87.169 +         int i;
  87.170 +
  87.171 +         /* initialize an array to sort colors */
  87.172 +         png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
  87.173 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  87.174 +
  87.175 +         /* initialize the dither_sort array */
  87.176 +         for (i = 0; i < num_palette; i++)
  87.177 +            png_ptr->dither_sort[i] = (png_byte)i;
  87.178 +
  87.179 +         /* Find the least used palette entries by starting a
  87.180 +            bubble sort, and running it until we have sorted
  87.181 +            out enough colors.  Note that we don't care about
  87.182 +            sorting all the colors, just finding which are
  87.183 +            least used. */
  87.184 +
  87.185 +         for (i = num_palette - 1; i >= maximum_colors; i--)
  87.186 +         {
  87.187 +            int done; /* to stop early if the list is pre-sorted */
  87.188 +            int j;
  87.189 +
  87.190 +            done = 1;
  87.191 +            for (j = 0; j < i; j++)
  87.192 +            {
  87.193 +               if (histogram[png_ptr->dither_sort[j]]
  87.194 +                   < histogram[png_ptr->dither_sort[j + 1]])
  87.195 +               {
  87.196 +                  png_byte t;
  87.197 +
  87.198 +                  t = png_ptr->dither_sort[j];
  87.199 +                  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
  87.200 +                  png_ptr->dither_sort[j + 1] = t;
  87.201 +                  done = 0;
  87.202 +               }
  87.203 +            }
  87.204 +            if (done)
  87.205 +               break;
  87.206 +         }
  87.207 +
  87.208 +         /* swap the palette around, and set up a table, if necessary */
  87.209 +         if (full_dither)
  87.210 +         {
  87.211 +            int j = num_palette;
  87.212 +
  87.213 +            /* put all the useful colors within the max, but don't
  87.214 +               move the others */
  87.215 +            for (i = 0; i < maximum_colors; i++)
  87.216 +            {
  87.217 +               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
  87.218 +               {
  87.219 +                  do
  87.220 +                     j--;
  87.221 +                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
  87.222 +                  palette[i] = palette[j];
  87.223 +               }
  87.224 +            }
  87.225 +         }
  87.226 +         else
  87.227 +         {
  87.228 +            int j = num_palette;
  87.229 +
  87.230 +            /* move all the used colors inside the max limit, and
  87.231 +               develop a translation table */
  87.232 +            for (i = 0; i < maximum_colors; i++)
  87.233 +            {
  87.234 +               /* only move the colors we need to */
  87.235 +               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
  87.236 +               {
  87.237 +                  png_color tmp_color;
  87.238 +
  87.239 +                  do
  87.240 +                     j--;
  87.241 +                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
  87.242 +
  87.243 +                  tmp_color = palette[j];
  87.244 +                  palette[j] = palette[i];
  87.245 +                  palette[i] = tmp_color;
  87.246 +                  /* indicate where the color went */
  87.247 +                  png_ptr->dither_index[j] = (png_byte)i;
  87.248 +                  png_ptr->dither_index[i] = (png_byte)j;
  87.249 +               }
  87.250 +            }
  87.251 +
  87.252 +            /* find closest color for those colors we are not using */
  87.253 +            for (i = 0; i < num_palette; i++)
  87.254 +            {
  87.255 +               if ((int)png_ptr->dither_index[i] >= maximum_colors)
  87.256 +               {
  87.257 +                  int min_d, k, min_k, d_index;
  87.258 +
  87.259 +                  /* find the closest color to one we threw out */
  87.260 +                  d_index = png_ptr->dither_index[i];
  87.261 +                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  87.262 +                  for (k = 1, min_k = 0; k < maximum_colors; k++)
  87.263 +                  {
  87.264 +                     int d;
  87.265 +
  87.266 +                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  87.267 +
  87.268 +                     if (d < min_d)
  87.269 +                     {
  87.270 +                        min_d = d;
  87.271 +                        min_k = k;
  87.272 +                     }
  87.273 +                  }
  87.274 +                  /* point to closest color */
  87.275 +                  png_ptr->dither_index[i] = (png_byte)min_k;
  87.276 +               }
  87.277 +            }
  87.278 +         }
  87.279 +         png_free(png_ptr, png_ptr->dither_sort);
  87.280 +         png_ptr->dither_sort = NULL;
  87.281 +      }
  87.282 +      else
  87.283 +      {
  87.284 +         /* This is much harder to do simply (and quickly).  Perhaps
  87.285 +            we need to go through a median cut routine, but those
  87.286 +            don't always behave themselves with only a few colors
  87.287 +            as input.  So we will just find the closest two colors,
  87.288 +            and throw out one of them (chosen somewhat randomly).
  87.289 +            [We don't understand this at all, so if someone wants to
  87.290 +             work on improving it, be our guest - AED, GRP]
  87.291 +            */
  87.292 +         int i;
  87.293 +         int max_d;
  87.294 +         int num_new_palette;
  87.295 +         png_dsortp t;
  87.296 +         png_dsortpp hash;
  87.297 +
  87.298 +         t = NULL;
  87.299 +
  87.300 +         /* initialize palette index arrays */
  87.301 +         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
  87.302 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  87.303 +         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  87.304 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  87.305 +
  87.306 +         /* initialize the sort array */
  87.307 +         for (i = 0; i < num_palette; i++)
  87.308 +         {
  87.309 +            png_ptr->index_to_palette[i] = (png_byte)i;
  87.310 +            png_ptr->palette_to_index[i] = (png_byte)i;
  87.311 +         }
  87.312 +
  87.313 +         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
  87.314 +            png_sizeof(png_dsortp)));
  87.315 +         for (i = 0; i < 769; i++)
  87.316 +            hash[i] = NULL;
  87.317 +/*         png_memset(hash, 0, 769 * png_sizeof(png_dsortp)); */
  87.318 +
  87.319 +         num_new_palette = num_palette;
  87.320 +
  87.321 +         /* initial wild guess at how far apart the farthest pixel
  87.322 +            pair we will be eliminating will be.  Larger
  87.323 +            numbers mean more areas will be allocated, Smaller
  87.324 +            numbers run the risk of not saving enough data, and
  87.325 +            having to do this all over again.
  87.326 +
  87.327 +            I have not done extensive checking on this number.
  87.328 +            */
  87.329 +         max_d = 96;
  87.330 +
  87.331 +         while (num_new_palette > maximum_colors)
  87.332 +         {
  87.333 +            for (i = 0; i < num_new_palette - 1; i++)
  87.334 +            {
  87.335 +               int j;
  87.336 +
  87.337 +               for (j = i + 1; j < num_new_palette; j++)
  87.338 +               {
  87.339 +                  int d;
  87.340 +
  87.341 +                  d = PNG_COLOR_DIST(palette[i], palette[j]);
  87.342 +
  87.343 +                  if (d <= max_d)
  87.344 +                  {
  87.345 +
  87.346 +                     t = (png_dsortp)png_malloc_warn(png_ptr,
  87.347 +                         (png_uint_32)(png_sizeof(png_dsort)));
  87.348 +                     if (t == NULL)
  87.349 +                         break;
  87.350 +                     t->next = hash[d];
  87.351 +                     t->left = (png_byte)i;
  87.352 +                     t->right = (png_byte)j;
  87.353 +                     hash[d] = t;
  87.354 +                  }
  87.355 +               }
  87.356 +               if (t == NULL)
  87.357 +                  break;
  87.358 +            }
  87.359 +
  87.360 +            if (t != NULL)
  87.361 +            for (i = 0; i <= max_d; i++)
  87.362 +            {
  87.363 +               if (hash[i] != NULL)
  87.364 +               {
  87.365 +                  png_dsortp p;
  87.366 +
  87.367 +                  for (p = hash[i]; p; p = p->next)
  87.368 +                  {
  87.369 +                     if ((int)png_ptr->index_to_palette[p->left]
  87.370 +                        < num_new_palette &&
  87.371 +                        (int)png_ptr->index_to_palette[p->right]
  87.372 +                        < num_new_palette)
  87.373 +                     {
  87.374 +                        int j, next_j;
  87.375 +
  87.376 +                        if (num_new_palette & 0x01)
  87.377 +                        {
  87.378 +                           j = p->left;
  87.379 +                           next_j = p->right;
  87.380 +                        }
  87.381 +                        else
  87.382 +                        {
  87.383 +                           j = p->right;
  87.384 +                           next_j = p->left;
  87.385 +                        }
  87.386 +
  87.387 +                        num_new_palette--;
  87.388 +                        palette[png_ptr->index_to_palette[j]]
  87.389 +                          = palette[num_new_palette];
  87.390 +                        if (!full_dither)
  87.391 +                        {
  87.392 +                           int k;
  87.393 +
  87.394 +                           for (k = 0; k < num_palette; k++)
  87.395 +                           {
  87.396 +                              if (png_ptr->dither_index[k] ==
  87.397 +                                 png_ptr->index_to_palette[j])
  87.398 +                                 png_ptr->dither_index[k] =
  87.399 +                                    png_ptr->index_to_palette[next_j];
  87.400 +                              if ((int)png_ptr->dither_index[k] ==
  87.401 +                                 num_new_palette)
  87.402 +                                 png_ptr->dither_index[k] =
  87.403 +                                    png_ptr->index_to_palette[j];
  87.404 +                           }
  87.405 +                        }
  87.406 +
  87.407 +                        png_ptr->index_to_palette[png_ptr->palette_to_index
  87.408 +                           [num_new_palette]] = png_ptr->index_to_palette[j];
  87.409 +                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
  87.410 +                           = png_ptr->palette_to_index[num_new_palette];
  87.411 +
  87.412 +                        png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
  87.413 +                        png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
  87.414 +                     }
  87.415 +                     if (num_new_palette <= maximum_colors)
  87.416 +                        break;
  87.417 +                  }
  87.418 +                  if (num_new_palette <= maximum_colors)
  87.419 +                     break;
  87.420 +               }
  87.421 +            }
  87.422 +
  87.423 +            for (i = 0; i < 769; i++)
  87.424 +            {
  87.425 +               if (hash[i] != NULL)
  87.426 +               {
  87.427 +                  png_dsortp p = hash[i];
  87.428 +                  while (p)
  87.429 +                  {
  87.430 +                     t = p->next;
  87.431 +                     png_free(png_ptr, p);
  87.432 +                     p = t;
  87.433 +                  }
  87.434 +               }
  87.435 +               hash[i] = 0;
  87.436 +            }
  87.437 +            max_d += 96;
  87.438 +         }
  87.439 +         png_free(png_ptr, hash);
  87.440 +         png_free(png_ptr, png_ptr->palette_to_index);
  87.441 +         png_free(png_ptr, png_ptr->index_to_palette);
  87.442 +         png_ptr->palette_to_index = NULL;
  87.443 +         png_ptr->index_to_palette = NULL;
  87.444 +      }
  87.445 +      num_palette = maximum_colors;
  87.446 +   }
  87.447 +   if (png_ptr->palette == NULL)
  87.448 +   {
  87.449 +      png_ptr->palette = palette;
  87.450 +   }
  87.451 +   png_ptr->num_palette = (png_uint_16)num_palette;
  87.452 +
  87.453 +   if (full_dither)
  87.454 +   {
  87.455 +      int i;
  87.456 +      png_bytep distance;
  87.457 +      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
  87.458 +         PNG_DITHER_BLUE_BITS;
  87.459 +      int num_red = (1 << PNG_DITHER_RED_BITS);
  87.460 +      int num_green = (1 << PNG_DITHER_GREEN_BITS);
  87.461 +      int num_blue = (1 << PNG_DITHER_BLUE_BITS);
  87.462 +      png_size_t num_entries = ((png_size_t)1 << total_bits);
  87.463 +
  87.464 +      png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
  87.465 +         (png_uint_32)(num_entries * png_sizeof(png_byte)));
  87.466 +
  87.467 +      png_memset(png_ptr->palette_lookup, 0, num_entries *
  87.468 +         png_sizeof(png_byte));
  87.469 +
  87.470 +      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
  87.471 +         png_sizeof(png_byte)));
  87.472 +
  87.473 +      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
  87.474 +
  87.475 +      for (i = 0; i < num_palette; i++)
  87.476 +      {
  87.477 +         int ir, ig, ib;
  87.478 +         int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
  87.479 +         int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
  87.480 +         int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
  87.481 +
  87.482 +         for (ir = 0; ir < num_red; ir++)
  87.483 +         {
  87.484 +            /* int dr = abs(ir - r); */
  87.485 +            int dr = ((ir > r) ? ir - r : r - ir);
  87.486 +            int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
  87.487 +
  87.488 +            for (ig = 0; ig < num_green; ig++)
  87.489 +            {
  87.490 +               /* int dg = abs(ig - g); */
  87.491 +               int dg = ((ig > g) ? ig - g : g - ig);
  87.492 +               int dt = dr + dg;
  87.493 +               int dm = ((dr > dg) ? dr : dg);
  87.494 +               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
  87.495 +
  87.496 +               for (ib = 0; ib < num_blue; ib++)
  87.497 +               {
  87.498 +                  int d_index = index_g | ib;
  87.499 +                  /* int db = abs(ib - b); */
  87.500 +                  int db = ((ib > b) ? ib - b : b - ib);
  87.501 +                  int dmax = ((dm > db) ? dm : db);
  87.502 +                  int d = dmax + dt + db;
  87.503 +
  87.504 +                  if (d < (int)distance[d_index])
  87.505 +                  {
  87.506 +                     distance[d_index] = (png_byte)d;
  87.507 +                     png_ptr->palette_lookup[d_index] = (png_byte)i;
  87.508 +                  }
  87.509 +               }
  87.510 +            }
  87.511 +         }
  87.512 +      }
  87.513 +
  87.514 +      png_free(png_ptr, distance);
  87.515 +   }
  87.516 +}
  87.517 +#endif
  87.518 +
  87.519 +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  87.520 +/* Transform the image from the file_gamma to the screen_gamma.  We
  87.521 + * only do transformations on images where the file_gamma and screen_gamma
  87.522 + * are not close reciprocals, otherwise it slows things down slightly, and
  87.523 + * also needlessly introduces small errors.
  87.524 + *
  87.525 + * We will turn off gamma transformation later if no semitransparent entries
  87.526 + * are present in the tRNS array for palette images.  We can't do it here
  87.527 + * because we don't necessarily have the tRNS chunk yet.
  87.528 + */
  87.529 +void PNGAPI
  87.530 +png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
  87.531 +{
  87.532 +   png_debug(1, "in png_set_gamma\n");
  87.533 +   if (png_ptr == NULL) return;
  87.534 +   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
  87.535 +       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
  87.536 +       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  87.537 +     png_ptr->transformations |= PNG_GAMMA;
  87.538 +   png_ptr->gamma = (float)file_gamma;
  87.539 +   png_ptr->screen_gamma = (float)scrn_gamma;
  87.540 +}
  87.541 +#endif
  87.542 +
  87.543 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  87.544 +/* Expand paletted images to RGB, expand grayscale images of
  87.545 + * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
  87.546 + * to alpha channels.
  87.547 + */
  87.548 +void PNGAPI
  87.549 +png_set_expand(png_structp png_ptr)
  87.550 +{
  87.551 +   png_debug(1, "in png_set_expand\n");
  87.552 +   if (png_ptr == NULL) return;
  87.553 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  87.554 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  87.555 +}
  87.556 +
  87.557 +/* GRR 19990627:  the following three functions currently are identical
  87.558 + *  to png_set_expand().  However, it is entirely reasonable that someone
  87.559 + *  might wish to expand an indexed image to RGB but *not* expand a single,
  87.560 + *  fully transparent palette entry to a full alpha channel--perhaps instead
  87.561 + *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
  87.562 + *  the transparent color with a particular RGB value, or drop tRNS entirely.
  87.563 + *  IOW, a future version of the library may make the transformations flag
  87.564 + *  a bit more fine-grained, with separate bits for each of these three
  87.565 + *  functions.
  87.566 + *
  87.567 + *  More to the point, these functions make it obvious what libpng will be
  87.568 + *  doing, whereas "expand" can (and does) mean any number of things.
  87.569 + *
  87.570 + *  GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
  87.571 + *  to expand only the sample depth but not to expand the tRNS to alpha.
  87.572 + */
  87.573 +
  87.574 +/* Expand paletted images to RGB. */
  87.575 +void PNGAPI
  87.576 +png_set_palette_to_rgb(png_structp png_ptr)
  87.577 +{
  87.578 +   png_debug(1, "in png_set_palette_to_rgb\n");
  87.579 +   if (png_ptr == NULL) return;
  87.580 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  87.581 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  87.582 +}
  87.583 +
  87.584 +#if !defined(PNG_1_0_X)
  87.585 +/* Expand grayscale images of less than 8-bit depth to 8 bits. */
  87.586 +void PNGAPI
  87.587 +png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
  87.588 +{
  87.589 +   png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n");
  87.590 +   if (png_ptr == NULL) return;
  87.591 +   png_ptr->transformations |= PNG_EXPAND;
  87.592 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  87.593 +}
  87.594 +#endif
  87.595 +
  87.596 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  87.597 +/* Expand grayscale images of less than 8-bit depth to 8 bits. */
  87.598 +/* Deprecated as of libpng-1.2.9 */
  87.599 +void PNGAPI
  87.600 +png_set_gray_1_2_4_to_8(png_structp png_ptr)
  87.601 +{
  87.602 +   png_debug(1, "in png_set_gray_1_2_4_to_8\n");
  87.603 +   if (png_ptr == NULL) return;
  87.604 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  87.605 +}
  87.606 +#endif
  87.607 +
  87.608 +
  87.609 +/* Expand tRNS chunks to alpha channels. */
  87.610 +void PNGAPI
  87.611 +png_set_tRNS_to_alpha(png_structp png_ptr)
  87.612 +{
  87.613 +   png_debug(1, "in png_set_tRNS_to_alpha\n");
  87.614 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  87.615 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  87.616 +}
  87.617 +#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
  87.618 +
  87.619 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  87.620 +void PNGAPI
  87.621 +png_set_gray_to_rgb(png_structp png_ptr)
  87.622 +{
  87.623 +   png_debug(1, "in png_set_gray_to_rgb\n");
  87.624 +   png_ptr->transformations |= PNG_GRAY_TO_RGB;
  87.625 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  87.626 +}
  87.627 +#endif
  87.628 +
  87.629 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  87.630 +#if defined(PNG_FLOATING_POINT_SUPPORTED)
  87.631 +/* Convert a RGB image to a grayscale of the same width.  This allows us,
  87.632 + * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  87.633 + */
  87.634 +
  87.635 +void PNGAPI
  87.636 +png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
  87.637 +   double green)
  87.638 +{
  87.639 +      int red_fixed = (int)((float)red*100000.0 + 0.5);
  87.640 +      int green_fixed = (int)((float)green*100000.0 + 0.5);
  87.641 +      if (png_ptr == NULL) return;
  87.642 +      png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
  87.643 +}
  87.644 +#endif
  87.645 +
  87.646 +void PNGAPI
  87.647 +png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
  87.648 +   png_fixed_point red, png_fixed_point green)
  87.649 +{
  87.650 +   png_debug(1, "in png_set_rgb_to_gray\n");
  87.651 +   if (png_ptr == NULL) return;
  87.652 +   switch(error_action)
  87.653 +   {
  87.654 +      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
  87.655 +              break;
  87.656 +      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
  87.657 +              break;
  87.658 +      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
  87.659 +   }
  87.660 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  87.661 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  87.662 +      png_ptr->transformations |= PNG_EXPAND;
  87.663 +#else
  87.664 +   {
  87.665 +      png_warning(png_ptr,
  87.666 +        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
  87.667 +      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
  87.668 +   }
  87.669 +#endif
  87.670 +   {
  87.671 +      png_uint_16 red_int, green_int;
  87.672 +      if (red < 0 || green < 0)
  87.673 +      {
  87.674 +         red_int   =  6968; /* .212671 * 32768 + .5 */
  87.675 +         green_int = 23434; /* .715160 * 32768 + .5 */
  87.676 +      }
  87.677 +      else if (red + green < 100000L)
  87.678 +      {
  87.679 +        red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
  87.680 +        green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
  87.681 +      }
  87.682 +      else
  87.683 +      {
  87.684 +         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
  87.685 +         red_int   =  6968;
  87.686 +         green_int = 23434;
  87.687 +      }
  87.688 +      png_ptr->rgb_to_gray_red_coeff   = red_int;
  87.689 +      png_ptr->rgb_to_gray_green_coeff = green_int;
  87.690 +      png_ptr->rgb_to_gray_blue_coeff  = 
  87.691 +         (png_uint_16)(32768 - red_int - green_int);
  87.692 +   }
  87.693 +}
  87.694 +#endif
  87.695 +
  87.696 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  87.697 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  87.698 +    defined(PNG_LEGACY_SUPPORTED)
  87.699 +void PNGAPI
  87.700 +png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  87.701 +   read_user_transform_fn)
  87.702 +{
  87.703 +   png_debug(1, "in png_set_read_user_transform_fn\n");
  87.704 +   if (png_ptr == NULL) return;
  87.705 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  87.706 +   png_ptr->transformations |= PNG_USER_TRANSFORM;
  87.707 +   png_ptr->read_user_transform_fn = read_user_transform_fn;
  87.708 +#endif
  87.709 +#ifdef PNG_LEGACY_SUPPORTED
  87.710 +   if (read_user_transform_fn)
  87.711 +      png_warning(png_ptr,
  87.712 +        "This version of libpng does not support user transforms");
  87.713 +#endif
  87.714 +}
  87.715 +#endif
  87.716 +
  87.717 +/* Initialize everything needed for the read.  This includes modifying
  87.718 + * the palette.
  87.719 + */
  87.720 +void /* PRIVATE */
  87.721 +png_init_read_transformations(png_structp png_ptr)
  87.722 +{
  87.723 +   png_debug(1, "in png_init_read_transformations\n");
  87.724 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.725 +   if (png_ptr != NULL)
  87.726 +#endif
  87.727 +  {
  87.728 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
  87.729 + || defined(PNG_READ_GAMMA_SUPPORTED)
  87.730 +   int color_type = png_ptr->color_type;
  87.731 +#endif
  87.732 +
  87.733 +#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  87.734 +
  87.735 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  87.736 +   /* Detect gray background and attempt to enable optimization
  87.737 +    * for gray --> RGB case */
  87.738 +   /* Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
  87.739 +    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
  87.740 +    * background color might actually be gray yet not be flagged as such.
  87.741 +    * This is not a problem for the current code, which uses
  87.742 +    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
  87.743 +    * png_do_gray_to_rgb() transformation.
  87.744 +    */
  87.745 +   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  87.746 +       !(color_type & PNG_COLOR_MASK_COLOR))
  87.747 +   {
  87.748 +          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  87.749 +   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
  87.750 +              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  87.751 +              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  87.752 +              png_ptr->background.red == png_ptr->background.green &&
  87.753 +              png_ptr->background.red == png_ptr->background.blue)
  87.754 +   {
  87.755 +          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  87.756 +          png_ptr->background.gray = png_ptr->background.red;
  87.757 +   }
  87.758 +#endif
  87.759 +
  87.760 +   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  87.761 +       (png_ptr->transformations & PNG_EXPAND))
  87.762 +   {
  87.763 +      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
  87.764 +      {
  87.765 +         /* expand background and tRNS chunks */
  87.766 +         switch (png_ptr->bit_depth)
  87.767 +         {
  87.768 +            case 1:
  87.769 +               png_ptr->background.gray *= (png_uint_16)0xff;
  87.770 +               png_ptr->background.red = png_ptr->background.green
  87.771 +                 =  png_ptr->background.blue = png_ptr->background.gray;
  87.772 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  87.773 +               {
  87.774 +                 png_ptr->trans_values.gray *= (png_uint_16)0xff;
  87.775 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  87.776 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  87.777 +               }
  87.778 +               break;
  87.779 +            case 2:
  87.780 +               png_ptr->background.gray *= (png_uint_16)0x55;
  87.781 +               png_ptr->background.red = png_ptr->background.green
  87.782 +                 = png_ptr->background.blue = png_ptr->background.gray;
  87.783 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  87.784 +               {
  87.785 +                 png_ptr->trans_values.gray *= (png_uint_16)0x55;
  87.786 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  87.787 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  87.788 +               }
  87.789 +               break;
  87.790 +            case 4:
  87.791 +               png_ptr->background.gray *= (png_uint_16)0x11;
  87.792 +               png_ptr->background.red = png_ptr->background.green
  87.793 +                 = png_ptr->background.blue = png_ptr->background.gray;
  87.794 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  87.795 +               {
  87.796 +                 png_ptr->trans_values.gray *= (png_uint_16)0x11;
  87.797 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  87.798 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  87.799 +               }
  87.800 +               break;
  87.801 +            case 8:
  87.802 +            case 16:
  87.803 +               png_ptr->background.red = png_ptr->background.green
  87.804 +                 = png_ptr->background.blue = png_ptr->background.gray;
  87.805 +               break;
  87.806 +         }
  87.807 +      }
  87.808 +      else if (color_type == PNG_COLOR_TYPE_PALETTE)
  87.809 +      {
  87.810 +         png_ptr->background.red   =
  87.811 +            png_ptr->palette[png_ptr->background.index].red;
  87.812 +         png_ptr->background.green =
  87.813 +            png_ptr->palette[png_ptr->background.index].green;
  87.814 +         png_ptr->background.blue  =
  87.815 +            png_ptr->palette[png_ptr->background.index].blue;
  87.816 +
  87.817 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  87.818 +        if (png_ptr->transformations & PNG_INVERT_ALPHA)
  87.819 +        {
  87.820 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  87.821 +           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  87.822 +#endif
  87.823 +           {
  87.824 +           /* invert the alpha channel (in tRNS) unless the pixels are
  87.825 +              going to be expanded, in which case leave it for later */
  87.826 +              int i, istop;
  87.827 +              istop=(int)png_ptr->num_trans;
  87.828 +              for (i=0; i<istop; i++)
  87.829 +                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
  87.830 +           }
  87.831 +        }
  87.832 +#endif
  87.833 +
  87.834 +      }
  87.835 +   }
  87.836 +#endif
  87.837 +
  87.838 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  87.839 +   png_ptr->background_1 = png_ptr->background;
  87.840 +#endif
  87.841 +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  87.842 +
  87.843 +   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
  87.844 +       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
  87.845 +         < PNG_GAMMA_THRESHOLD))
  87.846 +   {
  87.847 +    int i, k;
  87.848 +    k=0;
  87.849 +    for (i=0; i<png_ptr->num_trans; i++)
  87.850 +    {
  87.851 +      if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
  87.852 +        k=1; /* partial transparency is present */
  87.853 +    }
  87.854 +    if (k == 0)
  87.855 +      png_ptr->transformations &= ~PNG_GAMMA;
  87.856 +   }
  87.857 +
  87.858 +   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
  87.859 +        png_ptr->gamma != 0.0)
  87.860 +   {
  87.861 +      png_build_gamma_table(png_ptr);
  87.862 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
  87.863 +      if (png_ptr->transformations & PNG_BACKGROUND)
  87.864 +      {
  87.865 +         if (color_type == PNG_COLOR_TYPE_PALETTE)
  87.866 +         {
  87.867 +           /* could skip if no transparency and
  87.868 +           */
  87.869 +            png_color back, back_1;
  87.870 +            png_colorp palette = png_ptr->palette;
  87.871 +            int num_palette = png_ptr->num_palette;
  87.872 +            int i;
  87.873 +            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  87.874 +            {
  87.875 +               back.red = png_ptr->gamma_table[png_ptr->background.red];
  87.876 +               back.green = png_ptr->gamma_table[png_ptr->background.green];
  87.877 +               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  87.878 +
  87.879 +               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  87.880 +               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  87.881 +               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  87.882 +            }
  87.883 +            else
  87.884 +            {
  87.885 +               double g, gs;
  87.886 +
  87.887 +               switch (png_ptr->background_gamma_type)
  87.888 +               {
  87.889 +                  case PNG_BACKGROUND_GAMMA_SCREEN:
  87.890 +                     g = (png_ptr->screen_gamma);
  87.891 +                     gs = 1.0;
  87.892 +                     break;
  87.893 +                  case PNG_BACKGROUND_GAMMA_FILE:
  87.894 +                     g = 1.0 / (png_ptr->gamma);
  87.895 +                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  87.896 +                     break;
  87.897 +                  case PNG_BACKGROUND_GAMMA_UNIQUE:
  87.898 +                     g = 1.0 / (png_ptr->background_gamma);
  87.899 +                     gs = 1.0 / (png_ptr->background_gamma *
  87.900 +                                 png_ptr->screen_gamma);
  87.901 +                     break;
  87.902 +                  default:
  87.903 +                     g = 1.0;    /* back_1 */
  87.904 +                     gs = 1.0;   /* back */
  87.905 +               }
  87.906 +
  87.907 +               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
  87.908 +               {
  87.909 +                  back.red   = (png_byte)png_ptr->background.red;
  87.910 +                  back.green = (png_byte)png_ptr->background.green;
  87.911 +                  back.blue  = (png_byte)png_ptr->background.blue;
  87.912 +               }
  87.913 +               else
  87.914 +               {
  87.915 +                  back.red = (png_byte)(pow(
  87.916 +                     (double)png_ptr->background.red/255, gs) * 255.0 + .5);
  87.917 +                  back.green = (png_byte)(pow(
  87.918 +                     (double)png_ptr->background.green/255, gs) * 255.0 + .5);
  87.919 +                  back.blue = (png_byte)(pow(
  87.920 +                     (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
  87.921 +               }
  87.922 +
  87.923 +               back_1.red = (png_byte)(pow(
  87.924 +                  (double)png_ptr->background.red/255, g) * 255.0 + .5);
  87.925 +               back_1.green = (png_byte)(pow(
  87.926 +                  (double)png_ptr->background.green/255, g) * 255.0 + .5);
  87.927 +               back_1.blue = (png_byte)(pow(
  87.928 +                  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
  87.929 +            }
  87.930 +            for (i = 0; i < num_palette; i++)
  87.931 +            {
  87.932 +               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
  87.933 +               {
  87.934 +                  if (png_ptr->trans[i] == 0)
  87.935 +                  {
  87.936 +                     palette[i] = back;
  87.937 +                  }
  87.938 +                  else /* if (png_ptr->trans[i] != 0xff) */
  87.939 +                  {
  87.940 +                     png_byte v, w;
  87.941 +
  87.942 +                     v = png_ptr->gamma_to_1[palette[i].red];
  87.943 +                     png_composite(w, v, png_ptr->trans[i], back_1.red);
  87.944 +                     palette[i].red = png_ptr->gamma_from_1[w];
  87.945 +
  87.946 +                     v = png_ptr->gamma_to_1[palette[i].green];
  87.947 +                     png_composite(w, v, png_ptr->trans[i], back_1.green);
  87.948 +                     palette[i].green = png_ptr->gamma_from_1[w];
  87.949 +
  87.950 +                     v = png_ptr->gamma_to_1[palette[i].blue];
  87.951 +                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
  87.952 +                     palette[i].blue = png_ptr->gamma_from_1[w];
  87.953 +                  }
  87.954 +               }
  87.955 +               else
  87.956 +               {
  87.957 +                  palette[i].red = png_ptr->gamma_table[palette[i].red];
  87.958 +                  palette[i].green = png_ptr->gamma_table[palette[i].green];
  87.959 +                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  87.960 +               }
  87.961 +            }
  87.962 +	    /* Prevent the transformations being done again, and make sure
  87.963 +	     * that the now spurious alpha channel is stripped - the code
  87.964 +	     * has just reduced background composition and gamma correction
  87.965 +	     * to a simple alpha channel strip.
  87.966 +	     */
  87.967 +	    png_ptr->transformations &= ~PNG_BACKGROUND;
  87.968 +	    png_ptr->transformations &= ~PNG_GAMMA;
  87.969 +	    png_ptr->transformations |= PNG_STRIP_ALPHA;
  87.970 +         }
  87.971 +         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
  87.972 +         else
  87.973 +         /* color_type != PNG_COLOR_TYPE_PALETTE */
  87.974 +         {
  87.975 +            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
  87.976 +            double g = 1.0;
  87.977 +            double gs = 1.0;
  87.978 +
  87.979 +            switch (png_ptr->background_gamma_type)
  87.980 +            {
  87.981 +               case PNG_BACKGROUND_GAMMA_SCREEN:
  87.982 +                  g = (png_ptr->screen_gamma);
  87.983 +                  gs = 1.0;
  87.984 +                  break;
  87.985 +               case PNG_BACKGROUND_GAMMA_FILE:
  87.986 +                  g = 1.0 / (png_ptr->gamma);
  87.987 +                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  87.988 +                  break;
  87.989 +               case PNG_BACKGROUND_GAMMA_UNIQUE:
  87.990 +                  g = 1.0 / (png_ptr->background_gamma);
  87.991 +                  gs = 1.0 / (png_ptr->background_gamma *
  87.992 +                     png_ptr->screen_gamma);
  87.993 +                  break;
  87.994 +            }
  87.995 +
  87.996 +            png_ptr->background_1.gray = (png_uint_16)(pow(
  87.997 +               (double)png_ptr->background.gray / m, g) * m + .5);
  87.998 +            png_ptr->background.gray = (png_uint_16)(pow(
  87.999 +               (double)png_ptr->background.gray / m, gs) * m + .5);
 87.1000 +
 87.1001 +            if ((png_ptr->background.red != png_ptr->background.green) ||
 87.1002 +                (png_ptr->background.red != png_ptr->background.blue) ||
 87.1003 +                (png_ptr->background.red != png_ptr->background.gray))
 87.1004 +            {
 87.1005 +               /* RGB or RGBA with color background */
 87.1006 +               png_ptr->background_1.red = (png_uint_16)(pow(
 87.1007 +                  (double)png_ptr->background.red / m, g) * m + .5);
 87.1008 +               png_ptr->background_1.green = (png_uint_16)(pow(
 87.1009 +                  (double)png_ptr->background.green / m, g) * m + .5);
 87.1010 +               png_ptr->background_1.blue = (png_uint_16)(pow(
 87.1011 +                  (double)png_ptr->background.blue / m, g) * m + .5);
 87.1012 +               png_ptr->background.red = (png_uint_16)(pow(
 87.1013 +                  (double)png_ptr->background.red / m, gs) * m + .5);
 87.1014 +               png_ptr->background.green = (png_uint_16)(pow(
 87.1015 +                  (double)png_ptr->background.green / m, gs) * m + .5);
 87.1016 +               png_ptr->background.blue = (png_uint_16)(pow(
 87.1017 +                  (double)png_ptr->background.blue / m, gs) * m + .5);
 87.1018 +            }
 87.1019 +            else
 87.1020 +            {
 87.1021 +               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
 87.1022 +               png_ptr->background_1.red = png_ptr->background_1.green
 87.1023 +                 = png_ptr->background_1.blue = png_ptr->background_1.gray;
 87.1024 +               png_ptr->background.red = png_ptr->background.green
 87.1025 +                 = png_ptr->background.blue = png_ptr->background.gray;
 87.1026 +            }
 87.1027 +         }
 87.1028 +      }
 87.1029 +      else
 87.1030 +      /* transformation does not include PNG_BACKGROUND */
 87.1031 +#endif /* PNG_READ_BACKGROUND_SUPPORTED */
 87.1032 +      if (color_type == PNG_COLOR_TYPE_PALETTE)
 87.1033 +      {
 87.1034 +         png_colorp palette = png_ptr->palette;
 87.1035 +         int num_palette = png_ptr->num_palette;
 87.1036 +         int i;
 87.1037 +
 87.1038 +         for (i = 0; i < num_palette; i++)
 87.1039 +         {
 87.1040 +            palette[i].red = png_ptr->gamma_table[palette[i].red];
 87.1041 +            palette[i].green = png_ptr->gamma_table[palette[i].green];
 87.1042 +            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 87.1043 +         }
 87.1044 +
 87.1045 +	 /* Done the gamma correction. */
 87.1046 +	 png_ptr->transformations &= ~PNG_GAMMA;
 87.1047 +      }
 87.1048 +   }
 87.1049 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1050 +   else
 87.1051 +#endif
 87.1052 +#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
 87.1053 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1054 +   /* No GAMMA transformation */
 87.1055 +   if ((png_ptr->transformations & PNG_BACKGROUND) &&
 87.1056 +       (color_type == PNG_COLOR_TYPE_PALETTE))
 87.1057 +   {
 87.1058 +      int i;
 87.1059 +      int istop = (int)png_ptr->num_trans;
 87.1060 +      png_color back;
 87.1061 +      png_colorp palette = png_ptr->palette;
 87.1062 +
 87.1063 +      back.red   = (png_byte)png_ptr->background.red;
 87.1064 +      back.green = (png_byte)png_ptr->background.green;
 87.1065 +      back.blue  = (png_byte)png_ptr->background.blue;
 87.1066 +
 87.1067 +      for (i = 0; i < istop; i++)
 87.1068 +      {
 87.1069 +         if (png_ptr->trans[i] == 0)
 87.1070 +         {
 87.1071 +            palette[i] = back;
 87.1072 +         }
 87.1073 +         else if (png_ptr->trans[i] != 0xff)
 87.1074 +         {
 87.1075 +            /* The png_composite() macro is defined in png.h */
 87.1076 +            png_composite(palette[i].red, palette[i].red,
 87.1077 +               png_ptr->trans[i], back.red);
 87.1078 +            png_composite(palette[i].green, palette[i].green,
 87.1079 +               png_ptr->trans[i], back.green);
 87.1080 +            png_composite(palette[i].blue, palette[i].blue,
 87.1081 +               png_ptr->trans[i], back.blue);
 87.1082 +         }
 87.1083 +      }
 87.1084 +
 87.1085 +      /* Handled alpha, still need to strip the channel. */
 87.1086 +      png_ptr->transformations &= ~PNG_BACKGROUND;
 87.1087 +      png_ptr->transformations |= PNG_STRIP_ALPHA;
 87.1088 +   }
 87.1089 +#endif /* PNG_READ_BACKGROUND_SUPPORTED */
 87.1090 +
 87.1091 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 87.1092 +   if ((png_ptr->transformations & PNG_SHIFT) &&
 87.1093 +      (color_type == PNG_COLOR_TYPE_PALETTE))
 87.1094 +   {
 87.1095 +      png_uint_16 i;
 87.1096 +      png_uint_16 istop = png_ptr->num_palette;
 87.1097 +      int sr = 8 - png_ptr->sig_bit.red;
 87.1098 +      int sg = 8 - png_ptr->sig_bit.green;
 87.1099 +      int sb = 8 - png_ptr->sig_bit.blue;
 87.1100 +
 87.1101 +      if (sr < 0 || sr > 8)
 87.1102 +         sr = 0;
 87.1103 +      if (sg < 0 || sg > 8)
 87.1104 +         sg = 0;
 87.1105 +      if (sb < 0 || sb > 8)
 87.1106 +         sb = 0;
 87.1107 +      for (i = 0; i < istop; i++)
 87.1108 +      {
 87.1109 +         png_ptr->palette[i].red >>= sr;
 87.1110 +         png_ptr->palette[i].green >>= sg;
 87.1111 +         png_ptr->palette[i].blue >>= sb;
 87.1112 +      }
 87.1113 +   }
 87.1114 +#endif  /* PNG_READ_SHIFT_SUPPORTED */
 87.1115 + }
 87.1116 +#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
 87.1117 + && !defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1118 +   if (png_ptr)
 87.1119 +      return;
 87.1120 +#endif
 87.1121 +}
 87.1122 +
 87.1123 +/* Modify the info structure to reflect the transformations.  The
 87.1124 + * info should be updated so a PNG file could be written with it,
 87.1125 + * assuming the transformations result in valid PNG data.
 87.1126 + */
 87.1127 +void /* PRIVATE */
 87.1128 +png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
 87.1129 +{
 87.1130 +   png_debug(1, "in png_read_transform_info\n");
 87.1131 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 87.1132 +   if (png_ptr->transformations & PNG_EXPAND)
 87.1133 +   {
 87.1134 +      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 87.1135 +      {
 87.1136 +         if (png_ptr->num_trans &&
 87.1137 +              (png_ptr->transformations & PNG_EXPAND_tRNS))
 87.1138 +            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 87.1139 +         else
 87.1140 +            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
 87.1141 +         info_ptr->bit_depth = 8;
 87.1142 +         info_ptr->num_trans = 0;
 87.1143 +      }
 87.1144 +      else
 87.1145 +      {
 87.1146 +         if (png_ptr->num_trans)
 87.1147 +         {
 87.1148 +            if (png_ptr->transformations & PNG_EXPAND_tRNS)
 87.1149 +              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
 87.1150 +#if 0 /* Removed from libpng-1.2.27 */
 87.1151 +            else
 87.1152 +              info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
 87.1153 +#endif
 87.1154 +         }
 87.1155 +         if (info_ptr->bit_depth < 8)
 87.1156 +            info_ptr->bit_depth = 8;
 87.1157 +         info_ptr->num_trans = 0;
 87.1158 +      }
 87.1159 +   }
 87.1160 +#endif
 87.1161 +
 87.1162 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1163 +   if (png_ptr->transformations & PNG_BACKGROUND)
 87.1164 +   {
 87.1165 +      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
 87.1166 +      info_ptr->num_trans = 0;
 87.1167 +      info_ptr->background = png_ptr->background;
 87.1168 +   }
 87.1169 +#endif
 87.1170 +
 87.1171 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.1172 +   if (png_ptr->transformations & PNG_GAMMA)
 87.1173 +   {
 87.1174 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 87.1175 +      info_ptr->gamma = png_ptr->gamma;
 87.1176 +#endif
 87.1177 +#ifdef PNG_FIXED_POINT_SUPPORTED
 87.1178 +      info_ptr->int_gamma = png_ptr->int_gamma;
 87.1179 +#endif
 87.1180 +   }
 87.1181 +#endif
 87.1182 +
 87.1183 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 87.1184 +   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
 87.1185 +      info_ptr->bit_depth = 8;
 87.1186 +#endif
 87.1187 +
 87.1188 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 87.1189 +   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
 87.1190 +      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
 87.1191 +#endif
 87.1192 +
 87.1193 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 87.1194 +   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
 87.1195 +      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
 87.1196 +#endif
 87.1197 +
 87.1198 +#if defined(PNG_READ_DITHER_SUPPORTED)
 87.1199 +   if (png_ptr->transformations & PNG_DITHER)
 87.1200 +   {
 87.1201 +      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
 87.1202 +         (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
 87.1203 +         png_ptr->palette_lookup && info_ptr->bit_depth == 8)
 87.1204 +      {
 87.1205 +         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
 87.1206 +      }
 87.1207 +   }
 87.1208 +#endif
 87.1209 +
 87.1210 +#if defined(PNG_READ_PACK_SUPPORTED)
 87.1211 +   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
 87.1212 +      info_ptr->bit_depth = 8;
 87.1213 +#endif
 87.1214 +
 87.1215 +   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 87.1216 +      info_ptr->channels = 1;
 87.1217 +   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
 87.1218 +      info_ptr->channels = 3;
 87.1219 +   else
 87.1220 +      info_ptr->channels = 1;
 87.1221 +
 87.1222 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 87.1223 +   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
 87.1224 +      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
 87.1225 +#endif
 87.1226 +
 87.1227 +   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
 87.1228 +      info_ptr->channels++;
 87.1229 +
 87.1230 +#if defined(PNG_READ_FILLER_SUPPORTED)
 87.1231 +   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
 87.1232 +   if ((png_ptr->transformations & PNG_FILLER) &&
 87.1233 +       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
 87.1234 +       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
 87.1235 +   {
 87.1236 +      info_ptr->channels++;
 87.1237 +      /* if adding a true alpha channel not just filler */
 87.1238 +#if !defined(PNG_1_0_X)
 87.1239 +      if (png_ptr->transformations & PNG_ADD_ALPHA)
 87.1240 +        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
 87.1241 +#endif
 87.1242 +   }
 87.1243 +#endif
 87.1244 +
 87.1245 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
 87.1246 +defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 87.1247 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 87.1248 +     {
 87.1249 +       if (info_ptr->bit_depth < png_ptr->user_transform_depth)
 87.1250 +         info_ptr->bit_depth = png_ptr->user_transform_depth;
 87.1251 +       if (info_ptr->channels < png_ptr->user_transform_channels)
 87.1252 +         info_ptr->channels = png_ptr->user_transform_channels;
 87.1253 +     }
 87.1254 +#endif
 87.1255 +
 87.1256 +   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
 87.1257 +      info_ptr->bit_depth);
 87.1258 +
 87.1259 +   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
 87.1260 +
 87.1261 +#if !defined(PNG_READ_EXPAND_SUPPORTED)
 87.1262 +   if (png_ptr)
 87.1263 +      return;
 87.1264 +#endif
 87.1265 +}
 87.1266 +
 87.1267 +/* Transform the row.  The order of transformations is significant,
 87.1268 + * and is very touchy.  If you add a transformation, take care to
 87.1269 + * decide how it fits in with the other transformations here.
 87.1270 + */
 87.1271 +void /* PRIVATE */
 87.1272 +png_do_read_transformations(png_structp png_ptr)
 87.1273 +{
 87.1274 +   png_debug(1, "in png_do_read_transformations\n");
 87.1275 +   if (png_ptr->row_buf == NULL)
 87.1276 +   {
 87.1277 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
 87.1278 +      char msg[50];
 87.1279 +
 87.1280 +      png_snprintf2(msg, 50,
 87.1281 +         "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
 87.1282 +         png_ptr->pass);
 87.1283 +      png_error(png_ptr, msg);
 87.1284 +#else
 87.1285 +      png_error(png_ptr, "NULL row buffer");
 87.1286 +#endif
 87.1287 +   }
 87.1288 +#ifdef PNG_WARN_UNINITIALIZED_ROW
 87.1289 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
 87.1290 +      /* Application has failed to call either png_read_start_image()
 87.1291 +       * or png_read_update_info() after setting transforms that expand
 87.1292 +       * pixels.  This check added to libpng-1.2.19 */
 87.1293 +#if (PNG_WARN_UNINITIALIZED_ROW==1)
 87.1294 +      png_error(png_ptr, "Uninitialized row");
 87.1295 +#else
 87.1296 +      png_warning(png_ptr, "Uninitialized row");
 87.1297 +#endif
 87.1298 +#endif
 87.1299 +
 87.1300 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 87.1301 +   if (png_ptr->transformations & PNG_EXPAND)
 87.1302 +   {
 87.1303 +      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
 87.1304 +      {
 87.1305 +         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1306 +            png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
 87.1307 +      }
 87.1308 +      else
 87.1309 +      {
 87.1310 +         if (png_ptr->num_trans &&
 87.1311 +             (png_ptr->transformations & PNG_EXPAND_tRNS))
 87.1312 +            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1313 +               &(png_ptr->trans_values));
 87.1314 +         else
 87.1315 +            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1316 +               NULL);
 87.1317 +      }
 87.1318 +   }
 87.1319 +#endif
 87.1320 +
 87.1321 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 87.1322 +   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
 87.1323 +      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1324 +         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
 87.1325 +#endif
 87.1326 +
 87.1327 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 87.1328 +   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
 87.1329 +   {
 87.1330 +      int rgb_error =
 87.1331 +         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1332 +      if (rgb_error)
 87.1333 +      {
 87.1334 +         png_ptr->rgb_to_gray_status=1;
 87.1335 +         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 
 87.1336 +             PNG_RGB_TO_GRAY_WARN)
 87.1337 +            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
 87.1338 +         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
 87.1339 +             PNG_RGB_TO_GRAY_ERR)
 87.1340 +            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
 87.1341 +      }
 87.1342 +   }
 87.1343 +#endif
 87.1344 +
 87.1345 +/*
 87.1346 +From Andreas Dilger e-mail to png-implement, 26 March 1998:
 87.1347 +
 87.1348 +  In most cases, the "simple transparency" should be done prior to doing
 87.1349 +  gray-to-RGB, or you will have to test 3x as many bytes to check if a
 87.1350 +  pixel is transparent.  You would also need to make sure that the
 87.1351 +  transparency information is upgraded to RGB.
 87.1352 +
 87.1353 +  To summarize, the current flow is:
 87.1354 +  - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
 87.1355 +                                  with background "in place" if transparent,
 87.1356 +                                  convert to RGB if necessary
 87.1357 +  - Gray + alpha -> composite with gray background and remove alpha bytes,
 87.1358 +                                  convert to RGB if necessary
 87.1359 +
 87.1360 +  To support RGB backgrounds for gray images we need:
 87.1361 +  - Gray + simple transparency -> convert to RGB + simple transparency, compare
 87.1362 +                                  3 or 6 bytes and composite with background
 87.1363 +                                  "in place" if transparent (3x compare/pixel
 87.1364 +                                  compared to doing composite with gray bkgrnd)
 87.1365 +  - Gray + alpha -> convert to RGB + alpha, composite with background and
 87.1366 +                                  remove alpha bytes (3x float operations/pixel
 87.1367 +                                  compared with composite on gray background)
 87.1368 +
 87.1369 +  Greg's change will do this.  The reason it wasn't done before is for
 87.1370 +  performance, as this increases the per-pixel operations.  If we would check
 87.1371 +  in advance if the background was gray or RGB, and position the gray-to-RGB
 87.1372 +  transform appropriately, then it would save a lot of work/time.
 87.1373 + */
 87.1374 +
 87.1375 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 87.1376 +   /* if gray -> RGB, do so now only if background is non-gray; else do later
 87.1377 +    * for performance reasons */
 87.1378 +   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
 87.1379 +       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
 87.1380 +      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1381 +#endif
 87.1382 +
 87.1383 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1384 +   if ((png_ptr->transformations & PNG_BACKGROUND) &&
 87.1385 +      ((png_ptr->num_trans != 0 ) ||
 87.1386 +      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
 87.1387 +      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1388 +         &(png_ptr->trans_values), &(png_ptr->background)
 87.1389 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.1390 +         , &(png_ptr->background_1),
 87.1391 +         png_ptr->gamma_table, png_ptr->gamma_from_1,
 87.1392 +         png_ptr->gamma_to_1, png_ptr->gamma_16_table,
 87.1393 +         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
 87.1394 +         png_ptr->gamma_shift
 87.1395 +#endif
 87.1396 +);
 87.1397 +#endif
 87.1398 +
 87.1399 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.1400 +   if ((png_ptr->transformations & PNG_GAMMA) &&
 87.1401 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.1402 +      !((png_ptr->transformations & PNG_BACKGROUND) &&
 87.1403 +      ((png_ptr->num_trans != 0) ||
 87.1404 +      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
 87.1405 +#endif
 87.1406 +      (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
 87.1407 +      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1408 +         png_ptr->gamma_table, png_ptr->gamma_16_table,
 87.1409 +         png_ptr->gamma_shift);
 87.1410 +#endif
 87.1411 +
 87.1412 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 87.1413 +   if (png_ptr->transformations & PNG_16_TO_8)
 87.1414 +      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1415 +#endif
 87.1416 +
 87.1417 +#if defined(PNG_READ_DITHER_SUPPORTED)
 87.1418 +   if (png_ptr->transformations & PNG_DITHER)
 87.1419 +   {
 87.1420 +      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1421 +         png_ptr->palette_lookup, png_ptr->dither_index);
 87.1422 +      if (png_ptr->row_info.rowbytes == (png_uint_32)0)
 87.1423 +         png_error(png_ptr, "png_do_dither returned rowbytes=0");
 87.1424 +   }
 87.1425 +#endif
 87.1426 +
 87.1427 +#if defined(PNG_READ_INVERT_SUPPORTED)
 87.1428 +   if (png_ptr->transformations & PNG_INVERT_MONO)
 87.1429 +      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1430 +#endif
 87.1431 +
 87.1432 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 87.1433 +   if (png_ptr->transformations & PNG_SHIFT)
 87.1434 +      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1435 +         &(png_ptr->shift));
 87.1436 +#endif
 87.1437 +
 87.1438 +#if defined(PNG_READ_PACK_SUPPORTED)
 87.1439 +   if (png_ptr->transformations & PNG_PACK)
 87.1440 +      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1441 +#endif
 87.1442 +
 87.1443 +#if defined(PNG_READ_BGR_SUPPORTED)
 87.1444 +   if (png_ptr->transformations & PNG_BGR)
 87.1445 +      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1446 +#endif
 87.1447 +
 87.1448 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 87.1449 +   if (png_ptr->transformations & PNG_PACKSWAP)
 87.1450 +      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1451 +#endif
 87.1452 +
 87.1453 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 87.1454 +   /* if gray -> RGB, do so now only if we did not do so above */
 87.1455 +   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
 87.1456 +       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
 87.1457 +      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1458 +#endif
 87.1459 +
 87.1460 +#if defined(PNG_READ_FILLER_SUPPORTED)
 87.1461 +   if (png_ptr->transformations & PNG_FILLER)
 87.1462 +      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
 87.1463 +         (png_uint_32)png_ptr->filler, png_ptr->flags);
 87.1464 +#endif
 87.1465 +
 87.1466 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 87.1467 +   if (png_ptr->transformations & PNG_INVERT_ALPHA)
 87.1468 +      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1469 +#endif
 87.1470 +
 87.1471 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 87.1472 +   if (png_ptr->transformations & PNG_SWAP_ALPHA)
 87.1473 +      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1474 +#endif
 87.1475 +
 87.1476 +#if defined(PNG_READ_SWAP_SUPPORTED)
 87.1477 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
 87.1478 +      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
 87.1479 +#endif
 87.1480 +
 87.1481 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 87.1482 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 87.1483 +    {
 87.1484 +      if (png_ptr->read_user_transform_fn != NULL)
 87.1485 +        (*(png_ptr->read_user_transform_fn)) /* user read transform function */
 87.1486 +          (png_ptr,                    /* png_ptr */
 87.1487 +           &(png_ptr->row_info),       /* row_info:     */
 87.1488 +             /*  png_uint_32 width;          width of row */
 87.1489 +             /*  png_uint_32 rowbytes;       number of bytes in row */
 87.1490 +             /*  png_byte color_type;        color type of pixels */
 87.1491 +             /*  png_byte bit_depth;         bit depth of samples */
 87.1492 +             /*  png_byte channels;          number of channels (1-4) */
 87.1493 +             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
 87.1494 +           png_ptr->row_buf + 1);      /* start of pixel data for row */
 87.1495 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 87.1496 +      if (png_ptr->user_transform_depth)
 87.1497 +         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
 87.1498 +      if (png_ptr->user_transform_channels)
 87.1499 +         png_ptr->row_info.channels = png_ptr->user_transform_channels;
 87.1500 +#endif
 87.1501 +      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
 87.1502 +         png_ptr->row_info.channels);
 87.1503 +      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
 87.1504 +         png_ptr->row_info.width);
 87.1505 +   }
 87.1506 +#endif
 87.1507 +
 87.1508 +}
 87.1509 +
 87.1510 +#if defined(PNG_READ_PACK_SUPPORTED)
 87.1511 +/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
 87.1512 + * without changing the actual values.  Thus, if you had a row with
 87.1513 + * a bit depth of 1, you would end up with bytes that only contained
 87.1514 + * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
 87.1515 + * png_do_shift() after this.
 87.1516 + */
 87.1517 +void /* PRIVATE */
 87.1518 +png_do_unpack(png_row_infop row_info, png_bytep row)
 87.1519 +{
 87.1520 +   png_debug(1, "in png_do_unpack\n");
 87.1521 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1522 +   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
 87.1523 +#else
 87.1524 +   if (row_info->bit_depth < 8)
 87.1525 +#endif
 87.1526 +   {
 87.1527 +      png_uint_32 i;
 87.1528 +      png_uint_32 row_width=row_info->width;
 87.1529 +
 87.1530 +      switch (row_info->bit_depth)
 87.1531 +      {
 87.1532 +         case 1:
 87.1533 +         {
 87.1534 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
 87.1535 +            png_bytep dp = row + (png_size_t)row_width - 1;
 87.1536 +            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
 87.1537 +            for (i = 0; i < row_width; i++)
 87.1538 +            {
 87.1539 +               *dp = (png_byte)((*sp >> shift) & 0x01);
 87.1540 +               if (shift == 7)
 87.1541 +               {
 87.1542 +                  shift = 0;
 87.1543 +                  sp--;
 87.1544 +               }
 87.1545 +               else
 87.1546 +                  shift++;
 87.1547 +
 87.1548 +               dp--;
 87.1549 +            }
 87.1550 +            break;
 87.1551 +         }
 87.1552 +         case 2:
 87.1553 +         {
 87.1554 +
 87.1555 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
 87.1556 +            png_bytep dp = row + (png_size_t)row_width - 1;
 87.1557 +            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 87.1558 +            for (i = 0; i < row_width; i++)
 87.1559 +            {
 87.1560 +               *dp = (png_byte)((*sp >> shift) & 0x03);
 87.1561 +               if (shift == 6)
 87.1562 +               {
 87.1563 +                  shift = 0;
 87.1564 +                  sp--;
 87.1565 +               }
 87.1566 +               else
 87.1567 +                  shift += 2;
 87.1568 +
 87.1569 +               dp--;
 87.1570 +            }
 87.1571 +            break;
 87.1572 +         }
 87.1573 +         case 4:
 87.1574 +         {
 87.1575 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
 87.1576 +            png_bytep dp = row + (png_size_t)row_width - 1;
 87.1577 +            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
 87.1578 +            for (i = 0; i < row_width; i++)
 87.1579 +            {
 87.1580 +               *dp = (png_byte)((*sp >> shift) & 0x0f);
 87.1581 +               if (shift == 4)
 87.1582 +               {
 87.1583 +                  shift = 0;
 87.1584 +                  sp--;
 87.1585 +               }
 87.1586 +               else
 87.1587 +                  shift = 4;
 87.1588 +
 87.1589 +               dp--;
 87.1590 +            }
 87.1591 +            break;
 87.1592 +         }
 87.1593 +      }
 87.1594 +      row_info->bit_depth = 8;
 87.1595 +      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
 87.1596 +      row_info->rowbytes = row_width * row_info->channels;
 87.1597 +   }
 87.1598 +}
 87.1599 +#endif
 87.1600 +
 87.1601 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 87.1602 +/* Reverse the effects of png_do_shift.  This routine merely shifts the
 87.1603 + * pixels back to their significant bits values.  Thus, if you have
 87.1604 + * a row of bit depth 8, but only 5 are significant, this will shift
 87.1605 + * the values back to 0 through 31.
 87.1606 + */
 87.1607 +void /* PRIVATE */
 87.1608 +png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
 87.1609 +{
 87.1610 +   png_debug(1, "in png_do_unshift\n");
 87.1611 +   if (
 87.1612 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1613 +       row != NULL && row_info != NULL && sig_bits != NULL &&
 87.1614 +#endif
 87.1615 +       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
 87.1616 +   {
 87.1617 +      int shift[4];
 87.1618 +      int channels = 0;
 87.1619 +      int c;
 87.1620 +      png_uint_16 value = 0;
 87.1621 +      png_uint_32 row_width = row_info->width;
 87.1622 +
 87.1623 +      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
 87.1624 +      {
 87.1625 +         shift[channels++] = row_info->bit_depth - sig_bits->red;
 87.1626 +         shift[channels++] = row_info->bit_depth - sig_bits->green;
 87.1627 +         shift[channels++] = row_info->bit_depth - sig_bits->blue;
 87.1628 +      }
 87.1629 +      else
 87.1630 +      {
 87.1631 +         shift[channels++] = row_info->bit_depth - sig_bits->gray;
 87.1632 +      }
 87.1633 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
 87.1634 +      {
 87.1635 +         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
 87.1636 +      }
 87.1637 +
 87.1638 +      for (c = 0; c < channels; c++)
 87.1639 +      {
 87.1640 +         if (shift[c] <= 0)
 87.1641 +            shift[c] = 0;
 87.1642 +         else
 87.1643 +            value = 1;
 87.1644 +      }
 87.1645 +
 87.1646 +      if (!value)
 87.1647 +         return;
 87.1648 +
 87.1649 +      switch (row_info->bit_depth)
 87.1650 +      {
 87.1651 +         case 2:
 87.1652 +         {
 87.1653 +            png_bytep bp;
 87.1654 +            png_uint_32 i;
 87.1655 +            png_uint_32 istop = row_info->rowbytes;
 87.1656 +
 87.1657 +            for (bp = row, i = 0; i < istop; i++)
 87.1658 +            {
 87.1659 +               *bp >>= 1;
 87.1660 +               *bp++ &= 0x55;
 87.1661 +            }
 87.1662 +            break;
 87.1663 +         }
 87.1664 +         case 4:
 87.1665 +         {
 87.1666 +            png_bytep bp = row;
 87.1667 +            png_uint_32 i;
 87.1668 +            png_uint_32 istop = row_info->rowbytes;
 87.1669 +            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
 87.1670 +               (png_byte)((int)0xf >> shift[0]));
 87.1671 +
 87.1672 +            for (i = 0; i < istop; i++)
 87.1673 +            {
 87.1674 +               *bp >>= shift[0];
 87.1675 +               *bp++ &= mask;
 87.1676 +            }
 87.1677 +            break;
 87.1678 +         }
 87.1679 +         case 8:
 87.1680 +         {
 87.1681 +            png_bytep bp = row;
 87.1682 +            png_uint_32 i;
 87.1683 +            png_uint_32 istop = row_width * channels;
 87.1684 +
 87.1685 +            for (i = 0; i < istop; i++)
 87.1686 +            {
 87.1687 +               *bp++ >>= shift[i%channels];
 87.1688 +            }
 87.1689 +            break;
 87.1690 +         }
 87.1691 +         case 16:
 87.1692 +         {
 87.1693 +            png_bytep bp = row;
 87.1694 +            png_uint_32 i;
 87.1695 +            png_uint_32 istop = channels * row_width;
 87.1696 +
 87.1697 +            for (i = 0; i < istop; i++)
 87.1698 +            {
 87.1699 +               value = (png_uint_16)((*bp << 8) + *(bp + 1));
 87.1700 +               value >>= shift[i%channels];
 87.1701 +               *bp++ = (png_byte)(value >> 8);
 87.1702 +               *bp++ = (png_byte)(value & 0xff);
 87.1703 +            }
 87.1704 +            break;
 87.1705 +         }
 87.1706 +      }
 87.1707 +   }
 87.1708 +}
 87.1709 +#endif
 87.1710 +
 87.1711 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 87.1712 +/* chop rows of bit depth 16 down to 8 */
 87.1713 +void /* PRIVATE */
 87.1714 +png_do_chop(png_row_infop row_info, png_bytep row)
 87.1715 +{
 87.1716 +   png_debug(1, "in png_do_chop\n");
 87.1717 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1718 +   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
 87.1719 +#else
 87.1720 +   if (row_info->bit_depth == 16)
 87.1721 +#endif
 87.1722 +   {
 87.1723 +      png_bytep sp = row;
 87.1724 +      png_bytep dp = row;
 87.1725 +      png_uint_32 i;
 87.1726 +      png_uint_32 istop = row_info->width * row_info->channels;
 87.1727 +
 87.1728 +      for (i = 0; i<istop; i++, sp += 2, dp++)
 87.1729 +      {
 87.1730 +#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
 87.1731 +      /* This does a more accurate scaling of the 16-bit color
 87.1732 +       * value, rather than a simple low-byte truncation.
 87.1733 +       *
 87.1734 +       * What the ideal calculation should be:
 87.1735 +       *   *dp = (((((png_uint_32)(*sp) << 8) |
 87.1736 +       *          (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
 87.1737 +       *
 87.1738 +       * GRR: no, I think this is what it really should be:
 87.1739 +       *   *dp = (((((png_uint_32)(*sp) << 8) |
 87.1740 +       *           (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
 87.1741 +       *
 87.1742 +       * GRR: here's the exact calculation with shifts:
 87.1743 +       *   temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
 87.1744 +       *   *dp = (temp - (temp >> 8)) >> 8;
 87.1745 +       *
 87.1746 +       * Approximate calculation with shift/add instead of multiply/divide:
 87.1747 +       *   *dp = ((((png_uint_32)(*sp) << 8) |
 87.1748 +       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
 87.1749 +       *
 87.1750 +       * What we actually do to avoid extra shifting and conversion:
 87.1751 +       */
 87.1752 +
 87.1753 +         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
 87.1754 +#else
 87.1755 +       /* Simply discard the low order byte */
 87.1756 +         *dp = *sp;
 87.1757 +#endif
 87.1758 +      }
 87.1759 +      row_info->bit_depth = 8;
 87.1760 +      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
 87.1761 +      row_info->rowbytes = row_info->width * row_info->channels;
 87.1762 +   }
 87.1763 +}
 87.1764 +#endif
 87.1765 +
 87.1766 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 87.1767 +void /* PRIVATE */
 87.1768 +png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
 87.1769 +{
 87.1770 +   png_debug(1, "in png_do_read_swap_alpha\n");
 87.1771 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1772 +   if (row != NULL && row_info != NULL)
 87.1773 +#endif
 87.1774 +   {
 87.1775 +      png_uint_32 row_width = row_info->width;
 87.1776 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 87.1777 +      {
 87.1778 +         /* This converts from RGBA to ARGB */
 87.1779 +         if (row_info->bit_depth == 8)
 87.1780 +         {
 87.1781 +            png_bytep sp = row + row_info->rowbytes;
 87.1782 +            png_bytep dp = sp;
 87.1783 +            png_byte save;
 87.1784 +            png_uint_32 i;
 87.1785 +
 87.1786 +            for (i = 0; i < row_width; i++)
 87.1787 +            {
 87.1788 +               save = *(--sp);
 87.1789 +               *(--dp) = *(--sp);
 87.1790 +               *(--dp) = *(--sp);
 87.1791 +               *(--dp) = *(--sp);
 87.1792 +               *(--dp) = save;
 87.1793 +            }
 87.1794 +         }
 87.1795 +         /* This converts from RRGGBBAA to AARRGGBB */
 87.1796 +         else
 87.1797 +         {
 87.1798 +            png_bytep sp = row + row_info->rowbytes;
 87.1799 +            png_bytep dp = sp;
 87.1800 +            png_byte save[2];
 87.1801 +            png_uint_32 i;
 87.1802 +
 87.1803 +            for (i = 0; i < row_width; i++)
 87.1804 +            {
 87.1805 +               save[0] = *(--sp);
 87.1806 +               save[1] = *(--sp);
 87.1807 +               *(--dp) = *(--sp);
 87.1808 +               *(--dp) = *(--sp);
 87.1809 +               *(--dp) = *(--sp);
 87.1810 +               *(--dp) = *(--sp);
 87.1811 +               *(--dp) = *(--sp);
 87.1812 +               *(--dp) = *(--sp);
 87.1813 +               *(--dp) = save[0];
 87.1814 +               *(--dp) = save[1];
 87.1815 +            }
 87.1816 +         }
 87.1817 +      }
 87.1818 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 87.1819 +      {
 87.1820 +         /* This converts from GA to AG */
 87.1821 +         if (row_info->bit_depth == 8)
 87.1822 +         {
 87.1823 +            png_bytep sp = row + row_info->rowbytes;
 87.1824 +            png_bytep dp = sp;
 87.1825 +            png_byte save;
 87.1826 +            png_uint_32 i;
 87.1827 +
 87.1828 +            for (i = 0; i < row_width; i++)
 87.1829 +            {
 87.1830 +               save = *(--sp);
 87.1831 +               *(--dp) = *(--sp);
 87.1832 +               *(--dp) = save;
 87.1833 +            }
 87.1834 +         }
 87.1835 +         /* This converts from GGAA to AAGG */
 87.1836 +         else
 87.1837 +         {
 87.1838 +            png_bytep sp = row + row_info->rowbytes;
 87.1839 +            png_bytep dp = sp;
 87.1840 +            png_byte save[2];
 87.1841 +            png_uint_32 i;
 87.1842 +
 87.1843 +            for (i = 0; i < row_width; i++)
 87.1844 +            {
 87.1845 +               save[0] = *(--sp);
 87.1846 +               save[1] = *(--sp);
 87.1847 +               *(--dp) = *(--sp);
 87.1848 +               *(--dp) = *(--sp);
 87.1849 +               *(--dp) = save[0];
 87.1850 +               *(--dp) = save[1];
 87.1851 +            }
 87.1852 +         }
 87.1853 +      }
 87.1854 +   }
 87.1855 +}
 87.1856 +#endif
 87.1857 +
 87.1858 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 87.1859 +void /* PRIVATE */
 87.1860 +png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
 87.1861 +{
 87.1862 +   png_debug(1, "in png_do_read_invert_alpha\n");
 87.1863 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1864 +   if (row != NULL && row_info != NULL)
 87.1865 +#endif
 87.1866 +   {
 87.1867 +      png_uint_32 row_width = row_info->width;
 87.1868 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 87.1869 +      {
 87.1870 +         /* This inverts the alpha channel in RGBA */
 87.1871 +         if (row_info->bit_depth == 8)
 87.1872 +         {
 87.1873 +            png_bytep sp = row + row_info->rowbytes;
 87.1874 +            png_bytep dp = sp;
 87.1875 +            png_uint_32 i;
 87.1876 +
 87.1877 +            for (i = 0; i < row_width; i++)
 87.1878 +            {
 87.1879 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1880 +
 87.1881 +/*             This does nothing:
 87.1882 +               *(--dp) = *(--sp);
 87.1883 +               *(--dp) = *(--sp);
 87.1884 +               *(--dp) = *(--sp);
 87.1885 +               We can replace it with:
 87.1886 +*/
 87.1887 +               sp-=3;
 87.1888 +               dp=sp;
 87.1889 +            }
 87.1890 +         }
 87.1891 +         /* This inverts the alpha channel in RRGGBBAA */
 87.1892 +         else
 87.1893 +         {
 87.1894 +            png_bytep sp = row + row_info->rowbytes;
 87.1895 +            png_bytep dp = sp;
 87.1896 +            png_uint_32 i;
 87.1897 +
 87.1898 +            for (i = 0; i < row_width; i++)
 87.1899 +            {
 87.1900 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1901 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1902 +
 87.1903 +/*             This does nothing:
 87.1904 +               *(--dp) = *(--sp);
 87.1905 +               *(--dp) = *(--sp);
 87.1906 +               *(--dp) = *(--sp);
 87.1907 +               *(--dp) = *(--sp);
 87.1908 +               *(--dp) = *(--sp);
 87.1909 +               *(--dp) = *(--sp);
 87.1910 +               We can replace it with:
 87.1911 +*/
 87.1912 +               sp-=6;
 87.1913 +               dp=sp;
 87.1914 +            }
 87.1915 +         }
 87.1916 +      }
 87.1917 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 87.1918 +      {
 87.1919 +         /* This inverts the alpha channel in GA */
 87.1920 +         if (row_info->bit_depth == 8)
 87.1921 +         {
 87.1922 +            png_bytep sp = row + row_info->rowbytes;
 87.1923 +            png_bytep dp = sp;
 87.1924 +            png_uint_32 i;
 87.1925 +
 87.1926 +            for (i = 0; i < row_width; i++)
 87.1927 +            {
 87.1928 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1929 +               *(--dp) = *(--sp);
 87.1930 +            }
 87.1931 +         }
 87.1932 +         /* This inverts the alpha channel in GGAA */
 87.1933 +         else
 87.1934 +         {
 87.1935 +            png_bytep sp  = row + row_info->rowbytes;
 87.1936 +            png_bytep dp = sp;
 87.1937 +            png_uint_32 i;
 87.1938 +
 87.1939 +            for (i = 0; i < row_width; i++)
 87.1940 +            {
 87.1941 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1942 +               *(--dp) = (png_byte)(255 - *(--sp));
 87.1943 +/*
 87.1944 +               *(--dp) = *(--sp);
 87.1945 +               *(--dp) = *(--sp);
 87.1946 +*/
 87.1947 +               sp-=2;
 87.1948 +               dp=sp;
 87.1949 +            }
 87.1950 +         }
 87.1951 +      }
 87.1952 +   }
 87.1953 +}
 87.1954 +#endif
 87.1955 +
 87.1956 +#if defined(PNG_READ_FILLER_SUPPORTED)
 87.1957 +/* Add filler channel if we have RGB color */
 87.1958 +void /* PRIVATE */
 87.1959 +png_do_read_filler(png_row_infop row_info, png_bytep row,
 87.1960 +   png_uint_32 filler, png_uint_32 flags)
 87.1961 +{
 87.1962 +   png_uint_32 i;
 87.1963 +   png_uint_32 row_width = row_info->width;
 87.1964 +
 87.1965 +   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
 87.1966 +   png_byte lo_filler = (png_byte)(filler & 0xff);
 87.1967 +
 87.1968 +   png_debug(1, "in png_do_read_filler\n");
 87.1969 +   if (
 87.1970 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.1971 +       row != NULL  && row_info != NULL &&
 87.1972 +#endif
 87.1973 +       row_info->color_type == PNG_COLOR_TYPE_GRAY)
 87.1974 +   {
 87.1975 +      if (row_info->bit_depth == 8)
 87.1976 +      {
 87.1977 +         /* This changes the data from G to GX */
 87.1978 +         if (flags & PNG_FLAG_FILLER_AFTER)
 87.1979 +         {
 87.1980 +            png_bytep sp = row + (png_size_t)row_width;
 87.1981 +            png_bytep dp =  sp + (png_size_t)row_width;
 87.1982 +            for (i = 1; i < row_width; i++)
 87.1983 +            {
 87.1984 +               *(--dp) = lo_filler;
 87.1985 +               *(--dp) = *(--sp);
 87.1986 +            }
 87.1987 +            *(--dp) = lo_filler;
 87.1988 +            row_info->channels = 2;
 87.1989 +            row_info->pixel_depth = 16;
 87.1990 +            row_info->rowbytes = row_width * 2;
 87.1991 +         }
 87.1992 +      /* This changes the data from G to XG */
 87.1993 +         else
 87.1994 +         {
 87.1995 +            png_bytep sp = row + (png_size_t)row_width;
 87.1996 +            png_bytep dp = sp  + (png_size_t)row_width;
 87.1997 +            for (i = 0; i < row_width; i++)
 87.1998 +            {
 87.1999 +               *(--dp) = *(--sp);
 87.2000 +               *(--dp) = lo_filler;
 87.2001 +            }
 87.2002 +            row_info->channels = 2;
 87.2003 +            row_info->pixel_depth = 16;
 87.2004 +            row_info->rowbytes = row_width * 2;
 87.2005 +         }
 87.2006 +      }
 87.2007 +      else if (row_info->bit_depth == 16)
 87.2008 +      {
 87.2009 +         /* This changes the data from GG to GGXX */
 87.2010 +         if (flags & PNG_FLAG_FILLER_AFTER)
 87.2011 +         {
 87.2012 +            png_bytep sp = row + (png_size_t)row_width * 2;
 87.2013 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2014 +            for (i = 1; i < row_width; i++)
 87.2015 +            {
 87.2016 +               *(--dp) = hi_filler;
 87.2017 +               *(--dp) = lo_filler;
 87.2018 +               *(--dp) = *(--sp);
 87.2019 +               *(--dp) = *(--sp);
 87.2020 +            }
 87.2021 +            *(--dp) = hi_filler;
 87.2022 +            *(--dp) = lo_filler;
 87.2023 +            row_info->channels = 2;
 87.2024 +            row_info->pixel_depth = 32;
 87.2025 +            row_info->rowbytes = row_width * 4;
 87.2026 +         }
 87.2027 +         /* This changes the data from GG to XXGG */
 87.2028 +         else
 87.2029 +         {
 87.2030 +            png_bytep sp = row + (png_size_t)row_width * 2;
 87.2031 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2032 +            for (i = 0; i < row_width; i++)
 87.2033 +            {
 87.2034 +               *(--dp) = *(--sp);
 87.2035 +               *(--dp) = *(--sp);
 87.2036 +               *(--dp) = hi_filler;
 87.2037 +               *(--dp) = lo_filler;
 87.2038 +            }
 87.2039 +            row_info->channels = 2;
 87.2040 +            row_info->pixel_depth = 32;
 87.2041 +            row_info->rowbytes = row_width * 4;
 87.2042 +         }
 87.2043 +      }
 87.2044 +   } /* COLOR_TYPE == GRAY */
 87.2045 +   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 87.2046 +   {
 87.2047 +      if (row_info->bit_depth == 8)
 87.2048 +      {
 87.2049 +         /* This changes the data from RGB to RGBX */
 87.2050 +         if (flags & PNG_FLAG_FILLER_AFTER)
 87.2051 +         {
 87.2052 +            png_bytep sp = row + (png_size_t)row_width * 3;
 87.2053 +            png_bytep dp = sp  + (png_size_t)row_width;
 87.2054 +            for (i = 1; i < row_width; i++)
 87.2055 +            {
 87.2056 +               *(--dp) = lo_filler;
 87.2057 +               *(--dp) = *(--sp);
 87.2058 +               *(--dp) = *(--sp);
 87.2059 +               *(--dp) = *(--sp);
 87.2060 +            }
 87.2061 +            *(--dp) = lo_filler;
 87.2062 +            row_info->channels = 4;
 87.2063 +            row_info->pixel_depth = 32;
 87.2064 +            row_info->rowbytes = row_width * 4;
 87.2065 +         }
 87.2066 +      /* This changes the data from RGB to XRGB */
 87.2067 +         else
 87.2068 +         {
 87.2069 +            png_bytep sp = row + (png_size_t)row_width * 3;
 87.2070 +            png_bytep dp = sp + (png_size_t)row_width;
 87.2071 +            for (i = 0; i < row_width; i++)
 87.2072 +            {
 87.2073 +               *(--dp) = *(--sp);
 87.2074 +               *(--dp) = *(--sp);
 87.2075 +               *(--dp) = *(--sp);
 87.2076 +               *(--dp) = lo_filler;
 87.2077 +            }
 87.2078 +            row_info->channels = 4;
 87.2079 +            row_info->pixel_depth = 32;
 87.2080 +            row_info->rowbytes = row_width * 4;
 87.2081 +         }
 87.2082 +      }
 87.2083 +      else if (row_info->bit_depth == 16)
 87.2084 +      {
 87.2085 +         /* This changes the data from RRGGBB to RRGGBBXX */
 87.2086 +         if (flags & PNG_FLAG_FILLER_AFTER)
 87.2087 +         {
 87.2088 +            png_bytep sp = row + (png_size_t)row_width * 6;
 87.2089 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2090 +            for (i = 1; i < row_width; i++)
 87.2091 +            {
 87.2092 +               *(--dp) = hi_filler;
 87.2093 +               *(--dp) = lo_filler;
 87.2094 +               *(--dp) = *(--sp);
 87.2095 +               *(--dp) = *(--sp);
 87.2096 +               *(--dp) = *(--sp);
 87.2097 +               *(--dp) = *(--sp);
 87.2098 +               *(--dp) = *(--sp);
 87.2099 +               *(--dp) = *(--sp);
 87.2100 +            }
 87.2101 +            *(--dp) = hi_filler;
 87.2102 +            *(--dp) = lo_filler;
 87.2103 +            row_info->channels = 4;
 87.2104 +            row_info->pixel_depth = 64;
 87.2105 +            row_info->rowbytes = row_width * 8;
 87.2106 +         }
 87.2107 +         /* This changes the data from RRGGBB to XXRRGGBB */
 87.2108 +         else
 87.2109 +         {
 87.2110 +            png_bytep sp = row + (png_size_t)row_width * 6;
 87.2111 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2112 +            for (i = 0; i < row_width; i++)
 87.2113 +            {
 87.2114 +               *(--dp) = *(--sp);
 87.2115 +               *(--dp) = *(--sp);
 87.2116 +               *(--dp) = *(--sp);
 87.2117 +               *(--dp) = *(--sp);
 87.2118 +               *(--dp) = *(--sp);
 87.2119 +               *(--dp) = *(--sp);
 87.2120 +               *(--dp) = hi_filler;
 87.2121 +               *(--dp) = lo_filler;
 87.2122 +            }
 87.2123 +            row_info->channels = 4;
 87.2124 +            row_info->pixel_depth = 64;
 87.2125 +            row_info->rowbytes = row_width * 8;
 87.2126 +         }
 87.2127 +      }
 87.2128 +   } /* COLOR_TYPE == RGB */
 87.2129 +}
 87.2130 +#endif
 87.2131 +
 87.2132 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 87.2133 +/* expand grayscale files to RGB, with or without alpha */
 87.2134 +void /* PRIVATE */
 87.2135 +png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
 87.2136 +{
 87.2137 +   png_uint_32 i;
 87.2138 +   png_uint_32 row_width = row_info->width;
 87.2139 +
 87.2140 +   png_debug(1, "in png_do_gray_to_rgb\n");
 87.2141 +   if (row_info->bit_depth >= 8 &&
 87.2142 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.2143 +       row != NULL && row_info != NULL &&
 87.2144 +#endif
 87.2145 +      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
 87.2146 +   {
 87.2147 +      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
 87.2148 +      {
 87.2149 +         if (row_info->bit_depth == 8)
 87.2150 +         {
 87.2151 +            png_bytep sp = row + (png_size_t)row_width - 1;
 87.2152 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2153 +            for (i = 0; i < row_width; i++)
 87.2154 +            {
 87.2155 +               *(dp--) = *sp;
 87.2156 +               *(dp--) = *sp;
 87.2157 +               *(dp--) = *(sp--);
 87.2158 +            }
 87.2159 +         }
 87.2160 +         else
 87.2161 +         {
 87.2162 +            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
 87.2163 +            png_bytep dp = sp  + (png_size_t)row_width * 4;
 87.2164 +            for (i = 0; i < row_width; i++)
 87.2165 +            {
 87.2166 +               *(dp--) = *sp;
 87.2167 +               *(dp--) = *(sp - 1);
 87.2168 +               *(dp--) = *sp;
 87.2169 +               *(dp--) = *(sp - 1);
 87.2170 +               *(dp--) = *(sp--);
 87.2171 +               *(dp--) = *(sp--);
 87.2172 +            }
 87.2173 +         }
 87.2174 +      }
 87.2175 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 87.2176 +      {
 87.2177 +         if (row_info->bit_depth == 8)
 87.2178 +         {
 87.2179 +            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
 87.2180 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 87.2181 +            for (i = 0; i < row_width; i++)
 87.2182 +            {
 87.2183 +               *(dp--) = *(sp--);
 87.2184 +               *(dp--) = *sp;
 87.2185 +               *(dp--) = *sp;
 87.2186 +               *(dp--) = *(sp--);
 87.2187 +            }
 87.2188 +         }
 87.2189 +         else
 87.2190 +         {
 87.2191 +            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
 87.2192 +            png_bytep dp = sp  + (png_size_t)row_width * 4;
 87.2193 +            for (i = 0; i < row_width; i++)
 87.2194 +            {
 87.2195 +               *(dp--) = *(sp--);
 87.2196 +               *(dp--) = *(sp--);
 87.2197 +               *(dp--) = *sp;
 87.2198 +               *(dp--) = *(sp - 1);
 87.2199 +               *(dp--) = *sp;
 87.2200 +               *(dp--) = *(sp - 1);
 87.2201 +               *(dp--) = *(sp--);
 87.2202 +               *(dp--) = *(sp--);
 87.2203 +            }
 87.2204 +         }
 87.2205 +      }
 87.2206 +      row_info->channels += (png_byte)2;
 87.2207 +      row_info->color_type |= PNG_COLOR_MASK_COLOR;
 87.2208 +      row_info->pixel_depth = (png_byte)(row_info->channels *
 87.2209 +         row_info->bit_depth);
 87.2210 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.2211 +   }
 87.2212 +}
 87.2213 +#endif
 87.2214 +
 87.2215 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 87.2216 +/* reduce RGB files to grayscale, with or without alpha
 87.2217 + * using the equation given in Poynton's ColorFAQ at
 87.2218 + * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
 87.2219 + * New link:
 87.2220 + * <http://www.poynton.com/notes/colour_and_gamma/>
 87.2221 + * Charles Poynton poynton at poynton.com
 87.2222 + *
 87.2223 + *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
 87.2224 + *
 87.2225 + *  We approximate this with
 87.2226 + *
 87.2227 + *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
 87.2228 + *
 87.2229 + *  which can be expressed with integers as
 87.2230 + *
 87.2231 + *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
 87.2232 + *
 87.2233 + *  The calculation is to be done in a linear colorspace.
 87.2234 + *
 87.2235 + *  Other integer coefficents can be used via png_set_rgb_to_gray().
 87.2236 + */
 87.2237 +int /* PRIVATE */
 87.2238 +png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
 87.2239 +
 87.2240 +{
 87.2241 +   png_uint_32 i;
 87.2242 +
 87.2243 +   png_uint_32 row_width = row_info->width;
 87.2244 +   int rgb_error = 0;
 87.2245 +
 87.2246 +   png_debug(1, "in png_do_rgb_to_gray\n");
 87.2247 +   if (
 87.2248 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.2249 +       row != NULL && row_info != NULL &&
 87.2250 +#endif
 87.2251 +      (row_info->color_type & PNG_COLOR_MASK_COLOR))
 87.2252 +   {
 87.2253 +      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
 87.2254 +      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
 87.2255 +      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
 87.2256 +
 87.2257 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 87.2258 +      {
 87.2259 +         if (row_info->bit_depth == 8)
 87.2260 +         {
 87.2261 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2262 +            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
 87.2263 +            {
 87.2264 +               png_bytep sp = row;
 87.2265 +               png_bytep dp = row;
 87.2266 +
 87.2267 +               for (i = 0; i < row_width; i++)
 87.2268 +               {
 87.2269 +                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
 87.2270 +                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
 87.2271 +                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
 87.2272 +                  if (red != green || red != blue)
 87.2273 +                  {
 87.2274 +                     rgb_error |= 1;
 87.2275 +                     *(dp++) = png_ptr->gamma_from_1[
 87.2276 +                       (rc*red + gc*green + bc*blue)>>15];
 87.2277 +                  }
 87.2278 +                  else
 87.2279 +                     *(dp++) = *(sp - 1);
 87.2280 +               }
 87.2281 +            }
 87.2282 +            else
 87.2283 +#endif
 87.2284 +            {
 87.2285 +               png_bytep sp = row;
 87.2286 +               png_bytep dp = row;
 87.2287 +               for (i = 0; i < row_width; i++)
 87.2288 +               {
 87.2289 +                  png_byte red   = *(sp++);
 87.2290 +                  png_byte green = *(sp++);
 87.2291 +                  png_byte blue  = *(sp++);
 87.2292 +                  if (red != green || red != blue)
 87.2293 +                  {
 87.2294 +                     rgb_error |= 1;
 87.2295 +                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
 87.2296 +                  }
 87.2297 +                  else
 87.2298 +                     *(dp++) = *(sp - 1);
 87.2299 +               }
 87.2300 +            }
 87.2301 +         }
 87.2302 +
 87.2303 +         else /* RGB bit_depth == 16 */
 87.2304 +         {
 87.2305 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2306 +            if (png_ptr->gamma_16_to_1 != NULL &&
 87.2307 +                png_ptr->gamma_16_from_1 != NULL)
 87.2308 +            {
 87.2309 +               png_bytep sp = row;
 87.2310 +               png_bytep dp = row;
 87.2311 +               for (i = 0; i < row_width; i++)
 87.2312 +               {
 87.2313 +                  png_uint_16 red, green, blue, w;
 87.2314 +
 87.2315 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2316 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2317 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2318 +
 87.2319 +                  if (red == green && red == blue)
 87.2320 +                     w = red;
 87.2321 +                  else
 87.2322 +                  {
 87.2323 +                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
 87.2324 +                                  png_ptr->gamma_shift][red>>8];
 87.2325 +                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
 87.2326 +                                  png_ptr->gamma_shift][green>>8];
 87.2327 +                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
 87.2328 +                                  png_ptr->gamma_shift][blue>>8];
 87.2329 +                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
 87.2330 +                                  + bc*blue_1)>>15);
 87.2331 +                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
 87.2332 +                         png_ptr->gamma_shift][gray16 >> 8];
 87.2333 +                     rgb_error |= 1;
 87.2334 +                  }
 87.2335 +
 87.2336 +                  *(dp++) = (png_byte)((w>>8) & 0xff);
 87.2337 +                  *(dp++) = (png_byte)(w & 0xff);
 87.2338 +               }
 87.2339 +            }
 87.2340 +            else
 87.2341 +#endif
 87.2342 +            {
 87.2343 +               png_bytep sp = row;
 87.2344 +               png_bytep dp = row;
 87.2345 +               for (i = 0; i < row_width; i++)
 87.2346 +               {
 87.2347 +                  png_uint_16 red, green, blue, gray16;
 87.2348 +
 87.2349 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2350 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2351 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2352 +
 87.2353 +                  if (red != green || red != blue)
 87.2354 +                     rgb_error |= 1;
 87.2355 +                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
 87.2356 +                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
 87.2357 +                  *(dp++) = (png_byte)(gray16 & 0xff);
 87.2358 +               }
 87.2359 +            }
 87.2360 +         }
 87.2361 +      }
 87.2362 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 87.2363 +      {
 87.2364 +         if (row_info->bit_depth == 8)
 87.2365 +         {
 87.2366 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2367 +            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
 87.2368 +            {
 87.2369 +               png_bytep sp = row;
 87.2370 +               png_bytep dp = row;
 87.2371 +               for (i = 0; i < row_width; i++)
 87.2372 +               {
 87.2373 +                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
 87.2374 +                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
 87.2375 +                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
 87.2376 +                  if (red != green || red != blue)
 87.2377 +                     rgb_error |= 1;
 87.2378 +                  *(dp++) =  png_ptr->gamma_from_1
 87.2379 +                             [(rc*red + gc*green + bc*blue)>>15];
 87.2380 +                  *(dp++) = *(sp++);  /* alpha */
 87.2381 +               }
 87.2382 +            }
 87.2383 +            else
 87.2384 +#endif
 87.2385 +            {
 87.2386 +               png_bytep sp = row;
 87.2387 +               png_bytep dp = row;
 87.2388 +               for (i = 0; i < row_width; i++)
 87.2389 +               {
 87.2390 +                  png_byte red   = *(sp++);
 87.2391 +                  png_byte green = *(sp++);
 87.2392 +                  png_byte blue  = *(sp++);
 87.2393 +                  if (red != green || red != blue)
 87.2394 +                     rgb_error |= 1;
 87.2395 +                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
 87.2396 +                  *(dp++) = *(sp++);  /* alpha */
 87.2397 +               }
 87.2398 +            }
 87.2399 +         }
 87.2400 +         else /* RGBA bit_depth == 16 */
 87.2401 +         {
 87.2402 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2403 +            if (png_ptr->gamma_16_to_1 != NULL &&
 87.2404 +                png_ptr->gamma_16_from_1 != NULL)
 87.2405 +            {
 87.2406 +               png_bytep sp = row;
 87.2407 +               png_bytep dp = row;
 87.2408 +               for (i = 0; i < row_width; i++)
 87.2409 +               {
 87.2410 +                  png_uint_16 red, green, blue, w;
 87.2411 +
 87.2412 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2413 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2414 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 87.2415 +
 87.2416 +                  if (red == green && red == blue)
 87.2417 +                     w = red;
 87.2418 +                  else
 87.2419 +                  {
 87.2420 +                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
 87.2421 +                                  png_ptr->gamma_shift][red>>8];
 87.2422 +                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
 87.2423 +                                  png_ptr->gamma_shift][green>>8];
 87.2424 +                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
 87.2425 +                                  png_ptr->gamma_shift][blue>>8];
 87.2426 +                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
 87.2427 +                                  + gc * green_1 + bc * blue_1)>>15);
 87.2428 +                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
 87.2429 +                         png_ptr->gamma_shift][gray16 >> 8];
 87.2430 +                     rgb_error |= 1;
 87.2431 +                  }
 87.2432 +
 87.2433 +                  *(dp++) = (png_byte)((w>>8) & 0xff);
 87.2434 +                  *(dp++) = (png_byte)(w & 0xff);
 87.2435 +                  *(dp++) = *(sp++);  /* alpha */
 87.2436 +                  *(dp++) = *(sp++);
 87.2437 +               }
 87.2438 +            }
 87.2439 +            else
 87.2440 +#endif
 87.2441 +            {
 87.2442 +               png_bytep sp = row;
 87.2443 +               png_bytep dp = row;
 87.2444 +               for (i = 0; i < row_width; i++)
 87.2445 +               {
 87.2446 +                  png_uint_16 red, green, blue, gray16;
 87.2447 +                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 87.2448 +                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 87.2449 +                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 87.2450 +                  if (red != green || red != blue)
 87.2451 +                     rgb_error |= 1;
 87.2452 +                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
 87.2453 +                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
 87.2454 +                  *(dp++) = (png_byte)(gray16 & 0xff);
 87.2455 +                  *(dp++) = *(sp++);  /* alpha */
 87.2456 +                  *(dp++) = *(sp++);
 87.2457 +               }
 87.2458 +            }
 87.2459 +         }
 87.2460 +      }
 87.2461 +   row_info->channels -= (png_byte)2;
 87.2462 +      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
 87.2463 +      row_info->pixel_depth = (png_byte)(row_info->channels *
 87.2464 +         row_info->bit_depth);
 87.2465 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.2466 +   }
 87.2467 +   return rgb_error;
 87.2468 +}
 87.2469 +#endif
 87.2470 +
 87.2471 +/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
 87.2472 + * large of png_color.  This lets grayscale images be treated as
 87.2473 + * paletted.  Most useful for gamma correction and simplification
 87.2474 + * of code.
 87.2475 + */
 87.2476 +void PNGAPI
 87.2477 +png_build_grayscale_palette(int bit_depth, png_colorp palette)
 87.2478 +{
 87.2479 +   int num_palette;
 87.2480 +   int color_inc;
 87.2481 +   int i;
 87.2482 +   int v;
 87.2483 +
 87.2484 +   png_debug(1, "in png_do_build_grayscale_palette\n");
 87.2485 +   if (palette == NULL)
 87.2486 +      return;
 87.2487 +
 87.2488 +   switch (bit_depth)
 87.2489 +   {
 87.2490 +      case 1:
 87.2491 +         num_palette = 2;
 87.2492 +         color_inc = 0xff;
 87.2493 +         break;
 87.2494 +      case 2:
 87.2495 +         num_palette = 4;
 87.2496 +         color_inc = 0x55;
 87.2497 +         break;
 87.2498 +      case 4:
 87.2499 +         num_palette = 16;
 87.2500 +         color_inc = 0x11;
 87.2501 +         break;
 87.2502 +      case 8:
 87.2503 +         num_palette = 256;
 87.2504 +         color_inc = 1;
 87.2505 +         break;
 87.2506 +      default:
 87.2507 +         num_palette = 0;
 87.2508 +         color_inc = 0;
 87.2509 +         break;
 87.2510 +   }
 87.2511 +
 87.2512 +   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
 87.2513 +   {
 87.2514 +      palette[i].red = (png_byte)v;
 87.2515 +      palette[i].green = (png_byte)v;
 87.2516 +      palette[i].blue = (png_byte)v;
 87.2517 +   }
 87.2518 +}
 87.2519 +
 87.2520 +/* This function is currently unused.  Do we really need it? */
 87.2521 +#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
 87.2522 +void /* PRIVATE */
 87.2523 +png_correct_palette(png_structp png_ptr, png_colorp palette,
 87.2524 +   int num_palette)
 87.2525 +{
 87.2526 +   png_debug(1, "in png_correct_palette\n");
 87.2527 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
 87.2528 +    defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
 87.2529 +   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
 87.2530 +   {
 87.2531 +      png_color back, back_1;
 87.2532 +
 87.2533 +      if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
 87.2534 +      {
 87.2535 +         back.red = png_ptr->gamma_table[png_ptr->background.red];
 87.2536 +         back.green = png_ptr->gamma_table[png_ptr->background.green];
 87.2537 +         back.blue = png_ptr->gamma_table[png_ptr->background.blue];
 87.2538 +
 87.2539 +         back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
 87.2540 +         back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
 87.2541 +         back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
 87.2542 +      }
 87.2543 +      else
 87.2544 +      {
 87.2545 +         double g;
 87.2546 +
 87.2547 +         g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
 87.2548 +
 87.2549 +         if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
 87.2550 +             fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
 87.2551 +         {
 87.2552 +            back.red = png_ptr->background.red;
 87.2553 +            back.green = png_ptr->background.green;
 87.2554 +            back.blue = png_ptr->background.blue;
 87.2555 +         }
 87.2556 +         else
 87.2557 +         {
 87.2558 +            back.red =
 87.2559 +               (png_byte)(pow((double)png_ptr->background.red/255, g) *
 87.2560 +                255.0 + 0.5);
 87.2561 +            back.green =
 87.2562 +               (png_byte)(pow((double)png_ptr->background.green/255, g) *
 87.2563 +                255.0 + 0.5);
 87.2564 +            back.blue =
 87.2565 +               (png_byte)(pow((double)png_ptr->background.blue/255, g) *
 87.2566 +                255.0 + 0.5);
 87.2567 +         }
 87.2568 +
 87.2569 +         g = 1.0 / png_ptr->background_gamma;
 87.2570 +
 87.2571 +         back_1.red =
 87.2572 +            (png_byte)(pow((double)png_ptr->background.red/255, g) *
 87.2573 +             255.0 + 0.5);
 87.2574 +         back_1.green =
 87.2575 +            (png_byte)(pow((double)png_ptr->background.green/255, g) *
 87.2576 +             255.0 + 0.5);
 87.2577 +         back_1.blue =
 87.2578 +            (png_byte)(pow((double)png_ptr->background.blue/255, g) *
 87.2579 +             255.0 + 0.5);
 87.2580 +      }
 87.2581 +
 87.2582 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 87.2583 +      {
 87.2584 +         png_uint_32 i;
 87.2585 +
 87.2586 +         for (i = 0; i < (png_uint_32)num_palette; i++)
 87.2587 +         {
 87.2588 +            if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
 87.2589 +            {
 87.2590 +               palette[i] = back;
 87.2591 +            }
 87.2592 +            else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
 87.2593 +            {
 87.2594 +               png_byte v, w;
 87.2595 +
 87.2596 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
 87.2597 +               png_composite(w, v, png_ptr->trans[i], back_1.red);
 87.2598 +               palette[i].red = png_ptr->gamma_from_1[w];
 87.2599 +
 87.2600 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
 87.2601 +               png_composite(w, v, png_ptr->trans[i], back_1.green);
 87.2602 +               palette[i].green = png_ptr->gamma_from_1[w];
 87.2603 +
 87.2604 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
 87.2605 +               png_composite(w, v, png_ptr->trans[i], back_1.blue);
 87.2606 +               palette[i].blue = png_ptr->gamma_from_1[w];
 87.2607 +            }
 87.2608 +            else
 87.2609 +            {
 87.2610 +               palette[i].red = png_ptr->gamma_table[palette[i].red];
 87.2611 +               palette[i].green = png_ptr->gamma_table[palette[i].green];
 87.2612 +               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 87.2613 +            }
 87.2614 +         }
 87.2615 +      }
 87.2616 +      else
 87.2617 +      {
 87.2618 +         int i;
 87.2619 +
 87.2620 +         for (i = 0; i < num_palette; i++)
 87.2621 +         {
 87.2622 +            if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
 87.2623 +            {
 87.2624 +               palette[i] = back;
 87.2625 +            }
 87.2626 +            else
 87.2627 +            {
 87.2628 +               palette[i].red = png_ptr->gamma_table[palette[i].red];
 87.2629 +               palette[i].green = png_ptr->gamma_table[palette[i].green];
 87.2630 +               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 87.2631 +            }
 87.2632 +         }
 87.2633 +      }
 87.2634 +   }
 87.2635 +   else
 87.2636 +#endif
 87.2637 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2638 +   if (png_ptr->transformations & PNG_GAMMA)
 87.2639 +   {
 87.2640 +      int i;
 87.2641 +
 87.2642 +      for (i = 0; i < num_palette; i++)
 87.2643 +      {
 87.2644 +         palette[i].red = png_ptr->gamma_table[palette[i].red];
 87.2645 +         palette[i].green = png_ptr->gamma_table[palette[i].green];
 87.2646 +         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 87.2647 +      }
 87.2648 +   }
 87.2649 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2650 +   else
 87.2651 +#endif
 87.2652 +#endif
 87.2653 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2654 +   if (png_ptr->transformations & PNG_BACKGROUND)
 87.2655 +   {
 87.2656 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 87.2657 +      {
 87.2658 +         png_color back;
 87.2659 +
 87.2660 +         back.red   = (png_byte)png_ptr->background.red;
 87.2661 +         back.green = (png_byte)png_ptr->background.green;
 87.2662 +         back.blue  = (png_byte)png_ptr->background.blue;
 87.2663 +
 87.2664 +         for (i = 0; i < (int)png_ptr->num_trans; i++)
 87.2665 +         {
 87.2666 +            if (png_ptr->trans[i] == 0)
 87.2667 +            {
 87.2668 +               palette[i].red = back.red;
 87.2669 +               palette[i].green = back.green;
 87.2670 +               palette[i].blue = back.blue;
 87.2671 +            }
 87.2672 +            else if (png_ptr->trans[i] != 0xff)
 87.2673 +            {
 87.2674 +               png_composite(palette[i].red, png_ptr->palette[i].red,
 87.2675 +                  png_ptr->trans[i], back.red);
 87.2676 +               png_composite(palette[i].green, png_ptr->palette[i].green,
 87.2677 +                  png_ptr->trans[i], back.green);
 87.2678 +               png_composite(palette[i].blue, png_ptr->palette[i].blue,
 87.2679 +                  png_ptr->trans[i], back.blue);
 87.2680 +            }
 87.2681 +         }
 87.2682 +      }
 87.2683 +      else /* assume grayscale palette (what else could it be?) */
 87.2684 +      {
 87.2685 +         int i;
 87.2686 +
 87.2687 +         for (i = 0; i < num_palette; i++)
 87.2688 +         {
 87.2689 +            if (i == (png_byte)png_ptr->trans_values.gray)
 87.2690 +            {
 87.2691 +               palette[i].red = (png_byte)png_ptr->background.red;
 87.2692 +               palette[i].green = (png_byte)png_ptr->background.green;
 87.2693 +               palette[i].blue = (png_byte)png_ptr->background.blue;
 87.2694 +            }
 87.2695 +         }
 87.2696 +      }
 87.2697 +   }
 87.2698 +#endif
 87.2699 +}
 87.2700 +#endif
 87.2701 +
 87.2702 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 87.2703 +/* Replace any alpha or transparency with the supplied background color.
 87.2704 + * "background" is already in the screen gamma, while "background_1" is
 87.2705 + * at a gamma of 1.0.  Paletted files have already been taken care of.
 87.2706 + */
 87.2707 +void /* PRIVATE */
 87.2708 +png_do_background(png_row_infop row_info, png_bytep row,
 87.2709 +   png_color_16p trans_values, png_color_16p background
 87.2710 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2711 +   , png_color_16p background_1,
 87.2712 +   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
 87.2713 +   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
 87.2714 +   png_uint_16pp gamma_16_to_1, int gamma_shift
 87.2715 +#endif
 87.2716 +   )
 87.2717 +{
 87.2718 +   png_bytep sp, dp;
 87.2719 +   png_uint_32 i;
 87.2720 +   png_uint_32 row_width=row_info->width;
 87.2721 +   int shift;
 87.2722 +
 87.2723 +   png_debug(1, "in png_do_background\n");
 87.2724 +   if (background != NULL &&
 87.2725 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.2726 +       row != NULL && row_info != NULL &&
 87.2727 +#endif
 87.2728 +      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
 87.2729 +      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
 87.2730 +   {
 87.2731 +      switch (row_info->color_type)
 87.2732 +      {
 87.2733 +         case PNG_COLOR_TYPE_GRAY:
 87.2734 +         {
 87.2735 +            switch (row_info->bit_depth)
 87.2736 +            {
 87.2737 +               case 1:
 87.2738 +               {
 87.2739 +                  sp = row;
 87.2740 +                  shift = 7;
 87.2741 +                  for (i = 0; i < row_width; i++)
 87.2742 +                  {
 87.2743 +                     if ((png_uint_16)((*sp >> shift) & 0x01)
 87.2744 +                        == trans_values->gray)
 87.2745 +                     {
 87.2746 +                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
 87.2747 +                        *sp |= (png_byte)(background->gray << shift);
 87.2748 +                     }
 87.2749 +                     if (!shift)
 87.2750 +                     {
 87.2751 +                        shift = 7;
 87.2752 +                        sp++;
 87.2753 +                     }
 87.2754 +                     else
 87.2755 +                        shift--;
 87.2756 +                  }
 87.2757 +                  break;
 87.2758 +               }
 87.2759 +               case 2:
 87.2760 +               {
 87.2761 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2762 +                  if (gamma_table != NULL)
 87.2763 +                  {
 87.2764 +                     sp = row;
 87.2765 +                     shift = 6;
 87.2766 +                     for (i = 0; i < row_width; i++)
 87.2767 +                     {
 87.2768 +                        if ((png_uint_16)((*sp >> shift) & 0x03)
 87.2769 +                            == trans_values->gray)
 87.2770 +                        {
 87.2771 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 87.2772 +                           *sp |= (png_byte)(background->gray << shift);
 87.2773 +                        }
 87.2774 +                        else
 87.2775 +                        {
 87.2776 +                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
 87.2777 +                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
 87.2778 +                               (p << 4) | (p << 6)] >> 6) & 0x03);
 87.2779 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 87.2780 +                           *sp |= (png_byte)(g << shift);
 87.2781 +                        }
 87.2782 +                        if (!shift)
 87.2783 +                        {
 87.2784 +                           shift = 6;
 87.2785 +                           sp++;
 87.2786 +                        }
 87.2787 +                        else
 87.2788 +                           shift -= 2;
 87.2789 +                     }
 87.2790 +                  }
 87.2791 +                  else
 87.2792 +#endif
 87.2793 +                  {
 87.2794 +                     sp = row;
 87.2795 +                     shift = 6;
 87.2796 +                     for (i = 0; i < row_width; i++)
 87.2797 +                     {
 87.2798 +                        if ((png_uint_16)((*sp >> shift) & 0x03)
 87.2799 +                            == trans_values->gray)
 87.2800 +                        {
 87.2801 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 87.2802 +                           *sp |= (png_byte)(background->gray << shift);
 87.2803 +                        }
 87.2804 +                        if (!shift)
 87.2805 +                        {
 87.2806 +                           shift = 6;
 87.2807 +                           sp++;
 87.2808 +                        }
 87.2809 +                        else
 87.2810 +                           shift -= 2;
 87.2811 +                     }
 87.2812 +                  }
 87.2813 +                  break;
 87.2814 +               }
 87.2815 +               case 4:
 87.2816 +               {
 87.2817 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2818 +                  if (gamma_table != NULL)
 87.2819 +                  {
 87.2820 +                     sp = row;
 87.2821 +                     shift = 4;
 87.2822 +                     for (i = 0; i < row_width; i++)
 87.2823 +                     {
 87.2824 +                        if ((png_uint_16)((*sp >> shift) & 0x0f)
 87.2825 +                            == trans_values->gray)
 87.2826 +                        {
 87.2827 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 87.2828 +                           *sp |= (png_byte)(background->gray << shift);
 87.2829 +                        }
 87.2830 +                        else
 87.2831 +                        {
 87.2832 +                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
 87.2833 +                           png_byte g = (png_byte)((gamma_table[p |
 87.2834 +                             (p << 4)] >> 4) & 0x0f);
 87.2835 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 87.2836 +                           *sp |= (png_byte)(g << shift);
 87.2837 +                        }
 87.2838 +                        if (!shift)
 87.2839 +                        {
 87.2840 +                           shift = 4;
 87.2841 +                           sp++;
 87.2842 +                        }
 87.2843 +                        else
 87.2844 +                           shift -= 4;
 87.2845 +                     }
 87.2846 +                  }
 87.2847 +                  else
 87.2848 +#endif
 87.2849 +                  {
 87.2850 +                     sp = row;
 87.2851 +                     shift = 4;
 87.2852 +                     for (i = 0; i < row_width; i++)
 87.2853 +                     {
 87.2854 +                        if ((png_uint_16)((*sp >> shift) & 0x0f)
 87.2855 +                            == trans_values->gray)
 87.2856 +                        {
 87.2857 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 87.2858 +                           *sp |= (png_byte)(background->gray << shift);
 87.2859 +                        }
 87.2860 +                        if (!shift)
 87.2861 +                        {
 87.2862 +                           shift = 4;
 87.2863 +                           sp++;
 87.2864 +                        }
 87.2865 +                        else
 87.2866 +                           shift -= 4;
 87.2867 +                     }
 87.2868 +                  }
 87.2869 +                  break;
 87.2870 +               }
 87.2871 +               case 8:
 87.2872 +               {
 87.2873 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2874 +                  if (gamma_table != NULL)
 87.2875 +                  {
 87.2876 +                     sp = row;
 87.2877 +                     for (i = 0; i < row_width; i++, sp++)
 87.2878 +                     {
 87.2879 +                        if (*sp == trans_values->gray)
 87.2880 +                        {
 87.2881 +                           *sp = (png_byte)background->gray;
 87.2882 +                        }
 87.2883 +                        else
 87.2884 +                        {
 87.2885 +                           *sp = gamma_table[*sp];
 87.2886 +                        }
 87.2887 +                     }
 87.2888 +                  }
 87.2889 +                  else
 87.2890 +#endif
 87.2891 +                  {
 87.2892 +                     sp = row;
 87.2893 +                     for (i = 0; i < row_width; i++, sp++)
 87.2894 +                     {
 87.2895 +                        if (*sp == trans_values->gray)
 87.2896 +                        {
 87.2897 +                           *sp = (png_byte)background->gray;
 87.2898 +                        }
 87.2899 +                     }
 87.2900 +                  }
 87.2901 +                  break;
 87.2902 +               }
 87.2903 +               case 16:
 87.2904 +               {
 87.2905 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2906 +                  if (gamma_16 != NULL)
 87.2907 +                  {
 87.2908 +                     sp = row;
 87.2909 +                     for (i = 0; i < row_width; i++, sp += 2)
 87.2910 +                     {
 87.2911 +                        png_uint_16 v;
 87.2912 +
 87.2913 +                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 87.2914 +                        if (v == trans_values->gray)
 87.2915 +                        {
 87.2916 +                           /* background is already in screen gamma */
 87.2917 +                           *sp = (png_byte)((background->gray >> 8) & 0xff);
 87.2918 +                           *(sp + 1) = (png_byte)(background->gray & 0xff);
 87.2919 +                        }
 87.2920 +                        else
 87.2921 +                        {
 87.2922 +                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 87.2923 +                           *sp = (png_byte)((v >> 8) & 0xff);
 87.2924 +                           *(sp + 1) = (png_byte)(v & 0xff);
 87.2925 +                        }
 87.2926 +                     }
 87.2927 +                  }
 87.2928 +                  else
 87.2929 +#endif
 87.2930 +                  {
 87.2931 +                     sp = row;
 87.2932 +                     for (i = 0; i < row_width; i++, sp += 2)
 87.2933 +                     {
 87.2934 +                        png_uint_16 v;
 87.2935 +
 87.2936 +                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 87.2937 +                        if (v == trans_values->gray)
 87.2938 +                        {
 87.2939 +                           *sp = (png_byte)((background->gray >> 8) & 0xff);
 87.2940 +                           *(sp + 1) = (png_byte)(background->gray & 0xff);
 87.2941 +                        }
 87.2942 +                     }
 87.2943 +                  }
 87.2944 +                  break;
 87.2945 +               }
 87.2946 +            }
 87.2947 +            break;
 87.2948 +         }
 87.2949 +         case PNG_COLOR_TYPE_RGB:
 87.2950 +         {
 87.2951 +            if (row_info->bit_depth == 8)
 87.2952 +            {
 87.2953 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2954 +               if (gamma_table != NULL)
 87.2955 +               {
 87.2956 +                  sp = row;
 87.2957 +                  for (i = 0; i < row_width; i++, sp += 3)
 87.2958 +                  {
 87.2959 +                     if (*sp == trans_values->red &&
 87.2960 +                        *(sp + 1) == trans_values->green &&
 87.2961 +                        *(sp + 2) == trans_values->blue)
 87.2962 +                     {
 87.2963 +                        *sp = (png_byte)background->red;
 87.2964 +                        *(sp + 1) = (png_byte)background->green;
 87.2965 +                        *(sp + 2) = (png_byte)background->blue;
 87.2966 +                     }
 87.2967 +                     else
 87.2968 +                     {
 87.2969 +                        *sp = gamma_table[*sp];
 87.2970 +                        *(sp + 1) = gamma_table[*(sp + 1)];
 87.2971 +                        *(sp + 2) = gamma_table[*(sp + 2)];
 87.2972 +                     }
 87.2973 +                  }
 87.2974 +               }
 87.2975 +               else
 87.2976 +#endif
 87.2977 +               {
 87.2978 +                  sp = row;
 87.2979 +                  for (i = 0; i < row_width; i++, sp += 3)
 87.2980 +                  {
 87.2981 +                     if (*sp == trans_values->red &&
 87.2982 +                        *(sp + 1) == trans_values->green &&
 87.2983 +                        *(sp + 2) == trans_values->blue)
 87.2984 +                     {
 87.2985 +                        *sp = (png_byte)background->red;
 87.2986 +                        *(sp + 1) = (png_byte)background->green;
 87.2987 +                        *(sp + 2) = (png_byte)background->blue;
 87.2988 +                     }
 87.2989 +                  }
 87.2990 +               }
 87.2991 +            }
 87.2992 +            else /* if (row_info->bit_depth == 16) */
 87.2993 +            {
 87.2994 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.2995 +               if (gamma_16 != NULL)
 87.2996 +               {
 87.2997 +                  sp = row;
 87.2998 +                  for (i = 0; i < row_width; i++, sp += 6)
 87.2999 +                  {
 87.3000 +                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 87.3001 +                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 87.3002 +                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
 87.3003 +                     if (r == trans_values->red && g == trans_values->green &&
 87.3004 +                        b == trans_values->blue)
 87.3005 +                     {
 87.3006 +                        /* background is already in screen gamma */
 87.3007 +                        *sp = (png_byte)((background->red >> 8) & 0xff);
 87.3008 +                        *(sp + 1) = (png_byte)(background->red & 0xff);
 87.3009 +                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
 87.3010 +                        *(sp + 3) = (png_byte)(background->green & 0xff);
 87.3011 +                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 87.3012 +                        *(sp + 5) = (png_byte)(background->blue & 0xff);
 87.3013 +                     }
 87.3014 +                     else
 87.3015 +                     {
 87.3016 +                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 87.3017 +                        *sp = (png_byte)((v >> 8) & 0xff);
 87.3018 +                        *(sp + 1) = (png_byte)(v & 0xff);
 87.3019 +                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
 87.3020 +                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
 87.3021 +                        *(sp + 3) = (png_byte)(v & 0xff);
 87.3022 +                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
 87.3023 +                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
 87.3024 +                        *(sp + 5) = (png_byte)(v & 0xff);
 87.3025 +                     }
 87.3026 +                  }
 87.3027 +               }
 87.3028 +               else
 87.3029 +#endif
 87.3030 +               {
 87.3031 +                  sp = row;
 87.3032 +                  for (i = 0; i < row_width; i++, sp += 6)
 87.3033 +                  {
 87.3034 +                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
 87.3035 +                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 87.3036 +                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
 87.3037 +
 87.3038 +                     if (r == trans_values->red && g == trans_values->green &&
 87.3039 +                        b == trans_values->blue)
 87.3040 +                     {
 87.3041 +                        *sp = (png_byte)((background->red >> 8) & 0xff);
 87.3042 +                        *(sp + 1) = (png_byte)(background->red & 0xff);
 87.3043 +                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
 87.3044 +                        *(sp + 3) = (png_byte)(background->green & 0xff);
 87.3045 +                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 87.3046 +                        *(sp + 5) = (png_byte)(background->blue & 0xff);
 87.3047 +                     }
 87.3048 +                  }
 87.3049 +               }
 87.3050 +            }
 87.3051 +            break;
 87.3052 +         }
 87.3053 +         case PNG_COLOR_TYPE_GRAY_ALPHA:
 87.3054 +         {
 87.3055 +            if (row_info->bit_depth == 8)
 87.3056 +            {
 87.3057 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3058 +               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
 87.3059 +                   gamma_table != NULL)
 87.3060 +               {
 87.3061 +                  sp = row;
 87.3062 +                  dp = row;
 87.3063 +                  for (i = 0; i < row_width; i++, sp += 2, dp++)
 87.3064 +                  {
 87.3065 +                     png_uint_16 a = *(sp + 1);
 87.3066 +
 87.3067 +                     if (a == 0xff)
 87.3068 +                     {
 87.3069 +                        *dp = gamma_table[*sp];
 87.3070 +                     }
 87.3071 +                     else if (a == 0)
 87.3072 +                     {
 87.3073 +                        /* background is already in screen gamma */
 87.3074 +                        *dp = (png_byte)background->gray;
 87.3075 +                     }
 87.3076 +                     else
 87.3077 +                     {
 87.3078 +                        png_byte v, w;
 87.3079 +
 87.3080 +                        v = gamma_to_1[*sp];
 87.3081 +                        png_composite(w, v, a, background_1->gray);
 87.3082 +                        *dp = gamma_from_1[w];
 87.3083 +                     }
 87.3084 +                  }
 87.3085 +               }
 87.3086 +               else
 87.3087 +#endif
 87.3088 +               {
 87.3089 +                  sp = row;
 87.3090 +                  dp = row;
 87.3091 +                  for (i = 0; i < row_width; i++, sp += 2, dp++)
 87.3092 +                  {
 87.3093 +                     png_byte a = *(sp + 1);
 87.3094 +
 87.3095 +                     if (a == 0xff)
 87.3096 +                     {
 87.3097 +                        *dp = *sp;
 87.3098 +                     }
 87.3099 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3100 +                     else if (a == 0)
 87.3101 +                     {
 87.3102 +                        *dp = (png_byte)background->gray;
 87.3103 +                     }
 87.3104 +                     else
 87.3105 +                     {
 87.3106 +                        png_composite(*dp, *sp, a, background_1->gray);
 87.3107 +                     }
 87.3108 +#else
 87.3109 +                     *dp = (png_byte)background->gray;
 87.3110 +#endif
 87.3111 +                  }
 87.3112 +               }
 87.3113 +            }
 87.3114 +            else /* if (png_ptr->bit_depth == 16) */
 87.3115 +            {
 87.3116 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3117 +               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
 87.3118 +                   gamma_16_to_1 != NULL)
 87.3119 +               {
 87.3120 +                  sp = row;
 87.3121 +                  dp = row;
 87.3122 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
 87.3123 +                  {
 87.3124 +                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 87.3125 +
 87.3126 +                     if (a == (png_uint_16)0xffff)
 87.3127 +                     {
 87.3128 +                        png_uint_16 v;
 87.3129 +
 87.3130 +                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 87.3131 +                        *dp = (png_byte)((v >> 8) & 0xff);
 87.3132 +                        *(dp + 1) = (png_byte)(v & 0xff);
 87.3133 +                     }
 87.3134 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3135 +                     else if (a == 0)
 87.3136 +#else
 87.3137 +                     else
 87.3138 +#endif
 87.3139 +                     {
 87.3140 +                        /* background is already in screen gamma */
 87.3141 +                        *dp = (png_byte)((background->gray >> 8) & 0xff);
 87.3142 +                        *(dp + 1) = (png_byte)(background->gray & 0xff);
 87.3143 +                     }
 87.3144 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3145 +                     else
 87.3146 +                     {
 87.3147 +                        png_uint_16 g, v, w;
 87.3148 +
 87.3149 +                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
 87.3150 +                        png_composite_16(v, g, a, background_1->gray);
 87.3151 +                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
 87.3152 +                        *dp = (png_byte)((w >> 8) & 0xff);
 87.3153 +                        *(dp + 1) = (png_byte)(w & 0xff);
 87.3154 +                     }
 87.3155 +#endif
 87.3156 +                  }
 87.3157 +               }
 87.3158 +               else
 87.3159 +#endif
 87.3160 +               {
 87.3161 +                  sp = row;
 87.3162 +                  dp = row;
 87.3163 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
 87.3164 +                  {
 87.3165 +                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 87.3166 +                     if (a == (png_uint_16)0xffff)
 87.3167 +                     {
 87.3168 +                        png_memcpy(dp, sp, 2);
 87.3169 +                     }
 87.3170 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3171 +                     else if (a == 0)
 87.3172 +#else
 87.3173 +                     else
 87.3174 +#endif
 87.3175 +                     {
 87.3176 +                        *dp = (png_byte)((background->gray >> 8) & 0xff);
 87.3177 +                        *(dp + 1) = (png_byte)(background->gray & 0xff);
 87.3178 +                     }
 87.3179 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3180 +                     else
 87.3181 +                     {
 87.3182 +                        png_uint_16 g, v;
 87.3183 +
 87.3184 +                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 87.3185 +                        png_composite_16(v, g, a, background_1->gray);
 87.3186 +                        *dp = (png_byte)((v >> 8) & 0xff);
 87.3187 +                        *(dp + 1) = (png_byte)(v & 0xff);
 87.3188 +                     }
 87.3189 +#endif
 87.3190 +                  }
 87.3191 +               }
 87.3192 +            }
 87.3193 +            break;
 87.3194 +         }
 87.3195 +         case PNG_COLOR_TYPE_RGB_ALPHA:
 87.3196 +         {
 87.3197 +            if (row_info->bit_depth == 8)
 87.3198 +            {
 87.3199 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3200 +               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
 87.3201 +                   gamma_table != NULL)
 87.3202 +               {
 87.3203 +                  sp = row;
 87.3204 +                  dp = row;
 87.3205 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
 87.3206 +                  {
 87.3207 +                     png_byte a = *(sp + 3);
 87.3208 +
 87.3209 +                     if (a == 0xff)
 87.3210 +                     {
 87.3211 +                        *dp = gamma_table[*sp];
 87.3212 +                        *(dp + 1) = gamma_table[*(sp + 1)];
 87.3213 +                        *(dp + 2) = gamma_table[*(sp + 2)];
 87.3214 +                     }
 87.3215 +                     else if (a == 0)
 87.3216 +                     {
 87.3217 +                        /* background is already in screen gamma */
 87.3218 +                        *dp = (png_byte)background->red;
 87.3219 +                        *(dp + 1) = (png_byte)background->green;
 87.3220 +                        *(dp + 2) = (png_byte)background->blue;
 87.3221 +                     }
 87.3222 +                     else
 87.3223 +                     {
 87.3224 +                        png_byte v, w;
 87.3225 +
 87.3226 +                        v = gamma_to_1[*sp];
 87.3227 +                        png_composite(w, v, a, background_1->red);
 87.3228 +                        *dp = gamma_from_1[w];
 87.3229 +                        v = gamma_to_1[*(sp + 1)];
 87.3230 +                        png_composite(w, v, a, background_1->green);
 87.3231 +                        *(dp + 1) = gamma_from_1[w];
 87.3232 +                        v = gamma_to_1[*(sp + 2)];
 87.3233 +                        png_composite(w, v, a, background_1->blue);
 87.3234 +                        *(dp + 2) = gamma_from_1[w];
 87.3235 +                     }
 87.3236 +                  }
 87.3237 +               }
 87.3238 +               else
 87.3239 +#endif
 87.3240 +               {
 87.3241 +                  sp = row;
 87.3242 +                  dp = row;
 87.3243 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
 87.3244 +                  {
 87.3245 +                     png_byte a = *(sp + 3);
 87.3246 +
 87.3247 +                     if (a == 0xff)
 87.3248 +                     {
 87.3249 +                        *dp = *sp;
 87.3250 +                        *(dp + 1) = *(sp + 1);
 87.3251 +                        *(dp + 2) = *(sp + 2);
 87.3252 +                     }
 87.3253 +                     else if (a == 0)
 87.3254 +                     {
 87.3255 +                        *dp = (png_byte)background->red;
 87.3256 +                        *(dp + 1) = (png_byte)background->green;
 87.3257 +                        *(dp + 2) = (png_byte)background->blue;
 87.3258 +                     }
 87.3259 +                     else
 87.3260 +                     {
 87.3261 +                        png_composite(*dp, *sp, a, background->red);
 87.3262 +                        png_composite(*(dp + 1), *(sp + 1), a,
 87.3263 +                           background->green);
 87.3264 +                        png_composite(*(dp + 2), *(sp + 2), a,
 87.3265 +                           background->blue);
 87.3266 +                     }
 87.3267 +                  }
 87.3268 +               }
 87.3269 +            }
 87.3270 +            else /* if (row_info->bit_depth == 16) */
 87.3271 +            {
 87.3272 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3273 +               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
 87.3274 +                   gamma_16_to_1 != NULL)
 87.3275 +               {
 87.3276 +                  sp = row;
 87.3277 +                  dp = row;
 87.3278 +                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
 87.3279 +                  {
 87.3280 +                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
 87.3281 +                         << 8) + (png_uint_16)(*(sp + 7)));
 87.3282 +                     if (a == (png_uint_16)0xffff)
 87.3283 +                     {
 87.3284 +                        png_uint_16 v;
 87.3285 +
 87.3286 +                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 87.3287 +                        *dp = (png_byte)((v >> 8) & 0xff);
 87.3288 +                        *(dp + 1) = (png_byte)(v & 0xff);
 87.3289 +                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
 87.3290 +                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
 87.3291 +                        *(dp + 3) = (png_byte)(v & 0xff);
 87.3292 +                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
 87.3293 +                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
 87.3294 +                        *(dp + 5) = (png_byte)(v & 0xff);
 87.3295 +                     }
 87.3296 +                     else if (a == 0)
 87.3297 +                     {
 87.3298 +                        /* background is already in screen gamma */
 87.3299 +                        *dp = (png_byte)((background->red >> 8) & 0xff);
 87.3300 +                        *(dp + 1) = (png_byte)(background->red & 0xff);
 87.3301 +                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
 87.3302 +                        *(dp + 3) = (png_byte)(background->green & 0xff);
 87.3303 +                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 87.3304 +                        *(dp + 5) = (png_byte)(background->blue & 0xff);
 87.3305 +                     }
 87.3306 +                     else
 87.3307 +                     {
 87.3308 +                        png_uint_16 v, w, x;
 87.3309 +
 87.3310 +                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
 87.3311 +                        png_composite_16(w, v, a, background_1->red);
 87.3312 +                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
 87.3313 +                        *dp = (png_byte)((x >> 8) & 0xff);
 87.3314 +                        *(dp + 1) = (png_byte)(x & 0xff);
 87.3315 +                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
 87.3316 +                        png_composite_16(w, v, a, background_1->green);
 87.3317 +                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
 87.3318 +                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
 87.3319 +                        *(dp + 3) = (png_byte)(x & 0xff);
 87.3320 +                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
 87.3321 +                        png_composite_16(w, v, a, background_1->blue);
 87.3322 +                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
 87.3323 +                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
 87.3324 +                        *(dp + 5) = (png_byte)(x & 0xff);
 87.3325 +                     }
 87.3326 +                  }
 87.3327 +               }
 87.3328 +               else
 87.3329 +#endif
 87.3330 +               {
 87.3331 +                  sp = row;
 87.3332 +                  dp = row;
 87.3333 +                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
 87.3334 +                  {
 87.3335 +                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
 87.3336 +                        << 8) + (png_uint_16)(*(sp + 7)));
 87.3337 +                     if (a == (png_uint_16)0xffff)
 87.3338 +                     {
 87.3339 +                        png_memcpy(dp, sp, 6);
 87.3340 +                     }
 87.3341 +                     else if (a == 0)
 87.3342 +                     {
 87.3343 +                        *dp = (png_byte)((background->red >> 8) & 0xff);
 87.3344 +                        *(dp + 1) = (png_byte)(background->red & 0xff);
 87.3345 +                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
 87.3346 +                        *(dp + 3) = (png_byte)(background->green & 0xff);
 87.3347 +                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 87.3348 +                        *(dp + 5) = (png_byte)(background->blue & 0xff);
 87.3349 +                     }
 87.3350 +                     else
 87.3351 +                     {
 87.3352 +                        png_uint_16 v;
 87.3353 +
 87.3354 +                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 87.3355 +                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
 87.3356 +                            + *(sp + 3));
 87.3357 +                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
 87.3358 +                            + *(sp + 5));
 87.3359 +
 87.3360 +                        png_composite_16(v, r, a, background->red);
 87.3361 +                        *dp = (png_byte)((v >> 8) & 0xff);
 87.3362 +                        *(dp + 1) = (png_byte)(v & 0xff);
 87.3363 +                        png_composite_16(v, g, a, background->green);
 87.3364 +                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
 87.3365 +                        *(dp + 3) = (png_byte)(v & 0xff);
 87.3366 +                        png_composite_16(v, b, a, background->blue);
 87.3367 +                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
 87.3368 +                        *(dp + 5) = (png_byte)(v & 0xff);
 87.3369 +                     }
 87.3370 +                  }
 87.3371 +               }
 87.3372 +            }
 87.3373 +            break;
 87.3374 +         }
 87.3375 +      }
 87.3376 +
 87.3377 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
 87.3378 +      {
 87.3379 +         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
 87.3380 +         row_info->channels--;
 87.3381 +         row_info->pixel_depth = (png_byte)(row_info->channels *
 87.3382 +            row_info->bit_depth);
 87.3383 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.3384 +      }
 87.3385 +   }
 87.3386 +}
 87.3387 +#endif
 87.3388 +
 87.3389 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.3390 +/* Gamma correct the image, avoiding the alpha channel.  Make sure
 87.3391 + * you do this after you deal with the transparency issue on grayscale
 87.3392 + * or RGB images. If your bit depth is 8, use gamma_table, if it
 87.3393 + * is 16, use gamma_16_table and gamma_shift.  Build these with
 87.3394 + * build_gamma_table().
 87.3395 + */
 87.3396 +void /* PRIVATE */
 87.3397 +png_do_gamma(png_row_infop row_info, png_bytep row,
 87.3398 +   png_bytep gamma_table, png_uint_16pp gamma_16_table,
 87.3399 +   int gamma_shift)
 87.3400 +{
 87.3401 +   png_bytep sp;
 87.3402 +   png_uint_32 i;
 87.3403 +   png_uint_32 row_width=row_info->width;
 87.3404 +
 87.3405 +   png_debug(1, "in png_do_gamma\n");
 87.3406 +   if (
 87.3407 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.3408 +       row != NULL && row_info != NULL &&
 87.3409 +#endif
 87.3410 +       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
 87.3411 +        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
 87.3412 +   {
 87.3413 +      switch (row_info->color_type)
 87.3414 +      {
 87.3415 +         case PNG_COLOR_TYPE_RGB:
 87.3416 +         {
 87.3417 +            if (row_info->bit_depth == 8)
 87.3418 +            {
 87.3419 +               sp = row;
 87.3420 +               for (i = 0; i < row_width; i++)
 87.3421 +               {
 87.3422 +                  *sp = gamma_table[*sp];
 87.3423 +                  sp++;
 87.3424 +                  *sp = gamma_table[*sp];
 87.3425 +                  sp++;
 87.3426 +                  *sp = gamma_table[*sp];
 87.3427 +                  sp++;
 87.3428 +               }
 87.3429 +            }
 87.3430 +            else /* if (row_info->bit_depth == 16) */
 87.3431 +            {
 87.3432 +               sp = row;
 87.3433 +               for (i = 0; i < row_width; i++)
 87.3434 +               {
 87.3435 +                  png_uint_16 v;
 87.3436 +
 87.3437 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3438 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3439 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3440 +                  sp += 2;
 87.3441 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3442 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3443 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3444 +                  sp += 2;
 87.3445 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3446 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3447 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3448 +                  sp += 2;
 87.3449 +               }
 87.3450 +            }
 87.3451 +            break;
 87.3452 +         }
 87.3453 +         case PNG_COLOR_TYPE_RGB_ALPHA:
 87.3454 +         {
 87.3455 +            if (row_info->bit_depth == 8)
 87.3456 +            {
 87.3457 +               sp = row;
 87.3458 +               for (i = 0; i < row_width; i++)
 87.3459 +               {
 87.3460 +                  *sp = gamma_table[*sp];
 87.3461 +                  sp++;
 87.3462 +                  *sp = gamma_table[*sp];
 87.3463 +                  sp++;
 87.3464 +                  *sp = gamma_table[*sp];
 87.3465 +                  sp++;
 87.3466 +                  sp++;
 87.3467 +               }
 87.3468 +            }
 87.3469 +            else /* if (row_info->bit_depth == 16) */
 87.3470 +            {
 87.3471 +               sp = row;
 87.3472 +               for (i = 0; i < row_width; i++)
 87.3473 +               {
 87.3474 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3475 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3476 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3477 +                  sp += 2;
 87.3478 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3479 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3480 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3481 +                  sp += 2;
 87.3482 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3483 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3484 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3485 +                  sp += 4;
 87.3486 +               }
 87.3487 +            }
 87.3488 +            break;
 87.3489 +         }
 87.3490 +         case PNG_COLOR_TYPE_GRAY_ALPHA:
 87.3491 +         {
 87.3492 +            if (row_info->bit_depth == 8)
 87.3493 +            {
 87.3494 +               sp = row;
 87.3495 +               for (i = 0; i < row_width; i++)
 87.3496 +               {
 87.3497 +                  *sp = gamma_table[*sp];
 87.3498 +                  sp += 2;
 87.3499 +               }
 87.3500 +            }
 87.3501 +            else /* if (row_info->bit_depth == 16) */
 87.3502 +            {
 87.3503 +               sp = row;
 87.3504 +               for (i = 0; i < row_width; i++)
 87.3505 +               {
 87.3506 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3507 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3508 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3509 +                  sp += 4;
 87.3510 +               }
 87.3511 +            }
 87.3512 +            break;
 87.3513 +         }
 87.3514 +         case PNG_COLOR_TYPE_GRAY:
 87.3515 +         {
 87.3516 +            if (row_info->bit_depth == 2)
 87.3517 +            {
 87.3518 +               sp = row;
 87.3519 +               for (i = 0; i < row_width; i += 4)
 87.3520 +               {
 87.3521 +                  int a = *sp & 0xc0;
 87.3522 +                  int b = *sp & 0x30;
 87.3523 +                  int c = *sp & 0x0c;
 87.3524 +                  int d = *sp & 0x03;
 87.3525 +
 87.3526 +                  *sp = (png_byte)(
 87.3527 +                        ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
 87.3528 +                        ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
 87.3529 +                        ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
 87.3530 +                        ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
 87.3531 +                  sp++;
 87.3532 +               }
 87.3533 +            }
 87.3534 +            if (row_info->bit_depth == 4)
 87.3535 +            {
 87.3536 +               sp = row;
 87.3537 +               for (i = 0; i < row_width; i += 2)
 87.3538 +               {
 87.3539 +                  int msb = *sp & 0xf0;
 87.3540 +                  int lsb = *sp & 0x0f;
 87.3541 +
 87.3542 +                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
 87.3543 +                          | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
 87.3544 +                  sp++;
 87.3545 +               }
 87.3546 +            }
 87.3547 +            else if (row_info->bit_depth == 8)
 87.3548 +            {
 87.3549 +               sp = row;
 87.3550 +               for (i = 0; i < row_width; i++)
 87.3551 +               {
 87.3552 +                  *sp = gamma_table[*sp];
 87.3553 +                  sp++;
 87.3554 +               }
 87.3555 +            }
 87.3556 +            else if (row_info->bit_depth == 16)
 87.3557 +            {
 87.3558 +               sp = row;
 87.3559 +               for (i = 0; i < row_width; i++)
 87.3560 +               {
 87.3561 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 87.3562 +                  *sp = (png_byte)((v >> 8) & 0xff);
 87.3563 +                  *(sp + 1) = (png_byte)(v & 0xff);
 87.3564 +                  sp += 2;
 87.3565 +               }
 87.3566 +            }
 87.3567 +            break;
 87.3568 +         }
 87.3569 +      }
 87.3570 +   }
 87.3571 +}
 87.3572 +#endif
 87.3573 +
 87.3574 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 87.3575 +/* Expands a palette row to an RGB or RGBA row depending
 87.3576 + * upon whether you supply trans and num_trans.
 87.3577 + */
 87.3578 +void /* PRIVATE */
 87.3579 +png_do_expand_palette(png_row_infop row_info, png_bytep row,
 87.3580 +   png_colorp palette, png_bytep trans, int num_trans)
 87.3581 +{
 87.3582 +   int shift, value;
 87.3583 +   png_bytep sp, dp;
 87.3584 +   png_uint_32 i;
 87.3585 +   png_uint_32 row_width=row_info->width;
 87.3586 +
 87.3587 +   png_debug(1, "in png_do_expand_palette\n");
 87.3588 +   if (
 87.3589 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.3590 +       row != NULL && row_info != NULL &&
 87.3591 +#endif
 87.3592 +       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
 87.3593 +   {
 87.3594 +      if (row_info->bit_depth < 8)
 87.3595 +      {
 87.3596 +         switch (row_info->bit_depth)
 87.3597 +         {
 87.3598 +            case 1:
 87.3599 +            {
 87.3600 +               sp = row + (png_size_t)((row_width - 1) >> 3);
 87.3601 +               dp = row + (png_size_t)row_width - 1;
 87.3602 +               shift = 7 - (int)((row_width + 7) & 0x07);
 87.3603 +               for (i = 0; i < row_width; i++)
 87.3604 +               {
 87.3605 +                  if ((*sp >> shift) & 0x01)
 87.3606 +                     *dp = 1;
 87.3607 +                  else
 87.3608 +                     *dp = 0;
 87.3609 +                  if (shift == 7)
 87.3610 +                  {
 87.3611 +                     shift = 0;
 87.3612 +                     sp--;
 87.3613 +                  }
 87.3614 +                  else
 87.3615 +                     shift++;
 87.3616 +
 87.3617 +                  dp--;
 87.3618 +               }
 87.3619 +               break;
 87.3620 +            }
 87.3621 +            case 2:
 87.3622 +            {
 87.3623 +               sp = row + (png_size_t)((row_width - 1) >> 2);
 87.3624 +               dp = row + (png_size_t)row_width - 1;
 87.3625 +               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 87.3626 +               for (i = 0; i < row_width; i++)
 87.3627 +               {
 87.3628 +                  value = (*sp >> shift) & 0x03;
 87.3629 +                  *dp = (png_byte)value;
 87.3630 +                  if (shift == 6)
 87.3631 +                  {
 87.3632 +                     shift = 0;
 87.3633 +                     sp--;
 87.3634 +                  }
 87.3635 +                  else
 87.3636 +                     shift += 2;
 87.3637 +
 87.3638 +                  dp--;
 87.3639 +               }
 87.3640 +               break;
 87.3641 +            }
 87.3642 +            case 4:
 87.3643 +            {
 87.3644 +               sp = row + (png_size_t)((row_width - 1) >> 1);
 87.3645 +               dp = row + (png_size_t)row_width - 1;
 87.3646 +               shift = (int)((row_width & 0x01) << 2);
 87.3647 +               for (i = 0; i < row_width; i++)
 87.3648 +               {
 87.3649 +                  value = (*sp >> shift) & 0x0f;
 87.3650 +                  *dp = (png_byte)value;
 87.3651 +                  if (shift == 4)
 87.3652 +                  {
 87.3653 +                     shift = 0;
 87.3654 +                     sp--;
 87.3655 +                  }
 87.3656 +                  else
 87.3657 +                     shift += 4;
 87.3658 +
 87.3659 +                  dp--;
 87.3660 +               }
 87.3661 +               break;
 87.3662 +            }
 87.3663 +         }
 87.3664 +         row_info->bit_depth = 8;
 87.3665 +         row_info->pixel_depth = 8;
 87.3666 +         row_info->rowbytes = row_width;
 87.3667 +      }
 87.3668 +      switch (row_info->bit_depth)
 87.3669 +      {
 87.3670 +         case 8:
 87.3671 +         {
 87.3672 +            if (trans != NULL)
 87.3673 +            {
 87.3674 +               sp = row + (png_size_t)row_width - 1;
 87.3675 +               dp = row + (png_size_t)(row_width << 2) - 1;
 87.3676 +
 87.3677 +               for (i = 0; i < row_width; i++)
 87.3678 +               {
 87.3679 +                  if ((int)(*sp) >= num_trans)
 87.3680 +                     *dp-- = 0xff;
 87.3681 +                  else
 87.3682 +                     *dp-- = trans[*sp];
 87.3683 +                  *dp-- = palette[*sp].blue;
 87.3684 +                  *dp-- = palette[*sp].green;
 87.3685 +                  *dp-- = palette[*sp].red;
 87.3686 +                  sp--;
 87.3687 +               }
 87.3688 +               row_info->bit_depth = 8;
 87.3689 +               row_info->pixel_depth = 32;
 87.3690 +               row_info->rowbytes = row_width * 4;
 87.3691 +               row_info->color_type = 6;
 87.3692 +               row_info->channels = 4;
 87.3693 +            }
 87.3694 +            else
 87.3695 +            {
 87.3696 +               sp = row + (png_size_t)row_width - 1;
 87.3697 +               dp = row + (png_size_t)(row_width * 3) - 1;
 87.3698 +
 87.3699 +               for (i = 0; i < row_width; i++)
 87.3700 +               {
 87.3701 +                  *dp-- = palette[*sp].blue;
 87.3702 +                  *dp-- = palette[*sp].green;
 87.3703 +                  *dp-- = palette[*sp].red;
 87.3704 +                  sp--;
 87.3705 +               }
 87.3706 +               row_info->bit_depth = 8;
 87.3707 +               row_info->pixel_depth = 24;
 87.3708 +               row_info->rowbytes = row_width * 3;
 87.3709 +               row_info->color_type = 2;
 87.3710 +               row_info->channels = 3;
 87.3711 +            }
 87.3712 +            break;
 87.3713 +         }
 87.3714 +      }
 87.3715 +   }
 87.3716 +}
 87.3717 +
 87.3718 +/* If the bit depth < 8, it is expanded to 8.  Also, if the already
 87.3719 + * expanded transparency value is supplied, an alpha channel is built.
 87.3720 + */
 87.3721 +void /* PRIVATE */
 87.3722 +png_do_expand(png_row_infop row_info, png_bytep row,
 87.3723 +   png_color_16p trans_value)
 87.3724 +{
 87.3725 +   int shift, value;
 87.3726 +   png_bytep sp, dp;
 87.3727 +   png_uint_32 i;
 87.3728 +   png_uint_32 row_width=row_info->width;
 87.3729 +
 87.3730 +   png_debug(1, "in png_do_expand\n");
 87.3731 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.3732 +   if (row != NULL && row_info != NULL)
 87.3733 +#endif
 87.3734 +   {
 87.3735 +      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
 87.3736 +      {
 87.3737 +         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
 87.3738 +
 87.3739 +         if (row_info->bit_depth < 8)
 87.3740 +         {
 87.3741 +            switch (row_info->bit_depth)
 87.3742 +            {
 87.3743 +               case 1:
 87.3744 +               {
 87.3745 +                  gray = (png_uint_16)((gray&0x01)*0xff);
 87.3746 +                  sp = row + (png_size_t)((row_width - 1) >> 3);
 87.3747 +                  dp = row + (png_size_t)row_width - 1;
 87.3748 +                  shift = 7 - (int)((row_width + 7) & 0x07);
 87.3749 +                  for (i = 0; i < row_width; i++)
 87.3750 +                  {
 87.3751 +                     if ((*sp >> shift) & 0x01)
 87.3752 +                        *dp = 0xff;
 87.3753 +                     else
 87.3754 +                        *dp = 0;
 87.3755 +                     if (shift == 7)
 87.3756 +                     {
 87.3757 +                        shift = 0;
 87.3758 +                        sp--;
 87.3759 +                     }
 87.3760 +                     else
 87.3761 +                        shift++;
 87.3762 +
 87.3763 +                     dp--;
 87.3764 +                  }
 87.3765 +                  break;
 87.3766 +               }
 87.3767 +               case 2:
 87.3768 +               {
 87.3769 +                  gray = (png_uint_16)((gray&0x03)*0x55);
 87.3770 +                  sp = row + (png_size_t)((row_width - 1) >> 2);
 87.3771 +                  dp = row + (png_size_t)row_width - 1;
 87.3772 +                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 87.3773 +                  for (i = 0; i < row_width; i++)
 87.3774 +                  {
 87.3775 +                     value = (*sp >> shift) & 0x03;
 87.3776 +                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
 87.3777 +                        (value << 6));
 87.3778 +                     if (shift == 6)
 87.3779 +                     {
 87.3780 +                        shift = 0;
 87.3781 +                        sp--;
 87.3782 +                     }
 87.3783 +                     else
 87.3784 +                        shift += 2;
 87.3785 +
 87.3786 +                     dp--;
 87.3787 +                  }
 87.3788 +                  break;
 87.3789 +               }
 87.3790 +               case 4:
 87.3791 +               {
 87.3792 +                  gray = (png_uint_16)((gray&0x0f)*0x11);
 87.3793 +                  sp = row + (png_size_t)((row_width - 1) >> 1);
 87.3794 +                  dp = row + (png_size_t)row_width - 1;
 87.3795 +                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
 87.3796 +                  for (i = 0; i < row_width; i++)
 87.3797 +                  {
 87.3798 +                     value = (*sp >> shift) & 0x0f;
 87.3799 +                     *dp = (png_byte)(value | (value << 4));
 87.3800 +                     if (shift == 4)
 87.3801 +                     {
 87.3802 +                        shift = 0;
 87.3803 +                        sp--;
 87.3804 +                     }
 87.3805 +                     else
 87.3806 +                        shift = 4;
 87.3807 +
 87.3808 +                     dp--;
 87.3809 +                  }
 87.3810 +                  break;
 87.3811 +               }
 87.3812 +            }
 87.3813 +            row_info->bit_depth = 8;
 87.3814 +            row_info->pixel_depth = 8;
 87.3815 +            row_info->rowbytes = row_width;
 87.3816 +         }
 87.3817 +
 87.3818 +         if (trans_value != NULL)
 87.3819 +         {
 87.3820 +            if (row_info->bit_depth == 8)
 87.3821 +            {
 87.3822 +               gray = gray & 0xff;
 87.3823 +               sp = row + (png_size_t)row_width - 1;
 87.3824 +               dp = row + (png_size_t)(row_width << 1) - 1;
 87.3825 +               for (i = 0; i < row_width; i++)
 87.3826 +               {
 87.3827 +                  if (*sp == gray)
 87.3828 +                     *dp-- = 0;
 87.3829 +                  else
 87.3830 +                     *dp-- = 0xff;
 87.3831 +                  *dp-- = *sp--;
 87.3832 +               }
 87.3833 +            }
 87.3834 +            else if (row_info->bit_depth == 16)
 87.3835 +            {
 87.3836 +               png_byte gray_high = (gray >> 8) & 0xff;
 87.3837 +               png_byte gray_low = gray & 0xff;
 87.3838 +               sp = row + row_info->rowbytes - 1;
 87.3839 +               dp = row + (row_info->rowbytes << 1) - 1;
 87.3840 +               for (i = 0; i < row_width; i++)
 87.3841 +               {
 87.3842 +                  if (*(sp - 1) == gray_high && *(sp) == gray_low) 
 87.3843 +                  {
 87.3844 +                     *dp-- = 0;
 87.3845 +                     *dp-- = 0;
 87.3846 +                  }
 87.3847 +                  else
 87.3848 +                  {
 87.3849 +                     *dp-- = 0xff;
 87.3850 +                     *dp-- = 0xff;
 87.3851 +                  }
 87.3852 +                  *dp-- = *sp--;
 87.3853 +                  *dp-- = *sp--;
 87.3854 +               }
 87.3855 +            }
 87.3856 +            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
 87.3857 +            row_info->channels = 2;
 87.3858 +            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
 87.3859 +            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
 87.3860 +               row_width);
 87.3861 +         }
 87.3862 +      }
 87.3863 +      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
 87.3864 +      {
 87.3865 +         if (row_info->bit_depth == 8)
 87.3866 +         {
 87.3867 +            png_byte red = trans_value->red & 0xff;
 87.3868 +            png_byte green = trans_value->green & 0xff;
 87.3869 +            png_byte blue = trans_value->blue & 0xff;
 87.3870 +            sp = row + (png_size_t)row_info->rowbytes - 1;
 87.3871 +            dp = row + (png_size_t)(row_width << 2) - 1;
 87.3872 +            for (i = 0; i < row_width; i++)
 87.3873 +            {
 87.3874 +               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
 87.3875 +                  *dp-- = 0;
 87.3876 +               else
 87.3877 +                  *dp-- = 0xff;
 87.3878 +               *dp-- = *sp--;
 87.3879 +               *dp-- = *sp--;
 87.3880 +               *dp-- = *sp--;
 87.3881 +            }
 87.3882 +         }
 87.3883 +         else if (row_info->bit_depth == 16)
 87.3884 +         {
 87.3885 +            png_byte red_high = (trans_value->red >> 8) & 0xff;
 87.3886 +            png_byte green_high = (trans_value->green >> 8) & 0xff;
 87.3887 +            png_byte blue_high = (trans_value->blue >> 8) & 0xff;
 87.3888 +            png_byte red_low = trans_value->red & 0xff;
 87.3889 +            png_byte green_low = trans_value->green & 0xff;
 87.3890 +            png_byte blue_low = trans_value->blue & 0xff;
 87.3891 +            sp = row + row_info->rowbytes - 1;
 87.3892 +            dp = row + (png_size_t)(row_width << 3) - 1;
 87.3893 +            for (i = 0; i < row_width; i++)
 87.3894 +            {
 87.3895 +               if (*(sp - 5) == red_high &&
 87.3896 +                  *(sp - 4) == red_low &&
 87.3897 +                  *(sp - 3) == green_high &&
 87.3898 +                  *(sp - 2) == green_low &&
 87.3899 +                  *(sp - 1) == blue_high &&
 87.3900 +                  *(sp    ) == blue_low)
 87.3901 +               {
 87.3902 +                  *dp-- = 0;
 87.3903 +                  *dp-- = 0;
 87.3904 +               }
 87.3905 +               else
 87.3906 +               {
 87.3907 +                  *dp-- = 0xff;
 87.3908 +                  *dp-- = 0xff;
 87.3909 +               }
 87.3910 +               *dp-- = *sp--;
 87.3911 +               *dp-- = *sp--;
 87.3912 +               *dp-- = *sp--;
 87.3913 +               *dp-- = *sp--;
 87.3914 +               *dp-- = *sp--;
 87.3915 +               *dp-- = *sp--;
 87.3916 +            }
 87.3917 +         }
 87.3918 +         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 87.3919 +         row_info->channels = 4;
 87.3920 +         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
 87.3921 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.3922 +      }
 87.3923 +   }
 87.3924 +}
 87.3925 +#endif
 87.3926 +
 87.3927 +#if defined(PNG_READ_DITHER_SUPPORTED)
 87.3928 +void /* PRIVATE */
 87.3929 +png_do_dither(png_row_infop row_info, png_bytep row,
 87.3930 +    png_bytep palette_lookup, png_bytep dither_lookup)
 87.3931 +{
 87.3932 +   png_bytep sp, dp;
 87.3933 +   png_uint_32 i;
 87.3934 +   png_uint_32 row_width=row_info->width;
 87.3935 +
 87.3936 +   png_debug(1, "in png_do_dither\n");
 87.3937 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.3938 +   if (row != NULL && row_info != NULL)
 87.3939 +#endif
 87.3940 +   {
 87.3941 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
 87.3942 +         palette_lookup && row_info->bit_depth == 8)
 87.3943 +      {
 87.3944 +         int r, g, b, p;
 87.3945 +         sp = row;
 87.3946 +         dp = row;
 87.3947 +         for (i = 0; i < row_width; i++)
 87.3948 +         {
 87.3949 +            r = *sp++;
 87.3950 +            g = *sp++;
 87.3951 +            b = *sp++;
 87.3952 +
 87.3953 +            /* this looks real messy, but the compiler will reduce
 87.3954 +               it down to a reasonable formula.  For example, with
 87.3955 +               5 bits per color, we get:
 87.3956 +               p = (((r >> 3) & 0x1f) << 10) |
 87.3957 +                  (((g >> 3) & 0x1f) << 5) |
 87.3958 +                  ((b >> 3) & 0x1f);
 87.3959 +               */
 87.3960 +            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
 87.3961 +               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
 87.3962 +               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
 87.3963 +               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
 87.3964 +               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
 87.3965 +               (PNG_DITHER_BLUE_BITS)) |
 87.3966 +               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
 87.3967 +               ((1 << PNG_DITHER_BLUE_BITS) - 1));
 87.3968 +
 87.3969 +            *dp++ = palette_lookup[p];
 87.3970 +         }
 87.3971 +         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
 87.3972 +         row_info->channels = 1;
 87.3973 +         row_info->pixel_depth = row_info->bit_depth;
 87.3974 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.3975 +      }
 87.3976 +      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
 87.3977 +         palette_lookup != NULL && row_info->bit_depth == 8)
 87.3978 +      {
 87.3979 +         int r, g, b, p;
 87.3980 +         sp = row;
 87.3981 +         dp = row;
 87.3982 +         for (i = 0; i < row_width; i++)
 87.3983 +         {
 87.3984 +            r = *sp++;
 87.3985 +            g = *sp++;
 87.3986 +            b = *sp++;
 87.3987 +            sp++;
 87.3988 +
 87.3989 +            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
 87.3990 +               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
 87.3991 +               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
 87.3992 +               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
 87.3993 +               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
 87.3994 +               (PNG_DITHER_BLUE_BITS)) |
 87.3995 +               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
 87.3996 +               ((1 << PNG_DITHER_BLUE_BITS) - 1));
 87.3997 +
 87.3998 +            *dp++ = palette_lookup[p];
 87.3999 +         }
 87.4000 +         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
 87.4001 +         row_info->channels = 1;
 87.4002 +         row_info->pixel_depth = row_info->bit_depth;
 87.4003 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 87.4004 +      }
 87.4005 +      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
 87.4006 +         dither_lookup && row_info->bit_depth == 8)
 87.4007 +      {
 87.4008 +         sp = row;
 87.4009 +         for (i = 0; i < row_width; i++, sp++)
 87.4010 +         {
 87.4011 +            *sp = dither_lookup[*sp];
 87.4012 +         }
 87.4013 +      }
 87.4014 +   }
 87.4015 +}
 87.4016 +#endif
 87.4017 +
 87.4018 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 87.4019 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 87.4020 +static PNG_CONST int png_gamma_shift[] =
 87.4021 +   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
 87.4022 +
 87.4023 +/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
 87.4024 + * tables, we don't make a full table if we are reducing to 8-bit in
 87.4025 + * the future.  Note also how the gamma_16 tables are segmented so that
 87.4026 + * we don't need to allocate > 64K chunks for a full 16-bit table.
 87.4027 + */
 87.4028 +void /* PRIVATE */
 87.4029 +png_build_gamma_table(png_structp png_ptr)
 87.4030 +{
 87.4031 +  png_debug(1, "in png_build_gamma_table\n");
 87.4032 +
 87.4033 +  if (png_ptr->bit_depth <= 8)
 87.4034 +  {
 87.4035 +     int i;
 87.4036 +     double g;
 87.4037 +
 87.4038 +     if (png_ptr->screen_gamma > .000001)
 87.4039 +        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
 87.4040 +     else
 87.4041 +        g = 1.0;
 87.4042 +
 87.4043 +     png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
 87.4044 +        (png_uint_32)256);
 87.4045 +
 87.4046 +     for (i = 0; i < 256; i++)
 87.4047 +     {
 87.4048 +        png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
 87.4049 +           g) * 255.0 + .5);
 87.4050 +     }
 87.4051 +
 87.4052 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
 87.4053 +   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 87.4054 +     if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
 87.4055 +     {
 87.4056 +
 87.4057 +        g = 1.0 / (png_ptr->gamma);
 87.4058 +
 87.4059 +        png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
 87.4060 +           (png_uint_32)256);
 87.4061 +
 87.4062 +        for (i = 0; i < 256; i++)
 87.4063 +        {
 87.4064 +           png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
 87.4065 +              g) * 255.0 + .5);
 87.4066 +        }
 87.4067 +
 87.4068 +
 87.4069 +        png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
 87.4070 +           (png_uint_32)256);
 87.4071 +
 87.4072 +        if (png_ptr->screen_gamma > 0.000001)
 87.4073 +           g = 1.0 / png_ptr->screen_gamma;
 87.4074 +        else
 87.4075 +           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
 87.4076 +
 87.4077 +        for (i = 0; i < 256; i++)
 87.4078 +        {
 87.4079 +           png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
 87.4080 +              g) * 255.0 + .5);
 87.4081 +
 87.4082 +        }
 87.4083 +     }
 87.4084 +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
 87.4085 +  }
 87.4086 +  else
 87.4087 +  {
 87.4088 +     double g;
 87.4089 +     int i, j, shift, num;
 87.4090 +     int sig_bit;
 87.4091 +     png_uint_32 ig;
 87.4092 +
 87.4093 +     if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
 87.4094 +     {
 87.4095 +        sig_bit = (int)png_ptr->sig_bit.red;
 87.4096 +        if ((int)png_ptr->sig_bit.green > sig_bit)
 87.4097 +           sig_bit = png_ptr->sig_bit.green;
 87.4098 +        if ((int)png_ptr->sig_bit.blue > sig_bit)
 87.4099 +           sig_bit = png_ptr->sig_bit.blue;
 87.4100 +     }
 87.4101 +     else
 87.4102 +     {
 87.4103 +        sig_bit = (int)png_ptr->sig_bit.gray;
 87.4104 +     }
 87.4105 +
 87.4106 +     if (sig_bit > 0)
 87.4107 +        shift = 16 - sig_bit;
 87.4108 +     else
 87.4109 +        shift = 0;
 87.4110 +
 87.4111 +     if (png_ptr->transformations & PNG_16_TO_8)
 87.4112 +     {
 87.4113 +        if (shift < (16 - PNG_MAX_GAMMA_8))
 87.4114 +           shift = (16 - PNG_MAX_GAMMA_8);
 87.4115 +     }
 87.4116 +
 87.4117 +     if (shift > 8)
 87.4118 +        shift = 8;
 87.4119 +     if (shift < 0)
 87.4120 +        shift = 0;
 87.4121 +
 87.4122 +     png_ptr->gamma_shift = (png_byte)shift;
 87.4123 +
 87.4124 +     num = (1 << (8 - shift));
 87.4125 +
 87.4126 +     if (png_ptr->screen_gamma > .000001)
 87.4127 +        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
 87.4128 +     else
 87.4129 +        g = 1.0;
 87.4130 +
 87.4131 +     png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
 87.4132 +        (png_uint_32)(num * png_sizeof(png_uint_16p)));
 87.4133 +
 87.4134 +     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
 87.4135 +     {
 87.4136 +        double fin, fout;
 87.4137 +        png_uint_32 last, max;
 87.4138 +
 87.4139 +        for (i = 0; i < num; i++)
 87.4140 +        {
 87.4141 +           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
 87.4142 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 87.4143 +        }
 87.4144 +
 87.4145 +        g = 1.0 / g;
 87.4146 +        last = 0;
 87.4147 +        for (i = 0; i < 256; i++)
 87.4148 +        {
 87.4149 +           fout = ((double)i + 0.5) / 256.0;
 87.4150 +           fin = pow(fout, g);
 87.4151 +           max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
 87.4152 +           while (last <= max)
 87.4153 +           {
 87.4154 +              png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
 87.4155 +                 [(int)(last >> (8 - shift))] = (png_uint_16)(
 87.4156 +                 (png_uint_16)i | ((png_uint_16)i << 8));
 87.4157 +              last++;
 87.4158 +           }
 87.4159 +        }
 87.4160 +        while (last < ((png_uint_32)num << 8))
 87.4161 +        {
 87.4162 +           png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
 87.4163 +              [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
 87.4164 +           last++;
 87.4165 +        }
 87.4166 +     }
 87.4167 +     else
 87.4168 +     {
 87.4169 +        for (i = 0; i < num; i++)
 87.4170 +        {
 87.4171 +           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
 87.4172 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 87.4173 +
 87.4174 +           ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
 87.4175 +           for (j = 0; j < 256; j++)
 87.4176 +           {
 87.4177 +              png_ptr->gamma_16_table[i][j] =
 87.4178 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 87.4179 +                    65535.0, g) * 65535.0 + .5);
 87.4180 +           }
 87.4181 +        }
 87.4182 +     }
 87.4183 +
 87.4184 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
 87.4185 +   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 87.4186 +     if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
 87.4187 +     {
 87.4188 +
 87.4189 +        g = 1.0 / (png_ptr->gamma);
 87.4190 +
 87.4191 +        png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
 87.4192 +           (png_uint_32)(num * png_sizeof(png_uint_16p )));
 87.4193 +
 87.4194 +        for (i = 0; i < num; i++)
 87.4195 +        {
 87.4196 +           png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
 87.4197 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 87.4198 +
 87.4199 +           ig = (((png_uint_32)i *
 87.4200 +              (png_uint_32)png_gamma_shift[shift]) >> 4);
 87.4201 +           for (j = 0; j < 256; j++)
 87.4202 +           {
 87.4203 +              png_ptr->gamma_16_to_1[i][j] =
 87.4204 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 87.4205 +                    65535.0, g) * 65535.0 + .5);
 87.4206 +           }
 87.4207 +        }
 87.4208 +
 87.4209 +        if (png_ptr->screen_gamma > 0.000001)
 87.4210 +           g = 1.0 / png_ptr->screen_gamma;
 87.4211 +        else
 87.4212 +           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
 87.4213 +
 87.4214 +        png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
 87.4215 +           (png_uint_32)(num * png_sizeof(png_uint_16p)));
 87.4216 +
 87.4217 +        for (i = 0; i < num; i++)
 87.4218 +        {
 87.4219 +           png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
 87.4220 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 87.4221 +
 87.4222 +           ig = (((png_uint_32)i *
 87.4223 +              (png_uint_32)png_gamma_shift[shift]) >> 4);
 87.4224 +           for (j = 0; j < 256; j++)
 87.4225 +           {
 87.4226 +              png_ptr->gamma_16_from_1[i][j] =
 87.4227 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 87.4228 +                    65535.0, g) * 65535.0 + .5);
 87.4229 +           }
 87.4230 +        }
 87.4231 +     }
 87.4232 +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
 87.4233 +  }
 87.4234 +}
 87.4235 +#endif
 87.4236 +/* To do: install integer version of png_build_gamma_table here */
 87.4237 +#endif
 87.4238 +
 87.4239 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 87.4240 +/* undoes intrapixel differencing  */
 87.4241 +void /* PRIVATE */
 87.4242 +png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
 87.4243 +{
 87.4244 +   png_debug(1, "in png_do_read_intrapixel\n");
 87.4245 +   if (
 87.4246 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 87.4247 +       row != NULL && row_info != NULL &&
 87.4248 +#endif
 87.4249 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
 87.4250 +   {
 87.4251 +      int bytes_per_pixel;
 87.4252 +      png_uint_32 row_width = row_info->width;
 87.4253 +      if (row_info->bit_depth == 8)
 87.4254 +      {
 87.4255 +         png_bytep rp;
 87.4256 +         png_uint_32 i;
 87.4257 +
 87.4258 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 87.4259 +            bytes_per_pixel = 3;
 87.4260 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 87.4261 +            bytes_per_pixel = 4;
 87.4262 +         else
 87.4263 +            return;
 87.4264 +
 87.4265 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
 87.4266 +         {
 87.4267 +            *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
 87.4268 +            *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
 87.4269 +         }
 87.4270 +      }
 87.4271 +      else if (row_info->bit_depth == 16)
 87.4272 +      {
 87.4273 +         png_bytep rp;
 87.4274 +         png_uint_32 i;
 87.4275 +
 87.4276 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 87.4277 +            bytes_per_pixel = 6;
 87.4278 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 87.4279 +            bytes_per_pixel = 8;
 87.4280 +         else
 87.4281 +            return;
 87.4282 +
 87.4283 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
 87.4284 +         {
 87.4285 +            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
 87.4286 +            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
 87.4287 +            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
 87.4288 +            png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
 87.4289 +            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
 87.4290 +            *(rp  ) = (png_byte)((red >> 8) & 0xff);
 87.4291 +            *(rp+1) = (png_byte)(red & 0xff);
 87.4292 +            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
 87.4293 +            *(rp+5) = (png_byte)(blue & 0xff);
 87.4294 +         }
 87.4295 +      }
 87.4296 +   }
 87.4297 +}
 87.4298 +#endif /* PNG_MNG_FEATURES_SUPPORTED */
 87.4299 +#endif /* PNG_READ_SUPPORTED */
    88.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    88.2 +++ b/libs/libpng/pngrutil.c	Sun Jun 07 17:25:49 2015 +0300
    88.3 @@ -0,0 +1,3234 @@
    88.4 +
    88.5 +/* pngrutil.c - utilities to read a PNG file
    88.6 + *
    88.7 + * Last changed in libpng 1.2.33 [October 31, 2008]
    88.8 + * For conditions of distribution and use, see copyright notice in png.h
    88.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   88.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   88.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   88.12 + *
   88.13 + * This file contains routines that are only called from within
   88.14 + * libpng itself during the course of reading an image.
   88.15 + */
   88.16 +
   88.17 +#define PNG_INTERNAL
   88.18 +#include "png.h"
   88.19 +#if defined(PNG_READ_SUPPORTED)
   88.20 +
   88.21 +#if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
   88.22 +#  define WIN32_WCE_OLD
   88.23 +#endif
   88.24 +
   88.25 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   88.26 +#  if defined(WIN32_WCE_OLD)
   88.27 +/* strtod() function is not supported on WindowsCE */
   88.28 +__inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
   88.29 +{
   88.30 +   double result = 0;
   88.31 +   int len;
   88.32 +   wchar_t *str, *end;
   88.33 +
   88.34 +   len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
   88.35 +   str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
   88.36 +   if ( NULL != str )
   88.37 +   {
   88.38 +      MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
   88.39 +      result = wcstod(str, &end);
   88.40 +      len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
   88.41 +      *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
   88.42 +      png_free(png_ptr, str);
   88.43 +   }
   88.44 +   return result;
   88.45 +}
   88.46 +#  else
   88.47 +#    define png_strtod(p,a,b) strtod(a,b)
   88.48 +#  endif
   88.49 +#endif
   88.50 +
   88.51 +png_uint_32 PNGAPI
   88.52 +png_get_uint_31(png_structp png_ptr, png_bytep buf)
   88.53 +{
   88.54 +#ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
   88.55 +   png_uint_32 i = png_get_uint_32(buf);
   88.56 +#else
   88.57 +   /* Avoid an extra function call by inlining the result. */
   88.58 +   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
   88.59 +      ((png_uint_32)(*(buf + 1)) << 16) +
   88.60 +      ((png_uint_32)(*(buf + 2)) << 8) +
   88.61 +      (png_uint_32)(*(buf + 3));
   88.62 +#endif
   88.63 +   if (i > PNG_UINT_31_MAX)
   88.64 +     png_error(png_ptr, "PNG unsigned integer out of range.");
   88.65 +   return (i);
   88.66 +}
   88.67 +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
   88.68 +/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
   88.69 +png_uint_32 PNGAPI
   88.70 +png_get_uint_32(png_bytep buf)
   88.71 +{
   88.72 +   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
   88.73 +      ((png_uint_32)(*(buf + 1)) << 16) +
   88.74 +      ((png_uint_32)(*(buf + 2)) << 8) +
   88.75 +      (png_uint_32)(*(buf + 3));
   88.76 +
   88.77 +   return (i);
   88.78 +}
   88.79 +
   88.80 +/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
   88.81 + * data is stored in the PNG file in two's complement format, and it is
   88.82 + * assumed that the machine format for signed integers is the same. */
   88.83 +png_int_32 PNGAPI
   88.84 +png_get_int_32(png_bytep buf)
   88.85 +{
   88.86 +   png_int_32 i = ((png_int_32)(*buf) << 24) +
   88.87 +      ((png_int_32)(*(buf + 1)) << 16) +
   88.88 +      ((png_int_32)(*(buf + 2)) << 8) +
   88.89 +      (png_int_32)(*(buf + 3));
   88.90 +
   88.91 +   return (i);
   88.92 +}
   88.93 +
   88.94 +/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
   88.95 +png_uint_16 PNGAPI
   88.96 +png_get_uint_16(png_bytep buf)
   88.97 +{
   88.98 +   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
   88.99 +      (png_uint_16)(*(buf + 1)));
  88.100 +
  88.101 +   return (i);
  88.102 +}
  88.103 +#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
  88.104 +
  88.105 +/* Read the chunk header (length + type name).
  88.106 + * Put the type name into png_ptr->chunk_name, and return the length.
  88.107 + */
  88.108 +png_uint_32 /* PRIVATE */
  88.109 +png_read_chunk_header(png_structp png_ptr)
  88.110 +{
  88.111 +   png_byte buf[8];
  88.112 +   png_uint_32 length;
  88.113 +
  88.114 +   /* read the length and the chunk name */
  88.115 +   png_read_data(png_ptr, buf, 8);
  88.116 +   length = png_get_uint_31(png_ptr, buf);
  88.117 +
  88.118 +   /* put the chunk name into png_ptr->chunk_name */
  88.119 +   png_memcpy(png_ptr->chunk_name, buf + 4, 4);
  88.120 +
  88.121 +   png_debug2(0, "Reading %s chunk, length = %lu\n",
  88.122 +      png_ptr->chunk_name, length);
  88.123 +
  88.124 +   /* reset the crc and run it over the chunk name */
  88.125 +   png_reset_crc(png_ptr);
  88.126 +   png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
  88.127 +
  88.128 +   /* check to see if chunk name is valid */
  88.129 +   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  88.130 +
  88.131 +   return length;
  88.132 +}
  88.133 +
  88.134 +/* Read data, and (optionally) run it through the CRC. */
  88.135 +void /* PRIVATE */
  88.136 +png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  88.137 +{
  88.138 +   if (png_ptr == NULL) return;
  88.139 +   png_read_data(png_ptr, buf, length);
  88.140 +   png_calculate_crc(png_ptr, buf, length);
  88.141 +}
  88.142 +
  88.143 +/* Optionally skip data and then check the CRC.  Depending on whether we
  88.144 +   are reading a ancillary or critical chunk, and how the program has set
  88.145 +   things up, we may calculate the CRC on the data and print a message.
  88.146 +   Returns '1' if there was a CRC error, '0' otherwise. */
  88.147 +int /* PRIVATE */
  88.148 +png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  88.149 +{
  88.150 +   png_size_t i;
  88.151 +   png_size_t istop = png_ptr->zbuf_size;
  88.152 +
  88.153 +   for (i = (png_size_t)skip; i > istop; i -= istop)
  88.154 +   {
  88.155 +      png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  88.156 +   }
  88.157 +   if (i)
  88.158 +   {
  88.159 +      png_crc_read(png_ptr, png_ptr->zbuf, i);
  88.160 +   }
  88.161 +
  88.162 +   if (png_crc_error(png_ptr))
  88.163 +   {
  88.164 +      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
  88.165 +           !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  88.166 +          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
  88.167 +          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  88.168 +      {
  88.169 +         png_chunk_warning(png_ptr, "CRC error");
  88.170 +      }
  88.171 +      else
  88.172 +      {
  88.173 +         png_chunk_error(png_ptr, "CRC error");
  88.174 +      }
  88.175 +      return (1);
  88.176 +   }
  88.177 +
  88.178 +   return (0);
  88.179 +}
  88.180 +
  88.181 +/* Compare the CRC stored in the PNG file with that calculated by libpng from
  88.182 +   the data it has read thus far. */
  88.183 +int /* PRIVATE */
  88.184 +png_crc_error(png_structp png_ptr)
  88.185 +{
  88.186 +   png_byte crc_bytes[4];
  88.187 +   png_uint_32 crc;
  88.188 +   int need_crc = 1;
  88.189 +
  88.190 +   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  88.191 +   {
  88.192 +      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  88.193 +          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  88.194 +         need_crc = 0;
  88.195 +   }
  88.196 +   else                                                    /* critical */
  88.197 +   {
  88.198 +      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  88.199 +         need_crc = 0;
  88.200 +   }
  88.201 +
  88.202 +   png_read_data(png_ptr, crc_bytes, 4);
  88.203 +
  88.204 +   if (need_crc)
  88.205 +   {
  88.206 +      crc = png_get_uint_32(crc_bytes);
  88.207 +      return ((int)(crc != png_ptr->crc));
  88.208 +   }
  88.209 +   else
  88.210 +      return (0);
  88.211 +}
  88.212 +
  88.213 +#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  88.214 +    defined(PNG_READ_iCCP_SUPPORTED)
  88.215 +/*
  88.216 + * Decompress trailing data in a chunk.  The assumption is that chunkdata
  88.217 + * points at an allocated area holding the contents of a chunk with a
  88.218 + * trailing compressed part.  What we get back is an allocated area
  88.219 + * holding the original prefix part and an uncompressed version of the
  88.220 + * trailing part (the malloc area passed in is freed).
  88.221 + */
  88.222 +void /* PRIVATE */
  88.223 +png_decompress_chunk(png_structp png_ptr, int comp_type,
  88.224 +                              png_size_t chunklength,
  88.225 +                              png_size_t prefix_size, png_size_t *newlength)
  88.226 +{
  88.227 +   static PNG_CONST char msg[] = "Error decoding compressed text";
  88.228 +   png_charp text;
  88.229 +   png_size_t text_size;
  88.230 +
  88.231 +   if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  88.232 +   {
  88.233 +      int ret = Z_OK;
  88.234 +      png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
  88.235 +      png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
  88.236 +      png_ptr->zstream.next_out = png_ptr->zbuf;
  88.237 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  88.238 +
  88.239 +      text_size = 0;
  88.240 +      text = NULL;
  88.241 +
  88.242 +      while (png_ptr->zstream.avail_in)
  88.243 +      {
  88.244 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  88.245 +         if (ret != Z_OK && ret != Z_STREAM_END)
  88.246 +         {
  88.247 +            if (png_ptr->zstream.msg != NULL)
  88.248 +               png_warning(png_ptr, png_ptr->zstream.msg);
  88.249 +            else
  88.250 +               png_warning(png_ptr, msg);
  88.251 +            inflateReset(&png_ptr->zstream);
  88.252 +            png_ptr->zstream.avail_in = 0;
  88.253 +
  88.254 +            if (text ==  NULL)
  88.255 +            {
  88.256 +               text_size = prefix_size + png_sizeof(msg) + 1;
  88.257 +               text = (png_charp)png_malloc_warn(png_ptr, text_size);
  88.258 +               if (text ==  NULL)
  88.259 +                 {
  88.260 +                    png_free(png_ptr, png_ptr->chunkdata);
  88.261 +                    png_ptr->chunkdata = NULL;
  88.262 +                    png_error(png_ptr, "Not enough memory to decompress chunk");
  88.263 +                 }
  88.264 +               png_memcpy(text, png_ptr->chunkdata, prefix_size);
  88.265 +            }
  88.266 +
  88.267 +            text[text_size - 1] = 0x00;
  88.268 +
  88.269 +            /* Copy what we can of the error message into the text chunk */
  88.270 +            text_size = (png_size_t)(chunklength -
  88.271 +              (text - png_ptr->chunkdata) - 1);
  88.272 +            if (text_size > png_sizeof(msg))
  88.273 +               text_size = png_sizeof(msg);
  88.274 +            png_memcpy(text + prefix_size, msg, text_size);
  88.275 +            break;
  88.276 +         }
  88.277 +         if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  88.278 +         {
  88.279 +            if (text == NULL)
  88.280 +            {
  88.281 +               text_size = prefix_size +
  88.282 +                   png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  88.283 +               text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
  88.284 +               if (text ==  NULL)
  88.285 +               {
  88.286 +                  png_free(png_ptr, png_ptr->chunkdata);
  88.287 +                  png_ptr->chunkdata = NULL;
  88.288 +                  png_error(png_ptr,
  88.289 +                    "Not enough memory to decompress chunk.");
  88.290 +               }
  88.291 +               png_memcpy(text + prefix_size, png_ptr->zbuf,
  88.292 +                    text_size - prefix_size);
  88.293 +               png_memcpy(text, png_ptr->chunkdata, prefix_size);
  88.294 +               *(text + text_size) = 0x00;
  88.295 +            }
  88.296 +            else
  88.297 +            {
  88.298 +               png_charp tmp;
  88.299 +
  88.300 +               tmp = text;
  88.301 +               text = (png_charp)png_malloc_warn(png_ptr,
  88.302 +                  (png_uint_32)(text_size +
  88.303 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
  88.304 +               if (text == NULL)
  88.305 +               {
  88.306 +                  png_free(png_ptr, tmp);
  88.307 +                  png_free(png_ptr, png_ptr->chunkdata);
  88.308 +                  png_ptr->chunkdata = NULL;
  88.309 +                  png_error(png_ptr,
  88.310 +                    "Not enough memory to decompress chunk..");
  88.311 +               }
  88.312 +               png_memcpy(text, tmp, text_size);
  88.313 +               png_free(png_ptr, tmp);
  88.314 +               png_memcpy(text + text_size, png_ptr->zbuf,
  88.315 +                  (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  88.316 +               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  88.317 +               *(text + text_size) = 0x00;
  88.318 +            }
  88.319 +            if (ret == Z_STREAM_END)
  88.320 +               break;
  88.321 +            else
  88.322 +            {
  88.323 +               png_ptr->zstream.next_out = png_ptr->zbuf;
  88.324 +               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  88.325 +            }
  88.326 +         }
  88.327 +      }
  88.328 +      if (ret != Z_STREAM_END)
  88.329 +      {
  88.330 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  88.331 +         char umsg[52];
  88.332 +
  88.333 +         if (ret == Z_BUF_ERROR)
  88.334 +            png_snprintf(umsg, 52,
  88.335 +                "Buffer error in compressed datastream in %s chunk",
  88.336 +                png_ptr->chunk_name);
  88.337 +         else if (ret == Z_DATA_ERROR)
  88.338 +            png_snprintf(umsg, 52,
  88.339 +                "Data error in compressed datastream in %s chunk",
  88.340 +                png_ptr->chunk_name);
  88.341 +         else
  88.342 +            png_snprintf(umsg, 52,
  88.343 +                "Incomplete compressed datastream in %s chunk",
  88.344 +                png_ptr->chunk_name);
  88.345 +         png_warning(png_ptr, umsg);
  88.346 +#else
  88.347 +         png_warning(png_ptr,
  88.348 +            "Incomplete compressed datastream in chunk other than IDAT");
  88.349 +#endif
  88.350 +         text_size = prefix_size;
  88.351 +         if (text ==  NULL)
  88.352 +         {
  88.353 +            text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
  88.354 +            if (text == NULL)
  88.355 +              {
  88.356 +                png_free(png_ptr, png_ptr->chunkdata);
  88.357 +                png_ptr->chunkdata = NULL;
  88.358 +                png_error(png_ptr, "Not enough memory for text.");
  88.359 +              }
  88.360 +            png_memcpy(text, png_ptr->chunkdata, prefix_size);
  88.361 +         }
  88.362 +         *(text + text_size) = 0x00;
  88.363 +      }
  88.364 +
  88.365 +      inflateReset(&png_ptr->zstream);
  88.366 +      png_ptr->zstream.avail_in = 0;
  88.367 +
  88.368 +      png_free(png_ptr, png_ptr->chunkdata);
  88.369 +      png_ptr->chunkdata = text;
  88.370 +      *newlength=text_size;
  88.371 +   }
  88.372 +   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  88.373 +   {
  88.374 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  88.375 +      char umsg[50];
  88.376 +
  88.377 +      png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
  88.378 +      png_warning(png_ptr, umsg);
  88.379 +#else
  88.380 +      png_warning(png_ptr, "Unknown zTXt compression type");
  88.381 +#endif
  88.382 +
  88.383 +      *(png_ptr->chunkdata + prefix_size) = 0x00;
  88.384 +      *newlength = prefix_size;
  88.385 +   }
  88.386 +}
  88.387 +#endif
  88.388 +
  88.389 +/* read and check the IDHR chunk */
  88.390 +void /* PRIVATE */
  88.391 +png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.392 +{
  88.393 +   png_byte buf[13];
  88.394 +   png_uint_32 width, height;
  88.395 +   int bit_depth, color_type, compression_type, filter_type;
  88.396 +   int interlace_type;
  88.397 +
  88.398 +   png_debug(1, "in png_handle_IHDR\n");
  88.399 +
  88.400 +   if (png_ptr->mode & PNG_HAVE_IHDR)
  88.401 +      png_error(png_ptr, "Out of place IHDR");
  88.402 +
  88.403 +   /* check the length */
  88.404 +   if (length != 13)
  88.405 +      png_error(png_ptr, "Invalid IHDR chunk");
  88.406 +
  88.407 +   png_ptr->mode |= PNG_HAVE_IHDR;
  88.408 +
  88.409 +   png_crc_read(png_ptr, buf, 13);
  88.410 +   png_crc_finish(png_ptr, 0);
  88.411 +
  88.412 +   width = png_get_uint_31(png_ptr, buf);
  88.413 +   height = png_get_uint_31(png_ptr, buf + 4);
  88.414 +   bit_depth = buf[8];
  88.415 +   color_type = buf[9];
  88.416 +   compression_type = buf[10];
  88.417 +   filter_type = buf[11];
  88.418 +   interlace_type = buf[12];
  88.419 +
  88.420 +   /* set internal variables */
  88.421 +   png_ptr->width = width;
  88.422 +   png_ptr->height = height;
  88.423 +   png_ptr->bit_depth = (png_byte)bit_depth;
  88.424 +   png_ptr->interlaced = (png_byte)interlace_type;
  88.425 +   png_ptr->color_type = (png_byte)color_type;
  88.426 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  88.427 +   png_ptr->filter_type = (png_byte)filter_type;
  88.428 +#endif
  88.429 +   png_ptr->compression_type = (png_byte)compression_type;
  88.430 +
  88.431 +   /* find number of channels */
  88.432 +   switch (png_ptr->color_type)
  88.433 +   {
  88.434 +      case PNG_COLOR_TYPE_GRAY:
  88.435 +      case PNG_COLOR_TYPE_PALETTE:
  88.436 +         png_ptr->channels = 1;
  88.437 +         break;
  88.438 +      case PNG_COLOR_TYPE_RGB:
  88.439 +         png_ptr->channels = 3;
  88.440 +         break;
  88.441 +      case PNG_COLOR_TYPE_GRAY_ALPHA:
  88.442 +         png_ptr->channels = 2;
  88.443 +         break;
  88.444 +      case PNG_COLOR_TYPE_RGB_ALPHA:
  88.445 +         png_ptr->channels = 4;
  88.446 +         break;
  88.447 +   }
  88.448 +
  88.449 +   /* set up other useful info */
  88.450 +   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  88.451 +   png_ptr->channels);
  88.452 +   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  88.453 +   png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
  88.454 +   png_debug1(3, "channels = %d\n", png_ptr->channels);
  88.455 +   png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
  88.456 +   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  88.457 +      color_type, interlace_type, compression_type, filter_type);
  88.458 +}
  88.459 +
  88.460 +/* read and check the palette */
  88.461 +void /* PRIVATE */
  88.462 +png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.463 +{
  88.464 +   png_color palette[PNG_MAX_PALETTE_LENGTH];
  88.465 +   int num, i;
  88.466 +#ifndef PNG_NO_POINTER_INDEXING
  88.467 +   png_colorp pal_ptr;
  88.468 +#endif
  88.469 +
  88.470 +   png_debug(1, "in png_handle_PLTE\n");
  88.471 +
  88.472 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  88.473 +      png_error(png_ptr, "Missing IHDR before PLTE");
  88.474 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  88.475 +   {
  88.476 +      png_warning(png_ptr, "Invalid PLTE after IDAT");
  88.477 +      png_crc_finish(png_ptr, length);
  88.478 +      return;
  88.479 +   }
  88.480 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  88.481 +      png_error(png_ptr, "Duplicate PLTE chunk");
  88.482 +
  88.483 +   png_ptr->mode |= PNG_HAVE_PLTE;
  88.484 +
  88.485 +   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  88.486 +   {
  88.487 +      png_warning(png_ptr,
  88.488 +        "Ignoring PLTE chunk in grayscale PNG");
  88.489 +      png_crc_finish(png_ptr, length);
  88.490 +      return;
  88.491 +   }
  88.492 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  88.493 +   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  88.494 +   {
  88.495 +      png_crc_finish(png_ptr, length);
  88.496 +      return;
  88.497 +   }
  88.498 +#endif
  88.499 +
  88.500 +   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  88.501 +   {
  88.502 +      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  88.503 +      {
  88.504 +         png_warning(png_ptr, "Invalid palette chunk");
  88.505 +         png_crc_finish(png_ptr, length);
  88.506 +         return;
  88.507 +      }
  88.508 +      else
  88.509 +      {
  88.510 +         png_error(png_ptr, "Invalid palette chunk");
  88.511 +      }
  88.512 +   }
  88.513 +
  88.514 +   num = (int)length / 3;
  88.515 +
  88.516 +#ifndef PNG_NO_POINTER_INDEXING
  88.517 +   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  88.518 +   {
  88.519 +      png_byte buf[3];
  88.520 +
  88.521 +      png_crc_read(png_ptr, buf, 3);
  88.522 +      pal_ptr->red = buf[0];
  88.523 +      pal_ptr->green = buf[1];
  88.524 +      pal_ptr->blue = buf[2];
  88.525 +   }
  88.526 +#else
  88.527 +   for (i = 0; i < num; i++)
  88.528 +   {
  88.529 +      png_byte buf[3];
  88.530 +
  88.531 +      png_crc_read(png_ptr, buf, 3);
  88.532 +      /* don't depend upon png_color being any order */
  88.533 +      palette[i].red = buf[0];
  88.534 +      palette[i].green = buf[1];
  88.535 +      palette[i].blue = buf[2];
  88.536 +   }
  88.537 +#endif
  88.538 +
  88.539 +   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  88.540 +      whatever the normal CRC configuration tells us.  However, if we
  88.541 +      have an RGB image, the PLTE can be considered ancillary, so
  88.542 +      we will act as though it is. */
  88.543 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  88.544 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  88.545 +#endif
  88.546 +   {
  88.547 +      png_crc_finish(png_ptr, 0);
  88.548 +   }
  88.549 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  88.550 +   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
  88.551 +   {
  88.552 +      /* If we don't want to use the data from an ancillary chunk,
  88.553 +         we have two options: an error abort, or a warning and we
  88.554 +         ignore the data in this chunk (which should be OK, since
  88.555 +         it's considered ancillary for a RGB or RGBA image). */
  88.556 +      if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  88.557 +      {
  88.558 +         if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  88.559 +         {
  88.560 +            png_chunk_error(png_ptr, "CRC error");
  88.561 +         }
  88.562 +         else
  88.563 +         {
  88.564 +            png_chunk_warning(png_ptr, "CRC error");
  88.565 +            return;
  88.566 +         }
  88.567 +      }
  88.568 +      /* Otherwise, we (optionally) emit a warning and use the chunk. */
  88.569 +      else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  88.570 +      {
  88.571 +         png_chunk_warning(png_ptr, "CRC error");
  88.572 +      }
  88.573 +   }
  88.574 +#endif
  88.575 +
  88.576 +   png_set_PLTE(png_ptr, info_ptr, palette, num);
  88.577 +
  88.578 +#if defined(PNG_READ_tRNS_SUPPORTED)
  88.579 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  88.580 +   {
  88.581 +      if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  88.582 +      {
  88.583 +         if (png_ptr->num_trans > (png_uint_16)num)
  88.584 +         {
  88.585 +            png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  88.586 +            png_ptr->num_trans = (png_uint_16)num;
  88.587 +         }
  88.588 +         if (info_ptr->num_trans > (png_uint_16)num)
  88.589 +         {
  88.590 +            png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  88.591 +            info_ptr->num_trans = (png_uint_16)num;
  88.592 +         }
  88.593 +      }
  88.594 +   }
  88.595 +#endif
  88.596 +
  88.597 +}
  88.598 +
  88.599 +void /* PRIVATE */
  88.600 +png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.601 +{
  88.602 +   png_debug(1, "in png_handle_IEND\n");
  88.603 +
  88.604 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  88.605 +   {
  88.606 +      png_error(png_ptr, "No image in file");
  88.607 +   }
  88.608 +
  88.609 +   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  88.610 +
  88.611 +   if (length != 0)
  88.612 +   {
  88.613 +      png_warning(png_ptr, "Incorrect IEND chunk length");
  88.614 +   }
  88.615 +   png_crc_finish(png_ptr, length);
  88.616 +
  88.617 +   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
  88.618 +}
  88.619 +
  88.620 +#if defined(PNG_READ_gAMA_SUPPORTED)
  88.621 +void /* PRIVATE */
  88.622 +png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.623 +{
  88.624 +   png_fixed_point igamma;
  88.625 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.626 +   float file_gamma;
  88.627 +#endif
  88.628 +   png_byte buf[4];
  88.629 +
  88.630 +   png_debug(1, "in png_handle_gAMA\n");
  88.631 +
  88.632 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  88.633 +      png_error(png_ptr, "Missing IHDR before gAMA");
  88.634 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  88.635 +   {
  88.636 +      png_warning(png_ptr, "Invalid gAMA after IDAT");
  88.637 +      png_crc_finish(png_ptr, length);
  88.638 +      return;
  88.639 +   }
  88.640 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  88.641 +      /* Should be an error, but we can cope with it */
  88.642 +      png_warning(png_ptr, "Out of place gAMA chunk");
  88.643 +
  88.644 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  88.645 +#if defined(PNG_READ_sRGB_SUPPORTED)
  88.646 +      && !(info_ptr->valid & PNG_INFO_sRGB)
  88.647 +#endif
  88.648 +      )
  88.649 +   {
  88.650 +      png_warning(png_ptr, "Duplicate gAMA chunk");
  88.651 +      png_crc_finish(png_ptr, length);
  88.652 +      return;
  88.653 +   }
  88.654 +
  88.655 +   if (length != 4)
  88.656 +   {
  88.657 +      png_warning(png_ptr, "Incorrect gAMA chunk length");
  88.658 +      png_crc_finish(png_ptr, length);
  88.659 +      return;
  88.660 +   }
  88.661 +
  88.662 +   png_crc_read(png_ptr, buf, 4);
  88.663 +   if (png_crc_finish(png_ptr, 0))
  88.664 +      return;
  88.665 +
  88.666 +   igamma = (png_fixed_point)png_get_uint_32(buf);
  88.667 +   /* check for zero gamma */
  88.668 +   if (igamma == 0)
  88.669 +      {
  88.670 +         png_warning(png_ptr,
  88.671 +           "Ignoring gAMA chunk with gamma=0");
  88.672 +         return;
  88.673 +      }
  88.674 +
  88.675 +#if defined(PNG_READ_sRGB_SUPPORTED)
  88.676 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  88.677 +      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  88.678 +      {
  88.679 +         png_warning(png_ptr,
  88.680 +           "Ignoring incorrect gAMA value when sRGB is also present");
  88.681 +#ifndef PNG_NO_CONSOLE_IO
  88.682 +         fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
  88.683 +#endif
  88.684 +         return;
  88.685 +      }
  88.686 +#endif /* PNG_READ_sRGB_SUPPORTED */
  88.687 +
  88.688 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.689 +   file_gamma = (float)igamma / (float)100000.0;
  88.690 +#  ifdef PNG_READ_GAMMA_SUPPORTED
  88.691 +     png_ptr->gamma = file_gamma;
  88.692 +#  endif
  88.693 +     png_set_gAMA(png_ptr, info_ptr, file_gamma);
  88.694 +#endif
  88.695 +#ifdef PNG_FIXED_POINT_SUPPORTED
  88.696 +   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  88.697 +#endif
  88.698 +}
  88.699 +#endif
  88.700 +
  88.701 +#if defined(PNG_READ_sBIT_SUPPORTED)
  88.702 +void /* PRIVATE */
  88.703 +png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.704 +{
  88.705 +   png_size_t truelen;
  88.706 +   png_byte buf[4];
  88.707 +
  88.708 +   png_debug(1, "in png_handle_sBIT\n");
  88.709 +
  88.710 +   buf[0] = buf[1] = buf[2] = buf[3] = 0;
  88.711 +
  88.712 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  88.713 +      png_error(png_ptr, "Missing IHDR before sBIT");
  88.714 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  88.715 +   {
  88.716 +      png_warning(png_ptr, "Invalid sBIT after IDAT");
  88.717 +      png_crc_finish(png_ptr, length);
  88.718 +      return;
  88.719 +   }
  88.720 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  88.721 +   {
  88.722 +      /* Should be an error, but we can cope with it */
  88.723 +      png_warning(png_ptr, "Out of place sBIT chunk");
  88.724 +   }
  88.725 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  88.726 +   {
  88.727 +      png_warning(png_ptr, "Duplicate sBIT chunk");
  88.728 +      png_crc_finish(png_ptr, length);
  88.729 +      return;
  88.730 +   }
  88.731 +
  88.732 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  88.733 +      truelen = 3;
  88.734 +   else
  88.735 +      truelen = (png_size_t)png_ptr->channels;
  88.736 +
  88.737 +   if (length != truelen || length > 4)
  88.738 +   {
  88.739 +      png_warning(png_ptr, "Incorrect sBIT chunk length");
  88.740 +      png_crc_finish(png_ptr, length);
  88.741 +      return;
  88.742 +   }
  88.743 +
  88.744 +   png_crc_read(png_ptr, buf, truelen);
  88.745 +   if (png_crc_finish(png_ptr, 0))
  88.746 +      return;
  88.747 +
  88.748 +   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  88.749 +   {
  88.750 +      png_ptr->sig_bit.red = buf[0];
  88.751 +      png_ptr->sig_bit.green = buf[1];
  88.752 +      png_ptr->sig_bit.blue = buf[2];
  88.753 +      png_ptr->sig_bit.alpha = buf[3];
  88.754 +   }
  88.755 +   else
  88.756 +   {
  88.757 +      png_ptr->sig_bit.gray = buf[0];
  88.758 +      png_ptr->sig_bit.red = buf[0];
  88.759 +      png_ptr->sig_bit.green = buf[0];
  88.760 +      png_ptr->sig_bit.blue = buf[0];
  88.761 +      png_ptr->sig_bit.alpha = buf[1];
  88.762 +   }
  88.763 +   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  88.764 +}
  88.765 +#endif
  88.766 +
  88.767 +#if defined(PNG_READ_cHRM_SUPPORTED)
  88.768 +void /* PRIVATE */
  88.769 +png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.770 +{
  88.771 +   png_byte buf[32];
  88.772 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.773 +   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  88.774 +#endif
  88.775 +   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  88.776 +      int_y_green, int_x_blue, int_y_blue;
  88.777 +
  88.778 +   png_uint_32 uint_x, uint_y;
  88.779 +
  88.780 +   png_debug(1, "in png_handle_cHRM\n");
  88.781 +
  88.782 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  88.783 +      png_error(png_ptr, "Missing IHDR before cHRM");
  88.784 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  88.785 +   {
  88.786 +      png_warning(png_ptr, "Invalid cHRM after IDAT");
  88.787 +      png_crc_finish(png_ptr, length);
  88.788 +      return;
  88.789 +   }
  88.790 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  88.791 +      /* Should be an error, but we can cope with it */
  88.792 +      png_warning(png_ptr, "Missing PLTE before cHRM");
  88.793 +
  88.794 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  88.795 +#if defined(PNG_READ_sRGB_SUPPORTED)
  88.796 +      && !(info_ptr->valid & PNG_INFO_sRGB)
  88.797 +#endif
  88.798 +      )
  88.799 +   {
  88.800 +      png_warning(png_ptr, "Duplicate cHRM chunk");
  88.801 +      png_crc_finish(png_ptr, length);
  88.802 +      return;
  88.803 +   }
  88.804 +
  88.805 +   if (length != 32)
  88.806 +   {
  88.807 +      png_warning(png_ptr, "Incorrect cHRM chunk length");
  88.808 +      png_crc_finish(png_ptr, length);
  88.809 +      return;
  88.810 +   }
  88.811 +
  88.812 +   png_crc_read(png_ptr, buf, 32);
  88.813 +   if (png_crc_finish(png_ptr, 0))
  88.814 +      return;
  88.815 +
  88.816 +   uint_x = png_get_uint_32(buf);
  88.817 +   uint_y = png_get_uint_32(buf + 4);
  88.818 +   if (uint_x > 80000L || uint_y > 80000L ||
  88.819 +      uint_x + uint_y > 100000L)
  88.820 +   {
  88.821 +      png_warning(png_ptr, "Invalid cHRM white point");
  88.822 +      return;
  88.823 +   }
  88.824 +   int_x_white = (png_fixed_point)uint_x;
  88.825 +   int_y_white = (png_fixed_point)uint_y;
  88.826 +
  88.827 +   uint_x = png_get_uint_32(buf + 8);
  88.828 +   uint_y = png_get_uint_32(buf + 12);
  88.829 +   if (uint_x + uint_y > 100000L)
  88.830 +   {
  88.831 +      png_warning(png_ptr, "Invalid cHRM red point");
  88.832 +      return;
  88.833 +   }
  88.834 +   int_x_red = (png_fixed_point)uint_x;
  88.835 +   int_y_red = (png_fixed_point)uint_y;
  88.836 +
  88.837 +   uint_x = png_get_uint_32(buf + 16);
  88.838 +   uint_y = png_get_uint_32(buf + 20);
  88.839 +   if (uint_x + uint_y > 100000L)
  88.840 +   {
  88.841 +      png_warning(png_ptr, "Invalid cHRM green point");
  88.842 +      return;
  88.843 +   }
  88.844 +   int_x_green = (png_fixed_point)uint_x;
  88.845 +   int_y_green = (png_fixed_point)uint_y;
  88.846 +
  88.847 +   uint_x = png_get_uint_32(buf + 24);
  88.848 +   uint_y = png_get_uint_32(buf + 28);
  88.849 +   if (uint_x + uint_y > 100000L)
  88.850 +   {
  88.851 +      png_warning(png_ptr, "Invalid cHRM blue point");
  88.852 +      return;
  88.853 +   }
  88.854 +   int_x_blue = (png_fixed_point)uint_x;
  88.855 +   int_y_blue = (png_fixed_point)uint_y;
  88.856 +
  88.857 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.858 +   white_x = (float)int_x_white / (float)100000.0;
  88.859 +   white_y = (float)int_y_white / (float)100000.0;
  88.860 +   red_x   = (float)int_x_red   / (float)100000.0;
  88.861 +   red_y   = (float)int_y_red   / (float)100000.0;
  88.862 +   green_x = (float)int_x_green / (float)100000.0;
  88.863 +   green_y = (float)int_y_green / (float)100000.0;
  88.864 +   blue_x  = (float)int_x_blue  / (float)100000.0;
  88.865 +   blue_y  = (float)int_y_blue  / (float)100000.0;
  88.866 +#endif
  88.867 +
  88.868 +#if defined(PNG_READ_sRGB_SUPPORTED)
  88.869 +   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
  88.870 +      {
  88.871 +      if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
  88.872 +          PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
  88.873 +          PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
  88.874 +          PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||
  88.875 +          PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||
  88.876 +          PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
  88.877 +          PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
  88.878 +          PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
  88.879 +         {
  88.880 +            png_warning(png_ptr,
  88.881 +              "Ignoring incorrect cHRM value when sRGB is also present");
  88.882 +#ifndef PNG_NO_CONSOLE_IO
  88.883 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.884 +            fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
  88.885 +               white_x, white_y, red_x, red_y);
  88.886 +            fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
  88.887 +               green_x, green_y, blue_x, blue_y);
  88.888 +#else
  88.889 +            fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
  88.890 +               int_x_white, int_y_white, int_x_red, int_y_red);
  88.891 +            fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
  88.892 +               int_x_green, int_y_green, int_x_blue, int_y_blue);
  88.893 +#endif
  88.894 +#endif /* PNG_NO_CONSOLE_IO */
  88.895 +         }
  88.896 +         return;
  88.897 +      }
  88.898 +#endif /* PNG_READ_sRGB_SUPPORTED */
  88.899 +
  88.900 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  88.901 +   png_set_cHRM(png_ptr, info_ptr,
  88.902 +      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  88.903 +#endif
  88.904 +#ifdef PNG_FIXED_POINT_SUPPORTED
  88.905 +   png_set_cHRM_fixed(png_ptr, info_ptr,
  88.906 +      int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  88.907 +      int_y_green, int_x_blue, int_y_blue);
  88.908 +#endif
  88.909 +}
  88.910 +#endif
  88.911 +
  88.912 +#if defined(PNG_READ_sRGB_SUPPORTED)
  88.913 +void /* PRIVATE */
  88.914 +png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  88.915 +{
  88.916 +   int intent;
  88.917 +   png_byte buf[1];
  88.918 +
  88.919 +   png_debug(1, "in png_handle_sRGB\n");
  88.920 +
  88.921 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  88.922 +      png_error(png_ptr, "Missing IHDR before sRGB");
  88.923 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  88.924 +   {
  88.925 +      png_warning(png_ptr, "Invalid sRGB after IDAT");
  88.926 +      png_crc_finish(png_ptr, length);
  88.927 +      return;
  88.928 +   }
  88.929 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  88.930 +      /* Should be an error, but we can cope with it */
  88.931 +      png_warning(png_ptr, "Out of place sRGB chunk");
  88.932 +
  88.933 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  88.934 +   {
  88.935 +      png_warning(png_ptr, "Duplicate sRGB chunk");
  88.936 +      png_crc_finish(png_ptr, length);
  88.937 +      return;
  88.938 +   }
  88.939 +
  88.940 +   if (length != 1)
  88.941 +   {
  88.942 +      png_warning(png_ptr, "Incorrect sRGB chunk length");
  88.943 +      png_crc_finish(png_ptr, length);
  88.944 +      return;
  88.945 +   }
  88.946 +
  88.947 +   png_crc_read(png_ptr, buf, 1);
  88.948 +   if (png_crc_finish(png_ptr, 0))
  88.949 +      return;
  88.950 +
  88.951 +   intent = buf[0];
  88.952 +   /* check for bad intent */
  88.953 +   if (intent >= PNG_sRGB_INTENT_LAST)
  88.954 +   {
  88.955 +      png_warning(png_ptr, "Unknown sRGB intent");
  88.956 +      return;
  88.957 +   }
  88.958 +
  88.959 +#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  88.960 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
  88.961 +   {
  88.962 +   png_fixed_point igamma;
  88.963 +#ifdef PNG_FIXED_POINT_SUPPORTED
  88.964 +      igamma=info_ptr->int_gamma;
  88.965 +#else
  88.966 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
  88.967 +      igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
  88.968 +#  endif
  88.969 +#endif
  88.970 +      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  88.971 +      {
  88.972 +         png_warning(png_ptr,
  88.973 +           "Ignoring incorrect gAMA value when sRGB is also present");
  88.974 +#ifndef PNG_NO_CONSOLE_IO
  88.975 +#  ifdef PNG_FIXED_POINT_SUPPORTED
  88.976 +         fprintf(stderr, "incorrect gamma=(%d/100000)\n",
  88.977 +            (int)png_ptr->int_gamma);
  88.978 +#  else
  88.979 +#    ifdef PNG_FLOATING_POINT_SUPPORTED
  88.980 +         fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
  88.981 +#    endif
  88.982 +#  endif
  88.983 +#endif
  88.984 +      }
  88.985 +   }
  88.986 +#endif /* PNG_READ_gAMA_SUPPORTED */
  88.987 +
  88.988 +#ifdef PNG_READ_cHRM_SUPPORTED
  88.989 +#ifdef PNG_FIXED_POINT_SUPPORTED
  88.990 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  88.991 +      if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||
  88.992 +          PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||
  88.993 +          PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||
  88.994 +          PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||
  88.995 +          PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||
  88.996 +          PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
  88.997 +          PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||
  88.998 +          PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))
  88.999 +         {
 88.1000 +            png_warning(png_ptr,
 88.1001 +              "Ignoring incorrect cHRM value when sRGB is also present");
 88.1002 +         }
 88.1003 +#endif /* PNG_FIXED_POINT_SUPPORTED */
 88.1004 +#endif /* PNG_READ_cHRM_SUPPORTED */
 88.1005 +
 88.1006 +   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
 88.1007 +}
 88.1008 +#endif /* PNG_READ_sRGB_SUPPORTED */
 88.1009 +
 88.1010 +#if defined(PNG_READ_iCCP_SUPPORTED)
 88.1011 +void /* PRIVATE */
 88.1012 +png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1013 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 88.1014 +{
 88.1015 +   png_byte compression_type;
 88.1016 +   png_bytep pC;
 88.1017 +   png_charp profile;
 88.1018 +   png_uint_32 skip = 0;
 88.1019 +   png_uint_32 profile_size, profile_length;
 88.1020 +   png_size_t slength, prefix_length, data_length;
 88.1021 +
 88.1022 +   png_debug(1, "in png_handle_iCCP\n");
 88.1023 +
 88.1024 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1025 +      png_error(png_ptr, "Missing IHDR before iCCP");
 88.1026 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1027 +   {
 88.1028 +      png_warning(png_ptr, "Invalid iCCP after IDAT");
 88.1029 +      png_crc_finish(png_ptr, length);
 88.1030 +      return;
 88.1031 +   }
 88.1032 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
 88.1033 +      /* Should be an error, but we can cope with it */
 88.1034 +      png_warning(png_ptr, "Out of place iCCP chunk");
 88.1035 +
 88.1036 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
 88.1037 +   {
 88.1038 +      png_warning(png_ptr, "Duplicate iCCP chunk");
 88.1039 +      png_crc_finish(png_ptr, length);
 88.1040 +      return;
 88.1041 +   }
 88.1042 +
 88.1043 +#ifdef PNG_MAX_MALLOC_64K
 88.1044 +   if (length > (png_uint_32)65535L)
 88.1045 +   {
 88.1046 +      png_warning(png_ptr, "iCCP chunk too large to fit in memory");
 88.1047 +      skip = length - (png_uint_32)65535L;
 88.1048 +      length = (png_uint_32)65535L;
 88.1049 +   }
 88.1050 +#endif
 88.1051 +
 88.1052 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1053 +   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
 88.1054 +   slength = (png_size_t)length;
 88.1055 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.1056 +
 88.1057 +   if (png_crc_finish(png_ptr, skip))
 88.1058 +   {
 88.1059 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1060 +      png_ptr->chunkdata = NULL;
 88.1061 +      return;
 88.1062 +   }
 88.1063 +
 88.1064 +   png_ptr->chunkdata[slength] = 0x00;
 88.1065 +
 88.1066 +   for (profile = png_ptr->chunkdata; *profile; profile++)
 88.1067 +      /* empty loop to find end of name */ ;
 88.1068 +
 88.1069 +   ++profile;
 88.1070 +
 88.1071 +   /* there should be at least one zero (the compression type byte)
 88.1072 +      following the separator, and we should be on it  */
 88.1073 +   if ( profile >= png_ptr->chunkdata + slength - 1)
 88.1074 +   {
 88.1075 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1076 +      png_ptr->chunkdata = NULL;
 88.1077 +      png_warning(png_ptr, "Malformed iCCP chunk");
 88.1078 +      return;
 88.1079 +   }
 88.1080 +
 88.1081 +   /* compression_type should always be zero */
 88.1082 +   compression_type = *profile++;
 88.1083 +   if (compression_type)
 88.1084 +   {
 88.1085 +      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
 88.1086 +      compression_type = 0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
 88.1087 +                                 wrote nonzero) */
 88.1088 +   }
 88.1089 +
 88.1090 +   prefix_length = profile - png_ptr->chunkdata;
 88.1091 +   png_decompress_chunk(png_ptr, compression_type,
 88.1092 +     slength, prefix_length, &data_length);
 88.1093 +
 88.1094 +   profile_length = data_length - prefix_length;
 88.1095 +
 88.1096 +   if ( prefix_length > data_length || profile_length < 4)
 88.1097 +   {
 88.1098 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1099 +      png_ptr->chunkdata = NULL;
 88.1100 +      png_warning(png_ptr, "Profile size field missing from iCCP chunk");
 88.1101 +      return;
 88.1102 +   }
 88.1103 +
 88.1104 +   /* Check the profile_size recorded in the first 32 bits of the ICC profile */
 88.1105 +   pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
 88.1106 +   profile_size = ((*(pC    ))<<24) |
 88.1107 +                  ((*(pC + 1))<<16) |
 88.1108 +                  ((*(pC + 2))<< 8) |
 88.1109 +                  ((*(pC + 3))    );
 88.1110 +
 88.1111 +   if (profile_size < profile_length)
 88.1112 +      profile_length = profile_size;
 88.1113 +
 88.1114 +   if (profile_size > profile_length)
 88.1115 +   {
 88.1116 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1117 +      png_ptr->chunkdata = NULL;
 88.1118 +      png_warning(png_ptr, "Ignoring truncated iCCP profile.");
 88.1119 +      return;
 88.1120 +   }
 88.1121 +
 88.1122 +   png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
 88.1123 +     compression_type, png_ptr->chunkdata + prefix_length, profile_length);
 88.1124 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1125 +   png_ptr->chunkdata = NULL;
 88.1126 +}
 88.1127 +#endif /* PNG_READ_iCCP_SUPPORTED */
 88.1128 +
 88.1129 +#if defined(PNG_READ_sPLT_SUPPORTED)
 88.1130 +void /* PRIVATE */
 88.1131 +png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1132 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 88.1133 +{
 88.1134 +   png_bytep entry_start;
 88.1135 +   png_sPLT_t new_palette;
 88.1136 +#ifdef PNG_NO_POINTER_INDEXING
 88.1137 +   png_sPLT_entryp pp;
 88.1138 +#endif
 88.1139 +   int data_length, entry_size, i;
 88.1140 +   png_uint_32 skip = 0;
 88.1141 +   png_size_t slength;
 88.1142 +
 88.1143 +   png_debug(1, "in png_handle_sPLT\n");
 88.1144 +
 88.1145 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1146 +      png_error(png_ptr, "Missing IHDR before sPLT");
 88.1147 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1148 +   {
 88.1149 +      png_warning(png_ptr, "Invalid sPLT after IDAT");
 88.1150 +      png_crc_finish(png_ptr, length);
 88.1151 +      return;
 88.1152 +   }
 88.1153 +
 88.1154 +#ifdef PNG_MAX_MALLOC_64K
 88.1155 +   if (length > (png_uint_32)65535L)
 88.1156 +   {
 88.1157 +      png_warning(png_ptr, "sPLT chunk too large to fit in memory");
 88.1158 +      skip = length - (png_uint_32)65535L;
 88.1159 +      length = (png_uint_32)65535L;
 88.1160 +   }
 88.1161 +#endif
 88.1162 +
 88.1163 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1164 +   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
 88.1165 +   slength = (png_size_t)length;
 88.1166 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.1167 +
 88.1168 +   if (png_crc_finish(png_ptr, skip))
 88.1169 +   {
 88.1170 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1171 +      png_ptr->chunkdata = NULL;
 88.1172 +      return;
 88.1173 +   }
 88.1174 +
 88.1175 +   png_ptr->chunkdata[slength] = 0x00;
 88.1176 +
 88.1177 +   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
 88.1178 +      /* empty loop to find end of name */ ;
 88.1179 +   ++entry_start;
 88.1180 +
 88.1181 +   /* a sample depth should follow the separator, and we should be on it  */
 88.1182 +   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
 88.1183 +   {
 88.1184 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1185 +      png_ptr->chunkdata = NULL;
 88.1186 +      png_warning(png_ptr, "malformed sPLT chunk");
 88.1187 +      return;
 88.1188 +   }
 88.1189 +
 88.1190 +   new_palette.depth = *entry_start++;
 88.1191 +   entry_size = (new_palette.depth == 8 ? 6 : 10);
 88.1192 +   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
 88.1193 +
 88.1194 +   /* integrity-check the data length */
 88.1195 +   if (data_length % entry_size)
 88.1196 +   {
 88.1197 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1198 +      png_ptr->chunkdata = NULL;
 88.1199 +      png_warning(png_ptr, "sPLT chunk has bad length");
 88.1200 +      return;
 88.1201 +   }
 88.1202 +
 88.1203 +   new_palette.nentries = (png_int_32) ( data_length / entry_size);
 88.1204 +   if ((png_uint_32) new_palette.nentries >
 88.1205 +       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
 88.1206 +   {
 88.1207 +       png_warning(png_ptr, "sPLT chunk too long");
 88.1208 +       return;
 88.1209 +   }
 88.1210 +   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
 88.1211 +       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
 88.1212 +   if (new_palette.entries == NULL)
 88.1213 +   {
 88.1214 +       png_warning(png_ptr, "sPLT chunk requires too much memory");
 88.1215 +       return;
 88.1216 +   }
 88.1217 +
 88.1218 +#ifndef PNG_NO_POINTER_INDEXING
 88.1219 +   for (i = 0; i < new_palette.nentries; i++)
 88.1220 +   {
 88.1221 +      png_sPLT_entryp pp = new_palette.entries + i;
 88.1222 +
 88.1223 +      if (new_palette.depth == 8)
 88.1224 +      {
 88.1225 +          pp->red = *entry_start++;
 88.1226 +          pp->green = *entry_start++;
 88.1227 +          pp->blue = *entry_start++;
 88.1228 +          pp->alpha = *entry_start++;
 88.1229 +      }
 88.1230 +      else
 88.1231 +      {
 88.1232 +          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
 88.1233 +          pp->green = png_get_uint_16(entry_start); entry_start += 2;
 88.1234 +          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
 88.1235 +          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
 88.1236 +      }
 88.1237 +      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
 88.1238 +   }
 88.1239 +#else
 88.1240 +   pp = new_palette.entries;
 88.1241 +   for (i = 0; i < new_palette.nentries; i++)
 88.1242 +   {
 88.1243 +
 88.1244 +      if (new_palette.depth == 8)
 88.1245 +      {
 88.1246 +          pp[i].red   = *entry_start++;
 88.1247 +          pp[i].green = *entry_start++;
 88.1248 +          pp[i].blue  = *entry_start++;
 88.1249 +          pp[i].alpha = *entry_start++;
 88.1250 +      }
 88.1251 +      else
 88.1252 +      {
 88.1253 +          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
 88.1254 +          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
 88.1255 +          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
 88.1256 +          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
 88.1257 +      }
 88.1258 +      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
 88.1259 +   }
 88.1260 +#endif
 88.1261 +
 88.1262 +   /* discard all chunk data except the name and stash that */
 88.1263 +   new_palette.name = png_ptr->chunkdata;
 88.1264 +
 88.1265 +   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
 88.1266 +
 88.1267 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1268 +   png_ptr->chunkdata = NULL;
 88.1269 +   png_free(png_ptr, new_palette.entries);
 88.1270 +}
 88.1271 +#endif /* PNG_READ_sPLT_SUPPORTED */
 88.1272 +
 88.1273 +#if defined(PNG_READ_tRNS_SUPPORTED)
 88.1274 +void /* PRIVATE */
 88.1275 +png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1276 +{
 88.1277 +   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
 88.1278 +
 88.1279 +   png_debug(1, "in png_handle_tRNS\n");
 88.1280 +
 88.1281 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1282 +      png_error(png_ptr, "Missing IHDR before tRNS");
 88.1283 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1284 +   {
 88.1285 +      png_warning(png_ptr, "Invalid tRNS after IDAT");
 88.1286 +      png_crc_finish(png_ptr, length);
 88.1287 +      return;
 88.1288 +   }
 88.1289 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
 88.1290 +   {
 88.1291 +      png_warning(png_ptr, "Duplicate tRNS chunk");
 88.1292 +      png_crc_finish(png_ptr, length);
 88.1293 +      return;
 88.1294 +   }
 88.1295 +
 88.1296 +   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 88.1297 +   {
 88.1298 +      png_byte buf[2];
 88.1299 +
 88.1300 +      if (length != 2)
 88.1301 +      {
 88.1302 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 88.1303 +         png_crc_finish(png_ptr, length);
 88.1304 +         return;
 88.1305 +      }
 88.1306 +
 88.1307 +      png_crc_read(png_ptr, buf, 2);
 88.1308 +      png_ptr->num_trans = 1;
 88.1309 +      png_ptr->trans_values.gray = png_get_uint_16(buf);
 88.1310 +   }
 88.1311 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 88.1312 +   {
 88.1313 +      png_byte buf[6];
 88.1314 +
 88.1315 +      if (length != 6)
 88.1316 +      {
 88.1317 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 88.1318 +         png_crc_finish(png_ptr, length);
 88.1319 +         return;
 88.1320 +      }
 88.1321 +      png_crc_read(png_ptr, buf, (png_size_t)length);
 88.1322 +      png_ptr->num_trans = 1;
 88.1323 +      png_ptr->trans_values.red = png_get_uint_16(buf);
 88.1324 +      png_ptr->trans_values.green = png_get_uint_16(buf + 2);
 88.1325 +      png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
 88.1326 +   }
 88.1327 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 88.1328 +   {
 88.1329 +      if (!(png_ptr->mode & PNG_HAVE_PLTE))
 88.1330 +      {
 88.1331 +         /* Should be an error, but we can cope with it. */
 88.1332 +         png_warning(png_ptr, "Missing PLTE before tRNS");
 88.1333 +      }
 88.1334 +      if (length > (png_uint_32)png_ptr->num_palette ||
 88.1335 +          length > PNG_MAX_PALETTE_LENGTH)
 88.1336 +      {
 88.1337 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 88.1338 +         png_crc_finish(png_ptr, length);
 88.1339 +         return;
 88.1340 +      }
 88.1341 +      if (length == 0)
 88.1342 +      {
 88.1343 +         png_warning(png_ptr, "Zero length tRNS chunk");
 88.1344 +         png_crc_finish(png_ptr, length);
 88.1345 +         return;
 88.1346 +      }
 88.1347 +      png_crc_read(png_ptr, readbuf, (png_size_t)length);
 88.1348 +      png_ptr->num_trans = (png_uint_16)length;
 88.1349 +   }
 88.1350 +   else
 88.1351 +   {
 88.1352 +      png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
 88.1353 +      png_crc_finish(png_ptr, length);
 88.1354 +      return;
 88.1355 +   }
 88.1356 +
 88.1357 +   if (png_crc_finish(png_ptr, 0))
 88.1358 +   {
 88.1359 +      png_ptr->num_trans = 0;
 88.1360 +      return;
 88.1361 +   }
 88.1362 +
 88.1363 +   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
 88.1364 +      &(png_ptr->trans_values));
 88.1365 +}
 88.1366 +#endif
 88.1367 +
 88.1368 +#if defined(PNG_READ_bKGD_SUPPORTED)
 88.1369 +void /* PRIVATE */
 88.1370 +png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1371 +{
 88.1372 +   png_size_t truelen;
 88.1373 +   png_byte buf[6];
 88.1374 +
 88.1375 +   png_debug(1, "in png_handle_bKGD\n");
 88.1376 +
 88.1377 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1378 +      png_error(png_ptr, "Missing IHDR before bKGD");
 88.1379 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1380 +   {
 88.1381 +      png_warning(png_ptr, "Invalid bKGD after IDAT");
 88.1382 +      png_crc_finish(png_ptr, length);
 88.1383 +      return;
 88.1384 +   }
 88.1385 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
 88.1386 +            !(png_ptr->mode & PNG_HAVE_PLTE))
 88.1387 +   {
 88.1388 +      png_warning(png_ptr, "Missing PLTE before bKGD");
 88.1389 +      png_crc_finish(png_ptr, length);
 88.1390 +      return;
 88.1391 +   }
 88.1392 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
 88.1393 +   {
 88.1394 +      png_warning(png_ptr, "Duplicate bKGD chunk");
 88.1395 +      png_crc_finish(png_ptr, length);
 88.1396 +      return;
 88.1397 +   }
 88.1398 +
 88.1399 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 88.1400 +      truelen = 1;
 88.1401 +   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
 88.1402 +      truelen = 6;
 88.1403 +   else
 88.1404 +      truelen = 2;
 88.1405 +
 88.1406 +   if (length != truelen)
 88.1407 +   {
 88.1408 +      png_warning(png_ptr, "Incorrect bKGD chunk length");
 88.1409 +      png_crc_finish(png_ptr, length);
 88.1410 +      return;
 88.1411 +   }
 88.1412 +
 88.1413 +   png_crc_read(png_ptr, buf, truelen);
 88.1414 +   if (png_crc_finish(png_ptr, 0))
 88.1415 +      return;
 88.1416 +
 88.1417 +   /* We convert the index value into RGB components so that we can allow
 88.1418 +    * arbitrary RGB values for background when we have transparency, and
 88.1419 +    * so it is easy to determine the RGB values of the background color
 88.1420 +    * from the info_ptr struct. */
 88.1421 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 88.1422 +   {
 88.1423 +      png_ptr->background.index = buf[0];
 88.1424 +      if (info_ptr && info_ptr->num_palette)
 88.1425 +      {
 88.1426 +          if (buf[0] > info_ptr->num_palette)
 88.1427 +          {
 88.1428 +             png_warning(png_ptr, "Incorrect bKGD chunk index value");
 88.1429 +             return;
 88.1430 +          }
 88.1431 +          png_ptr->background.red =
 88.1432 +             (png_uint_16)png_ptr->palette[buf[0]].red;
 88.1433 +          png_ptr->background.green =
 88.1434 +             (png_uint_16)png_ptr->palette[buf[0]].green;
 88.1435 +          png_ptr->background.blue =
 88.1436 +             (png_uint_16)png_ptr->palette[buf[0]].blue;
 88.1437 +      }
 88.1438 +   }
 88.1439 +   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
 88.1440 +   {
 88.1441 +      png_ptr->background.red =
 88.1442 +      png_ptr->background.green =
 88.1443 +      png_ptr->background.blue =
 88.1444 +      png_ptr->background.gray = png_get_uint_16(buf);
 88.1445 +   }
 88.1446 +   else
 88.1447 +   {
 88.1448 +      png_ptr->background.red = png_get_uint_16(buf);
 88.1449 +      png_ptr->background.green = png_get_uint_16(buf + 2);
 88.1450 +      png_ptr->background.blue = png_get_uint_16(buf + 4);
 88.1451 +   }
 88.1452 +
 88.1453 +   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
 88.1454 +}
 88.1455 +#endif
 88.1456 +
 88.1457 +#if defined(PNG_READ_hIST_SUPPORTED)
 88.1458 +void /* PRIVATE */
 88.1459 +png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1460 +{
 88.1461 +   unsigned int num, i;
 88.1462 +   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
 88.1463 +
 88.1464 +   png_debug(1, "in png_handle_hIST\n");
 88.1465 +
 88.1466 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1467 +      png_error(png_ptr, "Missing IHDR before hIST");
 88.1468 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1469 +   {
 88.1470 +      png_warning(png_ptr, "Invalid hIST after IDAT");
 88.1471 +      png_crc_finish(png_ptr, length);
 88.1472 +      return;
 88.1473 +   }
 88.1474 +   else if (!(png_ptr->mode & PNG_HAVE_PLTE))
 88.1475 +   {
 88.1476 +      png_warning(png_ptr, "Missing PLTE before hIST");
 88.1477 +      png_crc_finish(png_ptr, length);
 88.1478 +      return;
 88.1479 +   }
 88.1480 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
 88.1481 +   {
 88.1482 +      png_warning(png_ptr, "Duplicate hIST chunk");
 88.1483 +      png_crc_finish(png_ptr, length);
 88.1484 +      return;
 88.1485 +   }
 88.1486 +
 88.1487 +   num = length / 2 ;
 88.1488 +   if (num != (unsigned int) png_ptr->num_palette || num >
 88.1489 +      (unsigned int) PNG_MAX_PALETTE_LENGTH)
 88.1490 +   {
 88.1491 +      png_warning(png_ptr, "Incorrect hIST chunk length");
 88.1492 +      png_crc_finish(png_ptr, length);
 88.1493 +      return;
 88.1494 +   }
 88.1495 +
 88.1496 +   for (i = 0; i < num; i++)
 88.1497 +   {
 88.1498 +      png_byte buf[2];
 88.1499 +
 88.1500 +      png_crc_read(png_ptr, buf, 2);
 88.1501 +      readbuf[i] = png_get_uint_16(buf);
 88.1502 +   }
 88.1503 +
 88.1504 +   if (png_crc_finish(png_ptr, 0))
 88.1505 +      return;
 88.1506 +
 88.1507 +   png_set_hIST(png_ptr, info_ptr, readbuf);
 88.1508 +}
 88.1509 +#endif
 88.1510 +
 88.1511 +#if defined(PNG_READ_pHYs_SUPPORTED)
 88.1512 +void /* PRIVATE */
 88.1513 +png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1514 +{
 88.1515 +   png_byte buf[9];
 88.1516 +   png_uint_32 res_x, res_y;
 88.1517 +   int unit_type;
 88.1518 +
 88.1519 +   png_debug(1, "in png_handle_pHYs\n");
 88.1520 +
 88.1521 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1522 +      png_error(png_ptr, "Missing IHDR before pHYs");
 88.1523 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1524 +   {
 88.1525 +      png_warning(png_ptr, "Invalid pHYs after IDAT");
 88.1526 +      png_crc_finish(png_ptr, length);
 88.1527 +      return;
 88.1528 +   }
 88.1529 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
 88.1530 +   {
 88.1531 +      png_warning(png_ptr, "Duplicate pHYs chunk");
 88.1532 +      png_crc_finish(png_ptr, length);
 88.1533 +      return;
 88.1534 +   }
 88.1535 +
 88.1536 +   if (length != 9)
 88.1537 +   {
 88.1538 +      png_warning(png_ptr, "Incorrect pHYs chunk length");
 88.1539 +      png_crc_finish(png_ptr, length);
 88.1540 +      return;
 88.1541 +   }
 88.1542 +
 88.1543 +   png_crc_read(png_ptr, buf, 9);
 88.1544 +   if (png_crc_finish(png_ptr, 0))
 88.1545 +      return;
 88.1546 +
 88.1547 +   res_x = png_get_uint_32(buf);
 88.1548 +   res_y = png_get_uint_32(buf + 4);
 88.1549 +   unit_type = buf[8];
 88.1550 +   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
 88.1551 +}
 88.1552 +#endif
 88.1553 +
 88.1554 +#if defined(PNG_READ_oFFs_SUPPORTED)
 88.1555 +void /* PRIVATE */
 88.1556 +png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1557 +{
 88.1558 +   png_byte buf[9];
 88.1559 +   png_int_32 offset_x, offset_y;
 88.1560 +   int unit_type;
 88.1561 +
 88.1562 +   png_debug(1, "in png_handle_oFFs\n");
 88.1563 +
 88.1564 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1565 +      png_error(png_ptr, "Missing IHDR before oFFs");
 88.1566 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1567 +   {
 88.1568 +      png_warning(png_ptr, "Invalid oFFs after IDAT");
 88.1569 +      png_crc_finish(png_ptr, length);
 88.1570 +      return;
 88.1571 +   }
 88.1572 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
 88.1573 +   {
 88.1574 +      png_warning(png_ptr, "Duplicate oFFs chunk");
 88.1575 +      png_crc_finish(png_ptr, length);
 88.1576 +      return;
 88.1577 +   }
 88.1578 +
 88.1579 +   if (length != 9)
 88.1580 +   {
 88.1581 +      png_warning(png_ptr, "Incorrect oFFs chunk length");
 88.1582 +      png_crc_finish(png_ptr, length);
 88.1583 +      return;
 88.1584 +   }
 88.1585 +
 88.1586 +   png_crc_read(png_ptr, buf, 9);
 88.1587 +   if (png_crc_finish(png_ptr, 0))
 88.1588 +      return;
 88.1589 +
 88.1590 +   offset_x = png_get_int_32(buf);
 88.1591 +   offset_y = png_get_int_32(buf + 4);
 88.1592 +   unit_type = buf[8];
 88.1593 +   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
 88.1594 +}
 88.1595 +#endif
 88.1596 +
 88.1597 +#if defined(PNG_READ_pCAL_SUPPORTED)
 88.1598 +/* read the pCAL chunk (described in the PNG Extensions document) */
 88.1599 +void /* PRIVATE */
 88.1600 +png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1601 +{
 88.1602 +   png_int_32 X0, X1;
 88.1603 +   png_byte type, nparams;
 88.1604 +   png_charp buf, units, endptr;
 88.1605 +   png_charpp params;
 88.1606 +   png_size_t slength;
 88.1607 +   int i;
 88.1608 +
 88.1609 +   png_debug(1, "in png_handle_pCAL\n");
 88.1610 +
 88.1611 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1612 +      png_error(png_ptr, "Missing IHDR before pCAL");
 88.1613 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1614 +   {
 88.1615 +      png_warning(png_ptr, "Invalid pCAL after IDAT");
 88.1616 +      png_crc_finish(png_ptr, length);
 88.1617 +      return;
 88.1618 +   }
 88.1619 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
 88.1620 +   {
 88.1621 +      png_warning(png_ptr, "Duplicate pCAL chunk");
 88.1622 +      png_crc_finish(png_ptr, length);
 88.1623 +      return;
 88.1624 +   }
 88.1625 +
 88.1626 +   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
 88.1627 +      length + 1);
 88.1628 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1629 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 88.1630 +   if (png_ptr->chunkdata == NULL)
 88.1631 +     {
 88.1632 +       png_warning(png_ptr, "No memory for pCAL purpose.");
 88.1633 +       return;
 88.1634 +     }
 88.1635 +   slength = (png_size_t)length;
 88.1636 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.1637 +
 88.1638 +   if (png_crc_finish(png_ptr, 0))
 88.1639 +   {
 88.1640 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1641 +      png_ptr->chunkdata = NULL;
 88.1642 +      return;
 88.1643 +   }
 88.1644 +
 88.1645 +   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
 88.1646 +
 88.1647 +   png_debug(3, "Finding end of pCAL purpose string\n");
 88.1648 +   for (buf = png_ptr->chunkdata; *buf; buf++)
 88.1649 +      /* empty loop */ ;
 88.1650 +
 88.1651 +   endptr = png_ptr->chunkdata + slength;
 88.1652 +
 88.1653 +   /* We need to have at least 12 bytes after the purpose string
 88.1654 +      in order to get the parameter information. */
 88.1655 +   if (endptr <= buf + 12)
 88.1656 +   {
 88.1657 +      png_warning(png_ptr, "Invalid pCAL data");
 88.1658 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1659 +      png_ptr->chunkdata = NULL;
 88.1660 +      return;
 88.1661 +   }
 88.1662 +
 88.1663 +   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
 88.1664 +   X0 = png_get_int_32((png_bytep)buf+1);
 88.1665 +   X1 = png_get_int_32((png_bytep)buf+5);
 88.1666 +   type = buf[9];
 88.1667 +   nparams = buf[10];
 88.1668 +   units = buf + 11;
 88.1669 +
 88.1670 +   png_debug(3, "Checking pCAL equation type and number of parameters\n");
 88.1671 +   /* Check that we have the right number of parameters for known
 88.1672 +      equation types. */
 88.1673 +   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
 88.1674 +       (type == PNG_EQUATION_BASE_E && nparams != 3) ||
 88.1675 +       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
 88.1676 +       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
 88.1677 +   {
 88.1678 +      png_warning(png_ptr, "Invalid pCAL parameters for equation type");
 88.1679 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1680 +      png_ptr->chunkdata = NULL;
 88.1681 +      return;
 88.1682 +   }
 88.1683 +   else if (type >= PNG_EQUATION_LAST)
 88.1684 +   {
 88.1685 +      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
 88.1686 +   }
 88.1687 +
 88.1688 +   for (buf = units; *buf; buf++)
 88.1689 +      /* Empty loop to move past the units string. */ ;
 88.1690 +
 88.1691 +   png_debug(3, "Allocating pCAL parameters array\n");
 88.1692 +   params = (png_charpp)png_malloc_warn(png_ptr,
 88.1693 +      (png_uint_32)(nparams * png_sizeof(png_charp))) ;
 88.1694 +   if (params == NULL)
 88.1695 +     {
 88.1696 +       png_free(png_ptr, png_ptr->chunkdata);
 88.1697 +       png_ptr->chunkdata = NULL;
 88.1698 +       png_warning(png_ptr, "No memory for pCAL params.");
 88.1699 +       return;
 88.1700 +     }
 88.1701 +
 88.1702 +   /* Get pointers to the start of each parameter string. */
 88.1703 +   for (i = 0; i < (int)nparams; i++)
 88.1704 +   {
 88.1705 +      buf++; /* Skip the null string terminator from previous parameter. */
 88.1706 +
 88.1707 +      png_debug1(3, "Reading pCAL parameter %d\n", i);
 88.1708 +      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
 88.1709 +         /* Empty loop to move past each parameter string */ ;
 88.1710 +
 88.1711 +      /* Make sure we haven't run out of data yet */
 88.1712 +      if (buf > endptr)
 88.1713 +      {
 88.1714 +         png_warning(png_ptr, "Invalid pCAL data");
 88.1715 +         png_free(png_ptr, png_ptr->chunkdata);
 88.1716 +         png_ptr->chunkdata = NULL;
 88.1717 +         png_free(png_ptr, params);
 88.1718 +         return;
 88.1719 +      }
 88.1720 +   }
 88.1721 +
 88.1722 +   png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
 88.1723 +      units, params);
 88.1724 +
 88.1725 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1726 +   png_ptr->chunkdata = NULL;
 88.1727 +   png_free(png_ptr, params);
 88.1728 +}
 88.1729 +#endif
 88.1730 +
 88.1731 +#if defined(PNG_READ_sCAL_SUPPORTED)
 88.1732 +/* read the sCAL chunk */
 88.1733 +void /* PRIVATE */
 88.1734 +png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1735 +{
 88.1736 +   png_charp ep;
 88.1737 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 88.1738 +   double width, height;
 88.1739 +   png_charp vp;
 88.1740 +#else
 88.1741 +#ifdef PNG_FIXED_POINT_SUPPORTED
 88.1742 +   png_charp swidth, sheight;
 88.1743 +#endif
 88.1744 +#endif
 88.1745 +   png_size_t slength;
 88.1746 +
 88.1747 +   png_debug(1, "in png_handle_sCAL\n");
 88.1748 +
 88.1749 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1750 +      png_error(png_ptr, "Missing IHDR before sCAL");
 88.1751 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1752 +   {
 88.1753 +      png_warning(png_ptr, "Invalid sCAL after IDAT");
 88.1754 +      png_crc_finish(png_ptr, length);
 88.1755 +      return;
 88.1756 +   }
 88.1757 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
 88.1758 +   {
 88.1759 +      png_warning(png_ptr, "Duplicate sCAL chunk");
 88.1760 +      png_crc_finish(png_ptr, length);
 88.1761 +      return;
 88.1762 +   }
 88.1763 +
 88.1764 +   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
 88.1765 +      length + 1);
 88.1766 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 88.1767 +   if (png_ptr->chunkdata == NULL)
 88.1768 +   {
 88.1769 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk");
 88.1770 +      return;
 88.1771 +   }
 88.1772 +   slength = (png_size_t)length;
 88.1773 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.1774 +
 88.1775 +   if (png_crc_finish(png_ptr, 0))
 88.1776 +   {
 88.1777 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1778 +      png_ptr->chunkdata = NULL;
 88.1779 +      return;
 88.1780 +   }
 88.1781 +
 88.1782 +   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
 88.1783 +
 88.1784 +   ep = png_ptr->chunkdata + 1;        /* skip unit byte */
 88.1785 +
 88.1786 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 88.1787 +   width = png_strtod(png_ptr, ep, &vp);
 88.1788 +   if (*vp)
 88.1789 +   {
 88.1790 +      png_warning(png_ptr, "malformed width string in sCAL chunk");
 88.1791 +      return;
 88.1792 +   }
 88.1793 +#else
 88.1794 +#ifdef PNG_FIXED_POINT_SUPPORTED
 88.1795 +   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
 88.1796 +   if (swidth == NULL)
 88.1797 +   {
 88.1798 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
 88.1799 +      return;
 88.1800 +   }
 88.1801 +   png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
 88.1802 +#endif
 88.1803 +#endif
 88.1804 +
 88.1805 +   for (ep = png_ptr->chunkdata; *ep; ep++)
 88.1806 +      /* empty loop */ ;
 88.1807 +   ep++;
 88.1808 +
 88.1809 +   if (png_ptr->chunkdata + slength < ep)
 88.1810 +   {
 88.1811 +      png_warning(png_ptr, "Truncated sCAL chunk");
 88.1812 +#if defined(PNG_FIXED_POINT_SUPPORTED) && \
 88.1813 +    !defined(PNG_FLOATING_POINT_SUPPORTED)
 88.1814 +      png_free(png_ptr, swidth);
 88.1815 +#endif
 88.1816 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1817 +      png_ptr->chunkdata = NULL;
 88.1818 +      return;
 88.1819 +   }
 88.1820 +
 88.1821 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 88.1822 +   height = png_strtod(png_ptr, ep, &vp);
 88.1823 +   if (*vp)
 88.1824 +   {
 88.1825 +      png_warning(png_ptr, "malformed height string in sCAL chunk");
 88.1826 +      return;
 88.1827 +   }
 88.1828 +#else
 88.1829 +#ifdef PNG_FIXED_POINT_SUPPORTED
 88.1830 +   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
 88.1831 +   if (sheight == NULL)
 88.1832 +   {
 88.1833 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
 88.1834 +      return;
 88.1835 +   }
 88.1836 +   png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
 88.1837 +#endif
 88.1838 +#endif
 88.1839 +
 88.1840 +   if (png_ptr->chunkdata + slength < ep
 88.1841 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 88.1842 +      || width <= 0. || height <= 0.
 88.1843 +#endif
 88.1844 +      )
 88.1845 +   {
 88.1846 +      png_warning(png_ptr, "Invalid sCAL data");
 88.1847 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1848 +      png_ptr->chunkdata = NULL;
 88.1849 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
 88.1850 +      png_free(png_ptr, swidth);
 88.1851 +      png_free(png_ptr, sheight);
 88.1852 +#endif
 88.1853 +      return;
 88.1854 +   }
 88.1855 +
 88.1856 +
 88.1857 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 88.1858 +   png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
 88.1859 +#else
 88.1860 +#ifdef PNG_FIXED_POINT_SUPPORTED
 88.1861 +   png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
 88.1862 +#endif
 88.1863 +#endif
 88.1864 +
 88.1865 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1866 +   png_ptr->chunkdata = NULL;
 88.1867 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
 88.1868 +   png_free(png_ptr, swidth);
 88.1869 +   png_free(png_ptr, sheight);
 88.1870 +#endif
 88.1871 +}
 88.1872 +#endif
 88.1873 +
 88.1874 +#if defined(PNG_READ_tIME_SUPPORTED)
 88.1875 +void /* PRIVATE */
 88.1876 +png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1877 +{
 88.1878 +   png_byte buf[7];
 88.1879 +   png_time mod_time;
 88.1880 +
 88.1881 +   png_debug(1, "in png_handle_tIME\n");
 88.1882 +
 88.1883 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1884 +      png_error(png_ptr, "Out of place tIME chunk");
 88.1885 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
 88.1886 +   {
 88.1887 +      png_warning(png_ptr, "Duplicate tIME chunk");
 88.1888 +      png_crc_finish(png_ptr, length);
 88.1889 +      return;
 88.1890 +   }
 88.1891 +
 88.1892 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1893 +      png_ptr->mode |= PNG_AFTER_IDAT;
 88.1894 +
 88.1895 +   if (length != 7)
 88.1896 +   {
 88.1897 +      png_warning(png_ptr, "Incorrect tIME chunk length");
 88.1898 +      png_crc_finish(png_ptr, length);
 88.1899 +      return;
 88.1900 +   }
 88.1901 +
 88.1902 +   png_crc_read(png_ptr, buf, 7);
 88.1903 +   if (png_crc_finish(png_ptr, 0))
 88.1904 +      return;
 88.1905 +
 88.1906 +   mod_time.second = buf[6];
 88.1907 +   mod_time.minute = buf[5];
 88.1908 +   mod_time.hour = buf[4];
 88.1909 +   mod_time.day = buf[3];
 88.1910 +   mod_time.month = buf[2];
 88.1911 +   mod_time.year = png_get_uint_16(buf);
 88.1912 +
 88.1913 +   png_set_tIME(png_ptr, info_ptr, &mod_time);
 88.1914 +}
 88.1915 +#endif
 88.1916 +
 88.1917 +#if defined(PNG_READ_tEXt_SUPPORTED)
 88.1918 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 88.1919 +void /* PRIVATE */
 88.1920 +png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.1921 +{
 88.1922 +   png_textp text_ptr;
 88.1923 +   png_charp key;
 88.1924 +   png_charp text;
 88.1925 +   png_uint_32 skip = 0;
 88.1926 +   png_size_t slength;
 88.1927 +   int ret;
 88.1928 +
 88.1929 +   png_debug(1, "in png_handle_tEXt\n");
 88.1930 +
 88.1931 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.1932 +      png_error(png_ptr, "Missing IHDR before tEXt");
 88.1933 +
 88.1934 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 88.1935 +      png_ptr->mode |= PNG_AFTER_IDAT;
 88.1936 +
 88.1937 +#ifdef PNG_MAX_MALLOC_64K
 88.1938 +   if (length > (png_uint_32)65535L)
 88.1939 +   {
 88.1940 +      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
 88.1941 +      skip = length - (png_uint_32)65535L;
 88.1942 +      length = (png_uint_32)65535L;
 88.1943 +   }
 88.1944 +#endif
 88.1945 +
 88.1946 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1947 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 88.1948 +   if (png_ptr->chunkdata == NULL)
 88.1949 +   {
 88.1950 +     png_warning(png_ptr, "No memory to process text chunk.");
 88.1951 +     return;
 88.1952 +   }
 88.1953 +   slength = (png_size_t)length;
 88.1954 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.1955 +
 88.1956 +   if (png_crc_finish(png_ptr, skip))
 88.1957 +   {
 88.1958 +      png_free(png_ptr, png_ptr->chunkdata);
 88.1959 +      png_ptr->chunkdata = NULL;
 88.1960 +      return;
 88.1961 +   }
 88.1962 +
 88.1963 +   key = png_ptr->chunkdata;
 88.1964 +   key[slength] = 0x00;
 88.1965 +
 88.1966 +   for (text = key; *text; text++)
 88.1967 +      /* empty loop to find end of key */ ;
 88.1968 +
 88.1969 +   if (text != key + slength)
 88.1970 +      text++;
 88.1971 +
 88.1972 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 88.1973 +      (png_uint_32)png_sizeof(png_text));
 88.1974 +   if (text_ptr == NULL)
 88.1975 +   {
 88.1976 +     png_warning(png_ptr, "Not enough memory to process text chunk.");
 88.1977 +     png_free(png_ptr, png_ptr->chunkdata);
 88.1978 +     png_ptr->chunkdata = NULL;
 88.1979 +     return;
 88.1980 +   }
 88.1981 +   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
 88.1982 +   text_ptr->key = key;
 88.1983 +#ifdef PNG_iTXt_SUPPORTED
 88.1984 +   text_ptr->lang = NULL;
 88.1985 +   text_ptr->lang_key = NULL;
 88.1986 +   text_ptr->itxt_length = 0;
 88.1987 +#endif
 88.1988 +   text_ptr->text = text;
 88.1989 +   text_ptr->text_length = png_strlen(text);
 88.1990 +
 88.1991 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 88.1992 +
 88.1993 +   png_free(png_ptr, png_ptr->chunkdata);
 88.1994 +   png_ptr->chunkdata = NULL;
 88.1995 +   png_free(png_ptr, text_ptr);
 88.1996 +   if (ret)
 88.1997 +     png_warning(png_ptr, "Insufficient memory to process text chunk.");
 88.1998 +}
 88.1999 +#endif
 88.2000 +
 88.2001 +#if defined(PNG_READ_zTXt_SUPPORTED)
 88.2002 +/* note: this does not correctly handle chunks that are > 64K under DOS */
 88.2003 +void /* PRIVATE */
 88.2004 +png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.2005 +{
 88.2006 +   png_textp text_ptr;
 88.2007 +   png_charp text;
 88.2008 +   int comp_type;
 88.2009 +   int ret;
 88.2010 +   png_size_t slength, prefix_len, data_len;
 88.2011 +
 88.2012 +   png_debug(1, "in png_handle_zTXt\n");
 88.2013 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.2014 +      png_error(png_ptr, "Missing IHDR before zTXt");
 88.2015 +
 88.2016 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 88.2017 +      png_ptr->mode |= PNG_AFTER_IDAT;
 88.2018 +
 88.2019 +#ifdef PNG_MAX_MALLOC_64K
 88.2020 +   /* We will no doubt have problems with chunks even half this size, but
 88.2021 +      there is no hard and fast rule to tell us where to stop. */
 88.2022 +   if (length > (png_uint_32)65535L)
 88.2023 +   {
 88.2024 +     png_warning(png_ptr, "zTXt chunk too large to fit in memory");
 88.2025 +     png_crc_finish(png_ptr, length);
 88.2026 +     return;
 88.2027 +   }
 88.2028 +#endif
 88.2029 +
 88.2030 +   png_free(png_ptr, png_ptr->chunkdata);
 88.2031 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 88.2032 +   if (png_ptr->chunkdata == NULL)
 88.2033 +   {
 88.2034 +     png_warning(png_ptr, "Out of memory processing zTXt chunk.");
 88.2035 +     return;
 88.2036 +   }
 88.2037 +   slength = (png_size_t)length;
 88.2038 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.2039 +   if (png_crc_finish(png_ptr, 0))
 88.2040 +   {
 88.2041 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2042 +      png_ptr->chunkdata = NULL;
 88.2043 +      return;
 88.2044 +   }
 88.2045 +
 88.2046 +   png_ptr->chunkdata[slength] = 0x00;
 88.2047 +
 88.2048 +   for (text = png_ptr->chunkdata; *text; text++)
 88.2049 +      /* empty loop */ ;
 88.2050 +
 88.2051 +   /* zTXt must have some text after the chunkdataword */
 88.2052 +   if (text >= png_ptr->chunkdata + slength - 2)
 88.2053 +   {
 88.2054 +      png_warning(png_ptr, "Truncated zTXt chunk");
 88.2055 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2056 +      png_ptr->chunkdata = NULL;
 88.2057 +      return;
 88.2058 +   }
 88.2059 +   else
 88.2060 +   {
 88.2061 +       comp_type = *(++text);
 88.2062 +       if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
 88.2063 +       {
 88.2064 +          png_warning(png_ptr, "Unknown compression type in zTXt chunk");
 88.2065 +          comp_type = PNG_TEXT_COMPRESSION_zTXt;
 88.2066 +       }
 88.2067 +       text++;        /* skip the compression_method byte */
 88.2068 +   }
 88.2069 +   prefix_len = text - png_ptr->chunkdata;
 88.2070 +
 88.2071 +   png_decompress_chunk(png_ptr, comp_type,
 88.2072 +     (png_size_t)length, prefix_len, &data_len);
 88.2073 +
 88.2074 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 88.2075 +      (png_uint_32)png_sizeof(png_text));
 88.2076 +   if (text_ptr == NULL)
 88.2077 +   {
 88.2078 +     png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
 88.2079 +     png_free(png_ptr, png_ptr->chunkdata);
 88.2080 +     png_ptr->chunkdata = NULL;
 88.2081 +     return;
 88.2082 +   }
 88.2083 +   text_ptr->compression = comp_type;
 88.2084 +   text_ptr->key = png_ptr->chunkdata;
 88.2085 +#ifdef PNG_iTXt_SUPPORTED
 88.2086 +   text_ptr->lang = NULL;
 88.2087 +   text_ptr->lang_key = NULL;
 88.2088 +   text_ptr->itxt_length = 0;
 88.2089 +#endif
 88.2090 +   text_ptr->text = png_ptr->chunkdata + prefix_len;
 88.2091 +   text_ptr->text_length = data_len;
 88.2092 +
 88.2093 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 88.2094 +
 88.2095 +   png_free(png_ptr, text_ptr);
 88.2096 +   png_free(png_ptr, png_ptr->chunkdata);
 88.2097 +   png_ptr->chunkdata = NULL;
 88.2098 +   if (ret)
 88.2099 +     png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
 88.2100 +}
 88.2101 +#endif
 88.2102 +
 88.2103 +#if defined(PNG_READ_iTXt_SUPPORTED)
 88.2104 +/* note: this does not correctly handle chunks that are > 64K under DOS */
 88.2105 +void /* PRIVATE */
 88.2106 +png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.2107 +{
 88.2108 +   png_textp text_ptr;
 88.2109 +   png_charp key, lang, text, lang_key;
 88.2110 +   int comp_flag;
 88.2111 +   int comp_type = 0;
 88.2112 +   int ret;
 88.2113 +   png_size_t slength, prefix_len, data_len;
 88.2114 +
 88.2115 +   png_debug(1, "in png_handle_iTXt\n");
 88.2116 +
 88.2117 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 88.2118 +      png_error(png_ptr, "Missing IHDR before iTXt");
 88.2119 +
 88.2120 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 88.2121 +      png_ptr->mode |= PNG_AFTER_IDAT;
 88.2122 +
 88.2123 +#ifdef PNG_MAX_MALLOC_64K
 88.2124 +   /* We will no doubt have problems with chunks even half this size, but
 88.2125 +      there is no hard and fast rule to tell us where to stop. */
 88.2126 +   if (length > (png_uint_32)65535L)
 88.2127 +   {
 88.2128 +     png_warning(png_ptr, "iTXt chunk too large to fit in memory");
 88.2129 +     png_crc_finish(png_ptr, length);
 88.2130 +     return;
 88.2131 +   }
 88.2132 +#endif
 88.2133 +
 88.2134 +   png_free(png_ptr, png_ptr->chunkdata);
 88.2135 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 88.2136 +   if (png_ptr->chunkdata == NULL)
 88.2137 +   {
 88.2138 +     png_warning(png_ptr, "No memory to process iTXt chunk.");
 88.2139 +     return;
 88.2140 +   }
 88.2141 +   slength = (png_size_t)length;
 88.2142 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 88.2143 +   if (png_crc_finish(png_ptr, 0))
 88.2144 +   {
 88.2145 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2146 +      png_ptr->chunkdata = NULL;
 88.2147 +      return;
 88.2148 +   }
 88.2149 +
 88.2150 +   png_ptr->chunkdata[slength] = 0x00;
 88.2151 +
 88.2152 +   for (lang = png_ptr->chunkdata; *lang; lang++)
 88.2153 +      /* empty loop */ ;
 88.2154 +   lang++;        /* skip NUL separator */
 88.2155 +
 88.2156 +   /* iTXt must have a language tag (possibly empty), two compression bytes,
 88.2157 +      translated keyword (possibly empty), and possibly some text after the
 88.2158 +      keyword */
 88.2159 +
 88.2160 +   if (lang >= png_ptr->chunkdata + slength - 3)
 88.2161 +   {
 88.2162 +      png_warning(png_ptr, "Truncated iTXt chunk");
 88.2163 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2164 +      png_ptr->chunkdata = NULL;
 88.2165 +      return;
 88.2166 +   }
 88.2167 +   else
 88.2168 +   {
 88.2169 +       comp_flag = *lang++;
 88.2170 +       comp_type = *lang++;
 88.2171 +   }
 88.2172 +
 88.2173 +   for (lang_key = lang; *lang_key; lang_key++)
 88.2174 +      /* empty loop */ ;
 88.2175 +   lang_key++;        /* skip NUL separator */
 88.2176 +
 88.2177 +   if (lang_key >= png_ptr->chunkdata + slength)
 88.2178 +   {
 88.2179 +      png_warning(png_ptr, "Truncated iTXt chunk");
 88.2180 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2181 +      png_ptr->chunkdata = NULL;
 88.2182 +      return;
 88.2183 +   }
 88.2184 +
 88.2185 +   for (text = lang_key; *text; text++)
 88.2186 +      /* empty loop */ ;
 88.2187 +   text++;        /* skip NUL separator */
 88.2188 +   if (text >= png_ptr->chunkdata + slength)
 88.2189 +   {
 88.2190 +      png_warning(png_ptr, "Malformed iTXt chunk");
 88.2191 +      png_free(png_ptr, png_ptr->chunkdata);
 88.2192 +      png_ptr->chunkdata = NULL;
 88.2193 +      return;
 88.2194 +   }
 88.2195 +
 88.2196 +   prefix_len = text - png_ptr->chunkdata;
 88.2197 +
 88.2198 +   key=png_ptr->chunkdata;
 88.2199 +   if (comp_flag)
 88.2200 +       png_decompress_chunk(png_ptr, comp_type,
 88.2201 +         (size_t)length, prefix_len, &data_len);
 88.2202 +   else
 88.2203 +       data_len = png_strlen(png_ptr->chunkdata + prefix_len);
 88.2204 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 88.2205 +      (png_uint_32)png_sizeof(png_text));
 88.2206 +   if (text_ptr == NULL)
 88.2207 +   {
 88.2208 +     png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
 88.2209 +     png_free(png_ptr, png_ptr->chunkdata);
 88.2210 +     png_ptr->chunkdata = NULL;
 88.2211 +     return;
 88.2212 +   }
 88.2213 +   text_ptr->compression = (int)comp_flag + 1;
 88.2214 +   text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
 88.2215 +   text_ptr->lang = png_ptr->chunkdata + (lang - key);
 88.2216 +   text_ptr->itxt_length = data_len;
 88.2217 +   text_ptr->text_length = 0;
 88.2218 +   text_ptr->key = png_ptr->chunkdata;
 88.2219 +   text_ptr->text = png_ptr->chunkdata + prefix_len;
 88.2220 +
 88.2221 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 88.2222 +
 88.2223 +   png_free(png_ptr, text_ptr);
 88.2224 +   png_free(png_ptr, png_ptr->chunkdata);
 88.2225 +   png_ptr->chunkdata = NULL;
 88.2226 +   if (ret)
 88.2227 +     png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
 88.2228 +}
 88.2229 +#endif
 88.2230 +
 88.2231 +/* This function is called when we haven't found a handler for a
 88.2232 +   chunk.  If there isn't a problem with the chunk itself (ie bad
 88.2233 +   chunk name, CRC, or a critical chunk), the chunk is silently ignored
 88.2234 +   -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
 88.2235 +   case it will be saved away to be written out later. */
 88.2236 +void /* PRIVATE */
 88.2237 +png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 88.2238 +{
 88.2239 +   png_uint_32 skip = 0;
 88.2240 +
 88.2241 +   png_debug(1, "in png_handle_unknown\n");
 88.2242 +
 88.2243 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 88.2244 +   {
 88.2245 +#ifdef PNG_USE_LOCAL_ARRAYS
 88.2246 +      PNG_CONST PNG_IDAT;
 88.2247 +#endif
 88.2248 +      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
 88.2249 +         png_ptr->mode |= PNG_AFTER_IDAT;
 88.2250 +   }
 88.2251 +
 88.2252 +   if (!(png_ptr->chunk_name[0] & 0x20))
 88.2253 +   {
 88.2254 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 88.2255 +      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 88.2256 +           PNG_HANDLE_CHUNK_ALWAYS
 88.2257 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 88.2258 +           && png_ptr->read_user_chunk_fn == NULL
 88.2259 +#endif
 88.2260 +        )
 88.2261 +#endif
 88.2262 +          png_chunk_error(png_ptr, "unknown critical chunk");
 88.2263 +   }
 88.2264 +
 88.2265 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 88.2266 +   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
 88.2267 +       (png_ptr->read_user_chunk_fn != NULL))
 88.2268 +   {
 88.2269 +#ifdef PNG_MAX_MALLOC_64K
 88.2270 +       if (length > (png_uint_32)65535L)
 88.2271 +       {
 88.2272 +           png_warning(png_ptr, "unknown chunk too large to fit in memory");
 88.2273 +           skip = length - (png_uint_32)65535L;
 88.2274 +           length = (png_uint_32)65535L;
 88.2275 +       }
 88.2276 +#endif
 88.2277 +       png_memcpy((png_charp)png_ptr->unknown_chunk.name,
 88.2278 +                  (png_charp)png_ptr->chunk_name, 
 88.2279 +                  png_sizeof(png_ptr->unknown_chunk.name));
 88.2280 +       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
 88.2281 +       png_ptr->unknown_chunk.size = (png_size_t)length;
 88.2282 +       if (length == 0)
 88.2283 +         png_ptr->unknown_chunk.data = NULL;
 88.2284 +       else
 88.2285 +       {
 88.2286 +         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
 88.2287 +         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
 88.2288 +       }
 88.2289 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 88.2290 +       if (png_ptr->read_user_chunk_fn != NULL)
 88.2291 +       {
 88.2292 +          /* callback to user unknown chunk handler */
 88.2293 +          int ret;
 88.2294 +          ret = (*(png_ptr->read_user_chunk_fn))
 88.2295 +            (png_ptr, &png_ptr->unknown_chunk);
 88.2296 +          if (ret < 0)
 88.2297 +             png_chunk_error(png_ptr, "error in user chunk");
 88.2298 +          if (ret == 0)
 88.2299 +          {
 88.2300 +             if (!(png_ptr->chunk_name[0] & 0x20))
 88.2301 +                if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 88.2302 +                     PNG_HANDLE_CHUNK_ALWAYS)
 88.2303 +                   png_chunk_error(png_ptr, "unknown critical chunk");
 88.2304 +             png_set_unknown_chunks(png_ptr, info_ptr,
 88.2305 +               &png_ptr->unknown_chunk, 1);
 88.2306 +          }
 88.2307 +       }
 88.2308 +       else
 88.2309 +#endif
 88.2310 +       png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
 88.2311 +       png_free(png_ptr, png_ptr->unknown_chunk.data);
 88.2312 +       png_ptr->unknown_chunk.data = NULL;
 88.2313 +   }
 88.2314 +   else
 88.2315 +#endif
 88.2316 +      skip = length;
 88.2317 +
 88.2318 +   png_crc_finish(png_ptr, skip);
 88.2319 +
 88.2320 +#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 88.2321 +   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
 88.2322 +#endif
 88.2323 +}
 88.2324 +
 88.2325 +/* This function is called to verify that a chunk name is valid.
 88.2326 +   This function can't have the "critical chunk check" incorporated
 88.2327 +   into it, since in the future we will need to be able to call user
 88.2328 +   functions to handle unknown critical chunks after we check that
 88.2329 +   the chunk name itself is valid. */
 88.2330 +
 88.2331 +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
 88.2332 +
 88.2333 +void /* PRIVATE */
 88.2334 +png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
 88.2335 +{
 88.2336 +   png_debug(1, "in png_check_chunk_name\n");
 88.2337 +   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
 88.2338 +       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
 88.2339 +   {
 88.2340 +      png_chunk_error(png_ptr, "invalid chunk type");
 88.2341 +   }
 88.2342 +}
 88.2343 +
 88.2344 +/* Combines the row recently read in with the existing pixels in the
 88.2345 +   row.  This routine takes care of alpha and transparency if requested.
 88.2346 +   This routine also handles the two methods of progressive display
 88.2347 +   of interlaced images, depending on the mask value.
 88.2348 +   The mask value describes which pixels are to be combined with
 88.2349 +   the row.  The pattern always repeats every 8 pixels, so just 8
 88.2350 +   bits are needed.  A one indicates the pixel is to be combined,
 88.2351 +   a zero indicates the pixel is to be skipped.  This is in addition
 88.2352 +   to any alpha or transparency value associated with the pixel.  If
 88.2353 +   you want all pixels to be combined, pass 0xff (255) in mask.  */
 88.2354 +
 88.2355 +void /* PRIVATE */
 88.2356 +png_combine_row(png_structp png_ptr, png_bytep row, int mask)
 88.2357 +{
 88.2358 +   png_debug(1, "in png_combine_row\n");
 88.2359 +   if (mask == 0xff)
 88.2360 +   {
 88.2361 +      png_memcpy(row, png_ptr->row_buf + 1,
 88.2362 +         PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
 88.2363 +   }
 88.2364 +   else
 88.2365 +   {
 88.2366 +      switch (png_ptr->row_info.pixel_depth)
 88.2367 +      {
 88.2368 +         case 1:
 88.2369 +         {
 88.2370 +            png_bytep sp = png_ptr->row_buf + 1;
 88.2371 +            png_bytep dp = row;
 88.2372 +            int s_inc, s_start, s_end;
 88.2373 +            int m = 0x80;
 88.2374 +            int shift;
 88.2375 +            png_uint_32 i;
 88.2376 +            png_uint_32 row_width = png_ptr->width;
 88.2377 +
 88.2378 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2379 +            if (png_ptr->transformations & PNG_PACKSWAP)
 88.2380 +            {
 88.2381 +                s_start = 0;
 88.2382 +                s_end = 7;
 88.2383 +                s_inc = 1;
 88.2384 +            }
 88.2385 +            else
 88.2386 +#endif
 88.2387 +            {
 88.2388 +                s_start = 7;
 88.2389 +                s_end = 0;
 88.2390 +                s_inc = -1;
 88.2391 +            }
 88.2392 +
 88.2393 +            shift = s_start;
 88.2394 +
 88.2395 +            for (i = 0; i < row_width; i++)
 88.2396 +            {
 88.2397 +               if (m & mask)
 88.2398 +               {
 88.2399 +                  int value;
 88.2400 +
 88.2401 +                  value = (*sp >> shift) & 0x01;
 88.2402 +                  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
 88.2403 +                  *dp |= (png_byte)(value << shift);
 88.2404 +               }
 88.2405 +
 88.2406 +               if (shift == s_end)
 88.2407 +               {
 88.2408 +                  shift = s_start;
 88.2409 +                  sp++;
 88.2410 +                  dp++;
 88.2411 +               }
 88.2412 +               else
 88.2413 +                  shift += s_inc;
 88.2414 +
 88.2415 +               if (m == 1)
 88.2416 +                  m = 0x80;
 88.2417 +               else
 88.2418 +                  m >>= 1;
 88.2419 +            }
 88.2420 +            break;
 88.2421 +         }
 88.2422 +         case 2:
 88.2423 +         {
 88.2424 +            png_bytep sp = png_ptr->row_buf + 1;
 88.2425 +            png_bytep dp = row;
 88.2426 +            int s_start, s_end, s_inc;
 88.2427 +            int m = 0x80;
 88.2428 +            int shift;
 88.2429 +            png_uint_32 i;
 88.2430 +            png_uint_32 row_width = png_ptr->width;
 88.2431 +            int value;
 88.2432 +
 88.2433 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2434 +            if (png_ptr->transformations & PNG_PACKSWAP)
 88.2435 +            {
 88.2436 +               s_start = 0;
 88.2437 +               s_end = 6;
 88.2438 +               s_inc = 2;
 88.2439 +            }
 88.2440 +            else
 88.2441 +#endif
 88.2442 +            {
 88.2443 +               s_start = 6;
 88.2444 +               s_end = 0;
 88.2445 +               s_inc = -2;
 88.2446 +            }
 88.2447 +
 88.2448 +            shift = s_start;
 88.2449 +
 88.2450 +            for (i = 0; i < row_width; i++)
 88.2451 +            {
 88.2452 +               if (m & mask)
 88.2453 +               {
 88.2454 +                  value = (*sp >> shift) & 0x03;
 88.2455 +                  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 88.2456 +                  *dp |= (png_byte)(value << shift);
 88.2457 +               }
 88.2458 +
 88.2459 +               if (shift == s_end)
 88.2460 +               {
 88.2461 +                  shift = s_start;
 88.2462 +                  sp++;
 88.2463 +                  dp++;
 88.2464 +               }
 88.2465 +               else
 88.2466 +                  shift += s_inc;
 88.2467 +               if (m == 1)
 88.2468 +                  m = 0x80;
 88.2469 +               else
 88.2470 +                  m >>= 1;
 88.2471 +            }
 88.2472 +            break;
 88.2473 +         }
 88.2474 +         case 4:
 88.2475 +         {
 88.2476 +            png_bytep sp = png_ptr->row_buf + 1;
 88.2477 +            png_bytep dp = row;
 88.2478 +            int s_start, s_end, s_inc;
 88.2479 +            int m = 0x80;
 88.2480 +            int shift;
 88.2481 +            png_uint_32 i;
 88.2482 +            png_uint_32 row_width = png_ptr->width;
 88.2483 +            int value;
 88.2484 +
 88.2485 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2486 +            if (png_ptr->transformations & PNG_PACKSWAP)
 88.2487 +            {
 88.2488 +               s_start = 0;
 88.2489 +               s_end = 4;
 88.2490 +               s_inc = 4;
 88.2491 +            }
 88.2492 +            else
 88.2493 +#endif
 88.2494 +            {
 88.2495 +               s_start = 4;
 88.2496 +               s_end = 0;
 88.2497 +               s_inc = -4;
 88.2498 +            }
 88.2499 +            shift = s_start;
 88.2500 +
 88.2501 +            for (i = 0; i < row_width; i++)
 88.2502 +            {
 88.2503 +               if (m & mask)
 88.2504 +               {
 88.2505 +                  value = (*sp >> shift) & 0xf;
 88.2506 +                  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 88.2507 +                  *dp |= (png_byte)(value << shift);
 88.2508 +               }
 88.2509 +
 88.2510 +               if (shift == s_end)
 88.2511 +               {
 88.2512 +                  shift = s_start;
 88.2513 +                  sp++;
 88.2514 +                  dp++;
 88.2515 +               }
 88.2516 +               else
 88.2517 +                  shift += s_inc;
 88.2518 +               if (m == 1)
 88.2519 +                  m = 0x80;
 88.2520 +               else
 88.2521 +                  m >>= 1;
 88.2522 +            }
 88.2523 +            break;
 88.2524 +         }
 88.2525 +         default:
 88.2526 +         {
 88.2527 +            png_bytep sp = png_ptr->row_buf + 1;
 88.2528 +            png_bytep dp = row;
 88.2529 +            png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
 88.2530 +            png_uint_32 i;
 88.2531 +            png_uint_32 row_width = png_ptr->width;
 88.2532 +            png_byte m = 0x80;
 88.2533 +
 88.2534 +
 88.2535 +            for (i = 0; i < row_width; i++)
 88.2536 +            {
 88.2537 +               if (m & mask)
 88.2538 +               {
 88.2539 +                  png_memcpy(dp, sp, pixel_bytes);
 88.2540 +               }
 88.2541 +
 88.2542 +               sp += pixel_bytes;
 88.2543 +               dp += pixel_bytes;
 88.2544 +
 88.2545 +               if (m == 1)
 88.2546 +                  m = 0x80;
 88.2547 +               else
 88.2548 +                  m >>= 1;
 88.2549 +            }
 88.2550 +            break;
 88.2551 +         }
 88.2552 +      }
 88.2553 +   }
 88.2554 +}
 88.2555 +
 88.2556 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 88.2557 +/* OLD pre-1.0.9 interface:
 88.2558 +void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
 88.2559 +   png_uint_32 transformations)
 88.2560 + */
 88.2561 +void /* PRIVATE */
 88.2562 +png_do_read_interlace(png_structp png_ptr)
 88.2563 +{
 88.2564 +   png_row_infop row_info = &(png_ptr->row_info);
 88.2565 +   png_bytep row = png_ptr->row_buf + 1;
 88.2566 +   int pass = png_ptr->pass;
 88.2567 +   png_uint_32 transformations = png_ptr->transformations;
 88.2568 +#ifdef PNG_USE_LOCAL_ARRAYS
 88.2569 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 88.2570 +   /* offset to next interlace block */
 88.2571 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 88.2572 +#endif
 88.2573 +
 88.2574 +   png_debug(1, "in png_do_read_interlace\n");
 88.2575 +   if (row != NULL && row_info != NULL)
 88.2576 +   {
 88.2577 +      png_uint_32 final_width;
 88.2578 +
 88.2579 +      final_width = row_info->width * png_pass_inc[pass];
 88.2580 +
 88.2581 +      switch (row_info->pixel_depth)
 88.2582 +      {
 88.2583 +         case 1:
 88.2584 +         {
 88.2585 +            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
 88.2586 +            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
 88.2587 +            int sshift, dshift;
 88.2588 +            int s_start, s_end, s_inc;
 88.2589 +            int jstop = png_pass_inc[pass];
 88.2590 +            png_byte v;
 88.2591 +            png_uint_32 i;
 88.2592 +            int j;
 88.2593 +
 88.2594 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2595 +            if (transformations & PNG_PACKSWAP)
 88.2596 +            {
 88.2597 +                sshift = (int)((row_info->width + 7) & 0x07);
 88.2598 +                dshift = (int)((final_width + 7) & 0x07);
 88.2599 +                s_start = 7;
 88.2600 +                s_end = 0;
 88.2601 +                s_inc = -1;
 88.2602 +            }
 88.2603 +            else
 88.2604 +#endif
 88.2605 +            {
 88.2606 +                sshift = 7 - (int)((row_info->width + 7) & 0x07);
 88.2607 +                dshift = 7 - (int)((final_width + 7) & 0x07);
 88.2608 +                s_start = 0;
 88.2609 +                s_end = 7;
 88.2610 +                s_inc = 1;
 88.2611 +            }
 88.2612 +
 88.2613 +            for (i = 0; i < row_info->width; i++)
 88.2614 +            {
 88.2615 +               v = (png_byte)((*sp >> sshift) & 0x01);
 88.2616 +               for (j = 0; j < jstop; j++)
 88.2617 +               {
 88.2618 +                  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
 88.2619 +                  *dp |= (png_byte)(v << dshift);
 88.2620 +                  if (dshift == s_end)
 88.2621 +                  {
 88.2622 +                     dshift = s_start;
 88.2623 +                     dp--;
 88.2624 +                  }
 88.2625 +                  else
 88.2626 +                     dshift += s_inc;
 88.2627 +               }
 88.2628 +               if (sshift == s_end)
 88.2629 +               {
 88.2630 +                  sshift = s_start;
 88.2631 +                  sp--;
 88.2632 +               }
 88.2633 +               else
 88.2634 +                  sshift += s_inc;
 88.2635 +            }
 88.2636 +            break;
 88.2637 +         }
 88.2638 +         case 2:
 88.2639 +         {
 88.2640 +            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
 88.2641 +            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
 88.2642 +            int sshift, dshift;
 88.2643 +            int s_start, s_end, s_inc;
 88.2644 +            int jstop = png_pass_inc[pass];
 88.2645 +            png_uint_32 i;
 88.2646 +
 88.2647 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2648 +            if (transformations & PNG_PACKSWAP)
 88.2649 +            {
 88.2650 +               sshift = (int)(((row_info->width + 3) & 0x03) << 1);
 88.2651 +               dshift = (int)(((final_width + 3) & 0x03) << 1);
 88.2652 +               s_start = 6;
 88.2653 +               s_end = 0;
 88.2654 +               s_inc = -2;
 88.2655 +            }
 88.2656 +            else
 88.2657 +#endif
 88.2658 +            {
 88.2659 +               sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
 88.2660 +               dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
 88.2661 +               s_start = 0;
 88.2662 +               s_end = 6;
 88.2663 +               s_inc = 2;
 88.2664 +            }
 88.2665 +
 88.2666 +            for (i = 0; i < row_info->width; i++)
 88.2667 +            {
 88.2668 +               png_byte v;
 88.2669 +               int j;
 88.2670 +
 88.2671 +               v = (png_byte)((*sp >> sshift) & 0x03);
 88.2672 +               for (j = 0; j < jstop; j++)
 88.2673 +               {
 88.2674 +                  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
 88.2675 +                  *dp |= (png_byte)(v << dshift);
 88.2676 +                  if (dshift == s_end)
 88.2677 +                  {
 88.2678 +                     dshift = s_start;
 88.2679 +                     dp--;
 88.2680 +                  }
 88.2681 +                  else
 88.2682 +                     dshift += s_inc;
 88.2683 +               }
 88.2684 +               if (sshift == s_end)
 88.2685 +               {
 88.2686 +                  sshift = s_start;
 88.2687 +                  sp--;
 88.2688 +               }
 88.2689 +               else
 88.2690 +                  sshift += s_inc;
 88.2691 +            }
 88.2692 +            break;
 88.2693 +         }
 88.2694 +         case 4:
 88.2695 +         {
 88.2696 +            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
 88.2697 +            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
 88.2698 +            int sshift, dshift;
 88.2699 +            int s_start, s_end, s_inc;
 88.2700 +            png_uint_32 i;
 88.2701 +            int jstop = png_pass_inc[pass];
 88.2702 +
 88.2703 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2704 +            if (transformations & PNG_PACKSWAP)
 88.2705 +            {
 88.2706 +               sshift = (int)(((row_info->width + 1) & 0x01) << 2);
 88.2707 +               dshift = (int)(((final_width + 1) & 0x01) << 2);
 88.2708 +               s_start = 4;
 88.2709 +               s_end = 0;
 88.2710 +               s_inc = -4;
 88.2711 +            }
 88.2712 +            else
 88.2713 +#endif
 88.2714 +            {
 88.2715 +               sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
 88.2716 +               dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
 88.2717 +               s_start = 0;
 88.2718 +               s_end = 4;
 88.2719 +               s_inc = 4;
 88.2720 +            }
 88.2721 +
 88.2722 +            for (i = 0; i < row_info->width; i++)
 88.2723 +            {
 88.2724 +               png_byte v = (png_byte)((*sp >> sshift) & 0xf);
 88.2725 +               int j;
 88.2726 +
 88.2727 +               for (j = 0; j < jstop; j++)
 88.2728 +               {
 88.2729 +                  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
 88.2730 +                  *dp |= (png_byte)(v << dshift);
 88.2731 +                  if (dshift == s_end)
 88.2732 +                  {
 88.2733 +                     dshift = s_start;
 88.2734 +                     dp--;
 88.2735 +                  }
 88.2736 +                  else
 88.2737 +                     dshift += s_inc;
 88.2738 +               }
 88.2739 +               if (sshift == s_end)
 88.2740 +               {
 88.2741 +                  sshift = s_start;
 88.2742 +                  sp--;
 88.2743 +               }
 88.2744 +               else
 88.2745 +                  sshift += s_inc;
 88.2746 +            }
 88.2747 +            break;
 88.2748 +         }
 88.2749 +         default:
 88.2750 +         {
 88.2751 +            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
 88.2752 +            png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
 88.2753 +            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
 88.2754 +
 88.2755 +            int jstop = png_pass_inc[pass];
 88.2756 +            png_uint_32 i;
 88.2757 +
 88.2758 +            for (i = 0; i < row_info->width; i++)
 88.2759 +            {
 88.2760 +               png_byte v[8];
 88.2761 +               int j;
 88.2762 +
 88.2763 +               png_memcpy(v, sp, pixel_bytes);
 88.2764 +               for (j = 0; j < jstop; j++)
 88.2765 +               {
 88.2766 +                  png_memcpy(dp, v, pixel_bytes);
 88.2767 +                  dp -= pixel_bytes;
 88.2768 +               }
 88.2769 +               sp -= pixel_bytes;
 88.2770 +            }
 88.2771 +            break;
 88.2772 +         }
 88.2773 +      }
 88.2774 +      row_info->width = final_width;
 88.2775 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
 88.2776 +   }
 88.2777 +#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
 88.2778 +   transformations = transformations; /* silence compiler warning */
 88.2779 +#endif
 88.2780 +}
 88.2781 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 88.2782 +
 88.2783 +void /* PRIVATE */
 88.2784 +png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
 88.2785 +   png_bytep prev_row, int filter)
 88.2786 +{
 88.2787 +   png_debug(1, "in png_read_filter_row\n");
 88.2788 +   png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
 88.2789 +   switch (filter)
 88.2790 +   {
 88.2791 +      case PNG_FILTER_VALUE_NONE:
 88.2792 +         break;
 88.2793 +      case PNG_FILTER_VALUE_SUB:
 88.2794 +      {
 88.2795 +         png_uint_32 i;
 88.2796 +         png_uint_32 istop = row_info->rowbytes;
 88.2797 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 88.2798 +         png_bytep rp = row + bpp;
 88.2799 +         png_bytep lp = row;
 88.2800 +
 88.2801 +         for (i = bpp; i < istop; i++)
 88.2802 +         {
 88.2803 +            *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
 88.2804 +            rp++;
 88.2805 +         }
 88.2806 +         break;
 88.2807 +      }
 88.2808 +      case PNG_FILTER_VALUE_UP:
 88.2809 +      {
 88.2810 +         png_uint_32 i;
 88.2811 +         png_uint_32 istop = row_info->rowbytes;
 88.2812 +         png_bytep rp = row;
 88.2813 +         png_bytep pp = prev_row;
 88.2814 +
 88.2815 +         for (i = 0; i < istop; i++)
 88.2816 +         {
 88.2817 +            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
 88.2818 +            rp++;
 88.2819 +         }
 88.2820 +         break;
 88.2821 +      }
 88.2822 +      case PNG_FILTER_VALUE_AVG:
 88.2823 +      {
 88.2824 +         png_uint_32 i;
 88.2825 +         png_bytep rp = row;
 88.2826 +         png_bytep pp = prev_row;
 88.2827 +         png_bytep lp = row;
 88.2828 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 88.2829 +         png_uint_32 istop = row_info->rowbytes - bpp;
 88.2830 +
 88.2831 +         for (i = 0; i < bpp; i++)
 88.2832 +         {
 88.2833 +            *rp = (png_byte)(((int)(*rp) +
 88.2834 +               ((int)(*pp++) / 2 )) & 0xff);
 88.2835 +            rp++;
 88.2836 +         }
 88.2837 +
 88.2838 +         for (i = 0; i < istop; i++)
 88.2839 +         {
 88.2840 +            *rp = (png_byte)(((int)(*rp) +
 88.2841 +               (int)(*pp++ + *lp++) / 2 ) & 0xff);
 88.2842 +            rp++;
 88.2843 +         }
 88.2844 +         break;
 88.2845 +      }
 88.2846 +      case PNG_FILTER_VALUE_PAETH:
 88.2847 +      {
 88.2848 +         png_uint_32 i;
 88.2849 +         png_bytep rp = row;
 88.2850 +         png_bytep pp = prev_row;
 88.2851 +         png_bytep lp = row;
 88.2852 +         png_bytep cp = prev_row;
 88.2853 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 88.2854 +         png_uint_32 istop=row_info->rowbytes - bpp;
 88.2855 +
 88.2856 +         for (i = 0; i < bpp; i++)
 88.2857 +         {
 88.2858 +            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
 88.2859 +            rp++;
 88.2860 +         }
 88.2861 +
 88.2862 +         for (i = 0; i < istop; i++)   /* use leftover rp,pp */
 88.2863 +         {
 88.2864 +            int a, b, c, pa, pb, pc, p;
 88.2865 +
 88.2866 +            a = *lp++;
 88.2867 +            b = *pp++;
 88.2868 +            c = *cp++;
 88.2869 +
 88.2870 +            p = b - c;
 88.2871 +            pc = a - c;
 88.2872 +
 88.2873 +#ifdef PNG_USE_ABS
 88.2874 +            pa = abs(p);
 88.2875 +            pb = abs(pc);
 88.2876 +            pc = abs(p + pc);
 88.2877 +#else
 88.2878 +            pa = p < 0 ? -p : p;
 88.2879 +            pb = pc < 0 ? -pc : pc;
 88.2880 +            pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 88.2881 +#endif
 88.2882 +
 88.2883 +            /*
 88.2884 +               if (pa <= pb && pa <= pc)
 88.2885 +                  p = a;
 88.2886 +               else if (pb <= pc)
 88.2887 +                  p = b;
 88.2888 +               else
 88.2889 +                  p = c;
 88.2890 +             */
 88.2891 +
 88.2892 +            p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
 88.2893 +
 88.2894 +            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
 88.2895 +            rp++;
 88.2896 +         }
 88.2897 +         break;
 88.2898 +      }
 88.2899 +      default:
 88.2900 +         png_warning(png_ptr, "Ignoring bad adaptive filter type");
 88.2901 +         *row = 0;
 88.2902 +         break;
 88.2903 +   }
 88.2904 +}
 88.2905 +
 88.2906 +void /* PRIVATE */
 88.2907 +png_read_finish_row(png_structp png_ptr)
 88.2908 +{
 88.2909 +#ifdef PNG_USE_LOCAL_ARRAYS
 88.2910 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 88.2911 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 88.2912 +
 88.2913 +   /* start of interlace block */
 88.2914 +   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 88.2915 +
 88.2916 +   /* offset to next interlace block */
 88.2917 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 88.2918 +
 88.2919 +   /* start of interlace block in the y direction */
 88.2920 +   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 88.2921 +
 88.2922 +   /* offset to next interlace block in the y direction */
 88.2923 +   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 88.2924 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 88.2925 +#endif
 88.2926 +
 88.2927 +   png_debug(1, "in png_read_finish_row\n");
 88.2928 +   png_ptr->row_number++;
 88.2929 +   if (png_ptr->row_number < png_ptr->num_rows)
 88.2930 +      return;
 88.2931 +
 88.2932 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 88.2933 +   if (png_ptr->interlaced)
 88.2934 +   {
 88.2935 +      png_ptr->row_number = 0;
 88.2936 +      png_memset_check(png_ptr, png_ptr->prev_row, 0,
 88.2937 +         png_ptr->rowbytes + 1);
 88.2938 +      do
 88.2939 +      {
 88.2940 +         png_ptr->pass++;
 88.2941 +         if (png_ptr->pass >= 7)
 88.2942 +            break;
 88.2943 +         png_ptr->iwidth = (png_ptr->width +
 88.2944 +            png_pass_inc[png_ptr->pass] - 1 -
 88.2945 +            png_pass_start[png_ptr->pass]) /
 88.2946 +            png_pass_inc[png_ptr->pass];
 88.2947 +
 88.2948 +         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
 88.2949 +            png_ptr->iwidth) + 1;
 88.2950 +
 88.2951 +         if (!(png_ptr->transformations & PNG_INTERLACE))
 88.2952 +         {
 88.2953 +            png_ptr->num_rows = (png_ptr->height +
 88.2954 +               png_pass_yinc[png_ptr->pass] - 1 -
 88.2955 +               png_pass_ystart[png_ptr->pass]) /
 88.2956 +               png_pass_yinc[png_ptr->pass];
 88.2957 +            if (!(png_ptr->num_rows))
 88.2958 +               continue;
 88.2959 +         }
 88.2960 +         else  /* if (png_ptr->transformations & PNG_INTERLACE) */
 88.2961 +            break;
 88.2962 +      } while (png_ptr->iwidth == 0);
 88.2963 +
 88.2964 +      if (png_ptr->pass < 7)
 88.2965 +         return;
 88.2966 +   }
 88.2967 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 88.2968 +
 88.2969 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
 88.2970 +   {
 88.2971 +#ifdef PNG_USE_LOCAL_ARRAYS
 88.2972 +      PNG_CONST PNG_IDAT;
 88.2973 +#endif
 88.2974 +      char extra;
 88.2975 +      int ret;
 88.2976 +
 88.2977 +      png_ptr->zstream.next_out = (Byte *)&extra;
 88.2978 +      png_ptr->zstream.avail_out = (uInt)1;
 88.2979 +      for (;;)
 88.2980 +      {
 88.2981 +         if (!(png_ptr->zstream.avail_in))
 88.2982 +         {
 88.2983 +            while (!png_ptr->idat_size)
 88.2984 +            {
 88.2985 +               png_byte chunk_length[4];
 88.2986 +
 88.2987 +               png_crc_finish(png_ptr, 0);
 88.2988 +
 88.2989 +               png_read_data(png_ptr, chunk_length, 4);
 88.2990 +               png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
 88.2991 +               png_reset_crc(png_ptr);
 88.2992 +               png_crc_read(png_ptr, png_ptr->chunk_name, 4);
 88.2993 +               if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
 88.2994 +                  png_error(png_ptr, "Not enough image data");
 88.2995 +
 88.2996 +            }
 88.2997 +            png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
 88.2998 +            png_ptr->zstream.next_in = png_ptr->zbuf;
 88.2999 +            if (png_ptr->zbuf_size > png_ptr->idat_size)
 88.3000 +               png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
 88.3001 +            png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
 88.3002 +            png_ptr->idat_size -= png_ptr->zstream.avail_in;
 88.3003 +         }
 88.3004 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
 88.3005 +         if (ret == Z_STREAM_END)
 88.3006 +         {
 88.3007 +            if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
 88.3008 +               png_ptr->idat_size)
 88.3009 +               png_warning(png_ptr, "Extra compressed data");
 88.3010 +            png_ptr->mode |= PNG_AFTER_IDAT;
 88.3011 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 88.3012 +            break;
 88.3013 +         }
 88.3014 +         if (ret != Z_OK)
 88.3015 +            png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
 88.3016 +                      "Decompression Error");
 88.3017 +
 88.3018 +         if (!(png_ptr->zstream.avail_out))
 88.3019 +         {
 88.3020 +            png_warning(png_ptr, "Extra compressed data.");
 88.3021 +            png_ptr->mode |= PNG_AFTER_IDAT;
 88.3022 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 88.3023 +            break;
 88.3024 +         }
 88.3025 +
 88.3026 +      }
 88.3027 +      png_ptr->zstream.avail_out = 0;
 88.3028 +   }
 88.3029 +
 88.3030 +   if (png_ptr->idat_size || png_ptr->zstream.avail_in)
 88.3031 +      png_warning(png_ptr, "Extra compression data");
 88.3032 +
 88.3033 +   inflateReset(&png_ptr->zstream);
 88.3034 +
 88.3035 +   png_ptr->mode |= PNG_AFTER_IDAT;
 88.3036 +}
 88.3037 +
 88.3038 +void /* PRIVATE */
 88.3039 +png_read_start_row(png_structp png_ptr)
 88.3040 +{
 88.3041 +#ifdef PNG_USE_LOCAL_ARRAYS
 88.3042 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 88.3043 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 88.3044 +
 88.3045 +   /* start of interlace block */
 88.3046 +   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 88.3047 +
 88.3048 +   /* offset to next interlace block */
 88.3049 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 88.3050 +
 88.3051 +   /* start of interlace block in the y direction */
 88.3052 +   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 88.3053 +
 88.3054 +   /* offset to next interlace block in the y direction */
 88.3055 +   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 88.3056 +#endif
 88.3057 +#endif
 88.3058 +
 88.3059 +   int max_pixel_depth;
 88.3060 +   png_size_t row_bytes;
 88.3061 +
 88.3062 +   png_debug(1, "in png_read_start_row\n");
 88.3063 +   png_ptr->zstream.avail_in = 0;
 88.3064 +   png_init_read_transformations(png_ptr);
 88.3065 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 88.3066 +   if (png_ptr->interlaced)
 88.3067 +   {
 88.3068 +      if (!(png_ptr->transformations & PNG_INTERLACE))
 88.3069 +         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
 88.3070 +            png_pass_ystart[0]) / png_pass_yinc[0];
 88.3071 +      else
 88.3072 +         png_ptr->num_rows = png_ptr->height;
 88.3073 +
 88.3074 +      png_ptr->iwidth = (png_ptr->width +
 88.3075 +         png_pass_inc[png_ptr->pass] - 1 -
 88.3076 +         png_pass_start[png_ptr->pass]) /
 88.3077 +         png_pass_inc[png_ptr->pass];
 88.3078 +
 88.3079 +         png_ptr->irowbytes =
 88.3080 +            PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
 88.3081 +   }
 88.3082 +   else
 88.3083 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 88.3084 +   {
 88.3085 +      png_ptr->num_rows = png_ptr->height;
 88.3086 +      png_ptr->iwidth = png_ptr->width;
 88.3087 +      png_ptr->irowbytes = png_ptr->rowbytes + 1;
 88.3088 +   }
 88.3089 +   max_pixel_depth = png_ptr->pixel_depth;
 88.3090 +
 88.3091 +#if defined(PNG_READ_PACK_SUPPORTED)
 88.3092 +   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
 88.3093 +      max_pixel_depth = 8;
 88.3094 +#endif
 88.3095 +
 88.3096 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 88.3097 +   if (png_ptr->transformations & PNG_EXPAND)
 88.3098 +   {
 88.3099 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 88.3100 +      {
 88.3101 +         if (png_ptr->num_trans)
 88.3102 +            max_pixel_depth = 32;
 88.3103 +         else
 88.3104 +            max_pixel_depth = 24;
 88.3105 +      }
 88.3106 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 88.3107 +      {
 88.3108 +         if (max_pixel_depth < 8)
 88.3109 +            max_pixel_depth = 8;
 88.3110 +         if (png_ptr->num_trans)
 88.3111 +            max_pixel_depth *= 2;
 88.3112 +      }
 88.3113 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 88.3114 +      {
 88.3115 +         if (png_ptr->num_trans)
 88.3116 +         {
 88.3117 +            max_pixel_depth *= 4;
 88.3118 +            max_pixel_depth /= 3;
 88.3119 +         }
 88.3120 +      }
 88.3121 +   }
 88.3122 +#endif
 88.3123 +
 88.3124 +#if defined(PNG_READ_FILLER_SUPPORTED)
 88.3125 +   if (png_ptr->transformations & (PNG_FILLER))
 88.3126 +   {
 88.3127 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 88.3128 +         max_pixel_depth = 32;
 88.3129 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 88.3130 +      {
 88.3131 +         if (max_pixel_depth <= 8)
 88.3132 +            max_pixel_depth = 16;
 88.3133 +         else
 88.3134 +            max_pixel_depth = 32;
 88.3135 +      }
 88.3136 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 88.3137 +      {
 88.3138 +         if (max_pixel_depth <= 32)
 88.3139 +            max_pixel_depth = 32;
 88.3140 +         else
 88.3141 +            max_pixel_depth = 64;
 88.3142 +      }
 88.3143 +   }
 88.3144 +#endif
 88.3145 +
 88.3146 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 88.3147 +   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
 88.3148 +   {
 88.3149 +      if (
 88.3150 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 88.3151 +        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
 88.3152 +#endif
 88.3153 +#if defined(PNG_READ_FILLER_SUPPORTED)
 88.3154 +        (png_ptr->transformations & (PNG_FILLER)) ||
 88.3155 +#endif
 88.3156 +        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 88.3157 +      {
 88.3158 +         if (max_pixel_depth <= 16)
 88.3159 +            max_pixel_depth = 32;
 88.3160 +         else
 88.3161 +            max_pixel_depth = 64;
 88.3162 +      }
 88.3163 +      else
 88.3164 +      {
 88.3165 +         if (max_pixel_depth <= 8)
 88.3166 +           {
 88.3167 +             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 88.3168 +               max_pixel_depth = 32;
 88.3169 +             else
 88.3170 +               max_pixel_depth = 24;
 88.3171 +           }
 88.3172 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 88.3173 +            max_pixel_depth = 64;
 88.3174 +         else
 88.3175 +            max_pixel_depth = 48;
 88.3176 +      }
 88.3177 +   }
 88.3178 +#endif
 88.3179 +
 88.3180 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
 88.3181 +defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 88.3182 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 88.3183 +     {
 88.3184 +       int user_pixel_depth = png_ptr->user_transform_depth*
 88.3185 +         png_ptr->user_transform_channels;
 88.3186 +       if (user_pixel_depth > max_pixel_depth)
 88.3187 +         max_pixel_depth=user_pixel_depth;
 88.3188 +     }
 88.3189 +#endif
 88.3190 +
 88.3191 +   /* align the width on the next larger 8 pixels.  Mainly used
 88.3192 +      for interlacing */
 88.3193 +   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
 88.3194 +   /* calculate the maximum bytes needed, adding a byte and a pixel
 88.3195 +      for safety's sake */
 88.3196 +   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
 88.3197 +      1 + ((max_pixel_depth + 7) >> 3);
 88.3198 +#ifdef PNG_MAX_MALLOC_64K
 88.3199 +   if (row_bytes > (png_uint_32)65536L)
 88.3200 +      png_error(png_ptr, "This image requires a row greater than 64KB");
 88.3201 +#endif
 88.3202 +
 88.3203 +   if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
 88.3204 +   {
 88.3205 +     png_free(png_ptr, png_ptr->big_row_buf);
 88.3206 +     png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
 88.3207 +     png_ptr->row_buf = png_ptr->big_row_buf+32;
 88.3208 +     png_ptr->old_big_row_buf_size = row_bytes+64;
 88.3209 +   }
 88.3210 +
 88.3211 +#ifdef PNG_MAX_MALLOC_64K
 88.3212 +   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
 88.3213 +      png_error(png_ptr, "This image requires a row greater than 64KB");
 88.3214 +#endif
 88.3215 +   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
 88.3216 +      png_error(png_ptr, "Row has too many bytes to allocate in memory.");
 88.3217 +
 88.3218 +   if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
 88.3219 +   {
 88.3220 +     png_free(png_ptr, png_ptr->prev_row);
 88.3221 +     png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
 88.3222 +        png_ptr->rowbytes + 1));
 88.3223 +     png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
 88.3224 +   }
 88.3225 +
 88.3226 +   png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
 88.3227 +
 88.3228 +   png_debug1(3, "width = %lu,\n", png_ptr->width);
 88.3229 +   png_debug1(3, "height = %lu,\n", png_ptr->height);
 88.3230 +   png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
 88.3231 +   png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
 88.3232 +   png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
 88.3233 +   png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
 88.3234 +
 88.3235 +   png_ptr->flags |= PNG_FLAG_ROW_INIT;
 88.3236 +}
 88.3237 +#endif /* PNG_READ_SUPPORTED */
    89.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    89.2 +++ b/libs/libpng/pngset.c	Sun Jun 07 17:25:49 2015 +0300
    89.3 @@ -0,0 +1,1293 @@
    89.4 +
    89.5 +/* pngset.c - storage of image information into info struct
    89.6 + *
    89.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    89.8 + * For conditions of distribution and use, see copyright notice in png.h
    89.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   89.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   89.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   89.12 + *
   89.13 + * The functions here are used during reads to store data from the file
   89.14 + * into the info struct, and during writes to store application data
   89.15 + * into the info struct for writing into the file.  This abstracts the
   89.16 + * info struct and allows us to change the structure in the future.
   89.17 + */
   89.18 +
   89.19 +#define PNG_INTERNAL
   89.20 +#include "png.h"
   89.21 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   89.22 +
   89.23 +#if defined(PNG_bKGD_SUPPORTED)
   89.24 +void PNGAPI
   89.25 +png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
   89.26 +{
   89.27 +   png_debug1(1, "in %s storage function\n", "bKGD");
   89.28 +   if (png_ptr == NULL || info_ptr == NULL)
   89.29 +      return;
   89.30 +
   89.31 +   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
   89.32 +   info_ptr->valid |= PNG_INFO_bKGD;
   89.33 +}
   89.34 +#endif
   89.35 +
   89.36 +#if defined(PNG_cHRM_SUPPORTED)
   89.37 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   89.38 +void PNGAPI
   89.39 +png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
   89.40 +   double white_x, double white_y, double red_x, double red_y,
   89.41 +   double green_x, double green_y, double blue_x, double blue_y)
   89.42 +{
   89.43 +   png_debug1(1, "in %s storage function\n", "cHRM");
   89.44 +   if (png_ptr == NULL || info_ptr == NULL)
   89.45 +      return;
   89.46 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
   89.47 +       blue_x || blue_y))
   89.48 +   {
   89.49 +      png_warning(png_ptr,
   89.50 +        "Ignoring attempt to set all-zero chromaticity values");
   89.51 +      return;
   89.52 +   }
   89.53 +   if (white_x < 0.0 || white_y < 0.0 ||
   89.54 +         red_x < 0.0 ||   red_y < 0.0 ||
   89.55 +       green_x < 0.0 || green_y < 0.0 ||
   89.56 +        blue_x < 0.0 ||  blue_y < 0.0)
   89.57 +   {
   89.58 +      png_warning(png_ptr,
   89.59 +        "Ignoring attempt to set negative chromaticity value");
   89.60 +      return;
   89.61 +   }
   89.62 +   if (white_x > 21474.83 || white_y > 21474.83 ||
   89.63 +         red_x > 21474.83 ||   red_y > 21474.83 ||
   89.64 +       green_x > 21474.83 || green_y > 21474.83 ||
   89.65 +        blue_x > 21474.83 ||  blue_y > 21474.83)
   89.66 +   {
   89.67 +      png_warning(png_ptr,
   89.68 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
   89.69 +      return;
   89.70 +   }
   89.71 +
   89.72 +   info_ptr->x_white = (float)white_x;
   89.73 +   info_ptr->y_white = (float)white_y;
   89.74 +   info_ptr->x_red   = (float)red_x;
   89.75 +   info_ptr->y_red   = (float)red_y;
   89.76 +   info_ptr->x_green = (float)green_x;
   89.77 +   info_ptr->y_green = (float)green_y;
   89.78 +   info_ptr->x_blue  = (float)blue_x;
   89.79 +   info_ptr->y_blue  = (float)blue_y;
   89.80 +#ifdef PNG_FIXED_POINT_SUPPORTED
   89.81 +   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
   89.82 +   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
   89.83 +   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
   89.84 +   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
   89.85 +   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
   89.86 +   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
   89.87 +   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
   89.88 +   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
   89.89 +#endif
   89.90 +   info_ptr->valid |= PNG_INFO_cHRM;
   89.91 +}
   89.92 +#endif
   89.93 +#ifdef PNG_FIXED_POINT_SUPPORTED
   89.94 +void PNGAPI
   89.95 +png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
   89.96 +   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
   89.97 +   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
   89.98 +   png_fixed_point blue_x, png_fixed_point blue_y)
   89.99 +{
  89.100 +   png_debug1(1, "in %s storage function\n", "cHRM");
  89.101 +   if (png_ptr == NULL || info_ptr == NULL)
  89.102 +      return;
  89.103 +
  89.104 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
  89.105 +       blue_x || blue_y))
  89.106 +   {
  89.107 +      png_warning(png_ptr,
  89.108 +        "Ignoring attempt to set all-zero chromaticity values");
  89.109 +      return;
  89.110 +   }
  89.111 +   if (white_x < 0 || white_y < 0 ||
  89.112 +         red_x < 0 ||   red_y < 0 ||
  89.113 +       green_x < 0 || green_y < 0 ||
  89.114 +        blue_x < 0 ||  blue_y < 0)
  89.115 +   {
  89.116 +      png_warning(png_ptr,
  89.117 +        "Ignoring attempt to set negative chromaticity value");
  89.118 +      return;
  89.119 +   }
  89.120 +   if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
  89.121 +       white_y > (png_fixed_point) PNG_UINT_31_MAX ||
  89.122 +         red_x > (png_fixed_point) PNG_UINT_31_MAX ||
  89.123 +         red_y > (png_fixed_point) PNG_UINT_31_MAX ||
  89.124 +       green_x > (png_fixed_point) PNG_UINT_31_MAX ||
  89.125 +       green_y > (png_fixed_point) PNG_UINT_31_MAX ||
  89.126 +        blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
  89.127 +        blue_y > (png_fixed_point) PNG_UINT_31_MAX )
  89.128 +   {
  89.129 +      png_warning(png_ptr,
  89.130 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
  89.131 +      return;
  89.132 +   }
  89.133 +   info_ptr->int_x_white = white_x;
  89.134 +   info_ptr->int_y_white = white_y;
  89.135 +   info_ptr->int_x_red   = red_x;
  89.136 +   info_ptr->int_y_red   = red_y;
  89.137 +   info_ptr->int_x_green = green_x;
  89.138 +   info_ptr->int_y_green = green_y;
  89.139 +   info_ptr->int_x_blue  = blue_x;
  89.140 +   info_ptr->int_y_blue  = blue_y;
  89.141 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.142 +   info_ptr->x_white = (float)(white_x/100000.);
  89.143 +   info_ptr->y_white = (float)(white_y/100000.);
  89.144 +   info_ptr->x_red   = (float)(  red_x/100000.);
  89.145 +   info_ptr->y_red   = (float)(  red_y/100000.);
  89.146 +   info_ptr->x_green = (float)(green_x/100000.);
  89.147 +   info_ptr->y_green = (float)(green_y/100000.);
  89.148 +   info_ptr->x_blue  = (float)( blue_x/100000.);
  89.149 +   info_ptr->y_blue  = (float)( blue_y/100000.);
  89.150 +#endif
  89.151 +   info_ptr->valid |= PNG_INFO_cHRM;
  89.152 +}
  89.153 +#endif
  89.154 +#endif
  89.155 +
  89.156 +#if defined(PNG_gAMA_SUPPORTED)
  89.157 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.158 +void PNGAPI
  89.159 +png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  89.160 +{
  89.161 +   double gamma;
  89.162 +   png_debug1(1, "in %s storage function\n", "gAMA");
  89.163 +   if (png_ptr == NULL || info_ptr == NULL)
  89.164 +      return;
  89.165 +
  89.166 +   /* Check for overflow */
  89.167 +   if (file_gamma > 21474.83)
  89.168 +   {
  89.169 +      png_warning(png_ptr, "Limiting gamma to 21474.83");
  89.170 +      gamma=21474.83;
  89.171 +   }
  89.172 +   else
  89.173 +      gamma = file_gamma;
  89.174 +   info_ptr->gamma = (float)gamma;
  89.175 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.176 +   info_ptr->int_gamma = (int)(gamma*100000.+.5);
  89.177 +#endif
  89.178 +   info_ptr->valid |= PNG_INFO_gAMA;
  89.179 +   if (gamma == 0.0)
  89.180 +      png_warning(png_ptr, "Setting gamma=0");
  89.181 +}
  89.182 +#endif
  89.183 +void PNGAPI
  89.184 +png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  89.185 +   int_gamma)
  89.186 +{
  89.187 +   png_fixed_point gamma;
  89.188 +
  89.189 +   png_debug1(1, "in %s storage function\n", "gAMA");
  89.190 +   if (png_ptr == NULL || info_ptr == NULL)
  89.191 +      return;
  89.192 +
  89.193 +   if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
  89.194 +   {
  89.195 +     png_warning(png_ptr, "Limiting gamma to 21474.83");
  89.196 +     gamma=PNG_UINT_31_MAX;
  89.197 +   }
  89.198 +   else
  89.199 +   {
  89.200 +     if (int_gamma < 0)
  89.201 +     {
  89.202 +       png_warning(png_ptr, "Setting negative gamma to zero");
  89.203 +       gamma = 0;
  89.204 +     }
  89.205 +     else
  89.206 +       gamma = int_gamma;
  89.207 +   }
  89.208 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.209 +   info_ptr->gamma = (float)(gamma/100000.);
  89.210 +#endif
  89.211 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.212 +   info_ptr->int_gamma = gamma;
  89.213 +#endif
  89.214 +   info_ptr->valid |= PNG_INFO_gAMA;
  89.215 +   if (gamma == 0)
  89.216 +      png_warning(png_ptr, "Setting gamma=0");
  89.217 +}
  89.218 +#endif
  89.219 +
  89.220 +#if defined(PNG_hIST_SUPPORTED)
  89.221 +void PNGAPI
  89.222 +png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
  89.223 +{
  89.224 +   int i;
  89.225 +
  89.226 +   png_debug1(1, "in %s storage function\n", "hIST");
  89.227 +   if (png_ptr == NULL || info_ptr == NULL)
  89.228 +      return;
  89.229 +   if (info_ptr->num_palette == 0 || info_ptr->num_palette
  89.230 +       > PNG_MAX_PALETTE_LENGTH)
  89.231 +   {
  89.232 +       png_warning(png_ptr,
  89.233 +          "Invalid palette size, hIST allocation skipped.");
  89.234 +       return;
  89.235 +   }
  89.236 +
  89.237 +#ifdef PNG_FREE_ME_SUPPORTED
  89.238 +   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  89.239 +#endif
  89.240 +   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
  89.241 +      1.2.1 */
  89.242 +   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  89.243 +      (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)));
  89.244 +   if (png_ptr->hist == NULL)
  89.245 +     {
  89.246 +       png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
  89.247 +       return;
  89.248 +     }
  89.249 +
  89.250 +   for (i = 0; i < info_ptr->num_palette; i++)
  89.251 +       png_ptr->hist[i] = hist[i];
  89.252 +   info_ptr->hist = png_ptr->hist;
  89.253 +   info_ptr->valid |= PNG_INFO_hIST;
  89.254 +
  89.255 +#ifdef PNG_FREE_ME_SUPPORTED
  89.256 +   info_ptr->free_me |= PNG_FREE_HIST;
  89.257 +#else
  89.258 +   png_ptr->flags |= PNG_FLAG_FREE_HIST;
  89.259 +#endif
  89.260 +}
  89.261 +#endif
  89.262 +
  89.263 +void PNGAPI
  89.264 +png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  89.265 +   png_uint_32 width, png_uint_32 height, int bit_depth,
  89.266 +   int color_type, int interlace_type, int compression_type,
  89.267 +   int filter_type)
  89.268 +{
  89.269 +   png_debug1(1, "in %s storage function\n", "IHDR");
  89.270 +   if (png_ptr == NULL || info_ptr == NULL)
  89.271 +      return;
  89.272 +
  89.273 +   /* check for width and height valid values */
  89.274 +   if (width == 0 || height == 0)
  89.275 +      png_error(png_ptr, "Image width or height is zero in IHDR");
  89.276 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  89.277 +   if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
  89.278 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
  89.279 +#else
  89.280 +   if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
  89.281 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
  89.282 +#endif
  89.283 +   if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
  89.284 +      png_error(png_ptr, "Invalid image size in IHDR");
  89.285 +   if ( width > (PNG_UINT_32_MAX
  89.286 +                 >> 3)      /* 8-byte RGBA pixels */
  89.287 +                 - 64       /* bigrowbuf hack */
  89.288 +                 - 1        /* filter byte */
  89.289 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  89.290 +                 - 8)       /* extra max_pixel_depth pad */
  89.291 +      png_warning(png_ptr, "Width is too large for libpng to process pixels");
  89.292 +
  89.293 +   /* check other values */
  89.294 +   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  89.295 +      bit_depth != 8 && bit_depth != 16)
  89.296 +      png_error(png_ptr, "Invalid bit depth in IHDR");
  89.297 +
  89.298 +   if (color_type < 0 || color_type == 1 ||
  89.299 +      color_type == 5 || color_type > 6)
  89.300 +      png_error(png_ptr, "Invalid color type in IHDR");
  89.301 +
  89.302 +   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  89.303 +       ((color_type == PNG_COLOR_TYPE_RGB ||
  89.304 +         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  89.305 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  89.306 +      png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
  89.307 +
  89.308 +   if (interlace_type >= PNG_INTERLACE_LAST)
  89.309 +      png_error(png_ptr, "Unknown interlace method in IHDR");
  89.310 +
  89.311 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  89.312 +      png_error(png_ptr, "Unknown compression method in IHDR");
  89.313 +
  89.314 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  89.315 +   /* Accept filter_method 64 (intrapixel differencing) only if
  89.316 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  89.317 +    * 2. Libpng did not read a PNG signature (this filter_method is only
  89.318 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  89.319 +    * 3. The application called png_permit_mng_features with a mask that
  89.320 +    *    included PNG_FLAG_MNG_FILTER_64 and
  89.321 +    * 4. The filter_method is 64 and
  89.322 +    * 5. The color_type is RGB or RGBA
  89.323 +    */
  89.324 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
  89.325 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  89.326 +   if (filter_type != PNG_FILTER_TYPE_BASE)
  89.327 +   {
  89.328 +     if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  89.329 +        (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  89.330 +        ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  89.331 +        (color_type == PNG_COLOR_TYPE_RGB ||
  89.332 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  89.333 +        png_error(png_ptr, "Unknown filter method in IHDR");
  89.334 +     if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
  89.335 +        png_warning(png_ptr, "Invalid filter method in IHDR");
  89.336 +   }
  89.337 +#else
  89.338 +   if (filter_type != PNG_FILTER_TYPE_BASE)
  89.339 +      png_error(png_ptr, "Unknown filter method in IHDR");
  89.340 +#endif
  89.341 +
  89.342 +   info_ptr->width = width;
  89.343 +   info_ptr->height = height;
  89.344 +   info_ptr->bit_depth = (png_byte)bit_depth;
  89.345 +   info_ptr->color_type =(png_byte) color_type;
  89.346 +   info_ptr->compression_type = (png_byte)compression_type;
  89.347 +   info_ptr->filter_type = (png_byte)filter_type;
  89.348 +   info_ptr->interlace_type = (png_byte)interlace_type;
  89.349 +   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  89.350 +      info_ptr->channels = 1;
  89.351 +   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  89.352 +      info_ptr->channels = 3;
  89.353 +   else
  89.354 +      info_ptr->channels = 1;
  89.355 +   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  89.356 +      info_ptr->channels++;
  89.357 +   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  89.358 +
  89.359 +   /* check for potential overflow */
  89.360 +   if (width > (PNG_UINT_32_MAX
  89.361 +                 >> 3)      /* 8-byte RGBA pixels */
  89.362 +                 - 64       /* bigrowbuf hack */
  89.363 +                 - 1        /* filter byte */
  89.364 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  89.365 +                 - 8)       /* extra max_pixel_depth pad */
  89.366 +      info_ptr->rowbytes = (png_size_t)0;
  89.367 +   else
  89.368 +      info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  89.369 +}
  89.370 +
  89.371 +#if defined(PNG_oFFs_SUPPORTED)
  89.372 +void PNGAPI
  89.373 +png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  89.374 +   png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  89.375 +{
  89.376 +   png_debug1(1, "in %s storage function\n", "oFFs");
  89.377 +   if (png_ptr == NULL || info_ptr == NULL)
  89.378 +      return;
  89.379 +
  89.380 +   info_ptr->x_offset = offset_x;
  89.381 +   info_ptr->y_offset = offset_y;
  89.382 +   info_ptr->offset_unit_type = (png_byte)unit_type;
  89.383 +   info_ptr->valid |= PNG_INFO_oFFs;
  89.384 +}
  89.385 +#endif
  89.386 +
  89.387 +#if defined(PNG_pCAL_SUPPORTED)
  89.388 +void PNGAPI
  89.389 +png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  89.390 +   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
  89.391 +   png_charp units, png_charpp params)
  89.392 +{
  89.393 +   png_uint_32 length;
  89.394 +   int i;
  89.395 +
  89.396 +   png_debug1(1, "in %s storage function\n", "pCAL");
  89.397 +   if (png_ptr == NULL || info_ptr == NULL)
  89.398 +      return;
  89.399 +
  89.400 +   length = png_strlen(purpose) + 1;
  89.401 +   png_debug1(3, "allocating purpose for info (%lu bytes)\n",
  89.402 +     (unsigned long)length);
  89.403 +   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  89.404 +   if (info_ptr->pcal_purpose == NULL)
  89.405 +   {
  89.406 +       png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
  89.407 +      return;
  89.408 +   }
  89.409 +   png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
  89.410 +
  89.411 +   png_debug(3, "storing X0, X1, type, and nparams in info\n");
  89.412 +   info_ptr->pcal_X0 = X0;
  89.413 +   info_ptr->pcal_X1 = X1;
  89.414 +   info_ptr->pcal_type = (png_byte)type;
  89.415 +   info_ptr->pcal_nparams = (png_byte)nparams;
  89.416 +
  89.417 +   length = png_strlen(units) + 1;
  89.418 +   png_debug1(3, "allocating units for info (%lu bytes)\n",
  89.419 +     (unsigned long)length);
  89.420 +   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  89.421 +   if (info_ptr->pcal_units == NULL)
  89.422 +   {
  89.423 +       png_warning(png_ptr, "Insufficient memory for pCAL units.");
  89.424 +      return;
  89.425 +   }
  89.426 +   png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
  89.427 +
  89.428 +   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  89.429 +      (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
  89.430 +   if (info_ptr->pcal_params == NULL)
  89.431 +   {
  89.432 +       png_warning(png_ptr, "Insufficient memory for pCAL params.");
  89.433 +      return;
  89.434 +   }
  89.435 +
  89.436 +   info_ptr->pcal_params[nparams] = NULL;
  89.437 +
  89.438 +   for (i = 0; i < nparams; i++)
  89.439 +   {
  89.440 +      length = png_strlen(params[i]) + 1;
  89.441 +      png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i,
  89.442 +        (unsigned long)length);
  89.443 +      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  89.444 +      if (info_ptr->pcal_params[i] == NULL)
  89.445 +      {
  89.446 +          png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
  89.447 +          return;
  89.448 +      }
  89.449 +      png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
  89.450 +   }
  89.451 +
  89.452 +   info_ptr->valid |= PNG_INFO_pCAL;
  89.453 +#ifdef PNG_FREE_ME_SUPPORTED
  89.454 +   info_ptr->free_me |= PNG_FREE_PCAL;
  89.455 +#endif
  89.456 +}
  89.457 +#endif
  89.458 +
  89.459 +#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
  89.460 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.461 +void PNGAPI
  89.462 +png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
  89.463 +             int unit, double width, double height)
  89.464 +{
  89.465 +   png_debug1(1, "in %s storage function\n", "sCAL");
  89.466 +   if (png_ptr == NULL || info_ptr == NULL)
  89.467 +      return;
  89.468 +
  89.469 +   info_ptr->scal_unit = (png_byte)unit;
  89.470 +   info_ptr->scal_pixel_width = width;
  89.471 +   info_ptr->scal_pixel_height = height;
  89.472 +
  89.473 +   info_ptr->valid |= PNG_INFO_sCAL;
  89.474 +}
  89.475 +#else
  89.476 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.477 +void PNGAPI
  89.478 +png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  89.479 +             int unit, png_charp swidth, png_charp sheight)
  89.480 +{
  89.481 +   png_uint_32 length;
  89.482 +
  89.483 +   png_debug1(1, "in %s storage function\n", "sCAL");
  89.484 +   if (png_ptr == NULL || info_ptr == NULL)
  89.485 +      return;
  89.486 +
  89.487 +   info_ptr->scal_unit = (png_byte)unit;
  89.488 +
  89.489 +   length = png_strlen(swidth) + 1;
  89.490 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
  89.491 +      (unsigned int)length);
  89.492 +   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
  89.493 +   if (info_ptr->scal_s_width == NULL)
  89.494 +   {
  89.495 +      png_warning(png_ptr,
  89.496 +       "Memory allocation failed while processing sCAL.");
  89.497 +      return;
  89.498 +   }
  89.499 +   png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
  89.500 +
  89.501 +   length = png_strlen(sheight) + 1;
  89.502 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
  89.503 +      (unsigned int)length);
  89.504 +   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
  89.505 +   if (info_ptr->scal_s_height == NULL)
  89.506 +   {
  89.507 +      png_free (png_ptr, info_ptr->scal_s_width);
  89.508 +      info_ptr->scal_s_width = NULL;
  89.509 +      png_warning(png_ptr,
  89.510 +       "Memory allocation failed while processing sCAL.");
  89.511 +      return;
  89.512 +   }
  89.513 +   png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
  89.514 +   info_ptr->valid |= PNG_INFO_sCAL;
  89.515 +#ifdef PNG_FREE_ME_SUPPORTED
  89.516 +   info_ptr->free_me |= PNG_FREE_SCAL;
  89.517 +#endif
  89.518 +}
  89.519 +#endif
  89.520 +#endif
  89.521 +#endif
  89.522 +
  89.523 +#if defined(PNG_pHYs_SUPPORTED)
  89.524 +void PNGAPI
  89.525 +png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  89.526 +   png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  89.527 +{
  89.528 +   png_debug1(1, "in %s storage function\n", "pHYs");
  89.529 +   if (png_ptr == NULL || info_ptr == NULL)
  89.530 +      return;
  89.531 +
  89.532 +   info_ptr->x_pixels_per_unit = res_x;
  89.533 +   info_ptr->y_pixels_per_unit = res_y;
  89.534 +   info_ptr->phys_unit_type = (png_byte)unit_type;
  89.535 +   info_ptr->valid |= PNG_INFO_pHYs;
  89.536 +}
  89.537 +#endif
  89.538 +
  89.539 +void PNGAPI
  89.540 +png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  89.541 +   png_colorp palette, int num_palette)
  89.542 +{
  89.543 +
  89.544 +   png_debug1(1, "in %s storage function\n", "PLTE");
  89.545 +   if (png_ptr == NULL || info_ptr == NULL)
  89.546 +      return;
  89.547 +
  89.548 +   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  89.549 +     {
  89.550 +       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  89.551 +         png_error(png_ptr, "Invalid palette length");
  89.552 +       else
  89.553 +       {
  89.554 +         png_warning(png_ptr, "Invalid palette length");
  89.555 +         return;
  89.556 +       }
  89.557 +     }
  89.558 +
  89.559 +   /*
  89.560 +    * It may not actually be necessary to set png_ptr->palette here;
  89.561 +    * we do it for backward compatibility with the way the png_handle_tRNS
  89.562 +    * function used to do the allocation.
  89.563 +    */
  89.564 +#ifdef PNG_FREE_ME_SUPPORTED
  89.565 +   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  89.566 +#endif
  89.567 +
  89.568 +   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  89.569 +      of num_palette entries,
  89.570 +      in case of an invalid PNG file that has too-large sample values. */
  89.571 +   png_ptr->palette = (png_colorp)png_malloc(png_ptr,
  89.572 +      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  89.573 +   png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
  89.574 +      png_sizeof(png_color));
  89.575 +   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  89.576 +   info_ptr->palette = png_ptr->palette;
  89.577 +   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  89.578 +
  89.579 +#ifdef PNG_FREE_ME_SUPPORTED
  89.580 +   info_ptr->free_me |= PNG_FREE_PLTE;
  89.581 +#else
  89.582 +   png_ptr->flags |= PNG_FLAG_FREE_PLTE;
  89.583 +#endif
  89.584 +
  89.585 +   info_ptr->valid |= PNG_INFO_PLTE;
  89.586 +}
  89.587 +
  89.588 +#if defined(PNG_sBIT_SUPPORTED)
  89.589 +void PNGAPI
  89.590 +png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  89.591 +   png_color_8p sig_bit)
  89.592 +{
  89.593 +   png_debug1(1, "in %s storage function\n", "sBIT");
  89.594 +   if (png_ptr == NULL || info_ptr == NULL)
  89.595 +      return;
  89.596 +
  89.597 +   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  89.598 +   info_ptr->valid |= PNG_INFO_sBIT;
  89.599 +}
  89.600 +#endif
  89.601 +
  89.602 +#if defined(PNG_sRGB_SUPPORTED)
  89.603 +void PNGAPI
  89.604 +png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
  89.605 +{
  89.606 +   png_debug1(1, "in %s storage function\n", "sRGB");
  89.607 +   if (png_ptr == NULL || info_ptr == NULL)
  89.608 +      return;
  89.609 +
  89.610 +   info_ptr->srgb_intent = (png_byte)intent;
  89.611 +   info_ptr->valid |= PNG_INFO_sRGB;
  89.612 +}
  89.613 +
  89.614 +void PNGAPI
  89.615 +png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  89.616 +   int intent)
  89.617 +{
  89.618 +#if defined(PNG_gAMA_SUPPORTED)
  89.619 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.620 +   float file_gamma;
  89.621 +#endif
  89.622 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.623 +   png_fixed_point int_file_gamma;
  89.624 +#endif
  89.625 +#endif
  89.626 +#if defined(PNG_cHRM_SUPPORTED)
  89.627 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.628 +   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  89.629 +#endif
  89.630 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.631 +   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  89.632 +      int_green_y, int_blue_x, int_blue_y;
  89.633 +#endif
  89.634 +#endif
  89.635 +   png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
  89.636 +   if (png_ptr == NULL || info_ptr == NULL)
  89.637 +      return;
  89.638 +
  89.639 +   png_set_sRGB(png_ptr, info_ptr, intent);
  89.640 +
  89.641 +#if defined(PNG_gAMA_SUPPORTED)
  89.642 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.643 +   file_gamma = (float).45455;
  89.644 +   png_set_gAMA(png_ptr, info_ptr, file_gamma);
  89.645 +#endif
  89.646 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.647 +   int_file_gamma = 45455L;
  89.648 +   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
  89.649 +#endif
  89.650 +#endif
  89.651 +
  89.652 +#if defined(PNG_cHRM_SUPPORTED)
  89.653 +#ifdef PNG_FIXED_POINT_SUPPORTED
  89.654 +   int_white_x = 31270L;
  89.655 +   int_white_y = 32900L;
  89.656 +   int_red_x   = 64000L;
  89.657 +   int_red_y   = 33000L;
  89.658 +   int_green_x = 30000L;
  89.659 +   int_green_y = 60000L;
  89.660 +   int_blue_x  = 15000L;
  89.661 +   int_blue_y  =  6000L;
  89.662 +
  89.663 +   png_set_cHRM_fixed(png_ptr, info_ptr,
  89.664 +      int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
  89.665 +      int_blue_x, int_blue_y);
  89.666 +#endif
  89.667 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  89.668 +   white_x = (float).3127;
  89.669 +   white_y = (float).3290;
  89.670 +   red_x   = (float).64;
  89.671 +   red_y   = (float).33;
  89.672 +   green_x = (float).30;
  89.673 +   green_y = (float).60;
  89.674 +   blue_x  = (float).15;
  89.675 +   blue_y  = (float).06;
  89.676 +
  89.677 +   png_set_cHRM(png_ptr, info_ptr,
  89.678 +      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  89.679 +#endif
  89.680 +#endif
  89.681 +}
  89.682 +#endif
  89.683 +
  89.684 +
  89.685 +#if defined(PNG_iCCP_SUPPORTED)
  89.686 +void PNGAPI
  89.687 +png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  89.688 +             png_charp name, int compression_type,
  89.689 +             png_charp profile, png_uint_32 proflen)
  89.690 +{
  89.691 +   png_charp new_iccp_name;
  89.692 +   png_charp new_iccp_profile;
  89.693 +   png_uint_32 length;
  89.694 +
  89.695 +   png_debug1(1, "in %s storage function\n", "iCCP");
  89.696 +   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  89.697 +      return;
  89.698 +
  89.699 +   length = png_strlen(name)+1;
  89.700 +   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  89.701 +   if (new_iccp_name == NULL)
  89.702 +   {
  89.703 +      png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
  89.704 +      return;
  89.705 +   }
  89.706 +   png_memcpy(new_iccp_name, name, length);
  89.707 +   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
  89.708 +   if (new_iccp_profile == NULL)
  89.709 +   {
  89.710 +      png_free (png_ptr, new_iccp_name);
  89.711 +      png_warning(png_ptr,
  89.712 +      "Insufficient memory to process iCCP profile.");
  89.713 +      return;
  89.714 +   }
  89.715 +   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  89.716 +
  89.717 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  89.718 +
  89.719 +   info_ptr->iccp_proflen = proflen;
  89.720 +   info_ptr->iccp_name = new_iccp_name;
  89.721 +   info_ptr->iccp_profile = new_iccp_profile;
  89.722 +   /* Compression is always zero but is here so the API and info structure
  89.723 +    * does not have to change if we introduce multiple compression types */
  89.724 +   info_ptr->iccp_compression = (png_byte)compression_type;
  89.725 +#ifdef PNG_FREE_ME_SUPPORTED
  89.726 +   info_ptr->free_me |= PNG_FREE_ICCP;
  89.727 +#endif
  89.728 +   info_ptr->valid |= PNG_INFO_iCCP;
  89.729 +}
  89.730 +#endif
  89.731 +
  89.732 +#if defined(PNG_TEXT_SUPPORTED)
  89.733 +void PNGAPI
  89.734 +png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  89.735 +   int num_text)
  89.736 +{
  89.737 +   int ret;
  89.738 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  89.739 +   if (ret)
  89.740 +     png_error(png_ptr, "Insufficient memory to store text");
  89.741 +}
  89.742 +
  89.743 +int /* PRIVATE */
  89.744 +png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  89.745 +   int num_text)
  89.746 +{
  89.747 +   int i;
  89.748 +
  89.749 +   png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
  89.750 +      "text" : (png_const_charp)png_ptr->chunk_name));
  89.751 +
  89.752 +   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  89.753 +      return(0);
  89.754 +
  89.755 +   /* Make sure we have enough space in the "text" array in info_struct
  89.756 +    * to hold all of the incoming text_ptr objects.
  89.757 +    */
  89.758 +   if (info_ptr->num_text + num_text > info_ptr->max_text)
  89.759 +   {
  89.760 +      if (info_ptr->text != NULL)
  89.761 +      {
  89.762 +         png_textp old_text;
  89.763 +         int old_max;
  89.764 +
  89.765 +         old_max = info_ptr->max_text;
  89.766 +         info_ptr->max_text = info_ptr->num_text + num_text + 8;
  89.767 +         old_text = info_ptr->text;
  89.768 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  89.769 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  89.770 +         if (info_ptr->text == NULL)
  89.771 +           {
  89.772 +             png_free(png_ptr, old_text);
  89.773 +             return(1);
  89.774 +           }
  89.775 +         png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
  89.776 +            png_sizeof(png_text)));
  89.777 +         png_free(png_ptr, old_text);
  89.778 +      }
  89.779 +      else
  89.780 +      {
  89.781 +         info_ptr->max_text = num_text + 8;
  89.782 +         info_ptr->num_text = 0;
  89.783 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  89.784 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  89.785 +         if (info_ptr->text == NULL)
  89.786 +           return(1);
  89.787 +#ifdef PNG_FREE_ME_SUPPORTED
  89.788 +         info_ptr->free_me |= PNG_FREE_TEXT;
  89.789 +#endif
  89.790 +      }
  89.791 +      png_debug1(3, "allocated %d entries for info_ptr->text\n",
  89.792 +         info_ptr->max_text);
  89.793 +   }
  89.794 +   for (i = 0; i < num_text; i++)
  89.795 +   {
  89.796 +      png_size_t text_length, key_len;
  89.797 +      png_size_t lang_len, lang_key_len;
  89.798 +      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  89.799 +
  89.800 +      if (text_ptr[i].key == NULL)
  89.801 +          continue;
  89.802 +
  89.803 +      key_len = png_strlen(text_ptr[i].key);
  89.804 +
  89.805 +      if (text_ptr[i].compression <= 0)
  89.806 +      {
  89.807 +        lang_len = 0;
  89.808 +        lang_key_len = 0;
  89.809 +      }
  89.810 +      else
  89.811 +#ifdef PNG_iTXt_SUPPORTED
  89.812 +      {
  89.813 +        /* set iTXt data */
  89.814 +        if (text_ptr[i].lang != NULL)
  89.815 +          lang_len = png_strlen(text_ptr[i].lang);
  89.816 +        else
  89.817 +          lang_len = 0;
  89.818 +        if (text_ptr[i].lang_key != NULL)
  89.819 +          lang_key_len = png_strlen(text_ptr[i].lang_key);
  89.820 +        else
  89.821 +          lang_key_len = 0;
  89.822 +      }
  89.823 +#else
  89.824 +      {
  89.825 +        png_warning(png_ptr, "iTXt chunk not supported.");
  89.826 +        continue;
  89.827 +      }
  89.828 +#endif
  89.829 +
  89.830 +      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  89.831 +      {
  89.832 +         text_length = 0;
  89.833 +#ifdef PNG_iTXt_SUPPORTED
  89.834 +         if (text_ptr[i].compression > 0)
  89.835 +            textp->compression = PNG_ITXT_COMPRESSION_NONE;
  89.836 +         else
  89.837 +#endif
  89.838 +            textp->compression = PNG_TEXT_COMPRESSION_NONE;
  89.839 +      }
  89.840 +      else
  89.841 +      {
  89.842 +         text_length = png_strlen(text_ptr[i].text);
  89.843 +         textp->compression = text_ptr[i].compression;
  89.844 +      }
  89.845 +
  89.846 +      textp->key = (png_charp)png_malloc_warn(png_ptr,
  89.847 +         (png_uint_32)
  89.848 +         (key_len + text_length + lang_len + lang_key_len + 4));
  89.849 +      if (textp->key == NULL)
  89.850 +        return(1);
  89.851 +      png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
  89.852 +         (png_uint_32)
  89.853 +         (key_len + lang_len + lang_key_len + text_length + 4),
  89.854 +         (int)textp->key);
  89.855 +
  89.856 +      png_memcpy(textp->key, text_ptr[i].key,
  89.857 +         (png_size_t)(key_len));
  89.858 +      *(textp->key + key_len) = '\0';
  89.859 +#ifdef PNG_iTXt_SUPPORTED
  89.860 +      if (text_ptr[i].compression > 0)
  89.861 +      {
  89.862 +         textp->lang = textp->key + key_len + 1;
  89.863 +         png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  89.864 +         *(textp->lang + lang_len) = '\0';
  89.865 +         textp->lang_key = textp->lang + lang_len + 1;
  89.866 +         png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  89.867 +         *(textp->lang_key + lang_key_len) = '\0';
  89.868 +         textp->text = textp->lang_key + lang_key_len + 1;
  89.869 +      }
  89.870 +      else
  89.871 +#endif
  89.872 +      {
  89.873 +#ifdef PNG_iTXt_SUPPORTED
  89.874 +         textp->lang=NULL;
  89.875 +         textp->lang_key=NULL;
  89.876 +#endif
  89.877 +         textp->text = textp->key + key_len + 1;
  89.878 +      }
  89.879 +      if (text_length)
  89.880 +         png_memcpy(textp->text, text_ptr[i].text,
  89.881 +            (png_size_t)(text_length));
  89.882 +      *(textp->text + text_length) = '\0';
  89.883 +
  89.884 +#ifdef PNG_iTXt_SUPPORTED
  89.885 +      if (textp->compression > 0)
  89.886 +      {
  89.887 +         textp->text_length = 0;
  89.888 +         textp->itxt_length = text_length;
  89.889 +      }
  89.890 +      else
  89.891 +#endif
  89.892 +      {
  89.893 +         textp->text_length = text_length;
  89.894 +#ifdef PNG_iTXt_SUPPORTED
  89.895 +         textp->itxt_length = 0;
  89.896 +#endif
  89.897 +      }
  89.898 +      info_ptr->num_text++;
  89.899 +      png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
  89.900 +   }
  89.901 +   return(0);
  89.902 +}
  89.903 +#endif
  89.904 +
  89.905 +#if defined(PNG_tIME_SUPPORTED)
  89.906 +void PNGAPI
  89.907 +png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
  89.908 +{
  89.909 +   png_debug1(1, "in %s storage function\n", "tIME");
  89.910 +   if (png_ptr == NULL || info_ptr == NULL ||
  89.911 +       (png_ptr->mode & PNG_WROTE_tIME))
  89.912 +      return;
  89.913 +
  89.914 +   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  89.915 +   info_ptr->valid |= PNG_INFO_tIME;
  89.916 +}
  89.917 +#endif
  89.918 +
  89.919 +#if defined(PNG_tRNS_SUPPORTED)
  89.920 +void PNGAPI
  89.921 +png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  89.922 +   png_bytep trans, int num_trans, png_color_16p trans_values)
  89.923 +{
  89.924 +   png_debug1(1, "in %s storage function\n", "tRNS");
  89.925 +   if (png_ptr == NULL || info_ptr == NULL)
  89.926 +      return;
  89.927 +
  89.928 +   if (trans != NULL)
  89.929 +   {
  89.930 +       /*
  89.931 +        * It may not actually be necessary to set png_ptr->trans here;
  89.932 +        * we do it for backward compatibility with the way the png_handle_tRNS
  89.933 +        * function used to do the allocation.
  89.934 +        */
  89.935 +
  89.936 +#ifdef PNG_FREE_ME_SUPPORTED
  89.937 +       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  89.938 +#endif
  89.939 +
  89.940 +       /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  89.941 +       png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
  89.942 +           (png_uint_32)PNG_MAX_PALETTE_LENGTH);
  89.943 +       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  89.944 +         png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
  89.945 +   }
  89.946 +
  89.947 +   if (trans_values != NULL)
  89.948 +   {
  89.949 +      int sample_max = (1 << info_ptr->bit_depth);
  89.950 +      if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  89.951 +          (int)trans_values->gray > sample_max) ||
  89.952 +          (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  89.953 +          ((int)trans_values->red > sample_max ||
  89.954 +          (int)trans_values->green > sample_max ||
  89.955 +          (int)trans_values->blue > sample_max)))
  89.956 +        png_warning(png_ptr,
  89.957 +           "tRNS chunk has out-of-range samples for bit_depth");
  89.958 +      png_memcpy(&(info_ptr->trans_values), trans_values,
  89.959 +         png_sizeof(png_color_16));
  89.960 +      if (num_trans == 0)
  89.961 +        num_trans = 1;
  89.962 +   }
  89.963 +
  89.964 +   info_ptr->num_trans = (png_uint_16)num_trans;
  89.965 +   if (num_trans != 0)
  89.966 +   {
  89.967 +      info_ptr->valid |= PNG_INFO_tRNS;
  89.968 +#ifdef PNG_FREE_ME_SUPPORTED
  89.969 +      info_ptr->free_me |= PNG_FREE_TRNS;
  89.970 +#else
  89.971 +      png_ptr->flags |= PNG_FLAG_FREE_TRNS;
  89.972 +#endif
  89.973 +   }
  89.974 +}
  89.975 +#endif
  89.976 +
  89.977 +#if defined(PNG_sPLT_SUPPORTED)
  89.978 +void PNGAPI
  89.979 +png_set_sPLT(png_structp png_ptr,
  89.980 +             png_infop info_ptr, png_sPLT_tp entries, int nentries)
  89.981 +/*
  89.982 + *  entries        - array of png_sPLT_t structures
  89.983 + *                   to be added to the list of palettes
  89.984 + *                   in the info structure.
  89.985 + *  nentries       - number of palette structures to be
  89.986 + *                   added.
  89.987 + */
  89.988 +{
  89.989 +    png_sPLT_tp np;
  89.990 +    int i;
  89.991 +
  89.992 +    if (png_ptr == NULL || info_ptr == NULL)
  89.993 +       return;
  89.994 +
  89.995 +    np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  89.996 +        (info_ptr->splt_palettes_num + nentries) *
  89.997 +        (png_uint_32)png_sizeof(png_sPLT_t));
  89.998 +    if (np == NULL)
  89.999 +    {
 89.1000 +      png_warning(png_ptr, "No memory for sPLT palettes.");
 89.1001 +      return;
 89.1002 +    }
 89.1003 +
 89.1004 +    png_memcpy(np, info_ptr->splt_palettes,
 89.1005 +           info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
 89.1006 +    png_free(png_ptr, info_ptr->splt_palettes);
 89.1007 +    info_ptr->splt_palettes=NULL;
 89.1008 +
 89.1009 +    for (i = 0; i < nentries; i++)
 89.1010 +    {
 89.1011 +        png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
 89.1012 +        png_sPLT_tp from = entries + i;
 89.1013 +        png_uint_32 length;
 89.1014 +
 89.1015 +        length = png_strlen(from->name) + 1;
 89.1016 +        to->name = (png_charp)png_malloc_warn(png_ptr, length);
 89.1017 +        if (to->name == NULL)
 89.1018 +        {
 89.1019 +           png_warning(png_ptr,
 89.1020 +             "Out of memory while processing sPLT chunk");
 89.1021 +           continue;
 89.1022 +        }
 89.1023 +        png_memcpy(to->name, from->name, length);
 89.1024 +        to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
 89.1025 +            (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry)));
 89.1026 +        if (to->entries == NULL)
 89.1027 +        {
 89.1028 +           png_warning(png_ptr,
 89.1029 +             "Out of memory while processing sPLT chunk");
 89.1030 +           png_free(png_ptr, to->name);
 89.1031 +           to->name = NULL;
 89.1032 +           continue;
 89.1033 +        }
 89.1034 +        png_memcpy(to->entries, from->entries,
 89.1035 +            from->nentries * png_sizeof(png_sPLT_entry));
 89.1036 +        to->nentries = from->nentries;
 89.1037 +        to->depth = from->depth;
 89.1038 +    }
 89.1039 +
 89.1040 +    info_ptr->splt_palettes = np;
 89.1041 +    info_ptr->splt_palettes_num += nentries;
 89.1042 +    info_ptr->valid |= PNG_INFO_sPLT;
 89.1043 +#ifdef PNG_FREE_ME_SUPPORTED
 89.1044 +    info_ptr->free_me |= PNG_FREE_SPLT;
 89.1045 +#endif
 89.1046 +}
 89.1047 +#endif /* PNG_sPLT_SUPPORTED */
 89.1048 +
 89.1049 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 89.1050 +void PNGAPI
 89.1051 +png_set_unknown_chunks(png_structp png_ptr,
 89.1052 +   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
 89.1053 +{
 89.1054 +    png_unknown_chunkp np;
 89.1055 +    int i;
 89.1056 +
 89.1057 +    if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
 89.1058 +        return;
 89.1059 +
 89.1060 +    np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
 89.1061 +        (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) *
 89.1062 +        png_sizeof(png_unknown_chunk)));
 89.1063 +    if (np == NULL)
 89.1064 +    {
 89.1065 +       png_warning(png_ptr,
 89.1066 +          "Out of memory while processing unknown chunk.");
 89.1067 +       return;
 89.1068 +    }
 89.1069 +
 89.1070 +    png_memcpy(np, info_ptr->unknown_chunks,
 89.1071 +           info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
 89.1072 +    png_free(png_ptr, info_ptr->unknown_chunks);
 89.1073 +    info_ptr->unknown_chunks=NULL;
 89.1074 +
 89.1075 +    for (i = 0; i < num_unknowns; i++)
 89.1076 +    {
 89.1077 +       png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
 89.1078 +       png_unknown_chunkp from = unknowns + i;
 89.1079 +
 89.1080 +       png_memcpy((png_charp)to->name, 
 89.1081 +                  (png_charp)from->name, 
 89.1082 +                  png_sizeof(from->name));
 89.1083 +       to->name[png_sizeof(to->name)-1] = '\0';
 89.1084 +       to->size = from->size;
 89.1085 +       /* note our location in the read or write sequence */
 89.1086 +       to->location = (png_byte)(png_ptr->mode & 0xff);
 89.1087 +
 89.1088 +       if (from->size == 0)
 89.1089 +          to->data=NULL;
 89.1090 +       else
 89.1091 +       {
 89.1092 +          to->data = (png_bytep)png_malloc_warn(png_ptr,
 89.1093 +            (png_uint_32)from->size);
 89.1094 +          if (to->data == NULL)
 89.1095 +          {
 89.1096 +             png_warning(png_ptr,
 89.1097 +              "Out of memory while processing unknown chunk.");
 89.1098 +             to->size = 0;
 89.1099 +          }
 89.1100 +          else
 89.1101 +             png_memcpy(to->data, from->data, from->size);
 89.1102 +       }
 89.1103 +    }
 89.1104 +
 89.1105 +    info_ptr->unknown_chunks = np;
 89.1106 +    info_ptr->unknown_chunks_num += num_unknowns;
 89.1107 +#ifdef PNG_FREE_ME_SUPPORTED
 89.1108 +    info_ptr->free_me |= PNG_FREE_UNKN;
 89.1109 +#endif
 89.1110 +}
 89.1111 +void PNGAPI
 89.1112 +png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
 89.1113 +   int chunk, int location)
 89.1114 +{
 89.1115 +   if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
 89.1116 +         (int)info_ptr->unknown_chunks_num)
 89.1117 +      info_ptr->unknown_chunks[chunk].location = (png_byte)location;
 89.1118 +}
 89.1119 +#endif
 89.1120 +
 89.1121 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
 89.1122 +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 89.1123 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 89.1124 +void PNGAPI
 89.1125 +png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
 89.1126 +{
 89.1127 +   /* This function is deprecated in favor of png_permit_mng_features()
 89.1128 +      and will be removed from libpng-1.3.0 */
 89.1129 +   png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
 89.1130 +   if (png_ptr == NULL)
 89.1131 +      return;
 89.1132 +   png_ptr->mng_features_permitted = (png_byte)
 89.1133 +     ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
 89.1134 +     ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
 89.1135 +}
 89.1136 +#endif
 89.1137 +#endif
 89.1138 +
 89.1139 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 89.1140 +png_uint_32 PNGAPI
 89.1141 +png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
 89.1142 +{
 89.1143 +   png_debug(1, "in png_permit_mng_features\n");
 89.1144 +   if (png_ptr == NULL)
 89.1145 +      return (png_uint_32)0;
 89.1146 +   png_ptr->mng_features_permitted =
 89.1147 +     (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
 89.1148 +   return (png_uint_32)png_ptr->mng_features_permitted;
 89.1149 +}
 89.1150 +#endif
 89.1151 +
 89.1152 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 89.1153 +void PNGAPI
 89.1154 +png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
 89.1155 +   chunk_list, int num_chunks)
 89.1156 +{
 89.1157 +    png_bytep new_list, p;
 89.1158 +    int i, old_num_chunks;
 89.1159 +    if (png_ptr == NULL)
 89.1160 +       return;
 89.1161 +    if (num_chunks == 0)
 89.1162 +    {
 89.1163 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
 89.1164 +        png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
 89.1165 +      else
 89.1166 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
 89.1167 +
 89.1168 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS)
 89.1169 +        png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
 89.1170 +      else
 89.1171 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
 89.1172 +      return;
 89.1173 +    }
 89.1174 +    if (chunk_list == NULL)
 89.1175 +      return;
 89.1176 +    old_num_chunks = png_ptr->num_chunk_list;
 89.1177 +    new_list=(png_bytep)png_malloc(png_ptr,
 89.1178 +       (png_uint_32)
 89.1179 +       (5*(num_chunks + old_num_chunks)));
 89.1180 +    if (png_ptr->chunk_list != NULL)
 89.1181 +    {
 89.1182 +       png_memcpy(new_list, png_ptr->chunk_list,
 89.1183 +          (png_size_t)(5*old_num_chunks));
 89.1184 +       png_free(png_ptr, png_ptr->chunk_list);
 89.1185 +       png_ptr->chunk_list=NULL;
 89.1186 +    }
 89.1187 +    png_memcpy(new_list + 5*old_num_chunks, chunk_list,
 89.1188 +       (png_size_t)(5*num_chunks));
 89.1189 +    for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
 89.1190 +       *p=(png_byte)keep;
 89.1191 +    png_ptr->num_chunk_list = old_num_chunks + num_chunks;
 89.1192 +    png_ptr->chunk_list = new_list;
 89.1193 +#ifdef PNG_FREE_ME_SUPPORTED
 89.1194 +    png_ptr->free_me |= PNG_FREE_LIST;
 89.1195 +#endif
 89.1196 +}
 89.1197 +#endif
 89.1198 +
 89.1199 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 89.1200 +void PNGAPI
 89.1201 +png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
 89.1202 +   png_user_chunk_ptr read_user_chunk_fn)
 89.1203 +{
 89.1204 +   png_debug(1, "in png_set_read_user_chunk_fn\n");
 89.1205 +   if (png_ptr == NULL)
 89.1206 +      return;
 89.1207 +   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
 89.1208 +   png_ptr->user_chunk_ptr = user_chunk_ptr;
 89.1209 +}
 89.1210 +#endif
 89.1211 +
 89.1212 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 89.1213 +void PNGAPI
 89.1214 +png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
 89.1215 +{
 89.1216 +   png_debug1(1, "in %s storage function\n", "rows");
 89.1217 +
 89.1218 +   if (png_ptr == NULL || info_ptr == NULL)
 89.1219 +      return;
 89.1220 +
 89.1221 +   if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
 89.1222 +      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
 89.1223 +   info_ptr->row_pointers = row_pointers;
 89.1224 +   if (row_pointers)
 89.1225 +      info_ptr->valid |= PNG_INFO_IDAT;
 89.1226 +}
 89.1227 +#endif
 89.1228 +
 89.1229 +#ifdef PNG_WRITE_SUPPORTED
 89.1230 +void PNGAPI
 89.1231 +png_set_compression_buffer_size(png_structp png_ptr,
 89.1232 +    png_uint_32 size)
 89.1233 +{
 89.1234 +    if (png_ptr == NULL)
 89.1235 +       return;
 89.1236 +    png_free(png_ptr, png_ptr->zbuf);
 89.1237 +    png_ptr->zbuf_size = (png_size_t)size;
 89.1238 +    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
 89.1239 +    png_ptr->zstream.next_out = png_ptr->zbuf;
 89.1240 +    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 89.1241 +}
 89.1242 +#endif
 89.1243 +
 89.1244 +void PNGAPI
 89.1245 +png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
 89.1246 +{
 89.1247 +   if (png_ptr && info_ptr)
 89.1248 +      info_ptr->valid &= ~mask;
 89.1249 +}
 89.1250 +
 89.1251 +
 89.1252 +#ifndef PNG_1_0_X
 89.1253 +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
 89.1254 +/* function was added to libpng 1.2.0 and should always exist by default */
 89.1255 +void PNGAPI
 89.1256 +png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
 89.1257 +{
 89.1258 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
 89.1259 +    if (png_ptr != NULL)
 89.1260 +    png_ptr->asm_flags = 0;
 89.1261 +    asm_flags = asm_flags; /* Quiet the compiler */
 89.1262 +}
 89.1263 +
 89.1264 +/* this function was added to libpng 1.2.0 */
 89.1265 +void PNGAPI
 89.1266 +png_set_mmx_thresholds (png_structp png_ptr,
 89.1267 +                        png_byte mmx_bitdepth_threshold,
 89.1268 +                        png_uint_32 mmx_rowbytes_threshold)
 89.1269 +{
 89.1270 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
 89.1271 +    if (png_ptr == NULL)
 89.1272 +       return;
 89.1273 +    /* Quiet the compiler */
 89.1274 +    mmx_bitdepth_threshold = mmx_bitdepth_threshold;
 89.1275 +    mmx_rowbytes_threshold = mmx_rowbytes_threshold;
 89.1276 +}
 89.1277 +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
 89.1278 +
 89.1279 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 89.1280 +/* this function was added to libpng 1.2.6 */
 89.1281 +void PNGAPI
 89.1282 +png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
 89.1283 +    png_uint_32 user_height_max)
 89.1284 +{
 89.1285 +    /* Images with dimensions larger than these limits will be
 89.1286 +     * rejected by png_set_IHDR().  To accept any PNG datastream
 89.1287 +     * regardless of dimensions, set both limits to 0x7ffffffL.
 89.1288 +     */
 89.1289 +    if (png_ptr == NULL) return;
 89.1290 +    png_ptr->user_width_max = user_width_max;
 89.1291 +    png_ptr->user_height_max = user_height_max;
 89.1292 +}
 89.1293 +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
 89.1294 +
 89.1295 +#endif /* ?PNG_1_0_X */
 89.1296 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    90.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    90.2 +++ b/libs/libpng/pngtrans.c	Sun Jun 07 17:25:49 2015 +0300
    90.3 @@ -0,0 +1,662 @@
    90.4 +
    90.5 +/* pngtrans.c - transforms the data in a row (used by both readers and writers)
    90.6 + *
    90.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    90.8 + * For conditions of distribution and use, see copyright notice in png.h
    90.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   90.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   90.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   90.12 + */
   90.13 +
   90.14 +#define PNG_INTERNAL
   90.15 +#include "png.h"
   90.16 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   90.17 +
   90.18 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
   90.19 +/* turn on BGR-to-RGB mapping */
   90.20 +void PNGAPI
   90.21 +png_set_bgr(png_structp png_ptr)
   90.22 +{
   90.23 +   png_debug(1, "in png_set_bgr\n");
   90.24 +   if (png_ptr == NULL) return;
   90.25 +   png_ptr->transformations |= PNG_BGR;
   90.26 +}
   90.27 +#endif
   90.28 +
   90.29 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
   90.30 +/* turn on 16 bit byte swapping */
   90.31 +void PNGAPI
   90.32 +png_set_swap(png_structp png_ptr)
   90.33 +{
   90.34 +   png_debug(1, "in png_set_swap\n");
   90.35 +   if (png_ptr == NULL) return;
   90.36 +   if (png_ptr->bit_depth == 16)
   90.37 +      png_ptr->transformations |= PNG_SWAP_BYTES;
   90.38 +}
   90.39 +#endif
   90.40 +
   90.41 +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
   90.42 +/* turn on pixel packing */
   90.43 +void PNGAPI
   90.44 +png_set_packing(png_structp png_ptr)
   90.45 +{
   90.46 +   png_debug(1, "in png_set_packing\n");
   90.47 +   if (png_ptr == NULL) return;
   90.48 +   if (png_ptr->bit_depth < 8)
   90.49 +   {
   90.50 +      png_ptr->transformations |= PNG_PACK;
   90.51 +      png_ptr->usr_bit_depth = 8;
   90.52 +   }
   90.53 +}
   90.54 +#endif
   90.55 +
   90.56 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   90.57 +/* turn on packed pixel swapping */
   90.58 +void PNGAPI
   90.59 +png_set_packswap(png_structp png_ptr)
   90.60 +{
   90.61 +   png_debug(1, "in png_set_packswap\n");
   90.62 +   if (png_ptr == NULL) return;
   90.63 +   if (png_ptr->bit_depth < 8)
   90.64 +      png_ptr->transformations |= PNG_PACKSWAP;
   90.65 +}
   90.66 +#endif
   90.67 +
   90.68 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
   90.69 +void PNGAPI
   90.70 +png_set_shift(png_structp png_ptr, png_color_8p true_bits)
   90.71 +{
   90.72 +   png_debug(1, "in png_set_shift\n");
   90.73 +   if (png_ptr == NULL) return;
   90.74 +   png_ptr->transformations |= PNG_SHIFT;
   90.75 +   png_ptr->shift = *true_bits;
   90.76 +}
   90.77 +#endif
   90.78 +
   90.79 +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
   90.80 +    defined(PNG_WRITE_INTERLACING_SUPPORTED)
   90.81 +int PNGAPI
   90.82 +png_set_interlace_handling(png_structp png_ptr)
   90.83 +{
   90.84 +   png_debug(1, "in png_set_interlace handling\n");
   90.85 +   if (png_ptr && png_ptr->interlaced)
   90.86 +   {
   90.87 +      png_ptr->transformations |= PNG_INTERLACE;
   90.88 +      return (7);
   90.89 +   }
   90.90 +
   90.91 +   return (1);
   90.92 +}
   90.93 +#endif
   90.94 +
   90.95 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
   90.96 +/* Add a filler byte on read, or remove a filler or alpha byte on write.
   90.97 + * The filler type has changed in v0.95 to allow future 2-byte fillers
   90.98 + * for 48-bit input data, as well as to avoid problems with some compilers
   90.99 + * that don't like bytes as parameters.
  90.100 + */
  90.101 +void PNGAPI
  90.102 +png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  90.103 +{
  90.104 +   png_debug(1, "in png_set_filler\n");
  90.105 +   if (png_ptr == NULL) return;
  90.106 +   png_ptr->transformations |= PNG_FILLER;
  90.107 +   png_ptr->filler = (png_byte)filler;
  90.108 +   if (filler_loc == PNG_FILLER_AFTER)
  90.109 +      png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
  90.110 +   else
  90.111 +      png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
  90.112 +
  90.113 +   /* This should probably go in the "do_read_filler" routine.
  90.114 +    * I attempted to do that in libpng-1.0.1a but that caused problems
  90.115 +    * so I restored it in libpng-1.0.2a
  90.116 +   */
  90.117 +
  90.118 +   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  90.119 +   {
  90.120 +      png_ptr->usr_channels = 4;
  90.121 +   }
  90.122 +
  90.123 +   /* Also I added this in libpng-1.0.2a (what happens when we expand
  90.124 +    * a less-than-8-bit grayscale to GA? */
  90.125 +
  90.126 +   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
  90.127 +   {
  90.128 +      png_ptr->usr_channels = 2;
  90.129 +   }
  90.130 +}
  90.131 +
  90.132 +#if !defined(PNG_1_0_X)
  90.133 +/* Added to libpng-1.2.7 */
  90.134 +void PNGAPI
  90.135 +png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  90.136 +{
  90.137 +   png_debug(1, "in png_set_add_alpha\n");
  90.138 +   if (png_ptr == NULL) return;
  90.139 +   png_set_filler(png_ptr, filler, filler_loc);
  90.140 +   png_ptr->transformations |= PNG_ADD_ALPHA;
  90.141 +}
  90.142 +#endif
  90.143 +
  90.144 +#endif
  90.145 +
  90.146 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
  90.147 +    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  90.148 +void PNGAPI
  90.149 +png_set_swap_alpha(png_structp png_ptr)
  90.150 +{
  90.151 +   png_debug(1, "in png_set_swap_alpha\n");
  90.152 +   if (png_ptr == NULL) return;
  90.153 +   png_ptr->transformations |= PNG_SWAP_ALPHA;
  90.154 +}
  90.155 +#endif
  90.156 +
  90.157 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
  90.158 +    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  90.159 +void PNGAPI
  90.160 +png_set_invert_alpha(png_structp png_ptr)
  90.161 +{
  90.162 +   png_debug(1, "in png_set_invert_alpha\n");
  90.163 +   if (png_ptr == NULL) return;
  90.164 +   png_ptr->transformations |= PNG_INVERT_ALPHA;
  90.165 +}
  90.166 +#endif
  90.167 +
  90.168 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
  90.169 +void PNGAPI
  90.170 +png_set_invert_mono(png_structp png_ptr)
  90.171 +{
  90.172 +   png_debug(1, "in png_set_invert_mono\n");
  90.173 +   if (png_ptr == NULL) return;
  90.174 +   png_ptr->transformations |= PNG_INVERT_MONO;
  90.175 +}
  90.176 +
  90.177 +/* invert monochrome grayscale data */
  90.178 +void /* PRIVATE */
  90.179 +png_do_invert(png_row_infop row_info, png_bytep row)
  90.180 +{
  90.181 +   png_debug(1, "in png_do_invert\n");
  90.182 +  /* This test removed from libpng version 1.0.13 and 1.2.0:
  90.183 +   *   if (row_info->bit_depth == 1 &&
  90.184 +   */
  90.185 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  90.186 +   if (row == NULL || row_info == NULL)
  90.187 +     return;
  90.188 +#endif
  90.189 +   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  90.190 +   {
  90.191 +      png_bytep rp = row;
  90.192 +      png_uint_32 i;
  90.193 +      png_uint_32 istop = row_info->rowbytes;
  90.194 +
  90.195 +      for (i = 0; i < istop; i++)
  90.196 +      {
  90.197 +         *rp = (png_byte)(~(*rp));
  90.198 +         rp++;
  90.199 +      }
  90.200 +   }
  90.201 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  90.202 +      row_info->bit_depth == 8)
  90.203 +   {
  90.204 +      png_bytep rp = row;
  90.205 +      png_uint_32 i;
  90.206 +      png_uint_32 istop = row_info->rowbytes;
  90.207 +
  90.208 +      for (i = 0; i < istop; i+=2)
  90.209 +      {
  90.210 +         *rp = (png_byte)(~(*rp));
  90.211 +         rp+=2;
  90.212 +      }
  90.213 +   }
  90.214 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  90.215 +      row_info->bit_depth == 16)
  90.216 +   {
  90.217 +      png_bytep rp = row;
  90.218 +      png_uint_32 i;
  90.219 +      png_uint_32 istop = row_info->rowbytes;
  90.220 +
  90.221 +      for (i = 0; i < istop; i+=4)
  90.222 +      {
  90.223 +         *rp = (png_byte)(~(*rp));
  90.224 +         *(rp+1) = (png_byte)(~(*(rp+1)));
  90.225 +         rp+=4;
  90.226 +      }
  90.227 +   }
  90.228 +}
  90.229 +#endif
  90.230 +
  90.231 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  90.232 +/* swaps byte order on 16 bit depth images */
  90.233 +void /* PRIVATE */
  90.234 +png_do_swap(png_row_infop row_info, png_bytep row)
  90.235 +{
  90.236 +   png_debug(1, "in png_do_swap\n");
  90.237 +   if (
  90.238 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  90.239 +       row != NULL && row_info != NULL &&
  90.240 +#endif
  90.241 +       row_info->bit_depth == 16)
  90.242 +   {
  90.243 +      png_bytep rp = row;
  90.244 +      png_uint_32 i;
  90.245 +      png_uint_32 istop= row_info->width * row_info->channels;
  90.246 +
  90.247 +      for (i = 0; i < istop; i++, rp += 2)
  90.248 +      {
  90.249 +         png_byte t = *rp;
  90.250 +         *rp = *(rp + 1);
  90.251 +         *(rp + 1) = t;
  90.252 +      }
  90.253 +   }
  90.254 +}
  90.255 +#endif
  90.256 +
  90.257 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  90.258 +static PNG_CONST png_byte onebppswaptable[256] = {
  90.259 +   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
  90.260 +   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  90.261 +   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
  90.262 +   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  90.263 +   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
  90.264 +   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  90.265 +   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
  90.266 +   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  90.267 +   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
  90.268 +   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  90.269 +   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
  90.270 +   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  90.271 +   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
  90.272 +   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  90.273 +   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
  90.274 +   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  90.275 +   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
  90.276 +   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  90.277 +   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
  90.278 +   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  90.279 +   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
  90.280 +   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  90.281 +   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
  90.282 +   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  90.283 +   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
  90.284 +   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  90.285 +   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
  90.286 +   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  90.287 +   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
  90.288 +   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  90.289 +   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
  90.290 +   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  90.291 +};
  90.292 +
  90.293 +static PNG_CONST png_byte twobppswaptable[256] = {
  90.294 +   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
  90.295 +   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
  90.296 +   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
  90.297 +   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
  90.298 +   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
  90.299 +   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
  90.300 +   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
  90.301 +   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
  90.302 +   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
  90.303 +   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
  90.304 +   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
  90.305 +   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
  90.306 +   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
  90.307 +   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
  90.308 +   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
  90.309 +   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
  90.310 +   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
  90.311 +   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
  90.312 +   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
  90.313 +   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
  90.314 +   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
  90.315 +   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
  90.316 +   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
  90.317 +   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
  90.318 +   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
  90.319 +   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
  90.320 +   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
  90.321 +   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
  90.322 +   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
  90.323 +   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
  90.324 +   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
  90.325 +   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
  90.326 +};
  90.327 +
  90.328 +static PNG_CONST png_byte fourbppswaptable[256] = {
  90.329 +   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  90.330 +   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
  90.331 +   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
  90.332 +   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
  90.333 +   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
  90.334 +   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
  90.335 +   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
  90.336 +   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
  90.337 +   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
  90.338 +   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
  90.339 +   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
  90.340 +   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
  90.341 +   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
  90.342 +   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
  90.343 +   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
  90.344 +   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
  90.345 +   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
  90.346 +   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
  90.347 +   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
  90.348 +   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
  90.349 +   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
  90.350 +   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
  90.351 +   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
  90.352 +   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
  90.353 +   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
  90.354 +   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
  90.355 +   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
  90.356 +   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
  90.357 +   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
  90.358 +   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
  90.359 +   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
  90.360 +   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
  90.361 +};
  90.362 +
  90.363 +/* swaps pixel packing order within bytes */
  90.364 +void /* PRIVATE */
  90.365 +png_do_packswap(png_row_infop row_info, png_bytep row)
  90.366 +{
  90.367 +   png_debug(1, "in png_do_packswap\n");
  90.368 +   if (
  90.369 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  90.370 +       row != NULL && row_info != NULL &&
  90.371 +#endif
  90.372 +       row_info->bit_depth < 8)
  90.373 +   {
  90.374 +      png_bytep rp, end, table;
  90.375 +
  90.376 +      end = row + row_info->rowbytes;
  90.377 +
  90.378 +      if (row_info->bit_depth == 1)
  90.379 +         table = (png_bytep)onebppswaptable;
  90.380 +      else if (row_info->bit_depth == 2)
  90.381 +         table = (png_bytep)twobppswaptable;
  90.382 +      else if (row_info->bit_depth == 4)
  90.383 +         table = (png_bytep)fourbppswaptable;
  90.384 +      else
  90.385 +         return;
  90.386 +
  90.387 +      for (rp = row; rp < end; rp++)
  90.388 +         *rp = table[*rp];
  90.389 +   }
  90.390 +}
  90.391 +#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
  90.392 +
  90.393 +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
  90.394 +    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  90.395 +/* remove filler or alpha byte(s) */
  90.396 +void /* PRIVATE */
  90.397 +png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
  90.398 +{
  90.399 +   png_debug(1, "in png_do_strip_filler\n");
  90.400 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  90.401 +   if (row != NULL && row_info != NULL)
  90.402 +#endif
  90.403 +   {
  90.404 +      png_bytep sp=row;
  90.405 +      png_bytep dp=row;
  90.406 +      png_uint_32 row_width=row_info->width;
  90.407 +      png_uint_32 i;
  90.408 +
  90.409 +      if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||
  90.410 +         (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  90.411 +         (flags & PNG_FLAG_STRIP_ALPHA))) &&
  90.412 +         row_info->channels == 4)
  90.413 +      {
  90.414 +         if (row_info->bit_depth == 8)
  90.415 +         {
  90.416 +            /* This converts from RGBX or RGBA to RGB */
  90.417 +            if (flags & PNG_FLAG_FILLER_AFTER)
  90.418 +            {
  90.419 +               dp+=3; sp+=4;
  90.420 +               for (i = 1; i < row_width; i++)
  90.421 +               {
  90.422 +                  *dp++ = *sp++;
  90.423 +                  *dp++ = *sp++;
  90.424 +                  *dp++ = *sp++;
  90.425 +                  sp++;
  90.426 +               }
  90.427 +            }
  90.428 +            /* This converts from XRGB or ARGB to RGB */
  90.429 +            else
  90.430 +            {
  90.431 +               for (i = 0; i < row_width; i++)
  90.432 +               {
  90.433 +                  sp++;
  90.434 +                  *dp++ = *sp++;
  90.435 +                  *dp++ = *sp++;
  90.436 +                  *dp++ = *sp++;
  90.437 +               }
  90.438 +            }
  90.439 +            row_info->pixel_depth = 24;
  90.440 +            row_info->rowbytes = row_width * 3;
  90.441 +         }
  90.442 +         else /* if (row_info->bit_depth == 16) */
  90.443 +         {
  90.444 +            if (flags & PNG_FLAG_FILLER_AFTER)
  90.445 +            {
  90.446 +               /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */
  90.447 +               sp += 8; dp += 6;
  90.448 +               for (i = 1; i < row_width; i++)
  90.449 +               {
  90.450 +                  /* This could be (although png_memcpy is probably slower):
  90.451 +                  png_memcpy(dp, sp, 6);
  90.452 +                  sp += 8;
  90.453 +                  dp += 6;
  90.454 +                  */
  90.455 +
  90.456 +                  *dp++ = *sp++;
  90.457 +                  *dp++ = *sp++;
  90.458 +                  *dp++ = *sp++;
  90.459 +                  *dp++ = *sp++;
  90.460 +                  *dp++ = *sp++;
  90.461 +                  *dp++ = *sp++;
  90.462 +                  sp += 2;
  90.463 +               }
  90.464 +            }
  90.465 +            else
  90.466 +            {
  90.467 +               /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */
  90.468 +               for (i = 0; i < row_width; i++)
  90.469 +               {
  90.470 +                  /* This could be (although png_memcpy is probably slower):
  90.471 +                  png_memcpy(dp, sp, 6);
  90.472 +                  sp += 8;
  90.473 +                  dp += 6;
  90.474 +                  */
  90.475 +
  90.476 +                  sp+=2;
  90.477 +                  *dp++ = *sp++;
  90.478 +                  *dp++ = *sp++;
  90.479 +                  *dp++ = *sp++;
  90.480 +                  *dp++ = *sp++;
  90.481 +                  *dp++ = *sp++;
  90.482 +                  *dp++ = *sp++;
  90.483 +               }
  90.484 +            }
  90.485 +            row_info->pixel_depth = 48;
  90.486 +            row_info->rowbytes = row_width * 6;
  90.487 +         }
  90.488 +         row_info->channels = 3;
  90.489 +      }
  90.490 +      else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||
  90.491 +         (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  90.492 +         (flags & PNG_FLAG_STRIP_ALPHA))) &&
  90.493 +          row_info->channels == 2)
  90.494 +      {
  90.495 +         if (row_info->bit_depth == 8)
  90.496 +         {
  90.497 +            /* This converts from GX or GA to G */
  90.498 +            if (flags & PNG_FLAG_FILLER_AFTER)
  90.499 +            {
  90.500 +               for (i = 0; i < row_width; i++)
  90.501 +               {
  90.502 +                  *dp++ = *sp++;
  90.503 +                  sp++;
  90.504 +               }
  90.505 +            }
  90.506 +            /* This converts from XG or AG to G */
  90.507 +            else
  90.508 +            {
  90.509 +               for (i = 0; i < row_width; i++)
  90.510 +               {
  90.511 +                  sp++;
  90.512 +                  *dp++ = *sp++;
  90.513 +               }
  90.514 +            }
  90.515 +            row_info->pixel_depth = 8;
  90.516 +            row_info->rowbytes = row_width;
  90.517 +         }
  90.518 +         else /* if (row_info->bit_depth == 16) */
  90.519 +         {
  90.520 +            if (flags & PNG_FLAG_FILLER_AFTER)
  90.521 +            {
  90.522 +               /* This converts from GGXX or GGAA to GG */
  90.523 +               sp += 4; dp += 2;
  90.524 +               for (i = 1; i < row_width; i++)
  90.525 +               {
  90.526 +                  *dp++ = *sp++;
  90.527 +                  *dp++ = *sp++;
  90.528 +                  sp += 2;
  90.529 +               }
  90.530 +            }
  90.531 +            else
  90.532 +            {
  90.533 +               /* This converts from XXGG or AAGG to GG */
  90.534 +               for (i = 0; i < row_width; i++)
  90.535 +               {
  90.536 +                  sp += 2;
  90.537 +                  *dp++ = *sp++;
  90.538 +                  *dp++ = *sp++;
  90.539 +               }
  90.540 +            }
  90.541 +            row_info->pixel_depth = 16;
  90.542 +            row_info->rowbytes = row_width * 2;
  90.543 +         }
  90.544 +         row_info->channels = 1;
  90.545 +      }
  90.546 +      if (flags & PNG_FLAG_STRIP_ALPHA)
  90.547 +        row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  90.548 +   }
  90.549 +}
  90.550 +#endif
  90.551 +
  90.552 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  90.553 +/* swaps red and blue bytes within a pixel */
  90.554 +void /* PRIVATE */
  90.555 +png_do_bgr(png_row_infop row_info, png_bytep row)
  90.556 +{
  90.557 +   png_debug(1, "in png_do_bgr\n");
  90.558 +   if (
  90.559 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  90.560 +       row != NULL && row_info != NULL &&
  90.561 +#endif
  90.562 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
  90.563 +   {
  90.564 +      png_uint_32 row_width = row_info->width;
  90.565 +      if (row_info->bit_depth == 8)
  90.566 +      {
  90.567 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  90.568 +         {
  90.569 +            png_bytep rp;
  90.570 +            png_uint_32 i;
  90.571 +
  90.572 +            for (i = 0, rp = row; i < row_width; i++, rp += 3)
  90.573 +            {
  90.574 +               png_byte save = *rp;
  90.575 +               *rp = *(rp + 2);
  90.576 +               *(rp + 2) = save;
  90.577 +            }
  90.578 +         }
  90.579 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  90.580 +         {
  90.581 +            png_bytep rp;
  90.582 +            png_uint_32 i;
  90.583 +
  90.584 +            for (i = 0, rp = row; i < row_width; i++, rp += 4)
  90.585 +            {
  90.586 +               png_byte save = *rp;
  90.587 +               *rp = *(rp + 2);
  90.588 +               *(rp + 2) = save;
  90.589 +            }
  90.590 +         }
  90.591 +      }
  90.592 +      else if (row_info->bit_depth == 16)
  90.593 +      {
  90.594 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  90.595 +         {
  90.596 +            png_bytep rp;
  90.597 +            png_uint_32 i;
  90.598 +
  90.599 +            for (i = 0, rp = row; i < row_width; i++, rp += 6)
  90.600 +            {
  90.601 +               png_byte save = *rp;
  90.602 +               *rp = *(rp + 4);
  90.603 +               *(rp + 4) = save;
  90.604 +               save = *(rp + 1);
  90.605 +               *(rp + 1) = *(rp + 5);
  90.606 +               *(rp + 5) = save;
  90.607 +            }
  90.608 +         }
  90.609 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  90.610 +         {
  90.611 +            png_bytep rp;
  90.612 +            png_uint_32 i;
  90.613 +
  90.614 +            for (i = 0, rp = row; i < row_width; i++, rp += 8)
  90.615 +            {
  90.616 +               png_byte save = *rp;
  90.617 +               *rp = *(rp + 4);
  90.618 +               *(rp + 4) = save;
  90.619 +               save = *(rp + 1);
  90.620 +               *(rp + 1) = *(rp + 5);
  90.621 +               *(rp + 5) = save;
  90.622 +            }
  90.623 +         }
  90.624 +      }
  90.625 +   }
  90.626 +}
  90.627 +#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
  90.628 +
  90.629 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  90.630 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  90.631 +    defined(PNG_LEGACY_SUPPORTED)
  90.632 +void PNGAPI
  90.633 +png_set_user_transform_info(png_structp png_ptr, png_voidp
  90.634 +   user_transform_ptr, int user_transform_depth, int user_transform_channels)
  90.635 +{
  90.636 +   png_debug(1, "in png_set_user_transform_info\n");
  90.637 +   if (png_ptr == NULL) return;
  90.638 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  90.639 +   png_ptr->user_transform_ptr = user_transform_ptr;
  90.640 +   png_ptr->user_transform_depth = (png_byte)user_transform_depth;
  90.641 +   png_ptr->user_transform_channels = (png_byte)user_transform_channels;
  90.642 +#else
  90.643 +   if (user_transform_ptr || user_transform_depth || user_transform_channels)
  90.644 +      png_warning(png_ptr,
  90.645 +        "This version of libpng does not support user transform info");
  90.646 +#endif
  90.647 +}
  90.648 +#endif
  90.649 +
  90.650 +/* This function returns a pointer to the user_transform_ptr associated with
  90.651 + * the user transform functions.  The application should free any memory
  90.652 + * associated with this pointer before png_write_destroy and png_read_destroy
  90.653 + * are called.
  90.654 + */
  90.655 +png_voidp PNGAPI
  90.656 +png_get_user_transform_ptr(png_structp png_ptr)
  90.657 +{
  90.658 +   if (png_ptr == NULL) return (NULL);
  90.659 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  90.660 +   return ((png_voidp)png_ptr->user_transform_ptr);
  90.661 +#else
  90.662 +   return (NULL);
  90.663 +#endif
  90.664 +}
  90.665 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    91.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    91.2 +++ b/libs/libpng/pngvcrd.c	Sun Jun 07 17:25:49 2015 +0300
    91.3 @@ -0,0 +1,1 @@
    91.4 +/* pnggvrd.c was removed from libpng-1.2.20. */
    92.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    92.2 +++ b/libs/libpng/pngwio.c	Sun Jun 07 17:25:49 2015 +0300
    92.3 @@ -0,0 +1,234 @@
    92.4 +
    92.5 +/* pngwio.c - functions for data output
    92.6 + *
    92.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    92.8 + * For conditions of distribution and use, see copyright notice in png.h
    92.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   92.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   92.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   92.12 + *
   92.13 + * This file provides a location for all output.  Users who need
   92.14 + * special handling are expected to write functions that have the same
   92.15 + * arguments as these and perform similar functions, but that possibly
   92.16 + * use different output methods.  Note that you shouldn't change these
   92.17 + * functions, but rather write replacement functions and then change
   92.18 + * them at run time with png_set_write_fn(...).
   92.19 + */
   92.20 +
   92.21 +#define PNG_INTERNAL
   92.22 +#include "png.h"
   92.23 +#ifdef PNG_WRITE_SUPPORTED
   92.24 +
   92.25 +/* Write the data to whatever output you are using.  The default routine
   92.26 +   writes to a file pointer.  Note that this routine sometimes gets called
   92.27 +   with very small lengths, so you should implement some kind of simple
   92.28 +   buffering if you are using unbuffered writes.  This should never be asked
   92.29 +   to write more than 64K on a 16 bit machine.  */
   92.30 +
   92.31 +void /* PRIVATE */
   92.32 +png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   92.33 +{
   92.34 +   if (png_ptr->write_data_fn != NULL )
   92.35 +      (*(png_ptr->write_data_fn))(png_ptr, data, length);
   92.36 +   else
   92.37 +      png_error(png_ptr, "Call to NULL write function");
   92.38 +}
   92.39 +
   92.40 +#if !defined(PNG_NO_STDIO)
   92.41 +/* This is the function that does the actual writing of data.  If you are
   92.42 +   not writing to a standard C stream, you should create a replacement
   92.43 +   write_data function and use it at run time with png_set_write_fn(), rather
   92.44 +   than changing the library. */
   92.45 +#ifndef USE_FAR_KEYWORD
   92.46 +void PNGAPI
   92.47 +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   92.48 +{
   92.49 +   png_uint_32 check;
   92.50 +
   92.51 +   if (png_ptr == NULL) return;
   92.52 +#if defined(_WIN32_WCE)
   92.53 +   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   92.54 +      check = 0;
   92.55 +#else
   92.56 +   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
   92.57 +#endif
   92.58 +   if (check != length)
   92.59 +      png_error(png_ptr, "Write Error");
   92.60 +}
   92.61 +#else
   92.62 +/* this is the model-independent version. Since the standard I/O library
   92.63 +   can't handle far buffers in the medium and small models, we have to copy
   92.64 +   the data.
   92.65 +*/
   92.66 +
   92.67 +#define NEAR_BUF_SIZE 1024
   92.68 +#define MIN(a,b) (a <= b ? a : b)
   92.69 +
   92.70 +void PNGAPI
   92.71 +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   92.72 +{
   92.73 +   png_uint_32 check;
   92.74 +   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   92.75 +   png_FILE_p io_ptr;
   92.76 +
   92.77 +   if (png_ptr == NULL) return;
   92.78 +   /* Check if data really is near. If so, use usual code. */
   92.79 +   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   92.80 +   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   92.81 +   if ((png_bytep)near_data == data)
   92.82 +   {
   92.83 +#if defined(_WIN32_WCE)
   92.84 +      if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
   92.85 +         check = 0;
   92.86 +#else
   92.87 +      check = fwrite(near_data, 1, length, io_ptr);
   92.88 +#endif
   92.89 +   }
   92.90 +   else
   92.91 +   {
   92.92 +      png_byte buf[NEAR_BUF_SIZE];
   92.93 +      png_size_t written, remaining, err;
   92.94 +      check = 0;
   92.95 +      remaining = length;
   92.96 +      do
   92.97 +      {
   92.98 +         written = MIN(NEAR_BUF_SIZE, remaining);
   92.99 +         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
  92.100 +#if defined(_WIN32_WCE)
  92.101 +         if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
  92.102 +            err = 0;
  92.103 +#else
  92.104 +         err = fwrite(buf, 1, written, io_ptr);
  92.105 +#endif
  92.106 +         if (err != written)
  92.107 +            break;
  92.108 +         else
  92.109 +            check += err;
  92.110 +         data += written;
  92.111 +         remaining -= written;
  92.112 +      }
  92.113 +      while (remaining != 0);
  92.114 +   }
  92.115 +   if (check != length)
  92.116 +      png_error(png_ptr, "Write Error");
  92.117 +}
  92.118 +
  92.119 +#endif
  92.120 +#endif
  92.121 +
  92.122 +/* This function is called to output any data pending writing (normally
  92.123 +   to disk).  After png_flush is called, there should be no data pending
  92.124 +   writing in any buffers. */
  92.125 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  92.126 +void /* PRIVATE */
  92.127 +png_flush(png_structp png_ptr)
  92.128 +{
  92.129 +   if (png_ptr->output_flush_fn != NULL)
  92.130 +      (*(png_ptr->output_flush_fn))(png_ptr);
  92.131 +}
  92.132 +
  92.133 +#if !defined(PNG_NO_STDIO)
  92.134 +void PNGAPI
  92.135 +png_default_flush(png_structp png_ptr)
  92.136 +{
  92.137 +#if !defined(_WIN32_WCE)
  92.138 +   png_FILE_p io_ptr;
  92.139 +#endif
  92.140 +   if (png_ptr == NULL) return;
  92.141 +#if !defined(_WIN32_WCE)
  92.142 +   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
  92.143 +   if (io_ptr != NULL)
  92.144 +      fflush(io_ptr);
  92.145 +#endif
  92.146 +}
  92.147 +#endif
  92.148 +#endif
  92.149 +
  92.150 +/* This function allows the application to supply new output functions for
  92.151 +   libpng if standard C streams aren't being used.
  92.152 +
  92.153 +   This function takes as its arguments:
  92.154 +   png_ptr       - pointer to a png output data structure
  92.155 +   io_ptr        - pointer to user supplied structure containing info about
  92.156 +                   the output functions.  May be NULL.
  92.157 +   write_data_fn - pointer to a new output function that takes as its
  92.158 +                   arguments a pointer to a png_struct, a pointer to
  92.159 +                   data to be written, and a 32-bit unsigned int that is
  92.160 +                   the number of bytes to be written.  The new write
  92.161 +                   function should call png_error(png_ptr, "Error msg")
  92.162 +                   to exit and output any fatal error messages.
  92.163 +   flush_data_fn - pointer to a new flush function that takes as its
  92.164 +                   arguments a pointer to a png_struct.  After a call to
  92.165 +                   the flush function, there should be no data in any buffers
  92.166 +                   or pending transmission.  If the output method doesn't do
  92.167 +                   any buffering of ouput, a function prototype must still be
  92.168 +                   supplied although it doesn't have to do anything.  If
  92.169 +                   PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
  92.170 +                   time, output_flush_fn will be ignored, although it must be
  92.171 +                   supplied for compatibility. */
  92.172 +void PNGAPI
  92.173 +png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
  92.174 +   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
  92.175 +{
  92.176 +   if (png_ptr == NULL) return;
  92.177 +   png_ptr->io_ptr = io_ptr;
  92.178 +
  92.179 +#if !defined(PNG_NO_STDIO)
  92.180 +   if (write_data_fn != NULL)
  92.181 +      png_ptr->write_data_fn = write_data_fn;
  92.182 +   else
  92.183 +      png_ptr->write_data_fn = png_default_write_data;
  92.184 +#else
  92.185 +   png_ptr->write_data_fn = write_data_fn;
  92.186 +#endif
  92.187 +
  92.188 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  92.189 +#if !defined(PNG_NO_STDIO)
  92.190 +   if (output_flush_fn != NULL)
  92.191 +      png_ptr->output_flush_fn = output_flush_fn;
  92.192 +   else
  92.193 +      png_ptr->output_flush_fn = png_default_flush;
  92.194 +#else
  92.195 +   png_ptr->output_flush_fn = output_flush_fn;
  92.196 +#endif
  92.197 +#endif /* PNG_WRITE_FLUSH_SUPPORTED */
  92.198 +
  92.199 +   /* It is an error to read while writing a png file */
  92.200 +   if (png_ptr->read_data_fn != NULL)
  92.201 +   {
  92.202 +      png_ptr->read_data_fn = NULL;
  92.203 +      png_warning(png_ptr,
  92.204 +         "Attempted to set both read_data_fn and write_data_fn in");
  92.205 +      png_warning(png_ptr,
  92.206 +         "the same structure.  Resetting read_data_fn to NULL.");
  92.207 +   }
  92.208 +}
  92.209 +
  92.210 +#if defined(USE_FAR_KEYWORD)
  92.211 +#if defined(_MSC_VER)
  92.212 +void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  92.213 +{
  92.214 +   void *near_ptr;
  92.215 +   void FAR *far_ptr;
  92.216 +   FP_OFF(near_ptr) = FP_OFF(ptr);
  92.217 +   far_ptr = (void FAR *)near_ptr;
  92.218 +   if (check != 0)
  92.219 +      if (FP_SEG(ptr) != FP_SEG(far_ptr))
  92.220 +         png_error(png_ptr, "segment lost in conversion");
  92.221 +   return(near_ptr);
  92.222 +}
  92.223 +#  else
  92.224 +void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  92.225 +{
  92.226 +   void *near_ptr;
  92.227 +   void FAR *far_ptr;
  92.228 +   near_ptr = (void FAR *)ptr;
  92.229 +   far_ptr = (void FAR *)near_ptr;
  92.230 +   if (check != 0)
  92.231 +      if (far_ptr != ptr)
  92.232 +         png_error(png_ptr, "segment lost in conversion");
  92.233 +   return(near_ptr);
  92.234 +}
  92.235 +#   endif
  92.236 +#   endif
  92.237 +#endif /* PNG_WRITE_SUPPORTED */
    93.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    93.2 +++ b/libs/libpng/pngwrite.c	Sun Jun 07 17:25:49 2015 +0300
    93.3 @@ -0,0 +1,1547 @@
    93.4 +
    93.5 +/* pngwrite.c - general routines to write a PNG file
    93.6 + *
    93.7 + * Last changed in libpng 1.2.31 [August 19, 2008]
    93.8 + * For conditions of distribution and use, see copyright notice in png.h
    93.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   93.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   93.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   93.12 + */
   93.13 +
   93.14 +/* get internal access to png.h */
   93.15 +#define PNG_INTERNAL
   93.16 +#include "png.h"
   93.17 +#ifdef PNG_WRITE_SUPPORTED
   93.18 +
   93.19 +/* Writes all the PNG information.  This is the suggested way to use the
   93.20 + * library.  If you have a new chunk to add, make a function to write it,
   93.21 + * and put it in the correct location here.  If you want the chunk written
   93.22 + * after the image data, put it in png_write_end().  I strongly encourage
   93.23 + * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
   93.24 + * the chunk, as that will keep the code from breaking if you want to just
   93.25 + * write a plain PNG file.  If you have long comments, I suggest writing
   93.26 + * them in png_write_end(), and compressing them.
   93.27 + */
   93.28 +void PNGAPI
   93.29 +png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
   93.30 +{
   93.31 +   png_debug(1, "in png_write_info_before_PLTE\n");
   93.32 +   if (png_ptr == NULL || info_ptr == NULL)
   93.33 +      return;
   93.34 +   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
   93.35 +   {
   93.36 +   png_write_sig(png_ptr); /* write PNG signature */
   93.37 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
   93.38 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
   93.39 +   {
   93.40 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
   93.41 +      png_ptr->mng_features_permitted=0;
   93.42 +   }
   93.43 +#endif
   93.44 +   /* write IHDR information. */
   93.45 +   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
   93.46 +      info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
   93.47 +      info_ptr->filter_type,
   93.48 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   93.49 +      info_ptr->interlace_type);
   93.50 +#else
   93.51 +      0);
   93.52 +#endif
   93.53 +   /* the rest of these check to see if the valid field has the appropriate
   93.54 +      flag set, and if it does, writes the chunk. */
   93.55 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
   93.56 +   if (info_ptr->valid & PNG_INFO_gAMA)
   93.57 +   {
   93.58 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
   93.59 +      png_write_gAMA(png_ptr, info_ptr->gamma);
   93.60 +#else
   93.61 +#ifdef PNG_FIXED_POINT_SUPPORTED
   93.62 +      png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
   93.63 +#  endif
   93.64 +#endif
   93.65 +   }
   93.66 +#endif
   93.67 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
   93.68 +   if (info_ptr->valid & PNG_INFO_sRGB)
   93.69 +      png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
   93.70 +#endif
   93.71 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
   93.72 +   if (info_ptr->valid & PNG_INFO_iCCP)
   93.73 +      png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
   93.74 +                     info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
   93.75 +#endif
   93.76 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
   93.77 +   if (info_ptr->valid & PNG_INFO_sBIT)
   93.78 +      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
   93.79 +#endif
   93.80 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
   93.81 +   if (info_ptr->valid & PNG_INFO_cHRM)
   93.82 +   {
   93.83 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   93.84 +      png_write_cHRM(png_ptr,
   93.85 +         info_ptr->x_white, info_ptr->y_white,
   93.86 +         info_ptr->x_red, info_ptr->y_red,
   93.87 +         info_ptr->x_green, info_ptr->y_green,
   93.88 +         info_ptr->x_blue, info_ptr->y_blue);
   93.89 +#else
   93.90 +#  ifdef PNG_FIXED_POINT_SUPPORTED
   93.91 +      png_write_cHRM_fixed(png_ptr,
   93.92 +         info_ptr->int_x_white, info_ptr->int_y_white,
   93.93 +         info_ptr->int_x_red, info_ptr->int_y_red,
   93.94 +         info_ptr->int_x_green, info_ptr->int_y_green,
   93.95 +         info_ptr->int_x_blue, info_ptr->int_y_blue);
   93.96 +#  endif
   93.97 +#endif
   93.98 +   }
   93.99 +#endif
  93.100 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  93.101 +   if (info_ptr->unknown_chunks_num)
  93.102 +   {
  93.103 +       png_unknown_chunk *up;
  93.104 +
  93.105 +       png_debug(5, "writing extra chunks\n");
  93.106 +
  93.107 +       for (up = info_ptr->unknown_chunks;
  93.108 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  93.109 +            up++)
  93.110 +       {
  93.111 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  93.112 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  93.113 +            up->location && !(up->location & PNG_HAVE_PLTE) &&
  93.114 +            !(up->location & PNG_HAVE_IDAT) &&
  93.115 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  93.116 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  93.117 +         {
  93.118 +            if (up->size == 0)
  93.119 +               png_warning(png_ptr, "Writing zero-length unknown chunk");
  93.120 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  93.121 +         }
  93.122 +       }
  93.123 +   }
  93.124 +#endif
  93.125 +      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
  93.126 +   }
  93.127 +}
  93.128 +
  93.129 +void PNGAPI
  93.130 +png_write_info(png_structp png_ptr, png_infop info_ptr)
  93.131 +{
  93.132 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  93.133 +   int i;
  93.134 +#endif
  93.135 +
  93.136 +   png_debug(1, "in png_write_info\n");
  93.137 +
  93.138 +   if (png_ptr == NULL || info_ptr == NULL)
  93.139 +      return;
  93.140 +
  93.141 +   png_write_info_before_PLTE(png_ptr, info_ptr);
  93.142 +
  93.143 +   if (info_ptr->valid & PNG_INFO_PLTE)
  93.144 +      png_write_PLTE(png_ptr, info_ptr->palette,
  93.145 +         (png_uint_32)info_ptr->num_palette);
  93.146 +   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  93.147 +      png_error(png_ptr, "Valid palette required for paletted images");
  93.148 +
  93.149 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
  93.150 +   if (info_ptr->valid & PNG_INFO_tRNS)
  93.151 +      {
  93.152 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  93.153 +         /* invert the alpha channel (in tRNS) */
  93.154 +         if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
  93.155 +            info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  93.156 +         {
  93.157 +            int j;
  93.158 +            for (j=0; j<(int)info_ptr->num_trans; j++)
  93.159 +               info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
  93.160 +         }
  93.161 +#endif
  93.162 +      png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
  93.163 +         info_ptr->num_trans, info_ptr->color_type);
  93.164 +      }
  93.165 +#endif
  93.166 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
  93.167 +   if (info_ptr->valid & PNG_INFO_bKGD)
  93.168 +      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
  93.169 +#endif
  93.170 +#if defined(PNG_WRITE_hIST_SUPPORTED)
  93.171 +   if (info_ptr->valid & PNG_INFO_hIST)
  93.172 +      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
  93.173 +#endif
  93.174 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
  93.175 +   if (info_ptr->valid & PNG_INFO_oFFs)
  93.176 +      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
  93.177 +         info_ptr->offset_unit_type);
  93.178 +#endif
  93.179 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
  93.180 +   if (info_ptr->valid & PNG_INFO_pCAL)
  93.181 +      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
  93.182 +         info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
  93.183 +         info_ptr->pcal_units, info_ptr->pcal_params);
  93.184 +#endif
  93.185 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
  93.186 +   if (info_ptr->valid & PNG_INFO_sCAL)
  93.187 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
  93.188 +      png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
  93.189 +          info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
  93.190 +#else
  93.191 +#ifdef PNG_FIXED_POINT_SUPPORTED
  93.192 +      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
  93.193 +          info_ptr->scal_s_width, info_ptr->scal_s_height);
  93.194 +#else
  93.195 +      png_warning(png_ptr,
  93.196 +          "png_write_sCAL not supported; sCAL chunk not written.");
  93.197 +#endif
  93.198 +#endif
  93.199 +#endif
  93.200 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
  93.201 +   if (info_ptr->valid & PNG_INFO_pHYs)
  93.202 +      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
  93.203 +         info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
  93.204 +#endif
  93.205 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  93.206 +   if (info_ptr->valid & PNG_INFO_tIME)
  93.207 +   {
  93.208 +      png_write_tIME(png_ptr, &(info_ptr->mod_time));
  93.209 +      png_ptr->mode |= PNG_WROTE_tIME;
  93.210 +   }
  93.211 +#endif
  93.212 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
  93.213 +   if (info_ptr->valid & PNG_INFO_sPLT)
  93.214 +     for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  93.215 +       png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
  93.216 +#endif
  93.217 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  93.218 +   /* Check to see if we need to write text chunks */
  93.219 +   for (i = 0; i < info_ptr->num_text; i++)
  93.220 +   {
  93.221 +      png_debug2(2, "Writing header text chunk %d, type %d\n", i,
  93.222 +         info_ptr->text[i].compression);
  93.223 +      /* an internationalized chunk? */
  93.224 +      if (info_ptr->text[i].compression > 0)
  93.225 +      {
  93.226 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
  93.227 +          /* write international chunk */
  93.228 +          png_write_iTXt(png_ptr,
  93.229 +                         info_ptr->text[i].compression,
  93.230 +                         info_ptr->text[i].key,
  93.231 +                         info_ptr->text[i].lang,
  93.232 +                         info_ptr->text[i].lang_key,
  93.233 +                         info_ptr->text[i].text);
  93.234 +#else
  93.235 +          png_warning(png_ptr, "Unable to write international text");
  93.236 +#endif
  93.237 +          /* Mark this chunk as written */
  93.238 +          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  93.239 +      }
  93.240 +      /* If we want a compressed text chunk */
  93.241 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
  93.242 +      {
  93.243 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
  93.244 +         /* write compressed chunk */
  93.245 +         png_write_zTXt(png_ptr, info_ptr->text[i].key,
  93.246 +            info_ptr->text[i].text, 0,
  93.247 +            info_ptr->text[i].compression);
  93.248 +#else
  93.249 +         png_warning(png_ptr, "Unable to write compressed text");
  93.250 +#endif
  93.251 +         /* Mark this chunk as written */
  93.252 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  93.253 +      }
  93.254 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  93.255 +      {
  93.256 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
  93.257 +         /* write uncompressed chunk */
  93.258 +         png_write_tEXt(png_ptr, info_ptr->text[i].key,
  93.259 +                         info_ptr->text[i].text,
  93.260 +                         0);
  93.261 +#else
  93.262 +         png_warning(png_ptr, "Unable to write uncompressed text");
  93.263 +#endif
  93.264 +         /* Mark this chunk as written */
  93.265 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  93.266 +      }
  93.267 +   }
  93.268 +#endif
  93.269 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  93.270 +   if (info_ptr->unknown_chunks_num)
  93.271 +   {
  93.272 +       png_unknown_chunk *up;
  93.273 +
  93.274 +       png_debug(5, "writing extra chunks\n");
  93.275 +
  93.276 +       for (up = info_ptr->unknown_chunks;
  93.277 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  93.278 +            up++)
  93.279 +       {
  93.280 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  93.281 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  93.282 +            up->location && (up->location & PNG_HAVE_PLTE) &&
  93.283 +            !(up->location & PNG_HAVE_IDAT) &&
  93.284 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  93.285 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  93.286 +         {
  93.287 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  93.288 +         }
  93.289 +       }
  93.290 +   }
  93.291 +#endif
  93.292 +}
  93.293 +
  93.294 +/* Writes the end of the PNG file.  If you don't want to write comments or
  93.295 + * time information, you can pass NULL for info.  If you already wrote these
  93.296 + * in png_write_info(), do not write them again here.  If you have long
  93.297 + * comments, I suggest writing them here, and compressing them.
  93.298 + */
  93.299 +void PNGAPI
  93.300 +png_write_end(png_structp png_ptr, png_infop info_ptr)
  93.301 +{
  93.302 +   png_debug(1, "in png_write_end\n");
  93.303 +   if (png_ptr == NULL)
  93.304 +      return;
  93.305 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
  93.306 +      png_error(png_ptr, "No IDATs written into file");
  93.307 +
  93.308 +   /* see if user wants us to write information chunks */
  93.309 +   if (info_ptr != NULL)
  93.310 +   {
  93.311 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  93.312 +      int i; /* local index variable */
  93.313 +#endif
  93.314 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  93.315 +      /* check to see if user has supplied a time chunk */
  93.316 +      if ((info_ptr->valid & PNG_INFO_tIME) &&
  93.317 +         !(png_ptr->mode & PNG_WROTE_tIME))
  93.318 +         png_write_tIME(png_ptr, &(info_ptr->mod_time));
  93.319 +#endif
  93.320 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  93.321 +      /* loop through comment chunks */
  93.322 +      for (i = 0; i < info_ptr->num_text; i++)
  93.323 +      {
  93.324 +         png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
  93.325 +            info_ptr->text[i].compression);
  93.326 +         /* an internationalized chunk? */
  93.327 +         if (info_ptr->text[i].compression > 0)
  93.328 +         {
  93.329 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
  93.330 +             /* write international chunk */
  93.331 +             png_write_iTXt(png_ptr,
  93.332 +                         info_ptr->text[i].compression,
  93.333 +                         info_ptr->text[i].key,
  93.334 +                         info_ptr->text[i].lang,
  93.335 +                         info_ptr->text[i].lang_key,
  93.336 +                         info_ptr->text[i].text);
  93.337 +#else
  93.338 +             png_warning(png_ptr, "Unable to write international text");
  93.339 +#endif
  93.340 +             /* Mark this chunk as written */
  93.341 +             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  93.342 +         }
  93.343 +         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
  93.344 +         {
  93.345 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
  93.346 +            /* write compressed chunk */
  93.347 +            png_write_zTXt(png_ptr, info_ptr->text[i].key,
  93.348 +               info_ptr->text[i].text, 0,
  93.349 +               info_ptr->text[i].compression);
  93.350 +#else
  93.351 +            png_warning(png_ptr, "Unable to write compressed text");
  93.352 +#endif
  93.353 +            /* Mark this chunk as written */
  93.354 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  93.355 +         }
  93.356 +         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  93.357 +         {
  93.358 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
  93.359 +            /* write uncompressed chunk */
  93.360 +            png_write_tEXt(png_ptr, info_ptr->text[i].key,
  93.361 +               info_ptr->text[i].text, 0);
  93.362 +#else
  93.363 +            png_warning(png_ptr, "Unable to write uncompressed text");
  93.364 +#endif
  93.365 +
  93.366 +            /* Mark this chunk as written */
  93.367 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  93.368 +         }
  93.369 +      }
  93.370 +#endif
  93.371 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  93.372 +   if (info_ptr->unknown_chunks_num)
  93.373 +   {
  93.374 +       png_unknown_chunk *up;
  93.375 +
  93.376 +       png_debug(5, "writing extra chunks\n");
  93.377 +
  93.378 +       for (up = info_ptr->unknown_chunks;
  93.379 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  93.380 +            up++)
  93.381 +       {
  93.382 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  93.383 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  93.384 +            up->location && (up->location & PNG_AFTER_IDAT) &&
  93.385 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  93.386 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  93.387 +         {
  93.388 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  93.389 +         }
  93.390 +       }
  93.391 +   }
  93.392 +#endif
  93.393 +   }
  93.394 +
  93.395 +   png_ptr->mode |= PNG_AFTER_IDAT;
  93.396 +
  93.397 +   /* write end of PNG file */
  93.398 +   png_write_IEND(png_ptr);
  93.399 +   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
  93.400 +    * and restored again in libpng-1.2.30, may cause some applications that
  93.401 +    * do not set png_ptr->output_flush_fn to crash.  If your application
  93.402 +    * experiences a problem, please try building libpng with
  93.403 +    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
  93.404 +    * png-mng-implement at lists.sf.net .  This kludge will be removed
  93.405 +    * from libpng-1.4.0.
  93.406 +    */
  93.407 +#if defined(PNG_WRITE_FLUSH_SUPPORTED) && \
  93.408 +    defined(PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED)
  93.409 +   png_flush(png_ptr);
  93.410 +#endif
  93.411 +}
  93.412 +
  93.413 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  93.414 +#if !defined(_WIN32_WCE)
  93.415 +/* "time.h" functions are not supported on WindowsCE */
  93.416 +void PNGAPI
  93.417 +png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
  93.418 +{
  93.419 +   png_debug(1, "in png_convert_from_struct_tm\n");
  93.420 +   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  93.421 +   ptime->month = (png_byte)(ttime->tm_mon + 1);
  93.422 +   ptime->day = (png_byte)ttime->tm_mday;
  93.423 +   ptime->hour = (png_byte)ttime->tm_hour;
  93.424 +   ptime->minute = (png_byte)ttime->tm_min;
  93.425 +   ptime->second = (png_byte)ttime->tm_sec;
  93.426 +}
  93.427 +
  93.428 +void PNGAPI
  93.429 +png_convert_from_time_t(png_timep ptime, time_t ttime)
  93.430 +{
  93.431 +   struct tm *tbuf;
  93.432 +
  93.433 +   png_debug(1, "in png_convert_from_time_t\n");
  93.434 +   tbuf = gmtime(&ttime);
  93.435 +   png_convert_from_struct_tm(ptime, tbuf);
  93.436 +}
  93.437 +#endif
  93.438 +#endif
  93.439 +
  93.440 +/* Initialize png_ptr structure, and allocate any memory needed */
  93.441 +png_structp PNGAPI
  93.442 +png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
  93.443 +   png_error_ptr error_fn, png_error_ptr warn_fn)
  93.444 +{
  93.445 +#ifdef PNG_USER_MEM_SUPPORTED
  93.446 +   return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  93.447 +      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
  93.448 +}
  93.449 +
  93.450 +/* Alternate initialize png_ptr structure, and allocate any memory needed */
  93.451 +png_structp PNGAPI
  93.452 +png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
  93.453 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  93.454 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  93.455 +{
  93.456 +#endif /* PNG_USER_MEM_SUPPORTED */
  93.457 +#ifdef PNG_SETJMP_SUPPORTED
  93.458 +    volatile
  93.459 +#endif
  93.460 +    png_structp png_ptr;
  93.461 +#ifdef PNG_SETJMP_SUPPORTED
  93.462 +#ifdef USE_FAR_KEYWORD
  93.463 +   jmp_buf jmpbuf;
  93.464 +#endif
  93.465 +#endif
  93.466 +   int i;
  93.467 +   png_debug(1, "in png_create_write_struct\n");
  93.468 +#ifdef PNG_USER_MEM_SUPPORTED
  93.469 +   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  93.470 +      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  93.471 +#else
  93.472 +   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  93.473 +#endif /* PNG_USER_MEM_SUPPORTED */
  93.474 +   if (png_ptr == NULL)
  93.475 +      return (NULL);
  93.476 +
  93.477 +   /* added at libpng-1.2.6 */
  93.478 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  93.479 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  93.480 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  93.481 +#endif
  93.482 +
  93.483 +#ifdef PNG_SETJMP_SUPPORTED
  93.484 +#ifdef USE_FAR_KEYWORD
  93.485 +   if (setjmp(jmpbuf))
  93.486 +#else
  93.487 +   if (setjmp(png_ptr->jmpbuf))
  93.488 +#endif
  93.489 +   {
  93.490 +      png_free(png_ptr, png_ptr->zbuf);
  93.491 +       png_ptr->zbuf=NULL;
  93.492 +      png_destroy_struct(png_ptr);
  93.493 +      return (NULL);
  93.494 +   }
  93.495 +#ifdef USE_FAR_KEYWORD
  93.496 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  93.497 +#endif
  93.498 +#endif
  93.499 +
  93.500 +#ifdef PNG_USER_MEM_SUPPORTED
  93.501 +   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  93.502 +#endif /* PNG_USER_MEM_SUPPORTED */
  93.503 +   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  93.504 +
  93.505 +   if (user_png_ver)
  93.506 +   {
  93.507 +     i=0;
  93.508 +     do
  93.509 +     {
  93.510 +       if (user_png_ver[i] != png_libpng_ver[i])
  93.511 +          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  93.512 +     } while (png_libpng_ver[i++]);
  93.513 +   }
  93.514 +
  93.515 +   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  93.516 +   {
  93.517 +     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  93.518 +      * we must recompile any applications that use any older library version.
  93.519 +      * For versions after libpng 1.0, we will be compatible, so we need
  93.520 +      * only check the first digit.
  93.521 +      */
  93.522 +     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  93.523 +         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  93.524 +         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  93.525 +     {
  93.526 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  93.527 +        char msg[80];
  93.528 +        if (user_png_ver)
  93.529 +        {
  93.530 +          png_snprintf(msg, 80,
  93.531 +             "Application was compiled with png.h from libpng-%.20s",
  93.532 +             user_png_ver);
  93.533 +          png_warning(png_ptr, msg);
  93.534 +        }
  93.535 +        png_snprintf(msg, 80,
  93.536 +           "Application  is  running with png.c from libpng-%.20s",
  93.537 +           png_libpng_ver);
  93.538 +        png_warning(png_ptr, msg);
  93.539 +#endif
  93.540 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  93.541 +        png_ptr->flags=0;
  93.542 +#endif
  93.543 +        png_error(png_ptr,
  93.544 +           "Incompatible libpng version in application and library");
  93.545 +     }
  93.546 +   }
  93.547 +
  93.548 +   /* initialize zbuf - compression buffer */
  93.549 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  93.550 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  93.551 +      (png_uint_32)png_ptr->zbuf_size);
  93.552 +
  93.553 +   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  93.554 +      png_flush_ptr_NULL);
  93.555 +
  93.556 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  93.557 +   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  93.558 +      1, png_doublep_NULL, png_doublep_NULL);
  93.559 +#endif
  93.560 +
  93.561 +#ifdef PNG_SETJMP_SUPPORTED
  93.562 +/* Applications that neglect to set up their own setjmp() and then encounter
  93.563 +   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
  93.564 +   abort instead of returning. */
  93.565 +#ifdef USE_FAR_KEYWORD
  93.566 +   if (setjmp(jmpbuf))
  93.567 +      PNG_ABORT();
  93.568 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  93.569 +#else
  93.570 +   if (setjmp(png_ptr->jmpbuf))
  93.571 +      PNG_ABORT();
  93.572 +#endif
  93.573 +#endif
  93.574 +   return (png_ptr);
  93.575 +}
  93.576 +
  93.577 +/* Initialize png_ptr structure, and allocate any memory needed */
  93.578 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  93.579 +/* Deprecated. */
  93.580 +#undef png_write_init
  93.581 +void PNGAPI
  93.582 +png_write_init(png_structp png_ptr)
  93.583 +{
  93.584 +   /* We only come here via pre-1.0.7-compiled applications */
  93.585 +   png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  93.586 +}
  93.587 +
  93.588 +void PNGAPI
  93.589 +png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  93.590 +   png_size_t png_struct_size, png_size_t png_info_size)
  93.591 +{
  93.592 +   /* We only come here via pre-1.0.12-compiled applications */
  93.593 +   if (png_ptr == NULL) return;
  93.594 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  93.595 +   if (png_sizeof(png_struct) > png_struct_size ||
  93.596 +      png_sizeof(png_info) > png_info_size)
  93.597 +   {
  93.598 +      char msg[80];
  93.599 +      png_ptr->warning_fn=NULL;
  93.600 +      if (user_png_ver)
  93.601 +      {
  93.602 +        png_snprintf(msg, 80,
  93.603 +           "Application was compiled with png.h from libpng-%.20s",
  93.604 +           user_png_ver);
  93.605 +        png_warning(png_ptr, msg);
  93.606 +      }
  93.607 +      png_snprintf(msg, 80,
  93.608 +         "Application  is  running with png.c from libpng-%.20s",
  93.609 +         png_libpng_ver);
  93.610 +      png_warning(png_ptr, msg);
  93.611 +   }
  93.612 +#endif
  93.613 +   if (png_sizeof(png_struct) > png_struct_size)
  93.614 +     {
  93.615 +       png_ptr->error_fn=NULL;
  93.616 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  93.617 +       png_ptr->flags=0;
  93.618 +#endif
  93.619 +       png_error(png_ptr,
  93.620 +       "The png struct allocated by the application for writing is too small.");
  93.621 +     }
  93.622 +   if (png_sizeof(png_info) > png_info_size)
  93.623 +     {
  93.624 +       png_ptr->error_fn=NULL;
  93.625 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  93.626 +       png_ptr->flags=0;
  93.627 +#endif
  93.628 +       png_error(png_ptr,
  93.629 +       "The info struct allocated by the application for writing is too small.");
  93.630 +     }
  93.631 +   png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
  93.632 +}
  93.633 +#endif /* PNG_1_0_X || PNG_1_2_X */
  93.634 +
  93.635 +
  93.636 +void PNGAPI
  93.637 +png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  93.638 +   png_size_t png_struct_size)
  93.639 +{
  93.640 +   png_structp png_ptr=*ptr_ptr;
  93.641 +#ifdef PNG_SETJMP_SUPPORTED
  93.642 +   jmp_buf tmp_jmp; /* to save current jump buffer */
  93.643 +#endif
  93.644 +
  93.645 +   int i = 0;
  93.646 +
  93.647 +   if (png_ptr == NULL)
  93.648 +      return;
  93.649 +
  93.650 +   do
  93.651 +   {
  93.652 +     if (user_png_ver[i] != png_libpng_ver[i])
  93.653 +     {
  93.654 +#ifdef PNG_LEGACY_SUPPORTED
  93.655 +       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  93.656 +#else
  93.657 +       png_ptr->warning_fn=NULL;
  93.658 +       png_warning(png_ptr,
  93.659 + "Application uses deprecated png_write_init() and should be recompiled.");
  93.660 +       break;
  93.661 +#endif
  93.662 +     }
  93.663 +   } while (png_libpng_ver[i++]);
  93.664 +
  93.665 +   png_debug(1, "in png_write_init_3\n");
  93.666 +
  93.667 +#ifdef PNG_SETJMP_SUPPORTED
  93.668 +   /* save jump buffer and error functions */
  93.669 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  93.670 +#endif
  93.671 +
  93.672 +   if (png_sizeof(png_struct) > png_struct_size)
  93.673 +     {
  93.674 +       png_destroy_struct(png_ptr);
  93.675 +       png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  93.676 +       *ptr_ptr = png_ptr;
  93.677 +     }
  93.678 +
  93.679 +   /* reset all variables to 0 */
  93.680 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
  93.681 +
  93.682 +   /* added at libpng-1.2.6 */
  93.683 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  93.684 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  93.685 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  93.686 +#endif
  93.687 +
  93.688 +#ifdef PNG_SETJMP_SUPPORTED
  93.689 +   /* restore jump buffer */
  93.690 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  93.691 +#endif
  93.692 +
  93.693 +   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  93.694 +      png_flush_ptr_NULL);
  93.695 +
  93.696 +   /* initialize zbuf - compression buffer */
  93.697 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  93.698 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  93.699 +      (png_uint_32)png_ptr->zbuf_size);
  93.700 +
  93.701 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  93.702 +   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  93.703 +      1, png_doublep_NULL, png_doublep_NULL);
  93.704 +#endif
  93.705 +}
  93.706 +
  93.707 +/* Write a few rows of image data.  If the image is interlaced,
  93.708 + * either you will have to write the 7 sub images, or, if you
  93.709 + * have called png_set_interlace_handling(), you will have to
  93.710 + * "write" the image seven times.
  93.711 + */
  93.712 +void PNGAPI
  93.713 +png_write_rows(png_structp png_ptr, png_bytepp row,
  93.714 +   png_uint_32 num_rows)
  93.715 +{
  93.716 +   png_uint_32 i; /* row counter */
  93.717 +   png_bytepp rp; /* row pointer */
  93.718 +
  93.719 +   png_debug(1, "in png_write_rows\n");
  93.720 +
  93.721 +   if (png_ptr == NULL)
  93.722 +      return;
  93.723 +
  93.724 +   /* loop through the rows */
  93.725 +   for (i = 0, rp = row; i < num_rows; i++, rp++)
  93.726 +   {
  93.727 +      png_write_row(png_ptr, *rp);
  93.728 +   }
  93.729 +}
  93.730 +
  93.731 +/* Write the image.  You only need to call this function once, even
  93.732 + * if you are writing an interlaced image.
  93.733 + */
  93.734 +void PNGAPI
  93.735 +png_write_image(png_structp png_ptr, png_bytepp image)
  93.736 +{
  93.737 +   png_uint_32 i; /* row index */
  93.738 +   int pass, num_pass; /* pass variables */
  93.739 +   png_bytepp rp; /* points to current row */
  93.740 +
  93.741 +   if (png_ptr == NULL)
  93.742 +      return;
  93.743 +
  93.744 +   png_debug(1, "in png_write_image\n");
  93.745 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  93.746 +   /* intialize interlace handling.  If image is not interlaced,
  93.747 +      this will set pass to 1 */
  93.748 +   num_pass = png_set_interlace_handling(png_ptr);
  93.749 +#else
  93.750 +   num_pass = 1;
  93.751 +#endif
  93.752 +   /* loop through passes */
  93.753 +   for (pass = 0; pass < num_pass; pass++)
  93.754 +   {
  93.755 +      /* loop through image */
  93.756 +      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  93.757 +      {
  93.758 +         png_write_row(png_ptr, *rp);
  93.759 +      }
  93.760 +   }
  93.761 +}
  93.762 +
  93.763 +/* called by user to write a row of image data */
  93.764 +void PNGAPI
  93.765 +png_write_row(png_structp png_ptr, png_bytep row)
  93.766 +{
  93.767 +   if (png_ptr == NULL)
  93.768 +      return;
  93.769 +   png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
  93.770 +      png_ptr->row_number, png_ptr->pass);
  93.771 +
  93.772 +   /* initialize transformations and other stuff if first time */
  93.773 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  93.774 +   {
  93.775 +   /* make sure we wrote the header info */
  93.776 +   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  93.777 +      png_error(png_ptr,
  93.778 +         "png_write_info was never called before png_write_row.");
  93.779 +
  93.780 +   /* check for transforms that have been set but were defined out */
  93.781 +#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  93.782 +   if (png_ptr->transformations & PNG_INVERT_MONO)
  93.783 +      png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
  93.784 +#endif
  93.785 +#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  93.786 +   if (png_ptr->transformations & PNG_FILLER)
  93.787 +      png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
  93.788 +#endif
  93.789 +#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
  93.790 +   if (png_ptr->transformations & PNG_PACKSWAP)
  93.791 +      png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
  93.792 +#endif
  93.793 +#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  93.794 +   if (png_ptr->transformations & PNG_PACK)
  93.795 +      png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
  93.796 +#endif
  93.797 +#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  93.798 +   if (png_ptr->transformations & PNG_SHIFT)
  93.799 +      png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
  93.800 +#endif
  93.801 +#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  93.802 +   if (png_ptr->transformations & PNG_BGR)
  93.803 +      png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
  93.804 +#endif
  93.805 +#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  93.806 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
  93.807 +      png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
  93.808 +#endif
  93.809 +
  93.810 +      png_write_start_row(png_ptr);
  93.811 +   }
  93.812 +
  93.813 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  93.814 +   /* if interlaced and not interested in row, return */
  93.815 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  93.816 +   {
  93.817 +      switch (png_ptr->pass)
  93.818 +      {
  93.819 +         case 0:
  93.820 +            if (png_ptr->row_number & 0x07)
  93.821 +            {
  93.822 +               png_write_finish_row(png_ptr);
  93.823 +               return;
  93.824 +            }
  93.825 +            break;
  93.826 +         case 1:
  93.827 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  93.828 +            {
  93.829 +               png_write_finish_row(png_ptr);
  93.830 +               return;
  93.831 +            }
  93.832 +            break;
  93.833 +         case 2:
  93.834 +            if ((png_ptr->row_number & 0x07) != 4)
  93.835 +            {
  93.836 +               png_write_finish_row(png_ptr);
  93.837 +               return;
  93.838 +            }
  93.839 +            break;
  93.840 +         case 3:
  93.841 +            if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  93.842 +            {
  93.843 +               png_write_finish_row(png_ptr);
  93.844 +               return;
  93.845 +            }
  93.846 +            break;
  93.847 +         case 4:
  93.848 +            if ((png_ptr->row_number & 0x03) != 2)
  93.849 +            {
  93.850 +               png_write_finish_row(png_ptr);
  93.851 +               return;
  93.852 +            }
  93.853 +            break;
  93.854 +         case 5:
  93.855 +            if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  93.856 +            {
  93.857 +               png_write_finish_row(png_ptr);
  93.858 +               return;
  93.859 +            }
  93.860 +            break;
  93.861 +         case 6:
  93.862 +            if (!(png_ptr->row_number & 0x01))
  93.863 +            {
  93.864 +               png_write_finish_row(png_ptr);
  93.865 +               return;
  93.866 +            }
  93.867 +            break;
  93.868 +      }
  93.869 +   }
  93.870 +#endif
  93.871 +
  93.872 +   /* set up row info for transformations */
  93.873 +   png_ptr->row_info.color_type = png_ptr->color_type;
  93.874 +   png_ptr->row_info.width = png_ptr->usr_width;
  93.875 +   png_ptr->row_info.channels = png_ptr->usr_channels;
  93.876 +   png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
  93.877 +   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  93.878 +      png_ptr->row_info.channels);
  93.879 +
  93.880 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  93.881 +      png_ptr->row_info.width);
  93.882 +
  93.883 +   png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
  93.884 +   png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
  93.885 +   png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
  93.886 +   png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
  93.887 +   png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
  93.888 +   png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
  93.889 +
  93.890 +   /* Copy user's row into buffer, leaving room for filter byte. */
  93.891 +   png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
  93.892 +      png_ptr->row_info.rowbytes);
  93.893 +
  93.894 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  93.895 +   /* handle interlacing */
  93.896 +   if (png_ptr->interlaced && png_ptr->pass < 6 &&
  93.897 +      (png_ptr->transformations & PNG_INTERLACE))
  93.898 +   {
  93.899 +      png_do_write_interlace(&(png_ptr->row_info),
  93.900 +         png_ptr->row_buf + 1, png_ptr->pass);
  93.901 +      /* this should always get caught above, but still ... */
  93.902 +      if (!(png_ptr->row_info.width))
  93.903 +      {
  93.904 +         png_write_finish_row(png_ptr);
  93.905 +         return;
  93.906 +      }
  93.907 +   }
  93.908 +#endif
  93.909 +
  93.910 +   /* handle other transformations */
  93.911 +   if (png_ptr->transformations)
  93.912 +      png_do_write_transformations(png_ptr);
  93.913 +
  93.914 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  93.915 +   /* Write filter_method 64 (intrapixel differencing) only if
  93.916 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  93.917 +    * 2. Libpng did not write a PNG signature (this filter_method is only
  93.918 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  93.919 +    * 3. The application called png_permit_mng_features with a mask that
  93.920 +    *    included PNG_FLAG_MNG_FILTER_64 and
  93.921 +    * 4. The filter_method is 64 and
  93.922 +    * 5. The color_type is RGB or RGBA
  93.923 +    */
  93.924 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  93.925 +      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  93.926 +   {
  93.927 +      /* Intrapixel differencing */
  93.928 +      png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  93.929 +   }
  93.930 +#endif
  93.931 +
  93.932 +   /* Find a filter if necessary, filter the row and write it out. */
  93.933 +   png_write_find_filter(png_ptr, &(png_ptr->row_info));
  93.934 +
  93.935 +   if (png_ptr->write_row_fn != NULL)
  93.936 +      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  93.937 +}
  93.938 +
  93.939 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  93.940 +/* Set the automatic flush interval or 0 to turn flushing off */
  93.941 +void PNGAPI
  93.942 +png_set_flush(png_structp png_ptr, int nrows)
  93.943 +{
  93.944 +   png_debug(1, "in png_set_flush\n");
  93.945 +   if (png_ptr == NULL)
  93.946 +      return;
  93.947 +   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  93.948 +}
  93.949 +
  93.950 +/* flush the current output buffers now */
  93.951 +void PNGAPI
  93.952 +png_write_flush(png_structp png_ptr)
  93.953 +{
  93.954 +   int wrote_IDAT;
  93.955 +
  93.956 +   png_debug(1, "in png_write_flush\n");
  93.957 +   if (png_ptr == NULL)
  93.958 +      return;
  93.959 +   /* We have already written out all of the data */
  93.960 +   if (png_ptr->row_number >= png_ptr->num_rows)
  93.961 +     return;
  93.962 +
  93.963 +   do
  93.964 +   {
  93.965 +      int ret;
  93.966 +
  93.967 +      /* compress the data */
  93.968 +      ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  93.969 +      wrote_IDAT = 0;
  93.970 +
  93.971 +      /* check for compression errors */
  93.972 +      if (ret != Z_OK)
  93.973 +      {
  93.974 +         if (png_ptr->zstream.msg != NULL)
  93.975 +            png_error(png_ptr, png_ptr->zstream.msg);
  93.976 +         else
  93.977 +            png_error(png_ptr, "zlib error");
  93.978 +      }
  93.979 +
  93.980 +      if (!(png_ptr->zstream.avail_out))
  93.981 +      {
  93.982 +         /* write the IDAT and reset the zlib output buffer */
  93.983 +         png_write_IDAT(png_ptr, png_ptr->zbuf,
  93.984 +                        png_ptr->zbuf_size);
  93.985 +         png_ptr->zstream.next_out = png_ptr->zbuf;
  93.986 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  93.987 +         wrote_IDAT = 1;
  93.988 +      }
  93.989 +   } while(wrote_IDAT == 1);
  93.990 +
  93.991 +   /* If there is any data left to be output, write it into a new IDAT */
  93.992 +   if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  93.993 +   {
  93.994 +      /* write the IDAT and reset the zlib output buffer */
  93.995 +      png_write_IDAT(png_ptr, png_ptr->zbuf,
  93.996 +                     png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  93.997 +      png_ptr->zstream.next_out = png_ptr->zbuf;
  93.998 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  93.999 +   }
 93.1000 +   png_ptr->flush_rows = 0;
 93.1001 +   png_flush(png_ptr);
 93.1002 +}
 93.1003 +#endif /* PNG_WRITE_FLUSH_SUPPORTED */
 93.1004 +
 93.1005 +/* free all memory used by the write */
 93.1006 +void PNGAPI
 93.1007 +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
 93.1008 +{
 93.1009 +   png_structp png_ptr = NULL;
 93.1010 +   png_infop info_ptr = NULL;
 93.1011 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1012 +   png_free_ptr free_fn = NULL;
 93.1013 +   png_voidp mem_ptr = NULL;
 93.1014 +#endif
 93.1015 +
 93.1016 +   png_debug(1, "in png_destroy_write_struct\n");
 93.1017 +   if (png_ptr_ptr != NULL)
 93.1018 +   {
 93.1019 +      png_ptr = *png_ptr_ptr;
 93.1020 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1021 +      free_fn = png_ptr->free_fn;
 93.1022 +      mem_ptr = png_ptr->mem_ptr;
 93.1023 +#endif
 93.1024 +   }
 93.1025 +
 93.1026 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1027 +   if (png_ptr != NULL)
 93.1028 +   {
 93.1029 +      free_fn = png_ptr->free_fn;
 93.1030 +      mem_ptr = png_ptr->mem_ptr;
 93.1031 +   }
 93.1032 +#endif
 93.1033 +
 93.1034 +   if (info_ptr_ptr != NULL)
 93.1035 +      info_ptr = *info_ptr_ptr;
 93.1036 +
 93.1037 +   if (info_ptr != NULL)
 93.1038 +   {
 93.1039 +      if (png_ptr != NULL)
 93.1040 +      {
 93.1041 +        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
 93.1042 +
 93.1043 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 93.1044 +        if (png_ptr->num_chunk_list)
 93.1045 +        {
 93.1046 +           png_free(png_ptr, png_ptr->chunk_list);
 93.1047 +           png_ptr->chunk_list=NULL;
 93.1048 +           png_ptr->num_chunk_list = 0;
 93.1049 +        }
 93.1050 +#endif
 93.1051 +      }
 93.1052 +
 93.1053 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1054 +      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
 93.1055 +         (png_voidp)mem_ptr);
 93.1056 +#else
 93.1057 +      png_destroy_struct((png_voidp)info_ptr);
 93.1058 +#endif
 93.1059 +      *info_ptr_ptr = NULL;
 93.1060 +   }
 93.1061 +
 93.1062 +   if (png_ptr != NULL)
 93.1063 +   {
 93.1064 +      png_write_destroy(png_ptr);
 93.1065 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1066 +      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
 93.1067 +         (png_voidp)mem_ptr);
 93.1068 +#else
 93.1069 +      png_destroy_struct((png_voidp)png_ptr);
 93.1070 +#endif
 93.1071 +      *png_ptr_ptr = NULL;
 93.1072 +   }
 93.1073 +}
 93.1074 +
 93.1075 +
 93.1076 +/* Free any memory used in png_ptr struct (old method) */
 93.1077 +void /* PRIVATE */
 93.1078 +png_write_destroy(png_structp png_ptr)
 93.1079 +{
 93.1080 +#ifdef PNG_SETJMP_SUPPORTED
 93.1081 +   jmp_buf tmp_jmp; /* save jump buffer */
 93.1082 +#endif
 93.1083 +   png_error_ptr error_fn;
 93.1084 +   png_error_ptr warning_fn;
 93.1085 +   png_voidp error_ptr;
 93.1086 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1087 +   png_free_ptr free_fn;
 93.1088 +#endif
 93.1089 +
 93.1090 +   png_debug(1, "in png_write_destroy\n");
 93.1091 +   /* free any memory zlib uses */
 93.1092 +   deflateEnd(&png_ptr->zstream);
 93.1093 +
 93.1094 +   /* free our memory.  png_free checks NULL for us. */
 93.1095 +   png_free(png_ptr, png_ptr->zbuf);
 93.1096 +   png_free(png_ptr, png_ptr->row_buf);
 93.1097 +#ifndef PNG_NO_WRITE_FILTER
 93.1098 +   png_free(png_ptr, png_ptr->prev_row);
 93.1099 +   png_free(png_ptr, png_ptr->sub_row);
 93.1100 +   png_free(png_ptr, png_ptr->up_row);
 93.1101 +   png_free(png_ptr, png_ptr->avg_row);
 93.1102 +   png_free(png_ptr, png_ptr->paeth_row);
 93.1103 +#endif
 93.1104 +
 93.1105 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 93.1106 +   png_free(png_ptr, png_ptr->time_buffer);
 93.1107 +#endif
 93.1108 +
 93.1109 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 93.1110 +   png_free(png_ptr, png_ptr->prev_filters);
 93.1111 +   png_free(png_ptr, png_ptr->filter_weights);
 93.1112 +   png_free(png_ptr, png_ptr->inv_filter_weights);
 93.1113 +   png_free(png_ptr, png_ptr->filter_costs);
 93.1114 +   png_free(png_ptr, png_ptr->inv_filter_costs);
 93.1115 +#endif
 93.1116 +
 93.1117 +#ifdef PNG_SETJMP_SUPPORTED
 93.1118 +   /* reset structure */
 93.1119 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
 93.1120 +#endif
 93.1121 +
 93.1122 +   error_fn = png_ptr->error_fn;
 93.1123 +   warning_fn = png_ptr->warning_fn;
 93.1124 +   error_ptr = png_ptr->error_ptr;
 93.1125 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1126 +   free_fn = png_ptr->free_fn;
 93.1127 +#endif
 93.1128 +
 93.1129 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
 93.1130 +
 93.1131 +   png_ptr->error_fn = error_fn;
 93.1132 +   png_ptr->warning_fn = warning_fn;
 93.1133 +   png_ptr->error_ptr = error_ptr;
 93.1134 +#ifdef PNG_USER_MEM_SUPPORTED
 93.1135 +   png_ptr->free_fn = free_fn;
 93.1136 +#endif
 93.1137 +
 93.1138 +#ifdef PNG_SETJMP_SUPPORTED
 93.1139 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
 93.1140 +#endif
 93.1141 +}
 93.1142 +
 93.1143 +/* Allow the application to select one or more row filters to use. */
 93.1144 +void PNGAPI
 93.1145 +png_set_filter(png_structp png_ptr, int method, int filters)
 93.1146 +{
 93.1147 +   png_debug(1, "in png_set_filter\n");
 93.1148 +   if (png_ptr == NULL)
 93.1149 +      return;
 93.1150 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 93.1151 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
 93.1152 +      (method == PNG_INTRAPIXEL_DIFFERENCING))
 93.1153 +         method = PNG_FILTER_TYPE_BASE;
 93.1154 +#endif
 93.1155 +   if (method == PNG_FILTER_TYPE_BASE)
 93.1156 +   {
 93.1157 +      switch (filters & (PNG_ALL_FILTERS | 0x07))
 93.1158 +      {
 93.1159 +#ifndef PNG_NO_WRITE_FILTER
 93.1160 +         case 5:
 93.1161 +         case 6:
 93.1162 +         case 7: png_warning(png_ptr, "Unknown row filter for method 0");
 93.1163 +#endif /* PNG_NO_WRITE_FILTER */
 93.1164 +         case PNG_FILTER_VALUE_NONE:
 93.1165 +              png_ptr->do_filter=PNG_FILTER_NONE; break;
 93.1166 +#ifndef PNG_NO_WRITE_FILTER
 93.1167 +         case PNG_FILTER_VALUE_SUB:
 93.1168 +              png_ptr->do_filter=PNG_FILTER_SUB; break;
 93.1169 +         case PNG_FILTER_VALUE_UP:
 93.1170 +              png_ptr->do_filter=PNG_FILTER_UP; break;
 93.1171 +         case PNG_FILTER_VALUE_AVG:
 93.1172 +              png_ptr->do_filter=PNG_FILTER_AVG; break;
 93.1173 +         case PNG_FILTER_VALUE_PAETH:
 93.1174 +              png_ptr->do_filter=PNG_FILTER_PAETH; break;
 93.1175 +         default: png_ptr->do_filter = (png_byte)filters; break;
 93.1176 +#else
 93.1177 +         default: png_warning(png_ptr, "Unknown row filter for method 0");
 93.1178 +#endif /* PNG_NO_WRITE_FILTER */
 93.1179 +      }
 93.1180 +
 93.1181 +      /* If we have allocated the row_buf, this means we have already started
 93.1182 +       * with the image and we should have allocated all of the filter buffers
 93.1183 +       * that have been selected.  If prev_row isn't already allocated, then
 93.1184 +       * it is too late to start using the filters that need it, since we
 93.1185 +       * will be missing the data in the previous row.  If an application
 93.1186 +       * wants to start and stop using particular filters during compression,
 93.1187 +       * it should start out with all of the filters, and then add and
 93.1188 +       * remove them after the start of compression.
 93.1189 +       */
 93.1190 +      if (png_ptr->row_buf != NULL)
 93.1191 +      {
 93.1192 +#ifndef PNG_NO_WRITE_FILTER
 93.1193 +         if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
 93.1194 +         {
 93.1195 +            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
 93.1196 +              (png_ptr->rowbytes + 1));
 93.1197 +            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
 93.1198 +         }
 93.1199 +
 93.1200 +         if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
 93.1201 +         {
 93.1202 +            if (png_ptr->prev_row == NULL)
 93.1203 +            {
 93.1204 +               png_warning(png_ptr, "Can't add Up filter after starting");
 93.1205 +               png_ptr->do_filter &= ~PNG_FILTER_UP;
 93.1206 +            }
 93.1207 +            else
 93.1208 +            {
 93.1209 +               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
 93.1210 +                  (png_ptr->rowbytes + 1));
 93.1211 +               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
 93.1212 +            }
 93.1213 +         }
 93.1214 +
 93.1215 +         if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
 93.1216 +         {
 93.1217 +            if (png_ptr->prev_row == NULL)
 93.1218 +            {
 93.1219 +               png_warning(png_ptr, "Can't add Average filter after starting");
 93.1220 +               png_ptr->do_filter &= ~PNG_FILTER_AVG;
 93.1221 +            }
 93.1222 +            else
 93.1223 +            {
 93.1224 +               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
 93.1225 +                  (png_ptr->rowbytes + 1));
 93.1226 +               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
 93.1227 +            }
 93.1228 +         }
 93.1229 +
 93.1230 +         if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
 93.1231 +             png_ptr->paeth_row == NULL)
 93.1232 +         {
 93.1233 +            if (png_ptr->prev_row == NULL)
 93.1234 +            {
 93.1235 +               png_warning(png_ptr, "Can't add Paeth filter after starting");
 93.1236 +               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
 93.1237 +            }
 93.1238 +            else
 93.1239 +            {
 93.1240 +               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
 93.1241 +                  (png_ptr->rowbytes + 1));
 93.1242 +               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
 93.1243 +            }
 93.1244 +         }
 93.1245 +
 93.1246 +         if (png_ptr->do_filter == PNG_NO_FILTERS)
 93.1247 +#endif /* PNG_NO_WRITE_FILTER */
 93.1248 +            png_ptr->do_filter = PNG_FILTER_NONE;
 93.1249 +      }
 93.1250 +   }
 93.1251 +   else
 93.1252 +      png_error(png_ptr, "Unknown custom filter method");
 93.1253 +}
 93.1254 +
 93.1255 +/* This allows us to influence the way in which libpng chooses the "best"
 93.1256 + * filter for the current scanline.  While the "minimum-sum-of-absolute-
 93.1257 + * differences metric is relatively fast and effective, there is some
 93.1258 + * question as to whether it can be improved upon by trying to keep the
 93.1259 + * filtered data going to zlib more consistent, hopefully resulting in
 93.1260 + * better compression.
 93.1261 + */
 93.1262 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)      /* GRR 970116 */
 93.1263 +void PNGAPI
 93.1264 +png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
 93.1265 +   int num_weights, png_doublep filter_weights,
 93.1266 +   png_doublep filter_costs)
 93.1267 +{
 93.1268 +   int i;
 93.1269 +
 93.1270 +   png_debug(1, "in png_set_filter_heuristics\n");
 93.1271 +   if (png_ptr == NULL)
 93.1272 +      return;
 93.1273 +   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
 93.1274 +   {
 93.1275 +      png_warning(png_ptr, "Unknown filter heuristic method");
 93.1276 +      return;
 93.1277 +   }
 93.1278 +
 93.1279 +   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
 93.1280 +   {
 93.1281 +      heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
 93.1282 +   }
 93.1283 +
 93.1284 +   if (num_weights < 0 || filter_weights == NULL ||
 93.1285 +      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
 93.1286 +   {
 93.1287 +      num_weights = 0;
 93.1288 +   }
 93.1289 +
 93.1290 +   png_ptr->num_prev_filters = (png_byte)num_weights;
 93.1291 +   png_ptr->heuristic_method = (png_byte)heuristic_method;
 93.1292 +
 93.1293 +   if (num_weights > 0)
 93.1294 +   {
 93.1295 +      if (png_ptr->prev_filters == NULL)
 93.1296 +      {
 93.1297 +         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
 93.1298 +            (png_uint_32)(png_sizeof(png_byte) * num_weights));
 93.1299 +
 93.1300 +         /* To make sure that the weighting starts out fairly */
 93.1301 +         for (i = 0; i < num_weights; i++)
 93.1302 +         {
 93.1303 +            png_ptr->prev_filters[i] = 255;
 93.1304 +         }
 93.1305 +      }
 93.1306 +
 93.1307 +      if (png_ptr->filter_weights == NULL)
 93.1308 +      {
 93.1309 +         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
 93.1310 +            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
 93.1311 +
 93.1312 +         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
 93.1313 +            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
 93.1314 +         for (i = 0; i < num_weights; i++)
 93.1315 +         {
 93.1316 +            png_ptr->inv_filter_weights[i] =
 93.1317 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
 93.1318 +         }
 93.1319 +      }
 93.1320 +
 93.1321 +      for (i = 0; i < num_weights; i++)
 93.1322 +      {
 93.1323 +         if (filter_weights[i] < 0.0)
 93.1324 +         {
 93.1325 +            png_ptr->inv_filter_weights[i] =
 93.1326 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
 93.1327 +         }
 93.1328 +         else
 93.1329 +         {
 93.1330 +            png_ptr->inv_filter_weights[i] =
 93.1331 +               (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
 93.1332 +            png_ptr->filter_weights[i] =
 93.1333 +               (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
 93.1334 +         }
 93.1335 +      }
 93.1336 +   }
 93.1337 +
 93.1338 +   /* If, in the future, there are other filter methods, this would
 93.1339 +    * need to be based on png_ptr->filter.
 93.1340 +    */
 93.1341 +   if (png_ptr->filter_costs == NULL)
 93.1342 +   {
 93.1343 +      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
 93.1344 +         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 93.1345 +
 93.1346 +      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
 93.1347 +         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 93.1348 +
 93.1349 +      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
 93.1350 +      {
 93.1351 +         png_ptr->inv_filter_costs[i] =
 93.1352 +         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
 93.1353 +      }
 93.1354 +   }
 93.1355 +
 93.1356 +   /* Here is where we set the relative costs of the different filters.  We
 93.1357 +    * should take the desired compression level into account when setting
 93.1358 +    * the costs, so that Paeth, for instance, has a high relative cost at low
 93.1359 +    * compression levels, while it has a lower relative cost at higher
 93.1360 +    * compression settings.  The filter types are in order of increasing
 93.1361 +    * relative cost, so it would be possible to do this with an algorithm.
 93.1362 +    */
 93.1363 +   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
 93.1364 +   {
 93.1365 +      if (filter_costs == NULL || filter_costs[i] < 0.0)
 93.1366 +      {
 93.1367 +         png_ptr->inv_filter_costs[i] =
 93.1368 +         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
 93.1369 +      }
 93.1370 +      else if (filter_costs[i] >= 1.0)
 93.1371 +      {
 93.1372 +         png_ptr->inv_filter_costs[i] =
 93.1373 +            (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
 93.1374 +         png_ptr->filter_costs[i] =
 93.1375 +            (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
 93.1376 +      }
 93.1377 +   }
 93.1378 +}
 93.1379 +#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
 93.1380 +
 93.1381 +void PNGAPI
 93.1382 +png_set_compression_level(png_structp png_ptr, int level)
 93.1383 +{
 93.1384 +   png_debug(1, "in png_set_compression_level\n");
 93.1385 +   if (png_ptr == NULL)
 93.1386 +      return;
 93.1387 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
 93.1388 +   png_ptr->zlib_level = level;
 93.1389 +}
 93.1390 +
 93.1391 +void PNGAPI
 93.1392 +png_set_compression_mem_level(png_structp png_ptr, int mem_level)
 93.1393 +{
 93.1394 +   png_debug(1, "in png_set_compression_mem_level\n");
 93.1395 +   if (png_ptr == NULL)
 93.1396 +      return;
 93.1397 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
 93.1398 +   png_ptr->zlib_mem_level = mem_level;
 93.1399 +}
 93.1400 +
 93.1401 +void PNGAPI
 93.1402 +png_set_compression_strategy(png_structp png_ptr, int strategy)
 93.1403 +{
 93.1404 +   png_debug(1, "in png_set_compression_strategy\n");
 93.1405 +   if (png_ptr == NULL)
 93.1406 +      return;
 93.1407 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
 93.1408 +   png_ptr->zlib_strategy = strategy;
 93.1409 +}
 93.1410 +
 93.1411 +void PNGAPI
 93.1412 +png_set_compression_window_bits(png_structp png_ptr, int window_bits)
 93.1413 +{
 93.1414 +   if (png_ptr == NULL)
 93.1415 +      return;
 93.1416 +   if (window_bits > 15)
 93.1417 +      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
 93.1418 +   else if (window_bits < 8)
 93.1419 +      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
 93.1420 +#ifndef WBITS_8_OK
 93.1421 +   /* avoid libpng bug with 256-byte windows */
 93.1422 +   if (window_bits == 8)
 93.1423 +     {
 93.1424 +       png_warning(png_ptr, "Compression window is being reset to 512");
 93.1425 +       window_bits=9;
 93.1426 +     }
 93.1427 +#endif
 93.1428 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
 93.1429 +   png_ptr->zlib_window_bits = window_bits;
 93.1430 +}
 93.1431 +
 93.1432 +void PNGAPI
 93.1433 +png_set_compression_method(png_structp png_ptr, int method)
 93.1434 +{
 93.1435 +   png_debug(1, "in png_set_compression_method\n");
 93.1436 +   if (png_ptr == NULL)
 93.1437 +      return;
 93.1438 +   if (method != 8)
 93.1439 +      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
 93.1440 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
 93.1441 +   png_ptr->zlib_method = method;
 93.1442 +}
 93.1443 +
 93.1444 +void PNGAPI
 93.1445 +png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
 93.1446 +{
 93.1447 +   if (png_ptr == NULL)
 93.1448 +      return;
 93.1449 +   png_ptr->write_row_fn = write_row_fn;
 93.1450 +}
 93.1451 +
 93.1452 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 93.1453 +void PNGAPI
 93.1454 +png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
 93.1455 +   write_user_transform_fn)
 93.1456 +{
 93.1457 +   png_debug(1, "in png_set_write_user_transform_fn\n");
 93.1458 +   if (png_ptr == NULL)
 93.1459 +      return;
 93.1460 +   png_ptr->transformations |= PNG_USER_TRANSFORM;
 93.1461 +   png_ptr->write_user_transform_fn = write_user_transform_fn;
 93.1462 +}
 93.1463 +#endif
 93.1464 +
 93.1465 +
 93.1466 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 93.1467 +void PNGAPI
 93.1468 +png_write_png(png_structp png_ptr, png_infop info_ptr,
 93.1469 +              int transforms, voidp params)
 93.1470 +{
 93.1471 +   if (png_ptr == NULL || info_ptr == NULL)
 93.1472 +      return;
 93.1473 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 93.1474 +   /* invert the alpha channel from opacity to transparency */
 93.1475 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
 93.1476 +       png_set_invert_alpha(png_ptr);
 93.1477 +#endif
 93.1478 +
 93.1479 +   /* Write the file header information. */
 93.1480 +   png_write_info(png_ptr, info_ptr);
 93.1481 +
 93.1482 +   /* ------ these transformations don't touch the info structure ------- */
 93.1483 +
 93.1484 +#if defined(PNG_WRITE_INVERT_SUPPORTED)
 93.1485 +   /* invert monochrome pixels */
 93.1486 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
 93.1487 +       png_set_invert_mono(png_ptr);
 93.1488 +#endif
 93.1489 +
 93.1490 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
 93.1491 +   /* Shift the pixels up to a legal bit depth and fill in
 93.1492 +    * as appropriate to correctly scale the image.
 93.1493 +    */
 93.1494 +   if ((transforms & PNG_TRANSFORM_SHIFT)
 93.1495 +               && (info_ptr->valid & PNG_INFO_sBIT))
 93.1496 +       png_set_shift(png_ptr, &info_ptr->sig_bit);
 93.1497 +#endif
 93.1498 +
 93.1499 +#if defined(PNG_WRITE_PACK_SUPPORTED)
 93.1500 +   /* pack pixels into bytes */
 93.1501 +   if (transforms & PNG_TRANSFORM_PACKING)
 93.1502 +       png_set_packing(png_ptr);
 93.1503 +#endif
 93.1504 +
 93.1505 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 93.1506 +   /* swap location of alpha bytes from ARGB to RGBA */
 93.1507 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
 93.1508 +       png_set_swap_alpha(png_ptr);
 93.1509 +#endif
 93.1510 +
 93.1511 +#if defined(PNG_WRITE_FILLER_SUPPORTED)
 93.1512 +   /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
 93.1513 +    * RGB (4 channels -> 3 channels). The second parameter is not used.
 93.1514 +    */
 93.1515 +   if (transforms & PNG_TRANSFORM_STRIP_FILLER)
 93.1516 +       png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
 93.1517 +#endif
 93.1518 +
 93.1519 +#if defined(PNG_WRITE_BGR_SUPPORTED)
 93.1520 +   /* flip BGR pixels to RGB */
 93.1521 +   if (transforms & PNG_TRANSFORM_BGR)
 93.1522 +       png_set_bgr(png_ptr);
 93.1523 +#endif
 93.1524 +
 93.1525 +#if defined(PNG_WRITE_SWAP_SUPPORTED)
 93.1526 +   /* swap bytes of 16-bit files to most significant byte first */
 93.1527 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
 93.1528 +       png_set_swap(png_ptr);
 93.1529 +#endif
 93.1530 +
 93.1531 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 93.1532 +   /* swap bits of 1, 2, 4 bit packed pixel formats */
 93.1533 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
 93.1534 +       png_set_packswap(png_ptr);
 93.1535 +#endif
 93.1536 +
 93.1537 +   /* ----------------------- end of transformations ------------------- */
 93.1538 +
 93.1539 +   /* write the bits */
 93.1540 +   if (info_ptr->valid & PNG_INFO_IDAT)
 93.1541 +       png_write_image(png_ptr, info_ptr->row_pointers);
 93.1542 +
 93.1543 +   /* It is REQUIRED to call this to finish writing the rest of the file */
 93.1544 +   png_write_end(png_ptr, info_ptr);
 93.1545 +
 93.1546 +   transforms = transforms; /* quiet compiler warnings */
 93.1547 +   params = params;
 93.1548 +}
 93.1549 +#endif
 93.1550 +#endif /* PNG_WRITE_SUPPORTED */
    94.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    94.2 +++ b/libs/libpng/pngwtran.c	Sun Jun 07 17:25:49 2015 +0300
    94.3 @@ -0,0 +1,572 @@
    94.4 +
    94.5 +/* pngwtran.c - transforms the data in a row for PNG writers
    94.6 + *
    94.7 + * Last changed in libpng 1.2.9 April 14, 2006
    94.8 + * For conditions of distribution and use, see copyright notice in png.h
    94.9 + * Copyright (c) 1998-2006 Glenn Randers-Pehrson
   94.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   94.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   94.12 + */
   94.13 +
   94.14 +#define PNG_INTERNAL
   94.15 +#include "png.h"
   94.16 +#ifdef PNG_WRITE_SUPPORTED
   94.17 +
   94.18 +/* Transform the data according to the user's wishes.  The order of
   94.19 + * transformations is significant.
   94.20 + */
   94.21 +void /* PRIVATE */
   94.22 +png_do_write_transformations(png_structp png_ptr)
   94.23 +{
   94.24 +   png_debug(1, "in png_do_write_transformations\n");
   94.25 +
   94.26 +   if (png_ptr == NULL)
   94.27 +      return;
   94.28 +
   94.29 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   94.30 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
   94.31 +      if (png_ptr->write_user_transform_fn != NULL)
   94.32 +        (*(png_ptr->write_user_transform_fn)) /* user write transform function */
   94.33 +          (png_ptr,                    /* png_ptr */
   94.34 +           &(png_ptr->row_info),       /* row_info:     */
   94.35 +             /*  png_uint_32 width;          width of row */
   94.36 +             /*  png_uint_32 rowbytes;       number of bytes in row */
   94.37 +             /*  png_byte color_type;        color type of pixels */
   94.38 +             /*  png_byte bit_depth;         bit depth of samples */
   94.39 +             /*  png_byte channels;          number of channels (1-4) */
   94.40 +             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
   94.41 +           png_ptr->row_buf + 1);      /* start of pixel data for row */
   94.42 +#endif
   94.43 +#if defined(PNG_WRITE_FILLER_SUPPORTED)
   94.44 +   if (png_ptr->transformations & PNG_FILLER)
   94.45 +      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
   94.46 +         png_ptr->flags);
   94.47 +#endif
   94.48 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   94.49 +   if (png_ptr->transformations & PNG_PACKSWAP)
   94.50 +      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.51 +#endif
   94.52 +#if defined(PNG_WRITE_PACK_SUPPORTED)
   94.53 +   if (png_ptr->transformations & PNG_PACK)
   94.54 +      png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
   94.55 +         (png_uint_32)png_ptr->bit_depth);
   94.56 +#endif
   94.57 +#if defined(PNG_WRITE_SWAP_SUPPORTED)
   94.58 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
   94.59 +      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.60 +#endif
   94.61 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
   94.62 +   if (png_ptr->transformations & PNG_SHIFT)
   94.63 +      png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
   94.64 +         &(png_ptr->shift));
   94.65 +#endif
   94.66 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   94.67 +   if (png_ptr->transformations & PNG_SWAP_ALPHA)
   94.68 +      png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.69 +#endif
   94.70 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   94.71 +   if (png_ptr->transformations & PNG_INVERT_ALPHA)
   94.72 +      png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.73 +#endif
   94.74 +#if defined(PNG_WRITE_BGR_SUPPORTED)
   94.75 +   if (png_ptr->transformations & PNG_BGR)
   94.76 +      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.77 +#endif
   94.78 +#if defined(PNG_WRITE_INVERT_SUPPORTED)
   94.79 +   if (png_ptr->transformations & PNG_INVERT_MONO)
   94.80 +      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
   94.81 +#endif
   94.82 +}
   94.83 +
   94.84 +#if defined(PNG_WRITE_PACK_SUPPORTED)
   94.85 +/* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
   94.86 + * row_info bit depth should be 8 (one pixel per byte).  The channels
   94.87 + * should be 1 (this only happens on grayscale and paletted images).
   94.88 + */
   94.89 +void /* PRIVATE */
   94.90 +png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
   94.91 +{
   94.92 +   png_debug(1, "in png_do_pack\n");
   94.93 +   if (row_info->bit_depth == 8 &&
   94.94 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
   94.95 +       row != NULL && row_info != NULL &&
   94.96 +#endif
   94.97 +      row_info->channels == 1)
   94.98 +   {
   94.99 +      switch ((int)bit_depth)
  94.100 +      {
  94.101 +         case 1:
  94.102 +         {
  94.103 +            png_bytep sp, dp;
  94.104 +            int mask, v;
  94.105 +            png_uint_32 i;
  94.106 +            png_uint_32 row_width = row_info->width;
  94.107 +
  94.108 +            sp = row;
  94.109 +            dp = row;
  94.110 +            mask = 0x80;
  94.111 +            v = 0;
  94.112 +
  94.113 +            for (i = 0; i < row_width; i++)
  94.114 +            {
  94.115 +               if (*sp != 0)
  94.116 +                  v |= mask;
  94.117 +               sp++;
  94.118 +               if (mask > 1)
  94.119 +                  mask >>= 1;
  94.120 +               else
  94.121 +               {
  94.122 +                  mask = 0x80;
  94.123 +                  *dp = (png_byte)v;
  94.124 +                  dp++;
  94.125 +                  v = 0;
  94.126 +               }
  94.127 +            }
  94.128 +            if (mask != 0x80)
  94.129 +               *dp = (png_byte)v;
  94.130 +            break;
  94.131 +         }
  94.132 +         case 2:
  94.133 +         {
  94.134 +            png_bytep sp, dp;
  94.135 +            int shift, v;
  94.136 +            png_uint_32 i;
  94.137 +            png_uint_32 row_width = row_info->width;
  94.138 +
  94.139 +            sp = row;
  94.140 +            dp = row;
  94.141 +            shift = 6;
  94.142 +            v = 0;
  94.143 +            for (i = 0; i < row_width; i++)
  94.144 +            {
  94.145 +               png_byte value;
  94.146 +
  94.147 +               value = (png_byte)(*sp & 0x03);
  94.148 +               v |= (value << shift);
  94.149 +               if (shift == 0)
  94.150 +               {
  94.151 +                  shift = 6;
  94.152 +                  *dp = (png_byte)v;
  94.153 +                  dp++;
  94.154 +                  v = 0;
  94.155 +               }
  94.156 +               else
  94.157 +                  shift -= 2;
  94.158 +               sp++;
  94.159 +            }
  94.160 +            if (shift != 6)
  94.161 +               *dp = (png_byte)v;
  94.162 +            break;
  94.163 +         }
  94.164 +         case 4:
  94.165 +         {
  94.166 +            png_bytep sp, dp;
  94.167 +            int shift, v;
  94.168 +            png_uint_32 i;
  94.169 +            png_uint_32 row_width = row_info->width;
  94.170 +
  94.171 +            sp = row;
  94.172 +            dp = row;
  94.173 +            shift = 4;
  94.174 +            v = 0;
  94.175 +            for (i = 0; i < row_width; i++)
  94.176 +            {
  94.177 +               png_byte value;
  94.178 +
  94.179 +               value = (png_byte)(*sp & 0x0f);
  94.180 +               v |= (value << shift);
  94.181 +
  94.182 +               if (shift == 0)
  94.183 +               {
  94.184 +                  shift = 4;
  94.185 +                  *dp = (png_byte)v;
  94.186 +                  dp++;
  94.187 +                  v = 0;
  94.188 +               }
  94.189 +               else
  94.190 +                  shift -= 4;
  94.191 +
  94.192 +               sp++;
  94.193 +            }
  94.194 +            if (shift != 4)
  94.195 +               *dp = (png_byte)v;
  94.196 +            break;
  94.197 +         }
  94.198 +      }
  94.199 +      row_info->bit_depth = (png_byte)bit_depth;
  94.200 +      row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
  94.201 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  94.202 +         row_info->width);
  94.203 +   }
  94.204 +}
  94.205 +#endif
  94.206 +
  94.207 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
  94.208 +/* Shift pixel values to take advantage of whole range.  Pass the
  94.209 + * true number of bits in bit_depth.  The row should be packed
  94.210 + * according to row_info->bit_depth.  Thus, if you had a row of
  94.211 + * bit depth 4, but the pixels only had values from 0 to 7, you
  94.212 + * would pass 3 as bit_depth, and this routine would translate the
  94.213 + * data to 0 to 15.
  94.214 + */
  94.215 +void /* PRIVATE */
  94.216 +png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
  94.217 +{
  94.218 +   png_debug(1, "in png_do_shift\n");
  94.219 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  94.220 +   if (row != NULL && row_info != NULL &&
  94.221 +#else
  94.222 +   if (
  94.223 +#endif
  94.224 +      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  94.225 +   {
  94.226 +      int shift_start[4], shift_dec[4];
  94.227 +      int channels = 0;
  94.228 +
  94.229 +      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
  94.230 +      {
  94.231 +         shift_start[channels] = row_info->bit_depth - bit_depth->red;
  94.232 +         shift_dec[channels] = bit_depth->red;
  94.233 +         channels++;
  94.234 +         shift_start[channels] = row_info->bit_depth - bit_depth->green;
  94.235 +         shift_dec[channels] = bit_depth->green;
  94.236 +         channels++;
  94.237 +         shift_start[channels] = row_info->bit_depth - bit_depth->blue;
  94.238 +         shift_dec[channels] = bit_depth->blue;
  94.239 +         channels++;
  94.240 +      }
  94.241 +      else
  94.242 +      {
  94.243 +         shift_start[channels] = row_info->bit_depth - bit_depth->gray;
  94.244 +         shift_dec[channels] = bit_depth->gray;
  94.245 +         channels++;
  94.246 +      }
  94.247 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  94.248 +      {
  94.249 +         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
  94.250 +         shift_dec[channels] = bit_depth->alpha;
  94.251 +         channels++;
  94.252 +      }
  94.253 +
  94.254 +      /* with low row depths, could only be grayscale, so one channel */
  94.255 +      if (row_info->bit_depth < 8)
  94.256 +      {
  94.257 +         png_bytep bp = row;
  94.258 +         png_uint_32 i;
  94.259 +         png_byte mask;
  94.260 +         png_uint_32 row_bytes = row_info->rowbytes;
  94.261 +
  94.262 +         if (bit_depth->gray == 1 && row_info->bit_depth == 2)
  94.263 +            mask = 0x55;
  94.264 +         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
  94.265 +            mask = 0x11;
  94.266 +         else
  94.267 +            mask = 0xff;
  94.268 +
  94.269 +         for (i = 0; i < row_bytes; i++, bp++)
  94.270 +         {
  94.271 +            png_uint_16 v;
  94.272 +            int j;
  94.273 +
  94.274 +            v = *bp;
  94.275 +            *bp = 0;
  94.276 +            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
  94.277 +            {
  94.278 +               if (j > 0)
  94.279 +                  *bp |= (png_byte)((v << j) & 0xff);
  94.280 +               else
  94.281 +                  *bp |= (png_byte)((v >> (-j)) & mask);
  94.282 +            }
  94.283 +         }
  94.284 +      }
  94.285 +      else if (row_info->bit_depth == 8)
  94.286 +      {
  94.287 +         png_bytep bp = row;
  94.288 +         png_uint_32 i;
  94.289 +         png_uint_32 istop = channels * row_info->width;
  94.290 +
  94.291 +         for (i = 0; i < istop; i++, bp++)
  94.292 +         {
  94.293 +
  94.294 +            png_uint_16 v;
  94.295 +            int j;
  94.296 +            int c = (int)(i%channels);
  94.297 +
  94.298 +            v = *bp;
  94.299 +            *bp = 0;
  94.300 +            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  94.301 +            {
  94.302 +               if (j > 0)
  94.303 +                  *bp |= (png_byte)((v << j) & 0xff);
  94.304 +               else
  94.305 +                  *bp |= (png_byte)((v >> (-j)) & 0xff);
  94.306 +            }
  94.307 +         }
  94.308 +      }
  94.309 +      else
  94.310 +      {
  94.311 +         png_bytep bp;
  94.312 +         png_uint_32 i;
  94.313 +         png_uint_32 istop = channels * row_info->width;
  94.314 +
  94.315 +         for (bp = row, i = 0; i < istop; i++)
  94.316 +         {
  94.317 +            int c = (int)(i%channels);
  94.318 +            png_uint_16 value, v;
  94.319 +            int j;
  94.320 +
  94.321 +            v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
  94.322 +            value = 0;
  94.323 +            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  94.324 +            {
  94.325 +               if (j > 0)
  94.326 +                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
  94.327 +               else
  94.328 +                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
  94.329 +            }
  94.330 +            *bp++ = (png_byte)(value >> 8);
  94.331 +            *bp++ = (png_byte)(value & 0xff);
  94.332 +         }
  94.333 +      }
  94.334 +   }
  94.335 +}
  94.336 +#endif
  94.337 +
  94.338 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  94.339 +void /* PRIVATE */
  94.340 +png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
  94.341 +{
  94.342 +   png_debug(1, "in png_do_write_swap_alpha\n");
  94.343 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  94.344 +   if (row != NULL && row_info != NULL)
  94.345 +#endif
  94.346 +   {
  94.347 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  94.348 +      {
  94.349 +         /* This converts from ARGB to RGBA */
  94.350 +         if (row_info->bit_depth == 8)
  94.351 +         {
  94.352 +            png_bytep sp, dp;
  94.353 +            png_uint_32 i;
  94.354 +            png_uint_32 row_width = row_info->width;
  94.355 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.356 +            {
  94.357 +               png_byte save = *(sp++);
  94.358 +               *(dp++) = *(sp++);
  94.359 +               *(dp++) = *(sp++);
  94.360 +               *(dp++) = *(sp++);
  94.361 +               *(dp++) = save;
  94.362 +            }
  94.363 +         }
  94.364 +         /* This converts from AARRGGBB to RRGGBBAA */
  94.365 +         else
  94.366 +         {
  94.367 +            png_bytep sp, dp;
  94.368 +            png_uint_32 i;
  94.369 +            png_uint_32 row_width = row_info->width;
  94.370 +
  94.371 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.372 +            {
  94.373 +               png_byte save[2];
  94.374 +               save[0] = *(sp++);
  94.375 +               save[1] = *(sp++);
  94.376 +               *(dp++) = *(sp++);
  94.377 +               *(dp++) = *(sp++);
  94.378 +               *(dp++) = *(sp++);
  94.379 +               *(dp++) = *(sp++);
  94.380 +               *(dp++) = *(sp++);
  94.381 +               *(dp++) = *(sp++);
  94.382 +               *(dp++) = save[0];
  94.383 +               *(dp++) = save[1];
  94.384 +            }
  94.385 +         }
  94.386 +      }
  94.387 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  94.388 +      {
  94.389 +         /* This converts from AG to GA */
  94.390 +         if (row_info->bit_depth == 8)
  94.391 +         {
  94.392 +            png_bytep sp, dp;
  94.393 +            png_uint_32 i;
  94.394 +            png_uint_32 row_width = row_info->width;
  94.395 +
  94.396 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.397 +            {
  94.398 +               png_byte save = *(sp++);
  94.399 +               *(dp++) = *(sp++);
  94.400 +               *(dp++) = save;
  94.401 +            }
  94.402 +         }
  94.403 +         /* This converts from AAGG to GGAA */
  94.404 +         else
  94.405 +         {
  94.406 +            png_bytep sp, dp;
  94.407 +            png_uint_32 i;
  94.408 +            png_uint_32 row_width = row_info->width;
  94.409 +
  94.410 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.411 +            {
  94.412 +               png_byte save[2];
  94.413 +               save[0] = *(sp++);
  94.414 +               save[1] = *(sp++);
  94.415 +               *(dp++) = *(sp++);
  94.416 +               *(dp++) = *(sp++);
  94.417 +               *(dp++) = save[0];
  94.418 +               *(dp++) = save[1];
  94.419 +            }
  94.420 +         }
  94.421 +      }
  94.422 +   }
  94.423 +}
  94.424 +#endif
  94.425 +
  94.426 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  94.427 +void /* PRIVATE */
  94.428 +png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
  94.429 +{
  94.430 +   png_debug(1, "in png_do_write_invert_alpha\n");
  94.431 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  94.432 +   if (row != NULL && row_info != NULL)
  94.433 +#endif
  94.434 +   {
  94.435 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  94.436 +      {
  94.437 +         /* This inverts the alpha channel in RGBA */
  94.438 +         if (row_info->bit_depth == 8)
  94.439 +         {
  94.440 +            png_bytep sp, dp;
  94.441 +            png_uint_32 i;
  94.442 +            png_uint_32 row_width = row_info->width;
  94.443 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.444 +            {
  94.445 +               /* does nothing
  94.446 +               *(dp++) = *(sp++);
  94.447 +               *(dp++) = *(sp++);
  94.448 +               *(dp++) = *(sp++);
  94.449 +               */
  94.450 +               sp+=3; dp = sp;
  94.451 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.452 +            }
  94.453 +         }
  94.454 +         /* This inverts the alpha channel in RRGGBBAA */
  94.455 +         else
  94.456 +         {
  94.457 +            png_bytep sp, dp;
  94.458 +            png_uint_32 i;
  94.459 +            png_uint_32 row_width = row_info->width;
  94.460 +
  94.461 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.462 +            {
  94.463 +               /* does nothing
  94.464 +               *(dp++) = *(sp++);
  94.465 +               *(dp++) = *(sp++);
  94.466 +               *(dp++) = *(sp++);
  94.467 +               *(dp++) = *(sp++);
  94.468 +               *(dp++) = *(sp++);
  94.469 +               *(dp++) = *(sp++);
  94.470 +               */
  94.471 +               sp+=6; dp = sp;
  94.472 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.473 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.474 +            }
  94.475 +         }
  94.476 +      }
  94.477 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  94.478 +      {
  94.479 +         /* This inverts the alpha channel in GA */
  94.480 +         if (row_info->bit_depth == 8)
  94.481 +         {
  94.482 +            png_bytep sp, dp;
  94.483 +            png_uint_32 i;
  94.484 +            png_uint_32 row_width = row_info->width;
  94.485 +
  94.486 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.487 +            {
  94.488 +               *(dp++) = *(sp++);
  94.489 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.490 +            }
  94.491 +         }
  94.492 +         /* This inverts the alpha channel in GGAA */
  94.493 +         else
  94.494 +         {
  94.495 +            png_bytep sp, dp;
  94.496 +            png_uint_32 i;
  94.497 +            png_uint_32 row_width = row_info->width;
  94.498 +
  94.499 +            for (i = 0, sp = dp = row; i < row_width; i++)
  94.500 +            {
  94.501 +               /* does nothing
  94.502 +               *(dp++) = *(sp++);
  94.503 +               *(dp++) = *(sp++);
  94.504 +               */
  94.505 +               sp+=2; dp = sp;
  94.506 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.507 +               *(dp++) = (png_byte)(255 - *(sp++));
  94.508 +            }
  94.509 +         }
  94.510 +      }
  94.511 +   }
  94.512 +}
  94.513 +#endif
  94.514 +
  94.515 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  94.516 +/* undoes intrapixel differencing  */
  94.517 +void /* PRIVATE */
  94.518 +png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
  94.519 +{
  94.520 +   png_debug(1, "in png_do_write_intrapixel\n");
  94.521 +   if (
  94.522 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  94.523 +       row != NULL && row_info != NULL &&
  94.524 +#endif
  94.525 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
  94.526 +   {
  94.527 +      int bytes_per_pixel;
  94.528 +      png_uint_32 row_width = row_info->width;
  94.529 +      if (row_info->bit_depth == 8)
  94.530 +      {
  94.531 +         png_bytep rp;
  94.532 +         png_uint_32 i;
  94.533 +
  94.534 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  94.535 +            bytes_per_pixel = 3;
  94.536 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  94.537 +            bytes_per_pixel = 4;
  94.538 +         else
  94.539 +            return;
  94.540 +
  94.541 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  94.542 +         {
  94.543 +            *(rp)   = (png_byte)((*rp     - *(rp+1))&0xff);
  94.544 +            *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
  94.545 +         }
  94.546 +      }
  94.547 +      else if (row_info->bit_depth == 16)
  94.548 +      {
  94.549 +         png_bytep rp;
  94.550 +         png_uint_32 i;
  94.551 +
  94.552 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  94.553 +            bytes_per_pixel = 6;
  94.554 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  94.555 +            bytes_per_pixel = 8;
  94.556 +         else
  94.557 +            return;
  94.558 +
  94.559 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  94.560 +         {
  94.561 +            png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
  94.562 +            png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
  94.563 +            png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
  94.564 +            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);
  94.565 +            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
  94.566 +            *(rp  ) = (png_byte)((red >> 8) & 0xff);
  94.567 +            *(rp+1) = (png_byte)(red & 0xff);
  94.568 +            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
  94.569 +            *(rp+5) = (png_byte)(blue & 0xff);
  94.570 +         }
  94.571 +      }
  94.572 +   }
  94.573 +}
  94.574 +#endif /* PNG_MNG_FEATURES_SUPPORTED */
  94.575 +#endif /* PNG_WRITE_SUPPORTED */
    95.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    95.2 +++ b/libs/libpng/pngwutil.c	Sun Jun 07 17:25:49 2015 +0300
    95.3 @@ -0,0 +1,2827 @@
    95.4 +
    95.5 +/* pngwutil.c - utilities to write a PNG file
    95.6 + *
    95.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    95.8 + * For conditions of distribution and use, see copyright notice in png.h
    95.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   95.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   95.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   95.12 + */
   95.13 +
   95.14 +#define PNG_INTERNAL
   95.15 +#include "png.h"
   95.16 +#ifdef PNG_WRITE_SUPPORTED
   95.17 +
   95.18 +/* Place a 32-bit number into a buffer in PNG byte order.  We work
   95.19 + * with unsigned numbers for convenience, although one supported
   95.20 + * ancillary chunk uses signed (two's complement) numbers.
   95.21 + */
   95.22 +void PNGAPI
   95.23 +png_save_uint_32(png_bytep buf, png_uint_32 i)
   95.24 +{
   95.25 +   buf[0] = (png_byte)((i >> 24) & 0xff);
   95.26 +   buf[1] = (png_byte)((i >> 16) & 0xff);
   95.27 +   buf[2] = (png_byte)((i >> 8) & 0xff);
   95.28 +   buf[3] = (png_byte)(i & 0xff);
   95.29 +}
   95.30 +
   95.31 +/* The png_save_int_32 function assumes integers are stored in two's
   95.32 + * complement format.  If this isn't the case, then this routine needs to
   95.33 + * be modified to write data in two's complement format.
   95.34 + */
   95.35 +void PNGAPI
   95.36 +png_save_int_32(png_bytep buf, png_int_32 i)
   95.37 +{
   95.38 +   buf[0] = (png_byte)((i >> 24) & 0xff);
   95.39 +   buf[1] = (png_byte)((i >> 16) & 0xff);
   95.40 +   buf[2] = (png_byte)((i >> 8) & 0xff);
   95.41 +   buf[3] = (png_byte)(i & 0xff);
   95.42 +}
   95.43 +
   95.44 +/* Place a 16-bit number into a buffer in PNG byte order.
   95.45 + * The parameter is declared unsigned int, not png_uint_16,
   95.46 + * just to avoid potential problems on pre-ANSI C compilers.
   95.47 + */
   95.48 +void PNGAPI
   95.49 +png_save_uint_16(png_bytep buf, unsigned int i)
   95.50 +{
   95.51 +   buf[0] = (png_byte)((i >> 8) & 0xff);
   95.52 +   buf[1] = (png_byte)(i & 0xff);
   95.53 +}
   95.54 +
   95.55 +/* Simple function to write the signature.  If we have already written
   95.56 + * the magic bytes of the signature, or more likely, the PNG stream is
   95.57 + * being embedded into another stream and doesn't need its own signature,
   95.58 + * we should call png_set_sig_bytes() to tell libpng how many of the
   95.59 + * bytes have already been written.
   95.60 + */
   95.61 +void /* PRIVATE */
   95.62 +png_write_sig(png_structp png_ptr)
   95.63 +{
   95.64 +   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   95.65 +
   95.66 +   /* write the rest of the 8 byte signature */
   95.67 +   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
   95.68 +      (png_size_t)(8 - png_ptr->sig_bytes));
   95.69 +   if (png_ptr->sig_bytes < 3)
   95.70 +      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
   95.71 +}
   95.72 +
   95.73 +/* Write a PNG chunk all at once.  The type is an array of ASCII characters
   95.74 + * representing the chunk name.  The array must be at least 4 bytes in
   95.75 + * length, and does not need to be null terminated.  To be safe, pass the
   95.76 + * pre-defined chunk names here, and if you need a new one, define it
   95.77 + * where the others are defined.  The length is the length of the data.
   95.78 + * All the data must be present.  If that is not possible, use the
   95.79 + * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
   95.80 + * functions instead.
   95.81 + */
   95.82 +void PNGAPI
   95.83 +png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
   95.84 +   png_bytep data, png_size_t length)
   95.85 +{
   95.86 +   if (png_ptr == NULL) return;
   95.87 +   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
   95.88 +   png_write_chunk_data(png_ptr, data, (png_size_t)length);
   95.89 +   png_write_chunk_end(png_ptr);
   95.90 +}
   95.91 +
   95.92 +/* Write the start of a PNG chunk.  The type is the chunk type.
   95.93 + * The total_length is the sum of the lengths of all the data you will be
   95.94 + * passing in png_write_chunk_data().
   95.95 + */
   95.96 +void PNGAPI
   95.97 +png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
   95.98 +   png_uint_32 length)
   95.99 +{
  95.100 +   png_byte buf[8];
  95.101 +
  95.102 +   png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
  95.103 +      (unsigned long)length);
  95.104 +   if (png_ptr == NULL) return;
  95.105 +
  95.106 +   /* write the length and the chunk name */
  95.107 +   png_save_uint_32(buf, length);
  95.108 +   png_memcpy(buf + 4, chunk_name, 4);
  95.109 +   png_write_data(png_ptr, buf, (png_size_t)8);
  95.110 +   /* put the chunk name into png_ptr->chunk_name */
  95.111 +   png_memcpy(png_ptr->chunk_name, chunk_name, 4);
  95.112 +   /* reset the crc and run it over the chunk name */
  95.113 +   png_reset_crc(png_ptr);
  95.114 +   png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
  95.115 +}
  95.116 +
  95.117 +/* Write the data of a PNG chunk started with png_write_chunk_start().
  95.118 + * Note that multiple calls to this function are allowed, and that the
  95.119 + * sum of the lengths from these calls *must* add up to the total_length
  95.120 + * given to png_write_chunk_start().
  95.121 + */
  95.122 +void PNGAPI
  95.123 +png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
  95.124 +{
  95.125 +   /* write the data, and run the CRC over it */
  95.126 +   if (png_ptr == NULL) return;
  95.127 +   if (data != NULL && length > 0)
  95.128 +   {
  95.129 +      png_write_data(png_ptr, data, length);
  95.130 +      /* update the CRC after writing the data,
  95.131 +       * in case that the user I/O routine alters it.
  95.132 +       */
  95.133 +      png_calculate_crc(png_ptr, data, length);
  95.134 +   }
  95.135 +}
  95.136 +
  95.137 +/* Finish a chunk started with png_write_chunk_start(). */
  95.138 +void PNGAPI
  95.139 +png_write_chunk_end(png_structp png_ptr)
  95.140 +{
  95.141 +   png_byte buf[4];
  95.142 +
  95.143 +   if (png_ptr == NULL) return;
  95.144 +
  95.145 +   /* write the crc in a single operation */
  95.146 +   png_save_uint_32(buf, png_ptr->crc);
  95.147 +
  95.148 +   png_write_data(png_ptr, buf, (png_size_t)4);
  95.149 +}
  95.150 +
  95.151 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
  95.152 +/*
  95.153 + * This pair of functions encapsulates the operation of (a) compressing a
  95.154 + * text string, and (b) issuing it later as a series of chunk data writes.
  95.155 + * The compression_state structure is shared context for these functions
  95.156 + * set up by the caller in order to make the whole mess thread-safe.
  95.157 + */
  95.158 +
  95.159 +typedef struct
  95.160 +{
  95.161 +    char *input;   /* the uncompressed input data */
  95.162 +    int input_len;   /* its length */
  95.163 +    int num_output_ptr; /* number of output pointers used */
  95.164 +    int max_output_ptr; /* size of output_ptr */
  95.165 +    png_charpp output_ptr; /* array of pointers to output */
  95.166 +} compression_state;
  95.167 +
  95.168 +/* compress given text into storage in the png_ptr structure */
  95.169 +static int /* PRIVATE */
  95.170 +png_text_compress(png_structp png_ptr,
  95.171 +        png_charp text, png_size_t text_len, int compression,
  95.172 +        compression_state *comp)
  95.173 +{
  95.174 +   int ret;
  95.175 +
  95.176 +   comp->num_output_ptr = 0;
  95.177 +   comp->max_output_ptr = 0;
  95.178 +   comp->output_ptr = NULL;
  95.179 +   comp->input = NULL;
  95.180 +   comp->input_len = 0;
  95.181 +
  95.182 +   /* we may just want to pass the text right through */
  95.183 +   if (compression == PNG_TEXT_COMPRESSION_NONE)
  95.184 +   {
  95.185 +       comp->input = text;
  95.186 +       comp->input_len = text_len;
  95.187 +       return((int)text_len);
  95.188 +   }
  95.189 +
  95.190 +   if (compression >= PNG_TEXT_COMPRESSION_LAST)
  95.191 +   {
  95.192 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  95.193 +      char msg[50];
  95.194 +      png_snprintf(msg, 50, "Unknown compression type %d", compression);
  95.195 +      png_warning(png_ptr, msg);
  95.196 +#else
  95.197 +      png_warning(png_ptr, "Unknown compression type");
  95.198 +#endif
  95.199 +   }
  95.200 +
  95.201 +   /* We can't write the chunk until we find out how much data we have,
  95.202 +    * which means we need to run the compressor first and save the
  95.203 +    * output.  This shouldn't be a problem, as the vast majority of
  95.204 +    * comments should be reasonable, but we will set up an array of
  95.205 +    * malloc'd pointers to be sure.
  95.206 +    *
  95.207 +    * If we knew the application was well behaved, we could simplify this
  95.208 +    * greatly by assuming we can always malloc an output buffer large
  95.209 +    * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
  95.210 +    * and malloc this directly.  The only time this would be a bad idea is
  95.211 +    * if we can't malloc more than 64K and we have 64K of random input
  95.212 +    * data, or if the input string is incredibly large (although this
  95.213 +    * wouldn't cause a failure, just a slowdown due to swapping).
  95.214 +    */
  95.215 +
  95.216 +   /* set up the compression buffers */
  95.217 +   png_ptr->zstream.avail_in = (uInt)text_len;
  95.218 +   png_ptr->zstream.next_in = (Bytef *)text;
  95.219 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  95.220 +   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
  95.221 +
  95.222 +   /* this is the same compression loop as in png_write_row() */
  95.223 +   do
  95.224 +   {
  95.225 +      /* compress the data */
  95.226 +      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
  95.227 +      if (ret != Z_OK)
  95.228 +      {
  95.229 +         /* error */
  95.230 +         if (png_ptr->zstream.msg != NULL)
  95.231 +            png_error(png_ptr, png_ptr->zstream.msg);
  95.232 +         else
  95.233 +            png_error(png_ptr, "zlib error");
  95.234 +      }
  95.235 +      /* check to see if we need more room */
  95.236 +      if (!(png_ptr->zstream.avail_out))
  95.237 +      {
  95.238 +         /* make sure the output array has room */
  95.239 +         if (comp->num_output_ptr >= comp->max_output_ptr)
  95.240 +         {
  95.241 +            int old_max;
  95.242 +
  95.243 +            old_max = comp->max_output_ptr;
  95.244 +            comp->max_output_ptr = comp->num_output_ptr + 4;
  95.245 +            if (comp->output_ptr != NULL)
  95.246 +            {
  95.247 +               png_charpp old_ptr;
  95.248 +
  95.249 +               old_ptr = comp->output_ptr;
  95.250 +               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  95.251 +                  (png_uint_32)
  95.252 +                  (comp->max_output_ptr * png_sizeof(png_charpp)));
  95.253 +               png_memcpy(comp->output_ptr, old_ptr, old_max
  95.254 +                  * png_sizeof(png_charp));
  95.255 +               png_free(png_ptr, old_ptr);
  95.256 +            }
  95.257 +            else
  95.258 +               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  95.259 +                  (png_uint_32)
  95.260 +                  (comp->max_output_ptr * png_sizeof(png_charp)));
  95.261 +         }
  95.262 +
  95.263 +         /* save the data */
  95.264 +         comp->output_ptr[comp->num_output_ptr] =
  95.265 +            (png_charp)png_malloc(png_ptr,
  95.266 +            (png_uint_32)png_ptr->zbuf_size);
  95.267 +         png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  95.268 +            png_ptr->zbuf_size);
  95.269 +         comp->num_output_ptr++;
  95.270 +
  95.271 +         /* and reset the buffer */
  95.272 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  95.273 +         png_ptr->zstream.next_out = png_ptr->zbuf;
  95.274 +      }
  95.275 +   /* continue until we don't have any more to compress */
  95.276 +   } while (png_ptr->zstream.avail_in);
  95.277 +
  95.278 +   /* finish the compression */
  95.279 +   do
  95.280 +   {
  95.281 +      /* tell zlib we are finished */
  95.282 +      ret = deflate(&png_ptr->zstream, Z_FINISH);
  95.283 +
  95.284 +      if (ret == Z_OK)
  95.285 +      {
  95.286 +         /* check to see if we need more room */
  95.287 +         if (!(png_ptr->zstream.avail_out))
  95.288 +         {
  95.289 +            /* check to make sure our output array has room */
  95.290 +            if (comp->num_output_ptr >= comp->max_output_ptr)
  95.291 +            {
  95.292 +               int old_max;
  95.293 +
  95.294 +               old_max = comp->max_output_ptr;
  95.295 +               comp->max_output_ptr = comp->num_output_ptr + 4;
  95.296 +               if (comp->output_ptr != NULL)
  95.297 +               {
  95.298 +                  png_charpp old_ptr;
  95.299 +
  95.300 +                  old_ptr = comp->output_ptr;
  95.301 +                  /* This could be optimized to realloc() */
  95.302 +                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  95.303 +                     (png_uint_32)(comp->max_output_ptr *
  95.304 +                     png_sizeof(png_charp)));
  95.305 +                  png_memcpy(comp->output_ptr, old_ptr,
  95.306 +                     old_max * png_sizeof(png_charp));
  95.307 +                  png_free(png_ptr, old_ptr);
  95.308 +               }
  95.309 +               else
  95.310 +                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  95.311 +                     (png_uint_32)(comp->max_output_ptr *
  95.312 +                     png_sizeof(png_charp)));
  95.313 +            }
  95.314 +
  95.315 +            /* save off the data */
  95.316 +            comp->output_ptr[comp->num_output_ptr] =
  95.317 +               (png_charp)png_malloc(png_ptr,
  95.318 +               (png_uint_32)png_ptr->zbuf_size);
  95.319 +            png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  95.320 +               png_ptr->zbuf_size);
  95.321 +            comp->num_output_ptr++;
  95.322 +
  95.323 +            /* and reset the buffer pointers */
  95.324 +            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  95.325 +            png_ptr->zstream.next_out = png_ptr->zbuf;
  95.326 +         }
  95.327 +      }
  95.328 +      else if (ret != Z_STREAM_END)
  95.329 +      {
  95.330 +         /* we got an error */
  95.331 +         if (png_ptr->zstream.msg != NULL)
  95.332 +            png_error(png_ptr, png_ptr->zstream.msg);
  95.333 +         else
  95.334 +            png_error(png_ptr, "zlib error");
  95.335 +      }
  95.336 +   } while (ret != Z_STREAM_END);
  95.337 +
  95.338 +   /* text length is number of buffers plus last buffer */
  95.339 +   text_len = png_ptr->zbuf_size * comp->num_output_ptr;
  95.340 +   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
  95.341 +      text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
  95.342 +
  95.343 +   return((int)text_len);
  95.344 +}
  95.345 +
  95.346 +/* ship the compressed text out via chunk writes */
  95.347 +static void /* PRIVATE */
  95.348 +png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
  95.349 +{
  95.350 +   int i;
  95.351 +
  95.352 +   /* handle the no-compression case */
  95.353 +   if (comp->input)
  95.354 +   {
  95.355 +       png_write_chunk_data(png_ptr, (png_bytep)comp->input,
  95.356 +                            (png_size_t)comp->input_len);
  95.357 +       return;
  95.358 +   }
  95.359 +
  95.360 +   /* write saved output buffers, if any */
  95.361 +   for (i = 0; i < comp->num_output_ptr; i++)
  95.362 +   {
  95.363 +      png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
  95.364 +         (png_size_t)png_ptr->zbuf_size);
  95.365 +      png_free(png_ptr, comp->output_ptr[i]);
  95.366 +       comp->output_ptr[i]=NULL;
  95.367 +   }
  95.368 +   if (comp->max_output_ptr != 0)
  95.369 +      png_free(png_ptr, comp->output_ptr);
  95.370 +       comp->output_ptr=NULL;
  95.371 +   /* write anything left in zbuf */
  95.372 +   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
  95.373 +      png_write_chunk_data(png_ptr, png_ptr->zbuf,
  95.374 +         (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  95.375 +
  95.376 +   /* reset zlib for another zTXt/iTXt or image data */
  95.377 +   deflateReset(&png_ptr->zstream);
  95.378 +   png_ptr->zstream.data_type = Z_BINARY;
  95.379 +}
  95.380 +#endif
  95.381 +
  95.382 +/* Write the IHDR chunk, and update the png_struct with the necessary
  95.383 + * information.  Note that the rest of this code depends upon this
  95.384 + * information being correct.
  95.385 + */
  95.386 +void /* PRIVATE */
  95.387 +png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
  95.388 +   int bit_depth, int color_type, int compression_type, int filter_type,
  95.389 +   int interlace_type)
  95.390 +{
  95.391 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.392 +   PNG_IHDR;
  95.393 +#endif
  95.394 +   int ret;
  95.395 +
  95.396 +   png_byte buf[13]; /* buffer to store the IHDR info */
  95.397 +
  95.398 +   png_debug(1, "in png_write_IHDR\n");
  95.399 +   /* Check that we have valid input data from the application info */
  95.400 +   switch (color_type)
  95.401 +   {
  95.402 +      case PNG_COLOR_TYPE_GRAY:
  95.403 +         switch (bit_depth)
  95.404 +         {
  95.405 +            case 1:
  95.406 +            case 2:
  95.407 +            case 4:
  95.408 +            case 8:
  95.409 +            case 16: png_ptr->channels = 1; break;
  95.410 +            default: png_error(png_ptr, "Invalid bit depth for grayscale image");
  95.411 +         }
  95.412 +         break;
  95.413 +      case PNG_COLOR_TYPE_RGB:
  95.414 +         if (bit_depth != 8 && bit_depth != 16)
  95.415 +            png_error(png_ptr, "Invalid bit depth for RGB image");
  95.416 +         png_ptr->channels = 3;
  95.417 +         break;
  95.418 +      case PNG_COLOR_TYPE_PALETTE:
  95.419 +         switch (bit_depth)
  95.420 +         {
  95.421 +            case 1:
  95.422 +            case 2:
  95.423 +            case 4:
  95.424 +            case 8: png_ptr->channels = 1; break;
  95.425 +            default: png_error(png_ptr, "Invalid bit depth for paletted image");
  95.426 +         }
  95.427 +         break;
  95.428 +      case PNG_COLOR_TYPE_GRAY_ALPHA:
  95.429 +         if (bit_depth != 8 && bit_depth != 16)
  95.430 +            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
  95.431 +         png_ptr->channels = 2;
  95.432 +         break;
  95.433 +      case PNG_COLOR_TYPE_RGB_ALPHA:
  95.434 +         if (bit_depth != 8 && bit_depth != 16)
  95.435 +            png_error(png_ptr, "Invalid bit depth for RGBA image");
  95.436 +         png_ptr->channels = 4;
  95.437 +         break;
  95.438 +      default:
  95.439 +         png_error(png_ptr, "Invalid image color type specified");
  95.440 +   }
  95.441 +
  95.442 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  95.443 +   {
  95.444 +      png_warning(png_ptr, "Invalid compression type specified");
  95.445 +      compression_type = PNG_COMPRESSION_TYPE_BASE;
  95.446 +   }
  95.447 +
  95.448 +   /* Write filter_method 64 (intrapixel differencing) only if
  95.449 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  95.450 +    * 2. Libpng did not write a PNG signature (this filter_method is only
  95.451 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  95.452 +    * 3. The application called png_permit_mng_features with a mask that
  95.453 +    *    included PNG_FLAG_MNG_FILTER_64 and
  95.454 +    * 4. The filter_method is 64 and
  95.455 +    * 5. The color_type is RGB or RGBA
  95.456 +    */
  95.457 +   if (
  95.458 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  95.459 +      !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  95.460 +      ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  95.461 +      (color_type == PNG_COLOR_TYPE_RGB ||
  95.462 +       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  95.463 +      (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
  95.464 +#endif
  95.465 +      filter_type != PNG_FILTER_TYPE_BASE)
  95.466 +   {
  95.467 +      png_warning(png_ptr, "Invalid filter type specified");
  95.468 +      filter_type = PNG_FILTER_TYPE_BASE;
  95.469 +   }
  95.470 +
  95.471 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
  95.472 +   if (interlace_type != PNG_INTERLACE_NONE &&
  95.473 +      interlace_type != PNG_INTERLACE_ADAM7)
  95.474 +   {
  95.475 +      png_warning(png_ptr, "Invalid interlace type specified");
  95.476 +      interlace_type = PNG_INTERLACE_ADAM7;
  95.477 +   }
  95.478 +#else
  95.479 +   interlace_type=PNG_INTERLACE_NONE;
  95.480 +#endif
  95.481 +
  95.482 +   /* save off the relevent information */
  95.483 +   png_ptr->bit_depth = (png_byte)bit_depth;
  95.484 +   png_ptr->color_type = (png_byte)color_type;
  95.485 +   png_ptr->interlaced = (png_byte)interlace_type;
  95.486 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  95.487 +   png_ptr->filter_type = (png_byte)filter_type;
  95.488 +#endif
  95.489 +   png_ptr->compression_type = (png_byte)compression_type;
  95.490 +   png_ptr->width = width;
  95.491 +   png_ptr->height = height;
  95.492 +
  95.493 +   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
  95.494 +   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
  95.495 +   /* set the usr info, so any transformations can modify it */
  95.496 +   png_ptr->usr_width = png_ptr->width;
  95.497 +   png_ptr->usr_bit_depth = png_ptr->bit_depth;
  95.498 +   png_ptr->usr_channels = png_ptr->channels;
  95.499 +
  95.500 +   /* pack the header information into the buffer */
  95.501 +   png_save_uint_32(buf, width);
  95.502 +   png_save_uint_32(buf + 4, height);
  95.503 +   buf[8] = (png_byte)bit_depth;
  95.504 +   buf[9] = (png_byte)color_type;
  95.505 +   buf[10] = (png_byte)compression_type;
  95.506 +   buf[11] = (png_byte)filter_type;
  95.507 +   buf[12] = (png_byte)interlace_type;
  95.508 +
  95.509 +   /* write the chunk */
  95.510 +   png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
  95.511 +
  95.512 +   /* initialize zlib with PNG info */
  95.513 +   png_ptr->zstream.zalloc = png_zalloc;
  95.514 +   png_ptr->zstream.zfree = png_zfree;
  95.515 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  95.516 +   if (!(png_ptr->do_filter))
  95.517 +   {
  95.518 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  95.519 +         png_ptr->bit_depth < 8)
  95.520 +         png_ptr->do_filter = PNG_FILTER_NONE;
  95.521 +      else
  95.522 +         png_ptr->do_filter = PNG_ALL_FILTERS;
  95.523 +   }
  95.524 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
  95.525 +   {
  95.526 +      if (png_ptr->do_filter != PNG_FILTER_NONE)
  95.527 +         png_ptr->zlib_strategy = Z_FILTERED;
  95.528 +      else
  95.529 +         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
  95.530 +   }
  95.531 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
  95.532 +      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
  95.533 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
  95.534 +      png_ptr->zlib_mem_level = 8;
  95.535 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
  95.536 +      png_ptr->zlib_window_bits = 15;
  95.537 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
  95.538 +      png_ptr->zlib_method = 8;
  95.539 +   ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
  95.540 +         png_ptr->zlib_method, png_ptr->zlib_window_bits,
  95.541 +         png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
  95.542 +   if (ret != Z_OK)
  95.543 +   {
  95.544 +      if (ret == Z_VERSION_ERROR) png_error(png_ptr,
  95.545 +          "zlib failed to initialize compressor -- version error");
  95.546 +      if (ret == Z_STREAM_ERROR) png_error(png_ptr,
  95.547 +           "zlib failed to initialize compressor -- stream error");
  95.548 +      if (ret == Z_MEM_ERROR) png_error(png_ptr,
  95.549 +           "zlib failed to initialize compressor -- mem error");
  95.550 +      png_error(png_ptr, "zlib failed to initialize compressor");
  95.551 +   }
  95.552 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  95.553 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  95.554 +   /* libpng is not interested in zstream.data_type */
  95.555 +   /* set it to a predefined value, to avoid its evaluation inside zlib */
  95.556 +   png_ptr->zstream.data_type = Z_BINARY;
  95.557 +
  95.558 +   png_ptr->mode = PNG_HAVE_IHDR;
  95.559 +}
  95.560 +
  95.561 +/* write the palette.  We are careful not to trust png_color to be in the
  95.562 + * correct order for PNG, so people can redefine it to any convenient
  95.563 + * structure.
  95.564 + */
  95.565 +void /* PRIVATE */
  95.566 +png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
  95.567 +{
  95.568 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.569 +   PNG_PLTE;
  95.570 +#endif
  95.571 +   png_uint_32 i;
  95.572 +   png_colorp pal_ptr;
  95.573 +   png_byte buf[3];
  95.574 +
  95.575 +   png_debug(1, "in png_write_PLTE\n");
  95.576 +   if ((
  95.577 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  95.578 +        !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
  95.579 +#endif
  95.580 +        num_pal == 0) || num_pal > 256)
  95.581 +   {
  95.582 +     if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  95.583 +     {
  95.584 +        png_error(png_ptr, "Invalid number of colors in palette");
  95.585 +     }
  95.586 +     else
  95.587 +     {
  95.588 +        png_warning(png_ptr, "Invalid number of colors in palette");
  95.589 +        return;
  95.590 +     }
  95.591 +   }
  95.592 +
  95.593 +   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  95.594 +   {
  95.595 +      png_warning(png_ptr,
  95.596 +        "Ignoring request to write a PLTE chunk in grayscale PNG");
  95.597 +      return;
  95.598 +   }
  95.599 +
  95.600 +   png_ptr->num_palette = (png_uint_16)num_pal;
  95.601 +   png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
  95.602 +
  95.603 +   png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
  95.604 +     (png_uint_32)(num_pal * 3));
  95.605 +#ifndef PNG_NO_POINTER_INDEXING
  95.606 +   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
  95.607 +   {
  95.608 +      buf[0] = pal_ptr->red;
  95.609 +      buf[1] = pal_ptr->green;
  95.610 +      buf[2] = pal_ptr->blue;
  95.611 +      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  95.612 +   }
  95.613 +#else
  95.614 +   /* This is a little slower but some buggy compilers need to do this instead */
  95.615 +   pal_ptr=palette;
  95.616 +   for (i = 0; i < num_pal; i++)
  95.617 +   {
  95.618 +      buf[0] = pal_ptr[i].red;
  95.619 +      buf[1] = pal_ptr[i].green;
  95.620 +      buf[2] = pal_ptr[i].blue;
  95.621 +      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  95.622 +   }
  95.623 +#endif
  95.624 +   png_write_chunk_end(png_ptr);
  95.625 +   png_ptr->mode |= PNG_HAVE_PLTE;
  95.626 +}
  95.627 +
  95.628 +/* write an IDAT chunk */
  95.629 +void /* PRIVATE */
  95.630 +png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
  95.631 +{
  95.632 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.633 +   PNG_IDAT;
  95.634 +#endif
  95.635 +   png_debug(1, "in png_write_IDAT\n");
  95.636 +
  95.637 +   /* Optimize the CMF field in the zlib stream. */
  95.638 +   /* This hack of the zlib stream is compliant to the stream specification. */
  95.639 +   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
  95.640 +       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
  95.641 +   {
  95.642 +      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
  95.643 +      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
  95.644 +      {
  95.645 +         /* Avoid memory underflows and multiplication overflows. */
  95.646 +         /* The conditions below are practically always satisfied;
  95.647 +            however, they still must be checked. */
  95.648 +         if (length >= 2 &&
  95.649 +             png_ptr->height < 16384 && png_ptr->width < 16384)
  95.650 +         {
  95.651 +            png_uint_32 uncompressed_idat_size = png_ptr->height *
  95.652 +               ((png_ptr->width *
  95.653 +               png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
  95.654 +            unsigned int z_cinfo = z_cmf >> 4;
  95.655 +            unsigned int half_z_window_size = 1 << (z_cinfo + 7);
  95.656 +            while (uncompressed_idat_size <= half_z_window_size &&
  95.657 +                   half_z_window_size >= 256)
  95.658 +            {
  95.659 +               z_cinfo--;
  95.660 +               half_z_window_size >>= 1;
  95.661 +            }
  95.662 +            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
  95.663 +            if (data[0] != (png_byte)z_cmf)
  95.664 +            {
  95.665 +               data[0] = (png_byte)z_cmf;
  95.666 +               data[1] &= 0xe0;
  95.667 +               data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
  95.668 +            }
  95.669 +         }
  95.670 +      }
  95.671 +      else
  95.672 +         png_error(png_ptr,
  95.673 +            "Invalid zlib compression method or flags in IDAT");
  95.674 +   }
  95.675 +
  95.676 +   png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
  95.677 +   png_ptr->mode |= PNG_HAVE_IDAT;
  95.678 +}
  95.679 +
  95.680 +/* write an IEND chunk */
  95.681 +void /* PRIVATE */
  95.682 +png_write_IEND(png_structp png_ptr)
  95.683 +{
  95.684 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.685 +   PNG_IEND;
  95.686 +#endif
  95.687 +   png_debug(1, "in png_write_IEND\n");
  95.688 +   png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
  95.689 +     (png_size_t)0);
  95.690 +   png_ptr->mode |= PNG_HAVE_IEND;
  95.691 +}
  95.692 +
  95.693 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
  95.694 +/* write a gAMA chunk */
  95.695 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  95.696 +void /* PRIVATE */
  95.697 +png_write_gAMA(png_structp png_ptr, double file_gamma)
  95.698 +{
  95.699 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.700 +   PNG_gAMA;
  95.701 +#endif
  95.702 +   png_uint_32 igamma;
  95.703 +   png_byte buf[4];
  95.704 +
  95.705 +   png_debug(1, "in png_write_gAMA\n");
  95.706 +   /* file_gamma is saved in 1/100,000ths */
  95.707 +   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
  95.708 +   png_save_uint_32(buf, igamma);
  95.709 +   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
  95.710 +}
  95.711 +#endif
  95.712 +#ifdef PNG_FIXED_POINT_SUPPORTED
  95.713 +void /* PRIVATE */
  95.714 +png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
  95.715 +{
  95.716 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.717 +   PNG_gAMA;
  95.718 +#endif
  95.719 +   png_byte buf[4];
  95.720 +
  95.721 +   png_debug(1, "in png_write_gAMA\n");
  95.722 +   /* file_gamma is saved in 1/100,000ths */
  95.723 +   png_save_uint_32(buf, (png_uint_32)file_gamma);
  95.724 +   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
  95.725 +}
  95.726 +#endif
  95.727 +#endif
  95.728 +
  95.729 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
  95.730 +/* write a sRGB chunk */
  95.731 +void /* PRIVATE */
  95.732 +png_write_sRGB(png_structp png_ptr, int srgb_intent)
  95.733 +{
  95.734 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.735 +   PNG_sRGB;
  95.736 +#endif
  95.737 +   png_byte buf[1];
  95.738 +
  95.739 +   png_debug(1, "in png_write_sRGB\n");
  95.740 +   if (srgb_intent >= PNG_sRGB_INTENT_LAST)
  95.741 +         png_warning(png_ptr,
  95.742 +            "Invalid sRGB rendering intent specified");
  95.743 +   buf[0]=(png_byte)srgb_intent;
  95.744 +   png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
  95.745 +}
  95.746 +#endif
  95.747 +
  95.748 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
  95.749 +/* write an iCCP chunk */
  95.750 +void /* PRIVATE */
  95.751 +png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
  95.752 +   png_charp profile, int profile_len)
  95.753 +{
  95.754 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.755 +   PNG_iCCP;
  95.756 +#endif
  95.757 +   png_size_t name_len;
  95.758 +   png_charp new_name;
  95.759 +   compression_state comp;
  95.760 +   int embedded_profile_len = 0;
  95.761 +
  95.762 +   png_debug(1, "in png_write_iCCP\n");
  95.763 +
  95.764 +   comp.num_output_ptr = 0;
  95.765 +   comp.max_output_ptr = 0;
  95.766 +   comp.output_ptr = NULL;
  95.767 +   comp.input = NULL;
  95.768 +   comp.input_len = 0;
  95.769 +
  95.770 +   if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
  95.771 +      &new_name)) == 0)
  95.772 +   {
  95.773 +      png_warning(png_ptr, "Empty keyword in iCCP chunk");
  95.774 +      return;
  95.775 +   }
  95.776 +
  95.777 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  95.778 +      png_warning(png_ptr, "Unknown compression type in iCCP chunk");
  95.779 +
  95.780 +   if (profile == NULL)
  95.781 +      profile_len = 0;
  95.782 +
  95.783 +   if (profile_len > 3)
  95.784 +      embedded_profile_len =
  95.785 +          ((*( (png_bytep)profile    ))<<24) |
  95.786 +          ((*( (png_bytep)profile + 1))<<16) |
  95.787 +          ((*( (png_bytep)profile + 2))<< 8) |
  95.788 +          ((*( (png_bytep)profile + 3))    );
  95.789 +
  95.790 +   if (profile_len < embedded_profile_len)
  95.791 +   {
  95.792 +      png_warning(png_ptr,
  95.793 +        "Embedded profile length too large in iCCP chunk");
  95.794 +      return;
  95.795 +   }
  95.796 +
  95.797 +   if (profile_len > embedded_profile_len)
  95.798 +   {
  95.799 +      png_warning(png_ptr,
  95.800 +        "Truncating profile to actual length in iCCP chunk");
  95.801 +      profile_len = embedded_profile_len;
  95.802 +   }
  95.803 +
  95.804 +   if (profile_len)
  95.805 +      profile_len = png_text_compress(png_ptr, profile,
  95.806 +        (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
  95.807 +
  95.808 +   /* make sure we include the NULL after the name and the compression type */
  95.809 +   png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
  95.810 +          (png_uint_32)(name_len + profile_len + 2));
  95.811 +   new_name[name_len + 1] = 0x00;
  95.812 +   png_write_chunk_data(png_ptr, (png_bytep)new_name,
  95.813 +     (png_size_t)(name_len + 2));
  95.814 +
  95.815 +   if (profile_len)
  95.816 +      png_write_compressed_data_out(png_ptr, &comp);
  95.817 +
  95.818 +   png_write_chunk_end(png_ptr);
  95.819 +   png_free(png_ptr, new_name);
  95.820 +}
  95.821 +#endif
  95.822 +
  95.823 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
  95.824 +/* write a sPLT chunk */
  95.825 +void /* PRIVATE */
  95.826 +png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
  95.827 +{
  95.828 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.829 +   PNG_sPLT;
  95.830 +#endif
  95.831 +   png_size_t name_len;
  95.832 +   png_charp new_name;
  95.833 +   png_byte entrybuf[10];
  95.834 +   int entry_size = (spalette->depth == 8 ? 6 : 10);
  95.835 +   int palette_size = entry_size * spalette->nentries;
  95.836 +   png_sPLT_entryp ep;
  95.837 +#ifdef PNG_NO_POINTER_INDEXING
  95.838 +   int i;
  95.839 +#endif
  95.840 +
  95.841 +   png_debug(1, "in png_write_sPLT\n");
  95.842 +   if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
  95.843 +      spalette->name, &new_name))==0)
  95.844 +   {
  95.845 +      png_warning(png_ptr, "Empty keyword in sPLT chunk");
  95.846 +      return;
  95.847 +   }
  95.848 +
  95.849 +   /* make sure we include the NULL after the name */
  95.850 +   png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
  95.851 +     (png_uint_32)(name_len + 2 + palette_size));
  95.852 +   png_write_chunk_data(png_ptr, (png_bytep)new_name,
  95.853 +     (png_size_t)(name_len + 1));
  95.854 +   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
  95.855 +
  95.856 +   /* loop through each palette entry, writing appropriately */
  95.857 +#ifndef PNG_NO_POINTER_INDEXING
  95.858 +   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
  95.859 +   {
  95.860 +      if (spalette->depth == 8)
  95.861 +      {
  95.862 +          entrybuf[0] = (png_byte)ep->red;
  95.863 +          entrybuf[1] = (png_byte)ep->green;
  95.864 +          entrybuf[2] = (png_byte)ep->blue;
  95.865 +          entrybuf[3] = (png_byte)ep->alpha;
  95.866 +          png_save_uint_16(entrybuf + 4, ep->frequency);
  95.867 +      }
  95.868 +      else
  95.869 +      {
  95.870 +          png_save_uint_16(entrybuf + 0, ep->red);
  95.871 +          png_save_uint_16(entrybuf + 2, ep->green);
  95.872 +          png_save_uint_16(entrybuf + 4, ep->blue);
  95.873 +          png_save_uint_16(entrybuf + 6, ep->alpha);
  95.874 +          png_save_uint_16(entrybuf + 8, ep->frequency);
  95.875 +      }
  95.876 +      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
  95.877 +   }
  95.878 +#else
  95.879 +   ep=spalette->entries;
  95.880 +   for (i=0; i>spalette->nentries; i++)
  95.881 +   {
  95.882 +      if (spalette->depth == 8)
  95.883 +      {
  95.884 +          entrybuf[0] = (png_byte)ep[i].red;
  95.885 +          entrybuf[1] = (png_byte)ep[i].green;
  95.886 +          entrybuf[2] = (png_byte)ep[i].blue;
  95.887 +          entrybuf[3] = (png_byte)ep[i].alpha;
  95.888 +          png_save_uint_16(entrybuf + 4, ep[i].frequency);
  95.889 +      }
  95.890 +      else
  95.891 +      {
  95.892 +          png_save_uint_16(entrybuf + 0, ep[i].red);
  95.893 +          png_save_uint_16(entrybuf + 2, ep[i].green);
  95.894 +          png_save_uint_16(entrybuf + 4, ep[i].blue);
  95.895 +          png_save_uint_16(entrybuf + 6, ep[i].alpha);
  95.896 +          png_save_uint_16(entrybuf + 8, ep[i].frequency);
  95.897 +      }
  95.898 +      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
  95.899 +   }
  95.900 +#endif
  95.901 +
  95.902 +   png_write_chunk_end(png_ptr);
  95.903 +   png_free(png_ptr, new_name);
  95.904 +}
  95.905 +#endif
  95.906 +
  95.907 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
  95.908 +/* write the sBIT chunk */
  95.909 +void /* PRIVATE */
  95.910 +png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
  95.911 +{
  95.912 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.913 +   PNG_sBIT;
  95.914 +#endif
  95.915 +   png_byte buf[4];
  95.916 +   png_size_t size;
  95.917 +
  95.918 +   png_debug(1, "in png_write_sBIT\n");
  95.919 +   /* make sure we don't depend upon the order of PNG_COLOR_8 */
  95.920 +   if (color_type & PNG_COLOR_MASK_COLOR)
  95.921 +   {
  95.922 +      png_byte maxbits;
  95.923 +
  95.924 +      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
  95.925 +                png_ptr->usr_bit_depth);
  95.926 +      if (sbit->red == 0 || sbit->red > maxbits ||
  95.927 +          sbit->green == 0 || sbit->green > maxbits ||
  95.928 +          sbit->blue == 0 || sbit->blue > maxbits)
  95.929 +      {
  95.930 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  95.931 +         return;
  95.932 +      }
  95.933 +      buf[0] = sbit->red;
  95.934 +      buf[1] = sbit->green;
  95.935 +      buf[2] = sbit->blue;
  95.936 +      size = 3;
  95.937 +   }
  95.938 +   else
  95.939 +   {
  95.940 +      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
  95.941 +      {
  95.942 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  95.943 +         return;
  95.944 +      }
  95.945 +      buf[0] = sbit->gray;
  95.946 +      size = 1;
  95.947 +   }
  95.948 +
  95.949 +   if (color_type & PNG_COLOR_MASK_ALPHA)
  95.950 +   {
  95.951 +      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
  95.952 +      {
  95.953 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  95.954 +         return;
  95.955 +      }
  95.956 +      buf[size++] = sbit->alpha;
  95.957 +   }
  95.958 +
  95.959 +   png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
  95.960 +}
  95.961 +#endif
  95.962 +
  95.963 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
  95.964 +/* write the cHRM chunk */
  95.965 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  95.966 +void /* PRIVATE */
  95.967 +png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
  95.968 +   double red_x, double red_y, double green_x, double green_y,
  95.969 +   double blue_x, double blue_y)
  95.970 +{
  95.971 +#ifdef PNG_USE_LOCAL_ARRAYS
  95.972 +   PNG_cHRM;
  95.973 +#endif
  95.974 +   png_byte buf[32];
  95.975 +   png_uint_32 itemp;
  95.976 +
  95.977 +   png_debug(1, "in png_write_cHRM\n");
  95.978 +   /* each value is saved in 1/100,000ths */
  95.979 +   if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
  95.980 +       white_x + white_y > 1.0)
  95.981 +   {
  95.982 +      png_warning(png_ptr, "Invalid cHRM white point specified");
  95.983 +#if !defined(PNG_NO_CONSOLE_IO)
  95.984 +      fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
  95.985 +#endif
  95.986 +      return;
  95.987 +   }
  95.988 +   itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
  95.989 +   png_save_uint_32(buf, itemp);
  95.990 +   itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
  95.991 +   png_save_uint_32(buf + 4, itemp);
  95.992 +
  95.993 +   if (red_x < 0 ||  red_y < 0 || red_x + red_y > 1.0)
  95.994 +   {
  95.995 +      png_warning(png_ptr, "Invalid cHRM red point specified");
  95.996 +      return;
  95.997 +   }
  95.998 +   itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
  95.999 +   png_save_uint_32(buf + 8, itemp);
 95.1000 +   itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
 95.1001 +   png_save_uint_32(buf + 12, itemp);
 95.1002 +
 95.1003 +   if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
 95.1004 +   {
 95.1005 +      png_warning(png_ptr, "Invalid cHRM green point specified");
 95.1006 +      return;
 95.1007 +   }
 95.1008 +   itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
 95.1009 +   png_save_uint_32(buf + 16, itemp);
 95.1010 +   itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
 95.1011 +   png_save_uint_32(buf + 20, itemp);
 95.1012 +
 95.1013 +   if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
 95.1014 +   {
 95.1015 +      png_warning(png_ptr, "Invalid cHRM blue point specified");
 95.1016 +      return;
 95.1017 +   }
 95.1018 +   itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
 95.1019 +   png_save_uint_32(buf + 24, itemp);
 95.1020 +   itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
 95.1021 +   png_save_uint_32(buf + 28, itemp);
 95.1022 +
 95.1023 +   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 95.1024 +}
 95.1025 +#endif
 95.1026 +#ifdef PNG_FIXED_POINT_SUPPORTED
 95.1027 +void /* PRIVATE */
 95.1028 +png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
 95.1029 +   png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
 95.1030 +   png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
 95.1031 +   png_fixed_point blue_y)
 95.1032 +{
 95.1033 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1034 +   PNG_cHRM;
 95.1035 +#endif
 95.1036 +   png_byte buf[32];
 95.1037 +
 95.1038 +   png_debug(1, "in png_write_cHRM\n");
 95.1039 +   /* each value is saved in 1/100,000ths */
 95.1040 +   if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
 95.1041 +   {
 95.1042 +      png_warning(png_ptr, "Invalid fixed cHRM white point specified");
 95.1043 +#if !defined(PNG_NO_CONSOLE_IO)
 95.1044 +      fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
 95.1045 +        (unsigned long)white_y);
 95.1046 +#endif
 95.1047 +      return;
 95.1048 +   }
 95.1049 +   png_save_uint_32(buf, (png_uint_32)white_x);
 95.1050 +   png_save_uint_32(buf + 4, (png_uint_32)white_y);
 95.1051 +
 95.1052 +   if (red_x + red_y > 100000L)
 95.1053 +   {
 95.1054 +      png_warning(png_ptr, "Invalid cHRM fixed red point specified");
 95.1055 +      return;
 95.1056 +   }
 95.1057 +   png_save_uint_32(buf + 8, (png_uint_32)red_x);
 95.1058 +   png_save_uint_32(buf + 12, (png_uint_32)red_y);
 95.1059 +
 95.1060 +   if (green_x + green_y > 100000L)
 95.1061 +   {
 95.1062 +      png_warning(png_ptr, "Invalid fixed cHRM green point specified");
 95.1063 +      return;
 95.1064 +   }
 95.1065 +   png_save_uint_32(buf + 16, (png_uint_32)green_x);
 95.1066 +   png_save_uint_32(buf + 20, (png_uint_32)green_y);
 95.1067 +
 95.1068 +   if (blue_x + blue_y > 100000L)
 95.1069 +   {
 95.1070 +      png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
 95.1071 +      return;
 95.1072 +   }
 95.1073 +   png_save_uint_32(buf + 24, (png_uint_32)blue_x);
 95.1074 +   png_save_uint_32(buf + 28, (png_uint_32)blue_y);
 95.1075 +
 95.1076 +   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 95.1077 +}
 95.1078 +#endif
 95.1079 +#endif
 95.1080 +
 95.1081 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
 95.1082 +/* write the tRNS chunk */
 95.1083 +void /* PRIVATE */
 95.1084 +png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
 95.1085 +   int num_trans, int color_type)
 95.1086 +{
 95.1087 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1088 +   PNG_tRNS;
 95.1089 +#endif
 95.1090 +   png_byte buf[6];
 95.1091 +
 95.1092 +   png_debug(1, "in png_write_tRNS\n");
 95.1093 +   if (color_type == PNG_COLOR_TYPE_PALETTE)
 95.1094 +   {
 95.1095 +      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
 95.1096 +      {
 95.1097 +         png_warning(png_ptr, "Invalid number of transparent colors specified");
 95.1098 +         return;
 95.1099 +      }
 95.1100 +      /* write the chunk out as it is */
 95.1101 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
 95.1102 +        (png_size_t)num_trans);
 95.1103 +   }
 95.1104 +   else if (color_type == PNG_COLOR_TYPE_GRAY)
 95.1105 +   {
 95.1106 +      /* one 16 bit value */
 95.1107 +      if (tran->gray >= (1 << png_ptr->bit_depth))
 95.1108 +      {
 95.1109 +         png_warning(png_ptr,
 95.1110 +           "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
 95.1111 +         return;
 95.1112 +      }
 95.1113 +      png_save_uint_16(buf, tran->gray);
 95.1114 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
 95.1115 +   }
 95.1116 +   else if (color_type == PNG_COLOR_TYPE_RGB)
 95.1117 +   {
 95.1118 +      /* three 16 bit values */
 95.1119 +      png_save_uint_16(buf, tran->red);
 95.1120 +      png_save_uint_16(buf + 2, tran->green);
 95.1121 +      png_save_uint_16(buf + 4, tran->blue);
 95.1122 +      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
 95.1123 +      {
 95.1124 +         png_warning(png_ptr,
 95.1125 +           "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
 95.1126 +         return;
 95.1127 +      }
 95.1128 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
 95.1129 +   }
 95.1130 +   else
 95.1131 +   {
 95.1132 +      png_warning(png_ptr, "Can't write tRNS with an alpha channel");
 95.1133 +   }
 95.1134 +}
 95.1135 +#endif
 95.1136 +
 95.1137 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
 95.1138 +/* write the background chunk */
 95.1139 +void /* PRIVATE */
 95.1140 +png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
 95.1141 +{
 95.1142 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1143 +   PNG_bKGD;
 95.1144 +#endif
 95.1145 +   png_byte buf[6];
 95.1146 +
 95.1147 +   png_debug(1, "in png_write_bKGD\n");
 95.1148 +   if (color_type == PNG_COLOR_TYPE_PALETTE)
 95.1149 +   {
 95.1150 +      if (
 95.1151 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 95.1152 +          (png_ptr->num_palette ||
 95.1153 +          (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
 95.1154 +#endif
 95.1155 +         back->index > png_ptr->num_palette)
 95.1156 +      {
 95.1157 +         png_warning(png_ptr, "Invalid background palette index");
 95.1158 +         return;
 95.1159 +      }
 95.1160 +      buf[0] = back->index;
 95.1161 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
 95.1162 +   }
 95.1163 +   else if (color_type & PNG_COLOR_MASK_COLOR)
 95.1164 +   {
 95.1165 +      png_save_uint_16(buf, back->red);
 95.1166 +      png_save_uint_16(buf + 2, back->green);
 95.1167 +      png_save_uint_16(buf + 4, back->blue);
 95.1168 +      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
 95.1169 +      {
 95.1170 +         png_warning(png_ptr,
 95.1171 +           "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
 95.1172 +         return;
 95.1173 +      }
 95.1174 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
 95.1175 +   }
 95.1176 +   else
 95.1177 +   {
 95.1178 +      if (back->gray >= (1 << png_ptr->bit_depth))
 95.1179 +      {
 95.1180 +         png_warning(png_ptr,
 95.1181 +           "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
 95.1182 +         return;
 95.1183 +      }
 95.1184 +      png_save_uint_16(buf, back->gray);
 95.1185 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
 95.1186 +   }
 95.1187 +}
 95.1188 +#endif
 95.1189 +
 95.1190 +#if defined(PNG_WRITE_hIST_SUPPORTED)
 95.1191 +/* write the histogram */
 95.1192 +void /* PRIVATE */
 95.1193 +png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
 95.1194 +{
 95.1195 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1196 +   PNG_hIST;
 95.1197 +#endif
 95.1198 +   int i;
 95.1199 +   png_byte buf[3];
 95.1200 +
 95.1201 +   png_debug(1, "in png_write_hIST\n");
 95.1202 +   if (num_hist > (int)png_ptr->num_palette)
 95.1203 +   {
 95.1204 +      png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
 95.1205 +         png_ptr->num_palette);
 95.1206 +      png_warning(png_ptr, "Invalid number of histogram entries specified");
 95.1207 +      return;
 95.1208 +   }
 95.1209 +
 95.1210 +   png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
 95.1211 +     (png_uint_32)(num_hist * 2));
 95.1212 +   for (i = 0; i < num_hist; i++)
 95.1213 +   {
 95.1214 +      png_save_uint_16(buf, hist[i]);
 95.1215 +      png_write_chunk_data(png_ptr, buf, (png_size_t)2);
 95.1216 +   }
 95.1217 +   png_write_chunk_end(png_ptr);
 95.1218 +}
 95.1219 +#endif
 95.1220 +
 95.1221 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
 95.1222 +    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
 95.1223 +/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
 95.1224 + * and if invalid, correct the keyword rather than discarding the entire
 95.1225 + * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
 95.1226 + * length, forbids leading or trailing whitespace, multiple internal spaces,
 95.1227 + * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
 95.1228 + *
 95.1229 + * The new_key is allocated to hold the corrected keyword and must be freed
 95.1230 + * by the calling routine.  This avoids problems with trying to write to
 95.1231 + * static keywords without having to have duplicate copies of the strings.
 95.1232 + */
 95.1233 +png_size_t /* PRIVATE */
 95.1234 +png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
 95.1235 +{
 95.1236 +   png_size_t key_len;
 95.1237 +   png_charp kp, dp;
 95.1238 +   int kflag;
 95.1239 +   int kwarn=0;
 95.1240 +
 95.1241 +   png_debug(1, "in png_check_keyword\n");
 95.1242 +   *new_key = NULL;
 95.1243 +
 95.1244 +   if (key == NULL || (key_len = png_strlen(key)) == 0)
 95.1245 +   {
 95.1246 +      png_warning(png_ptr, "zero length keyword");
 95.1247 +      return ((png_size_t)0);
 95.1248 +   }
 95.1249 +
 95.1250 +   png_debug1(2, "Keyword to be checked is '%s'\n", key);
 95.1251 +
 95.1252 +   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
 95.1253 +   if (*new_key == NULL)
 95.1254 +   {
 95.1255 +      png_warning(png_ptr, "Out of memory while procesing keyword");
 95.1256 +      return ((png_size_t)0);
 95.1257 +   }
 95.1258 +
 95.1259 +   /* Replace non-printing characters with a blank and print a warning */
 95.1260 +   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
 95.1261 +   {
 95.1262 +      if ((png_byte)*kp < 0x20 ||
 95.1263 +         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
 95.1264 +      {
 95.1265 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
 95.1266 +         char msg[40];
 95.1267 +
 95.1268 +         png_snprintf(msg, 40,
 95.1269 +           "invalid keyword character 0x%02X", (png_byte)*kp);
 95.1270 +         png_warning(png_ptr, msg);
 95.1271 +#else
 95.1272 +         png_warning(png_ptr, "invalid character in keyword");
 95.1273 +#endif
 95.1274 +         *dp = ' ';
 95.1275 +      }
 95.1276 +      else
 95.1277 +      {
 95.1278 +         *dp = *kp;
 95.1279 +      }
 95.1280 +   }
 95.1281 +   *dp = '\0';
 95.1282 +
 95.1283 +   /* Remove any trailing white space. */
 95.1284 +   kp = *new_key + key_len - 1;
 95.1285 +   if (*kp == ' ')
 95.1286 +   {
 95.1287 +      png_warning(png_ptr, "trailing spaces removed from keyword");
 95.1288 +
 95.1289 +      while (*kp == ' ')
 95.1290 +      {
 95.1291 +        *(kp--) = '\0';
 95.1292 +        key_len--;
 95.1293 +      }
 95.1294 +   }
 95.1295 +
 95.1296 +   /* Remove any leading white space. */
 95.1297 +   kp = *new_key;
 95.1298 +   if (*kp == ' ')
 95.1299 +   {
 95.1300 +      png_warning(png_ptr, "leading spaces removed from keyword");
 95.1301 +
 95.1302 +      while (*kp == ' ')
 95.1303 +      {
 95.1304 +        kp++;
 95.1305 +        key_len--;
 95.1306 +      }
 95.1307 +   }
 95.1308 +
 95.1309 +   png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
 95.1310 +
 95.1311 +   /* Remove multiple internal spaces. */
 95.1312 +   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
 95.1313 +   {
 95.1314 +      if (*kp == ' ' && kflag == 0)
 95.1315 +      {
 95.1316 +         *(dp++) = *kp;
 95.1317 +         kflag = 1;
 95.1318 +      }
 95.1319 +      else if (*kp == ' ')
 95.1320 +      {
 95.1321 +         key_len--;
 95.1322 +         kwarn=1;
 95.1323 +      }
 95.1324 +      else
 95.1325 +      {
 95.1326 +         *(dp++) = *kp;
 95.1327 +         kflag = 0;
 95.1328 +      }
 95.1329 +   }
 95.1330 +   *dp = '\0';
 95.1331 +   if (kwarn)
 95.1332 +      png_warning(png_ptr, "extra interior spaces removed from keyword");
 95.1333 +
 95.1334 +   if (key_len == 0)
 95.1335 +   {
 95.1336 +      png_free(png_ptr, *new_key);
 95.1337 +       *new_key=NULL;
 95.1338 +      png_warning(png_ptr, "Zero length keyword");
 95.1339 +   }
 95.1340 +
 95.1341 +   if (key_len > 79)
 95.1342 +   {
 95.1343 +      png_warning(png_ptr, "keyword length must be 1 - 79 characters");
 95.1344 +      new_key[79] = '\0';
 95.1345 +      key_len = 79;
 95.1346 +   }
 95.1347 +
 95.1348 +   return (key_len);
 95.1349 +}
 95.1350 +#endif
 95.1351 +
 95.1352 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
 95.1353 +/* write a tEXt chunk */
 95.1354 +void /* PRIVATE */
 95.1355 +png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
 95.1356 +   png_size_t text_len)
 95.1357 +{
 95.1358 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1359 +   PNG_tEXt;
 95.1360 +#endif
 95.1361 +   png_size_t key_len;
 95.1362 +   png_charp new_key;
 95.1363 +
 95.1364 +   png_debug(1, "in png_write_tEXt\n");
 95.1365 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 95.1366 +   {
 95.1367 +      png_warning(png_ptr, "Empty keyword in tEXt chunk");
 95.1368 +      return;
 95.1369 +   }
 95.1370 +
 95.1371 +   if (text == NULL || *text == '\0')
 95.1372 +      text_len = 0;
 95.1373 +   else
 95.1374 +      text_len = png_strlen(text);
 95.1375 +
 95.1376 +   /* make sure we include the 0 after the key */
 95.1377 +   png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
 95.1378 +      (png_uint_32)(key_len + text_len + 1));
 95.1379 +   /*
 95.1380 +    * We leave it to the application to meet PNG-1.0 requirements on the
 95.1381 +    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
 95.1382 +    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
 95.1383 +    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
 95.1384 +    */
 95.1385 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 95.1386 +     (png_size_t)(key_len + 1));
 95.1387 +   if (text_len)
 95.1388 +      png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
 95.1389 +
 95.1390 +   png_write_chunk_end(png_ptr);
 95.1391 +   png_free(png_ptr, new_key);
 95.1392 +}
 95.1393 +#endif
 95.1394 +
 95.1395 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
 95.1396 +/* write a compressed text chunk */
 95.1397 +void /* PRIVATE */
 95.1398 +png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
 95.1399 +   png_size_t text_len, int compression)
 95.1400 +{
 95.1401 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1402 +   PNG_zTXt;
 95.1403 +#endif
 95.1404 +   png_size_t key_len;
 95.1405 +   char buf[1];
 95.1406 +   png_charp new_key;
 95.1407 +   compression_state comp;
 95.1408 +
 95.1409 +   png_debug(1, "in png_write_zTXt\n");
 95.1410 +
 95.1411 +   comp.num_output_ptr = 0;
 95.1412 +   comp.max_output_ptr = 0;
 95.1413 +   comp.output_ptr = NULL;
 95.1414 +   comp.input = NULL;
 95.1415 +   comp.input_len = 0;
 95.1416 +
 95.1417 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 95.1418 +   {
 95.1419 +      png_warning(png_ptr, "Empty keyword in zTXt chunk");
 95.1420 +      png_free(png_ptr, new_key);
 95.1421 +      return;
 95.1422 +   }
 95.1423 +
 95.1424 +   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
 95.1425 +   {
 95.1426 +      png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
 95.1427 +      png_free(png_ptr, new_key);
 95.1428 +      return;
 95.1429 +   }
 95.1430 +
 95.1431 +   text_len = png_strlen(text);
 95.1432 +
 95.1433 +   /* compute the compressed data; do it now for the length */
 95.1434 +   text_len = png_text_compress(png_ptr, text, text_len, compression,
 95.1435 +       &comp);
 95.1436 +
 95.1437 +   /* write start of chunk */
 95.1438 +   png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
 95.1439 +     (png_uint_32)(key_len+text_len + 2));
 95.1440 +   /* write key */
 95.1441 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 95.1442 +     (png_size_t)(key_len + 1));
 95.1443 +   png_free(png_ptr, new_key);
 95.1444 +
 95.1445 +   buf[0] = (png_byte)compression;
 95.1446 +   /* write compression */
 95.1447 +   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
 95.1448 +   /* write the compressed data */
 95.1449 +   png_write_compressed_data_out(png_ptr, &comp);
 95.1450 +
 95.1451 +   /* close the chunk */
 95.1452 +   png_write_chunk_end(png_ptr);
 95.1453 +}
 95.1454 +#endif
 95.1455 +
 95.1456 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
 95.1457 +/* write an iTXt chunk */
 95.1458 +void /* PRIVATE */
 95.1459 +png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
 95.1460 +    png_charp lang, png_charp lang_key, png_charp text)
 95.1461 +{
 95.1462 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1463 +   PNG_iTXt;
 95.1464 +#endif
 95.1465 +   png_size_t lang_len, key_len, lang_key_len, text_len;
 95.1466 +   png_charp new_lang, new_key;
 95.1467 +   png_byte cbuf[2];
 95.1468 +   compression_state comp;
 95.1469 +
 95.1470 +   png_debug(1, "in png_write_iTXt\n");
 95.1471 +
 95.1472 +   comp.num_output_ptr = 0;
 95.1473 +   comp.max_output_ptr = 0;
 95.1474 +   comp.output_ptr = NULL;
 95.1475 +   comp.input = NULL;
 95.1476 +
 95.1477 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 95.1478 +   {
 95.1479 +      png_warning(png_ptr, "Empty keyword in iTXt chunk");
 95.1480 +      return;
 95.1481 +   }
 95.1482 +   if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
 95.1483 +   {
 95.1484 +      png_warning(png_ptr, "Empty language field in iTXt chunk");
 95.1485 +      new_lang = NULL;
 95.1486 +      lang_len = 0;
 95.1487 +   }
 95.1488 +
 95.1489 +   if (lang_key == NULL)
 95.1490 +     lang_key_len = 0;
 95.1491 +   else
 95.1492 +     lang_key_len = png_strlen(lang_key);
 95.1493 +
 95.1494 +   if (text == NULL)
 95.1495 +      text_len = 0;
 95.1496 +   else
 95.1497 +     text_len = png_strlen(text);
 95.1498 +
 95.1499 +   /* compute the compressed data; do it now for the length */
 95.1500 +   text_len = png_text_compress(png_ptr, text, text_len, compression-2,
 95.1501 +      &comp);
 95.1502 +
 95.1503 +
 95.1504 +   /* make sure we include the compression flag, the compression byte,
 95.1505 +    * and the NULs after the key, lang, and lang_key parts */
 95.1506 +
 95.1507 +   png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
 95.1508 +          (png_uint_32)(
 95.1509 +        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
 95.1510 +        + key_len
 95.1511 +        + lang_len
 95.1512 +        + lang_key_len
 95.1513 +        + text_len));
 95.1514 +
 95.1515 +   /*
 95.1516 +    * We leave it to the application to meet PNG-1.0 requirements on the
 95.1517 +    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
 95.1518 +    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
 95.1519 +    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
 95.1520 +    */
 95.1521 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 95.1522 +     (png_size_t)(key_len + 1));
 95.1523 +
 95.1524 +   /* set the compression flag */
 95.1525 +   if (compression == PNG_ITXT_COMPRESSION_NONE || \
 95.1526 +       compression == PNG_TEXT_COMPRESSION_NONE)
 95.1527 +       cbuf[0] = 0;
 95.1528 +   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
 95.1529 +       cbuf[0] = 1;
 95.1530 +   /* set the compression method */
 95.1531 +   cbuf[1] = 0;
 95.1532 +   png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
 95.1533 +
 95.1534 +   cbuf[0] = 0;
 95.1535 +   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
 95.1536 +     (png_size_t)(lang_len + 1));
 95.1537 +   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
 95.1538 +     (png_size_t)(lang_key_len + 1));
 95.1539 +   png_write_compressed_data_out(png_ptr, &comp);
 95.1540 +
 95.1541 +   png_write_chunk_end(png_ptr);
 95.1542 +   png_free(png_ptr, new_key);
 95.1543 +   png_free(png_ptr, new_lang);
 95.1544 +}
 95.1545 +#endif
 95.1546 +
 95.1547 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
 95.1548 +/* write the oFFs chunk */
 95.1549 +void /* PRIVATE */
 95.1550 +png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
 95.1551 +   int unit_type)
 95.1552 +{
 95.1553 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1554 +   PNG_oFFs;
 95.1555 +#endif
 95.1556 +   png_byte buf[9];
 95.1557 +
 95.1558 +   png_debug(1, "in png_write_oFFs\n");
 95.1559 +   if (unit_type >= PNG_OFFSET_LAST)
 95.1560 +      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
 95.1561 +
 95.1562 +   png_save_int_32(buf, x_offset);
 95.1563 +   png_save_int_32(buf + 4, y_offset);
 95.1564 +   buf[8] = (png_byte)unit_type;
 95.1565 +
 95.1566 +   png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
 95.1567 +}
 95.1568 +#endif
 95.1569 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
 95.1570 +/* write the pCAL chunk (described in the PNG extensions document) */
 95.1571 +void /* PRIVATE */
 95.1572 +png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
 95.1573 +   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
 95.1574 +{
 95.1575 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1576 +   PNG_pCAL;
 95.1577 +#endif
 95.1578 +   png_size_t purpose_len, units_len, total_len;
 95.1579 +   png_uint_32p params_len;
 95.1580 +   png_byte buf[10];
 95.1581 +   png_charp new_purpose;
 95.1582 +   int i;
 95.1583 +
 95.1584 +   png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
 95.1585 +   if (type >= PNG_EQUATION_LAST)
 95.1586 +      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
 95.1587 +
 95.1588 +   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
 95.1589 +   png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
 95.1590 +   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
 95.1591 +   png_debug1(3, "pCAL units length = %d\n", (int)units_len);
 95.1592 +   total_len = purpose_len + units_len + 10;
 95.1593 +
 95.1594 +   params_len = (png_uint_32p)png_malloc(png_ptr,
 95.1595 +      (png_uint_32)(nparams * png_sizeof(png_uint_32)));
 95.1596 +
 95.1597 +   /* Find the length of each parameter, making sure we don't count the
 95.1598 +      null terminator for the last parameter. */
 95.1599 +   for (i = 0; i < nparams; i++)
 95.1600 +   {
 95.1601 +      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
 95.1602 +      png_debug2(3, "pCAL parameter %d length = %lu\n", i,
 95.1603 +        (unsigned long) params_len[i]);
 95.1604 +      total_len += (png_size_t)params_len[i];
 95.1605 +   }
 95.1606 +
 95.1607 +   png_debug1(3, "pCAL total length = %d\n", (int)total_len);
 95.1608 +   png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
 95.1609 +   png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
 95.1610 +     (png_size_t)purpose_len);
 95.1611 +   png_save_int_32(buf, X0);
 95.1612 +   png_save_int_32(buf + 4, X1);
 95.1613 +   buf[8] = (png_byte)type;
 95.1614 +   buf[9] = (png_byte)nparams;
 95.1615 +   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
 95.1616 +   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
 95.1617 +
 95.1618 +   png_free(png_ptr, new_purpose);
 95.1619 +
 95.1620 +   for (i = 0; i < nparams; i++)
 95.1621 +   {
 95.1622 +      png_write_chunk_data(png_ptr, (png_bytep)params[i],
 95.1623 +         (png_size_t)params_len[i]);
 95.1624 +   }
 95.1625 +
 95.1626 +   png_free(png_ptr, params_len);
 95.1627 +   png_write_chunk_end(png_ptr);
 95.1628 +}
 95.1629 +#endif
 95.1630 +
 95.1631 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
 95.1632 +/* write the sCAL chunk */
 95.1633 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
 95.1634 +void /* PRIVATE */
 95.1635 +png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
 95.1636 +{
 95.1637 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1638 +   PNG_sCAL;
 95.1639 +#endif
 95.1640 +   char buf[64];
 95.1641 +   png_size_t total_len;
 95.1642 +
 95.1643 +   png_debug(1, "in png_write_sCAL\n");
 95.1644 +
 95.1645 +   buf[0] = (char)unit;
 95.1646 +#if defined(_WIN32_WCE)
 95.1647 +/* sprintf() function is not supported on WindowsCE */
 95.1648 +   {
 95.1649 +      wchar_t wc_buf[32];
 95.1650 +      size_t wc_len;
 95.1651 +      swprintf(wc_buf, TEXT("%12.12e"), width);
 95.1652 +      wc_len = wcslen(wc_buf);
 95.1653 +      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
 95.1654 +      total_len = wc_len + 2;
 95.1655 +      swprintf(wc_buf, TEXT("%12.12e"), height);
 95.1656 +      wc_len = wcslen(wc_buf);
 95.1657 +      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
 95.1658 +         NULL, NULL);
 95.1659 +      total_len += wc_len;
 95.1660 +   }
 95.1661 +#else
 95.1662 +   png_snprintf(buf + 1, 63, "%12.12e", width);
 95.1663 +   total_len = 1 + png_strlen(buf + 1) + 1;
 95.1664 +   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
 95.1665 +   total_len += png_strlen(buf + total_len);
 95.1666 +#endif
 95.1667 +
 95.1668 +   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 95.1669 +   png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
 95.1670 +}
 95.1671 +#else
 95.1672 +#ifdef PNG_FIXED_POINT_SUPPORTED
 95.1673 +void /* PRIVATE */
 95.1674 +png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
 95.1675 +   png_charp height)
 95.1676 +{
 95.1677 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1678 +   PNG_sCAL;
 95.1679 +#endif
 95.1680 +   png_byte buf[64];
 95.1681 +   png_size_t wlen, hlen, total_len;
 95.1682 +
 95.1683 +   png_debug(1, "in png_write_sCAL_s\n");
 95.1684 +
 95.1685 +   wlen = png_strlen(width);
 95.1686 +   hlen = png_strlen(height);
 95.1687 +   total_len = wlen + hlen + 2;
 95.1688 +   if (total_len > 64)
 95.1689 +   {
 95.1690 +      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
 95.1691 +      return;
 95.1692 +   }
 95.1693 +
 95.1694 +   buf[0] = (png_byte)unit;
 95.1695 +   png_memcpy(buf + 1, width, wlen + 1);      /* append the '\0' here */
 95.1696 +   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */
 95.1697 +
 95.1698 +   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 95.1699 +   png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
 95.1700 +}
 95.1701 +#endif
 95.1702 +#endif
 95.1703 +#endif
 95.1704 +
 95.1705 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
 95.1706 +/* write the pHYs chunk */
 95.1707 +void /* PRIVATE */
 95.1708 +png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
 95.1709 +   png_uint_32 y_pixels_per_unit,
 95.1710 +   int unit_type)
 95.1711 +{
 95.1712 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1713 +   PNG_pHYs;
 95.1714 +#endif
 95.1715 +   png_byte buf[9];
 95.1716 +
 95.1717 +   png_debug(1, "in png_write_pHYs\n");
 95.1718 +   if (unit_type >= PNG_RESOLUTION_LAST)
 95.1719 +      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
 95.1720 +
 95.1721 +   png_save_uint_32(buf, x_pixels_per_unit);
 95.1722 +   png_save_uint_32(buf + 4, y_pixels_per_unit);
 95.1723 +   buf[8] = (png_byte)unit_type;
 95.1724 +
 95.1725 +   png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
 95.1726 +}
 95.1727 +#endif
 95.1728 +
 95.1729 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 95.1730 +/* Write the tIME chunk.  Use either png_convert_from_struct_tm()
 95.1731 + * or png_convert_from_time_t(), or fill in the structure yourself.
 95.1732 + */
 95.1733 +void /* PRIVATE */
 95.1734 +png_write_tIME(png_structp png_ptr, png_timep mod_time)
 95.1735 +{
 95.1736 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1737 +   PNG_tIME;
 95.1738 +#endif
 95.1739 +   png_byte buf[7];
 95.1740 +
 95.1741 +   png_debug(1, "in png_write_tIME\n");
 95.1742 +   if (mod_time->month  > 12 || mod_time->month  < 1 ||
 95.1743 +       mod_time->day    > 31 || mod_time->day    < 1 ||
 95.1744 +       mod_time->hour   > 23 || mod_time->second > 60)
 95.1745 +   {
 95.1746 +      png_warning(png_ptr, "Invalid time specified for tIME chunk");
 95.1747 +      return;
 95.1748 +   }
 95.1749 +
 95.1750 +   png_save_uint_16(buf, mod_time->year);
 95.1751 +   buf[2] = mod_time->month;
 95.1752 +   buf[3] = mod_time->day;
 95.1753 +   buf[4] = mod_time->hour;
 95.1754 +   buf[5] = mod_time->minute;
 95.1755 +   buf[6] = mod_time->second;
 95.1756 +
 95.1757 +   png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
 95.1758 +}
 95.1759 +#endif
 95.1760 +
 95.1761 +/* initializes the row writing capability of libpng */
 95.1762 +void /* PRIVATE */
 95.1763 +png_write_start_row(png_structp png_ptr)
 95.1764 +{
 95.1765 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 95.1766 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1767 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 95.1768 +
 95.1769 +   /* start of interlace block */
 95.1770 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 95.1771 +
 95.1772 +   /* offset to next interlace block */
 95.1773 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 95.1774 +
 95.1775 +   /* start of interlace block in the y direction */
 95.1776 +   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 95.1777 +
 95.1778 +   /* offset to next interlace block in the y direction */
 95.1779 +   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 95.1780 +#endif
 95.1781 +#endif
 95.1782 +
 95.1783 +   png_size_t buf_size;
 95.1784 +
 95.1785 +   png_debug(1, "in png_write_start_row\n");
 95.1786 +   buf_size = (png_size_t)(PNG_ROWBYTES(
 95.1787 +      png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
 95.1788 +
 95.1789 +   /* set up row buffer */
 95.1790 +   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
 95.1791 +     (png_uint_32)buf_size);
 95.1792 +   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
 95.1793 +
 95.1794 +#ifndef PNG_NO_WRITE_FILTER
 95.1795 +   /* set up filtering buffer, if using this filter */
 95.1796 +   if (png_ptr->do_filter & PNG_FILTER_SUB)
 95.1797 +   {
 95.1798 +      png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
 95.1799 +         (png_uint_32)(png_ptr->rowbytes + 1));
 95.1800 +      png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
 95.1801 +   }
 95.1802 +
 95.1803 +   /* We only need to keep the previous row if we are using one of these. */
 95.1804 +   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
 95.1805 +   {
 95.1806 +     /* set up previous row buffer */
 95.1807 +      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
 95.1808 +        (png_uint_32)buf_size);
 95.1809 +      png_memset(png_ptr->prev_row, 0, buf_size);
 95.1810 +
 95.1811 +      if (png_ptr->do_filter & PNG_FILTER_UP)
 95.1812 +      {
 95.1813 +         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
 95.1814 +           (png_uint_32)(png_ptr->rowbytes + 1));
 95.1815 +         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
 95.1816 +      }
 95.1817 +
 95.1818 +      if (png_ptr->do_filter & PNG_FILTER_AVG)
 95.1819 +      {
 95.1820 +         png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
 95.1821 +           (png_uint_32)(png_ptr->rowbytes + 1));
 95.1822 +         png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
 95.1823 +      }
 95.1824 +
 95.1825 +      if (png_ptr->do_filter & PNG_FILTER_PAETH)
 95.1826 +      {
 95.1827 +         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
 95.1828 +           (png_uint_32)(png_ptr->rowbytes + 1));
 95.1829 +         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
 95.1830 +      }
 95.1831 +   }
 95.1832 +#endif /* PNG_NO_WRITE_FILTER */
 95.1833 +
 95.1834 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 95.1835 +   /* if interlaced, we need to set up width and height of pass */
 95.1836 +   if (png_ptr->interlaced)
 95.1837 +   {
 95.1838 +      if (!(png_ptr->transformations & PNG_INTERLACE))
 95.1839 +      {
 95.1840 +         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
 95.1841 +            png_pass_ystart[0]) / png_pass_yinc[0];
 95.1842 +         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
 95.1843 +            png_pass_start[0]) / png_pass_inc[0];
 95.1844 +      }
 95.1845 +      else
 95.1846 +      {
 95.1847 +         png_ptr->num_rows = png_ptr->height;
 95.1848 +         png_ptr->usr_width = png_ptr->width;
 95.1849 +      }
 95.1850 +   }
 95.1851 +   else
 95.1852 +#endif
 95.1853 +   {
 95.1854 +      png_ptr->num_rows = png_ptr->height;
 95.1855 +      png_ptr->usr_width = png_ptr->width;
 95.1856 +   }
 95.1857 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 95.1858 +   png_ptr->zstream.next_out = png_ptr->zbuf;
 95.1859 +}
 95.1860 +
 95.1861 +/* Internal use only.  Called when finished processing a row of data. */
 95.1862 +void /* PRIVATE */
 95.1863 +png_write_finish_row(png_structp png_ptr)
 95.1864 +{
 95.1865 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 95.1866 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1867 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 95.1868 +
 95.1869 +   /* start of interlace block */
 95.1870 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 95.1871 +
 95.1872 +   /* offset to next interlace block */
 95.1873 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 95.1874 +
 95.1875 +   /* start of interlace block in the y direction */
 95.1876 +   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 95.1877 +
 95.1878 +   /* offset to next interlace block in the y direction */
 95.1879 +   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 95.1880 +#endif
 95.1881 +#endif
 95.1882 +
 95.1883 +   int ret;
 95.1884 +
 95.1885 +   png_debug(1, "in png_write_finish_row\n");
 95.1886 +   /* next row */
 95.1887 +   png_ptr->row_number++;
 95.1888 +
 95.1889 +   /* see if we are done */
 95.1890 +   if (png_ptr->row_number < png_ptr->num_rows)
 95.1891 +      return;
 95.1892 +
 95.1893 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 95.1894 +   /* if interlaced, go to next pass */
 95.1895 +   if (png_ptr->interlaced)
 95.1896 +   {
 95.1897 +      png_ptr->row_number = 0;
 95.1898 +      if (png_ptr->transformations & PNG_INTERLACE)
 95.1899 +      {
 95.1900 +         png_ptr->pass++;
 95.1901 +      }
 95.1902 +      else
 95.1903 +      {
 95.1904 +         /* loop until we find a non-zero width or height pass */
 95.1905 +         do
 95.1906 +         {
 95.1907 +            png_ptr->pass++;
 95.1908 +            if (png_ptr->pass >= 7)
 95.1909 +               break;
 95.1910 +            png_ptr->usr_width = (png_ptr->width +
 95.1911 +               png_pass_inc[png_ptr->pass] - 1 -
 95.1912 +               png_pass_start[png_ptr->pass]) /
 95.1913 +               png_pass_inc[png_ptr->pass];
 95.1914 +            png_ptr->num_rows = (png_ptr->height +
 95.1915 +               png_pass_yinc[png_ptr->pass] - 1 -
 95.1916 +               png_pass_ystart[png_ptr->pass]) /
 95.1917 +               png_pass_yinc[png_ptr->pass];
 95.1918 +            if (png_ptr->transformations & PNG_INTERLACE)
 95.1919 +               break;
 95.1920 +         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
 95.1921 +
 95.1922 +      }
 95.1923 +
 95.1924 +      /* reset the row above the image for the next pass */
 95.1925 +      if (png_ptr->pass < 7)
 95.1926 +      {
 95.1927 +         if (png_ptr->prev_row != NULL)
 95.1928 +            png_memset(png_ptr->prev_row, 0,
 95.1929 +               (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
 95.1930 +               png_ptr->usr_bit_depth, png_ptr->width)) + 1);
 95.1931 +         return;
 95.1932 +      }
 95.1933 +   }
 95.1934 +#endif
 95.1935 +
 95.1936 +   /* if we get here, we've just written the last row, so we need
 95.1937 +      to flush the compressor */
 95.1938 +   do
 95.1939 +   {
 95.1940 +      /* tell the compressor we are done */
 95.1941 +      ret = deflate(&png_ptr->zstream, Z_FINISH);
 95.1942 +      /* check for an error */
 95.1943 +      if (ret == Z_OK)
 95.1944 +      {
 95.1945 +         /* check to see if we need more room */
 95.1946 +         if (!(png_ptr->zstream.avail_out))
 95.1947 +         {
 95.1948 +            png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
 95.1949 +            png_ptr->zstream.next_out = png_ptr->zbuf;
 95.1950 +            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 95.1951 +         }
 95.1952 +      }
 95.1953 +      else if (ret != Z_STREAM_END)
 95.1954 +      {
 95.1955 +         if (png_ptr->zstream.msg != NULL)
 95.1956 +            png_error(png_ptr, png_ptr->zstream.msg);
 95.1957 +         else
 95.1958 +            png_error(png_ptr, "zlib error");
 95.1959 +      }
 95.1960 +   } while (ret != Z_STREAM_END);
 95.1961 +
 95.1962 +   /* write any extra space */
 95.1963 +   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
 95.1964 +   {
 95.1965 +      png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
 95.1966 +         png_ptr->zstream.avail_out);
 95.1967 +   }
 95.1968 +
 95.1969 +   deflateReset(&png_ptr->zstream);
 95.1970 +   png_ptr->zstream.data_type = Z_BINARY;
 95.1971 +}
 95.1972 +
 95.1973 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
 95.1974 +/* Pick out the correct pixels for the interlace pass.
 95.1975 + * The basic idea here is to go through the row with a source
 95.1976 + * pointer and a destination pointer (sp and dp), and copy the
 95.1977 + * correct pixels for the pass.  As the row gets compacted,
 95.1978 + * sp will always be >= dp, so we should never overwrite anything.
 95.1979 + * See the default: case for the easiest code to understand.
 95.1980 + */
 95.1981 +void /* PRIVATE */
 95.1982 +png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
 95.1983 +{
 95.1984 +#ifdef PNG_USE_LOCAL_ARRAYS
 95.1985 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 95.1986 +
 95.1987 +   /* start of interlace block */
 95.1988 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 95.1989 +
 95.1990 +   /* offset to next interlace block */
 95.1991 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 95.1992 +#endif
 95.1993 +
 95.1994 +   png_debug(1, "in png_do_write_interlace\n");
 95.1995 +   /* we don't have to do anything on the last pass (6) */
 95.1996 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 95.1997 +   if (row != NULL && row_info != NULL && pass < 6)
 95.1998 +#else
 95.1999 +   if (pass < 6)
 95.2000 +#endif
 95.2001 +   {
 95.2002 +      /* each pixel depth is handled separately */
 95.2003 +      switch (row_info->pixel_depth)
 95.2004 +      {
 95.2005 +         case 1:
 95.2006 +         {
 95.2007 +            png_bytep sp;
 95.2008 +            png_bytep dp;
 95.2009 +            int shift;
 95.2010 +            int d;
 95.2011 +            int value;
 95.2012 +            png_uint_32 i;
 95.2013 +            png_uint_32 row_width = row_info->width;
 95.2014 +
 95.2015 +            dp = row;
 95.2016 +            d = 0;
 95.2017 +            shift = 7;
 95.2018 +            for (i = png_pass_start[pass]; i < row_width;
 95.2019 +               i += png_pass_inc[pass])
 95.2020 +            {
 95.2021 +               sp = row + (png_size_t)(i >> 3);
 95.2022 +               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
 95.2023 +               d |= (value << shift);
 95.2024 +
 95.2025 +               if (shift == 0)
 95.2026 +               {
 95.2027 +                  shift = 7;
 95.2028 +                  *dp++ = (png_byte)d;
 95.2029 +                  d = 0;
 95.2030 +               }
 95.2031 +               else
 95.2032 +                  shift--;
 95.2033 +
 95.2034 +            }
 95.2035 +            if (shift != 7)
 95.2036 +               *dp = (png_byte)d;
 95.2037 +            break;
 95.2038 +         }
 95.2039 +         case 2:
 95.2040 +         {
 95.2041 +            png_bytep sp;
 95.2042 +            png_bytep dp;
 95.2043 +            int shift;
 95.2044 +            int d;
 95.2045 +            int value;
 95.2046 +            png_uint_32 i;
 95.2047 +            png_uint_32 row_width = row_info->width;
 95.2048 +
 95.2049 +            dp = row;
 95.2050 +            shift = 6;
 95.2051 +            d = 0;
 95.2052 +            for (i = png_pass_start[pass]; i < row_width;
 95.2053 +               i += png_pass_inc[pass])
 95.2054 +            {
 95.2055 +               sp = row + (png_size_t)(i >> 2);
 95.2056 +               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
 95.2057 +               d |= (value << shift);
 95.2058 +
 95.2059 +               if (shift == 0)
 95.2060 +               {
 95.2061 +                  shift = 6;
 95.2062 +                  *dp++ = (png_byte)d;
 95.2063 +                  d = 0;
 95.2064 +               }
 95.2065 +               else
 95.2066 +                  shift -= 2;
 95.2067 +            }
 95.2068 +            if (shift != 6)
 95.2069 +                   *dp = (png_byte)d;
 95.2070 +            break;
 95.2071 +         }
 95.2072 +         case 4:
 95.2073 +         {
 95.2074 +            png_bytep sp;
 95.2075 +            png_bytep dp;
 95.2076 +            int shift;
 95.2077 +            int d;
 95.2078 +            int value;
 95.2079 +            png_uint_32 i;
 95.2080 +            png_uint_32 row_width = row_info->width;
 95.2081 +
 95.2082 +            dp = row;
 95.2083 +            shift = 4;
 95.2084 +            d = 0;
 95.2085 +            for (i = png_pass_start[pass]; i < row_width;
 95.2086 +               i += png_pass_inc[pass])
 95.2087 +            {
 95.2088 +               sp = row + (png_size_t)(i >> 1);
 95.2089 +               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
 95.2090 +               d |= (value << shift);
 95.2091 +
 95.2092 +               if (shift == 0)
 95.2093 +               {
 95.2094 +                  shift = 4;
 95.2095 +                  *dp++ = (png_byte)d;
 95.2096 +                  d = 0;
 95.2097 +               }
 95.2098 +               else
 95.2099 +                  shift -= 4;
 95.2100 +            }
 95.2101 +            if (shift != 4)
 95.2102 +               *dp = (png_byte)d;
 95.2103 +            break;
 95.2104 +         }
 95.2105 +         default:
 95.2106 +         {
 95.2107 +            png_bytep sp;
 95.2108 +            png_bytep dp;
 95.2109 +            png_uint_32 i;
 95.2110 +            png_uint_32 row_width = row_info->width;
 95.2111 +            png_size_t pixel_bytes;
 95.2112 +
 95.2113 +            /* start at the beginning */
 95.2114 +            dp = row;
 95.2115 +            /* find out how many bytes each pixel takes up */
 95.2116 +            pixel_bytes = (row_info->pixel_depth >> 3);
 95.2117 +            /* loop through the row, only looking at the pixels that
 95.2118 +               matter */
 95.2119 +            for (i = png_pass_start[pass]; i < row_width;
 95.2120 +               i += png_pass_inc[pass])
 95.2121 +            {
 95.2122 +               /* find out where the original pixel is */
 95.2123 +               sp = row + (png_size_t)i * pixel_bytes;
 95.2124 +               /* move the pixel */
 95.2125 +               if (dp != sp)
 95.2126 +                  png_memcpy(dp, sp, pixel_bytes);
 95.2127 +               /* next pixel */
 95.2128 +               dp += pixel_bytes;
 95.2129 +            }
 95.2130 +            break;
 95.2131 +         }
 95.2132 +      }
 95.2133 +      /* set new row width */
 95.2134 +      row_info->width = (row_info->width +
 95.2135 +         png_pass_inc[pass] - 1 -
 95.2136 +         png_pass_start[pass]) /
 95.2137 +         png_pass_inc[pass];
 95.2138 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
 95.2139 +            row_info->width);
 95.2140 +   }
 95.2141 +}
 95.2142 +#endif
 95.2143 +
 95.2144 +/* This filters the row, chooses which filter to use, if it has not already
 95.2145 + * been specified by the application, and then writes the row out with the
 95.2146 + * chosen filter.
 95.2147 + */
 95.2148 +#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
 95.2149 +#define PNG_HISHIFT 10
 95.2150 +#define PNG_LOMASK ((png_uint_32)0xffffL)
 95.2151 +#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
 95.2152 +void /* PRIVATE */
 95.2153 +png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
 95.2154 +{
 95.2155 +   png_bytep best_row;
 95.2156 +#ifndef PNG_NO_WRITE_FILTER
 95.2157 +   png_bytep prev_row, row_buf;
 95.2158 +   png_uint_32 mins, bpp;
 95.2159 +   png_byte filter_to_do = png_ptr->do_filter;
 95.2160 +   png_uint_32 row_bytes = row_info->rowbytes;
 95.2161 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2162 +   int num_p_filters = (int)png_ptr->num_prev_filters;
 95.2163 +#endif
 95.2164 +
 95.2165 +   png_debug(1, "in png_write_find_filter\n");
 95.2166 +   /* find out how many bytes offset each pixel is */
 95.2167 +   bpp = (row_info->pixel_depth + 7) >> 3;
 95.2168 +
 95.2169 +   prev_row = png_ptr->prev_row;
 95.2170 +#endif
 95.2171 +   best_row = png_ptr->row_buf;
 95.2172 +#ifndef PNG_NO_WRITE_FILTER
 95.2173 +   row_buf = best_row;
 95.2174 +   mins = PNG_MAXSUM;
 95.2175 +
 95.2176 +   /* The prediction method we use is to find which method provides the
 95.2177 +    * smallest value when summing the absolute values of the distances
 95.2178 +    * from zero, using anything >= 128 as negative numbers.  This is known
 95.2179 +    * as the "minimum sum of absolute differences" heuristic.  Other
 95.2180 +    * heuristics are the "weighted minimum sum of absolute differences"
 95.2181 +    * (experimental and can in theory improve compression), and the "zlib
 95.2182 +    * predictive" method (not implemented yet), which does test compressions
 95.2183 +    * of lines using different filter methods, and then chooses the
 95.2184 +    * (series of) filter(s) that give minimum compressed data size (VERY
 95.2185 +    * computationally expensive).
 95.2186 +    *
 95.2187 +    * GRR 980525:  consider also
 95.2188 +    *   (1) minimum sum of absolute differences from running average (i.e.,
 95.2189 +    *       keep running sum of non-absolute differences & count of bytes)
 95.2190 +    *       [track dispersion, too?  restart average if dispersion too large?]
 95.2191 +    *  (1b) minimum sum of absolute differences from sliding average, probably
 95.2192 +    *       with window size <= deflate window (usually 32K)
 95.2193 +    *   (2) minimum sum of squared differences from zero or running average
 95.2194 +    *       (i.e., ~ root-mean-square approach)
 95.2195 +    */
 95.2196 +
 95.2197 +
 95.2198 +   /* We don't need to test the 'no filter' case if this is the only filter
 95.2199 +    * that has been chosen, as it doesn't actually do anything to the data.
 95.2200 +    */
 95.2201 +   if ((filter_to_do & PNG_FILTER_NONE) &&
 95.2202 +       filter_to_do != PNG_FILTER_NONE)
 95.2203 +   {
 95.2204 +      png_bytep rp;
 95.2205 +      png_uint_32 sum = 0;
 95.2206 +      png_uint_32 i;
 95.2207 +      int v;
 95.2208 +
 95.2209 +      for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
 95.2210 +      {
 95.2211 +         v = *rp;
 95.2212 +         sum += (v < 128) ? v : 256 - v;
 95.2213 +      }
 95.2214 +
 95.2215 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2216 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2217 +      {
 95.2218 +         png_uint_32 sumhi, sumlo;
 95.2219 +         int j;
 95.2220 +         sumlo = sum & PNG_LOMASK;
 95.2221 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
 95.2222 +
 95.2223 +         /* Reduce the sum if we match any of the previous rows */
 95.2224 +         for (j = 0; j < num_p_filters; j++)
 95.2225 +         {
 95.2226 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
 95.2227 +            {
 95.2228 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 95.2229 +                  PNG_WEIGHT_SHIFT;
 95.2230 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 95.2231 +                  PNG_WEIGHT_SHIFT;
 95.2232 +            }
 95.2233 +         }
 95.2234 +
 95.2235 +         /* Factor in the cost of this filter (this is here for completeness,
 95.2236 +          * but it makes no sense to have a "cost" for the NONE filter, as
 95.2237 +          * it has the minimum possible computational cost - none).
 95.2238 +          */
 95.2239 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
 95.2240 +            PNG_COST_SHIFT;
 95.2241 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
 95.2242 +            PNG_COST_SHIFT;
 95.2243 +
 95.2244 +         if (sumhi > PNG_HIMASK)
 95.2245 +            sum = PNG_MAXSUM;
 95.2246 +         else
 95.2247 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 95.2248 +      }
 95.2249 +#endif
 95.2250 +      mins = sum;
 95.2251 +   }
 95.2252 +
 95.2253 +   /* sub filter */
 95.2254 +   if (filter_to_do == PNG_FILTER_SUB)
 95.2255 +   /* it's the only filter so no testing is needed */
 95.2256 +   {
 95.2257 +      png_bytep rp, lp, dp;
 95.2258 +      png_uint_32 i;
 95.2259 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
 95.2260 +           i++, rp++, dp++)
 95.2261 +      {
 95.2262 +         *dp = *rp;
 95.2263 +      }
 95.2264 +      for (lp = row_buf + 1; i < row_bytes;
 95.2265 +         i++, rp++, lp++, dp++)
 95.2266 +      {
 95.2267 +         *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
 95.2268 +      }
 95.2269 +      best_row = png_ptr->sub_row;
 95.2270 +   }
 95.2271 +
 95.2272 +   else if (filter_to_do & PNG_FILTER_SUB)
 95.2273 +   {
 95.2274 +      png_bytep rp, dp, lp;
 95.2275 +      png_uint_32 sum = 0, lmins = mins;
 95.2276 +      png_uint_32 i;
 95.2277 +      int v;
 95.2278 +
 95.2279 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2280 +      /* We temporarily increase the "minimum sum" by the factor we
 95.2281 +       * would reduce the sum of this filter, so that we can do the
 95.2282 +       * early exit comparison without scaling the sum each time.
 95.2283 +       */
 95.2284 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2285 +      {
 95.2286 +         int j;
 95.2287 +         png_uint_32 lmhi, lmlo;
 95.2288 +         lmlo = lmins & PNG_LOMASK;
 95.2289 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 95.2290 +
 95.2291 +         for (j = 0; j < num_p_filters; j++)
 95.2292 +         {
 95.2293 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
 95.2294 +            {
 95.2295 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 95.2296 +                  PNG_WEIGHT_SHIFT;
 95.2297 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 95.2298 +                  PNG_WEIGHT_SHIFT;
 95.2299 +            }
 95.2300 +         }
 95.2301 +
 95.2302 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 95.2303 +            PNG_COST_SHIFT;
 95.2304 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 95.2305 +            PNG_COST_SHIFT;
 95.2306 +
 95.2307 +         if (lmhi > PNG_HIMASK)
 95.2308 +            lmins = PNG_MAXSUM;
 95.2309 +         else
 95.2310 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 95.2311 +      }
 95.2312 +#endif
 95.2313 +
 95.2314 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
 95.2315 +           i++, rp++, dp++)
 95.2316 +      {
 95.2317 +         v = *dp = *rp;
 95.2318 +
 95.2319 +         sum += (v < 128) ? v : 256 - v;
 95.2320 +      }
 95.2321 +      for (lp = row_buf + 1; i < row_bytes;
 95.2322 +         i++, rp++, lp++, dp++)
 95.2323 +      {
 95.2324 +         v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
 95.2325 +
 95.2326 +         sum += (v < 128) ? v : 256 - v;
 95.2327 +
 95.2328 +         if (sum > lmins)  /* We are already worse, don't continue. */
 95.2329 +            break;
 95.2330 +      }
 95.2331 +
 95.2332 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2333 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2334 +      {
 95.2335 +         int j;
 95.2336 +         png_uint_32 sumhi, sumlo;
 95.2337 +         sumlo = sum & PNG_LOMASK;
 95.2338 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 95.2339 +
 95.2340 +         for (j = 0; j < num_p_filters; j++)
 95.2341 +         {
 95.2342 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
 95.2343 +            {
 95.2344 +               sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
 95.2345 +                  PNG_WEIGHT_SHIFT;
 95.2346 +               sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
 95.2347 +                  PNG_WEIGHT_SHIFT;
 95.2348 +            }
 95.2349 +         }
 95.2350 +
 95.2351 +         sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 95.2352 +            PNG_COST_SHIFT;
 95.2353 +         sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 95.2354 +            PNG_COST_SHIFT;
 95.2355 +
 95.2356 +         if (sumhi > PNG_HIMASK)
 95.2357 +            sum = PNG_MAXSUM;
 95.2358 +         else
 95.2359 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 95.2360 +      }
 95.2361 +#endif
 95.2362 +
 95.2363 +      if (sum < mins)
 95.2364 +      {
 95.2365 +         mins = sum;
 95.2366 +         best_row = png_ptr->sub_row;
 95.2367 +      }
 95.2368 +   }
 95.2369 +
 95.2370 +   /* up filter */
 95.2371 +   if (filter_to_do == PNG_FILTER_UP)
 95.2372 +   {
 95.2373 +      png_bytep rp, dp, pp;
 95.2374 +      png_uint_32 i;
 95.2375 +
 95.2376 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
 95.2377 +           pp = prev_row + 1; i < row_bytes;
 95.2378 +           i++, rp++, pp++, dp++)
 95.2379 +      {
 95.2380 +         *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
 95.2381 +      }
 95.2382 +      best_row = png_ptr->up_row;
 95.2383 +   }
 95.2384 +
 95.2385 +   else if (filter_to_do & PNG_FILTER_UP)
 95.2386 +   {
 95.2387 +      png_bytep rp, dp, pp;
 95.2388 +      png_uint_32 sum = 0, lmins = mins;
 95.2389 +      png_uint_32 i;
 95.2390 +      int v;
 95.2391 +
 95.2392 +
 95.2393 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2394 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2395 +      {
 95.2396 +         int j;
 95.2397 +         png_uint_32 lmhi, lmlo;
 95.2398 +         lmlo = lmins & PNG_LOMASK;
 95.2399 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 95.2400 +
 95.2401 +         for (j = 0; j < num_p_filters; j++)
 95.2402 +         {
 95.2403 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
 95.2404 +            {
 95.2405 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 95.2406 +                  PNG_WEIGHT_SHIFT;
 95.2407 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 95.2408 +                  PNG_WEIGHT_SHIFT;
 95.2409 +            }
 95.2410 +         }
 95.2411 +
 95.2412 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
 95.2413 +            PNG_COST_SHIFT;
 95.2414 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
 95.2415 +            PNG_COST_SHIFT;
 95.2416 +
 95.2417 +         if (lmhi > PNG_HIMASK)
 95.2418 +            lmins = PNG_MAXSUM;
 95.2419 +         else
 95.2420 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 95.2421 +      }
 95.2422 +#endif
 95.2423 +
 95.2424 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
 95.2425 +           pp = prev_row + 1; i < row_bytes; i++)
 95.2426 +      {
 95.2427 +         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 95.2428 +
 95.2429 +         sum += (v < 128) ? v : 256 - v;
 95.2430 +
 95.2431 +         if (sum > lmins)  /* We are already worse, don't continue. */
 95.2432 +            break;
 95.2433 +      }
 95.2434 +
 95.2435 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2436 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2437 +      {
 95.2438 +         int j;
 95.2439 +         png_uint_32 sumhi, sumlo;
 95.2440 +         sumlo = sum & PNG_LOMASK;
 95.2441 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 95.2442 +
 95.2443 +         for (j = 0; j < num_p_filters; j++)
 95.2444 +         {
 95.2445 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
 95.2446 +            {
 95.2447 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 95.2448 +                  PNG_WEIGHT_SHIFT;
 95.2449 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 95.2450 +                  PNG_WEIGHT_SHIFT;
 95.2451 +            }
 95.2452 +         }
 95.2453 +
 95.2454 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
 95.2455 +            PNG_COST_SHIFT;
 95.2456 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
 95.2457 +            PNG_COST_SHIFT;
 95.2458 +
 95.2459 +         if (sumhi > PNG_HIMASK)
 95.2460 +            sum = PNG_MAXSUM;
 95.2461 +         else
 95.2462 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 95.2463 +      }
 95.2464 +#endif
 95.2465 +
 95.2466 +      if (sum < mins)
 95.2467 +      {
 95.2468 +         mins = sum;
 95.2469 +         best_row = png_ptr->up_row;
 95.2470 +      }
 95.2471 +   }
 95.2472 +
 95.2473 +   /* avg filter */
 95.2474 +   if (filter_to_do == PNG_FILTER_AVG)
 95.2475 +   {
 95.2476 +      png_bytep rp, dp, pp, lp;
 95.2477 +      png_uint_32 i;
 95.2478 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
 95.2479 +           pp = prev_row + 1; i < bpp; i++)
 95.2480 +      {
 95.2481 +         *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
 95.2482 +      }
 95.2483 +      for (lp = row_buf + 1; i < row_bytes; i++)
 95.2484 +      {
 95.2485 +         *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
 95.2486 +                 & 0xff);
 95.2487 +      }
 95.2488 +      best_row = png_ptr->avg_row;
 95.2489 +   }
 95.2490 +
 95.2491 +   else if (filter_to_do & PNG_FILTER_AVG)
 95.2492 +   {
 95.2493 +      png_bytep rp, dp, pp, lp;
 95.2494 +      png_uint_32 sum = 0, lmins = mins;
 95.2495 +      png_uint_32 i;
 95.2496 +      int v;
 95.2497 +
 95.2498 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2499 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2500 +      {
 95.2501 +         int j;
 95.2502 +         png_uint_32 lmhi, lmlo;
 95.2503 +         lmlo = lmins & PNG_LOMASK;
 95.2504 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 95.2505 +
 95.2506 +         for (j = 0; j < num_p_filters; j++)
 95.2507 +         {
 95.2508 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
 95.2509 +            {
 95.2510 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 95.2511 +                  PNG_WEIGHT_SHIFT;
 95.2512 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 95.2513 +                  PNG_WEIGHT_SHIFT;
 95.2514 +            }
 95.2515 +         }
 95.2516 +
 95.2517 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
 95.2518 +            PNG_COST_SHIFT;
 95.2519 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
 95.2520 +            PNG_COST_SHIFT;
 95.2521 +
 95.2522 +         if (lmhi > PNG_HIMASK)
 95.2523 +            lmins = PNG_MAXSUM;
 95.2524 +         else
 95.2525 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 95.2526 +      }
 95.2527 +#endif
 95.2528 +
 95.2529 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
 95.2530 +           pp = prev_row + 1; i < bpp; i++)
 95.2531 +      {
 95.2532 +         v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
 95.2533 +
 95.2534 +         sum += (v < 128) ? v : 256 - v;
 95.2535 +      }
 95.2536 +      for (lp = row_buf + 1; i < row_bytes; i++)
 95.2537 +      {
 95.2538 +         v = *dp++ =
 95.2539 +          (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
 95.2540 +
 95.2541 +         sum += (v < 128) ? v : 256 - v;
 95.2542 +
 95.2543 +         if (sum > lmins)  /* We are already worse, don't continue. */
 95.2544 +            break;
 95.2545 +      }
 95.2546 +
 95.2547 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2548 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2549 +      {
 95.2550 +         int j;
 95.2551 +         png_uint_32 sumhi, sumlo;
 95.2552 +         sumlo = sum & PNG_LOMASK;
 95.2553 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 95.2554 +
 95.2555 +         for (j = 0; j < num_p_filters; j++)
 95.2556 +         {
 95.2557 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
 95.2558 +            {
 95.2559 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 95.2560 +                  PNG_WEIGHT_SHIFT;
 95.2561 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 95.2562 +                  PNG_WEIGHT_SHIFT;
 95.2563 +            }
 95.2564 +         }
 95.2565 +
 95.2566 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
 95.2567 +            PNG_COST_SHIFT;
 95.2568 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
 95.2569 +            PNG_COST_SHIFT;
 95.2570 +
 95.2571 +         if (sumhi > PNG_HIMASK)
 95.2572 +            sum = PNG_MAXSUM;
 95.2573 +         else
 95.2574 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 95.2575 +      }
 95.2576 +#endif
 95.2577 +
 95.2578 +      if (sum < mins)
 95.2579 +      {
 95.2580 +         mins = sum;
 95.2581 +         best_row = png_ptr->avg_row;
 95.2582 +      }
 95.2583 +   }
 95.2584 +
 95.2585 +   /* Paeth filter */
 95.2586 +   if (filter_to_do == PNG_FILTER_PAETH)
 95.2587 +   {
 95.2588 +      png_bytep rp, dp, pp, cp, lp;
 95.2589 +      png_uint_32 i;
 95.2590 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
 95.2591 +           pp = prev_row + 1; i < bpp; i++)
 95.2592 +      {
 95.2593 +         *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 95.2594 +      }
 95.2595 +
 95.2596 +      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
 95.2597 +      {
 95.2598 +         int a, b, c, pa, pb, pc, p;
 95.2599 +
 95.2600 +         b = *pp++;
 95.2601 +         c = *cp++;
 95.2602 +         a = *lp++;
 95.2603 +
 95.2604 +         p = b - c;
 95.2605 +         pc = a - c;
 95.2606 +
 95.2607 +#ifdef PNG_USE_ABS
 95.2608 +         pa = abs(p);
 95.2609 +         pb = abs(pc);
 95.2610 +         pc = abs(p + pc);
 95.2611 +#else
 95.2612 +         pa = p < 0 ? -p : p;
 95.2613 +         pb = pc < 0 ? -pc : pc;
 95.2614 +         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 95.2615 +#endif
 95.2616 +
 95.2617 +         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
 95.2618 +
 95.2619 +         *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
 95.2620 +      }
 95.2621 +      best_row = png_ptr->paeth_row;
 95.2622 +   }
 95.2623 +
 95.2624 +   else if (filter_to_do & PNG_FILTER_PAETH)
 95.2625 +   {
 95.2626 +      png_bytep rp, dp, pp, cp, lp;
 95.2627 +      png_uint_32 sum = 0, lmins = mins;
 95.2628 +      png_uint_32 i;
 95.2629 +      int v;
 95.2630 +
 95.2631 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2632 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2633 +      {
 95.2634 +         int j;
 95.2635 +         png_uint_32 lmhi, lmlo;
 95.2636 +         lmlo = lmins & PNG_LOMASK;
 95.2637 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 95.2638 +
 95.2639 +         for (j = 0; j < num_p_filters; j++)
 95.2640 +         {
 95.2641 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
 95.2642 +            {
 95.2643 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 95.2644 +                  PNG_WEIGHT_SHIFT;
 95.2645 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 95.2646 +                  PNG_WEIGHT_SHIFT;
 95.2647 +            }
 95.2648 +         }
 95.2649 +
 95.2650 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 95.2651 +            PNG_COST_SHIFT;
 95.2652 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 95.2653 +            PNG_COST_SHIFT;
 95.2654 +
 95.2655 +         if (lmhi > PNG_HIMASK)
 95.2656 +            lmins = PNG_MAXSUM;
 95.2657 +         else
 95.2658 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 95.2659 +      }
 95.2660 +#endif
 95.2661 +
 95.2662 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
 95.2663 +           pp = prev_row + 1; i < bpp; i++)
 95.2664 +      {
 95.2665 +         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 95.2666 +
 95.2667 +         sum += (v < 128) ? v : 256 - v;
 95.2668 +      }
 95.2669 +
 95.2670 +      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
 95.2671 +      {
 95.2672 +         int a, b, c, pa, pb, pc, p;
 95.2673 +
 95.2674 +         b = *pp++;
 95.2675 +         c = *cp++;
 95.2676 +         a = *lp++;
 95.2677 +
 95.2678 +#ifndef PNG_SLOW_PAETH
 95.2679 +         p = b - c;
 95.2680 +         pc = a - c;
 95.2681 +#ifdef PNG_USE_ABS
 95.2682 +         pa = abs(p);
 95.2683 +         pb = abs(pc);
 95.2684 +         pc = abs(p + pc);
 95.2685 +#else
 95.2686 +         pa = p < 0 ? -p : p;
 95.2687 +         pb = pc < 0 ? -pc : pc;
 95.2688 +         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 95.2689 +#endif
 95.2690 +         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
 95.2691 +#else /* PNG_SLOW_PAETH */
 95.2692 +         p = a + b - c;
 95.2693 +         pa = abs(p - a);
 95.2694 +         pb = abs(p - b);
 95.2695 +         pc = abs(p - c);
 95.2696 +         if (pa <= pb && pa <= pc)
 95.2697 +            p = a;
 95.2698 +         else if (pb <= pc)
 95.2699 +            p = b;
 95.2700 +         else
 95.2701 +            p = c;
 95.2702 +#endif /* PNG_SLOW_PAETH */
 95.2703 +
 95.2704 +         v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
 95.2705 +
 95.2706 +         sum += (v < 128) ? v : 256 - v;
 95.2707 +
 95.2708 +         if (sum > lmins)  /* We are already worse, don't continue. */
 95.2709 +            break;
 95.2710 +      }
 95.2711 +
 95.2712 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2713 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 95.2714 +      {
 95.2715 +         int j;
 95.2716 +         png_uint_32 sumhi, sumlo;
 95.2717 +         sumlo = sum & PNG_LOMASK;
 95.2718 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 95.2719 +
 95.2720 +         for (j = 0; j < num_p_filters; j++)
 95.2721 +         {
 95.2722 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
 95.2723 +            {
 95.2724 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 95.2725 +                  PNG_WEIGHT_SHIFT;
 95.2726 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 95.2727 +                  PNG_WEIGHT_SHIFT;
 95.2728 +            }
 95.2729 +         }
 95.2730 +
 95.2731 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 95.2732 +            PNG_COST_SHIFT;
 95.2733 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 95.2734 +            PNG_COST_SHIFT;
 95.2735 +
 95.2736 +         if (sumhi > PNG_HIMASK)
 95.2737 +            sum = PNG_MAXSUM;
 95.2738 +         else
 95.2739 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 95.2740 +      }
 95.2741 +#endif
 95.2742 +
 95.2743 +      if (sum < mins)
 95.2744 +      {
 95.2745 +         best_row = png_ptr->paeth_row;
 95.2746 +      }
 95.2747 +   }
 95.2748 +#endif /* PNG_NO_WRITE_FILTER */
 95.2749 +   /* Do the actual writing of the filtered row data from the chosen filter. */
 95.2750 +
 95.2751 +   png_write_filtered_row(png_ptr, best_row);
 95.2752 +
 95.2753 +#ifndef PNG_NO_WRITE_FILTER
 95.2754 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 95.2755 +   /* Save the type of filter we picked this time for future calculations */
 95.2756 +   if (png_ptr->num_prev_filters > 0)
 95.2757 +   {
 95.2758 +      int j;
 95.2759 +      for (j = 1; j < num_p_filters; j++)
 95.2760 +      {
 95.2761 +         png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
 95.2762 +      }
 95.2763 +      png_ptr->prev_filters[j] = best_row[0];
 95.2764 +   }
 95.2765 +#endif
 95.2766 +#endif /* PNG_NO_WRITE_FILTER */
 95.2767 +}
 95.2768 +
 95.2769 +
 95.2770 +/* Do the actual writing of a previously filtered row. */
 95.2771 +void /* PRIVATE */
 95.2772 +png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
 95.2773 +{
 95.2774 +   png_debug(1, "in png_write_filtered_row\n");
 95.2775 +   png_debug1(2, "filter = %d\n", filtered_row[0]);
 95.2776 +   /* set up the zlib input buffer */
 95.2777 +
 95.2778 +   png_ptr->zstream.next_in = filtered_row;
 95.2779 +   png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
 95.2780 +   /* repeat until we have compressed all the data */
 95.2781 +   do
 95.2782 +   {
 95.2783 +      int ret; /* return of zlib */
 95.2784 +
 95.2785 +      /* compress the data */
 95.2786 +      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
 95.2787 +      /* check for compression errors */
 95.2788 +      if (ret != Z_OK)
 95.2789 +      {
 95.2790 +         if (png_ptr->zstream.msg != NULL)
 95.2791 +            png_error(png_ptr, png_ptr->zstream.msg);
 95.2792 +         else
 95.2793 +            png_error(png_ptr, "zlib error");
 95.2794 +      }
 95.2795 +
 95.2796 +      /* see if it is time to write another IDAT */
 95.2797 +      if (!(png_ptr->zstream.avail_out))
 95.2798 +      {
 95.2799 +         /* write the IDAT and reset the zlib output buffer */
 95.2800 +         png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
 95.2801 +         png_ptr->zstream.next_out = png_ptr->zbuf;
 95.2802 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 95.2803 +      }
 95.2804 +   /* repeat until all data has been compressed */
 95.2805 +   } while (png_ptr->zstream.avail_in);
 95.2806 +
 95.2807 +   /* swap the current and previous rows */
 95.2808 +   if (png_ptr->prev_row != NULL)
 95.2809 +   {
 95.2810 +      png_bytep tptr;
 95.2811 +
 95.2812 +      tptr = png_ptr->prev_row;
 95.2813 +      png_ptr->prev_row = png_ptr->row_buf;
 95.2814 +      png_ptr->row_buf = tptr;
 95.2815 +   }
 95.2816 +
 95.2817 +   /* finish row - updates counters and flushes zlib if last row */
 95.2818 +   png_write_finish_row(png_ptr);
 95.2819 +
 95.2820 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 95.2821 +   png_ptr->flush_rows++;
 95.2822 +
 95.2823 +   if (png_ptr->flush_dist > 0 &&
 95.2824 +       png_ptr->flush_rows >= png_ptr->flush_dist)
 95.2825 +   {
 95.2826 +      png_write_flush(png_ptr);
 95.2827 +   }
 95.2828 +#endif
 95.2829 +}
 95.2830 +#endif /* PNG_WRITE_SUPPORTED */
    96.1 --- a/proj.mk	Sun Jun 07 02:29:10 2015 +0300
    96.2 +++ b/proj.mk	Sun Jun 07 17:25:49 2015 +0300
    96.3 @@ -2,7 +2,10 @@
    96.4  
    96.5  src = $(wildcard $(root)/src/*.c) \
    96.6  	  $(wildcard $(root)/libs/drawtext/*.c) \
    96.7 -	  $(wildcard $(root)/libs/vmath/*.c)
    96.8 +	  $(wildcard $(root)/libs/vmath/*.c) \
    96.9 +	  $(wildcard $(root)/libs/imago/*.c) \
   96.10 +	  $(wildcard $(root)/libs/libjpeg/*.c) \
   96.11 +	  $(wildcard $(root)/libs/libpng/*.c)
   96.12  
   96.13  ccsrc = $(wildcard $(root)/src/*.cc) \
   96.14  		$(wildcard $(root)/libs/vmath/*.cc)
   96.15 @@ -10,6 +13,12 @@
   96.16  bin = photoshoot3d
   96.17  title = 3D Photo Shoot
   96.18  
   96.19 -incpaths = -I$(root)/src -I$(root)/libs -I$(root)/libs/vmath -I$(root)/libs/drawtext
   96.20 +incpaths = -I$(root)/src -I$(root)/libs \
   96.21 +		   -I$(root)/libs/vmath \
   96.22 +		   -I$(root)/libs/imago \
   96.23 +		   -I$(root)/libs/libjpeg \
   96.24 +		   -I$(root)/libs/libpng \
   96.25 +		   -I$(root)/libs/drawtext
   96.26  libpaths =
   96.27  defs = -DNO_FREETYPE
   96.28 +libs = -lz -lm