├── AntTweakBar.dll ├── Framework.cpp ├── Framework.hpp ├── Framework.inl ├── README.txt ├── configure.sh ├── core ├── Affine.cpp ├── Algebra.hpp ├── Matrix2x2.cpp ├── Matrix3x3.cpp ├── Matrix4x4.cpp ├── Projection.cpp ├── Transform.hpp ├── Vector2.cpp ├── Vector3.cpp └── Vector4.cpp ├── docs ├── PreprocessTree.hpp ├── globalsShader.glsl ├── shader.glsl └── treeInfo3D.glsl ├── freeglut.dll ├── glew.hpp ├── glm.cpp ├── glm.hpp ├── include ├── AntTweakBar.h └── GL │ ├── freeglut.h │ ├── freeglut_ext.h │ ├── freeglut_std.h │ ├── glew.h │ ├── glxew.h │ └── wglew.h ├── lib ├── linux │ ├── lin32 │ │ ├── libAntTweakBar.so │ │ ├── libAntTweakBar.so.1 │ │ ├── libGLEW.so │ │ ├── libGLEW.so.1.7 │ │ ├── libGLEW.so.1.7.0 │ │ ├── libglut.so │ │ ├── libglut.so.3 │ │ └── libglut.so.3.9.0 │ └── lin64 │ │ ├── libAntTweakBar.so │ │ ├── libAntTweakBar.so.1 │ │ ├── libGLEW.so │ │ ├── libGLEW.so.1.9 │ │ ├── libGLEW.so.1.9.0 │ │ ├── libglut.so │ │ ├── libglut.so.3 │ │ └── libglut.so.3.9.0 └── windows │ └── win32 │ ├── AntTweakBar.lib │ ├── freeglut.lib │ └── glew32s.lib ├── libpng ├── png.c ├── png.h ├── pngconf.h ├── pngerror.c ├── pnggccrd.c ├── pngget.c ├── pngmem.c ├── pngpread.c ├── pngread.c ├── pngrio.c ├── pngrtran.c ├── pngrutil.c ├── pngset.c ├── pngtrans.c ├── pngvcrd.c ├── pngwio.c ├── pngwrite.c ├── pngwtran.c ├── pngwutil.c └── zlib │ ├── adler32.c │ ├── compress.c │ ├── crc32.c │ ├── crc32.h │ ├── deflate.c │ ├── deflate.h │ ├── inffast.c │ ├── inffast.h │ ├── inffixed.h │ ├── inflate.c │ ├── inflate.h │ ├── inftrees.c │ ├── inftrees.h │ ├── trees.c │ ├── trees.h │ ├── uncompr.c │ ├── zconf.h │ ├── zlib.h │ ├── zutil.c │ └── zutil.h ├── lightfield.glsl ├── main.cpp ├── mesh.glsl ├── models ├── Stone_F_3.mtl ├── Stone_F_3.obj ├── Stone_Forest_1.mtl └── Stone_Forest_1.obj ├── premake4.lua └── preview.glsl /AntTweakBar.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/AntTweakBar.dll -------------------------------------------------------------------------------- /Framework.hpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | // \author J Dupuy 3 | // \brief Utility functions and classes for simple OpenGL demos. 4 | // 5 | //////////////////////////////////////////////////////////////////////////////// 6 | 7 | #ifndef FRAMEWORK_HPP 8 | #define FRAMEWORK_HPP 9 | 10 | #include 11 | #include 12 | #include "glew.hpp" 13 | 14 | // offset for buffer objects 15 | #define FW_BUFFER_OFFSET(i) ((char*)NULL + (i)) 16 | 17 | namespace fw { 18 | // Framework exception 19 | class FWException : public std::exception { 20 | public: 21 | virtual ~FWException() throw() {} 22 | const char* what() const throw() {return mMessage.c_str();} 23 | protected: 24 | std::string mMessage; 25 | }; 26 | 27 | 28 | // Check if power of two 29 | bool is_power_of_two(GLuint number); 30 | // Get next power of two 31 | GLuint next_power_of_two(GLuint number); 32 | // Get exponent of next power of two 33 | GLuint next_power_of_two_exponent(GLuint number); 34 | 35 | 36 | // Build GLSL program 37 | GLvoid build_glsl_program(GLuint program, 38 | const std::string& srcfile, 39 | const std::string& options, 40 | GLboolean link) throw(FWException); 41 | 42 | 43 | // Check OpenGL errors 44 | // (throws an exception if an error is detected) 45 | GLvoid check_gl_error() throw(FWException); 46 | 47 | 48 | // Check framebuffer status 49 | // (throws an exception if the active framebuffer is not framebuffer complete) 50 | GLvoid check_framebuffer_status() throw(FWException); 51 | 52 | 53 | // Init debug output 54 | // (throws an exception if the extension is not supported) 55 | GLvoid init_debug_output(std::ostream& outputStream) throw(FWException); 56 | 57 | 58 | // Save a portion of the OpenGL front/back buffer (= take a screenshot). 59 | // File will be a TGA in BGR format, uncompressed. 60 | // The OpenGL state is restored the way it was before this function call. 61 | GLvoid save_gl_front_buffer(GLint x, 62 | GLint y, 63 | GLsizei width, 64 | GLsizei height) throw(FWException); 65 | GLvoid save_gl_back_buffer(GLint x, 66 | GLint y, 67 | GLsizei width, 68 | GLsizei height) throw(FWException); 69 | 70 | 71 | // Half to float conversion 72 | GLhalf float_to_half(GLfloat f); 73 | GLfloat half_to_float(GLhalf h); 74 | 75 | 76 | // Pack four normalized floats in an unsigned integer using 77 | // equation 2.1 from August 6, 2012 GL4.3 core profile specs. 78 | // The values must be in range [0.f,1.f] (checked in debug mode) 79 | // Memory layout: msb lsb 80 | // wwzz zzzz zzzz yyyy yyyy yyxx xxxx xxxx 81 | GLuint pack_4f_to_uint_2_10_10_10_rev(GLfloat x, 82 | GLfloat y, 83 | GLfloat z, 84 | GLfloat w); 85 | GLuint pack_4fv_to_uint_2_10_10_10_rev(const GLfloat *v); 86 | 87 | 88 | // Pack four normalized floats in a signed integer using 89 | // equation 2.2 from August 6, 2012 GL4.3 core profile specs. 90 | // The values must be in range [-1.f,1.f] (checked in debug mode) 91 | // Memory layout: msb lsb 92 | // wwzz zzzz zzzz yyyy yyyy yyxx xxxx xxxx 93 | GLint pack_4f_to_int_2_10_10_10_rev(GLfloat x, 94 | GLfloat y, 95 | GLfloat z, 96 | GLfloat w); 97 | GLint pack_4fv_to_int_2_10_10_10_rev(const GLfloat *v); 98 | 99 | 100 | // Convert RGB8 to packed types. 101 | GLubyte pack_3ub_to_ubyte_3_3_2(GLubyte r, 102 | GLubyte g, 103 | GLubyte b); 104 | GLushort pack_3ub_to_ushort_4_4_4(GLubyte r, 105 | GLubyte g, 106 | GLubyte b); 107 | GLushort pack_3ub_to_ushort_5_5_5(GLubyte r, 108 | GLubyte g, 109 | GLubyte b); 110 | GLushort pack_3ub_to_ushort_5_6_5(GLubyte r, 111 | GLubyte g, 112 | GLubyte b); 113 | GLubyte pack_3ubv_to_ubyte_3_3_2(const GLubyte *v); 114 | GLushort pack_3ubv_to_ushort_4_4_4(const GLubyte *v); 115 | GLushort pack_3ubv_to_ushort_5_5_5(const GLubyte *v); 116 | GLushort pack_3ubv_to_ushort_5_6_5(const GLubyte *v); 117 | 118 | 119 | // Convert RGBA8 to packed types. 120 | GLushort pack_4ub_to_ushort_4_4_4_4(GLubyte r, 121 | GLubyte g, 122 | GLubyte b, 123 | GLubyte a); 124 | GLushort pack_4ub_to_ushort_5_5_5_1(GLubyte r, 125 | GLubyte g, 126 | GLubyte b, 127 | GLubyte a); 128 | GLushort pack_4ubv_to_ushort_4_4_4_4(const GLubyte *v); 129 | GLushort pack_4ubv_to_ushort_5_5_5_1(const GLubyte *v); 130 | 131 | 132 | // Upload a TGA to a texture bound as GL_TEXTURE_2D 133 | void tex_tga_image2D(const std::string& filename, 134 | GLboolean genMipmaps, 135 | GLboolean immutable) throw(FWException); 136 | // Upload 6 TGAs to a texture bound as GL_TEXTURE_CUBE_MAP 137 | void tex_tga_cube_map(const std::string filenames[6], 138 | GLboolean genMipmaps, 139 | GLboolean immutable) throw(FWException); 140 | // Upload multiple TGAs to a texture bound as GL_TEXTURE_3D 141 | void tex_tga_sprites_image3D(const std::vector& filenames, 142 | GLboolean genMipmaps, 143 | GLboolean immutable) throw(FWException); 144 | 145 | #ifndef _NO_PNG // removes dependencies on libpng 146 | // Upload a PNG to a texture bound as GL_TEXTURE_2D (using libpng) 147 | void tex_png_image2D(const std::string& filename, 148 | GLboolean genMipmaps, 149 | GLboolean immutable) throw(FWException); 150 | // Upload 6 PNGs to a texture bound as GL_TEXTURE_CUBE_MAP (using libpng) 151 | void tex_png_cube_map(const std::string filenames[6], 152 | GLboolean genMipmaps, 153 | GLboolean immutable) throw(FWException); 154 | // Upload multiple PNGs to a texture bound as GL_TEXTURE_3D (using libpng) 155 | void tex_png_sprites_image3D(const std::vector& filenames, 156 | GLboolean genMipmaps, 157 | GLboolean immutable) throw(FWException); 158 | #endif // _NO_PNG 159 | 160 | 161 | // Build meshes centered at (0,0,0). 162 | // Three buffers must be bound: GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER 163 | // and GL_DRAW_INDIRECT_BUFFER. 164 | // The vertex array must be built by the user, who also provides the 165 | // Vertex type which must have the following constructor: 166 | // Vertex(GLfloat px, GLfloat py, GLfloat pz, 167 | // GLfloat nx, GLfloat ny, GLfloat nz, 168 | // GLfloat tx, GLfloat ty, GLfloat tz); 169 | // Index format is unsigned short, if the amount of indexes exceeds is 170 | // uint16 limit, an exception is thrown. 171 | template 172 | void buffer_cylinder_data(GLfloat base, 173 | GLfloat top, 174 | GLfloat height, 175 | GLint slices, 176 | GLint stacks) throw(FWException); 177 | 178 | template 179 | void buffer_sphere_data(GLfloat radius, 180 | GLint slices, 181 | GLint stacks) throw(FWException); 182 | 183 | template 184 | void buffer_disk_data(GLfloat inner, 185 | GLfloat outer, 186 | GLint slices, 187 | GLint loops) throw(FWException); 188 | 189 | template 190 | void buffer_cube_data(GLfloat width, 191 | GLfloat height, 192 | GLfloat depth) throw(FWException); 193 | 194 | 195 | // Render the frame using FSAA. Each pixel will be 196 | // generated from the last mip level of a sampleCnt x sampleCnt 197 | // texture. This is a heavy process which should be used 198 | // to generate ground truth images only. 199 | // - sampleCnt should be greater than 8 and preferably a power of two 200 | // - frustum gives the params of the projection (left, right, etc.) 201 | // - set_transform_func uploads the projection matrix to all the drawing 202 | // programs. 203 | // - draw_func must perform all (and only all) the draw calls to the 204 | // back buffer 205 | // Note: Requires an OpenGL4.3 GPU. 206 | void render_fsaa(GLsizei width, 207 | GLsizei height, 208 | GLsizei sampleCnt, 209 | GLfloat *frustum, // frustum data 210 | GLboolean perspective, // perspective of ortho matrix 211 | void (*set_transforms_func)(GLfloat *perspectiveMatrix), 212 | void (*draw_func)() ) throw(FWException); 213 | 214 | 215 | // Indirect drawing command : DrawArraysIndirectCommand 216 | typedef struct { 217 | GLuint count; 218 | GLuint primCount; 219 | GLuint first; 220 | GLuint baseInstance; 221 | } DrawArraysIndirectCommand; 222 | 223 | 224 | // Indirect drawing command : DrawElementsIndirectCommand 225 | typedef struct { 226 | GLuint count; 227 | GLuint primCount; 228 | GLuint firstIndex; 229 | GLint baseVertex; 230 | GLuint baseInstance; 231 | } DrawElementsIndirectCommand; 232 | 233 | 234 | // Basic timer class 235 | class Timer { 236 | public: 237 | // Constructors / Destructor 238 | Timer(); 239 | 240 | // Manipulation 241 | void Start(); 242 | void Stop() ; 243 | 244 | // Queries 245 | double Ticks() const; 246 | 247 | // Members 248 | private: 249 | double mStartTicks; 250 | double mStopTicks; 251 | bool mIsTicking; 252 | }; 253 | 254 | 255 | // Tga image loader 256 | class Tga { 257 | public: 258 | // Constants 259 | enum { 260 | PIXEL_FORMAT_UNKNOWN=0, 261 | PIXEL_FORMAT_LUMINANCE, 262 | PIXEL_FORMAT_LUMINANCE_ALPHA, 263 | PIXEL_FORMAT_BGR, 264 | PIXEL_FORMAT_BGRA 265 | }; 266 | 267 | // Constructors / Destructor 268 | Tga(); 269 | // see Load 270 | explicit Tga(const std::string& filename) throw(FWException); 271 | ~Tga(); 272 | 273 | // Manipulation 274 | // load from a tga file 275 | void Load(const std::string& filename) throw(FWException); 276 | 277 | // Queries 278 | GLushort Width() const; 279 | GLushort Height() const; 280 | GLint PixelFormat() const; 281 | GLint BitsPerPixel() const; 282 | GLubyte* Pixels() const; // data must be used for read only 283 | 284 | private: 285 | // Non copyable 286 | Tga(const Tga& tga); 287 | Tga& operator=(const Tga& tga); 288 | 289 | // Internal manipulation 290 | GLushort _UnpackUint16(GLubyte msb, GLubyte lsb); 291 | void _Flip(); 292 | void _LoadColourMapped(std::ifstream&, GLchar*) throw(FWException); 293 | void _LoadLuminance(std::ifstream&, GLchar*) throw(FWException); 294 | void _LoadUnmapped(std::ifstream&, GLchar*) throw(FWException); 295 | void _LoadUnmappedRle(std::ifstream&, GLchar*) throw(FWException); 296 | void _LoadColourMappedRle(std::ifstream&, GLchar*) throw(FWException); 297 | void _LoadLuminanceRle(std::ifstream&, GLchar*) throw(FWException); 298 | void _Clear(); 299 | 300 | // Members 301 | GLubyte* mPixels; 302 | GLint mPixelFormat; 303 | GLushort mWidth; 304 | GLushort mHeight; 305 | }; 306 | 307 | #ifndef _NO_PNG // removes dependencies on libpng 308 | class Png { 309 | public: 310 | // Constants 311 | enum { 312 | PIXEL_FORMAT_UNKNOWN=0, 313 | PIXEL_FORMAT_LUMINANCE, 314 | PIXEL_FORMAT_LUMINANCE_ALPHA, 315 | PIXEL_FORMAT_RGB, 316 | PIXEL_FORMAT_RGBA 317 | }; 318 | 319 | // Constructors / Destructor 320 | Png(); 321 | // see Load 322 | explicit Png(const std::string& filename) throw(FWException); 323 | ~Png(); 324 | 325 | // Manipulation 326 | // load from a tga file 327 | void Load(const std::string& filename) throw(FWException); 328 | 329 | // Queries 330 | GLushort Width() const; 331 | GLushort Height() const; 332 | GLint PixelFormat() const; 333 | GLint BitsPerPixel() const; 334 | GLubyte* Pixels() const; // data must be used for read only 335 | 336 | private: 337 | // Non copyable 338 | Png(const Png& png); 339 | Png& operator=(const Png& png); 340 | 341 | // Internal manipulation 342 | void _Clear(); 343 | 344 | // Members 345 | GLubyte* mPixels; 346 | GLint mPixelFormat; 347 | GLushort mWidth; 348 | GLushort mHeight; 349 | GLubyte mBitsPerPixel; 350 | }; 351 | #endif 352 | 353 | 354 | } // namespace fw 355 | 356 | 357 | #include "Framework.inl" // mesh impl 358 | 359 | #endif 360 | 361 | -------------------------------------------------------------------------------- /Framework.inl: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | // \author J Dupuy 3 | // \brief Utility functions and classes for simple OpenGL demos. 4 | // 5 | //////////////////////////////////////////////////////////////////////////////// 6 | 7 | namespace fw { 8 | namespace impl { 9 | class _NoArrayBufferBoundException : public FWException { 10 | public: 11 | _NoArrayBufferBoundException() { 12 | mMessage = "No buffer bound to target GL_ARRAY_BUFFER."; 13 | } 14 | }; 15 | 16 | class _NoElementArrayBufferBoundException : public FWException { 17 | public: 18 | _NoElementArrayBufferBoundException() { 19 | mMessage = "No buffer bound to target GL_ELEMENT_ARRAY_BUFFER."; 20 | } 21 | }; 22 | 23 | class _NoDrawIndirectBufferBoundException : public FWException { 24 | public: 25 | _NoDrawIndirectBufferBoundException() { 26 | mMessage = "No buffer bound to target GL_DRAW_INDIRECT_BUFFER."; 27 | } 28 | }; 29 | 30 | // check bound buffers for mesh generation 31 | inline void check_bound_buffers() throw(FWException) { 32 | GLint arrayBuffer = 0; 33 | GLint elementArrayBuffer = 0; 34 | GLint drawIndirectBuffer = 0; 35 | glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuffer); 36 | glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &elementArrayBuffer); 37 | glGetIntegerv(GL_DRAW_INDIRECT_BUFFER_BINDING, &drawIndirectBuffer); 38 | if(arrayBuffer==0) 39 | throw _NoArrayBufferBoundException(); 40 | if(elementArrayBuffer==0) 41 | throw _NoElementArrayBufferBoundException(); 42 | if(drawIndirectBuffer==0) 43 | throw _NoDrawIndirectBufferBoundException(); 44 | } 45 | } 46 | 47 | template 48 | inline 49 | void buffer_cube_data(GLfloat width, 50 | GLfloat height, 51 | GLfloat depth) throw(FWException) { 52 | impl::check_bound_buffers(); 53 | const GLint vertexCnt = 24; 54 | const DrawElementsIndirectCommand command = {24u,0u,0u,0u,0u}; 55 | const GLushort indexes[] = {0u,1u,5u,7u,5u,2u,8u,9u}; 56 | width*= 0.5f; 57 | height*= 0.5f; 58 | depth*= 0.5f; 59 | Vertex *vertices = new Vertex[vertexCnt]; 60 | for(GLint i=0; i<24; ++i) 61 | vertices[i] = Vertex(width, height, depth, 62 | i%6,i%6,i%6, 63 | i%4,i%4,i%6); 64 | 65 | // Upload data 66 | glBufferData(GL_ARRAY_BUFFER, 67 | sizeof(Vertex)*vertexCnt, 68 | vertices, 69 | GL_STATIC_DRAW); 70 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, 71 | sizeof(indexes), 72 | indexes, 73 | GL_STATIC_DRAW); 74 | glBufferData(GL_DRAW_INDIRECT_BUFFER, 75 | sizeof(DrawElementsIndirectCommand), 76 | &command.count, 77 | GL_STATIC_DRAW); 78 | 79 | // cleanup 80 | delete[] vertices; 81 | } 82 | 83 | } 84 | 85 | 86 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | Building the sources 2 | -------------------- 3 | 4 | 1 On Windows 5 | Just open the solution with vs2010, and press CTRL+F5. 6 | 7 | 2 On Linux 8 | In a terminal, type "sh configure.sh", then either: 9 | - "make config=release32" or 10 | - "make config=release64" 11 | depending on your system. 12 | You can get more options for build by typing "make help" 13 | 14 | Enjoy ! 15 | 16 | -------------------------------------------------------------------------------- /configure.sh: -------------------------------------------------------------------------------- 1 | echo "Building symbolic links..." 2 | 3 | ln -sf ./libGLEW.so.1.7.0 ./lib/linux/lin32/libGLEW.so.1.7 4 | ln -sf ./libGLEW.so.1.7.0 ./lib/linux/lin32/libGLEW.so 5 | ln -sf ./libglut.so.3.9.0 ./lib/linux/lin32/libglut.so 6 | ln -sf ./libglut.so.3.9.0 ./lib/linux/lin32/libglut.so.3 7 | ln -sf ./libAntTweakBar.so ./lib/linux/lin32/libAntTweakBar.so.1 8 | 9 | ln -sf ./libGLEW.so.1.9.0 ./lib/linux/lin64/libGLEW.so.1.9 10 | ln -sf ./libGLEW.so.1.9.0 ./lib/linux/lin64/libGLEW.so 11 | ln -sf ./libglut.so.3.9.0 ./lib/linux/lin64/libglut.so 12 | ln -sf ./libglut.so.3.9.0 ./lib/linux/lin64/libglut.so.3 13 | ln -sf ./libAntTweakBar.so ./lib/linux/lin64/libAntTweakBar.so.1 14 | 15 | echo "done!" 16 | 17 | -------------------------------------------------------------------------------- /core/Affine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Transform.hpp" 4 | 5 | //////////////////////////////////////////////////////////////////////////////// 6 | // static data 7 | const Affine Affine::IDENTITY(Matrix3x3::Diagonal(1,1,1), 8 | Vector3(0,0,0), 9 | 1.0f); 10 | 11 | 12 | 13 | //////////////////////////////////////////////////////////////////////////////// 14 | // Factories 15 | Affine Affine::Translation(const Vector3& translation) 16 | { 17 | return Affine(Matrix3x3::Diagonal(1,1,1), 18 | translation, 19 | 1.0f); 20 | } 21 | 22 | 23 | //////////////////////////////////////////////////////////////////////////////// 24 | // Constructor 25 | Affine::Affine(): 26 | mUnitAxis(Matrix3x3::Diagonal(1,1,1)), 27 | mPosition(Vector3(0,0,0)), 28 | mScale(1.f), 29 | mIsRS(true) 30 | { 31 | } 32 | 33 | 34 | //////////////////////////////////////////////////////////////////////////////// 35 | // Explicit constructor 36 | Affine::Affine(const Matrix3x3& unitAxis, 37 | const Vector3& position, 38 | float scale) : 39 | mUnitAxis(unitAxis), 40 | mPosition(position), 41 | mScale(scale), 42 | mIsRS(position == Vector3::ZERO) 43 | { 44 | // if(position != Vector3::ZERO) 45 | // mIsRS = false; 46 | } 47 | 48 | 49 | //////////////////////////////////////////////////////////////////////////////// 50 | // Comparison operators 51 | bool Affine::operator==(const Affine& affine) const 52 | { return !((*this) != affine); } 53 | 54 | bool Affine::operator!=(const Affine& affine) const 55 | { 56 | return ( mUnitAxis != affine.mUnitAxis 57 | || mPosition != affine.mPosition 58 | || mScale != affine.mScale); 59 | } 60 | 61 | 62 | //////////////////////////////////////////////////////////////////////////////// 63 | // Translate 64 | void Affine::TranslateWorld(const Vector3& direction) 65 | { 66 | // std::cout << mPosition[0] << ' ' 67 | // << mPosition[1] << ' ' 68 | // << mPosition[2] << '\n'; 69 | mPosition+= direction; 70 | mIsRS = mPosition == Vector3::ZERO; 71 | } 72 | 73 | void Affine::TranslateLocal(const Vector3& direction) 74 | { 75 | TranslateWorld(mUnitAxis * direction); 76 | } 77 | 78 | 79 | //////////////////////////////////////////////////////////////////////////////// 80 | // RotateWorld 81 | void Affine::RotateAboutWorldX(float radians) 82 | { mUnitAxis = Matrix3x3::RotationAboutX(radians) * mUnitAxis; normalizeAxis(); } 83 | 84 | void Affine::RotateAboutWorldY(float radians) 85 | { mUnitAxis = Matrix3x3::RotationAboutY(radians) * mUnitAxis; normalizeAxis(); } 86 | 87 | void Affine::RotateAboutWorldZ(float radians) 88 | { mUnitAxis = Matrix3x3::RotationAboutZ(radians) * mUnitAxis; normalizeAxis(); } 89 | 90 | 91 | //////////////////////////////////////////////////////////////////////////////// 92 | // RotateLocal 93 | void Affine::RotateAboutLocalX(float radians) 94 | { mUnitAxis *= Matrix3x3::RotationAboutX(radians); 95 | normalizeAxis(); } 96 | 97 | void Affine::RotateAboutLocalY(float radians) 98 | { mUnitAxis *= Matrix3x3::RotationAboutY(radians); 99 | normalizeAxis(); } 100 | 101 | void Affine::RotateAboutLocalZ(float radians) 102 | { mUnitAxis *= Matrix3x3::RotationAboutZ(radians); 103 | normalizeAxis(); } 104 | 105 | 106 | //////////////////////////////////////////////////////////////////////////////// 107 | // Look at 108 | void Affine::LookAt(const Vector3& targetPos, 109 | const Vector3& unitUp) 110 | { 111 | mUnitAxis = Matrix3x3::LookAtRotation(mPosition, targetPos, unitUp); 112 | } 113 | 114 | 115 | //////////////////////////////////////////////////////////////////////////////// 116 | // Reset 117 | void Affine::MakeDefaultAxis() 118 | { 119 | mUnitAxis = Matrix3x3::Diagonal(1,1,1); 120 | } 121 | 122 | void Affine::MakeZeroPosition() 123 | { 124 | mIsRS = true; 125 | mPosition = Vector3(0.0f, 0.0f, 0.0f); 126 | } 127 | 128 | void Affine::MakeUnitScale() 129 | { 130 | mScale = 1.0f; 131 | } 132 | 133 | 134 | //////////////////////////////////////////////////////////////////////////////// 135 | // Matrix extraction 136 | Matrix4x4 Affine::ExtractTransformMatrix() const 137 | { 138 | return Matrix4x4(mUnitAxis[0][0]*mScale, 139 | mUnitAxis[1][0]*mScale, 140 | mUnitAxis[2][0]*mScale, 141 | mPosition[0], 142 | mUnitAxis[0][1]*mScale, 143 | mUnitAxis[1][1]*mScale, 144 | mUnitAxis[2][1]*mScale, 145 | mPosition[1], 146 | mUnitAxis[0][2]*mScale, 147 | mUnitAxis[1][2]*mScale, 148 | mUnitAxis[2][2]*mScale, 149 | mPosition[2], 150 | 0.0f, 0.0f, 0.0f, 1.0f); 151 | } 152 | 153 | Matrix4x4 Affine::ExtractInverseTransformMatrix() const 154 | { 155 | if(mIsRS) 156 | { 157 | // return transpose only 158 | float invScale = 1.0f / mScale; 159 | return Matrix4x4(mUnitAxis[0][0]*invScale, 160 | mUnitAxis[0][1], 161 | mUnitAxis[0][2], 162 | 0.0f, 163 | mUnitAxis[1][0], 164 | mUnitAxis[1][1]*invScale, 165 | mUnitAxis[1][2], 166 | 0.0f, 167 | mUnitAxis[2][0], 168 | mUnitAxis[2][1], 169 | mUnitAxis[2][2]*invScale, 170 | 0.0f, 171 | 0.0f , 0.0f, 0.0f, 1.0f); 172 | } 173 | else 174 | { 175 | // compute full inverse 176 | return ExtractTransformMatrix().Inverse(); 177 | } 178 | } 179 | 180 | 181 | //////////////////////////////////////////////////////////////////////////////// 182 | // Axis queries 183 | const Vector3& Affine::UnitXAxis() const { return mUnitAxis[0]; } 184 | const Vector3& Affine::UnitYAxis() const { return mUnitAxis[1]; } 185 | const Vector3& Affine::UnitZAxis() const { return mUnitAxis[2]; } 186 | 187 | 188 | //////////////////////////////////////////////////////////////////////////////// 189 | // Accessors 190 | const Matrix3x3& Affine::GetUnitAxis() const { return mUnitAxis; } 191 | const Vector3& Affine::GetPosition() const { return mPosition; } 192 | float Affine::GetScale() const { return mScale; } 193 | 194 | 195 | //////////////////////////////////////////////////////////////////////////////// 196 | // Mutators 197 | void Affine::SetPosition(const Vector3& position) 198 | { 199 | mPosition = position; 200 | mIsRS = mPosition == Vector3::ZERO; 201 | } 202 | 203 | void Affine::SetScale(float nonZeroScale) 204 | { 205 | #ifndef NDEBUG 206 | assert(nonZeroScale != 0.0f); 207 | #endif 208 | mScale = nonZeroScale; 209 | } 210 | 211 | 212 | //////////////////////////////////////////////////////////////////////////////// 213 | // Normalize Axis 214 | void Affine::normalizeAxis() 215 | { 216 | mUnitAxis[0] = mUnitAxis[0].Normalize(); 217 | mUnitAxis[1] = mUnitAxis[1].Normalize(); 218 | mUnitAxis[2] = mUnitAxis[2].Normalize(); 219 | } 220 | 221 | 222 | -------------------------------------------------------------------------------- /core/Matrix2x2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Algebra.hpp" 5 | 6 | //////////////////////////////////////////////////////////////////////////////// 7 | // Constants 8 | const Matrix2x2 Matrix2x2::IDENTITY(1,0, 9 | 0,1); 10 | 11 | 12 | //////////////////////////////////////////////////////////////////////////////// 13 | // Factories 14 | Matrix2x2 Matrix2x2::CompMult(const Matrix2x2& m1, 15 | const Matrix2x2& m2) 16 | { 17 | return Matrix2x2(Vector2::CompMult(m1[0], m2[0]), 18 | Vector2::CompMult(m1[1], m2[1])); 19 | } 20 | 21 | Matrix2x2 Matrix2x2::CompDiv(const Matrix2x2& m1, 22 | const Matrix2x2& m2) 23 | { 24 | return Matrix2x2(Vector2::CompDiv(m1[0], m2[0]), 25 | Vector2::CompDiv(m1[1], m2[1])); 26 | } 27 | 28 | Matrix2x2 Matrix2x2::CompPow(const Matrix2x2& base, 29 | const Matrix2x2& exponent) 30 | { 31 | return Matrix2x2(Vector2::CompPow(base[0], exponent[0]), 32 | Vector2::CompPow(base[1], exponent[1])); 33 | } 34 | 35 | Matrix2x2 Matrix2x2::CompMin(const Matrix2x2& m1, 36 | const Matrix2x2& m2) 37 | { 38 | return Matrix2x2(Vector2::CompMin(m1[0], m2[0]), 39 | Vector2::CompMin(m1[1], m2[1])); 40 | } 41 | 42 | Matrix2x2 Matrix2x2::CompMax(const Matrix2x2& m1, 43 | const Matrix2x2& m2) 44 | { 45 | return Matrix2x2(Vector2::CompMax(m1[0], m2[0]), 46 | Vector2::CompMax(m1[1], m2[1])); 47 | } 48 | 49 | Matrix2x2 Matrix2x2::CompClamp(const Matrix2x2& m, 50 | const Matrix2x2& min, 51 | const Matrix2x2& max) 52 | { 53 | return Matrix2x2(Vector2::CompClamp(m[0], min[0], max[0]), 54 | Vector2::CompClamp(m[1], min[1], max[1])); 55 | } 56 | 57 | 58 | //////////////////////////////////////////////////////////////////////////////// 59 | // Factories (continued) 60 | Matrix2x2 Matrix2x2::OuterProduct(const Vector2& c, 61 | const Vector2& r) 62 | { 63 | return Matrix2x2(r[0]*c, r[1]*c); 64 | } 65 | 66 | Matrix2x2 Matrix2x2::Diagonal(const float& m00, const float& m11) 67 | { 68 | return Matrix2x2(m00, 0, 69 | 0 , m11); 70 | } 71 | 72 | Matrix2x2 Matrix2x2::Rotation(const float& radians) 73 | { 74 | // do some precomputations 75 | float c = std::cos(radians); 76 | float s = std::sin(radians); 77 | return Matrix2x2( c, -s, 78 | s, c); 79 | } 80 | 81 | Matrix2x2 Matrix2x2::Scale(const float& x, 82 | const float& y) 83 | { 84 | return Diagonal(x, y); 85 | } 86 | 87 | 88 | //////////////////////////////////////////////////////////////////////////////// 89 | // Column Constructor 90 | Matrix2x2::Matrix2x2(const Vector2& c0, 91 | const Vector2& c1): 92 | mC0(c0), mC1(c1) 93 | { 94 | 95 | } 96 | 97 | 98 | //////////////////////////////////////////////////////////////////////////////// 99 | // Scalar Constructor 100 | Matrix2x2::Matrix2x2(const float& m00, const float& m10, 101 | const float& m01, const float& m11): 102 | mC0(Vector2(m00,m01)), 103 | mC1(Vector2(m10,m11)) 104 | { 105 | 106 | } 107 | 108 | 109 | //////////////////////////////////////////////////////////////////////////////// 110 | // Access operators 111 | const Vector2& Matrix2x2::operator[](size_t column) const 112 | { 113 | #ifndef NDEBUG 114 | assert(column < 2); 115 | #endif 116 | return (&mC0)[column]; 117 | } 118 | 119 | Vector2& Matrix2x2::operator[](size_t column) 120 | { 121 | return const_cast< Vector2& > 122 | ((static_cast< const Matrix2x2& >(*this))[column]); 123 | } 124 | 125 | 126 | //////////////////////////////////////////////////////////////////////////////// 127 | // Arithmetic operators 128 | Matrix2x2 Matrix2x2::operator+(const Matrix2x2& m) const 129 | { return Matrix2x2(mC0+m.mC0, mC1+m.mC1); } 130 | 131 | Matrix2x2 Matrix2x2::operator-(const Matrix2x2& m) const 132 | { return Matrix2x2(mC0-m.mC0, mC1-m.mC1); } 133 | 134 | Matrix2x2 Matrix2x2::operator+() const 135 | { return (*this); } 136 | 137 | Matrix2x2 Matrix2x2::operator-() const 138 | { return Matrix2x2(-mC0, -mC1); } 139 | 140 | Matrix2x2 Matrix2x2::operator*(const Matrix2x2& m) const 141 | { 142 | return Matrix2x2( (*this)[0][0]*m[0][0] 143 | + (*this)[1][0]*m[0][1], 144 | (*this)[0][0]*m[1][0] 145 | + (*this)[1][0]*m[1][1], 146 | (*this)[0][1]*m[0][0] 147 | + (*this)[1][1]*m[0][1], 148 | (*this)[0][1]*m[1][0] 149 | + (*this)[1][1]*m[1][1]); 150 | } 151 | 152 | Vector2 Matrix2x2::operator*(const Vector2& v) const 153 | { 154 | return Vector2((*this)[0][0]*v[0]+(*this)[1][0]*v[1], 155 | (*this)[0][1]*v[0]+(*this)[1][1]*v[1]); 156 | } 157 | 158 | 159 | //////////////////////////////////////////////////////////////////////////////// 160 | // Assignment operators 161 | Matrix2x2& Matrix2x2::operator+=(const Matrix2x2& m) 162 | { 163 | mC0 += m.mC0; 164 | mC1 += m.mC1; 165 | return (*this); 166 | } 167 | 168 | Matrix2x2& Matrix2x2::operator-=(const Matrix2x2& m) 169 | { 170 | mC0 -= m.mC0; 171 | mC1 -= m.mC1; 172 | return (*this); 173 | } 174 | 175 | Matrix2x2& Matrix2x2::operator*=(const Matrix2x2& m) 176 | { 177 | return (*this) = (*this) * m; 178 | } 179 | 180 | 181 | //////////////////////////////////////////////////////////////////////////////// 182 | // Comparison operators 183 | bool Matrix2x2::operator==(const Matrix2x2& m) const 184 | { return !((*this) != m); } 185 | 186 | bool Matrix2x2::operator!=(const Matrix2x2& m) const 187 | { return (mC0 != m.mC0 || mC1 != m.mC1); } 188 | 189 | 190 | //////////////////////////////////////////////////////////////////////////////// 191 | // Is Invertible ? 192 | bool Matrix2x2::IsInvertible() const 193 | { 194 | return (Determinant() != 0.0f); 195 | } 196 | 197 | 198 | //////////////////////////////////////////////////////////////////////////////// 199 | // Determinant 200 | float Matrix2x2::Determinant() const 201 | { 202 | return ( (*this)[0][0]*(*this)[1][1] - (*this)[1][0]*(*this)[0][1] ); 203 | } 204 | 205 | 206 | //////////////////////////////////////////////////////////////////////////////// 207 | // Inverse 208 | Matrix2x2 Matrix2x2::Inverse() const 209 | { 210 | #ifndef NDEBUG 211 | assert(IsInvertible()); 212 | #endif 213 | return 1.0f/Determinant() * Adjugate(); 214 | } 215 | 216 | 217 | //////////////////////////////////////////////////////////////////////////////// 218 | // Transpose 219 | Matrix2x2 Matrix2x2::Transpose() const 220 | { 221 | return Matrix2x2( (*this)[0][0], (*this)[0][1], 222 | (*this)[1][0], (*this)[1][1] ); 223 | } 224 | 225 | 226 | //////////////////////////////////////////////////////////////////////////////// 227 | // Adjugate 228 | Matrix2x2 Matrix2x2::Adjugate() const 229 | { 230 | // return transpose of cofactors 231 | return Matrix2x2( (*this)[1][1], -(*this)[0][1], 232 | -(*this)[1][0], (*this)[0][0]); 233 | } 234 | 235 | 236 | //////////////////////////////////////////////////////////////////////////////// 237 | // Queries 238 | Matrix2x2 Matrix2x2::Sign() const 239 | { return Matrix2x2(mC0.Sign(), 240 | mC1.Sign()); } 241 | 242 | Matrix2x2 Matrix2x2::Abs() const 243 | { return Matrix2x2(mC0.Abs(), 244 | mC1.Abs()); } 245 | 246 | Matrix2x2 Matrix2x2::Sqr() const 247 | { return Matrix2x2(mC0.Sqr(), 248 | mC1.Sqr()); } 249 | 250 | Matrix2x2 Matrix2x2::Sqrt() const 251 | { return Matrix2x2(mC0.Sqrt(), 252 | mC1.Sqrt()); } 253 | 254 | Matrix2x2 Matrix2x2::Exp() const 255 | { return Matrix2x2(mC0.Exp(), 256 | mC1.Exp()); } 257 | 258 | Matrix2x2 Matrix2x2::Log() const 259 | { return Matrix2x2(mC0.Log(), 260 | mC1.Log()); } 261 | 262 | Matrix2x2 Matrix2x2::Log10() const 263 | { return Matrix2x2(mC0.Log10(), 264 | mC1.Log10()); } 265 | 266 | Matrix2x2 Matrix2x2::Ceil() const 267 | { return Matrix2x2(mC0.Ceil(), 268 | mC1.Ceil()); } 269 | 270 | Matrix2x2 Matrix2x2::Floor() const 271 | { return Matrix2x2(mC0.Floor(), 272 | mC1.Floor()); } 273 | 274 | Matrix2x2 Matrix2x2::Frac() const 275 | { return Matrix2x2(mC0.Frac(), 276 | mC1.Frac()); } 277 | 278 | 279 | //////////////////////////////////////////////////////////////////////////////// 280 | // Additionnal operators 281 | Matrix2x2 operator*(const float& s, const Matrix2x2& m) 282 | { 283 | return Matrix2x2(s*m[0], s*m[1]); 284 | } 285 | 286 | 287 | #if 0 288 | 289 | int main(int argc, char **argv) 290 | { 291 | Matrix2x2 m = Matrix2x2::IDENTITY; 292 | 293 | } 294 | #endif 295 | 296 | -------------------------------------------------------------------------------- /core/Projection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Transform.hpp" 5 | 6 | 7 | //////////////////////////////////////////////////////////////////////////////// 8 | // Perspective factory 9 | Projection Projection::Perspective(float fovy, 10 | float aspect, 11 | float near, 12 | float far) 13 | { 14 | #ifndef NDEBUG 15 | assert( fovy > 0.0f 16 | && aspect > 0.0f 17 | && near < far 18 | && near > 0.0f); 19 | #endif 20 | 21 | // convert to frustum 22 | float halfHeight = std::tan(fovy*0.5f) * near; 23 | float halfWidth = halfHeight * aspect; 24 | 25 | return Frustum(-halfWidth, 26 | halfWidth, 27 | -halfHeight, 28 | halfHeight, 29 | near, 30 | far); 31 | } 32 | 33 | 34 | //////////////////////////////////////////////////////////////////////////////// 35 | // Frustum factory 36 | Projection Projection::Frustum(float left, 37 | float right, 38 | float bottom, 39 | float top, 40 | float near, 41 | float far) 42 | { 43 | #ifndef NDEBUG 44 | assert( left != right 45 | && bottom != top 46 | && near != far 47 | && near > 0.0f 48 | && far > 0.0f); 49 | #endif 50 | 51 | return Projection(left, 52 | right, 53 | bottom, 54 | top, 55 | near, 56 | far, 57 | PROJECTION_TYPE_PERSPECTIVE); 58 | } 59 | 60 | 61 | //////////////////////////////////////////////////////////////////////////////// 62 | // Ortho factory 63 | Projection Projection::Orthographic(float left, 64 | float right, 65 | float bottom, 66 | float top, 67 | float near, 68 | float far) 69 | { 70 | #ifndef NDEBUG 71 | assert( left != right 72 | && bottom != top 73 | && near != far); 74 | #endif 75 | return Projection(left, 76 | right, 77 | bottom, 78 | top, 79 | near, 80 | far, 81 | PROJECTION_TYPE_ORTHOGRAPHIC); 82 | } 83 | 84 | 85 | //////////////////////////////////////////////////////////////////////////////// 86 | // Explicit constructor 87 | Projection::Projection(float left, 88 | float right, 89 | float bottom, 90 | float top, 91 | float near, 92 | float far, 93 | Projection::ProjectionType type): 94 | mLeft(left), 95 | mRight(right), 96 | mBottom(bottom), 97 | mTop(top), 98 | mNear(near), 99 | mFar(far), 100 | mType(type) 101 | { 102 | } 103 | 104 | 105 | //////////////////////////////////////////////////////////////////////////////// 106 | // Extract Matrix 107 | Matrix4x4 Projection::ExtractTransformMatrix() const 108 | { 109 | if(mType == PROJECTION_TYPE_PERSPECTIVE) 110 | return Matrix4x4::Frustum(mLeft, 111 | mRight, 112 | mBottom, 113 | mTop, 114 | mNear, 115 | mFar); 116 | return Matrix4x4::Ortho(mLeft, 117 | mRight, 118 | mBottom, 119 | mTop, 120 | mNear, 121 | mFar); 122 | } 123 | 124 | Matrix4x4 Projection::ExtractInverseTransformMatrix() const 125 | { 126 | return ExtractTransformMatrix().Inverse(); 127 | } 128 | 129 | 130 | //////////////////////////////////////////////////////////////////////////////// 131 | // Queries 132 | float Projection::Width() const 133 | { return std::abs(mRight - mLeft); } 134 | 135 | float Projection::Height() const 136 | { return std::abs(mTop - mBottom); } 137 | 138 | float Projection::Depth() const 139 | { return std::abs(mFar - mNear); } 140 | 141 | float Projection::Aspect() const 142 | { return Width() / Height(); } 143 | 144 | bool Projection::IsPerspective() const 145 | { return (mType == PROJECTION_TYPE_PERSPECTIVE); } 146 | 147 | bool Projection::IsOrthographic() const 148 | { return (mType == PROJECTION_TYPE_ORTHOGRAPHIC); } 149 | 150 | 151 | //////////////////////////////////////////////////////////////////////////////// 152 | // Fit to aspect 153 | void Projection::FitHeightToAspect(float aspect) 154 | { 155 | // compute factor 156 | float factor = Aspect()/aspect; 157 | 158 | mBottom *= factor; 159 | mTop *= factor; 160 | } 161 | 162 | void Projection::FitWidthToAspect(float aspect) 163 | { 164 | // get current aspect 165 | float factor = aspect/Aspect(); 166 | 167 | mLeft *= factor; 168 | mRight *= factor; 169 | } 170 | 171 | 172 | //////////////////////////////////////////////////////////////////////////////// 173 | // Accessors 174 | const float& Projection::GetLeft() const 175 | {return mLeft;} 176 | const float& Projection::GetRight() const 177 | {return mRight;} 178 | const float& Projection::GetBottom() const 179 | {return mBottom;} 180 | const float& Projection::GetTop() const 181 | {return mTop;} 182 | const float& Projection::GetNear() const 183 | {return mNear;} 184 | const float& Projection::GetFar() const 185 | {return mFar;} 186 | Projection::ProjectionType Projection::GetType() const 187 | {return mType;} 188 | 189 | 190 | //////////////////////////////////////////////////////////////////////////////// 191 | // Mutators 192 | void Projection::SetLeft(float left) 193 | { 194 | #ifndef NDEBUG 195 | assert(left != mRight); 196 | #endif 197 | mLeft = left; 198 | } 199 | 200 | void Projection::SetRight(float right) 201 | { 202 | #ifndef NDEBUG 203 | assert(right != mLeft); 204 | #endif 205 | mRight = right; 206 | } 207 | 208 | void Projection::SetBottom(float bottom) 209 | { 210 | #ifndef NDEBUG 211 | assert(bottom != mTop); 212 | #endif 213 | mBottom = bottom; 214 | } 215 | 216 | void Projection::SetTop(float top) 217 | { 218 | #ifndef NDEBUG 219 | assert(mBottom != top); 220 | #endif 221 | mTop = top; 222 | } 223 | 224 | void Projection::SetNear(float near) 225 | { 226 | #ifndef NDEBUG 227 | assert(near != mFar); 228 | if(mType == PROJECTION_TYPE_PERSPECTIVE) 229 | assert(near > 0.0f); 230 | #endif 231 | mNear = near; 232 | } 233 | 234 | void Projection::SetFar(float far) 235 | { 236 | #ifndef NDEBUG 237 | assert(far != mNear); 238 | if(mType == PROJECTION_TYPE_PERSPECTIVE) 239 | assert(far > 0.0f); 240 | #endif 241 | mFar = far; 242 | } 243 | 244 | void Projection::SetType(Projection::ProjectionType type) 245 | { 246 | mType = type; 247 | } 248 | 249 | 250 | 251 | 252 | 253 | -------------------------------------------------------------------------------- /core/Transform.hpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | // \file Transform.hpp 3 | // \author J Dupuy 4 | // \brief Provides classes to build transformations easily. Every class has at 5 | // least one matrix extraction method. 6 | // List of classes 7 | // - Affine: allows to build affine transformations in a right handed 8 | // cartesian coordinate system. 9 | // - Projection: allows to build projections. 10 | // - PinholeCamera: builds both an affine and a pin hole projection. 11 | // The pin hole projection is locked to the aspect of the camera's 12 | // resolution. 13 | // 14 | //////////////////////////////////////////////////////////////////////////////// 15 | 16 | #ifndef TRANSFORM_HPP 17 | #define TRANSFORM_HPP 18 | 19 | #include "Algebra.hpp" 20 | 21 | //////////////////////////////////////////////////////////////////////////////// 22 | // Affine definition 23 | class Affine 24 | { 25 | public: 26 | // Factories 27 | static Affine Translation(const Vector3& translation); 28 | static Affine RotationAboutX(float radians); 29 | static Affine RotationAboutY(float radians); 30 | static Affine RotationAboutZ(float radians); 31 | static Affine RotationAboutAxis(const Vector3& unitAxis, 32 | float radians); 33 | static Affine Rotation(float yawRadians, 34 | float pitchRadians, 35 | float rollRadians); 36 | static Affine Scale(float nonZeroFactor); 37 | static Affine LookAt(const Vector3& position, 38 | const Vector3& targetPosition, 39 | const Vector3& unitUp); 40 | 41 | // Constructors 42 | Affine(); 43 | 44 | // Comparison operators 45 | bool operator==(const Affine& affine) const; 46 | bool operator!=(const Affine& affine) const; 47 | 48 | // Translations 49 | void TranslateWorld(const Vector3& direction); 50 | void TranslateLocal(const Vector3& direction); 51 | 52 | // Rotations 53 | void RotateAboutWorldX(float radians); 54 | void RotateAboutWorldY(float radians); 55 | void RotateAboutWorldZ(float radians); 56 | void RotateAboutLocalX(float radians); 57 | void RotateAboutLocalY(float radians); 58 | void RotateAboutLocalZ(float radians); 59 | 60 | // Look at 61 | void LookAt(const Vector3& targetPos, 62 | const Vector3& unitUp); 63 | 64 | // Resets 65 | void MakeDefaultAxis(); 66 | void MakeZeroPosition(); 67 | void MakeUnitScale(); 68 | 69 | // Matrix extraction 70 | Matrix4x4 ExtractTransformMatrix() const; 71 | Matrix4x4 ExtractInverseTransformMatrix() const; 72 | 73 | // Axis queries 74 | const Vector3& UnitXAxis() const; 75 | const Vector3& UnitYAxis() const; 76 | const Vector3& UnitZAxis() const; 77 | 78 | // Accessors 79 | const Matrix3x3& GetUnitAxis() const; 80 | const Vector3& GetPosition() const; 81 | float GetScale() const; 82 | 83 | // Mutators 84 | void SetScale(float nonZeroScale); 85 | void SetPosition(const Vector3& position); 86 | 87 | // Constants 88 | static const Affine IDENTITY; // neutral transformation 89 | 90 | private: 91 | // Hidden constructors 92 | Affine(const Matrix3x3& unitAxis, 93 | const Vector3& position, 94 | float scale); 95 | 96 | // Internal manipulation 97 | void normalizeAxis(); 98 | 99 | // Members 100 | Matrix3x3 mUnitAxis; // axis 101 | Vector3 mPosition; // position 102 | float mScale; // (uniform) scale 103 | bool mIsRS; // rotation scale only 104 | }; 105 | 106 | 107 | //////////////////////////////////////////////////////////////////////////////// 108 | // Projection definition 109 | class Projection 110 | { 111 | public: 112 | // Constants 113 | enum ProjectionType 114 | { 115 | PROJECTION_TYPE_PERSPECTIVE = 0, 116 | PROJECTION_TYPE_ORTHOGRAPHIC 117 | }; 118 | 119 | // Factories 120 | static Projection Orthographic(float left, 121 | float right, 122 | float bottom, 123 | float top, 124 | float near, 125 | float far); 126 | static Projection Perspective(float fovyRadians, 127 | float aspect, 128 | float near, 129 | float far); 130 | static Projection Frustum(float left, 131 | float right, 132 | float bottom, 133 | float top, 134 | float near, 135 | float far); 136 | 137 | // Manipulation 138 | void FitHeightToAspect(float aspect); 139 | void FitWidthToAspect(float aspect); 140 | 141 | // Matrix extraction 142 | Matrix4x4 ExtractTransformMatrix() const; 143 | Matrix4x4 ExtractInverseTransformMatrix() const; 144 | 145 | // Queries 146 | float Width() const; 147 | float Height() const; 148 | float Depth() const; 149 | float Aspect() const; 150 | bool IsPerspective() const; 151 | bool IsOrthographic() const; 152 | 153 | // Mutators 154 | void SetLeft(float left); 155 | void SetRight(float left); 156 | void SetBottom(float left); 157 | void SetTop(float left); 158 | void SetNear(float left); 159 | void SetFar(float left); 160 | void SetType(ProjectionType type); 161 | 162 | // Accessors 163 | const float& GetLeft() const; 164 | const float& GetRight() const; 165 | const float& GetBottom() const; 166 | const float& GetTop() const; 167 | const float& GetNear() const; 168 | const float& GetFar() const; 169 | ProjectionType GetType() const; 170 | 171 | private: 172 | 173 | // Hidden construtors 174 | Projection(float left, 175 | float right, 176 | float bottom, 177 | float top, 178 | float near, 179 | float far, 180 | ProjectionType type); 181 | 182 | // Members 183 | float mLeft; 184 | float mRight; 185 | float mBottom; 186 | float mTop; 187 | float mNear; 188 | float mFar; 189 | ProjectionType mType; 190 | }; 191 | 192 | 193 | //////////////////////////////////////////////////////////////////////////////// 194 | // PinholeCamera definition 195 | //class PinholeCamera 196 | //{ 197 | //public: 198 | // // Factories (using default settings) 199 | // static PinholeCamera Perspective(int16_t xres, 200 | // int16_t yres); 201 | // static PinholeCamera Parallel(int16_t xres, 202 | // int16_t yres); 203 | // static PinholeCamera Left(int16_t xres, 204 | // int16_t yres); 205 | // static PinholeCamera Right(int16_t xres, 206 | // int16_t yres); 207 | // static PinholeCamera Top(int16_t xres, 208 | // int16_t yres); 209 | // static PinholeCamera Bottom(int16_t xres, 210 | // int16_t yres); 211 | // static PinholeCamera Front(int16_t xres, 212 | // int16_t yres); 213 | // static PinholeCamera Back(int16_t xres, 214 | // int16_t yres); 215 | 216 | // // Matrix extraction 217 | // Matrix4x4 ExtractViewTransformMatrix() const; 218 | // Matrix4x4 ExtractInverseViewTransformMatrix() const; 219 | // Matrix4x4 ExtractProjectionTransformMatrix() const; 220 | // Matrix4x4 ExtractInverseProjectionTransformMatrix() const; 221 | 222 | // // Queries 223 | // float ResolutionAspect() const; 224 | 225 | // // Member access 226 | // Affine& CoordinateSystem(); 227 | 228 | // // Extract a normalized direction 229 | // Vector3 PixelRayDir(int16_t x, 230 | // int16_t y, 231 | // float offsetX, 232 | // float offsetY) const; 233 | 234 | // // Mutators 235 | // static void SetDefaultPerspectiveProjection(const Projection& p); 236 | // static void SetDefaultParallelProjection(const Projection& p); 237 | // void SetProjection(const Projection& projection); 238 | // void SetXResolution(int16_t pixelCount); 239 | // void SetYResolution(int16_t pixelCount); 240 | 241 | // // Accessors 242 | // const Projection& GetProjection() const; 243 | // int16_t GetXResolution() const; 244 | // int16_t GetYResolution() const; 245 | 246 | //protected: 247 | // // Hidden constructors 248 | // PinholeCamera(const AffineTransform& coordSystem, 249 | // const Projection& projection, 250 | // int16_t xres, 251 | // int16_t yres); 252 | 253 | // // Members 254 | // static Projection sDefaultPerspectiveProjection; 255 | // static Projection sDefaultParallelProjection; 256 | 257 | // Affine mCoordinateSystem; 258 | // Projection mProjection; 259 | // int16_t mXResolution; 260 | // int16_t mYResolution; 261 | //}; 262 | 263 | 264 | #endif 265 | 266 | -------------------------------------------------------------------------------- /core/Vector2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Algebra.hpp" 6 | 7 | //////////////////////////////////////////////////////////////////////////////// 8 | // Constants 9 | const Vector2 Vector2::ZERO(0.0f,0.0f); 10 | 11 | 12 | //////////////////////////////////////////////////////////////////////////////// 13 | // Factories 14 | Vector2 Vector2::CompMult(const Vector2& u, 15 | const Vector2& v) 16 | { return Vector2(u[0]*v[0], u[1]*v[1]); } 17 | 18 | Vector2 Vector2::CompDiv(const Vector2& u, 19 | const Vector2& v) 20 | { return Vector2(u[0]/v[0], u[1]/v[1]); } 21 | 22 | Vector2 Vector2::CompPow(const Vector2& base, 23 | const Vector2& exponent) 24 | { return Vector2(std::pow(base[0],exponent[0]), 25 | std::pow(base[1],exponent[1])); } 26 | 27 | Vector2 Vector2::CompMin(const Vector2& u, 28 | const Vector2& v) 29 | { return Vector2(std::min(u[0],v[0]), 30 | std::min(u[1],v[1])); } 31 | 32 | Vector2 Vector2::CompMax(const Vector2& u, 33 | const Vector2& v) 34 | { return Vector2(std::max(u[0],v[0]), 35 | std::max(u[1],v[1])); } 36 | 37 | Vector2 Vector2::CompClamp(const Vector2& v, 38 | const Vector2& min, 39 | const Vector2& max) 40 | { return CompMin(CompMax(v, min), max); } 41 | 42 | 43 | //////////////////////////////////////////////////////////////////////////////// 44 | // Factories (continued) 45 | Vector2 Vector2::Reflect(const Vector2& incident, 46 | const Vector2& unitNormal) 47 | { 48 | return incident - 2.0f*DotProduct(unitNormal, incident)*unitNormal; 49 | } 50 | 51 | Vector2 Vector2::Refract(const Vector2& unitIncident, 52 | const Vector2& unitNormal, 53 | const float& eta) 54 | { 55 | float nDotI = DotProduct(unitIncident, unitNormal); 56 | float k = 1.0f - eta * eta * (1.0f - nDotI * nDotI); 57 | if(k < 0.0f) 58 | return Vector2(0.0f, 0.0f); 59 | else 60 | return eta * unitIncident - (eta * nDotI + std::sqrt(k)) * unitNormal; 61 | } 62 | 63 | 64 | //////////////////////////////////////////////////////////////////////////////// 65 | // Constructor 66 | Vector2::Vector2(const float& x, const float& y) : 67 | mX(x), mY(y) 68 | { 69 | } 70 | 71 | 72 | //////////////////////////////////////////////////////////////////////////////// 73 | // Length 74 | float Vector2::Length() const 75 | { 76 | return std::sqrt(LengthSquared()); 77 | } 78 | 79 | 80 | //////////////////////////////////////////////////////////////////////////////// 81 | // Length Squared 82 | float Vector2::LengthSquared() const 83 | { 84 | return mX*mX + mY*mY; 85 | } 86 | 87 | 88 | //////////////////////////////////////////////////////////////////////////////// 89 | // Normalize 90 | Vector2 Vector2::Normalize() const 91 | { 92 | float invLength = 1.0f/Length(); 93 | return invLength*(*this); 94 | } 95 | 96 | 97 | //////////////////////////////////////////////////////////////////////////////// 98 | // Bracket operators 99 | const float& Vector2::operator[](size_t row) const 100 | { 101 | #ifndef NDEBUG 102 | assert(row < 2); 103 | #endif 104 | return (&mX)[row]; 105 | } 106 | 107 | float& Vector2::operator[](size_t row) 108 | { 109 | return const_cast< float& >((static_cast< const Vector2& >(*this))[row]); 110 | } 111 | 112 | 113 | //////////////////////////////////////////////////////////////////////////////// 114 | // Arithmetic operators 115 | Vector2 Vector2::operator+(const Vector2& v) const 116 | {return Vector2(mX+v.mX, mY+v.mY);} 117 | 118 | Vector2 Vector2::operator-(const Vector2& v) const 119 | {return Vector2(mX-v.mX, mY-v.mY);} 120 | 121 | Vector2 Vector2::operator*(const float& s) const 122 | {return Vector2(mX*s, mY*s);} 123 | 124 | Vector2 Vector2::operator/(const float& s) const 125 | { 126 | #ifndef NDEBUG 127 | assert(s != 0.0f); 128 | #endif 129 | return (1.0f/s) * (*this); 130 | } 131 | 132 | Vector2 Vector2::operator+() const 133 | {return Vector2(+mX, +mY);} 134 | 135 | Vector2 Vector2::operator-() const 136 | {return Vector2(-mX, -mY);} 137 | 138 | //////////////////////////////////////////////////////////////////////////////// 139 | // Assignment operators 140 | Vector2& Vector2::operator+=(const Vector2& v) 141 | {mX+=v.mX; mY+=v.mY; return (*this);} 142 | 143 | Vector2& Vector2::operator-=(const Vector2& v) 144 | {mX-=v.mX; mY-=v.mY; return (*this);} 145 | 146 | Vector2& Vector2::operator*=(const float& s) 147 | {mX*=s; mY*=s; return (*this);} 148 | 149 | Vector2& Vector2::operator/=(const float& s) 150 | { 151 | #ifndef NDEBUG 152 | assert(s != 0.0f); 153 | #endif 154 | float invS = 1.0f/s; 155 | mX *= invS; 156 | mY *= invS; 157 | return (*this); 158 | } 159 | 160 | //////////////////////////////////////////////////////////////////////////////// 161 | // Comparison operators 162 | bool Vector2::operator==(const Vector2& v) const 163 | { return (v.mX == mX && v.mY == mY); } 164 | 165 | bool Vector2::operator!=(const Vector2& v) const 166 | { return !(v == *this); } 167 | 168 | 169 | //////////////////////////////////////////////////////////////////////////////// 170 | // Additionnal operators 171 | Vector2 operator*(const float& s, const Vector2& v) 172 | { 173 | return Vector2(s*v[0], s*v[1]); 174 | } 175 | 176 | 177 | //////////////////////////////////////////////////////////////////////////////// 178 | // Per component queries 179 | Vector2 Vector2::Sign() const 180 | { return Vector2(float((mX > 0) - (mX < 0)), 181 | float((mY > 0) - (mY < 0))); } 182 | 183 | Vector2 Vector2::Abs() const 184 | {return Vector2(std::abs(mX), std::abs(mY));} 185 | 186 | Vector2 Vector2::Sqr() const 187 | {return Vector2(mX*mX, mY*mY);} 188 | 189 | Vector2 Vector2::Sqrt() const 190 | {return Vector2(std::sqrt(mX), std::sqrt(mY));} 191 | 192 | Vector2 Vector2::Exp() const 193 | {return Vector2(std::exp(mX), std::exp(mY));} 194 | 195 | Vector2 Vector2::Log() const 196 | {return Vector2(std::log(mX), std::log(mY));} 197 | 198 | Vector2 Vector2::Log10() const 199 | {return Vector2(std::log10(mX), std::log10(mY));} 200 | 201 | Vector2 Vector2::Ceil() const 202 | {return Vector2(std::ceil(mX), std::ceil(mY));} 203 | 204 | Vector2 Vector2::Floor() const 205 | {return Vector2(std::floor(mX), std::floor(mY));} 206 | 207 | Vector2 Vector2::Frac() const 208 | {return (*this) - Floor();} 209 | 210 | 211 | //////////////////////////////////////////////////////////////////////////////// 212 | // Dot product 213 | float Vector2::DotProduct(const Vector2& u, const Vector2& v) 214 | { 215 | return u[0]*v[0]+u[1]*v[1]; 216 | } 217 | 218 | 219 | #if 0 220 | #include 221 | int main(int argc, char** argv) 222 | { 223 | Vector2 x = Vector2(1,8); 224 | std::cout << x.GetX() << " " << x.GetY() << std::endl; 225 | std::cout << x[0] << " " << x[1] << std::endl; 226 | x[0] += 2; 227 | x[1] = 9 - x[1]; 228 | std::cout << x[0] << " " << x[1] << std::endl; 229 | 230 | Vector2 a(1,0); 231 | a = a.Abs(); 232 | a = a.Exp(); 233 | a = -Vector2(0,1); 234 | Vector2 b(1,0); 235 | b = -Vector2::CompMult(a,b); 236 | std::cout << Vector2::DotProduct(a.Normalize(),b) << std::endl; 237 | std::cout << a.Length() << " " << a.LengthSquared() << std::endl; 238 | } 239 | #endif 240 | 241 | -------------------------------------------------------------------------------- /core/Vector3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Algebra.hpp" 6 | 7 | //////////////////////////////////////////////////////////////////////////////// 8 | // Constants 9 | const Vector3 Vector3::ZERO(0.0f,0.0f,0.0f); 10 | 11 | 12 | //////////////////////////////////////////////////////////////////////////////// 13 | // Factories 14 | Vector3 Vector3::CompMult(const Vector3& u, 15 | const Vector3& v) 16 | { return Vector3(u[0]*v[0], u[1]*v[1], u[2]*v[2]); } 17 | 18 | Vector3 Vector3::CompDiv(const Vector3& u, 19 | const Vector3& v) 20 | { return Vector3(u[0]/v[0], u[1]/v[1], u[2]/v[2]); } 21 | 22 | Vector3 Vector3::CompPow(const Vector3& base, 23 | const Vector3& exponent) 24 | { return Vector3(std::pow(base[0],exponent[0]), 25 | std::pow(base[1],exponent[1]), 26 | std::pow(base[2],exponent[2])); } 27 | 28 | Vector3 Vector3::CompMin(const Vector3& u, 29 | const Vector3& v) 30 | { return Vector3(std::min(u[0],v[0]), 31 | std::min(u[1],v[1]), 32 | std::min(u[2],v[2])); } 33 | 34 | Vector3 Vector3::CompMax(const Vector3& u, 35 | const Vector3& v) 36 | { return Vector3(std::max(u[0],v[0]), 37 | std::max(u[1],v[1]), 38 | std::max(u[2],v[2])); } 39 | 40 | Vector3 Vector3::CompClamp(const Vector3& v, 41 | const Vector3& min, 42 | const Vector3& max) 43 | { return CompMin(CompMax(v, min), max); } 44 | 45 | 46 | //////////////////////////////////////////////////////////////////////////////// 47 | // Factories (continued) 48 | Vector3 Vector3::Reflect(const Vector3& incident, 49 | const Vector3& unitNormal) 50 | { 51 | return incident - 2.0f*DotProduct(unitNormal, incident)*unitNormal; 52 | } 53 | 54 | Vector3 Vector3::Refract(const Vector3& unitIncident, 55 | const Vector3& unitNormal, 56 | const float& eta) 57 | { 58 | float nDotI = DotProduct(unitIncident, unitNormal); 59 | float k = 1.0f - eta * eta * (1.0f - nDotI * nDotI); 60 | if(k < 0.0f) 61 | return Vector3(0.0f, 0.0f, 0.0f); 62 | else 63 | return eta * unitIncident - (eta * nDotI + std::sqrt(k)) * unitNormal; 64 | } 65 | 66 | 67 | //////////////////////////////////////////////////////////////////////////////// 68 | // Constructor 69 | Vector3::Vector3(const float& x, const float& y, const float& z) : 70 | mX(x), mY(y), mZ(z) 71 | { 72 | } 73 | 74 | 75 | //////////////////////////////////////////////////////////////////////////////// 76 | // Length 77 | float Vector3::Length() const 78 | { 79 | return std::sqrt(LengthSquared()); 80 | } 81 | 82 | 83 | //////////////////////////////////////////////////////////////////////////////// 84 | // Length Squared 85 | float Vector3::LengthSquared() const 86 | { 87 | return mX*mX + mY*mY + mZ*mZ; 88 | } 89 | 90 | 91 | //////////////////////////////////////////////////////////////////////////////// 92 | // Normalize 93 | Vector3 Vector3::Normalize() const 94 | { 95 | float invLength = 1.0f/Length(); 96 | return invLength*(*this); 97 | } 98 | 99 | 100 | //////////////////////////////////////////////////////////////////////////////// 101 | // Bracket operators 102 | const float& Vector3::operator[](size_t row) const 103 | { 104 | #ifndef NDEBUG 105 | assert(row < 3); 106 | #endif 107 | return (&mX)[row]; 108 | } 109 | 110 | float& Vector3::operator[](size_t row) 111 | { 112 | return const_cast< float& >((static_cast< const Vector3& >(*this))[row]); 113 | } 114 | 115 | 116 | //////////////////////////////////////////////////////////////////////////////// 117 | // Arithmetic operators 118 | Vector3 Vector3::operator+(const Vector3& v) const 119 | {return Vector3(mX+v.mX, mY+v.mY, mZ+v.mZ);} 120 | 121 | Vector3 Vector3::operator-(const Vector3& v) const 122 | {return Vector3(mX-v.mX, mY-v.mY, mZ-v.mZ);} 123 | 124 | Vector3 Vector3::operator*(const float& s) const 125 | {return Vector3(mX*s, mY*s, mZ*s);} 126 | 127 | Vector3 Vector3::operator/(const float& s) const 128 | { 129 | #ifndef NDEBUG 130 | assert(s != 0.0f); 131 | #endif 132 | return (1.0f/s) * (*this); 133 | } 134 | 135 | Vector3 Vector3::operator+() const 136 | {return Vector3(+mX, +mY, +mZ);} 137 | 138 | Vector3 Vector3::operator-() const 139 | {return Vector3(-mX, -mY, -mZ);} 140 | 141 | //////////////////////////////////////////////////////////////////////////////// 142 | // Assignment operators 143 | Vector3& Vector3::operator+=(const Vector3& v) 144 | {mX+=v.mX; mY+=v.mY; mZ+=v.mZ; return (*this);} 145 | 146 | Vector3& Vector3::operator-=(const Vector3& v) 147 | {mX-=v.mX; mY-=v.mY; mZ-=v.mZ; return (*this);} 148 | 149 | Vector3& Vector3::operator*=(const float& s) 150 | {mX*=s; mY*=s; mZ*=s; return (*this);} 151 | 152 | Vector3& Vector3::operator/=(const float& s) 153 | { 154 | #ifndef NDEBUG 155 | assert(s != 0.0f); 156 | #endif 157 | float invS = 1.0f/s; 158 | mX *= invS; 159 | mY *= invS; 160 | mZ *= invS; 161 | return (*this); 162 | } 163 | 164 | //////////////////////////////////////////////////////////////////////////////// 165 | // Comparison operators 166 | bool Vector3::operator==(const Vector3& v) const 167 | { return (v.mX == mX && v.mY == mY && v.mZ == mZ); } 168 | 169 | bool Vector3::operator!=(const Vector3& v) const 170 | { return !(v == *this); } 171 | 172 | 173 | //////////////////////////////////////////////////////////////////////////////// 174 | // Additionnal operators 175 | Vector3 operator*(const float& s, const Vector3& v) 176 | { 177 | return Vector3(s*v[0], s*v[1], s*v[2]); 178 | } 179 | 180 | 181 | //////////////////////////////////////////////////////////////////////////////// 182 | // Per component queries 183 | Vector3 Vector3::Sign() const 184 | { return Vector3(float((mX > 0) - (mX < 0)), 185 | float((mY > 0) - (mY < 0)), 186 | float((mZ > 0) - (mZ < 0))); } 187 | 188 | Vector3 Vector3::Abs() const 189 | {return Vector3(std::abs(mX), std::abs(mY), std::abs(mZ));} 190 | 191 | Vector3 Vector3::Sqr() const 192 | {return Vector3(mX*mX, mY*mY, mZ*mZ);} 193 | 194 | Vector3 Vector3::Sqrt() const 195 | {return Vector3(std::sqrt(mX), std::sqrt(mY), std::sqrt(mZ));} 196 | 197 | Vector3 Vector3::Exp() const 198 | {return Vector3(std::exp(mX), std::exp(mY), std::exp(mZ));} 199 | 200 | Vector3 Vector3::Log() const 201 | {return Vector3(std::log(mX), std::log(mY), std::log(mZ));} 202 | 203 | Vector3 Vector3::Log10() const 204 | {return Vector3(std::log10(mX), std::log10(mY), std::log10(mZ));} 205 | 206 | Vector3 Vector3::Ceil() const 207 | {return Vector3(std::ceil(mX), std::ceil(mY), std::ceil(mZ));} 208 | 209 | Vector3 Vector3::Floor() const 210 | {return Vector3(std::floor(mX), std::floor(mY), std::floor(mZ));} 211 | 212 | Vector3 Vector3::Frac() const 213 | {return (*this) - Floor();} 214 | 215 | 216 | //////////////////////////////////////////////////////////////////////////////// 217 | // Dot product 218 | float Vector3::DotProduct(const Vector3& u, const Vector3& v) 219 | { 220 | return u[0]*v[0]+u[1]*v[1]+u[2]*v[2]; 221 | } 222 | 223 | 224 | //////////////////////////////////////////////////////////////////////////////// 225 | // Cross product 226 | Vector3 Vector3::CrossProduct(const Vector3& u, const Vector3& v) 227 | { 228 | return Vector3(u[1]*v[2]-v[1]*u[2], 229 | u[2]*v[0]-v[2]*u[0], 230 | u[0]*v[1]-v[0]*u[1]); 231 | } 232 | 233 | 234 | #if 0 235 | #include 236 | int main(int argc, char** argv) 237 | { 238 | Vector3 x = Vector3(1,8,0); 239 | std::cout << x.GetX() << " " << x.GetY() << " " << x.GetZ() << " " << std::endl; 240 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << std::endl; 241 | x[0]+= 3; 242 | x[1] = 9 - x[1]; 243 | x[2] -= x.GetY(); 244 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << std::endl; 245 | 246 | x = Vector3(-98,0,56).Sign(); 247 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << std::endl; 248 | 249 | Vector3 a(1,0,0); 250 | a = a.Abs(); 251 | a = a.Exp(); 252 | a = -Vector3(1,0,0); 253 | Vector3 b(1,0,0); 254 | b = -Vector3::CompMult(a,b); 255 | std::cout << Vector3::DotProduct(a.Normalize(),b) << std::endl; 256 | std::cout << a.Length() << " " << a.LengthSquared() << std::endl; 257 | } 258 | #endif 259 | 260 | -------------------------------------------------------------------------------- /core/Vector4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Algebra.hpp" 6 | 7 | //////////////////////////////////////////////////////////////////////////////// 8 | // Constants 9 | const Vector4 Vector4::ZERO(0.0f,0.0f,0.0f,0.0f); 10 | 11 | 12 | //////////////////////////////////////////////////////////////////////////////// 13 | // Factories 14 | Vector4 Vector4::CompMult(const Vector4& u, 15 | const Vector4& v) 16 | { return Vector4(u[0]*v[0], u[1]*v[1], u[2]*v[2], u[3]*v[3]); } 17 | 18 | Vector4 Vector4::CompDiv(const Vector4& u, 19 | const Vector4& v) 20 | { return Vector4(u[0]/v[0], u[1]/v[1], u[2]/v[2], u[3]/v[3]); } 21 | 22 | Vector4 Vector4::CompPow(const Vector4& base, 23 | const Vector4& exponent) 24 | { return Vector4(std::pow(base[0],exponent[0]), 25 | std::pow(base[1],exponent[1]), 26 | std::pow(base[2],exponent[2]), 27 | std::pow(base[3],exponent[3])); } 28 | 29 | Vector4 Vector4::CompMin(const Vector4& u, 30 | const Vector4& v) 31 | { return Vector4(std::min(u[0],v[0]), 32 | std::min(u[1],v[1]), 33 | std::min(u[2],v[2]), 34 | std::min(u[3],v[3])); } 35 | 36 | Vector4 Vector4::CompMax(const Vector4& u, 37 | const Vector4& v) 38 | { return Vector4(std::max(u[0],v[0]), 39 | std::max(u[1],v[1]), 40 | std::max(u[2],v[2]), 41 | std::max(u[3],v[3])); } 42 | 43 | Vector4 Vector4::CompClamp(const Vector4& v, 44 | const Vector4& min, 45 | const Vector4& max) 46 | { return CompMin(CompMax(v, min), max); } 47 | 48 | 49 | //////////////////////////////////////////////////////////////////////////////// 50 | // Constructor 51 | Vector4::Vector4(const float& x, 52 | const float& y, 53 | const float& z, 54 | const float& w) : 55 | mX(x), mY(y), mZ(z), mW(w) 56 | { 57 | } 58 | 59 | 60 | //////////////////////////////////////////////////////////////////////////////// 61 | // Length 62 | float Vector4::Length() const 63 | { 64 | return std::sqrt(LengthSquared()); 65 | } 66 | 67 | 68 | //////////////////////////////////////////////////////////////////////////////// 69 | // Length Squared 70 | float Vector4::LengthSquared() const 71 | { 72 | return mX*mX + mY*mY + mZ*mZ + mW*mW; 73 | } 74 | 75 | 76 | //////////////////////////////////////////////////////////////////////////////// 77 | // Normalize 78 | Vector4 Vector4::Normalize() const 79 | { 80 | float invLength = 1.0f/Length(); 81 | return invLength*(*this); 82 | } 83 | 84 | 85 | //////////////////////////////////////////////////////////////////////////////// 86 | // Bracket operators 87 | const float& Vector4::operator[](size_t row) const 88 | { 89 | #ifndef NDEBUG 90 | assert(row < 4); 91 | #endif 92 | return (&mX)[row]; 93 | } 94 | 95 | float& Vector4::operator[](size_t row) 96 | { 97 | return const_cast< float& >((static_cast< const Vector4& >(*this))[row]); 98 | } 99 | 100 | 101 | //////////////////////////////////////////////////////////////////////////////// 102 | // Arithmetic operators 103 | Vector4 Vector4::operator+(const Vector4& v) const 104 | {return Vector4(mX+v.mX, mY+v.mY, mZ+v.mZ, mW+v.mW);} 105 | 106 | Vector4 Vector4::operator-(const Vector4& v) const 107 | {return Vector4(mX-v.mX, mY-v.mY, mZ-v.mZ, mW-v.mW);} 108 | 109 | Vector4 Vector4::operator*(const float& s) const 110 | {return Vector4(mX*s, mY*s, mZ*s, mW*s);} 111 | 112 | Vector4 Vector4::operator/(const float& s) const 113 | { 114 | #ifndef NDEBUG 115 | assert(s != 0.0f); 116 | #endif 117 | return (1.0f/s) * (*this); 118 | } 119 | 120 | Vector4 Vector4::operator+() const 121 | {return Vector4(+mX, +mY, +mZ, +mW);} 122 | 123 | Vector4 Vector4::operator-() const 124 | {return Vector4(-mX, -mY, -mZ, -mW);} 125 | 126 | //////////////////////////////////////////////////////////////////////////////// 127 | // Assignment operators 128 | Vector4& Vector4::operator+=(const Vector4& v) 129 | {mX+=v.mX; mY+=v.mY; mZ+=v.mZ; mW+=v.mW; return (*this);} 130 | 131 | Vector4& Vector4::operator-=(const Vector4& v) 132 | {mX-=v.mX; mY-=v.mY; mZ-=v.mZ; mW-=v.mW; return (*this);} 133 | 134 | Vector4& Vector4::operator*=(const float& s) 135 | {mX*=s; mY*=s; mZ*=s; mW*=s; return (*this);} 136 | 137 | Vector4& Vector4::operator/=(const float& s) 138 | { 139 | #ifndef NDEBUG 140 | assert(s != 0.0f); 141 | #endif 142 | float invS = 1.0f/s; 143 | mX *= invS; 144 | mY *= invS; 145 | mZ *= invS; 146 | mW *= invS; 147 | return (*this); 148 | } 149 | 150 | //////////////////////////////////////////////////////////////////////////////// 151 | // Comparison operators 152 | bool Vector4::operator==(const Vector4& v) const 153 | { return (v.mX == mX && v.mY == mY && v.mZ == mZ && v.mW == mW); } 154 | 155 | bool Vector4::operator!=(const Vector4& v) const 156 | { return !(v == *this); } 157 | 158 | 159 | //////////////////////////////////////////////////////////////////////////////// 160 | // Additionnal operators 161 | Vector4 operator*(const float& s, const Vector4& v) 162 | { 163 | return Vector4(s*v[0], s*v[1], s*v[2], s*v[3]); 164 | } 165 | 166 | 167 | //////////////////////////////////////////////////////////////////////////////// 168 | // Per component queries 169 | Vector4 Vector4::Sign() const 170 | { return Vector4(float((mX > 0) - (mX < 0)), 171 | float((mY > 0) - (mY < 0)), 172 | float((mZ > 0) - (mZ < 0)), 173 | float((mW > 0) - (mW < 0))); } 174 | 175 | Vector4 Vector4::Abs() const 176 | {return Vector4(std::abs(mX), std::abs(mY), std::abs(mZ), std::abs(mW));} 177 | 178 | Vector4 Vector4::Sqr() const 179 | {return Vector4(mX*mX, mY*mY, mZ*mZ, mW*mW);} 180 | 181 | Vector4 Vector4::Sqrt() const 182 | {return Vector4(std::sqrt(mX), std::sqrt(mY), std::sqrt(mZ), std::sqrt(mW));} 183 | 184 | Vector4 Vector4::Exp() const 185 | {return Vector4(std::exp(mX), std::exp(mY), std::exp(mZ), std::exp(mW));} 186 | 187 | Vector4 Vector4::Log() const 188 | {return Vector4(std::log(mX), std::log(mY), std::log(mZ), std::log(mW));} 189 | 190 | Vector4 Vector4::Log10() const 191 | {return Vector4(std::log10(mX), 192 | std::log10(mY), 193 | std::log10(mZ), 194 | std::log10(mW));} 195 | 196 | Vector4 Vector4::Ceil() const 197 | {return Vector4(std::ceil(mX), std::ceil(mY), std::ceil(mZ), std::ceil(mW));} 198 | 199 | Vector4 Vector4::Floor() const 200 | {return Vector4(std::floor(mX), 201 | std::floor(mY), 202 | std::floor(mZ), 203 | std::floor(mW));} 204 | 205 | Vector4 Vector4::Frac() const 206 | {return (*this) - Floor();} 207 | 208 | 209 | //////////////////////////////////////////////////////////////////////////////// 210 | // Dot product 211 | float Vector4::DotProduct(const Vector4& u, const Vector4& v) 212 | { 213 | return u[0]*v[0]+u[1]*v[1]+u[2]*v[2]+u[3]*v[3]; 214 | } 215 | 216 | 217 | #if 0 218 | #include 219 | int main(int argc, char** argv) 220 | { 221 | Vector4 x = Vector4(1,8,0,8); 222 | std::cout << x.GetX() << " " 223 | << x.GetY() << " " 224 | << x.GetZ() << " " 225 | << x.GetW() << std::endl; 226 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl; 227 | x[0]+= 4; 228 | x[1] = 9 - x[1]; 229 | x[2] -= x.GetY(); 230 | x[3] = x.GetW()-1; 231 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl; 232 | 233 | x = Vector4(-98,0,56,-0).Sign(); 234 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl; 235 | 236 | x = 2.0f*x; 237 | 238 | x /= 2.0f; 239 | std::cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl; 240 | 241 | Vector4 a(1,0,0,1); 242 | a = a.Abs(); 243 | a = a.Exp(); 244 | a = -Vector4(1,0,0,0); 245 | Vector4 b(-1,0,0,0); 246 | b = -Vector4::CompMult(a,b); 247 | std::cout << Vector4::DotProduct(a.Normalize(),b) << std::endl; 248 | std::cout << a.Length() << " " << a.LengthSquared() << std::endl; 249 | } 250 | #endif 251 | 252 | -------------------------------------------------------------------------------- /docs/globalsShader.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Proland: a procedural landscape rendering library. 3 | * Copyright (c) 2008-2011 INRIA 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /* 20 | * Proland is distributed under a dual-license scheme. 21 | * You can obtain a specific license from Inria: proland-licensing@inria.fr. 22 | */ 23 | 24 | /* 25 | * Authors: Eric Bruneton, Antoine Begault, Guillaume Piolat. 26 | */ 27 | 28 | uniform globals { 29 | vec3 worldCameraPos; 30 | vec3 worldSunDir; 31 | float minRadius; 32 | float maxRadius; 33 | float treeTau; 34 | float treeHeight; 35 | float treeDensity; 36 | int nViews; 37 | float maxTreeDistance; 38 | vec4 shadowLimit; 39 | mat4 tangentFrameToShadow[4]; 40 | }; 41 | 42 | vec3 getWorldCameraPos() { 43 | return worldCameraPos; 44 | } 45 | 46 | vec3 getWorldSunDir() { 47 | return worldSunDir; 48 | } 49 | 50 | float getMinRadius() { 51 | return minRadius; 52 | } 53 | 54 | float getMaxRadius() { 55 | return maxRadius; 56 | } 57 | 58 | float getTreeTau() { 59 | return treeTau; 60 | } 61 | 62 | float getTreeHeight() { 63 | return treeHeight; 64 | } 65 | 66 | float getTreeDensity() { 67 | return treeDensity; 68 | } 69 | 70 | int getNViews() { 71 | return nViews; 72 | } 73 | 74 | float getMaxTreeDistance() { 75 | return maxTreeDistance; 76 | } 77 | 78 | float getShadowLimit(int i) 79 | { 80 | return shadowLimit[i]; 81 | } 82 | 83 | mat4 getTangentFrameToShadow(int i) 84 | { 85 | return tangentFrameToShadow[i]; 86 | } 87 | "; 88 | -------------------------------------------------------------------------------- /docs/shader.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Proland: a procedural landscape rendering library. 3 | * Copyright (c) 2008-2011 INRIA 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /* 20 | * Proland is distributed under a dual-license scheme. 21 | * You can obtain a specific license from Inria: proland-licensing@inria.fr. 22 | */ 23 | 24 | /* 25 | * Authors: Eric Bruneton, Antoine Begault, Guillaume Piolat. 26 | */ 27 | 28 | uniform vec3 cameraRefPos; // xy: ref pos in horizontal plane, z: (1+(zFar+zNear)/(zFar-zNear))/2 29 | uniform vec4 clip[4]; 30 | uniform mat4 localToTangentFrame; 31 | uniform mat4 tangentFrameToScreen; 32 | uniform mat4 tangentFrameToWorld; 33 | uniform mat3 tangentSpaceToWorld; 34 | uniform vec3 tangentSunDir; 35 | uniform vec3 focalPos; // cameraPos is (0,0,focalPos.z) in tangent frame 36 | uniform float plantRadius; 37 | uniform float pass; 38 | 39 | 40 | 41 | #ifdef _VERTEX_ 42 | bool isVisible(vec4 c, float r) { 43 | return dot(c, clip[0]) > - r && dot(c, clip[1]) > - r && dot(c, clip[2]) > - r && dot(c, clip[3]) > - r; 44 | } 45 | layout(location=0) in vec3 lPos; // tree position 46 | layout(location=1) in vec3 lParams; // tree params ? 47 | out vec3 gPos; 48 | out vec3 gParams; 49 | out float generate; 50 | void main() 51 | { 52 | gPos = (localToTangentFrame * vec4(lPos - vec3(cameraRefPos.xy, 0.0), 1.0)).xyz; 53 | gParams = lParams; 54 | if (getTreeDensity() == 0.0) gParams.x = 1.0; 55 | float maxDist = getMaxTreeDistance(); 56 | float r = gParams.x * plantRadius; 57 | generate = length(vec3(gPos.xy,gPos.z-focalPos.z)) < maxDist && isVisible(vec4(gPos + vec3(0.0, 0.0, r), 1.0), r) ? 1.0 : 0.0; 58 | } 59 | #endif 60 | 61 | 62 | 63 | #ifdef _GEOMETRY_ 64 | layout(points) in; 65 | layout(triangle_strip,max_vertices=4) out; 66 | in vec3 gPos[]; 67 | in vec3 gParams[]; 68 | in float generate[]; 69 | flat out vec3 cPos; // camera position in local frame of cone (where cone center = 0,0,0, cone height = cone radius = 1) 70 | flat out vec4 lDIR; 71 | out vec4 cDir; // camera direction in local frame of cone (where cone center = 0,0,0, cone height = cone radius = 1) 72 | out vec3 tDir; 73 | flat out ivec3 vId; 74 | flat out vec3 vW; 75 | flat out ivec3 lId; 76 | flat out vec3 lW; 77 | void main() 78 | { 79 | if (selectTree(gPos[0] + cameraRefPos, gParams[0].y) && generate[0] > 0.5 && pass < 2.0) { 80 | vec3 WCP = getWorldCameraPos(); 81 | vec3 WSD = getWorldSunDir(); 82 | vec3 wPos = (tangentFrameToWorld * vec4(gPos[0], 1.0)).xyz; // world pos 83 | 84 | float ca = gParams[0].y * 2.0 - 1.0; // wtf ? 85 | float sa = sqrt(1.0 - ca * ca); 86 | mat2 rotation = mat2(ca, sa, -sa, ca); // rotating for what ? 87 | vec3 tSize = vec3(1.0, 1.0, 0.0) * gParams[0].x * plantRadius; 88 | 89 | // local frame 90 | vec3 gDir = normalize(vec3(gPos[0].xy, 0.0)); 91 | vec3 gLeft = vec3(-gDir.y, gDir.x, 0.0); 92 | vec3 gUp = vec3(0.0, 0.0, 1.0); 93 | gLeft *= tSize.x; 94 | gDir *= tSize.x; 95 | gUp *= tSize.y; 96 | 97 | // tree center 98 | vec3 O = gPos[0] + gUp * tSize.z / tSize.y + gUp; // TODO tree base expressed in function of tree radius or tree height, or tree half height? must be consistent with usage in brdf model 99 | bool below = focalPos.z < O.z + gUp.z; 100 | // quad 101 | vec3 A = +gLeft - gDir - gUp; 102 | vec3 B = -gLeft - gDir - gUp; 103 | vec3 C = +gLeft + (below ? -gDir : gDir) + gUp; 104 | vec3 D = -gLeft + (below ? -gDir : gDir) + gUp; 105 | 106 | // compute camera pos in tree frame 107 | cPos = (vec3(0.0, 0.0, focalPos.z) - O) / tSize.xxy; 108 | cPos = vec3(rotation * cPos.xy, cPos.z); 109 | vec3 cDIR = normalize(cPos); 110 | lDIR.xyz = normalize(vec3(rotation * tangentSunDir.xy, tangentSunDir.z) / tSize.xxy); 111 | lDIR.w = length(vec3(rotation * tangentSunDir.xy, tangentSunDir.z) / tSize.xxy); 112 | 113 | // compute views for eye and sun 114 | ivec3 vv; 115 | vec3 rr; 116 | findViews(cDIR, vv, rr); 117 | findViews(lDIR.xyz, lId, lW); 118 | vId = vv; 119 | vW = rr; 120 | 121 | // project 122 | gl_Position = tangentFrameToScreen * vec4(O + A, 1.0); 123 | tDir = O + A - vec3(0.0, 0.0, focalPos.z); 124 | cDir.xyz = vec3(rotation * A.xy, A.z) / tSize.xxy - cPos; 125 | cDir.w = dot(cPos + cDir.xyz, cDIR); // what's this ? 126 | EmitVertex(); 127 | gl_Position = tangentFrameToScreen * vec4(O + B, 1.0); 128 | tDir = O + B - vec3(0.0, 0.0, focalPos.z); 129 | cDir.xyz = vec3(rotation * B.xy, B.z) / tSize.xxy - cPos; 130 | cDir.w = dot(cPos + cDir.xyz, cDIR); 131 | EmitVertex(); 132 | gl_Position = tangentFrameToScreen * vec4(O + C, 1.0); 133 | tDir = O + C - vec3(0.0, 0.0, focalPos.z); 134 | cDir.xyz = vec3(rotation * C.xy, C.z) / tSize.xxy - cPos; 135 | cDir.w = dot(cPos + cDir.xyz, cDIR); 136 | EmitVertex(); 137 | gl_Position = tangentFrameToScreen * vec4(O + D, 1.0); 138 | tDir = O + D - vec3(0.0, 0.0, focalPos.z); 139 | cDir.xyz = vec3(rotation * D.xy, D.z) / tSize.xxy - cPos; 140 | cDir.w = dot(cPos + cDir.xyz, cDIR); 141 | EmitVertex(); 142 | EndPrimitive(); 143 | } 144 | } 145 | #endif 146 | 147 | 148 | 149 | 150 | #ifdef _FRAGMENT_ 151 | uniform sampler2DArray treeSampler; 152 | uniform Views { 153 | mat3 views[MAXNT]; 154 | }; 155 | flat in vec3 cPos; 156 | flat in vec4 lDIR; 157 | in vec4 cDir; 158 | in vec3 tDir; 159 | flat in ivec3 vId; 160 | flat in vec3 vW; 161 | flat in ivec3 lId; 162 | flat in vec3 lW; 163 | layout(location=0) out vec4 color; 164 | void main() { 165 | vec3 p0 = cPos + cDir.xyz * (1.0 + cDir.w / length(cDir.xyz)); 166 | vec2 uv0 = (views[vId.x] * p0).xy * 0.5 + 0.5; 167 | vec2 uv1 = (views[vId.y] * p0).xy * 0.5 + 0.5; 168 | vec2 uv2 = (views[vId.z] * p0).xy * 0.5 + 0.5; 169 | vec4 c0 = texture(treeSampler, vec3(uv0, vId.x)); 170 | vec4 c1 = texture(treeSampler, vec3(uv1, vId.y)); 171 | vec4 c2 = texture(treeSampler, vec3(uv2, vId.z)); 172 | // linear blend 173 | color = c0 * vW.x + c1 * vW.y + c2 * vW.z; 174 | color.rgb /= (color.a == 0.0 ? 1.0 : color.a); 175 | float t = (color.r * (2.0 * sqrt(2.0)) - sqrt(2.0)) - cDir.w; 176 | t = 1.0 - t / length(cDir.xyz); 177 | vec3 p = cPos + cDir.xyz * t; 178 | // iterative refinement 179 | uv0 = (views[vId.x] * p).xy * 0.5 + vec2(0.5); 180 | uv1 = (views[vId.y] * p).xy * 0.5 + vec2(0.5); 181 | uv2 = (views[vId.z] * p).xy * 0.5 + vec2(0.5); 182 | c0 = texture(treeSampler, vec3(uv0, vId.x)); 183 | c1 = texture(treeSampler, vec3(uv1, vId.y)); 184 | c2 = texture(treeSampler, vec3(uv2, vId.z)); 185 | vec4 color2 = c0 * vW.x + c1 * vW.y + c2 * vW.z; 186 | color2.rgb /= (color2.a == 0.0 ? 1.0 : color2.a); 187 | t = (color2.r * (2.0 * sqrt(2.0)) - sqrt(2.0)) - cDir.w; 188 | t = 1.0 - t / length(cDir.xyz); 189 | p = cPos + cDir.xyz * t; 190 | float fragZ = (gl_FragCoord.z - cameraRefPos.z) / t + cameraRefPos.z; 191 | gl_FragDepth = fragZ; 192 | if (color2.a > 0.0) { 193 | uv0 = (views[lId.x] * p).xy * 0.5 + vec2(0.5); 194 | uv1 = (views[lId.y] * p).xy * 0.5 + vec2(0.5); 195 | uv2 = (views[lId.z] * p).xy * 0.5 + vec2(0.5); 196 | c0 = texture(treeSampler, vec3(uv0, lId.x)); 197 | c1 = texture(treeSampler, vec3(uv1, lId.y)); 198 | c2 = texture(treeSampler, vec3(uv2, lId.z)); 199 | vec4 c = c0 * lW.x + c1 * lW.y + c2 * lW.z; 200 | c.r /= (c.a == 0.0 ? 1.0 : c.a); 201 | float d = (c.r * (2.0 * sqrt(2.0)) - sqrt(2.0)) - dot(p, lDIR.xyz); 202 | float kc = exp(-getTreeTau() * max(d, 0.0)); 203 | vec3 P = vec3(0.0, 0.0, focalPos.z) + tDir * t; // position in tangent frame 204 | int slice = -1; 205 | slice += fragZ < getShadowLimit(0) ? 1 : 0; 206 | slice += fragZ < getShadowLimit(1) ? 1 : 0; 207 | slice += fragZ < getShadowLimit(2) ? 1 : 0; 208 | slice += fragZ < getShadowLimit(3) ? 1 : 0; 209 | if (slice >= 0) { 210 | mat4 t2s = getTangentFrameToShadow(slice); 211 | vec3 qs = (t2s * vec4(P, 1.0)).xyz * 0.5 + vec3(0.5); 212 | float ts = shadow(vec4(qs.xy, slice, qs.z), t2s[3][3]); 213 | kc *= ts; 214 | } 215 | float tz = 0.5 - p.z * 0.5; 216 | float shear = 2.0 / getTreeHeight(); 217 | float skykc = color2.b / (1.0 + tz * tz * getTreeDensity() / (shear * shear)); 218 | color.rgb = vec3(1.0, pass == 0.0 ? kc : skykc, 0.0); 219 | } 220 | } 221 | #endif 222 | 223 | 224 | -------------------------------------------------------------------------------- /docs/treeInfo3D.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Proland: a procedural landscape rendering library. 3 | * Copyright (c) 2008-2011 INRIA 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /* 20 | * Proland is distributed under a dual-license scheme. 21 | * You can obtain a specific license from Inria: proland-licensing@inria.fr. 22 | */ 23 | 24 | /* 25 | * Authors: Eric Bruneton, Antoine Begault, Guillaume Piolat. 26 | */ 27 | 28 | const int MAXN = 9; 29 | const int MAXNT = 2*MAXN*(MAXN+1)+1; 30 | 31 | uniform sampler2DArray treeShadowMap; 32 | 33 | bool selectTree(vec3 p, float seed) 34 | { 35 | float d = getTreeDensity(); 36 | if (d == 0.0) { 37 | return p.x > -10.0 && p.x < 10.0 && p.y > 50.0 && p.y < 70.0 && seed <= 0.01; 38 | } else { 39 | return seed <= d; 40 | } 41 | } 42 | vec3 shadow0(vec3 coord) 43 | { 44 | return texture(treeShadowMap, coord.xyz).xyz; 45 | } 46 | 47 | float shadow1(vec4 coord, float dz) 48 | { 49 | vec2 za = texture(treeShadowMap, coord.xyz).xy; 50 | float d = (coord.w - za.x) / dz; 51 | return d < 0.0 ? 1.0 : za.y; 52 | } 53 | 54 | float shadow(vec4 coord, float dz) 55 | { 56 | float ts = shadow1(coord, dz); 57 | 58 | vec2 off = 1.0 / vec2(textureSize(treeShadowMap, 0).xy); 59 | ts += shadow1(vec4(coord.x - off.x, coord.y, coord.zw), dz); 60 | ts += shadow1(vec4(coord.x + off.x, coord.y, coord.zw), dz); 61 | ts += shadow1(vec4(coord.x, coord.y - off.y, coord.zw), dz); 62 | ts += shadow1(vec4(coord.x, coord.y + off.y, coord.zw), dz); 63 | ts += shadow1(vec4(coord.x - off.x, coord.y - off.y, coord.zw), dz); 64 | ts += shadow1(vec4(coord.x + off.x, coord.y - off.y, coord.zw), dz); 65 | ts += shadow1(vec4(coord.x - off.x, coord.y + off.y, coord.zw), dz); 66 | ts += shadow1(vec4(coord.x + off.x, coord.y + off.y, coord.zw), dz); 67 | ts /= 9.0; 68 | 69 | return ts; 70 | } 71 | 72 | int viewNumber(int i, int j) { 73 | int n = getNViews(); 74 | return i * ((2 * n + 1) - abs(i)) + j + (n * (n + 1)); 75 | } 76 | 77 | void findViews(vec3 cDIR, out ivec3 vv, out vec3 rr) 78 | { 79 | int n = getNViews(); 80 | vec3 VDIR = vec3(cDIR.xy, max(cDIR.z, 0.01)); 81 | float a = abs(VDIR.x) > abs(VDIR.y) ? VDIR.y / VDIR.x : -VDIR.x / VDIR.y; 82 | float nxx = n * (1.0 - a) * acos(VDIR.z) / 3.141592657; 83 | float nyy = n * (1.0 + a) * acos(VDIR.z) / 3.141592657; 84 | int i = int(floor(nxx)); 85 | int j = int(floor(nyy)); 86 | float ti = nxx - i; 87 | float tj = nyy - j; 88 | float alpha = 1.0 - ti - tj; 89 | bool b = alpha > 0.0; 90 | ivec3 ii = ivec3(b ? i : i + 1, i + 1, i); 91 | ivec3 jj = ivec3(b ? j : j + 1, j, j + 1); 92 | rr = vec3(abs(alpha), b ? ti : 1.0 - tj, b ? tj : 1.0 - ti); 93 | if (abs(VDIR.y) >= abs(VDIR.x)) { 94 | ivec3 tmp = ii; 95 | ii = -jj; 96 | jj = tmp; 97 | } 98 | ii *= int(sign(VDIR.x + VDIR.y)); 99 | jj *= int(sign(VDIR.x + VDIR.y)); 100 | vv = ivec3(viewNumber(ii.x,jj.x), viewNumber(ii.y,jj.y), viewNumber(ii.z,jj.z)); 101 | } 102 | "; 103 | -------------------------------------------------------------------------------- /freeglut.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/freeglut.dll -------------------------------------------------------------------------------- /glew.hpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | // \author Jonathan Dupuy 3 | // 4 | //////////////////////////////////////////////////////////////////////////////// 5 | 6 | #ifndef GLEW_HPP 7 | #define GLEW_HPP 8 | 9 | #ifdef _WIN32 10 | # define GLEW_STATIC 11 | #endif // _WIN32 12 | 13 | #define GLEW_NO_GLU 14 | 15 | #include "GL/glew.h" 16 | 17 | #endif 18 | 19 | //////////////////////////////////////////////////////////////////////////////// 20 | 21 | -------------------------------------------------------------------------------- /glm.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | glm.h 3 | Nate Robins, 1997, 2000 4 | nate@pobox.com, http://www.pobox.com/~nate 5 | 6 | Wavefront OBJ model file format reader/writer/manipulator. 7 | 8 | Includes routines for generating smooth normals with 9 | preservation of edges, welding redundant vertices & texture 10 | coordinate generation (spheremap and planar projections) + more. 11 | 12 | */ 13 | 14 | #include "GL/freeglut.h" 15 | 16 | #ifndef M_PI 17 | #define M_PI 3.14159265f 18 | #endif 19 | 20 | #define GLM_NONE (0) /* render with only vertices */ 21 | #define GLM_FLAT (1 << 0) /* render with facet normals */ 22 | #define GLM_SMOOTH (1 << 1) /* render with vertex normals */ 23 | #define GLM_TEXTURE (1 << 2) /* render with texture coords */ 24 | #define GLM_COLOR (1 << 3) /* render with colors */ 25 | #define GLM_MATERIAL (1 << 4) /* render with materials */ 26 | 27 | 28 | /* GLMmaterial: Structure that defines a material in a model. 29 | */ 30 | typedef struct _GLMmaterial 31 | { 32 | char* name; /* name of material */ 33 | GLfloat diffuse[4]; /* diffuse component */ 34 | GLfloat ambient[4]; /* ambient component */ 35 | GLfloat specular[4]; /* specular component */ 36 | GLfloat emmissive[4]; /* emmissive component */ 37 | GLfloat shininess; /* specular exponent */ 38 | } GLMmaterial; 39 | 40 | /* GLMtriangle: Structure that defines a triangle in a model. 41 | */ 42 | typedef struct _GLMtriangle { 43 | GLuint vindices[3]; /* array of triangle vertex indices */ 44 | GLuint nindices[3]; /* array of triangle normal indices */ 45 | GLuint tindices[3]; /* array of triangle texcoord indices*/ 46 | GLuint findex; /* index of triangle facet normal */ 47 | } GLMtriangle; 48 | 49 | /* GLMgroup: Structure that defines a group in a model. 50 | */ 51 | typedef struct _GLMgroup { 52 | char* name; /* name of this group */ 53 | GLuint numtriangles; /* number of triangles in this group */ 54 | GLuint* triangles; /* array of triangle indices */ 55 | GLuint material; /* index to material for group */ 56 | struct _GLMgroup* next; /* pointer to next group in model */ 57 | } GLMgroup; 58 | 59 | /* GLMmodel: Structure that defines a model. 60 | */ 61 | typedef struct _GLMmodel { 62 | char* pathname; /* path to this model */ 63 | char* mtllibname; /* name of the material library */ 64 | 65 | GLuint numvertices; /* number of vertices in model */ 66 | GLfloat* vertices; /* array of vertices */ 67 | 68 | GLuint numnormals; /* number of normals in model */ 69 | GLfloat* normals; /* array of normals */ 70 | 71 | GLuint numtexcoords; /* number of texcoords in model */ 72 | GLfloat* texcoords; /* array of texture coordinates */ 73 | 74 | GLuint numfacetnorms; /* number of facetnorms in model */ 75 | GLfloat* facetnorms; /* array of facetnorms */ 76 | 77 | GLuint numtriangles; /* number of triangles in model */ 78 | GLMtriangle* triangles; /* array of triangles */ 79 | 80 | GLuint nummaterials; /* number of materials in model */ 81 | GLMmaterial* materials; /* array of materials */ 82 | 83 | GLuint numgroups; /* number of groups in model */ 84 | GLMgroup* groups; /* linked list of groups */ 85 | 86 | GLfloat position[3]; /* position of the model */ 87 | 88 | } GLMmodel; 89 | 90 | 91 | /* glmUnitize: "unitize" a model by translating it to the origin and 92 | * scaling it to fit in a unit cube around the origin. Returns the 93 | * scalefactor used. 94 | * 95 | * model - properly initialized GLMmodel structure 96 | */ 97 | GLfloat 98 | glmUnitize(GLMmodel* model); 99 | 100 | /* glmDimensions: Calculates the dimensions (width, height, depth) of 101 | * a model. 102 | * 103 | * model - initialized GLMmodel structure 104 | * dimensions - array of 3 GLfloats (GLfloat dimensions[3]) 105 | */ 106 | GLvoid 107 | glmDimensions(GLMmodel* model, GLfloat* dimensions); 108 | 109 | /* glmScale: Scales a model by a given amount. 110 | * 111 | * model - properly initialized GLMmodel structure 112 | * scale - scalefactor (0.5 = half as large, 2.0 = twice as large) 113 | */ 114 | GLvoid 115 | glmScale(GLMmodel* model, GLfloat scale); 116 | 117 | /* glmReverseWinding: Reverse the polygon winding for all polygons in 118 | * this model. Default winding is counter-clockwise. Also changes 119 | * the direction of the normals. 120 | * 121 | * model - properly initialized GLMmodel structure 122 | */ 123 | GLvoid 124 | glmReverseWinding(GLMmodel* model); 125 | 126 | /* glmFacetNormals: Generates facet normals for a model (by taking the 127 | * cross product of the two vectors derived from the sides of each 128 | * triangle). Assumes a counter-clockwise winding. 129 | * 130 | * model - initialized GLMmodel structure 131 | */ 132 | GLvoid 133 | glmFacetNormals(GLMmodel* model); 134 | 135 | /* glmVertexNormals: Generates smooth vertex normals for a model. 136 | * First builds a list of all the triangles each vertex is in. Then 137 | * loops through each vertex in the the list averaging all the facet 138 | * normals of the triangles each vertex is in. Finally, sets the 139 | * normal index in the triangle for the vertex to the generated smooth 140 | * normal. If the dot product of a facet normal and the facet normal 141 | * associated with the first triangle in the list of triangles the 142 | * current vertex is in is greater than the cosine of the angle 143 | * parameter to the function, that facet normal is not added into the 144 | * average normal calculation and the corresponding vertex is given 145 | * the facet normal. This tends to preserve hard edges. The angle to 146 | * use depends on the model, but 90 degrees is usually a good start. 147 | * 148 | * model - initialized GLMmodel structure 149 | * angle - maximum angle (in degrees) to smooth across 150 | */ 151 | GLvoid 152 | glmVertexNormals(GLMmodel* model, GLfloat angle); 153 | 154 | /* glmLinearTexture: Generates texture coordinates according to a 155 | * linear projection of the texture map. It generates these by 156 | * linearly mapping the vertices onto a square. 157 | * 158 | * model - pointer to initialized GLMmodel structure 159 | */ 160 | GLvoid 161 | glmLinearTexture(GLMmodel* model); 162 | 163 | /* glmSpheremapTexture: Generates texture coordinates according to a 164 | * spherical projection of the texture map. Sometimes referred to as 165 | * spheremap, or reflection map texture coordinates. It generates 166 | * these by using the normal to calculate where that vertex would map 167 | * onto a sphere. Since it is impossible to map something flat 168 | * perfectly onto something spherical, there is distortion at the 169 | * poles. This particular implementation causes the poles along the X 170 | * axis to be distorted. 171 | * 172 | * model - pointer to initialized GLMmodel structure 173 | */ 174 | GLvoid 175 | glmSpheremapTexture(GLMmodel* model); 176 | 177 | /* glmDelete: Deletes a GLMmodel structure. 178 | * 179 | * model - initialized GLMmodel structure 180 | */ 181 | GLvoid 182 | glmDelete(GLMmodel* model); 183 | 184 | /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file. 185 | * Returns a pointer to the created object which should be free'd with 186 | * glmDelete(). 187 | * 188 | * filename - name of the file containing the Wavefront .OBJ format data. 189 | */ 190 | GLMmodel* 191 | glmReadOBJ(const char* filename); 192 | 193 | /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to 194 | * a file. 195 | * 196 | * model - initialized GLMmodel structure 197 | * filename - name of the file to write the Wavefront .OBJ format data to 198 | * mode - a bitwise or of values describing what is written to the file 199 | * GLM_NONE - write only vertices 200 | * GLM_FLAT - write facet normals 201 | * GLM_SMOOTH - write vertex normals 202 | * GLM_TEXTURE - write texture coords 203 | * GLM_FLAT and GLM_SMOOTH should not both be specified. 204 | */ 205 | GLvoid 206 | glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode); 207 | 208 | /* glmDraw: Renders the model to the current OpenGL context using the 209 | * mode specified. 210 | * 211 | * model - initialized GLMmodel structure 212 | * mode - a bitwise OR of values describing what is to be rendered. 213 | * GLM_NONE - render with only vertices 214 | * GLM_FLAT - render with facet normals 215 | * GLM_SMOOTH - render with vertex normals 216 | * GLM_TEXTURE - render with texture coords 217 | * GLM_FLAT and GLM_SMOOTH should not both be specified. 218 | */ 219 | GLvoid 220 | glmDraw(GLMmodel* model, GLuint mode); 221 | 222 | /* glmList: Generates and returns a display list for the model using 223 | * the mode specified. 224 | * 225 | * model - initialized GLMmodel structure 226 | * mode - a bitwise OR of values describing what is to be rendered. 227 | * GLM_NONE - render with only vertices 228 | * GLM_FLAT - render with facet normals 229 | * GLM_SMOOTH - render with vertex normals 230 | * GLM_TEXTURE - render with texture coords 231 | * GLM_FLAT and GLM_SMOOTH should not both be specified. 232 | */ 233 | GLuint 234 | glmList(GLMmodel* model, GLuint mode); 235 | 236 | /* glmWeld: eliminate (weld) vectors that are within an epsilon of 237 | * each other. 238 | * 239 | * model - initialized GLMmodel structure 240 | * epsilon - maximum difference between vertices 241 | * ( 0.00001 is a good start for a unitized model) 242 | * 243 | */ 244 | GLvoid 245 | glmWeld(GLMmodel* model, GLfloat epsilon); 246 | 247 | /* glmReadPPM: read a PPM raw (type P6) file. The PPM file has a header 248 | * that should look something like: 249 | * 250 | * P6 251 | * # comment 252 | * width height max_value 253 | * rgbrgbrgb... 254 | * 255 | * where "P6" is the magic cookie which identifies the file type and 256 | * should be the only characters on the first line followed by a 257 | * carriage return. Any line starting with a # mark will be treated 258 | * as a comment and discarded. After the magic cookie, three integer 259 | * values are expected: width, height of the image and the maximum 260 | * value for a pixel (max_value must be < 256 for PPM raw files). The 261 | * data section consists of width*height rgb triplets (one byte each) 262 | * in binary format (i.e., such as that written with fwrite() or 263 | * equivalent). 264 | * 265 | * The rgb data is returned as an array of unsigned chars (packed 266 | * rgb). The malloc()'d memory should be free()'d by the caller. If 267 | * an error occurs, an error message is sent to stderr and NULL is 268 | * returned. 269 | * 270 | * filename - name of the .ppm file. 271 | * width - will contain the width of the image on return. 272 | * height - will contain the height of the image on return. 273 | * 274 | */ 275 | GLubyte* 276 | glmReadPPM(char* filename, int* width, int* height); 277 | -------------------------------------------------------------------------------- /include/GL/freeglut.h: -------------------------------------------------------------------------------- 1 | #ifndef __FREEGLUT_H__ 2 | #define __FREEGLUT_H__ 3 | 4 | /* 5 | * freeglut.h 6 | * 7 | * The freeglut library include file 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 10 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 12 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | #include "freeglut_std.h" 18 | #include "freeglut_ext.h" 19 | 20 | /*** END OF FILE ***/ 21 | 22 | #endif /* __FREEGLUT_H__ */ 23 | -------------------------------------------------------------------------------- /include/GL/freeglut_ext.h: -------------------------------------------------------------------------------- 1 | #ifndef __FREEGLUT_EXT_H__ 2 | #define __FREEGLUT_EXT_H__ 3 | 4 | /* 5 | * freeglut_ext.h 6 | * 7 | * The non-GLUT-compatible extensions to the freeglut library include file 8 | * 9 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 10 | * Written by Pawel W. Olszta, 11 | * Creation date: Thu Dec 2 1999 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a 14 | * copy of this software and associated documentation files (the "Software"), 15 | * to deal in the Software without restriction, including without limitation 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 17 | * and/or sell copies of the Software, and to permit persons to whom the 18 | * Software is furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included 21 | * in all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 24 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 26 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 27 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 28 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | */ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /* 36 | * Additional GLUT Key definitions for the Special key function 37 | */ 38 | #define GLUT_KEY_NUM_LOCK 0x006D 39 | #define GLUT_KEY_BEGIN 0x006E 40 | #define GLUT_KEY_DELETE 0x006F 41 | 42 | /* 43 | * GLUT API Extension macro definitions -- behaviour when the user clicks on an "x" to close a window 44 | */ 45 | #define GLUT_ACTION_EXIT 0 46 | #define GLUT_ACTION_GLUTMAINLOOP_RETURNS 1 47 | #define GLUT_ACTION_CONTINUE_EXECUTION 2 48 | 49 | /* 50 | * Create a new rendering context when the user opens a new window? 51 | */ 52 | #define GLUT_CREATE_NEW_CONTEXT 0 53 | #define GLUT_USE_CURRENT_CONTEXT 1 54 | 55 | /* 56 | * Direct/Indirect rendering context options (has meaning only in Unix/X11) 57 | */ 58 | #define GLUT_FORCE_INDIRECT_CONTEXT 0 59 | #define GLUT_ALLOW_DIRECT_CONTEXT 1 60 | #define GLUT_TRY_DIRECT_CONTEXT 2 61 | #define GLUT_FORCE_DIRECT_CONTEXT 3 62 | 63 | /* 64 | * GLUT API Extension macro definitions -- the glutGet parameters 65 | */ 66 | #define GLUT_INIT_STATE 0x007C 67 | 68 | #define GLUT_ACTION_ON_WINDOW_CLOSE 0x01F9 69 | 70 | #define GLUT_WINDOW_BORDER_WIDTH 0x01FA 71 | #define GLUT_WINDOW_HEADER_HEIGHT 0x01FB 72 | 73 | #define GLUT_VERSION 0x01FC 74 | 75 | #define GLUT_RENDERING_CONTEXT 0x01FD 76 | #define GLUT_DIRECT_RENDERING 0x01FE 77 | 78 | #define GLUT_FULL_SCREEN 0x01FF 79 | 80 | /* 81 | * New tokens for glutInitDisplayMode. 82 | * Only one GLUT_AUXn bit may be used at a time. 83 | * Value 0x0400 is defined in OpenGLUT. 84 | */ 85 | #define GLUT_AUX 0x1000 86 | 87 | #define GLUT_AUX1 0x1000 88 | #define GLUT_AUX2 0x2000 89 | #define GLUT_AUX3 0x4000 90 | #define GLUT_AUX4 0x8000 91 | 92 | /* 93 | * Context-related flags, see freeglut_state.c 94 | */ 95 | #define GLUT_INIT_MAJOR_VERSION 0x0200 96 | #define GLUT_INIT_MINOR_VERSION 0x0201 97 | #define GLUT_INIT_FLAGS 0x0202 98 | #define GLUT_INIT_PROFILE 0x0203 99 | 100 | /* 101 | * Flags for glutInitContextFlags, see freeglut_init.c 102 | */ 103 | #define GLUT_DEBUG 0x0001 104 | #define GLUT_FORWARD_COMPATIBLE 0x0002 105 | 106 | 107 | /* 108 | * Flags for glutInitContextProfile, see freeglut_init.c 109 | */ 110 | #define GLUT_CORE_PROFILE 0x0001 111 | #define GLUT_COMPATIBILITY_PROFILE 0x0002 112 | 113 | /* 114 | * Process loop function, see freeglut_main.c 115 | */ 116 | FGAPI void FGAPIENTRY glutMainLoopEvent( void ); 117 | FGAPI void FGAPIENTRY glutLeaveMainLoop( void ); 118 | FGAPI void FGAPIENTRY glutExit ( void ); 119 | 120 | /* 121 | * Window management functions, see freeglut_window.c 122 | */ 123 | FGAPI void FGAPIENTRY glutFullScreenToggle( void ); 124 | 125 | /* 126 | * Window-specific callback functions, see freeglut_callbacks.c 127 | */ 128 | FGAPI void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) ); 129 | FGAPI void FGAPIENTRY glutCloseFunc( void (* callback)( void ) ); 130 | FGAPI void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) ); 131 | /* A. Donev: Also a destruction callback for menus */ 132 | FGAPI void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) ); 133 | 134 | /* 135 | * State setting and retrieval functions, see freeglut_state.c 136 | */ 137 | FGAPI void FGAPIENTRY glutSetOption ( GLenum option_flag, int value ); 138 | FGAPI int * FGAPIENTRY glutGetModeValues(GLenum mode, int * size); 139 | /* A.Donev: User-data manipulation */ 140 | FGAPI void* FGAPIENTRY glutGetWindowData( void ); 141 | FGAPI void FGAPIENTRY glutSetWindowData(void* data); 142 | FGAPI void* FGAPIENTRY glutGetMenuData( void ); 143 | FGAPI void FGAPIENTRY glutSetMenuData(void* data); 144 | 145 | /* 146 | * Font stuff, see freeglut_font.c 147 | */ 148 | FGAPI int FGAPIENTRY glutBitmapHeight( void* font ); 149 | FGAPI GLfloat FGAPIENTRY glutStrokeHeight( void* font ); 150 | FGAPI void FGAPIENTRY glutBitmapString( void* font, const unsigned char *string ); 151 | FGAPI void FGAPIENTRY glutStrokeString( void* font, const unsigned char *string ); 152 | 153 | /* 154 | * Geometry functions, see freeglut_geometry.c 155 | */ 156 | FGAPI void FGAPIENTRY glutWireRhombicDodecahedron( void ); 157 | FGAPI void FGAPIENTRY glutSolidRhombicDodecahedron( void ); 158 | FGAPI void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale ); 159 | FGAPI void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale ); 160 | FGAPI void FGAPIENTRY glutWireCylinder( GLdouble radius, GLdouble height, GLint slices, GLint stacks); 161 | FGAPI void FGAPIENTRY glutSolidCylinder( GLdouble radius, GLdouble height, GLint slices, GLint stacks); 162 | 163 | /* 164 | * Extension functions, see freeglut_ext.c 165 | */ 166 | typedef void (*GLUTproc)(); 167 | FGAPI GLUTproc FGAPIENTRY glutGetProcAddress( const char *procName ); 168 | 169 | /* 170 | * Joystick functions, see freeglut_joystick.c 171 | */ 172 | /* USE OF THESE FUNCTIONS IS DEPRECATED !!!!! */ 173 | /* If you have a serious need for these functions in your application, please either 174 | * contact the "freeglut" developer community at freeglut-developer@lists.sourceforge.net, 175 | * switch to the OpenGLUT library, or else port your joystick functionality over to PLIB's 176 | * "js" library. 177 | */ 178 | int glutJoystickGetNumAxes( int ident ); 179 | int glutJoystickGetNumButtons( int ident ); 180 | int glutJoystickNotWorking( int ident ); 181 | float glutJoystickGetDeadBand( int ident, int axis ); 182 | void glutJoystickSetDeadBand( int ident, int axis, float db ); 183 | float glutJoystickGetSaturation( int ident, int axis ); 184 | void glutJoystickSetSaturation( int ident, int axis, float st ); 185 | void glutJoystickSetMinRange( int ident, float *axes ); 186 | void glutJoystickSetMaxRange( int ident, float *axes ); 187 | void glutJoystickSetCenter( int ident, float *axes ); 188 | void glutJoystickGetMinRange( int ident, float *axes ); 189 | void glutJoystickGetMaxRange( int ident, float *axes ); 190 | void glutJoystickGetCenter( int ident, float *axes ); 191 | 192 | /* 193 | * Initialization functions, see freeglut_init.c 194 | */ 195 | FGAPI void FGAPIENTRY glutInitContextVersion( int majorVersion, int minorVersion ); 196 | FGAPI void FGAPIENTRY glutInitContextFlags( int flags ); 197 | FGAPI void FGAPIENTRY glutInitContextProfile( int profile ); 198 | 199 | /* 200 | * GLUT API macro definitions -- the display mode definitions 201 | */ 202 | #define GLUT_CAPTIONLESS 0x0400 203 | #define GLUT_BORDERLESS 0x0800 204 | #define GLUT_SRGB 0x1000 205 | 206 | #ifdef __cplusplus 207 | } 208 | #endif 209 | 210 | /*** END OF FILE ***/ 211 | 212 | #endif /* __FREEGLUT_EXT_H__ */ 213 | -------------------------------------------------------------------------------- /lib/linux/lin32/libAntTweakBar.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin32/libAntTweakBar.so -------------------------------------------------------------------------------- /lib/linux/lin32/libAntTweakBar.so.1: -------------------------------------------------------------------------------- 1 | ./libAntTweakBar.so -------------------------------------------------------------------------------- /lib/linux/lin32/libGLEW.so: -------------------------------------------------------------------------------- 1 | ./libGLEW.so.1.7.0 -------------------------------------------------------------------------------- /lib/linux/lin32/libGLEW.so.1.7: -------------------------------------------------------------------------------- 1 | ./libGLEW.so.1.7.0 -------------------------------------------------------------------------------- /lib/linux/lin32/libGLEW.so.1.7.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin32/libGLEW.so.1.7.0 -------------------------------------------------------------------------------- /lib/linux/lin32/libglut.so: -------------------------------------------------------------------------------- 1 | ./libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/linux/lin32/libglut.so.3: -------------------------------------------------------------------------------- 1 | ./libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/linux/lin32/libglut.so.3.9.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin32/libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libAntTweakBar.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin64/libAntTweakBar.so -------------------------------------------------------------------------------- /lib/linux/lin64/libAntTweakBar.so.1: -------------------------------------------------------------------------------- 1 | ./libAntTweakBar.so -------------------------------------------------------------------------------- /lib/linux/lin64/libGLEW.so: -------------------------------------------------------------------------------- 1 | ./libGLEW.so.1.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libGLEW.so.1.9: -------------------------------------------------------------------------------- 1 | ./libGLEW.so.1.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libGLEW.so.1.9.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin64/libGLEW.so.1.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libglut.so: -------------------------------------------------------------------------------- 1 | ./libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libglut.so.3: -------------------------------------------------------------------------------- 1 | ./libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/linux/lin64/libglut.so.3.9.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/linux/lin64/libglut.so.3.9.0 -------------------------------------------------------------------------------- /lib/windows/win32/AntTweakBar.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/windows/win32/AntTweakBar.lib -------------------------------------------------------------------------------- /lib/windows/win32/freeglut.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/windows/win32/freeglut.lib -------------------------------------------------------------------------------- /lib/windows/win32/glew32s.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdupuy/lightfield/1484e72eddc3f2692dff9991a86bcec3c85dab1d/lib/windows/win32/glew32s.lib -------------------------------------------------------------------------------- /libpng/pngerror.c: -------------------------------------------------------------------------------- 1 | 2 | /* pngerror.c - stub functions for i/o and memory allocation 3 | * 4 | * libpng version 1.2.8 - December 3, 2004 5 | * For conditions of distribution and use, see copyright notice in png.h 6 | * Copyright (c) 1998-2004 Glenn Randers-Pehrson 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 9 | * 10 | * This file provides a location for all error handling. Users who 11 | * need special error handling are expected to write replacement functions 12 | * and use png_set_error_fn() to use those functions. See the instructions 13 | * at each function. 14 | */ 15 | 16 | #define PNG_INTERNAL 17 | #include "png.h" 18 | 19 | static void /* PRIVATE */ 20 | png_default_error PNGARG((png_structp png_ptr, 21 | png_const_charp error_message)); 22 | static void /* PRIVATE */ 23 | png_default_warning PNGARG((png_structp png_ptr, 24 | png_const_charp warning_message)); 25 | 26 | /* This function is called whenever there is a fatal error. This function 27 | * should not be changed. If there is a need to handle errors differently, 28 | * you should supply a replacement error function and use png_set_error_fn() 29 | * to replace the error function at run-time. 30 | */ 31 | void PNGAPI 32 | png_error(png_structp png_ptr, png_const_charp error_message) 33 | { 34 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED 35 | char msg[16]; 36 | if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) 37 | { 38 | if (*error_message == '#') 39 | { 40 | int offset; 41 | for (offset=1; offset<15; offset++) 42 | if (*(error_message+offset) == ' ') 43 | break; 44 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) 45 | { 46 | int i; 47 | for (i=0; iflags&PNG_FLAG_STRIP_ERROR_TEXT) 58 | { 59 | msg[0]='0'; 60 | msg[1]='\0'; 61 | error_message=msg; 62 | } 63 | } 64 | } 65 | #endif 66 | if (png_ptr != NULL && png_ptr->error_fn != NULL) 67 | (*(png_ptr->error_fn))(png_ptr, error_message); 68 | 69 | /* If the custom handler doesn't exist, or if it returns, 70 | use the default handler, which will not return. */ 71 | png_default_error(png_ptr, error_message); 72 | } 73 | 74 | /* This function is called whenever there is a non-fatal error. This function 75 | * should not be changed. If there is a need to handle warnings differently, 76 | * you should supply a replacement warning function and use 77 | * png_set_error_fn() to replace the warning function at run-time. 78 | */ 79 | void PNGAPI 80 | png_warning(png_structp png_ptr, png_const_charp warning_message) 81 | { 82 | int offset = 0; 83 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED 84 | if (png_ptr->flags&(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) 85 | #endif 86 | { 87 | if (*warning_message == '#') 88 | { 89 | for (offset=1; offset<15; offset++) 90 | if (*(warning_message+offset) == ' ') 91 | break; 92 | } 93 | } 94 | if (png_ptr != NULL && png_ptr->warning_fn != NULL) 95 | (*(png_ptr->warning_fn))(png_ptr, warning_message+offset); 96 | else 97 | png_default_warning(png_ptr, warning_message+offset); 98 | } 99 | 100 | /* These utilities are used internally to build an error message that relates 101 | * to the current chunk. The chunk name comes from png_ptr->chunk_name, 102 | * this is used to prefix the message. The message is limited in length 103 | * to 63 bytes, the name characters are output as hex digits wrapped in [] 104 | * if the character is invalid. 105 | */ 106 | #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) 107 | static PNG_CONST char png_digit[16] = { 108 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 109 | 'A', 'B', 'C', 'D', 'E', 'F' 110 | }; 111 | 112 | static void /* PRIVATE */ 113 | png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp 114 | error_message) 115 | { 116 | int iout = 0, iin = 0; 117 | 118 | while (iin < 4) 119 | { 120 | int c = png_ptr->chunk_name[iin++]; 121 | if (isnonalpha(c)) 122 | { 123 | buffer[iout++] = '['; 124 | buffer[iout++] = png_digit[(c & 0xf0) >> 4]; 125 | buffer[iout++] = png_digit[c & 0x0f]; 126 | buffer[iout++] = ']'; 127 | } 128 | else 129 | { 130 | buffer[iout++] = (png_byte)c; 131 | } 132 | } 133 | 134 | if (error_message == NULL) 135 | buffer[iout] = 0; 136 | else 137 | { 138 | buffer[iout++] = ':'; 139 | buffer[iout++] = ' '; 140 | png_strncpy(buffer+iout, error_message, 63); 141 | buffer[iout+63] = 0; 142 | } 143 | } 144 | 145 | void PNGAPI 146 | png_chunk_error(png_structp png_ptr, png_const_charp error_message) 147 | { 148 | char msg[18+64]; 149 | png_format_buffer(png_ptr, msg, error_message); 150 | png_error(png_ptr, msg); 151 | } 152 | 153 | void PNGAPI 154 | png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) 155 | { 156 | char msg[18+64]; 157 | png_format_buffer(png_ptr, msg, warning_message); 158 | png_warning(png_ptr, msg); 159 | } 160 | 161 | /* This is the default error handling function. Note that replacements for 162 | * this function MUST NOT RETURN, or the program will likely crash. This 163 | * function is used by default, or if the program supplies NULL for the 164 | * error function pointer in png_set_error_fn(). 165 | */ 166 | static void /* PRIVATE */ 167 | png_default_error(png_structp png_ptr, png_const_charp error_message) 168 | { 169 | #ifndef PNG_NO_CONSOLE_IO 170 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED 171 | if (*error_message == '#') 172 | { 173 | int offset; 174 | char error_number[16]; 175 | for (offset=0; offset<15; offset++) 176 | { 177 | error_number[offset] = *(error_message+offset+1); 178 | if (*(error_message+offset) == ' ') 179 | break; 180 | } 181 | if((offset > 1) && (offset < 15)) 182 | { 183 | error_number[offset-1]='\0'; 184 | fprintf(stderr, "libpng error no. %s: %s\n", error_number, 185 | error_message+offset); 186 | } 187 | else 188 | fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset); 189 | } 190 | else 191 | #endif 192 | fprintf(stderr, "libpng error: %s\n", error_message); 193 | #endif 194 | 195 | #ifdef PNG_SETJMP_SUPPORTED 196 | # ifdef USE_FAR_KEYWORD 197 | { 198 | jmp_buf jmpbuf; 199 | png_memcpy(jmpbuf,png_ptr->jmpbuf,png_sizeof(jmp_buf)); 200 | longjmp(jmpbuf, 1); 201 | } 202 | # else 203 | longjmp(png_ptr->jmpbuf, 1); 204 | # endif 205 | #else 206 | /* make compiler happy */ ; 207 | if (png_ptr) 208 | PNG_ABORT(); 209 | #endif 210 | #ifdef PNG_NO_CONSOLE_IO 211 | /* make compiler happy */ ; 212 | if (&error_message != NULL) 213 | return; 214 | #endif 215 | } 216 | 217 | /* This function is called when there is a warning, but the library thinks 218 | * it can continue anyway. Replacement functions don't have to do anything 219 | * here if you don't want them to. In the default configuration, png_ptr is 220 | * not used, but it is passed in case it may be useful. 221 | */ 222 | static void /* PRIVATE */ 223 | png_default_warning(png_structp png_ptr, png_const_charp warning_message) 224 | { 225 | #ifndef PNG_NO_CONSOLE_IO 226 | # ifdef PNG_ERROR_NUMBERS_SUPPORTED 227 | if (*warning_message == '#') 228 | { 229 | int offset; 230 | char warning_number[16]; 231 | for (offset=0; offset<15; offset++) 232 | { 233 | warning_number[offset]=*(warning_message+offset+1); 234 | if (*(warning_message+offset) == ' ') 235 | break; 236 | } 237 | if((offset > 1) && (offset < 15)) 238 | { 239 | warning_number[offset-1]='\0'; 240 | fprintf(stderr, "libpng warning no. %s: %s\n", warning_number, 241 | warning_message+offset); 242 | } 243 | else 244 | fprintf(stderr, "libpng warning: %s\n", warning_message); 245 | } 246 | else 247 | # endif 248 | fprintf(stderr, "libpng warning: %s\n", warning_message); 249 | #else 250 | /* make compiler happy */ ; 251 | if (warning_message) 252 | return; 253 | #endif 254 | /* make compiler happy */ ; 255 | if (png_ptr) 256 | return; 257 | } 258 | 259 | /* This function is called when the application wants to use another method 260 | * of handling errors and warnings. Note that the error function MUST NOT 261 | * return to the calling routine or serious problems will occur. The return 262 | * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1) 263 | */ 264 | void PNGAPI 265 | png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, 266 | png_error_ptr error_fn, png_error_ptr warning_fn) 267 | { 268 | png_ptr->error_ptr = error_ptr; 269 | png_ptr->error_fn = error_fn; 270 | png_ptr->warning_fn = warning_fn; 271 | } 272 | 273 | 274 | /* This function returns a pointer to the error_ptr associated with the user 275 | * functions. The application should free any memory associated with this 276 | * pointer before png_write_destroy and png_read_destroy are called. 277 | */ 278 | png_voidp PNGAPI 279 | png_get_error_ptr(png_structp png_ptr) 280 | { 281 | return ((png_voidp)png_ptr->error_ptr); 282 | } 283 | 284 | 285 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED 286 | void PNGAPI 287 | png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) 288 | { 289 | if(png_ptr != NULL) 290 | { 291 | png_ptr->flags &= 292 | ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); 293 | } 294 | } 295 | #endif 296 | -------------------------------------------------------------------------------- /libpng/pngrio.c: -------------------------------------------------------------------------------- 1 | 2 | /* pngrio.c - functions for data input 3 | * 4 | * libpng 1.2.8 - December 3, 2004 5 | * For conditions of distribution and use, see copyright notice in png.h 6 | * Copyright (c) 1998-2004 Glenn Randers-Pehrson 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 9 | * 10 | * This file provides a location for all input. Users who need 11 | * special handling are expected to write a function that has the same 12 | * arguments as this and performs a similar function, but that possibly 13 | * has a different input method. Note that you shouldn't change this 14 | * function, but rather write a replacement function and then make 15 | * libpng use it at run time with png_set_read_fn(...). 16 | */ 17 | 18 | #define PNG_INTERNAL 19 | #include "png.h" 20 | 21 | /* Read the data from whatever input you are using. The default routine 22 | reads from a file pointer. Note that this routine sometimes gets called 23 | with very small lengths, so you should implement some kind of simple 24 | buffering if you are using unbuffered reads. This should never be asked 25 | to read more then 64K on a 16 bit machine. */ 26 | void /* PRIVATE */ 27 | png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) 28 | { 29 | png_debug1(4,"reading %d bytes\n", (int)length); 30 | if (png_ptr->read_data_fn != NULL) 31 | (*(png_ptr->read_data_fn))(png_ptr, data, length); 32 | else 33 | png_error(png_ptr, "Call to NULL read function"); 34 | } 35 | 36 | #if !defined(PNG_NO_STDIO) 37 | /* This is the function that does the actual reading of data. If you are 38 | not reading from a standard C stream, you should create a replacement 39 | read_data function and use it at run time with png_set_read_fn(), rather 40 | than changing the library. */ 41 | #ifndef USE_FAR_KEYWORD 42 | void PNGAPI 43 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) 44 | { 45 | png_size_t check; 46 | 47 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t 48 | * instead of an int, which is what fread() actually returns. 49 | */ 50 | #if defined(_WIN32_WCE) 51 | if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) 52 | check = 0; 53 | #else 54 | check = (png_size_t)fread(data, (png_size_t)1, length, 55 | (png_FILE_p)png_ptr->io_ptr); 56 | #endif 57 | 58 | if (check != length) 59 | png_error(png_ptr, "Read Error"); 60 | } 61 | #else 62 | /* this is the model-independent version. Since the standard I/O library 63 | can't handle far buffers in the medium and small models, we have to copy 64 | the data. 65 | */ 66 | 67 | #define NEAR_BUF_SIZE 1024 68 | #define MIN(a,b) (a <= b ? a : b) 69 | 70 | static void /* PRIVATE */ 71 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) 72 | { 73 | int check; 74 | png_byte *n_data; 75 | png_FILE_p io_ptr; 76 | 77 | /* Check if data really is near. If so, use usual code. */ 78 | n_data = (png_byte *)CVT_PTR_NOCHECK(data); 79 | io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); 80 | if ((png_bytep)n_data == data) 81 | { 82 | #if defined(_WIN32_WCE) 83 | if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) 84 | check = 0; 85 | #else 86 | check = fread(n_data, 1, length, io_ptr); 87 | #endif 88 | } 89 | else 90 | { 91 | png_byte buf[NEAR_BUF_SIZE]; 92 | png_size_t read, remaining, err; 93 | check = 0; 94 | remaining = length; 95 | do 96 | { 97 | read = MIN(NEAR_BUF_SIZE, remaining); 98 | #if defined(_WIN32_WCE) 99 | if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) ) 100 | err = 0; 101 | #else 102 | err = fread(buf, (png_size_t)1, read, io_ptr); 103 | #endif 104 | png_memcpy(data, buf, read); /* copy far buffer to near buffer */ 105 | if(err != read) 106 | break; 107 | else 108 | check += err; 109 | data += read; 110 | remaining -= read; 111 | } 112 | while (remaining != 0); 113 | } 114 | if ((png_uint_32)check != (png_uint_32)length) 115 | png_error(png_ptr, "read Error"); 116 | } 117 | #endif 118 | #endif 119 | 120 | /* This function allows the application to supply a new input function 121 | for libpng if standard C streams aren't being used. 122 | 123 | This function takes as its arguments: 124 | png_ptr - pointer to a png input data structure 125 | io_ptr - pointer to user supplied structure containing info about 126 | the input functions. May be NULL. 127 | read_data_fn - pointer to a new input function that takes as its 128 | arguments a pointer to a png_struct, a pointer to 129 | a location where input data can be stored, and a 32-bit 130 | unsigned int that is the number of bytes to be read. 131 | To exit and output any fatal error messages the new write 132 | function should call png_error(png_ptr, "Error msg"). */ 133 | void PNGAPI 134 | png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, 135 | png_rw_ptr read_data_fn) 136 | { 137 | png_ptr->io_ptr = io_ptr; 138 | 139 | #if !defined(PNG_NO_STDIO) 140 | if (read_data_fn != NULL) 141 | png_ptr->read_data_fn = read_data_fn; 142 | else 143 | png_ptr->read_data_fn = png_default_read_data; 144 | #else 145 | png_ptr->read_data_fn = read_data_fn; 146 | #endif 147 | 148 | /* It is an error to write to a read device */ 149 | if (png_ptr->write_data_fn != NULL) 150 | { 151 | png_ptr->write_data_fn = NULL; 152 | png_warning(png_ptr, 153 | "It's an error to set both read_data_fn and write_data_fn in the "); 154 | png_warning(png_ptr, 155 | "same structure. Resetting write_data_fn to NULL."); 156 | } 157 | 158 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) 159 | png_ptr->output_flush_fn = NULL; 160 | #endif 161 | } 162 | -------------------------------------------------------------------------------- /libpng/pngwio.c: -------------------------------------------------------------------------------- 1 | 2 | /* pngwio.c - functions for data output 3 | * 4 | * libpng 1.2.8 - December 3, 2004 5 | * For conditions of distribution and use, see copyright notice in png.h 6 | * Copyright (c) 1998-2004 Glenn Randers-Pehrson 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 9 | * 10 | * This file provides a location for all output. Users who need 11 | * special handling are expected to write functions that have the same 12 | * arguments as these and perform similar functions, but that possibly 13 | * use different output methods. Note that you shouldn't change these 14 | * functions, but rather write replacement functions and then change 15 | * them at run time with png_set_write_fn(...). 16 | */ 17 | 18 | #define PNG_INTERNAL 19 | #include "png.h" 20 | #ifdef PNG_WRITE_SUPPORTED 21 | 22 | /* Write the data to whatever output you are using. The default routine 23 | writes to a file pointer. Note that this routine sometimes gets called 24 | with very small lengths, so you should implement some kind of simple 25 | buffering if you are using unbuffered writes. This should never be asked 26 | to write more than 64K on a 16 bit machine. */ 27 | 28 | void /* PRIVATE */ 29 | png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) 30 | { 31 | if (png_ptr->write_data_fn != NULL ) 32 | (*(png_ptr->write_data_fn))(png_ptr, data, length); 33 | else 34 | png_error(png_ptr, "Call to NULL write function"); 35 | } 36 | 37 | #if !defined(PNG_NO_STDIO) 38 | /* This is the function that does the actual writing of data. If you are 39 | not writing to a standard C stream, you should create a replacement 40 | write_data function and use it at run time with png_set_write_fn(), rather 41 | than changing the library. */ 42 | #ifndef USE_FAR_KEYWORD 43 | void PNGAPI 44 | png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) 45 | { 46 | png_uint_32 check; 47 | 48 | #if defined(_WIN32_WCE) 49 | if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) 50 | check = 0; 51 | #else 52 | check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); 53 | #endif 54 | if (check != length) 55 | png_error(png_ptr, "Write Error"); 56 | } 57 | #else 58 | /* this is the model-independent version. Since the standard I/O library 59 | can't handle far buffers in the medium and small models, we have to copy 60 | the data. 61 | */ 62 | 63 | #define NEAR_BUF_SIZE 1024 64 | #define MIN(a,b) (a <= b ? a : b) 65 | 66 | void PNGAPI 67 | png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) 68 | { 69 | png_uint_32 check; 70 | png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ 71 | png_FILE_p io_ptr; 72 | 73 | /* Check if data really is near. If so, use usual code. */ 74 | near_data = (png_byte *)CVT_PTR_NOCHECK(data); 75 | io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); 76 | if ((png_bytep)near_data == data) 77 | { 78 | #if defined(_WIN32_WCE) 79 | if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) 80 | check = 0; 81 | #else 82 | check = fwrite(near_data, 1, length, io_ptr); 83 | #endif 84 | } 85 | else 86 | { 87 | png_byte buf[NEAR_BUF_SIZE]; 88 | png_size_t written, remaining, err; 89 | check = 0; 90 | remaining = length; 91 | do 92 | { 93 | written = MIN(NEAR_BUF_SIZE, remaining); 94 | png_memcpy(buf, data, written); /* copy far buffer to near buffer */ 95 | #if defined(_WIN32_WCE) 96 | if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) 97 | err = 0; 98 | #else 99 | err = fwrite(buf, 1, written, io_ptr); 100 | #endif 101 | if (err != written) 102 | break; 103 | else 104 | check += err; 105 | data += written; 106 | remaining -= written; 107 | } 108 | while (remaining != 0); 109 | } 110 | if (check != length) 111 | png_error(png_ptr, "Write Error"); 112 | } 113 | 114 | #endif 115 | #endif 116 | 117 | /* This function is called to output any data pending writing (normally 118 | to disk). After png_flush is called, there should be no data pending 119 | writing in any buffers. */ 120 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) 121 | void /* PRIVATE */ 122 | png_flush(png_structp png_ptr) 123 | { 124 | if (png_ptr->output_flush_fn != NULL) 125 | (*(png_ptr->output_flush_fn))(png_ptr); 126 | } 127 | 128 | #if !defined(PNG_NO_STDIO) 129 | void PNGAPI 130 | png_default_flush(png_structp png_ptr) 131 | { 132 | #if !defined(_WIN32_WCE) 133 | png_FILE_p io_ptr; 134 | io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); 135 | if (io_ptr != NULL) 136 | fflush(io_ptr); 137 | #endif 138 | } 139 | #endif 140 | #endif 141 | 142 | /* This function allows the application to supply new output functions for 143 | libpng if standard C streams aren't being used. 144 | 145 | This function takes as its arguments: 146 | png_ptr - pointer to a png output data structure 147 | io_ptr - pointer to user supplied structure containing info about 148 | the output functions. May be NULL. 149 | write_data_fn - pointer to a new output function that takes as its 150 | arguments a pointer to a png_struct, a pointer to 151 | data to be written, and a 32-bit unsigned int that is 152 | the number of bytes to be written. The new write 153 | function should call png_error(png_ptr, "Error msg") 154 | to exit and output any fatal error messages. 155 | flush_data_fn - pointer to a new flush function that takes as its 156 | arguments a pointer to a png_struct. After a call to 157 | the flush function, there should be no data in any buffers 158 | or pending transmission. If the output method doesn't do 159 | any buffering of ouput, a function prototype must still be 160 | supplied although it doesn't have to do anything. If 161 | PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile 162 | time, output_flush_fn will be ignored, although it must be 163 | supplied for compatibility. */ 164 | void PNGAPI 165 | png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, 166 | png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) 167 | { 168 | png_ptr->io_ptr = io_ptr; 169 | 170 | #if !defined(PNG_NO_STDIO) 171 | if (write_data_fn != NULL) 172 | png_ptr->write_data_fn = write_data_fn; 173 | else 174 | png_ptr->write_data_fn = png_default_write_data; 175 | #else 176 | png_ptr->write_data_fn = write_data_fn; 177 | #endif 178 | 179 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) 180 | #if !defined(PNG_NO_STDIO) 181 | if (output_flush_fn != NULL) 182 | png_ptr->output_flush_fn = output_flush_fn; 183 | else 184 | png_ptr->output_flush_fn = png_default_flush; 185 | #else 186 | png_ptr->output_flush_fn = output_flush_fn; 187 | #endif 188 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ 189 | 190 | /* It is an error to read while writing a png file */ 191 | if (png_ptr->read_data_fn != NULL) 192 | { 193 | png_ptr->read_data_fn = NULL; 194 | png_warning(png_ptr, 195 | "Attempted to set both read_data_fn and write_data_fn in"); 196 | png_warning(png_ptr, 197 | "the same structure. Resetting read_data_fn to NULL."); 198 | } 199 | } 200 | 201 | #if defined(USE_FAR_KEYWORD) 202 | #if defined(_MSC_VER) 203 | void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check) 204 | { 205 | void *near_ptr; 206 | void FAR *far_ptr; 207 | FP_OFF(near_ptr) = FP_OFF(ptr); 208 | far_ptr = (void FAR *)near_ptr; 209 | if(check != 0) 210 | if(FP_SEG(ptr) != FP_SEG(far_ptr)) 211 | png_error(png_ptr,"segment lost in conversion"); 212 | return(near_ptr); 213 | } 214 | # else 215 | void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check) 216 | { 217 | void *near_ptr; 218 | void FAR *far_ptr; 219 | near_ptr = (void FAR *)ptr; 220 | far_ptr = (void FAR *)near_ptr; 221 | if(check != 0) 222 | if(far_ptr != ptr) 223 | png_error(png_ptr,"segment lost in conversion"); 224 | return(near_ptr); 225 | } 226 | # endif 227 | # endif 228 | #endif /* PNG_WRITE_SUPPORTED */ 229 | -------------------------------------------------------------------------------- /libpng/zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2004 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | #define BASE 65521UL /* largest prime smaller than 65536 */ 12 | #define NMAX 5552 13 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 14 | 15 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 16 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 17 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 18 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 19 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 20 | 21 | /* use NO_DIVIDE if your processor does not do division in hardware */ 22 | #ifdef NO_DIVIDE 23 | # define MOD(a) \ 24 | do { \ 25 | if (a >= (BASE << 16)) a -= (BASE << 16); \ 26 | if (a >= (BASE << 15)) a -= (BASE << 15); \ 27 | if (a >= (BASE << 14)) a -= (BASE << 14); \ 28 | if (a >= (BASE << 13)) a -= (BASE << 13); \ 29 | if (a >= (BASE << 12)) a -= (BASE << 12); \ 30 | if (a >= (BASE << 11)) a -= (BASE << 11); \ 31 | if (a >= (BASE << 10)) a -= (BASE << 10); \ 32 | if (a >= (BASE << 9)) a -= (BASE << 9); \ 33 | if (a >= (BASE << 8)) a -= (BASE << 8); \ 34 | if (a >= (BASE << 7)) a -= (BASE << 7); \ 35 | if (a >= (BASE << 6)) a -= (BASE << 6); \ 36 | if (a >= (BASE << 5)) a -= (BASE << 5); \ 37 | if (a >= (BASE << 4)) a -= (BASE << 4); \ 38 | if (a >= (BASE << 3)) a -= (BASE << 3); \ 39 | if (a >= (BASE << 2)) a -= (BASE << 2); \ 40 | if (a >= (BASE << 1)) a -= (BASE << 1); \ 41 | if (a >= BASE) a -= BASE; \ 42 | } while (0) 43 | # define MOD4(a) \ 44 | do { \ 45 | if (a >= (BASE << 4)) a -= (BASE << 4); \ 46 | if (a >= (BASE << 3)) a -= (BASE << 3); \ 47 | if (a >= (BASE << 2)) a -= (BASE << 2); \ 48 | if (a >= (BASE << 1)) a -= (BASE << 1); \ 49 | if (a >= BASE) a -= BASE; \ 50 | } while (0) 51 | #else 52 | # define MOD(a) a %= BASE 53 | # define MOD4(a) a %= BASE 54 | #endif 55 | 56 | /* ========================================================================= */ 57 | uLong ZEXPORT adler32(adler, buf, len) 58 | uLong adler; 59 | const Bytef *buf; 60 | uInt len; 61 | { 62 | unsigned long sum2; 63 | unsigned n; 64 | 65 | /* split Adler-32 into component sums */ 66 | sum2 = (adler >> 16) & 0xffff; 67 | adler &= 0xffff; 68 | 69 | /* in case user likes doing a byte at a time, keep it fast */ 70 | if (len == 1) { 71 | adler += buf[0]; 72 | if (adler >= BASE) 73 | adler -= BASE; 74 | sum2 += adler; 75 | if (sum2 >= BASE) 76 | sum2 -= BASE; 77 | return adler | (sum2 << 16); 78 | } 79 | 80 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 81 | if (buf == Z_NULL) 82 | return 1L; 83 | 84 | /* in case short lengths are provided, keep it somewhat fast */ 85 | if (len < 16) { 86 | while (len--) { 87 | adler += *buf++; 88 | sum2 += adler; 89 | } 90 | if (adler >= BASE) 91 | adler -= BASE; 92 | MOD4(sum2); /* only added so many BASE's */ 93 | return adler | (sum2 << 16); 94 | } 95 | 96 | /* do length NMAX blocks -- requires just one modulo operation */ 97 | while (len >= NMAX) { 98 | len -= NMAX; 99 | n = NMAX / 16; /* NMAX is divisible by 16 */ 100 | do { 101 | DO16(buf); /* 16 sums unrolled */ 102 | buf += 16; 103 | } while (--n); 104 | MOD(adler); 105 | MOD(sum2); 106 | } 107 | 108 | /* do remaining bytes (less than NMAX, still just one modulo) */ 109 | if (len) { /* avoid modulos if none remaining */ 110 | while (len >= 16) { 111 | len -= 16; 112 | DO16(buf); 113 | buf += 16; 114 | } 115 | while (len--) { 116 | adler += *buf++; 117 | sum2 += adler; 118 | } 119 | MOD(adler); 120 | MOD(sum2); 121 | } 122 | 123 | /* return recombined sums */ 124 | return adler | (sum2 << 16); 125 | } 126 | 127 | /* ========================================================================= */ 128 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 129 | uLong adler1; 130 | uLong adler2; 131 | z_off_t len2; 132 | { 133 | unsigned long sum1; 134 | unsigned long sum2; 135 | unsigned rem; 136 | 137 | /* the derivation of this formula is left as an exercise for the reader */ 138 | rem = (unsigned)(len2 % BASE); 139 | sum1 = adler1 & 0xffff; 140 | sum2 = rem * sum1; 141 | MOD(sum2); 142 | sum1 += (adler2 & 0xffff) + BASE - 1; 143 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 144 | if (sum1 > BASE) sum1 -= BASE; 145 | if (sum1 > BASE) sum1 -= BASE; 146 | if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); 147 | if (sum2 > BASE) sum2 -= BASE; 148 | return sum1 | (sum2 << 16); 149 | } 150 | -------------------------------------------------------------------------------- /libpng/zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2003 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 23 | Bytef *dest; 24 | uLongf *destLen; 25 | const Bytef *source; 26 | uLong sourceLen; 27 | int level; 28 | { 29 | z_stream stream; 30 | int err; 31 | 32 | stream.next_in = (Bytef*)source; 33 | stream.avail_in = (uInt)sourceLen; 34 | #ifdef MAXSEG_64K 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | #endif 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | stream.opaque = (voidpf)0; 45 | 46 | err = deflateInit(&stream, level); 47 | if (err != Z_OK) return err; 48 | 49 | err = deflate(&stream, Z_FINISH); 50 | if (err != Z_STREAM_END) { 51 | deflateEnd(&stream); 52 | return err == Z_OK ? Z_BUF_ERROR : err; 53 | } 54 | *destLen = stream.total_out; 55 | 56 | err = deflateEnd(&stream); 57 | return err; 58 | } 59 | 60 | /* =========================================================================== 61 | */ 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) 63 | Bytef *dest; 64 | uLongf *destLen; 65 | const Bytef *source; 66 | uLong sourceLen; 67 | { 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 69 | } 70 | 71 | /* =========================================================================== 72 | If the default memLevel or windowBits for deflateInit() is changed, then 73 | this function needs to be updated. 74 | */ 75 | uLong ZEXPORT compressBound (sourceLen) 76 | uLong sourceLen; 77 | { 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; 79 | } 80 | -------------------------------------------------------------------------------- /libpng/zlib/deflate.h: -------------------------------------------------------------------------------- 1 | /* deflate.h -- internal compression state 2 | * Copyright (C) 1995-2004 Jean-loup Gailly 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef DEFLATE_H 14 | #define DEFLATE_H 15 | 16 | #include "zutil.h" 17 | 18 | /* define NO_GZIP when compiling if you want to disable gzip header and 19 | trailer creation by deflate(). NO_GZIP would be used to avoid linking in 20 | the crc code when it is not needed. For shared libraries, gzip encoding 21 | should be left enabled. */ 22 | #ifndef NO_GZIP 23 | # define GZIP 24 | #endif 25 | 26 | /* =========================================================================== 27 | * Internal compression state. 28 | */ 29 | 30 | #define LENGTH_CODES 29 31 | /* number of length codes, not counting the special END_BLOCK code */ 32 | 33 | #define LITERALS 256 34 | /* number of literal bytes 0..255 */ 35 | 36 | #define L_CODES (LITERALS+1+LENGTH_CODES) 37 | /* number of Literal or Length codes, including the END_BLOCK code */ 38 | 39 | #define D_CODES 30 40 | /* number of distance codes */ 41 | 42 | #define BL_CODES 19 43 | /* number of codes used to transfer the bit lengths */ 44 | 45 | #define HEAP_SIZE (2*L_CODES+1) 46 | /* maximum heap size */ 47 | 48 | #define MAX_BITS 15 49 | /* All codes must not exceed MAX_BITS bits */ 50 | 51 | #define INIT_STATE 42 52 | #define EXTRA_STATE 69 53 | #define NAME_STATE 73 54 | #define COMMENT_STATE 91 55 | #define HCRC_STATE 103 56 | #define BUSY_STATE 113 57 | #define FINISH_STATE 666 58 | /* Stream status */ 59 | 60 | 61 | /* Data structure describing a single value and its code string. */ 62 | typedef struct ct_data_s { 63 | union { 64 | ush freq; /* frequency count */ 65 | ush code; /* bit string */ 66 | } fc; 67 | union { 68 | ush dad; /* father node in Huffman tree */ 69 | ush len; /* length of bit string */ 70 | } dl; 71 | } FAR ct_data; 72 | 73 | #define Freq fc.freq 74 | #define Code fc.code 75 | #define Dad dl.dad 76 | #define Len dl.len 77 | 78 | typedef struct static_tree_desc_s static_tree_desc; 79 | 80 | typedef struct tree_desc_s { 81 | ct_data *dyn_tree; /* the dynamic tree */ 82 | int max_code; /* largest code with non zero frequency */ 83 | static_tree_desc *stat_desc; /* the corresponding static tree */ 84 | } FAR tree_desc; 85 | 86 | typedef ush Pos; 87 | typedef Pos FAR Posf; 88 | typedef unsigned IPos; 89 | 90 | /* A Pos is an index in the character window. We use short instead of int to 91 | * save space in the various tables. IPos is used only for parameter passing. 92 | */ 93 | 94 | typedef struct internal_state { 95 | z_streamp strm; /* pointer back to this zlib stream */ 96 | int status; /* as the name implies */ 97 | Bytef *pending_buf; /* output still pending */ 98 | ulg pending_buf_size; /* size of pending_buf */ 99 | Bytef *pending_out; /* next pending byte to output to the stream */ 100 | uInt pending; /* nb of bytes in the pending buffer */ 101 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 102 | gz_headerp gzhead; /* gzip header information to write */ 103 | uInt gzindex; /* where in extra, name, or comment */ 104 | Byte method; /* STORED (for zip only) or DEFLATED */ 105 | int last_flush; /* value of flush param for previous deflate call */ 106 | 107 | /* used by deflate.c: */ 108 | 109 | uInt w_size; /* LZ77 window size (32K by default) */ 110 | uInt w_bits; /* log2(w_size) (8..16) */ 111 | uInt w_mask; /* w_size - 1 */ 112 | 113 | Bytef *window; 114 | /* Sliding window. Input bytes are read into the second half of the window, 115 | * and move to the first half later to keep a dictionary of at least wSize 116 | * bytes. With this organization, matches are limited to a distance of 117 | * wSize-MAX_MATCH bytes, but this ensures that IO is always 118 | * performed with a length multiple of the block size. Also, it limits 119 | * the window size to 64K, which is quite useful on MSDOS. 120 | * To do: use the user input buffer as sliding window. 121 | */ 122 | 123 | ulg window_size; 124 | /* Actual size of window: 2*wSize, except when the user input buffer 125 | * is directly used as sliding window. 126 | */ 127 | 128 | Posf *prev; 129 | /* Link to older string with same hash index. To limit the size of this 130 | * array to 64K, this link is maintained only for the last 32K strings. 131 | * An index in this array is thus a window index modulo 32K. 132 | */ 133 | 134 | Posf *head; /* Heads of the hash chains or NIL. */ 135 | 136 | uInt ins_h; /* hash index of string to be inserted */ 137 | uInt hash_size; /* number of elements in hash table */ 138 | uInt hash_bits; /* log2(hash_size) */ 139 | uInt hash_mask; /* hash_size-1 */ 140 | 141 | uInt hash_shift; 142 | /* Number of bits by which ins_h must be shifted at each input 143 | * step. It must be such that after MIN_MATCH steps, the oldest 144 | * byte no longer takes part in the hash key, that is: 145 | * hash_shift * MIN_MATCH >= hash_bits 146 | */ 147 | 148 | long block_start; 149 | /* Window position at the beginning of the current output block. Gets 150 | * negative when the window is moved backwards. 151 | */ 152 | 153 | uInt match_length; /* length of best match */ 154 | IPos prev_match; /* previous match */ 155 | int match_available; /* set if previous match exists */ 156 | uInt strstart; /* start of string to insert */ 157 | uInt match_start; /* start of matching string */ 158 | uInt lookahead; /* number of valid bytes ahead in window */ 159 | 160 | uInt prev_length; 161 | /* Length of the best match at previous step. Matches not greater than this 162 | * are discarded. This is used in the lazy match evaluation. 163 | */ 164 | 165 | uInt max_chain_length; 166 | /* To speed up deflation, hash chains are never searched beyond this 167 | * length. A higher limit improves compression ratio but degrades the 168 | * speed. 169 | */ 170 | 171 | uInt max_lazy_match; 172 | /* Attempt to find a better match only when the current match is strictly 173 | * smaller than this value. This mechanism is used only for compression 174 | * levels >= 4. 175 | */ 176 | # define max_insert_length max_lazy_match 177 | /* Insert new strings in the hash table only if the match length is not 178 | * greater than this length. This saves time but degrades compression. 179 | * max_insert_length is used only for compression levels <= 3. 180 | */ 181 | 182 | int level; /* compression level (1..9) */ 183 | int strategy; /* favor or force Huffman coding*/ 184 | 185 | uInt good_match; 186 | /* Use a faster search when the previous match is longer than this */ 187 | 188 | int nice_match; /* Stop searching when current match exceeds this */ 189 | 190 | /* used by trees.c: */ 191 | /* Didn't use ct_data typedef below to supress compiler warning */ 192 | struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 193 | struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 194 | struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 195 | 196 | struct tree_desc_s l_desc; /* desc. for literal tree */ 197 | struct tree_desc_s d_desc; /* desc. for distance tree */ 198 | struct tree_desc_s bl_desc; /* desc. for bit length tree */ 199 | 200 | ush bl_count[MAX_BITS+1]; 201 | /* number of codes at each bit length for an optimal tree */ 202 | 203 | int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 204 | int heap_len; /* number of elements in the heap */ 205 | int heap_max; /* element of largest frequency */ 206 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 207 | * The same heap array is used to build all trees. 208 | */ 209 | 210 | uch depth[2*L_CODES+1]; 211 | /* Depth of each subtree used as tie breaker for trees of equal frequency 212 | */ 213 | 214 | uchf *l_buf; /* buffer for literals or lengths */ 215 | 216 | uInt lit_bufsize; 217 | /* Size of match buffer for literals/lengths. There are 4 reasons for 218 | * limiting lit_bufsize to 64K: 219 | * - frequencies can be kept in 16 bit counters 220 | * - if compression is not successful for the first block, all input 221 | * data is still in the window so we can still emit a stored block even 222 | * when input comes from standard input. (This can also be done for 223 | * all blocks if lit_bufsize is not greater than 32K.) 224 | * - if compression is not successful for a file smaller than 64K, we can 225 | * even emit a stored file instead of a stored block (saving 5 bytes). 226 | * This is applicable only for zip (not gzip or zlib). 227 | * - creating new Huffman trees less frequently may not provide fast 228 | * adaptation to changes in the input data statistics. (Take for 229 | * example a binary file with poorly compressible code followed by 230 | * a highly compressible string table.) Smaller buffer sizes give 231 | * fast adaptation but have of course the overhead of transmitting 232 | * trees more frequently. 233 | * - I can't count above 4 234 | */ 235 | 236 | uInt last_lit; /* running index in l_buf */ 237 | 238 | ushf *d_buf; 239 | /* Buffer for distances. To simplify the code, d_buf and l_buf have 240 | * the same number of elements. To use different lengths, an extra flag 241 | * array would be necessary. 242 | */ 243 | 244 | ulg opt_len; /* bit length of current block with optimal trees */ 245 | ulg static_len; /* bit length of current block with static trees */ 246 | uInt matches; /* number of string matches in current block */ 247 | int last_eob_len; /* bit length of EOB code for last block */ 248 | 249 | #ifdef DEBUG 250 | ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 251 | ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 252 | #endif 253 | 254 | ush bi_buf; 255 | /* Output buffer. bits are inserted starting at the bottom (least 256 | * significant bits). 257 | */ 258 | int bi_valid; 259 | /* Number of valid bits in bi_buf. All bits above the last valid bit 260 | * are always zero. 261 | */ 262 | 263 | } FAR deflate_state; 264 | 265 | /* Output a byte on the stream. 266 | * IN assertion: there is enough room in pending_buf. 267 | */ 268 | #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} 269 | 270 | 271 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 272 | /* Minimum amount of lookahead, except at the end of the input file. 273 | * See deflate.c for comments about the MIN_MATCH+1. 274 | */ 275 | 276 | #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 277 | /* In order to simplify the code, particularly on 16 bit machines, match 278 | * distances are limited to MAX_DIST instead of WSIZE. 279 | */ 280 | 281 | /* in trees.c */ 282 | void _tr_init OF((deflate_state *s)); 283 | int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); 284 | void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, 285 | int eof)); 286 | void _tr_align OF((deflate_state *s)); 287 | void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, 288 | int eof)); 289 | 290 | #define d_code(dist) \ 291 | ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 292 | /* Mapping from a distance to a distance code. dist is the distance - 1 and 293 | * must not have side effects. _dist_code[256] and _dist_code[257] are never 294 | * used. 295 | */ 296 | 297 | #ifndef DEBUG 298 | /* Inline versions of _tr_tally for speed: */ 299 | 300 | #if defined(GEN_TREES_H) || !defined(STDC) 301 | extern uch _length_code[]; 302 | extern uch _dist_code[]; 303 | #else 304 | extern const uch _length_code[]; 305 | extern const uch _dist_code[]; 306 | #endif 307 | 308 | # define _tr_tally_lit(s, c, flush) \ 309 | { uch cc = (c); \ 310 | s->d_buf[s->last_lit] = 0; \ 311 | s->l_buf[s->last_lit++] = cc; \ 312 | s->dyn_ltree[cc].Freq++; \ 313 | flush = (s->last_lit == s->lit_bufsize-1); \ 314 | } 315 | # define _tr_tally_dist(s, distance, length, flush) \ 316 | { uch len = (length); \ 317 | ush dist = (distance); \ 318 | s->d_buf[s->last_lit] = dist; \ 319 | s->l_buf[s->last_lit++] = len; \ 320 | dist--; \ 321 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 322 | s->dyn_dtree[d_code(dist)].Freq++; \ 323 | flush = (s->last_lit == s->lit_bufsize-1); \ 324 | } 325 | #else 326 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 327 | # define _tr_tally_dist(s, distance, length, flush) \ 328 | flush = _tr_tally(s, distance, length) 329 | #endif 330 | 331 | #endif /* DEFLATE_H */ 332 | -------------------------------------------------------------------------------- /libpng/zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /libpng/zlib/inffixed.h: -------------------------------------------------------------------------------- 1 | /* inffixed.h -- table for decoding fixed codes 2 | * Generated automatically by makefixed(). 3 | */ 4 | 5 | /* WARNING: this file should *not* be used by applications. It 6 | is part of the implementation of the compression library and 7 | is subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /libpng/zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2004 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY, /* i/o: waiting for input or output to copy stored block */ 36 | TABLE, /* i: waiting for dynamic block table lengths */ 37 | LENLENS, /* i: waiting for code length code lengths */ 38 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 39 | LEN, /* i: waiting for length/lit code */ 40 | LENEXT, /* i: waiting for length extra bits */ 41 | DIST, /* i: waiting for distance code */ 42 | DISTEXT, /* i: waiting for distance extra bits */ 43 | MATCH, /* o: waiting for output space to copy string */ 44 | LIT, /* o: waiting for output space to write literal */ 45 | CHECK, /* i: waiting for 32-bit check value */ 46 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 47 | DONE, /* finished check, done -- remain here until reset */ 48 | BAD, /* got a data error -- remain here until reset */ 49 | MEM, /* got an inflate() memory error -- remain here until reset */ 50 | SYNC /* looking for synchronization bytes to restart inflate() */ 51 | } inflate_mode; 52 | 53 | /* 54 | State transitions between above modes - 55 | 56 | (most modes can go to the BAD or MEM mode -- not shown for clarity) 57 | 58 | Process header: 59 | HEAD -> (gzip) or (zlib) 60 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME 61 | NAME -> COMMENT -> HCRC -> TYPE 62 | (zlib) -> DICTID or TYPE 63 | DICTID -> DICT -> TYPE 64 | Read deflate blocks: 65 | TYPE -> STORED or TABLE or LEN or CHECK 66 | STORED -> COPY -> TYPE 67 | TABLE -> LENLENS -> CODELENS -> LEN 68 | Read deflate codes: 69 | LEN -> LENEXT or LIT or TYPE 70 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 71 | LIT -> LEN 72 | Process trailer: 73 | CHECK -> LENGTH -> DONE 74 | */ 75 | 76 | /* state maintained between inflate() calls. Approximately 7K bytes. */ 77 | struct inflate_state { 78 | inflate_mode mode; /* current inflate mode */ 79 | int last; /* true if processing last block */ 80 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 81 | int havedict; /* true if dictionary provided */ 82 | int flags; /* gzip header method and flags (0 if zlib) */ 83 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 84 | unsigned long check; /* protected copy of check value */ 85 | unsigned long total; /* protected copy of output count */ 86 | gz_headerp head; /* where to save gzip header information */ 87 | /* sliding window */ 88 | unsigned wbits; /* log base 2 of requested window size */ 89 | unsigned wsize; /* window size or zero if not using window */ 90 | unsigned whave; /* valid bytes in the window */ 91 | unsigned write; /* window write index */ 92 | unsigned char FAR *window; /* allocated sliding window, if needed */ 93 | /* bit accumulator */ 94 | unsigned long hold; /* input bit accumulator */ 95 | unsigned bits; /* number of bits in "in" */ 96 | /* for string and stored block copying */ 97 | unsigned length; /* literal or length of data to copy */ 98 | unsigned offset; /* distance back to copy string from */ 99 | /* for table and code decoding */ 100 | unsigned extra; /* extra bits needed */ 101 | /* fixed and dynamic code tables */ 102 | code const FAR *lencode; /* starting table for length/literal codes */ 103 | code const FAR *distcode; /* starting table for distance codes */ 104 | unsigned lenbits; /* index bits for lencode */ 105 | unsigned distbits; /* index bits for distcode */ 106 | /* dynamic table building */ 107 | unsigned ncode; /* number of code length code lengths */ 108 | unsigned nlen; /* number of length code lengths */ 109 | unsigned ndist; /* number of distance code lengths */ 110 | unsigned have; /* number of code lengths in lens[] */ 111 | code FAR *next; /* next available space in codes[] */ 112 | unsigned short lens[320]; /* temporary storage for code lengths */ 113 | unsigned short work[288]; /* work area for code table building */ 114 | code codes[ENOUGH]; /* space for code tables */ 115 | }; 116 | -------------------------------------------------------------------------------- /libpng/zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of dynamic tree. The maximum found in a long but non- 39 | exhaustive search was 1444 code structures (852 for length/literals 40 | and 592 for distances, the latter actually the result of an 41 | exhaustive search). The true maximum is not known, but the value 42 | below is more than safe. */ 43 | #define ENOUGH 2048 44 | #define MAXD 592 45 | 46 | /* Type of code to build for inftable() */ 47 | typedef enum { 48 | CODES, 49 | LENS, 50 | DISTS 51 | } codetype; 52 | 53 | extern int inflate_table OF((codetype type, unsigned short FAR *lens, 54 | unsigned codes, code FAR * FAR *table, 55 | unsigned FAR *bits, unsigned short FAR *work)); 56 | -------------------------------------------------------------------------------- /libpng/zlib/trees.h: -------------------------------------------------------------------------------- 1 | /* header created automatically with -DGEN_TREES_H */ 2 | 3 | local const ct_data static_ltree[L_CODES+2] = { 4 | {{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, 5 | {{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, 6 | {{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, 7 | {{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, 8 | {{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, 9 | {{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, 10 | {{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, 11 | {{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, 12 | {{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, 13 | {{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, 14 | {{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, 15 | {{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, 16 | {{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, 17 | {{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, 18 | {{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, 19 | {{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, 20 | {{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, 21 | {{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, 22 | {{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, 23 | {{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, 24 | {{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, 25 | {{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, 26 | {{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, 27 | {{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, 28 | {{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, 29 | {{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, 30 | {{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, 31 | {{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, 32 | {{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, 33 | {{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, 34 | {{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, 35 | {{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, 36 | {{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, 37 | {{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, 38 | {{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, 39 | {{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, 40 | {{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, 41 | {{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, 42 | {{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, 43 | {{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, 44 | {{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, 45 | {{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, 46 | {{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, 47 | {{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, 48 | {{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, 49 | {{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, 50 | {{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, 51 | {{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, 52 | {{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, 53 | {{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, 54 | {{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, 55 | {{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, 56 | {{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, 57 | {{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, 58 | {{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, 59 | {{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, 60 | {{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, 61 | {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} 62 | }; 63 | 64 | local const ct_data static_dtree[D_CODES] = { 65 | {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, 66 | {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, 67 | {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, 68 | {{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, 69 | {{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, 70 | {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} 71 | }; 72 | 73 | const uch _dist_code[DIST_CODE_LEN] = { 74 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 75 | 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 76 | 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 77 | 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 78 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 79 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 80 | 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 81 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 82 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 83 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 84 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 85 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 86 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 87 | 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 88 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 89 | 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 90 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 91 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 92 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 93 | 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 94 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 95 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 96 | 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 97 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 98 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 99 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 100 | }; 101 | 102 | const uch _length_code[MAX_MATCH-MIN_MATCH+1]= { 103 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 104 | 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 105 | 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 106 | 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 107 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 108 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 109 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 110 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 111 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 112 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 113 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 114 | 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 115 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 116 | }; 117 | 118 | local const int base_length[LENGTH_CODES] = { 119 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 120 | 64, 80, 96, 112, 128, 160, 192, 224, 0 121 | }; 122 | 123 | local const int base_dist[D_CODES] = { 124 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 125 | 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 126 | 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 127 | }; 128 | 129 | -------------------------------------------------------------------------------- /libpng/zlib/uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Decompresses the source buffer into the destination buffer. sourceLen is 13 | the byte length of the source buffer. Upon entry, destLen is the total 14 | size of the destination buffer, which must be large enough to hold the 15 | entire uncompressed data. (The size of the uncompressed data must have 16 | been saved previously by the compressor and transmitted to the decompressor 17 | by some mechanism outside the scope of this compression library.) 18 | Upon exit, destLen is the actual size of the compressed buffer. 19 | This function can be used to decompress a whole file at once if the 20 | input file is mmap'ed. 21 | 22 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 23 | enough memory, Z_BUF_ERROR if there was not enough room in the output 24 | buffer, or Z_DATA_ERROR if the input data was corrupted. 25 | */ 26 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) 27 | Bytef *dest; 28 | uLongf *destLen; 29 | const Bytef *source; 30 | uLong sourceLen; 31 | { 32 | z_stream stream; 33 | int err; 34 | 35 | stream.next_in = (Bytef*)source; 36 | stream.avail_in = (uInt)sourceLen; 37 | /* Check for source > 64K on 16-bit machine: */ 38 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 39 | 40 | stream.next_out = dest; 41 | stream.avail_out = (uInt)*destLen; 42 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 43 | 44 | stream.zalloc = (alloc_func)0; 45 | stream.zfree = (free_func)0; 46 | 47 | err = inflateInit(&stream); 48 | if (err != Z_OK) return err; 49 | 50 | err = inflate(&stream, Z_FINISH); 51 | if (err != Z_STREAM_END) { 52 | inflateEnd(&stream); 53 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 54 | return Z_DATA_ERROR; 55 | return err; 56 | } 57 | *destLen = stream.total_out; 58 | 59 | err = inflateEnd(&stream); 60 | return err; 61 | } 62 | -------------------------------------------------------------------------------- /libpng/zlib/zconf.h: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #ifndef ZCONF_H 9 | #define ZCONF_H 10 | 11 | /* 12 | * If you *really* need a unique prefix for all types and library functions, 13 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 14 | */ 15 | #ifdef Z_PREFIX 16 | # define deflateInit_ z_deflateInit_ 17 | # define deflate z_deflate 18 | # define deflateEnd z_deflateEnd 19 | # define inflateInit_ z_inflateInit_ 20 | # define inflate z_inflate 21 | # define inflateEnd z_inflateEnd 22 | # define deflateInit2_ z_deflateInit2_ 23 | # define deflateSetDictionary z_deflateSetDictionary 24 | # define deflateCopy z_deflateCopy 25 | # define deflateReset z_deflateReset 26 | # define deflateParams z_deflateParams 27 | # define deflateBound z_deflateBound 28 | # define deflatePrime z_deflatePrime 29 | # define inflateInit2_ z_inflateInit2_ 30 | # define inflateSetDictionary z_inflateSetDictionary 31 | # define inflateSync z_inflateSync 32 | # define inflateSyncPoint z_inflateSyncPoint 33 | # define inflateCopy z_inflateCopy 34 | # define inflateReset z_inflateReset 35 | # define inflateBack z_inflateBack 36 | # define inflateBackEnd z_inflateBackEnd 37 | # define compress z_compress 38 | # define compress2 z_compress2 39 | # define compressBound z_compressBound 40 | # define uncompress z_uncompress 41 | # define adler32 z_adler32 42 | # define crc32 z_crc32 43 | # define get_crc_table z_get_crc_table 44 | # define zError z_zError 45 | 46 | # define alloc_func z_alloc_func 47 | # define free_func z_free_func 48 | # define in_func z_in_func 49 | # define out_func z_out_func 50 | # define Byte z_Byte 51 | # define uInt z_uInt 52 | # define uLong z_uLong 53 | # define Bytef z_Bytef 54 | # define charf z_charf 55 | # define intf z_intf 56 | # define uIntf z_uIntf 57 | # define uLongf z_uLongf 58 | # define voidpf z_voidpf 59 | # define voidp z_voidp 60 | #endif 61 | 62 | #if defined(__MSDOS__) && !defined(MSDOS) 63 | # define MSDOS 64 | #endif 65 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 66 | # define OS2 67 | #endif 68 | #if defined(_WINDOWS) && !defined(WINDOWS) 69 | # define WINDOWS 70 | #endif 71 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 72 | # ifndef WIN32 73 | # define WIN32 74 | # endif 75 | #endif 76 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 77 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 78 | # ifndef SYS16BIT 79 | # define SYS16BIT 80 | # endif 81 | # endif 82 | #endif 83 | 84 | /* 85 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 86 | * than 64k bytes at a time (needed on systems with 16-bit int). 87 | */ 88 | #ifdef SYS16BIT 89 | # define MAXSEG_64K 90 | #endif 91 | #ifdef MSDOS 92 | # define UNALIGNED_OK 93 | #endif 94 | 95 | #ifdef __STDC_VERSION__ 96 | # ifndef STDC 97 | # define STDC 98 | # endif 99 | # if __STDC_VERSION__ >= 199901L 100 | # ifndef STDC99 101 | # define STDC99 102 | # endif 103 | # endif 104 | #endif 105 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 106 | # define STDC 107 | #endif 108 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 109 | # define STDC 110 | #endif 111 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 112 | # define STDC 113 | #endif 114 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 115 | # define STDC 116 | #endif 117 | 118 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 119 | # define STDC 120 | #endif 121 | 122 | #ifndef STDC 123 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 124 | # define const /* note: need a more gentle solution here */ 125 | # endif 126 | #endif 127 | 128 | /* Some Mac compilers merge all .h files incorrectly: */ 129 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) 130 | # define NO_DUMMY_DECL 131 | #endif 132 | 133 | /* Maximum value for memLevel in deflateInit2 */ 134 | #ifndef MAX_MEM_LEVEL 135 | # ifdef MAXSEG_64K 136 | # define MAX_MEM_LEVEL 8 137 | # else 138 | # define MAX_MEM_LEVEL 9 139 | # endif 140 | #endif 141 | 142 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 143 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 144 | * created by gzip. (Files created by minigzip can still be extracted by 145 | * gzip.) 146 | */ 147 | #ifndef MAX_WBITS 148 | # define MAX_WBITS 15 /* 32K LZ77 window */ 149 | #endif 150 | 151 | /* The memory requirements for deflate are (in bytes): 152 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 153 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 154 | plus a few kilobytes for small objects. For example, if you want to reduce 155 | the default memory requirements from 256K to 128K, compile with 156 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 157 | Of course this will generally degrade compression (there's no free lunch). 158 | 159 | The memory requirements for inflate are (in bytes) 1 << windowBits 160 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 161 | for small objects. 162 | */ 163 | 164 | /* Type declarations */ 165 | 166 | #ifndef OF /* function prototypes */ 167 | # ifdef STDC 168 | # define OF(args) args 169 | # else 170 | # define OF(args) () 171 | # endif 172 | #endif 173 | 174 | /* The following definitions for FAR are needed only for MSDOS mixed 175 | * model programming (small or medium model with some far allocations). 176 | * This was tested only with MSC; for other MSDOS compilers you may have 177 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 178 | * just define FAR to be empty. 179 | */ 180 | #ifdef SYS16BIT 181 | # if defined(M_I86SM) || defined(M_I86MM) 182 | /* MSC small or medium model */ 183 | # define SMALL_MEDIUM 184 | # ifdef _MSC_VER 185 | # define FAR _far 186 | # else 187 | # define FAR far 188 | # endif 189 | # endif 190 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 191 | /* Turbo C small or medium model */ 192 | # define SMALL_MEDIUM 193 | # ifdef __BORLANDC__ 194 | # define FAR _far 195 | # else 196 | # define FAR far 197 | # endif 198 | # endif 199 | #endif 200 | 201 | #if defined(WINDOWS) || defined(WIN32) 202 | /* If building or using zlib as a DLL, define ZLIB_DLL. 203 | * This is not mandatory, but it offers a little performance increase. 204 | */ 205 | # ifdef ZLIB_DLL 206 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 207 | # ifdef ZLIB_INTERNAL 208 | # define ZEXTERN extern __declspec(dllexport) 209 | # else 210 | # define ZEXTERN extern __declspec(dllimport) 211 | # endif 212 | # endif 213 | # endif /* ZLIB_DLL */ 214 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 215 | * define ZLIB_WINAPI. 216 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 217 | */ 218 | # ifdef ZLIB_WINAPI 219 | # ifdef FAR 220 | # undef FAR 221 | # endif 222 | # include 223 | /* No need for _export, use ZLIB.DEF instead. */ 224 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 225 | # define ZEXPORT WINAPI 226 | # ifdef WIN32 227 | # define ZEXPORTVA WINAPIV 228 | # else 229 | # define ZEXPORTVA FAR CDECL 230 | # endif 231 | # endif 232 | #endif 233 | 234 | #if defined (__BEOS__) 235 | # ifdef ZLIB_DLL 236 | # ifdef ZLIB_INTERNAL 237 | # define ZEXPORT __declspec(dllexport) 238 | # define ZEXPORTVA __declspec(dllexport) 239 | # else 240 | # define ZEXPORT __declspec(dllimport) 241 | # define ZEXPORTVA __declspec(dllimport) 242 | # endif 243 | # endif 244 | #endif 245 | 246 | #ifndef ZEXTERN 247 | # define ZEXTERN extern 248 | #endif 249 | #ifndef ZEXPORT 250 | # define ZEXPORT 251 | #endif 252 | #ifndef ZEXPORTVA 253 | # define ZEXPORTVA 254 | #endif 255 | 256 | #ifndef FAR 257 | # define FAR 258 | #endif 259 | 260 | #if !defined(__MACTYPES__) 261 | typedef unsigned char Byte; /* 8 bits */ 262 | #endif 263 | typedef unsigned int uInt; /* 16 bits or more */ 264 | typedef unsigned long uLong; /* 32 bits or more */ 265 | 266 | #ifdef SMALL_MEDIUM 267 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 268 | # define Bytef Byte FAR 269 | #else 270 | typedef Byte FAR Bytef; 271 | #endif 272 | typedef char FAR charf; 273 | typedef int FAR intf; 274 | typedef uInt FAR uIntf; 275 | typedef uLong FAR uLongf; 276 | 277 | #ifdef STDC 278 | typedef void const *voidpc; 279 | typedef void FAR *voidpf; 280 | typedef void *voidp; 281 | #else 282 | typedef Byte const *voidpc; 283 | typedef Byte FAR *voidpf; 284 | typedef Byte *voidp; 285 | #endif 286 | 287 | #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ 288 | # include /* for off_t */ 289 | # include /* for SEEK_* and off_t */ 290 | # ifdef VMS 291 | # include /* for off_t */ 292 | # endif 293 | # define z_off_t off_t 294 | #endif 295 | #ifndef SEEK_SET 296 | # define SEEK_SET 0 /* Seek from beginning of file. */ 297 | # define SEEK_CUR 1 /* Seek from current position. */ 298 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 299 | #endif 300 | #ifndef z_off_t 301 | # define z_off_t long 302 | #endif 303 | 304 | #if defined(__OS400__) 305 | # define NO_vsnprintf 306 | #endif 307 | 308 | #if defined(__MVS__) 309 | # define NO_vsnprintf 310 | # ifdef FAR 311 | # undef FAR 312 | # endif 313 | #endif 314 | 315 | /* MVS linker does not support external names larger than 8 bytes */ 316 | #if defined(__MVS__) 317 | # pragma map(deflateInit_,"DEIN") 318 | # pragma map(deflateInit2_,"DEIN2") 319 | # pragma map(deflateEnd,"DEEND") 320 | # pragma map(deflateBound,"DEBND") 321 | # pragma map(inflateInit_,"ININ") 322 | # pragma map(inflateInit2_,"ININ2") 323 | # pragma map(inflateEnd,"INEND") 324 | # pragma map(inflateSync,"INSY") 325 | # pragma map(inflateSetDictionary,"INSEDI") 326 | # pragma map(compressBound,"CMBND") 327 | # pragma map(inflate_table,"INTABL") 328 | # pragma map(inflate_fast,"INFA") 329 | # pragma map(inflate_copyright,"INCOPY") 330 | #endif 331 | 332 | #endif /* ZCONF_H */ 333 | -------------------------------------------------------------------------------- /libpng/zlib/zutil.c: -------------------------------------------------------------------------------- 1 | /* zutil.c -- target dependent utility functions for the compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #ifndef NO_DUMMY_DECL 11 | struct internal_state {int dummy;}; /* for buggy compilers */ 12 | #endif 13 | 14 | const char * const z_errmsg[10] = { 15 | "need dictionary", /* Z_NEED_DICT 2 */ 16 | "stream end", /* Z_STREAM_END 1 */ 17 | "", /* Z_OK 0 */ 18 | "file error", /* Z_ERRNO (-1) */ 19 | "stream error", /* Z_STREAM_ERROR (-2) */ 20 | "data error", /* Z_DATA_ERROR (-3) */ 21 | "insufficient memory", /* Z_MEM_ERROR (-4) */ 22 | "buffer error", /* Z_BUF_ERROR (-5) */ 23 | "incompatible version",/* Z_VERSION_ERROR (-6) */ 24 | ""}; 25 | 26 | 27 | const char * ZEXPORT zlibVersion() 28 | { 29 | return ZLIB_VERSION; 30 | } 31 | 32 | uLong ZEXPORT zlibCompileFlags() 33 | { 34 | uLong flags; 35 | 36 | flags = 0; 37 | switch (sizeof(uInt)) { 38 | case 2: break; 39 | case 4: flags += 1; break; 40 | case 8: flags += 2; break; 41 | default: flags += 3; 42 | } 43 | switch (sizeof(uLong)) { 44 | case 2: break; 45 | case 4: flags += 1 << 2; break; 46 | case 8: flags += 2 << 2; break; 47 | default: flags += 3 << 2; 48 | } 49 | switch (sizeof(voidpf)) { 50 | case 2: break; 51 | case 4: flags += 1 << 4; break; 52 | case 8: flags += 2 << 4; break; 53 | default: flags += 3 << 4; 54 | } 55 | switch (sizeof(z_off_t)) { 56 | case 2: break; 57 | case 4: flags += 1 << 6; break; 58 | case 8: flags += 2 << 6; break; 59 | default: flags += 3 << 6; 60 | } 61 | #ifdef DEBUG 62 | flags += 1 << 8; 63 | #endif 64 | #if defined(ASMV) || defined(ASMINF) 65 | flags += 1 << 9; 66 | #endif 67 | #ifdef ZLIB_WINAPI 68 | flags += 1 << 10; 69 | #endif 70 | #ifdef BUILDFIXED 71 | flags += 1 << 12; 72 | #endif 73 | #ifdef DYNAMIC_CRC_TABLE 74 | flags += 1 << 13; 75 | #endif 76 | #ifdef NO_GZCOMPRESS 77 | flags += 1L << 16; 78 | #endif 79 | #ifdef NO_GZIP 80 | flags += 1L << 17; 81 | #endif 82 | #ifdef PKZIP_BUG_WORKAROUND 83 | flags += 1L << 20; 84 | #endif 85 | #ifdef FASTEST 86 | flags += 1L << 21; 87 | #endif 88 | #ifdef STDC 89 | # ifdef NO_vsnprintf 90 | flags += 1L << 25; 91 | # ifdef HAS_vsprintf_void 92 | flags += 1L << 26; 93 | # endif 94 | # else 95 | # ifdef HAS_vsnprintf_void 96 | flags += 1L << 26; 97 | # endif 98 | # endif 99 | #else 100 | flags += 1L << 24; 101 | # ifdef NO_snprintf 102 | flags += 1L << 25; 103 | # ifdef HAS_sprintf_void 104 | flags += 1L << 26; 105 | # endif 106 | # else 107 | # ifdef HAS_snprintf_void 108 | flags += 1L << 26; 109 | # endif 110 | # endif 111 | #endif 112 | return flags; 113 | } 114 | 115 | #ifdef DEBUG 116 | 117 | # ifndef verbose 118 | # define verbose 0 119 | # endif 120 | int z_verbose = verbose; 121 | 122 | void z_error (m) 123 | char *m; 124 | { 125 | fprintf(stderr, "%s\n", m); 126 | exit(1); 127 | } 128 | #endif 129 | 130 | /* exported to allow conversion of error code to string for compress() and 131 | * uncompress() 132 | */ 133 | const char * ZEXPORT zError(err) 134 | int err; 135 | { 136 | return ERR_MSG(err); 137 | } 138 | 139 | #if defined(_WIN32_WCE) 140 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 141 | * errno. We define it as a global variable to simplify porting. 142 | * Its value is always 0 and should not be used. 143 | */ 144 | int errno = 0; 145 | #endif 146 | 147 | #ifndef HAVE_MEMCPY 148 | 149 | void zmemcpy(dest, source, len) 150 | Bytef* dest; 151 | const Bytef* source; 152 | uInt len; 153 | { 154 | if (len == 0) return; 155 | do { 156 | *dest++ = *source++; /* ??? to be unrolled */ 157 | } while (--len != 0); 158 | } 159 | 160 | int zmemcmp(s1, s2, len) 161 | const Bytef* s1; 162 | const Bytef* s2; 163 | uInt len; 164 | { 165 | uInt j; 166 | 167 | for (j = 0; j < len; j++) { 168 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 169 | } 170 | return 0; 171 | } 172 | 173 | void zmemzero(dest, len) 174 | Bytef* dest; 175 | uInt len; 176 | { 177 | if (len == 0) return; 178 | do { 179 | *dest++ = 0; /* ??? to be unrolled */ 180 | } while (--len != 0); 181 | } 182 | #endif 183 | 184 | 185 | #ifdef SYS16BIT 186 | 187 | #ifdef __TURBOC__ 188 | /* Turbo C in 16-bit mode */ 189 | 190 | # define MY_ZCALLOC 191 | 192 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes 193 | * and farmalloc(64K) returns a pointer with an offset of 8, so we 194 | * must fix the pointer. Warning: the pointer must be put back to its 195 | * original form in order to free it, use zcfree(). 196 | */ 197 | 198 | #define MAX_PTR 10 199 | /* 10*64K = 640K */ 200 | 201 | local int next_ptr = 0; 202 | 203 | typedef struct ptr_table_s { 204 | voidpf org_ptr; 205 | voidpf new_ptr; 206 | } ptr_table; 207 | 208 | local ptr_table table[MAX_PTR]; 209 | /* This table is used to remember the original form of pointers 210 | * to large buffers (64K). Such pointers are normalized with a zero offset. 211 | * Since MSDOS is not a preemptive multitasking OS, this table is not 212 | * protected from concurrent access. This hack doesn't work anyway on 213 | * a protected system like OS/2. Use Microsoft C instead. 214 | */ 215 | 216 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 217 | { 218 | voidpf buf = opaque; /* just to make some compilers happy */ 219 | ulg bsize = (ulg)items*size; 220 | 221 | /* If we allocate less than 65520 bytes, we assume that farmalloc 222 | * will return a usable pointer which doesn't have to be normalized. 223 | */ 224 | if (bsize < 65520L) { 225 | buf = farmalloc(bsize); 226 | if (*(ush*)&buf != 0) return buf; 227 | } else { 228 | buf = farmalloc(bsize + 16L); 229 | } 230 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 231 | table[next_ptr].org_ptr = buf; 232 | 233 | /* Normalize the pointer to seg:0 */ 234 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 235 | *(ush*)&buf = 0; 236 | table[next_ptr++].new_ptr = buf; 237 | return buf; 238 | } 239 | 240 | void zcfree (voidpf opaque, voidpf ptr) 241 | { 242 | int n; 243 | if (*(ush*)&ptr != 0) { /* object < 64K */ 244 | farfree(ptr); 245 | return; 246 | } 247 | /* Find the original pointer */ 248 | for (n = 0; n < next_ptr; n++) { 249 | if (ptr != table[n].new_ptr) continue; 250 | 251 | farfree(table[n].org_ptr); 252 | while (++n < next_ptr) { 253 | table[n-1] = table[n]; 254 | } 255 | next_ptr--; 256 | return; 257 | } 258 | ptr = opaque; /* just to make some compilers happy */ 259 | Assert(0, "zcfree: ptr not found"); 260 | } 261 | 262 | #endif /* __TURBOC__ */ 263 | 264 | 265 | #ifdef M_I86 266 | /* Microsoft C in 16-bit mode */ 267 | 268 | # define MY_ZCALLOC 269 | 270 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 271 | # define _halloc halloc 272 | # define _hfree hfree 273 | #endif 274 | 275 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 276 | { 277 | if (opaque) opaque = 0; /* to make compiler happy */ 278 | return _halloc((long)items, size); 279 | } 280 | 281 | void zcfree (voidpf opaque, voidpf ptr) 282 | { 283 | if (opaque) opaque = 0; /* to make compiler happy */ 284 | _hfree(ptr); 285 | } 286 | 287 | #endif /* M_I86 */ 288 | 289 | #endif /* SYS16BIT */ 290 | 291 | 292 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ 293 | 294 | #ifndef STDC 295 | extern voidp malloc OF((uInt size)); 296 | extern voidp calloc OF((uInt items, uInt size)); 297 | extern void free OF((voidpf ptr)); 298 | #endif 299 | 300 | voidpf zcalloc (opaque, items, size) 301 | voidpf opaque; 302 | unsigned items; 303 | unsigned size; 304 | { 305 | if (opaque) items += size - size; /* make compiler happy */ 306 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 307 | (voidpf)calloc(items, size); 308 | } 309 | 310 | void zcfree (opaque, ptr) 311 | voidpf opaque; 312 | voidpf ptr; 313 | { 314 | free(ptr); 315 | if (opaque) return; /* make compiler happy */ 316 | } 317 | 318 | #endif /* MY_ZCALLOC */ 319 | -------------------------------------------------------------------------------- /libpng/zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #define ZLIB_INTERNAL 17 | #include "zlib.h" 18 | 19 | #ifdef STDC 20 | # ifndef _WIN32_WCE 21 | # include 22 | # endif 23 | # include 24 | # include 25 | #endif 26 | #ifdef NO_ERRNO_H 27 | # ifdef _WIN32_WCE 28 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 29 | * errno. We define it as a global variable to simplify porting. 30 | * Its value is always 0 and should not be used. We rename it to 31 | * avoid conflict with other libraries that use the same workaround. 32 | */ 33 | # define errno z_errno 34 | # endif 35 | extern int errno; 36 | #else 37 | # ifndef _WIN32_WCE 38 | # include 39 | # endif 40 | #endif 41 | 42 | #ifndef local 43 | # define local static 44 | #endif 45 | /* compile with -Dlocal if your debugger can't find static symbols */ 46 | 47 | typedef unsigned char uch; 48 | typedef uch FAR uchf; 49 | typedef unsigned short ush; 50 | typedef ush FAR ushf; 51 | typedef unsigned long ulg; 52 | 53 | extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 54 | /* (size given to avoid silly warnings with Visual C++) */ 55 | 56 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 57 | 58 | #define ERR_RETURN(strm,err) \ 59 | return (strm->msg = (char*)ERR_MSG(err), (err)) 60 | /* To be used only when the state is known to be valid */ 61 | 62 | /* common constants */ 63 | 64 | #ifndef DEF_WBITS 65 | # define DEF_WBITS MAX_WBITS 66 | #endif 67 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 68 | 69 | #if MAX_MEM_LEVEL >= 8 70 | # define DEF_MEM_LEVEL 8 71 | #else 72 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 73 | #endif 74 | /* default memLevel */ 75 | 76 | #define STORED_BLOCK 0 77 | #define STATIC_TREES 1 78 | #define DYN_TREES 2 79 | /* The three kinds of block type */ 80 | 81 | #define MIN_MATCH 3 82 | #define MAX_MATCH 258 83 | /* The minimum and maximum match lengths */ 84 | 85 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 86 | 87 | /* target dependencies */ 88 | 89 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 90 | # define OS_CODE 0x00 91 | # if defined(__TURBOC__) || defined(__BORLANDC__) 92 | # if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 93 | /* Allow compilation with ANSI keywords only enabled */ 94 | void _Cdecl farfree( void *block ); 95 | void *_Cdecl farmalloc( unsigned long nbytes ); 96 | # else 97 | # include 98 | # endif 99 | # else /* MSC or DJGPP */ 100 | # include 101 | # endif 102 | #endif 103 | 104 | #ifdef AMIGA 105 | # define OS_CODE 0x01 106 | #endif 107 | 108 | #if defined(VAXC) || defined(VMS) 109 | # define OS_CODE 0x02 110 | # define F_OPEN(name, mode) \ 111 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 112 | #endif 113 | 114 | #if defined(ATARI) || defined(atarist) 115 | # define OS_CODE 0x05 116 | #endif 117 | 118 | #ifdef OS2 119 | # define OS_CODE 0x06 120 | # ifdef M_I86 121 | #include 122 | # endif 123 | #endif 124 | 125 | #if defined(MACOS) || defined(TARGET_OS_MAC) 126 | # define OS_CODE 0x07 127 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 128 | # include /* for fdopen */ 129 | # else 130 | # ifndef fdopen 131 | # define fdopen(fd,mode) NULL /* No fdopen() */ 132 | # endif 133 | # endif 134 | #endif 135 | 136 | #ifdef TOPS20 137 | # define OS_CODE 0x0a 138 | #endif 139 | 140 | #ifdef WIN32 141 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 142 | # define OS_CODE 0x0b 143 | # endif 144 | #endif 145 | 146 | #ifdef __50SERIES /* Prime/PRIMOS */ 147 | # define OS_CODE 0x0f 148 | #endif 149 | 150 | #if defined(_BEOS_) || defined(RISCOS) 151 | # define fdopen(fd,mode) NULL /* No fdopen() */ 152 | #endif 153 | 154 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) 155 | # if defined(_WIN32_WCE) 156 | # define fdopen(fd,mode) NULL /* No fdopen() */ 157 | # ifndef _PTRDIFF_T_DEFINED 158 | typedef int ptrdiff_t; 159 | # define _PTRDIFF_T_DEFINED 160 | # endif 161 | # else 162 | # define fdopen(fd,type) _fdopen(fd,type) 163 | # endif 164 | #endif 165 | 166 | /* common defaults */ 167 | 168 | #ifndef OS_CODE 169 | # define OS_CODE 0x03 /* assume Unix */ 170 | #endif 171 | 172 | #ifndef F_OPEN 173 | # define F_OPEN(name, mode) fopen((name), (mode)) 174 | #endif 175 | 176 | /* functions */ 177 | 178 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 179 | # ifndef HAVE_VSNPRINTF 180 | # define HAVE_VSNPRINTF 181 | # endif 182 | #endif 183 | #if defined(__CYGWIN__) 184 | # ifndef HAVE_VSNPRINTF 185 | # define HAVE_VSNPRINTF 186 | # endif 187 | #endif 188 | #ifndef HAVE_VSNPRINTF 189 | # ifdef MSDOS 190 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 191 | but for now we just assume it doesn't. */ 192 | # define NO_vsnprintf 193 | # endif 194 | # ifdef __TURBOC__ 195 | # define NO_vsnprintf 196 | # endif 197 | # ifdef WIN32 198 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 199 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 200 | # define vsnprintf _vsnprintf 201 | # endif 202 | # endif 203 | # ifdef __SASC 204 | # define NO_vsnprintf 205 | # endif 206 | #endif 207 | #ifdef VMS 208 | # define NO_vsnprintf 209 | #endif 210 | 211 | #if defined(pyr) 212 | # define NO_MEMCPY 213 | #endif 214 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 215 | /* Use our own functions for small and medium model with MSC <= 5.0. 216 | * You may have to use the same strategy for Borland C (untested). 217 | * The __SC__ check is for Symantec. 218 | */ 219 | # define NO_MEMCPY 220 | #endif 221 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 222 | # define HAVE_MEMCPY 223 | #endif 224 | #ifdef HAVE_MEMCPY 225 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 226 | # define zmemcpy _fmemcpy 227 | # define zmemcmp _fmemcmp 228 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 229 | # else 230 | # define zmemcpy memcpy 231 | # define zmemcmp memcmp 232 | # define zmemzero(dest, len) memset(dest, 0, len) 233 | # endif 234 | #else 235 | extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); 236 | extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 237 | extern void zmemzero OF((Bytef* dest, uInt len)); 238 | #endif 239 | 240 | /* Diagnostic functions */ 241 | #ifdef DEBUG 242 | # include 243 | extern int z_verbose; 244 | extern void z_error OF((char *m)); 245 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 246 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 247 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 248 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 249 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 250 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 251 | #else 252 | # define Assert(cond,msg) 253 | # define Trace(x) 254 | # define Tracev(x) 255 | # define Tracevv(x) 256 | # define Tracec(c,x) 257 | # define Tracecv(c,x) 258 | #endif 259 | 260 | 261 | voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); 262 | void zcfree OF((voidpf opaque, voidpf ptr)); 263 | 264 | #define ZALLOC(strm, items, size) \ 265 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 266 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 267 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 268 | 269 | #endif /* ZUTIL_H */ 270 | -------------------------------------------------------------------------------- /lightfield.glsl: -------------------------------------------------------------------------------- 1 | #version 420 2 | 3 | //------------------------------------------------------------------------------ 4 | // constants 5 | #define PI 3.141592657 6 | #define TWO_PI 6.283185307 7 | #define SQRT_2 1.414213562 8 | #define INV_SQRT_2 0.707106781 9 | 10 | 11 | //------------------------------------------------------------------------------ 12 | // function decl 13 | void find_views(vec3 cDir, 14 | out ivec3 layers, 15 | out vec3 weights); 16 | 17 | vec3 spherical_to_cartesian(float theta, float phi); 18 | 19 | 20 | //------------------------------------------------------------------------------ 21 | // uniforms 22 | uniform sampler2DArray sView; 23 | uniform int uViewCount; 24 | 25 | layout(std140) uniform ViewAxis { 26 | mat3 uAxis[VIEWCNT]; // VIEWCNT must be defined 27 | }; 28 | 29 | uniform vec3 uCamPos; 30 | uniform mat3 uBillboardAxis; 31 | uniform mat4 uModelViewProjection; 32 | 33 | 34 | 35 | //------------------------------------------------------------------------------ 36 | // vertex shader 37 | #ifdef _VERTEX_ 38 | layout(location=0) out vec3 oTexCoord; 39 | layout(location=1) out vec3 oViewDir; 40 | 41 | void main() { 42 | vec2 p = vec2(gl_VertexID & 1, (gl_VertexID >> 1) & 1)*2.0-1.0; 43 | gl_Position = uModelViewProjection * vec4(p,0,1); 44 | oTexCoord = uBillboardAxis * vec3(p,0); 45 | oViewDir = normalize(uCamPos-oTexCoord); 46 | } 47 | #endif 48 | 49 | 50 | //------------------------------------------------------------------------------ 51 | // fragment shader 52 | #ifdef _FRAGMENT_ 53 | layout(location=0) in vec3 iTexCoord; 54 | layout(location=1) in vec3 iViewDir; 55 | layout(location=0) out vec4 oColour; 56 | 57 | layout(depth_greater) out float gl_FragDepth; 58 | 59 | void main() { 60 | ivec3 layers; 61 | vec3 weights; 62 | find_views(-uBillboardAxis[2], layers, weights); 63 | 64 | // texcoords for each view 65 | vec2 texCoord0 = (uAxis[layers[0]] * iTexCoord).st*0.5+0.5; 66 | vec2 texCoord1 = (uAxis[layers[1]] * iTexCoord).st*0.5+0.5; 67 | vec2 texCoord2 = (uAxis[layers[2]] * iTexCoord).st*0.5+0.5; 68 | vec4 t0 = texture(sView, vec3(texCoord0, layers[0])); 69 | vec4 t1 = texture(sView, vec3(texCoord1, layers[1])); 70 | vec4 t2 = texture(sView, vec3(texCoord2, layers[2])); 71 | vec4 t = t0*weights[0]+t1*weights[1]+t2*weights[2]; // lerp 72 | 73 | vec4 z = 74 | // second iteration (bugged) 75 | // vec3 q = iTexCoord + iViewDir*fma(t.r,SQRT_2,-INV_SQRT_2); 76 | // texCoord0 = (uAxis[layers[0]] * q).st*0.5+0.5; 77 | // texCoord1 = (uAxis[layers[1]] * q).st*0.5+0.5; 78 | // texCoord2 = (uAxis[layers[2]] * q).st*0.5+0.5; 79 | // t0 = texture(sView, vec3(texCoord0, layers[0])); 80 | // t1 = texture(sView, vec3(texCoord1, layers[1])); 81 | // t2 = texture(sView, vec3(texCoord2, layers[2])); 82 | // t = t0*weights[0]+t1*weights[1]+t2*weights[2]; 83 | 84 | // set depth 85 | gl_FragDepth = gl_FragCoord.z; 86 | 87 | // build output 88 | oColour.rgb = spherical_to_cartesian(t.g*PI, t.b*TWO_PI)*t.a; 89 | oColour.rgb = t.rrr*t.a; 90 | oColour.rgb = vec3(gl_FragDepth); 91 | 92 | } 93 | #endif 94 | 95 | 96 | //------------------------------------------------------------------------------ 97 | // function impl 98 | ivec3 _view_number(ivec3 i, ivec3 j) { 99 | return i*((2*uViewCount+1)-abs(i))+j+(uViewCount*(uViewCount+1)); 100 | } 101 | 102 | 103 | //------------------------------------------------------------------------------ 104 | void find_views(vec3 cDir, out ivec3 layers, out vec3 weights) { 105 | vec3 VDIR = vec3(cDir.x, max(cDir.y, 0.01), cDir.z); 106 | float a = abs(VDIR.z) > abs(VDIR.x) ? VDIR.x / VDIR.z : -VDIR.z / VDIR.x; 107 | float nxx = uViewCount * (1.0 - a) * acos(VDIR.y) / PI; 108 | float nyy = uViewCount * (1.0 + a) * acos(VDIR.y) / PI; 109 | int i = int(floor(nxx)); 110 | int j = int(floor(nyy)); 111 | float ti = nxx - i; 112 | float tj = nyy - j; 113 | float alpha = 1.0 - ti - tj; 114 | bool b = alpha > 0.0; 115 | ivec3 ii = ivec3(b ? i : i + 1, i + 1, i); 116 | ivec3 jj = ivec3(b ? j : j + 1, j, j + 1); 117 | weights = vec3(abs(alpha), b ? ti : 1.0 - tj, b ? tj : 1.0 - ti); 118 | if (abs(VDIR.x) >= abs(VDIR.z)) { 119 | ivec3 tmp = ii; 120 | ii = -jj; 121 | jj = tmp; 122 | } 123 | ii *= int(sign(VDIR.x + VDIR.z)); 124 | jj *= int(sign(VDIR.x + VDIR.z)); 125 | layers = _view_number(ii,jj); 126 | } 127 | 128 | 129 | //------------------------------------------------------------------------------ 130 | vec3 spherical_to_cartesian(float theta, float phi) { 131 | float sinTheta = sin(theta); 132 | return vec3(sinTheta*cos(phi), 133 | cos(theta), 134 | sinTheta*sin(phi)); 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /mesh.glsl: -------------------------------------------------------------------------------- 1 | #version 420 2 | 3 | 4 | //------------------------------------------------------------------------------ 5 | #define SQRT_2 1.414213562 6 | #define INV_SQRT_2 0.707106781 7 | #define INV_PI 0.318309886 8 | #define INV_TWO_PI 0.159154943 9 | 10 | 11 | //------------------------------------------------------------------------------ 12 | uniform mat4 uModelView; 13 | uniform mat4 uModelViewProjection; 14 | 15 | 16 | //------------------------------------------------------------------------------ 17 | #ifdef _VERTEX_ 18 | layout(location=0) in vec3 iPosition; 19 | layout(location=1) in vec3 iNormal; 20 | layout(location=0) out vec4 oData; 21 | 22 | void main() { 23 | vec4 viewPos = uModelView * vec4(iPosition,1.0); 24 | gl_Position = uModelViewProjection * vec4(iPosition,1.0); 25 | oData.xyz = iNormal; // normal in world space 26 | oData.w = (-viewPos.z+INV_SQRT_2) * SQRT_2; // depth in view space 27 | } 28 | #endif 29 | 30 | 31 | //------------------------------------------------------------------------------ 32 | #ifdef _FRAGMENT_ 33 | layout(location=0) in vec4 iData; 34 | #define iNormal iData.xyz 35 | #define iDepth iData.w 36 | layout(location=0) out vec4 oData; // in [0,1] 37 | 38 | void main() { 39 | #if 1 40 | vec3 n = normalize(iNormal); 41 | oData.r = iDepth; // depth 42 | oData.g = acos(n.y) * INV_PI; // theta 43 | oData.b = fma(atan(n.x,-n.z), INV_TWO_PI, 0.5); // phi 44 | oData.a = 1.0; // opacity 45 | #else // for debug 46 | oData.rgb = normalize(iNormal); 47 | oData.b = -oColour.b; 48 | #endif 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /models/Stone_F_3.mtl: -------------------------------------------------------------------------------- 1 | # Max2Mtl Version 4.0 Mar 10th, 2001 2 | # 3 | newmtl Stone_F_3 4 | Ka 0.6 0.6 0.6 5 | Kd 0.6 0.6 0.6 6 | Ks 0.9 0.9 0.9 7 | d 1.0 8 | Ns 0.0 9 | illum 2 10 | map_Kd S3Diffus.jpg 11 | # 12 | # EOF 13 | -------------------------------------------------------------------------------- /models/Stone_Forest_1.mtl: -------------------------------------------------------------------------------- 1 | # Max2Mtl Version 4.0 Mar 10th, 2001 2 | # 3 | newmtl Stone_F_1 4 | Ka 0.6 0.6 0.6 5 | Kd 0.6 0.6 0.6 6 | Ks 0.9 0.9 0.9 7 | d 1.0 8 | Ns 0.0 9 | illum 2 10 | # 11 | # EOF 12 | -------------------------------------------------------------------------------- /premake4.lua: -------------------------------------------------------------------------------- 1 | solution "OpenGL" 2 | configurations { 3 | "debug", 4 | "release" 5 | } 6 | platforms { "x64", "x32" } 7 | 8 | -- --------------------------------------------------------- 9 | -- Project 10 | project "demo" 11 | basedir "./" 12 | language "C++" 13 | location "./" 14 | kind "ConsoleApp" -- Shouldn't this be in configuration section ? 15 | files { "*.hpp", "*.cpp" } 16 | files { "core/*.cpp" } 17 | files { "libpng/*.c", "libpng/zlib/*.c" } 18 | includedirs { 19 | "include", 20 | "core", 21 | "libpng", 22 | "libpng/zlib" 23 | } 24 | objdir "obj" 25 | 26 | -- Debug configurations 27 | configuration {"debug"} 28 | defines {"DEBUG"} 29 | flags {"Symbols", "ExtraWarnings"} 30 | 31 | -- Release configurations 32 | configuration {"release"} 33 | defines {"NDEBUG"} 34 | flags {"Optimize"} 35 | 36 | -- Linux x86 platform gmake 37 | configuration {"linux", "gmake", "x32"} 38 | linkoptions { 39 | "-Wl,-rpath,./lib/linux/lin32 -L./lib/linux/lin32 -lGLEW -lglut -lAntTweakBar" 40 | } 41 | libdirs { 42 | "lib/linux/lin32" 43 | } 44 | 45 | -- Linux x64 platform gmake 46 | configuration {"linux", "gmake", "x64"} 47 | linkoptions { 48 | "-Wl,-rpath,./lib/linux/lin64 -L./lib/linux/lin64 -lGLEW -lglut -lAntTweakBar" 49 | } 50 | libdirs { 51 | "lib/linux/lin64" 52 | } 53 | 54 | -- Visual x86 55 | configuration {"vs2010", "x32"} 56 | libdirs { 57 | "lib/windows/win32" 58 | } 59 | links { 60 | "glew32s", 61 | "freeglut", 62 | "AntTweakBar" 63 | } 64 | 65 | ---- Visual x64 66 | -- configuration {"vs2010", "x64"} 67 | -- links { 68 | -- "glew32s", 69 | -- "freeglut", 70 | -- } 71 | -- libdirs { 72 | -- "lib/windows/win64" 73 | -- } 74 | 75 | 76 | -------------------------------------------------------------------------------- /preview.glsl: -------------------------------------------------------------------------------- 1 | #version 420 2 | 3 | uniform sampler2DArray sView; 4 | uniform float uLayer; 5 | 6 | #ifdef _VERTEX_ 7 | layout(location=0) out vec2 oTexCoord; 8 | 9 | void main() { 10 | oTexCoord = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2); 11 | gl_Position.xy = oTexCoord*2.0-1.0; 12 | } 13 | #endif 14 | 15 | 16 | #ifdef _FRAGMENT_ 17 | layout(location=0)in vec2 iTexCoord; 18 | layout(location=0)out vec4 oColour; 19 | 20 | void main() { 21 | oColour = texture(sView, vec3(iTexCoord,uLayer)); 22 | } 23 | 24 | #endif 25 | 26 | --------------------------------------------------------------------------------