glmark2-2012.08/./src/0000775000175000017500000000000012013417376013505 5ustar alfalf00000000000000glmark2-2012.08/./android/0000775000175000017500000000000012013417376014336 5ustar alfalf00000000000000glmark2-2012.08/./data/0000775000175000017500000000000012013417376013627 5ustar alfalf00000000000000glmark2-2012.08/./doc/0000775000175000017500000000000012013417376013463 5ustar alfalf00000000000000glmark2-2012.08/./waflib/0000775000175000017500000000000012013417421014151 5ustar alfalf00000000000000glmark2-2012.08/./waf0000775000175000017500000000764412013417376013434 0ustar alfalf00000000000000#!/usr/bin/env python # encoding: ISO8859-1 # Thomas Nagy, 2005-2011 """ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import os, sys VERSION="1.6.11" REVISION="30618c54883417962c38f5d395f83584" INSTALL='' C1='#*' C2='#%' cwd = os.getcwd() join = os.path.join WAF='waf' def b(x): return x if sys.hexversion>0x300000f: WAF='waf3' def b(x): return x.encode() def err(m): print(('\033[91mError: %s\033[0m' % m)) sys.exit(1) def unpack_wafdir(dir): f = open(sys.argv[0],'rb') c = 'corrupt archive (%d)' while 1: line = f.readline() if not line: err('run waf-light from a folder containing waflib') if line == b('#==>\n'): txt = f.readline() if not txt: err(c % 1) if f.readline() != b('#<==\n'): err(c % 2) break if not txt: err(c % 3) txt = txt[1:-1].replace(b(C1), b('\n')).replace(b(C2), b('\r')) import shutil, tarfile try: shutil.rmtree(dir) except OSError: pass try: for x in ['Tools', 'extras']: os.makedirs(join(dir, 'waflib', x)) except OSError: err("Cannot unpack waf lib into %s\nMove waf into a writeable directory" % dir) os.chdir(dir) tmp = 't.bz2' t = open(tmp,'wb') t.write(txt) t.close() try: t = tarfile.open(tmp) except: try: os.system('bunzip2 t.bz2') t = tarfile.open('t') tmp = 't' except: os.chdir(cwd) try: shutil.rmtree(dir) except OSError: pass err("Waf cannot be unpacked, check that bzip2 support is present") for x in t: t.extract(x) t.close() for x in ['Tools', 'extras']: os.chmod(join('waflib',x), 493) if sys.hexversion<0x300000f: sys.path = [join(dir, 'waflib')] + sys.path import fixpy2 fixpy2.fixdir(dir) os.unlink(tmp) os.chdir(cwd) try: dir = unicode(dir, 'mbcs') except: pass try: from ctypes import windll windll.kernel32.SetFileAttributesW(dir, 2) except: pass def test(dir): try: os.stat(join(dir, 'waflib')) return os.path.abspath(dir) except OSError: pass def find_lib(): name = sys.argv[0] base = os.path.dirname(os.path.abspath(name)) #devs use $WAFDIR w=test(os.environ.get('WAFDIR', '')) if w: return w #waf-light if name.endswith('waf-light'): w = test(base) if w: return w err('waf-light requires waflib -> export WAFDIR=/folder') dirname = '%s-%s-%s' % (WAF, VERSION, REVISION) for i in [INSTALL,'/usr','/usr/local','/opt']: w = test(i + '/lib/' + dirname) if w: return w #waf-local dir = join(base, (sys.platform != 'win32' and '.' or '') + dirname) w = test(dir) if w: return w #unpack unpack_wafdir(dir) return dir wafdir = find_lib() sys.path.insert(0, wafdir) if __name__ == '__main__': import waflib.extras.compat15 from waflib import Scripting Scripting.waf_entry_point(cwd, VERSION, wafdir) glmark2-2012.08/./INSTALL0000664000175000017500000000034112013417376013745 0ustar alfalf00000000000000glmark2 uses the WAF build system. To configure glmark2 use: $ ./waf configure [--enable-gl --enable-glesv2 --data-path=DATA_PATH --prefix=PREFIX] To build use: $ ./waf To install use: $ ./waf install --destdir=DESTDIR glmark2-2012.08/./wscript0000664000175000017500000001334112013417376014336 0ustar alfalf00000000000000import commands import subprocess import os import Options import Scripting from waflib import Context out = 'build' top = '.' VERSION = '2012.08' APPNAME = 'glmark2' def options(opt): opt.tool_options('gnu_dirs') opt.tool_options('compiler_cc') opt.tool_options('compiler_cxx') opt.add_option('--enable-gl', action='store_true', dest = 'gl', default = False, help='build using OpenGL 2.0') opt.add_option('--enable-glesv2', action='store_true', dest = 'glesv2', default = False, help='build using OpenGL ES 2.0') opt.add_option('--no-debug', action='store_false', dest = 'debug', default = True, help='disable compiler debug information') opt.add_option('--no-opt', action='store_false', dest = 'opt', default = True, help='disable compiler optimizations') opt.add_option('--data-path', action='store', dest = 'data_path', help='path to main data (also see --data(root)dir)') opt.add_option('--extras-path', action='store', dest = 'extras_path', help='path to additional data (models, shaders, textures)') def configure(ctx): if not Options.options.gl and not Options.options.glesv2: ctx.fatal("You must configure using at least one of --enable-gl, --enable-glesv2") ctx.check_tool('gnu_dirs') ctx.check_tool('compiler_cc') ctx.check_tool('compiler_cxx') # Check required headers req_headers = ['stdlib.h', 'string.h', 'unistd.h', 'stdint.h', 'stdio.h', 'jpeglib.h'] for header in req_headers: ctx.check_cxx(header_name = header, auto_add_header_name = True, mandatory = True) # Check for required libs req_libs = [('m', 'm'), ('jpeg', 'jpeg')] for (lib, uselib) in req_libs: ctx.check_cxx(lib = lib, uselib_store = uselib) # Check required functions req_funcs = [('memset', 'string.h', []) ,('sqrt', 'math.h', ['m'])] for func, header, uselib in req_funcs: ctx.check_cxx(function_name = func, header_name = header, uselib = uselib, mandatory = True) # Check required packages req_pkgs = [('x11', 'x11'), ('libpng12', 'libpng12')] for (pkg, uselib) in req_pkgs: ctx.check_cfg(package = pkg, uselib_store = uselib, args = '--cflags --libs', mandatory = True) # Check optional packages opt_pkgs = [('gl', 'gl', Options.options.gl), ('egl', 'egl', Options.options.glesv2), ('glesv2', 'glesv2', Options.options.glesv2)] for (pkg, uselib, mandatory) in opt_pkgs: ctx.check_cfg(package = pkg, uselib_store = uselib, args = '--cflags --libs', mandatory = mandatory) ctx.env.append_unique('CXXFLAGS', '-Werror -Wall -Wextra -Wnon-virtual-dtor'.split(' ')) # Prepend -O# and -g flags so that they can be overriden by the # CFLAGS environment variable if Options.options.opt: ctx.env.prepend_value('CXXFLAGS', '-O2') if Options.options.debug: ctx.env.prepend_value('CXXFLAGS', '-g') ctx.env.HAVE_EXTRAS = False if Options.options.extras_path is not None: ctx.env.HAVE_EXTRAS = True ctx.env.append_unique('GLMARK_EXTRAS_PATH', Options.options.extras_path) ctx.env.append_unique('DEFINES', 'GLMARK_EXTRAS_PATH="%s"' % Options.options.extras_path) if Options.options.data_path is not None: data_path = Options.options.data_path else: data_path = os.path.join(ctx.env.DATADIR, 'glmark2') ctx.env.append_unique('GLMARK_DATA_PATH', data_path) ctx.env.append_unique('DEFINES', 'GLMARK_DATA_PATH="%s"' % data_path) ctx.env.append_unique('DEFINES', 'GLMARK_VERSION="%s"' % VERSION) ctx.env.GLMARK2_VERSION = VERSION ctx.env.USE_GL = Options.options.gl ctx.env.USE_GLESv2 = Options.options.glesv2 ctx.msg("Prefix", ctx.env.PREFIX, color = 'PINK') ctx.msg("Data path", data_path, color = 'PINK') ctx.msg("Including extras", "Yes" if ctx.env.HAVE_EXTRAS else "No", color = 'PINK'); if ctx.env.HAVE_EXTRAS: ctx.msg("Extras path", Options.options.extras_path, color = 'PINK') ctx.msg("Building GL2 version", "Yes" if ctx.env.USE_GL else "No", color = 'PINK') ctx.msg("Building GLESv2 version", "Yes" if ctx.env.USE_GLESv2 else "No", color = 'PINK') def build(ctx): ctx.recurse('src') ctx.recurse('data') ctx.recurse('doc') class Glmark2Dist(Context.Context): """ Custom dist command that preserves symbolic links""" cmd = "dist" def execute(self): self.recurse([os.path.dirname(Context.g_module.root_path)]) self.archive() def get_files(self): import fnmatch files = [] excludes = ['*.bzr', '*~', './.*waf*', './build*', '*.swp', '*.pyc', '*glmark2-*.tar.gz'] for (dirpath, dirnames, filenames) in os.walk(top): names_to_remove = [] names = dirnames + filenames for n in names: for exclude in excludes: if fnmatch.fnmatch(os.path.join(dirpath, n), exclude): names_to_remove.append(n) break for d in names_to_remove: if d in dirnames: dirnames.remove(d) if d in filenames: filenames.remove(d) files.extend([os.path.join(dirpath, d) for d in dirnames]) files.extend([os.path.join(dirpath, f) for f in filenames]) return files def archive(self): import tarfile tar = tarfile.open(APPNAME + '-' + VERSION + '.tar.gz', 'w:gz') for f in self.get_files(): tar.add(f, arcname = APPNAME + '-' + VERSION + '/' + f, recursive = False) tar.close() glmark2-2012.08/./README.android0000664000175000017500000000277212013417376015225 0ustar alfalf00000000000000Command-line arguments ====================== The Android version of glmark2 can accept command-line arguments from either an extra intent key or a file. If arguments are specified in an intent key, the file is disregarded. Arguments from an extra intent key ---------------------------------- The 'args' extra intent key is used to specify arguments. For example: am start -a android.intent.action.MAIN \ -n org.linaro.glmark2/org.linaro.glmark2.Glmark2Activity \ -e args '-b :duration=2 -b texture -f /path/file --debug' Arguments from a file --------------------- If the 'args' intent key is not defined, the contents of the file '/data/glmark2/args' (if present) are used as command line arguments. The arguments can be placed in either a single or multiple lines. For example: -b :duration -b texture -f /path/file --debug Android limitations and peculiarities ===================================== The Android version of glmark2 doesn't accept all of the command-line arguments that the X11 version accepts. In particular, the Android version currently ignores the following: --validate --frame-end --off-screen --reuse-context --fullscreen -l,--list-scenes The default visual config used on Android is: 'red=5:green=6:blue=5:alpha=0:depth=16:buffer=1' Of course, you can change it using the '--visual-config' option. The Android system is free to resize the application at will, so although the '-s,--size' option is initially taken into account, it usually doesn't have any lasting effect. glmark2-2012.08/./README0000664000175000017500000000077012013417376013602 0ustar alfalf00000000000000glmark2 is an OpenGL 2.0 and ES 2.0 benchmark. glmark2 was developed by Alexandros Frantzis based on the original glmark benchmark by Ben Smith. It is licensed under the GPLv3 (see COPYING). To build glmark2 for X11/GL(ES2) you need: * python 2.x (>= 2.4) for the build system (waf) * libpng 1.2 and for OpenGL 2.0: * libGL or for OpenGL ES 2.0: * libEGL * libGLESv2 Read the INSTALL file for building/installation instructions, Read the INSTALL.android file for instructions for Android. glmark2-2012.08/./INSTALL.android0000664000175000017500000000220512013417376015365 0ustar alfalf00000000000000The minimum Android API level for glmark2 is 9 (>= Android 2.3). Building using the SDK and NDK ------------------------------ To build and install glmark2 you need the Android SDK and NDK. The 'android', 'adb' and 'ndk-build' tools used below are included there. To build glmark2 for Android we start by building the native part: $ cd android $ ndk-build To continue building the package from the command line: $ android update project -p . -s $ ant debug {or release} To install to a device, you need to have set up an ADB connection to the device. Then do: $ adb install bin/Glmark2Activity-debug.apk Alternatively you can load the project (in the android/ directory) in Eclipse using the ADT plugin and manage the build and install process from there. Keep in mind that when updating the native build (ndk-build etc) you need to refresh the Eclipse project, otherwise it won't notice that something has changed. Building using the Android build system --------------------------------------- Copy the glmark2 source tree to somewhere the Android build system can access it (eg external/glmark2) and build the GLMark2 module: $ make GLMark2 glmark2-2012.08/./NEWS0000664000175000017500000001644412013417376013426 0ustar alfalf00000000000000glmark2 2012.08 (20120817) ========================== * Display benchmark results in the Android GUI. * Add an Android option menu offering load/save/delete benchmark list functionality, access to last benchmark results, settings and information about glmark2. * Properly handle spaces in scene options on Android. * Exclude from score calculation benchmarks whose set up wasn't successful. * Display an appropriate message (instead of a 0 FPS value) if benchmark set up isn't successful. * Add support for standard GNU installation directories. glmark2 2012.07 (20120719) ========================== * Add Android GUI for defining and running benchmarks. * Add benchmark based on the WebGL jellyfish demo (scene 'jellyfish'). * Add benchmark based on the WebGL dynamic terrain demo (scene 'terrain'). * Extend texture scene with the option to compute texture coordinates in the shader (option 'texgen'). * Add support for reading texture data from JPEG files. * Properly support options that have a finite set of acceptable values. glmark2 2012.06 (20120621) ========================== * Add command-line option to render in fullscreen mode (--fullscreen). * Display the frame time in addition to the FPS for each benchmark. glmark2 2012.05 (20120524) ========================== * Add benchmark based on the SGI "Ideas in Motion" demo. * Ensure that the framebuffer is drawn opaquely. * Add command-line option to configure the visual used for rendering (--visual-config). * Add support for additional models and textures in the 'texture' scene. glmark2 2012.03 (20120322) ========================== * X11: - Support rendering to an off-screen surface (--off-screen). - Add command-line option to select which method to use to "end" a frame (--frame-end). glmark2 2012.02 (20120216) ========================== * Consolidate X11 and Android main loops. * Accept command-line options on Android through either a file ('/data/glmark2/args') or an extra intent key ('args'). * Support per-scene options for displaying an FPS count on screen (show-fps, fps-pos, fps-size), and remove --show-fps command-line option. * Support per-scene options for displaying a benchmark title on screen (title, title-pos, title-size). * Add command-line option to run benchmarks indefinitely (--run-forever). * Add command-line option to annotate the benchmarks with on-screen information (--annotate == -b :show-fps=true:title=#info#). * Move various utility classes to libmatrix. * Fix build issues on Android ICS. glmark2 2012.01 (20120119) ========================== * X11: - Run each benchmark in a fresh GL context. Use --reuse-context to revert to the old default behavior of using the same context for all benchmarks. - Add option for showing a live FPS counter on screen (--show-fps). - If the list of benchmarks to run contains only option-setting descriptions, run the default benchmarks. * Android: - Fix crash on platforms not supporting glMapBufferOES. - Log the glmark2 score when finishing. - Log an error message if we fail to find a suitable EGLContext. - Implement various stability improvements. glmark2 2011.12 (20111215) ========================== * Add benchmark for bilinear filtering implemented in the fragment shader (not included in the default benchmarks). * Ensure we don't call any GL functions before binding a GL context. * Fix bug in the ShaderSource object that could lead to shader compilation errors in strict OpenGL ES 2.0 implementations. * Refactor scene update code to reduce duplication. glmark2 2011.11 (20111116) ========================== * Add benchmark for bump mapping using a height map. * Add benchmark for bump mapping using a tangent space normal map (not included in the default benchmarks). * Implement validation support for all default benchmarks. * Add a colored prefix to log messages in debug mode. * Clean up and refactor code. glmark2 2011.10 (20111018) ========================== * Add benchmark for buffer (VBO) updates. * Add benchmark for drop-shadow desktop effect. * Add support for glmark2 extra large models. * Enable the selection of additional models in the shading benchmark. * Gracefully handle unsupported OpenGL versions, on both X11 and Android. * Disable screen dimming and screen rotation on Android. glmark2 2011.09 (20110921) ========================== * Add benchmark for blur desktop effect. * Add support for multiple lights in the phong shading benchmark. * Add support for loading models from OBJ geometry files. * Add Stanford Bunny model and make it available in the build benchmark. * Add per-scene options to set shader precision at runtime. * Add command-line option to specify the benchmarks to run using a text file. glmark2 2011.08 (20110818) ========================== * Port to Android (see INSTALL.android). * Add benchmark based on pulsar X11 GL screensaver. * Add benchmark for 2D image processing using the GPU. * Add command line option to set the size of the output window (-s, --size). * Implement ShaderSource object to simplify complex shader creation. * Implement utility functions to access resources in an abstract way. glmark2 2011.07 (20110719) ========================== * Replace SDL with custom window handling code. * Add benchmark for shader conditionals. * Add benchmark for shader function calls. * Add benchmark for shader loops. * Add benchmark for real phong (vs blinn-phong) lighting model. * Add benchmark for normal mapping. * Refactor Mesh class to increase flexibility in vertex attribute handling. glmark2 2011.06 (20110624) ========================== * Improve benchmark versatility by allowing runtime-configurable, per-scene options. * Add command line option to list avalaible scenes and their supported options (-l,--list-scenes). * Allow specifying the scenes to run and their options from the command line (-b,--benchmark). * Add basic output validation functionality (--validate). * Add command line option to call glFinish() instead of swapping the front and back buffers (--no-swap-buffers). * Manually disable VSync for GL/GLX (work around an SDL bug). * Replace custom math and shader infrastructure with functionality provided by LibMatrix (lp:libmatrix). * Improve user documentation (--help and man page). glmark2 11.05 (20110530) ========================== * Don't use the SDL_OPENGL flag for GLESv2 (LP: #761848). * Ensure our screen updates are not synchronized with the vertical retrace (LP: 761855). * Use the correct GL headers depending on the flavor (desktop vs ES2). * Query the correct GL object for shader linking status. glmark2 11.01 (20110125) ========================== * Fix visual corruption in glmark2-es2 due to missing depth buffer. * Fix linking issues with gcc 4.5. * Use correct GL functions to manipulate shaders vs programs. * Make result reporting more parser-friendly. * Upgrade build system to waf 1.6.2. glmark2 10.07.1 (20100728) ========================== * Define the precision of fragment shader variables for OpenGL ES 2.0. glmark2 10.07 (20100715) ======================== * First release. * Included benchmarks: - Rendering using vertex arrays - Rendering using VBOs - Texturing using nearest filtering - Texturing using linear filtering - Texturing using trilinear mipmapped filtering - Lighting per vertex using simple GLSL shaders - Lighting per pixel using elaborate GLSL shaders glmark2-2012.08/./COPYING.SGI0000664000175000017500000000416112013417376014374 0ustar alfalf00000000000000The data and some of the control logic for the "ideas" scene was adapted from the "Ideas in Motion" GLUT demo. Per the rights granted by the source for that demo, the following notice is reproduced here: /* * (c) Copyright 1993, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * * US Government Users Restricted Rights * Use, duplication, or disclosure by the Government is subject to * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph * (c)(1)(ii) of the Rights in Technical Data and Computer Software * clause at DFARS 252.227-7013 and/or in similar or successor * clauses in the FAR or the DOD or NASA FAR Supplement. * Unpublished-- rights reserved under the copyright laws of the * United States. Contractor/manufacturer is Silicon Graphics, * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. * * OpenGL(TM) is a trademark of Silicon Graphics, Inc. */ glmark2-2012.08/./COPYING0000664000175000017500000010451312013417376013755 0ustar alfalf00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . glmark2-2012.08/./src/libjpeg-turbo/0000775000175000017500000000000012013417376016252 5ustar alfalf00000000000000glmark2-2012.08/./src/scene-ideas/0000775000175000017500000000000012013417376015665 5ustar alfalf00000000000000glmark2-2012.08/./src/libmatrix/0000775000175000017500000000000012013417376015500 5ustar alfalf00000000000000glmark2-2012.08/./src/scene-terrain/0000775000175000017500000000000012013417376016244 5ustar alfalf00000000000000glmark2-2012.08/./src/libpng/0000775000175000017500000000000012013417376014760 5ustar alfalf00000000000000glmark2-2012.08/./src/gl-headers.cpp0000664000175000017500000000264012013417376016226 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "gl-headers.h" void* (*GLExtensions::MapBuffer) (GLenum target, GLenum access) = 0; GLboolean (*GLExtensions::UnmapBuffer) (GLenum target) = 0; bool GLExtensions::support(const std::string &ext) { std::string ext_string; const char* exts = reinterpret_cast(glGetString(GL_EXTENSIONS)); if (exts) { ext_string = exts; } const size_t ext_size = ext.size(); size_t pos = 0; while ((pos = ext_string.find(ext, pos)) != std::string::npos) { char c = ext_string[pos + ext_size]; if (c == ' ' || c == '\0') break; } return pos != std::string::npos; } glmark2-2012.08/./src/scene-build.cpp0000664000175000017500000001743412013417376016414 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include "scene.h" #include "log.h" #include "mat.h" #include "stack.h" #include "shader-source.h" #include "model.h" #include "util.h" #include SceneBuild::SceneBuild(Canvas &pCanvas) : Scene(pCanvas, "build"), orientModel_(false) { const ModelMap& modelMap = Model::find_models(); std::string optionValues; for (ModelMap::const_iterator modelIt = modelMap.begin(); modelIt != modelMap.end(); modelIt++) { static bool doSeparator(false); if (doSeparator) { optionValues += ","; } const std::string& curName = modelIt->first; optionValues += curName; doSeparator = true; } options_["use-vbo"] = Scene::Option("use-vbo", "true", "Whether to use VBOs for rendering", "false,true"); options_["interleave"] = Scene::Option("interleave", "false", "Whether to interleave vertex attribute data", "false,true"); options_["model"] = Scene::Option("model", "horse", "Which model to use", optionValues); } SceneBuild::~SceneBuild() { } bool SceneBuild::load() { rotationSpeed_ = 36.0f; running_ = false; return true; } void SceneBuild::unload() { mesh_.reset(); } bool SceneBuild::setup() { using LibMatrix::vec3; if (!Scene::setup()) return false; /* Set up shaders */ static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.frag"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); static const LibMatrix::vec4 materialDiffuse(1.0f, 1.0f, 1.0f, 1.0f); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); vtx_source.add_const("LightSourcePosition", lightPosition); vtx_source.add_const("MaterialDiffuse", materialDiffuse); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } Model model; const std::string& whichModel(options_["model"].value); bool modelLoaded = model.load(whichModel); if(!modelLoaded) return false; // Now that we're successfully loaded, there are a few quirks about // some of the known models that we need to account for. The draw // logic for the scene wants to rotate the model around the Y axis. // Most of our models are described this way. Some need adjustment // (an additional rotation that gets the model into the correct // orientation). // // Here's a summary: // // Angel rotates around the Y axis // Armadillo rotates around the Y axis // Buddha rotates around the X axis // Bunny rotates around the Y axis // Dragon rotates around the X axis // Horse rotates around the Y axis if (whichModel == "buddha" || whichModel == "dragon") { orientModel_ = true; orientationAngle_ = -90.0; orientationVec_ = vec3(1.0, 0.0, 0.0); } else if (whichModel == "armadillo") { orientModel_ = true; orientationAngle_ = 180.0; orientationVec_ = vec3(0.0, 1.0, 0.0); } model.calculate_normals(); /* Tell the converter that we only care about position and normal attributes */ std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); model.convert_to_mesh(mesh_, attribs); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); useVbo_ = (options_["use-vbo"].value == "true"); bool interleave = (options_["interleave"].value == "true"); mesh_.vbo_update_method(Mesh::VBOUpdateMethodMap); mesh_.interleave(interleave); if (useVbo_) mesh_.build_vbo(); else mesh_.build_array(); /* Calculate a projection matrix that is a good fit for the model */ vec3 maxVec = model.maxVec(); vec3 minVec = model.minVec(); vec3 diffVec = maxVec - minVec; centerVec_ = maxVec + minVec; centerVec_ /= 2.0; float diameter = diffVec.length(); radius_ = diameter / 2; float fovy = 2.0 * atanf(radius_ / (2.0 + radius_)); fovy /= M_PI; fovy *= 180.0; float aspect(static_cast(canvas_.width())/static_cast(canvas_.height())); perspective_.setIdentity(); perspective_ *= LibMatrix::Mat4::perspective(fovy, aspect, 2.0, 2.0 + diameter); program_.start(); currentFrame_ = 0; rotation_ = 0.0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneBuild::teardown() { program_.stop(); program_.release(); mesh_.reset(); Scene::teardown(); } void SceneBuild::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; rotation_ = rotationSpeed_ * elapsed_time; } void SceneBuild::draw() { LibMatrix::Stack4 model_view; // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::mat4 model_view_proj(perspective_); model_view.translate(-centerVec_.x(), -centerVec_.y(), -(centerVec_.z() + 2.0 + radius_)); model_view.rotate(rotation_, 0.0f, 1.0f, 0.0f); if (orientModel_) { model_view.rotate(orientationAngle_, orientationVec_.x(), orientationVec_.y(), orientationVec_.z()); } model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. LibMatrix::mat4 normal_matrix(model_view.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; if (useVbo_) { mesh_.render_vbo(); } else { mesh_.render_array(); } } Scene::ValidationResult SceneBuild::validate() { static const double radius_3d(std::sqrt(3.0)); if (rotation_ != 0) return Scene::ValidationUnknown; Canvas::Pixel ref(0xa7, 0xa7, 0xa7, 0xff); Canvas::Pixel pixel = canvas_.read_pixel(canvas_.width() / 2, canvas_.height() / 2); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } } glmark2-2012.08/./src/image-reader.cpp0000664000175000017500000002130512013417376016534 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include #include #include #include #include "image-reader.h" #include "log.h" #include "util.h" /******* * PNG * *******/ struct PNGReaderPrivate { PNGReaderPrivate() : png(0), info(0), rows(0), png_error(0), current_row(0), row_stride(0) {} static void png_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) { std::istream *is = reinterpret_cast(png_get_io_ptr(png_ptr)); is->read(reinterpret_cast(data), length); } png_structp png; png_infop info; png_bytepp rows; bool png_error; unsigned int current_row; unsigned int row_stride; }; PNGReader::PNGReader(const std::string& filename): priv_(new PNGReaderPrivate()) { priv_->png_error = !init(filename); } PNGReader::~PNGReader() { finish(); delete priv_; } bool PNGReader::error() { return priv_->png_error; } bool PNGReader::nextRow(unsigned char *dst) { bool ret; if (priv_->current_row < height()) { memcpy(dst, priv_->rows[priv_->current_row], priv_->row_stride); priv_->current_row++; ret = true; } else { ret = false; } return ret; } unsigned int PNGReader::width() const { return png_get_image_width(priv_->png, priv_->info); } unsigned int PNGReader::height() const { return png_get_image_height(priv_->png, priv_->info); } unsigned int PNGReader::pixelBytes() const { if (png_get_color_type(priv_->png, priv_->info) == PNG_COLOR_TYPE_RGB) { return 3; } return 4; } bool PNGReader::init(const std::string& filename) { static const int png_transforms = PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_GRAY_TO_RGB | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND; Log::debug("Reading PNG file %s\n", filename.c_str()); const std::auto_ptr is_ptr(Util::get_resource(filename)); if (!(*is_ptr)) { Log::error("Cannot open file %s!\n", filename.c_str()); return false; } /* Set up all the libpng structs we need */ priv_->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); if (!priv_->png) { Log::error("Couldn't create libpng read struct\n"); return false; } priv_->info = png_create_info_struct(priv_->png); if (!priv_->info) { Log::error("Couldn't create libpng info struct\n"); return false; } /* Set up libpng error handling */ if (setjmp(png_jmpbuf(priv_->png))) { Log::error("libpng error while reading file %s\n", filename.c_str()); return false; } /* Read the image information and data */ png_set_read_fn(priv_->png, reinterpret_cast(is_ptr.get()), PNGReaderPrivate::png_read_fn); png_read_png(priv_->png, priv_->info, png_transforms, 0); priv_->rows = png_get_rows(priv_->png, priv_->info); priv_->current_row = 0; priv_->row_stride = width() * pixelBytes(); return true; } void PNGReader::finish() { if (priv_->png) { png_destroy_read_struct(&priv_->png, &priv_->info, 0); } } /******** * JPEG * ********/ struct JPEGErrorMgr { struct jpeg_error_mgr pub; jmp_buf jmp_buffer; JPEGErrorMgr() { jpeg_std_error(&pub); pub.error_exit = error_exit; } static void error_exit(j_common_ptr cinfo) { JPEGErrorMgr *err = reinterpret_cast(cinfo->err); char buffer[JMSG_LENGTH_MAX]; /* Create the message */ (*cinfo->err->format_message)(cinfo, buffer); std::string msg(std::string(buffer) + "\n"); Log::error(msg.c_str()); longjmp(err->jmp_buffer, 1); } }; struct JPEGIStreamSourceMgr { static const int BUFFER_SIZE = 4096; struct jpeg_source_mgr pub; std::istream *is; JOCTET buffer[BUFFER_SIZE]; JPEGIStreamSourceMgr(const std::string& filename) : is(0) { is = Util::get_resource(filename); /* Fill in jpeg_source_mgr pub struct */ pub.init_source = init_source; pub.fill_input_buffer = fill_input_buffer; pub.skip_input_data = skip_input_data; pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ pub.term_source = term_source; pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ pub.next_input_byte = NULL; /* until buffer loaded */ } ~JPEGIStreamSourceMgr() { delete is; } bool error() { return !is || (is->fail() && !is->eof()); } static void init_source(j_decompress_ptr cinfo) { static_cast(cinfo); } static boolean fill_input_buffer(j_decompress_ptr cinfo) { JPEGIStreamSourceMgr *src = reinterpret_cast(cinfo->src); src->is->read(reinterpret_cast(src->buffer), BUFFER_SIZE); src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer = src->is->gcount(); /* * If the decoder needs more data, but we have no more bytes left to * read mark the end of input. */ if (src->pub.bytes_in_buffer == 0) { src->pub.bytes_in_buffer = 2; src->buffer[0] = 0xFF; src->buffer[0] = JPEG_EOI; } return TRUE; } static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) { JPEGIStreamSourceMgr *src = reinterpret_cast(cinfo->src); if (num_bytes > 0) { size_t n = static_cast(num_bytes); while (n > src->pub.bytes_in_buffer) { n -= src->pub.bytes_in_buffer; (*src->fill_input_buffer)(cinfo); } src->pub.next_input_byte += n; src->pub.bytes_in_buffer -= n; } } static void term_source(j_decompress_ptr cinfo) { static_cast(cinfo); } }; struct JPEGReaderPrivate { JPEGReaderPrivate(const std::string& filename) : source_mgr(filename), jpeg_error(false) {} struct jpeg_decompress_struct cinfo; JPEGErrorMgr error_mgr; JPEGIStreamSourceMgr source_mgr; bool jpeg_error; }; JPEGReader::JPEGReader(const std::string& filename) : priv_(new JPEGReaderPrivate(filename)) { priv_->jpeg_error = !init(filename); } JPEGReader::~JPEGReader() { finish(); delete priv_; } bool JPEGReader::error() { return priv_->jpeg_error || priv_->source_mgr.error(); } bool JPEGReader::nextRow(unsigned char *dst) { bool ret = true; unsigned char *buffer[1]; buffer[0] = dst; /* Set up error handling */ if (setjmp(priv_->error_mgr.jmp_buffer)) { return false; } /* While there are lines left, read next line */ if (priv_->cinfo.output_scanline < priv_->cinfo.output_height) { jpeg_read_scanlines(&priv_->cinfo, buffer, 1); } else { jpeg_finish_decompress(&priv_->cinfo); ret = false; } return ret; } unsigned int JPEGReader::width() const { return priv_->cinfo.output_width; } unsigned int JPEGReader::height() const { return priv_->cinfo.output_height; } unsigned int JPEGReader::pixelBytes() const { return priv_->cinfo.output_components; } bool JPEGReader::init(const std::string& filename) { Log::debug("Reading JPEG file %s\n", filename.c_str()); /* Initialize error manager */ priv_->cinfo.err = reinterpret_cast(&priv_->error_mgr); if (setjmp(priv_->error_mgr.jmp_buffer)) { return false; } jpeg_create_decompress(&priv_->cinfo); priv_->cinfo.src = reinterpret_cast(&priv_->source_mgr); /* Read header */ jpeg_read_header(&priv_->cinfo, TRUE); jpeg_start_decompress(&priv_->cinfo); return true; } void JPEGReader::finish() { jpeg_destroy_decompress(&priv_->cinfo); } glmark2-2012.08/./src/gl-visual-config.cpp0000664000175000017500000001010212013417376017351 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "gl-visual-config.h" #include "util.h" #include "log.h" #include GLVisualConfig::GLVisualConfig(const std::string &s) : red(1), green(1), blue(1), alpha(1), depth(1), buffer(1) { std::vector elems; Util::split(s, ':', elems, Util::SplitModeNormal); for (std::vector::const_iterator iter = elems.begin(); iter != elems.end(); iter++) { std::vector opt; Util::split(*iter, '=', opt, Util::SplitModeNormal); if (opt.size() == 2) { if (opt[0] == "r" || opt[0] == "red") red = Util::fromString(opt[1]); else if (opt[0] == "g" || opt[0] == "green") green = Util::fromString(opt[1]); else if (opt[0] == "b" || opt[0] == "blue") blue = Util::fromString(opt[1]); else if (opt[0] == "a" || opt[0] == "alpha") alpha = Util::fromString(opt[1]); else if (opt[0] == "d" || opt[0] == "depth") depth = Util::fromString(opt[1]); else if (opt[0] == "buf" || opt[0] == "buffer") buffer = Util::fromString(opt[1]); } else Log::info("Warning: ignoring invalid option string '%s' " "in GLVisualConfig description\n", iter->c_str()); } } int GLVisualConfig::match_score(const GLVisualConfig &target) const { int score(0); /* * R,G,B,A integer values are at most 8 bits wide (for current widespread * hardware), so we need to scale them by 4 to get them in the [0,32] range. */ score += score_component(red, target.red, 4); score += score_component(green, target.green, 4); score += score_component(blue, target.blue, 4); score += score_component(alpha, target.alpha, 4); score += score_component(depth, target.depth, 1); score += score_component(buffer, target.buffer, 1); return score; } int GLVisualConfig::score_component(int component, int target, int scale) const { /* * The maximum (positive) score that can be returned is based * on the maximum bit width of the components. We assume this to * be 32 bits, which is a reasonable assumption for current platforms. */ static const int MAXIMUM_COMPONENT_SCORE = 32; static const int UNACCEPTABLE_COMPONENT_PENALTY = -1000; int score(0); if ((component > 0 && target == 0) || (component == 0 && target > 0)) { /* * Penalize components that are not present but have been requested, * and components that have been excluded but are present. */ score = UNACCEPTABLE_COMPONENT_PENALTY; } else if (component == target) { /* Reward exact matches with the maximum per component score */ score = MAXIMUM_COMPONENT_SCORE; } else { /* * Reward deeper than requested component values, penalize shallower * than requested component values. Because the ranges of component * values vary we use a scaling factor to even them out, so that the * score for all components ranges from [0,MAXIMUM_COMPONENT_SCORE). */ score = scale * (component - target); } return score; } glmark2-2012.08/./src/text-renderer.h0000664000175000017500000000273012013417376016450 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_TEXT_RENDERER_H_ #define GLMARK2_TEXT_RENDERER_H_ #include #include "gl-headers.h" #include "vec.h" #include "program.h" #include "canvas.h" /** * Renders text using OpenGL textures. */ class TextRenderer { public: TextRenderer(Canvas& canvas); ~TextRenderer(); void text(const std::string& t); void position(const LibMatrix::vec2& p); void size(float s); void render(); private: void create_geometry(); LibMatrix::vec2 get_glyph_coords(char c); Canvas& canvas_; bool dirty_; std::string text_; LibMatrix::vec2 position_; LibMatrix::vec2 size_; Program program_; GLuint vbo_[2]; GLuint texture_; }; #endif glmark2-2012.08/./src/canvas-x11-egl.cpp0000664000175000017500000002226212013417376016644 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "canvas-x11-egl.h" #include "log.h" #include "options.h" #include #include #include /********************* * Protected methods * *********************/ XVisualInfo * CanvasX11EGL::get_xvisualinfo() { XVisualInfo vis_tmpl; XVisualInfo *vis_info; int num_visuals; EGLint vid; if (!ensure_egl_config()) return 0; if (!eglGetConfigAttrib(egl_display_, egl_config_, EGL_NATIVE_VISUAL_ID, &vid)) { Log::error("eglGetConfigAttrib() failed with error: %d\n", eglGetError()); return 0; } /* The X window visual must match the EGL config */ vis_tmpl.visualid = vid; vis_info = XGetVisualInfo(xdpy_, VisualIDMask, &vis_tmpl, &num_visuals); if (!vis_info) { Log::error("couldn't get X visual\n"); return 0; } return vis_info; } bool CanvasX11EGL::make_current() { if (!ensure_egl_surface()) return false; if (!ensure_egl_context()) return false; if (egl_context_ == eglGetCurrentContext()) return true; if (!eglMakeCurrent(egl_display_, egl_surface_, egl_surface_, egl_context_)) { Log::error("Error: eglMakeCurrent failed with error %d\n", eglGetError()); return false; } if (!eglSwapInterval(egl_display_, 0)) Log::info("** Failed to set swap interval. Results may be bounded above by refresh rate.\n"); init_gl_extensions(); return true; } void CanvasX11EGL::get_glvisualconfig(GLVisualConfig &visual_config) { if (!ensure_egl_config()) return; get_glvisualconfig_egl(egl_config_, visual_config); } /******************* * Private methods * *******************/ bool CanvasX11EGL::ensure_egl_display() { if (egl_display_) return true; egl_display_ = eglGetDisplay((EGLNativeDisplayType) xdpy_); if (!egl_display_) { Log::error("eglGetDisplay() failed with error: %d\n", eglGetError()); return false; } if (!eglInitialize(egl_display_, NULL, NULL)) { Log::error("eglInitialize() failed with error: %d\n", eglGetError()); return false; egl_display_ = 0; } return true; } bool CanvasX11EGL::ensure_egl_config() { const EGLint attribs[] = { EGL_RED_SIZE, visual_config_.red, EGL_GREEN_SIZE, visual_config_.green, EGL_BLUE_SIZE, visual_config_.blue, EGL_ALPHA_SIZE, visual_config_.alpha, EGL_DEPTH_SIZE, visual_config_.depth, EGL_BUFFER_SIZE, visual_config_.buffer, #ifdef USE_GLESv2 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, #elif USE_GL EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, #endif EGL_NONE }; EGLint num_configs; EGLint vid; if (egl_config_) return true; if (!ensure_egl_display()) return false; /* Find out how many configs match the attributes */ if (!eglChooseConfig(egl_display_, attribs, 0, 0, &num_configs)) { Log::error("eglChooseConfig() (explore) failed with error: %d\n", eglGetError()); return false; } if (num_configs == 0) { Log::error("eglChooseConfig() didn't return any configs\n"); return false; } /* Get all the matching configs */ std::vector configs(num_configs); if (!eglChooseConfig(egl_display_, attribs, &(configs[0]), num_configs, &num_configs)) { Log::error("eglChooseConfig() failed with error: %d\n", eglGetError()); return false; } /* Select the best matching config */ egl_config_ = select_best_config(configs); if (!eglGetConfigAttrib(egl_display_, egl_config_, EGL_NATIVE_VISUAL_ID, &vid)) { Log::error("eglGetConfigAttrib() failed with error: %d\n", eglGetError()); return false; } if (Options::show_debug) { int buf, red, green, blue, alpha, depth, id, native_id; eglGetConfigAttrib(egl_display_, egl_config_, EGL_CONFIG_ID, &id); eglGetConfigAttrib(egl_display_, egl_config_, EGL_NATIVE_VISUAL_ID, &native_id); eglGetConfigAttrib(egl_display_, egl_config_, EGL_BUFFER_SIZE, &buf); eglGetConfigAttrib(egl_display_, egl_config_, EGL_RED_SIZE, &red); eglGetConfigAttrib(egl_display_, egl_config_, EGL_GREEN_SIZE, &green); eglGetConfigAttrib(egl_display_, egl_config_, EGL_BLUE_SIZE, &blue); eglGetConfigAttrib(egl_display_, egl_config_, EGL_ALPHA_SIZE, &alpha); eglGetConfigAttrib(egl_display_, egl_config_, EGL_DEPTH_SIZE, &depth); Log::debug("EGL chosen config ID: 0x%x Native Visual ID: 0x%x\n" " Buffer: %d bits\n" " Red: %d bits\n" " Green: %d bits\n" " Blue: %d bits\n" " Alpha: %d bits\n" " Depth: %d bits\n", id, native_id, buf, red, green, blue, alpha, depth); } return true; } bool CanvasX11EGL::reset_context() { if (!ensure_egl_display()) return false; if (!egl_context_) return true; if (eglDestroyContext(egl_display_, egl_context_) == EGL_FALSE) { Log::debug("eglDestroyContext() failed with error: 0x%x\n", eglGetError()); } egl_context_ = 0; return true; } bool CanvasX11EGL::ensure_egl_context() { if (egl_context_) return true; if (!ensure_egl_display()) return false; if (!ensure_egl_config()) return false; static const EGLint ctx_attribs[] = { #ifdef USE_GLESv2 EGL_CONTEXT_CLIENT_VERSION, 2, #endif EGL_NONE }; egl_context_ = eglCreateContext(egl_display_, egl_config_, EGL_NO_CONTEXT, ctx_attribs); if (!egl_context_) { Log::error("eglCreateContext() failed with error: 0x%x\n", eglGetError()); return false; } return true; } bool CanvasX11EGL::ensure_egl_surface() { if (egl_surface_) return true; if (!ensure_egl_display()) return false; #ifdef USE_GLESv2 eglBindAPI(EGL_OPENGL_ES_API); #elif USE_GL eglBindAPI(EGL_OPENGL_API); #endif egl_surface_ = eglCreateWindowSurface(egl_display_, egl_config_, (EGLNativeWindowType) xwin_, NULL); if (!egl_surface_) { Log::error("eglCreateWindowSurface failed with error: %d\n", eglGetError()); return false; } return true; } void CanvasX11EGL::init_gl_extensions() { #if USE_GLESv2 if (GLExtensions::support("GL_OES_mapbuffer")) { GLExtensions::MapBuffer = reinterpret_cast(eglGetProcAddress("glMapBufferOES")); GLExtensions::UnmapBuffer = reinterpret_cast(eglGetProcAddress("glUnmapBufferOES")); } #elif USE_GL GLExtensions::MapBuffer = glMapBuffer; GLExtensions::UnmapBuffer = glUnmapBuffer; #endif } void CanvasX11EGL::get_glvisualconfig_egl(EGLConfig config, GLVisualConfig &visual_config) { eglGetConfigAttrib(egl_display_, config, EGL_BUFFER_SIZE, &visual_config.buffer); eglGetConfigAttrib(egl_display_, config, EGL_RED_SIZE, &visual_config.red); eglGetConfigAttrib(egl_display_, config, EGL_GREEN_SIZE, &visual_config.green); eglGetConfigAttrib(egl_display_, config, EGL_BLUE_SIZE, &visual_config.blue); eglGetConfigAttrib(egl_display_, config, EGL_ALPHA_SIZE, &visual_config.alpha); eglGetConfigAttrib(egl_display_, config, EGL_DEPTH_SIZE, &visual_config.depth); } EGLConfig CanvasX11EGL::select_best_config(std::vector configs) { int best_score(INT_MIN); EGLConfig best_config(0); /* * Go through all the configs and choose the one with the best score, * i.e., the one better matching the requested config. */ for (std::vector::const_iterator iter = configs.begin(); iter != configs.end(); iter++) { const EGLConfig config(*iter); GLVisualConfig vc; int score; get_glvisualconfig_egl(config, vc); score = vc.match_score(visual_config_); if (score > best_score) { best_score = score; best_config = config; } } return best_config; } glmark2-2012.08/./src/scene-shading.cpp0000664000175000017500000002610712013417376016727 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "util.h" #include "shader-source.h" #include "model.h" #include #include using LibMatrix::vec3; using std::string; using std::endl; SceneShading::SceneShading(Canvas &pCanvas) : Scene(pCanvas, "shading"), orientModel_(false) { const ModelMap& modelMap = Model::find_models(); std::string optionValues; for (ModelMap::const_iterator modelIt = modelMap.begin(); modelIt != modelMap.end(); modelIt++) { static bool doSeparator(false); if (doSeparator) { optionValues += ","; } const std::string& curName = modelIt->first; optionValues += curName; doSeparator = true; } options_["shading"] = Scene::Option("shading", "gouraud", "Which shading method to use", "gouraud,blinn-phong-inf,phong"); options_["num-lights"] = Scene::Option("num-lights", "1", "The number of lights applied to the scene (phong only)"); options_["model"] = Scene::Option("model", "cat", "Which model to use", optionValues); } SceneShading::~SceneShading() { } bool SceneShading::load() { rotationSpeed_ = 36.0f; running_ = false; return true; } void SceneShading::unload() { mesh_.reset(); } static string get_fragment_shader_source(const string& frg_file, unsigned int lights) { ShaderSource source(frg_file); static const string lightPositionName("LightSourcePosition"); static const string lightColorName("LightColor"); static const string callCompute(" gl_FragColor += compute_color("); static const string commaString(", "); static const string rParenString(");"); std::stringstream doLightSS; doLightSS << string(" gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);"); doLightSS << endl; float theta(2.0 * M_PI / lights); float phi(theta / 2.0); float intensity(0.8 / lights); LibMatrix::vec4 lightCol(intensity, intensity, intensity, 1.0); for (unsigned int l = 0; l < lights; l++) { // Construct constant names for the light position and color and add it // to the list of constants for the shader. string indexString(Util::toString(l)); string curLightPosition(lightPositionName + indexString); string curLightColor(lightColorName + indexString); float sin_theta(sin(theta * l)); float cos_theta(cos(theta * l)); float sin_phi(sin(phi * l)); float cos_phi(cos(phi * l)); LibMatrix::vec4 lightPos(cos_phi * sin_theta, cos_phi * cos_theta, sin_phi, 1.0); source.add_const(curLightPosition, lightPos); source.add_const(curLightColor, lightCol); // Add the section of source to the substantive... doLightSS << callCompute; doLightSS << curLightPosition; doLightSS << commaString; doLightSS << curLightColor; doLightSS << rParenString; doLightSS << endl; } source.replace("$DO_LIGHTS$", doLightSS.str()); return source.str(); } bool SceneShading::setup() { if (!Scene::setup()) return false; static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); static const LibMatrix::vec4 materialDiffuse(0.0f, 0.0f, 1.0f, 1.0f); // Calculate half vector for blinn-phong shading model LibMatrix::vec3 halfVector(lightPosition[0], lightPosition[1], lightPosition[2]); halfVector.normalize(); halfVector += LibMatrix::vec3(0.0, 0.0, 1.0); halfVector.normalize(); // Load and add constants to shaders std::string vtx_shader_filename; std::string frg_shader_filename; const std::string &shading = options_["shading"].value; ShaderSource vtx_source; ShaderSource frg_source; if (shading == "gouraud") { vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.vert"; frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.frag"; frg_source.append_file(frg_shader_filename); vtx_source.append_file(vtx_shader_filename); vtx_source.add_const("LightSourcePosition", lightPosition); vtx_source.add_const("MaterialDiffuse", materialDiffuse); } else if (shading == "blinn-phong-inf") { vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-advanced.vert"; frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-advanced.frag"; frg_source.append_file(frg_shader_filename); frg_source.add_const("LightSourcePosition", lightPosition); frg_source.add_const("LightSourceHalfVector", halfVector); vtx_source.append_file(vtx_shader_filename); } else if (shading == "phong") { vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-phong.vert"; frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-phong.frag"; unsigned int num_lights = Util::fromString(options_["num-lights"].value); string fragsource = get_fragment_shader_source(frg_shader_filename, num_lights); frg_source.append(fragsource); frg_source.add_const("MaterialDiffuse", materialDiffuse); vtx_source.append_file(vtx_shader_filename); } if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } Model model; const std::string& whichModel(options_["model"].value); bool modelLoaded = model.load(whichModel); if(!modelLoaded) return false; // Now that we're successfully loaded, there are a few quirks about // some of the known models that we need to account for. The draw // logic for the scene wants to rotate the model around the Y axis. // Most of our models are described this way. Some need adjustment // (an additional rotation that gets the model into the correct // orientation). // // Here's a summary: // // Angel rotates around the Y axis // Armadillo rotates around the Y axis // Buddha rotates around the X axis // Bunny rotates around the Y axis // Dragon rotates around the X axis // Horse rotates around the Y axis if (whichModel == "buddha" || whichModel == "dragon") { orientModel_ = true; orientationAngle_ = -90.0; orientationVec_ = vec3(1.0, 0.0, 0.0); } else if (whichModel == "armadillo") { orientModel_ = true; orientationAngle_ = 180.0; orientationVec_ = vec3(0.0, 1.0, 0.0); } model.calculate_normals(); /* Tell the converter that we only care about position and normal attributes */ std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); model.convert_to_mesh(mesh_, attribs); mesh_.build_vbo(); /* Calculate a projection matrix that is a good fit for the model */ vec3 maxVec = model.maxVec(); vec3 minVec = model.minVec(); vec3 diffVec = maxVec - minVec; centerVec_ = maxVec + minVec; centerVec_ /= 2.0; float diameter = diffVec.length(); radius_ = diameter / 2; float fovy = 2.0 * atanf(radius_ / (2.0 + radius_)); fovy /= M_PI; fovy *= 180.0; float aspect(static_cast(canvas_.width())/static_cast(canvas_.height())); perspective_.setIdentity(); perspective_ *= LibMatrix::Mat4::perspective(fovy, aspect, 2.0, 2.0 + diameter); program_.start(); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); currentFrame_ = 0; rotation_ = 0.0f; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneShading::teardown() { program_.stop(); program_.release(); Scene::teardown(); } void SceneShading::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; rotation_ = rotationSpeed_ * elapsed_time; } void SceneShading::draw() { // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::Stack4 model_view; model_view.translate(-centerVec_.x(), -centerVec_.y(), -(centerVec_.z() + 2.0 + radius_)); model_view.rotate(rotation_, 0.0f, 1.0f, 0.0f); if (orientModel_) { model_view.rotate(orientationAngle_, orientationVec_.x(), orientationVec_.y(), orientationVec_.z()); } LibMatrix::mat4 model_view_proj(perspective_); model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. LibMatrix::mat4 normal_matrix(model_view.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; // Load the modelview matrix itself program_["ModelViewMatrix"] = model_view.getCurrent(); mesh_.render_vbo(); } Scene::ValidationResult SceneShading::validate() { static const double radius_3d(std::sqrt(3.0)); if (rotation_ != 0) return Scene::ValidationUnknown; Canvas::Pixel ref; Canvas::Pixel pixel = canvas_.read_pixel(canvas_.width() / 3, canvas_.height() / 3); const std::string &filter = options_["shading"].value; if (filter == "gouraud") ref = Canvas::Pixel(0x00, 0x00, 0x2d, 0xff); else if (filter == "blinn-phong-inf") ref = Canvas::Pixel(0x1a, 0x1a, 0x3e, 0xff); else if (filter == "phong" && options_["num-lights"].value == "1") ref = Canvas::Pixel(0x05, 0x05, 0xad, 0xff); else return Scene::ValidationUnknown; double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } } glmark2-2012.08/./src/benchmark.h0000664000175000017500000000621612013417376015615 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_BENCHMARK_H_ #define GLMARK2_BENCHMARK_H_ #include #include #include #include "scene.h" /** * A glmark2 benchmark. * * A benchmark is a Scene configured with a set of option values. */ class Benchmark { public: typedef std::pair OptionPair; /** * Creates a benchmark using a scene object reference. * * @param scene the scene to use * @param options the options to use */ Benchmark(Scene &scene, const std::vector &options); /** * Creates a benchmark using a scene name. * * To use a scene by name, that scene must have been previously registered * using ::register_scene(). * * @param name the name of the scene to use * @param options the options to use */ Benchmark(const std::string &name, const std::vector &options); /** * Creates a benchmark from a description string. * * The description string is of the form scene[:opt1=val1:opt2=val2...]. * The specified scene must have been previously registered using * ::register_scene(). * * @param s a description string */ Benchmark(const std::string &s); /** * Gets the Scene associated with the benchmark. * * This method doesn't prepare the scene for a run. * (See ::setup_scene()) * * @return the Scene */ Scene &scene() const { return scene_; } /** * Sets up the Scene associated with the benchmark. * * @return the Scene */ Scene &setup_scene(); /** * Tears down the Scene associated with the benchmark. */ void teardown_scene(); /** * Whether the benchmark needs extra decoration. */ bool needs_decoration() const; /** * Registers a Scene, so that it becomes accessible by name. */ static void register_scene(Scene &scene); /** * Gets a registered scene by its name. * * @return the Scene */ static Scene &get_scene_by_name(const std::string &name); /** * Gets the registered scenes. * * @return the Scene */ static const std::map &scenes() { return sceneMap_; } private: Scene &scene_; std::vector options_; void load_options(); static std::map sceneMap_; }; #endif glmark2-2012.08/./src/benchmark.cpp0000664000175000017500000000767312013417376016160 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "benchmark.h" #include "log.h" #include "util.h" using std::string; using std::vector; using std::map; std::map Benchmark::sceneMap_; static Scene & get_scene_from_description(const string &s) { vector elems; Util::split(s, ':', elems, Util::SplitModeNormal); const string &name = !elems.empty() ? elems[0] : ""; return Benchmark::get_scene_by_name(name); } static vector get_options_from_description(const string &s) { vector options; vector elems; Util::split(s, ':', elems, Util::SplitModeNormal); for (vector::const_iterator iter = elems.begin() + 1; iter != elems.end(); iter++) { vector opt; Util::split(*iter, '=', opt, Util::SplitModeNormal); if (opt.size() == 2) options.push_back(Benchmark::OptionPair(opt[0], opt[1])); else Log::info("Warning: ignoring invalid option string '%s' " "in benchmark description\n", iter->c_str()); } return options; } void Benchmark::register_scene(Scene &scene) { sceneMap_[scene.name()] = &scene; } Scene & Benchmark::get_scene_by_name(const string &name) { map::const_iterator iter; if ((iter = sceneMap_.find(name)) != sceneMap_.end()) return *(iter->second); else return Scene::dummy(); } Benchmark::Benchmark(Scene &scene, const vector &options) : scene_(scene), options_(options) { } Benchmark::Benchmark(const string &name, const vector &options) : scene_(Benchmark::get_scene_by_name(name)), options_(options) { } Benchmark::Benchmark(const string &s) : scene_(get_scene_from_description(s)), options_(get_options_from_description(s)) { } Scene & Benchmark::setup_scene() { scene_.reset_options(); load_options(); scene_.load(); scene_.setup(); return scene_; } void Benchmark::teardown_scene() { scene_.teardown(); scene_.unload(); } bool Benchmark::needs_decoration() const { for (vector::const_iterator iter = options_.begin(); iter != options_.end(); iter++) { if ((iter->first == "show-fps" && iter->second == "true") || (iter->first == "title" && !iter->second.empty())) { return true; } } return false; } void Benchmark::load_options() { for (vector::iterator iter = options_.begin(); iter != options_.end(); iter++) { if (!scene_.set_option(iter->first, iter->second)) { map::const_iterator opt_iter = scene_.options().find(iter->first); if (opt_iter == scene_.options().end()) { Log::info("Warning: Scene '%s' doesn't accept option '%s'\n", scene_.name().c_str(), iter->first.c_str()); } else { Log::info("Warning: Scene '%s' doesn't accept value '%s' for option '%s'\n", scene_.name().c_str(), iter->second.c_str(), iter->first.c_str()); } } } } glmark2-2012.08/./src/canvas-x11.cpp0000664000175000017500000003200312013417376016071 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker */ #include "canvas-x11.h" #include "log.h" #include "options.h" #include "util.h" #include #include #include #include /****************** * Public methods * ******************/ bool CanvasX11::reset() { release_fbo(); if (!reset_context()) return false; if (!do_make_current()) return false; if (!supports_gl2()) { Log::error("Glmark2 needs OpenGL(ES) version >= 2.0 to run" " (but version string is: '%s')!\n", glGetString(GL_VERSION)); return false; } glViewport(0, 0, width_, height_); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); clear(); return true; } bool CanvasX11::init() { xdpy_ = XOpenDisplay(NULL); if (!xdpy_) return false; resize_no_viewport(width_, height_); if (!xwin_) return false; return reset(); } void CanvasX11::visible(bool visible) { if (visible && !offscreen_) XMapWindow(xdpy_, xwin_); } void CanvasX11::clear() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); #if USE_GL glClearDepth(1.0f); #elif USE_GLESv2 glClearDepthf(1.0f); #endif glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void CanvasX11::update() { Options::FrameEnd m = Options::frame_end; if (m == Options::FrameEndDefault) { if (offscreen_) m = Options::FrameEndFinish; else m = Options::FrameEndSwap; } switch(m) { case Options::FrameEndSwap: swap_buffers(); break; case Options::FrameEndFinish: glFinish(); break; case Options::FrameEndReadPixels: read_pixel(width_ / 2, height_ / 2); break; case Options::FrameEndNone: default: break; } } void CanvasX11::print_info() { do_make_current(); std::stringstream ss; ss << " OpenGL Information" << std::endl; ss << " GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl; ss << " GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl; ss << " GL_VERSION: " << glGetString(GL_VERSION) << std::endl; Log::info("%s", ss.str().c_str()); } Canvas::Pixel CanvasX11::read_pixel(int x, int y) { uint8_t pixel[4]; glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); return Canvas::Pixel(pixel[0], pixel[1], pixel[2], pixel[3]); } void CanvasX11::write_to_file(std::string &filename) { char *pixels = new char[width_ * height_ * 4]; for (int i = 0; i < height_; i++) { glReadPixels(0, i, width_, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[(height_ - i - 1) * width_ * 4]); } std::ofstream output (filename.c_str(), std::ios::out | std::ios::binary); output.write(pixels, 4 * width_ * height_); delete [] pixels; } bool CanvasX11::should_quit() { XEvent event; if (!XPending(xdpy_)) return false; XNextEvent(xdpy_, &event); if (event.type == KeyPress) { if (XLookupKeysym(&event.xkey, 0) == XK_Escape) return true; } else if (event.type == ClientMessage) { /* Window Delete event from window manager */ return true; } return false; } void CanvasX11::resize(int width, int height) { resize_no_viewport(width, height); glViewport(0, 0, width_, height_); } unsigned int CanvasX11::fbo() { return fbo_; } bool CanvasX11::supports_gl2() { std::string gl_version_str(reinterpret_cast(glGetString(GL_VERSION))); int gl_major(0); size_t point_pos(gl_version_str.find('.')); if (point_pos != std::string::npos) { point_pos--; size_t start_pos(gl_version_str.rfind(' ', point_pos)); if (start_pos == std::string::npos) start_pos = 0; else start_pos++; gl_major = Util::fromString( gl_version_str.substr(start_pos, point_pos - start_pos + 1) ); } return gl_major >= 2; } /******************* * Private methods * *******************/ bool CanvasX11::ensure_x_window() { static const char *win_name("glmark2 "GLMARK_VERSION); if (xwin_) return true; if (!xdpy_) { Log::error("Error: X11 Display has not been initialized!\n"); return false; } XVisualInfo *vis_info = get_xvisualinfo(); if (!vis_info) { Log::error("Error: Could not get a valid XVisualInfo!\n"); return false; } Log::debug("Creating XWindow W: %d H: %d VisualID: 0x%x\n", width_, height_, vis_info->visualid); /* window attributes */ XSetWindowAttributes attr; unsigned long mask; Window root = RootWindow(xdpy_, DefaultScreen(xdpy_)); attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(xdpy_, root, vis_info->visual, AllocNone); attr.event_mask = KeyPressMask; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; xwin_ = XCreateWindow(xdpy_, root, 0, 0, width_, height_, 0, vis_info->depth, InputOutput, vis_info->visual, mask, &attr); XFree(vis_info); if (!xwin_) { Log::error("Error: XCreateWindow() failed!\n"); return false; } /* set hints and properties */ if (fullscreen_) { Atom atom = XInternAtom(xdpy_, "_NET_WM_STATE_FULLSCREEN", True); XChangeProperty(xdpy_, xwin_, XInternAtom(xdpy_, "_NET_WM_STATE", True), XA_ATOM, 32, PropModeReplace, reinterpret_cast(&atom), 1); } else { XSizeHints sizehints; sizehints.min_width = width_; sizehints.min_height = height_; sizehints.max_width = width_; sizehints.max_height = height_; sizehints.flags = PMaxSize | PMinSize; XSetWMProperties(xdpy_, xwin_, NULL, NULL, NULL, 0, &sizehints, NULL, NULL); } /* Set the window name */ XStoreName(xdpy_ , xwin_, win_name); /* Gracefully handle Window Delete event from window manager */ Atom wmDelete = XInternAtom(xdpy_, "WM_DELETE_WINDOW", True); XSetWMProtocols(xdpy_, xwin_, &wmDelete, 1); return true; } void CanvasX11::resize_no_viewport(int width, int height) { bool request_fullscreen = (width == -1 || height == -1); /* Recreate an existing window only if it has actually been resized */ if (xwin_) { if (width_ != width || height_ != height || fullscreen_ != request_fullscreen) { XDestroyWindow(xdpy_, xwin_); xwin_ = 0; } else { return; } } fullscreen_ = request_fullscreen; if (fullscreen_) { /* Get the screen (root window) size */ XWindowAttributes window_attr; XGetWindowAttributes(xdpy_, RootWindow(xdpy_, DefaultScreen(xdpy_)), &window_attr); width_ = window_attr.width; height_ = window_attr.height; } else { width_ = width; height_ = height; } if (!ensure_x_window()) Log::error("Error: Couldn't create X Window!\n"); if (color_renderbuffer_) { glBindRenderbuffer(GL_RENDERBUFFER, color_renderbuffer_); glRenderbufferStorage(GL_RENDERBUFFER, gl_color_format_, width_, height_); } if (depth_renderbuffer_) { glBindRenderbuffer(GL_RENDERBUFFER, depth_renderbuffer_); glRenderbufferStorage(GL_RENDERBUFFER, gl_depth_format_, width_, height_); } projection_ = LibMatrix::Mat4::perspective(60.0, width_ / static_cast(height_), 1.0, 1024.0); } bool CanvasX11::do_make_current() { if (!make_current()) return false; if (offscreen_) { if (!ensure_fbo()) return false; glBindFramebuffer(GL_FRAMEBUFFER, fbo_); } return true; } bool CanvasX11::ensure_gl_formats() { if (gl_color_format_ && gl_depth_format_) return true; GLVisualConfig vc; get_glvisualconfig(vc); gl_color_format_ = 0; gl_depth_format_ = 0; bool supports_rgba8(false); bool supports_rgb8(false); bool supports_depth24(false); bool supports_depth32(false); #if USE_GLESv2 if (GLExtensions::support("GL_ARM_rgba8")) supports_rgba8 = true; if (GLExtensions::support("GL_OES_rgb8_rgba8")) { supports_rgba8 = true; supports_rgb8 = true; } if (GLExtensions::support("GL_OES_depth24")) supports_depth24 = true; if (GLExtensions::support("GL_OES_depth32")) supports_depth32 = true; #elif USE_GL supports_rgba8 = true; supports_rgb8 = true; supports_depth24 = true; supports_depth32 = true; #endif if (vc.buffer == 32) { if (supports_rgba8) gl_color_format_ = GL_RGBA8; else gl_color_format_ = GL_RGBA4; } else if (vc.buffer == 24) { if (supports_rgb8) gl_color_format_ = GL_RGB8; else gl_color_format_ = GL_RGB565; } else if (vc.buffer == 16) { if (vc.red == 4 && vc.green == 4 && vc.blue == 4 && vc.alpha == 4) { gl_color_format_ = GL_RGBA4; } else if (vc.red == 5 && vc.green == 5 && vc.blue == 5 && vc.alpha == 1) { gl_color_format_ = GL_RGB5_A1; } else if (vc.red == 5 && vc.green == 6 && vc.blue == 5 && vc.alpha == 0) { gl_color_format_ = GL_RGB565; } } if (vc.depth == 32 && supports_depth32) gl_depth_format_ = GL_DEPTH_COMPONENT32; else if (vc.depth >= 24 && supports_depth24) gl_depth_format_ = GL_DEPTH_COMPONENT24; else if (vc.depth == 16) gl_depth_format_ = GL_DEPTH_COMPONENT16; Log::debug("Selected Renderbuffer ColorFormat: %s DepthFormat: %s\n", get_gl_format_str(gl_color_format_), get_gl_format_str(gl_depth_format_)); return (gl_color_format_ && gl_depth_format_); } bool CanvasX11::ensure_fbo() { if (!fbo_) { if (!ensure_gl_formats()) return false; /* Create a texture for the color attachment */ glGenRenderbuffers(1, &color_renderbuffer_); glBindRenderbuffer(GL_RENDERBUFFER, color_renderbuffer_); glRenderbufferStorage(GL_RENDERBUFFER, gl_color_format_, width_, height_); /* Create a renderbuffer for the depth attachment */ glGenRenderbuffers(1, &depth_renderbuffer_); glBindRenderbuffer(GL_RENDERBUFFER, depth_renderbuffer_); glRenderbufferStorage(GL_RENDERBUFFER, gl_depth_format_, width_, height_); /* Create a FBO and set it up */ glGenFramebuffers(1, &fbo_); glBindFramebuffer(GL_FRAMEBUFFER, fbo_); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_renderbuffer_); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_renderbuffer_); } return true; } void CanvasX11::release_fbo() { glDeleteFramebuffers(1, &fbo_); glDeleteRenderbuffers(1, &color_renderbuffer_); glDeleteRenderbuffers(1, &depth_renderbuffer_); fbo_ = 0; color_renderbuffer_ = 0; depth_renderbuffer_ = 0; gl_color_format_ = 0; gl_depth_format_ = 0; } const char * CanvasX11::get_gl_format_str(GLenum f) { const char *str; switch(f) { case GL_RGBA8: str = "GL_RGBA8"; break; case GL_RGB8: str = "GL_RGB8"; break; case GL_RGBA4: str = "GL_RGBA4"; break; case GL_RGB5_A1: str = "GL_RGB5_A1"; break; case GL_RGB565: str = "GL_RGB565"; break; case GL_DEPTH_COMPONENT16: str = "GL_DEPTH_COMPONENT16"; break; case GL_DEPTH_COMPONENT24: str = "GL_DEPTH_COMPONENT24"; break; case GL_DEPTH_COMPONENT32: str = "GL_DEPTH_COMPONENT32"; break; case GL_NONE: str = "GL_NONE"; break; default: str = "Unknown"; break; } return str; } glmark2-2012.08/./src/canvas.h0000664000175000017500000001543412013417376015140 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker */ #ifndef GLMARK2_CANVAS_H_ #define GLMARK2_CANVAS_H_ #include "gl-headers.h" #include "mat.h" #include "gl-visual-config.h" #include #include #include #include /** * Abstraction for a GL rendering target. */ class Canvas { public: virtual ~Canvas() {} /** * A pixel value. */ struct Pixel { Pixel(): r(0), g(0), b(0), a(0) {} Pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t a): r(r), g(g), b(b), a(a) {} /** * Gets the pixel value as a 32-bit integer in 0xAABBGGRR format. * * @return the pixel value */ uint32_t to_le32() { return static_cast(r) + (static_cast(g) << 8) + (static_cast(b) << 16) + (static_cast(a) << 24); } /** * Gets the euclidian distance from this pixel in 3D RGB space. * * @param p the pixel to get the distance from * * @return the euclidian distance */ double distance_rgb(const Canvas::Pixel &p) { // These work without casts because of integer promotion rules // (the uint8_ts are promoted to ints) double d = (r - p.r) * (r - p.r) + (g - p.g) * (g - p.g) + (b - p.b) * (b - p.b); return std::sqrt(d); } uint8_t r; uint8_t g; uint8_t b; uint8_t a; }; /** * Initializes the canvas and makes it the target of GL operations. * * This method should be implemented in derived classes. * * @return whether initialization succeeded */ virtual bool init() { return false; } /** * Resets the canvas, destroying and recreating resources to give each new * test scenario a fresh context for rendering. * * This method should be implemented in derived classes. * * @return whether reset succeeded */ virtual bool reset() { return false; } /** * Changes the visibility of the canvas. * * The canvas is initially not visible. * * This method should be implemented in derived classes. * * @param visible true to make the Canvas visible, false otherwise */ virtual void visible(bool visible) { static_cast(visible); } /** * Clears the canvas. * This method should be implemented in derived classes. */ virtual void clear() {} /** * Ensures that the canvas on-screen representation gets updated * with the latest canvas contents. * * This method should be implemented in derived classes. */ virtual void update() {} /** * Prints information about the canvas. * * This method should be implemented in derived classes. */ virtual void print_info() {} /** * Reads a pixel from the canvas. * * The (0, 0) point is the lower left corner. The X and Y coordinates * increase towards the right and top, respectively. * * This method should be implemented in derived classes. * * @param x the X coordinate * @param y the Y coordinate * * @return the pixel */ virtual Pixel read_pixel(int x, int y) { static_cast(x); static_cast(y); return Pixel(); } /** * Writes the canvas contents to a file. * * The pixel save order is upper left to lower right. Each pixel value * is stored as four consecutive bytes R,G,B,A. * * This method should be implemented in derived classes. * * @param filename the name of the file to write to */ virtual void write_to_file(std::string &filename) { static_cast(filename); } /** * Whether we should quit the application. * * This method should be implemented in derived classes. * * @return true if we should quit, false otherwise */ virtual bool should_quit() { return false; } /** * Resizes the canvas. * * This method should be implemented in derived classes. * * @param width the new width in pixels * @param height the new height in pixels * * @return true if we should quit, false otherwise */ virtual void resize(int width, int height) { static_cast(width); static_cast(height); } /** * Gets the FBO associated with the canvas. * * @return the FBO */ virtual unsigned int fbo() { return 0; } /** * Gets a dummy canvas object. * * @return the dummy canvas */ static Canvas &dummy() { static Canvas dummy_canvas(0, 0); return dummy_canvas; } /** * Gets the width of the canvas. * * @return the width in pixels */ int width() { return width_; } /** * Gets the height of the canvas. * * @return the height in pixels */ int height() { return height_; } /** * Gets the projection matrix recommended for use with the canvas. * * It's not mandatory to use this projection matrix. * * @return the projection matrix */ const LibMatrix::mat4 &projection() { return projection_; } /** * Sets whether the canvas should be backed by an off-screen surface. * * This takes effect after the next init()/reset(). */ void offscreen(bool offscreen) { offscreen_ = offscreen; } /** * Sets the preferred visual configuration. * * This takes effect after the next init()/reset(). */ void visual_config(GLVisualConfig &config) { visual_config_ = config; } protected: Canvas(int width, int height) : width_(width), height_(height), offscreen_(false) {} int width_; int height_; LibMatrix::mat4 projection_; bool offscreen_; GLVisualConfig visual_config_; }; #endif glmark2-2012.08/./src/benchmark-collection.cpp0000664000175000017500000000577112013417376020306 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include #include "benchmark-collection.h" #include "default-benchmarks.h" #include "options.h" #include "log.h" #include "util.h" BenchmarkCollection::~BenchmarkCollection() { Util::dispose_pointer_vector(benchmarks_); } void BenchmarkCollection::add(const std::vector &benchmarks) { for (std::vector::const_iterator iter = benchmarks.begin(); iter != benchmarks.end(); iter++) { benchmarks_.push_back(new Benchmark(*iter)); } } void BenchmarkCollection::populate_from_options() { if (Options::annotate) { std::vector annotate; annotate.push_back(":show-fps=true:title=#info#"); add(annotate); } if (!Options::benchmarks.empty()) add(Options::benchmarks); if (!Options::benchmark_files.empty()) add_benchmarks_from_files(); if (!benchmarks_contain_normal_scenes()) add(DefaultBenchmarks::get()); } bool BenchmarkCollection::needs_decoration() { for (std::vector::const_iterator bench_iter = benchmarks_.begin(); bench_iter != benchmarks_.end(); bench_iter++) { const Benchmark *bench = *bench_iter; if (bench->needs_decoration()) return true; } return false; } void BenchmarkCollection::add_benchmarks_from_files() { for (std::vector::const_iterator iter = Options::benchmark_files.begin(); iter != Options::benchmark_files.end(); iter++) { std::ifstream ifs(iter->c_str()); if (!ifs.fail()) { std::string line; while (getline(ifs, line)) { if (!line.empty()) benchmarks_.push_back(new Benchmark(line)); } } else { Log::error("Cannot open benchmark file %s\n", iter->c_str()); } } } bool BenchmarkCollection::benchmarks_contain_normal_scenes() { for (std::vector::const_iterator bench_iter = benchmarks_.begin(); bench_iter != benchmarks_.end(); bench_iter++) { const Benchmark *bench = *bench_iter; if (!bench->scene().name().empty()) return true; } return false; } glmark2-2012.08/./src/options.cpp0000664000175000017500000001642212013417376015711 0ustar alfalf00000000000000/* * Copyright © 2011-2012 Linaro Limited * * This file is part of glcompbench. * * glcompbench is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * glcompbench is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with glcompbench. If not, see . * * Authors: * Alexandros Frantzis * Jesse Barker */ #include #include #include #include #include #include "options.h" #include "util.h" std::vector Options::benchmarks; std::vector Options::benchmark_files; bool Options::validate = false; Options::FrameEnd Options::frame_end = Options::FrameEndDefault; std::pair Options::size(800, 600); bool Options::list_scenes = false; bool Options::show_all_options = false; bool Options::show_debug = false; bool Options::show_help = false; bool Options::reuse_context = false; bool Options::run_forever = false; bool Options::annotate = false; bool Options::offscreen = false; GLVisualConfig Options::visual_config; static struct option long_options[] = { {"annotate", 0, 0, 0}, {"benchmark", 1, 0, 0}, {"benchmark-file", 1, 0, 0}, {"validate", 0, 0, 0}, {"frame-end", 1, 0, 0}, {"off-screen", 0, 0, 0}, {"visual-config", 1, 0, 0}, {"reuse-context", 0, 0, 0}, {"run-forever", 0, 0, 0}, {"size", 1, 0, 0}, {"fullscreen", 0, 0, 0}, {"list-scenes", 0, 0, 0}, {"show-all-options", 0, 0, 0}, {"debug", 0, 0, 0}, {"help", 0, 0, 0}, {0, 0, 0, 0} }; /** * Parses a size string of the form WxH * * @param str the string to parse * @param size the parsed size (width, height) */ static void parse_size(const std::string &str, std::pair &size) { std::vector d; Util::split(str, 'x', d, Util::SplitModeNormal); size.first = Util::fromString(d[0]); /* * Parse the second element (height). If there is none, use the value * of the first element for the second (width = height) */ if (d.size() > 1) size.second = Util::fromString(d[1]); else size.second = size.first; } /** * Parses a frame-end method string * * @param str the string to parse * * @return the parsed frame end method */ static Options::FrameEnd frame_end_from_str(const std::string &str) { Options::FrameEnd m = Options::FrameEndDefault; if (str == "swap") m = Options::FrameEndSwap; else if (str == "finish") m = Options::FrameEndFinish; else if (str == "readpixels") m = Options::FrameEndReadPixels; else if (str == "none") m = Options::FrameEndNone; return m; } void Options::print_help() { printf("A benchmark for Open GL (ES) 2.0\n" "\n" "Options:\n" " -b, --benchmark BENCH A benchmark to run: 'scene(:opt1=val1)*'\n" " (the option can be used multiple times)\n" " -f, --benchmark-file F Load benchmarks to run from a file containing a\n" " list of benchmark descriptions (one per line)\n" " (the option can be used multiple times)\n" " --validate Run a quick output validation test instead of \n" " running the benchmarks\n" " --frame-end METHOD How to end a frame [default,none,swap,finish,readpixels]\n" " --off-screen Render to an off-screen surface\n" " --visual-config C The visual configuration to use for the rendering\n" " target: 'red=R:green=G:blue=B:alpha=A:buffer=BUF'.\n" " The parameters may be defined in any order, and any\n" " omitted parameters assume a default value of '1'\n" " --reuse-context Use a single context for all scenes\n" " (by default, each scene gets its own context)\n" " -s, --size WxH Size of the output window (default: 800x600)\n" " --fullscreen Run in fullscreen mode (equivalent to --size -1x-1)\n" " -l, --list-scenes Display information about the available scenes\n" " and their options\n" " --show-all-options Show all scene option values used for benchmarks\n" " (only explicitly set options are shown by default)\n" " --run-forever Run indefinitely, looping from the last benchmark\n" " back to the first\n" " --annotate Annotate the benchmarks with on-screen information\n" " (same as -b :show-fps=true:title=#info#)\n" " -d, --debug Display debug messages\n" " -h, --help Display help\n"); } bool Options::parse_args(int argc, char **argv) { while (1) { int option_index = -1; int c; const char *optname = ""; c = getopt_long(argc, argv, "b:f:s:ldh", long_options, &option_index); if (c == -1) break; if (c == ':' || c == '?') return false; if (option_index != -1) optname = long_options[option_index].name; if (!strcmp(optname, "annotate")) Options::annotate = true; if (c == 'b' || !strcmp(optname, "benchmark")) Options::benchmarks.push_back(optarg); else if (c == 'f' || !strcmp(optname, "benchmark-file")) Options::benchmark_files.push_back(optarg); else if (!strcmp(optname, "validate")) Options::validate = true; else if (!strcmp(optname, "frame-end")) Options::frame_end = frame_end_from_str(optarg); else if (!strcmp(optname, "off-screen")) Options::offscreen = true; else if (!strcmp(optname, "visual-config")) Options::visual_config = GLVisualConfig(optarg); else if (!strcmp(optname, "reuse-context")) Options::reuse_context = true; else if (c == 's' || !strcmp(optname, "size")) parse_size(optarg, Options::size); else if (!strcmp(optname, "fullscreen")) Options::size = std::pair(-1, -1); else if (c == 'l' || !strcmp(optname, "list-scenes")) Options::list_scenes = true; else if (!strcmp(optname, "show-all-options")) Options::show_all_options = true; else if (!strcmp(optname, "run-forever")) Options::run_forever = true; else if (c == 'd' || !strcmp(optname, "debug")) Options::show_debug = true; else if (c == 'h' || !strcmp(optname, "help")) Options::show_help = true; } return true; } glmark2-2012.08/./src/scene-texture.cpp0000664000175000017500000002540512013417376017012 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "program.h" #include "shader-source.h" #include "texture.h" #include "model.h" #include "util.h" #include using LibMatrix::vec3; using std::string; SceneTexture::SceneTexture(Canvas &pCanvas) : Scene(pCanvas, "texture"), radius_(0.0), orientModel_(false), orientationAngle_(0.0) { const ModelMap& modelMap = Model::find_models(); string optionValues; for (ModelMap::const_iterator modelIt = modelMap.begin(); modelIt != modelMap.end(); modelIt++) { static bool doSeparator(false); if (doSeparator) { optionValues += ","; } const std::string& curName = modelIt->first; optionValues += curName; doSeparator = true; } options_["model"] = Scene::Option("model", "cube", "Which model to use", optionValues); options_["texture-filter"] = Scene::Option("texture-filter", "nearest", "The texture filter to use", "nearest,linear,linear-shader,mipmap"); optionValues = ""; const TextureMap& textureMap = Texture::find_textures(); for (TextureMap::const_iterator textureIt = textureMap.begin(); textureIt != textureMap.end(); textureIt++) { static bool doSeparator(false); if (doSeparator) { optionValues += ","; } const std::string& curName = textureIt->first; optionValues += curName; doSeparator = true; } options_["texture"] = Scene::Option("texture", "crate-base", "Which texture to use", optionValues); options_["texgen"] = Scene::Option("texgen", "false", "Whether to generate texcoords in the shader", "false,true"); } SceneTexture::~SceneTexture() { } bool SceneTexture::load() { rotationSpeed_ = LibMatrix::vec3(36.0f, 36.0f, 36.0f); running_ = false; return true; } void SceneTexture::unload() { mesh_.reset(); } bool SceneTexture::setup() { if (!Scene::setup()) return false; static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.vert"); static const std::string vtx_shader_texgen_filename(GLMARK_DATA_PATH"/shaders/light-basic-texgen.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic-tex.frag"); static const std::string frg_shader_bilinear_filename(GLMARK_DATA_PATH"/shaders/light-basic-tex-bilinear.frag"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); static const LibMatrix::vec4 materialDiffuse(1.0f, 1.0f, 1.0f, 1.0f); // Create texture according to selected filtering GLint min_filter = GL_NONE; GLint mag_filter = GL_NONE; const std::string &filter = options_["texture-filter"].value; if (filter == "nearest") { min_filter = GL_NEAREST; mag_filter = GL_NEAREST; } else if (filter == "linear") { min_filter = GL_LINEAR; mag_filter = GL_LINEAR; } else if (filter == "linear-shader") { min_filter = GL_NEAREST; mag_filter = GL_NEAREST; } else if (filter == "mipmap") { min_filter = GL_LINEAR_MIPMAP_LINEAR; mag_filter = GL_LINEAR; } const string& whichTexture(options_["texture"].value); if (!Texture::load(whichTexture, &texture_, min_filter, mag_filter, 0)) return false; // Load shaders bool doTexGen(options_["texgen"].value == "true"); ShaderSource vtx_source; if (doTexGen) { vtx_source.append_file(vtx_shader_texgen_filename); vtx_source.add_const("PI", static_cast(M_PI)); } else { vtx_source.append_file(vtx_shader_filename); } ShaderSource frg_source; if (filter == "linear-shader") { frg_source.append_file(frg_shader_bilinear_filename); LibMatrix::vec2 texture_size(512, 512); frg_source.add_const("TextureSize", texture_size); } else { frg_source.append_file(frg_shader_filename); } // Add constants to shaders vtx_source.add_const("LightSourcePosition", lightPosition); vtx_source.add_const("MaterialDiffuse", materialDiffuse); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } Model model; const string& whichModel(options_["model"].value); bool modelLoaded = model.load(whichModel); if(!modelLoaded) return false; // Now that we're successfully loaded, there are a few quirks about // some of the known models that we need to account for. The draw // logic for the scene wants to rotate the model around the Y axis. // Most of our models are described this way. Some need adjustment // (an additional rotation that gets the model into the correct // orientation). // // Here's a summary: // // Angel rotates around the Y axis // Armadillo rotates around the Y axis // Buddha rotates around the X axis // Bunny rotates around the Y axis // Dragon rotates around the X axis // Horse rotates around the Y axis if (whichModel == "buddha" || whichModel == "dragon") { orientModel_ = true; orientationAngle_ = -90.0; orientationVec_ = vec3(1.0, 0.0, 0.0); } else if (whichModel == "armadillo") { orientModel_ = true; orientationAngle_ = 180.0; orientationVec_ = vec3(0.0, 1.0, 0.0); } if (model.needTexcoords()) model.calculate_texcoords(); model.calculate_normals(); // Tell the converter which attributes we care about std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); if (!doTexGen) { attribs.push_back(std::pair(Model::AttribTypeTexcoord, 2)); } model.convert_to_mesh(mesh_, attribs); mesh_.build_vbo(); // Calculate a projection matrix that is a good fit for the model vec3 maxVec = model.maxVec(); vec3 minVec = model.minVec(); vec3 diffVec = maxVec - minVec; centerVec_ = maxVec + minVec; centerVec_ /= 2.0; float diameter = diffVec.length(); radius_ = diameter / 2; float fovy = 2.0 * atanf(radius_ / (2.0 + radius_)); fovy /= M_PI; fovy *= 180.0; float aspect(static_cast(canvas_.width())/static_cast(canvas_.height())); perspective_.setIdentity(); perspective_ *= LibMatrix::Mat4::perspective(fovy, aspect, 2.0, 2.0 + diameter); program_.start(); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); if (doTexGen) { program_["CenterPoint"] = centerVec_; } else { attrib_locations.push_back(program_["texcoord"].location()); } mesh_.set_attrib_locations(attrib_locations); currentFrame_ = 0; rotation_ = LibMatrix::vec3(); running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneTexture::teardown() { program_.stop(); program_.release(); glDeleteTextures(1, &texture_); Scene::teardown(); } void SceneTexture::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; rotation_ = rotationSpeed_ * elapsed_time; } void SceneTexture::draw() { // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::Stack4 model_view; model_view.translate(-centerVec_.x(), -centerVec_.y(), -(centerVec_.z() + 2.5 + radius_)); model_view.rotate(rotation_.x(), 1.0f, 0.0f, 0.0f); model_view.rotate(rotation_.y(), 0.0f, 1.0f, 0.0f); model_view.rotate(rotation_.z(), 0.0f, 0.0f, 1.0f); if (orientModel_) { model_view.rotate(orientationAngle_, orientationVec_.x(), orientationVec_.y(), orientationVec_.z()); } LibMatrix::mat4 model_view_proj(perspective_); model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. LibMatrix::mat4 normal_matrix(model_view.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); mesh_.render_vbo(); } Scene::ValidationResult SceneTexture::validate() { static const double radius_3d(std::sqrt(3 * 2.0 * 2.0)); if (rotation_.x() != 0 || rotation_.y() != 0 || rotation_.z() != 0) return Scene::ValidationUnknown; Canvas::Pixel ref; Canvas::Pixel pixel = canvas_.read_pixel(canvas_.width() / 2 + 3, canvas_.height() / 2 + 3); const std::string &filter = options_["texture-filter"].value; if (filter == "nearest") ref = Canvas::Pixel(0x2b, 0x2a, 0x28, 0xff); else if (filter == "linear") ref = Canvas::Pixel(0x2b, 0x2a, 0x28, 0xff); else if (filter == "linear-shader") ref = Canvas::Pixel(0x2d, 0x2c, 0x2a, 0xff); else if (filter == "mipmap") ref = Canvas::Pixel(0x2c, 0x2d, 0x2a, 0xff); else return Scene::ValidationUnknown; double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } } glmark2-2012.08/./src/scene-terrain.cpp0000664000175000017500000003002712013417376016752 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "mesh.h" #include "util.h" #include "texture.h" #include "shader-source.h" #include "renderer.h" using LibMatrix::vec2; using LibMatrix::vec3; using LibMatrix::vec4; using LibMatrix::mat4; class SceneTerrainPrivate { public: SceneTerrainPrivate(Canvas &canvas, const LibMatrix::vec2 &repeat_overlay, bool use_bloom, bool use_tilt_shift) : canvas(canvas), repeat_overlay(repeat_overlay), use_bloom(use_bloom), use_tilt_shift(use_tilt_shift), terrain_renderer(0), bloom_v_renderer(0), bloom_h_renderer(0), overlay_renderer(0), tilt_v_renderer(0), tilt_h_renderer(0), copy_renderer(0), height_map_renderer(0), normal_map_renderer(0), specular_map_renderer(0), height_normal_chain(0), bloom_chain(0), tilt_chain(0), terrain_chain(0) { init_renderers(); } ~SceneTerrainPrivate() { release_renderers(); } void init_renderers() { /* Create and set up renderers */ const vec2 map_res(256.0f, 256.0f); const vec2 screen_res(canvas.width(), canvas.height()); const vec2 bloom_res(256.0f, 256.0f); const vec2 grass_res(512.0f, 512.0f); height_map_renderer = new SimplexNoiseRenderer(map_res); height_map_renderer->setup(height_map_renderer->size(), false, false); normal_map_renderer = new NormalFromHeightRenderer(map_res); normal_map_renderer->setup(normal_map_renderer->size(), false, false); specular_map_renderer = new LuminanceRenderer(grass_res); specular_map_renderer->setup_texture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); specular_map_renderer->setup(specular_map_renderer->size(), false, false); terrain_renderer = new TerrainRenderer(screen_res, repeat_overlay); terrain_renderer->setup(terrain_renderer->size(), !use_bloom && !use_tilt_shift, true); terrain_renderer->setup_texture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); /* Bloom */ if (use_bloom) { bloom_h_renderer = new BlurRenderer(bloom_res, 2, 4.0, BlurRenderer::BlurDirectionHorizontal, vec2(1.0, 1.0) / screen_res, 0.0); bloom_h_renderer->setup_texture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); bloom_h_renderer->setup(bloom_h_renderer->size(), false, false); bloom_v_renderer = new BlurRenderer(bloom_res, 2, 4.0, BlurRenderer::BlurDirectionVertical, vec2(1.0, 1.0) / bloom_res, 0.0); bloom_v_renderer->setup_texture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); bloom_v_renderer->setup(bloom_v_renderer->size(), false, false); overlay_renderer = new OverlayRenderer(*terrain_renderer, 0.6); } /* Tilt-shift */ if (use_tilt_shift) { tilt_h_renderer = new BlurRenderer(screen_res, 4, 2.7, BlurRenderer::BlurDirectionHorizontal, vec2(1.0, 1.0) / screen_res, 0.5); tilt_h_renderer->setup_texture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); tilt_h_renderer->setup(tilt_h_renderer->size(), false, false); tilt_v_renderer = new BlurRenderer(screen_res, 4, 2.7, BlurRenderer::BlurDirectionVertical, vec2(1.0, 1.0) / screen_res, 0.5); } /* Copy renderer */ if (use_bloom && !use_tilt_shift) copy_renderer = new CopyRenderer(screen_res); /* Height normal chain */ height_normal_chain = new RendererChain(); height_normal_chain->append(*height_map_renderer); height_normal_chain->append(*normal_map_renderer); /* Bloom effect chain */ if (use_bloom) { bloom_chain = new RendererChain(); bloom_chain->append(*bloom_h_renderer); bloom_chain->append(*bloom_v_renderer); bloom_chain->append(*overlay_renderer); } /* Tilt-shift effect chain */ if (use_tilt_shift) { tilt_chain = new RendererChain(); tilt_chain->append(*tilt_h_renderer); tilt_chain->append(*tilt_v_renderer); } /* Terrain chain */ terrain_chain = new RendererChain(); terrain_chain->append(*terrain_renderer); if (use_bloom) terrain_chain->append(*bloom_chain); if (use_tilt_shift) terrain_chain->append(*tilt_chain); /* * If are just using bloom, the terrain is rendered to a texture and * bloom applied on that texture. We need to "copy" that texture's * contents to the screen to make the scene visible. */ if (use_bloom && !use_tilt_shift) terrain_chain->append(*copy_renderer); /* * Set up renderer textures. */ terrain_renderer->height_map_texture(height_map_renderer->texture()); terrain_renderer->normal_map_texture(normal_map_renderer->texture()); terrain_renderer->specular_map_texture(specular_map_renderer->texture()); specular_map_renderer->input_texture(terrain_renderer->diffuse1_texture()); } void release_renderers() { delete terrain_chain; delete bloom_chain; delete tilt_chain; delete height_normal_chain; delete height_map_renderer; delete normal_map_renderer; delete specular_map_renderer; delete terrain_renderer; delete bloom_v_renderer; delete bloom_h_renderer; delete overlay_renderer; delete tilt_v_renderer; delete tilt_h_renderer; delete copy_renderer; } Canvas &canvas; LibMatrix::vec2 repeat_overlay; bool use_bloom; bool use_tilt_shift; /* Renderers */ TerrainRenderer *terrain_renderer; BlurRenderer *bloom_v_renderer; BlurRenderer *bloom_h_renderer; OverlayRenderer *overlay_renderer; BlurRenderer *tilt_v_renderer; BlurRenderer *tilt_h_renderer; CopyRenderer *copy_renderer; SimplexNoiseRenderer *height_map_renderer; NormalFromHeightRenderer *normal_map_renderer; LuminanceRenderer *specular_map_renderer; /* Chains */ RendererChain *height_normal_chain; RendererChain *bloom_chain; RendererChain *tilt_chain; RendererChain *terrain_chain; }; SceneTerrain::SceneTerrain(Canvas &pCanvas) : Scene(pCanvas, "terrain"), priv_(0) { options_["repeat-overlay"] = Scene::Option("repeat-overlay", "6.0", "How many times to repeat the terrain texture on the terrain plane (per side)"); options_["bloom"] = Scene::Option("bloom", "true", "Use bloom post-processing effect", "false,true"); options_["tilt-shift"] = Scene::Option("tilt-shift", "true", "Use tilt-shift post-processing effect", "false,true"); } SceneTerrain::~SceneTerrain() { delete priv_; } bool SceneTerrain::supported(bool show_errors) { GLint vertex_textures; glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &vertex_textures); if (show_errors && vertex_textures <= 0) { Log::error("SceneTerrain requires Vertex Texture Fetch support, " "but GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS is %d\n", vertex_textures); } return vertex_textures > 0; } bool SceneTerrain::load() { Scene::load(); running_ = false; return true; } void SceneTerrain::unload() { Scene::unload(); } bool SceneTerrain::setup() { if (!Scene::setup()) return false; /* Parse options */ float repeat = Util::fromString(options_["repeat-overlay"].value); LibMatrix::vec2 repeat_overlay(repeat, repeat); bool use_bloom = options_["bloom"].value == "true"; bool use_tilt_shift = options_["tilt-shift"].value == "true"; priv_ = new SceneTerrainPrivate(canvas_, repeat_overlay, use_bloom, use_tilt_shift); /* Set up terrain rendering program */ LibMatrix::Stack4 model; LibMatrix::Stack4 camera; LibMatrix::mat4 projection = LibMatrix::Mat4::perspective( 40.0, canvas_.width() / static_cast(canvas_.height()), 2.0, 4000.0); /* Place camera */ camera.lookAt(-1200.0f, 800.0f, 1200.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* Move and rotate plane */ model.translate(0.0f, -125.0f, 0.0f); model.rotate(-90.0, 1.0f, 0.0f, 0.0f); LibMatrix::mat4 view_matrix(camera.getCurrent()); LibMatrix::mat4 model_matrix(model.getCurrent()); LibMatrix::mat4 model_view_matrix(view_matrix * model_matrix); LibMatrix::mat4 normal_matrix(model_view_matrix); normal_matrix.inverse().transpose(); /* Set up terrain renderer program */ priv_->terrain_renderer->program().start(); priv_->terrain_renderer->program()["viewMatrix"] = view_matrix; priv_->terrain_renderer->program()["modelViewMatrix"] = model_view_matrix; priv_->terrain_renderer->program()["normalMatrix"] = normal_matrix; priv_->terrain_renderer->program()["projectionMatrix"] = projection; /* Create the specular map */ priv_->specular_map_renderer->render(); glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, canvas_.width(), canvas_.height()); currentFrame_ = 0; startTime_ = Util::get_timestamp_us() / 1000000.0; running_ = true; return true; } void SceneTerrain::teardown() { delete priv_; priv_ = 0; Scene::teardown(); } void SceneTerrain::update() { Scene::update(); double now = Util::get_timestamp_us() / 1000000.0; float diff = now - startTime_; float scale = priv_->terrain_renderer->repeat_overlay().x() / priv_->height_map_renderer->uv_scale().x(); /* Update height map */ priv_->height_map_renderer->program().start(); priv_->height_map_renderer->program()["uvOffset"] = vec2(diff * 0.05f, 0.0f); priv_->terrain_renderer->program().start(); priv_->terrain_renderer->program()["uOffset"] = vec2(scale * diff * 0.05f, 0.0f); } void SceneTerrain::draw() { /* Render the height and normal maps used by the terrain */ priv_->height_normal_chain->render(); /* Render the terrain plus any post-processing effects */ priv_->terrain_chain->render(); } Scene::ValidationResult SceneTerrain::validate() { return Scene::ValidationUnknown; } glmark2-2012.08/./src/canvas-x11-glx.h0000664000175000017500000000345212013417376016334 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_CANVAS_X11_GLX_H_ #define GLMARK2_CANVAS_X11_GLX_H_ #include "canvas-x11.h" #define GLX_GLXEXT_PROTOTYPES #include #include #include /** * Canvas for rendering to an X11 window using GLX. */ class CanvasX11GLX : public CanvasX11 { public: CanvasX11GLX(int width, int height) : CanvasX11(width, height), glx_fbconfig_(0), glx_context_(0) {} ~CanvasX11GLX() {} protected: XVisualInfo *get_xvisualinfo(); bool make_current(); bool reset_context(); void swap_buffers() { glXSwapBuffers(xdpy_, xwin_); } void get_glvisualconfig(GLVisualConfig &visual_config); private: bool check_glx_version(); void init_extensions(); bool ensure_glx_fbconfig(); bool ensure_glx_context(); void init_gl_extensions(); void get_glvisualconfig_glx(GLXFBConfig config, GLVisualConfig &visual_config); GLXFBConfig select_best_config(std::vector configs); GLXFBConfig glx_fbconfig_; GLXContext glx_context_; }; #endif glmark2-2012.08/./src/mesh.h0000664000175000017500000001001612013417376014610 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #ifndef GLMARK2_MESH_H_ #define GLMARK2_MESH_H_ #include #include "vec.h" #include "gl-headers.h" /** * A mesh of vertices. */ class Mesh { public: Mesh(); ~Mesh(); void set_vertex_format(const std::vector &format); void set_attrib_locations(const std::vector &locations); void set_attrib(unsigned int pos, const LibMatrix::vec2 &v, std::vector *vertex = 0); void set_attrib(unsigned int pos, const LibMatrix::vec3 &v, std::vector *vertex = 0); void set_attrib(unsigned int pos, const LibMatrix::vec4 &v, std::vector *vertex = 0); void next_vertex(); std::vector >& vertices(); enum VBOUpdateMethod { VBOUpdateMethodMap, VBOUpdateMethodSubData, }; enum VBOUsage { VBOUsageStatic, VBOUsageStream, VBOUsageDynamic, }; void vbo_update_method(VBOUpdateMethod method); void vbo_usage(VBOUsage usage); void interleave(bool interleave); void reset(); void build_array(); void build_vbo(); void update_array(const std::vector >& ranges); void update_vbo(const std::vector >& ranges); void delete_array(); void delete_vbo(); void render_array(); void render_vbo(); typedef void (*grid_configuration_func)(Mesh &mesh, int x, int y, int n_x, int n_y, LibMatrix::vec3 &ul, LibMatrix::vec3 &ll, LibMatrix::vec3 &ur, LibMatrix::vec3 &lr); void make_grid(int n_x, int n_y, double width, double height, double spacing, grid_configuration_func conf_func = 0); private: bool check_attrib(unsigned int pos, int dim); std::vector &ensure_vertex(); void update_single_array(const std::vector >& ranges, size_t n, size_t nfloats, size_t offset); void update_single_vbo(const std::vector >& ranges, size_t n, size_t nfloats); // // vertex_format_ is a vector of pairs describing the attribute data. // pair.first is the dimension of the attribute (vec2, vec3, vec4) and // pair.second is the number of float values into the vertex data of that // attribute. So, for example, if there are 3 attributes, a vec3 position // a vec3 normal and a vec2 texture coordinate, you would have a // vertex_format_ with 3 elements as follows: {<3, 0>, <3, 3>, <2, 6>} and // the total size of each vertex would be the sum of the dimensions times // the size of a float, or: 8 * sizeof(float) == 32 bytes // std::vector > vertex_format_; std::vector attrib_locations_; int vertex_size_; std::vector > vertices_; std::vector vertex_arrays_; std::vector vbos_; std::vector attrib_data_ptr_; int vertex_stride_; bool interleave_; VBOUpdateMethod vbo_update_method_; VBOUsage vbo_usage_; }; #endif glmark2-2012.08/./src/image-reader.h0000664000175000017500000000355712013417376016212 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include class ImageReader { public: virtual bool error() = 0; virtual bool nextRow(unsigned char *dst) = 0; virtual unsigned int width() const = 0; virtual unsigned int height() const = 0; virtual unsigned int pixelBytes() const = 0; virtual ~ImageReader() {} }; class PNGReaderPrivate; class PNGReader : public ImageReader { public: PNGReader(const std::string& filename); virtual ~PNGReader(); bool error(); bool nextRow(unsigned char *dst); unsigned int width() const; unsigned int height() const; unsigned int pixelBytes() const; private: bool init(const std::string& filename); void finish(); PNGReaderPrivate *priv_; }; class JPEGReaderPrivate; class JPEGReader : public ImageReader { public: JPEGReader(const std::string& filename); virtual ~JPEGReader(); bool error(); bool nextRow(unsigned char *dst); unsigned int width() const; unsigned int height() const; unsigned int pixelBytes() const; private: bool init(const std::string& filename); void finish(); JPEGReaderPrivate *priv_; }; glmark2-2012.08/./src/scene-default-options.cpp0000664000175000017500000000410012013417376020414 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "scene.h" #include "benchmark.h" #include "log.h" bool SceneDefaultOptions::setup() { const std::map &scenes = Benchmark::scenes(); for (std::list >::const_iterator iter = defaultOptions_.begin(); iter != defaultOptions_.end(); iter++) { for (std::map::const_iterator scene_iter = scenes.begin(); scene_iter != scenes.end(); scene_iter++) { Scene &scene(*(scene_iter->second)); /* * Display warning only if the option value is unsupported, not if * the scene doesn't support the option at all. */ if (!scene.set_option_default(iter->first, iter->second) && scene.options().find(iter->first) != scene.options().end()) { Log::info("Warning: Scene '%s' doesn't accept default value '%s' for option '%s'\n", scene.name().c_str(), iter->second.c_str(), iter->first.c_str()); } } } return true; } bool SceneDefaultOptions::set_option(const std::string &opt, const std::string &val) { defaultOptions_.push_back(std::pair(opt, val)); return true; } glmark2-2012.08/./src/scene-effect-2d.cpp0000664000175000017500000003033512013417376017047 0ustar alfalf00000000000000/* * Copyright © 2010-2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include #include #include #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "program.h" #include "shader-source.h" #include "util.h" #include "texture.h" SceneEffect2D::SceneEffect2D(Canvas &pCanvas) : Scene(pCanvas, "effect2d") { options_["kernel"] = Scene::Option("kernel", "0,0,0;0,1,0;0,0,0", "The convolution kernel matrix to use [format: \"a,b,c...;d,e,f...\"");; options_["normalize"] = Scene::Option("normalize", "true", "Whether to normalize the supplied convolution kernel matrix", "false,true"); } SceneEffect2D::~SceneEffect2D() { } /* * Calculates the offset of the coefficient with index i * from the center of the kernel matrix. Note that we are * using the standard OpenGL texture coordinate system * (x grows rightwards, y grows upwards). */ static LibMatrix::vec2 calc_offset(unsigned int i, unsigned int width, unsigned int height) { int x = i % width - (width - 1) / 2; int y = -(i / width - (height - 1) / 2); return LibMatrix::vec2(static_cast(x), static_cast(y)); } /** * Creates a fragment shader implementing 2D image convolution. * * In the mathematical definition of 2D convolution, the kernel/filter (2D * impulse response) is essentially mirrored in both directions (that is, * rotated 180 degrees) when being applied on a 2D block of data (eg pixels). * * Most image manipulation programs, however, use the term kernel/filter to * describe a 180 degree rotation of the 2D impulse response. This is more * intuitive from a human understanding perspective because this rotated matrix * can be regarded as a stencil that can be directly applied by just "placing" * it on the image. * * In order to be compatible with image manipulation programs, we will * use the same definition of kernel/filter (180 degree rotation of impulse * response). This also means that we don't need to perform the (implicit) * rotation of the kernel in our convolution implementation. * * @param canvas the destination Canvas for this shader * @param array the array holding the filter coefficients in row-major * order * @param width the width of the filter * @param width the height of the filter * * @return a string containing the frament source code */ static std::string create_convolution_fragment_shader(Canvas &canvas, std::vector &array, unsigned int width, unsigned int height) { static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/effect-2d-convolution.frag"); ShaderSource source(frg_shader_filename); if (width * height != array.size()) { Log::error("Convolution filter size doesn't match supplied dimensions\n"); return ""; } /* Steps are needed to be able to access nearby pixels */ source.add_const("TextureStepX", 1.0f/canvas.width()); source.add_const("TextureStepY", 1.0f/canvas.height()); std::stringstream ss_def; std::stringstream ss_convolution; /* Set up stringstream floating point options */ ss_def << std::fixed; ss_convolution.precision(1); ss_convolution << std::fixed; ss_convolution << "result = "; for(std::vector::const_iterator iter = array.begin(); iter != array.end(); iter++) { unsigned int i = iter - array.begin(); /* Add Filter coefficient const definitions */ ss_def << "const float Kernel" << i << " = " << *iter << ";" << std::endl; /* Add convolution term using the current filter coefficient */ LibMatrix::vec2 offset(calc_offset(i, width, height)); ss_convolution << "texture2D(Texture0, TextureCoord + vec2(" << offset.x() << " * TextureStepX, " << offset.y() << " * TextureStepY)) * Kernel" << i; if (iter + 1 != array.end()) ss_convolution << " +" << std::endl; } ss_convolution << ";" << std::endl; source.add(ss_def.str()); source.replace("$CONVOLUTION$", ss_convolution.str()); return source.str(); } /** * Creates a string containing a printout of a kernel matrix. * * @param filter the vector containing the filter coefficients * @param width the width of the filter * * @return the printout */ static std::string kernel_printout(const std::vector &kernel, unsigned int width) { std::stringstream ss; ss << std::fixed; for (std::vector::const_iterator iter = kernel.begin(); iter != kernel.end(); iter++) { ss << *iter << " "; if ((iter - kernel.begin()) % width == width - 1) ss << std::endl; } return ss.str(); } /** * Parses a string representation of a matrix and returns it * in row-major format. * * In the string representation, elements are delimited using * commas (',') and rows are delimited using semi-colons (';'). * eg 0,0,0;0,1.0,0;0,0,0 * * @param str the matrix string representation to parse * @param matrix the float vector to populate * @param[out] width the width of the matrix * @param[out] height the height of the matrix * * @return whether parsing succeeded */ static bool parse_matrix(const std::string &str, std::vector &matrix, unsigned int &width, unsigned int &height) { std::vector rows; unsigned int w = UINT_MAX; Util::split(str, ';', rows, Util::SplitModeNormal); Log::debug("Parsing kernel matrix:\n"); static const std::string format("%f "); static const std::string format_cont(Log::continuation_prefix + format); static const std::string newline(Log::continuation_prefix + "\n"); for (std::vector::const_iterator iter = rows.begin(); iter != rows.end(); iter++) { std::vector elems; Util::split(*iter, ',', elems, Util::SplitModeNormal); if (w != UINT_MAX && elems.size() != w) { Log::error("Matrix row %u contains %u elements, whereas previous" " rows had %u\n", iter - rows.begin(), elems.size(), w); return false; } w = elems.size(); for (std::vector::const_iterator iter_el = elems.begin(); iter_el != elems.end(); iter_el++) { float f(Util::fromString(*iter_el)); matrix.push_back(f); if (iter_el == elems.begin()) Log::debug(format.c_str(), f); else Log::debug(format_cont.c_str(), f); } Log::debug(newline.c_str()); } width = w; height = rows.size(); return true; } /** * Normalizes a convolution kernel matrix. * * @param filter the filter to normalize */ static void normalize(std::vector &kernel) { float sum = std::accumulate(kernel.begin(), kernel.end(), 0.0); /* * If sum is essentially zero, perform a zero-sum normalization. * This normalizes positive and negative values separately, */ if (fabs(sum) < 0.00000001) { sum = 0.0; for (std::vector::iterator iter = kernel.begin(); iter != kernel.end(); iter++) { if (*iter > 0.0) sum += *iter; } } /* * We can simply compare with 0.0f here, because we just care about * avoiding division-by-zero. */ if (sum == 0.0) return; for (std::vector::iterator iter = kernel.begin(); iter != kernel.end(); iter++) { *iter /= sum; } } bool SceneEffect2D::load() { Texture::load("effect-2d", &texture_, GL_NEAREST, GL_NEAREST, 0); running_ = false; return true; } void SceneEffect2D::unload() { glDeleteTextures(1, &texture_); } bool SceneEffect2D::setup() { if (!Scene::setup()) return false; Texture::find_textures(); static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/effect-2d.vert"); std::vector kernel; unsigned int kernel_width = 0; unsigned int kernel_height = 0; /* Parse the kernel matrix from the options */ if (!parse_matrix(options_["kernel"].value, kernel, kernel_width, kernel_height)) { return false; } /* Normalize the kernel matrix if needed */ if (options_["normalize"].value == "true") { normalize(kernel); Log::debug("Normalized kernel matrix:\n%s", kernel_printout(kernel, kernel_width).c_str()); } /* Create and load the shaders */ ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source; frg_source.append(create_convolution_fragment_shader(canvas_, kernel, kernel_width, kernel_height)); if (frg_source.str().empty()) return false; if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } std::vector vertex_format; vertex_format.push_back(3); mesh_.set_vertex_format(vertex_format); mesh_.make_grid(1, 1, 2.0, 2.0, 0.0); mesh_.build_vbo(); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); program_.start(); // Load texture sampler value program_["Texture0"] = 0; currentFrame_ = 0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneEffect2D::teardown() { mesh_.reset(); program_.stop(); program_.release(); Scene::teardown(); } void SceneEffect2D::update() { Scene::update(); } void SceneEffect2D::draw() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); mesh_.render_vbo(); } Scene::ValidationResult SceneEffect2D::validate() { static const double radius_3d(std::sqrt(3.0)); std::vector kernel; std::vector kernel_edge; std::vector kernel_blur; unsigned int kernel_width = 0; unsigned int kernel_height = 0; if (!parse_matrix("0,1,0;1,-4,1;0,1,0;", kernel_edge, kernel_width, kernel_height)) { return Scene::ValidationUnknown; } if (!parse_matrix("1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;", kernel_blur, kernel_width, kernel_height)) { return Scene::ValidationUnknown; } if (!parse_matrix(options_["kernel"].value, kernel, kernel_width, kernel_height)) { return Scene::ValidationUnknown; } Canvas::Pixel ref; if (kernel == kernel_edge) ref = Canvas::Pixel(0x17, 0x0c, 0x2f, 0xff); else if (kernel == kernel_blur) ref = Canvas::Pixel(0xc7, 0xe1, 0x8d, 0xff); else return Scene::ValidationUnknown; Canvas::Pixel pixel = canvas_.read_pixel(452, 237); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/scene-conditionals.cpp0000664000175000017500000001155112013417376017775 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "shader-source.h" #include "util.h" #include static const std::string shader_file_base(GLMARK_DATA_PATH"/shaders/conditionals"); static const std::string vtx_file(shader_file_base + ".vert"); static const std::string frg_file(shader_file_base + ".frag"); static const std::string step_conditional_file(shader_file_base + "-step-conditional.all"); static const std::string step_simple_file(shader_file_base + "-step-simple.all"); SceneConditionals::SceneConditionals(Canvas &pCanvas) : SceneGrid(pCanvas, "conditionals") { options_["fragment-steps"] = Scene::Option("fragment-steps", "1", "The number of computational steps in the fragment shader"); options_["fragment-conditionals"] = Scene::Option("fragment-conditionals", "true", "Whether each computational step includes an if-else clause", "false,true"); options_["vertex-steps"] = Scene::Option("vertex-steps", "1", "The number of computational steps in the vertex shader"); options_["vertex-conditionals"] = Scene::Option("vertex-conditionals", "true", "Whether each computational step includes an if-else clause", "false,true"); } SceneConditionals::~SceneConditionals() { } static std::string get_vertex_shader_source(int steps, bool conditionals) { ShaderSource source(vtx_file); ShaderSource source_main; for (int i = 0; i < steps; i++) { if (conditionals) source_main.append_file(step_conditional_file); else source_main.append_file(step_simple_file); } source.replace("$MAIN$", source_main.str()); return source.str(); } static std::string get_fragment_shader_source(int steps, bool conditionals) { ShaderSource source(frg_file); ShaderSource source_main; for (int i = 0; i < steps; i++) { if (conditionals) source_main.append_file(step_conditional_file); else source_main.append_file(step_simple_file); } source.replace("$MAIN$", source_main.str()); return source.str(); } bool SceneConditionals::setup() { if (!SceneGrid::setup()) return false; /* Parse options */ bool vtx_conditionals = options_["vertex-conditionals"].value == "true"; bool frg_conditionals = options_["fragment-conditionals"].value == "true"; int vtx_steps(Util::fromString(options_["vertex-steps"].value)); int frg_steps(Util::fromString(options_["fragment-steps"].value)); /* Load shaders */ std::string vtx_shader(get_vertex_shader_source(vtx_steps, vtx_conditionals)); std::string frg_shader(get_fragment_shader_source(frg_steps, frg_conditionals)); if (!Scene::load_shaders_from_strings(program_, vtx_shader, frg_shader)) return false; program_.start(); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } Scene::ValidationResult SceneConditionals::validate() { static const double radius_3d(std::sqrt(3.0 * 5.0 * 5.0)); bool frg_conditionals = options_["fragment-conditionals"].value == "true"; int frg_steps(Util::fromString(options_["fragment-steps"].value)); if (!frg_conditionals) return Scene::ValidationUnknown; Canvas::Pixel ref; if (frg_steps == 0) ref = Canvas::Pixel(0xa0, 0xa0, 0xa0, 0xff); else if (frg_steps == 5) ref = Canvas::Pixel(0x25, 0x25, 0x25, 0xff); else return Scene::ValidationUnknown; Canvas::Pixel pixel = canvas_.read_pixel(293, 89); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/mesh.cpp0000664000175000017500000004071012013417376015147 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #include "mesh.h" #include "log.h" #include "gl-headers.h" Mesh::Mesh() : vertex_size_(0), interleave_(false), vbo_update_method_(VBOUpdateMethodMap), vbo_usage_(VBOUsageStatic) { } Mesh::~Mesh() { reset(); } /* * Sets the vertex format for this mesh. * * The format consists of a vector of integers, each * specifying the size in floats of each vertex attribute. * * e.g. {4, 3, 2} => 3 attributes vec4, vec3, vec2 */ void Mesh::set_vertex_format(const std::vector &format) { int pos = 0; vertex_format_.clear(); for (std::vector::const_iterator iter = format.begin(); iter != format.end(); iter++) { int n = *iter; vertex_format_.push_back(std::pair(n, pos)); pos += n; } vertex_size_ = pos; } /* * Sets the attribute locations. * * These are the locations used in glEnableVertexAttribArray() * and other related functions. */ void Mesh::set_attrib_locations(const std::vector &locations) { if (locations.size() != vertex_format_.size()) Log::error("Trying to set attribute locations using wrong size\n"); attrib_locations_ = locations; } /** * Checks that an attribute is of the correct dimensionality. * * @param pos the position/index of the attribute to check * @param dim the size of the attribute (in #floats) * * @return whether the check succeeded */ bool Mesh::check_attrib(unsigned int pos, int dim) { if (pos > vertex_format_.size()) { Log::error("Trying to set non-existent attribute\n"); return false; } if (vertex_format_[pos].first != dim) { Log::error("Trying to set attribute with value of invalid type\n"); return false; } return true; } /** * Ensures that we have a vertex to process. * * @return the vertex to process */ std::vector & Mesh::ensure_vertex() { if (vertices_.empty()) next_vertex(); return vertices_.back(); } /* * Sets the value of an attribute in the current vertex. * * The pos parameter refers to the position of the attribute * as specified indirectly when setting the format using * set_vertex_format(). e.g. 0 = first attribute, 1 = second * etc */ void Mesh::set_attrib(unsigned int pos, const LibMatrix::vec2 &v, std::vector *vertex) { if (!check_attrib(pos, 2)) return; std::vector &vtx = !vertex ? ensure_vertex() : *vertex; int offset = vertex_format_[pos].second; vtx[offset] = v.x(); vtx[offset + 1] = v.y(); } void Mesh::set_attrib(unsigned int pos, const LibMatrix::vec3 &v, std::vector *vertex) { if (!check_attrib(pos, 3)) return; std::vector &vtx = !vertex ? ensure_vertex() : *vertex; int offset = vertex_format_[pos].second; vtx[offset] = v.x(); vtx[offset + 1] = v.y(); vtx[offset + 2] = v.z(); } void Mesh::set_attrib(unsigned int pos, const LibMatrix::vec4 &v, std::vector *vertex) { if (!check_attrib(pos, 4)) return; std::vector &vtx = !vertex ? ensure_vertex() : *vertex; int offset = vertex_format_[pos].second; vtx[offset] = v.x(); vtx[offset + 1] = v.y(); vtx[offset + 2] = v.z(); vtx[offset + 3] = v.w(); } /* * Adds a new vertex to the list and makes it current. */ void Mesh::next_vertex() { vertices_.push_back(std::vector(vertex_size_)); } /** * Gets the mesh vertices. * * You should use the ::set_attrib() method to manipulate * the vertex data. * * You shouldn't resize the vector (change the number of vertices) * manually. Use ::next_vertex() instead. */ std::vector >& Mesh::vertices() { return vertices_; } /** * Sets the VBO update method. * * The default value is VBOUpdateMethodMap. */ void Mesh::vbo_update_method(Mesh::VBOUpdateMethod method) { vbo_update_method_ = method; } /** * Sets the VBO usage hint. * * The usage hint takes effect in the next call to ::build_vbo(). * * The default value is VBOUsageStatic. */ void Mesh::vbo_usage(Mesh::VBOUsage usage) { vbo_usage_ = usage; } /** * Sets the vertex attribute interleaving mode. * * If true the vertex attributes are going to be interleaved in a single * buffer. Otherwise they will be separated into different buffers (one * per attribute). * * Interleaving mode takes effect in the next call to ::build_array() or * ::build_vbo(). * * @param interleave whether to interleave */ void Mesh::interleave(bool interleave) { interleave_ = interleave; } /** * Resets a Mesh object to its initial, empty state. */ void Mesh::reset() { delete_array(); delete_vbo(); vertices_.clear(); vertex_format_.clear(); attrib_locations_.clear(); attrib_data_ptr_.clear(); vertex_size_ = 0; vertex_stride_ = 0; } /** * Builds a vertex array containing the mesh vertex data. * * The way the vertex array is constructed is affected by the current * interleave value, which can set using ::interleave(). */ void Mesh::build_array() { int nvertices = vertices_.size(); if (!interleave_) { /* Create an array for each attribute */ for (std::vector >::const_iterator ai = vertex_format_.begin(); ai != vertex_format_.end(); ai++) { float *array = new float[nvertices * ai->first]; float *cur = array; /* Fill in the array */ for (std::vector >::const_iterator vi = vertices_.begin(); vi != vertices_.end(); vi++) { for (int i = 0; i < ai->first; i++) *cur++ = (*vi)[ai->second + i]; } vertex_arrays_.push_back(array); attrib_data_ptr_.push_back(array); } vertex_stride_ = 0; } else { float *array = new float[nvertices * vertex_size_]; float *cur = array; for (std::vector >::const_iterator vi = vertices_.begin(); vi != vertices_.end(); vi++) { /* Fill in the array */ for (int i = 0; i < vertex_size_; i++) *cur++ = (*vi)[i]; } for (size_t i = 0; i < vertex_format_.size(); i++) attrib_data_ptr_.push_back(array + vertex_format_[i].second); vertex_arrays_.push_back(array); vertex_stride_ = vertex_size_ * sizeof(float); } } /** * Builds a vertex buffer object containing the mesh vertex data. * * The way the VBO is constructed is affected by the current interleave * value (::interleave()) and the vbo usage hint (::vbo_usage()). */ void Mesh::build_vbo() { delete_array(); build_array(); int nvertices = vertices_.size(); attrib_data_ptr_.clear(); GLenum buffer_usage; if (vbo_usage_ == Mesh::VBOUsageStream) buffer_usage = GL_STREAM_DRAW; else if (vbo_usage_ == Mesh::VBOUsageDynamic) buffer_usage = GL_DYNAMIC_DRAW; else /* if (vbo_usage_ == Mesh::VBOUsageStatic) */ buffer_usage = GL_STATIC_DRAW; if (!interleave_) { /* Create a vbo for each attribute */ for (std::vector >::const_iterator ai = vertex_format_.begin(); ai != vertex_format_.end(); ai++) { float *data = vertex_arrays_[ai - vertex_format_.begin()]; GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, nvertices * ai->first * sizeof(float), data, buffer_usage); vbos_.push_back(vbo); attrib_data_ptr_.push_back(0); } vertex_stride_ = 0; } else { GLuint vbo; /* Create a single vbo to store all attribute data */ glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, nvertices * vertex_size_ * sizeof(float), vertex_arrays_[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); for (size_t i = 0; i < vertex_format_.size(); i++) { attrib_data_ptr_.push_back(reinterpret_cast(sizeof(float) * vertex_format_[i].second)); vbos_.push_back(vbo); } vertex_stride_ = vertex_size_ * sizeof(float); } delete_array(); } /** * Updates ranges of a single vertex array. * * @param ranges the ranges of vertices to update * @param n the index of the vertex array to update * @param nfloats how many floats to update for each vertex * @param offset the offset (in floats) in the vertex data to start reading from */ void Mesh::update_single_array(const std::vector >& ranges, size_t n, size_t nfloats, size_t offset) { float *array(vertex_arrays_[n]); /* Update supplied ranges */ for (std::vector >::const_iterator ri = ranges.begin(); ri != ranges.end(); ri++) { /* Update the current range from the vertex data */ float *dest(array + nfloats * ri->first); for (size_t n = ri->first; n <= ri->second; n++) { for (size_t i = 0; i < nfloats; i++) *dest++ = vertices_[n][offset + i]; } } } /** * Updates ranges of the vertex arrays. * * @param ranges the ranges of vertices to update */ void Mesh::update_array(const std::vector >& ranges) { /* If we don't have arrays to update, create them */ if (vertex_arrays_.empty()) { build_array(); return; } if (!interleave_) { for (size_t i = 0; i < vertex_arrays_.size(); i++) { update_single_array(ranges, i, vertex_format_[i].first, vertex_format_[i].second); } } else { update_single_array(ranges, 0, vertex_size_, 0); } } /** * Updates ranges of a single VBO. * * This method use either glMapBuffer or glBufferSubData to perform * the update. The used method can be set with ::vbo_update_method(). * * @param ranges the ranges of vertices to update * @param n the index of the vbo to update * @param nfloats how many floats to update for each vertex */ void Mesh::update_single_vbo(const std::vector >& ranges, size_t n, size_t nfloats) { float *src_start(vertex_arrays_[n]); float *dest_start(0); glBindBuffer(GL_ARRAY_BUFFER, vbos_[n]); if (vbo_update_method_ == VBOUpdateMethodMap) { dest_start = reinterpret_cast( GLExtensions::MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) ); } /* Update supplied ranges */ for (std::vector >::const_iterator iter = ranges.begin(); iter != ranges.end(); iter++) { float *src(src_start + nfloats * iter->first); float *src_end(src_start + nfloats * (iter->second + 1)); if (vbo_update_method_ == VBOUpdateMethodMap) { float *dest(dest_start + nfloats * iter->first); std::copy(src, src_end, dest); } else if (vbo_update_method_ == VBOUpdateMethodSubData) { glBufferSubData(GL_ARRAY_BUFFER, nfloats * iter->first * sizeof(float), (src_end - src) * sizeof(float), src); } } if (vbo_update_method_ == VBOUpdateMethodMap) GLExtensions::UnmapBuffer(GL_ARRAY_BUFFER); } /** * Updates ranges of the VBOs. * * @param ranges the ranges of vertices to update */ void Mesh::update_vbo(const std::vector >& ranges) { /* If we don't have VBOs to update, create them */ if (vbos_.empty()) { build_vbo(); return; } update_array(ranges); if (!interleave_) { for (size_t i = 0; i < vbos_.size(); i++) update_single_vbo(ranges, i, vertex_format_[i].first); } else { update_single_vbo(ranges, 0, vertex_size_); } glBindBuffer(GL_ARRAY_BUFFER, 0); } /** * Deletes all resources associated with built vertex arrays. */ void Mesh::delete_array() { for (size_t i = 0; i < vertex_arrays_.size(); i++) { delete [] vertex_arrays_[i]; } vertex_arrays_.clear(); } /** * Deletes all resources associated with built VBOs. */ void Mesh::delete_vbo() { for (size_t i = 0; i < vbos_.size(); i++) { GLuint vbo = vbos_[i]; glDeleteBuffers(1, &vbo); } vbos_.clear(); } /** * Renders a mesh using vertex arrays. * * The vertex arrays must have been previously initialized using * ::build_array(). */ void Mesh::render_array() { for (size_t i = 0; i < vertex_format_.size(); i++) { glEnableVertexAttribArray(attrib_locations_[i]); glVertexAttribPointer(attrib_locations_[i], vertex_format_[i].first, GL_FLOAT, GL_FALSE, vertex_stride_, attrib_data_ptr_[i]); } glDrawArrays(GL_TRIANGLES, 0, vertices_.size()); for (size_t i = 0; i < vertex_format_.size(); i++) { glDisableVertexAttribArray(attrib_locations_[i]); } } /** * Renders a mesh using vertex buffer objects. * * The vertex buffer objects must have been previously initialized using * ::build_vbo(). */ void Mesh::render_vbo() { for (size_t i = 0; i < vertex_format_.size(); i++) { glEnableVertexAttribArray(attrib_locations_[i]); glBindBuffer(GL_ARRAY_BUFFER, vbos_[i]); glVertexAttribPointer(attrib_locations_[i], vertex_format_[i].first, GL_FLOAT, GL_FALSE, vertex_stride_, attrib_data_ptr_[i]); } glDrawArrays(GL_TRIANGLES, 0, vertices_.size()); for (size_t i = 0; i < vertex_format_.size(); i++) { glDisableVertexAttribArray(attrib_locations_[i]); } } /** * Creates a grid mesh. * * @param n_x the number of grid cells on the X axis * @param n_y the number of grid cells on the Y axis * @param width the width X of the grid (normalized) * @param height the height Y of the grid (normalized) * @param spacing the spacing between cells (normalized) * @param conf_func a function to call to configure the grid (or NULL) */ void Mesh::make_grid(int n_x, int n_y, double width, double height, double spacing, grid_configuration_func conf_func) { double side_width = (width - (n_x - 1) * spacing) / n_x; double side_height = (height - (n_y - 1) * spacing) / n_y; for (int i = 0; i < n_x; i++) { for (int j = 0; j < n_y; j++) { LibMatrix::vec3 a(-width / 2 + i * (side_width + spacing), height / 2 - j * (side_height + spacing), 0); LibMatrix::vec3 b(a.x(), a.y() - side_height, 0); LibMatrix::vec3 c(a.x() + side_width, a.y(), 0); LibMatrix::vec3 d(a.x() + side_width, a.y() - side_height, 0); if (!conf_func) { std::vector ul(vertex_size_); std::vector ur(vertex_size_); std::vector ll(vertex_size_); std::vector lr(vertex_size_); set_attrib(0, a, &ul); set_attrib(0, c, &ur); set_attrib(0, b, &ll); set_attrib(0, d, &lr); next_vertex(); vertices_.back() = ul; next_vertex(); vertices_.back() = ll; next_vertex(); vertices_.back() = ur; next_vertex(); vertices_.back() = ll; next_vertex(); vertices_.back() = lr; next_vertex(); vertices_.back() = ur; } else { conf_func(*this, i, j, n_x, n_y, a, b, c, d); } } } } glmark2-2012.08/./src/canvas-android.h0000664000175000017500000000302412013417376016546 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_CANVAS_ANDROID_H_ #define GLMARK2_CANVAS_ANDROID_H_ #include "canvas.h" /** * Canvas for rendering to Android surfaces. * * This class doesn't perform any GLES2.0 surface and context management * (contrary to the CanvasX11* classes); these are handled in the Java * Android code. */ class CanvasAndroid : public Canvas { public: CanvasAndroid(int width, int height) : Canvas(width, height) {} ~CanvasAndroid() {} bool init(); void visible(bool visible); void clear(); void update(); void print_info(); Pixel read_pixel(int x, int y); void write_to_file(std::string &filename); bool should_quit(); void resize(int width, int height); private: void init_gl_extensions(); }; #endif glmark2-2012.08/./src/main.cpp0000664000175000017500000001472112013417376015142 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include "gl-headers.h" #include "scene.h" #include "benchmark.h" #include "options.h" #include "log.h" #include "util.h" #include "text-renderer.h" #include "main-loop.h" #include "benchmark-collection.h" #include #include #if USE_GL #include "canvas-x11-glx.h" #elif USE_GLESv2 #include "canvas-x11-egl.h" #endif using std::vector; using std::map; using std::string; void add_and_register_scenes(vector& scenes, Canvas& canvas) { scenes.push_back(new SceneDefaultOptions(canvas)); scenes.push_back(new SceneBuild(canvas)); scenes.push_back(new SceneTexture(canvas)); scenes.push_back(new SceneShading(canvas)); scenes.push_back(new SceneConditionals(canvas)); scenes.push_back(new SceneFunction(canvas)); scenes.push_back(new SceneLoop(canvas)); scenes.push_back(new SceneBump(canvas)); scenes.push_back(new SceneEffect2D(canvas)); scenes.push_back(new ScenePulsar(canvas)); scenes.push_back(new SceneDesktop(canvas)); scenes.push_back(new SceneBuffer(canvas)); scenes.push_back(new SceneIdeas(canvas)); scenes.push_back(new SceneTerrain(canvas)); scenes.push_back(new SceneJellyfish(canvas)); for (vector::const_iterator iter = scenes.begin(); iter != scenes.end(); iter++) { Benchmark::register_scene(**iter); } } static void list_scenes() { const map &scenes = Benchmark::scenes(); for (map::const_iterator scene_iter = scenes.begin(); scene_iter != scenes.end(); scene_iter++) { Scene *scene = scene_iter->second; if (scene->name().empty()) continue; Log::info("[Scene] %s\n", scene->name().c_str()); const map &options = scene->options(); for (map::const_iterator opt_iter = options.begin(); opt_iter != options.end(); opt_iter++) { const Scene::Option &opt = opt_iter->second; Log::info(" [Option] %s\n" " Description : %s\n" " Default Value: %s\n", opt.name.c_str(), opt.description.c_str(), opt.default_value.c_str()); /* Display list of acceptable values (if defined) */ if (!opt.acceptable_values.empty()) { Log::info(" Acceptable Values: "); for (vector::const_iterator val_iter = opt.acceptable_values.begin(); val_iter != opt.acceptable_values.end(); val_iter++) { std::string format_value(Log::continuation_prefix + "%s"); if (val_iter + 1 != opt.acceptable_values.end()) format_value += ","; else format_value += "\n"; Log::info(format_value.c_str(), val_iter->c_str()); } } } } } void do_benchmark(Canvas &canvas) { BenchmarkCollection benchmark_collection; MainLoop *loop; benchmark_collection.populate_from_options(); if (benchmark_collection.needs_decoration()) loop = new MainLoopDecoration(canvas, benchmark_collection.benchmarks()); else loop = new MainLoop(canvas, benchmark_collection.benchmarks()); while (loop->step()); Log::info("=======================================================\n"); Log::info(" glmark2 Score: %u \n", loop->score()); Log::info("=======================================================\n"); delete loop; } void do_validation(Canvas &canvas) { BenchmarkCollection benchmark_collection; benchmark_collection.populate_from_options(); MainLoopValidation loop(canvas, benchmark_collection.benchmarks()); while (loop.step()); } int main(int argc, char *argv[]) { if (!Options::parse_args(argc, argv)) return 1; /* Initialize Log class */ Log::init(Util::appname_from_path(argv[0]), Options::show_debug); if (Options::show_help) { Options::print_help(); return 0; } /* Force 800x600 output for validation */ if (Options::validate && Options::size != std::pair(800, 600)) { Log::info("Ignoring custom size %dx%d for validation. Using 800x600.\n", Options::size.first, Options::size.second); Options::size = std::pair(800, 600); } // Create the canvas #if USE_GL CanvasX11GLX canvas(Options::size.first, Options::size.second); #elif USE_GLESv2 CanvasX11EGL canvas(Options::size.first, Options::size.second); #endif canvas.offscreen(Options::offscreen); canvas.visual_config(Options::visual_config); vector scenes; // Register the scenes, so they can be looked up by name add_and_register_scenes(scenes, canvas); if (Options::list_scenes) { list_scenes(); return 0; } if (!canvas.init()) { Log::error("%s: Could not initialize canvas\n", __FUNCTION__); return 1; } Log::info("=======================================================\n"); Log::info(" glmark2 %s\n", GLMARK_VERSION); Log::info("=======================================================\n"); canvas.print_info(); Log::info("=======================================================\n"); canvas.visible(true); if (Options::validate) do_validation(canvas); else do_benchmark(canvas); Util::dispose_pointer_vector(scenes); return 0; } glmark2-2012.08/./src/canvas-android.cpp0000664000175000017500000001236512013417376017111 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker */ #include "canvas-android.h" #include "log.h" #include "options.h" #include "gl-headers.h" #include #include #include /****************** * Public methods * ******************/ bool CanvasAndroid::init() { EGLint attribs[] = { EGL_CONFIG_ID, 0, EGL_NONE }; /* Get the current EGL config */ EGLDisplay egl_display(eglGetCurrentDisplay()); EGLContext egl_context(eglGetCurrentContext()); EGLConfig egl_config(0); EGLint num_configs; eglQueryContext(egl_display, egl_context, EGL_CONFIG_ID, &(attribs[1])); eglChooseConfig(egl_display, attribs, &egl_config, 1, &num_configs); resize(width_, height_); if (!eglSwapInterval(egl_display, 0)) Log::info("** Failed to set swap interval. Results may be bounded above by refresh rate.\n"); init_gl_extensions(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); clear(); if (Options::show_debug) { int buf, red, green, blue, alpha, depth, id, native_id; eglGetConfigAttrib(egl_display, egl_config, EGL_CONFIG_ID, &id); eglGetConfigAttrib(egl_display, egl_config, EGL_NATIVE_VISUAL_ID, &native_id); eglGetConfigAttrib(egl_display, egl_config, EGL_BUFFER_SIZE, &buf); eglGetConfigAttrib(egl_display, egl_config, EGL_RED_SIZE, &red); eglGetConfigAttrib(egl_display, egl_config, EGL_GREEN_SIZE, &green); eglGetConfigAttrib(egl_display, egl_config, EGL_BLUE_SIZE, &blue); eglGetConfigAttrib(egl_display, egl_config, EGL_ALPHA_SIZE, &alpha); eglGetConfigAttrib(egl_display, egl_config, EGL_DEPTH_SIZE, &depth); Log::debug("EGL chosen config ID: 0x%x Native Visual ID: 0x%x\n" " Buffer: %d bits\n" " Red: %d bits\n" " Green: %d bits\n" " Blue: %d bits\n" " Alpha: %d bits\n" " Depth: %d bits\n", id, native_id, buf, red, green, blue, alpha, depth); } return true; } void CanvasAndroid::visible(bool visible) { static_cast(visible); } void CanvasAndroid::clear() { glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepthf(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void CanvasAndroid::update() { } void CanvasAndroid::print_info() { std::stringstream ss; ss << " OpenGL Information" << std::endl; ss << " GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl; ss << " GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl; ss << " GL_VERSION: " << glGetString(GL_VERSION) << std::endl; Log::info("%s", ss.str().c_str()); } Canvas::Pixel CanvasAndroid::read_pixel(int x, int y) { uint8_t pixel[4]; glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); return Canvas::Pixel(pixel[0], pixel[1], pixel[2], pixel[3]); } void CanvasAndroid::write_to_file(std::string &filename) { char *pixels = new char[width_ * height_ * 4]; for (int i = 0; i < height_; i++) { glReadPixels(0, i, width_, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[(height_ - i - 1) * width_ * 4]); } std::ofstream output (filename.c_str(), std::ios::out | std::ios::binary); output.write(pixels, 4 * width_ * height_); delete [] pixels; } bool CanvasAndroid::should_quit() { return false; } void CanvasAndroid::resize(int width, int height) { width_ = width; height_ = height; glViewport(0, 0, width_, height_); projection_ = LibMatrix::Mat4::perspective(60.0, width_ / static_cast(height_), 1.0, 1024.0); } /******************* * Private methods * *******************/ void CanvasAndroid::init_gl_extensions() { /* * Parse the extensions we care about from the extension string. * Don't even bother to get function pointers until we know the * extension is present. */ std::string extString; const char* exts = reinterpret_cast(glGetString(GL_EXTENSIONS)); if (exts) { extString = exts; } if (extString.find("GL_OES_mapbuffer") != std::string::npos) { GLExtensions::MapBuffer = reinterpret_cast(eglGetProcAddress("glMapBufferOES")); GLExtensions::UnmapBuffer = reinterpret_cast(eglGetProcAddress("glUnmapBufferOES")); } } glmark2-2012.08/./src/scene-desktop.cpp0000664000175000017500000007546112013417376016772 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include #include #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "program.h" #include "shader-source.h" #include "util.h" #include "texture.h" enum BlurDirection { BlurDirectionHorizontal, BlurDirectionVertical, BlurDirectionBoth }; static void create_blur_shaders(ShaderSource& vtx_source, ShaderSource& frg_source, unsigned int radius, float sigma, BlurDirection direction) { vtx_source.append_file(GLMARK_DATA_PATH"/shaders/desktop.vert"); frg_source.append_file(GLMARK_DATA_PATH"/shaders/desktop-blur.frag"); /* Don't let the gaussian curve become too narrow */ if (sigma < 1.0) sigma = 1.0; unsigned int side = 2 * radius + 1; for (unsigned int i = 0; i < radius + 1; i++) { float s2 = 2.0 * sigma * sigma; float k = 1.0 / std::sqrt(M_PI * s2) * std::exp( - (static_cast(i) * i) / s2); std::stringstream ss_tmp; ss_tmp << "Kernel" << i; frg_source.add_const(ss_tmp.str(), k); } std::stringstream ss; ss << "result = " << std::endl; if (direction == BlurDirectionHorizontal) { for (unsigned int i = 0; i < side; i++) { int offset = static_cast(i - radius); ss << "texture2D(Texture0, TextureCoord + vec2(" << offset << ".0 * TextureStepX, 0.0)) * Kernel" << std::abs(offset) << " +" << std::endl; } ss << "0.0 ;" << std::endl; } else if (direction == BlurDirectionVertical) { for (unsigned int i = 0; i < side; i++) { int offset = static_cast(i - radius); ss << "texture2D(Texture0, TextureCoord + vec2(0.0, " << offset << ".0 * TextureStepY)) * Kernel" << std::abs(offset) << " +" << std::endl; } ss << "0.0 ;" << std::endl; } else if (direction == BlurDirectionBoth) { for (unsigned int i = 0; i < side; i++) { int ioffset = static_cast(i - radius); for (unsigned int j = 0; j < side; j++) { int joffset = static_cast(j - radius); ss << "texture2D(Texture0, TextureCoord + vec2(" << ioffset << ".0 * TextureStepX, " << joffset << ".0 * TextureStepY))" << " * Kernel" << std::abs(ioffset) << " * Kernel" << std::abs(joffset) << " +" << std::endl; } } ss << " 0.0;" << std::endl; } frg_source.replace("$CONVOLUTION$", ss.str()); } /** * A RenderObject represents a source and target of rendering * operations. */ class RenderObject { public: RenderObject() : texture_(0), fbo_(0), rotation_rad_(0), texture_contents_invalid_(true) { } virtual ~RenderObject() {} virtual void init() { /* Create a texture to draw to */ glGenTextures(1, &texture_); glBindTexture(GL_TEXTURE_2D, texture_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.x(), size_.y(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); /* Create a FBO */ glGenFramebuffers(1, &fbo_); glBindFramebuffer(GL_FRAMEBUFFER, fbo_); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); /* Load the shader program when this class if first used */ if (RenderObject::use_count == 0) { ShaderSource vtx_source(GLMARK_DATA_PATH"/shaders/desktop.vert"); ShaderSource frg_source(GLMARK_DATA_PATH"/shaders/desktop.frag"); Scene::load_shaders_from_strings(main_program, vtx_source.str(), frg_source.str()); } texture_contents_invalid_ = true; RenderObject::use_count++; } virtual void release() { /* Release resources */ if (texture_ != 0) { glDeleteTextures(1, &texture_); texture_ = 0; } if (fbo_ != 0) { glDeleteFramebuffers(1, &fbo_); fbo_ = 0; } /* * Release the shader program when object of this class * are no longer in use. */ RenderObject::use_count--; if (RenderObject::use_count == 0) RenderObject::main_program.release(); } void make_current() { glBindFramebuffer(GL_FRAMEBUFFER, fbo_); glViewport(0, 0, size_.x(), size_.y()); } void position(const LibMatrix::vec2& pos) { pos_ = pos; } const LibMatrix::vec2& position() { return pos_; } virtual void size(const LibMatrix::vec2& size) { /* Recreate the backing texture with correct size */ if (size_.x() != size.x() || size_.y() != size.y()) { size_ = size; glBindTexture(GL_TEXTURE_2D, texture_); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.x(), size_.y(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); texture_contents_invalid_ = true; } if (texture_contents_invalid_) { clear(); texture_contents_invalid_ = false; } } const LibMatrix::vec2& size() { return size_; } const LibMatrix::vec2& speed() { return speed_; } void speed(const LibMatrix::vec2& speed) { speed_ = speed; } GLuint texture() { return texture_; } virtual void clear() { make_current(); glClear(GL_COLOR_BUFFER_BIT); } virtual void render_to(RenderObject& target) { render_to(target, main_program); } virtual void render_to(RenderObject& target, Program& program) { LibMatrix::vec2 anchor(pos_); LibMatrix::vec2 ll(pos_ - anchor); LibMatrix::vec2 ur(pos_ + size_ - anchor); /* Calculate new position according to rotation value */ GLfloat position[2 * 4] = { rotate_x(ll.x(), ll.y()) + anchor.x(), rotate_y(ll.x(), ll.y()) + anchor.y(), rotate_x(ur.x(), ll.y()) + anchor.x(), rotate_y(ur.x(), ll.y()) + anchor.y(), rotate_x(ll.x(), ur.y()) + anchor.x(), rotate_y(ll.x(), ur.y()) + anchor.y(), rotate_x(ur.x(), ur.y()) + anchor.x(), rotate_y(ur.x(), ur.y()) + anchor.y(), }; /* Normalize position and write back to array */ for (int i = 0; i < 4; i++) { const LibMatrix::vec2& v2( target.normalize_position( LibMatrix::vec2(position[2 * i], position[2 * i + 1]) ) ); position[2 * i] = v2.x(); position[2 * i + 1] = v2.y(); } static const GLfloat texcoord[2 * 4] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, }; target.make_current(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); draw_quad_with_program(position, texcoord, program); } virtual void render_from(RenderObject& target, Program& program = main_program) { LibMatrix::vec2 final_pos(pos_ + size_); LibMatrix::vec2 ll_tex(target.normalize_texcoord(pos_)); LibMatrix::vec2 ur_tex(target.normalize_texcoord(final_pos)); static const GLfloat position_blur[2 * 4] = { -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, }; GLfloat texcoord_blur[2 * 4] = { ll_tex.x(), ll_tex.y(), ur_tex.x(), ll_tex.y(), ll_tex.x(), ur_tex.y(), ur_tex.x(), ur_tex.y(), }; make_current(); glBindTexture(GL_TEXTURE_2D, target.texture()); draw_quad_with_program(position_blur, texcoord_blur, program); } /** * Normalizes a position from [0, size] to [-1.0, 1.0] */ LibMatrix::vec2 normalize_position(const LibMatrix::vec2& pos) { return pos * 2.0 / size_ - 1.0; } /** * Normalizes a position from [0, size] to [0.0, 1.0] */ LibMatrix::vec2 normalize_texcoord(const LibMatrix::vec2& pos) { return pos / size_; } void rotation(float degrees) { rotation_rad_ = (M_PI * degrees / 180.0); } protected: void draw_quad_with_program(const GLfloat *position, const GLfloat *texcoord, Program &program) { int pos_index = program["position"].location(); int tex_index = program["texcoord"].location(); program.start(); glEnableVertexAttribArray(pos_index); glEnableVertexAttribArray(tex_index); glVertexAttribPointer(pos_index, 2, GL_FLOAT, GL_FALSE, 0, position); glVertexAttribPointer(tex_index, 2, GL_FLOAT, GL_FALSE, 0, texcoord); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(tex_index); glDisableVertexAttribArray(pos_index); program.stop(); } static Program main_program; LibMatrix::vec2 pos_; LibMatrix::vec2 size_; LibMatrix::vec2 speed_; GLuint texture_; GLuint fbo_; private: float rotate_x(float x, float y) { return x * cos(rotation_rad_) - y * sin(rotation_rad_); } float rotate_y(float x, float y) { return x * sin(rotation_rad_) + y * cos(rotation_rad_); } float rotation_rad_; bool texture_contents_invalid_; static int use_count; }; int RenderObject::use_count = 0; Program RenderObject::main_program; /** * A RenderObject representing the screen. * * Rendering to this objects renders to the screen framebuffer. */ class RenderScreen : public RenderObject { public: RenderScreen(Canvas &canvas) { fbo_ = canvas.fbo(); } virtual void init() {} virtual void release() {} }; /** * A RenderObject with a background image. * * The image is drawn to the RenderObject automatically when the * object is cleared, resized etc */ class RenderClearImage : public RenderObject { public: RenderClearImage(const std::string& texture) : RenderObject(), background_texture_name(texture), background_texture_(0) {} virtual void init() { RenderObject::init(); /* Load the image into a texture */ Texture::load(background_texture_name, &background_texture_, GL_LINEAR, GL_LINEAR, 0); } virtual void release() { glDeleteTextures(1, &background_texture_); background_texture_ = 0; RenderObject::release(); } virtual void clear() { static const GLfloat position[2 * 4] = { -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, }; static const GLfloat texcoord[2 * 4] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, }; make_current(); glClear(GL_COLOR_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, background_texture_); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); draw_quad_with_program(position, texcoord, main_program); glDisable(GL_BLEND); } private: std::string background_texture_name; GLuint background_texture_; }; /** * A RenderObject that blurs the target it is drawn to. */ class RenderWindowBlur : public RenderObject { public: RenderWindowBlur(unsigned int passes, unsigned int radius, bool separable, bool draw_contents = true) : RenderObject(), passes_(passes), radius_(radius), separable_(separable), draw_contents_(draw_contents) {} virtual void init() { RenderObject::init(); /* Only have one instance of the window contents data */ if (draw_contents_ && RenderWindowBlur::use_count == 0) window_contents_.init(); RenderWindowBlur::use_count++; } virtual void release() { RenderWindowBlur::use_count--; /* Only have one instance of the window contents data */ if (draw_contents_ && RenderWindowBlur::use_count == 0) window_contents_.release(); RenderObject::release(); } virtual void size(const LibMatrix::vec2& size) { RenderObject::size(size); if (draw_contents_) window_contents_.size(size); } virtual void render_to(RenderObject& target) { if (separable_) { Program& blur_program_h1 = blur_program_h(target.size().x()); Program& blur_program_v1 = blur_program_v(target.size().y()); for (unsigned int i = 0; i < passes_; i++) { render_from(target, blur_program_h1); RenderObject::render_to(target, blur_program_v1); } } else { Program& blur_program1 = blur_program(target.size().x(), target.size().y()); for (unsigned int i = 0; i < passes_; i++) { if (i % 2 == 0) render_from(target, blur_program1); else RenderObject::render_to(target, blur_program1); } if (passes_ % 2 == 1) RenderObject::render_to(target); } /* * Blend the window contents with the target texture. */ if (draw_contents_) { glEnable(GL_BLEND); /* * Blend the colors normally, but don't change the * destination alpha value. */ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE); window_contents_.position(position()); window_contents_.render_to(target); glDisable(GL_BLEND); } } private: Program& blur_program(unsigned int w, unsigned int h) { /* * If the size of the window has changed we must recreate * the shader to contain the correct texture step values. */ if (blur_program_dim_.x() != w || blur_program_dim_.y() != h || !blur_program_.ready()) { blur_program_dim_.x(w); blur_program_dim_.y(h); blur_program_.release(); ShaderSource vtx_source; ShaderSource frg_source; create_blur_shaders(vtx_source, frg_source, radius_, radius_ / 3.0, BlurDirectionBoth); frg_source.add_const("TextureStepX", 1.0 / w); frg_source.add_const("TextureStepY", 1.0 / h); Scene::load_shaders_from_strings(blur_program_, vtx_source.str(), frg_source.str()); } return blur_program_; } Program& blur_program_h(unsigned int w) { /* * If the size of the window has changed we must recreate * the shader to contain the correct texture step values. */ if (blur_program_dim_.x() != w || !blur_program_h_.ready()) { blur_program_dim_.x(w); blur_program_h_.release(); ShaderSource vtx_source; ShaderSource frg_source; create_blur_shaders(vtx_source, frg_source, radius_, radius_ / 3.0, BlurDirectionHorizontal); frg_source.add_const("TextureStepX", 1.0 / w); Scene::load_shaders_from_strings(blur_program_h_, vtx_source.str(), frg_source.str()); } return blur_program_h_; } Program& blur_program_v(unsigned int h) { /* * If the size of the window has changed we must recreate * the shader to contain the correct texture step values. */ if (blur_program_dim_.y() != h || !blur_program_v_.ready()) { blur_program_dim_.y(h); blur_program_v_.release(); ShaderSource vtx_source; ShaderSource frg_source; create_blur_shaders(vtx_source, frg_source, radius_, radius_ / 3.0, BlurDirectionVertical); frg_source.add_const("TextureStepY", 1.0 / h); Scene::load_shaders_from_strings(blur_program_v_, vtx_source.str(), frg_source.str()); } return blur_program_v_; } LibMatrix::uvec2 blur_program_dim_; Program blur_program_; Program blur_program_h_; Program blur_program_v_; unsigned int passes_; unsigned int radius_; bool separable_; bool draw_contents_; static int use_count; static RenderClearImage window_contents_; }; /** * A RenderObject that draws a drop shadow around the window. */ class RenderWindowShadow : public RenderObject { public: using RenderObject::size; RenderWindowShadow(unsigned int shadow_size, bool draw_contents = true) : RenderObject(), shadow_size_(shadow_size), draw_contents_(draw_contents) {} virtual void init() { RenderObject::init(); /* * Only have one instance of the resources. * This works only if all windows have the same size, which * is currently the case for this scene. If this condition * ceases to be true we will need to create the resources per * object. */ if (RenderWindowShadow::use_count == 0) { shadow_h_.init(); shadow_v_.init(); shadow_corner_.init(); if (draw_contents_) window_contents_.init(); } RenderWindowShadow::use_count++; } virtual void release() { RenderWindowShadow::use_count--; /* Only have one instance of the data */ if (RenderWindowShadow::use_count == 0) { shadow_h_.release(); shadow_v_.release(); shadow_corner_.release(); if (draw_contents_) window_contents_.release(); } RenderObject::release(); } virtual void size(const LibMatrix::vec2& size) { RenderObject::size(size); shadow_h_.size(LibMatrix::vec2(size.x() - shadow_size_, static_cast(shadow_size_))); shadow_v_.size(LibMatrix::vec2(size.y() - shadow_size_, static_cast(shadow_size_))); shadow_corner_.size(LibMatrix::vec2(static_cast(shadow_size_), static_cast(shadow_size_))); if (draw_contents_) window_contents_.size(size); } virtual void render_to(RenderObject& target) { glEnable(GL_BLEND); /* * Blend the colors normally, but don't change the * destination alpha value. */ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE); /* Bottom shadow */ shadow_h_.rotation(0.0); shadow_h_.position(position() + LibMatrix::vec2(shadow_size_, -shadow_h_.size().y())); shadow_h_.render_to(target); /* Right shadow */ shadow_v_.rotation(90.0); shadow_v_.position(position() + LibMatrix::vec2(size().x() + shadow_v_.size().y(), 0.0)); shadow_v_.render_to(target); /* Bottom right shadow */ shadow_corner_.rotation(0.0); shadow_corner_.position(position() + LibMatrix::vec2(size().x(), -shadow_corner_.size().y())); shadow_corner_.render_to(target); /* Top right shadow */ shadow_corner_.rotation(90.0); shadow_corner_.position(position() + size() + LibMatrix::vec2(shadow_corner_.size().x(), -shadow_corner_.size().y())); shadow_corner_.render_to(target); /* Bottom left shadow */ shadow_corner_.rotation(-90.0); shadow_corner_.position(position()); shadow_corner_.render_to(target); /* * Blend the window contents with the target texture. */ if (draw_contents_) { window_contents_.position(position()); window_contents_.render_to(target); } glDisable(GL_BLEND); } private: unsigned int shadow_size_; bool draw_contents_; static int use_count; static RenderClearImage window_contents_; static RenderClearImage shadow_h_; static RenderClearImage shadow_v_; static RenderClearImage shadow_corner_; }; int RenderWindowBlur::use_count = 0; RenderClearImage RenderWindowBlur::window_contents_("desktop-window"); int RenderWindowShadow::use_count = 0; RenderClearImage RenderWindowShadow::window_contents_("desktop-window"); RenderClearImage RenderWindowShadow::shadow_h_("desktop-shadow"); RenderClearImage RenderWindowShadow::shadow_v_("desktop-shadow"); RenderClearImage RenderWindowShadow::shadow_corner_("desktop-shadow-corner"); /******************************* * SceneDesktop implementation * *******************************/ /** * Private structure used to avoid contaminating scene.h with all of the * SceneDesktop internal classes. */ struct SceneDesktopPrivate { RenderScreen screen; RenderClearImage desktop; std::vector windows; SceneDesktopPrivate(Canvas &canvas) : screen(canvas), desktop("effect-2d") {} ~SceneDesktopPrivate() { Util::dispose_pointer_vector(windows); } }; SceneDesktop::SceneDesktop(Canvas &canvas) : Scene(canvas, "desktop") { priv_ = new SceneDesktopPrivate(canvas); options_["effect"] = Scene::Option("effect", "blur", "The effect to use", "blur,shadow"); options_["windows"] = Scene::Option("windows", "4", "the number of windows"); options_["window-size"] = Scene::Option("window-size", "0.35", "the window size as a percentage of the minimum screen dimension [0.0 - 0.5]"); options_["passes"] = Scene::Option("passes", "1", "the number of effect passes (effect dependent)"); options_["blur-radius"] = Scene::Option("blur-radius", "5", "the blur effect radius (in pixels)"); options_["separable"] = Scene::Option("separable", "true", "use separable convolution for the blur effect", "false,true"); options_["shadow-size"] = Scene::Option("shadow-size", "20", "the size of the shadow (in pixels)"); } SceneDesktop::~SceneDesktop() { delete priv_; } bool SceneDesktop::load() { return true; } void SceneDesktop::unload() { } bool SceneDesktop::setup() { if (!Scene::setup()) return false; /* Parse the options */ unsigned int windows(0); unsigned int passes(0); unsigned int blur_radius(0); float window_size_factor(0.0); unsigned int shadow_size(0); bool separable(options_["separable"].value == "true"); windows = Util::fromString(options_["windows"].value); window_size_factor = Util::fromString(options_["window-size"].value); passes = Util::fromString(options_["passes"].value); blur_radius = Util::fromString(options_["blur-radius"].value); shadow_size = Util::fromString(options_["shadow-size"].value); // Make sure the Texture object knows where to find our images. Texture::find_textures(); /* Ensure we get a transparent clear color for all following operations */ glClearColor(0.0, 0.0, 0.0, 0.0); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); /* Set up the screen and desktop RenderObjects */ priv_->screen.init(); priv_->desktop.init(); priv_->screen.size(LibMatrix::vec2(canvas_.width(), canvas_.height())); priv_->desktop.size(LibMatrix::vec2(canvas_.width(), canvas_.height())); /* Create the windows */ const float angular_step(2.0 * M_PI / windows); unsigned int min_dimension = std::min(canvas_.width(), canvas_.height()); float window_size(min_dimension * window_size_factor); static const LibMatrix::vec2 corner_offset(window_size / 2.0, window_size / 2.0); for (unsigned int i = 0; i < windows; i++) { LibMatrix::vec2 center(canvas_.width() * (0.5 + 0.25 * cos(i * angular_step)), canvas_.height() * (0.5 + 0.25 * sin(i * angular_step))); RenderObject* win; if (options_["effect"].value == "shadow") win = new RenderWindowShadow(shadow_size); else win = new RenderWindowBlur(passes, blur_radius, separable); win->init(); win->position(center - corner_offset); win->size(LibMatrix::vec2(window_size, window_size)); /* * Set the speed in increments of about 30 degrees (but not exactly, * so we don't get windows moving just on the X axis or Y axis). */ win->speed(LibMatrix::vec2(cos(0.1 + i * M_PI / 6.0) * canvas_.width() / 3, sin(0.1 + i * M_PI / 6.0) * canvas_.height() / 3)); /* * Perform a dummy rendering to ensure internal shaders are initialized * now, in order not to affect the benchmarking. */ win->render_to(priv_->desktop); priv_->windows.push_back(win); } /* * Ensure the screen is the current rendering target (it might have changed * to a FBO in the previous steps). */ priv_->screen.make_current(); currentFrame_ = 0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneDesktop::teardown() { for (std::vector::iterator winIt = priv_->windows.begin(); winIt != priv_->windows.end(); winIt++) { RenderObject* curObj = *winIt; curObj->release(); delete curObj; } priv_->windows.clear(); priv_->screen.make_current(); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); priv_->desktop.release(); priv_->screen.release(); Scene::teardown(); } void SceneDesktop::update() { double current_time = Util::get_timestamp_us() / 1000000.0; double dt = current_time - lastUpdateTime_; Scene::update(); std::vector& windows(priv_->windows); /* * Move the windows around the screen, bouncing them back when * they reach the edge. */ for (std::vector::const_iterator iter = windows.begin(); iter != windows.end(); iter++) { bool should_update = true; RenderObject *win = *iter; LibMatrix::vec2 new_pos( win->position().x() + win->speed().x() * dt, win->position().y() + win->speed().y() * dt); if (new_pos.x() < 0.0 || new_pos.x() + win->size().x() > static_cast(canvas_.width())) { win->speed(LibMatrix::vec2(-win->speed().x(), win->speed().y())); should_update = false; } if (new_pos.y() < 0.0 || new_pos.y() + win->size().y() > static_cast(canvas_.height())) { win->speed(LibMatrix::vec2(win->speed().x(), -win->speed().y())); should_update = false; } if (should_update) win->position(new_pos); } } void SceneDesktop::draw() { std::vector& windows(priv_->windows); /* Ensure we get a transparent clear color for all following operations */ glClearColor(0.0, 0.0, 0.0, 0.0); priv_->desktop.clear(); for (std::vector::const_iterator iter = windows.begin(); iter != windows.end(); iter++) { RenderObject *win = *iter; win->render_to(priv_->desktop); } priv_->desktop.render_to(priv_->screen); } Scene::ValidationResult SceneDesktop::validate() { static const double radius_3d(std::sqrt(3.0 * 2.0 * 2.0)); Canvas::Pixel ref; /* Parse the options */ unsigned int windows(0); unsigned int passes(0); unsigned int blur_radius(0); float window_size_factor(0.0); unsigned int shadow_size(0); windows = Util::fromString(options_["windows"].value); window_size_factor = Util::fromString(options_["window-size"].value); passes = Util::fromString(options_["passes"].value); blur_radius = Util::fromString(options_["blur-radius"].value); shadow_size = Util::fromString(options_["shadow-size"].value); if (options_["effect"].value == "blur") { if (windows == 4 && passes == 1 && blur_radius == 5) ref = Canvas::Pixel(0x89, 0xa3, 0x53, 0xff); else return Scene::ValidationUnknown; } else if (options_["effect"].value == "shadow") { if (windows == 4 && fabs(window_size_factor - 0.35) < 0.0001 && shadow_size == 20) { ref = Canvas::Pixel(0x1f, 0x27, 0x0d, 0xff); } else { return Scene::ValidationUnknown; } } Canvas::Pixel pixel = canvas_.read_pixel(512, 209); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/scene-function.cpp0000664000175000017500000001345712013417376017143 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "shader-source.h" #include "util.h" static const std::string shader_file_base(GLMARK_DATA_PATH"/shaders/function"); static const std::string vtx_file(shader_file_base + ".vert"); static const std::string frg_file(shader_file_base + ".frag"); static const std::string call_file(shader_file_base + "-call.all"); static const std::string step_low_file(shader_file_base + "-step-low.all"); static const std::string step_medium_file(shader_file_base + "-step-medium.all"); SceneFunction::SceneFunction(Canvas &pCanvas) : SceneGrid(pCanvas, "function") { options_["fragment-steps"] = Scene::Option("fragment-steps", "1", "The number of computational steps in the fragment shader"); options_["fragment-function"] = Scene::Option("fragment-function", "true", "Whether each computational step includes a function call", "false,true"); options_["vertex-steps"] = Scene::Option("vertex-steps", "1", "The number of computational steps in the vertex shader"); options_["vertex-function"] = Scene::Option("vertex-function", "true", "Whether each computational step includes an if-else clause", "false,true"); options_["vertex-complexity"] = Scene::Option("vertex-complexity", "low", "The complexity of each computational step in the vertex shader", "low,medium"); options_["fragment-complexity"] = Scene::Option("fragment-complexity", "low", "The complexity of each computational step in the fragment shader", "low,medium"); } SceneFunction::~SceneFunction() { } static std::string get_vertex_shader_source(int steps, bool function, std::string &complexity) { ShaderSource source(vtx_file); ShaderSource source_main; std::string step_file; if (complexity == "low") step_file = step_low_file; else if (complexity == "medium") step_file = step_medium_file; for (int i = 0; i < steps; i++) { if (function) source_main.append_file(call_file); else source_main.append_file(step_file); } if (function) source.replace_with_file("$PROCESS$", step_file); else source.replace("$PROCESS$", ""); source.replace("$MAIN$", source_main.str()); return source.str(); } static std::string get_fragment_shader_source(int steps, bool function, std::string &complexity) { ShaderSource source(frg_file); ShaderSource source_main; std::string step_file; if (complexity == "low") step_file = step_low_file; else if (complexity == "medium") step_file = step_medium_file; for (int i = 0; i < steps; i++) { if (function) source_main.append_file(call_file); else source_main.append_file(step_file); } if (function) source.replace_with_file("$PROCESS$", step_file); else source.replace("$PROCESS$", ""); source.replace("$MAIN$", source_main.str()); return source.str(); } bool SceneFunction::setup() { if (!SceneGrid::setup()) return false; /* Parse options */ bool vtx_function = options_["vertex-function"].value == "true"; bool frg_function = options_["fragment-function"].value == "true"; std::string vtx_complexity = options_["vertex-complexity"].value; std::string frg_complexity = options_["fragment-complexity"].value; int vtx_steps = Util::fromString(options_["vertex-steps"].value); int frg_steps = Util::fromString(options_["fragment-steps"].value); /* Load shaders */ std::string vtx_shader(get_vertex_shader_source(vtx_steps, vtx_function, vtx_complexity)); std::string frg_shader(get_fragment_shader_source(frg_steps, frg_function, frg_complexity)); if (!Scene::load_shaders_from_strings(program_, vtx_shader, frg_shader)) return false; program_.start(); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } Scene::ValidationResult SceneFunction::validate() { static const double radius_3d(std::sqrt(3.0 * 15.0 * 15.0)); int frg_steps = Util::fromString(options_["fragment-steps"].value); Canvas::Pixel ref; if (frg_steps == 5) ref = Canvas::Pixel(0x5e, 0x5e, 0x5e, 0xff); else return Scene::ValidationUnknown; Canvas::Pixel pixel = canvas_.read_pixel(293, 89); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/scene-buffer.cpp0000664000175000017500000003344512013417376016566 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker */ #include "scene.h" #include "log.h" #include "mat.h" #include "stack.h" #include "shader-source.h" #include "util.h" #include "gl-headers.h" #include /*********************** * Wave implementation * ***********************/ /** * A callback used to set up the grid by the Wave class. * It is called for each "quad" of the grid. */ static void wave_grid_conf(Mesh &mesh, int x, int y, int n_x, int n_y, LibMatrix::vec3 &ul, LibMatrix::vec3 &ll, LibMatrix::vec3 &ur, LibMatrix::vec3 &lr) { // These parameters are unused in this instance of a virtual callback // function. static_cast(x); static_cast(y); static_cast(n_x); static_cast(n_y); /* * Order matters here, so that Wave::vertex_length_index() can work. * Vertices of the triangles at index i that belong to length index i * are even, those that belong to i + 1 are odd. */ const LibMatrix::vec3* t[] = { &ll, &ur, &ul, &ur, &ll, &lr }; for (int i = 0; i < 6; i++) { mesh.next_vertex(); /* * Set the vertex position and the three vertex positions * of the triangle this vertex belongs to. */ mesh.set_attrib(0, *t[i]); mesh.set_attrib(1, *t[3 * (i / 3)]); mesh.set_attrib(2, *t[3 * (i / 3) + 1]); mesh.set_attrib(3, *t[3 * (i / 3) + 2]); } } /** * Renders a grid mesh modulated by a sine wave */ class WaveMesh { public: /** * Creates a wave mesh. * * @param length the total length of the grid (in model coordinates) * @param width the total width of the grid (in model coordinates) * @param nlength the number of length-wise grid subdivisions * @param nwidth the number of width-wise grid subdivisions * @param wavelength the wave length as a proportion of the length * @param duty_cycle the duty cycle () */ WaveMesh(double length, double width, size_t nlength, size_t nwidth, double wavelength, double duty_cycle) : length_(length), width_(width), nlength_(nlength), nwidth_(nwidth), wave_k_(2 * M_PI / (wavelength * length)), wave_period_(2.0 * M_PI / wave_k_), wave_full_period_(wave_period_ / duty_cycle), wave_velocity_(0.1 * length), displacement_(nlength + 1) { create_program(); create_mesh(); } ~WaveMesh() { reset(); } /** * Updates the state of a wave mesh. * * @param elapsed the time elapsed since the beginning of the rendering */ void update(double elapsed) { std::vector >& vertices(mesh_.vertices()); /* Figure out which length index ranges need update */ std::vector > ranges; for (size_t n = 0; n <= nlength_; n++) { double d(displacement(n, elapsed)); if (d != displacement_[n]) { if (ranges.size() > 0 && ranges.back().second == n - 1) { ranges.back().second = n; } else { ranges.push_back( std::pair(n > 0 ? n - 1 : 0, n) ); } } displacement_[n] = d; } /* Update the vertex data of the changed ranges */ for (std::vector >::iterator iter = ranges.begin(); iter != ranges.end(); iter++) { /* First vertex of length index range */ size_t vstart(iter->first * nwidth_ * 6 + (iter->first % 2)); /* * First vertex not included in the range. We should also update all * vertices of triangles touching index i. */ size_t vend((iter->second + (iter->second < nlength_)) * nwidth_ * 6); for (size_t v = vstart; v < vend; v++) { size_t vt = 3 * (v / 3); vertices[v][0 * 3 + 2] = displacement_[vertex_length_index(v)]; vertices[v][1 * 3 + 2] = displacement_[vertex_length_index(vt)]; vertices[v][2 * 3 + 2] = displacement_[vertex_length_index(vt + 1)]; vertices[v][3 * 3 + 2] = displacement_[vertex_length_index(vt + 2)]; } /* Update pair with actual vertex range */ iter->first = vstart; iter->second = vend - 1; } mesh_.update_vbo(ranges); } Mesh& mesh() { return mesh_; } Program& program() { return program_; } void reset() { program_.stop(); program_.release(); mesh_.reset(); } private: Mesh mesh_; Program program_; double length_; double width_; size_t nlength_; size_t nwidth_; /* Wave parameters */ double wave_k_; double wave_period_; double wave_full_period_; double wave_fill_; double wave_velocity_; std::vector displacement_; /** * Calculates the length index of a vertex. */ size_t vertex_length_index(size_t v) { return v / (6 * nwidth_) + (v % 2); } /** * The sine wave function with duty-cycle. * * @param x the space coordinate * * @return the operation error code */ double wave_func(double x) { double r(fmod(x, wave_full_period_)); if (r < 0) r += wave_full_period_; /* * Return either the sine value or 0.0, depending on the * wave duty cycle. */ if (r > wave_period_) { return 0; } else { return 0.2 * std::sin(wave_k_ * r); } } /** * Calculates the displacement of the wave. * * @param n the length index * @param elapsed the time elapsed since the beginning of the rendering * * @return the displacement at point n at time elapsed */ double displacement(size_t n, double elapsed) { double x(n * length_ / nlength_); return wave_func(x - wave_velocity_ * elapsed); } /** * Creates the GL shader program. */ void create_program() { /* Set up shaders */ static const std::string vtx_shader_filename( GLMARK_DATA_PATH"/shaders/buffer-wireframe.vert"); static const std::string frg_shader_filename( GLMARK_DATA_PATH"/shaders/buffer-wireframe.frag"); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return; } } /** * Creates the grid mesh. */ void create_mesh() { /* * We need to pass the positions of all vertex of the triangle * in order to draw the wireframe. */ std::vector vertex_format; vertex_format.push_back(3); // Position of vertex vertex_format.push_back(3); // Position of triangle vertex 0 vertex_format.push_back(3); // Position of triangle vertex 1 vertex_format.push_back(3); // Position of triangle vertex 2 mesh_.set_vertex_format(vertex_format); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["tvertex0"].location()); attrib_locations.push_back(program_["tvertex1"].location()); attrib_locations.push_back(program_["tvertex2"].location()); mesh_.set_attrib_locations(attrib_locations); mesh_.make_grid(nlength_, nwidth_, length_, width_, 0.0, wave_grid_conf); } }; /****************************** * SceneBuffer implementation * ******************************/ struct SceneBufferPrivate { WaveMesh *wave; SceneBufferPrivate() : wave(0) {} ~SceneBufferPrivate() { delete wave; } }; SceneBuffer::SceneBuffer(Canvas &pCanvas) : Scene(pCanvas, "buffer") { priv_ = new SceneBufferPrivate(); options_["interleave"] = Scene::Option("interleave", "false", "Whether to interleave vertex attribute data", "false,true"); options_["update-method"] = Scene::Option("update-method", "map", "Which method to use to update vertex data", "map,subdata"); options_["update-fraction"] = Scene::Option("update-fraction", "1.0", "The fraction of the mesh length that is updated at every iteration (0.0-1.0)"); options_["update-dispersion"] = Scene::Option("update-dispersion", "0.0", "How dispersed the updates are [0.0 - 1.0]"); options_["columns"] = Scene::Option("columns", "100", "The number of mesh subdivisions length-wise"); options_["rows"] = Scene::Option("rows", "20", "The number of mesh subdisivisions width-wise"); options_["buffer-usage"] = Scene::Option("buffer-usage", "static", "How the buffer will be used", "static,stream,dynamic"); } SceneBuffer::~SceneBuffer() { delete priv_; } bool SceneBuffer::supported(bool show_errors) { if (options_["update-method"].value != "subdata" && (GLExtensions::MapBuffer == 0 || GLExtensions::UnmapBuffer == 0)) { if (show_errors) { Log::error("Requested MapBuffer VBO update method but GL_OES_mapbuffer" " is not supported!\n"); } return false; } return true; } bool SceneBuffer::load() { running_ = false; return true; } void SceneBuffer::unload() { } bool SceneBuffer::setup() { using LibMatrix::vec3; if (!Scene::setup()) return false; bool interleave = (options_["interleave"].value == "true"); Mesh::VBOUpdateMethod update_method; Mesh::VBOUsage usage; double update_fraction; double update_dispersion; size_t nlength; size_t nwidth; if (options_["update-method"].value == "map") update_method = Mesh::VBOUpdateMethodMap; else if (options_["update-method"].value == "subdata") update_method = Mesh::VBOUpdateMethodSubData; else update_method = Mesh::VBOUpdateMethodMap; if (options_["buffer-usage"].value == "static") usage = Mesh::VBOUsageStatic; else if (options_["buffer-usage"].value == "stream") usage = Mesh::VBOUsageStream; else usage = Mesh::VBOUsageDynamic; update_fraction = Util::fromString(options_["update-fraction"].value); update_dispersion = Util::fromString(options_["update-dispersion"].value); nlength = Util::fromString(options_["columns"].value); nwidth = Util::fromString(options_["rows"].value); priv_->wave = new WaveMesh(5.0, 2.0, nlength, nwidth, update_fraction * (1.0 - update_dispersion + 0.0001), update_fraction); priv_->wave->mesh().interleave(interleave); priv_->wave->mesh().vbo_update_method(update_method); priv_->wave->mesh().vbo_usage(usage); priv_->wave->mesh().build_vbo(); priv_->wave->program().start(); priv_->wave->program()["Viewport"] = LibMatrix::vec2(canvas_.width(), canvas_.height()); glDisable(GL_CULL_FACE); currentFrame_ = 0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneBuffer::teardown() { delete priv_->wave; priv_->wave = 0; glEnable(GL_CULL_FACE); Scene::teardown(); } void SceneBuffer::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; priv_->wave->update(elapsed_time); } void SceneBuffer::draw() { LibMatrix::Stack4 model_view; // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::mat4 model_view_proj(canvas_.projection()); model_view.translate(0.0, 0.0, -4.0); model_view.rotate(45.0, -1.0, 0.0, 0.0); model_view_proj *= model_view.getCurrent(); priv_->wave->program()["ModelViewProjectionMatrix"] = model_view_proj; priv_->wave->mesh().render_vbo(); } Scene::ValidationResult SceneBuffer::validate() { static const double radius_3d(std::sqrt(3.0 * 2.0 * 2.0)); Canvas::Pixel ref(0x34, 0x99, 0xd7, 0xff); Canvas::Pixel pixel = canvas_.read_pixel(402, 189); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/scene-bump.cpp0000664000175000017500000003125712013417376016257 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jesse Barker (glmark2) */ #include "scene.h" #include "log.h" #include "mat.h" #include "stack.h" #include "shader-source.h" #include "model.h" #include "texture.h" #include "util.h" #include SceneBump::SceneBump(Canvas &pCanvas) : Scene(pCanvas, "bump"), texture_(0), rotation_(0.0f), rotationSpeed_(0.0f) { options_["bump-render"] = Scene::Option("bump-render", "off", "How to render bumps", "off,normals,normals-tangent,height,high-poly"); } SceneBump::~SceneBump() { } bool SceneBump::load() { rotationSpeed_ = 36.0f; running_ = false; return true; } void SceneBump::unload() { } bool SceneBump::setup_model_plain(const std::string &type) { static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-poly.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-poly.frag"); static const std::string low_poly_filename("asteroid-low"); static const std::string high_poly_filename("asteroid-high"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); Model model; /* Calculate the half vector */ LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z()); halfVector.normalize(); halfVector += LibMatrix::vec3(0.0, 0.0, 1.0); halfVector.normalize(); std::string poly_filename = type == "high-poly" ? high_poly_filename : low_poly_filename; if(!model.load(poly_filename)) return false; model.calculate_normals(); /* Tell the converter that we only care about position and normal attributes */ std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); model.convert_to_mesh(mesh_, attribs); /* Load shaders */ ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); /* Add constants to shaders */ frg_source.add_const("LightSourcePosition", lightPosition); frg_source.add_const("LightSourceHalfVector", halfVector); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); return true; } bool SceneBump::setup_model_normals() { static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals.frag"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); Model model; if(!model.load("asteroid-low")) return false; /* Calculate the half vector */ LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z()); halfVector.normalize(); halfVector += LibMatrix::vec3(0.0, 0.0, 1.0); halfVector.normalize(); /* * We don't care about the vertex normals. We are using a per-fragment * normal map (in object space coordinates). */ std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeTexcoord, 2)); model.convert_to_mesh(mesh_, attribs); /* Load shaders */ ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); /* Add constants to shaders */ frg_source.add_const("LightSourcePosition", lightPosition); frg_source.add_const("LightSourceHalfVector", halfVector); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["texcoord"].location()); mesh_.set_attrib_locations(attrib_locations); if (!Texture::load("asteroid-normal-map", &texture_, GL_NEAREST, GL_NEAREST, 0)) { return false; } return true; } bool SceneBump::setup_model_normals_tangent() { static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals-tangent.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals-tangent.frag"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); Model model; if(!model.load("asteroid-low")) return false; model.calculate_normals(); /* Calculate the half vector */ LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z()); halfVector.normalize(); halfVector += LibMatrix::vec3(0.0, 0.0, 1.0); halfVector.normalize(); std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); attribs.push_back(std::pair(Model::AttribTypeTexcoord, 2)); attribs.push_back(std::pair(Model::AttribTypeTangent, 3)); model.convert_to_mesh(mesh_, attribs); /* Load shaders */ ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); /* Add constants to shaders */ frg_source.add_const("LightSourcePosition", lightPosition); frg_source.add_const("LightSourceHalfVector", halfVector); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); attrib_locations.push_back(program_["texcoord"].location()); attrib_locations.push_back(program_["tangent"].location()); mesh_.set_attrib_locations(attrib_locations); if (!Texture::load("asteroid-normal-map-tangent", &texture_, GL_NEAREST, GL_NEAREST, 0)) { return false; } return true; } bool SceneBump::setup_model_height() { static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-height.vert"); static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-height.frag"); static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f); Model model; if(!model.load("asteroid-low")) return false; model.calculate_normals(); /* Calculate the half vector */ LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z()); halfVector.normalize(); halfVector += LibMatrix::vec3(0.0, 0.0, 1.0); halfVector.normalize(); std::vector > attribs; attribs.push_back(std::pair(Model::AttribTypePosition, 3)); attribs.push_back(std::pair(Model::AttribTypeNormal, 3)); attribs.push_back(std::pair(Model::AttribTypeTexcoord, 2)); attribs.push_back(std::pair(Model::AttribTypeTangent, 3)); model.convert_to_mesh(mesh_, attribs); /* Load shaders */ ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); /* Add constants to shaders */ frg_source.add_const("LightSourcePosition", lightPosition); frg_source.add_const("LightSourceHalfVector", halfVector); frg_source.add_const("TextureStepX", 1.0 / 1024.0); frg_source.add_const("TextureStepY", 1.0 / 1024.0); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); attrib_locations.push_back(program_["texcoord"].location()); attrib_locations.push_back(program_["tangent"].location()); mesh_.set_attrib_locations(attrib_locations); if (!Texture::load("asteroid-height-map", &texture_, GL_NEAREST, GL_NEAREST, 0)) { return false; } return true; } bool SceneBump::setup() { if (!Scene::setup()) return false; const std::string &bump_render = options_["bump-render"].value; Texture::find_textures(); Model::find_models(); bool setup_succeeded = false; if (bump_render == "normals") setup_succeeded = setup_model_normals(); else if (bump_render == "normals-tangent") setup_succeeded = setup_model_normals_tangent(); else if (bump_render == "height") setup_succeeded = setup_model_height(); else if (bump_render == "off" || bump_render == "high-poly") setup_succeeded = setup_model_plain(bump_render); if (!setup_succeeded) return false; mesh_.build_vbo(); program_.start(); // Load texture sampler value program_["NormalMap"] = 0; program_["HeightMap"] = 0; currentFrame_ = 0; rotation_ = 0.0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneBump::teardown() { mesh_.reset(); program_.stop(); program_.release(); glDeleteTextures(1, &texture_); texture_ = 0; Scene::teardown(); } void SceneBump::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; rotation_ = rotationSpeed_ * elapsed_time; } void SceneBump::draw() { LibMatrix::Stack4 model_view; // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::mat4 model_view_proj(canvas_.projection()); model_view.translate(0.0f, 0.0f, -3.5f); model_view.rotate(rotation_, 0.0f, 1.0f, 0.0f); model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. LibMatrix::mat4 normal_matrix(model_view.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); mesh_.render_vbo(); } Scene::ValidationResult SceneBump::validate() { static const double radius_3d(std::sqrt(3.0)); if (rotation_ != 0) return Scene::ValidationUnknown; Canvas::Pixel ref; Canvas::Pixel pixel = canvas_.read_pixel(canvas_.width() / 2, canvas_.height() / 2); const std::string &bump_render = options_["bump-render"].value; if (bump_render == "off") ref = Canvas::Pixel(0x81, 0x81, 0x81, 0xff); else if (bump_render == "high-poly") ref = Canvas::Pixel(0x9c, 0x9c, 0x9c, 0xff); else if (bump_render == "normals") ref = Canvas::Pixel(0xa4, 0xa4, 0xa4, 0xff); else if (bump_render == "normals-tangent") ref = Canvas::Pixel(0x99, 0x99, 0x99, 0xff); else if (bump_render == "height") ref = Canvas::Pixel(0x9d, 0x9d, 0x9d, 0xff); else return Scene::ValidationUnknown; double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } } glmark2-2012.08/./src/benchmark-collection.h0000664000175000017500000000311612013417376017742 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #ifndef GLMARK2_BENCHMARK_COLLECTION_H_ #define GLMARK2_BENCHMARK_COLLECTION_H_ #include #include #include "benchmark.h" class BenchmarkCollection { public: BenchmarkCollection() {} ~BenchmarkCollection(); /* * Adds benchmarks to the collection. */ void add(const std::vector &benchmarks); /* * Populates the collection guided by the global options. */ void populate_from_options(); /* * Whether the benchmarks in this collection need decoration. */ bool needs_decoration(); const std::vector& benchmarks() { return benchmarks_; } private: void add_benchmarks_from_files(); bool benchmarks_contain_normal_scenes(); std::vector benchmarks_; }; #endif /* GLMARK2_BENCHMARK_COLLECTION_H_ */ glmark2-2012.08/./src/model.h0000664000175000017500000000676312013417376014772 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_MODEL_H_ #define GLMARK2_MODEL_H_ #include #include #include #include #include "vec.h" // Forward declare the mesh object. We don't need the whole header here. class Mesh; enum ModelFormat { MODEL_INVALID, MODEL_3DS, MODEL_OBJ }; /** * A descriptor for a model file. */ class ModelDescriptor { std::string name_; std::string pathname_; ModelFormat format_; ModelDescriptor(); public: ModelDescriptor(const std::string& name, ModelFormat format, const std::string& pathname) : name_(name), pathname_(pathname), format_(format) {} ~ModelDescriptor() {} const std::string& pathname() const { return pathname_; } ModelFormat format() const { return format_; } }; typedef std::map ModelMap; /** * A model as loaded from a 3D object data file. */ class Model { public: typedef enum { AttribTypePosition = 1, AttribTypeNormal, AttribTypeTexcoord, AttribTypeTangent, AttribTypeBitangent, AttribTypeCustom } AttribType; Model() : gotTexcoords_(false) {} ~Model() {} bool load(const std::string& name); bool needTexcoords() const { return !gotTexcoords_; } void calculate_texcoords(); void calculate_normals(); void convert_to_mesh(Mesh &mesh); void convert_to_mesh(Mesh &mesh, const std::vector > &attribs); const LibMatrix::vec3& minVec() const { return minVec_; } const LibMatrix::vec3& maxVec() const { return maxVec_; } static const ModelMap& find_models(); private: // If the model we loaded contained texcoord data... bool gotTexcoords_; struct Face { uint32_t a, b, c; uint16_t face_flags; }; struct Vertex { LibMatrix::vec3 v; LibMatrix::vec3 n; LibMatrix::vec2 t; LibMatrix::vec3 nt; LibMatrix::vec3 nb; }; struct Object { Object(const std::string &name) : name(name) {} std::string name; std::vector vertices; std::vector faces; }; void append_object_to_mesh(const Object &object, Mesh &mesh, int p_pos, int n_pos, int t_pos, int nt_pos, int nb_pos); bool load_3ds(const std::string &filename); bool load_obj(const std::string &filename); // For vertices of the bounding box for this model. void compute_bounding_box(const Object& object); LibMatrix::vec3 minVec_; LibMatrix::vec3 maxVec_; std::vector objects_; }; #endif glmark2-2012.08/./src/gl-headers.h0000664000175000017500000000353212013417376015674 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_GL_HEADERS_H_ #define GLMARK2_GL_HEADERS_H_ #define GL_GLEXT_PROTOTYPES #if USE_GL #include #include #ifndef GL_RGB565 #define GL_RGB565 0x8D62 #endif #elif USE_GLESv2 #include #include #ifndef GL_WRITE_ONLY #define GL_WRITE_ONLY GL_WRITE_ONLY_OES #endif #ifndef GL_DEPTH_COMPONENT24 #define GL_DEPTH_COMPONENT24 GL_DEPTH_COMPONENT24_OES #endif #ifndef GL_DEPTH_COMPONENT32 #define GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT32_OES #endif #ifndef GL_RGBA8 #define GL_RGBA8 GL_RGBA8_OES #endif #ifndef GL_RGB8 #define GL_RGB8 GL_RGB8_OES #endif #endif #include /** * Struct that holds pointers to functions that belong to extensions * in either GL2.0 or GLES2.0. */ struct GLExtensions { /** * Whether the current context has support for a GL extension. * * @return true if the extension is supported */ static bool support(const std::string &ext); static void* (*MapBuffer) (GLenum target, GLenum access); static GLboolean (*UnmapBuffer) (GLenum target); }; #endif glmark2-2012.08/./src/canvas-x11.h0000664000175000017500000000710612013417376015544 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_CANVAS_X11_H_ #define GLMARK2_CANVAS_X11_H_ #include "canvas.h" #include #include /** * Canvas for rendering with GL to an X11 window. */ class CanvasX11 : public Canvas { public: ~CanvasX11() {} virtual bool init(); virtual bool reset(); virtual void visible(bool visible); virtual void clear(); virtual void update(); virtual void print_info(); virtual Pixel read_pixel(int x, int y); virtual void write_to_file(std::string &filename); virtual bool should_quit(); virtual void resize(int width, int height); virtual unsigned int fbo(); protected: CanvasX11(int width, int height) : Canvas(width, height), xwin_(0), xdpy_(0), fullscreen_(false), gl_color_format_(0), gl_depth_format_(0), color_renderbuffer_(0), depth_renderbuffer_(0), fbo_(0) {} /** * Gets the XVisualInfo to use for creating the X window with. * * The caller should XFree() the returned XVisualInfo when done. * * This method should be implemented in derived classes. * * @return the XVisualInfo */ virtual XVisualInfo *get_xvisualinfo() = 0; /** * Makes the canvas the current target for GL rendering. * * This method should be implemented in derived classes. * * @return whether the operation succeeded */ virtual bool make_current() = 0; /** * Resets the underlying GL context for rendering. * * This method should be implemented in derived classes. * * @return whether the operation succeeded */ virtual bool reset_context() = 0; /** * Swaps the GL buffers (assuming double buffering is used). * * This method should be implemented in derived classes. * * @return whether the operation succeeded */ virtual void swap_buffers() = 0; /** * Gets information about the GL visual used for this canvas. * * This method should be implemented in derived classes. */ virtual void get_glvisualconfig(GLVisualConfig &visual_config) = 0; /** * Whether the current implementation supports GL(ES) 2.0. * * @return true if it supports GL(ES) 2.0, false otherwise */ bool supports_gl2(); /** The X window associated with this canvas. */ Window xwin_; /** The X display associated with this canvas. */ Display *xdpy_; private: void resize_no_viewport(int width, int height); bool ensure_x_window(); bool do_make_current(); bool ensure_gl_formats(); bool ensure_fbo(); void release_fbo(); const char *get_gl_format_str(GLenum f); bool fullscreen_; GLenum gl_color_format_; GLenum gl_depth_format_; GLuint color_renderbuffer_; GLuint depth_renderbuffer_; GLuint fbo_; }; #endif glmark2-2012.08/./src/options.h0000664000175000017500000000323412013417376015353 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of glcompbench. * * glcompbench is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * glcompbench is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with glcompbench. If not, see . * * Authors: * Alexandros Frantzis * Jesse Barker */ #ifndef OPTIONS_H_ #define OPTIONS_H_ #include #include #include "gl-visual-config.h" struct Options { enum FrameEnd { FrameEndDefault, FrameEndNone, FrameEndSwap, FrameEndFinish, FrameEndReadPixels }; static bool parse_args(int argc, char **argv); static void print_help(); static std::vector benchmarks; static std::vector benchmark_files; static bool validate; static FrameEnd frame_end; static std::pair size; static bool list_scenes; static bool show_all_options; static bool show_debug; static bool show_help; static bool reuse_context; static bool run_forever; static bool annotate; static bool offscreen; static GLVisualConfig visual_config; }; #endif /* OPTIONS_H_ */ glmark2-2012.08/./src/android.cpp0000664000175000017500000003704512013417376015642 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * Copyright © 2011 0xlab - http://0xlab.org/ * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) * Jim Huang (Strict JNI registration using JNI_OnLoad()) */ #include #include #include #include #include #include "canvas-android.h" #include "benchmark.h" #include "options.h" #include "log.h" #include "util.h" #include "main-loop.h" #include "benchmark-collection.h" static Canvas *g_canvas; static MainLoop *g_loop; static BenchmarkCollection *g_benchmark_collection; static std::ostream *g_log_extra; class MainLoopAndroid : public MainLoop { public: MainLoopAndroid(Canvas &canvas, const std::vector &benchmarks) : MainLoop(canvas, benchmarks) {} virtual void log_scene_info() {} virtual void log_scene_result() { if (scene_setup_status_ == SceneSetupStatusSuccess) { Log::info("%s FPS: %u FrameTime: %.3f ms\n", scene_->info_string().c_str(), scene_->average_fps(), 1000.0 / scene_->average_fps()); } else if (scene_setup_status_ == SceneSetupStatusUnsupported) { Log::info("%s Unsupported\n", scene_->info_string().c_str()); } else { Log::info("%s Set up failed\n", scene_->info_string().c_str()); } } }; class MainLoopDecorationAndroid : public MainLoopDecoration { public: MainLoopDecorationAndroid(Canvas &canvas, const std::vector &benchmarks) : MainLoopDecoration(canvas, benchmarks) {} virtual void log_scene_info() {} virtual void log_scene_result() { if (scene_setup_status_ == SceneSetupStatusSuccess) { Log::info("%s FPS: %u FrameTime: %.3f ms\n", scene_->info_string().c_str(), scene_->average_fps(), 1000.0 / scene_->average_fps()); } else if (scene_setup_status_ == SceneSetupStatusUnsupported) { Log::info("%s Unsupported\n", scene_->info_string().c_str()); } else { Log::info("%s Set up failed\n", scene_->info_string().c_str()); } } }; /** * Converts an std::vector containing arguments to argc,argv. */ static void arg_vector_to_argv(const std::vector &arguments, int &argc, char **&argv) { argc = arguments.size() + 1; argv = new char* [argc]; argv[0] = strdup("glmark2"); for (unsigned int i = 0; i < arguments.size(); i++) argv[i + 1] = strdup(arguments[i].c_str()); } /** * Populates the command line arguments from the arguments file. * * @param argc the number of arguments * @param argv the argument array */ static void get_args_from_file(const std::string &arguments_file, int &argc, char **&argv) { std::vector arguments; std::ifstream ifs(arguments_file.c_str()); if (!ifs.fail()) { std::string line; while (getline(ifs, line)) { if (!line.empty()) Util::split(line, ' ', arguments, Util::SplitModeQuoted); } } arg_vector_to_argv(arguments, argc, argv); } /** * Populates the command line arguments from the arguments file. * * @param argc the number of arguments * @param argv the argument array */ static void get_args_from_string(const std::string &args_str, int &argc, char **&argv) { std::vector arguments; Util::split(args_str, ' ', arguments, Util::SplitModeQuoted); arg_vector_to_argv(arguments, argc, argv); } /** * Releases the command line arguments. * * @param argc the number of arguments * @param argv the argument array */ static void release_args(int argc, char **argv) { for (int i = 0; i < argc; i++) free(argv[i]); delete[] argv; } /** * Converts a GLVisualConfig Java object to a GLVisualConfig C++ object. * * @param env the JNIEnv * @param jvc the Java VisualConfig object to convert * @param vc the C++ VisualConfig object to fill */ static void gl_visual_config_from_jobject(JNIEnv *env, jobject jvc, GLVisualConfig &vc) { jclass cls = env->GetObjectClass(jvc); jfieldID fid; fid = env->GetFieldID(cls, "red", "I"); vc.red = env->GetIntField(jvc, fid); fid = env->GetFieldID(cls, "green", "I"); vc.green = env->GetIntField(jvc, fid); fid = env->GetFieldID(cls, "blue", "I"); vc.blue = env->GetIntField(jvc, fid); fid = env->GetFieldID(cls, "alpha", "I"); vc.alpha = env->GetIntField(jvc, fid); fid = env->GetFieldID(cls, "depth", "I"); vc.depth = env->GetIntField(jvc, fid); fid = env->GetFieldID(cls, "buffer", "I"); vc.buffer = env->GetIntField(jvc, fid); } /** * Creates a SceneInfo Java object from a Scene. * * @param env the JNIEnv */ static jobject scene_info_from_scene(JNIEnv *env, Scene &scene) { jclass cls = env->FindClass("org/linaro/glmark2/SceneInfo"); jmethodID constructor = env->GetMethodID(cls, "", "(Ljava/lang/String;)V"); jmethodID add_option = env->GetMethodID(cls, "addOption", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V"); /* Create the SceneInfo object */ jstring name = env->NewStringUTF(scene.name().c_str()); jobject scene_info = env->NewObject(cls, constructor, name); const std::map &options = scene.options(); /* Add options to the SceneInfo object */ for (std::map::const_iterator opt_iter = options.begin(); opt_iter != options.end(); opt_iter++) { const Scene::Option &opt = opt_iter->second; jstring opt_name = env->NewStringUTF(opt.name.c_str()); jstring opt_description = env->NewStringUTF(opt.description.c_str()); jstring opt_default_value = env->NewStringUTF(opt.default_value.c_str()); /* Create and populate the acceptable values array */ jclass string_cls = env->FindClass("java/lang/String"); jobjectArray opt_acceptable_values = env->NewObjectArray(opt.acceptable_values.size(), string_cls, 0); for (size_t i = 0; i < opt.acceptable_values.size(); i++) { jstring opt_value = env->NewStringUTF(opt.acceptable_values[i].c_str()); env->SetObjectArrayElement(opt_acceptable_values, i, opt_value); env->DeleteLocalRef(opt_value); } env->CallVoidMethod(scene_info, add_option, opt_name, opt_description, opt_default_value, opt_acceptable_values); env->DeleteLocalRef(opt_name); env->DeleteLocalRef(opt_description); env->DeleteLocalRef(opt_default_value); env->DeleteLocalRef(opt_acceptable_values); } return scene_info; } class DummyCanvas : public Canvas { public: DummyCanvas() : Canvas(0, 0) {} }; /** * Creates all the available scenes and adds them to the supplied vector. * * @param scenes the vector to add the scenes to * @param canvas the canvas to create the scenes with */ static void create_and_add_scenes(std::vector& scenes, Canvas& canvas) { scenes.push_back(new SceneDefaultOptions(canvas)); scenes.push_back(new SceneBuild(canvas)); scenes.push_back(new SceneTexture(canvas)); scenes.push_back(new SceneShading(canvas)); scenes.push_back(new SceneConditionals(canvas)); scenes.push_back(new SceneFunction(canvas)); scenes.push_back(new SceneLoop(canvas)); scenes.push_back(new SceneBump(canvas)); scenes.push_back(new SceneEffect2D(canvas)); scenes.push_back(new ScenePulsar(canvas)); scenes.push_back(new SceneDesktop(canvas)); scenes.push_back(new SceneBuffer(canvas)); scenes.push_back(new SceneIdeas(canvas)); scenes.push_back(new SceneTerrain(canvas)); scenes.push_back(new SceneJellyfish(canvas)); } void Java_org_linaro_glmark2_native_init(JNIEnv* env, jclass clazz, jobject asset_manager, jstring args, jstring log_file) { static_cast(clazz); static const std::string arguments_file("/data/glmark2/args"); int argc = 0; char **argv = 0; /* Load arguments from argument string or arguments file and parse them */ if (args) { if (env->GetStringUTFLength(args) > 0) { const char *args_c_str = env->GetStringUTFChars(args, 0); if (args_c_str) { get_args_from_string(std::string(args_c_str), argc, argv); env->ReleaseStringUTFChars(args, args_c_str); } } } else { get_args_from_file(arguments_file, argc, argv); } Options::parse_args(argc, argv); release_args(argc, argv); /* Get the log file path and open the log file */ const char *log_file_c_str = env->GetStringUTFChars(log_file, 0); if (log_file_c_str) { g_log_extra = new std::ofstream(log_file_c_str, std::ios::binary); env->ReleaseStringUTFChars(log_file, log_file_c_str); } /* Force reuse of EGL/GL context */ Options::reuse_context = true; Log::init("glmark2", Options::show_debug, g_log_extra); Util::android_set_asset_manager(AAssetManager_fromJava(env, asset_manager)); g_canvas = new CanvasAndroid(100, 100); g_canvas->init(); Log::info("glmark2 %s\n", GLMARK_VERSION); g_canvas->print_info(); std::vector scenes; /* Add and register scenes */ create_and_add_scenes(scenes, *g_canvas); for (std::vector::const_iterator iter = scenes.begin(); iter != scenes.end(); iter++) { Benchmark::register_scene(**iter); } g_benchmark_collection = new BenchmarkCollection(); g_benchmark_collection->populate_from_options(); if (g_benchmark_collection->needs_decoration()) { g_loop = new MainLoopDecorationAndroid(*g_canvas, g_benchmark_collection->benchmarks()); } else { g_loop = new MainLoopAndroid(*g_canvas, g_benchmark_collection->benchmarks()); } } void Java_org_linaro_glmark2_native_resize(JNIEnv* env, jclass clazz, jint w, jint h) { static_cast(env); static_cast(clazz); Log::debug("Resizing to %d x %d\n", w, h); g_canvas->resize(w, h); } void Java_org_linaro_glmark2_native_done(JNIEnv* env) { static_cast(env); delete g_loop; delete g_benchmark_collection; delete g_canvas; delete g_log_extra; } jboolean Java_org_linaro_glmark2_native_render(JNIEnv* env) { static_cast(env); if (!g_loop->step()) { Log::info("glmark2 Score: %u\n", g_loop->score()); return false; } return true; } jint Java_org_linaro_glmark2_native_scoreConfig(JNIEnv* env, jclass clazz, jobject jvc, jobject jtarget) { static_cast(clazz); GLVisualConfig vc; GLVisualConfig target; gl_visual_config_from_jobject(env, jvc, vc); gl_visual_config_from_jobject(env, jtarget, target); return vc.match_score(target); } jobjectArray Java_org_linaro_glmark2_native_getSceneInfo(JNIEnv* env, jclass clazz, jobject asset_manager) { static_cast(clazz); Util::android_set_asset_manager(AAssetManager_fromJava(env, asset_manager)); std::vector scenes; DummyCanvas canvas; std::vector si_vector; create_and_add_scenes(scenes, canvas); /* Create SceneInfo instances for all the scenes */ for (std::vector::const_iterator iter = scenes.begin(); iter != scenes.end(); iter++) { jobject si = scene_info_from_scene(env, **iter); si_vector.push_back(si); } /* Create a SceneInfo[] array */ jclass si_cls = env->FindClass("org/linaro/glmark2/SceneInfo"); jobjectArray si_array = env->NewObjectArray(si_vector.size(), si_cls, 0); /* Populate the SceneInfo[] array */ for (size_t i = 0; i < si_vector.size(); i++) env->SetObjectArrayElement(si_array, i, si_vector[i]); Util::dispose_pointer_vector(scenes); return si_array; } static JNINativeMethod glmark2_native_methods[] = { { "init", "(Landroid/content/res/AssetManager;Ljava/lang/String;Ljava/lang/String;)V", reinterpret_cast(Java_org_linaro_glmark2_native_init) }, { "resize", "(II)V", reinterpret_cast(Java_org_linaro_glmark2_native_resize) }, { "done", "()V", reinterpret_cast(Java_org_linaro_glmark2_native_done) }, { "render", "()Z", reinterpret_cast(Java_org_linaro_glmark2_native_render) }, { "scoreConfig", "(Lorg/linaro/glmark2/GLVisualConfig;Lorg/linaro/glmark2/GLVisualConfig;)I", reinterpret_cast(Java_org_linaro_glmark2_native_scoreConfig) }, { "getSceneInfo", "(Landroid/content/res/AssetManager;)[Lorg/linaro/glmark2/SceneInfo;", reinterpret_cast(Java_org_linaro_glmark2_native_getSceneInfo) } }; static int register_native_methods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods) { jclass clazz; clazz = env->FindClass(className); if (clazz == NULL) { Log::error("Native registration unable to find class '%s'\n", className); return JNI_FALSE; } if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { Log::error("RegisterNatives failed for '%s'\n", className); return JNI_FALSE; } return JNI_TRUE; } static int register_natives(JNIEnv *env) { const char* const class_path_name = "org/linaro/glmark2/Glmark2Native"; return register_native_methods(env, class_path_name, glmark2_native_methods, sizeof(glmark2_native_methods) / sizeof(glmark2_native_methods[0])); } /* * Returns the JNI version on success, -1 on failure. */ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { static_cast(reserved); JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_4) != JNI_OK) { Log::error("JNI_OnLoad: GetEnv failed\n"); goto bail; } assert(env != NULL); if (!register_natives(env)) { Log::error("JNI_OnLoad: glmark2 native registration failed\n"); goto bail; } /* success -- return valid version number */ result = JNI_VERSION_1_4; bail: return result; } glmark2-2012.08/./src/scene-loop.cpp0000664000175000017500000001322512013417376016260 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "shader-source.h" #include "util.h" static const std::string shader_file_base(GLMARK_DATA_PATH"/shaders/loop"); static const std::string vtx_file(shader_file_base + ".vert"); static const std::string frg_file(shader_file_base + ".frag"); static const std::string step_simple_file(shader_file_base + "-step-simple.all"); static const std::string step_loop_file(shader_file_base + "-step-loop.all"); SceneLoop::SceneLoop(Canvas &pCanvas) : SceneGrid(pCanvas, "loop") { options_["fragment-steps"] = Scene::Option("fragment-steps", "1", "The number of computational steps in the fragment shader"); options_["fragment-loop"] = Scene::Option("fragment-function", "true", "Whether to execute the steps in the vertex shader using a for loop", "false,true"); options_["vertex-steps"] = Scene::Option("vertex-steps", "1", "The number of computational steps in the vertex shader"); options_["vertex-loop"] = Scene::Option("vertex-function", "true", "Whether to execute the steps in the vertex shader using a for loop", "false,true"); options_["vertex-uniform"] = Scene::Option("vertex-uniform", "true", "Whether to use a uniform in the vertex shader for the number of loop iterations to perform (i.e. vertex-steps)", "false,true"); options_["fragment-uniform"] = Scene::Option("fragment-uniform", "true", "Whether to use a uniform in the fragment shader for the number of loop iterations to perform (i.e. fragment-steps)", "false,true"); } SceneLoop::~SceneLoop() { } static std::string get_fragment_shader_source(int steps, bool loop, bool uniform) { ShaderSource source(frg_file); ShaderSource source_main; if (loop) { source_main.append_file(step_loop_file); if (uniform) { source_main.replace("$NLOOPS$", "FragmentLoops"); } else { source_main.replace("$NLOOPS$", Util::toString(steps)); } } else { for (int i = 0; i < steps; i++) source_main.append_file(step_simple_file); } source.replace("$MAIN$", source_main.str()); return source.str(); } static std::string get_vertex_shader_source(int steps, bool loop, bool uniform) { ShaderSource source(vtx_file); ShaderSource source_main; if (loop) { source_main.append_file(step_loop_file); if (uniform) { source_main.replace("$NLOOPS$", "VertexLoops"); } else { source_main.replace("$NLOOPS$", Util::toString(steps)); } } else { for (int i = 0; i < steps; i++) source_main.append_file(step_simple_file); } source.replace("$MAIN$", source_main.str()); return source.str(); } bool SceneLoop::setup() { if (!SceneGrid::setup()) return false; /* Parse options */ bool vtx_loop = options_["vertex-loop"].value == "true"; bool frg_loop = options_["fragment-loop"].value == "true"; bool vtx_uniform = options_["vertex-uniform"].value == "true"; bool frg_uniform = options_["fragment-uniform"].value == "true"; int vtx_steps = Util::fromString(options_["vertex-steps"].value); int frg_steps = Util::fromString(options_["fragment-steps"].value); /* Load shaders */ std::string vtx_shader(get_vertex_shader_source(vtx_steps, vtx_loop, vtx_uniform)); std::string frg_shader(get_fragment_shader_source(frg_steps, frg_loop, frg_uniform)); if (!Scene::load_shaders_from_strings(program_, vtx_shader, frg_shader)) return false; program_.start(); program_["VertexLoops"] = vtx_steps; program_["FragmentLoops"] = frg_steps; std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } Scene::ValidationResult SceneLoop::validate() { static const double radius_3d(std::sqrt(3.0 * 15.0 * 15.0)); int frg_steps = Util::fromString(options_["fragment-steps"].value); Canvas::Pixel ref; if (frg_steps == 5) ref = Canvas::Pixel(0x5e, 0x5e, 0x5e, 0xff); else return Scene::ValidationUnknown; Canvas::Pixel pixel = canvas_.read_pixel(293, 89); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } glmark2-2012.08/./src/default-benchmarks.h0000664000175000017500000000667612013417376017434 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_DEFAULT_BENCHMARKS_H_ #define GLMARK2_DEFAULT_BENCHMARKS_H_ #include #include class DefaultBenchmarks { public: static const std::vector& get() { static std::vector default_benchmarks; if (default_benchmarks.empty()) populate(default_benchmarks); return default_benchmarks; } private: static void populate(std::vector& benchmarks) { benchmarks.push_back("build:use-vbo=false"); benchmarks.push_back("build:use-vbo=true"); benchmarks.push_back("texture:texture-filter=nearest"); benchmarks.push_back("texture:texture-filter=linear"); benchmarks.push_back("texture:texture-filter=mipmap"); benchmarks.push_back("shading:shading=gouraud"); benchmarks.push_back("shading:shading=blinn-phong-inf"); benchmarks.push_back("shading:shading=phong"); benchmarks.push_back("bump:bump-render=high-poly"); benchmarks.push_back("bump:bump-render=normals"); benchmarks.push_back("bump:bump-render=height"); benchmarks.push_back("effect2d:kernel=0,1,0;1,-4,1;0,1,0;"); benchmarks.push_back("effect2d:kernel=1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;"); benchmarks.push_back("pulsar:quads=5:texture=false:light=false"); benchmarks.push_back("desktop:windows=4:effect=blur:blur-radius=5:passes=1:separable=true"); benchmarks.push_back("desktop:windows=4:effect=shadow"); benchmarks.push_back("buffer:update-fraction=0.5:update-dispersion=0.9:columns=200:update-method=map:interleave=false"); benchmarks.push_back("buffer:update-fraction=0.5:update-dispersion=0.9:columns=200:update-method=subdata:interleave=false"); benchmarks.push_back("buffer:update-fraction=0.5:update-dispersion=0.9:columns=200:update-method=map:interleave=true"); benchmarks.push_back("ideas:speed=duration"); benchmarks.push_back("jellyfish"); benchmarks.push_back("terrain"); benchmarks.push_back("conditionals:vertex-steps=0:fragment-steps=0"); benchmarks.push_back("conditionals:vertex-steps=0:fragment-steps=5"); benchmarks.push_back("conditionals:vertex-steps=5:fragment-steps=0"); benchmarks.push_back("function:fragment-steps=5:fragment-complexity=low"); benchmarks.push_back("function:fragment-steps=5:fragment-complexity=medium"); benchmarks.push_back("loop:vertex-steps=5:fragment-steps=5:fragment-loop=false"); benchmarks.push_back("loop:vertex-steps=5:fragment-steps=5:fragment-uniform=false"); benchmarks.push_back("loop:vertex-steps=5:fragment-steps=5:fragment-uniform=true"); } }; #endif glmark2-2012.08/./src/scene-grid.cpp0000664000175000017500000000600512013417376016232 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "util.h" SceneGrid::SceneGrid(Canvas &pCanvas, const std::string &name) : Scene(pCanvas, name) { options_["grid-size"] = Scene::Option("grid-size", "32", "The number of squares per side of the grid (controls the number of vertices)"); options_["grid-length"] = Scene::Option("grid-length", "5.0", "The length of each side of the grid (normalized) (controls the area drawn to)"); } SceneGrid::~SceneGrid() { } bool SceneGrid::load() { rotationSpeed_ = 36.0f; running_ = false; return true; } void SceneGrid::unload() { } bool SceneGrid::setup() { if (!Scene::setup()) return false; int grid_size(Util::fromString(options_["grid-size"].value)); double grid_length(Util::fromString(options_["grid-length"].value)); /* Create and configure the grid mesh */ std::vector vertex_format; vertex_format.push_back(3); mesh_.set_vertex_format(vertex_format); /* * The spacing needed in order for the area of the requested grid * to be the same as the area of a grid with size 32 and spacing 0.02. */ double spacing = grid_length * (1 - 4.38 / 5.0) / (grid_size - 1.0); mesh_.make_grid(grid_size, grid_size, grid_length, grid_length, grid_size > 1 ? spacing : 0); mesh_.build_vbo(); currentFrame_ = 0; rotation_ = 0.0f; return true; } void SceneGrid::teardown() { program_.stop(); program_.release(); mesh_.reset(); Scene::teardown(); } void SceneGrid::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; rotation_ = rotationSpeed_ * elapsed_time; } void SceneGrid::draw() { // Load the ModelViewProjectionMatrix uniform in the shader LibMatrix::Stack4 model_view; LibMatrix::mat4 model_view_proj(canvas_.projection()); model_view.translate(0.0f, 0.0f, -5.0f); model_view.rotate(rotation_, 0.0f, 0.0f, 1.0f); model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; mesh_.render_vbo(); } Scene::ValidationResult SceneGrid::validate() { return Scene::ValidationUnknown; } glmark2-2012.08/./src/model.cpp0000664000175000017500000006045312013417376015321 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #include "mesh.h" #include "model.h" #include "vec.h" #include "log.h" #include "options.h" #include "util.h" #include "float.h" #include "math.h" #include #include #include using std::string; using std::vector; using LibMatrix::vec3; using LibMatrix::uvec3; #define read_or_fail(file, dst, size) do { \ file.read(reinterpret_cast((dst)), (size)); \ if (file.gcount() < (std::streamsize)(size)) { \ Log::error("%s: %d: Failed to read %zd bytes from 3ds file (read %zd)\n", \ __FUNCTION__, __LINE__, \ (size_t)(size), file.gcount()); \ return false; \ } \ } while(0); /** * Computes the bounding box for a Model::Object. * * @param object the Model object */ void Model::compute_bounding_box(const Object& object) { float minX(FLT_MAX); float maxX(FLT_MIN); float minY(FLT_MAX); float maxY(FLT_MIN); float minZ(FLT_MAX); float maxZ(FLT_MIN); for (vector::const_iterator vIt = object.vertices.begin(); vIt != object.vertices.end(); vIt++) { const vec3& curVtx = vIt->v; if (curVtx.x() < minX) { minX = curVtx.x(); } if (curVtx.x() > maxX) { maxX = curVtx.x(); } if (curVtx.y() < minY) { minY = curVtx.y(); } if (curVtx.y() > maxY) { maxY = curVtx.y(); } if (curVtx.z() < minZ) { minZ = curVtx.z(); } if (curVtx.z() > maxZ) { maxZ = curVtx.z(); } } maxVec_ = vec3(maxX, maxY, maxZ); minVec_ = vec3(minX, minY, minZ); } /** * Appends the vertices of a Model::Object to a Mesh. * * @param object the object to append * @param mesh the mesh to append to * @param p_pos the attribute position to use for the 'position' attribute * @param n_pos the attribute position to use for the 'normal' attribute * @param t_pos the attribute position to use for the 'texcoord' attribute */ void Model::append_object_to_mesh(const Object &object, Mesh &mesh, int p_pos, int n_pos, int t_pos, int nt_pos, int nb_pos) { size_t face_count = object.faces.size(); for(size_t i = 0; i < 3 * face_count; i += 3) { const Face &face = object.faces[i / 3]; const Vertex &a = object.vertices[face.a]; const Vertex &b = object.vertices[face.b]; const Vertex &c = object.vertices[face.c]; mesh.next_vertex(); if (p_pos >= 0) mesh.set_attrib(p_pos, a.v); if (n_pos >= 0) mesh.set_attrib(n_pos, a.n); if (t_pos >= 0) mesh.set_attrib(t_pos, a.t); if (nt_pos >= 0) mesh.set_attrib(nt_pos, a.nt); if (nb_pos >= 0) mesh.set_attrib(nb_pos, a.nb); mesh.next_vertex(); if (p_pos >= 0) mesh.set_attrib(p_pos, b.v); if (n_pos >= 0) mesh.set_attrib(n_pos, b.n); if (t_pos >= 0) mesh.set_attrib(t_pos, b.t); if (nt_pos >= 0) mesh.set_attrib(nt_pos, b.nt); if (nb_pos >= 0) mesh.set_attrib(nb_pos, b.nb); mesh.next_vertex(); if (p_pos >= 0) mesh.set_attrib(p_pos, c.v); if (n_pos >= 0) mesh.set_attrib(n_pos, c.n); if (t_pos >= 0) mesh.set_attrib(t_pos, c.t); if (nt_pos >= 0) mesh.set_attrib(nt_pos, c.nt); if (nb_pos >= 0) mesh.set_attrib(nb_pos, c.nb); } } /** * Converts a model to a mesh using the default attributes bindings. * * The default attributes and their order is: Position, Normal, Texcoord * * @param mesh the mesh to populate */ void Model::convert_to_mesh(Mesh &mesh) { std::vector > attribs; attribs.push_back(std::pair(AttribTypePosition, 3)); attribs.push_back(std::pair(AttribTypeNormal, 3)); attribs.push_back(std::pair(AttribTypeTexcoord, 2)); convert_to_mesh(mesh, attribs); } /** * Converts a model to a mesh using custom attribute bindings. * * The attribute bindings are pairs of . * * @param mesh the mesh to populate * @param attribs the attribute bindings to use */ void Model::convert_to_mesh(Mesh &mesh, const std::vector > &attribs) { std::vector format; int p_pos = -1; int n_pos = -1; int t_pos = -1; int nt_pos = -1; int nb_pos = -1; mesh.reset(); for (std::vector >::const_iterator ai = attribs.begin(); ai != attribs.end(); ai++) { format.push_back(ai->second); if (ai->first == AttribTypePosition) p_pos = ai - attribs.begin(); else if (ai->first == AttribTypeNormal) n_pos = ai - attribs.begin(); else if (ai->first == AttribTypeTexcoord) t_pos = ai - attribs.begin(); else if (ai->first == AttribTypeTangent) nt_pos = ai - attribs.begin(); else if (ai->first == AttribTypeBitangent) nb_pos = ai - attribs.begin(); } mesh.set_vertex_format(format); for (std::vector::const_iterator iter = objects_.begin(); iter != objects_.end(); iter++) { append_object_to_mesh(*iter, mesh, p_pos, n_pos, t_pos, nt_pos, nb_pos); } } void Model::calculate_texcoords() { // Since the model didn't come with texcoords, and we don't actually know // if it came with normals, either, we'll use positional spherical mapping // to generate texcoords for the model. See: // http://www.mvps.org/directx/articles/spheremap.htm for more details. vec3 centerVec = maxVec_ + minVec_; centerVec *= 0.5; for (std::vector::iterator iter = objects_.begin(); iter != objects_.end(); iter++) { Object &object = *iter; for (vector::iterator vertexIt = object.vertices.begin(); vertexIt != object.vertices.end(); vertexIt++) { Vertex& curVertex = *vertexIt; vec3 vnorm(curVertex.v - centerVec); vnorm.normalize(); curVertex.t.x(asinf(vnorm.x()) / M_PI + 0.5); curVertex.t.y(asinf(vnorm.y()) / M_PI + 0.5); } } } /** * Calculates the normal vectors of the model vertices. */ void Model::calculate_normals() { LibMatrix::vec3 n; for (std::vector::iterator iter = objects_.begin(); iter != objects_.end(); iter++) { Object &object = *iter; for (vector::const_iterator f_iter = object.faces.begin(); f_iter != object.faces.end(); f_iter++) { const Face &face = *f_iter; Vertex &a = object.vertices[face.a]; Vertex &b = object.vertices[face.b]; Vertex &c = object.vertices[face.c]; /* Calculate normal */ n = LibMatrix::vec3::cross(b.v - a.v, c.v - a.v); n.normalize(); a.n += n; b.n += n; c.n += n; LibMatrix::vec3 q1(b.v - a.v); LibMatrix::vec3 q2(c.v - a.v); LibMatrix::vec2 u1(b.t - a.t); LibMatrix::vec2 u2(c.t - a.t); float det = (u1.x() * u2.y() - u2.x() * u1.y()); /* Calculate tangent */ LibMatrix::vec3 nt; nt.x(det * (u2.y() * q1.x() - u1.y() * q2.x())); nt.y(det * (u2.y() * q1.y() - u1.y() * q2.y())); nt.z(det * (u2.y() * q1.z() - u1.y() * q2.z())); nt.normalize(); a.nt += nt; b.nt += nt; c.nt += nt; /* Calculate bitangent */ LibMatrix::vec3 nb; nb.x(det * (u1.x() * q2.x() - u2.x() * q1.x())); nb.y(det * (u1.x() * q2.y() - u2.x() * q1.y())); nb.z(det * (u1.x() * q2.z() - u2.x() * q1.z())); nb.normalize(); a.nb += nb; b.nb += nb; c.nb += nb; } for (vector::iterator v_iter = object.vertices.begin(); v_iter != object.vertices.end(); v_iter++) { Vertex &v = *v_iter; /* Orthogonalize */ v.nt = (v.nt - v.n * LibMatrix::vec3::dot(v.nt, v.n)); v.n.normalize(); v.nt.normalize(); v.nb.normalize(); } } } /** * Load a model from a 3DS file. * * @param filename the name of the file * * @return whether loading succeeded */ bool Model::load_3ds(const std::string &filename) { Object *object(0); Log::debug("Loading model from 3ds file '%s'\n", filename.c_str()); const std::auto_ptr input_file_ptr(Util::get_resource(filename)); std::istream& input_file(*input_file_ptr); if (!input_file) { Log::error("Could not open 3ds file '%s'\n", filename.c_str()); return false; } // Loop to scan the whole file while (!input_file.eof()) { uint16_t chunk_id; uint32_t chunk_length; // Read the chunk header input_file.read(reinterpret_cast(&chunk_id), 2); if (input_file.gcount() == 0) { continue; } else if (input_file.gcount() < 2) { Log::error("%s: %d: Failed to read %zd bytes from 3ds file (read %zd)\n", __FUNCTION__, __LINE__, 2, input_file.gcount()); return false; } //Read the lenght of the chunk read_or_fail(input_file, &chunk_length, 4); switch (chunk_id) { //----------------- MAIN3DS ----------------- // Description: Main chunk, contains all the other chunks // Chunk ID: 4d4d // Chunk Lenght: 0 + sub chunks //------------------------------------------- case 0x4d4d: break; //----------------- EDIT3DS ----------------- // Description: 3D Editor chunk, objects layout info // Chunk ID: 3d3d (hex) // Chunk Lenght: 0 + sub chunks //------------------------------------------- case 0x3d3d: break; //--------------- EDIT_OBJECT --------------- // Description: Object block, info for each object // Chunk ID: 4000 (hex) // Chunk Lenght: len(object name) + sub chunks //------------------------------------------- case 0x4000: { std::stringstream ss; unsigned char c = 1; for (int i = 0; i < 20 && c != '\0'; i++) { read_or_fail(input_file, &c, 1); ss << c; } objects_.push_back(Object(ss.str())); object = &objects_.back(); } break; //--------------- OBJ_TRIMESH --------------- // Description: Triangular mesh, contains chunks for 3d mesh info // Chunk ID: 4100 (hex) // Chunk Lenght: 0 + sub chunks //------------------------------------------- case 0x4100: break; //--------------- TRI_VERTEXL --------------- // Description: Vertices list // Chunk ID: 4110 (hex) // Chunk Lenght: 1 x unsigned short (number of vertices) // + 3 x float (vertex coordinates) x (number of vertices) // + sub chunks //------------------------------------------- case 0x4110: { uint16_t qty; read_or_fail(input_file, &qty, sizeof(uint16_t)); object->vertices.resize(qty); for (uint16_t i = 0; i < qty; i++) { float f[3]; read_or_fail(input_file, f, sizeof(float) * 3); object->vertices[i].v.x(f[0]); object->vertices[i].v.y(f[1]); object->vertices[i].v.z(f[2]); } } break; //--------------- TRI_FACEL1 ---------------- // Description: Polygons (faces) list // Chunk ID: 4120 (hex) // Chunk Lenght: 1 x unsigned short (number of polygons) // + 3 x unsigned short (polygon points) x (number of polygons) // + sub chunks //------------------------------------------- case 0x4120: { uint16_t qty; read_or_fail(input_file, &qty, sizeof(uint16_t)); object->faces.resize(qty); for (uint16_t i = 0; i < qty; i++) { read_or_fail(input_file, &object->faces[i].a, sizeof(uint16_t)); read_or_fail(input_file, &object->faces[i].b, sizeof(uint16_t)); read_or_fail(input_file, &object->faces[i].c, sizeof(uint16_t)); read_or_fail(input_file, &object->faces[i].face_flags, sizeof(uint16_t)); } } break; //------------- TRI_MAPPINGCOORS ------------ // Description: Vertices list // Chunk ID: 4140 (hex) // Chunk Lenght: 1 x unsigned short (number of mapping points) // + 2 x float (mapping coordinates) x (number of mapping points) // + sub chunks //------------------------------------------- case 0x4140: { uint16_t qty; read_or_fail(input_file, &qty, sizeof(uint16_t)); for (uint16_t i = 0; i < qty; i++) { float f[2]; read_or_fail(input_file, f, sizeof(float) * 2); object->vertices[i].t.x(f[0]); object->vertices[i].t.y(f[1]); } } gotTexcoords_ = true; break; //----------- Skip unknow chunks ------------ //We need to skip all the chunks that currently we don't use //We use the chunk lenght information to set the file pointer //to the same level next chunk //------------------------------------------- default: input_file.seekg(chunk_length - 6, std::ios::cur); } } // Compute bounding box for perspective projection compute_bounding_box(*object); if (Options::show_debug) { for (std::vector::const_iterator iter = objects_.begin(); iter != objects_.end(); iter++) { Log::debug(" Object name: %s Vertex count: %d Face count: %d\n", iter->name.c_str(), iter->vertices.size(), iter->faces.size()); } } return true; } /** * Parse vec3 values from an OBJ file. * * @param source the source line to parse * @param v the vec3 to populate */ static void obj_get_values(const string& source, vec3& v) { // Skip the definition type... string::size_type endPos = source.find(" "); string::size_type startPos(0); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } // Find the first value... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } string::size_type numChars(endPos - startPos); string xs(source, startPos, numChars); float x = Util::fromString(xs); // Then the second value... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } numChars = endPos - startPos; string ys(source, startPos, numChars); float y = Util::fromString(ys); // And the third value (there might be a fourth, but we don't care)... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { numChars = endPos; } else { numChars = endPos - startPos; } string zs(source, startPos, endPos - startPos); float z = Util::fromString(zs); v.x(x); v.y(y); v.z(z); } /** * Parse uvec3 values from an OBJ file. * * @param source the source line to parse * @param v the uvec3 to populate */ static void obj_get_values(const string& source, uvec3& v) { // Skip the definition type... string::size_type endPos = source.find(" "); string::size_type startPos(0); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } // Find the first value... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } string::size_type numChars(endPos - startPos); string xs(source, startPos, numChars); unsigned int x = Util::fromString(xs); // Then the second value... startPos = endPos+1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } numChars = endPos - startPos; string ys(source, startPos, numChars); unsigned int y = Util::fromString(ys); // And the third value (there might be a fourth, but we don't care)... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { numChars = endPos; } else { numChars = endPos - startPos; } string zs(source, startPos, numChars); unsigned int z = Util::fromString(zs); v.x(x); v.y(y); v.z(z); } /** * Load a model from an OBJ file. * * @param filename the name of the file * * @return whether loading succeeded */ bool Model::load_obj(const std::string &filename) { Log::debug("Loading model from obj file '%s'\n", filename.c_str()); const std::auto_ptr input_file_ptr(Util::get_resource(filename)); std::istream& inputFile(*input_file_ptr); if (!inputFile) { Log::error("Failed to open '%s'\n", filename.c_str()); return false; } vector sourceVec; string curLine; while (getline(inputFile, curLine)) { sourceVec.push_back(curLine); } // Give ourselves an object to populate. objects_.push_back(Object(filename)); Object& object(objects_.back()); static const string vertex_definition("v"); static const string normal_definition("vn"); static const string texcoord_definition("vt"); static const string face_definition("f"); for (vector::const_iterator lineIt = sourceVec.begin(); lineIt != sourceVec.end(); lineIt++) { const string& curSrc = *lineIt; // Is it a vertex attribute, a face description, comment or other? // We only care about the first two, we ignore comments, object names, // group names, smoothing groups, etc. string::size_type startPos(0); string::size_type spacePos = curSrc.find(" ", startPos); string definitionType(curSrc, startPos, spacePos - startPos); if (definitionType == vertex_definition) { Vertex v; obj_get_values(curSrc, v.v); object.vertices.push_back(v); } else if (definitionType == normal_definition) { // If we encounter an OBJ model with normals, we can update this // to update object.vertices.n directly Log::debug("We got a normal...\n"); } else if (definitionType == texcoord_definition) { // If we encounter an OBJ model with normals, we can update this // to update object.vertices.t directly Log::debug("We got a texcoord...\n"); } else if (definitionType == face_definition) { uvec3 v; obj_get_values(curSrc, v); Face f; // OBJ models index from '1'. f.a = v.x() - 1; f.b = v.y() - 1; f.c = v.z() - 1; object.faces.push_back(f); } } // Compute bounding box for perspective projection compute_bounding_box(object); Log::debug("Object populated with %u vertices and %u faces.\n", object.vertices.size(), object.faces.size()); return true; } namespace ModelPrivate { ModelMap modelMap; } /** * Locate all available models. * * This method scans the built-in data paths and build a database of usable * models available to scenes. Map is available on a read-only basis to scenes * that might find it useful for listing models, etc. * * @return a map containing information about the located models */ const ModelMap& Model::find_models() { if (!ModelPrivate::modelMap.empty()) { return ModelPrivate::modelMap; } vector pathVec; string dataDir(GLMARK_DATA_PATH"/models"); Util::list_files(dataDir, pathVec); #ifdef GLMARK_EXTRAS_PATH string extrasDir(GLMARK_EXTRAS_PATH"/models"); Util::list_files(extrasDir, pathVec); #endif // Now that we have a list of all of the model files available to us, // let's go through and pull out the names and what format they're in // so the scene can decide which ones to use. for(vector::const_iterator pathIt = pathVec.begin(); pathIt != pathVec.end(); pathIt++) { const string& curPath = *pathIt; string::size_type namePos(0); string::size_type slashPos = curPath.rfind("/"); if (slashPos != string::npos) { // Advance to the first character after the last slash namePos = slashPos + 1; } ModelFormat format(MODEL_INVALID); string::size_type extPos = curPath.rfind(".3ds"); if (extPos == string::npos) { // It's not a 3ds model extPos = curPath.rfind(".obj"); if (extPos == string::npos) { // It's not an obj model either, so skip it. continue; } format = MODEL_OBJ; } else { // It's a 3ds model format = MODEL_3DS; } string name(curPath, namePos, extPos - namePos); ModelDescriptor* desc = new ModelDescriptor(name, format, curPath); ModelPrivate::modelMap.insert(std::make_pair(name, desc)); } return ModelPrivate::modelMap; } /** * Load a model by name. * * You must initialize the available model collection using * Model::find_models() before using this method. * * @param modelName the model name * * @return whether the operation succeeded */ bool Model::load(const string& modelName) { bool retVal(false); ModelMap::const_iterator modelIt = ModelPrivate::modelMap.find(modelName); if (modelIt == ModelPrivate::modelMap.end()) { return retVal; } ModelDescriptor* desc = modelIt->second; switch (desc->format()) { case MODEL_INVALID: break; case MODEL_3DS: retVal = load_3ds(desc->pathname()); break; case MODEL_OBJ: retVal = load_obj(desc->pathname()); break; } return retVal; } glmark2-2012.08/./src/canvas-x11-glx.cpp0000664000175000017500000001755112013417376016674 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "canvas-x11-glx.h" #include "log.h" #include "options.h" #include #include static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT_; static PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA_; /********************* * Protected methods * *********************/ XVisualInfo * CanvasX11GLX::get_xvisualinfo() { if (!ensure_glx_fbconfig()) return 0; XVisualInfo *vis_info = glXGetVisualFromFBConfig(xdpy_, glx_fbconfig_ ); return vis_info; } bool CanvasX11GLX::make_current() { if (!ensure_glx_context()) return false; if (glx_context_ == glXGetCurrentContext()) return true; init_extensions(); if (!glXMakeCurrent(xdpy_, xwin_, glx_context_)) { Log::error("glXMakeCurrent failed\n"); return false; } if ((!glXSwapIntervalEXT_ || glXSwapIntervalEXT_(xdpy_, xwin_, 0)) && (!glXSwapIntervalMESA_ || glXSwapIntervalMESA_(0))) { Log::info("** Failed to set swap interval. Results may be bounded above by refresh rate.\n"); } return true; } void CanvasX11GLX::get_glvisualconfig(GLVisualConfig &visual_config) { if (!ensure_glx_fbconfig()) return; get_glvisualconfig_glx(glx_fbconfig_, visual_config); } /******************* * Private methods * *******************/ bool CanvasX11GLX::check_glx_version() { int glx_major, glx_minor; if (!glXQueryVersion(xdpy_, &glx_major, &glx_minor ) || (glx_major == 1 && glx_minor < 3) || glx_major < 1) { Log::error("GLX version >= 1.3 is required\n"); return false; } return true; } void CanvasX11GLX::init_extensions() { /* * Parse the extensions we care about from the extension string. * Don't even bother to get function pointers until we know the * extension is present. */ std::string extString; const char* exts = glXQueryExtensionsString(xdpy_, 0); if (exts) { extString = exts; } /* * GLX_EXT_swap_control or GL_MESA_swap_control. Note that * GLX_SGI_swap_control is not enough because it doesn't allow 0 as a valid * value (i.e. you can't turn off VSync). */ if (extString.find("GLX_EXT_swap_control") != std::string::npos) { glXSwapIntervalEXT_ = reinterpret_cast( glXGetProcAddress( reinterpret_cast("glXSwapIntervalEXT") ) ); } else if (extString.find("GLX_MESA_swap_control") != std::string::npos) { glXSwapIntervalMESA_ = reinterpret_cast( glXGetProcAddress( reinterpret_cast("glXSwapIntervalMESA") ) ); } if (!glXSwapIntervalEXT_ && !glXSwapIntervalMESA_) { Log::info("** GLX does not support GLX_EXT_swap_control or GLX_MESA_swap_control!\n"); } } bool CanvasX11GLX::ensure_glx_fbconfig() { static int attribs[] = { GLX_X_RENDERABLE, True, GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR, GLX_RED_SIZE, visual_config_.red, GLX_GREEN_SIZE, visual_config_.green, GLX_BLUE_SIZE, visual_config_.blue, GLX_ALPHA_SIZE, visual_config_.alpha, GLX_DEPTH_SIZE, visual_config_.depth, GLX_BUFFER_SIZE, visual_config_.buffer, GLX_DOUBLEBUFFER, True, None }; int num_configs; if (glx_fbconfig_) return true; if (!check_glx_version()) return false; GLXFBConfig *fbc = glXChooseFBConfig(xdpy_, DefaultScreen(xdpy_), attribs, &num_configs); if (!fbc) { Log::error("glXChooseFBConfig() failed\n"); return false; } std::vector configs(fbc, fbc + num_configs); Log::debug("Found %d matching FB configs.\n", num_configs); /* Select the best matching config */ glx_fbconfig_ = select_best_config(configs); XFree(fbc); if (Options::show_debug) { int buf, red, green, blue, alpha, depth, id, native_id; glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_FBCONFIG_ID, &id); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_VISUAL_ID, &native_id); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_BUFFER_SIZE, &buf); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_RED_SIZE, &red); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_GREEN_SIZE, &green); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_BLUE_SIZE, &blue); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_ALPHA_SIZE, &alpha); glXGetFBConfigAttrib(xdpy_, glx_fbconfig_, GLX_DEPTH_SIZE, &depth); Log::debug("GLX chosen config ID: 0x%x Native Visual ID: 0x%x\n" " Buffer: %d bits\n" " Red: %d bits\n" " Green: %d bits\n" " Blue: %d bits\n" " Alpha: %d bits\n" " Depth: %d bits\n", id, native_id, buf, red, green, blue, alpha, depth); } return true; } void CanvasX11GLX::init_gl_extensions() { GLExtensions::MapBuffer = glMapBuffer; GLExtensions::UnmapBuffer = glUnmapBuffer; } bool CanvasX11GLX::reset_context() { glXDestroyContext(xdpy_, glx_context_); glx_context_ = 0; return true; } bool CanvasX11GLX::ensure_glx_context() { if (glx_context_) return true; if (!ensure_glx_fbconfig()) return false; glx_context_ = glXCreateNewContext(xdpy_, glx_fbconfig_, GLX_RGBA_TYPE, 0, True); if (!glx_context_) { Log::error("glXCreateNewContext failed\n"); return false; } init_gl_extensions(); return true; } void CanvasX11GLX::get_glvisualconfig_glx(const GLXFBConfig config, GLVisualConfig &visual_config) { glXGetFBConfigAttrib(xdpy_, config, GLX_BUFFER_SIZE, &visual_config.buffer); glXGetFBConfigAttrib(xdpy_, config, GLX_RED_SIZE, &visual_config.red); glXGetFBConfigAttrib(xdpy_, config, GLX_GREEN_SIZE, &visual_config.green); glXGetFBConfigAttrib(xdpy_, config, GLX_BLUE_SIZE, &visual_config.blue); glXGetFBConfigAttrib(xdpy_, config, GLX_ALPHA_SIZE, &visual_config.alpha); glXGetFBConfigAttrib(xdpy_, config, GLX_DEPTH_SIZE, &visual_config.depth); } GLXFBConfig CanvasX11GLX::select_best_config(std::vector configs) { int best_score(INT_MIN); GLXFBConfig best_config(0); /* * Go through all the configs and choose the one with the best score, * i.e., the one better matching the requested config. */ for (std::vector::const_iterator iter = configs.begin(); iter != configs.end(); iter++) { const GLXFBConfig config(*iter); GLVisualConfig vc; int score; get_glvisualconfig_glx(config, vc); score = vc.match_score(visual_config_); if (score > best_score) { best_score = score; best_config = config; } } return best_config; } glmark2-2012.08/./src/gl-visual-config.h0000664000175000017500000000361412013417376017030 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #ifndef GLMARK2_GL_VISUAL_CONFIG_H_ #define GLMARK2_GL_VISUAL_CONFIG_H_ #include /** * Configuration parameters for a GL visual */ class GLVisualConfig { public: GLVisualConfig(): red(1), green(1), blue(1), alpha(1), depth(1), buffer(1) {} GLVisualConfig(int r, int g, int b, int a, int d, int buf): red(r), green(g), blue(b), alpha(a), depth(d), buffer(buf) {} GLVisualConfig(const std::string &s); /** * How well a GLVisualConfig matches another target config. * * The returned score has no meaning on its own. Its only purpose is * to allow comparison of how well different configs match a target * config, with a higher scores denoting a better match. * * Also note that this operation is not commutative: * a.match_score(b) != b.match_score(a) * * @return the match score */ int match_score(const GLVisualConfig &target) const; int red; int green; int blue; int alpha; int depth; int buffer; private: int score_component(int component, int target, int scale) const; }; #endif glmark2-2012.08/./src/scene.h0000664000175000017500000002773012013417376014764 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Marc Ordinas i Llopis, Collabora Ltd. (pulsar scene) * Jesse Barker (glmark2) */ #ifndef GLMARK2_SCENE_H_ #define GLMARK2_SCENE_H_ #include "gl-headers.h" #include "mesh.h" #include "vec.h" #include "program.h" #include #include #include #include #include #include "canvas.h" /** * A configurable scene used for creating benchmarks. */ class Scene { public: virtual ~Scene(); /** * Scene options. */ struct Option { Option(const std::string &nam, const std::string &val, const std::string &desc, const std::string &values = ""); Option() {} std::string name; std::string value; std::string default_value; std::string description; std::vector acceptable_values; bool set; }; /** * The result of a validation check. */ enum ValidationResult { ValidationFailure, ValidationSuccess, ValidationUnknown }; /** * Checks whether this scene (in its current configuration) is supported. * * @param show_errors whether to log errors about unsupported features * * @return whether the scene is supported */ virtual bool supported(bool show_errors); /** * Performs option-independent resource loading and configuration. * * It should be safe to call ::load() (and the corresponding ::unload()) * only once per program execution, although you may choose to do so more * times to better manage resource consumption. * * @return whether loading succeeded */ virtual bool load(); /** * Performs option-independent resource unloading. */ virtual void unload(); /** * Performs option-dependent resource loading and configuration. * * This method also prepares a scene for a benchmark run. * It should be called just before running a scene/benchmark. * * The base Scene::setup() method also checks whether a scene * configuration is supported by calling ::supported(true). * * @return whether setting the scene up succeeded */ virtual bool setup(); /** * Performs option-dependent resource unloading. * * This method should be called just after running a scene/benchmark. * * @return the operation status */ virtual void teardown(); /** * Updates the scene state. */ virtual void update(); /** * Draws the current scene state. */ virtual void draw(); /** * Gets an informational string describing the scene. * * @param title if specified, a custom title to use, instead of the default */ virtual std::string info_string(const std::string &title = ""); /** * Sets the value of an option for this scene. * * @param opt the option to set * @param val the value to set the option to * * @return whether the option value was set successfully */ virtual bool set_option(const std::string &opt, const std::string &val); /** * Validates the current output of this scene. * * This method should be called after having called ::draw() once. * * @return the validation result */ virtual ValidationResult validate() { return ValidationUnknown; } /** * Gets whether this scene is running. * * @return true if running, false otherwise */ bool running() { return running_; } /** * Sets whether this scene is running. * * @return true if running, false otherwise */ void running(bool r) { running_ = r; } /** * Gets the average FPS value for this scene. * * @return the average FPS value */ unsigned average_fps(); /** * Gets the name of the scene. * @return the name of the scene */ const std::string &name() { return name_; } /** * Resets all scene options to their default values. */ void reset_options(); /** * Sets the default value of a scene option. */ bool set_option_default(const std::string &opt, const std::string &val); /** * Gets the scene options. * * @return the scene options */ const std::map &options() { return options_; } /** * Gets a dummy scene object reference. * * @return the dummy Scene */ static Scene &dummy() { static Scene dummy_scene(Canvas::dummy(), ""); return dummy_scene; } /** * Loads a shader program from a pair of vertex and fragment shader strings. * * @return whether the operation succeeded */ static bool load_shaders_from_strings(Program &program, const std::string &vtx_shader, const std::string &frg_shader, const std::string &vtx_shader_filename = "None", const std::string &frg_shader_filename = "None"); protected: Scene(Canvas &pCanvas, const std::string &name); std::string construct_title(const std::string &title); Canvas &canvas_; std::string name_; std::map options_; double startTime_; double lastUpdateTime_; unsigned currentFrame_; bool running_; double duration_; // Duration of run in seconds }; /* * Special Scene used for setting the default options */ class SceneDefaultOptions : public Scene { public: SceneDefaultOptions(Canvas &pCanvas) : Scene(pCanvas, "") {} bool set_option(const std::string &opt, const std::string &val); bool setup(); private: std::list > defaultOptions_; }; class SceneBuild : public Scene { public: SceneBuild(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneBuild(); protected: Program program_; LibMatrix::mat4 perspective_; LibMatrix::vec3 centerVec_; float radius_; Mesh mesh_; bool orientModel_; float orientationAngle_; LibMatrix::vec3 orientationVec_; float rotation_; float rotationSpeed_; bool useVbo_; }; class SceneTexture : public Scene { public: SceneTexture(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneTexture(); protected: Program program_; Mesh mesh_; GLuint texture_; float radius_; bool orientModel_; float orientationAngle_; LibMatrix::vec3 orientationVec_; LibMatrix::mat4 perspective_; LibMatrix::vec3 centerVec_; LibMatrix::vec3 rotation_; LibMatrix::vec3 rotationSpeed_; }; class SceneShading : public Scene { public: SceneShading(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneShading(); protected: Program program_; float radius_; bool orientModel_; float orientationAngle_; LibMatrix::vec3 orientationVec_; LibMatrix::vec3 centerVec_; LibMatrix::mat4 perspective_; Mesh mesh_; float rotation_; float rotationSpeed_; }; class SceneGrid : public Scene { public: SceneGrid(Canvas &pCanvas, const std::string &name); virtual bool load(); virtual void unload(); virtual bool setup(); virtual void teardown(); virtual void update(); virtual void draw(); virtual ValidationResult validate(); ~SceneGrid(); protected: Program program_; Mesh mesh_; float rotation_; float rotationSpeed_; }; class SceneConditionals : public SceneGrid { public: SceneConditionals(Canvas &pCanvas); bool setup(); ValidationResult validate(); ~SceneConditionals(); }; class SceneFunction : public SceneGrid { public: SceneFunction(Canvas &pCanvas); bool setup(); ValidationResult validate(); ~SceneFunction(); }; class SceneLoop : public SceneGrid { public: SceneLoop(Canvas &pCanvas); bool setup(); ValidationResult validate(); ~SceneLoop(); }; class SceneBump : public Scene { public: SceneBump(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneBump(); protected: Program program_; Mesh mesh_; GLuint texture_; float rotation_; float rotationSpeed_; private: bool setup_model_plain(const std::string &type); bool setup_model_normals(); bool setup_model_normals_tangent(); bool setup_model_height(); }; class SceneEffect2D : public Scene { public: SceneEffect2D(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneEffect2D(); protected: Program program_; Mesh mesh_; GLuint texture_; }; class ScenePulsar : public Scene { public: ScenePulsar(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~ScenePulsar(); protected: int numQuads_; Program program_; Mesh mesh_; LibMatrix::vec3 scale_; std::vector rotations_; std::vector rotationSpeeds_; GLuint texture_; private: void create_and_setup_mesh(); }; struct SceneDesktopPrivate; class SceneDesktop : public Scene { public: SceneDesktop(Canvas &canvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneDesktop(); private: SceneDesktopPrivate *priv_; }; struct SceneBufferPrivate; class SceneBuffer : public Scene { public: SceneBuffer(Canvas &canvas); bool supported(bool show_errors); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneBuffer(); private: SceneBufferPrivate *priv_; }; class SceneIdeasPrivate; class SceneIdeas : public Scene { public: SceneIdeas(Canvas &pCanvas); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneIdeas(); private: SceneIdeasPrivate* priv_; }; class SceneTerrainPrivate; class SceneTerrain : public Scene { public: SceneTerrain(Canvas &pCanvas); bool supported(bool show_errors); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); ~SceneTerrain(); private: SceneTerrainPrivate* priv_; }; class JellyfishPrivate; class SceneJellyfish : public Scene { JellyfishPrivate* priv_; public: SceneJellyfish(Canvas &pCanvas); ~SceneJellyfish(); bool load(); void unload(); bool setup(); void teardown(); void update(); void draw(); ValidationResult validate(); }; #endif glmark2-2012.08/./src/main-loop.h0000664000175000017500000000654612013417376015564 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #ifndef GLMARK2_MAIN_LOOP_H_ #define GLMARK2_MAIN_LOOP_H_ #include "canvas.h" #include "benchmark.h" #include "text-renderer.h" #include "vec.h" #include /** * Main loop for benchmarking. */ class MainLoop { public: MainLoop(Canvas &canvas, const std::vector &benchmarks); virtual ~MainLoop() {} /** * Resets the main loop. * * You need to call reset() if the loop has finished and * you need to run it again. */ void reset(); /** * Gets the current total benchmarking score. */ unsigned int score(); /** * Perform the next main loop step. * * @returns whether the loop has finished */ bool step(); /** * Overridable method for drawing the canvas contents. */ virtual void draw(); /** * Overridable method for pre scene-setup customizations. */ virtual void before_scene_setup() {} /** * Overridable method for post scene-setup customizations. */ virtual void after_scene_setup() {} /** * Overridable method for logging scene info. */ virtual void log_scene_info(); /** * Overridable method for logging scene result. */ virtual void log_scene_result(); protected: enum SceneSetupStatus { SceneSetupStatusUnknown, SceneSetupStatusSuccess, SceneSetupStatusFailure, SceneSetupStatusUnsupported }; void next_benchmark(); Canvas &canvas_; Scene *scene_; const std::vector &benchmarks_; unsigned int score_; unsigned int benchmarks_run_; SceneSetupStatus scene_setup_status_; std::vector::const_iterator bench_iter_; }; /** * Main loop for benchmarking with decorations (eg FPS, demo) */ class MainLoopDecoration : public MainLoop { public: MainLoopDecoration(Canvas &canvas, const std::vector &benchmarks); virtual ~MainLoopDecoration(); virtual void draw(); virtual void before_scene_setup(); virtual void after_scene_setup(); protected: void fps_renderer_update_text(unsigned int fps); LibMatrix::vec2 vec2_from_pos_string(const std::string &s); bool show_fps_; bool show_title_; TextRenderer *fps_renderer_; TextRenderer *title_renderer_; unsigned int last_fps_; uint64_t fps_timestamp_; }; /** * Main loop for validation. */ class MainLoopValidation : public MainLoop { public: MainLoopValidation(Canvas &canvas, const std::vector &benchmarks); virtual void draw(); virtual void log_scene_result(); }; #endif /* GLMARK2_MAIN_LOOP_H_ */ glmark2-2012.08/./src/texture.h0000664000175000017500000000470112013417376015360 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_TEXTURE_H_ #define GLMARK2_TEXTURE_H_ #include "gl-headers.h" #include #include /** * A descriptor for a texture file. */ class TextureDescriptor { public: enum FileType { FileTypeUnknown, FileTypePNG, FileTypeJPEG, }; TextureDescriptor(const std::string& name, const std::string& pathname, FileType filetype) : name_(name), pathname_(pathname), filetype_(filetype) {} ~TextureDescriptor() {} const std::string& pathname() const { return pathname_; } FileType filetype() const { return filetype_; } private: std::string name_; std::string pathname_; FileType filetype_; TextureDescriptor(); }; typedef std::map TextureMap; class Texture { public: /** * Load a texture by name. * * You must initialize the available texture collection using * Texture::find_textures() before using this method. * * @name: the texture name * * @return: true if the operation succeeded, false otherwise */ static bool load(const std::string &name, GLuint *pTexture, ...); /** * Locate all available textures. * * This method scans the built-in data paths and builds a database of usable * textures available to scenes. Map is available on a read-only basis to * scenes that might find it useful for listing textures, etc. * * @return: a map containing information about the located textures */ static const TextureMap& find_textures(); }; #endif glmark2-2012.08/./src/texture.cpp0000664000175000017500000001406412013417376015716 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #include "texture.h" #include "log.h" #include "util.h" #include "image-reader.h" #include #include class ImageData { void resize(unsigned int w, unsigned int h, unsigned int b) { width = w; height = h; bpp = b; delete [] pixels; pixels = new unsigned char[bpp * width * height]; } public: ImageData() : pixels(0), width(0), height(0), bpp(0) {} ~ImageData() { delete [] pixels; } bool load(ImageReader &reader); unsigned char *pixels; unsigned int width; unsigned int height; unsigned int bpp; }; bool ImageData::load(ImageReader &reader) { if (reader.error()) return false; resize(reader.width(), reader.height(), reader.pixelBytes()); Log::debug(" Height: %d Width: %d Bpp: %d\n", width, height, bpp); /* * Copy the row data to the image buffer in reverse Y order, suitable * for texture upload. */ unsigned char *ptr = &pixels[bpp * width * (height - 1)]; while (reader.nextRow(ptr)) ptr -= bpp * width; return !reader.error(); } static void setup_texture(GLuint *tex, ImageData &image, GLint min_filter, GLint mag_filter) { GLenum format = image.bpp == 3 ? GL_RGB : GL_RGBA; glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, format, image.width, image.height, 0, format, GL_UNSIGNED_BYTE, image.pixels); if ((min_filter != GL_NEAREST && min_filter != GL_LINEAR) || (mag_filter != GL_NEAREST && mag_filter != GL_LINEAR)) { glGenerateMipmap(GL_TEXTURE_2D); } } namespace TexturePrivate { TextureMap textureMap; } bool Texture::load(const std::string &textureName, GLuint *pTexture, ...) { // Make sure the named texture is in the map. TextureMap::const_iterator textureIt = TexturePrivate::textureMap.find(textureName); if (textureIt == TexturePrivate::textureMap.end()) { return false; } // Pull the pathname out of the descriptor and use it for the PNG load. TextureDescriptor* desc = textureIt->second; const std::string& filename = desc->pathname(); ImageData image; if (desc->filetype() == TextureDescriptor::FileTypePNG) { PNGReader reader(filename); if (!image.load(reader)) return false; } else if (desc->filetype() == TextureDescriptor::FileTypeJPEG) { JPEGReader reader(filename); if (!image.load(reader)) return false; } va_list ap; va_start(ap, pTexture); GLint arg; while ((arg = va_arg(ap, GLint)) != 0) { GLint arg2 = va_arg(ap, GLint); setup_texture(pTexture, image, arg, arg2); pTexture++; } va_end(ap); return true; } const TextureMap& Texture::find_textures() { using std::vector; using std::string; if (!TexturePrivate::textureMap.empty()) { return TexturePrivate::textureMap; } vector pathVec; string dataDir(GLMARK_DATA_PATH"/textures"); Util::list_files(dataDir, pathVec); // Now that we have a list of all of the image files available to us, // let's go through and pull out the names and what format they're in // so the scene can decide which ones to use. for(vector::const_iterator pathIt = pathVec.begin(); pathIt != pathVec.end(); pathIt++) { const string& curPath = *pathIt; string::size_type namePos(0); string::size_type slashPos = curPath.rfind("/"); if (slashPos != string::npos) { // Advance to the first character after the last slash namePos = slashPos + 1; } // Find the position of the extension string::size_type pngExtPos = curPath.rfind(".png"); string::size_type jpgExtPos = curPath.rfind(".jpg"); string::size_type extPos(string::npos); // Select the extension that's closer to the end of the file name if (pngExtPos == string::npos) { extPos = jpgExtPos; } else if (jpgExtPos == string::npos) { extPos = pngExtPos; } else { extPos = std::max(pngExtPos, jpgExtPos); } if (extPos == string::npos) { // We can't trivially determine it's an image file so skip it... continue; } // Set the file type based on the extension TextureDescriptor::FileType type(TextureDescriptor::FileTypeUnknown); if (extPos == pngExtPos) { type = TextureDescriptor::FileTypePNG; } else if (extPos == jpgExtPos) { type = TextureDescriptor::FileTypeJPEG; } string name(curPath, namePos, extPos - namePos); TextureDescriptor* desc = new TextureDescriptor(name, curPath, type); TexturePrivate::textureMap.insert(std::make_pair(name, desc)); } return TexturePrivate::textureMap; } glmark2-2012.08/./src/main-loop.cpp0000664000175000017500000002116512013417376016111 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "options.h" #include "main-loop.h" #include "util.h" #include "log.h" #include #include /************ * MainLoop * ************/ MainLoop::MainLoop(Canvas &canvas, const std::vector &benchmarks) : canvas_(canvas), benchmarks_(benchmarks) { reset(); } void MainLoop::reset() { scene_ = 0; scene_setup_status_ = SceneSetupStatusUnknown; score_ = 0; benchmarks_run_ = 0; bench_iter_ = benchmarks_.begin(); } unsigned int MainLoop::score() { if (benchmarks_run_) return score_ / benchmarks_run_; else return score_; } bool MainLoop::step() { /* Find the next normal scene */ if (!scene_) { /* Find a normal scene */ while (bench_iter_ != benchmarks_.end()) { scene_ = &(*bench_iter_)->scene(); /* * Scenes with empty names are option-setting scenes. * Just set them up and continue with the search. */ if (scene_->name().empty()) (*bench_iter_)->setup_scene(); else break; next_benchmark(); } /* If we have found a valid scene, set it up */ if (bench_iter_ != benchmarks_.end()) { if (!Options::reuse_context) canvas_.reset(); before_scene_setup(); scene_ = &(*bench_iter_)->setup_scene(); if (!scene_->running()) { if (!scene_->supported(false)) scene_setup_status_ = SceneSetupStatusUnsupported; else scene_setup_status_ = SceneSetupStatusFailure; } else { scene_setup_status_ = SceneSetupStatusSuccess; } after_scene_setup(); log_scene_info(); } else { /* ... otherwise we are done */ return false; } } bool should_quit = canvas_.should_quit(); if (scene_ ->running() && !should_quit) draw(); /* * Need to recheck whether the scene is still running, because code * in draw() may have changed the state. */ if (!scene_->running() || should_quit) { if (scene_setup_status_ == SceneSetupStatusSuccess) { score_ += scene_->average_fps(); benchmarks_run_++; } log_scene_result(); (*bench_iter_)->teardown_scene(); scene_ = 0; next_benchmark(); } return !should_quit; } void MainLoop::draw() { canvas_.clear(); scene_->draw(); scene_->update(); canvas_.update(); } void MainLoop::log_scene_info() { Log::info("%s", scene_->info_string().c_str()); Log::flush(); } void MainLoop::log_scene_result() { static const std::string format_fps(Log::continuation_prefix + " FPS: %u FrameTime: %.3f ms\n"); static const std::string format_unsupported(Log::continuation_prefix + " Unsupported\n"); static const std::string format_fail(Log::continuation_prefix + " Set up failed\n"); if (scene_setup_status_ == SceneSetupStatusSuccess) { Log::info(format_fps.c_str(), scene_->average_fps(), 1000.0 / scene_->average_fps()); } else if (scene_setup_status_ == SceneSetupStatusUnsupported) { Log::info(format_unsupported.c_str()); } else { Log::info(format_fail.c_str()); } } void MainLoop::next_benchmark() { bench_iter_++; if (bench_iter_ == benchmarks_.end() && Options::run_forever) bench_iter_ = benchmarks_.begin(); } /********************** * MainLoopDecoration * **********************/ MainLoopDecoration::MainLoopDecoration(Canvas &canvas, const std::vector &benchmarks) : MainLoop(canvas, benchmarks), show_fps_(false), show_title_(false), fps_renderer_(0), title_renderer_(0), last_fps_(0) { } MainLoopDecoration::~MainLoopDecoration() { delete fps_renderer_; fps_renderer_ = 0; delete title_renderer_; title_renderer_ = 0; } void MainLoopDecoration::draw() { static const unsigned int fps_interval = 500000; canvas_.clear(); scene_->draw(); scene_->update(); if (show_fps_) { uint64_t now = Util::get_timestamp_us(); if (now - fps_timestamp_ >= fps_interval) { last_fps_ = scene_->average_fps(); fps_renderer_update_text(last_fps_); fps_timestamp_ = now; } fps_renderer_->render(); } if (show_title_) title_renderer_->render(); canvas_.update(); } void MainLoopDecoration::before_scene_setup() { delete fps_renderer_; fps_renderer_ = 0; delete title_renderer_; title_renderer_ = 0; } void MainLoopDecoration::after_scene_setup() { const Scene::Option &show_fps_option(scene_->options().find("show-fps")->second); const Scene::Option &title_option(scene_->options().find("title")->second); show_fps_ = show_fps_option.value == "true"; show_title_ = !title_option.value.empty(); if (show_fps_) { const Scene::Option &fps_pos_option(scene_->options().find("fps-pos")->second); const Scene::Option &fps_size_option(scene_->options().find("fps-size")->second); fps_renderer_ = new TextRenderer(canvas_); fps_renderer_->position(vec2_from_pos_string(fps_pos_option.value)); fps_renderer_->size(Util::fromString(fps_size_option.value)); fps_renderer_update_text(last_fps_); fps_timestamp_ = Util::get_timestamp_us(); } if (show_title_) { const Scene::Option &title_pos_option(scene_->options().find("title-pos")->second); const Scene::Option &title_size_option(scene_->options().find("title-size")->second); title_renderer_ = new TextRenderer(canvas_); title_renderer_->position(vec2_from_pos_string(title_pos_option.value)); title_renderer_->size(Util::fromString(title_size_option.value)); if (title_option.value == "#info#") title_renderer_->text(scene_->info_string()); else if (title_option.value == "#name#") title_renderer_->text(scene_->name()); else if (title_option.value == "#r2d2#") title_renderer_->text("Help me, Obi-Wan Kenobi. You're my only hope."); else title_renderer_->text(title_option.value); } } void MainLoopDecoration::fps_renderer_update_text(unsigned int fps) { std::stringstream ss; ss << "FPS: " << fps; fps_renderer_->text(ss.str()); } LibMatrix::vec2 MainLoopDecoration::vec2_from_pos_string(const std::string &s) { LibMatrix::vec2 v(0.0, 0.0); std::vector elems; Util::split(s, ',', elems, Util::SplitModeNormal); if (elems.size() > 0) v.x(Util::fromString(elems[0])); if (elems.size() > 1) v.y(Util::fromString(elems[1])); return v; } /********************** * MainLoopValidation * **********************/ MainLoopValidation::MainLoopValidation(Canvas &canvas, const std::vector &benchmarks) : MainLoop(canvas, benchmarks) { } void MainLoopValidation::draw() { /* Draw only the first frame of the scene and stop */ canvas_.clear(); scene_->draw(); canvas_.update(); scene_->running(false); } void MainLoopValidation::log_scene_result() { static const std::string format(Log::continuation_prefix + " Validation: %s\n"); std::string result; switch(scene_->validate()) { case Scene::ValidationSuccess: result = "Success"; break; case Scene::ValidationFailure: result = "Failure"; break; case Scene::ValidationUnknown: result = "Unknown"; break; default: break; } Log::info(format.c_str(), result.c_str()); } glmark2-2012.08/./src/scene-pulsar.cpp0000664000175000017500000002250112013417376016612 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) * Marc Ordinas i Llopis, Collabora Ltd. (pulsar scene) * Jesse Barker (glmark2) */ #include #include "scene.h" #include "mat.h" #include "stack.h" #include "vec.h" #include "log.h" #include "shader-source.h" #include "util.h" #include "texture.h" #include using LibMatrix::vec2; using LibMatrix::vec3; using LibMatrix::vec4; using LibMatrix::mat4; using LibMatrix::Stack4; ScenePulsar::ScenePulsar(Canvas &pCanvas) : Scene(pCanvas, "pulsar"), numQuads_(0), texture_(0) { options_["quads"] = Scene::Option("quads", "5", "Number of quads to render"); options_["texture"] = Scene::Option("texture", "false", "Enable texturing", "false,true"); options_["light"] = Scene::Option("light", "false", "Enable lighting", "false,true"); options_["random"] = Scene::Option("random", "false", "Enable random rotation speeds", "false,true"); } ScenePulsar::~ScenePulsar() { } bool ScenePulsar::load() { scale_ = vec3(1.0, 1.0, 1.0); running_ = false; return true; } void ScenePulsar::unload() { } bool ScenePulsar::setup() { if (!Scene::setup()) return false; // Disable back-face culling glDisable(GL_CULL_FACE); // Enable alpha blending glEnable(GL_BLEND); // Blend the colors normally, but don't change the destination alpha value. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE); // Create a rotation for each quad. numQuads_ = Util::fromString(options_["quads"].value); srand((unsigned)time(0)); for (int i = 0; i < numQuads_; i++) { rotations_.push_back(vec3()); if (options_["random"].value == "true") { rotationSpeeds_.push_back(vec3((static_cast(rand()) / static_cast(RAND_MAX)) * 5.0, (static_cast(rand()) / static_cast(RAND_MAX)) * 5.0, 0.0)); } else { float integral; float x_rot = std::modf((i + 1) * M_PI, &integral); float y_rot = std::modf((i + 1) * M_E, &integral); rotationSpeeds_.push_back(vec3(x_rot * 5.0, y_rot * 5.0, 0.0)); } } // Load shaders std::string vtx_shader_filename; std::string frg_shader_filename; static const vec4 lightPosition(-20.0f, 20.0f,-20.0f, 1.0f); if (options_["light"].value == "true") { vtx_shader_filename = GLMARK_DATA_PATH"/shaders/pulsar-light.vert"; } else { vtx_shader_filename = GLMARK_DATA_PATH"/shaders/pulsar.vert"; } if (options_["texture"].value == "true") { frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic-tex.frag"; Texture::find_textures(); if (!Texture::load("crate-base", &texture_, GL_NEAREST, GL_NEAREST, 0)) return false; } else { frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.frag"; } ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); if (options_["light"].value == "true") { // Load the light position constant vtx_source.add_const("LightSourcePosition", lightPosition); } if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } create_and_setup_mesh(); program_.start(); currentFrame_ = 0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void ScenePulsar::teardown() { program_.stop(); program_.release(); if (options_["texture"].value == "true") { glDeleteTextures(1, &texture_); texture_ = 0; } // Re-enable back-face culling glEnable(GL_CULL_FACE); // Disable alpha blending glDisable(GL_BLEND); mesh_.reset(); Scene::teardown(); } void ScenePulsar::update() { Scene::update(); double elapsed_time = lastUpdateTime_ - startTime_; for (int i = 0; i < numQuads_; i++) { rotations_[i] = rotationSpeeds_[i] * (elapsed_time * 60); } scale_ = vec3(cos(elapsed_time / 3.60) * 10.0, sin(elapsed_time / 3.60) * 10.0, 1.0); } void ScenePulsar::draw() { if (options_["texture"].value == "true") { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); } for (int i = 0; i < numQuads_; i++) { // Load the ModelViewProjectionMatrix uniform in the shader Stack4 model_view; mat4 model_view_proj(canvas_.projection()); model_view.scale(scale_.x(), scale_.y(), scale_.z()); model_view.translate(0.0f, 0.0f, -10.0f); model_view.rotate(rotations_[i].x(), 1.0f, 0.0f, 0.0f); model_view.rotate(rotations_[i].y(), 0.0f, 1.0f, 0.0f); model_view.rotate(rotations_[i].z(), 0.0f, 0.0f, 1.0f); model_view_proj *= model_view.getCurrent(); program_["ModelViewProjectionMatrix"] = model_view_proj; if (options_["light"].value == "true") { // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. mat4 normal_matrix(model_view.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; } mesh_.render_vbo(); } } Scene::ValidationResult ScenePulsar::validate() { static const double radius_3d(std::sqrt(3.0)); int quads = Util::fromString(options_["quads"].value); if (options_["texture"].value != "false" || options_["light"].value != "false" || quads != 5) { return Scene::ValidationUnknown; } Canvas::Pixel ref(0x77, 0x02, 0x77, 0xff); Canvas::Pixel pixel = canvas_.read_pixel(400, 299); double dist = pixel.distance_rgb(ref); if (dist < radius_3d + 0.01) { return Scene::ValidationSuccess; } else { Log::debug("Validation failed! Expected: 0x%x Actual: 0x%x Distance: %f\n", ref.to_le32(), pixel.to_le32(), dist); return Scene::ValidationFailure; } return Scene::ValidationUnknown; } void ScenePulsar::create_and_setup_mesh() { bool texture = options_["texture"].value == "true"; bool light = options_["light"].value == "true"; struct PlaneMeshVertex { vec3 position; vec4 color; vec2 texcoord; vec3 normal; }; PlaneMeshVertex plane_vertices[] = { { vec3(-1.0, -1.0, 0.0), vec4(1.0, 0.0, 0.0, 0.4), vec2(0.0, 0.0), vec3(0.0, 0.0, 1.0) }, { vec3(-1.0, 1.0, 0.0), vec4(0.0, 1.0, 0.0, 0.4), vec2(0.0, 1.0), vec3(0.0, 0.0, 1.0) }, { vec3(1.0, 1.0, 0.0), vec4(0.0, 0.0, 1.0, 0.4), vec2(1.0, 1.0), vec3(0.0, 0.0, 1.0) }, { vec3(1.0, -1.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), vec2(1.0, 0.0), vec3(0.0, 0.0, 1.0) } }; unsigned int vertex_index[] = {0, 1, 2, 0, 2, 3}; // Set vertex format std::vector vertex_format; vertex_format.push_back(3); // Position vertex_format.push_back(4); // Color if (texture) vertex_format.push_back(2); // Texcoord if (light) vertex_format.push_back(3); // Normal mesh_.set_vertex_format(vertex_format); // Build the plane mesh for (size_t i = 0; i < sizeof(vertex_index) / sizeof(*vertex_index); i++) { PlaneMeshVertex& vertex = plane_vertices[vertex_index[i]]; mesh_.next_vertex(); mesh_.set_attrib(0, vertex.position); mesh_.set_attrib(1, vertex.color); if (texture) mesh_.set_attrib(2, vertex.texcoord); if (light) mesh_.set_attrib(2 + static_cast(texture), vertex.normal); } mesh_.build_vbo(); // Set attribute locations std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["vtxcolor"].location()); if (texture) attrib_locations.push_back(program_["texcoord"].location()); if (light) attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); } glmark2-2012.08/./src/text-renderer.cpp0000664000175000017500000002030212013417376016776 0ustar alfalf00000000000000/* * Copyright © 2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #include "text-renderer.h" #include "gl-headers.h" #include "scene.h" #include "shader-source.h" #include "vec.h" #include "mat.h" #include "texture.h" using LibMatrix::vec2; using LibMatrix::mat4; /* These are specific to the glyph texture atlas we are using */ static const unsigned int texture_size(512); static const vec2 glyph_size_pixels(29.0, 57.0); static const vec2 glyph_size(glyph_size_pixels/texture_size); /****************** * Public methods * ******************/ /** * TextRenderer default constructor. */ TextRenderer::TextRenderer(Canvas& canvas) : canvas_(canvas), dirty_(false), position_(-1.0, -1.0), texture_(0) { size(0.03); glGenBuffers(2, vbo_); ShaderSource vtx_source(GLMARK_DATA_PATH"/shaders/text-renderer.vert"); ShaderSource frg_source(GLMARK_DATA_PATH"/shaders/text-renderer.frag"); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return; } GLint prev_program; glGetIntegerv(GL_CURRENT_PROGRAM, &prev_program); program_.start(); program_["Texture0"] = 0; glUseProgram(prev_program); /* Load the glyph texture atlas */ Texture::find_textures(); Texture::load("glyph-atlas", &texture_, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR,0); } TextRenderer::~TextRenderer() { glDeleteBuffers(2, vbo_); glDeleteTextures(1, &texture_); } /** * Sets the text string to render. * * @param t the text string */ void TextRenderer::text(const std::string& t) { if (text_ != t) { text_ = t; dirty_ = true; } } /** * Sets the screen position to render at. * * @param t the position */ void TextRenderer::position(const LibMatrix::vec2& p) { if (position_ != p) { position_ = p; dirty_ = true; } } /** * Sets the size of each rendered glyph. * * The size corresponds to the width of each glyph * in normalized screen coordinates. * * @param s the size of each glyph */ void TextRenderer::size(float s) { if (size_.x() != s) { /* Take into account the glyph and canvas aspect ratio */ double canvas_aspect = static_cast(canvas_.width()) / canvas_.height(); double glyph_aspect_rev = glyph_size.y() / glyph_size.x(); size_ = vec2(s, s * canvas_aspect * glyph_aspect_rev); dirty_ = true; } } /** * Renders the text. */ void TextRenderer::render() { /* Save state */ GLint prev_program = 0; GLint prev_array_buffer = 0; GLint prev_elem_array_buffer = 0; GLint prev_blend_src_rgb = 0; GLint prev_blend_dst_rgb = 0; GLint prev_blend_src_alpha = 0; GLint prev_blend_dst_alpha = 0; GLboolean prev_blend = GL_FALSE; GLboolean prev_depth_test = GL_FALSE; glGetIntegerv(GL_CURRENT_PROGRAM, &prev_program); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev_array_buffer); glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prev_elem_array_buffer); glGetIntegerv(GL_BLEND_SRC_RGB, &prev_blend_src_rgb); glGetIntegerv(GL_BLEND_DST_RGB, &prev_blend_dst_rgb); glGetIntegerv(GL_BLEND_SRC_ALPHA, &prev_blend_src_alpha); glGetIntegerv(GL_BLEND_DST_ALPHA, &prev_blend_dst_alpha); glGetBooleanv(GL_BLEND, &prev_blend); glGetBooleanv(GL_DEPTH_TEST, &prev_depth_test); /* Set new state */ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_); glBindBuffer(GL_ARRAY_BUFFER, vbo_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_[1]); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); if (dirty_) { create_geometry(); dirty_ = false; } program_.start(); GLint position_loc = program_["position"].location(); GLint texcoord_loc = program_["texcoord"].location(); /* Render */ glEnableVertexAttribArray(position_loc); glEnableVertexAttribArray(texcoord_loc); glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); glVertexAttribPointer(texcoord_loc, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), reinterpret_cast(2 * sizeof(float))); glDrawElements(GL_TRIANGLES, 6 * text_.length(), GL_UNSIGNED_SHORT, 0); glDisableVertexAttribArray(texcoord_loc); glDisableVertexAttribArray(position_loc); /* Restore state */ if (prev_depth_test == GL_TRUE) glEnable(GL_DEPTH_TEST); if (prev_blend == GL_FALSE) glDisable(GL_BLEND); glBlendFuncSeparate(prev_blend_src_rgb, prev_blend_dst_rgb, prev_blend_src_alpha, prev_blend_dst_alpha); glBindBuffer(GL_ARRAY_BUFFER, prev_array_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prev_elem_array_buffer); glUseProgram(prev_program); } /******************* * Private methods * *******************/ /** * Creates the geometry needed to render the text. * * This method assumes that the text VBOs are properly bound. */ void TextRenderer::create_geometry() { std::vector array; std::vector elem_array; vec2 pos(position_); for (size_t i = 0; i < text_.size(); i++) { vec2 texcoord = get_glyph_coords(text_[i]); /* Emit the elements for this glyph quad */ /* Lower left */ array.push_back(pos.x()); array.push_back(pos.y()); array.push_back(texcoord.x()); array.push_back(texcoord.y()); /* Lower right */ pos.x(pos.x() + size_.x()); texcoord.x(texcoord.x() + glyph_size.x()); array.push_back(pos.x()); array.push_back(pos.y()); array.push_back(texcoord.x()); array.push_back(texcoord.y()); /* Upper left */ pos.x(pos.x() - size_.x()); pos.y(pos.y() + size_.y()); texcoord.x(texcoord.x() - glyph_size.x()); texcoord.y(texcoord.y() + glyph_size.y()); array.push_back(pos.x()); array.push_back(pos.y()); array.push_back(texcoord.x()); array.push_back(texcoord.y()); /* Upper right */ pos.x(pos.x() + size_.x()); texcoord.x(texcoord.x() + glyph_size.x()); array.push_back(pos.x()); array.push_back(pos.y()); array.push_back(texcoord.x()); array.push_back(texcoord.y()); /* Prepare for the next glyph */ pos.y(pos.y() - size_.y()); /* Emit the element indices for this glyph quad */ elem_array.push_back(4 * i); elem_array.push_back(4 * i + 1); elem_array.push_back(4 * i + 2); elem_array.push_back(4 * i + 2); elem_array.push_back(4 * i + 1); elem_array.push_back(4 * i + 3); } /* Load the data into the corresponding VBOs */ glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(float), &array[0], GL_DYNAMIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elem_array.size() * sizeof(GLushort), &elem_array[0], GL_DYNAMIC_DRAW); } /** * Gets the texcoords of a glyph in the glyph texture atlas. * * @param c the character to get the glyph texcoords of * * @return the texcoords */ vec2 TextRenderer::get_glyph_coords(char c) { static const unsigned int glyphs_per_row(texture_size / glyph_size_pixels.x()); /* We only support the ASCII printable characters */ if (c < 32 || c >= 127) c = 32; int n = c - 32; int row = n / glyphs_per_row; int col = n % glyphs_per_row; return vec2(col * glyph_size.x(), 1.0 - (row + 1) * glyph_size.y()); } glmark2-2012.08/./src/scene-jellyfish.cpp0000664000175000017500000004647512013417376017315 0ustar alfalf00000000000000// // Copyright © 2012 Linaro Limited // // This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. // // glmark2 is free software: you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the Free Software // Foundation, either version 3 of the License, or (at your option) any later // version. // // glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // You should have received a copy of the GNU General Public License along with // glmark2. If not, see . // // Authors: // Aleksandar Rodic - Creator and WebGL implementation // Jesse Barker - glmark2 port // #include #include #include #include #include "scene.h" #include "scene-jellyfish.h" #include "log.h" #include "util.h" #include "texture.h" #include "shader-source.h" SceneJellyfish::SceneJellyfish(Canvas& canvas) : Scene(canvas, "jellyfish"), priv_(0) { } SceneJellyfish::~SceneJellyfish() { delete priv_; } bool SceneJellyfish::load() { running_ = false; return true; } void SceneJellyfish::unload() { } bool SceneJellyfish::setup() { if (!Scene::setup()) return false; // Set up our private object that does all of the lifting priv_ = new JellyfishPrivate(); if (!priv_->initialize()) return false; // Set core scene timing after actual initialization so we don't measure // set up time. startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; running_ = true; return true; } void SceneJellyfish::teardown() { priv_->cleanup(); Scene::teardown(); } void SceneJellyfish::update() { Scene::update(); priv_->update_viewport(LibMatrix::vec2(canvas_.width(), canvas_.height())); priv_->update_time(); } void SceneJellyfish::draw() { priv_->draw(); } Scene::ValidationResult SceneJellyfish::validate() { return Scene::ValidationUnknown; } // // JellyfishPrivate implementation // using LibMatrix::mat4; using LibMatrix::vec3; using LibMatrix::vec2; using std::string; using std::vector; bool GradientRenderer::init() { // Program set up static const string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/gradient.vert"); static const string frg_shader_filename(GLMARK_DATA_PATH"/shaders/gradient.frag"); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } positionLocation_ = program_["position"].location(); uvLocation_ = program_["uvIn"].location(); // Set up the position data for our "quad". vertices_.push_back(vec2(-1.0, -1.0)); vertices_.push_back(vec2(1.0, -1.0)); vertices_.push_back(vec2(-1.0, 1.0)); vertices_.push_back(vec2(1.0, 1.0)); uvs_.push_back(vec2(1.0, 1.0)); uvs_.push_back(vec2(1.0, 1.0)); uvs_.push_back(vec2(0.0, 0.0)); uvs_.push_back(vec2(0.0, 0.0)); uvOffset_ = vertices_.size() * sizeof(vec2); // Set up the VBO and stash our position data in it. glGenBuffers(1, &bufferObject_); glBindBuffer(GL_ARRAY_BUFFER, bufferObject_); glBufferData(GL_ARRAY_BUFFER, (vertices_.size() + uvs_.size()) * sizeof(vec2), 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, vertices_.size() * sizeof(vec2), &vertices_.front()); glBufferSubData(GL_ARRAY_BUFFER, uvOffset_, uvs_.size() * sizeof(vec2), &uvs_.front()); glBindBuffer(GL_ARRAY_BUFFER, 0); return true; } void GradientRenderer::cleanup() { program_.stop(); program_.release(); glBindBuffer(GL_ARRAY_BUFFER, 0); glDeleteBuffers(1, &bufferObject_); } void GradientRenderer::draw() { static const vec3 lightBlue(0.360784314, 0.584313725, 1.0); static const vec3 darkBlue(0.074509804, 0.156862745, 0.619607843); glBindBuffer(GL_ARRAY_BUFFER, bufferObject_); program_.start(); program_["color1"] = lightBlue; program_["color2"] = darkBlue; glEnableVertexAttribArray(positionLocation_); glEnableVertexAttribArray(uvLocation_); glVertexAttribPointer(positionLocation_, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(uvLocation_, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(uvOffset_)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(positionLocation_); glDisableVertexAttribArray(uvLocation_); program_.stop(); glBindBuffer(GL_ARRAY_BUFFER, 0); } //! // Parse index values from an OBJ file. // // @param source the source line to parse // @param idx the unsigned short to populate // static void obj_get_index(const string& source, unsigned short& idx) { // Skip the definition type... string::size_type endPos = source.find(" "); string::size_type startPos(0); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } // Find the first value... startPos = endPos + 1; string is(source, startPos); idx = Util::fromString(is); } //! // Parse vec3 values from an OBJ file. // // @param source the source line to parse // @param v the vec3 to populate // static void obj_get_values(const string& source, vec3& v) { // Skip the definition type... string::size_type endPos = source.find(" "); string::size_type startPos(0); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } // Find the first value... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } string::size_type numChars(endPos - startPos); string xs(source, startPos, numChars); float x = Util::fromString(xs); // Then the second value... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { Log::error("Bad element '%s'\n", source.c_str()); return; } numChars = endPos - startPos; string ys(source, startPos, numChars); float y = Util::fromString(ys); // And the third value (there might be a fourth, but we don't care)... startPos = endPos + 1; endPos = source.find(" ", startPos); if (endPos == string::npos) { numChars = endPos; } else { numChars = endPos - startPos; } string zs(source, startPos, endPos - startPos); float z = Util::fromString(zs); v.x(x); v.y(y); v.z(z); } // Custom OBJ loader. // // To support the jellyfish model, some amendments to the OBJ format are // necessary. In particular, a vertex color attribute is required, and // it contains an index list rather than a face list. bool JellyfishPrivate::load_obj(const std::string &filename) { Log::debug("Loading model from file '%s'\n", filename.c_str()); const std::auto_ptr input_file_ptr(Util::get_resource(filename)); std::istream& inputFile(*input_file_ptr); if (!inputFile) { Log::error("Failed to open '%s'\n", filename.c_str()); return false; } vector sourceVec; string curLine; while (getline(inputFile, curLine)) { sourceVec.push_back(curLine); } static const string vertex_definition("v"); static const string normal_definition("vn"); static const string texcoord_definition("vt"); static const string color_definition("vc"); static const string index_definition("i"); for (vector::const_iterator lineIt = sourceVec.begin(); lineIt != sourceVec.end(); lineIt++) { const string& curSrc = *lineIt; // Is it a vertex attribute, a face description, comment or other? // We only care about the first two, we ignore comments, object names, // group names, smoothing groups, etc. string::size_type startPos(0); string::size_type spacePos = curSrc.find(" ", startPos); string definitionType(curSrc, startPos, spacePos - startPos); if (definitionType == vertex_definition) { vec3 v; obj_get_values(curSrc, v); positions_.push_back(v); } else if (definitionType == normal_definition) { vec3 v; obj_get_values(curSrc, v); normals_.push_back(v); } else if (definitionType == color_definition) { vec3 v; obj_get_values(curSrc, v); colors_.push_back(v); } else if (definitionType == texcoord_definition) { vec3 v; obj_get_values(curSrc, v); texcoords_.push_back(v); } else if (definitionType == index_definition) { unsigned short idx(0); obj_get_index(curSrc, idx); indices_.push_back(idx); } } Log::debug("Object populated with %u vertices %u normals %u colors %u texcoords and %u indices.\n", positions_.size(), normals_.size(), colors_.size(), texcoords_.size(), indices_.size()); return true; } JellyfishPrivate::JellyfishPrivate() : positionLocation_(0), normalLocation_(0), colorLocation_(0), texcoordLocation_(0), viewport_(512.0, 512.0), lightPosition_(10.0, 40.0, -60.0), lightColor_(0.8, 1.3, 1.1, 1.0), lightRadius_(200.0), ambientColor_(0.3, 0.2, 1.0, 1.0), fresnelColor_(0.8, 0.7, 0.6, 1.1), fresnelPower_(1.0), rotation_(0.0), currentTime_(0.0), lastUpdateTime_(0.0), cullFace_(0), depthTest_(0), blend_(0), blendFuncSrc_(0), blendFuncDst_(0) { } JellyfishPrivate::~JellyfishPrivate() { positions_.clear(); normals_.clear(); colors_.clear(); texcoords_.clear(); indices_.clear(); } bool JellyfishPrivate::initialize() { static const string modelFilename(GLMARK_DATA_PATH"/models/jellyfish.jobj"); if (!load_obj(modelFilename)) { return false; } // Now that we've setup the vertex data, we can setup the map of how // that data will be laid out in the buffer object. static const unsigned int sv3(sizeof(vec3)); dataMap_.positionOffset = 0; dataMap_.positionSize = positions_.size() * sv3; dataMap_.totalSize = dataMap_.positionSize; dataMap_.normalOffset = dataMap_.positionOffset + dataMap_.positionSize; dataMap_.normalSize = normals_.size() * sv3; dataMap_.totalSize += dataMap_.normalSize; dataMap_.colorOffset = dataMap_.normalOffset + dataMap_.normalSize; dataMap_.colorSize = colors_.size() * sv3; dataMap_.totalSize += dataMap_.colorSize; dataMap_.texcoordOffset = dataMap_.colorOffset + dataMap_.colorSize; dataMap_.texcoordSize = texcoords_.size() * sv3; dataMap_.totalSize += dataMap_.texcoordSize; lastUpdateTime_ = Util::get_timestamp_us() / 1000.0; currentTime_ = static_cast(lastUpdateTime_) % 100000000 / 1000.0; whichCaustic_ = static_cast(currentTime_ * 30) % 32 + 1; rotation_ = 0.0; if (!gradient_.init()) { return false; } // Set up program first so we can store attribute and uniform locations // away for the using std::string; static const string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/jellyfish.vert"); static const string frg_shader_filename(GLMARK_DATA_PATH"/shaders/jellyfish.frag"); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } // Stash away attribute and uniform locations for handy use. positionLocation_ = program_["aVertexPosition"].location(); normalLocation_ = program_["aVertexNormal"].location(); colorLocation_ = program_["aVertexColor"].location(); texcoordLocation_ = program_["aTextureCoord"].location(); // We need 2 buffers for our work here. One for the vertex data. // and one for the index data. glGenBuffers(2, &bufferObjects_[0]); // First, setup the vertex data by binding the first buffer object, // allocating its data store, and filling it in with our vertex data. glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBufferData(GL_ARRAY_BUFFER, dataMap_.totalSize, 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.positionOffset, dataMap_.positionSize, &positions_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.normalOffset, dataMap_.normalSize, &normals_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.colorOffset, dataMap_.colorSize, &colors_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.texcoordOffset, dataMap_.texcoordSize, &texcoords_.front()); // Now repeat for our index data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_.size() * sizeof(unsigned short), &indices_.front(), GL_STATIC_DRAW); // "Unbind" our buffer objects to make sure the state is consistent. glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Finally, set up our textures. // // First, the main jellyfish texture bool gotTex = Texture::load("jellyfish256", &textureObjects_[0], GL_LINEAR, GL_LINEAR, 0); if (!gotTex || textureObjects_[0] == 0) { Log::error("Jellyfish texture set up failed!!!\n"); return false; } glBindTexture(GL_TEXTURE_2D, 0); // Then, the caustics textures static const string baseName("jellyfish-caustics-"); for (unsigned int i = 1; i < 33; i++) { std::stringstream ss; ss << std::setw(2) << std::setfill('0') << i; string curName(baseName); curName += ss.str(); gotTex = Texture::load(curName, &textureObjects_[i], GL_LINEAR, GL_LINEAR, 0); if (!gotTex || textureObjects_[i] == 0) { Log::error("Caustics texture[%u] set up failed!!!\n", i); return false; } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); } // Save the GL state we are changing so we can restore it later. cullFace_ = glIsEnabled(GL_CULL_FACE); depthTest_ = glIsEnabled(GL_DEPTH_TEST); blend_ = glIsEnabled(GL_BLEND); glGetIntegerv(GL_BLEND_SRC_RGB, &blendFuncSrc_); glGetIntegerv(GL_BLEND_DST_RGB, &blendFuncDst_); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); return true; } void JellyfishPrivate::update_viewport(const vec2& vp) { if (viewport_.x() == vp.x() && viewport_.y() == vp.y()) { return; } viewport_ = vp; projection_.loadIdentity(); projection_.perspective(30.0, viewport_.x()/viewport_.y(), 20.0, 120.0); } void JellyfishPrivate::update_time() { double now = Util::get_timestamp_us() / 1000.0; double elapsedTime = now - lastUpdateTime_; rotation_ += (2.0 * elapsedTime) / 1000.0; currentTime_ = static_cast(now) % 100000000 / 1000.0; whichCaustic_ = static_cast(currentTime_ * 30) % 32 + 1; lastUpdateTime_ = now; } void JellyfishPrivate::cleanup() { // Restore the GL state we changed for the scene. glBlendFunc(blendFuncSrc_, blendFuncDst_); if (GL_FALSE == blend_) { glDisable(GL_BLEND); } if (GL_TRUE == cullFace_) { glEnable(GL_CULL_FACE); } if (GL_TRUE == depthTest_) { glEnable(GL_DEPTH_TEST); } program_.stop(); program_.release(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDeleteTextures(33, &textureObjects_[0]); glDeleteBuffers(2, &bufferObjects_[0]); gradient_.cleanup(); } void JellyfishPrivate::draw() { // "Clear" the background to the desired gradient. gradient_.draw(); // We need "world", "world view projection", and "world inverse transpose" // matrix uniforms for the current shader. // // NOTE: Some of this seems a bit of a no-op (e.g., multipying by and // inverting identity matrices), but leave it like the original // WebGL files for the time being. Worth revisiting not doing all // of that math every draw call (might even be good to be doing // some of it in the shader as well). world_.push(); world_.translate(0.0, 5.0, -75.0); world_.rotate(sin(rotation_ / 10.0) * 30.0, 0.0, 1.0, 0.0); world_.rotate(sin(rotation_ / 20.0) * 30.0, 1.0, 0.0, 0.0); world_.scale(5.0, 5.0, 5.0); world_.translate(0.0, sin(rotation_ / 10.0) * 2.5, 0.0); mat4 worldViewProjection(projection_.getCurrent()); worldViewProjection *= world_.getCurrent();; mat4 worldInverseTranspose(world_.getCurrent()); worldInverseTranspose.inverse().transpose(); // Load up the uniforms program_.start(); program_["uWorld"] = world_.getCurrent(); program_["uWorldViewProj"] = worldViewProjection; program_["uWorldInvTranspose"] = worldInverseTranspose; program_["uCurrentTime"] = currentTime_; // Revisit making these constants rather than uniforms as they appear never // to change program_["uLightPos"] = lightPosition_; program_["uLightRadius"] = lightRadius_; program_["uLightCol"] = lightColor_; program_["uAmbientCol"] = ambientColor_; program_["uFresnelCol"] = fresnelColor_; program_["uFresnelPower"] = fresnelPower_; // Set up textures for this frame. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureObjects_[0]); program_["uSampler"] = 0; glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textureObjects_[whichCaustic_]); program_["uSampler1"] = 1; glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glEnableVertexAttribArray(positionLocation_); glEnableVertexAttribArray(normalLocation_); glEnableVertexAttribArray(colorLocation_); glEnableVertexAttribArray(texcoordLocation_); glVertexAttribPointer(positionLocation_ , 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.positionOffset)); glVertexAttribPointer(normalLocation_ , 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.normalOffset)); glVertexAttribPointer(colorLocation_ , 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.colorOffset)); glVertexAttribPointer(texcoordLocation_ , 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.texcoordOffset)); glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_SHORT, 0); glDisableVertexAttribArray(positionLocation_); glDisableVertexAttribArray(normalLocation_); glDisableVertexAttribArray(colorLocation_); glDisableVertexAttribArray(texcoordLocation_); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); program_.stop(); world_.pop(); } glmark2-2012.08/./src/scene-ideas.cpp0000664000175000017500000002706512013417376016403 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "scene.h" #include "stack.h" #include "splines.h" #include "table.h" #include "logo.h" #include "lamp.h" #include "util.h" #include "log.h" #include using LibMatrix::Stack4; using LibMatrix::mat4; using LibMatrix::vec3; using LibMatrix::vec4; using LibMatrix::uvec3; using std::string; using std::map; class SceneIdeasPrivate { public: SceneIdeasPrivate() : valid_(false), currentSpeed_(1.0), // Real time. currentTime_(START_TIME_), timeOffset_(START_TIME_) { startTime_.tv_sec = 0; startTime_.tv_usec = 0; } ~SceneIdeasPrivate() { } void initialize(map& options); void reset_time(); void update_time(); void update_projection(const mat4& proj); void draw(); bool valid() { return valid_; } private: void postIdle(); void initLights(); bool valid_; Stack4 projection_; Stack4 modelview_; float currentSpeed_; float currentTime_; float timeOffset_; struct timeval startTime_; static const float CYCLE_TIME_; static const float TIME_; static const float START_TIME_; // Table Table table_; // Logo SGILogo logo_; // Lamp Lamp lamp_; // Light constants static const vec4 light0_position_; static const vec4 light1_position_; static const vec4 light2_position_; // Object constants ViewFromSpline viewFromSpline_; ViewToSpline viewToSpline_; LightPositionSpline lightPosSpline_; LogoPositionSpline logoPosSpline_; LogoRotationSpline logoRotSpline_; vec3 viewFrom_; vec3 viewTo_; vec3 lightPos_; vec3 logoPos_; vec3 logoRot_; vec4 lightPositions_[3]; }; const float SceneIdeasPrivate::TIME_(15.0); const float SceneIdeasPrivate::CYCLE_TIME_(TIME_ * 1.0 - 3.0); const float SceneIdeasPrivate::START_TIME_(0.6); const vec4 SceneIdeasPrivate::light0_position_(0.0, 1.0, 0.0, 0.0); const vec4 SceneIdeasPrivate::light1_position_(-1.0, 0.0, 0.0, 0.0); const vec4 SceneIdeasPrivate::light2_position_(0.0, -1.0, 0.0, 0.0); void SceneIdeasPrivate::initLights() { const mat4& curMV(modelview_.getCurrent()); lightPositions_[0] = curMV * light0_position_; lightPositions_[1] = curMV * light1_position_; lightPositions_[2] = curMV * light2_position_; } void SceneIdeasPrivate::initialize(map& options) { // Initialize the positions for the lights we'll use. initLights(); // Tell the objects in the scene to initialize themselves. table_.init(); if (!table_.valid()) { Log::debug("SceneIdeas: table object not properly initialized!\n"); return; } logo_.init(); if (!logo_.valid()) { Log::debug("SceneIdeas: logo object not properly initialized!\n"); return; } lamp_.init(); if (!lamp_.valid()) { Log::debug("SceneIdeas: lamp object not properly initialized!\n"); return; } reset_time(); // If the option string tells us the user wants the speed to be a function // of the scene duration, do it. Otherwise, take the value explicitly. static const string durationLabel("duration"); static const string speedLabel("speed"); if (options[speedLabel].value == durationLabel) { float duration = Util::fromString(options[durationLabel].value); currentSpeed_ = (CYCLE_TIME_ - START_TIME_) / duration; } else { currentSpeed_ = Util::fromString(options[speedLabel].value); } // If we're here, we're okay to run. valid_ = true; } void SceneIdeasPrivate::reset_time() { timeOffset_ = START_TIME_; gettimeofday(&startTime_, NULL); } void SceneIdeasPrivate::update_time() { // Compute new time struct timeval current = {0, 0}; gettimeofday(¤t, NULL); float timediff = (current.tv_sec - startTime_.tv_sec) + static_cast(current.tv_usec - startTime_.tv_usec) / 1000000.0; float sceneTime = timediff * currentSpeed_ + timeOffset_; // Keep the current time in [START_TIME_..CYCLE_TIME_) // Every other cycle starting with 0 start at the beginning and goes // forward in time. Other cycles start at the end and go backwards. currentTime_ = std::fmod(sceneTime, CYCLE_TIME_); unsigned int cycle = sceneTime/CYCLE_TIME_; if (cycle % 2) { currentTime_ = CYCLE_TIME_ - currentTime_; } } void SceneIdeasPrivate::update_projection(const mat4& proj) { // Projection hasn't changed since last frame. if (projection_.getCurrent() == proj) { return; } projection_.loadIdentity(); projection_ *= proj; } SceneIdeas::SceneIdeas(Canvas& canvas) : Scene(canvas, "ideas"), priv_(0) { options_["speed"] = Scene::Option("speed", "duration", "Time coefficient (1.0 is \"wall clock\" speed, <1.0 is slower, >1.0 is faster). A special value of \"duration\" computes this as a function of the \"duration\" option"); } SceneIdeas::~SceneIdeas() { delete priv_; } bool SceneIdeas::load() { running_ = false; return true; } void SceneIdeas::unload() { } bool SceneIdeas::setup() { if (!Scene::setup()) return false; priv_ = new SceneIdeasPrivate(); priv_->initialize(options_); if (!priv_->valid()) return false; priv_->update_projection(canvas_.projection()); // Core Scene state currentFrame_ = 0; running_ = true; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return true; } void SceneIdeas::update() { Scene::update(); priv_->update_time(); priv_->update_projection(canvas_.projection()); } void SceneIdeasPrivate::draw() { viewFromSpline_.getCurrentVec(currentTime_, viewFrom_); viewToSpline_.getCurrentVec(currentTime_, viewTo_); lightPosSpline_.getCurrentVec(currentTime_, lightPos_); logoPosSpline_.getCurrentVec(currentTime_, logoPos_); logoRotSpline_.getCurrentVec(currentTime_, logoRot_); // Tell the logo its new position logo_.setPosition(logoPos_); vec4 lp4(lightPos_.x(), lightPos_.y(), lightPos_.z(), 0.0); // // SHADOW // modelview_.loadIdentity(); modelview_.lookAt(viewFrom_.x(), viewFrom_.y(), viewFrom_.z(), viewTo_.x(), viewTo_.y(), viewTo_.z(), 0.0, 1.0, 0.0); float pca(0.0); if (viewFrom_.y() > 0.0) { table_.draw(modelview_, projection_, lightPos_, logoPos_, currentTime_, pca); } glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); if (logoPos_.y() < 0.0) { // Set the color assuming we're still under the table. uvec3 flatColor(128 / 2, 102 / 2, 179 / 2); if (logoPos_.y() > -0.33) { // We're emerging from the table float c(1.0 - logoPos_.y() / -0.33); pca /= 4.0; flatColor.x(static_cast(128.0 * (1.0 - c) * 0.5 + 255.0 * pca * c)); flatColor.y(static_cast(102.0 * (1.0 - c) * 0.5 + 255.0 * pca * c)); flatColor.z(static_cast(179.0 * (1.0 - c) * 0.5 + 200.0 * pca * c)); } modelview_.push(); modelview_.scale(0.04, 0.0, 0.04); modelview_.rotate(-90.0, 1.0, 0.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.z()), 0.0, 0.0, 1.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.y()), 0.0, 1.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.x()), 1.0, 0.0, 0.0); modelview_.rotate(0.1 * 353, 1.0, 0.0, 0.0); modelview_.rotate(0.1 * 450, 0.0, 1.0, 0.0); logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_FLAT, flatColor); modelview_.pop(); } if (logoPos_.y() > 0.0) { modelview_.push(); modelview_.translate(lightPos_.x(), lightPos_.y(), lightPos_.z()); mat4 tv; tv[3][1] = -1.0; tv[3][3] = 0.0; tv[0][0] = tv[1][1] = tv[2][2] = lightPos_.y(); modelview_ *= tv; modelview_.translate(-lightPos_.x() + logoPos_.x(), -lightPos_.y() + logoPos_.y(), -lightPos_.z() + logoPos_.z()); modelview_.scale(0.04, 0.04, 0.04); modelview_.rotate(-90.0, 1.0, 0.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.z()), 0.0, 0.0, 1.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.y()), 0.0, 1.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.x()), 1.0, 0.0, 0.0); modelview_.rotate(35.3, 1.0, 0.0, 0.0); modelview_.rotate(45.0, 0.0, 1.0, 0.0); logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_SHADOW); modelview_.pop(); } // // DONE SHADOW // glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); modelview_.loadIdentity(); modelview_.lookAt(viewFrom_.x(), viewFrom_.y(), viewFrom_.z(), viewTo_.x(), viewTo_.y(), viewTo_.z(), 0.0, 1.0, 0.0); modelview_.push(); modelview_.translate(lightPos_.x(), lightPos_.y(), lightPos_.z()); modelview_.scale(0.1, 0.1, 0.1); float x(lightPos_.x() - logoPos_.x()); float y(lightPos_.y() - logoPos_.y()); float z(lightPos_.z() - logoPos_.z()); double a3(0.0); if (x != 0.0) { a3 = -atan2(z, x) * 10.0 * 180.0 / M_PI; } double a4(-atan2(sqrt(x * x + z * z), y) * 10.0 * 180.0 / M_PI); modelview_.rotate(0.1 * static_cast(a3), 0.0, 1.0, 0.0); modelview_.rotate(0.1 * static_cast(a4), 0.0, 0.0, 1.0); modelview_.rotate(-90.0, 1.0, 0.0, 0.0); lamp_.draw(modelview_, projection_, lightPositions_); modelview_.pop(); lightPositions_[0] = modelview_.getCurrent() * lp4; if (logoPos_.y() > -0.33) { modelview_.push(); modelview_.translate(logoPos_.x(), logoPos_.y(), logoPos_.z()); modelview_.scale(0.04, 0.04, 0.04); modelview_.rotate(-90.0, 1.0, 0.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.z()), 0.0, 0.0, 1.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.y()), 0.0, 1.0, 0.0); modelview_.rotate(0.1 * static_cast(10.0 * logoRot_.x()), 1.0, 0.0, 0.0); modelview_.rotate(35.3, 1.0, 0.0, 0.0); modelview_.rotate(45.0, 0.0, 1.0, 0.0); logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_NORMAL); modelview_.pop(); } if (viewFrom_.y() < 0.0) { table_.drawUnder(modelview_, projection_); } } void SceneIdeas::draw() { priv_->draw(); } Scene::ValidationResult SceneIdeas::validate() { return Scene::ValidationUnknown; } void SceneIdeas::teardown() { delete priv_; priv_ = 0; Scene::teardown(); } glmark2-2012.08/./src/wscript_build0000664000175000017500000000340212013417376016301 0ustar alfalf00000000000000all_sources = bld.path.ant_glob('*.cpp scene-ideas/*.cc scene-terrain/*.cpp') common_sources = [f for f in all_sources if f.name.find('canvas-') == -1 and f.name.find('android') == -1 ] gl_sources = ['canvas-x11.cpp', 'canvas-x11-glx.cpp'] glesv2_sources = ['canvas-x11.cpp', 'canvas-x11-egl.cpp'] libmatrix_sources = [f for f in bld.path.ant_glob('libmatrix/*.cc') if not f.name.endswith('test.cc')] includes = ['.', 'scene-ideas', 'scene-terrain'] if bld.env.USE_GL: bld( features = ['cxx', 'cxxstlib'], source = libmatrix_sources, target = 'matrix', lib = ['m'], includes = ['.'], export_includes = 'libmatrix', defines = ['USE_GL', 'USE_EXCEPTIONS'] ) bld( features = ['cxx', 'cprogram'], source = common_sources + gl_sources, target = 'glmark2', use = ['x11', 'gl', 'matrix', 'libpng12'], lib = ['m', 'jpeg'], includes = includes, defines = ['USE_GL', 'USE_EXCEPTIONS'] ) if bld.env.USE_GLESv2: bld( features = ['cxx', 'cxxstlib'], source = libmatrix_sources, target = 'matrix-es2', lib = ['m'], includes = ['.'], export_includes = 'libmatrix', defines = ['USE_GLESv2', 'USE_EXCEPTIONS'] ) bld( features = ['cxx', 'cprogram'], source = common_sources + glesv2_sources, target = 'glmark2-es2', use = ['x11', 'egl', 'glesv2', 'matrix-es2', 'libpng12'], lib = ['m', 'dl', 'jpeg'], includes = includes, defines = ['USE_GLESv2', 'USE_EXCEPTIONS'] ) glmark2-2012.08/./src/canvas-x11-egl.h0000664000175000017500000000361412013417376016311 0ustar alfalf00000000000000/* * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis (glmark2) */ #ifndef GLMARK2_CANVAS_X11_EGL_H_ #define GLMARK2_CANVAS_X11_EGL_H_ #include "canvas-x11.h" #include #include /** * Canvas for rendering to an X11 window using EGL. */ class CanvasX11EGL : public CanvasX11 { public: CanvasX11EGL(int width, int height) : CanvasX11(width, height), egl_display_(EGL_NO_DISPLAY), egl_surface_(EGL_NO_SURFACE), egl_config_(0), egl_context_(EGL_NO_CONTEXT) {} ~CanvasX11EGL() {} protected: XVisualInfo *get_xvisualinfo(); bool make_current(); bool reset_context(); void swap_buffers() { eglSwapBuffers(egl_display_, egl_surface_); } void get_glvisualconfig(GLVisualConfig &visual_config); private: bool ensure_egl_display(); bool ensure_egl_config(); bool ensure_egl_context(); bool ensure_egl_surface(); void init_gl_extensions(); void get_glvisualconfig_egl(EGLConfig config, GLVisualConfig &visual_config); EGLConfig select_best_config(std::vector configs); EGLDisplay egl_display_; EGLSurface egl_surface_; EGLConfig egl_config_; EGLContext egl_context_; }; #endif glmark2-2012.08/./src/scene.cpp0000664000175000017500000001737412013417376015322 0ustar alfalf00000000000000/* * Copyright © 2008 Ben Smith * Copyright © 2010-2011 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) */ #include "scene.h" #include "log.h" #include "shader-source.h" #include "options.h" #include "util.h" #include #include #include using std::stringstream; using std::string; using std::map; Scene::Option::Option(const std::string &nam, const std::string &val, const std::string &desc, const std::string &values) : name(nam), value(val), default_value(val), description(desc), set(false) { Util::split(values, ',', acceptable_values, Util::SplitModeNormal); } Scene::Scene(Canvas &pCanvas, const string &name) : canvas_(pCanvas), name_(name), startTime_(0), lastUpdateTime_(0), currentFrame_(0), running_(0), duration_(0) { options_["duration"] = Scene::Option("duration", "10.0", "The duration of each benchmark in seconds"); options_["vertex-precision"] = Scene::Option("vertex-precision", "default,default,default,default", "The precision values for the vertex shader (\"int,float,sampler2d,samplercube\")"); options_["fragment-precision"] = Scene::Option("fragment-precision", "default,default,default,default", "The precision values for the fragment shader (\"int,float,sampler2d,samplercube\")"); /* FPS options */ options_["show-fps"] = Scene::Option("show-fps", "false", "Show live FPS counter", "false,true"); options_["fps-pos"] = Scene::Option("fps-pos", "-1.0,-1.0", "The position on screen where to show FPS"); options_["fps-size"] = Scene::Option("fps-size", "0.03", "The width of each glyph for the FPS"); /* Title options */ options_["title"] = Scene::Option("title", "", "The scene title to show"); options_["title-pos"] = Scene::Option("title-pos", "-0.7,-1.0", "The position on screen where to show the title"); options_["title-size"] = Scene::Option("title-size", "0.03", "The width of each glyph in the title"); } Scene::~Scene() { } bool Scene::supported(bool show_errors) { static_cast(show_errors); return true; } bool Scene::load() { return true; } void Scene::unload() { } bool Scene::setup() { duration_ = Util::fromString(options_["duration"].value); ShaderSource::default_precision( ShaderSource::Precision(options_["vertex-precision"].value), ShaderSource::ShaderTypeVertex ); ShaderSource::default_precision( ShaderSource::Precision(options_["fragment-precision"].value), ShaderSource::ShaderTypeFragment ); currentFrame_ = 0; running_ = false; startTime_ = Util::get_timestamp_us() / 1000000.0; lastUpdateTime_ = startTime_; return supported(true); } void Scene::teardown() { } void Scene::update() { double current_time = Util::get_timestamp_us() / 1000000.0; double elapsed_time = current_time - startTime_; currentFrame_++; lastUpdateTime_ = current_time; if (elapsed_time >= duration_) running_ = false; } void Scene::draw() { } string Scene::info_string(const string &title) { stringstream ss; ss << "[" << name_ << "] " << Scene::construct_title(title); return ss.str(); } unsigned Scene::average_fps() { double elapsed_time = lastUpdateTime_ - startTime_; return currentFrame_ / elapsed_time; } bool Scene::set_option(const string &opt, const string &val) { map::iterator iter = options_.find(opt); if (iter == options_.end()) return false; std::vector &values(iter->second.acceptable_values); if (!values.empty() && std::find(values.begin(), values.end(), val) == values.end()) { return false; } iter->second.value = val; iter->second.set = true; return true; } void Scene::reset_options() { for (map::iterator iter = options_.begin(); iter != options_.end(); iter++) { Option &opt = iter->second; opt.value = opt.default_value; opt.set = false; } } bool Scene::set_option_default(const string &opt, const string &val) { map::iterator iter = options_.find(opt); if (iter == options_.end()) return false; std::vector &values(iter->second.acceptable_values); if (!values.empty() && std::find(values.begin(), values.end(), val) == values.end()) { return false; } iter->second.default_value = val; return true; } string Scene::construct_title(const string &title) { stringstream ss; if (title == "") { for (map::iterator iter = options_.begin(); iter != options_.end(); iter++) { if (Options::show_all_options || iter->second.set) { ss << iter->first << "=" << iter->second.value << ":"; } } if (ss.str().empty()) ss << ":"; } else ss << title; return ss.str(); } bool Scene::load_shaders_from_strings(Program &program, const std::string &vtx_shader, const std::string &frg_shader, const std::string &vtx_shader_filename, const std::string &frg_shader_filename) { program.init(); Log::debug("Loading vertex shader from file %s:\n%s", vtx_shader_filename.c_str(), vtx_shader.c_str()); program.addShader(GL_VERTEX_SHADER, vtx_shader); if (!program.valid()) { Log::error("Failed to add vertex shader from file %s:\n %s\n", vtx_shader_filename.c_str(), program.errorMessage().c_str()); program.release(); return false; } Log::debug("Loading fragment shader from file %s:\n%s", frg_shader_filename.c_str(), frg_shader.c_str()); program.addShader(GL_FRAGMENT_SHADER, frg_shader); if (!program.valid()) { Log::error("Failed to add fragment shader from file %s:\n %s\n", frg_shader_filename.c_str(), program.errorMessage().c_str()); program.release(); return false; } program.build(); if (!program.ready()) { Log::error("Failed to link program created from files %s and %s: %s\n", vtx_shader_filename.c_str(), frg_shader_filename.c_str(), program.errorMessage().c_str()); program.release(); return false; } return true; } glmark2-2012.08/./src/scene-jellyfish.h0000664000175000017500000000665512013417376016756 0ustar alfalf00000000000000// // Copyright © 2012 Linaro Limited // // This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. // // glmark2 is free software: you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the Free Software // Foundation, either version 3 of the License, or (at your option) any later // version. // // glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // You should have received a copy of the GNU General Public License along with // glmark2. If not, see . // // Authors: // Aleksandar Rodic - Creator and WebGL implementation // Jesse Barker - glmark2 port // #ifndef SCENE_JELLYFISH_ #define SCENE_JELLYFISH_ #include #include "vec.h" #include "stack.h" #include "program.h" class GradientRenderer { Program program_; int positionLocation_; int uvLocation_; unsigned int uvOffset_; unsigned int bufferObject_; std::vector vertices_; std::vector uvs_; public: GradientRenderer() : positionLocation_(0), uvLocation_(0), uvOffset_(0), bufferObject_(0) {} ~GradientRenderer() { vertices_.clear(); uvs_.clear(); } bool init(); void cleanup(); void draw(); }; class JellyfishPrivate { bool load_obj(const std::string& filename); // For the background gradient. GradientRenderer gradient_; // Vertex data. std::vector positions_; std::vector normals_; std::vector colors_; std::vector texcoords_; std::vector indices_; // A simple map so we know where each section of our data starts within // our vertex buffer object. struct VertexDataMap { unsigned int positionOffset; unsigned int positionSize; unsigned int normalOffset; unsigned int normalSize; unsigned int colorOffset; unsigned int colorSize; unsigned int texcoordOffset; unsigned int texcoordSize; unsigned int totalSize; } dataMap_; // Object handles unsigned int bufferObjects_[2]; unsigned int textureObjects_[33]; unsigned int whichCaustic_; std::map causticMap_; // Program state, including attributes, uniforms, and, locations. Program program_; int positionLocation_; int normalLocation_; int colorLocation_; int texcoordLocation_; LibMatrix::vec2 viewport_; LibMatrix::Stack4 world_; LibMatrix::Stack4 projection_; LibMatrix::vec3 lightPosition_; LibMatrix::vec4 lightColor_; float lightRadius_; LibMatrix::vec4 ambientColor_; LibMatrix::vec4 fresnelColor_; float fresnelPower_; float rotation_; float currentTime_; double lastUpdateTime_; // GL state we plan to override, so we can restore it cleanly. unsigned int cullFace_; unsigned int depthTest_; unsigned int blend_; int blendFuncSrc_; int blendFuncDst_; public: JellyfishPrivate(); ~JellyfishPrivate(); bool initialize(); void update_viewport(const LibMatrix::vec2& viewport); void update_time(); void cleanup(); void draw(); }; #endif // SCENE_JELLYFISH_ glmark2-2012.08/./src/libjpeg-turbo/simd/0000775000175000017500000000000012013417376017206 5ustar alfalf00000000000000glmark2-2012.08/./src/libjpeg-turbo/jdct.h0000664000175000017500000001616012013417376017353 0ustar alfalf00000000000000/* * jdct.h * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This include file contains common declarations for the forward and * inverse DCT modules. These declarations are private to the DCT managers * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms. * The individual DCT algorithms are kept in separate files to ease * machine-dependent tuning (e.g., assembly coding). */ /* * A forward DCT routine is given a pointer to a work area of type DCTELEM[]; * the DCT is to be performed in-place in that buffer. Type DCTELEM is int * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT * implementations use an array of type FAST_FLOAT, instead.) * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE). * The DCT outputs are returned scaled up by a factor of 8; they therefore * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This * convention improves accuracy in integer implementations and saves some * work in floating-point ones. * Quantization of the output coefficients is done by jcdctmgr.c. This * step requires an unsigned type and also one with twice the bits. */ #if BITS_IN_JSAMPLE == 8 #ifndef WITH_SIMD typedef int DCTELEM; /* 16 or 32 bits is fine */ typedef unsigned int UDCTELEM; typedef unsigned long long UDCTELEM2; #else typedef short DCTELEM; /* prefer 16 bit with SIMD for parellelism */ typedef unsigned short UDCTELEM; typedef unsigned int UDCTELEM2; #endif #else typedef INT32 DCTELEM; /* must have 32 bits */ typedef UINT32 UDCTELEM; typedef unsigned long long UDCTELEM2; #endif /* * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer * to an output sample array. The routine must dequantize the input data as * well as perform the IDCT; for dequantization, it uses the multiplier table * pointed to by compptr->dct_table. The output data is to be placed into the * sample array starting at a specified column. (Any row offset needed will * be applied to the array pointer before it is passed to the IDCT code.) * Note that the number of samples emitted by the IDCT routine is * DCT_scaled_size * DCT_scaled_size. */ /* typedef inverse_DCT_method_ptr is declared in jpegint.h */ /* * Each IDCT routine has its own ideas about the best dct_table element type. */ typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ #if BITS_IN_JSAMPLE == 8 typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ #define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ #else typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ #endif typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */ /* * Each IDCT routine is responsible for range-limiting its results and * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could * be quite far out of range if the input data is corrupt, so a bulletproof * range-limiting step is required. We use a mask-and-table-lookup method * to do the combined operations quickly. See the comments with * prepare_range_limit_table (in jdmaster.c) for more info. */ #define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE) #define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */ /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_fdct_islow jFDislow #define jpeg_fdct_ifast jFDifast #define jpeg_fdct_float jFDfloat #define jpeg_idct_islow jRDislow #define jpeg_idct_ifast jRDifast #define jpeg_idct_float jRDfloat #define jpeg_idct_4x4 jRD4x4 #define jpeg_idct_2x2 jRD2x2 #define jpeg_idct_1x1 jRD1x1 #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* Extern declarations for the forward and inverse DCT routines. */ EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data)); EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data)); EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data)); EXTERN(void) jpeg_idct_islow JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jpeg_idct_ifast JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jpeg_idct_float JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jpeg_idct_4x4 JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jpeg_idct_2x2 JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jpeg_idct_1x1 JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); /* * Macros for handling fixed-point arithmetic; these are used by many * but not all of the DCT/IDCT modules. * * All values are expected to be of type INT32. * Fractional constants are scaled left by CONST_BITS bits. * CONST_BITS is defined within each module using these macros, * and may differ from one module to the next. */ #define ONE ((INT32) 1) #define CONST_SCALE (ONE << CONST_BITS) /* Convert a positive real constant to an integer scaled by CONST_SCALE. * Caution: some C compilers fail to reduce "FIX(constant)" at compile time, * thus causing a lot of useless floating-point operations at run time. */ #define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) /* Descale and correctly round an INT32 value that's scaled by N bits. * We assume RIGHT_SHIFT rounds towards minus infinity, so adding * the fudge factor is correct for either sign of X. */ #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n) /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. * This macro is used only when the two inputs will actually be no more than * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a * full 32x32 multiply. This provides a useful speedup on many machines. * Unfortunately there is no way to specify a 16x16->32 multiply portably * in C, but some C compilers will do the right thing if you provide the * correct combination of casts. */ #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const))) #endif #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const))) #endif #ifndef MULTIPLY16C16 /* default definition */ #define MULTIPLY16C16(var,const) ((var) * (const)) #endif /* Same except both inputs are variables. */ #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ #define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2))) #endif #ifndef MULTIPLY16V16 /* default definition */ #define MULTIPLY16V16(var1,var2) ((var1) * (var2)) #endif glmark2-2012.08/./src/libjpeg-turbo/jpegcomp.h0000664000175000017500000000155612013417376020236 0ustar alfalf00000000000000/* * jpegcomp.h * * Copyright (C) 2010, D. R. Commander * For conditions of distribution and use, see the accompanying README file. * * JPEG compatibility macros * These declarations are considered internal to the JPEG library; most * applications using the library shouldn't need to include this file. */ #if JPEG_LIB_VERSION >= 70 #define _DCT_scaled_size DCT_h_scaled_size #define _min_DCT_scaled_size min_DCT_h_scaled_size #define _min_DCT_h_scaled_size min_DCT_h_scaled_size #define _min_DCT_v_scaled_size min_DCT_v_scaled_size #define _jpeg_width jpeg_width #define _jpeg_height jpeg_height #else #define _DCT_scaled_size DCT_scaled_size #define _min_DCT_scaled_size min_DCT_scaled_size #define _min_DCT_h_scaled_size min_DCT_scaled_size #define _min_DCT_v_scaled_size min_DCT_scaled_size #define _jpeg_width image_width #define _jpeg_height image_height #endif glmark2-2012.08/./src/libjpeg-turbo/jcapistd.c0000664000175000017500000001337112013417376020224 0ustar alfalf00000000000000/* * jcapistd.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains application interface code for the compression half * of the JPEG library. These are the "standard" API routines that are * used in the normal full-compression case. They are not used by a * transcoding-only application. Note that if an application links in * jpeg_start_compress, it will end up linking in the entire compressor. * We thus must separate this file from jcapimin.c to avoid linking the * whole compression library into a transcoder. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Compression initialization. * Before calling this, all parameters and a data destination must be set up. * * We require a write_all_tables parameter as a failsafe check when writing * multiple datastreams from the same compression object. Since prior runs * will have left all the tables marked sent_table=TRUE, a subsequent run * would emit an abbreviated stream (no tables) by default. This may be what * is wanted, but for safety's sake it should not be the default behavior: * programmers should have to make a deliberate choice to emit abbreviated * images. Therefore the documentation and examples should encourage people * to pass write_all_tables=TRUE; then it will take active thought to do the * wrong thing. */ GLOBAL(void) jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables) { if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (write_all_tables) jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */ /* (Re)initialize error mgr and destination modules */ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); (*cinfo->dest->init_destination) (cinfo); /* Perform master selection of active modules */ jinit_compress_master(cinfo); /* Set up for the first pass */ (*cinfo->master->prepare_for_pass) (cinfo); /* Ready for application to drive first pass through jpeg_write_scanlines * or jpeg_write_raw_data. */ cinfo->next_scanline = 0; cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING); } /* * Write some scanlines of data to the JPEG compressor. * * The return value will be the number of lines actually written. * This should be less than the supplied num_lines only in case that * the data destination module has requested suspension of the compressor, * or if more than image_height scanlines are passed in. * * Note: we warn about excess calls to jpeg_write_scanlines() since * this likely signals an application programmer error. However, * excess scanlines passed in the last valid call are *silently* ignored, * so that the application need not adjust num_lines for end-of-image * when using a multiple-scanline buffer. */ GLOBAL(JDIMENSION) jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines) { JDIMENSION row_ctr, rows_left; if (cinfo->global_state != CSTATE_SCANNING) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (cinfo->next_scanline >= cinfo->image_height) WARNMS(cinfo, JWRN_TOO_MUCH_DATA); /* Call progress monitor hook if present */ if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) cinfo->next_scanline; cinfo->progress->pass_limit = (long) cinfo->image_height; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* Give master control module another chance if this is first call to * jpeg_write_scanlines. This lets output of the frame/scan headers be * delayed so that application can write COM, etc, markers between * jpeg_start_compress and jpeg_write_scanlines. */ if (cinfo->master->call_pass_startup) (*cinfo->master->pass_startup) (cinfo); /* Ignore any extra scanlines at bottom of image. */ rows_left = cinfo->image_height - cinfo->next_scanline; if (num_lines > rows_left) num_lines = rows_left; row_ctr = 0; (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines); cinfo->next_scanline += row_ctr; return row_ctr; } /* * Alternate entry point to write raw data. * Processes exactly one iMCU row per call, unless suspended. */ GLOBAL(JDIMENSION) jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data, JDIMENSION num_lines) { JDIMENSION lines_per_iMCU_row; if (cinfo->global_state != CSTATE_RAW_OK) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (cinfo->next_scanline >= cinfo->image_height) { WARNMS(cinfo, JWRN_TOO_MUCH_DATA); return 0; } /* Call progress monitor hook if present */ if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) cinfo->next_scanline; cinfo->progress->pass_limit = (long) cinfo->image_height; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* Give master control module another chance if this is first call to * jpeg_write_raw_data. This lets output of the frame/scan headers be * delayed so that application can write COM, etc, markers between * jpeg_start_compress and jpeg_write_raw_data. */ if (cinfo->master->call_pass_startup) (*cinfo->master->pass_startup) (cinfo); /* Verify that at least one iMCU row has been passed. */ lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; if (num_lines < lines_per_iMCU_row) ERREXIT(cinfo, JERR_BUFFER_SIZE); /* Directly compress the row. */ if (! (*cinfo->coef->compress_data) (cinfo, data)) { /* If compressor did not consume the whole row, suspend processing. */ return 0; } /* OK, we processed one iMCU row. */ cinfo->next_scanline += lines_per_iMCU_row; return lines_per_iMCU_row; } glmark2-2012.08/./src/libjpeg-turbo/jccolext.c.inc0000664000175000017500000000675512013417376021016 0ustar alfalf00000000000000/* * jccolext.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains input colorspace conversion routines. */ /* This file is included by jccolor.c */ /* * Convert some rows of samples to the JPEG colorspace. * * Note that we change from the application's interleaved-pixel format * to our internal noninterleaved, one-plane-per-component format. * The input buffer is therefore three times as wide as the output buffer. * * A starting row offset is provided only for the output buffer. The caller * can easily adjust the passed input_buf value to accommodate any row * offset required on that side. */ INLINE LOCAL(void) rgb_ycc_convert_internal (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; register int r, g, b; register INT32 * ctab = cconvert->rgb_ycc_tab; register JSAMPROW inptr; register JSAMPROW outptr0, outptr1, outptr2; register JDIMENSION col; JDIMENSION num_cols = cinfo->image_width; while (--num_rows >= 0) { inptr = *input_buf++; outptr0 = output_buf[0][output_row]; outptr1 = output_buf[1][output_row]; outptr2 = output_buf[2][output_row]; output_row++; for (col = 0; col < num_cols; col++) { r = GETJSAMPLE(inptr[RGB_RED]); g = GETJSAMPLE(inptr[RGB_GREEN]); b = GETJSAMPLE(inptr[RGB_BLUE]); inptr += RGB_PIXELSIZE; /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations * must be too; we do not need an explicit range-limiting operation. * Hence the value being shifted is never negative, and we don't * need the general RIGHT_SHIFT macro. */ /* Y */ outptr0[col] = (JSAMPLE) ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) >> SCALEBITS); /* Cb */ outptr1[col] = (JSAMPLE) ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) >> SCALEBITS); /* Cr */ outptr2[col] = (JSAMPLE) ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) >> SCALEBITS); } } } /**************** Cases other than RGB -> YCbCr **************/ /* * Convert some rows of samples to the JPEG colorspace. * This version handles RGB->grayscale conversion, which is the same * as the RGB->Y portion of RGB->YCbCr. * We assume rgb_ycc_start has been called (we only use the Y tables). */ INLINE LOCAL(void) rgb_gray_convert_internal (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; register int r, g, b; register INT32 * ctab = cconvert->rgb_ycc_tab; register JSAMPROW inptr; register JSAMPROW outptr; register JDIMENSION col; JDIMENSION num_cols = cinfo->image_width; while (--num_rows >= 0) { inptr = *input_buf++; outptr = output_buf[0][output_row]; output_row++; for (col = 0; col < num_cols; col++) { r = GETJSAMPLE(inptr[RGB_RED]); g = GETJSAMPLE(inptr[RGB_GREEN]); b = GETJSAMPLE(inptr[RGB_BLUE]); inptr += RGB_PIXELSIZE; /* Y */ outptr[col] = (JSAMPLE) ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) >> SCALEBITS); } } } glmark2-2012.08/./src/libjpeg-turbo/jconfig.h0000664000175000017500000000316512013417376020047 0ustar alfalf00000000000000/* jconfig.h. Generated from jconfig.h.in by configure. */ /* Version ID for the JPEG library. * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60". */ #define JPEG_LIB_VERSION 62 /* libjpeg-turbo version */ #define LIBJPEG_TURBO_VERSION 1.2.0 /* Support arithmetic encoding */ #define C_ARITH_CODING_SUPPORTED 1 /* Support arithmetic decoding */ #define D_ARITH_CODING_SUPPORTED 1 /* Compiler supports function prototypes. */ #define HAVE_PROTOTYPES 1 /* Define to 1 if you have the header file. */ #define HAVE_STDDEF_H 1 /* Define to 1 if you have the header file. */ #define HAVE_STDLIB_H 1 /* Compiler supports 'unsigned char'. */ #define HAVE_UNSIGNED_CHAR 1 /* Compiler supports 'unsigned short'. */ #define HAVE_UNSIGNED_SHORT 1 /* Compiler does not support pointers to unspecified structures. */ /* #undef INCOMPLETE_TYPES_BROKEN */ /* Compiler has rather than standard . */ /* #undef NEED_BSD_STRINGS */ /* Linker requires that global names be unique in first 15 characters. */ /* #undef NEED_SHORT_EXTERNAL_NAMES */ /* Need to include in order to obtain size_t. */ #define NEED_SYS_TYPES_H 1 /* Broken compiler shifts signed values as an unsigned shift. */ /* #undef RIGHT_SHIFT_IS_UNSIGNED */ /* Use accelerated SIMD routines. */ #define WITH_SIMD 1 /* Define to 1 if type `char' is unsigned and you are not using gcc. */ #ifndef __CHAR_UNSIGNED__ /* # undef __CHAR_UNSIGNED__ */ #endif /* Define to empty if `const' does not conform to ANSI C. */ /* #undef const */ /* Define to `unsigned int' if does not define. */ /* #undef size_t */ glmark2-2012.08/./src/libjpeg-turbo/jdmainct.c0000664000175000017500000005037512013417376020221 0ustar alfalf00000000000000/* * jdmainct.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the main buffer controller for decompression. * The main buffer lies between the JPEG decompressor proper and the * post-processor; it holds downsampled data in the JPEG colorspace. * * Note that this code is bypassed in raw-data mode, since the application * supplies the equivalent of the main buffer in that case. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* * In the current system design, the main buffer need never be a full-image * buffer; any full-height buffers will be found inside the coefficient or * postprocessing controllers. Nonetheless, the main controller is not * trivial. Its responsibility is to provide context rows for upsampling/ * rescaling, and doing this in an efficient fashion is a bit tricky. * * Postprocessor input data is counted in "row groups". A row group * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) * sample rows of each component. (We require DCT_scaled_size values to be * chosen such that these numbers are integers. In practice DCT_scaled_size * values will likely be powers of two, so we actually have the stronger * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.) * Upsampling will typically produce max_v_samp_factor pixel rows from each * row group (times any additional scale factor that the upsampler is * applying). * * The coefficient controller will deliver data to us one iMCU row at a time; * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or * exactly min_DCT_scaled_size row groups. (This amount of data corresponds * to one row of MCUs when the image is fully interleaved.) Note that the * number of sample rows varies across components, but the number of row * groups does not. Some garbage sample rows may be included in the last iMCU * row at the bottom of the image. * * Depending on the vertical scaling algorithm used, the upsampler may need * access to the sample row(s) above and below its current input row group. * The upsampler is required to set need_context_rows TRUE at global selection * time if so. When need_context_rows is FALSE, this controller can simply * obtain one iMCU row at a time from the coefficient controller and dole it * out as row groups to the postprocessor. * * When need_context_rows is TRUE, this controller guarantees that the buffer * passed to postprocessing contains at least one row group's worth of samples * above and below the row group(s) being processed. Note that the context * rows "above" the first passed row group appear at negative row offsets in * the passed buffer. At the top and bottom of the image, the required * context rows are manufactured by duplicating the first or last real sample * row; this avoids having special cases in the upsampling inner loops. * * The amount of context is fixed at one row group just because that's a * convenient number for this controller to work with. The existing * upsamplers really only need one sample row of context. An upsampler * supporting arbitrary output rescaling might wish for more than one row * group of context when shrinking the image; tough, we don't handle that. * (This is justified by the assumption that downsizing will be handled mostly * by adjusting the DCT_scaled_size values, so that the actual scale factor at * the upsample step needn't be much less than one.) * * To provide the desired context, we have to retain the last two row groups * of one iMCU row while reading in the next iMCU row. (The last row group * can't be processed until we have another row group for its below-context, * and so we have to save the next-to-last group too for its above-context.) * We could do this most simply by copying data around in our buffer, but * that'd be very slow. We can avoid copying any data by creating a rather * strange pointer structure. Here's how it works. We allocate a workspace * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number * of row groups per iMCU row). We create two sets of redundant pointers to * the workspace. Labeling the physical row groups 0 to M+1, the synthesized * pointer lists look like this: * M+1 M-1 * master pointer --> 0 master pointer --> 0 * 1 1 * ... ... * M-3 M-3 * M-2 M * M-1 M+1 * M M-2 * M+1 M-1 * 0 0 * We read alternate iMCU rows using each master pointer; thus the last two * row groups of the previous iMCU row remain un-overwritten in the workspace. * The pointer lists are set up so that the required context rows appear to * be adjacent to the proper places when we pass the pointer lists to the * upsampler. * * The above pictures describe the normal state of the pointer lists. * At top and bottom of the image, we diddle the pointer lists to duplicate * the first or last sample row as necessary (this is cheaper than copying * sample rows around). * * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that * situation each iMCU row provides only one row group so the buffering logic * must be different (eg, we must read two iMCU rows before we can emit the * first row group). For now, we simply do not support providing context * rows when min_DCT_scaled_size is 1. That combination seems unlikely to * be worth providing --- if someone wants a 1/8th-size preview, they probably * want it quick and dirty, so a context-free upsampler is sufficient. */ /* Private buffer controller object */ typedef struct { struct jpeg_d_main_controller pub; /* public fields */ /* Pointer to allocated workspace (M or M+2 row groups). */ JSAMPARRAY buffer[MAX_COMPONENTS]; boolean buffer_full; /* Have we gotten an iMCU row from decoder? */ JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */ /* Remaining fields are only used in the context case. */ /* These are the master pointers to the funny-order pointer lists. */ JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */ int whichptr; /* indicates which pointer set is now in use */ int context_state; /* process_data state machine status */ JDIMENSION rowgroups_avail; /* row groups available to postprocessor */ JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */ } my_main_controller; typedef my_main_controller * my_main_ptr; /* context_state values: */ #define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */ #define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */ #define CTX_POSTPONED_ROW 2 /* feeding postponed row group */ /* Forward declarations */ METHODDEF(void) process_data_simple_main JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); METHODDEF(void) process_data_context_main JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); #ifdef QUANT_2PASS_SUPPORTED METHODDEF(void) process_data_crank_post JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); #endif LOCAL(void) alloc_funny_pointers (j_decompress_ptr cinfo) /* Allocate space for the funny pointer lists. * This is done only once, not once per pass. */ { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; int ci, rgroup; int M = cinfo->_min_DCT_scaled_size; jpeg_component_info *compptr; JSAMPARRAY xbuf; /* Get top-level space for component array pointers. * We alloc both arrays with one call to save a few cycles. */ main_ptr->xbuffer[0] = (JSAMPIMAGE) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->num_components * 2 * SIZEOF(JSAMPARRAY)); main_ptr->xbuffer[1] = main_ptr->xbuffer[0] + cinfo->num_components; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; /* height of a row group of component */ /* Get space for pointer lists --- M+4 row groups in each list. * We alloc both pointer lists with one call to save a few cycles. */ xbuf = (JSAMPARRAY) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW)); xbuf += rgroup; /* want one row group at negative offsets */ main_ptr->xbuffer[0][ci] = xbuf; xbuf += rgroup * (M + 4); main_ptr->xbuffer[1][ci] = xbuf; } } LOCAL(void) make_funny_pointers (j_decompress_ptr cinfo) /* Create the funny pointer lists discussed in the comments above. * The actual workspace is already allocated (in main_ptr->buffer), * and the space for the pointer lists is allocated too. * This routine just fills in the curiously ordered lists. * This will be repeated at the beginning of each pass. */ { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; int ci, i, rgroup; int M = cinfo->_min_DCT_scaled_size; jpeg_component_info *compptr; JSAMPARRAY buf, xbuf0, xbuf1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; /* height of a row group of component */ xbuf0 = main_ptr->xbuffer[0][ci]; xbuf1 = main_ptr->xbuffer[1][ci]; /* First copy the workspace pointers as-is */ buf = main_ptr->buffer[ci]; for (i = 0; i < rgroup * (M + 2); i++) { xbuf0[i] = xbuf1[i] = buf[i]; } /* In the second list, put the last four row groups in swapped order */ for (i = 0; i < rgroup * 2; i++) { xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i]; xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i]; } /* The wraparound pointers at top and bottom will be filled later * (see set_wraparound_pointers, below). Initially we want the "above" * pointers to duplicate the first actual data line. This only needs * to happen in xbuffer[0]. */ for (i = 0; i < rgroup; i++) { xbuf0[i - rgroup] = xbuf0[0]; } } } LOCAL(void) set_wraparound_pointers (j_decompress_ptr cinfo) /* Set up the "wraparound" pointers at top and bottom of the pointer lists. * This changes the pointer list state from top-of-image to the normal state. */ { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; int ci, i, rgroup; int M = cinfo->_min_DCT_scaled_size; jpeg_component_info *compptr; JSAMPARRAY xbuf0, xbuf1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; /* height of a row group of component */ xbuf0 = main_ptr->xbuffer[0][ci]; xbuf1 = main_ptr->xbuffer[1][ci]; for (i = 0; i < rgroup; i++) { xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i]; xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i]; xbuf0[rgroup*(M+2) + i] = xbuf0[i]; xbuf1[rgroup*(M+2) + i] = xbuf1[i]; } } } LOCAL(void) set_bottom_pointers (j_decompress_ptr cinfo) /* Change the pointer lists to duplicate the last sample row at the bottom * of the image. whichptr indicates which xbuffer holds the final iMCU row. * Also sets rowgroups_avail to indicate number of nondummy row groups in row. */ { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; int ci, i, rgroup, iMCUheight, rows_left; jpeg_component_info *compptr; JSAMPARRAY xbuf; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Count sample rows in one iMCU row and in one row group */ iMCUheight = compptr->v_samp_factor * compptr->_DCT_scaled_size; rgroup = iMCUheight / cinfo->_min_DCT_scaled_size; /* Count nondummy sample rows remaining for this component */ rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight); if (rows_left == 0) rows_left = iMCUheight; /* Count nondummy row groups. Should get same answer for each component, * so we need only do it once. */ if (ci == 0) { main_ptr->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1); } /* Duplicate the last real sample row rgroup*2 times; this pads out the * last partial rowgroup and ensures at least one full rowgroup of context. */ xbuf = main_ptr->xbuffer[main_ptr->whichptr][ci]; for (i = 0; i < rgroup * 2; i++) { xbuf[rows_left + i] = xbuf[rows_left-1]; } } } /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; switch (pass_mode) { case JBUF_PASS_THRU: if (cinfo->upsample->need_context_rows) { main_ptr->pub.process_data = process_data_context_main; make_funny_pointers(cinfo); /* Create the xbuffer[] lists */ main_ptr->whichptr = 0; /* Read first iMCU row into xbuffer[0] */ main_ptr->context_state = CTX_PREPARE_FOR_IMCU; main_ptr->iMCU_row_ctr = 0; } else { /* Simple case with no context needed */ main_ptr->pub.process_data = process_data_simple_main; } main_ptr->buffer_full = FALSE; /* Mark buffer empty */ main_ptr->rowgroup_ctr = 0; break; #ifdef QUANT_2PASS_SUPPORTED case JBUF_CRANK_DEST: /* For last pass of 2-pass quantization, just crank the postprocessor */ main_ptr->pub.process_data = process_data_crank_post; break; #endif default: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); break; } } /* * Process some data. * This handles the simple case where no context is required. */ METHODDEF(void) process_data_simple_main (j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; JDIMENSION rowgroups_avail; /* Read input data if we haven't filled the main buffer yet */ if (! main_ptr->buffer_full) { if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->buffer)) return; /* suspension forced, can do nothing more */ main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ } /* There are always min_DCT_scaled_size row groups in an iMCU row. */ rowgroups_avail = (JDIMENSION) cinfo->_min_DCT_scaled_size; /* Note: at the bottom of the image, we may pass extra garbage row groups * to the postprocessor. The postprocessor has to check for bottom * of image anyway (at row resolution), so no point in us doing it too. */ /* Feed the postprocessor */ (*cinfo->post->post_process_data) (cinfo, main_ptr->buffer, &main_ptr->rowgroup_ctr, rowgroups_avail, output_buf, out_row_ctr, out_rows_avail); /* Has postprocessor consumed all the data yet? If so, mark buffer empty */ if (main_ptr->rowgroup_ctr >= rowgroups_avail) { main_ptr->buffer_full = FALSE; main_ptr->rowgroup_ctr = 0; } } /* * Process some data. * This handles the case where context rows must be provided. */ METHODDEF(void) process_data_context_main (j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; /* Read input data if we haven't filled the main buffer yet */ if (! main_ptr->buffer_full) { if (! (*cinfo->coef->decompress_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr])) return; /* suspension forced, can do nothing more */ main_ptr->buffer_full = TRUE; /* OK, we have an iMCU row to work with */ main_ptr->iMCU_row_ctr++; /* count rows received */ } /* Postprocessor typically will not swallow all the input data it is handed * in one call (due to filling the output buffer first). Must be prepared * to exit and restart. This switch lets us keep track of how far we got. * Note that each case falls through to the next on successful completion. */ switch (main_ptr->context_state) { case CTX_POSTPONED_ROW: /* Call postprocessor using previously set pointers for postponed row */ (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, output_buf, out_row_ctr, out_rows_avail); if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) return; /* Need to suspend */ main_ptr->context_state = CTX_PREPARE_FOR_IMCU; if (*out_row_ctr >= out_rows_avail) return; /* Postprocessor exactly filled output buf */ /*FALLTHROUGH*/ case CTX_PREPARE_FOR_IMCU: /* Prepare to process first M-1 row groups of this iMCU row */ main_ptr->rowgroup_ctr = 0; main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size - 1); /* Check for bottom of image: if so, tweak pointers to "duplicate" * the last sample row, and adjust rowgroups_avail to ignore padding rows. */ if (main_ptr->iMCU_row_ctr == cinfo->total_iMCU_rows) set_bottom_pointers(cinfo); main_ptr->context_state = CTX_PROCESS_IMCU; /*FALLTHROUGH*/ case CTX_PROCESS_IMCU: /* Call postprocessor using previously set pointers */ (*cinfo->post->post_process_data) (cinfo, main_ptr->xbuffer[main_ptr->whichptr], &main_ptr->rowgroup_ctr, main_ptr->rowgroups_avail, output_buf, out_row_ctr, out_rows_avail); if (main_ptr->rowgroup_ctr < main_ptr->rowgroups_avail) return; /* Need to suspend */ /* After the first iMCU, change wraparound pointers to normal state */ if (main_ptr->iMCU_row_ctr == 1) set_wraparound_pointers(cinfo); /* Prepare to load new iMCU row using other xbuffer list */ main_ptr->whichptr ^= 1; /* 0=>1 or 1=>0 */ main_ptr->buffer_full = FALSE; /* Still need to process last row group of this iMCU row, */ /* which is saved at index M+1 of the other xbuffer */ main_ptr->rowgroup_ctr = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 1); main_ptr->rowgroups_avail = (JDIMENSION) (cinfo->_min_DCT_scaled_size + 2); main_ptr->context_state = CTX_POSTPONED_ROW; } } /* * Process some data. * Final pass of two-pass quantization: just call the postprocessor. * Source data will be the postprocessor controller's internal buffer. */ #ifdef QUANT_2PASS_SUPPORTED METHODDEF(void) process_data_crank_post (j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL, (JDIMENSION *) NULL, (JDIMENSION) 0, output_buf, out_row_ctr, out_rows_avail); } #endif /* QUANT_2PASS_SUPPORTED */ /* * Initialize main buffer controller. */ GLOBAL(void) jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer) { my_main_ptr main_ptr; int ci, rgroup, ngroups; jpeg_component_info *compptr; main_ptr = (my_main_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_main_controller)); cinfo->main = (struct jpeg_d_main_controller *) main_ptr; main_ptr->pub.start_pass = start_pass_main; if (need_full_buffer) /* shouldn't happen */ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); /* Allocate the workspace. * ngroups is the number of row groups we need. */ if (cinfo->upsample->need_context_rows) { if (cinfo->_min_DCT_scaled_size < 2) /* unsupported, see comments above */ ERREXIT(cinfo, JERR_NOTIMPL); alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */ ngroups = cinfo->_min_DCT_scaled_size + 2; } else { ngroups = cinfo->_min_DCT_scaled_size; } for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { rgroup = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; /* height of a row group of component */ main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, compptr->width_in_blocks * compptr->_DCT_scaled_size, (JDIMENSION) (rgroup * ngroups)); } } glmark2-2012.08/./src/libjpeg-turbo/jaricom.c0000664000175000017500000001171212013417376020044 0ustar alfalf00000000000000/* * jaricom.c * * Developed 1997-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains probability estimation tables for common use in * arithmetic entropy encoding and decoding routines. * * This data represents Table D.2 in the JPEG spec (ISO/IEC IS 10918-1 * and CCITT Recommendation ITU-T T.81) and Table 24 in the JBIG spec * (ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82). */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* The following #define specifies the packing of the four components * into the compact INT32 representation. * Note that this formula must match the actual arithmetic encoder * and decoder implementation. The implementation has to be changed * if this formula is changed. * The current organization is leaned on Markus Kuhn's JBIG * implementation (jbig_tab.c). */ #define V(i,a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b) const INT32 jpeg_aritab[113+1] = { /* * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS */ V( 0, 0x5a1d, 1, 1, 1 ), V( 1, 0x2586, 14, 2, 0 ), V( 2, 0x1114, 16, 3, 0 ), V( 3, 0x080b, 18, 4, 0 ), V( 4, 0x03d8, 20, 5, 0 ), V( 5, 0x01da, 23, 6, 0 ), V( 6, 0x00e5, 25, 7, 0 ), V( 7, 0x006f, 28, 8, 0 ), V( 8, 0x0036, 30, 9, 0 ), V( 9, 0x001a, 33, 10, 0 ), V( 10, 0x000d, 35, 11, 0 ), V( 11, 0x0006, 9, 12, 0 ), V( 12, 0x0003, 10, 13, 0 ), V( 13, 0x0001, 12, 13, 0 ), V( 14, 0x5a7f, 15, 15, 1 ), V( 15, 0x3f25, 36, 16, 0 ), V( 16, 0x2cf2, 38, 17, 0 ), V( 17, 0x207c, 39, 18, 0 ), V( 18, 0x17b9, 40, 19, 0 ), V( 19, 0x1182, 42, 20, 0 ), V( 20, 0x0cef, 43, 21, 0 ), V( 21, 0x09a1, 45, 22, 0 ), V( 22, 0x072f, 46, 23, 0 ), V( 23, 0x055c, 48, 24, 0 ), V( 24, 0x0406, 49, 25, 0 ), V( 25, 0x0303, 51, 26, 0 ), V( 26, 0x0240, 52, 27, 0 ), V( 27, 0x01b1, 54, 28, 0 ), V( 28, 0x0144, 56, 29, 0 ), V( 29, 0x00f5, 57, 30, 0 ), V( 30, 0x00b7, 59, 31, 0 ), V( 31, 0x008a, 60, 32, 0 ), V( 32, 0x0068, 62, 33, 0 ), V( 33, 0x004e, 63, 34, 0 ), V( 34, 0x003b, 32, 35, 0 ), V( 35, 0x002c, 33, 9, 0 ), V( 36, 0x5ae1, 37, 37, 1 ), V( 37, 0x484c, 64, 38, 0 ), V( 38, 0x3a0d, 65, 39, 0 ), V( 39, 0x2ef1, 67, 40, 0 ), V( 40, 0x261f, 68, 41, 0 ), V( 41, 0x1f33, 69, 42, 0 ), V( 42, 0x19a8, 70, 43, 0 ), V( 43, 0x1518, 72, 44, 0 ), V( 44, 0x1177, 73, 45, 0 ), V( 45, 0x0e74, 74, 46, 0 ), V( 46, 0x0bfb, 75, 47, 0 ), V( 47, 0x09f8, 77, 48, 0 ), V( 48, 0x0861, 78, 49, 0 ), V( 49, 0x0706, 79, 50, 0 ), V( 50, 0x05cd, 48, 51, 0 ), V( 51, 0x04de, 50, 52, 0 ), V( 52, 0x040f, 50, 53, 0 ), V( 53, 0x0363, 51, 54, 0 ), V( 54, 0x02d4, 52, 55, 0 ), V( 55, 0x025c, 53, 56, 0 ), V( 56, 0x01f8, 54, 57, 0 ), V( 57, 0x01a4, 55, 58, 0 ), V( 58, 0x0160, 56, 59, 0 ), V( 59, 0x0125, 57, 60, 0 ), V( 60, 0x00f6, 58, 61, 0 ), V( 61, 0x00cb, 59, 62, 0 ), V( 62, 0x00ab, 61, 63, 0 ), V( 63, 0x008f, 61, 32, 0 ), V( 64, 0x5b12, 65, 65, 1 ), V( 65, 0x4d04, 80, 66, 0 ), V( 66, 0x412c, 81, 67, 0 ), V( 67, 0x37d8, 82, 68, 0 ), V( 68, 0x2fe8, 83, 69, 0 ), V( 69, 0x293c, 84, 70, 0 ), V( 70, 0x2379, 86, 71, 0 ), V( 71, 0x1edf, 87, 72, 0 ), V( 72, 0x1aa9, 87, 73, 0 ), V( 73, 0x174e, 72, 74, 0 ), V( 74, 0x1424, 72, 75, 0 ), V( 75, 0x119c, 74, 76, 0 ), V( 76, 0x0f6b, 74, 77, 0 ), V( 77, 0x0d51, 75, 78, 0 ), V( 78, 0x0bb6, 77, 79, 0 ), V( 79, 0x0a40, 77, 48, 0 ), V( 80, 0x5832, 80, 81, 1 ), V( 81, 0x4d1c, 88, 82, 0 ), V( 82, 0x438e, 89, 83, 0 ), V( 83, 0x3bdd, 90, 84, 0 ), V( 84, 0x34ee, 91, 85, 0 ), V( 85, 0x2eae, 92, 86, 0 ), V( 86, 0x299a, 93, 87, 0 ), V( 87, 0x2516, 86, 71, 0 ), V( 88, 0x5570, 88, 89, 1 ), V( 89, 0x4ca9, 95, 90, 0 ), V( 90, 0x44d9, 96, 91, 0 ), V( 91, 0x3e22, 97, 92, 0 ), V( 92, 0x3824, 99, 93, 0 ), V( 93, 0x32b4, 99, 94, 0 ), V( 94, 0x2e17, 93, 86, 0 ), V( 95, 0x56a8, 95, 96, 1 ), V( 96, 0x4f46, 101, 97, 0 ), V( 97, 0x47e5, 102, 98, 0 ), V( 98, 0x41cf, 103, 99, 0 ), V( 99, 0x3c3d, 104, 100, 0 ), V( 100, 0x375e, 99, 93, 0 ), V( 101, 0x5231, 105, 102, 0 ), V( 102, 0x4c0f, 106, 103, 0 ), V( 103, 0x4639, 107, 104, 0 ), V( 104, 0x415e, 103, 99, 0 ), V( 105, 0x5627, 105, 106, 1 ), V( 106, 0x50e7, 108, 107, 0 ), V( 107, 0x4b85, 109, 103, 0 ), V( 108, 0x5597, 110, 109, 0 ), V( 109, 0x504f, 111, 107, 0 ), V( 110, 0x5a10, 110, 111, 1 ), V( 111, 0x5522, 112, 109, 0 ), V( 112, 0x59eb, 112, 111, 1 ), /* * This last entry is used for fixed probability estimate of 0.5 * as recommended in Section 10.3 Table 5 of ITU-T Rec. T.851. */ V( 113, 0x5a1d, 113, 113, 0 ) }; glmark2-2012.08/./src/libjpeg-turbo/jdmarker.c0000664000175000017500000012046612013417376020226 0ustar alfalf00000000000000/* * jdmarker.c * * Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 2012, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains routines to decode JPEG datastream markers. * Most of the complexity arises from our desire to support input * suspension: if not all of the data for a marker is available, * we must exit back to the application. On resumption, we reprocess * the marker. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" typedef enum { /* JPEG marker codes */ M_SOF0 = 0xc0, M_SOF1 = 0xc1, M_SOF2 = 0xc2, M_SOF3 = 0xc3, M_SOF5 = 0xc5, M_SOF6 = 0xc6, M_SOF7 = 0xc7, M_JPG = 0xc8, M_SOF9 = 0xc9, M_SOF10 = 0xca, M_SOF11 = 0xcb, M_SOF13 = 0xcd, M_SOF14 = 0xce, M_SOF15 = 0xcf, M_DHT = 0xc4, M_DAC = 0xcc, M_RST0 = 0xd0, M_RST1 = 0xd1, M_RST2 = 0xd2, M_RST3 = 0xd3, M_RST4 = 0xd4, M_RST5 = 0xd5, M_RST6 = 0xd6, M_RST7 = 0xd7, M_SOI = 0xd8, M_EOI = 0xd9, M_SOS = 0xda, M_DQT = 0xdb, M_DNL = 0xdc, M_DRI = 0xdd, M_DHP = 0xde, M_EXP = 0xdf, M_APP0 = 0xe0, M_APP1 = 0xe1, M_APP2 = 0xe2, M_APP3 = 0xe3, M_APP4 = 0xe4, M_APP5 = 0xe5, M_APP6 = 0xe6, M_APP7 = 0xe7, M_APP8 = 0xe8, M_APP9 = 0xe9, M_APP10 = 0xea, M_APP11 = 0xeb, M_APP12 = 0xec, M_APP13 = 0xed, M_APP14 = 0xee, M_APP15 = 0xef, M_JPG0 = 0xf0, M_JPG13 = 0xfd, M_COM = 0xfe, M_TEM = 0x01, M_ERROR = 0x100 } JPEG_MARKER; /* Private state */ typedef struct { struct jpeg_marker_reader pub; /* public fields */ /* Application-overridable marker processing methods */ jpeg_marker_parser_method process_COM; jpeg_marker_parser_method process_APPn[16]; /* Limit on marker data length to save for each marker type */ unsigned int length_limit_COM; unsigned int length_limit_APPn[16]; /* Status of COM/APPn marker saving */ jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */ unsigned int bytes_read; /* data bytes read so far in marker */ /* Note: cur_marker is not linked into marker_list until it's all read. */ } my_marker_reader; typedef my_marker_reader * my_marker_ptr; /* * Macros for fetching data from the data source module. * * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect * the current restart point; we update them only when we have reached a * suitable place to restart if a suspension occurs. */ /* Declare and initialize local copies of input pointer/count */ #define INPUT_VARS(cinfo) \ struct jpeg_source_mgr * datasrc = (cinfo)->src; \ const JOCTET * next_input_byte = datasrc->next_input_byte; \ size_t bytes_in_buffer = datasrc->bytes_in_buffer /* Unload the local copies --- do this only at a restart boundary */ #define INPUT_SYNC(cinfo) \ ( datasrc->next_input_byte = next_input_byte, \ datasrc->bytes_in_buffer = bytes_in_buffer ) /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */ #define INPUT_RELOAD(cinfo) \ ( next_input_byte = datasrc->next_input_byte, \ bytes_in_buffer = datasrc->bytes_in_buffer ) /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available. * Note we do *not* do INPUT_SYNC before calling fill_input_buffer, * but we must reload the local copies after a successful fill. */ #define MAKE_BYTE_AVAIL(cinfo,action) \ if (bytes_in_buffer == 0) { \ if (! (*datasrc->fill_input_buffer) (cinfo)) \ { action; } \ INPUT_RELOAD(cinfo); \ } /* Read a byte into variable V. * If must suspend, take the specified action (typically "return FALSE"). */ #define INPUT_BYTE(cinfo,V,action) \ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ bytes_in_buffer--; \ V = GETJOCTET(*next_input_byte++); ) /* As above, but read two bytes interpreted as an unsigned 16-bit integer. * V should be declared unsigned int or perhaps INT32. */ #define INPUT_2BYTES(cinfo,V,action) \ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ bytes_in_buffer--; \ V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \ MAKE_BYTE_AVAIL(cinfo,action); \ bytes_in_buffer--; \ V += GETJOCTET(*next_input_byte++); ) /* * Routines to process JPEG markers. * * Entry condition: JPEG marker itself has been read and its code saved * in cinfo->unread_marker; input restart point is just after the marker. * * Exit: if return TRUE, have read and processed any parameters, and have * updated the restart point to point after the parameters. * If return FALSE, was forced to suspend before reaching end of * marker parameters; restart point has not been moved. Same routine * will be called again after application supplies more input data. * * This approach to suspension assumes that all of a marker's parameters * can fit into a single input bufferload. This should hold for "normal" * markers. Some COM/APPn markers might have large parameter segments * that might not fit. If we are simply dropping such a marker, we use * skip_input_data to get past it, and thereby put the problem on the * source manager's shoulders. If we are saving the marker's contents * into memory, we use a slightly different convention: when forced to * suspend, the marker processor updates the restart point to the end of * what it's consumed (ie, the end of the buffer) before returning FALSE. * On resumption, cinfo->unread_marker still contains the marker code, * but the data source will point to the next chunk of marker data. * The marker processor must retain internal state to deal with this. * * Note that we don't bother to avoid duplicate trace messages if a * suspension occurs within marker parameters. Other side effects * require more care. */ LOCAL(boolean) get_soi (j_decompress_ptr cinfo) /* Process an SOI marker */ { int i; TRACEMS(cinfo, 1, JTRC_SOI); if (cinfo->marker->saw_SOI) ERREXIT(cinfo, JERR_SOI_DUPLICATE); /* Reset all parameters that are defined to be reset by SOI */ for (i = 0; i < NUM_ARITH_TBLS; i++) { cinfo->arith_dc_L[i] = 0; cinfo->arith_dc_U[i] = 1; cinfo->arith_ac_K[i] = 5; } cinfo->restart_interval = 0; /* Set initial assumptions for colorspace etc */ cinfo->jpeg_color_space = JCS_UNKNOWN; cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */ cinfo->saw_JFIF_marker = FALSE; cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */ cinfo->JFIF_minor_version = 1; cinfo->density_unit = 0; cinfo->X_density = 1; cinfo->Y_density = 1; cinfo->saw_Adobe_marker = FALSE; cinfo->Adobe_transform = 0; cinfo->marker->saw_SOI = TRUE; return TRUE; } LOCAL(boolean) get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith) /* Process a SOFn marker */ { INT32 length; int c, ci; jpeg_component_info * compptr; INPUT_VARS(cinfo); cinfo->progressive_mode = is_prog; cinfo->arith_code = is_arith; INPUT_2BYTES(cinfo, length, return FALSE); INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE); INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE); INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE); INPUT_BYTE(cinfo, cinfo->num_components, return FALSE); length -= 8; TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker, (int) cinfo->image_width, (int) cinfo->image_height, cinfo->num_components); if (cinfo->marker->saw_SOF) ERREXIT(cinfo, JERR_SOF_DUPLICATE); /* We don't support files in which the image height is initially specified */ /* as 0 and is later redefined by DNL. As long as we have to check that, */ /* might as well have a general sanity check. */ if (cinfo->image_height <= 0 || cinfo->image_width <= 0 || cinfo->num_components <= 0) ERREXIT(cinfo, JERR_EMPTY_IMAGE); if (length != (cinfo->num_components * 3)) ERREXIT(cinfo, JERR_BAD_LENGTH); if (cinfo->comp_info == NULL) /* do only once, even if suspend */ cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->num_components * SIZEOF(jpeg_component_info)); for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { compptr->component_index = ci; INPUT_BYTE(cinfo, compptr->component_id, return FALSE); INPUT_BYTE(cinfo, c, return FALSE); compptr->h_samp_factor = (c >> 4) & 15; compptr->v_samp_factor = (c ) & 15; INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE); TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT, compptr->component_id, compptr->h_samp_factor, compptr->v_samp_factor, compptr->quant_tbl_no); } cinfo->marker->saw_SOF = TRUE; INPUT_SYNC(cinfo); return TRUE; } LOCAL(boolean) get_sos (j_decompress_ptr cinfo) /* Process a SOS marker */ { INT32 length; int i, ci, n, c, cc; jpeg_component_info * compptr; INPUT_VARS(cinfo); if (! cinfo->marker->saw_SOF) ERREXIT(cinfo, JERR_SOS_NO_SOF); INPUT_2BYTES(cinfo, length, return FALSE); INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */ TRACEMS1(cinfo, 1, JTRC_SOS, n); if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN) ERREXIT(cinfo, JERR_BAD_LENGTH); cinfo->comps_in_scan = n; /* Collect the component-spec parameters */ for (i = 0; i < cinfo->num_components; i++) cinfo->cur_comp_info[i] = NULL; for (i = 0; i < n; i++) { INPUT_BYTE(cinfo, cc, return FALSE); INPUT_BYTE(cinfo, c, return FALSE); for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (cc == compptr->component_id && !cinfo->cur_comp_info[ci]) goto id_found; } ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc); id_found: cinfo->cur_comp_info[i] = compptr; compptr->dc_tbl_no = (c >> 4) & 15; compptr->ac_tbl_no = (c ) & 15; TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc, compptr->dc_tbl_no, compptr->ac_tbl_no); } /* Collect the additional scan parameters Ss, Se, Ah/Al. */ INPUT_BYTE(cinfo, c, return FALSE); cinfo->Ss = c; INPUT_BYTE(cinfo, c, return FALSE); cinfo->Se = c; INPUT_BYTE(cinfo, c, return FALSE); cinfo->Ah = (c >> 4) & 15; cinfo->Al = (c ) & 15; TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); /* Prepare to scan data & restart markers */ cinfo->marker->next_restart_num = 0; /* Count another SOS marker */ cinfo->input_scan_number++; INPUT_SYNC(cinfo); return TRUE; } #ifdef D_ARITH_CODING_SUPPORTED LOCAL(boolean) get_dac (j_decompress_ptr cinfo) /* Process a DAC marker */ { INT32 length; int index, val; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; while (length > 0) { INPUT_BYTE(cinfo, index, return FALSE); INPUT_BYTE(cinfo, val, return FALSE); length -= 2; TRACEMS2(cinfo, 1, JTRC_DAC, index, val); if (index < 0 || index >= (2*NUM_ARITH_TBLS)) ERREXIT1(cinfo, JERR_DAC_INDEX, index); if (index >= NUM_ARITH_TBLS) { /* define AC table */ cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val; } else { /* define DC table */ cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F); cinfo->arith_dc_U[index] = (UINT8) (val >> 4); if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index]) ERREXIT1(cinfo, JERR_DAC_VALUE, val); } } if (length != 0) ERREXIT(cinfo, JERR_BAD_LENGTH); INPUT_SYNC(cinfo); return TRUE; } #else /* ! D_ARITH_CODING_SUPPORTED */ #define get_dac(cinfo) skip_variable(cinfo) #endif /* D_ARITH_CODING_SUPPORTED */ LOCAL(boolean) get_dht (j_decompress_ptr cinfo) /* Process a DHT marker */ { INT32 length; UINT8 bits[17]; UINT8 huffval[256]; int i, index, count; JHUFF_TBL **htblptr; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; while (length > 16) { INPUT_BYTE(cinfo, index, return FALSE); TRACEMS1(cinfo, 1, JTRC_DHT, index); bits[0] = 0; count = 0; for (i = 1; i <= 16; i++) { INPUT_BYTE(cinfo, bits[i], return FALSE); count += bits[i]; } length -= 1 + 16; TRACEMS8(cinfo, 2, JTRC_HUFFBITS, bits[1], bits[2], bits[3], bits[4], bits[5], bits[6], bits[7], bits[8]); TRACEMS8(cinfo, 2, JTRC_HUFFBITS, bits[9], bits[10], bits[11], bits[12], bits[13], bits[14], bits[15], bits[16]); /* Here we just do minimal validation of the counts to avoid walking * off the end of our table space. jdhuff.c will check more carefully. */ if (count > 256 || ((INT32) count) > length) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); for (i = 0; i < count; i++) INPUT_BYTE(cinfo, huffval[i], return FALSE); length -= count; if (index & 0x10) { /* AC table definition */ index -= 0x10; htblptr = &cinfo->ac_huff_tbl_ptrs[index]; } else { /* DC table definition */ htblptr = &cinfo->dc_huff_tbl_ptrs[index]; } if (index < 0 || index >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_DHT_INDEX, index); if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval)); } if (length != 0) ERREXIT(cinfo, JERR_BAD_LENGTH); INPUT_SYNC(cinfo); return TRUE; } LOCAL(boolean) get_dqt (j_decompress_ptr cinfo) /* Process a DQT marker */ { INT32 length; int n, i, prec; unsigned int tmp; JQUANT_TBL *quant_ptr; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; while (length > 0) { INPUT_BYTE(cinfo, n, return FALSE); prec = n >> 4; n &= 0x0F; TRACEMS2(cinfo, 1, JTRC_DQT, n, prec); if (n >= NUM_QUANT_TBLS) ERREXIT1(cinfo, JERR_DQT_INDEX, n); if (cinfo->quant_tbl_ptrs[n] == NULL) cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo); quant_ptr = cinfo->quant_tbl_ptrs[n]; for (i = 0; i < DCTSIZE2; i++) { if (prec) INPUT_2BYTES(cinfo, tmp, return FALSE); else INPUT_BYTE(cinfo, tmp, return FALSE); /* We convert the zigzag-order table to natural array order. */ quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp; } if (cinfo->err->trace_level >= 2) { for (i = 0; i < DCTSIZE2; i += 8) { TRACEMS8(cinfo, 2, JTRC_QUANTVALS, quant_ptr->quantval[i], quant_ptr->quantval[i+1], quant_ptr->quantval[i+2], quant_ptr->quantval[i+3], quant_ptr->quantval[i+4], quant_ptr->quantval[i+5], quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]); } } length -= DCTSIZE2+1; if (prec) length -= DCTSIZE2; } if (length != 0) ERREXIT(cinfo, JERR_BAD_LENGTH); INPUT_SYNC(cinfo); return TRUE; } LOCAL(boolean) get_dri (j_decompress_ptr cinfo) /* Process a DRI marker */ { INT32 length; unsigned int tmp; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); if (length != 4) ERREXIT(cinfo, JERR_BAD_LENGTH); INPUT_2BYTES(cinfo, tmp, return FALSE); TRACEMS1(cinfo, 1, JTRC_DRI, tmp); cinfo->restart_interval = tmp; INPUT_SYNC(cinfo); return TRUE; } /* * Routines for processing APPn and COM markers. * These are either saved in memory or discarded, per application request. * APP0 and APP14 are specially checked to see if they are * JFIF and Adobe markers, respectively. */ #define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */ #define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */ #define APPN_DATA_LEN 14 /* Must be the largest of the above!! */ LOCAL(void) examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data, unsigned int datalen, INT32 remaining) /* Examine first few bytes from an APP0. * Take appropriate action if it is a JFIF marker. * datalen is # of bytes at data[], remaining is length of rest of marker data. */ { INT32 totallen = (INT32) datalen + remaining; if (datalen >= APP0_DATA_LEN && GETJOCTET(data[0]) == 0x4A && GETJOCTET(data[1]) == 0x46 && GETJOCTET(data[2]) == 0x49 && GETJOCTET(data[3]) == 0x46 && GETJOCTET(data[4]) == 0) { /* Found JFIF APP0 marker: save info */ cinfo->saw_JFIF_marker = TRUE; cinfo->JFIF_major_version = GETJOCTET(data[5]); cinfo->JFIF_minor_version = GETJOCTET(data[6]); cinfo->density_unit = GETJOCTET(data[7]); cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]); cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]); /* Check version. * Major version must be 1, anything else signals an incompatible change. * (We used to treat this as an error, but now it's a nonfatal warning, * because some bozo at Hijaak couldn't read the spec.) * Minor version should be 0..2, but process anyway if newer. */ if (cinfo->JFIF_major_version != 1) WARNMS2(cinfo, JWRN_JFIF_MAJOR, cinfo->JFIF_major_version, cinfo->JFIF_minor_version); /* Generate trace messages */ TRACEMS5(cinfo, 1, JTRC_JFIF, cinfo->JFIF_major_version, cinfo->JFIF_minor_version, cinfo->X_density, cinfo->Y_density, cinfo->density_unit); /* Validate thumbnail dimensions and issue appropriate messages */ if (GETJOCTET(data[12]) | GETJOCTET(data[13])) TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, GETJOCTET(data[12]), GETJOCTET(data[13])); totallen -= APP0_DATA_LEN; if (totallen != ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3)) TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen); } else if (datalen >= 6 && GETJOCTET(data[0]) == 0x4A && GETJOCTET(data[1]) == 0x46 && GETJOCTET(data[2]) == 0x58 && GETJOCTET(data[3]) == 0x58 && GETJOCTET(data[4]) == 0) { /* Found JFIF "JFXX" extension APP0 marker */ /* The library doesn't actually do anything with these, * but we try to produce a helpful trace message. */ switch (GETJOCTET(data[5])) { case 0x10: TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen); break; case 0x11: TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen); break; case 0x13: TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen); break; default: TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION, GETJOCTET(data[5]), (int) totallen); break; } } else { /* Start of APP0 does not match "JFIF" or "JFXX", or too short */ TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen); } } LOCAL(void) examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data, unsigned int datalen, INT32 remaining) /* Examine first few bytes from an APP14. * Take appropriate action if it is an Adobe marker. * datalen is # of bytes at data[], remaining is length of rest of marker data. */ { unsigned int version, flags0, flags1, transform; if (datalen >= APP14_DATA_LEN && GETJOCTET(data[0]) == 0x41 && GETJOCTET(data[1]) == 0x64 && GETJOCTET(data[2]) == 0x6F && GETJOCTET(data[3]) == 0x62 && GETJOCTET(data[4]) == 0x65) { /* Found Adobe APP14 marker */ version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]); flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]); flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]); transform = GETJOCTET(data[11]); TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform); cinfo->saw_Adobe_marker = TRUE; cinfo->Adobe_transform = (UINT8) transform; } else { /* Start of APP14 does not match "Adobe", or too short */ TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining)); } } METHODDEF(boolean) get_interesting_appn (j_decompress_ptr cinfo) /* Process an APP0 or APP14 marker without saving it */ { INT32 length; JOCTET b[APPN_DATA_LEN]; unsigned int i, numtoread; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; /* get the interesting part of the marker data */ if (length >= APPN_DATA_LEN) numtoread = APPN_DATA_LEN; else if (length > 0) numtoread = (unsigned int) length; else numtoread = 0; for (i = 0; i < numtoread; i++) INPUT_BYTE(cinfo, b[i], return FALSE); length -= numtoread; /* process it */ switch (cinfo->unread_marker) { case M_APP0: examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length); break; case M_APP14: examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length); break; default: /* can't get here unless jpeg_save_markers chooses wrong processor */ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); break; } /* skip any remaining data -- could be lots */ INPUT_SYNC(cinfo); if (length > 0) (*cinfo->src->skip_input_data) (cinfo, (long) length); return TRUE; } #ifdef SAVE_MARKERS_SUPPORTED METHODDEF(boolean) save_marker (j_decompress_ptr cinfo) /* Save an APPn or COM marker into the marker list */ { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; jpeg_saved_marker_ptr cur_marker = marker->cur_marker; unsigned int bytes_read, data_length; JOCTET FAR * data; INT32 length = 0; INPUT_VARS(cinfo); if (cur_marker == NULL) { /* begin reading a marker */ INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; if (length >= 0) { /* watch out for bogus length word */ /* figure out how much we want to save */ unsigned int limit; if (cinfo->unread_marker == (int) M_COM) limit = marker->length_limit_COM; else limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0]; if ((unsigned int) length < limit) limit = (unsigned int) length; /* allocate and initialize the marker item */ cur_marker = (jpeg_saved_marker_ptr) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(struct jpeg_marker_struct) + limit); cur_marker->next = NULL; cur_marker->marker = (UINT8) cinfo->unread_marker; cur_marker->original_length = (unsigned int) length; cur_marker->data_length = limit; /* data area is just beyond the jpeg_marker_struct */ data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1); marker->cur_marker = cur_marker; marker->bytes_read = 0; bytes_read = 0; data_length = limit; } else { /* deal with bogus length word */ bytes_read = data_length = 0; data = NULL; } } else { /* resume reading a marker */ bytes_read = marker->bytes_read; data_length = cur_marker->data_length; data = cur_marker->data + bytes_read; } while (bytes_read < data_length) { INPUT_SYNC(cinfo); /* move the restart point to here */ marker->bytes_read = bytes_read; /* If there's not at least one byte in buffer, suspend */ MAKE_BYTE_AVAIL(cinfo, return FALSE); /* Copy bytes with reasonable rapidity */ while (bytes_read < data_length && bytes_in_buffer > 0) { *data++ = *next_input_byte++; bytes_in_buffer--; bytes_read++; } } /* Done reading what we want to read */ if (cur_marker != NULL) { /* will be NULL if bogus length word */ /* Add new marker to end of list */ if (cinfo->marker_list == NULL) { cinfo->marker_list = cur_marker; } else { jpeg_saved_marker_ptr prev = cinfo->marker_list; while (prev->next != NULL) prev = prev->next; prev->next = cur_marker; } /* Reset pointer & calc remaining data length */ data = cur_marker->data; length = cur_marker->original_length - data_length; } /* Reset to initial state for next marker */ marker->cur_marker = NULL; /* Process the marker if interesting; else just make a generic trace msg */ switch (cinfo->unread_marker) { case M_APP0: examine_app0(cinfo, data, data_length, length); break; case M_APP14: examine_app14(cinfo, data, data_length, length); break; default: TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) (data_length + length)); break; } /* skip any remaining data -- could be lots */ INPUT_SYNC(cinfo); /* do before skip_input_data */ if (length > 0) (*cinfo->src->skip_input_data) (cinfo, (long) length); return TRUE; } #endif /* SAVE_MARKERS_SUPPORTED */ METHODDEF(boolean) skip_variable (j_decompress_ptr cinfo) /* Skip over an unknown or uninteresting variable-length marker */ { INT32 length; INPUT_VARS(cinfo); INPUT_2BYTES(cinfo, length, return FALSE); length -= 2; TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length); INPUT_SYNC(cinfo); /* do before skip_input_data */ if (length > 0) (*cinfo->src->skip_input_data) (cinfo, (long) length); return TRUE; } /* * Find the next JPEG marker, save it in cinfo->unread_marker. * Returns FALSE if had to suspend before reaching a marker; * in that case cinfo->unread_marker is unchanged. * * Note that the result might not be a valid marker code, * but it will never be 0 or FF. */ LOCAL(boolean) next_marker (j_decompress_ptr cinfo) { int c; INPUT_VARS(cinfo); for (;;) { INPUT_BYTE(cinfo, c, return FALSE); /* Skip any non-FF bytes. * This may look a bit inefficient, but it will not occur in a valid file. * We sync after each discarded byte so that a suspending data source * can discard the byte from its buffer. */ while (c != 0xFF) { cinfo->marker->discarded_bytes++; INPUT_SYNC(cinfo); INPUT_BYTE(cinfo, c, return FALSE); } /* This loop swallows any duplicate FF bytes. Extra FFs are legal as * pad bytes, so don't count them in discarded_bytes. We assume there * will not be so many consecutive FF bytes as to overflow a suspending * data source's input buffer. */ do { INPUT_BYTE(cinfo, c, return FALSE); } while (c == 0xFF); if (c != 0) break; /* found a valid marker, exit loop */ /* Reach here if we found a stuffed-zero data sequence (FF/00). * Discard it and loop back to try again. */ cinfo->marker->discarded_bytes += 2; INPUT_SYNC(cinfo); } if (cinfo->marker->discarded_bytes != 0) { WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c); cinfo->marker->discarded_bytes = 0; } cinfo->unread_marker = c; INPUT_SYNC(cinfo); return TRUE; } LOCAL(boolean) first_marker (j_decompress_ptr cinfo) /* Like next_marker, but used to obtain the initial SOI marker. */ /* For this marker, we do not allow preceding garbage or fill; otherwise, * we might well scan an entire input file before realizing it ain't JPEG. * If an application wants to process non-JFIF files, it must seek to the * SOI before calling the JPEG library. */ { int c, c2; INPUT_VARS(cinfo); INPUT_BYTE(cinfo, c, return FALSE); INPUT_BYTE(cinfo, c2, return FALSE); if (c != 0xFF || c2 != (int) M_SOI) ERREXIT2(cinfo, JERR_NO_SOI, c, c2); cinfo->unread_marker = c2; INPUT_SYNC(cinfo); return TRUE; } /* * Read markers until SOS or EOI. * * Returns same codes as are defined for jpeg_consume_input: * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. */ METHODDEF(int) read_markers (j_decompress_ptr cinfo) { /* Outer loop repeats once for each marker. */ for (;;) { /* Collect the marker proper, unless we already did. */ /* NB: first_marker() enforces the requirement that SOI appear first. */ if (cinfo->unread_marker == 0) { if (! cinfo->marker->saw_SOI) { if (! first_marker(cinfo)) return JPEG_SUSPENDED; } else { if (! next_marker(cinfo)) return JPEG_SUSPENDED; } } /* At this point cinfo->unread_marker contains the marker code and the * input point is just past the marker proper, but before any parameters. * A suspension will cause us to return with this state still true. */ switch (cinfo->unread_marker) { case M_SOI: if (! get_soi(cinfo)) return JPEG_SUSPENDED; break; case M_SOF0: /* Baseline */ case M_SOF1: /* Extended sequential, Huffman */ if (! get_sof(cinfo, FALSE, FALSE)) return JPEG_SUSPENDED; break; case M_SOF2: /* Progressive, Huffman */ if (! get_sof(cinfo, TRUE, FALSE)) return JPEG_SUSPENDED; break; case M_SOF9: /* Extended sequential, arithmetic */ if (! get_sof(cinfo, FALSE, TRUE)) return JPEG_SUSPENDED; break; case M_SOF10: /* Progressive, arithmetic */ if (! get_sof(cinfo, TRUE, TRUE)) return JPEG_SUSPENDED; break; /* Currently unsupported SOFn types */ case M_SOF3: /* Lossless, Huffman */ case M_SOF5: /* Differential sequential, Huffman */ case M_SOF6: /* Differential progressive, Huffman */ case M_SOF7: /* Differential lossless, Huffman */ case M_JPG: /* Reserved for JPEG extensions */ case M_SOF11: /* Lossless, arithmetic */ case M_SOF13: /* Differential sequential, arithmetic */ case M_SOF14: /* Differential progressive, arithmetic */ case M_SOF15: /* Differential lossless, arithmetic */ ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker); break; case M_SOS: if (! get_sos(cinfo)) return JPEG_SUSPENDED; cinfo->unread_marker = 0; /* processed the marker */ return JPEG_REACHED_SOS; case M_EOI: TRACEMS(cinfo, 1, JTRC_EOI); cinfo->unread_marker = 0; /* processed the marker */ return JPEG_REACHED_EOI; case M_DAC: if (! get_dac(cinfo)) return JPEG_SUSPENDED; break; case M_DHT: if (! get_dht(cinfo)) return JPEG_SUSPENDED; break; case M_DQT: if (! get_dqt(cinfo)) return JPEG_SUSPENDED; break; case M_DRI: if (! get_dri(cinfo)) return JPEG_SUSPENDED; break; case M_APP0: case M_APP1: case M_APP2: case M_APP3: case M_APP4: case M_APP5: case M_APP6: case M_APP7: case M_APP8: case M_APP9: case M_APP10: case M_APP11: case M_APP12: case M_APP13: case M_APP14: case M_APP15: if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[ cinfo->unread_marker - (int) M_APP0]) (cinfo)) return JPEG_SUSPENDED; break; case M_COM: if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo)) return JPEG_SUSPENDED; break; case M_RST0: /* these are all parameterless */ case M_RST1: case M_RST2: case M_RST3: case M_RST4: case M_RST5: case M_RST6: case M_RST7: case M_TEM: TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker); break; case M_DNL: /* Ignore DNL ... perhaps the wrong thing */ if (! skip_variable(cinfo)) return JPEG_SUSPENDED; break; default: /* must be DHP, EXP, JPGn, or RESn */ /* For now, we treat the reserved markers as fatal errors since they are * likely to be used to signal incompatible JPEG Part 3 extensions. * Once the JPEG 3 version-number marker is well defined, this code * ought to change! */ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); break; } /* Successfully processed marker, so reset state variable */ cinfo->unread_marker = 0; } /* end loop */ } /* * Read a restart marker, which is expected to appear next in the datastream; * if the marker is not there, take appropriate recovery action. * Returns FALSE if suspension is required. * * This is called by the entropy decoder after it has read an appropriate * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder * has already read a marker from the data source. Under normal conditions * cinfo->unread_marker will be reset to 0 before returning; if not reset, * it holds a marker which the decoder will be unable to read past. */ METHODDEF(boolean) read_restart_marker (j_decompress_ptr cinfo) { /* Obtain a marker unless we already did. */ /* Note that next_marker will complain if it skips any data. */ if (cinfo->unread_marker == 0) { if (! next_marker(cinfo)) return FALSE; } if (cinfo->unread_marker == ((int) M_RST0 + cinfo->marker->next_restart_num)) { /* Normal case --- swallow the marker and let entropy decoder continue */ TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num); cinfo->unread_marker = 0; } else { /* Uh-oh, the restart markers have been messed up. */ /* Let the data source manager determine how to resync. */ if (! (*cinfo->src->resync_to_restart) (cinfo, cinfo->marker->next_restart_num)) return FALSE; } /* Update next-restart state */ cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7; return TRUE; } /* * This is the default resync_to_restart method for data source managers * to use if they don't have any better approach. Some data source managers * may be able to back up, or may have additional knowledge about the data * which permits a more intelligent recovery strategy; such managers would * presumably supply their own resync method. * * read_restart_marker calls resync_to_restart if it finds a marker other than * the restart marker it was expecting. (This code is *not* used unless * a nonzero restart interval has been declared.) cinfo->unread_marker is * the marker code actually found (might be anything, except 0 or FF). * The desired restart marker number (0..7) is passed as a parameter. * This routine is supposed to apply whatever error recovery strategy seems * appropriate in order to position the input stream to the next data segment. * Note that cinfo->unread_marker is treated as a marker appearing before * the current data-source input point; usually it should be reset to zero * before returning. * Returns FALSE if suspension is required. * * This implementation is substantially constrained by wanting to treat the * input as a data stream; this means we can't back up. Therefore, we have * only the following actions to work with: * 1. Simply discard the marker and let the entropy decoder resume at next * byte of file. * 2. Read forward until we find another marker, discarding intervening * data. (In theory we could look ahead within the current bufferload, * without having to discard data if we don't find the desired marker. * This idea is not implemented here, in part because it makes behavior * dependent on buffer size and chance buffer-boundary positions.) * 3. Leave the marker unread (by failing to zero cinfo->unread_marker). * This will cause the entropy decoder to process an empty data segment, * inserting dummy zeroes, and then we will reprocess the marker. * * #2 is appropriate if we think the desired marker lies ahead, while #3 is * appropriate if the found marker is a future restart marker (indicating * that we have missed the desired restart marker, probably because it got * corrupted). * We apply #2 or #3 if the found marker is a restart marker no more than * two counts behind or ahead of the expected one. We also apply #2 if the * found marker is not a legal JPEG marker code (it's certainly bogus data). * If the found marker is a restart marker more than 2 counts away, we do #1 * (too much risk that the marker is erroneous; with luck we will be able to * resync at some future point). * For any valid non-restart JPEG marker, we apply #3. This keeps us from * overrunning the end of a scan. An implementation limited to single-scan * files might find it better to apply #2 for markers other than EOI, since * any other marker would have to be bogus data in that case. */ GLOBAL(boolean) jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired) { int marker = cinfo->unread_marker; int action = 1; /* Always put up a warning. */ WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired); /* Outer loop handles repeated decision after scanning forward. */ for (;;) { if (marker < (int) M_SOF0) action = 2; /* invalid marker */ else if (marker < (int) M_RST0 || marker > (int) M_RST7) action = 3; /* valid non-restart marker */ else { if (marker == ((int) M_RST0 + ((desired+1) & 7)) || marker == ((int) M_RST0 + ((desired+2) & 7))) action = 3; /* one of the next two expected restarts */ else if (marker == ((int) M_RST0 + ((desired-1) & 7)) || marker == ((int) M_RST0 + ((desired-2) & 7))) action = 2; /* a prior restart, so advance */ else action = 1; /* desired restart or too far away */ } TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action); switch (action) { case 1: /* Discard marker and let entropy decoder resume processing. */ cinfo->unread_marker = 0; return TRUE; case 2: /* Scan to the next marker, and repeat the decision loop. */ if (! next_marker(cinfo)) return FALSE; marker = cinfo->unread_marker; break; case 3: /* Return without advancing past this marker. */ /* Entropy decoder will be forced to process an empty segment. */ return TRUE; } } /* end loop */ } /* * Reset marker processing state to begin a fresh datastream. */ METHODDEF(void) reset_marker_reader (j_decompress_ptr cinfo) { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; cinfo->comp_info = NULL; /* until allocated by get_sof */ cinfo->input_scan_number = 0; /* no SOS seen yet */ cinfo->unread_marker = 0; /* no pending marker */ marker->pub.saw_SOI = FALSE; /* set internal state too */ marker->pub.saw_SOF = FALSE; marker->pub.discarded_bytes = 0; marker->cur_marker = NULL; } /* * Initialize the marker reader module. * This is called only once, when the decompression object is created. */ GLOBAL(void) jinit_marker_reader (j_decompress_ptr cinfo) { my_marker_ptr marker; int i; /* Create subobject in permanent pool */ marker = (my_marker_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_marker_reader)); cinfo->marker = (struct jpeg_marker_reader *) marker; /* Initialize public method pointers */ marker->pub.reset_marker_reader = reset_marker_reader; marker->pub.read_markers = read_markers; marker->pub.read_restart_marker = read_restart_marker; /* Initialize COM/APPn processing. * By default, we examine and then discard APP0 and APP14, * but simply discard COM and all other APPn. */ marker->process_COM = skip_variable; marker->length_limit_COM = 0; for (i = 0; i < 16; i++) { marker->process_APPn[i] = skip_variable; marker->length_limit_APPn[i] = 0; } marker->process_APPn[0] = get_interesting_appn; marker->process_APPn[14] = get_interesting_appn; /* Reset marker processing state */ reset_marker_reader(cinfo); } /* * Control saving of COM and APPn markers into marker_list. */ #ifdef SAVE_MARKERS_SUPPORTED GLOBAL(void) jpeg_save_markers (j_decompress_ptr cinfo, int marker_code, unsigned int length_limit) { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; long maxlength; jpeg_marker_parser_method processor; /* Length limit mustn't be larger than what we can allocate * (should only be a concern in a 16-bit environment). */ maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct); if (((long) length_limit) > maxlength) length_limit = (unsigned int) maxlength; /* Choose processor routine to use. * APP0/APP14 have special requirements. */ if (length_limit) { processor = save_marker; /* If saving APP0/APP14, save at least enough for our internal use. */ if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN) length_limit = APP0_DATA_LEN; else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN) length_limit = APP14_DATA_LEN; } else { processor = skip_variable; /* If discarding APP0/APP14, use our regular on-the-fly processor. */ if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14) processor = get_interesting_appn; } if (marker_code == (int) M_COM) { marker->process_COM = processor; marker->length_limit_COM = length_limit; } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) { marker->process_APPn[marker_code - (int) M_APP0] = processor; marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit; } else ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code); } #endif /* SAVE_MARKERS_SUPPORTED */ /* * Install a special processing method for COM or APPn markers. */ GLOBAL(void) jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code, jpeg_marker_parser_method routine) { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; if (marker_code == (int) M_COM) marker->process_COM = routine; else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) marker->process_APPn[marker_code - (int) M_APP0] = routine; else ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code); } glmark2-2012.08/./src/libjpeg-turbo/jdphuff.c0000664000175000017500000005011712013417376020050 0ustar alfalf00000000000000/* * jdphuff.c * * Copyright (C) 1995-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains Huffman entropy decoding routines for progressive JPEG. * * Much of the complexity here has to do with supporting input suspension. * If the data source module demands suspension, we want to be able to back * up to the start of the current MCU. To do this, we copy state variables * into local working storage, and update them back to the permanent * storage only upon successful completion of an MCU. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdhuff.h" /* Declarations shared with jdhuff.c */ #ifdef D_PROGRESSIVE_SUPPORTED /* * Expanded entropy decoder object for progressive Huffman decoding. * * The savable_state subrecord contains fields that change within an MCU, * but must not be updated permanently until we complete the MCU. */ typedef struct { unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ } savable_state; /* This macro is to work around compilers with missing or broken * structure assignment. You'll need to fix this code if you have * such a compiler and you change MAX_COMPS_IN_SCAN. */ #ifndef NO_STRUCT_ASSIGN #define ASSIGN_STATE(dest,src) ((dest) = (src)) #else #if MAX_COMPS_IN_SCAN == 4 #define ASSIGN_STATE(dest,src) \ ((dest).EOBRUN = (src).EOBRUN, \ (dest).last_dc_val[0] = (src).last_dc_val[0], \ (dest).last_dc_val[1] = (src).last_dc_val[1], \ (dest).last_dc_val[2] = (src).last_dc_val[2], \ (dest).last_dc_val[3] = (src).last_dc_val[3]) #endif #endif typedef struct { struct jpeg_entropy_decoder pub; /* public fields */ /* These fields are loaded into local variables at start of each MCU. * In case of suspension, we exit WITHOUT updating them. */ bitread_perm_state bitstate; /* Bit buffer at start of MCU */ savable_state saved; /* Other state at start of MCU */ /* These fields are NOT loaded into local working state. */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ /* Pointers to derived tables (these workspaces have image lifespan) */ d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ } phuff_entropy_decoder; typedef phuff_entropy_decoder * phuff_entropy_ptr; /* Forward declarations */ METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, JBLOCKROW *MCU_data)); /* * Initialize for a Huffman-compressed scan. */ METHODDEF(void) start_pass_phuff_decoder (j_decompress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; boolean is_DC_band, bad; int ci, coefi, tbl; int *coef_bit_ptr; jpeg_component_info * compptr; is_DC_band = (cinfo->Ss == 0); /* Validate scan parameters */ bad = FALSE; if (is_DC_band) { if (cinfo->Se != 0) bad = TRUE; } else { /* need not check Ss/Se < 0 since they came from unsigned bytes */ if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) bad = TRUE; /* AC scans may have only one component */ if (cinfo->comps_in_scan != 1) bad = TRUE; } if (cinfo->Ah != 0) { /* Successive approximation refinement scan: must have Al = Ah-1. */ if (cinfo->Al != cinfo->Ah-1) bad = TRUE; } if (cinfo->Al > 13) /* need not check for < 0 */ bad = TRUE; /* Arguably the maximum Al value should be less than 13 for 8-bit precision, * but the spec doesn't say so, and we try to be liberal about what we * accept. Note: large Al values could result in out-of-range DC * coefficients during early scans, leading to bizarre displays due to * overflows in the IDCT math. But we won't crash. */ if (bad) ERREXIT4(cinfo, JERR_BAD_PROGRESSION, cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); /* Update progression status, and verify that scan order is legal. * Note that inter-scan inconsistencies are treated as warnings * not fatal errors ... not clear if this is right way to behave. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { int cindex = cinfo->cur_comp_info[ci]->component_index; coef_bit_ptr = & cinfo->coef_bits[cindex][0]; if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; if (cinfo->Ah != expected) WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); coef_bit_ptr[coefi] = cinfo->Al; } } /* Select MCU decoding routine */ if (cinfo->Ah == 0) { if (is_DC_band) entropy->pub.decode_mcu = decode_mcu_DC_first; else entropy->pub.decode_mcu = decode_mcu_AC_first; } else { if (is_DC_band) entropy->pub.decode_mcu = decode_mcu_DC_refine; else entropy->pub.decode_mcu = decode_mcu_AC_refine; } for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Make sure requested tables are present, and compute derived tables. * We may build same derived table more than once, but it's not expensive. */ if (is_DC_band) { if (cinfo->Ah == 0) { /* DC refinement needs no table */ tbl = compptr->dc_tbl_no; jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, & entropy->derived_tbls[tbl]); } } else { tbl = compptr->ac_tbl_no; jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, & entropy->derived_tbls[tbl]); /* remember the single active table */ entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; } /* Initialize DC predictions to 0 */ entropy->saved.last_dc_val[ci] = 0; } /* Initialize bitread state variables */ entropy->bitstate.bits_left = 0; entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ entropy->pub.insufficient_data = FALSE; /* Initialize private state variables */ entropy->saved.EOBRUN = 0; /* Initialize restart counter */ entropy->restarts_to_go = cinfo->restart_interval; } /* * Figure F.12: extend sign bit. * On some machines, a shift and add will be faster than a table lookup. */ #ifdef AVOID_TABLES #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) #else #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) static const int extend_test[16] = /* entry n is 2**(n-1) */ { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; #endif /* AVOID_TABLES */ /* * Check for a restart marker & resynchronize decoder. * Returns FALSE if must suspend. */ LOCAL(boolean) process_restart (j_decompress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int ci; /* Throw away any unused bits remaining in bit buffer; */ /* include any full bytes in next_marker's count of discarded bytes */ cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; entropy->bitstate.bits_left = 0; /* Advance past the RSTn marker */ if (! (*cinfo->marker->read_restart_marker) (cinfo)) return FALSE; /* Re-initialize DC predictions to 0 */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) entropy->saved.last_dc_val[ci] = 0; /* Re-init EOB run count, too */ entropy->saved.EOBRUN = 0; /* Reset restart counter */ entropy->restarts_to_go = cinfo->restart_interval; /* Reset out-of-data flag, unless read_restart_marker left us smack up * against a marker. In that case we will end up treating the next data * segment as empty, and we can avoid producing bogus output pixels by * leaving the flag set. */ if (cinfo->unread_marker == 0) entropy->pub.insufficient_data = FALSE; return TRUE; } /* * Huffman MCU decoding. * Each of these routines decodes and returns one MCU's worth of * Huffman-compressed coefficients. * The coefficients are reordered from zigzag order into natural array order, * but are not dequantized. * * The i'th block of the MCU is stored into the block pointed to by * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. * * We return FALSE if data source requested suspension. In that case no * changes have been made to permanent state. (Exception: some output * coefficients may already have been assigned. This is harmless for * spectral selection, since we'll just re-assign them on the next call. * Successive approximation AC refinement has to be more careful, however.) */ /* * MCU decoding for DC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Al = cinfo->Al; register int s, r; int blkn, ci; JBLOCKROW block; BITREAD_STATE_VARS; savable_state state; d_derived_tbl * tbl; jpeg_component_info * compptr; /* Process restart marker if needed; may have to suspend */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! process_restart(cinfo)) return FALSE; } /* If we've run out of data, just leave the MCU set to zeroes. * This way, we return uniform gray for the remainder of the segment. */ if (! entropy->pub.insufficient_data) { /* Load up working state */ BITREAD_LOAD_STATE(cinfo,entropy->bitstate); ASSIGN_STATE(state, entropy->saved); /* Outer loop handles each block in the MCU */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; tbl = entropy->derived_tbls[compptr->dc_tbl_no]; /* Decode a single block's worth of coefficients */ /* Section F.2.2.1: decode the DC coefficient difference */ HUFF_DECODE(s, br_state, tbl, return FALSE, label1); if (s) { CHECK_BIT_BUFFER(br_state, s, return FALSE); r = GET_BITS(s); s = HUFF_EXTEND(r, s); } /* Convert DC difference to actual value, update last_dc_val */ s += state.last_dc_val[ci]; state.last_dc_val[ci] = s; /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ (*block)[0] = (JCOEF) (s << Al); } /* Completed MCU, so update state */ BITREAD_SAVE_STATE(cinfo,entropy->bitstate); ASSIGN_STATE(entropy->saved, state); } /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; return TRUE; } /* * MCU decoding for AC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Se = cinfo->Se; int Al = cinfo->Al; register int s, k, r; unsigned int EOBRUN; JBLOCKROW block; BITREAD_STATE_VARS; d_derived_tbl * tbl; /* Process restart marker if needed; may have to suspend */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! process_restart(cinfo)) return FALSE; } /* If we've run out of data, just leave the MCU set to zeroes. * This way, we return uniform gray for the remainder of the segment. */ if (! entropy->pub.insufficient_data) { /* Load up working state. * We can avoid loading/saving bitread state if in an EOB run. */ EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ /* There is always only one block per MCU */ if (EOBRUN > 0) /* if it's a band of zeroes... */ EOBRUN--; /* ...process it now (we do nothing) */ else { BITREAD_LOAD_STATE(cinfo,entropy->bitstate); block = MCU_data[0]; tbl = entropy->ac_derived_tbl; for (k = cinfo->Ss; k <= Se; k++) { HUFF_DECODE(s, br_state, tbl, return FALSE, label2); r = s >> 4; s &= 15; if (s) { k += r; CHECK_BIT_BUFFER(br_state, s, return FALSE); r = GET_BITS(s); s = HUFF_EXTEND(r, s); /* Scale and output coefficient in natural (dezigzagged) order */ (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); } else { if (r == 15) { /* ZRL */ k += 15; /* skip 15 zeroes in band */ } else { /* EOBr, run length is 2^r + appended bits */ EOBRUN = 1 << r; if (r) { /* EOBr, r > 0 */ CHECK_BIT_BUFFER(br_state, r, return FALSE); r = GET_BITS(r); EOBRUN += r; } EOBRUN--; /* this band is processed at this moment */ break; /* force end-of-band */ } } } BITREAD_SAVE_STATE(cinfo,entropy->bitstate); } /* Completed MCU, so update state */ entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ } /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; return TRUE; } /* * MCU decoding for DC successive approximation refinement scan. * Note: we assume such scans can be multi-component, although the spec * is not very clear on the point. */ METHODDEF(boolean) decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ int blkn; JBLOCKROW block; BITREAD_STATE_VARS; /* Process restart marker if needed; may have to suspend */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! process_restart(cinfo)) return FALSE; } /* Not worth the cycles to check insufficient_data here, * since we will not change the data anyway if we read zeroes. */ /* Load up working state */ BITREAD_LOAD_STATE(cinfo,entropy->bitstate); /* Outer loop handles each block in the MCU */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; /* Encoded data is simply the next bit of the two's-complement DC value */ CHECK_BIT_BUFFER(br_state, 1, return FALSE); if (GET_BITS(1)) (*block)[0] |= p1; /* Note: since we use |=, repeating the assignment later is safe */ } /* Completed MCU, so update state */ BITREAD_SAVE_STATE(cinfo,entropy->bitstate); /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; return TRUE; } /* * MCU decoding for AC successive approximation refinement scan. */ METHODDEF(boolean) decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; int Se = cinfo->Se; int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ register int s, k, r; unsigned int EOBRUN; JBLOCKROW block; JCOEFPTR thiscoef; BITREAD_STATE_VARS; d_derived_tbl * tbl; int num_newnz; int newnz_pos[DCTSIZE2]; /* Process restart marker if needed; may have to suspend */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! process_restart(cinfo)) return FALSE; } /* If we've run out of data, don't modify the MCU. */ if (! entropy->pub.insufficient_data) { /* Load up working state */ BITREAD_LOAD_STATE(cinfo,entropy->bitstate); EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ /* There is always only one block per MCU */ block = MCU_data[0]; tbl = entropy->ac_derived_tbl; /* If we are forced to suspend, we must undo the assignments to any newly * nonzero coefficients in the block, because otherwise we'd get confused * next time about which coefficients were already nonzero. * But we need not undo addition of bits to already-nonzero coefficients; * instead, we can test the current bit to see if we already did it. */ num_newnz = 0; /* initialize coefficient loop counter to start of band */ k = cinfo->Ss; if (EOBRUN == 0) { for (; k <= Se; k++) { HUFF_DECODE(s, br_state, tbl, goto undoit, label3); r = s >> 4; s &= 15; if (s) { if (s != 1) /* size of new coef should always be 1 */ WARNMS(cinfo, JWRN_HUFF_BAD_CODE); CHECK_BIT_BUFFER(br_state, 1, goto undoit); if (GET_BITS(1)) s = p1; /* newly nonzero coef is positive */ else s = m1; /* newly nonzero coef is negative */ } else { if (r != 15) { EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ if (r) { CHECK_BIT_BUFFER(br_state, r, goto undoit); r = GET_BITS(r); EOBRUN += r; } break; /* rest of block is handled by EOB logic */ } /* note s = 0 for processing ZRL */ } /* Advance over already-nonzero coefs and r still-zero coefs, * appending correction bits to the nonzeroes. A correction bit is 1 * if the absolute value of the coefficient must be increased. */ do { thiscoef = *block + jpeg_natural_order[k]; if (*thiscoef != 0) { CHECK_BIT_BUFFER(br_state, 1, goto undoit); if (GET_BITS(1)) { if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ if (*thiscoef >= 0) *thiscoef += p1; else *thiscoef += m1; } } } else { if (--r < 0) break; /* reached target zero coefficient */ } k++; } while (k <= Se); if (s) { int pos = jpeg_natural_order[k]; /* Output newly nonzero coefficient */ (*block)[pos] = (JCOEF) s; /* Remember its position in case we have to suspend */ newnz_pos[num_newnz++] = pos; } } } if (EOBRUN > 0) { /* Scan any remaining coefficient positions after the end-of-band * (the last newly nonzero coefficient, if any). Append a correction * bit to each already-nonzero coefficient. A correction bit is 1 * if the absolute value of the coefficient must be increased. */ for (; k <= Se; k++) { thiscoef = *block + jpeg_natural_order[k]; if (*thiscoef != 0) { CHECK_BIT_BUFFER(br_state, 1, goto undoit); if (GET_BITS(1)) { if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ if (*thiscoef >= 0) *thiscoef += p1; else *thiscoef += m1; } } } } /* Count one block completed in EOB run */ EOBRUN--; } /* Completed MCU, so update state */ BITREAD_SAVE_STATE(cinfo,entropy->bitstate); entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ } /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; return TRUE; undoit: /* Re-zero any output coefficients that we made newly nonzero */ while (num_newnz > 0) (*block)[newnz_pos[--num_newnz]] = 0; return FALSE; } /* * Module initialization routine for progressive Huffman entropy decoding. */ GLOBAL(void) jinit_phuff_decoder (j_decompress_ptr cinfo) { phuff_entropy_ptr entropy; int *coef_bit_ptr; int ci, i; entropy = (phuff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(phuff_entropy_decoder)); cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; entropy->pub.start_pass = start_pass_phuff_decoder; /* Mark derived tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->derived_tbls[i] = NULL; } /* Create progression status table */ cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->num_components*DCTSIZE2*SIZEOF(int)); coef_bit_ptr = & cinfo->coef_bits[0][0]; for (ci = 0; ci < cinfo->num_components; ci++) for (i = 0; i < DCTSIZE2; i++) *coef_bit_ptr++ = -1; } #endif /* D_PROGRESSIVE_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jdmerge.c0000664000175000017500000003572012013417376020042 0ustar alfalf00000000000000/* * jdmerge.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2009, 2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains code for merged upsampling/color conversion. * * This file combines functions from jdsample.c and jdcolor.c; * read those files first to understand what's going on. * * When the chroma components are to be upsampled by simple replication * (ie, box filtering), we can save some work in color conversion by * calculating all the output pixels corresponding to a pair of chroma * samples at one time. In the conversion equations * R = Y + K1 * Cr * G = Y + K2 * Cb + K3 * Cr * B = Y + K4 * Cb * only the Y term varies among the group of pixels corresponding to a pair * of chroma samples, so the rest of the terms can be calculated just once. * At typical sampling ratios, this eliminates half or three-quarters of the * multiplications needed for color conversion. * * This file currently provides implementations for the following cases: * YCbCr => RGB color conversion only. * Sampling ratios of 2h1v or 2h2v. * No scaling needed at upsample time. * Corner-aligned (non-CCIR601) sampling alignment. * Other special cases could be added, but in most applications these are * the only common cases. (For uncommon cases we fall back on the more * general code in jdsample.c and jdcolor.c.) */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jsimd.h" #include "config.h" #ifdef UPSAMPLE_MERGING_SUPPORTED /* Private subobject */ typedef struct { struct jpeg_upsampler pub; /* public fields */ /* Pointer to routine to do actual upsampling/conversion of one row group */ JMETHOD(void, upmethod, (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); /* Private state for YCC->RGB conversion */ int * Cr_r_tab; /* => table for Cr to R conversion */ int * Cb_b_tab; /* => table for Cb to B conversion */ INT32 * Cr_g_tab; /* => table for Cr to G conversion */ INT32 * Cb_g_tab; /* => table for Cb to G conversion */ /* For 2:1 vertical sampling, we produce two output rows at a time. * We need a "spare" row buffer to hold the second output row if the * application provides just a one-row buffer; we also use the spare * to discard the dummy last row if the image height is odd. */ JSAMPROW spare_row; boolean spare_full; /* T if spare buffer is occupied */ JDIMENSION out_row_width; /* samples per output row */ JDIMENSION rows_to_go; /* counts rows remaining in image */ } my_upsampler; typedef my_upsampler * my_upsample_ptr; #define SCALEBITS 16 /* speediest right-shift on some machines */ #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) #define FIX(x) ((INT32) ((x) * (1L<RGB colorspace conversion. * This is taken directly from jdcolor.c; see that file for more info. */ LOCAL(void) build_ycc_rgb_table (j_decompress_ptr cinfo) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; int i; INT32 x; SHIFT_TEMPS upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int)); upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int)); upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32)); upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32)); for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* Cr=>R value is nearest int to 1.40200 * x */ upsample->Cr_r_tab[i] = (int) RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); /* Cb=>B value is nearest int to 1.77200 * x */ upsample->Cb_b_tab[i] = (int) RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); /* Cr=>G value is scaled-up -0.71414 * x */ upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; /* Cb=>G value is scaled-up -0.34414 * x */ /* We also add in ONE_HALF so that need not do it in inner loop */ upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; } } /* * Initialize for an upsampling pass. */ METHODDEF(void) start_pass_merged_upsample (j_decompress_ptr cinfo) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; /* Mark the spare buffer empty */ upsample->spare_full = FALSE; /* Initialize total-height counter for detecting bottom of image */ upsample->rows_to_go = cinfo->output_height; } /* * Control routine to do upsampling (and color conversion). * * The control routine just handles the row buffering considerations. */ METHODDEF(void) merged_2v_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) /* 2:1 vertical sampling case: may need a spare row. */ { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; JSAMPROW work_ptrs[2]; JDIMENSION num_rows; /* number of rows returned to caller */ if (upsample->spare_full) { /* If we have a spare row saved from a previous cycle, just return it. */ jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1, upsample->out_row_width); num_rows = 1; upsample->spare_full = FALSE; } else { /* Figure number of rows to return to caller. */ num_rows = 2; /* Not more than the distance to the end of the image. */ if (num_rows > upsample->rows_to_go) num_rows = upsample->rows_to_go; /* And not more than what the client can accept: */ out_rows_avail -= *out_row_ctr; if (num_rows > out_rows_avail) num_rows = out_rows_avail; /* Create output pointer array for upsampler. */ work_ptrs[0] = output_buf[*out_row_ctr]; if (num_rows > 1) { work_ptrs[1] = output_buf[*out_row_ctr + 1]; } else { work_ptrs[1] = upsample->spare_row; upsample->spare_full = TRUE; } /* Now do the upsampling. */ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); } /* Adjust counts */ *out_row_ctr += num_rows; upsample->rows_to_go -= num_rows; /* When the buffer is emptied, declare this input row group consumed */ if (! upsample->spare_full) (*in_row_group_ctr)++; } METHODDEF(void) merged_1v_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) /* 1:1 vertical sampling case: much easier, never need a spare row. */ { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; /* Just do the upsampling. */ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, output_buf + *out_row_ctr); /* Adjust counts */ (*out_row_ctr)++; (*in_row_group_ctr)++; } /* * These are the routines invoked by the control routines to do * the actual upsampling/conversion. One row group is processed per call. * * Note: since we may be writing directly into application-supplied buffers, * we have to be honest about the output width; we can't assume the buffer * has been rounded up to an even width. */ /* * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. */ METHODDEF(void) h2v1_merged_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { switch (cinfo->out_color_space) { case JCS_EXT_RGB: extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_BGR: extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; default: h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; } } /* * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. */ METHODDEF(void) h2v2_merged_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { switch (cinfo->out_color_space) { case JCS_EXT_RGB: extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_BGR: extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; default: h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, output_buf); break; } } /* * Module initialization routine for merged upsampling/color conversion. * * NB: this is called under the conditions determined by use_merged_upsample() * in jdmaster.c. That routine MUST correspond to the actual capabilities * of this module; no safety checks are made here. */ GLOBAL(void) jinit_merged_upsampler (j_decompress_ptr cinfo) { my_upsample_ptr upsample; upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler)); cinfo->upsample = (struct jpeg_upsampler *) upsample; upsample->pub.start_pass = start_pass_merged_upsample; upsample->pub.need_context_rows = FALSE; upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; if (cinfo->max_v_samp_factor == 2) { upsample->pub.upsample = merged_2v_upsample; if (jsimd_can_h2v2_merged_upsample()) upsample->upmethod = jsimd_h2v2_merged_upsample; else upsample->upmethod = h2v2_merged_upsample; /* Allocate a spare row buffer */ upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); } else { upsample->pub.upsample = merged_1v_upsample; if (jsimd_can_h2v1_merged_upsample()) upsample->upmethod = jsimd_h2v1_merged_upsample; else upsample->upmethod = h2v1_merged_upsample; /* No spare row needed */ upsample->spare_row = NULL; } build_ycc_rgb_table(cinfo); } #endif /* UPSAMPLE_MERGING_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jpegint.h0000664000175000017500000003733212013417376020073 0ustar alfalf00000000000000/* * jpegint.h * * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 1997-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file provides common declarations for the various JPEG modules. * These declarations are considered internal to the JPEG library; most * applications using the library shouldn't need to include this file. */ /* Declarations for both compression & decompression */ typedef enum { /* Operating modes for buffer controllers */ JBUF_PASS_THRU, /* Plain stripwise operation */ /* Remaining modes require a full-image buffer to have been created */ JBUF_SAVE_SOURCE, /* Run source subobject only, save output */ JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */ JBUF_SAVE_AND_PASS /* Run both subobjects, save output */ } J_BUF_MODE; /* Values of global_state field (jdapi.c has some dependencies on ordering!) */ #define CSTATE_START 100 /* after create_compress */ #define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */ #define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */ #define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */ #define DSTATE_START 200 /* after create_decompress */ #define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */ #define DSTATE_READY 202 /* found SOS, ready for start_decompress */ #define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/ #define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */ #define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */ #define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */ #define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */ #define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */ #define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */ #define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */ /* Declarations for compression modules */ /* Master control module */ struct jpeg_comp_master { JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo)); JMETHOD(void, pass_startup, (j_compress_ptr cinfo)); JMETHOD(void, finish_pass, (j_compress_ptr cinfo)); /* State variables made visible to other modules */ boolean call_pass_startup; /* True if pass_startup must be called */ boolean is_last_pass; /* True during last pass */ }; /* Main buffer control (downsampled-data buffer) */ struct jpeg_c_main_controller { JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); JMETHOD(void, process_data, (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)); }; /* Compression preprocessing (downsampling input buffer control) */ struct jpeg_c_prep_controller { JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); JMETHOD(void, pre_process_data, (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail, JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, JDIMENSION out_row_groups_avail)); }; /* Coefficient buffer control */ struct jpeg_c_coef_controller { JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode)); JMETHOD(boolean, compress_data, (j_compress_ptr cinfo, JSAMPIMAGE input_buf)); }; /* Colorspace conversion */ struct jpeg_color_converter { JMETHOD(void, start_pass, (j_compress_ptr cinfo)); JMETHOD(void, color_convert, (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); }; /* Downsampling */ struct jpeg_downsampler { JMETHOD(void, start_pass, (j_compress_ptr cinfo)); JMETHOD(void, downsample, (j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_index, JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)); boolean need_context_rows; /* TRUE if need rows above & below */ }; /* Forward DCT (also controls coefficient quantization) */ struct jpeg_forward_dct { JMETHOD(void, start_pass, (j_compress_ptr cinfo)); /* perhaps this should be an array??? */ JMETHOD(void, forward_DCT, (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks)); }; /* Entropy encoding */ struct jpeg_entropy_encoder { JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics)); JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data)); JMETHOD(void, finish_pass, (j_compress_ptr cinfo)); }; /* Marker writing */ struct jpeg_marker_writer { JMETHOD(void, write_file_header, (j_compress_ptr cinfo)); JMETHOD(void, write_frame_header, (j_compress_ptr cinfo)); JMETHOD(void, write_scan_header, (j_compress_ptr cinfo)); JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo)); JMETHOD(void, write_tables_only, (j_compress_ptr cinfo)); /* These routines are exported to allow insertion of extra markers */ /* Probably only COM and APPn markers should be written this way */ JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker, unsigned int datalen)); JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val)); }; /* Declarations for decompression modules */ /* Master control module */ struct jpeg_decomp_master { JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo)); JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo)); /* State variables made visible to other modules */ boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */ }; /* Input control module */ struct jpeg_input_controller { JMETHOD(int, consume_input, (j_decompress_ptr cinfo)); JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo)); JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo)); JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo)); /* State variables made visible to other modules */ boolean has_multiple_scans; /* True if file has multiple scans */ boolean eoi_reached; /* True when EOI has been consumed */ }; /* Main buffer control (downsampled-data buffer) */ struct jpeg_d_main_controller { JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)); JMETHOD(void, process_data, (j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); }; /* Coefficient buffer control */ struct jpeg_d_coef_controller { JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo)); JMETHOD(int, consume_data, (j_decompress_ptr cinfo)); JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo)); JMETHOD(int, decompress_data, (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); /* Pointer to array of coefficient virtual arrays, or NULL if none */ jvirt_barray_ptr *coef_arrays; }; /* Decompression postprocessing (color quantization buffer control) */ struct jpeg_d_post_controller { JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)); JMETHOD(void, post_process_data, (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); }; /* Marker reading & parsing */ struct jpeg_marker_reader { JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo)); /* Read markers until SOS or EOI. * Returns same codes as are defined for jpeg_consume_input: * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. */ JMETHOD(int, read_markers, (j_decompress_ptr cinfo)); /* Read a restart marker --- exported for use by entropy decoder only */ jpeg_marker_parser_method read_restart_marker; /* State of marker reader --- nominally internal, but applications * supplying COM or APPn handlers might like to know the state. */ boolean saw_SOI; /* found SOI? */ boolean saw_SOF; /* found SOF? */ int next_restart_num; /* next restart number expected (0-7) */ unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */ }; /* Entropy decoding */ struct jpeg_entropy_decoder { JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)); /* This is here to share code between baseline and progressive decoders; */ /* other modules probably should not use it */ boolean insufficient_data; /* set TRUE after emitting warning */ }; /* Inverse DCT (also performs dequantization) */ typedef JMETHOD(void, inverse_DCT_method_ptr, (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); struct jpeg_inverse_dct { JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); /* It is useful to allow each component to have a separate IDCT method. */ inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS]; }; /* Upsampling (note that upsampler must also call color converter) */ struct jpeg_upsampler { JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); JMETHOD(void, upsample, (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); boolean need_context_rows; /* TRUE if need rows above & below */ }; /* Colorspace conversion */ struct jpeg_color_deconverter { JMETHOD(void, start_pass, (j_decompress_ptr cinfo)); JMETHOD(void, color_convert, (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); }; /* Color quantization or color precision reduction */ struct jpeg_color_quantizer { JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan)); JMETHOD(void, color_quantize, (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)); JMETHOD(void, finish_pass, (j_decompress_ptr cinfo)); JMETHOD(void, new_color_map, (j_decompress_ptr cinfo)); }; /* Miscellaneous useful macros */ #undef MAX #define MAX(a,b) ((a) > (b) ? (a) : (b)) #undef MIN #define MIN(a,b) ((a) < (b) ? (a) : (b)) /* We assume that right shift corresponds to signed division by 2 with * rounding towards minus infinity. This is correct for typical "arithmetic * shift" instructions that shift in copies of the sign bit. But some * C compilers implement >> with an unsigned shift. For these machines you * must define RIGHT_SHIFT_IS_UNSIGNED. * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity. * It is only applied with constant shift counts. SHIFT_TEMPS must be * included in the variables of any routine using RIGHT_SHIFT. */ #ifdef RIGHT_SHIFT_IS_UNSIGNED #define SHIFT_TEMPS INT32 shift_temp; #define RIGHT_SHIFT(x,shft) \ ((shift_temp = (x)) < 0 ? \ (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \ (shift_temp >> (shft))) #else #define SHIFT_TEMPS #define RIGHT_SHIFT(x,shft) ((x) >> (shft)) #endif /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jinit_compress_master jICompress #define jinit_c_master_control jICMaster #define jinit_c_main_controller jICMainC #define jinit_c_prep_controller jICPrepC #define jinit_c_coef_controller jICCoefC #define jinit_color_converter jICColor #define jinit_downsampler jIDownsampler #define jinit_forward_dct jIFDCT #define jinit_huff_encoder jIHEncoder #define jinit_phuff_encoder jIPHEncoder #define jinit_arith_encoder jIAEncoder #define jinit_marker_writer jIMWriter #define jinit_master_decompress jIDMaster #define jinit_d_main_controller jIDMainC #define jinit_d_coef_controller jIDCoefC #define jinit_d_post_controller jIDPostC #define jinit_input_controller jIInCtlr #define jinit_marker_reader jIMReader #define jinit_huff_decoder jIHDecoder #define jinit_phuff_decoder jIPHDecoder #define jinit_arith_decoder jIADecoder #define jinit_inverse_dct jIIDCT #define jinit_upsampler jIUpsampler #define jinit_color_deconverter jIDColor #define jinit_1pass_quantizer jI1Quant #define jinit_2pass_quantizer jI2Quant #define jinit_merged_upsampler jIMUpsampler #define jinit_memory_mgr jIMemMgr #define jdiv_round_up jDivRound #define jround_up jRound #define jcopy_sample_rows jCopySamples #define jcopy_block_row jCopyBlocks #define jzero_far jZeroFar #define jpeg_zigzag_order jZIGTable #define jpeg_natural_order jZAGTable #define jpeg_aritab jAriTab #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* Compression module initialization routines */ EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo, boolean transcode_only)); EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_arith_encoder JPP((j_compress_ptr cinfo)); EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo)); /* Decompression module initialization routines */ EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo, boolean need_full_buffer)); EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo)); EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo)); /* Memory manager initialization */ EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo)); /* Utility routines in jutils.c */ EXTERN(long) jdiv_round_up JPP((long a, long b)); EXTERN(long) jround_up JPP((long a, long b)); EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row, JSAMPARRAY output_array, int dest_row, int num_rows, JDIMENSION num_cols)); EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row, JDIMENSION num_blocks)); EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero)); /* Constant tables in jutils.c */ #if 0 /* This table is not actually needed in v6a */ extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */ #endif extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */ /* Arithmetic coding probability estimation tables in jaricom.c */ extern const INT32 jpeg_aritab[]; /* Suppress undefined-structure complaints if necessary. */ #ifdef INCOMPLETE_TYPES_BROKEN #ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */ struct jvirt_sarray_control { long dummy; }; struct jvirt_barray_control { long dummy; }; #endif #endif /* INCOMPLETE_TYPES_BROKEN */ glmark2-2012.08/./src/libjpeg-turbo/jdinput.c0000664000175000017500000004073512013417376020104 0ustar alfalf00000000000000/* * jdinput.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 2002-2009 by Guido Vollbeding. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains input control logic for the JPEG decompressor. * These routines are concerned with controlling the decompressor's input * processing (marker reading and coefficient decoding). The actual input * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* Private state */ typedef struct { struct jpeg_input_controller pub; /* public fields */ boolean inheaders; /* TRUE until first SOS is reached */ } my_input_controller; typedef my_input_controller * my_inputctl_ptr; /* Forward declarations */ METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); /* * Routines to calculate various quantities related to the size of the image. */ #if JPEG_LIB_VERSION >= 80 /* * Compute output image dimensions and related values. * NOTE: this is exported for possible use by application. * Hence it mustn't do anything that can't be done twice. */ GLOBAL(void) jpeg_core_output_dimensions (j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase. * This function is used for transcoding and full decompression. */ { #ifdef IDCT_SCALING_SUPPORTED int ci; jpeg_component_info *compptr; /* Compute actual output image dimensions and DCT scaling choices. */ if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) { /* Provide 1/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 1; cinfo->min_DCT_v_scaled_size = 1; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) { /* Provide 2/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 2; cinfo->min_DCT_v_scaled_size = 2; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) { /* Provide 4/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 4; cinfo->min_DCT_v_scaled_size = 4; } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) { /* Provide 8/block_size scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size); cinfo->min_DCT_h_scaled_size = 8; cinfo->min_DCT_v_scaled_size = 8; } /* Recompute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size; compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size; } #else /* !IDCT_SCALING_SUPPORTED */ /* Hardwire it to "no scaling" */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; /* jdinput.c has already initialized DCT_scaled_size, * and has computed unscaled downsampled_width and downsampled_height. */ #endif /* IDCT_SCALING_SUPPORTED */ } #endif LOCAL(void) initial_setup (j_decompress_ptr cinfo) /* Called once, when first SOS marker is reached */ { int ci; jpeg_component_info *compptr; /* Make sure image isn't bigger than I can handle */ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); /* For now, precision must match compiled-in value... */ if (cinfo->data_precision != BITS_IN_JSAMPLE) ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); /* Check that number of components won't exceed internal array sizes */ if (cinfo->num_components > MAX_COMPONENTS) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPONENTS); /* Compute maximum sampling factors; check factor validity */ cinfo->max_h_samp_factor = 1; cinfo->max_v_samp_factor = 1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) ERREXIT(cinfo, JERR_BAD_SAMPLING); cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, compptr->h_samp_factor); cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, compptr->v_samp_factor); } #if JPEG_LIB_VERSION >=80 cinfo->block_size = DCTSIZE; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; #endif /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. * In the full decompressor, this will be overridden by jdmaster.c; * but in the transcoder, jdmaster.c is not used, so we must do it here. */ #if JPEG_LIB_VERSION >= 70 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE; #else cinfo->min_DCT_scaled_size = DCTSIZE; #endif /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { #if JPEG_LIB_VERSION >= 70 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; #else compptr->DCT_scaled_size = DCTSIZE; #endif /* Size in DCT blocks */ compptr->width_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, (long) (cinfo->max_h_samp_factor * DCTSIZE)); compptr->height_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * DCTSIZE)); /* downsampled_width and downsampled_height will also be overridden by * jdmaster.c if we are doing full decompression. The transcoder library * doesn't use these values, but the calling application might. */ /* Size in samples */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, (long) cinfo->max_h_samp_factor); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, (long) cinfo->max_v_samp_factor); /* Mark component needed, until color conversion says otherwise */ compptr->component_needed = TRUE; /* Mark no quantization table yet saved for component */ compptr->quant_table = NULL; } /* Compute number of fully interleaved MCU rows. */ cinfo->total_iMCU_rows = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); /* Decide whether file contains multiple scans */ if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) cinfo->inputctl->has_multiple_scans = TRUE; else cinfo->inputctl->has_multiple_scans = FALSE; } LOCAL(void) per_scan_setup (j_decompress_ptr cinfo) /* Do computations that are needed before processing a JPEG scan */ /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ { int ci, mcublks, tmp; jpeg_component_info *compptr; if (cinfo->comps_in_scan == 1) { /* Noninterleaved (single-component) scan */ compptr = cinfo->cur_comp_info[0]; /* Overall image size in MCUs */ cinfo->MCUs_per_row = compptr->width_in_blocks; cinfo->MCU_rows_in_scan = compptr->height_in_blocks; /* For noninterleaved scan, always one block per MCU */ compptr->MCU_width = 1; compptr->MCU_height = 1; compptr->MCU_blocks = 1; compptr->MCU_sample_width = compptr->_DCT_scaled_size; compptr->last_col_width = 1; /* For noninterleaved scans, it is convenient to define last_row_height * as the number of block rows present in the last iMCU row. */ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); if (tmp == 0) tmp = compptr->v_samp_factor; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ cinfo->blocks_in_MCU = 1; cinfo->MCU_membership[0] = 0; } else { /* Interleaved (multi-component) scan */ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, MAX_COMPS_IN_SCAN); /* Overall image size in MCUs */ cinfo->MCUs_per_row = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, (long) (cinfo->max_h_samp_factor*DCTSIZE)); cinfo->MCU_rows_in_scan = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); cinfo->blocks_in_MCU = 0; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Sampling factors give # of blocks of component in each MCU */ compptr->MCU_width = compptr->h_samp_factor; compptr->MCU_height = compptr->v_samp_factor; compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size; /* Figure number of non-dummy blocks in last MCU column & row */ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); if (tmp == 0) tmp = compptr->MCU_width; compptr->last_col_width = tmp; tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); if (tmp == 0) tmp = compptr->MCU_height; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ mcublks = compptr->MCU_blocks; if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) ERREXIT(cinfo, JERR_BAD_MCU_SIZE); while (mcublks-- > 0) { cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; } } } } /* * Save away a copy of the Q-table referenced by each component present * in the current scan, unless already saved during a prior scan. * * In a multiple-scan JPEG file, the encoder could assign different components * the same Q-table slot number, but change table definitions between scans * so that each component uses a different Q-table. (The IJG encoder is not * currently capable of doing this, but other encoders might.) Since we want * to be able to dequantize all the components at the end of the file, this * means that we have to save away the table actually used for each component. * We do this by copying the table at the start of the first scan containing * the component. * The JPEG spec prohibits the encoder from changing the contents of a Q-table * slot between scans of a component using that slot. If the encoder does so * anyway, this decoder will simply use the Q-table values that were current * at the start of the first scan for the component. * * The decompressor output side looks only at the saved quant tables, * not at the current Q-table slots. */ LOCAL(void) latch_quant_tables (j_decompress_ptr cinfo) { int ci, qtblno; jpeg_component_info *compptr; JQUANT_TBL * qtbl; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* No work if we already saved Q-table for this component */ if (compptr->quant_table != NULL) continue; /* Make sure specified quantization table is present */ qtblno = compptr->quant_tbl_no; if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || cinfo->quant_tbl_ptrs[qtblno] == NULL) ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); /* OK, save away the quantization table */ qtbl = (JQUANT_TBL *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(JQUANT_TBL)); MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); compptr->quant_table = qtbl; } } /* * Initialize the input modules to read a scan of compressed data. * The first call to this is done by jdmaster.c after initializing * the entire decompressor (during jpeg_start_decompress). * Subsequent calls come from consume_markers, below. */ METHODDEF(void) start_input_pass (j_decompress_ptr cinfo) { per_scan_setup(cinfo); latch_quant_tables(cinfo); (*cinfo->entropy->start_pass) (cinfo); (*cinfo->coef->start_input_pass) (cinfo); cinfo->inputctl->consume_input = cinfo->coef->consume_data; } /* * Finish up after inputting a compressed-data scan. * This is called by the coefficient controller after it's read all * the expected data of the scan. */ METHODDEF(void) finish_input_pass (j_decompress_ptr cinfo) { cinfo->inputctl->consume_input = consume_markers; } /* * Read JPEG markers before, between, or after compressed-data scans. * Change state as necessary when a new scan is reached. * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. * * The consume_input method pointer points either here or to the * coefficient controller's consume_data routine, depending on whether * we are reading a compressed data segment or inter-segment markers. */ METHODDEF(int) consume_markers (j_decompress_ptr cinfo) { my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; int val; if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ return JPEG_REACHED_EOI; val = (*cinfo->marker->read_markers) (cinfo); switch (val) { case JPEG_REACHED_SOS: /* Found SOS */ if (inputctl->inheaders) { /* 1st SOS */ initial_setup(cinfo); inputctl->inheaders = FALSE; /* Note: start_input_pass must be called by jdmaster.c * before any more input can be consumed. jdapimin.c is * responsible for enforcing this sequencing. */ } else { /* 2nd or later SOS marker */ if (! inputctl->pub.has_multiple_scans) ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ start_input_pass(cinfo); } break; case JPEG_REACHED_EOI: /* Found EOI */ inputctl->pub.eoi_reached = TRUE; if (inputctl->inheaders) { /* Tables-only datastream, apparently */ if (cinfo->marker->saw_SOF) ERREXIT(cinfo, JERR_SOF_NO_SOS); } else { /* Prevent infinite loop in coef ctlr's decompress_data routine * if user set output_scan_number larger than number of scans. */ if (cinfo->output_scan_number > cinfo->input_scan_number) cinfo->output_scan_number = cinfo->input_scan_number; } break; case JPEG_SUSPENDED: break; } return val; } /* * Reset state to begin a fresh datastream. */ METHODDEF(void) reset_input_controller (j_decompress_ptr cinfo) { my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; inputctl->pub.consume_input = consume_markers; inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ inputctl->pub.eoi_reached = FALSE; inputctl->inheaders = TRUE; /* Reset other modules */ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); (*cinfo->marker->reset_marker_reader) (cinfo); /* Reset progression state -- would be cleaner if entropy decoder did this */ cinfo->coef_bits = NULL; } /* * Initialize the input controller module. * This is called only once, when the decompression object is created. */ GLOBAL(void) jinit_input_controller (j_decompress_ptr cinfo) { my_inputctl_ptr inputctl; /* Create subobject in permanent pool */ inputctl = (my_inputctl_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_input_controller)); cinfo->inputctl = (struct jpeg_input_controller *) inputctl; /* Initialize method pointers */ inputctl->pub.consume_input = consume_markers; inputctl->pub.reset_input_controller = reset_input_controller; inputctl->pub.start_input_pass = start_input_pass; inputctl->pub.finish_input_pass = finish_input_pass; /* Initialize state: can't use reset_input_controller since we don't * want to try to reset other modules yet. */ inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ inputctl->pub.eoi_reached = FALSE; inputctl->inheaders = TRUE; } glmark2-2012.08/./src/libjpeg-turbo/jdsample.c0000664000175000017500000004104412013417376020220 0ustar alfalf00000000000000/* * jdsample.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains upsampling routines. * * Upsampling input data is counted in "row groups". A row group * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) * sample rows of each component. Upsampling will normally produce * max_v_samp_factor pixel rows from each row group (but this could vary * if the upsampler is applying a scale factor of its own). * * An excellent reference for image resampling is * Digital Image Warping, George Wolberg, 1990. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jsimd.h" #include "jpegcomp.h" /* Pointer to routine to upsample a single component */ typedef JMETHOD(void, upsample1_ptr, (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); /* Private subobject */ typedef struct { struct jpeg_upsampler pub; /* public fields */ /* Color conversion buffer. When using separate upsampling and color * conversion steps, this buffer holds one upsampled row group until it * has been color converted and output. * Note: we do not allocate any storage for component(s) which are full-size, * ie do not need rescaling. The corresponding entry of color_buf[] is * simply set to point to the input data array, thereby avoiding copying. */ JSAMPARRAY color_buf[MAX_COMPONENTS]; /* Per-component upsampling method pointers */ upsample1_ptr methods[MAX_COMPONENTS]; int next_row_out; /* counts rows emitted from color_buf */ JDIMENSION rows_to_go; /* counts rows remaining in image */ /* Height of an input row group for each component. */ int rowgroup_height[MAX_COMPONENTS]; /* These arrays save pixel expansion factors so that int_expand need not * recompute them each time. They are unused for other upsampling methods. */ UINT8 h_expand[MAX_COMPONENTS]; UINT8 v_expand[MAX_COMPONENTS]; } my_upsampler; typedef my_upsampler * my_upsample_ptr; /* * Initialize for an upsampling pass. */ METHODDEF(void) start_pass_upsample (j_decompress_ptr cinfo) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; /* Mark the conversion buffer empty */ upsample->next_row_out = cinfo->max_v_samp_factor; /* Initialize total-height counter for detecting bottom of image */ upsample->rows_to_go = cinfo->output_height; } /* * Control routine to do upsampling (and color conversion). * * In this version we upsample each component independently. * We upsample one row group into the conversion buffer, then apply * color conversion a row at a time. */ METHODDEF(void) sep_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; int ci; jpeg_component_info * compptr; JDIMENSION num_rows; /* Fill the conversion buffer, if it's empty */ if (upsample->next_row_out >= cinfo->max_v_samp_factor) { for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Invoke per-component upsample method. Notice we pass a POINTER * to color_buf[ci], so that fullsize_upsample can change it. */ (*upsample->methods[ci]) (cinfo, compptr, input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]), upsample->color_buf + ci); } upsample->next_row_out = 0; } /* Color-convert and emit rows */ /* How many we have in the buffer: */ num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out); /* Not more than the distance to the end of the image. Need this test * in case the image height is not a multiple of max_v_samp_factor: */ if (num_rows > upsample->rows_to_go) num_rows = upsample->rows_to_go; /* And not more than what the client can accept: */ out_rows_avail -= *out_row_ctr; if (num_rows > out_rows_avail) num_rows = out_rows_avail; (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf, (JDIMENSION) upsample->next_row_out, output_buf + *out_row_ctr, (int) num_rows); /* Adjust counts */ *out_row_ctr += num_rows; upsample->rows_to_go -= num_rows; upsample->next_row_out += num_rows; /* When the buffer is emptied, declare this input row group consumed */ if (upsample->next_row_out >= cinfo->max_v_samp_factor) (*in_row_group_ctr)++; } /* * These are the routines invoked by sep_upsample to upsample pixel values * of a single component. One row group is processed per call. */ /* * For full-size components, we just make color_buf[ci] point at the * input buffer, and thus avoid copying any data. Note that this is * safe only because sep_upsample doesn't declare the input row group * "consumed" until we are done color converting and emitting it. */ METHODDEF(void) fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { *output_data_ptr = input_data; } /* * This is a no-op version used for "uninteresting" components. * These components will not be referenced by color conversion. */ METHODDEF(void) noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { *output_data_ptr = NULL; /* safety check */ } /* * This version handles any integral sampling ratios. * This is not used for typical JPEG files, so it need not be fast. * Nor, for that matter, is it particularly accurate: the algorithm is * simple replication of the input pixel onto the corresponding output * pixels. The hi-falutin sampling literature refers to this as a * "box filter". A box filter tends to introduce visible artifacts, * so if you are actually going to use 3:1 or 4:1 sampling ratios * you would be well advised to improve this code. */ METHODDEF(void) int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; JSAMPARRAY output_data = *output_data_ptr; register JSAMPROW inptr, outptr; register JSAMPLE invalue; register int h; JSAMPROW outend; int h_expand, v_expand; int inrow, outrow; h_expand = upsample->h_expand[compptr->component_index]; v_expand = upsample->v_expand[compptr->component_index]; inrow = outrow = 0; while (outrow < cinfo->max_v_samp_factor) { /* Generate one output row with proper horizontal expansion */ inptr = input_data[inrow]; outptr = output_data[outrow]; outend = outptr + cinfo->output_width; while (outptr < outend) { invalue = *inptr++; /* don't need GETJSAMPLE() here */ for (h = h_expand; h > 0; h--) { *outptr++ = invalue; } } /* Generate any additional output rows by duplicating the first one */ if (v_expand > 1) { jcopy_sample_rows(output_data, outrow, output_data, outrow+1, v_expand-1, cinfo->output_width); } inrow++; outrow += v_expand; } } /* * Fast processing for the common case of 2:1 horizontal and 1:1 vertical. * It's still a box filter. */ METHODDEF(void) h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { JSAMPARRAY output_data = *output_data_ptr; register JSAMPROW inptr, outptr; register JSAMPLE invalue; JSAMPROW outend; int inrow; for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { inptr = input_data[inrow]; outptr = output_data[inrow]; outend = outptr + cinfo->output_width; while (outptr < outend) { invalue = *inptr++; /* don't need GETJSAMPLE() here */ *outptr++ = invalue; *outptr++ = invalue; } } } /* * Fast processing for the common case of 2:1 horizontal and 2:1 vertical. * It's still a box filter. */ METHODDEF(void) h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { JSAMPARRAY output_data = *output_data_ptr; register JSAMPROW inptr, outptr; register JSAMPLE invalue; JSAMPROW outend; int inrow, outrow; inrow = outrow = 0; while (outrow < cinfo->max_v_samp_factor) { inptr = input_data[inrow]; outptr = output_data[outrow]; outend = outptr + cinfo->output_width; while (outptr < outend) { invalue = *inptr++; /* don't need GETJSAMPLE() here */ *outptr++ = invalue; *outptr++ = invalue; } jcopy_sample_rows(output_data, outrow, output_data, outrow+1, 1, cinfo->output_width); inrow++; outrow += 2; } } /* * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical. * * The upsampling algorithm is linear interpolation between pixel centers, * also known as a "triangle filter". This is a good compromise between * speed and visual quality. The centers of the output pixels are 1/4 and 3/4 * of the way between input pixel centers. * * A note about the "bias" calculations: when rounding fractional values to * integer, we do not want to always round 0.5 up to the next integer. * If we did that, we'd introduce a noticeable bias towards larger values. * Instead, this code is arranged so that 0.5 will be rounded up or down at * alternate pixel locations (a simple ordered dither pattern). */ METHODDEF(void) h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { JSAMPARRAY output_data = *output_data_ptr; register JSAMPROW inptr, outptr; register int invalue; register JDIMENSION colctr; int inrow; for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { inptr = input_data[inrow]; outptr = output_data[inrow]; /* Special case for first column */ invalue = GETJSAMPLE(*inptr++); *outptr++ = (JSAMPLE) invalue; *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2); for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { /* General case: 3/4 * nearer pixel + 1/4 * further pixel */ invalue = GETJSAMPLE(*inptr++) * 3; *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2); *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2); } /* Special case for last column */ invalue = GETJSAMPLE(*inptr); *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2); *outptr++ = (JSAMPLE) invalue; } } /* * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical. * Again a triangle filter; see comments for h2v1 case, above. * * It is OK for us to reference the adjacent input rows because we demanded * context from the main buffer controller (see initialization code). */ METHODDEF(void) h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { JSAMPARRAY output_data = *output_data_ptr; register JSAMPROW inptr0, inptr1, outptr; #if BITS_IN_JSAMPLE == 8 register int thiscolsum, lastcolsum, nextcolsum; #else register INT32 thiscolsum, lastcolsum, nextcolsum; #endif register JDIMENSION colctr; int inrow, outrow, v; inrow = outrow = 0; while (outrow < cinfo->max_v_samp_factor) { for (v = 0; v < 2; v++) { /* inptr0 points to nearest input row, inptr1 points to next nearest */ inptr0 = input_data[inrow]; if (v == 0) /* next nearest is row above */ inptr1 = input_data[inrow-1]; else /* next nearest is row below */ inptr1 = input_data[inrow+1]; outptr = output_data[outrow++]; /* Special case for first column */ thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4); *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); lastcolsum = thiscolsum; thiscolsum = nextcolsum; for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */ /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */ nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); lastcolsum = thiscolsum; thiscolsum = nextcolsum; } /* Special case for last column */ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4); } inrow++; } } /* * Module initialization routine for upsampling. */ GLOBAL(void) jinit_upsampler (j_decompress_ptr cinfo) { my_upsample_ptr upsample; int ci; jpeg_component_info * compptr; boolean need_buffer, do_fancy; int h_in_group, v_in_group, h_out_group, v_out_group; upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler)); cinfo->upsample = (struct jpeg_upsampler *) upsample; upsample->pub.start_pass = start_pass_upsample; upsample->pub.upsample = sep_upsample; upsample->pub.need_context_rows = FALSE; /* until we find out differently */ if (cinfo->CCIR601_sampling) /* this isn't supported */ ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1, * so don't ask for it. */ do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1; /* Verify we can handle the sampling factors, select per-component methods, * and create storage as needed. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Compute size of an "input group" after IDCT scaling. This many samples * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. */ h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / cinfo->_min_DCT_scaled_size; h_out_group = cinfo->max_h_samp_factor; v_out_group = cinfo->max_v_samp_factor; upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ need_buffer = TRUE; if (! compptr->component_needed) { /* Don't bother to upsample an uninteresting component. */ upsample->methods[ci] = noop_upsample; need_buffer = FALSE; } else if (h_in_group == h_out_group && v_in_group == v_out_group) { /* Fullsize components can be processed without any work. */ upsample->methods[ci] = fullsize_upsample; need_buffer = FALSE; } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) { /* Special cases for 2h1v upsampling */ if (do_fancy && compptr->downsampled_width > 2) { if (jsimd_can_h2v1_fancy_upsample()) upsample->methods[ci] = jsimd_h2v1_fancy_upsample; else upsample->methods[ci] = h2v1_fancy_upsample; } else { if (jsimd_can_h2v1_upsample()) upsample->methods[ci] = jsimd_h2v1_upsample; else upsample->methods[ci] = h2v1_upsample; } } else if (h_in_group * 2 == h_out_group && v_in_group * 2 == v_out_group) { /* Special cases for 2h2v upsampling */ if (do_fancy && compptr->downsampled_width > 2) { if (jsimd_can_h2v2_fancy_upsample()) upsample->methods[ci] = jsimd_h2v2_fancy_upsample; else upsample->methods[ci] = h2v2_fancy_upsample; upsample->pub.need_context_rows = TRUE; } else { if (jsimd_can_h2v2_upsample()) upsample->methods[ci] = jsimd_h2v2_upsample; else upsample->methods[ci] = h2v2_upsample; } } else if ((h_out_group % h_in_group) == 0 && (v_out_group % v_in_group) == 0) { /* Generic integral-factors upsampling method */ upsample->methods[ci] = int_upsample; upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group); } else ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); if (need_buffer) { upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) jround_up((long) cinfo->output_width, (long) cinfo->max_h_samp_factor), (JDIMENSION) cinfo->max_v_samp_factor); } } } glmark2-2012.08/./src/libjpeg-turbo/jdhuff.h0000664000175000017500000002172212013417376017675 0ustar alfalf00000000000000/* * jdhuff.h * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2010-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains declarations for Huffman entropy decoding routines * that are shared between the sequential decoder (jdhuff.c) and the * progressive decoder (jdphuff.c). No other modules need to see these. */ /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_make_d_derived_tbl jMkDDerived #define jpeg_fill_bit_buffer jFilBitBuf #define jpeg_huff_decode jHufDecode #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* Derived data constructed for each Huffman table */ #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ typedef struct { /* Basic tables: (element [0] of each array is unused) */ INT32 maxcode[18]; /* largest code of length k (-1 if none) */ /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ INT32 valoffset[18]; /* huffval[] offset for codes of length k */ /* valoffset[k] = huffval[] index of 1st symbol of code length k, less * the smallest code of length k; so given a code of length k, the * corresponding symbol is huffval[code + valoffset[k]] */ /* Link to public Huffman table (needed only in jpeg_huff_decode) */ JHUFF_TBL *pub; /* Lookahead table: indexed by the next HUFF_LOOKAHEAD bits of * the input data stream. If the next Huffman code is no more * than HUFF_LOOKAHEAD bits long, we can obtain its length and * the corresponding symbol directly from this tables. * * The lower 8 bits of each table entry contain the number of * bits in the corresponding Huffman code, or HUFF_LOOKAHEAD + 1 * if too long. The next 8 bits of each entry contain the * symbol. */ int lookup[1< 32 bits on your machine, and shifting/masking longs is * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE * appropriately should be a win. Unfortunately we can't define the size * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) * because not all machines measure sizeof in 8-bit bytes. */ typedef struct { /* Bitreading state saved across MCUs */ bit_buf_type get_buffer; /* current bit-extraction buffer */ int bits_left; /* # of unused bits in it */ } bitread_perm_state; typedef struct { /* Bitreading working state within an MCU */ /* Current data source location */ /* We need a copy, rather than munging the original, in case of suspension */ const JOCTET * next_input_byte; /* => next byte to read from source */ size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ /* Bit input buffer --- note these values are kept in register variables, * not in this struct, inside the inner loops. */ bit_buf_type get_buffer; /* current bit-extraction buffer */ int bits_left; /* # of unused bits in it */ /* Pointer needed by jpeg_fill_bit_buffer. */ j_decompress_ptr cinfo; /* back link to decompress master record */ } bitread_working_state; /* Macros to declare and load/save bitread local variables. */ #define BITREAD_STATE_VARS \ register bit_buf_type get_buffer; \ register int bits_left; \ bitread_working_state br_state #define BITREAD_LOAD_STATE(cinfop,permstate) \ br_state.cinfo = cinfop; \ br_state.next_input_byte = cinfop->src->next_input_byte; \ br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ get_buffer = permstate.get_buffer; \ bits_left = permstate.bits_left; #define BITREAD_SAVE_STATE(cinfop,permstate) \ cinfop->src->next_input_byte = br_state.next_input_byte; \ cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ permstate.get_buffer = get_buffer; \ permstate.bits_left = bits_left /* * These macros provide the in-line portion of bit fetching. * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer * before using GET_BITS, PEEK_BITS, or DROP_BITS. * The variables get_buffer and bits_left are assumed to be locals, * but the state struct might not be (jpeg_huff_decode needs this). * CHECK_BIT_BUFFER(state,n,action); * Ensure there are N bits in get_buffer; if suspend, take action. * val = GET_BITS(n); * Fetch next N bits. * val = PEEK_BITS(n); * Fetch next N bits without removing them from the buffer. * DROP_BITS(n); * Discard next N bits. * The value N should be a simple variable, not an expression, because it * is evaluated multiple times. */ #define CHECK_BIT_BUFFER(state,nbits,action) \ { if (bits_left < (nbits)) { \ if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \ { action; } \ get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } #define GET_BITS(nbits) \ (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1)) #define PEEK_BITS(nbits) \ (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1)) #define DROP_BITS(nbits) \ (bits_left -= (nbits)) /* Load up the bit buffer to a depth of at least nbits */ EXTERN(boolean) jpeg_fill_bit_buffer JPP((bitread_working_state * state, register bit_buf_type get_buffer, register int bits_left, int nbits)); /* * Code for extracting next Huffman-coded symbol from input bit stream. * Again, this is time-critical and we make the main paths be macros. * * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits * without looping. Usually, more than 95% of the Huffman codes will be 8 * or fewer bits long. The few overlength codes are handled with a loop, * which need not be inline code. * * Notes about the HUFF_DECODE macro: * 1. Near the end of the data segment, we may fail to get enough bits * for a lookahead. In that case, we do it the hard way. * 2. If the lookahead table contains no entry, the next code must be * more than HUFF_LOOKAHEAD bits long. * 3. jpeg_huff_decode returns -1 if forced to suspend. */ #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ { register int nb, look; \ if (bits_left < HUFF_LOOKAHEAD) { \ if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ get_buffer = state.get_buffer; bits_left = state.bits_left; \ if (bits_left < HUFF_LOOKAHEAD) { \ nb = 1; goto slowlabel; \ } \ } \ look = PEEK_BITS(HUFF_LOOKAHEAD); \ if ((nb = (htbl->lookup[look] >> HUFF_LOOKAHEAD)) <= HUFF_LOOKAHEAD) { \ DROP_BITS(nb); \ result = htbl->lookup[look] & ((1 << HUFF_LOOKAHEAD) - 1); \ } else { \ slowlabel: \ if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ { failaction; } \ get_buffer = state.get_buffer; bits_left = state.bits_left; \ } \ } #define HUFF_DECODE_FAST(s,nb,htbl) \ FILL_BIT_BUFFER_FAST; \ s = PEEK_BITS(HUFF_LOOKAHEAD); \ s = htbl->lookup[s]; \ nb = s >> HUFF_LOOKAHEAD; \ /* Pre-execute the common case of nb <= HUFF_LOOKAHEAD */ \ DROP_BITS(nb); \ s = s & ((1 << HUFF_LOOKAHEAD) - 1); \ if (nb > HUFF_LOOKAHEAD) { \ /* Equivalent of jpeg_huff_decode() */ \ /* Don't use GET_BITS() here because we don't want to modify bits_left */ \ s = (get_buffer >> bits_left) & ((1 << (nb)) - 1); \ while (s > htbl->maxcode[nb]) { \ s <<= 1; \ s |= GET_BITS(1); \ nb++; \ } \ s = htbl->pub->huffval[ (int) (s + htbl->valoffset[nb]) & 0xFF ]; \ } /* Out-of-line case for Huffman code fetching */ EXTERN(int) jpeg_huff_decode JPP((bitread_working_state * state, register bit_buf_type get_buffer, register int bits_left, d_derived_tbl * htbl, int min_bits)); glmark2-2012.08/./src/libjpeg-turbo/jdarith.c0000664000175000017500000005605612013417376020057 0ustar alfalf00000000000000/* * jdarith.c * * Developed 1997-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains portable arithmetic entropy decoding routines for JPEG * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). * * Both sequential and progressive modes are supported in this single module. * * Suspension is not currently supported in this module. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Expanded entropy decoder object for arithmetic decoding. */ typedef struct { struct jpeg_entropy_decoder pub; /* public fields */ INT32 c; /* C register, base of coding interval + input bit buffer */ INT32 a; /* A register, normalized size of coding interval */ int ct; /* bit shift counter, # of bits left in bit buffer part of C */ /* init: ct = -16 */ /* run: ct = 0..7 */ /* error: ct = -1 */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ /* Pointers to statistics areas (these workspaces have image lifespan) */ unsigned char * dc_stats[NUM_ARITH_TBLS]; unsigned char * ac_stats[NUM_ARITH_TBLS]; /* Statistics bin for coding with fixed probability 0.5 */ unsigned char fixed_bin[4]; } arith_entropy_decoder; typedef arith_entropy_decoder * arith_entropy_ptr; /* The following two definitions specify the allocation chunk size * for the statistics area. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least * 49 statistics bins for DC, and 245 statistics bins for AC coding. * * We use a compact representation with 1 byte per statistics bin, * thus the numbers directly represent byte sizes. * This 1 byte per statistics bin contains the meaning of the MPS * (more probable symbol) in the highest bit (mask 0x80), and the * index into the probability estimation state machine table * in the lower bits (mask 0x7F). */ #define DC_STAT_BINS 64 #define AC_STAT_BINS 256 LOCAL(int) get_byte (j_decompress_ptr cinfo) /* Read next input byte; we do not support suspension in this module. */ { struct jpeg_source_mgr * src = cinfo->src; if (src->bytes_in_buffer == 0) if (! (*src->fill_input_buffer) (cinfo)) ERREXIT(cinfo, JERR_CANT_SUSPEND); src->bytes_in_buffer--; return GETJOCTET(*src->next_input_byte++); } /* * The core arithmetic decoding routine (common in JPEG and JBIG). * This needs to go as fast as possible. * Machine-dependent optimization facilities * are not utilized in this portable implementation. * However, this code should be fairly efficient and * may be a good base for further optimizations anyway. * * Return value is 0 or 1 (binary decision). * * Note: I've changed the handling of the code base & bit * buffer register C compared to other implementations * based on the standards layout & procedures. * While it also contains both the actual base of the * coding interval (16 bits) and the next-bits buffer, * the cut-point between these two parts is floating * (instead of fixed) with the bit shift counter CT. * Thus, we also need only one (variable instead of * fixed size) shift for the LPS/MPS decision, and * we can get away with any renormalization update * of C (except for new data insertion, of course). * * I've also introduced a new scheme for accessing * the probability estimation state machine table, * derived from Markus Kuhn's JBIG implementation. */ LOCAL(int) arith_decode (j_decompress_ptr cinfo, unsigned char *st) { register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; register unsigned char nl, nm; register INT32 qe, temp; register int sv, data; /* Renormalization & data input per section D.2.6 */ while (e->a < 0x8000L) { if (--e->ct < 0) { /* Need to fetch next data byte */ if (cinfo->unread_marker) data = 0; /* stuff zero data */ else { data = get_byte(cinfo); /* read next input byte */ if (data == 0xFF) { /* zero stuff or marker code */ do data = get_byte(cinfo); while (data == 0xFF); /* swallow extra 0xFF bytes */ if (data == 0) data = 0xFF; /* discard stuffed zero byte */ else { /* Note: Different from the Huffman decoder, hitting * a marker while processing the compressed data * segment is legal in arithmetic coding. * The convention is to supply zero data * then until decoding is complete. */ cinfo->unread_marker = data; data = 0; } } } e->c = (e->c << 8) | data; /* insert data into C register */ if ((e->ct += 8) < 0) /* update bit shift counter */ /* Need more initial bytes */ if (++e->ct == 0) /* Got 2 initial bytes -> re-init A and exit loop */ e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ } e->a <<= 1; } /* Fetch values from our compact representation of Table D.2: * Qe values and probability estimation state machine */ sv = *st; qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ temp = e->a - qe; e->a = temp; temp <<= e->ct; if (e->c >= temp) { e->c -= temp; /* Conditional LPS (less probable symbol) exchange */ if (e->a < qe) { e->a = qe; *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ } else { e->a = qe; *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ sv ^= 0x80; /* Exchange LPS/MPS */ } } else if (e->a < 0x8000L) { /* Conditional MPS (more probable symbol) exchange */ if (e->a < qe) { *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ sv ^= 0x80; /* Exchange LPS/MPS */ } else { *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ } } return sv >> 7; } /* * Check for a restart marker & resynchronize decoder. */ LOCAL(void) process_restart (j_decompress_ptr cinfo) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; int ci; jpeg_component_info * compptr; /* Advance past the RSTn marker */ if (! (*cinfo->marker->read_restart_marker) (cinfo)) ERREXIT(cinfo, JERR_CANT_SUSPEND); /* Re-initialize statistics areas */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); /* Reset DC predictions to 0 */ entropy->last_dc_val[ci] = 0; entropy->dc_context[ci] = 0; } if (! cinfo->progressive_mode || cinfo->Ss) { MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); } } /* Reset arithmetic decoding variables */ entropy->c = 0; entropy->a = 0; entropy->ct = -16; /* force reading 2 initial bytes to fill C */ /* Reset restart counter */ entropy->restarts_to_go = cinfo->restart_interval; } /* * Arithmetic MCU decoding. * Each of these routines decodes and returns one MCU's worth of * arithmetic-compressed coefficients. * The coefficients are reordered from zigzag order into natural array order, * but are not dequantized. * * The i'th block of the MCU is stored into the block pointed to by * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. */ /* * MCU decoding for DC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; unsigned char *st; int blkn, ci, tbl, sign; int v, m; /* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) process_restart(cinfo); entropy->restarts_to_go--; } if (entropy->ct == -1) return TRUE; /* if error do nothing */ /* Outer loop handles each block in the MCU */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; /* Figure F.19: Decode_DC_DIFF */ if (arith_decode(cinfo, st) == 0) entropy->dc_context[ci] = 0; else { /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */ sign = arith_decode(cinfo, st + 1); st += 2; st += sign; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* magnitude overflow */ return TRUE; } st += 1; } } /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) entropy->dc_context[ci] = 0; /* zero diff category */ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ else entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m; v += 1; if (sign) v = -v; entropy->last_dc_val[ci] += v; } /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); } return TRUE; } /* * MCU decoding for AC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; unsigned char *st; int tbl, sign, k; int v, m; /* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) process_restart(cinfo); entropy->restarts_to_go--; } if (entropy->ct == -1) return TRUE; /* if error do nothing */ /* There is always only one block per MCU */ block = MCU_data[0]; tbl = cinfo->cur_comp_info[0]->ac_tbl_no; /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ /* Figure F.20: Decode_AC_coefficients */ for (k = cinfo->Ss; k <= cinfo->Se; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); if (arith_decode(cinfo, st)) break; /* EOB flag */ while (arith_decode(cinfo, st + 1) == 0) { st += 3; k++; if (k > cinfo->Se) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* spectral overflow */ return TRUE; } } /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */ sign = arith_decode(cinfo, entropy->fixed_bin); st += 2; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { if (arith_decode(cinfo, st)) { m <<= 1; st = entropy->ac_stats[tbl] + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* magnitude overflow */ return TRUE; } st += 1; } } } v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m; v += 1; if (sign) v = -v; /* Scale and output coefficient in natural (dezigzagged) order */ (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); } return TRUE; } /* * MCU decoding for DC successive approximation refinement scan. */ METHODDEF(boolean) decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; unsigned char *st; int p1, blkn; /* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) process_restart(cinfo); entropy->restarts_to_go--; } st = entropy->fixed_bin; /* use fixed probability estimation */ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ /* Outer loop handles each block in the MCU */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { /* Encoded data is simply the next bit of the two's-complement DC value */ if (arith_decode(cinfo, st)) MCU_data[blkn][0][0] |= p1; } return TRUE; } /* * MCU decoding for AC successive approximation refinement scan. */ METHODDEF(boolean) decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; JCOEFPTR thiscoef; unsigned char *st; int tbl, k, kex; int p1, m1; /* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) process_restart(cinfo); entropy->restarts_to_go--; } if (entropy->ct == -1) return TRUE; /* if error do nothing */ /* There is always only one block per MCU */ block = MCU_data[0]; tbl = cinfo->cur_comp_info[0]->ac_tbl_no; p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ /* Establish EOBx (previous stage end-of-block) index */ for (kex = cinfo->Se; kex > 0; kex--) if ((*block)[jpeg_natural_order[kex]]) break; for (k = cinfo->Ss; k <= cinfo->Se; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); if (k > kex) if (arith_decode(cinfo, st)) break; /* EOB flag */ for (;;) { thiscoef = *block + jpeg_natural_order[k]; if (*thiscoef) { /* previously nonzero coef */ if (arith_decode(cinfo, st + 2)) { if (*thiscoef < 0) *thiscoef += m1; else *thiscoef += p1; } break; } if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ if (arith_decode(cinfo, entropy->fixed_bin)) *thiscoef = m1; else *thiscoef = p1; break; } st += 3; k++; if (k > cinfo->Se) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* spectral overflow */ return TRUE; } } } return TRUE; } /* * Decode one MCU's worth of arithmetic-compressed coefficients. */ METHODDEF(boolean) decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; jpeg_component_info * compptr; JBLOCKROW block; unsigned char *st; int blkn, ci, tbl, sign, k; int v, m; /* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) process_restart(cinfo); entropy->restarts_to_go--; } if (entropy->ct == -1) return TRUE; /* if error do nothing */ /* Outer loop handles each block in the MCU */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ tbl = compptr->dc_tbl_no; /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; /* Figure F.19: Decode_DC_DIFF */ if (arith_decode(cinfo, st) == 0) entropy->dc_context[ci] = 0; else { /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */ sign = arith_decode(cinfo, st + 1); st += 2; st += sign; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* magnitude overflow */ return TRUE; } st += 1; } } /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) entropy->dc_context[ci] = 0; /* zero diff category */ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ else entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m; v += 1; if (sign) v = -v; entropy->last_dc_val[ci] += v; } (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ tbl = compptr->ac_tbl_no; /* Figure F.20: Decode_AC_coefficients */ for (k = 1; k <= DCTSIZE2 - 1; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); if (arith_decode(cinfo, st)) break; /* EOB flag */ while (arith_decode(cinfo, st + 1) == 0) { st += 3; k++; if (k > DCTSIZE2 - 1) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* spectral overflow */ return TRUE; } } /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */ sign = arith_decode(cinfo, entropy->fixed_bin); st += 2; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { if (arith_decode(cinfo, st)) { m <<= 1; st = entropy->ac_stats[tbl] + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) { WARNMS(cinfo, JWRN_ARITH_BAD_CODE); entropy->ct = -1; /* magnitude overflow */ return TRUE; } st += 1; } } } v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m; v += 1; if (sign) v = -v; (*block)[jpeg_natural_order[k]] = (JCOEF) v; } } return TRUE; } /* * Initialize for an arithmetic-compressed scan. */ METHODDEF(void) start_pass (j_decompress_ptr cinfo) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; int ci, tbl; jpeg_component_info * compptr; if (cinfo->progressive_mode) { /* Validate progressive scan parameters */ if (cinfo->Ss == 0) { if (cinfo->Se != 0) goto bad; } else { /* need not check Ss/Se < 0 since they came from unsigned bytes */ if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) goto bad; /* AC scans may have only one component */ if (cinfo->comps_in_scan != 1) goto bad; } if (cinfo->Ah != 0) { /* Successive approximation refinement scan: must have Al = Ah-1. */ if (cinfo->Ah-1 != cinfo->Al) goto bad; } if (cinfo->Al > 13) { /* need not check for < 0 */ bad: ERREXIT4(cinfo, JERR_BAD_PROGRESSION, cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); } /* Update progression status, and verify that scan order is legal. * Note that inter-scan inconsistencies are treated as warnings * not fatal errors ... not clear if this is right way to behave. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; if (cinfo->Ah != expected) WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); coef_bit_ptr[coefi] = cinfo->Al; } } /* Select MCU decoding routine */ if (cinfo->Ah == 0) { if (cinfo->Ss == 0) entropy->pub.decode_mcu = decode_mcu_DC_first; else entropy->pub.decode_mcu = decode_mcu_AC_first; } else { if (cinfo->Ss == 0) entropy->pub.decode_mcu = decode_mcu_DC_refine; else entropy->pub.decode_mcu = decode_mcu_AC_refine; } } else { /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. * This ought to be an error condition, but we make it a warning. */ if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1)) WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); /* Select MCU decoding routine */ entropy->pub.decode_mcu = decode_mcu; } /* Allocate & initialize requested statistics areas */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { tbl = compptr->dc_tbl_no; if (tbl < 0 || tbl >= NUM_ARITH_TBLS) ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); if (entropy->dc_stats[tbl] == NULL) entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); /* Initialize DC predictions to 0 */ entropy->last_dc_val[ci] = 0; entropy->dc_context[ci] = 0; } if (! cinfo->progressive_mode || cinfo->Ss) { tbl = compptr->ac_tbl_no; if (tbl < 0 || tbl >= NUM_ARITH_TBLS) ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); if (entropy->ac_stats[tbl] == NULL) entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); } } /* Initialize arithmetic decoding variables */ entropy->c = 0; entropy->a = 0; entropy->ct = -16; /* force reading 2 initial bytes to fill C */ /* Initialize restart counter */ entropy->restarts_to_go = cinfo->restart_interval; } /* * Module initialization routine for arithmetic entropy decoding. */ GLOBAL(void) jinit_arith_decoder (j_decompress_ptr cinfo) { arith_entropy_ptr entropy; int i; entropy = (arith_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(arith_entropy_decoder)); cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; entropy->pub.start_pass = start_pass; /* Mark tables unallocated */ for (i = 0; i < NUM_ARITH_TBLS; i++) { entropy->dc_stats[i] = NULL; entropy->ac_stats[i] = NULL; } /* Initialize index for fixed probability estimation */ entropy->fixed_bin[0] = 113; if (cinfo->progressive_mode) { /* Create progression status table */ int *coef_bit_ptr, ci; cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->num_components*DCTSIZE2*SIZEOF(int)); coef_bit_ptr = & cinfo->coef_bits[0][0]; for (ci = 0; ci < cinfo->num_components; ci++) for (i = 0; i < DCTSIZE2; i++) *coef_bit_ptr++ = -1; } } glmark2-2012.08/./src/libjpeg-turbo/jmemmgr.c0000664000175000017500000012226212013417376020061 0ustar alfalf00000000000000/* * jmemmgr.c * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the JPEG system-independent memory management * routines. This code is usable across a wide variety of machines; most * of the system dependencies have been isolated in a separate file. * The major functions provided here are: * * pool-based allocation and freeing of memory; * * policy decisions about how to divide available memory among the * virtual arrays; * * control logic for swapping virtual arrays between main memory and * backing storage. * The separate system-dependent file provides the actual backing-storage * access code, and it contains the policy decision about how much total * main memory to use. * This file is system-dependent in the sense that some of its functions * are unnecessary in some systems. For example, if there is enough virtual * memory so that backing storage will never be used, much of the virtual * array control logic could be removed. (Of course, if you have that much * memory then you shouldn't care about a little bit of unused code...) */ #define JPEG_INTERNALS #define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */ #include "jinclude.h" #include "jpeglib.h" #include "jmemsys.h" /* import the system-dependent declarations */ #ifndef NO_GETENV #ifndef HAVE_STDLIB_H /* should declare getenv() */ extern char * getenv JPP((const char * name)); #endif #endif LOCAL(size_t) round_up_pow2 (size_t a, size_t b) /* a rounded up to the next multiple of b, i.e. ceil(a/b)*b */ /* Assumes a >= 0, b > 0, and b is a power of 2 */ { return ((a + b - 1) & (~(b - 1))); } /* * Some important notes: * The allocation routines provided here must never return NULL. * They should exit to error_exit if unsuccessful. * * It's not a good idea to try to merge the sarray and barray routines, * even though they are textually almost the same, because samples are * usually stored as bytes while coefficients are shorts or ints. Thus, * in machines where byte pointers have a different representation from * word pointers, the resulting machine code could not be the same. */ /* * Many machines require storage alignment: longs must start on 4-byte * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc() * always returns pointers that are multiples of the worst-case alignment * requirement, and we had better do so too. * There isn't any really portable way to determine the worst-case alignment * requirement. This module assumes that the alignment requirement is * multiples of ALIGN_SIZE. * By default, we define ALIGN_SIZE as sizeof(double). This is necessary on some * workstations (where doubles really do need 8-byte alignment) and will work * fine on nearly everything. If your machine has lesser alignment needs, * you can save a few bytes by making ALIGN_SIZE smaller. * The only place I know of where this will NOT work is certain Macintosh * 680x0 compilers that define double as a 10-byte IEEE extended float. * Doing 10-byte alignment is counterproductive because longwords won't be * aligned well. Put "#define ALIGN_SIZE 4" in jconfig.h if you have * such a compiler. */ #ifndef ALIGN_SIZE /* so can override from jconfig.h */ #ifndef WITH_SIMD #define ALIGN_SIZE SIZEOF(double) #else #define ALIGN_SIZE 16 /* Most SIMD implementations require this */ #endif #endif /* * We allocate objects from "pools", where each pool is gotten with a single * request to jpeg_get_small() or jpeg_get_large(). There is no per-object * overhead within a pool, except for alignment padding. Each pool has a * header with a link to the next pool of the same class. * Small and large pool headers are identical except that the latter's * link pointer must be FAR on 80x86 machines. */ typedef struct small_pool_struct * small_pool_ptr; typedef struct small_pool_struct { small_pool_ptr next; /* next in list of pools */ size_t bytes_used; /* how many bytes already used within pool */ size_t bytes_left; /* bytes still available in this pool */ } small_pool_hdr; typedef struct large_pool_struct FAR * large_pool_ptr; typedef struct large_pool_struct { large_pool_ptr next; /* next in list of pools */ size_t bytes_used; /* how many bytes already used within pool */ size_t bytes_left; /* bytes still available in this pool */ } large_pool_hdr; /* * Here is the full definition of a memory manager object. */ typedef struct { struct jpeg_memory_mgr pub; /* public fields */ /* Each pool identifier (lifetime class) names a linked list of pools. */ small_pool_ptr small_list[JPOOL_NUMPOOLS]; large_pool_ptr large_list[JPOOL_NUMPOOLS]; /* Since we only have one lifetime class of virtual arrays, only one * linked list is necessary (for each datatype). Note that the virtual * array control blocks being linked together are actually stored somewhere * in the small-pool list. */ jvirt_sarray_ptr virt_sarray_list; jvirt_barray_ptr virt_barray_list; /* This counts total space obtained from jpeg_get_small/large */ size_t total_space_allocated; /* alloc_sarray and alloc_barray set this value for use by virtual * array routines. */ JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */ } my_memory_mgr; typedef my_memory_mgr * my_mem_ptr; /* * The control blocks for virtual arrays. * Note that these blocks are allocated in the "small" pool area. * System-dependent info for the associated backing store (if any) is hidden * inside the backing_store_info struct. */ struct jvirt_sarray_control { JSAMPARRAY mem_buffer; /* => the in-memory buffer */ JDIMENSION rows_in_array; /* total virtual array height */ JDIMENSION samplesperrow; /* width of array (and of memory buffer) */ JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */ JDIMENSION rows_in_mem; /* height of memory buffer */ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ JDIMENSION cur_start_row; /* first logical row # in the buffer */ JDIMENSION first_undef_row; /* row # of first uninitialized row */ boolean pre_zero; /* pre-zero mode requested? */ boolean dirty; /* do current buffer contents need written? */ boolean b_s_open; /* is backing-store data valid? */ jvirt_sarray_ptr next; /* link to next virtual sarray control block */ backing_store_info b_s_info; /* System-dependent control info */ }; struct jvirt_barray_control { JBLOCKARRAY mem_buffer; /* => the in-memory buffer */ JDIMENSION rows_in_array; /* total virtual array height */ JDIMENSION blocksperrow; /* width of array (and of memory buffer) */ JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */ JDIMENSION rows_in_mem; /* height of memory buffer */ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ JDIMENSION cur_start_row; /* first logical row # in the buffer */ JDIMENSION first_undef_row; /* row # of first uninitialized row */ boolean pre_zero; /* pre-zero mode requested? */ boolean dirty; /* do current buffer contents need written? */ boolean b_s_open; /* is backing-store data valid? */ jvirt_barray_ptr next; /* link to next virtual barray control block */ backing_store_info b_s_info; /* System-dependent control info */ }; #ifdef MEM_STATS /* optional extra stuff for statistics */ LOCAL(void) print_mem_stats (j_common_ptr cinfo, int pool_id) { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; small_pool_ptr shdr_ptr; large_pool_ptr lhdr_ptr; /* Since this is only a debugging stub, we can cheat a little by using * fprintf directly rather than going through the trace message code. * This is helpful because message parm array can't handle longs. */ fprintf(stderr, "Freeing pool %d, total space = %ld\n", pool_id, mem->total_space_allocated); for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL; lhdr_ptr = lhdr_ptr->next) { fprintf(stderr, " Large chunk used %ld\n", (long) lhdr_ptr->bytes_used); } for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL; shdr_ptr = shdr_ptr->next) { fprintf(stderr, " Small chunk used %ld free %ld\n", (long) shdr_ptr->bytes_used, (long) shdr_ptr->bytes_left); } } #endif /* MEM_STATS */ LOCAL(void) out_of_memory (j_common_ptr cinfo, int which) /* Report an out-of-memory error and stop execution */ /* If we compiled MEM_STATS support, report alloc requests before dying */ { #ifdef MEM_STATS cinfo->err->trace_level = 2; /* force self_destruct to report stats */ #endif ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which); } /* * Allocation of "small" objects. * * For these, we use pooled storage. When a new pool must be created, * we try to get enough space for the current request plus a "slop" factor, * where the slop will be the amount of leftover space in the new pool. * The speed vs. space tradeoff is largely determined by the slop values. * A different slop value is provided for each pool class (lifetime), * and we also distinguish the first pool of a class from later ones. * NOTE: the values given work fairly well on both 16- and 32-bit-int * machines, but may be too small if longs are 64 bits or more. * * Since we do not know what alignment malloc() gives us, we have to * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment * adjustment. */ static const size_t first_pool_slop[JPOOL_NUMPOOLS] = { 1600, /* first PERMANENT pool */ 16000 /* first IMAGE pool */ }; static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = { 0, /* additional PERMANENT pools */ 5000 /* additional IMAGE pools */ }; #define MIN_SLOP 50 /* greater than 0 to avoid futile looping */ METHODDEF(void *) alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject) /* Allocate a "small" object */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; small_pool_ptr hdr_ptr, prev_hdr_ptr; char * data_ptr; size_t min_request, slop; /* * Round up the requested size to a multiple of ALIGN_SIZE in order * to assure alignment for the next object allocated in the same pool * and so that algorithms can straddle outside the proper area up * to the next alignment. */ sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); /* Check for unsatisfiable request (do now to ensure no overflow below) */ if ((SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK) out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ /* See if space is available in any existing pool */ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ prev_hdr_ptr = NULL; hdr_ptr = mem->small_list[pool_id]; while (hdr_ptr != NULL) { if (hdr_ptr->bytes_left >= sizeofobject) break; /* found pool with enough space */ prev_hdr_ptr = hdr_ptr; hdr_ptr = hdr_ptr->next; } /* Time to make a new pool? */ if (hdr_ptr == NULL) { /* min_request is what we need now, slop is what will be leftover */ min_request = SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1; if (prev_hdr_ptr == NULL) /* first pool in class? */ slop = first_pool_slop[pool_id]; else slop = extra_pool_slop[pool_id]; /* Don't ask for more than MAX_ALLOC_CHUNK */ if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request)) slop = (size_t) (MAX_ALLOC_CHUNK-min_request); /* Try to get space, if fail reduce slop and try again */ for (;;) { hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop); if (hdr_ptr != NULL) break; slop /= 2; if (slop < MIN_SLOP) /* give up when it gets real small */ out_of_memory(cinfo, 2); /* jpeg_get_small failed */ } mem->total_space_allocated += min_request + slop; /* Success, initialize the new pool header and add to end of list */ hdr_ptr->next = NULL; hdr_ptr->bytes_used = 0; hdr_ptr->bytes_left = sizeofobject + slop; if (prev_hdr_ptr == NULL) /* first pool in class? */ mem->small_list[pool_id] = hdr_ptr; else prev_hdr_ptr->next = hdr_ptr; } /* OK, allocate the object from the current pool */ data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */ data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */ if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */ data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE; data_ptr += hdr_ptr->bytes_used; /* point to place for object */ hdr_ptr->bytes_used += sizeofobject; hdr_ptr->bytes_left -= sizeofobject; return (void *) data_ptr; } /* * Allocation of "large" objects. * * The external semantics of these are the same as "small" objects, * except that FAR pointers are used on 80x86. However the pool * management heuristics are quite different. We assume that each * request is large enough that it may as well be passed directly to * jpeg_get_large; the pool management just links everything together * so that we can free it all on demand. * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY * structures. The routines that create these structures (see below) * deliberately bunch rows together to ensure a large request size. */ METHODDEF(void FAR *) alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject) /* Allocate a "large" object */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; large_pool_ptr hdr_ptr; char FAR * data_ptr; /* * Round up the requested size to a multiple of ALIGN_SIZE so that * algorithms can straddle outside the proper area up to the next * alignment. */ sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); /* Check for unsatisfiable request (do now to ensure no overflow below) */ if ((SIZEOF(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK) out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ /* Always make a new pool */ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject + SIZEOF(large_pool_hdr) + ALIGN_SIZE - 1); if (hdr_ptr == NULL) out_of_memory(cinfo, 4); /* jpeg_get_large failed */ mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr) + ALIGN_SIZE - 1; /* Success, initialize the new pool header and add to list */ hdr_ptr->next = mem->large_list[pool_id]; /* We maintain space counts in each pool header for statistical purposes, * even though they are not needed for allocation. */ hdr_ptr->bytes_used = sizeofobject; hdr_ptr->bytes_left = 0; mem->large_list[pool_id] = hdr_ptr; data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */ data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */ if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */ data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE; return (void FAR *) data_ptr; } /* * Creation of 2-D sample arrays. * The pointers are in near heap, the samples themselves in FAR heap. * * To minimize allocation overhead and to allow I/O of large contiguous * blocks, we allocate the sample rows in groups of as many rows as possible * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request. * NB: the virtual array control routines, later in this file, know about * this chunking of rows. The rowsperchunk value is left in the mem manager * object so that it can be saved away if this sarray is the workspace for * a virtual array. * * Since we are often upsampling with a factor 2, we align the size (not * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have * to be as careful about size. */ METHODDEF(JSAMPARRAY) alloc_sarray (j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows) /* Allocate a 2-D sample array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; JSAMPARRAY result; JSAMPROW workspace; JDIMENSION rowsperchunk, currow, i; long ltemp; /* Make sure each row is properly aligned */ if ((ALIGN_SIZE % SIZEOF(JSAMPLE)) != 0) out_of_memory(cinfo, 5); /* safety check */ samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) / SIZEOF(JSAMPLE)); /* Calculate max # of rows allowed in one allocation chunk */ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / ((long) samplesperrow * SIZEOF(JSAMPLE)); if (ltemp <= 0) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); if (ltemp < (long) numrows) rowsperchunk = (JDIMENSION) ltemp; else rowsperchunk = numrows; mem->last_rowsperchunk = rowsperchunk; /* Get space for row pointers (small object) */ result = (JSAMPARRAY) alloc_small(cinfo, pool_id, (size_t) (numrows * SIZEOF(JSAMPROW))); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { rowsperchunk = MIN(rowsperchunk, numrows - currow); workspace = (JSAMPROW) alloc_large(cinfo, pool_id, (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow * SIZEOF(JSAMPLE))); for (i = rowsperchunk; i > 0; i--) { result[currow++] = workspace; workspace += samplesperrow; } } return result; } /* * Creation of 2-D coefficient-block arrays. * This is essentially the same as the code for sample arrays, above. */ METHODDEF(JBLOCKARRAY) alloc_barray (j_common_ptr cinfo, int pool_id, JDIMENSION blocksperrow, JDIMENSION numrows) /* Allocate a 2-D coefficient-block array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; JBLOCKARRAY result; JBLOCKROW workspace; JDIMENSION rowsperchunk, currow, i; long ltemp; /* Make sure each row is properly aligned */ if ((SIZEOF(JBLOCK) % ALIGN_SIZE) != 0) out_of_memory(cinfo, 6); /* safety check */ /* Calculate max # of rows allowed in one allocation chunk */ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / ((long) blocksperrow * SIZEOF(JBLOCK)); if (ltemp <= 0) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); if (ltemp < (long) numrows) rowsperchunk = (JDIMENSION) ltemp; else rowsperchunk = numrows; mem->last_rowsperchunk = rowsperchunk; /* Get space for row pointers (small object) */ result = (JBLOCKARRAY) alloc_small(cinfo, pool_id, (size_t) (numrows * SIZEOF(JBLOCKROW))); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { rowsperchunk = MIN(rowsperchunk, numrows - currow); workspace = (JBLOCKROW) alloc_large(cinfo, pool_id, (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow * SIZEOF(JBLOCK))); for (i = rowsperchunk; i > 0; i--) { result[currow++] = workspace; workspace += blocksperrow; } } return result; } /* * About virtual array management: * * The above "normal" array routines are only used to allocate strip buffers * (as wide as the image, but just a few rows high). Full-image-sized buffers * are handled as "virtual" arrays. The array is still accessed a strip at a * time, but the memory manager must save the whole array for repeated * accesses. The intended implementation is that there is a strip buffer in * memory (as high as is possible given the desired memory limit), plus a * backing file that holds the rest of the array. * * The request_virt_array routines are told the total size of the image and * the maximum number of rows that will be accessed at once. The in-memory * buffer must be at least as large as the maxaccess value. * * The request routines create control blocks but not the in-memory buffers. * That is postponed until realize_virt_arrays is called. At that time the * total amount of space needed is known (approximately, anyway), so free * memory can be divided up fairly. * * The access_virt_array routines are responsible for making a specific strip * area accessible (after reading or writing the backing file, if necessary). * Note that the access routines are told whether the caller intends to modify * the accessed strip; during a read-only pass this saves having to rewrite * data to disk. The access routines are also responsible for pre-zeroing * any newly accessed rows, if pre-zeroing was requested. * * In current usage, the access requests are usually for nonoverlapping * strips; that is, successive access start_row numbers differ by exactly * num_rows = maxaccess. This means we can get good performance with simple * buffer dump/reload logic, by making the in-memory buffer be a multiple * of the access height; then there will never be accesses across bufferload * boundaries. The code will still work with overlapping access requests, * but it doesn't handle bufferload overlaps very efficiently. */ METHODDEF(jvirt_sarray_ptr) request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION samplesperrow, JDIMENSION numrows, JDIMENSION maxaccess) /* Request a virtual 2-D sample array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; jvirt_sarray_ptr result; /* Only IMAGE-lifetime virtual arrays are currently supported */ if (pool_id != JPOOL_IMAGE) ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ /* get control block */ result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id, SIZEOF(struct jvirt_sarray_control)); result->mem_buffer = NULL; /* marks array not yet realized */ result->rows_in_array = numrows; result->samplesperrow = samplesperrow; result->maxaccess = maxaccess; result->pre_zero = pre_zero; result->b_s_open = FALSE; /* no associated backing-store object */ result->next = mem->virt_sarray_list; /* add to list of virtual arrays */ mem->virt_sarray_list = result; return result; } METHODDEF(jvirt_barray_ptr) request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION blocksperrow, JDIMENSION numrows, JDIMENSION maxaccess) /* Request a virtual 2-D coefficient-block array */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; jvirt_barray_ptr result; /* Only IMAGE-lifetime virtual arrays are currently supported */ if (pool_id != JPOOL_IMAGE) ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ /* get control block */ result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id, SIZEOF(struct jvirt_barray_control)); result->mem_buffer = NULL; /* marks array not yet realized */ result->rows_in_array = numrows; result->blocksperrow = blocksperrow; result->maxaccess = maxaccess; result->pre_zero = pre_zero; result->b_s_open = FALSE; /* no associated backing-store object */ result->next = mem->virt_barray_list; /* add to list of virtual arrays */ mem->virt_barray_list = result; return result; } METHODDEF(void) realize_virt_arrays (j_common_ptr cinfo) /* Allocate the in-memory buffers for any unrealized virtual arrays */ { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; size_t space_per_minheight, maximum_space, avail_mem; size_t minheights, max_minheights; jvirt_sarray_ptr sptr; jvirt_barray_ptr bptr; /* Compute the minimum space needed (maxaccess rows in each buffer) * and the maximum space needed (full image height in each buffer). * These may be of use to the system-dependent jpeg_mem_available routine. */ space_per_minheight = 0; maximum_space = 0; for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { if (sptr->mem_buffer == NULL) { /* if not realized yet */ space_per_minheight += (long) sptr->maxaccess * (long) sptr->samplesperrow * SIZEOF(JSAMPLE); maximum_space += (long) sptr->rows_in_array * (long) sptr->samplesperrow * SIZEOF(JSAMPLE); } } for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { if (bptr->mem_buffer == NULL) { /* if not realized yet */ space_per_minheight += (long) bptr->maxaccess * (long) bptr->blocksperrow * SIZEOF(JBLOCK); maximum_space += (long) bptr->rows_in_array * (long) bptr->blocksperrow * SIZEOF(JBLOCK); } } if (space_per_minheight <= 0) return; /* no unrealized arrays, no work */ /* Determine amount of memory to actually use; this is system-dependent. */ avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space, mem->total_space_allocated); /* If the maximum space needed is available, make all the buffers full * height; otherwise parcel it out with the same number of minheights * in each buffer. */ if (avail_mem >= maximum_space) max_minheights = 1000000000L; else { max_minheights = avail_mem / space_per_minheight; /* If there doesn't seem to be enough space, try to get the minimum * anyway. This allows a "stub" implementation of jpeg_mem_available(). */ if (max_minheights <= 0) max_minheights = 1; } /* Allocate the in-memory buffers and initialize backing store as needed. */ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { if (sptr->mem_buffer == NULL) { /* if not realized yet */ minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L; if (minheights <= max_minheights) { /* This buffer fits in memory */ sptr->rows_in_mem = sptr->rows_in_array; } else { /* It doesn't fit in memory, create backing store. */ sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess); jpeg_open_backing_store(cinfo, & sptr->b_s_info, (long) sptr->rows_in_array * (long) sptr->samplesperrow * (long) SIZEOF(JSAMPLE)); sptr->b_s_open = TRUE; } sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE, sptr->samplesperrow, sptr->rows_in_mem); sptr->rowsperchunk = mem->last_rowsperchunk; sptr->cur_start_row = 0; sptr->first_undef_row = 0; sptr->dirty = FALSE; } } for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { if (bptr->mem_buffer == NULL) { /* if not realized yet */ minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L; if (minheights <= max_minheights) { /* This buffer fits in memory */ bptr->rows_in_mem = bptr->rows_in_array; } else { /* It doesn't fit in memory, create backing store. */ bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess); jpeg_open_backing_store(cinfo, & bptr->b_s_info, (long) bptr->rows_in_array * (long) bptr->blocksperrow * (long) SIZEOF(JBLOCK)); bptr->b_s_open = TRUE; } bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE, bptr->blocksperrow, bptr->rows_in_mem); bptr->rowsperchunk = mem->last_rowsperchunk; bptr->cur_start_row = 0; bptr->first_undef_row = 0; bptr->dirty = FALSE; } } } LOCAL(void) do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing) /* Do backing store read or write of a virtual sample array */ { long bytesperrow, file_offset, byte_count, rows, thisrow, i; bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE); file_offset = ptr->cur_start_row * bytesperrow; /* Loop to read or write each allocation chunk in mem_buffer */ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { /* One chunk, but check for short chunk at end of buffer */ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); /* Transfer no more than is currently defined */ thisrow = (long) ptr->cur_start_row + i; rows = MIN(rows, (long) ptr->first_undef_row - thisrow); /* Transfer no more than fits in file */ rows = MIN(rows, (long) ptr->rows_in_array - thisrow); if (rows <= 0) /* this chunk might be past end of file! */ break; byte_count = rows * bytesperrow; if (writing) (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, (void FAR *) ptr->mem_buffer[i], file_offset, byte_count); else (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, (void FAR *) ptr->mem_buffer[i], file_offset, byte_count); file_offset += byte_count; } } LOCAL(void) do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing) /* Do backing store read or write of a virtual coefficient-block array */ { long bytesperrow, file_offset, byte_count, rows, thisrow, i; bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK); file_offset = ptr->cur_start_row * bytesperrow; /* Loop to read or write each allocation chunk in mem_buffer */ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { /* One chunk, but check for short chunk at end of buffer */ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); /* Transfer no more than is currently defined */ thisrow = (long) ptr->cur_start_row + i; rows = MIN(rows, (long) ptr->first_undef_row - thisrow); /* Transfer no more than fits in file */ rows = MIN(rows, (long) ptr->rows_in_array - thisrow); if (rows <= 0) /* this chunk might be past end of file! */ break; byte_count = rows * bytesperrow; if (writing) (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, (void FAR *) ptr->mem_buffer[i], file_offset, byte_count); else (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, (void FAR *) ptr->mem_buffer[i], file_offset, byte_count); file_offset += byte_count; } } METHODDEF(JSAMPARRAY) access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable) /* Access the part of a virtual sample array starting at start_row */ /* and extending for num_rows rows. writable is true if */ /* caller intends to modify the accessed area. */ { JDIMENSION end_row = start_row + num_rows; JDIMENSION undef_row; /* debugging check */ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || ptr->mem_buffer == NULL) ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); /* Make the desired part of the virtual array accessible */ if (start_row < ptr->cur_start_row || end_row > ptr->cur_start_row+ptr->rows_in_mem) { if (! ptr->b_s_open) ERREXIT(cinfo, JERR_VIRTUAL_BUG); /* Flush old buffer contents if necessary */ if (ptr->dirty) { do_sarray_io(cinfo, ptr, TRUE); ptr->dirty = FALSE; } /* Decide what part of virtual array to access. * Algorithm: if target address > current window, assume forward scan, * load starting at target address. If target address < current window, * assume backward scan, load so that target area is top of window. * Note that when switching from forward write to forward read, will have * start_row = 0, so the limiting case applies and we load from 0 anyway. */ if (start_row > ptr->cur_start_row) { ptr->cur_start_row = start_row; } else { /* use long arithmetic here to avoid overflow & unsigned problems */ long ltemp; ltemp = (long) end_row - (long) ptr->rows_in_mem; if (ltemp < 0) ltemp = 0; /* don't fall off front end of file */ ptr->cur_start_row = (JDIMENSION) ltemp; } /* Read in the selected part of the array. * During the initial write pass, we will do no actual read * because the selected part is all undefined. */ do_sarray_io(cinfo, ptr, FALSE); } /* Ensure the accessed part of the array is defined; prezero if needed. * To improve locality of access, we only prezero the part of the array * that the caller is about to access, not the entire in-memory array. */ if (ptr->first_undef_row < end_row) { if (ptr->first_undef_row < start_row) { if (writable) /* writer skipped over a section of array */ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); undef_row = start_row; /* but reader is allowed to read ahead */ } else { undef_row = ptr->first_undef_row; } if (writable) ptr->first_undef_row = end_row; if (ptr->pre_zero) { size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE); undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ end_row -= ptr->cur_start_row; while (undef_row < end_row) { jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); undef_row++; } } else { if (! writable) /* reader looking at undefined data */ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); } } /* Flag the buffer dirty if caller will write in it */ if (writable) ptr->dirty = TRUE; /* Return address of proper part of the buffer */ return ptr->mem_buffer + (start_row - ptr->cur_start_row); } METHODDEF(JBLOCKARRAY) access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable) /* Access the part of a virtual block array starting at start_row */ /* and extending for num_rows rows. writable is true if */ /* caller intends to modify the accessed area. */ { JDIMENSION end_row = start_row + num_rows; JDIMENSION undef_row; /* debugging check */ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || ptr->mem_buffer == NULL) ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); /* Make the desired part of the virtual array accessible */ if (start_row < ptr->cur_start_row || end_row > ptr->cur_start_row+ptr->rows_in_mem) { if (! ptr->b_s_open) ERREXIT(cinfo, JERR_VIRTUAL_BUG); /* Flush old buffer contents if necessary */ if (ptr->dirty) { do_barray_io(cinfo, ptr, TRUE); ptr->dirty = FALSE; } /* Decide what part of virtual array to access. * Algorithm: if target address > current window, assume forward scan, * load starting at target address. If target address < current window, * assume backward scan, load so that target area is top of window. * Note that when switching from forward write to forward read, will have * start_row = 0, so the limiting case applies and we load from 0 anyway. */ if (start_row > ptr->cur_start_row) { ptr->cur_start_row = start_row; } else { /* use long arithmetic here to avoid overflow & unsigned problems */ long ltemp; ltemp = (long) end_row - (long) ptr->rows_in_mem; if (ltemp < 0) ltemp = 0; /* don't fall off front end of file */ ptr->cur_start_row = (JDIMENSION) ltemp; } /* Read in the selected part of the array. * During the initial write pass, we will do no actual read * because the selected part is all undefined. */ do_barray_io(cinfo, ptr, FALSE); } /* Ensure the accessed part of the array is defined; prezero if needed. * To improve locality of access, we only prezero the part of the array * that the caller is about to access, not the entire in-memory array. */ if (ptr->first_undef_row < end_row) { if (ptr->first_undef_row < start_row) { if (writable) /* writer skipped over a section of array */ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); undef_row = start_row; /* but reader is allowed to read ahead */ } else { undef_row = ptr->first_undef_row; } if (writable) ptr->first_undef_row = end_row; if (ptr->pre_zero) { size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK); undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ end_row -= ptr->cur_start_row; while (undef_row < end_row) { jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); undef_row++; } } else { if (! writable) /* reader looking at undefined data */ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); } } /* Flag the buffer dirty if caller will write in it */ if (writable) ptr->dirty = TRUE; /* Return address of proper part of the buffer */ return ptr->mem_buffer + (start_row - ptr->cur_start_row); } /* * Release all objects belonging to a specified pool. */ METHODDEF(void) free_pool (j_common_ptr cinfo, int pool_id) { my_mem_ptr mem = (my_mem_ptr) cinfo->mem; small_pool_ptr shdr_ptr; large_pool_ptr lhdr_ptr; size_t space_freed; if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ #ifdef MEM_STATS if (cinfo->err->trace_level > 1) print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */ #endif /* If freeing IMAGE pool, close any virtual arrays first */ if (pool_id == JPOOL_IMAGE) { jvirt_sarray_ptr sptr; jvirt_barray_ptr bptr; for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { if (sptr->b_s_open) { /* there may be no backing store */ sptr->b_s_open = FALSE; /* prevent recursive close if error */ (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info); } } mem->virt_sarray_list = NULL; for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { if (bptr->b_s_open) { /* there may be no backing store */ bptr->b_s_open = FALSE; /* prevent recursive close if error */ (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info); } } mem->virt_barray_list = NULL; } /* Release large objects */ lhdr_ptr = mem->large_list[pool_id]; mem->large_list[pool_id] = NULL; while (lhdr_ptr != NULL) { large_pool_ptr next_lhdr_ptr = lhdr_ptr->next; space_freed = lhdr_ptr->bytes_used + lhdr_ptr->bytes_left + SIZEOF(large_pool_hdr); jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed); mem->total_space_allocated -= space_freed; lhdr_ptr = next_lhdr_ptr; } /* Release small objects */ shdr_ptr = mem->small_list[pool_id]; mem->small_list[pool_id] = NULL; while (shdr_ptr != NULL) { small_pool_ptr next_shdr_ptr = shdr_ptr->next; space_freed = shdr_ptr->bytes_used + shdr_ptr->bytes_left + SIZEOF(small_pool_hdr); jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed); mem->total_space_allocated -= space_freed; shdr_ptr = next_shdr_ptr; } } /* * Close up shop entirely. * Note that this cannot be called unless cinfo->mem is non-NULL. */ METHODDEF(void) self_destruct (j_common_ptr cinfo) { int pool; /* Close all backing store, release all memory. * Releasing pools in reverse order might help avoid fragmentation * with some (brain-damaged) malloc libraries. */ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { free_pool(cinfo, pool); } /* Release the memory manager control block too. */ jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr)); cinfo->mem = NULL; /* ensures I will be called only once */ jpeg_mem_term(cinfo); /* system-dependent cleanup */ } /* * Memory manager initialization. * When this is called, only the error manager pointer is valid in cinfo! */ GLOBAL(void) jinit_memory_mgr (j_common_ptr cinfo) { my_mem_ptr mem; long max_to_use; int pool; size_t test_mac; cinfo->mem = NULL; /* for safety if init fails */ /* Check for configuration errors. * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably * doesn't reflect any real hardware alignment requirement. * The test is a little tricky: for X>0, X and X-1 have no one-bits * in common if and only if X is a power of 2, ie has only one one-bit. * Some compilers may give an "unreachable code" warning here; ignore it. */ if ((ALIGN_SIZE & (ALIGN_SIZE-1)) != 0) ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE); /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be * a multiple of ALIGN_SIZE. * Again, an "unreachable code" warning may be ignored here. * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK. */ test_mac = (size_t) MAX_ALLOC_CHUNK; if ((long) test_mac != MAX_ALLOC_CHUNK || (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0) ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK); max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */ /* Attempt to allocate memory manager's control block */ mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr)); if (mem == NULL) { jpeg_mem_term(cinfo); /* system-dependent cleanup */ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0); } /* OK, fill in the method pointers */ mem->pub.alloc_small = alloc_small; mem->pub.alloc_large = alloc_large; mem->pub.alloc_sarray = alloc_sarray; mem->pub.alloc_barray = alloc_barray; mem->pub.request_virt_sarray = request_virt_sarray; mem->pub.request_virt_barray = request_virt_barray; mem->pub.realize_virt_arrays = realize_virt_arrays; mem->pub.access_virt_sarray = access_virt_sarray; mem->pub.access_virt_barray = access_virt_barray; mem->pub.free_pool = free_pool; mem->pub.self_destruct = self_destruct; /* Make MAX_ALLOC_CHUNK accessible to other modules */ mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK; /* Initialize working state */ mem->pub.max_memory_to_use = max_to_use; for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { mem->small_list[pool] = NULL; mem->large_list[pool] = NULL; } mem->virt_sarray_list = NULL; mem->virt_barray_list = NULL; mem->total_space_allocated = SIZEOF(my_memory_mgr); /* Declare ourselves open for business */ cinfo->mem = & mem->pub; /* Check for an environment variable JPEGMEM; if found, override the * default max_memory setting from jpeg_mem_init. Note that the * surrounding application may again override this value. * If your system doesn't support getenv(), define NO_GETENV to disable * this feature. */ #ifndef NO_GETENV { char * memenv; if ((memenv = getenv("JPEGMEM")) != NULL) { char ch = 'x'; if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { if (ch == 'm' || ch == 'M') max_to_use *= 1000L; mem->pub.max_memory_to_use = max_to_use * 1000L; } } } #endif } glmark2-2012.08/./src/libjpeg-turbo/jchuff.h0000664000175000017500000000304712013417376017674 0ustar alfalf00000000000000/* * jchuff.h * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains declarations for Huffman entropy encoding routines * that are shared between the sequential encoder (jchuff.c) and the * progressive encoder (jcphuff.c). No other modules need to see these. */ /* The legal range of a DCT coefficient is * -1024 .. +1023 for 8-bit data; * -16384 .. +16383 for 12-bit data. * Hence the magnitude should always fit in 10 or 14 bits respectively. */ #if BITS_IN_JSAMPLE == 8 #define MAX_COEF_BITS 10 #else #define MAX_COEF_BITS 14 #endif /* Derived data constructed for each Huffman table */ typedef struct { unsigned int ehufco[256]; /* code for each symbol */ char ehufsi[256]; /* length of code for each symbol */ /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */ } c_derived_tbl; /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_make_c_derived_tbl jMkCDerived #define jpeg_gen_optimal_table jGenOptTbl #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* Expand a Huffman table definition into the derived format */ EXTERN(void) jpeg_make_c_derived_tbl JPP((j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl ** pdtbl)); /* Generate an optimal table definition given the specified counts */ EXTERN(void) jpeg_gen_optimal_table JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])); glmark2-2012.08/./src/libjpeg-turbo/jdcoefct.c0000664000175000017500000006175612013417376020216 0ustar alfalf00000000000000/* * jdcoefct.c * * Copyright (C) 1994-1997, Thomas G. Lane. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the coefficient buffer controller for decompression. * This controller is the top level of the JPEG decompressor proper. * The coefficient buffer lies between entropy decoding and inverse-DCT steps. * * In buffered-image mode, this controller is the interface between * input-oriented processing and output-oriented processing. * Also, the input side (only) is used when reading a file for transcoding. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* Block smoothing is only applicable for progressive JPEG, so: */ #ifndef D_PROGRESSIVE_SUPPORTED #undef BLOCK_SMOOTHING_SUPPORTED #endif /* Private buffer controller object */ typedef struct { struct jpeg_d_coef_controller pub; /* public fields */ /* These variables keep track of the current location of the input side. */ /* cinfo->input_iMCU_row is also used for this. */ JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ int MCU_vert_offset; /* counts MCU rows within iMCU row */ int MCU_rows_per_iMCU_row; /* number of such rows needed */ /* The output side's location is represented by cinfo->output_iMCU_row. */ /* In single-pass modes, it's sufficient to buffer just one MCU. * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, * and let the entropy decoder write into that workspace each time. * (On 80x86, the workspace is FAR even though it's not really very big; * this is to keep the module interfaces unchanged when a large coefficient * buffer is necessary.) * In multi-pass modes, this array points to the current MCU's blocks * within the virtual arrays; it is used only by the input side. */ JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; /* Temporary workspace for one MCU */ JCOEF * workspace; #ifdef D_MULTISCAN_FILES_SUPPORTED /* In multi-pass modes, we need a virtual block array for each component. */ jvirt_barray_ptr whole_image[MAX_COMPONENTS]; #endif #ifdef BLOCK_SMOOTHING_SUPPORTED /* When doing block smoothing, we latch coefficient Al values here */ int * coef_bits_latch; #define SAVED_COEFS 6 /* we save coef_bits[0..5] */ #endif } my_coef_controller; typedef my_coef_controller * my_coef_ptr; /* Forward declarations */ METHODDEF(int) decompress_onepass JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); #ifdef D_MULTISCAN_FILES_SUPPORTED METHODDEF(int) decompress_data JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); #endif #ifdef BLOCK_SMOOTHING_SUPPORTED LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); METHODDEF(int) decompress_smooth_data JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); #endif LOCAL(void) start_iMCU_row (j_decompress_ptr cinfo) /* Reset within-iMCU-row counters for a new row (input side) */ { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; /* In an interleaved scan, an MCU row is the same as an iMCU row. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. * But at the bottom of the image, process only what's left. */ if (cinfo->comps_in_scan > 1) { coef->MCU_rows_per_iMCU_row = 1; } else { if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; else coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; } coef->MCU_ctr = 0; coef->MCU_vert_offset = 0; } /* * Initialize for an input processing pass. */ METHODDEF(void) start_input_pass (j_decompress_ptr cinfo) { cinfo->input_iMCU_row = 0; start_iMCU_row(cinfo); } /* * Initialize for an output processing pass. */ METHODDEF(void) start_output_pass (j_decompress_ptr cinfo) { #ifdef BLOCK_SMOOTHING_SUPPORTED my_coef_ptr coef = (my_coef_ptr) cinfo->coef; /* If multipass, check to see whether to use block smoothing on this pass */ if (coef->pub.coef_arrays != NULL) { if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) coef->pub.decompress_data = decompress_smooth_data; else coef->pub.decompress_data = decompress_data; } #endif cinfo->output_iMCU_row = 0; } /* * Decompress and return some data in the single-pass case. * Always attempts to emit one fully interleaved MCU row ("iMCU" row). * Input and output must run in lockstep since we have only a one-MCU buffer. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. * * NB: output_buf contains a plane for each component in image, * which we index according to the component's SOF position. */ METHODDEF(int) decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; int blkn, ci, xindex, yindex, yoffset, useful_width; JSAMPARRAY output_ptr; JDIMENSION start_col, output_col; jpeg_component_info *compptr; inverse_DCT_method_ptr inverse_DCT; /* Loop to process as much as one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) { /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ jzero_far((void FAR *) coef->MCU_buffer[0], (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK))); if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->MCU_ctr = MCU_col_num; return JPEG_SUSPENDED; } /* Determine where data should go in output_buf and do the IDCT thing. * We skip dummy blocks at the right and bottom edges (but blkn gets * incremented past them!). Note the inner loop relies on having * allocated the MCU_buffer[] blocks sequentially. */ blkn = 0; /* index of current DCT block within MCU */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Don't bother to IDCT an uninteresting component. */ if (! compptr->component_needed) { blkn += compptr->MCU_blocks; continue; } inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width : compptr->last_col_width; output_ptr = output_buf[compptr->component_index] + yoffset * compptr->_DCT_scaled_size; start_col = MCU_col_num * compptr->MCU_sample_width; for (yindex = 0; yindex < compptr->MCU_height; yindex++) { if (cinfo->input_iMCU_row < last_iMCU_row || yoffset+yindex < compptr->last_row_height) { output_col = start_col; for (xindex = 0; xindex < useful_width; xindex++) { (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) coef->MCU_buffer[blkn+xindex], output_ptr, output_col); output_col += compptr->_DCT_scaled_size; } } blkn += compptr->MCU_width; output_ptr += compptr->_DCT_scaled_size; } } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->MCU_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ cinfo->output_iMCU_row++; if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { start_iMCU_row(cinfo); return JPEG_ROW_COMPLETED; } /* Completed the scan */ (*cinfo->inputctl->finish_input_pass) (cinfo); return JPEG_SCAN_COMPLETED; } /* * Dummy consume-input routine for single-pass operation. */ METHODDEF(int) dummy_consume_data (j_decompress_ptr cinfo) { return JPEG_SUSPENDED; /* Always indicate nothing was done */ } #ifdef D_MULTISCAN_FILES_SUPPORTED /* * Consume input data and store it in the full-image coefficient buffer. * We read as much as one fully interleaved MCU row ("iMCU" row) per call, * ie, v_samp_factor block rows for each component in the scan. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. */ METHODDEF(int) consume_data (j_decompress_ptr cinfo) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ int blkn, ci, xindex, yindex, yoffset; JDIMENSION start_col; JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; JBLOCKROW buffer_ptr; jpeg_component_info *compptr; /* Align the virtual buffers for the components used in this scan. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; buffer[ci] = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], cinfo->input_iMCU_row * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, TRUE); /* Note: entropy decoder expects buffer to be zeroed, * but this is handled automatically by the memory manager * because we requested a pre-zeroed array. */ } /* Loop to process one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) { /* Construct list of pointers to DCT blocks belonging to this MCU */ blkn = 0; /* index of current DCT block within MCU */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; start_col = MCU_col_num * compptr->MCU_width; for (yindex = 0; yindex < compptr->MCU_height; yindex++) { buffer_ptr = buffer[ci][yindex+yoffset] + start_col; for (xindex = 0; xindex < compptr->MCU_width; xindex++) { coef->MCU_buffer[blkn++] = buffer_ptr++; } } } /* Try to fetch the MCU. */ if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->MCU_ctr = MCU_col_num; return JPEG_SUSPENDED; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->MCU_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { start_iMCU_row(cinfo); return JPEG_ROW_COMPLETED; } /* Completed the scan */ (*cinfo->inputctl->finish_input_pass) (cinfo); return JPEG_SCAN_COMPLETED; } /* * Decompress and return some data in the multi-pass case. * Always attempts to emit one fully interleaved MCU row ("iMCU" row). * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. * * NB: output_buf contains a plane for each component in image. */ METHODDEF(int) decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; JDIMENSION block_num; int ci, block_row, block_rows; JBLOCKARRAY buffer; JBLOCKROW buffer_ptr; JSAMPARRAY output_ptr; JDIMENSION output_col; jpeg_component_info *compptr; inverse_DCT_method_ptr inverse_DCT; /* Force some input to be done if we are getting ahead of the input. */ while (cinfo->input_scan_number < cinfo->output_scan_number || (cinfo->input_scan_number == cinfo->output_scan_number && cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) return JPEG_SUSPENDED; } /* OK, output from the virtual arrays. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Don't bother to IDCT an uninteresting component. */ if (! compptr->component_needed) continue; /* Align the virtual buffer for this component. */ buffer = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[ci], cinfo->output_iMCU_row * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, FALSE); /* Count non-dummy DCT block rows in this iMCU row. */ if (cinfo->output_iMCU_row < last_iMCU_row) block_rows = compptr->v_samp_factor; else { /* NB: can't use last_row_height here; it is input-side-dependent! */ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); if (block_rows == 0) block_rows = compptr->v_samp_factor; } inverse_DCT = cinfo->idct->inverse_DCT[ci]; output_ptr = output_buf[ci]; /* Loop over all DCT blocks to be processed. */ for (block_row = 0; block_row < block_rows; block_row++) { buffer_ptr = buffer[block_row]; output_col = 0; for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, output_ptr, output_col); buffer_ptr++; output_col += compptr->_DCT_scaled_size; } output_ptr += compptr->_DCT_scaled_size; } } if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) return JPEG_ROW_COMPLETED; return JPEG_SCAN_COMPLETED; } #endif /* D_MULTISCAN_FILES_SUPPORTED */ #ifdef BLOCK_SMOOTHING_SUPPORTED /* * This code applies interblock smoothing as described by section K.8 * of the JPEG standard: the first 5 AC coefficients are estimated from * the DC values of a DCT block and its 8 neighboring blocks. * We apply smoothing only for progressive JPEG decoding, and only if * the coefficients it can estimate are not yet known to full precision. */ /* Natural-order array positions of the first 5 zigzag-order coefficients */ #define Q01_POS 1 #define Q10_POS 8 #define Q20_POS 16 #define Q11_POS 9 #define Q02_POS 2 /* * Determine whether block smoothing is applicable and safe. * We also latch the current states of the coef_bits[] entries for the * AC coefficients; otherwise, if the input side of the decompressor * advances into a new scan, we might think the coefficients are known * more accurately than they really are. */ LOCAL(boolean) smoothing_ok (j_decompress_ptr cinfo) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; boolean smoothing_useful = FALSE; int ci, coefi; jpeg_component_info *compptr; JQUANT_TBL * qtable; int * coef_bits; int * coef_bits_latch; if (! cinfo->progressive_mode || cinfo->coef_bits == NULL) return FALSE; /* Allocate latch area if not already done */ if (coef->coef_bits_latch == NULL) coef->coef_bits_latch = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->num_components * (SAVED_COEFS * SIZEOF(int))); coef_bits_latch = coef->coef_bits_latch; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* All components' quantization values must already be latched. */ if ((qtable = compptr->quant_table) == NULL) return FALSE; /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ if (qtable->quantval[0] == 0 || qtable->quantval[Q01_POS] == 0 || qtable->quantval[Q10_POS] == 0 || qtable->quantval[Q20_POS] == 0 || qtable->quantval[Q11_POS] == 0 || qtable->quantval[Q02_POS] == 0) return FALSE; /* DC values must be at least partly known for all components. */ coef_bits = cinfo->coef_bits[ci]; if (coef_bits[0] < 0) return FALSE; /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ for (coefi = 1; coefi <= 5; coefi++) { coef_bits_latch[coefi] = coef_bits[coefi]; if (coef_bits[coefi] != 0) smoothing_useful = TRUE; } coef_bits_latch += SAVED_COEFS; } return smoothing_useful; } /* * Variant of decompress_data for use when doing block smoothing. */ METHODDEF(int) decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; JDIMENSION block_num, last_block_column; int ci, block_row, block_rows, access_rows; JBLOCKARRAY buffer; JBLOCKROW buffer_ptr, prev_block_row, next_block_row; JSAMPARRAY output_ptr; JDIMENSION output_col; jpeg_component_info *compptr; inverse_DCT_method_ptr inverse_DCT; boolean first_row, last_row; JCOEF * workspace; int *coef_bits; JQUANT_TBL *quanttbl; INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; int Al, pred; /* Keep a local variable to avoid looking it up more than once */ workspace = coef->workspace; /* Force some input to be done if we are getting ahead of the input. */ while (cinfo->input_scan_number <= cinfo->output_scan_number && ! cinfo->inputctl->eoi_reached) { if (cinfo->input_scan_number == cinfo->output_scan_number) { /* If input is working on current scan, we ordinarily want it to * have completed the current row. But if input scan is DC, * we want it to keep one row ahead so that next block row's DC * values are up to date. */ JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) break; } if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) return JPEG_SUSPENDED; } /* OK, output from the virtual arrays. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Don't bother to IDCT an uninteresting component. */ if (! compptr->component_needed) continue; /* Count non-dummy DCT block rows in this iMCU row. */ if (cinfo->output_iMCU_row < last_iMCU_row) { block_rows = compptr->v_samp_factor; access_rows = block_rows * 2; /* this and next iMCU row */ last_row = FALSE; } else { /* NB: can't use last_row_height here; it is input-side-dependent! */ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); if (block_rows == 0) block_rows = compptr->v_samp_factor; access_rows = block_rows; /* this iMCU row only */ last_row = TRUE; } /* Align the virtual buffer for this component. */ if (cinfo->output_iMCU_row > 0) { access_rows += compptr->v_samp_factor; /* prior iMCU row too */ buffer = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[ci], (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, (JDIMENSION) access_rows, FALSE); buffer += compptr->v_samp_factor; /* point to current iMCU row */ first_row = FALSE; } else { buffer = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[ci], (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); first_row = TRUE; } /* Fetch component-dependent info */ coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); quanttbl = compptr->quant_table; Q00 = quanttbl->quantval[0]; Q01 = quanttbl->quantval[Q01_POS]; Q10 = quanttbl->quantval[Q10_POS]; Q20 = quanttbl->quantval[Q20_POS]; Q11 = quanttbl->quantval[Q11_POS]; Q02 = quanttbl->quantval[Q02_POS]; inverse_DCT = cinfo->idct->inverse_DCT[ci]; output_ptr = output_buf[ci]; /* Loop over all DCT blocks to be processed. */ for (block_row = 0; block_row < block_rows; block_row++) { buffer_ptr = buffer[block_row]; if (first_row && block_row == 0) prev_block_row = buffer_ptr; else prev_block_row = buffer[block_row-1]; if (last_row && block_row == block_rows-1) next_block_row = buffer_ptr; else next_block_row = buffer[block_row+1]; /* We fetch the surrounding DC values using a sliding-register approach. * Initialize all nine here so as to do the right thing on narrow pics. */ DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; DC7 = DC8 = DC9 = (int) next_block_row[0][0]; output_col = 0; last_block_column = compptr->width_in_blocks - 1; for (block_num = 0; block_num <= last_block_column; block_num++) { /* Fetch current DCT block into workspace so we can modify it. */ jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); /* Update DC values */ if (block_num < last_block_column) { DC3 = (int) prev_block_row[1][0]; DC6 = (int) buffer_ptr[1][0]; DC9 = (int) next_block_row[1][0]; } /* Compute coefficient estimates per K.8. * An estimate is applied only if coefficient is still zero, * and is not known to be fully accurate. */ /* AC01 */ if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { num = 36 * Q00 * (DC4 - DC6); if (num >= 0) { pred = (int) (((Q01<<7) + num) / (Q01<<8)); if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { pred = (int) (((Q10<<7) + num) / (Q10<<8)); if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { pred = (int) (((Q20<<7) + num) / (Q20<<8)); if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { pred = (int) (((Q11<<7) + num) / (Q11<<8)); if (Al > 0 && pred >= (1< 0 && pred >= (1<= 0) { pred = (int) (((Q02<<7) + num) / (Q02<<8)); if (Al > 0 && pred >= (1< 0 && pred >= (1<_DCT_scaled_size; } output_ptr += compptr->_DCT_scaled_size; } } if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows) return JPEG_ROW_COMPLETED; return JPEG_SCAN_COMPLETED; } #endif /* BLOCK_SMOOTHING_SUPPORTED */ /* * Initialize coefficient buffer controller. */ GLOBAL(void) jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) { my_coef_ptr coef; coef = (my_coef_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller)); cinfo->coef = (struct jpeg_d_coef_controller *) coef; coef->pub.start_input_pass = start_input_pass; coef->pub.start_output_pass = start_output_pass; #ifdef BLOCK_SMOOTHING_SUPPORTED coef->coef_bits_latch = NULL; #endif /* Create the coefficient buffer. */ if (need_full_buffer) { #ifdef D_MULTISCAN_FILES_SUPPORTED /* Allocate a full-image virtual array for each component, */ /* padded to a multiple of samp_factor DCT blocks in each direction. */ /* Note we ask for a pre-zeroed array. */ int ci, access_rows; jpeg_component_info *compptr; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { access_rows = compptr->v_samp_factor; #ifdef BLOCK_SMOOTHING_SUPPORTED /* If block smoothing could be used, need a bigger window */ if (cinfo->progressive_mode) access_rows *= 3; #endif coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, (JDIMENSION) jround_up((long) compptr->width_in_blocks, (long) compptr->h_samp_factor), (JDIMENSION) jround_up((long) compptr->height_in_blocks, (long) compptr->v_samp_factor), (JDIMENSION) access_rows); } coef->pub.consume_data = consume_data; coef->pub.decompress_data = decompress_data; coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { /* We only need a single-MCU buffer. */ JBLOCKROW buffer; int i; buffer = (JBLOCKROW) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) { coef->MCU_buffer[i] = buffer + i; } coef->pub.consume_data = dummy_consume_data; coef->pub.decompress_data = decompress_onepass; coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ } /* Allocate the workspace buffer */ coef->workspace = (JCOEF *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(JCOEF) * DCTSIZE2); } glmark2-2012.08/./src/libjpeg-turbo/jcinit.c0000664000175000017500000000457212013417376017706 0ustar alfalf00000000000000/* * jcinit.c * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains initialization logic for the JPEG compressor. * This routine is in charge of selecting the modules to be executed and * making an initialization call to each one. * * Logically, this code belongs in jcmaster.c. It's split out because * linking this routine implies linking the entire compression library. * For a transcoding-only application, we want to be able to use jcmaster.c * without linking in the whole library. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Master selection of compression modules. * This is done once at the start of processing an image. We determine * which modules will be used and give them appropriate initialization calls. */ GLOBAL(void) jinit_compress_master (j_compress_ptr cinfo) { /* Initialize master control (includes parameter checking/processing) */ jinit_c_master_control(cinfo, FALSE /* full compression */); /* Preprocessing */ if (! cinfo->raw_data_in) { jinit_color_converter(cinfo); jinit_downsampler(cinfo); jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */); } /* Forward DCT */ jinit_forward_dct(cinfo); /* Entropy encoding: either Huffman or arithmetic coding. */ if (cinfo->arith_code) { #ifdef C_ARITH_CODING_SUPPORTED jinit_arith_encoder(cinfo); #else ERREXIT(cinfo, JERR_ARITH_NOTIMPL); #endif } else { if (cinfo->progressive_mode) { #ifdef C_PROGRESSIVE_SUPPORTED jinit_phuff_encoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else jinit_huff_encoder(cinfo); } /* Need a full-image coefficient buffer in any multi-pass mode. */ jinit_c_coef_controller(cinfo, (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding)); jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */); jinit_marker_writer(cinfo); /* We can now tell the memory manager to allocate virtual arrays. */ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); /* Write the datastream header (SOI) immediately. * Frame and scan headers are postponed till later. * This lets application insert special markers after the SOI. */ (*cinfo->marker->write_file_header) (cinfo); } glmark2-2012.08/./src/libjpeg-turbo/jcphuff.c0000664000175000017500000006101512013417376020046 0ustar alfalf00000000000000/* * jcphuff.c * * Copyright (C) 1995-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains Huffman entropy encoding routines for progressive JPEG. * * We do not support output suspension in this module, since the library * currently does not allow multiple-scan files to be written with output * suspension. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jchuff.h" /* Declarations shared with jchuff.c */ #ifdef C_PROGRESSIVE_SUPPORTED /* Expanded entropy encoder object for progressive Huffman encoding. */ typedef struct { struct jpeg_entropy_encoder pub; /* public fields */ /* Mode flag: TRUE for optimization, FALSE for actual data output */ boolean gather_statistics; /* Bit-level coding status. * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. */ JOCTET * next_output_byte; /* => next byte to write in buffer */ size_t free_in_buffer; /* # of byte spaces remaining in buffer */ INT32 put_buffer; /* current bit-accumulation buffer */ int put_bits; /* # of bits now in it */ j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ /* Coding status for DC components */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ /* Coding status for AC components */ int ac_tbl_no; /* the table number of the single component */ unsigned int EOBRUN; /* run length of EOBs */ unsigned int BE; /* # of buffered correction bits before MCU */ char * bit_buffer; /* buffer for correction bits (1 per char) */ /* packing correction bits tightly would save some space but cost time... */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ int next_restart_num; /* next restart number to write (0-7) */ /* Pointers to derived tables (these workspaces have image lifespan). * Since any one scan codes only DC or only AC, we only need one set * of tables, not one for DC and one for AC. */ c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; /* Statistics tables for optimization; again, one set is enough */ long * count_ptrs[NUM_HUFF_TBLS]; } phuff_entropy_encoder; typedef phuff_entropy_encoder * phuff_entropy_ptr; /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit * buffer can hold. Larger sizes may slightly improve compression, but * 1000 is already well into the realm of overkill. * The minimum safe size is 64 bits. */ #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. * We assume that int right shift is unsigned if INT32 right shift is, * which should be safe. */ #ifdef RIGHT_SHIFT_IS_UNSIGNED #define ISHIFT_TEMPS int ishift_temp; #define IRIGHT_SHIFT(x,shft) \ ((ishift_temp = (x)) < 0 ? \ (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ (ishift_temp >> (shft))) #else #define ISHIFT_TEMPS #define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) #endif /* Forward declarations */ METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); /* * Initialize for a Huffman-compressed scan using progressive JPEG. */ METHODDEF(void) start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; boolean is_DC_band; int ci, tbl; jpeg_component_info * compptr; entropy->cinfo = cinfo; entropy->gather_statistics = gather_statistics; is_DC_band = (cinfo->Ss == 0); /* We assume jcmaster.c already validated the scan parameters. */ /* Select execution routines */ if (cinfo->Ah == 0) { if (is_DC_band) entropy->pub.encode_mcu = encode_mcu_DC_first; else entropy->pub.encode_mcu = encode_mcu_AC_first; } else { if (is_DC_band) entropy->pub.encode_mcu = encode_mcu_DC_refine; else { entropy->pub.encode_mcu = encode_mcu_AC_refine; /* AC refinement needs a correction bit buffer */ if (entropy->bit_buffer == NULL) entropy->bit_buffer = (char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, MAX_CORR_BITS * SIZEOF(char)); } } if (gather_statistics) entropy->pub.finish_pass = finish_pass_gather_phuff; else entropy->pub.finish_pass = finish_pass_phuff; /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 * for AC coefficients. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Initialize DC predictions to 0 */ entropy->last_dc_val[ci] = 0; /* Get table index */ if (is_DC_band) { if (cinfo->Ah != 0) /* DC refinement needs no table */ continue; tbl = compptr->dc_tbl_no; } else { entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; } if (gather_statistics) { /* Check for invalid table index */ /* (make_c_derived_tbl does this in the other path) */ if (tbl < 0 || tbl >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); /* Allocate and zero the statistics tables */ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ if (entropy->count_ptrs[tbl] == NULL) entropy->count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long)); MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); } else { /* Compute derived values for Huffman table */ /* We may do this more than once for a table, but it's not expensive */ jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, & entropy->derived_tbls[tbl]); } } /* Initialize AC stuff */ entropy->EOBRUN = 0; entropy->BE = 0; /* Initialize bit buffer to empty */ entropy->put_buffer = 0; entropy->put_bits = 0; /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0; } /* Outputting bytes to the file. * NB: these must be called only when actually outputting, * that is, entropy->gather_statistics == FALSE. */ /* Emit a byte */ #define emit_byte(entropy,val) \ { *(entropy)->next_output_byte++ = (JOCTET) (val); \ if (--(entropy)->free_in_buffer == 0) \ dump_buffer(entropy); } LOCAL(void) dump_buffer (phuff_entropy_ptr entropy) /* Empty the output buffer; we do not support suspension in this module. */ { struct jpeg_destination_mgr * dest = entropy->cinfo->dest; if (! (*dest->empty_output_buffer) (entropy->cinfo)) ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); /* After a successful buffer dump, must reset buffer pointers */ entropy->next_output_byte = dest->next_output_byte; entropy->free_in_buffer = dest->free_in_buffer; } /* Outputting bits to the file */ /* Only the right 24 bits of put_buffer are used; the valid bits are * left-justified in this part. At most 16 bits can be passed to emit_bits * in one call, and we never retain more than 7 bits in put_buffer * between calls, so 24 bits are sufficient. */ LOCAL(void) emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) /* Emit some bits, unless we are in gather mode */ { /* This routine is heavily used, so it's worth coding tightly. */ register INT32 put_buffer = (INT32) code; register int put_bits = entropy->put_bits; /* if size is 0, caller used an invalid Huffman table entry */ if (size == 0) ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); if (entropy->gather_statistics) return; /* do nothing if we're only getting stats */ put_buffer &= (((INT32) 1)<put_buffer; /* and merge with old buffer contents */ while (put_bits >= 8) { int c = (int) ((put_buffer >> 16) & 0xFF); emit_byte(entropy, c); if (c == 0xFF) { /* need to stuff a zero byte? */ emit_byte(entropy, 0); } put_buffer <<= 8; put_bits -= 8; } entropy->put_buffer = put_buffer; /* update variables */ entropy->put_bits = put_bits; } LOCAL(void) flush_bits (phuff_entropy_ptr entropy) { emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ entropy->put_buffer = 0; /* and reset bit-buffer to empty */ entropy->put_bits = 0; } /* * Emit (or just count) a Huffman symbol. */ LOCAL(void) emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) { if (entropy->gather_statistics) entropy->count_ptrs[tbl_no][symbol]++; else { c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); } } /* * Emit bits from a correction bit buffer. */ LOCAL(void) emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, unsigned int nbits) { if (entropy->gather_statistics) return; /* no real work */ while (nbits > 0) { emit_bits(entropy, (unsigned int) (*bufstart), 1); bufstart++; nbits--; } } /* * Emit any pending EOBRUN symbol. */ LOCAL(void) emit_eobrun (phuff_entropy_ptr entropy) { register int temp, nbits; if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ temp = entropy->EOBRUN; nbits = 0; while ((temp >>= 1)) nbits++; /* safety check: shouldn't happen given limited correction-bit buffer */ if (nbits > 14) ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); if (nbits) emit_bits(entropy, entropy->EOBRUN, nbits); entropy->EOBRUN = 0; /* Emit any buffered correction bits */ emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); entropy->BE = 0; } } /* * Emit a restart marker & resynchronize predictions. */ LOCAL(void) emit_restart (phuff_entropy_ptr entropy, int restart_num) { int ci; emit_eobrun(entropy); if (! entropy->gather_statistics) { flush_bits(entropy); emit_byte(entropy, 0xFF); emit_byte(entropy, JPEG_RST0 + restart_num); } if (entropy->cinfo->Ss == 0) { /* Re-initialize DC predictions to 0 */ for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) entropy->last_dc_val[ci] = 0; } else { /* Re-initialize all AC-related fields to 0 */ entropy->EOBRUN = 0; entropy->BE = 0; } } /* * MCU encoding for DC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; register int temp, temp2; register int nbits; int blkn, ci; int Al = cinfo->Al; JBLOCKROW block; jpeg_component_info * compptr; ISHIFT_TEMPS entropy->next_output_byte = cinfo->dest->next_output_byte; entropy->free_in_buffer = cinfo->dest->free_in_buffer; /* Emit restart marker if needed */ if (cinfo->restart_interval) if (entropy->restarts_to_go == 0) emit_restart(entropy, entropy->next_restart_num); /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; /* Compute the DC value after the required point transform by Al. * This is simply an arithmetic right shift. */ temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); /* DC differences are figured on the point-transformed values. */ temp = temp2 - entropy->last_dc_val[ci]; entropy->last_dc_val[ci] = temp2; /* Encode the DC coefficient difference per section G.1.2.1 */ temp2 = temp; if (temp < 0) { temp = -temp; /* temp is abs value of input */ /* For a negative input, want temp2 = bitwise complement of abs(input) */ /* This code assumes we are on a two's complement machine */ temp2--; } /* Find the number of bits needed for the magnitude of the coefficient */ nbits = 0; while (temp) { nbits++; temp >>= 1; } /* Check for out-of-range coefficient values. * Since we're encoding a difference, the range limit is twice as much. */ if (nbits > MAX_COEF_BITS+1) ERREXIT(cinfo, JERR_BAD_DCT_COEF); /* Count/emit the Huffman-coded symbol for the number of bits */ emit_symbol(entropy, compptr->dc_tbl_no, nbits); /* Emit that number of bits of the value, if positive, */ /* or the complement of its magnitude, if negative. */ if (nbits) /* emit_bits rejects calls with size 0 */ emit_bits(entropy, (unsigned int) temp2, nbits); } cinfo->dest->next_output_byte = entropy->next_output_byte; cinfo->dest->free_in_buffer = entropy->free_in_buffer; /* Update restart-interval state too */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } return TRUE; } /* * MCU encoding for AC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; register int temp, temp2; register int nbits; register int r, k; int Se = cinfo->Se; int Al = cinfo->Al; JBLOCKROW block; entropy->next_output_byte = cinfo->dest->next_output_byte; entropy->free_in_buffer = cinfo->dest->free_in_buffer; /* Emit restart marker if needed */ if (cinfo->restart_interval) if (entropy->restarts_to_go == 0) emit_restart(entropy, entropy->next_restart_num); /* Encode the MCU data block */ block = MCU_data[0]; /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ r = 0; /* r = run length of zeros */ for (k = cinfo->Ss; k <= Se; k++) { if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { r++; continue; } /* We must apply the point transform by Al. For AC coefficients this * is an integer division with rounding towards 0. To do this portably * in C, we shift after obtaining the absolute value; so the code is * interwoven with finding the abs value (temp) and output bits (temp2). */ if (temp < 0) { temp = -temp; /* temp is abs value of input */ temp >>= Al; /* apply the point transform */ /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ temp2 = ~temp; } else { temp >>= Al; /* apply the point transform */ temp2 = temp; } /* Watch out for case that nonzero coef is zero after point transform */ if (temp == 0) { r++; continue; } /* Emit any pending EOBRUN */ if (entropy->EOBRUN > 0) emit_eobrun(entropy); /* if run length > 15, must emit special run-length-16 codes (0xF0) */ while (r > 15) { emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); r -= 16; } /* Find the number of bits needed for the magnitude of the coefficient */ nbits = 1; /* there must be at least one 1 bit */ while ((temp >>= 1)) nbits++; /* Check for out-of-range coefficient values */ if (nbits > MAX_COEF_BITS) ERREXIT(cinfo, JERR_BAD_DCT_COEF); /* Count/emit Huffman symbol for run length / number of bits */ emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); /* Emit that number of bits of the value, if positive, */ /* or the complement of its magnitude, if negative. */ emit_bits(entropy, (unsigned int) temp2, nbits); r = 0; /* reset zero run length */ } if (r > 0) { /* If there are trailing zeroes, */ entropy->EOBRUN++; /* count an EOB */ if (entropy->EOBRUN == 0x7FFF) emit_eobrun(entropy); /* force it out to avoid overflow */ } cinfo->dest->next_output_byte = entropy->next_output_byte; cinfo->dest->free_in_buffer = entropy->free_in_buffer; /* Update restart-interval state too */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } return TRUE; } /* * MCU encoding for DC successive approximation refinement scan. * Note: we assume such scans can be multi-component, although the spec * is not very clear on the point. */ METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; register int temp; int blkn; int Al = cinfo->Al; JBLOCKROW block; entropy->next_output_byte = cinfo->dest->next_output_byte; entropy->free_in_buffer = cinfo->dest->free_in_buffer; /* Emit restart marker if needed */ if (cinfo->restart_interval) if (entropy->restarts_to_go == 0) emit_restart(entropy, entropy->next_restart_num); /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; /* We simply emit the Al'th bit of the DC coefficient value. */ temp = (*block)[0]; emit_bits(entropy, (unsigned int) (temp >> Al), 1); } cinfo->dest->next_output_byte = entropy->next_output_byte; cinfo->dest->free_in_buffer = entropy->free_in_buffer; /* Update restart-interval state too */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } return TRUE; } /* * MCU encoding for AC successive approximation refinement scan. */ METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; register int temp; register int r, k; int EOB; char *BR_buffer; unsigned int BR; int Se = cinfo->Se; int Al = cinfo->Al; JBLOCKROW block; int absvalues[DCTSIZE2]; entropy->next_output_byte = cinfo->dest->next_output_byte; entropy->free_in_buffer = cinfo->dest->free_in_buffer; /* Emit restart marker if needed */ if (cinfo->restart_interval) if (entropy->restarts_to_go == 0) emit_restart(entropy, entropy->next_restart_num); /* Encode the MCU data block */ block = MCU_data[0]; /* It is convenient to make a pre-pass to determine the transformed * coefficients' absolute values and the EOB position. */ EOB = 0; for (k = cinfo->Ss; k <= Se; k++) { temp = (*block)[jpeg_natural_order[k]]; /* We must apply the point transform by Al. For AC coefficients this * is an integer division with rounding towards 0. To do this portably * in C, we shift after obtaining the absolute value. */ if (temp < 0) temp = -temp; /* temp is abs value of input */ temp >>= Al; /* apply the point transform */ absvalues[k] = temp; /* save abs value for main pass */ if (temp == 1) EOB = k; /* EOB = index of last newly-nonzero coef */ } /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ r = 0; /* r = run length of zeros */ BR = 0; /* BR = count of buffered bits added now */ BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ for (k = cinfo->Ss; k <= Se; k++) { if ((temp = absvalues[k]) == 0) { r++; continue; } /* Emit any required ZRLs, but not if they can be folded into EOB */ while (r > 15 && k <= EOB) { /* emit any pending EOBRUN and the BE correction bits */ emit_eobrun(entropy); /* Emit ZRL */ emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); r -= 16; /* Emit buffered correction bits that must be associated with ZRL */ emit_buffered_bits(entropy, BR_buffer, BR); BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ BR = 0; } /* If the coef was previously nonzero, it only needs a correction bit. * NOTE: a straight translation of the spec's figure G.7 would suggest * that we also need to test r > 15. But if r > 15, we can only get here * if k > EOB, which implies that this coefficient is not 1. */ if (temp > 1) { /* The correction bit is the next bit of the absolute value. */ BR_buffer[BR++] = (char) (temp & 1); continue; } /* Emit any pending EOBRUN and the BE correction bits */ emit_eobrun(entropy); /* Count/emit Huffman symbol for run length / number of bits */ emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); /* Emit output bit for newly-nonzero coef */ temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; emit_bits(entropy, (unsigned int) temp, 1); /* Emit buffered correction bits that must be associated with this code */ emit_buffered_bits(entropy, BR_buffer, BR); BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ BR = 0; r = 0; /* reset zero run length */ } if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ entropy->EOBRUN++; /* count an EOB */ entropy->BE += BR; /* concat my correction bits to older ones */ /* We force out the EOB if we risk either: * 1. overflow of the EOB counter; * 2. overflow of the correction bit buffer during the next MCU. */ if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) emit_eobrun(entropy); } cinfo->dest->next_output_byte = entropy->next_output_byte; cinfo->dest->free_in_buffer = entropy->free_in_buffer; /* Update restart-interval state too */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } return TRUE; } /* * Finish up at the end of a Huffman-compressed progressive scan. */ METHODDEF(void) finish_pass_phuff (j_compress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; entropy->next_output_byte = cinfo->dest->next_output_byte; entropy->free_in_buffer = cinfo->dest->free_in_buffer; /* Flush out any buffered data */ emit_eobrun(entropy); flush_bits(entropy); cinfo->dest->next_output_byte = entropy->next_output_byte; cinfo->dest->free_in_buffer = entropy->free_in_buffer; } /* * Finish up a statistics-gathering pass and create the new Huffman tables. */ METHODDEF(void) finish_pass_gather_phuff (j_compress_ptr cinfo) { phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; boolean is_DC_band; int ci, tbl; jpeg_component_info * compptr; JHUFF_TBL **htblptr; boolean did[NUM_HUFF_TBLS]; /* Flush out buffered data (all we care about is counting the EOB symbol) */ emit_eobrun(entropy); is_DC_band = (cinfo->Ss == 0); /* It's important not to apply jpeg_gen_optimal_table more than once * per table, because it clobbers the input frequency counts! */ MEMZERO(did, SIZEOF(did)); for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; if (is_DC_band) { if (cinfo->Ah != 0) /* DC refinement needs no table */ continue; tbl = compptr->dc_tbl_no; } else { tbl = compptr->ac_tbl_no; } if (! did[tbl]) { if (is_DC_band) htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; else htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); did[tbl] = TRUE; } } } /* * Module initialization routine for progressive Huffman entropy encoding. */ GLOBAL(void) jinit_phuff_encoder (j_compress_ptr cinfo) { phuff_entropy_ptr entropy; int i; entropy = (phuff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(phuff_entropy_encoder)); cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; entropy->pub.start_pass = start_pass_phuff; /* Mark tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->derived_tbls[i] = NULL; entropy->count_ptrs[i] = NULL; } entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ } #endif /* C_PROGRESSIVE_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jchuff.c0000664000175000017500000007440112013417376017671 0ustar alfalf00000000000000/* * jchuff.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains Huffman entropy encoding routines. * * Much of the complexity here has to do with supporting output suspension. * If the data destination module demands suspension, we want to be able to * back up to the start of the current MCU. To do this, we copy state * variables into local working storage, and update them back to the * permanent JPEG objects only upon successful completion of an MCU. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jchuff.h" /* Declarations shared with jcphuff.c */ #include static unsigned char jpeg_nbits_table[65536]; static int jpeg_nbits_table_init = 0; #ifndef min #define min(a,b) ((a)<(b)?(a):(b)) #endif /* Expanded entropy encoder object for Huffman encoding. * * The savable_state subrecord contains fields that change within an MCU, * but must not be updated permanently until we complete the MCU. */ typedef struct { size_t put_buffer; /* current bit-accumulation buffer */ int put_bits; /* # of bits now in it */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ } savable_state; /* This macro is to work around compilers with missing or broken * structure assignment. You'll need to fix this code if you have * such a compiler and you change MAX_COMPS_IN_SCAN. */ #ifndef NO_STRUCT_ASSIGN #define ASSIGN_STATE(dest,src) ((dest) = (src)) #else #if MAX_COMPS_IN_SCAN == 4 #define ASSIGN_STATE(dest,src) \ ((dest).put_buffer = (src).put_buffer, \ (dest).put_bits = (src).put_bits, \ (dest).last_dc_val[0] = (src).last_dc_val[0], \ (dest).last_dc_val[1] = (src).last_dc_val[1], \ (dest).last_dc_val[2] = (src).last_dc_val[2], \ (dest).last_dc_val[3] = (src).last_dc_val[3]) #endif #endif typedef struct { struct jpeg_entropy_encoder pub; /* public fields */ savable_state saved; /* Bit buffer & DC state at start of MCU */ /* These fields are NOT loaded into local working state. */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ int next_restart_num; /* next restart number to write (0-7) */ /* Pointers to derived tables (these workspaces have image lifespan) */ c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */ long * dc_count_ptrs[NUM_HUFF_TBLS]; long * ac_count_ptrs[NUM_HUFF_TBLS]; #endif } huff_entropy_encoder; typedef huff_entropy_encoder * huff_entropy_ptr; /* Working state while writing an MCU. * This struct contains all the fields that are needed by subroutines. */ typedef struct { JOCTET * next_output_byte; /* => next byte to write in buffer */ size_t free_in_buffer; /* # of byte spaces remaining in buffer */ savable_state cur; /* Current bit buffer & DC state */ j_compress_ptr cinfo; /* dump_buffer needs access to this */ } working_state; /* Forward declarations */ METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo)); #ifdef ENTROPY_OPT_SUPPORTED METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data)); METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo)); #endif /* * Initialize for a Huffman-compressed scan. * If gather_statistics is TRUE, we do not output anything during the scan, * just count the Huffman symbols used and generate Huffman code tables. */ METHODDEF(void) start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci, dctbl, actbl; jpeg_component_info * compptr; if (gather_statistics) { #ifdef ENTROPY_OPT_SUPPORTED entropy->pub.encode_mcu = encode_mcu_gather; entropy->pub.finish_pass = finish_pass_gather; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { entropy->pub.encode_mcu = encode_mcu_huff; entropy->pub.finish_pass = finish_pass_huff; } for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; dctbl = compptr->dc_tbl_no; actbl = compptr->ac_tbl_no; if (gather_statistics) { #ifdef ENTROPY_OPT_SUPPORTED /* Check for invalid table indexes */ /* (make_c_derived_tbl does this in the other path) */ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl); if (actbl < 0 || actbl >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl); /* Allocate and zero the statistics tables */ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ if (entropy->dc_count_ptrs[dctbl] == NULL) entropy->dc_count_ptrs[dctbl] = (long *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long)); MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long)); if (entropy->ac_count_ptrs[actbl] == NULL) entropy->ac_count_ptrs[actbl] = (long *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long)); MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long)); #endif } else { /* Compute derived values for Huffman tables */ /* We may do this more than once for a table, but it's not expensive */ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, & entropy->dc_derived_tbls[dctbl]); jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, & entropy->ac_derived_tbls[actbl]); } /* Initialize DC predictions to 0 */ entropy->saved.last_dc_val[ci] = 0; } /* Initialize bit buffer to empty */ entropy->saved.put_buffer = 0; entropy->saved.put_bits = 0; /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0; } /* * Compute the derived values for a Huffman table. * This routine also performs some validation checks on the table. * * Note this is also used by jcphuff.c. */ GLOBAL(void) jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl ** pdtbl) { JHUFF_TBL *htbl; c_derived_tbl *dtbl; int p, i, l, lastp, si, maxsymbol; char huffsize[257]; unsigned int huffcode[257]; unsigned int code; /* Note that huffsize[] and huffcode[] are filled in code-length order, * paralleling the order of the symbols themselves in htbl->huffval[]. */ /* Find the input Huffman table */ if (tblno < 0 || tblno >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; if (htbl == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); /* Allocate a workspace if we haven't already done so. */ if (*pdtbl == NULL) *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(c_derived_tbl)); dtbl = *pdtbl; /* Figure C.1: make table of Huffman code length for each symbol */ p = 0; for (l = 1; l <= 16; l++) { i = (int) htbl->bits[l]; if (i < 0 || p + i > 256) /* protect against table overrun */ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); while (i--) huffsize[p++] = (char) l; } huffsize[p] = 0; lastp = p; /* Figure C.2: generate the codes themselves */ /* We also validate that the counts represent a legal Huffman code tree. */ code = 0; si = huffsize[0]; p = 0; while (huffsize[p]) { while (((int) huffsize[p]) == si) { huffcode[p++] = code; code++; } /* code is now 1 more than the last code used for codelength si; but * it must still fit in si bits, since no code is allowed to be all ones. */ if (((INT32) code) >= (((INT32) 1) << si)) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); code <<= 1; si++; } /* Figure C.3: generate encoding tables */ /* These are code and size indexed by symbol value */ /* Set all codeless symbols to have code length 0; * this lets us detect duplicate VAL entries here, and later * allows emit_bits to detect any attempt to emit such symbols. */ MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); /* This is also a convenient place to check for out-of-range * and duplicated VAL entries. We allow 0..255 for AC symbols * but only 0..15 for DC. (We could constrain them further * based on data depth and mode, but this seems enough.) */ maxsymbol = isDC ? 15 : 255; for (p = 0; p < lastp; p++) { i = htbl->huffval[p]; if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); dtbl->ehufco[i] = huffcode[p]; dtbl->ehufsi[i] = huffsize[p]; } if(!jpeg_nbits_table_init) { for(i = 0; i < 65536; i++) { int nbits = 0, temp = i; while (temp) {temp >>= 1; nbits++;} jpeg_nbits_table[i] = nbits; } jpeg_nbits_table_init = 1; } } /* Outputting bytes to the file */ /* Emit a byte, taking 'action' if must suspend. */ #define emit_byte(state,val,action) \ { *(state)->next_output_byte++ = (JOCTET) (val); \ if (--(state)->free_in_buffer == 0) \ if (! dump_buffer(state)) \ { action; } } LOCAL(boolean) dump_buffer (working_state * state) /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */ { struct jpeg_destination_mgr * dest = state->cinfo->dest; dest->free_in_buffer = state->free_in_buffer; if (! (*dest->empty_output_buffer) (state->cinfo)) return FALSE; /* After a successful buffer dump, must reset buffer pointers */ state->next_output_byte = dest->next_output_byte; state->free_in_buffer = dest->free_in_buffer; return TRUE; } /* Outputting bits to the file */ /* These macros perform the same task as the emit_bits() function in the * original libjpeg code. In addition to reducing overhead by explicitly * inlining the code, additional performance is achieved by taking into * account the size of the bit buffer and waiting until it is almost full * before emptying it. This mostly benefits 64-bit platforms, since 6 * bytes can be stored in a 64-bit bit buffer before it has to be emptied. */ #define EMIT_BYTE() { \ JOCTET c; \ put_bits -= 8; \ c = (JOCTET)GETJOCTET(put_buffer >> put_bits); \ *buffer++ = c; \ if (c == 0xFF) /* need to stuff a zero byte? */ \ *buffer++ = 0; \ } #define PUT_BITS(code, size) { \ put_bits += size; \ put_buffer = (put_buffer << size) | code; \ } #define CHECKBUF15() { \ if (put_bits > 15) { \ EMIT_BYTE() \ EMIT_BYTE() \ } \ } #define CHECKBUF31() { \ if (put_bits > 31) { \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ } \ } #define CHECKBUF47() { \ if (put_bits > 47) { \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ EMIT_BYTE() \ } \ } #if __WORDSIZE==64 || defined(_WIN64) #define EMIT_BITS(code, size) { \ CHECKBUF47() \ PUT_BITS(code, size) \ } #define EMIT_CODE(code, size) { \ temp2 &= (((INT32) 1)<free_in_buffer < BUFSIZE) { \ localbuf = 1; \ buffer = _buffer; \ } \ else buffer = state->next_output_byte; \ } #define STORE_BUFFER() { \ if (localbuf) { \ bytes = buffer - _buffer; \ buffer = _buffer; \ while (bytes > 0) { \ bytestocopy = min(bytes, state->free_in_buffer); \ MEMCOPY(state->next_output_byte, buffer, bytestocopy); \ state->next_output_byte += bytestocopy; \ buffer += bytestocopy; \ state->free_in_buffer -= bytestocopy; \ if (state->free_in_buffer == 0) \ if (! dump_buffer(state)) return FALSE; \ bytes -= bytestocopy; \ } \ } \ else { \ state->free_in_buffer -= (buffer - state->next_output_byte); \ state->next_output_byte = buffer; \ } \ } LOCAL(boolean) flush_bits (working_state * state) { JOCTET _buffer[BUFSIZE], *buffer; size_t put_buffer; int put_bits; size_t bytes, bytestocopy; int localbuf = 0; put_buffer = state->cur.put_buffer; put_bits = state->cur.put_bits; LOAD_BUFFER() /* fill any partial byte with ones */ PUT_BITS(0x7F, 7) while (put_bits >= 8) EMIT_BYTE() state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ state->cur.put_bits = 0; STORE_BUFFER() return TRUE; } /* Encode a single block's worth of coefficients */ LOCAL(boolean) encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val, c_derived_tbl *dctbl, c_derived_tbl *actbl) { int temp, temp2, temp3; int nbits; int r, code, size; JOCTET _buffer[BUFSIZE], *buffer; size_t put_buffer; int put_bits; int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0]; size_t bytes, bytestocopy; int localbuf = 0; put_buffer = state->cur.put_buffer; put_bits = state->cur.put_bits; LOAD_BUFFER() /* Encode the DC coefficient difference per section F.1.2.1 */ temp = temp2 = block[0] - last_dc_val; /* This is a well-known technique for obtaining the absolute value without a * branch. It is derived from an assembly language technique presented in * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by * Agner Fog. */ temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); temp ^= temp3; temp -= temp3; /* For a negative input, want temp2 = bitwise complement of abs(input) */ /* This code assumes we are on a two's complement machine */ temp2 += temp3; /* Find the number of bits needed for the magnitude of the coefficient */ nbits = jpeg_nbits_table[temp]; /* Emit the Huffman-coded symbol for the number of bits */ code = dctbl->ehufco[nbits]; size = dctbl->ehufsi[nbits]; PUT_BITS(code, size) CHECKBUF15() /* Mask off any extra bits in code */ temp2 &= (((INT32) 1)<> (CHAR_BIT * sizeof(int) - 1); \ temp ^= temp3; \ temp -= temp3; \ temp2 += temp3; \ nbits = jpeg_nbits_table[temp]; \ /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \ while (r > 15) { \ EMIT_BITS(code_0xf0, size_0xf0) \ r -= 16; \ } \ /* Emit Huffman symbol for run length / number of bits */ \ temp3 = (r << 4) + nbits; \ code = actbl->ehufco[temp3]; \ size = actbl->ehufsi[temp3]; \ EMIT_CODE(code, size) \ r = 0; \ } \ } /* One iteration for each value in jpeg_natural_order[] */ kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3); kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18); kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26); kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27); kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21); kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57); kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15); kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58); kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39); kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47); kloop(55); kloop(62); kloop(63); /* If the last coef(s) were zero, emit an end-of-block code */ if (r > 0) { code = actbl->ehufco[0]; size = actbl->ehufsi[0]; EMIT_BITS(code, size) } state->cur.put_buffer = put_buffer; state->cur.put_bits = put_bits; STORE_BUFFER() return TRUE; } /* * Emit a restart marker & resynchronize predictions. */ LOCAL(boolean) emit_restart (working_state * state, int restart_num) { int ci; if (! flush_bits(state)) return FALSE; emit_byte(state, 0xFF, return FALSE); emit_byte(state, JPEG_RST0 + restart_num, return FALSE); /* Re-initialize DC predictions to 0 */ for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) state->cur.last_dc_val[ci] = 0; /* The restart counter is not updated until we successfully write the MCU. */ return TRUE; } /* * Encode and output one MCU's worth of Huffman-compressed coefficients. */ METHODDEF(boolean) encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; working_state state; int blkn, ci; jpeg_component_info * compptr; /* Load up working state */ state.next_output_byte = cinfo->dest->next_output_byte; state.free_in_buffer = cinfo->dest->free_in_buffer; ASSIGN_STATE(state.cur, entropy->saved); state.cinfo = cinfo; /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! emit_restart(&state, entropy->next_restart_num)) return FALSE; } /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; if (! encode_one_block(&state, MCU_data[blkn][0], state.cur.last_dc_val[ci], entropy->dc_derived_tbls[compptr->dc_tbl_no], entropy->ac_derived_tbls[compptr->ac_tbl_no])) return FALSE; /* Update last_dc_val */ state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; } /* Completed MCU, so update state */ cinfo->dest->next_output_byte = state.next_output_byte; cinfo->dest->free_in_buffer = state.free_in_buffer; ASSIGN_STATE(entropy->saved, state.cur); /* Update restart-interval state too */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } return TRUE; } /* * Finish up at the end of a Huffman-compressed scan. */ METHODDEF(void) finish_pass_huff (j_compress_ptr cinfo) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; working_state state; /* Load up working state ... flush_bits needs it */ state.next_output_byte = cinfo->dest->next_output_byte; state.free_in_buffer = cinfo->dest->free_in_buffer; ASSIGN_STATE(state.cur, entropy->saved); state.cinfo = cinfo; /* Flush out the last data */ if (! flush_bits(&state)) ERREXIT(cinfo, JERR_CANT_SUSPEND); /* Update state */ cinfo->dest->next_output_byte = state.next_output_byte; cinfo->dest->free_in_buffer = state.free_in_buffer; ASSIGN_STATE(entropy->saved, state.cur); } /* * Huffman coding optimization. * * We first scan the supplied data and count the number of uses of each symbol * that is to be Huffman-coded. (This process MUST agree with the code above.) * Then we build a Huffman coding tree for the observed counts. * Symbols which are not needed at all for the particular image are not * assigned any code, which saves space in the DHT marker as well as in * the compressed data. */ #ifdef ENTROPY_OPT_SUPPORTED /* Process a single block's worth of coefficients */ LOCAL(void) htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[]) { register int temp; register int nbits; register int k, r; /* Encode the DC coefficient difference per section F.1.2.1 */ temp = block[0] - last_dc_val; if (temp < 0) temp = -temp; /* Find the number of bits needed for the magnitude of the coefficient */ nbits = 0; while (temp) { nbits++; temp >>= 1; } /* Check for out-of-range coefficient values. * Since we're encoding a difference, the range limit is twice as much. */ if (nbits > MAX_COEF_BITS+1) ERREXIT(cinfo, JERR_BAD_DCT_COEF); /* Count the Huffman symbol for the number of bits */ dc_counts[nbits]++; /* Encode the AC coefficients per section F.1.2.2 */ r = 0; /* r = run length of zeros */ for (k = 1; k < DCTSIZE2; k++) { if ((temp = block[jpeg_natural_order[k]]) == 0) { r++; } else { /* if run length > 15, must emit special run-length-16 codes (0xF0) */ while (r > 15) { ac_counts[0xF0]++; r -= 16; } /* Find the number of bits needed for the magnitude of the coefficient */ if (temp < 0) temp = -temp; /* Find the number of bits needed for the magnitude of the coefficient */ nbits = 1; /* there must be at least one 1 bit */ while ((temp >>= 1)) nbits++; /* Check for out-of-range coefficient values */ if (nbits > MAX_COEF_BITS) ERREXIT(cinfo, JERR_BAD_DCT_COEF); /* Count Huffman symbol for run length / number of bits */ ac_counts[(r << 4) + nbits]++; r = 0; } } /* If the last coef(s) were zero, emit an end-of-block code */ if (r > 0) ac_counts[0]++; } /* * Trial-encode one MCU's worth of Huffman-compressed coefficients. * No data is actually output, so no suspension return is possible. */ METHODDEF(boolean) encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int blkn, ci; jpeg_component_info * compptr; /* Take care of restart intervals if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { /* Re-initialize DC predictions to 0 */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) entropy->saved.last_dc_val[ci] = 0; /* Update restart state */ entropy->restarts_to_go = cinfo->restart_interval; } entropy->restarts_to_go--; } for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci], entropy->dc_count_ptrs[compptr->dc_tbl_no], entropy->ac_count_ptrs[compptr->ac_tbl_no]); entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; } return TRUE; } /* * Generate the best Huffman code table for the given counts, fill htbl. * Note this is also used by jcphuff.c. * * The JPEG standard requires that no symbol be assigned a codeword of all * one bits (so that padding bits added at the end of a compressed segment * can't look like a valid code). Because of the canonical ordering of * codewords, this just means that there must be an unused slot in the * longest codeword length category. Section K.2 of the JPEG spec suggests * reserving such a slot by pretending that symbol 256 is a valid symbol * with count 1. In theory that's not optimal; giving it count zero but * including it in the symbol set anyway should give a better Huffman code. * But the theoretically better code actually seems to come out worse in * practice, because it produces more all-ones bytes (which incur stuffed * zero bytes in the final file). In any case the difference is tiny. * * The JPEG standard requires Huffman codes to be no more than 16 bits long. * If some symbols have a very small but nonzero probability, the Huffman tree * must be adjusted to meet the code length restriction. We currently use * the adjustment method suggested in JPEG section K.2. This method is *not* * optimal; it may not choose the best possible limited-length code. But * typically only very-low-frequency symbols will be given less-than-optimal * lengths, so the code is almost optimal. Experimental comparisons against * an optimal limited-length-code algorithm indicate that the difference is * microscopic --- usually less than a hundredth of a percent of total size. * So the extra complexity of an optimal algorithm doesn't seem worthwhile. */ GLOBAL(void) jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]) { #define MAX_CLEN 32 /* assumed maximum initial code length */ UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ int codesize[257]; /* codesize[k] = code length of symbol k */ int others[257]; /* next symbol in current branch of tree */ int c1, c2; int p, i, j; long v; /* This algorithm is explained in section K.2 of the JPEG standard */ MEMZERO(bits, SIZEOF(bits)); MEMZERO(codesize, SIZEOF(codesize)); for (i = 0; i < 257; i++) others[i] = -1; /* init links to empty */ freq[256] = 1; /* make sure 256 has a nonzero count */ /* Including the pseudo-symbol 256 in the Huffman procedure guarantees * that no real symbol is given code-value of all ones, because 256 * will be placed last in the largest codeword category. */ /* Huffman's basic algorithm to assign optimal code lengths to symbols */ for (;;) { /* Find the smallest nonzero frequency, set c1 = its symbol */ /* In case of ties, take the larger symbol number */ c1 = -1; v = 1000000000L; for (i = 0; i <= 256; i++) { if (freq[i] && freq[i] <= v) { v = freq[i]; c1 = i; } } /* Find the next smallest nonzero frequency, set c2 = its symbol */ /* In case of ties, take the larger symbol number */ c2 = -1; v = 1000000000L; for (i = 0; i <= 256; i++) { if (freq[i] && freq[i] <= v && i != c1) { v = freq[i]; c2 = i; } } /* Done if we've merged everything into one frequency */ if (c2 < 0) break; /* Else merge the two counts/trees */ freq[c1] += freq[c2]; freq[c2] = 0; /* Increment the codesize of everything in c1's tree branch */ codesize[c1]++; while (others[c1] >= 0) { c1 = others[c1]; codesize[c1]++; } others[c1] = c2; /* chain c2 onto c1's tree branch */ /* Increment the codesize of everything in c2's tree branch */ codesize[c2]++; while (others[c2] >= 0) { c2 = others[c2]; codesize[c2]++; } } /* Now count the number of symbols of each code length */ for (i = 0; i <= 256; i++) { if (codesize[i]) { /* The JPEG standard seems to think that this can't happen, */ /* but I'm paranoid... */ if (codesize[i] > MAX_CLEN) ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); bits[codesize[i]]++; } } /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure * Huffman procedure assigned any such lengths, we must adjust the coding. * Here is what the JPEG spec says about how this next bit works: * Since symbols are paired for the longest Huffman code, the symbols are * removed from this length category two at a time. The prefix for the pair * (which is one bit shorter) is allocated to one of the pair; then, * skipping the BITS entry for that prefix length, a code word from the next * shortest nonzero BITS entry is converted into a prefix for two code words * one bit longer. */ for (i = MAX_CLEN; i > 16; i--) { while (bits[i] > 0) { j = i - 2; /* find length of new prefix to be used */ while (bits[j] == 0) j--; bits[i] -= 2; /* remove two symbols */ bits[i-1]++; /* one goes in this length */ bits[j+1] += 2; /* two new symbols in this length */ bits[j]--; /* symbol of this length is now a prefix */ } } /* Remove the count for the pseudo-symbol 256 from the largest codelength */ while (bits[i] == 0) /* find largest codelength still in use */ i--; bits[i]--; /* Return final symbol counts (only for lengths 0..16) */ MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); /* Return a list of the symbols sorted by code length */ /* It's not real clear to me why we don't need to consider the codelength * changes made above, but the JPEG spec seems to think this works. */ p = 0; for (i = 1; i <= MAX_CLEN; i++) { for (j = 0; j <= 255; j++) { if (codesize[j] == i) { htbl->huffval[p] = (UINT8) j; p++; } } } /* Set sent_table FALSE so updated table will be written to JPEG file. */ htbl->sent_table = FALSE; } /* * Finish up a statistics-gathering pass and create the new Huffman tables. */ METHODDEF(void) finish_pass_gather (j_compress_ptr cinfo) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci, dctbl, actbl; jpeg_component_info * compptr; JHUFF_TBL **htblptr; boolean did_dc[NUM_HUFF_TBLS]; boolean did_ac[NUM_HUFF_TBLS]; /* It's important not to apply jpeg_gen_optimal_table more than once * per table, because it clobbers the input frequency counts! */ MEMZERO(did_dc, SIZEOF(did_dc)); MEMZERO(did_ac, SIZEOF(did_ac)); for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; dctbl = compptr->dc_tbl_no; actbl = compptr->ac_tbl_no; if (! did_dc[dctbl]) { htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl]; if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]); did_dc[dctbl] = TRUE; } if (! did_ac[actbl]) { htblptr = & cinfo->ac_huff_tbl_ptrs[actbl]; if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]); did_ac[actbl] = TRUE; } } } #endif /* ENTROPY_OPT_SUPPORTED */ /* * Module initialization routine for Huffman entropy encoding. */ GLOBAL(void) jinit_huff_encoder (j_compress_ptr cinfo) { huff_entropy_ptr entropy; int i; entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_encoder)); cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; entropy->pub.start_pass = start_pass_huff; /* Mark tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; #ifdef ENTROPY_OPT_SUPPORTED entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; #endif } } glmark2-2012.08/./src/libjpeg-turbo/jccoefct.c0000664000175000017500000004002012013417376020172 0ustar alfalf00000000000000/* * jccoefct.c * * Copyright (C) 1994-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the coefficient buffer controller for compression. * This controller is the top level of the JPEG compressor proper. * The coefficient buffer lies between forward-DCT and entropy encoding steps. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* We use a full-image coefficient buffer when doing Huffman optimization, * and also for writing multiple-scan JPEG files. In all cases, the DCT * step is run during the first pass, and subsequent passes need only read * the buffered coefficients. */ #ifdef ENTROPY_OPT_SUPPORTED #define FULL_COEF_BUFFER_SUPPORTED #else #ifdef C_MULTISCAN_FILES_SUPPORTED #define FULL_COEF_BUFFER_SUPPORTED #endif #endif /* Private buffer controller object */ typedef struct { struct jpeg_c_coef_controller pub; /* public fields */ JDIMENSION iMCU_row_num; /* iMCU row # within image */ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ int MCU_vert_offset; /* counts MCU rows within iMCU row */ int MCU_rows_per_iMCU_row; /* number of such rows needed */ /* For single-pass compression, it's sufficient to buffer just one MCU * (although this may prove a bit slow in practice). We allocate a * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each * MCU constructed and sent. (On 80x86, the workspace is FAR even though * it's not really very big; this is to keep the module interfaces unchanged * when a large coefficient buffer is necessary.) * In multi-pass modes, this array points to the current MCU's blocks * within the virtual arrays. */ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; /* In multi-pass modes, we need a virtual block array for each component. */ jvirt_barray_ptr whole_image[MAX_COMPONENTS]; } my_coef_controller; typedef my_coef_controller * my_coef_ptr; /* Forward declarations */ METHODDEF(boolean) compress_data JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); #ifdef FULL_COEF_BUFFER_SUPPORTED METHODDEF(boolean) compress_first_pass JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); METHODDEF(boolean) compress_output JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf)); #endif LOCAL(void) start_iMCU_row (j_compress_ptr cinfo) /* Reset within-iMCU-row counters for a new row */ { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; /* In an interleaved scan, an MCU row is the same as an iMCU row. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. * But at the bottom of the image, process only what's left. */ if (cinfo->comps_in_scan > 1) { coef->MCU_rows_per_iMCU_row = 1; } else { if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; else coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; } coef->mcu_ctr = 0; coef->MCU_vert_offset = 0; } /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; coef->iMCU_row_num = 0; start_iMCU_row(cinfo); switch (pass_mode) { case JBUF_PASS_THRU: if (coef->whole_image[0] != NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); coef->pub.compress_data = compress_data; break; #ifdef FULL_COEF_BUFFER_SUPPORTED case JBUF_SAVE_AND_PASS: if (coef->whole_image[0] == NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); coef->pub.compress_data = compress_first_pass; break; case JBUF_CRANK_DEST: if (coef->whole_image[0] == NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); coef->pub.compress_data = compress_output; break; #endif default: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); break; } } /* * Process some data in the single-pass case. * We process the equivalent of one fully interleaved MCU row ("iMCU" row) * per call, ie, v_samp_factor block rows for each component in the image. * Returns TRUE if the iMCU row is completed, FALSE if suspended. * * NB: input_buf contains a plane for each component in image, * which we index according to the component's SOF position. */ METHODDEF(boolean) compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; int blkn, bi, ci, yindex, yoffset, blockcnt; JDIMENSION ypos, xpos; jpeg_component_info *compptr; /* Loop to write as much as one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) { /* Determine where data comes from in input_buf and do the DCT thing. * Each call on forward_DCT processes a horizontal row of DCT blocks * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks * sequentially. Dummy blocks at the right or bottom edge are filled in * specially. The data in them does not matter for image reconstruction, * so we fill them with values that will encode to the smallest amount of * data, viz: all zeroes in the AC entries, DC entries equal to previous * block's DC value. (Thanks to Thomas Kinsman for this idea.) */ blkn = 0; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width : compptr->last_col_width; xpos = MCU_col_num * compptr->MCU_sample_width; ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */ for (yindex = 0; yindex < compptr->MCU_height; yindex++) { if (coef->iMCU_row_num < last_iMCU_row || yoffset+yindex < compptr->last_row_height) { (*cinfo->fdct->forward_DCT) (cinfo, compptr, input_buf[compptr->component_index], coef->MCU_buffer[blkn], ypos, xpos, (JDIMENSION) blockcnt); if (blockcnt < compptr->MCU_width) { /* Create some dummy blocks at the right edge of the image. */ jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt], (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK)); for (bi = blockcnt; bi < compptr->MCU_width; bi++) { coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0]; } } } else { /* Create a row of dummy blocks at the bottom of the image. */ jzero_far((void FAR *) coef->MCU_buffer[blkn], compptr->MCU_width * SIZEOF(JBLOCK)); for (bi = 0; bi < compptr->MCU_width; bi++) { coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0]; } } blkn += compptr->MCU_width; ypos += DCTSIZE; } } /* Try to write the MCU. In event of a suspension failure, we will * re-DCT the MCU on restart (a bit inefficient, could be fixed...) */ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->mcu_ctr = MCU_col_num; return FALSE; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->mcu_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ coef->iMCU_row_num++; start_iMCU_row(cinfo); return TRUE; } #ifdef FULL_COEF_BUFFER_SUPPORTED /* * Process some data in the first pass of a multi-pass case. * We process the equivalent of one fully interleaved MCU row ("iMCU" row) * per call, ie, v_samp_factor block rows for each component in the image. * This amount of data is read from the source buffer, DCT'd and quantized, * and saved into the virtual arrays. We also generate suitable dummy blocks * as needed at the right and lower edges. (The dummy blocks are constructed * in the virtual arrays, which have been padded appropriately.) This makes * it possible for subsequent passes not to worry about real vs. dummy blocks. * * We must also emit the data to the entropy encoder. This is conveniently * done by calling compress_output() after we've loaded the current strip * of the virtual arrays. * * NB: input_buf contains a plane for each component in image. All * components are DCT'd and loaded into the virtual arrays in this pass. * However, it may be that only a subset of the components are emitted to * the entropy encoder during this first pass; be careful about looking * at the scan-dependent variables (MCU dimensions, etc). */ METHODDEF(boolean) compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; JDIMENSION blocks_across, MCUs_across, MCUindex; int bi, ci, h_samp_factor, block_row, block_rows, ndummy; JCOEF lastDC; jpeg_component_info *compptr; JBLOCKARRAY buffer; JBLOCKROW thisblockrow, lastblockrow; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Align the virtual buffer for this component. */ buffer = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[ci], coef->iMCU_row_num * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, TRUE); /* Count non-dummy DCT block rows in this iMCU row. */ if (coef->iMCU_row_num < last_iMCU_row) block_rows = compptr->v_samp_factor; else { /* NB: can't use last_row_height here, since may not be set! */ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); if (block_rows == 0) block_rows = compptr->v_samp_factor; } blocks_across = compptr->width_in_blocks; h_samp_factor = compptr->h_samp_factor; /* Count number of dummy blocks to be added at the right margin. */ ndummy = (int) (blocks_across % h_samp_factor); if (ndummy > 0) ndummy = h_samp_factor - ndummy; /* Perform DCT for all non-dummy blocks in this iMCU row. Each call * on forward_DCT processes a complete horizontal row of DCT blocks. */ for (block_row = 0; block_row < block_rows; block_row++) { thisblockrow = buffer[block_row]; (*cinfo->fdct->forward_DCT) (cinfo, compptr, input_buf[ci], thisblockrow, (JDIMENSION) (block_row * DCTSIZE), (JDIMENSION) 0, blocks_across); if (ndummy > 0) { /* Create dummy blocks at the right edge of the image. */ thisblockrow += blocks_across; /* => first dummy block */ jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK)); lastDC = thisblockrow[-1][0]; for (bi = 0; bi < ndummy; bi++) { thisblockrow[bi][0] = lastDC; } } } /* If at end of image, create dummy block rows as needed. * The tricky part here is that within each MCU, we want the DC values * of the dummy blocks to match the last real block's DC value. * This squeezes a few more bytes out of the resulting file... */ if (coef->iMCU_row_num == last_iMCU_row) { blocks_across += ndummy; /* include lower right corner */ MCUs_across = blocks_across / h_samp_factor; for (block_row = block_rows; block_row < compptr->v_samp_factor; block_row++) { thisblockrow = buffer[block_row]; lastblockrow = buffer[block_row-1]; jzero_far((void FAR *) thisblockrow, (size_t) (blocks_across * SIZEOF(JBLOCK))); for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) { lastDC = lastblockrow[h_samp_factor-1][0]; for (bi = 0; bi < h_samp_factor; bi++) { thisblockrow[bi][0] = lastDC; } thisblockrow += h_samp_factor; /* advance to next MCU in row */ lastblockrow += h_samp_factor; } } } } /* NB: compress_output will increment iMCU_row_num if successful. * A suspension return will result in redoing all the work above next time. */ /* Emit data to the entropy encoder, sharing code with subsequent passes */ return compress_output(cinfo, input_buf); } /* * Process some data in subsequent passes of a multi-pass case. * We process the equivalent of one fully interleaved MCU row ("iMCU" row) * per call, ie, v_samp_factor block rows for each component in the scan. * The data is obtained from the virtual arrays and fed to the entropy coder. * Returns TRUE if the iMCU row is completed, FALSE if suspended. * * NB: input_buf is ignored; it is likely to be a NULL pointer. */ METHODDEF(boolean) compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ int blkn, ci, xindex, yindex, yoffset; JDIMENSION start_col; JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; JBLOCKROW buffer_ptr; jpeg_component_info *compptr; /* Align the virtual buffers for the components used in this scan. * NB: during first pass, this is safe only because the buffers will * already be aligned properly, so jmemmgr.c won't need to do any I/O. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; buffer[ci] = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], coef->iMCU_row_num * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, FALSE); } /* Loop to process one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) { /* Construct list of pointers to DCT blocks belonging to this MCU */ blkn = 0; /* index of current DCT block within MCU */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; start_col = MCU_col_num * compptr->MCU_width; for (yindex = 0; yindex < compptr->MCU_height; yindex++) { buffer_ptr = buffer[ci][yindex+yoffset] + start_col; for (xindex = 0; xindex < compptr->MCU_width; xindex++) { coef->MCU_buffer[blkn++] = buffer_ptr++; } } } /* Try to write the MCU. */ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->mcu_ctr = MCU_col_num; return FALSE; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->mcu_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ coef->iMCU_row_num++; start_iMCU_row(cinfo); return TRUE; } #endif /* FULL_COEF_BUFFER_SUPPORTED */ /* * Initialize coefficient buffer controller. */ GLOBAL(void) jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer) { my_coef_ptr coef; coef = (my_coef_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller)); cinfo->coef = (struct jpeg_c_coef_controller *) coef; coef->pub.start_pass = start_pass_coef; /* Create the coefficient buffer. */ if (need_full_buffer) { #ifdef FULL_COEF_BUFFER_SUPPORTED /* Allocate a full-image virtual array for each component, */ /* padded to a multiple of samp_factor DCT blocks in each direction. */ int ci; jpeg_component_info *compptr; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, (JDIMENSION) jround_up((long) compptr->width_in_blocks, (long) compptr->h_samp_factor), (JDIMENSION) jround_up((long) compptr->height_in_blocks, (long) compptr->v_samp_factor), (JDIMENSION) compptr->v_samp_factor); } #else ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); #endif } else { /* We only need a single-MCU buffer. */ JBLOCKROW buffer; int i; buffer = (JBLOCKROW) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { coef->MCU_buffer[i] = buffer + i; } coef->whole_image[0] = NULL; /* flag for no virtual arrays */ } } glmark2-2012.08/./src/libjpeg-turbo/jerror.c0000664000175000017500000001717112013417376017730 0ustar alfalf00000000000000/* * jerror.c * * Copyright (C) 1991-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains simple error-reporting and trace-message routines. * These are suitable for Unix-like systems and others where writing to * stderr is the right thing to do. Many applications will want to replace * some or all of these routines. * * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, * you get a Windows-specific hack to display error messages in a dialog box. * It ain't much, but it beats dropping error messages into the bit bucket, * which is what happens to output to stderr under most Windows C compilers. * * These routines are used by both the compression and decompression code. */ /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ #include "jinclude.h" #include "jpeglib.h" #include "jversion.h" #include "jerror.h" #ifdef USE_WINDOWS_MESSAGEBOX #include #endif #ifndef EXIT_FAILURE /* define exit() codes if not provided */ #define EXIT_FAILURE 1 #endif /* * Create the message string table. * We do this from the master message list in jerror.h by re-reading * jerror.h with a suitable definition for macro JMESSAGE. * The message table is made an external symbol just in case any applications * want to refer to it directly. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_std_message_table jMsgTable #endif #define JMESSAGE(code,string) string , const char * const jpeg_std_message_table[] = { #include "jerror.h" NULL }; /* * Error exit handler: must not return to caller. * * Applications may override this if they want to get control back after * an error. Typically one would longjmp somewhere instead of exiting. * The setjmp buffer can be made a private field within an expanded error * handler object. Note that the info needed to generate an error message * is stored in the error object, so you can generate the message now or * later, at your convenience. * You should make sure that the JPEG object is cleaned up (with jpeg_abort * or jpeg_destroy) at some point. */ METHODDEF(void) error_exit (j_common_ptr cinfo) { /* Always display the message */ (*cinfo->err->output_message) (cinfo); /* Let the memory manager delete any temp files before we die */ jpeg_destroy(cinfo); exit(EXIT_FAILURE); } /* * Actual output of an error or trace message. * Applications may override this method to send JPEG messages somewhere * other than stderr. * * On Windows, printing to stderr is generally completely useless, * so we provide optional code to produce an error-dialog popup. * Most Windows applications will still prefer to override this routine, * but if they don't, it'll do something at least marginally useful. * * NOTE: to use the library in an environment that doesn't support the * C stdio library, you may have to delete the call to fprintf() entirely, * not just not use this routine. */ METHODDEF(void) output_message (j_common_ptr cinfo) { char buffer[JMSG_LENGTH_MAX]; /* Create the message */ (*cinfo->err->format_message) (cinfo, buffer); #ifdef USE_WINDOWS_MESSAGEBOX /* Display it in a message dialog box */ MessageBox(GetActiveWindow(), buffer, "JPEG Library Error", MB_OK | MB_ICONERROR); #else /* Send it to stderr, adding a newline */ fprintf(stderr, "%s\n", buffer); #endif } /* * Decide whether to emit a trace or warning message. * msg_level is one of: * -1: recoverable corrupt-data warning, may want to abort. * 0: important advisory messages (always display to user). * 1: first level of tracing detail. * 2,3,...: successively more detailed tracing messages. * An application might override this method if it wanted to abort on warnings * or change the policy about which messages to display. */ METHODDEF(void) emit_message (j_common_ptr cinfo, int msg_level) { struct jpeg_error_mgr * err = cinfo->err; if (msg_level < 0) { /* It's a warning message. Since corrupt files may generate many warnings, * the policy implemented here is to show only the first warning, * unless trace_level >= 3. */ if (err->num_warnings == 0 || err->trace_level >= 3) (*err->output_message) (cinfo); /* Always count warnings in num_warnings. */ err->num_warnings++; } else { /* It's a trace message. Show it if trace_level >= msg_level. */ if (err->trace_level >= msg_level) (*err->output_message) (cinfo); } } /* * Format a message string for the most recent JPEG error or message. * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX * characters. Note that no '\n' character is added to the string. * Few applications should need to override this method. */ METHODDEF(void) format_message (j_common_ptr cinfo, char * buffer) { struct jpeg_error_mgr * err = cinfo->err; int msg_code = err->msg_code; const char * msgtext = NULL; const char * msgptr; char ch; boolean isstring; /* Look up message string in proper table */ if (msg_code > 0 && msg_code <= err->last_jpeg_message) { msgtext = err->jpeg_message_table[msg_code]; } else if (err->addon_message_table != NULL && msg_code >= err->first_addon_message && msg_code <= err->last_addon_message) { msgtext = err->addon_message_table[msg_code - err->first_addon_message]; } /* Defend against bogus message number */ if (msgtext == NULL) { err->msg_parm.i[0] = msg_code; msgtext = err->jpeg_message_table[0]; } /* Check for string parameter, as indicated by %s in the message text */ isstring = FALSE; msgptr = msgtext; while ((ch = *msgptr++) != '\0') { if (ch == '%') { if (*msgptr == 's') isstring = TRUE; break; } } /* Format the message into the passed buffer */ if (isstring) sprintf(buffer, msgtext, err->msg_parm.s); else sprintf(buffer, msgtext, err->msg_parm.i[0], err->msg_parm.i[1], err->msg_parm.i[2], err->msg_parm.i[3], err->msg_parm.i[4], err->msg_parm.i[5], err->msg_parm.i[6], err->msg_parm.i[7]); } /* * Reset error state variables at start of a new image. * This is called during compression startup to reset trace/error * processing to default state, without losing any application-specific * method pointers. An application might possibly want to override * this method if it has additional error processing state. */ METHODDEF(void) reset_error_mgr (j_common_ptr cinfo) { cinfo->err->num_warnings = 0; /* trace_level is not reset since it is an application-supplied parameter */ cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ } /* * Fill in the standard error-handling methods in a jpeg_error_mgr object. * Typical call is: * struct jpeg_compress_struct cinfo; * struct jpeg_error_mgr err; * * cinfo.err = jpeg_std_error(&err); * after which the application may override some of the methods. */ GLOBAL(struct jpeg_error_mgr *) jpeg_std_error (struct jpeg_error_mgr * err) { err->error_exit = error_exit; err->emit_message = emit_message; err->output_message = output_message; err->format_message = format_message; err->reset_error_mgr = reset_error_mgr; err->trace_level = 0; /* default = no tracing */ err->num_warnings = 0; /* no warnings emitted yet */ err->msg_code = 0; /* may be useful as a flag for "no error" */ /* Initialize message table pointers */ err->jpeg_message_table = jpeg_std_message_table; err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; err->addon_message_table = NULL; err->first_addon_message = 0; /* for safety */ err->last_addon_message = 0; return err; } glmark2-2012.08/./src/libjpeg-turbo/jcprepct.c0000664000175000017500000002745112013417376020241 0ustar alfalf00000000000000/* * jcprepct.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the compression preprocessing controller. * This controller manages the color conversion, downsampling, * and edge expansion steps. * * Most of the complexity here is associated with buffering input rows * as required by the downsampler. See the comments at the head of * jcsample.c for the downsampler's needs. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* At present, jcsample.c can request context rows only for smoothing. * In the future, we might also need context rows for CCIR601 sampling * or other more-complex downsampling procedures. The code to support * context rows should be compiled only if needed. */ #ifdef INPUT_SMOOTHING_SUPPORTED #define CONTEXT_ROWS_SUPPORTED #endif /* * For the simple (no-context-row) case, we just need to buffer one * row group's worth of pixels for the downsampling step. At the bottom of * the image, we pad to a full row group by replicating the last pixel row. * The downsampler's last output row is then replicated if needed to pad * out to a full iMCU row. * * When providing context rows, we must buffer three row groups' worth of * pixels. Three row groups are physically allocated, but the row pointer * arrays are made five row groups high, with the extra pointers above and * below "wrapping around" to point to the last and first real row groups. * This allows the downsampler to access the proper context rows. * At the top and bottom of the image, we create dummy context rows by * copying the first or last real pixel row. This copying could be avoided * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the * trouble on the compression side. */ /* Private buffer controller object */ typedef struct { struct jpeg_c_prep_controller pub; /* public fields */ /* Downsampling input buffer. This buffer holds color-converted data * until we have enough to do a downsample step. */ JSAMPARRAY color_buf[MAX_COMPONENTS]; JDIMENSION rows_to_go; /* counts rows remaining in source image */ int next_buf_row; /* index of next row to store in color_buf */ #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */ int this_row_group; /* starting row index of group to process */ int next_buf_stop; /* downsample when we reach this index */ #endif } my_prep_controller; typedef my_prep_controller * my_prep_ptr; /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode) { my_prep_ptr prep = (my_prep_ptr) cinfo->prep; if (pass_mode != JBUF_PASS_THRU) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); /* Initialize total-height counter for detecting bottom of image */ prep->rows_to_go = cinfo->image_height; /* Mark the conversion buffer empty */ prep->next_buf_row = 0; #ifdef CONTEXT_ROWS_SUPPORTED /* Preset additional state variables for context mode. * These aren't used in non-context mode, so we needn't test which mode. */ prep->this_row_group = 0; /* Set next_buf_stop to stop after two row groups have been read in. */ prep->next_buf_stop = 2 * cinfo->max_v_samp_factor; #endif } /* * Expand an image vertically from height input_rows to height output_rows, * by duplicating the bottom row. */ LOCAL(void) expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols, int input_rows, int output_rows) { register int row; for (row = input_rows; row < output_rows; row++) { jcopy_sample_rows(image_data, input_rows-1, image_data, row, 1, num_cols); } } /* * Process some data in the simple no-context case. * * Preprocessor output data is counted in "row groups". A row group * is defined to be v_samp_factor sample rows of each component. * Downsampling will produce this much data from each max_v_samp_factor * input rows. */ METHODDEF(void) pre_process_data (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail, JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, JDIMENSION out_row_groups_avail) { my_prep_ptr prep = (my_prep_ptr) cinfo->prep; int numrows, ci; JDIMENSION inrows; jpeg_component_info * compptr; while (*in_row_ctr < in_rows_avail && *out_row_group_ctr < out_row_groups_avail) { /* Do color conversion to fill the conversion buffer. */ inrows = in_rows_avail - *in_row_ctr; numrows = cinfo->max_v_samp_factor - prep->next_buf_row; numrows = (int) MIN((JDIMENSION) numrows, inrows); (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, prep->color_buf, (JDIMENSION) prep->next_buf_row, numrows); *in_row_ctr += numrows; prep->next_buf_row += numrows; prep->rows_to_go -= numrows; /* If at bottom of image, pad to fill the conversion buffer. */ if (prep->rows_to_go == 0 && prep->next_buf_row < cinfo->max_v_samp_factor) { for (ci = 0; ci < cinfo->num_components; ci++) { expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, prep->next_buf_row, cinfo->max_v_samp_factor); } prep->next_buf_row = cinfo->max_v_samp_factor; } /* If we've filled the conversion buffer, empty it. */ if (prep->next_buf_row == cinfo->max_v_samp_factor) { (*cinfo->downsample->downsample) (cinfo, prep->color_buf, (JDIMENSION) 0, output_buf, *out_row_group_ctr); prep->next_buf_row = 0; (*out_row_group_ctr)++; } /* If at bottom of image, pad the output to a full iMCU height. * Note we assume the caller is providing a one-iMCU-height output buffer! */ if (prep->rows_to_go == 0 && *out_row_group_ctr < out_row_groups_avail) { for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { expand_bottom_edge(output_buf[ci], compptr->width_in_blocks * DCTSIZE, (int) (*out_row_group_ctr * compptr->v_samp_factor), (int) (out_row_groups_avail * compptr->v_samp_factor)); } *out_row_group_ctr = out_row_groups_avail; break; /* can exit outer loop without test */ } } } #ifdef CONTEXT_ROWS_SUPPORTED /* * Process some data in the context case. */ METHODDEF(void) pre_process_context (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail, JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr, JDIMENSION out_row_groups_avail) { my_prep_ptr prep = (my_prep_ptr) cinfo->prep; int numrows, ci; int buf_height = cinfo->max_v_samp_factor * 3; JDIMENSION inrows; while (*out_row_group_ctr < out_row_groups_avail) { if (*in_row_ctr < in_rows_avail) { /* Do color conversion to fill the conversion buffer. */ inrows = in_rows_avail - *in_row_ctr; numrows = prep->next_buf_stop - prep->next_buf_row; numrows = (int) MIN((JDIMENSION) numrows, inrows); (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr, prep->color_buf, (JDIMENSION) prep->next_buf_row, numrows); /* Pad at top of image, if first time through */ if (prep->rows_to_go == cinfo->image_height) { for (ci = 0; ci < cinfo->num_components; ci++) { int row; for (row = 1; row <= cinfo->max_v_samp_factor; row++) { jcopy_sample_rows(prep->color_buf[ci], 0, prep->color_buf[ci], -row, 1, cinfo->image_width); } } } *in_row_ctr += numrows; prep->next_buf_row += numrows; prep->rows_to_go -= numrows; } else { /* Return for more data, unless we are at the bottom of the image. */ if (prep->rows_to_go != 0) break; /* When at bottom of image, pad to fill the conversion buffer. */ if (prep->next_buf_row < prep->next_buf_stop) { for (ci = 0; ci < cinfo->num_components; ci++) { expand_bottom_edge(prep->color_buf[ci], cinfo->image_width, prep->next_buf_row, prep->next_buf_stop); } prep->next_buf_row = prep->next_buf_stop; } } /* If we've gotten enough data, downsample a row group. */ if (prep->next_buf_row == prep->next_buf_stop) { (*cinfo->downsample->downsample) (cinfo, prep->color_buf, (JDIMENSION) prep->this_row_group, output_buf, *out_row_group_ctr); (*out_row_group_ctr)++; /* Advance pointers with wraparound as necessary. */ prep->this_row_group += cinfo->max_v_samp_factor; if (prep->this_row_group >= buf_height) prep->this_row_group = 0; if (prep->next_buf_row >= buf_height) prep->next_buf_row = 0; prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor; } } } /* * Create the wrapped-around downsampling input buffer needed for context mode. */ LOCAL(void) create_context_buffer (j_compress_ptr cinfo) { my_prep_ptr prep = (my_prep_ptr) cinfo->prep; int rgroup_height = cinfo->max_v_samp_factor; int ci, i; jpeg_component_info * compptr; JSAMPARRAY true_buffer, fake_buffer; /* Grab enough space for fake row pointers for all the components; * we need five row groups' worth of pointers for each component. */ fake_buffer = (JSAMPARRAY) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (cinfo->num_components * 5 * rgroup_height) * SIZEOF(JSAMPROW)); for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Allocate the actual buffer space (3 row groups) for this component. * We make the buffer wide enough to allow the downsampler to edge-expand * horizontally within the buffer, if it so chooses. */ true_buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE * cinfo->max_h_samp_factor) / compptr->h_samp_factor), (JDIMENSION) (3 * rgroup_height)); /* Copy true buffer row pointers into the middle of the fake row array */ MEMCOPY(fake_buffer + rgroup_height, true_buffer, 3 * rgroup_height * SIZEOF(JSAMPROW)); /* Fill in the above and below wraparound pointers */ for (i = 0; i < rgroup_height; i++) { fake_buffer[i] = true_buffer[2 * rgroup_height + i]; fake_buffer[4 * rgroup_height + i] = true_buffer[i]; } prep->color_buf[ci] = fake_buffer + rgroup_height; fake_buffer += 5 * rgroup_height; /* point to space for next component */ } } #endif /* CONTEXT_ROWS_SUPPORTED */ /* * Initialize preprocessing controller. */ GLOBAL(void) jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer) { my_prep_ptr prep; int ci; jpeg_component_info * compptr; if (need_full_buffer) /* safety check */ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); prep = (my_prep_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_prep_controller)); cinfo->prep = (struct jpeg_c_prep_controller *) prep; prep->pub.start_pass = start_pass_prep; /* Allocate the color conversion buffer. * We make the buffer wide enough to allow the downsampler to edge-expand * horizontally within the buffer, if it so chooses. */ if (cinfo->downsample->need_context_rows) { /* Set up to provide context rows */ #ifdef CONTEXT_ROWS_SUPPORTED prep->pub.pre_process_data = pre_process_context; create_context_buffer(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { /* No context, just make it tall enough for one row group */ prep->pub.pre_process_data = pre_process_data; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { prep->color_buf[ci] = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE * cinfo->max_h_samp_factor) / compptr->h_samp_factor), (JDIMENSION) cinfo->max_v_samp_factor); } } } glmark2-2012.08/./src/libjpeg-turbo/jquant1.c0000664000175000017500000007536112013417376020015 0ustar alfalf00000000000000/* * jquant1.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 2009, D. R. Commander * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains 1-pass color quantization (color mapping) routines. * These routines provide mapping to a fixed color map using equally spaced * color values. Optional Floyd-Steinberg or ordered dithering is available. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #ifdef QUANT_1PASS_SUPPORTED /* * The main purpose of 1-pass quantization is to provide a fast, if not very * high quality, colormapped output capability. A 2-pass quantizer usually * gives better visual quality; however, for quantized grayscale output this * quantizer is perfectly adequate. Dithering is highly recommended with this * quantizer, though you can turn it off if you really want to. * * In 1-pass quantization the colormap must be chosen in advance of seeing the * image. We use a map consisting of all combinations of Ncolors[i] color * values for the i'th component. The Ncolors[] values are chosen so that * their product, the total number of colors, is no more than that requested. * (In most cases, the product will be somewhat less.) * * Since the colormap is orthogonal, the representative value for each color * component can be determined without considering the other components; * then these indexes can be combined into a colormap index by a standard * N-dimensional-array-subscript calculation. Most of the arithmetic involved * can be precalculated and stored in the lookup table colorindex[]. * colorindex[i][j] maps pixel value j in component i to the nearest * representative value (grid plane) for that component; this index is * multiplied by the array stride for component i, so that the * index of the colormap entry closest to a given pixel value is just * sum( colorindex[component-number][pixel-component-value] ) * Aside from being fast, this scheme allows for variable spacing between * representative values with no additional lookup cost. * * If gamma correction has been applied in color conversion, it might be wise * to adjust the color grid spacing so that the representative colors are * equidistant in linear space. At this writing, gamma correction is not * implemented by jdcolor, so nothing is done here. */ /* Declarations for ordered dithering. * * We use a standard 16x16 ordered dither array. The basic concept of ordered * dithering is described in many references, for instance Dale Schumacher's * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991). * In place of Schumacher's comparisons against a "threshold" value, we add a * "dither" value to the input pixel and then round the result to the nearest * output value. The dither value is equivalent to (0.5 - threshold) times * the distance between output values. For ordered dithering, we assume that * the output colors are equally spaced; if not, results will probably be * worse, since the dither may be too much or too little at a given point. * * The normal calculation would be to form pixel value + dither, range-limit * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual. * We can skip the separate range-limiting step by extending the colorindex * table in both directions. */ #define ODITHER_SIZE 16 /* dimension of dither matrix */ /* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ #define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE) /* # cells in matrix */ #define ODITHER_MASK (ODITHER_SIZE-1) /* mask for wrapping around counters */ typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE]; typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE]; static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = { /* Bayer's order-4 dither array. Generated by the code given in * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I. * The values in this array must range from 0 to ODITHER_CELLS-1. */ { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 }, { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 }, { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 }, { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 }, { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 }, { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 }, { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 }, { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 }, { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 }, { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 }, { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 }, { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 }, { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 }, { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 }, { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 }, { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 } }; /* Declarations for Floyd-Steinberg dithering. * * Errors are accumulated into the array fserrors[], at a resolution of * 1/16th of a pixel count. The error at a given pixel is propagated * to its not-yet-processed neighbors using the standard F-S fractions, * ... (here) 7/16 * 3/16 5/16 1/16 * We work left-to-right on even rows, right-to-left on odd rows. * * We can get away with a single array (holding one row's worth of errors) * by using it to store the current row's errors at pixel columns not yet * processed, but the next row's errors at columns already processed. We * need only a few extra variables to hold the errors immediately around the * current column. (If we are lucky, those variables are in registers, but * even if not, they're probably cheaper to access than array elements are.) * * The fserrors[] array is indexed [component#][position]. * We provide (#columns + 2) entries per component; the extra entry at each * end saves us from special-casing the first and last pixels. * * Note: on a wide image, we might not have enough room in a PC's near data * segment to hold the error array; so it is allocated with alloc_large. */ #if BITS_IN_JSAMPLE == 8 typedef INT16 FSERROR; /* 16 bits should be enough */ typedef int LOCFSERROR; /* use 'int' for calculation temps */ #else typedef INT32 FSERROR; /* may need more than 16 bits */ typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */ #endif typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */ /* Private subobject */ #define MAX_Q_COMPS 4 /* max components I can handle */ typedef struct { struct jpeg_color_quantizer pub; /* public fields */ /* Initially allocated colormap is saved here */ JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ int sv_actual; /* number of entries in use */ JSAMPARRAY colorindex; /* Precomputed mapping for speed */ /* colorindex[i][j] = index of color closest to pixel value j in component i, * premultiplied as described above. Since colormap indexes must fit into * JSAMPLEs, the entries of this array will too. */ boolean is_padded; /* is the colorindex padded for odither? */ int Ncolors[MAX_Q_COMPS]; /* # of values alloced to each component */ /* Variables for ordered dithering */ int row_index; /* cur row's vertical index in dither matrix */ ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */ /* Variables for Floyd-Steinberg dithering */ FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */ boolean on_odd_row; /* flag to remember which row we are on */ } my_cquantizer; typedef my_cquantizer * my_cquantize_ptr; /* * Policy-making subroutines for create_colormap and create_colorindex. * These routines determine the colormap to be used. The rest of the module * only assumes that the colormap is orthogonal. * * * select_ncolors decides how to divvy up the available colors * among the components. * * output_value defines the set of representative values for a component. * * largest_input_value defines the mapping from input values to * representative values for a component. * Note that the latter two routines may impose different policies for * different components, though this is not currently done. */ LOCAL(int) select_ncolors (j_decompress_ptr cinfo, int Ncolors[]) /* Determine allocation of desired colors to components, */ /* and fill in Ncolors[] array to indicate choice. */ /* Return value is total number of colors (product of Ncolors[] values). */ { int nc = cinfo->out_color_components; /* number of color components */ int max_colors = cinfo->desired_number_of_colors; int total_colors, iroot, i, j; boolean changed; long temp; int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE }; RGB_order[0] = rgb_green[cinfo->out_color_space]; RGB_order[1] = rgb_red[cinfo->out_color_space]; RGB_order[2] = rgb_blue[cinfo->out_color_space]; /* We can allocate at least the nc'th root of max_colors per component. */ /* Compute floor(nc'th root of max_colors). */ iroot = 1; do { iroot++; temp = iroot; /* set temp = iroot ** nc */ for (i = 1; i < nc; i++) temp *= iroot; } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */ iroot--; /* now iroot = floor(root) */ /* Must have at least 2 color values per component */ if (iroot < 2) ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp); /* Initialize to iroot color values for each component */ total_colors = 1; for (i = 0; i < nc; i++) { Ncolors[i] = iroot; total_colors *= iroot; } /* We may be able to increment the count for one or more components without * exceeding max_colors, though we know not all can be incremented. * Sometimes, the first component can be incremented more than once! * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.) * In RGB colorspace, try to increment G first, then R, then B. */ do { changed = FALSE; for (i = 0; i < nc; i++) { j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); /* calculate new total_colors if Ncolors[j] is incremented */ temp = total_colors / Ncolors[j]; temp *= Ncolors[j]+1; /* done in long arith to avoid oflo */ if (temp > (long) max_colors) break; /* won't fit, done with this pass */ Ncolors[j]++; /* OK, apply the increment */ total_colors = (int) temp; changed = TRUE; } } while (changed); return total_colors; } LOCAL(int) output_value (j_decompress_ptr cinfo, int ci, int j, int maxj) /* Return j'th output value, where j will range from 0 to maxj */ /* The output values must fall in 0..MAXJSAMPLE in increasing order */ { /* We always provide values 0 and MAXJSAMPLE for each component; * any additional values are equally spaced between these limits. * (Forcing the upper and lower values to the limits ensures that * dithering can't produce a color outside the selected gamut.) */ return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj); } LOCAL(int) largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj) /* Return largest input value that should map to j'th output value */ /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */ { /* Breakpoints are halfway between values returned by output_value */ return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj)); } /* * Create the colormap. */ LOCAL(void) create_colormap (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; JSAMPARRAY colormap; /* Created colormap */ int total_colors; /* Number of distinct output colors */ int i,j,k, nci, blksize, blkdist, ptr, val; /* Select number of colors for each component */ total_colors = select_ncolors(cinfo, cquantize->Ncolors); /* Report selected color counts */ if (cinfo->out_color_components == 3) TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS, total_colors, cquantize->Ncolors[0], cquantize->Ncolors[1], cquantize->Ncolors[2]); else TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors); /* Allocate and fill in the colormap. */ /* The colors are ordered in the map in standard row-major order, */ /* i.e. rightmost (highest-indexed) color changes most rapidly. */ colormap = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components); /* blksize is number of adjacent repeated entries for a component */ /* blkdist is distance between groups of identical entries for a component */ blkdist = total_colors; for (i = 0; i < cinfo->out_color_components; i++) { /* fill in colormap entries for i'th color component */ nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ blksize = blkdist / nci; for (j = 0; j < nci; j++) { /* Compute j'th output value (out of nci) for component */ val = output_value(cinfo, i, j, nci-1); /* Fill in all colormap entries that have this value of this component */ for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { /* fill in blksize entries beginning at ptr */ for (k = 0; k < blksize; k++) colormap[i][ptr+k] = (JSAMPLE) val; } } blkdist = blksize; /* blksize of this color is blkdist of next */ } /* Save the colormap in private storage, * where it will survive color quantization mode changes. */ cquantize->sv_colormap = colormap; cquantize->sv_actual = total_colors; } /* * Create the color index table. */ LOCAL(void) create_colorindex (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; JSAMPROW indexptr; int i,j,k, nci, blksize, val, pad; /* For ordered dither, we pad the color index tables by MAXJSAMPLE in * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE). * This is not necessary in the other dithering modes. However, we * flag whether it was done in case user changes dithering mode. */ if (cinfo->dither_mode == JDITHER_ORDERED) { pad = MAXJSAMPLE*2; cquantize->is_padded = TRUE; } else { pad = 0; cquantize->is_padded = FALSE; } cquantize->colorindex = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) (MAXJSAMPLE+1 + pad), (JDIMENSION) cinfo->out_color_components); /* blksize is number of adjacent repeated entries for a component */ blksize = cquantize->sv_actual; for (i = 0; i < cinfo->out_color_components; i++) { /* fill in colorindex entries for i'th color component */ nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ blksize = blksize / nci; /* adjust colorindex pointers to provide padding at negative indexes. */ if (pad) cquantize->colorindex[i] += MAXJSAMPLE; /* in loop, val = index of current output value, */ /* and k = largest j that maps to current val */ indexptr = cquantize->colorindex[i]; val = 0; k = largest_input_value(cinfo, i, 0, nci-1); for (j = 0; j <= MAXJSAMPLE; j++) { while (j > k) /* advance val if past boundary */ k = largest_input_value(cinfo, i, ++val, nci-1); /* premultiply so that no multiplication needed in main processing */ indexptr[j] = (JSAMPLE) (val * blksize); } /* Pad at both ends if necessary */ if (pad) for (j = 1; j <= MAXJSAMPLE; j++) { indexptr[-j] = indexptr[0]; indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE]; } } } /* * Create an ordered-dither array for a component having ncolors * distinct output values. */ LOCAL(ODITHER_MATRIX_PTR) make_odither_array (j_decompress_ptr cinfo, int ncolors) { ODITHER_MATRIX_PTR odither; int j,k; INT32 num,den; odither = (ODITHER_MATRIX_PTR) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(ODITHER_MATRIX)); /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1). * Hence the dither value for the matrix cell with fill order f * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1). * On 16-bit-int machine, be careful to avoid overflow. */ den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1)); for (j = 0; j < ODITHER_SIZE; j++) { for (k = 0; k < ODITHER_SIZE; k++) { num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k]))) * MAXJSAMPLE; /* Ensure round towards zero despite C's lack of consistency * about rounding negative values in integer division... */ odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den); } } return odither; } /* * Create the ordered-dither tables. * Components having the same number of representative colors may * share a dither table. */ LOCAL(void) create_odither_tables (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; ODITHER_MATRIX_PTR odither; int i, j, nci; for (i = 0; i < cinfo->out_color_components; i++) { nci = cquantize->Ncolors[i]; /* # of distinct values for this color */ odither = NULL; /* search for matching prior component */ for (j = 0; j < i; j++) { if (nci == cquantize->Ncolors[j]) { odither = cquantize->odither[j]; break; } } if (odither == NULL) /* need a new table? */ odither = make_odither_array(cinfo, nci); cquantize->odither[i] = odither; } } /* * Map some rows of pixels to the output colormapped representation. */ METHODDEF(void) color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* General case, no dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; JSAMPARRAY colorindex = cquantize->colorindex; register int pixcode, ci; register JSAMPROW ptrin, ptrout; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; register int nc = cinfo->out_color_components; for (row = 0; row < num_rows; row++) { ptrin = input_buf[row]; ptrout = output_buf[row]; for (col = width; col > 0; col--) { pixcode = 0; for (ci = 0; ci < nc; ci++) { pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]); } *ptrout++ = (JSAMPLE) pixcode; } } } METHODDEF(void) color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* Fast path for out_color_components==3, no dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; register int pixcode; register JSAMPROW ptrin, ptrout; JSAMPROW colorindex0 = cquantize->colorindex[0]; JSAMPROW colorindex1 = cquantize->colorindex[1]; JSAMPROW colorindex2 = cquantize->colorindex[2]; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; for (row = 0; row < num_rows; row++) { ptrin = input_buf[row]; ptrout = output_buf[row]; for (col = width; col > 0; col--) { pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]); pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]); pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]); *ptrout++ = (JSAMPLE) pixcode; } } } METHODDEF(void) quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* General case, with ordered dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; register JSAMPROW input_ptr; register JSAMPROW output_ptr; JSAMPROW colorindex_ci; int * dither; /* points to active row of dither matrix */ int row_index, col_index; /* current indexes into dither matrix */ int nc = cinfo->out_color_components; int ci; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; for (row = 0; row < num_rows; row++) { /* Initialize output values to 0 so can process components separately */ jzero_far((void FAR *) output_buf[row], (size_t) (width * SIZEOF(JSAMPLE))); row_index = cquantize->row_index; for (ci = 0; ci < nc; ci++) { input_ptr = input_buf[row] + ci; output_ptr = output_buf[row]; colorindex_ci = cquantize->colorindex[ci]; dither = cquantize->odither[ci][row_index]; col_index = 0; for (col = width; col > 0; col--) { /* Form pixel value + dither, range-limit to 0..MAXJSAMPLE, * select output value, accumulate into output code for this pixel. * Range-limiting need not be done explicitly, as we have extended * the colorindex table to produce the right answers for out-of-range * inputs. The maximum dither is +- MAXJSAMPLE; this sets the * required amount of padding. */ *output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]]; input_ptr += nc; output_ptr++; col_index = (col_index + 1) & ODITHER_MASK; } } /* Advance row index for next row */ row_index = (row_index + 1) & ODITHER_MASK; cquantize->row_index = row_index; } } METHODDEF(void) quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* Fast path for out_color_components==3, with ordered dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; register int pixcode; register JSAMPROW input_ptr; register JSAMPROW output_ptr; JSAMPROW colorindex0 = cquantize->colorindex[0]; JSAMPROW colorindex1 = cquantize->colorindex[1]; JSAMPROW colorindex2 = cquantize->colorindex[2]; int * dither0; /* points to active row of dither matrix */ int * dither1; int * dither2; int row_index, col_index; /* current indexes into dither matrix */ int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; for (row = 0; row < num_rows; row++) { row_index = cquantize->row_index; input_ptr = input_buf[row]; output_ptr = output_buf[row]; dither0 = cquantize->odither[0][row_index]; dither1 = cquantize->odither[1][row_index]; dither2 = cquantize->odither[2][row_index]; col_index = 0; for (col = width; col > 0; col--) { pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) + dither0[col_index]]); pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) + dither1[col_index]]); pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) + dither2[col_index]]); *output_ptr++ = (JSAMPLE) pixcode; col_index = (col_index + 1) & ODITHER_MASK; } row_index = (row_index + 1) & ODITHER_MASK; cquantize->row_index = row_index; } } METHODDEF(void) quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* General case, with Floyd-Steinberg dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; register LOCFSERROR cur; /* current error or pixel value */ LOCFSERROR belowerr; /* error for pixel below cur */ LOCFSERROR bpreverr; /* error for below/prev col */ LOCFSERROR bnexterr; /* error for below/next col */ LOCFSERROR delta; register FSERRPTR errorptr; /* => fserrors[] at column before current */ register JSAMPROW input_ptr; register JSAMPROW output_ptr; JSAMPROW colorindex_ci; JSAMPROW colormap_ci; int pixcode; int nc = cinfo->out_color_components; int dir; /* 1 for left-to-right, -1 for right-to-left */ int dirnc; /* dir * nc */ int ci; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; JSAMPLE *range_limit = cinfo->sample_range_limit; SHIFT_TEMPS for (row = 0; row < num_rows; row++) { /* Initialize output values to 0 so can process components separately */ jzero_far((void FAR *) output_buf[row], (size_t) (width * SIZEOF(JSAMPLE))); for (ci = 0; ci < nc; ci++) { input_ptr = input_buf[row] + ci; output_ptr = output_buf[row]; if (cquantize->on_odd_row) { /* work right to left in this row */ input_ptr += (width-1) * nc; /* so point to rightmost pixel */ output_ptr += width-1; dir = -1; dirnc = -nc; errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */ } else { /* work left to right in this row */ dir = 1; dirnc = nc; errorptr = cquantize->fserrors[ci]; /* => entry before first column */ } colorindex_ci = cquantize->colorindex[ci]; colormap_ci = cquantize->sv_colormap[ci]; /* Preset error values: no error propagated to first pixel from left */ cur = 0; /* and no error propagated to row below yet */ belowerr = bpreverr = 0; for (col = width; col > 0; col--) { /* cur holds the error propagated from the previous pixel on the * current line. Add the error propagated from the previous line * to form the complete error correction term for this pixel, and * round the error term (which is expressed * 16) to an integer. * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct * for either sign of the error value. * Note: errorptr points to *previous* column's array entry. */ cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. * The maximum error is +- MAXJSAMPLE; this sets the required size * of the range_limit array. */ cur += GETJSAMPLE(*input_ptr); cur = GETJSAMPLE(range_limit[cur]); /* Select output value, accumulate into output code for this pixel */ pixcode = GETJSAMPLE(colorindex_ci[cur]); *output_ptr += (JSAMPLE) pixcode; /* Compute actual representation error at this pixel */ /* Note: we can do this even though we don't have the final */ /* pixel code, because the colormap is orthogonal. */ cur -= GETJSAMPLE(colormap_ci[pixcode]); /* Compute error fractions to be propagated to adjacent pixels. * Add these into the running sums, and simultaneously shift the * next-line error sums left by 1 column. */ bnexterr = cur; delta = cur * 2; cur += delta; /* form error * 3 */ errorptr[0] = (FSERROR) (bpreverr + cur); cur += delta; /* form error * 5 */ bpreverr = belowerr + cur; belowerr = bnexterr; cur += delta; /* form error * 7 */ /* At this point cur contains the 7/16 error value to be propagated * to the next pixel on the current line, and all the errors for the * next line have been shifted over. We are therefore ready to move on. */ input_ptr += dirnc; /* advance input ptr to next column */ output_ptr += dir; /* advance output ptr to next column */ errorptr += dir; /* advance errorptr to current column */ } /* Post-loop cleanup: we must unload the final error value into the * final fserrors[] entry. Note we need not unload belowerr because * it is for the dummy column before or after the actual array. */ errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */ } cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE); } } /* * Allocate workspace for Floyd-Steinberg errors. */ LOCAL(void) alloc_fs_workspace (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; size_t arraysize; int i; arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); for (i = 0; i < cinfo->out_color_components; i++) { cquantize->fserrors[i] = (FSERRPTR) (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); } } /* * Initialize for one-pass color quantization. */ METHODDEF(void) start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; size_t arraysize; int i; /* Install my colormap. */ cinfo->colormap = cquantize->sv_colormap; cinfo->actual_number_of_colors = cquantize->sv_actual; /* Initialize for desired dithering mode. */ switch (cinfo->dither_mode) { case JDITHER_NONE: if (cinfo->out_color_components == 3) cquantize->pub.color_quantize = color_quantize3; else cquantize->pub.color_quantize = color_quantize; break; case JDITHER_ORDERED: if (cinfo->out_color_components == 3) cquantize->pub.color_quantize = quantize3_ord_dither; else cquantize->pub.color_quantize = quantize_ord_dither; cquantize->row_index = 0; /* initialize state for ordered dither */ /* If user changed to ordered dither from another mode, * we must recreate the color index table with padding. * This will cost extra space, but probably isn't very likely. */ if (! cquantize->is_padded) create_colorindex(cinfo); /* Create ordered-dither tables if we didn't already. */ if (cquantize->odither[0] == NULL) create_odither_tables(cinfo); break; case JDITHER_FS: cquantize->pub.color_quantize = quantize_fs_dither; cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */ /* Allocate Floyd-Steinberg workspace if didn't already. */ if (cquantize->fserrors[0] == NULL) alloc_fs_workspace(cinfo); /* Initialize the propagated errors to zero. */ arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR)); for (i = 0; i < cinfo->out_color_components; i++) jzero_far((void FAR *) cquantize->fserrors[i], arraysize); break; default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } } /* * Finish up at the end of the pass. */ METHODDEF(void) finish_pass_1_quant (j_decompress_ptr cinfo) { /* no work in 1-pass case */ } /* * Switch to a new external colormap between output passes. * Shouldn't get to this module! */ METHODDEF(void) new_color_map_1_quant (j_decompress_ptr cinfo) { ERREXIT(cinfo, JERR_MODE_CHANGE); } /* * Module initialization routine for 1-pass color quantization. */ GLOBAL(void) jinit_1pass_quantizer (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize; cquantize = (my_cquantize_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_cquantizer)); cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; cquantize->pub.start_pass = start_pass_1_quant; cquantize->pub.finish_pass = finish_pass_1_quant; cquantize->pub.new_color_map = new_color_map_1_quant; cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */ cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */ /* Make sure my internal arrays won't overflow */ if (cinfo->out_color_components > MAX_Q_COMPS) ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); /* Make sure colormap indexes can be represented by JSAMPLEs */ if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1)) ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1); /* Create the colormap and color index table. */ create_colormap(cinfo); create_colorindex(cinfo); /* Allocate Floyd-Steinberg workspace now if requested. * We do this now since it is FAR storage and may affect the memory * manager's space calculations. If the user changes to FS dither * mode in a later pass, we will allocate the space then, and will * possibly overrun the max_memory_to_use setting. */ if (cinfo->dither_mode == JDITHER_FS) alloc_fs_workspace(cinfo); } #endif /* QUANT_1PASS_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jfdctfst.c0000664000175000017500000001663212013417376020235 0ustar alfalf00000000000000/* * jfdctfst.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a fast, not so accurate integer implementation of the * forward DCT (Discrete Cosine Transform). * * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT * on each column. Direct algorithms are also available, but they are * much more complex and seem not to be any faster when reduced to code. * * This implementation is based on Arai, Agui, and Nakajima's algorithm for * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in * Japanese, but the algorithm is described in the Pennebaker & Mitchell * JPEG textbook (see REFERENCES section in file README). The following code * is based directly on figure 4-8 in P&M. * While an 8-point DCT cannot be done in less than 11 multiplies, it is * possible to arrange the computation so that many of the multiplies are * simple scalings of the final outputs. These multiplies can then be * folded into the multiplications or divisions by the JPEG quantization * table entries. The AA&N method leaves only 5 multiplies and 29 adds * to be done in the DCT itself. * The primary disadvantage of this method is that with fixed-point math, * accuracy is lost due to imprecise representation of the scaled * quantization values. The smaller the quantization table entry, the less * precise the scaled value, so this implementation does worse with high- * quality-setting files than with low-quality ones. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_IFAST_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* Scaling decisions are generally the same as in the LL&M algorithm; * see jfdctint.c for more details. However, we choose to descale * (right shift) multiplication products as soon as they are formed, * rather than carrying additional fractional bits into subsequent additions. * This compromises accuracy slightly, but it lets us save a few shifts. * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) * everywhere except in the multiplications proper; this saves a good deal * of work on 16-bit-int machines. * * Again to save a few shifts, the intermediate results between pass 1 and * pass 2 are not upscaled, but are represented only to integral precision. * * A final compromise is to represent the multiplicative constants to only * 8 fractional bits, rather than 13. This saves some shifting work on some * machines, and may also reduce the cost of multiplication (since there * are fewer one-bits in the constants). */ #define CONST_BITS 8 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus * causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) */ #if CONST_BITS == 8 #define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */ #define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */ #define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */ #define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */ #else #define FIX_0_382683433 FIX(0.382683433) #define FIX_0_541196100 FIX(0.541196100) #define FIX_0_707106781 FIX(0.707106781) #define FIX_1_306562965 FIX(1.306562965) #endif /* We can gain a little more speed, with a further compromise in accuracy, * by omitting the addition in a descaling shift. This yields an incorrectly * rounded result half the time... */ #ifndef USE_ACCURATE_ROUNDING #undef DESCALE #define DESCALE(x,n) RIGHT_SHIFT(x, n) #endif /* Multiply a DCTELEM variable by an INT32 constant, and immediately * descale to yield a DCTELEM result. */ #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) /* * Perform the forward DCT on one block of samples. */ GLOBAL(void) jpeg_fdct_ifast (DCTELEM * data) { DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; DCTELEM tmp10, tmp11, tmp12, tmp13; DCTELEM z1, z2, z3, z4, z5, z11, z13; DCTELEM *dataptr; int ctr; SHIFT_TEMPS /* Pass 1: process rows. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[0] + dataptr[7]; tmp7 = dataptr[0] - dataptr[7]; tmp1 = dataptr[1] + dataptr[6]; tmp6 = dataptr[1] - dataptr[6]; tmp2 = dataptr[2] + dataptr[5]; tmp5 = dataptr[2] - dataptr[5]; tmp3 = dataptr[3] + dataptr[4]; tmp4 = dataptr[3] - dataptr[4]; /* Even part */ tmp10 = tmp0 + tmp3; /* phase 2 */ tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[0] = tmp10 + tmp11; /* phase 3 */ dataptr[4] = tmp10 - tmp11; z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ dataptr[2] = tmp13 + z1; /* phase 5 */ dataptr[6] = tmp13 - z1; /* Odd part */ tmp10 = tmp4 + tmp5; /* phase 2 */ tmp11 = tmp5 + tmp6; tmp12 = tmp6 + tmp7; /* The rotator is modified from fig 4-8 to avoid extra negations. */ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ z11 = tmp7 + z3; /* phase 5 */ z13 = tmp7 - z3; dataptr[5] = z13 + z2; /* phase 6 */ dataptr[3] = z13 - z2; dataptr[1] = z11 + z4; dataptr[7] = z11 - z4; dataptr += DCTSIZE; /* advance pointer to next row */ } /* Pass 2: process columns. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; /* Even part */ tmp10 = tmp0 + tmp3; /* phase 2 */ tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ dataptr[DCTSIZE*4] = tmp10 - tmp11; z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ dataptr[DCTSIZE*6] = tmp13 - z1; /* Odd part */ tmp10 = tmp4 + tmp5; /* phase 2 */ tmp11 = tmp5 + tmp6; tmp12 = tmp6 + tmp7; /* The rotator is modified from fig 4-8 to avoid extra negations. */ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ z11 = tmp7 + z3; /* phase 5 */ z13 = tmp7 - z3; dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ dataptr[DCTSIZE*3] = z13 - z2; dataptr[DCTSIZE*1] = z11 + z4; dataptr[DCTSIZE*7] = z11 - z4; dataptr++; /* advance pointer to next column */ } } #endif /* DCT_IFAST_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jcsample.c0000664000175000017500000004531212013417376020221 0ustar alfalf00000000000000/* * jcsample.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains downsampling routines. * * Downsampling input data is counted in "row groups". A row group * is defined to be max_v_samp_factor pixel rows of each component, * from which the downsampler produces v_samp_factor sample rows. * A single row group is processed in each call to the downsampler module. * * The downsampler is responsible for edge-expansion of its output data * to fill an integral number of DCT blocks horizontally. The source buffer * may be modified if it is helpful for this purpose (the source buffer is * allocated wide enough to correspond to the desired output width). * The caller (the prep controller) is responsible for vertical padding. * * The downsampler may request "context rows" by setting need_context_rows * during startup. In this case, the input arrays will contain at least * one row group's worth of pixels above and below the passed-in data; * the caller will create dummy rows at image top and bottom by replicating * the first or last real pixel row. * * An excellent reference for image resampling is * Digital Image Warping, George Wolberg, 1990. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. * * The downsampling algorithm used here is a simple average of the source * pixels covered by the output pixel. The hi-falutin sampling literature * refers to this as a "box filter". In general the characteristics of a box * filter are not very good, but for the specific cases we normally use (1:1 * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not * nearly so bad. If you intend to use other sampling ratios, you'd be well * advised to improve this code. * * A simple input-smoothing capability is provided. This is mainly intended * for cleaning up color-dithered GIF input files (if you find it inadequate, * we suggest using an external filtering program such as pnmconvol). When * enabled, each input pixel P is replaced by a weighted sum of itself and its * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF, * where SF = (smoothing_factor / 1024). * Currently, smoothing is only supported for 2h2v sampling factors. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jsimd.h" /* Pointer to routine to downsample a single component */ typedef JMETHOD(void, downsample1_ptr, (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)); /* Private subobject */ typedef struct { struct jpeg_downsampler pub; /* public fields */ /* Downsampling method pointers, one per component */ downsample1_ptr methods[MAX_COMPONENTS]; } my_downsampler; typedef my_downsampler * my_downsample_ptr; /* * Initialize for a downsampling pass. */ METHODDEF(void) start_pass_downsample (j_compress_ptr cinfo) { /* no work for now */ } /* * Expand a component horizontally from width input_cols to width output_cols, * by duplicating the rightmost samples. */ LOCAL(void) expand_right_edge (JSAMPARRAY image_data, int num_rows, JDIMENSION input_cols, JDIMENSION output_cols) { register JSAMPROW ptr; register JSAMPLE pixval; register int count; int row; int numcols = (int) (output_cols - input_cols); if (numcols > 0) { for (row = 0; row < num_rows; row++) { ptr = image_data[row] + input_cols; pixval = ptr[-1]; /* don't need GETJSAMPLE() here */ for (count = numcols; count > 0; count--) *ptr++ = pixval; } } } /* * Do downsampling for a whole row group (all components). * * In this version we simply downsample each component independently. */ METHODDEF(void) sep_downsample (j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_index, JSAMPIMAGE output_buf, JDIMENSION out_row_group_index) { my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample; int ci; jpeg_component_info * compptr; JSAMPARRAY in_ptr, out_ptr; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { in_ptr = input_buf[ci] + in_row_index; out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor); (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr); } } /* * Downsample pixel values of a single component. * One row group is processed per call. * This version handles arbitrary integral sampling ratios, without smoothing. * Note that this version is not actually used for customary sampling ratios. */ METHODDEF(void) int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v; JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; JSAMPROW inptr, outptr; INT32 outvalue; h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor; v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor; numpix = h_expand * v_expand; numpix2 = numpix/2; /* Expand input data enough to let all the output samples be generated * by the standard loop. Special-casing padded output would be more * efficient. */ expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width, output_cols * h_expand); inrow = 0; for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { outptr = output_data[outrow]; for (outcol = 0, outcol_h = 0; outcol < output_cols; outcol++, outcol_h += h_expand) { outvalue = 0; for (v = 0; v < v_expand; v++) { inptr = input_data[inrow+v] + outcol_h; for (h = 0; h < h_expand; h++) { outvalue += (INT32) GETJSAMPLE(*inptr++); } } *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix); } inrow += v_expand; } } /* * Downsample pixel values of a single component. * This version handles the special case of a full-size component, * without smoothing. */ METHODDEF(void) fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { /* Copy the data */ jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor, cinfo->image_width); /* Edge-expand */ expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width, compptr->width_in_blocks * DCTSIZE); } /* * Downsample pixel values of a single component. * This version handles the common case of 2:1 horizontal and 1:1 vertical, * without smoothing. * * A note about the "bias" calculations: when rounding fractional values to * integer, we do not want to always round 0.5 up to the next integer. * If we did that, we'd introduce a noticeable bias towards larger values. * Instead, this code is arranged so that 0.5 will be rounded up or down at * alternate pixel locations (a simple ordered dither pattern). */ METHODDEF(void) h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { int outrow; JDIMENSION outcol; JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; register JSAMPROW inptr, outptr; register int bias; /* Expand input data enough to let all the output samples be generated * by the standard loop. Special-casing padded output would be more * efficient. */ expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width, output_cols * 2); for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { outptr = output_data[outrow]; inptr = input_data[outrow]; bias = 0; /* bias = 0,1,0,1,... for successive samples */ for (outcol = 0; outcol < output_cols; outcol++) { *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1]) + bias) >> 1); bias ^= 1; /* 0=>1, 1=>0 */ inptr += 2; } } } /* * Downsample pixel values of a single component. * This version handles the standard case of 2:1 horizontal and 2:1 vertical, * without smoothing. */ METHODDEF(void) h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { int inrow, outrow; JDIMENSION outcol; JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; register JSAMPROW inptr0, inptr1, outptr; register int bias; /* Expand input data enough to let all the output samples be generated * by the standard loop. Special-casing padded output would be more * efficient. */ expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width, output_cols * 2); inrow = 0; for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { outptr = output_data[outrow]; inptr0 = input_data[inrow]; inptr1 = input_data[inrow+1]; bias = 1; /* bias = 1,2,1,2,... for successive samples */ for (outcol = 0; outcol < output_cols; outcol++) { *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]) + bias) >> 2); bias ^= 3; /* 1=>2, 2=>1 */ inptr0 += 2; inptr1 += 2; } inrow += 2; } } #ifdef INPUT_SMOOTHING_SUPPORTED /* * Downsample pixel values of a single component. * This version handles the standard case of 2:1 horizontal and 2:1 vertical, * with smoothing. One row of context is required. */ METHODDEF(void) h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { int inrow, outrow; JDIMENSION colctr; JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr; INT32 membersum, neighsum, memberscale, neighscale; /* Expand input data enough to let all the output samples be generated * by the standard loop. Special-casing padded output would be more * efficient. */ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, cinfo->image_width, output_cols * 2); /* We don't bother to form the individual "smoothed" input pixel values; * we can directly compute the output which is the average of the four * smoothed values. Each of the four member pixels contributes a fraction * (1-8*SF) to its own smoothed image and a fraction SF to each of the three * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final * output. The four corner-adjacent neighbor pixels contribute a fraction * SF to just one smoothed pixel, or SF/4 to the final output; while the * eight edge-adjacent neighbors contribute SF to each of two smoothed * pixels, or SF/2 overall. In order to use integer arithmetic, these * factors are scaled by 2^16 = 65536. * Also recall that SF = smoothing_factor / 1024. */ memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */ neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */ inrow = 0; for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { outptr = output_data[outrow]; inptr0 = input_data[inrow]; inptr1 = input_data[inrow+1]; above_ptr = input_data[inrow-1]; below_ptr = input_data[inrow+2]; /* Special case for first column: pretend column -1 is same as column 0 */ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]); neighsum += neighsum; neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]); membersum = membersum * memberscale + neighsum * neighscale; *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; for (colctr = output_cols - 2; colctr > 0; colctr--) { /* sum of pixels directly mapped to this output element */ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); /* sum of edge-neighbor pixels */ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) + GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]); /* The edge-neighbors count twice as much as corner-neighbors */ neighsum += neighsum; /* Add in the corner-neighbors */ neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) + GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]); /* form final output scaled up by 2^16 */ membersum = membersum * memberscale + neighsum * neighscale; /* round, descale and output it */ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; } /* Special case for last column */ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) + GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]); neighsum += neighsum; neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) + GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]); membersum = membersum * memberscale + neighsum * neighscale; *outptr = (JSAMPLE) ((membersum + 32768) >> 16); inrow += 2; } } /* * Downsample pixel values of a single component. * This version handles the special case of a full-size component, * with smoothing. One row of context is required. */ METHODDEF(void) fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { int outrow; JDIMENSION colctr; JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; register JSAMPROW inptr, above_ptr, below_ptr, outptr; INT32 membersum, neighsum, memberscale, neighscale; int colsum, lastcolsum, nextcolsum; /* Expand input data enough to let all the output samples be generated * by the standard loop. Special-casing padded output would be more * efficient. */ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, cinfo->image_width, output_cols); /* Each of the eight neighbor pixels contributes a fraction SF to the * smoothed pixel, while the main pixel contributes (1-8*SF). In order * to use integer arithmetic, these factors are multiplied by 2^16 = 65536. * Also recall that SF = smoothing_factor / 1024. */ memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */ neighscale = cinfo->smoothing_factor * 64; /* scaled SF */ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { outptr = output_data[outrow]; inptr = input_data[outrow]; above_ptr = input_data[outrow-1]; below_ptr = input_data[outrow+1]; /* Special case for first column */ colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) + GETJSAMPLE(*inptr); membersum = GETJSAMPLE(*inptr++); nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(*inptr); neighsum = colsum + (colsum - membersum) + nextcolsum; membersum = membersum * memberscale + neighsum * neighscale; *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); lastcolsum = colsum; colsum = nextcolsum; for (colctr = output_cols - 2; colctr > 0; colctr--) { membersum = GETJSAMPLE(*inptr++); above_ptr++; below_ptr++; nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + GETJSAMPLE(*inptr); neighsum = lastcolsum + (colsum - membersum) + nextcolsum; membersum = membersum * memberscale + neighsum * neighscale; *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); lastcolsum = colsum; colsum = nextcolsum; } /* Special case for last column */ membersum = GETJSAMPLE(*inptr); neighsum = lastcolsum + (colsum - membersum) + colsum; membersum = membersum * memberscale + neighsum * neighscale; *outptr = (JSAMPLE) ((membersum + 32768) >> 16); } } #endif /* INPUT_SMOOTHING_SUPPORTED */ /* * Module initialization routine for downsampling. * Note that we must select a routine for each component. */ GLOBAL(void) jinit_downsampler (j_compress_ptr cinfo) { my_downsample_ptr downsample; int ci; jpeg_component_info * compptr; boolean smoothok = TRUE; downsample = (my_downsample_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_downsampler)); cinfo->downsample = (struct jpeg_downsampler *) downsample; downsample->pub.start_pass = start_pass_downsample; downsample->pub.downsample = sep_downsample; downsample->pub.need_context_rows = FALSE; if (cinfo->CCIR601_sampling) ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); /* Verify we can handle the sampling factors, and set up method pointers */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (compptr->h_samp_factor == cinfo->max_h_samp_factor && compptr->v_samp_factor == cinfo->max_v_samp_factor) { #ifdef INPUT_SMOOTHING_SUPPORTED if (cinfo->smoothing_factor) { downsample->methods[ci] = fullsize_smooth_downsample; downsample->pub.need_context_rows = TRUE; } else #endif downsample->methods[ci] = fullsize_downsample; } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && compptr->v_samp_factor == cinfo->max_v_samp_factor) { smoothok = FALSE; if (jsimd_can_h2v1_downsample()) downsample->methods[ci] = jsimd_h2v1_downsample; else downsample->methods[ci] = h2v1_downsample; } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) { #ifdef INPUT_SMOOTHING_SUPPORTED if (cinfo->smoothing_factor) { downsample->methods[ci] = h2v2_smooth_downsample; downsample->pub.need_context_rows = TRUE; } else #endif if (jsimd_can_h2v2_downsample()) downsample->methods[ci] = jsimd_h2v2_downsample; else downsample->methods[ci] = h2v2_downsample; } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 && (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) { smoothok = FALSE; downsample->methods[ci] = int_downsample; } else ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); } #ifdef INPUT_SMOOTHING_SUPPORTED if (cinfo->smoothing_factor && !smoothok) TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL); #endif } glmark2-2012.08/./src/libjpeg-turbo/jsimddct.h0000664000175000017500000001075312013417376020232 0ustar alfalf00000000000000/* * jsimddct.h * * Copyright 2009 Pierre Ossman for Cendio AB * * Based on the x86 SIMD extension for IJG JPEG library, * Copyright (C) 1999-2006, MIYASAKA Masaru. * For conditions of distribution and use, see copyright notice in jsimdext.inc * */ /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jsimd_can_convsamp jSCanConv #define jsimd_can_convsamp_float jSCanConvF #define jsimd_convsamp jSConv #define jsimd_convsamp_float jSConvF #define jsimd_can_fdct_islow jSCanFDCTIS #define jsimd_can_fdct_ifast jSCanFDCTIF #define jsimd_can_fdct_float jSCanFDCTFl #define jsimd_fdct_islow jSFDCTIS #define jsimd_fdct_ifast jSFDCTIF #define jsimd_fdct_float jSFDCTFl #define jsimd_can_quantize jSCanQuant #define jsimd_can_quantize_float jSCanQuantF #define jsimd_quantize jSQuant #define jsimd_quantize_float jSQuantF #define jsimd_can_idct_2x2 jSCanIDCT22 #define jsimd_can_idct_4x4 jSCanIDCT44 #define jsimd_idct_2x2 jSIDCT22 #define jsimd_idct_4x4 jSIDCT44 #define jsimd_can_idct_islow jSCanIDCTIS #define jsimd_can_idct_ifast jSCanIDCTIF #define jsimd_can_idct_float jSCanIDCTFl #define jsimd_idct_islow jSIDCTIS #define jsimd_idct_ifast jSIDCTIF #define jsimd_idct_float jSIDCTFl #endif /* NEED_SHORT_EXTERNAL_NAMES */ EXTERN(int) jsimd_can_convsamp JPP((void)); EXTERN(int) jsimd_can_convsamp_float JPP((void)); EXTERN(void) jsimd_convsamp JPP((JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)); EXTERN(void) jsimd_convsamp_float JPP((JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)); EXTERN(int) jsimd_can_fdct_islow JPP((void)); EXTERN(int) jsimd_can_fdct_ifast JPP((void)); EXTERN(int) jsimd_can_fdct_float JPP((void)); EXTERN(void) jsimd_fdct_islow JPP((DCTELEM * data)); EXTERN(void) jsimd_fdct_ifast JPP((DCTELEM * data)); EXTERN(void) jsimd_fdct_float JPP((FAST_FLOAT * data)); EXTERN(int) jsimd_can_quantize JPP((void)); EXTERN(int) jsimd_can_quantize_float JPP((void)); EXTERN(void) jsimd_quantize JPP((JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)); EXTERN(void) jsimd_quantize_float JPP((JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)); EXTERN(int) jsimd_can_idct_2x2 JPP((void)); EXTERN(int) jsimd_can_idct_4x4 JPP((void)); EXTERN(void) jsimd_idct_2x2 JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_4x4 JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(int) jsimd_can_idct_islow JPP((void)); EXTERN(int) jsimd_can_idct_ifast JPP((void)); EXTERN(int) jsimd_can_idct_float JPP((void)); EXTERN(void) jsimd_idct_islow JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_ifast JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_float JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); glmark2-2012.08/./src/libjpeg-turbo/jfdctint.c0000664000175000017500000002547212013417376020235 0ustar alfalf00000000000000/* * jfdctint.c * * Copyright (C) 1991-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a slow-but-accurate integer implementation of the * forward DCT (Discrete Cosine Transform). * * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT * on each column. Direct algorithms are also available, but they are * much more complex and seem not to be any faster when reduced to code. * * This implementation is based on an algorithm described in * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. * The primary algorithm described there uses 11 multiplies and 29 adds. * We use their alternate method with 12 multiplies and 32 adds. * The advantage of this method is that no data path contains more than one * multiplication; this allows a very simple and accurate implementation in * scaled fixed-point arithmetic, with a minimal number of shifts. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_ISLOW_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* * The poop on this scaling stuff is as follows: * * Each 1-D DCT step produces outputs which are a factor of sqrt(N) * larger than the true DCT outputs. The final outputs are therefore * a factor of N larger than desired; since N=8 this can be cured by * a simple right shift at the end of the algorithm. The advantage of * this arrangement is that we save two multiplications per 1-D DCT, * because the y0 and y4 outputs need not be divided by sqrt(N). * In the IJG code, this factor of 8 is removed by the quantization step * (in jcdctmgr.c), NOT in this module. * * We have to do addition and subtraction of the integer inputs, which * is no problem, and multiplication by fractional constants, which is * a problem to do in integer arithmetic. We multiply all the constants * by CONST_SCALE and convert them to integer constants (thus retaining * CONST_BITS bits of precision in the constants). After doing a * multiplication we have to divide the product by CONST_SCALE, with proper * rounding, to produce the correct output. This division can be done * cheaply as a right shift of CONST_BITS bits. We postpone shifting * as long as possible so that partial sums can be added together with * full fractional precision. * * The outputs of the first pass are scaled up by PASS1_BITS bits so that * they are represented to better-than-integral precision. These outputs * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word * with the recommended scaling. (For 12-bit sample data, the intermediate * array is INT32 anyway.) * * To avoid overflow of the 32-bit intermediate results in pass 2, we must * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis * shows that the values given below are the most effective. */ #if BITS_IN_JSAMPLE == 8 #define CONST_BITS 13 #define PASS1_BITS 2 #else #define CONST_BITS 13 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */ #endif /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus * causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) */ #if CONST_BITS == 13 #define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ #define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ #define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ #define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ #define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ #define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ #define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ #define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ #else #define FIX_0_298631336 FIX(0.298631336) #define FIX_0_390180644 FIX(0.390180644) #define FIX_0_541196100 FIX(0.541196100) #define FIX_0_765366865 FIX(0.765366865) #define FIX_0_899976223 FIX(0.899976223) #define FIX_1_175875602 FIX(1.175875602) #define FIX_1_501321110 FIX(1.501321110) #define FIX_1_847759065 FIX(1.847759065) #define FIX_1_961570560 FIX(1.961570560) #define FIX_2_053119869 FIX(2.053119869) #define FIX_2_562915447 FIX(2.562915447) #define FIX_3_072711026 FIX(3.072711026) #endif /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. * For 8-bit samples with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. * For 12-bit samples, a full 32-bit multiplication will be needed. */ #if BITS_IN_JSAMPLE == 8 #define MULTIPLY(var,const) MULTIPLY16C16(var,const) #else #define MULTIPLY(var,const) ((var) * (const)) #endif /* * Perform the forward DCT on one block of samples. */ GLOBAL(void) jpeg_fdct_islow (DCTELEM * data) { INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; INT32 tmp10, tmp11, tmp12, tmp13; INT32 z1, z2, z3, z4, z5; DCTELEM *dataptr; int ctr; SHIFT_TEMPS /* Pass 1: process rows. */ /* Note results are scaled up by sqrt(8) compared to a true DCT; */ /* furthermore, we scale the results by 2**PASS1_BITS. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[0] + dataptr[7]; tmp7 = dataptr[0] - dataptr[7]; tmp1 = dataptr[1] + dataptr[6]; tmp6 = dataptr[1] - dataptr[6]; tmp2 = dataptr[2] + dataptr[5]; tmp5 = dataptr[2] - dataptr[5]; tmp3 = dataptr[3] + dataptr[4]; tmp4 = dataptr[3] - dataptr[4]; /* Even part per LL&M figure 1 --- note that published figure is faulty; * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". */ tmp10 = tmp0 + tmp3; tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS); dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865), CONST_BITS-PASS1_BITS); dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065), CONST_BITS-PASS1_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * cK represents cos(K*pi/16). * i0..i3 in the paper are tmp4..tmp7 here. */ z1 = tmp4 + tmp7; z2 = tmp5 + tmp6; z3 = tmp4 + tmp6; z4 = tmp5 + tmp7; z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS); dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS); dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS); dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS); dataptr += DCTSIZE; /* advance pointer to next row */ } /* Pass 2: process columns. * We remove the PASS1_BITS scaling, but leave the results scaled up * by an overall factor of 8. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; /* Even part per LL&M figure 1 --- note that published figure is faulty; * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". */ tmp10 = tmp0 + tmp3; tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS); z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865), CONST_BITS+PASS1_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065), CONST_BITS+PASS1_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * cK represents cos(K*pi/16). * i0..i3 in the paper are tmp4..tmp7 here. */ z1 = tmp4 + tmp7; z2 = tmp5 + tmp6; z3 = tmp4 + tmp6; z4 = tmp5 + tmp7; z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS+PASS1_BITS); dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS+PASS1_BITS); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS+PASS1_BITS); dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS+PASS1_BITS); dataptr++; /* advance pointer to next column */ } } #endif /* DCT_ISLOW_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jcapimin.c0000664000175000017500000002235412013417376020216 0ustar alfalf00000000000000/* * jcapimin.c * * Copyright (C) 1994-1998, Thomas G. Lane. * Modified 2003-2010 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains application interface code for the compression half * of the JPEG library. These are the "minimum" API routines that may be * needed in either the normal full-compression case or the transcoding-only * case. * * Most of the routines intended to be called directly by an application * are in this file or in jcapistd.c. But also see jcparam.c for * parameter-setup helper routines, jcomapi.c for routines shared by * compression and decompression, and jctrans.c for the transcoding case. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Initialization of a JPEG compression object. * The error manager must already be set up (in case memory manager fails). */ GLOBAL(void) jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize) { int i; /* Guard against version mismatches between library and caller. */ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ if (version != JPEG_LIB_VERSION) ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); if (structsize != SIZEOF(struct jpeg_compress_struct)) ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, (int) SIZEOF(struct jpeg_compress_struct), (int) structsize); /* For debugging purposes, we zero the whole master structure. * But the application has already set the err pointer, and may have set * client_data, so we have to save and restore those fields. * Note: if application hasn't set client_data, tools like Purify may * complain here. */ { struct jpeg_error_mgr * err = cinfo->err; void * client_data = cinfo->client_data; /* ignore Purify complaint here */ MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct)); cinfo->err = err; cinfo->client_data = client_data; } cinfo->is_decompressor = FALSE; /* Initialize a memory manager instance for this object */ jinit_memory_mgr((j_common_ptr) cinfo); /* Zero out pointers to permanent structures. */ cinfo->progress = NULL; cinfo->dest = NULL; cinfo->comp_info = NULL; for (i = 0; i < NUM_QUANT_TBLS; i++) { cinfo->quant_tbl_ptrs[i] = NULL; #if JPEG_LIB_VERSION >= 70 cinfo->q_scale_factor[i] = 100; #endif } for (i = 0; i < NUM_HUFF_TBLS; i++) { cinfo->dc_huff_tbl_ptrs[i] = NULL; cinfo->ac_huff_tbl_ptrs[i] = NULL; } #if JPEG_LIB_VERSION >= 80 /* Must do it here for emit_dqt in case jpeg_write_tables is used */ cinfo->block_size = DCTSIZE; cinfo->natural_order = jpeg_natural_order; cinfo->lim_Se = DCTSIZE2-1; #endif cinfo->script_space = NULL; cinfo->input_gamma = 1.0; /* in case application forgets */ /* OK, I'm ready */ cinfo->global_state = CSTATE_START; } /* * Destruction of a JPEG compression object */ GLOBAL(void) jpeg_destroy_compress (j_compress_ptr cinfo) { jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ } /* * Abort processing of a JPEG compression operation, * but don't destroy the object itself. */ GLOBAL(void) jpeg_abort_compress (j_compress_ptr cinfo) { jpeg_abort((j_common_ptr) cinfo); /* use common routine */ } /* * Forcibly suppress or un-suppress all quantization and Huffman tables. * Marks all currently defined tables as already written (if suppress) * or not written (if !suppress). This will control whether they get emitted * by a subsequent jpeg_start_compress call. * * This routine is exported for use by applications that want to produce * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but * since it is called by jpeg_start_compress, we put it here --- otherwise * jcparam.o would be linked whether the application used it or not. */ GLOBAL(void) jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress) { int i; JQUANT_TBL * qtbl; JHUFF_TBL * htbl; for (i = 0; i < NUM_QUANT_TBLS; i++) { if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL) qtbl->sent_table = suppress; } for (i = 0; i < NUM_HUFF_TBLS; i++) { if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL) htbl->sent_table = suppress; if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL) htbl->sent_table = suppress; } } /* * Finish JPEG compression. * * If a multipass operating mode was selected, this may do a great deal of * work including most of the actual output. */ GLOBAL(void) jpeg_finish_compress (j_compress_ptr cinfo) { JDIMENSION iMCU_row; if (cinfo->global_state == CSTATE_SCANNING || cinfo->global_state == CSTATE_RAW_OK) { /* Terminate first pass */ if (cinfo->next_scanline < cinfo->image_height) ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); (*cinfo->master->finish_pass) (cinfo); } else if (cinfo->global_state != CSTATE_WRCOEFS) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Perform any remaining passes */ while (! cinfo->master->is_last_pass) { (*cinfo->master->prepare_for_pass) (cinfo); for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) iMCU_row; cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* We bypass the main controller and invoke coef controller directly; * all work is being done from the coefficient buffer. */ if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL)) ERREXIT(cinfo, JERR_CANT_SUSPEND); } (*cinfo->master->finish_pass) (cinfo); } /* Write EOI, do final cleanup */ (*cinfo->marker->write_file_trailer) (cinfo); (*cinfo->dest->term_destination) (cinfo); /* We can use jpeg_abort to release memory and reset global_state */ jpeg_abort((j_common_ptr) cinfo); } /* * Write a special marker. * This is only recommended for writing COM or APPn markers. * Must be called after jpeg_start_compress() and before * first call to jpeg_write_scanlines() or jpeg_write_raw_data(). */ GLOBAL(void) jpeg_write_marker (j_compress_ptr cinfo, int marker, const JOCTET *dataptr, unsigned int datalen) { JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val)); if (cinfo->next_scanline != 0 || (cinfo->global_state != CSTATE_SCANNING && cinfo->global_state != CSTATE_RAW_OK && cinfo->global_state != CSTATE_WRCOEFS)) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */ while (datalen--) { (*write_marker_byte) (cinfo, *dataptr); dataptr++; } } /* Same, but piecemeal. */ GLOBAL(void) jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen) { if (cinfo->next_scanline != 0 || (cinfo->global_state != CSTATE_SCANNING && cinfo->global_state != CSTATE_RAW_OK && cinfo->global_state != CSTATE_WRCOEFS)) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); } GLOBAL(void) jpeg_write_m_byte (j_compress_ptr cinfo, int val) { (*cinfo->marker->write_marker_byte) (cinfo, val); } /* * Alternate compression function: just write an abbreviated table file. * Before calling this, all parameters and a data destination must be set up. * * To produce a pair of files containing abbreviated tables and abbreviated * image data, one would proceed as follows: * * initialize JPEG object * set JPEG parameters * set destination to table file * jpeg_write_tables(cinfo); * set destination to image file * jpeg_start_compress(cinfo, FALSE); * write data... * jpeg_finish_compress(cinfo); * * jpeg_write_tables has the side effect of marking all tables written * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress * will not re-emit the tables unless it is passed write_all_tables=TRUE. */ GLOBAL(void) jpeg_write_tables (j_compress_ptr cinfo) { if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* (Re)initialize error mgr and destination modules */ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); (*cinfo->dest->init_destination) (cinfo); /* Initialize the marker writer ... bit of a crock to do it here. */ jinit_marker_writer(cinfo); /* Write them tables! */ (*cinfo->marker->write_tables_only) (cinfo); /* And clean up. */ (*cinfo->dest->term_destination) (cinfo); /* * In library releases up through v6a, we called jpeg_abort() here to free * any working memory allocated by the destination manager and marker * writer. Some applications had a problem with that: they allocated space * of their own from the library memory manager, and didn't want it to go * away during write_tables. So now we do nothing. This will cause a * memory leak if an app calls write_tables repeatedly without doing a full * compression cycle or otherwise resetting the JPEG object. However, that * seems less bad than unexpectedly freeing memory in the normal case. * An app that prefers the old behavior can call jpeg_abort for itself after * each call to jpeg_write_tables(). */ } glmark2-2012.08/./src/libjpeg-turbo/config.h0000664000175000017500000000725212013417376017676 0ustar alfalf00000000000000/* config.h. Generated from config.h.in by configure. */ /* config.h.in. Generated from configure.ac by autoheader. */ /* Build number */ #define BUILD "20120626" /* Support arithmetic encoding */ #define C_ARITH_CODING_SUPPORTED 1 /* Support arithmetic decoding */ #define D_ARITH_CODING_SUPPORTED 1 /* Define to 1 if you have the header file. */ #define HAVE_DLFCN_H 1 /* Define to 1 if you have the header file. */ #define HAVE_INTTYPES_H 1 /* Define to 1 if you have the header file. */ /* #undef HAVE_JNI_H */ /* Define to 1 if you have the `memcpy' function. */ #define HAVE_MEMCPY 1 /* Define to 1 if you have the header file. */ #define HAVE_MEMORY_H 1 /* Define to 1 if you have the `memset' function. */ #define HAVE_MEMSET 1 /* Define if your compiler supports prototypes */ #define HAVE_PROTOTYPES 1 /* Define to 1 if you have the header file. */ #define HAVE_STDDEF_H 1 /* Define to 1 if you have the header file. */ #define HAVE_STDINT_H 1 /* Define to 1 if you have the header file. */ #define HAVE_STDLIB_H 1 /* Define to 1 if you have the header file. */ #define HAVE_STRINGS_H 1 /* Define to 1 if you have the header file. */ #define HAVE_STRING_H 1 /* Define to 1 if you have the header file. */ #define HAVE_SYS_STAT_H 1 /* Define to 1 if you have the header file. */ #define HAVE_SYS_TYPES_H 1 /* Define to 1 if you have the header file. */ #define HAVE_UNISTD_H 1 /* Define to 1 if the system has the type `unsigned char'. */ #define HAVE_UNSIGNED_CHAR 1 /* Define to 1 if the system has the type `unsigned short'. */ #define HAVE_UNSIGNED_SHORT 1 /* Compiler does not support pointers to undefined structures. */ /* #undef INCOMPLETE_TYPES_BROKEN */ /* How to obtain function inlining. */ #define INLINE __attribute__((always_inline)) /* libjpeg API version */ #define JPEG_LIB_VERSION 62 /* libjpeg-turbo version */ #define LIBJPEG_TURBO_VERSION 1.2.0 /* Define to the sub-directory in which libtool stores uninstalled libraries. */ #define LT_OBJDIR ".libs/" /* Define if you have BSD-like bzero and bcopy */ /* #undef NEED_BSD_STRINGS */ /* Define if you need short function names */ /* #undef NEED_SHORT_EXTERNAL_NAMES */ /* Define if you have sys/types.h */ #define NEED_SYS_TYPES_H 1 /* Name of package */ #define PACKAGE "libjpeg-turbo" /* Define to the address where bug reports for this package should be sent. */ #define PACKAGE_BUGREPORT "" /* Define to the full name of this package. */ #define PACKAGE_NAME "libjpeg-turbo" /* Define to the full name and version of this package. */ #define PACKAGE_STRING "libjpeg-turbo 1.2.0" /* Define to the one symbol short name of this package. */ #define PACKAGE_TARNAME "libjpeg-turbo" /* Define to the home page for this package. */ #define PACKAGE_URL "" /* Define to the version of this package. */ #define PACKAGE_VERSION "1.2.0" /* Define if shift is unsigned */ /* #undef RIGHT_SHIFT_IS_UNSIGNED */ /* Define to 1 if you have the ANSI C header files. */ #define STDC_HEADERS 1 /* Version number of package */ #define VERSION "1.2.0" /* Use accelerated SIMD routines. */ #define WITH_SIMD 1 /* Define to 1 if type `char' is unsigned and you are not using gcc. */ #ifndef __CHAR_UNSIGNED__ /* # undef __CHAR_UNSIGNED__ */ #endif /* Define to empty if `const' does not conform to ANSI C. */ /* #undef const */ /* Define to `__inline__' or `__inline' if that's what the C compiler calls it, or to nothing if 'inline' is not supported under any name. */ #ifndef __cplusplus /* #undef inline */ #endif /* Define to `unsigned int' if does not define. */ /* #undef size_t */ glmark2-2012.08/./src/libjpeg-turbo/jccolor.c0000664000175000017500000004540712013417376020063 0ustar alfalf00000000000000/* * jccolor.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains input colorspace conversion routines. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jsimd.h" #include "config.h" /* Private subobject */ typedef struct { struct jpeg_color_converter pub; /* public fields */ /* Private state for RGB->YCC conversion */ INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ } my_color_converter; typedef my_color_converter * my_cconvert_ptr; /**************** RGB -> YCbCr conversion: most common case **************/ /* * YCbCr is defined per CCIR 601-1, except that Cb and Cr are * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. * The conversion equations to be implemented are therefore * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) * were not represented exactly. Now we sacrifice exact representation of * maximum red and maximum blue in order to get exact grayscales. * * To avoid floating-point arithmetic, we represent the fractional constants * as integers scaled up by 2^16 (about 4 digits precision); we have to divide * the products by 2^16, with appropriate rounding, to get the correct answer. * * For even more speed, we avoid doing any multiplications in the inner loop * by precalculating the constants times R,G,B for all possible values. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); * for 12-bit samples it is still acceptable. It's not very reasonable for * 16-bit samples, but if you want lossless storage you shouldn't be changing * colorspace anyway. * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included * in the tables to save adding them separately in the inner loop. */ #define SCALEBITS 16 /* speediest right-shift on some machines */ #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) #define FIX(x) ((INT32) ((x) * (1L< Y section */ #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ #define R_CB_OFF (3*(MAXJSAMPLE+1)) #define G_CB_OFF (4*(MAXJSAMPLE+1)) #define B_CB_OFF (5*(MAXJSAMPLE+1)) #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ #define G_CR_OFF (6*(MAXJSAMPLE+1)) #define B_CR_OFF (7*(MAXJSAMPLE+1)) #define TABLE_SIZE (8*(MAXJSAMPLE+1)) /* Include inline routines for colorspace extensions */ #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #define RGB_RED EXT_RGB_RED #define RGB_GREEN EXT_RGB_GREEN #define RGB_BLUE EXT_RGB_BLUE #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE #define rgb_ycc_convert_internal extrgb_ycc_convert_internal #define rgb_gray_convert_internal extrgb_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal #define RGB_RED EXT_RGBX_RED #define RGB_GREEN EXT_RGBX_GREEN #define RGB_BLUE EXT_RGBX_BLUE #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal #define rgb_gray_convert_internal extrgbx_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal #define RGB_RED EXT_BGR_RED #define RGB_GREEN EXT_BGR_GREEN #define RGB_BLUE EXT_BGR_BLUE #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE #define rgb_ycc_convert_internal extbgr_ycc_convert_internal #define rgb_gray_convert_internal extbgr_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal #define RGB_RED EXT_BGRX_RED #define RGB_GREEN EXT_BGRX_GREEN #define RGB_BLUE EXT_BGRX_BLUE #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal #define rgb_gray_convert_internal extbgrx_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal #define RGB_RED EXT_XBGR_RED #define RGB_GREEN EXT_XBGR_GREEN #define RGB_BLUE EXT_XBGR_BLUE #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal #define rgb_gray_convert_internal extxbgr_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal #define RGB_RED EXT_XRGB_RED #define RGB_GREEN EXT_XRGB_GREEN #define RGB_BLUE EXT_XRGB_BLUE #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal #define rgb_gray_convert_internal extxrgb_gray_convert_internal #include "jccolext.c.inc" #undef RGB_RED #undef RGB_GREEN #undef RGB_BLUE #undef RGB_PIXELSIZE #undef rgb_ycc_convert_internal #undef rgb_gray_convert_internal /* * Initialize for RGB->YCC colorspace conversion. */ METHODDEF(void) rgb_ycc_start (j_compress_ptr cinfo) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; INT32 * rgb_ycc_tab; INT32 i; /* Allocate and fill in the conversion tables. */ cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (TABLE_SIZE * SIZEOF(INT32))); for (i = 0; i <= MAXJSAMPLE; i++) { rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. * This ensures that the maximum output will round to MAXJSAMPLE * not MAXJSAMPLE+1, and thus that we don't have to range-limit. */ rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; /* B=>Cb and R=>Cr tables are the same rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; */ rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; } } /* * Convert some rows of samples to the JPEG colorspace. */ METHODDEF(void) rgb_ycc_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { switch (cinfo->in_color_space) { case JCS_EXT_RGB: extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_BGR: extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; default: rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; } } /**************** Cases other than RGB -> YCbCr **************/ /* * Convert some rows of samples to the JPEG colorspace. */ METHODDEF(void) rgb_gray_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { switch (cinfo->in_color_space) { case JCS_EXT_RGB: extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_BGR: extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; default: rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, num_rows); break; } } /* * Convert some rows of samples to the JPEG colorspace. * This version handles Adobe-style CMYK->YCCK conversion, * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same * conversion as above, while passing K (black) unchanged. * We assume rgb_ycc_start has been called. */ METHODDEF(void) cmyk_ycck_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; register int r, g, b; register INT32 * ctab = cconvert->rgb_ycc_tab; register JSAMPROW inptr; register JSAMPROW outptr0, outptr1, outptr2, outptr3; register JDIMENSION col; JDIMENSION num_cols = cinfo->image_width; while (--num_rows >= 0) { inptr = *input_buf++; outptr0 = output_buf[0][output_row]; outptr1 = output_buf[1][output_row]; outptr2 = output_buf[2][output_row]; outptr3 = output_buf[3][output_row]; output_row++; for (col = 0; col < num_cols; col++) { r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); /* K passes through as-is */ outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ inptr += 4; /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations * must be too; we do not need an explicit range-limiting operation. * Hence the value being shifted is never negative, and we don't * need the general RIGHT_SHIFT macro. */ /* Y */ outptr0[col] = (JSAMPLE) ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) >> SCALEBITS); /* Cb */ outptr1[col] = (JSAMPLE) ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) >> SCALEBITS); /* Cr */ outptr2[col] = (JSAMPLE) ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) >> SCALEBITS); } } } /* * Convert some rows of samples to the JPEG colorspace. * This version handles grayscale output with no conversion. * The source can be either plain grayscale or YCbCr (since Y == gray). */ METHODDEF(void) grayscale_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { register JSAMPROW inptr; register JSAMPROW outptr; register JDIMENSION col; JDIMENSION num_cols = cinfo->image_width; int instride = cinfo->input_components; while (--num_rows >= 0) { inptr = *input_buf++; outptr = output_buf[0][output_row]; output_row++; for (col = 0; col < num_cols; col++) { outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ inptr += instride; } } } /* * Convert some rows of samples to the JPEG colorspace. * This version handles multi-component colorspaces without conversion. * We assume input_components == num_components. */ METHODDEF(void) null_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { register JSAMPROW inptr; register JSAMPROW outptr; register JDIMENSION col; register int ci; int nc = cinfo->num_components; JDIMENSION num_cols = cinfo->image_width; while (--num_rows >= 0) { /* It seems fastest to make a separate pass for each component. */ for (ci = 0; ci < nc; ci++) { inptr = *input_buf; outptr = output_buf[ci][output_row]; for (col = 0; col < num_cols; col++) { outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ inptr += nc; } } input_buf++; output_row++; } } /* * Empty method for start_pass. */ METHODDEF(void) null_method (j_compress_ptr cinfo) { /* no work needed */ } /* * Module initialization routine for input colorspace conversion. */ GLOBAL(void) jinit_color_converter (j_compress_ptr cinfo) { my_cconvert_ptr cconvert; cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_converter)); cinfo->cconvert = (struct jpeg_color_converter *) cconvert; /* set start_pass to null method until we find out differently */ cconvert->pub.start_pass = null_method; /* Make sure input_components agrees with in_color_space */ switch (cinfo->in_color_space) { case JCS_GRAYSCALE: if (cinfo->input_components != 1) ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); break; case JCS_RGB: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); break; case JCS_YCbCr: if (cinfo->input_components != 3) ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); break; case JCS_CMYK: case JCS_YCCK: if (cinfo->input_components != 4) ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); break; default: /* JCS_UNKNOWN can be anything */ if (cinfo->input_components < 1) ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); break; } /* Check num_components, set conversion method based on requested space */ switch (cinfo->jpeg_color_space) { case JCS_GRAYSCALE: if (cinfo->num_components != 1) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); if (cinfo->in_color_space == JCS_GRAYSCALE) cconvert->pub.color_convert = grayscale_convert; else if (cinfo->in_color_space == JCS_RGB || cinfo->in_color_space == JCS_EXT_RGB || cinfo->in_color_space == JCS_EXT_RGBX || cinfo->in_color_space == JCS_EXT_BGR || cinfo->in_color_space == JCS_EXT_BGRX || cinfo->in_color_space == JCS_EXT_XBGR || cinfo->in_color_space == JCS_EXT_XRGB || cinfo->in_color_space == JCS_EXT_RGBA || cinfo->in_color_space == JCS_EXT_BGRA || cinfo->in_color_space == JCS_EXT_ABGR || cinfo->in_color_space == JCS_EXT_ARGB) { if (jsimd_can_rgb_gray()) cconvert->pub.color_convert = jsimd_rgb_gray_convert; else { cconvert->pub.start_pass = rgb_ycc_start; cconvert->pub.color_convert = rgb_gray_convert; } } else if (cinfo->in_color_space == JCS_YCbCr) cconvert->pub.color_convert = grayscale_convert; else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_RGB: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: if (cinfo->num_components != 3) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); if (cinfo->in_color_space == cinfo->jpeg_color_space && rgb_pixelsize[cinfo->in_color_space] == 3) cconvert->pub.color_convert = null_convert; else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_YCbCr: if (cinfo->num_components != 3) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); if (cinfo->in_color_space == JCS_RGB || cinfo->in_color_space == JCS_EXT_RGB || cinfo->in_color_space == JCS_EXT_RGBX || cinfo->in_color_space == JCS_EXT_BGR || cinfo->in_color_space == JCS_EXT_BGRX || cinfo->in_color_space == JCS_EXT_XBGR || cinfo->in_color_space == JCS_EXT_XRGB || cinfo->in_color_space == JCS_EXT_RGBA || cinfo->in_color_space == JCS_EXT_BGRA || cinfo->in_color_space == JCS_EXT_ABGR || cinfo->in_color_space == JCS_EXT_ARGB) { if (jsimd_can_rgb_ycc()) cconvert->pub.color_convert = jsimd_rgb_ycc_convert; else { cconvert->pub.start_pass = rgb_ycc_start; cconvert->pub.color_convert = rgb_ycc_convert; } } else if (cinfo->in_color_space == JCS_YCbCr) cconvert->pub.color_convert = null_convert; else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_CMYK: if (cinfo->num_components != 4) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); if (cinfo->in_color_space == JCS_CMYK) cconvert->pub.color_convert = null_convert; else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_YCCK: if (cinfo->num_components != 4) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); if (cinfo->in_color_space == JCS_CMYK) { cconvert->pub.start_pass = rgb_ycc_start; cconvert->pub.color_convert = cmyk_ycck_convert; } else if (cinfo->in_color_space == JCS_YCCK) cconvert->pub.color_convert = null_convert; else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; default: /* allow null conversion of JCS_UNKNOWN */ if (cinfo->jpeg_color_space != cinfo->in_color_space || cinfo->num_components != cinfo->input_components) ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); cconvert->pub.color_convert = null_convert; break; } } glmark2-2012.08/./src/libjpeg-turbo/jcomapi.c0000664000175000017500000000604612013417376020046 0ustar alfalf00000000000000/* * jcomapi.c * * Copyright (C) 1994-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains application interface routines that are used for both * compression and decompression. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Abort processing of a JPEG compression or decompression operation, * but don't destroy the object itself. * * For this, we merely clean up all the nonpermanent memory pools. * Note that temp files (virtual arrays) are not allowed to belong to * the permanent pool, so we will be able to close all temp files here. * Closing a data source or destination, if necessary, is the application's * responsibility. */ GLOBAL(void) jpeg_abort (j_common_ptr cinfo) { int pool; /* Do nothing if called on a not-initialized or destroyed JPEG object. */ if (cinfo->mem == NULL) return; /* Releasing pools in reverse order might help avoid fragmentation * with some (brain-damaged) malloc libraries. */ for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { (*cinfo->mem->free_pool) (cinfo, pool); } /* Reset overall state for possible reuse of object */ if (cinfo->is_decompressor) { cinfo->global_state = DSTATE_START; /* Try to keep application from accessing now-deleted marker list. * A bit kludgy to do it here, but this is the most central place. */ ((j_decompress_ptr) cinfo)->marker_list = NULL; } else { cinfo->global_state = CSTATE_START; } } /* * Destruction of a JPEG object. * * Everything gets deallocated except the master jpeg_compress_struct itself * and the error manager struct. Both of these are supplied by the application * and must be freed, if necessary, by the application. (Often they are on * the stack and so don't need to be freed anyway.) * Closing a data source or destination, if necessary, is the application's * responsibility. */ GLOBAL(void) jpeg_destroy (j_common_ptr cinfo) { /* We need only tell the memory manager to release everything. */ /* NB: mem pointer is NULL if memory mgr failed to initialize. */ if (cinfo->mem != NULL) (*cinfo->mem->self_destruct) (cinfo); cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ cinfo->global_state = 0; /* mark it destroyed */ } /* * Convenience routines for allocating quantization and Huffman tables. * (Would jutils.c be a more reasonable place to put these?) */ GLOBAL(JQUANT_TBL *) jpeg_alloc_quant_table (j_common_ptr cinfo) { JQUANT_TBL *tbl; tbl = (JQUANT_TBL *) (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL)); tbl->sent_table = FALSE; /* make sure this is false in any new table */ return tbl; } GLOBAL(JHUFF_TBL *) jpeg_alloc_huff_table (j_common_ptr cinfo) { JHUFF_TBL *tbl; tbl = (JHUFF_TBL *) (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL)); tbl->sent_table = FALSE; /* make sure this is false in any new table */ return tbl; } glmark2-2012.08/./src/libjpeg-turbo/jidctred.c0000664000175000017500000003233012013417376020207 0ustar alfalf00000000000000/* * jidctred.c * * Copyright (C) 1994-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains inverse-DCT routines that produce reduced-size output: * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block. * * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M) * algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step * with an 8-to-4 step that produces the four averages of two adjacent outputs * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output). * These steps were derived by computing the corresponding values at the end * of the normal LL&M code, then simplifying as much as possible. * * 1x1 is trivial: just take the DC coefficient divided by 8. * * See jidctint.c for additional comments. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef IDCT_SCALING_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* Scaling is the same as in jidctint.c. */ #if BITS_IN_JSAMPLE == 8 #define CONST_BITS 13 #define PASS1_BITS 2 #else #define CONST_BITS 13 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */ #endif /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus * causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) */ #if CONST_BITS == 13 #define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */ #define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */ #define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */ #define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */ #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ #define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */ #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ #define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */ #define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */ #define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */ #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ #define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */ #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ #define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */ #else #define FIX_0_211164243 FIX(0.211164243) #define FIX_0_509795579 FIX(0.509795579) #define FIX_0_601344887 FIX(0.601344887) #define FIX_0_720959822 FIX(0.720959822) #define FIX_0_765366865 FIX(0.765366865) #define FIX_0_850430095 FIX(0.850430095) #define FIX_0_899976223 FIX(0.899976223) #define FIX_1_061594337 FIX(1.061594337) #define FIX_1_272758580 FIX(1.272758580) #define FIX_1_451774981 FIX(1.451774981) #define FIX_1_847759065 FIX(1.847759065) #define FIX_2_172734803 FIX(2.172734803) #define FIX_2_562915447 FIX(2.562915447) #define FIX_3_624509785 FIX(3.624509785) #endif /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. * For 8-bit samples with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. * For 12-bit samples, a full 32-bit multiplication will be needed. */ #if BITS_IN_JSAMPLE == 8 #define MULTIPLY(var,const) MULTIPLY16C16(var,const) #else #define MULTIPLY(var,const) ((var) * (const)) #endif /* Dequantize a coefficient by multiplying it by the multiplier-table * entry; produce an int result. In this module, both inputs and result * are 16 bits or less, so either int or short multiply will work. */ #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval)) /* * Perform dequantization and inverse DCT on one block of coefficients, * producing a reduced-size 4x4 output block. */ GLOBAL(void) jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { INT32 tmp0, tmp2, tmp10, tmp12; INT32 z1, z2, z3, z4; JCOEFPTR inptr; ISLOW_MULT_TYPE * quantptr; int * wsptr; JSAMPROW outptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); int ctr; int workspace[DCTSIZE*4]; /* buffers data between passes */ SHIFT_TEMPS /* Pass 1: process columns from input, store into work array. */ inptr = coef_block; quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; wsptr = workspace; for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { /* Don't bother to process column 4, because second pass won't use it */ if (ctr == DCTSIZE-4) continue; if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero; we need not examine term 4 for 4x4 output */ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; wsptr[DCTSIZE*2] = dcval; wsptr[DCTSIZE*3] = dcval; continue; } /* Even part */ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); tmp0 <<= (CONST_BITS+1); z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865); tmp10 = tmp0 + tmp2; tmp12 = tmp0 - tmp2; /* Odd part */ z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ /* Final output stage */ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1); wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1); wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1); wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1); } /* Pass 2: process 4 rows from work array, store into output array. */ wsptr = workspace; for (ctr = 0; ctr < 4; ctr++) { outptr = output_buf[ctr] + output_col; /* It's not clear whether a zero row test is worthwhile here ... */ #ifndef NO_ZERO_ROW_TEST if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { /* AC terms all zero */ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) & RANGE_MASK]; outptr[0] = dcval; outptr[1] = dcval; outptr[2] = dcval; outptr[3] = dcval; wsptr += DCTSIZE; /* advance pointer to next row */ continue; } #endif /* Even part */ tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1); tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065) + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865); tmp10 = tmp0 + tmp2; tmp12 = tmp0 - tmp2; /* Odd part */ z1 = (INT32) wsptr[7]; z2 = (INT32) wsptr[5]; z3 = (INT32) wsptr[3]; z4 = (INT32) wsptr[1]; tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ /* Final output stage */ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2, CONST_BITS+PASS1_BITS+3+1) & RANGE_MASK]; outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2, CONST_BITS+PASS1_BITS+3+1) & RANGE_MASK]; outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0, CONST_BITS+PASS1_BITS+3+1) & RANGE_MASK]; outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0, CONST_BITS+PASS1_BITS+3+1) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ } } /* * Perform dequantization and inverse DCT on one block of coefficients, * producing a reduced-size 2x2 output block. */ GLOBAL(void) jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { INT32 tmp0, tmp10, z1; JCOEFPTR inptr; ISLOW_MULT_TYPE * quantptr; int * wsptr; JSAMPROW outptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); int ctr; int workspace[DCTSIZE*2]; /* buffers data between passes */ SHIFT_TEMPS /* Pass 1: process columns from input, store into work array. */ inptr = coef_block; quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; wsptr = workspace; for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { /* Don't bother to process columns 2,4,6 */ if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6) continue; if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; continue; } /* Even part */ z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); tmp10 = z1 << (CONST_BITS+2); /* Odd part */ z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */ z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */ z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */ z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ /* Final output stage */ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2); wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2); } /* Pass 2: process 2 rows from work array, store into output array. */ wsptr = workspace; for (ctr = 0; ctr < 2; ctr++) { outptr = output_buf[ctr] + output_col; /* It's not clear whether a zero row test is worthwhile here ... */ #ifndef NO_ZERO_ROW_TEST if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) { /* AC terms all zero */ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) & RANGE_MASK]; outptr[0] = dcval; outptr[1] = dcval; wsptr += DCTSIZE; /* advance pointer to next row */ continue; } #endif /* Even part */ tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2); /* Odd part */ tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */ + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */ + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */ + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ /* Final output stage */ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0, CONST_BITS+PASS1_BITS+3+2) & RANGE_MASK]; outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0, CONST_BITS+PASS1_BITS+3+2) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ } } /* * Perform dequantization and inverse DCT on one block of coefficients, * producing a reduced-size 1x1 output block. */ GLOBAL(void) jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { int dcval; ISLOW_MULT_TYPE * quantptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); SHIFT_TEMPS /* We hardly need an inverse DCT routine for this: just take the * average pixel value, which is one-eighth of the DC coefficient. */ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; dcval = DEQUANTIZE(coef_block[0], quantptr[0]); dcval = (int) DESCALE((INT32) dcval, 3); output_buf[0][output_col] = range_limit[dcval & RANGE_MASK]; } #endif /* IDCT_SCALING_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jinclude.h0000664000175000017500000000626212013417376020226 0ustar alfalf00000000000000/* * jinclude.h * * Copyright (C) 1991-1994, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file exists to provide a single place to fix any problems with * including the wrong system include files. (Common problems are taken * care of by the standard jconfig symbols, but on really weird systems * you may have to edit this file.) * * NOTE: this file is NOT intended to be included by applications using the * JPEG library. Most applications need only include jpeglib.h. */ /* Include auto-config file to find out which system include files we need. */ #include "jconfig.h" /* auto configuration options */ #define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */ /* * We need the NULL macro and size_t typedef. * On an ANSI-conforming system it is sufficient to include . * Otherwise, we get them from or ; we may have to * pull in as well. * Note that the core JPEG library does not require ; * only the default error handler and data source/destination modules do. * But we must pull it in because of the references to FILE in jpeglib.h. * You can remove those references if you want to compile without . */ #ifdef HAVE_STDDEF_H #include #endif #ifdef HAVE_STDLIB_H #include #endif #ifdef NEED_SYS_TYPES_H #include #endif #include /* * We need memory copying and zeroing functions, plus strncpy(). * ANSI and System V implementations declare these in . * BSD doesn't have the mem() functions, but it does have bcopy()/bzero(). * Some systems may declare memset and memcpy in . * * NOTE: we assume the size parameters to these functions are of type size_t. * Change the casts in these macros if not! */ #ifdef NEED_BSD_STRINGS #include #define MEMZERO(target,size) bzero((void *)(target), (size_t)(size)) #define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size)) #else /* not BSD, assume ANSI/SysV string lib */ #include #define MEMZERO(target,size) memset((void *)(target), 0, (size_t)(size)) #define MEMCOPY(dest,src,size) memcpy((void *)(dest), (const void *)(src), (size_t)(size)) #endif /* * In ANSI C, and indeed any rational implementation, size_t is also the * type returned by sizeof(). However, it seems there are some irrational * implementations out there, in which sizeof() returns an int even though * size_t is defined as long or unsigned long. To ensure consistent results * we always use this SIZEOF() macro in place of using sizeof() directly. */ #define SIZEOF(object) ((size_t) sizeof(object)) /* * The modules that use fread() and fwrite() always invoke them through * these macros. On some systems you may need to twiddle the argument casts. * CAUTION: argument order is different from underlying functions! */ #define JFREAD(file,buf,sizeofbuf) \ ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) #define JFWRITE(file,buf,sizeofbuf) \ ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) glmark2-2012.08/./src/libjpeg-turbo/jpeglib.h0000664000175000017500000014203712013417376020046 0ustar alfalf00000000000000/* * jpeglib.h * * Copyright (C) 1991-1998, Thomas G. Lane. * Modified 2002-2009 by Guido Vollbeding. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file defines the application interface for the JPEG library. * Most applications using the library need only include this file, * and perhaps jerror.h if they want to know the exact error codes. */ #ifndef JPEGLIB_H #define JPEGLIB_H /* * First we include the configuration files that record how this * installation of the JPEG library is set up. jconfig.h can be * generated automatically for many systems. jmorecfg.h contains * manual configuration options that most people need not worry about. */ #ifndef JCONFIG_INCLUDED /* in case jinclude.h already did */ #include "jconfig.h" /* widely used configuration options */ #endif #include "jmorecfg.h" /* seldom changed options */ #ifdef __cplusplus #ifndef DONT_USE_EXTERN_C extern "C" { #endif #endif /* Various constants determining the sizes of things. * All of these are specified by the JPEG standard, so don't change them * if you want to be compatible. */ #define DCTSIZE 8 /* The basic DCT block is 8x8 samples */ #define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */ #define NUM_QUANT_TBLS 4 /* Quantization tables are numbered 0..3 */ #define NUM_HUFF_TBLS 4 /* Huffman tables are numbered 0..3 */ #define NUM_ARITH_TBLS 16 /* Arith-coding tables are numbered 0..15 */ #define MAX_COMPS_IN_SCAN 4 /* JPEG limit on # of components in one scan */ #define MAX_SAMP_FACTOR 4 /* JPEG limit on sampling factors */ /* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard; * the PostScript DCT filter can emit files with many more than 10 blocks/MCU. * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU * to handle it. We even let you do this from the jconfig.h file. However, * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe * sometimes emits noncompliant files doesn't mean you should too. */ #define C_MAX_BLOCKS_IN_MCU 10 /* compressor's limit on blocks per MCU */ #ifndef D_MAX_BLOCKS_IN_MCU #define D_MAX_BLOCKS_IN_MCU 10 /* decompressor's limit on blocks per MCU */ #endif /* Data structures for images (arrays of samples and of DCT coefficients). * On 80x86 machines, the image arrays are too big for near pointers, * but the pointer arrays can fit in near memory. */ typedef JSAMPLE FAR *JSAMPROW; /* ptr to one image row of pixel samples. */ typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */ typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */ typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */ typedef JBLOCK FAR *JBLOCKROW; /* pointer to one row of coefficient blocks */ typedef JBLOCKROW *JBLOCKARRAY; /* a 2-D array of coefficient blocks */ typedef JBLOCKARRAY *JBLOCKIMAGE; /* a 3-D array of coefficient blocks */ typedef JCOEF FAR *JCOEFPTR; /* useful in a couple of places */ /* Types for JPEG compression parameters and working tables. */ /* DCT coefficient quantization tables. */ typedef struct { /* This array gives the coefficient quantizers in natural array order * (not the zigzag order in which they are stored in a JPEG DQT marker). * CAUTION: IJG versions prior to v6a kept this array in zigzag order. */ UINT16 quantval[DCTSIZE2]; /* quantization step for each coefficient */ /* This field is used only during compression. It's initialized FALSE when * the table is created, and set TRUE when it's been output to the file. * You could suppress output of a table by setting this to TRUE. * (See jpeg_suppress_tables for an example.) */ boolean sent_table; /* TRUE when table has been output */ } JQUANT_TBL; /* Huffman coding tables. */ typedef struct { /* These two fields directly represent the contents of a JPEG DHT marker */ UINT8 bits[17]; /* bits[k] = # of symbols with codes of */ /* length k bits; bits[0] is unused */ UINT8 huffval[256]; /* The symbols, in order of incr code length */ /* This field is used only during compression. It's initialized FALSE when * the table is created, and set TRUE when it's been output to the file. * You could suppress output of a table by setting this to TRUE. * (See jpeg_suppress_tables for an example.) */ boolean sent_table; /* TRUE when table has been output */ } JHUFF_TBL; /* Basic info about one component (color channel). */ typedef struct { /* These values are fixed over the whole image. */ /* For compression, they must be supplied by parameter setup; */ /* for decompression, they are read from the SOF marker. */ int component_id; /* identifier for this component (0..255) */ int component_index; /* its index in SOF or cinfo->comp_info[] */ int h_samp_factor; /* horizontal sampling factor (1..4) */ int v_samp_factor; /* vertical sampling factor (1..4) */ int quant_tbl_no; /* quantization table selector (0..3) */ /* These values may vary between scans. */ /* For compression, they must be supplied by parameter setup; */ /* for decompression, they are read from the SOS marker. */ /* The decompressor output side may not use these variables. */ int dc_tbl_no; /* DC entropy table selector (0..3) */ int ac_tbl_no; /* AC entropy table selector (0..3) */ /* Remaining fields should be treated as private by applications. */ /* These values are computed during compression or decompression startup: */ /* Component's size in DCT blocks. * Any dummy blocks added to complete an MCU are not counted; therefore * these values do not depend on whether a scan is interleaved or not. */ JDIMENSION width_in_blocks; JDIMENSION height_in_blocks; /* Size of a DCT block in samples. Always DCTSIZE for compression. * For decompression this is the size of the output from one DCT block, * reflecting any scaling we choose to apply during the IDCT step. * Values of 1,2,4,8 are likely to be supported. Note that different * components may receive different IDCT scalings. */ #if JPEG_LIB_VERSION >= 70 int DCT_h_scaled_size; int DCT_v_scaled_size; #else int DCT_scaled_size; #endif /* The downsampled dimensions are the component's actual, unpadded number * of samples at the main buffer (preprocessing/compression interface), thus * downsampled_width = ceil(image_width * Hi/Hmax) * and similarly for height. For decompression, IDCT scaling is included, so * downsampled_width = ceil(image_width * Hi/Hmax * DCT_[h_]scaled_size/DCTSIZE) */ JDIMENSION downsampled_width; /* actual width in samples */ JDIMENSION downsampled_height; /* actual height in samples */ /* This flag is used only for decompression. In cases where some of the * components will be ignored (eg grayscale output from YCbCr image), * we can skip most computations for the unused components. */ boolean component_needed; /* do we need the value of this component? */ /* These values are computed before starting a scan of the component. */ /* The decompressor output side may not use these variables. */ int MCU_width; /* number of blocks per MCU, horizontally */ int MCU_height; /* number of blocks per MCU, vertically */ int MCU_blocks; /* MCU_width * MCU_height */ int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_[h_]scaled_size */ int last_col_width; /* # of non-dummy blocks across in last MCU */ int last_row_height; /* # of non-dummy blocks down in last MCU */ /* Saved quantization table for component; NULL if none yet saved. * See jdinput.c comments about the need for this information. * This field is currently used only for decompression. */ JQUANT_TBL * quant_table; /* Private per-component storage for DCT or IDCT subsystem. */ void * dct_table; } jpeg_component_info; /* The script for encoding a multiple-scan file is an array of these: */ typedef struct { int comps_in_scan; /* number of components encoded in this scan */ int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */ int Ss, Se; /* progressive JPEG spectral selection parms */ int Ah, Al; /* progressive JPEG successive approx. parms */ } jpeg_scan_info; /* The decompressor can save APPn and COM markers in a list of these: */ typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr; struct jpeg_marker_struct { jpeg_saved_marker_ptr next; /* next in list, or NULL */ UINT8 marker; /* marker code: JPEG_COM, or JPEG_APP0+n */ unsigned int original_length; /* # bytes of data in the file */ unsigned int data_length; /* # bytes of data saved at data[] */ JOCTET FAR * data; /* the data contained in the marker */ /* the marker length word is not counted in data_length or original_length */ }; /* Known color spaces. */ #define JCS_EXTENSIONS 1 #define JCS_ALPHA_EXTENSIONS 1 typedef enum { JCS_UNKNOWN, /* error/unspecified */ JCS_GRAYSCALE, /* monochrome */ JCS_RGB, /* red/green/blue as specified by the RGB_RED, RGB_GREEN, RGB_BLUE, and RGB_PIXELSIZE macros */ JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */ JCS_CMYK, /* C/M/Y/K */ JCS_YCCK, /* Y/Cb/Cr/K */ JCS_EXT_RGB, /* red/green/blue */ JCS_EXT_RGBX, /* red/green/blue/x */ JCS_EXT_BGR, /* blue/green/red */ JCS_EXT_BGRX, /* blue/green/red/x */ JCS_EXT_XBGR, /* x/blue/green/red */ JCS_EXT_XRGB, /* x/red/green/blue */ /* When out_color_space it set to JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR, or JCS_EXT_XRGB during decompression, the X byte is undefined, and in order to ensure the best performance, libjpeg-turbo can set that byte to whatever value it wishes. Use the following colorspace constants to ensure that the X byte is set to 0xFF, so that it can be interpreted as an opaque alpha channel. */ JCS_EXT_RGBA, /* red/green/blue/alpha */ JCS_EXT_BGRA, /* blue/green/red/alpha */ JCS_EXT_ABGR, /* alpha/blue/green/red */ JCS_EXT_ARGB /* alpha/red/green/blue */ } J_COLOR_SPACE; /* DCT/IDCT algorithm options. */ typedef enum { JDCT_ISLOW, /* slow but accurate integer algorithm */ JDCT_IFAST, /* faster, less accurate integer method */ JDCT_FLOAT /* floating-point: accurate, fast on fast HW */ } J_DCT_METHOD; #ifndef JDCT_DEFAULT /* may be overridden in jconfig.h */ #define JDCT_DEFAULT JDCT_ISLOW #endif #ifndef JDCT_FASTEST /* may be overridden in jconfig.h */ #define JDCT_FASTEST JDCT_IFAST #endif /* Dithering options for decompression. */ typedef enum { JDITHER_NONE, /* no dithering */ JDITHER_ORDERED, /* simple ordered dither */ JDITHER_FS /* Floyd-Steinberg error diffusion dither */ } J_DITHER_MODE; /* Common fields between JPEG compression and decompression master structs. */ #define jpeg_common_fields \ struct jpeg_error_mgr * err; /* Error handler module */\ struct jpeg_memory_mgr * mem; /* Memory manager module */\ struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\ void * client_data; /* Available for use by application */\ boolean is_decompressor; /* So common code can tell which is which */\ int global_state /* For checking call sequence validity */ /* Routines that are to be used by both halves of the library are declared * to receive a pointer to this structure. There are no actual instances of * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct. */ struct jpeg_common_struct { jpeg_common_fields; /* Fields common to both master struct types */ /* Additional fields follow in an actual jpeg_compress_struct or * jpeg_decompress_struct. All three structs must agree on these * initial fields! (This would be a lot cleaner in C++.) */ }; typedef struct jpeg_common_struct * j_common_ptr; typedef struct jpeg_compress_struct * j_compress_ptr; typedef struct jpeg_decompress_struct * j_decompress_ptr; /* Master record for a compression instance */ struct jpeg_compress_struct { jpeg_common_fields; /* Fields shared with jpeg_decompress_struct */ /* Destination for compressed data */ struct jpeg_destination_mgr * dest; /* Description of source image --- these fields must be filled in by * outer application before starting compression. in_color_space must * be correct before you can even call jpeg_set_defaults(). */ JDIMENSION image_width; /* input image width */ JDIMENSION image_height; /* input image height */ int input_components; /* # of color components in input image */ J_COLOR_SPACE in_color_space; /* colorspace of input image */ double input_gamma; /* image gamma of input image */ /* Compression parameters --- these fields must be set before calling * jpeg_start_compress(). We recommend calling jpeg_set_defaults() to * initialize everything to reasonable defaults, then changing anything * the application specifically wants to change. That way you won't get * burnt when new parameters are added. Also note that there are several * helper routines to simplify changing parameters. */ #if JPEG_LIB_VERSION >= 70 unsigned int scale_num, scale_denom; /* fraction by which to scale image */ JDIMENSION jpeg_width; /* scaled JPEG image width */ JDIMENSION jpeg_height; /* scaled JPEG image height */ /* Dimensions of actual JPEG image that will be written to file, * derived from input dimensions by scaling factors above. * These fields are computed by jpeg_start_compress(). * You can also use jpeg_calc_jpeg_dimensions() to determine these values * in advance of calling jpeg_start_compress(). */ #endif int data_precision; /* bits of precision in image data */ int num_components; /* # of color components in JPEG image */ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */ jpeg_component_info * comp_info; /* comp_info[i] describes component that appears i'th in SOF */ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]; #if JPEG_LIB_VERSION >= 70 int q_scale_factor[NUM_QUANT_TBLS]; #endif /* ptrs to coefficient quantization tables, or NULL if not defined, * and corresponding scale factors (percentage, initialized 100). */ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]; JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]; /* ptrs to Huffman coding tables, or NULL if not defined */ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */ int num_scans; /* # of entries in scan_info array */ const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */ /* The default value of scan_info is NULL, which causes a single-scan * sequential JPEG file to be emitted. To create a multi-scan file, * set num_scans and scan_info to point to an array of scan definitions. */ boolean raw_data_in; /* TRUE=caller supplies downsampled data */ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */ boolean optimize_coding; /* TRUE=optimize entropy encoding parms */ boolean CCIR601_sampling; /* TRUE=first samples are cosited */ #if JPEG_LIB_VERSION >= 70 boolean do_fancy_downsampling; /* TRUE=apply fancy downsampling */ #endif int smoothing_factor; /* 1..100, or 0 for no input smoothing */ J_DCT_METHOD dct_method; /* DCT algorithm selector */ /* The restart interval can be specified in absolute MCUs by setting * restart_interval, or in MCU rows by setting restart_in_rows * (in which case the correct restart_interval will be figured * for each scan). */ unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */ int restart_in_rows; /* if > 0, MCU rows per restart interval */ /* Parameters controlling emission of special markers. */ boolean write_JFIF_header; /* should a JFIF marker be written? */ UINT8 JFIF_major_version; /* What to write for the JFIF version number */ UINT8 JFIF_minor_version; /* These three values are not used by the JPEG code, merely copied */ /* into the JFIF APP0 marker. density_unit can be 0 for unknown, */ /* 1 for dots/inch, or 2 for dots/cm. Note that the pixel aspect */ /* ratio is defined by X_density/Y_density even when density_unit=0. */ UINT8 density_unit; /* JFIF code for pixel size units */ UINT16 X_density; /* Horizontal pixel density */ UINT16 Y_density; /* Vertical pixel density */ boolean write_Adobe_marker; /* should an Adobe marker be written? */ /* State variable: index of next scanline to be written to * jpeg_write_scanlines(). Application may use this to control its * processing loop, e.g., "while (next_scanline < image_height)". */ JDIMENSION next_scanline; /* 0 .. image_height-1 */ /* Remaining fields are known throughout compressor, but generally * should not be touched by a surrounding application. */ /* * These fields are computed during compression startup */ boolean progressive_mode; /* TRUE if scan script uses progressive mode */ int max_h_samp_factor; /* largest h_samp_factor */ int max_v_samp_factor; /* largest v_samp_factor */ #if JPEG_LIB_VERSION >= 70 int min_DCT_h_scaled_size; /* smallest DCT_h_scaled_size of any component */ int min_DCT_v_scaled_size; /* smallest DCT_v_scaled_size of any component */ #endif JDIMENSION total_iMCU_rows; /* # of iMCU rows to be input to coef ctlr */ /* The coefficient controller receives data in units of MCU rows as defined * for fully interleaved scans (whether the JPEG file is interleaved or not). * There are v_samp_factor * DCTSIZE sample rows of each component in an * "iMCU" (interleaved MCU) row. */ /* * These fields are valid during any one scan. * They describe the components and MCUs actually appearing in the scan. */ int comps_in_scan; /* # of JPEG components in this scan */ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]; /* *cur_comp_info[i] describes component that appears i'th in SOS */ JDIMENSION MCUs_per_row; /* # of MCUs across the image */ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */ int blocks_in_MCU; /* # of DCT blocks per MCU */ int MCU_membership[C_MAX_BLOCKS_IN_MCU]; /* MCU_membership[i] is index in cur_comp_info of component owning */ /* i'th block in an MCU */ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */ #if JPEG_LIB_VERSION >= 80 int block_size; /* the basic DCT block size: 1..16 */ const int * natural_order; /* natural-order position array */ int lim_Se; /* min( Se, DCTSIZE2-1 ) */ #endif /* * Links to compression subobjects (methods and private variables of modules) */ struct jpeg_comp_master * master; struct jpeg_c_main_controller * main; struct jpeg_c_prep_controller * prep; struct jpeg_c_coef_controller * coef; struct jpeg_marker_writer * marker; struct jpeg_color_converter * cconvert; struct jpeg_downsampler * downsample; struct jpeg_forward_dct * fdct; struct jpeg_entropy_encoder * entropy; jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */ int script_space_size; }; /* Master record for a decompression instance */ struct jpeg_decompress_struct { jpeg_common_fields; /* Fields shared with jpeg_compress_struct */ /* Source of compressed data */ struct jpeg_source_mgr * src; /* Basic description of image --- filled in by jpeg_read_header(). */ /* Application may inspect these values to decide how to process image. */ JDIMENSION image_width; /* nominal image width (from SOF marker) */ JDIMENSION image_height; /* nominal image height */ int num_components; /* # of color components in JPEG image */ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */ /* Decompression processing parameters --- these fields must be set before * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes * them to default values. */ J_COLOR_SPACE out_color_space; /* colorspace for output */ unsigned int scale_num, scale_denom; /* fraction by which to scale image */ double output_gamma; /* image gamma wanted in output */ boolean buffered_image; /* TRUE=multiple output passes */ boolean raw_data_out; /* TRUE=downsampled data wanted */ J_DCT_METHOD dct_method; /* IDCT algorithm selector */ boolean do_fancy_upsampling; /* TRUE=apply fancy upsampling */ boolean do_block_smoothing; /* TRUE=apply interblock smoothing */ boolean quantize_colors; /* TRUE=colormapped output wanted */ /* the following are ignored if not quantize_colors: */ J_DITHER_MODE dither_mode; /* type of color dithering to use */ boolean two_pass_quantize; /* TRUE=use two-pass color quantization */ int desired_number_of_colors; /* max # colors to use in created colormap */ /* these are significant only in buffered-image mode: */ boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */ boolean enable_external_quant;/* enable future use of external colormap */ boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */ /* Description of actual output image that will be returned to application. * These fields are computed by jpeg_start_decompress(). * You can also use jpeg_calc_output_dimensions() to determine these values * in advance of calling jpeg_start_decompress(). */ JDIMENSION output_width; /* scaled image width */ JDIMENSION output_height; /* scaled image height */ int out_color_components; /* # of color components in out_color_space */ int output_components; /* # of color components returned */ /* output_components is 1 (a colormap index) when quantizing colors; * otherwise it equals out_color_components. */ int rec_outbuf_height; /* min recommended height of scanline buffer */ /* If the buffer passed to jpeg_read_scanlines() is less than this many rows * high, space and time will be wasted due to unnecessary data copying. * Usually rec_outbuf_height will be 1 or 2, at most 4. */ /* When quantizing colors, the output colormap is described by these fields. * The application can supply a colormap by setting colormap non-NULL before * calling jpeg_start_decompress; otherwise a colormap is created during * jpeg_start_decompress or jpeg_start_output. * The map has out_color_components rows and actual_number_of_colors columns. */ int actual_number_of_colors; /* number of entries in use */ JSAMPARRAY colormap; /* The color map as a 2-D pixel array */ /* State variables: these variables indicate the progress of decompression. * The application may examine these but must not modify them. */ /* Row index of next scanline to be read from jpeg_read_scanlines(). * Application may use this to control its processing loop, e.g., * "while (output_scanline < output_height)". */ JDIMENSION output_scanline; /* 0 .. output_height-1 */ /* Current input scan number and number of iMCU rows completed in scan. * These indicate the progress of the decompressor input side. */ int input_scan_number; /* Number of SOS markers seen so far */ JDIMENSION input_iMCU_row; /* Number of iMCU rows completed */ /* The "output scan number" is the notional scan being displayed by the * output side. The decompressor will not allow output scan/row number * to get ahead of input scan/row, but it can fall arbitrarily far behind. */ int output_scan_number; /* Nominal scan number being displayed */ JDIMENSION output_iMCU_row; /* Number of iMCU rows read */ /* Current progression status. coef_bits[c][i] indicates the precision * with which component c's DCT coefficient i (in zigzag order) is known. * It is -1 when no data has yet been received, otherwise it is the point * transform (shift) value for the most recent scan of the coefficient * (thus, 0 at completion of the progression). * This pointer is NULL when reading a non-progressive file. */ int (*coef_bits)[DCTSIZE2]; /* -1 or current Al value for each coef */ /* Internal JPEG parameters --- the application usually need not look at * these fields. Note that the decompressor output side may not use * any parameters that can change between scans. */ /* Quantization and Huffman tables are carried forward across input * datastreams when processing abbreviated JPEG datastreams. */ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]; /* ptrs to coefficient quantization tables, or NULL if not defined */ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]; JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]; /* ptrs to Huffman coding tables, or NULL if not defined */ /* These parameters are never carried across datastreams, since they * are given in SOF/SOS markers or defined to be reset by SOI. */ int data_precision; /* bits of precision in image data */ jpeg_component_info * comp_info; /* comp_info[i] describes component that appears i'th in SOF */ #if JPEG_LIB_VERSION >= 80 boolean is_baseline; /* TRUE if Baseline SOF0 encountered */ #endif boolean progressive_mode; /* TRUE if SOFn specifies progressive mode */ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */ unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */ /* These fields record data obtained from optional markers recognized by * the JPEG library. */ boolean saw_JFIF_marker; /* TRUE iff a JFIF APP0 marker was found */ /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */ UINT8 JFIF_major_version; /* JFIF version number */ UINT8 JFIF_minor_version; UINT8 density_unit; /* JFIF code for pixel size units */ UINT16 X_density; /* Horizontal pixel density */ UINT16 Y_density; /* Vertical pixel density */ boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */ UINT8 Adobe_transform; /* Color transform code from Adobe marker */ boolean CCIR601_sampling; /* TRUE=first samples are cosited */ /* Aside from the specific data retained from APPn markers known to the * library, the uninterpreted contents of any or all APPn and COM markers * can be saved in a list for examination by the application. */ jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */ /* Remaining fields are known throughout decompressor, but generally * should not be touched by a surrounding application. */ /* * These fields are computed during decompression startup */ int max_h_samp_factor; /* largest h_samp_factor */ int max_v_samp_factor; /* largest v_samp_factor */ #if JPEG_LIB_VERSION >= 70 int min_DCT_h_scaled_size; /* smallest DCT_h_scaled_size of any component */ int min_DCT_v_scaled_size; /* smallest DCT_v_scaled_size of any component */ #else int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */ #endif JDIMENSION total_iMCU_rows; /* # of iMCU rows in image */ /* The coefficient controller's input and output progress is measured in * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows * in fully interleaved JPEG scans, but are used whether the scan is * interleaved or not. We define an iMCU row as v_samp_factor DCT block * rows of each component. Therefore, the IDCT output contains * v_samp_factor*DCT_[v_]scaled_size sample rows of a component per iMCU row. */ JSAMPLE * sample_range_limit; /* table for fast range-limiting */ /* * These fields are valid during any one scan. * They describe the components and MCUs actually appearing in the scan. * Note that the decompressor output side must not use these fields. */ int comps_in_scan; /* # of JPEG components in this scan */ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]; /* *cur_comp_info[i] describes component that appears i'th in SOS */ JDIMENSION MCUs_per_row; /* # of MCUs across the image */ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */ int blocks_in_MCU; /* # of DCT blocks per MCU */ int MCU_membership[D_MAX_BLOCKS_IN_MCU]; /* MCU_membership[i] is index in cur_comp_info of component owning */ /* i'th block in an MCU */ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */ #if JPEG_LIB_VERSION >= 80 /* These fields are derived from Se of first SOS marker. */ int block_size; /* the basic DCT block size: 1..16 */ const int * natural_order; /* natural-order position array for entropy decode */ int lim_Se; /* min( Se, DCTSIZE2-1 ) for entropy decode */ #endif /* This field is shared between entropy decoder and marker parser. * It is either zero or the code of a JPEG marker that has been * read from the data source, but has not yet been processed. */ int unread_marker; /* * Links to decompression subobjects (methods, private variables of modules) */ struct jpeg_decomp_master * master; struct jpeg_d_main_controller * main; struct jpeg_d_coef_controller * coef; struct jpeg_d_post_controller * post; struct jpeg_input_controller * inputctl; struct jpeg_marker_reader * marker; struct jpeg_entropy_decoder * entropy; struct jpeg_inverse_dct * idct; struct jpeg_upsampler * upsample; struct jpeg_color_deconverter * cconvert; struct jpeg_color_quantizer * cquantize; }; /* "Object" declarations for JPEG modules that may be supplied or called * directly by the surrounding application. * As with all objects in the JPEG library, these structs only define the * publicly visible methods and state variables of a module. Additional * private fields may exist after the public ones. */ /* Error handler object */ struct jpeg_error_mgr { /* Error exit handler: does not return to caller */ JMETHOD(void, error_exit, (j_common_ptr cinfo)); /* Conditionally emit a trace or warning message */ JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level)); /* Routine that actually outputs a trace or error message */ JMETHOD(void, output_message, (j_common_ptr cinfo)); /* Format a message string for the most recent JPEG error or message */ JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer)); #define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */ /* Reset error state variables at start of a new image */ JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo)); /* The message ID code and any parameters are saved here. * A message can have one string parameter or up to 8 int parameters. */ int msg_code; #define JMSG_STR_PARM_MAX 80 union { int i[8]; char s[JMSG_STR_PARM_MAX]; } msg_parm; /* Standard state variables for error facility */ int trace_level; /* max msg_level that will be displayed */ /* For recoverable corrupt-data errors, we emit a warning message, * but keep going unless emit_message chooses to abort. emit_message * should count warnings in num_warnings. The surrounding application * can check for bad data by seeing if num_warnings is nonzero at the * end of processing. */ long num_warnings; /* number of corrupt-data warnings */ /* These fields point to the table(s) of error message strings. * An application can change the table pointer to switch to a different * message list (typically, to change the language in which errors are * reported). Some applications may wish to add additional error codes * that will be handled by the JPEG library error mechanism; the second * table pointer is used for this purpose. * * First table includes all errors generated by JPEG library itself. * Error code 0 is reserved for a "no such error string" message. */ const char * const * jpeg_message_table; /* Library errors */ int last_jpeg_message; /* Table contains strings 0..last_jpeg_message */ /* Second table can be added by application (see cjpeg/djpeg for example). * It contains strings numbered first_addon_message..last_addon_message. */ const char * const * addon_message_table; /* Non-library errors */ int first_addon_message; /* code for first string in addon table */ int last_addon_message; /* code for last string in addon table */ }; /* Progress monitor object */ struct jpeg_progress_mgr { JMETHOD(void, progress_monitor, (j_common_ptr cinfo)); long pass_counter; /* work units completed in this pass */ long pass_limit; /* total number of work units in this pass */ int completed_passes; /* passes completed so far */ int total_passes; /* total number of passes expected */ }; /* Data destination object for compression */ struct jpeg_destination_mgr { JOCTET * next_output_byte; /* => next byte to write in buffer */ size_t free_in_buffer; /* # of byte spaces remaining in buffer */ JMETHOD(void, init_destination, (j_compress_ptr cinfo)); JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo)); JMETHOD(void, term_destination, (j_compress_ptr cinfo)); }; /* Data source object for decompression */ struct jpeg_source_mgr { const JOCTET * next_input_byte; /* => next byte to read from buffer */ size_t bytes_in_buffer; /* # of bytes remaining in buffer */ JMETHOD(void, init_source, (j_decompress_ptr cinfo)); JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo)); JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes)); JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired)); JMETHOD(void, term_source, (j_decompress_ptr cinfo)); }; /* Memory manager object. * Allocates "small" objects (a few K total), "large" objects (tens of K), * and "really big" objects (virtual arrays with backing store if needed). * The memory manager does not allow individual objects to be freed; rather, * each created object is assigned to a pool, and whole pools can be freed * at once. This is faster and more convenient than remembering exactly what * to free, especially where malloc()/free() are not too speedy. * NB: alloc routines never return NULL. They exit to error_exit if not * successful. */ #define JPOOL_PERMANENT 0 /* lasts until master record is destroyed */ #define JPOOL_IMAGE 1 /* lasts until done with image/datastream */ #define JPOOL_NUMPOOLS 2 typedef struct jvirt_sarray_control * jvirt_sarray_ptr; typedef struct jvirt_barray_control * jvirt_barray_ptr; struct jpeg_memory_mgr { /* Method pointers */ JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id, size_t sizeofobject)); JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id, size_t sizeofobject)); JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows)); JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id, JDIMENSION blocksperrow, JDIMENSION numrows)); JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION samplesperrow, JDIMENSION numrows, JDIMENSION maxaccess)); JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION blocksperrow, JDIMENSION numrows, JDIMENSION maxaccess)); JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo)); JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo, jvirt_sarray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)); JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo, jvirt_barray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)); JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id)); JMETHOD(void, self_destruct, (j_common_ptr cinfo)); /* Limit on memory allocation for this JPEG object. (Note that this is * merely advisory, not a guaranteed maximum; it only affects the space * used for virtual-array buffers.) May be changed by outer application * after creating the JPEG object. */ long max_memory_to_use; /* Maximum allocation request accepted by alloc_large. */ long max_alloc_chunk; }; /* Routine signature for application-supplied marker processing methods. * Need not pass marker code since it is stored in cinfo->unread_marker. */ typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo)); /* Declarations for routines called by application. * The JPP macro hides prototype parameters from compilers that can't cope. * Note JPP requires double parentheses. */ #ifdef HAVE_PROTOTYPES #define JPP(arglist) arglist #else #define JPP(arglist) () #endif /* Short forms of external names for systems with brain-damaged linkers. * We shorten external names to be unique in the first six letters, which * is good enough for all known systems. * (If your compiler itself needs names to be unique in less than 15 * characters, you are out of luck. Get a better compiler.) */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_std_error jStdError #define jpeg_CreateCompress jCreaCompress #define jpeg_CreateDecompress jCreaDecompress #define jpeg_destroy_compress jDestCompress #define jpeg_destroy_decompress jDestDecompress #define jpeg_stdio_dest jStdDest #define jpeg_stdio_src jStdSrc #if JPEG_LIB_VERSION >= 80 #define jpeg_mem_dest jMemDest #define jpeg_mem_src jMemSrc #endif #define jpeg_set_defaults jSetDefaults #define jpeg_set_colorspace jSetColorspace #define jpeg_default_colorspace jDefColorspace #define jpeg_set_quality jSetQuality #define jpeg_set_linear_quality jSetLQuality #if JPEG_LIB_VERSION >= 70 #define jpeg_default_qtables jDefQTables #endif #define jpeg_add_quant_table jAddQuantTable #define jpeg_quality_scaling jQualityScaling #define jpeg_simple_progression jSimProgress #define jpeg_suppress_tables jSuppressTables #define jpeg_alloc_quant_table jAlcQTable #define jpeg_alloc_huff_table jAlcHTable #define jpeg_start_compress jStrtCompress #define jpeg_write_scanlines jWrtScanlines #define jpeg_finish_compress jFinCompress #if JPEG_LIB_VERSION >= 70 #define jpeg_calc_jpeg_dimensions jCjpegDimensions #endif #define jpeg_write_raw_data jWrtRawData #define jpeg_write_marker jWrtMarker #define jpeg_write_m_header jWrtMHeader #define jpeg_write_m_byte jWrtMByte #define jpeg_write_tables jWrtTables #define jpeg_read_header jReadHeader #define jpeg_start_decompress jStrtDecompress #define jpeg_read_scanlines jReadScanlines #define jpeg_finish_decompress jFinDecompress #define jpeg_read_raw_data jReadRawData #define jpeg_has_multiple_scans jHasMultScn #define jpeg_start_output jStrtOutput #define jpeg_finish_output jFinOutput #define jpeg_input_complete jInComplete #define jpeg_new_colormap jNewCMap #define jpeg_consume_input jConsumeInput #if JPEG_LIB_VERSION >= 80 #define jpeg_core_output_dimensions jCoreDimensions #endif #define jpeg_calc_output_dimensions jCalcDimensions #define jpeg_save_markers jSaveMarkers #define jpeg_set_marker_processor jSetMarker #define jpeg_read_coefficients jReadCoefs #define jpeg_write_coefficients jWrtCoefs #define jpeg_copy_critical_parameters jCopyCrit #define jpeg_abort_compress jAbrtCompress #define jpeg_abort_decompress jAbrtDecompress #define jpeg_abort jAbort #define jpeg_destroy jDestroy #define jpeg_resync_to_restart jResyncRestart #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* Default error-management setup */ EXTERN(struct jpeg_error_mgr *) jpeg_std_error JPP((struct jpeg_error_mgr * err)); /* Initialization of JPEG compression objects. * jpeg_create_compress() and jpeg_create_decompress() are the exported * names that applications should call. These expand to calls on * jpeg_CreateCompress and jpeg_CreateDecompress with additional information * passed for version mismatch checking. * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx. */ #define jpeg_create_compress(cinfo) \ jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \ (size_t) sizeof(struct jpeg_compress_struct)) #define jpeg_create_decompress(cinfo) \ jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \ (size_t) sizeof(struct jpeg_decompress_struct)) EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo, int version, size_t structsize)); EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo, int version, size_t structsize)); /* Destruction of JPEG compression objects */ EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo)); /* Standard data source and destination managers: stdio streams. */ /* Caller is responsible for opening the file before and closing after. */ EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile)); EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile)); #if JPEG_LIB_VERSION >= 80 /* Data source and destination managers: memory buffers. */ EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize)); EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo, unsigned char * inbuffer, unsigned long insize)); #endif /* Default parameter setup for compression */ EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo)); /* Compression parameter setup aids */ EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo, J_COLOR_SPACE colorspace)); EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality, boolean force_baseline)); EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo, int scale_factor, boolean force_baseline)); #if JPEG_LIB_VERSION >= 70 EXTERN(void) jpeg_default_qtables JPP((j_compress_ptr cinfo, boolean force_baseline)); #endif EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, boolean force_baseline)); EXTERN(int) jpeg_quality_scaling JPP((int quality)); EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo, boolean suppress)); EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo)); EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo)); /* Main entry points for compression */ EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo, boolean write_all_tables)); EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines)); EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo)); #if JPEG_LIB_VERSION >= 70 /* Precalculate JPEG dimensions for current compression parameters. */ EXTERN(void) jpeg_calc_jpeg_dimensions JPP((j_compress_ptr cinfo)); #endif /* Replaces jpeg_write_scanlines when writing raw downsampled data. */ EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo, JSAMPIMAGE data, JDIMENSION num_lines)); /* Write a special marker. See libjpeg.txt concerning safe usage. */ EXTERN(void) jpeg_write_marker JPP((j_compress_ptr cinfo, int marker, const JOCTET * dataptr, unsigned int datalen)); /* Same, but piecemeal. */ EXTERN(void) jpeg_write_m_header JPP((j_compress_ptr cinfo, int marker, unsigned int datalen)); EXTERN(void) jpeg_write_m_byte JPP((j_compress_ptr cinfo, int val)); /* Alternate compression function: just write an abbreviated table file */ EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo)); /* Decompression startup: read start of JPEG datastream to see what's there */ EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo, boolean require_image)); /* Return value is one of: */ #define JPEG_SUSPENDED 0 /* Suspended due to lack of input data */ #define JPEG_HEADER_OK 1 /* Found valid image datastream */ #define JPEG_HEADER_TABLES_ONLY 2 /* Found valid table-specs-only datastream */ /* If you pass require_image = TRUE (normal case), you need not check for * a TABLES_ONLY return code; an abbreviated file will cause an error exit. * JPEG_SUSPENDED is only possible if you use a data source module that can * give a suspension return (the stdio source module doesn't). */ /* Main entry points for decompression */ EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo)); EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)); EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo)); /* Replaces jpeg_read_scanlines when reading raw downsampled data. */ EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo, JSAMPIMAGE data, JDIMENSION max_lines)); /* Additional entry points for buffered-image mode. */ EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo)); EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo, int scan_number)); EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo)); EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo)); EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo)); EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo)); /* Return value is one of: */ /* #define JPEG_SUSPENDED 0 Suspended due to lack of input data */ #define JPEG_REACHED_SOS 1 /* Reached start of new scan */ #define JPEG_REACHED_EOI 2 /* Reached end of image */ #define JPEG_ROW_COMPLETED 3 /* Completed one iMCU row */ #define JPEG_SCAN_COMPLETED 4 /* Completed last iMCU row of a scan */ /* Precalculate output dimensions for current decompression parameters. */ #if JPEG_LIB_VERSION >= 80 EXTERN(void) jpeg_core_output_dimensions JPP((j_decompress_ptr cinfo)); #endif EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo)); /* Control saving of COM and APPn markers into marker_list. */ EXTERN(void) jpeg_save_markers JPP((j_decompress_ptr cinfo, int marker_code, unsigned int length_limit)); /* Install a special processing method for COM or APPn markers. */ EXTERN(void) jpeg_set_marker_processor JPP((j_decompress_ptr cinfo, int marker_code, jpeg_marker_parser_method routine)); /* Read or write raw DCT coefficients --- useful for lossless transcoding. */ EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo)); EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo)); /* If you choose to abort compression or decompression before completing * jpeg_finish_(de)compress, then you need to clean up to release memory, * temporary files, etc. You can just call jpeg_destroy_(de)compress * if you're done with the JPEG object, but if you want to clean it up and * reuse it, call this: */ EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo)); /* Generic versions of jpeg_abort and jpeg_destroy that work on either * flavor of JPEG object. These may be more convenient in some places. */ EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo)); EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo)); /* Default restart-marker-resync procedure for use by data source modules */ EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo, int desired)); /* These marker codes are exported since applications and data source modules * are likely to want to use them. */ #define JPEG_RST0 0xD0 /* RST0 marker code */ #define JPEG_EOI 0xD9 /* EOI marker code */ #define JPEG_APP0 0xE0 /* APP0 marker code */ #define JPEG_COM 0xFE /* COM marker code */ /* If we have a brain-damaged compiler that emits warnings (or worse, errors) * for structure definitions that are never filled in, keep it quiet by * supplying dummy definitions for the various substructures. */ #ifdef INCOMPLETE_TYPES_BROKEN #ifndef JPEG_INTERNALS /* will be defined in jpegint.h */ struct jvirt_sarray_control { long dummy; }; struct jvirt_barray_control { long dummy; }; struct jpeg_comp_master { long dummy; }; struct jpeg_c_main_controller { long dummy; }; struct jpeg_c_prep_controller { long dummy; }; struct jpeg_c_coef_controller { long dummy; }; struct jpeg_marker_writer { long dummy; }; struct jpeg_color_converter { long dummy; }; struct jpeg_downsampler { long dummy; }; struct jpeg_forward_dct { long dummy; }; struct jpeg_entropy_encoder { long dummy; }; struct jpeg_decomp_master { long dummy; }; struct jpeg_d_main_controller { long dummy; }; struct jpeg_d_coef_controller { long dummy; }; struct jpeg_d_post_controller { long dummy; }; struct jpeg_input_controller { long dummy; }; struct jpeg_marker_reader { long dummy; }; struct jpeg_entropy_decoder { long dummy; }; struct jpeg_inverse_dct { long dummy; }; struct jpeg_upsampler { long dummy; }; struct jpeg_color_deconverter { long dummy; }; struct jpeg_color_quantizer { long dummy; }; #endif /* JPEG_INTERNALS */ #endif /* INCOMPLETE_TYPES_BROKEN */ /* * The JPEG library modules define JPEG_INTERNALS before including this file. * The internal structure declarations are read only when that is true. * Applications using the library should not include jpegint.h, but may wish * to include jerror.h. */ #ifdef JPEG_INTERNALS #include "jpegint.h" /* fetch private declarations */ #include "jerror.h" /* fetch error codes too */ #endif #ifdef __cplusplus #ifndef DONT_USE_EXTERN_C } #endif #endif #endif /* JPEGLIB_H */ glmark2-2012.08/./src/libjpeg-turbo/jcparam.c0000664000175000017500000005353412013417376020045 0ustar alfalf00000000000000/* * jcparam.c * * Copyright (C) 1991-1998, Thomas G. Lane. * Modified 2003-2008 by Guido Vollbeding. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains optional default-setting code for the JPEG compressor. * Applications do not have to use this file, but those that don't use it * must know a lot more about the innards of the JPEG code. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Quantization table setup routines */ GLOBAL(void) jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, boolean force_baseline) /* Define a quantization table equal to the basic_table times * a scale factor (given as a percentage). * If force_baseline is TRUE, the computed quantization table entries * are limited to 1..255 for JPEG baseline compatibility. */ { JQUANT_TBL ** qtblptr; int i; long temp; /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS) ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl); qtblptr = & cinfo->quant_tbl_ptrs[which_tbl]; if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo); for (i = 0; i < DCTSIZE2; i++) { temp = ((long) basic_table[i] * scale_factor + 50L) / 100L; /* limit the values to the valid range */ if (temp <= 0L) temp = 1L; if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */ if (force_baseline && temp > 255L) temp = 255L; /* limit to baseline range if requested */ (*qtblptr)->quantval[i] = (UINT16) temp; } /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*qtblptr)->sent_table = FALSE; } /* These are the sample quantization tables given in JPEG spec section K.1. * The spec says that the values given produce "good" quality, and * when divided by 2, "very good" quality. */ static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = { 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 }; static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = { 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 }; #if JPEG_LIB_VERSION >= 70 GLOBAL(void) jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline) /* Set or change the 'quality' (quantization) setting, using default tables * and straight percentage-scaling quality scales. * This entry point allows different scalings for luminance and chrominance. */ { /* Set up two quantization tables using the specified scaling */ jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, cinfo->q_scale_factor[0], force_baseline); jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, cinfo->q_scale_factor[1], force_baseline); } #endif GLOBAL(void) jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor, boolean force_baseline) /* Set or change the 'quality' (quantization) setting, using default tables * and a straight percentage-scaling quality scale. In most cases it's better * to use jpeg_set_quality (below); this entry point is provided for * applications that insist on a linear percentage scaling. */ { /* Set up two quantization tables using the specified scaling */ jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl, scale_factor, force_baseline); jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl, scale_factor, force_baseline); } GLOBAL(int) jpeg_quality_scaling (int quality) /* Convert a user-specified quality rating to a percentage scaling factor * for an underlying quantization table, using our recommended scaling curve. * The input 'quality' factor should be 0 (terrible) to 100 (very good). */ { /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */ if (quality <= 0) quality = 1; if (quality > 100) quality = 100; /* The basic table is used as-is (scaling 100) for a quality of 50. * Qualities 50..100 are converted to scaling percentage 200 - 2*Q; * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table * to make all the table entries 1 (hence, minimum quantization loss). * Qualities 1..50 are converted to scaling percentage 5000/Q. */ if (quality < 50) quality = 5000 / quality; else quality = 200 - quality*2; return quality; } GLOBAL(void) jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline) /* Set or change the 'quality' (quantization) setting, using default tables. * This is the standard quality-adjusting entry point for typical user * interfaces; only those who want detailed control over quantization tables * would use the preceding three routines directly. */ { /* Convert user 0-100 rating to percentage scaling */ quality = jpeg_quality_scaling(quality); /* Set up standard quality tables */ jpeg_set_linear_quality(cinfo, quality, force_baseline); } /* * Huffman table setup routines */ LOCAL(void) add_huff_table (j_compress_ptr cinfo, JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val) /* Define a Huffman table */ { int nsymbols, len; if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); /* Copy the number-of-symbols-of-each-code-length counts */ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); /* Validate the counts. We do this here mainly so we can copy the right * number of symbols from the val[] array, without risking marching off * the end of memory. jchuff.c will do a more thorough test later. */ nsymbols = 0; for (len = 1; len <= 16; len++) nsymbols += bits[len]; if (nsymbols < 1 || nsymbols > 256) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8)); /* Initialize sent_table FALSE so table will be written to JPEG file. */ (*htblptr)->sent_table = FALSE; } LOCAL(void) std_huff_tables (j_compress_ptr cinfo) /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ /* IMPORTANT: these are only valid for 8-bit data precision! */ { static const UINT8 bits_dc_luminance[17] = { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; static const UINT8 val_dc_luminance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; static const UINT8 bits_dc_chrominance[17] = { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; static const UINT8 val_dc_chrominance[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; static const UINT8 bits_ac_luminance[17] = { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; static const UINT8 val_ac_luminance[] = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa }; static const UINT8 bits_ac_chrominance[17] = { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; static const UINT8 val_ac_chrominance[] = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa }; add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0], bits_dc_luminance, val_dc_luminance); add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0], bits_ac_luminance, val_ac_luminance); add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1], bits_dc_chrominance, val_dc_chrominance); add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1], bits_ac_chrominance, val_ac_chrominance); } /* * Default parameter setup for compression. * * Applications that don't choose to use this routine must do their * own setup of all these parameters. Alternately, you can call this * to establish defaults and then alter parameters selectively. This * is the recommended approach since, if we add any new parameters, * your code will still work (they'll be set to reasonable defaults). */ GLOBAL(void) jpeg_set_defaults (j_compress_ptr cinfo) { int i; /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Allocate comp_info array large enough for maximum component count. * Array is made permanent in case application wants to compress * multiple images at same param settings. */ if (cinfo->comp_info == NULL) cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, MAX_COMPONENTS * SIZEOF(jpeg_component_info)); /* Initialize everything not dependent on the color space */ #if JPEG_LIB_VERSION >= 70 cinfo->scale_num = 1; /* 1:1 scaling */ cinfo->scale_denom = 1; #endif cinfo->data_precision = BITS_IN_JSAMPLE; /* Set up two quantization tables using default quality of 75 */ jpeg_set_quality(cinfo, 75, TRUE); /* Set up two Huffman tables */ std_huff_tables(cinfo); /* Initialize default arithmetic coding conditioning */ for (i = 0; i < NUM_ARITH_TBLS; i++) { cinfo->arith_dc_L[i] = 0; cinfo->arith_dc_U[i] = 1; cinfo->arith_ac_K[i] = 5; } /* Default is no multiple-scan output */ cinfo->scan_info = NULL; cinfo->num_scans = 0; /* Expect normal source image, not raw downsampled data */ cinfo->raw_data_in = FALSE; /* Use Huffman coding, not arithmetic coding, by default */ cinfo->arith_code = FALSE; /* By default, don't do extra passes to optimize entropy coding */ cinfo->optimize_coding = FALSE; /* The standard Huffman tables are only valid for 8-bit data precision. * If the precision is higher, force optimization on so that usable * tables will be computed. This test can be removed if default tables * are supplied that are valid for the desired precision. */ if (cinfo->data_precision > 8) cinfo->optimize_coding = TRUE; /* By default, use the simpler non-cosited sampling alignment */ cinfo->CCIR601_sampling = FALSE; #if JPEG_LIB_VERSION >= 70 /* By default, apply fancy downsampling */ cinfo->do_fancy_downsampling = TRUE; #endif /* No input smoothing */ cinfo->smoothing_factor = 0; /* DCT algorithm preference */ cinfo->dct_method = JDCT_DEFAULT; /* No restart markers */ cinfo->restart_interval = 0; cinfo->restart_in_rows = 0; /* Fill in default JFIF marker parameters. Note that whether the marker * will actually be written is determined by jpeg_set_colorspace. * * By default, the library emits JFIF version code 1.01. * An application that wants to emit JFIF 1.02 extension markers should set * JFIF_minor_version to 2. We could probably get away with just defaulting * to 1.02, but there may still be some decoders in use that will complain * about that; saying 1.01 should minimize compatibility problems. */ cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */ cinfo->JFIF_minor_version = 1; cinfo->density_unit = 0; /* Pixel size is unknown by default */ cinfo->X_density = 1; /* Pixel aspect ratio is square by default */ cinfo->Y_density = 1; /* Choose JPEG colorspace based on input space, set defaults accordingly */ jpeg_default_colorspace(cinfo); } /* * Select an appropriate JPEG colorspace for in_color_space. */ GLOBAL(void) jpeg_default_colorspace (j_compress_ptr cinfo) { switch (cinfo->in_color_space) { case JCS_GRAYSCALE: jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); break; case JCS_RGB: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: jpeg_set_colorspace(cinfo, JCS_YCbCr); break; case JCS_YCbCr: jpeg_set_colorspace(cinfo, JCS_YCbCr); break; case JCS_CMYK: jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */ break; case JCS_YCCK: jpeg_set_colorspace(cinfo, JCS_YCCK); break; case JCS_UNKNOWN: jpeg_set_colorspace(cinfo, JCS_UNKNOWN); break; default: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); } } /* * Set the JPEG colorspace, and choose colorspace-dependent default values. */ GLOBAL(void) jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace) { jpeg_component_info * compptr; int ci; #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \ (compptr = &cinfo->comp_info[index], \ compptr->component_id = (id), \ compptr->h_samp_factor = (hsamp), \ compptr->v_samp_factor = (vsamp), \ compptr->quant_tbl_no = (quant), \ compptr->dc_tbl_no = (dctbl), \ compptr->ac_tbl_no = (actbl) ) /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* For all colorspaces, we use Q and Huff tables 0 for luminance components, * tables 1 for chrominance components. */ cinfo->jpeg_color_space = colorspace; cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */ cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */ switch (colorspace) { case JCS_GRAYSCALE: cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ cinfo->num_components = 1; /* JFIF specifies component ID 1 */ SET_COMP(0, 1, 1,1, 0, 0,0); break; case JCS_RGB: cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */ cinfo->num_components = 3; SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0); SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0); SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0); break; case JCS_YCbCr: cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */ cinfo->num_components = 3; /* JFIF specifies component IDs 1,2,3 */ /* We default to 2x2 subsamples of chrominance */ SET_COMP(0, 1, 2,2, 0, 0,0); SET_COMP(1, 2, 1,1, 1, 1,1); SET_COMP(2, 3, 1,1, 1, 1,1); break; case JCS_CMYK: cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */ cinfo->num_components = 4; SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0); SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0); SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0); SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0); break; case JCS_YCCK: cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */ cinfo->num_components = 4; SET_COMP(0, 1, 2,2, 0, 0,0); SET_COMP(1, 2, 1,1, 1, 1,1); SET_COMP(2, 3, 1,1, 1, 1,1); SET_COMP(3, 4, 2,2, 0, 0,0); break; case JCS_UNKNOWN: cinfo->num_components = cinfo->input_components; if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPONENTS); for (ci = 0; ci < cinfo->num_components; ci++) { SET_COMP(ci, ci, 1,1, 0, 0,0); } break; default: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); } } #ifdef C_PROGRESSIVE_SUPPORTED LOCAL(jpeg_scan_info *) fill_a_scan (jpeg_scan_info * scanptr, int ci, int Ss, int Se, int Ah, int Al) /* Support routine: generate one scan for specified component */ { scanptr->comps_in_scan = 1; scanptr->component_index[0] = ci; scanptr->Ss = Ss; scanptr->Se = Se; scanptr->Ah = Ah; scanptr->Al = Al; scanptr++; return scanptr; } LOCAL(jpeg_scan_info *) fill_scans (jpeg_scan_info * scanptr, int ncomps, int Ss, int Se, int Ah, int Al) /* Support routine: generate one scan for each component */ { int ci; for (ci = 0; ci < ncomps; ci++) { scanptr->comps_in_scan = 1; scanptr->component_index[0] = ci; scanptr->Ss = Ss; scanptr->Se = Se; scanptr->Ah = Ah; scanptr->Al = Al; scanptr++; } return scanptr; } LOCAL(jpeg_scan_info *) fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al) /* Support routine: generate interleaved DC scan if possible, else N scans */ { int ci; if (ncomps <= MAX_COMPS_IN_SCAN) { /* Single interleaved DC scan */ scanptr->comps_in_scan = ncomps; for (ci = 0; ci < ncomps; ci++) scanptr->component_index[ci] = ci; scanptr->Ss = scanptr->Se = 0; scanptr->Ah = Ah; scanptr->Al = Al; scanptr++; } else { /* Noninterleaved DC scan for each component */ scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al); } return scanptr; } /* * Create a recommended progressive-JPEG script. * cinfo->num_components and cinfo->jpeg_color_space must be correct. */ GLOBAL(void) jpeg_simple_progression (j_compress_ptr cinfo) { int ncomps = cinfo->num_components; int nscans; jpeg_scan_info * scanptr; /* Safety check to ensure start_compress not called yet. */ if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Figure space needed for script. Calculation must match code below! */ if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { /* Custom script for YCbCr color images. */ nscans = 10; } else { /* All-purpose script for other color spaces. */ if (ncomps > MAX_COMPS_IN_SCAN) nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */ else nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */ } /* Allocate space for script. * We need to put it in the permanent pool in case the application performs * multiple compressions without changing the settings. To avoid a memory * leak if jpeg_simple_progression is called repeatedly for the same JPEG * object, we try to re-use previously allocated space, and we allocate * enough space to handle YCbCr even if initially asked for grayscale. */ if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) { cinfo->script_space_size = MAX(nscans, 10); cinfo->script_space = (jpeg_scan_info *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, cinfo->script_space_size * SIZEOF(jpeg_scan_info)); } scanptr = cinfo->script_space; cinfo->scan_info = scanptr; cinfo->num_scans = nscans; if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) { /* Custom script for YCbCr color images. */ /* Initial DC scan */ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); /* Initial AC scan: get some luma data out in a hurry */ scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2); /* Chroma data is too small to be worth expending many scans on */ scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1); scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1); /* Complete spectral selection for luma AC */ scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2); /* Refine next bit of luma AC */ scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1); /* Finish DC successive approximation */ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); /* Finish AC successive approximation */ scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0); scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0); /* Luma bottom bit comes last since it's usually largest scan */ scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0); } else { /* All-purpose script for other color spaces. */ /* Successive approximation first pass */ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1); scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2); scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2); /* Successive approximation second pass */ scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1); /* Successive approximation final pass */ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0); scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0); } } #endif /* C_PROGRESSIVE_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/README0000664000175000017500000003411212013417376017133 0ustar alfalf00000000000000libjpeg-turbo note: This file contains portions of the libjpeg v6b and v8 README files, with additional wordsmithing by The libjpeg-turbo Project. It is included only for reference, as some parts of it may not apply to libjpeg-turbo. Please see README-turbo.txt for information specific to libjpeg-turbo. The Independent JPEG Group's JPEG software ========================================== This distribution contains a release of the Independent JPEG Group's free JPEG software. You are welcome to redistribute this software and to use it for any purpose, subject to the conditions under LEGAL ISSUES, below. This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone, Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson, Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers, and other members of the Independent JPEG Group. IJG is not affiliated with the official ISO JPEG standards committee. DOCUMENTATION ROADMAP ===================== This file contains the following sections: OVERVIEW General description of JPEG and the IJG software. LEGAL ISSUES Copyright, lack of warranty, terms of distribution. REFERENCES Where to learn more about JPEG. ARCHIVE LOCATIONS Where to find newer versions of this software. FILE FORMAT WARS Software *not* to get. TO DO Plans for future IJG releases. Other documentation files in the distribution are: User documentation: install.txt How to configure and install the IJG software. usage.txt Usage instructions for cjpeg, djpeg, jpegtran, rdjpgcom, and wrjpgcom. *.1 Unix-style man pages for programs (same info as usage.txt). wizard.txt Advanced usage instructions for JPEG wizards only. change.log Version-to-version change highlights. Programmer and internal documentation: libjpeg.txt How to use the JPEG library in your own programs. example.c Sample code for calling the JPEG library. structure.txt Overview of the JPEG library's internal structure. filelist.txt Road map of IJG files. coderules.txt Coding style rules --- please read if you contribute code. Please read at least the files install.txt and usage.txt. Some information can also be found in the JPEG FAQ (Frequently Asked Questions) article. See ARCHIVE LOCATIONS below to find out where to obtain the FAQ article. If you want to understand how the JPEG code works, we suggest reading one or more of the REFERENCES, then looking at the documentation files (in roughly the order listed) before diving into the code. OVERVIEW ======== This package contains C software to implement JPEG image encoding, decoding, and transcoding. JPEG (pronounced "jay-peg") is a standardized compression method for full-color and gray-scale images. JPEG's strong suit is compressing photographic images or other types of images that have smooth color and brightness transitions between neighboring pixels. Images with sharp lines or other abrupt features may not compress well with JPEG, and a higher JPEG quality may have to be used to avoid visible compression artifacts with such images. JPEG is lossy, meaning that the output pixels are not necessarily identical to the input pixels. However, on photographic content and other "smooth" images, very good compression ratios can be obtained with no visible compression artifacts, and extremely high compression ratios are possible if you are willing to sacrifice image quality (by reducing the "quality" setting in the compressor.) This software implements JPEG baseline, extended-sequential, and progressive compression processes. Provision is made for supporting all variants of these processes, although some uncommon parameter settings aren't implemented yet. We have made no provision for supporting the hierarchical or lossless processes defined in the standard. We provide a set of library routines for reading and writing JPEG image files, plus two sample applications "cjpeg" and "djpeg", which use the library to perform conversion between JPEG and some other popular image file formats. The library is intended to be reused in other applications. In order to support file conversion and viewing software, we have included considerable functionality beyond the bare JPEG coding/decoding capability; for example, the color quantization modules are not strictly part of JPEG decoding, but they are essential for output to colormapped file formats or colormapped displays. These extra functions can be compiled out of the library if not required for a particular application. We have also included "jpegtran", a utility for lossless transcoding between different JPEG processes, and "rdjpgcom" and "wrjpgcom", two simple applications for inserting and extracting textual comments in JFIF files. The emphasis in designing this software has been on achieving portability and flexibility, while also making it fast enough to be useful. In particular, the software is not intended to be read as a tutorial on JPEG. (See the REFERENCES section for introductory material.) Rather, it is intended to be reliable, portable, industrial-strength code. We do not claim to have achieved that goal in every aspect of the software, but we strive for it. We welcome the use of this software as a component of commercial products. No royalty is required, but we do ask for an acknowledgement in product documentation, as described under LEGAL ISSUES. LEGAL ISSUES ============ In plain English: 1. We don't promise that this software works. (But if you find any bugs, please let us know!) 2. You can use this software for whatever you want. You don't have to pay us. 3. You may not pretend that you wrote this software. If you use it in a program, you must acknowledge somewhere in your documentation that you've used the IJG code. In legalese: The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. This software is copyright (C) 1991-2010, Thomas G. Lane, Guido Vollbeding. All Rights Reserved except as specified below. Permission is hereby granted to use, copy, modify, and distribute this software (or portions thereof) for any purpose, without fee, subject to these conditions: (1) If any part of the source code for this software is distributed, then this README file must be included, with this copyright and no-warranty notice unaltered; and any additions, deletions, or changes to the original files must be clearly indicated in accompanying documentation. (2) If only executable code is distributed, then the accompanying documentation must state that "this software is based in part on the work of the Independent JPEG Group". (3) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. These conditions apply to any software derived from or based on the IJG code, not just to the unmodified library. If you use our work, you ought to acknowledge us. Permission is NOT granted for the use of any IJG author's name or company name in advertising or publicity relating to this software or products derived from it. This software may be referred to only as "the Independent JPEG Group's software". We specifically permit and encourage the use of this software as the basis of commercial products, provided that all warranty or liability claims are assumed by the product vendor. ansi2knr.c is included in this distribution by permission of L. Peter Deutsch, sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA. ansi2knr.c is NOT covered by the above copyright and conditions, but instead by the usual distribution terms of the Free Software Foundation; principally, that you must include source code if you redistribute it. (See the file ansi2knr.c for full details.) However, since ansi2knr.c is not needed as part of any program generated from the IJG code, this does not limit you more than the foregoing paragraphs do. The Unix configuration script "configure" was produced with GNU Autoconf. It is copyright by the Free Software Foundation but is freely distributable. The same holds for its supporting scripts (config.guess, config.sub, ltmain.sh). Another support script, install-sh, is copyright by X Consortium but is also freely distributable. The IJG distribution formerly included code to read and write GIF files. To avoid entanglement with the Unisys LZW patent, GIF reading support has been removed altogether, and the GIF writer has been simplified to produce "uncompressed GIFs". This technique does not use the LZW algorithm; the resulting GIF files are larger than usual, but are readable by all standard GIF decoders. We are required to state that "The Graphics Interchange Format(c) is the Copyright property of CompuServe Incorporated. GIF(sm) is a Service Mark property of CompuServe Incorporated." REFERENCES ========== We recommend reading one or more of these references before trying to understand the innards of the JPEG software. The best short technical introduction to the JPEG compression algorithm is Wallace, Gregory K. "The JPEG Still Picture Compression Standard", Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44. (Adjacent articles in that issue discuss MPEG motion picture compression, applications of JPEG, and related topics.) If you don't have the CACM issue handy, a PostScript file containing a revised version of Wallace's article is available at http://www.ijg.org/files/wallace.ps.gz. The file (actually a preprint for an article that appeared in IEEE Trans. Consumer Electronics) omits the sample images that appeared in CACM, but it includes corrections and some added material. Note: the Wallace article is copyright ACM and IEEE, and it may not be used for commercial purposes. A somewhat less technical, more leisurely introduction to JPEG can be found in "The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1. This book provides good explanations and example C code for a multitude of compression methods including JPEG. It is an excellent source if you are comfortable reading C code but don't know much about data compression in general. The book's JPEG sample code is far from industrial-strength, but when you are ready to look at a full implementation, you've got one here... The best currently available description of JPEG is the textbook "JPEG Still Image Data Compression Standard" by William B. Pennebaker and Joan L. Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1. Price US$59.95, 638 pp. The book includes the complete text of the ISO JPEG standards (DIS 10918-1 and draft DIS 10918-2). The original JPEG standard is divided into two parts, Part 1 being the actual specification, while Part 2 covers compliance testing methods. Part 1 is titled "Digital Compression and Coding of Continuous-tone Still Images, Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS 10918-1, ITU-T T.81. Part 2 is titled "Digital Compression and Coding of Continuous-tone Still Images, Part 2: Compliance testing" and has document numbers ISO/IEC IS 10918-2, ITU-T T.83. The JPEG standard does not specify all details of an interchangeable file format. For the omitted details we follow the "JFIF" conventions, revision 1.02. JFIF 1.02 has been adopted as an Ecma International Technical Report and thus received a formal publication status. It is available as a free download in PDF format from http://www.ecma-international.org/publications/techreports/E-TR-098.htm. A PostScript version of the JFIF document is available at http://www.ijg.org/files/jfif.ps.gz. There is also a plain text version at http://www.ijg.org/files/jfif.txt.gz, but it is missing the figures. The TIFF 6.0 file format specification can be obtained by FTP from ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz. The JPEG incorporation scheme found in the TIFF 6.0 spec of 3-June-92 has a number of serious problems. IJG does not recommend use of the TIFF 6.0 design (TIFF Compression tag 6). Instead, we recommend the JPEG design proposed by TIFF Technical Note #2 (Compression tag 7). Copies of this Note can be obtained from http://www.ijg.org/files/. It is expected that the next revision of the TIFF spec will replace the 6.0 JPEG design with the Note's design. Although IJG's own code does not support TIFF/JPEG, the free libtiff library uses our library to implement TIFF/JPEG per the Note. ARCHIVE LOCATIONS ================= The "official" archive site for this software is www.ijg.org. The most recent released version can always be found there in directory "files". This particular version will be archived as http://www.ijg.org/files/jpegsrc.v8d.tar.gz, and in Windows-compatible "zip" archive format as http://www.ijg.org/files/jpegsr8d.zip. The JPEG FAQ (Frequently Asked Questions) article is a source of some general information about JPEG. It is available on the World Wide Web at http://www.faqs.org/faqs/jpeg-faq/ and other news.answers archive sites, including the official news.answers archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/. If you don't have Web or FTP access, send e-mail to mail-server@rtfm.mit.edu with body send usenet/news.answers/jpeg-faq/part1 send usenet/news.answers/jpeg-faq/part2 FILE FORMAT WARS ================ The ISO JPEG standards committee actually promotes different formats like "JPEG 2000" or "JPEG XR", which are incompatible with original DCT-based JPEG. IJG therefore does not support these formats (see REFERENCES). Indeed, one of the original reasons for developing this free software was to help force convergence on common, interoperable format standards for JPEG files. Don't use an incompatible file format! (In any case, our decoder will remain capable of reading existing JPEG image files indefinitely.) TO DO ===== Please send bug reports, offers of help, etc. to jpeg-info@jpegclub.org. glmark2-2012.08/./src/libjpeg-turbo/jdatasrc-tj.c0000664000175000017500000001456412013417376020636 0ustar alfalf00000000000000/* * jdatasrc.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Modified 2009-2010 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains decompression data source routines for the case of * reading JPEG data from memory or from a file (or any stdio stream). * While these routines are sufficient for most applications, * some will want to use a different source manager. * IMPORTANT: we assume that fread() will correctly transcribe an array of * JOCTETs from 8-bit-wide elements on external storage. If char is wider * than 8 bits on your machine, you may need to do some tweaking. */ /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ #include "jinclude.h" #include "jpeglib.h" #include "jerror.h" /* * Initialize source --- called by jpeg_read_header * before any data is actually read. */ METHODDEF(void) init_mem_source (j_decompress_ptr cinfo) { /* no work necessary here */ } /* * Fill the input buffer --- called whenever buffer is emptied. * * In typical applications, this should read fresh data into the buffer * (ignoring the current state of next_input_byte & bytes_in_buffer), * reset the pointer & count to the start of the buffer, and return TRUE * indicating that the buffer has been reloaded. It is not necessary to * fill the buffer entirely, only to obtain at least one more byte. * * There is no such thing as an EOF return. If the end of the file has been * reached, the routine has a choice of ERREXIT() or inserting fake data into * the buffer. In most cases, generating a warning message and inserting a * fake EOI marker is the best course of action --- this will allow the * decompressor to output however much of the image is there. However, * the resulting error message is misleading if the real problem is an empty * input file, so we handle that case specially. * * In applications that need to be able to suspend compression due to input * not being available yet, a FALSE return indicates that no more data can be * obtained right now, but more may be forthcoming later. In this situation, * the decompressor will return to its caller (with an indication of the * number of scanlines it has read, if any). The application should resume * decompression after it has loaded more data into the input buffer. Note * that there are substantial restrictions on the use of suspension --- see * the documentation. * * When suspending, the decompressor will back up to a convenient restart point * (typically the start of the current MCU). next_input_byte & bytes_in_buffer * indicate where the restart point will be if the current call returns FALSE. * Data beyond this point must be rescanned after resumption, so move it to * the front of the buffer rather than discarding it. */ METHODDEF(boolean) fill_mem_input_buffer (j_decompress_ptr cinfo) { static JOCTET mybuffer[4]; /* The whole JPEG data is expected to reside in the supplied memory * buffer, so any request for more data beyond the given buffer size * is treated as an error. */ WARNMS(cinfo, JWRN_JPEG_EOF); /* Insert a fake EOI marker */ mybuffer[0] = (JOCTET) 0xFF; mybuffer[1] = (JOCTET) JPEG_EOI; cinfo->src->next_input_byte = mybuffer; cinfo->src->bytes_in_buffer = 2; return TRUE; } /* * Skip data --- used to skip over a potentially large amount of * uninteresting data (such as an APPn marker). * * Writers of suspendable-input applications must note that skip_input_data * is not granted the right to give a suspension return. If the skip extends * beyond the data currently in the buffer, the buffer can be marked empty so * that the next read will cause a fill_input_buffer call that can suspend. * Arranging for additional bytes to be discarded before reloading the input * buffer is the application writer's problem. */ METHODDEF(void) skip_input_data (j_decompress_ptr cinfo, long num_bytes) { struct jpeg_source_mgr * src = cinfo->src; /* Just a dumb implementation for now. Could use fseek() except * it doesn't work on pipes. Not clear that being smart is worth * any trouble anyway --- large skips are infrequent. */ if (num_bytes > 0) { while (num_bytes > (long) src->bytes_in_buffer) { num_bytes -= (long) src->bytes_in_buffer; (void) (*src->fill_input_buffer) (cinfo); /* note we assume that fill_input_buffer will never return FALSE, * so suspension need not be handled. */ } src->next_input_byte += (size_t) num_bytes; src->bytes_in_buffer -= (size_t) num_bytes; } } /* * An additional method that can be provided by data source modules is the * resync_to_restart method for error recovery in the presence of RST markers. * For the moment, this source module just uses the default resync method * provided by the JPEG library. That method assumes that no backtracking * is possible. */ /* * Terminate source --- called by jpeg_finish_decompress * after all data has been read. Often a no-op. * * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding * application must deal with any cleanup that should happen even * for error exit. */ METHODDEF(void) term_source (j_decompress_ptr cinfo) { /* no work necessary here */ } /* * Prepare for input from a supplied memory buffer. * The buffer must contain the whole JPEG data. */ GLOBAL(void) jpeg_mem_src_tj (j_decompress_ptr cinfo, unsigned char * inbuffer, unsigned long insize) { struct jpeg_source_mgr * src; if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ ERREXIT(cinfo, JERR_INPUT_EMPTY); /* The source object is made permanent so that a series of JPEG images * can be read from the same buffer by calling jpeg_mem_src only before * the first one. */ if (cinfo->src == NULL) { /* first time for this JPEG object? */ cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(struct jpeg_source_mgr)); } src = cinfo->src; src->init_source = init_mem_source; src->fill_input_buffer = fill_mem_input_buffer; src->skip_input_data = skip_input_data; src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->term_source = term_source; src->bytes_in_buffer = (size_t) insize; src->next_input_byte = (JOCTET *) inbuffer; } glmark2-2012.08/./src/libjpeg-turbo/jdatadst-tj.c0000664000175000017500000001402112013417376020625 0ustar alfalf00000000000000/* * jdatadst.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Modified 2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains compression data destination routines for the case of * emitting JPEG data to memory or to a file (or any stdio stream). * While these routines are sufficient for most applications, * some will want to use a different destination manager. * IMPORTANT: we assume that fwrite() will correctly transcribe an array of * JOCTETs into 8-bit-wide elements on external storage. If char is wider * than 8 bits on your machine, you may need to do some tweaking. */ /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ #include "jinclude.h" #include "jpeglib.h" #include "jerror.h" #ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ extern void * malloc JPP((size_t size)); extern void free JPP((void *ptr)); #endif #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ /* Expanded data destination object for memory output */ typedef struct { struct jpeg_destination_mgr pub; /* public fields */ unsigned char ** outbuffer; /* target buffer */ unsigned long * outsize; unsigned char * newbuffer; /* newly allocated buffer */ JOCTET * buffer; /* start of buffer */ size_t bufsize; boolean alloc; } my_mem_destination_mgr; typedef my_mem_destination_mgr * my_mem_dest_ptr; /* * Initialize destination --- called by jpeg_start_compress * before any data is actually written. */ METHODDEF(void) init_mem_destination (j_compress_ptr cinfo) { /* no work necessary here */ } /* * Empty the output buffer --- called whenever buffer fills up. * * In typical applications, this should write the entire output buffer * (ignoring the current state of next_output_byte & free_in_buffer), * reset the pointer & count to the start of the buffer, and return TRUE * indicating that the buffer has been dumped. * * In applications that need to be able to suspend compression due to output * overrun, a FALSE return indicates that the buffer cannot be emptied now. * In this situation, the compressor will return to its caller (possibly with * an indication that it has not accepted all the supplied scanlines). The * application should resume compression after it has made more room in the * output buffer. Note that there are substantial restrictions on the use of * suspension --- see the documentation. * * When suspending, the compressor will back up to a convenient restart point * (typically the start of the current MCU). next_output_byte & free_in_buffer * indicate where the restart point will be if the current call returns FALSE. * Data beyond this point will be regenerated after resumption, so do not * write it out when emptying the buffer externally. */ METHODDEF(boolean) empty_mem_output_buffer (j_compress_ptr cinfo) { size_t nextsize; JOCTET * nextbuffer; my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE); /* Try to allocate new buffer with double size */ nextsize = dest->bufsize * 2; nextbuffer = malloc(nextsize); if (nextbuffer == NULL) ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); if (dest->newbuffer != NULL) free(dest->newbuffer); dest->newbuffer = nextbuffer; dest->pub.next_output_byte = nextbuffer + dest->bufsize; dest->pub.free_in_buffer = dest->bufsize; dest->buffer = nextbuffer; dest->bufsize = nextsize; return TRUE; } /* * Terminate destination --- called by jpeg_finish_compress * after all data has been written. Usually needs to flush buffer. * * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding * application must deal with any cleanup that should happen even * for error exit. */ METHODDEF(void) term_mem_destination (j_compress_ptr cinfo) { my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; if(dest->alloc) *dest->outbuffer = dest->buffer; *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer); } /* * Prepare for output to a memory buffer. * The caller may supply an own initial buffer with appropriate size. * Otherwise, or when the actual data output exceeds the given size, * the library adapts the buffer size as necessary. * The standard library functions malloc/free are used for allocating * larger memory, so the buffer is available to the application after * finishing compression, and then the application is responsible for * freeing the requested memory. */ GLOBAL(void) jpeg_mem_dest_tj (j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize, boolean alloc) { my_mem_dest_ptr dest; if (outbuffer == NULL || outsize == NULL) /* sanity check */ ERREXIT(cinfo, JERR_BUFFER_SIZE); /* The destination object is made permanent so that multiple JPEG images * can be written to the same buffer without re-executing jpeg_mem_dest. */ if (cinfo->dest == NULL) { /* first time for this JPEG object? */ cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_mem_destination_mgr)); dest = (my_mem_dest_ptr) cinfo->dest; dest->newbuffer = NULL; } dest = (my_mem_dest_ptr) cinfo->dest; dest->pub.init_destination = init_mem_destination; dest->pub.empty_output_buffer = empty_mem_output_buffer; dest->pub.term_destination = term_mem_destination; dest->outbuffer = outbuffer; dest->outsize = outsize; dest->alloc = alloc; if (*outbuffer == NULL || *outsize == 0) { if (alloc) { /* Allocate initial buffer */ dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE); if (dest->newbuffer == NULL) ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); *outsize = OUTPUT_BUF_SIZE; } else ERREXIT(cinfo, JERR_BUFFER_SIZE); } dest->pub.next_output_byte = dest->buffer = *outbuffer; dest->pub.free_in_buffer = dest->bufsize = *outsize; } glmark2-2012.08/./src/libjpeg-turbo/jdtrans.c0000664000175000017500000001226412013417376020070 0ustar alfalf00000000000000/* * jdtrans.c * * Copyright (C) 1995-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains library routines for transcoding decompression, * that is, reading raw DCT coefficient arrays from an input JPEG file. * The routines in jdapimin.c will also be needed by a transcoder. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Forward declarations */ LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); /* * Read the coefficient arrays from a JPEG file. * jpeg_read_header must be completed before calling this. * * The entire image is read into a set of virtual coefficient-block arrays, * one per component. The return value is a pointer to the array of * virtual-array descriptors. These can be manipulated directly via the * JPEG memory manager, or handed off to jpeg_write_coefficients(). * To release the memory occupied by the virtual arrays, call * jpeg_finish_decompress() when done with the data. * * An alternative usage is to simply obtain access to the coefficient arrays * during a buffered-image-mode decompression operation. This is allowed * after any jpeg_finish_output() call. The arrays can be accessed until * jpeg_finish_decompress() is called. (Note that any call to the library * may reposition the arrays, so don't rely on access_virt_barray() results * to stay valid across library calls.) * * Returns NULL if suspended. This case need be checked only if * a suspending data source is used. */ GLOBAL(jvirt_barray_ptr *) jpeg_read_coefficients (j_decompress_ptr cinfo) { if (cinfo->global_state == DSTATE_READY) { /* First call: initialize active modules */ transdecode_master_selection(cinfo); cinfo->global_state = DSTATE_RDCOEFS; } if (cinfo->global_state == DSTATE_RDCOEFS) { /* Absorb whole file into the coef buffer */ for (;;) { int retcode; /* Call progress monitor hook if present */ if (cinfo->progress != NULL) (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); /* Absorb some more input */ retcode = (*cinfo->inputctl->consume_input) (cinfo); if (retcode == JPEG_SUSPENDED) return NULL; if (retcode == JPEG_REACHED_EOI) break; /* Advance progress counter if appropriate */ if (cinfo->progress != NULL && (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { /* startup underestimated number of scans; ratchet up one scan */ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; } } } /* Set state so that jpeg_finish_decompress does the right thing */ cinfo->global_state = DSTATE_STOPPING; } /* At this point we should be in state DSTATE_STOPPING if being used * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access * to the coefficients during a full buffered-image-mode decompression. */ if ((cinfo->global_state == DSTATE_STOPPING || cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { return cinfo->coef->coef_arrays; } /* Oops, improper usage */ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); return NULL; /* keep compiler happy */ } /* * Master selection of decompression modules for transcoding. * This substitutes for jdmaster.c's initialization of the full decompressor. */ LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo) { /* This is effectively a buffered-image operation. */ cinfo->buffered_image = TRUE; #if JPEG_LIB_VERSION >= 80 /* Compute output image dimensions and related values. */ jpeg_core_output_dimensions(cinfo); #endif /* Entropy decoding: either Huffman or arithmetic coding. */ if (cinfo->arith_code) { #ifdef D_ARITH_CODING_SUPPORTED jinit_arith_decoder(cinfo); #else ERREXIT(cinfo, JERR_ARITH_NOTIMPL); #endif } else { if (cinfo->progressive_mode) { #ifdef D_PROGRESSIVE_SUPPORTED jinit_phuff_decoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else jinit_huff_decoder(cinfo); } /* Always get a full-image coefficient buffer. */ jinit_d_coef_controller(cinfo, TRUE); /* We can now tell the memory manager to allocate virtual arrays. */ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); /* Initialize input side of decompressor to consume first scan. */ (*cinfo->inputctl->start_input_pass) (cinfo); /* Initialize progress monitoring. */ if (cinfo->progress != NULL) { int nscans; /* Estimate number of scans to set pass_limit. */ if (cinfo->progressive_mode) { /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nscans = 2 + 3 * cinfo->num_components; } else if (cinfo->inputctl->has_multiple_scans) { /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nscans = cinfo->num_components; } else { nscans = 1; } cinfo->progress->pass_counter = 0L; cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; cinfo->progress->completed_passes = 0; cinfo->progress->total_passes = 1; } } glmark2-2012.08/./src/libjpeg-turbo/jidctint.c0000664000175000017500000003473712013417376020244 0ustar alfalf00000000000000/* * jidctint.c * * Copyright (C) 1991-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a slow-but-accurate integer implementation of the * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine * must also perform dequantization of the input coefficients. * * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT * on each row (or vice versa, but it's more convenient to emit a row at * a time). Direct algorithms are also available, but they are much more * complex and seem not to be any faster when reduced to code. * * This implementation is based on an algorithm described in * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. * The primary algorithm described there uses 11 multiplies and 29 adds. * We use their alternate method with 12 multiplies and 32 adds. * The advantage of this method is that no data path contains more than one * multiplication; this allows a very simple and accurate implementation in * scaled fixed-point arithmetic, with a minimal number of shifts. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_ISLOW_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* * The poop on this scaling stuff is as follows: * * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) * larger than the true IDCT outputs. The final outputs are therefore * a factor of N larger than desired; since N=8 this can be cured by * a simple right shift at the end of the algorithm. The advantage of * this arrangement is that we save two multiplications per 1-D IDCT, * because the y0 and y4 inputs need not be divided by sqrt(N). * * We have to do addition and subtraction of the integer inputs, which * is no problem, and multiplication by fractional constants, which is * a problem to do in integer arithmetic. We multiply all the constants * by CONST_SCALE and convert them to integer constants (thus retaining * CONST_BITS bits of precision in the constants). After doing a * multiplication we have to divide the product by CONST_SCALE, with proper * rounding, to produce the correct output. This division can be done * cheaply as a right shift of CONST_BITS bits. We postpone shifting * as long as possible so that partial sums can be added together with * full fractional precision. * * The outputs of the first pass are scaled up by PASS1_BITS bits so that * they are represented to better-than-integral precision. These outputs * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word * with the recommended scaling. (To scale up 12-bit sample data further, an * intermediate INT32 array would be needed.) * * To avoid overflow of the 32-bit intermediate results in pass 2, we must * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis * shows that the values given below are the most effective. */ #if BITS_IN_JSAMPLE == 8 #define CONST_BITS 13 #define PASS1_BITS 2 #else #define CONST_BITS 13 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */ #endif /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus * causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) */ #if CONST_BITS == 13 #define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ #define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ #define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ #define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ #define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ #define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ #define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ #define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ #else #define FIX_0_298631336 FIX(0.298631336) #define FIX_0_390180644 FIX(0.390180644) #define FIX_0_541196100 FIX(0.541196100) #define FIX_0_765366865 FIX(0.765366865) #define FIX_0_899976223 FIX(0.899976223) #define FIX_1_175875602 FIX(1.175875602) #define FIX_1_501321110 FIX(1.501321110) #define FIX_1_847759065 FIX(1.847759065) #define FIX_1_961570560 FIX(1.961570560) #define FIX_2_053119869 FIX(2.053119869) #define FIX_2_562915447 FIX(2.562915447) #define FIX_3_072711026 FIX(3.072711026) #endif /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. * For 8-bit samples with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. * For 12-bit samples, a full 32-bit multiplication will be needed. */ #if BITS_IN_JSAMPLE == 8 #define MULTIPLY(var,const) MULTIPLY16C16(var,const) #else #define MULTIPLY(var,const) ((var) * (const)) #endif /* Dequantize a coefficient by multiplying it by the multiplier-table * entry; produce an int result. In this module, both inputs and result * are 16 bits or less, so either int or short multiply will work. */ #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval)) /* * Perform dequantization and inverse DCT on one block of coefficients. */ GLOBAL(void) jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { INT32 tmp0, tmp1, tmp2, tmp3; INT32 tmp10, tmp11, tmp12, tmp13; INT32 z1, z2, z3, z4, z5; JCOEFPTR inptr; ISLOW_MULT_TYPE * quantptr; int * wsptr; JSAMPROW outptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); int ctr; int workspace[DCTSIZE2]; /* buffers data between passes */ SHIFT_TEMPS /* Pass 1: process columns from input, store into work array. */ /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ /* furthermore, we scale the results by 2**PASS1_BITS. */ inptr = coef_block; quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; wsptr = workspace; for (ctr = DCTSIZE; ctr > 0; ctr--) { /* Due to quantization, we will usually find that many of the input * coefficients are zero, especially the AC terms. We can exploit this * by short-circuiting the IDCT calculation for any column in which all * the AC terms are zero. In that case each output is equal to the * DC coefficient (with scale factor as needed). * With typical images and quantization tables, half or more of the * column DCT calculations can be simplified this way. */ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; wsptr[DCTSIZE*2] = dcval; wsptr[DCTSIZE*3] = dcval; wsptr[DCTSIZE*4] = dcval; wsptr[DCTSIZE*5] = dcval; wsptr[DCTSIZE*6] = dcval; wsptr[DCTSIZE*7] = dcval; inptr++; /* advance pointers to next column */ quantptr++; wsptr++; continue; } /* Even part: reverse the even part of the forward DCT. */ /* The rotator is sqrt(2)*c(-6). */ z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); z1 = MULTIPLY(z2 + z3, FIX_0_541196100); tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065); tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); tmp0 = (z2 + z3) << CONST_BITS; tmp1 = (z2 - z3) << CONST_BITS; tmp10 = tmp0 + tmp3; tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; /* Odd part per figure 8; the matrix is unitary and hence its * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */ tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); z1 = tmp0 + tmp3; z2 = tmp1 + tmp2; z3 = tmp0 + tmp2; z4 = tmp1 + tmp3; z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; tmp0 += z1 + z3; tmp1 += z2 + z4; tmp2 += z2 + z3; tmp3 += z1 + z4; /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS); wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS); inptr++; /* advance pointers to next column */ quantptr++; wsptr++; } /* Pass 2: process rows from work array, store into output array. */ /* Note that we must descale the results by a factor of 8 == 2**3, */ /* and also undo the PASS1_BITS scaling. */ wsptr = workspace; for (ctr = 0; ctr < DCTSIZE; ctr++) { outptr = output_buf[ctr] + output_col; /* Rows of zeroes can be exploited in the same way as we did with columns. * However, the column calculation has created many nonzero AC terms, so * the simplification applies less often (typically 5% to 10% of the time). * On machines with very fast multiplication, it's possible that the * test takes more time than it's worth. In that case this section * may be commented out. */ #ifndef NO_ZERO_ROW_TEST if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { /* AC terms all zero */ JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) & RANGE_MASK]; outptr[0] = dcval; outptr[1] = dcval; outptr[2] = dcval; outptr[3] = dcval; outptr[4] = dcval; outptr[5] = dcval; outptr[6] = dcval; outptr[7] = dcval; wsptr += DCTSIZE; /* advance pointer to next row */ continue; } #endif /* Even part: reverse the even part of the forward DCT. */ /* The rotator is sqrt(2)*c(-6). */ z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[6]; z1 = MULTIPLY(z2 + z3, FIX_0_541196100); tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065); tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS; tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS; tmp10 = tmp0 + tmp3; tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; /* Odd part per figure 8; the matrix is unitary and hence its * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */ tmp0 = (INT32) wsptr[7]; tmp1 = (INT32) wsptr[5]; tmp2 = (INT32) wsptr[3]; tmp3 = (INT32) wsptr[1]; z1 = tmp0 + tmp3; z2 = tmp1 + tmp2; z3 = tmp0 + tmp2; z4 = tmp1 + tmp3; z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; tmp0 += z1 + z3; tmp1 += z2 + z4; tmp2 += z2 + z3; tmp3 += z1 + z4; /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0, CONST_BITS+PASS1_BITS+3) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ } } #endif /* DCT_ISLOW_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jdmaster.c0000664000175000017500000005114112013417376020231 0ustar alfalf00000000000000/* * jdmaster.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains master control logic for the JPEG decompressor. * These routines are concerned with selecting the modules to be executed * and with determining the number of passes and the work to be done in each * pass. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* Private state */ typedef struct { struct jpeg_decomp_master pub; /* public fields */ int pass_number; /* # of passes completed */ boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */ /* Saved references to initialized quantizer modules, * in case we need to switch modes. */ struct jpeg_color_quantizer * quantizer_1pass; struct jpeg_color_quantizer * quantizer_2pass; } my_decomp_master; typedef my_decomp_master * my_master_ptr; /* * Determine whether merged upsample/color conversion should be used. * CRUCIAL: this must match the actual capabilities of jdmerge.c! */ LOCAL(boolean) use_merged_upsample (j_decompress_ptr cinfo) { #ifdef UPSAMPLE_MERGING_SUPPORTED /* Merging is the equivalent of plain box-filter upsampling */ if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) return FALSE; /* jdmerge.c only supports YCC=>RGB color conversion */ if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || (cinfo->out_color_space != JCS_RGB && cinfo->out_color_space != JCS_EXT_RGB && cinfo->out_color_space != JCS_EXT_RGBX && cinfo->out_color_space != JCS_EXT_BGR && cinfo->out_color_space != JCS_EXT_BGRX && cinfo->out_color_space != JCS_EXT_XBGR && cinfo->out_color_space != JCS_EXT_XRGB && cinfo->out_color_space != JCS_EXT_RGBA && cinfo->out_color_space != JCS_EXT_BGRA && cinfo->out_color_space != JCS_EXT_ABGR && cinfo->out_color_space != JCS_EXT_ARGB) || cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]) return FALSE; /* and it only handles 2h1v or 2h2v sampling ratios */ if (cinfo->comp_info[0].h_samp_factor != 2 || cinfo->comp_info[1].h_samp_factor != 1 || cinfo->comp_info[2].h_samp_factor != 1 || cinfo->comp_info[0].v_samp_factor > 2 || cinfo->comp_info[1].v_samp_factor != 1 || cinfo->comp_info[2].v_samp_factor != 1) return FALSE; /* furthermore, it doesn't work if we've scaled the IDCTs differently */ if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size || cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size || cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size) return FALSE; /* ??? also need to test for upsample-time rescaling, when & if supported */ return TRUE; /* by golly, it'll work... */ #else return FALSE; #endif } /* * Compute output image dimensions and related values. * NOTE: this is exported for possible use by application. * Hence it mustn't do anything that can't be done twice. * Also note that it may be called before the master module is initialized! */ GLOBAL(void) jpeg_calc_output_dimensions (j_decompress_ptr cinfo) /* Do computations that are needed before master selection phase */ { #ifdef IDCT_SCALING_SUPPORTED int ci; jpeg_component_info *compptr; #endif /* Prevent application from calling me at wrong times */ if (cinfo->global_state != DSTATE_READY) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); #ifdef IDCT_SCALING_SUPPORTED /* Compute actual output image dimensions and DCT scaling choices. */ if (cinfo->scale_num * 8 <= cinfo->scale_denom) { /* Provide 1/8 scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 8L); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 8L); #if JPEG_LIB_VERSION >= 70 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 1; #else cinfo->min_DCT_scaled_size = 1; #endif } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) { /* Provide 1/4 scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 4L); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 4L); #if JPEG_LIB_VERSION >= 70 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 2; #else cinfo->min_DCT_scaled_size = 2; #endif } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) { /* Provide 1/2 scaling */ cinfo->output_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width, 2L); cinfo->output_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height, 2L); #if JPEG_LIB_VERSION >= 70 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 4; #else cinfo->min_DCT_scaled_size = 4; #endif } else { /* Provide 1/1 scaling */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; #if JPEG_LIB_VERSION >= 70 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE; #else cinfo->min_DCT_scaled_size = DCTSIZE; #endif } /* In selecting the actual DCT scaling for each component, we try to * scale up the chroma components via IDCT scaling rather than upsampling. * This saves time if the upsampler gets to use 1:1 scaling. * Note this code assumes that the supported DCT scalings are powers of 2. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { int ssize = cinfo->_min_DCT_scaled_size; while (ssize < DCTSIZE && (compptr->h_samp_factor * ssize * 2 <= cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) && (compptr->v_samp_factor * ssize * 2 <= cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size)) { ssize = ssize * 2; } #if JPEG_LIB_VERSION >= 70 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize; #else compptr->DCT_scaled_size = ssize; #endif } /* Recompute downsampled dimensions of components; * application needs to know these if using raw downsampled data. */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Size in samples, after IDCT scaling */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->image_width * (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size), (long) (cinfo->max_h_samp_factor * DCTSIZE)); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->image_height * (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size), (long) (cinfo->max_v_samp_factor * DCTSIZE)); } #else /* !IDCT_SCALING_SUPPORTED */ /* Hardwire it to "no scaling" */ cinfo->output_width = cinfo->image_width; cinfo->output_height = cinfo->image_height; /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE, * and has computed unscaled downsampled_width and downsampled_height. */ #endif /* IDCT_SCALING_SUPPORTED */ /* Report number of components in selected colorspace. */ /* Probably this should be in the color conversion module... */ switch (cinfo->out_color_space) { case JCS_GRAYSCALE: cinfo->out_color_components = 1; break; case JCS_RGB: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; break; case JCS_YCbCr: cinfo->out_color_components = 3; break; case JCS_CMYK: case JCS_YCCK: cinfo->out_color_components = 4; break; default: /* else must be same colorspace as in file */ cinfo->out_color_components = cinfo->num_components; break; } cinfo->output_components = (cinfo->quantize_colors ? 1 : cinfo->out_color_components); /* See if upsampler will want to emit more than one row at a time */ if (use_merged_upsample(cinfo)) cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; else cinfo->rec_outbuf_height = 1; } /* * Several decompression processes need to range-limit values to the range * 0..MAXJSAMPLE; the input value may fall somewhat outside this range * due to noise introduced by quantization, roundoff error, etc. These * processes are inner loops and need to be as fast as possible. On most * machines, particularly CPUs with pipelines or instruction prefetch, * a (subscript-check-less) C table lookup * x = sample_range_limit[x]; * is faster than explicit tests * if (x < 0) x = 0; * else if (x > MAXJSAMPLE) x = MAXJSAMPLE; * These processes all use a common table prepared by the routine below. * * For most steps we can mathematically guarantee that the initial value * of x is within MAXJSAMPLE+1 of the legal range, so a table running from * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial * limiting step (just after the IDCT), a wildly out-of-range value is * possible if the input data is corrupt. To avoid any chance of indexing * off the end of memory and getting a bad-pointer trap, we perform the * post-IDCT limiting thus: * x = range_limit[x & MASK]; * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit * samples. Under normal circumstances this is more than enough range and * a correct output will be generated; with bogus input data the mask will * cause wraparound, and we will safely generate a bogus-but-in-range output. * For the post-IDCT step, we want to convert the data from signed to unsigned * representation by adding CENTERJSAMPLE at the same time that we limit it. * So the post-IDCT limiting table ends up looking like this: * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE, * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), * 0,1,...,CENTERJSAMPLE-1 * Negative inputs select values from the upper half of the table after * masking. * * We can save some space by overlapping the start of the post-IDCT table * with the simpler range limiting table. The post-IDCT table begins at * sample_range_limit + CENTERJSAMPLE. * * Note that the table is allocated in near data space on PCs; it's small * enough and used often enough to justify this. */ LOCAL(void) prepare_range_limit_table (j_decompress_ptr cinfo) /* Allocate and fill in the sample_range_limit table */ { JSAMPLE * table; int i; table = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */ cinfo->sample_range_limit = table; /* First segment of "simple" table: limit[x] = 0 for x < 0 */ MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE)); /* Main part of "simple" table: limit[x] = x */ for (i = 0; i <= MAXJSAMPLE; i++) table[i] = (JSAMPLE) i; table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ /* End of simple table, rest of first half of post-IDCT table */ for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++) table[i] = MAXJSAMPLE; /* Second half of post-IDCT table */ MEMZERO(table + (2 * (MAXJSAMPLE+1)), (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); } /* * Master selection of decompression modules. * This is done once at jpeg_start_decompress time. We determine * which modules will be used and give them appropriate initialization calls. * We also initialize the decompressor input side to begin consuming data. * * Since jpeg_read_header has finished, we know what is in the SOF * and (first) SOS markers. We also have all the application parameter * settings. */ LOCAL(void) master_selection (j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; boolean use_c_buffer; long samplesperrow; JDIMENSION jd_samplesperrow; /* Initialize dimensions and other stuff */ jpeg_calc_output_dimensions(cinfo); prepare_range_limit_table(cinfo); /* Width of an output scanline must be representable as JDIMENSION. */ samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components; jd_samplesperrow = (JDIMENSION) samplesperrow; if ((long) jd_samplesperrow != samplesperrow) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); /* Initialize my private state */ master->pass_number = 0; master->using_merged_upsample = use_merged_upsample(cinfo); /* Color quantizer selection */ master->quantizer_1pass = NULL; master->quantizer_2pass = NULL; /* No mode changes if not using buffered-image mode. */ if (! cinfo->quantize_colors || ! cinfo->buffered_image) { cinfo->enable_1pass_quant = FALSE; cinfo->enable_external_quant = FALSE; cinfo->enable_2pass_quant = FALSE; } if (cinfo->quantize_colors) { if (cinfo->raw_data_out) ERREXIT(cinfo, JERR_NOTIMPL); /* 2-pass quantizer only works in 3-component color space. */ if (cinfo->out_color_components != 3) { cinfo->enable_1pass_quant = TRUE; cinfo->enable_external_quant = FALSE; cinfo->enable_2pass_quant = FALSE; cinfo->colormap = NULL; } else if (cinfo->colormap != NULL) { cinfo->enable_external_quant = TRUE; } else if (cinfo->two_pass_quantize) { cinfo->enable_2pass_quant = TRUE; } else { cinfo->enable_1pass_quant = TRUE; } if (cinfo->enable_1pass_quant) { #ifdef QUANT_1PASS_SUPPORTED jinit_1pass_quantizer(cinfo); master->quantizer_1pass = cinfo->cquantize; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } /* We use the 2-pass code to map to external colormaps. */ if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { #ifdef QUANT_2PASS_SUPPORTED jinit_2pass_quantizer(cinfo); master->quantizer_2pass = cinfo->cquantize; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } /* If both quantizers are initialized, the 2-pass one is left active; * this is necessary for starting with quantization to an external map. */ } /* Post-processing: in particular, color conversion first */ if (! cinfo->raw_data_out) { if (master->using_merged_upsample) { #ifdef UPSAMPLE_MERGING_SUPPORTED jinit_merged_upsampler(cinfo); /* does color conversion too */ #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { jinit_color_deconverter(cinfo); jinit_upsampler(cinfo); } jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); } /* Inverse DCT */ jinit_inverse_dct(cinfo); /* Entropy decoding: either Huffman or arithmetic coding. */ if (cinfo->arith_code) { #ifdef D_ARITH_CODING_SUPPORTED jinit_arith_decoder(cinfo); #else ERREXIT(cinfo, JERR_ARITH_NOTIMPL); #endif } else { if (cinfo->progressive_mode) { #ifdef D_PROGRESSIVE_SUPPORTED jinit_phuff_decoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else jinit_huff_decoder(cinfo); } /* Initialize principal buffer controllers. */ use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; jinit_d_coef_controller(cinfo, use_c_buffer); if (! cinfo->raw_data_out) jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); /* We can now tell the memory manager to allocate virtual arrays. */ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); /* Initialize input side of decompressor to consume first scan. */ (*cinfo->inputctl->start_input_pass) (cinfo); #ifdef D_MULTISCAN_FILES_SUPPORTED /* If jpeg_start_decompress will read the whole file, initialize * progress monitoring appropriately. The input step is counted * as one pass. */ if (cinfo->progress != NULL && ! cinfo->buffered_image && cinfo->inputctl->has_multiple_scans) { int nscans; /* Estimate number of scans to set pass_limit. */ if (cinfo->progressive_mode) { /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nscans = 2 + 3 * cinfo->num_components; } else { /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nscans = cinfo->num_components; } cinfo->progress->pass_counter = 0L; cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; cinfo->progress->completed_passes = 0; cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2); /* Count the input pass as done */ master->pass_number++; } #endif /* D_MULTISCAN_FILES_SUPPORTED */ } /* * Per-pass setup. * This is called at the beginning of each output pass. We determine which * modules will be active during this pass and give them appropriate * start_pass calls. We also set is_dummy_pass to indicate whether this * is a "real" output pass or a dummy pass for color quantization. * (In the latter case, jdapistd.c will crank the pass to completion.) */ METHODDEF(void) prepare_for_output_pass (j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; if (master->pub.is_dummy_pass) { #ifdef QUANT_2PASS_SUPPORTED /* Final pass of 2-pass quantization */ master->pub.is_dummy_pass = FALSE; (*cinfo->cquantize->start_pass) (cinfo, FALSE); (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* QUANT_2PASS_SUPPORTED */ } else { if (cinfo->quantize_colors && cinfo->colormap == NULL) { /* Select new quantization method */ if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { cinfo->cquantize = master->quantizer_2pass; master->pub.is_dummy_pass = TRUE; } else if (cinfo->enable_1pass_quant) { cinfo->cquantize = master->quantizer_1pass; } else { ERREXIT(cinfo, JERR_MODE_CHANGE); } } (*cinfo->idct->start_pass) (cinfo); (*cinfo->coef->start_output_pass) (cinfo); if (! cinfo->raw_data_out) { if (! master->using_merged_upsample) (*cinfo->cconvert->start_pass) (cinfo); (*cinfo->upsample->start_pass) (cinfo); if (cinfo->quantize_colors) (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); (*cinfo->post->start_pass) (cinfo, (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); } } /* Set up progress monitor's pass info if present */ if (cinfo->progress != NULL) { cinfo->progress->completed_passes = master->pass_number; cinfo->progress->total_passes = master->pass_number + (master->pub.is_dummy_pass ? 2 : 1); /* In buffered-image mode, we assume one more output pass if EOI not * yet reached, but no more passes if EOI has been reached. */ if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); } } } /* * Finish up at end of an output pass. */ METHODDEF(void) finish_output_pass (j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; if (cinfo->quantize_colors) (*cinfo->cquantize->finish_pass) (cinfo); master->pass_number++; } #ifdef D_MULTISCAN_FILES_SUPPORTED /* * Switch to a new external colormap between output passes. */ GLOBAL(void) jpeg_new_colormap (j_decompress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; /* Prevent application from calling me at wrong times */ if (cinfo->global_state != DSTATE_BUFIMAGE) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (cinfo->quantize_colors && cinfo->enable_external_quant && cinfo->colormap != NULL) { /* Select 2-pass quantizer for external colormap use */ cinfo->cquantize = master->quantizer_2pass; /* Notify quantizer of colormap change */ (*cinfo->cquantize->new_color_map) (cinfo); master->pub.is_dummy_pass = FALSE; /* just in case */ } else ERREXIT(cinfo, JERR_MODE_CHANGE); } #endif /* D_MULTISCAN_FILES_SUPPORTED */ /* * Initialize master decompression control and select active modules. * This is performed at the start of jpeg_start_decompress. */ GLOBAL(void) jinit_master_decompress (j_decompress_ptr cinfo) { my_master_ptr master; master = (my_master_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_decomp_master)); cinfo->master = (struct jpeg_decomp_master *) master; master->pub.prepare_for_output_pass = prepare_for_output_pass; master->pub.finish_output_pass = finish_output_pass; master->pub.is_dummy_pass = FALSE; master_selection(cinfo); } glmark2-2012.08/./src/libjpeg-turbo/jutils.c0000664000175000017500000001217012013417376017731 0ustar alfalf00000000000000/* * jutils.c * * Copyright (C) 1991-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains tables and miscellaneous utility routines needed * for both compression and decompression. * Note we prefix all global names with "j" to minimize conflicts with * a surrounding application. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element * of a DCT block read in natural order (left to right, top to bottom). */ #if 0 /* This table is not actually needed in v6a */ const int jpeg_zigzag_order[DCTSIZE2] = { 0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63 }; #endif /* * jpeg_natural_order[i] is the natural-order position of the i'th element * of zigzag order. * * When reading corrupted data, the Huffman decoders could attempt * to reference an entry beyond the end of this array (if the decoded * zero run length reaches past the end of the block). To prevent * wild stores without adding an inner-loop test, we put some extra * "63"s after the real entries. This will cause the extra coefficient * to be stored in location 63 of the block, not somewhere random. * The worst case would be a run-length of 15, which means we need 16 * fake entries. */ const int jpeg_natural_order[DCTSIZE2+16] = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ 63, 63, 63, 63, 63, 63, 63, 63 }; /* * Arithmetic utilities */ GLOBAL(long) jdiv_round_up (long a, long b) /* Compute a/b rounded up to next integer, ie, ceil(a/b) */ /* Assumes a >= 0, b > 0 */ { return (a + b - 1L) / b; } GLOBAL(long) jround_up (long a, long b) /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */ /* Assumes a >= 0, b > 0 */ { a += b - 1L; return a - (a % b); } /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays * and coefficient-block arrays. This won't work on 80x86 because the arrays * are FAR and we're assuming a small-pointer memory model. However, some * DOS compilers provide far-pointer versions of memcpy() and memset() even * in the small-model libraries. These will be used if USE_FMEM is defined. * Otherwise, the routines below do it the hard way. (The performance cost * is not all that great, because these routines aren't very heavily used.) */ #ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */ #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size) #define FMEMZERO(target,size) MEMZERO(target,size) #else /* 80x86 case, define if we can */ #ifdef USE_FMEM #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size)) #define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size)) #endif #endif GLOBAL(void) jcopy_sample_rows (JSAMPARRAY input_array, int source_row, JSAMPARRAY output_array, int dest_row, int num_rows, JDIMENSION num_cols) /* Copy some rows of samples from one place to another. * num_rows rows are copied from input_array[source_row++] * to output_array[dest_row++]; these areas may overlap for duplication. * The source and destination arrays must be at least as wide as num_cols. */ { register JSAMPROW inptr, outptr; #ifdef FMEMCOPY register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE)); #else register JDIMENSION count; #endif register int row; input_array += source_row; output_array += dest_row; for (row = num_rows; row > 0; row--) { inptr = *input_array++; outptr = *output_array++; #ifdef FMEMCOPY FMEMCOPY(outptr, inptr, count); #else for (count = num_cols; count > 0; count--) *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */ #endif } } GLOBAL(void) jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, JDIMENSION num_blocks) /* Copy a row of coefficient blocks from one place to another. */ { #ifdef FMEMCOPY FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF))); #else register JCOEFPTR inptr, outptr; register long count; inptr = (JCOEFPTR) input_row; outptr = (JCOEFPTR) output_row; for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) { *outptr++ = *inptr++; } #endif } GLOBAL(void) jzero_far (void FAR * target, size_t bytestozero) /* Zero out a chunk of FAR memory. */ /* This might be sample-array data, block-array data, or alloc_large data. */ { #ifdef FMEMZERO FMEMZERO(target, bytestozero); #else register char FAR * ptr = (char FAR *) target; register size_t count; for (count = bytestozero; count > 0; count--) { *ptr++ = 0; } #endif } glmark2-2012.08/./src/libjpeg-turbo/jdpostct.c0000664000175000017500000002277312013417376020263 0ustar alfalf00000000000000/* * jdpostct.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the decompression postprocessing controller. * This controller manages the upsampling, color conversion, and color * quantization/reduction steps; specifically, it controls the buffering * between upsample/color conversion and color quantization/reduction. * * If no color quantization/reduction is required, then this module has no * work to do, and it just hands off to the upsample/color conversion code. * An integrated upsample/convert/quantize process would replace this module * entirely. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Private buffer controller object */ typedef struct { struct jpeg_d_post_controller pub; /* public fields */ /* Color quantization source buffer: this holds output data from * the upsample/color conversion step to be passed to the quantizer. * For two-pass color quantization, we need a full-image buffer; * for one-pass operation, a strip buffer is sufficient. */ jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */ JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */ JDIMENSION strip_height; /* buffer size in rows */ /* for two-pass mode only: */ JDIMENSION starting_row; /* row # of first row in current strip */ JDIMENSION next_row; /* index of next row to fill/empty in strip */ } my_post_controller; typedef my_post_controller * my_post_ptr; /* Forward declarations */ METHODDEF(void) post_process_1pass JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); #ifdef QUANT_2PASS_SUPPORTED METHODDEF(void) post_process_prepass JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); METHODDEF(void) post_process_2pass JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)); #endif /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) { my_post_ptr post = (my_post_ptr) cinfo->post; switch (pass_mode) { case JBUF_PASS_THRU: if (cinfo->quantize_colors) { /* Single-pass processing with color quantization. */ post->pub.post_process_data = post_process_1pass; /* We could be doing buffered-image output before starting a 2-pass * color quantization; in that case, jinit_d_post_controller did not * allocate a strip buffer. Use the virtual-array buffer as workspace. */ if (post->buffer == NULL) { post->buffer = (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, post->whole_image, (JDIMENSION) 0, post->strip_height, TRUE); } } else { /* For single-pass processing without color quantization, * I have no work to do; just call the upsampler directly. */ post->pub.post_process_data = cinfo->upsample->upsample; } break; #ifdef QUANT_2PASS_SUPPORTED case JBUF_SAVE_AND_PASS: /* First pass of 2-pass quantization */ if (post->whole_image == NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); post->pub.post_process_data = post_process_prepass; break; case JBUF_CRANK_DEST: /* Second pass of 2-pass quantization */ if (post->whole_image == NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); post->pub.post_process_data = post_process_2pass; break; #endif /* QUANT_2PASS_SUPPORTED */ default: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); break; } post->starting_row = post->next_row = 0; } /* * Process some data in the one-pass (strip buffer) case. * This is used for color precision reduction as well as one-pass quantization. */ METHODDEF(void) post_process_1pass (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_post_ptr post = (my_post_ptr) cinfo->post; JDIMENSION num_rows, max_rows; /* Fill the buffer, but not more than what we can dump out in one go. */ /* Note we rely on the upsampler to detect bottom of image. */ max_rows = out_rows_avail - *out_row_ctr; if (max_rows > post->strip_height) max_rows = post->strip_height; num_rows = 0; (*cinfo->upsample->upsample) (cinfo, input_buf, in_row_group_ctr, in_row_groups_avail, post->buffer, &num_rows, max_rows); /* Quantize and emit data. */ (*cinfo->cquantize->color_quantize) (cinfo, post->buffer, output_buf + *out_row_ctr, (int) num_rows); *out_row_ctr += num_rows; } #ifdef QUANT_2PASS_SUPPORTED /* * Process some data in the first pass of 2-pass quantization. */ METHODDEF(void) post_process_prepass (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_post_ptr post = (my_post_ptr) cinfo->post; JDIMENSION old_next_row, num_rows; /* Reposition virtual buffer if at start of strip. */ if (post->next_row == 0) { post->buffer = (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, post->whole_image, post->starting_row, post->strip_height, TRUE); } /* Upsample some data (up to a strip height's worth). */ old_next_row = post->next_row; (*cinfo->upsample->upsample) (cinfo, input_buf, in_row_group_ctr, in_row_groups_avail, post->buffer, &post->next_row, post->strip_height); /* Allow quantizer to scan new data. No data is emitted, */ /* but we advance out_row_ctr so outer loop can tell when we're done. */ if (post->next_row > old_next_row) { num_rows = post->next_row - old_next_row; (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row, (JSAMPARRAY) NULL, (int) num_rows); *out_row_ctr += num_rows; } /* Advance if we filled the strip. */ if (post->next_row >= post->strip_height) { post->starting_row += post->strip_height; post->next_row = 0; } } /* * Process some data in the second pass of 2-pass quantization. */ METHODDEF(void) post_process_2pass (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) { my_post_ptr post = (my_post_ptr) cinfo->post; JDIMENSION num_rows, max_rows; /* Reposition virtual buffer if at start of strip. */ if (post->next_row == 0) { post->buffer = (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, post->whole_image, post->starting_row, post->strip_height, FALSE); } /* Determine number of rows to emit. */ num_rows = post->strip_height - post->next_row; /* available in strip */ max_rows = out_rows_avail - *out_row_ctr; /* available in output area */ if (num_rows > max_rows) num_rows = max_rows; /* We have to check bottom of image here, can't depend on upsampler. */ max_rows = cinfo->output_height - post->starting_row; if (num_rows > max_rows) num_rows = max_rows; /* Quantize and emit data. */ (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + post->next_row, output_buf + *out_row_ctr, (int) num_rows); *out_row_ctr += num_rows; /* Advance if we filled the strip. */ post->next_row += num_rows; if (post->next_row >= post->strip_height) { post->starting_row += post->strip_height; post->next_row = 0; } } #endif /* QUANT_2PASS_SUPPORTED */ /* * Initialize postprocessing controller. */ GLOBAL(void) jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer) { my_post_ptr post; post = (my_post_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_post_controller)); cinfo->post = (struct jpeg_d_post_controller *) post; post->pub.start_pass = start_pass_dpost; post->whole_image = NULL; /* flag for no virtual arrays */ post->buffer = NULL; /* flag for no strip buffer */ /* Create the quantization buffer, if needed */ if (cinfo->quantize_colors) { /* The buffer strip height is max_v_samp_factor, which is typically * an efficient number of rows for upsampling to return. * (In the presence of output rescaling, we might want to be smarter?) */ post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor; if (need_full_buffer) { /* Two-pass color quantization: need full-image storage. */ /* We round up the number of rows to a multiple of the strip height. */ #ifdef QUANT_2PASS_SUPPORTED post->whole_image = (*cinfo->mem->request_virt_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, cinfo->output_width * cinfo->out_color_components, (JDIMENSION) jround_up((long) cinfo->output_height, (long) post->strip_height), post->strip_height); #else ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); #endif /* QUANT_2PASS_SUPPORTED */ } else { /* One-pass color quantization: just make a strip buffer. */ post->buffer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width * cinfo->out_color_components, post->strip_height); } } } glmark2-2012.08/./src/libjpeg-turbo/jcdctmgr.c0000664000175000017500000004550312013417376020222 0ustar alfalf00000000000000/* * jcdctmgr.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 1999-2006, MIYASAKA Masaru. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2011 D. R. Commander * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the forward-DCT management logic. * This code selects a particular DCT implementation to be used, * and it performs related housekeeping chores including coefficient * quantization. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #include "jsimddct.h" /* Private subobject for this module */ typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data)); typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data)); typedef JMETHOD(void, convsamp_method_ptr, (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)); typedef JMETHOD(void, float_convsamp_method_ptr, (JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT *workspace)); typedef JMETHOD(void, quantize_method_ptr, (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)); typedef JMETHOD(void, float_quantize_method_ptr, (JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)); METHODDEF(void) quantize (JCOEFPTR, DCTELEM *, DCTELEM *); typedef struct { struct jpeg_forward_dct pub; /* public fields */ /* Pointer to the DCT routine actually in use */ forward_DCT_method_ptr dct; convsamp_method_ptr convsamp; quantize_method_ptr quantize; /* The actual post-DCT divisors --- not identical to the quant table * entries, because of scaling (especially for an unnormalized DCT). * Each table is given in normal array order. */ DCTELEM * divisors[NUM_QUANT_TBLS]; /* work area for FDCT subroutine */ DCTELEM * workspace; #ifdef DCT_FLOAT_SUPPORTED /* Same as above for the floating-point case. */ float_DCT_method_ptr float_dct; float_convsamp_method_ptr float_convsamp; float_quantize_method_ptr float_quantize; FAST_FLOAT * float_divisors[NUM_QUANT_TBLS]; FAST_FLOAT * float_workspace; #endif } my_fdct_controller; typedef my_fdct_controller * my_fdct_ptr; /* * Find the highest bit in an integer through binary search. */ LOCAL(int) flss (UINT16 val) { int bit; bit = 16; if (!val) return 0; if (!(val & 0xff00)) { bit -= 8; val <<= 8; } if (!(val & 0xf000)) { bit -= 4; val <<= 4; } if (!(val & 0xc000)) { bit -= 2; val <<= 2; } if (!(val & 0x8000)) { bit -= 1; val <<= 1; } return bit; } /* * Compute values to do a division using reciprocal. * * This implementation is based on an algorithm described in * "How to optimize for the Pentium family of microprocessors" * (http://www.agner.org/assem/). * More information about the basic algorithm can be found in * the paper "Integer Division Using Reciprocals" by Robert Alverson. * * The basic idea is to replace x/d by x * d^-1. In order to store * d^-1 with enough precision we shift it left a few places. It turns * out that this algoright gives just enough precision, and also fits * into DCTELEM: * * b = (the number of significant bits in divisor) - 1 * r = (word size) + b * f = 2^r / divisor * * f will not be an integer for most cases, so we need to compensate * for the rounding error introduced: * * no fractional part: * * result = input >> r * * fractional part of f < 0.5: * * round f down to nearest integer * result = ((input + 1) * f) >> r * * fractional part of f > 0.5: * * round f up to nearest integer * result = (input * f) >> r * * This is the original algorithm that gives truncated results. But we * want properly rounded results, so we replace "input" with * "input + divisor/2". * * In order to allow SIMD implementations we also tweak the values to * allow the same calculation to be made at all times: * * dctbl[0] = f rounded to nearest integer * dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5) * dctbl[2] = 1 << ((word size) * 2 - r) * dctbl[3] = r - (word size) * * dctbl[2] is for stupid instruction sets where the shift operation * isn't member wise (e.g. MMX). * * The reason dctbl[2] and dctbl[3] reduce the shift with (word size) * is that most SIMD implementations have a "multiply and store top * half" operation. * * Lastly, we store each of the values in their own table instead * of in a consecutive manner, yet again in order to allow SIMD * routines. */ LOCAL(int) compute_reciprocal (UINT16 divisor, DCTELEM * dtbl) { UDCTELEM2 fq, fr; UDCTELEM c; int b, r; b = flss(divisor) - 1; r = sizeof(DCTELEM) * 8 + b; fq = ((UDCTELEM2)1 << r) / divisor; fr = ((UDCTELEM2)1 << r) % divisor; c = divisor / 2; /* for rounding */ if (fr == 0) { /* divisor is power of two */ /* fq will be one bit too large to fit in DCTELEM, so adjust */ fq >>= 1; r--; } else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */ c++; } else { /* fractional part is > 0.5 */ fq++; } dtbl[DCTSIZE2 * 0] = (DCTELEM) fq; /* reciprocal */ dtbl[DCTSIZE2 * 1] = (DCTELEM) c; /* correction + roundfactor */ dtbl[DCTSIZE2 * 2] = (DCTELEM) (1 << (sizeof(DCTELEM)*8*2 - r)); /* scale */ dtbl[DCTSIZE2 * 3] = (DCTELEM) r - sizeof(DCTELEM)*8; /* shift */ if(r <= 16) return 0; else return 1; } /* * Initialize for a processing pass. * Verify that all referenced Q-tables are present, and set up * the divisor table for each one. * In the current implementation, DCT of all components is done during * the first pass, even if only some components will be output in the * first scan. Hence all components should be examined here. */ METHODDEF(void) start_pass_fdctmgr (j_compress_ptr cinfo) { my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; int ci, qtblno, i; jpeg_component_info *compptr; JQUANT_TBL * qtbl; DCTELEM * dtbl; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { qtblno = compptr->quant_tbl_no; /* Make sure specified quantization table is present */ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || cinfo->quant_tbl_ptrs[qtblno] == NULL) ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); qtbl = cinfo->quant_tbl_ptrs[qtblno]; /* Compute divisors for this quant table */ /* We may do this more than once for same table, but it's not a big deal */ switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: /* For LL&M IDCT method, divisors are equal to raw quantization * coefficients multiplied by 8 (to counteract scaling). */ if (fdct->divisors[qtblno] == NULL) { fdct->divisors[qtblno] = (DCTELEM *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (DCTSIZE2 * 4) * SIZEOF(DCTELEM)); } dtbl = fdct->divisors[qtblno]; for (i = 0; i < DCTSIZE2; i++) { if(!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i]) && fdct->quantize == jsimd_quantize) fdct->quantize = quantize; } break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: { /* For AA&N IDCT method, divisors are equal to quantization * coefficients scaled by scalefactor[row]*scalefactor[col], where * scalefactor[0] = 1 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 * We apply a further scale factor of 8. */ #define CONST_BITS 14 static const INT16 aanscales[DCTSIZE2] = { /* precomputed values scaled up by 14 bits */ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 }; SHIFT_TEMPS if (fdct->divisors[qtblno] == NULL) { fdct->divisors[qtblno] = (DCTELEM *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (DCTSIZE2 * 4) * SIZEOF(DCTELEM)); } dtbl = fdct->divisors[qtblno]; for (i = 0; i < DCTSIZE2; i++) { if(!compute_reciprocal( DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], (INT32) aanscales[i]), CONST_BITS-3), &dtbl[i]) && fdct->quantize == jsimd_quantize) fdct->quantize = quantize; } } break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: { /* For float AA&N IDCT method, divisors are equal to quantization * coefficients scaled by scalefactor[row]*scalefactor[col], where * scalefactor[0] = 1 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 * We apply a further scale factor of 8. * What's actually stored is 1/divisor so that the inner loop can * use a multiplication rather than a division. */ FAST_FLOAT * fdtbl; int row, col; static const double aanscalefactor[DCTSIZE] = { 1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 }; if (fdct->float_divisors[qtblno] == NULL) { fdct->float_divisors[qtblno] = (FAST_FLOAT *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, DCTSIZE2 * SIZEOF(FAST_FLOAT)); } fdtbl = fdct->float_divisors[qtblno]; i = 0; for (row = 0; row < DCTSIZE; row++) { for (col = 0; col < DCTSIZE; col++) { fdtbl[i] = (FAST_FLOAT) (1.0 / (((double) qtbl->quantval[i] * aanscalefactor[row] * aanscalefactor[col] * 8.0))); i++; } } } break; #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } } } /* * Load data into workspace, applying unsigned->signed conversion. */ METHODDEF(void) convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace) { register DCTELEM *workspaceptr; register JSAMPROW elemptr; register int elemr; workspaceptr = workspace; for (elemr = 0; elemr < DCTSIZE; elemr++) { elemptr = sample_data[elemr] + start_col; #if DCTSIZE == 8 /* unroll the inner loop */ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; #else { register int elemc; for (elemc = DCTSIZE; elemc > 0; elemc--) *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; } #endif } } /* * Quantize/descale the coefficients, and store into coef_blocks[]. */ METHODDEF(void) quantize (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace) { int i; DCTELEM temp; UDCTELEM recip, corr, shift; UDCTELEM2 product; JCOEFPTR output_ptr = coef_block; for (i = 0; i < DCTSIZE2; i++) { temp = workspace[i]; recip = divisors[i + DCTSIZE2 * 0]; corr = divisors[i + DCTSIZE2 * 1]; shift = divisors[i + DCTSIZE2 * 3]; if (temp < 0) { temp = -temp; product = (UDCTELEM2)(temp + corr) * recip; product >>= shift + sizeof(DCTELEM)*8; temp = product; temp = -temp; } else { product = (UDCTELEM2)(temp + corr) * recip; product >>= shift + sizeof(DCTELEM)*8; temp = product; } output_ptr[i] = (JCOEF) temp; } } /* * Perform forward DCT on one or more blocks of a component. * * The input samples are taken from the sample_data[] array starting at * position start_row/start_col, and moving to the right for any additional * blocks. The quantized coefficients are returned in coef_blocks[]. */ METHODDEF(void) forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks) /* This version is used for integer DCT implementations. */ { /* This routine is heavily used, so it's worth coding it tightly. */ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; DCTELEM * workspace; JDIMENSION bi; /* Make sure the compiler doesn't look up these every pass */ forward_DCT_method_ptr do_dct = fdct->dct; convsamp_method_ptr do_convsamp = fdct->convsamp; quantize_method_ptr do_quantize = fdct->quantize; workspace = fdct->workspace; sample_data += start_row; /* fold in the vertical offset once */ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { /* Load data into workspace, applying unsigned->signed conversion */ (*do_convsamp) (sample_data, start_col, workspace); /* Perform the DCT */ (*do_dct) (workspace); /* Quantize/descale the coefficients, and store into coef_blocks[] */ (*do_quantize) (coef_blocks[bi], divisors, workspace); } } #ifdef DCT_FLOAT_SUPPORTED METHODDEF(void) convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace) { register FAST_FLOAT *workspaceptr; register JSAMPROW elemptr; register int elemr; workspaceptr = workspace; for (elemr = 0; elemr < DCTSIZE; elemr++) { elemptr = sample_data[elemr] + start_col; #if DCTSIZE == 8 /* unroll the inner loop */ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); #else { register int elemc; for (elemc = DCTSIZE; elemc > 0; elemc--) *workspaceptr++ = (FAST_FLOAT) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); } #endif } } METHODDEF(void) quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace) { register FAST_FLOAT temp; register int i; register JCOEFPTR output_ptr = coef_block; for (i = 0; i < DCTSIZE2; i++) { /* Apply the quantization and scaling factor */ temp = workspace[i] * divisors[i]; /* Round to nearest integer. * Since C does not specify the direction of rounding for negative * quotients, we have to force the dividend positive for portability. * The maximum coefficient size is +-16K (for 12-bit data), so this * code should work for either 16-bit or 32-bit ints. */ output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384); } } METHODDEF(void) forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks) /* This version is used for floating-point DCT implementations. */ { /* This routine is heavily used, so it's worth coding it tightly. */ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no]; FAST_FLOAT * workspace; JDIMENSION bi; /* Make sure the compiler doesn't look up these every pass */ float_DCT_method_ptr do_dct = fdct->float_dct; float_convsamp_method_ptr do_convsamp = fdct->float_convsamp; float_quantize_method_ptr do_quantize = fdct->float_quantize; workspace = fdct->float_workspace; sample_data += start_row; /* fold in the vertical offset once */ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { /* Load data into workspace, applying unsigned->signed conversion */ (*do_convsamp) (sample_data, start_col, workspace); /* Perform the DCT */ (*do_dct) (workspace); /* Quantize/descale the coefficients, and store into coef_blocks[] */ (*do_quantize) (coef_blocks[bi], divisors, workspace); } } #endif /* DCT_FLOAT_SUPPORTED */ /* * Initialize FDCT manager. */ GLOBAL(void) jinit_forward_dct (j_compress_ptr cinfo) { my_fdct_ptr fdct; int i; fdct = (my_fdct_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_fdct_controller)); cinfo->fdct = (struct jpeg_forward_dct *) fdct; fdct->pub.start_pass = start_pass_fdctmgr; /* First determine the DCT... */ switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: fdct->pub.forward_DCT = forward_DCT; if (jsimd_can_fdct_islow()) fdct->dct = jsimd_fdct_islow; else fdct->dct = jpeg_fdct_islow; break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: fdct->pub.forward_DCT = forward_DCT; if (jsimd_can_fdct_ifast()) fdct->dct = jsimd_fdct_ifast; else fdct->dct = jpeg_fdct_ifast; break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: fdct->pub.forward_DCT = forward_DCT_float; if (jsimd_can_fdct_float()) fdct->float_dct = jsimd_fdct_float; else fdct->float_dct = jpeg_fdct_float; break; #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } /* ...then the supporting stages. */ switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: #endif #if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED) if (jsimd_can_convsamp()) fdct->convsamp = jsimd_convsamp; else fdct->convsamp = convsamp; if (jsimd_can_quantize()) fdct->quantize = jsimd_quantize; else fdct->quantize = quantize; break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: if (jsimd_can_convsamp_float()) fdct->float_convsamp = jsimd_convsamp_float; else fdct->float_convsamp = convsamp_float; if (jsimd_can_quantize_float()) fdct->float_quantize = jsimd_quantize_float; else fdct->float_quantize = quantize_float; break; #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } /* Allocate workspace memory */ #ifdef DCT_FLOAT_SUPPORTED if (cinfo->dct_method == JDCT_FLOAT) fdct->float_workspace = (FAST_FLOAT *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(FAST_FLOAT) * DCTSIZE2); else #endif fdct->workspace = (DCTELEM *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(DCTELEM) * DCTSIZE2); /* Mark divisor tables unallocated */ for (i = 0; i < NUM_QUANT_TBLS; i++) { fdct->divisors[i] = NULL; #ifdef DCT_FLOAT_SUPPORTED fdct->float_divisors[i] = NULL; #endif } } glmark2-2012.08/./src/libjpeg-turbo/jdcolor.c0000664000175000017500000004032512013417376020056 0ustar alfalf00000000000000/* * jdcolor.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2009, 2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains output colorspace conversion routines. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jsimd.h" #include "config.h" /* Private subobject */ typedef struct { struct jpeg_color_deconverter pub; /* public fields */ /* Private state for YCC->RGB conversion */ int * Cr_r_tab; /* => table for Cr to R conversion */ int * Cb_b_tab; /* => table for Cb to B conversion */ INT32 * Cr_g_tab; /* => table for Cr to G conversion */ INT32 * Cb_g_tab; /* => table for Cb to G conversion */ } my_color_deconverter; typedef my_color_deconverter * my_cconvert_ptr; /**************** YCbCr -> RGB conversion: most common case **************/ /* * YCbCr is defined per CCIR 601-1, except that Cb and Cr are * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. * The conversion equations to be implemented are therefore * R = Y + 1.40200 * Cr * G = Y - 0.34414 * Cb - 0.71414 * Cr * B = Y + 1.77200 * Cb * where Cb and Cr represent the incoming values less CENTERJSAMPLE. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) * * To avoid floating-point arithmetic, we represent the fractional constants * as integers scaled up by 2^16 (about 4 digits precision); we have to divide * the products by 2^16, with appropriate rounding, to get the correct answer. * Notice that Y, being an integral input, does not contribute any fraction * so it need not participate in the rounding. * * For even more speed, we avoid doing any multiplications in the inner loop * by precalculating the constants times Cb and Cr for all possible values. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); * for 12-bit samples it is still acceptable. It's not very reasonable for * 16-bit samples, but if you want lossless storage you shouldn't be changing * colorspace anyway. * The Cr=>R and Cb=>B values can be rounded to integers in advance; the * values for the G calculation are left scaled up, since we must add them * together before rounding. */ #define SCALEBITS 16 /* speediest right-shift on some machines */ #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) #define FIX(x) ((INT32) ((x) * (1L<RGB colorspace conversion. */ LOCAL(void) build_ycc_rgb_table (j_decompress_ptr cinfo) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; int i; INT32 x; SHIFT_TEMPS cconvert->Cr_r_tab = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int)); cconvert->Cb_b_tab = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int)); cconvert->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32)); cconvert->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32)); for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* Cr=>R value is nearest int to 1.40200 * x */ cconvert->Cr_r_tab[i] = (int) RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); /* Cb=>B value is nearest int to 1.77200 * x */ cconvert->Cb_b_tab[i] = (int) RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); /* Cr=>G value is scaled-up -0.71414 * x */ cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; /* Cb=>G value is scaled-up -0.34414 * x */ /* We also add in ONE_HALF so that need not do it in inner loop */ cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; } } /* * Convert some rows of samples to the output colorspace. */ METHODDEF(void) ycc_rgb_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { switch (cinfo->out_color_space) { case JCS_EXT_RGB: ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_BGR: ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; default: ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; } } /**************** Cases other than YCbCr -> RGB **************/ /* * Color conversion for no colorspace change: just copy the data, * converting from separate-planes to interleaved representation. */ METHODDEF(void) null_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { register JSAMPROW inptr, outptr; register JDIMENSION count; register int num_components = cinfo->num_components; JDIMENSION num_cols = cinfo->output_width; int ci; while (--num_rows >= 0) { for (ci = 0; ci < num_components; ci++) { inptr = input_buf[ci][input_row]; outptr = output_buf[0] + ci; for (count = num_cols; count > 0; count--) { *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ outptr += num_components; } } input_row++; output_buf++; } } /* * Color conversion for grayscale: just copy the data. * This also works for YCbCr -> grayscale conversion, in which * we just copy the Y (luminance) component and ignore chrominance. */ METHODDEF(void) grayscale_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, num_rows, cinfo->output_width); } /* * Convert grayscale to RGB */ METHODDEF(void) gray_rgb_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { switch (cinfo->out_color_space) { case JCS_EXT_RGB: gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_BGR: gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; default: gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, num_rows); break; } } /* * Adobe-style YCCK->CMYK conversion. * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same * conversion as above, while passing K (black) unchanged. * We assume build_ycc_rgb_table has been called. */ METHODDEF(void) ycck_cmyk_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; register int y, cb, cr; register JSAMPROW outptr; register JSAMPROW inptr0, inptr1, inptr2, inptr3; register JDIMENSION col; JDIMENSION num_cols = cinfo->output_width; /* copy these pointers into registers if possible */ register JSAMPLE * range_limit = cinfo->sample_range_limit; register int * Crrtab = cconvert->Cr_r_tab; register int * Cbbtab = cconvert->Cb_b_tab; register INT32 * Crgtab = cconvert->Cr_g_tab; register INT32 * Cbgtab = cconvert->Cb_g_tab; SHIFT_TEMPS while (--num_rows >= 0) { inptr0 = input_buf[0][input_row]; inptr1 = input_buf[1][input_row]; inptr2 = input_buf[2][input_row]; inptr3 = input_buf[3][input_row]; input_row++; outptr = *output_buf++; for (col = 0; col < num_cols; col++) { y = GETJSAMPLE(inptr0[col]); cb = GETJSAMPLE(inptr1[col]); cr = GETJSAMPLE(inptr2[col]); /* Range-limiting is essential due to noise introduced by DCT losses. */ outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS)))]; outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ /* K passes through unchanged */ outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */ outptr += 4; } } } /* * Empty method for start_pass. */ METHODDEF(void) start_pass_dcolor (j_decompress_ptr cinfo) { /* no work needed */ } /* * Module initialization routine for output colorspace conversion. */ GLOBAL(void) jinit_color_deconverter (j_decompress_ptr cinfo) { my_cconvert_ptr cconvert; int ci; cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_deconverter)); cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert; cconvert->pub.start_pass = start_pass_dcolor; /* Make sure num_components agrees with jpeg_color_space */ switch (cinfo->jpeg_color_space) { case JCS_GRAYSCALE: if (cinfo->num_components != 1) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); break; case JCS_RGB: case JCS_YCbCr: if (cinfo->num_components != 3) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); break; case JCS_CMYK: case JCS_YCCK: if (cinfo->num_components != 4) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); break; default: /* JCS_UNKNOWN can be anything */ if (cinfo->num_components < 1) ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); break; } /* Set out_color_components and conversion method based on requested space. * Also clear the component_needed flags for any unused components, * so that earlier pipeline stages can avoid useless computation. */ switch (cinfo->out_color_space) { case JCS_GRAYSCALE: cinfo->out_color_components = 1; if (cinfo->jpeg_color_space == JCS_GRAYSCALE || cinfo->jpeg_color_space == JCS_YCbCr) { cconvert->pub.color_convert = grayscale_convert; /* For color->grayscale conversion, only the Y (0) component is needed */ for (ci = 1; ci < cinfo->num_components; ci++) cinfo->comp_info[ci].component_needed = FALSE; } else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_RGB: case JCS_EXT_RGB: case JCS_EXT_RGBX: case JCS_EXT_BGR: case JCS_EXT_BGRX: case JCS_EXT_XBGR: case JCS_EXT_XRGB: case JCS_EXT_RGBA: case JCS_EXT_BGRA: case JCS_EXT_ABGR: case JCS_EXT_ARGB: cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; if (cinfo->jpeg_color_space == JCS_YCbCr) { if (jsimd_can_ycc_rgb()) cconvert->pub.color_convert = jsimd_ycc_rgb_convert; else { cconvert->pub.color_convert = ycc_rgb_convert; build_ycc_rgb_table(cinfo); } } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { cconvert->pub.color_convert = gray_rgb_convert; } else if (cinfo->jpeg_color_space == cinfo->out_color_space && rgb_pixelsize[cinfo->out_color_space] == 3) { cconvert->pub.color_convert = null_convert; } else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; case JCS_CMYK: cinfo->out_color_components = 4; if (cinfo->jpeg_color_space == JCS_YCCK) { cconvert->pub.color_convert = ycck_cmyk_convert; build_ycc_rgb_table(cinfo); } else if (cinfo->jpeg_color_space == JCS_CMYK) { cconvert->pub.color_convert = null_convert; } else ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; default: /* Permit null conversion to same output space */ if (cinfo->out_color_space == cinfo->jpeg_color_space) { cinfo->out_color_components = cinfo->num_components; cconvert->pub.color_convert = null_convert; } else /* unsupported non-null conversion */ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); break; } if (cinfo->quantize_colors) cinfo->output_components = 1; /* single colormapped output component */ else cinfo->output_components = cinfo->out_color_components; } glmark2-2012.08/./src/libjpeg-turbo/jquant2.c0000664000175000017500000013602712013417376020013 0ustar alfalf00000000000000/* * jquant2.c * * Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 2009, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains 2-pass color quantization (color mapping) routines. * These routines provide selection of a custom color map for an image, * followed by mapping of the image to that color map, with optional * Floyd-Steinberg dithering. * It is also possible to use just the second pass to map to an arbitrary * externally-given color map. * * Note: ordered dithering is not supported, since there isn't any fast * way to compute intercolor distances; it's unclear that ordered dither's * fundamental assumptions even hold with an irregularly spaced color map. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #ifdef QUANT_2PASS_SUPPORTED /* * This module implements the well-known Heckbert paradigm for color * quantization. Most of the ideas used here can be traced back to * Heckbert's seminal paper * Heckbert, Paul. "Color Image Quantization for Frame Buffer Display", * Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304. * * In the first pass over the image, we accumulate a histogram showing the * usage count of each possible color. To keep the histogram to a reasonable * size, we reduce the precision of the input; typical practice is to retain * 5 or 6 bits per color, so that 8 or 4 different input values are counted * in the same histogram cell. * * Next, the color-selection step begins with a box representing the whole * color space, and repeatedly splits the "largest" remaining box until we * have as many boxes as desired colors. Then the mean color in each * remaining box becomes one of the possible output colors. * * The second pass over the image maps each input pixel to the closest output * color (optionally after applying a Floyd-Steinberg dithering correction). * This mapping is logically trivial, but making it go fast enough requires * considerable care. * * Heckbert-style quantizers vary a good deal in their policies for choosing * the "largest" box and deciding where to cut it. The particular policies * used here have proved out well in experimental comparisons, but better ones * may yet be found. * * In earlier versions of the IJG code, this module quantized in YCbCr color * space, processing the raw upsampled data without a color conversion step. * This allowed the color conversion math to be done only once per colormap * entry, not once per pixel. However, that optimization precluded other * useful optimizations (such as merging color conversion with upsampling) * and it also interfered with desired capabilities such as quantizing to an * externally-supplied colormap. We have therefore abandoned that approach. * The present code works in the post-conversion color space, typically RGB. * * To improve the visual quality of the results, we actually work in scaled * RGB space, giving G distances more weight than R, and R in turn more than * B. To do everything in integer math, we must use integer scale factors. * The 2/3/1 scale factors used here correspond loosely to the relative * weights of the colors in the NTSC grayscale equation. * If you want to use this code to quantize a non-RGB color space, you'll * probably need to change these scale factors. */ #define R_SCALE 2 /* scale R distances by this much */ #define G_SCALE 3 /* scale G distances by this much */ #define B_SCALE 1 /* and B by this much */ static const int c_scales[3]={R_SCALE, G_SCALE, B_SCALE}; #define C0_SCALE c_scales[rgb_red[cinfo->out_color_space]] #define C1_SCALE c_scales[rgb_green[cinfo->out_color_space]] #define C2_SCALE c_scales[rgb_blue[cinfo->out_color_space]] /* * First we have the histogram data structure and routines for creating it. * * The number of bits of precision can be adjusted by changing these symbols. * We recommend keeping 6 bits for G and 5 each for R and B. * If you have plenty of memory and cycles, 6 bits all around gives marginally * better results; if you are short of memory, 5 bits all around will save * some space but degrade the results. * To maintain a fully accurate histogram, we'd need to allocate a "long" * (preferably unsigned long) for each cell. In practice this is overkill; * we can get by with 16 bits per cell. Few of the cell counts will overflow, * and clamping those that do overflow to the maximum value will give close- * enough results. This reduces the recommended histogram size from 256Kb * to 128Kb, which is a useful savings on PC-class machines. * (In the second pass the histogram space is re-used for pixel mapping data; * in that capacity, each cell must be able to store zero to the number of * desired colors. 16 bits/cell is plenty for that too.) * Since the JPEG code is intended to run in small memory model on 80x86 * machines, we can't just allocate the histogram in one chunk. Instead * of a true 3-D array, we use a row of pointers to 2-D arrays. Each * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that * on 80x86 machines, the pointer row is in near memory but the actual * arrays are in far memory (same arrangement as we use for image arrays). */ #define MAXNUMCOLORS (MAXJSAMPLE+1) /* maximum size of colormap */ /* These will do the right thing for either R,G,B or B,G,R color order, * but you may not like the results for other color orders. */ #define HIST_C0_BITS 5 /* bits of precision in R/B histogram */ #define HIST_C1_BITS 6 /* bits of precision in G histogram */ #define HIST_C2_BITS 5 /* bits of precision in B/R histogram */ /* Number of elements along histogram axes. */ #define HIST_C0_ELEMS (1<cquantize; register JSAMPROW ptr; register histptr histp; register hist3d histogram = cquantize->histogram; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; for (row = 0; row < num_rows; row++) { ptr = input_buf[row]; for (col = width; col > 0; col--) { /* get pixel value and index into the histogram */ histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT] [GETJSAMPLE(ptr[1]) >> C1_SHIFT] [GETJSAMPLE(ptr[2]) >> C2_SHIFT]; /* increment, check for overflow and undo increment if so. */ if (++(*histp) <= 0) (*histp)--; ptr += 3; } } } /* * Next we have the really interesting routines: selection of a colormap * given the completed histogram. * These routines work with a list of "boxes", each representing a rectangular * subset of the input color space (to histogram precision). */ typedef struct { /* The bounds of the box (inclusive); expressed as histogram indexes */ int c0min, c0max; int c1min, c1max; int c2min, c2max; /* The volume (actually 2-norm) of the box */ INT32 volume; /* The number of nonzero histogram cells within this box */ long colorcount; } box; typedef box * boxptr; LOCAL(boxptr) find_biggest_color_pop (boxptr boxlist, int numboxes) /* Find the splittable box with the largest color population */ /* Returns NULL if no splittable boxes remain */ { register boxptr boxp; register int i; register long maxc = 0; boxptr which = NULL; for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { if (boxp->colorcount > maxc && boxp->volume > 0) { which = boxp; maxc = boxp->colorcount; } } return which; } LOCAL(boxptr) find_biggest_volume (boxptr boxlist, int numboxes) /* Find the splittable box with the largest (scaled) volume */ /* Returns NULL if no splittable boxes remain */ { register boxptr boxp; register int i; register INT32 maxv = 0; boxptr which = NULL; for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { if (boxp->volume > maxv) { which = boxp; maxv = boxp->volume; } } return which; } LOCAL(void) update_box (j_decompress_ptr cinfo, boxptr boxp) /* Shrink the min/max bounds of a box to enclose only nonzero elements, */ /* and recompute its volume and population */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; histptr histp; int c0,c1,c2; int c0min,c0max,c1min,c1max,c2min,c2max; INT32 dist0,dist1,dist2; long ccount; c0min = boxp->c0min; c0max = boxp->c0max; c1min = boxp->c1min; c1max = boxp->c1max; c2min = boxp->c2min; c2max = boxp->c2max; if (c0max > c0min) for (c0 = c0min; c0 <= c0max; c0++) for (c1 = c1min; c1 <= c1max; c1++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++) if (*histp++ != 0) { boxp->c0min = c0min = c0; goto have_c0min; } } have_c0min: if (c0max > c0min) for (c0 = c0max; c0 >= c0min; c0--) for (c1 = c1min; c1 <= c1max; c1++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++) if (*histp++ != 0) { boxp->c0max = c0max = c0; goto have_c0max; } } have_c0max: if (c1max > c1min) for (c1 = c1min; c1 <= c1max; c1++) for (c0 = c0min; c0 <= c0max; c0++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++) if (*histp++ != 0) { boxp->c1min = c1min = c1; goto have_c1min; } } have_c1min: if (c1max > c1min) for (c1 = c1max; c1 >= c1min; c1--) for (c0 = c0min; c0 <= c0max; c0++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++) if (*histp++ != 0) { boxp->c1max = c1max = c1; goto have_c1max; } } have_c1max: if (c2max > c2min) for (c2 = c2min; c2 <= c2max; c2++) for (c0 = c0min; c0 <= c0max; c0++) { histp = & histogram[c0][c1min][c2]; for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS) if (*histp != 0) { boxp->c2min = c2min = c2; goto have_c2min; } } have_c2min: if (c2max > c2min) for (c2 = c2max; c2 >= c2min; c2--) for (c0 = c0min; c0 <= c0max; c0++) { histp = & histogram[c0][c1min][c2]; for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS) if (*histp != 0) { boxp->c2max = c2max = c2; goto have_c2max; } } have_c2max: /* Update box volume. * We use 2-norm rather than real volume here; this biases the method * against making long narrow boxes, and it has the side benefit that * a box is splittable iff norm > 0. * Since the differences are expressed in histogram-cell units, * we have to shift back to JSAMPLE units to get consistent distances; * after which, we scale according to the selected distance scale factors. */ dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE; dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE; dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE; boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2; /* Now scan remaining volume of box and compute population */ ccount = 0; for (c0 = c0min; c0 <= c0max; c0++) for (c1 = c1min; c1 <= c1max; c1++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++, histp++) if (*histp != 0) { ccount++; } } boxp->colorcount = ccount; } LOCAL(int) median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors) /* Repeatedly select and split the largest box until we have enough boxes */ { int n,lb; int c0,c1,c2,cmax; register boxptr b1,b2; while (numboxes < desired_colors) { /* Select box to split. * Current algorithm: by population for first half, then by volume. */ if (numboxes*2 <= desired_colors) { b1 = find_biggest_color_pop(boxlist, numboxes); } else { b1 = find_biggest_volume(boxlist, numboxes); } if (b1 == NULL) /* no splittable boxes left! */ break; b2 = &boxlist[numboxes]; /* where new box will go */ /* Copy the color bounds to the new box. */ b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max; b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min; /* Choose which axis to split the box on. * Current algorithm: longest scaled axis. * See notes in update_box about scaling distances. */ c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE; c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE; c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE; /* We want to break any ties in favor of green, then red, blue last. * This code does the right thing for R,G,B or B,G,R color orders only. */ if (rgb_red[cinfo->out_color_space] == 0) { cmax = c1; n = 1; if (c0 > cmax) { cmax = c0; n = 0; } if (c2 > cmax) { n = 2; } } else { cmax = c1; n = 1; if (c2 > cmax) { cmax = c2; n = 2; } if (c0 > cmax) { n = 0; } } /* Choose split point along selected axis, and update box bounds. * Current algorithm: split at halfway point. * (Since the box has been shrunk to minimum volume, * any split will produce two nonempty subboxes.) * Note that lb value is max for lower box, so must be < old max. */ switch (n) { case 0: lb = (b1->c0max + b1->c0min) / 2; b1->c0max = lb; b2->c0min = lb+1; break; case 1: lb = (b1->c1max + b1->c1min) / 2; b1->c1max = lb; b2->c1min = lb+1; break; case 2: lb = (b1->c2max + b1->c2min) / 2; b1->c2max = lb; b2->c2min = lb+1; break; } /* Update stats for boxes */ update_box(cinfo, b1); update_box(cinfo, b2); numboxes++; } return numboxes; } LOCAL(void) compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor) /* Compute representative color for a box, put it in colormap[icolor] */ { /* Current algorithm: mean weighted by pixels (not colors) */ /* Note it is important to get the rounding correct! */ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; histptr histp; int c0,c1,c2; int c0min,c0max,c1min,c1max,c2min,c2max; long count; long total = 0; long c0total = 0; long c1total = 0; long c2total = 0; c0min = boxp->c0min; c0max = boxp->c0max; c1min = boxp->c1min; c1max = boxp->c1max; c2min = boxp->c2min; c2max = boxp->c2max; for (c0 = c0min; c0 <= c0max; c0++) for (c1 = c1min; c1 <= c1max; c1++) { histp = & histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++) { if ((count = *histp++) != 0) { total += count; c0total += ((c0 << C0_SHIFT) + ((1<>1)) * count; c1total += ((c1 << C1_SHIFT) + ((1<>1)) * count; c2total += ((c2 << C2_SHIFT) + ((1<>1)) * count; } } } cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total); cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total); cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total); } LOCAL(void) select_colors (j_decompress_ptr cinfo, int desired_colors) /* Master routine for color selection */ { boxptr boxlist; int numboxes; int i; /* Allocate workspace for box list */ boxlist = (boxptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box)); /* Initialize one box containing whole space */ numboxes = 1; boxlist[0].c0min = 0; boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT; boxlist[0].c1min = 0; boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT; boxlist[0].c2min = 0; boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT; /* Shrink it to actually-used volume and set its statistics */ update_box(cinfo, & boxlist[0]); /* Perform median-cut to produce final box list */ numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors); /* Compute the representative color for each box, fill colormap */ for (i = 0; i < numboxes; i++) compute_color(cinfo, & boxlist[i], i); cinfo->actual_number_of_colors = numboxes; TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes); } /* * These routines are concerned with the time-critical task of mapping input * colors to the nearest color in the selected colormap. * * We re-use the histogram space as an "inverse color map", essentially a * cache for the results of nearest-color searches. All colors within a * histogram cell will be mapped to the same colormap entry, namely the one * closest to the cell's center. This may not be quite the closest entry to * the actual input color, but it's almost as good. A zero in the cache * indicates we haven't found the nearest color for that cell yet; the array * is cleared to zeroes before starting the mapping pass. When we find the * nearest color for a cell, its colormap index plus one is recorded in the * cache for future use. The pass2 scanning routines call fill_inverse_cmap * when they need to use an unfilled entry in the cache. * * Our method of efficiently finding nearest colors is based on the "locally * sorted search" idea described by Heckbert and on the incremental distance * calculation described by Spencer W. Thomas in chapter III.1 of Graphics * Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that * the distances from a given colormap entry to each cell of the histogram can * be computed quickly using an incremental method: the differences between * distances to adjacent cells themselves differ by a constant. This allows a * fairly fast implementation of the "brute force" approach of computing the * distance from every colormap entry to every histogram cell. Unfortunately, * it needs a work array to hold the best-distance-so-far for each histogram * cell (because the inner loop has to be over cells, not colormap entries). * The work array elements have to be INT32s, so the work array would need * 256Kb at our recommended precision. This is not feasible in DOS machines. * * To get around these problems, we apply Thomas' method to compute the * nearest colors for only the cells within a small subbox of the histogram. * The work array need be only as big as the subbox, so the memory usage * problem is solved. Furthermore, we need not fill subboxes that are never * referenced in pass2; many images use only part of the color gamut, so a * fair amount of work is saved. An additional advantage of this * approach is that we can apply Heckbert's locality criterion to quickly * eliminate colormap entries that are far away from the subbox; typically * three-fourths of the colormap entries are rejected by Heckbert's criterion, * and we need not compute their distances to individual cells in the subbox. * The speed of this approach is heavily influenced by the subbox size: too * small means too much overhead, too big loses because Heckbert's criterion * can't eliminate as many colormap entries. Empirically the best subbox * size seems to be about 1/512th of the histogram (1/8th in each direction). * * Thomas' article also describes a refined method which is asymptotically * faster than the brute-force method, but it is also far more complex and * cannot efficiently be applied to small subboxes. It is therefore not * useful for programs intended to be portable to DOS machines. On machines * with plenty of memory, filling the whole histogram in one shot with Thomas' * refined method might be faster than the present code --- but then again, * it might not be any faster, and it's certainly more complicated. */ /* log2(histogram cells in update box) for each axis; this can be adjusted */ #define BOX_C0_LOG (HIST_C0_BITS-3) #define BOX_C1_LOG (HIST_C1_BITS-3) #define BOX_C2_LOG (HIST_C2_BITS-3) #define BOX_C0_ELEMS (1<actual_number_of_colors; int maxc0, maxc1, maxc2; int centerc0, centerc1, centerc2; int i, x, ncolors; INT32 minmaxdist, min_dist, max_dist, tdist; INT32 mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */ /* Compute true coordinates of update box's upper corner and center. * Actually we compute the coordinates of the center of the upper-corner * histogram cell, which are the upper bounds of the volume we care about. * Note that since ">>" rounds down, the "center" values may be closer to * min than to max; hence comparisons to them must be "<=", not "<". */ maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT)); centerc0 = (minc0 + maxc0) >> 1; maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT)); centerc1 = (minc1 + maxc1) >> 1; maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT)); centerc2 = (minc2 + maxc2) >> 1; /* For each color in colormap, find: * 1. its minimum squared-distance to any point in the update box * (zero if color is within update box); * 2. its maximum squared-distance to any point in the update box. * Both of these can be found by considering only the corners of the box. * We save the minimum distance for each color in mindist[]; * only the smallest maximum distance is of interest. */ minmaxdist = 0x7FFFFFFFL; for (i = 0; i < numcolors; i++) { /* We compute the squared-c0-distance term, then add in the other two. */ x = GETJSAMPLE(cinfo->colormap[0][i]); if (x < minc0) { tdist = (x - minc0) * C0_SCALE; min_dist = tdist*tdist; tdist = (x - maxc0) * C0_SCALE; max_dist = tdist*tdist; } else if (x > maxc0) { tdist = (x - maxc0) * C0_SCALE; min_dist = tdist*tdist; tdist = (x - minc0) * C0_SCALE; max_dist = tdist*tdist; } else { /* within cell range so no contribution to min_dist */ min_dist = 0; if (x <= centerc0) { tdist = (x - maxc0) * C0_SCALE; max_dist = tdist*tdist; } else { tdist = (x - minc0) * C0_SCALE; max_dist = tdist*tdist; } } x = GETJSAMPLE(cinfo->colormap[1][i]); if (x < minc1) { tdist = (x - minc1) * C1_SCALE; min_dist += tdist*tdist; tdist = (x - maxc1) * C1_SCALE; max_dist += tdist*tdist; } else if (x > maxc1) { tdist = (x - maxc1) * C1_SCALE; min_dist += tdist*tdist; tdist = (x - minc1) * C1_SCALE; max_dist += tdist*tdist; } else { /* within cell range so no contribution to min_dist */ if (x <= centerc1) { tdist = (x - maxc1) * C1_SCALE; max_dist += tdist*tdist; } else { tdist = (x - minc1) * C1_SCALE; max_dist += tdist*tdist; } } x = GETJSAMPLE(cinfo->colormap[2][i]); if (x < minc2) { tdist = (x - minc2) * C2_SCALE; min_dist += tdist*tdist; tdist = (x - maxc2) * C2_SCALE; max_dist += tdist*tdist; } else if (x > maxc2) { tdist = (x - maxc2) * C2_SCALE; min_dist += tdist*tdist; tdist = (x - minc2) * C2_SCALE; max_dist += tdist*tdist; } else { /* within cell range so no contribution to min_dist */ if (x <= centerc2) { tdist = (x - maxc2) * C2_SCALE; max_dist += tdist*tdist; } else { tdist = (x - minc2) * C2_SCALE; max_dist += tdist*tdist; } } mindist[i] = min_dist; /* save away the results */ if (max_dist < minmaxdist) minmaxdist = max_dist; } /* Now we know that no cell in the update box is more than minmaxdist * away from some colormap entry. Therefore, only colors that are * within minmaxdist of some part of the box need be considered. */ ncolors = 0; for (i = 0; i < numcolors; i++) { if (mindist[i] <= minmaxdist) colorlist[ncolors++] = (JSAMPLE) i; } return ncolors; } LOCAL(void) find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[]) /* Find the closest colormap entry for each cell in the update box, * given the list of candidate colors prepared by find_nearby_colors. * Return the indexes of the closest entries in the bestcolor[] array. * This routine uses Thomas' incremental distance calculation method to * find the distance from a colormap entry to successive cells in the box. */ { int ic0, ic1, ic2; int i, icolor; register INT32 * bptr; /* pointer into bestdist[] array */ JSAMPLE * cptr; /* pointer into bestcolor[] array */ INT32 dist0, dist1; /* initial distance values */ register INT32 dist2; /* current distance in inner loop */ INT32 xx0, xx1; /* distance increments */ register INT32 xx2; INT32 inc0, inc1, inc2; /* initial values for increments */ /* This array holds the distance to the nearest-so-far color for each cell */ INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS]; /* Initialize best-distance for each cell of the update box */ bptr = bestdist; for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--) *bptr++ = 0x7FFFFFFFL; /* For each color selected by find_nearby_colors, * compute its distance to the center of each cell in the box. * If that's less than best-so-far, update best distance and color number. */ /* Nominal steps between cell centers ("x" in Thomas article) */ #define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE) #define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE) #define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE) for (i = 0; i < numcolors; i++) { icolor = GETJSAMPLE(colorlist[i]); /* Compute (square of) distance from minc0/c1/c2 to this color */ inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE; dist0 = inc0*inc0; inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE; dist0 += inc1*inc1; inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE; dist0 += inc2*inc2; /* Form the initial difference increments */ inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0; inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1; inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2; /* Now loop over all cells in box, updating distance per Thomas method */ bptr = bestdist; cptr = bestcolor; xx0 = inc0; for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) { dist1 = dist0; xx1 = inc1; for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) { dist2 = dist1; xx2 = inc2; for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) { if (dist2 < *bptr) { *bptr = dist2; *cptr = (JSAMPLE) icolor; } dist2 += xx2; xx2 += 2 * STEP_C2 * STEP_C2; bptr++; cptr++; } dist1 += xx1; xx1 += 2 * STEP_C1 * STEP_C1; } dist0 += xx0; xx0 += 2 * STEP_C0 * STEP_C0; } } } LOCAL(void) fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2) /* Fill the inverse-colormap entries in the update box that contains */ /* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */ /* we can fill as many others as we wish.) */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; int minc0, minc1, minc2; /* lower left corner of update box */ int ic0, ic1, ic2; register JSAMPLE * cptr; /* pointer into bestcolor[] array */ register histptr cachep; /* pointer into main cache array */ /* This array lists the candidate colormap indexes. */ JSAMPLE colorlist[MAXNUMCOLORS]; int numcolors; /* number of candidate colors */ /* This array holds the actually closest colormap index for each cell. */ JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS]; /* Convert cell coordinates to update box ID */ c0 >>= BOX_C0_LOG; c1 >>= BOX_C1_LOG; c2 >>= BOX_C2_LOG; /* Compute true coordinates of update box's origin corner. * Actually we compute the coordinates of the center of the corner * histogram cell, which are the lower bounds of the volume we care about. */ minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1); minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1); minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1); /* Determine which colormap entries are close enough to be candidates * for the nearest entry to some cell in the update box. */ numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist); /* Determine the actually nearest colors. */ find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist, bestcolor); /* Save the best color numbers (plus 1) in the main cache array */ c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */ c1 <<= BOX_C1_LOG; c2 <<= BOX_C2_LOG; cptr = bestcolor; for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) { for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) { cachep = & histogram[c0+ic0][c1+ic1][c2]; for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) { *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1); } } } } /* * Map some rows of pixels to the output colormapped representation. */ METHODDEF(void) pass2_no_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* This version performs no dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; register JSAMPROW inptr, outptr; register histptr cachep; register int c0, c1, c2; int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; for (row = 0; row < num_rows; row++) { inptr = input_buf[row]; outptr = output_buf[row]; for (col = width; col > 0; col--) { /* get pixel value and index into the cache */ c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT; c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT; c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT; cachep = & histogram[c0][c1][c2]; /* If we have not seen this color before, find nearest colormap entry */ /* and update the cache */ if (*cachep == 0) fill_inverse_cmap(cinfo, c0,c1,c2); /* Now emit the colormap index for this cell */ *outptr++ = (JSAMPLE) (*cachep - 1); } } } METHODDEF(void) pass2_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows) /* This version performs Floyd-Steinberg dithering */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */ LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */ LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */ register FSERRPTR errorptr; /* => fserrors[] at column before current */ JSAMPROW inptr; /* => current input pixel */ JSAMPROW outptr; /* => current output pixel */ histptr cachep; int dir; /* +1 or -1 depending on direction */ int dir3; /* 3*dir, for advancing inptr & errorptr */ int row; JDIMENSION col; JDIMENSION width = cinfo->output_width; JSAMPLE *range_limit = cinfo->sample_range_limit; int *error_limit = cquantize->error_limiter; JSAMPROW colormap0 = cinfo->colormap[0]; JSAMPROW colormap1 = cinfo->colormap[1]; JSAMPROW colormap2 = cinfo->colormap[2]; SHIFT_TEMPS for (row = 0; row < num_rows; row++) { inptr = input_buf[row]; outptr = output_buf[row]; if (cquantize->on_odd_row) { /* work right to left in this row */ inptr += (width-1) * 3; /* so point to rightmost pixel */ outptr += width-1; dir = -1; dir3 = -3; errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */ cquantize->on_odd_row = FALSE; /* flip for next time */ } else { /* work left to right in this row */ dir = 1; dir3 = 3; errorptr = cquantize->fserrors; /* => entry before first real column */ cquantize->on_odd_row = TRUE; /* flip for next time */ } /* Preset error values: no error propagated to first pixel from left */ cur0 = cur1 = cur2 = 0; /* and no error propagated to row below yet */ belowerr0 = belowerr1 = belowerr2 = 0; bpreverr0 = bpreverr1 = bpreverr2 = 0; for (col = width; col > 0; col--) { /* curN holds the error propagated from the previous pixel on the * current line. Add the error propagated from the previous line * to form the complete error correction term for this pixel, and * round the error term (which is expressed * 16) to an integer. * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct * for either sign of the error value. * Note: errorptr points to *previous* column's array entry. */ cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4); cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4); cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4); /* Limit the error using transfer function set by init_error_limit. * See comments with init_error_limit for rationale. */ cur0 = error_limit[cur0]; cur1 = error_limit[cur1]; cur2 = error_limit[cur2]; /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE. * The maximum error is +- MAXJSAMPLE (or less with error limiting); * this sets the required size of the range_limit array. */ cur0 += GETJSAMPLE(inptr[0]); cur1 += GETJSAMPLE(inptr[1]); cur2 += GETJSAMPLE(inptr[2]); cur0 = GETJSAMPLE(range_limit[cur0]); cur1 = GETJSAMPLE(range_limit[cur1]); cur2 = GETJSAMPLE(range_limit[cur2]); /* Index into the cache with adjusted pixel value */ cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT]; /* If we have not seen this color before, find nearest colormap */ /* entry and update the cache */ if (*cachep == 0) fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT); /* Now emit the colormap index for this cell */ { register int pixcode = *cachep - 1; *outptr = (JSAMPLE) pixcode; /* Compute representation error for this pixel */ cur0 -= GETJSAMPLE(colormap0[pixcode]); cur1 -= GETJSAMPLE(colormap1[pixcode]); cur2 -= GETJSAMPLE(colormap2[pixcode]); } /* Compute error fractions to be propagated to adjacent pixels. * Add these into the running sums, and simultaneously shift the * next-line error sums left by 1 column. */ { register LOCFSERROR bnexterr, delta; bnexterr = cur0; /* Process component 0 */ delta = cur0 * 2; cur0 += delta; /* form error * 3 */ errorptr[0] = (FSERROR) (bpreverr0 + cur0); cur0 += delta; /* form error * 5 */ bpreverr0 = belowerr0 + cur0; belowerr0 = bnexterr; cur0 += delta; /* form error * 7 */ bnexterr = cur1; /* Process component 1 */ delta = cur1 * 2; cur1 += delta; /* form error * 3 */ errorptr[1] = (FSERROR) (bpreverr1 + cur1); cur1 += delta; /* form error * 5 */ bpreverr1 = belowerr1 + cur1; belowerr1 = bnexterr; cur1 += delta; /* form error * 7 */ bnexterr = cur2; /* Process component 2 */ delta = cur2 * 2; cur2 += delta; /* form error * 3 */ errorptr[2] = (FSERROR) (bpreverr2 + cur2); cur2 += delta; /* form error * 5 */ bpreverr2 = belowerr2 + cur2; belowerr2 = bnexterr; cur2 += delta; /* form error * 7 */ } /* At this point curN contains the 7/16 error value to be propagated * to the next pixel on the current line, and all the errors for the * next line have been shifted over. We are therefore ready to move on. */ inptr += dir3; /* Advance pixel pointers to next column */ outptr += dir; errorptr += dir3; /* advance errorptr to current column */ } /* Post-loop cleanup: we must unload the final error values into the * final fserrors[] entry. Note we need not unload belowerrN because * it is for the dummy column before or after the actual array. */ errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */ errorptr[1] = (FSERROR) bpreverr1; errorptr[2] = (FSERROR) bpreverr2; } } /* * Initialize the error-limiting transfer function (lookup table). * The raw F-S error computation can potentially compute error values of up to * +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be * much less, otherwise obviously wrong pixels will be created. (Typical * effects include weird fringes at color-area boundaries, isolated bright * pixels in a dark area, etc.) The standard advice for avoiding this problem * is to ensure that the "corners" of the color cube are allocated as output * colors; then repeated errors in the same direction cannot cause cascading * error buildup. However, that only prevents the error from getting * completely out of hand; Aaron Giles reports that error limiting improves * the results even with corner colors allocated. * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty * well, but the smoother transfer function used below is even better. Thanks * to Aaron Giles for this idea. */ LOCAL(void) init_error_limit (j_decompress_ptr cinfo) /* Allocate and fill in the error_limiter table */ { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; int * table; int in, out; table = (int *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int)); table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */ cquantize->error_limiter = table; #define STEPSIZE ((MAXJSAMPLE+1)/16) /* Map errors 1:1 up to +- MAXJSAMPLE/16 */ out = 0; for (in = 0; in < STEPSIZE; in++, out++) { table[in] = out; table[-in] = -out; } /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */ for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) { table[in] = out; table[-in] = -out; } /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */ for (; in <= MAXJSAMPLE; in++) { table[in] = out; table[-in] = -out; } #undef STEPSIZE } /* * Finish up at the end of each pass. */ METHODDEF(void) finish_pass1 (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; /* Select the representative colors and fill in cinfo->colormap */ cinfo->colormap = cquantize->sv_colormap; select_colors(cinfo, cquantize->desired); /* Force next pass to zero the color index table */ cquantize->needs_zeroed = TRUE; } METHODDEF(void) finish_pass2 (j_decompress_ptr cinfo) { /* no work */ } /* * Initialize for each processing pass. */ METHODDEF(void) start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; int i; /* Only F-S dithering or no dithering is supported. */ /* If user asks for ordered dither, give him F-S. */ if (cinfo->dither_mode != JDITHER_NONE) cinfo->dither_mode = JDITHER_FS; if (is_pre_scan) { /* Set up method pointers */ cquantize->pub.color_quantize = prescan_quantize; cquantize->pub.finish_pass = finish_pass1; cquantize->needs_zeroed = TRUE; /* Always zero histogram */ } else { /* Set up method pointers */ if (cinfo->dither_mode == JDITHER_FS) cquantize->pub.color_quantize = pass2_fs_dither; else cquantize->pub.color_quantize = pass2_no_dither; cquantize->pub.finish_pass = finish_pass2; /* Make sure color count is acceptable */ i = cinfo->actual_number_of_colors; if (i < 1) ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1); if (i > MAXNUMCOLORS) ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS); if (cinfo->dither_mode == JDITHER_FS) { size_t arraysize = (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))); /* Allocate Floyd-Steinberg workspace if we didn't already. */ if (cquantize->fserrors == NULL) cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize); /* Initialize the propagated errors to zero. */ jzero_far((void FAR *) cquantize->fserrors, arraysize); /* Make the error-limit table if we didn't already. */ if (cquantize->error_limiter == NULL) init_error_limit(cinfo); cquantize->on_odd_row = FALSE; } } /* Zero the histogram or inverse color map, if necessary */ if (cquantize->needs_zeroed) { for (i = 0; i < HIST_C0_ELEMS; i++) { jzero_far((void FAR *) histogram[i], HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell)); } cquantize->needs_zeroed = FALSE; } } /* * Switch to a new external colormap between output passes. */ METHODDEF(void) new_color_map_2_quant (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; /* Reset the inverse color map */ cquantize->needs_zeroed = TRUE; } /* * Module initialization routine for 2-pass color quantization. */ GLOBAL(void) jinit_2pass_quantizer (j_decompress_ptr cinfo) { my_cquantize_ptr cquantize; int i; cquantize = (my_cquantize_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_cquantizer)); cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize; cquantize->pub.start_pass = start_pass_2_quant; cquantize->pub.new_color_map = new_color_map_2_quant; cquantize->fserrors = NULL; /* flag optional arrays not allocated */ cquantize->error_limiter = NULL; /* Make sure jdmaster didn't give me a case I can't handle */ if (cinfo->out_color_components != 3) ERREXIT(cinfo, JERR_NOTIMPL); /* Allocate the histogram/inverse colormap storage */ cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d)); for (i = 0; i < HIST_C0_ELEMS; i++) { cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell)); } cquantize->needs_zeroed = TRUE; /* histogram is garbage now */ /* Allocate storage for the completed colormap, if required. * We do this now since it is FAR storage and may affect * the memory manager's space calculations. */ if (cinfo->enable_2pass_quant) { /* Make sure color count is acceptable */ int desired = cinfo->desired_number_of_colors; /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */ if (desired < 8) ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8); /* Make sure colormap indexes can be represented by JSAMPLEs */ if (desired > MAXNUMCOLORS) ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS); cquantize->sv_colormap = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3); cquantize->desired = desired; } else cquantize->sv_colormap = NULL; /* Only F-S dithering or no dithering is supported. */ /* If user asks for ordered dither, give him F-S. */ if (cinfo->dither_mode != JDITHER_NONE) cinfo->dither_mode = JDITHER_FS; /* Allocate Floyd-Steinberg workspace if necessary. * This isn't really needed until pass 2, but again it is FAR storage. * Although we will cope with a later change in dither_mode, * we do not promise to honor max_memory_to_use if dither_mode changes. */ if (cinfo->dither_mode == JDITHER_FS) { cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR)))); /* Might as well create the error-limiting table too. */ init_error_limit(cinfo); } } #endif /* QUANT_2PASS_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jcmarker.c0000664000175000017500000004163312013417376020223 0ustar alfalf00000000000000/* * jcmarker.c * * Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains routines to write JPEG datastream markers. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" typedef enum { /* JPEG marker codes */ M_SOF0 = 0xc0, M_SOF1 = 0xc1, M_SOF2 = 0xc2, M_SOF3 = 0xc3, M_SOF5 = 0xc5, M_SOF6 = 0xc6, M_SOF7 = 0xc7, M_JPG = 0xc8, M_SOF9 = 0xc9, M_SOF10 = 0xca, M_SOF11 = 0xcb, M_SOF13 = 0xcd, M_SOF14 = 0xce, M_SOF15 = 0xcf, M_DHT = 0xc4, M_DAC = 0xcc, M_RST0 = 0xd0, M_RST1 = 0xd1, M_RST2 = 0xd2, M_RST3 = 0xd3, M_RST4 = 0xd4, M_RST5 = 0xd5, M_RST6 = 0xd6, M_RST7 = 0xd7, M_SOI = 0xd8, M_EOI = 0xd9, M_SOS = 0xda, M_DQT = 0xdb, M_DNL = 0xdc, M_DRI = 0xdd, M_DHP = 0xde, M_EXP = 0xdf, M_APP0 = 0xe0, M_APP1 = 0xe1, M_APP2 = 0xe2, M_APP3 = 0xe3, M_APP4 = 0xe4, M_APP5 = 0xe5, M_APP6 = 0xe6, M_APP7 = 0xe7, M_APP8 = 0xe8, M_APP9 = 0xe9, M_APP10 = 0xea, M_APP11 = 0xeb, M_APP12 = 0xec, M_APP13 = 0xed, M_APP14 = 0xee, M_APP15 = 0xef, M_JPG0 = 0xf0, M_JPG13 = 0xfd, M_COM = 0xfe, M_TEM = 0x01, M_ERROR = 0x100 } JPEG_MARKER; /* Private state */ typedef struct { struct jpeg_marker_writer pub; /* public fields */ unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */ } my_marker_writer; typedef my_marker_writer * my_marker_ptr; /* * Basic output routines. * * Note that we do not support suspension while writing a marker. * Therefore, an application using suspension must ensure that there is * enough buffer space for the initial markers (typ. 600-700 bytes) before * calling jpeg_start_compress, and enough space to write the trailing EOI * (a few bytes) before calling jpeg_finish_compress. Multipass compression * modes are not supported at all with suspension, so those two are the only * points where markers will be written. */ LOCAL(void) emit_byte (j_compress_ptr cinfo, int val) /* Emit a byte */ { struct jpeg_destination_mgr * dest = cinfo->dest; *(dest->next_output_byte)++ = (JOCTET) val; if (--dest->free_in_buffer == 0) { if (! (*dest->empty_output_buffer) (cinfo)) ERREXIT(cinfo, JERR_CANT_SUSPEND); } } LOCAL(void) emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark) /* Emit a marker code */ { emit_byte(cinfo, 0xFF); emit_byte(cinfo, (int) mark); } LOCAL(void) emit_2bytes (j_compress_ptr cinfo, int value) /* Emit a 2-byte integer; these are always MSB first in JPEG files */ { emit_byte(cinfo, (value >> 8) & 0xFF); emit_byte(cinfo, value & 0xFF); } /* * Routines to write specific marker types. */ LOCAL(int) emit_dqt (j_compress_ptr cinfo, int index) /* Emit a DQT marker */ /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */ { JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index]; int prec; int i; if (qtbl == NULL) ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index); prec = 0; for (i = 0; i < DCTSIZE2; i++) { if (qtbl->quantval[i] > 255) prec = 1; } if (! qtbl->sent_table) { emit_marker(cinfo, M_DQT); emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2); emit_byte(cinfo, index + (prec<<4)); for (i = 0; i < DCTSIZE2; i++) { /* The table entries must be emitted in zigzag order. */ unsigned int qval = qtbl->quantval[jpeg_natural_order[i]]; if (prec) emit_byte(cinfo, (int) (qval >> 8)); emit_byte(cinfo, (int) (qval & 0xFF)); } qtbl->sent_table = TRUE; } return prec; } LOCAL(void) emit_dht (j_compress_ptr cinfo, int index, boolean is_ac) /* Emit a DHT marker */ { JHUFF_TBL * htbl; int length, i; if (is_ac) { htbl = cinfo->ac_huff_tbl_ptrs[index]; index += 0x10; /* output index has AC bit set */ } else { htbl = cinfo->dc_huff_tbl_ptrs[index]; } if (htbl == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index); if (! htbl->sent_table) { emit_marker(cinfo, M_DHT); length = 0; for (i = 1; i <= 16; i++) length += htbl->bits[i]; emit_2bytes(cinfo, length + 2 + 1 + 16); emit_byte(cinfo, index); for (i = 1; i <= 16; i++) emit_byte(cinfo, htbl->bits[i]); for (i = 0; i < length; i++) emit_byte(cinfo, htbl->huffval[i]); htbl->sent_table = TRUE; } } LOCAL(void) emit_dac (j_compress_ptr cinfo) /* Emit a DAC marker */ /* Since the useful info is so small, we want to emit all the tables in */ /* one DAC marker. Therefore this routine does its own scan of the table. */ { #ifdef C_ARITH_CODING_SUPPORTED char dc_in_use[NUM_ARITH_TBLS]; char ac_in_use[NUM_ARITH_TBLS]; int length, i; jpeg_component_info *compptr; for (i = 0; i < NUM_ARITH_TBLS; i++) dc_in_use[i] = ac_in_use[i] = 0; for (i = 0; i < cinfo->comps_in_scan; i++) { compptr = cinfo->cur_comp_info[i]; dc_in_use[compptr->dc_tbl_no] = 1; ac_in_use[compptr->ac_tbl_no] = 1; } length = 0; for (i = 0; i < NUM_ARITH_TBLS; i++) length += dc_in_use[i] + ac_in_use[i]; emit_marker(cinfo, M_DAC); emit_2bytes(cinfo, length*2 + 2); for (i = 0; i < NUM_ARITH_TBLS; i++) { if (dc_in_use[i]) { emit_byte(cinfo, i); emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4)); } if (ac_in_use[i]) { emit_byte(cinfo, i + 0x10); emit_byte(cinfo, cinfo->arith_ac_K[i]); } } #endif /* C_ARITH_CODING_SUPPORTED */ } LOCAL(void) emit_dri (j_compress_ptr cinfo) /* Emit a DRI marker */ { emit_marker(cinfo, M_DRI); emit_2bytes(cinfo, 4); /* fixed length */ emit_2bytes(cinfo, (int) cinfo->restart_interval); } LOCAL(void) emit_sof (j_compress_ptr cinfo, JPEG_MARKER code) /* Emit a SOF marker */ { int ci; jpeg_component_info *compptr; emit_marker(cinfo, code); emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */ /* Make sure image isn't bigger than SOF field can handle */ if ((long) cinfo->_jpeg_height > 65535L || (long) cinfo->_jpeg_width > 65535L) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535); emit_byte(cinfo, cinfo->data_precision); emit_2bytes(cinfo, (int) cinfo->_jpeg_height); emit_2bytes(cinfo, (int) cinfo->_jpeg_width); emit_byte(cinfo, cinfo->num_components); for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { emit_byte(cinfo, compptr->component_id); emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor); emit_byte(cinfo, compptr->quant_tbl_no); } } LOCAL(void) emit_sos (j_compress_ptr cinfo) /* Emit a SOS marker */ { int i, td, ta; jpeg_component_info *compptr; emit_marker(cinfo, M_SOS); emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */ emit_byte(cinfo, cinfo->comps_in_scan); for (i = 0; i < cinfo->comps_in_scan; i++) { compptr = cinfo->cur_comp_info[i]; emit_byte(cinfo, compptr->component_id); td = compptr->dc_tbl_no; ta = compptr->ac_tbl_no; if (cinfo->progressive_mode) { /* Progressive mode: only DC or only AC tables are used in one scan; * furthermore, Huffman coding of DC refinement uses no table at all. * We emit 0 for unused field(s); this is recommended by the P&M text * but does not seem to be specified in the standard. */ if (cinfo->Ss == 0) { ta = 0; /* DC scan */ if (cinfo->Ah != 0 && !cinfo->arith_code) td = 0; /* no DC table either */ } else { td = 0; /* AC scan */ } } emit_byte(cinfo, (td << 4) + ta); } emit_byte(cinfo, cinfo->Ss); emit_byte(cinfo, cinfo->Se); emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al); } LOCAL(void) emit_jfif_app0 (j_compress_ptr cinfo) /* Emit a JFIF-compliant APP0 marker */ { /* * Length of APP0 block (2 bytes) * Block ID (4 bytes - ASCII "JFIF") * Zero byte (1 byte to terminate the ID string) * Version Major, Minor (2 bytes - major first) * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm) * Xdpu (2 bytes - dots per unit horizontal) * Ydpu (2 bytes - dots per unit vertical) * Thumbnail X size (1 byte) * Thumbnail Y size (1 byte) */ emit_marker(cinfo, M_APP0); emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */ emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */ emit_byte(cinfo, 0x46); emit_byte(cinfo, 0x49); emit_byte(cinfo, 0x46); emit_byte(cinfo, 0); emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */ emit_byte(cinfo, cinfo->JFIF_minor_version); emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */ emit_2bytes(cinfo, (int) cinfo->X_density); emit_2bytes(cinfo, (int) cinfo->Y_density); emit_byte(cinfo, 0); /* No thumbnail image */ emit_byte(cinfo, 0); } LOCAL(void) emit_adobe_app14 (j_compress_ptr cinfo) /* Emit an Adobe APP14 marker */ { /* * Length of APP14 block (2 bytes) * Block ID (5 bytes - ASCII "Adobe") * Version Number (2 bytes - currently 100) * Flags0 (2 bytes - currently 0) * Flags1 (2 bytes - currently 0) * Color transform (1 byte) * * Although Adobe TN 5116 mentions Version = 101, all the Adobe files * now in circulation seem to use Version = 100, so that's what we write. * * We write the color transform byte as 1 if the JPEG color space is * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with * whether the encoder performed a transformation, which is pretty useless. */ emit_marker(cinfo, M_APP14); emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */ emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */ emit_byte(cinfo, 0x64); emit_byte(cinfo, 0x6F); emit_byte(cinfo, 0x62); emit_byte(cinfo, 0x65); emit_2bytes(cinfo, 100); /* Version */ emit_2bytes(cinfo, 0); /* Flags0 */ emit_2bytes(cinfo, 0); /* Flags1 */ switch (cinfo->jpeg_color_space) { case JCS_YCbCr: emit_byte(cinfo, 1); /* Color transform = 1 */ break; case JCS_YCCK: emit_byte(cinfo, 2); /* Color transform = 2 */ break; default: emit_byte(cinfo, 0); /* Color transform = 0 */ break; } } /* * These routines allow writing an arbitrary marker with parameters. * The only intended use is to emit COM or APPn markers after calling * write_file_header and before calling write_frame_header. * Other uses are not guaranteed to produce desirable results. * Counting the parameter bytes properly is the caller's responsibility. */ METHODDEF(void) write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen) /* Emit an arbitrary marker header */ { if (datalen > (unsigned int) 65533) /* safety check */ ERREXIT(cinfo, JERR_BAD_LENGTH); emit_marker(cinfo, (JPEG_MARKER) marker); emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */ } METHODDEF(void) write_marker_byte (j_compress_ptr cinfo, int val) /* Emit one byte of marker parameters following write_marker_header */ { emit_byte(cinfo, val); } /* * Write datastream header. * This consists of an SOI and optional APPn markers. * We recommend use of the JFIF marker, but not the Adobe marker, * when using YCbCr or grayscale data. The JFIF marker should NOT * be used for any other JPEG colorspace. The Adobe marker is helpful * to distinguish RGB, CMYK, and YCCK colorspaces. * Note that an application can write additional header markers after * jpeg_start_compress returns. */ METHODDEF(void) write_file_header (j_compress_ptr cinfo) { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; emit_marker(cinfo, M_SOI); /* first the SOI */ /* SOI is defined to reset restart interval to 0 */ marker->last_restart_interval = 0; if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */ emit_jfif_app0(cinfo); if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */ emit_adobe_app14(cinfo); } /* * Write frame header. * This consists of DQT and SOFn markers. * Note that we do not emit the SOF until we have emitted the DQT(s). * This avoids compatibility problems with incorrect implementations that * try to error-check the quant table numbers as soon as they see the SOF. */ METHODDEF(void) write_frame_header (j_compress_ptr cinfo) { int ci, prec; boolean is_baseline; jpeg_component_info *compptr; /* Emit DQT for each quantization table. * Note that emit_dqt() suppresses any duplicate tables. */ prec = 0; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { prec += emit_dqt(cinfo, compptr->quant_tbl_no); } /* now prec is nonzero iff there are any 16-bit quant tables. */ /* Check for a non-baseline specification. * Note we assume that Huffman table numbers won't be changed later. */ if (cinfo->arith_code || cinfo->progressive_mode || cinfo->data_precision != 8) { is_baseline = FALSE; } else { is_baseline = TRUE; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1) is_baseline = FALSE; } if (prec && is_baseline) { is_baseline = FALSE; /* If it's baseline except for quantizer size, warn the user */ TRACEMS(cinfo, 0, JTRC_16BIT_TABLES); } } /* Emit the proper SOF marker */ if (cinfo->arith_code) { emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */ } else { if (cinfo->progressive_mode) emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */ else if (is_baseline) emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */ else emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */ } } /* * Write scan header. * This consists of DHT or DAC markers, optional DRI, and SOS. * Compressed data will be written following the SOS. */ METHODDEF(void) write_scan_header (j_compress_ptr cinfo) { my_marker_ptr marker = (my_marker_ptr) cinfo->marker; int i; jpeg_component_info *compptr; if (cinfo->arith_code) { /* Emit arith conditioning info. We may have some duplication * if the file has multiple scans, but it's so small it's hardly * worth worrying about. */ emit_dac(cinfo); } else { /* Emit Huffman tables. * Note that emit_dht() suppresses any duplicate tables. */ for (i = 0; i < cinfo->comps_in_scan; i++) { compptr = cinfo->cur_comp_info[i]; if (cinfo->progressive_mode) { /* Progressive mode: only DC or only AC tables are used in one scan */ if (cinfo->Ss == 0) { if (cinfo->Ah == 0) /* DC needs no table for refinement scan */ emit_dht(cinfo, compptr->dc_tbl_no, FALSE); } else { emit_dht(cinfo, compptr->ac_tbl_no, TRUE); } } else { /* Sequential mode: need both DC and AC tables */ emit_dht(cinfo, compptr->dc_tbl_no, FALSE); emit_dht(cinfo, compptr->ac_tbl_no, TRUE); } } } /* Emit DRI if required --- note that DRI value could change for each scan. * We avoid wasting space with unnecessary DRIs, however. */ if (cinfo->restart_interval != marker->last_restart_interval) { emit_dri(cinfo); marker->last_restart_interval = cinfo->restart_interval; } emit_sos(cinfo); } /* * Write datastream trailer. */ METHODDEF(void) write_file_trailer (j_compress_ptr cinfo) { emit_marker(cinfo, M_EOI); } /* * Write an abbreviated table-specification datastream. * This consists of SOI, DQT and DHT tables, and EOI. * Any table that is defined and not marked sent_table = TRUE will be * emitted. Note that all tables will be marked sent_table = TRUE at exit. */ METHODDEF(void) write_tables_only (j_compress_ptr cinfo) { int i; emit_marker(cinfo, M_SOI); for (i = 0; i < NUM_QUANT_TBLS; i++) { if (cinfo->quant_tbl_ptrs[i] != NULL) (void) emit_dqt(cinfo, i); } if (! cinfo->arith_code) { for (i = 0; i < NUM_HUFF_TBLS; i++) { if (cinfo->dc_huff_tbl_ptrs[i] != NULL) emit_dht(cinfo, i, FALSE); if (cinfo->ac_huff_tbl_ptrs[i] != NULL) emit_dht(cinfo, i, TRUE); } } emit_marker(cinfo, M_EOI); } /* * Initialize the marker writer module. */ GLOBAL(void) jinit_marker_writer (j_compress_ptr cinfo) { my_marker_ptr marker; /* Create the subobject */ marker = (my_marker_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_marker_writer)); cinfo->marker = (struct jpeg_marker_writer *) marker; /* Initialize method pointers */ marker->pub.write_file_header = write_file_header; marker->pub.write_frame_header = write_frame_header; marker->pub.write_scan_header = write_scan_header; marker->pub.write_file_trailer = write_file_trailer; marker->pub.write_tables_only = write_tables_only; marker->pub.write_marker_header = write_marker_header; marker->pub.write_marker_byte = write_marker_byte; /* Initialize private state */ marker->last_restart_interval = 0; } glmark2-2012.08/./src/libjpeg-turbo/jsimd.h0000664000175000017500000001002712013417376017531 0ustar alfalf00000000000000/* * jsimd.h * * Copyright 2009 Pierre Ossman for Cendio AB * Copyright 2011 D. R. Commander * * Based on the x86 SIMD extension for IJG JPEG library, * Copyright (C) 1999-2006, MIYASAKA Masaru. * For conditions of distribution and use, see copyright notice in jsimdext.inc * */ /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jsimd_can_rgb_ycc jSCanRgbYcc #define jsimd_can_rgb_gray jSCanRgbGry #define jsimd_can_ycc_rgb jSCanYccRgb #define jsimd_rgb_ycc_convert jSRgbYccConv #define jsimd_rgb_gray_convert jSRgbGryConv #define jsimd_ycc_rgb_convert jSYccRgbConv #define jsimd_can_h2v2_downsample jSCanH2V2Down #define jsimd_can_h2v1_downsample jSCanH2V1Down #define jsimd_h2v2_downsample jSH2V2Down #define jsimd_h2v1_downsample jSH2V1Down #define jsimd_can_h2v2_upsample jSCanH2V2Up #define jsimd_can_h2v1_upsample jSCanH2V1Up #define jsimd_h2v2_upsample jSH2V2Up #define jsimd_h2v1_upsample jSH2V1Up #define jsimd_can_h2v2_fancy_upsample jSCanH2V2FUp #define jsimd_can_h2v1_fancy_upsample jSCanH2V1FUp #define jsimd_h2v2_fancy_upsample jSH2V2FUp #define jsimd_h2v1_fancy_upsample jSH2V1FUp #define jsimd_can_h2v2_merged_upsample jSCanH2V2MUp #define jsimd_can_h2v1_merged_upsample jSCanH2V1MUp #define jsimd_h2v2_merged_upsample jSH2V2MUp #define jsimd_h2v1_merged_upsample jSH2V1MUp #endif /* NEED_SHORT_EXTERNAL_NAMES */ EXTERN(int) jsimd_can_rgb_ycc JPP((void)); EXTERN(int) jsimd_can_rgb_gray JPP((void)); EXTERN(int) jsimd_can_ycc_rgb JPP((void)); EXTERN(void) jsimd_rgb_ycc_convert JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_rgb_gray_convert JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_ycc_rgb_convert JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(int) jsimd_can_h2v2_downsample JPP((void)); EXTERN(int) jsimd_can_h2v1_downsample JPP((void)); EXTERN(void) jsimd_h2v2_downsample JPP((j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)); EXTERN(void) jsimd_h2v1_downsample JPP((j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data)); EXTERN(int) jsimd_can_h2v2_upsample JPP((void)); EXTERN(int) jsimd_can_h2v1_upsample JPP((void)); EXTERN(void) jsimd_h2v2_upsample JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_upsample JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(int) jsimd_can_h2v2_fancy_upsample JPP((void)); EXTERN(int) jsimd_can_h2v1_fancy_upsample JPP((void)); EXTERN(void) jsimd_h2v2_fancy_upsample JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_fancy_upsample JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(int) jsimd_can_h2v2_merged_upsample JPP((void)); EXTERN(int) jsimd_can_h2v1_merged_upsample JPP((void)); EXTERN(void) jsimd_h2v2_merged_upsample JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_merged_upsample JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); glmark2-2012.08/./src/libjpeg-turbo/jddctmgr.c0000664000175000017500000002116512013417376020221 0ustar alfalf00000000000000/* * jddctmgr.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Copyright 2009 Pierre Ossman for Cendio AB * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the inverse-DCT management logic. * This code selects a particular IDCT implementation to be used, * and it performs related housekeeping chores. No code in this file * is executed per IDCT step, only during output pass setup. * * Note that the IDCT routines are responsible for performing coefficient * dequantization as well as the IDCT proper. This module sets up the * dequantization multiplier table needed by the IDCT routine. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #include "jsimddct.h" #include "jpegcomp.h" /* * The decompressor input side (jdinput.c) saves away the appropriate * quantization table for each component at the start of the first scan * involving that component. (This is necessary in order to correctly * decode files that reuse Q-table slots.) * When we are ready to make an output pass, the saved Q-table is converted * to a multiplier table that will actually be used by the IDCT routine. * The multiplier table contents are IDCT-method-dependent. To support * application changes in IDCT method between scans, we can remake the * multiplier tables if necessary. * In buffered-image mode, the first output pass may occur before any data * has been seen for some components, and thus before their Q-tables have * been saved away. To handle this case, multiplier tables are preset * to zeroes; the result of the IDCT will be a neutral gray level. */ /* Private subobject for this module */ typedef struct { struct jpeg_inverse_dct pub; /* public fields */ /* This array contains the IDCT method code that each multiplier table * is currently set up for, or -1 if it's not yet set up. * The actual multiplier tables are pointed to by dct_table in the * per-component comp_info structures. */ int cur_method[MAX_COMPONENTS]; } my_idct_controller; typedef my_idct_controller * my_idct_ptr; /* Allocated multiplier tables: big enough for any supported variant */ typedef union { ISLOW_MULT_TYPE islow_array[DCTSIZE2]; #ifdef DCT_IFAST_SUPPORTED IFAST_MULT_TYPE ifast_array[DCTSIZE2]; #endif #ifdef DCT_FLOAT_SUPPORTED FLOAT_MULT_TYPE float_array[DCTSIZE2]; #endif } multiplier_table; /* The current scaled-IDCT routines require ISLOW-style multiplier tables, * so be sure to compile that code if either ISLOW or SCALING is requested. */ #ifdef DCT_ISLOW_SUPPORTED #define PROVIDE_ISLOW_TABLES #else #ifdef IDCT_SCALING_SUPPORTED #define PROVIDE_ISLOW_TABLES #endif #endif /* * Prepare for an output pass. * Here we select the proper IDCT routine for each component and build * a matching multiplier table. */ METHODDEF(void) start_pass (j_decompress_ptr cinfo) { my_idct_ptr idct = (my_idct_ptr) cinfo->idct; int ci, i; jpeg_component_info *compptr; int method = 0; inverse_DCT_method_ptr method_ptr = NULL; JQUANT_TBL * qtbl; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Select the proper IDCT routine for this component's scaling */ switch (compptr->_DCT_scaled_size) { #ifdef IDCT_SCALING_SUPPORTED case 1: method_ptr = jpeg_idct_1x1; method = JDCT_ISLOW; /* jidctred uses islow-style table */ break; case 2: if (jsimd_can_idct_2x2()) method_ptr = jsimd_idct_2x2; else method_ptr = jpeg_idct_2x2; method = JDCT_ISLOW; /* jidctred uses islow-style table */ break; case 4: if (jsimd_can_idct_4x4()) method_ptr = jsimd_idct_4x4; else method_ptr = jpeg_idct_4x4; method = JDCT_ISLOW; /* jidctred uses islow-style table */ break; #endif case DCTSIZE: switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: if (jsimd_can_idct_islow()) method_ptr = jsimd_idct_islow; else method_ptr = jpeg_idct_islow; method = JDCT_ISLOW; break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: if (jsimd_can_idct_ifast()) method_ptr = jsimd_idct_ifast; else method_ptr = jpeg_idct_ifast; method = JDCT_IFAST; break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: if (jsimd_can_idct_float()) method_ptr = jsimd_idct_float; else method_ptr = jpeg_idct_float; method = JDCT_FLOAT; break; #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } break; default: ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size); break; } idct->pub.inverse_DCT[ci] = method_ptr; /* Create multiplier table from quant table. * However, we can skip this if the component is uninteresting * or if we already built the table. Also, if no quant table * has yet been saved for the component, we leave the * multiplier table all-zero; we'll be reading zeroes from the * coefficient controller's buffer anyway. */ if (! compptr->component_needed || idct->cur_method[ci] == method) continue; qtbl = compptr->quant_table; if (qtbl == NULL) /* happens if no data yet for component */ continue; idct->cur_method[ci] = method; switch (method) { #ifdef PROVIDE_ISLOW_TABLES case JDCT_ISLOW: { /* For LL&M IDCT method, multipliers are equal to raw quantization * coefficients, but are stored as ints to ensure access efficiency. */ ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table; for (i = 0; i < DCTSIZE2; i++) { ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i]; } } break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: { /* For AA&N IDCT method, multipliers are equal to quantization * coefficients scaled by scalefactor[row]*scalefactor[col], where * scalefactor[0] = 1 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 * For integer operation, the multiplier table is to be scaled by * IFAST_SCALE_BITS. */ IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table; #define CONST_BITS 14 static const INT16 aanscales[DCTSIZE2] = { /* precomputed values scaled up by 14 bits */ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 }; SHIFT_TEMPS for (i = 0; i < DCTSIZE2; i++) { ifmtbl[i] = (IFAST_MULT_TYPE) DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], (INT32) aanscales[i]), CONST_BITS-IFAST_SCALE_BITS); } } break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: { /* For float AA&N IDCT method, multipliers are equal to quantization * coefficients scaled by scalefactor[row]*scalefactor[col], where * scalefactor[0] = 1 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 */ FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; int row, col; static const double aanscalefactor[DCTSIZE] = { 1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 }; i = 0; for (row = 0; row < DCTSIZE; row++) { for (col = 0; col < DCTSIZE; col++) { fmtbl[i] = (FLOAT_MULT_TYPE) ((double) qtbl->quantval[i] * aanscalefactor[row] * aanscalefactor[col]); i++; } } } break; #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); break; } } } /* * Initialize IDCT manager. */ GLOBAL(void) jinit_inverse_dct (j_decompress_ptr cinfo) { my_idct_ptr idct; int ci; jpeg_component_info *compptr; idct = (my_idct_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_idct_controller)); cinfo->idct = (struct jpeg_inverse_dct *) idct; idct->pub.start_pass = start_pass; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Allocate and pre-zero a multiplier table for each component */ compptr->dct_table = (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(multiplier_table)); MEMZERO(compptr->dct_table, SIZEOF(multiplier_table)); /* Mark multiplier table not yet set up for any method */ idct->cur_method[ci] = -1; } } glmark2-2012.08/./src/libjpeg-turbo/jdmrgext.c.inc0000664000175000017500000001252312013417376021015 0ustar alfalf00000000000000/* * jdmrgext.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains code for merged upsampling/color conversion. */ /* This file is included by jdmerge.c */ /* * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. */ INLINE LOCAL(void) h2v1_merged_upsample_internal (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; register int y, cred, cgreen, cblue; int cb, cr; register JSAMPROW outptr; JSAMPROW inptr0, inptr1, inptr2; JDIMENSION col; /* copy these pointers into registers if possible */ register JSAMPLE * range_limit = cinfo->sample_range_limit; int * Crrtab = upsample->Cr_r_tab; int * Cbbtab = upsample->Cb_b_tab; INT32 * Crgtab = upsample->Cr_g_tab; INT32 * Cbgtab = upsample->Cb_g_tab; SHIFT_TEMPS inptr0 = input_buf[0][in_row_group_ctr]; inptr1 = input_buf[1][in_row_group_ctr]; inptr2 = input_buf[2][in_row_group_ctr]; outptr = output_buf[0]; /* Loop for each pair of output pixels */ for (col = cinfo->output_width >> 1; col > 0; col--) { /* Do the chroma part of the calculation */ cb = GETJSAMPLE(*inptr1++); cr = GETJSAMPLE(*inptr2++); cred = Crrtab[cr]; cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); cblue = Cbbtab[cb]; /* Fetch 2 Y values and emit 2 pixels */ y = GETJSAMPLE(*inptr0++); outptr[RGB_RED] = range_limit[y + cred]; outptr[RGB_GREEN] = range_limit[y + cgreen]; outptr[RGB_BLUE] = range_limit[y + cblue]; outptr += RGB_PIXELSIZE; y = GETJSAMPLE(*inptr0++); outptr[RGB_RED] = range_limit[y + cred]; outptr[RGB_GREEN] = range_limit[y + cgreen]; outptr[RGB_BLUE] = range_limit[y + cblue]; outptr += RGB_PIXELSIZE; } /* If image width is odd, do the last output column separately */ if (cinfo->output_width & 1) { cb = GETJSAMPLE(*inptr1); cr = GETJSAMPLE(*inptr2); cred = Crrtab[cr]; cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); cblue = Cbbtab[cb]; y = GETJSAMPLE(*inptr0); outptr[RGB_RED] = range_limit[y + cred]; outptr[RGB_GREEN] = range_limit[y + cgreen]; outptr[RGB_BLUE] = range_limit[y + cblue]; } } /* * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. */ INLINE LOCAL(void) h2v2_merged_upsample_internal (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; register int y, cred, cgreen, cblue; int cb, cr; register JSAMPROW outptr0, outptr1; JSAMPROW inptr00, inptr01, inptr1, inptr2; JDIMENSION col; /* copy these pointers into registers if possible */ register JSAMPLE * range_limit = cinfo->sample_range_limit; int * Crrtab = upsample->Cr_r_tab; int * Cbbtab = upsample->Cb_b_tab; INT32 * Crgtab = upsample->Cr_g_tab; INT32 * Cbgtab = upsample->Cb_g_tab; SHIFT_TEMPS inptr00 = input_buf[0][in_row_group_ctr*2]; inptr01 = input_buf[0][in_row_group_ctr*2 + 1]; inptr1 = input_buf[1][in_row_group_ctr]; inptr2 = input_buf[2][in_row_group_ctr]; outptr0 = output_buf[0]; outptr1 = output_buf[1]; /* Loop for each group of output pixels */ for (col = cinfo->output_width >> 1; col > 0; col--) { /* Do the chroma part of the calculation */ cb = GETJSAMPLE(*inptr1++); cr = GETJSAMPLE(*inptr2++); cred = Crrtab[cr]; cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); cblue = Cbbtab[cb]; /* Fetch 4 Y values and emit 4 pixels */ y = GETJSAMPLE(*inptr00++); outptr0[RGB_RED] = range_limit[y + cred]; outptr0[RGB_GREEN] = range_limit[y + cgreen]; outptr0[RGB_BLUE] = range_limit[y + cblue]; outptr0 += RGB_PIXELSIZE; y = GETJSAMPLE(*inptr00++); outptr0[RGB_RED] = range_limit[y + cred]; outptr0[RGB_GREEN] = range_limit[y + cgreen]; outptr0[RGB_BLUE] = range_limit[y + cblue]; outptr0 += RGB_PIXELSIZE; y = GETJSAMPLE(*inptr01++); outptr1[RGB_RED] = range_limit[y + cred]; outptr1[RGB_GREEN] = range_limit[y + cgreen]; outptr1[RGB_BLUE] = range_limit[y + cblue]; outptr1 += RGB_PIXELSIZE; y = GETJSAMPLE(*inptr01++); outptr1[RGB_RED] = range_limit[y + cred]; outptr1[RGB_GREEN] = range_limit[y + cgreen]; outptr1[RGB_BLUE] = range_limit[y + cblue]; outptr1 += RGB_PIXELSIZE; } /* If image width is odd, do the last output column separately */ if (cinfo->output_width & 1) { cb = GETJSAMPLE(*inptr1); cr = GETJSAMPLE(*inptr2); cred = Crrtab[cr]; cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); cblue = Cbbtab[cb]; y = GETJSAMPLE(*inptr00); outptr0[RGB_RED] = range_limit[y + cred]; outptr0[RGB_GREEN] = range_limit[y + cgreen]; outptr0[RGB_BLUE] = range_limit[y + cblue]; y = GETJSAMPLE(*inptr01); outptr1[RGB_RED] = range_limit[y + cred]; outptr1[RGB_GREEN] = range_limit[y + cgreen]; outptr1[RGB_BLUE] = range_limit[y + cblue]; } } glmark2-2012.08/./src/libjpeg-turbo/jmorecfg.h0000664000175000017500000003271612013417376020230 0ustar alfalf00000000000000/* * jmorecfg.h * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2009, 2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains additional configuration options that customize the * JPEG software for special applications or support machine-dependent * optimizations. Most users will not need to touch this file. */ /* * Define BITS_IN_JSAMPLE as either * 8 for 8-bit sample values (the usual setting) * 12 for 12-bit sample values * Only 8 and 12 are legal data precisions for lossy JPEG according to the * JPEG standard, and the IJG code does not support anything else! * We do not support run-time selection of data precision, sorry. */ #define BITS_IN_JSAMPLE 8 /* use 8 or 12 */ /* * Maximum number of components (color channels) allowed in JPEG image. * To meet the letter of the JPEG spec, set this to 255. However, darn * few applications need more than 4 channels (maybe 5 for CMYK + alpha * mask). We recommend 10 as a reasonable compromise; use 4 if you are * really short on memory. (Each allowed component costs a hundred or so * bytes of storage, whether actually used in an image or not.) */ #define MAX_COMPONENTS 10 /* maximum number of image components */ /* * Basic data types. * You may need to change these if you have a machine with unusual data * type sizes; for example, "char" not 8 bits, "short" not 16 bits, * or "long" not 32 bits. We don't care whether "int" is 16 or 32 bits, * but it had better be at least 16. */ /* Representation of a single sample (pixel element value). * We frequently allocate large arrays of these, so it's important to keep * them small. But if you have memory to burn and access to char or short * arrays is very slow on your hardware, you might want to change these. */ #if BITS_IN_JSAMPLE == 8 /* JSAMPLE should be the smallest type that will hold the values 0..255. * You can use a signed char by having GETJSAMPLE mask it with 0xFF. */ #ifdef HAVE_UNSIGNED_CHAR typedef unsigned char JSAMPLE; #define GETJSAMPLE(value) ((int) (value)) #else /* not HAVE_UNSIGNED_CHAR */ typedef char JSAMPLE; #ifdef __CHAR_UNSIGNED__ #define GETJSAMPLE(value) ((int) (value)) #else #define GETJSAMPLE(value) ((int) (value) & 0xFF) #endif /* __CHAR_UNSIGNED__ */ #endif /* HAVE_UNSIGNED_CHAR */ #define MAXJSAMPLE 255 #define CENTERJSAMPLE 128 #endif /* BITS_IN_JSAMPLE == 8 */ #if BITS_IN_JSAMPLE == 12 /* JSAMPLE should be the smallest type that will hold the values 0..4095. * On nearly all machines "short" will do nicely. */ typedef short JSAMPLE; #define GETJSAMPLE(value) ((int) (value)) #define MAXJSAMPLE 4095 #define CENTERJSAMPLE 2048 #endif /* BITS_IN_JSAMPLE == 12 */ /* Representation of a DCT frequency coefficient. * This should be a signed value of at least 16 bits; "short" is usually OK. * Again, we allocate large arrays of these, but you can change to int * if you have memory to burn and "short" is really slow. */ typedef short JCOEF; /* Compressed datastreams are represented as arrays of JOCTET. * These must be EXACTLY 8 bits wide, at least once they are written to * external storage. Note that when using the stdio data source/destination * managers, this is also the data type passed to fread/fwrite. */ #ifdef HAVE_UNSIGNED_CHAR typedef unsigned char JOCTET; #define GETJOCTET(value) (value) #else /* not HAVE_UNSIGNED_CHAR */ typedef char JOCTET; #ifdef __CHAR_UNSIGNED__ #define GETJOCTET(value) (value) #else #define GETJOCTET(value) ((value) & 0xFF) #endif /* __CHAR_UNSIGNED__ */ #endif /* HAVE_UNSIGNED_CHAR */ /* These typedefs are used for various table entries and so forth. * They must be at least as wide as specified; but making them too big * won't cost a huge amount of memory, so we don't provide special * extraction code like we did for JSAMPLE. (In other words, these * typedefs live at a different point on the speed/space tradeoff curve.) */ /* UINT8 must hold at least the values 0..255. */ #ifdef HAVE_UNSIGNED_CHAR typedef unsigned char UINT8; #else /* not HAVE_UNSIGNED_CHAR */ #ifdef __CHAR_UNSIGNED__ typedef char UINT8; #else /* not __CHAR_UNSIGNED__ */ typedef short UINT8; #endif /* __CHAR_UNSIGNED__ */ #endif /* HAVE_UNSIGNED_CHAR */ /* UINT16 must hold at least the values 0..65535. */ #ifdef HAVE_UNSIGNED_SHORT typedef unsigned short UINT16; #else /* not HAVE_UNSIGNED_SHORT */ typedef unsigned int UINT16; #endif /* HAVE_UNSIGNED_SHORT */ /* INT16 must hold at least the values -32768..32767. */ #ifndef XMD_H /* X11/xmd.h correctly defines INT16 */ typedef short INT16; #endif /* INT32 must hold at least signed 32-bit values. */ #ifndef XMD_H /* X11/xmd.h correctly defines INT32 */ typedef long INT32; #endif /* Datatype used for image dimensions. The JPEG standard only supports * images up to 64K*64K due to 16-bit fields in SOF markers. Therefore * "unsigned int" is sufficient on all machines. However, if you need to * handle larger images and you don't mind deviating from the spec, you * can change this datatype. */ typedef unsigned int JDIMENSION; #define JPEG_MAX_DIMENSION 65500L /* a tad under 64K to prevent overflows */ /* These macros are used in all function definitions and extern declarations. * You could modify them if you need to change function linkage conventions; * in particular, you'll need to do that to make the library a Windows DLL. * Another application is to make all functions global for use with debuggers * or code profilers that require it. */ /* a function called through method pointers: */ #define METHODDEF(type) static type /* a function used only in its module: */ #define LOCAL(type) static type /* a function referenced thru EXTERNs: */ #define GLOBAL(type) type /* a reference to a GLOBAL function: */ #define EXTERN(type) extern type /* This macro is used to declare a "method", that is, a function pointer. * We want to supply prototype parameters if the compiler can cope. * Note that the arglist parameter must be parenthesized! * Again, you can customize this if you need special linkage keywords. */ #ifdef HAVE_PROTOTYPES #define JMETHOD(type,methodname,arglist) type (*methodname) arglist #else #define JMETHOD(type,methodname,arglist) type (*methodname) () #endif /* Here is the pseudo-keyword for declaring pointers that must be "far" * on 80x86 machines. Most of the specialized coding for 80x86 is handled * by just saying "FAR *" where such a pointer is needed. In a few places * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol. */ #ifdef NEED_FAR_POINTERS #define FAR far #else #define FAR #endif /* * On a few systems, type boolean and/or its values FALSE, TRUE may appear * in standard header files. Or you may have conflicts with application- * specific header files that you want to include together with these files. * Defining HAVE_BOOLEAN before including jpeglib.h should make it work. */ #ifndef HAVE_BOOLEAN typedef int boolean; #endif #ifndef FALSE /* in case these macros already exist */ #define FALSE 0 /* values of boolean */ #endif #ifndef TRUE #define TRUE 1 #endif /* * The remaining options affect code selection within the JPEG library, * but they don't need to be visible to most applications using the library. * To minimize application namespace pollution, the symbols won't be * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined. */ #ifdef JPEG_INTERNALS #define JPEG_INTERNAL_OPTIONS #endif #ifdef JPEG_INTERNAL_OPTIONS /* * These defines indicate whether to include various optional functions. * Undefining some of these symbols will produce a smaller but less capable * library. Note that you can leave certain source files out of the * compilation/linking process if you've #undef'd the corresponding symbols. * (You may HAVE to do that if your compiler doesn't like null source files.) */ /* Capability options common to encoder and decoder: */ #define DCT_ISLOW_SUPPORTED /* slow but accurate integer algorithm */ #define DCT_IFAST_SUPPORTED /* faster, less accurate integer method */ #define DCT_FLOAT_SUPPORTED /* floating-point: accurate, fast on fast HW */ /* Encoder capability options: */ #define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ #define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ #define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */ /* Note: if you selected 12-bit data precision, it is dangerous to turn off * ENTROPY_OPT_SUPPORTED. The standard Huffman tables are only good for 8-bit * precision, so jchuff.c normally uses entropy optimization to compute * usable tables for higher precision. If you don't want to do optimization, * you'll have to supply different default Huffman tables. * The exact same statements apply for progressive JPEG: the default tables * don't work for progressive mode. (This may get fixed, however.) */ #define INPUT_SMOOTHING_SUPPORTED /* Input image smoothing option? */ /* Decoder capability options: */ #define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ #define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ #define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */ #define BLOCK_SMOOTHING_SUPPORTED /* Block smoothing? (Progressive only) */ #define IDCT_SCALING_SUPPORTED /* Output rescaling via IDCT? */ #undef UPSAMPLE_SCALING_SUPPORTED /* Output rescaling at upsample stage? */ #define UPSAMPLE_MERGING_SUPPORTED /* Fast path for sloppy upsampling? */ #define QUANT_1PASS_SUPPORTED /* 1-pass color quantization? */ #define QUANT_2PASS_SUPPORTED /* 2-pass color quantization? */ /* more capability options later, no doubt */ /* * Ordering of RGB data in scanlines passed to or from the application. * If your application wants to deal with data in the order B,G,R, just * change these macros. You can also deal with formats such as R,G,B,X * (one extra byte per pixel) by changing RGB_PIXELSIZE. Note that changing * the offsets will also change the order in which colormap data is organized. * RESTRICTIONS: * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats. * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not * useful if you are using JPEG color spaces other than YCbCr or grayscale. * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE * is not 3 (they don't understand about dummy color components!). So you * can't use color quantization if you change that value. */ #define RGB_RED 0 /* Offset of Red in an RGB scanline element */ #define RGB_GREEN 1 /* Offset of Green */ #define RGB_BLUE 2 /* Offset of Blue */ #define RGB_PIXELSIZE 3 /* JSAMPLEs per RGB scanline element */ #define JPEG_NUMCS 16 #define EXT_RGB_RED 0 #define EXT_RGB_GREEN 1 #define EXT_RGB_BLUE 2 #define EXT_RGB_PIXELSIZE 3 #define EXT_RGBX_RED 0 #define EXT_RGBX_GREEN 1 #define EXT_RGBX_BLUE 2 #define EXT_RGBX_PIXELSIZE 4 #define EXT_BGR_RED 2 #define EXT_BGR_GREEN 1 #define EXT_BGR_BLUE 0 #define EXT_BGR_PIXELSIZE 3 #define EXT_BGRX_RED 2 #define EXT_BGRX_GREEN 1 #define EXT_BGRX_BLUE 0 #define EXT_BGRX_PIXELSIZE 4 #define EXT_XBGR_RED 3 #define EXT_XBGR_GREEN 2 #define EXT_XBGR_BLUE 1 #define EXT_XBGR_PIXELSIZE 4 #define EXT_XRGB_RED 1 #define EXT_XRGB_GREEN 2 #define EXT_XRGB_BLUE 3 #define EXT_XRGB_PIXELSIZE 4 static const int rgb_red[JPEG_NUMCS] = { -1, -1, RGB_RED, -1, -1, -1, EXT_RGB_RED, EXT_RGBX_RED, EXT_BGR_RED, EXT_BGRX_RED, EXT_XBGR_RED, EXT_XRGB_RED, EXT_RGBX_RED, EXT_BGRX_RED, EXT_XBGR_RED, EXT_XRGB_RED }; static const int rgb_green[JPEG_NUMCS] = { -1, -1, RGB_GREEN, -1, -1, -1, EXT_RGB_GREEN, EXT_RGBX_GREEN, EXT_BGR_GREEN, EXT_BGRX_GREEN, EXT_XBGR_GREEN, EXT_XRGB_GREEN, EXT_RGBX_GREEN, EXT_BGRX_GREEN, EXT_XBGR_GREEN, EXT_XRGB_GREEN }; static const int rgb_blue[JPEG_NUMCS] = { -1, -1, RGB_BLUE, -1, -1, -1, EXT_RGB_BLUE, EXT_RGBX_BLUE, EXT_BGR_BLUE, EXT_BGRX_BLUE, EXT_XBGR_BLUE, EXT_XRGB_BLUE, EXT_RGBX_BLUE, EXT_BGRX_BLUE, EXT_XBGR_BLUE, EXT_XRGB_BLUE }; static const int rgb_pixelsize[JPEG_NUMCS] = { -1, -1, RGB_PIXELSIZE, -1, -1, -1, EXT_RGB_PIXELSIZE, EXT_RGBX_PIXELSIZE, EXT_BGR_PIXELSIZE, EXT_BGRX_PIXELSIZE, EXT_XBGR_PIXELSIZE, EXT_XRGB_PIXELSIZE, EXT_RGBX_PIXELSIZE, EXT_BGRX_PIXELSIZE, EXT_XBGR_PIXELSIZE, EXT_XRGB_PIXELSIZE }; /* Definitions for speed-related optimizations. */ /* On some machines (notably 68000 series) "int" is 32 bits, but multiplying * two 16-bit shorts is faster than multiplying two ints. Define MULTIPLIER * as short on such a machine. MULTIPLIER must be at least 16 bits wide. */ #ifndef MULTIPLIER #ifndef WITH_SIMD #define MULTIPLIER int /* type for fastest integer multiply */ #else #define MULTIPLIER short /* prefer 16-bit with SIMD for parellelism */ #endif #endif /* FAST_FLOAT should be either float or double, whichever is done faster * by your compiler. (Note that this type is only used in the floating point * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.) * Typically, float is faster in ANSI C compilers, while double is faster in * pre-ANSI compilers (because they insist on converting to double anyway). * The code below therefore chooses float if we have ANSI-style prototypes. */ #ifndef FAST_FLOAT #ifdef HAVE_PROTOTYPES #define FAST_FLOAT float #else #define FAST_FLOAT double #endif #endif #endif /* JPEG_INTERNAL_OPTIONS */ glmark2-2012.08/./src/libjpeg-turbo/jdhuff.c0000664000175000017500000006052212013417376017671 0ustar alfalf00000000000000/* * jdhuff.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2009-2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains Huffman entropy decoding routines. * * Much of the complexity here has to do with supporting input suspension. * If the data source module demands suspension, we want to be able to back * up to the start of the current MCU. To do this, we copy state variables * into local working storage, and update them back to the permanent * storage only upon successful completion of an MCU. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdhuff.h" /* Declarations shared with jdphuff.c */ #include "jpegcomp.h" /* * Expanded entropy decoder object for Huffman decoding. * * The savable_state subrecord contains fields that change within an MCU, * but must not be updated permanently until we complete the MCU. */ typedef struct { int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ } savable_state; /* This macro is to work around compilers with missing or broken * structure assignment. You'll need to fix this code if you have * such a compiler and you change MAX_COMPS_IN_SCAN. */ #ifndef NO_STRUCT_ASSIGN #define ASSIGN_STATE(dest,src) ((dest) = (src)) #else #if MAX_COMPS_IN_SCAN == 4 #define ASSIGN_STATE(dest,src) \ ((dest).last_dc_val[0] = (src).last_dc_val[0], \ (dest).last_dc_val[1] = (src).last_dc_val[1], \ (dest).last_dc_val[2] = (src).last_dc_val[2], \ (dest).last_dc_val[3] = (src).last_dc_val[3]) #endif #endif typedef struct { struct jpeg_entropy_decoder pub; /* public fields */ /* These fields are loaded into local variables at start of each MCU. * In case of suspension, we exit WITHOUT updating them. */ bitread_perm_state bitstate; /* Bit buffer at start of MCU */ savable_state saved; /* Other state at start of MCU */ /* These fields are NOT loaded into local working state. */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ /* Pointers to derived tables (these workspaces have image lifespan) */ d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; /* Precalculated info set up by start_pass for use in decode_mcu: */ /* Pointers to derived tables to be used for each block within an MCU */ d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; /* Whether we care about the DC and AC coefficient values for each block */ boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; } huff_entropy_decoder; typedef huff_entropy_decoder * huff_entropy_ptr; /* * Initialize for a Huffman-compressed scan. */ METHODDEF(void) start_pass_huff_decoder (j_decompress_ptr cinfo) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci, blkn, dctbl, actbl; jpeg_component_info * compptr; /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. * This ought to be an error condition, but we make it a warning because * there are some baseline files out there with all zeroes in these bytes. */ if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || cinfo->Ah != 0 || cinfo->Al != 0) WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; dctbl = compptr->dc_tbl_no; actbl = compptr->ac_tbl_no; /* Compute derived values for Huffman tables */ /* We may do this more than once for a table, but it's not expensive */ jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, & entropy->dc_derived_tbls[dctbl]); jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, & entropy->ac_derived_tbls[actbl]); /* Initialize DC predictions to 0 */ entropy->saved.last_dc_val[ci] = 0; } /* Precalculate decoding info for each block in an MCU of this scan */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; /* Precalculate which table to use for each block */ entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; /* Decide whether we really care about the coefficient values */ if (compptr->component_needed) { entropy->dc_needed[blkn] = TRUE; /* we don't need the ACs if producing a 1/8th-size image */ entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1); } else { entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; } } /* Initialize bitread state variables */ entropy->bitstate.bits_left = 0; entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ entropy->pub.insufficient_data = FALSE; /* Initialize restart counter */ entropy->restarts_to_go = cinfo->restart_interval; } /* * Compute the derived values for a Huffman table. * This routine also performs some validation checks on the table. * * Note this is also used by jdphuff.c. */ GLOBAL(void) jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl ** pdtbl) { JHUFF_TBL *htbl; d_derived_tbl *dtbl; int p, i, l, si, numsymbols; int lookbits, ctr; char huffsize[257]; unsigned int huffcode[257]; unsigned int code; /* Note that huffsize[] and huffcode[] are filled in code-length order, * paralleling the order of the symbols themselves in htbl->huffval[]. */ /* Find the input Huffman table */ if (tblno < 0 || tblno >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; if (htbl == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); /* Allocate a workspace if we haven't already done so. */ if (*pdtbl == NULL) *pdtbl = (d_derived_tbl *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(d_derived_tbl)); dtbl = *pdtbl; dtbl->pub = htbl; /* fill in back link */ /* Figure C.1: make table of Huffman code length for each symbol */ p = 0; for (l = 1; l <= 16; l++) { i = (int) htbl->bits[l]; if (i < 0 || p + i > 256) /* protect against table overrun */ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); while (i--) huffsize[p++] = (char) l; } huffsize[p] = 0; numsymbols = p; /* Figure C.2: generate the codes themselves */ /* We also validate that the counts represent a legal Huffman code tree. */ code = 0; si = huffsize[0]; p = 0; while (huffsize[p]) { while (((int) huffsize[p]) == si) { huffcode[p++] = code; code++; } /* code is now 1 more than the last code used for codelength si; but * it must still fit in si bits, since no code is allowed to be all ones. */ if (((INT32) code) >= (((INT32) 1) << si)) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); code <<= 1; si++; } /* Figure F.15: generate decoding tables for bit-sequential decoding */ p = 0; for (l = 1; l <= 16; l++) { if (htbl->bits[l]) { /* valoffset[l] = huffval[] index of 1st symbol of code length l, * minus the minimum code of length l */ dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; p += htbl->bits[l]; dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ } else { dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ } } dtbl->valoffset[17] = 0; dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ /* Compute lookahead tables to speed up decoding. * First we set all the table entries to 0, indicating "too long"; * then we iterate through the Huffman codes that are short enough and * fill in all the entries that correspond to bit sequences starting * with that code. */ for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; p = 0; for (l = 1; l <= HUFF_LOOKAHEAD; l++) { for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { /* l = current code's length, p = its index in huffcode[] & huffval[]. */ /* Generate left-justified code followed by all possible bit sequences */ lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; lookbits++; } } } /* Validate symbols as being reasonable. * For AC tables, we make no check, but accept all byte values 0..255. * For DC tables, we require the symbols to be in range 0..15. * (Tighter bounds could be applied depending on the data depth and mode, * but this is sufficient to ensure safe decoding.) */ if (isDC) { for (i = 0; i < numsymbols; i++) { int sym = htbl->huffval[i]; if (sym < 0 || sym > 15) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); } } } /* * Out-of-line code for bit fetching (shared with jdphuff.c). * See jdhuff.h for info about usage. * Note: current values of get_buffer and bits_left are passed as parameters, * but are returned in the corresponding fields of the state struct. * * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width * of get_buffer to be used. (On machines with wider words, an even larger * buffer could be used.) However, on some machines 32-bit shifts are * quite slow and take time proportional to the number of places shifted. * (This is true with most PC compilers, for instance.) In this case it may * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. */ #ifdef SLOW_SHIFT_32 #define MIN_GET_BITS 15 /* minimum allowable value */ #else #define MIN_GET_BITS (BIT_BUF_SIZE-7) #endif GLOBAL(boolean) jpeg_fill_bit_buffer (bitread_working_state * state, register bit_buf_type get_buffer, register int bits_left, int nbits) /* Load up the bit buffer to a depth of at least nbits */ { /* Copy heavily used state fields into locals (hopefully registers) */ register const JOCTET * next_input_byte = state->next_input_byte; register size_t bytes_in_buffer = state->bytes_in_buffer; j_decompress_ptr cinfo = state->cinfo; /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ /* (It is assumed that no request will be for more than that many bits.) */ /* We fail to do so only if we hit a marker or are forced to suspend. */ if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ while (bits_left < MIN_GET_BITS) { register int c; /* Attempt to read a byte */ if (bytes_in_buffer == 0) { if (! (*cinfo->src->fill_input_buffer) (cinfo)) return FALSE; next_input_byte = cinfo->src->next_input_byte; bytes_in_buffer = cinfo->src->bytes_in_buffer; } bytes_in_buffer--; c = GETJOCTET(*next_input_byte++); /* If it's 0xFF, check and discard stuffed zero byte */ if (c == 0xFF) { /* Loop here to discard any padding FF's on terminating marker, * so that we can save a valid unread_marker value. NOTE: we will * accept multiple FF's followed by a 0 as meaning a single FF data * byte. This data pattern is not valid according to the standard. */ do { if (bytes_in_buffer == 0) { if (! (*cinfo->src->fill_input_buffer) (cinfo)) return FALSE; next_input_byte = cinfo->src->next_input_byte; bytes_in_buffer = cinfo->src->bytes_in_buffer; } bytes_in_buffer--; c = GETJOCTET(*next_input_byte++); } while (c == 0xFF); if (c == 0) { /* Found FF/00, which represents an FF data byte */ c = 0xFF; } else { /* Oops, it's actually a marker indicating end of compressed data. * Save the marker code for later use. * Fine point: it might appear that we should save the marker into * bitread working state, not straight into permanent state. But * once we have hit a marker, we cannot need to suspend within the * current MCU, because we will read no more bytes from the data * source. So it is OK to update permanent state right away. */ cinfo->unread_marker = c; /* See if we need to insert some fake zero bits. */ goto no_more_bytes; } } /* OK, load c into get_buffer */ get_buffer = (get_buffer << 8) | c; bits_left += 8; } /* end while */ } else { no_more_bytes: /* We get here if we've read the marker that terminates the compressed * data segment. There should be enough bits in the buffer register * to satisfy the request; if so, no problem. */ if (nbits > bits_left) { /* Uh-oh. Report corrupted data to user and stuff zeroes into * the data stream, so that we can produce some kind of image. * We use a nonvolatile flag to ensure that only one warning message * appears per data segment. */ if (! cinfo->entropy->insufficient_data) { WARNMS(cinfo, JWRN_HIT_MARKER); cinfo->entropy->insufficient_data = TRUE; } /* Fill the buffer with zero bits */ get_buffer <<= MIN_GET_BITS - bits_left; bits_left = MIN_GET_BITS; } } /* Unload the local registers */ state->next_input_byte = next_input_byte; state->bytes_in_buffer = bytes_in_buffer; state->get_buffer = get_buffer; state->bits_left = bits_left; return TRUE; } /* Macro version of the above, which performs much better but does not handle markers. We have to hand off any blocks with markers to the slower routines. */ #define GET_BYTE \ { \ register int c0, c1; \ c0 = GETJOCTET(*buffer++); \ c1 = GETJOCTET(*buffer); \ /* Pre-execute most common case */ \ get_buffer = (get_buffer << 8) | c0; \ bits_left += 8; \ if (c0 == 0xFF) { \ /* Pre-execute case of FF/00, which represents an FF data byte */ \ buffer++; \ if (c1 != 0) { \ /* Oops, it's actually a marker indicating end of compressed data. */ \ cinfo->unread_marker = c1; \ /* Back out pre-execution and fill the buffer with zero bits */ \ buffer -= 2; \ get_buffer &= ~0xFF; \ } \ } \ } #if __WORDSIZE == 64 || defined(_WIN64) /* Pre-fetch 48 bytes, because the holding register is 64-bit */ #define FILL_BIT_BUFFER_FAST \ if (bits_left < 16) { \ GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \ } #else /* Pre-fetch 16 bytes, because the holding register is 32-bit */ #define FILL_BIT_BUFFER_FAST \ if (bits_left < 16) { \ GET_BYTE GET_BYTE \ } #endif /* * Out-of-line code for Huffman code decoding. * See jdhuff.h for info about usage. */ GLOBAL(int) jpeg_huff_decode (bitread_working_state * state, register bit_buf_type get_buffer, register int bits_left, d_derived_tbl * htbl, int min_bits) { register int l = min_bits; register INT32 code; /* HUFF_DECODE has determined that the code is at least min_bits */ /* bits long, so fetch that many bits in one swoop. */ CHECK_BIT_BUFFER(*state, l, return -1); code = GET_BITS(l); /* Collect the rest of the Huffman code one bit at a time. */ /* This is per Figure F.16 in the JPEG spec. */ while (code > htbl->maxcode[l]) { code <<= 1; CHECK_BIT_BUFFER(*state, 1, return -1); code |= GET_BITS(1); l++; } /* Unload the local registers */ state->get_buffer = get_buffer; state->bits_left = bits_left; /* With garbage input we may reach the sentinel value l = 17. */ if (l > 16) { WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); return 0; /* fake a zero as the safest result */ } return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; } /* * Figure F.12: extend sign bit. * On some machines, a shift and add will be faster than a table lookup. */ #define AVOID_TABLES #ifdef AVOID_TABLES #define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1))) #else #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) static const int extend_test[16] = /* entry n is 2**(n-1) */ { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; #endif /* AVOID_TABLES */ /* * Check for a restart marker & resynchronize decoder. * Returns FALSE if must suspend. */ LOCAL(boolean) process_restart (j_decompress_ptr cinfo) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci; /* Throw away any unused bits remaining in bit buffer; */ /* include any full bytes in next_marker's count of discarded bytes */ cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; entropy->bitstate.bits_left = 0; /* Advance past the RSTn marker */ if (! (*cinfo->marker->read_restart_marker) (cinfo)) return FALSE; /* Re-initialize DC predictions to 0 */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) entropy->saved.last_dc_val[ci] = 0; /* Reset restart counter */ entropy->restarts_to_go = cinfo->restart_interval; /* Reset out-of-data flag, unless read_restart_marker left us smack up * against a marker. In that case we will end up treating the next data * segment as empty, and we can avoid producing bogus output pixels by * leaving the flag set. */ if (cinfo->unread_marker == 0) entropy->pub.insufficient_data = FALSE; return TRUE; } LOCAL(boolean) decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; BITREAD_STATE_VARS; int blkn; savable_state state; /* Outer loop handles each block in the MCU */ /* Load up working state */ BITREAD_LOAD_STATE(cinfo,entropy->bitstate); ASSIGN_STATE(state, entropy->saved); for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { JBLOCKROW block = MCU_data[blkn]; d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; register int s, k, r; /* Decode a single block's worth of coefficients */ /* Section F.2.2.1: decode the DC coefficient difference */ HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); if (s) { CHECK_BIT_BUFFER(br_state, s, return FALSE); r = GET_BITS(s); s = HUFF_EXTEND(r, s); } if (entropy->dc_needed[blkn]) { /* Convert DC difference to actual value, update last_dc_val */ int ci = cinfo->MCU_membership[blkn]; s += state.last_dc_val[ci]; state.last_dc_val[ci] = s; /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ (*block)[0] = (JCOEF) s; } if (entropy->ac_needed[blkn]) { /* Section F.2.2.2: decode the AC coefficients */ /* Since zeroes are skipped, output area must be cleared beforehand */ for (k = 1; k < DCTSIZE2; k++) { HUFF_DECODE(s, br_state, actbl, return FALSE, label2); r = s >> 4; s &= 15; if (s) { k += r; CHECK_BIT_BUFFER(br_state, s, return FALSE); r = GET_BITS(s); s = HUFF_EXTEND(r, s); /* Output coefficient in natural (dezigzagged) order. * Note: the extra entries in jpeg_natural_order[] will save us * if k >= DCTSIZE2, which could happen if the data is corrupted. */ (*block)[jpeg_natural_order[k]] = (JCOEF) s; } else { if (r != 15) break; k += 15; } } } else { /* Section F.2.2.2: decode the AC coefficients */ /* In this path we just discard the values */ for (k = 1; k < DCTSIZE2; k++) { HUFF_DECODE(s, br_state, actbl, return FALSE, label3); r = s >> 4; s &= 15; if (s) { k += r; CHECK_BIT_BUFFER(br_state, s, return FALSE); DROP_BITS(s); } else { if (r != 15) break; k += 15; } } } } /* Completed MCU, so update state */ BITREAD_SAVE_STATE(cinfo,entropy->bitstate); ASSIGN_STATE(entropy->saved, state); return TRUE; } LOCAL(boolean) decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; BITREAD_STATE_VARS; JOCTET *buffer; int blkn; savable_state state; /* Outer loop handles each block in the MCU */ /* Load up working state */ BITREAD_LOAD_STATE(cinfo,entropy->bitstate); buffer = (JOCTET *) br_state.next_input_byte; ASSIGN_STATE(state, entropy->saved); for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { JBLOCKROW block = MCU_data[blkn]; d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; register int s, k, r, l; HUFF_DECODE_FAST(s, l, dctbl); if (s) { FILL_BIT_BUFFER_FAST r = GET_BITS(s); s = HUFF_EXTEND(r, s); } if (entropy->dc_needed[blkn]) { int ci = cinfo->MCU_membership[blkn]; s += state.last_dc_val[ci]; state.last_dc_val[ci] = s; (*block)[0] = (JCOEF) s; } if (entropy->ac_needed[blkn]) { for (k = 1; k < DCTSIZE2; k++) { HUFF_DECODE_FAST(s, l, actbl); r = s >> 4; s &= 15; if (s) { k += r; FILL_BIT_BUFFER_FAST r = GET_BITS(s); s = HUFF_EXTEND(r, s); (*block)[jpeg_natural_order[k]] = (JCOEF) s; } else { if (r != 15) break; k += 15; } } } else { for (k = 1; k < DCTSIZE2; k++) { HUFF_DECODE_FAST(s, l, actbl); r = s >> 4; s &= 15; if (s) { k += r; FILL_BIT_BUFFER_FAST DROP_BITS(s); } else { if (r != 15) break; k += 15; } } } } if (cinfo->unread_marker != 0) { cinfo->unread_marker = 0; return FALSE; } br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte); br_state.next_input_byte = buffer; BITREAD_SAVE_STATE(cinfo,entropy->bitstate); ASSIGN_STATE(entropy->saved, state); return TRUE; } /* * Decode and return one MCU's worth of Huffman-compressed coefficients. * The coefficients are reordered from zigzag order into natural array order, * but are not dequantized. * * The i'th block of the MCU is stored into the block pointed to by * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. * (Wholesale zeroing is usually a little faster than retail...) * * Returns FALSE if data source requested suspension. In that case no * changes have been made to permanent state. (Exception: some output * coefficients may already have been assigned. This is harmless for * this module, since we'll just re-assign them on the next call.) */ #define BUFSIZE (DCTSIZE2 * 2) METHODDEF(boolean) decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int usefast = 1; /* Process restart marker if needed; may have to suspend */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) if (! process_restart(cinfo)) return FALSE; usefast = 0; } if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU || cinfo->unread_marker != 0) usefast = 0; /* If we've run out of data, just leave the MCU set to zeroes. * This way, we return uniform gray for the remainder of the segment. */ if (! entropy->pub.insufficient_data) { if (usefast) { if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow; } else { use_slow: if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE; } } /* Account for restart interval (no-op if not using restarts) */ entropy->restarts_to_go--; return TRUE; } /* * Module initialization routine for Huffman entropy decoding. */ GLOBAL(void) jinit_huff_decoder (j_decompress_ptr cinfo) { huff_entropy_ptr entropy; int i; entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_decoder)); cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; entropy->pub.start_pass = start_pass_huff_decoder; entropy->pub.decode_mcu = decode_mcu; /* Mark tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; } } glmark2-2012.08/./src/libjpeg-turbo/README-turbo.txt0000775000175000017500000004104712013417376021112 0ustar alfalf00000000000000******************************************************************************* ** Background ******************************************************************************* libjpeg-turbo is a derivative of libjpeg that uses SIMD instructions (MMX, SSE2, NEON) to accelerate baseline JPEG compression and decompression on x86, x86-64, and ARM systems. On such systems, libjpeg-turbo is generally 2-4x as fast as the unmodified version of libjpeg, all else being equal. libjpeg-turbo was originally based on libjpeg/SIMD by Miyasaka Masaru, but the TigerVNC and VirtualGL projects made numerous enhancements to the codec in 2009, including improved support for Mac OS X, 64-bit support, support for 32-bit and big-endian pixel formats (RGBX, XBGR, etc.), accelerated Huffman encoding/decoding, and various bug fixes. The goal was to produce a fully open-source codec that could replace the partially closed-source TurboJPEG/IPP codec used by VirtualGL and TurboVNC. libjpeg-turbo generally achieves 80-120% of the performance of TurboJPEG/IPP. It is faster in some areas but slower in others. In early 2010, libjpeg-turbo spun off into its own independent project, with the goal of making high-speed JPEG compression/decompression technology available to a broader range of users and developers. ******************************************************************************* ** License ******************************************************************************* Most of libjpeg-turbo inherits the non-restrictive, BSD-style license used by libjpeg (see README.) The TurboJPEG/OSS wrapper (both C and Java versions) and associated test programs bear a similar license, which is reproduced below: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the libjpeg-turbo Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* ** Using libjpeg-turbo ******************************************************************************* libjpeg-turbo includes two APIs that can be used to compress and decompress JPEG images: TurboJPEG API: This API provides an easy-to-use interface for compressing and decompressing JPEG images in memory. It also provides some functionality that would not be straightforward to achieve using the underlying libjpeg API, such as generating planar YUV images and performing multiple simultaneous lossless transforms on an image. The Java interface for libjpeg-turbo is written on top of the TurboJPEG API. libjpeg API: This is the de facto industry-standard API for compressing and decompressing JPEG images. It is more difficult to use than the TurboJPEG API but also more powerful. libjpeg-turbo is both API/ABI-compatible and mathematically compatible with libjpeg v6b. It can also optionally be configured to be API/ABI-compatible with libjpeg v7 and v8 (see below.) ============================= Replacing libjpeg at Run Time ============================= If a Unix application is dynamically linked with libjpeg, then you can replace libjpeg with libjpeg-turbo at run time by manipulating LD_LIBRARY_PATH. For instance: [Using libjpeg] > time cjpeg vgl_5674_0098.jpg real 0m0.392s user 0m0.074s sys 0m0.020s [Using libjpeg-turbo] > export LD_LIBRARY_PATH=/opt/libjpeg-turbo/{lib}:$LD_LIBRARY_PATH > time cjpeg vgl_5674_0098.jpg real 0m0.109s user 0m0.029s sys 0m0.010s NOTE: {lib} can be lib, lib32, lib64, or lib/64, depending on the O/S and architecture. System administrators can also replace the libjpeg sym links in /usr/{lib} with links to the libjpeg-turbo dynamic library located in /opt/libjpeg-turbo/{lib}. This will effectively accelerate every application that uses the libjpeg dynamic library on the system. The libjpeg-turbo SDK for Visual C++ installs the libjpeg-turbo DLL (jpeg62.dll, jpeg7.dll, or jpeg8.dll, depending on whether it was built with libjpeg v6b, v7, or v8 emulation) into c:\libjpeg-turbo[64]\bin, and the PATH environment variable can be modified such that this directory is searched before any others that might contain a libjpeg DLL. However, if a libjpeg DLL exists in an application's install directory, then Windows will load this DLL first whenever the application is launched. Thus, if an application ships with jpeg62.dll, jpeg7.dll, or jpeg8.dll, then back up the application's version of this DLL and copy c:\libjpeg-turbo[64]\bin\jpeg*.dll into the application's install directory to accelerate it. The version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for Visual C++ requires the Visual C++ 2008 C run-time DLL (msvcr90.dll). msvcr90.dll ships with more recent versions of Windows, but users of older Windows releases can obtain it from the Visual C++ 2008 Redistributable Package, which is available as a free download from Microsoft's web site. NOTE: Features of libjpeg that require passing a C run-time structure, such as a file handle, from an application to libjpeg will probably not work with the version of the libjpeg-turbo DLL distributed in the libjpeg-turbo SDK for Visual C++, unless the application is also built to use the Visual C++ 2008 C run-time DLL. In particular, this affects jpeg_stdio_dest() and jpeg_stdio_src(). Mac applications typically embed their own copies of the libjpeg dylib inside the (hidden) application bundle, so it is not possible to globally replace libjpeg on OS X systems. If an application uses a shared library version of libjpeg, then it may be possible to replace the application's version of it. This would generally involve copying libjpeg.*.dylib from libjpeg-turbo into the appropriate place in the application bundle and using install_name_tool to repoint the dylib to the new directory. This requires an advanced knowledge of OS X and would not survive an upgrade or a re-install of the application. Thus, it is not recommended for most users. ======================= Replacing TurboJPEG/IPP ======================= libjpeg-turbo is a drop-in replacement for the TurboJPEG/IPP SDK used by VirtualGL 2.1.x and TurboVNC 0.6 (and prior.) libjpeg-turbo contains a wrapper library (TurboJPEG/OSS) that emulates the TurboJPEG API using libjpeg-turbo instead of the closed-source Intel Performance Primitives. You can replace the TurboJPEG/IPP package on Linux systems with the libjpeg-turbo package in order to make existing releases of VirtualGL 2.1.x and TurboVNC 0.x use the new codec at run time. Note that the 64-bit libjpeg-turbo packages contain only 64-bit binaries, whereas the TurboJPEG/IPP 64-bit packages contained both 64-bit and 32-bit binaries. Thus, to replace a TurboJPEG/IPP 64-bit package, install both the 64-bit and 32-bit versions of libjpeg-turbo. You can also build the VirtualGL 2.1.x and TurboVNC 0.6 source code with the libjpeg-turbo SDK instead of TurboJPEG/IPP. It should work identically. libjpeg-turbo also includes static library versions of TurboJPEG/OSS, which are used to build VirtualGL 2.2 and TurboVNC 1.0 and later. ======================================== Using libjpeg-turbo in Your Own Programs ======================================== For the most part, libjpeg-turbo should work identically to libjpeg, so in most cases, an application can be built against libjpeg and then run against libjpeg-turbo. On Unix systems (including Cygwin), you can build against libjpeg-turbo instead of libjpeg by setting CPATH=/opt/libjpeg-turbo/include and LIBRARY_PATH=/opt/libjpeg-turbo/{lib} ({lib} = lib32 or lib64, depending on whether you are building a 32-bit or a 64-bit application.) If using MinGW, then set CPATH=/c/libjpeg-turbo-gcc[64]/include and LIBRARY_PATH=/c/libjpeg-turbo-gcc[64]/lib Building against libjpeg-turbo is useful, for instance, if you want to build an application that leverages the libjpeg-turbo colorspace extensions (see below.) On Linux and Solaris systems, you would still need to manipulate LD_LIBRARY_PATH or create appropriate sym links to use libjpeg-turbo at run time. On such systems, you can pass -R /opt/libjpeg-turbo/{lib} to the linker to force the use of libjpeg-turbo at run time rather than libjpeg (also useful if you want to leverage the colorspace extensions), or you can link against the libjpeg-turbo static library. To force a Linux, Solaris, or MinGW application to link against the static version of libjpeg-turbo, you can use the following linker options: -Wl,-Bstatic -ljpeg -Wl,-Bdynamic On OS X, simply add /opt/libjpeg-turbo/lib/libjpeg.a to the linker command line (this also works on Linux and Solaris.) To build Visual C++ applications using libjpeg-turbo, add c:\libjpeg-turbo[64]\include to the system or user INCLUDE environment variable and c:\libjpeg-turbo[64]\lib to the system or user LIB environment variable, and then link against either jpeg.lib (to use the DLL version of libjpeg-turbo) or jpeg-static.lib (to use the static version of libjpeg-turbo.) ===================== Colorspace Extensions ===================== libjpeg-turbo includes extensions that allow JPEG images to be compressed directly from (and decompressed directly to) buffers that use BGR, BGRX, RGBX, XBGR, and XRGB pixel ordering. This is implemented with ten new colorspace constants: JCS_EXT_RGB /* red/green/blue */ JCS_EXT_RGBX /* red/green/blue/x */ JCS_EXT_BGR /* blue/green/red */ JCS_EXT_BGRX /* blue/green/red/x */ JCS_EXT_XBGR /* x/blue/green/red */ JCS_EXT_XRGB /* x/red/green/blue */ JCS_EXT_RGBA /* red/green/blue/alpha */ JCS_EXT_BGRA /* blue/green/red/alpha */ JCS_EXT_ABGR /* alpha/blue/green/red */ JCS_EXT_ARGB /* alpha/red/green/blue */ Setting cinfo.in_color_space (compression) or cinfo.out_color_space (decompression) to one of these values will cause libjpeg-turbo to read the red, green, and blue values from (or write them to) the appropriate position in the pixel when compressing from/decompressing to an RGB buffer. Your application can check for the existence of these extensions at compile time with: #ifdef JCS_EXTENSIONS At run time, attempting to use these extensions with a version of libjpeg that doesn't support them will result in a "Bogus input colorspace" error. When using the RGBX, BGRX, XBGR, and XRGB colorspaces during decompression, the X byte is undefined, and in order to ensure the best performance, libjpeg-turbo can set that byte to whatever value it wishes. If an application expects the X byte to be used as an alpha channel, then it should specify JCS_EXT_RGBA, JCS_EXT_BGRA, JCS_EXT_ABGR, or JCS_EXT_ARGB. When these colorspace constants are used, the X byte is guaranteed to be 0xFF, which is interpreted as opaque. Your application can check for the existence of the alpha channel colorspace extensions at compile time with: #ifdef JCS_ALPHA_EXTENSIONS jcstest.c, located in the libjpeg-turbo source tree, demonstrates how to check for the existence of the colorspace extensions at compile time and run time. ================================= libjpeg v7 and v8 API/ABI support ================================= With libjpeg v7 and v8, new features were added that necessitated extending the compression and decompression structures. Unfortunately, due to the exposed nature of those structures, extending them also necessitated breaking backward ABI compatibility with previous libjpeg releases. Thus, programs that are built to use libjpeg v7 or v8 did not work with libjpeg-turbo, since it is based on the libjpeg v6b code base. Although libjpeg v7 and v8 are still not as widely used as v6b, enough programs (including a few Linux distros) have made the switch that it was desirable to provide support for the libjpeg v7/v8 API/ABI in libjpeg-turbo. Although libjpeg-turbo can now be configured as a drop-in replacement for libjpeg v7 or v8, it should be noted that not all of the features in libjpeg v7 and v8 are supported (see below.) By passing an argument of --with-jpeg7 or --with-jpeg8 to configure, or an argument of -DWITH_JPEG7=1 or -DWITH_JPEG8=1 to cmake, you can build a version of libjpeg-turbo that emulates the libjpeg v7 or v8 API/ABI, so that programs that are built against libjpeg v7 or v8 can be run with libjpeg-turbo. The following section describes which libjpeg v7+ features are supported and which aren't. libjpeg v7 and v8 Features: --------------------------- Fully supported: -- cjpeg: Separate quality settings for luminance and chrominance Note that the libpjeg v7+ API was extended to accommodate this feature only for convenience purposes. It has always been possible to implement this feature with libjpeg v6b (see rdswitch.c for an example.) -- cjpeg: 32-bit BMP support -- jpegtran: lossless cropping -- jpegtran: -perfect option -- rdjpgcom: -raw option -- rdjpgcom: locale awareness Fully supported when using libjpeg v7/v8 emulation: -- libjpeg: In-memory source and destination managers Not supported: -- libjpeg: DCT scaling in compressor cinfo.scale_num and cinfo.scale_denom are silently ignored. There is no technical reason why DCT scaling cannot be supported, but without the SmartScale extension (see below), it would only be able to down-scale using ratios of 1/2, 8/15, 4/7, 8/13, 2/3, 8/11, 4/5, and 8/9, which is of limited usefulness. -- libjpeg: SmartScale cinfo.block_size is silently ignored. SmartScale is an extension to the JPEG format that allows for DCT block sizes other than 8x8. It would be difficult to support this feature while retaining backward compatibility with libjpeg v6b. -- libjpeg: IDCT scaling extensions in decompressor libjpeg-turbo still supports IDCT scaling with scaling factors of 1/2, 1/4, and 1/8 (same as libjpeg v6b.) -- libjpeg: Fancy downsampling in compressor cinfo.do_fancy_downsampling is silently ignored. This requires the DCT scaling feature, which is not supported. -- jpegtran: Scaling This requires both the DCT scaling and SmartScale features, which are not supported. -- Lossless RGB JPEG files This requires the SmartScale feature, which is not supported. ******************************************************************************* ** Performance pitfalls ******************************************************************************* =============== Restart Markers =============== The optimized Huffman decoder in libjpeg-turbo does not handle restart markers in a way that makes the rest of the libjpeg infrastructure happy, so it is necessary to use the slow Huffman decoder when decompressing a JPEG image that has restart markers. This can cause the decompression performance to drop by as much as 20%, but the performance will still be much greater than that of libjpeg. Many consumer packages, such as PhotoShop, use restart markers when generating JPEG images, so images generated by those programs will experience this issue. =============================================== Fast Integer Forward DCT at High Quality Levels =============================================== The algorithm used by the SIMD-accelerated quantization function cannot produce correct results whenever the fast integer forward DCT is used along with a JPEG quality of 98-100. Thus, libjpeg-turbo must use the non-SIMD quantization function in those cases. This causes performance to drop by as much as 40%. It is therefore strongly advised that you use the slow integer forward DCT whenever encoding images with a JPEG quality of 98 or higher. glmark2-2012.08/./src/libjpeg-turbo/jfdctflt.c0000664000175000017500000001255612013417376020227 0ustar alfalf00000000000000/* * jfdctflt.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a floating-point implementation of the * forward DCT (Discrete Cosine Transform). * * This implementation should be more accurate than either of the integer * DCT implementations. However, it may not give the same results on all * machines because of differences in roundoff behavior. Speed will depend * on the hardware's floating point capacity. * * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT * on each column. Direct algorithms are also available, but they are * much more complex and seem not to be any faster when reduced to code. * * This implementation is based on Arai, Agui, and Nakajima's algorithm for * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in * Japanese, but the algorithm is described in the Pennebaker & Mitchell * JPEG textbook (see REFERENCES section in file README). The following code * is based directly on figure 4-8 in P&M. * While an 8-point DCT cannot be done in less than 11 multiplies, it is * possible to arrange the computation so that many of the multiplies are * simple scalings of the final outputs. These multiplies can then be * folded into the multiplications or divisions by the JPEG quantization * table entries. The AA&N method leaves only 5 multiplies and 29 adds * to be done in the DCT itself. * The primary disadvantage of this method is that with a fixed-point * implementation, accuracy is lost due to imprecise representation of the * scaled quantization values. However, that problem does not arise if * we use floating point arithmetic. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_FLOAT_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* * Perform the forward DCT on one block of samples. */ GLOBAL(void) jpeg_fdct_float (FAST_FLOAT * data) { FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; FAST_FLOAT tmp10, tmp11, tmp12, tmp13; FAST_FLOAT z1, z2, z3, z4, z5, z11, z13; FAST_FLOAT *dataptr; int ctr; /* Pass 1: process rows. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[0] + dataptr[7]; tmp7 = dataptr[0] - dataptr[7]; tmp1 = dataptr[1] + dataptr[6]; tmp6 = dataptr[1] - dataptr[6]; tmp2 = dataptr[2] + dataptr[5]; tmp5 = dataptr[2] - dataptr[5]; tmp3 = dataptr[3] + dataptr[4]; tmp4 = dataptr[3] - dataptr[4]; /* Even part */ tmp10 = tmp0 + tmp3; /* phase 2 */ tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[0] = tmp10 + tmp11; /* phase 3 */ dataptr[4] = tmp10 - tmp11; z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ dataptr[2] = tmp13 + z1; /* phase 5 */ dataptr[6] = tmp13 - z1; /* Odd part */ tmp10 = tmp4 + tmp5; /* phase 2 */ tmp11 = tmp5 + tmp6; tmp12 = tmp6 + tmp7; /* The rotator is modified from fig 4-8 to avoid extra negations. */ z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ z11 = tmp7 + z3; /* phase 5 */ z13 = tmp7 - z3; dataptr[5] = z13 + z2; /* phase 6 */ dataptr[3] = z13 - z2; dataptr[1] = z11 + z4; dataptr[7] = z11 - z4; dataptr += DCTSIZE; /* advance pointer to next row */ } /* Pass 2: process columns. */ dataptr = data; for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; /* Even part */ tmp10 = tmp0 + tmp3; /* phase 2 */ tmp13 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp12 = tmp1 - tmp2; dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ dataptr[DCTSIZE*4] = tmp10 - tmp11; z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ dataptr[DCTSIZE*6] = tmp13 - z1; /* Odd part */ tmp10 = tmp4 + tmp5; /* phase 2 */ tmp11 = tmp5 + tmp6; tmp12 = tmp6 + tmp7; /* The rotator is modified from fig 4-8 to avoid extra negations. */ z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ z11 = tmp7 + z3; /* phase 5 */ z13 = tmp7 - z3; dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ dataptr[DCTSIZE*3] = z13 - z2; dataptr[DCTSIZE*1] = z11 + z4; dataptr[DCTSIZE*7] = z11 - z4; dataptr++; /* advance pointer to next column */ } } #endif /* DCT_FLOAT_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jmemnobs.c0000664000175000017500000000533412013417376020235 0ustar alfalf00000000000000/* * jmemnobs.c * * Copyright (C) 1992-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file provides a really simple implementation of the system- * dependent portion of the JPEG memory manager. This implementation * assumes that no backing-store files are needed: all required space * can be obtained from malloc(). * This is very portable in the sense that it'll compile on almost anything, * but you'd better have lots of main memory (or virtual memory) if you want * to process big images. * Note that the max_memory_to_use option is ignored by this implementation. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jmemsys.h" /* import the system-dependent declarations */ #ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ extern void * malloc JPP((size_t size)); extern void free JPP((void *ptr)); #endif /* * Memory allocation and freeing are controlled by the regular library * routines malloc() and free(). */ GLOBAL(void *) jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) { return (void *) malloc(sizeofobject); } GLOBAL(void) jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) { free(object); } /* * "Large" objects are treated the same as "small" ones. * NB: although we include FAR keywords in the routine declarations, * this file won't actually work in 80x86 small/medium model; at least, * you probably won't be able to process useful-size images in only 64KB. */ GLOBAL(void FAR *) jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) { return (void FAR *) malloc(sizeofobject); } GLOBAL(void) jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) { free(object); } /* * This routine computes the total memory space available for allocation. * Here we always say, "we got all you want bud!" */ GLOBAL(size_t) jpeg_mem_available (j_common_ptr cinfo, size_t min_bytes_needed, size_t max_bytes_needed, size_t already_allocated) { return max_bytes_needed; } /* * Backing store (temporary file) management. * Since jpeg_mem_available always promised the moon, * this should never be called and we can just error out. */ GLOBAL(void) jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, long total_bytes_needed) { ERREXIT(cinfo, JERR_NO_BACKING_STORE); } /* * These routines take care of any system-dependent initialization and * cleanup required. Here, there isn't any. */ GLOBAL(long) jpeg_mem_init (j_common_ptr cinfo) { return 0; /* just set max_memory_to_use to 0 */ } GLOBAL(void) jpeg_mem_term (j_common_ptr cinfo) { /* no work */ } glmark2-2012.08/./src/libjpeg-turbo/jcmainct.c0000664000175000017500000002221412013417376020207 0ustar alfalf00000000000000/* * jcmainct.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the main buffer controller for compression. * The main buffer lies between the pre-processor and the JPEG * compressor proper; it holds downsampled data in the JPEG colorspace. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Note: currently, there is no operating mode in which a full-image buffer * is needed at this step. If there were, that mode could not be used with * "raw data" input, since this module is bypassed in that case. However, * we've left the code here for possible use in special applications. */ #undef FULL_MAIN_BUFFER_SUPPORTED /* Private buffer controller object */ typedef struct { struct jpeg_c_main_controller pub; /* public fields */ JDIMENSION cur_iMCU_row; /* number of current iMCU row */ JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */ boolean suspended; /* remember if we suspended output */ J_BUF_MODE pass_mode; /* current operating mode */ /* If using just a strip buffer, this points to the entire set of buffers * (we allocate one for each component). In the full-image case, this * points to the currently accessible strips of the virtual arrays. */ JSAMPARRAY buffer[MAX_COMPONENTS]; #ifdef FULL_MAIN_BUFFER_SUPPORTED /* If using full-image storage, this array holds pointers to virtual-array * control blocks for each component. Unused if not full-image storage. */ jvirt_sarray_ptr whole_image[MAX_COMPONENTS]; #endif } my_main_controller; typedef my_main_controller * my_main_ptr; /* Forward declarations */ METHODDEF(void) process_data_simple_main JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)); #ifdef FULL_MAIN_BUFFER_SUPPORTED METHODDEF(void) process_data_buffer_main JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)); #endif /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode) { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; /* Do nothing in raw-data mode. */ if (cinfo->raw_data_in) return; main_ptr->cur_iMCU_row = 0; /* initialize counters */ main_ptr->rowgroup_ctr = 0; main_ptr->suspended = FALSE; main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */ switch (pass_mode) { case JBUF_PASS_THRU: #ifdef FULL_MAIN_BUFFER_SUPPORTED if (main_ptr->whole_image[0] != NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); #endif main_ptr->pub.process_data = process_data_simple_main; break; #ifdef FULL_MAIN_BUFFER_SUPPORTED case JBUF_SAVE_SOURCE: case JBUF_CRANK_DEST: case JBUF_SAVE_AND_PASS: if (main_ptr->whole_image[0] == NULL) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); main_ptr->pub.process_data = process_data_buffer_main; break; #endif default: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); break; } } /* * Process some data. * This routine handles the simple pass-through mode, * where we have only a strip buffer. */ METHODDEF(void) process_data_simple_main (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail) { my_main_ptr main_ptr = (my_main_ptr) cinfo->main; while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) { /* Read input data if we haven't filled the main buffer yet */ if (main_ptr->rowgroup_ctr < DCTSIZE) (*cinfo->prep->pre_process_data) (cinfo, input_buf, in_row_ctr, in_rows_avail, main_ptr->buffer, &main_ptr->rowgroup_ctr, (JDIMENSION) DCTSIZE); /* If we don't have a full iMCU row buffered, return to application for * more data. Note that preprocessor will always pad to fill the iMCU row * at the bottom of the image. */ if (main_ptr->rowgroup_ctr != DCTSIZE) return; /* Send the completed row to the compressor */ if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) { /* If compressor did not consume the whole row, then we must need to * suspend processing and return to the application. In this situation * we pretend we didn't yet consume the last input row; otherwise, if * it happened to be the last row of the image, the application would * think we were done. */ if (! main_ptr->suspended) { (*in_row_ctr)--; main_ptr->suspended = TRUE; } return; } /* We did finish the row. Undo our little suspension hack if a previous * call suspended; then mark the main buffer empty. */ if (main_ptr->suspended) { (*in_row_ctr)++; main_ptr->suspended = FALSE; } main_ptr->rowgroup_ctr = 0; main_ptr->cur_iMCU_row++; } } #ifdef FULL_MAIN_BUFFER_SUPPORTED /* * Process some data. * This routine handles all of the modes that use a full-size buffer. */ METHODDEF(void) process_data_buffer_main (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail) { my_main_ptr main = (my_main_ptr) cinfo->main; int ci; jpeg_component_info *compptr; boolean writing = (main_ptr->pass_mode != JBUF_CRANK_DEST); while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) { /* Realign the virtual buffers if at the start of an iMCU row. */ if (main_ptr->rowgroup_ctr == 0) { for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { main_ptr->buffer[ci] = (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, main_ptr->whole_image[ci], main_ptr->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE), (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing); } /* In a read pass, pretend we just read some source data. */ if (! writing) { *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE; main_ptr->rowgroup_ctr = DCTSIZE; } } /* If a write pass, read input data until the current iMCU row is full. */ /* Note: preprocessor will pad if necessary to fill the last iMCU row. */ if (writing) { (*cinfo->prep->pre_process_data) (cinfo, input_buf, in_row_ctr, in_rows_avail, main_ptr->buffer, &main_ptr->rowgroup_ctr, (JDIMENSION) DCTSIZE); /* Return to application if we need more data to fill the iMCU row. */ if (main_ptr->rowgroup_ctr < DCTSIZE) return; } /* Emit data, unless this is a sink-only pass. */ if (main_ptr->pass_mode != JBUF_SAVE_SOURCE) { if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) { /* If compressor did not consume the whole row, then we must need to * suspend processing and return to the application. In this situation * we pretend we didn't yet consume the last input row; otherwise, if * it happened to be the last row of the image, the application would * think we were done. */ if (! main_ptr->suspended) { (*in_row_ctr)--; main_ptr->suspended = TRUE; } return; } /* We did finish the row. Undo our little suspension hack if a previous * call suspended; then mark the main buffer empty. */ if (main_ptr->suspended) { (*in_row_ctr)++; main_ptr->suspended = FALSE; } } /* If get here, we are done with this iMCU row. Mark buffer empty. */ main_ptr->rowgroup_ctr = 0; main_ptr->cur_iMCU_row++; } } #endif /* FULL_MAIN_BUFFER_SUPPORTED */ /* * Initialize main buffer controller. */ GLOBAL(void) jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer) { my_main_ptr main_ptr; int ci; jpeg_component_info *compptr; main_ptr = (my_main_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_main_controller)); cinfo->main = (struct jpeg_c_main_controller *) main_ptr; main_ptr->pub.start_pass = start_pass_main; /* We don't need to create a buffer in raw-data mode. */ if (cinfo->raw_data_in) return; /* Create the buffer. It holds downsampled data, so each component * may be of a different size. */ if (need_full_buffer) { #ifdef FULL_MAIN_BUFFER_SUPPORTED /* Allocate a full-image virtual array for each component */ /* Note we pad the bottom to a multiple of the iMCU height */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { main_ptr->whole_image[ci] = (*cinfo->mem->request_virt_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, compptr->width_in_blocks * DCTSIZE, (JDIMENSION) jround_up((long) compptr->height_in_blocks, (long) compptr->v_samp_factor) * DCTSIZE, (JDIMENSION) (compptr->v_samp_factor * DCTSIZE)); } #else ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); #endif } else { #ifdef FULL_MAIN_BUFFER_SUPPORTED main_ptr->whole_image[0] = NULL; /* flag for no virtual arrays */ #endif /* Allocate a strip buffer for each component */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, compptr->width_in_blocks * DCTSIZE, (JDIMENSION) (compptr->v_samp_factor * DCTSIZE)); } } } glmark2-2012.08/./src/libjpeg-turbo/jdapistd.c0000664000175000017500000002230312013417376020220 0ustar alfalf00000000000000/* * jdapistd.c * * Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains application interface code for the decompression half * of the JPEG library. These are the "standard" API routines that are * used in the normal full-decompression case. They are not used by a * transcoding-only application. Note that if an application links in * jpeg_start_decompress, it will end up linking in the entire decompressor. * We thus must separate this file from jdapimin.c to avoid linking the * whole decompression library into a transcoder. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* Forward declarations */ LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo)); /* * Decompression initialization. * jpeg_read_header must be completed before calling this. * * If a multipass operating mode was selected, this will do all but the * last pass, and thus may take a great deal of time. * * Returns FALSE if suspended. The return value need be inspected only if * a suspending data source is used. */ GLOBAL(boolean) jpeg_start_decompress (j_decompress_ptr cinfo) { if (cinfo->global_state == DSTATE_READY) { /* First call: initialize master control, select active modules */ jinit_master_decompress(cinfo); if (cinfo->buffered_image) { /* No more work here; expecting jpeg_start_output next */ cinfo->global_state = DSTATE_BUFIMAGE; return TRUE; } cinfo->global_state = DSTATE_PRELOAD; } if (cinfo->global_state == DSTATE_PRELOAD) { /* If file has multiple scans, absorb them all into the coef buffer */ if (cinfo->inputctl->has_multiple_scans) { #ifdef D_MULTISCAN_FILES_SUPPORTED for (;;) { int retcode; /* Call progress monitor hook if present */ if (cinfo->progress != NULL) (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); /* Absorb some more input */ retcode = (*cinfo->inputctl->consume_input) (cinfo); if (retcode == JPEG_SUSPENDED) return FALSE; if (retcode == JPEG_REACHED_EOI) break; /* Advance progress counter if appropriate */ if (cinfo->progress != NULL && (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { /* jdmaster underestimated number of scans; ratchet up one scan */ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; } } } #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* D_MULTISCAN_FILES_SUPPORTED */ } cinfo->output_scan_number = cinfo->input_scan_number; } else if (cinfo->global_state != DSTATE_PRESCAN) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Perform any dummy output passes, and set up for the final pass */ return output_pass_setup(cinfo); } /* * Set up for an output pass, and perform any dummy pass(es) needed. * Common subroutine for jpeg_start_decompress and jpeg_start_output. * Entry: global_state = DSTATE_PRESCAN only if previously suspended. * Exit: If done, returns TRUE and sets global_state for proper output mode. * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN. */ LOCAL(boolean) output_pass_setup (j_decompress_ptr cinfo) { if (cinfo->global_state != DSTATE_PRESCAN) { /* First call: do pass setup */ (*cinfo->master->prepare_for_output_pass) (cinfo); cinfo->output_scanline = 0; cinfo->global_state = DSTATE_PRESCAN; } /* Loop over any required dummy passes */ while (cinfo->master->is_dummy_pass) { #ifdef QUANT_2PASS_SUPPORTED /* Crank through the dummy pass */ while (cinfo->output_scanline < cinfo->output_height) { JDIMENSION last_scanline; /* Call progress monitor hook if present */ if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) cinfo->output_scanline; cinfo->progress->pass_limit = (long) cinfo->output_height; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* Process some data */ last_scanline = cinfo->output_scanline; (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, &cinfo->output_scanline, (JDIMENSION) 0); if (cinfo->output_scanline == last_scanline) return FALSE; /* No progress made, must suspend */ } /* Finish up dummy pass, and set up for another one */ (*cinfo->master->finish_output_pass) (cinfo); (*cinfo->master->prepare_for_output_pass) (cinfo); cinfo->output_scanline = 0; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif /* QUANT_2PASS_SUPPORTED */ } /* Ready for application to drive output pass through * jpeg_read_scanlines or jpeg_read_raw_data. */ cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING; return TRUE; } /* * Read some scanlines of data from the JPEG decompressor. * * The return value will be the number of lines actually read. * This may be less than the number requested in several cases, * including bottom of image, data source suspension, and operating * modes that emit multiple scanlines at a time. * * Note: we warn about excess calls to jpeg_read_scanlines() since * this likely signals an application programmer error. However, * an oversize buffer (max_lines > scanlines remaining) is not an error. */ GLOBAL(JDIMENSION) jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines) { JDIMENSION row_ctr; if (cinfo->global_state != DSTATE_SCANNING) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (cinfo->output_scanline >= cinfo->output_height) { WARNMS(cinfo, JWRN_TOO_MUCH_DATA); return 0; } /* Call progress monitor hook if present */ if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) cinfo->output_scanline; cinfo->progress->pass_limit = (long) cinfo->output_height; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* Process some data */ row_ctr = 0; (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); cinfo->output_scanline += row_ctr; return row_ctr; } /* * Alternate entry point to read raw data. * Processes exactly one iMCU row per call, unless suspended. */ GLOBAL(JDIMENSION) jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, JDIMENSION max_lines) { JDIMENSION lines_per_iMCU_row; if (cinfo->global_state != DSTATE_RAW_OK) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); if (cinfo->output_scanline >= cinfo->output_height) { WARNMS(cinfo, JWRN_TOO_MUCH_DATA); return 0; } /* Call progress monitor hook if present */ if (cinfo->progress != NULL) { cinfo->progress->pass_counter = (long) cinfo->output_scanline; cinfo->progress->pass_limit = (long) cinfo->output_height; (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); } /* Verify that at least one iMCU row can be returned. */ lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size; if (max_lines < lines_per_iMCU_row) ERREXIT(cinfo, JERR_BUFFER_SIZE); /* Decompress directly into user's buffer. */ if (! (*cinfo->coef->decompress_data) (cinfo, data)) return 0; /* suspension forced, can do nothing more */ /* OK, we processed one iMCU row. */ cinfo->output_scanline += lines_per_iMCU_row; return lines_per_iMCU_row; } /* Additional entry points for buffered-image mode. */ #ifdef D_MULTISCAN_FILES_SUPPORTED /* * Initialize for an output pass in buffered-image mode. */ GLOBAL(boolean) jpeg_start_output (j_decompress_ptr cinfo, int scan_number) { if (cinfo->global_state != DSTATE_BUFIMAGE && cinfo->global_state != DSTATE_PRESCAN) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Limit scan number to valid range */ if (scan_number <= 0) scan_number = 1; if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number) scan_number = cinfo->input_scan_number; cinfo->output_scan_number = scan_number; /* Perform any dummy output passes, and set up for the real pass */ return output_pass_setup(cinfo); } /* * Finish up after an output pass in buffered-image mode. * * Returns FALSE if suspended. The return value need be inspected only if * a suspending data source is used. */ GLOBAL(boolean) jpeg_finish_output (j_decompress_ptr cinfo) { if ((cinfo->global_state == DSTATE_SCANNING || cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) { /* Terminate this pass. */ /* We do not require the whole pass to have been completed. */ (*cinfo->master->finish_output_pass) (cinfo); cinfo->global_state = DSTATE_BUFPOST; } else if (cinfo->global_state != DSTATE_BUFPOST) { /* BUFPOST = repeat call after a suspension, anything else is error */ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); } /* Read markers looking for SOS or EOI */ while (cinfo->input_scan_number <= cinfo->output_scan_number && ! cinfo->inputctl->eoi_reached) { if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) return FALSE; /* Suspend, come back later */ } cinfo->global_state = DSTATE_BUFIMAGE; return TRUE; } #endif /* D_MULTISCAN_FILES_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jidctflt.c0000664000175000017500000002040312013417376020220 0ustar alfalf00000000000000/* * jidctflt.c * * Copyright (C) 1994-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a floating-point implementation of the * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine * must also perform dequantization of the input coefficients. * * This implementation should be more accurate than either of the integer * IDCT implementations. However, it may not give the same results on all * machines because of differences in roundoff behavior. Speed will depend * on the hardware's floating point capacity. * * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT * on each row (or vice versa, but it's more convenient to emit a row at * a time). Direct algorithms are also available, but they are much more * complex and seem not to be any faster when reduced to code. * * This implementation is based on Arai, Agui, and Nakajima's algorithm for * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in * Japanese, but the algorithm is described in the Pennebaker & Mitchell * JPEG textbook (see REFERENCES section in file README). The following code * is based directly on figure 4-8 in P&M. * While an 8-point DCT cannot be done in less than 11 multiplies, it is * possible to arrange the computation so that many of the multiplies are * simple scalings of the final outputs. These multiplies can then be * folded into the multiplications or divisions by the JPEG quantization * table entries. The AA&N method leaves only 5 multiplies and 29 adds * to be done in the DCT itself. * The primary disadvantage of this method is that with a fixed-point * implementation, accuracy is lost due to imprecise representation of the * scaled quantization values. However, that problem does not arise if * we use floating point arithmetic. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_FLOAT_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* Dequantize a coefficient by multiplying it by the multiplier-table * entry; produce a float result. */ #define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) /* * Perform dequantization and inverse DCT on one block of coefficients. */ GLOBAL(void) jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; FAST_FLOAT tmp10, tmp11, tmp12, tmp13; FAST_FLOAT z5, z10, z11, z12, z13; JCOEFPTR inptr; FLOAT_MULT_TYPE * quantptr; FAST_FLOAT * wsptr; JSAMPROW outptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); int ctr; FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ SHIFT_TEMPS /* Pass 1: process columns from input, store into work array. */ inptr = coef_block; quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table; wsptr = workspace; for (ctr = DCTSIZE; ctr > 0; ctr--) { /* Due to quantization, we will usually find that many of the input * coefficients are zero, especially the AC terms. We can exploit this * by short-circuiting the IDCT calculation for any column in which all * the AC terms are zero. In that case each output is equal to the * DC coefficient (with scale factor as needed). * With typical images and quantization tables, half or more of the * column DCT calculations can be simplified this way. */ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; wsptr[DCTSIZE*2] = dcval; wsptr[DCTSIZE*3] = dcval; wsptr[DCTSIZE*4] = dcval; wsptr[DCTSIZE*5] = dcval; wsptr[DCTSIZE*6] = dcval; wsptr[DCTSIZE*7] = dcval; inptr++; /* advance pointers to next column */ quantptr++; wsptr++; continue; } /* Even part */ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); tmp10 = tmp0 + tmp2; /* phase 3 */ tmp11 = tmp0 - tmp2; tmp13 = tmp1 + tmp3; /* phases 5-3 */ tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ tmp0 = tmp10 + tmp13; /* phase 2 */ tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); z13 = tmp6 + tmp5; /* phase 6 */ z10 = tmp6 - tmp5; z11 = tmp4 + tmp7; z12 = tmp4 - tmp7; tmp7 = z11 + z13; /* phase 5 */ tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ tmp6 = tmp12 - tmp7; /* phase 2 */ tmp5 = tmp11 - tmp6; tmp4 = tmp10 + tmp5; wsptr[DCTSIZE*0] = tmp0 + tmp7; wsptr[DCTSIZE*7] = tmp0 - tmp7; wsptr[DCTSIZE*1] = tmp1 + tmp6; wsptr[DCTSIZE*6] = tmp1 - tmp6; wsptr[DCTSIZE*2] = tmp2 + tmp5; wsptr[DCTSIZE*5] = tmp2 - tmp5; wsptr[DCTSIZE*4] = tmp3 + tmp4; wsptr[DCTSIZE*3] = tmp3 - tmp4; inptr++; /* advance pointers to next column */ quantptr++; wsptr++; } /* Pass 2: process rows from work array, store into output array. */ /* Note that we must descale the results by a factor of 8 == 2**3. */ wsptr = workspace; for (ctr = 0; ctr < DCTSIZE; ctr++) { outptr = output_buf[ctr] + output_col; /* Rows of zeroes can be exploited in the same way as we did with columns. * However, the column calculation has created many nonzero AC terms, so * the simplification applies less often (typically 5% to 10% of the time). * And testing floats for zero is relatively expensive, so we don't bother. */ /* Even part */ tmp10 = wsptr[0] + wsptr[4]; tmp11 = wsptr[0] - wsptr[4]; tmp13 = wsptr[2] + wsptr[6]; tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; tmp0 = tmp10 + tmp13; tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ z13 = wsptr[5] + wsptr[3]; z10 = wsptr[5] - wsptr[3]; z11 = wsptr[1] + wsptr[7]; z12 = wsptr[1] - wsptr[7]; tmp7 = z11 + z13; tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ tmp6 = tmp12 - tmp7; tmp5 = tmp11 - tmp6; tmp4 = tmp10 + tmp5; /* Final output stage: scale down by a factor of 8 and range-limit */ outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3) & RANGE_MASK]; outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3) & RANGE_MASK]; outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3) & RANGE_MASK]; outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3) & RANGE_MASK]; outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3) & RANGE_MASK]; outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3) & RANGE_MASK]; outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3) & RANGE_MASK]; outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ } } #endif /* DCT_FLOAT_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jctrans.c0000664000175000017500000003403112013417376020063 0ustar alfalf00000000000000/* * jctrans.c * * Copyright (C) 1995-1998, Thomas G. Lane. * Modified 2000-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains library routines for transcoding compression, * that is, writing raw DCT coefficient arrays to an output JPEG file. * The routines in jcapimin.c will also be needed by a transcoder. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Forward declarations */ LOCAL(void) transencode_master_selection JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); LOCAL(void) transencode_coef_controller JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); /* * Compression initialization for writing raw-coefficient data. * Before calling this, all parameters and a data destination must be set up. * Call jpeg_finish_compress() to actually write the data. * * The number of passed virtual arrays must match cinfo->num_components. * Note that the virtual arrays need not be filled or even realized at * the time write_coefficients is called; indeed, if the virtual arrays * were requested from this compression object's memory manager, they * typically will be realized during this routine and filled afterwards. */ GLOBAL(void) jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) { if (cinfo->global_state != CSTATE_START) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); /* Mark all tables to be written */ jpeg_suppress_tables(cinfo, FALSE); /* (Re)initialize error mgr and destination modules */ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); (*cinfo->dest->init_destination) (cinfo); /* Perform master selection of active modules */ transencode_master_selection(cinfo, coef_arrays); /* Wait for jpeg_finish_compress() call */ cinfo->next_scanline = 0; /* so jpeg_write_marker works */ cinfo->global_state = CSTATE_WRCOEFS; } /* * Initialize the compression object with default parameters, * then copy from the source object all parameters needed for lossless * transcoding. Parameters that can be varied without loss (such as * scan script and Huffman optimization) are left in their default states. */ GLOBAL(void) jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, j_compress_ptr dstinfo) { JQUANT_TBL ** qtblptr; jpeg_component_info *incomp, *outcomp; JQUANT_TBL *c_quant, *slot_quant; int tblno, ci, coefi; /* Safety check to ensure start_compress not called yet. */ if (dstinfo->global_state != CSTATE_START) ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); /* Copy fundamental image dimensions */ dstinfo->image_width = srcinfo->image_width; dstinfo->image_height = srcinfo->image_height; dstinfo->input_components = srcinfo->num_components; dstinfo->in_color_space = srcinfo->jpeg_color_space; #if JPEG_LIB_VERSION >= 70 dstinfo->jpeg_width = srcinfo->output_width; dstinfo->jpeg_height = srcinfo->output_height; dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size; dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size; #endif /* Initialize all parameters to default values */ jpeg_set_defaults(dstinfo); /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. * Fix it to get the right header markers for the image colorspace. */ jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); dstinfo->data_precision = srcinfo->data_precision; dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; /* Copy the source's quantization tables. */ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; if (*qtblptr == NULL) *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); MEMCOPY((*qtblptr)->quantval, srcinfo->quant_tbl_ptrs[tblno]->quantval, SIZEOF((*qtblptr)->quantval)); (*qtblptr)->sent_table = FALSE; } } /* Copy the source's per-component info. * Note we assume jpeg_set_defaults has allocated the dest comp_info array. */ dstinfo->num_components = srcinfo->num_components; if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, MAX_COMPONENTS); for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; ci < dstinfo->num_components; ci++, incomp++, outcomp++) { outcomp->component_id = incomp->component_id; outcomp->h_samp_factor = incomp->h_samp_factor; outcomp->v_samp_factor = incomp->v_samp_factor; outcomp->quant_tbl_no = incomp->quant_tbl_no; /* Make sure saved quantization table for component matches the qtable * slot. If not, the input file re-used this qtable slot. * IJG encoder currently cannot duplicate this. */ tblno = outcomp->quant_tbl_no; if (tblno < 0 || tblno >= NUM_QUANT_TBLS || srcinfo->quant_tbl_ptrs[tblno] == NULL) ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); slot_quant = srcinfo->quant_tbl_ptrs[tblno]; c_quant = incomp->quant_table; if (c_quant != NULL) { for (coefi = 0; coefi < DCTSIZE2; coefi++) { if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); } } /* Note: we do not copy the source's Huffman table assignments; * instead we rely on jpeg_set_colorspace to have made a suitable choice. */ } /* Also copy JFIF version and resolution information, if available. * Strictly speaking this isn't "critical" info, but it's nearly * always appropriate to copy it if available. In particular, * if the application chooses to copy JFIF 1.02 extension markers from * the source file, we need to copy the version to make sure we don't * emit a file that has 1.02 extensions but a claimed version of 1.01. * We will *not*, however, copy version info from mislabeled "2.01" files. */ if (srcinfo->saw_JFIF_marker) { if (srcinfo->JFIF_major_version == 1) { dstinfo->JFIF_major_version = srcinfo->JFIF_major_version; dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version; } dstinfo->density_unit = srcinfo->density_unit; dstinfo->X_density = srcinfo->X_density; dstinfo->Y_density = srcinfo->Y_density; } } /* * Master selection of compression modules for transcoding. * This substitutes for jcinit.c's initialization of the full compressor. */ LOCAL(void) transencode_master_selection (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) { /* Although we don't actually use input_components for transcoding, * jcmaster.c's initial_setup will complain if input_components is 0. */ cinfo->input_components = 1; /* Initialize master control (includes parameter checking/processing) */ jinit_c_master_control(cinfo, TRUE /* transcode only */); /* Entropy encoding: either Huffman or arithmetic coding. */ if (cinfo->arith_code) { #ifdef C_ARITH_CODING_SUPPORTED jinit_arith_encoder(cinfo); #else ERREXIT(cinfo, JERR_ARITH_NOTIMPL); #endif } else { if (cinfo->progressive_mode) { #ifdef C_PROGRESSIVE_SUPPORTED jinit_phuff_encoder(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else jinit_huff_encoder(cinfo); } /* We need a special coefficient buffer controller. */ transencode_coef_controller(cinfo, coef_arrays); jinit_marker_writer(cinfo); /* We can now tell the memory manager to allocate virtual arrays. */ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); /* Write the datastream header (SOI, JFIF) immediately. * Frame and scan headers are postponed till later. * This lets application insert special markers after the SOI. */ (*cinfo->marker->write_file_header) (cinfo); } /* * The rest of this file is a special implementation of the coefficient * buffer controller. This is similar to jccoefct.c, but it handles only * output from presupplied virtual arrays. Furthermore, we generate any * dummy padding blocks on-the-fly rather than expecting them to be present * in the arrays. */ /* Private buffer controller object */ typedef struct { struct jpeg_c_coef_controller pub; /* public fields */ JDIMENSION iMCU_row_num; /* iMCU row # within image */ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ int MCU_vert_offset; /* counts MCU rows within iMCU row */ int MCU_rows_per_iMCU_row; /* number of such rows needed */ /* Virtual block array for each component. */ jvirt_barray_ptr * whole_image; /* Workspace for constructing dummy blocks at right/bottom edges. */ JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]; } my_coef_controller; typedef my_coef_controller * my_coef_ptr; LOCAL(void) start_iMCU_row (j_compress_ptr cinfo) /* Reset within-iMCU-row counters for a new row */ { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; /* In an interleaved scan, an MCU row is the same as an iMCU row. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. * But at the bottom of the image, process only what's left. */ if (cinfo->comps_in_scan > 1) { coef->MCU_rows_per_iMCU_row = 1; } else { if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; else coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; } coef->mcu_ctr = 0; coef->MCU_vert_offset = 0; } /* * Initialize for a processing pass. */ METHODDEF(void) start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; if (pass_mode != JBUF_CRANK_DEST) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); coef->iMCU_row_num = 0; start_iMCU_row(cinfo); } /* * Process some data. * We process the equivalent of one fully interleaved MCU row ("iMCU" row) * per call, ie, v_samp_factor block rows for each component in the scan. * The data is obtained from the virtual arrays and fed to the entropy coder. * Returns TRUE if the iMCU row is completed, FALSE if suspended. * * NB: input_buf is ignored; it is likely to be a NULL pointer. */ METHODDEF(boolean) compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) { my_coef_ptr coef = (my_coef_ptr) cinfo->coef; JDIMENSION MCU_col_num; /* index of current MCU within row */ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; int blkn, ci, xindex, yindex, yoffset, blockcnt; JDIMENSION start_col; JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; JBLOCKROW buffer_ptr; jpeg_component_info *compptr; /* Align the virtual buffers for the components used in this scan. */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; buffer[ci] = (*cinfo->mem->access_virt_barray) ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], coef->iMCU_row_num * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, FALSE); } /* Loop to process one whole iMCU row */ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; yoffset++) { for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) { /* Construct list of pointers to DCT blocks belonging to this MCU */ blkn = 0; /* index of current DCT block within MCU */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; start_col = MCU_col_num * compptr->MCU_width; blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width : compptr->last_col_width; for (yindex = 0; yindex < compptr->MCU_height; yindex++) { if (coef->iMCU_row_num < last_iMCU_row || yindex+yoffset < compptr->last_row_height) { /* Fill in pointers to real blocks in this row */ buffer_ptr = buffer[ci][yindex+yoffset] + start_col; for (xindex = 0; xindex < blockcnt; xindex++) MCU_buffer[blkn++] = buffer_ptr++; } else { /* At bottom of image, need a whole row of dummy blocks */ xindex = 0; } /* Fill in any dummy blocks needed in this row. * Dummy blocks are filled in the same way as in jccoefct.c: * all zeroes in the AC entries, DC entries equal to previous * block's DC value. The init routine has already zeroed the * AC entries, so we need only set the DC entries correctly. */ for (; xindex < compptr->MCU_width; xindex++) { MCU_buffer[blkn] = coef->dummy_buffer[blkn]; MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0]; blkn++; } } } /* Try to write the MCU. */ if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) { /* Suspension forced; update state counters and exit */ coef->MCU_vert_offset = yoffset; coef->mcu_ctr = MCU_col_num; return FALSE; } } /* Completed an MCU row, but perhaps not an iMCU row */ coef->mcu_ctr = 0; } /* Completed the iMCU row, advance counters for next one */ coef->iMCU_row_num++; start_iMCU_row(cinfo); return TRUE; } /* * Initialize coefficient buffer controller. * * Each passed coefficient array must be the right size for that * coefficient: width_in_blocks wide and height_in_blocks high, * with unitheight at least v_samp_factor. */ LOCAL(void) transencode_coef_controller (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) { my_coef_ptr coef; JBLOCKROW buffer; int i; coef = (my_coef_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller)); cinfo->coef = (struct jpeg_c_coef_controller *) coef; coef->pub.start_pass = start_pass_coef; coef->pub.compress_data = compress_output; /* Save pointer to virtual arrays */ coef->whole_image = coef_arrays; /* Allocate and pre-zero space for dummy DCT blocks. */ buffer = (JBLOCKROW) (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { coef->dummy_buffer[i] = buffer + i; } } glmark2-2012.08/./src/libjpeg-turbo/jerror.h0000664000175000017500000003463412013417376017740 0ustar alfalf00000000000000/* * jerror.h * * Copyright (C) 1994-1997, Thomas G. Lane. * Modified 1997-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file defines the error and message codes for the JPEG library. * Edit this file to add new codes, or to translate the message strings to * some other language. * A set of error-reporting macros are defined too. Some applications using * the JPEG library may wish to include this file to get the error codes * and/or the macros. */ /* * To define the enum list of message codes, include this file without * defining macro JMESSAGE. To create a message string table, include it * again with a suitable JMESSAGE definition (see jerror.c for an example). */ #ifndef JMESSAGE #ifndef JERROR_H /* First time through, define the enum list */ #define JMAKE_ENUM_LIST #else /* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */ #define JMESSAGE(code,string) #endif /* JERROR_H */ #endif /* JMESSAGE */ #ifdef JMAKE_ENUM_LIST typedef enum { #define JMESSAGE(code,string) code , #endif /* JMAKE_ENUM_LIST */ JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */ /* For maintenance convenience, list is alphabetical by message code name */ #if JPEG_LIB_VERSION < 70 JMESSAGE(JERR_ARITH_NOTIMPL, "Sorry, arithmetic coding is not implemented") #endif JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix") JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix") JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode") JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS") #if JPEG_LIB_VERSION >= 70 JMESSAGE(JERR_BAD_CROP_SPEC, "Invalid crop request") #endif JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range") JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported") #if JPEG_LIB_VERSION >= 70 JMESSAGE(JERR_BAD_DROP_SAMPLING, "Component index %d: mismatching sampling ratio %d:%d, %d:%d, %c") #endif JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition") JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace") JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace") JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length") JMESSAGE(JERR_BAD_LIB_VERSION, "Wrong JPEG library version: library is %d, caller expects %d") JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan") JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d") JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d") JMESSAGE(JERR_BAD_PROGRESSION, "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d") JMESSAGE(JERR_BAD_PROG_SCRIPT, "Invalid progressive parameters at scan script entry %d") JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors") JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d") JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d") JMESSAGE(JERR_BAD_STRUCT_SIZE, "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u") JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access") JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small") JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here") JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet") JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d") JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request") JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d") JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x") JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d") JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d") JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)") JMESSAGE(JERR_EMS_READ, "Read from EMS failed") JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed") JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan") JMESSAGE(JERR_FILE_READ, "Input file read error") JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?") JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet") JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow") JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry") JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels") JMESSAGE(JERR_INPUT_EMPTY, "Empty input file") JMESSAGE(JERR_INPUT_EOF, "Premature end of input file") JMESSAGE(JERR_MISMATCHED_QUANT_TABLE, "Cannot transcode due to multiple use of quantization table %d") JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data") JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change") JMESSAGE(JERR_NOTIMPL, "Not implemented yet") JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time") #if JPEG_LIB_VERSION >= 70 JMESSAGE(JERR_NO_ARITH_TABLE, "Arithmetic table 0x%02x was not defined") #endif JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported") JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined") JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image") JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined") JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x") JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)") JMESSAGE(JERR_QUANT_COMPONENTS, "Cannot quantize more than %d color components") JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors") JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors") JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers") JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker") JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x") JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers") JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF") JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s") JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file") JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file") JMESSAGE(JERR_TFILE_WRITE, "Write failed on temporary file --- out of disk space?") JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines") JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x") JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up") JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation") JMESSAGE(JERR_XMS_READ, "Read from XMS failed") JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed") JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT) JMESSAGE(JMSG_VERSION, JVERSION) JMESSAGE(JTRC_16BIT_TABLES, "Caution: quantization tables are too coarse for baseline JPEG") JMESSAGE(JTRC_ADOBE, "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d") JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u") JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u") JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x") JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x") JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d") JMESSAGE(JTRC_DRI, "Define Restart Interval %u") JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u") JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u") JMESSAGE(JTRC_EOI, "End Of Image") JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d") JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d") JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE, "Warning: thumbnail image size does not match data length %u") JMESSAGE(JTRC_JFIF_EXTENSION, "JFIF extension marker: type 0x%02x, length %u") JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image") JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u") JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x") JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u") JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors") JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors") JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization") JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d") JMESSAGE(JTRC_RST, "RST%d") JMESSAGE(JTRC_SMOOTH_NOTIMPL, "Smoothing not supported with nonstandard sampling ratios") JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d") JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d") JMESSAGE(JTRC_SOI, "Start of Image") JMESSAGE(JTRC_SOS, "Start Of Scan: %d components") JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d") JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d") JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s") JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s") JMESSAGE(JTRC_THUMB_JPEG, "JFIF extension marker: JPEG-compressed thumbnail image, length %u") JMESSAGE(JTRC_THUMB_PALETTE, "JFIF extension marker: palette thumbnail image, length %u") JMESSAGE(JTRC_THUMB_RGB, "JFIF extension marker: RGB thumbnail image, length %u") JMESSAGE(JTRC_UNKNOWN_IDS, "Unrecognized component IDs %d %d %d, assuming YCbCr") JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u") JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u") JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d") #if JPEG_LIB_VERSION >= 70 JMESSAGE(JWRN_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code") #endif JMESSAGE(JWRN_BOGUS_PROGRESSION, "Inconsistent progression sequence for component %d coefficient %d") JMESSAGE(JWRN_EXTRANEOUS_DATA, "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x") JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment") JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code") JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d") JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file") JMESSAGE(JWRN_MUST_RESYNC, "Corrupt JPEG data: found marker 0x%02x instead of RST%d") JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG") JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines") #if JPEG_LIB_VERSION < 70 JMESSAGE(JERR_BAD_CROP_SPEC, "Invalid crop request") #if defined(C_ARITH_CODING_SUPPORTED) || defined(D_ARITH_CODING_SUPPORTED) JMESSAGE(JERR_NO_ARITH_TABLE, "Arithmetic table 0x%02x was not defined") JMESSAGE(JWRN_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code") #endif #endif #ifdef JMAKE_ENUM_LIST JMSG_LASTMSGCODE } J_MESSAGE_CODE; #undef JMAKE_ENUM_LIST #endif /* JMAKE_ENUM_LIST */ /* Zap JMESSAGE macro so that future re-inclusions do nothing by default */ #undef JMESSAGE #ifndef JERROR_H #define JERROR_H /* Macros to simplify using the error and trace message stuff */ /* The first parameter is either type of cinfo pointer */ /* Fatal errors (print message and exit) */ #define ERREXIT(cinfo,code) \ ((cinfo)->err->msg_code = (code), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define ERREXIT1(cinfo,code,p1) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define ERREXIT2(cinfo,code,p1,p2) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (cinfo)->err->msg_parm.i[1] = (p2), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define ERREXIT3(cinfo,code,p1,p2,p3) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (cinfo)->err->msg_parm.i[1] = (p2), \ (cinfo)->err->msg_parm.i[2] = (p3), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define ERREXIT4(cinfo,code,p1,p2,p3,p4) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (cinfo)->err->msg_parm.i[1] = (p2), \ (cinfo)->err->msg_parm.i[2] = (p3), \ (cinfo)->err->msg_parm.i[3] = (p4), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define ERREXITS(cinfo,code,str) \ ((cinfo)->err->msg_code = (code), \ strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo))) #define MAKESTMT(stuff) do { stuff } while (0) /* Nonfatal errors (we can keep going, but the data is probably corrupt) */ #define WARNMS(cinfo,code) \ ((cinfo)->err->msg_code = (code), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) #define WARNMS1(cinfo,code,p1) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) #define WARNMS2(cinfo,code,p1,p2) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (cinfo)->err->msg_parm.i[1] = (p2), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1)) /* Informational/debugging messages */ #define TRACEMS(cinfo,lvl,code) \ ((cinfo)->err->msg_code = (code), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) #define TRACEMS1(cinfo,lvl,code,p1) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) #define TRACEMS2(cinfo,lvl,code,p1,p2) \ ((cinfo)->err->msg_code = (code), \ (cinfo)->err->msg_parm.i[0] = (p1), \ (cinfo)->err->msg_parm.i[1] = (p2), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) #define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \ (cinfo)->err->msg_code = (code); \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) #define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ (cinfo)->err->msg_code = (code); \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) #define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ _mp[4] = (p5); \ (cinfo)->err->msg_code = (code); \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) #define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \ _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \ (cinfo)->err->msg_code = (code); \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); ) #define TRACEMSS(cinfo,lvl,code,str) \ ((cinfo)->err->msg_code = (code), \ strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl))) #endif /* JERROR_H */ glmark2-2012.08/./src/libjpeg-turbo/jcarith.c0000664000175000017500000006674012013417376020057 0ustar alfalf00000000000000/* * jcarith.c * * Developed 1997-2009 by Guido Vollbeding. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains portable arithmetic entropy encoding routines for JPEG * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). * * Both sequential and progressive modes are supported in this single module. * * Suspension is not currently supported in this module. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* Expanded entropy encoder object for arithmetic encoding. */ typedef struct { struct jpeg_entropy_encoder pub; /* public fields */ INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */ INT32 a; /* A register, normalized size of coding interval */ INT32 sc; /* counter for stacked 0xFF values which might overflow */ INT32 zc; /* counter for pending 0x00 output values which might * * be discarded at the end ("Pacman" termination) */ int ct; /* bit shift counter, determines when next byte will be written */ int buffer; /* buffer for most recent output byte != 0xFF */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ int next_restart_num; /* next restart number to write (0-7) */ /* Pointers to statistics areas (these workspaces have image lifespan) */ unsigned char * dc_stats[NUM_ARITH_TBLS]; unsigned char * ac_stats[NUM_ARITH_TBLS]; /* Statistics bin for coding with fixed probability 0.5 */ unsigned char fixed_bin[4]; } arith_entropy_encoder; typedef arith_entropy_encoder * arith_entropy_ptr; /* The following two definitions specify the allocation chunk size * for the statistics area. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least * 49 statistics bins for DC, and 245 statistics bins for AC coding. * * We use a compact representation with 1 byte per statistics bin, * thus the numbers directly represent byte sizes. * This 1 byte per statistics bin contains the meaning of the MPS * (more probable symbol) in the highest bit (mask 0x80), and the * index into the probability estimation state machine table * in the lower bits (mask 0x7F). */ #define DC_STAT_BINS 64 #define AC_STAT_BINS 256 /* NOTE: Uncomment the following #define if you want to use the * given formula for calculating the AC conditioning parameter Kx * for spectral selection progressive coding in section G.1.3.2 * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4). * Although the spec and P&M authors claim that this "has proven * to give good results for 8 bit precision samples", I'm not * convinced yet that this is really beneficial. * Early tests gave only very marginal compression enhancements * (a few - around 5 or so - bytes even for very large files), * which would turn out rather negative if we'd suppress the * DAC (Define Arithmetic Conditioning) marker segments for * the default parameters in the future. * Note that currently the marker writing module emits 12-byte * DAC segments for a full-component scan in a color image. * This is not worth worrying about IMHO. However, since the * spec defines the default values to be used if the tables * are omitted (unlike Huffman tables, which are required * anyway), one might optimize this behaviour in the future, * and then it would be disadvantageous to use custom tables if * they don't provide sufficient gain to exceed the DAC size. * * On the other hand, I'd consider it as a reasonable result * that the conditioning has no significant influence on the * compression performance. This means that the basic * statistical model is already rather stable. * * Thus, at the moment, we use the default conditioning values * anyway, and do not use the custom formula. * #define CALCULATE_SPECTRAL_CONDITIONING */ /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. * We assume that int right shift is unsigned if INT32 right shift is, * which should be safe. */ #ifdef RIGHT_SHIFT_IS_UNSIGNED #define ISHIFT_TEMPS int ishift_temp; #define IRIGHT_SHIFT(x,shft) \ ((ishift_temp = (x)) < 0 ? \ (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ (ishift_temp >> (shft))) #else #define ISHIFT_TEMPS #define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) #endif LOCAL(void) emit_byte (int val, j_compress_ptr cinfo) /* Write next output byte; we do not support suspension in this module. */ { struct jpeg_destination_mgr * dest = cinfo->dest; *dest->next_output_byte++ = (JOCTET) val; if (--dest->free_in_buffer == 0) if (! (*dest->empty_output_buffer) (cinfo)) ERREXIT(cinfo, JERR_CANT_SUSPEND); } /* * Finish up at the end of an arithmetic-compressed scan. */ METHODDEF(void) finish_pass (j_compress_ptr cinfo) { arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; INT32 temp; /* Section D.1.8: Termination of encoding */ /* Find the e->c in the coding interval with the largest * number of trailing zero bits */ if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c) e->c = temp + 0x8000L; else e->c = temp; /* Send remaining bytes to output */ e->c <<= e->ct; if (e->c & 0xF8000000L) { /* One final overflow has to be handled */ if (e->buffer >= 0) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); emit_byte(e->buffer + 1, cinfo); if (e->buffer + 1 == 0xFF) emit_byte(0x00, cinfo); } e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ e->sc = 0; } else { if (e->buffer == 0) ++e->zc; else if (e->buffer >= 0) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); emit_byte(e->buffer, cinfo); } if (e->sc) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); do { emit_byte(0xFF, cinfo); emit_byte(0x00, cinfo); } while (--e->sc); } } /* Output final bytes only if they are not 0x00 */ if (e->c & 0x7FFF800L) { if (e->zc) /* output final pending zero bytes */ do emit_byte(0x00, cinfo); while (--e->zc); emit_byte((e->c >> 19) & 0xFF, cinfo); if (((e->c >> 19) & 0xFF) == 0xFF) emit_byte(0x00, cinfo); if (e->c & 0x7F800L) { emit_byte((e->c >> 11) & 0xFF, cinfo); if (((e->c >> 11) & 0xFF) == 0xFF) emit_byte(0x00, cinfo); } } } /* * The core arithmetic encoding routine (common in JPEG and JBIG). * This needs to go as fast as possible. * Machine-dependent optimization facilities * are not utilized in this portable implementation. * However, this code should be fairly efficient and * may be a good base for further optimizations anyway. * * Parameter 'val' to be encoded may be 0 or 1 (binary decision). * * Note: I've added full "Pacman" termination support to the * byte output routines, which is equivalent to the optional * Discard_final_zeros procedure (Figure D.15) in the spec. * Thus, we always produce the shortest possible output * stream compliant to the spec (no trailing zero bytes, * except for FF stuffing). * * I've also introduced a new scheme for accessing * the probability estimation state machine table, * derived from Markus Kuhn's JBIG implementation. */ LOCAL(void) arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) { register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; register unsigned char nl, nm; register INT32 qe, temp; register int sv; /* Fetch values from our compact representation of Table D.2: * Qe values and probability estimation state machine */ sv = *st; qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ /* Encode & estimation procedures per sections D.1.4 & D.1.5 */ e->a -= qe; if (val != (sv >> 7)) { /* Encode the less probable symbol */ if (e->a >= qe) { /* If the interval size (qe) for the less probable symbol (LPS) * is larger than the interval size for the MPS, then exchange * the two symbols for coding efficiency, otherwise code the LPS * as usual: */ e->c += e->a; e->a = qe; } *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ } else { /* Encode the more probable symbol */ if (e->a >= 0x8000L) return; /* A >= 0x8000 -> ready, no renormalization required */ if (e->a < qe) { /* If the interval size (qe) for the less probable symbol (LPS) * is larger than the interval size for the MPS, then exchange * the two symbols for coding efficiency: */ e->c += e->a; e->a = qe; } *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ } /* Renormalization & data output per section D.1.6 */ do { e->a <<= 1; e->c <<= 1; if (--e->ct == 0) { /* Another byte is ready for output */ temp = e->c >> 19; if (temp > 0xFF) { /* Handle overflow over all stacked 0xFF bytes */ if (e->buffer >= 0) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); emit_byte(e->buffer + 1, cinfo); if (e->buffer + 1 == 0xFF) emit_byte(0x00, cinfo); } e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ e->sc = 0; /* Note: The 3 spacer bits in the C register guarantee * that the new buffer byte can't be 0xFF here * (see page 160 in the P&M JPEG book). */ e->buffer = temp & 0xFF; /* new output byte, might overflow later */ } else if (temp == 0xFF) { ++e->sc; /* stack 0xFF byte (which might overflow later) */ } else { /* Output all stacked 0xFF bytes, they will not overflow any more */ if (e->buffer == 0) ++e->zc; else if (e->buffer >= 0) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); emit_byte(e->buffer, cinfo); } if (e->sc) { if (e->zc) do emit_byte(0x00, cinfo); while (--e->zc); do { emit_byte(0xFF, cinfo); emit_byte(0x00, cinfo); } while (--e->sc); } e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ } e->c &= 0x7FFFFL; e->ct += 8; } } while (e->a < 0x8000L); } /* * Emit a restart marker & resynchronize predictions. */ LOCAL(void) emit_restart (j_compress_ptr cinfo, int restart_num) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; int ci; jpeg_component_info * compptr; finish_pass(cinfo); emit_byte(0xFF, cinfo); emit_byte(JPEG_RST0 + restart_num, cinfo); /* Re-initialize statistics areas */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* DC needs no table for refinement scan */ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); /* Reset DC predictions to 0 */ entropy->last_dc_val[ci] = 0; entropy->dc_context[ci] = 0; } /* AC needs no table when not present */ if (cinfo->progressive_mode == 0 || cinfo->Se) { MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); } } /* Reset arithmetic encoding variables */ entropy->c = 0; entropy->a = 0x10000L; entropy->sc = 0; entropy->zc = 0; entropy->ct = 11; entropy->buffer = -1; /* empty */ } /* * MCU encoding for DC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; unsigned char *st; int blkn, ci, tbl; int v, v2, m; ISHIFT_TEMPS /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { emit_restart(cinfo, entropy->next_restart_num); entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; /* Compute the DC value after the required point transform by Al. * This is simply an arithmetic right shift. */ m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al); /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; /* Figure F.4: Encode_DC_DIFF */ if ((v = m - entropy->last_dc_val[ci]) == 0) { arith_encode(cinfo, st, 0); entropy->dc_context[ci] = 0; /* zero diff category */ } else { entropy->last_dc_val[ci] = m; arith_encode(cinfo, st, 1); /* Figure F.6: Encoding nonzero value v */ /* Figure F.7: Encoding the sign of v */ if (v > 0) { arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ st += 2; /* Table F.4: SP = S0 + 2 */ entropy->dc_context[ci] = 4; /* small positive diff category */ } else { v = -v; arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ st += 3; /* Table F.4: SN = S0 + 3 */ entropy->dc_context[ci] = 8; /* small negative diff category */ } /* Figure F.8: Encoding the magnitude category of v */ m = 0; if (v -= 1) { arith_encode(cinfo, st, 1); m = 1; v2 = v; st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st += 1; } } arith_encode(cinfo, st, 0); /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) entropy->dc_context[ci] = 0; /* zero diff category */ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) entropy->dc_context[ci] += 8; /* large diff category */ /* Figure F.9: Encoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) arith_encode(cinfo, st, (m & v) ? 1 : 0); } } return TRUE; } /* * MCU encoding for AC initial scan (either spectral selection, * or first pass of successive approximation). */ METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; unsigned char *st; int tbl, k, ke; int v, v2, m; /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { emit_restart(cinfo, entropy->next_restart_num); entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } /* Encode the MCU data block */ block = MCU_data[0]; tbl = cinfo->cur_comp_info[0]->ac_tbl_no; /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ /* Establish EOB (end-of-block) index */ for (ke = cinfo->Se; ke > 0; ke--) /* We must apply the point transform by Al. For AC coefficients this * is an integer division with rounding towards 0. To do this portably * in C, we shift after obtaining the absolute value. */ if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) { if (v >>= cinfo->Al) break; } else { v = -v; if (v >>= cinfo->Al) break; } /* Figure F.5: Encode_AC_Coefficients */ for (k = cinfo->Ss; k <= ke; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); arith_encode(cinfo, st, 0); /* EOB decision */ for (;;) { if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { if (v >>= cinfo->Al) { arith_encode(cinfo, st + 1, 1); arith_encode(cinfo, entropy->fixed_bin, 0); break; } } else { v = -v; if (v >>= cinfo->Al) { arith_encode(cinfo, st + 1, 1); arith_encode(cinfo, entropy->fixed_bin, 1); break; } } arith_encode(cinfo, st + 1, 0); st += 3; k++; } st += 2; /* Figure F.8: Encoding the magnitude category of v */ m = 0; if (v -= 1) { arith_encode(cinfo, st, 1); m = 1; v2 = v; if (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st = entropy->ac_stats[tbl] + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st += 1; } } } arith_encode(cinfo, st, 0); /* Figure F.9: Encoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) arith_encode(cinfo, st, (m & v) ? 1 : 0); } /* Encode EOB decision only if k <= cinfo->Se */ if (k <= cinfo->Se) { st = entropy->ac_stats[tbl] + 3 * (k - 1); arith_encode(cinfo, st, 1); } return TRUE; } /* * MCU encoding for DC successive approximation refinement scan. */ METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; unsigned char *st; int Al, blkn; /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { emit_restart(cinfo, entropy->next_restart_num); entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } st = entropy->fixed_bin; /* use fixed probability estimation */ Al = cinfo->Al; /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { /* We simply emit the Al'th bit of the DC coefficient value. */ arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1); } return TRUE; } /* * MCU encoding for AC successive approximation refinement scan. */ METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; JBLOCKROW block; unsigned char *st; int tbl, k, ke, kex; int v; /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { emit_restart(cinfo, entropy->next_restart_num); entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } /* Encode the MCU data block */ block = MCU_data[0]; tbl = cinfo->cur_comp_info[0]->ac_tbl_no; /* Section G.1.3.3: Encoding of AC coefficients */ /* Establish EOB (end-of-block) index */ for (ke = cinfo->Se; ke > 0; ke--) /* We must apply the point transform by Al. For AC coefficients this * is an integer division with rounding towards 0. To do this portably * in C, we shift after obtaining the absolute value. */ if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) { if (v >>= cinfo->Al) break; } else { v = -v; if (v >>= cinfo->Al) break; } /* Establish EOBx (previous stage end-of-block) index */ for (kex = ke; kex > 0; kex--) if ((v = (*block)[jpeg_natural_order[kex]]) >= 0) { if (v >>= cinfo->Ah) break; } else { v = -v; if (v >>= cinfo->Ah) break; } /* Figure G.10: Encode_AC_Coefficients_SA */ for (k = cinfo->Ss; k <= ke; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); if (k > kex) arith_encode(cinfo, st, 0); /* EOB decision */ for (;;) { if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { if (v >>= cinfo->Al) { if (v >> 1) /* previously nonzero coef */ arith_encode(cinfo, st + 2, (v & 1)); else { /* newly nonzero coef */ arith_encode(cinfo, st + 1, 1); arith_encode(cinfo, entropy->fixed_bin, 0); } break; } } else { v = -v; if (v >>= cinfo->Al) { if (v >> 1) /* previously nonzero coef */ arith_encode(cinfo, st + 2, (v & 1)); else { /* newly nonzero coef */ arith_encode(cinfo, st + 1, 1); arith_encode(cinfo, entropy->fixed_bin, 1); } break; } } arith_encode(cinfo, st + 1, 0); st += 3; k++; } } /* Encode EOB decision only if k <= cinfo->Se */ if (k <= cinfo->Se) { st = entropy->ac_stats[tbl] + 3 * (k - 1); arith_encode(cinfo, st, 1); } return TRUE; } /* * Encode and output one MCU's worth of arithmetic-compressed coefficients. */ METHODDEF(boolean) encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; jpeg_component_info * compptr; JBLOCKROW block; unsigned char *st; int blkn, ci, tbl, k, ke; int v, v2, m; /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { emit_restart(cinfo, entropy->next_restart_num); entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num++; entropy->next_restart_num &= 7; } entropy->restarts_to_go--; } /* Encode the MCU data blocks */ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { block = MCU_data[blkn]; ci = cinfo->MCU_membership[blkn]; compptr = cinfo->cur_comp_info[ci]; /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ tbl = compptr->dc_tbl_no; /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; /* Figure F.4: Encode_DC_DIFF */ if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { arith_encode(cinfo, st, 0); entropy->dc_context[ci] = 0; /* zero diff category */ } else { entropy->last_dc_val[ci] = (*block)[0]; arith_encode(cinfo, st, 1); /* Figure F.6: Encoding nonzero value v */ /* Figure F.7: Encoding the sign of v */ if (v > 0) { arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ st += 2; /* Table F.4: SP = S0 + 2 */ entropy->dc_context[ci] = 4; /* small positive diff category */ } else { v = -v; arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ st += 3; /* Table F.4: SN = S0 + 3 */ entropy->dc_context[ci] = 8; /* small negative diff category */ } /* Figure F.8: Encoding the magnitude category of v */ m = 0; if (v -= 1) { arith_encode(cinfo, st, 1); m = 1; v2 = v; st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st += 1; } } arith_encode(cinfo, st, 0); /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) entropy->dc_context[ci] = 0; /* zero diff category */ else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) entropy->dc_context[ci] += 8; /* large diff category */ /* Figure F.9: Encoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) arith_encode(cinfo, st, (m & v) ? 1 : 0); } /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ tbl = compptr->ac_tbl_no; /* Establish EOB (end-of-block) index */ for (ke = DCTSIZE2 - 1; ke > 0; ke--) if ((*block)[jpeg_natural_order[ke]]) break; /* Figure F.5: Encode_AC_Coefficients */ for (k = 1; k <= ke; k++) { st = entropy->ac_stats[tbl] + 3 * (k - 1); arith_encode(cinfo, st, 0); /* EOB decision */ while ((v = (*block)[jpeg_natural_order[k]]) == 0) { arith_encode(cinfo, st + 1, 0); st += 3; k++; } arith_encode(cinfo, st + 1, 1); /* Figure F.6: Encoding nonzero value v */ /* Figure F.7: Encoding the sign of v */ if (v > 0) { arith_encode(cinfo, entropy->fixed_bin, 0); } else { v = -v; arith_encode(cinfo, entropy->fixed_bin, 1); } st += 2; /* Figure F.8: Encoding the magnitude category of v */ m = 0; if (v -= 1) { arith_encode(cinfo, st, 1); m = 1; v2 = v; if (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st = entropy->ac_stats[tbl] + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (v2 >>= 1) { arith_encode(cinfo, st, 1); m <<= 1; st += 1; } } } arith_encode(cinfo, st, 0); /* Figure F.9: Encoding the magnitude bit pattern of v */ st += 14; while (m >>= 1) arith_encode(cinfo, st, (m & v) ? 1 : 0); } /* Encode EOB decision only if k <= DCTSIZE2 - 1 */ if (k <= DCTSIZE2 - 1) { st = entropy->ac_stats[tbl] + 3 * (k - 1); arith_encode(cinfo, st, 1); } } return TRUE; } /* * Initialize for an arithmetic-compressed scan. */ METHODDEF(void) start_pass (j_compress_ptr cinfo, boolean gather_statistics) { arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; int ci, tbl; jpeg_component_info * compptr; if (gather_statistics) /* Make sure to avoid that in the master control logic! * We are fully adaptive here and need no extra * statistics gathering pass! */ ERREXIT(cinfo, JERR_NOT_COMPILED); /* We assume jcmaster.c already validated the progressive scan parameters. */ /* Select execution routines */ if (cinfo->progressive_mode) { if (cinfo->Ah == 0) { if (cinfo->Ss == 0) entropy->pub.encode_mcu = encode_mcu_DC_first; else entropy->pub.encode_mcu = encode_mcu_AC_first; } else { if (cinfo->Ss == 0) entropy->pub.encode_mcu = encode_mcu_DC_refine; else entropy->pub.encode_mcu = encode_mcu_AC_refine; } } else entropy->pub.encode_mcu = encode_mcu; /* Allocate & initialize requested statistics areas */ for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* DC needs no table for refinement scan */ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { tbl = compptr->dc_tbl_no; if (tbl < 0 || tbl >= NUM_ARITH_TBLS) ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); if (entropy->dc_stats[tbl] == NULL) entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); /* Initialize DC predictions to 0 */ entropy->last_dc_val[ci] = 0; entropy->dc_context[ci] = 0; } /* AC needs no table when not present */ if (cinfo->progressive_mode == 0 || cinfo->Se) { tbl = compptr->ac_tbl_no; if (tbl < 0 || tbl >= NUM_ARITH_TBLS) ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); if (entropy->ac_stats[tbl] == NULL) entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); #ifdef CALCULATE_SPECTRAL_CONDITIONING if (cinfo->progressive_mode) /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */ cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4); #endif } } /* Initialize arithmetic encoding variables */ entropy->c = 0; entropy->a = 0x10000L; entropy->sc = 0; entropy->zc = 0; entropy->ct = 11; entropy->buffer = -1; /* empty */ /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0; } /* * Module initialization routine for arithmetic entropy encoding. */ GLOBAL(void) jinit_arith_encoder (j_compress_ptr cinfo) { arith_entropy_ptr entropy; int i; entropy = (arith_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(arith_entropy_encoder)); cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; entropy->pub.start_pass = start_pass; entropy->pub.finish_pass = finish_pass; /* Mark tables unallocated */ for (i = 0; i < NUM_ARITH_TBLS; i++) { entropy->dc_stats[i] = NULL; entropy->ac_stats[i] = NULL; } /* Initialize index for fixed probability estimation */ entropy->fixed_bin[0] = 113; } glmark2-2012.08/./src/libjpeg-turbo/jversion.h0000664000175000017500000000147612013417376020272 0ustar alfalf00000000000000/* * jversion.h * * Copyright (C) 1991-2010, Thomas G. Lane, Guido Vollbeding. * Copyright (C) 2010, 2012, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains software version identification. */ #if JPEG_LIB_VERSION >= 80 #define JVERSION "8b 16-May-2010" #elif JPEG_LIB_VERSION >= 70 #define JVERSION "7 27-Jun-2009" #else #define JVERSION "6b 27-Mar-1998" #endif #define JCOPYRIGHT "Copyright (C) 1991-2010 Thomas G. Lane, Guido Vollbeding\n" \ "Copyright (C) 1999-2006 MIYASAKA Masaru\n" \ "Copyright (C) 2009 Pierre Ossman for Cendio AB\n" \ "Copyright (C) 2009-2012 D. R. Commander\n" \ "Copyright (C) 2009-2011 Nokia Corporation and/or its subsidiary(-ies)" glmark2-2012.08/./src/libjpeg-turbo/jdapimin.c0000664000175000017500000003053512013417376020217 0ustar alfalf00000000000000/* * jdapimin.c * * Copyright (C) 1994-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains application interface code for the decompression half * of the JPEG library. These are the "minimum" API routines that may be * needed in either the normal full-decompression case or the * transcoding-only case. * * Most of the routines intended to be called directly by an application * are in this file or in jdapistd.c. But also see jcomapi.c for routines * shared by compression and decompression, and jdtrans.c for the transcoding * case. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" /* * Initialization of a JPEG decompression object. * The error manager must already be set up (in case memory manager fails). */ GLOBAL(void) jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize) { int i; /* Guard against version mismatches between library and caller. */ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ if (version != JPEG_LIB_VERSION) ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); if (structsize != SIZEOF(struct jpeg_decompress_struct)) ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize); /* For debugging purposes, we zero the whole master structure. * But the application has already set the err pointer, and may have set * client_data, so we have to save and restore those fields. * Note: if application hasn't set client_data, tools like Purify may * complain here. */ { struct jpeg_error_mgr * err = cinfo->err; void * client_data = cinfo->client_data; /* ignore Purify complaint here */ MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct)); cinfo->err = err; cinfo->client_data = client_data; } cinfo->is_decompressor = TRUE; /* Initialize a memory manager instance for this object */ jinit_memory_mgr((j_common_ptr) cinfo); /* Zero out pointers to permanent structures. */ cinfo->progress = NULL; cinfo->src = NULL; for (i = 0; i < NUM_QUANT_TBLS; i++) cinfo->quant_tbl_ptrs[i] = NULL; for (i = 0; i < NUM_HUFF_TBLS; i++) { cinfo->dc_huff_tbl_ptrs[i] = NULL; cinfo->ac_huff_tbl_ptrs[i] = NULL; } /* Initialize marker processor so application can override methods * for COM, APPn markers before calling jpeg_read_header. */ cinfo->marker_list = NULL; jinit_marker_reader(cinfo); /* And initialize the overall input controller. */ jinit_input_controller(cinfo); /* OK, I'm ready */ cinfo->global_state = DSTATE_START; } /* * Destruction of a JPEG decompression object */ GLOBAL(void) jpeg_destroy_decompress (j_decompress_ptr cinfo) { jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ } /* * Abort processing of a JPEG decompression operation, * but don't destroy the object itself. */ GLOBAL(void) jpeg_abort_decompress (j_decompress_ptr cinfo) { jpeg_abort((j_common_ptr) cinfo); /* use common routine */ } /* * Set default decompression parameters. */ LOCAL(void) default_decompress_parms (j_decompress_ptr cinfo) { /* Guess the input colorspace, and set output colorspace accordingly. */ /* (Wish JPEG committee had provided a real way to specify this...) */ /* Note application may override our guesses. */ switch (cinfo->num_components) { case 1: cinfo->jpeg_color_space = JCS_GRAYSCALE; cinfo->out_color_space = JCS_GRAYSCALE; break; case 3: if (cinfo->saw_JFIF_marker) { cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */ } else if (cinfo->saw_Adobe_marker) { switch (cinfo->Adobe_transform) { case 0: cinfo->jpeg_color_space = JCS_RGB; break; case 1: cinfo->jpeg_color_space = JCS_YCbCr; break; default: WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ break; } } else { /* Saw no special markers, try to guess from the component IDs */ int cid0 = cinfo->comp_info[0].component_id; int cid1 = cinfo->comp_info[1].component_id; int cid2 = cinfo->comp_info[2].component_id; if (cid0 == 1 && cid1 == 2 && cid2 == 3) cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */ else if (cid0 == 82 && cid1 == 71 && cid2 == 66) cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */ else { TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2); cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ } } /* Always guess RGB is proper output colorspace. */ cinfo->out_color_space = JCS_RGB; break; case 4: if (cinfo->saw_Adobe_marker) { switch (cinfo->Adobe_transform) { case 0: cinfo->jpeg_color_space = JCS_CMYK; break; case 2: cinfo->jpeg_color_space = JCS_YCCK; break; default: WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */ break; } } else { /* No special markers, assume straight CMYK. */ cinfo->jpeg_color_space = JCS_CMYK; } cinfo->out_color_space = JCS_CMYK; break; default: cinfo->jpeg_color_space = JCS_UNKNOWN; cinfo->out_color_space = JCS_UNKNOWN; break; } /* Set defaults for other decompression parameters. */ cinfo->scale_num = 1; /* 1:1 scaling */ cinfo->scale_denom = 1; cinfo->output_gamma = 1.0; cinfo->buffered_image = FALSE; cinfo->raw_data_out = FALSE; cinfo->dct_method = JDCT_DEFAULT; cinfo->do_fancy_upsampling = TRUE; cinfo->do_block_smoothing = TRUE; cinfo->quantize_colors = FALSE; /* We set these in case application only sets quantize_colors. */ cinfo->dither_mode = JDITHER_FS; #ifdef QUANT_2PASS_SUPPORTED cinfo->two_pass_quantize = TRUE; #else cinfo->two_pass_quantize = FALSE; #endif cinfo->desired_number_of_colors = 256; cinfo->colormap = NULL; /* Initialize for no mode change in buffered-image mode. */ cinfo->enable_1pass_quant = FALSE; cinfo->enable_external_quant = FALSE; cinfo->enable_2pass_quant = FALSE; } /* * Decompression startup: read start of JPEG datastream to see what's there. * Need only initialize JPEG object and supply a data source before calling. * * This routine will read as far as the first SOS marker (ie, actual start of * compressed data), and will save all tables and parameters in the JPEG * object. It will also initialize the decompression parameters to default * values, and finally return JPEG_HEADER_OK. On return, the application may * adjust the decompression parameters and then call jpeg_start_decompress. * (Or, if the application only wanted to determine the image parameters, * the data need not be decompressed. In that case, call jpeg_abort or * jpeg_destroy to release any temporary space.) * If an abbreviated (tables only) datastream is presented, the routine will * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then * re-use the JPEG object to read the abbreviated image datastream(s). * It is unnecessary (but OK) to call jpeg_abort in this case. * The JPEG_SUSPENDED return code only occurs if the data source module * requests suspension of the decompressor. In this case the application * should load more source data and then re-call jpeg_read_header to resume * processing. * If a non-suspending data source is used and require_image is TRUE, then the * return code need not be inspected since only JPEG_HEADER_OK is possible. * * This routine is now just a front end to jpeg_consume_input, with some * extra error checking. */ GLOBAL(int) jpeg_read_header (j_decompress_ptr cinfo, boolean require_image) { int retcode; if (cinfo->global_state != DSTATE_START && cinfo->global_state != DSTATE_INHEADER) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); retcode = jpeg_consume_input(cinfo); switch (retcode) { case JPEG_REACHED_SOS: retcode = JPEG_HEADER_OK; break; case JPEG_REACHED_EOI: if (require_image) /* Complain if application wanted an image */ ERREXIT(cinfo, JERR_NO_IMAGE); /* Reset to start state; it would be safer to require the application to * call jpeg_abort, but we can't change it now for compatibility reasons. * A side effect is to free any temporary memory (there shouldn't be any). */ jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */ retcode = JPEG_HEADER_TABLES_ONLY; break; case JPEG_SUSPENDED: /* no work */ break; } return retcode; } /* * Consume data in advance of what the decompressor requires. * This can be called at any time once the decompressor object has * been created and a data source has been set up. * * This routine is essentially a state machine that handles a couple * of critical state-transition actions, namely initial setup and * transition from header scanning to ready-for-start_decompress. * All the actual input is done via the input controller's consume_input * method. */ GLOBAL(int) jpeg_consume_input (j_decompress_ptr cinfo) { int retcode = JPEG_SUSPENDED; /* NB: every possible DSTATE value should be listed in this switch */ switch (cinfo->global_state) { case DSTATE_START: /* Start-of-datastream actions: reset appropriate modules */ (*cinfo->inputctl->reset_input_controller) (cinfo); /* Initialize application's data source module */ (*cinfo->src->init_source) (cinfo); cinfo->global_state = DSTATE_INHEADER; /*FALLTHROUGH*/ case DSTATE_INHEADER: retcode = (*cinfo->inputctl->consume_input) (cinfo); if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */ /* Set up default parameters based on header data */ default_decompress_parms(cinfo); /* Set global state: ready for start_decompress */ cinfo->global_state = DSTATE_READY; } break; case DSTATE_READY: /* Can't advance past first SOS until start_decompress is called */ retcode = JPEG_REACHED_SOS; break; case DSTATE_PRELOAD: case DSTATE_PRESCAN: case DSTATE_SCANNING: case DSTATE_RAW_OK: case DSTATE_BUFIMAGE: case DSTATE_BUFPOST: case DSTATE_STOPPING: retcode = (*cinfo->inputctl->consume_input) (cinfo); break; default: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); } return retcode; } /* * Have we finished reading the input file? */ GLOBAL(boolean) jpeg_input_complete (j_decompress_ptr cinfo) { /* Check for valid jpeg object */ if (cinfo->global_state < DSTATE_START || cinfo->global_state > DSTATE_STOPPING) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); return cinfo->inputctl->eoi_reached; } /* * Is there more than one scan? */ GLOBAL(boolean) jpeg_has_multiple_scans (j_decompress_ptr cinfo) { /* Only valid after jpeg_read_header completes */ if (cinfo->global_state < DSTATE_READY || cinfo->global_state > DSTATE_STOPPING) ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); return cinfo->inputctl->has_multiple_scans; } /* * Finish JPEG decompression. * * This will normally just verify the file trailer and release temp storage. * * Returns FALSE if suspended. The return value need be inspected only if * a suspending data source is used. */ GLOBAL(boolean) jpeg_finish_decompress (j_decompress_ptr cinfo) { if ((cinfo->global_state == DSTATE_SCANNING || cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) { /* Terminate final pass of non-buffered mode */ if (cinfo->output_scanline < cinfo->output_height) ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); (*cinfo->master->finish_output_pass) (cinfo); cinfo->global_state = DSTATE_STOPPING; } else if (cinfo->global_state == DSTATE_BUFIMAGE) { /* Finishing after a buffered-image operation */ cinfo->global_state = DSTATE_STOPPING; } else if (cinfo->global_state != DSTATE_STOPPING) { /* STOPPING = repeat call after a suspension, anything else is error */ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); } /* Read until EOI */ while (! cinfo->inputctl->eoi_reached) { if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) return FALSE; /* Suspend, come back later */ } /* Do final cleanup */ (*cinfo->src->term_source) (cinfo); /* We can use jpeg_abort to release memory and reset global_state */ jpeg_abort((j_common_ptr) cinfo); return TRUE; } glmark2-2012.08/./src/libjpeg-turbo/jdcolext.c.inc0000664000175000017500000000645512013417376021014 0ustar alfalf00000000000000/* * jdcolext.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 2009, 2011, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains output colorspace conversion routines. */ /* This file is included by jdcolor.c */ /* * Convert some rows of samples to the output colorspace. * * Note that we change from noninterleaved, one-plane-per-component format * to interleaved-pixel format. The output buffer is therefore three times * as wide as the input buffer. * A starting row offset is provided only for the input buffer. The caller * can easily adjust the passed output_buf value to accommodate any row * offset required on that side. */ INLINE LOCAL(void) ycc_rgb_convert_internal (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; register int y, cb, cr; register JSAMPROW outptr; register JSAMPROW inptr0, inptr1, inptr2; register JDIMENSION col; JDIMENSION num_cols = cinfo->output_width; /* copy these pointers into registers if possible */ register JSAMPLE * range_limit = cinfo->sample_range_limit; register int * Crrtab = cconvert->Cr_r_tab; register int * Cbbtab = cconvert->Cb_b_tab; register INT32 * Crgtab = cconvert->Cr_g_tab; register INT32 * Cbgtab = cconvert->Cb_g_tab; SHIFT_TEMPS while (--num_rows >= 0) { inptr0 = input_buf[0][input_row]; inptr1 = input_buf[1][input_row]; inptr2 = input_buf[2][input_row]; input_row++; outptr = *output_buf++; for (col = 0; col < num_cols; col++) { y = GETJSAMPLE(inptr0[col]); cb = GETJSAMPLE(inptr1[col]); cr = GETJSAMPLE(inptr2[col]); /* Range-limiting is essential due to noise introduced by DCT losses. */ outptr[RGB_RED] = range_limit[y + Crrtab[cr]]; outptr[RGB_GREEN] = range_limit[y + ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS))]; outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]]; /* Set unused byte to 0xFF so it can be interpreted as an opaque */ /* alpha channel value */ #ifdef RGB_ALPHA outptr[RGB_ALPHA] = 0xFF; #endif outptr += RGB_PIXELSIZE; } } } /* * Convert grayscale to RGB: just duplicate the graylevel three times. * This is provided to support applications that don't want to cope * with grayscale as a separate case. */ INLINE LOCAL(void) gray_rgb_convert_internal (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { register JSAMPROW inptr, outptr; register JDIMENSION col; JDIMENSION num_cols = cinfo->output_width; while (--num_rows >= 0) { inptr = input_buf[0][input_row++]; outptr = *output_buf++; for (col = 0; col < num_cols; col++) { /* We can dispense with GETJSAMPLE() here */ outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col]; /* Set unused byte to 0xFF so it can be interpreted as an opaque */ /* alpha channel value */ #ifdef RGB_ALPHA outptr[RGB_ALPHA] = 0xFF; #endif outptr += RGB_PIXELSIZE; } } } glmark2-2012.08/./src/libjpeg-turbo/jmemsys.h0000664000175000017500000002005612013417376020115 0ustar alfalf00000000000000/* * jmemsys.h * * Copyright (C) 1992-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This include file defines the interface between the system-independent * and system-dependent portions of the JPEG memory manager. No other * modules need include it. (The system-independent portion is jmemmgr.c; * there are several different versions of the system-dependent portion.) * * This file works as-is for the system-dependent memory managers supplied * in the IJG distribution. You may need to modify it if you write a * custom memory manager. If system-dependent changes are needed in * this file, the best method is to #ifdef them based on a configuration * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR * and USE_MAC_MEMMGR. */ /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_get_small jGetSmall #define jpeg_free_small jFreeSmall #define jpeg_get_large jGetLarge #define jpeg_free_large jFreeLarge #define jpeg_mem_available jMemAvail #define jpeg_open_backing_store jOpenBackStore #define jpeg_mem_init jMemInit #define jpeg_mem_term jMemTerm #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* * These two functions are used to allocate and release small chunks of * memory. (Typically the total amount requested through jpeg_get_small is * no more than 20K or so; this will be requested in chunks of a few K each.) * Behavior should be the same as for the standard library functions malloc * and free; in particular, jpeg_get_small must return NULL on failure. * On most systems, these ARE malloc and free. jpeg_free_small is passed the * size of the object being freed, just in case it's needed. * On an 80x86 machine using small-data memory model, these manage near heap. */ EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject)); EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object, size_t sizeofobject)); /* * These two functions are used to allocate and release large chunks of * memory (up to the total free space designated by jpeg_mem_available). * The interface is the same as above, except that on an 80x86 machine, * far pointers are used. On most other machines these are identical to * the jpeg_get/free_small routines; but we keep them separate anyway, * in case a different allocation strategy is desirable for large chunks. */ EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo, size_t sizeofobject)); EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object, size_t sizeofobject)); /* * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may * be requested in a single call to jpeg_get_large (and jpeg_get_small for that * matter, but that case should never come into play). This macro is needed * to model the 64Kb-segment-size limit of far addressing on 80x86 machines. * On those machines, we expect that jconfig.h will provide a proper value. * On machines with 32-bit flat address spaces, any large constant may be used. * * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type * size_t and will be a multiple of sizeof(align_type). */ #ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */ #define MAX_ALLOC_CHUNK 1000000000L #endif /* * This routine computes the total space still available for allocation by * jpeg_get_large. If more space than this is needed, backing store will be * used. NOTE: any memory already allocated must not be counted. * * There is a minimum space requirement, corresponding to the minimum * feasible buffer sizes; jmemmgr.c will request that much space even if * jpeg_mem_available returns zero. The maximum space needed, enough to hold * all working storage in memory, is also passed in case it is useful. * Finally, the total space already allocated is passed. If no better * method is available, cinfo->mem->max_memory_to_use - already_allocated * is often a suitable calculation. * * It is OK for jpeg_mem_available to underestimate the space available * (that'll just lead to more backing-store access than is really necessary). * However, an overestimate will lead to failure. Hence it's wise to subtract * a slop factor from the true available space. 5% should be enough. * * On machines with lots of virtual memory, any large constant may be returned. * Conversely, zero may be returned to always use the minimum amount of memory. */ EXTERN(size_t) jpeg_mem_available JPP((j_common_ptr cinfo, size_t min_bytes_needed, size_t max_bytes_needed, size_t already_allocated)); /* * This structure holds whatever state is needed to access a single * backing-store object. The read/write/close method pointers are called * by jmemmgr.c to manipulate the backing-store object; all other fields * are private to the system-dependent backing store routines. */ #define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */ #ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */ typedef unsigned short XMSH; /* type of extended-memory handles */ typedef unsigned short EMSH; /* type of expanded-memory handles */ typedef union { short file_handle; /* DOS file handle if it's a temp file */ XMSH xms_handle; /* handle if it's a chunk of XMS */ EMSH ems_handle; /* handle if it's a chunk of EMS */ } handle_union; #endif /* USE_MSDOS_MEMMGR */ #ifdef USE_MAC_MEMMGR /* Mac-specific junk */ #include #endif /* USE_MAC_MEMMGR */ typedef struct backing_store_struct * backing_store_ptr; typedef struct backing_store_struct { /* Methods for reading/writing/closing this backing-store object */ JMETHOD(void, read_backing_store, (j_common_ptr cinfo, backing_store_ptr info, void FAR * buffer_address, long file_offset, long byte_count)); JMETHOD(void, write_backing_store, (j_common_ptr cinfo, backing_store_ptr info, void FAR * buffer_address, long file_offset, long byte_count)); JMETHOD(void, close_backing_store, (j_common_ptr cinfo, backing_store_ptr info)); /* Private fields for system-dependent backing-store management */ #ifdef USE_MSDOS_MEMMGR /* For the MS-DOS manager (jmemdos.c), we need: */ handle_union handle; /* reference to backing-store storage object */ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ #else #ifdef USE_MAC_MEMMGR /* For the Mac manager (jmemmac.c), we need: */ short temp_file; /* file reference number to temp file */ FSSpec tempSpec; /* the FSSpec for the temp file */ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ #else /* For a typical implementation with temp files, we need: */ FILE * temp_file; /* stdio reference to temp file */ char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */ #endif #endif } backing_store_info; /* * Initial opening of a backing-store object. This must fill in the * read/write/close pointers in the object. The read/write routines * may take an error exit if the specified maximum file size is exceeded. * (If jpeg_mem_available always returns a large value, this routine can * just take an error exit.) */ EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo, backing_store_ptr info, long total_bytes_needed)); /* * These routines take care of any system-dependent initialization and * cleanup required. jpeg_mem_init will be called before anything is * allocated (and, therefore, nothing in cinfo is of use except the error * manager pointer). It should return a suitable default value for * max_memory_to_use; this may subsequently be overridden by the surrounding * application. (Note that max_memory_to_use is only important if * jpeg_mem_available chooses to consult it ... no one else will.) * jpeg_mem_term may assume that all requested memory has been freed and that * all opened backing-store objects have been closed. */ EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo)); EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo)); glmark2-2012.08/./src/libjpeg-turbo/jidctfst.c0000664000175000017500000003156212013417376020237 0ustar alfalf00000000000000/* * jidctfst.c * * Copyright (C) 1994-1998, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains a fast, not so accurate integer implementation of the * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine * must also perform dequantization of the input coefficients. * * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT * on each row (or vice versa, but it's more convenient to emit a row at * a time). Direct algorithms are also available, but they are much more * complex and seem not to be any faster when reduced to code. * * This implementation is based on Arai, Agui, and Nakajima's algorithm for * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in * Japanese, but the algorithm is described in the Pennebaker & Mitchell * JPEG textbook (see REFERENCES section in file README). The following code * is based directly on figure 4-8 in P&M. * While an 8-point DCT cannot be done in less than 11 multiplies, it is * possible to arrange the computation so that many of the multiplies are * simple scalings of the final outputs. These multiplies can then be * folded into the multiplications or divisions by the JPEG quantization * table entries. The AA&N method leaves only 5 multiplies and 29 adds * to be done in the DCT itself. * The primary disadvantage of this method is that with fixed-point math, * accuracy is lost due to imprecise representation of the scaled * quantization values. The smaller the quantization table entry, the less * precise the scaled value, so this implementation does worse with high- * quality-setting files than with low-quality ones. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdct.h" /* Private declarations for DCT subsystem */ #ifdef DCT_IFAST_SUPPORTED /* * This module is specialized to the case DCTSIZE = 8. */ #if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ #endif /* Scaling decisions are generally the same as in the LL&M algorithm; * see jidctint.c for more details. However, we choose to descale * (right shift) multiplication products as soon as they are formed, * rather than carrying additional fractional bits into subsequent additions. * This compromises accuracy slightly, but it lets us save a few shifts. * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) * everywhere except in the multiplications proper; this saves a good deal * of work on 16-bit-int machines. * * The dequantized coefficients are not integers because the AA&N scaling * factors have been incorporated. We represent them scaled up by PASS1_BITS, * so that the first and second IDCT rounds have the same input scaling. * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to * avoid a descaling shift; this compromises accuracy rather drastically * for small quantization table entries, but it saves a lot of shifts. * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, * so we use a much larger scaling factor to preserve accuracy. * * A final compromise is to represent the multiplicative constants to only * 8 fractional bits, rather than 13. This saves some shifting work on some * machines, and may also reduce the cost of multiplication (since there * are fewer one-bits in the constants). */ #if BITS_IN_JSAMPLE == 8 #define CONST_BITS 8 #define PASS1_BITS 2 #else #define CONST_BITS 8 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */ #endif /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus * causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) */ #if CONST_BITS == 8 #define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */ #define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */ #define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */ #define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */ #else #define FIX_1_082392200 FIX(1.082392200) #define FIX_1_414213562 FIX(1.414213562) #define FIX_1_847759065 FIX(1.847759065) #define FIX_2_613125930 FIX(2.613125930) #endif /* We can gain a little more speed, with a further compromise in accuracy, * by omitting the addition in a descaling shift. This yields an incorrectly * rounded result half the time... */ #ifndef USE_ACCURATE_ROUNDING #undef DESCALE #define DESCALE(x,n) RIGHT_SHIFT(x, n) #endif /* Multiply a DCTELEM variable by an INT32 constant, and immediately * descale to yield a DCTELEM result. */ #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) /* Dequantize a coefficient by multiplying it by the multiplier-table * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 * multiplication will do. For 12-bit data, the multiplier table is * declared INT32, so a 32-bit multiply will be used. */ #if BITS_IN_JSAMPLE == 8 #define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval)) #else #define DEQUANTIZE(coef,quantval) \ DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS) #endif /* Like DESCALE, but applies to a DCTELEM and produces an int. * We assume that int right shift is unsigned if INT32 right shift is. */ #ifdef RIGHT_SHIFT_IS_UNSIGNED #define ISHIFT_TEMPS DCTELEM ishift_temp; #if BITS_IN_JSAMPLE == 8 #define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */ #else #define DCTELEMBITS 32 /* DCTELEM must be 32 bits */ #endif #define IRIGHT_SHIFT(x,shft) \ ((ishift_temp = (x)) < 0 ? \ (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \ (ishift_temp >> (shft))) #else #define ISHIFT_TEMPS #define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) #endif #ifdef USE_ACCURATE_ROUNDING #define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n)) #else #define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n)) #endif /* * Perform dequantization and inverse DCT on one block of coefficients. */ GLOBAL(void) jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; DCTELEM tmp10, tmp11, tmp12, tmp13; DCTELEM z5, z10, z11, z12, z13; JCOEFPTR inptr; IFAST_MULT_TYPE * quantptr; int * wsptr; JSAMPROW outptr; JSAMPLE *range_limit = IDCT_range_limit(cinfo); int ctr; int workspace[DCTSIZE2]; /* buffers data between passes */ SHIFT_TEMPS /* for DESCALE */ ISHIFT_TEMPS /* for IDESCALE */ /* Pass 1: process columns from input, store into work array. */ inptr = coef_block; quantptr = (IFAST_MULT_TYPE *) compptr->dct_table; wsptr = workspace; for (ctr = DCTSIZE; ctr > 0; ctr--) { /* Due to quantization, we will usually find that many of the input * coefficients are zero, especially the AC terms. We can exploit this * by short-circuiting the IDCT calculation for any column in which all * the AC terms are zero. In that case each output is equal to the * DC coefficient (with scale factor as needed). * With typical images and quantization tables, half or more of the * column DCT calculations can be simplified this way. */ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; wsptr[DCTSIZE*2] = dcval; wsptr[DCTSIZE*3] = dcval; wsptr[DCTSIZE*4] = dcval; wsptr[DCTSIZE*5] = dcval; wsptr[DCTSIZE*6] = dcval; wsptr[DCTSIZE*7] = dcval; inptr++; /* advance pointers to next column */ quantptr++; wsptr++; continue; } /* Even part */ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); tmp10 = tmp0 + tmp2; /* phase 3 */ tmp11 = tmp0 - tmp2; tmp13 = tmp1 + tmp3; /* phases 5-3 */ tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */ tmp0 = tmp10 + tmp13; /* phase 2 */ tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); z13 = tmp6 + tmp5; /* phase 6 */ z10 = tmp6 - tmp5; z11 = tmp4 + tmp7; z12 = tmp4 - tmp7; tmp7 = z11 + z13; /* phase 5 */ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ tmp6 = tmp12 - tmp7; /* phase 2 */ tmp5 = tmp11 - tmp6; tmp4 = tmp10 + tmp5; wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7); wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7); wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6); wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6); wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5); wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5); wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4); wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4); inptr++; /* advance pointers to next column */ quantptr++; wsptr++; } /* Pass 2: process rows from work array, store into output array. */ /* Note that we must descale the results by a factor of 8 == 2**3, */ /* and also undo the PASS1_BITS scaling. */ wsptr = workspace; for (ctr = 0; ctr < DCTSIZE; ctr++) { outptr = output_buf[ctr] + output_col; /* Rows of zeroes can be exploited in the same way as we did with columns. * However, the column calculation has created many nonzero AC terms, so * the simplification applies less often (typically 5% to 10% of the time). * On machines with very fast multiplication, it's possible that the * test takes more time than it's worth. In that case this section * may be commented out. */ #ifndef NO_ZERO_ROW_TEST if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { /* AC terms all zero */ JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3) & RANGE_MASK]; outptr[0] = dcval; outptr[1] = dcval; outptr[2] = dcval; outptr[3] = dcval; outptr[4] = dcval; outptr[5] = dcval; outptr[6] = dcval; outptr[7] = dcval; wsptr += DCTSIZE; /* advance pointer to next row */ continue; } #endif /* Even part */ tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]); tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]); tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]); tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562) - tmp13; tmp0 = tmp10 + tmp13; tmp3 = tmp10 - tmp13; tmp1 = tmp11 + tmp12; tmp2 = tmp11 - tmp12; /* Odd part */ z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3]; z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3]; z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7]; z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7]; tmp7 = z11 + z13; /* phase 5 */ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ tmp6 = tmp12 - tmp7; /* phase 2 */ tmp5 = tmp11 - tmp6; tmp4 = tmp10 + tmp5; /* Final output stage: scale down by a factor of 8 and range-limit */ outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3) & RANGE_MASK]; outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3) & RANGE_MASK]; outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3) & RANGE_MASK]; outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3) & RANGE_MASK]; outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3) & RANGE_MASK]; outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3) & RANGE_MASK]; outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3) & RANGE_MASK]; outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ } } #endif /* DCT_IFAST_SUPPORTED */ glmark2-2012.08/./src/libjpeg-turbo/jcmaster.c0000664000175000017500000005053512013417376020236 0ustar alfalf00000000000000/* * jcmaster.c * * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 2003-2010 by Guido Vollbeding. * Copyright (C) 2010, D. R. Commander. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains master control logic for the JPEG compressor. * These routines are concerned with parameter validation, initial setup, * and inter-pass control (determining the number of passes and the work * to be done in each pass). */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jpegcomp.h" /* Private state */ typedef enum { main_pass, /* input data, also do first output step */ huff_opt_pass, /* Huffman code optimization pass */ output_pass /* data output pass */ } c_pass_type; typedef struct { struct jpeg_comp_master pub; /* public fields */ c_pass_type pass_type; /* the type of the current pass */ int pass_number; /* # of passes completed */ int total_passes; /* total # of passes needed */ int scan_number; /* current index in scan_info[] */ } my_comp_master; typedef my_comp_master * my_master_ptr; /* * Support routines that do various essential calculations. */ #if JPEG_LIB_VERSION >= 70 /* * Compute JPEG image dimensions and related values. * NOTE: this is exported for possible use by application. * Hence it mustn't do anything that can't be done twice. */ GLOBAL(void) jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) /* Do computations that are needed before master selection phase */ { /* Hardwire it to "no scaling" */ cinfo->jpeg_width = cinfo->image_width; cinfo->jpeg_height = cinfo->image_height; cinfo->min_DCT_h_scaled_size = DCTSIZE; cinfo->min_DCT_v_scaled_size = DCTSIZE; } #endif LOCAL(void) initial_setup (j_compress_ptr cinfo, boolean transcode_only) /* Do computations that are needed before master selection phase */ { int ci; jpeg_component_info *compptr; long samplesperrow; JDIMENSION jd_samplesperrow; #if JPEG_LIB_VERSION >= 70 #if JPEG_LIB_VERSION >= 80 if (!transcode_only) #endif jpeg_calc_jpeg_dimensions(cinfo); #endif /* Sanity check on image dimensions */ if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || cinfo->num_components <= 0 || cinfo->input_components <= 0) ERREXIT(cinfo, JERR_EMPTY_IMAGE); /* Make sure image isn't bigger than I can handle */ if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION || (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION) ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); /* Width of an input scanline must be representable as JDIMENSION. */ samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; jd_samplesperrow = (JDIMENSION) samplesperrow; if ((long) jd_samplesperrow != samplesperrow) ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); /* For now, precision must match compiled-in value... */ if (cinfo->data_precision != BITS_IN_JSAMPLE) ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); /* Check that number of components won't exceed internal array sizes */ if (cinfo->num_components > MAX_COMPONENTS) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPONENTS); /* Compute maximum sampling factors; check factor validity */ cinfo->max_h_samp_factor = 1; cinfo->max_v_samp_factor = 1; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) ERREXIT(cinfo, JERR_BAD_SAMPLING); cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, compptr->h_samp_factor); cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, compptr->v_samp_factor); } /* Compute dimensions of components */ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { /* Fill in the correct component_index value; don't rely on application */ compptr->component_index = ci; /* For compression, we never do DCT scaling. */ #if JPEG_LIB_VERSION >= 70 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; #else compptr->DCT_scaled_size = DCTSIZE; #endif /* Size in DCT blocks */ compptr->width_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, (long) (cinfo->max_h_samp_factor * DCTSIZE)); compptr->height_in_blocks = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, (long) (cinfo->max_v_samp_factor * DCTSIZE)); /* Size in samples */ compptr->downsampled_width = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, (long) cinfo->max_h_samp_factor); compptr->downsampled_height = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, (long) cinfo->max_v_samp_factor); /* Mark component needed (this flag isn't actually used for compression) */ compptr->component_needed = TRUE; } /* Compute number of fully interleaved MCU rows (number of times that * main controller will call coefficient controller). */ cinfo->total_iMCU_rows = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); } #ifdef C_MULTISCAN_FILES_SUPPORTED LOCAL(void) validate_script (j_compress_ptr cinfo) /* Verify that the scan script in cinfo->scan_info[] is valid; also * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. */ { const jpeg_scan_info * scanptr; int scanno, ncomps, ci, coefi, thisi; int Ss, Se, Ah, Al; boolean component_sent[MAX_COMPONENTS]; #ifdef C_PROGRESSIVE_SUPPORTED int * last_bitpos_ptr; int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; /* -1 until that coefficient has been seen; then last Al for it */ #endif if (cinfo->num_scans <= 0) ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; * for progressive JPEG, no scan can have this. */ scanptr = cinfo->scan_info; if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { #ifdef C_PROGRESSIVE_SUPPORTED cinfo->progressive_mode = TRUE; last_bitpos_ptr = & last_bitpos[0][0]; for (ci = 0; ci < cinfo->num_components; ci++) for (coefi = 0; coefi < DCTSIZE2; coefi++) *last_bitpos_ptr++ = -1; #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { cinfo->progressive_mode = FALSE; for (ci = 0; ci < cinfo->num_components; ci++) component_sent[ci] = FALSE; } for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { /* Validate component indexes */ ncomps = scanptr->comps_in_scan; if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); for (ci = 0; ci < ncomps; ci++) { thisi = scanptr->component_index[ci]; if (thisi < 0 || thisi >= cinfo->num_components) ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); /* Components must appear in SOF order within each scan */ if (ci > 0 && thisi <= scanptr->component_index[ci-1]) ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); } /* Validate progression parameters */ Ss = scanptr->Ss; Se = scanptr->Se; Ah = scanptr->Ah; Al = scanptr->Al; if (cinfo->progressive_mode) { #ifdef C_PROGRESSIVE_SUPPORTED /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that * seems wrong: the upper bound ought to depend on data precision. * Perhaps they really meant 0..N+1 for N-bit precision. * Here we allow 0..10 for 8-bit data; Al larger than 10 results in * out-of-range reconstructed DC values during the first DC scan, * which might cause problems for some decoders. */ #if BITS_IN_JSAMPLE == 8 #define MAX_AH_AL 10 #else #define MAX_AH_AL 13 #endif if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); if (Ss == 0) { if (Se != 0) /* DC and AC together not OK */ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); } else { if (ncomps != 1) /* AC scans must be for only one component */ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); } for (ci = 0; ci < ncomps; ci++) { last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); for (coefi = Ss; coefi <= Se; coefi++) { if (last_bitpos_ptr[coefi] < 0) { /* first scan of this coefficient */ if (Ah != 0) ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); } else { /* not first scan */ if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); } last_bitpos_ptr[coefi] = Al; } } #endif } else { /* For sequential JPEG, all progression parameters must be these: */ if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); /* Make sure components are not sent twice */ for (ci = 0; ci < ncomps; ci++) { thisi = scanptr->component_index[ci]; if (component_sent[thisi]) ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); component_sent[thisi] = TRUE; } } } /* Now verify that everything got sent. */ if (cinfo->progressive_mode) { #ifdef C_PROGRESSIVE_SUPPORTED /* For progressive mode, we only check that at least some DC data * got sent for each component; the spec does not require that all bits * of all coefficients be transmitted. Would it be wiser to enforce * transmission of all coefficient bits?? */ for (ci = 0; ci < cinfo->num_components; ci++) { if (last_bitpos[ci][0] < 0) ERREXIT(cinfo, JERR_MISSING_DATA); } #endif } else { for (ci = 0; ci < cinfo->num_components; ci++) { if (! component_sent[ci]) ERREXIT(cinfo, JERR_MISSING_DATA); } } } #endif /* C_MULTISCAN_FILES_SUPPORTED */ LOCAL(void) select_scan_parameters (j_compress_ptr cinfo) /* Set up the scan parameters for the current scan */ { int ci; #ifdef C_MULTISCAN_FILES_SUPPORTED if (cinfo->scan_info != NULL) { /* Prepare for current scan --- the script is already validated */ my_master_ptr master = (my_master_ptr) cinfo->master; const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; cinfo->comps_in_scan = scanptr->comps_in_scan; for (ci = 0; ci < scanptr->comps_in_scan; ci++) { cinfo->cur_comp_info[ci] = &cinfo->comp_info[scanptr->component_index[ci]]; } cinfo->Ss = scanptr->Ss; cinfo->Se = scanptr->Se; cinfo->Ah = scanptr->Ah; cinfo->Al = scanptr->Al; } else #endif { /* Prepare for single sequential-JPEG scan containing all components */ if (cinfo->num_components > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPS_IN_SCAN); cinfo->comps_in_scan = cinfo->num_components; for (ci = 0; ci < cinfo->num_components; ci++) { cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; } cinfo->Ss = 0; cinfo->Se = DCTSIZE2-1; cinfo->Ah = 0; cinfo->Al = 0; } } LOCAL(void) per_scan_setup (j_compress_ptr cinfo) /* Do computations that are needed before processing a JPEG scan */ /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ { int ci, mcublks, tmp; jpeg_component_info *compptr; if (cinfo->comps_in_scan == 1) { /* Noninterleaved (single-component) scan */ compptr = cinfo->cur_comp_info[0]; /* Overall image size in MCUs */ cinfo->MCUs_per_row = compptr->width_in_blocks; cinfo->MCU_rows_in_scan = compptr->height_in_blocks; /* For noninterleaved scan, always one block per MCU */ compptr->MCU_width = 1; compptr->MCU_height = 1; compptr->MCU_blocks = 1; compptr->MCU_sample_width = DCTSIZE; compptr->last_col_width = 1; /* For noninterleaved scans, it is convenient to define last_row_height * as the number of block rows present in the last iMCU row. */ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); if (tmp == 0) tmp = compptr->v_samp_factor; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ cinfo->blocks_in_MCU = 1; cinfo->MCU_membership[0] = 0; } else { /* Interleaved (multi-component) scan */ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, MAX_COMPS_IN_SCAN); /* Overall image size in MCUs */ cinfo->MCUs_per_row = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_width, (long) (cinfo->max_h_samp_factor*DCTSIZE)); cinfo->MCU_rows_in_scan = (JDIMENSION) jdiv_round_up((long) cinfo->_jpeg_height, (long) (cinfo->max_v_samp_factor*DCTSIZE)); cinfo->blocks_in_MCU = 0; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; /* Sampling factors give # of blocks of component in each MCU */ compptr->MCU_width = compptr->h_samp_factor; compptr->MCU_height = compptr->v_samp_factor; compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; /* Figure number of non-dummy blocks in last MCU column & row */ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); if (tmp == 0) tmp = compptr->MCU_width; compptr->last_col_width = tmp; tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); if (tmp == 0) tmp = compptr->MCU_height; compptr->last_row_height = tmp; /* Prepare array describing MCU composition */ mcublks = compptr->MCU_blocks; if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) ERREXIT(cinfo, JERR_BAD_MCU_SIZE); while (mcublks-- > 0) { cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; } } } /* Convert restart specified in rows to actual MCU count. */ /* Note that count must fit in 16 bits, so we provide limiting. */ if (cinfo->restart_in_rows > 0) { long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); } } /* * Per-pass setup. * This is called at the beginning of each pass. We determine which modules * will be active during this pass and give them appropriate start_pass calls. * We also set is_last_pass to indicate whether any more passes will be * required. */ METHODDEF(void) prepare_for_pass (j_compress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; switch (master->pass_type) { case main_pass: /* Initial pass: will collect input data, and do either Huffman * optimization or data output for the first scan. */ select_scan_parameters(cinfo); per_scan_setup(cinfo); if (! cinfo->raw_data_in) { (*cinfo->cconvert->start_pass) (cinfo); (*cinfo->downsample->start_pass) (cinfo); (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); } (*cinfo->fdct->start_pass) (cinfo); (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); (*cinfo->coef->start_pass) (cinfo, (master->total_passes > 1 ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); if (cinfo->optimize_coding) { /* No immediate data output; postpone writing frame/scan headers */ master->pub.call_pass_startup = FALSE; } else { /* Will write frame/scan headers at first jpeg_write_scanlines call */ master->pub.call_pass_startup = TRUE; } break; #ifdef ENTROPY_OPT_SUPPORTED case huff_opt_pass: /* Do Huffman optimization for a scan after the first one. */ select_scan_parameters(cinfo); per_scan_setup(cinfo); if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { (*cinfo->entropy->start_pass) (cinfo, TRUE); (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); master->pub.call_pass_startup = FALSE; break; } /* Special case: Huffman DC refinement scans need no Huffman table * and therefore we can skip the optimization pass for them. */ master->pass_type = output_pass; master->pass_number++; /*FALLTHROUGH*/ #endif case output_pass: /* Do a data-output pass. */ /* We need not repeat per-scan setup if prior optimization pass did it. */ if (! cinfo->optimize_coding) { select_scan_parameters(cinfo); per_scan_setup(cinfo); } (*cinfo->entropy->start_pass) (cinfo, FALSE); (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); /* We emit frame/scan headers now */ if (master->scan_number == 0) (*cinfo->marker->write_frame_header) (cinfo); (*cinfo->marker->write_scan_header) (cinfo); master->pub.call_pass_startup = FALSE; break; default: ERREXIT(cinfo, JERR_NOT_COMPILED); } master->pub.is_last_pass = (master->pass_number == master->total_passes-1); /* Set up progress monitor's pass info if present */ if (cinfo->progress != NULL) { cinfo->progress->completed_passes = master->pass_number; cinfo->progress->total_passes = master->total_passes; } } /* * Special start-of-pass hook. * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. * In single-pass processing, we need this hook because we don't want to * write frame/scan headers during jpeg_start_compress; we want to let the * application write COM markers etc. between jpeg_start_compress and the * jpeg_write_scanlines loop. * In multi-pass processing, this routine is not used. */ METHODDEF(void) pass_startup (j_compress_ptr cinfo) { cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ (*cinfo->marker->write_frame_header) (cinfo); (*cinfo->marker->write_scan_header) (cinfo); } /* * Finish up at end of pass. */ METHODDEF(void) finish_pass_master (j_compress_ptr cinfo) { my_master_ptr master = (my_master_ptr) cinfo->master; /* The entropy coder always needs an end-of-pass call, * either to analyze statistics or to flush its output buffer. */ (*cinfo->entropy->finish_pass) (cinfo); /* Update state for next pass */ switch (master->pass_type) { case main_pass: /* next pass is either output of scan 0 (after optimization) * or output of scan 1 (if no optimization). */ master->pass_type = output_pass; if (! cinfo->optimize_coding) master->scan_number++; break; case huff_opt_pass: /* next pass is always output of current scan */ master->pass_type = output_pass; break; case output_pass: /* next pass is either optimization or output of next scan */ if (cinfo->optimize_coding) master->pass_type = huff_opt_pass; master->scan_number++; break; } master->pass_number++; } /* * Initialize master compression control. */ GLOBAL(void) jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) { my_master_ptr master; master = (my_master_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_comp_master)); cinfo->master = (struct jpeg_comp_master *) master; master->pub.prepare_for_pass = prepare_for_pass; master->pub.pass_startup = pass_startup; master->pub.finish_pass = finish_pass_master; master->pub.is_last_pass = FALSE; /* Validate parameters, determine derived values */ initial_setup(cinfo, transcode_only); if (cinfo->scan_info != NULL) { #ifdef C_MULTISCAN_FILES_SUPPORTED validate_script(cinfo); #else ERREXIT(cinfo, JERR_NOT_COMPILED); #endif } else { cinfo->progressive_mode = FALSE; cinfo->num_scans = 1; } if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */ cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ /* Initialize my private state */ if (transcode_only) { /* no main pass in transcoding */ if (cinfo->optimize_coding) master->pass_type = huff_opt_pass; else master->pass_type = output_pass; } else { /* for normal compression, first pass is always this type: */ master->pass_type = main_pass; } master->scan_number = 0; master->pass_number = 0; if (cinfo->optimize_coding) master->total_passes = cinfo->num_scans * 2; else master->total_passes = cinfo->num_scans; } glmark2-2012.08/./src/libjpeg-turbo/simd/jsimd_arm.c0000664000175000017500000003272612013417376021331 0ustar alfalf00000000000000/* * jsimd_arm.c * * Copyright 2009 Pierre Ossman for Cendio AB * Copyright 2009-2011 D. R. Commander * * Based on the x86 SIMD extension for IJG JPEG library, * Copyright (C) 1999-2006, MIYASAKA Masaru. * For conditions of distribution and use, see copyright notice in jsimdext.inc * * This file contains the interface between the "normal" portions * of the library and the SIMD implementations when running on * ARM architecture. * * Based on the stubs from 'jsimd_none.c' */ #define JPEG_INTERNALS #include "../jinclude.h" #include "../jpeglib.h" #include "../jsimd.h" #include "../jdct.h" #include "../jsimddct.h" #include "jsimd.h" #include #include #include static unsigned int simd_support = ~0; #if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) #define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024) LOCAL(int) check_feature (char *buffer, char *feature) { char *p; if (*feature == 0) return 0; if (strncmp(buffer, "Features", 8) != 0) return 0; buffer += 8; while (isspace(*buffer)) buffer++; /* Check if 'feature' is present in the buffer as a separate word */ while ((p = strstr(buffer, feature))) { if (p > buffer && !isspace(*(p - 1))) { buffer++; continue; } p += strlen(feature); if (*p != 0 && !isspace(*p)) { buffer++; continue; } return 1; } return 0; } LOCAL(int) parse_proc_cpuinfo (int bufsize) { char *buffer = (char *)malloc(bufsize); FILE *fd; simd_support = 0; if (!buffer) return 0; fd = fopen("/proc/cpuinfo", "r"); if (fd) { while (fgets(buffer, bufsize, fd)) { if (!strchr(buffer, '\n') && !feof(fd)) { /* "impossible" happened - insufficient size of the buffer! */ fclose(fd); free(buffer); return 0; } if (check_feature(buffer, "neon")) simd_support |= JSIMD_ARM_NEON; } fclose(fd); } free(buffer); return 1; } #endif /* * Check what SIMD accelerations are supported. * * FIXME: This code is racy under a multi-threaded environment. */ LOCAL(void) init_simd (void) { char *env = NULL; #if !defined(__ARM_NEON__) && defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) int bufsize = 1024; /* an initial guess for the line buffer size limit */ #endif if (simd_support != ~0U) return; simd_support = 0; #if defined(__ARM_NEON__) simd_support |= JSIMD_ARM_NEON; #elif defined(__linux__) || defined(ANDROID) || defined(__ANDROID__) /* We still have a chance to use NEON regardless of globally used * -mcpu/-mfpu options passed to gcc by performing runtime detection via * /proc/cpuinfo parsing on linux/android */ while (!parse_proc_cpuinfo(bufsize)) { bufsize *= 2; if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT) break; } #endif /* Force different settings through environment variables */ env = getenv("JSIMD_FORCE_ARM_NEON"); if ((env != NULL) && (strcmp(env, "1") == 0)) simd_support &= JSIMD_ARM_NEON; env = getenv("JSIMD_FORCE_NO_SIMD"); if ((env != NULL) && (strcmp(env, "1") == 0)) simd_support = 0; } GLOBAL(int) jsimd_can_rgb_ycc (void) { init_simd(); /* The code is optimised for these values only */ if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4)) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(int) jsimd_can_rgb_gray (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_ycc_rgb (void) { init_simd(); /* The code is optimised for these values only */ if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4)) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(void) jsimd_rgb_ycc_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { void (*neonfct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int); switch(cinfo->in_color_space) { case JCS_EXT_RGB: neonfct=jsimd_extrgb_ycc_convert_neon; break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: neonfct=jsimd_extrgbx_ycc_convert_neon; break; case JCS_EXT_BGR: neonfct=jsimd_extbgr_ycc_convert_neon; break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: neonfct=jsimd_extbgrx_ycc_convert_neon; break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: neonfct=jsimd_extxbgr_ycc_convert_neon; break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: neonfct=jsimd_extxrgb_ycc_convert_neon; break; default: neonfct=jsimd_extrgb_ycc_convert_neon; break; } if (simd_support & JSIMD_ARM_NEON) neonfct(cinfo->image_width, input_buf, output_buf, output_row, num_rows); } GLOBAL(void) jsimd_rgb_gray_convert (j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows) { } GLOBAL(void) jsimd_ycc_rgb_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows) { void (*neonfct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int); switch(cinfo->out_color_space) { case JCS_EXT_RGB: neonfct=jsimd_ycc_extrgb_convert_neon; break; case JCS_EXT_RGBX: case JCS_EXT_RGBA: neonfct=jsimd_ycc_extrgbx_convert_neon; break; case JCS_EXT_BGR: neonfct=jsimd_ycc_extbgr_convert_neon; break; case JCS_EXT_BGRX: case JCS_EXT_BGRA: neonfct=jsimd_ycc_extbgrx_convert_neon; break; case JCS_EXT_XBGR: case JCS_EXT_ABGR: neonfct=jsimd_ycc_extxbgr_convert_neon; break; case JCS_EXT_XRGB: case JCS_EXT_ARGB: neonfct=jsimd_ycc_extxrgb_convert_neon; break; default: neonfct=jsimd_ycc_extrgb_convert_neon; break; } if (simd_support & JSIMD_ARM_NEON) neonfct(cinfo->output_width, input_buf, input_row, output_buf, num_rows); } GLOBAL(int) jsimd_can_h2v2_downsample (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_h2v1_downsample (void) { init_simd(); return 0; } GLOBAL(void) jsimd_h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { } GLOBAL(void) jsimd_h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY output_data) { } GLOBAL(int) jsimd_can_h2v2_upsample (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_h2v1_upsample (void) { init_simd(); return 0; } GLOBAL(void) jsimd_h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { } GLOBAL(void) jsimd_h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { } GLOBAL(int) jsimd_can_h2v2_fancy_upsample (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_h2v1_fancy_upsample (void) { init_simd(); return 0; } GLOBAL(void) jsimd_h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { } GLOBAL(void) jsimd_h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) { } GLOBAL(int) jsimd_can_h2v2_merged_upsample (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_h2v1_merged_upsample (void) { init_simd(); return 0; } GLOBAL(void) jsimd_h2v2_merged_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { } GLOBAL(void) jsimd_h2v1_merged_upsample (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf) { } GLOBAL(int) jsimd_can_convsamp (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if (sizeof(DCTELEM) != 2) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(int) jsimd_can_convsamp_float (void) { init_simd(); return 0; } GLOBAL(void) jsimd_convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace) { if (simd_support & JSIMD_ARM_NEON) jsimd_convsamp_neon(sample_data, start_col, workspace); } GLOBAL(void) jsimd_convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace) { } GLOBAL(int) jsimd_can_fdct_islow (void) { init_simd(); return 0; } GLOBAL(int) jsimd_can_fdct_ifast (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(DCTELEM) != 2) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(int) jsimd_can_fdct_float (void) { init_simd(); return 0; } GLOBAL(void) jsimd_fdct_islow (DCTELEM * data) { } GLOBAL(void) jsimd_fdct_ifast (DCTELEM * data) { if (simd_support & JSIMD_ARM_NEON) jsimd_fdct_ifast_neon(data); } GLOBAL(void) jsimd_fdct_float (FAST_FLOAT * data) { } GLOBAL(int) jsimd_can_quantize (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(JCOEF) != 2) return 0; if (sizeof(DCTELEM) != 2) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(int) jsimd_can_quantize_float (void) { init_simd(); return 0; } GLOBAL(void) jsimd_quantize (JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace) { if (simd_support & JSIMD_ARM_NEON) jsimd_quantize_neon(coef_block, divisors, workspace); } GLOBAL(void) jsimd_quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace) { } GLOBAL(int) jsimd_can_idct_2x2 (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(JCOEF) != 2) return 0; if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if (sizeof(ISLOW_MULT_TYPE) != 2) return 0; if ((simd_support & JSIMD_ARM_NEON)) return 1; return 0; } GLOBAL(int) jsimd_can_idct_4x4 (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(JCOEF) != 2) return 0; if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if (sizeof(ISLOW_MULT_TYPE) != 2) return 0; if ((simd_support & JSIMD_ARM_NEON)) return 1; return 0; } GLOBAL(void) jsimd_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { if ((simd_support & JSIMD_ARM_NEON)) jsimd_idct_2x2_neon(compptr->dct_table, coef_block, output_buf, output_col); } GLOBAL(void) jsimd_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { if ((simd_support & JSIMD_ARM_NEON)) jsimd_idct_4x4_neon(compptr->dct_table, coef_block, output_buf, output_col); } GLOBAL(int) jsimd_can_idct_islow (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(JCOEF) != 2) return 0; if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if (sizeof(ISLOW_MULT_TYPE) != 2) return 0; if (simd_support & JSIMD_ARM_NEON) return 1; return 0; } GLOBAL(int) jsimd_can_idct_ifast (void) { init_simd(); /* The code is optimised for these values only */ if (DCTSIZE != 8) return 0; if (sizeof(JCOEF) != 2) return 0; if (BITS_IN_JSAMPLE != 8) return 0; if (sizeof(JDIMENSION) != 4) return 0; if (sizeof(IFAST_MULT_TYPE) != 2) return 0; if (IFAST_SCALE_BITS != 2) return 0; if ((simd_support & JSIMD_ARM_NEON)) return 1; return 0; } GLOBAL(int) jsimd_can_idct_float (void) { init_simd(); return 0; } GLOBAL(void) jsimd_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { if ((simd_support & JSIMD_ARM_NEON)) jsimd_idct_islow_neon(compptr->dct_table, coef_block, output_buf, output_col); } GLOBAL(void) jsimd_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { if ((simd_support & JSIMD_ARM_NEON)) jsimd_idct_ifast_neon(compptr->dct_table, coef_block, output_buf, output_col); } GLOBAL(void) jsimd_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col) { } glmark2-2012.08/./src/libjpeg-turbo/simd/jsimd.h0000664000175000017500000010034312013417376020466 0ustar alfalf00000000000000/* * simd/jsimd.h * * Copyright 2009 Pierre Ossman for Cendio AB * Copyright 2011 D. R. Commander * * Based on the x86 SIMD extension for IJG JPEG library, * Copyright (C) 1999-2006, MIYASAKA Masaru. * For conditions of distribution and use, see copyright notice in jsimdext.inc * */ /* Bitmask for supported acceleration methods */ #define JSIMD_NONE 0x00 #define JSIMD_MMX 0x01 #define JSIMD_3DNOW 0x02 #define JSIMD_SSE 0x04 #define JSIMD_SSE2 0x08 #define JSIMD_ARM_NEON 0x10 /* Short forms of external names for systems with brain-damaged linkers. */ #ifdef NEED_SHORT_EXTERNAL_NAMES #define jpeg_simd_cpu_support jSiCpuSupport #define jsimd_rgb_ycc_convert_mmx jSRGBYCCM #define jsimd_extrgb_ycc_convert_mmx jSEXTRGBYCCM #define jsimd_extrgbx_ycc_convert_mmx jSEXTRGBXYCCM #define jsimd_extbgr_ycc_convert_mmx jSEXTBGRYCCM #define jsimd_extbgrx_ycc_convert_mmx jSEXTBGRXYCCM #define jsimd_extxbgr_ycc_convert_mmx jSEXTXBGRYCCM #define jsimd_extxrgb_ycc_convert_mmx jSEXTXRGBYCCM #define jsimd_rgb_gray_convert_mmx jSRGBGRYM #define jsimd_extrgb_gray_convert_mmx jSEXTRGBGRYM #define jsimd_extrgbx_gray_convert_mmx jSEXTRGBXGRYM #define jsimd_extbgr_gray_convert_mmx jSEXTBGRGRYM #define jsimd_extbgrx_gray_convert_mmx jSEXTBGRXGRYM #define jsimd_extxbgr_gray_convert_mmx jSEXTXBGRGRYM #define jsimd_extxrgb_gray_convert_mmx jSEXTXRGBGRYM #define jsimd_ycc_rgb_convert_mmx jSYCCRGBM #define jsimd_ycc_extrgb_convert_mmx jSYCCEXTRGBM #define jsimd_ycc_extrgbx_convert_mmx jSYCCEXTRGBXM #define jsimd_ycc_extbgr_convert_mmx jSYCCEXTBGRM #define jsimd_ycc_extbgrx_convert_mmx jSYCCEXTBGRXM #define jsimd_ycc_extxbgr_convert_mmx jSYCCEXTXBGRM #define jsimd_ycc_extxrgb_convert_mmx jSYCCEXTXRGBM #define jconst_rgb_ycc_convert_sse2 jSCRGBYCCS2 #define jsimd_rgb_ycc_convert_sse2 jSRGBYCCS2 #define jsimd_extrgb_ycc_convert_sse2 jSEXTRGBYCCS2 #define jsimd_extrgbx_ycc_convert_sse2 jSEXTRGBXYCCS2 #define jsimd_extbgr_ycc_convert_sse2 jSEXTBGRYCCS2 #define jsimd_extbgrx_ycc_convert_sse2 jSEXTBGRXYCCS2 #define jsimd_extxbgr_ycc_convert_sse2 jSEXTXBGRYCCS2 #define jsimd_extxrgb_ycc_convert_sse2 jSEXTXRGBYCCS2 #define jconst_rgb_gray_convert_sse2 jSCRGBGRYS2 #define jsimd_rgb_gray_convert_sse2 jSRGBGRYS2 #define jsimd_extrgb_gray_convert_sse2 jSEXTRGBGRYS2 #define jsimd_extrgbx_gray_convert_sse2 jSEXTRGBXGRYS2 #define jsimd_extbgr_gray_convert_sse2 jSEXTBGRGRYS2 #define jsimd_extbgrx_gray_convert_sse2 jSEXTBGRXGRYS2 #define jsimd_extxbgr_gray_convert_sse2 jSEXTXBGRGRYS2 #define jsimd_extxrgb_gray_convert_sse2 jSEXTXRGBGRYS2 #define jconst_ycc_rgb_convert_sse2 jSCYCCRGBS2 #define jsimd_ycc_rgb_convert_sse2 jSYCCRGBS2 #define jsimd_ycc_extrgb_convert_sse2 jSYCCEXTRGBS2 #define jsimd_ycc_extrgbx_convert_sse2 jSYCCEXTRGBXS2 #define jsimd_ycc_extbgr_convert_sse2 jSYCCEXTBGRS2 #define jsimd_ycc_extbgrx_convert_sse2 jSYCCEXTBGRXS2 #define jsimd_ycc_extxbgr_convert_sse2 jSYCCEXTXBGRS2 #define jsimd_ycc_extxrgb_convert_sse2 jSYCCEXTXRGBS2 #define jsimd_h2v2_downsample_mmx jSDnH2V2M #define jsimd_h2v1_downsample_mmx jSDnH2V1M #define jsimd_h2v2_downsample_sse2 jSDnH2V2S2 #define jsimd_h2v1_downsample_sse2 jSDnH2V1S2 #define jsimd_h2v2_upsample_mmx jSUpH2V2M #define jsimd_h2v1_upsample_mmx jSUpH2V1M #define jsimd_h2v2_fancy_upsample_mmx jSFUpH2V2M #define jsimd_h2v1_fancy_upsample_mmx jSFUpH2V1M #define jsimd_h2v2_merged_upsample_mmx jSMUpH2V2M #define jsimd_h2v2_extrgb_merged_upsample_mmx jSMUpH2V2EXTRGBM #define jsimd_h2v2_extrgbx_merged_upsample_mmx jSMUpH2V2EXTRGBXM #define jsimd_h2v2_extbgr_merged_upsample_mmx jSMUpH2V2EXTBGRM #define jsimd_h2v2_extbgrx_merged_upsample_mmx jSMUpH2V2EXTBGRXM #define jsimd_h2v2_extxbgr_merged_upsample_mmx jSMUpH2V2EXTXBGRM #define jsimd_h2v2_extxrgb_merged_upsample_mmx jSMUpH2V2EXTXRGBM #define jsimd_h2v1_merged_upsample_mmx jSMUpH2V1M #define jsimd_h2v1_extrgb_merged_upsample_mmx jSMUpH2V1EXTRGBM #define jsimd_h2v1_extrgbx_merged_upsample_mmx jSMUpH2V1EXTRGBXM #define jsimd_h2v1_extbgr_merged_upsample_mmx jSMUpH2V1EXTBGRM #define jsimd_h2v1_extbgrx_merged_upsample_mmx jSMUpH2V1EXTBGRXM #define jsimd_h2v1_extxbgr_merged_upsample_mmx jSMUpH2V1EXTXBGRM #define jsimd_h2v1_extxrgb_merged_upsample_mmx jSMUpH2V1EXTXRGBM #define jsimd_h2v2_upsample_sse2 jSUpH2V2S2 #define jsimd_h2v1_upsample_sse2 jSUpH2V1S2 #define jconst_fancy_upsample_sse2 jSCFUpS2 #define jsimd_h2v2_fancy_upsample_sse2 jSFUpH2V2S2 #define jsimd_h2v1_fancy_upsample_sse2 jSFUpH2V1S2 #define jconst_merged_upsample_sse2 jSCMUpS2 #define jsimd_h2v2_merged_upsample_sse2 jSMUpH2V2S2 #define jsimd_h2v2_extrgb_merged_upsample_sse2 jSMUpH2V2EXTRGBS2 #define jsimd_h2v2_extrgbx_merged_upsample_sse2 jSMUpH2V2EXTRGBXS2 #define jsimd_h2v2_extbgr_merged_upsample_sse2 jSMUpH2V2EXTBGRS2 #define jsimd_h2v2_extbgrx_merged_upsample_sse2 jSMUpH2V2EXTBGRXS2 #define jsimd_h2v2_extxbgr_merged_upsample_sse2 jSMUpH2V2EXTXBGRS2 #define jsimd_h2v2_extxrgb_merged_upsample_sse2 jSMUpH2V2EXTXRGBS2 #define jsimd_h2v1_merged_upsample_sse2 jSMUpH2V1S2 #define jsimd_h2v1_extrgb_merged_upsample_sse2 jSMUpH2V1EXTRGBS2 #define jsimd_h2v1_extrgbx_merged_upsample_sse2 jSMUpH2V1EXTRGBXS2 #define jsimd_h2v1_extbgr_merged_upsample_sse2 jSMUpH2V1EXTBGRS2 #define jsimd_h2v1_extbgrx_merged_upsample_sse2 jSMUpH2V1EXTBGRXS2 #define jsimd_h2v1_extxbgr_merged_upsample_sse2 jSMUpH2V1EXTXBGRS2 #define jsimd_h2v1_extxrgb_merged_upsample_sse2 jSMUpH2V1EXTXRGBS2 #define jsimd_convsamp_mmx jSConvM #define jsimd_convsamp_sse2 jSConvS2 #define jsimd_convsamp_float_3dnow jSConvF3D #define jsimd_convsamp_float_sse jSConvFS #define jsimd_convsamp_float_sse2 jSConvFS2 #define jsimd_fdct_islow_mmx jSFDMIS #define jsimd_fdct_ifast_mmx jSFDMIF #define jconst_fdct_islow_sse2 jSCFDS2IS #define jsimd_fdct_islow_sse2 jSFDS2IS #define jconst_fdct_ifast_sse2 jSCFDS2IF #define jsimd_fdct_ifast_sse2 jSFDS2IF #define jsimd_fdct_float_3dnow jSFD3DF #define jconst_fdct_float_sse jSCFDSF #define jsimd_fdct_float_sse jSFDSF #define jsimd_quantize_mmx jSQuantM #define jsimd_quantize_sse2 jSQuantS2 #define jsimd_quantize_float_3dnow jSQuantF3D #define jsimd_quantize_float_sse jSQuantFS #define jsimd_quantize_float_sse2 jSQuantFS2 #define jsimd_idct_2x2_mmx jSIDM22 #define jsimd_idct_4x4_mmx jSIDM44 #define jconst_idct_red_sse2 jSCIDS2R #define jsimd_idct_2x2_sse2 jSIDS222 #define jsimd_idct_4x4_sse2 jSIDS244 #define jsimd_idct_islow_mmx jSIDMIS #define jsimd_idct_ifast_mmx jSIDMIF #define jconst_idct_islow_sse2 jSCIDS2IS #define jsimd_idct_islow_sse2 jSIDS2IS #define jconst_idct_ifast_sse2 jSCIDS2IF #define jsimd_idct_ifast_sse2 jSIDS2IF #define jsimd_idct_float_3dnow jSID3DF #define jconst_fdct_float_sse jSCIDSF #define jsimd_idct_float_sse jSIDSF #define jconst_fdct_float_sse2 jSCIDS2F #define jsimd_idct_float_sse2 jSIDS2F #endif /* NEED_SHORT_EXTERNAL_NAMES */ /* SIMD Ext: retrieve SIMD/CPU information */ EXTERN(unsigned int) jpeg_simd_cpu_support JPP((void)); /* SIMD Color Space Conversion */ EXTERN(void) jsimd_rgb_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgb_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgbx_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgr_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgrx_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxbgr_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxrgb_ycc_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_rgb_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgb_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgbx_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgr_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgrx_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxbgr_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxrgb_gray_convert_mmx JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_ycc_rgb_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgb_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgbx_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgr_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgrx_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxbgr_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxrgb_convert_mmx JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); extern const int jconst_rgb_ycc_convert_sse2[]; EXTERN(void) jsimd_rgb_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgb_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgbx_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgr_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgrx_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxbgr_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxrgb_ycc_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); extern const int jconst_rgb_gray_convert_sse2[]; EXTERN(void) jsimd_rgb_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgb_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgbx_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgr_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgrx_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxbgr_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxrgb_gray_convert_sse2 JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); extern const int jconst_ycc_rgb_convert_sse2[]; EXTERN(void) jsimd_ycc_rgb_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgb_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgbx_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgr_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgrx_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxbgr_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxrgb_convert_sse2 JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_rgb_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgb_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extrgbx_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgr_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extbgrx_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxbgr_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_extxrgb_ycc_convert_neon JPP((JDIMENSION img_width, JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)); EXTERN(void) jsimd_ycc_rgb_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgb_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extrgbx_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgr_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extbgrx_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxbgr_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); EXTERN(void) jsimd_ycc_extxrgb_convert_neon JPP((JDIMENSION out_width, JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)); /* SIMD Downsample */ EXTERN(void) jsimd_h2v2_downsample_mmx JPP((JDIMENSION image_width, int max_v_samp_factor, JDIMENSION v_samp_factor, JDIMENSION width_blocks, JSAMPARRAY input_data, JSAMPARRAY output_data)); EXTERN(void) jsimd_h2v1_downsample_mmx JPP((JDIMENSION image_width, int max_v_samp_factor, JDIMENSION v_samp_factor, JDIMENSION width_blocks, JSAMPARRAY input_data, JSAMPARRAY output_data)); EXTERN(void) jsimd_h2v2_downsample_sse2 JPP((JDIMENSION image_width, int max_v_samp_factor, JDIMENSION v_samp_factor, JDIMENSION width_blocks, JSAMPARRAY input_data, JSAMPARRAY output_data)); EXTERN(void) jsimd_h2v1_downsample_sse2 JPP((JDIMENSION image_width, int max_v_samp_factor, JDIMENSION v_samp_factor, JDIMENSION width_blocks, JSAMPARRAY input_data, JSAMPARRAY output_data)); /* SIMD Upsample */ EXTERN(void) jsimd_h2v2_upsample_mmx JPP((int max_v_samp_factor, JDIMENSION output_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_upsample_mmx JPP((int max_v_samp_factor, JDIMENSION output_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v2_fancy_upsample_mmx JPP((int max_v_samp_factor, JDIMENSION downsampled_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_fancy_upsample_mmx JPP((int max_v_samp_factor, JDIMENSION downsampled_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v2_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extrgb_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extrgbx_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extbgr_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extbgrx_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extxbgr_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extxrgb_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extrgb_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extrgbx_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extbgr_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extbgrx_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extxbgr_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extxrgb_merged_upsample_mmx JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_upsample_sse2 JPP((int max_v_samp_factor, JDIMENSION output_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_upsample_sse2 JPP((int max_v_samp_factor, JDIMENSION output_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); extern const int jconst_fancy_upsample_sse2[]; EXTERN(void) jsimd_h2v2_fancy_upsample_sse2 JPP((int max_v_samp_factor, JDIMENSION downsampled_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); EXTERN(void) jsimd_h2v1_fancy_upsample_sse2 JPP((int max_v_samp_factor, JDIMENSION downsampled_width, JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); extern const int jconst_merged_upsample_sse2[]; EXTERN(void) jsimd_h2v2_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extrgb_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extrgbx_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extbgr_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extbgrx_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extxbgr_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v2_extxrgb_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extrgb_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extrgbx_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extbgr_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extbgrx_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extxbgr_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); EXTERN(void) jsimd_h2v1_extxrgb_merged_upsample_sse2 JPP((JDIMENSION output_width, JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)); /* SIMD Sample Conversion */ EXTERN(void) jsimd_convsamp_mmx JPP((JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)); EXTERN(void) jsimd_convsamp_sse2 JPP((JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)); EXTERN(void) jsimd_convsamp_neon JPP((JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM * workspace)); EXTERN(void) jsimd_convsamp_float_3dnow JPP((JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)); EXTERN(void) jsimd_convsamp_float_sse JPP((JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)); EXTERN(void) jsimd_convsamp_float_sse2 JPP((JSAMPARRAY sample_data, JDIMENSION start_col, FAST_FLOAT * workspace)); /* SIMD Forward DCT */ EXTERN(void) jsimd_fdct_islow_mmx JPP((DCTELEM * data)); EXTERN(void) jsimd_fdct_ifast_mmx JPP((DCTELEM * data)); extern const int jconst_fdct_ifast_sse2[]; EXTERN(void) jsimd_fdct_islow_sse2 JPP((DCTELEM * data)); extern const int jconst_fdct_islow_sse2[]; EXTERN(void) jsimd_fdct_ifast_sse2 JPP((DCTELEM * data)); EXTERN(void) jsimd_fdct_ifast_neon JPP((DCTELEM * data)); EXTERN(void) jsimd_fdct_float_3dnow JPP((FAST_FLOAT * data)); extern const int jconst_fdct_float_sse[]; EXTERN(void) jsimd_fdct_float_sse JPP((FAST_FLOAT * data)); /* SIMD Quantization */ EXTERN(void) jsimd_quantize_mmx JPP((JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)); EXTERN(void) jsimd_quantize_sse2 JPP((JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)); EXTERN(void) jsimd_quantize_neon JPP((JCOEFPTR coef_block, DCTELEM * divisors, DCTELEM * workspace)); EXTERN(void) jsimd_quantize_float_3dnow JPP((JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)); EXTERN(void) jsimd_quantize_float_sse JPP((JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)); EXTERN(void) jsimd_quantize_float_sse2 JPP((JCOEFPTR coef_block, FAST_FLOAT * divisors, FAST_FLOAT * workspace)); /* SIMD Reduced Inverse DCT */ EXTERN(void) jsimd_idct_2x2_mmx JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_4x4_mmx JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); extern const int jconst_idct_red_sse2[]; EXTERN(void) jsimd_idct_2x2_sse2 JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_4x4_sse2 JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_2x2_neon JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_4x4_neon JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); /* SIMD Inverse DCT */ EXTERN(void) jsimd_idct_islow_mmx JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_ifast_mmx JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); extern const int jconst_idct_islow_sse2[]; EXTERN(void) jsimd_idct_islow_sse2 JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); extern const int jconst_idct_ifast_sse2[]; EXTERN(void) jsimd_idct_ifast_sse2 JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_islow_neon JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_ifast_neon JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); EXTERN(void) jsimd_idct_float_3dnow JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); extern const int jconst_idct_float_sse[]; EXTERN(void) jsimd_idct_float_sse JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); extern const int jconst_idct_float_sse2[]; EXTERN(void) jsimd_idct_float_sse2 JPP((void * dct_table, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); glmark2-2012.08/./src/libjpeg-turbo/simd/jsimd_arm_neon.S0000664000175000017500000023037412013417376022327 0ustar alfalf00000000000000/* * ARM NEON optimizations for libjpeg-turbo * * Copyright (C) 2009-2011 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * Author: Siarhei Siamashka * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ #if defined(__linux__) && defined(__ELF__) .section .note.GNU-stack,"",%progbits /* mark stack as non-executable */ #endif .text .fpu neon .arch armv7a .object_arch armv4 .arm #define RESPECT_STRICT_ALIGNMENT 1 /*****************************************************************************/ /* Supplementary macro for setting function attributes */ .macro asm_function fname #ifdef __APPLE__ .func _\fname .globl _\fname _\fname: #else .func \fname .global \fname #ifdef __ELF__ .hidden \fname .type \fname, %function #endif \fname: #endif .endm /* Transpose a block of 4x4 coefficients in four 64-bit registers */ .macro transpose_4x4 x0, x1, x2, x3 vtrn.16 \x0, \x1 vtrn.16 \x2, \x3 vtrn.32 \x0, \x2 vtrn.32 \x1, \x3 .endm #define CENTERJSAMPLE 128 /*****************************************************************************/ /* * Perform dequantization and inverse DCT on one block of coefficients. * * GLOBAL(void) * jsimd_idct_islow_neon (void * dct_table, JCOEFPTR coef_block, * JSAMPARRAY output_buf, JDIMENSION output_col) */ #define FIX_0_298631336 (2446) #define FIX_0_390180644 (3196) #define FIX_0_541196100 (4433) #define FIX_0_765366865 (6270) #define FIX_0_899976223 (7373) #define FIX_1_175875602 (9633) #define FIX_1_501321110 (12299) #define FIX_1_847759065 (15137) #define FIX_1_961570560 (16069) #define FIX_2_053119869 (16819) #define FIX_2_562915447 (20995) #define FIX_3_072711026 (25172) #define FIX_1_175875602_MINUS_1_961570560 (FIX_1_175875602 - FIX_1_961570560) #define FIX_1_175875602_MINUS_0_390180644 (FIX_1_175875602 - FIX_0_390180644) #define FIX_0_541196100_MINUS_1_847759065 (FIX_0_541196100 - FIX_1_847759065) #define FIX_3_072711026_MINUS_2_562915447 (FIX_3_072711026 - FIX_2_562915447) #define FIX_0_298631336_MINUS_0_899976223 (FIX_0_298631336 - FIX_0_899976223) #define FIX_1_501321110_MINUS_0_899976223 (FIX_1_501321110 - FIX_0_899976223) #define FIX_2_053119869_MINUS_2_562915447 (FIX_2_053119869 - FIX_2_562915447) #define FIX_0_541196100_PLUS_0_765366865 (FIX_0_541196100 + FIX_0_765366865) /* * Reference SIMD-friendly 1-D ISLOW iDCT C implementation. * Uses some ideas from the comments in 'simd/jiss2int-64.asm' */ #define REF_1D_IDCT(xrow0, xrow1, xrow2, xrow3, xrow4, xrow5, xrow6, xrow7) \ { \ DCTELEM row0, row1, row2, row3, row4, row5, row6, row7; \ INT32 q1, q2, q3, q4, q5, q6, q7; \ INT32 tmp11_plus_tmp2, tmp11_minus_tmp2; \ \ /* 1-D iDCT input data */ \ row0 = xrow0; \ row1 = xrow1; \ row2 = xrow2; \ row3 = xrow3; \ row4 = xrow4; \ row5 = xrow5; \ row6 = xrow6; \ row7 = xrow7; \ \ q5 = row7 + row3; \ q4 = row5 + row1; \ q6 = MULTIPLY(q5, FIX_1_175875602_MINUS_1_961570560) + \ MULTIPLY(q4, FIX_1_175875602); \ q7 = MULTIPLY(q5, FIX_1_175875602) + \ MULTIPLY(q4, FIX_1_175875602_MINUS_0_390180644); \ q2 = MULTIPLY(row2, FIX_0_541196100) + \ MULTIPLY(row6, FIX_0_541196100_MINUS_1_847759065); \ q4 = q6; \ q3 = ((INT32) row0 - (INT32) row4) << 13; \ q6 += MULTIPLY(row5, -FIX_2_562915447) + \ MULTIPLY(row3, FIX_3_072711026_MINUS_2_562915447); \ /* now we can use q1 (reloadable constants have been used up) */ \ q1 = q3 + q2; \ q4 += MULTIPLY(row7, FIX_0_298631336_MINUS_0_899976223) + \ MULTIPLY(row1, -FIX_0_899976223); \ q5 = q7; \ q1 = q1 + q6; \ q7 += MULTIPLY(row7, -FIX_0_899976223) + \ MULTIPLY(row1, FIX_1_501321110_MINUS_0_899976223); \ \ /* (tmp11 + tmp2) has been calculated (out_row1 before descale) */ \ tmp11_plus_tmp2 = q1; \ row1 = 0; \ \ q1 = q1 - q6; \ q5 += MULTIPLY(row5, FIX_2_053119869_MINUS_2_562915447) + \ MULTIPLY(row3, -FIX_2_562915447); \ q1 = q1 - q6; \ q6 = MULTIPLY(row2, FIX_0_541196100_PLUS_0_765366865) + \ MULTIPLY(row6, FIX_0_541196100); \ q3 = q3 - q2; \ \ /* (tmp11 - tmp2) has been calculated (out_row6 before descale) */ \ tmp11_minus_tmp2 = q1; \ \ q1 = ((INT32) row0 + (INT32) row4) << 13; \ q2 = q1 + q6; \ q1 = q1 - q6; \ \ /* pick up the results */ \ tmp0 = q4; \ tmp1 = q5; \ tmp2 = (tmp11_plus_tmp2 - tmp11_minus_tmp2) / 2; \ tmp3 = q7; \ tmp10 = q2; \ tmp11 = (tmp11_plus_tmp2 + tmp11_minus_tmp2) / 2; \ tmp12 = q3; \ tmp13 = q1; \ } #define XFIX_0_899976223 d0[0] #define XFIX_0_541196100 d0[1] #define XFIX_2_562915447 d0[2] #define XFIX_0_298631336_MINUS_0_899976223 d0[3] #define XFIX_1_501321110_MINUS_0_899976223 d1[0] #define XFIX_2_053119869_MINUS_2_562915447 d1[1] #define XFIX_0_541196100_PLUS_0_765366865 d1[2] #define XFIX_1_175875602 d1[3] #define XFIX_1_175875602_MINUS_0_390180644 d2[0] #define XFIX_0_541196100_MINUS_1_847759065 d2[1] #define XFIX_3_072711026_MINUS_2_562915447 d2[2] #define XFIX_1_175875602_MINUS_1_961570560 d2[3] .balign 16 jsimd_idct_islow_neon_consts: .short FIX_0_899976223 /* d0[0] */ .short FIX_0_541196100 /* d0[1] */ .short FIX_2_562915447 /* d0[2] */ .short FIX_0_298631336_MINUS_0_899976223 /* d0[3] */ .short FIX_1_501321110_MINUS_0_899976223 /* d1[0] */ .short FIX_2_053119869_MINUS_2_562915447 /* d1[1] */ .short FIX_0_541196100_PLUS_0_765366865 /* d1[2] */ .short FIX_1_175875602 /* d1[3] */ /* reloadable constants */ .short FIX_1_175875602_MINUS_0_390180644 /* d2[0] */ .short FIX_0_541196100_MINUS_1_847759065 /* d2[1] */ .short FIX_3_072711026_MINUS_2_562915447 /* d2[2] */ .short FIX_1_175875602_MINUS_1_961570560 /* d2[3] */ asm_function jsimd_idct_islow_neon DCT_TABLE .req r0 COEF_BLOCK .req r1 OUTPUT_BUF .req r2 OUTPUT_COL .req r3 TMP1 .req r0 TMP2 .req r1 TMP3 .req r2 TMP4 .req ip ROW0L .req d16 ROW0R .req d17 ROW1L .req d18 ROW1R .req d19 ROW2L .req d20 ROW2R .req d21 ROW3L .req d22 ROW3R .req d23 ROW4L .req d24 ROW4R .req d25 ROW5L .req d26 ROW5R .req d27 ROW6L .req d28 ROW6R .req d29 ROW7L .req d30 ROW7R .req d31 /* Load and dequantize coefficients into NEON registers * with the following allocation: * 0 1 2 3 | 4 5 6 7 * ---------+-------- * 0 | d16 | d17 ( q8 ) * 1 | d18 | d19 ( q9 ) * 2 | d20 | d21 ( q10 ) * 3 | d22 | d23 ( q11 ) * 4 | d24 | d25 ( q12 ) * 5 | d26 | d27 ( q13 ) * 6 | d28 | d29 ( q14 ) * 7 | d30 | d31 ( q15 ) */ adr ip, jsimd_idct_islow_neon_consts vld1.16 {d16, d17, d18, d19}, [COEF_BLOCK, :128]! vld1.16 {d0, d1, d2, d3}, [DCT_TABLE, :128]! vld1.16 {d20, d21, d22, d23}, [COEF_BLOCK, :128]! vmul.s16 q8, q8, q0 vld1.16 {d4, d5, d6, d7}, [DCT_TABLE, :128]! vmul.s16 q9, q9, q1 vld1.16 {d24, d25, d26, d27}, [COEF_BLOCK, :128]! vmul.s16 q10, q10, q2 vld1.16 {d0, d1, d2, d3}, [DCT_TABLE, :128]! vmul.s16 q11, q11, q3 vld1.16 {d28, d29, d30, d31}, [COEF_BLOCK, :128] vmul.s16 q12, q12, q0 vld1.16 {d4, d5, d6, d7}, [DCT_TABLE, :128]! vmul.s16 q14, q14, q2 vmul.s16 q13, q13, q1 vld1.16 {d0, d1, d2, d3}, [ip, :128] /* load constants */ add ip, ip, #16 vmul.s16 q15, q15, q3 vpush {d8-d15} /* save NEON registers */ /* 1-D IDCT, pass 1, left 4x8 half */ vadd.s16 d4, ROW7L, ROW3L vadd.s16 d5, ROW5L, ROW1L vmull.s16 q6, d4, XFIX_1_175875602_MINUS_1_961570560 vmlal.s16 q6, d5, XFIX_1_175875602 vmull.s16 q7, d4, XFIX_1_175875602 /* Check for the zero coefficients in the right 4x8 half */ push {r4, r5} vmlal.s16 q7, d5, XFIX_1_175875602_MINUS_0_390180644 vsubl.s16 q3, ROW0L, ROW4L ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 1 * 8))] vmull.s16 q2, ROW2L, XFIX_0_541196100 vmlal.s16 q2, ROW6L, XFIX_0_541196100_MINUS_1_847759065 orr r0, r4, r5 vmov q4, q6 vmlsl.s16 q6, ROW5L, XFIX_2_562915447 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 2 * 8))] vmlal.s16 q6, ROW3L, XFIX_3_072711026_MINUS_2_562915447 vshl.s32 q3, q3, #13 orr r0, r0, r4 vmlsl.s16 q4, ROW1L, XFIX_0_899976223 orr r0, r0, r5 vadd.s32 q1, q3, q2 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 3 * 8))] vmov q5, q7 vadd.s32 q1, q1, q6 orr r0, r0, r4 vmlsl.s16 q7, ROW7L, XFIX_0_899976223 orr r0, r0, r5 vmlal.s16 q7, ROW1L, XFIX_1_501321110_MINUS_0_899976223 vrshrn.s32 ROW1L, q1, #11 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 4 * 8))] vsub.s32 q1, q1, q6 vmlal.s16 q5, ROW5L, XFIX_2_053119869_MINUS_2_562915447 orr r0, r0, r4 vmlsl.s16 q5, ROW3L, XFIX_2_562915447 orr r0, r0, r5 vsub.s32 q1, q1, q6 vmull.s16 q6, ROW2L, XFIX_0_541196100_PLUS_0_765366865 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 5 * 8))] vmlal.s16 q6, ROW6L, XFIX_0_541196100 vsub.s32 q3, q3, q2 orr r0, r0, r4 vrshrn.s32 ROW6L, q1, #11 orr r0, r0, r5 vadd.s32 q1, q3, q5 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 6 * 8))] vsub.s32 q3, q3, q5 vaddl.s16 q5, ROW0L, ROW4L orr r0, r0, r4 vrshrn.s32 ROW2L, q1, #11 orr r0, r0, r5 vrshrn.s32 ROW5L, q3, #11 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 7 * 8))] vshl.s32 q5, q5, #13 vmlal.s16 q4, ROW7L, XFIX_0_298631336_MINUS_0_899976223 orr r0, r0, r4 vadd.s32 q2, q5, q6 orrs r0, r0, r5 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 ldrd r4, [COEF_BLOCK, #(-96 + 2 * (4 + 0 * 8))] vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 orr r0, r4, r5 vsub.s32 q3, q1, q4 pop {r4, r5} vrshrn.s32 ROW7L, q2, #11 vrshrn.s32 ROW3L, q5, #11 vrshrn.s32 ROW0L, q6, #11 vrshrn.s32 ROW4L, q3, #11 beq 3f /* Go to do some special handling for the sparse right 4x8 half */ /* 1-D IDCT, pass 1, right 4x8 half */ vld1.s16 {d2}, [ip, :64] /* reload constants */ vadd.s16 d10, ROW7R, ROW3R vadd.s16 d8, ROW5R, ROW1R /* Transpose left 4x8 half */ vtrn.16 ROW6L, ROW7L vmull.s16 q6, d10, XFIX_1_175875602_MINUS_1_961570560 vmlal.s16 q6, d8, XFIX_1_175875602 vtrn.16 ROW2L, ROW3L vmull.s16 q7, d10, XFIX_1_175875602 vmlal.s16 q7, d8, XFIX_1_175875602_MINUS_0_390180644 vtrn.16 ROW0L, ROW1L vsubl.s16 q3, ROW0R, ROW4R vmull.s16 q2, ROW2R, XFIX_0_541196100 vmlal.s16 q2, ROW6R, XFIX_0_541196100_MINUS_1_847759065 vtrn.16 ROW4L, ROW5L vmov q4, q6 vmlsl.s16 q6, ROW5R, XFIX_2_562915447 vmlal.s16 q6, ROW3R, XFIX_3_072711026_MINUS_2_562915447 vtrn.32 ROW1L, ROW3L vshl.s32 q3, q3, #13 vmlsl.s16 q4, ROW1R, XFIX_0_899976223 vtrn.32 ROW4L, ROW6L vadd.s32 q1, q3, q2 vmov q5, q7 vadd.s32 q1, q1, q6 vtrn.32 ROW0L, ROW2L vmlsl.s16 q7, ROW7R, XFIX_0_899976223 vmlal.s16 q7, ROW1R, XFIX_1_501321110_MINUS_0_899976223 vrshrn.s32 ROW1R, q1, #11 vtrn.32 ROW5L, ROW7L vsub.s32 q1, q1, q6 vmlal.s16 q5, ROW5R, XFIX_2_053119869_MINUS_2_562915447 vmlsl.s16 q5, ROW3R, XFIX_2_562915447 vsub.s32 q1, q1, q6 vmull.s16 q6, ROW2R, XFIX_0_541196100_PLUS_0_765366865 vmlal.s16 q6, ROW6R, XFIX_0_541196100 vsub.s32 q3, q3, q2 vrshrn.s32 ROW6R, q1, #11 vadd.s32 q1, q3, q5 vsub.s32 q3, q3, q5 vaddl.s16 q5, ROW0R, ROW4R vrshrn.s32 ROW2R, q1, #11 vrshrn.s32 ROW5R, q3, #11 vshl.s32 q5, q5, #13 vmlal.s16 q4, ROW7R, XFIX_0_298631336_MINUS_0_899976223 vadd.s32 q2, q5, q6 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 vsub.s32 q3, q1, q4 vrshrn.s32 ROW7R, q2, #11 vrshrn.s32 ROW3R, q5, #11 vrshrn.s32 ROW0R, q6, #11 vrshrn.s32 ROW4R, q3, #11 /* Transpose right 4x8 half */ vtrn.16 ROW6R, ROW7R vtrn.16 ROW2R, ROW3R vtrn.16 ROW0R, ROW1R vtrn.16 ROW4R, ROW5R vtrn.32 ROW1R, ROW3R vtrn.32 ROW4R, ROW6R vtrn.32 ROW0R, ROW2R vtrn.32 ROW5R, ROW7R 1: /* 1-D IDCT, pass 2 (normal variant), left 4x8 half */ vld1.s16 {d2}, [ip, :64] /* reload constants */ vmull.s16 q6, ROW1R, XFIX_1_175875602 /* ROW5L <-> ROW1R */ vmlal.s16 q6, ROW1L, XFIX_1_175875602 vmlal.s16 q6, ROW3R, XFIX_1_175875602_MINUS_1_961570560 /* ROW7L <-> ROW3R */ vmlal.s16 q6, ROW3L, XFIX_1_175875602_MINUS_1_961570560 vmull.s16 q7, ROW3R, XFIX_1_175875602 /* ROW7L <-> ROW3R */ vmlal.s16 q7, ROW3L, XFIX_1_175875602 vmlal.s16 q7, ROW1R, XFIX_1_175875602_MINUS_0_390180644 /* ROW5L <-> ROW1R */ vmlal.s16 q7, ROW1L, XFIX_1_175875602_MINUS_0_390180644 vsubl.s16 q3, ROW0L, ROW0R /* ROW4L <-> ROW0R */ vmull.s16 q2, ROW2L, XFIX_0_541196100 vmlal.s16 q2, ROW2R, XFIX_0_541196100_MINUS_1_847759065 /* ROW6L <-> ROW2R */ vmov q4, q6 vmlsl.s16 q6, ROW1R, XFIX_2_562915447 /* ROW5L <-> ROW1R */ vmlal.s16 q6, ROW3L, XFIX_3_072711026_MINUS_2_562915447 vshl.s32 q3, q3, #13 vmlsl.s16 q4, ROW1L, XFIX_0_899976223 vadd.s32 q1, q3, q2 vmov q5, q7 vadd.s32 q1, q1, q6 vmlsl.s16 q7, ROW3R, XFIX_0_899976223 /* ROW7L <-> ROW3R */ vmlal.s16 q7, ROW1L, XFIX_1_501321110_MINUS_0_899976223 vshrn.s32 ROW1L, q1, #16 vsub.s32 q1, q1, q6 vmlal.s16 q5, ROW1R, XFIX_2_053119869_MINUS_2_562915447 /* ROW5L <-> ROW1R */ vmlsl.s16 q5, ROW3L, XFIX_2_562915447 vsub.s32 q1, q1, q6 vmull.s16 q6, ROW2L, XFIX_0_541196100_PLUS_0_765366865 vmlal.s16 q6, ROW2R, XFIX_0_541196100 /* ROW6L <-> ROW2R */ vsub.s32 q3, q3, q2 vshrn.s32 ROW2R, q1, #16 /* ROW6L <-> ROW2R */ vadd.s32 q1, q3, q5 vsub.s32 q3, q3, q5 vaddl.s16 q5, ROW0L, ROW0R /* ROW4L <-> ROW0R */ vshrn.s32 ROW2L, q1, #16 vshrn.s32 ROW1R, q3, #16 /* ROW5L <-> ROW1R */ vshl.s32 q5, q5, #13 vmlal.s16 q4, ROW3R, XFIX_0_298631336_MINUS_0_899976223 /* ROW7L <-> ROW3R */ vadd.s32 q2, q5, q6 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 vsub.s32 q3, q1, q4 vshrn.s32 ROW3R, q2, #16 /* ROW7L <-> ROW3R */ vshrn.s32 ROW3L, q5, #16 vshrn.s32 ROW0L, q6, #16 vshrn.s32 ROW0R, q3, #16 /* ROW4L <-> ROW0R */ /* 1-D IDCT, pass 2, right 4x8 half */ vld1.s16 {d2}, [ip, :64] /* reload constants */ vmull.s16 q6, ROW5R, XFIX_1_175875602 vmlal.s16 q6, ROW5L, XFIX_1_175875602 /* ROW5L <-> ROW1R */ vmlal.s16 q6, ROW7R, XFIX_1_175875602_MINUS_1_961570560 vmlal.s16 q6, ROW7L, XFIX_1_175875602_MINUS_1_961570560 /* ROW7L <-> ROW3R */ vmull.s16 q7, ROW7R, XFIX_1_175875602 vmlal.s16 q7, ROW7L, XFIX_1_175875602 /* ROW7L <-> ROW3R */ vmlal.s16 q7, ROW5R, XFIX_1_175875602_MINUS_0_390180644 vmlal.s16 q7, ROW5L, XFIX_1_175875602_MINUS_0_390180644 /* ROW5L <-> ROW1R */ vsubl.s16 q3, ROW4L, ROW4R /* ROW4L <-> ROW0R */ vmull.s16 q2, ROW6L, XFIX_0_541196100 /* ROW6L <-> ROW2R */ vmlal.s16 q2, ROW6R, XFIX_0_541196100_MINUS_1_847759065 vmov q4, q6 vmlsl.s16 q6, ROW5R, XFIX_2_562915447 vmlal.s16 q6, ROW7L, XFIX_3_072711026_MINUS_2_562915447 /* ROW7L <-> ROW3R */ vshl.s32 q3, q3, #13 vmlsl.s16 q4, ROW5L, XFIX_0_899976223 /* ROW5L <-> ROW1R */ vadd.s32 q1, q3, q2 vmov q5, q7 vadd.s32 q1, q1, q6 vmlsl.s16 q7, ROW7R, XFIX_0_899976223 vmlal.s16 q7, ROW5L, XFIX_1_501321110_MINUS_0_899976223 /* ROW5L <-> ROW1R */ vshrn.s32 ROW5L, q1, #16 /* ROW5L <-> ROW1R */ vsub.s32 q1, q1, q6 vmlal.s16 q5, ROW5R, XFIX_2_053119869_MINUS_2_562915447 vmlsl.s16 q5, ROW7L, XFIX_2_562915447 /* ROW7L <-> ROW3R */ vsub.s32 q1, q1, q6 vmull.s16 q6, ROW6L, XFIX_0_541196100_PLUS_0_765366865 /* ROW6L <-> ROW2R */ vmlal.s16 q6, ROW6R, XFIX_0_541196100 vsub.s32 q3, q3, q2 vshrn.s32 ROW6R, q1, #16 vadd.s32 q1, q3, q5 vsub.s32 q3, q3, q5 vaddl.s16 q5, ROW4L, ROW4R /* ROW4L <-> ROW0R */ vshrn.s32 ROW6L, q1, #16 /* ROW6L <-> ROW2R */ vshrn.s32 ROW5R, q3, #16 vshl.s32 q5, q5, #13 vmlal.s16 q4, ROW7R, XFIX_0_298631336_MINUS_0_899976223 vadd.s32 q2, q5, q6 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 vsub.s32 q3, q1, q4 vshrn.s32 ROW7R, q2, #16 vshrn.s32 ROW7L, q5, #16 /* ROW7L <-> ROW3R */ vshrn.s32 ROW4L, q6, #16 /* ROW4L <-> ROW0R */ vshrn.s32 ROW4R, q3, #16 2: /* Descale to 8-bit and range limit */ vqrshrn.s16 d16, q8, #2 vqrshrn.s16 d17, q9, #2 vqrshrn.s16 d18, q10, #2 vqrshrn.s16 d19, q11, #2 vpop {d8-d15} /* restore NEON registers */ vqrshrn.s16 d20, q12, #2 /* Transpose the final 8-bit samples and do signed->unsigned conversion */ vtrn.16 q8, q9 vqrshrn.s16 d21, q13, #2 vqrshrn.s16 d22, q14, #2 vmov.u8 q0, #(CENTERJSAMPLE) vqrshrn.s16 d23, q15, #2 vtrn.8 d16, d17 vtrn.8 d18, d19 vadd.u8 q8, q8, q0 vadd.u8 q9, q9, q0 vtrn.16 q10, q11 /* Store results to the output buffer */ ldmia OUTPUT_BUF!, {TMP1, TMP2} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL vst1.8 {d16}, [TMP1] vtrn.8 d20, d21 vst1.8 {d17}, [TMP2] ldmia OUTPUT_BUF!, {TMP1, TMP2} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL vst1.8 {d18}, [TMP1] vadd.u8 q10, q10, q0 vst1.8 {d19}, [TMP2] ldmia OUTPUT_BUF, {TMP1, TMP2, TMP3, TMP4} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL add TMP3, TMP3, OUTPUT_COL add TMP4, TMP4, OUTPUT_COL vtrn.8 d22, d23 vst1.8 {d20}, [TMP1] vadd.u8 q11, q11, q0 vst1.8 {d21}, [TMP2] vst1.8 {d22}, [TMP3] vst1.8 {d23}, [TMP4] bx lr 3: /* Left 4x8 half is done, right 4x8 half contains mostly zeros */ /* Transpose left 4x8 half */ vtrn.16 ROW6L, ROW7L vtrn.16 ROW2L, ROW3L vtrn.16 ROW0L, ROW1L vtrn.16 ROW4L, ROW5L vshl.s16 ROW0R, ROW0R, #2 /* PASS1_BITS */ vtrn.32 ROW1L, ROW3L vtrn.32 ROW4L, ROW6L vtrn.32 ROW0L, ROW2L vtrn.32 ROW5L, ROW7L cmp r0, #0 beq 4f /* Right 4x8 half has all zeros, go to 'sparse' second pass */ /* Only row 0 is non-zero for the right 4x8 half */ vdup.s16 ROW1R, ROW0R[1] vdup.s16 ROW2R, ROW0R[2] vdup.s16 ROW3R, ROW0R[3] vdup.s16 ROW4R, ROW0R[0] vdup.s16 ROW5R, ROW0R[1] vdup.s16 ROW6R, ROW0R[2] vdup.s16 ROW7R, ROW0R[3] vdup.s16 ROW0R, ROW0R[0] b 1b /* Go to 'normal' second pass */ 4: /* 1-D IDCT, pass 2 (sparse variant with zero rows 4-7), left 4x8 half */ vld1.s16 {d2}, [ip, :64] /* reload constants */ vmull.s16 q6, ROW1L, XFIX_1_175875602 vmlal.s16 q6, ROW3L, XFIX_1_175875602_MINUS_1_961570560 vmull.s16 q7, ROW3L, XFIX_1_175875602 vmlal.s16 q7, ROW1L, XFIX_1_175875602_MINUS_0_390180644 vmull.s16 q2, ROW2L, XFIX_0_541196100 vshll.s16 q3, ROW0L, #13 vmov q4, q6 vmlal.s16 q6, ROW3L, XFIX_3_072711026_MINUS_2_562915447 vmlsl.s16 q4, ROW1L, XFIX_0_899976223 vadd.s32 q1, q3, q2 vmov q5, q7 vmlal.s16 q7, ROW1L, XFIX_1_501321110_MINUS_0_899976223 vadd.s32 q1, q1, q6 vadd.s32 q6, q6, q6 vmlsl.s16 q5, ROW3L, XFIX_2_562915447 vshrn.s32 ROW1L, q1, #16 vsub.s32 q1, q1, q6 vmull.s16 q6, ROW2L, XFIX_0_541196100_PLUS_0_765366865 vsub.s32 q3, q3, q2 vshrn.s32 ROW2R, q1, #16 /* ROW6L <-> ROW2R */ vadd.s32 q1, q3, q5 vsub.s32 q3, q3, q5 vshll.s16 q5, ROW0L, #13 vshrn.s32 ROW2L, q1, #16 vshrn.s32 ROW1R, q3, #16 /* ROW5L <-> ROW1R */ vadd.s32 q2, q5, q6 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 vsub.s32 q3, q1, q4 vshrn.s32 ROW3R, q2, #16 /* ROW7L <-> ROW3R */ vshrn.s32 ROW3L, q5, #16 vshrn.s32 ROW0L, q6, #16 vshrn.s32 ROW0R, q3, #16 /* ROW4L <-> ROW0R */ /* 1-D IDCT, pass 2 (sparse variant with zero rows 4-7), right 4x8 half */ vld1.s16 {d2}, [ip, :64] /* reload constants */ vmull.s16 q6, ROW5L, XFIX_1_175875602 vmlal.s16 q6, ROW7L, XFIX_1_175875602_MINUS_1_961570560 vmull.s16 q7, ROW7L, XFIX_1_175875602 vmlal.s16 q7, ROW5L, XFIX_1_175875602_MINUS_0_390180644 vmull.s16 q2, ROW6L, XFIX_0_541196100 vshll.s16 q3, ROW4L, #13 vmov q4, q6 vmlal.s16 q6, ROW7L, XFIX_3_072711026_MINUS_2_562915447 vmlsl.s16 q4, ROW5L, XFIX_0_899976223 vadd.s32 q1, q3, q2 vmov q5, q7 vmlal.s16 q7, ROW5L, XFIX_1_501321110_MINUS_0_899976223 vadd.s32 q1, q1, q6 vadd.s32 q6, q6, q6 vmlsl.s16 q5, ROW7L, XFIX_2_562915447 vshrn.s32 ROW5L, q1, #16 /* ROW5L <-> ROW1R */ vsub.s32 q1, q1, q6 vmull.s16 q6, ROW6L, XFIX_0_541196100_PLUS_0_765366865 vsub.s32 q3, q3, q2 vshrn.s32 ROW6R, q1, #16 vadd.s32 q1, q3, q5 vsub.s32 q3, q3, q5 vshll.s16 q5, ROW4L, #13 vshrn.s32 ROW6L, q1, #16 /* ROW6L <-> ROW2R */ vshrn.s32 ROW5R, q3, #16 vadd.s32 q2, q5, q6 vsub.s32 q1, q5, q6 vadd.s32 q6, q2, q7 vsub.s32 q2, q2, q7 vadd.s32 q5, q1, q4 vsub.s32 q3, q1, q4 vshrn.s32 ROW7R, q2, #16 vshrn.s32 ROW7L, q5, #16 /* ROW7L <-> ROW3R */ vshrn.s32 ROW4L, q6, #16 /* ROW4L <-> ROW0R */ vshrn.s32 ROW4R, q3, #16 b 2b /* Go to epilogue */ .unreq DCT_TABLE .unreq COEF_BLOCK .unreq OUTPUT_BUF .unreq OUTPUT_COL .unreq TMP1 .unreq TMP2 .unreq TMP3 .unreq TMP4 .unreq ROW0L .unreq ROW0R .unreq ROW1L .unreq ROW1R .unreq ROW2L .unreq ROW2R .unreq ROW3L .unreq ROW3R .unreq ROW4L .unreq ROW4R .unreq ROW5L .unreq ROW5R .unreq ROW6L .unreq ROW6R .unreq ROW7L .unreq ROW7R .endfunc /*****************************************************************************/ /* * jsimd_idct_ifast_neon * * This function contains a fast, not so accurate integer implementation of * the inverse DCT (Discrete Cosine Transform). It uses the same calculations * and produces exactly the same output as IJG's original 'jpeg_idct_ifast' * function from jidctfst.c * * Normally 1-D AAN DCT needs 5 multiplications and 29 additions. * But in ARM NEON case some extra additions are required because VQDMULH * instruction can't handle the constants larger than 1. So the expressions * like "x * 1.082392200" have to be converted to "x * 0.082392200 + x", * which introduces an extra addition. Overall, there are 6 extra additions * per 1-D IDCT pass, totalling to 5 VQDMULH and 35 VADD/VSUB instructions. */ #define XFIX_1_082392200 d0[0] #define XFIX_1_414213562 d0[1] #define XFIX_1_847759065 d0[2] #define XFIX_2_613125930 d0[3] .balign 16 jsimd_idct_ifast_neon_consts: .short (277 * 128 - 256 * 128) /* XFIX_1_082392200 */ .short (362 * 128 - 256 * 128) /* XFIX_1_414213562 */ .short (473 * 128 - 256 * 128) /* XFIX_1_847759065 */ .short (669 * 128 - 512 * 128) /* XFIX_2_613125930 */ asm_function jsimd_idct_ifast_neon DCT_TABLE .req r0 COEF_BLOCK .req r1 OUTPUT_BUF .req r2 OUTPUT_COL .req r3 TMP1 .req r0 TMP2 .req r1 TMP3 .req r2 TMP4 .req ip /* Load and dequantize coefficients into NEON registers * with the following allocation: * 0 1 2 3 | 4 5 6 7 * ---------+-------- * 0 | d16 | d17 ( q8 ) * 1 | d18 | d19 ( q9 ) * 2 | d20 | d21 ( q10 ) * 3 | d22 | d23 ( q11 ) * 4 | d24 | d25 ( q12 ) * 5 | d26 | d27 ( q13 ) * 6 | d28 | d29 ( q14 ) * 7 | d30 | d31 ( q15 ) */ adr ip, jsimd_idct_ifast_neon_consts vld1.16 {d16, d17, d18, d19}, [COEF_BLOCK, :128]! vld1.16 {d0, d1, d2, d3}, [DCT_TABLE, :128]! vld1.16 {d20, d21, d22, d23}, [COEF_BLOCK, :128]! vmul.s16 q8, q8, q0 vld1.16 {d4, d5, d6, d7}, [DCT_TABLE, :128]! vmul.s16 q9, q9, q1 vld1.16 {d24, d25, d26, d27}, [COEF_BLOCK, :128]! vmul.s16 q10, q10, q2 vld1.16 {d0, d1, d2, d3}, [DCT_TABLE, :128]! vmul.s16 q11, q11, q3 vld1.16 {d28, d29, d30, d31}, [COEF_BLOCK, :128] vmul.s16 q12, q12, q0 vld1.16 {d4, d5, d6, d7}, [DCT_TABLE, :128]! vmul.s16 q14, q14, q2 vmul.s16 q13, q13, q1 vld1.16 {d0}, [ip, :64] /* load constants */ vmul.s16 q15, q15, q3 vpush {d8-d13} /* save NEON registers */ /* 1-D IDCT, pass 1 */ vsub.s16 q2, q10, q14 vadd.s16 q14, q10, q14 vsub.s16 q1, q11, q13 vadd.s16 q13, q11, q13 vsub.s16 q5, q9, q15 vadd.s16 q15, q9, q15 vqdmulh.s16 q4, q2, XFIX_1_414213562 vqdmulh.s16 q6, q1, XFIX_2_613125930 vadd.s16 q3, q1, q1 vsub.s16 q1, q5, q1 vadd.s16 q10, q2, q4 vqdmulh.s16 q4, q1, XFIX_1_847759065 vsub.s16 q2, q15, q13 vadd.s16 q3, q3, q6 vqdmulh.s16 q6, q2, XFIX_1_414213562 vadd.s16 q1, q1, q4 vqdmulh.s16 q4, q5, XFIX_1_082392200 vsub.s16 q10, q10, q14 vadd.s16 q2, q2, q6 vsub.s16 q6, q8, q12 vadd.s16 q12, q8, q12 vadd.s16 q9, q5, q4 vadd.s16 q5, q6, q10 vsub.s16 q10, q6, q10 vadd.s16 q6, q15, q13 vadd.s16 q8, q12, q14 vsub.s16 q3, q6, q3 vsub.s16 q12, q12, q14 vsub.s16 q3, q3, q1 vsub.s16 q1, q9, q1 vadd.s16 q2, q3, q2 vsub.s16 q15, q8, q6 vadd.s16 q1, q1, q2 vadd.s16 q8, q8, q6 vadd.s16 q14, q5, q3 vsub.s16 q9, q5, q3 vsub.s16 q13, q10, q2 vadd.s16 q10, q10, q2 /* Transpose */ vtrn.16 q8, q9 vsub.s16 q11, q12, q1 vtrn.16 q14, q15 vadd.s16 q12, q12, q1 vtrn.16 q10, q11 vtrn.16 q12, q13 vtrn.32 q9, q11 vtrn.32 q12, q14 vtrn.32 q8, q10 vtrn.32 q13, q15 vswp d28, d21 vswp d26, d19 /* 1-D IDCT, pass 2 */ vsub.s16 q2, q10, q14 vswp d30, d23 vadd.s16 q14, q10, q14 vswp d24, d17 vsub.s16 q1, q11, q13 vadd.s16 q13, q11, q13 vsub.s16 q5, q9, q15 vadd.s16 q15, q9, q15 vqdmulh.s16 q4, q2, XFIX_1_414213562 vqdmulh.s16 q6, q1, XFIX_2_613125930 vadd.s16 q3, q1, q1 vsub.s16 q1, q5, q1 vadd.s16 q10, q2, q4 vqdmulh.s16 q4, q1, XFIX_1_847759065 vsub.s16 q2, q15, q13 vadd.s16 q3, q3, q6 vqdmulh.s16 q6, q2, XFIX_1_414213562 vadd.s16 q1, q1, q4 vqdmulh.s16 q4, q5, XFIX_1_082392200 vsub.s16 q10, q10, q14 vadd.s16 q2, q2, q6 vsub.s16 q6, q8, q12 vadd.s16 q12, q8, q12 vadd.s16 q9, q5, q4 vadd.s16 q5, q6, q10 vsub.s16 q10, q6, q10 vadd.s16 q6, q15, q13 vadd.s16 q8, q12, q14 vsub.s16 q3, q6, q3 vsub.s16 q12, q12, q14 vsub.s16 q3, q3, q1 vsub.s16 q1, q9, q1 vadd.s16 q2, q3, q2 vsub.s16 q15, q8, q6 vadd.s16 q1, q1, q2 vadd.s16 q8, q8, q6 vadd.s16 q14, q5, q3 vsub.s16 q9, q5, q3 vsub.s16 q13, q10, q2 vpop {d8-d13} /* restore NEON registers */ vadd.s16 q10, q10, q2 vsub.s16 q11, q12, q1 vadd.s16 q12, q12, q1 /* Descale to 8-bit and range limit */ vmov.u8 q0, #0x80 vqshrn.s16 d16, q8, #5 vqshrn.s16 d17, q9, #5 vqshrn.s16 d18, q10, #5 vqshrn.s16 d19, q11, #5 vqshrn.s16 d20, q12, #5 vqshrn.s16 d21, q13, #5 vqshrn.s16 d22, q14, #5 vqshrn.s16 d23, q15, #5 vadd.u8 q8, q8, q0 vadd.u8 q9, q9, q0 vadd.u8 q10, q10, q0 vadd.u8 q11, q11, q0 /* Transpose the final 8-bit samples */ vtrn.16 q8, q9 vtrn.16 q10, q11 vtrn.32 q8, q10 vtrn.32 q9, q11 vtrn.8 d16, d17 vtrn.8 d18, d19 /* Store results to the output buffer */ ldmia OUTPUT_BUF!, {TMP1, TMP2} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL vst1.8 {d16}, [TMP1] vst1.8 {d17}, [TMP2] ldmia OUTPUT_BUF!, {TMP1, TMP2} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL vst1.8 {d18}, [TMP1] vtrn.8 d20, d21 vst1.8 {d19}, [TMP2] ldmia OUTPUT_BUF, {TMP1, TMP2, TMP3, TMP4} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL add TMP3, TMP3, OUTPUT_COL add TMP4, TMP4, OUTPUT_COL vst1.8 {d20}, [TMP1] vtrn.8 d22, d23 vst1.8 {d21}, [TMP2] vst1.8 {d22}, [TMP3] vst1.8 {d23}, [TMP4] bx lr .unreq DCT_TABLE .unreq COEF_BLOCK .unreq OUTPUT_BUF .unreq OUTPUT_COL .unreq TMP1 .unreq TMP2 .unreq TMP3 .unreq TMP4 .endfunc /*****************************************************************************/ /* * jsimd_idct_4x4_neon * * This function contains inverse-DCT code for getting reduced-size * 4x4 pixels output from an 8x8 DCT block. It uses the same calculations * and produces exactly the same output as IJG's original 'jpeg_idct_4x4' * function from jpeg-6b (jidctred.c). * * NOTE: jpeg-8 has an improved implementation of 4x4 inverse-DCT, which * requires much less arithmetic operations and hence should be faster. * The primary purpose of this particular NEON optimized function is * bit exact compatibility with jpeg-6b. * * TODO: a bit better instructions scheduling can be achieved by expanding * idct_helper/transpose_4x4 macros and reordering instructions, * but readability will suffer somewhat. */ #define CONST_BITS 13 #define FIX_0_211164243 (1730) /* FIX(0.211164243) */ #define FIX_0_509795579 (4176) /* FIX(0.509795579) */ #define FIX_0_601344887 (4926) /* FIX(0.601344887) */ #define FIX_0_720959822 (5906) /* FIX(0.720959822) */ #define FIX_0_765366865 (6270) /* FIX(0.765366865) */ #define FIX_0_850430095 (6967) /* FIX(0.850430095) */ #define FIX_0_899976223 (7373) /* FIX(0.899976223) */ #define FIX_1_061594337 (8697) /* FIX(1.061594337) */ #define FIX_1_272758580 (10426) /* FIX(1.272758580) */ #define FIX_1_451774981 (11893) /* FIX(1.451774981) */ #define FIX_1_847759065 (15137) /* FIX(1.847759065) */ #define FIX_2_172734803 (17799) /* FIX(2.172734803) */ #define FIX_2_562915447 (20995) /* FIX(2.562915447) */ #define FIX_3_624509785 (29692) /* FIX(3.624509785) */ .balign 16 jsimd_idct_4x4_neon_consts: .short FIX_1_847759065 /* d0[0] */ .short -FIX_0_765366865 /* d0[1] */ .short -FIX_0_211164243 /* d0[2] */ .short FIX_1_451774981 /* d0[3] */ .short -FIX_2_172734803 /* d1[0] */ .short FIX_1_061594337 /* d1[1] */ .short -FIX_0_509795579 /* d1[2] */ .short -FIX_0_601344887 /* d1[3] */ .short FIX_0_899976223 /* d2[0] */ .short FIX_2_562915447 /* d2[1] */ .short 1 << (CONST_BITS+1) /* d2[2] */ .short 0 /* d2[3] */ .macro idct_helper x4, x6, x8, x10, x12, x14, x16, shift, y26, y27, y28, y29 vmull.s16 q14, \x4, d2[2] vmlal.s16 q14, \x8, d0[0] vmlal.s16 q14, \x14, d0[1] vmull.s16 q13, \x16, d1[2] vmlal.s16 q13, \x12, d1[3] vmlal.s16 q13, \x10, d2[0] vmlal.s16 q13, \x6, d2[1] vmull.s16 q15, \x4, d2[2] vmlsl.s16 q15, \x8, d0[0] vmlsl.s16 q15, \x14, d0[1] vmull.s16 q12, \x16, d0[2] vmlal.s16 q12, \x12, d0[3] vmlal.s16 q12, \x10, d1[0] vmlal.s16 q12, \x6, d1[1] vadd.s32 q10, q14, q13 vsub.s32 q14, q14, q13 .if \shift > 16 vrshr.s32 q10, q10, #\shift vrshr.s32 q14, q14, #\shift vmovn.s32 \y26, q10 vmovn.s32 \y29, q14 .else vrshrn.s32 \y26, q10, #\shift vrshrn.s32 \y29, q14, #\shift .endif vadd.s32 q10, q15, q12 vsub.s32 q15, q15, q12 .if \shift > 16 vrshr.s32 q10, q10, #\shift vrshr.s32 q15, q15, #\shift vmovn.s32 \y27, q10 vmovn.s32 \y28, q15 .else vrshrn.s32 \y27, q10, #\shift vrshrn.s32 \y28, q15, #\shift .endif .endm asm_function jsimd_idct_4x4_neon DCT_TABLE .req r0 COEF_BLOCK .req r1 OUTPUT_BUF .req r2 OUTPUT_COL .req r3 TMP1 .req r0 TMP2 .req r1 TMP3 .req r2 TMP4 .req ip vpush {d8-d15} /* Load constants (d3 is just used for padding) */ adr TMP4, jsimd_idct_4x4_neon_consts vld1.16 {d0, d1, d2, d3}, [TMP4, :128] /* Load all COEF_BLOCK into NEON registers with the following allocation: * 0 1 2 3 | 4 5 6 7 * ---------+-------- * 0 | d4 | d5 * 1 | d6 | d7 * 2 | d8 | d9 * 3 | d10 | d11 * 4 | - | - * 5 | d12 | d13 * 6 | d14 | d15 * 7 | d16 | d17 */ vld1.16 {d4, d5, d6, d7}, [COEF_BLOCK, :128]! vld1.16 {d8, d9, d10, d11}, [COEF_BLOCK, :128]! add COEF_BLOCK, COEF_BLOCK, #16 vld1.16 {d12, d13, d14, d15}, [COEF_BLOCK, :128]! vld1.16 {d16, d17}, [COEF_BLOCK, :128]! /* dequantize */ vld1.16 {d18, d19, d20, d21}, [DCT_TABLE, :128]! vmul.s16 q2, q2, q9 vld1.16 {d22, d23, d24, d25}, [DCT_TABLE, :128]! vmul.s16 q3, q3, q10 vmul.s16 q4, q4, q11 add DCT_TABLE, DCT_TABLE, #16 vld1.16 {d26, d27, d28, d29}, [DCT_TABLE, :128]! vmul.s16 q5, q5, q12 vmul.s16 q6, q6, q13 vld1.16 {d30, d31}, [DCT_TABLE, :128]! vmul.s16 q7, q7, q14 vmul.s16 q8, q8, q15 /* Pass 1 */ idct_helper d4, d6, d8, d10, d12, d14, d16, 12, d4, d6, d8, d10 transpose_4x4 d4, d6, d8, d10 idct_helper d5, d7, d9, d11, d13, d15, d17, 12, d5, d7, d9, d11 transpose_4x4 d5, d7, d9, d11 /* Pass 2 */ idct_helper d4, d6, d8, d10, d7, d9, d11, 19, d26, d27, d28, d29 transpose_4x4 d26, d27, d28, d29 /* Range limit */ vmov.u16 q15, #0x80 vadd.s16 q13, q13, q15 vadd.s16 q14, q14, q15 vqmovun.s16 d26, q13 vqmovun.s16 d27, q14 /* Store results to the output buffer */ ldmia OUTPUT_BUF, {TMP1, TMP2, TMP3, TMP4} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL add TMP3, TMP3, OUTPUT_COL add TMP4, TMP4, OUTPUT_COL #if defined(__ARMEL__) && !RESPECT_STRICT_ALIGNMENT /* We can use much less instructions on little endian systems if the * OS kernel is not configured to trap unaligned memory accesses */ vst1.32 {d26[0]}, [TMP1]! vst1.32 {d27[0]}, [TMP3]! vst1.32 {d26[1]}, [TMP2]! vst1.32 {d27[1]}, [TMP4]! #else vst1.8 {d26[0]}, [TMP1]! vst1.8 {d27[0]}, [TMP3]! vst1.8 {d26[1]}, [TMP1]! vst1.8 {d27[1]}, [TMP3]! vst1.8 {d26[2]}, [TMP1]! vst1.8 {d27[2]}, [TMP3]! vst1.8 {d26[3]}, [TMP1]! vst1.8 {d27[3]}, [TMP3]! vst1.8 {d26[4]}, [TMP2]! vst1.8 {d27[4]}, [TMP4]! vst1.8 {d26[5]}, [TMP2]! vst1.8 {d27[5]}, [TMP4]! vst1.8 {d26[6]}, [TMP2]! vst1.8 {d27[6]}, [TMP4]! vst1.8 {d26[7]}, [TMP2]! vst1.8 {d27[7]}, [TMP4]! #endif vpop {d8-d15} bx lr .unreq DCT_TABLE .unreq COEF_BLOCK .unreq OUTPUT_BUF .unreq OUTPUT_COL .unreq TMP1 .unreq TMP2 .unreq TMP3 .unreq TMP4 .endfunc .purgem idct_helper /*****************************************************************************/ /* * jsimd_idct_2x2_neon * * This function contains inverse-DCT code for getting reduced-size * 2x2 pixels output from an 8x8 DCT block. It uses the same calculations * and produces exactly the same output as IJG's original 'jpeg_idct_2x2' * function from jpeg-6b (jidctred.c). * * NOTE: jpeg-8 has an improved implementation of 2x2 inverse-DCT, which * requires much less arithmetic operations and hence should be faster. * The primary purpose of this particular NEON optimized function is * bit exact compatibility with jpeg-6b. */ .balign 8 jsimd_idct_2x2_neon_consts: .short -FIX_0_720959822 /* d0[0] */ .short FIX_0_850430095 /* d0[1] */ .short -FIX_1_272758580 /* d0[2] */ .short FIX_3_624509785 /* d0[3] */ .macro idct_helper x4, x6, x10, x12, x16, shift, y26, y27 vshll.s16 q14, \x4, #15 vmull.s16 q13, \x6, d0[3] vmlal.s16 q13, \x10, d0[2] vmlal.s16 q13, \x12, d0[1] vmlal.s16 q13, \x16, d0[0] vadd.s32 q10, q14, q13 vsub.s32 q14, q14, q13 .if \shift > 16 vrshr.s32 q10, q10, #\shift vrshr.s32 q14, q14, #\shift vmovn.s32 \y26, q10 vmovn.s32 \y27, q14 .else vrshrn.s32 \y26, q10, #\shift vrshrn.s32 \y27, q14, #\shift .endif .endm asm_function jsimd_idct_2x2_neon DCT_TABLE .req r0 COEF_BLOCK .req r1 OUTPUT_BUF .req r2 OUTPUT_COL .req r3 TMP1 .req r0 TMP2 .req ip vpush {d8-d15} /* Load constants */ adr TMP2, jsimd_idct_2x2_neon_consts vld1.16 {d0}, [TMP2, :64] /* Load all COEF_BLOCK into NEON registers with the following allocation: * 0 1 2 3 | 4 5 6 7 * ---------+-------- * 0 | d4 | d5 * 1 | d6 | d7 * 2 | - | - * 3 | d10 | d11 * 4 | - | - * 5 | d12 | d13 * 6 | - | - * 7 | d16 | d17 */ vld1.16 {d4, d5, d6, d7}, [COEF_BLOCK, :128]! add COEF_BLOCK, COEF_BLOCK, #16 vld1.16 {d10, d11}, [COEF_BLOCK, :128]! add COEF_BLOCK, COEF_BLOCK, #16 vld1.16 {d12, d13}, [COEF_BLOCK, :128]! add COEF_BLOCK, COEF_BLOCK, #16 vld1.16 {d16, d17}, [COEF_BLOCK, :128]! /* Dequantize */ vld1.16 {d18, d19, d20, d21}, [DCT_TABLE, :128]! vmul.s16 q2, q2, q9 vmul.s16 q3, q3, q10 add DCT_TABLE, DCT_TABLE, #16 vld1.16 {d24, d25}, [DCT_TABLE, :128]! vmul.s16 q5, q5, q12 add DCT_TABLE, DCT_TABLE, #16 vld1.16 {d26, d27}, [DCT_TABLE, :128]! vmul.s16 q6, q6, q13 add DCT_TABLE, DCT_TABLE, #16 vld1.16 {d30, d31}, [DCT_TABLE, :128]! vmul.s16 q8, q8, q15 /* Pass 1 */ #if 0 idct_helper d4, d6, d10, d12, d16, 13, d4, d6 transpose_4x4 d4, d6, d8, d10 idct_helper d5, d7, d11, d13, d17, 13, d5, d7 transpose_4x4 d5, d7, d9, d11 #else vmull.s16 q13, d6, d0[3] vmlal.s16 q13, d10, d0[2] vmlal.s16 q13, d12, d0[1] vmlal.s16 q13, d16, d0[0] vmull.s16 q12, d7, d0[3] vmlal.s16 q12, d11, d0[2] vmlal.s16 q12, d13, d0[1] vmlal.s16 q12, d17, d0[0] vshll.s16 q14, d4, #15 vshll.s16 q15, d5, #15 vadd.s32 q10, q14, q13 vsub.s32 q14, q14, q13 vrshrn.s32 d4, q10, #13 vrshrn.s32 d6, q14, #13 vadd.s32 q10, q15, q12 vsub.s32 q14, q15, q12 vrshrn.s32 d5, q10, #13 vrshrn.s32 d7, q14, #13 vtrn.16 q2, q3 vtrn.32 q3, q5 #endif /* Pass 2 */ idct_helper d4, d6, d10, d7, d11, 20, d26, d27 /* Range limit */ vmov.u16 q15, #0x80 vadd.s16 q13, q13, q15 vqmovun.s16 d26, q13 vqmovun.s16 d27, q13 /* Store results to the output buffer */ ldmia OUTPUT_BUF, {TMP1, TMP2} add TMP1, TMP1, OUTPUT_COL add TMP2, TMP2, OUTPUT_COL vst1.8 {d26[0]}, [TMP1]! vst1.8 {d27[4]}, [TMP1]! vst1.8 {d26[1]}, [TMP2]! vst1.8 {d27[5]}, [TMP2]! vpop {d8-d15} bx lr .unreq DCT_TABLE .unreq COEF_BLOCK .unreq OUTPUT_BUF .unreq OUTPUT_COL .unreq TMP1 .unreq TMP2 .endfunc .purgem idct_helper /*****************************************************************************/ /* * jsimd_ycc_extrgb_convert_neon * jsimd_ycc_extbgr_convert_neon * jsimd_ycc_extrgbx_convert_neon * jsimd_ycc_extbgrx_convert_neon * jsimd_ycc_extxbgr_convert_neon * jsimd_ycc_extxrgb_convert_neon * * Colorspace conversion YCbCr -> RGB */ .macro do_load size .if \size == 8 vld1.8 {d4}, [U, :64]! vld1.8 {d5}, [V, :64]! vld1.8 {d0}, [Y, :64]! pld [U, #64] pld [V, #64] pld [Y, #64] .elseif \size == 4 vld1.8 {d4[0]}, [U]! vld1.8 {d4[1]}, [U]! vld1.8 {d4[2]}, [U]! vld1.8 {d4[3]}, [U]! vld1.8 {d5[0]}, [V]! vld1.8 {d5[1]}, [V]! vld1.8 {d5[2]}, [V]! vld1.8 {d5[3]}, [V]! vld1.8 {d0[0]}, [Y]! vld1.8 {d0[1]}, [Y]! vld1.8 {d0[2]}, [Y]! vld1.8 {d0[3]}, [Y]! .elseif \size == 2 vld1.8 {d4[4]}, [U]! vld1.8 {d4[5]}, [U]! vld1.8 {d5[4]}, [V]! vld1.8 {d5[5]}, [V]! vld1.8 {d0[4]}, [Y]! vld1.8 {d0[5]}, [Y]! .elseif \size == 1 vld1.8 {d4[6]}, [U]! vld1.8 {d5[6]}, [V]! vld1.8 {d0[6]}, [Y]! .else .error unsupported macroblock size .endif .endm .macro do_store bpp, size .if \bpp == 24 .if \size == 8 vst3.8 {d10, d11, d12}, [RGB]! .elseif \size == 4 vst3.8 {d10[0], d11[0], d12[0]}, [RGB]! vst3.8 {d10[1], d11[1], d12[1]}, [RGB]! vst3.8 {d10[2], d11[2], d12[2]}, [RGB]! vst3.8 {d10[3], d11[3], d12[3]}, [RGB]! .elseif \size == 2 vst3.8 {d10[4], d11[4], d12[4]}, [RGB]! vst3.8 {d10[5], d11[5], d12[5]}, [RGB]! .elseif \size == 1 vst3.8 {d10[6], d11[6], d12[6]}, [RGB]! .else .error unsupported macroblock size .endif .elseif \bpp == 32 .if \size == 8 vst4.8 {d10, d11, d12, d13}, [RGB]! .elseif \size == 4 vst4.8 {d10[0], d11[0], d12[0], d13[0]}, [RGB]! vst4.8 {d10[1], d11[1], d12[1], d13[1]}, [RGB]! vst4.8 {d10[2], d11[2], d12[2], d13[2]}, [RGB]! vst4.8 {d10[3], d11[3], d12[3], d13[3]}, [RGB]! .elseif \size == 2 vst4.8 {d10[4], d11[4], d12[4], d13[4]}, [RGB]! vst4.8 {d10[5], d11[5], d12[5], d13[5]}, [RGB]! .elseif \size == 1 vst4.8 {d10[6], d11[6], d12[6], d13[6]}, [RGB]! .else .error unsupported macroblock size .endif .else .error unsupported bpp .endif .endm .macro generate_jsimd_ycc_rgb_convert_neon colorid, bpp, r_offs, g_offs, b_offs /* * 2 stage pipelined YCbCr->RGB conversion */ .macro do_yuv_to_rgb_stage1 vaddw.u8 q3, q1, d4 /* q3 = u - 128 */ vaddw.u8 q4, q1, d5 /* q2 = v - 128 */ vmull.s16 q10, d6, d1[1] /* multiply by -11277 */ vmlal.s16 q10, d8, d1[2] /* multiply by -23401 */ vmull.s16 q11, d7, d1[1] /* multiply by -11277 */ vmlal.s16 q11, d9, d1[2] /* multiply by -23401 */ vmull.s16 q12, d8, d1[0] /* multiply by 22971 */ vmull.s16 q13, d9, d1[0] /* multiply by 22971 */ vmull.s16 q14, d6, d1[3] /* multiply by 29033 */ vmull.s16 q15, d7, d1[3] /* multiply by 29033 */ .endm .macro do_yuv_to_rgb_stage2 vrshrn.s32 d20, q10, #15 vrshrn.s32 d21, q11, #15 vrshrn.s32 d24, q12, #14 vrshrn.s32 d25, q13, #14 vrshrn.s32 d28, q14, #14 vrshrn.s32 d29, q15, #14 vaddw.u8 q10, q10, d0 vaddw.u8 q12, q12, d0 vaddw.u8 q14, q14, d0 vqmovun.s16 d1\g_offs, q10 vqmovun.s16 d1\r_offs, q12 vqmovun.s16 d1\b_offs, q14 .endm .macro do_yuv_to_rgb_stage2_store_load_stage1 vld1.8 {d4}, [U, :64]! vrshrn.s32 d20, q10, #15 vrshrn.s32 d21, q11, #15 vrshrn.s32 d24, q12, #14 vrshrn.s32 d25, q13, #14 vrshrn.s32 d28, q14, #14 vld1.8 {d5}, [V, :64]! vrshrn.s32 d29, q15, #14 vaddw.u8 q10, q10, d0 vaddw.u8 q12, q12, d0 vaddw.u8 q14, q14, d0 vqmovun.s16 d1\g_offs, q10 vld1.8 {d0}, [Y, :64]! vqmovun.s16 d1\r_offs, q12 pld [U, #64] pld [V, #64] pld [Y, #64] vqmovun.s16 d1\b_offs, q14 vaddw.u8 q3, q1, d4 /* q3 = u - 128 */ vaddw.u8 q4, q1, d5 /* q2 = v - 128 */ do_store \bpp, 8 vmull.s16 q10, d6, d1[1] /* multiply by -11277 */ vmlal.s16 q10, d8, d1[2] /* multiply by -23401 */ vmull.s16 q11, d7, d1[1] /* multiply by -11277 */ vmlal.s16 q11, d9, d1[2] /* multiply by -23401 */ vmull.s16 q12, d8, d1[0] /* multiply by 22971 */ vmull.s16 q13, d9, d1[0] /* multiply by 22971 */ vmull.s16 q14, d6, d1[3] /* multiply by 29033 */ vmull.s16 q15, d7, d1[3] /* multiply by 29033 */ .endm .macro do_yuv_to_rgb do_yuv_to_rgb_stage1 do_yuv_to_rgb_stage2 .endm /* Apple gas crashes on adrl, work around that by using adr. * But this requires a copy of these constants for each function. */ .balign 16 jsimd_ycc_\colorid\()_neon_consts: .short 0, 0, 0, 0 .short 22971, -11277, -23401, 29033 .short -128, -128, -128, -128 .short -128, -128, -128, -128 asm_function jsimd_ycc_\colorid\()_convert_neon OUTPUT_WIDTH .req r0 INPUT_BUF .req r1 INPUT_ROW .req r2 OUTPUT_BUF .req r3 NUM_ROWS .req r4 INPUT_BUF0 .req r5 INPUT_BUF1 .req r6 INPUT_BUF2 .req INPUT_BUF RGB .req r7 Y .req r8 U .req r9 V .req r10 N .req ip /* Load constants to d1, d2, d3 (d0 is just used for padding) */ adr ip, jsimd_ycc_\colorid\()_neon_consts vld1.16 {d0, d1, d2, d3}, [ip, :128] /* Save ARM registers and handle input arguments */ push {r4, r5, r6, r7, r8, r9, r10, lr} ldr NUM_ROWS, [sp, #(4 * 8)] ldr INPUT_BUF0, [INPUT_BUF] ldr INPUT_BUF1, [INPUT_BUF, #4] ldr INPUT_BUF2, [INPUT_BUF, #8] .unreq INPUT_BUF /* Save NEON registers */ vpush {d8-d15} /* Initially set d10, d11, d12, d13 to 0xFF */ vmov.u8 q5, #255 vmov.u8 q6, #255 /* Outer loop over scanlines */ cmp NUM_ROWS, #1 blt 9f 0: ldr Y, [INPUT_BUF0, INPUT_ROW, lsl #2] ldr U, [INPUT_BUF1, INPUT_ROW, lsl #2] mov N, OUTPUT_WIDTH ldr V, [INPUT_BUF2, INPUT_ROW, lsl #2] add INPUT_ROW, INPUT_ROW, #1 ldr RGB, [OUTPUT_BUF], #4 /* Inner loop over pixels */ subs N, N, #8 blt 3f do_load 8 do_yuv_to_rgb_stage1 subs N, N, #8 blt 2f 1: do_yuv_to_rgb_stage2_store_load_stage1 subs N, N, #8 bge 1b 2: do_yuv_to_rgb_stage2 do_store \bpp, 8 tst N, #7 beq 8f 3: tst N, #4 beq 3f do_load 4 3: tst N, #2 beq 4f do_load 2 4: tst N, #1 beq 5f do_load 1 5: do_yuv_to_rgb tst N, #4 beq 6f do_store \bpp, 4 6: tst N, #2 beq 7f do_store \bpp, 2 7: tst N, #1 beq 8f do_store \bpp, 1 8: subs NUM_ROWS, NUM_ROWS, #1 bgt 0b 9: /* Restore all registers and return */ vpop {d8-d15} pop {r4, r5, r6, r7, r8, r9, r10, pc} .unreq OUTPUT_WIDTH .unreq INPUT_ROW .unreq OUTPUT_BUF .unreq NUM_ROWS .unreq INPUT_BUF0 .unreq INPUT_BUF1 .unreq INPUT_BUF2 .unreq RGB .unreq Y .unreq U .unreq V .unreq N .endfunc .purgem do_yuv_to_rgb .purgem do_yuv_to_rgb_stage1 .purgem do_yuv_to_rgb_stage2 .purgem do_yuv_to_rgb_stage2_store_load_stage1 .endm /*--------------------------------- id ----- bpp R G B */ generate_jsimd_ycc_rgb_convert_neon extrgb, 24, 0, 1, 2 generate_jsimd_ycc_rgb_convert_neon extbgr, 24, 2, 1, 0 generate_jsimd_ycc_rgb_convert_neon extrgbx, 32, 0, 1, 2 generate_jsimd_ycc_rgb_convert_neon extbgrx, 32, 2, 1, 0 generate_jsimd_ycc_rgb_convert_neon extxbgr, 32, 3, 2, 1 generate_jsimd_ycc_rgb_convert_neon extxrgb, 32, 1, 2, 3 .purgem do_load .purgem do_store /*****************************************************************************/ /* * jsimd_extrgb_ycc_convert_neon * jsimd_extbgr_ycc_convert_neon * jsimd_extrgbx_ycc_convert_neon * jsimd_extbgrx_ycc_convert_neon * jsimd_extxbgr_ycc_convert_neon * jsimd_extxrgb_ycc_convert_neon * * Colorspace conversion RGB -> YCbCr */ .macro do_store size .if \size == 8 vst1.8 {d20}, [Y]! vst1.8 {d21}, [U]! vst1.8 {d22}, [V]! .elseif \size == 4 vst1.8 {d20[0]}, [Y]! vst1.8 {d20[1]}, [Y]! vst1.8 {d20[2]}, [Y]! vst1.8 {d20[3]}, [Y]! vst1.8 {d21[0]}, [U]! vst1.8 {d21[1]}, [U]! vst1.8 {d21[2]}, [U]! vst1.8 {d21[3]}, [U]! vst1.8 {d22[0]}, [V]! vst1.8 {d22[1]}, [V]! vst1.8 {d22[2]}, [V]! vst1.8 {d22[3]}, [V]! .elseif \size == 2 vst1.8 {d20[4]}, [Y]! vst1.8 {d20[5]}, [Y]! vst1.8 {d21[4]}, [U]! vst1.8 {d21[5]}, [U]! vst1.8 {d22[4]}, [V]! vst1.8 {d22[5]}, [V]! .elseif \size == 1 vst1.8 {d20[6]}, [Y]! vst1.8 {d21[6]}, [U]! vst1.8 {d22[6]}, [V]! .else .error unsupported macroblock size .endif .endm .macro do_load bpp, size .if \bpp == 24 .if \size == 8 vld3.8 {d10, d11, d12}, [RGB]! pld [RGB, #128] .elseif \size == 4 vld3.8 {d10[0], d11[0], d12[0]}, [RGB]! vld3.8 {d10[1], d11[1], d12[1]}, [RGB]! vld3.8 {d10[2], d11[2], d12[2]}, [RGB]! vld3.8 {d10[3], d11[3], d12[3]}, [RGB]! .elseif \size == 2 vld3.8 {d10[4], d11[4], d12[4]}, [RGB]! vld3.8 {d10[5], d11[5], d12[5]}, [RGB]! .elseif \size == 1 vld3.8 {d10[6], d11[6], d12[6]}, [RGB]! .else .error unsupported macroblock size .endif .elseif \bpp == 32 .if \size == 8 vld4.8 {d10, d11, d12, d13}, [RGB]! pld [RGB, #128] .elseif \size == 4 vld4.8 {d10[0], d11[0], d12[0], d13[0]}, [RGB]! vld4.8 {d10[1], d11[1], d12[1], d13[1]}, [RGB]! vld4.8 {d10[2], d11[2], d12[2], d13[2]}, [RGB]! vld4.8 {d10[3], d11[3], d12[3], d13[3]}, [RGB]! .elseif \size == 2 vld4.8 {d10[4], d11[4], d12[4], d13[4]}, [RGB]! vld4.8 {d10[5], d11[5], d12[5], d13[5]}, [RGB]! .elseif \size == 1 vld4.8 {d10[6], d11[6], d12[6], d13[6]}, [RGB]! .else .error unsupported macroblock size .endif .else .error unsupported bpp .endif .endm .macro generate_jsimd_rgb_ycc_convert_neon colorid, bpp, r_offs, g_offs, b_offs /* * 2 stage pipelined RGB->YCbCr conversion */ .macro do_rgb_to_yuv_stage1 vmovl.u8 q2, d1\r_offs /* r = { d4, d5 } */ vmovl.u8 q3, d1\g_offs /* g = { d6, d7 } */ vmovl.u8 q4, d1\b_offs /* b = { d8, d9 } */ vmull.u16 q7, d4, d0[0] vmlal.u16 q7, d6, d0[1] vmlal.u16 q7, d8, d0[2] vmull.u16 q8, d5, d0[0] vmlal.u16 q8, d7, d0[1] vmlal.u16 q8, d9, d0[2] vrev64.32 q9, q1 vrev64.32 q13, q1 vmlsl.u16 q9, d4, d0[3] vmlsl.u16 q9, d6, d1[0] vmlal.u16 q9, d8, d1[1] vmlsl.u16 q13, d5, d0[3] vmlsl.u16 q13, d7, d1[0] vmlal.u16 q13, d9, d1[1] vrev64.32 q14, q1 vrev64.32 q15, q1 vmlal.u16 q14, d4, d1[1] vmlsl.u16 q14, d6, d1[2] vmlsl.u16 q14, d8, d1[3] vmlal.u16 q15, d5, d1[1] vmlsl.u16 q15, d7, d1[2] vmlsl.u16 q15, d9, d1[3] .endm .macro do_rgb_to_yuv_stage2 vrshrn.u32 d20, q7, #16 vrshrn.u32 d21, q8, #16 vshrn.u32 d22, q9, #16 vshrn.u32 d23, q13, #16 vshrn.u32 d24, q14, #16 vshrn.u32 d25, q15, #16 vmovn.u16 d20, q10 /* d20 = y */ vmovn.u16 d21, q11 /* d21 = u */ vmovn.u16 d22, q12 /* d22 = v */ .endm .macro do_rgb_to_yuv do_rgb_to_yuv_stage1 do_rgb_to_yuv_stage2 .endm .macro do_rgb_to_yuv_stage2_store_load_stage1 vrshrn.u32 d20, q7, #16 vrshrn.u32 d21, q8, #16 vshrn.u32 d22, q9, #16 vrev64.32 q9, q1 vshrn.u32 d23, q13, #16 vrev64.32 q13, q1 vshrn.u32 d24, q14, #16 vshrn.u32 d25, q15, #16 do_load \bpp, 8 vmovn.u16 d20, q10 /* d20 = y */ vmovl.u8 q2, d1\r_offs /* r = { d4, d5 } */ vmovn.u16 d21, q11 /* d21 = u */ vmovl.u8 q3, d1\g_offs /* g = { d6, d7 } */ vmovn.u16 d22, q12 /* d22 = v */ vmovl.u8 q4, d1\b_offs /* b = { d8, d9 } */ vmull.u16 q7, d4, d0[0] vmlal.u16 q7, d6, d0[1] vmlal.u16 q7, d8, d0[2] vst1.8 {d20}, [Y]! vmull.u16 q8, d5, d0[0] vmlal.u16 q8, d7, d0[1] vmlal.u16 q8, d9, d0[2] vmlsl.u16 q9, d4, d0[3] vmlsl.u16 q9, d6, d1[0] vmlal.u16 q9, d8, d1[1] vst1.8 {d21}, [U]! vmlsl.u16 q13, d5, d0[3] vmlsl.u16 q13, d7, d1[0] vmlal.u16 q13, d9, d1[1] vrev64.32 q14, q1 vrev64.32 q15, q1 vmlal.u16 q14, d4, d1[1] vmlsl.u16 q14, d6, d1[2] vmlsl.u16 q14, d8, d1[3] vst1.8 {d22}, [V]! vmlal.u16 q15, d5, d1[1] vmlsl.u16 q15, d7, d1[2] vmlsl.u16 q15, d9, d1[3] .endm .balign 16 jsimd_\colorid\()_ycc_neon_consts: .short 19595, 38470, 7471, 11059 .short 21709, 32768, 27439, 5329 .short 32767, 128, 32767, 128 .short 32767, 128, 32767, 128 asm_function jsimd_\colorid\()_ycc_convert_neon OUTPUT_WIDTH .req r0 INPUT_BUF .req r1 OUTPUT_BUF .req r2 OUTPUT_ROW .req r3 NUM_ROWS .req r4 OUTPUT_BUF0 .req r5 OUTPUT_BUF1 .req r6 OUTPUT_BUF2 .req OUTPUT_BUF RGB .req r7 Y .req r8 U .req r9 V .req r10 N .req ip /* Load constants to d0, d1, d2, d3 */ adr ip, jsimd_\colorid\()_ycc_neon_consts vld1.16 {d0, d1, d2, d3}, [ip, :128] /* Save ARM registers and handle input arguments */ push {r4, r5, r6, r7, r8, r9, r10, lr} ldr NUM_ROWS, [sp, #(4 * 8)] ldr OUTPUT_BUF0, [OUTPUT_BUF] ldr OUTPUT_BUF1, [OUTPUT_BUF, #4] ldr OUTPUT_BUF2, [OUTPUT_BUF, #8] .unreq OUTPUT_BUF /* Save NEON registers */ vpush {d8-d15} /* Outer loop over scanlines */ cmp NUM_ROWS, #1 blt 9f 0: ldr Y, [OUTPUT_BUF0, OUTPUT_ROW, lsl #2] ldr U, [OUTPUT_BUF1, OUTPUT_ROW, lsl #2] mov N, OUTPUT_WIDTH ldr V, [OUTPUT_BUF2, OUTPUT_ROW, lsl #2] add OUTPUT_ROW, OUTPUT_ROW, #1 ldr RGB, [INPUT_BUF], #4 /* Inner loop over pixels */ subs N, N, #8 blt 3f do_load \bpp, 8 do_rgb_to_yuv_stage1 subs N, N, #8 blt 2f 1: do_rgb_to_yuv_stage2_store_load_stage1 subs N, N, #8 bge 1b 2: do_rgb_to_yuv_stage2 do_store 8 tst N, #7 beq 8f 3: tst N, #4 beq 3f do_load \bpp, 4 3: tst N, #2 beq 4f do_load \bpp, 2 4: tst N, #1 beq 5f do_load \bpp, 1 5: do_rgb_to_yuv tst N, #4 beq 6f do_store 4 6: tst N, #2 beq 7f do_store 2 7: tst N, #1 beq 8f do_store 1 8: subs NUM_ROWS, NUM_ROWS, #1 bgt 0b 9: /* Restore all registers and return */ vpop {d8-d15} pop {r4, r5, r6, r7, r8, r9, r10, pc} .unreq OUTPUT_WIDTH .unreq OUTPUT_ROW .unreq INPUT_BUF .unreq NUM_ROWS .unreq OUTPUT_BUF0 .unreq OUTPUT_BUF1 .unreq OUTPUT_BUF2 .unreq RGB .unreq Y .unreq U .unreq V .unreq N .endfunc .purgem do_rgb_to_yuv .purgem do_rgb_to_yuv_stage1 .purgem do_rgb_to_yuv_stage2 .purgem do_rgb_to_yuv_stage2_store_load_stage1 .endm /*--------------------------------- id ----- bpp R G B */ generate_jsimd_rgb_ycc_convert_neon extrgb, 24, 0, 1, 2 generate_jsimd_rgb_ycc_convert_neon extbgr, 24, 2, 1, 0 generate_jsimd_rgb_ycc_convert_neon extrgbx, 32, 0, 1, 2 generate_jsimd_rgb_ycc_convert_neon extbgrx, 32, 2, 1, 0 generate_jsimd_rgb_ycc_convert_neon extxbgr, 32, 3, 2, 1 generate_jsimd_rgb_ycc_convert_neon extxrgb, 32, 1, 2, 3 .purgem do_load .purgem do_store /*****************************************************************************/ /* * Load data into workspace, applying unsigned->signed conversion * * TODO: can be combined with 'jsimd_fdct_ifast_neon' to get * rid of VST1.16 instructions */ asm_function jsimd_convsamp_neon SAMPLE_DATA .req r0 START_COL .req r1 WORKSPACE .req r2 TMP1 .req r3 TMP2 .req r4 TMP3 .req r5 TMP4 .req ip push {r4, r5} vmov.u8 d0, #128 ldmia SAMPLE_DATA!, {TMP1, TMP2, TMP3, TMP4} add TMP1, TMP1, START_COL add TMP2, TMP2, START_COL add TMP3, TMP3, START_COL add TMP4, TMP4, START_COL vld1.8 {d16}, [TMP1] vsubl.u8 q8, d16, d0 vld1.8 {d18}, [TMP2] vsubl.u8 q9, d18, d0 vld1.8 {d20}, [TMP3] vsubl.u8 q10, d20, d0 vld1.8 {d22}, [TMP4] ldmia SAMPLE_DATA!, {TMP1, TMP2, TMP3, TMP4} vsubl.u8 q11, d22, d0 vst1.16 {d16, d17, d18, d19}, [WORKSPACE, :128]! add TMP1, TMP1, START_COL add TMP2, TMP2, START_COL vst1.16 {d20, d21, d22, d23}, [WORKSPACE, :128]! add TMP3, TMP3, START_COL add TMP4, TMP4, START_COL vld1.8 {d24}, [TMP1] vsubl.u8 q12, d24, d0 vld1.8 {d26}, [TMP2] vsubl.u8 q13, d26, d0 vld1.8 {d28}, [TMP3] vsubl.u8 q14, d28, d0 vld1.8 {d30}, [TMP4] vsubl.u8 q15, d30, d0 vst1.16 {d24, d25, d26, d27}, [WORKSPACE, :128]! vst1.16 {d28, d29, d30, d31}, [WORKSPACE, :128]! pop {r4, r5} bx lr .unreq SAMPLE_DATA .unreq START_COL .unreq WORKSPACE .unreq TMP1 .unreq TMP2 .unreq TMP3 .unreq TMP4 .endfunc /*****************************************************************************/ /* * jsimd_fdct_ifast_neon * * This function contains a fast, not so accurate integer implementation of * the forward DCT (Discrete Cosine Transform). It uses the same calculations * and produces exactly the same output as IJG's original 'jpeg_fdct_ifast' * function from jfdctfst.c * * TODO: can be combined with 'jsimd_convsamp_neon' to get * rid of a bunch of VLD1.16 instructions */ #define XFIX_0_382683433 d0[0] #define XFIX_0_541196100 d0[1] #define XFIX_0_707106781 d0[2] #define XFIX_1_306562965 d0[3] .balign 16 jsimd_fdct_ifast_neon_consts: .short (98 * 128) /* XFIX_0_382683433 */ .short (139 * 128) /* XFIX_0_541196100 */ .short (181 * 128) /* XFIX_0_707106781 */ .short (334 * 128 - 256 * 128) /* XFIX_1_306562965 */ asm_function jsimd_fdct_ifast_neon DATA .req r0 TMP .req ip vpush {d8-d15} /* Load constants */ adr TMP, jsimd_fdct_ifast_neon_consts vld1.16 {d0}, [TMP, :64] /* Load all DATA into NEON registers with the following allocation: * 0 1 2 3 | 4 5 6 7 * ---------+-------- * 0 | d16 | d17 | q8 * 1 | d18 | d19 | q9 * 2 | d20 | d21 | q10 * 3 | d22 | d23 | q11 * 4 | d24 | d25 | q12 * 5 | d26 | d27 | q13 * 6 | d28 | d29 | q14 * 7 | d30 | d31 | q15 */ vld1.16 {d16, d17, d18, d19}, [DATA, :128]! vld1.16 {d20, d21, d22, d23}, [DATA, :128]! vld1.16 {d24, d25, d26, d27}, [DATA, :128]! vld1.16 {d28, d29, d30, d31}, [DATA, :128] sub DATA, DATA, #(128 - 32) mov TMP, #2 1: /* Transpose */ vtrn.16 q12, q13 vtrn.16 q10, q11 vtrn.16 q8, q9 vtrn.16 q14, q15 vtrn.32 q9, q11 vtrn.32 q13, q15 vtrn.32 q8, q10 vtrn.32 q12, q14 vswp d30, d23 vswp d24, d17 vswp d26, d19 /* 1-D FDCT */ vadd.s16 q2, q11, q12 vswp d28, d21 vsub.s16 q12, q11, q12 vsub.s16 q6, q10, q13 vadd.s16 q10, q10, q13 vsub.s16 q7, q9, q14 vadd.s16 q9, q9, q14 vsub.s16 q1, q8, q15 vadd.s16 q8, q8, q15 vsub.s16 q4, q9, q10 vsub.s16 q5, q8, q2 vadd.s16 q3, q9, q10 vadd.s16 q4, q4, q5 vadd.s16 q2, q8, q2 vqdmulh.s16 q4, q4, XFIX_0_707106781 vadd.s16 q11, q12, q6 vadd.s16 q8, q2, q3 vsub.s16 q12, q2, q3 vadd.s16 q3, q6, q7 vadd.s16 q7, q7, q1 vqdmulh.s16 q3, q3, XFIX_0_707106781 vsub.s16 q6, q11, q7 vadd.s16 q10, q5, q4 vqdmulh.s16 q6, q6, XFIX_0_382683433 vsub.s16 q14, q5, q4 vqdmulh.s16 q11, q11, XFIX_0_541196100 vqdmulh.s16 q5, q7, XFIX_1_306562965 vadd.s16 q4, q1, q3 vsub.s16 q3, q1, q3 vadd.s16 q7, q7, q6 vadd.s16 q11, q11, q6 vadd.s16 q7, q7, q5 vadd.s16 q13, q3, q11 vsub.s16 q11, q3, q11 vadd.s16 q9, q4, q7 vsub.s16 q15, q4, q7 subs TMP, TMP, #1 bne 1b /* store results */ vst1.16 {d16, d17, d18, d19}, [DATA, :128]! vst1.16 {d20, d21, d22, d23}, [DATA, :128]! vst1.16 {d24, d25, d26, d27}, [DATA, :128]! vst1.16 {d28, d29, d30, d31}, [DATA, :128] vpop {d8-d15} bx lr .unreq DATA .unreq TMP .endfunc /*****************************************************************************/ /* * GLOBAL(void) * jsimd_quantize_neon (JCOEFPTR coef_block, DCTELEM * divisors, * DCTELEM * workspace); * * Note: the code uses 2 stage pipelining in order to improve instructions * scheduling and eliminate stalls (this provides ~15% better * performance for this function on both ARM Cortex-A8 and * ARM Cortex-A9 when compared to the non-pipelined variant). * The instructions which belong to the second stage use different * indentation for better readiability. */ asm_function jsimd_quantize_neon COEF_BLOCK .req r0 DIVISORS .req r1 WORKSPACE .req r2 RECIPROCAL .req DIVISORS CORRECTION .req r3 SHIFT .req ip LOOP_COUNT .req r4 vld1.16 {d0, d1, d2, d3}, [WORKSPACE, :128]! vabs.s16 q12, q0 add CORRECTION, DIVISORS, #(64 * 2) add SHIFT, DIVISORS, #(64 * 6) vld1.16 {d20, d21, d22, d23}, [CORRECTION, :128]! vabs.s16 q13, q1 vld1.16 {d16, d17, d18, d19}, [RECIPROCAL, :128]! vadd.u16 q12, q12, q10 /* add correction */ vadd.u16 q13, q13, q11 vmull.u16 q10, d24, d16 /* multiply by reciprocal */ vmull.u16 q11, d25, d17 vmull.u16 q8, d26, d18 vmull.u16 q9, d27, d19 vld1.16 {d24, d25, d26, d27}, [SHIFT, :128]! vshrn.u32 d20, q10, #16 vshrn.u32 d21, q11, #16 vshrn.u32 d22, q8, #16 vshrn.u32 d23, q9, #16 vneg.s16 q12, q12 vneg.s16 q13, q13 vshr.s16 q2, q0, #15 /* extract sign */ vshr.s16 q3, q1, #15 vshl.u16 q14, q10, q12 /* shift */ vshl.u16 q15, q11, q13 push {r4, r5} mov LOOP_COUNT, #3 1: vld1.16 {d0, d1, d2, d3}, [WORKSPACE, :128]! veor.u16 q14, q14, q2 /* restore sign */ vabs.s16 q12, q0 vld1.16 {d20, d21, d22, d23}, [CORRECTION, :128]! vabs.s16 q13, q1 veor.u16 q15, q15, q3 vld1.16 {d16, d17, d18, d19}, [RECIPROCAL, :128]! vadd.u16 q12, q12, q10 /* add correction */ vadd.u16 q13, q13, q11 vmull.u16 q10, d24, d16 /* multiply by reciprocal */ vmull.u16 q11, d25, d17 vmull.u16 q8, d26, d18 vmull.u16 q9, d27, d19 vsub.u16 q14, q14, q2 vld1.16 {d24, d25, d26, d27}, [SHIFT, :128]! vsub.u16 q15, q15, q3 vshrn.u32 d20, q10, #16 vshrn.u32 d21, q11, #16 vst1.16 {d28, d29, d30, d31}, [COEF_BLOCK, :128]! vshrn.u32 d22, q8, #16 vshrn.u32 d23, q9, #16 vneg.s16 q12, q12 vneg.s16 q13, q13 vshr.s16 q2, q0, #15 /* extract sign */ vshr.s16 q3, q1, #15 vshl.u16 q14, q10, q12 /* shift */ vshl.u16 q15, q11, q13 subs LOOP_COUNT, LOOP_COUNT, #1 bne 1b pop {r4, r5} veor.u16 q14, q14, q2 /* restore sign */ veor.u16 q15, q15, q3 vsub.u16 q14, q14, q2 vsub.u16 q15, q15, q3 vst1.16 {d28, d29, d30, d31}, [COEF_BLOCK, :128]! bx lr /* return */ .unreq COEF_BLOCK .unreq DIVISORS .unreq WORKSPACE .unreq RECIPROCAL .unreq CORRECTION .unreq SHIFT .unreq LOOP_COUNT .endfunc glmark2-2012.08/./src/scene-ideas/characters.h0000664000175000017500000001020612013417376020154 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #ifndef CHARACTERS_H_ #define CHARACTERS_H_ #include #include "vec.h" #include "gl-headers.h" class PrimitiveState { public: PrimitiveState(unsigned int type, unsigned int count, unsigned int offset) : type_(type), count_(count), bufferOffset_(offset) {} ~PrimitiveState() {} void issue() const { glDrawElements(type_, count_, GL_UNSIGNED_SHORT, reinterpret_cast(bufferOffset_)); } private: PrimitiveState(); unsigned int type_; // Primitive type (e.g. GL_TRIANGLE_STRIP) unsigned int count_; // Number of primitives unsigned int bufferOffset_; // Offset into the element array buffer }; struct Character { void draw() { glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glVertexAttribPointer(vertexIndex_, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(vertexIndex_); for (std::vector::const_iterator primIt = primVec_.begin(); primIt != primVec_.end(); primIt++) { primIt->issue(); } glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void init(int vertexAttribIndex) { vertexIndex_ = vertexAttribIndex; // We need 2 buffers for our work here. One for the vertex data. // and one for the index data. glGenBuffers(2, &bufferObjects_[0]); // First, setup the vertex data by binding the first buffer object, // allocating its data store, and filling it in with our vertex data. glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBufferData(GL_ARRAY_BUFFER, vertexData_.size() * sizeof(LibMatrix::vec2), &vertexData_.front(), GL_STATIC_DRAW); // Finally, setup the pointer to our vertex data and enable this // attribute array. glVertexAttribPointer(vertexIndex_, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(vertexIndex_); // Now repeat for our index data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData_.size() * sizeof(unsigned short), &indexData_.front(), GL_STATIC_DRAW); // Unbind our vertex buffer objects so that their state isn't affected // by other objects. glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } ~Character() { glDeleteBuffers(2, &bufferObjects_[0]); } Character() : vertexIndex_(0), vertexArray_(0) {} unsigned int bufferObjects_[2]; std::vector vertexData_; std::vector indexData_; int vertexIndex_; unsigned int vertexArray_; std::vector primVec_; }; struct LetterI : Character { LetterI(); }; struct LetterD : Character { LetterD(); }; struct LetterE : Character { LetterE(); }; struct LetterA : Character { LetterA(); }; struct LetterS : Character { LetterS(); }; struct LetterN : Character { LetterN(); }; struct LetterM : Character { LetterM(); }; struct LetterO : Character { LetterO(); }; struct LetterT : Character { LetterT(); }; #endif // CHARACTERS_H_ glmark2-2012.08/./src/scene-ideas/lamp.cc0000664000175000017500000002677012013417376017141 0ustar alfalf00000000000000/* * Vertex position data describing the lamp * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "lamp.h" #include "shader-source.h" #include "log.h" #include "scene.h" using std::string; using LibMatrix::vec3; using LibMatrix::vec4; using LibMatrix::Stack4; const string Lamp::modelviewName_("modelview"); const string Lamp::projectionName_("projection"); const string Lamp::light0PositionName_("light0Position"); const string Lamp::light1PositionName_("light1Position"); const string Lamp::light2PositionName_("light2Position"); const string Lamp::vertexAttribName_("vertex"); const string Lamp::normalAttribName_("normal"); const string Lamp::normalMatrixName_("normalMatrix"); Lamp::Lamp() : valid_(false) { vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.000000, 0.000000, 1.000000)); vertexData_.push_back(vec3(0.438371, 0.000000, 0.898794)); vertexData_.push_back(vec3(0.379641, 0.219186, 0.898794)); vertexData_.push_back(vec3(0.219186, 0.379641, 0.898794)); vertexData_.push_back(vec3(0.000000, 0.438371, 0.898794)); vertexData_.push_back(vec3(-0.219186, 0.379641, 0.898794)); vertexData_.push_back(vec3(-0.379641, 0.219186, 0.898794)); vertexData_.push_back(vec3(-0.438371, 0.000000, 0.898794)); vertexData_.push_back(vec3(-0.379641, -0.219186, 0.898794)); vertexData_.push_back(vec3(-0.219186, -0.379641, 0.898794)); vertexData_.push_back(vec3(0.000000, -0.438371, 0.898794)); vertexData_.push_back(vec3(0.219186, -0.379641, 0.898794)); vertexData_.push_back(vec3(0.379641, -0.219186, 0.898794)); vertexData_.push_back(vec3(0.438371, 0.000000, 0.898794)); vertexData_.push_back(vec3(0.788011, 0.000000, 0.615662)); vertexData_.push_back(vec3(0.682437, 0.394005, 0.615662)); vertexData_.push_back(vec3(0.394005, 0.682437, 0.615662)); vertexData_.push_back(vec3(0.000000, 0.788011, 0.615662)); vertexData_.push_back(vec3(-0.394005, 0.682437, 0.615662)); vertexData_.push_back(vec3(-0.682437, 0.394005, 0.615662)); vertexData_.push_back(vec3(-0.788011, 0.000000, 0.615662)); vertexData_.push_back(vec3(-0.682437, -0.394005, 0.615662)); vertexData_.push_back(vec3(-0.394005, -0.682437, 0.615662)); vertexData_.push_back(vec3(0.000000, -0.788011, 0.615662)); vertexData_.push_back(vec3(0.394005, -0.682437, 0.615662)); vertexData_.push_back(vec3(0.682437, -0.394005, 0.615662)); vertexData_.push_back(vec3(0.788011, 0.000000, 0.615662)); vertexData_.push_back(vec3(0.978148, 0.000000, 0.207912)); vertexData_.push_back(vec3(0.847101, 0.489074, 0.207912)); vertexData_.push_back(vec3(0.489074, 0.847101, 0.207912)); vertexData_.push_back(vec3(0.000000, 0.978148, 0.207912)); vertexData_.push_back(vec3(-0.489074, 0.847101, 0.207912)); vertexData_.push_back(vec3(-0.847101, 0.489074, 0.207912)); vertexData_.push_back(vec3(-0.978148, 0.000000, 0.207912)); vertexData_.push_back(vec3(-0.847101, -0.489074, 0.207912)); vertexData_.push_back(vec3(-0.489074, -0.847101, 0.207912)); vertexData_.push_back(vec3(0.000000, -0.978148, 0.207912)); vertexData_.push_back(vec3(0.489074, -0.847101, 0.207912)); vertexData_.push_back(vec3(0.847101, -0.489074, 0.207912)); vertexData_.push_back(vec3(0.978148, 0.000000, 0.207912)); vertexData_.push_back(vec3(0.970296, 0.000000, -0.241922)); vertexData_.push_back(vec3(0.840301, 0.485148, -0.241922)); vertexData_.push_back(vec3(0.485148, 0.840301, -0.241922)); vertexData_.push_back(vec3(0.000000, 0.970296, -0.241922)); vertexData_.push_back(vec3(-0.485148, 0.840301, -0.241922)); vertexData_.push_back(vec3(-0.840301, 0.485148, -0.241922)); vertexData_.push_back(vec3(-0.970296, 0.000000, -0.241922)); vertexData_.push_back(vec3(-0.840301, -0.485148, -0.241922)); vertexData_.push_back(vec3(-0.485148, -0.840301, -0.241922)); vertexData_.push_back(vec3(0.000000, -0.970296, -0.241922)); vertexData_.push_back(vec3(0.485148, -0.840301, -0.241922)); vertexData_.push_back(vec3(0.840301, -0.485148, -0.241922)); vertexData_.push_back(vec3(0.970296, 0.000000, -0.241922)); vertexData_.push_back(vec3(0.766044, 0.000000, -0.642788)); vertexData_.push_back(vec3(0.663414, 0.383022, -0.642788)); vertexData_.push_back(vec3(0.383022, 0.663414, -0.642788)); vertexData_.push_back(vec3(0.000000, 0.766044, -0.642788)); vertexData_.push_back(vec3(-0.383022, 0.663414, -0.642788)); vertexData_.push_back(vec3(-0.663414, 0.383022, -0.642788)); vertexData_.push_back(vec3(-0.766044, 0.000000, -0.642788)); vertexData_.push_back(vec3(-0.663414, -0.383022, -0.642788)); vertexData_.push_back(vec3(-0.383022, -0.663414, -0.642788)); vertexData_.push_back(vec3(0.000000, -0.766044, -0.642788)); vertexData_.push_back(vec3(0.383022, -0.663414, -0.642788)); vertexData_.push_back(vec3(0.663414, -0.383022, -0.642788)); vertexData_.push_back(vec3(0.766044, 0.000000, -0.642788)); // // The original implementation of both the logo and the lamp represented // the vertex and normal data in a triply-dimensioned array of floats and // all of the calls referenced double-indexed arrays of vector data. // To my mind, this made the code look clunky and overly verbose. // Representing the data as a STL vector of vec3 (itself a 3-float vector // quantity) provides both an efficient container and allows for more // concise looking code. The slightly goofy loops (using the original 2 // dimensional indices to compute a single offset into the STL vector) are // a compromise to avoid rearranging the original data. // // - jesse 2010/10/04 // for (unsigned int i = 0; i < 5; i++) { for (unsigned int j = 0; j < 13; j++) { indexData_.push_back(i * 13 + j); indexData_.push_back((i + 1) * 13 + j); } } unsigned int curIndex(5 * 13); for (unsigned int i = 0; i < 12; i++) { indexData_.push_back(curIndex + i); } } Lamp::~Lamp() { if (valid_) { glDeleteBuffers(2, &bufferObjects_[0]); } } void Lamp::init() { // Make sure we don't re-initialize... if (valid_) { return; } // Initialize shader sources from input files and create programs from them // The program for handling lighting... string lit_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-lamp-lit.vert"); string lit_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-lamp-lit.frag"); ShaderSource lit_vtx_source(lit_vtx_filename); ShaderSource lit_frg_source(lit_frg_filename); if (!Scene::load_shaders_from_strings(litProgram_, lit_vtx_source.str(), lit_frg_source.str())) { Log::error("No valid program for lit lamp rendering\n"); return; } // The simple program with no lighting... string unlit_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-lamp-unlit.vert"); string unlit_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-lamp-unlit.frag"); ShaderSource unlit_vtx_source(unlit_vtx_filename); ShaderSource unlit_frg_source(unlit_frg_filename); if (!Scene::load_shaders_from_strings(unlitProgram_, unlit_vtx_source.str(), unlit_frg_source.str())) { Log::error("No valid program for unlit lamp rendering.\n"); return; } // We need 2 buffers for our work here. One for the vertex data. // and one for the index data. glGenBuffers(2, &bufferObjects_[0]); // First, setup the vertex data by binding the first buffer object, // allocating its data store, and filling it in with our vertex data. glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBufferData(GL_ARRAY_BUFFER, vertexData_.size() * sizeof(vec3), &vertexData_.front(), GL_STATIC_DRAW); // Now repeat for our index data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData_.size() * sizeof(unsigned short), &indexData_.front(), GL_STATIC_DRAW); // We're ready to go. valid_ = true; } void Lamp::draw(Stack4& modelview, Stack4& projection, const vec4* lightPositions) { glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); litProgram_.start(); int vertexIndex(litProgram_[vertexAttribName_].location()); int normalIndex(litProgram_[normalAttribName_].location()); glVertexAttribPointer(vertexIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(vertexIndex); glEnableVertexAttribArray(normalIndex); const LibMatrix::mat4& mv = modelview.getCurrent(); LibMatrix::mat3 normalMatrix(mv[0][0], mv[1][0], mv[2][0], mv[0][1], mv[1][1], mv[2][1], mv[0][2], mv[1][2], mv[2][2]); normalMatrix.transpose().inverse(); litProgram_[normalMatrixName_] = normalMatrix; litProgram_[modelviewName_] = mv; litProgram_[projectionName_] = projection.getCurrent(); litProgram_[light0PositionName_] = lightPositions[0]; litProgram_[light1PositionName_] = lightPositions[1]; litProgram_[light2PositionName_] = lightPositions[2]; static const unsigned int sus(sizeof(unsigned short)); for (unsigned int i = 0; i < 5; i++) { glDrawElements(GL_TRIANGLE_STRIP, 26, GL_UNSIGNED_SHORT, reinterpret_cast(i * 26 * sus)); } glDisableVertexAttribArray(normalIndex); glDisableVertexAttribArray(vertexIndex); litProgram_.stop(); unlitProgram_.start(); vertexIndex = unlitProgram_[vertexAttribName_].location(); glVertexAttribPointer(vertexIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(vertexIndex); unlitProgram_[modelviewName_] = mv; unlitProgram_[projectionName_] = projection.getCurrent(); glDrawElements(GL_TRIANGLE_FAN, 12, GL_UNSIGNED_SHORT, reinterpret_cast(5 * 26 * sus)); glDisableVertexAttribArray(vertexIndex); unlitProgram_.stop(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } glmark2-2012.08/./src/scene-ideas/o.cc0000664000175000017500000001161112013417376016432 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'o' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterO::LetterO() { // Vertex data... vertexData_.push_back(vec2(2.975610, 9.603255)); vertexData_.push_back(vec2(2.878049, 9.342828)); vertexData_.push_back(vec2(2.292683, 9.131231)); vertexData_.push_back(vec2(2.048780, 8.691760)); vertexData_.push_back(vec2(1.707317, 8.528993)); vertexData_.push_back(vec2(1.658537, 7.731434)); vertexData_.push_back(vec2(0.878049, 7.047813)); vertexData_.push_back(vec2(1.349594, 5.550356)); vertexData_.push_back(vec2(0.569106, 5.029501)); vertexData_.push_back(vec2(1.528455, 4.443540)); vertexData_.push_back(vec2(0.991870, 3.434385)); vertexData_.push_back(vec2(1.967480, 3.955239)); vertexData_.push_back(vec2(1.772358, 2.994914)); vertexData_.push_back(vec2(2.422764, 3.825025)); vertexData_.push_back(vec2(2.829268, 3.092574)); vertexData_.push_back(vec2(3.154472, 3.971516)); vertexData_.push_back(vec2(3.512195, 3.727365)); vertexData_.push_back(vec2(3.772358, 4.264496)); vertexData_.push_back(vec2(4.130081, 4.524924)); vertexData_.push_back(vec2(4.162601, 4.996948)); vertexData_.push_back(vec2(4.699187, 5.403866)); vertexData_.push_back(vec2(4.471545, 6.461852)); vertexData_.push_back(vec2(5.219512, 7.243133)); vertexData_.push_back(vec2(4.439024, 8.105799)); vertexData_.push_back(vec2(5.235772, 8.756866)); vertexData_.push_back(vec2(4.065041, 8.870804)); vertexData_.push_back(vec2(4.991870, 9.391658)); vertexData_.push_back(vec2(3.853658, 9.228891)); vertexData_.push_back(vec2(4.390244, 9.912513)); vertexData_.push_back(vec2(3.463415, 9.407935)); vertexData_.push_back(vec2(3.674797, 9.912513)); vertexData_.push_back(vec2(2.829268, 9.342828)); vertexData_.push_back(vec2(2.959350, 9.603255)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(31); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 33, curOffset)); curOffset += (33 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 33, curOffset)); } glmark2-2012.08/./src/scene-ideas/e.cc0000664000175000017500000001161112013417376016420 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'e' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterE::LetterE() { // Vertex data... vertexData_.push_back(vec2(1.095436, 6.190871)); vertexData_.push_back(vec2(2.107884, 6.970954)); vertexData_.push_back(vec2(2.556017, 7.020747)); vertexData_.push_back(vec2(3.020747, 7.867220)); vertexData_.push_back(vec2(3.518672, 8.033195)); vertexData_.push_back(vec2(3.269710, 8.531120)); vertexData_.push_back(vec2(4.165975, 8.929461)); vertexData_.push_back(vec2(3.302905, 9.062241)); vertexData_.push_back(vec2(4.331950, 9.626556)); vertexData_.push_back(vec2(3.286307, 9.344398)); vertexData_.push_back(vec2(4.116183, 9.958507)); vertexData_.push_back(vec2(3.004149, 9.510373)); vertexData_.push_back(vec2(3.518672, 9.991701)); vertexData_.push_back(vec2(2.705394, 9.493776)); vertexData_.push_back(vec2(2.091286, 9.311203)); vertexData_.push_back(vec2(2.041494, 9.062241)); vertexData_.push_back(vec2(1.178423, 8.514523)); vertexData_.push_back(vec2(1.443983, 8.165976)); vertexData_.push_back(vec2(0.481328, 7.535270)); vertexData_.push_back(vec2(1.045643, 6.904564)); vertexData_.push_back(vec2(0.149378, 6.091286)); vertexData_.push_back(vec2(1.095436, 5.410789)); vertexData_.push_back(vec2(0.464730, 4.232365)); vertexData_.push_back(vec2(1.377593, 4.497925)); vertexData_.push_back(vec2(1.261411, 3.136930)); vertexData_.push_back(vec2(1.925311, 3.950207)); vertexData_.push_back(vec2(2.240664, 3.037344)); vertexData_.push_back(vec2(2.589212, 3.834025)); vertexData_.push_back(vec2(3.087137, 3.269710)); vertexData_.push_back(vec2(3.236515, 3.867220)); vertexData_.push_back(vec2(3.684647, 3.867220)); vertexData_.push_back(vec2(3.867220, 4.448133)); vertexData_.push_back(vec2(4.398340, 5.128631)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(31); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 33, curOffset)); curOffset += (33 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 33, curOffset)); } glmark2-2012.08/./src/scene-ideas/n.cc0000664000175000017500000001235212013417376016434 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'o' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterN::LetterN() { // Vertex data... vertexData_.push_back(vec2(1.009307, 9.444788)); vertexData_.push_back(vec2(2.548087, 9.742002)); vertexData_.push_back(vec2(1.737332, 9.213622)); vertexData_.push_back(vec2(2.994829, 9.659443)); vertexData_.push_back(vec2(1.985522, 8.751290)); vertexData_.push_back(vec2(3.127198, 9.180598)); vertexData_.push_back(vec2(1.935884, 7.975232)); vertexData_.push_back(vec2(2.481903, 6.571723)); vertexData_.push_back(vec2(1.472596, 5.019608)); vertexData_.push_back(vec2(1.439504, 2.988648)); vertexData_.push_back(vec2(1.025853, 2.988648)); vertexData_.push_back(vec2(2.283350, 6.059855)); vertexData_.push_back(vec2(2.035160, 5.366357)); vertexData_.push_back(vec2(3.292658, 7.711042)); vertexData_.push_back(vec2(3.540848, 7.744066)); vertexData_.push_back(vec2(4.384695, 9.031992)); vertexData_.push_back(vec2(4.699069, 8.916409)); vertexData_.push_back(vec2(5.609100, 9.808049)); vertexData_.push_back(vec2(5.145812, 8.982456)); vertexData_.push_back(vec2(6.155119, 9.791537)); vertexData_.push_back(vec2(5.410548, 8.635707)); vertexData_.push_back(vec2(6.337125, 9.312694)); vertexData_.push_back(vec2(5.360910, 7.991744)); vertexData_.push_back(vec2(6.088935, 8.090816)); vertexData_.push_back(vec2(4.947259, 5.977296)); vertexData_.push_back(vec2(5.261634, 4.804954)); vertexData_.push_back(vec2(4.616339, 4.028896)); vertexData_.push_back(vec2(5.211996, 3.962848)); vertexData_.push_back(vec2(4.732162, 3.318886)); vertexData_.push_back(vec2(5.559462, 3.814241)); vertexData_.push_back(vec2(5.228542, 3.038184)); vertexData_.push_back(vec2(5.940021, 3.814241)); vertexData_.push_back(vec2(5.906929, 3.335397)); vertexData_.push_back(vec2(6.684591, 4.094943)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(31); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 11, curOffset)); curOffset += (11 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 23, curOffset)); curOffset += (23 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 11, curOffset)); curOffset += (11 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 23, curOffset)); } glmark2-2012.08/./src/scene-ideas/splines.cc0000664000175000017500000001577112013417376017664 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "splines.h" using LibMatrix::vec3; void Spline::calcParams() { unsigned int numParams(controlData_.size() - 3); paramData_ = new param[numParams]; for (unsigned int i = 0; i < numParams; i++) { float x3(controlData_[i + 1].x()); float x2(controlData_[i + 2].x() - controlData_[i].x()); float x1(2.0 * controlData_[i].x() + -2.0 * controlData_[i+1].x() + 1.0 * controlData_[i+2].x() + -1.0 * controlData_[i+3].x()); float x0(-1.0 * controlData_[i].x() + 1.0 * controlData_[i+1].x() + -1.0 * controlData_[i+2].x() + 1.0 * controlData_[i+3].x()); float y3(controlData_[i + 1].y()); float y2(controlData_[i + 2].y() - controlData_[i].y()); float y1(2.0 * controlData_[i].y() + -2.0 * controlData_[i+1].y() + 1.0 * controlData_[i+2].y() + -1.0 * controlData_[i+3].y()); float y0(-1.0 * controlData_[i].y() + 1.0 * controlData_[i+1].y() + -1.0 * controlData_[i+2].y() + 1.0 * controlData_[i+3].y()); float z3(controlData_[i + 1].z()); float z2(controlData_[i + 2].z() - controlData_[i].z()); float z1(2.0 * controlData_[i].z() + -2.0 * controlData_[i+1].z() + 1.0 * controlData_[i+2].z() + -1.0 * controlData_[i+3].z()); float z0(-1.0 * controlData_[i].z() + 1.0 * controlData_[i+1].z() + -1.0 * controlData_[i+2].z() + 1.0 * controlData_[i+3].z()); paramData_[i][3].x(x3); paramData_[i][2].x(x2); paramData_[i][1].x(x1); paramData_[i][0].x(x0); paramData_[i][3].y(y3); paramData_[i][2].y(y2); paramData_[i][1].y(y1); paramData_[i][0].y(y0); paramData_[i][3].z(z3); paramData_[i][2].z(z2); paramData_[i][1].z(z1); paramData_[i][0].z(z0); } } void Spline::getCurrentVec(float currentTime, vec3& v) const { unsigned int integerTime(static_cast(currentTime)); float t(currentTime - static_cast(integerTime)); v.x(paramData_[integerTime][3].x() + paramData_[integerTime][2].x() * t + paramData_[integerTime][1].x() * t * t + paramData_[integerTime][0].x() * t * t * t); v.y(paramData_[integerTime][3].y() + paramData_[integerTime][2].y() * t + paramData_[integerTime][1].y() * t * t + paramData_[integerTime][0].y() * t * t * t); v.z(paramData_[integerTime][3].z() + paramData_[integerTime][2].z() * t + paramData_[integerTime][1].z() * t * t + paramData_[integerTime][0].z() * t * t * t); } ViewFromSpline::ViewFromSpline() { addControlPoint(vec3(-1.0, 1.0, -4.0)); addControlPoint(vec3(-1.0, -3.0, -4.0)); addControlPoint(vec3(-3.0, 1.0, -3.0)); addControlPoint(vec3(-1.8, 2.0, 5.4)); addControlPoint(vec3(-0.4, 2.0, 1.2)); addControlPoint(vec3(-0.2, 1.5, 0.6)); addControlPoint(vec3(-0.2, 1.2, 0.6)); addControlPoint(vec3(-0.8, 1.0, 2.4)); addControlPoint(vec3(-1.0, 2.0, 3.0)); addControlPoint(vec3(0.0, 4.0, 3.6)); addControlPoint(vec3(-0.8, 4.0, 1.2)); addControlPoint(vec3(-0.2, 3.0, 0.6)); addControlPoint(vec3(-0.1, 2.0, 0.3)); addControlPoint(vec3(-0.1, 2.0, 0.3)); addControlPoint(vec3(-0.1, 2.0, 0.3)); addControlPoint(vec3(-0.1, 2.0, 0.3)); calcParams(); } ViewToSpline::ViewToSpline() { addControlPoint(vec3(-1.0, 1.0, 0.0)); addControlPoint(vec3(-1.0, -3.0, 0.0)); addControlPoint(vec3(-1.0, 1.0, 0.0)); addControlPoint(vec3(0.1, 0.0, -0.3)); addControlPoint(vec3(0.1, 0.0, -0.3)); addControlPoint(vec3(0.1, 0.0, -0.3)); addControlPoint(vec3(0.0, 0.2, 0.0)); addControlPoint(vec3(0.0, 0.6, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); calcParams(); } LightPositionSpline::LightPositionSpline() { addControlPoint(vec3(0.0, 1.8, 0.0)); addControlPoint(vec3(0.0, 1.8, 0.0)); addControlPoint(vec3(0.0, 1.6, 0.0)); addControlPoint(vec3(0.0, 1.6, 0.0)); addControlPoint(vec3(0.0, 1.6, 0.0)); addControlPoint(vec3(0.0, 1.6, 0.0)); addControlPoint(vec3(0.0, 1.4, 0.0)); addControlPoint(vec3(0.0, 1.3, 0.0)); addControlPoint(vec3(-0.2, 1.5, 2.0)); addControlPoint(vec3(0.8, 1.5, -0.4)); addControlPoint(vec3(-0.8, 1.5, -0.4)); addControlPoint(vec3(0.8, 2.0, 1.0)); addControlPoint(vec3(1.8, 5.0, -1.8)); addControlPoint(vec3(8.0, 10.0, -4.0)); addControlPoint(vec3(8.0, 10.0, -4.0)); addControlPoint(vec3(8.0, 10.0, -4.0)); calcParams(); } LogoPositionSpline::LogoPositionSpline() { addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, -0.5, 0.0)); addControlPoint(vec3(0.0, 0.0, 0.0)); addControlPoint(vec3(0.0, 0.6, 0.0)); addControlPoint(vec3(0.0, 0.75, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.8, 0.0)); addControlPoint(vec3(0.0, 0.5, 0.0)); addControlPoint(vec3(0.0, 0.5, 0.0)); addControlPoint(vec3(0.0, 0.5, 0.0)); addControlPoint(vec3(0.0, 0.5, 0.0)); addControlPoint(vec3(0.0, 0.5, 0.0)); calcParams(); } LogoRotationSpline::LogoRotationSpline() { addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(0.0, 0.0, -18.4)); addControlPoint(vec3(240.0, 360.0, 180.0)); addControlPoint(vec3(90.0, 180.0, 90.0)); addControlPoint(vec3(11.9, 0.0, -18.4)); addControlPoint(vec3(11.9, 0.0, -18.4)); addControlPoint(vec3(11.9, 0.0, -18.4)); addControlPoint(vec3(11.9, 0.0, -18.4)); addControlPoint(vec3(11.9, 0.0, -18.4)); calcParams(); } glmark2-2012.08/./src/scene-ideas/a.cc0000664000175000017500000001470712013417376016425 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'a' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterA::LetterA() { // Vertex data... vertexData_.push_back(vec2(5.618949, 10.261048)); vertexData_.push_back(vec2(5.322348, 9.438848)); vertexData_.push_back(vec2(5.124614, 10.030832)); vertexData_.push_back(vec2(4.860968, 9.488181)); vertexData_.push_back(vec2(4.811534, 9.932169)); vertexData_.push_back(vec2(3.938208, 9.438848)); vertexData_.push_back(vec2(3.658084, 9.685509)); vertexData_.push_back(vec2(2.784758, 8.994862)); vertexData_.push_back(vec2(2.801236, 9.175745)); vertexData_.push_back(vec2(1.960865, 8.172662)); vertexData_.push_back(vec2(1.186406, 7.761562)); vertexData_.push_back(vec2(1.252317, 6.561151)); vertexData_.push_back(vec2(0.576725, 6.610483)); vertexData_.push_back(vec2(0.939238, 5.525180)); vertexData_.push_back(vec2(0.164779, 4.883864)); vertexData_.push_back(vec2(0.840371, 4.818089)); vertexData_.push_back(vec2(0.230690, 3.963001)); vertexData_.push_back(vec2(0.939238, 4.242549)); vertexData_.push_back(vec2(0.609681, 3.255909)); vertexData_.push_back(vec2(1.268795, 3.963001)); vertexData_.push_back(vec2(1.021627, 3.075026)); vertexData_.push_back(vec2(1.861998, 4.045221)); vertexData_.push_back(vec2(1.829042, 3.535457)); vertexData_.push_back(vec2(2.817714, 4.818089)); vertexData_.push_back(vec2(3.163749, 4.998972)); vertexData_.push_back(vec2(3.971164, 6.643371)); vertexData_.push_back(vec2(4.267765, 6.725591)); vertexData_.push_back(vec2(4.663234, 7.630010)); vertexData_.push_back(vec2(5.404737, 9.734840)); vertexData_.push_back(vec2(4.646756, 9.669065)); vertexData_.push_back(vec2(5.108136, 8.731757)); vertexData_.push_back(vec2(4.679712, 8.600205)); vertexData_.push_back(vec2(4.926879, 7.564234)); vertexData_.push_back(vec2(4.366632, 6.692703)); vertexData_.push_back(vec2(4.663234, 5.344296)); vertexData_.push_back(vec2(3.888774, 4.850976)); vertexData_.push_back(vec2(4.630278, 4.094553)); vertexData_.push_back(vec2(3.954686, 3.963001)); vertexData_.push_back(vec2(4.828012, 3.798561)); vertexData_.push_back(vec2(4.168898, 3.321686)); vertexData_.push_back(vec2(5.157569, 3.864337)); vertexData_.push_back(vec2(4.514933, 3.091470)); vertexData_.push_back(vec2(5.553038, 4.045221)); vertexData_.push_back(vec2(5.305870, 3.634121)); vertexData_.push_back(vec2(5.932029, 4.176773)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(34); indexData_.push_back(35); indexData_.push_back(36); indexData_.push_back(37); indexData_.push_back(38); indexData_.push_back(39); indexData_.push_back(40); indexData_.push_back(41); indexData_.push_back(42); indexData_.push_back(43); indexData_.push_back(44); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(34); indexData_.push_back(36); indexData_.push_back(38); indexData_.push_back(40); indexData_.push_back(42); indexData_.push_back(44); indexData_.push_back(43); indexData_.push_back(41); indexData_.push_back(39); indexData_.push_back(37); indexData_.push_back(35); indexData_.push_back(33); indexData_.push_back(31); indexData_.push_back(29); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 28, curOffset)); curOffset += (28 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 17, curOffset)); curOffset += (17 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 28, curOffset)); curOffset += (28 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 17, curOffset)); } glmark2-2012.08/./src/scene-ideas/lamp.h0000664000175000017500000000365512013417376017000 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #ifndef LAMP_H_ #define LAMP_H_ #include #include #include "vec.h" #include "stack.h" #include "gl-headers.h" #include "program.h" class Lamp { public: Lamp(); ~Lamp(); void init(); bool valid() const { return valid_; } void draw(LibMatrix::Stack4& modelview, LibMatrix::Stack4& projection, const LibMatrix::vec4* lightPositions); private: Program litProgram_; Program unlitProgram_; std::string litVertexShader_; std::string litFragmentShader_; std::string unlitVertexShader_; std::string unlitFragmentShader_; static const std::string modelviewName_; static const std::string projectionName_; static const std::string light0PositionName_; static const std::string light1PositionName_; static const std::string light2PositionName_; static const std::string vertexAttribName_; static const std::string normalAttribName_; static const std::string normalMatrixName_; std::vector vertexData_; std::vector indexData_; unsigned int bufferObjects_[2]; bool valid_; }; #endif // LAMP_H_ glmark2-2012.08/./src/scene-ideas/i.cc0000664000175000017500000001021112013417376016417 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'i' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterI::LetterI() { // Vertex data... vertexData_.push_back(vec2(0.548767, 9.414791)); vertexData_.push_back(vec2(2.795284, 9.757771)); vertexData_.push_back(vec2(1.457663, 9.311897)); vertexData_.push_back(vec2(2.503751, 9.157557)); vertexData_.push_back(vec2(1.714898, 8.986067)); vertexData_.push_back(vec2(2.109325, 7.785638)); vertexData_.push_back(vec2(1.286174, 7.013934)); vertexData_.push_back(vec2(1.800643, 6.070740)); vertexData_.push_back(vec2(0.994641, 5.161843)); vertexData_.push_back(vec2(1.783494, 4.767417)); vertexData_.push_back(vec2(0.943194, 4.167202)); vertexData_.push_back(vec2(1.852090, 4.304394)); vertexData_.push_back(vec2(1.063237, 3.549839)); vertexData_.push_back(vec2(2.023580, 3.978564)); vertexData_.push_back(vec2(1.406217, 3.172562)); vertexData_.push_back(vec2(2.315113, 3.875670)); vertexData_.push_back(vec2(2.006431, 3.018221)); vertexData_.push_back(vec2(2.812433, 3.944266)); vertexData_.push_back(vec2(2.726688, 3.429796)); vertexData_.push_back(vec2(3.258307, 4.132905)); vertexData_.push_back(vec2(1.989282, 10.923902)); vertexData_.push_back(vec2(2.778135, 12.295820)); vertexData_.push_back(vec2(2.966774, 11.678456)); vertexData_.push_back(vec2(3.687031, 12.947481)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(21); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 20, curOffset)); curOffset += (20 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 4, curOffset)); curOffset += (4 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 20, curOffset)); curOffset += (20 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 4, curOffset)); } glmark2-2012.08/./src/scene-ideas/m.cc0000664000175000017500000001725612013417376016443 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'm' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterM::LetterM() { // Vertex data... vertexData_.push_back(vec2(0.590769, 9.449335)); vertexData_.push_back(vec2(2.116923, 9.842375)); vertexData_.push_back(vec2(1.362051, 9.383828)); vertexData_.push_back(vec2(2.527179, 9.825998)); vertexData_.push_back(vec2(1.591795, 9.072672)); vertexData_.push_back(vec2(2.789744, 9.514841)); vertexData_.push_back(vec2(1.690256, 8.663255)); vertexData_.push_back(vec2(2.658462, 8.335722)); vertexData_.push_back(vec2(1.575385, 7.222108)); vertexData_.push_back(vec2(2.067692, 6.255886)); vertexData_.push_back(vec2(0.918974, 4.028659)); vertexData_.push_back(vec2(1.050256, 3.013306)); vertexData_.push_back(vec2(0.705641, 3.013306)); vertexData_.push_back(vec2(2.018461, 6.386899)); vertexData_.push_back(vec2(1.788718, 5.617196)); vertexData_.push_back(vec2(2.921026, 7.991812)); vertexData_.push_back(vec2(3.167180, 8.008188)); vertexData_.push_back(vec2(3.544615, 8.827022)); vertexData_.push_back(vec2(3.872821, 8.843398)); vertexData_.push_back(vec2(4.414359, 9.547595)); vertexData_.push_back(vec2(4.447179, 9.056294)); vertexData_.push_back(vec2(5.120000, 9.891504)); vertexData_.push_back(vec2(4.841026, 8.843398)); vertexData_.push_back(vec2(5.825641, 9.809621)); vertexData_.push_back(vec2(5.005128, 8.040941)); vertexData_.push_back(vec2(5.989744, 8.761515)); vertexData_.push_back(vec2(4.906667, 6.714432)); vertexData_.push_back(vec2(5.595897, 7.123848)); vertexData_.push_back(vec2(3.987692, 2.996929)); vertexData_.push_back(vec2(4.348718, 2.996929)); vertexData_.push_back(vec2(5.218462, 5.977482)); vertexData_.push_back(vec2(5.251282, 6.354146)); vertexData_.push_back(vec2(6.449231, 7.893552)); vertexData_.push_back(vec2(6.400000, 8.221085)); vertexData_.push_back(vec2(7.302564, 8.843398)); vertexData_.push_back(vec2(7.351795, 9.334698)); vertexData_.push_back(vec2(7.827693, 9.154554)); vertexData_.push_back(vec2(8.008205, 9.842375)); vertexData_.push_back(vec2(8.139487, 9.121801)); vertexData_.push_back(vec2(8.795897, 9.973388)); vertexData_.push_back(vec2(8.402051, 8.728762)); vertexData_.push_back(vec2(9.337436, 9.531218)); vertexData_.push_back(vec2(8.402051, 8.040941)); vertexData_.push_back(vec2(9.288205, 8.433982)); vertexData_.push_back(vec2(7.745641, 5.813715)); vertexData_.push_back(vec2(8.320000, 5.928352)); vertexData_.push_back(vec2(7.286154, 4.012282)); vertexData_.push_back(vec2(7.991795, 4.126919)); vertexData_.push_back(vec2(7.499487, 3.357216)); vertexData_.push_back(vec2(8.533334, 3.766633)); vertexData_.push_back(vec2(8.123077, 3.062436)); vertexData_.push_back(vec2(8.927179, 3.832139)); vertexData_.push_back(vec2(8.910769, 3.340839)); vertexData_.push_back(vec2(9.550769, 4.126919)); // Index data... indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(34); indexData_.push_back(36); indexData_.push_back(38); indexData_.push_back(40); indexData_.push_back(42); indexData_.push_back(44); indexData_.push_back(46); indexData_.push_back(48); indexData_.push_back(50); indexData_.push_back(52); indexData_.push_back(53); indexData_.push_back(51); indexData_.push_back(49); indexData_.push_back(47); indexData_.push_back(45); indexData_.push_back(43); indexData_.push_back(41); indexData_.push_back(39); indexData_.push_back(37); indexData_.push_back(35); indexData_.push_back(33); indexData_.push_back(31); indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(34); indexData_.push_back(35); indexData_.push_back(36); indexData_.push_back(37); indexData_.push_back(38); indexData_.push_back(39); indexData_.push_back(40); indexData_.push_back(41); indexData_.push_back(42); indexData_.push_back(43); indexData_.push_back(44); indexData_.push_back(45); indexData_.push_back(46); indexData_.push_back(47); indexData_.push_back(48); indexData_.push_back(49); indexData_.push_back(50); indexData_.push_back(51); indexData_.push_back(52); indexData_.push_back(53); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 13, curOffset)); curOffset += (13 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 17, curOffset)); curOffset += (17 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 24, curOffset)); curOffset += (24 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 13, curOffset)); curOffset += (13 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 17, curOffset)); curOffset += (17 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 24, curOffset)); } glmark2-2012.08/./src/scene-ideas/t.cc0000664000175000017500000001254512013417376016446 0ustar alfalf00000000000000/* * Vertex position data describing the letter 't' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterT::LetterT() { // Vertex data... vertexData_.push_back(vec2(2.986667, 14.034801)); vertexData_.push_back(vec2(2.445128, 10.088024)); vertexData_.push_back(vec2(1.788718, 9.236438)); vertexData_.push_back(vec2(2.264615, 7.664279)); vertexData_.push_back(vec2(1.165128, 5.666326)); vertexData_.push_back(vec2(2.034872, 4.945752)); vertexData_.push_back(vec2(1.132308, 3.766633)); vertexData_.push_back(vec2(2.182564, 3.570113)); vertexData_.push_back(vec2(1.411282, 2.309109)); vertexData_.push_back(vec2(2.510769, 2.341863)); vertexData_.push_back(vec2(2.149744, 1.048106)); vertexData_.push_back(vec2(3.364103, 1.375640)); vertexData_.push_back(vec2(3.167180, 0.327533)); vertexData_.push_back(vec2(4.381538, 0.736950)); vertexData_.push_back(vec2(5.005128, 0.032753)); vertexData_.push_back(vec2(5.612308, 0.638690)); vertexData_.push_back(vec2(6.235898, 0.540430)); vertexData_.push_back(vec2(7.187692, 1.162743)); vertexData_.push_back(vec2(1.985641, 9.039918)); vertexData_.push_back(vec2(2.133333, 10.186285)); vertexData_.push_back(vec2(1.509744, 9.023541)); vertexData_.push_back(vec2(1.608205, 9.662231)); vertexData_.push_back(vec2(1.050256, 9.023541)); vertexData_.push_back(vec2(1.050256, 9.334698)); vertexData_.push_back(vec2(0.196923, 9.007165)); vertexData_.push_back(vec2(2.363077, 9.711361)); vertexData_.push_back(vec2(2.264615, 9.023541)); vertexData_.push_back(vec2(3.282051, 9.563972)); vertexData_.push_back(vec2(3.446154, 9.023541)); vertexData_.push_back(vec2(4.069744, 9.531218)); vertexData_.push_back(vec2(4.299487, 9.236438)); vertexData_.push_back(vec2(4.644103, 9.613101)); vertexData_.push_back(vec2(5.251282, 9.875128)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(31); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 18, curOffset)); curOffset += (18 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 7, curOffset)); curOffset += (7 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 8, curOffset)); curOffset += (8 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 18, curOffset)); curOffset += (18 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 7, curOffset)); curOffset += (7 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 8, curOffset)); } glmark2-2012.08/./src/scene-ideas/table.cc0000664000175000017500000003151212013417376017265 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "table.h" #include "scene.h" #include "shader-source.h" #include "log.h" using std::string; using LibMatrix::vec3; using LibMatrix::Stack4; const string Table::modelviewName_("modelview"); const string Table::projectionName_("projection"); const string Table::lightPositionName_("lightPosition"); const string Table::logoDirectionName_("logoDirection"); const string Table::curTimeName_("currentTime"); const string Table::vertexAttribName_("vertex"); const unsigned int Table::TABLERES_(12); const vec3 Table::paperVertices_[4] = { vec3(-0.8, 0.0, 0.4), vec3(-0.2, 0.0, -1.4), vec3(0.4, 0.0, 0.8), vec3(1.0, 0.0, -1.0), }; Table::Table() : tableVertexIndex_(0), paperVertexIndex_(0), textVertexIndex_(0), underVertexIndex_(0), valid_(false) { tableVertices_.reserve((TABLERES_ + 1) * (TABLERES_ + 1)); for (unsigned int i = 0; i <= TABLERES_; i++) { for (unsigned int j = 0; j <= TABLERES_; j++) { float x((static_cast(i) - static_cast(TABLERES_) * 1.0 / 2.0) / 2.0); float z((static_cast(j) - static_cast(TABLERES_) * 1.0 / 2.0) / 2.0); tableVertices_.push_back(vec3(x, 0.0, z)); } } // Now that we've setup the vertex data, we can setup the map of how // that data will be laid out in the buffer object. dataMap_.tvOffset = 0; dataMap_.tvSize = tableVertices_.size() * sizeof(vec3); dataMap_.totalSize = dataMap_.tvSize; dataMap_.pvOffset = dataMap_.tvOffset + dataMap_.tvSize; dataMap_.pvSize = 4 * sizeof(vec3); dataMap_.totalSize += dataMap_.pvSize; for (unsigned int i = 0; i < TABLERES_; i++) { for (unsigned int j = 0; j <= TABLERES_; j++) { unsigned int curIndex1(i * (TABLERES_ + 1) + j); unsigned int curIndex2((i + 1) * (TABLERES_ + 1) + j); indexData_.push_back(curIndex1); indexData_.push_back(curIndex2); } } } Table::~Table(void) { if (valid_) { glDeleteBuffers(2, &bufferObjects_[0]); } } void Table::init(void) { // Make sure we don't re-initialize... if (valid_) { return; } // Initialize shader sources from input files and create programs from them // Program to render the table with lighting and a time-based fade... string table_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-table.vert"); string table_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-table.frag"); ShaderSource table_vtx_source(table_vtx_filename); ShaderSource table_frg_source(table_frg_filename); if (!Scene::load_shaders_from_strings(tableProgram_, table_vtx_source.str(), table_frg_source.str())) { Log::error("No valid program for table rendering.\n"); return; } textVertexIndex_ = tableProgram_[vertexAttribName_].location(); // Program to render the paper with lighting and a time-based fade... string paper_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-paper.vert"); string paper_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-paper.frag"); ShaderSource paper_vtx_source(paper_vtx_filename); ShaderSource paper_frg_source(paper_frg_filename); if (!Scene::load_shaders_from_strings(paperProgram_, paper_vtx_source.str(), paper_frg_source.str())) { Log::error("No valid program for paper rendering.\n"); return; } paperVertexIndex_ = paperProgram_[vertexAttribName_].location(); // Program to handle the text (time-based color fade)... string text_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-text.vert"); string text_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-text.frag"); ShaderSource text_vtx_source(text_vtx_filename); ShaderSource text_frg_source(text_frg_filename); if (!Scene::load_shaders_from_strings(textProgram_, text_vtx_source.str(), text_frg_source.str())) { Log::error("No valid program for text rendering.\n"); return; } textVertexIndex_ = textProgram_[vertexAttribName_].location(); // Program for the drawUnder functionality (just paint it black)... string under_table_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-under-table.vert"); string under_table_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-under-table.frag"); ShaderSource under_table_vtx_source(under_table_vtx_filename); ShaderSource under_table_frg_source(under_table_frg_filename); if (!Scene::load_shaders_from_strings(underProgram_, under_table_vtx_source.str(), under_table_frg_source.str())) { Log::error("No valid program for under table rendering.\n"); return; } underVertexIndex_ = underProgram_[vertexAttribName_].location(); // Tell all of the characters to initialize themselves... i_.init(textVertexIndex_); d_.init(textVertexIndex_); e_.init(textVertexIndex_); a_.init(textVertexIndex_); s_.init(textVertexIndex_); n_.init(textVertexIndex_); m_.init(textVertexIndex_); o_.init(textVertexIndex_); t_.init(textVertexIndex_); // We need 2 buffers for our work here. One for the vertex data. // and one for the index data. glGenBuffers(2, &bufferObjects_[0]); // First, setup the vertex data by binding the first buffer object, // allocating its data store, and filling it in with our vertex data. glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBufferData(GL_ARRAY_BUFFER, dataMap_.totalSize, 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.tvOffset, dataMap_.tvSize, &tableVertices_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.pvOffset, dataMap_.pvSize, &paperVertices_[0]); // Now repeat for our index data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData_.size() * sizeof(unsigned short), &indexData_.front(), GL_STATIC_DRAW); // We're ready to go. valid_ = true; } void Table::draw(Stack4& modelview, Stack4& projection, const vec3& lightPos, const vec3& logoPos, const float& currentTime, float& paperAlpha_out) { glDisable(GL_DEPTH_TEST); // Compute the light direction with respect to the logo... vec3 logoDirection(lightPos.x() - logoPos.x(), lightPos.y() - logoPos.y(), lightPos.z() - logoPos.z()); logoDirection.normalize(); // Compute the alpha component based upon drawing the paper (all of this will // be done in the shader, but we need to pass this back so that the logo's // shadow will look right). for (unsigned int i = 0; i < 4; i++) { vec3 lightDirection(lightPos.x() - paperVertices_[i].x(), lightPos.y() - paperVertices_[i].y(), lightPos.z() - paperVertices_[i].z()); lightDirection.normalize(); float c = vec3::dot(lightDirection, logoDirection); if (c < 0.0) { c = 0.0; } c = c * c * c * lightDirection.y(); if ((currentTime > 10.0) && (currentTime < 12.0)) { c *= 1.0 - (currentTime - 10.0) * 0.5; } paperAlpha_out += c; } glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); // Draw the table top tableProgram_.start(); tableProgram_[projectionName_] = projection.getCurrent(); tableProgram_[modelviewName_] = modelview.getCurrent(); tableProgram_[lightPositionName_] = lightPos; tableProgram_[logoDirectionName_] = logoDirection; tableProgram_[curTimeName_] = currentTime; glVertexAttribPointer(tableVertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.tvOffset)); glEnableVertexAttribArray(tableVertexIndex_); static const unsigned int twiceRes(2 * (TABLERES_ + 1)); for (unsigned int i = 0; i < TABLERES_; i++) { glDrawElements(GL_TRIANGLE_STRIP, twiceRes, GL_UNSIGNED_SHORT, reinterpret_cast(i * twiceRes * sizeof(unsigned short))); } glDisableVertexAttribArray(tableVertexIndex_); tableProgram_.stop(); if (logoPos.y() > -0.33 && logoPos.y() < 0.33) { glEnable(GL_DEPTH_TEST); } // Draw the paper lying on the table top paperProgram_.start(); paperProgram_[projectionName_] = projection.getCurrent(); paperProgram_[modelviewName_] = modelview.getCurrent(); paperProgram_[lightPositionName_] = lightPos; paperProgram_[logoDirectionName_] = logoDirection; paperProgram_[curTimeName_] = currentTime; glVertexAttribPointer(paperVertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.pvOffset)); glEnableVertexAttribArray(paperVertexIndex_); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(paperVertexIndex_); paperProgram_.stop(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); modelview.push(); modelview.rotate(-18.4, 0.0, 1.0, 0.0); modelview.translate(-0.3, 0.0, -0.8); modelview.rotate(-90.0, 1.0, 0.0, 0.0); modelview.scale(0.015, 0.015, 0.015); // Draw the text on the paper lying on the table top. // Each character has its own array and element buffers, and they have // been initialized with the vertex attrib location for this program. textProgram_.start(); textProgram_[projectionName_] = projection.getCurrent(); textProgram_[modelviewName_] = modelview.getCurrent(); textProgram_[curTimeName_] = currentTime; i_.draw(); modelview.translate(3.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); d_.draw(); modelview.translate(6.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); e_.draw(); modelview.translate(5.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); a_.draw(); modelview.translate(6.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); s_.draw(); modelview.translate(10.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); i_.draw(); modelview.translate(3.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); n_.draw(); modelview.translate(-31.0, -13.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); m_.draw(); modelview.translate(10.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); o_.draw(); modelview.translate(5.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); t_.draw(); modelview.translate(4.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); i_.draw(); modelview.translate(3.5, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); o_.draw(); modelview.translate(5.0, 0.0, 0.0); textProgram_[modelviewName_] = modelview.getCurrent(); n_.draw(); textProgram_.stop(); modelview.pop(); } void Table::drawUnder(Stack4& modelview, Stack4& projection) { glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); underProgram_.start(); underProgram_[modelviewName_] = modelview.getCurrent(); underProgram_[projectionName_] = projection.getCurrent(); glVertexAttribPointer(underVertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.tvOffset)); glEnableVertexAttribArray(underVertexIndex_); static const unsigned int twiceRes(2 * (TABLERES_ + 1)); for (unsigned int i = 0; i < TABLERES_; i++) { glDrawElements(GL_TRIANGLE_STRIP, twiceRes, GL_UNSIGNED_SHORT, reinterpret_cast(i * twiceRes * sizeof(unsigned short))); } glDisableVertexAttribArray(underVertexIndex_); underProgram_.stop(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glEnable(GL_DEPTH_TEST); } glmark2-2012.08/./src/scene-ideas/table.h0000664000175000017500000000534612013417376017135 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #ifndef TABLE_H_ #define TABLE_H_ #include #include #include "characters.h" #include "program.h" #include "stack.h" class Table { public: Table(); ~Table(); void init(); bool valid() const { return valid_; } void draw(LibMatrix::Stack4& modelview, LibMatrix::Stack4& projection, const LibMatrix::vec3& lightPosition, const LibMatrix::vec3& logoPosition, const float& currentTime, float& paperAlpha_out); void drawUnder(LibMatrix::Stack4& modelview, LibMatrix::Stack4& projection); private: // Text LetterI i_; LetterD d_; LetterE e_; LetterA a_; LetterS s_; LetterN n_; LetterM m_; LetterO o_; LetterT t_; Program tableProgram_; Program paperProgram_; Program textProgram_; Program underProgram_; std::string tableVertexShader_; std::string tableFragmentShader_; std::string paperVertexShader_; std::string paperFragmentShader_; std::string textVertexShader_; std::string textFragmentShader_; std::string underVertexShader_; std::string underFragmentShader_; static const std::string modelviewName_; static const std::string projectionName_; static const std::string lightPositionName_; static const std::string logoDirectionName_; static const std::string curTimeName_; static const std::string vertexAttribName_; static const unsigned int TABLERES_; std::vector tableVertices_; static const LibMatrix::vec3 paperVertices_[4]; struct VertexDataMap { unsigned int tvOffset; unsigned int tvSize; unsigned int pvOffset; unsigned int pvSize; unsigned int totalSize; } dataMap_; unsigned int bufferObjects_[2]; std::vector indexData_; int tableVertexIndex_; int paperVertexIndex_; int textVertexIndex_; int underVertexIndex_; bool valid_; }; #endif // TABLE_H_ glmark2-2012.08/./src/scene-ideas/d.cc0000664000175000017500000001200412013417376016414 0ustar alfalf00000000000000/* * Vertex position data describing the letter 'd' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterD::LetterD() { // Vertex data... vertexData_.push_back(vec2(4.714579, 9.987679)); vertexData_.push_back(vec2(2.841889, 9.429158)); vertexData_.push_back(vec2(2.825462, 9.166325)); vertexData_.push_back(vec2(1.856263, 8.722793)); vertexData_.push_back(vec2(2.004107, 8.000000)); vertexData_.push_back(vec2(0.969199, 7.605750)); vertexData_.push_back(vec2(1.494866, 6.636550)); vertexData_.push_back(vec2(0.607803, 6.028748)); vertexData_.push_back(vec2(1.527721, 4.960986)); vertexData_.push_back(vec2(0.772074, 4.254620)); vertexData_.push_back(vec2(1.774127, 4.139630)); vertexData_.push_back(vec2(1.445585, 3.186858)); vertexData_.push_back(vec2(2.266940, 3.843942)); vertexData_.push_back(vec2(2.250513, 3.022587)); vertexData_.push_back(vec2(2.776181, 3.843942)); vertexData_.push_back(vec2(3.137577, 3.383984)); vertexData_.push_back(vec2(3.351129, 4.008214)); vertexData_.push_back(vec2(3.909651, 4.451746)); vertexData_.push_back(vec2(4.090349, 4.960986)); vertexData_.push_back(vec2(4.862423, 5.946612)); vertexData_.push_back(vec2(4.763860, 6.652977)); vertexData_.push_back(vec2(5.388090, 7.572895)); vertexData_.push_back(vec2(4.862423, 8.492813)); vertexData_.push_back(vec2(5.618070, 9.921971)); vertexData_.push_back(vec2(4.698152, 10.940452)); vertexData_.push_back(vec2(5.338809, 12.303902)); vertexData_.push_back(vec2(4.238193, 12.960985)); vertexData_.push_back(vec2(4.451746, 14.554415)); vertexData_.push_back(vec2(3.581109, 14.291581)); vertexData_.push_back(vec2(3.613963, 15.342916)); vertexData_.push_back(vec2(2.677618, 15.145790)); vertexData_.push_back(vec2(2.480493, 15.540041)); vertexData_.push_back(vec2(2.036961, 15.211499)); vertexData_.push_back(vec2(1.281314, 15.112936)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(30); indexData_.push_back(31); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(30); indexData_.push_back(32); indexData_.push_back(33); indexData_.push_back(31); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 34, curOffset)); curOffset += (34 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 34, curOffset)); } glmark2-2012.08/./src/scene-ideas/splines.h0000664000175000017500000000350612013417376017517 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #ifndef SPLINES_H_ #define SPLINES_H_ #include #include "vec.h" class Spline { public: Spline() : paramData_(0) {} ~Spline() { delete [] paramData_; } void addControlPoint(const LibMatrix::vec3& point) { controlData_.push_back(point); } void getCurrentVec(float currentTime, LibMatrix::vec3& v) const; void calcParams(); private: std::vector controlData_; typedef LibMatrix::vec3 param[4]; param* paramData_; }; class ViewFromSpline : public Spline { public: ViewFromSpline(); ~ViewFromSpline() {} }; class ViewToSpline : public Spline { public: ViewToSpline(); ~ViewToSpline() {} }; class LightPositionSpline : public Spline { public: LightPositionSpline(); ~LightPositionSpline() {} }; class LogoPositionSpline : public Spline { public: LogoPositionSpline(); ~LogoPositionSpline() {} }; class LogoRotationSpline : public Spline { public: LogoRotationSpline(); ~LogoRotationSpline() {} }; #endif // SPLINES_H_ glmark2-2012.08/./src/scene-ideas/logo.cc0000664000175000017500000011142712013417376017142 0ustar alfalf00000000000000/* * Vertex position data describing the old Silicon Graphics logo * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "logo.h" #include "scene.h" #include "shader-source.h" #include "log.h" using std::string; using LibMatrix::vec3; using LibMatrix::uvec3; using LibMatrix::vec4; using LibMatrix::Stack4; using LibMatrix::mat4; const unsigned int SGILogo::textureResolution_(32); const string SGILogo::modelviewName_("modelview"); const string SGILogo::projectionName_("projection"); const string SGILogo::lightPositionName_("light0Position"); const string SGILogo::logoColorName_("logoColor"); const string SGILogo::vertexAttribName_("vertex"); const string SGILogo::normalAttribName_("normal"); const string SGILogo::normalMatrixName_("normalMatrix"); SGILogo::SGILogo(void) : normalNormalIndex_(0), normalVertexIndex_(0), flatVertexIndex_(0), shadowVertexIndex_(0), vertexIndex_(0), valid_(false), drawStyle_(LOGO_NORMAL) { // Single cylinder data... singleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); singleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 5.000000)); singleCylinderVertices_.push_back(vec3(0.707107, 0.707107, 0.000000)); singleCylinderVertices_.push_back(vec3(0.707107, 0.707107, 5.000000)); singleCylinderVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); singleCylinderVertices_.push_back(vec3(0.000000, 1.000000, 5.000000)); singleCylinderVertices_.push_back(vec3(-0.707107, 0.707107, 0.000000)); singleCylinderVertices_.push_back(vec3(-0.707107, 0.707107, 5.000000)); singleCylinderVertices_.push_back(vec3(-1.000000, 0.000000, 0.000000)); singleCylinderVertices_.push_back(vec3(-1.000000, 0.000000, 5.000000)); singleCylinderVertices_.push_back(vec3(-0.707107, -0.707107, 0.000000)); singleCylinderVertices_.push_back(vec3(-0.707107, -0.707107, 5.000000)); singleCylinderVertices_.push_back(vec3(0.000000, -1.000000, 0.000000)); singleCylinderVertices_.push_back(vec3(0.000000, -1.000000, 5.000000)); singleCylinderVertices_.push_back(vec3(0.707107, -0.707107, 0.000000)); singleCylinderVertices_.push_back(vec3(0.707107, -0.707107, 5.000000)); singleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); singleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 5.000000)); singleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(0.707107, 0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(0.707107, 0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(0.000000, 1.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(0.000000, 1.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(-0.707107, 0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(-0.707107, 0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(-0.707107, -0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(-0.707107, -0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(0.000000, -1.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(0.000000, -1.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(0.707107, -0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(0.707107, -0.707107, 0.000000)); singleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); singleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); // Double cylinder data... doubleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); doubleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 7.000000)); doubleCylinderVertices_.push_back(vec3(0.707107, 0.707107, 0.000000)); doubleCylinderVertices_.push_back(vec3(0.707107, 0.707107, 7.000000)); doubleCylinderVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); doubleCylinderVertices_.push_back(vec3(0.000000, 1.000000, 7.000000)); doubleCylinderVertices_.push_back(vec3(-0.707107, 0.707107, 0.000000)); doubleCylinderVertices_.push_back(vec3(-0.707107, 0.707107, 7.000000)); doubleCylinderVertices_.push_back(vec3(-1.000000, 0.000000, 0.000000)); doubleCylinderVertices_.push_back(vec3(-1.000000, 0.000000, 7.000000)); doubleCylinderVertices_.push_back(vec3(-0.707107, -0.707107, 0.000000)); doubleCylinderVertices_.push_back(vec3(-0.707107, -0.707107, 7.000000)); doubleCylinderVertices_.push_back(vec3(0.000000, -1.000000, 0.000000)); doubleCylinderVertices_.push_back(vec3(0.000000, -1.000000, 7.000000)); doubleCylinderVertices_.push_back(vec3(0.707107, -0.707107, 0.000000)); doubleCylinderVertices_.push_back(vec3(0.707107, -0.707107, 7.000000)); doubleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); doubleCylinderVertices_.push_back(vec3(1.000000, 0.000000, 7.000000)); doubleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.707107, 0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.707107, 0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.000000, 1.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.000000, 1.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(-0.707107, 0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(-0.707107, 0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(-0.707107, -0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(-0.707107, -0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.000000, -1.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.000000, -1.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.707107, -0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(0.707107, -0.707107, 0.000000)); doubleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); doubleCylinderNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); // Elbow data... elbowVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowVertices_.push_back(vec3(0.707107, 0.707107, 0.000000)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.707107, 0.000000)); elbowVertices_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, -0.707107, 0.000000)); elbowVertices_.push_back(vec3(0.000000, -1.000000, 0.000000)); elbowVertices_.push_back(vec3(0.707107, -0.707107, 0.000000)); elbowVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowVertices_.push_back(vec3(1.000000, 0.034074, 0.258819)); elbowVertices_.push_back(vec3(0.707107, 0.717087, 0.075806)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.717087, 0.075806)); elbowVertices_.push_back(vec3(-1.000000, 0.034074, 0.258819)); elbowVertices_.push_back(vec3(-0.707107, -0.648939, 0.441832)); elbowVertices_.push_back(vec3(0.000000, -0.931852, 0.517638)); elbowVertices_.push_back(vec3(0.707107, -0.648939, 0.441832)); elbowVertices_.push_back(vec3(1.000000, 0.034074, 0.258819)); elbowVertices_.push_back(vec3(1.000000, 0.133975, 0.500000)); elbowVertices_.push_back(vec3(0.707107, 0.746347, 0.146447)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.746347, 0.146447)); elbowVertices_.push_back(vec3(-1.000000, 0.133975, 0.500000)); elbowVertices_.push_back(vec3(-0.707107, -0.478398, 0.853553)); elbowVertices_.push_back(vec3(0.000000, -0.732051, 1.000000)); elbowVertices_.push_back(vec3(0.707107, -0.478398, 0.853553)); elbowVertices_.push_back(vec3(1.000000, 0.133975, 0.500000)); elbowVertices_.push_back(vec3(1.000000, 0.292893, 0.707107)); elbowVertices_.push_back(vec3(0.707107, 0.792893, 0.207107)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.792893, 0.207107)); elbowVertices_.push_back(vec3(-1.000000, 0.292893, 0.707107)); elbowVertices_.push_back(vec3(-0.707107, -0.207107, 1.207107)); elbowVertices_.push_back(vec3(0.000000, -0.414214, 1.414214)); elbowVertices_.push_back(vec3(0.707107, -0.207107, 1.207107)); elbowVertices_.push_back(vec3(1.000000, 0.292893, 0.707107)); elbowVertices_.push_back(vec3(1.000000, 0.500000, 0.866025)); elbowVertices_.push_back(vec3(0.707107, 0.853553, 0.253653)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.853553, 0.253653)); elbowVertices_.push_back(vec3(-1.000000, 0.500000, 0.866025)); elbowVertices_.push_back(vec3(-0.707107, 0.146447, 1.478398)); elbowVertices_.push_back(vec3(0.000000, 0.000000, 1.732051)); elbowVertices_.push_back(vec3(0.707107, 0.146447, 1.478398)); elbowVertices_.push_back(vec3(1.000000, 0.500000, 0.866025)); elbowVertices_.push_back(vec3(1.000000, 0.741181, 0.965926)); elbowVertices_.push_back(vec3(0.707107, 0.924194, 0.282913)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 0.924194, 0.282913)); elbowVertices_.push_back(vec3(-1.000000, 0.741181, 0.965926)); elbowVertices_.push_back(vec3(-0.707107, 0.558168, 1.648939)); elbowVertices_.push_back(vec3(0.000000, 0.482362, 1.931852)); elbowVertices_.push_back(vec3(0.707107, 0.558168, 1.648939)); elbowVertices_.push_back(vec3(1.000000, 0.741181, 0.965926)); elbowVertices_.push_back(vec3(1.000000, 1.000000, 1.000000)); elbowVertices_.push_back(vec3(0.707107, 1.000000, 0.292893)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowVertices_.push_back(vec3(-0.707107, 1.000000, 0.292893)); elbowVertices_.push_back(vec3(-1.000000, 1.000000, 1.000000)); elbowVertices_.push_back(vec3(-0.707107, 1.000000, 1.707107)); elbowVertices_.push_back(vec3(0.000000, 1.000000, 2.000000)); elbowVertices_.push_back(vec3(0.707107, 1.000000, 1.707107)); elbowVertices_.push_back(vec3(1.000000, 1.000000, 1.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.707107, 0.000000)); elbowNormals_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, 0.707107, 0.000000)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.707107, 0.000000)); elbowNormals_.push_back(vec3(0.000000, -1.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, -0.707107, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.683013, -0.183013)); elbowNormals_.push_back(vec3(0.000000, 0.965926, -0.258819)); elbowNormals_.push_back(vec3(-0.707107, 0.683013, -0.183013)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.683013, 0.183013)); elbowNormals_.push_back(vec3(0.000000, -0.965926, 0.258819)); elbowNormals_.push_back(vec3(0.707107, -0.683013, 0.183013)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.612372, -0.353553)); elbowNormals_.push_back(vec3(0.000000, 0.866025, -0.500000)); elbowNormals_.push_back(vec3(-0.707107, 0.612372, -0.353553)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.612372, 0.353553)); elbowNormals_.push_back(vec3(0.000000, -0.866025, 0.500000)); elbowNormals_.push_back(vec3(0.707107, -0.612372, 0.353553)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.500000, -0.500000)); elbowNormals_.push_back(vec3(0.000000, 0.707107, -0.707107)); elbowNormals_.push_back(vec3(-0.707107, 0.500000, -0.500000)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.500000, 0.500000)); elbowNormals_.push_back(vec3(0.000000, -0.707107, 0.707107)); elbowNormals_.push_back(vec3(0.707107, -0.500000, 0.500000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.353553, -0.612372)); elbowNormals_.push_back(vec3(0.000000, 0.500000, -0.866025)); elbowNormals_.push_back(vec3(-0.707107, 0.353553, -0.612372)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.353553, 0.612372)); elbowNormals_.push_back(vec3(0.000000, -0.500000, 0.866025)); elbowNormals_.push_back(vec3(0.707107, -0.353553, 0.612372)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.183013, -0.683013)); elbowNormals_.push_back(vec3(0.000000, 0.258819, -0.965926)); elbowNormals_.push_back(vec3(-0.707107, 0.183013, -0.683013)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, -0.183013, 0.683013)); elbowNormals_.push_back(vec3(0.000000, -0.258819, 0.965926)); elbowNormals_.push_back(vec3(0.707107, -0.183013, 0.683013)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(0.707107, 0.000000, -0.707107)); elbowNormals_.push_back(vec3(0.000000, 0.000000, -1.000000)); elbowNormals_.push_back(vec3(-0.707107, 0.000000, -0.707107)); elbowNormals_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowNormals_.push_back(vec3(-0.707107, 0.000000, 0.707107)); elbowNormals_.push_back(vec3(0.000000, 0.000000, 1.000000)); elbowNormals_.push_back(vec3(0.707107, 0.000000, 0.707107)); elbowNormals_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(0.707107, 0.707107, 0.000000)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.707107, 0.000000)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, -0.707107, 0.000000)); elbowShadowVertices_.push_back(vec3(0.000000, -1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(0.707107, -0.707107, 0.000000)); elbowShadowVertices_.push_back(vec3(1.000000, 0.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(1.000000, 0.019215, 0.195090)); elbowShadowVertices_.push_back(vec3(0.707107, 0.712735, 0.057141)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.712735, 0.057141)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.019215, 0.195090)); elbowShadowVertices_.push_back(vec3(-0.707107, -0.674305, 0.333040)); elbowShadowVertices_.push_back(vec3(0.000000, -0.961571, 0.390181)); elbowShadowVertices_.push_back(vec3(0.707107, -0.674305, 0.333040)); elbowShadowVertices_.push_back(vec3(1.000000, 0.019215, 0.195090)); elbowShadowVertices_.push_back(vec3(1.000000, 0.076120, 0.382683)); elbowShadowVertices_.push_back(vec3(0.707107, 0.729402, 0.112085)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.729402, 0.112085)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.076120, 0.382683)); elbowShadowVertices_.push_back(vec3(-0.707107, -0.577161, 0.653282)); elbowShadowVertices_.push_back(vec3(0.000000, -0.847759, 0.765367)); elbowShadowVertices_.push_back(vec3(0.707107, -0.577161, 0.653282)); elbowShadowVertices_.push_back(vec3(1.000000, 0.076120, 0.382683)); elbowShadowVertices_.push_back(vec3(1.000000, 0.168530, 0.555570)); elbowShadowVertices_.push_back(vec3(0.707107, 0.756468, 0.162723)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.756468, 0.162723)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.168530, 0.555570)); elbowShadowVertices_.push_back(vec3(-0.707107, -0.419407, 0.948418)); elbowShadowVertices_.push_back(vec3(0.000000, -0.662939, 1.111140)); elbowShadowVertices_.push_back(vec3(0.707107, -0.419407, 0.948418)); elbowShadowVertices_.push_back(vec3(1.000000, 0.168530, 0.555570)); elbowShadowVertices_.push_back(vec3(1.000000, 0.292893, 0.707107)); elbowShadowVertices_.push_back(vec3(0.707107, 0.792893, 0.207107)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.792893, 0.207107)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.292893, 0.707107)); elbowShadowVertices_.push_back(vec3(-0.707107, -0.207107, 1.207107)); elbowShadowVertices_.push_back(vec3(0.000000, -0.414214, 1.414214)); elbowShadowVertices_.push_back(vec3(0.707107, -0.207107, 1.207107)); elbowShadowVertices_.push_back(vec3(1.000000, 0.292893, 0.707107)); elbowShadowVertices_.push_back(vec3(1.000000, 0.444430, 0.831470)); elbowShadowVertices_.push_back(vec3(0.707107, 0.837277, 0.243532)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.837277, 0.243532)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.444430, 0.831470)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.051582, 1.419407)); elbowShadowVertices_.push_back(vec3(0.000000, -0.111140, 1.662939)); elbowShadowVertices_.push_back(vec3(0.707107, 0.051582, 1.419407)); elbowShadowVertices_.push_back(vec3(1.000000, 0.444430, 0.831470)); elbowShadowVertices_.push_back(vec3(1.000000, 0.617317, 0.923880)); elbowShadowVertices_.push_back(vec3(0.707107, 0.887915, 0.270598)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.887915, 0.270598)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.617317, 0.923880)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.346719, 1.577161)); elbowShadowVertices_.push_back(vec3(0.000000, 0.234633, 1.847759)); elbowShadowVertices_.push_back(vec3(0.707107, 0.346719, 1.577161)); elbowShadowVertices_.push_back(vec3(1.000000, 0.617317, 0.923880)); elbowShadowVertices_.push_back(vec3(1.000000, 0.804910, 0.980785)); elbowShadowVertices_.push_back(vec3(0.707107, 0.942859, 0.287265)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.942859, 0.287265)); elbowShadowVertices_.push_back(vec3(-1.000000, 0.804910, 0.980785)); elbowShadowVertices_.push_back(vec3(-0.707107, 0.666960, 1.674305)); elbowShadowVertices_.push_back(vec3(0.000000, 0.609819, 1.961571)); elbowShadowVertices_.push_back(vec3(0.707107, 0.666960, 1.674305)); elbowShadowVertices_.push_back(vec3(1.000000, 0.804910, 0.980785)); elbowShadowVertices_.push_back(vec3(1.000000, 1.000000, 1.000000)); elbowShadowVertices_.push_back(vec3(0.707107, 1.000000, 0.292893)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 0.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 1.000000, 0.292893)); elbowShadowVertices_.push_back(vec3(-1.000000, 1.000000, 1.000000)); elbowShadowVertices_.push_back(vec3(-0.707107, 1.000000, 1.707107)); elbowShadowVertices_.push_back(vec3(0.000000, 1.000000, 2.000000)); elbowShadowVertices_.push_back(vec3(0.707107, 1.000000, 1.707107)); elbowShadowVertices_.push_back(vec3(1.000000, 1.000000, 1.000000)); // Now that we've setup the vertex data, we can setup the map of how // that data will be laid out in the buffer object. static const unsigned int sv3(sizeof(vec3)); dataMap_.scvOffset = 0; dataMap_.scvSize = singleCylinderVertices_.size() * sv3; dataMap_.totalSize = dataMap_.scvSize; dataMap_.scnOffset = dataMap_.scvOffset + dataMap_.scvSize; dataMap_.scnSize = singleCylinderNormals_.size() * sv3; dataMap_.totalSize += dataMap_.scnSize; dataMap_.dcvOffset = dataMap_.scnOffset + dataMap_.scnSize; dataMap_.dcvSize = doubleCylinderVertices_.size() * sv3; dataMap_.totalSize += dataMap_.dcvSize; dataMap_.dcnOffset = dataMap_.dcvOffset + dataMap_.dcvSize; dataMap_.dcnSize = doubleCylinderNormals_.size() * sv3; dataMap_.totalSize += dataMap_.dcnSize; dataMap_.evOffset = dataMap_.dcnOffset + dataMap_.dcnSize; dataMap_.evSize = elbowVertices_.size() * sv3; dataMap_.totalSize += dataMap_.evSize; dataMap_.enOffset = dataMap_.evOffset + dataMap_.evSize; dataMap_.enSize = elbowNormals_.size() * sv3; dataMap_.totalSize += dataMap_.enSize; dataMap_.esvOffset = dataMap_.enOffset + dataMap_.enSize; dataMap_.esvSize = elbowShadowVertices_.size() * sv3; dataMap_.totalSize += dataMap_.esvSize; // // The original implementation of both the logo and the lamp represented // the vertex and normal data in a triply-dimensioned array of floats and // all of the calls referenced double-indexed arrays of vector data. // To my mind, this made the code look clunky and overly verbose. // Representing the data as a STL vector of vec3 (itself a 3-float vector // quantity) provides both an efficient container and allows for more // concise looking code. The slightly goofy loops (using the original 2 // dimensional indices to compute a single offset into the STL vector) are // a compromise to avoid rearranging the original data. // // - jesse 2010/10/04 // for (unsigned int i = 0; i < 8; i++) { for (unsigned int j = 0; j < 9; j++) { unsigned int index1(i * 9 + j); unsigned int index2((i + 1) * 9 + j); indexData_.push_back(index1); indexData_.push_back(index2); } } // Initialize the stipple pattern static const unsigned int patterns[] = { 0xaaaaaaaa, 0x55555555 }; for (unsigned int i = 0; i < textureResolution_; i++) { for (unsigned int j = 0; j < textureResolution_; j++) { // Alternate the pattern every other line. unsigned int curMask(1 << j); unsigned int curPattern(patterns[i % 2]); textureImage_[i][j] = ((curPattern & curMask) >> j) * 255; } } } SGILogo::~SGILogo() { if (valid_) { glDeleteBuffers(2, &bufferObjects_[0]); } } void SGILogo::init() { // Make sure we don't re-initialize... if (valid_) { return; } // Initialize shader sources from input files and create programs from them // The program for handling the main object with lighting... string logo_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-logo.vert"); string logo_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-logo.frag"); ShaderSource logo_vtx_source(logo_vtx_filename); ShaderSource logo_frg_source(logo_frg_filename); if (!Scene::load_shaders_from_strings(normalProgram_, logo_vtx_source.str(), logo_frg_source.str())) { Log::error("No valid program for normal logo rendering\n"); return; } normalVertexIndex_ = normalProgram_[vertexAttribName_].location(); normalNormalIndex_ = normalProgram_[normalAttribName_].location(); // The program for handling the flat object... string logo_flat_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-logo-flat.vert"); string logo_flat_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-logo-flat.frag"); ShaderSource logo_flat_vtx_source(logo_flat_vtx_filename); ShaderSource logo_flat_frg_source(logo_flat_frg_filename); if (!Scene::load_shaders_from_strings(flatProgram_, logo_flat_vtx_source.str(), logo_flat_frg_source.str())) { Log::error("No valid program for flat logo rendering\n"); return; } flatVertexIndex_ = flatProgram_[vertexAttribName_].location(); // The program for handling the shadow object with texturing... string logo_shadow_vtx_filename(GLMARK_DATA_PATH"/shaders/ideas-logo-shadow.vert"); string logo_shadow_frg_filename(GLMARK_DATA_PATH"/shaders/ideas-logo-shadow.frag"); ShaderSource logo_shadow_vtx_source(logo_shadow_vtx_filename); ShaderSource logo_shadow_frg_source(logo_shadow_frg_filename); if (!Scene::load_shaders_from_strings(shadowProgram_, logo_shadow_vtx_source.str(), logo_shadow_frg_source.str())) { Log::error("No valid program for shadow logo rendering\n"); return; } shadowVertexIndex_ = shadowProgram_[vertexAttribName_].location(); // We need 2 buffers for our work here. One for the vertex data. // and one for the index data. glGenBuffers(2, &bufferObjects_[0]); // First, setup the vertex data by binding the first buffer object, // allocating its data store, and filling it in with our vertex data. glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBufferData(GL_ARRAY_BUFFER, dataMap_.totalSize, 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.scvOffset, dataMap_.scvSize, &singleCylinderVertices_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.scnOffset, dataMap_.scnSize, &singleCylinderNormals_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.dcvOffset, dataMap_.dcvSize, &doubleCylinderVertices_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.dcnOffset, dataMap_.dcnSize, &doubleCylinderNormals_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.evOffset, dataMap_.evSize, &elbowVertices_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.enOffset, dataMap_.enSize, &elbowNormals_.front()); glBufferSubData(GL_ARRAY_BUFFER, dataMap_.esvOffset, dataMap_.esvSize, &elbowShadowVertices_.front()); // Now repeat for our index data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData_.size() * sizeof(unsigned short), &indexData_.front(), GL_STATIC_DRAW); // Setup our the texture that the shadow program will use... glGenTextures(1, &textureName_); glBindTexture(GL_TEXTURE_2D, textureName_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureResolution_, textureResolution_, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureImage_); // We're ready to go. valid_ = true; } void SGILogo::bendForward(Stack4& ms) { ms.translate(0.0, 1.0, 0.0); ms.rotate(90.0, 1.0, 0.0, 0.0); ms.translate(0.0, -1.0, 0.0); } void SGILogo::bendLeft(Stack4& ms) { ms.rotate(-90.0, 0.0, 0.0, 1.0); ms.translate(0.0, 1.0, 0.0); ms.rotate(90.0, 1.0, 0.0, 0.0); ms.translate(0.0, -1.0, 0.0); } void SGILogo::bendRight(Stack4& ms) { ms.rotate(90.0, 0.0, 0.0, 1.0); ms.translate(0.0, 1.0, 0.0); ms.rotate(90.0, 1.0, 0.0, 0.0); ms.translate(0.0, -1.0, 0.0); } void SGILogo::drawDoubleCylinder(void) { glVertexAttribPointer(vertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.dcvOffset)); if (drawStyle_ == LOGO_NORMAL) { glVertexAttribPointer(normalNormalIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.dcnOffset)); } glDrawArrays(GL_TRIANGLE_STRIP, 0, 18); } void SGILogo::drawSingleCylinder(void) { glVertexAttribPointer(vertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.scvOffset)); if (drawStyle_ == LOGO_NORMAL) { glVertexAttribPointer(normalNormalIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.scnOffset)); } glDrawArrays(GL_TRIANGLE_STRIP, 0, 18); } void SGILogo::drawElbow(void) { unsigned int startIdx(0); unsigned int endIdx(6); if (drawStyle_ == LOGO_NORMAL) { glVertexAttribPointer(vertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.evOffset)); glVertexAttribPointer(normalNormalIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.enOffset)); } else { endIdx = 8; glVertexAttribPointer(vertexIndex_, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast(dataMap_.esvOffset)); } for (unsigned int i = startIdx; i < endIdx; i++) { unsigned int curOffset(i * 18 * sizeof(unsigned short)); glDrawElements(GL_TRIANGLE_STRIP, 18, GL_UNSIGNED_SHORT, reinterpret_cast(curOffset)); } } // Generate a normal matrix from a modelview matrix // // Since we can't universally handle the normal matrix inside the // vertex shader (inverse() and transpose() built-ins not supported by // GLSL ES, for example), we'll generate it here, and load it as a // uniform. void SGILogo::updateXform(const mat4& mv, Program& program) { if (drawStyle_ == LOGO_NORMAL) { LibMatrix::mat3 normalMatrix(mv[0][0], mv[1][0], mv[2][0], mv[0][1], mv[1][1], mv[2][1], mv[0][2], mv[1][2], mv[2][2]); normalMatrix.transpose().inverse(); program[normalMatrixName_] = normalMatrix; } program[modelviewName_] = mv; } Program& SGILogo::getProgram() { switch (drawStyle_) { case LOGO_NORMAL: return normalProgram_; break; case LOGO_FLAT: return flatProgram_; break; case LOGO_SHADOW: return shadowProgram_; break; } return normalProgram_; } void SGILogo::draw(Stack4& modelview, Stack4& projection, const vec4& lightPosition, DrawStyle style, const uvec3& currentColor) { if (!valid_) { return; } glBindBuffer(GL_ARRAY_BUFFER, bufferObjects_[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects_[1]); // Setup the program to use based upon draw style and set it running. drawStyle_ = style; vec4 logoColor(currentColor.x() / 255.0, currentColor.y() / 255.0, currentColor.z() / 255.0, 1.0); Program& curProgram = getProgram(); curProgram.start(); switch (drawStyle_) { case LOGO_NORMAL: curProgram[lightPositionName_] = lightPosition; vertexIndex_ = normalVertexIndex_; glEnableVertexAttribArray(normalNormalIndex_); break; case LOGO_FLAT: curProgram[logoColorName_] = logoColor; vertexIndex_ = flatVertexIndex_; break; case LOGO_SHADOW: vertexIndex_ = shadowVertexIndex_; break; } glEnableVertexAttribArray(vertexIndex_); curProgram[projectionName_] = projection.getCurrent(); modelview.translate(5.500000, -3.500000, 4.500000); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendRight(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendLeft(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendRight(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendLeft(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendRight(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -7.000000); updateXform(modelview.getCurrent(), curProgram); drawDoubleCylinder(); bendForward(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); modelview.translate(0.0, 0.0, -5.000000); updateXform(modelview.getCurrent(), curProgram); drawSingleCylinder(); bendLeft(modelview); updateXform(modelview.getCurrent(), curProgram); drawElbow(); glDisableVertexAttribArray(vertexIndex_); switch (drawStyle_) { case LOGO_NORMAL: glDisableVertexAttribArray(normalNormalIndex_); break; case LOGO_FLAT: break; case LOGO_SHADOW: break; } curProgram.stop(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } glmark2-2012.08/./src/scene-ideas/s.cc0000664000175000017500000001106612013417376016442 0ustar alfalf00000000000000/* * Vertex position data describing the letter 's' * * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #include "characters.h" using LibMatrix::vec2; LetterS::LetterS() { // Vertex data... vertexData_.push_back(vec2(0.860393, 5.283798)); vertexData_.push_back(vec2(0.529473, 3.550052)); vertexData_.push_back(vec2(0.992761, 4.491228)); vertexData_.push_back(vec2(0.910031, 3.368421)); vertexData_.push_back(vec2(1.240951, 3.830753)); vertexData_.push_back(vec2(1.456050, 3.104231)); vertexData_.push_back(vec2(1.935884, 3.517028)); vertexData_.push_back(vec2(2.002068, 2.988648)); vertexData_.push_back(vec2(2.763185, 3.533540)); vertexData_.push_back(vec2(3.061013, 3.120743)); vertexData_.push_back(vec2(3.391934, 3.748194)); vertexData_.push_back(vec2(4.053774, 3.632611)); vertexData_.push_back(vec2(3.822130, 4.540764)); vertexData_.push_back(vec2(4.550155, 4.590299)); vertexData_.push_back(vec2(3.656670, 5.465428)); vertexData_.push_back(vec2(4.517063, 5.713106)); vertexData_.push_back(vec2(3.276112, 5.894737)); vertexData_.push_back(vec2(3.921407, 6.538700)); vertexData_.push_back(vec2(2.299896, 6.736842)); vertexData_.push_back(vec2(3.044467, 7.430341)); vertexData_.push_back(vec2(1.886246, 7.496388)); vertexData_.push_back(vec2(2.581179, 8.222910)); vertexData_.push_back(vec2(1.902792, 8.751290)); vertexData_.push_back(vec2(2.680455, 8.883385)); vertexData_.push_back(vec2(2.283350, 9.312694)); vertexData_.push_back(vec2(3.358842, 9.609907)); vertexData_.push_back(vec2(3.507756, 9.907121)); vertexData_.push_back(vec2(4.285419, 9.758514)); vertexData_.push_back(vec2(5.112720, 9.973168)); vertexData_.push_back(vec2(4.748707, 9.593395)); // Index data... indexData_.push_back(0); indexData_.push_back(1); indexData_.push_back(2); indexData_.push_back(3); indexData_.push_back(4); indexData_.push_back(5); indexData_.push_back(6); indexData_.push_back(7); indexData_.push_back(8); indexData_.push_back(9); indexData_.push_back(10); indexData_.push_back(11); indexData_.push_back(12); indexData_.push_back(13); indexData_.push_back(14); indexData_.push_back(15); indexData_.push_back(16); indexData_.push_back(17); indexData_.push_back(18); indexData_.push_back(19); indexData_.push_back(20); indexData_.push_back(21); indexData_.push_back(22); indexData_.push_back(23); indexData_.push_back(24); indexData_.push_back(25); indexData_.push_back(26); indexData_.push_back(27); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(0); indexData_.push_back(2); indexData_.push_back(4); indexData_.push_back(6); indexData_.push_back(8); indexData_.push_back(10); indexData_.push_back(12); indexData_.push_back(14); indexData_.push_back(16); indexData_.push_back(18); indexData_.push_back(20); indexData_.push_back(22); indexData_.push_back(24); indexData_.push_back(26); indexData_.push_back(28); indexData_.push_back(29); indexData_.push_back(27); indexData_.push_back(25); indexData_.push_back(23); indexData_.push_back(21); indexData_.push_back(19); indexData_.push_back(17); indexData_.push_back(15); indexData_.push_back(13); indexData_.push_back(11); indexData_.push_back(9); indexData_.push_back(7); indexData_.push_back(5); indexData_.push_back(3); indexData_.push_back(1); // Primitive state so that the draw call can issue the primitives we want. unsigned int curOffset(0); primVec_.push_back(PrimitiveState(GL_TRIANGLE_STRIP, 30, curOffset)); curOffset += (30 * sizeof(unsigned short)); primVec_.push_back(PrimitiveState(GL_LINE_STRIP, 30, curOffset)); } glmark2-2012.08/./src/scene-ideas/logo.h0000664000175000017500000001017712013417376017004 0ustar alfalf00000000000000/* * (c) Copyright 1993, Silicon Graphics, Inc. * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Jesse Barker */ #ifndef LOGO_H_ #define LOGO_H_ #include #include #include "vec.h" #include "stack.h" #include "gl-headers.h" #include "program.h" class SGILogo { public: SGILogo(); ~SGILogo(); // Initialize the logo void init(); bool valid() const { return valid_; } void setPosition(const LibMatrix::vec3& position) { currentPosition_ = position; } // Tell the logo to draw itself. DrawStyle tells it how. // - LOGO_NORMAL renders the logo lit and shaded. // - LOGO_FLAT renders the logo as if flattened onto a surface. // - LOGO_SHADOW renders a stippled-looking shadow of the object. enum DrawStyle { LOGO_NORMAL, LOGO_FLAT, LOGO_SHADOW }; void draw(LibMatrix::Stack4& modelview, LibMatrix::Stack4& projection, const LibMatrix::vec4& lightPosition, DrawStyle style, const LibMatrix::uvec3& color = LibMatrix::uvec3()); private: void drawElbow(); void drawSingleCylinder(); void drawDoubleCylinder(); void bendLeft(LibMatrix::Stack4& ms); void bendRight(LibMatrix::Stack4& ms); void bendForward(LibMatrix::Stack4& ms); void updateXform(const LibMatrix::mat4& mv, Program& program); Program& getProgram(); LibMatrix::vec3 currentPosition_; std::vector singleCylinderVertices_; std::vector singleCylinderNormals_; std::vector doubleCylinderVertices_; std::vector doubleCylinderNormals_; std::vector elbowVertices_; std::vector elbowNormals_; std::vector elbowShadowVertices_; std::vector indexData_; // A simple map so we know where each section of our data starts within // our vertex buffer object. struct VertexDataMap { unsigned int scvOffset; unsigned int scvSize; unsigned int scnOffset; unsigned int scnSize; unsigned int dcvOffset; unsigned int dcvSize; unsigned int dcnOffset; unsigned int dcnSize; unsigned int evOffset; unsigned int evSize; unsigned int enOffset; unsigned int enSize; unsigned int esvOffset; unsigned int esvSize; unsigned int totalSize; } dataMap_; unsigned int bufferObjects_[2]; Program normalProgram_; Program flatProgram_; Program shadowProgram_; std::string normalVertexShader_; std::string normalFragmentShader_; std::string flatVertexShader_; std::string flatFragmentShader_; std::string shadowVertexShader_; std::string shadowFragmentShader_; int normalNormalIndex_; int normalVertexIndex_; int flatVertexIndex_; int shadowVertexIndex_; int vertexIndex_; static const std::string modelviewName_; static const std::string projectionName_; static const std::string lightPositionName_; static const std::string logoColorName_; static const std::string vertexAttribName_; static const std::string normalAttribName_; static const std::string normalMatrixName_; // "Shadow" state GLuint textureName_; GLubyte textureImage_[32][32]; // This is the size in each direction of our texture image static const unsigned int textureResolution_; bool valid_; DrawStyle drawStyle_; }; #endif // LOGO_H_ glmark2-2012.08/./src/libmatrix/test/0000775000175000017500000000000012013417376016457 5ustar alfalf00000000000000glmark2-2012.08/./src/libmatrix/stack.h0000664000175000017500000000506312013417376016762 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef STACK_H_ #define STACK_H_ #include #include "mat.h" namespace LibMatrix { // // Simple matrix stack implementation suitable for tracking OpenGL matrix // state. Default construction puts an identity matrix on the top of the // stack. // template class MatrixStack { public: MatrixStack() { theStack_.push_back(T()); } MatrixStack(const T& matrix) { theStack_.push_back(matrix); } ~MatrixStack() {} const T& getCurrent() const { return theStack_.back(); } void push() { theStack_.push_back(theStack_.back()); } void pop() { theStack_.pop_back(); } void loadIdentity() { theStack_.back().setIdentity(); } T& operator*=(const T& rhs) { T& curMatrix = theStack_.back(); curMatrix *= rhs; return curMatrix; } void print() const { const T& curMatrix = theStack_.back(); curMatrix.print(); } unsigned int getDepth() const { return theStack_.size(); } private: std::vector theStack_; }; class Stack4 : public MatrixStack { public: void translate(float x, float y, float z) { *this *= Mat4::translate(x, y, z); } void scale(float x, float y, float z) { *this *= Mat4::scale(x, y, z); } void rotate(float angle, float x, float y, float z) { *this *= Mat4::rotate(angle, x, y, z); } void frustum(float left, float right, float bottom, float top, float near, float far) { *this *= Mat4::frustum(left, right, bottom, top, near, far); } void ortho(float left, float right, float bottom, float top, float near, float far) { *this *= Mat4::ortho(left, right, bottom, top, near, far); } void perspective(float fovy, float aspect, float zNear, float zFar) { *this *= Mat4::perspective(fovy, aspect, zNear, zFar); } void lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { *this *= Mat4::lookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ); } }; } // namespace LibMatrix #endif // STACK_H_ glmark2-2012.08/./src/libmatrix/program.cc0000664000175000017500000001652712013417376017471 0ustar alfalf00000000000000// // Copyright (c) 2011-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include #include #include #include #include "gl-if.h" #include "program.h" using std::string; using LibMatrix::mat4; using LibMatrix::mat3; using LibMatrix::vec2; using LibMatrix::vec3; using LibMatrix::vec4; Shader::Shader(unsigned int type, const string& source) : handle_(0), type_(type), source_(source), ready_(false), valid_(false) { // Create our shader and setup the source code. handle_ = glCreateShader(type); if (!handle_) { message_ = string("Failed to create the new shader."); return; } const GLchar* shaderSource = source_.c_str(); glShaderSource(handle_, 1, &shaderSource, NULL); GLint param = 0; glGetShaderiv(handle_, GL_SHADER_SOURCE_LENGTH, ¶m); if (static_cast(param) != source_.length() + 1) { std::ostringstream o(string("Expected shader source length ")); o << source_.length() << ", but got " << param << std::endl; message_ = o.str(); return; } valid_ = true; } Shader::~Shader() { handle_ = 0; type_ = 0; ready_ = false; valid_ = false; } void Shader::compile() { // Make sure we have a good shader and haven't already compiled it. if (!valid_ || ready_) { return; } glCompileShader(handle_); GLint param = 0; glGetShaderiv(handle_, GL_COMPILE_STATUS, ¶m); if (param == GL_FALSE) { glGetShaderiv(handle_, GL_INFO_LOG_LENGTH, ¶m); GLchar* infoLog = new GLchar[param + 1]; glGetShaderInfoLog(handle_, param + 1, NULL, infoLog); message_ = infoLog; delete [] infoLog; return; } ready_ = true; } void Shader::attach(unsigned int program) { // Shader must be valid and compiled to be attached to a program. if (!valid_ || !ready_) { return; } glAttachShader(program, handle_); } void Shader::release() { if (handle_) { glDeleteShader(handle_); } handle_ = 0; type_ = 0; ready_ = false; valid_ = false; } Program::Program() : handle_(0), ready_(false), valid_(false) { } Program::~Program() { // First release all of the shader resources attached to us and clean up // our handle. release(); } void Program::init() { handle_ = glCreateProgram(); if (!handle_) { message_ = string("Failed to create the new program"); return; } valid_ = true; } void Program::release() { // First delete all of the shader resources attached to us. for (std::vector::iterator shaderIt = shaders_.begin(); shaderIt != shaders_.end(); shaderIt++) { shaderIt->release(); } // Clear out the shader vector so we're ready to reuse it. shaders_.clear(); // Clear out the error string to make sure we don't return anything stale. message_.clear(); // Release all of the symbol map resources. for (std::map::iterator symbolIt = symbols_.begin(); symbolIt != symbols_.end(); symbolIt++) { delete (*symbolIt).second; } symbols_.clear(); if (handle_) { glDeleteProgram(handle_); } handle_ = 0; ready_ = false; valid_ = false; } void Program::addShader(unsigned int type, const string& source) { if (!valid_) { return; } Shader shader(type, source); if (!shader.valid()) { message_ = shader.errorMessage(); valid_ = false; return; } shader.compile(); if (!shader.ready()) { message_ = shader.errorMessage(); valid_ = false; return; } shader.attach(handle_); shaders_.push_back(shader); return; } void Program::build() { if (!valid_ || ready_) { return; } if (shaders_.empty()) { message_ = string("There are no shaders attached to this program"); return; } glLinkProgram(handle_); GLint param = 1; glGetProgramiv(handle_, GL_LINK_STATUS, ¶m); if (param == GL_FALSE) { glGetProgramiv(handle_, GL_INFO_LOG_LENGTH, ¶m); GLchar* infoLog = new GLchar[param + 1]; glGetProgramInfoLog(handle_, param + 1, NULL, infoLog); message_ = infoLog; delete [] infoLog; return; } ready_ = true; } void Program::start() { if (!valid_ || !ready_) { return; } glUseProgram(handle_); } void Program::stop() { glUseProgram(0); } int Program::getUniformLocation(const string& name) { GLint location = glGetUniformLocation(handle_, name.c_str()); if (location < 0) { message_ = string("Failed to get uniform location for \"") + name + string("\""); } return location; } int Program::getAttribIndex(const string& name) { GLint index = glGetAttribLocation(handle_, name.c_str()); if (index < 0) { message_ = string("Failed to get attribute location for \"") + name + string("\""); } return index; } Program::Symbol& Program::Symbol::operator=(const mat4& m) { if (type_ == Uniform) { // Our matrix representation is column-major, so transpose is false here. glUniformMatrix4fv(location_, 1, GL_FALSE, m); } return *this; } Program::Symbol& Program::Symbol::operator=(const mat3& m) { if (type_ == Uniform) { // Our matrix representation is column-major, so transpose is false here. glUniformMatrix3fv(location_, 1, GL_FALSE, m); } return *this; } Program::Symbol& Program::Symbol::operator=(const vec2& v) { if (type_ == Uniform) { glUniform2fv(location_, 1, v); } return *this; } Program::Symbol& Program::Symbol::operator=(const vec3& v) { if (type_ == Uniform) { glUniform3fv(location_, 1, v); } return *this; } Program::Symbol& Program::Symbol::operator=(const vec4& v) { if (type_ == Uniform) { glUniform4fv(location_, 1, v); } return *this; } Program::Symbol& Program::Symbol::operator=(const float& f) { if (type_ == Uniform) { glUniform1f(location_, f); } return *this; } Program::Symbol& Program::Symbol::operator=(const int& i) { if (type_ == Uniform) { glUniform1i(location_, i); } return *this; } Program::Symbol& Program::operator[](const std::string& name) { std::map::iterator mapIt = symbols_.find(name); if (mapIt == symbols_.end()) { Program::Symbol::SymbolType type(Program::Symbol::Attribute); int location = getAttribIndex(name); if (location < 0) { // No attribute found by that name. Let's try a uniform... type = Program::Symbol::Uniform; location = getUniformLocation(name); if (location < 0) { type = Program::Symbol::None; } } mapIt = symbols_.insert(mapIt, std::make_pair(name, new Symbol(name, location, type))); } return *(*mapIt).second; } glmark2-2012.08/./src/libmatrix/gl-if.h0000664000175000017500000000112012013417376016641 0ustar alfalf00000000000000// // Copyright (c) 2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef GL_IF_H_ #define GL_IF_H_ // Inclusion abstraction to provide project specific interface headers for // whatever flavor of OpenGL(|ES) is appropriate. For core libmatrix, this // is GLEW. #include "gl-headers.h" #endif // GL_IF_H_ glmark2-2012.08/./src/libmatrix/shader-source.h0000664000175000017500000000702712013417376020423 0ustar alfalf00000000000000// // Copyright (c) 2010-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #include #include #include #include "vec.h" #include "mat.h" /** * Helper class for loading and manipulating shader sources. */ class ShaderSource { public: enum ShaderType { ShaderTypeVertex, ShaderTypeFragment, ShaderTypeUnknown }; ShaderSource(ShaderType type = ShaderTypeUnknown) : precision_has_been_set_(false), type_(type) {} ShaderSource(const std::string &filename, ShaderType type = ShaderTypeUnknown) : precision_has_been_set_(false), type_(type) { append_file(filename); } void append(const std::string &str); void append_file(const std::string &filename); void replace(const std::string &remove, const std::string &insert); void replace_with_file(const std::string &remove, const std::string &filename); void add(const std::string &str, const std::string &function = ""); void add_const(const std::string &name, float f, const std::string &function = ""); void add_const(const std::string &name, std::vector &f, const std::string &function = ""); void add_const(const std::string &name, const LibMatrix::vec2 &v, const std::string &function = ""); void add_const(const std::string &name, const LibMatrix::vec3 &v, const std::string &function = ""); void add_const(const std::string &name, const LibMatrix::vec4 &v, const std::string &function = ""); void add_const(const std::string &name, const LibMatrix::mat3 &m, const std::string &function = ""); void add_array(const std::string &name, std::vector &array, const std::string &init_function, const std::string &decl_function = ""); ShaderType type(); std::string str(); enum PrecisionValue { PrecisionValueLow, PrecisionValueMedium, PrecisionValueHigh, PrecisionValueDefault }; struct Precision { Precision(); Precision(PrecisionValue int_p, PrecisionValue float_p, PrecisionValue sampler2d_p, PrecisionValue samplercube_p); Precision(const std::string& list); PrecisionValue int_precision; PrecisionValue float_precision; PrecisionValue sampler2d_precision; PrecisionValue samplercube_precision; }; void precision(const Precision& precision); const Precision& precision(); static void default_precision(const Precision& precision, ShaderType type = ShaderTypeUnknown); static const Precision& default_precision(ShaderType type); private: void add_global(const std::string &str); void add_local(const std::string &str, const std::string &function); bool load_file(const std::string& filename, std::string& str); void emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, const std::string& type_str); std::stringstream source_; Precision precision_; bool precision_has_been_set_; ShaderType type_; static std::vector default_precision_; }; glmark2-2012.08/./src/libmatrix/shader-source.cc0000664000175000017500000004122712013417376020561 0ustar alfalf00000000000000// // Copyright (c) 2010-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #include #include #include "shader-source.h" #include "log.h" #include "vec.h" #include "util.h" /** * Holds default precision values for all shader types * (even the unknown type, which is hardwired to default precision values) */ std::vector ShaderSource::default_precision_(ShaderSource::ShaderTypeUnknown + 1); /** * Loads the contents of a file into a string. * * @param filename the name of the file * @param str the string to put the contents of the file into */ bool ShaderSource::load_file(const std::string& filename, std::string& str) { std::auto_ptr is_ptr(Util::get_resource(filename)); std::istream& inputFile(*is_ptr); if (!inputFile) { Log::error("Failed to open \"%s\"\n", filename.c_str()); return false; } std::string curLine; while (getline(inputFile, curLine)) { str += curLine; str += '\n'; } return true; } /** * Appends a string to the shader source. * * @param str the string to append */ void ShaderSource::append(const std::string &str) { source_ << str; } /** * Appends the contents of a file to the shader source. * * @param filename the name of the file to append */ void ShaderSource::append_file(const std::string &filename) { std::string source; if (load_file(filename, source)) source_ << source; } /** * Replaces a string in the source with another string. * * @param remove the string to replace * @param insert the string to replace with */ void ShaderSource::replace(const std::string &remove, const std::string &insert) { std::string::size_type pos = 0; std::string str(source_.str()); while ((pos = str.find(remove, pos)) != std::string::npos) { str.replace(pos, remove.size(), insert); pos++; } source_.clear(); source_.str(str); } /** * Replaces a string in the source with the contents of a file. * * @param remove the string to replace * @param filename the name of the file to read from */ void ShaderSource::replace_with_file(const std::string &remove, const std::string &filename) { std::string source; if (load_file(filename, source)) replace(remove, source); } /** * Adds a string (usually containing a constant definition) at * global (per shader) scope. * * The string is placed after any default precision qualifiers. * * @param str the string to add */ void ShaderSource::add_global(const std::string &str) { std::string::size_type pos = 0; std::string source(source_.str()); /* Find the last precision qualifier */ pos = source.rfind("precision"); if (pos != std::string::npos) { /* * Find the next #endif line of a preprocessor block that contains * the precision qualifier. */ std::string::size_type pos_if = source.find("#if", pos); std::string::size_type pos_endif = source.find("#endif", pos); if (pos_endif != std::string::npos && pos_endif < pos_if) pos = pos_endif; /* Go to the next line */ pos = source.find("\n", pos); if (pos != std::string::npos) pos++; } else pos = 0; source.insert(pos, str); source_.clear(); source_.str(source); } /** * Adds a string (usually containing a constant definition) at * global (per shader) scope. * * The string is placed after any default precision qualifiers. * * @param function the function to add the string into * @param str the string to add */ void ShaderSource::add_local(const std::string &str, const std::string &function) { std::string::size_type pos = 0; std::string source(source_.str()); /* Find the function */ pos = source.find(function); pos = source.find('{', pos); /* Go to the next line */ pos = source.find("\n", pos); if (pos != std::string::npos) pos++; source.insert(pos, str); source_.clear(); source_.str(source); } /** * Adds a string (usually containing a constant definition) to a shader source * * If the function parameter is empty, the string will be added to global * scope, after any precision definitions. * * @param str the string to add * @param function if not empty, the function to add the string into */ void ShaderSource::add(const std::string &str, const std::string &function) { if (!function.empty()) add_local(str, function); else add_global(str); } /** * Adds a float constant definition. * * @param name the name of the constant * @param f the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, float f, const std::string &function) { std::stringstream ss; ss << "const float " << name << " = " << std::fixed << f << ";" << std::endl; add(ss.str(), function); } /** * Adds a float array constant definition. * * Note that various GLSL versions (including ES) don't support * array constants. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, std::vector &array, const std::string &function) { std::stringstream ss; ss << "const float " << name << "[" << array.size() << "] = {" << std::fixed; for(std::vector::const_iterator iter = array.begin(); iter != array.end(); iter++) { ss << *iter; if (iter + 1 != array.end()) ss << ", " << std::endl; } ss << "};" << std::endl; add(ss.str(), function); } /** * Adds a vec2 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec2 &v, const std::string &function) { std::stringstream ss; ss << "const vec2 " << name << " = vec2(" << std::fixed; ss << v.x() << ", " << v.y() << ");" << std::endl; add(ss.str(), function); } /** * Adds a vec3 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec3 &v, const std::string &function) { std::stringstream ss; ss << "const vec3 " << name << " = vec3(" << std::fixed; ss << v.x() << ", " << v.y() << ", " << v.z() << ");" << std::endl; add(ss.str(), function); } /** * Adds a vec4 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::vec4 &v, const std::string &function) { std::stringstream ss; ss << "const vec4 " << name << " = vec4(" << std::fixed; ss << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ");" << std::endl; add(ss.str(), function); } /** * Adds a mat3 constant definition. * * @param name the name of the constant * @param v the value of the constant * @param function if not empty, the function to put the definition in */ void ShaderSource::add_const(const std::string &name, const LibMatrix::mat3 &m, const std::string &function) { std::stringstream ss; ss << "const mat3 " << name << " = mat3(" << std::fixed; ss << m[0][0] << ", " << m[1][0] << ", " << m[2][0] << "," << std::endl; ss << m[0][1] << ", " << m[1][1] << ", " << m[2][1] << "," << std::endl; ss << m[0][2] << ", " << m[1][2] << ", " << m[2][2] << std::endl; ss << ");" << std::endl; add(ss.str(), function); } /** * Adds a float array declaration and initialization. * * @param name the name of the array * @param array the array values * @param init_function the function to put the initialization in * @param decl_function if not empty, the function to put the declaration in */ void ShaderSource::add_array(const std::string &name, std::vector &array, const std::string &init_function, const std::string &decl_function) { if (init_function.empty() || name.empty()) return; std::stringstream ss; ss << "float " << name << "[" << array.size() << "];" << std::endl; std::string decl(ss.str()); ss.clear(); ss.str(""); ss << std::fixed; for(std::vector::const_iterator iter = array.begin(); iter != array.end(); iter++) { ss << name << "[" << iter - array.begin() << "] = " << *iter << ";" << std::endl; } add(ss.str(), init_function); add(decl, decl_function); } /** * Gets the ShaderType for this ShaderSource. * * If the ShaderType is unknown, an attempt is made to infer * the type from the shader source contents. * * @return the ShaderType */ ShaderSource::ShaderType ShaderSource::type() { /* Try to infer the type from the source contents */ if (type_ == ShaderSource::ShaderTypeUnknown) { std::string source(source_.str()); if (source.find("gl_FragColor") != std::string::npos) type_ = ShaderSource::ShaderTypeFragment; else if (source.find("gl_Position") != std::string::npos) type_ = ShaderSource::ShaderTypeVertex; else Log::debug("Cannot infer shader type from contents. Leaving it Unknown.\n"); } return type_; } /** * Helper function that emits a precision statement. * * @param ss the stringstream to add the statement to * @param val the precision value * @param type_str the variable type to apply the precision value to */ void ShaderSource::emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, const std::string& type_str) { static const char *precision_map[] = { "lowp", "mediump", "highp", NULL }; if (val == ShaderSource::PrecisionValueHigh) { if (type_ == ShaderSource::ShaderTypeFragment) ss << "#ifdef GL_FRAGMENT_PRECISION_HIGH" << std::endl; ss << "precision highp " << type_str << ";" << std::endl; if (type_ == ShaderSource::ShaderTypeFragment) { ss << "#else" << std::endl; ss << "precision mediump " << type_str << ";" << std::endl; ss << "#endif" << std::endl; } } else if (val >= 0 && val < ShaderSource::PrecisionValueDefault) { ss << "precision " << precision_map[val] << " "; ss << type_str << ";" << std::endl; } /* There is no default precision in the fragment shader, so set it to mediump */ if (val == ShaderSource::PrecisionValueDefault && type_str == "float" && type_ == ShaderSource::ShaderTypeFragment) { ss << "precision mediump float;" << std::endl; } } /** * Gets a string containing the complete shader source. * * Precision statements are applied at this point. * * @return the shader source */ std::string ShaderSource::str() { /* Decide which precision values to use */ ShaderSource::Precision precision; /* Ensure we have tried to infer the type from the contents */ type(); if (precision_has_been_set_) precision = precision_; else precision = default_precision(type_); /* Create the precision statements */ std::stringstream ss; emit_precision(ss, precision.int_precision, "int"); emit_precision(ss, precision.float_precision, "float"); emit_precision(ss, precision.sampler2d_precision, "sampler2D"); emit_precision(ss, precision.samplercube_precision, "samplerCube"); std::string precision_str(ss.str()); if (!precision_str.empty()) { precision_str.insert(0, "#ifdef GL_ES\n"); precision_str.insert(precision_str.size(), "#endif\n"); } return precision_str + source_.str(); } /** * Sets the precision that will be used for this shader. * * This overrides any default values set with ShaderSource::default_*_precision(). * * @param precision the precision to set */ void ShaderSource::precision(const ShaderSource::Precision& precision) { precision_ = precision; precision_has_been_set_ = true; } /** * Gets the precision that will be used for this shader. * * @return the precision */ const ShaderSource::Precision& ShaderSource::precision() { return precision_; } /** * Sets the default precision that will be used for a shaders type. * * If type is ShaderTypeUnknown the supplied precision is used for all * shader types. * * This can be overriden per ShaderSource object by using ::precision(). * * @param precision the default precision to set * @param type the ShaderType to use the precision for */ void ShaderSource::default_precision(const ShaderSource::Precision& precision, ShaderSource::ShaderType type) { if (type < 0 || type > ShaderSource::ShaderTypeUnknown) type = ShaderSource::ShaderTypeUnknown; if (type == ShaderSource::ShaderTypeUnknown) { for (size_t i = 0; i < ShaderSource::ShaderTypeUnknown; i++) default_precision_[i] = precision; } else { default_precision_[type] = precision; } } /** * Gets the default precision that will be used for a shader type. * * It is valid to use a type of ShaderTypeUnknown. This will always * return a Precision with default values. * * @param type the ShaderType to get the precision of * * @return the precision */ const ShaderSource::Precision& ShaderSource::default_precision(ShaderSource::ShaderType type) { if (type < 0 || type > ShaderSource::ShaderTypeUnknown) type = ShaderSource::ShaderTypeUnknown; return default_precision_[type]; } /**************************************** * ShaderSource::Precision constructors * ****************************************/ /** * Creates a ShaderSource::Precision with default precision values. */ ShaderSource::Precision::Precision() : int_precision(ShaderSource::PrecisionValueDefault), float_precision(ShaderSource::PrecisionValueDefault), sampler2d_precision(ShaderSource::PrecisionValueDefault), samplercube_precision(ShaderSource::PrecisionValueDefault) { } /** * Creates a ShaderSource::Precision using the supplied precision values. */ ShaderSource::Precision::Precision(ShaderSource::PrecisionValue int_p, ShaderSource::PrecisionValue float_p, ShaderSource::PrecisionValue sampler2d_p, ShaderSource::PrecisionValue samplercube_p) : int_precision(int_p), float_precision(float_p), sampler2d_precision(sampler2d_p), samplercube_precision(samplercube_p) { } /** * Creates a ShaderSource::Precision from a string representation of * precision values. * * The string format is: * ",,," * * Each precision value is one of "high", "medium", "low" or "default". * * @param precision_values the string representation of the precision values */ ShaderSource::Precision::Precision(const std::string& precision_values) : int_precision(ShaderSource::PrecisionValueDefault), float_precision(ShaderSource::PrecisionValueDefault), sampler2d_precision(ShaderSource::PrecisionValueDefault), samplercube_precision(ShaderSource::PrecisionValueDefault) { std::vector elems; Util::split(precision_values, ',', elems, Util::SplitModeNormal); for (size_t i = 0; i < elems.size() && i < 4; i++) { const std::string& pstr(elems[i]); ShaderSource::PrecisionValue pval; if (pstr == "high") pval = ShaderSource::PrecisionValueHigh; else if (pstr == "medium") pval = ShaderSource::PrecisionValueMedium; else if (pstr == "low") pval = ShaderSource::PrecisionValueLow; else pval = ShaderSource::PrecisionValueDefault; switch(i) { case 0: int_precision = pval; break; case 1: float_precision = pval; break; case 2: sampler2d_precision = pval; break; case 3: samplercube_precision = pval; break; default: break; } } } glmark2-2012.08/./src/libmatrix/util.cc0000664000175000017500000002260512013417376016771 0ustar alfalf00000000000000// // Copyright (c) 2010-2011 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #include #include #include #ifdef ANDROID #include #else #include #endif #include "log.h" #include "util.h" using std::string; using std::vector; /* * State machine for bash-like quoted string escaping: * * \ * -----------> +---------+ * | ---------- | Escaped | * | | *,ESC +---------+ * | | * | v ' * +--------+ ---> +--------------+ ----- * | Normal | <--- | SingleQuoted | | *, ESC * +--------+ ' +--------------+ <---- * | ^ * | | * | | " +--------------+ ---- * | ---------- | DoubleQuoted | | *, ESC * -----------> +--------------+ <--- * " | ^ * \ | | *, ESC * v | * +---------------------+ * | DoubleQuotedEscaped | * +---------------------+ * * ESC: Mark character as Escaped */ static void fill_escape_vector(const string &str, vector &esc_vec) { enum State { StateNormal, StateEscaped, StateDoubleQuoted, StateDoubleQuotedEscaped, StateSingleQuoted }; State state = StateNormal; for (string::const_iterator iter = str.begin(); iter != str.end(); iter++) { const char c(*iter); bool esc = false; switch (state) { case StateNormal: if (c == '"') state = StateDoubleQuoted; else if (c == '\\') state = StateEscaped; else if (c == '\'') state = StateSingleQuoted; break; case StateEscaped: esc = true; state = StateNormal; break; case StateDoubleQuoted: if (c == '"') state = StateNormal; else if (c == '\\') state = StateDoubleQuotedEscaped; else esc = true; break; case StateDoubleQuotedEscaped: esc = true; state = StateDoubleQuoted; break; case StateSingleQuoted: if (c == '\'') state = StateNormal; else esc = true; default: break; } esc_vec.push_back(esc); } } static void split_normal(const string& src, char delim, vector& elementVec) { std::stringstream ss(src); string item; while(std::getline(ss, item, delim)) elementVec.push_back(item); } static void split_fuzzy(const string& src, char delim, vector& elementVec) { // Fuzzy case: Initialize our delimiter string based upon the caller's plus // a space to allow for more flexibility. string delimiter(" "); delimiter += delim; // Starting index into the string of the first token (by definition, if // we're parsing a string, there is at least one token). string::size_type startPos(0); // string::find_first_of() looks for any character in the string provided, // it is not treated as a sub-string, so regardless of where the space or // comma is or how many there are, the result is the same. string str(src); string::size_type endPos = str.find_first_of(delimiter); while (endPos != string::npos) { // Push back the current element starting at startPos for // (endPos - startPos) characters. std::string takes care of // terminators, etc. elementVec.push_back(string(str, startPos, endPos - startPos)); // Index of the next element after any delimiter characters. Same // caveat applies to find_first_not_of() that applies to // find_first_of(); endPos tells it where to start the search. string::size_type nextPos = str.find_first_not_of(delimiter, endPos); // Erase the part of the string we've already parsed. str = str.erase(startPos, nextPos - startPos); // Look for the next delimiter. If there isn't one, we bail out. endPos = str.find_first_of(delimiter); } // Regardless of whether we initially had one element or many, 'str' now // only contains one. elementVec.push_back(str); } static void split_quoted(const string& src, char delim, vector& elementVec) { std::stringstream ss; vector escVec; /* Mark characters in the string as escaped or not */ fill_escape_vector(src, escVec); /* Sanity check... */ if (src.length() != escVec.size()) return; for (vector::const_iterator iter = escVec.begin(); iter != escVec.end(); iter++) { bool escaped = static_cast(*iter); char c = src[iter - escVec.begin()]; /* Output all characters, except unescaped ",\,' */ if ((c != '"' && c != '\\' && c != '\'') || escaped) { /* If we reach an unescaped delimiter character, do a split */ if (c == delim && !escaped) { elementVec.push_back(ss.str()); ss.str(""); ss.clear(); } else { ss << c; } } } /* Handle final element, delimited by end of string */ const string &finalElement(ss.str()); if (!finalElement.empty()) elementVec.push_back(finalElement); } void Util::split(const string& src, char delim, vector& elementVec, Util::SplitMode mode) { // Trivial rejection if (src.empty()) { return; } switch (mode) { case Util::SplitModeNormal: return split_normal(src, delim, elementVec); case Util::SplitModeFuzzy: return split_fuzzy(src, delim, elementVec); case Util::SplitModeQuoted: return split_quoted(src, delim, elementVec); default: break; } } uint64_t Util::get_timestamp_us() { struct timeval tv; gettimeofday(&tv, NULL); uint64_t now = static_cast(tv.tv_sec) * 1000000 + static_cast(tv.tv_usec); return now; } std::string Util::appname_from_path(const std::string& path) { std::string::size_type slashPos = path.rfind("/"); std::string::size_type startPos(0); if (slashPos != std::string::npos) { startPos = slashPos + 1; } return std::string(path, startPos, std::string::npos); } #ifndef ANDROID std::istream * Util::get_resource(const std::string &path) { std::ifstream *ifs = new std::ifstream(path.c_str()); return static_cast(ifs); } void Util::list_files(const std::string& dirName, std::vector& fileVec) { DIR* dir = opendir(dirName.c_str()); if (!dir) { Log::error("Failed to open models directory '%s'\n", dirName.c_str()); return; } struct dirent* entry = readdir(dir); while (entry) { std::string pathname(dirName + "/"); pathname += std::string(entry->d_name); // Skip '.' and '..' if (entry->d_name[0] != '.') { fileVec.push_back(pathname); } entry = readdir(dir); } closedir(dir); } #else AAssetManager *Util::android_asset_manager = 0; void Util::android_set_asset_manager(AAssetManager *asset_manager) { Util::android_asset_manager = asset_manager; } AAssetManager * Util::android_get_asset_manager() { return Util::android_asset_manager; } std::istream * Util::get_resource(const std::string &path) { std::string path2(path); /* Remove leading '/' from path name, it confuses the AssetManager */ if (path2.size() > 0 && path2[0] == '/') path2.erase(0, 1); std::stringstream *ss = new std::stringstream; AAsset *asset = AAssetManager_open(Util::android_asset_manager, path2.c_str(), AASSET_MODE_RANDOM); if (asset) { ss->write(reinterpret_cast(AAsset_getBuffer(asset)), AAsset_getLength(asset)); Log::debug("Load asset %s\n", path2.c_str()); AAsset_close(asset); } else { Log::error("Couldn't load asset %s\n", path2.c_str()); } return static_cast(ss); } void Util::list_files(const std::string& dirName, std::vector& fileVec) { AAssetManager *mgr(Util::android_get_asset_manager()); std::string dir_name(dirName); /* Remove leading '/' from path, it confuses the AssetManager */ if (dir_name.size() > 0 && dir_name[0] == '/') dir_name.erase(0, 1); AAssetDir* dir = AAssetManager_openDir(mgr, dir_name.c_str()); if (!dir) { Log::error("Failed to open models directory '%s'\n", dir_name.c_str()); return; } const char *filename(0); while ((filename = AAssetDir_getNextFileName(dir)) != 0) { std::string pathname(dir_name + "/"); pathname += std::string(filename); fileVec.push_back(pathname); } AAssetDir_close(dir); } #endif glmark2-2012.08/./src/libmatrix/vec.h0000664000175000017500000004200412013417376016426 0ustar alfalf00000000000000// // Copyright (c) 2010-2011 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef VEC_H_ #define VEC_H_ #include // only needed for print() functions... #include namespace LibMatrix { // A template class for creating, managing and operating on a 2-element vector // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tvec2 { public: tvec2() : x_(0), y_(0) {} tvec2(const T t) : x_(t), y_(t) {} tvec2(const T x, const T y) : x_(x), y_(y) {} tvec2(const tvec2& v) : x_(v.x_), y_(v.y_) {} ~tvec2() {} // Print the elements of the vector to standard out. // Really only useful for debug and test. void print() const { std::cout << "| " << x_ << " " << y_ << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tvec2 into a call to // the OpenGL command "glUniform2fv()". operator const T*() const { return &x_;} // Get and set access members for the individual elements. const T x() const { return x_; } const T y() const { return y_; } void x(const T& val) { x_ = val; } void y(const T& val) { y_ = val; } // A direct assignment of 'rhs' to this. Return a reference to this. tvec2& operator=(const tvec2& rhs) { if (this != &rhs) { x_ = rhs.x_; y_ = rhs.y_; } return *this; } // Divide this by a scalar. Return a reference to this. tvec2& operator/=(const T& rhs) { x_ /= rhs; y_ /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tvec2 operator/(const T& rhs) const { return tvec2(*this) /= rhs; } // Component-wise divide of this by another vector. // Return a reference to this. tvec2& operator/=(const tvec2& rhs) { x_ /= rhs.x_; y_ /= rhs.y_; return *this; } // Component-wise divide of a copy of this by another vector. // Return the copy. const tvec2 operator/(const tvec2& rhs) const { return tvec2(*this) /= rhs; } // Multiply this by a scalar. Return a reference to this. tvec2& operator*=(const T& rhs) { x_ *= rhs; y_ *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tvec2 operator*(const T& rhs) const { return tvec2(*this) *= rhs; } // Component-wise multiply of this by another vector. // Return a reference to this. tvec2& operator*=(const tvec2& rhs) { x_ *= rhs.x_; y_ *= rhs.y_; return *this; } // Component-wise multiply of a copy of this by another vector. // Return the copy. const tvec2 operator*(const tvec2& rhs) const { return tvec2(*this) *= rhs; } // Add a scalar to this. Return a reference to this. tvec2& operator+=(const T& rhs) { x_ += rhs; y_ += rhs; return *this; } // Add a scalar to a copy of this. Return the copy. const tvec2 operator+(const T& rhs) const { return tvec2(*this) += rhs; } // Component-wise addition of another vector to this. // Return a reference to this. tvec2& operator+=(const tvec2& rhs) { x_ += rhs.x_; y_ += rhs.y_; return *this; } // Component-wise addition of another vector to a copy of this. // Return the copy. const tvec2 operator+(const tvec2& rhs) const { return tvec2(*this) += rhs; } // Subtract a scalar from this. Return a reference to this. tvec2& operator-=(const T& rhs) { x_ -= rhs; y_ -= rhs; return *this; } // Subtract a scalar from a copy of this. Return the copy. const tvec2 operator-(const T& rhs) const { return tvec2(*this) -= rhs; } // Component-wise subtraction of another vector from this. // Return a reference to this. tvec2& operator-=(const tvec2& rhs) { x_ -= rhs.x_; y_ -= rhs.y_; return *this; } // Component-wise subtraction of another vector from a copy of this. // Return the copy. const tvec2 operator-(const tvec2& rhs) const { return tvec2(*this) -= rhs; } // Compute the length of this and return it. float length() const { return sqrt(dot(*this, *this)); } // Make this a unit vector. void normalize() { float l = length(); x_ /= l; y_ /= l; } // Compute the dot product of two vectors. static T dot(const tvec2& v1, const tvec2& v2) { return (v1.x_ * v2.x_) + (v1.y_ * v2.y_); } private: T x_; T y_; }; // A template class for creating, managing and operating on a 3-element vector // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tvec3 { public: tvec3() : x_(0), y_(0), z_(0) {} tvec3(const T t) : x_(t), y_(t), z_(t) {} tvec3(const T x, const T y, const T z) : x_(x), y_(y), z_(z) {} tvec3(const tvec3& v) : x_(v.x_), y_(v.y_), z_(v.z_) {} ~tvec3() {} // Print the elements of the vector to standard out. // Really only useful for debug and test. void print() const { std::cout << "| " << x_ << " " << y_ << " " << z_ << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tvec3 into a call to // the OpenGL command "glUniform3fv()". operator const T*() const { return &x_;} // Get and set access members for the individual elements. const T x() const { return x_; } const T y() const { return y_; } const T z() const { return z_; } void x(const T& val) { x_ = val; } void y(const T& val) { y_ = val; } void z(const T& val) { z_ = val; } // A direct assignment of 'rhs' to this. Return a reference to this. tvec3& operator=(const tvec3& rhs) { if (this != &rhs) { x_ = rhs.x_; y_ = rhs.y_; z_ = rhs.z_; } return *this; } // Divide this by a scalar. Return a reference to this. tvec3& operator/=(const T& rhs) { x_ /= rhs; y_ /= rhs; z_ /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tvec3 operator/(const T& rhs) const { return tvec3(*this) /= rhs; } // Component-wise divide of this by another vector. // Return a reference to this. tvec3& operator/=(const tvec3& rhs) { x_ /= rhs.x_; y_ /= rhs.y_; z_ /= rhs.z_; return *this; } // Component-wise divide of a copy of this by another vector. // Return the copy. const tvec3 operator/(const tvec3& rhs) const { return tvec3(*this) /= rhs; } // Multiply this by a scalar. Return a reference to this. tvec3& operator*=(const T& rhs) { x_ *= rhs; y_ *= rhs; z_ *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tvec3 operator*(const T& rhs) const { return tvec3(*this) *= rhs; } // Component-wise multiply of this by another vector. // Return a reference to this. tvec3& operator*=(const tvec3& rhs) { x_ *= rhs.x_; y_ *= rhs.y_; z_ *= rhs.z_; return *this; } // Component-wise multiply of a copy of this by another vector. // Return the copy. const tvec3 operator*(const tvec3& rhs) const { return tvec3(*this) *= rhs; } // Add a scalar to this. Return a reference to this. tvec3& operator+=(const T& rhs) { x_ += rhs; y_ += rhs; z_ += rhs; return *this; } // Add a scalar to a copy of this. Return the copy. const tvec3 operator+(const T& rhs) const { return tvec3(*this) += rhs; } // Component-wise addition of another vector to this. // Return a reference to this. tvec3& operator+=(const tvec3& rhs) { x_ += rhs.x_; y_ += rhs.y_; z_ += rhs.z_; return *this; } // Component-wise addition of another vector to a copy of this. // Return the copy. const tvec3 operator+(const tvec3& rhs) const { return tvec3(*this) += rhs; } // Subtract a scalar from this. Return a reference to this. tvec3& operator-=(const T& rhs) { x_ -= rhs; y_ -= rhs; z_ -= rhs; return *this; } // Subtract a scalar from a copy of this. Return the copy. const tvec3 operator-(const T& rhs) const { return tvec3(*this) -= rhs; } // Component-wise subtraction of another vector from this. // Return a reference to this. tvec3& operator-=(const tvec3& rhs) { x_ -= rhs.x_; y_ -= rhs.y_; z_ -= rhs.z_; return *this; } // Component-wise subtraction of another vector from a copy of this. // Return the copy. const tvec3 operator-(const tvec3& rhs) const { return tvec3(*this) -= rhs; } // Compute the length of this and return it. float length() const { return sqrt(dot(*this, *this)); } // Make this a unit vector. void normalize() { float l = length(); x_ /= l; y_ /= l; z_ /= l; } // Compute the dot product of two vectors. static T dot(const tvec3& v1, const tvec3& v2) { return (v1.x_ * v2.x_) + (v1.y_ * v2.y_) + (v1.z_ * v2.z_); } // Compute the cross product of two vectors. static tvec3 cross(const tvec3& u, const tvec3& v) { return tvec3((u.y_ * v.z_) - (u.z_ * v.y_), (u.z_ * v.x_) - (u.x_ * v.z_), (u.x_ * v.y_) - (u.y_ * v.x_)); } private: T x_; T y_; T z_; }; // A template class for creating, managing and operating on a 4-element vector // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tvec4 { public: tvec4() : x_(0), y_(0), z_(0), w_(0) {} tvec4(const T t) : x_(t), y_(t), z_(t), w_(t) {} tvec4(const T x, const T y, const T z, const T w) : x_(x), y_(y), z_(z), w_(w) {} tvec4(const tvec4& v) : x_(v.x_), y_(v.y_), z_(v.z_), w_(v.w_) {} ~tvec4() {} // Print the elements of the vector to standard out. // Really only useful for debug and test. void print() const { std::cout << "| " << x_ << " " << y_ << " " << z_ << " " << w_ << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tvec4 into a call to // the OpenGL command "glUniform4fv()". operator const T*() const { return &x_;} // Get and set access members for the individual elements. const T x() const { return x_; } const T y() const { return y_; } const T z() const { return z_; } const T w() const { return w_; } void x(const T& val) { x_ = val; } void y(const T& val) { y_ = val; } void z(const T& val) { z_ = val; } void w(const T& val) { w_ = val; } // A direct assignment of 'rhs' to this. Return a reference to this. tvec4& operator=(const tvec4& rhs) { if (this != &rhs) { x_ = rhs.x_; y_ = rhs.y_; z_ = rhs.z_; w_ = rhs.w_; } return *this; } // Divide this by a scalar. Return a reference to this. tvec4& operator/=(const T& rhs) { x_ /= rhs; y_ /= rhs; z_ /= rhs; w_ /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tvec4 operator/(const T& rhs) const { return tvec4(*this) /= rhs; } // Component-wise divide of this by another vector. // Return a reference to this. tvec4& operator/=(const tvec4& rhs) { x_ /= rhs.x_; y_ /= rhs.y_; z_ /= rhs.z_; w_ /= rhs.w_; return *this; } // Component-wise divide of a copy of this by another vector. // Return the copy. const tvec4 operator/(const tvec4& rhs) const { return tvec4(*this) /= rhs; } // Multiply this by a scalar. Return a reference to this. tvec4& operator*=(const T& rhs) { x_ *= rhs; y_ *= rhs; z_ *= rhs; w_ *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tvec4 operator*(const T& rhs) const { return tvec4(*this) *= rhs; } // Component-wise multiply of this by another vector. // Return a reference to this. tvec4& operator*=(const tvec4& rhs) { x_ *= rhs.x_; y_ *= rhs.y_; z_ *= rhs.z_; w_ *= rhs.w_; return *this; } // Component-wise multiply of a copy of this by another vector. // Return the copy. const tvec4 operator*(const tvec4& rhs) const { return tvec4(*this) *= rhs; } // Add a scalar to this. Return a reference to this. tvec4& operator+=(const T& rhs) { x_ += rhs; y_ += rhs; z_ += rhs; w_ += rhs; return *this; } // Add a scalar to a copy of this. Return the copy. const tvec4 operator+(const T& rhs) const { return tvec4(*this) += rhs; } // Component-wise addition of another vector to this. // Return a reference to this. tvec4& operator+=(const tvec4& rhs) { x_ += rhs.x_; y_ += rhs.y_; z_ += rhs.z_; w_ += rhs.w_; return *this; } // Component-wise addition of another vector to a copy of this. // Return the copy. const tvec4 operator+(const tvec4& rhs) const { return tvec4(*this) += rhs; } // Subtract a scalar from this. Return a reference to this. tvec4& operator-=(const T& rhs) { x_ -= rhs; y_ -= rhs; z_ -= rhs; w_ -= rhs; return *this; } // Subtract a scalar from a copy of this. Return the copy. const tvec4 operator-(const T& rhs) const { return tvec4(*this) -= rhs; } // Component-wise subtraction of another vector from this. // Return a reference to this. tvec4& operator-=(const tvec4& rhs) { x_ -= rhs.x_; y_ -= rhs.y_; z_ -= rhs.z_; w_ -= rhs.w_; return *this; } // Component-wise subtraction of another vector from a copy of this. // Return the copy. const tvec4 operator-(const tvec4& rhs) const { return tvec4(*this) -= rhs; } // Compute the length of this and return it. float length() const { return sqrt(dot(*this, *this)); } // Make this a unit vector. void normalize() { float l = length(); x_ /= l; y_ /= l; z_ /= l; w_ /= l; } // Compute the dot product of two vectors. static T dot(const tvec4& v1, const tvec4& v2) { return (v1.x_ * v2.x_) + (v1.y_ * v2.y_) + (v1.z_ * v2.z_) + (v1.w_ * v2.w_); } private: T x_; T y_; T z_; T w_; }; // // Convenience typedefs. These are here to present a homogeneous view of these // objects with respect to shader source. // typedef tvec2 vec2; typedef tvec3 vec3; typedef tvec4 vec4; typedef tvec2 dvec2; typedef tvec3 dvec3; typedef tvec4 dvec4; typedef tvec2 ivec2; typedef tvec3 ivec3; typedef tvec4 ivec4; typedef tvec2 uvec2; typedef tvec3 uvec3; typedef tvec4 uvec4; typedef tvec2 bvec2; typedef tvec3 bvec3; typedef tvec4 bvec4; } // namespace LibMatrix // Global operators to allow for things like defining a new vector in terms of // a product of a scalar and a vector template const LibMatrix::tvec2 operator*(const T t, const LibMatrix::tvec2& v) { return v * t; } template const LibMatrix::tvec3 operator*(const T t, const LibMatrix::tvec3& v) { return v * t; } template const LibMatrix::tvec4 operator*(const T t, const LibMatrix::tvec4& v) { return v * t; } #endif // VEC_H_ glmark2-2012.08/./src/libmatrix/log.h0000664000175000017500000000310212013417376016426 0ustar alfalf00000000000000// // Copyright (c) 2010-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #ifndef LOG_H_ #define LOG_H_ #include #include class Log { public: static void init(const std::string& appname, bool do_debug = false, std::ostream *extra_out = 0) { appname_ = appname; do_debug_ = do_debug; extra_out_ = extra_out; } // Emit an informational message static void info(const char *fmt, ...); // Emit a debugging message static void debug(const char *fmt, ...); // Emit an error message static void error(const char *fmt, ...); // Explicit flush of the log buffer static void flush(); // A prefix constant that informs the logging infrastructure that the log // message is a continuation of a previous log message to be put on the // same line. static const std::string continuation_prefix; private: // A constant for identifying the log messages as originating from a // particular application. static std::string appname_; // Indicates whether debug level messages should generate any output static bool do_debug_; // Extra stream to output log messages to static std::ostream *extra_out_; }; #endif /* LOG_H_ */ glmark2-2012.08/./src/libmatrix/Makefile0000664000175000017500000000401312013417376017136 0ustar alfalf00000000000000CXXFLAGS = -Wall -Werror -pedantic -O3 LIBMATRIX = libmatrix.a LIBSRCS = mat.cc program.cc log.cc util.cc shader-source.cc LIBOBJS = $(LIBSRCS:.cc=.o) TESTDIR = test LIBMATRIX_TESTS = $(TESTDIR)/libmatrix_test TESTSRCS = $(TESTDIR)/options.cc \ $(TESTDIR)/const_vec_test.cc \ $(TESTDIR)/inverse_test.cc \ $(TESTDIR)/transpose_test.cc \ $(TESTDIR)/shader_source_test.cc \ $(TESTDIR)/util_split_test.cc \ $(TESTDIR)/libmatrix_test.cc TESTOBJS = $(TESTSRCS:.cc=.o) # Make sure to build both the library targets and the tests, and generate # a make failure if the tests don't pass. default: $(LIBMATRIX) $(LIBMATRIX_TESTS) run_tests # Main library targets here. mat.o : mat.cc mat.h vec.h program.o: program.cc program.h mat.h vec.h log.o: log.cc log.h util.o: util.cc util.h shader-source.o: shader-source.cc shader-source.h mat.h vec.h util.h libmatrix.a : mat.o stack.h program.o log.o util.o shader-source.o $(AR) -r $@ $(LIBOBJS) # Tests and execution targets here. $(TESTDIR)/options.o: $(TESTDIR)/options.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/libmatrix_test.o: $(TESTDIR)/libmatrix_test.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/inverse_test.h $(TESTDIR)/transpose_test.h $(TESTDIR)/const_vec_test.o: $(TESTDIR)/const_vec_test.cc $(TESTDIR)/const_vec_test.h $(TESTDIR)/libmatrix_test.h vec.h $(TESTDIR)/inverse_test.o: $(TESTDIR)/inverse_test.cc $(TESTDIR)/inverse_test.h $(TESTDIR)/libmatrix_test.h mat.h $(TESTDIR)/transpose_test.o: $(TESTDIR)/transpose_test.cc $(TESTDIR)/transpose_test.h $(TESTDIR)/libmatrix_test.h mat.h $(TESTDIR)/shader_source_test.o: $(TESTDIR)/shader_source_test.cc $(TESTDIR)/shader_source_test.h $(TESTDIR)/libmatrix_test.h shader-source.h $(TESTDIR)/util_split_test.o: $(TESTDIR)/util_split_test.cc $(TESTDIR)/util_split_test.h $(TESTDIR)/libmatrix_test.h util.h $(TESTDIR)/libmatrix_test: $(TESTOBJS) libmatrix.a $(CXX) -o $@ $^ run_tests: $(LIBMATRIX_TESTS) $(LIBMATRIX_TESTS) clean : $(RM) $(LIBOBJS) $(TESTOBJS) $(LIBMATRIX) $(LIBMATRIX_TESTS) glmark2-2012.08/./src/libmatrix/util.h0000664000175000017500000001026412013417376016631 0ustar alfalf00000000000000// // Copyright (c) 2010-2011 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #ifndef UTIL_H_ #define UTIL_H_ #include #include #include #include #include #ifdef ANDROID #include #endif struct Util { /** * How to perform the split() operation */ enum SplitMode { /** Normal split operation */ SplitModeNormal, /** Allow for spaces and multiple consecutive occurences of the delimiter */ SplitModeFuzzy, /** Take into account bash-like quoting and escaping rules */ SplitModeQuoted }; /** * split() - Splits a string into elements using a provided delimiter * * @s: the string to split * @delim: the delimiter to use * @elems: the string vector to populate * @mode: the SplitMode to use * * Using @delim to determine field boundaries, splits @s into separate * string elements. These elements are returned in the string vector * @elems. As long as @s is non-empty, there will be at least one * element in @elems. */ static void split(const std::string& src, char delim, std::vector& elems, Util::SplitMode mode); /** * get_timestamp_us() - Returns the current time in microseconds */ static uint64_t get_timestamp_us(); /** * get_resource() - Gets an input filestream for a given file. * * @path: the path to the file * * Returns a pointer to an input stream, which must be deleted when no * longer in use. */ static std::istream *get_resource(const std::string &path); /** * list_files() - Get a list of the files in a given directory. * * @dirName: the directory path to be listed. * @fileVec: the string vector to populate. * * Obtains a list of the files in @dirName, and returns them in the string * vector @fileVec. */ static void list_files(const std::string& dirName, std::vector& fileVec); /** * dispose_pointer_vector() - cleans up a vector of pointers * * @vec: vector of pointers to objects or plain-old-data * * Iterates across @vec and deletes the data pointed to by each of the * elements. Clears the vector, resetting it for reuse. */ template static void dispose_pointer_vector(std::vector &vec) { for (typename std::vector::const_iterator iter = vec.begin(); iter != vec.end(); iter++) { delete *iter; } vec.clear(); } /** * toString() - Converts a string to a plain-old-data type. * * @asString: a string representation of plain-old-data. */ template static T fromString(const std::string& asString) { std::stringstream ss(asString); T retVal = T(); ss >> retVal; return retVal; } /** * toString() - Converts a plain-old-data type to a string. * * @t: a simple value to be converted to a string */ template static std::string toString(const T t) { std::stringstream ss; ss << t; return ss.str(); } /** * appname_from_path() - get the name of an executable from an absolute path * * @path: absolute path of the running application (argv[0]) * * Returns the last portion of @path (everything after the final '/'). */ static std::string appname_from_path(const std::string& path); #ifdef ANDROID static void android_set_asset_manager(AAssetManager *asset_manager); static AAssetManager *android_get_asset_manager(void); private: static AAssetManager *android_asset_manager; #endif }; #endif /* UTIL_H */ glmark2-2012.08/./src/libmatrix/mat.cc0000664000175000017500000000751312013417376016576 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include "mat.h" namespace LibMatrix { namespace Mat4 { mat4 translate(float x, float y, float z) { mat4 t; t[0][3] = x; t[1][3] = y; t[2][3] = z; return t; } mat4 scale(float x, float y, float z) { mat4 s; s[0][0] = x; s[1][1] = y; s[2][2] = z; return s; } // // As per the OpenGL "red book" definition of rotation, from the appendix // on Homogeneous Coordinates and Transformation Matrices, the "upper left" // 3x3 portion of the result matrix is formed by: // // M = uuT + (cos a)(I - uuT) + (sin a)S // // where u is the normalized input vector, uuT is the outer product of that // vector and its transpose, I is the identity matrix and S is the matrix: // // | 0 -z' y' | // | z' 0 -x' | // | -y' x' 0 | // // where x', y' and z' are the elements of u // mat4 rotate(float angle, float x, float y, float z) { vec3 u(x, y, z); u.normalize(); mat3 uuT = outer(u, u); mat3 s; s[0][0] = 0; s[0][1] = -u.z(); s[0][2] = u.y(); s[1][0] = u.z(); s[1][1] = 0; s[1][2] = -u.x(); s[2][0] = -u.y(); s[2][1] = u.x(); s[2][2] = 0; mat3 i; i -= uuT; // degrees to radians float angleRadians(angle * M_PI / 180.0); i *= cos(angleRadians); s *= sin(angleRadians); i += s; mat3 m = uuT + i; mat4 r; r[0][0] = m[0][0]; r[0][1] = m[0][1]; r[0][2] = m[0][2]; r[1][0] = m[1][0]; r[1][1] = m[1][1]; r[1][2] = m[1][2]; r[2][0] = m[2][0]; r[2][1] = m[2][1]; r[2][2] = m[2][2]; return r; } mat4 frustum(float left, float right, float bottom, float top, float near, float far) { float twiceNear(2 * near); float width(right - left); float height(top - bottom); float depth(far - near); mat4 f; f[0][0] = twiceNear / width; f[0][2] = (right + left) / width; f[1][1] = twiceNear / height; f[1][2] = (top + bottom) / height; f[2][2] = -(far + near) / depth; f[2][3] = -(twiceNear * far) / depth; f[3][2] = -1; f[3][3] = 0; return f; } mat4 ortho(float left, float right, float bottom, float top, float near, float far) { float width(right - left); float height(top - bottom); float depth(far - near); mat4 o; o[0][0] = 2 / width; o[0][3] = (right + left) / width; o[1][1] = 2 / height; o[1][3] = (top + bottom) / height; o[2][2] = -2 / depth; o[2][3] = (far + near) / depth; return o; } mat4 perspective(float fovy, float aspect, float zNear, float zFar) { // degrees to radians float fovyRadians(fovy * M_PI / 180.0); // cotangent(x) = 1/tan(x) float f = 1/tan(fovyRadians / 2); float depth(zNear - zFar); mat4 p; p[0][0] = f / aspect; p[1][1] = f; p[2][2] = (zFar + zNear) / depth; p[2][3] = (2 * zFar * zNear) / depth; p[3][2] = -1; p[3][3] = 0; return p; } mat4 lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { vec3 f(centerX - eyeX, centerY - eyeY, centerZ - eyeZ); f.normalize(); vec3 up(upX, upY, upZ); vec3 s = vec3::cross(f, up); vec3 u = vec3::cross(s, f); s.normalize(); u.normalize(); mat4 la; la[0][0] = s.x(); la[0][1] = s.y(); la[0][2] = s.z(); la[1][0] = u.x(); la[1][1] = u.y(); la[1][2] = u.z(); la[2][0] = -f.x(); la[2][1] = -f.y(); la[2][2] = -f.z(); la *= translate(-eyeX, -eyeY, -eyeZ); return la; } } // namespace Mat4 } // namespace LibMatrix glmark2-2012.08/./src/libmatrix/README0000664000175000017500000000114112013417376016355 0ustar alfalf00000000000000libmatrix ========= A simple C++ template library that provides containers and arithmetic operations for vectors, matrices and matrix stacks of 2, 3 and 4 dimensions. Additionally, it provides implementations of the more common matrix transformations described by the OpenGL programming guide. libmatrix does not make any OpenGL calls, it merely replaces a portion of the fixed-function vertex processing API that have been removed from newer releases of core OpenGL and omitted from OpenGL ES. The goal is simply to provide developers an easier point of entry into developing for OpenGL and OpenGL ES. glmark2-2012.08/./src/libmatrix/log.cc0000664000175000017500000001046112013417376016572 0ustar alfalf00000000000000// // Copyright (c) 2010-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis // Jesse Barker // #include #include #include #include #include #include #include "log.h" #ifdef ANDROID #include #endif using std::string; const string Log::continuation_prefix("\x10"); string Log::appname_; bool Log::do_debug_(false); std::ostream* Log::extra_out_(0); static const string terminal_color_normal("\033[0m"); static const string terminal_color_red("\033[1;31m"); static const string terminal_color_cyan("\033[36m"); static const string terminal_color_yellow("\033[33m"); static const string empty; static void print_prefixed_message(std::ostream& stream, const string& color, const string& prefix, const string& fmt, va_list ap) { va_list aq; /* Estimate message size */ va_copy(aq, ap); int msg_size = vsnprintf(NULL, 0, fmt.c_str(), aq); va_end(aq); /* Create the buffer to hold the message */ char *buf = new char[msg_size + 1]; /* Store the message in the buffer */ va_copy(aq, ap); vsnprintf(buf, msg_size + 1, fmt.c_str(), aq); va_end(aq); /* * Print the message lines prefixed with the supplied prefix. * If the target stream is a terminal make the prefix colored. */ string linePrefix; if (!prefix.empty()) { static const string colon(": "); string start_color; string end_color; if (!color.empty()) { start_color = color; end_color = terminal_color_normal; } linePrefix = start_color + prefix + end_color + colon; } std::string line; std::stringstream ss(buf); while(std::getline(ss, line)) { /* * If this line is a continuation of a previous log message * just print the line plainly. */ if (line[0] == Log::continuation_prefix[0]) { stream << line.c_str() + 1; } else { /* Normal line, emit the prefix. */ stream << linePrefix << line; } /* Only emit a newline if the original message has it. */ if (!(ss.rdstate() & std::stringstream::eofbit)) stream << std::endl; } delete[] buf; } void Log::info(const char *fmt, ...) { static const string infoprefix("Info"); const string& prefix(do_debug_ ? infoprefix : empty); va_list ap; va_start(ap, fmt); #ifndef ANDROID static const string& infocolor(isatty(fileno(stdout)) ? terminal_color_cyan : empty); const string& color(do_debug_ ? infocolor : empty); print_prefixed_message(std::cout, color, prefix, fmt, ap); #else __android_log_vprint(ANDROID_LOG_INFO, appname_.c_str(), fmt, ap); #endif if (extra_out_) print_prefixed_message(*extra_out_, empty, prefix, fmt, ap); va_end(ap); } void Log::debug(const char *fmt, ...) { static const string dbgprefix("Debug"); if (!do_debug_) return; va_list ap; va_start(ap, fmt); #ifndef ANDROID static const string& dbgcolor(isatty(fileno(stdout)) ? terminal_color_yellow : empty); print_prefixed_message(std::cout, dbgcolor, dbgprefix, fmt, ap); #else __android_log_vprint(ANDROID_LOG_DEBUG, appname_.c_str(), fmt, ap); #endif if (extra_out_) print_prefixed_message(*extra_out_, empty, dbgprefix, fmt, ap); va_end(ap); } void Log::error(const char *fmt, ...) { static const string errprefix("Error"); va_list ap; va_start(ap, fmt); #ifndef ANDROID static const string& errcolor(isatty(fileno(stderr)) ? terminal_color_red : empty); print_prefixed_message(std::cerr, errcolor, errprefix, fmt, ap); #else __android_log_vprint(ANDROID_LOG_ERROR, appname_.c_str(), fmt, ap); #endif if (extra_out_) print_prefixed_message(*extra_out_, empty, errprefix, fmt, ap); va_end(ap); } void Log::flush() { #ifndef ANDROID std::cout.flush(); std::cerr.flush(); #endif if (extra_out_) extra_out_->flush(); } glmark2-2012.08/./src/libmatrix/program.h0000664000175000017500000001174112013417376017324 0ustar alfalf00000000000000// // Copyright (c) 2011-2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef PROGRAM_H_ #define PROGRAM_H_ #include #include #include #include "mat.h" // Simple shader container. Abstracts all of the OpenGL bits, but leaves // much of the semantics intact. This is typically only referenced directly // by the program object. class Shader { public: Shader() : handle_(0), type_(0), ready_(false), valid_(false) {} Shader(const Shader& shader) : handle_(shader.handle_), type_(shader.type_), source_(shader.source_), message_(shader.message_), ready_(shader.ready_), valid_(shader.valid_) {} Shader(unsigned int type, const std::string& source); ~Shader(); // Compiles the shader source so that it can be linked into a // program. // // Make sure the shader is "valid" before calling this one. void compile(); // Attaches a compiled shader to a program in preparation for // linking. // // Make sure the shader is "ready" before calling this one. void attach(unsigned int program); // Release any resources associated with this shader back to // OpenGL void release(); // If "valid" then the shader has successfully been created. // If "ready" then the shader has successfully been compiled. // If either is false, then additional information can be obtained // from the error message. bool valid() const { return valid_; } bool ready() const { return ready_; } const std::string& errorMessage() const { return message_; } private: unsigned int handle_; unsigned int type_; std::string source_; std::string message_; bool ready_; bool valid_; }; // Simple program container. Abstracts all of the OpenGL bits, but leaves // much of the semantics intact. class Program { public: Program(); ~Program(); // Initialize the program object for use. void init(); // Release any resources associated with this program back to // OpenGL void release(); // Create a new shader of the given type and source, compile it and // attach it to the program. // // Make sure the program is "valid" before calling this one. void addShader(unsigned int type, const std::string& source); // Link all of the attached shaders into a runnable program for use // in a rendering operation. // // Make sure the program is "valid" and that at least one shader // has been successfully added before calling this one. void build(); // Bind the program for use by the rendering context (i.e. actually // run it). // // Make sure the program is "ready" before calling this one. void start(); // Unbind the program from use by the rendering context (i.e. stop // using it). void stop(); class Symbol { public: enum SymbolType { None, Attribute, Uniform }; Symbol(const std::string& name, int location, SymbolType type) : type_(type), location_(location), name_(name) {} int location() const { return location_; } // These members cause data to be bound to program variables, so // the program must be bound for use for these to be effective. Symbol& operator=(const LibMatrix::mat4& m); Symbol& operator=(const LibMatrix::mat3& m); Symbol& operator=(const LibMatrix::vec2& v); Symbol& operator=(const LibMatrix::vec3& v); Symbol& operator=(const LibMatrix::vec4& v); Symbol& operator=(const float& f); Symbol& operator=(const int& i); private: Symbol(); SymbolType type_; GLint location_; std::string name_; }; // Get the handle to a named program input (the location in OpenGL // vernacular). Typically used in conjunction with various VertexAttrib // interfaces. Equality operators are used to load uniform data. Symbol& operator[](const std::string& name); // If "valid" then the program has successfully been created. // If "ready" then the program has successfully been built. // If either is false, then additional information can be obtained // from the error message. bool valid() const { return valid_; } bool ready() const { return ready_; } const std::string& errorMessage() const { return message_; } private: int getAttribIndex(const std::string& name); int getUniformLocation(const std::string& name); unsigned int handle_; std::map symbols_; std::vector shaders_; std::string message_; bool ready_; bool valid_; }; #endif // PROGRAM_H_ glmark2-2012.08/./src/libmatrix/COPYING0000664000175000017500000000206312013417376016534 0ustar alfalf00000000000000The MIT License Copyright (c) 2010 Linaro Limited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. glmark2-2012.08/./src/libmatrix/mat.h0000664000175000017500000011401112013417376016430 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef MAT_H_ #define MAT_H_ #include #include #include #include "vec.h" #ifndef USE_EXCEPTIONS // If we're not throwing exceptions, we'll need the logger to make sure the // caller is informed of errors. #include "log.h" #endif // USE_EXCEPTIONS namespace LibMatrix { // Proxy class for providing the functionality of a doubly-dimensioned array // representation of matrices. Each matrix class defines its operator[] // to return an ArrayProxy. The ArrayProxy then returns the appropriate item // from its operator[]. template class ArrayProxy { public: ArrayProxy(T* data) { data_ = data; } ~ArrayProxy() { data_ = 0; } T& operator[](int index) { return data_[index * dimension]; } const T& operator[](int index) const { return data_[index * dimension]; } private: T* data_; }; // Programming interfaces to all matrix objects are represented row-centric // (i.e. C/C++ style references to the data appear as matrix[row][column]). // However, the internal data representation is column-major, so when using // the raw data access member to treat the data as a singly-dimensioned array, // it does not have to be transposed. // // A template class for creating, managing and operating on a 2x2 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tmat2 { public: tmat2() { setIdentity(); } tmat2(const tmat2& m) { m_[0] = m.m_[0]; m_[1] = m.m_[1]; m_[2] = m.m_[2]; m_[3] = m.m_[3]; } tmat2(const T& c0r0, const T& c0r1, const T& c1r0, const T& c1r1) { m_[0] = c0r0; m_[1] = c0r1; m_[2] = c1r0; m_[3] = c1r1; } ~tmat2() {} // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; m_[1] = 0; m_[2] = 0; m_[3] = 1; } // Transpose this. Return a reference to this. tmat2& transpose() { T tmp_val = m_[1]; m_[1] = m_[2]; m_[2] = tmp_val; return *this; } // Compute the determinant of this and return it. T determinant() { return (m_[0] * m_[3]) - (m_[2] * m_[1]); } // Invert this. Return a reference to this. // // NOTE: If this is non-invertible, we will // throw to avoid undefined behavior. tmat2& inverse() #ifdef USE_EXCEPTIONS throw(std::runtime_error) #endif // USE_EXCEPTIONS { T d(determinant()); if (d == static_cast(0)) { #ifdef USE_EXCEPTIONS throw std::runtime_error("Matrix is noninvertible!!!!"); #else // !USE_EXCEPTIONS Log::error("Matrix is noninvertible!!!!\n"); return *this; #endif // USE_EXCEPTIONS } T c0r0(m_[3] / d); T c0r1(-m_[1] / d); T c1r0(-m_[2] / d); T c1r1(m_[0] / d); m_[0] = c0r0; m_[1] = c0r1; m_[2] = c1r0; m_[3] = c1r1; return *this; } // Print the elements of the matrix to standard out. // Really only useful for debug and test. void print() const { static const int precision(6); // row 0 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; std::cout << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tmat2 into a call to // the OpenGL command "glUniformMatrix2fv()". operator const T*() const { return &m_[0];} // Test if 'rhs' is equal to this. bool operator==(const tmat2& rhs) const { return m_[0] == rhs.m_[0] && m_[1] == rhs.m_[1] && m_[2] == rhs.m_[2] && m_[3] == rhs.m_[3]; } // Test if 'rhs' is not equal to this. bool operator!=(const tmat2& rhs) const { return !(*this == rhs); } // A direct assignment of 'rhs' to this. Return a reference to this. tmat2& operator=(const tmat2& rhs) { if (this != &rhs) { m_[0] = rhs.m_[0]; m_[1] = rhs.m_[1]; m_[2] = rhs.m_[2]; m_[3] = rhs.m_[3]; } return *this; } // Add another matrix to this. Return a reference to this. tmat2& operator+=(const tmat2& rhs) { m_[0] += rhs.m_[0]; m_[1] += rhs.m_[1]; m_[2] += rhs.m_[2]; m_[3] += rhs.m_[3]; return *this; } // Add another matrix to a copy of this. Return the copy. const tmat2 operator+(const tmat2& rhs) { return tmat2(*this) += rhs; } // Subtract another matrix from this. Return a reference to this. tmat2& operator-=(const tmat2& rhs) { m_[0] -= rhs.m_[0]; m_[1] -= rhs.m_[1]; m_[2] -= rhs.m_[2]; m_[3] -= rhs.m_[3]; return *this; } // Subtract another matrix from a copy of this. Return the copy. const tmat2 operator-(const tmat2& rhs) { return tmat2(*this) += rhs; } // Multiply this by another matrix. Return a reference to this. tmat2& operator*=(const tmat2& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[2] * rhs.m_[1])); T c0r1((m_[1] * rhs.m_[0]) + (m_[3] * rhs.m_[1])); T c1r0((m_[0] * rhs.m_[2]) + (m_[2] * rhs.m_[3])); T c1r1((m_[1] * rhs.m_[2]) + (m_[3] * rhs.m_[3])); m_[0] = c0r0; m_[1] = c0r1; m_[2] = c1r0; m_[3] = c1r1; return *this; } // Multiply a copy of this by another matrix. Return the copy. const tmat2 operator*(const tmat2& rhs) { return tmat2(*this) *= rhs; } // Multiply this by a scalar. Return a reference to this. tmat2& operator*=(const T& rhs) { m_[0] *= rhs; m_[1] *= rhs; m_[2] *= rhs; m_[3] *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tmat2 operator*(const T& rhs) { return tmat2(*this) *= rhs; } // Divide this by a scalar. Return a reference to this. tmat2& operator/=(const T& rhs) { m_[0] /= rhs; m_[1] /= rhs; m_[2] /= rhs; m_[3] /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tmat2 operator/(const T& rhs) { return tmat2(*this) /= rhs; } // Use an instance of the ArrayProxy class to support double-indexed // references to a matrix (i.e., m[1][1]). See comments above the // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); } const ArrayProxy operator[](int index) const { return ArrayProxy(const_cast(&m_[index])); } private: T m_[4]; }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template const tmat2 operator*(const T& lhs, const tmat2& rhs) { return tmat2(rhs) * lhs; } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template const tvec2 operator*(const tvec2& lhs, const tmat2& rhs) { T x((lhs.x() * rhs[0][0]) + (lhs.y() * rhs[1][0])); T y((lhs.x() * rhs[0][1]) + (lhs.y() * rhs[1][1])); return tvec2(x,y); } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template const tvec2 operator*(const tmat2& lhs, const tvec2& rhs) { T x((lhs[0][0] * rhs.x()) + (lhs[0][1] * rhs.y())); T y((lhs[1][0] * rhs.x()) + (lhs[1][1] * rhs.y())); return tvec2(x, y); } // Compute the outer product of two vectors. Return the resultant matrix. template const tmat2 outer(const tvec2& a, const tvec2& b) { tmat2 product; product[0][0] = a.x() * b.x(); product[0][1] = a.x() * b.y(); product[1][0] = a.y() * b.x(); product[1][1] = a.y() * b.y(); return product; } // A template class for creating, managing and operating on a 3x3 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tmat3 { public: tmat3() { setIdentity(); } tmat3(const tmat3& m) { m_[0] = m.m_[0]; m_[1] = m.m_[1]; m_[2] = m.m_[2]; m_[3] = m.m_[3]; m_[4] = m.m_[4]; m_[5] = m.m_[5]; m_[6] = m.m_[6]; m_[7] = m.m_[7]; m_[8] = m.m_[8]; } tmat3(const T& c0r0, const T& c0r1, const T& c0r2, const T& c1r0, const T& c1r1, const T& c1r2, const T& c2r0, const T& c2r1, const T& c2r2) { m_[0] = c0r0; m_[1] = c0r1; m_[2] = c0r2; m_[3] = c1r0; m_[4] = c1r1; m_[5] = c1r2; m_[6] = c2r0; m_[7] = c2r1; m_[8] = c2r2; } ~tmat3() {} // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; m_[1] = 0; m_[2] = 0; m_[3] = 0; m_[4] = 1; m_[5] = 0; m_[6] = 0; m_[7] = 0; m_[8] = 1; } // Transpose this. Return a reference to this. tmat3& transpose() { T tmp_val = m_[1]; m_[1] = m_[3]; m_[3] = tmp_val; tmp_val = m_[2]; m_[2] = m_[6]; m_[6] = tmp_val; tmp_val = m_[5]; m_[5] = m_[7]; m_[7] = tmp_val; return *this; } // Compute the determinant of this and return it. T determinant() { tmat2 minor0(m_[4], m_[5], m_[7], m_[8]); tmat2 minor3(m_[1], m_[2], m_[7], m_[8]); tmat2 minor6(m_[1], m_[2], m_[4], m_[5]); return (m_[0] * minor0.determinant()) - (m_[3] * minor3.determinant()) + (m_[6] * minor6.determinant()); } // Invert this. Return a reference to this. // // NOTE: If this is non-invertible, we will // throw to avoid undefined behavior. tmat3& inverse() #ifdef USE_EXCEPTIONS throw(std::runtime_error) #endif // USE_EXCEPTIONS { T d(determinant()); if (d == static_cast(0)) { #ifdef USE_EXCEPTIONS throw std::runtime_error("Matrix is noninvertible!!!!"); #else // !USE_EXCEPTIONS Log::error("Matrix is noninvertible!!!!\n"); return *this; #endif // USE_EXCEPTIONS } tmat2 minor0(m_[4], m_[5], m_[7], m_[8]); tmat2 minor1(m_[7], m_[8], m_[1], m_[2]); tmat2 minor2(m_[1], m_[2], m_[4], m_[5]); tmat2 minor3(m_[6], m_[8], m_[3], m_[5]); tmat2 minor4(m_[0], m_[2], m_[6], m_[8]); tmat2 minor5(m_[3], m_[5], m_[0], m_[2]); tmat2 minor6(m_[3], m_[4], m_[6], m_[7]); tmat2 minor7(m_[6], m_[7], m_[0], m_[1]); tmat2 minor8(m_[0], m_[1], m_[3], m_[4]); m_[0] = minor0.determinant() / d; m_[1] = minor1.determinant() / d; m_[2] = minor2.determinant() / d; m_[3] = minor3.determinant() / d; m_[4] = minor4.determinant() / d; m_[5] = minor5.determinant() / d; m_[6] = minor6.determinant() / d; m_[7] = minor7.determinant() / d; m_[8] = minor8.determinant() / d; return *this; } // Print the elements of the matrix to standard out. // Really only useful for debug and test. void print() const { static const int precision(6); // row 0 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[6]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[4]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[7]; std::cout << " |" << std::endl; // row 2 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[5]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[8]; std::cout << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tmat3 into a call to // the OpenGL command "glUniformMatrix3fv()". operator const T*() const { return &m_[0];} // Test if 'rhs' is equal to this. bool operator==(const tmat3& rhs) const { return m_[0] == rhs.m_[0] && m_[1] == rhs.m_[1] && m_[2] == rhs.m_[2] && m_[3] == rhs.m_[3] && m_[4] == rhs.m_[4] && m_[5] == rhs.m_[5] && m_[6] == rhs.m_[6] && m_[7] == rhs.m_[7] && m_[8] == rhs.m_[8]; } // Test if 'rhs' is not equal to this. bool operator!=(const tmat3& rhs) const { return !(*this == rhs); } // A direct assignment of 'rhs' to this. Return a reference to this. tmat3& operator=(const tmat3& rhs) { if (this != &rhs) { m_[0] = rhs.m_[0]; m_[1] = rhs.m_[1]; m_[2] = rhs.m_[2]; m_[3] = rhs.m_[3]; m_[4] = rhs.m_[4]; m_[5] = rhs.m_[5]; m_[6] = rhs.m_[6]; m_[7] = rhs.m_[7]; m_[8] = rhs.m_[8]; } return *this; } // Add another matrix to this. Return a reference to this. tmat3& operator+=(const tmat3& rhs) { m_[0] += rhs.m_[0]; m_[1] += rhs.m_[1]; m_[2] += rhs.m_[2]; m_[3] += rhs.m_[3]; m_[4] += rhs.m_[4]; m_[5] += rhs.m_[5]; m_[6] += rhs.m_[6]; m_[7] += rhs.m_[7]; m_[8] += rhs.m_[8]; return *this; } // Add another matrix to a copy of this. Return the copy. const tmat3 operator+(const tmat3& rhs) { return tmat3(*this) += rhs; } // Subtract another matrix from this. Return a reference to this. tmat3& operator-=(const tmat3& rhs) { m_[0] -= rhs.m_[0]; m_[1] -= rhs.m_[1]; m_[2] -= rhs.m_[2]; m_[3] -= rhs.m_[3]; m_[4] -= rhs.m_[4]; m_[5] -= rhs.m_[5]; m_[6] -= rhs.m_[6]; m_[7] -= rhs.m_[7]; m_[8] -= rhs.m_[8]; return *this; } // Subtract another matrix from a copy of this. Return the copy. const tmat3 operator-(const tmat3& rhs) { return tmat3(*this) -= rhs; } // Multiply this by another matrix. Return a reference to this. tmat3& operator*=(const tmat3& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[3] * rhs.m_[1]) + (m_[6] * rhs.m_[2])); T c0r1((m_[1] * rhs.m_[0]) + (m_[4] * rhs.m_[1]) + (m_[7] * rhs.m_[2])); T c0r2((m_[2] * rhs.m_[0]) + (m_[5] * rhs.m_[1]) + (m_[8] * rhs.m_[2])); T c1r0((m_[0] * rhs.m_[3]) + (m_[3] * rhs.m_[4]) + (m_[6] * rhs.m_[5])); T c1r1((m_[1] * rhs.m_[3]) + (m_[4] * rhs.m_[4]) + (m_[7] * rhs.m_[5])); T c1r2((m_[2] * rhs.m_[3]) + (m_[5] * rhs.m_[4]) + (m_[8] * rhs.m_[5])); T c2r0((m_[0] * rhs.m_[6]) + (m_[3] * rhs.m_[7]) + (m_[6] * rhs.m_[8])); T c2r1((m_[1] * rhs.m_[6]) + (m_[4] * rhs.m_[7]) + (m_[7] * rhs.m_[8])); T c2r2((m_[2] * rhs.m_[6]) + (m_[5] * rhs.m_[7]) + (m_[8] * rhs.m_[8])); m_[0] = c0r0; m_[1] = c0r1; m_[2] = c0r2; m_[3] = c1r0; m_[4] = c1r1; m_[5] = c1r2; m_[6] = c2r0; m_[7] = c2r1; m_[8] = c2r2; return *this; } // Multiply a copy of this by another matrix. Return the copy. const tmat3 operator*(const tmat3& rhs) { return tmat3(*this) *= rhs; } // Multiply this by a scalar. Return a reference to this. tmat3& operator*=(const T& rhs) { m_[0] *= rhs; m_[1] *= rhs; m_[2] *= rhs; m_[3] *= rhs; m_[4] *= rhs; m_[5] *= rhs; m_[6] *= rhs; m_[7] *= rhs; m_[8] *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tmat3 operator*(const T& rhs) { return tmat3(*this) *= rhs; } // Divide this by a scalar. Return a reference to this. tmat3& operator/=(const T& rhs) { m_[0] /= rhs; m_[1] /= rhs; m_[2] /= rhs; m_[3] /= rhs; m_[4] /= rhs; m_[5] /= rhs; m_[6] /= rhs; m_[7] /= rhs; m_[8] /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tmat3 operator/(const T& rhs) { return tmat3(*this) /= rhs; } // Use an instance of the ArrayProxy class to support double-indexed // references to a matrix (i.e., m[1][1]). See comments above the // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); } const ArrayProxy operator[](int index) const { return ArrayProxy(const_cast(&m_[index])); } private: T m_[9]; }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template const tmat3 operator*(const T& lhs, const tmat3& rhs) { return tmat3(rhs) * lhs; } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template const tvec3 operator*(const tvec3& lhs, const tmat3& rhs) { T x((lhs.x() * rhs[0][0]) + (lhs.y() * rhs[1][0]) + (lhs.z() * rhs[2][0])); T y((lhs.x() * rhs[0][1]) + (lhs.y() * rhs[1][1]) + (lhs.z() * rhs[2][1])); T z((lhs.x() * rhs[0][2]) + (lhs.y() * rhs[1][2]) + (lhs.z() * rhs[2][2])); return tvec3(x, y, z); } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template const tvec3 operator*(const tmat3& lhs, const tvec3& rhs) { T x((lhs[0][0] * rhs.x()) + (lhs[0][1] * rhs.y()) + (lhs[0][2] * rhs.z())); T y((lhs[1][0] * rhs.x()) + (lhs[1][1] * rhs.y()) + (lhs[1][2] * rhs.z())); T z((lhs[2][0] * rhs.x()) + (lhs[2][1] * rhs.y()) + (lhs[2][2] * rhs.z())); return tvec3(x, y, z); } // Compute the outer product of two vectors. Return the resultant matrix. template const tmat3 outer(const tvec3& a, const tvec3& b) { tmat3 product; product[0][0] = a.x() * b.x(); product[0][1] = a.x() * b.y(); product[0][2] = a.x() * b.z(); product[1][0] = a.y() * b.x(); product[1][1] = a.y() * b.y(); product[1][2] = a.y() * b.z(); product[2][0] = a.z() * b.x(); product[2][1] = a.z() * b.y(); product[2][2] = a.z() * b.z(); return product; } // A template class for creating, managing and operating on a 4x4 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template class tmat4 { public: tmat4() { setIdentity(); } tmat4(const tmat4& m) { m_[0] = m.m_[0]; m_[1] = m.m_[1]; m_[2] = m.m_[2]; m_[3] = m.m_[3]; m_[4] = m.m_[4]; m_[5] = m.m_[5]; m_[6] = m.m_[6]; m_[7] = m.m_[7]; m_[8] = m.m_[8]; m_[9] = m.m_[9]; m_[10] = m.m_[10]; m_[11] = m.m_[11]; m_[12] = m.m_[12]; m_[13] = m.m_[13]; m_[14] = m.m_[14]; m_[15] = m.m_[15]; } ~tmat4() {} // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; m_[1] = 0; m_[2] = 0; m_[3] = 0; m_[4] = 0; m_[5] = 1; m_[6] = 0; m_[7] = 0; m_[8] = 0; m_[9] = 0; m_[10] = 1; m_[11] = 0; m_[12] = 0; m_[13] = 0; m_[14] = 0; m_[15] = 1; } // Transpose this. Return a reference to this. tmat4& transpose() { T tmp_val = m_[1]; m_[1] = m_[4]; m_[4] = tmp_val; tmp_val = m_[2]; m_[2] = m_[8]; m_[8] = tmp_val; tmp_val = m_[3]; m_[3] = m_[12]; m_[12] = tmp_val; tmp_val = m_[6]; m_[6] = m_[9]; m_[9] = tmp_val; tmp_val = m_[7]; m_[7] = m_[13]; m_[13] = tmp_val; tmp_val = m_[11]; m_[11] = m_[14]; m_[14] = tmp_val; return *this; } // Compute the determinant of this and return it. T determinant() { tmat3 minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); tmat3 minor4(m_[1], m_[2], m_[3], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); tmat3 minor8(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[13], m_[14], m_[15]); tmat3 minor12(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[9], m_[10], m_[11]); return (m_[0] * minor0.determinant()) - (m_[4] * minor4.determinant()) + (m_[8] * minor8.determinant()) - (m_[12] * minor12.determinant()); } // Invert this. Return a reference to this. // // NOTE: If this is non-invertible, we will // throw to avoid undefined behavior. tmat4& inverse() #ifdef USE_EXCEPTIONS throw(std::runtime_error) #endif // USE_EXCEPTIONS { T d(determinant()); if (d == static_cast(0)) { #ifdef USE_EXCEPTIONS throw std::runtime_error("Matrix is noninvertible!!!!"); #else // !USE_EXCEPTIONS Log::error("Matrix is noninvertible!!!!\n"); return *this; #endif // USE_EXCEPTIONS } tmat3 minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); tmat3 minor1(m_[1], m_[2], m_[3], m_[13], m_[14], m_[15], m_[9], m_[10], m_[11]); tmat3 minor2(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[13], m_[14], m_[15]); tmat3 minor3(m_[1], m_[2], m_[3], m_[9], m_[10], m_[11], m_[5], m_[6], m_[7]); tmat3 minor4(m_[4], m_[6], m_[7], m_[12], m_[14], m_[15], m_[8], m_[10], m_[11]); tmat3 minor5(m_[0], m_[2], m_[3], m_[8], m_[10], m_[11], m_[12], m_[14], m_[15]); tmat3 minor6(m_[0], m_[2], m_[3], m_[12], m_[14], m_[15], m_[4], m_[6], m_[7]); tmat3 minor7(m_[0], m_[2], m_[3], m_[4], m_[6], m_[7], m_[8], m_[10], m_[11]); tmat3 minor8(m_[4], m_[5], m_[7], m_[8], m_[9], m_[11], m_[12], m_[13], m_[15]); tmat3 minor9(m_[0], m_[1], m_[3], m_[12], m_[13], m_[15], m_[8], m_[9], m_[11]); tmat3 minor10(m_[0], m_[1], m_[3], m_[4], m_[5], m_[7], m_[12], m_[13], m_[15]); tmat3 minor11(m_[0], m_[1], m_[3], m_[8], m_[9], m_[11], m_[4], m_[5], m_[7]); tmat3 minor12(m_[4], m_[5], m_[6], m_[12], m_[13], m_[14], m_[8], m_[9], m_[10]); tmat3 minor13(m_[0], m_[1], m_[2], m_[8], m_[9], m_[10], m_[12], m_[13], m_[14]); tmat3 minor14(m_[0], m_[1], m_[2], m_[12], m_[13], m_[14], m_[4], m_[5], m_[6]); tmat3 minor15(m_[0], m_[1], m_[2], m_[4], m_[5], m_[6], m_[8], m_[9], m_[10]); m_[0] = minor0.determinant() / d; m_[1] = minor1.determinant() / d; m_[2] = minor2.determinant() / d; m_[3] = minor3.determinant() / d; m_[4] = minor4.determinant() / d; m_[5] = minor5.determinant() / d; m_[6] = minor6.determinant() / d; m_[7] = minor7.determinant() / d; m_[8] = minor8.determinant() / d; m_[9] = minor9.determinant() / d; m_[10] = minor10.determinant() / d; m_[11] = minor11.determinant() / d; m_[12] = minor12.determinant() / d; m_[13] = minor13.determinant() / d; m_[14] = minor14.determinant() / d; m_[15] = minor15.determinant() / d; return *this; } // Print the elements of the matrix to standard out. // Really only useful for debug and test. void print() const { static const int precision(6); // row 0 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[4]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[8]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[12]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[5]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[9]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[13]; std::cout << " |" << std::endl; // row 2 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[6]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[10]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[14]; std::cout << " |" << std::endl; // row 3 std::cout << "| "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[7]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[11]; std::cout << " "; std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[15]; std::cout << " |" << std::endl; } // Allow raw data access for API calls and the like. // For example, it is valid to pass a tmat4 into a call to // the OpenGL command "glUniformMatrix4fv()". operator const T*() const { return &m_[0];} // Test if 'rhs' is equal to this. bool operator==(const tmat4& rhs) const { return m_[0] == rhs.m_[0] && m_[1] == rhs.m_[1] && m_[2] == rhs.m_[2] && m_[3] == rhs.m_[3] && m_[4] == rhs.m_[4] && m_[5] == rhs.m_[5] && m_[6] == rhs.m_[6] && m_[7] == rhs.m_[7] && m_[8] == rhs.m_[8] && m_[9] == rhs.m_[9] && m_[10] == rhs.m_[10] && m_[11] == rhs.m_[11] && m_[12] == rhs.m_[12] && m_[13] == rhs.m_[13] && m_[14] == rhs.m_[14] && m_[15] == rhs.m_[15]; } // Test if 'rhs' is not equal to this. bool operator!=(const tmat4& rhs) const { return !(*this == rhs); } // A direct assignment of 'rhs' to this. Return a reference to this. tmat4& operator=(const tmat4& rhs) { if (this != &rhs) { m_[0] = rhs.m_[0]; m_[1] = rhs.m_[1]; m_[2] = rhs.m_[2]; m_[3] = rhs.m_[3]; m_[4] = rhs.m_[4]; m_[5] = rhs.m_[5]; m_[6] = rhs.m_[6]; m_[7] = rhs.m_[7]; m_[8] = rhs.m_[8]; m_[9] = rhs.m_[9]; m_[10] = rhs.m_[10]; m_[11] = rhs.m_[11]; m_[12] = rhs.m_[12]; m_[13] = rhs.m_[13]; m_[14] = rhs.m_[14]; m_[15] = rhs.m_[15]; } return *this; } // Add another matrix to this. Return a reference to this. tmat4& operator+=(const tmat4& rhs) { m_[0] += rhs.m_[0]; m_[1] += rhs.m_[1]; m_[2] += rhs.m_[2]; m_[3] += rhs.m_[3]; m_[4] += rhs.m_[4]; m_[5] += rhs.m_[5]; m_[6] += rhs.m_[6]; m_[7] += rhs.m_[7]; m_[8] += rhs.m_[8]; m_[9] += rhs.m_[9]; m_[10] += rhs.m_[10]; m_[11] += rhs.m_[11]; m_[12] += rhs.m_[12]; m_[13] += rhs.m_[13]; m_[14] += rhs.m_[14]; m_[15] += rhs.m_[15]; return *this; } // Add another matrix to a copy of this. Return the copy. const tmat4 operator+(const tmat4& rhs) { return tmat4(*this) += rhs; } // Subtract another matrix from this. Return a reference to this. tmat4& operator-=(const tmat4& rhs) { m_[0] -= rhs.m_[0]; m_[1] -= rhs.m_[1]; m_[2] -= rhs.m_[2]; m_[3] -= rhs.m_[3]; m_[4] -= rhs.m_[4]; m_[5] -= rhs.m_[5]; m_[6] -= rhs.m_[6]; m_[7] -= rhs.m_[7]; m_[8] -= rhs.m_[8]; m_[9] -= rhs.m_[9]; m_[10] -= rhs.m_[10]; m_[11] -= rhs.m_[11]; m_[12] -= rhs.m_[12]; m_[13] -= rhs.m_[13]; m_[14] -= rhs.m_[14]; m_[15] -= rhs.m_[15]; return *this; } // Subtract another matrix from a copy of this. Return the copy. const tmat4 operator-(const tmat4& rhs) { return tmat4(*this) -= rhs; } // Multiply this by another matrix. Return a reference to this. tmat4& operator*=(const tmat4& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[4] * rhs.m_[1]) + (m_[8] * rhs.m_[2]) + (m_[12] * rhs.m_[3])); T c0r1((m_[1] * rhs.m_[0]) + (m_[5] * rhs.m_[1]) + (m_[9] * rhs.m_[2]) + (m_[13] * rhs.m_[3])); T c0r2((m_[2] * rhs.m_[0]) + (m_[6] * rhs.m_[1]) + (m_[10] * rhs.m_[2]) + (m_[14] * rhs.m_[3])); T c0r3((m_[3] * rhs.m_[0]) + (m_[7] * rhs.m_[1]) + (m_[11] * rhs.m_[2]) + (m_[15] * rhs.m_[3])); T c1r0((m_[0] * rhs.m_[4]) + (m_[4] * rhs.m_[5]) + (m_[8] * rhs.m_[6]) + (m_[12] * rhs.m_[7])); T c1r1((m_[1] * rhs.m_[4]) + (m_[5] * rhs.m_[5]) + (m_[9] * rhs.m_[6]) + (m_[13] * rhs.m_[7])); T c1r2((m_[2] * rhs.m_[4]) + (m_[6] * rhs.m_[5]) + (m_[10] * rhs.m_[6]) + (m_[14] * rhs.m_[7])); T c1r3((m_[3] * rhs.m_[4]) + (m_[7] * rhs.m_[5]) + (m_[11] * rhs.m_[6]) + (m_[15] * rhs.m_[7])); T c2r0((m_[0] * rhs.m_[8]) + (m_[4] * rhs.m_[9]) + (m_[8] * rhs.m_[10]) + (m_[12] * rhs.m_[11])); T c2r1((m_[1] * rhs.m_[8]) + (m_[5] * rhs.m_[9]) + (m_[9] * rhs.m_[10]) + (m_[13] * rhs.m_[11])); T c2r2((m_[2] * rhs.m_[8]) + (m_[6] * rhs.m_[9]) + (m_[10] * rhs.m_[10]) + (m_[14] * rhs.m_[11])); T c2r3((m_[3] * rhs.m_[8]) + (m_[7] * rhs.m_[9]) + (m_[11] * rhs.m_[10]) + (m_[15] * rhs.m_[11])); T c3r0((m_[0] * rhs.m_[12]) + (m_[4] * rhs.m_[13]) + (m_[8] * rhs.m_[14]) + (m_[12] * rhs.m_[15])); T c3r1((m_[1] * rhs.m_[12]) + (m_[5] * rhs.m_[13]) + (m_[9] * rhs.m_[14]) + (m_[13] * rhs.m_[15])); T c3r2((m_[2] * rhs.m_[12]) + (m_[6] * rhs.m_[13]) + (m_[10] * rhs.m_[14]) + (m_[14] * rhs.m_[15])); T c3r3((m_[3] * rhs.m_[12]) + (m_[7] * rhs.m_[13]) + (m_[11] * rhs.m_[14]) + (m_[15] * rhs.m_[15])); m_[0] = c0r0; m_[1] = c0r1; m_[2] = c0r2; m_[3] = c0r3; m_[4] = c1r0; m_[5] = c1r1; m_[6] = c1r2; m_[7] = c1r3; m_[8] = c2r0; m_[9] = c2r1; m_[10] = c2r2; m_[11] = c2r3; m_[12] = c3r0; m_[13] = c3r1; m_[14] = c3r2; m_[15] = c3r3; return *this; } // Multiply a copy of this by another matrix. Return the copy. const tmat4 operator*(const tmat4& rhs) { return tmat4(*this) *= rhs; } // Multiply this by a scalar. Return a reference to this. tmat4& operator*=(const T& rhs) { m_[0] *= rhs; m_[1] *= rhs; m_[2] *= rhs; m_[3] *= rhs; m_[4] *= rhs; m_[5] *= rhs; m_[6] *= rhs; m_[7] *= rhs; m_[8] *= rhs; m_[9] *= rhs; m_[10] *= rhs; m_[11] *= rhs; m_[12] *= rhs; m_[13] *= rhs; m_[14] *= rhs; m_[15] *= rhs; return *this; } // Multiply a copy of this by a scalar. Return the copy. const tmat4 operator*(const T& rhs) { return tmat4(*this) *= rhs; } // Divide this by a scalar. Return a reference to this. tmat4& operator/=(const T& rhs) { m_[0] /= rhs; m_[1] /= rhs; m_[2] /= rhs; m_[3] /= rhs; m_[4] /= rhs; m_[5] /= rhs; m_[6] /= rhs; m_[7] /= rhs; m_[8] /= rhs; m_[9] /= rhs; m_[10] /= rhs; m_[11] /= rhs; m_[12] /= rhs; m_[13] /= rhs; m_[14] /= rhs; m_[15] /= rhs; return *this; } // Divide a copy of this by a scalar. Return the copy. const tmat4 operator/(const T& rhs) { return tmat4(*this) /= rhs; } // Use an instance of the ArrayProxy class to support double-indexed // references to a matrix (i.e., m[1][1]). See comments above the // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); } const ArrayProxy operator[](int index) const { return ArrayProxy(const_cast(&m_[index])); } private: T m_[16]; }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template const tmat4 operator*(const T& lhs, const tmat4& rhs) { return tmat4(rhs) * lhs; } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template const tvec4 operator*(const tvec4& lhs, const tmat4& rhs) { T x((lhs.x() * rhs[0][0]) + (lhs.y() * rhs[1][0]) + (lhs.z() * rhs[2][0]) + (lhs.w() * rhs[3][0])); T y((lhs.x() * rhs[0][1]) + (lhs.y() * rhs[1][1]) + (lhs.z() * rhs[2][1]) + (lhs.w() * rhs[3][1])); T z((lhs.x() * rhs[0][2]) + (lhs.y() * rhs[1][2]) + (lhs.z() * rhs[2][2]) + (lhs.w() * rhs[3][2])); T w((lhs.x() * rhs[0][3]) + (lhs.y() * rhs[1][3]) + (lhs.z() * rhs[2][3]) + (lhs.w() * rhs[3][3])); return tvec4(x, y, z, w); } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template const tvec4 operator*(const tmat4& lhs, const tvec4& rhs) { T x((lhs[0][0] * rhs.x()) + (lhs[0][1] * rhs.y()) + (lhs[0][2] * rhs.z()) + (lhs[0][3] * rhs.w())); T y((lhs[1][0] * rhs.x()) + (lhs[1][1] * rhs.y()) + (lhs[1][2] * rhs.z()) + (lhs[1][3] * rhs.w())); T z((lhs[2][0] * rhs.x()) + (lhs[2][1] * rhs.y()) + (lhs[2][2] * rhs.z()) + (lhs[2][3] * rhs.w())); T w((lhs[3][0] * rhs.x()) + (lhs[3][1] * rhs.y()) + (lhs[3][2] * rhs.z()) + (lhs[3][3] * rhs.w())); return tvec4(x, y, z, w); } // Compute the outer product of two vectors. Return the resultant matrix. template const tmat4 outer(const tvec4& a, const tvec4& b) { tmat4 product; product[0][0] = a.x() * b.x(); product[0][1] = a.x() * b.y(); product[0][2] = a.x() * b.z(); product[0][3] = a.x() * b.w(); product[1][0] = a.y() * b.x(); product[1][1] = a.y() * b.y(); product[1][2] = a.y() * b.z(); product[1][3] = a.y() * b.w(); product[2][0] = a.z() * b.x(); product[2][1] = a.z() * b.y(); product[2][2] = a.z() * b.z(); product[2][3] = a.z() * b.w(); product[3][0] = a.w() * b.x(); product[3][1] = a.w() * b.y(); product[3][2] = a.w() * b.z(); product[3][3] = a.w() * b.w(); return product; } // // Convenience typedefs. These are here to present a homogeneous view of these // objects with respect to shader source. // typedef tmat2 mat2; typedef tmat3 mat3; typedef tmat4 mat4; typedef tmat2 dmat2; typedef tmat3 dmat3; typedef tmat4 dmat4; typedef tmat2 imat2; typedef tmat3 imat3; typedef tmat4 imat4; typedef tmat2 umat2; typedef tmat3 umat3; typedef tmat4 umat4; typedef tmat2 bmat2; typedef tmat3 bmat3; typedef tmat4 bmat4; namespace Mat4 { // // Some functions to generate transformation matrices that used to be provided // by OpenGL. // mat4 translate(float x, float y, float z); mat4 scale(float x, float y, float z); mat4 rotate(float angle, float x, float y, float z); mat4 frustum(float left, float right, float bottom, float top, float near, float far); mat4 ortho(float left, float right, float bottom, float top, float near, float far); mat4 perspective(float fovy, float aspect, float zNear, float zFar); mat4 lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ); } // namespace Mat4 } // namespace LibMatrix #endif // MAT_H_ glmark2-2012.08/./src/libmatrix/test/basic.frag0000664000175000017500000000010312013417376020373 0ustar alfalf00000000000000varying vec4 color; void main(void) { gl_FragColor = color; } glmark2-2012.08/./src/libmatrix/test/transpose_test.h0000664000175000017500000000173112013417376021707 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef TRANSPOSE_TEST_H_ #define TRANSPOSE_TEST_H_ class MatrixTest; class Options; class MatrixTest2x2Transpose : public MatrixTest { public: MatrixTest2x2Transpose() : MatrixTest("mat2::transpose") {} virtual void run(const Options& options); }; class MatrixTest3x3Transpose : public MatrixTest { public: MatrixTest3x3Transpose() : MatrixTest("mat3::transpose") {} virtual void run(const Options& options); }; class MatrixTest4x4Transpose : public MatrixTest { public: MatrixTest4x4Transpose() : MatrixTest("mat4::transpose") {} virtual void run(const Options& options); }; #endif // TRANSPOSE_TEST_H_ glmark2-2012.08/./src/libmatrix/test/inverse_test.h0000664000175000017500000000170212013417376021342 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef INVERSE_TEST_H_ #define INVERSE_TEST_H_ class MatrixTest; class Options; class MatrixTest2x2Inverse : public MatrixTest { public: MatrixTest2x2Inverse() : MatrixTest("mat2::inverse") {} virtual void run(const Options& options); }; class MatrixTest3x3Inverse : public MatrixTest { public: MatrixTest3x3Inverse() : MatrixTest("mat3::inverse") {} virtual void run(const Options& options); }; class MatrixTest4x4Inverse : public MatrixTest { public: MatrixTest4x4Inverse() : MatrixTest("mat4::inverse") {} virtual void run(const Options& options); }; #endif // INVERSE_TEST_H_ glmark2-2012.08/./src/libmatrix/test/libmatrix_test.cc0000664000175000017500000000372512013417376022027 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // Alexandros Frantzis - Util::split tests // #include #include #include #include "libmatrix_test.h" #include "inverse_test.h" #include "transpose_test.h" #include "const_vec_test.h" #include "shader_source_test.h" #include "util_split_test.h" using std::cerr; using std::cout; using std::endl; int main(int argc, char** argv) { Options testOptions("matrix_test"); testOptions.parseArgs(argc, argv); if (testOptions.showHelp()) { testOptions.printUsage(); return 0; } using std::vector; vector testVec; testVec.push_back(new MatrixTest2x2Inverse()); testVec.push_back(new MatrixTest3x3Inverse()); testVec.push_back(new MatrixTest4x4Inverse()); testVec.push_back(new MatrixTest2x2Transpose()); testVec.push_back(new MatrixTest3x3Transpose()); testVec.push_back(new MatrixTest4x4Transpose()); testVec.push_back(new ShaderSourceBasic()); testVec.push_back(new UtilSplitTestNormal()); testVec.push_back(new UtilSplitTestQuoted()); for (vector::iterator testIt = testVec.begin(); testIt != testVec.end(); testIt++) { MatrixTest* curTest = *testIt; if (testOptions.beVerbose()) { cout << "Running test " << curTest->name() << endl; } curTest->run(testOptions); if (!curTest->passed()) { cerr << curTest->name() << " does not work!" << endl; return 1; } if (testOptions.beVerbose()) { cout << curTest->name() << " is okay!" << endl; } } return 0; } glmark2-2012.08/./src/libmatrix/test/shader_source_test.h0000664000175000017500000000150712013417376022520 0ustar alfalf00000000000000// // Copyright (c) 2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef SHADER_SOURCE_TEST_H_ #define SHADER_SOURCE_TEST_H_ class MatrixTest; class Options; class ShaderSourceBasic : public MatrixTest { public: ShaderSourceBasic() : MatrixTest("ShaderSource::basic") {} virtual void run(const Options& options); }; class ShaderSourceAddConstGlobal : public MatrixTest { public: ShaderSourceAddConstGlobal() : MatrixTest("ShaderSource::AddConstGlobal") {} virtual void run(const Options& options); }; #endif // SHADER_SOURCE_TEST_H glmark2-2012.08/./src/libmatrix/test/shader_source_test.cc0000664000175000017500000000266712013417376022666 0ustar alfalf00000000000000// // Copyright (c) 2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include "libmatrix_test.h" #include "shader_source_test.h" #include "../shader-source.h" #include "../vec.h" using std::string; using LibMatrix::vec4; void ShaderSourceBasic::run(const Options& options) { static const string vtx_shader_filename("test/basic.vert"); ShaderSource vtx_source(vtx_shader_filename); ShaderSource vtx_source2(vtx_shader_filename); pass_ = (vtx_source.str() == vtx_source2.str()); } void ShaderSourceAddConstGlobal::run(const Options& options) { // Load the original shader source. static const string src_shader_filename("test/basic.vert"); ShaderSource src_shader(src_shader_filename); // Add constant at global scope static const vec4 constantColor(1.0, 1.0, 1.0, 1.0); src_shader.add_const("ConstantColor", constantColor); // Load the pre-modified shader static const string result_shader_filename("test/basic-global-const.vert"); ShaderSource result_shader(result_shader_filename); // Compare the output strings to confirm the results. pass_ = (src_shader.str() == result_shader.str()); } glmark2-2012.08/./src/libmatrix/test/basic-global-const.vert0000664000175000017500000000047112013417376023026 0ustar alfalf00000000000000const vec4 ConstantColor = vec4(1.000000, 1.000000, 1.000000, 1.000000); attribute vec3 position; uniform mat4 modelview; uniform mat4 projection; varying vec4 color; void main(void) { vec4 curVertex = vec4(position, 1.0); gl_Position = projection * modelview * curVertex; color = ConstantColor; } glmark2-2012.08/./src/libmatrix/test/const_vec_test.cc0000664000175000017500000000253312013417376022013 0ustar alfalf00000000000000// // Copyright (c) 2011 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include "libmatrix_test.h" #include "const_vec_test.h" #include "../vec.h" using LibMatrix::vec2; using LibMatrix::vec3; using LibMatrix::vec4; using std::cout; using std::endl; void Vec2TestConstOperator::run(const Options& options) { const vec2 a(1.0, 1.0); const vec2 b(2.0, 2.0); vec2 aplusb(a + b); vec2 aminusb(a - b); vec2 atimesb(a * b); vec2 adivb(a / b); const float s(2.5); vec2 stimesb(s * b); } void Vec3TestConstOperator::run(const Options& options) { const vec3 a(1.0, 1.0, 1.0); const vec3 b(2.0, 2.0, 2.0); vec3 aplusb(a + b); vec3 aminusb(a - b); vec3 atimesb(a * b); vec3 adivb(a / b); const float s(2.5); vec3 stimesb(s * b); } void Vec4TestConstOperator::run(const Options& options) { const vec4 a(1.0, 1.0, 1.0, 1.0); const vec4 b(2.0, 2.0, 2.0, 2.0); vec4 aplusb(a + b); vec4 aminusb(a - b); vec4 atimesb(a * b); vec4 adivb(a / b); const float s(2.5); vec4 stimesb(s * b); } glmark2-2012.08/./src/libmatrix/test/util_split_test.cc0000664000175000017500000000756212013417376022227 0ustar alfalf00000000000000// // Copyright (c) 2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis - original implementation. // #include #include #include #include "libmatrix_test.h" #include "util_split_test.h" #include "../util.h" using std::cout; using std::endl; using std::string; using std::vector; template static bool areVectorsEqual(vector& vec1, vector& vec2) { if (vec1.size() != vec2.size()) return false; for (unsigned int i = 0; i < vec1.size(); i++) { if (vec1[i] != vec2[i]) return false; } return true; } template static void printVector(vector& vec) { cout << "["; for (unsigned int i = 0; i < vec.size(); i++) { cout << '"' << vec[i] << '"'; if (i < vec.size() - 1) cout << ", "; } cout << "]"; } void UtilSplitTestNormal::run(const Options& options) { const string test1("abc def ghi"); const string test2(" abc: def :ghi "); vector expected1; vector expected2; vector results; expected1.push_back("abc"); expected1.push_back("def"); expected1.push_back("ghi"); expected2.push_back(" abc"); expected2.push_back(" def "); expected2.push_back("ghi "); if (options.beVerbose()) { cout << "Testing string \"" << test1 << "\"" << endl; } Util::split(test1, ' ', results, Util::SplitModeNormal); if (options.beVerbose()) { cout << "Split result: "; printVector(results); cout << endl << "Expected: "; printVector(expected1); cout << endl; } if (!areVectorsEqual(results, expected1)) { return; } results.clear(); if (options.beVerbose()) { cout << "Testing string \"" << test2 << "\"" << endl; } Util::split(test2, ':', results, Util::SplitModeNormal); if (options.beVerbose()) { cout << "Split result: "; printVector(results); cout << endl << "Expected: "; printVector(expected2); cout << endl; } if (!areVectorsEqual(results, expected2)) { return; } pass_ = true; } void UtilSplitTestQuoted::run(const Options& options) { const string test1("abc \"def' ghi\" klm\\ nop -b qr:title='123 \"456'"); const string test2("abc: def='1:2:3:'ghi : \":jk\""); vector expected1; vector expected2; vector results; expected1.push_back("abc"); expected1.push_back("def' ghi"); expected1.push_back("klm nop"); expected1.push_back("-b"); expected1.push_back("qr:title=123 \"456"); expected2.push_back("abc"); expected2.push_back(" def=1:2:3:ghi "); expected2.push_back(" :jk"); if (options.beVerbose()) { cout << "Testing string \"" << test1 << "\"" << endl; } Util::split(test1, ' ', results, Util::SplitModeQuoted); if (options.beVerbose()) { cout << "Split result: "; printVector(results); cout << endl << "Expected: "; printVector(expected1); cout << endl; } if (!areVectorsEqual(results, expected1)) { return; } results.clear(); if (options.beVerbose()) { cout << "Testing string \"" << test2 << "\"" << endl; } Util::split(test2, ':', results, Util::SplitModeQuoted); if (options.beVerbose()) { cout << "Split result: "; printVector(results); cout << endl << "Expected: "; printVector(expected2); cout << endl; } if (!areVectorsEqual(results, expected2)) { return; } pass_ = true; } glmark2-2012.08/./src/libmatrix/test/basic.vert0000664000175000017500000000036012013417376020441 0ustar alfalf00000000000000attribute vec3 position; uniform mat4 modelview; uniform mat4 projection; varying vec4 color; void main(void) { vec4 curVertex = vec4(position, 1.0); gl_Position = projection * modelview * curVertex; color = ConstantColor; } glmark2-2012.08/./src/libmatrix/test/options.cc0000664000175000017500000000367212013417376020471 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include #include #include "libmatrix_test.h" using std::cout; using std::endl; const std::string Options::verbose_name_("verbose"); const std::string Options::help_name_("help"); void Options::parseArgs(int argc, char** argv) { static struct option long_options[] = { {"verbose", 0, 0, 0}, {"help", 0, 0, 0}, {0, 0, 0, 0} }; int option_index(0); int c = getopt_long(argc, argv, "", long_options, &option_index); while (c != -1) { // getopt_long() returns '?' and prints an "unrecognized option" error // to stderr if it does not recognize an option. Just trigger // the help/usage message, stop processing and get out. if (c == '?') { show_help_ = true; break; } std::string optname(long_options[option_index].name); if (optname == verbose_name_) { verbose_ = true; } else if (optname == help_name_) { show_help_ = true; } c = getopt_long(argc, argv, "", long_options, &option_index); } } static void emitColumnOne(const std::string& text) { cout << std::setw(16) << text; } void Options::printUsage() { cout << app_name_ << ": directed functional test utility for libmatrix." << endl; cout << "Options:" << endl; emitColumnOne("--verbose"); cout << std::setw(0) << " Enable verbose output during test runs." << endl; emitColumnOne("--help"); cout << std::setw(0) << " Print this usage text." << endl; } glmark2-2012.08/./src/libmatrix/test/util_split_test.h0000664000175000017500000000146212013417376022062 0ustar alfalf00000000000000// // Copyright (c) 2012 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Alexandros Frantzis - original implementation. // #ifndef UTIL_SPLIT_TEST_H_ #define UTIL_SPLIT_TEST_H_ class MatrixTest; class Options; class UtilSplitTestNormal : public MatrixTest { public: UtilSplitTestNormal() : MatrixTest("Util::split::normal") {} virtual void run(const Options& options); }; class UtilSplitTestQuoted : public MatrixTest { public: UtilSplitTestQuoted() : MatrixTest("Util::split::quoted") {} virtual void run(const Options& options); }; #endif // UTIL_SPLIT_TEST_H_ glmark2-2012.08/./src/libmatrix/test/libmatrix_test.h0000664000175000017500000000241512013417376021664 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef LIBMATRIX_TEST_H_ #define LIBMATRIX_TEST_H_ class Options { Options(); static const std::string verbose_name_; static const std::string help_name_; std::string app_name_; bool show_help_; bool verbose_; public: Options(const std::string& app_name) : app_name_(app_name), show_help_(false), verbose_(false) {} ~Options() {} bool beVerbose() const { return verbose_; } bool showHelp() const { return show_help_; } void parseArgs(int argc, char** argv); void printUsage(); }; class MatrixTest { std::string name_; protected: bool pass_; MatrixTest(); public: MatrixTest(const std::string& name) : name_(name), pass_(false) {} ~MatrixTest(); const std::string& name() const { return name_; } virtual void run(const Options& options) = 0; const bool passed() const { return pass_; } }; #endif // LIBMATRIX_TEST_H_ glmark2-2012.08/./src/libmatrix/test/inverse_test.cc0000664000175000017500000000654012013417376021505 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include "libmatrix_test.h" #include "inverse_test.h" #include "../mat.h" using LibMatrix::mat2; using LibMatrix::mat3; using LibMatrix::mat4; using std::cout; using std::endl; void MatrixTest2x2Inverse::run(const Options& options) { mat2 m; if (options.beVerbose()) { cout << "Starting with mat2 (should be identity): " << endl << endl; m.print(); } m[0][1] = -2.5; if (options.beVerbose()) { cout << endl << "Matrix should now have (0, 1) == -2.500000" << endl << endl; m.print(); } mat2 mi(m); if (options.beVerbose()) { cout << endl << "Copy of previous matrix (should have (0, 1) == -2.500000)" << endl << endl; mi.print(); } mi.inverse(); if (options.beVerbose()) { cout << endl << "Inverse of copy: " << endl << endl; mi.print(); } mat2 i = m * mi; if (options.beVerbose()) { cout << endl << "Product of original and inverse (should be identity): " << endl << endl; i.print(); } mat2 ident; if (i == ident) { pass_ = true; } } void MatrixTest3x3Inverse::run(const Options& options) { mat3 m; if (options.beVerbose()) { cout << "Starting with mat3 (should be identity): " << endl << endl; m.print(); } m[1][2] = -2.5; if (options.beVerbose()) { cout << endl << "Matrix should now have (1, 2) == -2.500000" << endl << endl; m.print(); } mat3 mi(m); if (options.beVerbose()) { cout << endl << "Copy of previous matrix (should have (1, 2) == -2.500000)" << endl << endl; mi.print(); } mi.inverse(); if (options.beVerbose()) { cout << endl << "Inverse of copy: " << endl << endl; mi.print(); } mat3 i = m * mi; if (options.beVerbose()) { cout << endl << "Product of original and inverse (should be identity): " << endl << endl; i.print(); } mat3 ident; if (i == ident) { pass_ = true; } } void MatrixTest4x4Inverse::run(const Options& options) { mat4 m; if (options.beVerbose()) { cout << "Starting with mat4 (should be identity): " << endl << endl; m.print(); } m[2][3] = -2.5; if (options.beVerbose()) { cout << endl << "Matrix should now have (2, 3) == -2.500000" << endl << endl; m.print(); } mat4 mi(m); if (options.beVerbose()) { cout << endl << "Copy of previous matrix (should have (2, 3) == -2.500000)" << endl << endl; mi.print(); } mi.inverse(); if (options.beVerbose()) { cout << endl << "Inverse of copy: " << endl << endl; mi.print(); } mat4 i = m * mi; if (options.beVerbose()) { cout << endl << "Product of original and inverse (should be identity): " << endl << endl; i.print(); } mat4 ident; if (i == ident) { pass_ = true; } } glmark2-2012.08/./src/libmatrix/test/const_vec_test.h0000664000175000017500000000171012013417376021651 0ustar alfalf00000000000000// // Copyright (c) 2011 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef CONST_VEC_TEST_H_ #define CONST_VEC_TEST_H_ class MatrixTest; class Options; class Vec2TestConstOperator : public MatrixTest { public: Vec2TestConstOperator() : MatrixTest("vec2::const") {} virtual void run(const Options& options); }; class Vec3TestConstOperator : public MatrixTest { public: Vec3TestConstOperator() : MatrixTest("vec3::const") {} virtual void run(const Options& options); }; class Vec4TestConstOperator : public MatrixTest { public: Vec4TestConstOperator() : MatrixTest("vec4::const") {} virtual void run(const Options& options); }; #endif // CONST_VEC_TEST_H_ glmark2-2012.08/./src/libmatrix/test/transpose_test.cc0000664000175000017500000001574712013417376022061 0ustar alfalf00000000000000// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #include #include "libmatrix_test.h" #include "transpose_test.h" #include "../mat.h" using LibMatrix::mat2; using LibMatrix::mat3; using LibMatrix::mat4; using std::cout; using std::endl; void MatrixTest2x2Transpose::run(const Options& options) { // First, a simple test to ensure that the transpose of the identity is // the identity. if (options.beVerbose()) { cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; } mat2 m; if (options.beVerbose()) { cout << "Starting with mat2 (should be identity): " << endl << endl; m.print(); } m.transpose(); if (options.beVerbose()) { cout << endl << "Transpose of identity (should be identity): " << endl << endl; m.print(); } mat2 mi; if (m != mi) { // FAIL! Transpose of the identity is the identity. return; } // At this point, we have 2 identity matrices. // Next, set an element in the matrix and transpose twice. We should see // the original matrix (with i,j set). if (options.beVerbose()) { cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; } m[0][1] = 6.3; if (options.beVerbose()) { cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } mi = m; m.transpose().transpose(); if (options.beVerbose()) { cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } if (m != mi) { // FAIL! Transposing the same matrix twice should yield the original. return; } // Next, reset mi back to the identity. Set element element j,i in this // matrix and transpose m. They should now be equal. if (options.beVerbose()) { cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; } mi.setIdentity(); mi[1][0] = 6.3; m.transpose(); if (options.beVerbose()) { cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; m.print(); cout << endl; } if (m == mi) { pass_ = true; } // FAIL! Transposing the same matrix twice should yield the original. } void MatrixTest3x3Transpose::run(const Options& options) { // First, a simple test to ensure that the transpose of the identity is // the identity. if (options.beVerbose()) { cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; } mat3 m; if (options.beVerbose()) { cout << "Starting with mat2 (should be identity): " << endl << endl; m.print(); } m.transpose(); if (options.beVerbose()) { cout << endl << "Transpose of identity (should be identity): " << endl << endl; m.print(); } mat3 mi; if (m != mi) { // FAIL! Transpose of the identity is the identity. return; } // At this point, we have 2 identity matrices. // Next, set an element in the matrix and transpose twice. We should see // the original matrix (with i,j set). if (options.beVerbose()) { cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; } m[0][1] = 6.3; if (options.beVerbose()) { cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } mi = m; m.transpose().transpose(); if (options.beVerbose()) { cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } if (m != mi) { // FAIL! Transposing the same matrix twice should yield the original. return; } // Next, reset mi back to the identity. Set element element j,i in this // matrix and transpose m. They should now be equal. if (options.beVerbose()) { cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; } mi.setIdentity(); mi[1][0] = 6.3; m.transpose(); if (options.beVerbose()) { cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; m.print(); cout << endl; } if (m == mi) { pass_ = true; } // FAIL! Transposing the same matrix twice should yield the original. } void MatrixTest4x4Transpose::run(const Options& options) { // First, a simple test to ensure that the transpose of the identity is // the identity. if (options.beVerbose()) { cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; } mat4 m; if (options.beVerbose()) { cout << "Starting with mat2 (should be identity): " << endl << endl; m.print(); } m.transpose(); if (options.beVerbose()) { cout << endl << "Transpose of identity (should be identity): " << endl << endl; m.print(); } mat4 mi; if (m != mi) { // FAIL! Transpose of the identity is the identity. return; } // At this point, we have 2 identity matrices. // Next, set an element in the matrix and transpose twice. We should see // the original matrix (with i,j set). if (options.beVerbose()) { cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; } m[0][1] = 6.3; if (options.beVerbose()) { cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } mi = m; m.transpose().transpose(); if (options.beVerbose()) { cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; m.print(); } if (m != mi) { // FAIL! Transposing the same matrix twice should yield the original. return; } // Next, reset mi back to the identity. Set element element j,i in this // matrix and transpose m. They should now be equal. if (options.beVerbose()) { cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; } mi.setIdentity(); mi[1][0] = 6.3; m.transpose(); if (options.beVerbose()) { cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; m.print(); cout << endl; } if (m == mi) { pass_ = true; } // FAIL! Transposing the same matrix twice should yield the original. } glmark2-2012.08/./src/scene-terrain/renderer-chain.cpp0000664000175000017500000000500412013417376021635 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "renderer.h" void RendererChain::setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth) { static_cast(size); static_cast(onscreen); static_cast(has_depth); } void RendererChain::setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t) { static_cast(min_filter); static_cast(mag_filter); static_cast(wrap_s); static_cast(wrap_t); } void RendererChain::input_texture(GLuint t) { if (!renderers_.empty()) { IRenderer &renderer(*renderers_.front()); renderer.input_texture(t); } } GLuint RendererChain::texture() { GLuint t(0); if (!renderers_.empty()) { IRenderer &renderer(*renderers_.back()); t = renderer.texture(); } return t; } LibMatrix::vec2 RendererChain::size() { LibMatrix::vec2 s; if (!renderers_.empty()) { IRenderer &renderer(*renderers_.back()); s = renderer.size(); } return s; } void RendererChain::make_current() { if (!renderers_.empty()) { IRenderer &renderer(*renderers_.back()); renderer.make_current(); } } void RendererChain::update_mipmap() { if (!renderers_.empty()) { IRenderer &renderer(*renderers_.back()); renderer.update_mipmap(); } } void RendererChain::render() { for(std::vector::iterator iter = renderers_.begin(); iter != renderers_.end(); iter++) { (*iter)->render(); } } void RendererChain::append(IRenderer &renderer) { if (!renderers_.empty()) { IRenderer &prev_renderer(*renderers_.back()); renderer.input_texture(prev_renderer.texture()); } renderers_.push_back(&renderer); } glmark2-2012.08/./src/scene-terrain/simplex-noise-renderer.cpp0000664000175000017500000000342412013417376023353 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" LibMatrix::vec2 SimplexNoiseRenderer::uv_scale_(1.5f, 1.5); SimplexNoiseRenderer::SimplexNoiseRenderer(const LibMatrix::vec2 &size) : TextureRenderer(size, *noise_program(true)) { noise_program_ = noise_program(false); } Program * SimplexNoiseRenderer::noise_program(bool create_new) { static Program *noise_program(0); if (create_new) noise_program = 0; if (!noise_program) { noise_program = new Program(); ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain-noise.frag"); Scene::load_shaders_from_strings(*noise_program, vtx_shader.str(), frg_shader.str()); noise_program->start(); (*noise_program)["time"] = 1.0f; (*noise_program)["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); (*noise_program)["uvScale"] = uv_scale_; noise_program->stop(); } return noise_program; } glmark2-2012.08/./src/scene-terrain/terrain-renderer.cpp0000664000175000017500000001554212013417376022227 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "texture.h" #include "shader-source.h" TerrainRenderer::TerrainRenderer(const LibMatrix::vec2 &size, const LibMatrix::vec2 &repeat_overlay) : BaseRenderer(size), height_map_tex_(0), normal_map_tex_(0), specular_map_tex_(0), repeat_overlay_(repeat_overlay) { create_mesh(); init_textures(); init_program(); } TerrainRenderer::~TerrainRenderer() { deinit_textures(); } void TerrainRenderer::render() { make_current(); glClearColor(0.825f, 0.7425f, 0.61875f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); program_.start(); bind_textures(); mesh_.render_vbo(); program_.stop(); update_mipmap(); } void TerrainRenderer::init_textures() { /* Create textures */ Texture::load("terrain-grasslight-512", &diffuse1_tex_, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, 0); Texture::load("terrain-backgrounddetailed6", &diffuse2_tex_, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, 0); Texture::load("terrain-grasslight-512-nm", &detail_tex_, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, 0); /* Set REPEAT wrap mode */ glBindTexture(GL_TEXTURE_2D, diffuse1_tex_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, diffuse2_tex_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, detail_tex_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } void TerrainRenderer::init_program() { ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain.frag"); if (!Scene::load_shaders_from_strings(program_, vtx_shader.str(), frg_shader.str())) return; program_.start(); /* Fog */ program_["fogDensity"] = 0.00025f; program_["fogNear"] = 1.0f; program_["fogFar"] = 2000.0f; program_["fogColor"] = LibMatrix::vec3(0.825, 0.7425, 0.61875); /* Lights */ program_["ambientLightColor"] = color_to_vec3(0xffffff); program_["pointLightColor[0]"] = color_to_vec3(0xffffff); program_["pointLightPosition[0]"] = LibMatrix::vec3(0.0, 0.0, 0.0); program_["pointLightDistance[0]"] = 0.0f; /* Textures */ program_["tDiffuse1"] = 0; program_["tDiffuse2"] = 1; program_["tDetail"] = 2; program_["tNormal"] = 3; program_["tSpecular"] = 4; program_["tDisplacement"] = 5; program_["uNormalScale"] = 3.5f; program_["uDisplacementBias"] = 0.0f; program_["uDisplacementScale"] = 375.0f; program_["uDiffuseColor"] = color_to_vec3(0xffffff); program_["uSpecularColor"] = color_to_vec3(0xffffff); program_["uAmbientColor"] = color_to_vec3(0x888888); program_["uShininess"] = 30.0f; program_["uOpacity"] = 1.0f; program_["uRepeatOverlay"] = repeat_overlay_; program_["uOffset"] = LibMatrix::vec2(0.0, 0.0); std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); attrib_locations.push_back(program_["tangent"].location()); attrib_locations.push_back(program_["uv"].location()); mesh_.set_attrib_locations(attrib_locations); program_.stop(); } void TerrainRenderer::bind_textures() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, diffuse1_tex_); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, diffuse2_tex_); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, detail_tex_); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, normal_map_tex_); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, specular_map_tex_); glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_2D, height_map_tex_); } void TerrainRenderer::deinit_textures() { glDeleteTextures(1, &diffuse1_tex_); glDeleteTextures(1, &diffuse2_tex_); glDeleteTextures(1, &detail_tex_); } static void grid_conf(Mesh &mesh, int x, int y, int n_x, int n_y, LibMatrix::vec3 &ul, LibMatrix::vec3 &ll, LibMatrix::vec3 &ur, LibMatrix::vec3 &lr) { struct PlaneMeshVertex { LibMatrix::vec3 position; LibMatrix::vec2 texcoord; LibMatrix::vec3 normal; LibMatrix::vec3 tangent; }; LibMatrix::vec2 uv_ul(static_cast(x) / n_x, 1.0 - static_cast(y) / n_y); LibMatrix::vec2 uv_lr(static_cast(x + 1) / n_x, 1.0 - static_cast(y + 1) / n_y); LibMatrix::vec3 normal(0.0, 0.0, 1.0); LibMatrix::vec3 tangent(1.0, 0.0, 0.0); PlaneMeshVertex cell_vertices[] = { { ll, LibMatrix::vec2(uv_ul.x(), uv_lr.y()), normal, tangent }, { lr, LibMatrix::vec2(uv_lr.x(), uv_lr.y()), normal, tangent }, { ur, LibMatrix::vec2(uv_lr.x(), uv_ul.y()), normal, tangent }, { ul, LibMatrix::vec2(uv_ul.x(), uv_ul.y()), normal, tangent } }; unsigned int vertex_index[] = {0, 1, 2, 0, 2, 3}; for (size_t i = 0; i < sizeof(vertex_index) / sizeof(*vertex_index); i++) { PlaneMeshVertex& vertex = cell_vertices[vertex_index[i]]; mesh.next_vertex(); mesh.set_attrib(0, vertex.position); mesh.set_attrib(1, vertex.normal); mesh.set_attrib(2, vertex.tangent); mesh.set_attrib(3, vertex.texcoord); } } void TerrainRenderer::create_mesh() { std::vector vertex_format; vertex_format.push_back(3); vertex_format.push_back(3); vertex_format.push_back(3); vertex_format.push_back(2); mesh_.set_vertex_format(vertex_format); mesh_.make_grid(256, 256, 6000, 6000, 0, grid_conf); mesh_.build_vbo(); } glmark2-2012.08/./src/scene-terrain/renderer.h0000664000175000017500000002203612013417376020226 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include #include #include "mesh.h" #include "vec.h" #include "program.h" #include "gl-headers.h" /** * Renderer interface. */ class IRenderer { public: /** * Sets up the renderer's target. */ virtual void setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth) = 0; /** * Sets up the renderer's target texture (if any). */ virtual void setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t) = 0; /** * Sets the renderer's input texture. */ virtual void input_texture(GLuint t) = 0; /** * Gets the renderer's target texture (if any). */ virtual GLuint texture() = 0; /** * Gets the size of the renderer's target. */ virtual LibMatrix::vec2 size() = 0; /** * Makes the renderer current i.e. the rendering target. */ virtual void make_current() = 0; /** * Updates the mipmap of the texture backing the renderer (if any). */ virtual void update_mipmap() = 0; /** * Renders to the renderer's target. */ virtual void render() = 0; protected: virtual ~IRenderer() {} }; /** * A chain of renderers, which implements IRenderer */ class RendererChain : public IRenderer { public: RendererChain() {} virtual ~RendererChain() {} /* IRenderer methods */ void setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth); void setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t); void input_texture(GLuint t); GLuint texture(); LibMatrix::vec2 size(); void make_current(); void update_mipmap(); void render(); /** * Appends a renderer to the chain. * * @param renderer the renderer to append */ void append(IRenderer &renderer); private: std::vector renderers_; }; /** * A base implementation of the IRenderer interface. */ class BaseRenderer : public IRenderer { public: BaseRenderer(const LibMatrix::vec2 &size); virtual ~BaseRenderer(); /* IRenderer methods */ virtual void setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth); virtual void setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t); virtual void input_texture(GLuint t) { input_texture_ = t; } virtual GLuint texture() { return texture_; } virtual LibMatrix::vec2 size() { return size_; } virtual void make_current(); virtual void update_mipmap(); virtual void render() = 0; protected: void recreate(bool onscreen, bool has_depth); void create_texture(); void update_texture_parameters(); void create_fbo(bool has_depth); LibMatrix::vec2 size_; GLuint texture_; GLuint input_texture_; GLuint fbo_; GLuint depth_renderbuffer_; GLint min_filter_; GLint mag_filter_; GLint wrap_s_; GLint wrap_t_; }; /** * A renderer that renders its input texture to its target, * according to the supplied GL Program. */ class TextureRenderer : public BaseRenderer { public: TextureRenderer(const LibMatrix::vec2 &size, Program &program); virtual ~TextureRenderer() { } /* IRenderer/BaseRenderer methods */ virtual void render(); /** * Gets the program associated with the renderer. */ Program &program() { return program_; } private: void create_mesh(); Mesh mesh_; Program &program_; }; /** * A renderer that copies the input texture to its target. */ class CopyRenderer : public TextureRenderer { public: CopyRenderer(const LibMatrix::vec2 &size); virtual ~CopyRenderer() { delete copy_program_; } private: static Program *copy_program(bool create_new); Program *copy_program_; }; /** * A renderer that renders simplex noise to its target. */ class SimplexNoiseRenderer : public TextureRenderer { public: SimplexNoiseRenderer(const LibMatrix::vec2 &size); virtual ~SimplexNoiseRenderer() { delete noise_program_; } LibMatrix::vec2 uv_scale() { return uv_scale_; } private: static Program *noise_program(bool create_new); static LibMatrix::vec2 uv_scale_; Program *noise_program_; }; /** * A renderer that renders a normal map to its target from a * height map in its input texture. */ class NormalFromHeightRenderer : public TextureRenderer { public: NormalFromHeightRenderer(const LibMatrix::vec2 &size); virtual ~NormalFromHeightRenderer() { delete normal_from_height_program_; } private: static Program *normal_from_height_program(const LibMatrix::vec2 &size, bool create_new); Program *normal_from_height_program_; }; /** * A renderer that renders the luminance of its input texture to its target. */ class LuminanceRenderer : public TextureRenderer { public: LuminanceRenderer(const LibMatrix::vec2 &size); virtual ~LuminanceRenderer() { delete luminance_program_; } private: static Program *luminance_program(bool create_new); Program *luminance_program_; }; /** * A renderer that renders a blurred version of the input texture to its target. */ class BlurRenderer : public TextureRenderer { public: enum BlurDirection { BlurDirectionHorizontal, BlurDirectionVertical, BlurDirectionBoth }; BlurRenderer(const LibMatrix::vec2 &size, int radius, float sigma, BlurDirection dir, const LibMatrix::vec2 &step, float tilt_shift); virtual ~BlurRenderer() { delete blur_program_; } private: static Program *blur_program(bool create_new, int radius, float sigma, BlurDirection dir, const LibMatrix::vec2 &step, float tilt_shift); Program *blur_program_; }; /** * A renderer that renders with opacity (overlays) it's input texture over * the target of another renderer. */ class OverlayRenderer : public IRenderer { public: OverlayRenderer(IRenderer &target, GLfloat opacity); virtual ~OverlayRenderer() { } /* IRenderable Methods */ void setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth); void setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t); void input_texture(GLuint t) { input_texture_ = t; } virtual GLuint texture() { return target_renderer_.texture(); } virtual LibMatrix::vec2 size() { return target_renderer_.size(); } virtual void render(); void make_current(); void update_mipmap(); private: void create_mesh(); void create_program(); Mesh mesh_; Program program_; IRenderer &target_renderer_; GLfloat opacity_; GLuint input_texture_; }; /** * A renderer that renders a dynamic terrain as per the WebGL * dynamic terrain demo. */ class TerrainRenderer : public BaseRenderer { public: TerrainRenderer(const LibMatrix::vec2 &size, const LibMatrix::vec2 &repeat_overlay); virtual ~TerrainRenderer(); /* IRenderable Methods */ virtual void render(); /** * Gets the program associated with the renderer. */ Program &program() { return program_; } /** * Sets the height map texture to use. */ void height_map_texture(GLuint tex) { height_map_tex_ = tex; } /** * Sets the normal map texture to use. */ void normal_map_texture(GLuint tex) { normal_map_tex_ = tex; } /** * Sets the specular map texture to use. */ void specular_map_texture(GLuint tex) { specular_map_tex_ = tex; } /** * Returns the main diffuse texture. */ GLuint diffuse1_texture() { return diffuse1_tex_; } LibMatrix::vec2 repeat_overlay() { return repeat_overlay_; } private: void create_mesh(); void init_textures(); void init_program(); void bind_textures(); void deinit_textures(); LibMatrix::vec3 color_to_vec3(uint32_t c) { return LibMatrix::vec3(((c >> 0) & 0xff) / 255.0, ((c >> 8) & 0xff) / 255.0, ((c >> 16) & 0xff) / 255.0); } Mesh mesh_; Program program_; GLuint height_map_tex_; GLuint normal_map_tex_; GLuint specular_map_tex_; GLuint diffuse1_tex_; GLuint diffuse2_tex_; GLuint detail_tex_; LibMatrix::vec2 repeat_overlay_; }; glmark2-2012.08/./src/scene-terrain/luminance-renderer.cpp0000664000175000017500000000351412013417376022532 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" LuminanceRenderer::LuminanceRenderer(const LibMatrix::vec2 &size) : TextureRenderer(size, *luminance_program(true)) { luminance_program_ = luminance_program(false); } Program * LuminanceRenderer::luminance_program(bool create_new) { static Program *luminance_program(0); if (create_new) luminance_program = 0; if (!luminance_program) { luminance_program = new Program(); ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain-luminance.frag"); Scene::load_shaders_from_strings(*luminance_program, vtx_shader.str(), frg_shader.str()); luminance_program->start(); (*luminance_program)["tDiffuse"] = 0; (*luminance_program)["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); (*luminance_program)["uvScale"] = LibMatrix::vec2(1.0f, 1.0f); luminance_program->stop(); } return luminance_program; } glmark2-2012.08/./src/scene-terrain/base-renderer.cpp0000664000175000017500000000776612013417376021506 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "renderer.h" BaseRenderer::BaseRenderer(const LibMatrix::vec2 &size) : texture_(0), input_texture_(0), fbo_(0), depth_renderbuffer_(0), min_filter_(GL_LINEAR), mag_filter_(GL_LINEAR), wrap_s_(GL_CLAMP_TO_EDGE), wrap_t_(GL_CLAMP_TO_EDGE) { setup(size, true, true); } BaseRenderer::~BaseRenderer() { glDeleteTextures(1, &texture_); glDeleteRenderbuffers(1, &depth_renderbuffer_); glDeleteFramebuffers(1, &fbo_); } void BaseRenderer::setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth) { size_ = size; recreate(onscreen, has_depth); } void BaseRenderer::setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t) { min_filter_ = min_filter; mag_filter_ = mag_filter; wrap_s_ = wrap_s; wrap_t_ = wrap_t; update_texture_parameters(); } void BaseRenderer::make_current() { glBindFramebuffer(GL_FRAMEBUFFER, fbo_); glViewport(0, 0, size_.x(), size_.y()); if (!fbo_ || depth_renderbuffer_) { glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); } else { glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); } } void BaseRenderer::update_mipmap() { if (texture_ && min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR) { glBindTexture(GL_TEXTURE_2D, texture_); glGenerateMipmap(GL_TEXTURE_2D); } } void BaseRenderer::recreate(bool onscreen, bool has_depth) { if (texture_) { glDeleteTextures(1, &texture_); texture_ = 0; } if (fbo_) { glDeleteRenderbuffers(1, &depth_renderbuffer_); depth_renderbuffer_ = 0; glDeleteFramebuffers(1, &fbo_); fbo_ = 0; } if (!onscreen) { create_texture(); create_fbo(has_depth); } } void BaseRenderer::create_texture() { glGenTextures(1, &texture_); glBindTexture(GL_TEXTURE_2D, texture_); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.x(), size_.y(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); update_texture_parameters(); } void BaseRenderer::update_texture_parameters() { if (texture_) { glBindTexture(GL_TEXTURE_2D, texture_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t_); } update_mipmap(); } void BaseRenderer::create_fbo(bool has_depth) { if (has_depth) { /* Create a renderbuffer for depth storage */ glGenRenderbuffers(1, &depth_renderbuffer_); glBindRenderbuffer(GL_RENDERBUFFER, depth_renderbuffer_); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size_.x(), size_.y()); } /* Create the FBO and attach the texture and the renderebuffer */ glGenFramebuffers(1, &fbo_); glBindFramebuffer(GL_FRAMEBUFFER, fbo_); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0); if (has_depth) { glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_renderbuffer_); } glBindFramebuffer(GL_FRAMEBUFFER, 0); } glmark2-2012.08/./src/scene-terrain/copy-renderer.cpp0000664000175000017500000000336012013417376021530 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" CopyRenderer::CopyRenderer(const LibMatrix::vec2 &size) : TextureRenderer(size, *copy_program(true)) { copy_program_ = copy_program(false); } Program * CopyRenderer::copy_program(bool create_new) { static Program *copy_program(0); if (create_new) copy_program = 0; if (!copy_program) { copy_program = new Program(); ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain-overlay.frag"); Scene::load_shaders_from_strings(*copy_program, vtx_shader.str(), frg_shader.str()); copy_program->start(); (*copy_program)["tDiffuse"] = 0; (*copy_program)["opacity"] = 1.0f; (*copy_program)["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); (*copy_program)["uvScale"] = LibMatrix::vec2(1.0f, 1.0f); copy_program->stop(); } return copy_program; } glmark2-2012.08/./src/scene-terrain/blur-renderer.cpp0000664000175000017500000001353312013417376021525 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" void create_blur_shaders(ShaderSource& vtx_source, ShaderSource& frg_source, unsigned int radius, float sigma, BlurRenderer::BlurDirection direction, float tilt_shift); BlurRenderer::BlurRenderer(const LibMatrix::vec2 &size, int radius, float sigma, BlurDirection dir, const LibMatrix::vec2 &step, float tilt_shift) : TextureRenderer(size, *blur_program(true, radius, sigma, dir, step, tilt_shift)) { blur_program_ = blur_program(false, radius, sigma, dir, step, tilt_shift); } Program * BlurRenderer::blur_program(bool create_new, int radius, float sigma, BlurDirection dir, const LibMatrix::vec2 &step, float tilt_shift) { static Program *blur_program(0); if (create_new) blur_program = 0; if (!blur_program) { blur_program = new Program(); ShaderSource blur_vtx_shader; ShaderSource blur_frg_shader; create_blur_shaders(blur_vtx_shader, blur_frg_shader, radius, sigma, dir, tilt_shift); if (dir == BlurDirectionHorizontal || dir == BlurDirectionBoth) blur_frg_shader.add_const("TextureStepX", step.x()); if (dir == BlurDirectionVertical || dir == BlurDirectionBoth) blur_frg_shader.add_const("TextureStepY", step.y()); Scene::load_shaders_from_strings(*blur_program, blur_vtx_shader.str(), blur_frg_shader.str()); blur_program->start(); (*blur_program)["Texture0"] = 0; (*blur_program)["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); (*blur_program)["uvScale"] = LibMatrix::vec2(1.0f, 1.0f); blur_program->stop(); } return blur_program; } void create_blur_shaders(ShaderSource& vtx_source, ShaderSource& frg_source, unsigned int radius, float sigma, BlurRenderer::BlurDirection direction, float tilt_shift) { vtx_source.append_file(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); frg_source.append_file(GLMARK_DATA_PATH"/shaders/terrain-blur.frag"); /* Don't let the gaussian curve become too narrow */ if (sigma < 1.0) sigma = 1.0; unsigned int side = 2 * radius + 1; float values[radius]; float sum = 0.0; for (unsigned int i = 0; i < radius + 1; i++) { float s2 = 2.0 * sigma * sigma; float k = 1.0 / std::sqrt(M_PI * s2) * std::exp( - (static_cast(i) * i) / s2); values[i] = k; sum += k; } sum += sum - values[0]; for (unsigned int i = 0; i < radius + 1; i++) { std::stringstream ss_tmp; ss_tmp << "Kernel" << i; frg_source.add_const(ss_tmp.str(), values[i] / sum); } frg_source.add_const("TiltShift", tilt_shift); std::stringstream ss; if (direction == BlurRenderer::BlurDirectionHorizontal || direction == BlurRenderer::BlurDirectionBoth) { if (tilt_shift == 1.0) ss << "const float stepX = TextureStepX;" << std::endl; else ss << "float stepX = TextureStepX * abs(TiltShift - TextureCoord.y) / abs(1.0 - TiltShift);" << std::endl; } if (direction == BlurRenderer::BlurDirectionVertical || direction == BlurRenderer::BlurDirectionBoth) { if (tilt_shift == 1.0) ss << "const float stepY = TextureStepY;" << std::endl; else ss << "float stepY = TextureStepY * abs(TiltShift - TextureCoord.y) / abs(1.0 - TiltShift);" << std::endl; } ss << "result = " << std::endl; if (direction == BlurRenderer::BlurDirectionHorizontal) { for (unsigned int i = 0; i < side; i++) { int offset = static_cast(i - radius); ss << "texture2D(Texture0, TextureCoord + vec2(" << offset << ".0 * stepX, 0.0)) * Kernel" << std::abs(offset) << " +" << std::endl; } ss << "0.0 ;" << std::endl; } else if (direction == BlurRenderer::BlurDirectionVertical) { for (unsigned int i = 0; i < side; i++) { int offset = static_cast(i - radius); ss << "texture2D(Texture0, TextureCoord + vec2(0.0, " << offset << ".0 * stepY)) * Kernel" << std::abs(offset) << " +" << std::endl; } ss << "0.0 ;" << std::endl; } else if (direction == BlurRenderer::BlurDirectionBoth) { for (unsigned int i = 0; i < side; i++) { int ioffset = static_cast(i - radius); for (unsigned int j = 0; j < side; j++) { int joffset = static_cast(j - radius); ss << "texture2D(Texture0, TextureCoord + vec2(" << ioffset << ".0 * stepX, " << joffset << ".0 * stepY))" << " * Kernel" << std::abs(ioffset) << " * Kernel" << std::abs(joffset) << " +" << std::endl; } } ss << " 0.0;" << std::endl; } frg_source.replace("$CONVOLUTION$", ss.str()); } glmark2-2012.08/./src/scene-terrain/normal-from-height-renderer.cpp0000664000175000017500000000427312013417376024261 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" NormalFromHeightRenderer::NormalFromHeightRenderer(const LibMatrix::vec2 &size) : TextureRenderer(size, *normal_from_height_program(size, true)) { normal_from_height_program_ = normal_from_height_program(size, false); } Program * NormalFromHeightRenderer::normal_from_height_program(const LibMatrix::vec2 &size, bool create_new) { static Program *normal_from_height_program(0); if (create_new) normal_from_height_program = 0; if (!normal_from_height_program) { normal_from_height_program = new Program(); ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain-normalmap.frag"); Scene::load_shaders_from_strings(*normal_from_height_program, vtx_shader.str(), frg_shader.str()); normal_from_height_program->start(); (*normal_from_height_program)["heightMap"] = 0; (*normal_from_height_program)["resolution"] = size; (*normal_from_height_program)["height"] = 0.05f; (*normal_from_height_program)["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); (*normal_from_height_program)["uvScale"] = LibMatrix::vec2(1.0f, 1.0f); normal_from_height_program->stop(); } return normal_from_height_program; } glmark2-2012.08/./src/scene-terrain/texture-renderer.cpp0000664000175000017500000000336512013417376022263 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "renderer.h" TextureRenderer::TextureRenderer(const LibMatrix::vec2 &size, Program &program) : BaseRenderer(size), program_(program) { /* Create the mesh (quad) used for rendering */ create_mesh(); } void TextureRenderer::create_mesh() { // Set vertex format std::vector vertex_format; vertex_format.push_back(3); // Position mesh_.set_vertex_format(vertex_format); mesh_.make_grid(1, 1, 2.0, 2.0, 0); mesh_.build_vbo(); program_.start(); // Set attribute locations std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); program_.stop(); } void TextureRenderer::render() { make_current(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, input_texture_); program_.start(); mesh_.render_vbo(); program_.stop(); update_mipmap(); } glmark2-2012.08/./src/scene-terrain/overlay-renderer.cpp0000664000175000017500000000567412013417376022251 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ #include "scene.h" #include "renderer.h" #include "shader-source.h" OverlayRenderer::OverlayRenderer(IRenderer &target, GLfloat opacity) : target_renderer_(target), opacity_(opacity) { create_program(); create_mesh(); } void OverlayRenderer::setup(const LibMatrix::vec2 &size, bool onscreen, bool has_depth) { static_cast(size); static_cast(onscreen); static_cast(has_depth); } void OverlayRenderer::setup_texture(GLint min_filter, GLint mag_filter, GLint wrap_s, GLint wrap_t) { static_cast(min_filter); static_cast(mag_filter); static_cast(wrap_s); static_cast(wrap_t); } void OverlayRenderer::make_current() { target_renderer_.make_current(); } void OverlayRenderer::update_mipmap() { target_renderer_.update_mipmap(); } void OverlayRenderer::render() { target_renderer_.make_current(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, input_texture_); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); program_.start(); mesh_.render_vbo(); program_.stop(); glDisable(GL_BLEND); target_renderer_.update_mipmap(); } void OverlayRenderer::create_mesh() { // Set vertex format std::vector vertex_format; vertex_format.push_back(3); // Position mesh_.set_vertex_format(vertex_format); mesh_.make_grid(1, 1, 2.0, 2.0, 0); mesh_.build_vbo(); program_.start(); // Set attribute locations std::vector attrib_locations; attrib_locations.push_back(program_["position"].location()); mesh_.set_attrib_locations(attrib_locations); program_.stop(); } void OverlayRenderer::create_program() { ShaderSource vtx_shader(GLMARK_DATA_PATH"/shaders/terrain-texture.vert"); ShaderSource frg_shader(GLMARK_DATA_PATH"/shaders/terrain-overlay.frag"); if (!Scene::load_shaders_from_strings(program_, vtx_shader.str(), frg_shader.str())) return; program_.start(); program_["tDiffuse"] = 0; program_["opacity"] = opacity_; program_["uvOffset"] = LibMatrix::vec2(0.0f, 0.0f); program_["uvScale"] = LibMatrix::vec2(1.0f, 1.0f); program_.stop(); } glmark2-2012.08/./src/libpng/pnggccrd.c0000664000175000017500000001127012013417376016714 0ustar alfalf00000000000000/* pnggccrd.c was removed from libpng-1.2.20. */ /* This code snippet is for use by configure's compilation test. */ #if (!defined _MSC_VER) && \ defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ defined(PNG_MMX_CODE_SUPPORTED) int PNGAPI png_dummy_mmx_support(void); static int _mmx_supported = 2; // 0: no MMX; 1: MMX supported; 2: not tested int PNGAPI png_dummy_mmx_support(void) __attribute__((noinline)); int PNGAPI png_dummy_mmx_support(void) { int result; #ifdef PNG_MMX_CODE_SUPPORTED // superfluous, but what the heck __asm__ __volatile__ ( #ifdef __x86_64__ "pushq %%rbx \n\t" // rbx gets clobbered by CPUID instruction "pushq %%rcx \n\t" // so does rcx... "pushq %%rdx \n\t" // ...and rdx (but rcx & rdx safe on Linux) "pushfq \n\t" // save Eflag to stack "popq %%rax \n\t" // get Eflag from stack into rax "movq %%rax, %%rcx \n\t" // make another copy of Eflag in rcx "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21) "pushq %%rax \n\t" // save modified Eflag back to stack "popfq \n\t" // restore modified value to Eflag reg "pushfq \n\t" // save Eflag to stack "popq %%rax \n\t" // get Eflag from stack "pushq %%rcx \n\t" // save original Eflag to stack "popfq \n\t" // restore original Eflag #else "pushl %%ebx \n\t" // ebx gets clobbered by CPUID instruction "pushl %%ecx \n\t" // so does ecx... "pushl %%edx \n\t" // ...and edx (but ecx & edx safe on Linux) "pushfl \n\t" // save Eflag to stack "popl %%eax \n\t" // get Eflag from stack into eax "movl %%eax, %%ecx \n\t" // make another copy of Eflag in ecx "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21) "pushl %%eax \n\t" // save modified Eflag back to stack "popfl \n\t" // restore modified value to Eflag reg "pushfl \n\t" // save Eflag to stack "popl %%eax \n\t" // get Eflag from stack "pushl %%ecx \n\t" // save original Eflag to stack "popfl \n\t" // restore original Eflag #endif "xorl %%ecx, %%eax \n\t" // compare new Eflag with original Eflag "jz 0f \n\t" // if same, CPUID instr. is not supported "xorl %%eax, %%eax \n\t" // set eax to zero // ".byte 0x0f, 0xa2 \n\t" // CPUID instruction (two-byte opcode) "cpuid \n\t" // get the CPU identification info "cmpl $1, %%eax \n\t" // make sure eax return non-zero value "jl 0f \n\t" // if eax is zero, MMX is not supported "xorl %%eax, %%eax \n\t" // set eax to zero and... "incl %%eax \n\t" // ...increment eax to 1. This pair is // faster than the instruction "mov eax, 1" "cpuid \n\t" // get the CPU identification info again "andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23) "cmpl $0, %%edx \n\t" // 0 = MMX not supported "jz 0f \n\t" // non-zero = yes, MMX IS supported "movl $1, %%eax \n\t" // set return value to 1 "jmp 1f \n\t" // DONE: have MMX support "0: \n\t" // .NOT_SUPPORTED: target label for jump instructions "movl $0, %%eax \n\t" // set return value to 0 "1: \n\t" // .RETURN: target label for jump instructions #ifdef __x86_64__ "popq %%rdx \n\t" // restore rdx "popq %%rcx \n\t" // restore rcx "popq %%rbx \n\t" // restore rbx #else "popl %%edx \n\t" // restore edx "popl %%ecx \n\t" // restore ecx "popl %%ebx \n\t" // restore ebx #endif // "ret \n\t" // DONE: no MMX support // (fall through to standard C "ret") : "=a" (result) // output list : // any variables used on input (none) // no clobber list // , "%ebx", "%ecx", "%edx" // GRR: we handle these manually // , "memory" // if write to a variable gcc thought was in a reg // , "cc" // "condition codes" (flag bits) ); _mmx_supported = result; #else _mmx_supported = 0; #endif /* PNG_MMX_CODE_SUPPORTED */ return _mmx_supported; } #endif glmark2-2012.08/./src/libpng/pngerror.c0000664000175000017500000002702012013417376016763 0ustar alfalf00000000000000 /* pngerror.c - stub functions for i/o and memory allocation * * Last changed in libpng 1.2.45 [July 7, 2011] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file provides a location for all error handling. Users who * need special error handling are expected to write replacement functions * and use png_set_error_fn() to use those functions. See the instructions * at each function. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) static void /* PRIVATE */ png_default_error PNGARG((png_structp png_ptr, png_const_charp error_message)) PNG_NORETURN; #ifdef PNG_WARNINGS_SUPPORTED static void /* PRIVATE */ png_default_warning PNGARG((png_structp png_ptr, png_const_charp warning_message)); #endif /* PNG_WARNINGS_SUPPORTED */ /* This function is called whenever there is a fatal error. This function * should not be changed. If there is a need to handle errors differently, * you should supply a replacement error function and use png_set_error_fn() * to replace the error function at run-time. */ #ifdef PNG_ERROR_TEXT_SUPPORTED void PNGAPI png_error(png_structp png_ptr, png_const_charp error_message) { #ifdef PNG_ERROR_NUMBERS_SUPPORTED char msg[16]; if (png_ptr != NULL) { if (png_ptr->flags& (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) { if (*error_message == PNG_LITERAL_SHARP) { /* Strip "#nnnn " from beginning of error message. */ int offset; for (offset = 1; offset<15; offset++) if (error_message[offset] == ' ') break; if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) { int i; for (i = 0; i < offset - 1; i++) msg[i] = error_message[i + 1]; msg[i - 1] = '\0'; error_message = msg; } else error_message += offset; } else { if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) { msg[0] = '0'; msg[1] = '\0'; error_message = msg; } } } } #endif if (png_ptr != NULL && png_ptr->error_fn != NULL) (*(png_ptr->error_fn))(png_ptr, error_message); /* If the custom handler doesn't exist, or if it returns, use the default handler, which will not return. */ png_default_error(png_ptr, error_message); } #else void PNGAPI png_err(png_structp png_ptr) { /* Prior to 1.2.45 the error_fn received a NULL pointer, expressed * erroneously as '\0', instead of the empty string "". This was * apparently an error, introduced in libpng-1.2.20, and png_default_error * will crash in this case. */ if (png_ptr != NULL && png_ptr->error_fn != NULL) (*(png_ptr->error_fn))(png_ptr, ""); /* If the custom handler doesn't exist, or if it returns, use the default handler, which will not return. */ png_default_error(png_ptr, ""); } #endif /* PNG_ERROR_TEXT_SUPPORTED */ #ifdef PNG_WARNINGS_SUPPORTED /* This function is called whenever there is a non-fatal error. This function * should not be changed. If there is a need to handle warnings differently, * you should supply a replacement warning function and use * png_set_error_fn() to replace the warning function at run-time. */ void PNGAPI png_warning(png_structp png_ptr, png_const_charp warning_message) { int offset = 0; if (png_ptr != NULL) { #ifdef PNG_ERROR_NUMBERS_SUPPORTED if (png_ptr->flags& (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) #endif { if (*warning_message == PNG_LITERAL_SHARP) { for (offset = 1; offset < 15; offset++) if (warning_message[offset] == ' ') break; } } } if (png_ptr != NULL && png_ptr->warning_fn != NULL) (*(png_ptr->warning_fn))(png_ptr, warning_message + offset); else png_default_warning(png_ptr, warning_message + offset); } #endif /* PNG_WARNINGS_SUPPORTED */ #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI png_benign_error(png_structp png_ptr, png_const_charp error_message) { if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) png_warning(png_ptr, error_message); else png_error(png_ptr, error_message); } #endif /* These utilities are used internally to build an error message that relates * to the current chunk. The chunk name comes from png_ptr->chunk_name, * this is used to prefix the message. The message is limited in length * to 63 bytes, the name characters are output as hex digits wrapped in [] * if the character is invalid. */ #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) static PNG_CONST char png_digit[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; #define PNG_MAX_ERROR_TEXT 64 #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) static void /* PRIVATE */ png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp error_message) { int iout = 0, iin = 0; while (iin < 4) { int c = png_ptr->chunk_name[iin++]; if (isnonalpha(c)) { buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; buffer[iout++] = png_digit[(c & 0xf0) >> 4]; buffer[iout++] = png_digit[c & 0x0f]; buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET; } else { buffer[iout++] = (png_byte)c; } } if (error_message == NULL) buffer[iout] = '\0'; else { buffer[iout++] = ':'; buffer[iout++] = ' '; iin = 0; while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0') buffer[iout++] = error_message[iin++]; /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */ buffer[iout] = '\0'; } } #ifdef PNG_READ_SUPPORTED void PNGAPI png_chunk_error(png_structp png_ptr, png_const_charp error_message) { char msg[18+PNG_MAX_ERROR_TEXT]; if (png_ptr == NULL) png_error(png_ptr, error_message); else { png_format_buffer(png_ptr, msg, error_message); png_error(png_ptr, msg); } } #endif /* PNG_READ_SUPPORTED */ #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */ #ifdef PNG_WARNINGS_SUPPORTED void PNGAPI png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) { char msg[18+PNG_MAX_ERROR_TEXT]; if (png_ptr == NULL) png_warning(png_ptr, warning_message); else { png_format_buffer(png_ptr, msg, warning_message); png_warning(png_ptr, msg); } } #endif /* PNG_WARNINGS_SUPPORTED */ #ifdef PNG_READ_SUPPORTED #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message) { if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) png_chunk_warning(png_ptr, error_message); else png_chunk_error(png_ptr, error_message); } #endif #endif /* PNG_READ_SUPPORTED */ /* This is the default error handling function. Note that replacements for * this function MUST NOT RETURN, or the program will likely crash. This * function is used by default, or if the program supplies NULL for the * error function pointer in png_set_error_fn(). */ static void /* PRIVATE */ png_default_error(png_structp png_ptr, png_const_charp error_message) { #ifdef PNG_CONSOLE_IO_SUPPORTED #ifdef PNG_ERROR_NUMBERS_SUPPORTED if (*error_message == PNG_LITERAL_SHARP) { /* Strip "#nnnn " from beginning of error message. */ int offset; char error_number[16]; for (offset = 0; offset<15; offset++) { error_number[offset] = error_message[offset + 1]; if (error_message[offset] == ' ') break; } if ((offset > 1) && (offset < 15)) { error_number[offset - 1] = '\0'; fprintf(stderr, "libpng error no. %s: %s", error_number, error_message + offset + 1); fprintf(stderr, PNG_STRING_NEWLINE); } else { fprintf(stderr, "libpng error: %s, offset=%d", error_message, offset); fprintf(stderr, PNG_STRING_NEWLINE); } } else #endif { fprintf(stderr, "libpng error: %s", error_message); fprintf(stderr, PNG_STRING_NEWLINE); } #endif #ifdef PNG_SETJMP_SUPPORTED if (png_ptr) { # ifdef USE_FAR_KEYWORD { jmp_buf jmpbuf; png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf)); longjmp(jmpbuf,1); } # else longjmp(png_ptr->jmpbuf, 1); # endif } #endif /* Here if not setjmp support or if png_ptr is null. */ PNG_ABORT(); #ifndef PNG_CONSOLE_IO_SUPPORTED error_message = error_message; /* Make compiler happy */ #endif } #ifdef PNG_WARNINGS_SUPPORTED /* This function is called when there is a warning, but the library thinks * it can continue anyway. Replacement functions don't have to do anything * here if you don't want them to. In the default configuration, png_ptr is * not used, but it is passed in case it may be useful. */ static void /* PRIVATE */ png_default_warning(png_structp png_ptr, png_const_charp warning_message) { #ifdef PNG_CONSOLE_IO_SUPPORTED # ifdef PNG_ERROR_NUMBERS_SUPPORTED if (*warning_message == PNG_LITERAL_SHARP) { int offset; char warning_number[16]; for (offset = 0; offset < 15; offset++) { warning_number[offset] = warning_message[offset + 1]; if (warning_message[offset] == ' ') break; } if ((offset > 1) && (offset < 15)) { warning_number[offset + 1] = '\0'; fprintf(stderr, "libpng warning no. %s: %s", warning_number, warning_message + offset); fprintf(stderr, PNG_STRING_NEWLINE); } else { fprintf(stderr, "libpng warning: %s", warning_message); fprintf(stderr, PNG_STRING_NEWLINE); } } else # endif { fprintf(stderr, "libpng warning: %s", warning_message); fprintf(stderr, PNG_STRING_NEWLINE); } #else warning_message = warning_message; /* Make compiler happy */ #endif png_ptr = png_ptr; /* Make compiler happy */ } #endif /* PNG_WARNINGS_SUPPORTED */ /* This function is called when the application wants to use another method * of handling errors and warnings. Note that the error function MUST NOT * return to the calling routine or serious problems will occur. The return * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1) */ void PNGAPI png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn) { if (png_ptr == NULL) return; png_ptr->error_ptr = error_ptr; png_ptr->error_fn = error_fn; png_ptr->warning_fn = warning_fn; } /* This function returns a pointer to the error_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy and png_read_destroy are called. */ png_voidp PNGAPI png_get_error_ptr(png_structp png_ptr) { if (png_ptr == NULL) return NULL; return ((png_voidp)png_ptr->error_ptr); } #ifdef PNG_ERROR_NUMBERS_SUPPORTED void PNGAPI png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) { if (png_ptr != NULL) { png_ptr->flags &= ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); } } #endif #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/KNOWNBUG0000664000175000017500000000152012013417376016133 0ustar alfalf00000000000000 Known bugs in libpng version 1.2.46 1. February 23, 2006: The custom makefiles don't build libpng with -lz. STATUS: This is a subject of debate. The change will probably be made as a part of a major overhaul of the makefiles in libpng version 1.4.0. 2. February 24, 2006: The Makefile generated by the "configure" script fails to install symbolic links libpng12.so => libpng12.so.0.1.2.9betaN that are generated by the custom makefiles. 3. September 4, 2007: There is a report that pngtest crashes on MacOS 10. STATUS: workarounds are 1) Compile without optimization (crashes are observed with -arch i386 and -O2 or -O3, using gcc-4.0.1). 2) Compile pngtest.c with PNG_DEBUG defined (the bug goes away if you try to look at it). 3) Ignore the crash. The library itself seems to be OK. glmark2-2012.08/./src/libpng/pngwtran.c0000664000175000017500000004164712013417376017000 0ustar alfalf00000000000000 /* pngwtran.c - transforms the data in a row for PNG writers * * Last changed in libpng 1.2.43 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_WRITE_SUPPORTED /* Transform the data according to the user's wishes. The order of * transformations is significant. */ void /* PRIVATE */ png_do_write_transformations(png_structp png_ptr) { png_debug(1, "in png_do_write_transformations"); if (png_ptr == NULL) return; #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED if (png_ptr->transformations & PNG_USER_TRANSFORM) if (png_ptr->write_user_transform_fn != NULL) (*(png_ptr->write_user_transform_fn)) /* User write transform function */ (png_ptr, /* png_ptr */ &(png_ptr->row_info), /* row_info: */ /* png_uint_32 width; width of row */ /* png_uint_32 rowbytes; number of bytes in row */ /* png_byte color_type; color type of pixels */ /* png_byte bit_depth; bit depth of samples */ /* png_byte channels; number of channels (1-4) */ /* png_byte pixel_depth; bits per pixel (depth*channels) */ png_ptr->row_buf + 1); /* start of pixel data for row */ #endif #ifdef PNG_WRITE_FILLER_SUPPORTED if (png_ptr->transformations & PNG_FILLER) png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->flags); #endif #ifdef PNG_WRITE_PACKSWAP_SUPPORTED if (png_ptr->transformations & PNG_PACKSWAP) png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_WRITE_PACK_SUPPORTED if (png_ptr->transformations & PNG_PACK) png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, (png_uint_32)png_ptr->bit_depth); #endif #ifdef PNG_WRITE_SWAP_SUPPORTED if (png_ptr->transformations & PNG_SWAP_BYTES) png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_WRITE_SHIFT_SUPPORTED if (png_ptr->transformations & PNG_SHIFT) png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, &(png_ptr->shift)); #endif #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED if (png_ptr->transformations & PNG_SWAP_ALPHA) png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED if (png_ptr->transformations & PNG_INVERT_ALPHA) png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_WRITE_BGR_SUPPORTED if (png_ptr->transformations & PNG_BGR) png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_WRITE_INVERT_SUPPORTED if (png_ptr->transformations & PNG_INVERT_MONO) png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif } #ifdef PNG_WRITE_PACK_SUPPORTED /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The * row_info bit depth should be 8 (one pixel per byte). The channels * should be 1 (this only happens on grayscale and paletted images). */ void /* PRIVATE */ png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) { png_debug(1, "in png_do_pack"); if (row_info->bit_depth == 8 && #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif row_info->channels == 1) { switch ((int)bit_depth) { case 1: { png_bytep sp, dp; int mask, v; png_uint_32 i; png_uint_32 row_width = row_info->width; sp = row; dp = row; mask = 0x80; v = 0; for (i = 0; i < row_width; i++) { if (*sp != 0) v |= mask; sp++; if (mask > 1) mask >>= 1; else { mask = 0x80; *dp = (png_byte)v; dp++; v = 0; } } if (mask != 0x80) *dp = (png_byte)v; break; } case 2: { png_bytep sp, dp; int shift, v; png_uint_32 i; png_uint_32 row_width = row_info->width; sp = row; dp = row; shift = 6; v = 0; for (i = 0; i < row_width; i++) { png_byte value; value = (png_byte)(*sp & 0x03); v |= (value << shift); if (shift == 0) { shift = 6; *dp = (png_byte)v; dp++; v = 0; } else shift -= 2; sp++; } if (shift != 6) *dp = (png_byte)v; break; } case 4: { png_bytep sp, dp; int shift, v; png_uint_32 i; png_uint_32 row_width = row_info->width; sp = row; dp = row; shift = 4; v = 0; for (i = 0; i < row_width; i++) { png_byte value; value = (png_byte)(*sp & 0x0f); v |= (value << shift); if (shift == 0) { shift = 4; *dp = (png_byte)v; dp++; v = 0; } else shift -= 4; sp++; } if (shift != 4) *dp = (png_byte)v; break; } } row_info->bit_depth = (png_byte)bit_depth; row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); } } #endif #ifdef PNG_WRITE_SHIFT_SUPPORTED /* Shift pixel values to take advantage of whole range. Pass the * true number of bits in bit_depth. The row should be packed * according to row_info->bit_depth. Thus, if you had a row of * bit depth 4, but the pixels only had values from 0 to 7, you * would pass 3 as bit_depth, and this routine would translate the * data to 0 to 15. */ void /* PRIVATE */ png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) { png_debug(1, "in png_do_shift"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL && #else if ( #endif row_info->color_type != PNG_COLOR_TYPE_PALETTE) { int shift_start[4], shift_dec[4]; int channels = 0; if (row_info->color_type & PNG_COLOR_MASK_COLOR) { shift_start[channels] = row_info->bit_depth - bit_depth->red; shift_dec[channels] = bit_depth->red; channels++; shift_start[channels] = row_info->bit_depth - bit_depth->green; shift_dec[channels] = bit_depth->green; channels++; shift_start[channels] = row_info->bit_depth - bit_depth->blue; shift_dec[channels] = bit_depth->blue; channels++; } else { shift_start[channels] = row_info->bit_depth - bit_depth->gray; shift_dec[channels] = bit_depth->gray; channels++; } if (row_info->color_type & PNG_COLOR_MASK_ALPHA) { shift_start[channels] = row_info->bit_depth - bit_depth->alpha; shift_dec[channels] = bit_depth->alpha; channels++; } /* With low row depths, could only be grayscale, so one channel */ if (row_info->bit_depth < 8) { png_bytep bp = row; png_uint_32 i; png_byte mask; png_uint_32 row_bytes = row_info->rowbytes; if (bit_depth->gray == 1 && row_info->bit_depth == 2) mask = 0x55; else if (row_info->bit_depth == 4 && bit_depth->gray == 3) mask = 0x11; else mask = 0xff; for (i = 0; i < row_bytes; i++, bp++) { png_uint_16 v; int j; v = *bp; *bp = 0; for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) { if (j > 0) *bp |= (png_byte)((v << j) & 0xff); else *bp |= (png_byte)((v >> (-j)) & mask); } } } else if (row_info->bit_depth == 8) { png_bytep bp = row; png_uint_32 i; png_uint_32 istop = channels * row_info->width; for (i = 0; i < istop; i++, bp++) { png_uint_16 v; int j; int c = (int)(i%channels); v = *bp; *bp = 0; for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) { if (j > 0) *bp |= (png_byte)((v << j) & 0xff); else *bp |= (png_byte)((v >> (-j)) & 0xff); } } } else { png_bytep bp; png_uint_32 i; png_uint_32 istop = channels * row_info->width; for (bp = row, i = 0; i < istop; i++) { int c = (int)(i%channels); png_uint_16 value, v; int j; v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1)); value = 0; for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) { if (j > 0) value |= (png_uint_16)((v << j) & (png_uint_16)0xffff); else value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); } *bp++ = (png_byte)(value >> 8); *bp++ = (png_byte)(value & 0xff); } } } } #endif #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED void /* PRIVATE */ png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_write_swap_alpha"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { /* This converts from ARGB to RGBA */ if (row_info->bit_depth == 8) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { png_byte save = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = save; } } /* This converts from AARRGGBB to RRGGBBAA */ else { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { png_byte save[2]; save[0] = *(sp++); save[1] = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = save[0]; *(dp++) = save[1]; } } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { /* This converts from AG to GA */ if (row_info->bit_depth == 8) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { png_byte save = *(sp++); *(dp++) = *(sp++); *(dp++) = save; } } /* This converts from AAGG to GGAA */ else { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { png_byte save[2]; save[0] = *(sp++); save[1] = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = save[0]; *(dp++) = save[1]; } } } } } #endif #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED void /* PRIVATE */ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_write_invert_alpha"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { /* This inverts the alpha channel in RGBA */ if (row_info->bit_depth == 8) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { /* Does nothing *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); */ sp+=3; dp = sp; *(dp++) = (png_byte)(255 - *(sp++)); } } /* This inverts the alpha channel in RRGGBBAA */ else { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { /* Does nothing *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); *(dp++) = *(sp++); */ sp+=6; dp = sp; *(dp++) = (png_byte)(255 - *(sp++)); *(dp++) = (png_byte)(255 - *(sp++)); } } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { /* This inverts the alpha channel in GA */ if (row_info->bit_depth == 8) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { *(dp++) = *(sp++); *(dp++) = (png_byte)(255 - *(sp++)); } } /* This inverts the alpha channel in GGAA */ else { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width = row_info->width; for (i = 0, sp = dp = row; i < row_width; i++) { /* Does nothing *(dp++) = *(sp++); *(dp++) = *(sp++); */ sp+=2; dp = sp; *(dp++) = (png_byte)(255 - *(sp++)); *(dp++) = (png_byte)(255 - *(sp++)); } } } } } #endif #ifdef PNG_MNG_FEATURES_SUPPORTED /* Undoes intrapixel differencing */ void /* PRIVATE */ png_do_write_intrapixel(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_write_intrapixel"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif (row_info->color_type & PNG_COLOR_MASK_COLOR)) { int bytes_per_pixel; png_uint_32 row_width = row_info->width; if (row_info->bit_depth == 8) { png_bytep rp; png_uint_32 i; if (row_info->color_type == PNG_COLOR_TYPE_RGB) bytes_per_pixel = 3; else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) bytes_per_pixel = 4; else return; for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) { *(rp) = (png_byte)((*rp - *(rp+1))&0xff); *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff); } } else if (row_info->bit_depth == 16) { png_bytep rp; png_uint_32 i; if (row_info->color_type == PNG_COLOR_TYPE_RGB) bytes_per_pixel = 6; else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) bytes_per_pixel = 8; else return; for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) { png_uint_32 s0 = (*(rp ) << 8) | *(rp+1); png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3); png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5); png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); *(rp ) = (png_byte)((red >> 8) & 0xff); *(rp+1) = (png_byte)(red & 0xff); *(rp+4) = (png_byte)((blue >> 8) & 0xff); *(rp+5) = (png_byte)(blue & 0xff); } } } } #endif /* PNG_MNG_FEATURES_SUPPORTED */ #endif /* PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngconf.h0000664000175000017500000014130612013417376016570 0ustar alfalf00000000000000 /* pngconf.h - machine configurable file for libpng * * libpng version 1.2.46 - July 9, 2011 * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ /* Any machine specific code is near the front of this file, so if you * are configuring libpng for a machine, you may want to read the section * starting here down to where it starts to typedef png_color, png_text, * and png_info. */ #ifndef PNGCONF_H #define PNGCONF_H #define PNG_1_2_X /* * PNG_USER_CONFIG has to be defined on the compiler command line. This * includes the resource compiler for Windows DLL configurations. */ #ifdef PNG_USER_CONFIG # ifndef PNG_USER_PRIVATEBUILD # define PNG_USER_PRIVATEBUILD # endif #include "pngusr.h" #endif /* PNG_CONFIGURE_LIBPNG is set by the "configure" script. */ #ifdef PNG_CONFIGURE_LIBPNG #ifdef HAVE_CONFIG_H #include "config.h" #endif #endif /* * Added at libpng-1.2.8 * * If you create a private DLL you need to define in "pngusr.h" the followings: * #define PNG_USER_PRIVATEBUILD * e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons." * #define PNG_USER_DLLFNAME_POSTFIX * e.g. // private DLL "libpng13gx.dll" * #define PNG_USER_DLLFNAME_POSTFIX "gx" * * The following macros are also at your disposal if you want to complete the * DLL VERSIONINFO structure. * - PNG_USER_VERSIONINFO_COMMENTS * - PNG_USER_VERSIONINFO_COMPANYNAME * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS */ #ifdef __STDC__ #ifdef SPECIALBUILD # pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\ are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.") #endif #ifdef PRIVATEBUILD # pragma message("PRIVATEBUILD is deprecated.\ Use PNG_USER_PRIVATEBUILD instead.") # define PNG_USER_PRIVATEBUILD PRIVATEBUILD #endif #endif /* __STDC__ */ #ifndef PNG_VERSION_INFO_ONLY /* End of material added to libpng-1.2.8 */ /* Added at libpng-1.2.19, removed at libpng-1.2.20 because it caused trouble Restored at libpng-1.2.21 */ #if !defined(PNG_NO_WARN_UNINITIALIZED_ROW) && \ !defined(PNG_WARN_UNINITIALIZED_ROW) # define PNG_WARN_UNINITIALIZED_ROW 1 #endif /* End of material added at libpng-1.2.19/1.2.21 */ /* This is the size of the compression buffer, and thus the size of * an IDAT chunk. Make this whatever size you feel is best for your * machine. One of these will be allocated per png_struct. When this * is full, it writes the data to the disk, and does some other * calculations. Making this an extremely small size will slow * the library down, but you may want to experiment to determine * where it becomes significant, if you are concerned with memory * usage. Note that zlib allocates at least 32Kb also. For readers, * this describes the size of the buffer available to read the data in. * Unless this gets smaller than the size of a row (compressed), * it should not make much difference how big this is. */ #ifndef PNG_ZBUF_SIZE # define PNG_ZBUF_SIZE 8192 #endif /* Enable if you want a write-only libpng */ #ifndef PNG_NO_READ_SUPPORTED # define PNG_READ_SUPPORTED #endif /* Enable if you want a read-only libpng */ #ifndef PNG_NO_WRITE_SUPPORTED # define PNG_WRITE_SUPPORTED #endif /* Enabled in 1.2.41. */ #ifdef PNG_ALLOW_BENIGN_ERRORS # define png_benign_error png_warning # define png_chunk_benign_error png_chunk_warning #else # ifndef PNG_BENIGN_ERRORS_SUPPORTED # define png_benign_error png_error # define png_chunk_benign_error png_chunk_error # endif #endif /* Added in libpng-1.2.41 */ #if !defined(PNG_NO_WARNINGS) && !defined(PNG_WARNINGS_SUPPORTED) # define PNG_WARNINGS_SUPPORTED #endif #if !defined(PNG_NO_ERROR_TEXT) && !defined(PNG_ERROR_TEXT_SUPPORTED) # define PNG_ERROR_TEXT_SUPPORTED #endif #if !defined(PNG_NO_CHECK_cHRM) && !defined(PNG_CHECK_cHRM_SUPPORTED) # define PNG_CHECK_cHRM_SUPPORTED #endif /* Enabled by default in 1.2.0. You can disable this if you don't need to * support PNGs that are embedded in MNG datastreams */ #if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES) # ifndef PNG_MNG_FEATURES_SUPPORTED # define PNG_MNG_FEATURES_SUPPORTED # endif #endif #ifndef PNG_NO_FLOATING_POINT_SUPPORTED # ifndef PNG_FLOATING_POINT_SUPPORTED # define PNG_FLOATING_POINT_SUPPORTED # endif #endif /* If you are running on a machine where you cannot allocate more * than 64K of memory at once, uncomment this. While libpng will not * normally need that much memory in a chunk (unless you load up a very * large file), zlib needs to know how big of a chunk it can use, and * libpng thus makes sure to check any memory allocation to verify it * will fit into memory. #define PNG_MAX_MALLOC_64K */ #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) # define PNG_MAX_MALLOC_64K #endif /* Special munging to support doing things the 'cygwin' way: * 'Normal' png-on-win32 defines/defaults: * PNG_BUILD_DLL -- building dll * PNG_USE_DLL -- building an application, linking to dll * (no define) -- building static library, or building an * application and linking to the static lib * 'Cygwin' defines/defaults: * PNG_BUILD_DLL -- (ignored) building the dll * (no define) -- (ignored) building an application, linking to the dll * PNG_STATIC -- (ignored) building the static lib, or building an * application that links to the static lib. * ALL_STATIC -- (ignored) building various static libs, or building an * application that links to the static libs. * Thus, * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and * this bit of #ifdefs will define the 'correct' config variables based on * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but * unnecessary. * * Also, the precedence order is: * ALL_STATIC (since we can't #undef something outside our namespace) * PNG_BUILD_DLL * PNG_STATIC * (nothing) == PNG_USE_DLL * * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent * of auto-import in binutils, we no longer need to worry about * __declspec(dllexport) / __declspec(dllimport) and friends. Therefore, * we don't need to worry about PNG_STATIC or ALL_STATIC when it comes * to __declspec() stuff. However, we DO need to worry about * PNG_BUILD_DLL and PNG_STATIC because those change some defaults * such as CONSOLE_IO and whether GLOBAL_ARRAYS are allowed. */ #ifdef __CYGWIN__ # ifdef ALL_STATIC # ifdef PNG_BUILD_DLL # undef PNG_BUILD_DLL # endif # ifdef PNG_USE_DLL # undef PNG_USE_DLL # endif # ifdef PNG_DLL # undef PNG_DLL # endif # ifndef PNG_STATIC # define PNG_STATIC # endif # else # ifdef PNG_BUILD_DLL # ifdef PNG_STATIC # undef PNG_STATIC # endif # ifdef PNG_USE_DLL # undef PNG_USE_DLL # endif # ifndef PNG_DLL # define PNG_DLL # endif # else # ifdef PNG_STATIC # ifdef PNG_USE_DLL # undef PNG_USE_DLL # endif # ifdef PNG_DLL # undef PNG_DLL # endif # else # ifndef PNG_USE_DLL # define PNG_USE_DLL # endif # ifndef PNG_DLL # define PNG_DLL # endif # endif # endif # endif #endif /* This protects us against compilers that run on a windowing system * and thus don't have or would rather us not use the stdio types: * stdin, stdout, and stderr. The only one currently used is stderr * in png_error() and png_warning(). #defining PNG_NO_CONSOLE_IO will * prevent these from being compiled and used. #defining PNG_NO_STDIO * will also prevent these, plus will prevent the entire set of stdio * macros and functions (FILE *, printf, etc.) from being compiled and used, * unless (PNG_DEBUG > 0) has been #defined. * * #define PNG_NO_CONSOLE_IO * #define PNG_NO_STDIO */ #if !defined(PNG_NO_STDIO) && !defined(PNG_STDIO_SUPPORTED) # define PNG_STDIO_SUPPORTED #endif #ifdef _WIN32_WCE # include /* Console I/O functions are not supported on WindowsCE */ # define PNG_NO_CONSOLE_IO /* abort() may not be supported on some/all Windows CE platforms */ # define PNG_ABORT() exit(-1) # ifdef PNG_DEBUG # undef PNG_DEBUG # endif #endif #ifdef PNG_BUILD_DLL # ifndef PNG_CONSOLE_IO_SUPPORTED # ifndef PNG_NO_CONSOLE_IO # define PNG_NO_CONSOLE_IO # endif # endif #endif # ifdef PNG_NO_STDIO # ifndef PNG_NO_CONSOLE_IO # define PNG_NO_CONSOLE_IO # endif # ifdef PNG_DEBUG # if (PNG_DEBUG > 0) # include # endif # endif # else # ifndef _WIN32_WCE /* "stdio.h" functions are not supported on WindowsCE */ # include # endif # endif #if !(defined PNG_NO_CONSOLE_IO) && !defined(PNG_CONSOLE_IO_SUPPORTED) # define PNG_CONSOLE_IO_SUPPORTED #endif /* This macro protects us against machines that don't have function * prototypes (ie K&R style headers). If your compiler does not handle * function prototypes, define this macro and use the included ansi2knr. * I've always been able to use _NO_PROTO as the indicator, but you may * need to drag the empty declaration out in front of here, or change the * ifdef to suit your own needs. */ #ifndef PNGARG #ifdef OF /* zlib prototype munger */ # define PNGARG(arglist) OF(arglist) #else #ifdef _NO_PROTO # define PNGARG(arglist) () # ifndef PNG_TYPECAST_NULL # define PNG_TYPECAST_NULL # endif #else # define PNGARG(arglist) arglist #endif /* _NO_PROTO */ #endif /* OF */ #endif /* PNGARG */ /* Try to determine if we are compiling on a Mac. Note that testing for * just __MWERKS__ is not good enough, because the Codewarrior is now used * on non-Mac platforms. */ #ifndef MACOS # if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \ defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC) # define MACOS # endif #endif /* enough people need this for various reasons to include it here */ #if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE) # include #endif #if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED) # define PNG_SETJMP_SUPPORTED #endif #ifdef PNG_SETJMP_SUPPORTED /* This is an attempt to force a single setjmp behaviour on Linux. If * the X config stuff didn't define _BSD_SOURCE we wouldn't need this. * * You can bypass this test if you know that your application uses exactly * the same setjmp.h that was included when libpng was built. Only define * PNG_SKIP_SETJMP_CHECK while building your application, prior to the * application's '#include "png.h"'. Don't define PNG_SKIP_SETJMP_CHECK * while building a separate libpng library for general use. */ # ifndef PNG_SKIP_SETJMP_CHECK # ifdef __linux__ # ifdef _BSD_SOURCE # define PNG_SAVE_BSD_SOURCE # undef _BSD_SOURCE # endif # ifdef _SETJMP_H /* If you encounter a compiler error here, see the explanation * near the end of INSTALL. */ __pngconf.h__ in libpng already includes setjmp.h; __dont__ include it again.; # endif # endif /* __linux__ */ # endif /* PNG_SKIP_SETJMP_CHECK */ /* include setjmp.h for error handling */ # include # ifdef __linux__ # ifdef PNG_SAVE_BSD_SOURCE # ifndef _BSD_SOURCE # define _BSD_SOURCE # endif # undef PNG_SAVE_BSD_SOURCE # endif # endif /* __linux__ */ #endif /* PNG_SETJMP_SUPPORTED */ #ifdef BSD # include #else # include #endif /* Other defines for things like memory and the like can go here. */ #ifdef PNG_INTERNAL #include /* The functions exported by PNG_EXTERN are PNG_INTERNAL functions, which * aren't usually used outside the library (as far as I know), so it is * debatable if they should be exported at all. In the future, when it is * possible to have run-time registry of chunk-handling functions, some of * these will be made available again. #define PNG_EXTERN extern */ #define PNG_EXTERN /* Other defines specific to compilers can go here. Try to keep * them inside an appropriate ifdef/endif pair for portability. */ #ifdef PNG_FLOATING_POINT_SUPPORTED # ifdef MACOS /* We need to check that hasn't already been included earlier * as it seems it doesn't agree with , yet we should really use * if possible. */ # if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__) # include # endif # else # include # endif # if defined(_AMIGA) && defined(__SASC) && defined(_M68881) /* Amiga SAS/C: We must include builtin FPU functions when compiling using * MATH=68881 */ # include # endif #endif /* Codewarrior on NT has linking problems without this. */ #if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__) # define PNG_ALWAYS_EXTERN #endif /* This provides the non-ANSI (far) memory allocation routines. */ #if defined(__TURBOC__) && defined(__MSDOS__) # include # include #endif /* I have no idea why is this necessary... */ #if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \ defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__)) # include #endif /* This controls how fine the dithering gets. As this allocates * a largish chunk of memory (32K), those who are not as concerned * with dithering quality can decrease some or all of these. */ #ifndef PNG_DITHER_RED_BITS # define PNG_DITHER_RED_BITS 5 #endif #ifndef PNG_DITHER_GREEN_BITS # define PNG_DITHER_GREEN_BITS 5 #endif #ifndef PNG_DITHER_BLUE_BITS # define PNG_DITHER_BLUE_BITS 5 #endif /* This controls how fine the gamma correction becomes when you * are only interested in 8 bits anyway. Increasing this value * results in more memory being used, and more pow() functions * being called to fill in the gamma tables. Don't set this value * less then 8, and even that may not work (I haven't tested it). */ #ifndef PNG_MAX_GAMMA_8 # define PNG_MAX_GAMMA_8 11 #endif /* This controls how much a difference in gamma we can tolerate before * we actually start doing gamma conversion. */ #ifndef PNG_GAMMA_THRESHOLD # define PNG_GAMMA_THRESHOLD 0.05 #endif #endif /* PNG_INTERNAL */ /* The following uses const char * instead of char * for error * and warning message functions, so some compilers won't complain. * If you do not want to use const, define PNG_NO_CONST here. */ #ifndef PNG_NO_CONST # define PNG_CONST const #else # define PNG_CONST #endif /* The following defines give you the ability to remove code from the * library that you will not be using. I wish I could figure out how to * automate this, but I can't do that without making it seriously hard * on the users. So if you are not using an ability, change the #define * to and #undef, and that part of the library will not be compiled. If * your linker can't find a function, you may want to make sure the * ability is defined here. Some of these depend upon some others being * defined. I haven't figured out all the interactions here, so you may * have to experiment awhile to get everything to compile. If you are * creating or using a shared library, you probably shouldn't touch this, * as it will affect the size of the structures, and this will cause bad * things to happen if the library and/or application ever change. */ /* Any features you will not be using can be undef'ed here */ /* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user * to turn it off with "*TRANSFORMS_NOT_SUPPORTED" or *PNG_NO_*_TRANSFORMS * on the compile line, then pick and choose which ones to define without * having to edit this file. It is safe to use the *TRANSFORMS_NOT_SUPPORTED * if you only want to have a png-compliant reader/writer but don't need * any of the extra transformations. This saves about 80 kbytes in a * typical installation of the library. (PNG_NO_* form added in version * 1.0.1c, for consistency) */ /* The size of the png_text structure changed in libpng-1.0.6 when * iTXt support was added. iTXt support was turned off by default through * libpng-1.2.x, to support old apps that malloc the png_text structure * instead of calling png_set_text() and letting libpng malloc it. It * will be turned on by default in libpng-1.4.0. */ #if defined(PNG_1_0_X) || defined (PNG_1_2_X) # ifndef PNG_NO_iTXt_SUPPORTED # define PNG_NO_iTXt_SUPPORTED # endif # ifndef PNG_NO_READ_iTXt # define PNG_NO_READ_iTXt # endif # ifndef PNG_NO_WRITE_iTXt # define PNG_NO_WRITE_iTXt # endif #endif #if !defined(PNG_NO_iTXt_SUPPORTED) # if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt) # define PNG_READ_iTXt # endif # if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt) # define PNG_WRITE_iTXt # endif #endif /* The following support, added after version 1.0.0, can be turned off here en * masse by defining PNG_LEGACY_SUPPORTED in case you need binary compatibility * with old applications that require the length of png_struct and png_info * to remain unchanged. */ #ifdef PNG_LEGACY_SUPPORTED # define PNG_NO_FREE_ME # define PNG_NO_READ_UNKNOWN_CHUNKS # define PNG_NO_WRITE_UNKNOWN_CHUNKS # define PNG_NO_HANDLE_AS_UNKNOWN # define PNG_NO_READ_USER_CHUNKS # define PNG_NO_READ_iCCP # define PNG_NO_WRITE_iCCP # define PNG_NO_READ_iTXt # define PNG_NO_WRITE_iTXt # define PNG_NO_READ_sCAL # define PNG_NO_WRITE_sCAL # define PNG_NO_READ_sPLT # define PNG_NO_WRITE_sPLT # define PNG_NO_INFO_IMAGE # define PNG_NO_READ_RGB_TO_GRAY # define PNG_NO_READ_USER_TRANSFORM # define PNG_NO_WRITE_USER_TRANSFORM # define PNG_NO_USER_MEM # define PNG_NO_READ_EMPTY_PLTE # define PNG_NO_MNG_FEATURES # define PNG_NO_FIXED_POINT_SUPPORTED #endif /* Ignore attempt to turn off both floating and fixed point support */ #if !defined(PNG_FLOATING_POINT_SUPPORTED) || \ !defined(PNG_NO_FIXED_POINT_SUPPORTED) # define PNG_FIXED_POINT_SUPPORTED #endif #ifndef PNG_NO_FREE_ME # define PNG_FREE_ME_SUPPORTED #endif #ifdef PNG_READ_SUPPORTED #if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \ !defined(PNG_NO_READ_TRANSFORMS) # define PNG_READ_TRANSFORMS_SUPPORTED #endif #ifdef PNG_READ_TRANSFORMS_SUPPORTED # ifndef PNG_NO_READ_EXPAND # define PNG_READ_EXPAND_SUPPORTED # endif # ifndef PNG_NO_READ_SHIFT # define PNG_READ_SHIFT_SUPPORTED # endif # ifndef PNG_NO_READ_PACK # define PNG_READ_PACK_SUPPORTED # endif # ifndef PNG_NO_READ_BGR # define PNG_READ_BGR_SUPPORTED # endif # ifndef PNG_NO_READ_SWAP # define PNG_READ_SWAP_SUPPORTED # endif # ifndef PNG_NO_READ_PACKSWAP # define PNG_READ_PACKSWAP_SUPPORTED # endif # ifndef PNG_NO_READ_INVERT # define PNG_READ_INVERT_SUPPORTED # endif # ifndef PNG_NO_READ_DITHER # define PNG_READ_DITHER_SUPPORTED # endif # ifndef PNG_NO_READ_BACKGROUND # define PNG_READ_BACKGROUND_SUPPORTED # endif # ifndef PNG_NO_READ_16_TO_8 # define PNG_READ_16_TO_8_SUPPORTED # endif # ifndef PNG_NO_READ_FILLER # define PNG_READ_FILLER_SUPPORTED # endif # ifndef PNG_NO_READ_GAMMA # define PNG_READ_GAMMA_SUPPORTED # endif # ifndef PNG_NO_READ_GRAY_TO_RGB # define PNG_READ_GRAY_TO_RGB_SUPPORTED # endif # ifndef PNG_NO_READ_SWAP_ALPHA # define PNG_READ_SWAP_ALPHA_SUPPORTED # endif # ifndef PNG_NO_READ_INVERT_ALPHA # define PNG_READ_INVERT_ALPHA_SUPPORTED # endif # ifndef PNG_NO_READ_STRIP_ALPHA # define PNG_READ_STRIP_ALPHA_SUPPORTED # endif # ifndef PNG_NO_READ_USER_TRANSFORM # define PNG_READ_USER_TRANSFORM_SUPPORTED # endif # ifndef PNG_NO_READ_RGB_TO_GRAY # define PNG_READ_RGB_TO_GRAY_SUPPORTED # endif #endif /* PNG_READ_TRANSFORMS_SUPPORTED */ /* PNG_PROGRESSIVE_READ_NOT_SUPPORTED is deprecated. */ #if !defined(PNG_NO_PROGRESSIVE_READ) && \ !defined(PNG_PROGRESSIVE_READ_NOT_SUPPORTED) /* if you don't do progressive */ # define PNG_PROGRESSIVE_READ_SUPPORTED /* reading. This is not talking */ #endif /* about interlacing capability! You'll */ /* still have interlacing unless you change the following define: */ #define PNG_READ_INTERLACING_SUPPORTED /* required for PNG-compliant decoders */ /* PNG_NO_SEQUENTIAL_READ_SUPPORTED is deprecated. */ #if !defined(PNG_NO_SEQUENTIAL_READ) && \ !defined(PNG_SEQUENTIAL_READ_SUPPORTED) && \ !defined(PNG_NO_SEQUENTIAL_READ_SUPPORTED) # define PNG_SEQUENTIAL_READ_SUPPORTED #endif #define PNG_READ_INTERLACING_SUPPORTED /* required in PNG-compliant decoders */ #ifndef PNG_NO_READ_COMPOSITE_NODIV # ifndef PNG_NO_READ_COMPOSITED_NODIV /* libpng-1.0.x misspelling */ # define PNG_READ_COMPOSITE_NODIV_SUPPORTED /* well tested on Intel, SGI */ # endif #endif #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Deprecated, will be removed from version 2.0.0. Use PNG_MNG_FEATURES_SUPPORTED instead. */ #ifndef PNG_NO_READ_EMPTY_PLTE # define PNG_READ_EMPTY_PLTE_SUPPORTED #endif #endif #endif /* PNG_READ_SUPPORTED */ #ifdef PNG_WRITE_SUPPORTED # if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \ !defined(PNG_NO_WRITE_TRANSFORMS) # define PNG_WRITE_TRANSFORMS_SUPPORTED #endif #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED # ifndef PNG_NO_WRITE_SHIFT # define PNG_WRITE_SHIFT_SUPPORTED # endif # ifndef PNG_NO_WRITE_PACK # define PNG_WRITE_PACK_SUPPORTED # endif # ifndef PNG_NO_WRITE_BGR # define PNG_WRITE_BGR_SUPPORTED # endif # ifndef PNG_NO_WRITE_SWAP # define PNG_WRITE_SWAP_SUPPORTED # endif # ifndef PNG_NO_WRITE_PACKSWAP # define PNG_WRITE_PACKSWAP_SUPPORTED # endif # ifndef PNG_NO_WRITE_INVERT # define PNG_WRITE_INVERT_SUPPORTED # endif # ifndef PNG_NO_WRITE_FILLER # define PNG_WRITE_FILLER_SUPPORTED /* same as WRITE_STRIP_ALPHA */ # endif # ifndef PNG_NO_WRITE_SWAP_ALPHA # define PNG_WRITE_SWAP_ALPHA_SUPPORTED # endif #ifndef PNG_1_0_X # ifndef PNG_NO_WRITE_INVERT_ALPHA # define PNG_WRITE_INVERT_ALPHA_SUPPORTED # endif #endif # ifndef PNG_NO_WRITE_USER_TRANSFORM # define PNG_WRITE_USER_TRANSFORM_SUPPORTED # endif #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */ #if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \ !defined(PNG_WRITE_INTERLACING_SUPPORTED) #define PNG_WRITE_INTERLACING_SUPPORTED /* not required for PNG-compliant encoders, but can cause trouble if left undefined */ #endif #if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \ !defined(PNG_WRITE_WEIGHTED_FILTER) && \ defined(PNG_FLOATING_POINT_SUPPORTED) # define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED #endif #ifndef PNG_NO_WRITE_FLUSH # define PNG_WRITE_FLUSH_SUPPORTED #endif #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Deprecated, see PNG_MNG_FEATURES_SUPPORTED, above */ #ifndef PNG_NO_WRITE_EMPTY_PLTE # define PNG_WRITE_EMPTY_PLTE_SUPPORTED #endif #endif #endif /* PNG_WRITE_SUPPORTED */ #ifndef PNG_1_0_X # ifndef PNG_NO_ERROR_NUMBERS # define PNG_ERROR_NUMBERS_SUPPORTED # endif #endif /* PNG_1_0_X */ #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) # ifndef PNG_NO_USER_TRANSFORM_PTR # define PNG_USER_TRANSFORM_PTR_SUPPORTED # endif #endif #ifndef PNG_NO_STDIO # define PNG_TIME_RFC1123_SUPPORTED #endif /* This adds extra functions in pngget.c for accessing data from the * info pointer (added in version 0.99) * png_get_image_width() * png_get_image_height() * png_get_bit_depth() * png_get_color_type() * png_get_compression_type() * png_get_filter_type() * png_get_interlace_type() * png_get_pixel_aspect_ratio() * png_get_pixels_per_meter() * png_get_x_offset_pixels() * png_get_y_offset_pixels() * png_get_x_offset_microns() * png_get_y_offset_microns() */ #if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED) # define PNG_EASY_ACCESS_SUPPORTED #endif /* PNG_ASSEMBLER_CODE was enabled by default in version 1.2.0 * and removed from version 1.2.20. The following will be removed * from libpng-1.4.0 */ #if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_OPTIMIZED_CODE) # ifndef PNG_OPTIMIZED_CODE_SUPPORTED # define PNG_OPTIMIZED_CODE_SUPPORTED # endif #endif #if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE) # ifndef PNG_ASSEMBLER_CODE_SUPPORTED # define PNG_ASSEMBLER_CODE_SUPPORTED # endif # if defined(__GNUC__) && defined(__x86_64__) && (__GNUC__ < 4) /* work around 64-bit gcc compiler bugs in gcc-3.x */ # if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE) # define PNG_NO_MMX_CODE # endif # endif # ifdef __APPLE__ # if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE) # define PNG_NO_MMX_CODE # endif # endif # if (defined(__MWERKS__) && ((__MWERKS__ < 0x0900) || macintosh)) # if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE) # define PNG_NO_MMX_CODE # endif # endif # if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE) # define PNG_MMX_CODE_SUPPORTED # endif #endif /* end of obsolete code to be removed from libpng-1.4.0 */ /* Added at libpng-1.2.0 */ #ifndef PNG_1_0_X #if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED) # define PNG_USER_MEM_SUPPORTED #endif #endif /* PNG_1_0_X */ /* Added at libpng-1.2.6 */ #ifndef PNG_1_0_X # ifndef PNG_SET_USER_LIMITS_SUPPORTED # ifndef PNG_NO_SET_USER_LIMITS # define PNG_SET_USER_LIMITS_SUPPORTED # endif # endif #endif /* PNG_1_0_X */ /* Added at libpng-1.0.53 and 1.2.43 */ #ifndef PNG_USER_LIMITS_SUPPORTED # ifndef PNG_NO_USER_LIMITS # define PNG_USER_LIMITS_SUPPORTED # endif #endif /* Added at libpng-1.0.16 and 1.2.6. To accept all valid PNGS no matter * how large, set these limits to 0x7fffffffL */ #ifndef PNG_USER_WIDTH_MAX # define PNG_USER_WIDTH_MAX 1000000L #endif #ifndef PNG_USER_HEIGHT_MAX # define PNG_USER_HEIGHT_MAX 1000000L #endif /* Added at libpng-1.2.43. To accept all valid PNGs no matter * how large, set these two limits to 0. */ #ifndef PNG_USER_CHUNK_CACHE_MAX # define PNG_USER_CHUNK_CACHE_MAX 0 #endif /* Added at libpng-1.2.43 */ #ifndef PNG_USER_CHUNK_MALLOC_MAX # define PNG_USER_CHUNK_MALLOC_MAX 0 #endif #ifndef PNG_LITERAL_SHARP # define PNG_LITERAL_SHARP 0x23 #endif #ifndef PNG_LITERAL_LEFT_SQUARE_BRACKET # define PNG_LITERAL_LEFT_SQUARE_BRACKET 0x5b #endif #ifndef PNG_LITERAL_RIGHT_SQUARE_BRACKET # define PNG_LITERAL_RIGHT_SQUARE_BRACKET 0x5d #endif /* Added at libpng-1.2.34 */ #ifndef PNG_STRING_NEWLINE #define PNG_STRING_NEWLINE "\n" #endif /* These are currently experimental features, define them if you want */ /* very little testing */ /* #ifdef PNG_READ_SUPPORTED # ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED # define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED # endif #endif */ /* This is only for PowerPC big-endian and 680x0 systems */ /* some testing */ /* #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED # define PNG_READ_BIG_ENDIAN_SUPPORTED #endif */ /* Buggy compilers (e.g., gcc 2.7.2.2) need this */ /* #define PNG_NO_POINTER_INDEXING */ #if !defined(PNG_NO_POINTER_INDEXING) && \ !defined(PNG_POINTER_INDEXING_SUPPORTED) # define PNG_POINTER_INDEXING_SUPPORTED #endif /* These functions are turned off by default, as they will be phased out. */ /* #define PNG_USELESS_TESTS_SUPPORTED #define PNG_CORRECT_PALETTE_SUPPORTED */ /* Any chunks you are not interested in, you can undef here. The * ones that allocate memory may be expecially important (hIST, * tEXt, zTXt, tRNS, pCAL). Others will just save time and make png_info * a bit smaller. */ #if defined(PNG_READ_SUPPORTED) && \ !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \ !defined(PNG_NO_READ_ANCILLARY_CHUNKS) # define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED #endif #if defined(PNG_WRITE_SUPPORTED) && \ !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \ !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS) # define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED #endif #ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED #ifdef PNG_NO_READ_TEXT # define PNG_NO_READ_iTXt # define PNG_NO_READ_tEXt # define PNG_NO_READ_zTXt #endif #ifndef PNG_NO_READ_bKGD # define PNG_READ_bKGD_SUPPORTED # define PNG_bKGD_SUPPORTED #endif #ifndef PNG_NO_READ_cHRM # define PNG_READ_cHRM_SUPPORTED # define PNG_cHRM_SUPPORTED #endif #ifndef PNG_NO_READ_gAMA # define PNG_READ_gAMA_SUPPORTED # define PNG_gAMA_SUPPORTED #endif #ifndef PNG_NO_READ_hIST # define PNG_READ_hIST_SUPPORTED # define PNG_hIST_SUPPORTED #endif #ifndef PNG_NO_READ_iCCP # define PNG_READ_iCCP_SUPPORTED # define PNG_iCCP_SUPPORTED #endif #ifndef PNG_NO_READ_iTXt # ifndef PNG_READ_iTXt_SUPPORTED # define PNG_READ_iTXt_SUPPORTED # endif # ifndef PNG_iTXt_SUPPORTED # define PNG_iTXt_SUPPORTED # endif #endif #ifndef PNG_NO_READ_oFFs # define PNG_READ_oFFs_SUPPORTED # define PNG_oFFs_SUPPORTED #endif #ifndef PNG_NO_READ_pCAL # define PNG_READ_pCAL_SUPPORTED # define PNG_pCAL_SUPPORTED #endif #ifndef PNG_NO_READ_sCAL # define PNG_READ_sCAL_SUPPORTED # define PNG_sCAL_SUPPORTED #endif #ifndef PNG_NO_READ_pHYs # define PNG_READ_pHYs_SUPPORTED # define PNG_pHYs_SUPPORTED #endif #ifndef PNG_NO_READ_sBIT # define PNG_READ_sBIT_SUPPORTED # define PNG_sBIT_SUPPORTED #endif #ifndef PNG_NO_READ_sPLT # define PNG_READ_sPLT_SUPPORTED # define PNG_sPLT_SUPPORTED #endif #ifndef PNG_NO_READ_sRGB # define PNG_READ_sRGB_SUPPORTED # define PNG_sRGB_SUPPORTED #endif #ifndef PNG_NO_READ_tEXt # define PNG_READ_tEXt_SUPPORTED # define PNG_tEXt_SUPPORTED #endif #ifndef PNG_NO_READ_tIME # define PNG_READ_tIME_SUPPORTED # define PNG_tIME_SUPPORTED #endif #ifndef PNG_NO_READ_tRNS # define PNG_READ_tRNS_SUPPORTED # define PNG_tRNS_SUPPORTED #endif #ifndef PNG_NO_READ_zTXt # define PNG_READ_zTXt_SUPPORTED # define PNG_zTXt_SUPPORTED #endif #ifndef PNG_NO_READ_OPT_PLTE # define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */ #endif /* optional PLTE chunk in RGB and RGBA images */ #if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \ defined(PNG_READ_zTXt_SUPPORTED) # define PNG_READ_TEXT_SUPPORTED # define PNG_TEXT_SUPPORTED #endif #endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */ #ifndef PNG_NO_READ_UNKNOWN_CHUNKS # define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED # ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED # define PNG_UNKNOWN_CHUNKS_SUPPORTED # endif #endif #if !defined(PNG_NO_READ_USER_CHUNKS) && \ defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) # define PNG_READ_USER_CHUNKS_SUPPORTED # define PNG_USER_CHUNKS_SUPPORTED # ifdef PNG_NO_READ_UNKNOWN_CHUNKS # undef PNG_NO_READ_UNKNOWN_CHUNKS # endif # ifdef PNG_NO_HANDLE_AS_UNKNOWN # undef PNG_NO_HANDLE_AS_UNKNOWN # endif #endif #ifndef PNG_NO_HANDLE_AS_UNKNOWN # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED # define PNG_HANDLE_AS_UNKNOWN_SUPPORTED # endif #endif #ifdef PNG_WRITE_SUPPORTED #ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED #ifdef PNG_NO_WRITE_TEXT # define PNG_NO_WRITE_iTXt # define PNG_NO_WRITE_tEXt # define PNG_NO_WRITE_zTXt #endif #ifndef PNG_NO_WRITE_bKGD # define PNG_WRITE_bKGD_SUPPORTED # ifndef PNG_bKGD_SUPPORTED # define PNG_bKGD_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_cHRM # define PNG_WRITE_cHRM_SUPPORTED # ifndef PNG_cHRM_SUPPORTED # define PNG_cHRM_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_gAMA # define PNG_WRITE_gAMA_SUPPORTED # ifndef PNG_gAMA_SUPPORTED # define PNG_gAMA_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_hIST # define PNG_WRITE_hIST_SUPPORTED # ifndef PNG_hIST_SUPPORTED # define PNG_hIST_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_iCCP # define PNG_WRITE_iCCP_SUPPORTED # ifndef PNG_iCCP_SUPPORTED # define PNG_iCCP_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_iTXt # ifndef PNG_WRITE_iTXt_SUPPORTED # define PNG_WRITE_iTXt_SUPPORTED # endif # ifndef PNG_iTXt_SUPPORTED # define PNG_iTXt_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_oFFs # define PNG_WRITE_oFFs_SUPPORTED # ifndef PNG_oFFs_SUPPORTED # define PNG_oFFs_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_pCAL # define PNG_WRITE_pCAL_SUPPORTED # ifndef PNG_pCAL_SUPPORTED # define PNG_pCAL_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_sCAL # define PNG_WRITE_sCAL_SUPPORTED # ifndef PNG_sCAL_SUPPORTED # define PNG_sCAL_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_pHYs # define PNG_WRITE_pHYs_SUPPORTED # ifndef PNG_pHYs_SUPPORTED # define PNG_pHYs_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_sBIT # define PNG_WRITE_sBIT_SUPPORTED # ifndef PNG_sBIT_SUPPORTED # define PNG_sBIT_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_sPLT # define PNG_WRITE_sPLT_SUPPORTED # ifndef PNG_sPLT_SUPPORTED # define PNG_sPLT_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_sRGB # define PNG_WRITE_sRGB_SUPPORTED # ifndef PNG_sRGB_SUPPORTED # define PNG_sRGB_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_tEXt # define PNG_WRITE_tEXt_SUPPORTED # ifndef PNG_tEXt_SUPPORTED # define PNG_tEXt_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_tIME # define PNG_WRITE_tIME_SUPPORTED # ifndef PNG_tIME_SUPPORTED # define PNG_tIME_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_tRNS # define PNG_WRITE_tRNS_SUPPORTED # ifndef PNG_tRNS_SUPPORTED # define PNG_tRNS_SUPPORTED # endif #endif #ifndef PNG_NO_WRITE_zTXt # define PNG_WRITE_zTXt_SUPPORTED # ifndef PNG_zTXt_SUPPORTED # define PNG_zTXt_SUPPORTED # endif #endif #if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \ defined(PNG_WRITE_zTXt_SUPPORTED) # define PNG_WRITE_TEXT_SUPPORTED # ifndef PNG_TEXT_SUPPORTED # define PNG_TEXT_SUPPORTED # endif #endif #ifdef PNG_WRITE_tIME_SUPPORTED # ifndef PNG_NO_CONVERT_tIME # ifndef _WIN32_WCE /* The "tm" structure is not supported on WindowsCE */ # ifndef PNG_CONVERT_tIME_SUPPORTED # define PNG_CONVERT_tIME_SUPPORTED # endif # endif # endif #endif #endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */ #if !defined(PNG_NO_WRITE_FILTER) && !defined(PNG_WRITE_FILTER_SUPPORTED) # define PNG_WRITE_FILTER_SUPPORTED #endif #ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS # define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED # ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED # define PNG_UNKNOWN_CHUNKS_SUPPORTED # endif #endif #ifndef PNG_NO_HANDLE_AS_UNKNOWN # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED # define PNG_HANDLE_AS_UNKNOWN_SUPPORTED # endif #endif #endif /* PNG_WRITE_SUPPORTED */ /* Turn this off to disable png_read_png() and * png_write_png() and leave the row_pointers member * out of the info structure. */ #ifndef PNG_NO_INFO_IMAGE # define PNG_INFO_IMAGE_SUPPORTED #endif /* Need the time information for converting tIME chunks */ #ifdef PNG_CONVERT_tIME_SUPPORTED /* "time.h" functions are not supported on WindowsCE */ # include #endif /* Some typedefs to get us started. These should be safe on most of the * common platforms. The typedefs should be at least as large as the * numbers suggest (a png_uint_32 must be at least 32 bits long), but they * don't have to be exactly that size. Some compilers dislike passing * unsigned shorts as function parameters, so you may be better off using * unsigned int for png_uint_16. Likewise, for 64-bit systems, you may * want to have unsigned int for png_uint_32 instead of unsigned long. */ typedef unsigned long png_uint_32; typedef long png_int_32; typedef unsigned short png_uint_16; typedef short png_int_16; typedef unsigned char png_byte; /* This is usually size_t. It is typedef'ed just in case you need it to change (I'm not sure if you will or not, so I thought I'd be safe) */ #ifdef PNG_SIZE_T typedef PNG_SIZE_T png_size_t; # define png_sizeof(x) png_convert_size(sizeof(x)) #else typedef size_t png_size_t; # define png_sizeof(x) sizeof(x) #endif /* The following is needed for medium model support. It cannot be in the * PNG_INTERNAL section. Needs modification for other compilers besides * MSC. Model independent support declares all arrays and pointers to be * large using the far keyword. The zlib version used must also support * model independent data. As of version zlib 1.0.4, the necessary changes * have been made in zlib. The USE_FAR_KEYWORD define triggers other * changes that are needed. (Tim Wegner) */ /* Separate compiler dependencies (problem here is that zlib.h always defines FAR. (SJT) */ #ifdef __BORLANDC__ # if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__) # define LDATA 1 # else # define LDATA 0 # endif /* GRR: why is Cygwin in here? Cygwin is not Borland C... */ # if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__) # define PNG_MAX_MALLOC_64K # if (LDATA != 1) # ifndef FAR # define FAR __far # endif # define USE_FAR_KEYWORD # endif /* LDATA != 1 */ /* Possibly useful for moving data out of default segment. * Uncomment it if you want. Could also define FARDATA as * const if your compiler supports it. (SJT) # define FARDATA FAR */ # endif /* __WIN32__, __FLAT__, __CYGWIN__ */ #endif /* __BORLANDC__ */ /* Suggest testing for specific compiler first before testing for * FAR. The Watcom compiler defines both __MEDIUM__ and M_I86MM, * making reliance oncertain keywords suspect. (SJT) */ /* MSC Medium model */ #ifdef FAR # ifdef M_I86MM # define USE_FAR_KEYWORD # define FARDATA FAR # include # endif #endif /* SJT: default case */ #ifndef FAR # define FAR #endif /* At this point FAR is always defined */ #ifndef FARDATA # define FARDATA #endif /* Typedef for floating-point numbers that are converted to fixed-point with a multiple of 100,000, e.g., int_gamma */ typedef png_int_32 png_fixed_point; /* Add typedefs for pointers */ typedef void FAR * png_voidp; typedef png_byte FAR * png_bytep; typedef png_uint_32 FAR * png_uint_32p; typedef png_int_32 FAR * png_int_32p; typedef png_uint_16 FAR * png_uint_16p; typedef png_int_16 FAR * png_int_16p; typedef PNG_CONST char FAR * png_const_charp; typedef char FAR * png_charp; typedef png_fixed_point FAR * png_fixed_point_p; #ifndef PNG_NO_STDIO #ifdef _WIN32_WCE typedef HANDLE png_FILE_p; #else typedef FILE * png_FILE_p; #endif #endif #ifdef PNG_FLOATING_POINT_SUPPORTED typedef double FAR * png_doublep; #endif /* Pointers to pointers; i.e. arrays */ typedef png_byte FAR * FAR * png_bytepp; typedef png_uint_32 FAR * FAR * png_uint_32pp; typedef png_int_32 FAR * FAR * png_int_32pp; typedef png_uint_16 FAR * FAR * png_uint_16pp; typedef png_int_16 FAR * FAR * png_int_16pp; typedef PNG_CONST char FAR * FAR * png_const_charpp; typedef char FAR * FAR * png_charpp; typedef png_fixed_point FAR * FAR * png_fixed_point_pp; #ifdef PNG_FLOATING_POINT_SUPPORTED typedef double FAR * FAR * png_doublepp; #endif /* Pointers to pointers to pointers; i.e., pointer to array */ typedef char FAR * FAR * FAR * png_charppp; #if defined(PNG_1_0_X) || defined(PNG_1_2_X) /* SPC - Is this stuff deprecated? */ /* It'll be removed as of libpng-1.4.0 - GR-P */ /* libpng typedefs for types in zlib. If zlib changes * or another compression library is used, then change these. * Eliminates need to change all the source files. */ typedef charf * png_zcharp; typedef charf * FAR * png_zcharpp; typedef z_stream FAR * png_zstreamp; #endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */ /* * Define PNG_BUILD_DLL if the module being built is a Windows * LIBPNG DLL. * * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL. * It is equivalent to Microsoft predefined macro _DLL that is * automatically defined when you compile using the share * version of the CRT (C Run-Time library) * * The cygwin mods make this behavior a little different: * Define PNG_BUILD_DLL if you are building a dll for use with cygwin * Define PNG_STATIC if you are building a static library for use with cygwin, * -or- if you are building an application that you want to link to the * static library. * PNG_USE_DLL is defined by default (no user action needed) unless one of * the other flags is defined. */ #if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL)) # define PNG_DLL #endif /* If CYGWIN, then disallow GLOBAL ARRAYS unless building a static lib. * When building a static lib, default to no GLOBAL ARRAYS, but allow * command-line override */ #ifdef __CYGWIN__ # ifndef PNG_STATIC # ifdef PNG_USE_GLOBAL_ARRAYS # undef PNG_USE_GLOBAL_ARRAYS # endif # ifndef PNG_USE_LOCAL_ARRAYS # define PNG_USE_LOCAL_ARRAYS # endif # else # if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS) # ifdef PNG_USE_GLOBAL_ARRAYS # undef PNG_USE_GLOBAL_ARRAYS # endif # endif # endif # if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS) # define PNG_USE_LOCAL_ARRAYS # endif #endif /* Do not use global arrays (helps with building DLL's) * They are no longer used in libpng itself, since version 1.0.5c, * but might be required for some pre-1.0.5c applications. */ #if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS) # if defined(PNG_NO_GLOBAL_ARRAYS) || \ (defined(__GNUC__) && defined(PNG_DLL)) || defined(_MSC_VER) # define PNG_USE_LOCAL_ARRAYS # else # define PNG_USE_GLOBAL_ARRAYS # endif #endif #ifdef __CYGWIN__ # undef PNGAPI # define PNGAPI __cdecl # undef PNG_IMPEXP # define PNG_IMPEXP #endif /* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall", * you may get warnings regarding the linkage of png_zalloc and png_zfree. * Don't ignore those warnings; you must also reset the default calling * convention in your compiler to match your PNGAPI, and you must build * zlib and your applications the same way you build libpng. */ #if defined(__MINGW32__) && !defined(PNG_MODULEDEF) # ifndef PNG_NO_MODULEDEF # define PNG_NO_MODULEDEF # endif #endif #if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF) # define PNG_IMPEXP #endif #if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \ (( defined(_Windows) || defined(_WINDOWS) || \ defined(WIN32) || defined(_WIN32) || defined(__WIN32__) )) # ifndef PNGAPI # if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800)) # define PNGAPI __cdecl # else # define PNGAPI _cdecl # endif # endif # if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \ 0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */) # define PNG_IMPEXP # endif # ifndef PNG_IMPEXP # define PNG_EXPORT_TYPE1(type,symbol) PNG_IMPEXP type PNGAPI symbol # define PNG_EXPORT_TYPE2(type,symbol) type PNG_IMPEXP PNGAPI symbol /* Borland/Microsoft */ # if defined(_MSC_VER) || defined(__BORLANDC__) # if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500) # define PNG_EXPORT PNG_EXPORT_TYPE1 # else # define PNG_EXPORT PNG_EXPORT_TYPE2 # ifdef PNG_BUILD_DLL # define PNG_IMPEXP __export # else # define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in VC++ */ # endif /* Exists in Borland C++ for C++ classes (== huge) */ # endif # endif # ifndef PNG_IMPEXP # ifdef PNG_BUILD_DLL # define PNG_IMPEXP __declspec(dllexport) # else # define PNG_IMPEXP __declspec(dllimport) # endif # endif # endif /* PNG_IMPEXP */ #else /* !(DLL || non-cygwin WINDOWS) */ # if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__) # ifndef PNGAPI # define PNGAPI _System # endif # else # if 0 /* ... other platforms, with other meanings */ # endif # endif #endif #ifndef PNGAPI # define PNGAPI #endif #ifndef PNG_IMPEXP # define PNG_IMPEXP #endif #ifdef PNG_BUILDSYMS # ifndef PNG_EXPORT # define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END # endif # ifdef PNG_USE_GLOBAL_ARRAYS # ifndef PNG_EXPORT_VAR # define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT # endif # endif #endif #ifndef PNG_EXPORT # define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol #endif #ifdef PNG_USE_GLOBAL_ARRAYS # ifndef PNG_EXPORT_VAR # define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type # endif #endif #ifdef PNG_PEDANTIC_WARNINGS # ifndef PNG_PEDANTIC_WARNINGS_SUPPORTED # define PNG_PEDANTIC_WARNINGS_SUPPORTED # endif #endif #ifdef PNG_PEDANTIC_WARNINGS_SUPPORTED /* Support for compiler specific function attributes. These are used * so that where compiler support is available incorrect use of API * functions in png.h will generate compiler warnings. Added at libpng * version 1.2.41. */ # ifdef __GNUC__ # ifndef PNG_USE_RESULT # define PNG_USE_RESULT __attribute__((__warn_unused_result__)) # endif # ifndef PNG_NORETURN # define PNG_NORETURN __attribute__((__noreturn__)) # endif # ifndef PNG_ALLOCATED # define PNG_ALLOCATED __attribute__((__malloc__)) # endif /* This specifically protects structure members that should only be * accessed from within the library, therefore should be empty during * a library build. */ # ifndef PNG_DEPRECATED # define PNG_DEPRECATED __attribute__((__deprecated__)) # endif # ifndef PNG_DEPSTRUCT # define PNG_DEPSTRUCT __attribute__((__deprecated__)) # endif # ifndef PNG_PRIVATE # if 0 /* Doesn't work so we use deprecated instead*/ # define PNG_PRIVATE \ __attribute__((warning("This function is not exported by libpng."))) # else # define PNG_PRIVATE \ __attribute__((__deprecated__)) # endif # endif /* PNG_PRIVATE */ # endif /* __GNUC__ */ #endif /* PNG_PEDANTIC_WARNINGS */ #ifndef PNG_DEPRECATED # define PNG_DEPRECATED /* Use of this function is deprecated */ #endif #ifndef PNG_USE_RESULT # define PNG_USE_RESULT /* The result of this function must be checked */ #endif #ifndef PNG_NORETURN # define PNG_NORETURN /* This function does not return */ #endif #ifndef PNG_ALLOCATED # define PNG_ALLOCATED /* The result of the function is new memory */ #endif #ifndef PNG_DEPSTRUCT # define PNG_DEPSTRUCT /* Access to this struct member is deprecated */ #endif #ifndef PNG_PRIVATE # define PNG_PRIVATE /* This is a private libpng function */ #endif /* User may want to use these so they are not in PNG_INTERNAL. Any library * functions that are passed far data must be model independent. */ #ifndef PNG_ABORT # define PNG_ABORT() abort() #endif #ifdef PNG_SETJMP_SUPPORTED # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) #else # define png_jmpbuf(png_ptr) \ (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED) #endif #ifdef USE_FAR_KEYWORD /* memory model independent fns */ /* Use this to make far-to-near assignments */ # define CHECK 1 # define NOCHECK 0 # define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK)) # define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK)) # define png_snprintf _fsnprintf /* Added to v 1.2.19 */ # define png_strlen _fstrlen # define png_memcmp _fmemcmp /* SJT: added */ # define png_memcpy _fmemcpy # define png_memset _fmemset #else /* Use the usual functions */ # define CVT_PTR(ptr) (ptr) # define CVT_PTR_NOCHECK(ptr) (ptr) # ifndef PNG_NO_SNPRINTF # ifdef _MSC_VER # define png_snprintf _snprintf /* Added to v 1.2.19 */ # define png_snprintf2 _snprintf # define png_snprintf6 _snprintf # else # define png_snprintf snprintf /* Added to v 1.2.19 */ # define png_snprintf2 snprintf # define png_snprintf6 snprintf # endif # else /* You don't have or don't want to use snprintf(). Caution: Using * sprintf instead of snprintf exposes your application to accidental * or malevolent buffer overflows. If you don't have snprintf() * as a general rule you should provide one (you can get one from * Portable OpenSSH). */ # define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1) # define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2) # define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \ sprintf(s1,fmt,x1,x2,x3,x4,x5,x6) # endif # define png_strlen strlen # define png_memcmp memcmp /* SJT: added */ # define png_memcpy memcpy # define png_memset memset #endif /* End of memory model independent support */ /* Just a little check that someone hasn't tried to define something * contradictory. */ #if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K) # undef PNG_ZBUF_SIZE # define PNG_ZBUF_SIZE 65536L #endif /* Added at libpng-1.2.8 */ #endif /* PNG_VERSION_INFO_ONLY */ #endif /* PNGCONF_H */ glmark2-2012.08/./src/libpng/Y2KINFO0000664000175000017500000000441612013417376015771 0ustar alfalf00000000000000 Y2K compliance in libpng: ========================= July 9, 2011 Since the PNG Development group is an ad-hoc body, we can't make an official declaration. This is your unofficial assurance that libpng from version 0.71 and upward through 1.2.46 are Y2K compliant. It is my belief that earlier versions were also Y2K compliant. Libpng only has three year fields. One is a 2-byte unsigned integer that will hold years up to 65535. The other two hold the date in text format, and will hold years up to 9999. The integer is "png_uint_16 year" in png_time_struct. The strings are "png_charp time_buffer" in png_struct and "near_time_buffer", which is a local character string in png.c. There are seven time-related functions: png_convert_to_rfc_1123() in png.c (formerly png_convert_to_rfc_1152() in error) png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c png_convert_from_time_t() in pngwrite.c png_get_tIME() in pngget.c png_handle_tIME() in pngrutil.c, called in pngread.c png_set_tIME() in pngset.c png_write_tIME() in pngwutil.c, called in pngwrite.c All appear to handle dates properly in a Y2K environment. The png_convert_from_time_t() function calls gmtime() to convert from system clock time, which returns (year - 1900), which we properly convert to the full 4-digit year. There is a possibility that applications using libpng are not passing 4-digit years into the png_convert_to_rfc_1123() function, or that they are incorrectly passing only a 2-digit year instead of "year - 1900" into the png_convert_from_struct_tm() function, but this is not under our control. The libpng documentation has always stated that it works with 4-digit years, and the APIs have been documented as such. The tIME chunk itself is also Y2K compliant. It uses a 2-byte unsigned integer to hold the year, and can hold years as large as 65535. zlib, upon which libpng depends, is also Y2K compliant. It contains no date-related code. Glenn Randers-Pehrson libpng maintainer PNG Development Group glmark2-2012.08/./src/libpng/pngread.c0000664000175000017500000013432512013417376016554 0ustar alfalf00000000000000 /* pngread.c - read a PNG file * * Last changed in libpng 1.2.44 [June 26, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file contains routines that an application calls directly to * read a PNG file or stream. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_READ_SUPPORTED /* Create a PNG structure for reading, and allocate any memory needed. */ png_structp PNGAPI png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn) { #ifdef PNG_USER_MEM_SUPPORTED return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); } /* Alternate create PNG structure for reading, and allocate any memory * needed. */ png_structp PNGAPI png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn) { #endif /* PNG_USER_MEM_SUPPORTED */ #ifdef PNG_SETJMP_SUPPORTED volatile #endif png_structp png_ptr; #ifdef PNG_SETJMP_SUPPORTED #ifdef USE_FAR_KEYWORD jmp_buf jmpbuf; #endif #endif int i; png_debug(1, "in png_create_read_struct"); #ifdef PNG_USER_MEM_SUPPORTED png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); #else png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); #endif if (png_ptr == NULL) return (NULL); /* Added at libpng-1.2.6 */ #ifdef PNG_USER_LIMITS_SUPPORTED png_ptr->user_width_max = PNG_USER_WIDTH_MAX; png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; # ifdef PNG_USER_CHUNK_CACHE_MAX /* Added at libpng-1.2.43 and 1.4.0 */ png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; # endif # ifdef PNG_SET_USER_CHUNK_MALLOC_MAX /* Added at libpng-1.2.43 and 1.4.1 */ png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; # endif #endif #ifdef PNG_SETJMP_SUPPORTED #ifdef USE_FAR_KEYWORD if (setjmp(jmpbuf)) #else if (setjmp(png_ptr->jmpbuf)) #endif { png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif return (NULL); } #ifdef USE_FAR_KEYWORD png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); #endif #endif /* PNG_SETJMP_SUPPORTED */ #ifdef PNG_USER_MEM_SUPPORTED png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); #endif png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); if (user_png_ver) { i = 0; do { if (user_png_ver[i] != png_libpng_ver[i]) png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; } while (png_libpng_ver[i++]); } else png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) { /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so * we must recompile any applications that use any older library version. * For versions after libpng 1.0, we will be compatible, so we need * only check the first digit. */ if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || (user_png_ver[0] == '0' && user_png_ver[2] < '9')) { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char msg[80]; if (user_png_ver) { png_snprintf(msg, 80, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } png_snprintf(msg, 80, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg); #endif #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "Incompatible libpng version in application and library"); } } /* Initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_ptr->zstream.zalloc = png_zalloc; png_ptr->zstream.zfree = png_zfree; png_ptr->zstream.opaque = (voidpf)png_ptr; switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_MEM_ERROR: case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; default: png_error(png_ptr, "Unknown zlib error"); } png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL); #ifdef PNG_SETJMP_SUPPORTED /* Applications that neglect to set up their own setjmp() and then encounter a png_error() will longjmp here. Since the jmpbuf is then meaningless we abort instead of returning. */ #ifdef USE_FAR_KEYWORD if (setjmp(jmpbuf)) PNG_ABORT(); png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); #else if (setjmp(png_ptr->jmpbuf)) PNG_ABORT(); #endif #endif /* PNG_SETJMP_SUPPORTED */ return (png_ptr); } #if defined(PNG_1_0_X) || defined(PNG_1_2_X) /* Initialize PNG structure for reading, and allocate any memory needed. * This interface is deprecated in favour of the png_create_read_struct(), * and it will disappear as of libpng-1.3.0. */ #undef png_read_init void PNGAPI png_read_init(png_structp png_ptr) { /* We only come here via pre-1.0.7-compiled applications */ png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0); } void PNGAPI png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t png_info_size) { /* We only come here via pre-1.0.12-compiled applications */ if (png_ptr == NULL) return; #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) if (png_sizeof(png_struct) > png_struct_size || png_sizeof(png_info) > png_info_size) { char msg[80]; png_ptr->warning_fn = NULL; if (user_png_ver) { png_snprintf(msg, 80, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } png_snprintf(msg, 80, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg); } #endif if (png_sizeof(png_struct) > png_struct_size) { png_ptr->error_fn = NULL; #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "The png struct allocated by the application for reading is" " too small."); } if (png_sizeof(png_info) > png_info_size) { png_ptr->error_fn = NULL; #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "The info struct allocated by application for reading is" " too small."); } png_read_init_3(&png_ptr, user_png_ver, png_struct_size); } #endif /* PNG_1_0_X || PNG_1_2_X */ void PNGAPI png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, png_size_t png_struct_size) { #ifdef PNG_SETJMP_SUPPORTED jmp_buf tmp_jmp; /* to save current jump buffer */ #endif int i = 0; png_structp png_ptr=*ptr_ptr; if (png_ptr == NULL) return; do { if (user_png_ver[i] != png_libpng_ver[i]) { #ifdef PNG_LEGACY_SUPPORTED png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; #else png_ptr->warning_fn = NULL; png_warning(png_ptr, "Application uses deprecated png_read_init() and should be" " recompiled."); break; #endif } } while (png_libpng_ver[i++]); png_debug(1, "in png_read_init_3"); #ifdef PNG_SETJMP_SUPPORTED /* Save jump buffer and error functions */ png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); #endif if (png_sizeof(png_struct) > png_struct_size) { png_destroy_struct(png_ptr); *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); png_ptr = *ptr_ptr; } /* Reset all variables to 0 */ png_memset(png_ptr, 0, png_sizeof(png_struct)); #ifdef PNG_SETJMP_SUPPORTED /* Restore jump buffer */ png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); #endif /* Added at libpng-1.2.6 */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED png_ptr->user_width_max = PNG_USER_WIDTH_MAX; png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; #endif /* Initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zstream.zalloc = png_zalloc; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_ptr->zstream.zalloc = png_zalloc; png_ptr->zstream.zfree = png_zfree; png_ptr->zstream.opaque = (voidpf)png_ptr; switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; default: png_error(png_ptr, "Unknown zlib error"); } png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL); } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the information before the actual image data. This has been * changed in v0.90 to allow reading a file that already has the magic * bytes read from the stream. You can tell libpng how many bytes have * been read from the beginning of the stream (up to the maximum of 8) * via png_set_sig_bytes(), and we will only check the remaining bytes * here. The application can then have access to the signature bytes we * read if it is determined that this isn't a valid PNG file. */ void PNGAPI png_read_info(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_read_info"); if (png_ptr == NULL || info_ptr == NULL) return; /* If we haven't checked all of the PNG signature bytes, do so now. */ if (png_ptr->sig_bytes < 8) { png_size_t num_checked = png_ptr->sig_bytes, num_to_check = 8 - num_checked; png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); png_ptr->sig_bytes = 8; if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) { if (num_checked < 4 && png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) png_error(png_ptr, "Not a PNG file"); else png_error(png_ptr, "PNG file corrupted by ASCII conversion"); } if (num_checked < 3) png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; } for (;;) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IHDR; PNG_CONST PNG_IDAT; PNG_CONST PNG_IEND; PNG_CONST PNG_PLTE; #ifdef PNG_READ_bKGD_SUPPORTED PNG_CONST PNG_bKGD; #endif #ifdef PNG_READ_cHRM_SUPPORTED PNG_CONST PNG_cHRM; #endif #ifdef PNG_READ_gAMA_SUPPORTED PNG_CONST PNG_gAMA; #endif #ifdef PNG_READ_hIST_SUPPORTED PNG_CONST PNG_hIST; #endif #ifdef PNG_READ_iCCP_SUPPORTED PNG_CONST PNG_iCCP; #endif #ifdef PNG_READ_iTXt_SUPPORTED PNG_CONST PNG_iTXt; #endif #ifdef PNG_READ_oFFs_SUPPORTED PNG_CONST PNG_oFFs; #endif #ifdef PNG_READ_pCAL_SUPPORTED PNG_CONST PNG_pCAL; #endif #ifdef PNG_READ_pHYs_SUPPORTED PNG_CONST PNG_pHYs; #endif #ifdef PNG_READ_sBIT_SUPPORTED PNG_CONST PNG_sBIT; #endif #ifdef PNG_READ_sCAL_SUPPORTED PNG_CONST PNG_sCAL; #endif #ifdef PNG_READ_sPLT_SUPPORTED PNG_CONST PNG_sPLT; #endif #ifdef PNG_READ_sRGB_SUPPORTED PNG_CONST PNG_sRGB; #endif #ifdef PNG_READ_tEXt_SUPPORTED PNG_CONST PNG_tEXt; #endif #ifdef PNG_READ_tIME_SUPPORTED PNG_CONST PNG_tIME; #endif #ifdef PNG_READ_tRNS_SUPPORTED PNG_CONST PNG_tRNS; #endif #ifdef PNG_READ_zTXt_SUPPORTED PNG_CONST PNG_zTXt; #endif #endif /* PNG_USE_LOCAL_ARRAYS */ png_uint_32 length = png_read_chunk_header(png_ptr); PNG_CONST png_bytep chunk_name = png_ptr->chunk_name; /* This should be a binary subdivision search or a hash for * matching the chunk name rather than a linear search. */ if (!png_memcmp(chunk_name, png_IDAT, 4)) if (png_ptr->mode & PNG_AFTER_IDAT) png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; if (!png_memcmp(chunk_name, png_IHDR, 4)) png_handle_IHDR(png_ptr, info_ptr, length); else if (!png_memcmp(chunk_name, png_IEND, 4)) png_handle_IEND(png_ptr, info_ptr, length); #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED else if (png_handle_as_unknown(png_ptr, chunk_name)) { if (!png_memcmp(chunk_name, png_IDAT, 4)) png_ptr->mode |= PNG_HAVE_IDAT; png_handle_unknown(png_ptr, info_ptr, length); if (!png_memcmp(chunk_name, png_PLTE, 4)) png_ptr->mode |= PNG_HAVE_PLTE; else if (!png_memcmp(chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_error(png_ptr, "Missing PLTE before IDAT"); break; } } #endif else if (!png_memcmp(chunk_name, png_PLTE, 4)) png_handle_PLTE(png_ptr, info_ptr, length); else if (!png_memcmp(chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_error(png_ptr, "Missing PLTE before IDAT"); png_ptr->idat_size = length; png_ptr->mode |= PNG_HAVE_IDAT; break; } #ifdef PNG_READ_bKGD_SUPPORTED else if (!png_memcmp(chunk_name, png_bKGD, 4)) png_handle_bKGD(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_cHRM_SUPPORTED else if (!png_memcmp(chunk_name, png_cHRM, 4)) png_handle_cHRM(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_gAMA_SUPPORTED else if (!png_memcmp(chunk_name, png_gAMA, 4)) png_handle_gAMA(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_hIST_SUPPORTED else if (!png_memcmp(chunk_name, png_hIST, 4)) png_handle_hIST(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_oFFs_SUPPORTED else if (!png_memcmp(chunk_name, png_oFFs, 4)) png_handle_oFFs(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_pCAL_SUPPORTED else if (!png_memcmp(chunk_name, png_pCAL, 4)) png_handle_pCAL(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sCAL_SUPPORTED else if (!png_memcmp(chunk_name, png_sCAL, 4)) png_handle_sCAL(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_pHYs_SUPPORTED else if (!png_memcmp(chunk_name, png_pHYs, 4)) png_handle_pHYs(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sBIT_SUPPORTED else if (!png_memcmp(chunk_name, png_sBIT, 4)) png_handle_sBIT(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sRGB_SUPPORTED else if (!png_memcmp(chunk_name, png_sRGB, 4)) png_handle_sRGB(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_iCCP_SUPPORTED else if (!png_memcmp(chunk_name, png_iCCP, 4)) png_handle_iCCP(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sPLT_SUPPORTED else if (!png_memcmp(chunk_name, png_sPLT, 4)) png_handle_sPLT(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tEXt_SUPPORTED else if (!png_memcmp(chunk_name, png_tEXt, 4)) png_handle_tEXt(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tIME_SUPPORTED else if (!png_memcmp(chunk_name, png_tIME, 4)) png_handle_tIME(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tRNS_SUPPORTED else if (!png_memcmp(chunk_name, png_tRNS, 4)) png_handle_tRNS(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_zTXt_SUPPORTED else if (!png_memcmp(chunk_name, png_zTXt, 4)) png_handle_zTXt(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_iTXt_SUPPORTED else if (!png_memcmp(chunk_name, png_iTXt, 4)) png_handle_iTXt(png_ptr, info_ptr, length); #endif else png_handle_unknown(png_ptr, info_ptr, length); } } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ /* Optional call to update the users info_ptr structure */ void PNGAPI png_read_update_info(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_read_update_info"); if (png_ptr == NULL) return; if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) png_read_start_row(png_ptr); else png_warning(png_ptr, "Ignoring extra png_read_update_info() call; row buffer not reallocated"); png_read_transform_info(png_ptr, info_ptr); } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Initialize palette, background, etc, after transformations * are set, but before any reading takes place. This allows * the user to obtain a gamma-corrected palette, for example. * If the user doesn't call this, we will do it ourselves. */ void PNGAPI png_start_read_image(png_structp png_ptr) { png_debug(1, "in png_start_read_image"); if (png_ptr == NULL) return; if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) png_read_start_row(png_ptr); } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED void PNGAPI png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) { PNG_CONST PNG_IDAT; PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; int ret; if (png_ptr == NULL) return; png_debug2(1, "in png_read_row (row %lu, pass %d)", png_ptr->row_number, png_ptr->pass); if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) png_read_start_row(png_ptr); if (png_ptr->row_number == 0 && png_ptr->pass == 0) { /* Check for transforms that have been set but were defined out */ #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) if (png_ptr->transformations & PNG_INVERT_MONO) png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) if (png_ptr->transformations & PNG_FILLER) png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ !defined(PNG_READ_PACKSWAP_SUPPORTED) if (png_ptr->transformations & PNG_PACKSWAP) png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) if (png_ptr->transformations & PNG_PACK) png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) if (png_ptr->transformations & PNG_SHIFT) png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) if (png_ptr->transformations & PNG_BGR) png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined."); #endif #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) if (png_ptr->transformations & PNG_SWAP_BYTES) png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined."); #endif } #ifdef PNG_READ_INTERLACING_SUPPORTED /* If interlaced and we do not need a new row, combine row and return */ if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) { switch (png_ptr->pass) { case 0: if (png_ptr->row_number & 0x07) { if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 1: if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) { if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 2: if ((png_ptr->row_number & 0x07) != 4) { if (dsp_row != NULL && (png_ptr->row_number & 4)) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 3: if ((png_ptr->row_number & 3) || png_ptr->width < 3) { if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 4: if ((png_ptr->row_number & 3) != 2) { if (dsp_row != NULL && (png_ptr->row_number & 2)) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 5: if ((png_ptr->row_number & 1) || png_ptr->width < 2) { if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); png_read_finish_row(png_ptr); return; } break; case 6: if (!(png_ptr->row_number & 1)) { png_read_finish_row(png_ptr); return; } break; } } #endif if (!(png_ptr->mode & PNG_HAVE_IDAT)) png_error(png_ptr, "Invalid attempt to read row data"); png_ptr->zstream.next_out = png_ptr->row_buf; png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); do { if (!(png_ptr->zstream.avail_in)) { while (!png_ptr->idat_size) { png_crc_finish(png_ptr, 0); png_ptr->idat_size = png_read_chunk_header(png_ptr); if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) png_error(png_ptr, "Not enough image data"); } png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_in = png_ptr->zbuf; if (png_ptr->zbuf_size > png_ptr->idat_size) png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; png_crc_read(png_ptr, png_ptr->zbuf, (png_size_t)png_ptr->zstream.avail_in); png_ptr->idat_size -= png_ptr->zstream.avail_in; } ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret == Z_STREAM_END) { if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || png_ptr->idat_size) png_error(png_ptr, "Extra compressed data"); png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } if (ret != Z_OK) png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression error"); } while (png_ptr->zstream.avail_out); png_ptr->row_info.color_type = png_ptr->color_type; png_ptr->row_info.width = png_ptr->iwidth; png_ptr->row_info.channels = png_ptr->channels; png_ptr->row_info.bit_depth = png_ptr->bit_depth; png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->row_info.width); if (png_ptr->row_buf[0]) png_read_filter_row(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->prev_row + 1, (int)(png_ptr->row_buf[0])); png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1); #ifdef PNG_MNG_FEATURES_SUPPORTED if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) { /* Intrapixel differencing */ png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); } #endif if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) png_do_read_transformations(png_ptr); #ifdef PNG_READ_INTERLACING_SUPPORTED /* Blow up interlaced rows to full size */ if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) { if (png_ptr->pass < 6) /* Old interface (pre-1.0.9): * png_do_read_interlace(&(png_ptr->row_info), * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); */ png_do_read_interlace(png_ptr); if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]); if (row != NULL) png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]); } else #endif { if (row != NULL) png_combine_row(png_ptr, row, 0xff); if (dsp_row != NULL) png_combine_row(png_ptr, dsp_row, 0xff); } png_read_finish_row(png_ptr); if (png_ptr->read_row_fn != NULL) (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read one or more rows of image data. If the image is interlaced, * and png_set_interlace_handling() has been called, the rows need to * contain the contents of the rows from the previous pass. If the * image has alpha or transparency, and png_handle_alpha()[*] has been * called, the rows contents must be initialized to the contents of the * screen. * * "row" holds the actual image, and pixels are placed in it * as they arrive. If the image is displayed after each pass, it will * appear to "sparkle" in. "display_row" can be used to display a * "chunky" progressive image, with finer detail added as it becomes * available. If you do not want this "chunky" display, you may pass * NULL for display_row. If you do not want the sparkle display, and * you have not called png_handle_alpha(), you may pass NULL for rows. * If you have called png_handle_alpha(), and the image has either an * alpha channel or a transparency chunk, you must provide a buffer for * rows. In this case, you do not have to provide a display_row buffer * also, but you may. If the image is not interlaced, or if you have * not called png_set_interlace_handling(), the display_row buffer will * be ignored, so pass NULL to it. * * [*] png_handle_alpha() does not exist yet, as of this version of libpng */ void PNGAPI png_read_rows(png_structp png_ptr, png_bytepp row, png_bytepp display_row, png_uint_32 num_rows) { png_uint_32 i; png_bytepp rp; png_bytepp dp; png_debug(1, "in png_read_rows"); if (png_ptr == NULL) return; rp = row; dp = display_row; if (rp != NULL && dp != NULL) for (i = 0; i < num_rows; i++) { png_bytep rptr = *rp++; png_bytep dptr = *dp++; png_read_row(png_ptr, rptr, dptr); } else if (rp != NULL) for (i = 0; i < num_rows; i++) { png_bytep rptr = *rp; png_read_row(png_ptr, rptr, png_bytep_NULL); rp++; } else if (dp != NULL) for (i = 0; i < num_rows; i++) { png_bytep dptr = *dp; png_read_row(png_ptr, png_bytep_NULL, dptr); dp++; } } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the entire image. If the image has an alpha channel or a tRNS * chunk, and you have called png_handle_alpha()[*], you will need to * initialize the image to the current image that PNG will be overlaying. * We set the num_rows again here, in case it was incorrectly set in * png_read_start_row() by a call to png_read_update_info() or * png_start_read_image() if png_set_interlace_handling() wasn't called * prior to either of these functions like it should have been. You can * only call this function once. If you desire to have an image for * each pass of a interlaced image, use png_read_rows() instead. * * [*] png_handle_alpha() does not exist yet, as of this version of libpng */ void PNGAPI png_read_image(png_structp png_ptr, png_bytepp image) { png_uint_32 i, image_height; int pass, j; png_bytepp rp; png_debug(1, "in png_read_image"); if (png_ptr == NULL) return; #ifdef PNG_READ_INTERLACING_SUPPORTED pass = png_set_interlace_handling(png_ptr); #else if (png_ptr->interlaced) png_error(png_ptr, "Cannot read interlaced image -- interlace handler disabled."); pass = 1; #endif image_height=png_ptr->height; png_ptr->num_rows = image_height; /* Make sure this is set correctly */ for (j = 0; j < pass; j++) { rp = image; for (i = 0; i < image_height; i++) { png_read_row(png_ptr, *rp, png_bytep_NULL); rp++; } } } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the end of the PNG file. Will not read past the end of the * file, will verify the end is accurate, and will read any comments * or time information at the end of the file, if info is not NULL. */ void PNGAPI png_read_end(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_read_end"); if (png_ptr == NULL) return; png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ do { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IHDR; PNG_CONST PNG_IDAT; PNG_CONST PNG_IEND; PNG_CONST PNG_PLTE; #ifdef PNG_READ_bKGD_SUPPORTED PNG_CONST PNG_bKGD; #endif #ifdef PNG_READ_cHRM_SUPPORTED PNG_CONST PNG_cHRM; #endif #ifdef PNG_READ_gAMA_SUPPORTED PNG_CONST PNG_gAMA; #endif #ifdef PNG_READ_hIST_SUPPORTED PNG_CONST PNG_hIST; #endif #ifdef PNG_READ_iCCP_SUPPORTED PNG_CONST PNG_iCCP; #endif #ifdef PNG_READ_iTXt_SUPPORTED PNG_CONST PNG_iTXt; #endif #ifdef PNG_READ_oFFs_SUPPORTED PNG_CONST PNG_oFFs; #endif #ifdef PNG_READ_pCAL_SUPPORTED PNG_CONST PNG_pCAL; #endif #ifdef PNG_READ_pHYs_SUPPORTED PNG_CONST PNG_pHYs; #endif #ifdef PNG_READ_sBIT_SUPPORTED PNG_CONST PNG_sBIT; #endif #ifdef PNG_READ_sCAL_SUPPORTED PNG_CONST PNG_sCAL; #endif #ifdef PNG_READ_sPLT_SUPPORTED PNG_CONST PNG_sPLT; #endif #ifdef PNG_READ_sRGB_SUPPORTED PNG_CONST PNG_sRGB; #endif #ifdef PNG_READ_tEXt_SUPPORTED PNG_CONST PNG_tEXt; #endif #ifdef PNG_READ_tIME_SUPPORTED PNG_CONST PNG_tIME; #endif #ifdef PNG_READ_tRNS_SUPPORTED PNG_CONST PNG_tRNS; #endif #ifdef PNG_READ_zTXt_SUPPORTED PNG_CONST PNG_zTXt; #endif #endif /* PNG_USE_LOCAL_ARRAYS */ png_uint_32 length = png_read_chunk_header(png_ptr); PNG_CONST png_bytep chunk_name = png_ptr->chunk_name; if (!png_memcmp(chunk_name, png_IHDR, 4)) png_handle_IHDR(png_ptr, info_ptr, length); else if (!png_memcmp(chunk_name, png_IEND, 4)) png_handle_IEND(png_ptr, info_ptr, length); #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED else if (png_handle_as_unknown(png_ptr, chunk_name)) { if (!png_memcmp(chunk_name, png_IDAT, 4)) { if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) png_error(png_ptr, "Too many IDAT's found"); } png_handle_unknown(png_ptr, info_ptr, length); if (!png_memcmp(chunk_name, png_PLTE, 4)) png_ptr->mode |= PNG_HAVE_PLTE; } #endif else if (!png_memcmp(chunk_name, png_IDAT, 4)) { /* Zero length IDATs are legal after the last IDAT has been * read, but not after other chunks have been read. */ if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) png_error(png_ptr, "Too many IDAT's found"); png_crc_finish(png_ptr, length); } else if (!png_memcmp(chunk_name, png_PLTE, 4)) png_handle_PLTE(png_ptr, info_ptr, length); #ifdef PNG_READ_bKGD_SUPPORTED else if (!png_memcmp(chunk_name, png_bKGD, 4)) png_handle_bKGD(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_cHRM_SUPPORTED else if (!png_memcmp(chunk_name, png_cHRM, 4)) png_handle_cHRM(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_gAMA_SUPPORTED else if (!png_memcmp(chunk_name, png_gAMA, 4)) png_handle_gAMA(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_hIST_SUPPORTED else if (!png_memcmp(chunk_name, png_hIST, 4)) png_handle_hIST(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_oFFs_SUPPORTED else if (!png_memcmp(chunk_name, png_oFFs, 4)) png_handle_oFFs(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_pCAL_SUPPORTED else if (!png_memcmp(chunk_name, png_pCAL, 4)) png_handle_pCAL(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sCAL_SUPPORTED else if (!png_memcmp(chunk_name, png_sCAL, 4)) png_handle_sCAL(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_pHYs_SUPPORTED else if (!png_memcmp(chunk_name, png_pHYs, 4)) png_handle_pHYs(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sBIT_SUPPORTED else if (!png_memcmp(chunk_name, png_sBIT, 4)) png_handle_sBIT(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sRGB_SUPPORTED else if (!png_memcmp(chunk_name, png_sRGB, 4)) png_handle_sRGB(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_iCCP_SUPPORTED else if (!png_memcmp(chunk_name, png_iCCP, 4)) png_handle_iCCP(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_sPLT_SUPPORTED else if (!png_memcmp(chunk_name, png_sPLT, 4)) png_handle_sPLT(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tEXt_SUPPORTED else if (!png_memcmp(chunk_name, png_tEXt, 4)) png_handle_tEXt(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tIME_SUPPORTED else if (!png_memcmp(chunk_name, png_tIME, 4)) png_handle_tIME(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_tRNS_SUPPORTED else if (!png_memcmp(chunk_name, png_tRNS, 4)) png_handle_tRNS(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_zTXt_SUPPORTED else if (!png_memcmp(chunk_name, png_zTXt, 4)) png_handle_zTXt(png_ptr, info_ptr, length); #endif #ifdef PNG_READ_iTXt_SUPPORTED else if (!png_memcmp(chunk_name, png_iTXt, 4)) png_handle_iTXt(png_ptr, info_ptr, length); #endif else png_handle_unknown(png_ptr, info_ptr, length); } while (!(png_ptr->mode & PNG_HAVE_IEND)); } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ /* Free all memory used by the read */ void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr) { png_structp png_ptr = NULL; png_infop info_ptr = NULL, end_info_ptr = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn = NULL; png_voidp mem_ptr = NULL; #endif png_debug(1, "in png_destroy_read_struct"); if (png_ptr_ptr != NULL) png_ptr = *png_ptr_ptr; if (png_ptr == NULL) return; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; mem_ptr = png_ptr->mem_ptr; #endif if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (end_info_ptr_ptr != NULL) end_info_ptr = *end_info_ptr_ptr; png_read_destroy(png_ptr, info_ptr, end_info_ptr); if (info_ptr != NULL) { #ifdef PNG_TEXT_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = NULL; } if (end_info_ptr != NULL) { #ifdef PNG_READ_TEXT_SUPPORTED png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)end_info_ptr); #endif *end_info_ptr_ptr = NULL; } if (png_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif *png_ptr_ptr = NULL; } } /* Free all memory used by the read (old method) */ void /* PRIVATE */ png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr) { #ifdef PNG_SETJMP_SUPPORTED jmp_buf tmp_jmp; #endif png_error_ptr error_fn; png_error_ptr warning_fn; png_voidp error_ptr; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn; #endif png_debug(1, "in png_read_destroy"); if (info_ptr != NULL) png_info_destroy(png_ptr, info_ptr); if (end_info_ptr != NULL) png_info_destroy(png_ptr, end_info_ptr); png_free(png_ptr, png_ptr->zbuf); png_free(png_ptr, png_ptr->big_row_buf); png_free(png_ptr, png_ptr->prev_row); png_free(png_ptr, png_ptr->chunkdata); #ifdef PNG_READ_DITHER_SUPPORTED png_free(png_ptr, png_ptr->palette_lookup); png_free(png_ptr, png_ptr->dither_index); #endif #ifdef PNG_READ_GAMMA_SUPPORTED png_free(png_ptr, png_ptr->gamma_table); #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED png_free(png_ptr, png_ptr->gamma_from_1); png_free(png_ptr, png_ptr->gamma_to_1); #endif #ifdef PNG_FREE_ME_SUPPORTED if (png_ptr->free_me & PNG_FREE_PLTE) png_zfree(png_ptr, png_ptr->palette); png_ptr->free_me &= ~PNG_FREE_PLTE; #else if (png_ptr->flags & PNG_FLAG_FREE_PLTE) png_zfree(png_ptr, png_ptr->palette); png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; #endif #if defined(PNG_tRNS_SUPPORTED) || \ defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) #ifdef PNG_FREE_ME_SUPPORTED if (png_ptr->free_me & PNG_FREE_TRNS) png_free(png_ptr, png_ptr->trans); png_ptr->free_me &= ~PNG_FREE_TRNS; #else if (png_ptr->flags & PNG_FLAG_FREE_TRNS) png_free(png_ptr, png_ptr->trans); png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; #endif #endif #ifdef PNG_READ_hIST_SUPPORTED #ifdef PNG_FREE_ME_SUPPORTED if (png_ptr->free_me & PNG_FREE_HIST) png_free(png_ptr, png_ptr->hist); png_ptr->free_me &= ~PNG_FREE_HIST; #else if (png_ptr->flags & PNG_FLAG_FREE_HIST) png_free(png_ptr, png_ptr->hist); png_ptr->flags &= ~PNG_FLAG_FREE_HIST; #endif #endif #ifdef PNG_READ_GAMMA_SUPPORTED if (png_ptr->gamma_16_table != NULL) { int i; int istop = (1 << (8 - png_ptr->gamma_shift)); for (i = 0; i < istop; i++) { png_free(png_ptr, png_ptr->gamma_16_table[i]); } png_free(png_ptr, png_ptr->gamma_16_table); } #ifdef PNG_READ_BACKGROUND_SUPPORTED if (png_ptr->gamma_16_from_1 != NULL) { int i; int istop = (1 << (8 - png_ptr->gamma_shift)); for (i = 0; i < istop; i++) { png_free(png_ptr, png_ptr->gamma_16_from_1[i]); } png_free(png_ptr, png_ptr->gamma_16_from_1); } if (png_ptr->gamma_16_to_1 != NULL) { int i; int istop = (1 << (8 - png_ptr->gamma_shift)); for (i = 0; i < istop; i++) { png_free(png_ptr, png_ptr->gamma_16_to_1[i]); } png_free(png_ptr, png_ptr->gamma_16_to_1); } #endif #endif #ifdef PNG_TIME_RFC1123_SUPPORTED png_free(png_ptr, png_ptr->time_buffer); #endif inflateEnd(&png_ptr->zstream); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED png_free(png_ptr, png_ptr->save_buffer); #endif #ifdef PNG_PROGRESSIVE_READ_SUPPORTED #ifdef PNG_TEXT_SUPPORTED png_free(png_ptr, png_ptr->current_text); #endif /* PNG_TEXT_SUPPORTED */ #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ /* Save the important info out of the png_struct, in case it is * being used again. */ #ifdef PNG_SETJMP_SUPPORTED png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); #endif error_fn = png_ptr->error_fn; warning_fn = png_ptr->warning_fn; error_ptr = png_ptr->error_ptr; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; #endif png_memset(png_ptr, 0, png_sizeof(png_struct)); png_ptr->error_fn = error_fn; png_ptr->warning_fn = warning_fn; png_ptr->error_ptr = error_ptr; #ifdef PNG_USER_MEM_SUPPORTED png_ptr->free_fn = free_fn; #endif #ifdef PNG_SETJMP_SUPPORTED png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); #endif } void PNGAPI png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) { if (png_ptr == NULL) return; png_ptr->read_row_fn = read_row_fn; } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI png_read_png(png_structp png_ptr, png_infop info_ptr, int transforms, voidp params) { int row; if (png_ptr == NULL) return; #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel from opacity to transparency */ if (transforms & PNG_TRANSFORM_INVERT_ALPHA) png_set_invert_alpha(png_ptr); #endif /* png_read_info() gives us all of the information from the * PNG file before the first IDAT (image data chunk). */ png_read_info(png_ptr, info_ptr); if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) png_error(png_ptr, "Image is too high to process with png_read_png()"); /* -------------- image transformations start here ------------------- */ #ifdef PNG_READ_16_TO_8_SUPPORTED /* Tell libpng to strip 16 bit/color files down to 8 bits per color. */ if (transforms & PNG_TRANSFORM_STRIP_16) png_set_strip_16(png_ptr); #endif #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED /* Strip alpha bytes from the input data without combining with * the background (not recommended). */ if (transforms & PNG_TRANSFORM_STRIP_ALPHA) png_set_strip_alpha(png_ptr); #endif #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single * byte into separate bytes (useful for paletted and grayscale images). */ if (transforms & PNG_TRANSFORM_PACKING) png_set_packing(png_ptr); #endif #ifdef PNG_READ_PACKSWAP_SUPPORTED /* Change the order of packed pixels to least significant bit first * (not useful if you are using png_set_packing). */ if (transforms & PNG_TRANSFORM_PACKSWAP) png_set_packswap(png_ptr); #endif #ifdef PNG_READ_EXPAND_SUPPORTED /* Expand paletted colors into true RGB triplets * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel * Expand paletted or RGB images with transparency to full alpha * channels so the data will be available as RGBA quartets. */ if (transforms & PNG_TRANSFORM_EXPAND) if ((png_ptr->bit_depth < 8) || (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) png_set_expand(png_ptr); #endif /* We don't handle background color or gamma transformation or dithering. */ #ifdef PNG_READ_INVERT_SUPPORTED /* Invert monochrome files to have 0 as white and 1 as black */ if (transforms & PNG_TRANSFORM_INVERT_MONO) png_set_invert_mono(png_ptr); #endif #ifdef PNG_READ_SHIFT_SUPPORTED /* If you want to shift the pixel values from the range [0,255] or * [0,65535] to the original [0,7] or [0,31], or whatever range the * colors were originally in: */ if ((transforms & PNG_TRANSFORM_SHIFT) && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) { png_color_8p sig_bit; png_get_sBIT(png_ptr, info_ptr, &sig_bit); png_set_shift(png_ptr, sig_bit); } #endif #ifdef PNG_READ_BGR_SUPPORTED /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ if (transforms & PNG_TRANSFORM_BGR) png_set_bgr(png_ptr); #endif #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ if (transforms & PNG_TRANSFORM_SWAP_ALPHA) png_set_swap_alpha(png_ptr); #endif #ifdef PNG_READ_SWAP_SUPPORTED /* Swap bytes of 16 bit files to least significant byte first */ if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) png_set_swap(png_ptr); #endif /* Added at libpng-1.2.41 */ #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel from opacity to transparency */ if (transforms & PNG_TRANSFORM_INVERT_ALPHA) png_set_invert_alpha(png_ptr); #endif /* Added at libpng-1.2.41 */ #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand grayscale image to RGB */ if (transforms & PNG_TRANSFORM_GRAY_TO_RGB) png_set_gray_to_rgb(png_ptr); #endif /* We don't handle adding filler bytes */ /* Optional call to gamma correct and add the background to the palette * and update info structure. REQUIRED if you are expecting libpng to * update the palette for you (i.e., you selected such a transform above). */ png_read_update_info(png_ptr, info_ptr); /* -------------- image transformations end here ------------------- */ #ifdef PNG_FREE_ME_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); #endif if (info_ptr->row_pointers == NULL) { info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, info_ptr->height * png_sizeof(png_bytep)); png_memset(info_ptr->row_pointers, 0, info_ptr->height * png_sizeof(png_bytep)); #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_ROWS; #endif for (row = 0; row < (int)info_ptr->height; row++) info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); } png_read_image(png_ptr, info_ptr->row_pointers); info_ptr->valid |= PNG_INFO_IDAT; /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ png_read_end(png_ptr, info_ptr); transforms = transforms; /* Quiet compiler warnings */ params = params; } #endif /* PNG_INFO_IMAGE_SUPPORTED */ #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ #endif /* PNG_READ_SUPPORTED */ glmark2-2012.08/./src/libpng/pngmem.c0000664000175000017500000004223712013417376016417 0ustar alfalf00000000000000 /* pngmem.c - stub functions for memory allocation * * Last changed in libpng 1.2.41 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file provides a location for all memory allocation. Users who * need special memory handling are expected to supply replacement * functions for png_malloc() and png_free(), and to use * png_create_read_struct_2() and png_create_write_struct_2() to * identify the replacement functions. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) /* Borland DOS special memory handler */ #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) /* If you change this, be sure to change the one in png.h also */ /* Allocate memory for a png_struct. The malloc and memset can be replaced by a single call to calloc() if this is thought to improve performance. */ png_voidp /* PRIVATE */ png_create_struct(int type) { #ifdef PNG_USER_MEM_SUPPORTED return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); } /* Alternate version of png_create_struct, for use with user-defined malloc. */ png_voidp /* PRIVATE */ png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) { #endif /* PNG_USER_MEM_SUPPORTED */ png_size_t size; png_voidp struct_ptr; if (type == PNG_STRUCT_INFO) size = png_sizeof(png_info); else if (type == PNG_STRUCT_PNG) size = png_sizeof(png_struct); else return (png_get_copyright(NULL)); #ifdef PNG_USER_MEM_SUPPORTED if (malloc_fn != NULL) { png_struct dummy_struct; png_structp png_ptr = &dummy_struct; png_ptr->mem_ptr=mem_ptr; struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); } else #endif /* PNG_USER_MEM_SUPPORTED */ struct_ptr = (png_voidp)farmalloc(size); if (struct_ptr != NULL) png_memset(struct_ptr, 0, size); return (struct_ptr); } /* Free memory allocated by a png_create_struct() call */ void /* PRIVATE */ png_destroy_struct(png_voidp struct_ptr) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); } /* Free memory allocated by a png_create_struct() call */ void /* PRIVATE */ png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, png_voidp mem_ptr) { #endif if (struct_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED if (free_fn != NULL) { png_struct dummy_struct; png_structp png_ptr = &dummy_struct; png_ptr->mem_ptr=mem_ptr; (*(free_fn))(png_ptr, struct_ptr); return; } #endif /* PNG_USER_MEM_SUPPORTED */ farfree (struct_ptr); } } /* Allocate memory. For reasonable files, size should never exceed * 64K. However, zlib may allocate more then 64K if you don't tell * it not to. See zconf.h and png.h for more information. zlib does * need to allocate exactly 64K, so whatever you call here must * have the ability to do that. * * Borland seems to have a problem in DOS mode for exactly 64K. * It gives you a segment with an offset of 8 (perhaps to store its * memory stuff). zlib doesn't like this at all, so we have to * detect and deal with it. This code should not be needed in * Windows or OS/2 modes, and only in 16 bit mode. This code has * been updated by Alexander Lehmann for version 0.89 to waste less * memory. * * Note that we can't use png_size_t for the "size" declaration, * since on some systems a png_size_t is a 16-bit quantity, and as a * result, we would be truncating potentially larger memory requests * (which should cause a fatal error) and introducing major problems. */ png_voidp /* PRIVATE */ png_calloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; ret = (png_malloc(png_ptr, size)); if (ret != NULL) png_memset(ret,0,(png_size_t)size); return (ret); } png_voidp PNGAPI png_malloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; if (png_ptr == NULL || size == 0) return (NULL); #ifdef PNG_USER_MEM_SUPPORTED if (png_ptr->malloc_fn != NULL) ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); else ret = (png_malloc_default(png_ptr, size)); if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out of memory!"); return (ret); } png_voidp PNGAPI png_malloc_default(png_structp png_ptr, png_uint_32 size) { png_voidp ret; #endif /* PNG_USER_MEM_SUPPORTED */ if (png_ptr == NULL || size == 0) return (NULL); #ifdef PNG_MAX_MALLOC_64K if (size > (png_uint_32)65536L) { png_warning(png_ptr, "Cannot Allocate > 64K"); ret = NULL; } else #endif if (size != (size_t)size) ret = NULL; else if (size == (png_uint_32)65536L) { if (png_ptr->offset_table == NULL) { /* Try to see if we need to do any of this fancy stuff */ ret = farmalloc(size); if (ret == NULL || ((png_size_t)ret & 0xffff)) { int num_blocks; png_uint_32 total_size; png_bytep table; int i; png_byte huge * hptr; if (ret != NULL) { farfree(ret); ret = NULL; } if (png_ptr->zlib_window_bits > 14) num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); else num_blocks = 1; if (png_ptr->zlib_mem_level >= 7) num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); else num_blocks++; total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; table = farmalloc(total_size); if (table == NULL) { #ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out Of Memory."); /* Note "O", "M" */ else png_warning(png_ptr, "Out Of Memory."); #endif return (NULL); } if ((png_size_t)table & 0xfff0) { #ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Farmalloc didn't return normalized pointer"); else png_warning(png_ptr, "Farmalloc didn't return normalized pointer"); #endif return (NULL); } png_ptr->offset_table = table; png_ptr->offset_table_ptr = farmalloc(num_blocks * png_sizeof(png_bytep)); if (png_ptr->offset_table_ptr == NULL) { #ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out Of memory."); /* Note "O", "m" */ else png_warning(png_ptr, "Out Of memory."); #endif return (NULL); } hptr = (png_byte huge *)table; if ((png_size_t)hptr & 0xf) { hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ } for (i = 0; i < num_blocks; i++) { png_ptr->offset_table_ptr[i] = (png_bytep)hptr; hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ } png_ptr->offset_table_number = num_blocks; png_ptr->offset_table_count = 0; png_ptr->offset_table_count_free = 0; } } if (png_ptr->offset_table_count >= png_ptr->offset_table_number) { #ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ else png_warning(png_ptr, "Out of Memory."); #endif return (NULL); } ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; } else ret = farmalloc(size); #ifndef PNG_USER_MEM_SUPPORTED if (ret == NULL) { if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ else png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */ } #endif return (ret); } /* Free a pointer allocated by png_malloc(). In the default * configuration, png_ptr is not used, but is passed in case it * is needed. If ptr is NULL, return without taking any action. */ void PNGAPI png_free(png_structp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; #ifdef PNG_USER_MEM_SUPPORTED if (png_ptr->free_fn != NULL) { (*(png_ptr->free_fn))(png_ptr, ptr); return; } else png_free_default(png_ptr, ptr); } void PNGAPI png_free_default(png_structp png_ptr, png_voidp ptr) { #endif /* PNG_USER_MEM_SUPPORTED */ if (png_ptr == NULL || ptr == NULL) return; if (png_ptr->offset_table != NULL) { int i; for (i = 0; i < png_ptr->offset_table_count; i++) { if (ptr == png_ptr->offset_table_ptr[i]) { ptr = NULL; png_ptr->offset_table_count_free++; break; } } if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) { farfree(png_ptr->offset_table); farfree(png_ptr->offset_table_ptr); png_ptr->offset_table = NULL; png_ptr->offset_table_ptr = NULL; } } if (ptr != NULL) { farfree(ptr); } } #else /* Not the Borland DOS special memory handler */ /* Allocate memory for a png_struct or a png_info. The malloc and memset can be replaced by a single call to calloc() if this is thought to improve performance noticably. */ png_voidp /* PRIVATE */ png_create_struct(int type) { #ifdef PNG_USER_MEM_SUPPORTED return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); } /* Allocate memory for a png_struct or a png_info. The malloc and memset can be replaced by a single call to calloc() if this is thought to improve performance noticably. */ png_voidp /* PRIVATE */ png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) { #endif /* PNG_USER_MEM_SUPPORTED */ png_size_t size; png_voidp struct_ptr; if (type == PNG_STRUCT_INFO) size = png_sizeof(png_info); else if (type == PNG_STRUCT_PNG) size = png_sizeof(png_struct); else return (NULL); #ifdef PNG_USER_MEM_SUPPORTED if (malloc_fn != NULL) { png_struct dummy_struct; png_structp png_ptr = &dummy_struct; png_ptr->mem_ptr=mem_ptr; struct_ptr = (*(malloc_fn))(png_ptr, size); if (struct_ptr != NULL) png_memset(struct_ptr, 0, size); return (struct_ptr); } #endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) struct_ptr = (png_voidp)farmalloc(size); #else # if defined(_MSC_VER) && defined(MAXSEG_64K) struct_ptr = (png_voidp)halloc(size, 1); # else struct_ptr = (png_voidp)malloc(size); # endif #endif if (struct_ptr != NULL) png_memset(struct_ptr, 0, size); return (struct_ptr); } /* Free memory allocated by a png_create_struct() call */ void /* PRIVATE */ png_destroy_struct(png_voidp struct_ptr) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); } /* Free memory allocated by a png_create_struct() call */ void /* PRIVATE */ png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, png_voidp mem_ptr) { #endif /* PNG_USER_MEM_SUPPORTED */ if (struct_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED if (free_fn != NULL) { png_struct dummy_struct; png_structp png_ptr = &dummy_struct; png_ptr->mem_ptr=mem_ptr; (*(free_fn))(png_ptr, struct_ptr); return; } #endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) farfree(struct_ptr); #else # if defined(_MSC_VER) && defined(MAXSEG_64K) hfree(struct_ptr); # else free(struct_ptr); # endif #endif } } /* Allocate memory. For reasonable files, size should never exceed * 64K. However, zlib may allocate more then 64K if you don't tell * it not to. See zconf.h and png.h for more information. zlib does * need to allocate exactly 64K, so whatever you call here must * have the ability to do that. */ png_voidp /* PRIVATE */ png_calloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; ret = (png_malloc(png_ptr, size)); if (ret != NULL) png_memset(ret,0,(png_size_t)size); return (ret); } png_voidp PNGAPI png_malloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; #ifdef PNG_USER_MEM_SUPPORTED if (png_ptr == NULL || size == 0) return (NULL); if (png_ptr->malloc_fn != NULL) ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); else ret = (png_malloc_default(png_ptr, size)); if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out of Memory!"); return (ret); } png_voidp PNGAPI png_malloc_default(png_structp png_ptr, png_uint_32 size) { png_voidp ret; #endif /* PNG_USER_MEM_SUPPORTED */ if (png_ptr == NULL || size == 0) return (NULL); #ifdef PNG_MAX_MALLOC_64K if (size > (png_uint_32)65536L) { #ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Cannot Allocate > 64K"); else #endif return NULL; } #endif /* Check for overflow */ #if defined(__TURBOC__) && !defined(__FLAT__) if (size != (unsigned long)size) ret = NULL; else ret = farmalloc(size); #else # if defined(_MSC_VER) && defined(MAXSEG_64K) if (size != (unsigned long)size) ret = NULL; else ret = halloc(size, 1); # else if (size != (size_t)size) ret = NULL; else ret = malloc((size_t)size); # endif #endif #ifndef PNG_USER_MEM_SUPPORTED if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) png_error(png_ptr, "Out of Memory"); #endif return (ret); } /* Free a pointer allocated by png_malloc(). If ptr is NULL, return * without taking any action. */ void PNGAPI png_free(png_structp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; #ifdef PNG_USER_MEM_SUPPORTED if (png_ptr->free_fn != NULL) { (*(png_ptr->free_fn))(png_ptr, ptr); return; } else png_free_default(png_ptr, ptr); } void PNGAPI png_free_default(png_structp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; #endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) farfree(ptr); #else # if defined(_MSC_VER) && defined(MAXSEG_64K) hfree(ptr); # else free(ptr); # endif #endif } #endif /* Not Borland DOS special memory handler */ #ifdef PNG_1_0_X # define png_malloc_warn png_malloc #else /* This function was added at libpng version 1.2.3. The png_malloc_warn() * function will set up png_malloc() to issue a png_warning and return NULL * instead of issuing a png_error, if it fails to allocate the requested * memory. */ png_voidp PNGAPI png_malloc_warn(png_structp png_ptr, png_uint_32 size) { png_voidp ptr; png_uint_32 save_flags; if (png_ptr == NULL) return (NULL); save_flags = png_ptr->flags; png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); png_ptr->flags=save_flags; return(ptr); } #endif png_voidp PNGAPI png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, png_uint_32 length) { png_size_t size; size = (png_size_t)length; if ((png_uint_32)size != length) png_error(png_ptr, "Overflow in png_memcpy_check."); return(png_memcpy (s1, s2, size)); } png_voidp PNGAPI png_memset_check (png_structp png_ptr, png_voidp s1, int value, png_uint_32 length) { png_size_t size; size = (png_size_t)length; if ((png_uint_32)size != length) png_error(png_ptr, "Overflow in png_memset_check."); return (png_memset (s1, value, size)); } #ifdef PNG_USER_MEM_SUPPORTED /* This function is called when the application wants to use another method * of allocating and freeing memory. */ void PNGAPI png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn) { if (png_ptr != NULL) { png_ptr->mem_ptr = mem_ptr; png_ptr->malloc_fn = malloc_fn; png_ptr->free_fn = free_fn; } } /* This function returns a pointer to the mem_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy and png_read_destroy are called. */ png_voidp PNGAPI png_get_mem_ptr(png_structp png_ptr) { if (png_ptr == NULL) return (NULL); return ((png_voidp)png_ptr->mem_ptr); } #endif /* PNG_USER_MEM_SUPPORTED */ #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngrutil.c0000664000175000017500000030017412013417376016775 0ustar alfalf00000000000000 /* pngrutil.c - utilities to read a PNG file * * Last changed in libpng 1.2.45 [July 7, 2011] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file contains routines that are only called from within * libpng itself during the course of reading an image. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_READ_SUPPORTED #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500) # define WIN32_WCE_OLD #endif #ifdef PNG_FLOATING_POINT_SUPPORTED # ifdef WIN32_WCE_OLD /* The strtod() function is not supported on WindowsCE */ __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr) { double result = 0; int len; wchar_t *str, *end; len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0); str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t)); if ( NULL != str ) { MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len); result = wcstod(str, &end); len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL); *endptr = (char *)nptr + (png_strlen(nptr) - len + 1); png_free(png_ptr, str); } return result; } # else # define png_strtod(p,a,b) strtod(a,b) # endif #endif png_uint_32 PNGAPI png_get_uint_31(png_structp png_ptr, png_bytep buf) { #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED png_uint_32 i = png_get_uint_32(buf); #else /* Avoid an extra function call by inlining the result. */ png_uint_32 i = ((png_uint_32)(*buf) << 24) + ((png_uint_32)(*(buf + 1)) << 16) + ((png_uint_32)(*(buf + 2)) << 8) + (png_uint_32)(*(buf + 3)); #endif if (i > PNG_UINT_31_MAX) png_error(png_ptr, "PNG unsigned integer out of range."); return (i); } #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ png_uint_32 PNGAPI png_get_uint_32(png_bytep buf) { png_uint_32 i = ((png_uint_32)(*buf) << 24) + ((png_uint_32)(*(buf + 1)) << 16) + ((png_uint_32)(*(buf + 2)) << 8) + (png_uint_32)(*(buf + 3)); return (i); } /* Grab a signed 32-bit integer from a buffer in big-endian format. The * data is stored in the PNG file in two's complement format, and it is * assumed that the machine format for signed integers is the same. */ png_int_32 PNGAPI png_get_int_32(png_bytep buf) { png_int_32 i = ((png_int_32)(*buf) << 24) + ((png_int_32)(*(buf + 1)) << 16) + ((png_int_32)(*(buf + 2)) << 8) + (png_int_32)(*(buf + 3)); return (i); } /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ png_uint_16 PNGAPI png_get_uint_16(png_bytep buf) { png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + (png_uint_16)(*(buf + 1))); return (i); } #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ /* Read the chunk header (length + type name). * Put the type name into png_ptr->chunk_name, and return the length. */ png_uint_32 /* PRIVATE */ png_read_chunk_header(png_structp png_ptr) { png_byte buf[8]; png_uint_32 length; /* Read the length and the chunk name */ png_read_data(png_ptr, buf, 8); length = png_get_uint_31(png_ptr, buf); /* Put the chunk name into png_ptr->chunk_name */ png_memcpy(png_ptr->chunk_name, buf + 4, 4); png_debug2(0, "Reading %s chunk, length = %lu", png_ptr->chunk_name, length); /* Reset the crc and run it over the chunk name */ png_reset_crc(png_ptr); png_calculate_crc(png_ptr, png_ptr->chunk_name, 4); /* Check to see if chunk name is valid */ png_check_chunk_name(png_ptr, png_ptr->chunk_name); return length; } /* Read data, and (optionally) run it through the CRC. */ void /* PRIVATE */ png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) { if (png_ptr == NULL) return; png_read_data(png_ptr, buf, length); png_calculate_crc(png_ptr, buf, length); } /* Optionally skip data and then check the CRC. Depending on whether we * are reading a ancillary or critical chunk, and how the program has set * things up, we may calculate the CRC on the data and print a message. * Returns '1' if there was a CRC error, '0' otherwise. */ int /* PRIVATE */ png_crc_finish(png_structp png_ptr, png_uint_32 skip) { png_size_t i; png_size_t istop = png_ptr->zbuf_size; for (i = (png_size_t)skip; i > istop; i -= istop) { png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); } if (i) { png_crc_read(png_ptr, png_ptr->zbuf, i); } if (png_crc_error(png_ptr)) { if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */ !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))) { png_chunk_warning(png_ptr, "CRC error"); } else { png_chunk_error(png_ptr, "CRC error"); } return (1); } return (0); } /* Compare the CRC stored in the PNG file with that calculated by libpng from * the data it has read thus far. */ int /* PRIVATE */ png_crc_error(png_structp png_ptr) { png_byte crc_bytes[4]; png_uint_32 crc; int need_crc = 1; if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ { if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) need_crc = 0; } else /* critical */ { if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) need_crc = 0; } png_read_data(png_ptr, crc_bytes, 4); if (need_crc) { crc = png_get_uint_32(crc_bytes); return ((int)(crc != png_ptr->crc)); } else return (0); } #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ defined(PNG_READ_iCCP_SUPPORTED) static png_size_t png_inflate(png_structp png_ptr, const png_byte *data, png_size_t size, png_bytep output, png_size_t output_size) { png_size_t count = 0; png_ptr->zstream.next_in = (png_bytep)data; /* const_cast: VALID */ png_ptr->zstream.avail_in = size; while (1) { int ret, avail; /* Reset the output buffer each time round - we empty it * after every inflate call. */ png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = png_ptr->zbuf_size; ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out; /* First copy/count any new output - but only if we didn't * get an error code. */ if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0) { if (output != 0 && output_size > count) { int copy = output_size - count; if (avail < copy) copy = avail; png_memcpy(output + count, png_ptr->zbuf, copy); } count += avail; } if (ret == Z_OK) continue; /* Termination conditions - always reset the zstream, it * must be left in inflateInit state. */ png_ptr->zstream.avail_in = 0; inflateReset(&png_ptr->zstream); if (ret == Z_STREAM_END) return count; /* NOTE: may be zero. */ /* Now handle the error codes - the API always returns 0 * and the error message is dumped into the uncompressed * buffer if available. */ { PNG_CONST char *msg; if (png_ptr->zstream.msg != 0) msg = png_ptr->zstream.msg; else { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char umsg[52]; switch (ret) { case Z_BUF_ERROR: msg = "Buffer error in compressed datastream in %s chunk"; break; case Z_DATA_ERROR: msg = "Data error in compressed datastream in %s chunk"; break; default: msg = "Incomplete compressed datastream in %s chunk"; break; } png_snprintf(umsg, sizeof umsg, msg, png_ptr->chunk_name); msg = umsg; #else msg = "Damaged compressed datastream in chunk other than IDAT"; #endif } png_warning(png_ptr, msg); } /* 0 means an error - notice that this code simple ignores * zero length compressed chunks as a result. */ return 0; } } /* * Decompress trailing data in a chunk. The assumption is that chunkdata * points at an allocated area holding the contents of a chunk with a * trailing compressed part. What we get back is an allocated area * holding the original prefix part and an uncompressed version of the * trailing part (the malloc area passed in is freed). */ void /* PRIVATE */ png_decompress_chunk(png_structp png_ptr, int comp_type, png_size_t chunklength, png_size_t prefix_size, png_size_t *newlength) { /* The caller should guarantee this */ if (prefix_size > chunklength) { /* The recovery is to delete the chunk. */ png_warning(png_ptr, "invalid chunklength"); prefix_size = 0; /* To delete everything */ } else if (comp_type == PNG_COMPRESSION_TYPE_BASE) { png_size_t expanded_size = png_inflate(png_ptr, (png_bytep)(png_ptr->chunkdata + prefix_size), chunklength - prefix_size, 0/*output*/, 0/*output size*/); /* Now check the limits on this chunk - if the limit fails the * compressed data will be removed, the prefix will remain. */ #ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED if (png_ptr->user_chunk_malloc_max && (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1)) #else # ifdef PNG_USER_CHUNK_MALLOC_MAX if ((PNG_USER_CHUNK_MALLOC_MAX > 0) && prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1) # endif #endif png_warning(png_ptr, "Exceeded size limit while expanding chunk"); /* If the size is zero either there was an error and a message * has already been output (warning) or the size really is zero * and we have nothing to do - the code will exit through the * error case below. */ #if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \ defined(PNG_USER_CHUNK_MALLOC_MAX) else #endif if (expanded_size > 0) { /* Success (maybe) - really uncompress the chunk. */ png_size_t new_size = 0; png_charp text = png_malloc_warn(png_ptr, prefix_size + expanded_size + 1); if (text != NULL) { png_memcpy(text, png_ptr->chunkdata, prefix_size); new_size = png_inflate(png_ptr, (png_bytep)(png_ptr->chunkdata + prefix_size), chunklength - prefix_size, (png_bytep)(text + prefix_size), expanded_size); text[prefix_size + expanded_size] = 0; /* just in case */ if (new_size == expanded_size) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = text; *newlength = prefix_size + expanded_size; return; /* The success return! */ } png_warning(png_ptr, "png_inflate logic error"); png_free(png_ptr, text); } else png_warning(png_ptr, "Not enough memory to decompress chunk."); } } else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char umsg[50]; png_snprintf(umsg, sizeof umsg, "Unknown zTXt compression type %d", comp_type); png_warning(png_ptr, umsg); #else png_warning(png_ptr, "Unknown zTXt compression type"); #endif /* The recovery is to simply drop the data. */ } /* Generic error return - leave the prefix, delete the compressed * data, reallocate the chunkdata to remove the potentially large * amount of compressed data. */ { png_charp text = png_malloc_warn(png_ptr, prefix_size + 1); if (text != NULL) { if (prefix_size > 0) png_memcpy(text, png_ptr->chunkdata, prefix_size); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = text; /* This is an extra zero in the 'uncompressed' part. */ *(png_ptr->chunkdata + prefix_size) = 0x00; } /* Ignore a malloc error here - it is safe. */ } *newlength = prefix_size; } #endif /* Read and check the IDHR chunk */ void /* PRIVATE */ png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[13]; png_uint_32 width, height; int bit_depth, color_type, compression_type, filter_type; int interlace_type; png_debug(1, "in png_handle_IHDR"); if (png_ptr->mode & PNG_HAVE_IHDR) png_error(png_ptr, "Out of place IHDR"); /* Check the length */ if (length != 13) png_error(png_ptr, "Invalid IHDR chunk"); png_ptr->mode |= PNG_HAVE_IHDR; png_crc_read(png_ptr, buf, 13); png_crc_finish(png_ptr, 0); width = png_get_uint_31(png_ptr, buf); height = png_get_uint_31(png_ptr, buf + 4); bit_depth = buf[8]; color_type = buf[9]; compression_type = buf[10]; filter_type = buf[11]; interlace_type = buf[12]; /* Set internal variables */ png_ptr->width = width; png_ptr->height = height; png_ptr->bit_depth = (png_byte)bit_depth; png_ptr->interlaced = (png_byte)interlace_type; png_ptr->color_type = (png_byte)color_type; #ifdef PNG_MNG_FEATURES_SUPPORTED png_ptr->filter_type = (png_byte)filter_type; #endif png_ptr->compression_type = (png_byte)compression_type; /* Find number of channels */ switch (png_ptr->color_type) { case PNG_COLOR_TYPE_GRAY: case PNG_COLOR_TYPE_PALETTE: png_ptr->channels = 1; break; case PNG_COLOR_TYPE_RGB: png_ptr->channels = 3; break; case PNG_COLOR_TYPE_GRAY_ALPHA: png_ptr->channels = 2; break; case PNG_COLOR_TYPE_RGB_ALPHA: png_ptr->channels = 4; break; } /* Set up other useful info */ png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels); png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); png_debug1(3, "channels = %d", png_ptr->channels); png_debug1(3, "rowbytes = %lu", png_ptr->rowbytes); png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, interlace_type, compression_type, filter_type); } /* Read and check the palette */ void /* PRIVATE */ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_color palette[PNG_MAX_PALETTE_LENGTH]; int num, i; #ifdef PNG_POINTER_INDEXING_SUPPORTED png_colorp pal_ptr; #endif png_debug(1, "in png_handle_PLTE"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before PLTE"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid PLTE after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) png_error(png_ptr, "Duplicate PLTE chunk"); png_ptr->mode |= PNG_HAVE_PLTE; if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) { png_warning(png_ptr, "Ignoring PLTE chunk in grayscale PNG"); png_crc_finish(png_ptr, length); return; } #ifndef PNG_READ_OPT_PLTE_SUPPORTED if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) { png_crc_finish(png_ptr, length); return; } #endif if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) { if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) { png_warning(png_ptr, "Invalid palette chunk"); png_crc_finish(png_ptr, length); return; } else { png_error(png_ptr, "Invalid palette chunk"); } } num = (int)length / 3; #ifdef PNG_POINTER_INDEXING_SUPPORTED for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) { png_byte buf[3]; png_crc_read(png_ptr, buf, 3); pal_ptr->red = buf[0]; pal_ptr->green = buf[1]; pal_ptr->blue = buf[2]; } #else for (i = 0; i < num; i++) { png_byte buf[3]; png_crc_read(png_ptr, buf, 3); /* Don't depend upon png_color being any order */ palette[i].red = buf[0]; palette[i].green = buf[1]; palette[i].blue = buf[2]; } #endif /* If we actually NEED the PLTE chunk (ie for a paletted image), we do * whatever the normal CRC configuration tells us. However, if we * have an RGB image, the PLTE can be considered ancillary, so * we will act as though it is. */ #ifndef PNG_READ_OPT_PLTE_SUPPORTED if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) #endif { png_crc_finish(png_ptr, 0); } #ifndef PNG_READ_OPT_PLTE_SUPPORTED else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ { /* If we don't want to use the data from an ancillary chunk, we have two options: an error abort, or a warning and we ignore the data in this chunk (which should be OK, since it's considered ancillary for a RGB or RGBA image). */ if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) { if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) { png_chunk_error(png_ptr, "CRC error"); } else { png_chunk_warning(png_ptr, "CRC error"); return; } } /* Otherwise, we (optionally) emit a warning and use the chunk. */ else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) { png_chunk_warning(png_ptr, "CRC error"); } } #endif png_set_PLTE(png_ptr, info_ptr, palette, num); #ifdef PNG_READ_tRNS_SUPPORTED if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) { if (png_ptr->num_trans > (png_uint_16)num) { png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); png_ptr->num_trans = (png_uint_16)num; } if (info_ptr->num_trans > (png_uint_16)num) { png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); info_ptr->num_trans = (png_uint_16)num; } } } #endif } void /* PRIVATE */ png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_debug(1, "in png_handle_IEND"); if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) { png_error(png_ptr, "No image in file"); } png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); if (length != 0) { png_warning(png_ptr, "Incorrect IEND chunk length"); } png_crc_finish(png_ptr, length); info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */ } #ifdef PNG_READ_gAMA_SUPPORTED void /* PRIVATE */ png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_fixed_point igamma; #ifdef PNG_FLOATING_POINT_SUPPORTED float file_gamma; #endif png_byte buf[4]; png_debug(1, "in png_handle_gAMA"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before gAMA"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid gAMA after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place gAMA chunk"); if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) #ifdef PNG_READ_sRGB_SUPPORTED && !(info_ptr->valid & PNG_INFO_sRGB) #endif ) { png_warning(png_ptr, "Duplicate gAMA chunk"); png_crc_finish(png_ptr, length); return; } if (length != 4) { png_warning(png_ptr, "Incorrect gAMA chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 4); if (png_crc_finish(png_ptr, 0)) return; igamma = (png_fixed_point)png_get_uint_32(buf); /* Check for zero gamma */ if (igamma == 0) { png_warning(png_ptr, "Ignoring gAMA chunk with gamma=0"); return; } #ifdef PNG_READ_sRGB_SUPPORTED if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) { png_warning(png_ptr, "Ignoring incorrect gAMA value when sRGB is also present"); #ifdef PNG_CONSOLE_IO_SUPPORTED fprintf(stderr, "gamma = (%d/100000)", (int)igamma); #endif return; } #endif /* PNG_READ_sRGB_SUPPORTED */ #ifdef PNG_FLOATING_POINT_SUPPORTED file_gamma = (float)igamma / (float)100000.0; # ifdef PNG_READ_GAMMA_SUPPORTED png_ptr->gamma = file_gamma; # endif png_set_gAMA(png_ptr, info_ptr, file_gamma); #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_set_gAMA_fixed(png_ptr, info_ptr, igamma); #endif } #endif #ifdef PNG_READ_sBIT_SUPPORTED void /* PRIVATE */ png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_size_t truelen; png_byte buf[4]; png_debug(1, "in png_handle_sBIT"); buf[0] = buf[1] = buf[2] = buf[3] = 0; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sBIT"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid sBIT after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) { /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place sBIT chunk"); } if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) { png_warning(png_ptr, "Duplicate sBIT chunk"); png_crc_finish(png_ptr, length); return; } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) truelen = 3; else truelen = (png_size_t)png_ptr->channels; if (length != truelen || length > 4) { png_warning(png_ptr, "Incorrect sBIT chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, truelen); if (png_crc_finish(png_ptr, 0)) return; if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) { png_ptr->sig_bit.red = buf[0]; png_ptr->sig_bit.green = buf[1]; png_ptr->sig_bit.blue = buf[2]; png_ptr->sig_bit.alpha = buf[3]; } else { png_ptr->sig_bit.gray = buf[0]; png_ptr->sig_bit.red = buf[0]; png_ptr->sig_bit.green = buf[0]; png_ptr->sig_bit.blue = buf[0]; png_ptr->sig_bit.alpha = buf[1]; } png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); } #endif #ifdef PNG_READ_cHRM_SUPPORTED void /* PRIVATE */ png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[32]; #ifdef PNG_FLOATING_POINT_SUPPORTED float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; #endif png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, int_y_green, int_x_blue, int_y_blue; png_uint_32 uint_x, uint_y; png_debug(1, "in png_handle_cHRM"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before cHRM"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid cHRM after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Missing PLTE before cHRM"); if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) #ifdef PNG_READ_sRGB_SUPPORTED && !(info_ptr->valid & PNG_INFO_sRGB) #endif ) { png_warning(png_ptr, "Duplicate cHRM chunk"); png_crc_finish(png_ptr, length); return; } if (length != 32) { png_warning(png_ptr, "Incorrect cHRM chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 32); if (png_crc_finish(png_ptr, 0)) return; uint_x = png_get_uint_32(buf); uint_y = png_get_uint_32(buf + 4); int_x_white = (png_fixed_point)uint_x; int_y_white = (png_fixed_point)uint_y; uint_x = png_get_uint_32(buf + 8); uint_y = png_get_uint_32(buf + 12); int_x_red = (png_fixed_point)uint_x; int_y_red = (png_fixed_point)uint_y; uint_x = png_get_uint_32(buf + 16); uint_y = png_get_uint_32(buf + 20); int_x_green = (png_fixed_point)uint_x; int_y_green = (png_fixed_point)uint_y; uint_x = png_get_uint_32(buf + 24); uint_y = png_get_uint_32(buf + 28); int_x_blue = (png_fixed_point)uint_x; int_y_blue = (png_fixed_point)uint_y; #ifdef PNG_FLOATING_POINT_SUPPORTED white_x = (float)int_x_white / (float)100000.0; white_y = (float)int_y_white / (float)100000.0; red_x = (float)int_x_red / (float)100000.0; red_y = (float)int_y_red / (float)100000.0; green_x = (float)int_x_green / (float)100000.0; green_y = (float)int_y_green / (float)100000.0; blue_x = (float)int_x_blue / (float)100000.0; blue_y = (float)int_y_blue / (float)100000.0; #endif #ifdef PNG_READ_sRGB_SUPPORTED if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB)) { if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) || PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) || PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) || PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) || PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) || PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) || PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) || PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000)) { png_warning(png_ptr, "Ignoring incorrect cHRM value when sRGB is also present"); #ifdef PNG_CONSOLE_IO_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n", white_x, white_y, red_x, red_y); fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n", green_x, green_y, blue_x, blue_y); #else fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", (long)int_x_white, (long)int_y_white, (long)int_x_red, (long)int_y_red); fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n", (long)int_x_green, (long)int_y_green, (long)int_x_blue, (long)int_y_blue); #endif #endif /* PNG_CONSOLE_IO_SUPPORTED */ } return; } #endif /* PNG_READ_sRGB_SUPPORTED */ #ifdef PNG_FLOATING_POINT_SUPPORTED png_set_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_set_cHRM_fixed(png_ptr, info_ptr, int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, int_y_green, int_x_blue, int_y_blue); #endif } #endif #ifdef PNG_READ_sRGB_SUPPORTED void /* PRIVATE */ png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { int intent; png_byte buf[1]; png_debug(1, "in png_handle_sRGB"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sRGB"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid sRGB after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place sRGB chunk"); if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) { png_warning(png_ptr, "Duplicate sRGB chunk"); png_crc_finish(png_ptr, length); return; } if (length != 1) { png_warning(png_ptr, "Incorrect sRGB chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 1); if (png_crc_finish(png_ptr, 0)) return; intent = buf[0]; /* Check for bad intent */ if (intent >= PNG_sRGB_INTENT_LAST) { png_warning(png_ptr, "Unknown sRGB intent"); return; } #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)) { png_fixed_point igamma; #ifdef PNG_FIXED_POINT_SUPPORTED igamma=info_ptr->int_gamma; #else # ifdef PNG_FLOATING_POINT_SUPPORTED igamma=(png_fixed_point)(info_ptr->gamma * 100000.); # endif #endif if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) { png_warning(png_ptr, "Ignoring incorrect gAMA value when sRGB is also present"); #ifdef PNG_CONSOLE_IO_SUPPORTED # ifdef PNG_FIXED_POINT_SUPPORTED fprintf(stderr, "incorrect gamma=(%d/100000)\n", (int)png_ptr->int_gamma); # else # ifdef PNG_FLOATING_POINT_SUPPORTED fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma); # endif # endif #endif } } #endif /* PNG_READ_gAMA_SUPPORTED */ #ifdef PNG_READ_cHRM_SUPPORTED #ifdef PNG_FIXED_POINT_SUPPORTED if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) || PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000)) { png_warning(png_ptr, "Ignoring incorrect cHRM value when sRGB is also present"); } #endif /* PNG_FIXED_POINT_SUPPORTED */ #endif /* PNG_READ_cHRM_SUPPORTED */ png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); } #endif /* PNG_READ_sRGB_SUPPORTED */ #ifdef PNG_READ_iCCP_SUPPORTED void /* PRIVATE */ png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) /* Note: this does not properly handle chunks that are > 64K under DOS */ { png_byte compression_type; png_bytep pC; png_charp profile; png_uint_32 skip = 0; png_uint_32 profile_size, profile_length; png_size_t slength, prefix_length, data_length; png_debug(1, "in png_handle_iCCP"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before iCCP"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid iCCP after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place iCCP chunk"); if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)) { png_warning(png_ptr, "Duplicate iCCP chunk"); png_crc_finish(png_ptr, length); return; } #ifdef PNG_MAX_MALLOC_64K if (length > (png_uint_32)65535L) { png_warning(png_ptr, "iCCP chunk too large to fit in memory"); skip = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, skip)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; for (profile = png_ptr->chunkdata; *profile; profile++) /* Empty loop to find end of name */ ; ++profile; /* There should be at least one zero (the compression type byte) * following the separator, and we should be on it */ if ( profile >= png_ptr->chunkdata + slength - 1) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "Malformed iCCP chunk"); return; } /* Compression_type should always be zero */ compression_type = *profile++; if (compression_type) { png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 wrote nonzero) */ } prefix_length = profile - png_ptr->chunkdata; png_decompress_chunk(png_ptr, compression_type, slength, prefix_length, &data_length); profile_length = data_length - prefix_length; if ( prefix_length > data_length || profile_length < 4) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "Profile size field missing from iCCP chunk"); return; } /* Check the profile_size recorded in the first 32 bits of the ICC profile */ pC = (png_bytep)(png_ptr->chunkdata + prefix_length); profile_size = ((*(pC ))<<24) | ((*(pC + 1))<<16) | ((*(pC + 2))<< 8) | ((*(pC + 3)) ); if (profile_size < profile_length) profile_length = profile_size; if (profile_size > profile_length) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "Ignoring truncated iCCP profile."); return; } png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata, compression_type, png_ptr->chunkdata + prefix_length, profile_length); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; } #endif /* PNG_READ_iCCP_SUPPORTED */ #ifdef PNG_READ_sPLT_SUPPORTED void /* PRIVATE */ png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) /* Note: this does not properly handle chunks that are > 64K under DOS */ { png_bytep entry_start; png_sPLT_t new_palette; #ifdef PNG_POINTER_INDEXING_SUPPORTED png_sPLT_entryp pp; #endif int data_length, entry_size, i; png_uint_32 skip = 0; png_size_t slength; png_debug(1, "in png_handle_sPLT"); #ifdef PNG_USER_LIMITS_SUPPORTED if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) { png_crc_finish(png_ptr, length); return; } if (--png_ptr->user_chunk_cache_max == 1) { png_warning(png_ptr, "No space in chunk cache for sPLT"); png_crc_finish(png_ptr, length); return; } } #endif if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sPLT"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid sPLT after IDAT"); png_crc_finish(png_ptr, length); return; } #ifdef PNG_MAX_MALLOC_64K if (length > (png_uint_32)65535L) { png_warning(png_ptr, "sPLT chunk too large to fit in memory"); skip = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, skip)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++) /* Empty loop to find end of name */ ; ++entry_start; /* A sample depth should follow the separator, and we should be on it */ if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "malformed sPLT chunk"); return; } new_palette.depth = *entry_start++; entry_size = (new_palette.depth == 8 ? 6 : 10); data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata)); /* Integrity-check the data length */ if (data_length % entry_size) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "sPLT chunk has bad length"); return; } new_palette.nentries = (png_int_32) ( data_length / entry_size); if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry))) { png_warning(png_ptr, "sPLT chunk too long"); return; } new_palette.entries = (png_sPLT_entryp)png_malloc_warn( png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); if (new_palette.entries == NULL) { png_warning(png_ptr, "sPLT chunk requires too much memory"); return; } #ifdef PNG_POINTER_INDEXING_SUPPORTED for (i = 0; i < new_palette.nentries; i++) { pp = new_palette.entries + i; if (new_palette.depth == 8) { pp->red = *entry_start++; pp->green = *entry_start++; pp->blue = *entry_start++; pp->alpha = *entry_start++; } else { pp->red = png_get_uint_16(entry_start); entry_start += 2; pp->green = png_get_uint_16(entry_start); entry_start += 2; pp->blue = png_get_uint_16(entry_start); entry_start += 2; pp->alpha = png_get_uint_16(entry_start); entry_start += 2; } pp->frequency = png_get_uint_16(entry_start); entry_start += 2; } #else pp = new_palette.entries; for (i = 0; i < new_palette.nentries; i++) { if (new_palette.depth == 8) { pp[i].red = *entry_start++; pp[i].green = *entry_start++; pp[i].blue = *entry_start++; pp[i].alpha = *entry_start++; } else { pp[i].red = png_get_uint_16(entry_start); entry_start += 2; pp[i].green = png_get_uint_16(entry_start); entry_start += 2; pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; } pp->frequency = png_get_uint_16(entry_start); entry_start += 2; } #endif /* Discard all chunk data except the name and stash that */ new_palette.name = png_ptr->chunkdata; png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_free(png_ptr, new_palette.entries); } #endif /* PNG_READ_sPLT_SUPPORTED */ #ifdef PNG_READ_tRNS_SUPPORTED void /* PRIVATE */ png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; png_debug(1, "in png_handle_tRNS"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before tRNS"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid tRNS after IDAT"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) { png_warning(png_ptr, "Duplicate tRNS chunk"); png_crc_finish(png_ptr, length); return; } if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) { png_byte buf[2]; if (length != 2) { png_warning(png_ptr, "Incorrect tRNS chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 2); png_ptr->num_trans = 1; png_ptr->trans_values.gray = png_get_uint_16(buf); } else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) { png_byte buf[6]; if (length != 6) { png_warning(png_ptr, "Incorrect tRNS chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, (png_size_t)length); png_ptr->num_trans = 1; png_ptr->trans_values.red = png_get_uint_16(buf); png_ptr->trans_values.green = png_get_uint_16(buf + 2); png_ptr->trans_values.blue = png_get_uint_16(buf + 4); } else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { if (!(png_ptr->mode & PNG_HAVE_PLTE)) { /* Should be an error, but we can cope with it. */ png_warning(png_ptr, "Missing PLTE before tRNS"); } if (length > (png_uint_32)png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH) { png_warning(png_ptr, "Incorrect tRNS chunk length"); png_crc_finish(png_ptr, length); return; } if (length == 0) { png_warning(png_ptr, "Zero length tRNS chunk"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, readbuf, (png_size_t)length); png_ptr->num_trans = (png_uint_16)length; } else { png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); png_crc_finish(png_ptr, length); return; } if (png_crc_finish(png_ptr, 0)) { png_ptr->num_trans = 0; return; } png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, &(png_ptr->trans_values)); } #endif #ifdef PNG_READ_bKGD_SUPPORTED void /* PRIVATE */ png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_size_t truelen; png_byte buf[6]; png_debug(1, "in png_handle_bKGD"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before bKGD"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid bKGD after IDAT"); png_crc_finish(png_ptr, length); return; } else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) { png_warning(png_ptr, "Missing PLTE before bKGD"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) { png_warning(png_ptr, "Duplicate bKGD chunk"); png_crc_finish(png_ptr, length); return; } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) truelen = 1; else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) truelen = 6; else truelen = 2; if (length != truelen) { png_warning(png_ptr, "Incorrect bKGD chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, truelen); if (png_crc_finish(png_ptr, 0)) return; /* We convert the index value into RGB components so that we can allow * arbitrary RGB values for background when we have transparency, and * so it is easy to determine the RGB values of the background color * from the info_ptr struct. */ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { png_ptr->background.index = buf[0]; if (info_ptr && info_ptr->num_palette) { if (buf[0] >= info_ptr->num_palette) { png_warning(png_ptr, "Incorrect bKGD chunk index value"); return; } png_ptr->background.red = (png_uint_16)png_ptr->palette[buf[0]].red; png_ptr->background.green = (png_uint_16)png_ptr->palette[buf[0]].green; png_ptr->background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; } } else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ { png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray = png_get_uint_16(buf); } else { png_ptr->background.red = png_get_uint_16(buf); png_ptr->background.green = png_get_uint_16(buf + 2); png_ptr->background.blue = png_get_uint_16(buf + 4); } png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); } #endif #ifdef PNG_READ_hIST_SUPPORTED void /* PRIVATE */ png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { unsigned int num, i; png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; png_debug(1, "in png_handle_hIST"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before hIST"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid hIST after IDAT"); png_crc_finish(png_ptr, length); return; } else if (!(png_ptr->mode & PNG_HAVE_PLTE)) { png_warning(png_ptr, "Missing PLTE before hIST"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) { png_warning(png_ptr, "Duplicate hIST chunk"); png_crc_finish(png_ptr, length); return; } num = length / 2 ; if (num != (unsigned int) png_ptr->num_palette || num > (unsigned int) PNG_MAX_PALETTE_LENGTH) { png_warning(png_ptr, "Incorrect hIST chunk length"); png_crc_finish(png_ptr, length); return; } for (i = 0; i < num; i++) { png_byte buf[2]; png_crc_read(png_ptr, buf, 2); readbuf[i] = png_get_uint_16(buf); } if (png_crc_finish(png_ptr, 0)) return; png_set_hIST(png_ptr, info_ptr, readbuf); } #endif #ifdef PNG_READ_pHYs_SUPPORTED void /* PRIVATE */ png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[9]; png_uint_32 res_x, res_y; int unit_type; png_debug(1, "in png_handle_pHYs"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before pHYs"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid pHYs after IDAT"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) { png_warning(png_ptr, "Duplicate pHYs chunk"); png_crc_finish(png_ptr, length); return; } if (length != 9) { png_warning(png_ptr, "Incorrect pHYs chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 9); if (png_crc_finish(png_ptr, 0)) return; res_x = png_get_uint_32(buf); res_y = png_get_uint_32(buf + 4); unit_type = buf[8]; png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); } #endif #ifdef PNG_READ_oFFs_SUPPORTED void /* PRIVATE */ png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[9]; png_int_32 offset_x, offset_y; int unit_type; png_debug(1, "in png_handle_oFFs"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before oFFs"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid oFFs after IDAT"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) { png_warning(png_ptr, "Duplicate oFFs chunk"); png_crc_finish(png_ptr, length); return; } if (length != 9) { png_warning(png_ptr, "Incorrect oFFs chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 9); if (png_crc_finish(png_ptr, 0)) return; offset_x = png_get_int_32(buf); offset_y = png_get_int_32(buf + 4); unit_type = buf[8]; png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); } #endif #ifdef PNG_READ_pCAL_SUPPORTED /* Read the pCAL chunk (described in the PNG Extensions document) */ void /* PRIVATE */ png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_int_32 X0, X1; png_byte type, nparams; png_charp buf, units, endptr; png_charpp params; png_size_t slength; int i; png_debug(1, "in png_handle_pCAL"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before pCAL"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid pCAL after IDAT"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) { png_warning(png_ptr, "Duplicate pCAL chunk"); png_crc_finish(png_ptr, length); return; } png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)", length + 1); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); if (png_ptr->chunkdata == NULL) { png_warning(png_ptr, "No memory for pCAL purpose."); return; } slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, 0)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */ png_debug(3, "Finding end of pCAL purpose string"); for (buf = png_ptr->chunkdata; *buf; buf++) /* Empty loop */ ; endptr = png_ptr->chunkdata + slength; /* We need to have at least 12 bytes after the purpose string in order to get the parameter information. */ if (endptr <= buf + 12) { png_warning(png_ptr, "Invalid pCAL data"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); X0 = png_get_int_32((png_bytep)buf+1); X1 = png_get_int_32((png_bytep)buf+5); type = buf[9]; nparams = buf[10]; units = buf + 11; png_debug(3, "Checking pCAL equation type and number of parameters"); /* Check that we have the right number of parameters for known equation types. */ if ((type == PNG_EQUATION_LINEAR && nparams != 2) || (type == PNG_EQUATION_BASE_E && nparams != 3) || (type == PNG_EQUATION_ARBITRARY && nparams != 3) || (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) { png_warning(png_ptr, "Invalid pCAL parameters for equation type"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } else if (type >= PNG_EQUATION_LAST) { png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); } for (buf = units; *buf; buf++) /* Empty loop to move past the units string. */ ; png_debug(3, "Allocating pCAL parameters array"); params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams * png_sizeof(png_charp))) ; if (params == NULL) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_warning(png_ptr, "No memory for pCAL params."); return; } /* Get pointers to the start of each parameter string. */ for (i = 0; i < (int)nparams; i++) { buf++; /* Skip the null string terminator from previous parameter. */ png_debug1(3, "Reading pCAL parameter %d", i); for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++) /* Empty loop to move past each parameter string */ ; /* Make sure we haven't run out of data yet */ if (buf > endptr) { png_warning(png_ptr, "Invalid pCAL data"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_free(png_ptr, params); return; } } png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams, units, params); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_free(png_ptr, params); } #endif #ifdef PNG_READ_sCAL_SUPPORTED /* Read the sCAL chunk */ void /* PRIVATE */ png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_charp ep; #ifdef PNG_FLOATING_POINT_SUPPORTED double width, height; png_charp vp; #else #ifdef PNG_FIXED_POINT_SUPPORTED png_charp swidth, sheight; #endif #endif png_size_t slength; png_debug(1, "in png_handle_sCAL"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sCAL"); else if (png_ptr->mode & PNG_HAVE_IDAT) { png_warning(png_ptr, "Invalid sCAL after IDAT"); png_crc_finish(png_ptr, length); return; } else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) { png_warning(png_ptr, "Duplicate sCAL chunk"); png_crc_finish(png_ptr, length); return; } /* Need unit type, width, \0, height: minimum 4 bytes */ else if (length < 4) { png_warning(png_ptr, "sCAL chunk too short"); png_crc_finish(png_ptr, length); return; } png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)", length + 1); png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); if (png_ptr->chunkdata == NULL) { png_warning(png_ptr, "Out of memory while processing sCAL chunk"); png_crc_finish(png_ptr, length); return; } slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, 0)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */ ep = png_ptr->chunkdata + 1; /* Skip unit byte */ #ifdef PNG_FLOATING_POINT_SUPPORTED width = png_strtod(png_ptr, ep, &vp); if (*vp) { png_warning(png_ptr, "malformed width string in sCAL chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } #else #ifdef PNG_FIXED_POINT_SUPPORTED swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); if (swidth == NULL) { png_warning(png_ptr, "Out of memory while processing sCAL chunk width"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_memcpy(swidth, ep, (png_size_t)png_strlen(ep)); #endif #endif for (ep = png_ptr->chunkdata; *ep; ep++) /* Empty loop */ ; ep++; if (png_ptr->chunkdata + slength < ep) { png_warning(png_ptr, "Truncated sCAL chunk"); #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, swidth); #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } #ifdef PNG_FLOATING_POINT_SUPPORTED height = png_strtod(png_ptr, ep, &vp); if (*vp) { png_warning(png_ptr, "malformed height string in sCAL chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, swidth); #endif return; } #else #ifdef PNG_FIXED_POINT_SUPPORTED sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); if (sheight == NULL) { png_warning(png_ptr, "Out of memory while processing sCAL chunk height"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, swidth); #endif return; } png_memcpy(sheight, ep, (png_size_t)png_strlen(ep)); #endif #endif if (png_ptr->chunkdata + slength < ep #ifdef PNG_FLOATING_POINT_SUPPORTED || width <= 0. || height <= 0. #endif ) { png_warning(png_ptr, "Invalid sCAL data"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, swidth); png_free(png_ptr, sheight); #endif return; } #ifdef PNG_FLOATING_POINT_SUPPORTED png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height); #else #ifdef PNG_FIXED_POINT_SUPPORTED png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight); #endif #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, swidth); png_free(png_ptr, sheight); #endif } #endif #ifdef PNG_READ_tIME_SUPPORTED void /* PRIVATE */ png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[7]; png_time mod_time; png_debug(1, "in png_handle_tIME"); if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Out of place tIME chunk"); else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) { png_warning(png_ptr, "Duplicate tIME chunk"); png_crc_finish(png_ptr, length); return; } if (png_ptr->mode & PNG_HAVE_IDAT) png_ptr->mode |= PNG_AFTER_IDAT; if (length != 7) { png_warning(png_ptr, "Incorrect tIME chunk length"); png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 7); if (png_crc_finish(png_ptr, 0)) return; mod_time.second = buf[6]; mod_time.minute = buf[5]; mod_time.hour = buf[4]; mod_time.day = buf[3]; mod_time.month = buf[2]; mod_time.year = png_get_uint_16(buf); png_set_tIME(png_ptr, info_ptr, &mod_time); } #endif #ifdef PNG_READ_tEXt_SUPPORTED /* Note: this does not properly handle chunks that are > 64K under DOS */ void /* PRIVATE */ png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_textp text_ptr; png_charp key; png_charp text; png_uint_32 skip = 0; png_size_t slength; int ret; png_debug(1, "in png_handle_tEXt"); #ifdef PNG_USER_LIMITS_SUPPORTED if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) { png_crc_finish(png_ptr, length); return; } if (--png_ptr->user_chunk_cache_max == 1) { png_warning(png_ptr, "No space in chunk cache for tEXt"); png_crc_finish(png_ptr, length); return; } } #endif if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before tEXt"); if (png_ptr->mode & PNG_HAVE_IDAT) png_ptr->mode |= PNG_AFTER_IDAT; #ifdef PNG_MAX_MALLOC_64K if (length > (png_uint_32)65535L) { png_warning(png_ptr, "tEXt chunk too large to fit in memory"); skip = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); if (png_ptr->chunkdata == NULL) { png_warning(png_ptr, "No memory to process text chunk."); return; } slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, skip)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } key = png_ptr->chunkdata; key[slength] = 0x00; for (text = key; *text; text++) /* Empty loop to find end of key */ ; if (text != key + slength) text++; text_ptr = (png_textp)png_malloc_warn(png_ptr, (png_uint_32)png_sizeof(png_text)); if (text_ptr == NULL) { png_warning(png_ptr, "Not enough memory to process text chunk."); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; text_ptr->key = key; #ifdef PNG_iTXt_SUPPORTED text_ptr->lang = NULL; text_ptr->lang_key = NULL; text_ptr->itxt_length = 0; #endif text_ptr->text = text; text_ptr->text_length = png_strlen(text); ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; png_free(png_ptr, text_ptr); if (ret) png_warning(png_ptr, "Insufficient memory to process text chunk."); } #endif #ifdef PNG_READ_zTXt_SUPPORTED /* Note: this does not correctly handle chunks that are > 64K under DOS */ void /* PRIVATE */ png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_textp text_ptr; png_charp text; int comp_type; int ret; png_size_t slength, prefix_len, data_len; png_debug(1, "in png_handle_zTXt"); #ifdef PNG_USER_LIMITS_SUPPORTED if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) { png_crc_finish(png_ptr, length); return; } if (--png_ptr->user_chunk_cache_max == 1) { png_warning(png_ptr, "No space in chunk cache for zTXt"); png_crc_finish(png_ptr, length); return; } } #endif if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before zTXt"); if (png_ptr->mode & PNG_HAVE_IDAT) png_ptr->mode |= PNG_AFTER_IDAT; #ifdef PNG_MAX_MALLOC_64K /* We will no doubt have problems with chunks even half this size, but there is no hard and fast rule to tell us where to stop. */ if (length > (png_uint_32)65535L) { png_warning(png_ptr, "zTXt chunk too large to fit in memory"); png_crc_finish(png_ptr, length); return; } #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); if (png_ptr->chunkdata == NULL) { png_warning(png_ptr, "Out of memory processing zTXt chunk."); return; } slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, 0)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; for (text = png_ptr->chunkdata; *text; text++) /* Empty loop */ ; /* zTXt must have some text after the chunkdataword */ if (text >= png_ptr->chunkdata + slength - 2) { png_warning(png_ptr, "Truncated zTXt chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } else { comp_type = *(++text); if (comp_type != PNG_TEXT_COMPRESSION_zTXt) { png_warning(png_ptr, "Unknown compression type in zTXt chunk"); comp_type = PNG_TEXT_COMPRESSION_zTXt; } text++; /* Skip the compression_method byte */ } prefix_len = text - png_ptr->chunkdata; png_decompress_chunk(png_ptr, comp_type, (png_size_t)length, prefix_len, &data_len); text_ptr = (png_textp)png_malloc_warn(png_ptr, (png_uint_32)png_sizeof(png_text)); if (text_ptr == NULL) { png_warning(png_ptr, "Not enough memory to process zTXt chunk."); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } text_ptr->compression = comp_type; text_ptr->key = png_ptr->chunkdata; #ifdef PNG_iTXt_SUPPORTED text_ptr->lang = NULL; text_ptr->lang_key = NULL; text_ptr->itxt_length = 0; #endif text_ptr->text = png_ptr->chunkdata + prefix_len; text_ptr->text_length = data_len; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_free(png_ptr, text_ptr); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; if (ret) png_error(png_ptr, "Insufficient memory to store zTXt chunk."); } #endif #ifdef PNG_READ_iTXt_SUPPORTED /* Note: this does not correctly handle chunks that are > 64K under DOS */ void /* PRIVATE */ png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_textp text_ptr; png_charp key, lang, text, lang_key; int comp_flag; int comp_type = 0; int ret; png_size_t slength, prefix_len, data_len; png_debug(1, "in png_handle_iTXt"); #ifdef PNG_USER_LIMITS_SUPPORTED if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) { png_crc_finish(png_ptr, length); return; } if (--png_ptr->user_chunk_cache_max == 1) { png_warning(png_ptr, "No space in chunk cache for iTXt"); png_crc_finish(png_ptr, length); return; } } #endif if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before iTXt"); if (png_ptr->mode & PNG_HAVE_IDAT) png_ptr->mode |= PNG_AFTER_IDAT; #ifdef PNG_MAX_MALLOC_64K /* We will no doubt have problems with chunks even half this size, but there is no hard and fast rule to tell us where to stop. */ if (length > (png_uint_32)65535L) { png_warning(png_ptr, "iTXt chunk too large to fit in memory"); png_crc_finish(png_ptr, length); return; } #endif png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); if (png_ptr->chunkdata == NULL) { png_warning(png_ptr, "No memory to process iTXt chunk."); return; } slength = (png_size_t)length; png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); if (png_crc_finish(png_ptr, 0)) { png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } png_ptr->chunkdata[slength] = 0x00; for (lang = png_ptr->chunkdata; *lang; lang++) /* Empty loop */ ; lang++; /* Skip NUL separator */ /* iTXt must have a language tag (possibly empty), two compression bytes, * translated keyword (possibly empty), and possibly some text after the * keyword */ if (lang >= png_ptr->chunkdata + slength - 3) { png_warning(png_ptr, "Truncated iTXt chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } else { comp_flag = *lang++; comp_type = *lang++; } for (lang_key = lang; *lang_key; lang_key++) /* Empty loop */ ; lang_key++; /* Skip NUL separator */ if (lang_key >= png_ptr->chunkdata + slength) { png_warning(png_ptr, "Truncated iTXt chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } for (text = lang_key; *text; text++) /* Empty loop */ ; text++; /* Skip NUL separator */ if (text >= png_ptr->chunkdata + slength) { png_warning(png_ptr, "Malformed iTXt chunk"); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } prefix_len = text - png_ptr->chunkdata; key=png_ptr->chunkdata; if (comp_flag) png_decompress_chunk(png_ptr, comp_type, (size_t)length, prefix_len, &data_len); else data_len = png_strlen(png_ptr->chunkdata + prefix_len); text_ptr = (png_textp)png_malloc_warn(png_ptr, (png_uint_32)png_sizeof(png_text)); if (text_ptr == NULL) { png_warning(png_ptr, "Not enough memory to process iTXt chunk."); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; return; } text_ptr->compression = (int)comp_flag + 1; text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key); text_ptr->lang = png_ptr->chunkdata + (lang - key); text_ptr->itxt_length = data_len; text_ptr->text_length = 0; text_ptr->key = png_ptr->chunkdata; text_ptr->text = png_ptr->chunkdata + prefix_len; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_free(png_ptr, text_ptr); png_free(png_ptr, png_ptr->chunkdata); png_ptr->chunkdata = NULL; if (ret) png_error(png_ptr, "Insufficient memory to store iTXt chunk."); } #endif /* This function is called when we haven't found a handler for a chunk. If there isn't a problem with the chunk itself (ie bad chunk name, CRC, or a critical chunk), the chunk is silently ignored -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which case it will be saved away to be written out later. */ void /* PRIVATE */ png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_uint_32 skip = 0; png_debug(1, "in png_handle_unknown"); #ifdef PNG_USER_LIMITS_SUPPORTED if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) { png_crc_finish(png_ptr, length); return; } if (--png_ptr->user_chunk_cache_max == 1) { png_warning(png_ptr, "No space in chunk cache for unknown chunk"); png_crc_finish(png_ptr, length); return; } } #endif if (png_ptr->mode & PNG_HAVE_IDAT) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IDAT; #endif if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* Not an IDAT */ png_ptr->mode |= PNG_AFTER_IDAT; } if (!(png_ptr->chunk_name[0] & 0x20)) { #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != PNG_HANDLE_CHUNK_ALWAYS #ifdef PNG_READ_USER_CHUNKS_SUPPORTED && png_ptr->read_user_chunk_fn == NULL #endif ) #endif png_chunk_error(png_ptr, "unknown critical chunk"); } #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) #ifdef PNG_READ_USER_CHUNKS_SUPPORTED || (png_ptr->read_user_chunk_fn != NULL) #endif ) { #ifdef PNG_MAX_MALLOC_64K if (length > (png_uint_32)65535L) { png_warning(png_ptr, "unknown chunk too large to fit in memory"); skip = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_memcpy((png_charp)png_ptr->unknown_chunk.name, (png_charp)png_ptr->chunk_name, png_sizeof(png_ptr->unknown_chunk.name)); png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0'; png_ptr->unknown_chunk.size = (png_size_t)length; if (length == 0) png_ptr->unknown_chunk.data = NULL; else { png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length); } #ifdef PNG_READ_USER_CHUNKS_SUPPORTED if (png_ptr->read_user_chunk_fn != NULL) { /* Callback to user unknown chunk handler */ int ret; ret = (*(png_ptr->read_user_chunk_fn)) (png_ptr, &png_ptr->unknown_chunk); if (ret < 0) png_chunk_error(png_ptr, "error in user chunk"); if (ret == 0) { if (!(png_ptr->chunk_name[0] & 0x20)) #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != PNG_HANDLE_CHUNK_ALWAYS) #endif png_chunk_error(png_ptr, "unknown critical chunk"); png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); } } else #endif png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); png_free(png_ptr, png_ptr->unknown_chunk.data); png_ptr->unknown_chunk.data = NULL; } else #endif skip = length; png_crc_finish(png_ptr, skip); #ifndef PNG_READ_USER_CHUNKS_SUPPORTED info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */ #endif } /* This function is called to verify that a chunk name is valid. This function can't have the "critical chunk check" incorporated into it, since in the future we will need to be able to call user functions to handle unknown critical chunks after we check that the chunk name itself is valid. */ #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) void /* PRIVATE */ png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) { png_debug(1, "in png_check_chunk_name"); if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) { png_chunk_error(png_ptr, "invalid chunk type"); } } /* Combines the row recently read in with the existing pixels in the row. This routine takes care of alpha and transparency if requested. This routine also handles the two methods of progressive display of interlaced images, depending on the mask value. The mask value describes which pixels are to be combined with the row. The pattern always repeats every 8 pixels, so just 8 bits are needed. A one indicates the pixel is to be combined, a zero indicates the pixel is to be skipped. This is in addition to any alpha or transparency value associated with the pixel. If you want all pixels to be combined, pass 0xff (255) in mask. */ void /* PRIVATE */ png_combine_row(png_structp png_ptr, png_bytep row, int mask) { png_debug(1, "in png_combine_row"); if (mask == 0xff) { png_memcpy(row, png_ptr->row_buf + 1, PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width)); } else { switch (png_ptr->row_info.pixel_depth) { case 1: { png_bytep sp = png_ptr->row_buf + 1; png_bytep dp = row; int s_inc, s_start, s_end; int m = 0x80; int shift; png_uint_32 i; png_uint_32 row_width = png_ptr->width; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (png_ptr->transformations & PNG_PACKSWAP) { s_start = 0; s_end = 7; s_inc = 1; } else #endif { s_start = 7; s_end = 0; s_inc = -1; } shift = s_start; for (i = 0; i < row_width; i++) { if (m & mask) { int value; value = (*sp >> shift) & 0x01; *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); *dp |= (png_byte)(value << shift); } if (shift == s_end) { shift = s_start; sp++; dp++; } else shift += s_inc; if (m == 1) m = 0x80; else m >>= 1; } break; } case 2: { png_bytep sp = png_ptr->row_buf + 1; png_bytep dp = row; int s_start, s_end, s_inc; int m = 0x80; int shift; png_uint_32 i; png_uint_32 row_width = png_ptr->width; int value; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (png_ptr->transformations & PNG_PACKSWAP) { s_start = 0; s_end = 6; s_inc = 2; } else #endif { s_start = 6; s_end = 0; s_inc = -2; } shift = s_start; for (i = 0; i < row_width; i++) { if (m & mask) { value = (*sp >> shift) & 0x03; *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); *dp |= (png_byte)(value << shift); } if (shift == s_end) { shift = s_start; sp++; dp++; } else shift += s_inc; if (m == 1) m = 0x80; else m >>= 1; } break; } case 4: { png_bytep sp = png_ptr->row_buf + 1; png_bytep dp = row; int s_start, s_end, s_inc; int m = 0x80; int shift; png_uint_32 i; png_uint_32 row_width = png_ptr->width; int value; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (png_ptr->transformations & PNG_PACKSWAP) { s_start = 0; s_end = 4; s_inc = 4; } else #endif { s_start = 4; s_end = 0; s_inc = -4; } shift = s_start; for (i = 0; i < row_width; i++) { if (m & mask) { value = (*sp >> shift) & 0xf; *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); *dp |= (png_byte)(value << shift); } if (shift == s_end) { shift = s_start; sp++; dp++; } else shift += s_inc; if (m == 1) m = 0x80; else m >>= 1; } break; } default: { png_bytep sp = png_ptr->row_buf + 1; png_bytep dp = row; png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); png_uint_32 i; png_uint_32 row_width = png_ptr->width; png_byte m = 0x80; for (i = 0; i < row_width; i++) { if (m & mask) { png_memcpy(dp, sp, pixel_bytes); } sp += pixel_bytes; dp += pixel_bytes; if (m == 1) m = 0x80; else m >>= 1; } break; } } } } #ifdef PNG_READ_INTERLACING_SUPPORTED /* OLD pre-1.0.9 interface: void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, png_uint_32 transformations) */ void /* PRIVATE */ png_do_read_interlace(png_structp png_ptr) { png_row_infop row_info = &(png_ptr->row_info); png_bytep row = png_ptr->row_buf + 1; int pass = png_ptr->pass; png_uint_32 transformations = png_ptr->transformations; /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Offset to next interlace block */ PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; png_debug(1, "in png_do_read_interlace"); if (row != NULL && row_info != NULL) { png_uint_32 final_width; final_width = row_info->width * png_pass_inc[pass]; switch (row_info->pixel_depth) { case 1: { png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); int sshift, dshift; int s_start, s_end, s_inc; int jstop = png_pass_inc[pass]; png_byte v; png_uint_32 i; int j; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (transformations & PNG_PACKSWAP) { sshift = (int)((row_info->width + 7) & 0x07); dshift = (int)((final_width + 7) & 0x07); s_start = 7; s_end = 0; s_inc = -1; } else #endif { sshift = 7 - (int)((row_info->width + 7) & 0x07); dshift = 7 - (int)((final_width + 7) & 0x07); s_start = 0; s_end = 7; s_inc = 1; } for (i = 0; i < row_info->width; i++) { v = (png_byte)((*sp >> sshift) & 0x01); for (j = 0; j < jstop; j++) { *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); *dp |= (png_byte)(v << dshift); if (dshift == s_end) { dshift = s_start; dp--; } else dshift += s_inc; } if (sshift == s_end) { sshift = s_start; sp--; } else sshift += s_inc; } break; } case 2: { png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); int sshift, dshift; int s_start, s_end, s_inc; int jstop = png_pass_inc[pass]; png_uint_32 i; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (transformations & PNG_PACKSWAP) { sshift = (int)(((row_info->width + 3) & 0x03) << 1); dshift = (int)(((final_width + 3) & 0x03) << 1); s_start = 6; s_end = 0; s_inc = -2; } else #endif { sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); s_start = 0; s_end = 6; s_inc = 2; } for (i = 0; i < row_info->width; i++) { png_byte v; int j; v = (png_byte)((*sp >> sshift) & 0x03); for (j = 0; j < jstop; j++) { *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); *dp |= (png_byte)(v << dshift); if (dshift == s_end) { dshift = s_start; dp--; } else dshift += s_inc; } if (sshift == s_end) { sshift = s_start; sp--; } else sshift += s_inc; } break; } case 4: { png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); int sshift, dshift; int s_start, s_end, s_inc; png_uint_32 i; int jstop = png_pass_inc[pass]; #ifdef PNG_READ_PACKSWAP_SUPPORTED if (transformations & PNG_PACKSWAP) { sshift = (int)(((row_info->width + 1) & 0x01) << 2); dshift = (int)(((final_width + 1) & 0x01) << 2); s_start = 4; s_end = 0; s_inc = -4; } else #endif { sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); s_start = 0; s_end = 4; s_inc = 4; } for (i = 0; i < row_info->width; i++) { png_byte v = (png_byte)((*sp >> sshift) & 0xf); int j; for (j = 0; j < jstop; j++) { *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); *dp |= (png_byte)(v << dshift); if (dshift == s_end) { dshift = s_start; dp--; } else dshift += s_inc; } if (sshift == s_end) { sshift = s_start; sp--; } else sshift += s_inc; } break; } default: { png_size_t pixel_bytes = (row_info->pixel_depth >> 3); png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; int jstop = png_pass_inc[pass]; png_uint_32 i; for (i = 0; i < row_info->width; i++) { png_byte v[8]; int j; png_memcpy(v, sp, pixel_bytes); for (j = 0; j < jstop; j++) { png_memcpy(dp, v, pixel_bytes); dp -= pixel_bytes; } sp -= pixel_bytes; } break; } } row_info->width = final_width; row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); } #ifndef PNG_READ_PACKSWAP_SUPPORTED transformations = transformations; /* Silence compiler warning */ #endif } #endif /* PNG_READ_INTERLACING_SUPPORTED */ void /* PRIVATE */ png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter) { png_debug(1, "in png_read_filter_row"); png_debug2(2, "row = %lu, filter = %d", png_ptr->row_number, filter); switch (filter) { case PNG_FILTER_VALUE_NONE: break; case PNG_FILTER_VALUE_SUB: { png_uint_32 i; png_uint_32 istop = row_info->rowbytes; png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; png_bytep rp = row + bpp; png_bytep lp = row; for (i = bpp; i < istop; i++) { *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); rp++; } break; } case PNG_FILTER_VALUE_UP: { png_uint_32 i; png_uint_32 istop = row_info->rowbytes; png_bytep rp = row; png_bytep pp = prev_row; for (i = 0; i < istop; i++) { *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); rp++; } break; } case PNG_FILTER_VALUE_AVG: { png_uint_32 i; png_bytep rp = row; png_bytep pp = prev_row; png_bytep lp = row; png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; png_uint_32 istop = row_info->rowbytes - bpp; for (i = 0; i < bpp; i++) { *rp = (png_byte)(((int)(*rp) + ((int)(*pp++) / 2 )) & 0xff); rp++; } for (i = 0; i < istop; i++) { *rp = (png_byte)(((int)(*rp) + (int)(*pp++ + *lp++) / 2 ) & 0xff); rp++; } break; } case PNG_FILTER_VALUE_PAETH: { png_uint_32 i; png_bytep rp = row; png_bytep pp = prev_row; png_bytep lp = row; png_bytep cp = prev_row; png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; png_uint_32 istop=row_info->rowbytes - bpp; for (i = 0; i < bpp; i++) { *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); rp++; } for (i = 0; i < istop; i++) /* Use leftover rp,pp */ { int a, b, c, pa, pb, pc, p; a = *lp++; b = *pp++; c = *cp++; p = b - c; pc = a - c; #ifdef PNG_USE_ABS pa = abs(p); pb = abs(pc); pc = abs(p + pc); #else pa = p < 0 ? -p : p; pb = pc < 0 ? -pc : pc; pc = (p + pc) < 0 ? -(p + pc) : p + pc; #endif /* if (pa <= pb && pa <= pc) p = a; else if (pb <= pc) p = b; else p = c; */ p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; *rp = (png_byte)(((int)(*rp) + p) & 0xff); rp++; } break; } default: png_warning(png_ptr, "Ignoring bad adaptive filter type"); *row = 0; break; } } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED void /* PRIVATE */ png_read_finish_row(png_structp png_ptr) { #ifdef PNG_READ_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; #endif /* PNG_READ_INTERLACING_SUPPORTED */ png_debug(1, "in png_read_finish_row"); png_ptr->row_number++; if (png_ptr->row_number < png_ptr->num_rows) return; #ifdef PNG_READ_INTERLACING_SUPPORTED if (png_ptr->interlaced) { png_ptr->row_number = 0; png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); do { png_ptr->pass++; if (png_ptr->pass >= 7) break; png_ptr->iwidth = (png_ptr->width + png_pass_inc[png_ptr->pass] - 1 - png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; if (!(png_ptr->transformations & PNG_INTERLACE)) { png_ptr->num_rows = (png_ptr->height + png_pass_yinc[png_ptr->pass] - 1 - png_pass_ystart[png_ptr->pass]) / png_pass_yinc[png_ptr->pass]; if (!(png_ptr->num_rows)) continue; } else /* if (png_ptr->transformations & PNG_INTERLACE) */ break; } while (png_ptr->iwidth == 0); if (png_ptr->pass < 7) return; } #endif /* PNG_READ_INTERLACING_SUPPORTED */ if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IDAT; #endif char extra; int ret; png_ptr->zstream.next_out = (Byte *)&extra; png_ptr->zstream.avail_out = (uInt)1; for (;;) { if (!(png_ptr->zstream.avail_in)) { while (!png_ptr->idat_size) { png_byte chunk_length[4]; png_crc_finish(png_ptr, 0); png_read_data(png_ptr, chunk_length, 4); png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length); png_reset_crc(png_ptr); png_crc_read(png_ptr, png_ptr->chunk_name, 4); if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) png_error(png_ptr, "Not enough image data"); } png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_in = png_ptr->zbuf; if (png_ptr->zbuf_size > png_ptr->idat_size) png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); png_ptr->idat_size -= png_ptr->zstream.avail_in; } ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret == Z_STREAM_END) { if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || png_ptr->idat_size) png_warning(png_ptr, "Extra compressed data."); png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } if (ret != Z_OK) png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression Error"); if (!(png_ptr->zstream.avail_out)) { png_warning(png_ptr, "Extra compressed data."); png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } } png_ptr->zstream.avail_out = 0; } if (png_ptr->idat_size || png_ptr->zstream.avail_in) png_warning(png_ptr, "Extra compression data."); inflateReset(&png_ptr->zstream); png_ptr->mode |= PNG_AFTER_IDAT; } #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ void /* PRIVATE */ png_read_start_row(png_structp png_ptr) { #ifdef PNG_READ_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; #endif int max_pixel_depth; png_size_t row_bytes; png_debug(1, "in png_read_start_row"); png_ptr->zstream.avail_in = 0; png_init_read_transformations(png_ptr); #ifdef PNG_READ_INTERLACING_SUPPORTED if (png_ptr->interlaced) { if (!(png_ptr->transformations & PNG_INTERLACE)) png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - png_pass_ystart[0]) / png_pass_yinc[0]; else png_ptr->num_rows = png_ptr->height; png_ptr->iwidth = (png_ptr->width + png_pass_inc[png_ptr->pass] - 1 - png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; } else #endif /* PNG_READ_INTERLACING_SUPPORTED */ { png_ptr->num_rows = png_ptr->height; png_ptr->iwidth = png_ptr->width; } max_pixel_depth = png_ptr->pixel_depth; #ifdef PNG_READ_PACK_SUPPORTED if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) max_pixel_depth = 8; #endif #ifdef PNG_READ_EXPAND_SUPPORTED if (png_ptr->transformations & PNG_EXPAND) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { if (png_ptr->num_trans) max_pixel_depth = 32; else max_pixel_depth = 24; } else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) { if (max_pixel_depth < 8) max_pixel_depth = 8; if (png_ptr->num_trans) max_pixel_depth *= 2; } else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) { if (png_ptr->num_trans) { max_pixel_depth *= 4; max_pixel_depth /= 3; } } } #endif #ifdef PNG_READ_FILLER_SUPPORTED if (png_ptr->transformations & (PNG_FILLER)) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) max_pixel_depth = 32; else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) { if (max_pixel_depth <= 8) max_pixel_depth = 16; else max_pixel_depth = 32; } else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) { if (max_pixel_depth <= 32) max_pixel_depth = 32; else max_pixel_depth = 64; } } #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED if (png_ptr->transformations & PNG_GRAY_TO_RGB) { if ( #ifdef PNG_READ_EXPAND_SUPPORTED (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || #endif #ifdef PNG_READ_FILLER_SUPPORTED (png_ptr->transformations & (PNG_FILLER)) || #endif png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { if (max_pixel_depth <= 16) max_pixel_depth = 32; else max_pixel_depth = 64; } else { if (max_pixel_depth <= 8) { if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) max_pixel_depth = 32; else max_pixel_depth = 24; } else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) max_pixel_depth = 64; else max_pixel_depth = 48; } } #endif #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) if (png_ptr->transformations & PNG_USER_TRANSFORM) { int user_pixel_depth = png_ptr->user_transform_depth* png_ptr->user_transform_channels; if (user_pixel_depth > max_pixel_depth) max_pixel_depth=user_pixel_depth; } #endif /* Align the width on the next larger 8 pixels. Mainly used * for interlacing */ row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); /* Calculate the maximum bytes needed, adding a byte and a pixel * for safety's sake */ row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + 1 + ((max_pixel_depth + 7) >> 3); #ifdef PNG_MAX_MALLOC_64K if (row_bytes > (png_uint_32)65536L) png_error(png_ptr, "This image requires a row greater than 64KB"); #endif if (row_bytes + 64 > png_ptr->old_big_row_buf_size) { png_free(png_ptr, png_ptr->big_row_buf); if (png_ptr->interlaced) png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, row_bytes + 64); else png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 64); png_ptr->old_big_row_buf_size = row_bytes + 64; /* Use 32 bytes of padding before and after row_buf. */ png_ptr->row_buf = png_ptr->big_row_buf + 32; png_ptr->old_big_row_buf_size = row_bytes + 64; } #ifdef PNG_MAX_MALLOC_64K if ((png_uint_32)row_bytes + 1 > (png_uint_32)65536L) png_error(png_ptr, "This image requires a row greater than 64KB"); #endif if ((png_uint_32)row_bytes > (png_uint_32)(PNG_SIZE_MAX - 1)) png_error(png_ptr, "Row has too many bytes to allocate in memory."); if (row_bytes + 1 > png_ptr->old_prev_row_size) { png_free(png_ptr, png_ptr->prev_row); png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( row_bytes + 1)); png_memset_check(png_ptr, png_ptr->prev_row, 0, row_bytes + 1); png_ptr->old_prev_row_size = row_bytes + 1; } png_ptr->rowbytes = row_bytes; png_debug1(3, "width = %lu,", png_ptr->width); png_debug1(3, "height = %lu,", png_ptr->height); png_debug1(3, "iwidth = %lu,", png_ptr->iwidth); png_debug1(3, "num_rows = %lu,", png_ptr->num_rows); png_debug1(3, "rowbytes = %lu,", png_ptr->rowbytes); png_debug1(3, "irowbytes = %lu", PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); png_ptr->flags |= PNG_FLAG_ROW_INIT; } #endif /* PNG_READ_SUPPORTED */ glmark2-2012.08/./src/libpng/INSTALL0000664000175000017500000001206312013417376016013 0ustar alfalf00000000000000 Installing libpng version 1.2.46 - July 9, 2011 On Unix/Linux and similar systems, you can simply type ./configure [--prefix=/path] make check make install and ignore the rest of this document. If configure does not work on your system and you have a reasonably up-to-date set of tools, running ./autogen.sh before running ./configure may fix the problem. You can also run the individual commands in autogen.sh with the --force option, if supported by your version of the tools. If you run 'libtoolize --force', though, this will replace the distributed, patched, version of ltmain.sh with an unpatched version and your shared library builds may fail to produce libraries with the correct version numbers. Instead, you can use one of the custom-built makefiles in the "scripts" directory cp scripts/makefile.system makefile make test make install The files that are presently available in the scripts directory are listed and described in scripts/README.txt. Or you can use one of the "projects" in the "projects" directory. Before installing libpng, you must first install zlib, if it is not already on your system. zlib can usually be found wherever you got libpng. zlib can be placed in another directory, at the same level as libpng. If you want to use "cmake" (see www.cmake.org), type cmake . -DCMAKE_INSTALL_PREFIX=/path make make install If your system already has a preinstalled zlib you will still need to have access to the zlib.h and zconf.h include files that correspond to the version of zlib that's installed. You can rename the directories that you downloaded (they might be called "libpng-1.2.46" or "libpng12" and "zlib-1.2.3" or "zlib123") so that you have directories called "zlib" and "libpng". Your directory structure should look like this: .. (the parent directory) libpng (this directory) INSTALL (this file) README *.h *.c CMakeLists.txt => "cmake" script configuration files: configure.ac, configure, Makefile.am, Makefile.in, autogen.sh, config.guess, ltmain.sh, missing, aclocal.m4, config.h.in, config.sub, depcomp, install-sh, mkinstalldirs, test-pngtest.sh contrib gregbook pngminim pngminus pngsuite visupng projects cbuilder5 (Borland) visualc6 (msvc) visualc71 xcode scripts makefile.* *.def (module definition files) pngtest.png etc. zlib README *.h *.c contrib etc. If the line endings in the files look funny, you may wish to get the other distribution of libpng. It is available in both tar.gz (UNIX style line endings) and zip (DOS style line endings) formats. If you are building libpng with MSVC, you can enter the libpng projects\visualc6 or visualc71 directory and follow the instructions in README.txt. Otherwise enter the zlib directory and follow the instructions in zlib/README, then come back here and run "configure" or choose the appropriate makefile.sys in the scripts directory. Copy the file (or files) that you need from the scripts directory into this directory, for example MSDOS example: copy scripts\makefile.msc makefile UNIX example: cp scripts/makefile.std makefile Read the makefile to see if you need to change any source or target directories to match your preferences. Then read pngconf.h to see if you want to make any configuration changes. Then just run "make" which will create the libpng library in this directory and "make test" which will run a quick test that reads the "pngtest.png" file and writes a "pngout.png" file that should be identical to it. Look for "9782 zero samples" in the output of the test. For more confidence, you can run another test by typing "pngtest pngnow.png" and looking for "289 zero samples" in the output. Also, you can run "pngtest -m contrib/pngsuite/*.png" and compare your output with the result shown in contrib/pngsuite/README. Most of the makefiles will allow you to run "make install" to put the library in its final resting place (if you want to do that, run "make install" in the zlib directory first if necessary). Some also allow you to run "make test-installed" after you have run "make install". If you encounter a compiler error message complaining about the lines __png.h__ already includes setjmp.h; __dont__ include it again.; this means you have compiled another module that includes setjmp.h, which is hazardous because the two modules might not include exactly the same setjmp.h. If you are sure that you know what you are doing and that they are exactly the same, then you can comment out or delete the two lines. Better yet, use the cexcept interface instead, as demonstrated in contrib/visupng of the libpng distribution. Further information can be found in the README and libpng.txt files, in the individual makefiles, in png.h, and the manual pages libpng.3 and png.5. glmark2-2012.08/./src/libpng/pngset.c0000664000175000017500000010666112013417376016436 0ustar alfalf00000000000000 /* pngset.c - storage of image information into info struct * * Last changed in libpng 1.2.43 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * The functions here are used during reads to store data from the file * into the info struct, and during writes to store application data * into the info struct for writing into the file. This abstracts the * info struct and allows us to change the structure in the future. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #ifdef PNG_bKGD_SUPPORTED void PNGAPI png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background) { png_debug1(1, "in %s storage function", "bKGD"); if (png_ptr == NULL || info_ptr == NULL) return; png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16)); info_ptr->valid |= PNG_INFO_bKGD; } #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI png_set_cHRM(png_structp png_ptr, png_infop info_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y) { png_debug1(1, "in %s storage function", "cHRM"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->x_white = (float)white_x; info_ptr->y_white = (float)white_y; info_ptr->x_red = (float)red_x; info_ptr->y_red = (float)red_y; info_ptr->x_green = (float)green_x; info_ptr->y_green = (float)green_y; info_ptr->x_blue = (float)blue_x; info_ptr->y_blue = (float)blue_y; #ifdef PNG_FIXED_POINT_SUPPORTED info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5); info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5); info_ptr->int_x_red = (png_fixed_point)( red_x*100000.+0.5); info_ptr->int_y_red = (png_fixed_point)( red_y*100000.+0.5); info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5); info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5); info_ptr->int_x_blue = (png_fixed_point)( blue_x*100000.+0.5); info_ptr->int_y_blue = (png_fixed_point)( blue_y*100000.+0.5); #endif info_ptr->valid |= PNG_INFO_cHRM; } #endif /* PNG_FLOATING_POINT_SUPPORTED */ #ifdef PNG_FIXED_POINT_SUPPORTED void PNGAPI png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y) { png_debug1(1, "in %s storage function", "cHRM fixed"); if (png_ptr == NULL || info_ptr == NULL) return; #ifdef PNG_CHECK_cHRM_SUPPORTED if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y)) #endif { info_ptr->int_x_white = white_x; info_ptr->int_y_white = white_y; info_ptr->int_x_red = red_x; info_ptr->int_y_red = red_y; info_ptr->int_x_green = green_x; info_ptr->int_y_green = green_y; info_ptr->int_x_blue = blue_x; info_ptr->int_y_blue = blue_y; #ifdef PNG_FLOATING_POINT_SUPPORTED info_ptr->x_white = (float)(white_x/100000.); info_ptr->y_white = (float)(white_y/100000.); info_ptr->x_red = (float)( red_x/100000.); info_ptr->y_red = (float)( red_y/100000.); info_ptr->x_green = (float)(green_x/100000.); info_ptr->y_green = (float)(green_y/100000.); info_ptr->x_blue = (float)( blue_x/100000.); info_ptr->y_blue = (float)( blue_y/100000.); #endif info_ptr->valid |= PNG_INFO_cHRM; } } #endif /* PNG_FIXED_POINT_SUPPORTED */ #endif /* PNG_cHRM_SUPPORTED */ #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma) { double png_gamma; png_debug1(1, "in %s storage function", "gAMA"); if (png_ptr == NULL || info_ptr == NULL) return; /* Check for overflow */ if (file_gamma > 21474.83) { png_warning(png_ptr, "Limiting gamma to 21474.83"); png_gamma=21474.83; } else png_gamma = file_gamma; info_ptr->gamma = (float)png_gamma; #ifdef PNG_FIXED_POINT_SUPPORTED info_ptr->int_gamma = (int)(png_gamma*100000.+.5); #endif info_ptr->valid |= PNG_INFO_gAMA; if (png_gamma == 0.0) png_warning(png_ptr, "Setting gamma=0"); } #endif void PNGAPI png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point int_gamma) { png_fixed_point png_gamma; png_debug1(1, "in %s storage function", "gAMA"); if (png_ptr == NULL || info_ptr == NULL) return; if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX) { png_warning(png_ptr, "Limiting gamma to 21474.83"); png_gamma=PNG_UINT_31_MAX; } else { if (int_gamma < 0) { png_warning(png_ptr, "Setting negative gamma to zero"); png_gamma = 0; } else png_gamma = int_gamma; } #ifdef PNG_FLOATING_POINT_SUPPORTED info_ptr->gamma = (float)(png_gamma/100000.); #endif #ifdef PNG_FIXED_POINT_SUPPORTED info_ptr->int_gamma = png_gamma; #endif info_ptr->valid |= PNG_INFO_gAMA; if (png_gamma == 0) png_warning(png_ptr, "Setting gamma=0"); } #endif #ifdef PNG_hIST_SUPPORTED void PNGAPI png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist) { int i; png_debug1(1, "in %s storage function", "hIST"); if (png_ptr == NULL || info_ptr == NULL) return; if (info_ptr->num_palette == 0 || info_ptr->num_palette > PNG_MAX_PALETTE_LENGTH) { png_warning(png_ptr, "Invalid palette size, hIST allocation skipped."); return; } #ifdef PNG_FREE_ME_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0); #endif /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in * version 1.2.1 */ png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr, (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16))); if (png_ptr->hist == NULL) { png_warning(png_ptr, "Insufficient memory for hIST chunk data."); return; } for (i = 0; i < info_ptr->num_palette; i++) png_ptr->hist[i] = hist[i]; info_ptr->hist = png_ptr->hist; info_ptr->valid |= PNG_INFO_hIST; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_HIST; #else png_ptr->flags |= PNG_FLAG_FREE_HIST; #endif } #endif void PNGAPI png_set_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type) { png_debug1(1, "in %s storage function", "IHDR"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->width = width; info_ptr->height = height; info_ptr->bit_depth = (png_byte)bit_depth; info_ptr->color_type = (png_byte)color_type; info_ptr->compression_type = (png_byte)compression_type; info_ptr->filter_type = (png_byte)filter_type; info_ptr->interlace_type = (png_byte)interlace_type; png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type, info_ptr->compression_type, info_ptr->filter_type); if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) info_ptr->channels = 1; else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) info_ptr->channels = 3; else info_ptr->channels = 1; if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) info_ptr->channels++; info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); /* Check for potential overflow */ if (width > (PNG_UINT_32_MAX >> 3) /* 8-byte RGBA pixels */ - 64 /* bigrowbuf hack */ - 1 /* filter byte */ - 7*8 /* rounding of width to multiple of 8 pixels */ - 8) /* extra max_pixel_depth pad */ info_ptr->rowbytes = (png_size_t)0; else info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width); } #ifdef PNG_oFFs_SUPPORTED void PNGAPI png_set_oFFs(png_structp png_ptr, png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y, int unit_type) { png_debug1(1, "in %s storage function", "oFFs"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->x_offset = offset_x; info_ptr->y_offset = offset_y; info_ptr->offset_unit_type = (png_byte)unit_type; info_ptr->valid |= PNG_INFO_oFFs; } #endif #ifdef PNG_pCAL_SUPPORTED void PNGAPI png_set_pCAL(png_structp png_ptr, png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) { png_uint_32 length; int i; png_debug1(1, "in %s storage function", "pCAL"); if (png_ptr == NULL || info_ptr == NULL) return; length = png_strlen(purpose) + 1; png_debug1(3, "allocating purpose for info (%lu bytes)", (unsigned long)length); info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length); if (info_ptr->pcal_purpose == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL purpose."); return; } png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length); png_debug(3, "storing X0, X1, type, and nparams in info"); info_ptr->pcal_X0 = X0; info_ptr->pcal_X1 = X1; info_ptr->pcal_type = (png_byte)type; info_ptr->pcal_nparams = (png_byte)nparams; length = png_strlen(units) + 1; png_debug1(3, "allocating units for info (%lu bytes)", (unsigned long)length); info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length); if (info_ptr->pcal_units == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL units."); return; } png_memcpy(info_ptr->pcal_units, units, (png_size_t)length); info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)((nparams + 1) * png_sizeof(png_charp))); if (info_ptr->pcal_params == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL params."); return; } png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp)); for (i = 0; i < nparams; i++) { length = png_strlen(params[i]) + 1; png_debug2(3, "allocating parameter %d for info (%lu bytes)", i, (unsigned long)length); info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length); if (info_ptr->pcal_params[i] == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL parameter."); return; } png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length); } info_ptr->valid |= PNG_INFO_pCAL; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_PCAL; #endif } #endif #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED) #ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width, double height) { png_debug1(1, "in %s storage function", "sCAL"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->scal_unit = (png_byte)unit; info_ptr->scal_pixel_width = width; info_ptr->scal_pixel_height = height; info_ptr->valid |= PNG_INFO_sCAL; } #else #ifdef PNG_FIXED_POINT_SUPPORTED void PNGAPI png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, int unit, png_charp swidth, png_charp sheight) { png_uint_32 length; png_debug1(1, "in %s storage function", "sCAL"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->scal_unit = (png_byte)unit; length = png_strlen(swidth) + 1; png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)length); info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length); if (info_ptr->scal_s_width == NULL) { png_warning(png_ptr, "Memory allocation failed while processing sCAL."); return; } png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length); length = png_strlen(sheight) + 1; png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)length); info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length); if (info_ptr->scal_s_height == NULL) { png_free (png_ptr, info_ptr->scal_s_width); info_ptr->scal_s_width = NULL; png_warning(png_ptr, "Memory allocation failed while processing sCAL."); return; } png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length); info_ptr->valid |= PNG_INFO_sCAL; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_SCAL; #endif } #endif #endif #endif #ifdef PNG_pHYs_SUPPORTED void PNGAPI png_set_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type) { png_debug1(1, "in %s storage function", "pHYs"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->x_pixels_per_unit = res_x; info_ptr->y_pixels_per_unit = res_y; info_ptr->phys_unit_type = (png_byte)unit_type; info_ptr->valid |= PNG_INFO_pHYs; } #endif void PNGAPI png_set_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp palette, int num_palette) { png_debug1(1, "in %s storage function", "PLTE"); if (png_ptr == NULL || info_ptr == NULL) return; if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH) { if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) png_error(png_ptr, "Invalid palette length"); else { png_warning(png_ptr, "Invalid palette length"); return; } } /* It may not actually be necessary to set png_ptr->palette here; * we do it for backward compatibility with the way the png_handle_tRNS * function used to do the allocation. */ #ifdef PNG_FREE_ME_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); #endif /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead * of num_palette entries, in case of an invalid PNG file that has * too-large sample values. */ png_ptr->palette = (png_colorp)png_calloc(png_ptr, PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color)); png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color)); info_ptr->palette = png_ptr->palette; info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_PLTE; #else png_ptr->flags |= PNG_FLAG_FREE_PLTE; #endif info_ptr->valid |= PNG_INFO_PLTE; } #ifdef PNG_sBIT_SUPPORTED void PNGAPI png_set_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p sig_bit) { png_debug1(1, "in %s storage function", "sBIT"); if (png_ptr == NULL || info_ptr == NULL) return; png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8)); info_ptr->valid |= PNG_INFO_sBIT; } #endif #ifdef PNG_sRGB_SUPPORTED void PNGAPI png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent) { png_debug1(1, "in %s storage function", "sRGB"); if (png_ptr == NULL || info_ptr == NULL) return; info_ptr->srgb_intent = (png_byte)intent; info_ptr->valid |= PNG_INFO_sRGB; } void PNGAPI png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr, int intent) { #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED float file_gamma; #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_fixed_point int_file_gamma; #endif #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; #endif png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y; #endif png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM"); if (png_ptr == NULL || info_ptr == NULL) return; png_set_sRGB(png_ptr, info_ptr, intent); #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED file_gamma = (float).45455; png_set_gAMA(png_ptr, info_ptr, file_gamma); #endif #ifdef PNG_FIXED_POINT_SUPPORTED int_file_gamma = 45455L; png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma); #endif #endif #ifdef PNG_cHRM_SUPPORTED int_white_x = 31270L; int_white_y = 32900L; int_red_x = 64000L; int_red_y = 33000L; int_green_x = 30000L; int_green_y = 60000L; int_blue_x = 15000L; int_blue_y = 6000L; #ifdef PNG_FLOATING_POINT_SUPPORTED white_x = (float).3127; white_y = (float).3290; red_x = (float).64; red_y = (float).33; green_x = (float).30; green_y = (float).60; blue_x = (float).15; blue_y = (float).06; #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_set_cHRM_fixed(png_ptr, info_ptr, int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y); #endif #ifdef PNG_FLOATING_POINT_SUPPORTED png_set_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); #endif #endif /* cHRM */ } #endif /* sRGB */ #ifdef PNG_iCCP_SUPPORTED void PNGAPI png_set_iCCP(png_structp png_ptr, png_infop info_ptr, png_charp name, int compression_type, png_charp profile, png_uint_32 proflen) { png_charp new_iccp_name; png_charp new_iccp_profile; png_uint_32 length; png_debug1(1, "in %s storage function", "iCCP"); if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL) return; length = png_strlen(name)+1; new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length); if (new_iccp_name == NULL) { png_warning(png_ptr, "Insufficient memory to process iCCP chunk."); return; } png_memcpy(new_iccp_name, name, length); new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen); if (new_iccp_profile == NULL) { png_free (png_ptr, new_iccp_name); png_warning(png_ptr, "Insufficient memory to process iCCP profile."); return; } png_memcpy(new_iccp_profile, profile, (png_size_t)proflen); png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0); info_ptr->iccp_proflen = proflen; info_ptr->iccp_name = new_iccp_name; info_ptr->iccp_profile = new_iccp_profile; /* Compression is always zero but is here so the API and info structure * does not have to change if we introduce multiple compression types */ info_ptr->iccp_compression = (png_byte)compression_type; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_ICCP; #endif info_ptr->valid |= PNG_INFO_iCCP; } #endif #ifdef PNG_TEXT_SUPPORTED void PNGAPI png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, int num_text) { int ret; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text); if (ret) png_error(png_ptr, "Insufficient memory to store text"); } int /* PRIVATE */ png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, int num_text) { int i; png_debug1(1, "in %s storage function", ((png_ptr == NULL || png_ptr->chunk_name[0] == '\0') ? "text" : (png_const_charp)png_ptr->chunk_name)); if (png_ptr == NULL || info_ptr == NULL || num_text == 0) return(0); /* Make sure we have enough space in the "text" array in info_struct * to hold all of the incoming text_ptr objects. */ if (info_ptr->num_text + num_text > info_ptr->max_text) { if (info_ptr->text != NULL) { png_textp old_text; int old_max; old_max = info_ptr->max_text; info_ptr->max_text = info_ptr->num_text + num_text + 8; old_text = info_ptr->text; info_ptr->text = (png_textp)png_malloc_warn(png_ptr, (png_uint_32)(info_ptr->max_text * png_sizeof(png_text))); if (info_ptr->text == NULL) { png_free(png_ptr, old_text); return(1); } png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max * png_sizeof(png_text))); png_free(png_ptr, old_text); } else { info_ptr->max_text = num_text + 8; info_ptr->num_text = 0; info_ptr->text = (png_textp)png_malloc_warn(png_ptr, (png_uint_32)(info_ptr->max_text * png_sizeof(png_text))); if (info_ptr->text == NULL) return(1); #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_TEXT; #endif } png_debug1(3, "allocated %d entries for info_ptr->text", info_ptr->max_text); } for (i = 0; i < num_text; i++) { png_size_t text_length, key_len; png_size_t lang_len, lang_key_len; png_textp textp = &(info_ptr->text[info_ptr->num_text]); if (text_ptr[i].key == NULL) continue; key_len = png_strlen(text_ptr[i].key); if (text_ptr[i].compression <= 0) { lang_len = 0; lang_key_len = 0; } else #ifdef PNG_iTXt_SUPPORTED { /* Set iTXt data */ if (text_ptr[i].lang != NULL) lang_len = png_strlen(text_ptr[i].lang); else lang_len = 0; if (text_ptr[i].lang_key != NULL) lang_key_len = png_strlen(text_ptr[i].lang_key); else lang_key_len = 0; } #else /* PNG_iTXt_SUPPORTED */ { png_warning(png_ptr, "iTXt chunk not supported."); continue; } #endif if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0') { text_length = 0; #ifdef PNG_iTXt_SUPPORTED if (text_ptr[i].compression > 0) textp->compression = PNG_ITXT_COMPRESSION_NONE; else #endif textp->compression = PNG_TEXT_COMPRESSION_NONE; } else { text_length = png_strlen(text_ptr[i].text); textp->compression = text_ptr[i].compression; } textp->key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32) (key_len + text_length + lang_len + lang_key_len + 4)); if (textp->key == NULL) return(1); png_debug2(2, "Allocated %lu bytes at %x in png_set_text", (png_uint_32) (key_len + lang_len + lang_key_len + text_length + 4), (int)textp->key); png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len)); *(textp->key + key_len) = '\0'; #ifdef PNG_iTXt_SUPPORTED if (text_ptr[i].compression > 0) { textp->lang = textp->key + key_len + 1; png_memcpy(textp->lang, text_ptr[i].lang, lang_len); *(textp->lang + lang_len) = '\0'; textp->lang_key = textp->lang + lang_len + 1; png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len); *(textp->lang_key + lang_key_len) = '\0'; textp->text = textp->lang_key + lang_key_len + 1; } else #endif { #ifdef PNG_iTXt_SUPPORTED textp->lang=NULL; textp->lang_key=NULL; #endif textp->text = textp->key + key_len + 1; } if (text_length) png_memcpy(textp->text, text_ptr[i].text, (png_size_t)(text_length)); *(textp->text + text_length) = '\0'; #ifdef PNG_iTXt_SUPPORTED if (textp->compression > 0) { textp->text_length = 0; textp->itxt_length = text_length; } else #endif { textp->text_length = text_length; #ifdef PNG_iTXt_SUPPORTED textp->itxt_length = 0; #endif } info_ptr->num_text++; png_debug1(3, "transferred text chunk %d", info_ptr->num_text); } return(0); } #endif #ifdef PNG_tIME_SUPPORTED void PNGAPI png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time) { png_debug1(1, "in %s storage function", "tIME"); if (png_ptr == NULL || info_ptr == NULL || (png_ptr->mode & PNG_WROTE_tIME)) return; png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time)); info_ptr->valid |= PNG_INFO_tIME; } #endif #ifdef PNG_tRNS_SUPPORTED void PNGAPI png_set_tRNS(png_structp png_ptr, png_infop info_ptr, png_bytep trans, int num_trans, png_color_16p trans_values) { png_debug1(1, "in %s storage function", "tRNS"); if (png_ptr == NULL || info_ptr == NULL) return; if (trans != NULL) { /* It may not actually be necessary to set png_ptr->trans here; * we do it for backward compatibility with the way the png_handle_tRNS * function used to do the allocation. */ #ifdef PNG_FREE_ME_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); #endif /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */ png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr, (png_uint_32)PNG_MAX_PALETTE_LENGTH); if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans); } if (trans_values != NULL) { int sample_max = (1 << info_ptr->bit_depth); if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && (int)trans_values->gray > sample_max) || (info_ptr->color_type == PNG_COLOR_TYPE_RGB && ((int)trans_values->red > sample_max || (int)trans_values->green > sample_max || (int)trans_values->blue > sample_max))) png_warning(png_ptr, "tRNS chunk has out-of-range samples for bit_depth"); png_memcpy(&(info_ptr->trans_values), trans_values, png_sizeof(png_color_16)); if (num_trans == 0) num_trans = 1; } info_ptr->num_trans = (png_uint_16)num_trans; if (num_trans != 0) { info_ptr->valid |= PNG_INFO_tRNS; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_TRNS; #else png_ptr->flags |= PNG_FLAG_FREE_TRNS; #endif } } #endif #ifdef PNG_sPLT_SUPPORTED void PNGAPI png_set_sPLT(png_structp png_ptr, png_infop info_ptr, png_sPLT_tp entries, int nentries) /* * entries - array of png_sPLT_t structures * to be added to the list of palettes * in the info structure. * nentries - number of palette structures to be * added. */ { png_sPLT_tp np; int i; if (png_ptr == NULL || info_ptr == NULL) return; np = (png_sPLT_tp)png_malloc_warn(png_ptr, (info_ptr->splt_palettes_num + nentries) * (png_uint_32)png_sizeof(png_sPLT_t)); if (np == NULL) { png_warning(png_ptr, "No memory for sPLT palettes."); return; } png_memcpy(np, info_ptr->splt_palettes, info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t)); png_free(png_ptr, info_ptr->splt_palettes); info_ptr->splt_palettes=NULL; for (i = 0; i < nentries; i++) { png_sPLT_tp to = np + info_ptr->splt_palettes_num + i; png_sPLT_tp from = entries + i; png_uint_32 length; length = png_strlen(from->name) + 1; to->name = (png_charp)png_malloc_warn(png_ptr, length); if (to->name == NULL) { png_warning(png_ptr, "Out of memory while processing sPLT chunk"); continue; } png_memcpy(to->name, from->name, length); to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr, (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry))); if (to->entries == NULL) { png_warning(png_ptr, "Out of memory while processing sPLT chunk"); png_free(png_ptr, to->name); to->name = NULL; continue; } png_memcpy(to->entries, from->entries, from->nentries * png_sizeof(png_sPLT_entry)); to->nentries = from->nentries; to->depth = from->depth; } info_ptr->splt_palettes = np; info_ptr->splt_palettes_num += nentries; info_ptr->valid |= PNG_INFO_sPLT; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_SPLT; #endif } #endif /* PNG_sPLT_SUPPORTED */ #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED void PNGAPI png_set_unknown_chunks(png_structp png_ptr, png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns) { png_unknown_chunkp np; int i; if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0) return; np = (png_unknown_chunkp)png_malloc_warn(png_ptr, (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) * png_sizeof(png_unknown_chunk))); if (np == NULL) { png_warning(png_ptr, "Out of memory while processing unknown chunk."); return; } png_memcpy(np, info_ptr->unknown_chunks, info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk)); png_free(png_ptr, info_ptr->unknown_chunks); info_ptr->unknown_chunks = NULL; for (i = 0; i < num_unknowns; i++) { png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i; png_unknown_chunkp from = unknowns + i; png_memcpy((png_charp)to->name, (png_charp)from->name, png_sizeof(from->name)); to->name[png_sizeof(to->name)-1] = '\0'; to->size = from->size; /* Note our location in the read or write sequence */ to->location = (png_byte)(png_ptr->mode & 0xff); if (from->size == 0) to->data=NULL; else { to->data = (png_bytep)png_malloc_warn(png_ptr, (png_uint_32)from->size); if (to->data == NULL) { png_warning(png_ptr, "Out of memory while processing unknown chunk."); to->size = 0; } else png_memcpy(to->data, from->data, from->size); } } info_ptr->unknown_chunks = np; info_ptr->unknown_chunks_num += num_unknowns; #ifdef PNG_FREE_ME_SUPPORTED info_ptr->free_me |= PNG_FREE_UNKN; #endif } void PNGAPI png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr, int chunk, int location) { if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk < (int)info_ptr->unknown_chunks_num) info_ptr->unknown_chunks[chunk].location = (png_byte)location; } #endif #if defined(PNG_1_0_X) || defined(PNG_1_2_X) #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) void PNGAPI png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted) { /* This function is deprecated in favor of png_permit_mng_features() and will be removed from libpng-1.3.0 */ png_debug(1, "in png_permit_empty_plte, DEPRECATED."); if (png_ptr == NULL) return; png_ptr->mng_features_permitted = (png_byte) ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) | ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE))); } #endif #endif #ifdef PNG_MNG_FEATURES_SUPPORTED png_uint_32 PNGAPI png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features) { png_debug(1, "in png_permit_mng_features"); if (png_ptr == NULL) return (png_uint_32)0; png_ptr->mng_features_permitted = (png_byte)(mng_features & PNG_ALL_MNG_FEATURES); return (png_uint_32)png_ptr->mng_features_permitted; } #endif #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED void PNGAPI png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep chunk_list, int num_chunks) { png_bytep new_list, p; int i, old_num_chunks; if (png_ptr == NULL) return; if (num_chunks == 0) { if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE) png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS; else png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS; if (keep == PNG_HANDLE_CHUNK_ALWAYS) png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS; else png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS; return; } if (chunk_list == NULL) return; old_num_chunks = png_ptr->num_chunk_list; new_list=(png_bytep)png_malloc(png_ptr, (png_uint_32) (5*(num_chunks + old_num_chunks))); if (png_ptr->chunk_list != NULL) { png_memcpy(new_list, png_ptr->chunk_list, (png_size_t)(5*old_num_chunks)); png_free(png_ptr, png_ptr->chunk_list); png_ptr->chunk_list=NULL; } png_memcpy(new_list + 5*old_num_chunks, chunk_list, (png_size_t)(5*num_chunks)); for (p = new_list + 5*old_num_chunks + 4, i = 0; inum_chunk_list = old_num_chunks + num_chunks; png_ptr->chunk_list = new_list; #ifdef PNG_FREE_ME_SUPPORTED png_ptr->free_me |= PNG_FREE_LIST; #endif } #endif #ifdef PNG_READ_USER_CHUNKS_SUPPORTED void PNGAPI png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn) { png_debug(1, "in png_set_read_user_chunk_fn"); if (png_ptr == NULL) return; png_ptr->read_user_chunk_fn = read_user_chunk_fn; png_ptr->user_chunk_ptr = user_chunk_ptr; } #endif #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers) { png_debug1(1, "in %s storage function", "rows"); if (png_ptr == NULL || info_ptr == NULL) return; if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers)) png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); info_ptr->row_pointers = row_pointers; if (row_pointers) info_ptr->valid |= PNG_INFO_IDAT; } #endif void PNGAPI png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size) { if (png_ptr == NULL) return; png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf_size = (png_size_t)size; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } void PNGAPI png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask) { if (png_ptr && info_ptr) info_ptr->valid &= ~mask; } #ifndef PNG_1_0_X #ifdef PNG_ASSEMBLER_CODE_SUPPORTED /* Function was added to libpng 1.2.0 and should always exist by default */ void PNGAPI png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags) { /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */ if (png_ptr != NULL) png_ptr->asm_flags = 0; asm_flags = asm_flags; /* Quiet the compiler */ } /* This function was added to libpng 1.2.0 */ void PNGAPI png_set_mmx_thresholds (png_structp png_ptr, png_byte mmx_bitdepth_threshold, png_uint_32 mmx_rowbytes_threshold) { /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */ if (png_ptr == NULL) return; /* Quiet the compiler */ mmx_bitdepth_threshold = mmx_bitdepth_threshold; mmx_rowbytes_threshold = mmx_rowbytes_threshold; } #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED /* This function was added to libpng 1.2.6 */ void PNGAPI png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max) { /* Images with dimensions larger than these limits will be * rejected by png_set_IHDR(). To accept any PNG datastream * regardless of dimensions, set both limits to 0x7ffffffL. */ if (png_ptr == NULL) return; png_ptr->user_width_max = user_width_max; png_ptr->user_height_max = user_height_max; } #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI png_set_benign_errors(png_structp png_ptr, int allowed) { png_debug(1, "in png_set_benign_errors"); if (allowed) png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; else png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN; } #endif /* PNG_BENIGN_ERRORS_SUPPORTED */ #endif /* ?PNG_1_0_X */ #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngwio.c0000664000175000017500000001757012013417376016441 0ustar alfalf00000000000000 /* pngwio.c - functions for data output * * Last changed in libpng 1.2.41 [December 3, 2009] * Copyright (c) 1998-2009 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file provides a location for all output. Users who need * special handling are expected to write functions that have the same * arguments as these and perform similar functions, but that possibly * use different output methods. Note that you shouldn't change these * functions, but rather write replacement functions and then change * them at run time with png_set_write_fn(...). */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_WRITE_SUPPORTED /* Write the data to whatever output you are using. The default routine * writes to a file pointer. Note that this routine sometimes gets called * with very small lengths, so you should implement some kind of simple * buffering if you are using unbuffered writes. This should never be asked * to write more than 64K on a 16 bit machine. */ void /* PRIVATE */ png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { if (png_ptr->write_data_fn != NULL ) (*(png_ptr->write_data_fn))(png_ptr, data, length); else png_error(png_ptr, "Call to NULL write function"); } #ifdef PNG_STDIO_SUPPORTED /* This is the function that does the actual writing of data. If you are * not writing to a standard C stream, you should create a replacement * write_data function and use it at run time with png_set_write_fn(), rather * than changing the library. */ #ifndef USE_FAR_KEYWORD void PNGAPI png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_uint_32 check; if (png_ptr == NULL) return; #ifdef _WIN32_WCE if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) check = 0; #else check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); #endif if (check != length) png_error(png_ptr, "Write Error"); } #else /* This is the model-independent version. Since the standard I/O library * can't handle far buffers in the medium and small models, we have to copy * the data. */ #define NEAR_BUF_SIZE 1024 #define MIN(a,b) (a <= b ? a : b) void PNGAPI png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_uint_32 check; png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ png_FILE_p io_ptr; if (png_ptr == NULL) return; /* Check if data really is near. If so, use usual code. */ near_data = (png_byte *)CVT_PTR_NOCHECK(data); io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); if ((png_bytep)near_data == data) { #ifdef _WIN32_WCE if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) check = 0; #else check = fwrite(near_data, 1, length, io_ptr); #endif } else { png_byte buf[NEAR_BUF_SIZE]; png_size_t written, remaining, err; check = 0; remaining = length; do { written = MIN(NEAR_BUF_SIZE, remaining); png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ #ifdef _WIN32_WCE if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) err = 0; #else err = fwrite(buf, 1, written, io_ptr); #endif if (err != written) break; else check += err; data += written; remaining -= written; } while (remaining != 0); } if (check != length) png_error(png_ptr, "Write Error"); } #endif #endif /* This function is called to output any data pending writing (normally * to disk). After png_flush is called, there should be no data pending * writing in any buffers. */ #ifdef PNG_WRITE_FLUSH_SUPPORTED void /* PRIVATE */ png_flush(png_structp png_ptr) { if (png_ptr->output_flush_fn != NULL) (*(png_ptr->output_flush_fn))(png_ptr); } #ifdef PNG_STDIO_SUPPORTED void PNGAPI png_default_flush(png_structp png_ptr) { #ifndef _WIN32_WCE png_FILE_p io_ptr; #endif if (png_ptr == NULL) return; #ifndef _WIN32_WCE io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); fflush(io_ptr); #endif } #endif #endif /* This function allows the application to supply new output functions for * libpng if standard C streams aren't being used. * * This function takes as its arguments: * png_ptr - pointer to a png output data structure * io_ptr - pointer to user supplied structure containing info about * the output functions. May be NULL. * write_data_fn - pointer to a new output function that takes as its * arguments a pointer to a png_struct, a pointer to * data to be written, and a 32-bit unsigned int that is * the number of bytes to be written. The new write * function should call png_error(png_ptr, "Error msg") * to exit and output any fatal error messages. May be * NULL, in which case libpng's default function will * be used. * flush_data_fn - pointer to a new flush function that takes as its * arguments a pointer to a png_struct. After a call to * the flush function, there should be no data in any buffers * or pending transmission. If the output method doesn't do * any buffering of output, a function prototype must still be * supplied although it doesn't have to do anything. If * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile * time, output_flush_fn will be ignored, although it must be * supplied for compatibility. May be NULL, in which case * libpng's default function will be used, if * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not * a good idea if io_ptr does not point to a standard * *FILE structure. */ void PNGAPI png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) { if (png_ptr == NULL) return; png_ptr->io_ptr = io_ptr; #ifdef PNG_STDIO_SUPPORTED if (write_data_fn != NULL) png_ptr->write_data_fn = write_data_fn; else png_ptr->write_data_fn = png_default_write_data; #else png_ptr->write_data_fn = write_data_fn; #endif #ifdef PNG_WRITE_FLUSH_SUPPORTED #ifdef PNG_STDIO_SUPPORTED if (output_flush_fn != NULL) png_ptr->output_flush_fn = output_flush_fn; else png_ptr->output_flush_fn = png_default_flush; #else png_ptr->output_flush_fn = output_flush_fn; #endif #endif /* PNG_WRITE_FLUSH_SUPPORTED */ /* It is an error to read while writing a png file */ if (png_ptr->read_data_fn != NULL) { png_ptr->read_data_fn = NULL; png_warning(png_ptr, "Attempted to set both read_data_fn and write_data_fn in"); png_warning(png_ptr, "the same structure. Resetting read_data_fn to NULL."); } } #ifdef USE_FAR_KEYWORD #ifdef _MSC_VER void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) { void *near_ptr; void FAR *far_ptr; FP_OFF(near_ptr) = FP_OFF(ptr); far_ptr = (void FAR *)near_ptr; if (check != 0) if (FP_SEG(ptr) != FP_SEG(far_ptr)) png_error(png_ptr, "segment lost in conversion"); return(near_ptr); } # else void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) { void *near_ptr; void FAR *far_ptr; near_ptr = (void FAR *)ptr; far_ptr = (void FAR *)near_ptr; if (check != 0) if (far_ptr != ptr) png_error(png_ptr, "segment lost in conversion"); return(near_ptr); } # endif # endif #endif /* PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngwrite.c0000664000175000017500000014160112013417376016766 0ustar alfalf00000000000000 /* pngwrite.c - general routines to write a PNG file * * Last changed in libpng 1.2.45 [July 7, 2011] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ /* Get internal access to png.h */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_WRITE_SUPPORTED /* Writes all the PNG information. This is the suggested way to use the * library. If you have a new chunk to add, make a function to write it, * and put it in the correct location here. If you want the chunk written * after the image data, put it in png_write_end(). I strongly encourage * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing * the chunk, as that will keep the code from breaking if you want to just * write a plain PNG file. If you have long comments, I suggest writing * them in png_write_end(), and compressing them. */ void PNGAPI png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_write_info_before_PLTE"); if (png_ptr == NULL || info_ptr == NULL) return; if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) { /* Write PNG signature */ png_write_sig(png_ptr); #ifdef PNG_MNG_FEATURES_SUPPORTED if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \ (png_ptr->mng_features_permitted)) { png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); png_ptr->mng_features_permitted = 0; } #endif /* Write IHDR information. */ png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, info_ptr->filter_type, #ifdef PNG_WRITE_INTERLACING_SUPPORTED info_ptr->interlace_type); #else 0); #endif /* The rest of these check to see if the valid field has the appropriate * flag set, and if it does, writes the chunk. */ #ifdef PNG_WRITE_gAMA_SUPPORTED if (info_ptr->valid & PNG_INFO_gAMA) { # ifdef PNG_FLOATING_POINT_SUPPORTED png_write_gAMA(png_ptr, info_ptr->gamma); #else #ifdef PNG_FIXED_POINT_SUPPORTED png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma); # endif #endif } #endif #ifdef PNG_WRITE_sRGB_SUPPORTED if (info_ptr->valid & PNG_INFO_sRGB) png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent); #endif #ifdef PNG_WRITE_iCCP_SUPPORTED if (info_ptr->valid & PNG_INFO_iCCP) png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE, info_ptr->iccp_profile, (int)info_ptr->iccp_proflen); #endif #ifdef PNG_WRITE_sBIT_SUPPORTED if (info_ptr->valid & PNG_INFO_sBIT) png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); #endif #ifdef PNG_WRITE_cHRM_SUPPORTED if (info_ptr->valid & PNG_INFO_cHRM) { #ifdef PNG_FLOATING_POINT_SUPPORTED png_write_cHRM(png_ptr, info_ptr->x_white, info_ptr->y_white, info_ptr->x_red, info_ptr->y_red, info_ptr->x_green, info_ptr->y_green, info_ptr->x_blue, info_ptr->y_blue); #else # ifdef PNG_FIXED_POINT_SUPPORTED png_write_cHRM_fixed(png_ptr, info_ptr->int_x_white, info_ptr->int_y_white, info_ptr->int_x_red, info_ptr->int_y_red, info_ptr->int_x_green, info_ptr->int_y_green, info_ptr->int_x_blue, info_ptr->int_y_blue); # endif #endif } #endif #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED if (info_ptr->unknown_chunks_num) { png_unknown_chunk *up; png_debug(5, "writing extra chunks"); for (up = info_ptr->unknown_chunks; up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; up++) { int keep = png_handle_as_unknown(png_ptr, up->name); if (keep != PNG_HANDLE_CHUNK_NEVER && up->location && !(up->location & PNG_HAVE_PLTE) && !(up->location & PNG_HAVE_IDAT) && ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) { if (up->size == 0) png_warning(png_ptr, "Writing zero-length unknown chunk"); png_write_chunk(png_ptr, up->name, up->data, up->size); } } } #endif png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; } } void PNGAPI png_write_info(png_structp png_ptr, png_infop info_ptr) { #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) int i; #endif png_debug(1, "in png_write_info"); if (png_ptr == NULL || info_ptr == NULL) return; png_write_info_before_PLTE(png_ptr, info_ptr); if (info_ptr->valid & PNG_INFO_PLTE) png_write_PLTE(png_ptr, info_ptr->palette, (png_uint_32)info_ptr->num_palette); else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) png_error(png_ptr, "Valid palette required for paletted images"); #ifdef PNG_WRITE_tRNS_SUPPORTED if (info_ptr->valid & PNG_INFO_tRNS) { #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel (in tRNS) */ if ((png_ptr->transformations & PNG_INVERT_ALPHA) && info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { int j; for (j = 0; j<(int)info_ptr->num_trans; j++) info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]); } #endif png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values), info_ptr->num_trans, info_ptr->color_type); } #endif #ifdef PNG_WRITE_bKGD_SUPPORTED if (info_ptr->valid & PNG_INFO_bKGD) png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); #endif #ifdef PNG_WRITE_hIST_SUPPORTED if (info_ptr->valid & PNG_INFO_hIST) png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); #endif #ifdef PNG_WRITE_oFFs_SUPPORTED if (info_ptr->valid & PNG_INFO_oFFs) png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, info_ptr->offset_unit_type); #endif #ifdef PNG_WRITE_pCAL_SUPPORTED if (info_ptr->valid & PNG_INFO_pCAL) png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, info_ptr->pcal_units, info_ptr->pcal_params); #endif #ifdef PNG_sCAL_SUPPORTED if (info_ptr->valid & PNG_INFO_sCAL) #ifdef PNG_WRITE_sCAL_SUPPORTED #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) png_write_sCAL(png_ptr, (int)info_ptr->scal_unit, info_ptr->scal_pixel_width, info_ptr->scal_pixel_height); #else /* !FLOATING_POINT */ #ifdef PNG_FIXED_POINT_SUPPORTED png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, info_ptr->scal_s_width, info_ptr->scal_s_height); #endif /* FIXED_POINT */ #endif /* FLOATING_POINT */ #else /* !WRITE_sCAL */ png_warning(png_ptr, "png_write_sCAL not supported; sCAL chunk not written."); #endif /* WRITE_sCAL */ #endif /* sCAL */ #ifdef PNG_WRITE_pHYs_SUPPORTED if (info_ptr->valid & PNG_INFO_pHYs) png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); #endif /* pHYs */ #ifdef PNG_WRITE_tIME_SUPPORTED if (info_ptr->valid & PNG_INFO_tIME) { png_write_tIME(png_ptr, &(info_ptr->mod_time)); png_ptr->mode |= PNG_WROTE_tIME; } #endif /* tIME */ #ifdef PNG_WRITE_sPLT_SUPPORTED if (info_ptr->valid & PNG_INFO_sPLT) for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); #endif /* sPLT */ #ifdef PNG_WRITE_TEXT_SUPPORTED /* Check to see if we need to write text chunks */ for (i = 0; i < info_ptr->num_text; i++) { png_debug2(2, "Writing header text chunk %d, type %d", i, info_ptr->text[i].compression); /* An internationalized chunk? */ if (info_ptr->text[i].compression > 0) { #ifdef PNG_WRITE_iTXt_SUPPORTED /* Write international chunk */ png_write_iTXt(png_ptr, info_ptr->text[i].compression, info_ptr->text[i].key, info_ptr->text[i].lang, info_ptr->text[i].lang_key, info_ptr->text[i].text); #else png_warning(png_ptr, "Unable to write international text"); #endif /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } /* If we want a compressed text chunk */ else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) { #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write compressed chunk */ png_write_zTXt(png_ptr, info_ptr->text[i].key, info_ptr->text[i].text, 0, info_ptr->text[i].compression); #else png_warning(png_ptr, "Unable to write compressed text"); #endif /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; } else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) { #ifdef PNG_WRITE_tEXt_SUPPORTED /* Write uncompressed chunk */ png_write_tEXt(png_ptr, info_ptr->text[i].key, info_ptr->text[i].text, 0); /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; #else /* Can't get here */ png_warning(png_ptr, "Unable to write uncompressed text"); #endif } } #endif /* tEXt */ #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED if (info_ptr->unknown_chunks_num) { png_unknown_chunk *up; png_debug(5, "writing extra chunks"); for (up = info_ptr->unknown_chunks; up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; up++) { int keep = png_handle_as_unknown(png_ptr, up->name); if (keep != PNG_HANDLE_CHUNK_NEVER && up->location && (up->location & PNG_HAVE_PLTE) && !(up->location & PNG_HAVE_IDAT) && !(up->location & PNG_AFTER_IDAT) && ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) { png_write_chunk(png_ptr, up->name, up->data, up->size); } } } #endif } /* Writes the end of the PNG file. If you don't want to write comments or * time information, you can pass NULL for info. If you already wrote these * in png_write_info(), do not write them again here. If you have long * comments, I suggest writing them here, and compressing them. */ void PNGAPI png_write_end(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_write_end"); if (png_ptr == NULL) return; if (!(png_ptr->mode & PNG_HAVE_IDAT)) png_error(png_ptr, "No IDATs written into file"); /* See if user wants us to write information chunks */ if (info_ptr != NULL) { #ifdef PNG_WRITE_TEXT_SUPPORTED int i; /* local index variable */ #endif #ifdef PNG_WRITE_tIME_SUPPORTED /* Check to see if user has supplied a time chunk */ if ((info_ptr->valid & PNG_INFO_tIME) && !(png_ptr->mode & PNG_WROTE_tIME)) png_write_tIME(png_ptr, &(info_ptr->mod_time)); #endif #ifdef PNG_WRITE_TEXT_SUPPORTED /* Loop through comment chunks */ for (i = 0; i < info_ptr->num_text; i++) { png_debug2(2, "Writing trailer text chunk %d, type %d", i, info_ptr->text[i].compression); /* An internationalized chunk? */ if (info_ptr->text[i].compression > 0) { #ifdef PNG_WRITE_iTXt_SUPPORTED /* Write international chunk */ png_write_iTXt(png_ptr, info_ptr->text[i].compression, info_ptr->text[i].key, info_ptr->text[i].lang, info_ptr->text[i].lang_key, info_ptr->text[i].text); #else png_warning(png_ptr, "Unable to write international text"); #endif /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) { #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write compressed chunk */ png_write_zTXt(png_ptr, info_ptr->text[i].key, info_ptr->text[i].text, 0, info_ptr->text[i].compression); #else png_warning(png_ptr, "Unable to write compressed text"); #endif /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; } else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) { #ifdef PNG_WRITE_tEXt_SUPPORTED /* Write uncompressed chunk */ png_write_tEXt(png_ptr, info_ptr->text[i].key, info_ptr->text[i].text, 0); #else png_warning(png_ptr, "Unable to write uncompressed text"); #endif /* Mark this chunk as written */ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } } #endif #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED if (info_ptr->unknown_chunks_num) { png_unknown_chunk *up; png_debug(5, "writing extra chunks"); for (up = info_ptr->unknown_chunks; up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; up++) { int keep = png_handle_as_unknown(png_ptr, up->name); if (keep != PNG_HANDLE_CHUNK_NEVER && up->location && (up->location & PNG_AFTER_IDAT) && ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) { png_write_chunk(png_ptr, up->name, up->data, up->size); } } } #endif } png_ptr->mode |= PNG_AFTER_IDAT; /* Write end of PNG file */ png_write_IEND(png_ptr); /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, * and restored again in libpng-1.2.30, may cause some applications that * do not set png_ptr->output_flush_fn to crash. If your application * experiences a problem, please try building libpng with * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to * png-mng-implement at lists.sf.net . */ #ifdef PNG_WRITE_FLUSH_SUPPORTED # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED png_flush(png_ptr); # endif #endif } #ifdef PNG_CONVERT_tIME_SUPPORTED /* "tm" structure is not supported on WindowsCE */ void PNGAPI png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime) { png_debug(1, "in png_convert_from_struct_tm"); ptime->year = (png_uint_16)(1900 + ttime->tm_year); ptime->month = (png_byte)(ttime->tm_mon + 1); ptime->day = (png_byte)ttime->tm_mday; ptime->hour = (png_byte)ttime->tm_hour; ptime->minute = (png_byte)ttime->tm_min; ptime->second = (png_byte)ttime->tm_sec; } void PNGAPI png_convert_from_time_t(png_timep ptime, time_t ttime) { struct tm *tbuf; png_debug(1, "in png_convert_from_time_t"); tbuf = gmtime(&ttime); png_convert_from_struct_tm(ptime, tbuf); } #endif /* Initialize png_ptr structure, and allocate any memory needed */ png_structp PNGAPI png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn) { #ifdef PNG_USER_MEM_SUPPORTED return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn, warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); } /* Alternate initialize png_ptr structure, and allocate any memory needed */ png_structp PNGAPI png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn) { #endif /* PNG_USER_MEM_SUPPORTED */ #ifdef PNG_SETJMP_SUPPORTED volatile #endif png_structp png_ptr; #ifdef PNG_SETJMP_SUPPORTED #ifdef USE_FAR_KEYWORD jmp_buf jmpbuf; #endif #endif int i; png_debug(1, "in png_create_write_struct"); #ifdef PNG_USER_MEM_SUPPORTED png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); #else png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); #endif /* PNG_USER_MEM_SUPPORTED */ if (png_ptr == NULL) return (NULL); /* Added at libpng-1.2.6 */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED png_ptr->user_width_max = PNG_USER_WIDTH_MAX; png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; #endif #ifdef PNG_SETJMP_SUPPORTED #ifdef USE_FAR_KEYWORD if (setjmp(jmpbuf)) #else if (setjmp(png_ptr->jmpbuf)) #endif { png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif return (NULL); } #ifdef USE_FAR_KEYWORD png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); #endif #endif #ifdef PNG_USER_MEM_SUPPORTED png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); #endif /* PNG_USER_MEM_SUPPORTED */ png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); if (user_png_ver) { i = 0; do { if (user_png_ver[i] != png_libpng_ver[i]) png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; } while (png_libpng_ver[i++]); } if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) { /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so * we must recompile any applications that use any older library version. * For versions after libpng 1.0, we will be compatible, so we need * only check the first digit. */ if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || (user_png_ver[0] == '0' && user_png_ver[2] < '9')) { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char msg[80]; if (user_png_ver) { png_snprintf(msg, 80, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } png_snprintf(msg, 80, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg); #endif #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "Incompatible libpng version in application and library"); } } /* Initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL, png_flush_ptr_NULL); #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, 1, png_doublep_NULL, png_doublep_NULL); #endif #ifdef PNG_SETJMP_SUPPORTED /* Applications that neglect to set up their own setjmp() and then * encounter a png_error() will longjmp here. Since the jmpbuf is * then meaningless we abort instead of returning. */ #ifdef USE_FAR_KEYWORD if (setjmp(jmpbuf)) PNG_ABORT(); png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); #else if (setjmp(png_ptr->jmpbuf)) PNG_ABORT(); #endif #endif return (png_ptr); } /* Initialize png_ptr structure, and allocate any memory needed */ #if defined(PNG_1_0_X) || defined(PNG_1_2_X) /* Deprecated. */ #undef png_write_init void PNGAPI png_write_init(png_structp png_ptr) { /* We only come here via pre-1.0.7-compiled applications */ png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0); } void PNGAPI png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t png_info_size) { /* We only come here via pre-1.0.12-compiled applications */ if (png_ptr == NULL) return; #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) if (png_sizeof(png_struct) > png_struct_size || png_sizeof(png_info) > png_info_size) { char msg[80]; png_ptr->warning_fn = NULL; if (user_png_ver) { png_snprintf(msg, 80, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } png_snprintf(msg, 80, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg); } #endif if (png_sizeof(png_struct) > png_struct_size) { png_ptr->error_fn = NULL; #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "The png struct allocated by the application for writing is" " too small."); } if (png_sizeof(png_info) > png_info_size) { png_ptr->error_fn = NULL; #ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags = 0; #endif png_error(png_ptr, "The info struct allocated by the application for writing is" " too small."); } png_write_init_3(&png_ptr, user_png_ver, png_struct_size); } #endif /* PNG_1_0_X || PNG_1_2_X */ void PNGAPI png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, png_size_t png_struct_size) { png_structp png_ptr = *ptr_ptr; #ifdef PNG_SETJMP_SUPPORTED jmp_buf tmp_jmp; /* to save current jump buffer */ #endif int i = 0; if (png_ptr == NULL) return; do { if (user_png_ver[i] != png_libpng_ver[i]) { #ifdef PNG_LEGACY_SUPPORTED png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; #else png_ptr->warning_fn = NULL; png_warning(png_ptr, "Application uses deprecated png_write_init() and should be recompiled."); #endif } } while (png_libpng_ver[i++]); png_debug(1, "in png_write_init_3"); #ifdef PNG_SETJMP_SUPPORTED /* Save jump buffer and error functions */ png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); #endif if (png_sizeof(png_struct) > png_struct_size) { png_destroy_struct(png_ptr); png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); *ptr_ptr = png_ptr; } /* Reset all variables to 0 */ png_memset(png_ptr, 0, png_sizeof(png_struct)); /* Added at libpng-1.2.6 */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED png_ptr->user_width_max = PNG_USER_WIDTH_MAX; png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; #endif #ifdef PNG_SETJMP_SUPPORTED /* Restore jump buffer */ png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); #endif png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL, png_flush_ptr_NULL); /* Initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, 1, png_doublep_NULL, png_doublep_NULL); #endif } /* Write a few rows of image data. If the image is interlaced, * either you will have to write the 7 sub images, or, if you * have called png_set_interlace_handling(), you will have to * "write" the image seven times. */ void PNGAPI png_write_rows(png_structp png_ptr, png_bytepp row, png_uint_32 num_rows) { png_uint_32 i; /* row counter */ png_bytepp rp; /* row pointer */ png_debug(1, "in png_write_rows"); if (png_ptr == NULL) return; /* Loop through the rows */ for (i = 0, rp = row; i < num_rows; i++, rp++) { png_write_row(png_ptr, *rp); } } /* Write the image. You only need to call this function once, even * if you are writing an interlaced image. */ void PNGAPI png_write_image(png_structp png_ptr, png_bytepp image) { png_uint_32 i; /* row index */ int pass, num_pass; /* pass variables */ png_bytepp rp; /* points to current row */ if (png_ptr == NULL) return; png_debug(1, "in png_write_image"); #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Initialize interlace handling. If image is not interlaced, * this will set pass to 1 */ num_pass = png_set_interlace_handling(png_ptr); #else num_pass = 1; #endif /* Loop through passes */ for (pass = 0; pass < num_pass; pass++) { /* Loop through image */ for (i = 0, rp = image; i < png_ptr->height; i++, rp++) { png_write_row(png_ptr, *rp); } } } /* Called by user to write a row of image data */ void PNGAPI png_write_row(png_structp png_ptr, png_bytep row) { if (png_ptr == NULL) return; png_debug2(1, "in png_write_row (row %ld, pass %d)", png_ptr->row_number, png_ptr->pass); /* Initialize transformations and other stuff if first time */ if (png_ptr->row_number == 0 && png_ptr->pass == 0) { /* Make sure we wrote the header info */ if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) png_error(png_ptr, "png_write_info was never called before png_write_row."); /* Check for transforms that have been set but were defined out */ #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) if (png_ptr->transformations & PNG_INVERT_MONO) png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) if (png_ptr->transformations & PNG_FILLER) png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ defined(PNG_READ_PACKSWAP_SUPPORTED) if (png_ptr->transformations & PNG_PACKSWAP) png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) if (png_ptr->transformations & PNG_PACK) png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) if (png_ptr->transformations & PNG_SHIFT) png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) if (png_ptr->transformations & PNG_BGR) png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined."); #endif #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) if (png_ptr->transformations & PNG_SWAP_BYTES) png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined."); #endif png_write_start_row(png_ptr); } #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced and not interested in row, return */ if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) { switch (png_ptr->pass) { case 0: if (png_ptr->row_number & 0x07) { png_write_finish_row(png_ptr); return; } break; case 1: if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) { png_write_finish_row(png_ptr); return; } break; case 2: if ((png_ptr->row_number & 0x07) != 4) { png_write_finish_row(png_ptr); return; } break; case 3: if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) { png_write_finish_row(png_ptr); return; } break; case 4: if ((png_ptr->row_number & 0x03) != 2) { png_write_finish_row(png_ptr); return; } break; case 5: if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) { png_write_finish_row(png_ptr); return; } break; case 6: if (!(png_ptr->row_number & 0x01)) { png_write_finish_row(png_ptr); return; } break; } } #endif /* Set up row info for transformations */ png_ptr->row_info.color_type = png_ptr->color_type; png_ptr->row_info.width = png_ptr->usr_width; png_ptr->row_info.channels = png_ptr->usr_channels; png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth; png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * png_ptr->row_info.channels); png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->row_info.width); png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type); png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width); png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels); png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth); png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth); png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes); /* Copy user's row into buffer, leaving room for filter byte. */ png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes); #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Handle interlacing */ if (png_ptr->interlaced && png_ptr->pass < 6 && (png_ptr->transformations & PNG_INTERLACE)) { png_do_write_interlace(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->pass); /* This should always get caught above, but still ... */ if (!(png_ptr->row_info.width)) { png_write_finish_row(png_ptr); return; } } #endif /* Handle other transformations */ if (png_ptr->transformations) png_do_write_transformations(png_ptr); #ifdef PNG_MNG_FEATURES_SUPPORTED /* Write filter_method 64 (intrapixel differencing) only if * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and * 2. Libpng did not write a PNG signature (this filter_method is only * used in PNG datastreams that are embedded in MNG datastreams) and * 3. The application called png_permit_mng_features with a mask that * included PNG_FLAG_MNG_FILTER_64 and * 4. The filter_method is 64 and * 5. The color_type is RGB or RGBA */ if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) { /* Intrapixel differencing */ png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); } #endif /* Find a filter if necessary, filter the row and write it out. */ png_write_find_filter(png_ptr, &(png_ptr->row_info)); if (png_ptr->write_row_fn != NULL) (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); } #ifdef PNG_WRITE_FLUSH_SUPPORTED /* Set the automatic flush interval or 0 to turn flushing off */ void PNGAPI png_set_flush(png_structp png_ptr, int nrows) { png_debug(1, "in png_set_flush"); if (png_ptr == NULL) return; png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); } /* Flush the current output buffers now */ void PNGAPI png_write_flush(png_structp png_ptr) { int wrote_IDAT; png_debug(1, "in png_write_flush"); if (png_ptr == NULL) return; /* We have already written out all of the data */ if (png_ptr->row_number >= png_ptr->num_rows) return; do { int ret; /* Compress the data */ ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); wrote_IDAT = 0; /* Check for compression errors */ if (ret != Z_OK) { if (png_ptr->zstream.msg != NULL) png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } if (!(png_ptr->zstream.avail_out)) { /* Write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; wrote_IDAT = 1; } } while(wrote_IDAT == 1); /* If there is any data left to be output, write it into a new IDAT */ if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) { /* Write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - png_ptr->zstream.avail_out); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } png_ptr->flush_rows = 0; png_flush(png_ptr); } #endif /* PNG_WRITE_FLUSH_SUPPORTED */ /* Free all memory used by the write */ void PNGAPI png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) { png_structp png_ptr = NULL; png_infop info_ptr = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn = NULL; png_voidp mem_ptr = NULL; #endif png_debug(1, "in png_destroy_write_struct"); if (png_ptr_ptr != NULL) { png_ptr = *png_ptr_ptr; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; mem_ptr = png_ptr->mem_ptr; #endif } #ifdef PNG_USER_MEM_SUPPORTED if (png_ptr != NULL) { free_fn = png_ptr->free_fn; mem_ptr = png_ptr->mem_ptr; } #endif if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (info_ptr != NULL) { if (png_ptr != NULL) { png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED if (png_ptr->num_chunk_list) { png_free(png_ptr, png_ptr->chunk_list); png_ptr->chunk_list = NULL; png_ptr->num_chunk_list = 0; } #endif } #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = NULL; } if (png_ptr != NULL) { png_write_destroy(png_ptr); #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif *png_ptr_ptr = NULL; } } /* Free any memory used in png_ptr struct (old method) */ void /* PRIVATE */ png_write_destroy(png_structp png_ptr) { #ifdef PNG_SETJMP_SUPPORTED jmp_buf tmp_jmp; /* Save jump buffer */ #endif png_error_ptr error_fn; png_error_ptr warning_fn; png_voidp error_ptr; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn; #endif png_debug(1, "in png_write_destroy"); /* Free any memory zlib uses */ deflateEnd(&png_ptr->zstream); /* Free our memory. png_free checks NULL for us. */ png_free(png_ptr, png_ptr->zbuf); png_free(png_ptr, png_ptr->row_buf); #ifdef PNG_WRITE_FILTER_SUPPORTED png_free(png_ptr, png_ptr->prev_row); png_free(png_ptr, png_ptr->sub_row); png_free(png_ptr, png_ptr->up_row); png_free(png_ptr, png_ptr->avg_row); png_free(png_ptr, png_ptr->paeth_row); #endif #ifdef PNG_TIME_RFC1123_SUPPORTED png_free(png_ptr, png_ptr->time_buffer); #endif #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED png_free(png_ptr, png_ptr->prev_filters); png_free(png_ptr, png_ptr->filter_weights); png_free(png_ptr, png_ptr->inv_filter_weights); png_free(png_ptr, png_ptr->filter_costs); png_free(png_ptr, png_ptr->inv_filter_costs); #endif #ifdef PNG_SETJMP_SUPPORTED /* Reset structure */ png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); #endif error_fn = png_ptr->error_fn; warning_fn = png_ptr->warning_fn; error_ptr = png_ptr->error_ptr; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; #endif png_memset(png_ptr, 0, png_sizeof(png_struct)); png_ptr->error_fn = error_fn; png_ptr->warning_fn = warning_fn; png_ptr->error_ptr = error_ptr; #ifdef PNG_USER_MEM_SUPPORTED png_ptr->free_fn = free_fn; #endif #ifdef PNG_SETJMP_SUPPORTED png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); #endif } /* Allow the application to select one or more row filters to use. */ void PNGAPI png_set_filter(png_structp png_ptr, int method, int filters) { png_debug(1, "in png_set_filter"); if (png_ptr == NULL) return; #ifdef PNG_MNG_FEATURES_SUPPORTED if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && (method == PNG_INTRAPIXEL_DIFFERENCING)) method = PNG_FILTER_TYPE_BASE; #endif if (method == PNG_FILTER_TYPE_BASE) { switch (filters & (PNG_ALL_FILTERS | 0x07)) { #ifdef PNG_WRITE_FILTER_SUPPORTED case 5: case 6: case 7: png_warning(png_ptr, "Unknown row filter for method 0"); #endif /* PNG_WRITE_FILTER_SUPPORTED */ case PNG_FILTER_VALUE_NONE: png_ptr->do_filter = PNG_FILTER_NONE; break; #ifdef PNG_WRITE_FILTER_SUPPORTED case PNG_FILTER_VALUE_SUB: png_ptr->do_filter = PNG_FILTER_SUB; break; case PNG_FILTER_VALUE_UP: png_ptr->do_filter = PNG_FILTER_UP; break; case PNG_FILTER_VALUE_AVG: png_ptr->do_filter = PNG_FILTER_AVG; break; case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter = PNG_FILTER_PAETH; break; default: png_ptr->do_filter = (png_byte)filters; break; #else default: png_warning(png_ptr, "Unknown row filter for method 0"); #endif /* PNG_WRITE_FILTER_SUPPORTED */ } /* If we have allocated the row_buf, this means we have already started * with the image and we should have allocated all of the filter buffers * that have been selected. If prev_row isn't already allocated, then * it is too late to start using the filters that need it, since we * will be missing the data in the previous row. If an application * wants to start and stop using particular filters during compression, * it should start out with all of the filters, and then add and * remove them after the start of compression. */ if (png_ptr->row_buf != NULL) { #ifdef PNG_WRITE_FILTER_SUPPORTED if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL) { png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, (png_ptr->rowbytes + 1)); png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; } if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL) { if (png_ptr->prev_row == NULL) { png_warning(png_ptr, "Can't add Up filter after starting"); png_ptr->do_filter &= ~PNG_FILTER_UP; } else { png_ptr->up_row = (png_bytep)png_malloc(png_ptr, (png_ptr->rowbytes + 1)); png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; } } if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL) { if (png_ptr->prev_row == NULL) { png_warning(png_ptr, "Can't add Average filter after starting"); png_ptr->do_filter &= ~PNG_FILTER_AVG; } else { png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, (png_ptr->rowbytes + 1)); png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; } } if ((png_ptr->do_filter & PNG_FILTER_PAETH) && png_ptr->paeth_row == NULL) { if (png_ptr->prev_row == NULL) { png_warning(png_ptr, "Can't add Paeth filter after starting"); png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); } else { png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, (png_ptr->rowbytes + 1)); png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; } } if (png_ptr->do_filter == PNG_NO_FILTERS) #endif /* PNG_WRITE_FILTER_SUPPORTED */ png_ptr->do_filter = PNG_FILTER_NONE; } } else png_error(png_ptr, "Unknown custom filter method"); } /* This allows us to influence the way in which libpng chooses the "best" * filter for the current scanline. While the "minimum-sum-of-absolute- * differences metric is relatively fast and effective, there is some * question as to whether it can be improved upon by trying to keep the * filtered data going to zlib more consistent, hopefully resulting in * better compression. */ #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */ void PNGAPI png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, int num_weights, png_doublep filter_weights, png_doublep filter_costs) { int i; png_debug(1, "in png_set_filter_heuristics"); if (png_ptr == NULL) return; if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST) { png_warning(png_ptr, "Unknown filter heuristic method"); return; } if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT) { heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; } if (num_weights < 0 || filter_weights == NULL || heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) { num_weights = 0; } png_ptr->num_prev_filters = (png_byte)num_weights; png_ptr->heuristic_method = (png_byte)heuristic_method; if (num_weights > 0) { if (png_ptr->prev_filters == NULL) { png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, (png_uint_32)(png_sizeof(png_byte) * num_weights)); /* To make sure that the weighting starts out fairly */ for (i = 0; i < num_weights; i++) { png_ptr->prev_filters[i] = 255; } } if (png_ptr->filter_weights == NULL) { png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); for (i = 0; i < num_weights; i++) { png_ptr->inv_filter_weights[i] = png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; } } for (i = 0; i < num_weights; i++) { if (filter_weights[i] < 0.0) { png_ptr->inv_filter_weights[i] = png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; } else { png_ptr->inv_filter_weights[i] = (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5); png_ptr->filter_weights[i] = (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5); } } } /* If, in the future, there are other filter methods, this would * need to be based on png_ptr->filter. */ if (png_ptr->filter_costs == NULL) { png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) { png_ptr->inv_filter_costs[i] = png_ptr->filter_costs[i] = PNG_COST_FACTOR; } } /* Here is where we set the relative costs of the different filters. We * should take the desired compression level into account when setting * the costs, so that Paeth, for instance, has a high relative cost at low * compression levels, while it has a lower relative cost at higher * compression settings. The filter types are in order of increasing * relative cost, so it would be possible to do this with an algorithm. */ for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) { if (filter_costs == NULL || filter_costs[i] < 0.0) { png_ptr->inv_filter_costs[i] = png_ptr->filter_costs[i] = PNG_COST_FACTOR; } else if (filter_costs[i] >= 1.0) { png_ptr->inv_filter_costs[i] = (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5); png_ptr->filter_costs[i] = (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5); } } } #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ void PNGAPI png_set_compression_level(png_structp png_ptr, int level) { png_debug(1, "in png_set_compression_level"); if (png_ptr == NULL) return; png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; png_ptr->zlib_level = level; } void PNGAPI png_set_compression_mem_level(png_structp png_ptr, int mem_level) { png_debug(1, "in png_set_compression_mem_level"); if (png_ptr == NULL) return; png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; png_ptr->zlib_mem_level = mem_level; } void PNGAPI png_set_compression_strategy(png_structp png_ptr, int strategy) { png_debug(1, "in png_set_compression_strategy"); if (png_ptr == NULL) return; png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; png_ptr->zlib_strategy = strategy; } void PNGAPI png_set_compression_window_bits(png_structp png_ptr, int window_bits) { if (png_ptr == NULL) return; if (window_bits > 15) png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); else if (window_bits < 8) png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); #ifndef WBITS_8_OK /* Avoid libpng bug with 256-byte windows */ if (window_bits == 8) { png_warning(png_ptr, "Compression window is being reset to 512"); window_bits = 9; } #endif png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; png_ptr->zlib_window_bits = window_bits; } void PNGAPI png_set_compression_method(png_structp png_ptr, int method) { png_debug(1, "in png_set_compression_method"); if (png_ptr == NULL) return; if (method != 8) png_warning(png_ptr, "Only compression method 8 is supported by PNG"); png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; png_ptr->zlib_method = method; } void PNGAPI png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) { if (png_ptr == NULL) return; png_ptr->write_row_fn = write_row_fn; } #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED void PNGAPI png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr write_user_transform_fn) { png_debug(1, "in png_set_write_user_transform_fn"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_USER_TRANSFORM; png_ptr->write_user_transform_fn = write_user_transform_fn; } #endif #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI png_write_png(png_structp png_ptr, png_infop info_ptr, int transforms, voidp params) { if (png_ptr == NULL || info_ptr == NULL) return; /* Write the file header information. */ png_write_info(png_ptr, info_ptr); /* ------ these transformations don't touch the info structure ------- */ #ifdef PNG_WRITE_INVERT_SUPPORTED /* Invert monochrome pixels */ if (transforms & PNG_TRANSFORM_INVERT_MONO) png_set_invert_mono(png_ptr); #endif #ifdef PNG_WRITE_SHIFT_SUPPORTED /* Shift the pixels up to a legal bit depth and fill in * as appropriate to correctly scale the image. */ if ((transforms & PNG_TRANSFORM_SHIFT) && (info_ptr->valid & PNG_INFO_sBIT)) png_set_shift(png_ptr, &info_ptr->sig_bit); #endif #ifdef PNG_WRITE_PACK_SUPPORTED /* Pack pixels into bytes */ if (transforms & PNG_TRANSFORM_PACKING) png_set_packing(png_ptr); #endif #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED /* Swap location of alpha bytes from ARGB to RGBA */ if (transforms & PNG_TRANSFORM_SWAP_ALPHA) png_set_swap_alpha(png_ptr); #endif #ifdef PNG_WRITE_FILLER_SUPPORTED /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */ if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); #endif #ifdef PNG_WRITE_BGR_SUPPORTED /* Flip BGR pixels to RGB */ if (transforms & PNG_TRANSFORM_BGR) png_set_bgr(png_ptr); #endif #ifdef PNG_WRITE_SWAP_SUPPORTED /* Swap bytes of 16-bit files to most significant byte first */ if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) png_set_swap(png_ptr); #endif #ifdef PNG_WRITE_PACKSWAP_SUPPORTED /* Swap bits of 1, 2, 4 bit packed pixel formats */ if (transforms & PNG_TRANSFORM_PACKSWAP) png_set_packswap(png_ptr); #endif #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel from opacity to transparency */ if (transforms & PNG_TRANSFORM_INVERT_ALPHA) png_set_invert_alpha(png_ptr); #endif /* ----------------------- end of transformations ------------------- */ /* Write the bits */ if (info_ptr->valid & PNG_INFO_IDAT) png_write_image(png_ptr, info_ptr->row_pointers); /* It is REQUIRED to call this to finish writing the rest of the file */ png_write_end(png_ptr, info_ptr); transforms = transforms; /* Quiet compiler warnings */ params = params; } #endif #endif /* PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/CMakeLists.txt0000664000175000017500000001672212013417376017530 0ustar alfalf00000000000000cmake_minimum_required(VERSION 2.4.3) set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) if(UNIX AND NOT DEFINED CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.") endif() project(libpng C) enable_testing() # Copyright (C) 2007-2010 Glenn Randers-Pehrson # This code is released under the libpng license. # For conditions of distribution and use, see the disclaimer # and license in png.h set(PNGLIB_MAJOR 1) set(PNGLIB_MINOR 2) set(PNGLIB_RELEASE 46) set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR}) set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE}) # needed packages find_package(ZLIB REQUIRED) include_directories(${ZLIB_INCLUDE_DIR}) if(NOT WIN32) find_library(M_LIBRARY NAMES m PATHS /usr/lib /usr/local/lib ) if(NOT M_LIBRARY) message(STATUS "math library 'libm' not found - floating point support disabled") endif() else() # not needed on windows set(M_LIBRARY "") endif() # COMMAND LINE OPTIONS if(DEFINED PNG_SHARED) option(PNG_SHARED "Build shared lib" ${PNG_SHARED}) else() option(PNG_SHARED "Build shared lib" ON) endif() if(DEFINED PNG_STATIC) option(PNG_STATIC "Build static lib" ${PNG_STATIC}) else() option(PNG_STATIC "Build static lib" ON) endif() if(MINGW) option(PNG_TESTS "Build pngtest" NO) else() option(PNG_TESTS "Build pngtest" YES) endif() option(PNG_NO_CONSOLE_IO "FIXME" YES) option(PNG_NO_STDIO "FIXME" YES) option(PNG_DEBUG "Build with debug output" NO) option(PNGARG "FIXME" YES) #TODO: # PNG_CONSOLE_IO_SUPPORTED # maybe needs improving, but currently I don't know when we can enable what :) set(png_asm_tmp "OFF") if(NOT WIN32) find_program(uname_executable NAMES uname PATHS /bin /usr/bin /usr/local/bin) if(uname_executable) exec_program(${uname_executable} ARGS --machine OUTPUT_VARIABLE uname_output) if("uname_output" MATCHES "^.*i[1-9]86.*$") set(png_asm_tmp "ON") else("uname_output" MATCHES "^.*i[1-9]86.*$") set(png_asm_tmp "OFF") endif("uname_output" MATCHES "^.*i[1-9]86.*$") endif(uname_executable) else() # this env var is normally only set on win64 set(TEXT "ProgramFiles(x86)") if("$ENV{${TEXT}}" STREQUAL "") set(png_asm_tmp "ON") endif("$ENV{${TEXT}}" STREQUAL "") endif() # SET LIBNAME set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR}) # to distinguish between debug and release lib set(CMAKE_DEBUG_POSTFIX "d") # OUR SOURCES set(libpng_sources png.h pngconf.h png.c pngerror.c pngget.c pngmem.c pngpread.c pngread.c pngrio.c pngrtran.c pngrutil.c pngset.c pngtrans.c pngwio.c pngwrite.c pngwtran.c pngwutil.c ) set(pngtest_sources pngtest.c ) # SOME NEEDED DEFINITIONS add_definitions(-DPNG_CONFIGURE_LIBPNG) if(_AIX) add_definitions(-D_ALL_SOURCE) endif(_AIX) if(MSVC) add_definitions(-DPNG_NO_MODULEDEF -D_CRT_SECURE_NO_DEPRECATE) endif(MSVC) if(PNG_SHARED OR NOT MSVC) #if building msvc static this has NOT to be defined add_definitions(-DZLIB_DLL) endif() add_definitions(-DLIBPNG_NO_MMX) add_definitions(-DPNG_NO_MMX_CODE) if(PNG_CONSOLE_IO_SUPPORTED) add_definitions(-DPNG_CONSOLE_IO_SUPPORTED) endif() if(PNG_NO_CONSOLE_IO) add_definitions(-DPNG_NO_CONSOLE_IO) endif() if(PNG_NO_STDIO) add_definitions(-DPNG_NO_STDIO) endif() if(PNG_DEBUG) add_definitions(-DPNG_DEBUG) endif() if(NOT M_LIBRARY AND NOT WIN32) add_definitions(-DPNG_NO_FLOATING_POINT_SUPPORTED) endif() # NOW BUILD OUR TARGET include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR}) if(PNG_SHARED) add_library(${PNG_LIB_NAME} SHARED ${libpng_sources}) if(MSVC) # msvc does not append 'lib' - do it here to have consistent name set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib") endif() target_link_libraries(${PNG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY}) endif() if(PNG_STATIC) # does not work without changing name set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static) add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources}) if(MSVC) # msvc does not append 'lib' - do it here to have consistent name set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib") endif() endif() if(PNG_SHARED AND WIN32) set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL) endif() if(PNG_TESTS AND PNG_SHARED) # does not work with msvc due to png_lib_ver issue add_executable(pngtest ${pngtest_sources}) target_link_libraries(pngtest ${PNG_LIB_NAME}) add_test(pngtest pngtest ${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png) endif() # CREATE PKGCONFIG FILES # we use the same files like ./configure, so we have to set its vars set(prefix ${CMAKE_INSTALL_PREFIX}) set(exec_prefix ${CMAKE_INSTALL_PREFIX}) set(libdir ${CMAKE_INSTALL_PREFIX}/lib) set(includedir ${CMAKE_INSTALL_PREFIX}/include) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/libpng.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/libpng-config.in ${CMAKE_CURRENT_BINARY_DIR}/libpng-config) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/libpng.pc.in ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/libpng-config.in ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config) # SET UP LINKS if(PNG_SHARED) set_target_properties(${PNG_LIB_NAME} PROPERTIES # VERSION 0.${PNGLIB_RELEASE}.1.2.46 VERSION 0.${PNGLIB_RELEASE}.0 SOVERSION 0 CLEAN_DIRECT_OUTPUT 1) endif() if(PNG_STATIC) if(NOT WIN32) # that's uncool on win32 - it overwrites our static import lib... set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES OUTPUT_NAME ${PNG_LIB_NAME} CLEAN_DIRECT_OUTPUT 1) endif() endif() # INSTALL if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) if(PNG_SHARED) install(TARGETS ${PNG_LIB_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) endif() if(PNG_STATIC) install(TARGETS ${PNG_LIB_NAME_STATIC} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) endif() endif() if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL ) install(FILES png.h pngconf.h DESTINATION include) install(FILES png.h pngconf.h DESTINATION include/${PNGLIB_NAME}) endif() if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL ) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config DESTINATION bin) endif() if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL ) # Install man pages install(FILES libpng.3 libpngpf.3 DESTINATION man/man3) install(FILES png.5 DESTINATION man/man5) # Install pkg-config files install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc DESTINATION lib/pkgconfig) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc DESTINATION lib/pkgconfig) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config DESTINATION bin) endif() # what's with libpng.txt and all the extra files? # UNINSTALL # do we need this? # DIST # do we need this? # to create msvc import lib for mingw compiled shared lib # pexports libpng.dll > libpng.def # lib /def:libpng.def /machine:x86 glmark2-2012.08/./src/libpng/pngvcrd.c0000664000175000017500000000006012013417376016563 0ustar alfalf00000000000000/* pnggvrd.c was removed from libpng-1.2.20. */ glmark2-2012.08/./src/libpng/pngwutil.c0000664000175000017500000024620612013417376017007 0ustar alfalf00000000000000 /* pngwutil.c - utilities to write a PNG file * * Last changed in libpng 1.2.43 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_WRITE_SUPPORTED /* Place a 32-bit number into a buffer in PNG byte order. We work * with unsigned numbers for convenience, although one supported * ancillary chunk uses signed (two's complement) numbers. */ void PNGAPI png_save_uint_32(png_bytep buf, png_uint_32 i) { buf[0] = (png_byte)((i >> 24) & 0xff); buf[1] = (png_byte)((i >> 16) & 0xff); buf[2] = (png_byte)((i >> 8) & 0xff); buf[3] = (png_byte)(i & 0xff); } /* The png_save_int_32 function assumes integers are stored in two's * complement format. If this isn't the case, then this routine needs to * be modified to write data in two's complement format. */ void PNGAPI png_save_int_32(png_bytep buf, png_int_32 i) { buf[0] = (png_byte)((i >> 24) & 0xff); buf[1] = (png_byte)((i >> 16) & 0xff); buf[2] = (png_byte)((i >> 8) & 0xff); buf[3] = (png_byte)(i & 0xff); } /* Place a 16-bit number into a buffer in PNG byte order. * The parameter is declared unsigned int, not png_uint_16, * just to avoid potential problems on pre-ANSI C compilers. */ void PNGAPI png_save_uint_16(png_bytep buf, unsigned int i) { buf[0] = (png_byte)((i >> 8) & 0xff); buf[1] = (png_byte)(i & 0xff); } /* Simple function to write the signature. If we have already written * the magic bytes of the signature, or more likely, the PNG stream is * being embedded into another stream and doesn't need its own signature, * we should call png_set_sig_bytes() to tell libpng how many of the * bytes have already been written. */ void /* PRIVATE */ png_write_sig(png_structp png_ptr) { png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; /* Write the rest of the 8 byte signature */ png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], (png_size_t)(8 - png_ptr->sig_bytes)); if (png_ptr->sig_bytes < 3) png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; } /* Write a PNG chunk all at once. The type is an array of ASCII characters * representing the chunk name. The array must be at least 4 bytes in * length, and does not need to be null terminated. To be safe, pass the * pre-defined chunk names here, and if you need a new one, define it * where the others are defined. The length is the length of the data. * All the data must be present. If that is not possible, use the * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() * functions instead. */ void PNGAPI png_write_chunk(png_structp png_ptr, png_bytep chunk_name, png_bytep data, png_size_t length) { if (png_ptr == NULL) return; png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length); png_write_chunk_data(png_ptr, data, (png_size_t)length); png_write_chunk_end(png_ptr); } /* Write the start of a PNG chunk. The type is the chunk type. * The total_length is the sum of the lengths of all the data you will be * passing in png_write_chunk_data(). */ void PNGAPI png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, png_uint_32 length) { png_byte buf[8]; png_debug2(0, "Writing %s chunk, length = %lu", chunk_name, (unsigned long)length); if (png_ptr == NULL) return; /* Write the length and the chunk name */ png_save_uint_32(buf, length); png_memcpy(buf + 4, chunk_name, 4); png_write_data(png_ptr, buf, (png_size_t)8); /* Put the chunk name into png_ptr->chunk_name */ png_memcpy(png_ptr->chunk_name, chunk_name, 4); /* Reset the crc and run it over the chunk name */ png_reset_crc(png_ptr); png_calculate_crc(png_ptr, chunk_name, (png_size_t)4); } /* Write the data of a PNG chunk started with png_write_chunk_start(). * Note that multiple calls to this function are allowed, and that the * sum of the lengths from these calls *must* add up to the total_length * given to png_write_chunk_start(). */ void PNGAPI png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length) { /* Write the data, and run the CRC over it */ if (png_ptr == NULL) return; if (data != NULL && length > 0) { png_write_data(png_ptr, data, length); /* Update the CRC after writing the data, * in case that the user I/O routine alters it. */ png_calculate_crc(png_ptr, data, length); } } /* Finish a chunk started with png_write_chunk_start(). */ void PNGAPI png_write_chunk_end(png_structp png_ptr) { png_byte buf[4]; if (png_ptr == NULL) return; /* Write the crc in a single operation */ png_save_uint_32(buf, png_ptr->crc); png_write_data(png_ptr, buf, (png_size_t)4); } #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED) /* This pair of functions encapsulates the operation of (a) compressing a * text string, and (b) issuing it later as a series of chunk data writes. * The compression_state structure is shared context for these functions * set up by the caller in order to make the whole mess thread-safe. */ typedef struct { char *input; /* The uncompressed input data */ int input_len; /* Its length */ int num_output_ptr; /* Number of output pointers used */ int max_output_ptr; /* Size of output_ptr */ png_charpp output_ptr; /* Array of pointers to output */ } compression_state; /* Compress given text into storage in the png_ptr structure */ static int /* PRIVATE */ png_text_compress(png_structp png_ptr, png_charp text, png_size_t text_len, int compression, compression_state *comp) { int ret; comp->num_output_ptr = 0; comp->max_output_ptr = 0; comp->output_ptr = NULL; comp->input = NULL; comp->input_len = 0; /* We may just want to pass the text right through */ if (compression == PNG_TEXT_COMPRESSION_NONE) { comp->input = text; comp->input_len = text_len; return((int)text_len); } if (compression >= PNG_TEXT_COMPRESSION_LAST) { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char msg[50]; png_snprintf(msg, 50, "Unknown compression type %d", compression); png_warning(png_ptr, msg); #else png_warning(png_ptr, "Unknown compression type"); #endif } /* We can't write the chunk until we find out how much data we have, * which means we need to run the compressor first and save the * output. This shouldn't be a problem, as the vast majority of * comments should be reasonable, but we will set up an array of * malloc'd pointers to be sure. * * If we knew the application was well behaved, we could simplify this * greatly by assuming we can always malloc an output buffer large * enough to hold the compressed text ((1001 * text_len / 1000) + 12) * and malloc this directly. The only time this would be a bad idea is * if we can't malloc more than 64K and we have 64K of random input * data, or if the input string is incredibly large (although this * wouldn't cause a failure, just a slowdown due to swapping). */ /* Set up the compression buffers */ png_ptr->zstream.avail_in = (uInt)text_len; png_ptr->zstream.next_in = (Bytef *)text; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf; /* This is the same compression loop as in png_write_row() */ do { /* Compress the data */ ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); if (ret != Z_OK) { /* Error */ if (png_ptr->zstream.msg != NULL) png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* Check to see if we need more room */ if (!(png_ptr->zstream.avail_out)) { /* Make sure the output array has room */ if (comp->num_output_ptr >= comp->max_output_ptr) { int old_max; old_max = comp->max_output_ptr; comp->max_output_ptr = comp->num_output_ptr + 4; if (comp->output_ptr != NULL) { png_charpp old_ptr; old_ptr = comp->output_ptr; comp->output_ptr = (png_charpp)png_malloc(png_ptr, (png_uint_32) (comp->max_output_ptr * png_sizeof(png_charpp))); png_memcpy(comp->output_ptr, old_ptr, old_max * png_sizeof(png_charp)); png_free(png_ptr, old_ptr); } else comp->output_ptr = (png_charpp)png_malloc(png_ptr, (png_uint_32) (comp->max_output_ptr * png_sizeof(png_charp))); } /* Save the data */ comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, png_ptr->zbuf_size); comp->num_output_ptr++; /* and reset the buffer */ png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_out = png_ptr->zbuf; } /* Continue until we don't have any more to compress */ } while (png_ptr->zstream.avail_in); /* Finish the compression */ do { /* Tell zlib we are finished */ ret = deflate(&png_ptr->zstream, Z_FINISH); if (ret == Z_OK) { /* Check to see if we need more room */ if (!(png_ptr->zstream.avail_out)) { /* Check to make sure our output array has room */ if (comp->num_output_ptr >= comp->max_output_ptr) { int old_max; old_max = comp->max_output_ptr; comp->max_output_ptr = comp->num_output_ptr + 4; if (comp->output_ptr != NULL) { png_charpp old_ptr; old_ptr = comp->output_ptr; /* This could be optimized to realloc() */ comp->output_ptr = (png_charpp)png_malloc(png_ptr, (png_uint_32)(comp->max_output_ptr * png_sizeof(png_charp))); png_memcpy(comp->output_ptr, old_ptr, old_max * png_sizeof(png_charp)); png_free(png_ptr, old_ptr); } else comp->output_ptr = (png_charpp)png_malloc(png_ptr, (png_uint_32)(comp->max_output_ptr * png_sizeof(png_charp))); } /* Save the data */ comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, png_ptr->zbuf_size); comp->num_output_ptr++; /* and reset the buffer pointers */ png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_out = png_ptr->zbuf; } } else if (ret != Z_STREAM_END) { /* We got an error */ if (png_ptr->zstream.msg != NULL) png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } } while (ret != Z_STREAM_END); /* Text length is number of buffers plus last buffer */ text_len = png_ptr->zbuf_size * comp->num_output_ptr; if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; return((int)text_len); } /* Ship the compressed text out via chunk writes */ static void /* PRIVATE */ png_write_compressed_data_out(png_structp png_ptr, compression_state *comp) { int i; /* Handle the no-compression case */ if (comp->input) { png_write_chunk_data(png_ptr, (png_bytep)comp->input, (png_size_t)comp->input_len); return; } /* Write saved output buffers, if any */ for (i = 0; i < comp->num_output_ptr; i++) { png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i], (png_size_t)png_ptr->zbuf_size); png_free(png_ptr, comp->output_ptr[i]); comp->output_ptr[i]=NULL; } if (comp->max_output_ptr != 0) png_free(png_ptr, comp->output_ptr); comp->output_ptr=NULL; /* Write anything left in zbuf */ if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) png_write_chunk_data(png_ptr, png_ptr->zbuf, (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); /* Reset zlib for another zTXt/iTXt or image data */ deflateReset(&png_ptr->zstream); png_ptr->zstream.data_type = Z_BINARY; } #endif /* Write the IHDR chunk, and update the png_struct with the necessary * information. Note that the rest of this code depends upon this * information being correct. */ void /* PRIVATE */ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_type, int filter_type, int interlace_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_IHDR; #endif int ret; png_byte buf[13]; /* Buffer to store the IHDR info */ png_debug(1, "in png_write_IHDR"); /* Check that we have valid input data from the application info */ switch (color_type) { case PNG_COLOR_TYPE_GRAY: switch (bit_depth) { case 1: case 2: case 4: case 8: case 16: png_ptr->channels = 1; break; default: png_error(png_ptr, "Invalid bit depth for grayscale image"); } break; case PNG_COLOR_TYPE_RGB: if (bit_depth != 8 && bit_depth != 16) png_error(png_ptr, "Invalid bit depth for RGB image"); png_ptr->channels = 3; break; case PNG_COLOR_TYPE_PALETTE: switch (bit_depth) { case 1: case 2: case 4: case 8: png_ptr->channels = 1; break; default: png_error(png_ptr, "Invalid bit depth for paletted image"); } break; case PNG_COLOR_TYPE_GRAY_ALPHA: if (bit_depth != 8 && bit_depth != 16) png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); png_ptr->channels = 2; break; case PNG_COLOR_TYPE_RGB_ALPHA: if (bit_depth != 8 && bit_depth != 16) png_error(png_ptr, "Invalid bit depth for RGBA image"); png_ptr->channels = 4; break; default: png_error(png_ptr, "Invalid image color type specified"); } if (compression_type != PNG_COMPRESSION_TYPE_BASE) { png_warning(png_ptr, "Invalid compression type specified"); compression_type = PNG_COMPRESSION_TYPE_BASE; } /* Write filter_method 64 (intrapixel differencing) only if * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and * 2. Libpng did not write a PNG signature (this filter_method is only * used in PNG datastreams that are embedded in MNG datastreams) and * 3. The application called png_permit_mng_features with a mask that * included PNG_FLAG_MNG_FILTER_64 and * 4. The filter_method is 64 and * 5. The color_type is RGB or RGBA */ if ( #ifdef PNG_MNG_FEATURES_SUPPORTED !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) && (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && #endif filter_type != PNG_FILTER_TYPE_BASE) { png_warning(png_ptr, "Invalid filter type specified"); filter_type = PNG_FILTER_TYPE_BASE; } #ifdef PNG_WRITE_INTERLACING_SUPPORTED if (interlace_type != PNG_INTERLACE_NONE && interlace_type != PNG_INTERLACE_ADAM7) { png_warning(png_ptr, "Invalid interlace type specified"); interlace_type = PNG_INTERLACE_ADAM7; } #else interlace_type=PNG_INTERLACE_NONE; #endif /* Save the relevent information */ png_ptr->bit_depth = (png_byte)bit_depth; png_ptr->color_type = (png_byte)color_type; png_ptr->interlaced = (png_byte)interlace_type; #ifdef PNG_MNG_FEATURES_SUPPORTED png_ptr->filter_type = (png_byte)filter_type; #endif png_ptr->compression_type = (png_byte)compression_type; png_ptr->width = width; png_ptr->height = height; png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); /* Set the usr info, so any transformations can modify it */ png_ptr->usr_width = png_ptr->width; png_ptr->usr_bit_depth = png_ptr->bit_depth; png_ptr->usr_channels = png_ptr->channels; /* Pack the header information into the buffer */ png_save_uint_32(buf, width); png_save_uint_32(buf + 4, height); buf[8] = (png_byte)bit_depth; buf[9] = (png_byte)color_type; buf[10] = (png_byte)compression_type; buf[11] = (png_byte)filter_type; buf[12] = (png_byte)interlace_type; /* Write the chunk */ png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13); /* Initialize zlib with PNG info */ png_ptr->zstream.zalloc = png_zalloc; png_ptr->zstream.zfree = png_zfree; png_ptr->zstream.opaque = (voidpf)png_ptr; if (!(png_ptr->do_filter)) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || png_ptr->bit_depth < 8) png_ptr->do_filter = PNG_FILTER_NONE; else png_ptr->do_filter = PNG_ALL_FILTERS; } if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) { if (png_ptr->do_filter != PNG_FILTER_NONE) png_ptr->zlib_strategy = Z_FILTERED; else png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; } if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) png_ptr->zlib_mem_level = 8; if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) png_ptr->zlib_window_bits = 15; if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) png_ptr->zlib_method = 8; ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, png_ptr->zlib_method, png_ptr->zlib_window_bits, png_ptr->zlib_mem_level, png_ptr->zlib_strategy); if (ret != Z_OK) { if (ret == Z_VERSION_ERROR) png_error(png_ptr, "zlib failed to initialize compressor -- version error"); if (ret == Z_STREAM_ERROR) png_error(png_ptr, "zlib failed to initialize compressor -- stream error"); if (ret == Z_MEM_ERROR) png_error(png_ptr, "zlib failed to initialize compressor -- mem error"); png_error(png_ptr, "zlib failed to initialize compressor"); } png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; /* libpng is not interested in zstream.data_type */ /* Set it to a predefined value, to avoid its evaluation inside zlib */ png_ptr->zstream.data_type = Z_BINARY; png_ptr->mode = PNG_HAVE_IHDR; } /* Write the palette. We are careful not to trust png_color to be in the * correct order for PNG, so people can redefine it to any convenient * structure. */ void /* PRIVATE */ png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_PLTE; #endif png_uint_32 i; png_colorp pal_ptr; png_byte buf[3]; png_debug(1, "in png_write_PLTE"); if (( #ifdef PNG_MNG_FEATURES_SUPPORTED !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && #endif num_pal == 0) || num_pal > 256) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { png_error(png_ptr, "Invalid number of colors in palette"); } else { png_warning(png_ptr, "Invalid number of colors in palette"); return; } } if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) { png_warning(png_ptr, "Ignoring request to write a PLTE chunk in grayscale PNG"); return; } png_ptr->num_palette = (png_uint_16)num_pal; png_debug1(3, "num_palette = %d", png_ptr->num_palette); png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, (png_uint_32)(num_pal * 3)); #ifdef PNG_POINTER_INDEXING_SUPPORTED for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) { buf[0] = pal_ptr->red; buf[1] = pal_ptr->green; buf[2] = pal_ptr->blue; png_write_chunk_data(png_ptr, buf, (png_size_t)3); } #else /* This is a little slower but some buggy compilers need to do this * instead */ pal_ptr=palette; for (i = 0; i < num_pal; i++) { buf[0] = pal_ptr[i].red; buf[1] = pal_ptr[i].green; buf[2] = pal_ptr[i].blue; png_write_chunk_data(png_ptr, buf, (png_size_t)3); } #endif png_write_chunk_end(png_ptr); png_ptr->mode |= PNG_HAVE_PLTE; } /* Write an IDAT chunk */ void /* PRIVATE */ png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_IDAT; #endif png_debug(1, "in png_write_IDAT"); /* Optimize the CMF field in the zlib stream. */ /* This hack of the zlib stream is compliant to the stream specification. */ if (!(png_ptr->mode & PNG_HAVE_IDAT) && png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) { unsigned int z_cmf = data[0]; /* zlib compression method and flags */ if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) { /* Avoid memory underflows and multiplication overflows. * * The conditions below are practically always satisfied; * however, they still must be checked. */ if (length >= 2 && png_ptr->height < 16384 && png_ptr->width < 16384) { png_uint_32 uncompressed_idat_size = png_ptr->height * ((png_ptr->width * png_ptr->channels * png_ptr->bit_depth + 15) >> 3); unsigned int z_cinfo = z_cmf >> 4; unsigned int half_z_window_size = 1 << (z_cinfo + 7); while (uncompressed_idat_size <= half_z_window_size && half_z_window_size >= 256) { z_cinfo--; half_z_window_size >>= 1; } z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); if (data[0] != (png_byte)z_cmf) { data[0] = (png_byte)z_cmf; data[1] &= 0xe0; data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f); } } } else png_error(png_ptr, "Invalid zlib compression method or flags in IDAT"); } png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length); png_ptr->mode |= PNG_HAVE_IDAT; } /* Write an IEND chunk */ void /* PRIVATE */ png_write_IEND(png_structp png_ptr) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_IEND; #endif png_debug(1, "in png_write_IEND"); png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL, (png_size_t)0); png_ptr->mode |= PNG_HAVE_IEND; } #ifdef PNG_WRITE_gAMA_SUPPORTED /* Write a gAMA chunk */ #ifdef PNG_FLOATING_POINT_SUPPORTED void /* PRIVATE */ png_write_gAMA(png_structp png_ptr, double file_gamma) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_gAMA; #endif png_uint_32 igamma; png_byte buf[4]; png_debug(1, "in png_write_gAMA"); /* file_gamma is saved in 1/100,000ths */ igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5); png_save_uint_32(buf, igamma); png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); } #endif #ifdef PNG_FIXED_POINT_SUPPORTED void /* PRIVATE */ png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_gAMA; #endif png_byte buf[4]; png_debug(1, "in png_write_gAMA"); /* file_gamma is saved in 1/100,000ths */ png_save_uint_32(buf, (png_uint_32)file_gamma); png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); } #endif #endif #ifdef PNG_WRITE_sRGB_SUPPORTED /* Write a sRGB chunk */ void /* PRIVATE */ png_write_sRGB(png_structp png_ptr, int srgb_intent) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_sRGB; #endif png_byte buf[1]; png_debug(1, "in png_write_sRGB"); if (srgb_intent >= PNG_sRGB_INTENT_LAST) png_warning(png_ptr, "Invalid sRGB rendering intent specified"); buf[0]=(png_byte)srgb_intent; png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1); } #endif #ifdef PNG_WRITE_iCCP_SUPPORTED /* Write an iCCP chunk */ void /* PRIVATE */ png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type, png_charp profile, int profile_len) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_iCCP; #endif png_size_t name_len; png_charp new_name; compression_state comp; int embedded_profile_len = 0; png_debug(1, "in png_write_iCCP"); comp.num_output_ptr = 0; comp.max_output_ptr = 0; comp.output_ptr = NULL; comp.input = NULL; comp.input_len = 0; if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0) return; if (compression_type != PNG_COMPRESSION_TYPE_BASE) png_warning(png_ptr, "Unknown compression type in iCCP chunk"); if (profile == NULL) profile_len = 0; if (profile_len > 3) embedded_profile_len = ((*( (png_bytep)profile ))<<24) | ((*( (png_bytep)profile + 1))<<16) | ((*( (png_bytep)profile + 2))<< 8) | ((*( (png_bytep)profile + 3)) ); if (embedded_profile_len < 0) { png_warning(png_ptr, "Embedded profile length in iCCP chunk is negative"); png_free(png_ptr, new_name); return; } if (profile_len < embedded_profile_len) { png_warning(png_ptr, "Embedded profile length too large in iCCP chunk"); png_free(png_ptr, new_name); return; } if (profile_len > embedded_profile_len) { png_warning(png_ptr, "Truncating profile to actual length in iCCP chunk"); profile_len = embedded_profile_len; } if (profile_len) profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp); /* Make sure we include the NULL after the name and the compression type */ png_write_chunk_start(png_ptr, (png_bytep)png_iCCP, (png_uint_32)(name_len + profile_len + 2)); new_name[name_len + 1] = 0x00; png_write_chunk_data(png_ptr, (png_bytep)new_name, (png_size_t)(name_len + 2)); if (profile_len) png_write_compressed_data_out(png_ptr, &comp); png_write_chunk_end(png_ptr); png_free(png_ptr, new_name); } #endif #ifdef PNG_WRITE_sPLT_SUPPORTED /* Write a sPLT chunk */ void /* PRIVATE */ png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_sPLT; #endif png_size_t name_len; png_charp new_name; png_byte entrybuf[10]; int entry_size = (spalette->depth == 8 ? 6 : 10); int palette_size = entry_size * spalette->nentries; png_sPLT_entryp ep; #ifndef PNG_POINTER_INDEXING_SUPPORTED int i; #endif png_debug(1, "in png_write_sPLT"); if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0) return; /* Make sure we include the NULL after the name */ png_write_chunk_start(png_ptr, (png_bytep)png_sPLT, (png_uint_32)(name_len + 2 + palette_size)); png_write_chunk_data(png_ptr, (png_bytep)new_name, (png_size_t)(name_len + 1)); png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1); /* Loop through each palette entry, writing appropriately */ #ifdef PNG_POINTER_INDEXING_SUPPORTED for (ep = spalette->entries; epentries + spalette->nentries; ep++) { if (spalette->depth == 8) { entrybuf[0] = (png_byte)ep->red; entrybuf[1] = (png_byte)ep->green; entrybuf[2] = (png_byte)ep->blue; entrybuf[3] = (png_byte)ep->alpha; png_save_uint_16(entrybuf + 4, ep->frequency); } else { png_save_uint_16(entrybuf + 0, ep->red); png_save_uint_16(entrybuf + 2, ep->green); png_save_uint_16(entrybuf + 4, ep->blue); png_save_uint_16(entrybuf + 6, ep->alpha); png_save_uint_16(entrybuf + 8, ep->frequency); } png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); } #else ep=spalette->entries; for (i=0; i>spalette->nentries; i++) { if (spalette->depth == 8) { entrybuf[0] = (png_byte)ep[i].red; entrybuf[1] = (png_byte)ep[i].green; entrybuf[2] = (png_byte)ep[i].blue; entrybuf[3] = (png_byte)ep[i].alpha; png_save_uint_16(entrybuf + 4, ep[i].frequency); } else { png_save_uint_16(entrybuf + 0, ep[i].red); png_save_uint_16(entrybuf + 2, ep[i].green); png_save_uint_16(entrybuf + 4, ep[i].blue); png_save_uint_16(entrybuf + 6, ep[i].alpha); png_save_uint_16(entrybuf + 8, ep[i].frequency); } png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); } #endif png_write_chunk_end(png_ptr); png_free(png_ptr, new_name); } #endif #ifdef PNG_WRITE_sBIT_SUPPORTED /* Write the sBIT chunk */ void /* PRIVATE */ png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_sBIT; #endif png_byte buf[4]; png_size_t size; png_debug(1, "in png_write_sBIT"); /* Make sure we don't depend upon the order of PNG_COLOR_8 */ if (color_type & PNG_COLOR_MASK_COLOR) { png_byte maxbits; maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : png_ptr->usr_bit_depth); if (sbit->red == 0 || sbit->red > maxbits || sbit->green == 0 || sbit->green > maxbits || sbit->blue == 0 || sbit->blue > maxbits) { png_warning(png_ptr, "Invalid sBIT depth specified"); return; } buf[0] = sbit->red; buf[1] = sbit->green; buf[2] = sbit->blue; size = 3; } else { if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) { png_warning(png_ptr, "Invalid sBIT depth specified"); return; } buf[0] = sbit->gray; size = 1; } if (color_type & PNG_COLOR_MASK_ALPHA) { if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) { png_warning(png_ptr, "Invalid sBIT depth specified"); return; } buf[size++] = sbit->alpha; } png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size); } #endif #ifdef PNG_WRITE_cHRM_SUPPORTED /* Write the cHRM chunk */ #ifdef PNG_FLOATING_POINT_SUPPORTED void /* PRIVATE */ png_write_cHRM(png_structp png_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_cHRM; #endif png_byte buf[32]; png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y; png_debug(1, "in png_write_cHRM"); int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5); int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5); int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5); int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5); int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5); int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5); int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5); int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5); #ifdef PNG_CHECK_cHRM_SUPPORTED if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y)) #endif { /* Each value is saved in 1/100,000ths */ png_save_uint_32(buf, int_white_x); png_save_uint_32(buf + 4, int_white_y); png_save_uint_32(buf + 8, int_red_x); png_save_uint_32(buf + 12, int_red_y); png_save_uint_32(buf + 16, int_green_x); png_save_uint_32(buf + 20, int_green_y); png_save_uint_32(buf + 24, int_blue_x); png_save_uint_32(buf + 28, int_blue_y); png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); } } #endif #ifdef PNG_FIXED_POINT_SUPPORTED void /* PRIVATE */ png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_cHRM; #endif png_byte buf[32]; png_debug(1, "in png_write_cHRM"); /* Each value is saved in 1/100,000ths */ #ifdef PNG_CHECK_cHRM_SUPPORTED if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y)) #endif { png_save_uint_32(buf, (png_uint_32)white_x); png_save_uint_32(buf + 4, (png_uint_32)white_y); png_save_uint_32(buf + 8, (png_uint_32)red_x); png_save_uint_32(buf + 12, (png_uint_32)red_y); png_save_uint_32(buf + 16, (png_uint_32)green_x); png_save_uint_32(buf + 20, (png_uint_32)green_y); png_save_uint_32(buf + 24, (png_uint_32)blue_x); png_save_uint_32(buf + 28, (png_uint_32)blue_y); png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); } } #endif #endif #ifdef PNG_WRITE_tRNS_SUPPORTED /* Write the tRNS chunk */ void /* PRIVATE */ png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran, int num_trans, int color_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_tRNS; #endif png_byte buf[6]; png_debug(1, "in png_write_tRNS"); if (color_type == PNG_COLOR_TYPE_PALETTE) { if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) { png_warning(png_ptr, "Invalid number of transparent colors specified"); return; } /* Write the chunk out as it is */ png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans); } else if (color_type == PNG_COLOR_TYPE_GRAY) { /* One 16 bit value */ if (tran->gray >= (1 << png_ptr->bit_depth)) { png_warning(png_ptr, "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); return; } png_save_uint_16(buf, tran->gray); png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2); } else if (color_type == PNG_COLOR_TYPE_RGB) { /* Three 16 bit values */ png_save_uint_16(buf, tran->red); png_save_uint_16(buf + 2, tran->green); png_save_uint_16(buf + 4, tran->blue); if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) { png_warning(png_ptr, "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); return; } png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6); } else { png_warning(png_ptr, "Can't write tRNS with an alpha channel"); } } #endif #ifdef PNG_WRITE_bKGD_SUPPORTED /* Write the background chunk */ void /* PRIVATE */ png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_bKGD; #endif png_byte buf[6]; png_debug(1, "in png_write_bKGD"); if (color_type == PNG_COLOR_TYPE_PALETTE) { if ( #ifdef PNG_MNG_FEATURES_SUPPORTED (png_ptr->num_palette || (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && #endif back->index >= png_ptr->num_palette) { png_warning(png_ptr, "Invalid background palette index"); return; } buf[0] = back->index; png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1); } else if (color_type & PNG_COLOR_MASK_COLOR) { png_save_uint_16(buf, back->red); png_save_uint_16(buf + 2, back->green); png_save_uint_16(buf + 4, back->blue); if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) { png_warning(png_ptr, "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); return; } png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6); } else { if (back->gray >= (1 << png_ptr->bit_depth)) { png_warning(png_ptr, "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); return; } png_save_uint_16(buf, back->gray); png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2); } } #endif #ifdef PNG_WRITE_hIST_SUPPORTED /* Write the histogram */ void /* PRIVATE */ png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_hIST; #endif int i; png_byte buf[3]; png_debug(1, "in png_write_hIST"); if (num_hist > (int)png_ptr->num_palette) { png_debug2(3, "num_hist = %d, num_palette = %d", num_hist, png_ptr->num_palette); png_warning(png_ptr, "Invalid number of histogram entries specified"); return; } png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2)); for (i = 0; i < num_hist; i++) { png_save_uint_16(buf, hist[i]); png_write_chunk_data(png_ptr, buf, (png_size_t)2); } png_write_chunk_end(png_ptr); } #endif #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, * and if invalid, correct the keyword rather than discarding the entire * chunk. The PNG 1.0 specification requires keywords 1-79 characters in * length, forbids leading or trailing whitespace, multiple internal spaces, * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. * * The new_key is allocated to hold the corrected keyword and must be freed * by the calling routine. This avoids problems with trying to write to * static keywords without having to have duplicate copies of the strings. */ png_size_t /* PRIVATE */ png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key) { png_size_t key_len; png_charp kp, dp; int kflag; int kwarn=0; png_debug(1, "in png_check_keyword"); *new_key = NULL; if (key == NULL || (key_len = png_strlen(key)) == 0) { png_warning(png_ptr, "zero length keyword"); return ((png_size_t)0); } png_debug1(2, "Keyword to be checked is '%s'", key); *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); if (*new_key == NULL) { png_warning(png_ptr, "Out of memory while procesing keyword"); return ((png_size_t)0); } /* Replace non-printing characters with a blank and print a warning */ for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++) { if ((png_byte)*kp < 0x20 || ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1)) { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char msg[40]; png_snprintf(msg, 40, "invalid keyword character 0x%02X", (png_byte)*kp); png_warning(png_ptr, msg); #else png_warning(png_ptr, "invalid character in keyword"); #endif *dp = ' '; } else { *dp = *kp; } } *dp = '\0'; /* Remove any trailing white space. */ kp = *new_key + key_len - 1; if (*kp == ' ') { png_warning(png_ptr, "trailing spaces removed from keyword"); while (*kp == ' ') { *(kp--) = '\0'; key_len--; } } /* Remove any leading white space. */ kp = *new_key; if (*kp == ' ') { png_warning(png_ptr, "leading spaces removed from keyword"); while (*kp == ' ') { kp++; key_len--; } } png_debug1(2, "Checking for multiple internal spaces in '%s'", kp); /* Remove multiple internal spaces. */ for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) { if (*kp == ' ' && kflag == 0) { *(dp++) = *kp; kflag = 1; } else if (*kp == ' ') { key_len--; kwarn=1; } else { *(dp++) = *kp; kflag = 0; } } *dp = '\0'; if (kwarn) png_warning(png_ptr, "extra interior spaces removed from keyword"); if (key_len == 0) { png_free(png_ptr, *new_key); *new_key=NULL; png_warning(png_ptr, "Zero length keyword"); } if (key_len > 79) { png_warning(png_ptr, "keyword length must be 1 - 79 characters"); (*new_key)[79] = '\0'; key_len = 79; } return (key_len); } #endif #ifdef PNG_WRITE_tEXt_SUPPORTED /* Write a tEXt chunk */ void /* PRIVATE */ png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text, png_size_t text_len) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_tEXt; #endif png_size_t key_len; png_charp new_key; png_debug(1, "in png_write_tEXt"); if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) return; if (text == NULL || *text == '\0') text_len = 0; else text_len = png_strlen(text); /* Make sure we include the 0 after the key */ png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)(key_len + text_len + 1)); /* * We leave it to the application to meet PNG-1.0 requirements on the * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. */ png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1)); if (text_len) png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len); png_write_chunk_end(png_ptr); png_free(png_ptr, new_key); } #endif #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write a compressed text chunk */ void /* PRIVATE */ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, png_size_t text_len, int compression) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_zTXt; #endif png_size_t key_len; char buf[1]; png_charp new_key; compression_state comp; png_debug(1, "in png_write_zTXt"); comp.num_output_ptr = 0; comp.max_output_ptr = 0; comp.output_ptr = NULL; comp.input = NULL; comp.input_len = 0; if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) { png_free(png_ptr, new_key); return; } if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) { png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); png_free(png_ptr, new_key); return; } text_len = png_strlen(text); /* Compute the compressed data; do it now for the length */ text_len = png_text_compress(png_ptr, text, text_len, compression, &comp); /* Write start of chunk */ png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)(key_len+text_len + 2)); /* Write key */ png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1)); png_free(png_ptr, new_key); buf[0] = (png_byte)compression; /* Write compression */ png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1); /* Write the compressed data */ png_write_compressed_data_out(png_ptr, &comp); /* Close the chunk */ png_write_chunk_end(png_ptr); } #endif #ifdef PNG_WRITE_iTXt_SUPPORTED /* Write an iTXt chunk */ void /* PRIVATE */ png_write_iTXt(png_structp png_ptr, int compression, png_charp key, png_charp lang, png_charp lang_key, png_charp text) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_iTXt; #endif png_size_t lang_len, key_len, lang_key_len, text_len; png_charp new_lang; png_charp new_key = NULL; png_byte cbuf[2]; compression_state comp; png_debug(1, "in png_write_iTXt"); comp.num_output_ptr = 0; comp.max_output_ptr = 0; comp.output_ptr = NULL; comp.input = NULL; if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) return; if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0) { png_warning(png_ptr, "Empty language field in iTXt chunk"); new_lang = NULL; lang_len = 0; } if (lang_key == NULL) lang_key_len = 0; else lang_key_len = png_strlen(lang_key); if (text == NULL) text_len = 0; else text_len = png_strlen(text); /* Compute the compressed data; do it now for the length */ text_len = png_text_compress(png_ptr, text, text_len, compression-2, &comp); /* Make sure we include the compression flag, the compression byte, * and the NULs after the key, lang, and lang_key parts */ png_write_chunk_start(png_ptr, (png_bytep)png_iTXt, (png_uint_32)( 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ + key_len + lang_len + lang_key_len + text_len)); /* We leave it to the application to meet PNG-1.0 requirements on the * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. */ png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1)); /* Set the compression flag */ if (compression == PNG_ITXT_COMPRESSION_NONE || \ compression == PNG_TEXT_COMPRESSION_NONE) cbuf[0] = 0; else /* compression == PNG_ITXT_COMPRESSION_zTXt */ cbuf[0] = 1; /* Set the compression method */ cbuf[1] = 0; png_write_chunk_data(png_ptr, cbuf, (png_size_t)2); cbuf[0] = 0; png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), (png_size_t)(lang_len + 1)); png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), (png_size_t)(lang_key_len + 1)); png_write_compressed_data_out(png_ptr, &comp); png_write_chunk_end(png_ptr); png_free(png_ptr, new_key); png_free(png_ptr, new_lang); } #endif #ifdef PNG_WRITE_oFFs_SUPPORTED /* Write the oFFs chunk */ void /* PRIVATE */ png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, int unit_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_oFFs; #endif png_byte buf[9]; png_debug(1, "in png_write_oFFs"); if (unit_type >= PNG_OFFSET_LAST) png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); png_save_int_32(buf, x_offset); png_save_int_32(buf + 4, y_offset); buf[8] = (png_byte)unit_type; png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9); } #endif #ifdef PNG_WRITE_pCAL_SUPPORTED /* Write the pCAL chunk (described in the PNG extensions document) */ void /* PRIVATE */ png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_pCAL; #endif png_size_t purpose_len, units_len, total_len; png_uint_32p params_len; png_byte buf[10]; png_charp new_purpose; int i; png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); if (type >= PNG_EQUATION_LAST) png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); png_debug1(3, "pCAL units length = %d", (int)units_len); total_len = purpose_len + units_len + 10; params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams * png_sizeof(png_uint_32))); /* Find the length of each parameter, making sure we don't count the null terminator for the last parameter. */ for (i = 0; i < nparams; i++) { params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); png_debug2(3, "pCAL parameter %d length = %lu", i, (unsigned long) params_len[i]); total_len += (png_size_t)params_len[i]; } png_debug1(3, "pCAL total length = %d", (int)total_len); png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len); png_write_chunk_data(png_ptr, (png_bytep)new_purpose, (png_size_t)purpose_len); png_save_int_32(buf, X0); png_save_int_32(buf + 4, X1); buf[8] = (png_byte)type; buf[9] = (png_byte)nparams; png_write_chunk_data(png_ptr, buf, (png_size_t)10); png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len); png_free(png_ptr, new_purpose); for (i = 0; i < nparams; i++) { png_write_chunk_data(png_ptr, (png_bytep)params[i], (png_size_t)params_len[i]); } png_free(png_ptr, params_len); png_write_chunk_end(png_ptr); } #endif #ifdef PNG_WRITE_sCAL_SUPPORTED /* Write the sCAL chunk */ #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) void /* PRIVATE */ png_write_sCAL(png_structp png_ptr, int unit, double width, double height) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_sCAL; #endif char buf[64]; png_size_t total_len; png_debug(1, "in png_write_sCAL"); buf[0] = (char)unit; #ifdef _WIN32_WCE /* sprintf() function is not supported on WindowsCE */ { wchar_t wc_buf[32]; size_t wc_len; swprintf(wc_buf, TEXT("%12.12e"), width); wc_len = wcslen(wc_buf); WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL); total_len = wc_len + 2; swprintf(wc_buf, TEXT("%12.12e"), height); wc_len = wcslen(wc_buf); WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len, NULL, NULL); total_len += wc_len; } #else png_snprintf(buf + 1, 63, "%12.12e", width); total_len = 1 + png_strlen(buf + 1) + 1; png_snprintf(buf + total_len, 64-total_len, "%12.12e", height); total_len += png_strlen(buf + total_len); #endif png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len); } #else #ifdef PNG_FIXED_POINT_SUPPORTED void /* PRIVATE */ png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width, png_charp height) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_sCAL; #endif png_byte buf[64]; png_size_t wlen, hlen, total_len; png_debug(1, "in png_write_sCAL_s"); wlen = png_strlen(width); hlen = png_strlen(height); total_len = wlen + hlen + 2; if (total_len > 64) { png_warning(png_ptr, "Can't write sCAL (buffer too small)"); return; } buf[0] = (png_byte)unit; png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len); } #endif #endif #endif #ifdef PNG_WRITE_pHYs_SUPPORTED /* Write the pHYs chunk */ void /* PRIVATE */ png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, int unit_type) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_pHYs; #endif png_byte buf[9]; png_debug(1, "in png_write_pHYs"); if (unit_type >= PNG_RESOLUTION_LAST) png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); png_save_uint_32(buf, x_pixels_per_unit); png_save_uint_32(buf + 4, y_pixels_per_unit); buf[8] = (png_byte)unit_type; png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9); } #endif #ifdef PNG_WRITE_tIME_SUPPORTED /* Write the tIME chunk. Use either png_convert_from_struct_tm() * or png_convert_from_time_t(), or fill in the structure yourself. */ void /* PRIVATE */ png_write_tIME(png_structp png_ptr, png_timep mod_time) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_tIME; #endif png_byte buf[7]; png_debug(1, "in png_write_tIME"); if (mod_time->month > 12 || mod_time->month < 1 || mod_time->day > 31 || mod_time->day < 1 || mod_time->hour > 23 || mod_time->second > 60) { png_warning(png_ptr, "Invalid time specified for tIME chunk"); return; } png_save_uint_16(buf, mod_time->year); buf[2] = mod_time->month; buf[3] = mod_time->day; buf[4] = mod_time->hour; buf[5] = mod_time->minute; buf[6] = mod_time->second; png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7); } #endif /* Initializes the row writing capability of libpng */ void /* PRIVATE */ png_write_start_row(png_structp png_ptr) { #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; #endif png_size_t buf_size; png_debug(1, "in png_write_start_row"); buf_size = (png_size_t)(PNG_ROWBYTES( png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1); /* Set up row buffer */ png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size); png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; #ifdef PNG_WRITE_FILTER_SUPPORTED /* Set up filtering buffer, if using this filter */ if (png_ptr->do_filter & PNG_FILTER_SUB) { png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(png_ptr->rowbytes + 1)); png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; } /* We only need to keep the previous row if we are using one of these. */ if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) { /* Set up previous row buffer */ png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, (png_uint_32)buf_size); if (png_ptr->do_filter & PNG_FILTER_UP) { png_ptr->up_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(png_ptr->rowbytes + 1)); png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; } if (png_ptr->do_filter & PNG_FILTER_AVG) { png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(png_ptr->rowbytes + 1)); png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; } if (png_ptr->do_filter & PNG_FILTER_PAETH) { png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(png_ptr->rowbytes + 1)); png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; } } #endif /* PNG_WRITE_FILTER_SUPPORTED */ #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced, we need to set up width and height of pass */ if (png_ptr->interlaced) { if (!(png_ptr->transformations & PNG_INTERLACE)) { png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - png_pass_ystart[0]) / png_pass_yinc[0]; png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - png_pass_start[0]) / png_pass_inc[0]; } else { png_ptr->num_rows = png_ptr->height; png_ptr->usr_width = png_ptr->width; } } else #endif { png_ptr->num_rows = png_ptr->height; png_ptr->usr_width = png_ptr->width; } png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_ptr->zstream.next_out = png_ptr->zbuf; } /* Internal use only. Called when finished processing a row of data. */ void /* PRIVATE */ png_write_finish_row(png_structp png_ptr) { #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; #endif int ret; png_debug(1, "in png_write_finish_row"); /* Next row */ png_ptr->row_number++; /* See if we are done */ if (png_ptr->row_number < png_ptr->num_rows) return; #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced, go to next pass */ if (png_ptr->interlaced) { png_ptr->row_number = 0; if (png_ptr->transformations & PNG_INTERLACE) { png_ptr->pass++; } else { /* Loop until we find a non-zero width or height pass */ do { png_ptr->pass++; if (png_ptr->pass >= 7) break; png_ptr->usr_width = (png_ptr->width + png_pass_inc[png_ptr->pass] - 1 - png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; png_ptr->num_rows = (png_ptr->height + png_pass_yinc[png_ptr->pass] - 1 - png_pass_ystart[png_ptr->pass]) / png_pass_yinc[png_ptr->pass]; if (png_ptr->transformations & PNG_INTERLACE) break; } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); } /* Reset the row above the image for the next pass */ if (png_ptr->pass < 7) { if (png_ptr->prev_row != NULL) png_memset(png_ptr->prev_row, 0, (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* png_ptr->usr_bit_depth, png_ptr->width)) + 1); return; } } #endif /* If we get here, we've just written the last row, so we need to flush the compressor */ do { /* Tell the compressor we are done */ ret = deflate(&png_ptr->zstream, Z_FINISH); /* Check for an error */ if (ret == Z_OK) { /* Check to see if we need more room */ if (!(png_ptr->zstream.avail_out)) { png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } } else if (ret != Z_STREAM_END) { if (png_ptr->zstream.msg != NULL) png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } } while (ret != Z_STREAM_END); /* Write any extra space */ if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) { png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - png_ptr->zstream.avail_out); } deflateReset(&png_ptr->zstream); png_ptr->zstream.data_type = Z_BINARY; } #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Pick out the correct pixels for the interlace pass. * The basic idea here is to go through the row with a source * pointer and a destination pointer (sp and dp), and copy the * correct pixels for the pass. As the row gets compacted, * sp will always be >= dp, so we should never overwrite anything. * See the default: case for the easiest code to understand. */ void /* PRIVATE */ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) { /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; png_debug(1, "in png_do_write_interlace"); /* We don't have to do anything on the last pass (6) */ #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL && pass < 6) #else if (pass < 6) #endif { /* Each pixel depth is handled separately */ switch (row_info->pixel_depth) { case 1: { png_bytep sp; png_bytep dp; int shift; int d; int value; png_uint_32 i; png_uint_32 row_width = row_info->width; dp = row; d = 0; shift = 7; for (i = png_pass_start[pass]; i < row_width; i += png_pass_inc[pass]) { sp = row + (png_size_t)(i >> 3); value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; d |= (value << shift); if (shift == 0) { shift = 7; *dp++ = (png_byte)d; d = 0; } else shift--; } if (shift != 7) *dp = (png_byte)d; break; } case 2: { png_bytep sp; png_bytep dp; int shift; int d; int value; png_uint_32 i; png_uint_32 row_width = row_info->width; dp = row; shift = 6; d = 0; for (i = png_pass_start[pass]; i < row_width; i += png_pass_inc[pass]) { sp = row + (png_size_t)(i >> 2); value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; d |= (value << shift); if (shift == 0) { shift = 6; *dp++ = (png_byte)d; d = 0; } else shift -= 2; } if (shift != 6) *dp = (png_byte)d; break; } case 4: { png_bytep sp; png_bytep dp; int shift; int d; int value; png_uint_32 i; png_uint_32 row_width = row_info->width; dp = row; shift = 4; d = 0; for (i = png_pass_start[pass]; i < row_width; i += png_pass_inc[pass]) { sp = row + (png_size_t)(i >> 1); value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; d |= (value << shift); if (shift == 0) { shift = 4; *dp++ = (png_byte)d; d = 0; } else shift -= 4; } if (shift != 4) *dp = (png_byte)d; break; } default: { png_bytep sp; png_bytep dp; png_uint_32 i; png_uint_32 row_width = row_info->width; png_size_t pixel_bytes; /* Start at the beginning */ dp = row; /* Find out how many bytes each pixel takes up */ pixel_bytes = (row_info->pixel_depth >> 3); /* Loop through the row, only looking at the pixels that matter */ for (i = png_pass_start[pass]; i < row_width; i += png_pass_inc[pass]) { /* Find out where the original pixel is */ sp = row + (png_size_t)i * pixel_bytes; /* Move the pixel */ if (dp != sp) png_memcpy(dp, sp, pixel_bytes); /* Next pixel */ dp += pixel_bytes; } break; } } /* Set new row width */ row_info->width = (row_info->width + png_pass_inc[pass] - 1 - png_pass_start[pass]) / png_pass_inc[pass]; row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); } } #endif /* This filters the row, chooses which filter to use, if it has not already * been specified by the application, and then writes the row out with the * chosen filter. */ #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) #define PNG_HISHIFT 10 #define PNG_LOMASK ((png_uint_32)0xffffL) #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) void /* PRIVATE */ png_write_find_filter(png_structp png_ptr, png_row_infop row_info) { png_bytep best_row; #ifdef PNG_WRITE_FILTER_SUPPORTED png_bytep prev_row, row_buf; png_uint_32 mins, bpp; png_byte filter_to_do = png_ptr->do_filter; png_uint_32 row_bytes = row_info->rowbytes; #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED int num_p_filters = (int)png_ptr->num_prev_filters; #endif png_debug(1, "in png_write_find_filter"); #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS) { /* These will never be selected so we need not test them. */ filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH); } #endif /* Find out how many bytes offset each pixel is */ bpp = (row_info->pixel_depth + 7) >> 3; prev_row = png_ptr->prev_row; #endif best_row = png_ptr->row_buf; #ifdef PNG_WRITE_FILTER_SUPPORTED row_buf = best_row; mins = PNG_MAXSUM; /* The prediction method we use is to find which method provides the * smallest value when summing the absolute values of the distances * from zero, using anything >= 128 as negative numbers. This is known * as the "minimum sum of absolute differences" heuristic. Other * heuristics are the "weighted minimum sum of absolute differences" * (experimental and can in theory improve compression), and the "zlib * predictive" method (not implemented yet), which does test compressions * of lines using different filter methods, and then chooses the * (series of) filter(s) that give minimum compressed data size (VERY * computationally expensive). * * GRR 980525: consider also * (1) minimum sum of absolute differences from running average (i.e., * keep running sum of non-absolute differences & count of bytes) * [track dispersion, too? restart average if dispersion too large?] * (1b) minimum sum of absolute differences from sliding average, probably * with window size <= deflate window (usually 32K) * (2) minimum sum of squared differences from zero or running average * (i.e., ~ root-mean-square approach) */ /* We don't need to test the 'no filter' case if this is the only filter * that has been chosen, as it doesn't actually do anything to the data. */ if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE) { png_bytep rp; png_uint_32 sum = 0; png_uint_32 i; int v; for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) { v = *rp; sum += (v < 128) ? v : 256 - v; } #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { png_uint_32 sumhi, sumlo; int j; sumlo = sum & PNG_LOMASK; sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ /* Reduce the sum if we match any of the previous rows */ for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) { sumlo = (sumlo * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; sumhi = (sumhi * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } /* Factor in the cost of this filter (this is here for completeness, * but it makes no sense to have a "cost" for the NONE filter, as * it has the minimum possible computational cost - none). */ sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> PNG_COST_SHIFT; sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> PNG_COST_SHIFT; if (sumhi > PNG_HIMASK) sum = PNG_MAXSUM; else sum = (sumhi << PNG_HISHIFT) + sumlo; } #endif mins = sum; } /* Sub filter */ if (filter_to_do == PNG_FILTER_SUB) /* It's the only filter so no testing is needed */ { png_bytep rp, lp, dp; png_uint_32 i; for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; i++, rp++, dp++) { *dp = *rp; } for (lp = row_buf + 1; i < row_bytes; i++, rp++, lp++, dp++) { *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); } best_row = png_ptr->sub_row; } else if (filter_to_do & PNG_FILTER_SUB) { png_bytep rp, dp, lp; png_uint_32 sum = 0, lmins = mins; png_uint_32 i; int v; #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* We temporarily increase the "minimum sum" by the factor we * would reduce the sum of this filter, so that we can do the * early exit comparison without scaling the sum each time. */ if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 lmhi, lmlo; lmlo = lmins & PNG_LOMASK; lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) { lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> PNG_COST_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> PNG_COST_SHIFT; if (lmhi > PNG_HIMASK) lmins = PNG_MAXSUM; else lmins = (lmhi << PNG_HISHIFT) + lmlo; } #endif for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; i++, rp++, dp++) { v = *dp = *rp; sum += (v < 128) ? v : 256 - v; } for (lp = row_buf + 1; i < row_bytes; i++, rp++, lp++, dp++) { v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); sum += (v < 128) ? v : 256 - v; if (sum > lmins) /* We are already worse, don't continue. */ break; } #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 sumhi, sumlo; sumlo = sum & PNG_LOMASK; sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) { sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> PNG_COST_SHIFT; sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> PNG_COST_SHIFT; if (sumhi > PNG_HIMASK) sum = PNG_MAXSUM; else sum = (sumhi << PNG_HISHIFT) + sumlo; } #endif if (sum < mins) { mins = sum; best_row = png_ptr->sub_row; } } /* Up filter */ if (filter_to_do == PNG_FILTER_UP) { png_bytep rp, dp, pp; png_uint_32 i; for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, pp = prev_row + 1; i < row_bytes; i++, rp++, pp++, dp++) { *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); } best_row = png_ptr->up_row; } else if (filter_to_do & PNG_FILTER_UP) { png_bytep rp, dp, pp; png_uint_32 sum = 0, lmins = mins; png_uint_32 i; int v; #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 lmhi, lmlo; lmlo = lmins & PNG_LOMASK; lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) { lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> PNG_COST_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> PNG_COST_SHIFT; if (lmhi > PNG_HIMASK) lmins = PNG_MAXSUM; else lmins = (lmhi << PNG_HISHIFT) + lmlo; } #endif for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, pp = prev_row + 1; i < row_bytes; i++) { v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); sum += (v < 128) ? v : 256 - v; if (sum > lmins) /* We are already worse, don't continue. */ break; } #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 sumhi, sumlo; sumlo = sum & PNG_LOMASK; sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) { sumlo = (sumlo * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; sumhi = (sumhi * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> PNG_COST_SHIFT; sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> PNG_COST_SHIFT; if (sumhi > PNG_HIMASK) sum = PNG_MAXSUM; else sum = (sumhi << PNG_HISHIFT) + sumlo; } #endif if (sum < mins) { mins = sum; best_row = png_ptr->up_row; } } /* Avg filter */ if (filter_to_do == PNG_FILTER_AVG) { png_bytep rp, dp, pp, lp; png_uint_32 i; for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, pp = prev_row + 1; i < bpp; i++) { *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); } for (lp = row_buf + 1; i < row_bytes; i++) { *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); } best_row = png_ptr->avg_row; } else if (filter_to_do & PNG_FILTER_AVG) { png_bytep rp, dp, pp, lp; png_uint_32 sum = 0, lmins = mins; png_uint_32 i; int v; #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 lmhi, lmlo; lmlo = lmins & PNG_LOMASK; lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) { lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> PNG_COST_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> PNG_COST_SHIFT; if (lmhi > PNG_HIMASK) lmins = PNG_MAXSUM; else lmins = (lmhi << PNG_HISHIFT) + lmlo; } #endif for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, pp = prev_row + 1; i < bpp; i++) { v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); sum += (v < 128) ? v : 256 - v; } for (lp = row_buf + 1; i < row_bytes; i++) { v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); sum += (v < 128) ? v : 256 - v; if (sum > lmins) /* We are already worse, don't continue. */ break; } #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 sumhi, sumlo; sumlo = sum & PNG_LOMASK; sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) { sumlo = (sumlo * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; sumhi = (sumhi * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> PNG_COST_SHIFT; sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> PNG_COST_SHIFT; if (sumhi > PNG_HIMASK) sum = PNG_MAXSUM; else sum = (sumhi << PNG_HISHIFT) + sumlo; } #endif if (sum < mins) { mins = sum; best_row = png_ptr->avg_row; } } /* Paeth filter */ if (filter_to_do == PNG_FILTER_PAETH) { png_bytep rp, dp, pp, cp, lp; png_uint_32 i; for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, pp = prev_row + 1; i < bpp; i++) { *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); } for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) { int a, b, c, pa, pb, pc, p; b = *pp++; c = *cp++; a = *lp++; p = b - c; pc = a - c; #ifdef PNG_USE_ABS pa = abs(p); pb = abs(pc); pc = abs(p + pc); #else pa = p < 0 ? -p : p; pb = pc < 0 ? -pc : pc; pc = (p + pc) < 0 ? -(p + pc) : p + pc; #endif p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); } best_row = png_ptr->paeth_row; } else if (filter_to_do & PNG_FILTER_PAETH) { png_bytep rp, dp, pp, cp, lp; png_uint_32 sum = 0, lmins = mins; png_uint_32 i; int v; #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 lmhi, lmlo; lmlo = lmins & PNG_LOMASK; lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) { lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> PNG_COST_SHIFT; lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> PNG_COST_SHIFT; if (lmhi > PNG_HIMASK) lmins = PNG_MAXSUM; else lmins = (lmhi << PNG_HISHIFT) + lmlo; } #endif for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, pp = prev_row + 1; i < bpp; i++) { v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); sum += (v < 128) ? v : 256 - v; } for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) { int a, b, c, pa, pb, pc, p; b = *pp++; c = *cp++; a = *lp++; #ifndef PNG_SLOW_PAETH p = b - c; pc = a - c; #ifdef PNG_USE_ABS pa = abs(p); pb = abs(pc); pc = abs(p + pc); #else pa = p < 0 ? -p : p; pb = pc < 0 ? -pc : pc; pc = (p + pc) < 0 ? -(p + pc) : p + pc; #endif p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; #else /* PNG_SLOW_PAETH */ p = a + b - c; pa = abs(p - a); pb = abs(p - b); pc = abs(p - c); if (pa <= pb && pa <= pc) p = a; else if (pb <= pc) p = b; else p = c; #endif /* PNG_SLOW_PAETH */ v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); sum += (v < 128) ? v : 256 - v; if (sum > lmins) /* We are already worse, don't continue. */ break; } #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) { int j; png_uint_32 sumhi, sumlo; sumlo = sum & PNG_LOMASK; sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; for (j = 0; j < num_p_filters; j++) { if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) { sumlo = (sumlo * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; sumhi = (sumhi * png_ptr->filter_weights[j]) >> PNG_WEIGHT_SHIFT; } } sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> PNG_COST_SHIFT; sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> PNG_COST_SHIFT; if (sumhi > PNG_HIMASK) sum = PNG_MAXSUM; else sum = (sumhi << PNG_HISHIFT) + sumlo; } #endif if (sum < mins) { best_row = png_ptr->paeth_row; } } #endif /* PNG_WRITE_FILTER_SUPPORTED */ /* Do the actual writing of the filtered row data from the chosen filter. */ png_write_filtered_row(png_ptr, best_row); #ifdef PNG_WRITE_FILTER_SUPPORTED #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* Save the type of filter we picked this time for future calculations */ if (png_ptr->num_prev_filters > 0) { int j; for (j = 1; j < num_p_filters; j++) { png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; } png_ptr->prev_filters[j] = best_row[0]; } #endif #endif /* PNG_WRITE_FILTER_SUPPORTED */ } /* Do the actual writing of a previously filtered row. */ void /* PRIVATE */ png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) { png_debug(1, "in png_write_filtered_row"); png_debug1(2, "filter = %d", filtered_row[0]); /* Set up the zlib input buffer */ png_ptr->zstream.next_in = filtered_row; png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; /* Repeat until we have compressed all the data */ do { int ret; /* Return of zlib */ /* Compress the data */ ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); /* Check for compression errors */ if (ret != Z_OK) { if (png_ptr->zstream.msg != NULL) png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* See if it is time to write another IDAT */ if (!(png_ptr->zstream.avail_out)) { /* Write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } /* Repeat until all data has been compressed */ } while (png_ptr->zstream.avail_in); /* Swap the current and previous rows */ if (png_ptr->prev_row != NULL) { png_bytep tptr; tptr = png_ptr->prev_row; png_ptr->prev_row = png_ptr->row_buf; png_ptr->row_buf = tptr; } /* Finish row - updates counters and flushes zlib if last row */ png_write_finish_row(png_ptr); #ifdef PNG_WRITE_FLUSH_SUPPORTED png_ptr->flush_rows++; if (png_ptr->flush_dist > 0 && png_ptr->flush_rows >= png_ptr->flush_dist) { png_write_flush(png_ptr); } #endif } #endif /* PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngtrans.c0000664000175000017500000005122112013417376016761 0ustar alfalf00000000000000 /* pngtrans.c - transforms the data in a row (used by both readers and writers) * * Last changed in libpng 1.2.41 [December 3, 2009] * Copyright (c) 1998-2009 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) /* Turn on BGR-to-RGB mapping */ void PNGAPI png_set_bgr(png_structp png_ptr) { png_debug(1, "in png_set_bgr"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_BGR; } #endif #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) /* Turn on 16 bit byte swapping */ void PNGAPI png_set_swap(png_structp png_ptr) { png_debug(1, "in png_set_swap"); if (png_ptr == NULL) return; if (png_ptr->bit_depth == 16) png_ptr->transformations |= PNG_SWAP_BYTES; } #endif #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) /* Turn on pixel packing */ void PNGAPI png_set_packing(png_structp png_ptr) { png_debug(1, "in png_set_packing"); if (png_ptr == NULL) return; if (png_ptr->bit_depth < 8) { png_ptr->transformations |= PNG_PACK; png_ptr->usr_bit_depth = 8; } } #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) /* Turn on packed pixel swapping */ void PNGAPI png_set_packswap(png_structp png_ptr) { png_debug(1, "in png_set_packswap"); if (png_ptr == NULL) return; if (png_ptr->bit_depth < 8) png_ptr->transformations |= PNG_PACKSWAP; } #endif #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) void PNGAPI png_set_shift(png_structp png_ptr, png_color_8p true_bits) { png_debug(1, "in png_set_shift"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_SHIFT; png_ptr->shift = *true_bits; } #endif #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ defined(PNG_WRITE_INTERLACING_SUPPORTED) int PNGAPI png_set_interlace_handling(png_structp png_ptr) { png_debug(1, "in png_set_interlace handling"); if (png_ptr && png_ptr->interlaced) { png_ptr->transformations |= PNG_INTERLACE; return (7); } return (1); } #endif #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) /* Add a filler byte on read, or remove a filler or alpha byte on write. * The filler type has changed in v0.95 to allow future 2-byte fillers * for 48-bit input data, as well as to avoid problems with some compilers * that don't like bytes as parameters. */ void PNGAPI png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_filler"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_FILLER; #ifdef PNG_LEGACY_SUPPORTED png_ptr->filler = (png_byte)filler; #else png_ptr->filler = (png_uint_16)filler; #endif if (filler_loc == PNG_FILLER_AFTER) png_ptr->flags |= PNG_FLAG_FILLER_AFTER; else png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER; /* This should probably go in the "do_read_filler" routine. * I attempted to do that in libpng-1.0.1a but that caused problems * so I restored it in libpng-1.0.2a */ if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) { png_ptr->usr_channels = 4; } /* Also I added this in libpng-1.0.2a (what happens when we expand * a less-than-8-bit grayscale to GA? */ if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8) { png_ptr->usr_channels = 2; } } #ifndef PNG_1_0_X /* Added to libpng-1.2.7 */ void PNGAPI png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_add_alpha"); if (png_ptr == NULL) return; png_set_filler(png_ptr, filler, filler_loc); png_ptr->transformations |= PNG_ADD_ALPHA; } #endif #endif #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) void PNGAPI png_set_swap_alpha(png_structp png_ptr) { png_debug(1, "in png_set_swap_alpha"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_SWAP_ALPHA; } #endif #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) void PNGAPI png_set_invert_alpha(png_structp png_ptr) { png_debug(1, "in png_set_invert_alpha"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_INVERT_ALPHA; } #endif #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) void PNGAPI png_set_invert_mono(png_structp png_ptr) { png_debug(1, "in png_set_invert_mono"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_INVERT_MONO; } /* Invert monochrome grayscale data */ void /* PRIVATE */ png_do_invert(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_invert"); /* This test removed from libpng version 1.0.13 and 1.2.0: * if (row_info->bit_depth == 1 && */ #ifdef PNG_USELESS_TESTS_SUPPORTED if (row == NULL || row_info == NULL) return; #endif if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { png_bytep rp = row; png_uint_32 i; png_uint_32 istop = row_info->rowbytes; for (i = 0; i < istop; i++) { *rp = (png_byte)(~(*rp)); rp++; } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && row_info->bit_depth == 8) { png_bytep rp = row; png_uint_32 i; png_uint_32 istop = row_info->rowbytes; for (i = 0; i < istop; i+=2) { *rp = (png_byte)(~(*rp)); rp+=2; } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && row_info->bit_depth == 16) { png_bytep rp = row; png_uint_32 i; png_uint_32 istop = row_info->rowbytes; for (i = 0; i < istop; i+=4) { *rp = (png_byte)(~(*rp)); *(rp+1) = (png_byte)(~(*(rp+1))); rp+=4; } } } #endif #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) /* Swaps byte order on 16 bit depth images */ void /* PRIVATE */ png_do_swap(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_swap"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif row_info->bit_depth == 16) { png_bytep rp = row; png_uint_32 i; png_uint_32 istop= row_info->width * row_info->channels; for (i = 0; i < istop; i++, rp += 2) { png_byte t = *rp; *rp = *(rp + 1); *(rp + 1) = t; } } } #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) static PNG_CONST png_byte onebppswaptable[256] = { 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF }; static PNG_CONST png_byte twobppswaptable[256] = { 0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0, 0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0, 0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4, 0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4, 0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8, 0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8, 0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC, 0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC, 0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1, 0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1, 0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5, 0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5, 0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9, 0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9, 0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD, 0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD, 0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2, 0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2, 0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6, 0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6, 0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA, 0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA, 0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE, 0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE, 0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3, 0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3, 0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7, 0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7, 0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB, 0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB, 0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF, 0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF }; static PNG_CONST png_byte fourbppswaptable[256] = { 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71, 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1, 0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72, 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2, 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3, 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74, 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4, 0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75, 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5, 0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76, 0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6, 0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77, 0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7, 0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78, 0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8, 0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79, 0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9, 0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A, 0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA, 0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B, 0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB, 0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC, 0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D, 0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD, 0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E, 0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE, 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F, 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF }; /* Swaps pixel packing order within bytes */ void /* PRIVATE */ png_do_packswap(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_packswap"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif row_info->bit_depth < 8) { png_bytep rp, end, table; end = row + row_info->rowbytes; if (row_info->bit_depth == 1) table = (png_bytep)onebppswaptable; else if (row_info->bit_depth == 2) table = (png_bytep)twobppswaptable; else if (row_info->bit_depth == 4) table = (png_bytep)fourbppswaptable; else return; for (rp = row; rp < end; rp++) *rp = table[*rp]; } } #endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */ #if defined(PNG_WRITE_FILLER_SUPPORTED) || \ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) /* Remove filler or alpha byte(s) */ void /* PRIVATE */ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) { png_debug(1, "in png_do_strip_filler"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { png_bytep sp=row; png_bytep dp=row; png_uint_32 row_width=row_info->width; png_uint_32 i; if ((row_info->color_type == PNG_COLOR_TYPE_RGB || (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && (flags & PNG_FLAG_STRIP_ALPHA))) && row_info->channels == 4) { if (row_info->bit_depth == 8) { /* This converts from RGBX or RGBA to RGB */ if (flags & PNG_FLAG_FILLER_AFTER) { dp+=3; sp+=4; for (i = 1; i < row_width; i++) { *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; sp++; } } /* This converts from XRGB or ARGB to RGB */ else { for (i = 0; i < row_width; i++) { sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; } } row_info->pixel_depth = 24; row_info->rowbytes = row_width * 3; } else /* if (row_info->bit_depth == 16) */ { if (flags & PNG_FLAG_FILLER_AFTER) { /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */ sp += 8; dp += 6; for (i = 1; i < row_width; i++) { /* This could be (although png_memcpy is probably slower): png_memcpy(dp, sp, 6); sp += 8; dp += 6; */ *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; sp += 2; } } else { /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */ for (i = 0; i < row_width; i++) { /* This could be (although png_memcpy is probably slower): png_memcpy(dp, sp, 6); sp += 8; dp += 6; */ sp+=2; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; } } row_info->pixel_depth = 48; row_info->rowbytes = row_width * 6; } row_info->channels = 3; } else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY || (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && (flags & PNG_FLAG_STRIP_ALPHA))) && row_info->channels == 2) { if (row_info->bit_depth == 8) { /* This converts from GX or GA to G */ if (flags & PNG_FLAG_FILLER_AFTER) { for (i = 0; i < row_width; i++) { *dp++ = *sp++; sp++; } } /* This converts from XG or AG to G */ else { for (i = 0; i < row_width; i++) { sp++; *dp++ = *sp++; } } row_info->pixel_depth = 8; row_info->rowbytes = row_width; } else /* if (row_info->bit_depth == 16) */ { if (flags & PNG_FLAG_FILLER_AFTER) { /* This converts from GGXX or GGAA to GG */ sp += 4; dp += 2; for (i = 1; i < row_width; i++) { *dp++ = *sp++; *dp++ = *sp++; sp += 2; } } else { /* This converts from XXGG or AAGG to GG */ for (i = 0; i < row_width; i++) { sp += 2; *dp++ = *sp++; *dp++ = *sp++; } } row_info->pixel_depth = 16; row_info->rowbytes = row_width * 2; } row_info->channels = 1; } if (flags & PNG_FLAG_STRIP_ALPHA) row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; } } #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) /* Swaps red and blue bytes within a pixel */ void /* PRIVATE */ png_do_bgr(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_bgr"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif (row_info->color_type & PNG_COLOR_MASK_COLOR)) { png_uint_32 row_width = row_info->width; if (row_info->bit_depth == 8) { if (row_info->color_type == PNG_COLOR_TYPE_RGB) { png_bytep rp; png_uint_32 i; for (i = 0, rp = row; i < row_width; i++, rp += 3) { png_byte save = *rp; *rp = *(rp + 2); *(rp + 2) = save; } } else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { png_bytep rp; png_uint_32 i; for (i = 0, rp = row; i < row_width; i++, rp += 4) { png_byte save = *rp; *rp = *(rp + 2); *(rp + 2) = save; } } } else if (row_info->bit_depth == 16) { if (row_info->color_type == PNG_COLOR_TYPE_RGB) { png_bytep rp; png_uint_32 i; for (i = 0, rp = row; i < row_width; i++, rp += 6) { png_byte save = *rp; *rp = *(rp + 4); *(rp + 4) = save; save = *(rp + 1); *(rp + 1) = *(rp + 5); *(rp + 5) = save; } } else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { png_bytep rp; png_uint_32 i; for (i = 0, rp = row; i < row_width; i++, rp += 8) { png_byte save = *rp; *rp = *(rp + 4); *(rp + 4) = save; save = *(rp + 1); *(rp + 1) = *(rp + 5); *(rp + 5) = save; } } } } } #endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */ #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) void PNGAPI png_set_user_transform_info(png_structp png_ptr, png_voidp user_transform_ptr, int user_transform_depth, int user_transform_channels) { png_debug(1, "in png_set_user_transform_info"); if (png_ptr == NULL) return; #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED png_ptr->user_transform_ptr = user_transform_ptr; png_ptr->user_transform_depth = (png_byte)user_transform_depth; png_ptr->user_transform_channels = (png_byte)user_transform_channels; #else if (user_transform_ptr || user_transform_depth || user_transform_channels) png_warning(png_ptr, "This version of libpng does not support user transform info"); #endif } #endif /* This function returns a pointer to the user_transform_ptr associated with * the user transform functions. The application should free any memory * associated with this pointer before png_write_destroy and png_read_destroy * are called. */ png_voidp PNGAPI png_get_user_transform_ptr(png_structp png_ptr) { if (png_ptr == NULL) return (NULL); #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED return ((png_voidp)png_ptr->user_transform_ptr); #else return (NULL); #endif } #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngrio.c0000664000175000017500000001314512013417376016426 0ustar alfalf00000000000000 /* pngrio.c - functions for data input * * Last changed in libpng 1.2.43 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file provides a location for all input. Users who need * special handling are expected to write a function that has the same * arguments as this and performs a similar function, but that possibly * has a different input method. Note that you shouldn't change this * function, but rather write a replacement function and then make * libpng use it at run time with png_set_read_fn(...). */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_READ_SUPPORTED /* Read the data from whatever input you are using. The default routine * reads from a file pointer. Note that this routine sometimes gets called * with very small lengths, so you should implement some kind of simple * buffering if you are using unbuffered reads. This should never be asked * to read more then 64K on a 16 bit machine. */ void /* PRIVATE */ png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_debug1(4, "reading %d bytes", (int)length); if (png_ptr->read_data_fn != NULL) (*(png_ptr->read_data_fn))(png_ptr, data, length); else png_error(png_ptr, "Call to NULL read function"); } #ifdef PNG_STDIO_SUPPORTED /* This is the function that does the actual reading of data. If you are * not reading from a standard C stream, you should create a replacement * read_data function and use it at run time with png_set_read_fn(), rather * than changing the library. */ #ifndef USE_FAR_KEYWORD void PNGAPI png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_size_t check; if (png_ptr == NULL) return; /* fread() returns 0 on error, so it is OK to store this in a png_size_t * instead of an int, which is what fread() actually returns. */ #ifdef _WIN32_WCE if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) check = 0; #else check = (png_size_t)fread(data, (png_size_t)1, length, (png_FILE_p)png_ptr->io_ptr); #endif if (check != length) png_error(png_ptr, "Read Error"); } #else /* This is the model-independent version. Since the standard I/O library can't handle far buffers in the medium and small models, we have to copy the data. */ #define NEAR_BUF_SIZE 1024 #define MIN(a,b) (a <= b ? a : b) static void PNGAPI png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { int check; png_byte *n_data; png_FILE_p io_ptr; if (png_ptr == NULL) return; /* Check if data really is near. If so, use usual code. */ n_data = (png_byte *)CVT_PTR_NOCHECK(data); io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); if ((png_bytep)n_data == data) { #ifdef _WIN32_WCE if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) check = 0; #else check = fread(n_data, 1, length, io_ptr); #endif } else { png_byte buf[NEAR_BUF_SIZE]; png_size_t read, remaining, err; check = 0; remaining = length; do { read = MIN(NEAR_BUF_SIZE, remaining); #ifdef _WIN32_WCE if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) ) err = 0; #else err = fread(buf, (png_size_t)1, read, io_ptr); #endif png_memcpy(data, buf, read); /* copy far buffer to near buffer */ if (err != read) break; else check += err; data += read; remaining -= read; } while (remaining != 0); } if ((png_uint_32)check != (png_uint_32)length) png_error(png_ptr, "read Error"); } #endif #endif /* This function allows the application to supply a new input function * for libpng if standard C streams aren't being used. * * This function takes as its arguments: * png_ptr - pointer to a png input data structure * io_ptr - pointer to user supplied structure containing info about * the input functions. May be NULL. * read_data_fn - pointer to a new input function that takes as its * arguments a pointer to a png_struct, a pointer to * a location where input data can be stored, and a 32-bit * unsigned int that is the number of bytes to be read. * To exit and output any fatal error messages the new write * function should call png_error(png_ptr, "Error msg"). * May be NULL, in which case libpng's default function will * be used. */ void PNGAPI png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn) { if (png_ptr == NULL) return; png_ptr->io_ptr = io_ptr; #ifdef PNG_STDIO_SUPPORTED if (read_data_fn != NULL) png_ptr->read_data_fn = read_data_fn; else png_ptr->read_data_fn = png_default_read_data; #else png_ptr->read_data_fn = read_data_fn; #endif /* It is an error to write to a read device */ if (png_ptr->write_data_fn != NULL) { png_ptr->write_data_fn = NULL; png_warning(png_ptr, "It's an error to set both read_data_fn and write_data_fn in the "); png_warning(png_ptr, "same structure. Resetting write_data_fn to NULL."); } #ifdef PNG_WRITE_FLUSH_SUPPORTED png_ptr->output_flush_fn = NULL; #endif } #endif /* PNG_READ_SUPPORTED */ glmark2-2012.08/./src/libpng/pngpread.c0000664000175000017500000013743212013417376016736 0ustar alfalf00000000000000 /* pngpread.c - read a png file in push mode * * Last changed in libpng 1.2.44 [June 26, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_PROGRESSIVE_READ_SUPPORTED /* Push model modes */ #define PNG_READ_SIG_MODE 0 #define PNG_READ_CHUNK_MODE 1 #define PNG_READ_IDAT_MODE 2 #define PNG_SKIP_MODE 3 #define PNG_READ_tEXt_MODE 4 #define PNG_READ_zTXt_MODE 5 #define PNG_READ_DONE_MODE 6 #define PNG_READ_iTXt_MODE 7 #define PNG_ERROR_MODE 8 void PNGAPI png_process_data(png_structp png_ptr, png_infop info_ptr, png_bytep buffer, png_size_t buffer_size) { if (png_ptr == NULL || info_ptr == NULL) return; png_push_restore_buffer(png_ptr, buffer, buffer_size); while (png_ptr->buffer_size) { png_process_some_data(png_ptr, info_ptr); } } /* What we do with the incoming data depends on what we were previously * doing before we ran out of data... */ void /* PRIVATE */ png_process_some_data(png_structp png_ptr, png_infop info_ptr) { if (png_ptr == NULL) return; switch (png_ptr->process_mode) { case PNG_READ_SIG_MODE: { png_push_read_sig(png_ptr, info_ptr); break; } case PNG_READ_CHUNK_MODE: { png_push_read_chunk(png_ptr, info_ptr); break; } case PNG_READ_IDAT_MODE: { png_push_read_IDAT(png_ptr); break; } #ifdef PNG_READ_tEXt_SUPPORTED case PNG_READ_tEXt_MODE: { png_push_read_tEXt(png_ptr, info_ptr); break; } #endif #ifdef PNG_READ_zTXt_SUPPORTED case PNG_READ_zTXt_MODE: { png_push_read_zTXt(png_ptr, info_ptr); break; } #endif #ifdef PNG_READ_iTXt_SUPPORTED case PNG_READ_iTXt_MODE: { png_push_read_iTXt(png_ptr, info_ptr); break; } #endif case PNG_SKIP_MODE: { png_push_crc_finish(png_ptr); break; } default: { png_ptr->buffer_size = 0; break; } } } /* Read any remaining signature bytes from the stream and compare them with * the correct PNG signature. It is possible that this routine is called * with bytes already read from the signature, either because they have been * checked by the calling application, or because of multiple calls to this * routine. */ void /* PRIVATE */ png_push_read_sig(png_structp png_ptr, png_infop info_ptr) { png_size_t num_checked = png_ptr->sig_bytes, num_to_check = 8 - num_checked; if (png_ptr->buffer_size < num_to_check) { num_to_check = png_ptr->buffer_size; } png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check); if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) { if (num_checked < 4 && png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) png_error(png_ptr, "Not a PNG file"); else png_error(png_ptr, "PNG file corrupted by ASCII conversion"); } else { if (png_ptr->sig_bytes >= 8) { png_ptr->process_mode = PNG_READ_CHUNK_MODE; } } } void /* PRIVATE */ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IHDR; PNG_CONST PNG_IDAT; PNG_CONST PNG_IEND; PNG_CONST PNG_PLTE; #ifdef PNG_READ_bKGD_SUPPORTED PNG_CONST PNG_bKGD; #endif #ifdef PNG_READ_cHRM_SUPPORTED PNG_CONST PNG_cHRM; #endif #ifdef PNG_READ_gAMA_SUPPORTED PNG_CONST PNG_gAMA; #endif #ifdef PNG_READ_hIST_SUPPORTED PNG_CONST PNG_hIST; #endif #ifdef PNG_READ_iCCP_SUPPORTED PNG_CONST PNG_iCCP; #endif #ifdef PNG_READ_iTXt_SUPPORTED PNG_CONST PNG_iTXt; #endif #ifdef PNG_READ_oFFs_SUPPORTED PNG_CONST PNG_oFFs; #endif #ifdef PNG_READ_pCAL_SUPPORTED PNG_CONST PNG_pCAL; #endif #ifdef PNG_READ_pHYs_SUPPORTED PNG_CONST PNG_pHYs; #endif #ifdef PNG_READ_sBIT_SUPPORTED PNG_CONST PNG_sBIT; #endif #ifdef PNG_READ_sCAL_SUPPORTED PNG_CONST PNG_sCAL; #endif #ifdef PNG_READ_sRGB_SUPPORTED PNG_CONST PNG_sRGB; #endif #ifdef PNG_READ_sPLT_SUPPORTED PNG_CONST PNG_sPLT; #endif #ifdef PNG_READ_tEXt_SUPPORTED PNG_CONST PNG_tEXt; #endif #ifdef PNG_READ_tIME_SUPPORTED PNG_CONST PNG_tIME; #endif #ifdef PNG_READ_tRNS_SUPPORTED PNG_CONST PNG_tRNS; #endif #ifdef PNG_READ_zTXt_SUPPORTED PNG_CONST PNG_zTXt; #endif #endif /* PNG_USE_LOCAL_ARRAYS */ /* First we make sure we have enough data for the 4 byte chunk name * and the 4 byte chunk length before proceeding with decoding the * chunk data. To fully decode each of these chunks, we also make * sure we have enough data in the buffer for the 4 byte CRC at the * end of every chunk (except IDAT, which is handled separately). */ if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) { png_byte chunk_length[4]; if (png_ptr->buffer_size < 8) { png_push_save_buffer(png_ptr); return; } png_push_fill_buffer(png_ptr, chunk_length, 4); png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); png_reset_crc(png_ptr); png_crc_read(png_ptr, png_ptr->chunk_name, 4); png_check_chunk_name(png_ptr, png_ptr->chunk_name); png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; } if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) if (png_ptr->mode & PNG_AFTER_IDAT) png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) { if (png_ptr->push_length != 13) png_error(png_ptr, "Invalid IHDR length"); if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); } else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); png_ptr->process_mode = PNG_READ_DONE_MODE; png_push_have_end(png_ptr, info_ptr); } #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) png_ptr->mode |= PNG_HAVE_IDAT; png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) png_ptr->mode |= PNG_HAVE_PLTE; else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_error(png_ptr, "Missing PLTE before IDAT"); } } #endif else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); } else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { /* If we reach an IDAT chunk, this means we have read all of the * header chunks, and we can start reading the image (or if this * is called after the image has been read - we have an error). */ if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_error(png_ptr, "Missing PLTE before IDAT"); if (png_ptr->mode & PNG_HAVE_IDAT) { if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) if (png_ptr->push_length == 0) return; if (png_ptr->mode & PNG_AFTER_IDAT) png_error(png_ptr, "Too many IDAT's found"); } png_ptr->idat_size = png_ptr->push_length; png_ptr->mode |= PNG_HAVE_IDAT; png_ptr->process_mode = PNG_READ_IDAT_MODE; png_push_have_info(png_ptr, info_ptr); png_ptr->zstream.avail_out = (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; png_ptr->zstream.next_out = png_ptr->row_buf; return; } #ifdef PNG_READ_gAMA_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_sBIT_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_cHRM_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_sRGB_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_iCCP_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_sPLT_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_tRNS_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_bKGD_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_hIST_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_pHYs_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_oFFs_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_pCAL_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_sCAL_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_tIME_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_tEXt_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_zTXt_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); } #endif #ifdef PNG_READ_iTXt_SUPPORTED else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length); } #endif else { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); } png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; } void /* PRIVATE */ png_push_crc_skip(png_structp png_ptr, png_uint_32 skip) { png_ptr->process_mode = PNG_SKIP_MODE; png_ptr->skip_length = skip; } void /* PRIVATE */ png_push_crc_finish(png_structp png_ptr) { if (png_ptr->skip_length && png_ptr->save_buffer_size) { png_size_t save_size; if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size) save_size = (png_size_t)png_ptr->skip_length; else save_size = png_ptr->save_buffer_size; png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); png_ptr->skip_length -= save_size; png_ptr->buffer_size -= save_size; png_ptr->save_buffer_size -= save_size; png_ptr->save_buffer_ptr += save_size; } if (png_ptr->skip_length && png_ptr->current_buffer_size) { png_size_t save_size; if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size) save_size = (png_size_t)png_ptr->skip_length; else save_size = png_ptr->current_buffer_size; png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); png_ptr->skip_length -= save_size; png_ptr->buffer_size -= save_size; png_ptr->current_buffer_size -= save_size; png_ptr->current_buffer_ptr += save_size; } if (!png_ptr->skip_length) { if (png_ptr->buffer_size < 4) { png_push_save_buffer(png_ptr); return; } png_crc_finish(png_ptr, 0); png_ptr->process_mode = PNG_READ_CHUNK_MODE; } } void PNGAPI png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) { png_bytep ptr; if (png_ptr == NULL) return; ptr = buffer; if (png_ptr->save_buffer_size) { png_size_t save_size; if (length < png_ptr->save_buffer_size) save_size = length; else save_size = png_ptr->save_buffer_size; png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); length -= save_size; ptr += save_size; png_ptr->buffer_size -= save_size; png_ptr->save_buffer_size -= save_size; png_ptr->save_buffer_ptr += save_size; } if (length && png_ptr->current_buffer_size) { png_size_t save_size; if (length < png_ptr->current_buffer_size) save_size = length; else save_size = png_ptr->current_buffer_size; png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); png_ptr->buffer_size -= save_size; png_ptr->current_buffer_size -= save_size; png_ptr->current_buffer_ptr += save_size; } } void /* PRIVATE */ png_push_save_buffer(png_structp png_ptr) { if (png_ptr->save_buffer_size) { if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) { png_size_t i, istop; png_bytep sp; png_bytep dp; istop = png_ptr->save_buffer_size; for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; i < istop; i++, sp++, dp++) { *dp = *sp; } } } if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > png_ptr->save_buffer_max) { png_size_t new_max; png_bytep old_buffer; if (png_ptr->save_buffer_size > PNG_SIZE_MAX - (png_ptr->current_buffer_size + 256)) { png_error(png_ptr, "Potential overflow of save_buffer"); } new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; old_buffer = png_ptr->save_buffer; png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, (png_uint_32)new_max); if (png_ptr->save_buffer == NULL) { png_free(png_ptr, old_buffer); png_error(png_ptr, "Insufficient memory for save_buffer"); } png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); png_free(png_ptr, old_buffer); png_ptr->save_buffer_max = new_max; } if (png_ptr->current_buffer_size) { png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); png_ptr->save_buffer_size += png_ptr->current_buffer_size; png_ptr->current_buffer_size = 0; } png_ptr->save_buffer_ptr = png_ptr->save_buffer; png_ptr->buffer_size = 0; } void /* PRIVATE */ png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, png_size_t buffer_length) { png_ptr->current_buffer = buffer; png_ptr->current_buffer_size = buffer_length; png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; png_ptr->current_buffer_ptr = png_ptr->current_buffer; } void /* PRIVATE */ png_push_read_IDAT(png_structp png_ptr) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST PNG_IDAT; #endif if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) { png_byte chunk_length[4]; if (png_ptr->buffer_size < 8) { png_push_save_buffer(png_ptr); return; } png_push_fill_buffer(png_ptr, chunk_length, 4); png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); png_reset_crc(png_ptr); png_crc_read(png_ptr, png_ptr->chunk_name, 4); png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { png_ptr->process_mode = PNG_READ_CHUNK_MODE; if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) png_error(png_ptr, "Not enough compressed data"); return; } png_ptr->idat_size = png_ptr->push_length; } if (png_ptr->idat_size && png_ptr->save_buffer_size) { png_size_t save_size; if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size) { save_size = (png_size_t)png_ptr->idat_size; /* Check for overflow */ if ((png_uint_32)save_size != png_ptr->idat_size) png_error(png_ptr, "save_size overflowed in pngpread"); } else save_size = png_ptr->save_buffer_size; png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); png_ptr->idat_size -= save_size; png_ptr->buffer_size -= save_size; png_ptr->save_buffer_size -= save_size; png_ptr->save_buffer_ptr += save_size; } if (png_ptr->idat_size && png_ptr->current_buffer_size) { png_size_t save_size; if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) { save_size = (png_size_t)png_ptr->idat_size; /* Check for overflow */ if ((png_uint_32)save_size != png_ptr->idat_size) png_error(png_ptr, "save_size overflowed in pngpread"); } else save_size = png_ptr->current_buffer_size; png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); png_ptr->idat_size -= save_size; png_ptr->buffer_size -= save_size; png_ptr->current_buffer_size -= save_size; png_ptr->current_buffer_ptr += save_size; } if (!png_ptr->idat_size) { if (png_ptr->buffer_size < 4) { png_push_save_buffer(png_ptr); return; } png_crc_finish(png_ptr, 0); png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; png_ptr->mode |= PNG_AFTER_IDAT; } } void /* PRIVATE */ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, png_size_t buffer_length) { /* The caller checks for a non-zero buffer length. */ if (!(buffer_length > 0) || buffer == NULL) png_error(png_ptr, "No IDAT data (internal error)"); /* This routine must process all the data it has been given * before returning, calling the row callback as required to * handle the uncompressed results. */ png_ptr->zstream.next_in = buffer; png_ptr->zstream.avail_in = (uInt)buffer_length; /* Keep going until the decompressed data is all processed * or the stream marked as finished. */ while (png_ptr->zstream.avail_in > 0 && !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) { int ret; /* We have data for zlib, but we must check that zlib * has somewhere to put the results. It doesn't matter * if we don't expect any results -- it may be the input * data is just the LZ end code. */ if (!(png_ptr->zstream.avail_out > 0)) { png_ptr->zstream.avail_out = (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; png_ptr->zstream.next_out = png_ptr->row_buf; } /* Using Z_SYNC_FLUSH here means that an unterminated * LZ stream can still be handled (a stream with a missing * end code), otherwise (Z_NO_FLUSH) a future zlib * implementation might defer output and, therefore, * change the current behavior. (See comments in inflate.c * for why this doesn't happen at present with zlib 1.2.5.) */ ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH); /* Check for any failure before proceeding. */ if (ret != Z_OK && ret != Z_STREAM_END) { /* Terminate the decompression. */ png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; /* This may be a truncated stream (missing or * damaged end code). Treat that as a warning. */ if (png_ptr->row_number >= png_ptr->num_rows || png_ptr->pass > 6) png_warning(png_ptr, "Truncated compressed data in IDAT"); else png_error(png_ptr, "Decompression error in IDAT"); /* Skip the check on unprocessed input */ return; } /* Did inflate output any data? */ if (png_ptr->zstream.next_out != png_ptr->row_buf) { /* Is this unexpected data after the last row? * If it is, artificially terminate the LZ output * here. */ if (png_ptr->row_number >= png_ptr->num_rows || png_ptr->pass > 6) { /* Extra data. */ png_warning(png_ptr, "Extra compressed data in IDAT"); png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; /* Do no more processing; skip the unprocessed * input check below. */ return; } /* Do we have a complete row? */ if (png_ptr->zstream.avail_out == 0) png_push_process_row(png_ptr); } /* And check for the end of the stream. */ if (ret == Z_STREAM_END) png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; } /* All the data should have been processed, if anything * is left at this point we have bytes of IDAT data * after the zlib end code. */ if (png_ptr->zstream.avail_in > 0) png_warning(png_ptr, "Extra compression data"); } void /* PRIVATE */ png_push_process_row(png_structp png_ptr) { png_ptr->row_info.color_type = png_ptr->color_type; png_ptr->row_info.width = png_ptr->iwidth; png_ptr->row_info.channels = png_ptr->channels; png_ptr->row_info.bit_depth = png_ptr->bit_depth; png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->row_info.width); png_read_filter_row(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->prev_row + 1, (int)(png_ptr->row_buf[0])); png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1); if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) png_do_read_transformations(png_ptr); #ifdef PNG_READ_INTERLACING_SUPPORTED /* Blow up interlaced rows to full size */ if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) { if (png_ptr->pass < 6) /* old interface (pre-1.0.9): png_do_read_interlace(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); */ png_do_read_interlace(png_ptr); switch (png_ptr->pass) { case 0: { int i; for (i = 0; i < 8 && png_ptr->pass == 0; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */ } if (png_ptr->pass == 2) /* Pass 1 might be empty */ { for (i = 0; i < 4 && png_ptr->pass == 2; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } if (png_ptr->pass == 4 && png_ptr->height <= 4) { for (i = 0; i < 2 && png_ptr->pass == 4; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } if (png_ptr->pass == 6 && png_ptr->height <= 4) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } break; } case 1: { int i; for (i = 0; i < 8 && png_ptr->pass == 1; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } if (png_ptr->pass == 2) /* Skip top 4 generated rows */ { for (i = 0; i < 4 && png_ptr->pass == 2; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } break; } case 2: { int i; for (i = 0; i < 4 && png_ptr->pass == 2; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } for (i = 0; i < 4 && png_ptr->pass == 2; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } if (png_ptr->pass == 4) /* Pass 3 might be empty */ { for (i = 0; i < 2 && png_ptr->pass == 4; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } break; } case 3: { int i; for (i = 0; i < 4 && png_ptr->pass == 3; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } if (png_ptr->pass == 4) /* Skip top two generated rows */ { for (i = 0; i < 2 && png_ptr->pass == 4; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } break; } case 4: { int i; for (i = 0; i < 2 && png_ptr->pass == 4; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } for (i = 0; i < 2 && png_ptr->pass == 4; i++) { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } if (png_ptr->pass == 6) /* Pass 5 might be empty */ { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } break; } case 5: { int i; for (i = 0; i < 2 && png_ptr->pass == 5; i++) { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } if (png_ptr->pass == 6) /* Skip top generated row */ { png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } break; } case 6: { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); if (png_ptr->pass != 6) break; png_push_have_row(png_ptr, png_bytep_NULL); png_read_push_finish_row(png_ptr); } } } else #endif { png_push_have_row(png_ptr, png_ptr->row_buf + 1); png_read_push_finish_row(png_ptr); } } void /* PRIVATE */ png_read_push_finish_row(png_structp png_ptr) { #ifdef PNG_USE_LOCAL_ARRAYS /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; /* Height of interlace block. This is not currently used - if you need * it, uncomment it here and in png.h PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; */ #endif png_ptr->row_number++; if (png_ptr->row_number < png_ptr->num_rows) return; #ifdef PNG_READ_INTERLACING_SUPPORTED if (png_ptr->interlaced) { png_ptr->row_number = 0; png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); do { png_ptr->pass++; if ((png_ptr->pass == 1 && png_ptr->width < 5) || (png_ptr->pass == 3 && png_ptr->width < 3) || (png_ptr->pass == 5 && png_ptr->width < 2)) png_ptr->pass++; if (png_ptr->pass > 7) png_ptr->pass--; if (png_ptr->pass >= 7) break; png_ptr->iwidth = (png_ptr->width + png_pass_inc[png_ptr->pass] - 1 - png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; if (png_ptr->transformations & PNG_INTERLACE) break; png_ptr->num_rows = (png_ptr->height + png_pass_yinc[png_ptr->pass] - 1 - png_pass_ystart[png_ptr->pass]) / png_pass_yinc[png_ptr->pass]; } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); } #endif /* PNG_READ_INTERLACING_SUPPORTED */ } #ifdef PNG_READ_tEXt_SUPPORTED void /* PRIVATE */ png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) { png_error(png_ptr, "Out of place tEXt"); info_ptr = info_ptr; /* To quiet some compiler warnings */ } #ifdef PNG_MAX_MALLOC_64K png_ptr->skip_length = 0; /* This may not be necessary */ if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */ { png_warning(png_ptr, "tEXt chunk too large to fit in memory"); png_ptr->skip_length = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_ptr->current_text = (png_charp)png_malloc(png_ptr, (png_uint_32)(length + 1)); png_ptr->current_text[length] = '\0'; png_ptr->current_text_ptr = png_ptr->current_text; png_ptr->current_text_size = (png_size_t)length; png_ptr->current_text_left = (png_size_t)length; png_ptr->process_mode = PNG_READ_tEXt_MODE; } void /* PRIVATE */ png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->buffer_size && png_ptr->current_text_left) { png_size_t text_size; if (png_ptr->buffer_size < png_ptr->current_text_left) text_size = png_ptr->buffer_size; else text_size = png_ptr->current_text_left; png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); png_ptr->current_text_left -= text_size; png_ptr->current_text_ptr += text_size; } if (!(png_ptr->current_text_left)) { png_textp text_ptr; png_charp text; png_charp key; int ret; if (png_ptr->buffer_size < 4) { png_push_save_buffer(png_ptr); return; } png_push_crc_finish(png_ptr); #ifdef PNG_MAX_MALLOC_64K if (png_ptr->skip_length) return; #endif key = png_ptr->current_text; for (text = key; *text; text++) /* Empty loop */ ; if (text < key + png_ptr->current_text_size) text++; text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)png_sizeof(png_text)); text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; text_ptr->key = key; #ifdef PNG_iTXt_SUPPORTED text_ptr->lang = NULL; text_ptr->lang_key = NULL; #endif text_ptr->text = text; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_free(png_ptr, key); png_free(png_ptr, text_ptr); png_ptr->current_text = NULL; if (ret) png_warning(png_ptr, "Insufficient memory to store text chunk."); } } #endif #ifdef PNG_READ_zTXt_SUPPORTED void /* PRIVATE */ png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) { png_error(png_ptr, "Out of place zTXt"); info_ptr = info_ptr; /* To quiet some compiler warnings */ } #ifdef PNG_MAX_MALLOC_64K /* We can't handle zTXt chunks > 64K, since we don't have enough space * to be able to store the uncompressed data. Actually, the threshold * is probably around 32K, but it isn't as definite as 64K is. */ if (length > (png_uint_32)65535L) { png_warning(png_ptr, "zTXt chunk too large to fit in memory"); png_push_crc_skip(png_ptr, length); return; } #endif png_ptr->current_text = (png_charp)png_malloc(png_ptr, (png_uint_32)(length + 1)); png_ptr->current_text[length] = '\0'; png_ptr->current_text_ptr = png_ptr->current_text; png_ptr->current_text_size = (png_size_t)length; png_ptr->current_text_left = (png_size_t)length; png_ptr->process_mode = PNG_READ_zTXt_MODE; } void /* PRIVATE */ png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->buffer_size && png_ptr->current_text_left) { png_size_t text_size; if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left) text_size = png_ptr->buffer_size; else text_size = png_ptr->current_text_left; png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); png_ptr->current_text_left -= text_size; png_ptr->current_text_ptr += text_size; } if (!(png_ptr->current_text_left)) { png_textp text_ptr; png_charp text; png_charp key; int ret; png_size_t text_size, key_size; if (png_ptr->buffer_size < 4) { png_push_save_buffer(png_ptr); return; } png_push_crc_finish(png_ptr); key = png_ptr->current_text; for (text = key; *text; text++) /* Empty loop */ ; /* zTXt can't have zero text */ if (text >= key + png_ptr->current_text_size) { png_ptr->current_text = NULL; png_free(png_ptr, key); return; } text++; if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */ { png_ptr->current_text = NULL; png_free(png_ptr, key); return; } text++; png_ptr->zstream.next_in = (png_bytep )text; png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size - (text - key)); png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; key_size = text - key; text_size = 0; text = NULL; ret = Z_STREAM_END; while (png_ptr->zstream.avail_in) { ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret != Z_OK && ret != Z_STREAM_END) { inflateReset(&png_ptr->zstream); png_ptr->zstream.avail_in = 0; png_ptr->current_text = NULL; png_free(png_ptr, key); png_free(png_ptr, text); return; } if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END) { if (text == NULL) { text = (png_charp)png_malloc(png_ptr, (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out + key_size + 1)); png_memcpy(text + key_size, png_ptr->zbuf, png_ptr->zbuf_size - png_ptr->zstream.avail_out); png_memcpy(text, key, key_size); text_size = key_size + png_ptr->zbuf_size - png_ptr->zstream.avail_out; *(text + text_size) = '\0'; } else { png_charp tmp; tmp = text; text = (png_charp)png_malloc(png_ptr, text_size + (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); png_memcpy(text, tmp, text_size); png_free(png_ptr, tmp); png_memcpy(text + text_size, png_ptr->zbuf, png_ptr->zbuf_size - png_ptr->zstream.avail_out); text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; *(text + text_size) = '\0'; } if (ret != Z_STREAM_END) { png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } } else { break; } if (ret == Z_STREAM_END) break; } inflateReset(&png_ptr->zstream); png_ptr->zstream.avail_in = 0; if (ret != Z_STREAM_END) { png_ptr->current_text = NULL; png_free(png_ptr, key); png_free(png_ptr, text); return; } png_ptr->current_text = NULL; png_free(png_ptr, key); key = text; text += key_size; text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)png_sizeof(png_text)); text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt; text_ptr->key = key; #ifdef PNG_iTXt_SUPPORTED text_ptr->lang = NULL; text_ptr->lang_key = NULL; #endif text_ptr->text = text; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_free(png_ptr, key); png_free(png_ptr, text_ptr); if (ret) png_warning(png_ptr, "Insufficient memory to store text chunk."); } } #endif #ifdef PNG_READ_iTXt_SUPPORTED void /* PRIVATE */ png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND)) { png_error(png_ptr, "Out of place iTXt"); info_ptr = info_ptr; /* To quiet some compiler warnings */ } #ifdef PNG_MAX_MALLOC_64K png_ptr->skip_length = 0; /* This may not be necessary */ if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */ { png_warning(png_ptr, "iTXt chunk too large to fit in memory"); png_ptr->skip_length = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_ptr->current_text = (png_charp)png_malloc(png_ptr, (png_uint_32)(length + 1)); png_ptr->current_text[length] = '\0'; png_ptr->current_text_ptr = png_ptr->current_text; png_ptr->current_text_size = (png_size_t)length; png_ptr->current_text_left = (png_size_t)length; png_ptr->process_mode = PNG_READ_iTXt_MODE; } void /* PRIVATE */ png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->buffer_size && png_ptr->current_text_left) { png_size_t text_size; if (png_ptr->buffer_size < png_ptr->current_text_left) text_size = png_ptr->buffer_size; else text_size = png_ptr->current_text_left; png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); png_ptr->current_text_left -= text_size; png_ptr->current_text_ptr += text_size; } if (!(png_ptr->current_text_left)) { png_textp text_ptr; png_charp key; int comp_flag; png_charp lang; png_charp lang_key; png_charp text; int ret; if (png_ptr->buffer_size < 4) { png_push_save_buffer(png_ptr); return; } png_push_crc_finish(png_ptr); #ifdef PNG_MAX_MALLOC_64K if (png_ptr->skip_length) return; #endif key = png_ptr->current_text; for (lang = key; *lang; lang++) /* Empty loop */ ; if (lang < key + png_ptr->current_text_size - 3) lang++; comp_flag = *lang++; lang++; /* Skip comp_type, always zero */ for (lang_key = lang; *lang_key; lang_key++) /* Empty loop */ ; lang_key++; /* Skip NUL separator */ text=lang_key; if (lang_key < key + png_ptr->current_text_size - 1) { for (; *text; text++) /* Empty loop */ ; } if (text < key + png_ptr->current_text_size) text++; text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)png_sizeof(png_text)); text_ptr->compression = comp_flag + 2; text_ptr->key = key; text_ptr->lang = lang; text_ptr->lang_key = lang_key; text_ptr->text = text; text_ptr->text_length = 0; text_ptr->itxt_length = png_strlen(text); ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); png_ptr->current_text = NULL; png_free(png_ptr, text_ptr); if (ret) png_warning(png_ptr, "Insufficient memory to store iTXt chunk."); } } #endif /* This function is called when we haven't found a handler for this * chunk. If there isn't a problem with the chunk itself (ie a bad chunk * name or a critical chunk), the chunk is (currently) silently ignored. */ void /* PRIVATE */ png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_uint_32 skip = 0; if (!(png_ptr->chunk_name[0] & 0x20)) { #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != PNG_HANDLE_CHUNK_ALWAYS #ifdef PNG_READ_USER_CHUNKS_SUPPORTED && png_ptr->read_user_chunk_fn == NULL #endif ) #endif png_chunk_error(png_ptr, "unknown critical chunk"); info_ptr = info_ptr; /* To quiet some compiler warnings */ } #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) { #ifdef PNG_MAX_MALLOC_64K if (length > (png_uint_32)65535L) { png_warning(png_ptr, "unknown chunk too large to fit in memory"); skip = length - (png_uint_32)65535L; length = (png_uint_32)65535L; } #endif png_memcpy((png_charp)png_ptr->unknown_chunk.name, (png_charp)png_ptr->chunk_name, png_sizeof(png_ptr->unknown_chunk.name)); png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1] = '\0'; png_ptr->unknown_chunk.size = (png_size_t)length; if (length == 0) png_ptr->unknown_chunk.data = NULL; else { png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, (png_uint_32)length); png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length); } #ifdef PNG_READ_USER_CHUNKS_SUPPORTED if (png_ptr->read_user_chunk_fn != NULL) { /* Callback to user unknown chunk handler */ int ret; ret = (*(png_ptr->read_user_chunk_fn)) (png_ptr, &png_ptr->unknown_chunk); if (ret < 0) png_chunk_error(png_ptr, "error in user chunk"); if (ret == 0) { if (!(png_ptr->chunk_name[0] & 0x20)) if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != PNG_HANDLE_CHUNK_ALWAYS) png_chunk_error(png_ptr, "unknown critical chunk"); png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); } } else #endif png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); png_free(png_ptr, png_ptr->unknown_chunk.data); png_ptr->unknown_chunk.data = NULL; } else #endif skip=length; png_push_crc_skip(png_ptr, skip); } void /* PRIVATE */ png_push_have_info(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->info_fn != NULL) (*(png_ptr->info_fn))(png_ptr, info_ptr); } void /* PRIVATE */ png_push_have_end(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->end_fn != NULL) (*(png_ptr->end_fn))(png_ptr, info_ptr); } void /* PRIVATE */ png_push_have_row(png_structp png_ptr, png_bytep row) { if (png_ptr->row_fn != NULL) (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, (int)png_ptr->pass); } void PNGAPI png_progressive_combine_row (png_structp png_ptr, png_bytep old_row, png_bytep new_row) { #ifdef PNG_USE_LOCAL_ARRAYS PNG_CONST int FARDATA png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; #endif if (png_ptr == NULL) return; if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */ png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); } void PNGAPI png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn) { if (png_ptr == NULL) return; png_ptr->info_fn = info_fn; png_ptr->row_fn = row_fn; png_ptr->end_fn = end_fn; png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); } png_voidp PNGAPI png_get_progressive_ptr(png_structp png_ptr) { if (png_ptr == NULL) return (NULL); return png_ptr->io_ptr; } #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ glmark2-2012.08/./src/libpng/pngget.c0000664000175000017500000005727612013417376016431 0ustar alfalf00000000000000 /* pngget.c - retrieval of values from info struct * * Last changed in libpng 1.2.43 [February 25, 2010] * Copyright (c) 1998-2010 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) png_uint_32 PNGAPI png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->valid & flag); else return(0); } png_uint_32 PNGAPI png_get_rowbytes(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->rowbytes); else return(0); } #ifdef PNG_INFO_IMAGE_SUPPORTED png_bytepp PNGAPI png_get_rows(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->row_pointers); else return(0); } #endif #ifdef PNG_EASY_ACCESS_SUPPORTED /* Easy access to info, added in libpng-0.99 */ png_uint_32 PNGAPI png_get_image_width(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->width; return (0); } png_uint_32 PNGAPI png_get_image_height(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->height; return (0); } png_byte PNGAPI png_get_bit_depth(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->bit_depth; return (0); } png_byte PNGAPI png_get_color_type(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->color_type; return (0); } png_byte PNGAPI png_get_filter_type(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->filter_type; return (0); } png_byte PNGAPI png_get_interlace_type(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->interlace_type; return (0); } png_byte PNGAPI png_get_compression_type(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->compression_type; return (0); } png_uint_32 PNGAPI png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_pHYs_SUPPORTED if (info_ptr->valid & PNG_INFO_pHYs) { png_debug1(1, "in %s retrieval function", "png_get_x_pixels_per_meter"); if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER) return (0); else return (info_ptr->x_pixels_per_unit); } #else return (0); #endif return (0); } png_uint_32 PNGAPI png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_pHYs_SUPPORTED if (info_ptr->valid & PNG_INFO_pHYs) { png_debug1(1, "in %s retrieval function", "png_get_y_pixels_per_meter"); if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER) return (0); else return (info_ptr->y_pixels_per_unit); } #else return (0); #endif return (0); } png_uint_32 PNGAPI png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_pHYs_SUPPORTED if (info_ptr->valid & PNG_INFO_pHYs) { png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter"); if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER || info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit) return (0); else return (info_ptr->x_pixels_per_unit); } #else return (0); #endif return (0); } #ifdef PNG_FLOATING_POINT_SUPPORTED float PNGAPI png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_pHYs_SUPPORTED if (info_ptr->valid & PNG_INFO_pHYs) { png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio"); if (info_ptr->x_pixels_per_unit == 0) return ((float)0.0); else return ((float)((float)info_ptr->y_pixels_per_unit /(float)info_ptr->x_pixels_per_unit)); } #else return (0.0); #endif return ((float)0.0); } #endif png_int_32 PNGAPI png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_oFFs_SUPPORTED if (info_ptr->valid & PNG_INFO_oFFs) { png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns"); if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER) return (0); else return (info_ptr->x_offset); } #else return (0); #endif return (0); } png_int_32 PNGAPI png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_oFFs_SUPPORTED if (info_ptr->valid & PNG_INFO_oFFs) { png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns"); if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER) return (0); else return (info_ptr->y_offset); } #else return (0); #endif return (0); } png_int_32 PNGAPI png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_oFFs_SUPPORTED if (info_ptr->valid & PNG_INFO_oFFs) { png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns"); if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL) return (0); else return (info_ptr->x_offset); } #else return (0); #endif return (0); } png_int_32 PNGAPI png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) #ifdef PNG_oFFs_SUPPORTED if (info_ptr->valid & PNG_INFO_oFFs) { png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns"); if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL) return (0); else return (info_ptr->y_offset); } #else return (0); #endif return (0); } #if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED) png_uint_32 PNGAPI png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) { return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr) *.0254 +.5)); } png_uint_32 PNGAPI png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) { return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr) *.0254 +.5)); } png_uint_32 PNGAPI png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr) { return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr) *.0254 +.5)); } float PNGAPI png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr) { return ((float)png_get_x_offset_microns(png_ptr, info_ptr) *.00003937); } float PNGAPI png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr) { return ((float)png_get_y_offset_microns(png_ptr, info_ptr) *.00003937); } #ifdef PNG_pHYs_SUPPORTED png_uint_32 PNGAPI png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) { png_uint_32 retval = 0; if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) { png_debug1(1, "in %s retrieval function", "pHYs"); if (res_x != NULL) { *res_x = info_ptr->x_pixels_per_unit; retval |= PNG_INFO_pHYs; } if (res_y != NULL) { *res_y = info_ptr->y_pixels_per_unit; retval |= PNG_INFO_pHYs; } if (unit_type != NULL) { *unit_type = (int)info_ptr->phys_unit_type; retval |= PNG_INFO_pHYs; if (*unit_type == 1) { if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50); if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50); } } } return (retval); } #endif /* PNG_pHYs_SUPPORTED */ #endif /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */ /* png_get_channels really belongs in here, too, but it's been around longer */ #endif /* PNG_EASY_ACCESS_SUPPORTED */ png_byte PNGAPI png_get_channels(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->channels); else return (0); } png_bytep PNGAPI png_get_signature(png_structp png_ptr, png_infop info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->signature); else return (NULL); } #ifdef PNG_bKGD_SUPPORTED png_uint_32 PNGAPI png_get_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p *background) { if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) && background != NULL) { png_debug1(1, "in %s retrieval function", "bKGD"); *background = &(info_ptr->background); return (PNG_INFO_bKGD); } return (0); } #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI png_get_cHRM(png_structp png_ptr, png_infop info_ptr, double *white_x, double *white_y, double *red_x, double *red_y, double *green_x, double *green_y, double *blue_x, double *blue_y) { if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) { png_debug1(1, "in %s retrieval function", "cHRM"); if (white_x != NULL) *white_x = (double)info_ptr->x_white; if (white_y != NULL) *white_y = (double)info_ptr->y_white; if (red_x != NULL) *red_x = (double)info_ptr->x_red; if (red_y != NULL) *red_y = (double)info_ptr->y_red; if (green_x != NULL) *green_x = (double)info_ptr->x_green; if (green_y != NULL) *green_y = (double)info_ptr->y_green; if (blue_x != NULL) *blue_x = (double)info_ptr->x_blue; if (blue_y != NULL) *blue_y = (double)info_ptr->y_blue; return (PNG_INFO_cHRM); } return (0); } #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_uint_32 PNGAPI png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x, png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y, png_fixed_point *blue_x, png_fixed_point *blue_y) { png_debug1(1, "in %s retrieval function", "cHRM"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) { if (white_x != NULL) *white_x = info_ptr->int_x_white; if (white_y != NULL) *white_y = info_ptr->int_y_white; if (red_x != NULL) *red_x = info_ptr->int_x_red; if (red_y != NULL) *red_y = info_ptr->int_y_red; if (green_x != NULL) *green_x = info_ptr->int_x_green; if (green_y != NULL) *green_y = info_ptr->int_y_green; if (blue_x != NULL) *blue_x = info_ptr->int_x_blue; if (blue_y != NULL) *blue_y = info_ptr->int_y_blue; return (PNG_INFO_cHRM); } return (0); } #endif #endif #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma) { png_debug1(1, "in %s retrieval function", "gAMA"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) && file_gamma != NULL) { *file_gamma = (double)info_ptr->gamma; return (PNG_INFO_gAMA); } return (0); } #endif #ifdef PNG_FIXED_POINT_SUPPORTED png_uint_32 PNGAPI png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point *int_file_gamma) { png_debug1(1, "in %s retrieval function", "gAMA"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) && int_file_gamma != NULL) { *int_file_gamma = info_ptr->int_gamma; return (PNG_INFO_gAMA); } return (0); } #endif #endif #ifdef PNG_sRGB_SUPPORTED png_uint_32 PNGAPI png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent) { png_debug1(1, "in %s retrieval function", "sRGB"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB) && file_srgb_intent != NULL) { *file_srgb_intent = (int)info_ptr->srgb_intent; return (PNG_INFO_sRGB); } return (0); } #endif #ifdef PNG_iCCP_SUPPORTED png_uint_32 PNGAPI png_get_iCCP(png_structp png_ptr, png_infop info_ptr, png_charpp name, int *compression_type, png_charpp profile, png_uint_32 *proflen) { png_debug1(1, "in %s retrieval function", "iCCP"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP) && name != NULL && profile != NULL && proflen != NULL) { *name = info_ptr->iccp_name; *profile = info_ptr->iccp_profile; /* Compression_type is a dummy so the API won't have to change * if we introduce multiple compression types later. */ *proflen = (int)info_ptr->iccp_proflen; *compression_type = (int)info_ptr->iccp_compression; return (PNG_INFO_iCCP); } return (0); } #endif #ifdef PNG_sPLT_SUPPORTED png_uint_32 PNGAPI png_get_sPLT(png_structp png_ptr, png_infop info_ptr, png_sPLT_tpp spalettes) { if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL) { *spalettes = info_ptr->splt_palettes; return ((png_uint_32)info_ptr->splt_palettes_num); } return (0); } #endif #ifdef PNG_hIST_SUPPORTED png_uint_32 PNGAPI png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist) { png_debug1(1, "in %s retrieval function", "hIST"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) && hist != NULL) { *hist = info_ptr->hist; return (PNG_INFO_hIST); } return (0); } #endif png_uint_32 PNGAPI png_get_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_type, int *compression_type, int *filter_type) { png_debug1(1, "in %s retrieval function", "IHDR"); if (png_ptr == NULL || info_ptr == NULL || width == NULL || height == NULL || bit_depth == NULL || color_type == NULL) return (0); *width = info_ptr->width; *height = info_ptr->height; *bit_depth = info_ptr->bit_depth; *color_type = info_ptr->color_type; if (compression_type != NULL) *compression_type = info_ptr->compression_type; if (filter_type != NULL) *filter_type = info_ptr->filter_type; if (interlace_type != NULL) *interlace_type = info_ptr->interlace_type; /* This is redundant if we can be sure that the info_ptr values were all * assigned in png_set_IHDR(). We do the check anyhow in case an * application has ignored our advice not to mess with the members * of info_ptr directly. */ png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type, info_ptr->compression_type, info_ptr->filter_type); return (1); } #ifdef PNG_oFFs_SUPPORTED png_uint_32 PNGAPI png_get_oFFs(png_structp png_ptr, png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type) { png_debug1(1, "in %s retrieval function", "oFFs"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) && offset_x != NULL && offset_y != NULL && unit_type != NULL) { *offset_x = info_ptr->x_offset; *offset_y = info_ptr->y_offset; *unit_type = (int)info_ptr->offset_unit_type; return (PNG_INFO_oFFs); } return (0); } #endif #ifdef PNG_pCAL_SUPPORTED png_uint_32 PNGAPI png_get_pCAL(png_structp png_ptr, png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams, png_charp *units, png_charpp *params) { png_debug1(1, "in %s retrieval function", "pCAL"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL && nparams != NULL && units != NULL && params != NULL) { *purpose = info_ptr->pcal_purpose; *X0 = info_ptr->pcal_X0; *X1 = info_ptr->pcal_X1; *type = (int)info_ptr->pcal_type; *nparams = (int)info_ptr->pcal_nparams; *units = info_ptr->pcal_units; *params = info_ptr->pcal_params; return (PNG_INFO_pCAL); } return (0); } #endif #ifdef PNG_sCAL_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI png_get_sCAL(png_structp png_ptr, png_infop info_ptr, int *unit, double *width, double *height) { if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) { *unit = info_ptr->scal_unit; *width = info_ptr->scal_pixel_width; *height = info_ptr->scal_pixel_height; return (PNG_INFO_sCAL); } return(0); } #else #ifdef PNG_FIXED_POINT_SUPPORTED png_uint_32 PNGAPI png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr, int *unit, png_charpp width, png_charpp height) { if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) { *unit = info_ptr->scal_unit; *width = info_ptr->scal_s_width; *height = info_ptr->scal_s_height; return (PNG_INFO_sCAL); } return(0); } #endif #endif #endif #ifdef PNG_pHYs_SUPPORTED png_uint_32 PNGAPI png_get_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) { png_uint_32 retval = 0; png_debug1(1, "in %s retrieval function", "pHYs"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) { if (res_x != NULL) { *res_x = info_ptr->x_pixels_per_unit; retval |= PNG_INFO_pHYs; } if (res_y != NULL) { *res_y = info_ptr->y_pixels_per_unit; retval |= PNG_INFO_pHYs; } if (unit_type != NULL) { *unit_type = (int)info_ptr->phys_unit_type; retval |= PNG_INFO_pHYs; } } return (retval); } #endif png_uint_32 PNGAPI png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette, int *num_palette) { png_debug1(1, "in %s retrieval function", "PLTE"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE) && palette != NULL) { *palette = info_ptr->palette; *num_palette = info_ptr->num_palette; png_debug1(3, "num_palette = %d", *num_palette); return (PNG_INFO_PLTE); } return (0); } #ifdef PNG_sBIT_SUPPORTED png_uint_32 PNGAPI png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit) { png_debug1(1, "in %s retrieval function", "sBIT"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) && sig_bit != NULL) { *sig_bit = &(info_ptr->sig_bit); return (PNG_INFO_sBIT); } return (0); } #endif #ifdef PNG_TEXT_SUPPORTED png_uint_32 PNGAPI png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr, int *num_text) { if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0) { png_debug1(1, "in %s retrieval function", (png_ptr->chunk_name[0] == '\0' ? "text" : (png_const_charp)png_ptr->chunk_name)); if (text_ptr != NULL) *text_ptr = info_ptr->text; if (num_text != NULL) *num_text = info_ptr->num_text; return ((png_uint_32)info_ptr->num_text); } if (num_text != NULL) *num_text = 0; return(0); } #endif #ifdef PNG_tIME_SUPPORTED png_uint_32 PNGAPI png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time) { png_debug1(1, "in %s retrieval function", "tIME"); if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) && mod_time != NULL) { *mod_time = &(info_ptr->mod_time); return (PNG_INFO_tIME); } return (0); } #endif #ifdef PNG_tRNS_SUPPORTED png_uint_32 PNGAPI png_get_tRNS(png_structp png_ptr, png_infop info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values) { png_uint_32 retval = 0; if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) { png_debug1(1, "in %s retrieval function", "tRNS"); if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { if (trans != NULL) { *trans = info_ptr->trans; retval |= PNG_INFO_tRNS; } if (trans_values != NULL) *trans_values = &(info_ptr->trans_values); } else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */ { if (trans_values != NULL) { *trans_values = &(info_ptr->trans_values); retval |= PNG_INFO_tRNS; } if (trans != NULL) *trans = NULL; } if (num_trans != NULL) { *num_trans = info_ptr->num_trans; retval |= PNG_INFO_tRNS; } } return (retval); } #endif #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED png_uint_32 PNGAPI png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr, png_unknown_chunkpp unknowns) { if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL) { *unknowns = info_ptr->unknown_chunks; return ((png_uint_32)info_ptr->unknown_chunks_num); } return (0); } #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED png_byte PNGAPI png_get_rgb_to_gray_status (png_structp png_ptr) { return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0); } #endif #ifdef PNG_USER_CHUNKS_SUPPORTED png_voidp PNGAPI png_get_user_chunk_ptr(png_structp png_ptr) { return (png_ptr? png_ptr->user_chunk_ptr : NULL); } #endif png_uint_32 PNGAPI png_get_compression_buffer_size(png_structp png_ptr) { return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L); } #ifdef PNG_ASSEMBLER_CODE_SUPPORTED #ifndef PNG_1_0_X /* This function was added to libpng 1.2.0 and should exist by default */ png_uint_32 PNGAPI png_get_asm_flags (png_structp png_ptr) { /* Obsolete, to be removed from libpng-1.4.0 */ return (png_ptr? 0L: 0L); } /* This function was added to libpng 1.2.0 and should exist by default */ png_uint_32 PNGAPI png_get_asm_flagmask (int flag_select) { /* Obsolete, to be removed from libpng-1.4.0 */ flag_select=flag_select; return 0L; } /* GRR: could add this: && defined(PNG_MMX_CODE_SUPPORTED) */ /* This function was added to libpng 1.2.0 */ png_uint_32 PNGAPI png_get_mmx_flagmask (int flag_select, int *compilerID) { /* Obsolete, to be removed from libpng-1.4.0 */ flag_select=flag_select; *compilerID = -1; /* unknown (i.e., no asm/MMX code compiled) */ return 0L; } /* This function was added to libpng 1.2.0 */ png_byte PNGAPI png_get_mmx_bitdepth_threshold (png_structp png_ptr) { /* Obsolete, to be removed from libpng-1.4.0 */ return (png_ptr? 0: 0); } /* This function was added to libpng 1.2.0 */ png_uint_32 PNGAPI png_get_mmx_rowbytes_threshold (png_structp png_ptr) { /* Obsolete, to be removed from libpng-1.4.0 */ return (png_ptr? 0L: 0L); } #endif /* ?PNG_1_0_X */ #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED /* These functions were added to libpng 1.2.6 but not enabled * by default. They will be enabled in libpng-1.4.0 */ png_uint_32 PNGAPI png_get_user_width_max (png_structp png_ptr) { return (png_ptr? png_ptr->user_width_max : 0); } png_uint_32 PNGAPI png_get_user_height_max (png_structp png_ptr) { return (png_ptr? png_ptr->user_height_max : 0); } #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ glmark2-2012.08/./src/libpng/pngrtran.c0000664000175000017500000044147512013417376016776 0ustar alfalf00000000000000 /* pngrtran.c - transforms the data in a row for PNG readers * * Last changed in libpng 1.2.45 [July 7, 2011] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file contains functions optionally called by an application * in order to tell libpng how to handle data when reading a PNG. * Transformations that are used in both reading and writing are * in pngtrans.c. */ #define PNG_INTERNAL #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" #ifdef PNG_READ_SUPPORTED /* Set the action on getting a CRC error for an ancillary or critical chunk. */ void PNGAPI png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) { png_debug(1, "in png_set_crc_action"); if (png_ptr == NULL) return; /* Tell libpng how we react to CRC errors in critical chunks */ switch (crit_action) { case PNG_CRC_NO_CHANGE: /* Leave setting as is */ break; case PNG_CRC_WARN_USE: /* Warn/use data */ png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; break; case PNG_CRC_QUIET_USE: /* Quiet/use data */ png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | PNG_FLAG_CRC_CRITICAL_IGNORE; break; case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ png_warning(png_ptr, "Can't discard critical data on CRC error."); case PNG_CRC_ERROR_QUIT: /* Error/quit */ case PNG_CRC_DEFAULT: default: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; break; } /* Tell libpng how we react to CRC errors in ancillary chunks */ switch (ancil_action) { case PNG_CRC_NO_CHANGE: /* Leave setting as is */ break; case PNG_CRC_WARN_USE: /* Warn/use data */ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; break; case PNG_CRC_QUIET_USE: /* Quiet/use data */ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN; break; case PNG_CRC_ERROR_QUIT: /* Error/quit */ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; break; case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ case PNG_CRC_DEFAULT: default: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; break; } } #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ defined(PNG_FLOATING_POINT_SUPPORTED) /* Handle alpha and tRNS via a background color */ void PNGAPI png_set_background(png_structp png_ptr, png_color_16p background_color, int background_gamma_code, int need_expand, double background_gamma) { png_debug(1, "in png_set_background"); if (png_ptr == NULL) return; if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) { png_warning(png_ptr, "Application must supply a known background gamma"); return; } png_ptr->transformations |= PNG_BACKGROUND; png_memcpy(&(png_ptr->background), background_color, png_sizeof(png_color_16)); png_ptr->background_gamma = (float)background_gamma; png_ptr->background_gamma_type = (png_byte)(background_gamma_code); png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0); } #endif #ifdef PNG_READ_16_TO_8_SUPPORTED /* Strip 16 bit depth files to 8 bit depth */ void PNGAPI png_set_strip_16(png_structp png_ptr) { png_debug(1, "in png_set_strip_16"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_16_TO_8; } #endif #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED void PNGAPI png_set_strip_alpha(png_structp png_ptr) { png_debug(1, "in png_set_strip_alpha"); if (png_ptr == NULL) return; png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; } #endif #ifdef PNG_READ_DITHER_SUPPORTED /* Dither file to 8 bit. Supply a palette, the current number * of elements in the palette, the maximum number of elements * allowed, and a histogram if possible. If the current number * of colors is greater then the maximum number, the palette will be * modified to fit in the maximum number. "full_dither" indicates * whether we need a dithering cube set up for RGB images, or if we * simply are reducing the number of colors in a paletted image. */ typedef struct png_dsort_struct { struct png_dsort_struct FAR * next; png_byte left; png_byte right; } png_dsort; typedef png_dsort FAR * png_dsortp; typedef png_dsort FAR * FAR * png_dsortpp; void PNGAPI png_set_dither(png_structp png_ptr, png_colorp palette, int num_palette, int maximum_colors, png_uint_16p histogram, int full_dither) { png_debug(1, "in png_set_dither"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_DITHER; if (!full_dither) { int i; png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette * png_sizeof(png_byte))); for (i = 0; i < num_palette; i++) png_ptr->dither_index[i] = (png_byte)i; } if (num_palette > maximum_colors) { if (histogram != NULL) { /* This is easy enough, just throw out the least used colors. * Perhaps not the best solution, but good enough. */ int i; /* Initialize an array to sort colors */ png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette * png_sizeof(png_byte))); /* Initialize the dither_sort array */ for (i = 0; i < num_palette; i++) png_ptr->dither_sort[i] = (png_byte)i; /* Find the least used palette entries by starting a * bubble sort, and running it until we have sorted * out enough colors. Note that we don't care about * sorting all the colors, just finding which are * least used. */ for (i = num_palette - 1; i >= maximum_colors; i--) { int done; /* To stop early if the list is pre-sorted */ int j; done = 1; for (j = 0; j < i; j++) { if (histogram[png_ptr->dither_sort[j]] < histogram[png_ptr->dither_sort[j + 1]]) { png_byte t; t = png_ptr->dither_sort[j]; png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1]; png_ptr->dither_sort[j + 1] = t; done = 0; } } if (done) break; } /* Swap the palette around, and set up a table, if necessary */ if (full_dither) { int j = num_palette; /* Put all the useful colors within the max, but don't * move the others. */ for (i = 0; i < maximum_colors; i++) { if ((int)png_ptr->dither_sort[i] >= maximum_colors) { do j--; while ((int)png_ptr->dither_sort[j] >= maximum_colors); palette[i] = palette[j]; } } } else { int j = num_palette; /* Move all the used colors inside the max limit, and * develop a translation table. */ for (i = 0; i < maximum_colors; i++) { /* Only move the colors we need to */ if ((int)png_ptr->dither_sort[i] >= maximum_colors) { png_color tmp_color; do j--; while ((int)png_ptr->dither_sort[j] >= maximum_colors); tmp_color = palette[j]; palette[j] = palette[i]; palette[i] = tmp_color; /* Indicate where the color went */ png_ptr->dither_index[j] = (png_byte)i; png_ptr->dither_index[i] = (png_byte)j; } } /* Find closest color for those colors we are not using */ for (i = 0; i < num_palette; i++) { if ((int)png_ptr->dither_index[i] >= maximum_colors) { int min_d, k, min_k, d_index; /* Find the closest color to one we threw out */ d_index = png_ptr->dither_index[i]; min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); for (k = 1, min_k = 0; k < maximum_colors; k++) { int d; d = PNG_COLOR_DIST(palette[d_index], palette[k]); if (d < min_d) { min_d = d; min_k = k; } } /* Point to closest color */ png_ptr->dither_index[i] = (png_byte)min_k; } } } png_free(png_ptr, png_ptr->dither_sort); png_ptr->dither_sort = NULL; } else { /* This is much harder to do simply (and quickly). Perhaps * we need to go through a median cut routine, but those * don't always behave themselves with only a few colors * as input. So we will just find the closest two colors, * and throw out one of them (chosen somewhat randomly). * [We don't understand this at all, so if someone wants to * work on improving it, be our guest - AED, GRP] */ int i; int max_d; int num_new_palette; png_dsortp t; png_dsortpp hash; t = NULL; /* Initialize palette index arrays */ png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette * png_sizeof(png_byte))); png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette * png_sizeof(png_byte))); /* Initialize the sort array */ for (i = 0; i < num_palette; i++) { png_ptr->index_to_palette[i] = (png_byte)i; png_ptr->palette_to_index[i] = (png_byte)i; } hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 * png_sizeof(png_dsortp))); num_new_palette = num_palette; /* Initial wild guess at how far apart the farthest pixel * pair we will be eliminating will be. Larger * numbers mean more areas will be allocated, Smaller * numbers run the risk of not saving enough data, and * having to do this all over again. * * I have not done extensive checking on this number. */ max_d = 96; while (num_new_palette > maximum_colors) { for (i = 0; i < num_new_palette - 1; i++) { int j; for (j = i + 1; j < num_new_palette; j++) { int d; d = PNG_COLOR_DIST(palette[i], palette[j]); if (d <= max_d) { t = (png_dsortp)png_malloc_warn(png_ptr, (png_uint_32)(png_sizeof(png_dsort))); if (t == NULL) break; t->next = hash[d]; t->left = (png_byte)i; t->right = (png_byte)j; hash[d] = t; } } if (t == NULL) break; } if (t != NULL) for (i = 0; i <= max_d; i++) { if (hash[i] != NULL) { png_dsortp p; for (p = hash[i]; p; p = p->next) { if ((int)png_ptr->index_to_palette[p->left] < num_new_palette && (int)png_ptr->index_to_palette[p->right] < num_new_palette) { int j, next_j; if (num_new_palette & 0x01) { j = p->left; next_j = p->right; } else { j = p->right; next_j = p->left; } num_new_palette--; palette[png_ptr->index_to_palette[j]] = palette[num_new_palette]; if (!full_dither) { int k; for (k = 0; k < num_palette; k++) { if (png_ptr->dither_index[k] == png_ptr->index_to_palette[j]) png_ptr->dither_index[k] = png_ptr->index_to_palette[next_j]; if ((int)png_ptr->dither_index[k] == num_new_palette) png_ptr->dither_index[k] = png_ptr->index_to_palette[j]; } } png_ptr->index_to_palette[png_ptr->palette_to_index [num_new_palette]] = png_ptr->index_to_palette[j]; png_ptr->palette_to_index[png_ptr->index_to_palette[j]] = png_ptr->palette_to_index[num_new_palette]; png_ptr->index_to_palette[j] = (png_byte)num_new_palette; png_ptr->palette_to_index[num_new_palette] = (png_byte)j; } if (num_new_palette <= maximum_colors) break; } if (num_new_palette <= maximum_colors) break; } } for (i = 0; i < 769; i++) { if (hash[i] != NULL) { png_dsortp p = hash[i]; while (p) { t = p->next; png_free(png_ptr, p); p = t; } } hash[i] = 0; } max_d += 96; } png_free(png_ptr, hash); png_free(png_ptr, png_ptr->palette_to_index); png_free(png_ptr, png_ptr->index_to_palette); png_ptr->palette_to_index = NULL; png_ptr->index_to_palette = NULL; } num_palette = maximum_colors; } if (png_ptr->palette == NULL) { png_ptr->palette = palette; } png_ptr->num_palette = (png_uint_16)num_palette; if (full_dither) { int i; png_bytep distance; int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS; int num_red = (1 << PNG_DITHER_RED_BITS); int num_green = (1 << PNG_DITHER_GREEN_BITS); int num_blue = (1 << PNG_DITHER_BLUE_BITS); png_size_t num_entries = ((png_size_t)1 << total_bits); png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr, (png_uint_32)(num_entries * png_sizeof(png_byte))); distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * png_sizeof(png_byte))); png_memset(distance, 0xff, num_entries * png_sizeof(png_byte)); for (i = 0; i < num_palette; i++) { int ir, ig, ib; int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS)); int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS)); int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS)); for (ir = 0; ir < num_red; ir++) { /* int dr = abs(ir - r); */ int dr = ((ir > r) ? ir - r : r - ir); int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS)); for (ig = 0; ig < num_green; ig++) { /* int dg = abs(ig - g); */ int dg = ((ig > g) ? ig - g : g - ig); int dt = dr + dg; int dm = ((dr > dg) ? dr : dg); int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS); for (ib = 0; ib < num_blue; ib++) { int d_index = index_g | ib; /* int db = abs(ib - b); */ int db = ((ib > b) ? ib - b : b - ib); int dmax = ((dm > db) ? dm : db); int d = dmax + dt + db; if (d < (int)distance[d_index]) { distance[d_index] = (png_byte)d; png_ptr->palette_lookup[d_index] = (png_byte)i; } } } } } png_free(png_ptr, distance); } } #endif #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) /* Transform the image from the file_gamma to the screen_gamma. We * only do transformations on images where the file_gamma and screen_gamma * are not close reciprocals, otherwise it slows things down slightly, and * also needlessly introduces small errors. * * We will turn off gamma transformation later if no semitransparent entries * are present in the tRNS array for palette images. We can't do it here * because we don't necessarily have the tRNS chunk yet. */ void PNGAPI png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) { png_debug(1, "in png_set_gamma"); if (png_ptr == NULL) return; if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) png_ptr->transformations |= PNG_GAMMA; png_ptr->gamma = (float)file_gamma; png_ptr->screen_gamma = (float)scrn_gamma; } #endif #ifdef PNG_READ_EXPAND_SUPPORTED /* Expand paletted images to RGB, expand grayscale images of * less than 8-bit depth to 8-bit depth, and expand tRNS chunks * to alpha channels. */ void PNGAPI png_set_expand(png_structp png_ptr) { png_debug(1, "in png_set_expand"); if (png_ptr == NULL) return; png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } /* GRR 19990627: the following three functions currently are identical * to png_set_expand(). However, it is entirely reasonable that someone * might wish to expand an indexed image to RGB but *not* expand a single, * fully transparent palette entry to a full alpha channel--perhaps instead * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace * the transparent color with a particular RGB value, or drop tRNS entirely. * IOW, a future version of the library may make the transformations flag * a bit more fine-grained, with separate bits for each of these three * functions. * * More to the point, these functions make it obvious what libpng will be * doing, whereas "expand" can (and does) mean any number of things. * * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified * to expand only the sample depth but not to expand the tRNS to alpha * and its name was changed to png_set_expand_gray_1_2_4_to_8(). */ /* Expand paletted images to RGB. */ void PNGAPI png_set_palette_to_rgb(png_structp png_ptr) { png_debug(1, "in png_set_palette_to_rgb"); if (png_ptr == NULL) return; png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } #ifndef PNG_1_0_X /* Expand grayscale images of less than 8-bit depth to 8 bits. */ void PNGAPI png_set_expand_gray_1_2_4_to_8(png_structp png_ptr) { png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); if (png_ptr == NULL) return; png_ptr->transformations |= PNG_EXPAND; png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } #endif #if defined(PNG_1_0_X) || defined(PNG_1_2_X) /* Expand grayscale images of less than 8-bit depth to 8 bits. */ /* Deprecated as of libpng-1.2.9 */ void PNGAPI png_set_gray_1_2_4_to_8(png_structp png_ptr) { png_debug(1, "in png_set_gray_1_2_4_to_8"); if (png_ptr == NULL) return; png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); } #endif /* Expand tRNS chunks to alpha channels. */ void PNGAPI png_set_tRNS_to_alpha(png_structp png_ptr) { png_debug(1, "in png_set_tRNS_to_alpha"); png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED void PNGAPI png_set_gray_to_rgb(png_structp png_ptr) { png_debug(1, "in png_set_gray_to_rgb"); png_ptr->transformations |= PNG_GRAY_TO_RGB; png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED /* Convert a RGB image to a grayscale of the same width. This allows us, * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. */ void PNGAPI png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, double green) { int red_fixed, green_fixed; if (png_ptr == NULL) return; if (red > 21474.83647 || red < -21474.83648 || green > 21474.83647 || green < -21474.83648) { png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); red_fixed = -1; green_fixed = -1; } else { red_fixed = (int)((float)red*100000.0 + 0.5); green_fixed = (int)((float)green*100000.0 + 0.5); } png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); } #endif void PNGAPI png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, png_fixed_point red, png_fixed_point green) { png_debug(1, "in png_set_rgb_to_gray"); if (png_ptr == NULL) return; switch(error_action) { case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; break; case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; break; case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) #ifdef PNG_READ_EXPAND_SUPPORTED png_ptr->transformations |= PNG_EXPAND; #else { png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); png_ptr->transformations &= ~PNG_RGB_TO_GRAY; } #endif { png_uint_16 red_int, green_int; if (red < 0 || green < 0) { red_int = 6968; /* .212671 * 32768 + .5 */ green_int = 23434; /* .715160 * 32768 + .5 */ } else if (red + green < 100000L) { red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); } else { png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); red_int = 6968; green_int = 23434; } png_ptr->rgb_to_gray_red_coeff = red_int; png_ptr->rgb_to_gray_green_coeff = green_int; png_ptr->rgb_to_gray_blue_coeff = (png_uint_16)(32768 - red_int - green_int); } } #endif #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) void PNGAPI png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr read_user_transform_fn) { png_debug(1, "in png_set_read_user_transform_fn"); if (png_ptr == NULL) return; #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED png_ptr->transformations |= PNG_USER_TRANSFORM; png_ptr->read_user_transform_fn = read_user_transform_fn; #endif #ifdef PNG_LEGACY_SUPPORTED if (read_user_transform_fn) png_warning(png_ptr, "This version of libpng does not support user transforms"); #endif } #endif /* Initialize everything needed for the read. This includes modifying * the palette. */ void /* PRIVATE */ png_init_read_transformations(png_structp png_ptr) { png_debug(1, "in png_init_read_transformations"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (png_ptr != NULL) #endif { #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_SHIFT_SUPPORTED) || \ defined(PNG_READ_GAMMA_SUPPORTED) int color_type = png_ptr->color_type; #endif #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Detect gray background and attempt to enable optimization * for gray --> RGB case * * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or * RGB_ALPHA (in which case need_expand is superfluous anyway), the * background color might actually be gray yet not be flagged as such. * This is not a problem for the current code, which uses * PNG_BACKGROUND_IS_GRAY only to decide when to do the * png_do_gray_to_rgb() transformation. */ if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && !(color_type & PNG_COLOR_MASK_COLOR)) { png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; } else if ((png_ptr->transformations & PNG_BACKGROUND) && !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && (png_ptr->transformations & PNG_GRAY_TO_RGB) && png_ptr->background.red == png_ptr->background.green && png_ptr->background.red == png_ptr->background.blue) { png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; png_ptr->background.gray = png_ptr->background.red; } #endif if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && (png_ptr->transformations & PNG_EXPAND)) { if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ { /* Expand background and tRNS chunks */ switch (png_ptr->bit_depth) { case 1: png_ptr->background.gray *= (png_uint_16)0xff; png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray; if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) { png_ptr->trans_values.gray *= (png_uint_16)0xff; png_ptr->trans_values.red = png_ptr->trans_values.green = png_ptr->trans_values.blue = png_ptr->trans_values.gray; } break; case 2: png_ptr->background.gray *= (png_uint_16)0x55; png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray; if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) { png_ptr->trans_values.gray *= (png_uint_16)0x55; png_ptr->trans_values.red = png_ptr->trans_values.green = png_ptr->trans_values.blue = png_ptr->trans_values.gray; } break; case 4: png_ptr->background.gray *= (png_uint_16)0x11; png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray; if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) { png_ptr->trans_values.gray *= (png_uint_16)0x11; png_ptr->trans_values.red = png_ptr->trans_values.green = png_ptr->trans_values.blue = png_ptr->trans_values.gray; } break; case 8: case 16: png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray; break; } } else if (color_type == PNG_COLOR_TYPE_PALETTE) { png_ptr->background.red = png_ptr->palette[png_ptr->background.index].red; png_ptr->background.green = png_ptr->palette[png_ptr->background.index].green; png_ptr->background.blue = png_ptr->palette[png_ptr->background.index].blue; #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED if (png_ptr->transformations & PNG_INVERT_ALPHA) { #ifdef PNG_READ_EXPAND_SUPPORTED if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) #endif { /* Invert the alpha channel (in tRNS) unless the pixels are * going to be expanded, in which case leave it for later */ int i, istop; istop=(int)png_ptr->num_trans; for (i=0; itrans[i] = (png_byte)(255 - png_ptr->trans[i]); } } #endif } } #endif #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) png_ptr->background_1 = png_ptr->background; #endif #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0) && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0) < PNG_GAMMA_THRESHOLD)) { int i, k; k=0; for (i=0; inum_trans; i++) { if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) k=1; /* Partial transparency is present */ } if (k == 0) png_ptr->transformations &= ~PNG_GAMMA; } if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) && png_ptr->gamma != 0.0) { png_build_gamma_table(png_ptr); #ifdef PNG_READ_BACKGROUND_SUPPORTED if (png_ptr->transformations & PNG_BACKGROUND) { if (color_type == PNG_COLOR_TYPE_PALETTE) { /* Could skip if no transparency */ png_color back, back_1; png_colorp palette = png_ptr->palette; int num_palette = png_ptr->num_palette; int i; if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) { back.red = png_ptr->gamma_table[png_ptr->background.red]; back.green = png_ptr->gamma_table[png_ptr->background.green]; back.blue = png_ptr->gamma_table[png_ptr->background.blue]; back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; } else { double g, gs; switch (png_ptr->background_gamma_type) { case PNG_BACKGROUND_GAMMA_SCREEN: g = (png_ptr->screen_gamma); gs = 1.0; break; case PNG_BACKGROUND_GAMMA_FILE: g = 1.0 / (png_ptr->gamma); gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); break; case PNG_BACKGROUND_GAMMA_UNIQUE: g = 1.0 / (png_ptr->background_gamma); gs = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); break; default: g = 1.0; /* back_1 */ gs = 1.0; /* back */ } if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD) { back.red = (png_byte)png_ptr->background.red; back.green = (png_byte)png_ptr->background.green; back.blue = (png_byte)png_ptr->background.blue; } else { back.red = (png_byte)(pow( (double)png_ptr->background.red/255, gs) * 255.0 + .5); back.green = (png_byte)(pow( (double)png_ptr->background.green/255, gs) * 255.0 + .5); back.blue = (png_byte)(pow( (double)png_ptr->background.blue/255, gs) * 255.0 + .5); } back_1.red = (png_byte)(pow( (double)png_ptr->background.red/255, g) * 255.0 + .5); back_1.green = (png_byte)(pow( (double)png_ptr->background.green/255, g) * 255.0 + .5); back_1.blue = (png_byte)(pow( (double)png_ptr->background.blue/255, g) * 255.0 + .5); } for (i = 0; i < num_palette; i++) { if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff) { if (png_ptr->trans[i] == 0) { palette[i] = back; } else /* if (png_ptr->trans[i] != 0xff) */ { png_byte v, w; v = png_ptr->gamma_to_1[palette[i].red]; png_composite(w, v, png_ptr->trans[i], back_1.red); palette[i].red = png_ptr->gamma_from_1[w]; v = png_ptr->gamma_to_1[palette[i].green]; png_composite(w, v, png_ptr->trans[i], back_1.green); palette[i].green = png_ptr->gamma_from_1[w]; v = png_ptr->gamma_to_1[palette[i].blue]; png_composite(w, v, png_ptr->trans[i], back_1.blue); palette[i].blue = png_ptr->gamma_from_1[w]; } } else { palette[i].red = png_ptr->gamma_table[palette[i].red]; palette[i].green = png_ptr->gamma_table[palette[i].green]; palette[i].blue = png_ptr->gamma_table[palette[i].blue]; } } /* Prevent the transformations being done again, and make sure * that the now spurious alpha channel is stripped - the code * has just reduced background composition and gamma correction * to a simple alpha channel strip. */ png_ptr->transformations &= ~PNG_BACKGROUND; png_ptr->transformations &= ~PNG_GAMMA; png_ptr->transformations |= PNG_STRIP_ALPHA; } /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ else /* color_type != PNG_COLOR_TYPE_PALETTE */ { double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1); double g = 1.0; double gs = 1.0; switch (png_ptr->background_gamma_type) { case PNG_BACKGROUND_GAMMA_SCREEN: g = (png_ptr->screen_gamma); gs = 1.0; break; case PNG_BACKGROUND_GAMMA_FILE: g = 1.0 / (png_ptr->gamma); gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); break; case PNG_BACKGROUND_GAMMA_UNIQUE: g = 1.0 / (png_ptr->background_gamma); gs = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); break; } png_ptr->background_1.gray = (png_uint_16)(pow( (double)png_ptr->background.gray / m, g) * m + .5); png_ptr->background.gray = (png_uint_16)(pow( (double)png_ptr->background.gray / m, gs) * m + .5); if ((png_ptr->background.red != png_ptr->background.green) || (png_ptr->background.red != png_ptr->background.blue) || (png_ptr->background.red != png_ptr->background.gray)) { /* RGB or RGBA with color background */ png_ptr->background_1.red = (png_uint_16)(pow( (double)png_ptr->background.red / m, g) * m + .5); png_ptr->background_1.green = (png_uint_16)(pow( (double)png_ptr->background.green / m, g) * m + .5); png_ptr->background_1.blue = (png_uint_16)(pow( (double)png_ptr->background.blue / m, g) * m + .5); png_ptr->background.red = (png_uint_16)(pow( (double)png_ptr->background.red / m, gs) * m + .5); png_ptr->background.green = (png_uint_16)(pow( (double)png_ptr->background.green / m, gs) * m + .5); png_ptr->background.blue = (png_uint_16)(pow( (double)png_ptr->background.blue / m, gs) * m + .5); } else { /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ png_ptr->background_1.red = png_ptr->background_1.green = png_ptr->background_1.blue = png_ptr->background_1.gray; png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = png_ptr->background.gray; } } } else /* Transformation does not include PNG_BACKGROUND */ #endif /* PNG_READ_BACKGROUND_SUPPORTED */ if (color_type == PNG_COLOR_TYPE_PALETTE) { png_colorp palette = png_ptr->palette; int num_palette = png_ptr->num_palette; int i; for (i = 0; i < num_palette; i++) { palette[i].red = png_ptr->gamma_table[palette[i].red]; palette[i].green = png_ptr->gamma_table[palette[i].green]; palette[i].blue = png_ptr->gamma_table[palette[i].blue]; } /* Done the gamma correction. */ png_ptr->transformations &= ~PNG_GAMMA; } } #ifdef PNG_READ_BACKGROUND_SUPPORTED else #endif #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */ #ifdef PNG_READ_BACKGROUND_SUPPORTED /* No GAMMA transformation */ if ((png_ptr->transformations & PNG_BACKGROUND) && (color_type == PNG_COLOR_TYPE_PALETTE)) { int i; int istop = (int)png_ptr->num_trans; png_color back; png_colorp palette = png_ptr->palette; back.red = (png_byte)png_ptr->background.red; back.green = (png_byte)png_ptr->background.green; back.blue = (png_byte)png_ptr->background.blue; for (i = 0; i < istop; i++) { if (png_ptr->trans[i] == 0) { palette[i] = back; } else if (png_ptr->trans[i] != 0xff) { /* The png_composite() macro is defined in png.h */ png_composite(palette[i].red, palette[i].red, png_ptr->trans[i], back.red); png_composite(palette[i].green, palette[i].green, png_ptr->trans[i], back.green); png_composite(palette[i].blue, palette[i].blue, png_ptr->trans[i], back.blue); } } /* Handled alpha, still need to strip the channel. */ png_ptr->transformations &= ~PNG_BACKGROUND; png_ptr->transformations |= PNG_STRIP_ALPHA; } #endif /* PNG_READ_BACKGROUND_SUPPORTED */ #ifdef PNG_READ_SHIFT_SUPPORTED if ((png_ptr->transformations & PNG_SHIFT) && (color_type == PNG_COLOR_TYPE_PALETTE)) { png_uint_16 i; png_uint_16 istop = png_ptr->num_palette; int sr = 8 - png_ptr->sig_bit.red; int sg = 8 - png_ptr->sig_bit.green; int sb = 8 - png_ptr->sig_bit.blue; if (sr < 0 || sr > 8) sr = 0; if (sg < 0 || sg > 8) sg = 0; if (sb < 0 || sb > 8) sb = 0; for (i = 0; i < istop; i++) { png_ptr->palette[i].red >>= sr; png_ptr->palette[i].green >>= sg; png_ptr->palette[i].blue >>= sb; } } #endif /* PNG_READ_SHIFT_SUPPORTED */ } #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ && !defined(PNG_READ_BACKGROUND_SUPPORTED) if (png_ptr) return; #endif } /* Modify the info structure to reflect the transformations. The * info should be updated so a PNG file could be written with it, * assuming the transformations result in valid PNG data. */ void /* PRIVATE */ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_read_transform_info"); #ifdef PNG_READ_EXPAND_SUPPORTED if (png_ptr->transformations & PNG_EXPAND) { if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { if (png_ptr->num_trans) info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; else info_ptr->color_type = PNG_COLOR_TYPE_RGB; info_ptr->bit_depth = 8; info_ptr->num_trans = 0; } else { if (png_ptr->num_trans) { if (png_ptr->transformations & PNG_EXPAND_tRNS) info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; } if (info_ptr->bit_depth < 8) info_ptr->bit_depth = 8; info_ptr->num_trans = 0; } } #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED if (png_ptr->transformations & PNG_BACKGROUND) { info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; info_ptr->num_trans = 0; info_ptr->background = png_ptr->background; } #endif #ifdef PNG_READ_GAMMA_SUPPORTED if (png_ptr->transformations & PNG_GAMMA) { #ifdef PNG_FLOATING_POINT_SUPPORTED info_ptr->gamma = png_ptr->gamma; #endif #ifdef PNG_FIXED_POINT_SUPPORTED info_ptr->int_gamma = png_ptr->int_gamma; #endif } #endif #ifdef PNG_READ_16_TO_8_SUPPORTED if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16)) info_ptr->bit_depth = 8; #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED if (png_ptr->transformations & PNG_GRAY_TO_RGB) info_ptr->color_type |= PNG_COLOR_MASK_COLOR; #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED if (png_ptr->transformations & PNG_RGB_TO_GRAY) info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; #endif #ifdef PNG_READ_DITHER_SUPPORTED if (png_ptr->transformations & PNG_DITHER) { if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && png_ptr->palette_lookup && info_ptr->bit_depth == 8) { info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; } } #endif #ifdef PNG_READ_PACK_SUPPORTED if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) info_ptr->bit_depth = 8; #endif if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) info_ptr->channels = 1; else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) info_ptr->channels = 3; else info_ptr->channels = 1; #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; #endif if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) info_ptr->channels++; #ifdef PNG_READ_FILLER_SUPPORTED /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ if ((png_ptr->transformations & PNG_FILLER) && ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) { info_ptr->channels++; /* If adding a true alpha channel not just filler */ #ifndef PNG_1_0_X if (png_ptr->transformations & PNG_ADD_ALPHA) info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; #endif } #endif #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ defined(PNG_READ_USER_TRANSFORM_SUPPORTED) if (png_ptr->transformations & PNG_USER_TRANSFORM) { if (info_ptr->bit_depth < png_ptr->user_transform_depth) info_ptr->bit_depth = png_ptr->user_transform_depth; if (info_ptr->channels < png_ptr->user_transform_channels) info_ptr->channels = png_ptr->user_transform_channels; } #endif info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); #ifndef PNG_READ_EXPAND_SUPPORTED if (png_ptr) return; #endif } /* Transform the row. The order of transformations is significant, * and is very touchy. If you add a transformation, take care to * decide how it fits in with the other transformations here. */ void /* PRIVATE */ png_do_read_transformations(png_structp png_ptr) { png_debug(1, "in png_do_read_transformations"); if (png_ptr->row_buf == NULL) { #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE) char msg[50]; png_snprintf2(msg, 50, "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number, png_ptr->pass); png_error(png_ptr, msg); #else png_error(png_ptr, "NULL row buffer"); #endif } #ifdef PNG_WARN_UNINITIALIZED_ROW if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) /* Application has failed to call either png_read_start_image() * or png_read_update_info() after setting transforms that expand * pixels. This check added to libpng-1.2.19 */ #if (PNG_WARN_UNINITIALIZED_ROW==1) png_error(png_ptr, "Uninitialized row"); #else png_warning(png_ptr, "Uninitialized row"); #endif #endif #ifdef PNG_READ_EXPAND_SUPPORTED if (png_ptr->transformations & PNG_EXPAND) { if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) { png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->palette, png_ptr->trans, png_ptr->num_trans); } else { if (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND_tRNS)) png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, &(png_ptr->trans_values)); else png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, NULL); } } #endif #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)); #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED if (png_ptr->transformations & PNG_RGB_TO_GRAY) { int rgb_error = png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); if (rgb_error) { png_ptr->rgb_to_gray_status=1; if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == PNG_RGB_TO_GRAY_WARN) png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == PNG_RGB_TO_GRAY_ERR) png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); } } #endif /* From Andreas Dilger e-mail to png-implement, 26 March 1998: * * In most cases, the "simple transparency" should be done prior to doing * gray-to-RGB, or you will have to test 3x as many bytes to check if a * pixel is transparent. You would also need to make sure that the * transparency information is upgraded to RGB. * * To summarize, the current flow is: * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite * with background "in place" if transparent, * convert to RGB if necessary * - Gray + alpha -> composite with gray background and remove alpha bytes, * convert to RGB if necessary * * To support RGB backgrounds for gray images we need: * - Gray + simple transparency -> convert to RGB + simple transparency, * compare 3 or 6 bytes and composite with * background "in place" if transparent * (3x compare/pixel compared to doing * composite with gray bkgrnd) * - Gray + alpha -> convert to RGB + alpha, composite with background and * remove alpha bytes (3x float * operations/pixel compared with composite * on gray background) * * Greg's change will do this. The reason it wasn't done before is for * performance, as this increases the per-pixel operations. If we would check * in advance if the background was gray or RGB, and position the gray-to-RGB * transform appropriately, then it would save a lot of work/time. */ #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* If gray -> RGB, do so now only if background is non-gray; else do later * for performance reasons */ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED if ((png_ptr->transformations & PNG_BACKGROUND) && ((png_ptr->num_trans != 0 ) || (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1, &(png_ptr->trans_values), &(png_ptr->background) #ifdef PNG_READ_GAMMA_SUPPORTED , &(png_ptr->background_1), png_ptr->gamma_table, png_ptr->gamma_from_1, png_ptr->gamma_to_1, png_ptr->gamma_16_table, png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1, png_ptr->gamma_shift #endif ); #endif #ifdef PNG_READ_GAMMA_SUPPORTED if ((png_ptr->transformations & PNG_GAMMA) && #ifdef PNG_READ_BACKGROUND_SUPPORTED !((png_ptr->transformations & PNG_BACKGROUND) && ((png_ptr->num_trans != 0) || (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && #endif (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->gamma_table, png_ptr->gamma_16_table, png_ptr->gamma_shift); #endif #ifdef PNG_READ_16_TO_8_SUPPORTED if (png_ptr->transformations & PNG_16_TO_8) png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_DITHER_SUPPORTED if (png_ptr->transformations & PNG_DITHER) { png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->palette_lookup, png_ptr->dither_index); if (png_ptr->row_info.rowbytes == (png_uint_32)0) png_error(png_ptr, "png_do_dither returned rowbytes=0"); } #endif #ifdef PNG_READ_INVERT_SUPPORTED if (png_ptr->transformations & PNG_INVERT_MONO) png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_SHIFT_SUPPORTED if (png_ptr->transformations & PNG_SHIFT) png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1, &(png_ptr->shift)); #endif #ifdef PNG_READ_PACK_SUPPORTED if (png_ptr->transformations & PNG_PACK) png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_BGR_SUPPORTED if (png_ptr->transformations & PNG_BGR) png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_PACKSWAP_SUPPORTED if (png_ptr->transformations & PNG_PACKSWAP) png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* If gray -> RGB, do so now only if we did not do so above */ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_FILLER_SUPPORTED if (png_ptr->transformations & PNG_FILLER) png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, (png_uint_32)png_ptr->filler, png_ptr->flags); #endif #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED if (png_ptr->transformations & PNG_INVERT_ALPHA) png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED if (png_ptr->transformations & PNG_SWAP_ALPHA) png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_SWAP_SUPPORTED if (png_ptr->transformations & PNG_SWAP_BYTES) png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); #endif #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED if (png_ptr->transformations & PNG_USER_TRANSFORM) { if (png_ptr->read_user_transform_fn != NULL) (*(png_ptr->read_user_transform_fn)) /* User read transform function */ (png_ptr, /* png_ptr */ &(png_ptr->row_info), /* row_info: */ /* png_uint_32 width; width of row */ /* png_uint_32 rowbytes; number of bytes in row */ /* png_byte color_type; color type of pixels */ /* png_byte bit_depth; bit depth of samples */ /* png_byte channels; number of channels (1-4) */ /* png_byte pixel_depth; bits per pixel (depth*channels) */ png_ptr->row_buf + 1); /* start of pixel data for row */ #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED if (png_ptr->user_transform_depth) png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; if (png_ptr->user_transform_channels) png_ptr->row_info.channels = png_ptr->user_transform_channels; #endif png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * png_ptr->row_info.channels); png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->row_info.width); } #endif } #ifdef PNG_READ_PACK_SUPPORTED /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, * without changing the actual values. Thus, if you had a row with * a bit depth of 1, you would end up with bytes that only contained * the numbers 0 or 1. If you would rather they contain 0 and 255, use * png_do_shift() after this. */ void /* PRIVATE */ png_do_unpack(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_unpack"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL && row_info->bit_depth < 8) #else if (row_info->bit_depth < 8) #endif { png_uint_32 i; png_uint_32 row_width=row_info->width; switch (row_info->bit_depth) { case 1: { png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); png_bytep dp = row + (png_size_t)row_width - 1; png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); for (i = 0; i < row_width; i++) { *dp = (png_byte)((*sp >> shift) & 0x01); if (shift == 7) { shift = 0; sp--; } else shift++; dp--; } break; } case 2: { png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); png_bytep dp = row + (png_size_t)row_width - 1; png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); for (i = 0; i < row_width; i++) { *dp = (png_byte)((*sp >> shift) & 0x03); if (shift == 6) { shift = 0; sp--; } else shift += 2; dp--; } break; } case 4: { png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); png_bytep dp = row + (png_size_t)row_width - 1; png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); for (i = 0; i < row_width; i++) { *dp = (png_byte)((*sp >> shift) & 0x0f); if (shift == 4) { shift = 0; sp--; } else shift = 4; dp--; } break; } } row_info->bit_depth = 8; row_info->pixel_depth = (png_byte)(8 * row_info->channels); row_info->rowbytes = row_width * row_info->channels; } } #endif #ifdef PNG_READ_SHIFT_SUPPORTED /* Reverse the effects of png_do_shift. This routine merely shifts the * pixels back to their significant bits values. Thus, if you have * a row of bit depth 8, but only 5 are significant, this will shift * the values back to 0 through 31. */ void /* PRIVATE */ png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits) { png_debug(1, "in png_do_unshift"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && sig_bits != NULL && #endif row_info->color_type != PNG_COLOR_TYPE_PALETTE) { int shift[4]; int channels = 0; int c; png_uint_16 value = 0; png_uint_32 row_width = row_info->width; if (row_info->color_type & PNG_COLOR_MASK_COLOR) { shift[channels++] = row_info->bit_depth - sig_bits->red; shift[channels++] = row_info->bit_depth - sig_bits->green; shift[channels++] = row_info->bit_depth - sig_bits->blue; } else { shift[channels++] = row_info->bit_depth - sig_bits->gray; } if (row_info->color_type & PNG_COLOR_MASK_ALPHA) { shift[channels++] = row_info->bit_depth - sig_bits->alpha; } for (c = 0; c < channels; c++) { if (shift[c] <= 0) shift[c] = 0; else value = 1; } if (!value) return; switch (row_info->bit_depth) { case 2: { png_bytep bp; png_uint_32 i; png_uint_32 istop = row_info->rowbytes; for (bp = row, i = 0; i < istop; i++) { *bp >>= 1; *bp++ &= 0x55; } break; } case 4: { png_bytep bp = row; png_uint_32 i; png_uint_32 istop = row_info->rowbytes; png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | (png_byte)((int)0xf >> shift[0])); for (i = 0; i < istop; i++) { *bp >>= shift[0]; *bp++ &= mask; } break; } case 8: { png_bytep bp = row; png_uint_32 i; png_uint_32 istop = row_width * channels; for (i = 0; i < istop; i++) { *bp++ >>= shift[i%channels]; } break; } case 16: { png_bytep bp = row; png_uint_32 i; png_uint_32 istop = channels * row_width; for (i = 0; i < istop; i++) { value = (png_uint_16)((*bp << 8) + *(bp + 1)); value >>= shift[i%channels]; *bp++ = (png_byte)(value >> 8); *bp++ = (png_byte)(value & 0xff); } break; } } } } #endif #ifdef PNG_READ_16_TO_8_SUPPORTED /* Chop rows of bit depth 16 down to 8 */ void /* PRIVATE */ png_do_chop(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_chop"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL && row_info->bit_depth == 16) #else if (row_info->bit_depth == 16) #endif { png_bytep sp = row; png_bytep dp = row; png_uint_32 i; png_uint_32 istop = row_info->width * row_info->channels; for (i = 0; i> 8)) >> 8; * * Approximate calculation with shift/add instead of multiply/divide: * *dp = ((((png_uint_32)(*sp) << 8) | * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; * * What we actually do to avoid extra shifting and conversion: */ *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); #else /* Simply discard the low order byte */ *dp = *sp; #endif } row_info->bit_depth = 8; row_info->pixel_depth = (png_byte)(8 * row_info->channels); row_info->rowbytes = row_info->width * row_info->channels; } } #endif #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED void /* PRIVATE */ png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_read_swap_alpha"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { png_uint_32 row_width = row_info->width; if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { /* This converts from RGBA to ARGB */ if (row_info->bit_depth == 8) { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_byte save; png_uint_32 i; for (i = 0; i < row_width; i++) { save = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = save; } } /* This converts from RRGGBBAA to AARRGGBB */ else { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_byte save[2]; png_uint_32 i; for (i = 0; i < row_width; i++) { save[0] = *(--sp); save[1] = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = save[0]; *(--dp) = save[1]; } } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { /* This converts from GA to AG */ if (row_info->bit_depth == 8) { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_byte save; png_uint_32 i; for (i = 0; i < row_width; i++) { save = *(--sp); *(--dp) = *(--sp); *(--dp) = save; } } /* This converts from GGAA to AAGG */ else { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_byte save[2]; png_uint_32 i; for (i = 0; i < row_width; i++) { save[0] = *(--sp); save[1] = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = save[0]; *(--dp) = save[1]; } } } } } #endif #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED void /* PRIVATE */ png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_read_invert_alpha"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { png_uint_32 row_width = row_info->width; if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { /* This inverts the alpha channel in RGBA */ if (row_info->bit_depth == 8) { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_uint_32 i; for (i = 0; i < row_width; i++) { *(--dp) = (png_byte)(255 - *(--sp)); /* This does nothing: *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); We can replace it with: */ sp-=3; dp=sp; } } /* This inverts the alpha channel in RRGGBBAA */ else { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_uint_32 i; for (i = 0; i < row_width; i++) { *(--dp) = (png_byte)(255 - *(--sp)); *(--dp) = (png_byte)(255 - *(--sp)); /* This does nothing: *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); We can replace it with: */ sp-=6; dp=sp; } } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { /* This inverts the alpha channel in GA */ if (row_info->bit_depth == 8) { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_uint_32 i; for (i = 0; i < row_width; i++) { *(--dp) = (png_byte)(255 - *(--sp)); *(--dp) = *(--sp); } } /* This inverts the alpha channel in GGAA */ else { png_bytep sp = row + row_info->rowbytes; png_bytep dp = sp; png_uint_32 i; for (i = 0; i < row_width; i++) { *(--dp) = (png_byte)(255 - *(--sp)); *(--dp) = (png_byte)(255 - *(--sp)); /* *(--dp) = *(--sp); *(--dp) = *(--sp); */ sp-=2; dp=sp; } } } } } #endif #ifdef PNG_READ_FILLER_SUPPORTED /* Add filler channel if we have RGB color */ void /* PRIVATE */ png_do_read_filler(png_row_infop row_info, png_bytep row, png_uint_32 filler, png_uint_32 flags) { png_uint_32 i; png_uint_32 row_width = row_info->width; png_byte hi_filler = (png_byte)((filler>>8) & 0xff); png_byte lo_filler = (png_byte)(filler & 0xff); png_debug(1, "in png_do_read_filler"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif row_info->color_type == PNG_COLOR_TYPE_GRAY) { if (row_info->bit_depth == 8) { /* This changes the data from G to GX */ if (flags & PNG_FLAG_FILLER_AFTER) { png_bytep sp = row + (png_size_t)row_width; png_bytep dp = sp + (png_size_t)row_width; for (i = 1; i < row_width; i++) { *(--dp) = lo_filler; *(--dp) = *(--sp); } *(--dp) = lo_filler; row_info->channels = 2; row_info->pixel_depth = 16; row_info->rowbytes = row_width * 2; } /* This changes the data from G to XG */ else { png_bytep sp = row + (png_size_t)row_width; png_bytep dp = sp + (png_size_t)row_width; for (i = 0; i < row_width; i++) { *(--dp) = *(--sp); *(--dp) = lo_filler; } row_info->channels = 2; row_info->pixel_depth = 16; row_info->rowbytes = row_width * 2; } } else if (row_info->bit_depth == 16) { /* This changes the data from GG to GGXX */ if (flags & PNG_FLAG_FILLER_AFTER) { png_bytep sp = row + (png_size_t)row_width * 2; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 1; i < row_width; i++) { *(--dp) = hi_filler; *(--dp) = lo_filler; *(--dp) = *(--sp); *(--dp) = *(--sp); } *(--dp) = hi_filler; *(--dp) = lo_filler; row_info->channels = 2; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; } /* This changes the data from GG to XXGG */ else { png_bytep sp = row + (png_size_t)row_width * 2; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 0; i < row_width; i++) { *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = hi_filler; *(--dp) = lo_filler; } row_info->channels = 2; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; } } } /* COLOR_TYPE == GRAY */ else if (row_info->color_type == PNG_COLOR_TYPE_RGB) { if (row_info->bit_depth == 8) { /* This changes the data from RGB to RGBX */ if (flags & PNG_FLAG_FILLER_AFTER) { png_bytep sp = row + (png_size_t)row_width * 3; png_bytep dp = sp + (png_size_t)row_width; for (i = 1; i < row_width; i++) { *(--dp) = lo_filler; *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); } *(--dp) = lo_filler; row_info->channels = 4; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; } /* This changes the data from RGB to XRGB */ else { png_bytep sp = row + (png_size_t)row_width * 3; png_bytep dp = sp + (png_size_t)row_width; for (i = 0; i < row_width; i++) { *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = lo_filler; } row_info->channels = 4; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; } } else if (row_info->bit_depth == 16) { /* This changes the data from RRGGBB to RRGGBBXX */ if (flags & PNG_FLAG_FILLER_AFTER) { png_bytep sp = row + (png_size_t)row_width * 6; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 1; i < row_width; i++) { *(--dp) = hi_filler; *(--dp) = lo_filler; *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); } *(--dp) = hi_filler; *(--dp) = lo_filler; row_info->channels = 4; row_info->pixel_depth = 64; row_info->rowbytes = row_width * 8; } /* This changes the data from RRGGBB to XXRRGGBB */ else { png_bytep sp = row + (png_size_t)row_width * 6; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 0; i < row_width; i++) { *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = hi_filler; *(--dp) = lo_filler; } row_info->channels = 4; row_info->pixel_depth = 64; row_info->rowbytes = row_width * 8; } } } /* COLOR_TYPE == RGB */ } #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand grayscale files to RGB, with or without alpha */ void /* PRIVATE */ png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) { png_uint_32 i; png_uint_32 row_width = row_info->width; png_debug(1, "in png_do_gray_to_rgb"); if (row_info->bit_depth >= 8 && #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif !(row_info->color_type & PNG_COLOR_MASK_COLOR)) { if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { if (row_info->bit_depth == 8) { png_bytep sp = row + (png_size_t)row_width - 1; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 0; i < row_width; i++) { *(dp--) = *sp; *(dp--) = *sp; *(dp--) = *(sp--); } } else { png_bytep sp = row + (png_size_t)row_width * 2 - 1; png_bytep dp = sp + (png_size_t)row_width * 4; for (i = 0; i < row_width; i++) { *(dp--) = *sp; *(dp--) = *(sp - 1); *(dp--) = *sp; *(dp--) = *(sp - 1); *(dp--) = *(sp--); *(dp--) = *(sp--); } } } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { if (row_info->bit_depth == 8) { png_bytep sp = row + (png_size_t)row_width * 2 - 1; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 0; i < row_width; i++) { *(dp--) = *(sp--); *(dp--) = *sp; *(dp--) = *sp; *(dp--) = *(sp--); } } else { png_bytep sp = row + (png_size_t)row_width * 4 - 1; png_bytep dp = sp + (png_size_t)row_width * 4; for (i = 0; i < row_width; i++) { *(dp--) = *(sp--); *(dp--) = *(sp--); *(dp--) = *sp; *(dp--) = *(sp - 1); *(dp--) = *sp; *(dp--) = *(sp - 1); *(dp--) = *(sp--); *(dp--) = *(sp--); } } } row_info->channels += (png_byte)2; row_info->color_type |= PNG_COLOR_MASK_COLOR; row_info->pixel_depth = (png_byte)(row_info->channels * row_info->bit_depth); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } } #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED /* Reduce RGB files to grayscale, with or without alpha * using the equation given in Poynton's ColorFAQ at * (THIS LINK IS DEAD June 2008) * New link: * * Charles Poynton poynton at poynton.com * * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B * * We approximate this with * * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B * * which can be expressed with integers as * * Y = (6969 * R + 23434 * G + 2365 * B)/32768 * * The calculation is to be done in a linear colorspace. * * Other integer coefficents can be used via png_set_rgb_to_gray(). */ int /* PRIVATE */ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) { png_uint_32 i; png_uint_32 row_width = row_info->width; int rgb_error = 0; png_debug(1, "in png_do_rgb_to_gray"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif (row_info->color_type & PNG_COLOR_MASK_COLOR)) { png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff; if (row_info->color_type == PNG_COLOR_TYPE_RGB) { if (row_info->bit_depth == 8) { #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_byte red = png_ptr->gamma_to_1[*(sp++)]; png_byte green = png_ptr->gamma_to_1[*(sp++)]; png_byte blue = png_ptr->gamma_to_1[*(sp++)]; if (red != green || red != blue) { rgb_error |= 1; *(dp++) = png_ptr->gamma_from_1[ (rc*red + gc*green + bc*blue)>>15]; } else *(dp++) = *(sp - 1); } } else #endif { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_byte red = *(sp++); png_byte green = *(sp++); png_byte blue = *(sp++); if (red != green || red != blue) { rgb_error |= 1; *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); } else *(dp++) = *(sp - 1); } } } else /* RGB bit_depth == 16 */ { #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, w; red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; if (red == green && red == blue) w = red; else { png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> png_ptr->gamma_shift][red>>8]; png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> png_ptr->gamma_shift][green>>8]; png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> png_ptr->gamma_shift][blue>>8]; png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 + bc*blue_1)>>15); w = png_ptr->gamma_16_from_1[(gray16&0xff) >> png_ptr->gamma_shift][gray16 >> 8]; rgb_error |= 1; } *(dp++) = (png_byte)((w>>8) & 0xff); *(dp++) = (png_byte)(w & 0xff); } } else #endif { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, gray16; red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; if (red != green || red != blue) rgb_error |= 1; gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); *(dp++) = (png_byte)((gray16>>8) & 0xff); *(dp++) = (png_byte)(gray16 & 0xff); } } } } if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { if (row_info->bit_depth == 8) { #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_byte red = png_ptr->gamma_to_1[*(sp++)]; png_byte green = png_ptr->gamma_to_1[*(sp++)]; png_byte blue = png_ptr->gamma_to_1[*(sp++)]; if (red != green || red != blue) rgb_error |= 1; *(dp++) = png_ptr->gamma_from_1 [(rc*red + gc*green + bc*blue)>>15]; *(dp++) = *(sp++); /* alpha */ } } else #endif { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_byte red = *(sp++); png_byte green = *(sp++); png_byte blue = *(sp++); if (red != green || red != blue) rgb_error |= 1; *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); *(dp++) = *(sp++); /* alpha */ } } } else /* RGBA bit_depth == 16 */ { #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, w; red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; if (red == green && red == blue) w = red; else { png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> png_ptr->gamma_shift][red>>8]; png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> png_ptr->gamma_shift][green>>8]; png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> png_ptr->gamma_shift][blue>>8]; png_uint_16 gray16 = (png_uint_16)((rc * red_1 + gc * green_1 + bc * blue_1)>>15); w = png_ptr->gamma_16_from_1[(gray16&0xff) >> png_ptr->gamma_shift][gray16 >> 8]; rgb_error |= 1; } *(dp++) = (png_byte)((w>>8) & 0xff); *(dp++) = (png_byte)(w & 0xff); *(dp++) = *(sp++); /* alpha */ *(dp++) = *(sp++); } } else #endif { png_bytep sp = row; png_bytep dp = row; for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, gray16; red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; if (red != green || red != blue) rgb_error |= 1; gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); *(dp++) = (png_byte)((gray16>>8) & 0xff); *(dp++) = (png_byte)(gray16 & 0xff); *(dp++) = *(sp++); /* alpha */ *(dp++) = *(sp++); } } } } row_info->channels -= (png_byte)2; row_info->color_type &= ~PNG_COLOR_MASK_COLOR; row_info->pixel_depth = (png_byte)(row_info->channels * row_info->bit_depth); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } return rgb_error; } #endif /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth * large of png_color. This lets grayscale images be treated as * paletted. Most useful for gamma correction and simplification * of code. */ void PNGAPI png_build_grayscale_palette(int bit_depth, png_colorp palette) { int num_palette; int color_inc; int i; int v; png_debug(1, "in png_do_build_grayscale_palette"); if (palette == NULL) return; switch (bit_depth) { case 1: num_palette = 2; color_inc = 0xff; break; case 2: num_palette = 4; color_inc = 0x55; break; case 4: num_palette = 16; color_inc = 0x11; break; case 8: num_palette = 256; color_inc = 1; break; default: num_palette = 0; color_inc = 0; break; } for (i = 0, v = 0; i < num_palette; i++, v += color_inc) { palette[i].red = (png_byte)v; palette[i].green = (png_byte)v; palette[i].blue = (png_byte)v; } } /* This function is currently unused. Do we really need it? */ #if defined(PNG_READ_DITHER_SUPPORTED) && \ defined(PNG_CORRECT_PALETTE_SUPPORTED) void /* PRIVATE */ png_correct_palette(png_structp png_ptr, png_colorp palette, int num_palette) { png_debug(1, "in png_correct_palette"); #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ defined(PNG_READ_GAMMA_SUPPORTED) && \ defined(PNG_FLOATING_POINT_SUPPORTED) if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND)) { png_color back, back_1; if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) { back.red = png_ptr->gamma_table[png_ptr->background.red]; back.green = png_ptr->gamma_table[png_ptr->background.green]; back.blue = png_ptr->gamma_table[png_ptr->background.blue]; back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; } else { double g; g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN || fabs(g - 1.0) < PNG_GAMMA_THRESHOLD) { back.red = png_ptr->background.red; back.green = png_ptr->background.green; back.blue = png_ptr->background.blue; } else { back.red = (png_byte)(pow((double)png_ptr->background.red/255, g) * 255.0 + 0.5); back.green = (png_byte)(pow((double)png_ptr->background.green/255, g) * 255.0 + 0.5); back.blue = (png_byte)(pow((double)png_ptr->background.blue/255, g) * 255.0 + 0.5); } g = 1.0 / png_ptr->background_gamma; back_1.red = (png_byte)(pow((double)png_ptr->background.red/255, g) * 255.0 + 0.5); back_1.green = (png_byte)(pow((double)png_ptr->background.green/255, g) * 255.0 + 0.5); back_1.blue = (png_byte)(pow((double)png_ptr->background.blue/255, g) * 255.0 + 0.5); } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { png_uint_32 i; for (i = 0; i < (png_uint_32)num_palette; i++) { if (i < png_ptr->num_trans && png_ptr->trans[i] == 0) { palette[i] = back; } else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff) { png_byte v, w; v = png_ptr->gamma_to_1[png_ptr->palette[i].red]; png_composite(w, v, png_ptr->trans[i], back_1.red); palette[i].red = png_ptr->gamma_from_1[w]; v = png_ptr->gamma_to_1[png_ptr->palette[i].green]; png_composite(w, v, png_ptr->trans[i], back_1.green); palette[i].green = png_ptr->gamma_from_1[w]; v = png_ptr->gamma_to_1[png_ptr->palette[i].blue]; png_composite(w, v, png_ptr->trans[i], back_1.blue); palette[i].blue = png_ptr->gamma_from_1[w]; } else { palette[i].red = png_ptr->gamma_table[palette[i].red]; palette[i].green = png_ptr->gamma_table[palette[i].green]; palette[i].blue = png_ptr->gamma_table[palette[i].blue]; } } } else { int i; for (i = 0; i < num_palette; i++) { if (palette[i].red == (png_byte)png_ptr->trans_values.gray) { palette[i] = back; } else { palette[i].red = png_ptr->gamma_table[palette[i].red]; palette[i].green = png_ptr->gamma_table[palette[i].green]; palette[i].blue = png_ptr->gamma_table[palette[i].blue]; } } } } else #endif #ifdef PNG_READ_GAMMA_SUPPORTED if (png_ptr->transformations & PNG_GAMMA) { int i; for (i = 0; i < num_palette; i++) { palette[i].red = png_ptr->gamma_table[palette[i].red]; palette[i].green = png_ptr->gamma_table[palette[i].green]; palette[i].blue = png_ptr->gamma_table[palette[i].blue]; } } #ifdef PNG_READ_BACKGROUND_SUPPORTED else #endif #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED if (png_ptr->transformations & PNG_BACKGROUND) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { png_color back; back.red = (png_byte)png_ptr->background.red; back.green = (png_byte)png_ptr->background.green; back.blue = (png_byte)png_ptr->background.blue; for (i = 0; i < (int)png_ptr->num_trans; i++) { if (png_ptr->trans[i] == 0) { palette[i].red = back.red; palette[i].green = back.green; palette[i].blue = back.blue; } else if (png_ptr->trans[i] != 0xff) { png_composite(palette[i].red, png_ptr->palette[i].red, png_ptr->trans[i], back.red); png_composite(palette[i].green, png_ptr->palette[i].green, png_ptr->trans[i], back.green); png_composite(palette[i].blue, png_ptr->palette[i].blue, png_ptr->trans[i], back.blue); } } } else /* Assume grayscale palette (what else could it be?) */ { int i; for (i = 0; i < num_palette; i++) { if (i == (png_byte)png_ptr->trans_values.gray) { palette[i].red = (png_byte)png_ptr->background.red; palette[i].green = (png_byte)png_ptr->background.green; palette[i].blue = (png_byte)png_ptr->background.blue; } } } } #endif } #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED /* Replace any alpha or transparency with the supplied background color. * "background" is already in the screen gamma, while "background_1" is * at a gamma of 1.0. Paletted files have already been taken care of. */ void /* PRIVATE */ png_do_background(png_row_infop row_info, png_bytep row, png_color_16p trans_values, png_color_16p background #ifdef PNG_READ_GAMMA_SUPPORTED , png_color_16p background_1, png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, png_uint_16pp gamma_16_to_1, int gamma_shift #endif ) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width=row_info->width; int shift; png_debug(1, "in png_do_background"); if (background != NULL && #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) || (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values))) { switch (row_info->color_type) { case PNG_COLOR_TYPE_GRAY: { switch (row_info->bit_depth) { case 1: { sp = row; shift = 7; for (i = 0; i < row_width; i++) { if ((png_uint_16)((*sp >> shift) & 0x01) == trans_values->gray) { *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); *sp |= (png_byte)(background->gray << shift); } if (!shift) { shift = 7; sp++; } else shift--; } break; } case 2: { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_table != NULL) { sp = row; shift = 6; for (i = 0; i < row_width; i++) { if ((png_uint_16)((*sp >> shift) & 0x03) == trans_values->gray) { *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); *sp |= (png_byte)(background->gray << shift); } else { png_byte p = (png_byte)((*sp >> shift) & 0x03); png_byte g = (png_byte)((gamma_table [p | (p << 2) | (p << 4) | (p << 6)] >> 6) & 0x03); *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); *sp |= (png_byte)(g << shift); } if (!shift) { shift = 6; sp++; } else shift -= 2; } } else #endif { sp = row; shift = 6; for (i = 0; i < row_width; i++) { if ((png_uint_16)((*sp >> shift) & 0x03) == trans_values->gray) { *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); *sp |= (png_byte)(background->gray << shift); } if (!shift) { shift = 6; sp++; } else shift -= 2; } } break; } case 4: { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_table != NULL) { sp = row; shift = 4; for (i = 0; i < row_width; i++) { if ((png_uint_16)((*sp >> shift) & 0x0f) == trans_values->gray) { *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); *sp |= (png_byte)(background->gray << shift); } else { png_byte p = (png_byte)((*sp >> shift) & 0x0f); png_byte g = (png_byte)((gamma_table[p | (p << 4)] >> 4) & 0x0f); *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); *sp |= (png_byte)(g << shift); } if (!shift) { shift = 4; sp++; } else shift -= 4; } } else #endif { sp = row; shift = 4; for (i = 0; i < row_width; i++) { if ((png_uint_16)((*sp >> shift) & 0x0f) == trans_values->gray) { *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); *sp |= (png_byte)(background->gray << shift); } if (!shift) { shift = 4; sp++; } else shift -= 4; } } break; } case 8: { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_table != NULL) { sp = row; for (i = 0; i < row_width; i++, sp++) { if (*sp == trans_values->gray) { *sp = (png_byte)background->gray; } else { *sp = gamma_table[*sp]; } } } else #endif { sp = row; for (i = 0; i < row_width; i++, sp++) { if (*sp == trans_values->gray) { *sp = (png_byte)background->gray; } } } break; } case 16: { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_16 != NULL) { sp = row; for (i = 0; i < row_width; i++, sp += 2) { png_uint_16 v; v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); if (v == trans_values->gray) { /* Background is already in screen gamma */ *sp = (png_byte)((background->gray >> 8) & 0xff); *(sp + 1) = (png_byte)(background->gray & 0xff); } else { v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); } } } else #endif { sp = row; for (i = 0; i < row_width; i++, sp += 2) { png_uint_16 v; v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); if (v == trans_values->gray) { *sp = (png_byte)((background->gray >> 8) & 0xff); *(sp + 1) = (png_byte)(background->gray & 0xff); } } } break; } } break; } case PNG_COLOR_TYPE_RGB: { if (row_info->bit_depth == 8) { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_table != NULL) { sp = row; for (i = 0; i < row_width; i++, sp += 3) { if (*sp == trans_values->red && *(sp + 1) == trans_values->green && *(sp + 2) == trans_values->blue) { *sp = (png_byte)background->red; *(sp + 1) = (png_byte)background->green; *(sp + 2) = (png_byte)background->blue; } else { *sp = gamma_table[*sp]; *(sp + 1) = gamma_table[*(sp + 1)]; *(sp + 2) = gamma_table[*(sp + 2)]; } } } else #endif { sp = row; for (i = 0; i < row_width; i++, sp += 3) { if (*sp == trans_values->red && *(sp + 1) == trans_values->green && *(sp + 2) == trans_values->blue) { *sp = (png_byte)background->red; *(sp + 1) = (png_byte)background->green; *(sp + 2) = (png_byte)background->blue; } } } } else /* if (row_info->bit_depth == 16) */ { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_16 != NULL) { sp = row; for (i = 0; i < row_width; i++, sp += 6) { png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); if (r == trans_values->red && g == trans_values->green && b == trans_values->blue) { /* Background is already in screen gamma */ *sp = (png_byte)((background->red >> 8) & 0xff); *(sp + 1) = (png_byte)(background->red & 0xff); *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); *(sp + 3) = (png_byte)(background->green & 0xff); *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); *(sp + 5) = (png_byte)(background->blue & 0xff); } else { png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; *(sp + 2) = (png_byte)((v >> 8) & 0xff); *(sp + 3) = (png_byte)(v & 0xff); v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; *(sp + 4) = (png_byte)((v >> 8) & 0xff); *(sp + 5) = (png_byte)(v & 0xff); } } } else #endif { sp = row; for (i = 0; i < row_width; i++, sp += 6) { png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1)); png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); if (r == trans_values->red && g == trans_values->green && b == trans_values->blue) { *sp = (png_byte)((background->red >> 8) & 0xff); *(sp + 1) = (png_byte)(background->red & 0xff); *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); *(sp + 3) = (png_byte)(background->green & 0xff); *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); *(sp + 5) = (png_byte)(background->blue & 0xff); } } } } break; } case PNG_COLOR_TYPE_GRAY_ALPHA: { if (row_info->bit_depth == 8) { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_to_1 != NULL && gamma_from_1 != NULL && gamma_table != NULL) { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 2, dp++) { png_uint_16 a = *(sp + 1); if (a == 0xff) { *dp = gamma_table[*sp]; } else if (a == 0) { /* Background is already in screen gamma */ *dp = (png_byte)background->gray; } else { png_byte v, w; v = gamma_to_1[*sp]; png_composite(w, v, a, background_1->gray); *dp = gamma_from_1[w]; } } } else #endif { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 2, dp++) { png_byte a = *(sp + 1); if (a == 0xff) { *dp = *sp; } #ifdef PNG_READ_GAMMA_SUPPORTED else if (a == 0) { *dp = (png_byte)background->gray; } else { png_composite(*dp, *sp, a, background_1->gray); } #else *dp = (png_byte)background->gray; #endif } } } else /* if (png_ptr->bit_depth == 16) */ { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_16 != NULL && gamma_16_from_1 != NULL && gamma_16_to_1 != NULL) { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 4, dp += 2) { png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); if (a == (png_uint_16)0xffff) { png_uint_16 v; v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; *dp = (png_byte)((v >> 8) & 0xff); *(dp + 1) = (png_byte)(v & 0xff); } #ifdef PNG_READ_GAMMA_SUPPORTED else if (a == 0) #else else #endif { /* Background is already in screen gamma */ *dp = (png_byte)((background->gray >> 8) & 0xff); *(dp + 1) = (png_byte)(background->gray & 0xff); } #ifdef PNG_READ_GAMMA_SUPPORTED else { png_uint_16 g, v, w; g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; png_composite_16(v, g, a, background_1->gray); w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; *dp = (png_byte)((w >> 8) & 0xff); *(dp + 1) = (png_byte)(w & 0xff); } #endif } } else #endif { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 4, dp += 2) { png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); if (a == (png_uint_16)0xffff) { png_memcpy(dp, sp, 2); } #ifdef PNG_READ_GAMMA_SUPPORTED else if (a == 0) #else else #endif { *dp = (png_byte)((background->gray >> 8) & 0xff); *(dp + 1) = (png_byte)(background->gray & 0xff); } #ifdef PNG_READ_GAMMA_SUPPORTED else { png_uint_16 g, v; g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); png_composite_16(v, g, a, background_1->gray); *dp = (png_byte)((v >> 8) & 0xff); *(dp + 1) = (png_byte)(v & 0xff); } #endif } } } break; } case PNG_COLOR_TYPE_RGB_ALPHA: { if (row_info->bit_depth == 8) { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_to_1 != NULL && gamma_from_1 != NULL && gamma_table != NULL) { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 4, dp += 3) { png_byte a = *(sp + 3); if (a == 0xff) { *dp = gamma_table[*sp]; *(dp + 1) = gamma_table[*(sp + 1)]; *(dp + 2) = gamma_table[*(sp + 2)]; } else if (a == 0) { /* Background is already in screen gamma */ *dp = (png_byte)background->red; *(dp + 1) = (png_byte)background->green; *(dp + 2) = (png_byte)background->blue; } else { png_byte v, w; v = gamma_to_1[*sp]; png_composite(w, v, a, background_1->red); *dp = gamma_from_1[w]; v = gamma_to_1[*(sp + 1)]; png_composite(w, v, a, background_1->green); *(dp + 1) = gamma_from_1[w]; v = gamma_to_1[*(sp + 2)]; png_composite(w, v, a, background_1->blue); *(dp + 2) = gamma_from_1[w]; } } } else #endif { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 4, dp += 3) { png_byte a = *(sp + 3); if (a == 0xff) { *dp = *sp; *(dp + 1) = *(sp + 1); *(dp + 2) = *(sp + 2); } else if (a == 0) { *dp = (png_byte)background->red; *(dp + 1) = (png_byte)background->green; *(dp + 2) = (png_byte)background->blue; } else { png_composite(*dp, *sp, a, background->red); png_composite(*(dp + 1), *(sp + 1), a, background->green); png_composite(*(dp + 2), *(sp + 2), a, background->blue); } } } } else /* if (row_info->bit_depth == 16) */ { #ifdef PNG_READ_GAMMA_SUPPORTED if (gamma_16 != NULL && gamma_16_from_1 != NULL && gamma_16_to_1 != NULL) { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 8, dp += 6) { png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) + (png_uint_16)(*(sp + 7))); if (a == (png_uint_16)0xffff) { png_uint_16 v; v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; *dp = (png_byte)((v >> 8) & 0xff); *(dp + 1) = (png_byte)(v & 0xff); v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; *(dp + 2) = (png_byte)((v >> 8) & 0xff); *(dp + 3) = (png_byte)(v & 0xff); v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; *(dp + 4) = (png_byte)((v >> 8) & 0xff); *(dp + 5) = (png_byte)(v & 0xff); } else if (a == 0) { /* Background is already in screen gamma */ *dp = (png_byte)((background->red >> 8) & 0xff); *(dp + 1) = (png_byte)(background->red & 0xff); *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); *(dp + 3) = (png_byte)(background->green & 0xff); *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); *(dp + 5) = (png_byte)(background->blue & 0xff); } else { png_uint_16 v, w, x; v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; png_composite_16(w, v, a, background_1->red); x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; *dp = (png_byte)((x >> 8) & 0xff); *(dp + 1) = (png_byte)(x & 0xff); v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; png_composite_16(w, v, a, background_1->green); x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; *(dp + 2) = (png_byte)((x >> 8) & 0xff); *(dp + 3) = (png_byte)(x & 0xff); v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; png_composite_16(w, v, a, background_1->blue); x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8]; *(dp + 4) = (png_byte)((x >> 8) & 0xff); *(dp + 5) = (png_byte)(x & 0xff); } } } else #endif { sp = row; dp = row; for (i = 0; i < row_width; i++, sp += 8, dp += 6) { png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) + (png_uint_16)(*(sp + 7))); if (a == (png_uint_16)0xffff) { png_memcpy(dp, sp, 6); } else if (a == 0) { *dp = (png_byte)((background->red >> 8) & 0xff); *(dp + 1) = (png_byte)(background->red & 0xff); *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); *(dp + 3) = (png_byte)(background->green & 0xff); *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); *(dp + 5) = (png_byte)(background->blue & 0xff); } else { png_uint_16 v; png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) + *(sp + 3)); png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) + *(sp + 5)); png_composite_16(v, r, a, background->red); *dp = (png_byte)((v >> 8) & 0xff); *(dp + 1) = (png_byte)(v & 0xff); png_composite_16(v, g, a, background->green); *(dp + 2) = (png_byte)((v >> 8) & 0xff); *(dp + 3) = (png_byte)(v & 0xff); png_composite_16(v, b, a, background->blue); *(dp + 4) = (png_byte)((v >> 8) & 0xff); *(dp + 5) = (png_byte)(v & 0xff); } } } } break; } } if (row_info->color_type & PNG_COLOR_MASK_ALPHA) { row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; row_info->channels--; row_info->pixel_depth = (png_byte)(row_info->channels * row_info->bit_depth); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } } } #endif #ifdef PNG_READ_GAMMA_SUPPORTED /* Gamma correct the image, avoiding the alpha channel. Make sure * you do this after you deal with the transparency issue on grayscale * or RGB images. If your bit depth is 8, use gamma_table, if it * is 16, use gamma_16_table and gamma_shift. Build these with * build_gamma_table(). */ void /* PRIVATE */ png_do_gamma(png_row_infop row_info, png_bytep row, png_bytep gamma_table, png_uint_16pp gamma_16_table, int gamma_shift) { png_bytep sp; png_uint_32 i; png_uint_32 row_width=row_info->width; png_debug(1, "in png_do_gamma"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif ((row_info->bit_depth <= 8 && gamma_table != NULL) || (row_info->bit_depth == 16 && gamma_16_table != NULL))) { switch (row_info->color_type) { case PNG_COLOR_TYPE_RGB: { if (row_info->bit_depth == 8) { sp = row; for (i = 0; i < row_width; i++) { *sp = gamma_table[*sp]; sp++; *sp = gamma_table[*sp]; sp++; *sp = gamma_table[*sp]; sp++; } } else /* if (row_info->bit_depth == 16) */ { sp = row; for (i = 0; i < row_width; i++) { png_uint_16 v; v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; } } break; } case PNG_COLOR_TYPE_RGB_ALPHA: { if (row_info->bit_depth == 8) { sp = row; for (i = 0; i < row_width; i++) { *sp = gamma_table[*sp]; sp++; *sp = gamma_table[*sp]; sp++; *sp = gamma_table[*sp]; sp++; sp++; } } else /* if (row_info->bit_depth == 16) */ { sp = row; for (i = 0; i < row_width; i++) { png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 4; } } break; } case PNG_COLOR_TYPE_GRAY_ALPHA: { if (row_info->bit_depth == 8) { sp = row; for (i = 0; i < row_width; i++) { *sp = gamma_table[*sp]; sp += 2; } } else /* if (row_info->bit_depth == 16) */ { sp = row; for (i = 0; i < row_width; i++) { png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 4; } } break; } case PNG_COLOR_TYPE_GRAY: { if (row_info->bit_depth == 2) { sp = row; for (i = 0; i < row_width; i += 4) { int a = *sp & 0xc0; int b = *sp & 0x30; int c = *sp & 0x0c; int d = *sp & 0x03; *sp = (png_byte)( ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); sp++; } } if (row_info->bit_depth == 4) { sp = row; for (i = 0; i < row_width; i += 2) { int msb = *sp & 0xf0; int lsb = *sp & 0x0f; *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); sp++; } } else if (row_info->bit_depth == 8) { sp = row; for (i = 0; i < row_width; i++) { *sp = gamma_table[*sp]; sp++; } } else if (row_info->bit_depth == 16) { sp = row; for (i = 0; i < row_width; i++) { png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); sp += 2; } } break; } } } } #endif #ifdef PNG_READ_EXPAND_SUPPORTED /* Expands a palette row to an RGB or RGBA row depending * upon whether you supply trans and num_trans. */ void /* PRIVATE */ png_do_expand_palette(png_row_infop row_info, png_bytep row, png_colorp palette, png_bytep trans, int num_trans) { int shift, value; png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width=row_info->width; png_debug(1, "in png_do_expand_palette"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif row_info->color_type == PNG_COLOR_TYPE_PALETTE) { if (row_info->bit_depth < 8) { switch (row_info->bit_depth) { case 1: { sp = row + (png_size_t)((row_width - 1) >> 3); dp = row + (png_size_t)row_width - 1; shift = 7 - (int)((row_width + 7) & 0x07); for (i = 0; i < row_width; i++) { if ((*sp >> shift) & 0x01) *dp = 1; else *dp = 0; if (shift == 7) { shift = 0; sp--; } else shift++; dp--; } break; } case 2: { sp = row + (png_size_t)((row_width - 1) >> 2); dp = row + (png_size_t)row_width - 1; shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); for (i = 0; i < row_width; i++) { value = (*sp >> shift) & 0x03; *dp = (png_byte)value; if (shift == 6) { shift = 0; sp--; } else shift += 2; dp--; } break; } case 4: { sp = row + (png_size_t)((row_width - 1) >> 1); dp = row + (png_size_t)row_width - 1; shift = (int)((row_width & 0x01) << 2); for (i = 0; i < row_width; i++) { value = (*sp >> shift) & 0x0f; *dp = (png_byte)value; if (shift == 4) { shift = 0; sp--; } else shift += 4; dp--; } break; } } row_info->bit_depth = 8; row_info->pixel_depth = 8; row_info->rowbytes = row_width; } switch (row_info->bit_depth) { case 8: { if (trans != NULL) { sp = row + (png_size_t)row_width - 1; dp = row + (png_size_t)(row_width << 2) - 1; for (i = 0; i < row_width; i++) { if ((int)(*sp) >= num_trans) *dp-- = 0xff; else *dp-- = trans[*sp]; *dp-- = palette[*sp].blue; *dp-- = palette[*sp].green; *dp-- = palette[*sp].red; sp--; } row_info->bit_depth = 8; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; row_info->color_type = 6; row_info->channels = 4; } else { sp = row + (png_size_t)row_width - 1; dp = row + (png_size_t)(row_width * 3) - 1; for (i = 0; i < row_width; i++) { *dp-- = palette[*sp].blue; *dp-- = palette[*sp].green; *dp-- = palette[*sp].red; sp--; } row_info->bit_depth = 8; row_info->pixel_depth = 24; row_info->rowbytes = row_width * 3; row_info->color_type = 2; row_info->channels = 3; } break; } } } } /* If the bit depth < 8, it is expanded to 8. Also, if the already * expanded transparency value is supplied, an alpha channel is built. */ void /* PRIVATE */ png_do_expand(png_row_infop row_info, png_bytep row, png_color_16p trans_value) { int shift, value; png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width=row_info->width; png_debug(1, "in png_do_expand"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0); if (row_info->bit_depth < 8) { switch (row_info->bit_depth) { case 1: { gray = (png_uint_16)((gray&0x01)*0xff); sp = row + (png_size_t)((row_width - 1) >> 3); dp = row + (png_size_t)row_width - 1; shift = 7 - (int)((row_width + 7) & 0x07); for (i = 0; i < row_width; i++) { if ((*sp >> shift) & 0x01) *dp = 0xff; else *dp = 0; if (shift == 7) { shift = 0; sp--; } else shift++; dp--; } break; } case 2: { gray = (png_uint_16)((gray&0x03)*0x55); sp = row + (png_size_t)((row_width - 1) >> 2); dp = row + (png_size_t)row_width - 1; shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); for (i = 0; i < row_width; i++) { value = (*sp >> shift) & 0x03; *dp = (png_byte)(value | (value << 2) | (value << 4) | (value << 6)); if (shift == 6) { shift = 0; sp--; } else shift += 2; dp--; } break; } case 4: { gray = (png_uint_16)((gray&0x0f)*0x11); sp = row + (png_size_t)((row_width - 1) >> 1); dp = row + (png_size_t)row_width - 1; shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); for (i = 0; i < row_width; i++) { value = (*sp >> shift) & 0x0f; *dp = (png_byte)(value | (value << 4)); if (shift == 4) { shift = 0; sp--; } else shift = 4; dp--; } break; } } row_info->bit_depth = 8; row_info->pixel_depth = 8; row_info->rowbytes = row_width; } if (trans_value != NULL) { if (row_info->bit_depth == 8) { gray = gray & 0xff; sp = row + (png_size_t)row_width - 1; dp = row + (png_size_t)(row_width << 1) - 1; for (i = 0; i < row_width; i++) { if (*sp == gray) *dp-- = 0; else *dp-- = 0xff; *dp-- = *sp--; } } else if (row_info->bit_depth == 16) { png_byte gray_high = (gray >> 8) & 0xff; png_byte gray_low = gray & 0xff; sp = row + row_info->rowbytes - 1; dp = row + (row_info->rowbytes << 1) - 1; for (i = 0; i < row_width; i++) { if (*(sp - 1) == gray_high && *(sp) == gray_low) { *dp-- = 0; *dp-- = 0; } else { *dp-- = 0xff; *dp-- = 0xff; } *dp-- = *sp--; *dp-- = *sp--; } } row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; row_info->channels = 2; row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } } else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) { if (row_info->bit_depth == 8) { png_byte red = trans_value->red & 0xff; png_byte green = trans_value->green & 0xff; png_byte blue = trans_value->blue & 0xff; sp = row + (png_size_t)row_info->rowbytes - 1; dp = row + (png_size_t)(row_width << 2) - 1; for (i = 0; i < row_width; i++) { if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) *dp-- = 0; else *dp-- = 0xff; *dp-- = *sp--; *dp-- = *sp--; *dp-- = *sp--; } } else if (row_info->bit_depth == 16) { png_byte red_high = (trans_value->red >> 8) & 0xff; png_byte green_high = (trans_value->green >> 8) & 0xff; png_byte blue_high = (trans_value->blue >> 8) & 0xff; png_byte red_low = trans_value->red & 0xff; png_byte green_low = trans_value->green & 0xff; png_byte blue_low = trans_value->blue & 0xff; sp = row + row_info->rowbytes - 1; dp = row + (png_size_t)(row_width << 3) - 1; for (i = 0; i < row_width; i++) { if (*(sp - 5) == red_high && *(sp - 4) == red_low && *(sp - 3) == green_high && *(sp - 2) == green_low && *(sp - 1) == blue_high && *(sp ) == blue_low) { *dp-- = 0; *dp-- = 0; } else { *dp-- = 0xff; *dp-- = 0xff; } *dp-- = *sp--; *dp-- = *sp--; *dp-- = *sp--; *dp-- = *sp--; *dp-- = *sp--; *dp-- = *sp--; } } row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; row_info->channels = 4; row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } } } #endif #ifdef PNG_READ_DITHER_SUPPORTED void /* PRIVATE */ png_do_dither(png_row_infop row_info, png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup) { png_bytep sp, dp; png_uint_32 i; png_uint_32 row_width=row_info->width; png_debug(1, "in png_do_dither"); #ifdef PNG_USELESS_TESTS_SUPPORTED if (row != NULL && row_info != NULL) #endif { if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup && row_info->bit_depth == 8) { int r, g, b, p; sp = row; dp = row; for (i = 0; i < row_width; i++) { r = *sp++; g = *sp++; b = *sp++; /* This looks real messy, but the compiler will reduce * it down to a reasonable formula. For example, with * 5 bits per color, we get: * p = (((r >> 3) & 0x1f) << 10) | * (((g >> 3) & 0x1f) << 5) | * ((b >> 3) & 0x1f); */ p = (((r >> (8 - PNG_DITHER_RED_BITS)) & ((1 << PNG_DITHER_RED_BITS) - 1)) << (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & ((1 << PNG_DITHER_GREEN_BITS) - 1)) << (PNG_DITHER_BLUE_BITS)) | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & ((1 << PNG_DITHER_BLUE_BITS) - 1)); *dp++ = palette_lookup[p]; } row_info->color_type = PNG_COLOR_TYPE_PALETTE; row_info->channels = 1; row_info->pixel_depth = row_info->bit_depth; row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && palette_lookup != NULL && row_info->bit_depth == 8) { int r, g, b, p; sp = row; dp = row; for (i = 0; i < row_width; i++) { r = *sp++; g = *sp++; b = *sp++; sp++; p = (((r >> (8 - PNG_DITHER_RED_BITS)) & ((1 << PNG_DITHER_RED_BITS) - 1)) << (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & ((1 << PNG_DITHER_GREEN_BITS) - 1)) << (PNG_DITHER_BLUE_BITS)) | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & ((1 << PNG_DITHER_BLUE_BITS) - 1)); *dp++ = palette_lookup[p]; } row_info->color_type = PNG_COLOR_TYPE_PALETTE; row_info->channels = 1; row_info->pixel_depth = row_info->bit_depth; row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); } else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && dither_lookup && row_info->bit_depth == 8) { sp = row; for (i = 0; i < row_width; i++, sp++) { *sp = dither_lookup[*sp]; } } } } #endif #ifdef PNG_FLOATING_POINT_SUPPORTED #ifdef PNG_READ_GAMMA_SUPPORTED static PNG_CONST int png_gamma_shift[] = {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00}; /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit * tables, we don't make a full table if we are reducing to 8-bit in * the future. Note also how the gamma_16 tables are segmented so that * we don't need to allocate > 64K chunks for a full 16-bit table. * * See the PNG extensions document for an integer algorithm for creating * the gamma tables. Maybe we will implement that here someday. * * We should only reach this point if * * the file_gamma is known (i.e., the gAMA or sRGB chunk is present, * or the application has provided a file_gamma) * * AND * { * the screen_gamma is known * OR * * RGB_to_gray transformation is being performed * } * * AND * { * the screen_gamma is different from the reciprocal of the * file_gamma by more than the specified threshold * * OR * * a background color has been specified and the file_gamma * and screen_gamma are not 1.0, within the specified threshold. * } */ void /* PRIVATE */ png_build_gamma_table(png_structp png_ptr) { png_debug(1, "in png_build_gamma_table"); if (png_ptr->bit_depth <= 8) { int i; double g; if (png_ptr->screen_gamma > .000001) g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); else g = 1.0; png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); for (i = 0; i < 256; i++) { png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, g) * 255.0 + .5); } #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) { g = 1.0 / (png_ptr->gamma); png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); for (i = 0; i < 256; i++) { png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, g) * 255.0 + .5); } png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); if (png_ptr->screen_gamma > 0.000001) g = 1.0 / png_ptr->screen_gamma; else g = png_ptr->gamma; /* Probably doing rgb_to_gray */ for (i = 0; i < 256; i++) { png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, g) * 255.0 + .5); } } #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ } else { double g; int i, j, shift, num; int sig_bit; png_uint_32 ig; if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) { sig_bit = (int)png_ptr->sig_bit.red; if ((int)png_ptr->sig_bit.green > sig_bit) sig_bit = png_ptr->sig_bit.green; if ((int)png_ptr->sig_bit.blue > sig_bit) sig_bit = png_ptr->sig_bit.blue; } else { sig_bit = (int)png_ptr->sig_bit.gray; } if (sig_bit > 0) shift = 16 - sig_bit; else shift = 0; if (png_ptr->transformations & PNG_16_TO_8) { if (shift < (16 - PNG_MAX_GAMMA_8)) shift = (16 - PNG_MAX_GAMMA_8); } if (shift > 8) shift = 8; if (shift < 0) shift = 0; png_ptr->gamma_shift = (png_byte)shift; num = (1 << (8 - shift)); if (png_ptr->screen_gamma > .000001) g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); else g = 1.0; png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr, (png_uint_32)(num * png_sizeof(png_uint_16p))); if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) { double fin, fout; png_uint_32 last, max; for (i = 0; i < num; i++) { png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(256 * png_sizeof(png_uint_16))); } g = 1.0 / g; last = 0; for (i = 0; i < 256; i++) { fout = ((double)i + 0.5) / 256.0; fin = pow(fout, g); max = (png_uint_32)(fin * (double)((png_uint_32)num << 8)); while (last <= max) { png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] [(int)(last >> (8 - shift))] = (png_uint_16)( (png_uint_16)i | ((png_uint_16)i << 8)); last++; } } while (last < ((png_uint_32)num << 8)) { png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] [(int)(last >> (8 - shift))] = (png_uint_16)65535L; last++; } } else { for (i = 0; i < num; i++) { png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(256 * png_sizeof(png_uint_16))); ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); for (j = 0; j < 256; j++) { png_ptr->gamma_16_table[i][j] = (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 65535.0, g) * 65535.0 + .5); } } } #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) { g = 1.0 / (png_ptr->gamma); png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr, (png_uint_32)(num * png_sizeof(png_uint_16p ))); for (i = 0; i < num; i++) { png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(256 * png_sizeof(png_uint_16))); ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); for (j = 0; j < 256; j++) { png_ptr->gamma_16_to_1[i][j] = (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 65535.0, g) * 65535.0 + .5); } } if (png_ptr->screen_gamma > 0.000001) g = 1.0 / png_ptr->screen_gamma; else g = png_ptr->gamma; /* Probably doing rgb_to_gray */ png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr, (png_uint_32)(num * png_sizeof(png_uint_16p))); for (i = 0; i < num; i++) { png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, (png_uint_32)(256 * png_sizeof(png_uint_16))); ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); for (j = 0; j < 256; j++) { png_ptr->gamma_16_from_1[i][j] = (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 65535.0, g) * 65535.0 + .5); } } } #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ } } #endif /* To do: install integer version of png_build_gamma_table here */ #endif #ifdef PNG_MNG_FEATURES_SUPPORTED /* Undoes intrapixel differencing */ void /* PRIVATE */ png_do_read_intrapixel(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_read_intrapixel"); if ( #ifdef PNG_USELESS_TESTS_SUPPORTED row != NULL && row_info != NULL && #endif (row_info->color_type & PNG_COLOR_MASK_COLOR)) { int bytes_per_pixel; png_uint_32 row_width = row_info->width; if (row_info->bit_depth == 8) { png_bytep rp; png_uint_32 i; if (row_info->color_type == PNG_COLOR_TYPE_RGB) bytes_per_pixel = 3; else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) bytes_per_pixel = 4; else return; for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) { *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); } } else if (row_info->bit_depth == 16) { png_bytep rp; png_uint_32 i; if (row_info->color_type == PNG_COLOR_TYPE_RGB) bytes_per_pixel = 6; else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) bytes_per_pixel = 8; else return; for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) { png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL); png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL); *(rp ) = (png_byte)((red >> 8) & 0xff); *(rp+1) = (png_byte)(red & 0xff); *(rp+4) = (png_byte)((blue >> 8) & 0xff); *(rp+5) = (png_byte)(blue & 0xff); } } } } #endif /* PNG_MNG_FEATURES_SUPPORTED */ #endif /* PNG_READ_SUPPORTED */ glmark2-2012.08/./src/libpng/README0000664000175000017500000003441412013417376015646 0ustar alfalf00000000000000README for libpng version 1.2.46 - July 9, 2011 (shared library 12.0) See the note about version numbers near the top of png.h See INSTALL for instructions on how to install libpng. Libpng comes in several distribution formats. Get libpng-*.tar.gz, libpng-*.tar.xz, or libpng-*.tar.bz2 if you want UNIX-style line endings in the text files, or lpng*.7z or lpng*.zip if you want DOS-style line endings. You can get UNIX-style line endings from the *.zip file by using "unzip -a" but there seems to be no simple way to recover UNIX-style line endings from the *.7z file. The *.tar.xz file is recommended for *NIX users instead. Version 0.89 was the first official release of libpng. Don't let the fact that it's the first release fool you. The libpng library has been in extensive use and testing since mid-1995. By late 1997 it had finally gotten to the stage where there hadn't been significant changes to the API in some time, and people have a bad feeling about libraries with versions < 1.0. Version 1.0.0 was released in March 1998. **** Note that some of the changes to the png_info structure render this version of the library binary incompatible with libpng-0.89 or earlier versions if you are using a shared library. The type of the "filler" parameter for png_set_filler() has changed from png_byte to png_uint_32, which will affect shared-library applications that use this function. To avoid problems with changes to the internals of png_info_struct, new APIs have been made available in 0.95 to avoid direct application access to info_ptr. These functions are the png_set_ and png_get_ functions. These functions should be used when accessing/storing the info_struct data, rather than manipulating it directly, to avoid such problems in the future. It is important to note that the APIs do not make current programs that access the info struct directly incompatible with the new library. However, it is strongly suggested that new programs use the new APIs (as shown in example.c and pngtest.c), and older programs be converted to the new format, to facilitate upgrades in the future. **** Additions since 0.90 include the ability to compile libpng as a Windows DLL, and new APIs for accessing data in the info struct. Experimental functions include the ability to set weighting and cost factors for row filter selection, direct reads of integers from buffers on big-endian processors that support misaligned data access, faster methods of doing alpha composition, and more accurate 16->8 bit color conversion. The additions since 0.89 include the ability to read from a PNG stream which has had some (or all) of the signature bytes read by the calling application. This also allows the reading of embedded PNG streams that do not have the PNG file signature. As well, it is now possible to set the library action on the detection of chunk CRC errors. It is possible to set different actions based on whether the CRC error occurred in a critical or an ancillary chunk. The changes made to the library, and bugs fixed are based on discussions on the png-mng-implement mailing list and not on material submitted privately to Guy, Andreas, or Glenn. They will forward any good suggestions to the list. For a detailed description on using libpng, read libpng.txt. For examples of libpng in a program, see example.c and pngtest.c. For usage information and restrictions (what little they are) on libpng, see png.h. For a description on using zlib (the compression library used by libpng) and zlib's restrictions, see zlib.h I have included a general makefile, as well as several machine and compiler specific ones, but you may have to modify one for your own needs. You should use zlib 1.0.4 or later to run this, but it MAY work with versions as old as zlib 0.95. Even so, there are bugs in older zlib versions which can cause the output of invalid compression streams for some images. You will definitely need zlib 1.0.4 or later if you are taking advantage of the MS-DOS "far" structure allocation for the small and medium memory models. You should also note that zlib is a compression library that is useful for more things than just PNG files. You can use zlib as a drop-in replacement for fread() and fwrite() if you are so inclined. zlib should be available at the same place that libpng is, or at ftp://ftp.simplesystems.org/pub/png/src/ You may also want a copy of the PNG specification. It is available as an RFC, a W3C Recommendation, and an ISO/IEC Standard. You can find these at http://www.libpng.org/pub/png/pngdocs.html This code is currently being archived at libpng.sf.net in the [DOWNLOAD] area, and on CompuServe, Lib 20 (PNG SUPPORT) at GO GRAPHSUP. If you can't find it in any of those places, e-mail me, and I'll help you find it. If you have any code changes, requests, problems, etc., please e-mail them to me. Also, I'd appreciate any make files or project files, and any modifications you needed to make to get libpng to compile, along with a #define variable to tell what compiler/system you are on. If you needed to add transformations to libpng, or wish libpng would provide the image in a different way, drop me a note (and code, if possible), so I can consider supporting the transformation. Finally, if you get any warning messages when compiling libpng (note: not zlib), and they are easy to fix, I'd appreciate the fix. Please mention "libpng" somewhere in the subject line. Thanks. This release was created and will be supported by myself (of course based in a large way on Guy's and Andreas' earlier work), and the PNG development group. Send comments/corrections/commendations to png-mng-implement at lists.sf.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement to subscribe) or to glennrp at users.sourceforge.net You can't reach Guy, the original libpng author, at the addresses given in previous versions of this document. He and Andreas will read mail addressed to the png-mng-implement list, however. Please do not send general questions about PNG. Send them to the (png-mng-misc at lists.sourceforge.net, subscription required, visit https://lists.sourceforge.net/lists/listinfo/png-mng-misc to subscribe). On the other hand, please do not send libpng questions to that address, send them to me or to the png-mng-implement list. I'll get them in the end anyway. If you have a question about something in the PNG specification that is related to using libpng, send it to me. Send me any questions that start with "I was using libpng, and ...". If in doubt, send questions to me. I'll bounce them to others, if necessary. Please do not send suggestions on how to change PNG. We have been discussing PNG for twelve years now, and it is official and finished. If you have suggestions for libpng, however, I'll gladly listen. Even if your suggestion is not used immediately, it may be used later. Files in this distribution: ANNOUNCE => Announcement of this version, with recent changes CHANGES => Description of changes between libpng versions KNOWNBUG => List of known bugs and deficiencies LICENSE => License to use and redistribute libpng README => This file TODO => Things not implemented in the current library Y2KINFO => Statement of Y2K compliance example.c => Example code for using libpng functions libpng-*-*-diff.txt => Diff from previous release libpng.3 => manual page for libpng (includes libpng.txt) libpng.txt => Description of libpng and its functions libpngpf.3 => manual page for libpng's private functions png.5 => manual page for the PNG format png.c => Basic interface functions common to library png.h => Library function and interface declarations pngconf.h => System specific library configuration pngerror.c => Error/warning message I/O functions pngget.c => Functions for retrieving info from struct pngmem.c => Memory handling functions pngbar.png => PNG logo, 88x31 pngnow.png => PNG logo, 98x31 pngpread.c => Progressive reading functions pngread.c => Read data/helper high-level functions pngrio.c => Lowest-level data read I/O functions pngrtran.c => Read data transformation functions pngrutil.c => Read data utility functions pngset.c => Functions for storing data into the info_struct pngtest.c => Library test program pngtest.png => Library test sample image pngtrans.c => Common data transformation functions pngwio.c => Lowest-level write I/O functions pngwrite.c => High-level write functions pngwtran.c => Write data transformations pngwutil.c => Write utility functions contrib => Contributions gregbook => source code for PNG reading and writing, from Greg Roelofs' "PNG: The Definitive Guide", O'Reilly, 1999 msvctest => Builds and runs pngtest using a MSVC workspace pngminim => Simple pnm2pngm and png2pnmm programs pngminus => Simple pnm2png and png2pnm programs pngsuite => Test images visupng => Contains a MSVC workspace for VisualPng projects => Contains project files and workspaces for building a DLL beos => Contains a Beos workspace for building libpng c5builder => Contains a Borland workspace for building libpng and zlib netware.txt => Contains instructions for downloading a set of project files for building libpng and zlib on Netware. visualc6 => Contains a Microsoft Visual C++ (MSVC) workspace for building libpng and zlib wince.txt => Contains instructions for downloading a Microsoft Visual C++ (Windows CD Toolkit) workspace for building libpng and zlib on WindowsCE xcode => Contains xcode project files scripts => Directory containing scripts for building libpng: descrip.mms => VMS makefile for MMS or MMK makefile.std => Generic UNIX makefile (cc, creates static libpng.a) makefile.elf => Linux/ELF gcc makefile symbol versioning, creates libpng12.so.0.1.2.46) makefile.linux => Linux/ELF makefile (gcc, creates libpng12.so.0.1.2.46) makefile.gcmmx => Linux/ELF makefile (gcc, creates libpng12.so.0.1.2.46, previously used assembler code tuned for Intel MMX platform) makefile.gcc => Generic makefile (gcc, creates static libpng.a) makefile.knr => Archaic UNIX Makefile that converts files with ansi2knr (Requires ansi2knr.c from ftp://ftp.cs.wisc.edu/ghost) makefile.aix => AIX makefile makefile.cygwin => Cygwin/gcc makefile makefile.darwin => Darwin makefile makefile.dec => DEC Alpha UNIX makefile makefile.freebsd => FreeBSD makefile makefile.hpgcc => HPUX makefile using gcc makefile.hpux => HPUX (10.20 and 11.00) makefile makefile.hp64 => HPUX (10.20 and 11.00) makefile, 64 bit makefile.ibmc => IBM C/C++ version 3.x for Win32 and OS/2 (static) makefile.intel => Intel C/C++ version 4.0 and later libpng.icc => Project file, IBM VisualAge/C++ 4.0 or later makefile.netbsd => NetBSD/cc makefile, makes libpng.so. makefile.ne12bsd => NetBSD/cc makefile, makes libpng12.so makefile.openbsd => OpenBSD makefile makefile.sgi => Silicon Graphics IRIX (cc, creates static lib) makefile.sggcc => Silicon Graphics (gcc, creates libpng12.so.0.1.2.46) makefile.sunos => Sun makefile makefile.solaris => Solaris 2.X makefile (gcc, creates libpng12.so.0.1.2.46) makefile.so9 => Solaris 9 makefile (gcc, creates libpng12.so.0.1.2.46) makefile.32sunu => Sun Ultra 32-bit makefile makefile.64sunu => Sun Ultra 64-bit makefile makefile.sco => For SCO OSr5 ELF and Unixware 7 with Native cc makefile.mips => MIPS makefile makefile.acorn => Acorn makefile makefile.amiga => Amiga makefile smakefile.ppc => AMIGA smakefile for SAS C V6.58/7.00 PPC compiler (Requires SCOPTIONS, copied from scripts/SCOPTIONS.ppc) makefile.atari => Atari makefile makefile.beos => BEOS makefile for X86 makefile.bor => Borland makefile (uses bcc) makefile.bc32 => 32-bit Borland C++ (all modules compiled in C mode) makefile.tc3 => Turbo C 3.0 makefile makefile.dj2 => DJGPP 2 makefile makefile.msc => Microsoft C makefile makefile.vcawin32=> makefile for Microsoft Visual C++ 5.0 and later (previously used assembler code tuned for Intel MMX platform) makefile.vcwin32 => makefile for Microsoft Visual C++ 4.0 and later (does not use assembler code) makefile.os2 => OS/2 Makefile (gcc and emx, requires pngos2.def) pngos2.def => OS/2 module definition file used by makefile.os2 makefile.watcom => Watcom 10a+ Makefile, 32-bit flat memory model makevms.com => VMS build script SCOPTIONS.ppc => Used with smakefile.ppc Good luck, and happy coding. -Glenn Randers-Pehrson (current maintainer, since 1998) Internet: glennrp at users.sourceforge.net -Andreas Eric Dilger (former maintainer, 1996-1997) Internet: adilger at enel.ucalgary.ca Web: http://members.shaw.ca/adilger/ -Guy Eric Schalnat (original author and former maintainer, 1995-1996) (formerly of Group 42, Inc) Internet: gschal at infinet.com glmark2-2012.08/./src/libpng/TODO0000664000175000017500000000231712013417376015453 0ustar alfalf00000000000000TODO - list of things to do for libpng: Final bug fixes. Improve API by hiding the png_struct and png_info structs. Finish work on the no-floating-point version (including gamma compensation) Better C++ wrapper/full C++ implementation? Fix problem with C++ and EXTERN "C". cHRM transformation. Improve setjmp/longjmp usage or remove it in favor of returning error codes. Add "grayscale->palette" transformation and "palette->grayscale" detection. Improved dithering. Multi-lingual error and warning message support. Complete sRGB transformation (presently it simply uses gamma=0.45455). Man pages for function calls. Better documentation. Better filter selection (counting huffman bits/precompression? filter inertia? filter costs?). Histogram creation. Text conversion between different code pages (Latin-1 -> Mac and DOS). Should we always malloc 2^bit_depth PLTE/tRNS/hIST entries for safety? Build gamma tables using fixed point (and do away with floating point entirely). Use greater precision when changing to linear gamma for compositing against background and doing rgb-to-gray transformation. Investigate pre-incremented loop counters and other loop constructions. Add interpolated method of handling interlacing. glmark2-2012.08/./src/libpng/ANNOUNCE0000664000175000017500000000363312013417376016116 0ustar alfalf00000000000000 Libpng 1.2.46 - July 9, 2011 This is a public release of libpng, intended for use in production codes. Files available for download: Source files with LF line endings (for Unix/Linux) and with a "configure" script libpng-1.2.46.tar.xz (LZMA-compressed, recommended) libpng-1.2.46.tar.gz libpng-1.2.46.tar.bz2 Source files with LF line endings (for Unix/Linux) without the "configure" script libpng-1.2.46-no-config.tar.xz (LZMA-compressed, recommended) libpng-1.2.46-no-config.tar.gz libpng-1.2.46-no-config.tar.bz2 Source files with CRLF line endings (for Windows), without the "configure" script lpng1246.zip lpng1246.7z lpng1246.tar.bz2 Project files libpng-1.2.46-project-netware.zip libpng-1.2.46-project-wince.zip Other information: libpng-1.2.46-README.txt libpng-1.2.46-KNOWNBUGS.txt libpng-1.2.46-LICENSE.txt libpng-1.2.46-Y2K-compliance.txt libpng-1.2.46-[previous version]-diff.txt Changes since the last public release (1.2.43): version 1.2.45 [July 9, 2011] Fixed uninitialized memory read in png_format_buffer() (Bug report by Frank Busse, related to CVE-2004-0421). Pass "" instead of '\0' to png_default_error() in png_err(). This mistake was introduced in libpng-1.2.20beta01. Check for up->location !PNG_AFTER_IDAT when writing unknown chunks before IDAT. Ported bugfix in pngrtran.c from 1.5.3: when expanding a paletted image, always expand to RGBA if transparency is present. Check for integer overflow in png_set_rgb_to_gray(). Check for sCAL chunk too short. Added CMakeLists.txt, projects/xcode, and pnggccrd.c to EXTRA_DIST in Makefile.am and Makefile.in Udated copyright year to 2011. Send comments/corrections/commendations to png-mng-implement at lists.sf.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement to subscribe) or to glennrp at users.sourceforge.net Glenn R-P glmark2-2012.08/./src/libpng/png.c0000664000175000017500000007525212013417376015723 0ustar alfalf00000000000000 /* png.c - location for general purpose libpng functions * * Last changed in libpng 1.2.46 [February 25, 2011] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #define PNG_INTERNAL #define PNG_NO_EXTERN #define PNG_NO_PEDANTIC_WARNINGS #include "png.h" /* Generate a compiler error if there is an old png.h in the search path. */ typedef version_1_2_46 Your_png_h_is_not_version_1_2_46; /* Version information for C files. This had better match the version * string defined in png.h. */ #ifdef PNG_USE_GLOBAL_ARRAYS /* png_libpng_ver was changed to a function in version 1.0.5c */ PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING; #ifdef PNG_READ_SUPPORTED /* png_sig was changed to a function in version 1.0.5c */ /* Place to hold the signature string for a PNG file. */ PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; #endif /* PNG_READ_SUPPORTED */ /* Invoke global declarations for constant strings for known chunk types */ PNG_IHDR; PNG_IDAT; PNG_IEND; PNG_PLTE; PNG_bKGD; PNG_cHRM; PNG_gAMA; PNG_hIST; PNG_iCCP; PNG_iTXt; PNG_oFFs; PNG_pCAL; PNG_sCAL; PNG_pHYs; PNG_sBIT; PNG_sPLT; PNG_sRGB; PNG_tEXt; PNG_tIME; PNG_tRNS; PNG_zTXt; #ifdef PNG_READ_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; /* Height of interlace block. This is not currently used - if you need * it, uncomment it here and in png.h PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; */ /* Mask to determine which pixels are valid in a pass */ PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; /* Mask to determine which pixels to overwrite while displaying */ PNG_CONST int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; #endif /* PNG_READ_SUPPORTED */ #endif /* PNG_USE_GLOBAL_ARRAYS */ /* Tells libpng that we have already handled the first "num_bytes" bytes * of the PNG file signature. If the PNG data is embedded into another * stream we can set num_bytes = 8 so that libpng will not attempt to read * or write any of the magic bytes before it starts on the IHDR. */ #ifdef PNG_READ_SUPPORTED void PNGAPI png_set_sig_bytes(png_structp png_ptr, int num_bytes) { png_debug(1, "in png_set_sig_bytes"); if (png_ptr == NULL) return; if (num_bytes > 8) png_error(png_ptr, "Too many bytes for PNG signature."); png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); } /* Checks whether the supplied bytes match the PNG signature. We allow * checking less than the full 8-byte signature so that those apps that * already read the first few bytes of a file to determine the file type * can simply check the remaining bytes for extra assurance. Returns * an integer less than, equal to, or greater than zero if sig is found, * respectively, to be less than, to match, or be greater than the correct * PNG signature (this is the same behaviour as strcmp, memcmp, etc). */ int PNGAPI png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check) { png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; if (num_to_check > 8) num_to_check = 8; else if (num_to_check < 1) return (-1); if (start > 7) return (-1); if (start + num_to_check > 8) num_to_check = 8 - start; return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); } #if defined(PNG_1_0_X) || defined(PNG_1_2_X) /* (Obsolete) function to check signature bytes. It does not allow one * to check a partial signature. This function might be removed in the * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG. */ int PNGAPI png_check_sig(png_bytep sig, int num) { return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num)); } #endif #endif /* PNG_READ_SUPPORTED */ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) /* Function to allocate memory for zlib and clear it to 0. */ #ifdef PNG_1_0_X voidpf PNGAPI #else voidpf /* PRIVATE */ #endif png_zalloc(voidpf png_ptr, uInt items, uInt size) { png_voidp ptr; png_structp p=(png_structp)png_ptr; png_uint_32 save_flags=p->flags; png_uint_32 num_bytes; if (png_ptr == NULL) return (NULL); if (items > PNG_UINT_32_MAX/size) { png_warning (p, "Potential overflow in png_zalloc()"); return (NULL); } num_bytes = (png_uint_32)items * size; p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); p->flags=save_flags; #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO) if (ptr == NULL) return ((voidpf)ptr); if (num_bytes > (png_uint_32)0x8000L) { png_memset(ptr, 0, (png_size_t)0x8000L); png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0, (png_size_t)(num_bytes - (png_uint_32)0x8000L)); } else { png_memset(ptr, 0, (png_size_t)num_bytes); } #endif return ((voidpf)ptr); } /* Function to free memory for zlib */ #ifdef PNG_1_0_X void PNGAPI #else void /* PRIVATE */ #endif png_zfree(voidpf png_ptr, voidpf ptr) { png_free((png_structp)png_ptr, (png_voidp)ptr); } /* Reset the CRC variable to 32 bits of 1's. Care must be taken * in case CRC is > 32 bits to leave the top bits 0. */ void /* PRIVATE */ png_reset_crc(png_structp png_ptr) { png_ptr->crc = crc32(0, Z_NULL, 0); } /* Calculate the CRC over a section of data. We can only pass as * much data to this routine as the largest single buffer size. We * also check that this data will actually be used before going to the * trouble of calculating it. */ void /* PRIVATE */ png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length) { int need_crc = 1; if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ { if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) need_crc = 0; } else /* critical */ { if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) need_crc = 0; } if (need_crc) png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length); } /* Allocate the memory for an info_struct for the application. We don't * really need the png_ptr, but it could potentially be useful in the * future. This should be used in favour of malloc(png_sizeof(png_info)) * and png_info_init() so that applications that want to use a shared * libpng don't have to be recompiled if png_info changes size. */ png_infop PNGAPI png_create_info_struct(png_structp png_ptr) { png_infop info_ptr; png_debug(1, "in png_create_info_struct"); if (png_ptr == NULL) return (NULL); #ifdef PNG_USER_MEM_SUPPORTED info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, png_ptr->malloc_fn, png_ptr->mem_ptr); #else info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); #endif if (info_ptr != NULL) png_info_init_3(&info_ptr, png_sizeof(png_info)); return (info_ptr); } /* This function frees the memory associated with a single info struct. * Normally, one would use either png_destroy_read_struct() or * png_destroy_write_struct() to free an info struct, but this may be * useful for some applications. */ void PNGAPI png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) { png_infop info_ptr = NULL; png_debug(1, "in png_destroy_info_struct"); if (png_ptr == NULL) return; if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (info_ptr != NULL) { png_info_destroy(png_ptr, info_ptr); #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn, png_ptr->mem_ptr); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = NULL; } } /* Initialize the info structure. This is now an internal function (0.89) * and applications using it are urged to use png_create_info_struct() * instead. */ #if defined(PNG_1_0_X) || defined(PNG_1_2_X) #undef png_info_init void PNGAPI png_info_init(png_infop info_ptr) { /* We only come here via pre-1.0.12-compiled applications */ png_info_init_3(&info_ptr, 0); } #endif void PNGAPI png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) { png_infop info_ptr = *ptr_ptr; png_debug(1, "in png_info_init_3"); if (info_ptr == NULL) return; if (png_sizeof(png_info) > png_info_struct_size) { png_destroy_struct(info_ptr); info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); *ptr_ptr = info_ptr; } /* Set everything to 0 */ png_memset(info_ptr, 0, png_sizeof(png_info)); } #ifdef PNG_FREE_ME_SUPPORTED void PNGAPI png_data_freer(png_structp png_ptr, png_infop info_ptr, int freer, png_uint_32 mask) { png_debug(1, "in png_data_freer"); if (png_ptr == NULL || info_ptr == NULL) return; if (freer == PNG_DESTROY_WILL_FREE_DATA) info_ptr->free_me |= mask; else if (freer == PNG_USER_WILL_FREE_DATA) info_ptr->free_me &= ~mask; else png_warning(png_ptr, "Unknown freer parameter in png_data_freer."); } #endif void PNGAPI png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, int num) { png_debug(1, "in png_free_data"); if (png_ptr == NULL || info_ptr == NULL) return; #ifdef PNG_TEXT_SUPPORTED /* Free text item num or (if num == -1) all text items */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) #else if (mask & PNG_FREE_TEXT) #endif { if (num != -1) { if (info_ptr->text && info_ptr->text[num].key) { png_free(png_ptr, info_ptr->text[num].key); info_ptr->text[num].key = NULL; } } else { int i; for (i = 0; i < info_ptr->num_text; i++) png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); png_free(png_ptr, info_ptr->text); info_ptr->text = NULL; info_ptr->num_text=0; } } #endif #ifdef PNG_tRNS_SUPPORTED /* Free any tRNS entry */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) #else if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS)) #endif { png_free(png_ptr, info_ptr->trans); info_ptr->trans = NULL; info_ptr->valid &= ~PNG_INFO_tRNS; #ifndef PNG_FREE_ME_SUPPORTED png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; #endif } #endif #ifdef PNG_sCAL_SUPPORTED /* Free any sCAL entry */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) #else if (mask & PNG_FREE_SCAL) #endif { #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) png_free(png_ptr, info_ptr->scal_s_width); png_free(png_ptr, info_ptr->scal_s_height); info_ptr->scal_s_width = NULL; info_ptr->scal_s_height = NULL; #endif info_ptr->valid &= ~PNG_INFO_sCAL; } #endif #ifdef PNG_pCAL_SUPPORTED /* Free any pCAL entry */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) #else if (mask & PNG_FREE_PCAL) #endif { png_free(png_ptr, info_ptr->pcal_purpose); png_free(png_ptr, info_ptr->pcal_units); info_ptr->pcal_purpose = NULL; info_ptr->pcal_units = NULL; if (info_ptr->pcal_params != NULL) { int i; for (i = 0; i < (int)info_ptr->pcal_nparams; i++) { png_free(png_ptr, info_ptr->pcal_params[i]); info_ptr->pcal_params[i] = NULL; } png_free(png_ptr, info_ptr->pcal_params); info_ptr->pcal_params = NULL; } info_ptr->valid &= ~PNG_INFO_pCAL; } #endif #ifdef PNG_iCCP_SUPPORTED /* Free any iCCP entry */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) #else if (mask & PNG_FREE_ICCP) #endif { png_free(png_ptr, info_ptr->iccp_name); png_free(png_ptr, info_ptr->iccp_profile); info_ptr->iccp_name = NULL; info_ptr->iccp_profile = NULL; info_ptr->valid &= ~PNG_INFO_iCCP; } #endif #ifdef PNG_sPLT_SUPPORTED /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) #else if (mask & PNG_FREE_SPLT) #endif { if (num != -1) { if (info_ptr->splt_palettes) { png_free(png_ptr, info_ptr->splt_palettes[num].name); png_free(png_ptr, info_ptr->splt_palettes[num].entries); info_ptr->splt_palettes[num].name = NULL; info_ptr->splt_palettes[num].entries = NULL; } } else { if (info_ptr->splt_palettes_num) { int i; for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i); png_free(png_ptr, info_ptr->splt_palettes); info_ptr->splt_palettes = NULL; info_ptr->splt_palettes_num = 0; } info_ptr->valid &= ~PNG_INFO_sPLT; } } #endif #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED if (png_ptr->unknown_chunk.data) { png_free(png_ptr, png_ptr->unknown_chunk.data); png_ptr->unknown_chunk.data = NULL; } #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) #else if (mask & PNG_FREE_UNKN) #endif { if (num != -1) { if (info_ptr->unknown_chunks) { png_free(png_ptr, info_ptr->unknown_chunks[num].data); info_ptr->unknown_chunks[num].data = NULL; } } else { int i; if (info_ptr->unknown_chunks_num) { for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++) png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i); png_free(png_ptr, info_ptr->unknown_chunks); info_ptr->unknown_chunks = NULL; info_ptr->unknown_chunks_num = 0; } } } #endif #ifdef PNG_hIST_SUPPORTED /* Free any hIST entry */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_HIST) & info_ptr->free_me) #else if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST)) #endif { png_free(png_ptr, info_ptr->hist); info_ptr->hist = NULL; info_ptr->valid &= ~PNG_INFO_hIST; #ifndef PNG_FREE_ME_SUPPORTED png_ptr->flags &= ~PNG_FLAG_FREE_HIST; #endif } #endif /* Free any PLTE entry that was internally allocated */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) #else if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE)) #endif { png_zfree(png_ptr, info_ptr->palette); info_ptr->palette = NULL; info_ptr->valid &= ~PNG_INFO_PLTE; #ifndef PNG_FREE_ME_SUPPORTED png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; #endif info_ptr->num_palette = 0; } #ifdef PNG_INFO_IMAGE_SUPPORTED /* Free any image bits attached to the info structure */ #ifdef PNG_FREE_ME_SUPPORTED if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) #else if (mask & PNG_FREE_ROWS) #endif { if (info_ptr->row_pointers) { int row; for (row = 0; row < (int)info_ptr->height; row++) { png_free(png_ptr, info_ptr->row_pointers[row]); info_ptr->row_pointers[row] = NULL; } png_free(png_ptr, info_ptr->row_pointers); info_ptr->row_pointers = NULL; } info_ptr->valid &= ~PNG_INFO_IDAT; } #endif #ifdef PNG_FREE_ME_SUPPORTED if (num == -1) info_ptr->free_me &= ~mask; else info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL); #endif } /* This is an internal routine to free any memory that the info struct is * pointing to before re-using it or freeing the struct itself. Recall * that png_free() checks for NULL pointers for us. */ void /* PRIVATE */ png_info_destroy(png_structp png_ptr, png_infop info_ptr) { png_debug(1, "in png_info_destroy"); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED if (png_ptr->num_chunk_list) { png_free(png_ptr, png_ptr->chunk_list); png_ptr->chunk_list = NULL; png_ptr->num_chunk_list = 0; } #endif png_info_init_3(&info_ptr, png_sizeof(png_info)); } #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ /* This function returns a pointer to the io_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy() or png_read_destroy() are called. */ png_voidp PNGAPI png_get_io_ptr(png_structp png_ptr) { if (png_ptr == NULL) return (NULL); return (png_ptr->io_ptr); } #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #ifdef PNG_STDIO_SUPPORTED /* Initialize the default input/output functions for the PNG file. If you * use your own read or write routines, you can call either png_set_read_fn() * or png_set_write_fn() instead of png_init_io(). If you have defined * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't * necessarily available. */ void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp) { png_debug(1, "in png_init_io"); if (png_ptr == NULL) return; png_ptr->io_ptr = (png_voidp)fp; } #endif #ifdef PNG_TIME_RFC1123_SUPPORTED /* Convert the supplied time into an RFC 1123 string suitable for use in * a "Creation Time" or other text-based time string. */ png_charp PNGAPI png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime) { static PNG_CONST char short_months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; if (png_ptr == NULL) return (NULL); if (png_ptr->time_buffer == NULL) { png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29* png_sizeof(char))); } #ifdef _WIN32_WCE { wchar_t time_buf[29]; wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"), ptime->day % 32, short_months[(ptime->month - 1) % 12], ptime->year, ptime->hour % 24, ptime->minute % 60, ptime->second % 61); WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29, NULL, NULL); } #else #ifdef USE_FAR_KEYWORD { char near_time_buf[29]; png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000", ptime->day % 32, short_months[(ptime->month - 1) % 12], ptime->year, ptime->hour % 24, ptime->minute % 60, ptime->second % 61); png_memcpy(png_ptr->time_buffer, near_time_buf, 29*png_sizeof(char)); } #else png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000", ptime->day % 32, short_months[(ptime->month - 1) % 12], ptime->year, ptime->hour % 24, ptime->minute % 60, ptime->second % 61); #endif #endif /* _WIN32_WCE */ return ((png_charp)png_ptr->time_buffer); } #endif /* PNG_TIME_RFC1123_SUPPORTED */ #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ png_charp PNGAPI png_get_copyright(png_structp png_ptr) { png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ #ifdef PNG_STRING_COPYRIGHT return PNG_STRING_COPYRIGHT #else #ifdef __STDC__ return ((png_charp) PNG_STRING_NEWLINE \ "libpng version 1.2.46 - July 9, 2011" PNG_STRING_NEWLINE \ "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ PNG_STRING_NEWLINE); #else return ((png_charp) "libpng version 1.2.46 - July 9, 2011\ Copyright (c) 1998-2011 Glenn Randers-Pehrson\ Copyright (c) 1996-1997 Andreas Dilger\ Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."); #endif #endif } /* The following return the library version as a short string in the * format 1.0.0 through 99.99.99zz. To get the version of *.h files * used with your application, print out PNG_LIBPNG_VER_STRING, which * is defined in png.h. * Note: now there is no difference between png_get_libpng_ver() and * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, * it is guaranteed that png.c uses the correct version of png.h. */ png_charp PNGAPI png_get_libpng_ver(png_structp png_ptr) { /* Version of *.c files used when building libpng */ png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ return ((png_charp) PNG_LIBPNG_VER_STRING); } png_charp PNGAPI png_get_header_ver(png_structp png_ptr) { /* Version of *.h files used when building libpng */ png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ return ((png_charp) PNG_LIBPNG_VER_STRING); } png_charp PNGAPI png_get_header_version(png_structp png_ptr) { /* Returns longer string containing both version and date */ png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ #ifdef __STDC__ return ((png_charp) PNG_HEADER_VERSION_STRING #ifndef PNG_READ_SUPPORTED " (NO READ SUPPORT)" #endif PNG_STRING_NEWLINE); #else return ((png_charp) PNG_HEADER_VERSION_STRING); #endif } #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED int PNGAPI png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name) { /* Check chunk_name and return "keep" value if it's on the list, else 0 */ int i; png_bytep p; if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0) return 0; p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5; for (i = png_ptr->num_chunk_list; i; i--, p -= 5) if (!png_memcmp(chunk_name, p, 4)) return ((int)*(p + 4)); return 0; } #endif /* This function, added to libpng-1.0.6g, is untested. */ int PNGAPI png_reset_zstream(png_structp png_ptr) { if (png_ptr == NULL) return Z_STREAM_ERROR; return (inflateReset(&png_ptr->zstream)); } #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ /* This function was added to libpng-1.0.7 */ png_uint_32 PNGAPI png_access_version_number(void) { /* Version of *.c files used when building libpng */ return((png_uint_32) PNG_LIBPNG_VER); } #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) #ifndef PNG_1_0_X /* This function was added to libpng 1.2.0 */ int PNGAPI png_mmx_support(void) { /* Obsolete, to be removed from libpng-1.4.0 */ return -1; } #endif /* PNG_1_0_X */ #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #ifdef PNG_SIZE_T /* Added at libpng version 1.2.6 */ PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size)); png_size_t PNGAPI png_convert_size(size_t size) { if (size > (png_size_t)-1) PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */ return ((png_size_t)size); } #endif /* PNG_SIZE_T */ /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_CHECK_cHRM_SUPPORTED /* * Multiply two 32-bit numbers, V1 and V2, using 32-bit * arithmetic, to produce a 64 bit result in the HI/LO words. * * A B * x C D * ------ * AD || BD * AC || CB || 0 * * where A and B are the high and low 16-bit words of V1, * C and D are the 16-bit words of V2, AD is the product of * A and D, and X || Y is (X << 16) + Y. */ void /* PRIVATE */ png_64bit_product (long v1, long v2, unsigned long *hi_product, unsigned long *lo_product) { int a, b, c, d; long lo, hi, x, y; a = (v1 >> 16) & 0xffff; b = v1 & 0xffff; c = (v2 >> 16) & 0xffff; d = v2 & 0xffff; lo = b * d; /* BD */ x = a * d + c * b; /* AD + CB */ y = ((lo >> 16) & 0xffff) + x; lo = (lo & 0xffff) | ((y & 0xffff) << 16); hi = (y >> 16) & 0xffff; hi += a * c; /* AC */ *hi_product = (unsigned long)hi; *lo_product = (unsigned long)lo; } int /* PRIVATE */ png_check_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y) { int ret = 1; unsigned long xy_hi,xy_lo,yx_hi,yx_lo; png_debug(1, "in function png_check_cHRM_fixed"); if (png_ptr == NULL) return 0; if (white_x < 0 || white_y <= 0 || red_x < 0 || red_y < 0 || green_x < 0 || green_y < 0 || blue_x < 0 || blue_y < 0) { png_warning(png_ptr, "Ignoring attempt to set negative chromaticity value"); ret = 0; } if (white_x > (png_fixed_point) PNG_UINT_31_MAX || white_y > (png_fixed_point) PNG_UINT_31_MAX || red_x > (png_fixed_point) PNG_UINT_31_MAX || red_y > (png_fixed_point) PNG_UINT_31_MAX || green_x > (png_fixed_point) PNG_UINT_31_MAX || green_y > (png_fixed_point) PNG_UINT_31_MAX || blue_x > (png_fixed_point) PNG_UINT_31_MAX || blue_y > (png_fixed_point) PNG_UINT_31_MAX ) { png_warning(png_ptr, "Ignoring attempt to set chromaticity value exceeding 21474.83"); ret = 0; } if (white_x > 100000L - white_y) { png_warning(png_ptr, "Invalid cHRM white point"); ret = 0; } if (red_x > 100000L - red_y) { png_warning(png_ptr, "Invalid cHRM red point"); ret = 0; } if (green_x > 100000L - green_y) { png_warning(png_ptr, "Invalid cHRM green point"); ret = 0; } if (blue_x > 100000L - blue_y) { png_warning(png_ptr, "Invalid cHRM blue point"); ret = 0; } png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo); png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo); if (xy_hi == yx_hi && xy_lo == yx_lo) { png_warning(png_ptr, "Ignoring attempt to set cHRM RGB triangle with zero area"); ret = 0; } return ret; } #endif /* PNG_CHECK_cHRM_SUPPORTED */ #endif /* PNG_cHRM_SUPPORTED */ void /* PRIVATE */ png_check_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type) { int error = 0; /* Check for width and height valid values */ if (width == 0) { png_warning(png_ptr, "Image width is zero in IHDR"); error = 1; } if (height == 0) { png_warning(png_ptr, "Image height is zero in IHDR"); error = 1; } #ifdef PNG_SET_USER_LIMITS_SUPPORTED if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX) #else if (width > PNG_USER_WIDTH_MAX) #endif { png_warning(png_ptr, "Image width exceeds user limit in IHDR"); error = 1; } #ifdef PNG_SET_USER_LIMITS_SUPPORTED if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX) #else if (height > PNG_USER_HEIGHT_MAX) #endif { png_warning(png_ptr, "Image height exceeds user limit in IHDR"); error = 1; } if (width > PNG_UINT_31_MAX) { png_warning(png_ptr, "Invalid image width in IHDR"); error = 1; } if ( height > PNG_UINT_31_MAX) { png_warning(png_ptr, "Invalid image height in IHDR"); error = 1; } if ( width > (PNG_UINT_32_MAX >> 3) /* 8-byte RGBA pixels */ - 64 /* bigrowbuf hack */ - 1 /* filter byte */ - 7*8 /* rounding of width to multiple of 8 pixels */ - 8) /* extra max_pixel_depth pad */ png_warning(png_ptr, "Width is too large for libpng to process pixels"); /* Check other values */ if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && bit_depth != 8 && bit_depth != 16) { png_warning(png_ptr, "Invalid bit depth in IHDR"); error = 1; } if (color_type < 0 || color_type == 1 || color_type == 5 || color_type > 6) { png_warning(png_ptr, "Invalid color type in IHDR"); error = 1; } if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || ((color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY_ALPHA || color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) { png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR"); error = 1; } if (interlace_type >= PNG_INTERLACE_LAST) { png_warning(png_ptr, "Unknown interlace method in IHDR"); error = 1; } if (compression_type != PNG_COMPRESSION_TYPE_BASE) { png_warning(png_ptr, "Unknown compression method in IHDR"); error = 1; } #ifdef PNG_MNG_FEATURES_SUPPORTED /* Accept filter_method 64 (intrapixel differencing) only if * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and * 2. Libpng did not read a PNG signature (this filter_method is only * used in PNG datastreams that are embedded in MNG datastreams) and * 3. The application called png_permit_mng_features with a mask that * included PNG_FLAG_MNG_FILTER_64 and * 4. The filter_method is 64 and * 5. The color_type is RGB or RGBA */ if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) && png_ptr->mng_features_permitted) png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); if (filter_type != PNG_FILTER_TYPE_BASE) { if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA))) { png_warning(png_ptr, "Unknown filter method in IHDR"); error = 1; } if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) { png_warning(png_ptr, "Invalid filter method in IHDR"); error = 1; } } #else if (filter_type != PNG_FILTER_TYPE_BASE) { png_warning(png_ptr, "Unknown filter method in IHDR"); error = 1; } #endif if (error == 1) png_error(png_ptr, "Invalid IHDR data"); } #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ glmark2-2012.08/./src/libpng/LICENSE0000664000175000017500000001014112013417376015762 0ustar alfalf00000000000000 This copy of the libpng notices is provided for your convenience. In case of any discrepancy between this copy and the notices in the file png.h that is included in the libpng distribution, the latter shall prevail. COPYRIGHT NOTICE, DISCLAIMER, and LICENSE: If you modify libpng you may insert additional notices immediately following this sentence. This code is released under the libpng license. libpng versions 1.2.6, August 15, 2004, through 1.2.46, July 9, 2011, are Copyright (c) 2004, 2006-2009 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-1.2.5 with the following individual added to the list of Contributing Authors Cosmin Truta libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-1.0.6 with the following individuals added to the list of Contributing Authors Simon-Pierre Cadieux Eric S. Raymond Gilles Vollant and with the following additions to the disclaimer: There is no warranty against interference with your enjoyment of the library or against infringement. There is no warranty that our efforts or the library will fulfill any of your particular purposes or needs. This library is provided with all faults, and the entire risk of satisfactory quality, performance, accuracy, and effort is with the user. libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-0.96, with the following individuals added to the list of Contributing Authors: Tom Lane Glenn Randers-Pehrson Willem van Schaik libpng versions 0.89, June 1996, through 0.96, May 1997, are Copyright (c) 1996, 1997 Andreas Dilger Distributed according to the same disclaimer and license as libpng-0.88, with the following individuals added to the list of Contributing Authors: John Bowler Kevin Bracey Sam Bushell Magnus Holmgren Greg Roelofs Tom Tanner libpng versions 0.5, May 1995, through 0.88, January 1996, are Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. For the purposes of this copyright and license, "Contributing Authors" is defined as the following set of individuals: Andreas Dilger Dave Martindale Guy Eric Schalnat Paul Schmidt Tim Wegner The PNG Reference Library is supplied "AS IS". The Contributing Authors and Group 42, Inc. disclaim all warranties, expressed or implied, including, without limitation, the warranties of merchantability and of fitness for any purpose. The Contributing Authors and Group 42, Inc. assume no liability for direct, indirect, incidental, special, exemplary, or consequential damages, which may result from the use of the PNG Reference Library, even if advised of the possibility of such damage. Permission is hereby granted to use, copy, modify, and distribute this source code, or portions hereof, for any purpose, without fee, subject to the following restrictions: 1. The origin of this source code must not be misrepresented. 2. Altered versions must be plainly marked as such and must not be misrepresented as being the original source. 3. This Copyright notice may not be removed or altered from any source or altered source distribution. The Contributing Authors and Group 42, Inc. specifically permit, without fee, and encourage the use of this source code as a component to supporting the PNG file format in commercial products. If you use this source code in a product, acknowledgment is not required but would be appreciated. A "png_get_copyright" function is available, for convenient use in "about" boxes and the like: printf("%s",png_get_copyright(NULL)); Also, the PNG logo (in PNG format, of course) is supplied in the files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31). Libpng is OSI Certified Open Source Software. OSI Certified Open Source is a certification mark of the Open Source Initiative. Glenn Randers-Pehrson glennrp at users.sourceforge.net July 9, 2011 glmark2-2012.08/./src/libpng/png.h0000664000175000017500000047277412013417376015742 0ustar alfalf00000000000000/* png.h - header file for PNG reference library * * libpng version 1.2.46 - July 9, 2011 * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This code is released under the libpng license (See LICENSE, below) * * Authors and maintainers: * libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat * libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger * libpng versions 0.97, January 1998, through 1.2.46 - July 9, 2011: Glenn * See also "Contributing Authors", below. * * Note about libpng version numbers: * * Due to various miscommunications, unforeseen code incompatibilities * and occasional factors outside the authors' control, version numbering * on the library has not always been consistent and straightforward. * The following table summarizes matters since version 0.89c, which was * the first widely used release: * * source png.h png.h shared-lib * version string int version * ------- ------ ----- ---------- * 0.89c "1.0 beta 3" 0.89 89 1.0.89 * 0.90 "1.0 beta 4" 0.90 90 0.90 [should have been 2.0.90] * 0.95 "1.0 beta 5" 0.95 95 0.95 [should have been 2.0.95] * 0.96 "1.0 beta 6" 0.96 96 0.96 [should have been 2.0.96] * 0.97b "1.00.97 beta 7" 1.00.97 97 1.0.1 [should have been 2.0.97] * 0.97c 0.97 97 2.0.97 * 0.98 0.98 98 2.0.98 * 0.99 0.99 98 2.0.99 * 0.99a-m 0.99 99 2.0.99 * 1.00 1.00 100 2.1.0 [100 should be 10000] * 1.0.0 (from here on, the 100 2.1.0 [100 should be 10000] * 1.0.1 png.h string is 10001 2.1.0 * 1.0.1a-e identical to the 10002 from here on, the shared library * 1.0.2 source version) 10002 is 2.V where V is the source code * 1.0.2a-b 10003 version, except as noted. * 1.0.3 10003 * 1.0.3a-d 10004 * 1.0.4 10004 * 1.0.4a-f 10005 * 1.0.5 (+ 2 patches) 10005 * 1.0.5a-d 10006 * 1.0.5e-r 10100 (not source compatible) * 1.0.5s-v 10006 (not binary compatible) * 1.0.6 (+ 3 patches) 10006 (still binary incompatible) * 1.0.6d-f 10007 (still binary incompatible) * 1.0.6g 10007 * 1.0.6h 10007 10.6h (testing xy.z so-numbering) * 1.0.6i 10007 10.6i * 1.0.6j 10007 2.1.0.6j (incompatible with 1.0.0) * 1.0.7beta11-14 DLLNUM 10007 2.1.0.7beta11-14 (binary compatible) * 1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible) * 1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible) * 1.0.7 1 10007 (still compatible) * 1.0.8beta1-4 1 10008 2.1.0.8beta1-4 * 1.0.8rc1 1 10008 2.1.0.8rc1 * 1.0.8 1 10008 2.1.0.8 * 1.0.9beta1-6 1 10009 2.1.0.9beta1-6 * 1.0.9rc1 1 10009 2.1.0.9rc1 * 1.0.9beta7-10 1 10009 2.1.0.9beta7-10 * 1.0.9rc2 1 10009 2.1.0.9rc2 * 1.0.9 1 10009 2.1.0.9 * 1.0.10beta1 1 10010 2.1.0.10beta1 * 1.0.10rc1 1 10010 2.1.0.10rc1 * 1.0.10 1 10010 2.1.0.10 * 1.0.11beta1-3 1 10011 2.1.0.11beta1-3 * 1.0.11rc1 1 10011 2.1.0.11rc1 * 1.0.11 1 10011 2.1.0.11 * 1.0.12beta1-2 2 10012 2.1.0.12beta1-2 * 1.0.12rc1 2 10012 2.1.0.12rc1 * 1.0.12 2 10012 2.1.0.12 * 1.1.0a-f - 10100 2.1.1.0a-f (branch abandoned) * 1.2.0beta1-2 2 10200 2.1.2.0beta1-2 * 1.2.0beta3-5 3 10200 3.1.2.0beta3-5 * 1.2.0rc1 3 10200 3.1.2.0rc1 * 1.2.0 3 10200 3.1.2.0 * 1.2.1beta1-4 3 10201 3.1.2.1beta1-4 * 1.2.1rc1-2 3 10201 3.1.2.1rc1-2 * 1.2.1 3 10201 3.1.2.1 * 1.2.2beta1-6 12 10202 12.so.0.1.2.2beta1-6 * 1.0.13beta1 10 10013 10.so.0.1.0.13beta1 * 1.0.13rc1 10 10013 10.so.0.1.0.13rc1 * 1.2.2rc1 12 10202 12.so.0.1.2.2rc1 * 1.0.13 10 10013 10.so.0.1.0.13 * 1.2.2 12 10202 12.so.0.1.2.2 * 1.2.3rc1-6 12 10203 12.so.0.1.2.3rc1-6 * 1.2.3 12 10203 12.so.0.1.2.3 * 1.2.4beta1-3 13 10204 12.so.0.1.2.4beta1-3 * 1.0.14rc1 13 10014 10.so.0.1.0.14rc1 * 1.2.4rc1 13 10204 12.so.0.1.2.4rc1 * 1.0.14 10 10014 10.so.0.1.0.14 * 1.2.4 13 10204 12.so.0.1.2.4 * 1.2.5beta1-2 13 10205 12.so.0.1.2.5beta1-2 * 1.0.15rc1-3 10 10015 10.so.0.1.0.15rc1-3 * 1.2.5rc1-3 13 10205 12.so.0.1.2.5rc1-3 * 1.0.15 10 10015 10.so.0.1.0.15 * 1.2.5 13 10205 12.so.0.1.2.5 * 1.2.6beta1-4 13 10206 12.so.0.1.2.6beta1-4 * 1.0.16 10 10016 10.so.0.1.0.16 * 1.2.6 13 10206 12.so.0.1.2.6 * 1.2.7beta1-2 13 10207 12.so.0.1.2.7beta1-2 * 1.0.17rc1 10 10017 10.so.0.1.0.17rc1 * 1.2.7rc1 13 10207 12.so.0.1.2.7rc1 * 1.0.17 10 10017 10.so.0.1.0.17 * 1.2.7 13 10207 12.so.0.1.2.7 * 1.2.8beta1-5 13 10208 12.so.0.1.2.8beta1-5 * 1.0.18rc1-5 10 10018 10.so.0.1.0.18rc1-5 * 1.2.8rc1-5 13 10208 12.so.0.1.2.8rc1-5 * 1.0.18 10 10018 10.so.0.1.0.18 * 1.2.8 13 10208 12.so.0.1.2.8 * 1.2.9beta1-3 13 10209 12.so.0.1.2.9beta1-3 * 1.2.9beta4-11 13 10209 12.so.0.9[.0] * 1.2.9rc1 13 10209 12.so.0.9[.0] * 1.2.9 13 10209 12.so.0.9[.0] * 1.2.10beta1-8 13 10210 12.so.0.10[.0] * 1.2.10rc1-3 13 10210 12.so.0.10[.0] * 1.2.10 13 10210 12.so.0.10[.0] * 1.2.11beta1-4 13 10211 12.so.0.11[.0] * 1.0.19rc1-5 10 10019 10.so.0.19[.0] * 1.2.11rc1-5 13 10211 12.so.0.11[.0] * 1.0.19 10 10019 10.so.0.19[.0] * 1.2.11 13 10211 12.so.0.11[.0] * 1.0.20 10 10020 10.so.0.20[.0] * 1.2.12 13 10212 12.so.0.12[.0] * 1.2.13beta1 13 10213 12.so.0.13[.0] * 1.0.21 10 10021 10.so.0.21[.0] * 1.2.13 13 10213 12.so.0.13[.0] * 1.2.14beta1-2 13 10214 12.so.0.14[.0] * 1.0.22rc1 10 10022 10.so.0.22[.0] * 1.2.14rc1 13 10214 12.so.0.14[.0] * 1.0.22 10 10022 10.so.0.22[.0] * 1.2.14 13 10214 12.so.0.14[.0] * 1.2.15beta1-6 13 10215 12.so.0.15[.0] * 1.0.23rc1-5 10 10023 10.so.0.23[.0] * 1.2.15rc1-5 13 10215 12.so.0.15[.0] * 1.0.23 10 10023 10.so.0.23[.0] * 1.2.15 13 10215 12.so.0.15[.0] * 1.2.16beta1-2 13 10216 12.so.0.16[.0] * 1.2.16rc1 13 10216 12.so.0.16[.0] * 1.0.24 10 10024 10.so.0.24[.0] * 1.2.16 13 10216 12.so.0.16[.0] * 1.2.17beta1-2 13 10217 12.so.0.17[.0] * 1.0.25rc1 10 10025 10.so.0.25[.0] * 1.2.17rc1-3 13 10217 12.so.0.17[.0] * 1.0.25 10 10025 10.so.0.25[.0] * 1.2.17 13 10217 12.so.0.17[.0] * 1.0.26 10 10026 10.so.0.26[.0] * 1.2.18 13 10218 12.so.0.18[.0] * 1.2.19beta1-31 13 10219 12.so.0.19[.0] * 1.0.27rc1-6 10 10027 10.so.0.27[.0] * 1.2.19rc1-6 13 10219 12.so.0.19[.0] * 1.0.27 10 10027 10.so.0.27[.0] * 1.2.19 13 10219 12.so.0.19[.0] * 1.2.20beta01-04 13 10220 12.so.0.20[.0] * 1.0.28rc1-6 10 10028 10.so.0.28[.0] * 1.2.20rc1-6 13 10220 12.so.0.20[.0] * 1.0.28 10 10028 10.so.0.28[.0] * 1.2.20 13 10220 12.so.0.20[.0] * 1.2.21beta1-2 13 10221 12.so.0.21[.0] * 1.2.21rc1-3 13 10221 12.so.0.21[.0] * 1.0.29 10 10029 10.so.0.29[.0] * 1.2.21 13 10221 12.so.0.21[.0] * 1.2.22beta1-4 13 10222 12.so.0.22[.0] * 1.0.30rc1 10 10030 10.so.0.30[.0] * 1.2.22rc1 13 10222 12.so.0.22[.0] * 1.0.30 10 10030 10.so.0.30[.0] * 1.2.22 13 10222 12.so.0.22[.0] * 1.2.23beta01-05 13 10223 12.so.0.23[.0] * 1.2.23rc01 13 10223 12.so.0.23[.0] * 1.2.23 13 10223 12.so.0.23[.0] * 1.2.24beta01-02 13 10224 12.so.0.24[.0] * 1.2.24rc01 13 10224 12.so.0.24[.0] * 1.2.24 13 10224 12.so.0.24[.0] * 1.2.25beta01-06 13 10225 12.so.0.25[.0] * 1.2.25rc01-02 13 10225 12.so.0.25[.0] * 1.0.31 10 10031 10.so.0.31[.0] * 1.2.25 13 10225 12.so.0.25[.0] * 1.2.26beta01-06 13 10226 12.so.0.26[.0] * 1.2.26rc01 13 10226 12.so.0.26[.0] * 1.2.26 13 10226 12.so.0.26[.0] * 1.0.32 10 10032 10.so.0.32[.0] * 1.2.27beta01-06 13 10227 12.so.0.27[.0] * 1.2.27rc01 13 10227 12.so.0.27[.0] * 1.0.33 10 10033 10.so.0.33[.0] * 1.2.27 13 10227 12.so.0.27[.0] * 1.0.34 10 10034 10.so.0.34[.0] * 1.2.28 13 10228 12.so.0.28[.0] * 1.2.29beta01-03 13 10229 12.so.0.29[.0] * 1.2.29rc01 13 10229 12.so.0.29[.0] * 1.0.35 10 10035 10.so.0.35[.0] * 1.2.29 13 10229 12.so.0.29[.0] * 1.0.37 10 10037 10.so.0.37[.0] * 1.2.30beta01-04 13 10230 12.so.0.30[.0] * 1.0.38rc01-08 10 10038 10.so.0.38[.0] * 1.2.30rc01-08 13 10230 12.so.0.30[.0] * 1.0.38 10 10038 10.so.0.38[.0] * 1.2.30 13 10230 12.so.0.30[.0] * 1.0.39rc01-03 10 10039 10.so.0.39[.0] * 1.2.31rc01-03 13 10231 12.so.0.31[.0] * 1.0.39 10 10039 10.so.0.39[.0] * 1.2.31 13 10231 12.so.0.31[.0] * 1.2.32beta01-02 13 10232 12.so.0.32[.0] * 1.0.40rc01 10 10040 10.so.0.40[.0] * 1.2.32rc01 13 10232 12.so.0.32[.0] * 1.0.40 10 10040 10.so.0.40[.0] * 1.2.32 13 10232 12.so.0.32[.0] * 1.2.33beta01-02 13 10233 12.so.0.33[.0] * 1.2.33rc01-02 13 10233 12.so.0.33[.0] * 1.0.41rc01 10 10041 10.so.0.41[.0] * 1.2.33 13 10233 12.so.0.33[.0] * 1.0.41 10 10041 10.so.0.41[.0] * 1.2.34beta01-07 13 10234 12.so.0.34[.0] * 1.0.42rc01 10 10042 10.so.0.42[.0] * 1.2.34rc01 13 10234 12.so.0.34[.0] * 1.0.42 10 10042 10.so.0.42[.0] * 1.2.34 13 10234 12.so.0.34[.0] * 1.2.35beta01-03 13 10235 12.so.0.35[.0] * 1.0.43rc01-02 10 10043 10.so.0.43[.0] * 1.2.35rc01-02 13 10235 12.so.0.35[.0] * 1.0.43 10 10043 10.so.0.43[.0] * 1.2.35 13 10235 12.so.0.35[.0] * 1.2.36beta01-05 13 10236 12.so.0.36[.0] * 1.2.36rc01 13 10236 12.so.0.36[.0] * 1.0.44 10 10044 10.so.0.44[.0] * 1.2.36 13 10236 12.so.0.36[.0] * 1.2.37beta01-03 13 10237 12.so.0.37[.0] * 1.2.37rc01 13 10237 12.so.0.37[.0] * 1.2.37 13 10237 12.so.0.37[.0] * 1.0.45 10 10045 12.so.0.45[.0] * 1.0.46 10 10046 10.so.0.46[.0] * 1.2.38beta01 13 10238 12.so.0.38[.0] * 1.2.38rc01-03 13 10238 12.so.0.38[.0] * 1.0.47 10 10047 10.so.0.47[.0] * 1.2.38 13 10238 12.so.0.38[.0] * 1.2.39beta01-05 13 10239 12.so.0.39[.0] * 1.2.39rc01 13 10239 12.so.0.39[.0] * 1.0.48 10 10048 10.so.0.48[.0] * 1.2.39 13 10239 12.so.0.39[.0] * 1.2.40beta01 13 10240 12.so.0.40[.0] * 1.2.40rc01 13 10240 12.so.0.40[.0] * 1.0.49 10 10049 10.so.0.49[.0] * 1.2.40 13 10240 12.so.0.40[.0] * 1.2.41beta01-18 13 10241 12.so.0.41[.0] * 1.0.51rc01 10 10051 10.so.0.51[.0] * 1.2.41rc01-03 13 10241 12.so.0.41[.0] * 1.0.51 10 10051 10.so.0.51[.0] * 1.2.41 13 10241 12.so.0.41[.0] * 1.2.42beta01-02 13 10242 12.so.0.42[.0] * 1.2.42rc01-05 13 10242 12.so.0.42[.0] * 1.0.52 10 10052 10.so.0.52[.0] * 1.2.42 13 10242 12.so.0.42[.0] * 1.2.43beta01-05 13 10243 12.so.0.43[.0] * 1.0.53rc01-02 10 10053 10.so.0.53[.0] * 1.2.43rc01-02 13 10243 12.so.0.43[.0] * 1.0.53 10 10053 10.so.0.53[.0] * 1.2.43 13 10243 12.so.0.43[.0] * 1.2.44beta01-03 13 10244 12.so.0.44[.0] * 1.2.44rc01-03 13 10244 12.so.0.44[.0] * 1.2.44 13 10244 12.so.0.44[.0] * 1.2.45beta01-03 13 10245 12.so.0.45[.0] * 1.0.55rc01 10 10055 10.so.0.55[.0] * 1.2.45rc01 13 10245 12.so.0.45[.0] * 1.0.55 10 10055 10.so.0.55[.0] * 1.2.45 13 10245 12.so.0.45[.0] * 1.2.46rc01-02 13 10246 12.so.0.46[.0] * 1.0.56 10 10056 10.so.0.56[.0] * 1.2.46 13 10246 12.so.0.46[.0] * * Henceforth the source version will match the shared-library major * and minor numbers; the shared-library major version number will be * used for changes in backward compatibility, as it is intended. The * PNG_LIBPNG_VER macro, which is not used within libpng but is available * for applications, is an unsigned integer of the form xyyzz corresponding * to the source version x.y.z (leading zeros in y and z). Beta versions * were given the previous public release number plus a letter, until * version 1.0.6j; from then on they were given the upcoming public * release number plus "betaNN" or "rcNN". * * Binary incompatibility exists only when applications make direct access * to the info_ptr or png_ptr members through png.h, and the compiled * application is loaded with a different version of the library. * * DLLNUM will change each time there are forward or backward changes * in binary compatibility (e.g., when a new feature is added). * * See libpng.txt or libpng.3 for more information. The PNG specification * is available as a W3C Recommendation and as an ISO Specification, * defines should NOT be changed. */ #define PNG_INFO_gAMA 0x0001 #define PNG_INFO_sBIT 0x0002 #define PNG_INFO_cHRM 0x0004 #define PNG_INFO_PLTE 0x0008 #define PNG_INFO_tRNS 0x0010 #define PNG_INFO_bKGD 0x0020 #define PNG_INFO_hIST 0x0040 #define PNG_INFO_pHYs 0x0080 #define PNG_INFO_oFFs 0x0100 #define PNG_INFO_tIME 0x0200 #define PNG_INFO_pCAL 0x0400 #define PNG_INFO_sRGB 0x0800 /* GR-P, 0.96a */ #define PNG_INFO_iCCP 0x1000 /* ESR, 1.0.6 */ #define PNG_INFO_sPLT 0x2000 /* ESR, 1.0.6 */ #define PNG_INFO_sCAL 0x4000 /* ESR, 1.0.6 */ #define PNG_INFO_IDAT 0x8000L /* ESR, 1.0.6 */ /* This is used for the transformation routines, as some of them * change these values for the row. It also should enable using * the routines for other purposes. */ typedef struct png_row_info_struct { png_uint_32 width; /* width of row */ png_uint_32 rowbytes; /* number of bytes in row */ png_byte color_type; /* color type of row */ png_byte bit_depth; /* bit depth of row */ png_byte channels; /* number of channels (1, 2, 3, or 4) */ png_byte pixel_depth; /* bits per pixel (depth * channels) */ } png_row_info; typedef png_row_info FAR * png_row_infop; typedef png_row_info FAR * FAR * png_row_infopp; /* These are the function types for the I/O functions and for the functions * that allow the user to override the default I/O functions with his or her * own. The png_error_ptr type should match that of user-supplied warning * and error functions, while the png_rw_ptr type should match that of the * user read/write data functions. */ typedef struct png_struct_def png_struct; typedef png_struct FAR * png_structp; typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp)); typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t)); typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp)); typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32, int)); typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32, int)); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop)); typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop)); typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep, png_uint_32, int)); #endif #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp, png_row_infop, png_bytep)); #endif #ifdef PNG_USER_CHUNKS_SUPPORTED typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp)); #endif #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp)); #endif /* Transform masks for the high-level interface */ #define PNG_TRANSFORM_IDENTITY 0x0000 /* read and write */ #define PNG_TRANSFORM_STRIP_16 0x0001 /* read only */ #define PNG_TRANSFORM_STRIP_ALPHA 0x0002 /* read only */ #define PNG_TRANSFORM_PACKING 0x0004 /* read and write */ #define PNG_TRANSFORM_PACKSWAP 0x0008 /* read and write */ #define PNG_TRANSFORM_EXPAND 0x0010 /* read only */ #define PNG_TRANSFORM_INVERT_MONO 0x0020 /* read and write */ #define PNG_TRANSFORM_SHIFT 0x0040 /* read and write */ #define PNG_TRANSFORM_BGR 0x0080 /* read and write */ #define PNG_TRANSFORM_SWAP_ALPHA 0x0100 /* read and write */ #define PNG_TRANSFORM_SWAP_ENDIAN 0x0200 /* read and write */ #define PNG_TRANSFORM_INVERT_ALPHA 0x0400 /* read and write */ #define PNG_TRANSFORM_STRIP_FILLER 0x0800 /* write only, deprecated */ /* Added to libpng-1.2.34 */ #define PNG_TRANSFORM_STRIP_FILLER_BEFORE 0x0800 /* write only */ #define PNG_TRANSFORM_STRIP_FILLER_AFTER 0x1000 /* write only */ /* Added to libpng-1.2.41 */ #define PNG_TRANSFORM_GRAY_TO_RGB 0x2000 /* read only */ /* Flags for MNG supported features */ #define PNG_FLAG_MNG_EMPTY_PLTE 0x01 #define PNG_FLAG_MNG_FILTER_64 0x04 #define PNG_ALL_MNG_FEATURES 0x05 typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t)); typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp)); /* The structure that holds the information to read and write PNG files. * The only people who need to care about what is inside of this are the * people who will be modifying the library for their own special needs. * It should NOT be accessed directly by an application, except to store * the jmp_buf. */ struct png_struct_def { #ifdef PNG_SETJMP_SUPPORTED jmp_buf jmpbuf; /* used in png_error */ #endif png_error_ptr error_fn PNG_DEPSTRUCT; /* function for printing errors and aborting */ png_error_ptr warning_fn PNG_DEPSTRUCT; /* function for printing warnings */ png_voidp error_ptr PNG_DEPSTRUCT; /* user supplied struct for error functions */ png_rw_ptr write_data_fn PNG_DEPSTRUCT; /* function for writing output data */ png_rw_ptr read_data_fn PNG_DEPSTRUCT; /* function for reading input data */ png_voidp io_ptr PNG_DEPSTRUCT; /* ptr to application struct for I/O functions */ #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED png_user_transform_ptr read_user_transform_fn PNG_DEPSTRUCT; /* user read transform */ #endif #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED png_user_transform_ptr write_user_transform_fn PNG_DEPSTRUCT; /* user write transform */ #endif /* These were added in libpng-1.0.2 */ #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) png_voidp user_transform_ptr PNG_DEPSTRUCT; /* user supplied struct for user transform */ png_byte user_transform_depth PNG_DEPSTRUCT; /* bit depth of user transformed pixels */ png_byte user_transform_channels PNG_DEPSTRUCT; /* channels in user transformed pixels */ #endif #endif png_uint_32 mode PNG_DEPSTRUCT; /* tells us where we are in the PNG file */ png_uint_32 flags PNG_DEPSTRUCT; /* flags indicating various things to libpng */ png_uint_32 transformations PNG_DEPSTRUCT; /* which transformations to perform */ z_stream zstream PNG_DEPSTRUCT; /* pointer to decompression structure (below) */ png_bytep zbuf PNG_DEPSTRUCT; /* buffer for zlib */ png_size_t zbuf_size PNG_DEPSTRUCT; /* size of zbuf */ int zlib_level PNG_DEPSTRUCT; /* holds zlib compression level */ int zlib_method PNG_DEPSTRUCT; /* holds zlib compression method */ int zlib_window_bits PNG_DEPSTRUCT; /* holds zlib compression window bits */ int zlib_mem_level PNG_DEPSTRUCT; /* holds zlib compression memory level */ int zlib_strategy PNG_DEPSTRUCT; /* holds zlib compression strategy */ png_uint_32 width PNG_DEPSTRUCT; /* width of image in pixels */ png_uint_32 height PNG_DEPSTRUCT; /* height of image in pixels */ png_uint_32 num_rows PNG_DEPSTRUCT; /* number of rows in current pass */ png_uint_32 usr_width PNG_DEPSTRUCT; /* width of row at start of write */ png_uint_32 rowbytes PNG_DEPSTRUCT; /* size of row in bytes */ #if 0 /* Replaced with the following in libpng-1.2.43 */ png_size_t irowbytes PNG_DEPSTRUCT; #endif /* Added in libpng-1.2.43 */ #ifdef PNG_USER_LIMITS_SUPPORTED /* Added in libpng-1.4.0: Total number of sPLT, text, and unknown * chunks that can be stored (0 means unlimited). */ png_uint_32 user_chunk_cache_max PNG_DEPSTRUCT; #endif png_uint_32 iwidth PNG_DEPSTRUCT; /* width of current interlaced row in pixels */ png_uint_32 row_number PNG_DEPSTRUCT; /* current row in interlace pass */ png_bytep prev_row PNG_DEPSTRUCT; /* buffer to save previous (unfiltered) row */ png_bytep row_buf PNG_DEPSTRUCT; /* buffer to save current (unfiltered) row */ #ifndef PNG_NO_WRITE_FILTER png_bytep sub_row PNG_DEPSTRUCT; /* buffer to save "sub" row when filtering */ png_bytep up_row PNG_DEPSTRUCT; /* buffer to save "up" row when filtering */ png_bytep avg_row PNG_DEPSTRUCT; /* buffer to save "avg" row when filtering */ png_bytep paeth_row PNG_DEPSTRUCT; /* buffer to save "Paeth" row when filtering */ #endif png_row_info row_info PNG_DEPSTRUCT; /* used for transformation routines */ png_uint_32 idat_size PNG_DEPSTRUCT; /* current IDAT size for read */ png_uint_32 crc PNG_DEPSTRUCT; /* current chunk CRC value */ png_colorp palette PNG_DEPSTRUCT; /* palette from the input file */ png_uint_16 num_palette PNG_DEPSTRUCT; /* number of color entries in palette */ png_uint_16 num_trans PNG_DEPSTRUCT; /* number of transparency values */ png_byte chunk_name[5] PNG_DEPSTRUCT; /* null-terminated name of current chunk */ png_byte compression PNG_DEPSTRUCT; /* file compression type (always 0) */ png_byte filter PNG_DEPSTRUCT; /* file filter type (always 0) */ png_byte interlaced PNG_DEPSTRUCT; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ png_byte pass PNG_DEPSTRUCT; /* current interlace pass (0 - 6) */ png_byte do_filter PNG_DEPSTRUCT; /* row filter flags (see PNG_FILTER_ below ) */ png_byte color_type PNG_DEPSTRUCT; /* color type of file */ png_byte bit_depth PNG_DEPSTRUCT; /* bit depth of file */ png_byte usr_bit_depth PNG_DEPSTRUCT; /* bit depth of users row */ png_byte pixel_depth PNG_DEPSTRUCT; /* number of bits per pixel */ png_byte channels PNG_DEPSTRUCT; /* number of channels in file */ png_byte usr_channels PNG_DEPSTRUCT; /* channels at start of write */ png_byte sig_bytes PNG_DEPSTRUCT; /* magic bytes read/written from start of file */ #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) #ifdef PNG_LEGACY_SUPPORTED png_byte filler PNG_DEPSTRUCT; /* filler byte for pixel expansion */ #else png_uint_16 filler PNG_DEPSTRUCT; /* filler bytes for pixel expansion */ #endif #endif #ifdef PNG_bKGD_SUPPORTED png_byte background_gamma_type PNG_DEPSTRUCT; # ifdef PNG_FLOATING_POINT_SUPPORTED float background_gamma PNG_DEPSTRUCT; # endif png_color_16 background PNG_DEPSTRUCT; /* background color in screen gamma space */ #ifdef PNG_READ_GAMMA_SUPPORTED png_color_16 background_1 PNG_DEPSTRUCT; /* background normalized to gamma 1.0 */ #endif #endif /* PNG_bKGD_SUPPORTED */ #ifdef PNG_WRITE_FLUSH_SUPPORTED png_flush_ptr output_flush_fn PNG_DEPSTRUCT; /* Function for flushing output */ png_uint_32 flush_dist PNG_DEPSTRUCT; /* how many rows apart to flush, 0 - no flush */ png_uint_32 flush_rows PNG_DEPSTRUCT; /* number of rows written since last flush */ #endif #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) int gamma_shift PNG_DEPSTRUCT; /* number of "insignificant" bits 16-bit gamma */ #ifdef PNG_FLOATING_POINT_SUPPORTED float gamma PNG_DEPSTRUCT; /* file gamma value */ float screen_gamma PNG_DEPSTRUCT; /* screen gamma value (display_exponent) */ #endif #endif #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) png_bytep gamma_table PNG_DEPSTRUCT; /* gamma table for 8-bit depth files */ png_bytep gamma_from_1 PNG_DEPSTRUCT; /* converts from 1.0 to screen */ png_bytep gamma_to_1 PNG_DEPSTRUCT; /* converts from file to 1.0 */ png_uint_16pp gamma_16_table PNG_DEPSTRUCT; /* gamma table for 16-bit depth files */ png_uint_16pp gamma_16_from_1 PNG_DEPSTRUCT; /* converts from 1.0 to screen */ png_uint_16pp gamma_16_to_1 PNG_DEPSTRUCT; /* converts from file to 1.0 */ #endif #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED) png_color_8 sig_bit PNG_DEPSTRUCT; /* significant bits in each available channel */ #endif #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) png_color_8 shift PNG_DEPSTRUCT; /* shift for significant bit tranformation */ #endif #if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \ || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) png_bytep trans PNG_DEPSTRUCT; /* transparency values for paletted files */ png_color_16 trans_values PNG_DEPSTRUCT; /* transparency values for non-paletted files */ #endif png_read_status_ptr read_row_fn PNG_DEPSTRUCT; /* called after each row is decoded */ png_write_status_ptr write_row_fn PNG_DEPSTRUCT; /* called after each row is encoded */ #ifdef PNG_PROGRESSIVE_READ_SUPPORTED png_progressive_info_ptr info_fn PNG_DEPSTRUCT; /* called after header data fully read */ png_progressive_row_ptr row_fn PNG_DEPSTRUCT; /* called after each prog. row is decoded */ png_progressive_end_ptr end_fn PNG_DEPSTRUCT; /* called after image is complete */ png_bytep save_buffer_ptr PNG_DEPSTRUCT; /* current location in save_buffer */ png_bytep save_buffer PNG_DEPSTRUCT; /* buffer for previously read data */ png_bytep current_buffer_ptr PNG_DEPSTRUCT; /* current location in current_buffer */ png_bytep current_buffer PNG_DEPSTRUCT; /* buffer for recently used data */ png_uint_32 push_length PNG_DEPSTRUCT; /* size of current input chunk */ png_uint_32 skip_length PNG_DEPSTRUCT; /* bytes to skip in input data */ png_size_t save_buffer_size PNG_DEPSTRUCT; /* amount of data now in save_buffer */ png_size_t save_buffer_max PNG_DEPSTRUCT; /* total size of save_buffer */ png_size_t buffer_size PNG_DEPSTRUCT; /* total amount of available input data */ png_size_t current_buffer_size PNG_DEPSTRUCT; /* amount of data now in current_buffer */ int process_mode PNG_DEPSTRUCT; /* what push library is currently doing */ int cur_palette PNG_DEPSTRUCT; /* current push library palette index */ # ifdef PNG_TEXT_SUPPORTED png_size_t current_text_size PNG_DEPSTRUCT; /* current size of text input data */ png_size_t current_text_left PNG_DEPSTRUCT; /* how much text left to read in input */ png_charp current_text PNG_DEPSTRUCT; /* current text chunk buffer */ png_charp current_text_ptr PNG_DEPSTRUCT; /* current location in current_text */ # endif /* PNG_TEXT_SUPPORTED */ #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) /* for the Borland special 64K segment handler */ png_bytepp offset_table_ptr PNG_DEPSTRUCT; png_bytep offset_table PNG_DEPSTRUCT; png_uint_16 offset_table_number PNG_DEPSTRUCT; png_uint_16 offset_table_count PNG_DEPSTRUCT; png_uint_16 offset_table_count_free PNG_DEPSTRUCT; #endif #ifdef PNG_READ_DITHER_SUPPORTED png_bytep palette_lookup PNG_DEPSTRUCT; /* lookup table for dithering */ png_bytep dither_index PNG_DEPSTRUCT; /* index translation for palette files */ #endif #if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED) png_uint_16p hist PNG_DEPSTRUCT; /* histogram */ #endif #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED png_byte heuristic_method PNG_DEPSTRUCT; /* heuristic for row filter selection */ png_byte num_prev_filters PNG_DEPSTRUCT; /* number of weights for previous rows */ png_bytep prev_filters PNG_DEPSTRUCT; /* filter type(s) of previous row(s) */ png_uint_16p filter_weights PNG_DEPSTRUCT; /* weight(s) for previous line(s) */ png_uint_16p inv_filter_weights PNG_DEPSTRUCT; /* 1/weight(s) for previous line(s) */ png_uint_16p filter_costs PNG_DEPSTRUCT; /* relative filter calculation cost */ png_uint_16p inv_filter_costs PNG_DEPSTRUCT; /* 1/relative filter calculation cost */ #endif #ifdef PNG_TIME_RFC1123_SUPPORTED png_charp time_buffer PNG_DEPSTRUCT; /* String to hold RFC 1123 time text */ #endif /* New members added in libpng-1.0.6 */ #ifdef PNG_FREE_ME_SUPPORTED png_uint_32 free_me PNG_DEPSTRUCT; /* flags items libpng is responsible for freeing */ #endif #ifdef PNG_USER_CHUNKS_SUPPORTED png_voidp user_chunk_ptr PNG_DEPSTRUCT; png_user_chunk_ptr read_user_chunk_fn PNG_DEPSTRUCT; /* user read chunk handler */ #endif #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED int num_chunk_list PNG_DEPSTRUCT; png_bytep chunk_list PNG_DEPSTRUCT; #endif /* New members added in libpng-1.0.3 */ #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED png_byte rgb_to_gray_status PNG_DEPSTRUCT; /* These were changed from png_byte in libpng-1.0.6 */ png_uint_16 rgb_to_gray_red_coeff PNG_DEPSTRUCT; png_uint_16 rgb_to_gray_green_coeff PNG_DEPSTRUCT; png_uint_16 rgb_to_gray_blue_coeff PNG_DEPSTRUCT; #endif /* New member added in libpng-1.0.4 (renamed in 1.0.9) */ #if defined(PNG_MNG_FEATURES_SUPPORTED) || \ defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) /* Changed from png_byte to png_uint_32 at version 1.2.0 */ #ifdef PNG_1_0_X png_byte mng_features_permitted PNG_DEPSTRUCT; #else png_uint_32 mng_features_permitted PNG_DEPSTRUCT; #endif /* PNG_1_0_X */ #endif /* New member added in libpng-1.0.7 */ #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) png_fixed_point int_gamma PNG_DEPSTRUCT; #endif /* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */ #ifdef PNG_MNG_FEATURES_SUPPORTED png_byte filter_type PNG_DEPSTRUCT; #endif #ifdef PNG_1_0_X /* New member added in libpng-1.0.10, ifdef'ed out in 1.2.0 */ png_uint_32 row_buf_size PNG_DEPSTRUCT; #endif /* New members added in libpng-1.2.0 */ #ifdef PNG_ASSEMBLER_CODE_SUPPORTED # ifndef PNG_1_0_X # ifdef PNG_MMX_CODE_SUPPORTED png_byte mmx_bitdepth_threshold PNG_DEPSTRUCT; png_uint_32 mmx_rowbytes_threshold PNG_DEPSTRUCT; # endif png_uint_32 asm_flags PNG_DEPSTRUCT; # endif #endif /* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */ #ifdef PNG_USER_MEM_SUPPORTED png_voidp mem_ptr PNG_DEPSTRUCT; /* user supplied struct for mem functions */ png_malloc_ptr malloc_fn PNG_DEPSTRUCT; /* function for allocating memory */ png_free_ptr free_fn PNG_DEPSTRUCT; /* function for freeing memory */ #endif /* New member added in libpng-1.0.13 and 1.2.0 */ png_bytep big_row_buf PNG_DEPSTRUCT; /* buffer to save current (unfiltered) row */ #ifdef PNG_READ_DITHER_SUPPORTED /* The following three members were added at version 1.0.14 and 1.2.4 */ png_bytep dither_sort PNG_DEPSTRUCT; /* working sort array */ png_bytep index_to_palette PNG_DEPSTRUCT; /* where the original index currently is */ /* in the palette */ png_bytep palette_to_index PNG_DEPSTRUCT; /* which original index points to this */ /* palette color */ #endif /* New members added in libpng-1.0.16 and 1.2.6 */ png_byte compression_type PNG_DEPSTRUCT; #ifdef PNG_USER_LIMITS_SUPPORTED png_uint_32 user_width_max PNG_DEPSTRUCT; png_uint_32 user_height_max PNG_DEPSTRUCT; #endif /* New member added in libpng-1.0.25 and 1.2.17 */ #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED /* Storage for unknown chunk that the library doesn't recognize. */ png_unknown_chunk unknown_chunk PNG_DEPSTRUCT; #endif /* New members added in libpng-1.2.26 */ png_uint_32 old_big_row_buf_size PNG_DEPSTRUCT; png_uint_32 old_prev_row_size PNG_DEPSTRUCT; /* New member added in libpng-1.2.30 */ png_charp chunkdata PNG_DEPSTRUCT; /* buffer for reading chunk data */ }; /* This triggers a compiler error in png.c, if png.c and png.h * do not agree upon the version number. */ typedef png_structp version_1_2_46; typedef png_struct FAR * FAR * png_structpp; /* Here are the function definitions most commonly used. This is not * the place to find out how to use libpng. See libpng.txt for the * full explanation, see example.c for the summary. This just provides * a simple one line description of the use of each function. */ /* Returns the version number of the library */ extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void)); /* Tell lib we have already handled the first magic bytes. * Handling more than 8 bytes from the beginning of the file is an error. */ extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr, int num_bytes)); /* Check sig[start] through sig[start + num_to_check - 1] to see if it's a * PNG file. Returns zero if the supplied bytes match the 8-byte PNG * signature, and non-zero otherwise. Having num_to_check == 0 or * start > 7 will always fail (ie return non-zero). */ extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start, png_size_t num_to_check)); /* Simple signature checking function. This is the same as calling * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n). */ extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num)) PNG_DEPRECATED; /* Allocate and initialize png_ptr struct for reading, and any other memory. */ extern PNG_EXPORT(png_structp,png_create_read_struct) PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED; /* Allocate and initialize png_ptr struct for writing, and any other memory */ extern PNG_EXPORT(png_structp,png_create_write_struct) PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED; #ifdef PNG_WRITE_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_WRITE_SUPPORTED extern PNG_EXPORT(void,png_set_compression_buffer_size) PNGARG((png_structp png_ptr, png_uint_32 size)); #endif /* Reset the compression stream */ extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr)); /* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */ #ifdef PNG_USER_MEM_SUPPORTED extern PNG_EXPORT(png_structp,png_create_read_struct_2) PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED; extern PNG_EXPORT(png_structp,png_create_write_struct_2) PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED; #endif /* Write a PNG chunk - size, type, (optional) data, CRC. */ extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr, png_bytep chunk_name, png_bytep data, png_size_t length)); /* Write the start of a PNG chunk - length and chunk name. */ extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr, png_bytep chunk_name, png_uint_32 length)); /* Write the data of a PNG chunk started with png_write_chunk_start(). */ extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)); /* Finish a chunk started with png_write_chunk_start() (includes CRC). */ extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr)); /* Allocate and initialize the info structure */ extern PNG_EXPORT(png_infop,png_create_info_struct) PNGARG((png_structp png_ptr)) PNG_ALLOCATED; #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Initialize the info structure (old interface - DEPRECATED) */ extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr)) PNG_DEPRECATED; #undef png_info_init #define png_info_init(info_ptr) png_info_init_3(&info_ptr,\ png_sizeof(png_info)); #endif extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr, png_size_t png_info_struct_size)); /* Writes all the PNG information before the image. */ extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the information before the actual image data. */ extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif #ifdef PNG_TIME_RFC1123_SUPPORTED extern PNG_EXPORT(png_charp,png_convert_to_rfc1123) PNGARG((png_structp png_ptr, png_timep ptime)); #endif #ifdef PNG_CONVERT_tIME_SUPPORTED /* Convert from a struct tm to png_time */ extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime, struct tm FAR * ttime)); /* Convert from time_t to png_time. Uses gmtime() */ extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime, time_t ttime)); #endif /* PNG_CONVERT_tIME_SUPPORTED */ #ifdef PNG_READ_EXPAND_SUPPORTED /* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */ extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr)); #ifndef PNG_1_0_X extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp png_ptr)); #endif extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr)); #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Deprecated */ extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr)) PNG_DEPRECATED; #endif #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) /* Use blue, green, red order for pixels. */ extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand the grayscale to 24-bit RGB if necessary. */ extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED /* Reduce RGB to grayscale. */ #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr, int error_action, double red, double green )); #endif extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr, int error_action, png_fixed_point red, png_fixed_point green )); extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp png_ptr)); #endif extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth, png_colorp palette)); #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) /* Add a filler byte to 8-bit Gray or 24-bit RGB images. */ extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr, png_uint_32 filler, int flags)); /* The values of the PNG_FILLER_ defines should NOT be changed */ #define PNG_FILLER_BEFORE 0 #define PNG_FILLER_AFTER 1 /* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */ #ifndef PNG_1_0_X extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr, png_uint_32 filler, int flags)); #endif #endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */ #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) /* Swap bytes in 16-bit depth files. */ extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) /* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */ extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED) /* Swap packing order of pixels in bytes. */ extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) /* Converts files to legal bit depths. */ extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr, png_color_8p true_bits)); #endif #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ defined(PNG_WRITE_INTERLACING_SUPPORTED) /* Have the code handle the interlacing. Returns the number of passes. */ extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) /* Invert monochrome files */ extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED /* Handle alpha and tRNS by replacing with a background color. */ #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr, png_color_16p background_color, int background_gamma_code, int need_expand, double background_gamma)); #endif #define PNG_BACKGROUND_GAMMA_UNKNOWN 0 #define PNG_BACKGROUND_GAMMA_SCREEN 1 #define PNG_BACKGROUND_GAMMA_FILE 2 #define PNG_BACKGROUND_GAMMA_UNIQUE 3 #endif #ifdef PNG_READ_16_TO_8_SUPPORTED /* Strip the second byte of information from a 16-bit depth file. */ extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_READ_DITHER_SUPPORTED /* Turn on dithering, and reduce the palette to the number of colors available. */ extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr, png_colorp palette, int num_palette, int maximum_colors, png_uint_16p histogram, int full_dither)); #endif #ifdef PNG_READ_GAMMA_SUPPORTED /* Handle gamma correction. Screen_gamma=(display_exponent) */ #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr, double screen_gamma, double default_file_gamma)); #endif #endif #if defined(PNG_1_0_X) || defined (PNG_1_2_X) #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \ defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) /* Permit or disallow empty PLTE (0: not permitted, 1: permitted) */ /* Deprecated and will be removed. Use png_permit_mng_features() instead. */ extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr, int empty_plte_permitted)) PNG_DEPRECATED; #endif #endif #ifdef PNG_WRITE_FLUSH_SUPPORTED /* Set how many lines between output flushes - 0 for no flushing */ extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows)); /* Flush the current PNG output buffer */ extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr)); #endif /* Optional update palette with requested transformations */ extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr)); /* Optional call to update the users info structure */ extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED /* Read one or more rows of image data. */ extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr, png_bytepp row, png_bytepp display_row, png_uint_32 num_rows)); #endif #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED /* Read a row of data. */ extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr, png_bytep row, png_bytep display_row)); #endif #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED /* Read the whole image into memory at once. */ extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr, png_bytepp image)); #endif /* Write a row of image data */ extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr, png_bytep row)); /* Write a few rows of image data */ extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr, png_bytepp row, png_uint_32 num_rows)); /* Write the image data */ extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr, png_bytepp image)); /* Writes the end of the PNG file. */ extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED /* Read the end of the PNG file. */ extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif /* Free any memory associated with the png_info_struct */ extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr, png_infopp info_ptr_ptr)); /* Free any memory associated with the png_struct and the png_info_structs */ extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)); /* Free all memory used by the read (old method - NOT DLL EXPORTED) */ extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)) PNG_DEPRECATED; /* Free any memory associated with the png_struct and the png_info_structs */ extern PNG_EXPORT(void,png_destroy_write_struct) PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)); /* Free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */ extern void png_write_destroy PNGARG((png_structp png_ptr)) PNG_DEPRECATED; /* Set the libpng method of handling chunk CRC errors */ extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr, int crit_action, int ancil_action)); /* Values for png_set_crc_action() to say how to handle CRC errors in * ancillary and critical chunks, and whether to use the data contained * therein. Note that it is impossible to "discard" data in a critical * chunk. For versions prior to 0.90, the action was always error/quit, * whereas in version 0.90 and later, the action for CRC errors in ancillary * chunks is warn/discard. These values should NOT be changed. * * value action:critical action:ancillary */ #define PNG_CRC_DEFAULT 0 /* error/quit warn/discard data */ #define PNG_CRC_ERROR_QUIT 1 /* error/quit error/quit */ #define PNG_CRC_WARN_DISCARD 2 /* (INVALID) warn/discard data */ #define PNG_CRC_WARN_USE 3 /* warn/use data warn/use data */ #define PNG_CRC_QUIET_USE 4 /* quiet/use data quiet/use data */ #define PNG_CRC_NO_CHANGE 5 /* use current value use current value */ /* These functions give the user control over the scan-line filtering in * libpng and the compression methods used by zlib. These functions are * mainly useful for testing, as the defaults should work with most users. * Those users who are tight on memory or want faster performance at the * expense of compression can modify them. See the compression library * header file (zlib.h) for an explination of the compression functions. */ /* Set the filtering method(s) used by libpng. Currently, the only valid * value for "method" is 0. */ extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method, int filters)); /* Flags for png_set_filter() to say which filters to use. The flags * are chosen so that they don't conflict with real filter types * below, in case they are supplied instead of the #defined constants. * These values should NOT be changed. */ #define PNG_NO_FILTERS 0x00 #define PNG_FILTER_NONE 0x08 #define PNG_FILTER_SUB 0x10 #define PNG_FILTER_UP 0x20 #define PNG_FILTER_AVG 0x40 #define PNG_FILTER_PAETH 0x80 #define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \ PNG_FILTER_AVG | PNG_FILTER_PAETH) /* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now. * These defines should NOT be changed. */ #define PNG_FILTER_VALUE_NONE 0 #define PNG_FILTER_VALUE_SUB 1 #define PNG_FILTER_VALUE_UP 2 #define PNG_FILTER_VALUE_AVG 3 #define PNG_FILTER_VALUE_PAETH 4 #define PNG_FILTER_VALUE_LAST 5 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */ /* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_ * defines, either the default (minimum-sum-of-absolute-differences), or * the experimental method (weighted-minimum-sum-of-absolute-differences). * * Weights are factors >= 1.0, indicating how important it is to keep the * filter type consistent between rows. Larger numbers mean the current * filter is that many times as likely to be the same as the "num_weights" * previous filters. This is cumulative for each previous row with a weight. * There needs to be "num_weights" values in "filter_weights", or it can be * NULL if the weights aren't being specified. Weights have no influence on * the selection of the first row filter. Well chosen weights can (in theory) * improve the compression for a given image. * * Costs are factors >= 1.0 indicating the relative decoding costs of a * filter type. Higher costs indicate more decoding expense, and are * therefore less likely to be selected over a filter with lower computational * costs. There needs to be a value in "filter_costs" for each valid filter * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't * setting the costs. Costs try to improve the speed of decompression without * unduly increasing the compressed image size. * * A negative weight or cost indicates the default value is to be used, and * values in the range [0.0, 1.0) indicate the value is to remain unchanged. * The default values for both weights and costs are currently 1.0, but may * change if good general weighting/cost heuristics can be found. If both * the weights and costs are set to 1.0, this degenerates the WEIGHTED method * to the UNWEIGHTED method, but with added encoding time/computation. */ #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr, int heuristic_method, int num_weights, png_doublep filter_weights, png_doublep filter_costs)); #endif #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ /* Heuristic used for row filter selection. These defines should NOT be * changed. */ #define PNG_FILTER_HEURISTIC_DEFAULT 0 /* Currently "UNWEIGHTED" */ #define PNG_FILTER_HEURISTIC_UNWEIGHTED 1 /* Used by libpng < 0.95 */ #define PNG_FILTER_HEURISTIC_WEIGHTED 2 /* Experimental feature */ #define PNG_FILTER_HEURISTIC_LAST 3 /* Not a valid value */ /* Set the library compression level. Currently, valid values range from * 0 - 9, corresponding directly to the zlib compression levels 0 - 9 * (0 - no compression, 9 - "maximal" compression). Note that tests have * shown that zlib compression levels 3-6 usually perform as well as level 9 * for PNG images, and do considerably fewer caclulations. In the future, * these values may not correspond directly to the zlib compression levels. */ extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr, int level)); extern PNG_EXPORT(void,png_set_compression_mem_level) PNGARG((png_structp png_ptr, int mem_level)); extern PNG_EXPORT(void,png_set_compression_strategy) PNGARG((png_structp png_ptr, int strategy)); extern PNG_EXPORT(void,png_set_compression_window_bits) PNGARG((png_structp png_ptr, int window_bits)); extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr, int method)); /* These next functions are called for input/output, memory, and error * handling. They are in the file pngrio.c, pngwio.c, and pngerror.c, * and call standard C I/O routines such as fread(), fwrite(), and * fprintf(). These functions can be made to use other I/O routines * at run time for those applications that need to handle I/O in a * different manner by calling png_set_???_fn(). See libpng.txt for * more information. */ #ifdef PNG_STDIO_SUPPORTED /* Initialize the input/output for the PNG file to the default functions. */ extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp)); #endif /* Replace the (error and abort), and warning functions with user * supplied functions. If no messages are to be printed you must still * write and use replacement functions. The replacement error_fn should * still do a longjmp to the last setjmp location if you are using this * method of error handling. If error_fn or warning_fn is NULL, the * default function will be used. */ extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn)); /* Return the user pointer associated with the error functions */ extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr)); /* Replace the default data output functions with a user supplied one(s). * If buffered output is not used, then output_flush_fn can be set to NULL. * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time * output_flush_fn will be ignored (and thus can be NULL). * It is probably a mistake to use NULL for output_flush_fn if * write_data_fn is not also NULL unless you have built libpng with * PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's * default flush function, which uses the standard *FILE structure, will * be used. */ extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)); /* Replace the default data input function with a user supplied one. */ extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn)); /* Return the user pointer associated with the I/O functions */ extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr, png_read_status_ptr read_row_fn)); extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr, png_write_status_ptr write_row_fn)); #ifdef PNG_USER_MEM_SUPPORTED /* Replace the default memory allocation functions with user supplied one(s). */ extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)); /* Return the user pointer associated with the memory functions */ extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr)); #endif #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp png_ptr, png_user_transform_ptr read_user_transform_fn)); #endif #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp png_ptr, png_user_transform_ptr write_user_transform_fn)); #endif #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_LEGACY_SUPPORTED) extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp png_ptr, png_voidp user_transform_ptr, int user_transform_depth, int user_transform_channels)); /* Return the user pointer associated with the user transform functions */ extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_USER_CHUNKS_SUPPORTED extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr, png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn)); extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp png_ptr)); #endif #ifdef PNG_PROGRESSIVE_READ_SUPPORTED /* Sets the function callbacks for the push reader, and a pointer to a * user-defined structure available to the callback functions. */ extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr, png_voidp progressive_ptr, png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn)); /* Returns the user pointer associated with the push read functions */ extern PNG_EXPORT(png_voidp,png_get_progressive_ptr) PNGARG((png_structp png_ptr)); /* Function to be called when data becomes available */ extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytep buffer, png_size_t buffer_size)); /* Function that combines rows. Not very much different than the * png_combine_row() call. Is this even used????? */ extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr, png_bytep old_row, png_bytep new_row)); #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr, png_uint_32 size)) PNG_ALLOCATED; #ifdef PNG_1_0_X # define png_malloc_warn png_malloc #else /* Added at libpng version 1.2.4 */ extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr, png_uint_32 size)) PNG_ALLOCATED; #endif /* Frees a pointer allocated by png_malloc() */ extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr)); #ifdef PNG_1_0_X /* Function to allocate memory for zlib. */ extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items, uInt size)); /* Function to free memory for zlib */ extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr)); #endif /* Free data that was allocated internally */ extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 free_me, int num)); #ifdef PNG_FREE_ME_SUPPORTED /* Reassign responsibility for freeing existing data, whether allocated * by libpng or by the application */ extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr, png_infop info_ptr, int freer, png_uint_32 mask)); #endif /* Assignments for png_data_freer */ #define PNG_DESTROY_WILL_FREE_DATA 1 #define PNG_SET_WILL_FREE_DATA 1 #define PNG_USER_WILL_FREE_DATA 2 /* Flags for png_ptr->free_me and info_ptr->free_me */ #define PNG_FREE_HIST 0x0008 #define PNG_FREE_ICCP 0x0010 #define PNG_FREE_SPLT 0x0020 #define PNG_FREE_ROWS 0x0040 #define PNG_FREE_PCAL 0x0080 #define PNG_FREE_SCAL 0x0100 #define PNG_FREE_UNKN 0x0200 #define PNG_FREE_LIST 0x0400 #define PNG_FREE_PLTE 0x1000 #define PNG_FREE_TRNS 0x2000 #define PNG_FREE_TEXT 0x4000 #define PNG_FREE_ALL 0x7fff #define PNG_FREE_MUL 0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */ #ifdef PNG_USER_MEM_SUPPORTED extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr, png_uint_32 size)) PNG_ALLOCATED; extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr, png_voidp ptr)); #endif extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr, png_voidp s1, png_voidp s2, png_uint_32 size)) PNG_DEPRECATED; extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr, png_voidp s1, int value, png_uint_32 size)) PNG_DEPRECATED; #if defined(USE_FAR_KEYWORD) /* memory model conversion function */ extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr, int check)); #endif /* USE_FAR_KEYWORD */ #ifndef PNG_NO_ERROR_TEXT /* Fatal error in PNG image of libpng - can't continue */ extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr, png_const_charp error_message)) PNG_NORETURN; /* The same, but the chunk name is prepended to the error string. */ extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr, png_const_charp error_message)) PNG_NORETURN; #else /* Fatal error in PNG image of libpng - can't continue */ extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr)) PNG_NORETURN; #endif #ifndef PNG_NO_WARNINGS /* Non-fatal error in libpng. Can continue, but may have a problem. */ extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr, png_const_charp warning_message)); #ifdef PNG_READ_SUPPORTED /* Non-fatal error in libpng, chunk name is prepended to message. */ extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr, png_const_charp warning_message)); #endif /* PNG_READ_SUPPORTED */ #endif /* PNG_NO_WARNINGS */ /* The png_set_ functions are for storing values in the png_info_struct. * Similarly, the png_get_ calls are used to read values from the * png_info_struct, either storing the parameters in the passed variables, or * setting pointers into the png_info_struct where the data is stored. The * png_get_ functions return a non-zero value if the data was available * in info_ptr, or return zero and do not change any of the parameters if the * data was not available. * * These functions should be used instead of directly accessing png_info * to avoid problems with future changes in the size and internal layout of * png_info_struct. */ /* Returns "flag" if chunk data is valid in info_ptr. */ extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)); /* Returns number of bytes needed to hold a transformed row. */ extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifdef PNG_INFO_IMAGE_SUPPORTED /* Returns row_pointers, which is an array of pointers to scanlines that was * returned from png_read_png(). */ extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Set row_pointers, which is an array of pointers to scanlines for use * by png_write_png(). */ extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)); #endif /* Returns number of color channels in image. */ extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifdef PNG_EASY_ACCESS_SUPPORTED /* Returns image width in pixels. */ extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image height in pixels. */ extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image bit_depth. */ extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image color_type. */ extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image filter_type. */ extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image interlace_type. */ extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image compression_type. */ extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns image resolution in pixels per meter, from pHYs chunk data. */ extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp png_ptr, png_infop info_ptr)); /* Returns pixel aspect ratio, computed from pHYs chunk data. */ #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif /* Returns image x, y offset in pixels or microns, from oFFs chunk data. */ extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp png_ptr, png_infop info_ptr)); extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif /* PNG_EASY_ACCESS_SUPPORTED */ /* Returns pointer to signature string read from PNG header */ extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifdef PNG_bKGD_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_16p *background)); #endif #ifdef PNG_bKGD_SUPPORTED extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_16p background)); #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr, png_infop info_ptr, double *white_x, double *white_y, double *red_x, double *red_y, double *green_x, double *green_y, double *blue_x, double *blue_y)); #endif #ifdef PNG_FIXED_POINT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr, png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y, png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point *int_blue_x, png_fixed_point *int_blue_y)); #endif #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr, png_infop info_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y)); #endif #ifdef PNG_FIXED_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr, png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y, png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, png_fixed_point int_blue_y)); #endif #endif #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr, png_infop info_ptr, double *file_gamma)); #endif extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr, png_infop info_ptr, png_fixed_point *int_file_gamma)); #endif #ifdef PNG_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr, png_infop info_ptr, double file_gamma)); #endif extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr, png_infop info_ptr, png_fixed_point int_file_gamma)); #endif #ifdef PNG_hIST_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)); #endif #ifdef PNG_hIST_SUPPORTED extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)); #endif extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_method, int *compression_method, int *filter_method)); extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_method, int compression_method, int filter_method)); #ifdef PNG_oFFs_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr, png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)); #endif #ifdef PNG_oFFs_SUPPORTED extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr, png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y, int unit_type)); #endif #ifdef PNG_pCAL_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr, png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams, png_charp *units, png_charpp *params)); #endif #ifdef PNG_pCAL_SUPPORTED extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr, png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)); #endif #ifdef PNG_pHYs_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)); #endif #ifdef PNG_pHYs_SUPPORTED extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)); #endif extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr, png_infop info_ptr, png_colorp *palette, int *num_palette)); extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr, png_infop info_ptr, png_colorp palette, int num_palette)); #ifdef PNG_sBIT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)); #endif #ifdef PNG_sBIT_SUPPORTED extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_8p sig_bit)); #endif #ifdef PNG_sRGB_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr, png_infop info_ptr, int *intent)); #endif #ifdef PNG_sRGB_SUPPORTED extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr, png_infop info_ptr, int intent)); extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr, png_infop info_ptr, int intent)); #endif #ifdef PNG_iCCP_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr, png_infop info_ptr, png_charpp name, int *compression_type, png_charpp profile, png_uint_32 *proflen)); /* Note to maintainer: profile should be png_bytepp */ #endif #ifdef PNG_iCCP_SUPPORTED extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr, png_infop info_ptr, png_charp name, int compression_type, png_charp profile, png_uint_32 proflen)); /* Note to maintainer: profile should be png_bytep */ #endif #ifdef PNG_sPLT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr, png_infop info_ptr, png_sPLT_tpp entries)); #endif #ifdef PNG_sPLT_SUPPORTED extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr, png_infop info_ptr, png_sPLT_tp entries, int nentries)); #endif #ifdef PNG_TEXT_SUPPORTED /* png_get_text also returns the number of text chunks in *num_text */ extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr, int *num_text)); #endif /* * Note while png_set_text() will accept a structure whose text, * language, and translated keywords are NULL pointers, the structure * returned by png_get_text will always contain regular * zero-terminated C strings. They might be empty strings but * they will never be NULL pointers. */ #ifdef PNG_TEXT_SUPPORTED extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, int num_text)); #endif #ifdef PNG_tIME_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)); #endif #ifdef PNG_tIME_SUPPORTED extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr, png_infop info_ptr, png_timep mod_time)); #endif #ifdef PNG_tRNS_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values)); #endif #ifdef PNG_tRNS_SUPPORTED extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytep trans, int num_trans, png_color_16p trans_values)); #endif #ifdef PNG_tRNS_SUPPORTED #endif #ifdef PNG_sCAL_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr, png_infop info_ptr, int *unit, double *width, double *height)); #else #ifdef PNG_FIXED_POINT_SUPPORTED extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr, png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight)); #endif #endif #endif /* PNG_sCAL_SUPPORTED */ #ifdef PNG_sCAL_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr, png_infop info_ptr, int unit, double width, double height)); #else #ifdef PNG_FIXED_POINT_SUPPORTED extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr, png_infop info_ptr, int unit, png_charp swidth, png_charp sheight)); #endif #endif #endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */ #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED /* Provide a list of chunks and how they are to be handled, if the built-in handling or default unknown chunk handling is not desired. Any chunks not listed will be handled in the default manner. The IHDR and IEND chunks must not be listed. keep = 0: follow default behaviour = 1: do not keep = 2: keep only if safe-to-copy = 3: keep even if unsafe-to-copy */ extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp png_ptr, int keep, png_bytep chunk_list, int num_chunks)); PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep chunk_name)); #endif #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr, png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)); extern PNG_EXPORT(void, png_set_unknown_chunk_location) PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location)); extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp png_ptr, png_infop info_ptr, png_unknown_chunkpp entries)); #endif /* Png_free_data() will turn off the "valid" flag for anything it frees. * If you need to turn it off for a chunk that your application has freed, * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */ extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr, png_infop info_ptr, int mask)); #ifdef PNG_INFO_IMAGE_SUPPORTED /* The "params" pointer is currently not used and is for future expansion. */ extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr, png_infop info_ptr, int transforms, png_voidp params)); extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr, png_infop info_ptr, int transforms, png_voidp params)); #endif /* Define PNG_DEBUG at compile time for debugging information. Higher * numbers for PNG_DEBUG mean more debugging information. This has * only been added since version 0.95 so it is not implemented throughout * libpng yet, but more support will be added as needed. */ #ifdef PNG_DEBUG #if (PNG_DEBUG > 0) #if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER) #include #if (PNG_DEBUG > 1) #ifndef _DEBUG # define _DEBUG #endif #ifndef png_debug #define png_debug(l,m) _RPT0(_CRT_WARN,m PNG_STRING_NEWLINE) #endif #ifndef png_debug1 #define png_debug1(l,m,p1) _RPT1(_CRT_WARN,m PNG_STRING_NEWLINE,p1) #endif #ifndef png_debug2 #define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m PNG_STRING_NEWLINE,p1,p2) #endif #endif #else /* PNG_DEBUG_FILE || !_MSC_VER */ #ifndef PNG_DEBUG_FILE #define PNG_DEBUG_FILE stderr #endif /* PNG_DEBUG_FILE */ #if (PNG_DEBUG > 1) /* Note: ["%s"m PNG_STRING_NEWLINE] probably does not work on non-ISO * compilers. */ # ifdef __STDC__ # ifndef png_debug # define png_debug(l,m) \ { \ int num_tabs=l; \ fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \ } # endif # ifndef png_debug1 # define png_debug1(l,m,p1) \ { \ int num_tabs=l; \ fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \ } # endif # ifndef png_debug2 # define png_debug2(l,m,p1,p2) \ { \ int num_tabs=l; \ fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \ } # endif # else /* __STDC __ */ # ifndef png_debug # define png_debug(l,m) \ { \ int num_tabs=l; \ char format[256]; \ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ m,PNG_STRING_NEWLINE); \ fprintf(PNG_DEBUG_FILE,format); \ } # endif # ifndef png_debug1 # define png_debug1(l,m,p1) \ { \ int num_tabs=l; \ char format[256]; \ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ m,PNG_STRING_NEWLINE); \ fprintf(PNG_DEBUG_FILE,format,p1); \ } # endif # ifndef png_debug2 # define png_debug2(l,m,p1,p2) \ { \ int num_tabs=l; \ char format[256]; \ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ m,PNG_STRING_NEWLINE); \ fprintf(PNG_DEBUG_FILE,format,p1,p2); \ } # endif # endif /* __STDC __ */ #endif /* (PNG_DEBUG > 1) */ #endif /* _MSC_VER */ #endif /* (PNG_DEBUG > 0) */ #endif /* PNG_DEBUG */ #ifndef png_debug #define png_debug(l, m) #endif #ifndef png_debug1 #define png_debug1(l, m, p1) #endif #ifndef png_debug2 #define png_debug2(l, m, p1, p2) #endif extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr)); #ifdef PNG_MNG_FEATURES_SUPPORTED extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp png_ptr, png_uint_32 mng_features_permitted)); #endif /* For use in png_set_keep_unknown, added to version 1.2.6 */ #define PNG_HANDLE_CHUNK_AS_DEFAULT 0 #define PNG_HANDLE_CHUNK_NEVER 1 #define PNG_HANDLE_CHUNK_IF_SAFE 2 #define PNG_HANDLE_CHUNK_ALWAYS 3 /* Added to version 1.2.0 */ #ifdef PNG_ASSEMBLER_CODE_SUPPORTED #ifdef PNG_MMX_CODE_SUPPORTED #define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED 0x01 /* not user-settable */ #define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU 0x02 /* not user-settable */ #define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW 0x04 #define PNG_ASM_FLAG_MMX_READ_INTERLACE 0x08 #define PNG_ASM_FLAG_MMX_READ_FILTER_SUB 0x10 #define PNG_ASM_FLAG_MMX_READ_FILTER_UP 0x20 #define PNG_ASM_FLAG_MMX_READ_FILTER_AVG 0x40 #define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80 #define PNG_ASM_FLAGS_INITIALIZED 0x80000000 /* not user-settable */ #define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ | PNG_ASM_FLAG_MMX_READ_INTERLACE \ | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ | PNG_ASM_FLAG_MMX_READ_FILTER_UP \ | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ) #define PNG_MMX_WRITE_FLAGS ( 0 ) #define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \ | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU \ | PNG_MMX_READ_FLAGS \ | PNG_MMX_WRITE_FLAGS ) #define PNG_SELECT_READ 1 #define PNG_SELECT_WRITE 2 #endif /* PNG_MMX_CODE_SUPPORTED */ #ifndef PNG_1_0_X /* pngget.c */ extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask) PNGARG((int flag_select, int *compilerID)); /* pngget.c */ extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask) PNGARG((int flag_select)); /* pngget.c */ extern PNG_EXPORT(png_uint_32,png_get_asm_flags) PNGARG((png_structp png_ptr)); /* pngget.c */ extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold) PNGARG((png_structp png_ptr)); /* pngget.c */ extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold) PNGARG((png_structp png_ptr)); /* pngset.c */ extern PNG_EXPORT(void,png_set_asm_flags) PNGARG((png_structp png_ptr, png_uint_32 asm_flags)); /* pngset.c */ extern PNG_EXPORT(void,png_set_mmx_thresholds) PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold, png_uint_32 mmx_rowbytes_threshold)); #endif /* PNG_1_0_X */ #ifndef PNG_1_0_X /* png.c, pnggccrd.c, or pngvcrd.c */ extern PNG_EXPORT(int,png_mmx_support) PNGARG((void)); #endif /* PNG_1_0_X */ #endif /* PNG_ASSEMBLER_CODE_SUPPORTED */ /* Strip the prepended error numbers ("#nnn ") from error and warning * messages before passing them to the error or warning handler. */ #ifdef PNG_ERROR_NUMBERS_SUPPORTED extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp png_ptr, png_uint_32 strip_mode)); #endif /* Added at libpng-1.2.6 */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max)); extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp png_ptr)); extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp png_ptr)); #endif /* Maintainer: Put new public prototypes here ^, in libpng.3, and in * project defs */ #ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED /* With these routines we avoid an integer divide, which will be slower on * most machines. However, it does take more operations than the corresponding * divide method, so it may be slower on a few RISC systems. There are two * shifts (by 8 or 16 bits) and an addition, versus a single integer divide. * * Note that the rounding factors are NOT supposed to be the same! 128 and * 32768 are correct for the NODIV code; 127 and 32767 are correct for the * standard method. * * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ] */ /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */ # define png_composite(composite, fg, alpha, bg) \ { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \ + (png_uint_16)(bg)*(png_uint_16)(255 - \ (png_uint_16)(alpha)) + (png_uint_16)128); \ (composite) = (png_byte)((temp + (temp >> 8)) >> 8); } # define png_composite_16(composite, fg, alpha, bg) \ { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \ + (png_uint_32)(bg)*(png_uint_32)(65535L - \ (png_uint_32)(alpha)) + (png_uint_32)32768L); \ (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); } #else /* Standard method using integer division */ # define png_composite(composite, fg, alpha, bg) \ (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) + \ (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \ (png_uint_16)127) / 255) # define png_composite_16(composite, fg, alpha, bg) \ (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \ (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) + \ (png_uint_32)32767) / (png_uint_32)65535L) #endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */ /* Inline macros to do direct reads of bytes from the input buffer. These * require that you are using an architecture that uses PNG byte ordering * (MSB first) and supports unaligned data storage. I think that PowerPC * in big-endian mode and 680x0 are the only ones that will support this. * The x86 line of processors definitely do not. The png_get_int_32() * routine also assumes we are using two's complement format for negative * values, which is almost certainly true. */ #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED # define png_get_uint_32(buf) ( *((png_uint_32p) (buf))) # define png_get_uint_16(buf) ( *((png_uint_16p) (buf))) # define png_get_int_32(buf) ( *((png_int_32p) (buf))) #else extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf)); extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf)); extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf)); #endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */ extern PNG_EXPORT(png_uint_32,png_get_uint_31) PNGARG((png_structp png_ptr, png_bytep buf)); /* No png_get_int_16 -- may be added if there's a real need for it. */ /* Place a 32-bit number into a buffer in PNG byte order (big-endian). */ extern PNG_EXPORT(void,png_save_uint_32) PNGARG((png_bytep buf, png_uint_32 i)); extern PNG_EXPORT(void,png_save_int_32) PNGARG((png_bytep buf, png_int_32 i)); /* Place a 16-bit number into a buffer in PNG byte order. * The parameter is declared unsigned int, not png_uint_16, * just to avoid potential problems on pre-ANSI C compilers. */ extern PNG_EXPORT(void,png_save_uint_16) PNGARG((png_bytep buf, unsigned int i)); /* No png_save_int_16 -- may be added if there's a real need for it. */ /* ************************************************************************* */ /* These next functions are used internally in the code. They generally * shouldn't be used unless you are writing code to add or replace some * functionality in libpng. More information about most functions can * be found in the files where the functions are located. */ /* Various modes of operation, that are visible to applications because * they are used for unknown chunk location. */ #define PNG_HAVE_IHDR 0x01 #define PNG_HAVE_PLTE 0x02 #define PNG_HAVE_IDAT 0x04 #define PNG_AFTER_IDAT 0x08 /* Have complete zlib datastream */ #define PNG_HAVE_IEND 0x10 #ifdef PNG_INTERNAL /* More modes of operation. Note that after an init, mode is set to * zero automatically when the structure is created. */ #define PNG_HAVE_gAMA 0x20 #define PNG_HAVE_cHRM 0x40 #define PNG_HAVE_sRGB 0x80 #define PNG_HAVE_CHUNK_HEADER 0x100 #define PNG_WROTE_tIME 0x200 #define PNG_WROTE_INFO_BEFORE_PLTE 0x400 #define PNG_BACKGROUND_IS_GRAY 0x800 #define PNG_HAVE_PNG_SIGNATURE 0x1000 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */ /* Flags for the transformations the PNG library does on the image data */ #define PNG_BGR 0x0001 #define PNG_INTERLACE 0x0002 #define PNG_PACK 0x0004 #define PNG_SHIFT 0x0008 #define PNG_SWAP_BYTES 0x0010 #define PNG_INVERT_MONO 0x0020 #define PNG_DITHER 0x0040 #define PNG_BACKGROUND 0x0080 #define PNG_BACKGROUND_EXPAND 0x0100 /* 0x0200 unused */ #define PNG_16_TO_8 0x0400 #define PNG_RGBA 0x0800 #define PNG_EXPAND 0x1000 #define PNG_GAMMA 0x2000 #define PNG_GRAY_TO_RGB 0x4000 #define PNG_FILLER 0x8000L #define PNG_PACKSWAP 0x10000L #define PNG_SWAP_ALPHA 0x20000L #define PNG_STRIP_ALPHA 0x40000L #define PNG_INVERT_ALPHA 0x80000L #define PNG_USER_TRANSFORM 0x100000L #define PNG_RGB_TO_GRAY_ERR 0x200000L #define PNG_RGB_TO_GRAY_WARN 0x400000L #define PNG_RGB_TO_GRAY 0x600000L /* two bits, RGB_TO_GRAY_ERR|WARN */ /* 0x800000L Unused */ #define PNG_ADD_ALPHA 0x1000000L /* Added to libpng-1.2.7 */ #define PNG_EXPAND_tRNS 0x2000000L /* Added to libpng-1.2.9 */ #define PNG_PREMULTIPLY_ALPHA 0x4000000L /* Added to libpng-1.2.41 */ /* by volker */ /* 0x8000000L unused */ /* 0x10000000L unused */ /* 0x20000000L unused */ /* 0x40000000L unused */ /* Flags for png_create_struct */ #define PNG_STRUCT_PNG 0x0001 #define PNG_STRUCT_INFO 0x0002 /* Scaling factor for filter heuristic weighting calculations */ #define PNG_WEIGHT_SHIFT 8 #define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT)) #define PNG_COST_SHIFT 3 #define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT)) /* Flags for the png_ptr->flags rather than declaring a byte for each one */ #define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001 #define PNG_FLAG_ZLIB_CUSTOM_LEVEL 0x0002 #define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL 0x0004 #define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS 0x0008 #define PNG_FLAG_ZLIB_CUSTOM_METHOD 0x0010 #define PNG_FLAG_ZLIB_FINISHED 0x0020 #define PNG_FLAG_ROW_INIT 0x0040 #define PNG_FLAG_FILLER_AFTER 0x0080 #define PNG_FLAG_CRC_ANCILLARY_USE 0x0100 #define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200 #define PNG_FLAG_CRC_CRITICAL_USE 0x0400 #define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800 #define PNG_FLAG_FREE_PLTE 0x1000 #define PNG_FLAG_FREE_TRNS 0x2000 #define PNG_FLAG_FREE_HIST 0x4000 #define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000L #define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000L #define PNG_FLAG_LIBRARY_MISMATCH 0x20000L #define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000L #define PNG_FLAG_STRIP_ERROR_TEXT 0x80000L #define PNG_FLAG_MALLOC_NULL_MEM_OK 0x100000L #define PNG_FLAG_ADD_ALPHA 0x200000L /* Added to libpng-1.2.8 */ #define PNG_FLAG_STRIP_ALPHA 0x400000L /* Added to libpng-1.2.8 */ /* 0x800000L unused */ /* 0x1000000L unused */ /* 0x2000000L unused */ /* 0x4000000L unused */ /* 0x8000000L unused */ /* 0x10000000L unused */ /* 0x20000000L unused */ /* 0x40000000L unused */ #define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ PNG_FLAG_CRC_ANCILLARY_NOWARN) #define PNG_FLAG_CRC_CRITICAL_MASK (PNG_FLAG_CRC_CRITICAL_USE | \ PNG_FLAG_CRC_CRITICAL_IGNORE) #define PNG_FLAG_CRC_MASK (PNG_FLAG_CRC_ANCILLARY_MASK | \ PNG_FLAG_CRC_CRITICAL_MASK) /* Save typing and make code easier to understand */ #define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \ abs((int)((c1).green) - (int)((c2).green)) + \ abs((int)((c1).blue) - (int)((c2).blue))) /* Added to libpng-1.2.6 JB */ #define PNG_ROWBYTES(pixel_bits, width) \ ((pixel_bits) >= 8 ? \ ((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \ (( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) ) /* PNG_OUT_OF_RANGE returns true if value is outside the range * ideal-delta..ideal+delta. Each argument is evaluated twice. * "ideal" and "delta" should be constants, normally simple * integers, "value" a variable. Added to libpng-1.2.6 JB */ #define PNG_OUT_OF_RANGE(value, ideal, delta) \ ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) ) /* Variables declared in png.c - only it needs to define PNG_NO_EXTERN */ #if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN) /* Place to hold the signature string for a PNG file. */ #ifdef PNG_USE_GLOBAL_ARRAYS PNG_EXPORT_VAR (PNG_CONST png_byte FARDATA) png_sig[8]; #else #endif #endif /* PNG_NO_EXTERN */ /* Constant strings for known chunk types. If you need to add a chunk, * define the name here, and add an invocation of the macro in png.c and * wherever it's needed. */ #define PNG_IHDR png_byte png_IHDR[5] = { 73, 72, 68, 82, '\0'} #define PNG_IDAT png_byte png_IDAT[5] = { 73, 68, 65, 84, '\0'} #define PNG_IEND png_byte png_IEND[5] = { 73, 69, 78, 68, '\0'} #define PNG_PLTE png_byte png_PLTE[5] = { 80, 76, 84, 69, '\0'} #define PNG_bKGD png_byte png_bKGD[5] = { 98, 75, 71, 68, '\0'} #define PNG_cHRM png_byte png_cHRM[5] = { 99, 72, 82, 77, '\0'} #define PNG_gAMA png_byte png_gAMA[5] = {103, 65, 77, 65, '\0'} #define PNG_hIST png_byte png_hIST[5] = {104, 73, 83, 84, '\0'} #define PNG_iCCP png_byte png_iCCP[5] = {105, 67, 67, 80, '\0'} #define PNG_iTXt png_byte png_iTXt[5] = {105, 84, 88, 116, '\0'} #define PNG_oFFs png_byte png_oFFs[5] = {111, 70, 70, 115, '\0'} #define PNG_pCAL png_byte png_pCAL[5] = {112, 67, 65, 76, '\0'} #define PNG_sCAL png_byte png_sCAL[5] = {115, 67, 65, 76, '\0'} #define PNG_pHYs png_byte png_pHYs[5] = {112, 72, 89, 115, '\0'} #define PNG_sBIT png_byte png_sBIT[5] = {115, 66, 73, 84, '\0'} #define PNG_sPLT png_byte png_sPLT[5] = {115, 80, 76, 84, '\0'} #define PNG_sRGB png_byte png_sRGB[5] = {115, 82, 71, 66, '\0'} #define PNG_tEXt png_byte png_tEXt[5] = {116, 69, 88, 116, '\0'} #define PNG_tIME png_byte png_tIME[5] = {116, 73, 77, 69, '\0'} #define PNG_tRNS png_byte png_tRNS[5] = {116, 82, 78, 83, '\0'} #define PNG_zTXt png_byte png_zTXt[5] = {122, 84, 88, 116, '\0'} #ifdef PNG_USE_GLOBAL_ARRAYS PNG_EXPORT_VAR (png_byte FARDATA) png_IHDR[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_IDAT[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_IEND[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_PLTE[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_bKGD[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_cHRM[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_gAMA[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_hIST[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_iCCP[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_iTXt[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_oFFs[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_pCAL[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_sCAL[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_pHYs[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_sBIT[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_sPLT[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_sRGB[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_tEXt[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_tIME[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_tRNS[5]; PNG_EXPORT_VAR (png_byte FARDATA) png_zTXt[5]; #endif /* PNG_USE_GLOBAL_ARRAYS */ #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Initialize png_ptr struct for reading, and allocate any other memory. * (old interface - DEPRECATED - use png_create_read_struct instead). */ extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr)) PNG_DEPRECATED; #undef png_read_init #define png_read_init(png_ptr) png_read_init_3(&png_ptr, \ PNG_LIBPNG_VER_STRING, png_sizeof(png_struct)); #endif extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr, png_const_charp user_png_ver, png_size_t png_struct_size)); #if defined(PNG_1_0_X) || defined (PNG_1_2_X) extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr, png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t png_info_size)); #endif #if defined(PNG_1_0_X) || defined (PNG_1_2_X) /* Initialize png_ptr struct for writing, and allocate any other memory. * (old interface - DEPRECATED - use png_create_write_struct instead). */ extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr)) PNG_DEPRECATED; #undef png_write_init #define png_write_init(png_ptr) png_write_init_3(&png_ptr, \ PNG_LIBPNG_VER_STRING, png_sizeof(png_struct)); #endif extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr, png_const_charp user_png_ver, png_size_t png_struct_size)); extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr, png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t png_info_size)); /* Allocate memory for an internal libpng struct */ PNG_EXTERN png_voidp png_create_struct PNGARG((int type)) PNG_PRIVATE; /* Free memory from internal libpng struct */ PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr)) PNG_PRIVATE; PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)) PNG_PRIVATE; PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr, png_free_ptr free_fn, png_voidp mem_ptr)) PNG_PRIVATE; /* Free any memory that info_ptr points to and reset struct. */ PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; #ifndef PNG_1_0_X /* Function to allocate memory for zlib. */ PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size)) PNG_PRIVATE; /* Function to free memory for zlib */ PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr)) PNG_PRIVATE; #ifdef PNG_SIZE_T /* Function to convert a sizeof an item to png_sizeof item */ PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size)) PNG_PRIVATE; #endif /* Next four functions are used internally as callbacks. PNGAPI is required * but not PNG_EXPORT. PNGAPI added at libpng version 1.2.3. */ PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)) PNG_PRIVATE; #ifdef PNG_PROGRESSIVE_READ_SUPPORTED PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr, png_bytep buffer, png_size_t length)) PNG_PRIVATE; #endif PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)) PNG_PRIVATE; #ifdef PNG_WRITE_FLUSH_SUPPORTED #ifdef PNG_STDIO_SUPPORTED PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr)) PNG_PRIVATE; #endif #endif #else /* PNG_1_0_X */ #ifdef PNG_PROGRESSIVE_READ_SUPPORTED PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr, png_bytep buffer, png_size_t length)) PNG_PRIVATE; #endif #endif /* PNG_1_0_X */ /* Reset the CRC variable */ PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Write the "data" buffer to whatever output you are using. */ PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)) PNG_PRIVATE; /* Read data from whatever input you are using into the "data" buffer */ PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)) PNG_PRIVATE; /* Read bytes into buf, and update png_ptr->crc */ PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, png_size_t length)) PNG_PRIVATE; /* Decompress data in a chunk that uses compression */ #if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \ defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr, int comp_type, png_size_t chunklength, png_size_t prefix_length, png_size_t *data_length)) PNG_PRIVATE; #endif /* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */ PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip) PNG_PRIVATE); /* Read the CRC from the file and compare it to the libpng calculated CRC */ PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Calculate the CRC over a section of data. Note that we are only * passing a maximum of 64K on systems that have this as a memory limit, * since this is the maximum buffer size we can specify. */ PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr, png_size_t length)) PNG_PRIVATE; #ifdef PNG_WRITE_FLUSH_SUPPORTED PNG_EXTERN void png_flush PNGARG((png_structp png_ptr)) PNG_PRIVATE; #endif /* Simple function to write the signature */ PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Write various chunks */ /* Write the IHDR chunk, and update the png_struct with the necessary * information. */ PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_method, int filter_method, int interlace_method)) PNG_PRIVATE; PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)) PNG_PRIVATE; PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data, png_size_t length)) PNG_PRIVATE; PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr)) PNG_PRIVATE; #ifdef PNG_WRITE_gAMA_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma)) PNG_PRIVATE; #endif #ifdef PNG_FIXED_POINT_SUPPORTED PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point file_gamma)) PNG_PRIVATE; #endif #endif #ifdef PNG_WRITE_sBIT_SUPPORTED PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit, int color_type)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_cHRM_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y)) PNG_PRIVATE; #endif #ifdef PNG_FIXED_POINT_SUPPORTED PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y, png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, png_fixed_point int_blue_y)) PNG_PRIVATE; #endif #endif #ifdef PNG_WRITE_sRGB_SUPPORTED PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr, int intent)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_iCCP_SUPPORTED PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr, png_charp name, int compression_type, png_charp profile, int proflen)) PNG_PRIVATE; /* Note to maintainer: profile should be png_bytep */ #endif #ifdef PNG_WRITE_sPLT_SUPPORTED PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr, png_sPLT_tp palette)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_tRNS_SUPPORTED PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans, png_color_16p values, int number, int color_type)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_bKGD_SUPPORTED PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr, png_color_16p values, int color_type)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_hIST_SUPPORTED PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist, int num_hist)) PNG_PRIVATE; #endif #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr, png_charp key, png_charpp new_key)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_tEXt_SUPPORTED PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key, png_charp text, png_size_t text_len)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_zTXt_SUPPORTED PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key, png_charp text, png_size_t text_len, int compression)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_iTXt_SUPPORTED PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr, int compression, png_charp key, png_charp lang, png_charp lang_key, png_charp text)) PNG_PRIVATE; #endif #ifdef PNG_TEXT_SUPPORTED /* Added at version 1.0.14 and 1.2.4 */ PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, int num_text)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_oFFs_SUPPORTED PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, int unit_type)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_pCAL_SUPPORTED PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_pHYs_SUPPORTED PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, int unit_type)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_tIME_SUPPORTED PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr, png_timep mod_time)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_sCAL_SUPPORTED #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr, int unit, double width, double height)) PNG_PRIVATE; #else #ifdef PNG_FIXED_POINT_SUPPORTED PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr, int unit, png_charp width, png_charp height)) PNG_PRIVATE; #endif #endif #endif /* Called when finished processing a row of data */ PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Internal use only. Called before first row of data */ PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr)) PNG_PRIVATE; #ifdef PNG_READ_GAMMA_SUPPORTED PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr)) PNG_PRIVATE; #endif /* Combine a row of data, dealing with alpha, etc. if requested */ PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row, int mask)) PNG_PRIVATE; #ifdef PNG_READ_INTERLACING_SUPPORTED /* Expand an interlaced row */ /* OLD pre-1.0.9 interface: PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info, png_bytep row, int pass, png_uint_32 transformations)) PNG_PRIVATE; */ PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr)) PNG_PRIVATE; #endif /* GRR TO DO (2.0 or whenever): simplify other internal calling interfaces */ #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Grab pixels out of a row for an interlaced pass */ PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info, png_bytep row, int pass)) PNG_PRIVATE; #endif /* Unfilter a row */ PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr, png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter)) PNG_PRIVATE; /* Choose the best filter to use and filter the row data */ PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr, png_row_infop row_info)) PNG_PRIVATE; /* Write out the filtered row. */ PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr, png_bytep filtered_row)) PNG_PRIVATE; /* Finish a row while reading, dealing with interlacing passes, etc. */ PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr)); /* Initialize the row buffers, etc. */ PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Optional call to update the users info structure */ PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; /* These are the functions that do the transformations */ #ifdef PNG_READ_FILLER_SUPPORTED PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info, png_bytep row, png_uint_32 filler, png_uint_32 flags)) PNG_PRIVATE; #endif #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #if defined(PNG_WRITE_FILLER_SUPPORTED) || \ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info, png_bytep row, png_uint_32 flags)) PNG_PRIVATE; #endif #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED) PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_PACK_SUPPORTED PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_SHIFT_SUPPORTED PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row, png_color_8p sig_bits)) PNG_PRIVATE; #endif #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_16_TO_8_SUPPORTED PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_READ_DITHER_SUPPORTED PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info, png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup)) PNG_PRIVATE; # ifdef PNG_CORRECT_PALETTE_SUPPORTED PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr, png_colorp palette, int num_palette)) PNG_PRIVATE; # endif #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_PACK_SUPPORTED PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)) PNG_PRIVATE; #endif #ifdef PNG_WRITE_SHIFT_SUPPORTED PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row, png_color_8p bit_depth)) PNG_PRIVATE; #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED #ifdef PNG_READ_GAMMA_SUPPORTED PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row, png_color_16p trans_values, png_color_16p background, png_color_16p background_1, png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, png_uint_16pp gamma_16_to_1, int gamma_shift)) PNG_PRIVATE; #else PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row, png_color_16p trans_values, png_color_16p background)) PNG_PRIVATE; #endif #endif #ifdef PNG_READ_GAMMA_SUPPORTED PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row, png_bytep gamma_table, png_uint_16pp gamma_16_table, int gamma_shift)) PNG_PRIVATE; #endif #ifdef PNG_READ_EXPAND_SUPPORTED PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info, png_bytep row, png_colorp palette, png_bytep trans, int num_trans)) PNG_PRIVATE; PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info, png_bytep row, png_color_16p trans_value)) PNG_PRIVATE; #endif /* The following decodes the appropriate chunks, and does error correction, * then calls the appropriate callback for the chunk if it is valid. */ /* Decode the IHDR chunk */ PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #ifdef PNG_READ_bKGD_SUPPORTED PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_cHRM_SUPPORTED PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_gAMA_SUPPORTED PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_hIST_SUPPORTED PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_iCCP_SUPPORTED extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif /* PNG_READ_iCCP_SUPPORTED */ #ifdef PNG_READ_iTXt_SUPPORTED PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_oFFs_SUPPORTED PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_pCAL_SUPPORTED PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_pHYs_SUPPORTED PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_sBIT_SUPPORTED PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_sCAL_SUPPORTED PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_sPLT_SUPPORTED extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif /* PNG_READ_sPLT_SUPPORTED */ #ifdef PNG_READ_sRGB_SUPPORTED PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_tEXt_SUPPORTED PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_tIME_SUPPORTED PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_tRNS_SUPPORTED PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif #ifdef PNG_READ_zTXt_SUPPORTED PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; #endif PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr, png_bytep chunk_name)) PNG_PRIVATE; /* Handle the transformations for reading and writing */ PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr)) PNG_PRIVATE; #ifdef PNG_PROGRESSIVE_READ_SUPPORTED PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr, png_bytep buffer, png_size_t buffer_length)) PNG_PRIVATE; PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr, png_bytep buffer, png_size_t buffer_length)) PNG_PRIVATE; PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row)) PNG_PRIVATE; PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr)) PNG_PRIVATE; #ifdef PNG_READ_tEXt_SUPPORTED PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; #endif #ifdef PNG_READ_zTXt_SUPPORTED PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; #endif #ifdef PNG_READ_iTXt_SUPPORTED PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)) PNG_PRIVATE; PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr)) PNG_PRIVATE; #endif #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ #ifdef PNG_MNG_FEATURES_SUPPORTED PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info, png_bytep row)) PNG_PRIVATE; #endif #ifdef PNG_ASSEMBLER_CODE_SUPPORTED #ifdef PNG_MMX_CODE_SUPPORTED /* png.c */ /* PRIVATE */ PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr)) PNG_PRIVATE; #endif #endif /* The following six functions will be exported in libpng-1.4.0. */ #if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED) PNG_EXTERN png_uint_32 png_get_pixels_per_inch PNGARG((png_structp png_ptr, png_infop info_ptr)); PNG_EXTERN png_uint_32 png_get_x_pixels_per_inch PNGARG((png_structp png_ptr, png_infop info_ptr)); PNG_EXTERN png_uint_32 png_get_y_pixels_per_inch PNGARG((png_structp png_ptr, png_infop info_ptr)); PNG_EXTERN float png_get_x_offset_inches PNGARG((png_structp png_ptr, png_infop info_ptr)); PNG_EXTERN float png_get_y_offset_inches PNGARG((png_structp png_ptr, png_infop info_ptr)); #ifdef PNG_pHYs_SUPPORTED PNG_EXTERN png_uint_32 png_get_pHYs_dpi PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)); #endif /* PNG_pHYs_SUPPORTED */ #endif /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */ /* Read the chunk header (length + type name) */ PNG_EXTERN png_uint_32 png_read_chunk_header PNGARG((png_structp png_ptr)) PNG_PRIVATE; /* Added at libpng version 1.2.34 */ #ifdef PNG_cHRM_SUPPORTED PNG_EXTERN int png_check_cHRM_fixed PNGARG((png_structp png_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y, png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, png_fixed_point int_blue_y)) PNG_PRIVATE; #endif #ifdef PNG_cHRM_SUPPORTED #ifdef PNG_CHECK_cHRM_SUPPORTED /* Added at libpng version 1.2.34 */ PNG_EXTERN void png_64bit_product PNGARG((long v1, long v2, unsigned long *hi_product, unsigned long *lo_product)) PNG_PRIVATE; #endif #endif /* Added at libpng version 1.2.41 */ PNG_EXTERN void png_check_IHDR PNGARG((png_structp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type)) PNG_PRIVATE; /* Added at libpng version 1.2.41 */ PNG_EXTERN png_voidp png_calloc PNGARG((png_structp png_ptr, png_uint_32 size)); /* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */ #endif /* PNG_INTERNAL */ #ifdef __cplusplus } #endif #endif /* PNG_VERSION_INFO_ONLY */ /* Do not put anything past this line */ #endif /* PNG_H */ glmark2-2012.08/./android/jni/0000775000175000017500000000000012013417376015116 5ustar alfalf00000000000000glmark2-2012.08/./android/src/0000775000175000017500000000000012013417376015125 5ustar alfalf00000000000000glmark2-2012.08/./android/res/0000775000175000017500000000000012013417376015127 5ustar alfalf00000000000000glmark2-2012.08/./android/assets0000777000175000017500000000000012013417376016704 2../data/ustar alfalf00000000000000glmark2-2012.08/./android/project.properties0000664000175000017500000000106312013417376020122 0ustar alfalf00000000000000# This file is automatically generated by Android Tools. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must be checked in Version Control Systems. # # To customize properties used by the Ant build system edit # "ant.properties", and override values to adapt the script to your # project structure. # # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. target=android-10 glmark2-2012.08/./android/CleanSpec.mk0000664000175000017500000000066712013417376016535 0ustar alfalf00000000000000$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/GLMark2*) $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/app/GLMark2.apk) $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libglmark2-android_intermediates) $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libglmark2-matrix_intermediates) $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libglmark2-png_intermediates) glmark2-2012.08/./android/build.xml0000664000175000017500000000751712013417376016171 0ustar alfalf00000000000000 glmark2-2012.08/./android/AndroidManifest.xml0000664000175000017500000000506512013417376020135 0ustar alfalf00000000000000 glmark2-2012.08/./android/Android.mk0000664000175000017500000000043112013417376016245 0ustar alfalf00000000000000LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_MODULE_TAGS := optional LOCAL_PACKAGE_NAME := GLMark2 LOCAL_JNI_SHARED_LIBRARIES := libglmark2-android include $(BUILD_PACKAGE) include $(LOCAL_PATH)/jni/Android.mk glmark2-2012.08/./android/jni/src0000777000175000017500000000000012013417376017042 2../../src/ustar alfalf00000000000000glmark2-2012.08/./android/jni/Android.mk0000664000175000017500000000561412013417376017035 0ustar alfalf00000000000000LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_MODULE := libglmark2-matrix LOCAL_CFLAGS := -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \ -Wno-error=unused-parameter LOCAL_C_INCLUDES := $(LOCAL_PATH)/src LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libmatrix/*.cc)) LOCAL_SHARED_LIBRARIES := libdl libstlport include external/stlport/libstlport.mk include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libglmark2-png LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libpng/*.c)) LOCAL_C_INCLUDES := external/zlib include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libglmark2-jpeg LOCAL_CFLAGS := -Werror -Wall -Wextra -Wno-error=attributes \ -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/libjpeg-turbo/ LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.c)) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.S)) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/*.c)) include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_MODULE := libglmark2-ideas LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DUSE_GLESv2 -Werror -Wall -Wextra\ -Wnon-virtual-dtor -Wno-error=unused-parameter LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \ $(LOCAL_PATH)/src/libmatrix LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/scene-ideas/*.cc)) LOCAL_SHARED_LIBRARIES := libdl libstlport include external/stlport/libstlport.mk include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_MODULE := libglmark2-android LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas libglmark2-jpeg LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DGLMARK_VERSION="\"2012.08\"" \ -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \ -Wno-error=unused-parameter LOCAL_SHARED_LIBRARIES := liblog libz libEGL libGLESv2 libandroid libdl libstlport LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \ $(LOCAL_PATH)/src/libmatrix \ $(LOCAL_PATH)/src/scene-ideas \ $(LOCAL_PATH)/src/scene-terrain \ $(LOCAL_PATH)/src/libjpeg-turbo \ $(LOCAL_PATH)/src/libpng \ external/zlib LOCAL_SRC_FILES := $(filter-out src/canvas% src/main.cpp, \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/*.cpp))) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/scene-terrain/*.cpp)) \ src/canvas-android.cpp LOCAL_PRELINK_MODULE := false include external/stlport/libstlport.mk include $(BUILD_SHARED_LIBRARY) glmark2-2012.08/./android/jni/Application.mk0000664000175000017500000000013312013417376017707 0ustar alfalf00000000000000APP_STL := stlport_static APP_PLATFORM := android-9 APP_BUILD_SCRIPT := jni/Android.ndk.mk glmark2-2012.08/./android/jni/Android.ndk.mk0000664000175000017500000000507212013417376017606 0ustar alfalf00000000000000LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_MODULE := libglmark2-matrix LOCAL_CFLAGS := -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \ -Wno-error=unused-parameter LOCAL_C_INCLUDES := $(LOCAL_PATH)/src LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libmatrix/*.cc)) include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libglmark2-png LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libpng/*.c)) include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libglmark2-jpeg LOCAL_CFLAGS := -Werror -Wall -Wextra -Wno-error=attributes \ -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/libjpeg-turbo/ LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.c)) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/simd/*.S)) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/libjpeg-turbo/*.c)) include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := .cc LOCAL_MODULE := libglmark2-ideas LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DUSE_GLESv2 -Werror -Wall -Wextra\ -Wnon-virtual-dtor -Wno-error=unused-parameter LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \ $(LOCAL_PATH)/src/libmatrix LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/scene-ideas/*.cc)) include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_MODULE := libglmark2-android LOCAL_STATIC_LIBRARIES := libglmark2-matrix libglmark2-png libglmark2-ideas libglmark2-jpeg LOCAL_CFLAGS := -DGLMARK_DATA_PATH="" -DGLMARK_VERSION="\"2012.08\"" \ -DUSE_GLESv2 -Werror -Wall -Wextra -Wnon-virtual-dtor \ -Wno-error=unused-parameter LOCAL_LDLIBS := -landroid -llog -lGLESv2 -lEGL -lz LOCAL_C_INCLUDES := $(LOCAL_PATH)/src \ $(LOCAL_PATH)/src/libmatrix \ $(LOCAL_PATH)/src/scene-ideas \ $(LOCAL_PATH)/src/scene-terrain \ $(LOCAL_PATH)/src/libjpeg-turbo \ $(LOCAL_PATH)/src/libpng LOCAL_SRC_FILES := $(filter-out src/canvas% src/main.cpp, \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/*.cpp))) \ $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/src/scene-terrain/*.cpp)) \ src/canvas-android.cpp include $(BUILD_SHARED_LIBRARY) glmark2-2012.08/./android/src/org/0000775000175000017500000000000012013417376015714 5ustar alfalf00000000000000glmark2-2012.08/./android/src/org/linaro/0000775000175000017500000000000012013417376017200 5ustar alfalf00000000000000glmark2-2012.08/./android/src/org/linaro/glmark2/0000775000175000017500000000000012013417376020537 5ustar alfalf00000000000000glmark2-2012.08/./android/src/org/linaro/glmark2/AboutActivity.java0000664000175000017500000000334212013417376024173 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ package org.linaro.glmark2; import android.app.Activity; import android.content.pm.PackageInfo; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class AboutActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_about); /* Get the application version */ String versionName = "?"; try { PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0); versionName = info.versionName; } catch (Exception e) { } /* Display the application version */ TextView tv = (TextView) findViewById(R.id.name_version); String formatString = getString(R.string.about_name_version_format); tv.setText(String.format(formatString, versionName)); } } glmark2-2012.08/./android/src/org/linaro/glmark2/Glmark2SurfaceView.java0000664000175000017500000001740612013417376025055 0ustar alfalf00000000000000package org.linaro.glmark2; import java.io.File; import android.graphics.PixelFormat; import android.opengl.GLSurfaceView; import android.app.Activity; import android.util.Log; import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; class Glmark2SurfaceView extends GLSurfaceView { public static final String LOG_TAG = "glmark2"; public Glmark2SurfaceView(Activity activity) { super(activity); mActivity = activity; setEGLContextClientVersion(2); setEGLConfigChooser(getConfigChooser()); setRenderer(new Glmark2Renderer(this)); } private EGLConfigChooser getConfigChooser() { String args = mActivity.getIntent().getStringExtra("args"); if (args == null) args = ""; String[] argv = args.split(" "); /* Find the visual-config option argument */ String configString = new String(); boolean keepNext = false; for (String arg : argv) { if (keepNext) { configString = arg; break; } if (arg.equals("--visual-config")) keepNext = true; } /* Parse the config string parameters */ String[] configParams = configString.split(":"); GLVisualConfig targetConfig = new GLVisualConfig(5, 6, 5, 0, 16, 1); for (String param : configParams) { String[] paramKeyValue = param.split("="); if (paramKeyValue.length < 2) continue; if (paramKeyValue[0].equals("red") || paramKeyValue[0].equals("r")) targetConfig.red = Integer.parseInt(paramKeyValue[1]); else if (paramKeyValue[0].equals("green") || paramKeyValue[0].equals("g")) targetConfig.green = Integer.parseInt(paramKeyValue[1]); else if (paramKeyValue[0].equals("blue") || paramKeyValue[0].equals("b")) targetConfig.blue = Integer.parseInt(paramKeyValue[1]); else if (paramKeyValue[0].equals("alpha") || paramKeyValue[0].equals("a")) targetConfig.alpha = Integer.parseInt(paramKeyValue[1]); else if (paramKeyValue[0].equals("depth") || paramKeyValue[0].equals("d")) targetConfig.depth = Integer.parseInt(paramKeyValue[1]); else if (paramKeyValue[0].equals("buffer") || paramKeyValue[0].equals("buf")) targetConfig.buffer = Integer.parseInt(paramKeyValue[1]); } return new Glmark2ConfigChooser(targetConfig); } /** * EGLConfigChooser that quits with an error dialog when a suitable config * cannot be found. */ private class Glmark2ConfigChooser implements EGLConfigChooser { private int[] mAttribList; public Glmark2ConfigChooser(GLVisualConfig targetConfig) { mAttribList = new int[] { EGL10.EGL_RED_SIZE, targetConfig.red, EGL10.EGL_GREEN_SIZE, targetConfig.green, EGL10.EGL_BLUE_SIZE, targetConfig.blue, EGL10.EGL_ALPHA_SIZE, targetConfig.alpha, EGL10.EGL_DEPTH_SIZE, targetConfig.depth, EGL10.EGL_BUFFER_SIZE, targetConfig.buffer, EGL10.EGL_RENDERABLE_TYPE, 4, /* 4 = EGL_OPENGL_ES2_BIT */ EGL10.EGL_NONE }; mTargetConfig = targetConfig; } @Override public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { try { return chooseConfigInternal(egl, display); } catch (Exception e) { /* Log an error message */ Log.e(LOG_TAG, "No suitable EGLConfig for GLES2.0 found. Please check that proper GLES2.0 drivers are installed."); /* Display an informative (and lethal for the app) dialog */ mActivity.runOnUiThread(new Runnable() { public void run() { mActivity.showDialog(Glmark2Activity.DIALOG_EGLCONFIG_FAIL_ID); } }); /* Wait here until the app process gets killed... */ synchronized (this) { try { this.wait(); } catch (Exception ex) { } } } return null; } private EGLConfig chooseConfigInternal(EGL10 egl, EGLDisplay display) { /* Get the number of available configs matching the attributes */ int[] num_config = new int[1]; if (!egl.eglChooseConfig(display, mAttribList, null, 0, num_config)) { throw new IllegalArgumentException("eglChooseConfig failed"); } int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No matching configs found"); } /* Get the matching configs */ EGLConfig[] configs = new EGLConfig[numConfigs]; if (!egl.eglChooseConfig(display, mAttribList, configs, numConfigs, num_config)) { throw new IllegalArgumentException("eglChooseConfig#2 failed"); } /* Find the best matching config. */ int bestScore = Integer.MIN_VALUE; EGLConfig bestConfig = configs[0]; for (EGLConfig config : configs) { GLVisualConfig vc = new GLVisualConfig(); vc.red = findConfigAttrib(egl, display, config, EGL10.EGL_RED_SIZE, 0); vc.green = findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0); vc.blue = findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0); vc.alpha = findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0); vc.depth = findConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0); vc.buffer = findConfigAttrib(egl, display, config, EGL10.EGL_BUFFER_SIZE, 0); int score = Glmark2Native.scoreConfig(vc, mTargetConfig); if (score > bestScore) { bestScore = score; bestConfig = config; } } return bestConfig; } private int findConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) { int[] value = new int[] { defaultValue }; egl.eglGetConfigAttrib(display, config, attribute, value); return value[0]; } protected GLVisualConfig mTargetConfig; } public Activity getActivity() { return mActivity; } private Activity mActivity; } class Glmark2Renderer implements GLSurfaceView.Renderer { public Glmark2Renderer(Glmark2SurfaceView view) { mView = view; } public void onDrawFrame(GL10 gl) { if (!Glmark2Native.render()) mView.getActivity().finish(); } public void onSurfaceChanged(GL10 gl, int width, int height) { Glmark2Native.resize(width, height); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { String args = mView.getActivity().getIntent().getStringExtra("args"); File f = new File(mView.getActivity().getFilesDir(), "last_run.log"); Glmark2Native.init(mView.getActivity().getAssets(), args, f.getAbsolutePath()); } private Glmark2SurfaceView mView; } glmark2-2012.08/./android/src/org/linaro/glmark2/SceneInfo.java0000664000175000017500000000552312013417376023260 0ustar alfalf00000000000000/* * Copyright © 2012 Linaro Limited * * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. * * glmark2 is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * glmark2. If not, see . * * Authors: * Alexandros Frantzis */ package org.linaro.glmark2; import android.os.Parcelable; import android.os.Parcel; import java.util.ArrayList; class SceneInfo implements Parcelable { static class Option { String name; String description; String defaultValue; String[] acceptableValues; } public SceneInfo(String name) { this.name = name; this.options = new ArrayList