[
  {
    "path": ".gitignore",
    "content": "# Prerequisites\n*.d\n\n# Compiled Object files\n*.slo\n*.lo\n*.o\n*.obj\n\n# Precompiled Headers\n*.gch\n*.pch\n\n# Compiled Dynamic libraries\n*.so\n*.dylib\n*.dll\n\n# Fortran module files\n*.mod\n*.smod\n\n# Compiled Static libraries\n*.lai\n*.la\n*.a\n*.lib\n\n# Executables\n*.exe\n*.out\n*.app\n\n# Local builds\nbuild*\nDebug*\nRelease*\n\n*.ipch\n*.suo\n*VC.db\n*.vcxproj\n*.vcxproj.filters\n"
  },
  {
    "path": "CMakeLists.txt",
    "content": "cmake_minimum_required (VERSION 2.6)\nproject (SOIL)\n\nset (SOIL_BUILD_TESTS false CACHE BOOL \"Build tests\")\n\nif(ANDROID)\n  # Android requires GL ES 2.0 package automatically\n  set(LIBRARIES GLESv2)\nelse()\n  find_package (OpenGL REQUIRED)\n  include_directories (${OPENGL_INCLUDE_DIR})\n  set(LIBRARIES ${OPENGL_LIBRARIES})\nendif()\n\nfile (GLOB SOIL_SOURCES src/*.c)\nfile (GLOB SOIL_HEADERS src/*.h)\n\nadd_library (SOIL ${SOIL_SOURCES} ${SOIL_HEADERS})\ntarget_link_libraries (SOIL ${LIBRARIES})\n\ninstall (TARGETS SOIL\n\tLIBRARY DESTINATION lib\n\tARCHIVE DESTINATION lib)\ninstall (FILES ${SOIL_HEADERS} DESTINATION include/SOIL)\n\nif (SOIL_BUILD_TESTS)\n\tadd_executable (test_SOIL src/test_SOIL.cpp)\n\ttarget_link_libraries (test_SOIL SOIL)\n\tinstall (TARGETS SOIL test_SOIL RUNTIME DESTINATION bin)\nendif ()"
  },
  {
    "path": "Makefile",
    "content": "# SOIL makefile for linux (based on the AngelScript makefile)\n# Type 'make' then 'make install' to complete the installation of the library\n\n# For 'make install' to work, set LOCAL according to your system configuration\nPREFIX = /usr/local\nLOCAL = $(PREFIX)\n\nLIB = libSOIL.a\nINC = SOIL.h\n\nSRCDIR = src\nLIBDIR = lib\nINCDIR = src\nOBJDIR = obj\n\nCFLAGS ?= -O2 -s -Wall\nDELETER = rm -f\nCOPIER = cp\n\nSRCNAMES = \\\n  image_helper.c \\\n  stb_image_aug.c  \\\n  image_DXT.c \\\n  SOIL.c \\\n\nOBJ = $(addprefix $(OBJDIR)/, $(notdir $(SRCNAMES:.c=.o)))\nBIN = $(LIBDIR)/$(LIB)\n\nall: $(BIN)\n\n$(BIN): $(OBJ)\n\tmkdir -p $(LIBDIR)\n\t$(DELETER) $(BIN)\n\tar r $(BIN) $(OBJ)\n\tranlib $(BIN)\n\t@echo -------------------------------------------------------------------\n\t@echo Done. As root, type 'make install' to install the library.\n\n$(OBJDIR)/%.o: $(SRCDIR)/%.c\n\tmkdir -p $(OBJDIR)\n\t$(CC) $(CFLAGS) -o $@ -c $<\n\n\nclean:\n\t$(DELETER) $(OBJ) $(BIN)\n\n$(LOCAL)/lib/:\n\tmkdir $(LOCAL)/lib\n\n$(LOCAL)/include/:\n\tmkdir $(LOCAL)/include\n\ninstall: $(BIN) $(LOCAL)/lib/ $(LOCAL)/include/\n\t@echo Installing to: $(LOCAL)/lib and $(LOCAL)/include...\n\t@echo -------------------------------------------------------------------\n\t$(COPIER) $(BIN) $(LOCAL)/lib/\n\t$(COPIER) $(INCDIR)/$(INC) $(LOCAL)/include/\n\t@echo -------------------------------------------------------------------\n\t@echo SOIL library installed. Enjoy!\n\nuninstall:\n\t$(DELETER) $(LOCAL)/include/$(INC) $(LOCAL)/lib/$(LIB)\n\t@echo -------------------------------------------------------------------\n\t@echo SOIL library uninstalled.\n\n.PHONY: all clean install uninstall\n"
  },
  {
    "path": "README.md",
    "content": "This is a clone of Simple OpenGL Image Library from http://lonesock.net/soil.html which hasn't changed since July 7, 2008.\n\nI wanted to work with the code and seeing it was MIT license and the original svn repo is offline, I figured it was acceptable to post it here.\n\nFeatures:\n=========\n\n* Readable Image Formats:\n  * BMP - non-1bpp, non-RLE (from stb_image documentation)\n  * PNG - non-interlaced (from stb_image documentation)\n  * JPG - JPEG baseline (from stb_image documentation)\n  * TGA - greyscale or RGB or RGBA or indexed, uncompressed or RLE\n  * DDS - DXT1/2/3/4/5, uncompressed, cubemaps (can't read 3D DDS files yet)\n  * PSD - (from stb_image documentation)\n  * HDR - converted to LDR, unless loaded with *HDR* functions (RGBE or RGBdivA or RGBdivA2) \n* Writeable Image Formats:\n  * TGA - Greyscale or RGB or RGBA, uncompressed\n  * BMP - RGB, uncompressed\n  * DDS - RGB as DXT1, or RGBA as DXT5 \n* Can load an image file directly into a 2D OpenGL texture, optionally performing the following functions:\n  * Can generate a new texture handle, or reuse one specified\n  * Can automatically rescale the image to the next largest power-of-two size\n  * Can automatically create MIPmaps\n  * Can scale (not simply clamp) the RGB values into the \"safe range\" for NTSC displays (16 to 235, as recommended here)\n  * Can multiply alpha on load (for more correct blending / compositing)\n  * Can flip the image vertically\n  * Can compress and upload any image as DXT1 or DXT5 (if EXT_texture_compression_s3tc is available), using an internal (very fast!) compressor\n  * Can convert the RGB to YCoCg color space (useful with DXT5 compression: see this link from NVIDIA)\n  * Will automatically downsize a texture if it is larger than GL_MAX_TEXTURE_SIZE\n  * Can directly upload DDS files (DXT1/3/5/uncompressed/cubemap, with or without MIPmaps). Note: directly uploading the compressed DDS image will disable the other options (no flipping, no pre-multiplying alpha, no rescaling, no creation of MIPmaps, no auto-downsizing)\n  * Can load rectangluar textures for GUI elements or splash screens (requires GL_ARB/EXT/NV_texture_rectangle) \n* Can decompress images from RAM (e.g. via PhysicsFS or similar) into an OpenGL texture (same features as regular 2D textures, above)\n* Can load cube maps directly into an OpenGL texture (same features as regular 2D textures, above)\n  * Can take six image files directly into an OpenGL cube map texture\n  * Can take a single image file where width = 6*height (or vice versa), split it into an OpenGL cube map texture \n* No external dependencies\n* Tiny\n* Cross platform (Windows, *nix, Mac OS X)\n* Public Domain \n\nUsage:\n=======\n\nSOIL is meant to be used as a static library (as it's tiny and in the public domain). You can use the static library file included in the zip (libSOIL.a works for MinGW and Microsoft compilers...feel free to rename it to SOIL.lib if that makes you happy), or compile the library yourself. The code is cross-platform and has been tested on Windows, Linux, and Mac. (The heaviest testing has been on the Windows platform, so feel free to email me if you find any issues with other platforms.)\n\nSimply include SOIL.h in your C or C++ file, link in the static library, and then use any of SOIL's functions. The file SOIL.h contains simple doxygen style documentation. (If you use the static library, no other header files are needed besides SOIL.h) Below are some simple usage examples:\n\nload an image file directly as a new OpenGL texture\n\n    GLuint tex_2d = SOIL_load_OGL_texture\n    (\n      \"img.png\",\n      SOIL_LOAD_AUTO,\n      SOIL_CREATE_NEW_ID,\n      SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT\n    );\n\ncheck for an error during the load process\n\n    if( 0 == tex_2d )\n    {\n      printf( \"SOIL loading error: '%s'\\n\", SOIL_last_result() );\n    }\n\nload another image, but into the same texture ID, overwriting the last one\n\n    tex_2d = SOIL_load_OGL_texture\n    (\n      \"some_other_img.dds\",\n      SOIL_LOAD_AUTO,\n      tex_2d,\n      SOIL_FLAG_DDS_LOAD_DIRECT\n    );\n\nload 6 images into a new OpenGL cube map, forcing RGB\n\n    GLuint tex_cube = SOIL_load_OGL_cubemap\n    (\n      \"xp.jpg\",\n      \"xn.jpg\",\n      \"yp.jpg\",\n      \"yn.jpg\",\n      \"zp.jpg\",\n      \"zn.jpg\",\n      SOIL_LOAD_RGB,\n      SOIL_CREATE_NEW_ID,\n      SOIL_FLAG_MIPMAPS\n    );\n\nload and split a single image into a new OpenGL cube map, default format\nface order = East South West North Up Down => \"ESWNUD\", case sensitive!\n\n    GLuint single_tex_cube = SOIL_load_OGL_single_cubemap\n    (\n      \"split_cubemap.png\",\n      \"EWUDNS\",\n      SOIL_LOAD_AUTO,\n      SOIL_CREATE_NEW_ID,\n      SOIL_FLAG_MIPMAPS\n    );\n\nactually, load a DDS cubemap over the last OpenGL cube map, default format\ntry to load it directly, but give the order of the faces in case that fails\nthe DDS cubemap face order is pre-defined as SOIL_DDS_CUBEMAP_FACE_ORDER\n\n    single_tex_cube = SOIL_load_OGL_single_cubemap\n    (\n      \"overwrite_cubemap.dds\",\n      SOIL_DDS_CUBEMAP_FACE_ORDER,\n      SOIL_LOAD_AUTO,\n      single_tex_cube,\n      SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT\n    );\n\nload an image as a heightmap, forcing greyscale (so channels should be 1)\n\n    int width, height, channels;\n    unsigned char *ht_map = SOIL_load_image\n    (\n      \"terrain.tga\",\n      &width, &height, &channels,\n      SOIL_LOAD_L\n    );\n\nsave that image as another type\n\n    int save_result = SOIL_save_image\n    (\n      \"new_terrain.dds\",\n      SOIL_SAVE_TYPE_DDS,\n      width, height, channels,\n      ht_map\n    );\n\nsave a screenshot of your awesome OpenGL game engine, running at 1024x768\n\n    save_result = SOIL_save_screenshot\n    (\n      \"awesomenessity.bmp\",\n      SOIL_SAVE_TYPE_BMP,\n      0, 0, 1024, 768\n    );\n\nloaded a file via PhysicsFS, need to decompress the image from RAM,\nwhere it's in a buffer: unsigned char *image_in_RAM\n\n    GLuint tex_2d_from_RAM = SOIL_load_OGL_texture_from_memory\n    (\n      image_in_RAM,\n      image_in_RAM_bytes,\n      SOIL_LOAD_AUTO,\n      SOIL_CREATE_NEW_ID,\n      SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT\n    );\n\ndone with the heightmap, free up the RAM\n\n    SOIL_free_image_data( ht_map );\n"
  },
  {
    "path": "projects/VC6/SOIL.dsp",
    "content": "# Microsoft Developer Studio Project File - Name=\"SOIL\" - Package Owner=<4>\r\n# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n# ** DO NOT EDIT **\r\n\r\n# TARGTYPE \"Win32 (x86) Static Library\" 0x0104\r\n\r\nCFG=SOIL - Win32 Debug\r\n!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n!MESSAGE use the Export Makefile command and run\r\n!MESSAGE \r\n!MESSAGE NMAKE /f \"SOIL.mak\".\r\n!MESSAGE \r\n!MESSAGE You can specify a configuration when running NMAKE\r\n!MESSAGE by defining the macro CFG on the command line. For example:\r\n!MESSAGE \r\n!MESSAGE NMAKE /f \"SOIL.mak\" CFG=\"SOIL - Win32 Debug\"\r\n!MESSAGE \r\n!MESSAGE Possible choices for configuration are:\r\n!MESSAGE \r\n!MESSAGE \"SOIL - Win32 Release\" (based on \"Win32 (x86) Static Library\")\r\n!MESSAGE \"SOIL - Win32 Debug\" (based on \"Win32 (x86) Static Library\")\r\n!MESSAGE \r\n\r\n# Begin Project\r\n# PROP AllowPerConfigDependencies 0\r\n# PROP Scc_ProjName \"\"\r\n# PROP Scc_LocalPath \"\"\r\nCPP=cl.exe\r\nRSC=rc.exe\r\n\r\n!IF  \"$(CFG)\" == \"SOIL - Win32 Release\"\r\n\r\n# PROP BASE Use_MFC 0\r\n# PROP BASE Use_Debug_Libraries 0\r\n# PROP BASE Output_Dir \"Release\"\r\n# PROP BASE Intermediate_Dir \"Release\"\r\n# PROP BASE Target_Dir \"\"\r\n# PROP Use_MFC 0\r\n# PROP Use_Debug_Libraries 0\r\n# PROP Output_Dir \"Release\"\r\n# PROP Intermediate_Dir \"Release\"\r\n# PROP Target_Dir \"\"\r\n# ADD BASE CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_MBCS\" /D \"_LIB\" /YX /FD /c\r\n# ADD CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_MBCS\" /D \"_LIB\" /YX /FD /c\r\n# ADD BASE RSC /l 0x409 /d \"NDEBUG\"\r\n# ADD RSC /l 0x409 /d \"NDEBUG\"\r\nBSC32=bscmake.exe\r\n# ADD BASE BSC32 /nologo\r\n# ADD BSC32 /nologo\r\nLIB32=link.exe -lib\r\n# ADD BASE LIB32 /nologo\r\n# ADD LIB32 /nologo\r\n\r\n!ELSEIF  \"$(CFG)\" == \"SOIL - Win32 Debug\"\r\n\r\n# PROP BASE Use_MFC 0\r\n# PROP BASE Use_Debug_Libraries 1\r\n# PROP BASE Output_Dir \"Debug\"\r\n# PROP BASE Intermediate_Dir \"Debug\"\r\n# PROP BASE Target_Dir \"\"\r\n# PROP Use_MFC 0\r\n# PROP Use_Debug_Libraries 1\r\n# PROP Output_Dir \"Debug\"\r\n# PROP Intermediate_Dir \"Debug\"\r\n# PROP Target_Dir \"\"\r\n# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_MBCS\" /D \"_LIB\" /YX /FD /GZ /c\r\n# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_MBCS\" /D \"_LIB\" /YX /FD /GZ /c\r\n# ADD BASE RSC /l 0x409 /d \"_DEBUG\"\r\n# ADD RSC /l 0x409 /d \"_DEBUG\"\r\nBSC32=bscmake.exe\r\n# ADD BASE BSC32 /nologo\r\n# ADD BSC32 /nologo\r\nLIB32=link.exe -lib\r\n# ADD BASE LIB32 /nologo\r\n# ADD LIB32 /nologo\r\n\r\n!ENDIF \r\n\r\n# Begin Target\r\n\r\n# Name \"SOIL - Win32 Release\"\r\n# Name \"SOIL - Win32 Debug\"\r\n# Begin Group \"Source Files\"\r\n\r\n# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\image_DXT.c\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\image_helper.c\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\SOIL.c\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\stb_image_aug.c\r\n# End Source File\r\n# End Group\r\n# Begin Group \"Header Files\"\r\n\r\n# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\image_DXT.h\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\image_helper.h\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\SOIL.h\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\stb_image_aug.h\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\stbi_DDS_aug.h\r\n# End Source File\r\n# Begin Source File\r\n\r\nSOURCE=..\\..\\src\\stbi_DDS_aug_c.h\r\n# End Source File\r\n# End Group\r\n# End Target\r\n# End Project\r\n"
  },
  {
    "path": "projects/VC6/SOIL.dsw",
    "content": "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n\r\n###############################################################################\r\n\r\nProject: \"SOIL\"=\".\\SOIL.dsp\" - Package Owner=<4>\r\n\r\nPackage=<5>\r\n{{{\r\n}}}\r\n\r\nPackage=<4>\r\n{{{\r\n}}}\r\n\r\n###############################################################################\r\n\r\nGlobal:\r\n\r\nPackage=<5>\r\n{{{\r\n}}}\r\n\r\nPackage=<3>\r\n{{{\r\n}}}\r\n\r\n###############################################################################\r\n\r\n"
  },
  {
    "path": "projects/VC7.1/SOIL.sln",
    "content": "Microsoft Visual Studio Solution File, Format Version 8.00\r\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"SOIL\", \"SOIL.vcproj\", \"{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}\"\r\n\tProjectSection(ProjectDependencies) = postProject\r\n\tEndProjectSection\r\nEndProject\r\nGlobal\r\n\tGlobalSection(SolutionConfiguration) = preSolution\r\n\t\tDebug = Debug\r\n\t\tRelease = Release\r\n\tEndGlobalSection\r\n\tGlobalSection(ProjectConfiguration) = postSolution\r\n\t\t{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}.Debug.ActiveCfg = Debug|Win32\r\n\t\t{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}.Debug.Build.0 = Debug|Win32\r\n\t\t{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}.Release.ActiveCfg = Release|Win32\r\n\t\t{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}.Release.Build.0 = Release|Win32\r\n\tEndGlobalSection\r\n\tGlobalSection(ExtensibilityGlobals) = postSolution\r\n\tEndGlobalSection\r\n\tGlobalSection(ExtensibilityAddIns) = postSolution\r\n\tEndGlobalSection\r\nEndGlobal\r\n"
  },
  {
    "path": "projects/VC7.1/SOIL.vcproj",
    "content": "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\r\n<VisualStudioProject\r\n\tProjectType=\"Visual C++\"\r\n\tVersion=\"7.10\"\r\n\tName=\"SOIL\"\r\n\tProjectGUID=\"{35D9B7E3-EE73-4C06-9B98-FCB7F7644C99}\"\r\n\tKeyword=\"Win32Proj\">\r\n\t<Platforms>\r\n\t\t<Platform\r\n\t\t\tName=\"Win32\"/>\r\n\t</Platforms>\r\n\t<Configurations>\r\n\t\t<Configuration\r\n\t\t\tName=\"Debug|Win32\"\r\n\t\t\tOutputDirectory=\"Debug\"\r\n\t\t\tIntermediateDirectory=\"Debug\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"2\">\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tOptimization=\"0\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;_DEBUG;_LIB\"\r\n\t\t\t\tMinimalRebuild=\"TRUE\"\r\n\t\t\t\tBasicRuntimeChecks=\"3\"\r\n\t\t\t\tRuntimeLibrary=\"5\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\r\n\t\t\t\tDebugInformationFormat=\"4\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t\tOutputFile=\"$(OutDir)/SOIL.lib\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedWrapperGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCAuxiliaryManagedWrapperGeneratorTool\"/>\r\n\t\t</Configuration>\r\n\t\t<Configuration\r\n\t\t\tName=\"Release|Win32\"\r\n\t\t\tOutputDirectory=\"Release\"\r\n\t\t\tIntermediateDirectory=\"Release\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"2\">\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_LIB\"\r\n\t\t\t\tRuntimeLibrary=\"4\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\r\n\t\t\t\tDebugInformationFormat=\"3\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t\tOutputFile=\"$(OutDir)/SOIL.lib\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedWrapperGeneratorTool\"/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCAuxiliaryManagedWrapperGeneratorTool\"/>\r\n\t\t</Configuration>\r\n\t</Configurations>\r\n\t<References>\r\n\t</References>\r\n\t<Files>\r\n\t\t<Filter\r\n\t\t\tName=\"Source Files\"\r\n\t\t\tFilter=\"cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx\"\r\n\t\t\tUniqueIdentifier=\"{4FC737F1-C7A5-4376-A066-2A32D752A2FF}\">\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.c\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.c\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.c\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.c\">\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Header Files\"\r\n\t\t\tFilter=\"h;hpp;hxx;hm;inl;inc;xsd\"\r\n\t\t\tUniqueIdentifier=\"{93995380-89BD-4b04-88EB-625FBE52EBFB}\">\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.h\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.h\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.h\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.h\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug.h\">\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug_c.h\">\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Resource Files\"\r\n\t\t\tFilter=\"rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx\"\r\n\t\t\tUniqueIdentifier=\"{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}\">\r\n\t\t</Filter>\r\n\t\t<File\r\n\t\t\tRelativePath=\".\\ReadMe.txt\">\r\n\t\t</File>\r\n\t</Files>\r\n\t<Globals>\r\n\t</Globals>\r\n</VisualStudioProject>\r\n"
  },
  {
    "path": "projects/VC8/SOIL.sln",
    "content": "﻿\r\nMicrosoft Visual Studio Solution File, Format Version 9.00\r\n# Visual Studio 2005\r\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"SOIL\", \"SOIL.vcproj\", \"{C32FB2B4-500C-43CD-A099-EECCE079D3F1}\"\r\nEndProject\r\nGlobal\r\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\r\n\t\tDebug|Win32 = Debug|Win32\r\n\t\tRelease|Win32 = Release|Win32\r\n\tEndGlobalSection\r\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|Win32.ActiveCfg = Debug|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|Win32.Build.0 = Debug|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|Win32.ActiveCfg = Release|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|Win32.Build.0 = Release|Win32\r\n\tEndGlobalSection\r\n\tGlobalSection(SolutionProperties) = preSolution\r\n\t\tHideSolutionNode = FALSE\r\n\tEndGlobalSection\r\nEndGlobal\r\n"
  },
  {
    "path": "projects/VC8/SOIL.vcproj",
    "content": "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\r\n<VisualStudioProject\r\n\tProjectType=\"Visual C++\"\r\n\tVersion=\"8.00\"\r\n\tName=\"SOIL\"\r\n\tProjectGUID=\"{C32FB2B4-500C-43CD-A099-EECCE079D3F1}\"\r\n\tRootNamespace=\"SOIL\"\r\n\tKeyword=\"Win32Proj\"\r\n\t>\r\n\t<Platforms>\r\n\t\t<Platform\r\n\t\t\tName=\"Win32\"\r\n\t\t/>\r\n\t</Platforms>\r\n\t<ToolFiles>\r\n\t</ToolFiles>\r\n\t<Configurations>\r\n\t\t<Configuration\r\n\t\t\tName=\"Debug|Win32\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tOptimization=\"0\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;_DEBUG;_LIB\"\r\n\t\t\t\tMinimalRebuild=\"true\"\r\n\t\t\t\tBasicRuntimeChecks=\"3\"\r\n\t\t\t\tRuntimeLibrary=\"3\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"4\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t\t<Configuration\r\n\t\t\tName=\"Release|Win32\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\tWholeProgramOptimization=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_LIB\"\r\n\t\t\t\tRuntimeLibrary=\"2\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t</Configurations>\r\n\t<References>\r\n\t</References>\r\n\t<Files>\r\n\t\t<Filter\r\n\t\t\tName=\"Source Files\"\r\n\t\t\tFilter=\"cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx\"\r\n\t\t\tUniqueIdentifier=\"{4FC737F1-C7A5-4376-A066-2A32D752A2FF}\"\r\n\t\t\t>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Header Files\"\r\n\t\t\tFilter=\"h;hpp;hxx;hm;inl;inc;xsd\"\r\n\t\t\tUniqueIdentifier=\"{93995380-89BD-4b04-88EB-625FBE52EBFB}\"\r\n\t\t\t>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug_c.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Resource Files\"\r\n\t\t\tFilter=\"rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav\"\r\n\t\t\tUniqueIdentifier=\"{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}\"\r\n\t\t\t>\r\n\t\t</Filter>\r\n\t\t<File\r\n\t\t\tRelativePath=\".\\ReadMe.txt\"\r\n\t\t\t>\r\n\t\t</File>\r\n\t</Files>\r\n\t<Globals>\r\n\t</Globals>\r\n</VisualStudioProject>\r\n"
  },
  {
    "path": "projects/VC9/SOIL.sln",
    "content": "﻿\r\nMicrosoft Visual Studio Solution File, Format Version 10.00\r\n# Visual Studio 2008\r\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"SOIL\", \"SOIL.vcproj\", \"{C32FB2B4-500C-43CD-A099-EECCE079D3F1}\"\r\nEndProject\r\nGlobal\r\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\r\n\t\tDebug|Win32 = Debug|Win32\r\n\t\tDebug|x64 = Debug|x64\r\n\t\tRelease|Win32 = Release|Win32\r\n\t\tRelease|x64 = Release|x64\r\n\tEndGlobalSection\r\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|Win32.ActiveCfg = Debug|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|Win32.Build.0 = Debug|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|x64.ActiveCfg = Debug|x64\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Debug|x64.Build.0 = Debug|x64\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|Win32.ActiveCfg = Release|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|Win32.Build.0 = Release|Win32\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|x64.ActiveCfg = Release|x64\r\n\t\t{C32FB2B4-500C-43CD-A099-EECCE079D3F1}.Release|x64.Build.0 = Release|x64\r\n\tEndGlobalSection\r\n\tGlobalSection(SolutionProperties) = preSolution\r\n\t\tHideSolutionNode = FALSE\r\n\tEndGlobalSection\r\nEndGlobal\r\n"
  },
  {
    "path": "projects/VC9/SOIL.vcproj",
    "content": "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\r\n<VisualStudioProject\r\n\tProjectType=\"Visual C++\"\r\n\tVersion=\"9.00\"\r\n\tName=\"SOIL\"\r\n\tProjectGUID=\"{C32FB2B4-500C-43CD-A099-EECCE079D3F1}\"\r\n\tRootNamespace=\"SOIL\"\r\n\tKeyword=\"Win32Proj\"\r\n\tTargetFrameworkVersion=\"131072\"\r\n\t>\r\n\t<Platforms>\r\n\t\t<Platform\r\n\t\t\tName=\"Win32\"\r\n\t\t/>\r\n\t\t<Platform\r\n\t\t\tName=\"x64\"\r\n\t\t/>\r\n\t</Platforms>\r\n\t<ToolFiles>\r\n\t</ToolFiles>\r\n\t<Configurations>\r\n\t\t<Configuration\r\n\t\t\tName=\"Debug|Win32\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tOptimization=\"0\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;_DEBUG;_LIB\"\r\n\t\t\t\tMinimalRebuild=\"true\"\r\n\t\t\t\tBasicRuntimeChecks=\"3\"\r\n\t\t\t\tRuntimeLibrary=\"3\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"4\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t\t<Configuration\r\n\t\t\tName=\"Release|Win32\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\tWholeProgramOptimization=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_LIB\"\r\n\t\t\t\tRuntimeLibrary=\"2\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t\t<Configuration\r\n\t\t\tName=\"Debug|x64\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(PlatformName)\\$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(PlatformName)\\$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t\tTargetEnvironment=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tOptimization=\"0\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;_DEBUG;_LIB\"\r\n\t\t\t\tMinimalRebuild=\"true\"\r\n\t\t\t\tBasicRuntimeChecks=\"3\"\r\n\t\t\t\tRuntimeLibrary=\"3\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t\t<Configuration\r\n\t\t\tName=\"Release|x64\"\r\n\t\t\tOutputDirectory=\"$(SolutionDir)$(PlatformName)\\$(ConfigurationName)\"\r\n\t\t\tIntermediateDirectory=\"$(PlatformName)\\$(ConfigurationName)\"\r\n\t\t\tConfigurationType=\"4\"\r\n\t\t\tCharacterSet=\"1\"\r\n\t\t\tWholeProgramOptimization=\"1\"\r\n\t\t\t>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreBuildEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCustomBuildTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXMLDataGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCMIDLTool\"\r\n\t\t\t\tTargetEnvironment=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCCLCompilerTool\"\r\n\t\t\t\tPreprocessorDefinitions=\"WIN32;NDEBUG;_LIB\"\r\n\t\t\t\tRuntimeLibrary=\"2\"\r\n\t\t\t\tUsePrecompiledHeader=\"0\"\r\n\t\t\t\tWarningLevel=\"3\"\r\n\t\t\t\tDetect64BitPortabilityProblems=\"true\"\r\n\t\t\t\tDebugInformationFormat=\"3\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCManagedResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCResourceCompilerTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPreLinkEventTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCLibrarianTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCALinkTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCXDCMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCBscMakeTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCFxCopTool\"\r\n\t\t\t/>\r\n\t\t\t<Tool\r\n\t\t\t\tName=\"VCPostBuildEventTool\"\r\n\t\t\t/>\r\n\t\t</Configuration>\r\n\t</Configurations>\r\n\t<References>\r\n\t</References>\r\n\t<Files>\r\n\t\t<Filter\r\n\t\t\tName=\"Source Files\"\r\n\t\t\tFilter=\"cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx\"\r\n\t\t\tUniqueIdentifier=\"{4FC737F1-C7A5-4376-A066-2A32D752A2FF}\"\r\n\t\t\t>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.c\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Header Files\"\r\n\t\t\tFilter=\"h;hpp;hxx;hm;inl;inc;xsd\"\r\n\t\t\tUniqueIdentifier=\"{93995380-89BD-4b04-88EB-625FBE52EBFB}\"\r\n\t\t\t>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_DXT.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\image_helper.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\SOIL.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stb_image_aug.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t\t<File\r\n\t\t\t\tRelativePath=\"..\\..\\src\\stbi_DDS_aug_c.h\"\r\n\t\t\t\t>\r\n\t\t\t</File>\r\n\t\t</Filter>\r\n\t\t<Filter\r\n\t\t\tName=\"Resource Files\"\r\n\t\t\tFilter=\"rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav\"\r\n\t\t\tUniqueIdentifier=\"{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}\"\r\n\t\t\t>\r\n\t\t</Filter>\r\n\t\t<File\r\n\t\t\tRelativePath=\".\\ReadMe.txt\"\r\n\t\t\t>\r\n\t\t</File>\r\n\t</Files>\r\n\t<Globals>\r\n\t</Globals>\r\n</VisualStudioProject>\r\n"
  },
  {
    "path": "projects/codeblocks/SOIL.cbp",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n<CodeBlocks_project_file>\n\t<FileVersion major=\"1\" minor=\"6\" />\n\t<Project>\n\t\t<Option title=\"Simple OpenGL Image Library\" />\n\t\t<Option pch_mode=\"2\" />\n\t\t<Option compiler=\"gcc\" />\n\t\t<Build>\n\t\t\t<Target title=\"lib-Release\">\n\t\t\t\t<Option output=\"..\\..\\lib\\libSOIL\" prefix_auto=\"1\" extension_auto=\"1\" />\n\t\t\t\t<Option working_dir=\"\" />\n\t\t\t\t<Option object_output=\"obj\\Release\\\" />\n\t\t\t\t<Option type=\"2\" />\n\t\t\t\t<Option compiler=\"gcc\" />\n\t\t\t\t<Option createDefFile=\"1\" />\n\t\t\t\t<Compiler>\n\t\t\t\t\t<Add option=\"-Os\" />\n\t\t\t\t\t<Add option=\"-O2\" />\n\t\t\t\t</Compiler>\n\t\t\t\t<Linker>\n\t\t\t\t\t<Add option=\"-s\" />\n\t\t\t\t</Linker>\n\t\t\t</Target>\n\t\t\t<Target title=\"test-Debug\">\n\t\t\t\t<Option output=\"..\\..\\testSOIL\" prefix_auto=\"1\" extension_auto=\"1\" />\n\t\t\t\t<Option working_dir=\"..\\..\\\" />\n\t\t\t\t<Option object_output=\"obj\\\" />\n\t\t\t\t<Option type=\"1\" />\n\t\t\t\t<Option compiler=\"gcc\" />\n\t\t\t\t<Compiler>\n\t\t\t\t\t<Add option=\"-O\" />\n\t\t\t\t\t<Add option=\"-pg\" />\n\t\t\t\t\t<Add option=\"-g\" />\n\t\t\t\t</Compiler>\n\t\t\t\t<Linker>\n\t\t\t\t\t<Add option=\"-pg -lgmon\" />\n\t\t\t\t\t<Add library=\"opengl32\" />\n\t\t\t\t\t<Add library=\"gdi32\" />\n\t\t\t\t</Linker>\n\t\t\t</Target>\n\t\t\t<Target title=\"test-Release\">\n\t\t\t\t<Option output=\"..\\..\\testSOIL\" prefix_auto=\"1\" extension_auto=\"1\" />\n\t\t\t\t<Option working_dir=\"..\\..\\\" />\n\t\t\t\t<Option object_output=\"obj\\\" />\n\t\t\t\t<Option type=\"1\" />\n\t\t\t\t<Option compiler=\"gcc\" />\n\t\t\t\t<Compiler>\n\t\t\t\t\t<Add option=\"-Os\" />\n\t\t\t\t\t<Add option=\"-O2\" />\n\t\t\t\t</Compiler>\n\t\t\t\t<Linker>\n\t\t\t\t\t<Add option=\"-s\" />\n\t\t\t\t\t<Add library=\"opengl32\" />\n\t\t\t\t\t<Add library=\"gdi32\" />\n\t\t\t\t</Linker>\n\t\t\t</Target>\n\t\t</Build>\n\t\t<Compiler>\n\t\t\t<Add option=\"-Wall\" />\n\t\t</Compiler>\n\t\t<Unit filename=\"..\\..\\src\\SOIL.c\">\n\t\t\t<Option compilerVar=\"CC\" />\n\t\t</Unit>\n\t\t<Unit filename=\"..\\..\\src\\SOIL.h\" />\n\t\t<Unit filename=\"..\\..\\src\\image_DXT.c\">\n\t\t\t<Option compilerVar=\"CC\" />\n\t\t</Unit>\n\t\t<Unit filename=\"..\\..\\src\\image_DXT.h\" />\n\t\t<Unit filename=\"..\\..\\src\\image_helper.c\">\n\t\t\t<Option compilerVar=\"CC\" />\n\t\t</Unit>\n\t\t<Unit filename=\"..\\..\\src\\image_helper.h\" />\n\t\t<Unit filename=\"..\\..\\src\\stb_image_aug.c\">\n\t\t\t<Option compilerVar=\"CC\" />\n\t\t</Unit>\n\t\t<Unit filename=\"..\\..\\src\\stb_image_aug.h\" />\n\t\t<Unit filename=\"..\\..\\src\\stbi_DDS_aug.h\" />\n\t\t<Unit filename=\"..\\..\\src\\stbi_DDS_aug_c.h\" />\n\t\t<Unit filename=\"..\\..\\src\\test_SOIL.cpp\">\n\t\t\t<Option target=\"test-Debug\" />\n\t\t\t<Option target=\"test-Release\" />\n\t\t</Unit>\n\t\t<Extensions>\n\t\t\t<code_completion />\n\t\t\t<envvars />\n\t\t\t<debugger />\n\t\t</Extensions>\n\t</Project>\n</CodeBlocks_project_file>\n"
  },
  {
    "path": "soil.html",
    "content": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n<HTML>\r\n<HEAD>\r\n\t<META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=windows-1252\">\r\n\t<TITLE>lonesock.net: SOIL</TITLE>\r\n<style type=\"text/css\">\r\n<!--\r\nbody { color: #000000; background-color: #FFFFFF; }\r\n.style1 { color: #505050; }\r\n.style2 { color: #A0A0A0; }\r\n.style3 { color: #8080FF; font-weight: bold; }\r\n.style15 { color: #8080FF; font-weight: bold; }\r\n.style17 { color: #008080; }\r\n.style18 { color: #800000; }\r\n.style4 { color: #F000F0; }\r\n.style5 { color: #0000A0; font-weight: bold; }\r\n.style16 { color: #00A000; font-weight: bold; }\r\n.style6 { color: #0000FF; }\r\n.style12 { color: #0000FF; }\r\n.style7 { color: #E0A000; }\r\n.style8 { color: #000000; }\r\n.style9 { color: #00A000; }\r\n.style10 { color: #FF0000; }\r\n.style34 { color: #000000; background-color: #80FFFF; font-weight: bold; }\r\n.style35 { color: #FFFFFF; background-color: #FF0000; font-weight: bold; }\r\n--></style>\r\n</HEAD>\r\n<BODY LANG=\"en-US\" DIR=\"LTR\">\r\n<PRE STYLE=\"margin-bottom: 0.2in; text-align: center\">\r\n<FONT SIZE=4 STYLE=\"font-size: 16pt\"><B>Simple OpenGL Image Library</B></FONT>\r\n</PRE>\r\n<P>\r\n<B>Introduction:</B><BR><BR>SOIL is a tiny C library used primarily\r\nfor uploading textures into OpenGL. It is based on stb_image version\r\n1.16, the public domain code from Sean Barrett (found <A HREF=\"http://www.nothings.org/stb_image.c\">here</A>).\r\nI have extended it to load TGA and DDS files, and to perform common\r\nfunctions needed in loading OpenGL textures.  SOIL can also be used\r\nto save and load images in a variety of formats (useful for loading\r\nheight maps, non-OpenGL applications, etc.)<BR>\r\n<BR>\r\n<B>Download:</B>\r\n<BR>\r\n<BR>\r\nYou can grab the latest version of SOIL <A HREF=\"http://www.lonesock.net/files/soil.zip\">here</A>.\r\n(July 7, 2008: see the change log at the bottom of this page.)<BR>\r\nYou can also checkout the latest code from the new SVN repository, login as guest/guest:<BR>\r\n<A HREF=\"svn://www.twisted-works.com/jdummer/public/SOIL\">svn://www.twisted-works.com/jdummer/public/SOIL</A><BR>\r\n(thanks for the SVN hosting, Sherief!)\r\n<BR>\r\n<BR>\r\n<B>License:</B>\r\n<BR>\r\n<BR>\r\nPublic Domain\r\n<BR>\r\n<BR>\r\n<B>Features:</B> \r\n</P>\r\n<UL>\r\n\t<LI>Readable Image Formats:\r\n\t<UL>\r\n\t\t<LI>BMP - non-1bpp, non-RLE (from stb_image documentation) \r\n\t\t<LI>PNG - non-interlaced (from stb_image documentation) \r\n\t\t<LI>JPG - JPEG baseline (from stb_image documentation) \r\n\t\t<LI>TGA - greyscale or RGB or RGBA or indexed, uncompressed or RLE\r\n\t\t<LI>DDS - DXT1/2/3/4/5, uncompressed, cubemaps (can't read 3D DDS files yet)\r\n\t\t<LI>PSD - (from stb_image documentation)\r\n\t\t<LI>HDR - converted to LDR, unless loaded with *HDR* functions (RGBE or RGBdivA or RGBdivA2)\r\n\t</UL>\r\n\t<LI>Writeable Image Formats:\r\n\t<UL>\r\n\t\t<LI>TGA - Greyscale or RGB or RGBA, uncompressed\r\n\t\t<LI>BMP - RGB, uncompressed\r\n\t\t<LI>DDS - RGB as DXT1, or RGBA as DXT5\r\n\t</UL>\r\n\t<LI>Can load an image file directly into a 2D OpenGL texture, optionally performing the following functions:\r\n\t<UL>\r\n\t\t<LI>Can generate a new texture handle, or reuse one specified\r\n\t\t<LI>Can automatically rescale the image to the next largest power-of-two size\r\n\t\t<LI>Can automatically create MIPmaps\r\n\t\t<LI>Can scale (not simply clamp) the RGB values into the \"safe range\" for NTSC displays (16 to 235, as recommended <a href=\"http://msdn2.microsoft.com/en-us/library/bb174608.aspx#NTSC_Suggestions\">here</a>)\r\n\t\t<LI>Can multiply alpha on load (for more correct blending / compositing)\r\n\t\t<LI>Can flip the image vertically\r\n\t\t<LI>Can compress and upload any image as DXT1 or DXT5 (if EXT_texture_compression_s3tc is available), using an internal (very fast!) compressor\r\n\t\t<LI>Can convert the RGB to YCoCg color space (useful with DXT5 compression: see <a href=\"http://developer.nvidia.com/object/real-time-ycocg-dxt-compression.html\">this link</a> from NVIDIA)\r\n\t\t<LI>Will automatically downsize a texture if it is larger than GL_MAX_TEXTURE_SIZE\r\n\t\t<LI>Can directly upload DDS files (DXT1/3/5/uncompressed/cubemap, with or without MIPmaps).  Note: directly uploading the compressed DDS image will disable the other options (no flipping, no pre-multiplying alpha, no rescaling, no creation of MIPmaps, no auto-downsizing)\r\n\t\t<LI>Can load rectangluar textures for GUI elements or splash screens (requires GL_ARB/EXT/NV_texture_rectangle)\r\n\t</UL>\r\n\t<LI>Can decompress images from RAM (e.g. via <a href=\"http://icculus.org/physfs/\">PhysicsFS</a> or similar) into an OpenGL texture (same features as regular 2D textures, above)\r\n\t<LI>Can load cube maps directly into an OpenGL texture (same features as regular 2D textures, above)\r\n\t<UL>\r\n\t\t<LI>Can take six image files directly into an OpenGL cube map texture \r\n\t\t<LI>Can take a single image file where width = 6*height (or vice versa), split it into an OpenGL cube map texture\r\n\t</UL>\r\n\t<LI>No external dependencies\r\n\t<LI>Tiny\r\n\t<LI>Cross platform (Windows, *nix, Mac OS X)\r\n\t<LI>Public Domain\r\n</UL>\r\n\r\n<PRE STYLE=\"margin-bottom: 0.2in\"><B>ToDo:</B></PRE>\r\n<UL>\r\n\t<LI>More testing\r\n\t<LI>add HDR functions to load from memory and load to RGBE unsigned char*\r\n</UL>\r\n<P><BR><B>Usage:</B><BR><BR>SOIL is meant to be used as a static\r\nlibrary (as it's tiny and in the public domain). You can use the static \r\nlibrary file included in the zip (libSOIL.a works for MinGW and Microsoft \r\ncompilers...feel free to rename it to SOIL.lib if that makes you happy), \r\nor compile the library yourself. The code is cross-platform and has been \r\ntested on Windows, Linux, and Mac.  (The heaviest testing has been on the \r\nWindows platform, so feel free to email me if you find any issues with \r\nother platforms.)\r\n<BR><BR>Simply include SOIL.h in your C or C++ file, \r\nlink in the static library, and then use any of SOIL's functions. The \r\nfile SOIL.h contains simple doxygen style documentation. (If you use the \r\nstatic library, no other header files are needed besides SOIL.h)  Below \r\nare some simple usage examples: \r\n</P>\r\n<pre>\r\n<code><span style=\"font: 8pt Courier New;\"><span class=\"style1\">/* load an image file directly as a new OpenGL texture */\r\n</span><span class=\"style11\">GLuint tex_2d </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_texture\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"img.png\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_AUTO</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_CREATE_NEW_ID</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_MIPMAPS </span><span class=\"style10\">| </span><span class=\"style11\">SOIL_FLAG_INVERT_Y </span><span class=\"style10\">| </span><span class=\"style11\">SOIL_FLAG_NTSC_SAFE_RGB </span><span class=\"style10\">| </span><span class=\"style11\">SOIL_FLAG_COMPRESS_TO_DXT\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* check for an error during the load process */\r\n</span><span class=\"style5\">if</span><span class=\"style10\">( </span><span class=\"style4\">0 </span><span class=\"style10\">== </span><span class=\"style11\">tex_2d </span><span class=\"style10\">)\r\n{\r\n\t</span><span class=\"style11\">printf</span><span class=\"style10\">( </span><span class=\"style6\">\"SOIL loading error: '%s'\\n\"</span><span class=\"style10\">, </span><span class=\"style11\">SOIL_last_result</span><span class=\"style10\">() );\r\n}\r\n\r\n</span><span class=\"style1\">/* load another image, but into the same texture ID, overwriting the last one */\r\n</span><span class=\"style11\">tex_2d </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_texture\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"some_other_img.dds\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_AUTO</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">tex_2d</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* load 6 images into a new OpenGL cube map, forcing RGB */\r\n</span><span class=\"style11\">GLuint tex_cube </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_cubemap\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"xp.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"xn.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"yp.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"yn.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"zp.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"zn.jpg\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_RGB</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_CREATE_NEW_ID</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_MIPMAPS\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* load and split a single image into a new OpenGL cube map, default format */\r\n</span><span class=\"style1\">/* face order = East South West North Up Down => \"ESWNUD\", case sensitive! */\r\n</span><span class=\"style11\">GLuint single_tex_cube </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_single_cubemap\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"split_cubemap.png\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style6\">\"EWUDNS\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_AUTO</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_CREATE_NEW_ID</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_MIPMAPS\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* actually, load a DDS cubemap over the last OpenGL cube map, default format */\r\n</span><span class=\"style1\">/* try to load it directly, but give the order of the faces in case that fails */\r\n</span><span class=\"style1\">/* the DDS cubemap face order is pre-defined as SOIL_DDS_CUBEMAP_FACE_ORDER */\r\n</span><span class=\"style11\">single_tex_cube </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_single_cubemap\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"overwrite_cubemap.dds\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_DDS_CUBEMAP_FACE_ORDER</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_AUTO</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">single_tex_cube</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* load an image as a heightmap, forcing greyscale (so channels should be 1) */\r\n</span><span class=\"style5\">int </span><span class=\"style11\">width</span><span class=\"style10\">, </span><span class=\"style11\">height</span><span class=\"style10\">, </span><span class=\"style11\">channels</span><span class=\"style10\">;\r\n</span><span class=\"style5\">unsigned char </span><span class=\"style10\">*</span><span class=\"style11\">ht_map </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_image\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"terrain.tga\"</span><span class=\"style10\">,\r\n\t\t&amp;</span><span class=\"style11\">width</span><span class=\"style10\">, &amp;</span><span class=\"style11\">height</span><span class=\"style10\">, &amp;</span><span class=\"style11\">channels</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_L\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* save that image as another type */\r\n</span><span class=\"style5\">int </span><span class=\"style11\">save_result </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_save_image\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"new_terrain.dds\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_SAVE_TYPE_DDS</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">width</span><span class=\"style10\">, </span><span class=\"style11\">height</span><span class=\"style10\">, </span><span class=\"style11\">channels</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">ht_map\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* save a screenshot of your awesome OpenGL game engine, running at 1024x768 */\r\n</span><span class=\"style11\">save_result </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_save_screenshot\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style6\">\"awesomenessity.bmp\"</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_SAVE_TYPE_BMP</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style4\">0</span><span class=\"style10\">, </span><span class=\"style4\">0</span><span class=\"style10\">, </span><span class=\"style4\">1024</span><span class=\"style10\">, </span><span class=\"style4\">768\r\n\t</span><span class=\"style10\">);\r\n\r\n</span><span class=\"style1\">/* loaded a file via PhysicsFS, need to decompress the image from RAM, */\r\n</span><span class=\"style1\">/* where it's in a buffer: unsigned char *image_in_RAM */\r\n</span><span class=\"style11\">GLuint tex_2d_from_RAM </span><span class=\"style10\">= </span><span class=\"style11\">SOIL_load_OGL_texture_from_memory\r\n\t</span><span class=\"style10\">(\r\n\t\t</span><span class=\"style11\">image_in_RAM</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">image_in_RAM_bytes</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_LOAD_AUTO</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_CREATE_NEW_ID</span><span class=\"style10\">,\r\n\t\t</span><span class=\"style11\">SOIL_FLAG_MIPMAPS </span><span class=\"style10\">| </span><span class=\"style11\">SOIL_FLAG_INVERT_Y </span><span class=\"style10\">| </span><span class=\"style11\">SOIL_FLAG_COMPRESS_TO_DXT\r\n\t</span><span class=\"style10\">);\r\n\t\r\n</span><span class=\"style1\">/* done with the heightmap, free up the RAM */\r\n</span><span class=\"style11\">SOIL_free_image_data</span><span class=\"style10\">( </span><span class=\"style11\">ht_map </span><span class=\"style10\">);</span></span>\r\n</code></pre>\r\n<BR>\r\n<BR>\r\n<B>Change Log:</B>\r\n<UL>\r\n<LI>July 7, 2008\r\n<UL>\r\n\t<LI>upgraded to stb_image 1.16 (threadsafe! loads PSD and HDR formats)\r\n\t<LI>removed <B>inline</B> keyword from native SOIL functions (thanks Sherief, Boder, Amnesiac5!)\r\n\t<LI>added SOIL_load_OGL_HDR_texture (loads a Radience HDR file into RGBE, RGB/a, RGB/A^2)\r\n\t<LI>fixed a potential bug loading DDS files with a filename\r\n\t<LI>added a VC9 project file (thanks Sherief!)\r\n</UL>\r\n<LI>November 10, 2007: added SOIL_FLAG_TEXTURE_RECTANGLE (pixel addressed non POT, useful for GUI, splash screens, etc.).  Not useful with cubemaps, and disables repeating and MIPmaps.\r\n<LI>November 8, 2007\r\n<UL>\r\n\t<LI>upgraded to stb_image 1.07\r\n\t<LI>fixed some includes and defines for compiling on OS X (thanks Mogui and swiftcoder!)\r\n</UL>\r\n<LI>October 30, 2007\r\n<UL>\r\n\t<LI>upgraded to stb_image 1.04, some tiny bug fixes\r\n\t<LI>there is now a makefile (under projects) for ease of building under Linux (thanks D J Peters!)\r\n\t<LI>Visual Studio 6/2003/2005 projects are working again\r\n\t<LI>patched SOIL for better pointer handling of the glCompressedTexImage2D extension (thanks Peter Sperl!)\r\n\t<LI>fixed DDS loading when force_channels=4 but there was no alpha; it was returning 3 channels.  (Thanks LaurentGom!)\r\n\t<LI>fixed a bunch of channel issues in general.  (Thanks Sean Barrett!)\r\n</UL>\r\n<LI>October 27, 2007\r\n<UL>\r\n\t<LI>correctly reports when there is no OpenGL context (thanks Merick Zero!)\r\n\t<LI>upgraded to stb_image 1.03 with support for loading the HDR image format\r\n\t<LI>fixed loading JPEG images while forcing the number of channels (e.g. to RGBA)\r\n\t<LI>changed SOIL_DDS_CUBEMAP_FACE_ORDER to a #define (thanks Dancho!)\r\n\t<LI>reorganized my additions to stb_image (you can define STBI_NO_DDS to compile SOIL without DDS support)\r\n\t<LI>added SOIL_FLAG_CoCg_Y, will convert RGB or RGBA to YCoCg color space (<a href=\"http://developer.nvidia.com/object/real-time-ycocg-dxt-compression.html\">link</a>)\r\n</UL>\r\n<LI>October 5, 2007\r\n<UL>\r\n\t<LI>added SOIL_FLAG_NTSC_SAFE_RGB\r\n\t<LI>bugfixed & optimized up_scale_image (used with SOIL_FLAG_POWER_OF_TWO and SOIL_FLAG_MIPMAPS)\r\n</UL>\r\n<LI>September 20, 2007\r\n<UL>\r\n\t<LI>upgraded to stb_image 1.0\r\n\t<LI>added the DXT source files to the MSVS projects\r\n\t<LI>removed sqrtf() calls (VS2k3 could not handle them)\r\n\t<LI>distributing only 1 library file (libSOIL.a, compiled with MinGW 4.2.1 tech preview!) for all windows compilers\r\n\t<LI>added an example of the *_from_memory() functions to the Usage section\r\n</UL>\r\n<LI>September 6, 2007\r\n<UL>\r\n\t<LI>added a slew of SOIL_load_*_from_memory() functions for people using PhysicsFS or similar\r\n\t<LI>more robust loading of non-compliant DDS files (thanks Dan!)\r\n</UL>\r\n<LI>September 1, 2007 - fixed bugs from the last update [8^)\r\n<LI>August 31, 2007\r\n<UL>\r\n\t<LI>can load uncompressed and cubemap DDS files\r\n\t<LI>can create a cubemap texture from a single (stitched) image file of any type\r\n\t<LI>sped up the image resizing code\r\n</UL>\r\n<LI>August 24, 2007 - updated the documentation examples (at the bottom of this page)\r\n<LI>August 22, 2007\r\n<UL>\r\n\t<LI>can load cube maps (needs serious testing)\r\n\t<LI>can compress 1- or 2-channel images to DXT1/5\r\n\t<LI>fixed some malloc() casts\r\n\t<LI>fixed C++ style comments\r\n\t<LI>fixed includes to compile under *nix or Mac (hopefully, needs testing...any volunteers?)\r\n</UL>\r\n<LI>August 16, 2007\r\n<UL>\r\n\t<LI>Will now downsize the image if necessary to fit GL_MAX_TEXTURE_SIZE\r\n\t<LI>added SOIL_create_OGL_texture() to upload raw image data that isn't from an image file\r\n</UL>\r\n<LI>August 14, 2007 (PM) - Can now load indexed TGA\r\n<LI>August 14, 2007 (AM)\r\n<UL>\r\n\t<LI>Updated to stb_image 0.97\r\n\t<LI>added result messages\r\n\t<LI>can now decompress DDS files (DXT1/2/3/4/5)\r\n</UL>\r\n<LI>August 11, 2007 - MIPmaps can now handle non-square textures\r\n<LI>August 7, 2007\r\n<UL>\r\n\t<LI>Can directly upload DXT1/3/5 DDS files (with or w/o MIPmaps)\r\n\t<LI>can compress any image to DXT1/5 (using a new &amp; fast &amp; simple compression scheme) and upload\r\n\t<LI>can save as DDS\r\n</UL>\r\n<LI>July 31, 2007 - added compressing to DXT and flipping about Y\r\n<LI>July 30, 2007 - initial release\r\n</UL>\r\n<BR>\r\n<BR>\r\n<PRE STYLE=\"text-align: center\">back to \r\n<A HREF=\"http://www.lonesock.net/\">www.lonesock.net</A></PRE>\r\n</BODY>\r\n</HTML>"
  },
  {
    "path": "src/SOIL.c",
    "content": "/*\r\n\tJonathan Dummer\r\n\t2007-07-26-10.36\r\n\r\n\tSimple OpenGL Image Library\r\n\r\n\tPublic Domain\r\n\tusing Sean Barret's stb_image as a base\r\n\r\n\tThanks to:\r\n\t* Sean Barret - for the awesome stb_image\r\n\t* Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts\r\n\t* everybody at gamedev.net\r\n*/\r\n\r\n#define SOIL_CHECK_FOR_GL_ERRORS 0\r\n\r\n#ifdef _WIN64\r\n\t#define WIN64_LEAN_AND_MEAN\r\n\t#include <windows.h>\r\n\t#include <wingdi.h>\r\n\t#include <GL/gl.h>\r\n#elif defined _WIN32\r\n\t#define WIN32_LEAN_AND_MEAN\r\n\t#include <windows.h>\r\n\t#include <wingdi.h>\r\n\t#include <GL/gl.h>\r\n#elif defined(__APPLE__) || defined(__APPLE_CC__)\r\n\t/*\tI can't test this Apple stuff!\t*/\r\n\t#include <OpenGL/gl.h>\r\n\t#include <Carbon/Carbon.h>\r\n\t#define APIENTRY\r\n#elif defined(__ANDROID__)\r\n\t#include <GLES/gl.h>\r\n\t#define APIENTRY\r\n#elif defined(__EMSCRIPTEN__)\r\n\t#include <GL/gl.h>\r\n#else\r\n\t#include <GL/gl.h>\r\n\t#include <GL/glx.h>\r\n#endif\r\n\r\n#include \"SOIL.h\"\r\n#include \"stb_image_aug.h\"\r\n#include \"image_helper.h\"\r\n#include \"image_DXT.h\"\r\n\r\n#include <stdlib.h>\r\n#include <string.h>\r\n\r\n/*\terror reporting\t*/\r\nchar *result_string_pointer = \"SOIL initialized\";\r\n\r\n/*\tfor loading cube maps\t*/\r\nenum{\r\n\tSOIL_CAPABILITY_UNKNOWN = -1,\r\n\tSOIL_CAPABILITY_NONE = 0,\r\n\tSOIL_CAPABILITY_PRESENT = 1\r\n};\r\nstatic int has_cubemap_capability = SOIL_CAPABILITY_UNKNOWN;\r\nint query_cubemap_capability( void );\r\n#define SOIL_TEXTURE_WRAP_R\t\t\t\t\t0x8072\r\n#define SOIL_CLAMP_TO_EDGE\t\t\t\t\t0x812F\r\n#define SOIL_NORMAL_MAP\t\t\t\t\t\t0x8511\r\n#define SOIL_REFLECTION_MAP\t\t\t\t\t0x8512\r\n#define SOIL_TEXTURE_CUBE_MAP\t\t\t\t0x8513\r\n#define SOIL_TEXTURE_BINDING_CUBE_MAP\t\t0x8514\r\n#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_X\t0x8515\r\n#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X\t0x8516\r\n#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y\t0x8517\r\n#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y\t0x8518\r\n#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z\t0x8519\r\n#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z\t0x851A\r\n#define SOIL_PROXY_TEXTURE_CUBE_MAP\t\t\t0x851B\r\n#define SOIL_MAX_CUBE_MAP_TEXTURE_SIZE\t\t0x851C\r\n/*\tfor non-power-of-two texture\t*/\r\nstatic int has_NPOT_capability = SOIL_CAPABILITY_UNKNOWN;\r\nint query_NPOT_capability( void );\r\n/*\tfor texture rectangles\t*/\r\nstatic int has_tex_rectangle_capability = SOIL_CAPABILITY_UNKNOWN;\r\nint query_tex_rectangle_capability( void );\r\n#define SOIL_TEXTURE_RECTANGLE_ARB\t\t\t\t0x84F5\r\n#define SOIL_MAX_RECTANGLE_TEXTURE_SIZE_ARB\t\t0x84F8\r\n/*\tfor using DXT compression\t*/\r\nstatic int has_DXT_capability = SOIL_CAPABILITY_UNKNOWN;\r\nint query_DXT_capability( void );\r\n#define SOIL_RGB_S3TC_DXT1\t\t0x83F0\r\n#define SOIL_RGBA_S3TC_DXT1\t\t0x83F1\r\n#define SOIL_RGBA_S3TC_DXT3\t\t0x83F2\r\n#define SOIL_RGBA_S3TC_DXT5\t\t0x83F3\r\ntypedef void (APIENTRY * P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid * data);\r\nP_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC soilGlCompressedTexImage2D = NULL;\r\nunsigned int SOIL_direct_load_DDS(\r\n\t\tconst char *filename,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tint flags,\r\n\t\tint loading_as_cubemap );\r\nunsigned int SOIL_direct_load_DDS_from_memory(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tint flags,\r\n\t\tint loading_as_cubemap );\r\n/*\tother functions\t*/\r\nunsigned int\r\n\tSOIL_internal_create_OGL_texture\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags,\r\n\t\tunsigned int opengl_texture_type,\r\n\t\tunsigned int opengl_texture_target,\r\n\t\tunsigned int texture_check_size_enum\r\n\t);\r\n\r\n/*\tand the code magic begins here [8^)\t*/\r\nunsigned int\r\n\tSOIL_load_OGL_texture\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels;\r\n\tunsigned int tex_id;\r\n\t/*\tdoes the user want direct uploading of the image as a DDS file?\t*/\r\n\tif( flags & SOIL_FLAG_DDS_LOAD_DIRECT )\r\n\t{\r\n\t\t/*\t1st try direct loading of the image as a DDS file\r\n\t\t\tnote: direct uploading will only load what is in the\r\n\t\t\tDDS file, no MIPmaps will be generated, the image will\r\n\t\t\tnot be flipped, etc.\t*/\r\n\t\ttex_id = SOIL_direct_load_DDS( filename, reuse_texture_ID, flags, 0 );\r\n\t\tif( tex_id )\r\n\t\t{\r\n\t\t\t/*\they, it worked!!\t*/\r\n\t\t\treturn tex_id;\r\n\t\t}\r\n\t}\r\n\t/*\ttry to load the image\t*/\r\n\timg = SOIL_load_image( filename, &width, &height, &channels, force_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tOK, make it a texture!\t*/\r\n\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\timg, width, height, channels,\r\n\t\t\treuse_texture_ID, flags,\r\n\t\t\tGL_TEXTURE_2D, GL_TEXTURE_2D,\r\n\t\t\tGL_MAX_TEXTURE_SIZE );\r\n\t/*\tand nuke the image data\t*/\r\n\tSOIL_free_image_data( img );\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_HDR_texture\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint fake_HDR_format,\r\n\t\tint rescale_to_max,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels;\r\n\tunsigned int tex_id;\r\n\t/*\tno direct uploading of the image as a DDS file\t*/\r\n\t/* error check */\r\n\tif( (fake_HDR_format != SOIL_HDR_RGBE) &&\r\n\t\t(fake_HDR_format != SOIL_HDR_RGBdivA) &&\r\n\t\t(fake_HDR_format != SOIL_HDR_RGBdivA2) )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid fake HDR format specified\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\ttry to load the image (only the HDR type) */\r\n\timg = stbi_hdr_load_rgbe( filename, &width, &height, &channels, 4 );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/* the load worked, do I need to convert it? */\r\n\tif( fake_HDR_format == SOIL_HDR_RGBdivA )\r\n\t{\r\n\t\tRGBE_to_RGBdivA( img, width, height, rescale_to_max );\r\n\t} else if( fake_HDR_format == SOIL_HDR_RGBdivA2 )\r\n\t{\r\n\t\tRGBE_to_RGBdivA2( img, width, height, rescale_to_max );\r\n\t}\r\n\t/*\tOK, make it a texture!\t*/\r\n\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\timg, width, height, channels,\r\n\t\t\treuse_texture_ID, flags,\r\n\t\t\tGL_TEXTURE_2D, GL_TEXTURE_2D,\r\n\t\t\tGL_MAX_TEXTURE_SIZE );\r\n\t/*\tand nuke the image data\t*/\r\n\tSOIL_free_image_data( img );\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_texture_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels;\r\n\tunsigned int tex_id;\r\n\t/*\tdoes the user want direct uploading of the image as a DDS file?\t*/\r\n\tif( flags & SOIL_FLAG_DDS_LOAD_DIRECT )\r\n\t{\r\n\t\t/*\t1st try direct loading of the image as a DDS file\r\n\t\t\tnote: direct uploading will only load what is in the\r\n\t\t\tDDS file, no MIPmaps will be generated, the image will\r\n\t\t\tnot be flipped, etc.\t*/\r\n\t\ttex_id = SOIL_direct_load_DDS_from_memory(\r\n\t\t\t\tbuffer, buffer_length,\r\n\t\t\t\treuse_texture_ID, flags, 0 );\r\n\t\tif( tex_id )\r\n\t\t{\r\n\t\t\t/*\they, it worked!!\t*/\r\n\t\t\treturn tex_id;\r\n\t\t}\r\n\t}\r\n\t/*\ttry to load the image\t*/\r\n\timg = SOIL_load_image_from_memory(\r\n\t\t\t\t\tbuffer, buffer_length,\r\n\t\t\t\t\t&width, &height, &channels,\r\n\t\t\t\t\tforce_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tOK, make it a texture!\t*/\r\n\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\timg, width, height, channels,\r\n\t\t\treuse_texture_ID, flags,\r\n\t\t\tGL_TEXTURE_2D, GL_TEXTURE_2D,\r\n\t\t\tGL_MAX_TEXTURE_SIZE );\r\n\t/*\tand nuke the image data\t*/\r\n\tSOIL_free_image_data( img );\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_cubemap\r\n\t(\r\n\t\tconst char *x_pos_file,\r\n\t\tconst char *x_neg_file,\r\n\t\tconst char *y_pos_file,\r\n\t\tconst char *y_neg_file,\r\n\t\tconst char *z_pos_file,\r\n\t\tconst char *z_neg_file,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels;\r\n\tunsigned int tex_id;\r\n\t/*\terror checking\t*/\r\n\tif( (x_pos_file == NULL) ||\r\n\t\t(x_neg_file == NULL) ||\r\n\t\t(y_pos_file == NULL) ||\r\n\t\t(y_neg_file == NULL) ||\r\n\t\t(z_pos_file == NULL) ||\r\n\t\t(z_neg_file == NULL) )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid cube map files list\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tcapability checking\t*/\r\n\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t{\r\n\t\tresult_string_pointer = \"No cube map capability present\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\t1st face: try to load the image\t*/\r\n\timg = SOIL_load_image( x_pos_file, &width, &height, &channels, force_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tupload the texture, and create a texture ID if necessary\t*/\r\n\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\timg, width, height, channels,\r\n\t\t\treuse_texture_ID, flags,\r\n\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_X,\r\n\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t/*\tand nuke the image data\t*/\r\n\tSOIL_free_image_data( img );\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image( x_neg_file, &width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image( y_pos_file, &width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image( y_neg_file, &width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image( z_pos_file, &width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image( z_neg_file, &width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_cubemap_from_memory\r\n\t(\r\n\t\tconst unsigned char *const x_pos_buffer,\r\n\t\tint x_pos_buffer_length,\r\n\t\tconst unsigned char *const x_neg_buffer,\r\n\t\tint x_neg_buffer_length,\r\n\t\tconst unsigned char *const y_pos_buffer,\r\n\t\tint y_pos_buffer_length,\r\n\t\tconst unsigned char *const y_neg_buffer,\r\n\t\tint y_neg_buffer_length,\r\n\t\tconst unsigned char *const z_pos_buffer,\r\n\t\tint z_pos_buffer_length,\r\n\t\tconst unsigned char *const z_neg_buffer,\r\n\t\tint z_neg_buffer_length,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels;\r\n\tunsigned int tex_id;\r\n\t/*\terror checking\t*/\r\n\tif( (x_pos_buffer == NULL) ||\r\n\t\t(x_neg_buffer == NULL) ||\r\n\t\t(y_pos_buffer == NULL) ||\r\n\t\t(y_neg_buffer == NULL) ||\r\n\t\t(z_pos_buffer == NULL) ||\r\n\t\t(z_neg_buffer == NULL) )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid cube map buffers list\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tcapability checking\t*/\r\n\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t{\r\n\t\tresult_string_pointer = \"No cube map capability present\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\t1st face: try to load the image\t*/\r\n\timg = SOIL_load_image_from_memory(\r\n\t\t\tx_pos_buffer, x_pos_buffer_length,\r\n\t\t\t&width, &height, &channels, force_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tupload the texture, and create a texture ID if necessary\t*/\r\n\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\timg, width, height, channels,\r\n\t\t\treuse_texture_ID, flags,\r\n\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_X,\r\n\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t/*\tand nuke the image data\t*/\r\n\tSOIL_free_image_data( img );\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image_from_memory(\r\n\t\t\t\tx_neg_buffer, x_neg_buffer_length,\r\n\t\t\t\t&width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image_from_memory(\r\n\t\t\t\ty_pos_buffer, y_pos_buffer_length,\r\n\t\t\t\t&width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image_from_memory(\r\n\t\t\t\ty_neg_buffer, y_neg_buffer_length,\r\n\t\t\t\t&width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image_from_memory(\r\n\t\t\t\tz_pos_buffer, z_pos_buffer_length,\r\n\t\t\t\t&width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tcontinue?\t*/\r\n\tif( tex_id != 0 )\r\n\t{\r\n\t\t/*\t1st face: try to load the image\t*/\r\n\t\timg = SOIL_load_image_from_memory(\r\n\t\t\t\tz_neg_buffer, z_neg_buffer_length,\r\n\t\t\t\t&width, &height, &channels, force_channels );\r\n\t\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\t\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t\t{\r\n\t\t\tchannels = force_channels;\r\n\t\t}\r\n\t\tif( NULL == img )\r\n\t\t{\r\n\t\t\t/*\timage loading failed\t*/\r\n\t\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tupload the texture, but reuse the assigned texture ID\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\timg, width, height, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t\t/*\tand nuke the image data\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t}\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_single_cubemap\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tconst char face_order[6],\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels, i;\r\n\tunsigned int tex_id = 0;\r\n\t/*\terror checking\t*/\r\n\tif( filename == NULL )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid single cube map file name\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tdoes the user want direct uploading of the image as a DDS file?\t*/\r\n\tif( flags & SOIL_FLAG_DDS_LOAD_DIRECT )\r\n\t{\r\n\t\t/*\t1st try direct loading of the image as a DDS file\r\n\t\t\tnote: direct uploading will only load what is in the\r\n\t\t\tDDS file, no MIPmaps will be generated, the image will\r\n\t\t\tnot be flipped, etc.\t*/\r\n\t\ttex_id = SOIL_direct_load_DDS( filename, reuse_texture_ID, flags, 1 );\r\n\t\tif( tex_id )\r\n\t\t{\r\n\t\t\t/*\they, it worked!!\t*/\r\n\t\t\treturn tex_id;\r\n\t\t}\r\n\t}\r\n\t/*\tface order checking\t*/\r\n\tfor( i = 0; i < 6; ++i )\r\n\t{\r\n\t\tif( (face_order[i] != 'N') &&\r\n\t\t\t(face_order[i] != 'S') &&\r\n\t\t\t(face_order[i] != 'W') &&\r\n\t\t\t(face_order[i] != 'E') &&\r\n\t\t\t(face_order[i] != 'U') &&\r\n\t\t\t(face_order[i] != 'D') )\r\n\t\t{\r\n\t\t\tresult_string_pointer = \"Invalid single cube map face order\";\r\n\t\t\treturn 0;\r\n\t\t};\r\n\t}\r\n\t/*\tcapability checking\t*/\r\n\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t{\r\n\t\tresult_string_pointer = \"No cube map capability present\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\t1st off, try to load the full image\t*/\r\n\timg = SOIL_load_image( filename, &width, &height, &channels, force_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tnow, does this image have the right dimensions?\t*/\r\n\tif( (width != 6*height) &&\r\n\t\t(6*width != height) )\r\n\t{\r\n\t\tSOIL_free_image_data( img );\r\n\t\tresult_string_pointer = \"Single cubemap image must have a 6:1 ratio\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\ttry the image split and create\t*/\r\n\ttex_id = SOIL_create_OGL_single_cubemap(\r\n\t\t\timg, width, height, channels,\r\n\t\t\tface_order, reuse_texture_ID, flags\r\n\t\t\t);\r\n\t/*\tnuke the temporary image data and return the texture handle\t*/\r\n\tSOIL_free_image_data( img );\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_load_OGL_single_cubemap_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tconst char face_order[6],\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tint width, height, channels, i;\r\n\tunsigned int tex_id = 0;\r\n\t/*\terror checking\t*/\r\n\tif( buffer == NULL )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid single cube map buffer\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tdoes the user want direct uploading of the image as a DDS file?\t*/\r\n\tif( flags & SOIL_FLAG_DDS_LOAD_DIRECT )\r\n\t{\r\n\t\t/*\t1st try direct loading of the image as a DDS file\r\n\t\t\tnote: direct uploading will only load what is in the\r\n\t\t\tDDS file, no MIPmaps will be generated, the image will\r\n\t\t\tnot be flipped, etc.\t*/\r\n\t\ttex_id = SOIL_direct_load_DDS_from_memory(\r\n\t\t\t\tbuffer, buffer_length,\r\n\t\t\t\treuse_texture_ID, flags, 1 );\r\n\t\tif( tex_id )\r\n\t\t{\r\n\t\t\t/*\they, it worked!!\t*/\r\n\t\t\treturn tex_id;\r\n\t\t}\r\n\t}\r\n\t/*\tface order checking\t*/\r\n\tfor( i = 0; i < 6; ++i )\r\n\t{\r\n\t\tif( (face_order[i] != 'N') &&\r\n\t\t\t(face_order[i] != 'S') &&\r\n\t\t\t(face_order[i] != 'W') &&\r\n\t\t\t(face_order[i] != 'E') &&\r\n\t\t\t(face_order[i] != 'U') &&\r\n\t\t\t(face_order[i] != 'D') )\r\n\t\t{\r\n\t\t\tresult_string_pointer = \"Invalid single cube map face order\";\r\n\t\t\treturn 0;\r\n\t\t};\r\n\t}\r\n\t/*\tcapability checking\t*/\r\n\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t{\r\n\t\tresult_string_pointer = \"No cube map capability present\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\t1st off, try to load the full image\t*/\r\n\timg = SOIL_load_image_from_memory(\r\n\t\t\tbuffer, buffer_length,\r\n\t\t\t&width, &height, &channels,\r\n\t\t\tforce_channels );\r\n\t/*\tchannels holds the original number of channels, which may have been forced\t*/\r\n\tif( (force_channels >= 1) && (force_channels <= 4) )\r\n\t{\r\n\t\tchannels = force_channels;\r\n\t}\r\n\tif( NULL == img )\r\n\t{\r\n\t\t/*\timage loading failed\t*/\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tnow, does this image have the right dimensions?\t*/\r\n\tif( (width != 6*height) &&\r\n\t\t(6*width != height) )\r\n\t{\r\n\t\tSOIL_free_image_data( img );\r\n\t\tresult_string_pointer = \"Single cubemap image must have a 6:1 ratio\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\ttry the image split and create\t*/\r\n\ttex_id = SOIL_create_OGL_single_cubemap(\r\n\t\t\timg, width, height, channels,\r\n\t\t\tface_order, reuse_texture_ID, flags\r\n\t\t\t);\r\n\t/*\tnuke the temporary image data and return the texture handle\t*/\r\n\tSOIL_free_image_data( img );\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_create_OGL_single_cubemap\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tconst char face_order[6],\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* sub_img;\r\n\tint dw, dh, sz, i;\r\n\tunsigned int tex_id;\r\n\t/*\terror checking\t*/\r\n\tif( data == NULL )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid single cube map image data\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tface order checking\t*/\r\n\tfor( i = 0; i < 6; ++i )\r\n\t{\r\n\t\tif( (face_order[i] != 'N') &&\r\n\t\t\t(face_order[i] != 'S') &&\r\n\t\t\t(face_order[i] != 'W') &&\r\n\t\t\t(face_order[i] != 'E') &&\r\n\t\t\t(face_order[i] != 'U') &&\r\n\t\t\t(face_order[i] != 'D') )\r\n\t\t{\r\n\t\t\tresult_string_pointer = \"Invalid single cube map face order\";\r\n\t\t\treturn 0;\r\n\t\t};\r\n\t}\r\n\t/*\tcapability checking\t*/\r\n\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t{\r\n\t\tresult_string_pointer = \"No cube map capability present\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tnow, does this image have the right dimensions?\t*/\r\n\tif( (width != 6*height) &&\r\n\t\t(6*width != height) )\r\n\t{\r\n\t\tresult_string_pointer = \"Single cubemap image must have a 6:1 ratio\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\twhich way am I stepping?\t*/\r\n\tif( width > height )\r\n\t{\r\n\t\tdw = height;\r\n\t\tdh = 0;\r\n\t} else\r\n\t{\r\n\t\tdw = 0;\r\n\t\tdh = width;\r\n\t}\r\n\tsz = dw+dh;\r\n\tsub_img = (unsigned char *)malloc( sz*sz*channels );\r\n\t/*\tdo the splitting and uploading\t*/\r\n\ttex_id = reuse_texture_ID;\r\n\tfor( i = 0; i < 6; ++i )\r\n\t{\r\n\t\tint x, y, idx = 0;\r\n\t\tunsigned int cubemap_target = 0;\r\n\t\t/*\tcopy in the sub-image\t*/\r\n\t\tfor( y = i*dh; y < i*dh+sz; ++y )\r\n\t\t{\r\n\t\t\tfor( x = i*dw*channels; x < (i*dw+sz)*channels; ++x )\r\n\t\t\t{\r\n\t\t\t\tsub_img[idx++] = data[y*width*channels+x];\r\n\t\t\t}\r\n\t\t}\r\n\t\t/*\twhat is my texture target?\r\n\t\t\tremember, this coordinate system is\r\n\t\t\tLHS if viewed from inside the cube!\t*/\r\n\t\tswitch( face_order[i] )\r\n\t\t{\r\n\t\tcase 'N':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z;\r\n\t\t\tbreak;\r\n\t\tcase 'S':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z;\r\n\t\t\tbreak;\r\n\t\tcase 'W':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X;\r\n\t\t\tbreak;\r\n\t\tcase 'E':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_X;\r\n\t\t\tbreak;\r\n\t\tcase 'U':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y;\r\n\t\t\tbreak;\r\n\t\tcase 'D':\r\n\t\t\tcubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\t/*\tupload it as a texture\t*/\r\n\t\ttex_id = SOIL_internal_create_OGL_texture(\r\n\t\t\t\tsub_img, sz, sz, channels,\r\n\t\t\t\ttex_id, flags,\r\n\t\t\t\tSOIL_TEXTURE_CUBE_MAP,\r\n\t\t\t\tcubemap_target,\r\n\t\t\t\tSOIL_MAX_CUBE_MAP_TEXTURE_SIZE );\r\n\t}\r\n\t/*\tand nuke the image and sub-image data\t*/\r\n\tSOIL_free_image_data( sub_img );\r\n\t/*\tand return the handle, such as it is\t*/\r\n\treturn tex_id;\r\n}\r\n\r\nunsigned int\r\n\tSOIL_create_OGL_texture\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t)\r\n{\r\n\t/*\twrapper function for 2D textures\t*/\r\n\treturn SOIL_internal_create_OGL_texture(\r\n\t\t\t\tdata, width, height, channels,\r\n\t\t\t\treuse_texture_ID, flags,\r\n\t\t\t\tGL_TEXTURE_2D, GL_TEXTURE_2D,\r\n\t\t\t\tGL_MAX_TEXTURE_SIZE );\r\n}\r\n\r\n#if SOIL_CHECK_FOR_GL_ERRORS\r\nvoid check_for_GL_errors( const char *calling_location )\r\n{\r\n\t/*\tcheck for errors\t*/\r\n\tGLenum err_code = glGetError();\r\n\twhile( GL_NO_ERROR != err_code )\r\n\t{\r\n\t\tprintf( \"OpenGL Error @ %s: %i\", calling_location, err_code );\r\n\t\terr_code = glGetError();\r\n\t}\r\n}\r\n#else\r\nvoid check_for_GL_errors( const char *calling_location )\r\n{\r\n\t/*\tno check for errors\t*/\r\n}\r\n#endif\r\n\r\nunsigned int\r\n\tSOIL_internal_create_OGL_texture\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags,\r\n\t\tunsigned int opengl_texture_type,\r\n\t\tunsigned int opengl_texture_target,\r\n\t\tunsigned int texture_check_size_enum\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tunsigned char* img;\r\n\tunsigned int tex_id;\r\n\tunsigned int internal_texture_format = 0, original_texture_format = 0;\r\n\tint DXT_mode = SOIL_CAPABILITY_UNKNOWN;\r\n\tint max_supported_size;\r\n\t/*\tIf the user wants to use the texture rectangle I kill a few flags\t*/\r\n\tif( flags & SOIL_FLAG_TEXTURE_RECTANGLE )\r\n\t{\r\n\t\t/*\twell, the user asked for it, can we do that?\t*/\r\n\t\tif( query_tex_rectangle_capability() == SOIL_CAPABILITY_PRESENT )\r\n\t\t{\r\n\t\t\t/*\tonly allow this if the user in _NOT_ trying to do a cubemap!\t*/\r\n\t\t\tif( opengl_texture_type == GL_TEXTURE_2D )\r\n\t\t\t{\r\n\t\t\t\t/*\tclean out the flags that cannot be used with texture rectangles\t*/\r\n\t\t\t\tflags &= ~(\r\n\t\t\t\t\t\tSOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS |\r\n\t\t\t\t\t\tSOIL_FLAG_TEXTURE_REPEATS\r\n\t\t\t\t\t);\r\n\t\t\t\t/*\tand change my target\t*/\r\n\t\t\t\topengl_texture_target = SOIL_TEXTURE_RECTANGLE_ARB;\r\n\t\t\t\topengl_texture_type = SOIL_TEXTURE_RECTANGLE_ARB;\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\t/*\tnot allowed for any other uses (yes, I'm looking at you, cubemaps!)\t*/\r\n\t\t\t\tflags &= ~SOIL_FLAG_TEXTURE_RECTANGLE;\r\n\t\t\t}\r\n\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tcan't do it, and that is a breakable offense (uv coords use pixels instead of [0,1]!)\t*/\r\n\t\t\tresult_string_pointer = \"Texture Rectangle extension unsupported\";\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t}\r\n\t/*\tcreate a copy the image data\t*/\r\n\timg = (unsigned char*)malloc( width*height*channels );\r\n\tmemcpy( img, data, width*height*channels );\r\n\t/*\tdoes the user want me to invert the image?\t*/\r\n\tif( flags & SOIL_FLAG_INVERT_Y )\r\n\t{\r\n\t\tint i, j;\r\n\t\tfor( j = 0; j*2 < height; ++j )\r\n\t\t{\r\n\t\t\tint index1 = j * width * channels;\r\n\t\t\tint index2 = (height - 1 - j) * width * channels;\r\n\t\t\tfor( i = width * channels; i > 0; --i )\r\n\t\t\t{\r\n\t\t\t\tunsigned char temp = img[index1];\r\n\t\t\t\timg[index1] = img[index2];\r\n\t\t\t\timg[index2] = temp;\r\n\t\t\t\t++index1;\r\n\t\t\t\t++index2;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t/*\tdoes the user want me to scale the colors into the NTSC safe RGB range?\t*/\r\n\tif( flags & SOIL_FLAG_NTSC_SAFE_RGB )\r\n\t{\r\n\t\tscale_image_RGB_to_NTSC_safe( img, width, height, channels );\r\n\t}\r\n\t/*\tdoes the user want me to convert from straight to pre-multiplied alpha?\r\n\t\t(and do we even _have_ alpha?)\t*/\r\n\tif( flags & SOIL_FLAG_MULTIPLY_ALPHA )\r\n\t{\r\n\t\tint i;\r\n\t\tswitch( channels )\r\n\t\t{\r\n\t\tcase 2:\r\n\t\t\tfor( i = 0; i < 2*width*height; i += 2 )\r\n\t\t\t{\r\n\t\t\t\timg[i] = (img[i] * img[i+1] + 128) >> 8;\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tcase 4:\r\n\t\t\tfor( i = 0; i < 4*width*height; i += 4 )\r\n\t\t\t{\r\n\t\t\t\timg[i+0] = (img[i+0] * img[i+3] + 128) >> 8;\r\n\t\t\t\timg[i+1] = (img[i+1] * img[i+3] + 128) >> 8;\r\n\t\t\t\timg[i+2] = (img[i+2] * img[i+3] + 128) >> 8;\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\t/*\tno other number of channels contains alpha data\t*/\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\t/*\tif the user can't support NPOT textures, make sure we force the POT option\t*/\r\n\tif( (query_NPOT_capability() == SOIL_CAPABILITY_NONE) &&\r\n\t\t!(flags & SOIL_FLAG_TEXTURE_RECTANGLE) )\r\n\t{\r\n\t\t/*\tadd in the POT flag */\r\n\t\tflags |= SOIL_FLAG_POWER_OF_TWO;\r\n\t}\r\n\t/*\thow large of a texture can this OpenGL implementation handle?\t*/\r\n\t/*\ttexture_check_size_enum will be GL_MAX_TEXTURE_SIZE or SOIL_MAX_CUBE_MAP_TEXTURE_SIZE\t*/\r\n\tglGetIntegerv( texture_check_size_enum, &max_supported_size );\r\n\t/*\tdo I need to make it a power of 2?\t*/\r\n\tif(\r\n\t\t(flags & SOIL_FLAG_POWER_OF_TWO) ||\t/*\tuser asked for it\t*/\r\n\t\t(flags & SOIL_FLAG_MIPMAPS) ||\t\t/*\tneed it for the MIP-maps\t*/\r\n\t\t(width > max_supported_size) ||\t\t/*\tit's too big, (make sure it's\t*/\r\n\t\t(height > max_supported_size) )\t\t/*\t2^n for later down-sampling)\t*/\r\n\t{\r\n\t\tint new_width = 1;\r\n\t\tint new_height = 1;\r\n\t\twhile( new_width < width )\r\n\t\t{\r\n\t\t\tnew_width *= 2;\r\n\t\t}\r\n\t\twhile( new_height < height )\r\n\t\t{\r\n\t\t\tnew_height *= 2;\r\n\t\t}\r\n\t\t/*\tstill?\t*/\r\n\t\tif( (new_width != width) || (new_height != height) )\r\n\t\t{\r\n\t\t\t/*\tyep, resize\t*/\r\n\t\t\tunsigned char *resampled = (unsigned char*)malloc( channels*new_width*new_height );\r\n\t\t\tup_scale_image(\r\n\t\t\t\t\timg, width, height, channels,\r\n\t\t\t\t\tresampled, new_width, new_height );\r\n\t\t\t/*\tOJO\tthis is for debug only!\t*/\r\n\t\t\t/*\r\n\t\t\tSOIL_save_image( \"\\\\showme.bmp\", SOIL_SAVE_TYPE_BMP,\r\n\t\t\t\t\t\t\tnew_width, new_height, channels,\r\n\t\t\t\t\t\t\tresampled );\r\n\t\t\t*/\r\n\t\t\t/*\tnuke the old guy, then point it at the new guy\t*/\r\n\t\t\tSOIL_free_image_data( img );\r\n\t\t\timg = resampled;\r\n\t\t\twidth = new_width;\r\n\t\t\theight = new_height;\r\n\t\t}\r\n\t}\r\n\t/*\tnow, if it is too large...\t*/\r\n\tif( (width > max_supported_size) || (height > max_supported_size) )\r\n\t{\r\n\t\t/*\tI've already made it a power of two, so simply use the MIPmapping\r\n\t\t\tcode to reduce its size to the allowable maximum.\t*/\r\n\t\tunsigned char *resampled;\r\n\t\tint reduce_block_x = 1, reduce_block_y = 1;\r\n\t\tint new_width, new_height;\r\n\t\tif( width > max_supported_size )\r\n\t\t{\r\n\t\t\treduce_block_x = width / max_supported_size;\r\n\t\t}\r\n\t\tif( height > max_supported_size )\r\n\t\t{\r\n\t\t\treduce_block_y = height / max_supported_size;\r\n\t\t}\r\n\t\tnew_width = width / reduce_block_x;\r\n\t\tnew_height = height / reduce_block_y;\r\n\t\tresampled = (unsigned char*)malloc( channels*new_width*new_height );\r\n\t\t/*\tperform the actual reduction\t*/\r\n\t\tmipmap_image(\timg, width, height, channels,\r\n\t\t\t\t\t\tresampled, reduce_block_x, reduce_block_y );\r\n\t\t/*\tnuke the old guy, then point it at the new guy\t*/\r\n\t\tSOIL_free_image_data( img );\r\n\t\timg = resampled;\r\n\t\twidth = new_width;\r\n\t\theight = new_height;\r\n\t}\r\n\t/*\tdoes the user want us to use YCoCg color space?\t*/\r\n\tif( flags & SOIL_FLAG_CoCg_Y )\r\n\t{\r\n\t\t/*\tthis will only work with RGB and RGBA images */\r\n\t\tconvert_RGB_to_YCoCg( img, width, height, channels );\r\n\t\t/*\r\n\t\tsave_image_as_DDS( \"CoCg_Y.dds\", width, height, channels, img );\r\n\t\t*/\r\n\t}\r\n\t/*\tcreate the OpenGL texture ID handle\r\n    \t(note: allowing a forced texture ID lets me reload a texture)\t*/\r\n    tex_id = reuse_texture_ID;\r\n    if( tex_id == 0 )\r\n    {\r\n\t\tglGenTextures( 1, &tex_id );\r\n    }\r\n\tcheck_for_GL_errors( \"glGenTextures\" );\r\n\t/* Note: sometimes glGenTextures fails (usually no OpenGL context)\t*/\r\n\tif( tex_id )\r\n\t{\r\n\t\t/*\tand what type am I using as the internal texture format?\t*/\r\n\t\tswitch( channels )\r\n\t\t{\r\n\t\tcase 1:\r\n\t\t\toriginal_texture_format = GL_LUMINANCE;\r\n\t\t\tbreak;\r\n\t\tcase 2:\r\n\t\t\toriginal_texture_format = GL_LUMINANCE_ALPHA;\r\n\t\t\tbreak;\r\n\t\tcase 3:\r\n\t\t\toriginal_texture_format = GL_RGB;\r\n\t\t\tbreak;\r\n\t\tcase 4:\r\n\t\t\toriginal_texture_format = GL_RGBA;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tinternal_texture_format = original_texture_format;\r\n\t\t/*\tdoes the user want me to, and can I, save as DXT?\t*/\r\n\t\tif( flags & SOIL_FLAG_COMPRESS_TO_DXT )\r\n\t\t{\r\n\t\t\tDXT_mode = query_DXT_capability();\r\n\t\t\tif( DXT_mode == SOIL_CAPABILITY_PRESENT )\r\n\t\t\t{\r\n\t\t\t\t/*\tI can use DXT, whether I compress it or OpenGL does\t*/\r\n\t\t\t\tif( (channels & 1) == 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\t/*\t1 or 3 channels = DXT1\t*/\r\n\t\t\t\t\tinternal_texture_format = SOIL_RGB_S3TC_DXT1;\r\n\t\t\t\t} else\r\n\t\t\t\t{\r\n\t\t\t\t\t/*\t2 or 4 channels = DXT5\t*/\r\n\t\t\t\t\tinternal_texture_format = SOIL_RGBA_S3TC_DXT5;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t/*  bind an OpenGL texture ID\t*/\r\n\t\tglBindTexture( opengl_texture_type, tex_id );\r\n\t\tcheck_for_GL_errors( \"glBindTexture\" );\r\n\t\t/*  upload the main image\t*/\r\n\t\tif( DXT_mode == SOIL_CAPABILITY_PRESENT )\r\n\t\t{\r\n\t\t\t/*\tuser wants me to do the DXT conversion!\t*/\r\n\t\t\tint DDS_size;\r\n\t\t\tunsigned char *DDS_data = NULL;\r\n\t\t\tif( (channels & 1) == 1 )\r\n\t\t\t{\r\n\t\t\t\t/*\tRGB, use DXT1\t*/\r\n\t\t\t\tDDS_data = convert_image_to_DXT1( img, width, height, channels, &DDS_size );\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\t/*\tRGBA, use DXT5\t*/\r\n\t\t\t\tDDS_data = convert_image_to_DXT5( img, width, height, channels, &DDS_size );\r\n\t\t\t}\r\n\t\t\tif( DDS_data )\r\n\t\t\t{\r\n\t\t\t\tsoilGlCompressedTexImage2D(\r\n\t\t\t\t\topengl_texture_target, 0,\r\n\t\t\t\t\tinternal_texture_format, width, height, 0,\r\n\t\t\t\t\tDDS_size, DDS_data );\r\n\t\t\t\tcheck_for_GL_errors( \"glCompressedTexImage2D\" );\r\n\t\t\t\tSOIL_free_image_data( DDS_data );\r\n\t\t\t\t/*\tprintf( \"Internal DXT compressor\\n\" );\t*/\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\t/*\tmy compression failed, try the OpenGL driver's version\t*/\r\n\t\t\t\tglTexImage2D(\r\n\t\t\t\t\topengl_texture_target, 0,\r\n\t\t\t\t\tinternal_texture_format, width, height, 0,\r\n\t\t\t\t\toriginal_texture_format, GL_UNSIGNED_BYTE, img );\r\n\t\t\t\tcheck_for_GL_errors( \"glTexImage2D\" );\r\n\t\t\t\t/*\tprintf( \"OpenGL DXT compressor\\n\" );\t*/\r\n\t\t\t}\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tuser want OpenGL to do all the work!\t*/\r\n\t\t\tglTexImage2D(\r\n\t\t\t\topengl_texture_target, 0,\r\n\t\t\t\tinternal_texture_format, width, height, 0,\r\n\t\t\t\toriginal_texture_format, GL_UNSIGNED_BYTE, img );\r\n\t\t\tcheck_for_GL_errors( \"glTexImage2D\" );\r\n\t\t\t/*printf( \"OpenGL DXT compressor\\n\" );\t*/\r\n\t\t}\r\n\t\t/*\tare any MIPmaps desired?\t*/\r\n\t\tif( flags & SOIL_FLAG_MIPMAPS )\r\n\t\t{\r\n\t\t\tint MIPlevel = 1;\r\n\t\t\tint MIPwidth = (width+1) / 2;\r\n\t\t\tint MIPheight = (height+1) / 2;\r\n\t\t\tunsigned char *resampled = (unsigned char*)malloc( channels*MIPwidth*MIPheight );\r\n\t\t\twhile( ((1<<MIPlevel) <= width) || ((1<<MIPlevel) <= height) )\r\n\t\t\t{\r\n\t\t\t\t/*\tdo this MIPmap level\t*/\r\n\t\t\t\tmipmap_image(\r\n\t\t\t\t\t\timg, width, height, channels,\r\n\t\t\t\t\t\tresampled,\r\n\t\t\t\t\t\t(1 << MIPlevel), (1 << MIPlevel) );\r\n\t\t\t\t/*  upload the MIPmaps\t*/\r\n\t\t\t\tif( DXT_mode == SOIL_CAPABILITY_PRESENT )\r\n\t\t\t\t{\r\n\t\t\t\t\t/*\tuser wants me to do the DXT conversion!\t*/\r\n\t\t\t\t\tint DDS_size;\r\n\t\t\t\t\tunsigned char *DDS_data = NULL;\r\n\t\t\t\t\tif( (channels & 1) == 1 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t/*\tRGB, use DXT1\t*/\r\n\t\t\t\t\t\tDDS_data = convert_image_to_DXT1(\r\n\t\t\t\t\t\t\t\tresampled, MIPwidth, MIPheight, channels, &DDS_size );\r\n\t\t\t\t\t} else\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t/*\tRGBA, use DXT5\t*/\r\n\t\t\t\t\t\tDDS_data = convert_image_to_DXT5(\r\n\t\t\t\t\t\t\t\tresampled, MIPwidth, MIPheight, channels, &DDS_size );\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif( DDS_data )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tsoilGlCompressedTexImage2D(\r\n\t\t\t\t\t\t\topengl_texture_target, MIPlevel,\r\n\t\t\t\t\t\t\tinternal_texture_format, MIPwidth, MIPheight, 0,\r\n\t\t\t\t\t\t\tDDS_size, DDS_data );\r\n\t\t\t\t\t\tcheck_for_GL_errors( \"glCompressedTexImage2D\" );\r\n\t\t\t\t\t\tSOIL_free_image_data( DDS_data );\r\n\t\t\t\t\t} else\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t/*\tmy compression failed, try the OpenGL driver's version\t*/\r\n\t\t\t\t\t\tglTexImage2D(\r\n\t\t\t\t\t\t\topengl_texture_target, MIPlevel,\r\n\t\t\t\t\t\t\tinternal_texture_format, MIPwidth, MIPheight, 0,\r\n\t\t\t\t\t\t\toriginal_texture_format, GL_UNSIGNED_BYTE, resampled );\r\n\t\t\t\t\t\tcheck_for_GL_errors( \"glTexImage2D\" );\r\n\t\t\t\t\t}\r\n\t\t\t\t} else\r\n\t\t\t\t{\r\n\t\t\t\t\t/*\tuser want OpenGL to do all the work!\t*/\r\n\t\t\t\t\tglTexImage2D(\r\n\t\t\t\t\t\topengl_texture_target, MIPlevel,\r\n\t\t\t\t\t\tinternal_texture_format, MIPwidth, MIPheight, 0,\r\n\t\t\t\t\t\toriginal_texture_format, GL_UNSIGNED_BYTE, resampled );\r\n\t\t\t\t\tcheck_for_GL_errors( \"glTexImage2D\" );\r\n\t\t\t\t}\r\n\t\t\t\t/*\tprep for the next level\t*/\r\n\t\t\t\t++MIPlevel;\r\n\t\t\t\tMIPwidth = (MIPwidth + 1) / 2;\r\n\t\t\t\tMIPheight = (MIPheight + 1) / 2;\r\n\t\t\t}\r\n\t\t\tSOIL_free_image_data( resampled );\r\n\t\t\t/*\tinstruct OpenGL to use the MIPmaps\t*/\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );\r\n\t\t\tcheck_for_GL_errors( \"GL_TEXTURE_MIN/MAG_FILTER\" );\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tinstruct OpenGL _NOT_ to use the MIPmaps\t*/\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );\r\n\t\t\tcheck_for_GL_errors( \"GL_TEXTURE_MIN/MAG_FILTER\" );\r\n\t\t}\r\n\t\t/*\tdoes the user want clamping, or wrapping?\t*/\r\n\t\tif( flags & SOIL_FLAG_TEXTURE_REPEATS )\r\n\t\t{\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_T, GL_REPEAT );\r\n\t\t\tif( opengl_texture_type == SOIL_TEXTURE_CUBE_MAP )\r\n\t\t\t{\r\n\t\t\t\t/*\tSOIL_TEXTURE_WRAP_R is invalid if cubemaps aren't supported\t*/\r\n\t\t\t\tglTexParameteri( opengl_texture_type, SOIL_TEXTURE_WRAP_R, GL_REPEAT );\r\n\t\t\t}\r\n\t\t\tcheck_for_GL_errors( \"GL_TEXTURE_WRAP_*\" );\r\n\t\t} else\r\n\t\t{\r\n\t\t\tunsigned int clamp_mode = SOIL_CLAMP_TO_EDGE;\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_S, clamp_mode );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_T, clamp_mode );\r\n\t\t\tif( opengl_texture_type == SOIL_TEXTURE_CUBE_MAP )\r\n\t\t\t{\r\n\t\t\t\t/*\tSOIL_TEXTURE_WRAP_R is invalid if cubemaps aren't supported\t*/\r\n\t\t\t\tglTexParameteri( opengl_texture_type, SOIL_TEXTURE_WRAP_R, clamp_mode );\r\n\t\t\t}\r\n\t\t\tcheck_for_GL_errors( \"GL_TEXTURE_WRAP_*\" );\r\n\t\t}\r\n\t\t/*\tdone\t*/\r\n\t\tresult_string_pointer = \"Image loaded as an OpenGL texture\";\r\n\t} else\r\n\t{\r\n\t\t/*\tfailed\t*/\r\n\t\tresult_string_pointer = \"Failed to generate an OpenGL texture name; missing OpenGL context?\";\r\n\t}\r\n\tSOIL_free_image_data( img );\r\n\treturn tex_id;\r\n}\r\n\r\nint\r\n\tSOIL_save_screenshot\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint image_type,\r\n\t\tint x, int y,\r\n\t\tint width, int height\r\n\t)\r\n{\r\n\tunsigned char *pixel_data;\r\n\tint i, j;\r\n\tint save_result;\r\n\r\n\t/*\terror checks\t*/\r\n\tif( (width < 1) || (height < 1) )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid screenshot dimensions\";\r\n\t\treturn 0;\r\n\t}\r\n\tif( (x < 0) || (y < 0) )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid screenshot location\";\r\n\t\treturn 0;\r\n\t}\r\n\tif( filename == NULL )\r\n\t{\r\n\t\tresult_string_pointer = \"Invalid screenshot filename\";\r\n\t\treturn 0;\r\n\t}\r\n\r\n    /*  Get the data from OpenGL\t*/\r\n    pixel_data = (unsigned char*)malloc( 3*width*height );\r\n    glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixel_data);\r\n\r\n    /*\tinvert the image\t*/\r\n    for( j = 0; j*2 < height; ++j )\r\n\t{\r\n\t\tint index1 = j * width * 3;\r\n\t\tint index2 = (height - 1 - j) * width * 3;\r\n\t\tfor( i = width * 3; i > 0; --i )\r\n\t\t{\r\n\t\t\tunsigned char temp = pixel_data[index1];\r\n\t\t\tpixel_data[index1] = pixel_data[index2];\r\n\t\t\tpixel_data[index2] = temp;\r\n\t\t\t++index1;\r\n\t\t\t++index2;\r\n\t\t}\r\n\t}\r\n\r\n    /*\tsave the image\t*/\r\n    save_result = SOIL_save_image( filename, image_type, width, height, 3, pixel_data);\r\n\r\n    /*  And free the memory\t*/\r\n    SOIL_free_image_data( pixel_data );\r\n\treturn save_result;\r\n}\r\n\r\nunsigned char*\r\n\tSOIL_load_image\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint *width, int *height, int *channels,\r\n\t\tint force_channels\r\n\t)\r\n{\r\n\tunsigned char *result = stbi_load( filename,\r\n\t\t\twidth, height, channels, force_channels );\r\n\tif( result == NULL )\r\n\t{\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t} else\r\n\t{\r\n\t\tresult_string_pointer = \"Image loaded\";\r\n\t}\r\n\treturn result;\r\n}\r\n\r\nunsigned char*\r\n\tSOIL_load_image_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tint *width, int *height, int *channels,\r\n\t\tint force_channels\r\n\t)\r\n{\r\n\tunsigned char *result = stbi_load_from_memory(\r\n\t\t\t\tbuffer, buffer_length,\r\n\t\t\t\twidth, height, channels,\r\n\t\t\t\tforce_channels );\r\n\tif( result == NULL )\r\n\t{\r\n\t\tresult_string_pointer = stbi_failure_reason();\r\n\t} else\r\n\t{\r\n\t\tresult_string_pointer = \"Image loaded from memory\";\r\n\t}\r\n\treturn result;\r\n}\r\n\r\nint\r\n\tSOIL_save_image\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint image_type,\r\n\t\tint width, int height, int channels,\r\n\t\tconst unsigned char *const data\r\n\t)\r\n{\r\n\tint save_result;\r\n\r\n\t/*\terror check\t*/\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(channels < 1) || (channels > 4) ||\r\n\t\t(data == NULL) ||\r\n\t\t(filename == NULL) )\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\tif( image_type == SOIL_SAVE_TYPE_BMP )\r\n\t{\r\n\t\tsave_result = stbi_write_bmp( filename,\r\n\t\t\t\twidth, height, channels, (void*)data );\r\n\t} else\r\n\tif( image_type == SOIL_SAVE_TYPE_TGA )\r\n\t{\r\n\t\tsave_result = stbi_write_tga( filename,\r\n\t\t\t\twidth, height, channels, (void*)data );\r\n\t} else\r\n\tif( image_type == SOIL_SAVE_TYPE_DDS )\r\n\t{\r\n\t\tsave_result = save_image_as_DDS( filename,\r\n\t\t\t\twidth, height, channels, (const unsigned char *const)data );\r\n\t} else\r\n\t{\r\n\t\tsave_result = 0;\r\n\t}\r\n\tif( save_result == 0 )\r\n\t{\r\n\t\tresult_string_pointer = \"Saving the image failed\";\r\n\t} else\r\n\t{\r\n\t\tresult_string_pointer = \"Image saved\";\r\n\t}\r\n\treturn save_result;\r\n}\r\n\r\nvoid\r\n\tSOIL_free_image_data\r\n\t(\r\n\t\tunsigned char *img_data\r\n\t)\r\n{\r\n\tfree( (void*)img_data );\r\n}\r\n\r\nconst char*\r\n\tSOIL_last_result\r\n\t(\r\n\t\tvoid\r\n\t)\r\n{\r\n\treturn result_string_pointer;\r\n}\r\n\r\nunsigned int SOIL_direct_load_DDS_from_memory(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tint flags,\r\n\t\tint loading_as_cubemap )\r\n{\r\n\t/*\tvariables\t*/\r\n\tDDS_header header;\r\n\tunsigned int buffer_index = 0;\r\n\tunsigned int tex_ID = 0;\r\n\t/*\tfile reading variables\t*/\r\n\tunsigned int S3TC_type = 0;\r\n\tunsigned char *DDS_data;\r\n\tunsigned int DDS_main_size;\r\n\tunsigned int DDS_full_size;\r\n\tunsigned int width, height;\r\n\tint mipmaps, cubemap, uncompressed, block_size = 16;\r\n\tunsigned int flag;\r\n\tunsigned int cf_target, ogl_target_start, ogl_target_end;\r\n\tunsigned int opengl_texture_type;\r\n\tint i;\r\n\t/*\t1st off, does the filename even exist?\t*/\r\n\tif( NULL == buffer )\r\n\t{\r\n\t\t/*\twe can't do it!\t*/\r\n\t\tresult_string_pointer = \"NULL buffer\";\r\n\t\treturn 0;\r\n\t}\r\n\tif( buffer_length < sizeof( DDS_header ) )\r\n\t{\r\n\t\t/*\twe can't do it!\t*/\r\n\t\tresult_string_pointer = \"DDS file was too small to contain the DDS header\";\r\n\t\treturn 0;\r\n\t}\r\n\t/*\ttry reading in the header\t*/\r\n\tmemcpy ( (void*)(&header), (const void *)buffer, sizeof( DDS_header ) );\r\n\tbuffer_index = sizeof( DDS_header );\r\n\t/*\tguilty until proven innocent\t*/\r\n\tresult_string_pointer = \"Failed to read a known DDS header\";\r\n\t/*\tvalidate the header (warning, \"goto\"'s ahead, shield your eyes!!)\t*/\r\n\tflag = ('D'<<0)|('D'<<8)|('S'<<16)|(' '<<24);\r\n\tif( header.dwMagic != flag ) {goto quick_exit;}\r\n\tif( header.dwSize != 124 ) {goto quick_exit;}\r\n\t/*\tI need all of these\t*/\r\n\tflag = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;\r\n\tif( (header.dwFlags & flag) != flag ) {goto quick_exit;}\r\n\t/*\tAccording to the MSDN spec, the dwFlags should contain\r\n\t\tDDSD_LINEARSIZE if it's compressed, or DDSD_PITCH if\r\n\t\tuncompressed.  Some DDS writers do not conform to the\r\n\t\tspec, so I need to make my reader more tolerant\t*/\r\n\t/*\tI need one of these\t*/\r\n\tflag = DDPF_FOURCC | DDPF_RGB;\r\n\tif( (header.sPixelFormat.dwFlags & flag) == 0 ) {goto quick_exit;}\r\n\tif( header.sPixelFormat.dwSize != 32 ) {goto quick_exit;}\r\n\tif( (header.sCaps.dwCaps1 & DDSCAPS_TEXTURE) == 0 ) {goto quick_exit;}\r\n\t/*\tmake sure it is a type we can upload\t*/\r\n\tif( (header.sPixelFormat.dwFlags & DDPF_FOURCC) &&\r\n\t\t!(\r\n\t\t(header.sPixelFormat.dwFourCC == (('D'<<0)|('X'<<8)|('T'<<16)|('1'<<24))) ||\r\n\t\t(header.sPixelFormat.dwFourCC == (('D'<<0)|('X'<<8)|('T'<<16)|('3'<<24))) ||\r\n\t\t(header.sPixelFormat.dwFourCC == (('D'<<0)|('X'<<8)|('T'<<16)|('5'<<24)))\r\n\t\t) )\r\n\t{\r\n\t\tgoto quick_exit;\r\n\t}\r\n\t/*\tOK, validated the header, let's load the image data\t*/\r\n\tresult_string_pointer = \"DDS header loaded and validated\";\r\n\twidth = header.dwWidth;\r\n\theight = header.dwHeight;\r\n\tuncompressed = 1 - (header.sPixelFormat.dwFlags & DDPF_FOURCC) / DDPF_FOURCC;\r\n\tcubemap = (header.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP) / DDSCAPS2_CUBEMAP;\r\n\tif( uncompressed )\r\n\t{\r\n\t\tS3TC_type = GL_RGB;\r\n\t\tblock_size = 3;\r\n\t\tif( header.sPixelFormat.dwFlags & DDPF_ALPHAPIXELS )\r\n\t\t{\r\n\t\t\tS3TC_type = GL_RGBA;\r\n\t\t\tblock_size = 4;\r\n\t\t}\r\n\t\tDDS_main_size = width * height * block_size;\r\n\t} else\r\n\t{\r\n\t\t/*\tcan we even handle direct uploading to OpenGL DXT compressed images?\t*/\r\n\t\tif( query_DXT_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t\t{\r\n\t\t\t/*\twe can't do it!\t*/\r\n\t\t\tresult_string_pointer = \"Direct upload of S3TC images not supported by the OpenGL driver\";\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\twell, we know it is DXT1/3/5, because we checked above\t*/\r\n\t\tswitch( (header.sPixelFormat.dwFourCC >> 24) - '0' )\r\n\t\t{\r\n\t\tcase 1:\r\n\t\t\tS3TC_type = SOIL_RGBA_S3TC_DXT1;\r\n\t\t\tblock_size = 8;\r\n\t\t\tbreak;\r\n\t\tcase 3:\r\n\t\t\tS3TC_type = SOIL_RGBA_S3TC_DXT3;\r\n\t\t\tblock_size = 16;\r\n\t\t\tbreak;\r\n\t\tcase 5:\r\n\t\t\tS3TC_type = SOIL_RGBA_S3TC_DXT5;\r\n\t\t\tblock_size = 16;\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tDDS_main_size = ((width+3)>>2)*((height+3)>>2)*block_size;\r\n\t}\r\n\tif( cubemap )\r\n\t{\r\n\t\t/* does the user want a cubemap?\t*/\r\n\t\tif( !loading_as_cubemap )\r\n\t\t{\r\n\t\t\t/*\twe can't do it!\t*/\r\n\t\t\tresult_string_pointer = \"DDS image was a cubemap\";\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t/*\tcan we even handle cubemaps with the OpenGL driver?\t*/\r\n\t\tif( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )\r\n\t\t{\r\n\t\t\t/*\twe can't do it!\t*/\r\n\t\t\tresult_string_pointer = \"Direct upload of cubemap images not supported by the OpenGL driver\";\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\togl_target_start = SOIL_TEXTURE_CUBE_MAP_POSITIVE_X;\r\n\t\togl_target_end =   SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z;\r\n\t\topengl_texture_type = SOIL_TEXTURE_CUBE_MAP;\r\n\t} else\r\n\t{\r\n\t\t/* does the user want a non-cubemap?\t*/\r\n\t\tif( loading_as_cubemap )\r\n\t\t{\r\n\t\t\t/*\twe can't do it!\t*/\r\n\t\t\tresult_string_pointer = \"DDS image was not a cubemap\";\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\togl_target_start = GL_TEXTURE_2D;\r\n\t\togl_target_end =   GL_TEXTURE_2D;\r\n\t\topengl_texture_type = GL_TEXTURE_2D;\r\n\t}\r\n\tif( (header.sCaps.dwCaps1 & DDSCAPS_MIPMAP) && (header.dwMipMapCount > 1) )\r\n\t{\r\n\t\tint shift_offset;\r\n\t\tmipmaps = header.dwMipMapCount - 1;\r\n\t\tDDS_full_size = DDS_main_size;\r\n\t\tif( uncompressed )\r\n\t\t{\r\n\t\t\t/*\tuncompressed DDS, simple MIPmap size calculation\t*/\r\n\t\t\tshift_offset = 0;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tcompressed DDS, MIPmap size calculation is block based\t*/\r\n\t\t\tshift_offset = 2;\r\n\t\t}\r\n\t\tfor( i = 1; i <= mipmaps; ++ i )\r\n\t\t{\r\n\t\t\tint w, h;\r\n\t\t\tw = width >> (shift_offset + i);\r\n\t\t\th = height >> (shift_offset + i);\r\n\t\t\tif( w < 1 )\r\n\t\t\t{\r\n\t\t\t\tw = 1;\r\n\t\t\t}\r\n\t\t\tif( h < 1 )\r\n\t\t\t{\r\n\t\t\t\th = 1;\r\n\t\t\t}\r\n\t\t\tDDS_full_size += w*h*block_size;\r\n\t\t}\r\n\t} else\r\n\t{\r\n\t\tmipmaps = 0;\r\n\t\tDDS_full_size = DDS_main_size;\r\n\t}\r\n\tDDS_data = (unsigned char*)malloc( DDS_full_size );\r\n\t/*\tgot the image data RAM, create or use an existing OpenGL texture handle\t*/\r\n\ttex_ID = reuse_texture_ID;\r\n\tif( tex_ID == 0 )\r\n\t{\r\n\t\tglGenTextures( 1, &tex_ID );\r\n\t}\r\n\t/*  bind an OpenGL texture ID\t*/\r\n\tglBindTexture( opengl_texture_type, tex_ID );\r\n\t/*\tdo this for each face of the cubemap!\t*/\r\n\tfor( cf_target = ogl_target_start; cf_target <= ogl_target_end; ++cf_target )\r\n\t{\r\n\t\tif( buffer_index + DDS_full_size <= buffer_length )\r\n\t\t{\r\n\t\t\tunsigned int byte_offset = DDS_main_size;\r\n\t\t\tmemcpy( (void*)DDS_data, (const void*)(&buffer[buffer_index]), DDS_full_size );\r\n\t\t\tbuffer_index += DDS_full_size;\r\n\t\t\t/*\tupload the main chunk\t*/\r\n\t\t\tif( uncompressed )\r\n\t\t\t{\r\n\t\t\t\t/*\tand remember, DXT uncompressed uses BGR(A),\r\n\t\t\t\t\tso swap to RGB(A) for ALL MIPmap levels\t*/\r\n\t\t\t\tfor( i = 0; i < DDS_full_size; i += block_size )\r\n\t\t\t\t{\r\n\t\t\t\t\tunsigned char temp = DDS_data[i];\r\n\t\t\t\t\tDDS_data[i] = DDS_data[i+2];\r\n\t\t\t\t\tDDS_data[i+2] = temp;\r\n\t\t\t\t}\r\n\t\t\t\tglTexImage2D(\r\n\t\t\t\t\tcf_target, 0,\r\n\t\t\t\t\tS3TC_type, width, height, 0,\r\n\t\t\t\t\tS3TC_type, GL_UNSIGNED_BYTE, DDS_data );\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\tsoilGlCompressedTexImage2D(\r\n\t\t\t\t\tcf_target, 0,\r\n\t\t\t\t\tS3TC_type, width, height, 0,\r\n\t\t\t\t\tDDS_main_size, DDS_data );\r\n\t\t\t}\r\n\t\t\t/*\tupload the mipmaps, if we have them\t*/\r\n\t\t\tfor( i = 1; i <= mipmaps; ++i )\r\n\t\t\t{\r\n\t\t\t\tint w, h, mip_size;\r\n\t\t\t\tw = width >> i;\r\n\t\t\t\th = height >> i;\r\n\t\t\t\tif( w < 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\tw = 1;\r\n\t\t\t\t}\r\n\t\t\t\tif( h < 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\th = 1;\r\n\t\t\t\t}\r\n\t\t\t\t/*\tupload this mipmap\t*/\r\n\t\t\t\tif( uncompressed )\r\n\t\t\t\t{\r\n\t\t\t\t\tmip_size = w*h*block_size;\r\n\t\t\t\t\tglTexImage2D(\r\n\t\t\t\t\t\tcf_target, i,\r\n\t\t\t\t\t\tS3TC_type, w, h, 0,\r\n\t\t\t\t\t\tS3TC_type, GL_UNSIGNED_BYTE, &DDS_data[byte_offset] );\r\n\t\t\t\t} else\r\n\t\t\t\t{\r\n\t\t\t\t\tmip_size = ((w+3)/4)*((h+3)/4)*block_size;\r\n\t\t\t\t\tsoilGlCompressedTexImage2D(\r\n\t\t\t\t\t\tcf_target, i,\r\n\t\t\t\t\t\tS3TC_type, w, h, 0,\r\n\t\t\t\t\t\tmip_size, &DDS_data[byte_offset] );\r\n\t\t\t\t}\r\n\t\t\t\t/*\tand move to the next mipmap\t*/\r\n\t\t\t\tbyte_offset += mip_size;\r\n\t\t\t}\r\n\t\t\t/*\tit worked!\t*/\r\n\t\t\tresult_string_pointer = \"DDS file loaded\";\r\n\t\t} else\r\n\t\t{\r\n\t\t\tglDeleteTextures( 1, & tex_ID );\r\n\t\t\ttex_ID = 0;\r\n\t\t\tcf_target = ogl_target_end + 1;\r\n\t\t\tresult_string_pointer = \"DDS file was too small for expected image data\";\r\n\t\t}\r\n\t}/* end reading each face */\r\n\tSOIL_free_image_data( DDS_data );\r\n\tif( tex_ID )\r\n\t{\r\n\t\t/*\tdid I have MIPmaps?\t*/\r\n\t\tif( mipmaps > 0 )\r\n\t\t{\r\n\t\t\t/*\tinstruct OpenGL to use the MIPmaps\t*/\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tinstruct OpenGL _NOT_ to use the MIPmaps\t*/\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );\r\n\t\t}\r\n\t\t/*\tdoes the user want clamping, or wrapping?\t*/\r\n\t\tif( flags & SOIL_FLAG_TEXTURE_REPEATS )\r\n\t\t{\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_T, GL_REPEAT );\r\n\t\t\tglTexParameteri( opengl_texture_type, SOIL_TEXTURE_WRAP_R, GL_REPEAT );\r\n\t\t} else\r\n\t\t{\r\n\t\t\tunsigned int clamp_mode = SOIL_CLAMP_TO_EDGE;\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_S, clamp_mode );\r\n\t\t\tglTexParameteri( opengl_texture_type, GL_TEXTURE_WRAP_T, clamp_mode );\r\n\t\t\tglTexParameteri( opengl_texture_type, SOIL_TEXTURE_WRAP_R, clamp_mode );\r\n\t\t}\r\n\t}\r\n\r\nquick_exit:\r\n\t/*\treport success or failure\t*/\r\n\treturn tex_ID;\r\n}\r\n\r\nunsigned int SOIL_direct_load_DDS(\r\n\t\tconst char *filename,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tint flags,\r\n\t\tint loading_as_cubemap )\r\n{\r\n\tFILE *f;\r\n\tunsigned char *buffer;\r\n\tsize_t buffer_length, bytes_read;\r\n\tunsigned int tex_ID = 0;\r\n\t/*\terror checks\t*/\r\n\tif( NULL == filename )\r\n\t{\r\n\t\tresult_string_pointer = \"NULL filename\";\r\n\t\treturn 0;\r\n\t}\r\n\tf = fopen( filename, \"rb\" );\r\n\tif( NULL == f )\r\n\t{\r\n\t\t/*\tthe file doesn't seem to exist (or be open-able)\t*/\r\n\t\tresult_string_pointer = \"Can not find DDS file\";\r\n\t\treturn 0;\r\n\t}\r\n\tfseek( f, 0, SEEK_END );\r\n\tbuffer_length = ftell( f );\r\n\tfseek( f, 0, SEEK_SET );\r\n\tbuffer = (unsigned char *) malloc( buffer_length );\r\n\tif( NULL == buffer )\r\n\t{\r\n\t\tresult_string_pointer = \"malloc failed\";\r\n\t\tfclose( f );\r\n\t\treturn 0;\r\n\t}\r\n\tbytes_read = fread( (void*)buffer, 1, buffer_length, f );\r\n\tfclose( f );\r\n\tif( bytes_read < buffer_length )\r\n\t{\r\n\t\t/*\thuh?\t*/\r\n\t\tbuffer_length = bytes_read;\r\n\t}\r\n\t/*\tnow try to do the loading\t*/\r\n\ttex_ID = SOIL_direct_load_DDS_from_memory(\r\n\t\t(const unsigned char *const)buffer, buffer_length,\r\n\t\treuse_texture_ID, flags, loading_as_cubemap );\r\n\tSOIL_free_image_data( buffer );\r\n\treturn tex_ID;\r\n}\r\n\r\nint query_NPOT_capability( void )\r\n{\r\n\t/*\tcheck for the capability\t*/\r\n\tif( has_NPOT_capability == SOIL_CAPABILITY_UNKNOWN )\r\n\t{\r\n\t\t/*\twe haven't yet checked for the capability, do so\t*/\r\n\t\tif(\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_ARB_texture_non_power_of_two\" ) )\r\n\t\t&&\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_OES_texture_npot\" ) )\r\n\t\t\t)\r\n\t\t{\r\n\t\t\t/*\tnot there, flag the failure\t*/\r\n\t\t\thas_NPOT_capability = SOIL_CAPABILITY_NONE;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tit's there!\t*/\r\n\t\t\thas_NPOT_capability = SOIL_CAPABILITY_PRESENT;\r\n\t\t}\r\n\t}\r\n\t/*\tlet the user know if we can do non-power-of-two textures or not\t*/\r\n\treturn has_NPOT_capability;\r\n}\r\n\r\nint query_tex_rectangle_capability( void )\r\n{\r\n\t/*\tcheck for the capability\t*/\r\n\tif( has_tex_rectangle_capability == SOIL_CAPABILITY_UNKNOWN )\r\n\t{\r\n\t\t/*\twe haven't yet checked for the capability, do so\t*/\r\n\t\tif(\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_ARB_texture_rectangle\" ) )\r\n\t\t&&\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_EXT_texture_rectangle\" ) )\r\n\t\t&&\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_NV_texture_rectangle\" ) )\r\n\t\t\t)\r\n\t\t{\r\n\t\t\t/*\tnot there, flag the failure\t*/\r\n\t\t\thas_tex_rectangle_capability = SOIL_CAPABILITY_NONE;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tit's there!\t*/\r\n\t\t\thas_tex_rectangle_capability = SOIL_CAPABILITY_PRESENT;\r\n\t\t}\r\n\t}\r\n\t/*\tlet the user know if we can do texture rectangles or not\t*/\r\n\treturn has_tex_rectangle_capability;\r\n}\r\n\r\nint query_cubemap_capability( void )\r\n{\r\n\t/*\tcheck for the capability\t*/\r\n\tif( has_cubemap_capability == SOIL_CAPABILITY_UNKNOWN )\r\n\t{\r\n\t\t/*\twe haven't yet checked for the capability, do so\t*/\r\n\t\tif(\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_ARB_texture_cube_map\" ) )\r\n\t\t&&\r\n\t\t\t(NULL == strstr( (char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_EXT_texture_cube_map\" ) )\r\n\t\t#ifdef GL_ES_VERSION_2_0\r\n\t\t&& (0) /* GL ES 2.0 supports cubemaps, always enable */\r\n\t\t#endif\r\n\t\t\t)\r\n\t\t{\r\n\t\t\t/*\tnot there, flag the failure\t*/\r\n\t\t\thas_cubemap_capability = SOIL_CAPABILITY_NONE;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tit's there!\t*/\r\n\t\t\thas_cubemap_capability = SOIL_CAPABILITY_PRESENT;\r\n\t\t}\r\n\t}\r\n\t/*\tlet the user know if we can do cubemaps or not\t*/\r\n\treturn has_cubemap_capability;\r\n}\r\n\r\nint query_DXT_capability( void )\r\n{\r\n\t/*\tcheck for the capability\t*/\r\n\tif( has_DXT_capability == SOIL_CAPABILITY_UNKNOWN )\r\n\t{\r\n\t\t/*\twe haven't yet checked for the capability, do so\t*/\r\n\t\tif( NULL == strstr(\r\n\t\t\t\t(char const*)glGetString( GL_EXTENSIONS ),\r\n\t\t\t\t\"GL_EXT_texture_compression_s3tc\" ) )\r\n\t\t{\r\n\t\t\t/*\tnot there, flag the failure\t*/\r\n\t\t\thas_DXT_capability = SOIL_CAPABILITY_NONE;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t/*\tand find the address of the extension function\t*/\r\n\t\t\tP_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC ext_addr = NULL;\r\n\t\t\t#ifdef WIN32\r\n\t\t\t\text_addr = (P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC)\r\n\t\t\t\t\t\twglGetProcAddress\r\n\t\t\t\t\t\t(\r\n\t\t\t\t\t\t\t\"glCompressedTexImage2DARB\"\r\n\t\t\t\t\t\t);\r\n\t\t\t#elif defined(__APPLE__) || defined(__APPLE_CC__)\r\n\t\t\t\t/*\tI can't test this Apple stuff!\t*/\r\n\t\t\t\tCFBundleRef bundle;\r\n\t\t\t\tCFURLRef bundleURL =\r\n\t\t\t\t\tCFURLCreateWithFileSystemPath(\r\n\t\t\t\t\t\tkCFAllocatorDefault,\r\n\t\t\t\t\t\tCFSTR(\"/System/Library/Frameworks/OpenGL.framework\"),\r\n\t\t\t\t\t\tkCFURLPOSIXPathStyle,\r\n\t\t\t\t\t\ttrue );\r\n\t\t\t\tCFStringRef extensionName =\r\n\t\t\t\t\tCFStringCreateWithCString(\r\n\t\t\t\t\t\tkCFAllocatorDefault,\r\n\t\t\t\t\t\t\"glCompressedTexImage2DARB\",\r\n\t\t\t\t\t\tkCFStringEncodingASCII );\r\n\t\t\t\tbundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );\r\n\t\t\t\tassert( bundle != NULL );\r\n\t\t\t\text_addr = (P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC)\r\n\t\t\t\t\t\tCFBundleGetFunctionPointerForName\r\n\t\t\t\t\t\t(\r\n\t\t\t\t\t\t\tbundle, extensionName\r\n\t\t\t\t\t\t);\r\n\t\t\t\tCFRelease( bundleURL );\r\n\t\t\t\tCFRelease( extensionName );\r\n\t\t\t\tCFRelease( bundle );\r\n\t\t\t#elif defined(__ANDROID__) || defined(__EMSCRIPTEN__)\r\n\t\t\t\text_addr = (P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC)(glCompressedTexImage2D);\r\n\t\t\t#else\r\n\t\t\t\text_addr = (P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC)\r\n\t\t\t\t\t\tglXGetProcAddressARB\r\n\t\t\t\t\t\t(\r\n\t\t\t\t\t\t\t(const GLubyte *)\"glCompressedTexImage2DARB\"\r\n\t\t\t\t\t\t);\r\n\t\t\t#endif\r\n\t\t\t/*\tFlag it so no checks needed later\t*/\r\n\t\t\tif( NULL == ext_addr )\r\n\t\t\t{\r\n\t\t\t\t/*\thmm, not good!!  This should not happen, but does on my\r\n\t\t\t\t\tlaptop's VIA chipset.  The GL_EXT_texture_compression_s3tc\r\n\t\t\t\t\tspec requires that ARB_texture_compression be present too.\r\n\t\t\t\t\tthis means I can upload and have the OpenGL drive do the\r\n\t\t\t\t\tconversion, but I can't use my own routines or load DDS files\r\n\t\t\t\t\tfrom disk and upload them directly [8^(\t*/\r\n\t\t\t\thas_DXT_capability = SOIL_CAPABILITY_NONE;\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\t/*\tall's well!\t*/\r\n\t\t\t\tsoilGlCompressedTexImage2D = ext_addr;\r\n\t\t\t\thas_DXT_capability = SOIL_CAPABILITY_PRESENT;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t/*\tlet the user know if we can do DXT or not\t*/\r\n\treturn has_DXT_capability;\r\n}\r\n"
  },
  {
    "path": "src/SOIL.h",
    "content": "/**\r\n\t@mainpage SOIL\r\n\r\n\tJonathan Dummer\r\n\t2007-07-26-10.36\r\n\r\n\tSimple OpenGL Image Library\r\n\r\n\tA tiny c library for uploading images as\r\n\ttextures into OpenGL.  Also saving and\r\n\tloading of images is supported.\r\n\r\n\tI'm using Sean's Tool Box image loader as a base:\r\n\thttp://www.nothings.org/\r\n\r\n\tI'm upgrading it to load TGA and DDS files, and a direct\r\n\tpath for loading DDS files straight into OpenGL textures,\r\n\twhen applicable.\r\n\r\n\tImage Formats:\r\n\t- BMP\t\tload & save\r\n\t- TGA\t\tload & save\r\n\t- DDS\t\tload & save\r\n\t- PNG\t\tload\r\n\t- JPG\t\tload\r\n\r\n\tOpenGL Texture Features:\r\n\t- resample to power-of-two sizes\r\n\t- MIPmap generation\r\n\t- compressed texture S3TC formats (if supported)\r\n\t- can pre-multiply alpha for you, for better compositing\r\n\t- can flip image about the y-axis (except pre-compressed DDS files)\r\n\r\n\tThanks to:\r\n\t* Sean Barret - for the awesome stb_image\r\n\t* Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts\r\n\t* everybody at gamedev.net\r\n**/\r\n\r\n#ifndef HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY\r\n#define HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY\r\n\r\n#ifdef __cplusplus\r\nextern \"C\" {\r\n#endif\r\n\r\n/**\r\n\tThe format of images that may be loaded (force_channels).\r\n\tSOIL_LOAD_AUTO leaves the image in whatever format it was found.\r\n\tSOIL_LOAD_L forces the image to load as Luminous (greyscale)\r\n\tSOIL_LOAD_LA forces the image to load as Luminous with Alpha\r\n\tSOIL_LOAD_RGB forces the image to load as Red Green Blue\r\n\tSOIL_LOAD_RGBA forces the image to load as Red Green Blue Alpha\r\n**/\r\nenum\r\n{\r\n\tSOIL_LOAD_AUTO = 0,\r\n\tSOIL_LOAD_L = 1,\r\n\tSOIL_LOAD_LA = 2,\r\n\tSOIL_LOAD_RGB = 3,\r\n\tSOIL_LOAD_RGBA = 4\r\n};\r\n\r\n/**\r\n\tPassed in as reuse_texture_ID, will cause SOIL to\r\n\tregister a new texture ID using glGenTextures().\r\n\tIf the value passed into reuse_texture_ID > 0 then\r\n\tSOIL will just re-use that texture ID (great for\r\n\treloading image assets in-game!)\r\n**/\r\nenum\r\n{\r\n\tSOIL_CREATE_NEW_ID = 0\r\n};\r\n\r\n/**\r\n\tflags you can pass into SOIL_load_OGL_texture()\r\n\tand SOIL_create_OGL_texture().\r\n\t(note that if SOIL_FLAG_DDS_LOAD_DIRECT is used\r\n\tthe rest of the flags with the exception of\r\n\tSOIL_FLAG_TEXTURE_REPEATS will be ignored while\r\n\tloading already-compressed DDS files.)\r\n\r\n\tSOIL_FLAG_POWER_OF_TWO: force the image to be POT\r\n\tSOIL_FLAG_MIPMAPS: generate mipmaps for the texture\r\n\tSOIL_FLAG_TEXTURE_REPEATS: otherwise will clamp\r\n\tSOIL_FLAG_MULTIPLY_ALPHA: for using (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) blending\r\n\tSOIL_FLAG_INVERT_Y: flip the image vertically\r\n\tSOIL_FLAG_COMPRESS_TO_DXT: if the card can display them, will convert RGB to DXT1, RGBA to DXT5\r\n\tSOIL_FLAG_DDS_LOAD_DIRECT: will load DDS files directly without _ANY_ additional processing\r\n\tSOIL_FLAG_NTSC_SAFE_RGB: clamps RGB components to the range [16,235]\r\n\tSOIL_FLAG_CoCg_Y: Google YCoCg; RGB=>CoYCg, RGBA=>CoCgAY\r\n\tSOIL_FLAG_TEXTURE_RECTANGE: uses ARB_texture_rectangle ; pixel indexed & no repeat or MIPmaps or cubemaps\r\n**/\r\nenum\r\n{\r\n\tSOIL_FLAG_POWER_OF_TWO = 1,\r\n\tSOIL_FLAG_MIPMAPS = 2,\r\n\tSOIL_FLAG_TEXTURE_REPEATS = 4,\r\n\tSOIL_FLAG_MULTIPLY_ALPHA = 8,\r\n\tSOIL_FLAG_INVERT_Y = 16,\r\n\tSOIL_FLAG_COMPRESS_TO_DXT = 32,\r\n\tSOIL_FLAG_DDS_LOAD_DIRECT = 64,\r\n\tSOIL_FLAG_NTSC_SAFE_RGB = 128,\r\n\tSOIL_FLAG_CoCg_Y = 256,\r\n\tSOIL_FLAG_TEXTURE_RECTANGLE = 512\r\n};\r\n\r\n/**\r\n\tThe types of images that may be saved.\r\n\t(TGA supports uncompressed RGB / RGBA)\r\n\t(BMP supports uncompressed RGB)\r\n\t(DDS supports DXT1 and DXT5)\r\n**/\r\nenum\r\n{\r\n\tSOIL_SAVE_TYPE_TGA = 0,\r\n\tSOIL_SAVE_TYPE_BMP = 1,\r\n\tSOIL_SAVE_TYPE_DDS = 2\r\n};\r\n\r\n/**\r\n\tDefines the order of faces in a DDS cubemap.\r\n\tI recommend that you use the same order in single\r\n\timage cubemap files, so they will be interchangeable\r\n\twith DDS cubemaps when using SOIL.\r\n**/\r\n#define SOIL_DDS_CUBEMAP_FACE_ORDER \"EWUDNS\"\r\n\r\n/**\r\n\tThe types of internal fake HDR representations\r\n\r\n\tSOIL_HDR_RGBE:\t\tRGB * pow( 2.0, A - 128.0 )\r\n\tSOIL_HDR_RGBdivA:\tRGB / A\r\n\tSOIL_HDR_RGBdivA2:\tRGB / (A*A)\r\n**/\r\nenum\r\n{\r\n\tSOIL_HDR_RGBE = 0,\r\n\tSOIL_HDR_RGBdivA = 1,\r\n\tSOIL_HDR_RGBdivA2 = 2\r\n};\r\n\r\n/**\r\n\tLoads an image from disk into an OpenGL texture.\r\n\t\\param filename the name of the file to upload as a texture\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_texture\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads 6 images from disk into an OpenGL cubemap texture.\r\n\t\\param x_pos_file the name of the file to upload as the +x cube face\r\n\t\\param x_neg_file the name of the file to upload as the -x cube face\r\n\t\\param y_pos_file the name of the file to upload as the +y cube face\r\n\t\\param y_neg_file the name of the file to upload as the -y cube face\r\n\t\\param z_pos_file the name of the file to upload as the +z cube face\r\n\t\\param z_neg_file the name of the file to upload as the -z cube face\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_cubemap\r\n\t(\r\n\t\tconst char *x_pos_file,\r\n\t\tconst char *x_neg_file,\r\n\t\tconst char *y_pos_file,\r\n\t\tconst char *y_neg_file,\r\n\t\tconst char *z_pos_file,\r\n\t\tconst char *z_neg_file,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads 1 image from disk and splits it into an OpenGL cubemap texture.\r\n\t\\param filename the name of the file to upload as a texture\r\n\t\\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_single_cubemap\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tconst char face_order[6],\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads an HDR image from disk into an OpenGL texture.\r\n\t\\param filename the name of the file to upload as a texture\r\n\t\\param fake_HDR_format SOIL_HDR_RGBE, SOIL_HDR_RGBdivA, SOIL_HDR_RGBdivA2\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_HDR_texture\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint fake_HDR_format,\r\n\t\tint rescale_to_max,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads an image from RAM into an OpenGL texture.\r\n\t\\param buffer the image data in RAM just as if it were still in a file\r\n\t\\param buffer_length the size of the buffer in bytes\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_texture_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads 6 images from memory into an OpenGL cubemap texture.\r\n\t\\param x_pos_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param x_pos_buffer_length the size of the above buffer\r\n\t\\param x_neg_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param x_neg_buffer_length the size of the above buffer\r\n\t\\param y_pos_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param y_pos_buffer_length the size of the above buffer\r\n\t\\param y_neg_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param y_neg_buffer_length the size of the above buffer\r\n\t\\param z_pos_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param z_pos_buffer_length the size of the above buffer\r\n\t\\param z_neg_buffer the image data in RAM to upload as the +x cube face\r\n\t\\param z_neg_buffer_length the size of the above buffer\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_cubemap_from_memory\r\n\t(\r\n\t\tconst unsigned char *const x_pos_buffer,\r\n\t\tint x_pos_buffer_length,\r\n\t\tconst unsigned char *const x_neg_buffer,\r\n\t\tint x_neg_buffer_length,\r\n\t\tconst unsigned char *const y_pos_buffer,\r\n\t\tint y_pos_buffer_length,\r\n\t\tconst unsigned char *const y_neg_buffer,\r\n\t\tint y_neg_buffer_length,\r\n\t\tconst unsigned char *const z_pos_buffer,\r\n\t\tint z_pos_buffer_length,\r\n\t\tconst unsigned char *const z_neg_buffer,\r\n\t\tint z_neg_buffer_length,\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tLoads 1 image from RAM and splits it into an OpenGL cubemap texture.\r\n\t\\param buffer the image data in RAM just as if it were still in a file\r\n\t\\param buffer_length the size of the buffer in bytes\r\n\t\\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.\r\n\t\\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_load_OGL_single_cubemap_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tconst char face_order[6],\r\n\t\tint force_channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tCreates a 2D OpenGL texture from raw image data.  Note that the raw data is\r\n\t_NOT_ freed after the upload (so the user can load various versions).\r\n\t\\param data the raw data to be uploaded as an OpenGL texture\r\n\t\\param width the width of the image in pixels\r\n\t\\param height the height of the image in pixels\r\n\t\\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_create_OGL_texture\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tCreates an OpenGL cubemap texture by splitting up 1 image into 6 parts.\r\n\t\\param data the raw data to be uploaded as an OpenGL texture\r\n\t\\param width the width of the image in pixels\r\n\t\\param height the height of the image in pixels\r\n\t\\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA\r\n\t\\param face_order the order of the faces in the file, and combination of NSWEUD, for North, South, Up, etc.\r\n\t\\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)\r\n\t\\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\\return 0-failed, otherwise returns the OpenGL texture handle\r\n**/\r\nunsigned int\r\n\tSOIL_create_OGL_single_cubemap\r\n\t(\r\n\t\tconst unsigned char *const data,\r\n\t\tint width, int height, int channels,\r\n\t\tconst char face_order[6],\r\n\t\tunsigned int reuse_texture_ID,\r\n\t\tunsigned int flags\r\n\t);\r\n\r\n/**\r\n\tCaptures the OpenGL window (RGB) and saves it to disk\r\n\t\\return 0 if it failed, otherwise returns 1\r\n**/\r\nint\r\n\tSOIL_save_screenshot\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint image_type,\r\n\t\tint x, int y,\r\n\t\tint width, int height\r\n\t);\r\n\r\n/**\r\n\tLoads an image from disk into an array of unsigned chars.\r\n\tNote that *channels return the original channel count of the\r\n\timage.  If force_channels was other than SOIL_LOAD_AUTO,\r\n\tthe resulting image has force_channels, but *channels may be\r\n\tdifferent (if the original image had a different channel\r\n\tcount).\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nunsigned char*\r\n\tSOIL_load_image\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint *width, int *height, int *channels,\r\n\t\tint force_channels\r\n\t);\r\n\r\n/**\r\n\tLoads an image from memory into an array of unsigned chars.\r\n\tNote that *channels return the original channel count of the\r\n\timage.  If force_channels was other than SOIL_LOAD_AUTO,\r\n\tthe resulting image has force_channels, but *channels may be\r\n\tdifferent (if the original image had a different channel\r\n\tcount).\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nunsigned char*\r\n\tSOIL_load_image_from_memory\r\n\t(\r\n\t\tconst unsigned char *const buffer,\r\n\t\tint buffer_length,\r\n\t\tint *width, int *height, int *channels,\r\n\t\tint force_channels\r\n\t);\r\n\r\n/**\r\n\tSaves an image from an array of unsigned chars (RGBA) to disk\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nint\r\n\tSOIL_save_image\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint image_type,\r\n\t\tint width, int height, int channels,\r\n\t\tconst unsigned char *const data\r\n\t);\r\n\r\n/**\r\n\tFrees the image data (note, this is just C's \"free()\"...this function is\r\n\tpresent mostly so C++ programmers don't forget to use \"free()\" and call\r\n\t\"delete []\" instead [8^)\r\n**/\r\nvoid\r\n\tSOIL_free_image_data\r\n\t(\r\n\t\tunsigned char *img_data\r\n\t);\r\n\r\n/**\r\n\tThis function resturn a pointer to a string describing the last thing\r\n\tthat happened inside SOIL.  It can be used to determine why an image\r\n\tfailed to load.\r\n**/\r\nconst char*\r\n\tSOIL_last_result\r\n\t(\r\n\t\tvoid\r\n\t);\r\n\r\n\r\n#ifdef __cplusplus\r\n}\r\n#endif\r\n\r\n#endif /* HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY\t*/\r\n"
  },
  {
    "path": "src/image_DXT.c",
    "content": "/*\r\n\tJonathan Dummer\r\n\t2007-07-31-10.32\r\n\r\n\tsimple DXT compression / decompression code\r\n\r\n\tpublic domain\r\n*/\r\n\r\n#include \"image_DXT.h\"\r\n#include <math.h>\r\n#include <stdlib.h>\r\n#include <string.h>\r\n#include <stdio.h>\r\n\r\n/*\tset this =1 if you want to use the covarince matrix method...\r\n\twhich is better than my method of using standard deviations\r\n\toverall, except on the infintesimal chance that the power\r\n\tmethod fails for finding the largest eigenvector\t*/\r\n#define USE_COV_MAT\t1\r\n\r\n/********* Function Prototypes *********/\r\n/*\r\n\tTakes a 4x4 block of pixels and compresses it into 8 bytes\r\n\tin DXT1 format (color only, no alpha).  Speed is valued\r\n\tover prettyness, at least for now.\r\n*/\r\nvoid compress_DDS_color_block(\r\n\t\t\t\tint channels,\r\n\t\t\t\tconst unsigned char *const uncompressed,\r\n\t\t\t\tunsigned char compressed[8] );\r\n/*\r\n\tTakes a 4x4 block of pixels and compresses the alpha\r\n\tcomponent it into 8 bytes for use in DXT5 DDS files.\r\n\tSpeed is valued over prettyness, at least for now.\r\n*/\r\nvoid compress_DDS_alpha_block(\r\n\t\t\t\tconst unsigned char *const uncompressed,\r\n\t\t\t\tunsigned char compressed[8] );\r\n\r\n/********* Actual Exposed Functions *********/\r\nint\r\n\tsave_image_as_DDS\r\n\t(\r\n\t\tconst char *filename,\r\n\t\tint width, int height, int channels,\r\n\t\tconst unsigned char *const data\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tFILE *fout;\r\n\tunsigned char *DDS_data;\r\n\tDDS_header header;\r\n\tint DDS_size;\r\n\t/*\terror check\t*/\r\n\tif( (NULL == filename) ||\r\n\t\t(width < 1) || (height < 1) ||\r\n\t\t(channels < 1) || (channels > 4) ||\r\n\t\t(data == NULL ) )\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tConvert the image\t*/\r\n\tif( (channels & 1) == 1 )\r\n\t{\r\n\t\t/*\tno alpha, just use DXT1\t*/\r\n\t\tDDS_data = convert_image_to_DXT1( data, width, height, channels, &DDS_size );\r\n\t} else\r\n\t{\r\n\t\t/*\thas alpha, so use DXT5\t*/\r\n\t\tDDS_data = convert_image_to_DXT5( data, width, height, channels, &DDS_size );\r\n\t}\r\n\t/*\tsave it\t*/\r\n\tmemset( &header, 0, sizeof( DDS_header ) );\r\n\theader.dwMagic = ('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24);\r\n\theader.dwSize = 124;\r\n\theader.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_LINEARSIZE;\r\n\theader.dwWidth = width;\r\n\theader.dwHeight = height;\r\n\theader.dwPitchOrLinearSize = DDS_size;\r\n\theader.sPixelFormat.dwSize = 32;\r\n\theader.sPixelFormat.dwFlags = DDPF_FOURCC;\r\n\tif( (channels & 1) == 1 )\r\n\t{\r\n\t\theader.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('1' << 24);\r\n\t} else\r\n\t{\r\n\t\theader.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('5' << 24);\r\n\t}\r\n\theader.sCaps.dwCaps1 = DDSCAPS_TEXTURE;\r\n\t/*\twrite it out\t*/\r\n\tfout = fopen( filename, \"wb\");\r\n\tfwrite( &header, sizeof( DDS_header ), 1, fout );\r\n\tfwrite( DDS_data, 1, DDS_size, fout );\r\n\tfclose( fout );\r\n\t/*\tdone\t*/\r\n\tfree( DDS_data );\r\n\treturn 1;\r\n}\r\n\r\nunsigned char* convert_image_to_DXT1(\r\n\t\tconst unsigned char *const uncompressed,\r\n\t\tint width, int height, int channels,\r\n\t\tint *out_size )\r\n{\r\n\tunsigned char *compressed;\r\n\tint i, j, x, y;\r\n\tunsigned char ublock[16*3];\r\n\tunsigned char cblock[8];\r\n\tint index = 0, chan_step = 1;\r\n\tint block_count = 0;\r\n\t/*\terror check\t*/\r\n\t*out_size = 0;\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(NULL == uncompressed) ||\r\n\t\t(channels < 1) || (channels > 4) )\r\n\t{\r\n\t\treturn NULL;\r\n\t}\r\n\t/*\tfor channels == 1 or 2, I do not step forward for R,G,B values\t*/\r\n\tif( channels < 3 )\r\n\t{\r\n\t\tchan_step = 0;\r\n\t}\r\n\t/*\tget the RAM for the compressed image\r\n\t\t(8 bytes per 4x4 pixel block)\t*/\r\n\t*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 8;\r\n\tcompressed = (unsigned char*)malloc( *out_size );\r\n\t/*\tgo through each block\t*/\r\n\tfor( j = 0; j < height; j += 4 )\r\n\t{\r\n\t\tfor( i = 0; i < width; i += 4 )\r\n\t\t{\r\n\t\t\t/*\tcopy this block into a new one\t*/\r\n\t\t\tint idx = 0;\r\n\t\t\tint mx = 4, my = 4;\r\n\t\t\tif( j+4 >= height )\r\n\t\t\t{\r\n\t\t\t\tmy = height - j;\r\n\t\t\t}\r\n\t\t\tif( i+4 >= width )\r\n\t\t\t{\r\n\t\t\t\tmx = width - i;\r\n\t\t\t}\r\n\t\t\tfor( y = 0; y < my; ++y )\r\n\t\t\t{\r\n\t\t\t\tfor( x = 0; x < mx; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];\r\n\t\t\t\t}\r\n\t\t\t\tfor( x = mx; x < 4; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = ublock[0];\r\n\t\t\t\t\tublock[idx++] = ublock[1];\r\n\t\t\t\t\tublock[idx++] = ublock[2];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tfor( y = my; y < 4; ++y )\r\n\t\t\t{\r\n\t\t\t\tfor( x = 0; x < 4; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = ublock[0];\r\n\t\t\t\t\tublock[idx++] = ublock[1];\r\n\t\t\t\t\tublock[idx++] = ublock[2];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t/*\tcompress the block\t*/\r\n\t\t\t++block_count;\r\n\t\t\tcompress_DDS_color_block( 3, ublock, cblock );\r\n\t\t\t/*\tcopy the data from the block into the main block\t*/\r\n\t\t\tfor( x = 0; x < 8; ++x )\r\n\t\t\t{\r\n\t\t\t\tcompressed[index++] = cblock[x];\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn compressed;\r\n}\r\n\r\nunsigned char* convert_image_to_DXT5(\r\n\t\tconst unsigned char *const uncompressed,\r\n\t\tint width, int height, int channels,\r\n\t\tint *out_size )\r\n{\r\n\tunsigned char *compressed;\r\n\tint i, j, x, y;\r\n\tunsigned char ublock[16*4];\r\n\tunsigned char cblock[8];\r\n\tint index = 0, chan_step = 1;\r\n\tint block_count = 0, has_alpha;\r\n\t/*\terror check\t*/\r\n\t*out_size = 0;\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(NULL == uncompressed) ||\r\n\t\t(channels < 1) || ( channels > 4) )\r\n\t{\r\n\t\treturn NULL;\r\n\t}\r\n\t/*\tfor channels == 1 or 2, I do not step forward for R,G,B vales\t*/\r\n\tif( channels < 3 )\r\n\t{\r\n\t\tchan_step = 0;\r\n\t}\r\n\t/*\t# channels = 1 or 3 have no alpha, 2 & 4 do have alpha\t*/\r\n\thas_alpha = 1 - (channels & 1);\r\n\t/*\tget the RAM for the compressed image\r\n\t\t(16 bytes per 4x4 pixel block)\t*/\r\n\t*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 16;\r\n\tcompressed = (unsigned char*)malloc( *out_size );\r\n\t/*\tgo through each block\t*/\r\n\tfor( j = 0; j < height; j += 4 )\r\n\t{\r\n\t\tfor( i = 0; i < width; i += 4 )\r\n\t\t{\r\n\t\t\t/*\tlocal variables, and my block counter\t*/\r\n\t\t\tint idx = 0;\r\n\t\t\tint mx = 4, my = 4;\r\n\t\t\tif( j+4 >= height )\r\n\t\t\t{\r\n\t\t\t\tmy = height - j;\r\n\t\t\t}\r\n\t\t\tif( i+4 >= width )\r\n\t\t\t{\r\n\t\t\t\tmx = width - i;\r\n\t\t\t}\r\n\t\t\tfor( y = 0; y < my; ++y )\r\n\t\t\t{\r\n\t\t\t\tfor( x = 0; x < mx; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];\r\n\t\t\t\t\tublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];\r\n\t\t\t\t\tublock[idx++] =\r\n\t\t\t\t\t\thas_alpha * uncompressed[(j+y)*width*channels+(i+x)*channels+channels-1]\r\n\t\t\t\t\t\t+ (1-has_alpha)*255;\r\n\t\t\t\t}\r\n\t\t\t\tfor( x = mx; x < 4; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = ublock[0];\r\n\t\t\t\t\tublock[idx++] = ublock[1];\r\n\t\t\t\t\tublock[idx++] = ublock[2];\r\n\t\t\t\t\tublock[idx++] = ublock[3];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tfor( y = my; y < 4; ++y )\r\n\t\t\t{\r\n\t\t\t\tfor( x = 0; x < 4; ++x )\r\n\t\t\t\t{\r\n\t\t\t\t\tublock[idx++] = ublock[0];\r\n\t\t\t\t\tublock[idx++] = ublock[1];\r\n\t\t\t\t\tublock[idx++] = ublock[2];\r\n\t\t\t\t\tublock[idx++] = ublock[3];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t/*\tnow compress the alpha block\t*/\r\n\t\t\tcompress_DDS_alpha_block( ublock, cblock );\r\n\t\t\t/*\tcopy the data from the compressed alpha block into the main buffer\t*/\r\n\t\t\tfor( x = 0; x < 8; ++x )\r\n\t\t\t{\r\n\t\t\t\tcompressed[index++] = cblock[x];\r\n\t\t\t}\r\n\t\t\t/*\tthen compress the color block\t*/\r\n\t\t\t++block_count;\r\n\t\t\tcompress_DDS_color_block( 4, ublock, cblock );\r\n\t\t\t/*\tcopy the data from the compressed color block into the main buffer\t*/\r\n\t\t\tfor( x = 0; x < 8; ++x )\r\n\t\t\t{\r\n\t\t\t\tcompressed[index++] = cblock[x];\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn compressed;\r\n}\r\n\r\n/********* Helper Functions *********/\r\nint convert_bit_range( int c, int from_bits, int to_bits )\r\n{\r\n\tint b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);\r\n\treturn (b + (b >> from_bits)) >> from_bits;\r\n}\r\n\r\nint rgb_to_565( int r, int g, int b )\r\n{\r\n\treturn\r\n\t\t(convert_bit_range( r, 8, 5 ) << 11) |\r\n\t\t(convert_bit_range( g, 8, 6 ) << 05) |\r\n\t\t(convert_bit_range( b, 8, 5 ) << 00);\r\n}\r\n\r\nvoid rgb_888_from_565( unsigned int c, int *r, int *g, int *b )\r\n{\r\n\t*r = convert_bit_range( (c >> 11) & 31, 5, 8 );\r\n\t*g = convert_bit_range( (c >> 05) & 63, 6, 8 );\r\n\t*b = convert_bit_range( (c >> 00) & 31, 5, 8 );\r\n}\r\n\r\nvoid compute_color_line_STDEV(\r\n\t\tconst unsigned char *const uncompressed,\r\n\t\tint channels,\r\n\t\tfloat point[3], float direction[3] )\r\n{\r\n\tconst float inv_16 = 1.0f / 16.0f;\r\n\tint i;\r\n\tfloat sum_r = 0.0f, sum_g = 0.0f, sum_b = 0.0f;\r\n\tfloat sum_rr = 0.0f, sum_gg = 0.0f, sum_bb = 0.0f;\r\n\tfloat sum_rg = 0.0f, sum_rb = 0.0f, sum_gb = 0.0f;\r\n\t/*\tcalculate all data needed for the covariance matrix\r\n\t\t( to compare with _rygdxt code)\t*/\r\n\tfor( i = 0; i < 16*channels; i += channels )\r\n\t{\r\n\t\tsum_r += uncompressed[i+0];\r\n\t\tsum_rr += uncompressed[i+0] * uncompressed[i+0];\r\n\t\tsum_g += uncompressed[i+1];\r\n\t\tsum_gg += uncompressed[i+1] * uncompressed[i+1];\r\n\t\tsum_b += uncompressed[i+2];\r\n\t\tsum_bb += uncompressed[i+2] * uncompressed[i+2];\r\n\t\tsum_rg += uncompressed[i+0] * uncompressed[i+1];\r\n\t\tsum_rb += uncompressed[i+0] * uncompressed[i+2];\r\n\t\tsum_gb += uncompressed[i+1] * uncompressed[i+2];\r\n\t}\r\n\t/*\tconvert the sums to averages\t*/\r\n\tsum_r *= inv_16;\r\n\tsum_g *= inv_16;\r\n\tsum_b *= inv_16;\r\n\t/*\tand convert the squares to the squares of the value - avg_value\t*/\r\n\tsum_rr -= 16.0f * sum_r * sum_r;\r\n\tsum_gg -= 16.0f * sum_g * sum_g;\r\n\tsum_bb -= 16.0f * sum_b * sum_b;\r\n\tsum_rg -= 16.0f * sum_r * sum_g;\r\n\tsum_rb -= 16.0f * sum_r * sum_b;\r\n\tsum_gb -= 16.0f * sum_g * sum_b;\r\n\t/*\tthe point on the color line is the average\t*/\r\n\tpoint[0] = sum_r;\r\n\tpoint[1] = sum_g;\r\n\tpoint[2] = sum_b;\r\n\t#if USE_COV_MAT\r\n\t/*\r\n\t\tThe following idea was from ryg.\r\n\t\t(https://mollyrocket.com/forums/viewtopic.php?t=392)\r\n\t\tThe method worked great (less RMSE than mine) most of\r\n\t\tthe time, but had some issues handling some simple\r\n\t\tboundary cases, like full green next to full red,\r\n\t\twhich would generate a covariance matrix like this:\r\n\r\n\t\t| 1  -1  0 |\r\n\t\t| -1  1  0 |\r\n\t\t| 0   0  0 |\r\n\r\n\t\tFor a given starting vector, the power method can\r\n\t\tgenerate all zeros!  So no starting with {1,1,1}\r\n\t\tas I was doing!  This kind of error is still a\r\n\t\tslight posibillity, but will be very rare.\r\n\t*/\r\n\t/*\tuse the covariance matrix directly\r\n\t\t(1st iteration, don't use all 1.0 values!)\t*/\r\n\tsum_r = 1.0f;\r\n\tsum_g = 2.718281828f;\r\n\tsum_b = 3.141592654f;\r\n\tdirection[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;\r\n\tdirection[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;\r\n\tdirection[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;\r\n\t/*\t2nd iteration, use results from the 1st guy\t*/\r\n\tsum_r = direction[0];\r\n\tsum_g = direction[1];\r\n\tsum_b = direction[2];\r\n\tdirection[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;\r\n\tdirection[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;\r\n\tdirection[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;\r\n\t/*\t3rd iteration, use results from the 2nd guy\t*/\r\n\tsum_r = direction[0];\r\n\tsum_g = direction[1];\r\n\tsum_b = direction[2];\r\n\tdirection[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;\r\n\tdirection[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;\r\n\tdirection[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;\r\n\t#else\r\n\t/*\tuse my standard deviation method\r\n\t\t(very robust, a tiny bit slower and less accurate)\t*/\r\n\tdirection[0] = sqrt( sum_rr );\r\n\tdirection[1] = sqrt( sum_gg );\r\n\tdirection[2] = sqrt( sum_bb );\r\n\t/*\twhich has a greater component\t*/\r\n\tif( sum_gg > sum_rr )\r\n\t{\r\n\t\t/*\tgreen has greater component, so base the other signs off of green\t*/\r\n\t\tif( sum_rg < 0.0f )\r\n\t\t{\r\n\t\t\tdirection[0] = -direction[0];\r\n\t\t}\r\n\t\tif( sum_gb < 0.0f )\r\n\t\t{\r\n\t\t\tdirection[2] = -direction[2];\r\n\t\t}\r\n\t} else\r\n\t{\r\n\t\t/*\tred has a greater component\t*/\r\n\t\tif( sum_rg < 0.0f )\r\n\t\t{\r\n\t\t\tdirection[1] = -direction[1];\r\n\t\t}\r\n\t\tif( sum_rb < 0.0f )\r\n\t\t{\r\n\t\t\tdirection[2] = -direction[2];\r\n\t\t}\r\n\t}\r\n\t#endif\r\n}\r\n\r\nvoid LSE_master_colors_max_min(\r\n\t\tint *cmax, int *cmin,\r\n\t\tint channels,\r\n\t\tconst unsigned char *const uncompressed )\r\n{\r\n\tint i, j;\r\n\t/*\tthe master colors\t*/\r\n\tint c0[3], c1[3];\r\n\t/*\tused for fitting the line\t*/\r\n\tfloat sum_x[] = { 0.0f, 0.0f, 0.0f };\r\n\tfloat sum_x2[] = { 0.0f, 0.0f, 0.0f };\r\n\tfloat dot_max = 1.0f, dot_min = -1.0f;\r\n\tfloat vec_len2 = 0.0f;\r\n\tfloat dot;\r\n\t/*\terror check\t*/\r\n\tif( (channels < 3) || (channels > 4) )\r\n\t{\r\n\t\treturn;\r\n\t}\r\n\tcompute_color_line_STDEV( uncompressed, channels, sum_x, sum_x2 );\r\n\tvec_len2 = 1.0f / ( 0.00001f +\r\n\t\t\tsum_x2[0]*sum_x2[0] + sum_x2[1]*sum_x2[1] + sum_x2[2]*sum_x2[2] );\r\n\t/*\tfinding the max and min vector values\t*/\r\n\tdot_max =\r\n\t\t\t(\r\n\t\t\t\tsum_x2[0] * uncompressed[0] +\r\n\t\t\t\tsum_x2[1] * uncompressed[1] +\r\n\t\t\t\tsum_x2[2] * uncompressed[2]\r\n\t\t\t);\r\n\tdot_min = dot_max;\r\n\tfor( i = 1; i < 16; ++i )\r\n\t{\r\n\t\tdot =\r\n\t\t\t(\r\n\t\t\t\tsum_x2[0] * uncompressed[i*channels+0] +\r\n\t\t\t\tsum_x2[1] * uncompressed[i*channels+1] +\r\n\t\t\t\tsum_x2[2] * uncompressed[i*channels+2]\r\n\t\t\t);\r\n\t\tif( dot < dot_min )\r\n\t\t{\r\n\t\t\tdot_min = dot;\r\n\t\t} else if( dot > dot_max )\r\n\t\t{\r\n\t\t\tdot_max = dot;\r\n\t\t}\r\n\t}\r\n\t/*\tand the offset (from the average location)\t*/\r\n\tdot = sum_x2[0]*sum_x[0] + sum_x2[1]*sum_x[1] + sum_x2[2]*sum_x[2];\r\n\tdot_min -= dot;\r\n\tdot_max -= dot;\r\n\t/*\tpost multiply by the scaling factor\t*/\r\n\tdot_min *= vec_len2;\r\n\tdot_max *= vec_len2;\r\n\t/*\tOK, build the master colors\t*/\r\n\tfor( i = 0; i < 3; ++i )\r\n\t{\r\n\t\t/*\tcolor 0\t*/\r\n\t\tc0[i] = (int)(0.5f + sum_x[i] + dot_max * sum_x2[i]);\r\n\t\tif( c0[i] < 0 )\r\n\t\t{\r\n\t\t\tc0[i] = 0;\r\n\t\t} else if( c0[i] > 255 )\r\n\t\t{\r\n\t\t\tc0[i] = 255;\r\n\t\t}\r\n\t\t/*\tcolor 1\t*/\r\n\t\tc1[i] = (int)(0.5f + sum_x[i] + dot_min * sum_x2[i]);\r\n\t\tif( c1[i] < 0 )\r\n\t\t{\r\n\t\t\tc1[i] = 0;\r\n\t\t} else if( c1[i] > 255 )\r\n\t\t{\r\n\t\t\tc1[i] = 255;\r\n\t\t}\r\n\t}\r\n\t/*\tdown_sample (with rounding?)\t*/\r\n\ti = rgb_to_565( c0[0], c0[1], c0[2] );\r\n\tj = rgb_to_565( c1[0], c1[1], c1[2] );\r\n\tif( i > j )\r\n\t{\r\n\t\t*cmax = i;\r\n\t\t*cmin = j;\r\n\t} else\r\n\t{\r\n\t\t*cmax = j;\r\n\t\t*cmin = i;\r\n\t}\r\n}\r\n\r\nvoid\r\n\tcompress_DDS_color_block\r\n\t(\r\n\t\tint channels,\r\n\t\tconst unsigned char *const uncompressed,\r\n\t\tunsigned char compressed[8]\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tint i;\r\n\tint next_bit;\r\n\tint enc_c0, enc_c1;\r\n\tint c0[4], c1[4];\r\n\tfloat color_line[] = { 0.0f, 0.0f, 0.0f, 0.0f };\r\n\tfloat vec_len2 = 0.0f, dot_offset = 0.0f;\r\n\t/*\tstupid order\t*/\r\n\tint swizzle4[] = { 0, 2, 3, 1 };\r\n\t/*\tget the master colors\t*/\r\n\tLSE_master_colors_max_min( &enc_c0, &enc_c1, channels, uncompressed );\r\n\t/*\tstore the 565 color 0 and color 1\t*/\r\n\tcompressed[0] = (enc_c0 >> 0) & 255;\r\n\tcompressed[1] = (enc_c0 >> 8) & 255;\r\n\tcompressed[2] = (enc_c1 >> 0) & 255;\r\n\tcompressed[3] = (enc_c1 >> 8) & 255;\r\n\t/*\tzero out the compressed data\t*/\r\n\tcompressed[4] = 0;\r\n\tcompressed[5] = 0;\r\n\tcompressed[6] = 0;\r\n\tcompressed[7] = 0;\r\n\t/*\treconstitute the master color vectors\t*/\r\n\trgb_888_from_565( enc_c0, &c0[0], &c0[1], &c0[2] );\r\n\trgb_888_from_565( enc_c1, &c1[0], &c1[1], &c1[2] );\r\n\t/*\tthe new vector\t*/\r\n\tvec_len2 = 0.0f;\r\n\tfor( i = 0; i < 3; ++i )\r\n\t{\r\n\t\tcolor_line[i] = (float)(c1[i] - c0[i]);\r\n\t\tvec_len2 += color_line[i] * color_line[i];\r\n\t}\r\n\tif( vec_len2 > 0.0f )\r\n\t{\r\n\t\tvec_len2 = 1.0f / vec_len2;\r\n\t}\r\n\t/*\tpre-proform the scaling\t*/\r\n\tcolor_line[0] *= vec_len2;\r\n\tcolor_line[1] *= vec_len2;\r\n\tcolor_line[2] *= vec_len2;\r\n\t/*\tcompute the offset (constant) portion of the dot product\t*/\r\n\tdot_offset = color_line[0]*c0[0] + color_line[1]*c0[1] + color_line[2]*c0[2];\r\n\t/*\tstore the rest of the bits\t*/\r\n\tnext_bit = 8*4;\r\n\tfor( i = 0; i < 16; ++i )\r\n\t{\r\n\t\t/*\tfind the dot product of this color, to place it on the line\r\n\t\t\t(should be [-1,1])\t*/\r\n\t\tint next_value = 0;\r\n\t\tfloat dot_product =\r\n\t\t\tcolor_line[0] * uncompressed[i*channels+0] +\r\n\t\t\tcolor_line[1] * uncompressed[i*channels+1] +\r\n\t\t\tcolor_line[2] * uncompressed[i*channels+2] -\r\n\t\t\tdot_offset;\r\n\t\t/*\tmap to [0,3]\t*/\r\n\t\tnext_value = (int)( dot_product * 3.0f + 0.5f );\r\n\t\tif( next_value > 3 )\r\n\t\t{\r\n\t\t\tnext_value = 3;\r\n\t\t} else if( next_value < 0 )\r\n\t\t{\r\n\t\t\tnext_value = 0;\r\n\t\t}\r\n\t\t/*\tOK, store this value\t*/\r\n\t\tcompressed[next_bit >> 3] |= swizzle4[ next_value ] << (next_bit & 7);\r\n\t\tnext_bit += 2;\r\n\t}\r\n\t/*\tdone compressing to DXT1\t*/\r\n}\r\n\r\nvoid\r\n\tcompress_DDS_alpha_block\r\n\t(\r\n\t\tconst unsigned char *const uncompressed,\r\n\t\tunsigned char compressed[8]\r\n\t)\r\n{\r\n\t/*\tvariables\t*/\r\n\tint i;\r\n\tint next_bit;\r\n\tint a0, a1;\r\n\tfloat scale_me;\r\n\t/*\tstupid order\t*/\r\n\tint swizzle8[] = { 1, 7, 6, 5, 4, 3, 2, 0 };\r\n\t/*\tget the alpha limits (a0 > a1)\t*/\r\n\ta0 = a1 = uncompressed[3];\r\n\tfor( i = 4+3; i < 16*4; i += 4 )\r\n\t{\r\n\t\tif( uncompressed[i] > a0 )\r\n\t\t{\r\n\t\t\ta0 = uncompressed[i];\r\n\t\t} else if( uncompressed[i] < a1 )\r\n\t\t{\r\n\t\t\ta1 = uncompressed[i];\r\n\t\t}\r\n\t}\r\n\t/*\tstore those limits, and zero the rest of the compressed dataset\t*/\r\n\tcompressed[0] = a0;\r\n\tcompressed[1] = a1;\r\n\t/*\tzero out the compressed data\t*/\r\n\tcompressed[2] = 0;\r\n\tcompressed[3] = 0;\r\n\tcompressed[4] = 0;\r\n\tcompressed[5] = 0;\r\n\tcompressed[6] = 0;\r\n\tcompressed[7] = 0;\r\n\t/*\tstore the all of the alpha values\t*/\r\n\tnext_bit = 8*2;\r\n\tscale_me = 7.9999f / (a0 - a1);\r\n\tfor( i = 3; i < 16*4; i += 4 )\r\n\t{\r\n\t\t/*\tconvert this alpha value to a 3 bit number\t*/\r\n\t\tint svalue;\r\n\t\tint value = (int)((uncompressed[i] - a1) * scale_me);\r\n\t\tsvalue = swizzle8[ value&7 ];\r\n\t\t/*\tOK, store this value, start with the 1st byte\t*/\r\n\t\tcompressed[next_bit >> 3] |= svalue << (next_bit & 7);\r\n\t\tif( (next_bit & 7) > 5 )\r\n\t\t{\r\n\t\t\t/*\tspans 2 bytes, fill in the start of the 2nd byte\t*/\r\n\t\t\tcompressed[1 + (next_bit >> 3)] |= svalue >> (8 - (next_bit & 7) );\r\n\t\t}\r\n\t\tnext_bit += 3;\r\n\t}\r\n\t/*\tdone compressing to DXT1\t*/\r\n}\r\n"
  },
  {
    "path": "src/image_DXT.h",
    "content": "/*\r\n\tJonathan Dummer\r\n\t2007-07-31-10.32\r\n\r\n\tsimple DXT compression / decompression code\r\n\r\n\tpublic domain\r\n*/\r\n\r\n#ifndef HEADER_IMAGE_DXT\r\n#define HEADER_IMAGE_DXT\r\n\r\n/**\r\n\tConverts an image from an array of unsigned chars (RGB or RGBA) to\r\n\tDXT1 or DXT5, then saves the converted image to disk.\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nint\r\nsave_image_as_DDS\r\n(\r\n    const char *filename,\r\n    int width, int height, int channels,\r\n    const unsigned char *const data\r\n);\r\n\r\n/**\r\n\ttake an image and convert it to DXT1 (no alpha)\r\n**/\r\nunsigned char*\r\nconvert_image_to_DXT1\r\n(\r\n    const unsigned char *const uncompressed,\r\n    int width, int height, int channels,\r\n    int *out_size\r\n);\r\n\r\n/**\r\n\ttake an image and convert it to DXT5 (with alpha)\r\n**/\r\nunsigned char*\r\nconvert_image_to_DXT5\r\n(\r\n    const unsigned char *const uncompressed,\r\n    int width, int height, int channels,\r\n    int *out_size\r\n);\r\n\r\n/**\tA bunch of DirectDraw Surface structures and flags **/\r\ntypedef struct\r\n{\r\n    unsigned int    dwMagic;\r\n    unsigned int    dwSize;\r\n    unsigned int    dwFlags;\r\n    unsigned int    dwHeight;\r\n    unsigned int    dwWidth;\r\n    unsigned int    dwPitchOrLinearSize;\r\n    unsigned int    dwDepth;\r\n    unsigned int    dwMipMapCount;\r\n    unsigned int    dwReserved1[ 11 ];\r\n\r\n    /*  DDPIXELFORMAT\t*/\r\n    struct\r\n    {\r\n        unsigned int    dwSize;\r\n        unsigned int    dwFlags;\r\n        unsigned int    dwFourCC;\r\n        unsigned int    dwRGBBitCount;\r\n        unsigned int    dwRBitMask;\r\n        unsigned int    dwGBitMask;\r\n        unsigned int    dwBBitMask;\r\n        unsigned int    dwAlphaBitMask;\r\n    }\r\n    sPixelFormat;\r\n\r\n    /*  DDCAPS2\t*/\r\n    struct\r\n    {\r\n        unsigned int    dwCaps1;\r\n        unsigned int    dwCaps2;\r\n        unsigned int    dwDDSX;\r\n        unsigned int    dwReserved;\r\n    }\r\n    sCaps;\r\n    unsigned int    dwReserved2;\r\n}\r\nDDS_header ;\r\n\r\n/*\tthe following constants were copied directly off the MSDN website\t*/\r\n\r\n/*\tThe dwFlags member of the original DDSURFACEDESC2 structure\r\n\tcan be set to one or more of the following values.\t*/\r\n#define DDSD_CAPS\t0x00000001\r\n#define DDSD_HEIGHT\t0x00000002\r\n#define DDSD_WIDTH\t0x00000004\r\n#define DDSD_PITCH\t0x00000008\r\n#define DDSD_PIXELFORMAT\t0x00001000\r\n#define DDSD_MIPMAPCOUNT\t0x00020000\r\n#define DDSD_LINEARSIZE\t0x00080000\r\n#define DDSD_DEPTH\t0x00800000\r\n\r\n/*\tDirectDraw Pixel Format\t*/\r\n#define DDPF_ALPHAPIXELS\t0x00000001\r\n#define DDPF_FOURCC\t0x00000004\r\n#define DDPF_RGB\t0x00000040\r\n\r\n/*\tThe dwCaps1 member of the DDSCAPS2 structure can be\r\n\tset to one or more of the following values.\t*/\r\n#define DDSCAPS_COMPLEX\t0x00000008\r\n#define DDSCAPS_TEXTURE\t0x00001000\r\n#define DDSCAPS_MIPMAP\t0x00400000\r\n\r\n/*\tThe dwCaps2 member of the DDSCAPS2 structure can be\r\n\tset to one or more of the following values.\t\t*/\r\n#define DDSCAPS2_CUBEMAP\t0x00000200\r\n#define DDSCAPS2_CUBEMAP_POSITIVEX\t0x00000400\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEX\t0x00000800\r\n#define DDSCAPS2_CUBEMAP_POSITIVEY\t0x00001000\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEY\t0x00002000\r\n#define DDSCAPS2_CUBEMAP_POSITIVEZ\t0x00004000\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEZ\t0x00008000\r\n#define DDSCAPS2_VOLUME\t0x00200000\r\n\r\n#endif /* HEADER_IMAGE_DXT\t*/\r\n"
  },
  {
    "path": "src/image_helper.c",
    "content": "/*\r\n    Jonathan Dummer\r\n\r\n    image helper functions\r\n\r\n    MIT license\r\n*/\r\n\r\n#include \"image_helper.h\"\r\n#include <stdlib.h>\r\n#include <math.h>\r\n\r\n/*\tUpscaling the image uses simple bilinear interpolation\t*/\r\nint\r\n\tup_scale_image\r\n\t(\r\n\t\tconst unsigned char* const orig,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned char* resampled,\r\n\t\tint resampled_width, int resampled_height\r\n\t)\r\n{\r\n\tfloat dx, dy;\r\n\tint x, y, c;\r\n\r\n    /* error(s) check\t*/\r\n    if ( \t(width < 1) || (height < 1) ||\r\n            (resampled_width < 2) || (resampled_height < 2) ||\r\n            (channels < 1) ||\r\n            (NULL == orig) || (NULL == resampled) )\r\n    {\r\n        /*\tsignify badness\t*/\r\n        return 0;\r\n    }\r\n    /*\r\n\t\tfor each given pixel in the new map, find the exact location\r\n\t\tfrom the original map which would contribute to this guy\r\n\t*/\r\n    dx = (width - 1.0f) / (resampled_width - 1.0f);\r\n    dy = (height - 1.0f) / (resampled_height - 1.0f);\r\n    for ( y = 0; y < resampled_height; ++y )\r\n    {\r\n    \t/* find the base y index and fractional offset from that\t*/\r\n    \tfloat sampley = y * dy;\r\n    \tint inty = (int)sampley;\r\n    \t/*\tif( inty < 0 ) { inty = 0; } else\t*/\r\n\t\tif( inty > height - 2 ) { inty = height - 2; }\r\n\t\tsampley -= inty;\r\n        for ( x = 0; x < resampled_width; ++x )\r\n        {\r\n\t\t\tfloat samplex = x * dx;\r\n\t\t\tint intx = (int)samplex;\r\n\t\t\tint base_index;\r\n\t\t\t/* find the base x index and fractional offset from that\t*/\r\n\t\t\t/*\tif( intx < 0 ) { intx = 0; } else\t*/\r\n\t\t\tif( intx > width - 2 ) { intx = width - 2; }\r\n\t\t\tsamplex -= intx;\r\n\t\t\t/*\tbase index into the original image\t*/\r\n\t\t\tbase_index = (inty * width + intx) * channels;\r\n            for ( c = 0; c < channels; ++c )\r\n            {\r\n            \t/*\tdo the sampling\t*/\r\n\t\t\t\tfloat value = 0.5f;\r\n\t\t\t\tvalue += orig[base_index]\r\n\t\t\t\t\t\t\t*(1.0f-samplex)*(1.0f-sampley);\r\n\t\t\t\tvalue += orig[base_index+channels]\r\n\t\t\t\t\t\t\t*(samplex)*(1.0f-sampley);\r\n\t\t\t\tvalue += orig[base_index+width*channels]\r\n\t\t\t\t\t\t\t*(1.0f-samplex)*(sampley);\r\n\t\t\t\tvalue += orig[base_index+width*channels+channels]\r\n\t\t\t\t\t\t\t*(samplex)*(sampley);\r\n\t\t\t\t/*\tmove to the next channel\t*/\r\n\t\t\t\t++base_index;\r\n            \t/*\tsave the new value\t*/\r\n            \tresampled[y*resampled_width*channels+x*channels+c] =\r\n\t\t\t\t\t\t(unsigned char)(value);\r\n            }\r\n        }\r\n    }\r\n    /*\tdone\t*/\r\n    return 1;\r\n}\r\n\r\nint\r\n\tmipmap_image\r\n\t(\r\n\t\tconst unsigned char* const orig,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned char* resampled,\r\n\t\tint block_size_x, int block_size_y\r\n\t)\r\n{\r\n\tint mip_width, mip_height;\r\n\tint i, j, c;\r\n\r\n\t/*\terror check\t*/\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(channels < 1) || (orig == NULL) ||\r\n\t\t(resampled == NULL) ||\r\n\t\t(block_size_x < 1) || (block_size_y < 1) )\r\n\t{\r\n\t\t/*\tnothing to do\t*/\r\n\t\treturn 0;\r\n\t}\r\n\tmip_width = width / block_size_x;\r\n\tmip_height = height / block_size_y;\r\n\tif( mip_width < 1 )\r\n\t{\r\n\t\tmip_width = 1;\r\n\t}\r\n\tif( mip_height < 1 )\r\n\t{\r\n\t\tmip_height = 1;\r\n\t}\r\n\tfor( j = 0; j < mip_height; ++j )\r\n\t{\r\n\t\tfor( i = 0; i < mip_width; ++i )\r\n\t\t{\r\n\t\t\tfor( c = 0; c < channels; ++c )\r\n\t\t\t{\r\n\t\t\t\tconst int index = (j*block_size_y)*width*channels + (i*block_size_x)*channels + c;\r\n\t\t\t\tint sum_value;\r\n\t\t\t\tint u,v;\r\n\t\t\t\tint u_block = block_size_x;\r\n\t\t\t\tint v_block = block_size_y;\r\n\t\t\t\tint block_area;\r\n\t\t\t\t/*\tdo a bit of checking so we don't over-run the boundaries\r\n\t\t\t\t\t(necessary for non-square textures!)\t*/\r\n\t\t\t\tif( block_size_x * (i+1) > width )\r\n\t\t\t\t{\r\n\t\t\t\t\tu_block = width - i*block_size_y;\r\n\t\t\t\t}\r\n\t\t\t\tif( block_size_y * (j+1) > height )\r\n\t\t\t\t{\r\n\t\t\t\t\tv_block = height - j*block_size_y;\r\n\t\t\t\t}\r\n\t\t\t\tblock_area = u_block*v_block;\r\n\t\t\t\t/*\tfor this pixel, see what the average\r\n\t\t\t\t\tof all the values in the block are.\r\n\t\t\t\t\tnote: start the sum at the rounding value, not at 0\t*/\r\n\t\t\t\tsum_value = block_area >> 1;\r\n\t\t\t\tfor( v = 0; v < v_block; ++v )\r\n\t\t\t\tfor( u = 0; u < u_block; ++u )\r\n\t\t\t\t{\r\n\t\t\t\t\tsum_value += orig[index + v*width*channels + u*channels];\r\n\t\t\t\t}\r\n\t\t\t\tresampled[j*mip_width*channels + i*channels + c] = sum_value / block_area;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nint\r\n\tscale_image_RGB_to_NTSC_safe\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t)\r\n{\r\n\tconst float scale_lo = 16.0f - 0.499f;\r\n\tconst float scale_hi = 235.0f + 0.499f;\r\n\tint i, j;\r\n\tint nc = channels;\r\n\tunsigned char scale_LUT[256];\r\n\t/*\terror check\t*/\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(channels < 1) || (orig == NULL) )\r\n\t{\r\n\t\t/*\tnothing to do\t*/\r\n\t\treturn 0;\r\n\t}\r\n\t/*\tset up the scaling Look Up Table\t*/\r\n\tfor( i = 0; i < 256; ++i )\r\n\t{\r\n\t\tscale_LUT[i] = (unsigned char)((scale_hi - scale_lo) * i / 255.0f + scale_lo);\r\n\t}\r\n\t/*\tfor channels = 2 or 4, ignore the alpha component\t*/\r\n\tnc -= 1 - (channels & 1);\r\n\t/*\tOK, go through the image and scale any non-alpha components\t*/\r\n\tfor( i = 0; i < width*height*channels; i += channels )\r\n\t{\r\n\t\tfor( j = 0; j < nc; ++j )\r\n\t\t{\r\n\t\t\torig[i+j] = scale_LUT[orig[i+j]];\r\n\t\t}\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nunsigned char clamp_byte( int x ) { return ( (x) < 0 ? (0) : ( (x) > 255 ? 255 : (x) ) ); }\r\n\r\n/*\r\n\tThis function takes the RGB components of the image\r\n\tand converts them into YCoCg.  3 components will be\r\n\tre-ordered to CoYCg (for optimum DXT1 compression),\r\n\twhile 4 components will be ordered CoCgAY (for DXT5\r\n\tcompression).\r\n*/\r\nint\r\n\tconvert_RGB_to_YCoCg\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t)\r\n{\r\n\tint i;\r\n\t/*\terror check\t*/\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(channels < 3) || (channels > 4) ||\r\n\t\t(orig == NULL) )\r\n\t{\r\n\t\t/*\tnothing to do\t*/\r\n\t\treturn -1;\r\n\t}\r\n\t/*\tdo the conversion\t*/\r\n\tif( channels == 3 )\r\n\t{\r\n\t\tfor( i = 0; i < width*height*3; i += 3 )\r\n\t\t{\r\n\t\t\tint r = orig[i+0];\r\n\t\t\tint g = (orig[i+1] + 1) >> 1;\r\n\t\t\tint b = orig[i+2];\r\n\t\t\tint tmp = (2 + r + b) >> 2;\r\n\t\t\t/*\tCo\t*/\r\n\t\t\torig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );\r\n\t\t\t/*\tY\t*/\r\n\t\t\torig[i+1] = clamp_byte( g + tmp );\r\n\t\t\t/*\tCg\t*/\r\n\t\t\torig[i+2] = clamp_byte( 128 + g - tmp );\r\n\t\t}\r\n\t} else\r\n\t{\r\n\t\tfor( i = 0; i < width*height*4; i += 4 )\r\n\t\t{\r\n\t\t\tint r = orig[i+0];\r\n\t\t\tint g = (orig[i+1] + 1) >> 1;\r\n\t\t\tint b = orig[i+2];\r\n\t\t\tunsigned char a = orig[i+3];\r\n\t\t\tint tmp = (2 + r + b) >> 2;\r\n\t\t\t/*\tCo\t*/\r\n\t\t\torig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );\r\n\t\t\t/*\tCg\t*/\r\n\t\t\torig[i+1] = clamp_byte( 128 + g - tmp );\r\n\t\t\t/*\tAlpha\t*/\r\n\t\t\torig[i+2] = a;\r\n\t\t\t/*\tY\t*/\r\n\t\t\torig[i+3] = clamp_byte( g + tmp );\r\n\t\t}\r\n\t}\r\n\t/*\tdone\t*/\r\n\treturn 0;\r\n}\r\n\r\n/*\r\n\tThis function takes the YCoCg components of the image\r\n\tand converts them into RGB.  See above.\r\n*/\r\nint\r\n\tconvert_YCoCg_to_RGB\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t)\r\n{\r\n\tint i;\r\n\t/*\terror check\t*/\r\n\tif( (width < 1) || (height < 1) ||\r\n\t\t(channels < 3) || (channels > 4) ||\r\n\t\t(orig == NULL) )\r\n\t{\r\n\t\t/*\tnothing to do\t*/\r\n\t\treturn -1;\r\n\t}\r\n\t/*\tdo the conversion\t*/\r\n\tif( channels == 3 )\r\n\t{\r\n\t\tfor( i = 0; i < width*height*3; i += 3 )\r\n\t\t{\r\n\t\t\tint co = orig[i+0] - 128;\r\n\t\t\tint y  = orig[i+1];\r\n\t\t\tint cg = orig[i+2] - 128;\r\n\t\t\t/*\tR\t*/\r\n\t\t\torig[i+0] = clamp_byte( y + co - cg );\r\n\t\t\t/*\tG\t*/\r\n\t\t\torig[i+1] = clamp_byte( y + cg );\r\n\t\t\t/*\tB\t*/\r\n\t\t\torig[i+2] = clamp_byte( y - co - cg );\r\n\t\t}\r\n\t} else\r\n\t{\r\n\t\tfor( i = 0; i < width*height*4; i += 4 )\r\n\t\t{\r\n\t\t\tint co = orig[i+0] - 128;\r\n\t\t\tint cg = orig[i+1] - 128;\r\n\t\t\tunsigned char a  = orig[i+2];\r\n\t\t\tint y  = orig[i+3];\r\n\t\t\t/*\tR\t*/\r\n\t\t\torig[i+0] = clamp_byte( y + co - cg );\r\n\t\t\t/*\tG\t*/\r\n\t\t\torig[i+1] = clamp_byte( y + cg );\r\n\t\t\t/*\tB\t*/\r\n\t\t\torig[i+2] = clamp_byte( y - co - cg );\r\n\t\t\t/*\tA\t*/\r\n\t\t\torig[i+3] = a;\r\n\t\t}\r\n\t}\r\n\t/*\tdone\t*/\r\n\treturn 0;\r\n}\r\n\r\nfloat\r\nfind_max_RGBE\r\n(\r\n\tunsigned char *image,\r\n    int width, int height\r\n)\r\n{\r\n\tfloat max_val = 0.0f;\r\n\tunsigned char *img = image;\r\n\tint i, j;\r\n\tfor( i = width * height; i > 0; --i )\r\n\t{\r\n\t\t/* float scale = powf( 2.0f, img[3] - 128.0f ) / 255.0f; */\r\n\t\tfloat scale = ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );\r\n\t\tfor( j = 0; j < 3; ++j )\r\n\t\t{\r\n\t\t\tif( img[j] * scale > max_val )\r\n\t\t\t{\r\n\t\t\t\tmax_val = img[j] * scale;\r\n\t\t\t}\r\n\t\t}\r\n\t\t/* next pixel */\r\n\t\timg += 4;\r\n\t}\r\n\treturn max_val;\r\n}\r\n\r\nint\r\nRGBE_to_RGBdivA\r\n(\r\n    unsigned char *image,\r\n    int width, int height,\r\n    int rescale_to_max\r\n)\r\n{\r\n\t/* local variables */\r\n\tint i, iv;\r\n\tunsigned char *img = image;\r\n\tfloat scale = 1.0f;\r\n\t/* error check */\r\n\tif( (!image) || (width < 1) || (height < 1) )\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\t/* convert (note: no negative numbers, but 0.0 is possible) */\r\n\tif( rescale_to_max )\r\n\t{\r\n\t\tscale = 255.0f / find_max_RGBE( image, width, height );\r\n\t}\r\n\tfor( i = width * height; i > 0; --i )\r\n\t{\r\n\t\t/* decode this pixel, and find the max */\r\n\t\tfloat r,g,b,e, m;\r\n\t\t/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */\r\n\t\te = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );\r\n\t\tr = e * img[0];\r\n\t\tg = e * img[1];\r\n\t\tb = e * img[2];\r\n\t\tm = (r > g) ? r : g;\r\n\t\tm = (b > m) ? b : m;\r\n\t\t/* and encode it into RGBdivA */\r\n\t\tiv = (m != 0.0f) ? (int)(255.0f / m) : 1.0f;\r\n\t\tiv = (iv < 1) ? 1 : iv;\r\n\t\timg[3] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * r + 0.5f);\r\n\t\timg[0] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * g + 0.5f);\r\n\t\timg[1] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * b + 0.5f);\r\n\t\timg[2] = (iv > 255) ? 255 : iv;\r\n\t\t/* and on to the next pixel */\r\n\t\timg += 4;\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nint\r\nRGBE_to_RGBdivA2\r\n(\r\n    unsigned char *image,\r\n    int width, int height,\r\n    int rescale_to_max\r\n)\r\n{\r\n\t/* local variables */\r\n\tint i, iv;\r\n\tunsigned char *img = image;\r\n\tfloat scale = 1.0f;\r\n\t/* error check */\r\n\tif( (!image) || (width < 1) || (height < 1) )\r\n\t{\r\n\t\treturn 0;\r\n\t}\r\n\t/* convert (note: no negative numbers, but 0.0 is possible) */\r\n\tif( rescale_to_max )\r\n\t{\r\n\t\tscale = 255.0f * 255.0f / find_max_RGBE( image, width, height );\r\n\t}\r\n\tfor( i = width * height; i > 0; --i )\r\n\t{\r\n\t\t/* decode this pixel, and find the max */\r\n\t\tfloat r,g,b,e, m;\r\n\t\t/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */\r\n\t\te = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );\r\n\t\tr = e * img[0];\r\n\t\tg = e * img[1];\r\n\t\tb = e * img[2];\r\n\t\tm = (r > g) ? r : g;\r\n\t\tm = (b > m) ? b : m;\r\n\t\t/* and encode it into RGBdivA */\r\n\t\tiv = (m != 0.0f) ? (int)sqrtf( 255.0f * 255.0f / m ) : 1.0f;\r\n\t\tiv = (iv < 1) ? 1 : iv;\r\n\t\timg[3] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * img[3] * r / 255.0f + 0.5f);\r\n\t\timg[0] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * img[3] * g / 255.0f + 0.5f);\r\n\t\timg[1] = (iv > 255) ? 255 : iv;\r\n\t\tiv = (int)(img[3] * img[3] * b / 255.0f + 0.5f);\r\n\t\timg[2] = (iv > 255) ? 255 : iv;\r\n\t\t/* and on to the next pixel */\r\n\t\timg += 4;\r\n\t}\r\n\treturn 1;\r\n}\r\n"
  },
  {
    "path": "src/image_helper.h",
    "content": "/*\r\n    Jonathan Dummer\r\n\r\n    Image helper functions\r\n\r\n    MIT license\r\n*/\r\n\r\n#ifndef HEADER_IMAGE_HELPER\r\n#define HEADER_IMAGE_HELPER\r\n\r\n#ifdef __cplusplus\r\nextern \"C\" {\r\n#endif\r\n\r\n/**\r\n\tThis function upscales an image.\r\n\tNot to be used to create MIPmaps,\r\n\tbut to make it square,\r\n\tor to make it a power-of-two sized.\r\n**/\r\nint\r\n\tup_scale_image\r\n\t(\r\n\t\tconst unsigned char* const orig,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned char* resampled,\r\n\t\tint resampled_width, int resampled_height\r\n\t);\r\n\r\n/**\r\n\tThis function downscales an image.\r\n\tUsed for creating MIPmaps,\r\n\tthe incoming image should be a\r\n\tpower-of-two sized.\r\n**/\r\nint\r\n\tmipmap_image\r\n\t(\r\n\t\tconst unsigned char* const orig,\r\n\t\tint width, int height, int channels,\r\n\t\tunsigned char* resampled,\r\n\t\tint block_size_x, int block_size_y\r\n\t);\r\n\r\n/**\r\n\tThis function takes the RGB components of the image\r\n\tand scales each channel from [0,255] to [16,235].\r\n\tThis makes the colors \"Safe\" for display on NTSC\r\n\tdisplays.  Note that this is _NOT_ a good idea for\r\n\tloading images like normal- or height-maps!\r\n**/\r\nint\r\n\tscale_image_RGB_to_NTSC_safe\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t);\r\n\r\n/**\r\n\tThis function takes the RGB components of the image\r\n\tand converts them into YCoCg.  3 components will be\r\n\tre-ordered to CoYCg (for optimum DXT1 compression),\r\n\twhile 4 components will be ordered CoCgAY (for DXT5\r\n\tcompression).\r\n**/\r\nint\r\n\tconvert_RGB_to_YCoCg\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t);\r\n\r\n/**\r\n\tThis function takes the YCoCg components of the image\r\n\tand converts them into RGB.  See above.\r\n**/\r\nint\r\n\tconvert_YCoCg_to_RGB\r\n\t(\r\n\t\tunsigned char* orig,\r\n\t\tint width, int height, int channels\r\n\t);\r\n\r\n/**\r\n\tConverts an HDR image from an array\r\n\tof unsigned chars (RGBE) to RGBdivA\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nint\r\n\tRGBE_to_RGBdivA\r\n\t(\r\n\t\tunsigned char *image,\r\n\t\tint width, int height,\r\n\t\tint rescale_to_max\r\n\t);\r\n\r\n/**\r\n\tConverts an HDR image from an array\r\n\tof unsigned chars (RGBE) to RGBdivA2\r\n\t\\return 0 if failed, otherwise returns 1\r\n**/\r\nint\r\n\tRGBE_to_RGBdivA2\r\n\t(\r\n\t\tunsigned char *image,\r\n\t\tint width, int height,\r\n\t\tint rescale_to_max\r\n\t);\r\n\r\n#ifdef __cplusplus\r\n}\r\n#endif\r\n\r\n#endif /* HEADER_IMAGE_HELPER\t*/\r\n"
  },
  {
    "path": "src/stb_image_aug.c",
    "content": "/* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c\r\n                      when you control the images you're loading\r\n\r\n   QUICK NOTES:\r\n      Primarily of interest to game developers and other people who can\r\n          avoid problematic images and only need the trivial interface\r\n\r\n      JPEG baseline (no JPEG progressive, no oddball channel decimations)\r\n      PNG non-interlaced\r\n      BMP non-1bpp, non-RLE\r\n      TGA (not sure what subset, if a subset)\r\n      PSD (composited view only, no extra channels)\r\n      HDR (radiance rgbE format)\r\n      writes BMP,TGA (define STBI_NO_WRITE to remove code)\r\n      decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)\r\n      supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)\r\n\r\n   TODO:\r\n      stbi_info_*\r\n\r\n   history:\r\n      1.16   major bugfix - convert_format converted one too many pixels\r\n      1.15   initialize some fields for thread safety\r\n      1.14   fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including)\r\n      1.13   threadsafe\r\n      1.12   const qualifiers in the API\r\n      1.11   Support installable IDCT, colorspace conversion routines\r\n      1.10   Fixes for 64-bit (don't use \"unsigned long\")\r\n             optimized upsampling by Fabian \"ryg\" Giesen\r\n      1.09   Fix format-conversion for PSD code (bad global variables!)\r\n      1.08   Thatcher Ulrich's PSD code integrated by Nicolas Schulz\r\n      1.07   attempt to fix C++ warning/errors again\r\n      1.06   attempt to fix C++ warning/errors again\r\n      1.05   fix TGA loading to return correct *comp and use good luminance calc\r\n      1.04   default float alpha is 1, not 255; use 'void *' for stbi_image_free\r\n      1.03   bugfixes to STBI_NO_STDIO, STBI_NO_HDR\r\n      1.02   support for (subset of) HDR files, float interface for preferred access to them\r\n      1.01   fix bug: possible bug in handling right-side up bmps... not sure\r\n             fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all\r\n      1.00   interface to zlib that skips zlib header\r\n      0.99   correct handling of alpha in palette\r\n      0.98   TGA loader by lonesock; dynamically add loaders (untested)\r\n      0.97   jpeg errors on too large a file; also catch another malloc failure\r\n      0.96   fix detection of invalid v value - particleman@mollyrocket forum\r\n      0.95   during header scan, seek to markers in case of padding\r\n      0.94   STBI_NO_STDIO to disable stdio usage; rename all #defines the same\r\n      0.93   handle jpegtran output; verbose errors\r\n      0.92   read 4,8,16,24,32-bit BMP files of several formats\r\n      0.91   output 24-bit Windows 3.0 BMP files\r\n      0.90   fix a few more warnings; bump version number to approach 1.0\r\n      0.61   bugfixes due to Marc LeBlanc, Christopher Lloyd\r\n      0.60   fix compiling as c++\r\n      0.59   fix warnings: merge Dave Moore's -Wall fixes\r\n      0.58   fix bug: zlib uncompressed mode len/nlen was wrong endian\r\n      0.57   fix bug: jpg last huffman symbol before marker was >9 bits but less\r\n                      than 16 available\r\n      0.56   fix bug: zlib uncompressed mode len vs. nlen\r\n      0.55   fix bug: restart_interval not initialized to 0\r\n      0.54   allow NULL for 'int *comp'\r\n      0.53   fix bug in png 3->4; speedup png decoding\r\n      0.52   png handles req_comp=3,4 directly; minor cleanup; jpeg comments\r\n      0.51   obey req_comp requests, 1-component jpegs return as 1-component,\r\n             on 'test' only check type, not whether we support this variant\r\n*/\r\n\r\n#include \"stb_image_aug.h\"\r\n\r\n#ifndef STBI_NO_HDR\r\n#include <math.h>  // ldexp\r\n#include <string.h> // strcmp\r\n#endif\r\n\r\n#ifndef STBI_NO_STDIO\r\n#include <stdio.h>\r\n#endif\r\n#include <stdlib.h>\r\n#include <memory.h>\r\n#include <assert.h>\r\n#include <stdarg.h>\r\n\r\n#ifndef _MSC_VER\r\n  #ifdef __cplusplus\r\n  #define __forceinline inline\r\n  #else\r\n  #define __forceinline\r\n  #endif\r\n#endif\r\n\r\n\r\n// implementation:\r\ntypedef unsigned char uint8;\r\ntypedef unsigned short uint16;\r\ntypedef   signed short  int16;\r\ntypedef unsigned int   uint32;\r\ntypedef   signed int    int32;\r\ntypedef unsigned int   uint;\r\n\r\n// should produce compiler error if size is wrong\r\ntypedef unsigned char validate_uint32[sizeof(uint32)==4];\r\n\r\n#if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)\r\n#define STBI_NO_WRITE\r\n#endif\r\n\r\n#ifndef STBI_NO_DDS\r\n#include \"stbi_DDS_aug.h\"\r\n#endif\r\n\r\n//\tI (JLD) want full messages for SOIL\r\n#define STBI_FAILURE_USERMSG 1\r\n\r\n//////////////////////////////////////////////////////////////////////////////\r\n//\r\n// Generic API that works on all image types\r\n//\r\n\r\n// this is not threadsafe\r\nstatic char *failure_reason;\r\n\r\nchar *stbi_failure_reason(void)\r\n{\r\n   return failure_reason;\r\n}\r\n\r\nstatic int e(char *str)\r\n{\r\n   failure_reason = str;\r\n   return 0;\r\n}\r\n\r\n#ifdef STBI_NO_FAILURE_STRINGS\r\n   #define e(x,y)  0\r\n#elif defined(STBI_FAILURE_USERMSG)\r\n   #define e(x,y)  e(y)\r\n#else\r\n   #define e(x,y)  e(x)\r\n#endif\r\n\r\n#define epf(x,y)   ((float *) (e(x,y)?NULL:NULL))\r\n#define epuc(x,y)  ((unsigned char *) (e(x,y)?NULL:NULL))\r\n\r\nvoid stbi_image_free(void *retval_from_stbi_load)\r\n{\r\n   free(retval_from_stbi_load);\r\n}\r\n\r\n#define MAX_LOADERS  32\r\nstbi_loader *loaders[MAX_LOADERS];\r\nstatic int max_loaders = 0;\r\n\r\nint stbi_register_loader(stbi_loader *loader)\r\n{\r\n   int i;\r\n   for (i=0; i < MAX_LOADERS; ++i) {\r\n      // already present?\r\n      if (loaders[i] == loader)\r\n         return 1;\r\n      // end of the list?\r\n      if (loaders[i] == NULL) {\r\n         loaders[i] = loader;\r\n         max_loaders = i+1;\r\n         return 1;\r\n      }\r\n   }\r\n   // no room for it\r\n   return 0;\r\n}\r\n\r\n#ifndef STBI_NO_HDR\r\nstatic float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);\r\nstatic stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp);\r\n#endif\r\n\r\n#ifndef STBI_NO_STDIO\r\nunsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   FILE *f = fopen(filename, \"rb\");\r\n   unsigned char *result;\r\n   if (!f) return epuc(\"can't fopen\", \"Unable to open file\");\r\n   result = stbi_load_from_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return result;\r\n}\r\n\r\nunsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   int i;\r\n   if (stbi_jpeg_test_file(f))\r\n      return stbi_jpeg_load_from_file(f,x,y,comp,req_comp);\r\n   if (stbi_png_test_file(f))\r\n      return stbi_png_load_from_file(f,x,y,comp,req_comp);\r\n   if (stbi_bmp_test_file(f))\r\n      return stbi_bmp_load_from_file(f,x,y,comp,req_comp);\r\n   if (stbi_psd_test_file(f))\r\n      return stbi_psd_load_from_file(f,x,y,comp,req_comp);\r\n   #ifndef STBI_NO_DDS\r\n   if (stbi_dds_test_file(f))\r\n      return stbi_dds_load_from_file(f,x,y,comp,req_comp);\r\n   #endif\r\n   #ifndef STBI_NO_HDR\r\n   if (stbi_hdr_test_file(f)) {\r\n      float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp);\r\n      return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);\r\n   }\r\n   #endif\r\n   for (i=0; i < max_loaders; ++i)\r\n      if (loaders[i]->test_file(f))\r\n         return loaders[i]->load_from_file(f,x,y,comp,req_comp);\r\n   // test tga last because it's a crappy test!\r\n   if (stbi_tga_test_file(f))\r\n      return stbi_tga_load_from_file(f,x,y,comp,req_comp);\r\n   return epuc(\"unknown image type\", \"Image not of any known type, or corrupt\");\r\n}\r\n#endif\r\n\r\nunsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   int i;\r\n   if (stbi_jpeg_test_memory(buffer,len))\r\n      return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   if (stbi_png_test_memory(buffer,len))\r\n      return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   if (stbi_bmp_test_memory(buffer,len))\r\n      return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   if (stbi_psd_test_memory(buffer,len))\r\n      return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   #ifndef STBI_NO_DDS\r\n   if (stbi_dds_test_memory(buffer,len))\r\n      return stbi_dds_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   #endif\r\n   #ifndef STBI_NO_HDR\r\n   if (stbi_hdr_test_memory(buffer, len)) {\r\n      float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);\r\n      return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);\r\n   }\r\n   #endif\r\n   for (i=0; i < max_loaders; ++i)\r\n      if (loaders[i]->test_memory(buffer,len))\r\n         return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   // test tga last because it's a crappy test!\r\n   if (stbi_tga_test_memory(buffer,len))\r\n      return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp);\r\n   return epuc(\"unknown image type\", \"Image not of any known type, or corrupt\");\r\n}\r\n\r\n#ifndef STBI_NO_HDR\r\n\r\n#ifndef STBI_NO_STDIO\r\nfloat *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   FILE *f = fopen(filename, \"rb\");\r\n   float *result;\r\n   if (!f) return epf(\"can't fopen\", \"Unable to open file\");\r\n   result = stbi_loadf_from_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return result;\r\n}\r\n\r\nfloat *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   unsigned char *data;\r\n   #ifndef STBI_NO_HDR\r\n   if (stbi_hdr_test_file(f))\r\n      return stbi_hdr_load_from_file(f,x,y,comp,req_comp);\r\n   #endif\r\n   data = stbi_load_from_file(f, x, y, comp, req_comp);\r\n   if (data)\r\n      return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);\r\n   return epf(\"unknown image type\", \"Image not of any known type, or corrupt\");\r\n}\r\n#endif\r\n\r\nfloat *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi_uc *data;\r\n   #ifndef STBI_NO_HDR\r\n   if (stbi_hdr_test_memory(buffer, len))\r\n      return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);\r\n   #endif\r\n   data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp);\r\n   if (data)\r\n      return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);\r\n   return epf(\"unknown image type\", \"Image not of any known type, or corrupt\");\r\n}\r\n#endif\r\n\r\n// these is-hdr-or-not is defined independent of whether STBI_NO_HDR is\r\n// defined, for API simplicity; if STBI_NO_HDR is defined, it always\r\n// reports false!\r\n\r\nint stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)\r\n{\r\n   #ifndef STBI_NO_HDR\r\n   return stbi_hdr_test_memory(buffer, len);\r\n   #else\r\n   return 0;\r\n   #endif\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_is_hdr          (char const *filename)\r\n{\r\n   FILE *f = fopen(filename, \"rb\");\r\n   int result=0;\r\n   if (f) {\r\n      result = stbi_is_hdr_from_file(f);\r\n      fclose(f);\r\n   }\r\n   return result;\r\n}\r\n\r\nextern int      stbi_is_hdr_from_file(FILE *f)\r\n{\r\n   #ifndef STBI_NO_HDR\r\n   return stbi_hdr_test_file(f);\r\n   #else\r\n   return 0;\r\n   #endif\r\n}\r\n\r\n#endif\r\n\r\n// @TODO: get image dimensions & components without fully decoding\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_info            (char const *filename,           int *x, int *y, int *comp);\r\nextern int      stbi_info_from_file  (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\nextern int      stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\n\r\n#ifndef STBI_NO_HDR\r\nstatic float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;\r\nstatic float l2h_gamma=2.2f, l2h_scale=1.0f;\r\n\r\nvoid   stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }\r\nvoid   stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }\r\n\r\nvoid   stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }\r\nvoid   stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }\r\n#endif\r\n\r\n\r\n//////////////////////////////////////////////////////////////////////////////\r\n//\r\n// Common code used by all image loaders\r\n//\r\n\r\nenum\r\n{\r\n   SCAN_load=0,\r\n   SCAN_type,\r\n   SCAN_header,\r\n};\r\n\r\ntypedef struct\r\n{\r\n   uint32 img_x, img_y;\r\n   int img_n, img_out_n;\r\n\r\n   #ifndef STBI_NO_STDIO\r\n   FILE  *img_file;\r\n   #endif\r\n   uint8 *img_buffer, *img_buffer_end;\r\n} stbi;\r\n\r\n#ifndef STBI_NO_STDIO\r\nstatic void start_file(stbi *s, FILE *f)\r\n{\r\n   s->img_file = f;\r\n}\r\n#endif\r\n\r\nstatic void start_mem(stbi *s, uint8 const *buffer, int len)\r\n{\r\n#ifndef STBI_NO_STDIO\r\n   s->img_file = NULL;\r\n#endif\r\n   s->img_buffer = (uint8 *) buffer;\r\n   s->img_buffer_end = (uint8 *) buffer+len;\r\n}\r\n\r\n__forceinline static int get8(stbi *s)\r\n{\r\n#ifndef STBI_NO_STDIO\r\n   if (s->img_file) {\r\n      int c = fgetc(s->img_file);\r\n      return c == EOF ? 0 : c;\r\n   }\r\n#endif\r\n   if (s->img_buffer < s->img_buffer_end)\r\n      return *s->img_buffer++;\r\n   return 0;\r\n}\r\n\r\n__forceinline static int at_eof(stbi *s)\r\n{\r\n#ifndef STBI_NO_STDIO\r\n   if (s->img_file)\r\n      return feof(s->img_file);\r\n#endif\r\n   return s->img_buffer >= s->img_buffer_end;\r\n}\r\n\r\n__forceinline static uint8 get8u(stbi *s)\r\n{\r\n   return (uint8) get8(s);\r\n}\r\n\r\nstatic void skip(stbi *s, int n)\r\n{\r\n#ifndef STBI_NO_STDIO\r\n   if (s->img_file)\r\n      fseek(s->img_file, n, SEEK_CUR);\r\n   else\r\n#endif\r\n      s->img_buffer += n;\r\n}\r\n\r\nstatic int get16(stbi *s)\r\n{\r\n   int z = get8(s);\r\n   return (z << 8) + get8(s);\r\n}\r\n\r\nstatic uint32 get32(stbi *s)\r\n{\r\n   uint32 z = get16(s);\r\n   return (z << 16) + get16(s);\r\n}\r\n\r\nstatic int get16le(stbi *s)\r\n{\r\n   int z = get8(s);\r\n   return z + (get8(s) << 8);\r\n}\r\n\r\nstatic uint32 get32le(stbi *s)\r\n{\r\n   uint32 z = get16le(s);\r\n   return z + (get16le(s) << 16);\r\n}\r\n\r\nstatic void getn(stbi *s, stbi_uc *buffer, int n)\r\n{\r\n#ifndef STBI_NO_STDIO\r\n   if (s->img_file) {\r\n      fread(buffer, 1, n, s->img_file);\r\n      return;\r\n   }\r\n#endif\r\n   memcpy(buffer, s->img_buffer, n);\r\n   s->img_buffer += n;\r\n}\r\n\r\n//////////////////////////////////////////////////////////////////////////////\r\n//\r\n//  generic converter from built-in img_n to req_comp\r\n//    individual types do this automatically as much as possible (e.g. jpeg\r\n//    does all cases internally since it needs to colorspace convert anyway,\r\n//    and it never has alpha, so very few cases ). png can automatically\r\n//    interleave an alpha=255 channel, but falls back to this for other cases\r\n//\r\n//  assume data buffer is malloced, so malloc a new one and free that one\r\n//  only failure mode is malloc failing\r\n\r\nstatic uint8 compute_y(int r, int g, int b)\r\n{\r\n   return (uint8) (((r*77) + (g*150) +  (29*b)) >> 8);\r\n}\r\n\r\nstatic unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)\r\n{\r\n   int i,j;\r\n   unsigned char *good;\r\n\r\n   if (req_comp == img_n) return data;\r\n   assert(req_comp >= 1 && req_comp <= 4);\r\n\r\n   good = (unsigned char *) malloc(req_comp * x * y);\r\n   if (good == NULL) {\r\n      free(data);\r\n      return epuc(\"outofmem\", \"Out of memory\");\r\n   }\r\n\r\n   for (j=0; j < (int) y; ++j) {\r\n      unsigned char *src  = data + j * x * img_n   ;\r\n      unsigned char *dest = good + j * x * req_comp;\r\n\r\n      #define COMBO(a,b)  ((a)*8+(b))\r\n      #define CASE(a,b)   case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)\r\n      // convert source image with img_n components to one with req_comp components;\r\n      // avoid switch per pixel, so use switch per scanline and massive macros\r\n      switch(COMBO(img_n, req_comp)) {\r\n         CASE(1,2) dest[0]=src[0], dest[1]=255; break;\r\n         CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;\r\n         CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;\r\n         CASE(2,1) dest[0]=src[0]; break;\r\n         CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;\r\n         CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;\r\n         CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;\r\n         CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break;\r\n         CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;\r\n         CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;\r\n         CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;\r\n         CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;\r\n         default: assert(0);\r\n      }\r\n      #undef CASE\r\n   }\r\n\r\n   free(data);\r\n   return good;\r\n}\r\n\r\n#ifndef STBI_NO_HDR\r\nstatic float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)\r\n{\r\n   int i,k,n;\r\n   float *output = (float *) malloc(x * y * comp * sizeof(float));\r\n   if (output == NULL) { free(data); return epf(\"outofmem\", \"Out of memory\"); }\r\n   // compute number of non-alpha components\r\n   if (comp & 1) n = comp; else n = comp-1;\r\n   for (i=0; i < x*y; ++i) {\r\n      for (k=0; k < n; ++k) {\r\n         output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;\r\n      }\r\n      if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;\r\n   }\r\n   free(data);\r\n   return output;\r\n}\r\n\r\n#define float2int(x)   ((int) (x))\r\nstatic stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp)\r\n{\r\n   int i,k,n;\r\n   stbi_uc *output = (stbi_uc *) malloc(x * y * comp);\r\n   if (output == NULL) { free(data); return epuc(\"outofmem\", \"Out of memory\"); }\r\n   // compute number of non-alpha components\r\n   if (comp & 1) n = comp; else n = comp-1;\r\n   for (i=0; i < x*y; ++i) {\r\n      for (k=0; k < n; ++k) {\r\n         float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;\r\n         if (z < 0) z = 0;\r\n         if (z > 255) z = 255;\r\n         output[i*comp + k] = float2int(z);\r\n      }\r\n      if (k < comp) {\r\n         float z = data[i*comp+k] * 255 + 0.5f;\r\n         if (z < 0) z = 0;\r\n         if (z > 255) z = 255;\r\n         output[i*comp + k] = float2int(z);\r\n      }\r\n   }\r\n   free(data);\r\n   return output;\r\n}\r\n#endif\r\n\r\n//////////////////////////////////////////////////////////////////////////////\r\n//\r\n//  \"baseline\" JPEG/JFIF decoder (not actually fully baseline implementation)\r\n//\r\n//    simple implementation\r\n//      - channel subsampling of at most 2 in each dimension\r\n//      - doesn't support delayed output of y-dimension\r\n//      - simple interface (only one output format: 8-bit interleaved RGB)\r\n//      - doesn't try to recover corrupt jpegs\r\n//      - doesn't allow partial loading, loading multiple at once\r\n//      - still fast on x86 (copying globals into locals doesn't help x86)\r\n//      - allocates lots of intermediate memory (full size of all components)\r\n//        - non-interleaved case requires this anyway\r\n//        - allows good upsampling (see next)\r\n//    high-quality\r\n//      - upsampled channels are bilinearly interpolated, even across blocks\r\n//      - quality integer IDCT derived from IJG's 'slow'\r\n//    performance\r\n//      - fast huffman; reasonable integer IDCT\r\n//      - uses a lot of intermediate memory, could cache poorly\r\n//      - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4\r\n//          stb_jpeg:   1.34 seconds (MSVC6, default release build)\r\n//          stb_jpeg:   1.06 seconds (MSVC6, processor = Pentium Pro)\r\n//          IJL11.dll:  1.08 seconds (compiled by intel)\r\n//          IJG 1998:   0.98 seconds (MSVC6, makefile provided by IJG)\r\n//          IJG 1998:   0.95 seconds (MSVC6, makefile + proc=PPro)\r\n\r\n// huffman decoding acceleration\r\n#define FAST_BITS   9  // larger handles more cases; smaller stomps less cache\r\n\r\ntypedef struct\r\n{\r\n   uint8  fast[1 << FAST_BITS];\r\n   // weirdly, repacking this into AoS is a 10% speed loss, instead of a win\r\n   uint16 code[256];\r\n   uint8  values[256];\r\n   uint8  size[257];\r\n   unsigned int maxcode[18];\r\n   int    delta[17];   // old 'firstsymbol' - old 'firstcode'\r\n} huffman;\r\n\r\ntypedef struct\r\n{\r\n   #if STBI_SIMD\r\n   unsigned short dequant2[4][64];\r\n   #endif\r\n   stbi s;\r\n   huffman huff_dc[4];\r\n   huffman huff_ac[4];\r\n   uint8 dequant[4][64];\r\n\r\n// sizes for components, interleaved MCUs\r\n   int img_h_max, img_v_max;\r\n   int img_mcu_x, img_mcu_y;\r\n   int img_mcu_w, img_mcu_h;\r\n\r\n// definition of jpeg image component\r\n   struct\r\n   {\r\n      int id;\r\n      int h,v;\r\n      int tq;\r\n      int hd,ha;\r\n      int dc_pred;\r\n\r\n      int x,y,w2,h2;\r\n      uint8 *data;\r\n      void *raw_data;\r\n      uint8 *linebuf;\r\n   } img_comp[4];\r\n\r\n   uint32         code_buffer; // jpeg entropy-coded buffer\r\n   int            code_bits;   // number of valid bits\r\n   unsigned char  marker;      // marker seen while filling entropy buffer\r\n   int            nomore;      // flag if we saw a marker so must stop\r\n\r\n   int scan_n, order[4];\r\n   int restart_interval, todo;\r\n} jpeg;\r\n\r\nstatic int build_huffman(huffman *h, int *count)\r\n{\r\n   int i,j,k=0,code;\r\n   // build size list for each symbol (from JPEG spec)\r\n   for (i=0; i < 16; ++i)\r\n      for (j=0; j < count[i]; ++j)\r\n         h->size[k++] = (uint8) (i+1);\r\n   h->size[k] = 0;\r\n\r\n   // compute actual symbols (from jpeg spec)\r\n   code = 0;\r\n   k = 0;\r\n   for(j=1; j <= 16; ++j) {\r\n      // compute delta to add to code to compute symbol id\r\n      h->delta[j] = k - code;\r\n      if (h->size[k] == j) {\r\n         while (h->size[k] == j)\r\n            h->code[k++] = (uint16) (code++);\r\n         if (code-1 >= (1 << j)) return e(\"bad code lengths\",\"Corrupt JPEG\");\r\n      }\r\n      // compute largest code + 1 for this size, preshifted as needed later\r\n      h->maxcode[j] = code << (16-j);\r\n      code <<= 1;\r\n   }\r\n   h->maxcode[j] = 0xffffffff;\r\n\r\n   // build non-spec acceleration table; 255 is flag for not-accelerated\r\n   memset(h->fast, 255, 1 << FAST_BITS);\r\n   for (i=0; i < k; ++i) {\r\n      int s = h->size[i];\r\n      if (s <= FAST_BITS) {\r\n         int c = h->code[i] << (FAST_BITS-s);\r\n         int m = 1 << (FAST_BITS-s);\r\n         for (j=0; j < m; ++j) {\r\n            h->fast[c+j] = (uint8) i;\r\n         }\r\n      }\r\n   }\r\n   return 1;\r\n}\r\n\r\nstatic void grow_buffer_unsafe(jpeg *j)\r\n{\r\n   do {\r\n      int b = j->nomore ? 0 : get8(&j->s);\r\n      if (b == 0xff) {\r\n         int c = get8(&j->s);\r\n         if (c != 0) {\r\n            j->marker = (unsigned char) c;\r\n            j->nomore = 1;\r\n            return;\r\n         }\r\n      }\r\n      j->code_buffer = (j->code_buffer << 8) | b;\r\n      j->code_bits += 8;\r\n   } while (j->code_bits <= 24);\r\n}\r\n\r\n// (1 << n) - 1\r\nstatic uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};\r\n\r\n// decode a jpeg huffman value from the bitstream\r\n__forceinline static int decode(jpeg *j, huffman *h)\r\n{\r\n   unsigned int temp;\r\n   int c,k;\r\n\r\n   if (j->code_bits < 16) grow_buffer_unsafe(j);\r\n\r\n   // look at the top FAST_BITS and determine what symbol ID it is,\r\n   // if the code is <= FAST_BITS\r\n   c = (j->code_buffer >> (j->code_bits - FAST_BITS)) & ((1 << FAST_BITS)-1);\r\n   k = h->fast[c];\r\n   if (k < 255) {\r\n      if (h->size[k] > j->code_bits)\r\n         return -1;\r\n      j->code_bits -= h->size[k];\r\n      return h->values[k];\r\n   }\r\n\r\n   // naive test is to shift the code_buffer down so k bits are\r\n   // valid, then test against maxcode. To speed this up, we've\r\n   // preshifted maxcode left so that it has (16-k) 0s at the\r\n   // end; in other words, regardless of the number of bits, it\r\n   // wants to be compared against something shifted to have 16;\r\n   // that way we don't need to shift inside the loop.\r\n   if (j->code_bits < 16)\r\n      temp = (j->code_buffer << (16 - j->code_bits)) & 0xffff;\r\n   else\r\n      temp = (j->code_buffer >> (j->code_bits - 16)) & 0xffff;\r\n   for (k=FAST_BITS+1 ; ; ++k)\r\n      if (temp < h->maxcode[k])\r\n         break;\r\n   if (k == 17) {\r\n      // error! code not found\r\n      j->code_bits -= 16;\r\n      return -1;\r\n   }\r\n\r\n   if (k > j->code_bits)\r\n      return -1;\r\n\r\n   // convert the huffman code to the symbol id\r\n   c = ((j->code_buffer >> (j->code_bits - k)) & bmask[k]) + h->delta[k];\r\n   assert((((j->code_buffer) >> (j->code_bits - h->size[c])) & bmask[h->size[c]]) == h->code[c]);\r\n\r\n   // convert the id to a symbol\r\n   j->code_bits -= k;\r\n   return h->values[c];\r\n}\r\n\r\n// combined JPEG 'receive' and JPEG 'extend', since baseline\r\n// always extends everything it receives.\r\n__forceinline static int extend_receive(jpeg *j, int n)\r\n{\r\n   unsigned int m = 1 << (n-1);\r\n   unsigned int k;\r\n   if (j->code_bits < n) grow_buffer_unsafe(j);\r\n   k = (j->code_buffer >> (j->code_bits - n)) & bmask[n];\r\n   j->code_bits -= n;\r\n   // the following test is probably a random branch that won't\r\n   // predict well. I tried to table accelerate it but failed.\r\n   // maybe it's compiling as a conditional move?\r\n   if (k < m)\r\n      return (-1 << n) + k + 1;\r\n   else\r\n      return k;\r\n}\r\n\r\n// given a value that's at position X in the zigzag stream,\r\n// where does it appear in the 8x8 matrix coded as row-major?\r\nstatic uint8 dezigzag[64+15] =\r\n{\r\n    0,  1,  8, 16,  9,  2,  3, 10,\r\n   17, 24, 32, 25, 18, 11,  4,  5,\r\n   12, 19, 26, 33, 40, 48, 41, 34,\r\n   27, 20, 13,  6,  7, 14, 21, 28,\r\n   35, 42, 49, 56, 57, 50, 43, 36,\r\n   29, 22, 15, 23, 30, 37, 44, 51,\r\n   58, 59, 52, 45, 38, 31, 39, 46,\r\n   53, 60, 61, 54, 47, 55, 62, 63,\r\n   // let corrupt input sample past end\r\n   63, 63, 63, 63, 63, 63, 63, 63,\r\n   63, 63, 63, 63, 63, 63, 63\r\n};\r\n\r\n// decode one 64-entry block--\r\nstatic int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)\r\n{\r\n   int diff,dc,k;\r\n   int t = decode(j, hdc);\r\n   if (t < 0) return e(\"bad huffman code\",\"Corrupt JPEG\");\r\n\r\n   // 0 all the ac values now so we can do it 32-bits at a time\r\n   memset(data,0,64*sizeof(data[0]));\r\n\r\n   diff = t ? extend_receive(j, t) : 0;\r\n   dc = j->img_comp[b].dc_pred + diff;\r\n   j->img_comp[b].dc_pred = dc;\r\n   data[0] = (short) dc;\r\n\r\n   // decode AC components, see JPEG spec\r\n   k = 1;\r\n   do {\r\n      int r,s;\r\n      int rs = decode(j, hac);\r\n      if (rs < 0) return e(\"bad huffman code\",\"Corrupt JPEG\");\r\n      s = rs & 15;\r\n      r = rs >> 4;\r\n      if (s == 0) {\r\n         if (rs != 0xf0) break; // end block\r\n         k += 16;\r\n      } else {\r\n         k += r;\r\n         // decode into unzigzag'd location\r\n         data[dezigzag[k++]] = (short) extend_receive(j,s);\r\n      }\r\n   } while (k < 64);\r\n   return 1;\r\n}\r\n\r\n// take a -128..127 value and clamp it and convert to 0..255\r\n__forceinline static uint8 clamp(int x)\r\n{\r\n   x += 128;\r\n   // trick to use a single test to catch both cases\r\n   if ((unsigned int) x > 255) {\r\n      if (x < 0) return 0;\r\n      if (x > 255) return 255;\r\n   }\r\n   return (uint8) x;\r\n}\r\n\r\n#define f2f(x)  (int) (((x) * 4096 + 0.5))\r\n#define fsh(x)  ((x) << 12)\r\n\r\n// derived from jidctint -- DCT_ISLOW\r\n#define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7)       \\\r\n   int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \\\r\n   p2 = s2;                                    \\\r\n   p3 = s6;                                    \\\r\n   p1 = (p2+p3) * f2f(0.5411961f);             \\\r\n   t2 = p1 + p3*f2f(-1.847759065f);            \\\r\n   t3 = p1 + p2*f2f( 0.765366865f);            \\\r\n   p2 = s0;                                    \\\r\n   p3 = s4;                                    \\\r\n   t0 = fsh(p2+p3);                            \\\r\n   t1 = fsh(p2-p3);                            \\\r\n   x0 = t0+t3;                                 \\\r\n   x3 = t0-t3;                                 \\\r\n   x1 = t1+t2;                                 \\\r\n   x2 = t1-t2;                                 \\\r\n   t0 = s7;                                    \\\r\n   t1 = s5;                                    \\\r\n   t2 = s3;                                    \\\r\n   t3 = s1;                                    \\\r\n   p3 = t0+t2;                                 \\\r\n   p4 = t1+t3;                                 \\\r\n   p1 = t0+t3;                                 \\\r\n   p2 = t1+t2;                                 \\\r\n   p5 = (p3+p4)*f2f( 1.175875602f);            \\\r\n   t0 = t0*f2f( 0.298631336f);                 \\\r\n   t1 = t1*f2f( 2.053119869f);                 \\\r\n   t2 = t2*f2f( 3.072711026f);                 \\\r\n   t3 = t3*f2f( 1.501321110f);                 \\\r\n   p1 = p5 + p1*f2f(-0.899976223f);            \\\r\n   p2 = p5 + p2*f2f(-2.562915447f);            \\\r\n   p3 = p3*f2f(-1.961570560f);                 \\\r\n   p4 = p4*f2f(-0.390180644f);                 \\\r\n   t3 += p1+p4;                                \\\r\n   t2 += p2+p3;                                \\\r\n   t1 += p2+p4;                                \\\r\n   t0 += p1+p3;\r\n\r\n#if !STBI_SIMD\r\n// .344 seconds on 3*anemones.jpg\r\nstatic void idct_block(uint8 *out, int out_stride, short data[64], uint8 *dequantize)\r\n{\r\n   int i,val[64],*v=val;\r\n   uint8 *o,*dq = dequantize;\r\n   short *d = data;\r\n\r\n   // columns\r\n   for (i=0; i < 8; ++i,++d,++dq, ++v) {\r\n      // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing\r\n      if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0\r\n           && d[40]==0 && d[48]==0 && d[56]==0) {\r\n         //    no shortcut                 0     seconds\r\n         //    (1|2|3|4|5|6|7)==0          0     seconds\r\n         //    all separate               -0.047 seconds\r\n         //    1 && 2|3 && 4|5 && 6|7:    -0.047 seconds\r\n         int dcterm = d[0] * dq[0] << 2;\r\n         v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;\r\n      } else {\r\n         IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],\r\n                 d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])\r\n         // constants scaled things up by 1<<12; let's bring them back\r\n         // down, but keep 2 extra bits of precision\r\n         x0 += 512; x1 += 512; x2 += 512; x3 += 512;\r\n         v[ 0] = (x0+t3) >> 10;\r\n         v[56] = (x0-t3) >> 10;\r\n         v[ 8] = (x1+t2) >> 10;\r\n         v[48] = (x1-t2) >> 10;\r\n         v[16] = (x2+t1) >> 10;\r\n         v[40] = (x2-t1) >> 10;\r\n         v[24] = (x3+t0) >> 10;\r\n         v[32] = (x3-t0) >> 10;\r\n      }\r\n   }\r\n\r\n   for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {\r\n      // no fast case since the first 1D IDCT spread components out\r\n      IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])\r\n      // constants scaled things up by 1<<12, plus we had 1<<2 from first\r\n      // loop, plus horizontal and vertical each scale by sqrt(8) so together\r\n      // we've got an extra 1<<3, so 1<<17 total we need to remove.\r\n      x0 += 65536; x1 += 65536; x2 += 65536; x3 += 65536;\r\n      o[0] = clamp((x0+t3) >> 17);\r\n      o[7] = clamp((x0-t3) >> 17);\r\n      o[1] = clamp((x1+t2) >> 17);\r\n      o[6] = clamp((x1-t2) >> 17);\r\n      o[2] = clamp((x2+t1) >> 17);\r\n      o[5] = clamp((x2-t1) >> 17);\r\n      o[3] = clamp((x3+t0) >> 17);\r\n      o[4] = clamp((x3-t0) >> 17);\r\n   }\r\n}\r\n#else\r\nstatic void idct_block(uint8 *out, int out_stride, short data[64], unsigned short *dequantize)\r\n{\r\n   int i,val[64],*v=val;\r\n   uint8 *o;\r\n   unsigned short *dq = dequantize;\r\n   short *d = data;\r\n\r\n   // columns\r\n   for (i=0; i < 8; ++i,++d,++dq, ++v) {\r\n      // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing\r\n      if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0\r\n           && d[40]==0 && d[48]==0 && d[56]==0) {\r\n         //    no shortcut                 0     seconds\r\n         //    (1|2|3|4|5|6|7)==0          0     seconds\r\n         //    all separate               -0.047 seconds\r\n         //    1 && 2|3 && 4|5 && 6|7:    -0.047 seconds\r\n         int dcterm = d[0] * dq[0] << 2;\r\n         v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;\r\n      } else {\r\n         IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],\r\n                 d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])\r\n         // constants scaled things up by 1<<12; let's bring them back\r\n         // down, but keep 2 extra bits of precision\r\n         x0 += 512; x1 += 512; x2 += 512; x3 += 512;\r\n         v[ 0] = (x0+t3) >> 10;\r\n         v[56] = (x0-t3) >> 10;\r\n         v[ 8] = (x1+t2) >> 10;\r\n         v[48] = (x1-t2) >> 10;\r\n         v[16] = (x2+t1) >> 10;\r\n         v[40] = (x2-t1) >> 10;\r\n         v[24] = (x3+t0) >> 10;\r\n         v[32] = (x3-t0) >> 10;\r\n      }\r\n   }\r\n\r\n   for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {\r\n      // no fast case since the first 1D IDCT spread components out\r\n      IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])\r\n      // constants scaled things up by 1<<12, plus we had 1<<2 from first\r\n      // loop, plus horizontal and vertical each scale by sqrt(8) so together\r\n      // we've got an extra 1<<3, so 1<<17 total we need to remove.\r\n      x0 += 65536; x1 += 65536; x2 += 65536; x3 += 65536;\r\n      o[0] = clamp((x0+t3) >> 17);\r\n      o[7] = clamp((x0-t3) >> 17);\r\n      o[1] = clamp((x1+t2) >> 17);\r\n      o[6] = clamp((x1-t2) >> 17);\r\n      o[2] = clamp((x2+t1) >> 17);\r\n      o[5] = clamp((x2-t1) >> 17);\r\n      o[3] = clamp((x3+t0) >> 17);\r\n      o[4] = clamp((x3-t0) >> 17);\r\n   }\r\n}\r\nstatic stbi_idct_8x8 stbi_idct_installed = idct_block;\r\n\r\nextern void stbi_install_idct(stbi_idct_8x8 func)\r\n{\r\n   stbi_idct_installed = func;\r\n}\r\n#endif\r\n\r\n#define MARKER_none  0xff\r\n// if there's a pending marker from the entropy stream, return that\r\n// otherwise, fetch from the stream and get a marker. if there's no\r\n// marker, return 0xff, which is never a valid marker value\r\nstatic uint8 get_marker(jpeg *j)\r\n{\r\n   uint8 x;\r\n   if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }\r\n   x = get8u(&j->s);\r\n   if (x != 0xff) return MARKER_none;\r\n   while (x == 0xff)\r\n      x = get8u(&j->s);\r\n   return x;\r\n}\r\n\r\n// in each scan, we'll have scan_n components, and the order\r\n// of the components is specified by order[]\r\n#define RESTART(x)     ((x) >= 0xd0 && (x) <= 0xd7)\r\n\r\n// after a restart interval, reset the entropy decoder and\r\n// the dc prediction\r\nstatic void reset(jpeg *j)\r\n{\r\n   j->code_bits = 0;\r\n   j->code_buffer = 0;\r\n   j->nomore = 0;\r\n   j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;\r\n   j->marker = MARKER_none;\r\n   j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;\r\n   // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,\r\n   // since we don't even allow 1<<30 pixels\r\n}\r\n\r\nstatic int parse_entropy_coded_data(jpeg *z)\r\n{\r\n   reset(z);\r\n   if (z->scan_n == 1) {\r\n      int i,j;\r\n      #if STBI_SIMD\r\n      __declspec(align(16))\r\n      #endif\r\n      short data[64];\r\n      int n = z->order[0];\r\n      // non-interleaved data, we just need to process one block at a time,\r\n      // in trivial scanline order\r\n      // number of blocks to do just depends on how many actual \"pixels\" this\r\n      // component has, independent of interleaved MCU blocking and such\r\n      int w = (z->img_comp[n].x+7) >> 3;\r\n      int h = (z->img_comp[n].y+7) >> 3;\r\n      for (j=0; j < h; ++j) {\r\n         for (i=0; i < w; ++i) {\r\n            if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;\r\n            #if STBI_SIMD\r\n            stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);\r\n            #else\r\n            idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);\r\n            #endif\r\n            // every data block is an MCU, so countdown the restart interval\r\n            if (--z->todo <= 0) {\r\n               if (z->code_bits < 24) grow_buffer_unsafe(z);\r\n               // if it's NOT a restart, then just bail, so we get corrupt data\r\n               // rather than no data\r\n               if (!RESTART(z->marker)) return 1;\r\n               reset(z);\r\n            }\r\n         }\r\n      }\r\n   } else { // interleaved!\r\n      int i,j,k,x,y;\r\n      short data[64];\r\n      for (j=0; j < z->img_mcu_y; ++j) {\r\n         for (i=0; i < z->img_mcu_x; ++i) {\r\n            // scan an interleaved mcu... process scan_n components in order\r\n            for (k=0; k < z->scan_n; ++k) {\r\n               int n = z->order[k];\r\n               // scan out an mcu's worth of this component; that's just determined\r\n               // by the basic H and V specified for the component\r\n               for (y=0; y < z->img_comp[n].v; ++y) {\r\n                  for (x=0; x < z->img_comp[n].h; ++x) {\r\n                     int x2 = (i*z->img_comp[n].h + x)*8;\r\n                     int y2 = (j*z->img_comp[n].v + y)*8;\r\n                     if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;\r\n                     #if STBI_SIMD\r\n                     stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);\r\n                     #else\r\n                     idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);\r\n                     #endif\r\n                  }\r\n               }\r\n            }\r\n            // after all interleaved components, that's an interleaved MCU,\r\n            // so now count down the restart interval\r\n            if (--z->todo <= 0) {\r\n               if (z->code_bits < 24) grow_buffer_unsafe(z);\r\n               // if it's NOT a restart, then just bail, so we get corrupt data\r\n               // rather than no data\r\n               if (!RESTART(z->marker)) return 1;\r\n               reset(z);\r\n            }\r\n         }\r\n      }\r\n   }\r\n   return 1;\r\n}\r\n\r\nstatic int process_marker(jpeg *z, int m)\r\n{\r\n   int L;\r\n   switch (m) {\r\n      case MARKER_none: // no marker found\r\n         return e(\"expected marker\",\"Corrupt JPEG\");\r\n\r\n      case 0xC2: // SOF - progressive\r\n         return e(\"progressive jpeg\",\"JPEG format not supported (progressive)\");\r\n\r\n      case 0xDD: // DRI - specify restart interval\r\n         if (get16(&z->s) != 4) return e(\"bad DRI len\",\"Corrupt JPEG\");\r\n         z->restart_interval = get16(&z->s);\r\n         return 1;\r\n\r\n      case 0xDB: // DQT - define quantization table\r\n         L = get16(&z->s)-2;\r\n         while (L > 0) {\r\n            int q = get8(&z->s);\r\n            int p = q >> 4;\r\n            int t = q & 15,i;\r\n            if (p != 0) return e(\"bad DQT type\",\"Corrupt JPEG\");\r\n            if (t > 3) return e(\"bad DQT table\",\"Corrupt JPEG\");\r\n            for (i=0; i < 64; ++i)\r\n               z->dequant[t][dezigzag[i]] = get8u(&z->s);\r\n            #if STBI_SIMD\r\n            for (i=0; i < 64; ++i)\r\n               z->dequant2[t][i] = dequant[t][i];\r\n            #endif\r\n            L -= 65;\r\n         }\r\n         return L==0;\r\n\r\n      case 0xC4: // DHT - define huffman table\r\n         L = get16(&z->s)-2;\r\n         while (L > 0) {\r\n            uint8 *v;\r\n            int sizes[16],i,m=0;\r\n            int q = get8(&z->s);\r\n            int tc = q >> 4;\r\n            int th = q & 15;\r\n            if (tc > 1 || th > 3) return e(\"bad DHT header\",\"Corrupt JPEG\");\r\n            for (i=0; i < 16; ++i) {\r\n               sizes[i] = get8(&z->s);\r\n               m += sizes[i];\r\n            }\r\n            L -= 17;\r\n            if (tc == 0) {\r\n               if (!build_huffman(z->huff_dc+th, sizes)) return 0;\r\n               v = z->huff_dc[th].values;\r\n            } else {\r\n               if (!build_huffman(z->huff_ac+th, sizes)) return 0;\r\n               v = z->huff_ac[th].values;\r\n            }\r\n            for (i=0; i < m; ++i)\r\n               v[i] = get8u(&z->s);\r\n            L -= m;\r\n         }\r\n         return L==0;\r\n   }\r\n   // check for comment block or APP blocks\r\n   if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {\r\n      skip(&z->s, get16(&z->s)-2);\r\n      return 1;\r\n   }\r\n   return 0;\r\n}\r\n\r\n// after we see SOS\r\nstatic int process_scan_header(jpeg *z)\r\n{\r\n   int i;\r\n   int Ls = get16(&z->s);\r\n   z->scan_n = get8(&z->s);\r\n   if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s.img_n) return e(\"bad SOS component count\",\"Corrupt JPEG\");\r\n   if (Ls != 6+2*z->scan_n) return e(\"bad SOS len\",\"Corrupt JPEG\");\r\n   for (i=0; i < z->scan_n; ++i) {\r\n      int id = get8(&z->s), which;\r\n      int q = get8(&z->s);\r\n      for (which = 0; which < z->s.img_n; ++which)\r\n         if (z->img_comp[which].id == id)\r\n            break;\r\n      if (which == z->s.img_n) return 0;\r\n      z->img_comp[which].hd = q >> 4;   if (z->img_comp[which].hd > 3) return e(\"bad DC huff\",\"Corrupt JPEG\");\r\n      z->img_comp[which].ha = q & 15;   if (z->img_comp[which].ha > 3) return e(\"bad AC huff\",\"Corrupt JPEG\");\r\n      z->order[i] = which;\r\n   }\r\n   if (get8(&z->s) != 0) return e(\"bad SOS\",\"Corrupt JPEG\");\r\n   get8(&z->s); // should be 63, but might be 0\r\n   if (get8(&z->s) != 0) return e(\"bad SOS\",\"Corrupt JPEG\");\r\n\r\n   return 1;\r\n}\r\n\r\nstatic int process_frame_header(jpeg *z, int scan)\r\n{\r\n   stbi *s = &z->s;\r\n   int Lf,p,i,q, h_max=1,v_max=1,c;\r\n   Lf = get16(s);         if (Lf < 11) return e(\"bad SOF len\",\"Corrupt JPEG\"); // JPEG\r\n   p  = get8(s);          if (p != 8) return e(\"only 8-bit\",\"JPEG format not supported: 8-bit only\"); // JPEG baseline\r\n   s->img_y = get16(s);   if (s->img_y == 0) return e(\"no header height\", \"JPEG format not supported: delayed height\"); // Legal, but we don't handle it--but neither does IJG\r\n   s->img_x = get16(s);   if (s->img_x == 0) return e(\"0 width\",\"Corrupt JPEG\"); // JPEG requires\r\n   c = get8(s);\r\n   if (c != 3 && c != 1) return e(\"bad component count\",\"Corrupt JPEG\");    // JFIF requires\r\n   s->img_n = c;\r\n   for (i=0; i < c; ++i) {\r\n      z->img_comp[i].data = NULL;\r\n      z->img_comp[i].linebuf = NULL;\r\n   }\r\n\r\n   if (Lf != 8+3*s->img_n) return e(\"bad SOF len\",\"Corrupt JPEG\");\r\n\r\n   for (i=0; i < s->img_n; ++i) {\r\n      z->img_comp[i].id = get8(s);\r\n      if (z->img_comp[i].id != i+1)   // JFIF requires\r\n         if (z->img_comp[i].id != i)  // some version of jpegtran outputs non-JFIF-compliant files!\r\n            return e(\"bad component ID\",\"Corrupt JPEG\");\r\n      q = get8(s);\r\n      z->img_comp[i].h = (q >> 4);  if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e(\"bad H\",\"Corrupt JPEG\");\r\n      z->img_comp[i].v = q & 15;    if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e(\"bad V\",\"Corrupt JPEG\");\r\n      z->img_comp[i].tq = get8(s);  if (z->img_comp[i].tq > 3) return e(\"bad TQ\",\"Corrupt JPEG\");\r\n   }\r\n\r\n   if (scan != SCAN_load) return 1;\r\n\r\n   if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e(\"too large\", \"Image too large to decode\");\r\n\r\n   for (i=0; i < s->img_n; ++i) {\r\n      if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;\r\n      if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;\r\n   }\r\n\r\n   // compute interleaved mcu info\r\n   z->img_h_max = h_max;\r\n   z->img_v_max = v_max;\r\n   z->img_mcu_w = h_max * 8;\r\n   z->img_mcu_h = v_max * 8;\r\n   z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;\r\n   z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;\r\n\r\n   for (i=0; i < s->img_n; ++i) {\r\n      // number of effective pixels (e.g. for non-interleaved MCU)\r\n      z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;\r\n      z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;\r\n      // to simplify generation, we'll allocate enough memory to decode\r\n      // the bogus oversized data from using interleaved MCUs and their\r\n      // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't\r\n      // discard the extra data until colorspace conversion\r\n      z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;\r\n      z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;\r\n      z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);\r\n      if (z->img_comp[i].raw_data == NULL) {\r\n         for(--i; i >= 0; --i) {\r\n            free(z->img_comp[i].raw_data);\r\n            z->img_comp[i].data = NULL;\r\n         }\r\n         return e(\"outofmem\", \"Out of memory\");\r\n      }\r\n      // align blocks for installable-idct using mmx/sse\r\n      z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);\r\n      z->img_comp[i].linebuf = NULL;\r\n   }\r\n\r\n   return 1;\r\n}\r\n\r\n// use comparisons since in some cases we handle more than one case (e.g. SOF)\r\n#define DNL(x)         ((x) == 0xdc)\r\n#define SOI(x)         ((x) == 0xd8)\r\n#define EOI(x)         ((x) == 0xd9)\r\n#define SOF(x)         ((x) == 0xc0 || (x) == 0xc1)\r\n#define SOS(x)         ((x) == 0xda)\r\n\r\nstatic int decode_jpeg_header(jpeg *z, int scan)\r\n{\r\n   int m;\r\n   z->marker = MARKER_none; // initialize cached marker to empty\r\n   m = get_marker(z);\r\n   if (!SOI(m)) return e(\"no SOI\",\"Corrupt JPEG\");\r\n   if (scan == SCAN_type) return 1;\r\n   m = get_marker(z);\r\n   while (!SOF(m)) {\r\n      if (!process_marker(z,m)) return 0;\r\n      m = get_marker(z);\r\n      while (m == MARKER_none) {\r\n         // some files have extra padding after their blocks, so ok, we'll scan\r\n         if (at_eof(&z->s)) return e(\"no SOF\", \"Corrupt JPEG\");\r\n         m = get_marker(z);\r\n      }\r\n   }\r\n   if (!process_frame_header(z, scan)) return 0;\r\n   return 1;\r\n}\r\n\r\nstatic int decode_jpeg_image(jpeg *j)\r\n{\r\n   int m;\r\n   j->restart_interval = 0;\r\n   if (!decode_jpeg_header(j, SCAN_load)) return 0;\r\n   m = get_marker(j);\r\n   while (!EOI(m)) {\r\n      if (SOS(m)) {\r\n         if (!process_scan_header(j)) return 0;\r\n         if (!parse_entropy_coded_data(j)) return 0;\r\n      } else {\r\n         if (!process_marker(j, m)) return 0;\r\n      }\r\n      m = get_marker(j);\r\n   }\r\n   return 1;\r\n}\r\n\r\n// static jfif-centered resampling (across block boundaries)\r\n\r\ntypedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,\r\n                                    int w, int hs);\r\n\r\n#define div4(x) ((uint8) ((x) >> 2))\r\n\r\nstatic uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)\r\n{\r\n   return in_near;\r\n}\r\n\r\nstatic uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)\r\n{\r\n   // need to generate two samples vertically for every one in input\r\n   int i;\r\n   for (i=0; i < w; ++i)\r\n      out[i] = div4(3*in_near[i] + in_far[i] + 2);\r\n   return out;\r\n}\r\n\r\nstatic uint8*  resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)\r\n{\r\n   // need to generate two samples horizontally for every one in input\r\n   int i;\r\n   uint8 *input = in_near;\r\n   if (w == 1) {\r\n      // if only one sample, can't do any interpolation\r\n      out[0] = out[1] = input[0];\r\n      return out;\r\n   }\r\n\r\n   out[0] = input[0];\r\n   out[1] = div4(input[0]*3 + input[1] + 2);\r\n   for (i=1; i < w-1; ++i) {\r\n      int n = 3*input[i]+2;\r\n      out[i*2+0] = div4(n+input[i-1]);\r\n      out[i*2+1] = div4(n+input[i+1]);\r\n   }\r\n   out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2);\r\n   out[i*2+1] = input[w-1];\r\n   return out;\r\n}\r\n\r\n#define div16(x) ((uint8) ((x) >> 4))\r\n\r\nstatic uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)\r\n{\r\n   // need to generate 2x2 samples for every one in input\r\n   int i,t0,t1;\r\n   if (w == 1) {\r\n      out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2);\r\n      return out;\r\n   }\r\n\r\n   t1 = 3*in_near[0] + in_far[0];\r\n   out[0] = div4(t1+2);\r\n   for (i=1; i < w; ++i) {\r\n      t0 = t1;\r\n      t1 = 3*in_near[i]+in_far[i];\r\n      out[i*2-1] = div16(3*t0 + t1 + 8);\r\n      out[i*2  ] = div16(3*t1 + t0 + 8);\r\n   }\r\n   out[w*2-1] = div4(t1+2);\r\n   return out;\r\n}\r\n\r\nstatic uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)\r\n{\r\n   // resample with nearest-neighbor\r\n   int i,j;\r\n   for (i=0; i < w; ++i)\r\n      for (j=0; j < hs; ++j)\r\n         out[i*hs+j] = in_near[i];\r\n   return out;\r\n}\r\n\r\n#define float2fixed(x)  ((int) ((x) * 65536 + 0.5))\r\n\r\n// 0.38 seconds on 3*anemones.jpg   (0.25 with processor = Pro)\r\n// VC6 without processor=Pro is generating multiple LEAs per multiply!\r\nstatic void YCbCr_to_RGB_row(uint8 *out, uint8 *y, uint8 *pcb, uint8 *pcr, int count, int step)\r\n{\r\n   int i;\r\n   for (i=0; i < count; ++i) {\r\n      int y_fixed = (y[i] << 16) + 32768; // rounding\r\n      int r,g,b;\r\n      int cr = pcr[i] - 128;\r\n      int cb = pcb[i] - 128;\r\n      r = y_fixed + cr*float2fixed(1.40200f);\r\n      g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);\r\n      b = y_fixed                            + cb*float2fixed(1.77200f);\r\n      r >>= 16;\r\n      g >>= 16;\r\n      b >>= 16;\r\n      if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }\r\n      if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }\r\n      if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }\r\n      out[0] = (uint8)r;\r\n      out[1] = (uint8)g;\r\n      out[2] = (uint8)b;\r\n      out[3] = 255;\r\n      out += step;\r\n   }\r\n}\r\n\r\n#if STBI_SIMD\r\nstatic stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;\r\n\r\nvoid stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)\r\n{\r\n   stbi_YCbCr_installed = func;\r\n}\r\n#endif\r\n\r\n\r\n// clean up the temporary component buffers\r\nstatic void cleanup_jpeg(jpeg *j)\r\n{\r\n   int i;\r\n   for (i=0; i < j->s.img_n; ++i) {\r\n      if (j->img_comp[i].data) {\r\n         free(j->img_comp[i].raw_data);\r\n         j->img_comp[i].data = NULL;\r\n      }\r\n      if (j->img_comp[i].linebuf) {\r\n         free(j->img_comp[i].linebuf);\r\n         j->img_comp[i].linebuf = NULL;\r\n      }\r\n   }\r\n}\r\n\r\ntypedef struct\r\n{\r\n   resample_row_func resample;\r\n   uint8 *line0,*line1;\r\n   int hs,vs;   // expansion factor in each axis\r\n   int w_lores; // horizontal pixels pre-expansion\r\n   int ystep;   // how far through vertical expansion we are\r\n   int ypos;    // which pre-expansion row we're on\r\n} stbi_resample;\r\n\r\nstatic uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)\r\n{\r\n   int n, decode_n;\r\n   // validate req_comp\r\n   if (req_comp < 0 || req_comp > 4) return epuc(\"bad req_comp\", \"Internal error\");\r\n   z->s.img_n = 0;\r\n\r\n   // load a jpeg image from whichever source\r\n   if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; }\r\n\r\n   // determine actual number of components to generate\r\n   n = req_comp ? req_comp : z->s.img_n;\r\n\r\n   if (z->s.img_n == 3 && n < 3)\r\n      decode_n = 1;\r\n   else\r\n      decode_n = z->s.img_n;\r\n\r\n   // resample and color-convert\r\n   {\r\n      int k;\r\n      uint i,j;\r\n      uint8 *output;\r\n      uint8 *coutput[4];\r\n\r\n      stbi_resample res_comp[4];\r\n\r\n      for (k=0; k < decode_n; ++k) {\r\n         stbi_resample *r = &res_comp[k];\r\n\r\n         // allocate line buffer big enough for upsampling off the edges\r\n         // with upsample factor of 4\r\n         z->img_comp[k].linebuf = (uint8 *) malloc(z->s.img_x + 3);\r\n         if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc(\"outofmem\", \"Out of memory\"); }\r\n\r\n         r->hs      = z->img_h_max / z->img_comp[k].h;\r\n         r->vs      = z->img_v_max / z->img_comp[k].v;\r\n         r->ystep   = r->vs >> 1;\r\n         r->w_lores = (z->s.img_x + r->hs-1) / r->hs;\r\n         r->ypos    = 0;\r\n         r->line0   = r->line1 = z->img_comp[k].data;\r\n\r\n         if      (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;\r\n         else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;\r\n         else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;\r\n         else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;\r\n         else                               r->resample = resample_row_generic;\r\n      }\r\n\r\n      // can't error after this so, this is safe\r\n      output = (uint8 *) malloc(n * z->s.img_x * z->s.img_y + 1);\r\n      if (!output) { cleanup_jpeg(z); return epuc(\"outofmem\", \"Out of memory\"); }\r\n\r\n      // now go ahead and resample\r\n      for (j=0; j < z->s.img_y; ++j) {\r\n         uint8 *out = output + n * z->s.img_x * j;\r\n         for (k=0; k < decode_n; ++k) {\r\n            stbi_resample *r = &res_comp[k];\r\n            int y_bot = r->ystep >= (r->vs >> 1);\r\n            coutput[k] = r->resample(z->img_comp[k].linebuf,\r\n                                     y_bot ? r->line1 : r->line0,\r\n                                     y_bot ? r->line0 : r->line1,\r\n                                     r->w_lores, r->hs);\r\n            if (++r->ystep >= r->vs) {\r\n               r->ystep = 0;\r\n               r->line0 = r->line1;\r\n               if (++r->ypos < z->img_comp[k].y)\r\n                  r->line1 += z->img_comp[k].w2;\r\n            }\r\n         }\r\n         if (n >= 3) {\r\n            uint8 *y = coutput[0];\r\n            if (z->s.img_n == 3) {\r\n               #if STBI_SIMD\r\n               stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);\r\n               #else\r\n               YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s.img_x, n);\r\n               #endif\r\n            } else\r\n               for (i=0; i < z->s.img_x; ++i) {\r\n                  out[0] = out[1] = out[2] = y[i];\r\n                  out[3] = 255; // not used if n==3\r\n                  out += n;\r\n               }\r\n         } else {\r\n            uint8 *y = coutput[0];\r\n            if (n == 1)\r\n               for (i=0; i < z->s.img_x; ++i) out[i] = y[i];\r\n            else\r\n               for (i=0; i < z->s.img_x; ++i) *out++ = y[i], *out++ = 255;\r\n         }\r\n      }\r\n      cleanup_jpeg(z);\r\n      *out_x = z->s.img_x;\r\n      *out_y = z->s.img_y;\r\n      if (comp) *comp  = z->s.img_n; // report original components, not output\r\n      return output;\r\n   }\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nunsigned char *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   jpeg j;\r\n   start_file(&j.s, f);\r\n   return load_jpeg_image(&j, x,y,comp,req_comp);\r\n}\r\n\r\nunsigned char *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   unsigned char *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n#endif\r\n\r\nunsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   jpeg j;\r\n   start_mem(&j.s, buffer,len);\r\n   return load_jpeg_image(&j, x,y,comp,req_comp);\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint stbi_jpeg_test_file(FILE *f)\r\n{\r\n   int n,r;\r\n   jpeg j;\r\n   n = ftell(f);\r\n   start_file(&j.s, f);\r\n   r = decode_jpeg_header(&j, SCAN_type);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint stbi_jpeg_test_memory(stbi_uc const *buffer, int len)\r\n{\r\n   jpeg j;\r\n   start_mem(&j.s, buffer,len);\r\n   return decode_jpeg_header(&j, SCAN_type);\r\n}\r\n\r\n// @TODO:\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_jpeg_info            (char const *filename,           int *x, int *y, int *comp);\r\nextern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\nextern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\n\r\n// public domain zlib decode    v0.2  Sean Barrett 2006-11-18\r\n//    simple implementation\r\n//      - all input must be provided in an upfront buffer\r\n//      - all output is written to a single output buffer (can malloc/realloc)\r\n//    performance\r\n//      - fast huffman\r\n\r\n// fast-way is faster to check than jpeg huffman, but slow way is slower\r\n#define ZFAST_BITS  9 // accelerate all cases in default tables\r\n#define ZFAST_MASK  ((1 << ZFAST_BITS) - 1)\r\n\r\n// zlib-style huffman encoding\r\n// (jpegs packs from left, zlib from right, so can't share code)\r\ntypedef struct\r\n{\r\n   uint16 fast[1 << ZFAST_BITS];\r\n   uint16 firstcode[16];\r\n   int maxcode[17];\r\n   uint16 firstsymbol[16];\r\n   uint8  size[288];\r\n   uint16 value[288];\r\n} zhuffman;\r\n\r\n__forceinline static int bitreverse16(int n)\r\n{\r\n  n = ((n & 0xAAAA) >>  1) | ((n & 0x5555) << 1);\r\n  n = ((n & 0xCCCC) >>  2) | ((n & 0x3333) << 2);\r\n  n = ((n & 0xF0F0) >>  4) | ((n & 0x0F0F) << 4);\r\n  n = ((n & 0xFF00) >>  8) | ((n & 0x00FF) << 8);\r\n  return n;\r\n}\r\n\r\n__forceinline static int bit_reverse(int v, int bits)\r\n{\r\n   assert(bits <= 16);\r\n   // to bit reverse n bits, reverse 16 and shift\r\n   // e.g. 11 bits, bit reverse and shift away 5\r\n   return bitreverse16(v) >> (16-bits);\r\n}\r\n\r\nstatic int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)\r\n{\r\n   int i,k=0;\r\n   int code, next_code[16], sizes[17];\r\n\r\n   // DEFLATE spec for generating codes\r\n   memset(sizes, 0, sizeof(sizes));\r\n   memset(z->fast, 255, sizeof(z->fast));\r\n   for (i=0; i < num; ++i)\r\n      ++sizes[sizelist[i]];\r\n   sizes[0] = 0;\r\n   for (i=1; i < 16; ++i)\r\n      assert(sizes[i] <= (1 << i));\r\n   code = 0;\r\n   for (i=1; i < 16; ++i) {\r\n      next_code[i] = code;\r\n      z->firstcode[i] = (uint16) code;\r\n      z->firstsymbol[i] = (uint16) k;\r\n      code = (code + sizes[i]);\r\n      if (sizes[i])\r\n         if (code-1 >= (1 << i)) return e(\"bad codelengths\",\"Corrupt JPEG\");\r\n      z->maxcode[i] = code << (16-i); // preshift for inner loop\r\n      code <<= 1;\r\n      k += sizes[i];\r\n   }\r\n   z->maxcode[16] = 0x10000; // sentinel\r\n   for (i=0; i < num; ++i) {\r\n      int s = sizelist[i];\r\n      if (s) {\r\n         int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];\r\n         z->size[c] = (uint8)s;\r\n         z->value[c] = (uint16)i;\r\n         if (s <= ZFAST_BITS) {\r\n            int k = bit_reverse(next_code[s],s);\r\n            while (k < (1 << ZFAST_BITS)) {\r\n               z->fast[k] = (uint16) c;\r\n               k += (1 << s);\r\n            }\r\n         }\r\n         ++next_code[s];\r\n      }\r\n   }\r\n   return 1;\r\n}\r\n\r\n// zlib-from-memory implementation for PNG reading\r\n//    because PNG allows splitting the zlib stream arbitrarily,\r\n//    and it's annoying structurally to have PNG call ZLIB call PNG,\r\n//    we require PNG read all the IDATs and combine them into a single\r\n//    memory buffer\r\n\r\ntypedef struct\r\n{\r\n   uint8 *zbuffer, *zbuffer_end;\r\n   int num_bits;\r\n   uint32 code_buffer;\r\n\r\n   char *zout;\r\n   char *zout_start;\r\n   char *zout_end;\r\n   int   z_expandable;\r\n\r\n   zhuffman z_length, z_distance;\r\n} zbuf;\r\n\r\n__forceinline static int zget8(zbuf *z)\r\n{\r\n   if (z->zbuffer >= z->zbuffer_end) return 0;\r\n   return *z->zbuffer++;\r\n}\r\n\r\nstatic void fill_bits(zbuf *z)\r\n{\r\n   do {\r\n      assert(z->code_buffer < (1U << z->num_bits));\r\n      z->code_buffer |= zget8(z) << z->num_bits;\r\n      z->num_bits += 8;\r\n   } while (z->num_bits <= 24);\r\n}\r\n\r\n__forceinline static unsigned int zreceive(zbuf *z, int n)\r\n{\r\n   unsigned int k;\r\n   if (z->num_bits < n) fill_bits(z);\r\n   k = z->code_buffer & ((1 << n) - 1);\r\n   z->code_buffer >>= n;\r\n   z->num_bits -= n;\r\n   return k;\r\n}\r\n\r\n__forceinline static int zhuffman_decode(zbuf *a, zhuffman *z)\r\n{\r\n   int b,s,k;\r\n   if (a->num_bits < 16) fill_bits(a);\r\n   b = z->fast[a->code_buffer & ZFAST_MASK];\r\n   if (b < 0xffff) {\r\n      s = z->size[b];\r\n      a->code_buffer >>= s;\r\n      a->num_bits -= s;\r\n      return z->value[b];\r\n   }\r\n\r\n   // not resolved by fast table, so compute it the slow way\r\n   // use jpeg approach, which requires MSbits at top\r\n   k = bit_reverse(a->code_buffer, 16);\r\n   for (s=ZFAST_BITS+1; ; ++s)\r\n      if (k < z->maxcode[s])\r\n         break;\r\n   if (s == 16) return -1; // invalid code!\r\n   // code size is s, so:\r\n   b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];\r\n   assert(z->size[b] == s);\r\n   a->code_buffer >>= s;\r\n   a->num_bits -= s;\r\n   return z->value[b];\r\n}\r\n\r\nstatic int expand(zbuf *z, int n)  // need to make room for n bytes\r\n{\r\n   char *q;\r\n   int cur, limit;\r\n   if (!z->z_expandable) return e(\"output buffer limit\",\"Corrupt PNG\");\r\n   cur   = (int) (z->zout     - z->zout_start);\r\n   limit = (int) (z->zout_end - z->zout_start);\r\n   while (cur + n > limit)\r\n      limit *= 2;\r\n   q = (char *) realloc(z->zout_start, limit);\r\n   if (q == NULL) return e(\"outofmem\", \"Out of memory\");\r\n   z->zout_start = q;\r\n   z->zout       = q + cur;\r\n   z->zout_end   = q + limit;\r\n   return 1;\r\n}\r\n\r\nstatic int length_base[31] = {\r\n   3,4,5,6,7,8,9,10,11,13,\r\n   15,17,19,23,27,31,35,43,51,59,\r\n   67,83,99,115,131,163,195,227,258,0,0 };\r\n\r\nstatic int length_extra[31]=\r\n{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };\r\n\r\nstatic int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,\r\n257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};\r\n\r\nstatic int dist_extra[32] =\r\n{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};\r\n\r\nstatic int parse_huffman_block(zbuf *a)\r\n{\r\n   for(;;) {\r\n      int z = zhuffman_decode(a, &a->z_length);\r\n      if (z < 256) {\r\n         if (z < 0) return e(\"bad huffman code\",\"Corrupt PNG\"); // error in huffman codes\r\n         if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;\r\n         *a->zout++ = (char) z;\r\n      } else {\r\n         uint8 *p;\r\n         int len,dist;\r\n         if (z == 256) return 1;\r\n         z -= 257;\r\n         len = length_base[z];\r\n         if (length_extra[z]) len += zreceive(a, length_extra[z]);\r\n         z = zhuffman_decode(a, &a->z_distance);\r\n         if (z < 0) return e(\"bad huffman code\",\"Corrupt PNG\");\r\n         dist = dist_base[z];\r\n         if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);\r\n         if (a->zout - a->zout_start < dist) return e(\"bad dist\",\"Corrupt PNG\");\r\n         if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;\r\n         p = (uint8 *) (a->zout - dist);\r\n         while (len--)\r\n            *a->zout++ = *p++;\r\n      }\r\n   }\r\n}\r\n\r\nstatic int compute_huffman_codes(zbuf *a)\r\n{\r\n   static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };\r\n   static zhuffman z_codelength; // static just to save stack space\r\n   uint8 lencodes[286+32+137];//padding for maximum single op\r\n   uint8 codelength_sizes[19];\r\n   int i,n;\r\n\r\n   int hlit  = zreceive(a,5) + 257;\r\n   int hdist = zreceive(a,5) + 1;\r\n   int hclen = zreceive(a,4) + 4;\r\n\r\n   memset(codelength_sizes, 0, sizeof(codelength_sizes));\r\n   for (i=0; i < hclen; ++i) {\r\n      int s = zreceive(a,3);\r\n      codelength_sizes[length_dezigzag[i]] = (uint8) s;\r\n   }\r\n   if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;\r\n\r\n   n = 0;\r\n   while (n < hlit + hdist) {\r\n      int c = zhuffman_decode(a, &z_codelength);\r\n      assert(c >= 0 && c < 19);\r\n      if (c < 16)\r\n         lencodes[n++] = (uint8) c;\r\n      else if (c == 16) {\r\n         c = zreceive(a,2)+3;\r\n         memset(lencodes+n, lencodes[n-1], c);\r\n         n += c;\r\n      } else if (c == 17) {\r\n         c = zreceive(a,3)+3;\r\n         memset(lencodes+n, 0, c);\r\n         n += c;\r\n      } else {\r\n         assert(c == 18);\r\n         c = zreceive(a,7)+11;\r\n         memset(lencodes+n, 0, c);\r\n         n += c;\r\n      }\r\n   }\r\n   if (n != hlit+hdist) return e(\"bad codelengths\",\"Corrupt PNG\");\r\n   if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;\r\n   if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;\r\n   return 1;\r\n}\r\n\r\nstatic int parse_uncompressed_block(zbuf *a)\r\n{\r\n   uint8 header[4];\r\n   int len,nlen,k;\r\n   if (a->num_bits & 7)\r\n      zreceive(a, a->num_bits & 7); // discard\r\n   // drain the bit-packed data into header\r\n   k = 0;\r\n   while (a->num_bits > 0) {\r\n      header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?\r\n      a->code_buffer >>= 8;\r\n      a->num_bits -= 8;\r\n   }\r\n   assert(a->num_bits == 0);\r\n   // now fill header the normal way\r\n   while (k < 4)\r\n      header[k++] = (uint8) zget8(a);\r\n   len  = header[1] * 256 + header[0];\r\n   nlen = header[3] * 256 + header[2];\r\n   if (nlen != (len ^ 0xffff)) return e(\"zlib corrupt\",\"Corrupt PNG\");\r\n   if (a->zbuffer + len > a->zbuffer_end) return e(\"read past buffer\",\"Corrupt PNG\");\r\n   if (a->zout + len > a->zout_end)\r\n      if (!expand(a, len)) return 0;\r\n   memcpy(a->zout, a->zbuffer, len);\r\n   a->zbuffer += len;\r\n   a->zout += len;\r\n   return 1;\r\n}\r\n\r\nstatic int parse_zlib_header(zbuf *a)\r\n{\r\n   int cmf   = zget8(a);\r\n   int cm    = cmf & 15;\r\n   /* int cinfo = cmf >> 4; */\r\n   int flg   = zget8(a);\r\n   if ((cmf*256+flg) % 31 != 0) return e(\"bad zlib header\",\"Corrupt PNG\"); // zlib spec\r\n   if (flg & 32) return e(\"no preset dict\",\"Corrupt PNG\"); // preset dictionary not allowed in png\r\n   if (cm != 8) return e(\"bad compression\",\"Corrupt PNG\"); // DEFLATE required for png\r\n   // window = 1 << (8 + cinfo)... but who cares, we fully buffer output\r\n   return 1;\r\n}\r\n\r\n// @TODO: should statically initialize these for optimal thread safety\r\nstatic uint8 default_length[288], default_distance[32];\r\nstatic void init_defaults(void)\r\n{\r\n   int i;   // use <= to match clearly with spec\r\n   for (i=0; i <= 143; ++i)     default_length[i]   = 8;\r\n   for (   ; i <= 255; ++i)     default_length[i]   = 9;\r\n   for (   ; i <= 279; ++i)     default_length[i]   = 7;\r\n   for (   ; i <= 287; ++i)     default_length[i]   = 8;\r\n\r\n   for (i=0; i <=  31; ++i)     default_distance[i] = 5;\r\n}\r\n\r\nstatic int parse_zlib(zbuf *a, int parse_header)\r\n{\r\n   int final, type;\r\n   if (parse_header)\r\n      if (!parse_zlib_header(a)) return 0;\r\n   a->num_bits = 0;\r\n   a->code_buffer = 0;\r\n   do {\r\n      final = zreceive(a,1);\r\n      type = zreceive(a,2);\r\n      if (type == 0) {\r\n         if (!parse_uncompressed_block(a)) return 0;\r\n      } else if (type == 3) {\r\n         return 0;\r\n      } else {\r\n         if (type == 1) {\r\n            // use fixed code lengths\r\n            if (!default_distance[31]) init_defaults();\r\n            if (!zbuild_huffman(&a->z_length  , default_length  , 288)) return 0;\r\n            if (!zbuild_huffman(&a->z_distance, default_distance,  32)) return 0;\r\n         } else {\r\n            if (!compute_huffman_codes(a)) return 0;\r\n         }\r\n         if (!parse_huffman_block(a)) return 0;\r\n      }\r\n   } while (!final);\r\n   return 1;\r\n}\r\n\r\nstatic int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)\r\n{\r\n   a->zout_start = obuf;\r\n   a->zout       = obuf;\r\n   a->zout_end   = obuf + olen;\r\n   a->z_expandable = exp;\r\n\r\n   return parse_zlib(a, parse_header);\r\n}\r\n\r\nchar *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)\r\n{\r\n   zbuf a;\r\n   char *p = (char *) malloc(initial_size);\r\n   if (p == NULL) return NULL;\r\n   a.zbuffer = (uint8 *) buffer;\r\n   a.zbuffer_end = (uint8 *) buffer + len;\r\n   if (do_zlib(&a, p, initial_size, 1, 1)) {\r\n      if (outlen) *outlen = (int) (a.zout - a.zout_start);\r\n      return a.zout_start;\r\n   } else {\r\n      free(a.zout_start);\r\n      return NULL;\r\n   }\r\n}\r\n\r\nchar *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)\r\n{\r\n   return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);\r\n}\r\n\r\nint stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)\r\n{\r\n   zbuf a;\r\n   a.zbuffer = (uint8 *) ibuffer;\r\n   a.zbuffer_end = (uint8 *) ibuffer + ilen;\r\n   if (do_zlib(&a, obuffer, olen, 0, 1))\r\n      return (int) (a.zout - a.zout_start);\r\n   else\r\n      return -1;\r\n}\r\n\r\nchar *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)\r\n{\r\n   zbuf a;\r\n   char *p = (char *) malloc(16384);\r\n   if (p == NULL) return NULL;\r\n   a.zbuffer = (uint8 *) buffer;\r\n   a.zbuffer_end = (uint8 *) buffer+len;\r\n   if (do_zlib(&a, p, 16384, 1, 0)) {\r\n      if (outlen) *outlen = (int) (a.zout - a.zout_start);\r\n      return a.zout_start;\r\n   } else {\r\n      free(a.zout_start);\r\n      return NULL;\r\n   }\r\n}\r\n\r\nint stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)\r\n{\r\n   zbuf a;\r\n   a.zbuffer = (uint8 *) ibuffer;\r\n   a.zbuffer_end = (uint8 *) ibuffer + ilen;\r\n   if (do_zlib(&a, obuffer, olen, 0, 0))\r\n      return (int) (a.zout - a.zout_start);\r\n   else\r\n      return -1;\r\n}\r\n\r\n// public domain \"baseline\" PNG decoder   v0.10  Sean Barrett 2006-11-18\r\n//    simple implementation\r\n//      - only 8-bit samples\r\n//      - no CRC checking\r\n//      - allocates lots of intermediate memory\r\n//        - avoids problem of streaming data between subsystems\r\n//        - avoids explicit window management\r\n//    performance\r\n//      - uses stb_zlib, a PD zlib implementation with fast huffman decoding\r\n\r\n\r\ntypedef struct\r\n{\r\n   uint32 length;\r\n   uint32 type;\r\n} chunk;\r\n\r\n#define PNG_TYPE(a,b,c,d)  (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))\r\n\r\nstatic chunk get_chunk_header(stbi *s)\r\n{\r\n   chunk c;\r\n   c.length = get32(s);\r\n   c.type   = get32(s);\r\n   return c;\r\n}\r\n\r\nstatic int check_png_header(stbi *s)\r\n{\r\n   static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };\r\n   int i;\r\n   for (i=0; i < 8; ++i)\r\n      if (get8(s) != png_sig[i]) return e(\"bad png sig\",\"Not a PNG\");\r\n   return 1;\r\n}\r\n\r\ntypedef struct\r\n{\r\n   stbi s;\r\n   uint8 *idata, *expanded, *out;\r\n} png;\r\n\r\n\r\nenum {\r\n   F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,\r\n   F_avg_first, F_paeth_first,\r\n};\r\n\r\nstatic uint8 first_row_filter[5] =\r\n{\r\n   F_none, F_sub, F_none, F_avg_first, F_paeth_first\r\n};\r\n\r\nstatic int paeth(int a, int b, int c)\r\n{\r\n   int p = a + b - c;\r\n   int pa = abs(p-a);\r\n   int pb = abs(p-b);\r\n   int pc = abs(p-c);\r\n   if (pa <= pb && pa <= pc) return a;\r\n   if (pb <= pc) return b;\r\n   return c;\r\n}\r\n\r\n// create the png data from post-deflated data\r\nstatic int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n)\r\n{\r\n   stbi *s = &a->s;\r\n   uint32 i,j,stride = s->img_x*out_n;\r\n   int k;\r\n   int img_n = s->img_n; // copy it into a local for later\r\n   assert(out_n == s->img_n || out_n == s->img_n+1);\r\n   a->out = (uint8 *) malloc(s->img_x * s->img_y * out_n);\r\n   if (!a->out) return e(\"outofmem\", \"Out of memory\");\r\n   if (raw_len != (img_n * s->img_x + 1) * s->img_y) return e(\"not enough pixels\",\"Corrupt PNG\");\r\n   for (j=0; j < s->img_y; ++j) {\r\n      uint8 *cur = a->out + stride*j;\r\n      uint8 *prior = cur - stride;\r\n      int filter = *raw++;\r\n      if (filter > 4) return e(\"invalid filter\",\"Corrupt PNG\");\r\n      // if first row, use special filter that doesn't sample previous row\r\n      if (j == 0) filter = first_row_filter[filter];\r\n      // handle first pixel explicitly\r\n      for (k=0; k < img_n; ++k) {\r\n         switch(filter) {\r\n            case F_none       : cur[k] = raw[k]; break;\r\n            case F_sub        : cur[k] = raw[k]; break;\r\n            case F_up         : cur[k] = raw[k] + prior[k]; break;\r\n            case F_avg        : cur[k] = raw[k] + (prior[k]>>1); break;\r\n            case F_paeth      : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;\r\n            case F_avg_first  : cur[k] = raw[k]; break;\r\n            case F_paeth_first: cur[k] = raw[k]; break;\r\n         }\r\n      }\r\n      if (img_n != out_n) cur[img_n] = 255;\r\n      raw += img_n;\r\n      cur += out_n;\r\n      prior += out_n;\r\n      // this is a little gross, so that we don't switch per-pixel or per-component\r\n      if (img_n == out_n) {\r\n         #define CASE(f) \\\r\n             case f:     \\\r\n                for (i=s->img_x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \\\r\n                   for (k=0; k < img_n; ++k)\r\n         switch(filter) {\r\n            CASE(F_none)  cur[k] = raw[k]; break;\r\n            CASE(F_sub)   cur[k] = raw[k] + cur[k-img_n]; break;\r\n            CASE(F_up)    cur[k] = raw[k] + prior[k]; break;\r\n            CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;\r\n            CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;\r\n            CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-img_n] >> 1); break;\r\n            CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;\r\n         }\r\n         #undef CASE\r\n      } else {\r\n         assert(img_n+1 == out_n);\r\n         #define CASE(f) \\\r\n             case f:     \\\r\n                for (i=s->img_x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \\\r\n                   for (k=0; k < img_n; ++k)\r\n         switch(filter) {\r\n            CASE(F_none)  cur[k] = raw[k]; break;\r\n            CASE(F_sub)   cur[k] = raw[k] + cur[k-out_n]; break;\r\n            CASE(F_up)    cur[k] = raw[k] + prior[k]; break;\r\n            CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;\r\n            CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;\r\n            CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-out_n] >> 1); break;\r\n            CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;\r\n         }\r\n         #undef CASE\r\n      }\r\n   }\r\n   return 1;\r\n}\r\n\r\nstatic int compute_transparency(png *z, uint8 tc[3], int out_n)\r\n{\r\n   stbi *s = &z->s;\r\n   uint32 i, pixel_count = s->img_x * s->img_y;\r\n   uint8 *p = z->out;\r\n\r\n   // compute color-based transparency, assuming we've\r\n   // already got 255 as the alpha value in the output\r\n   assert(out_n == 2 || out_n == 4);\r\n\r\n   if (out_n == 2) {\r\n      for (i=0; i < pixel_count; ++i) {\r\n         p[1] = (p[0] == tc[0] ? 0 : 255);\r\n         p += 2;\r\n      }\r\n   } else {\r\n      for (i=0; i < pixel_count; ++i) {\r\n         if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])\r\n            p[3] = 0;\r\n         p += 4;\r\n      }\r\n   }\r\n   return 1;\r\n}\r\n\r\nstatic int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)\r\n{\r\n   uint32 i, pixel_count = a->s.img_x * a->s.img_y;\r\n   uint8 *p, *temp_out, *orig = a->out;\r\n\r\n   p = (uint8 *) malloc(pixel_count * pal_img_n);\r\n   if (p == NULL) return e(\"outofmem\", \"Out of memory\");\r\n\r\n   // between here and free(out) below, exitting would leak\r\n   temp_out = p;\r\n\r\n   if (pal_img_n == 3) {\r\n      for (i=0; i < pixel_count; ++i) {\r\n         int n = orig[i]*4;\r\n         p[0] = palette[n  ];\r\n         p[1] = palette[n+1];\r\n         p[2] = palette[n+2];\r\n         p += 3;\r\n      }\r\n   } else {\r\n      for (i=0; i < pixel_count; ++i) {\r\n         int n = orig[i]*4;\r\n         p[0] = palette[n  ];\r\n         p[1] = palette[n+1];\r\n         p[2] = palette[n+2];\r\n         p[3] = palette[n+3];\r\n         p += 4;\r\n      }\r\n   }\r\n   free(a->out);\r\n   a->out = temp_out;\r\n   return 1;\r\n}\r\n\r\nstatic int parse_png_file(png *z, int scan, int req_comp)\r\n{\r\n   uint8 palette[1024], pal_img_n=0;\r\n   uint8 has_trans=0, tc[3];\r\n   uint32 ioff=0, idata_limit=0, i, pal_len=0;\r\n   int first=1,k;\r\n   stbi *s = &z->s;\r\n\r\n   if (!check_png_header(s)) return 0;\r\n\r\n   if (scan == SCAN_type) return 1;\r\n\r\n   for(;;first=0) {\r\n      chunk c = get_chunk_header(s);\r\n      if (first && c.type != PNG_TYPE('I','H','D','R'))\r\n         return e(\"first not IHDR\",\"Corrupt PNG\");\r\n      switch (c.type) {\r\n         case PNG_TYPE('I','H','D','R'): {\r\n            int depth,color,interlace,comp,filter;\r\n            if (!first) return e(\"multiple IHDR\",\"Corrupt PNG\");\r\n            if (c.length != 13) return e(\"bad IHDR len\",\"Corrupt PNG\");\r\n            s->img_x = get32(s); if (s->img_x > (1 << 24)) return e(\"too large\",\"Very large image (corrupt?)\");\r\n            s->img_y = get32(s); if (s->img_y > (1 << 24)) return e(\"too large\",\"Very large image (corrupt?)\");\r\n            depth = get8(s);  if (depth != 8)        return e(\"8bit only\",\"PNG not supported: 8-bit only\");\r\n            color = get8(s);  if (color > 6)         return e(\"bad ctype\",\"Corrupt PNG\");\r\n            if (color == 3) pal_img_n = 3; else if (color & 1) return e(\"bad ctype\",\"Corrupt PNG\");\r\n            comp  = get8(s);  if (comp) return e(\"bad comp method\",\"Corrupt PNG\");\r\n            filter= get8(s);  if (filter) return e(\"bad filter method\",\"Corrupt PNG\");\r\n            interlace = get8(s); if (interlace) return e(\"interlaced\",\"PNG not supported: interlaced mode\");\r\n            if (!s->img_x || !s->img_y) return e(\"0-pixel image\",\"Corrupt PNG\");\r\n            if (!pal_img_n) {\r\n               s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);\r\n               if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e(\"too large\", \"Image too large to decode\");\r\n               if (scan == SCAN_header) return 1;\r\n            } else {\r\n               // if paletted, then pal_n is our final components, and\r\n               // img_n is # components to decompress/filter.\r\n               s->img_n = 1;\r\n               if ((1 << 30) / s->img_x / 4 < s->img_y) return e(\"too large\",\"Corrupt PNG\");\r\n               // if SCAN_header, have to scan to see if we have a tRNS\r\n            }\r\n            break;\r\n         }\r\n\r\n         case PNG_TYPE('P','L','T','E'):  {\r\n            if (c.length > 256*3) return e(\"invalid PLTE\",\"Corrupt PNG\");\r\n            pal_len = c.length / 3;\r\n            if (pal_len * 3 != c.length) return e(\"invalid PLTE\",\"Corrupt PNG\");\r\n            for (i=0; i < pal_len; ++i) {\r\n               palette[i*4+0] = get8u(s);\r\n               palette[i*4+1] = get8u(s);\r\n               palette[i*4+2] = get8u(s);\r\n               palette[i*4+3] = 255;\r\n            }\r\n            break;\r\n         }\r\n\r\n         case PNG_TYPE('t','R','N','S'): {\r\n            if (z->idata) return e(\"tRNS after IDAT\",\"Corrupt PNG\");\r\n            if (pal_img_n) {\r\n               if (scan == SCAN_header) { s->img_n = 4; return 1; }\r\n               if (pal_len == 0) return e(\"tRNS before PLTE\",\"Corrupt PNG\");\r\n               if (c.length > pal_len) return e(\"bad tRNS len\",\"Corrupt PNG\");\r\n               pal_img_n = 4;\r\n               for (i=0; i < c.length; ++i)\r\n                  palette[i*4+3] = get8u(s);\r\n            } else {\r\n               if (!(s->img_n & 1)) return e(\"tRNS with alpha\",\"Corrupt PNG\");\r\n               if (c.length != (uint32) s->img_n*2) return e(\"bad tRNS len\",\"Corrupt PNG\");\r\n               has_trans = 1;\r\n               for (k=0; k < s->img_n; ++k)\r\n                  tc[k] = (uint8) get16(s); // non 8-bit images will be larger\r\n            }\r\n            break;\r\n         }\r\n\r\n         case PNG_TYPE('I','D','A','T'): {\r\n            if (pal_img_n && !pal_len) return e(\"no PLTE\",\"Corrupt PNG\");\r\n            if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }\r\n            if (ioff + c.length > idata_limit) {\r\n               uint8 *p;\r\n               if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;\r\n               while (ioff + c.length > idata_limit)\r\n                  idata_limit *= 2;\r\n               p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e(\"outofmem\", \"Out of memory\");\r\n               z->idata = p;\r\n            }\r\n            #ifndef STBI_NO_STDIO\r\n            if (s->img_file)\r\n            {\r\n               if (fread(z->idata+ioff,1,c.length,s->img_file) != c.length) return e(\"outofdata\",\"Corrupt PNG\");\r\n            }\r\n            else\r\n            #endif\r\n            {\r\n               memcpy(z->idata+ioff, s->img_buffer, c.length);\r\n               s->img_buffer += c.length;\r\n            }\r\n            ioff += c.length;\r\n            break;\r\n         }\r\n\r\n         case PNG_TYPE('I','E','N','D'): {\r\n            uint32 raw_len;\r\n            if (scan != SCAN_load) return 1;\r\n            if (z->idata == NULL) return e(\"no IDAT\",\"Corrupt PNG\");\r\n            z->expanded = (uint8 *) stbi_zlib_decode_malloc((char *) z->idata, ioff, (int *) &raw_len);\r\n            if (z->expanded == NULL) return 0; // zlib should set error\r\n            free(z->idata); z->idata = NULL;\r\n            if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)\r\n               s->img_out_n = s->img_n+1;\r\n            else\r\n               s->img_out_n = s->img_n;\r\n            if (!create_png_image(z, z->expanded, raw_len, s->img_out_n)) return 0;\r\n            if (has_trans)\r\n               if (!compute_transparency(z, tc, s->img_out_n)) return 0;\r\n            if (pal_img_n) {\r\n               // pal_img_n == 3 or 4\r\n               s->img_n = pal_img_n; // record the actual colors we had\r\n               s->img_out_n = pal_img_n;\r\n               if (req_comp >= 3) s->img_out_n = req_comp;\r\n               if (!expand_palette(z, palette, pal_len, s->img_out_n))\r\n                  return 0;\r\n            }\r\n            free(z->expanded); z->expanded = NULL;\r\n            return 1;\r\n         }\r\n\r\n         default:\r\n            // if critical, fail\r\n            if ((c.type & (1 << 29)) == 0) {\r\n               #ifndef STBI_NO_FAILURE_STRINGS\r\n               // not threadsafe\r\n               static char invalid_chunk[] = \"XXXX chunk not known\";\r\n               invalid_chunk[0] = (uint8) (c.type >> 24);\r\n               invalid_chunk[1] = (uint8) (c.type >> 16);\r\n               invalid_chunk[2] = (uint8) (c.type >>  8);\r\n               invalid_chunk[3] = (uint8) (c.type >>  0);\r\n               #endif\r\n               return e(invalid_chunk, \"PNG not supported: unknown chunk type\");\r\n            }\r\n            skip(s, c.length);\r\n            break;\r\n      }\r\n      // end of chunk, read and skip CRC\r\n      get32(s);\r\n   }\r\n}\r\n\r\nstatic unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)\r\n{\r\n   unsigned char *result=NULL;\r\n   p->expanded = NULL;\r\n   p->idata = NULL;\r\n   p->out = NULL;\r\n   if (req_comp < 0 || req_comp > 4) return epuc(\"bad req_comp\", \"Internal error\");\r\n   if (parse_png_file(p, SCAN_load, req_comp)) {\r\n      result = p->out;\r\n      p->out = NULL;\r\n      if (req_comp && req_comp != p->s.img_out_n) {\r\n         result = convert_format(result, p->s.img_out_n, req_comp, p->s.img_x, p->s.img_y);\r\n         p->s.img_out_n = req_comp;\r\n         if (result == NULL) return result;\r\n      }\r\n      *x = p->s.img_x;\r\n      *y = p->s.img_y;\r\n      if (n) *n = p->s.img_n;\r\n   }\r\n   free(p->out);      p->out      = NULL;\r\n   free(p->expanded); p->expanded = NULL;\r\n   free(p->idata);    p->idata    = NULL;\r\n\r\n   return result;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nunsigned char *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   png p;\r\n   start_file(&p.s, f);\r\n   return do_png(&p, x,y,comp,req_comp);\r\n}\r\n\r\nunsigned char *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   unsigned char *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_png_load_from_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n#endif\r\n\r\nunsigned char *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   png p;\r\n   start_mem(&p.s, buffer,len);\r\n   return do_png(&p, x,y,comp,req_comp);\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint stbi_png_test_file(FILE *f)\r\n{\r\n   png p;\r\n   int n,r;\r\n   n = ftell(f);\r\n   start_file(&p.s, f);\r\n   r = parse_png_file(&p, SCAN_type,STBI_default);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint stbi_png_test_memory(stbi_uc const *buffer, int len)\r\n{\r\n   png p;\r\n   start_mem(&p.s, buffer, len);\r\n   return parse_png_file(&p, SCAN_type,STBI_default);\r\n}\r\n\r\n// TODO: load header from png\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_png_info             (char const *filename,           int *x, int *y, int *comp);\r\nextern int      stbi_png_info_from_file   (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\nextern int      stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\n\r\n// Microsoft/Windows BMP image\r\n\r\nstatic int bmp_test(stbi *s)\r\n{\r\n   int sz;\r\n   if (get8(s) != 'B') return 0;\r\n   if (get8(s) != 'M') return 0;\r\n   get32le(s); // discard filesize\r\n   get16le(s); // discard reserved\r\n   get16le(s); // discard reserved\r\n   get32le(s); // discard data offset\r\n   sz = get32le(s);\r\n   if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;\r\n   return 0;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint      stbi_bmp_test_file        (FILE *f)\r\n{\r\n   stbi s;\r\n   int r,n = ftell(f);\r\n   start_file(&s,f);\r\n   r = bmp_test(&s);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint      stbi_bmp_test_memory      (stbi_uc const *buffer, int len)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return bmp_test(&s);\r\n}\r\n\r\n// returns 0..31 for the highest set bit\r\nstatic int high_bit(unsigned int z)\r\n{\r\n   int n=0;\r\n   if (z == 0) return -1;\r\n   if (z >= 0x10000) n += 16, z >>= 16;\r\n   if (z >= 0x00100) n +=  8, z >>=  8;\r\n   if (z >= 0x00010) n +=  4, z >>=  4;\r\n   if (z >= 0x00004) n +=  2, z >>=  2;\r\n   if (z >= 0x00002) n +=  1, z >>=  1;\r\n   return n;\r\n}\r\n\r\nstatic int bitcount(unsigned int a)\r\n{\r\n   a = (a & 0x55555555) + ((a >>  1) & 0x55555555); // max 2\r\n   a = (a & 0x33333333) + ((a >>  2) & 0x33333333); // max 4\r\n   a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits\r\n   a = (a + (a >> 8)); // max 16 per 8 bits\r\n   a = (a + (a >> 16)); // max 32 per 8 bits\r\n   return a & 0xff;\r\n}\r\n\r\nstatic int shiftsigned(int v, int shift, int bits)\r\n{\r\n   int result;\r\n   int z=0;\r\n\r\n   if (shift < 0) v <<= -shift;\r\n   else v >>= shift;\r\n   result = v;\r\n\r\n   z = bits;\r\n   while (z < 8) {\r\n      result += v >> z;\r\n      z += bits;\r\n   }\r\n   return result;\r\n}\r\n\r\nstatic stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   uint8 *out;\r\n   unsigned int mr=0,mg=0,mb=0,ma=0;\r\n   stbi_uc pal[256][4];\r\n   int psize=0,i,j,compress=0,width;\r\n   int bpp, flip_vertically, pad, target, offset, hsz;\r\n   if (get8(s) != 'B' || get8(s) != 'M') return epuc(\"not BMP\", \"Corrupt BMP\");\r\n   get32le(s); // discard filesize\r\n   get16le(s); // discard reserved\r\n   get16le(s); // discard reserved\r\n   offset = get32le(s);\r\n   hsz = get32le(s);\r\n   if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc(\"unknown BMP\", \"BMP type not supported: unknown\");\r\n   failure_reason = \"bad BMP\";\r\n   if (hsz == 12) {\r\n      s->img_x = get16le(s);\r\n      s->img_y = get16le(s);\r\n   } else {\r\n      s->img_x = get32le(s);\r\n      s->img_y = get32le(s);\r\n   }\r\n   if (get16le(s) != 1) return 0;\r\n   bpp = get16le(s);\r\n   if (bpp == 1) return epuc(\"monochrome\", \"BMP type not supported: 1-bit\");\r\n   flip_vertically = ((int) s->img_y) > 0;\r\n   s->img_y = abs((int) s->img_y);\r\n   if (hsz == 12) {\r\n      if (bpp < 24)\r\n         psize = (offset - 14 - 24) / 3;\r\n   } else {\r\n      compress = get32le(s);\r\n      if (compress == 1 || compress == 2) return epuc(\"BMP RLE\", \"BMP type not supported: RLE\");\r\n      get32le(s); // discard sizeof\r\n      get32le(s); // discard hres\r\n      get32le(s); // discard vres\r\n      get32le(s); // discard colorsused\r\n      get32le(s); // discard max important\r\n      if (hsz == 40 || hsz == 56) {\r\n         if (hsz == 56) {\r\n            get32le(s);\r\n            get32le(s);\r\n            get32le(s);\r\n            get32le(s);\r\n         }\r\n         if (bpp == 16 || bpp == 32) {\r\n            mr = mg = mb = 0;\r\n            if (compress == 0) {\r\n               if (bpp == 32) {\r\n                  mr = 0xff << 16;\r\n                  mg = 0xff <<  8;\r\n                  mb = 0xff <<  0;\r\n               } else {\r\n                  mr = 31 << 10;\r\n                  mg = 31 <<  5;\r\n                  mb = 31 <<  0;\r\n               }\r\n            } else if (compress == 3) {\r\n               mr = get32le(s);\r\n               mg = get32le(s);\r\n               mb = get32le(s);\r\n               // not documented, but generated by photoshop and handled by mspaint\r\n               if (mr == mg && mg == mb) {\r\n                  // ?!?!?\r\n                  return NULL;\r\n               }\r\n            } else\r\n               return NULL;\r\n         }\r\n      } else {\r\n         assert(hsz == 108);\r\n         mr = get32le(s);\r\n         mg = get32le(s);\r\n         mb = get32le(s);\r\n         ma = get32le(s);\r\n         get32le(s); // discard color space\r\n         for (i=0; i < 12; ++i)\r\n            get32le(s); // discard color space parameters\r\n      }\r\n      if (bpp < 16)\r\n         psize = (offset - 14 - hsz) >> 2;\r\n   }\r\n   s->img_n = ma ? 4 : 3;\r\n   if (req_comp && req_comp >= 3) // we can directly decode 3 or 4\r\n      target = req_comp;\r\n   else\r\n      target = s->img_n; // if they want monochrome, we'll post-convert\r\n   out = (stbi_uc *) malloc(target * s->img_x * s->img_y);\r\n   if (!out) return epuc(\"outofmem\", \"Out of memory\");\r\n   if (bpp < 16) {\r\n      int z=0;\r\n      if (psize == 0 || psize > 256) { free(out); return epuc(\"invalid\", \"Corrupt BMP\"); }\r\n      for (i=0; i < psize; ++i) {\r\n         pal[i][2] = get8(s);\r\n         pal[i][1] = get8(s);\r\n         pal[i][0] = get8(s);\r\n         if (hsz != 12) get8(s);\r\n         pal[i][3] = 255;\r\n      }\r\n      skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));\r\n      if (bpp == 4) width = (s->img_x + 1) >> 1;\r\n      else if (bpp == 8) width = s->img_x;\r\n      else { free(out); return epuc(\"bad bpp\", \"Corrupt BMP\"); }\r\n      pad = (-width)&3;\r\n      for (j=0; j < (int) s->img_y; ++j) {\r\n         for (i=0; i < (int) s->img_x; i += 2) {\r\n            int v=get8(s),v2=0;\r\n            if (bpp == 4) {\r\n               v2 = v & 15;\r\n               v >>= 4;\r\n            }\r\n            out[z++] = pal[v][0];\r\n            out[z++] = pal[v][1];\r\n            out[z++] = pal[v][2];\r\n            if (target == 4) out[z++] = 255;\r\n            if (i+1 == (int) s->img_x) break;\r\n            v = (bpp == 8) ? get8(s) : v2;\r\n            out[z++] = pal[v][0];\r\n            out[z++] = pal[v][1];\r\n            out[z++] = pal[v][2];\r\n            if (target == 4) out[z++] = 255;\r\n         }\r\n         skip(s, pad);\r\n      }\r\n   } else {\r\n      int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;\r\n      int z = 0;\r\n      int easy=0;\r\n      skip(s, offset - 14 - hsz);\r\n      if (bpp == 24) width = 3 * s->img_x;\r\n      else if (bpp == 16) width = 2*s->img_x;\r\n      else /* bpp = 32 and pad = 0 */ width=0;\r\n      pad = (-width) & 3;\r\n      if (bpp == 24) {\r\n         easy = 1;\r\n      } else if (bpp == 32) {\r\n         if (mb == 0xff && mg == 0xff00 && mr == 0xff000000 && ma == 0xff000000)\r\n            easy = 2;\r\n      }\r\n      if (!easy) {\r\n         if (!mr || !mg || !mb) return epuc(\"bad masks\", \"Corrupt BMP\");\r\n         // right shift amt to put high bit in position #7\r\n         rshift = high_bit(mr)-7; rcount = bitcount(mr);\r\n         gshift = high_bit(mg)-7; gcount = bitcount(mr);\r\n         bshift = high_bit(mb)-7; bcount = bitcount(mr);\r\n         ashift = high_bit(ma)-7; acount = bitcount(mr);\r\n      }\r\n      for (j=0; j < (int) s->img_y; ++j) {\r\n         if (easy) {\r\n            for (i=0; i < (int) s->img_x; ++i) {\r\n               int a;\r\n               out[z+2] = get8(s);\r\n               out[z+1] = get8(s);\r\n               out[z+0] = get8(s);\r\n               z += 3;\r\n               a = (easy == 2 ? get8(s) : 255);\r\n               if (target == 4) out[z++] = a;\r\n            }\r\n         } else {\r\n            for (i=0; i < (int) s->img_x; ++i) {\r\n               uint32 v = (bpp == 16 ? get16le(s) : get32le(s));\r\n               int a;\r\n               out[z++] = shiftsigned(v & mr, rshift, rcount);\r\n               out[z++] = shiftsigned(v & mg, gshift, gcount);\r\n               out[z++] = shiftsigned(v & mb, bshift, bcount);\r\n               a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);\r\n               if (target == 4) out[z++] = a;\r\n            }\r\n         }\r\n         skip(s, pad);\r\n      }\r\n   }\r\n   if (flip_vertically) {\r\n      stbi_uc t;\r\n      for (j=0; j < (int) s->img_y>>1; ++j) {\r\n         stbi_uc *p1 = out +      j     *s->img_x*target;\r\n         stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;\r\n         for (i=0; i < (int) s->img_x*target; ++i) {\r\n            t = p1[i], p1[i] = p2[i], p2[i] = t;\r\n         }\r\n      }\r\n   }\r\n\r\n   if (req_comp && req_comp != target) {\r\n      out = convert_format(out, target, req_comp, s->img_x, s->img_y);\r\n      if (out == NULL) return out; // convert_format frees input on failure\r\n   }\r\n\r\n   *x = s->img_x;\r\n   *y = s->img_y;\r\n   if (comp) *comp = target;\r\n   return out;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nstbi_uc *stbi_bmp_load             (char const *filename,           int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi_uc *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_bmp_load_from_file(f, x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n\r\nstbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_file(&s, f);\r\n   return bmp_load(&s, x,y,comp,req_comp);\r\n}\r\n#endif\r\n\r\nstbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return bmp_load(&s, x,y,comp,req_comp);\r\n}\r\n\r\n// Targa Truevision - TGA\r\n// by Jonathan Dummer\r\n\r\nstatic int tga_test(stbi *s)\r\n{\r\n\tint sz;\r\n\tget8u(s);\t\t//\tdiscard Offset\r\n\tsz = get8u(s);\t//\tcolor type\r\n\tif( sz > 1 ) return 0;\t//\tonly RGB or indexed allowed\r\n\tsz = get8u(s);\t//\timage type\r\n\tif( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0;\t//\tonly RGB or grey allowed, +/- RLE\r\n\tget16(s);\t\t//\tdiscard palette start\r\n\tget16(s);\t\t//\tdiscard palette length\r\n\tget8(s);\t\t\t//\tdiscard bits per palette color entry\r\n\tget16(s);\t\t//\tdiscard x origin\r\n\tget16(s);\t\t//\tdiscard y origin\r\n\tif( get16(s) < 1 ) return 0;\t\t//\ttest width\r\n\tif( get16(s) < 1 ) return 0;\t\t//\ttest height\r\n\tsz = get8(s);\t//\tbits per pixel\r\n\tif( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0;\t//\tonly RGB or RGBA or grey allowed\r\n\treturn 1;\t\t//\tseems to have passed everything\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint      stbi_tga_test_file        (FILE *f)\r\n{\r\n   stbi s;\r\n   int r,n = ftell(f);\r\n   start_file(&s, f);\r\n   r = tga_test(&s);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint      stbi_tga_test_memory      (stbi_uc const *buffer, int len)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return tga_test(&s);\r\n}\r\n\r\nstatic stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n\t//\tread in the TGA header stuff\r\n\tint tga_offset = get8u(s);\r\n\tint tga_indexed = get8u(s);\r\n\tint tga_image_type = get8u(s);\r\n\tint tga_is_RLE = 0;\r\n\tint tga_palette_start = get16le(s);\r\n\tint tga_palette_len = get16le(s);\r\n\tint tga_palette_bits = get8u(s);\r\n\tint tga_x_origin = get16le(s);\r\n\tint tga_y_origin = get16le(s);\r\n\tint tga_width = get16le(s);\r\n\tint tga_height = get16le(s);\r\n\tint tga_bits_per_pixel = get8u(s);\r\n\tint tga_inverted = get8u(s);\r\n\t//\timage data\r\n\tunsigned char *tga_data;\r\n\tunsigned char *tga_palette = NULL;\r\n\tint i, j;\r\n\tunsigned char raw_data[4];\r\n\tunsigned char trans_data[] = { 0,0,0,0 };\r\n\tint RLE_count = 0;\r\n\tint RLE_repeating = 0;\r\n\tint read_next_pixel = 1;\r\n\t//\tdo a tiny bit of precessing\r\n\tif( tga_image_type >= 8 )\r\n\t{\r\n\t\ttga_image_type -= 8;\r\n\t\ttga_is_RLE = 1;\r\n\t}\r\n\t/* int tga_alpha_bits = tga_inverted & 15; */\r\n\ttga_inverted = 1 - ((tga_inverted >> 5) & 1);\r\n\r\n\t//\terror check\r\n\tif( //(tga_indexed) ||\r\n\t\t(tga_width < 1) || (tga_height < 1) ||\r\n\t\t(tga_image_type < 1) || (tga_image_type > 3) ||\r\n\t\t((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&\r\n\t\t(tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))\r\n\t\t)\r\n\t{\r\n\t\treturn NULL;\r\n\t}\r\n\r\n\t//\tIf I'm paletted, then I'll use the number of bits from the palette\r\n\tif( tga_indexed )\r\n\t{\r\n\t\ttga_bits_per_pixel = tga_palette_bits;\r\n\t}\r\n\r\n\t//\ttga info\r\n\t*x = tga_width;\r\n\t*y = tga_height;\r\n\tif( (req_comp < 1) || (req_comp > 4) )\r\n\t{\r\n\t\t//\tjust use whatever the file was\r\n\t\treq_comp = tga_bits_per_pixel / 8;\r\n\t\t*comp = req_comp;\r\n\t} else\r\n\t{\r\n\t\t//\tforce a new number of components\r\n\t\t*comp = tga_bits_per_pixel/8;\r\n\t}\r\n\ttga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );\r\n\r\n\t//\tskip to the data's starting position (offset usually = 0)\r\n\tskip(s, tga_offset );\r\n\t//\tdo I need to load a palette?\r\n\tif( tga_indexed )\r\n\t{\r\n\t\t//\tany data to skip? (offset usually = 0)\r\n\t\tskip(s, tga_palette_start );\r\n\t\t//\tload the palette\r\n\t\ttga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );\r\n\t\tgetn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 );\r\n\t}\r\n\t//\tload the data\r\n\tfor( i = 0; i < tga_width * tga_height; ++i )\r\n\t{\r\n\t\t//\tif I'm in RLE mode, do I need to get a RLE chunk?\r\n\t\tif( tga_is_RLE )\r\n\t\t{\r\n\t\t\tif( RLE_count == 0 )\r\n\t\t\t{\r\n\t\t\t\t//\tyep, get the next byte as a RLE command\r\n\t\t\t\tint RLE_cmd = get8u(s);\r\n\t\t\t\tRLE_count = 1 + (RLE_cmd & 127);\r\n\t\t\t\tRLE_repeating = RLE_cmd >> 7;\r\n\t\t\t\tread_next_pixel = 1;\r\n\t\t\t} else if( !RLE_repeating )\r\n\t\t\t{\r\n\t\t\t\tread_next_pixel = 1;\r\n\t\t\t}\r\n\t\t} else\r\n\t\t{\r\n\t\t\tread_next_pixel = 1;\r\n\t\t}\r\n\t\t//\tOK, if I need to read a pixel, do it now\r\n\t\tif( read_next_pixel )\r\n\t\t{\r\n\t\t\t//\tload however much data we did have\r\n\t\t\tif( tga_indexed )\r\n\t\t\t{\r\n\t\t\t\t//\tread in 1 byte, then perform the lookup\r\n\t\t\t\tint pal_idx = get8u(s);\r\n\t\t\t\tif( pal_idx >= tga_palette_len )\r\n\t\t\t\t{\r\n\t\t\t\t\t//\tinvalid index\r\n\t\t\t\t\tpal_idx = 0;\r\n\t\t\t\t}\r\n\t\t\t\tpal_idx *= tga_bits_per_pixel / 8;\r\n\t\t\t\tfor( j = 0; j*8 < tga_bits_per_pixel; ++j )\r\n\t\t\t\t{\r\n\t\t\t\t\traw_data[j] = tga_palette[pal_idx+j];\r\n\t\t\t\t}\r\n\t\t\t} else\r\n\t\t\t{\r\n\t\t\t\t//\tread in the data raw\r\n\t\t\t\tfor( j = 0; j*8 < tga_bits_per_pixel; ++j )\r\n\t\t\t\t{\r\n\t\t\t\t\traw_data[j] = get8u(s);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t//\tconvert raw to the intermediate format\r\n\t\t\tswitch( tga_bits_per_pixel )\r\n\t\t\t{\r\n\t\t\tcase 8:\r\n\t\t\t\t//\tLuminous => RGBA\r\n\t\t\t\ttrans_data[0] = raw_data[0];\r\n\t\t\t\ttrans_data[1] = raw_data[0];\r\n\t\t\t\ttrans_data[2] = raw_data[0];\r\n\t\t\t\ttrans_data[3] = 255;\r\n\t\t\t\tbreak;\r\n\t\t\tcase 16:\r\n\t\t\t\t//\tLuminous,Alpha => RGBA\r\n\t\t\t\ttrans_data[0] = raw_data[0];\r\n\t\t\t\ttrans_data[1] = raw_data[0];\r\n\t\t\t\ttrans_data[2] = raw_data[0];\r\n\t\t\t\ttrans_data[3] = raw_data[1];\r\n\t\t\t\tbreak;\r\n\t\t\tcase 24:\r\n\t\t\t\t//\tBGR => RGBA\r\n\t\t\t\ttrans_data[0] = raw_data[2];\r\n\t\t\t\ttrans_data[1] = raw_data[1];\r\n\t\t\t\ttrans_data[2] = raw_data[0];\r\n\t\t\t\ttrans_data[3] = 255;\r\n\t\t\t\tbreak;\r\n\t\t\tcase 32:\r\n\t\t\t\t//\tBGRA => RGBA\r\n\t\t\t\ttrans_data[0] = raw_data[2];\r\n\t\t\t\ttrans_data[1] = raw_data[1];\r\n\t\t\t\ttrans_data[2] = raw_data[0];\r\n\t\t\t\ttrans_data[3] = raw_data[3];\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\t//\tclear the reading flag for the next pixel\r\n\t\t\tread_next_pixel = 0;\r\n\t\t} // end of reading a pixel\r\n\t\t//\tconvert to final format\r\n\t\tswitch( req_comp )\r\n\t\t{\r\n\t\tcase 1:\r\n\t\t\t//\tRGBA => Luminance\r\n\t\t\ttga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);\r\n\t\t\tbreak;\r\n\t\tcase 2:\r\n\t\t\t//\tRGBA => Luminance,Alpha\r\n\t\t\ttga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);\r\n\t\t\ttga_data[i*req_comp+1] = trans_data[3];\r\n\t\t\tbreak;\r\n\t\tcase 3:\r\n\t\t\t//\tRGBA => RGB\r\n\t\t\ttga_data[i*req_comp+0] = trans_data[0];\r\n\t\t\ttga_data[i*req_comp+1] = trans_data[1];\r\n\t\t\ttga_data[i*req_comp+2] = trans_data[2];\r\n\t\t\tbreak;\r\n\t\tcase 4:\r\n\t\t\t//\tRGBA => RGBA\r\n\t\t\ttga_data[i*req_comp+0] = trans_data[0];\r\n\t\t\ttga_data[i*req_comp+1] = trans_data[1];\r\n\t\t\ttga_data[i*req_comp+2] = trans_data[2];\r\n\t\t\ttga_data[i*req_comp+3] = trans_data[3];\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\t//\tin case we're in RLE mode, keep counting down\r\n\t\t--RLE_count;\r\n\t}\r\n\t//\tdo I need to invert the image?\r\n\tif( tga_inverted )\r\n\t{\r\n\t\tfor( j = 0; j*2 < tga_height; ++j )\r\n\t\t{\r\n\t\t\tint index1 = j * tga_width * req_comp;\r\n\t\t\tint index2 = (tga_height - 1 - j) * tga_width * req_comp;\r\n\t\t\tfor( i = tga_width * req_comp; i > 0; --i )\r\n\t\t\t{\r\n\t\t\t\tunsigned char temp = tga_data[index1];\r\n\t\t\t\ttga_data[index1] = tga_data[index2];\r\n\t\t\t\ttga_data[index2] = temp;\r\n\t\t\t\t++index1;\r\n\t\t\t\t++index2;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t//\tclear my palette, if I had one\r\n\tif( tga_palette != NULL )\r\n\t{\r\n\t\tfree( tga_palette );\r\n\t}\r\n\t//\tthe things I do to get rid of an error message, and yet keep\r\n\t//\tMicrosoft's C compilers happy... [8^(\r\n\ttga_palette_start = tga_palette_len = tga_palette_bits =\r\n\t\t\ttga_x_origin = tga_y_origin = 0;\r\n\t//\tOK, done\r\n\treturn tga_data;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nstbi_uc *stbi_tga_load             (char const *filename,           int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi_uc *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_tga_load_from_file(f, x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n\r\nstbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_file(&s, f);\r\n   return tga_load(&s, x,y,comp,req_comp);\r\n}\r\n#endif\r\n\r\nstbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return tga_load(&s, x,y,comp,req_comp);\r\n}\r\n\r\n\r\n// *************************************************************************************************\r\n// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicholas Schulz, tweaked by STB\r\n\r\nstatic int psd_test(stbi *s)\r\n{\r\n\tif (get32(s) != 0x38425053) return 0;\t// \"8BPS\"\r\n\telse return 1;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint stbi_psd_test_file(FILE *f)\r\n{\r\n   stbi s;\r\n   int r,n = ftell(f);\r\n   start_file(&s, f);\r\n   r = psd_test(&s);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint stbi_psd_test_memory(stbi_uc const *buffer, int len)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return psd_test(&s);\r\n}\r\n\r\nstatic stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n\tint\tpixelCount;\r\n\tint channelCount, compression;\r\n\tint channel, i, count, len;\r\n   int w,h;\r\n   uint8 *out;\r\n\r\n\t// Check identifier\r\n\tif (get32(s) != 0x38425053)\t// \"8BPS\"\r\n\t\treturn epuc(\"not PSD\", \"Corrupt PSD image\");\r\n\r\n\t// Check file type version.\r\n\tif (get16(s) != 1)\r\n\t\treturn epuc(\"wrong version\", \"Unsupported version of PSD image\");\r\n\r\n\t// Skip 6 reserved bytes.\r\n\tskip(s, 6 );\r\n\r\n\t// Read the number of channels (R, G, B, A, etc).\r\n\tchannelCount = get16(s);\r\n\tif (channelCount < 0 || channelCount > 16)\r\n\t\treturn epuc(\"wrong channel count\", \"Unsupported number of channels in PSD image\");\r\n\r\n\t// Read the rows and columns of the image.\r\n   h = get32(s);\r\n   w = get32(s);\r\n\r\n\t// Make sure the depth is 8 bits.\r\n\tif (get16(s) != 8)\r\n\t\treturn epuc(\"unsupported bit depth\", \"PSD bit depth is not 8 bit\");\r\n\r\n\t// Make sure the color mode is RGB.\r\n\t// Valid options are:\r\n\t//   0: Bitmap\r\n\t//   1: Grayscale\r\n\t//   2: Indexed color\r\n\t//   3: RGB color\r\n\t//   4: CMYK color\r\n\t//   7: Multichannel\r\n\t//   8: Duotone\r\n\t//   9: Lab color\r\n\tif (get16(s) != 3)\r\n\t\treturn epuc(\"wrong color format\", \"PSD is not in RGB color format\");\r\n\r\n\t// Skip the Mode Data.  (It's the palette for indexed color; other info for other modes.)\r\n\tskip(s,get32(s) );\r\n\r\n\t// Skip the image resources.  (resolution, pen tool paths, etc)\r\n\tskip(s, get32(s) );\r\n\r\n\t// Skip the reserved data.\r\n\tskip(s, get32(s) );\r\n\r\n\t// Find out if the data is compressed.\r\n\t// Known values:\r\n\t//   0: no compression\r\n\t//   1: RLE compressed\r\n\tcompression = get16(s);\r\n\tif (compression > 1)\r\n\t\treturn epuc(\"bad compression\", \"PSD has an unknown compression format\");\r\n\r\n\t// Create the destination image.\r\n\tout = (stbi_uc *) malloc(4 * w*h);\r\n\tif (!out) return epuc(\"outofmem\", \"Out of memory\");\r\n   pixelCount = w*h;\r\n\r\n\t// Initialize the data to zero.\r\n\t//memset( out, 0, pixelCount * 4 );\r\n\r\n\t// Finally, the image data.\r\n\tif (compression) {\r\n\t\t// RLE as used by .PSD and .TIFF\r\n\t\t// Loop until you get the number of unpacked bytes you are expecting:\r\n\t\t//     Read the next source byte into n.\r\n\t\t//     If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.\r\n\t\t//     Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.\r\n\t\t//     Else if n is 128, noop.\r\n\t\t// Endloop\r\n\r\n\t\t// The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,\r\n\t\t// which we're going to just skip.\r\n\t\tskip(s, h * channelCount * 2 );\r\n\r\n\t\t// Read the RLE data by channel.\r\n\t\tfor (channel = 0; channel < 4; channel++) {\r\n\t\t\tuint8 *p;\r\n\r\n         p = out+channel;\r\n\t\t\tif (channel >= channelCount) {\r\n\t\t\t\t// Fill this channel with default data.\r\n\t\t\t\tfor (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;\r\n\t\t\t} else {\r\n\t\t\t\t// Read the RLE data.\r\n\t\t\t\tcount = 0;\r\n\t\t\t\twhile (count < pixelCount) {\r\n\t\t\t\t\tlen = get8(s);\r\n\t\t\t\t\tif (len == 128) {\r\n\t\t\t\t\t\t// No-op.\r\n\t\t\t\t\t} else if (len < 128) {\r\n\t\t\t\t\t\t// Copy next len+1 bytes literally.\r\n\t\t\t\t\t\tlen++;\r\n\t\t\t\t\t\tcount += len;\r\n\t\t\t\t\t\twhile (len) {\r\n\t\t\t\t\t\t\t*p = get8(s);\r\n                     p += 4;\r\n\t\t\t\t\t\t\tlen--;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else if (len > 128) {\r\n\t\t\t\t\t\tuint32\tval;\r\n\t\t\t\t\t\t// Next -len+1 bytes in the dest are replicated from next source byte.\r\n\t\t\t\t\t\t// (Interpret len as a negative 8-bit int.)\r\n\t\t\t\t\t\tlen ^= 0x0FF;\r\n\t\t\t\t\t\tlen += 2;\r\n                  val = get8(s);\r\n\t\t\t\t\t\tcount += len;\r\n\t\t\t\t\t\twhile (len) {\r\n\t\t\t\t\t\t\t*p = val;\r\n                     p += 4;\r\n\t\t\t\t\t\t\tlen--;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t} else {\r\n\t\t// We're at the raw image data.  It's each channel in order (Red, Green, Blue, Alpha, ...)\r\n\t\t// where each channel consists of an 8-bit value for each pixel in the image.\r\n\r\n\t\t// Read the data by channel.\r\n\t\tfor (channel = 0; channel < 4; channel++) {\r\n\t\t\tuint8 *p;\r\n\r\n         p = out + channel;\r\n\t\t\tif (channel > channelCount) {\r\n\t\t\t\t// Fill this channel with default data.\r\n\t\t\t\tfor (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;\r\n\t\t\t} else {\r\n\t\t\t\t// Read the data.\r\n\t\t\t\tcount = 0;\r\n\t\t\t\tfor (i = 0; i < pixelCount; i++)\r\n\t\t\t\t\t*p = get8(s), p += 4;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tif (req_comp && req_comp != 4) {\r\n\t\tout = convert_format(out, 4, req_comp, w, h);\r\n\t\tif (out == NULL) return out; // convert_format frees input on failure\r\n\t}\r\n\r\n\tif (comp) *comp = channelCount;\r\n\t*y = h;\r\n\t*x = w;\r\n\r\n\treturn out;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nstbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi_uc *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_psd_load_from_file(f, x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n\r\nstbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_file(&s, f);\r\n   return psd_load(&s, x,y,comp,req_comp);\r\n}\r\n#endif\r\n\r\nstbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_mem(&s, buffer, len);\r\n   return psd_load(&s, x,y,comp,req_comp);\r\n}\r\n\r\n\r\n// *************************************************************************************************\r\n// Radiance RGBE HDR loader\r\n// originally by Nicolas Schulz\r\n#ifndef STBI_NO_HDR\r\nstatic int hdr_test(stbi *s)\r\n{\r\n   char *signature = \"#?RADIANCE\\n\";\r\n   int i;\r\n   for (i=0; signature[i]; ++i)\r\n      if (get8(s) != signature[i])\r\n         return 0;\r\n\treturn 1;\r\n}\r\n\r\nint stbi_hdr_test_memory(stbi_uc const *buffer, int len)\r\n{\r\n   stbi s;\r\n\tstart_mem(&s, buffer, len);\r\n\treturn hdr_test(&s);\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nint stbi_hdr_test_file(FILE *f)\r\n{\r\n   stbi s;\r\n   int r,n = ftell(f);\r\n   start_file(&s, f);\r\n   r = hdr_test(&s);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\n#define HDR_BUFLEN  1024\r\nstatic char *hdr_gettoken(stbi *z, char *buffer)\r\n{\r\n   int len=0;\r\n\t//char *s = buffer,\r\n\tchar c = '\\0';\r\n\r\n   c = get8(z);\r\n\r\n\twhile (!at_eof(z) && c != '\\n') {\r\n\t\tbuffer[len++] = c;\r\n      if (len == HDR_BUFLEN-1) {\r\n         // flush to end of line\r\n         while (!at_eof(z) && get8(z) != '\\n')\r\n            ;\r\n         break;\r\n      }\r\n      c = get8(z);\r\n\t}\r\n\r\n   buffer[len] = 0;\r\n\treturn buffer;\r\n}\r\n\r\nstatic void hdr_convert(float *output, stbi_uc *input, int req_comp)\r\n{\r\n\tif( input[3] != 0 ) {\r\n      float f1;\r\n\t\t// Exponent\r\n\t\tf1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));\r\n      if (req_comp <= 2)\r\n         output[0] = (input[0] + input[1] + input[2]) * f1 / 3;\r\n      else {\r\n         output[0] = input[0] * f1;\r\n         output[1] = input[1] * f1;\r\n         output[2] = input[2] * f1;\r\n      }\r\n      if (req_comp == 2) output[1] = 1;\r\n      if (req_comp == 4) output[3] = 1;\r\n\t} else {\r\n      switch (req_comp) {\r\n         case 4: output[3] = 1; /* fallthrough */\r\n         case 3: output[0] = output[1] = output[2] = 0;\r\n                 break;\r\n         case 2: output[1] = 1; /* fallthrough */\r\n         case 1: output[0] = 0;\r\n                 break;\r\n      }\r\n\t}\r\n}\r\n\r\n\r\nstatic float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   char buffer[HDR_BUFLEN];\r\n\tchar *token;\r\n\tint valid = 0;\r\n\tint width, height;\r\n   stbi_uc *scanline;\r\n\tfloat *hdr_data;\r\n\tint len;\r\n\tunsigned char count, value;\r\n\tint i, j, k, c1,c2, z;\r\n\r\n\r\n\t// Check identifier\r\n\tif (strcmp(hdr_gettoken(s,buffer), \"#?RADIANCE\") != 0)\r\n\t\treturn epf(\"not HDR\", \"Corrupt HDR image\");\r\n\r\n\t// Parse header\r\n\twhile(1) {\r\n\t\ttoken = hdr_gettoken(s,buffer);\r\n      if (token[0] == 0) break;\r\n\t\tif (strcmp(token, \"FORMAT=32-bit_rle_rgbe\") == 0) valid = 1;\r\n   }\r\n\r\n\tif (!valid)    return epf(\"unsupported format\", \"Unsupported HDR format\");\r\n\r\n   // Parse width and height\r\n   // can't use sscanf() if we're not using stdio!\r\n   token = hdr_gettoken(s,buffer);\r\n   if (strncmp(token, \"-Y \", 3))  return epf(\"unsupported data layout\", \"Unsupported HDR format\");\r\n   token += 3;\r\n   height = strtol(token, &token, 10);\r\n   while (*token == ' ') ++token;\r\n   if (strncmp(token, \"+X \", 3))  return epf(\"unsupported data layout\", \"Unsupported HDR format\");\r\n   token += 3;\r\n   width = strtol(token, NULL, 10);\r\n\r\n\t*x = width;\r\n\t*y = height;\r\n\r\n   *comp = 3;\r\n\tif (req_comp == 0) req_comp = 3;\r\n\r\n\t// Read data\r\n\thdr_data = (float *) malloc(height * width * req_comp * sizeof(float));\r\n\r\n\t// Load image data\r\n   // image data is stored as some number of sca\r\n\tif( width < 8 || width >= 32768) {\r\n\t\t// Read flat data\r\n      for (j=0; j < height; ++j) {\r\n         for (i=0; i < width; ++i) {\r\n            stbi_uc rgbe[4];\r\n           main_decode_loop:\r\n            getn(s, rgbe, 4);\r\n            hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);\r\n         }\r\n      }\r\n\t} else {\r\n\t\t// Read RLE-encoded data\r\n\t\tscanline = NULL;\r\n\r\n\t\tfor (j = 0; j < height; ++j) {\r\n         c1 = get8(s);\r\n         c2 = get8(s);\r\n         len = get8(s);\r\n         if (c1 != 2 || c2 != 2 || (len & 0x80)) {\r\n            // not run-length encoded, so we have to actually use THIS data as a decoded\r\n            // pixel (note this can't be a valid pixel--one of RGB must be >= 128)\r\n            stbi_uc rgbe[4] = { c1,c2,len, get8(s) };\r\n            hdr_convert(hdr_data, rgbe, req_comp);\r\n            i = 1;\r\n            j = 0;\r\n            free(scanline);\r\n            goto main_decode_loop; // yes, this is fucking insane; blame the fucking insane format\r\n         }\r\n         len <<= 8;\r\n         len |= get8(s);\r\n         if (len != width) { free(hdr_data); free(scanline); return epf(\"invalid decoded scanline length\", \"corrupt HDR\"); }\r\n         if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);\r\n\r\n\t\t\tfor (k = 0; k < 4; ++k) {\r\n\t\t\t\ti = 0;\r\n\t\t\t\twhile (i < width) {\r\n\t\t\t\t\tcount = get8(s);\r\n\t\t\t\t\tif (count > 128) {\r\n\t\t\t\t\t\t// Run\r\n\t\t\t\t\t\tvalue = get8(s);\r\n                  count -= 128;\r\n\t\t\t\t\t\tfor (z = 0; z < count; ++z)\r\n\t\t\t\t\t\t\tscanline[i++ * 4 + k] = value;\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\t// Dump\r\n\t\t\t\t\t\tfor (z = 0; z < count; ++z)\r\n\t\t\t\t\t\t\tscanline[i++ * 4 + k] = get8(s);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n         for (i=0; i < width; ++i)\r\n            hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);\r\n\t\t}\r\n      free(scanline);\r\n\t}\r\n\r\n   return hdr_data;\r\n}\r\n\r\nstatic stbi_uc *hdr_load_rgbe(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   char buffer[HDR_BUFLEN];\r\n\tchar *token;\r\n\tint valid = 0;\r\n\tint width, height;\r\n   stbi_uc *scanline;\r\n\tstbi_uc *rgbe_data;\r\n\tint len;\r\n\tunsigned char count, value;\r\n\tint i, j, k, c1,c2, z;\r\n\r\n\r\n\t// Check identifier\r\n\tif (strcmp(hdr_gettoken(s,buffer), \"#?RADIANCE\") != 0)\r\n\t\treturn epuc(\"not HDR\", \"Corrupt HDR image\");\r\n\r\n\t// Parse header\r\n\twhile(1) {\r\n\t\ttoken = hdr_gettoken(s,buffer);\r\n      if (token[0] == 0) break;\r\n\t\tif (strcmp(token, \"FORMAT=32-bit_rle_rgbe\") == 0) valid = 1;\r\n   }\r\n\r\n\tif (!valid)    return epuc(\"unsupported format\", \"Unsupported HDR format\");\r\n\r\n   // Parse width and height\r\n   // can't use sscanf() if we're not using stdio!\r\n   token = hdr_gettoken(s,buffer);\r\n   if (strncmp(token, \"-Y \", 3))  return epuc(\"unsupported data layout\", \"Unsupported HDR format\");\r\n   token += 3;\r\n   height = strtol(token, &token, 10);\r\n   while (*token == ' ') ++token;\r\n   if (strncmp(token, \"+X \", 3))  return epuc(\"unsupported data layout\", \"Unsupported HDR format\");\r\n   token += 3;\r\n   width = strtol(token, NULL, 10);\r\n\r\n\t*x = width;\r\n\t*y = height;\r\n\r\n\t// RGBE _MUST_ come out as 4 components\r\n   *comp = 4;\r\n\treq_comp = 4;\r\n\r\n\t// Read data\r\n\trgbe_data = (stbi_uc *) malloc(height * width * req_comp * sizeof(stbi_uc));\r\n\t//\tpoint to the beginning\r\n\tscanline = rgbe_data;\r\n\r\n\t// Load image data\r\n   // image data is stored as some number of scan lines\r\n\tif( width < 8 || width >= 32768) {\r\n\t\t// Read flat data\r\n      for (j=0; j < height; ++j) {\r\n         for (i=0; i < width; ++i) {\r\n           main_decode_loop:\r\n            //getn(rgbe, 4);\r\n            getn(s,scanline, 4);\r\n\t\t\tscanline += 4;\r\n         }\r\n      }\r\n\t} else {\r\n\t\t// Read RLE-encoded data\r\n\t\tfor (j = 0; j < height; ++j) {\r\n         c1 = get8(s);\r\n         c2 = get8(s);\r\n         len = get8(s);\r\n         if (c1 != 2 || c2 != 2 || (len & 0x80)) {\r\n            // not run-length encoded, so we have to actually use THIS data as a decoded\r\n            // pixel (note this can't be a valid pixel--one of RGB must be >= 128)\r\n            scanline[0] = c1;\r\n            scanline[1] = c2;\r\n            scanline[2] = len;\r\n            scanline[3] = get8(s);\r\n            scanline += 4;\r\n            i = 1;\r\n            j = 0;\r\n            goto main_decode_loop; // yes, this is insane; blame the insane format\r\n         }\r\n         len <<= 8;\r\n         len |= get8(s);\r\n         if (len != width) { free(rgbe_data); return epuc(\"invalid decoded scanline length\", \"corrupt HDR\"); }\r\n\t\t\tfor (k = 0; k < 4; ++k) {\r\n\t\t\t\ti = 0;\r\n\t\t\t\twhile (i < width) {\r\n\t\t\t\t\tcount = get8(s);\r\n\t\t\t\t\tif (count > 128) {\r\n\t\t\t\t\t\t// Run\r\n\t\t\t\t\t\tvalue = get8(s);\r\n                  count -= 128;\r\n\t\t\t\t\t\tfor (z = 0; z < count; ++z)\r\n\t\t\t\t\t\t\tscanline[i++ * 4 + k] = value;\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\t// Dump\r\n\t\t\t\t\t\tfor (z = 0; z < count; ++z)\r\n\t\t\t\t\t\t\tscanline[i++ * 4 + k] = get8(s);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t//\tmove the scanline on\r\n\t\t\tscanline += 4 * width;\r\n\t\t}\r\n\t}\r\n\r\n   return rgbe_data;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nfloat *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_file(&s,f);\r\n   return hdr_load(&s,x,y,comp,req_comp);\r\n}\r\n\r\nstbi_uc *stbi_hdr_load_rgbe_file(FILE *f, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_file(&s,f);\r\n   return hdr_load_rgbe(&s,x,y,comp,req_comp);\r\n}\r\n\r\nstbi_uc *stbi_hdr_load_rgbe        (char const *filename,           int *x, int *y, int *comp, int req_comp)\r\n{\r\n   FILE *f = fopen(filename, \"rb\");\r\n   unsigned char *result;\r\n   if (!f) return epuc(\"can't fopen\", \"Unable to open file\");\r\n   result = stbi_hdr_load_rgbe_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return result;\r\n}\r\n#endif\r\n\r\nfloat *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_mem(&s,buffer, len);\r\n   return hdr_load(&s,x,y,comp,req_comp);\r\n}\r\n\r\nstbi_uc *stbi_hdr_load_rgbe_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi s;\r\n   start_mem(&s,buffer, len);\r\n   return hdr_load_rgbe(&s,x,y,comp,req_comp);\r\n}\r\n\r\n#endif // STBI_NO_HDR\r\n\r\n/////////////////////// write image ///////////////////////\r\n\r\n#ifndef STBI_NO_WRITE\r\n\r\nstatic void write8(FILE *f, int x) { uint8 z = (uint8) x; fwrite(&z,1,1,f); }\r\n\r\nstatic void writefv(FILE *f, char *fmt, va_list v)\r\n{\r\n   while (*fmt) {\r\n      switch (*fmt++) {\r\n         case ' ': break;\r\n         case '1': { uint8 x = va_arg(v, int); write8(f,x); break; }\r\n         case '2': { int16 x = va_arg(v, int); write8(f,x); write8(f,x>>8); break; }\r\n         case '4': { int32 x = va_arg(v, int); write8(f,x); write8(f,x>>8); write8(f,x>>16); write8(f,x>>24); break; }\r\n         default:\r\n            assert(0);\r\n            va_end(v);\r\n            return;\r\n      }\r\n   }\r\n}\r\n\r\nstatic void writef(FILE *f, char *fmt, ...)\r\n{\r\n   va_list v;\r\n   va_start(v, fmt);\r\n   writefv(f,fmt,v);\r\n   va_end(v);\r\n}\r\n\r\nstatic void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad)\r\n{\r\n   uint8 bg[3] = { 255, 0, 255}, px[3];\r\n   uint32 zero = 0;\r\n   int i,j,k, j_end;\r\n\r\n   if (vdir < 0)\r\n      j_end = -1, j = y-1;\r\n   else\r\n      j_end =  y, j = 0;\r\n\r\n   for (; j != j_end; j += vdir) {\r\n      for (i=0; i < x; ++i) {\r\n         uint8 *d = (uint8 *) data + (j*x+i)*comp;\r\n         if (write_alpha < 0)\r\n            fwrite(&d[comp-1], 1, 1, f);\r\n         switch (comp) {\r\n            case 1:\r\n            case 2: writef(f, \"111\", d[0],d[0],d[0]);\r\n                    break;\r\n            case 4:\r\n               if (!write_alpha) {\r\n                  for (k=0; k < 3; ++k)\r\n                     px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255;\r\n                  writef(f, \"111\", px[1-rgb_dir],px[1],px[1+rgb_dir]);\r\n                  break;\r\n               }\r\n               /* FALLTHROUGH */\r\n            case 3:\r\n               writef(f, \"111\", d[1-rgb_dir],d[1],d[1+rgb_dir]);\r\n               break;\r\n         }\r\n         if (write_alpha > 0)\r\n            fwrite(&d[comp-1], 1, 1, f);\r\n      }\r\n      fwrite(&zero,scanline_pad,1,f);\r\n   }\r\n}\r\n\r\nstatic int outfile(char const *filename, int rgb_dir, int vdir, int x, int y, int comp, void *data, int alpha, int pad, char *fmt, ...)\r\n{\r\n   FILE *f = fopen(filename, \"wb\");\r\n   if (f) {\r\n      va_list v;\r\n      va_start(v, fmt);\r\n      writefv(f, fmt, v);\r\n      va_end(v);\r\n      write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad);\r\n      fclose(f);\r\n   }\r\n   return f != NULL;\r\n}\r\n\r\nint stbi_write_bmp(char const *filename, int x, int y, int comp, void *data)\r\n{\r\n   int pad = (-x*3) & 3;\r\n   return outfile(filename,-1,-1,x,y,comp,data,0,pad,\r\n           \"11 4 22 4\" \"4 44 22 444444\",\r\n           'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40,  // file header\r\n            40, x,y, 1,24, 0,0,0,0,0,0);             // bitmap header\r\n}\r\n\r\nint stbi_write_tga(char const *filename, int x, int y, int comp, void *data)\r\n{\r\n   int has_alpha = !(comp & 1);\r\n   return outfile(filename, -1,-1, x, y, comp, data, has_alpha, 0,\r\n                  \"111 221 2222 11\", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha);\r\n}\r\n\r\n// any other image formats that do interleaved rgb data?\r\n//    PNG: requires adler32,crc32 -- significant amount of code\r\n//    PSD: no, channels output separately\r\n//    TIFF: no, stripwise-interleaved... i think\r\n\r\n#endif // STBI_NO_WRITE\r\n\r\n//\tadd in my DDS loading support\r\n#ifndef STBI_NO_DDS\r\n#include \"stbi_DDS_aug_c.h\"\r\n#endif\r\n"
  },
  {
    "path": "src/stb_image_aug.h",
    "content": "/* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c\r\n                      when you control the images you're loading\r\n\r\n   QUICK NOTES:\r\n      Primarily of interest to game developers and other people who can\r\n          avoid problematic images and only need the trivial interface\r\n\r\n      JPEG baseline (no JPEG progressive, no oddball channel decimations)\r\n      PNG non-interlaced\r\n      BMP non-1bpp, non-RLE\r\n      TGA (not sure what subset, if a subset)\r\n      PSD (composited view only, no extra channels)\r\n      HDR (radiance rgbE format)\r\n      writes BMP,TGA (define STBI_NO_WRITE to remove code)\r\n      decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)\r\n      supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)\r\n        \r\n   TODO:\r\n      stbi_info_*\r\n  \r\n   history:\r\n      1.16   major bugfix - convert_format converted one too many pixels\r\n      1.15   initialize some fields for thread safety\r\n      1.14   fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including)\r\n      1.13   threadsafe\r\n      1.12   const qualifiers in the API\r\n      1.11   Support installable IDCT, colorspace conversion routines\r\n      1.10   Fixes for 64-bit (don't use \"unsigned long\")\r\n             optimized upsampling by Fabian \"ryg\" Giesen\r\n      1.09   Fix format-conversion for PSD code (bad global variables!)\r\n      1.08   Thatcher Ulrich's PSD code integrated by Nicolas Schulz\r\n      1.07   attempt to fix C++ warning/errors again\r\n      1.06   attempt to fix C++ warning/errors again\r\n      1.05   fix TGA loading to return correct *comp and use good luminance calc\r\n      1.04   default float alpha is 1, not 255; use 'void *' for stbi_image_free\r\n      1.03   bugfixes to STBI_NO_STDIO, STBI_NO_HDR\r\n      1.02   support for (subset of) HDR files, float interface for preferred access to them\r\n      1.01   fix bug: possible bug in handling right-side up bmps... not sure\r\n             fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all\r\n      1.00   interface to zlib that skips zlib header\r\n      0.99   correct handling of alpha in palette\r\n      0.98   TGA loader by lonesock; dynamically add loaders (untested)\r\n      0.97   jpeg errors on too large a file; also catch another malloc failure\r\n      0.96   fix detection of invalid v value - particleman@mollyrocket forum\r\n      0.95   during header scan, seek to markers in case of padding\r\n      0.94   STBI_NO_STDIO to disable stdio usage; rename all #defines the same\r\n      0.93   handle jpegtran output; verbose errors\r\n      0.92   read 4,8,16,24,32-bit BMP files of several formats\r\n      0.91   output 24-bit Windows 3.0 BMP files\r\n      0.90   fix a few more warnings; bump version number to approach 1.0\r\n      0.61   bugfixes due to Marc LeBlanc, Christopher Lloyd\r\n      0.60   fix compiling as c++\r\n      0.59   fix warnings: merge Dave Moore's -Wall fixes\r\n      0.58   fix bug: zlib uncompressed mode len/nlen was wrong endian\r\n      0.57   fix bug: jpg last huffman symbol before marker was >9 bits but less\r\n                      than 16 available\r\n      0.56   fix bug: zlib uncompressed mode len vs. nlen\r\n      0.55   fix bug: restart_interval not initialized to 0\r\n      0.54   allow NULL for 'int *comp'\r\n      0.53   fix bug in png 3->4; speedup png decoding\r\n      0.52   png handles req_comp=3,4 directly; minor cleanup; jpeg comments\r\n      0.51   obey req_comp requests, 1-component jpegs return as 1-component,\r\n             on 'test' only check type, not whether we support this variant\r\n*/\r\n\r\n#ifndef HEADER_STB_IMAGE_AUGMENTED\r\n#define HEADER_STB_IMAGE_AUGMENTED\r\n\r\n////   begin header file  ////////////////////////////////////////////////////\r\n//\r\n// Limitations:\r\n//    - no progressive/interlaced support (jpeg, png)\r\n//    - 8-bit samples only (jpeg, png)\r\n//    - not threadsafe\r\n//    - channel subsampling of at most 2 in each dimension (jpeg)\r\n//    - no delayed line count (jpeg) -- IJG doesn't support either\r\n//\r\n// Basic usage (see HDR discussion below):\r\n//    int x,y,n;\r\n//    unsigned char *data = stbi_load(filename, &x, &y, &n, 0);\r\n//    // ... process data if not NULL ... \r\n//    // ... x = width, y = height, n = # 8-bit components per pixel ...\r\n//    // ... replace '0' with '1'..'4' to force that many components per pixel\r\n//    stbi_image_free(data)\r\n//\r\n// Standard parameters:\r\n//    int *x       -- outputs image width in pixels\r\n//    int *y       -- outputs image height in pixels\r\n//    int *comp    -- outputs # of image components in image file\r\n//    int req_comp -- if non-zero, # of image components requested in result\r\n//\r\n// The return value from an image loader is an 'unsigned char *' which points\r\n// to the pixel data. The pixel data consists of *y scanlines of *x pixels,\r\n// with each pixel consisting of N interleaved 8-bit components; the first\r\n// pixel pointed to is top-left-most in the image. There is no padding between\r\n// image scanlines or between pixels, regardless of format. The number of\r\n// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.\r\n// If req_comp is non-zero, *comp has the number of components that _would_\r\n// have been output otherwise. E.g. if you set req_comp to 4, you will always\r\n// get RGBA output, but you can check *comp to easily see if it's opaque.\r\n//\r\n// An output image with N components has the following components interleaved\r\n// in this order in each pixel:\r\n//\r\n//     N=#comp     components\r\n//       1           grey\r\n//       2           grey, alpha\r\n//       3           red, green, blue\r\n//       4           red, green, blue, alpha\r\n//\r\n// If image loading fails for any reason, the return value will be NULL,\r\n// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()\r\n// can be queried for an extremely brief, end-user unfriendly explanation\r\n// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid\r\n// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly\r\n// more user-friendly ones.\r\n//\r\n// Paletted PNG and BMP images are automatically depalettized.\r\n//\r\n//\r\n// ===========================================================================\r\n//\r\n// HDR image support   (disable by defining STBI_NO_HDR)\r\n//\r\n// stb_image now supports loading HDR images in general, and currently\r\n// the Radiance .HDR file format, although the support is provided\r\n// generically. You can still load any file through the existing interface;\r\n// if you attempt to load an HDR file, it will be automatically remapped to\r\n// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;\r\n// both of these constants can be reconfigured through this interface:\r\n//\r\n//     stbi_hdr_to_ldr_gamma(2.2f);\r\n//     stbi_hdr_to_ldr_scale(1.0f);\r\n//\r\n// (note, do not use _inverse_ constants; stbi_image will invert them\r\n// appropriately).\r\n//\r\n// Additionally, there is a new, parallel interface for loading files as\r\n// (linear) floats to preserve the full dynamic range:\r\n//\r\n//    float *data = stbi_loadf(filename, &x, &y, &n, 0);\r\n// \r\n// If you load LDR images through this interface, those images will\r\n// be promoted to floating point values, run through the inverse of\r\n// constants corresponding to the above:\r\n//\r\n//     stbi_ldr_to_hdr_scale(1.0f);\r\n//     stbi_ldr_to_hdr_gamma(2.2f);\r\n//\r\n// Finally, given a filename (or an open file or memory block--see header\r\n// file for details) containing image data, you can query for the \"most\r\n// appropriate\" interface to use (that is, whether the image is HDR or\r\n// not), using:\r\n//\r\n//     stbi_is_hdr(char *filename);\r\n\r\n#ifndef STBI_NO_STDIO\r\n#include <stdio.h>\r\n#endif\r\n\r\n#define STBI_VERSION 1\r\n\r\nenum\r\n{\r\n   STBI_default = 0, // only used for req_comp\r\n\r\n   STBI_grey       = 1,\r\n   STBI_grey_alpha = 2,\r\n   STBI_rgb        = 3,\r\n   STBI_rgb_alpha  = 4,\r\n};\r\n\r\ntypedef unsigned char stbi_uc;\r\n\r\n#ifdef __cplusplus\r\nextern \"C\" {\r\n#endif\r\n\r\n// WRITING API\r\n\r\n#if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)\r\n// write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)\r\n// (you must include the appropriate extension in the filename).\r\n// returns TRUE on success, FALSE if couldn't open file, error writing file\r\nextern int      stbi_write_bmp       (char const *filename,     int x, int y, int comp, void *data);\r\nextern int      stbi_write_tga       (char const *filename,     int x, int y, int comp, void *data);\r\n#endif\r\n\r\n// PRIMARY API - works on images of any type\r\n\r\n// load image by filename, open file, or memory buffer\r\n#ifndef STBI_NO_STDIO\r\nextern stbi_uc *stbi_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_info_from_file  (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\nextern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n// for stbi_load_from_file, file pointer is left pointing immediately after image\r\n\r\n#ifndef STBI_NO_HDR\r\n#ifndef STBI_NO_STDIO\r\nextern float *stbi_loadf            (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern float *stbi_loadf_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\nextern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n\r\nextern void   stbi_hdr_to_ldr_gamma(float gamma);\r\nextern void   stbi_hdr_to_ldr_scale(float scale);\r\n\r\nextern void   stbi_ldr_to_hdr_gamma(float gamma);\r\nextern void   stbi_ldr_to_hdr_scale(float scale);\r\n\r\n#endif // STBI_NO_HDR\r\n\r\n// get a VERY brief reason for failure\r\n// NOT THREADSAFE\r\nextern char    *stbi_failure_reason  (void); \r\n\r\n// free the loaded image -- this is just free()\r\nextern void     stbi_image_free      (void *retval_from_stbi_load);\r\n\r\n// get image dimensions & components without fully decoding\r\nextern int      stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\nextern int      stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_info            (char const *filename,     int *x, int *y, int *comp);\r\nextern int      stbi_is_hdr          (char const *filename);\r\nextern int      stbi_is_hdr_from_file(FILE *f);\r\n#endif\r\n\r\n// ZLIB client - used by PNG, available for other purposes\r\n\r\nextern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);\r\nextern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);\r\nextern int   stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);\r\n\r\nextern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);\r\nextern int   stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);\r\n\r\n// TYPE-SPECIFIC ACCESS\r\n\r\n// is it a jpeg?\r\nextern int      stbi_jpeg_test_memory     (stbi_uc const *buffer, int len);\r\nextern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\n\r\n#ifndef STBI_NO_STDIO\r\nextern stbi_uc *stbi_jpeg_load            (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_jpeg_test_file       (FILE *f);\r\nextern stbi_uc *stbi_jpeg_load_from_file  (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n\r\nextern int      stbi_jpeg_info            (char const *filename,     int *x, int *y, int *comp);\r\nextern int      stbi_jpeg_info_from_file  (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\n\r\n// is it a png?\r\nextern int      stbi_png_test_memory      (stbi_uc const *buffer, int len);\r\nextern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);\r\n\r\n#ifndef STBI_NO_STDIO\r\nextern stbi_uc *stbi_png_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_png_info             (char const *filename,     int *x, int *y, int *comp);\r\nextern int      stbi_png_test_file        (FILE *f);\r\nextern stbi_uc *stbi_png_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\nextern int      stbi_png_info_from_file   (FILE *f,                  int *x, int *y, int *comp);\r\n#endif\r\n\r\n// is it a bmp?\r\nextern int      stbi_bmp_test_memory      (stbi_uc const *buffer, int len);\r\n\r\nextern stbi_uc *stbi_bmp_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_bmp_test_file        (FILE *f);\r\nextern stbi_uc *stbi_bmp_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\n\r\n// is it a tga?\r\nextern int      stbi_tga_test_memory      (stbi_uc const *buffer, int len);\r\n\r\nextern stbi_uc *stbi_tga_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_tga_test_file        (FILE *f);\r\nextern stbi_uc *stbi_tga_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\n\r\n// is it a psd?\r\nextern int      stbi_psd_test_memory      (stbi_uc const *buffer, int len);\r\n\r\nextern stbi_uc *stbi_psd_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_psd_test_file        (FILE *f);\r\nextern stbi_uc *stbi_psd_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\n\r\n// is it an hdr?\r\nextern int      stbi_hdr_test_memory      (stbi_uc const *buffer, int len);\r\n\r\nextern float *  stbi_hdr_load             (char const *filename,     int *x, int *y, int *comp, int req_comp);\r\nextern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_hdr_load_rgbe        (char const *filename,           int *x, int *y, int *comp, int req_comp);\r\nextern float *  stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_hdr_test_file        (FILE *f);\r\nextern float *  stbi_hdr_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_hdr_load_rgbe_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\n\r\n// define new loaders\r\ntypedef struct\r\n{\r\n   int       (*test_memory)(stbi_uc const *buffer, int len);\r\n   stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n   #ifndef STBI_NO_STDIO\r\n   int       (*test_file)(FILE *f);\r\n   stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);\r\n   #endif\r\n} stbi_loader;\r\n\r\n// register a loader by filling out the above structure (you must defined ALL functions)\r\n// returns 1 if added or already added, 0 if not added (too many loaders)\r\n// NOT THREADSAFE\r\nextern int stbi_register_loader(stbi_loader *loader);\r\n\r\n// define faster low-level operations (typically SIMD support)\r\n#if STBI_SIMD\r\ntypedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize);\r\n// compute an integer IDCT on \"input\"\r\n//     input[x] = data[x] * dequantize[x]\r\n//     write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'\r\n//                             CLAMP results to 0..255\r\ntypedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step);\r\n// compute a conversion from YCbCr to RGB\r\n//     'count' pixels\r\n//     write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B\r\n//     y: Y input channel\r\n//     cb: Cb input channel; scale/biased to be 0..255\r\n//     cr: Cr input channel; scale/biased to be 0..255\r\n\r\nextern void stbi_install_idct(stbi_idct_8x8 func);\r\nextern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);\r\n#endif // STBI_SIMD\r\n\r\n#ifdef __cplusplus\r\n}\r\n#endif\r\n\r\n//\r\n//\r\n////   end header file   /////////////////////////////////////////////////////\r\n#endif // STBI_INCLUDE_STB_IMAGE_H\r\n"
  },
  {
    "path": "src/stbi_DDS_aug.h",
    "content": "/*\r\n\tadding DDS loading support to stbi\r\n*/\r\n\r\n#ifndef HEADER_STB_IMAGE_DDS_AUGMENTATION\n#define HEADER_STB_IMAGE_DDS_AUGMENTATION\r\n\r\n//\tis it a DDS file?\r\nextern int      stbi_dds_test_memory      (stbi_uc const *buffer, int len);\r\n\r\nextern stbi_uc *stbi_dds_load             (char *filename,           int *x, int *y, int *comp, int req_comp);\r\nextern stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);\r\n#ifndef STBI_NO_STDIO\r\nextern int      stbi_dds_test_file        (FILE *f);\r\nextern stbi_uc *stbi_dds_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp);\r\n#endif\r\n\r\n//\r\n//\r\n////   end header file   /////////////////////////////////////////////////////\r\n#endif // HEADER_STB_IMAGE_DDS_AUGMENTATION\r\n"
  },
  {
    "path": "src/stbi_DDS_aug_c.h",
    "content": "\r\n///\tDDS file support, does decoding, _not_ direct uploading\r\n///\t(use SOIL for that ;-)\r\n\r\n///\tA bunch of DirectDraw Surface structures and flags\r\ntypedef struct {\r\n    unsigned int    dwMagic;\r\n    unsigned int    dwSize;\r\n    unsigned int    dwFlags;\r\n    unsigned int    dwHeight;\r\n    unsigned int    dwWidth;\r\n    unsigned int    dwPitchOrLinearSize;\r\n    unsigned int    dwDepth;\r\n    unsigned int    dwMipMapCount;\r\n    unsigned int    dwReserved1[ 11 ];\r\n\r\n    //  DDPIXELFORMAT\r\n    struct {\r\n      unsigned int    dwSize;\r\n      unsigned int    dwFlags;\r\n      unsigned int    dwFourCC;\r\n      unsigned int    dwRGBBitCount;\r\n      unsigned int    dwRBitMask;\r\n      unsigned int    dwGBitMask;\r\n      unsigned int    dwBBitMask;\r\n      unsigned int    dwAlphaBitMask;\r\n    }               sPixelFormat;\r\n\r\n    //  DDCAPS2\r\n    struct {\r\n      unsigned int    dwCaps1;\r\n      unsigned int    dwCaps2;\r\n      unsigned int    dwDDSX;\r\n      unsigned int    dwReserved;\r\n    }               sCaps;\r\n    unsigned int    dwReserved2;\r\n} DDS_header ;\r\n\r\n//\tthe following constants were copied directly off the MSDN website\r\n\r\n//\tThe dwFlags member of the original DDSURFACEDESC2 structure\r\n//\tcan be set to one or more of the following values.\r\n#define DDSD_CAPS\t0x00000001\r\n#define DDSD_HEIGHT\t0x00000002\r\n#define DDSD_WIDTH\t0x00000004\r\n#define DDSD_PITCH\t0x00000008\r\n#define DDSD_PIXELFORMAT\t0x00001000\r\n#define DDSD_MIPMAPCOUNT\t0x00020000\r\n#define DDSD_LINEARSIZE\t0x00080000\r\n#define DDSD_DEPTH\t0x00800000\r\n\r\n//\tDirectDraw Pixel Format\r\n#define DDPF_ALPHAPIXELS\t0x00000001\r\n#define DDPF_FOURCC\t0x00000004\r\n#define DDPF_RGB\t0x00000040\r\n\r\n//\tThe dwCaps1 member of the DDSCAPS2 structure can be\r\n//\tset to one or more of the following values.\r\n#define DDSCAPS_COMPLEX\t0x00000008\r\n#define DDSCAPS_TEXTURE\t0x00001000\r\n#define DDSCAPS_MIPMAP\t0x00400000\r\n\r\n//\tThe dwCaps2 member of the DDSCAPS2 structure can be\r\n//\tset to one or more of the following values.\r\n#define DDSCAPS2_CUBEMAP\t0x00000200\r\n#define DDSCAPS2_CUBEMAP_POSITIVEX\t0x00000400\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEX\t0x00000800\r\n#define DDSCAPS2_CUBEMAP_POSITIVEY\t0x00001000\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEY\t0x00002000\r\n#define DDSCAPS2_CUBEMAP_POSITIVEZ\t0x00004000\r\n#define DDSCAPS2_CUBEMAP_NEGATIVEZ\t0x00008000\r\n#define DDSCAPS2_VOLUME\t0x00200000\r\n\r\nstatic int dds_test(stbi *s)\r\n{\r\n\t//\tcheck the magic number\r\n\tif (get8(s) != 'D') return 0;\r\n\tif (get8(s) != 'D') return 0;\r\n\tif (get8(s) != 'S') return 0;\r\n\tif (get8(s) != ' ') return 0;\r\n\t//\tcheck header size\r\n\tif (get32le(s) != 124) return 0;\r\n\treturn 1;\r\n}\r\n#ifndef STBI_NO_STDIO\r\nint      stbi_dds_test_file        (FILE *f)\r\n{\r\n   stbi s;\r\n   int r,n = ftell(f);\r\n   start_file(&s,f);\r\n   r = dds_test(&s);\r\n   fseek(f,n,SEEK_SET);\r\n   return r;\r\n}\r\n#endif\r\n\r\nint      stbi_dds_test_memory      (stbi_uc const *buffer, int len)\r\n{\r\n   stbi s;\r\n   start_mem(&s,buffer, len);\r\n   return dds_test(&s);\r\n}\r\n\r\n//\thelper functions\r\nint stbi_convert_bit_range( int c, int from_bits, int to_bits )\r\n{\r\n\tint b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);\r\n\treturn (b + (b >> from_bits)) >> from_bits;\r\n}\r\nvoid stbi_rgb_888_from_565( unsigned int c, int *r, int *g, int *b )\r\n{\r\n\t*r = stbi_convert_bit_range( (c >> 11) & 31, 5, 8 );\r\n\t*g = stbi_convert_bit_range( (c >> 05) & 63, 6, 8 );\r\n\t*b = stbi_convert_bit_range( (c >> 00) & 31, 5, 8 );\r\n}\r\nvoid stbi_decode_DXT1_block(\r\n\t\t\tunsigned char uncompressed[16*4],\r\n\t\t\tunsigned char compressed[8] )\r\n{\r\n\tint next_bit = 4*8;\r\n\tint i, r, g, b;\r\n\tint c0, c1;\r\n\tunsigned char decode_colors[4*4];\r\n\t//\tfind the 2 primary colors\r\n\tc0 = compressed[0] + (compressed[1] << 8);\r\n\tc1 = compressed[2] + (compressed[3] << 8);\r\n\tstbi_rgb_888_from_565( c0, &r, &g, &b );\r\n\tdecode_colors[0] = r;\r\n\tdecode_colors[1] = g;\r\n\tdecode_colors[2] = b;\r\n\tdecode_colors[3] = 255;\r\n\tstbi_rgb_888_from_565( c1, &r, &g, &b );\r\n\tdecode_colors[4] = r;\r\n\tdecode_colors[5] = g;\r\n\tdecode_colors[6] = b;\r\n\tdecode_colors[7] = 255;\r\n\tif( c0 > c1 )\r\n\t{\r\n\t\t//\tno alpha, 2 interpolated colors\r\n\t\tdecode_colors[8] = (2*decode_colors[0] + decode_colors[4]) / 3;\r\n\t\tdecode_colors[9] = (2*decode_colors[1] + decode_colors[5]) / 3;\r\n\t\tdecode_colors[10] = (2*decode_colors[2] + decode_colors[6]) / 3;\r\n\t\tdecode_colors[11] = 255;\r\n\t\tdecode_colors[12] = (decode_colors[0] + 2*decode_colors[4]) / 3;\r\n\t\tdecode_colors[13] = (decode_colors[1] + 2*decode_colors[5]) / 3;\r\n\t\tdecode_colors[14] = (decode_colors[2] + 2*decode_colors[6]) / 3;\r\n\t\tdecode_colors[15] = 255;\r\n\t} else\r\n\t{\r\n\t\t//\t1 interpolated color, alpha\r\n\t\tdecode_colors[8] = (decode_colors[0] + decode_colors[4]) / 2;\r\n\t\tdecode_colors[9] = (decode_colors[1] + decode_colors[5]) / 2;\r\n\t\tdecode_colors[10] = (decode_colors[2] + decode_colors[6]) / 2;\r\n\t\tdecode_colors[11] = 255;\r\n\t\tdecode_colors[12] = 0;\r\n\t\tdecode_colors[13] = 0;\r\n\t\tdecode_colors[14] = 0;\r\n\t\tdecode_colors[15] = 0;\r\n\t}\r\n\t//\tdecode the block\r\n\tfor( i = 0; i < 16*4; i += 4 )\r\n\t{\r\n\t\tint idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 4;\r\n\t\tnext_bit += 2;\r\n\t\tuncompressed[i+0] = decode_colors[idx+0];\r\n\t\tuncompressed[i+1] = decode_colors[idx+1];\r\n\t\tuncompressed[i+2] = decode_colors[idx+2];\r\n\t\tuncompressed[i+3] = decode_colors[idx+3];\r\n\t}\r\n\t//\tdone\r\n}\r\nvoid stbi_decode_DXT23_alpha_block(\r\n\t\t\tunsigned char uncompressed[16*4],\r\n\t\t\tunsigned char compressed[8] )\r\n{\r\n\tint i, next_bit = 0;\r\n\t//\teach alpha value gets 4 bits\r\n\tfor( i = 3; i < 16*4; i += 4 )\r\n\t{\r\n\t\tuncompressed[i] = stbi_convert_bit_range(\r\n\t\t\t\t(compressed[next_bit>>3] >> (next_bit&7)) & 15,\r\n\t\t\t\t4, 8 );\r\n\t\tnext_bit += 4;\r\n\t}\r\n}\r\nvoid stbi_decode_DXT45_alpha_block(\r\n\t\t\tunsigned char uncompressed[16*4],\r\n\t\t\tunsigned char compressed[8] )\r\n{\r\n\tint i, next_bit = 8*2;\r\n\tunsigned char decode_alpha[8];\r\n\t//\teach alpha value gets 3 bits, and the 1st 2 bytes are the range\r\n\tdecode_alpha[0] = compressed[0];\r\n\tdecode_alpha[1] = compressed[1];\r\n\tif( decode_alpha[0] > decode_alpha[1] )\r\n\t{\r\n\t\t//\t6 step intermediate\r\n\t\tdecode_alpha[2] = (6*decode_alpha[0] + 1*decode_alpha[1]) / 7;\r\n\t\tdecode_alpha[3] = (5*decode_alpha[0] + 2*decode_alpha[1]) / 7;\r\n\t\tdecode_alpha[4] = (4*decode_alpha[0] + 3*decode_alpha[1]) / 7;\r\n\t\tdecode_alpha[5] = (3*decode_alpha[0] + 4*decode_alpha[1]) / 7;\r\n\t\tdecode_alpha[6] = (2*decode_alpha[0] + 5*decode_alpha[1]) / 7;\r\n\t\tdecode_alpha[7] = (1*decode_alpha[0] + 6*decode_alpha[1]) / 7;\r\n\t} else\r\n\t{\r\n\t\t//\t4 step intermediate, pluss full and none\r\n\t\tdecode_alpha[2] = (4*decode_alpha[0] + 1*decode_alpha[1]) / 5;\r\n\t\tdecode_alpha[3] = (3*decode_alpha[0] + 2*decode_alpha[1]) / 5;\r\n\t\tdecode_alpha[4] = (2*decode_alpha[0] + 3*decode_alpha[1]) / 5;\r\n\t\tdecode_alpha[5] = (1*decode_alpha[0] + 4*decode_alpha[1]) / 5;\r\n\t\tdecode_alpha[6] = 0;\r\n\t\tdecode_alpha[7] = 255;\r\n\t}\r\n\tfor( i = 3; i < 16*4; i += 4 )\r\n\t{\r\n\t\tint idx = 0, bit;\r\n\t\tbit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;\r\n\t\tidx += bit << 0;\r\n\t\t++next_bit;\r\n\t\tbit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;\r\n\t\tidx += bit << 1;\r\n\t\t++next_bit;\r\n\t\tbit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;\r\n\t\tidx += bit << 2;\r\n\t\t++next_bit;\r\n\t\tuncompressed[i] = decode_alpha[idx & 7];\r\n\t}\r\n\t//\tdone\r\n}\r\nvoid stbi_decode_DXT_color_block(\r\n\t\t\tunsigned char uncompressed[16*4],\r\n\t\t\tunsigned char compressed[8] )\r\n{\r\n\tint next_bit = 4*8;\r\n\tint i, r, g, b;\r\n\tint c0, c1;\r\n\tunsigned char decode_colors[4*3];\r\n\t//\tfind the 2 primary colors\r\n\tc0 = compressed[0] + (compressed[1] << 8);\r\n\tc1 = compressed[2] + (compressed[3] << 8);\r\n\tstbi_rgb_888_from_565( c0, &r, &g, &b );\r\n\tdecode_colors[0] = r;\r\n\tdecode_colors[1] = g;\r\n\tdecode_colors[2] = b;\r\n\tstbi_rgb_888_from_565( c1, &r, &g, &b );\r\n\tdecode_colors[3] = r;\r\n\tdecode_colors[4] = g;\r\n\tdecode_colors[5] = b;\r\n\t//\tLike DXT1, but no choicees:\r\n\t//\tno alpha, 2 interpolated colors\r\n\tdecode_colors[6] = (2*decode_colors[0] + decode_colors[3]) / 3;\r\n\tdecode_colors[7] = (2*decode_colors[1] + decode_colors[4]) / 3;\r\n\tdecode_colors[8] = (2*decode_colors[2] + decode_colors[5]) / 3;\r\n\tdecode_colors[9] = (decode_colors[0] + 2*decode_colors[3]) / 3;\r\n\tdecode_colors[10] = (decode_colors[1] + 2*decode_colors[4]) / 3;\r\n\tdecode_colors[11] = (decode_colors[2] + 2*decode_colors[5]) / 3;\r\n\t//\tdecode the block\r\n\tfor( i = 0; i < 16*4; i += 4 )\r\n\t{\r\n\t\tint idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 3;\r\n\t\tnext_bit += 2;\r\n\t\tuncompressed[i+0] = decode_colors[idx+0];\r\n\t\tuncompressed[i+1] = decode_colors[idx+1];\r\n\t\tuncompressed[i+2] = decode_colors[idx+2];\r\n\t}\r\n\t//\tdone\r\n}\r\nstatic stbi_uc *dds_load(stbi *s, int *x, int *y, int *comp, int req_comp)\r\n{\r\n\t//\tall variables go up front\r\n\tstbi_uc *dds_data = NULL;\r\n\tstbi_uc block[16*4];\r\n\tstbi_uc compressed[8];\r\n\tint flags, DXT_family;\r\n\tint has_alpha, has_mipmap;\r\n\tint is_compressed, cubemap_faces;\r\n\tint block_pitch, num_blocks;\r\n\tDDS_header header;\r\n\tint i, sz, cf;\r\n\t//\tload the header\r\n\tif( sizeof( DDS_header ) != 128 )\r\n\t{\r\n\t\treturn NULL;\r\n\t}\r\n\tgetn( s, (stbi_uc*)(&header), 128 );\r\n\t//\tand do some checking\r\n\tif( header.dwMagic != (('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24)) ) return NULL;\r\n\tif( header.dwSize != 124 ) return NULL;\r\n\tflags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;\r\n\tif( (header.dwFlags & flags) != flags ) return NULL;\r\n\t/*\tAccording to the MSDN spec, the dwFlags should contain\r\n\t\tDDSD_LINEARSIZE if it's compressed, or DDSD_PITCH if\r\n\t\tuncompressed.  Some DDS writers do not conform to the\r\n\t\tspec, so I need to make my reader more tolerant\t*/\r\n\tif( header.sPixelFormat.dwSize != 32 ) return NULL;\r\n\tflags = DDPF_FOURCC | DDPF_RGB;\r\n\tif( (header.sPixelFormat.dwFlags & flags) == 0 ) return NULL;\r\n\tif( (header.sCaps.dwCaps1 & DDSCAPS_TEXTURE) == 0 ) return NULL;\r\n\t//\tget the image data\r\n\ts->img_x = header.dwWidth;\r\n\ts->img_y = header.dwHeight;\r\n\ts->img_n = 4;\r\n\tis_compressed = (header.sPixelFormat.dwFlags & DDPF_FOURCC) / DDPF_FOURCC;\r\n\thas_alpha = (header.sPixelFormat.dwFlags & DDPF_ALPHAPIXELS) / DDPF_ALPHAPIXELS;\r\n\thas_mipmap = (header.sCaps.dwCaps1 & DDSCAPS_MIPMAP) && (header.dwMipMapCount > 1);\r\n\tcubemap_faces = (header.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP) / DDSCAPS2_CUBEMAP;\r\n\t/*\tI need cubemaps to have square faces\t*/\r\n\tcubemap_faces &= (s->img_x == s->img_y);\r\n\tcubemap_faces *= 5;\r\n\tcubemap_faces += 1;\r\n\tblock_pitch = (s->img_x+3) >> 2;\r\n\tnum_blocks = block_pitch * ((s->img_y+3) >> 2);\r\n\t/*\tlet the user know what's going on\t*/\r\n\t*x = s->img_x;\r\n\t*y = s->img_y;\r\n\t*comp = s->img_n;\r\n\t/*\tis this uncompressed?\t*/\r\n\tif( is_compressed )\r\n\t{\r\n\t\t/*\tcompressed\t*/\r\n\t\t//\tnote: header.sPixelFormat.dwFourCC is something like (('D'<<0)|('X'<<8)|('T'<<16)|('1'<<24))\r\n\t\tDXT_family = 1 + (header.sPixelFormat.dwFourCC >> 24) - '1';\r\n\t\tif( (DXT_family < 1) || (DXT_family > 5) ) return NULL;\r\n\t\t/*\tcheck the expected size...oops, nevermind...\r\n\t\t\tthose non-compliant writers leave\r\n\t\t\tdwPitchOrLinearSize == 0\t*/\r\n\t\t//\tpassed all the tests, get the RAM for decoding\r\n\t\tsz = (s->img_x)*(s->img_y)*4*cubemap_faces;\r\n\t\tdds_data = (unsigned char*)malloc( sz );\r\n\t\t/*\tdo this once for each face\t*/\r\n\t\tfor( cf = 0; cf < cubemap_faces; ++ cf )\r\n\t\t{\r\n\t\t\t//\tnow read and decode all the blocks\r\n\t\t\tfor( i = 0; i < num_blocks; ++i )\r\n\t\t\t{\r\n\t\t\t\t//\twhere are we?\r\n\t\t\t\tint bx, by, bw=4, bh=4;\r\n\t\t\t\tint ref_x = 4 * (i % block_pitch);\r\n\t\t\t\tint ref_y = 4 * (i / block_pitch);\r\n\t\t\t\t//\tget the next block's worth of compressed data, and decompress it\r\n\t\t\t\tif( DXT_family == 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\t//\tDXT1\r\n\t\t\t\t\tgetn( s, compressed, 8 );\r\n\t\t\t\t\tstbi_decode_DXT1_block( block, compressed );\r\n\t\t\t\t} else if( DXT_family < 4 )\r\n\t\t\t\t{\r\n\t\t\t\t\t//\tDXT2/3\r\n\t\t\t\t\tgetn( s, compressed, 8 );\r\n\t\t\t\t\tstbi_decode_DXT23_alpha_block ( block, compressed );\r\n\t\t\t\t\tgetn( s, compressed, 8 );\r\n\t\t\t\t\tstbi_decode_DXT_color_block ( block, compressed );\r\n\t\t\t\t} else\r\n\t\t\t\t{\r\n\t\t\t\t\t//\tDXT4/5\r\n\t\t\t\t\tgetn( s, compressed, 8 );\r\n\t\t\t\t\tstbi_decode_DXT45_alpha_block ( block, compressed );\r\n\t\t\t\t\tgetn( s, compressed, 8 );\r\n\t\t\t\t\tstbi_decode_DXT_color_block ( block, compressed );\r\n\t\t\t\t}\r\n\t\t\t\t//\tis this a partial block?\r\n\t\t\t\tif( ref_x + 4 > s->img_x )\r\n\t\t\t\t{\r\n\t\t\t\t\tbw = s->img_x - ref_x;\r\n\t\t\t\t}\r\n\t\t\t\tif( ref_y + 4 > s->img_y )\r\n\t\t\t\t{\r\n\t\t\t\t\tbh = s->img_y - ref_y;\r\n\t\t\t\t}\r\n\t\t\t\t//\tnow drop our decompressed data into the buffer\r\n\t\t\t\tfor( by = 0; by < bh; ++by )\r\n\t\t\t\t{\r\n\t\t\t\t\tint idx = 4*((ref_y+by+cf*s->img_x)*s->img_x + ref_x);\r\n\t\t\t\t\tfor( bx = 0; bx < bw*4; ++bx )\r\n\t\t\t\t\t{\r\n\r\n\t\t\t\t\t\tdds_data[idx+bx] = block[by*16+bx];\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t/*\tdone reading and decoding the main image...\r\n\t\t\t\tskip MIPmaps if present\t*/\r\n\t\t\tif( has_mipmap )\r\n\t\t\t{\r\n\t\t\t\tint block_size = 16;\r\n\t\t\t\tif( DXT_family == 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\tblock_size = 8;\r\n\t\t\t\t}\r\n\t\t\t\tfor( i = 1; i < header.dwMipMapCount; ++i )\r\n\t\t\t\t{\r\n\t\t\t\t\tint mx = s->img_x >> (i + 2);\r\n\t\t\t\t\tint my = s->img_y >> (i + 2);\r\n\t\t\t\t\tif( mx < 1 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tmx = 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif( my < 1 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tmy = 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tskip( s, mx*my*block_size );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}/* per cubemap face */\r\n\t} else\r\n\t{\r\n\t\t/*\tuncompressed\t*/\r\n\t\tDXT_family = 0;\r\n\t\ts->img_n = 3;\r\n\t\tif( has_alpha )\r\n\t\t{\r\n\t\t\ts->img_n = 4;\r\n\t\t}\r\n\t\t*comp = s->img_n;\r\n\t\tsz = s->img_x*s->img_y*s->img_n*cubemap_faces;\r\n\t\tdds_data = (unsigned char*)malloc( sz );\r\n\t\t/*\tdo this once for each face\t*/\r\n\t\tfor( cf = 0; cf < cubemap_faces; ++ cf )\r\n\t\t{\r\n\t\t\t/*\tread the main image for this face\t*/\r\n\t\t\tgetn( s, &dds_data[cf*s->img_x*s->img_y*s->img_n], s->img_x*s->img_y*s->img_n );\r\n\t\t\t/*\tdone reading and decoding the main image...\r\n\t\t\t\tskip MIPmaps if present\t*/\r\n\t\t\tif( has_mipmap )\r\n\t\t\t{\r\n\t\t\t\tfor( i = 1; i < header.dwMipMapCount; ++i )\r\n\t\t\t\t{\r\n\t\t\t\t\tint mx = s->img_x >> i;\r\n\t\t\t\t\tint my = s->img_y >> i;\r\n\t\t\t\t\tif( mx < 1 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tmx = 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif( my < 1 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tmy = 1;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tskip( s, mx*my*s->img_n );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t/*\tdata was BGR, I need it RGB\t*/\r\n\t\tfor( i = 0; i < sz; i += s->img_n )\r\n\t\t{\r\n\t\t\tunsigned char temp = dds_data[i];\r\n\t\t\tdds_data[i] = dds_data[i+2];\r\n\t\t\tdds_data[i+2] = temp;\r\n\t\t}\r\n\t}\r\n\t/*\tfinished decompressing into RGBA,\r\n\t\tadjust the y size if we have a cubemap\r\n\t\tnote: sz is already up to date\t*/\r\n\ts->img_y *= cubemap_faces;\r\n\t*y = s->img_y;\r\n\t//\tdid the user want something else, or\r\n\t//\tsee if all the alpha values are 255 (i.e. no transparency)\r\n\thas_alpha = 0;\r\n\tif( s->img_n == 4)\r\n\t{\r\n\t\tfor( i = 3; (i < sz) && (has_alpha == 0); i += 4 )\r\n\t\t{\r\n\t\t\thas_alpha |= (dds_data[i] < 255);\r\n\t\t}\r\n\t}\r\n\tif( (req_comp <= 4) && (req_comp >= 1) )\r\n\t{\r\n\t\t//\tuser has some requirements, meet them\r\n\t\tif( req_comp != s->img_n )\r\n\t\t{\r\n\t\t\tdds_data = convert_format( dds_data, s->img_n, req_comp, s->img_x, s->img_y );\r\n\t\t\t*comp = s->img_n;\r\n\t\t}\r\n\t} else\r\n\t{\r\n\t\t//\tuser had no requirements, only drop to RGB is no alpha\r\n\t\tif( (has_alpha == 0) && (s->img_n == 4) )\r\n\t\t{\r\n\t\t\tdds_data = convert_format( dds_data, 4, 3, s->img_x, s->img_y );\r\n\t\t\t*comp = 3;\r\n\t\t}\r\n\t}\r\n\t//\tOK, done\r\n\treturn dds_data;\r\n}\r\n\r\n#ifndef STBI_NO_STDIO\r\nstbi_uc *stbi_dds_load_from_file   (FILE *f,                  int *x, int *y, int *comp, int req_comp)\r\n{\r\n\tstbi s;\r\n   start_file(&s,f);\r\n   return dds_load(&s,x,y,comp,req_comp);\r\n}\r\n\r\nstbi_uc *stbi_dds_load             (char *filename,           int *x, int *y, int *comp, int req_comp)\r\n{\r\n   stbi_uc *data;\r\n   FILE *f = fopen(filename, \"rb\");\r\n   if (!f) return NULL;\r\n   data = stbi_dds_load_from_file(f,x,y,comp,req_comp);\r\n   fclose(f);\r\n   return data;\r\n}\r\n#endif\r\n\r\nstbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)\r\n{\r\n\tstbi s;\r\n   start_mem(&s,buffer, len);\r\n   return dds_load(&s,x,y,comp,req_comp);\r\n}\r\n"
  },
  {
    "path": "src/test_SOIL.cpp",
    "content": "#include <string>\r\n#include <iostream>\r\n\r\n#include <windows.h>\r\n#include <shellapi.h>\r\n#include <gl/gl.h>\r\n#include <gl/glext.h>\r\n\r\n#include \"SOIL.h\"\r\n\r\nLRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);\r\nvoid EnableOpenGL(HWND hwnd, HDC*, HGLRC*);\r\nvoid DisableOpenGL(HWND, HDC, HGLRC);\r\n\r\nint WINAPI WinMain(HINSTANCE hInstance,\r\n                   HINSTANCE hPrevInstance,\r\n                   LPSTR lpCmdLine,\r\n                   int nCmdShow)\r\n{\r\n    WNDCLASSEX wcex;\r\n    HWND hwnd;\r\n    HDC hDC;\r\n    HGLRC hRC;\r\n    MSG msg;\r\n    BOOL bQuit = FALSE;\r\n    float theta = 0.0f;\r\n\r\n    // register window class\r\n    wcex.cbSize = sizeof(WNDCLASSEX);\r\n    wcex.style = CS_OWNDC;\r\n    wcex.lpfnWndProc = WindowProc;\r\n    wcex.cbClsExtra = 0;\r\n    wcex.cbWndExtra = 0;\r\n    wcex.hInstance = hInstance;\r\n    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);\r\n    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);\r\n    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);\r\n    wcex.lpszMenuName = NULL;\r\n    wcex.lpszClassName = \"GLSample\";\r\n    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);\r\n\r\n\r\n    if (!RegisterClassEx(&wcex))\r\n        return 0;\r\n\r\n    // create main window\r\n    hwnd = CreateWindowEx(0,\r\n                          \"GLSample\",\r\n                          \"SOIL Sample\",\r\n                          WS_OVERLAPPEDWINDOW,\r\n                          CW_USEDEFAULT,\r\n                          CW_USEDEFAULT,\r\n                          512,\r\n                          512,\r\n                          NULL,\r\n                          NULL,\r\n                          hInstance,\r\n                          NULL);\r\n\r\n    ShowWindow(hwnd, nCmdShow);\r\n\r\n    //\tcheck my error handling\r\n    /*\r\n    SOIL_load_OGL_texture( \"img_test.png\", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 0 );\r\n    std::cout << \"'\" << SOIL_last_result() << \"'\" << std::endl;\r\n    */\r\n\r\n\r\n    // enable OpenGL for the window\r\n    EnableOpenGL(hwnd, &hDC, &hRC);\r\n\r\n    glEnable( GL_BLEND );\r\n    //glDisable( GL_BLEND );\r\n    //\tstraight alpha\r\n    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );\r\n    //\tpremultiplied alpha (remember to do the same in glColor!!)\r\n    //glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );\r\n\r\n    //\tdo I want alpha thresholding?\r\n    glEnable( GL_ALPHA_TEST );\r\n    glAlphaFunc( GL_GREATER, 0.5f );\r\n\r\n    //\tlog what the use is asking us to load\r\n    std::string load_me = lpCmdLine;\r\n    if( load_me.length() > 2 )\r\n    {\r\n\t\t//load_me = load_me.substr( 1, load_me.length() - 2 );\r\n\t\tload_me = load_me.substr( 0, load_me.length() - 0 );\r\n    } else\r\n    {\r\n    \t//load_me = \"img_test_uncompressed.dds\";\r\n    \t//load_me = \"img_test_indexed.tga\";\r\n    \t//load_me = \"img_test.dds\";\r\n    \tload_me = \"img_test.png\";\r\n    \t//load_me = \"odd_size.jpg\";\r\n    \t//load_me = \"img_cheryl.jpg\";\r\n    \t//load_me = \"oak_odd.png\";\r\n    \t//load_me = \"field_128_cube.dds\";\r\n    \t//load_me = \"field_128_cube_nomip.dds\";\r\n    \t//load_me = \"field_128_cube_uc.dds\";\r\n    \t//load_me = \"field_128_cube_uc_nomip.dds\";\r\n    \t//load_me = \"Goblin.dds\";\r\n    \t//load_me = \"parquet.dds\";\r\n    \t//load_me = \"stpeters_probe.hdr\";\r\n    \t//load_me = \"VeraMoBI_sdf.png\";\r\n\r\n    \t//\tfor testing the texture rectangle code\r\n    \t//load_me = \"test_rect.png\";\r\n    }\r\n\tstd::cout << \"'\" << load_me << \"'\" << std::endl;\r\n\r\n\t//\t1st try to load it as a single-image-cubemap\r\n\t//\t(note, need DDS ordered faces: \"EWUDNS\")\r\n\tGLuint tex_ID;\r\n    int time_me;\r\n\r\n    std::cout << \"Attempting to load as a cubemap\" << std::endl;\r\n    time_me = clock();\r\n\ttex_ID = SOIL_load_OGL_single_cubemap(\r\n\t\t\tload_me.c_str(),\r\n\t\t\tSOIL_DDS_CUBEMAP_FACE_ORDER,\r\n\t\t\tSOIL_LOAD_AUTO,\r\n\t\t\tSOIL_CREATE_NEW_ID,\r\n\t\t\tSOIL_FLAG_POWER_OF_TWO\r\n\t\t\t| SOIL_FLAG_MIPMAPS\r\n\t\t\t//| SOIL_FLAG_COMPRESS_TO_DXT\r\n\t\t\t//| SOIL_FLAG_TEXTURE_REPEATS\r\n\t\t\t//| SOIL_FLAG_INVERT_Y\r\n\t\t\t| SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\t\t);\r\n\ttime_me = clock() - time_me;\r\n\tstd::cout << \"the load time was \" << 0.001f * time_me << \" seconds (warning: low resolution timer)\" << std::endl;\r\n    if( tex_ID > 0 )\r\n    {\r\n    \tglEnable( GL_TEXTURE_CUBE_MAP );\r\n\t\tglEnable( GL_TEXTURE_GEN_S );\r\n\t\tglEnable( GL_TEXTURE_GEN_T );\r\n\t\tglEnable( GL_TEXTURE_GEN_R );\r\n\t\tglTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );\r\n\t\tglTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );\r\n\t\tglTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );\r\n\t\tglBindTexture( GL_TEXTURE_CUBE_MAP, tex_ID );\r\n\t\t//\treport\r\n\t\tstd::cout << \"the loaded single cube map ID was \" << tex_ID << std::endl;\r\n\t\t//std::cout << \"the load time was \" << 0.001f * time_me << \" seconds (warning: low resolution timer)\" << std::endl;\r\n    } else\r\n    {\r\n    \tstd::cout << \"Attempting to load as a HDR texture\" << std::endl;\r\n\t\ttime_me = clock();\r\n\t\ttex_ID = SOIL_load_OGL_HDR_texture(\r\n\t\t\t\tload_me.c_str(),\r\n\t\t\t\t//SOIL_HDR_RGBE,\r\n\t\t\t\t//SOIL_HDR_RGBdivA,\r\n\t\t\t\tSOIL_HDR_RGBdivA2,\r\n\t\t\t\t0,\r\n\t\t\t\tSOIL_CREATE_NEW_ID,\r\n\t\t\t\tSOIL_FLAG_POWER_OF_TWO\r\n\t\t\t\t| SOIL_FLAG_MIPMAPS\r\n\t\t\t\t//| SOIL_FLAG_COMPRESS_TO_DXT\r\n\t\t\t\t);\r\n\t\ttime_me = clock() - time_me;\r\n\t\tstd::cout << \"the load time was \" << 0.001f * time_me << \" seconds (warning: low resolution timer)\" << std::endl;\r\n\r\n\t\t//\tdid I fail?\r\n\t\tif( tex_ID < 1 )\r\n\t\t{\r\n\t\t\t//\tloading of the single-image-cubemap failed, try it as a simple texture\r\n\t\t\tstd::cout << \"Attempting to load as a simple 2D texture\" << std::endl;\r\n\t\t\t//\tload the texture, if specified\r\n\t\t\ttime_me = clock();\r\n\t\t\ttex_ID = SOIL_load_OGL_texture(\r\n\t\t\t\t\tload_me.c_str(),\r\n\t\t\t\t\tSOIL_LOAD_AUTO,\r\n\t\t\t\t\tSOIL_CREATE_NEW_ID,\r\n\t\t\t\t\tSOIL_FLAG_POWER_OF_TWO\r\n\t\t\t\t\t| SOIL_FLAG_MIPMAPS\r\n\t\t\t\t\t//| SOIL_FLAG_MULTIPLY_ALPHA\r\n\t\t\t\t\t//| SOIL_FLAG_COMPRESS_TO_DXT\r\n\t\t\t\t\t| SOIL_FLAG_DDS_LOAD_DIRECT\r\n\t\t\t\t\t//| SOIL_FLAG_NTSC_SAFE_RGB\r\n\t\t\t\t\t//| SOIL_FLAG_CoCg_Y\r\n\t\t\t\t\t//| SOIL_FLAG_TEXTURE_RECTANGLE\r\n\t\t\t\t\t);\r\n\t\t\ttime_me = clock() - time_me;\r\n\t\t\tstd::cout << \"the load time was \" << 0.001f * time_me << \" seconds (warning: low resolution timer)\" << std::endl;\r\n\t\t}\r\n\r\n\t\tif( tex_ID > 0 )\r\n\t\t{\r\n\t\t\t//\tenable texturing\r\n\t\t\tglEnable( GL_TEXTURE_2D );\r\n\t\t\t//glEnable( 0x84F5 );// enables texture rectangle\r\n\t\t\t//  bind an OpenGL texture ID\r\n\t\t\tglBindTexture( GL_TEXTURE_2D, tex_ID );\r\n\t\t\t//\treport\r\n\t\t\tstd::cout << \"the loaded texture ID was \" << tex_ID << std::endl;\r\n\t\t\t//std::cout << \"the load time was \" << 0.001f * time_me << \" seconds (warning: low resolution timer)\" << std::endl;\r\n\t\t} else\r\n\t\t{\r\n\t\t\t//\tloading of the texture failed...why?\r\n\t\t\tglDisable( GL_TEXTURE_2D );\r\n\t\t\tstd::cout << \"Texture loading failed: '\" << SOIL_last_result() << \"'\" << std::endl;\r\n\t\t}\r\n    }\r\n\r\n    // program main loop\r\n    const float ref_mag = 0.1f;\r\n    while (!bQuit)\r\n    {\r\n        // check for messages\r\n        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))\r\n        {\r\n            // handle or dispatch messages\r\n            if (msg.message == WM_QUIT)\r\n            {\r\n                bQuit = TRUE;\r\n            }\r\n            else\r\n            {\r\n                TranslateMessage(&msg);\r\n                DispatchMessage(&msg);\r\n            }\r\n        }\r\n        else\r\n        {\r\n            // OpenGL animation code goes here\r\n            theta = clock() * 0.1;\r\n\r\n            float tex_u_max = 1.0f;//0.2f;\r\n            float tex_v_max = 1.0f;//0.2f;\r\n\r\n            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r\n            glClear(GL_COLOR_BUFFER_BIT);\r\n\r\n            glPushMatrix();\r\n            glScalef( 0.8f, 0.8f, 0.8f );\r\n            //glRotatef(-0.314159f*theta, 0.0f, 0.0f, 1.0f);\r\n\t\t\tglColor4f( 1.0f, 1.0f, 1.0f, 1.0f );\r\n\t\t\tglNormal3f( 0.0f, 0.0f, 1.0f );\r\n            glBegin(GL_QUADS);\r\n\t\t\t\tglNormal3f( -ref_mag, -ref_mag, 1.0f );\r\n                glTexCoord2f( 0.0f, tex_v_max );\r\n                glVertex3f( -1.0f, -1.0f, -0.1f );\r\n\r\n                glNormal3f( ref_mag, -ref_mag, 1.0f );\r\n                glTexCoord2f( tex_u_max, tex_v_max );\r\n                glVertex3f( 1.0f, -1.0f, -0.1f );\r\n\r\n                glNormal3f( ref_mag, ref_mag, 1.0f );\r\n                glTexCoord2f( tex_u_max, 0.0f );\r\n                glVertex3f( 1.0f, 1.0f, -0.1f );\r\n\r\n                glNormal3f( -ref_mag, ref_mag, 1.0f );\r\n                glTexCoord2f( 0.0f, 0.0f );\r\n                glVertex3f( -1.0f, 1.0f, -0.1f );\r\n            glEnd();\r\n            glPopMatrix();\r\n\r\n\t\t\ttex_u_max = 1.0f;\r\n            tex_v_max = 1.0f;\r\n            glPushMatrix();\r\n            glScalef( 0.8f, 0.8f, 0.8f );\r\n            glRotatef(theta, 0.0f, 0.0f, 1.0f);\r\n\t\t\tglColor4f( 1.0f, 1.0f, 1.0f, 1.0f );\r\n\t\t\tglNormal3f( 0.0f, 0.0f, 1.0f );\r\n            glBegin(GL_QUADS);\r\n                glTexCoord2f( 0.0f, tex_v_max );\t\tglVertex3f( 0.0f, 0.0f, 0.1f );\r\n                glTexCoord2f( tex_u_max, tex_v_max );\t\tglVertex3f( 1.0f, 0.0f, 0.1f );\r\n                glTexCoord2f( tex_u_max, 0.0f );\t\tglVertex3f( 1.0f, 1.0f, 0.1f );\r\n                glTexCoord2f( 0.0f, 0.0f );\t\tglVertex3f( 0.0f, 1.0f, 0.1f );\r\n            glEnd();\r\n            glPopMatrix();\r\n\r\n            {\r\n\t\t\t\t/*\tcheck for errors\t*/\r\n\t\t\t\tGLenum err_code = glGetError();\r\n\t\t\t\twhile( GL_NO_ERROR != err_code )\r\n\t\t\t\t{\r\n\t\t\t\t\tprintf( \"OpenGL Error @ %s: %i\", \"drawing loop\", err_code );\r\n\t\t\t\t\terr_code = glGetError();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n            SwapBuffers(hDC);\r\n\r\n            Sleep (1);\r\n        }\r\n    }\r\n\r\n    //\tand show off the screenshot capability\r\n    /*\r\n    load_me += \"-screenshot.tga\";\r\n    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_TGA, 0, 0, 512, 512 );\r\n    //*/\r\n    //*\r\n    load_me += \"-screenshot.bmp\";\r\n    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_BMP, 0, 0, 512, 512 );\r\n    //*/\r\n    /*\r\n    load_me += \"-screenshot.dds\";\r\n    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_DDS, 0, 0, 512, 512 );\r\n    //*/\r\n\r\n    // shutdown OpenGL\r\n    DisableOpenGL(hwnd, hDC, hRC);\r\n\r\n    // destroy the window explicitly\r\n    DestroyWindow(hwnd);\r\n\r\n    return msg.wParam;\r\n}\r\n\r\nLRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\r\n{\r\n    switch (uMsg)\r\n    {\r\n        case WM_CLOSE:\r\n            PostQuitMessage(0);\r\n        break;\r\n\r\n        case WM_DESTROY:\r\n            return 0;\r\n\r\n        case WM_KEYDOWN:\r\n        {\r\n            switch (wParam)\r\n            {\r\n                case VK_ESCAPE:\r\n                    PostQuitMessage(0);\r\n                break;\r\n            }\r\n        }\r\n        break;\r\n\r\n        default:\r\n            return DefWindowProc(hwnd, uMsg, wParam, lParam);\r\n    }\r\n\r\n    return 0;\r\n}\r\n\r\nvoid EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)\r\n{\r\n    PIXELFORMATDESCRIPTOR pfd;\r\n\r\n    int iFormat;\r\n\r\n    /* get the device context (DC) */\r\n    *hDC = GetDC(hwnd);\r\n\r\n    /* set the pixel format for the DC */\r\n    ZeroMemory(&pfd, sizeof(pfd));\r\n\r\n    pfd.nSize = sizeof(pfd);\r\n    pfd.nVersion = 1;\r\n    pfd.dwFlags = PFD_DRAW_TO_WINDOW |\r\n                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;\r\n    pfd.iPixelType = PFD_TYPE_RGBA;\r\n    pfd.cColorBits = 24;\r\n    pfd.cDepthBits = 16;\r\n    pfd.iLayerType = PFD_MAIN_PLANE;\r\n\r\n    iFormat = ChoosePixelFormat(*hDC, &pfd);\r\n\r\n    SetPixelFormat(*hDC, iFormat, &pfd);\r\n\r\n    /* create and enable the render context (RC) */\r\n    *hRC = wglCreateContext(*hDC);\r\n\r\n    wglMakeCurrent(*hDC, *hRC);\r\n}\r\n\r\nvoid DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)\r\n{\r\n    wglMakeCurrent(NULL, NULL);\r\n    wglDeleteContext(hRC);\r\n    ReleaseDC(hwnd, hDC);\r\n}\r\n\r\n"
  }
]